Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AmbientLight share bug #910

Merged
merged 5 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
106 changes: 69 additions & 37 deletions packages/core/src/lighting/AmbientLight.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;

/**
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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));
}
}
}

Expand All @@ -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);
}
}

/**
Expand All @@ -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);
}
}

Expand All @@ -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 {
Expand Down