From 8c63420387254a72a3f9d6678392ee09817cb74a Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 1 Aug 2022 21:12:36 +0800 Subject: [PATCH 1/5] fix: ambientLight share bug --- packages/core/src/Scene.ts | 4 +- packages/core/src/lighting/AmbientLight.ts | 106 ++++++++++++++------- 2 files changed, 71 insertions(+), 39 deletions(-) diff --git a/packages/core/src/Scene.ts b/packages/core/src/Scene.ts index 6904c69311..ba1dd5220d 100644 --- a/packages/core/src/Scene.ts +++ b/packages/core/src/Scene.ts @@ -51,8 +51,8 @@ export class Scene extends EngineObject { const lastAmbientLight = this._ambientLight; if (lastAmbientLight !== value) { - lastAmbientLight && lastAmbientLight._setScene(null); - value._setScene(this); + lastAmbientLight && lastAmbientLight._removeScene(this); + value._addScene(this); this._ambientLight = value; } } diff --git a/packages/core/src/lighting/AmbientLight.ts b/packages/core/src/lighting/AmbientLight.ts index d6ab55236a..1aec84a8a3 100644 --- a/packages/core/src/lighting/AmbientLight.ts +++ b/packages/core/src/lighting/AmbientLight.ts @@ -1,6 +1,6 @@ import { Color, SphericalHarmonics3 } from "@oasis-engine/math"; import { Scene } from "../Scene"; -import { Shader } from "../shader"; +import { Shader, ShaderData } from "../shader"; import { ShaderMacro } from "../shader/ShaderMacro"; import { ShaderProperty } from "../shader/ShaderProperty"; import { TextureCube } from "../texture"; @@ -30,7 +30,7 @@ export class AmbientLight { private _specularIntensity: number = 1.0; private _diffuseMode: DiffuseMode = DiffuseMode.SolidColor; private _shArray: Float32Array = new Float32Array(27); - private _scene: Scene; + private _scenes: Scene[] = []; private _specularTextureDecodeRGBM: boolean = false; /** @@ -42,12 +42,10 @@ export class AmbientLight { set specularTextureDecodeRGBM(value: boolean) { this._specularTextureDecodeRGBM = value; - if (!this._scene) return; - if (value) { - this._scene.shaderData.enableMacro(AmbientLight._decodeRGBMMacro); - } else { - this._scene.shaderData.disableMacro(AmbientLight._decodeRGBMMacro); + const scenes = this._scenes; + for (let i = 0, n = scenes.length; i < n; i++) { + this._setSpecularTextureDecodeRGBM(scenes[i].shaderData); } } @@ -60,12 +58,10 @@ export class AmbientLight { set diffuseMode(value: DiffuseMode) { this._diffuseMode = value; - if (!this._scene) return; - if (value === DiffuseMode.SphericalHarmonics) { - this._scene.shaderData.enableMacro(AmbientLight._shMacro); - } else { - this._scene.shaderData.disableMacro(AmbientLight._shMacro); + const scenes = this._scenes; + for (let i = 0, n = scenes.length; i < n; i++) { + this._setDiffuseMode(scenes[i].shaderData); } } @@ -93,10 +89,12 @@ export class AmbientLight { set diffuseSphericalHarmonics(value: SphericalHarmonics3) { this._diffuseSphericalHarmonics = value; - if (!this._scene) return; if (value) { - this._scene.shaderData.setFloatArray(AmbientLight._diffuseSHProperty, this._preComputeSH(value, this._shArray)); + const scenes = this._scenes; + for (let i = 0, n = scenes.length; i < n; i++) { + scenes[i].shaderData.setFloatArray(AmbientLight._diffuseSHProperty, this._preComputeSH(value, this._shArray)); + } } } @@ -109,9 +107,11 @@ export class AmbientLight { set diffuseIntensity(value: number) { this._diffuseIntensity = value; - if (!this._scene) return; - this._scene.shaderData.setFloat(AmbientLight._diffuseIntensityProperty, value); + const scenes = this._scenes; + for (let i = 0, n = scenes.length; i < n; i++) { + scenes[i].shaderData.setFloat(AmbientLight._diffuseIntensityProperty, value); + } } /** @@ -124,16 +124,10 @@ export class AmbientLight { set specularTexture(value: TextureCube) { this._specularReflection = value; - if (!this._scene) return; - - const shaderData = this._scene.shaderData; - if (value) { - shaderData.setTexture(AmbientLight._specularTextureProperty, value); - shaderData.setFloat(AmbientLight._mipLevelProperty, this._specularReflection.mipmapCount - 1); - shaderData.enableMacro(AmbientLight._specularMacro); - } else { - shaderData.disableMacro(AmbientLight._specularMacro); + const scenes = this._scenes; + for (let i = 0, n = scenes.length; i < n; i++) { + this._setSpecularTexture(scenes[i].shaderData); } } @@ -146,27 +140,65 @@ export class AmbientLight { set specularIntensity(value: number) { this._specularIntensity = value; - if (!this._scene) return; - this._scene.shaderData.setFloat(AmbientLight._specularIntensityProperty, value); + for (let i = 0, n = this._scenes.length; i < n; i++) { + this._scenes[i].shaderData.setFloat(AmbientLight._specularIntensityProperty, value); + } } /** * @internal */ - _setScene(value: Scene) { - this._scene = value; - if (!value) return; + _addScene(scene: Scene): void { + this._scenes.push(scene); - const { shaderData } = value; + const shaderData = scene.shaderData; shaderData.setColor(AmbientLight._diffuseColorProperty, this._diffuseSolidColor); + shaderData.setFloat(AmbientLight._diffuseIntensityProperty, this._diffuseIntensity); + shaderData.setFloat(AmbientLight._specularIntensityProperty, this._specularIntensity); + shaderData.setFloatArray( + AmbientLight._diffuseSHProperty, + this._preComputeSH(this._diffuseSphericalHarmonics, this._shArray) + ); + + this._setDiffuseMode(shaderData); + this._setSpecularTextureDecodeRGBM(shaderData); + this._setSpecularTexture(shaderData); + } + + /** + * @internal + */ + _removeScene(scene: Scene): void { + const scenes = this._scenes; + const index = scenes.indexOf(scene); + scenes.splice(index, 1); + } + + private _setDiffuseMode(sceneShaderData: ShaderData): void { + if (this._diffuseMode === DiffuseMode.SphericalHarmonics) { + sceneShaderData.enableMacro(AmbientLight._shMacro); + } else { + sceneShaderData.disableMacro(AmbientLight._shMacro); + } + } - this.diffuseMode = this._diffuseMode; - this.diffuseSphericalHarmonics = this._diffuseSphericalHarmonics; - this.diffuseIntensity = this._diffuseIntensity; - this.specularTexture = this._specularReflection; - this.specularIntensity = this._specularIntensity; - this.specularTextureDecodeRGBM = this._specularTextureDecodeRGBM; + private _setSpecularTexture(sceneShaderData: ShaderData): void { + if (this._specularReflection) { + sceneShaderData.setTexture(AmbientLight._specularTextureProperty, this._specularReflection); + sceneShaderData.setFloat(AmbientLight._mipLevelProperty, this._specularReflection.mipmapCount - 1); + sceneShaderData.enableMacro(AmbientLight._specularMacro); + } else { + sceneShaderData.disableMacro(AmbientLight._specularMacro); + } + } + + private _setSpecularTextureDecodeRGBM(sceneShaderData: ShaderData): void { + if (this._specularTextureDecodeRGBM) { + sceneShaderData.enableMacro(AmbientLight._decodeRGBMMacro); + } else { + sceneShaderData.disableMacro(AmbientLight._decodeRGBMMacro); + } } private _preComputeSH(sh: SphericalHarmonics3, out: Float32Array): Float32Array { From b954a164dc9b5ded9c83118b1420ead16a5fdc1e Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 1 Aug 2022 21:30:31 +0800 Subject: [PATCH 2/5] refactaor: opt code --- packages/core/src/lighting/AmbientLight.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/src/lighting/AmbientLight.ts b/packages/core/src/lighting/AmbientLight.ts index 1aec84a8a3..c3e2dba546 100644 --- a/packages/core/src/lighting/AmbientLight.ts +++ b/packages/core/src/lighting/AmbientLight.ts @@ -92,8 +92,9 @@ export class AmbientLight { if (value) { const scenes = this._scenes; + const sh = this._preComputeSH(value, this._shArray); for (let i = 0, n = scenes.length; i < n; i++) { - scenes[i].shaderData.setFloatArray(AmbientLight._diffuseSHProperty, this._preComputeSH(value, this._shArray)); + scenes[i].shaderData.setFloatArray(AmbientLight._diffuseSHProperty, sh); } } } From f9e66ed54f2cb897d1646eb05c729843a352bca2 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 1 Aug 2022 21:33:06 +0800 Subject: [PATCH 3/5] refactor: opt code --- packages/core/src/Scene.ts | 4 ++-- packages/core/src/lighting/AmbientLight.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/src/Scene.ts b/packages/core/src/Scene.ts index ba1dd5220d..62b8a3dd75 100644 --- a/packages/core/src/Scene.ts +++ b/packages/core/src/Scene.ts @@ -51,8 +51,8 @@ export class Scene extends EngineObject { const lastAmbientLight = this._ambientLight; if (lastAmbientLight !== value) { - lastAmbientLight && lastAmbientLight._removeScene(this); - value._addScene(this); + lastAmbientLight && lastAmbientLight._removeFromScene(this); + value._addToScene(this); this._ambientLight = value; } } diff --git a/packages/core/src/lighting/AmbientLight.ts b/packages/core/src/lighting/AmbientLight.ts index c3e2dba546..ac95f8ee5a 100644 --- a/packages/core/src/lighting/AmbientLight.ts +++ b/packages/core/src/lighting/AmbientLight.ts @@ -150,7 +150,7 @@ export class AmbientLight { /** * @internal */ - _addScene(scene: Scene): void { + _addToScene(scene: Scene): void { this._scenes.push(scene); const shaderData = scene.shaderData; @@ -170,7 +170,7 @@ export class AmbientLight { /** * @internal */ - _removeScene(scene: Scene): void { + _removeFromScene(scene: Scene): void { const scenes = this._scenes; const index = scenes.indexOf(scene); scenes.splice(index, 1); From b6261afdacca5aa55145eff0cd5fdc0bab2de208 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 1 Aug 2022 21:35:31 +0800 Subject: [PATCH 4/5] refactor: opt code --- packages/core/src/lighting/AmbientLight.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/packages/core/src/lighting/AmbientLight.ts b/packages/core/src/lighting/AmbientLight.ts index ac95f8ee5a..78bebd3018 100644 --- a/packages/core/src/lighting/AmbientLight.ts +++ b/packages/core/src/lighting/AmbientLight.ts @@ -89,12 +89,11 @@ export class AmbientLight { set diffuseSphericalHarmonics(value: SphericalHarmonics3) { this._diffuseSphericalHarmonics = value; - if (value) { + this._preComputeSH(value, this._shArray); const scenes = this._scenes; - const sh = this._preComputeSH(value, this._shArray); for (let i = 0, n = scenes.length; i < n; i++) { - scenes[i].shaderData.setFloatArray(AmbientLight._diffuseSHProperty, sh); + scenes[i].shaderData.setFloatArray(AmbientLight._diffuseSHProperty, this._shArray); } } } @@ -157,10 +156,7 @@ export class AmbientLight { shaderData.setColor(AmbientLight._diffuseColorProperty, this._diffuseSolidColor); shaderData.setFloat(AmbientLight._diffuseIntensityProperty, this._diffuseIntensity); shaderData.setFloat(AmbientLight._specularIntensityProperty, this._specularIntensity); - shaderData.setFloatArray( - AmbientLight._diffuseSHProperty, - this._preComputeSH(this._diffuseSphericalHarmonics, this._shArray) - ); + shaderData.setFloatArray(AmbientLight._diffuseSHProperty, this._shArray); this._setDiffuseMode(shaderData); this._setSpecularTextureDecodeRGBM(shaderData); @@ -202,7 +198,7 @@ export class AmbientLight { } } - private _preComputeSH(sh: SphericalHarmonics3, out: Float32Array): Float32Array { + private _preComputeSH(sh: SphericalHarmonics3, out: Float32Array): void { /** * Basis constants * @@ -261,7 +257,5 @@ export class AmbientLight { out[24] = src[24] * 0.429042; // kernel2 * basis8 = 0.429042 out[25] = src[25] * 0.429042; out[26] = src[26] * 0.429042; - - return out; } } From cc493037e7a2d3025d7c42195993d8a013185d1e Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Mon, 1 Aug 2022 21:38:14 +0800 Subject: [PATCH 5/5] refactor: opt code --- packages/core/src/lighting/AmbientLight.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/core/src/lighting/AmbientLight.ts b/packages/core/src/lighting/AmbientLight.ts index 78bebd3018..5dbcb7aa3c 100644 --- a/packages/core/src/lighting/AmbientLight.ts +++ b/packages/core/src/lighting/AmbientLight.ts @@ -26,7 +26,7 @@ export class AmbientLight { private _diffuseSphericalHarmonics: SphericalHarmonics3; private _diffuseSolidColor: Color = new Color(0.212, 0.227, 0.259); private _diffuseIntensity: number = 1.0; - private _specularReflection: TextureCube; + private _specularTexture: TextureCube; private _specularIntensity: number = 1.0; private _diffuseMode: DiffuseMode = DiffuseMode.SolidColor; private _shArray: Float32Array = new Float32Array(27); @@ -119,11 +119,11 @@ export class AmbientLight { * @remarks This texture must be baked from @oasis-engine/baker */ get specularTexture(): TextureCube { - return this._specularReflection; + return this._specularTexture; } set specularTexture(value: TextureCube) { - this._specularReflection = value; + this._specularTexture = value; const scenes = this._scenes; for (let i = 0, n = scenes.length; i < n; i++) { @@ -181,9 +181,9 @@ export class AmbientLight { } private _setSpecularTexture(sceneShaderData: ShaderData): void { - if (this._specularReflection) { - sceneShaderData.setTexture(AmbientLight._specularTextureProperty, this._specularReflection); - sceneShaderData.setFloat(AmbientLight._mipLevelProperty, this._specularReflection.mipmapCount - 1); + if (this._specularTexture) { + sceneShaderData.setTexture(AmbientLight._specularTextureProperty, this._specularTexture); + sceneShaderData.setFloat(AmbientLight._mipLevelProperty, this._specularTexture.mipmapCount - 1); sceneShaderData.enableMacro(AmbientLight._specularMacro); } else { sceneShaderData.disableMacro(AmbientLight._specularMacro);