From e92cdbcc0364d8ddcb2d447afca7259b8b88ff4a Mon Sep 17 00:00:00 2001 From: JoneLau <32630749+JoneLau@users.noreply.github.com> Date: Wed, 24 Apr 2019 17:45:31 +0800 Subject: [PATCH] fix render-twice (#4294) --- cocos/3d/ui/components/canvas-component.ts | 2 +- .../ui/components/debug-canvas-component.ts | 39 +++++++++++++++++-- cocos/core/utils/profiler/profiler.ts | 1 - cocos/renderer/ui/ui.ts | 14 +++++++ 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index dcf160e800..8993d7d531 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -43,7 +43,7 @@ const _worldPos = new Vec3(); * @extends Component */ @ccclass('cc.CanvasComponent') -@executionOrder(100) +@executionOrder(110) @requireComponent(UITransformComponent) @menu('UI/Canvas') @executeInEditMode diff --git a/cocos/3d/ui/components/debug-canvas-component.ts b/cocos/3d/ui/components/debug-canvas-component.ts index 274d6b9a33..1c4a8202a2 100644 --- a/cocos/3d/ui/components/debug-canvas-component.ts +++ b/cocos/3d/ui/components/debug-canvas-component.ts @@ -1,4 +1,6 @@ import { Vec3 } from '../../../core/value-types'; +import { GFXClearFlag } from '../../../gfx/define'; +import { CameraComponent } from '../../framework'; import { CanvasComponent } from './canvas-component'; const _worldPos = new Vec3(); @@ -7,6 +9,34 @@ export class DebugCanvasComponent extends CanvasComponent{ super(); this._thisOnResized = this.alignWithScreen.bind(this); } + public __preload (){ + const cameraNode = new cc.Node('UICamera'); + cameraNode.setPosition(0, 0, 1000); + + this._camera = cc.director.root.ui.renderScene.createCamera({ + name: 'ui', + node: cameraNode, + projection: CameraComponent.ProjectionType.ORTHO, + targetDisplay: 0, + priority: this._priority, + isUI: true, + flows: ['UIFlow'], + }); + + this._camera!.fov = 45; + this._camera!.clearFlag = GFXClearFlag.COLOR | GFXClearFlag.DEPTH | GFXClearFlag.STENCIL; + + const device = cc.director.root.device; + this._camera!.resize(device.width, device.height); + + cc.view.on('design-resolution-changed', this._thisOnResized); + + this.alignWithScreen(); + + if (cc.director.root && cc.director.root.ui) { + cc.director.root.ui.debugScreen = this; + } + } // don't remove public onEnable (){ @@ -16,12 +46,15 @@ export class DebugCanvasComponent extends CanvasComponent{ public onDisable (){ } - public onDestroy (){ - if (CC_EDITOR) { - cc.director.off(cc.Director.EVENT_AFTER_UPDATE, this.alignWithScreen, this); + public onDestroy () { + if (this._camera) { + this._getRenderScene().destroyCamera(this._camera); } cc.view.off('design-resolution-changed', this._thisOnResized); + if (cc.director.root && cc.director.root.ui) { + cc.director.root.ui.debugScreen = null; + } } public alignWithScreen (){ diff --git a/cocos/core/utils/profiler/profiler.ts b/cocos/core/utils/profiler/profiler.ts index efad7c8819..e6024a1cc6 100644 --- a/cocos/core/utils/profiler/profiler.ts +++ b/cocos/core/utils/profiler/profiler.ts @@ -172,7 +172,6 @@ function generateNode () { _rootNode = new Node('PROFILER-NODE'); _rootNode.addComponent(UITransformComponent); const screen = _rootNode.addComponent(DebugCanvasComponent); - cc.director.root.ui.debugScreen = screen; cc.game.addPersistRootNode(_rootNode); // const camera = cc.director.root. diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index 1c7f54d53b..b204f4ca90 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -80,6 +80,9 @@ export class UI { set debugScreen (value){ this._debugScreen = value; + if (this._debugScreen && this._debugScreen.camera ) { + this._debugScreen.camera.view.visibility = this._screens.length + 1; + } } public device: GFXDevice; @@ -186,6 +189,10 @@ export class UI { comp.camera.view.visibility = this._screens.length; } + if (this._debugScreen && this._debugScreen.camera) { + this._debugScreen.camera.view.visibility = this._screens.length + 1; + } + } public getScreen (visibility: number) { @@ -197,6 +204,10 @@ export class UI { } } + if (this._debugScreen && this._debugScreen.camera && this._debugScreen.camera.visibility === visibility) { + return this._debugScreen; + } + return null; } @@ -207,6 +218,9 @@ export class UI { } this._screens.splice(idx, 1); + if (this._debugScreen && this._debugScreen.camera ) { + this._debugScreen.camera.view.visibility = this._screens.length + 1; + } let camera: Camera | null; for (let i = idx; i < this._screens.length;) {