directional light stack (#6813)

* directional light stack

* hasChangedFlags: bitwise or instead of direct assignment
This commit is contained in:
Yun Hsiao Wu
2020-06-03 15:02:57 +08:00
committed by GitHub
parent a0a7cc2f0b
commit 9ca7c2ce6e
7 changed files with 51 additions and 24 deletions
+1 -1
View File
@@ -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;
+11 -6
View File
@@ -50,6 +50,13 @@ export enum LightBakeType {
}
Enum(LightBakeType);
// tslint:disable: no-shadowed-variable
export namespace LightComponent {
export type Type = EnumAlias<typeof LightType>;
export type PhotometricTerm = EnumAlias<typeof PhotometricTerm>;
}
// 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<typeof LightType>;
export type PhotometricTerm = EnumAlias<typeof PhotometricTerm>;
}
+3 -2
View File
@@ -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;
+1 -1
View File
@@ -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;
}
}
+28 -3
View File
@@ -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) {
+6 -10
View File
@@ -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<Function, Pool<any>> = new Map<Function, Pool<any>>();
private _modelPools = new Map<Constructor<Model>, Pool<Model>>();
private _cameraPool: Pool<Camera> | null = null;
private _lightPools: Map<Function, Pool<any>> = new Map<Function, Pool<any>>();
private _directLightPool: Pool<DirectionalLight> | null = null;
private _sphereLightPool: Pool<SphereLight> | null = null;
private _spotLightPool: Pool<SpotLight> | null = null;
private _lightPools = new Map<Constructor<Light>, Pool<Light>>();
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<Model>);
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<Light>);
l.destroy();
if (p) {
p.free(l);
+1 -1
View File
@@ -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); }
}
}