Skip to content

Commit

Permalink
feat: support ambient-light replacement (galacean#569)
Browse files Browse the repository at this point in the history
* feat: support ambient-light replacement

Co-authored-by: shensi.zxd <shensi.zxd@alibaba-inc.com>
Co-authored-by: GuoLei <gl3336563@163.com>
  • Loading branch information
3 people authored Nov 9, 2021
1 parent f3f23e5 commit b2d6a8e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 11 deletions.
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

0 comments on commit b2d6a8e

Please sign in to comment.