From ae2dbaf04f2e232cc2f9fa9a32c427ef61d2a500 Mon Sep 17 00:00:00 2001 From: zxg0622 <247274249@qq.com> Date: Tue, 28 May 2019 20:23:43 +0800 Subject: [PATCH] fix ui model bug (#4512) --- cocos/3d/framework/model-component.ts | 1 + cocos/3d/ui/components/canvas-component.ts | 4 ++-- .../ui/components/debug-canvas-component.ts | 4 ++-- cocos/3d/ui/components/ui-component.ts | 22 +++++++++++++++++++ cocos/3d/ui/components/ui-model-component.ts | 13 ++++++----- cocos/3d/ui/components/ui-render-component.ts | 18 ++------------- 6 files changed, 37 insertions(+), 25 deletions(-) diff --git a/cocos/3d/framework/model-component.ts b/cocos/3d/framework/model-component.ts index 47f659b1db..e873b9d677 100644 --- a/cocos/3d/framework/model-component.ts +++ b/cocos/3d/framework/model-component.ts @@ -193,6 +193,7 @@ export class ModelComponent extends RenderableComponent { public recreateModel () { if (this._model) { this._model.destroy(); + this._model.scene.destroyModel(this._model); this._model = null; } this._updateModels(); diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index 9d91b0823f..ab8de510f2 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -172,11 +172,11 @@ export class CanvasComponent extends Component { } public __preload () { - const cameraNode = new cc.Node('UICamera' + this.node.name); + const cameraNode = new cc.Node('UICamera_' + this.node.name); cameraNode.setPosition(0, 0, 900); if (!CC_EDITOR) { this._camera = cc.director.root.ui.renderScene.createCamera({ - name: 'ui' + this.node.name, + name: 'ui_' + this.node.name, node: cameraNode, projection: cc.CameraComponent.ProjectionType.ORTHO, targetDisplay: 0, diff --git a/cocos/3d/ui/components/debug-canvas-component.ts b/cocos/3d/ui/components/debug-canvas-component.ts index 5638e1aa1d..31f8fc63cc 100644 --- a/cocos/3d/ui/components/debug-canvas-component.ts +++ b/cocos/3d/ui/components/debug-canvas-component.ts @@ -20,11 +20,11 @@ export class DebugCanvasComponent extends CanvasComponent { } public __preload (){ - const cameraNode = new cc.Node('UICamera'); + const cameraNode = new cc.Node('UICamera_Debug'); cameraNode.setPosition(0, 0, 1000); this._camera = cc.director.root.ui.renderScene.createCamera({ - name: 'ui', + name: 'ui_Debug', node: cameraNode, projection: cc.CameraComponent.ProjectionType.ORTHO, targetDisplay: 0, diff --git a/cocos/3d/ui/components/ui-component.ts b/cocos/3d/ui/components/ui-component.ts index ea347a44a9..12fb25f719 100644 --- a/cocos/3d/ui/components/ui-component.ts +++ b/cocos/3d/ui/components/ui-component.ts @@ -1,6 +1,7 @@ import { Component } from '../../../components'; import { ccclass, executeInEditMode, executionOrder, property } from '../../../core/data/class-decorator'; import { UI } from '../../../renderer/ui/ui'; +import { CanvasComponent } from './canvas-component'; /** * @zh @@ -41,6 +42,27 @@ export class UIComponent extends Component { protected _visibility = -1; + public onEnable () { + let parent = this.node; + // 获取被渲染相机的 visibility + while (parent) { + if (parent) { + const canvasComp = parent.getComponent(CanvasComponent); + if (canvasComp) { + this._visibility = canvasComp.visibility; + break; + } + } + + // @ts-ignore + parent = parent.parent; + } + } + + public onDisable () { + this._visibility = -1; + } + public updateAssembler (render: UI) { } diff --git a/cocos/3d/ui/components/ui-model-component.ts b/cocos/3d/ui/components/ui-model-component.ts index 2315367ff1..c8cd45d2f3 100644 --- a/cocos/3d/ui/components/ui-model-component.ts +++ b/cocos/3d/ui/components/ui-model-component.ts @@ -26,10 +26,6 @@ export class UIModelComponent extends UIComponent { this._modelComponent.recreateModel(); } - public onEnable () { - this._fitUIRenderQueue(); - } - public onDestroy () { this._modelComponent = this.getComponent(cc.RenderableComponent) as RenderableComponent; (this._modelComponent as any)._sceneGetter = null; @@ -56,6 +52,9 @@ export class UIModelComponent extends UIComponent { const matNum = this._modelComponent.sharedMaterials.length; for (let i = 0; i < matNum; i++) { const material = this._modelComponent.getMaterial(i, CC_EDITOR)! as any; + if (material == null) { + continue; + } const passes = material.passes; const ea = material.effectAsset!; const techIdx = material.technique; @@ -74,7 +73,11 @@ export class UIModelComponent extends UIComponent { } } for (let i = 0; i < matNum; i++) { - const passes = this._modelComponent.getMaterial(i, CC_EDITOR)!.passes; + const material = this._modelComponent.getMaterial(i, CC_EDITOR)!; + if (material == null) { + continue; + } + const passes = material.passes; for (const p of passes as any[]) { p._priority = RenderPriority.MAX - 11; } diff --git a/cocos/3d/ui/components/ui-render-component.ts b/cocos/3d/ui/components/ui-render-component.ts index 19040b9051..43ace9a2cf 100644 --- a/cocos/3d/ui/components/ui-render-component.ts +++ b/cocos/3d/ui/components/ui-render-component.ts @@ -232,21 +232,7 @@ export class UIRenderComponent extends UIComponent { } public onEnable () { - let parent = this.node; - // 获取被渲染相机的 visibility - while (parent) { - if (parent) { - const canvasComp = parent.getComponent(CanvasComponent); - if (canvasComp) { - this._visibility = canvasComp.visibility; - break; - } - } - - // @ts-ignore - parent = parent.parent; - } - + super.onEnable(); this.node.on(SystemEventType.ANCHOR_CHANGED, this._nodeStateChange, this); this.node.on(SystemEventType.TRANSFORM_CHANGED, this._nodeStateChange, this); this.node.on(SystemEventType.SIZE_CHANGED, this._nodeStateChange, this); @@ -259,7 +245,7 @@ export class UIRenderComponent extends UIComponent { } public onDisable () { - this._visibility = -1; + super.onDisable(); // this.node._renderComponent = null; // this.disableRender(); this.node.off(SystemEventType.ANCHOR_CHANGED, this._nodeStateChange, this);