diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index 21cb407a26..f7fdb08d8a 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -31,6 +31,7 @@ import Rect from '../../../core/value-types/rect'; import Size from '../../../core/value-types/size'; import { GFXClearFlag } from '../../../gfx/define'; import { Camera, ICameraInfo } from '../../../renderer/scene/camera'; +import { Node } from '../../../scene-graph/node'; /** * !#zh: 作为 UI 根节点,为所有子节点提供视窗四边的位置信息以供对齐,另外提供屏幕适配策略接口,方便从编辑器设置。 @@ -139,7 +140,6 @@ export class CanvasComponent extends Component { private _thisOnResized: ()=>void; private _camera: Camera | null = null; - private _cameraInfo: ICameraInfo | null = null; // /** // * !#en Current active canvas, the scene should only have one active canvas at the same time. @@ -161,9 +161,11 @@ export class CanvasComponent extends Component { } public __preload () { - this._cameraInfo = { + let cameraNode = new Node('UICamera'); + cameraNode.setPosition(0, 0, 1000); + this._camera = this._getRenderScene().createCamera({ name: 'ui', - node: this.node, + node: cameraNode, projection: CameraComponent.ProjectionType.ORTHO, fov: 45, stencil: 0, @@ -174,9 +176,10 @@ export class CanvasComponent extends Component { clearFlags: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, rect: new Rect(0, 0, 1, 1), depth: 1, + targetDisplay: 0, isUI: true, - } as ICameraInfo; - this._camera = this._getRenderScene().createCamera(this._cameraInfo); + }); + cc.director.root.ui.addScreen(this); // if (CC_DEV) { @@ -259,6 +262,12 @@ export class CanvasComponent extends Component { } this.node.width = nodeSize.width; this.node.height = nodeSize.height; + let nodeWorldPos = this.node.getWorldPosition(); + if (this._camera) { + this._camera.orthoHeight = this._camera.height; + this._camera.node.setPosition(nodeWorldPos.x, nodeWorldPos.y, this._camera.node.getWorldPosition().z); + this._camera.update(); + } } public applySettings () { diff --git a/cocos/3d/ui/mesh-buffer.ts b/cocos/3d/ui/mesh-buffer.ts index 35e439bfc0..36c00631fe 100644 --- a/cocos/3d/ui/mesh-buffer.ts +++ b/cocos/3d/ui/mesh-buffer.ts @@ -76,8 +76,8 @@ export class MeshBuffer { public initialize (data: IMeshBufferInitData) { this._vertexFormat = data.attributes; - this._vertexBytes = this.calculateBytes(this._vertexFormat); - this._initVDataCount = data.vertexCount * this._vertexBytes; + // this._vertexBytes = this.calculateBytes(this._vertexFormat); + this._initVDataCount = data.vertexCount * this._calculateFormatNum(this._vertexFormat); this._initIDataCount = data.indiceCount; this.primitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; this.byteOffset = 0; @@ -207,40 +207,40 @@ export class MeshBuffer { // this._vb.destroy(); } - private calculateBytes (vertexFormat: IGFXInputAttribute[]) { - let bytes = 0; + private _calculateFormatNum (vertexFormat: IGFXInputAttribute[]) { + // let bytes = 0; let num = 0; for (const attr of vertexFormat) { let name = GFXFormat[attr.format].toString(); if (name.startsWith('RGBA')) { - num = 4; + num += 4; } else if (name.startsWith('RGB')) { - num = 3; + num += 3; } else if (name.startsWith('RG')) { - num = 2; + num += 2; } else { - num = 1; + num += 1; } - name = name.substring(num); - switch (name) { - case '8I': - case '8UI': - bytes += num * 1; - break; - case '16I': - case '16UI': - bytes += num * 2; - break; - case '32I': - case '32UI': - case '32F': - bytes += num * 4; - break; - } + // name = name.substring(num); + // switch (name) { + // case '8I': + // case '8UI': + // bytes += num * 1; + // break; + // case '16I': + // case '16UI': + // bytes += num * 2; + // break; + // case '32I': + // case '32UI': + // case '32F': + // bytes += num * 4; + // break; + // } } - return bytes; + return num; } } cc.MeshBuffer = MeshBuffer; diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 85b490fb0c..cae98353cd 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -106,8 +106,8 @@ export class UI { private _cmdBuff: GFXCommandBuffer | null = null; private _renderArea: IGFXRect = { x: 0, y: 0, width: 0, height: 0 }; private _scene: RenderScene; - private _cameraNode: Node; - private _camera: Camera; + // private _cameraNode: Node; + // private _camera: Camera; private _attributes: IGFXInputAttribute[] = []; private _vertStride = 0; private _vertF32Count = 0; @@ -130,24 +130,24 @@ export class UI { name: 'GUIScene', }); - this._cameraNode = new Node('UICameraNode'); + // this._cameraNode = new Node('UICameraNode'); - this._camera = this._scene.createCamera({ - name: 'UICamera', - node: this._cameraNode, - projection: CameraProjection.ORTHO, - fov: 45, - stencil: 0, - orthoHeight: 10, - far: 4096, - near: 0.1, - color: cc.color(0, 0, 0, 255), - clearFlags: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, - rect: new Rect(0, 0, 1, 1), - depth: 1, - targetDisplay: 0, - isUI: true, - }); + // this._camera = this._scene.createCamera({ + // name: 'UICamera', + // node: this._cameraNode, + // projection: CameraProjection.ORTHO, + // fov: 45, + // stencil: 0, + // orthoHeight: 10, + // far: 4096, + // near: 0.1, + // color: cc.color(0, 0, 0, 255), + // clearFlags: GFXClearFlag.DEPTH | GFXClearFlag.STENCIL, + // rect: new Rect(0, 0, 1, 1), + // depth: 1, + // targetDisplay: 0, + // isUI: true, + // }); this._batches = new CachedArray(64); this._bufferInitData = { @@ -159,7 +159,7 @@ export class UI { this._vertF32Count = 9; this._vertStride = this._vertF32Count * 4; - this.resize(this._camera.width, this._camera.height); + // this.resize(this._camera.width, this._camera.height); } public initialize () { @@ -209,16 +209,16 @@ export class UI { } } - public resize (width: number, height: number) { - this._camera.orthoHeight = height; + // public resize (width: number, height: number) { + // this._camera.orthoHeight = height; - let cameraPos = this._camera.node.getWorldPosition(); - cameraPos.x = width * 0.5; - cameraPos.y = height * 0.5; - cameraPos.z = 1000.0; - this._camera.node.setPosition(cameraPos); - this._camera.update(); - } + // let cameraPos = this._camera.node.getWorldPosition(); + // cameraPos.x = width * 0.5; + // cameraPos.y = height * 0.5; + // cameraPos.z = 1000.0; + // this._camera.node.setPosition(cameraPos); + // this._camera.update(); + // } public createUIMaterial (info: IUIMaterialInfo): UIMaterial | null { const uiMtrl = new UIMaterial(); @@ -336,11 +336,11 @@ export class UI { if (curCamera) { cmdBuff.endRenderPass(); } - this._renderArea.width = this._camera.width; - this._renderArea.height = this._camera.height; + this._renderArea.width = camera.width; + this._renderArea.height = camera.height; // update ubo - mat4.array(_mat4Array, this._camera.matViewProj); + mat4.array(_mat4Array, camera.matViewProj); this._uboUI.view.set(_mat4Array, UBOUI.MAT_VIEW_PROJ_OFFSET); this._uiUBO!.update(this._uboUI.view); @@ -386,23 +386,26 @@ export class UI { // const len = node.children.length; fn1(renderComp); - renderCompList = this._defineNodeOrder(renderComp.node.children); - for (const comp of renderCompList){ - this._walk(comp, fn1, fn2, level); - } - // for (let i = 0; i < len; ++i) { - // const child = node.children[i]; - // this._walk(child, fn1, fn2, level); - // } + if (renderComp.node.children.length > 0) { + renderCompList = this._defineNodeOrder(renderComp.node.children); + for (const comp of renderCompList) { + this._walk(comp, fn1, fn2, level); + } + // for (let i = 0; i < len; ++i) { + // const child = node.children[i]; + // this._walk(child, fn1, fn2, level); + // } + + this._sortChildList.remove(renderCompList); + } - this._sortChildList.remove(renderCompList); fn2(renderComp); level += 1; } private _defineNodeOrder(childs: Node[]) { let sortList = this._sortChildList.add(); - sortList = []; + sortList.length = 0; childs.forEach((child) => { let renderComp = child.getComponent(UIRenderComponent);