Skip to content

Commit

Permalink
refcator: opt code (#4)
Browse files Browse the repository at this point in the history
* refcator: opt code
  • Loading branch information
GuoLei1990 authored Aug 6, 2021

Verified

This commit was signed with the committer’s verified signature.
fabpot Fabien Potencier
1 parent 441c885 commit f4b74a3
Showing 2 changed files with 208 additions and 202 deletions.
68 changes: 67 additions & 1 deletion packages/core/src/lighting/AmbientLight.ts
Original file line number Diff line number Diff line change
@@ -74,7 +74,10 @@ export class AmbientLight {
const shaderData = this._scene.shaderData;

if (value) {
shaderData.setFloatArray(AmbientLight._diffuseSHProperty, value.convertRadianceToIrradiance(this._shArray));
shaderData.setFloatArray(
AmbientLight._diffuseSHProperty,
this._convertRadianceToIrradiance(value, this._shArray)
);
}
}

@@ -131,4 +134,67 @@ export class AmbientLight {
shaderData.setFloat(AmbientLight._diffuseIntensityProperty, this._diffuseIntensity);
shaderData.setFloat(AmbientLight._specularIntensityProperty, this._specularIntensity);
}

private _convertRadianceToIrradiance(sh: SphericalHarmonics3, out: Float32Array): Float32Array {
/**
* Basis constants
*
* 0: 1/2 * Math.sqrt(1 / Math.PI)
*
* 1: -1/2 * Math.sqrt(3 / Math.PI)
* 2: 1/2 * Math.sqrt(3 / Math.PI)
* 3: -1/2 * Math.sqrt(3 / Math.PI)
*
* 4: 1/2 * Math.sqrt(15 / Math.PI)
* 5: -1/2 * Math.sqrt(15 / Math.PI)
* 6: 1/4 * Math.sqrt(5 / Math.PI)
* 7: -1/2 * Math.sqrt(15 / Math.PI)
* 8: 1/4 * Math.sqrt(15 / Math.PI)
*/

/**
* Convolution kernel
*
* 0: Math.PI
* 1: (2 * Math.PI) / 3
* 2: Math.PI / 4
*/

const src = sh.coefficients;

// l0
out[0] = src[0] * 0.886227; // kernel0 * basis0 = 0.886227
out[1] = src[1] * 0.886227;
out[2] = src[2] * 0.886227;

// l1
out[3] = src[3] * -1.023327; // kernel1 * basis1 = -1.023327;
out[4] = src[4] * -1.023327;
out[5] = src[5] * -1.023327;
out[6] = src[6] * 1.023327; // kernel1 * basis2 = 1.023327
out[7] = src[7] * 1.023327;
out[8] = src[8] * 1.023327;
out[9] = src[9] * -1.023327; // kernel1 * basis3 = -1.023327
out[10] = src[10] * -1.023327;
out[11] = src[11] * -1.023327;

// l2
out[12] = src[12] * 0.858086; // kernel2 * basis4 = 0.858086
out[13] = src[13] * 0.858086;
out[14] = src[14] * 0.858086;
out[15] = src[15] * -0.858086; // kernel2 * basis5 = -0.858086
out[16] = src[16] * -0.858086;
out[17] = src[17] * -0.858086;
out[18] = src[18] * 0.247708; // kernel2 * basis6 = 0.247708
out[19] = src[19] * 0.247708;
out[20] = src[20] * 0.247708;
out[21] = src[21] * -0.858086; // kernel2 * basis7 = -0.858086
out[22] = src[22] * -0.858086;
out[23] = src[23] * -0.858086;
out[24] = src[24] * 0.429042; // kernel2 * basis8 = 0.429042
out[25] = src[25] * 0.429042;
out[26] = src[26] * 0.429042;

return out;
}
}
Loading

0 comments on commit f4b74a3

Please sign in to comment.