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

feat: support ambient-light replacement #569

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 23 additions & 3 deletions packages/core/src/Scene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class Scene extends EngineObject {

/** Scene name. */
name: string;
/** Ambient light. */
ambientLight: AmbientLight;

/** The background of the scene. */
readonly background: Background = new Background(this._engine);
Expand All @@ -37,6 +35,28 @@ export class Scene extends EngineObject {

private _destroyed: boolean = false;
private _rootEntities: Entity[] = [];
private _ambientLight: AmbientLight;

/**
* Ambient light.
*/
get ambientLight(): AmbientLight {
return this._ambientLight;
}

set ambientLight(value: AmbientLight) {
if (!value) {
Logger.warn("The scene must have one ambient light");
return;
}

const lastAmbientLight = this._ambientLight;
if (lastAmbientLight !== value) {
lastAmbientLight && lastAmbientLight._setScene(null);
value._setScene(this);
this._ambientLight = value;
}
}

/**
* Count of root entities.
Expand Down Expand Up @@ -71,7 +91,7 @@ export class Scene extends EngineObject {
const shaderData = this.shaderData;
Scene.sceneFeatureManager.addObject(this);
shaderData._addRefCount(1);
this.ambientLight = new AmbientLight(this);
this.ambientLight = new AmbientLight();
}

/**
Expand Down
31 changes: 23 additions & 8 deletions packages/core/src/lighting/AmbientLight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export class AmbientLight {
);
private static _mipLevelProperty: ShaderProperty = Shader.getPropertyByName("u_envMapLight.mipMapLevel");

private _scene: Scene;
private _diffuseSphericalHarmonics: SphericalHarmonics3;
private _diffuseSolidColor: Color = new Color(0.212, 0.227, 0.259);
private _diffuseIntensity: number = 1.0;
private _specularReflection: TextureCubeMap;
private _specularIntensity: number = 1.0;
private _diffuseMode: DiffuseMode = DiffuseMode.SolidColor;
private _shArray: Float32Array = new Float32Array(27);
private _scene: Scene;

/**
* Diffuse mode of ambient light.
Expand All @@ -40,6 +40,8 @@ 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 {
Expand Down Expand Up @@ -71,10 +73,10 @@ export class AmbientLight {

set diffuseSphericalHarmonics(value: SphericalHarmonics3) {
this._diffuseSphericalHarmonics = value;
const shaderData = this._scene.shaderData;
if (!this._scene) return;

if (value) {
shaderData.setFloatArray(AmbientLight._diffuseSHProperty, this._preComputeSH(value, this._shArray));
this._scene.shaderData.setFloatArray(AmbientLight._diffuseSHProperty, this._preComputeSH(value, this._shArray));
}
}

Expand All @@ -87,6 +89,8 @@ export class AmbientLight {

set diffuseIntensity(value: number) {
this._diffuseIntensity = value;
if (!this._scene) return;

this._scene.shaderData.setFloat(AmbientLight._diffuseIntensityProperty, value);
}

Expand All @@ -100,6 +104,7 @@ export class AmbientLight {

set specularTexture(value: TextureCubeMap) {
this._specularReflection = value;
if (!this._scene) return;

const shaderData = this._scene.shaderData;

Expand All @@ -121,16 +126,26 @@ export class AmbientLight {

set specularIntensity(value: number) {
this._specularIntensity = value;
if (!this._scene) return;

this._scene.shaderData.setFloat(AmbientLight._specularIntensityProperty, value);
}

constructor(scene: Scene) {
this._scene = scene;
/**
* @internal
*/
_setScene(value: Scene) {
this._scene = value;
if (!value) return;

const { shaderData } = this._scene;
const { shaderData } = value;
shaderData.setColor(AmbientLight._diffuseColorProperty, this._diffuseSolidColor);
shaderData.setFloat(AmbientLight._diffuseIntensityProperty, this._diffuseIntensity);
shaderData.setFloat(AmbientLight._specularIntensityProperty, this._specularIntensity);

this.diffuseMode = this._diffuseMode;
this.diffuseSphericalHarmonics = this._diffuseSphericalHarmonics;
this.diffuseIntensity = this._diffuseIntensity;
this.specularTexture = this._specularReflection;
this.specularIntensity = this._specularIntensity;
}

private _preComputeSH(sh: SphericalHarmonics3, out: Float32Array): Float32Array {
Expand Down