From 9ca7c2ce6e403bcc62bb2df920ba065e149cc8f2 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Wed, 3 Jun 2020 15:02:57 +0800 Subject: [PATCH] directional light stack (#6813) * directional light stack * hasChangedFlags: bitwise or instead of direct assignment --- cocos/core/3d/framework/camera-component.ts | 2 +- cocos/core/3d/framework/light-component.ts | 17 +++++++---- cocos/core/3d/framework/model-component.ts | 5 ++-- cocos/core/renderer/scene/light.ts | 2 +- cocos/core/renderer/scene/render-scene.ts | 31 +++++++++++++++++++-- cocos/core/root.ts | 16 ++++------- cocos/core/scene-graph/node.ts | 2 +- 7 files changed, 51 insertions(+), 24 deletions(-) diff --git a/cocos/core/3d/framework/camera-component.ts b/cocos/core/3d/framework/camera-component.ts index 532ed4df89..d18529fb32 100644 --- a/cocos/core/3d/framework/camera-component.ts +++ b/cocos/core/3d/framework/camera-component.ts @@ -486,7 +486,7 @@ export class CameraComponent extends Component { } public onEnable () { - this.node.hasChangedFlags = TransformBit.POSITION; // trigger camera matrix update + this.node.hasChangedFlags |= TransformBit.POSITION; // trigger camera matrix update if (this._camera) { this._attachToScene(); return; diff --git a/cocos/core/3d/framework/light-component.ts b/cocos/core/3d/framework/light-component.ts index 6ff416ca78..9ce13de03c 100644 --- a/cocos/core/3d/framework/light-component.ts +++ b/cocos/core/3d/framework/light-component.ts @@ -50,6 +50,13 @@ export enum LightBakeType { } Enum(LightBakeType); +// tslint:disable: no-shadowed-variable +export namespace LightComponent { + export type Type = EnumAlias; + export type PhotometricTerm = EnumAlias; +} +// tslint:enable: no-shadowed-variable + @ccclass('cc.LightComponent') export class LightComponent extends Component { public static Type = LightType; @@ -197,6 +204,7 @@ export class LightComponent extends Component { if (this._light && !this._light.scene && this.node.scene) { switch (this._type) { case LightType.DIRECTIONAL: + this._getRenderScene().addDirectionalLight(this._light as DirectionalLight); this._getRenderScene().setMainLight(this._light as DirectionalLight); break; case LightType.SPHERE: @@ -213,7 +221,9 @@ export class LightComponent extends Component { if (this._light && this._light.scene) { switch (this._type) { case LightType.DIRECTIONAL: - this._light.scene.unsetMainLight(this._light as DirectionalLight); + const scene = this._light.scene; + scene.removeDirectionalLight(this._light as DirectionalLight); + scene.unsetMainLight(this._light as DirectionalLight); break; case LightType.SPHERE: this._light.scene.removeSphereLight(this._light as SphereLight); @@ -225,8 +235,3 @@ export class LightComponent extends Component { } } } - -export namespace LightComponent { - export type Type = EnumAlias; - export type PhotometricTerm = EnumAlias; -} diff --git a/cocos/core/3d/framework/model-component.ts b/cocos/core/3d/framework/model-component.ts index deedb858d0..01e4403da0 100644 --- a/cocos/core/3d/framework/model-component.ts +++ b/cocos/core/3d/framework/model-component.ts @@ -204,7 +204,7 @@ export class ModelComponent extends RenderableComponent { } @property({ - visible: function (this: ModelComponent) { + visible (this: ModelComponent) { return !!( this.mesh && this.mesh.struct.morph && @@ -344,7 +344,8 @@ export class ModelComponent extends RenderableComponent { protected _updateModelParams () { if (!this._mesh || !this._model) { return; } - this.node.hasChangedFlags = this._model.transform.hasChangedFlags = TransformBit.POSITION; + this.node.hasChangedFlags |= TransformBit.POSITION; + this._model.transform.hasChangedFlags |= TransformBit.POSITION; this._model.isDynamicBatching = this._isBatchingEnabled(); const meshCount = this._mesh ? this._mesh.subMeshCount : 0; const renderingMesh = this._mesh.renderingSubMeshes; diff --git a/cocos/core/renderer/scene/light.ts b/cocos/core/renderer/scene/light.ts index 39835830f8..0166818de1 100644 --- a/cocos/core/renderer/scene/light.ts +++ b/cocos/core/renderer/scene/light.ts @@ -73,7 +73,7 @@ export class Light { set node (n) { this._node = n; if (this._node) { - this._node.hasChangedFlags = TransformBit.ROTATION; + this._node.hasChangedFlags |= TransformBit.ROTATION; } } diff --git a/cocos/core/renderer/scene/render-scene.ts b/cocos/core/renderer/scene/render-scene.ts index 4aff4a5a46..6fe379d061 100644 --- a/cocos/core/renderer/scene/render-scene.ts +++ b/cocos/core/renderer/scene/render-scene.ts @@ -15,6 +15,7 @@ import { Skybox } from './skybox'; import { SphereLight } from './sphere-light'; import { SpotLight } from './spot-light'; import { PREVIEW } from 'internal:constants'; +import { TransformBit } from '../../scene-graph/node-enum'; export interface IRenderSceneInfo { name: string; @@ -116,6 +117,7 @@ export class RenderScene { private _skybox: Skybox; private _planarShadows: PlanarShadows; private _models: Model[] = []; + private _directionalLights: DirectionalLight[] = []; private _sphereLights: SphereLight[] = []; private _spotLights: SpotLight[] = []; private _mainLight: DirectionalLight | null = null; @@ -165,13 +167,36 @@ export class RenderScene { } public setMainLight (dl: DirectionalLight) { - dl.attachToScene(this); this._mainLight = dl; } public unsetMainLight (dl: DirectionalLight) { - dl.detachFromScene(); - this._mainLight = null; + if (this._mainLight === dl) { + const dlList = this._directionalLights; + if (dlList.length) { + this._mainLight = dlList[dlList.length - 1]; + if (this._mainLight.node) { // trigger update + this._mainLight.node.hasChangedFlags |= TransformBit.ROTATION; + } + } else { + this._mainLight = null; + } + } + } + + public addDirectionalLight (dl: DirectionalLight) { + dl.attachToScene(this); + this._directionalLights.push(dl); + } + + public removeDirectionalLight (dl: DirectionalLight) { + for (let i = 0; i < this._directionalLights.length; ++i) { + if (this._directionalLights[i] === dl) { + dl.detachFromScene(); + this._directionalLights.splice(i, 1); + return; + } + } } public addSphereLight (pl: SphereLight) { diff --git a/cocos/core/root.ts b/cocos/core/root.ts index 879387d0c0..385ee949c2 100644 --- a/cocos/core/root.ts +++ b/cocos/core/root.ts @@ -10,7 +10,6 @@ import { RenderPipeline } from './pipeline/render-pipeline'; import { IRenderViewInfo, RenderView } from './pipeline/render-view'; import { Camera, Light, Model } from './renderer'; import { DataPoolManager } from './renderer/data-pool-manager'; -import { DirectionalLight } from './renderer/scene/directional-light'; import { LightType } from './renderer/scene/light'; import { IRenderSceneInfo, RenderScene } from './renderer/scene/render-scene'; import { SphereLight } from './renderer/scene/sphere-light'; @@ -188,12 +187,9 @@ export class Root { private _dataPoolMgr: DataPoolManager; private _scenes: RenderScene[] = []; private _views: RenderView[] = []; - private _modelPools: Map> = new Map>(); + private _modelPools = new Map, Pool>(); private _cameraPool: Pool | null = null; - private _lightPools: Map> = new Map>(); - private _directLightPool: Pool | null = null; - private _sphereLightPool: Pool | null = null; - private _spotLightPool: Pool | null = null; + private _lightPools = new Map, Pool>(); private _time: number = 0; private _frameTime: number = 0; private _fpsTime: number = 0; @@ -490,11 +486,11 @@ export class Root { this._modelPools.set(mClass, new Pool(() => new mClass(), 10)); p = this._modelPools.get(mClass)!; } - return p.alloc(); + return p.alloc() as T; } public destroyModel (m: Model) { - const p = this._modelPools.get(m.constructor); + const p = this._modelPools.get(m.constructor as Constructor); if (p) { p.free(m); m.destroy(); @@ -525,11 +521,11 @@ export class Root { this._lightPools.set(lClass, new Pool(() => new lClass(), 4)); l = this._lightPools.get(lClass)!; } - return l.alloc(); + return l.alloc() as T; } public destroyLight (l: Light) { - const p = this._lightPools.get(l.constructor); + const p = this._lightPools.get(l.constructor as Constructor); l.destroy(); if (p) { p.free(l); diff --git a/cocos/core/scene-graph/node.ts b/cocos/core/scene-graph/node.ts index 2569efc60d..ae70bc4eb5 100644 --- a/cocos/core/scene-graph/node.ts +++ b/cocos/core/scene-graph/node.ts @@ -462,7 +462,7 @@ export class Node extends BaseNode { const len = this._children.length; for (let i = 0; i < len; ++i) { const child = this._children[i]; - child.isValid && child.invalidateChildren(dirtyBit); + if (child.isValid) { child.invalidateChildren(dirtyBit); } } }