From 6dfc30de74d389935c22e770ce8ae3b9e02ca257 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Tue, 29 Jan 2019 17:06:43 +0800 Subject: [PATCH] persist node change scene event --- cocos/3d/framework/camera-component.ts | 11 +++++++---- cocos/3d/framework/light-component.ts | 10 +++++++++- cocos/3d/framework/model-component.ts | 8 +++++++- cocos/core/director.js | 1 + cocos/renderer/scene/camera.ts | 9 +++++++++ cocos/scene-graph/node-event-enum.ts | 3 +++ 6 files changed, 36 insertions(+), 6 deletions(-) diff --git a/cocos/3d/framework/camera-component.ts b/cocos/3d/framework/camera-component.ts index f3fc42378d..43d5f136b9 100644 --- a/cocos/3d/framework/camera-component.ts +++ b/cocos/3d/framework/camera-component.ts @@ -285,19 +285,22 @@ export class CameraComponent extends Component { } public onLoad () { - + this.node.on(cc.Node.SCENE_CHANGED_FOR_PERSISTS, () => { + if (this._camera) { this._getRenderScene().destroyCamera(this._camera); this._camera = null; } + }); } public onEnable () { - this._camera = this._getRenderScene().createCamera(this); + if (!this._camera) { this._camera = this._getRenderScene().createCamera(this); } + this._camera.enabled = true; } public onDisable () { - if (this._camera) { this._getRenderScene().destroyCamera(this._camera); } + if (this._camera) { this._camera.enabled = false; } } public onDestroy () { - + if (this._camera) { this._getRenderScene().destroyCamera(this._camera); this._camera = null; } } public screenPointToRay (x: number, y: number, out?: ray) { diff --git a/cocos/3d/framework/light-component.ts b/cocos/3d/framework/light-component.ts index 56de33dff6..728c75ca31 100644 --- a/cocos/3d/framework/light-component.ts +++ b/cocos/3d/framework/light-component.ts @@ -250,6 +250,10 @@ export default class LightComponent extends Component { } } + public onLoad () { + this.node.on(cc.Node.SCENE_CHANGED_FOR_PERSISTS, () => this._destroyLight()); + } + public onEnable () { this._createLight(); if (this._light) { this._light.enabled = true; return; } @@ -283,8 +287,12 @@ export default class LightComponent extends Component { console.warn(`illegal light type ${this._type}`); return; } - if (!this._light) { console.warn('we don\'t support this many lights in forward pipeline.'); } + if (!this._light) { + console.warn('we don\'t support this many lights in forward pipeline.'); + return; + } this.color = this._color; + this._light.enabled = this.enabledInHierarchy; } protected _destroyLight () { diff --git a/cocos/3d/framework/model-component.ts b/cocos/3d/framework/model-component.ts index 2063beb14c..1cd72f1d5c 100644 --- a/cocos/3d/framework/model-component.ts +++ b/cocos/3d/framework/model-component.ts @@ -163,7 +163,12 @@ export class ModelComponent extends RenderableComponent { } public onLoad () { - super.onLoad(); + this.node.on(cc.Node.SCENE_CHANGED_FOR_PERSISTS, () => { + if (this._model) { + this._getRenderScene().destroyModel(this._model); + this._model = null; + } + }); } public onEnable () { @@ -184,6 +189,7 @@ export class ModelComponent extends RenderableComponent { public onDestroy () { if (this._model) { this._getRenderScene().destroyModel(this._model); + this._model = null; } } diff --git a/cocos/core/director.js b/cocos/core/director.js index d701c45a1c..f6cb76cb64 100644 --- a/cocos/core/director.js +++ b/cocos/core/director.js @@ -417,6 +417,7 @@ class Director extends EventTarget { else { node.parent = scene; } + node.emit(cc.Node.SCENE_CHANGED_FOR_PERSISTS); } CC_BUILD && CC_DEBUG && console.timeEnd('AttachPersist'); diff --git a/cocos/renderer/scene/camera.ts b/cocos/renderer/scene/camera.ts index 31698dab1d..84fbce08ce 100644 --- a/cocos/renderer/scene/camera.ts +++ b/cocos/renderer/scene/camera.ts @@ -35,6 +35,7 @@ export class Camera { private _scene: RenderScene; private _name: string; + private _enabled: boolean = false; private _proj: CameraProjection; private _isWindowSize: boolean = true; private _width: number; @@ -135,6 +136,14 @@ export class Camera { this._frustum.update(this._matViewProj, this._matViewProjInv); } + set enabled (val) { + this._enabled = val; + } + + get enabled () { + return this._enabled; + } + get view (): RenderView { return this._view; } diff --git a/cocos/scene-graph/node-event-enum.ts b/cocos/scene-graph/node-event-enum.ts index 02f37e9a68..56a1caa698 100644 --- a/cocos/scene-graph/node-event-enum.ts +++ b/cocos/scene-graph/node-event-enum.ts @@ -5,6 +5,9 @@ export enum EventType { POSITION_PART = 'position-part', ROTATION_PART = 'rotation-part', SCALE_PART = 'scale-part', + + SCENE_CHANGED_FOR_PERSISTS = 'scene-changed-for-persists', + TOUCH_START = 'touch-start', TOUCH_MOVE = 'touch-move', TOUCH_END = 'touch-end',