|
@@ -1,15 +1,57 @@
|
|
|
module BABYLON {
|
|
|
+ /**
|
|
|
+ * Class representing spherical polynomial coefficients to the 3rd degree
|
|
|
+ */
|
|
|
export class SphericalPolynomial {
|
|
|
+ /**
|
|
|
+ * The x coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public x: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The y coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public y: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The z coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public z: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The xx coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public xx: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The yy coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public yy: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The zz coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public zz: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The xy coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public xy: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The yz coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public yz: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The zx coefficients of the spherical polynomial
|
|
|
+ */
|
|
|
public zx: Vector3 = Vector3.Zero();
|
|
|
|
|
|
+ /**
|
|
|
+ * Adds an ambient color to the spherical polynomial
|
|
|
+ * @param color the color to add
|
|
|
+ */
|
|
|
public addAmbient(color: Color3): void {
|
|
|
var colorVector = new Vector3(color.r, color.g, color.b);
|
|
|
this.xx = this.xx.add(colorVector);
|
|
@@ -17,6 +59,10 @@ module BABYLON {
|
|
|
this.zz = this.zz.add(colorVector);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Scales the spherical polynomial by the given amount
|
|
|
+ * @param scale the amount to scale
|
|
|
+ */
|
|
|
public scale(scale: number)
|
|
|
{
|
|
|
this.x = this.x.scale(scale);
|
|
@@ -30,20 +76,25 @@ module BABYLON {
|
|
|
this.xy = this.xy.scale(scale);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the spherical polynomial from harmonics
|
|
|
+ * @param harmonics the spherical harmonics
|
|
|
+ * @returns the spherical polynomial
|
|
|
+ */
|
|
|
public static FromHarmonics(harmonics: SphericalHarmonics): SphericalPolynomial {
|
|
|
var result = new SphericalPolynomial();
|
|
|
|
|
|
- result.x = harmonics.L11.scale(1.02333);
|
|
|
- result.y = harmonics.L1_1.scale(1.02333);
|
|
|
- result.z = harmonics.L10.scale(1.02333);
|
|
|
+ result.x = harmonics.l11.scale(1.02333);
|
|
|
+ result.y = harmonics.l1_1.scale(1.02333);
|
|
|
+ result.z = harmonics.l10.scale(1.02333);
|
|
|
|
|
|
- result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
|
|
|
- result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
|
|
|
- result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
|
|
|
+ result.xx = harmonics.l00.scale(0.886277).subtract(harmonics.l20.scale(0.247708)).add(harmonics.lL22.scale(0.429043));
|
|
|
+ result.yy = harmonics.l00.scale(0.886277).subtract(harmonics.l20.scale(0.247708)).subtract(harmonics.lL22.scale(0.429043));
|
|
|
+ result.zz = harmonics.l00.scale(0.886277).add(harmonics.l20.scale(0.495417));
|
|
|
|
|
|
- result.yz = harmonics.L2_1.scale(0.858086);
|
|
|
- result.zx = harmonics.L21.scale(0.858086);
|
|
|
- result.xy = harmonics.L2_2.scale(0.858086);
|
|
|
+ result.yz = harmonics.l2_1.scale(0.858086);
|
|
|
+ result.zx = harmonics.l21.scale(0.858086);
|
|
|
+ result.xy = harmonics.l2_2.scale(0.858086);
|
|
|
|
|
|
result.scale(1.0 / Math.PI);
|
|
|
|
|
@@ -53,6 +104,7 @@ module BABYLON {
|
|
|
/**
|
|
|
* Constructs a spherical polynomial from an array.
|
|
|
* @param data defines the 9x3 coefficients (x, y, z, xx, yy, zz, yz, zx, xy)
|
|
|
+ * @returns the spherical polynomial
|
|
|
*/
|
|
|
public static FromArray(data: ArrayLike<ArrayLike<number>>): SphericalPolynomial {
|
|
|
const sp = new SphericalPolynomial();
|
|
@@ -69,99 +121,159 @@ module BABYLON {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Class representing spherical harmonics coefficients to the 3rd degree
|
|
|
+ */
|
|
|
export class SphericalHarmonics {
|
|
|
- public L00: Vector3 = Vector3.Zero();
|
|
|
- public L1_1: Vector3 = Vector3.Zero();
|
|
|
- public L10: Vector3 = Vector3.Zero();
|
|
|
- public L11: Vector3 = Vector3.Zero();
|
|
|
- public L2_2: Vector3 = Vector3.Zero();
|
|
|
- public L2_1: Vector3 = Vector3.Zero();
|
|
|
- public L20: Vector3 = Vector3.Zero();
|
|
|
- public L21: Vector3 = Vector3.Zero();
|
|
|
- public L22: Vector3 = Vector3.Zero();
|
|
|
+ /**
|
|
|
+ * The l0,0 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l00: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The l1,-1 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l1_1: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The l1,0 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l10: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The l1,1 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l11: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The l2,-2 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l2_2: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The l2,-1 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l2_1: Vector3 = Vector3.Zero();
|
|
|
|
|
|
+ /**
|
|
|
+ * The l2,0 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l20: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The l2,1 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public l21: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The l2,2 coefficients of the spherical harmonics
|
|
|
+ */
|
|
|
+ public lL22: Vector3 = Vector3.Zero();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Adds a light to the spherical harmonics
|
|
|
+ * @param direction the direction of the light
|
|
|
+ * @param color the color of the light
|
|
|
+ * @param deltaSolidAngle the delta solid angle of the light
|
|
|
+ */
|
|
|
public addLight(direction: Vector3, color: Color3, deltaSolidAngle: number): void {
|
|
|
var colorVector = new Vector3(color.r, color.g, color.b);
|
|
|
var c = colorVector.scale(deltaSolidAngle);
|
|
|
|
|
|
- this.L00 = this.L00.add(c.scale(0.282095));
|
|
|
+ this.l00 = this.l00.add(c.scale(0.282095));
|
|
|
|
|
|
- this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
|
|
|
- this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
|
|
|
- this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
|
|
|
+ this.l1_1 = this.l1_1.add(c.scale(0.488603 * direction.y));
|
|
|
+ this.l10 = this.l10.add(c.scale(0.488603 * direction.z));
|
|
|
+ this.l11 = this.l11.add(c.scale(0.488603 * direction.x));
|
|
|
|
|
|
- this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
|
|
|
- this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
|
|
|
- this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
|
|
|
+ this.l2_2 = this.l2_2.add(c.scale(1.092548 * direction.x * direction.y));
|
|
|
+ this.l2_1 = this.l2_1.add(c.scale(1.092548 * direction.y * direction.z));
|
|
|
+ this.l21 = this.l21.add(c.scale(1.092548 * direction.x * direction.z));
|
|
|
|
|
|
- this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
|
|
|
- this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
|
|
|
+ this.l20 = this.l20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
|
|
|
+ this.lL22 = this.lL22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Scales the spherical harmonics by the given amount
|
|
|
+ * @param scale the amount to scale
|
|
|
+ */
|
|
|
public scale(scale: number): void {
|
|
|
- this.L00 = this.L00.scale(scale);
|
|
|
- this.L1_1 = this.L1_1.scale(scale);
|
|
|
- this.L10 = this.L10.scale(scale);
|
|
|
- this.L11 = this.L11.scale(scale);
|
|
|
- this.L2_2 = this.L2_2.scale(scale);
|
|
|
- this.L2_1 = this.L2_1.scale(scale);
|
|
|
- this.L20 = this.L20.scale(scale);
|
|
|
- this.L21 = this.L21.scale(scale);
|
|
|
- this.L22 = this.L22.scale(scale);
|
|
|
+ this.l00 = this.l00.scale(scale);
|
|
|
+ this.l1_1 = this.l1_1.scale(scale);
|
|
|
+ this.l10 = this.l10.scale(scale);
|
|
|
+ this.l11 = this.l11.scale(scale);
|
|
|
+ this.l2_2 = this.l2_2.scale(scale);
|
|
|
+ this.l2_1 = this.l2_1.scale(scale);
|
|
|
+ this.l20 = this.l20.scale(scale);
|
|
|
+ this.l21 = this.l21.scale(scale);
|
|
|
+ this.lL22 = this.lL22.scale(scale);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Convert from incident radiance (Li) to irradiance (E) by applying convolution with the cosine-weighted hemisphere.
|
|
|
+ *
|
|
|
+ * ```
|
|
|
+ * E_lm = A_l * L_lm
|
|
|
+ * ```
|
|
|
+ *
|
|
|
+ * In spherical harmonics this convolution amounts to scaling factors for each frequency band.
|
|
|
+ * This corresponds to equation 5 in "An Efficient Representation for Irradiance Environment Maps", where
|
|
|
+ * the scaling factors are given in equation 9.
|
|
|
+ */
|
|
|
public convertIncidentRadianceToIrradiance(): void
|
|
|
{
|
|
|
- // Convert from incident radiance (Li) to irradiance (E) by applying convolution with the cosine-weighted hemisphere.
|
|
|
- //
|
|
|
- // E_lm = A_l * L_lm
|
|
|
- //
|
|
|
- // In spherical harmonics this convolution amounts to scaling factors for each frequency band.
|
|
|
- // This corresponds to equation 5 in "An Efficient Representation for Irradiance Environment Maps", where
|
|
|
- // the scaling factors are given in equation 9.
|
|
|
-
|
|
|
// Constant (Band 0)
|
|
|
- this.L00 = this.L00.scale(3.141593);
|
|
|
+ this.l00 = this.l00.scale(3.141593);
|
|
|
|
|
|
// Linear (Band 1)
|
|
|
- this.L1_1 = this.L1_1.scale(2.094395);
|
|
|
- this.L10 = this.L10.scale(2.094395);
|
|
|
- this.L11 = this.L11.scale(2.094395);
|
|
|
+ this.l1_1 = this.l1_1.scale(2.094395);
|
|
|
+ this.l10 = this.l10.scale(2.094395);
|
|
|
+ this.l11 = this.l11.scale(2.094395);
|
|
|
|
|
|
// Quadratic (Band 2)
|
|
|
- this.L2_2 = this.L2_2.scale(0.785398);
|
|
|
- this.L2_1 = this.L2_1.scale(0.785398);
|
|
|
- this.L20 = this.L20.scale(0.785398);
|
|
|
- this.L21 = this.L21.scale(0.785398);
|
|
|
- this.L22 = this.L22.scale(0.785398);
|
|
|
+ this.l2_2 = this.l2_2.scale(0.785398);
|
|
|
+ this.l2_1 = this.l2_1.scale(0.785398);
|
|
|
+ this.l20 = this.l20.scale(0.785398);
|
|
|
+ this.l21 = this.l21.scale(0.785398);
|
|
|
+ this.lL22 = this.lL22.scale(0.785398);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Convert from irradiance to outgoing radiance for Lambertian BDRF, suitable for efficient shader evaluation.
|
|
|
+ *
|
|
|
+ * ```
|
|
|
+ * L = (1/pi) * E * rho
|
|
|
+ * ```
|
|
|
+ *
|
|
|
+ * This is done by an additional scale by 1/pi, so is a fairly trivial operation but important conceptually.
|
|
|
+ */
|
|
|
public convertIrradianceToLambertianRadiance(): void
|
|
|
{
|
|
|
- // Convert from irradiance to outgoing radiance for Lambertian BDRF, suitable for efficient shader evaluation.
|
|
|
- // L = (1/pi) * E * rho
|
|
|
- //
|
|
|
- // This is done by an additional scale by 1/pi, so is a fairly trivial operation but important conceptually.
|
|
|
-
|
|
|
this.scale(1.0 / Math.PI);
|
|
|
|
|
|
// The resultant SH now represents outgoing radiance, so includes the Lambert 1/pi normalisation factor but without albedo (rho) applied
|
|
|
// (The pixel shader must apply albedo after texture fetches, etc).
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Gets the spherical harmonics from polynomial
|
|
|
+ * @param polynomial the spherical polynomial
|
|
|
+ * @returns the spherical harmonics
|
|
|
+ */
|
|
|
public static FromPolynomial(polynomial: SphericalPolynomial): SphericalHarmonics
|
|
|
{
|
|
|
var result = new SphericalHarmonics();
|
|
|
|
|
|
- result.L00 = polynomial.xx.scale(0.376127).add(polynomial.yy.scale(0.376127)).add(polynomial.zz.scale(0.376126));
|
|
|
- result.L1_1 = polynomial.y.scale(0.977204);
|
|
|
- result.L10 = polynomial.z.scale(0.977204);
|
|
|
- result.L11 = polynomial.x.scale(0.977204);
|
|
|
- result.L2_2 = polynomial.xy.scale(1.16538);
|
|
|
- result.L2_1 = polynomial.yz.scale(1.16538);
|
|
|
- result.L20 = polynomial.zz.scale(1.34567).subtract(polynomial.xx.scale(0.672834)).subtract(polynomial.yy.scale(0.672834));
|
|
|
- result.L21 = polynomial.zx.scale(1.16538);
|
|
|
- result.L22 = polynomial.xx.scale(1.16538).subtract(polynomial.yy.scale(1.16538));
|
|
|
+ result.l00 = polynomial.xx.scale(0.376127).add(polynomial.yy.scale(0.376127)).add(polynomial.zz.scale(0.376126));
|
|
|
+ result.l1_1 = polynomial.y.scale(0.977204);
|
|
|
+ result.l10 = polynomial.z.scale(0.977204);
|
|
|
+ result.l11 = polynomial.x.scale(0.977204);
|
|
|
+ result.l2_2 = polynomial.xy.scale(1.16538);
|
|
|
+ result.l2_1 = polynomial.yz.scale(1.16538);
|
|
|
+ result.l20 = polynomial.zz.scale(1.34567).subtract(polynomial.xx.scale(0.672834)).subtract(polynomial.yy.scale(0.672834));
|
|
|
+ result.l21 = polynomial.zx.scale(1.16538);
|
|
|
+ result.lL22 = polynomial.xx.scale(1.16538).subtract(polynomial.yy.scale(1.16538));
|
|
|
|
|
|
result.scale(Math.PI);
|
|
|
|
|
@@ -171,18 +283,19 @@ module BABYLON {
|
|
|
/**
|
|
|
* Constructs a spherical harmonics from an array.
|
|
|
* @param data defines the 9x3 coefficients (l00, l1-1, l10, l11, l2-2, l2-1, l20, l21, l22)
|
|
|
+ * @returns the spherical harmonics
|
|
|
*/
|
|
|
public static FromArray(data: ArrayLike<ArrayLike<number>>): SphericalHarmonics {
|
|
|
const sh = new SphericalHarmonics();
|
|
|
- Vector3.FromArrayToRef(data[0], 0, sh.L00);
|
|
|
- Vector3.FromArrayToRef(data[1], 0, sh.L1_1);
|
|
|
- Vector3.FromArrayToRef(data[2], 0, sh.L10);
|
|
|
- Vector3.FromArrayToRef(data[3], 0, sh.L11);
|
|
|
- Vector3.FromArrayToRef(data[4], 0, sh.L2_2);
|
|
|
- Vector3.FromArrayToRef(data[5], 0, sh.L2_1);
|
|
|
- Vector3.FromArrayToRef(data[6], 0, sh.L20);
|
|
|
- Vector3.FromArrayToRef(data[7], 0, sh.L21);
|
|
|
- Vector3.FromArrayToRef(data[8], 0, sh.L22);
|
|
|
+ Vector3.FromArrayToRef(data[0], 0, sh.l00);
|
|
|
+ Vector3.FromArrayToRef(data[1], 0, sh.l1_1);
|
|
|
+ Vector3.FromArrayToRef(data[2], 0, sh.l10);
|
|
|
+ Vector3.FromArrayToRef(data[3], 0, sh.l11);
|
|
|
+ Vector3.FromArrayToRef(data[4], 0, sh.l2_2);
|
|
|
+ Vector3.FromArrayToRef(data[5], 0, sh.l2_1);
|
|
|
+ Vector3.FromArrayToRef(data[6], 0, sh.l20);
|
|
|
+ Vector3.FromArrayToRef(data[7], 0, sh.l21);
|
|
|
+ Vector3.FromArrayToRef(data[8], 0, sh.lL22);
|
|
|
return sh;
|
|
|
}
|
|
|
}
|