babylon.sphericalPolynomial.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. module BABYLON {
  2. export class SphericalPolynomial {
  3. public x: Vector3 = Vector3.Zero();
  4. public y: Vector3 = Vector3.Zero();
  5. public z: Vector3 = Vector3.Zero();
  6. public xx: Vector3 = Vector3.Zero();
  7. public yy: Vector3 = Vector3.Zero();
  8. public zz: Vector3 = Vector3.Zero();
  9. public xy: Vector3 = Vector3.Zero();
  10. public yz: Vector3 = Vector3.Zero();
  11. public zx: Vector3 = Vector3.Zero();
  12. public addAmbient(color: Color3): void {
  13. var colorVector = new Vector3(color.r, color.g, color.b);
  14. this.xx = this.xx.add(colorVector);
  15. this.yy = this.yy.add(colorVector);
  16. this.zz = this.zz.add(colorVector);
  17. }
  18. public static getSphericalPolynomialFromHarmonics(harmonics: SphericalHarmonics): SphericalPolynomial {
  19. var result = new SphericalPolynomial();
  20. result.x = harmonics.L11.scale(1.02333);
  21. result.y = harmonics.L1_1.scale(1.02333);
  22. result.z = harmonics.L10.scale(1.02333);
  23. result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
  24. result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
  25. result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
  26. result.yz = harmonics.L2_1.scale(0.858086);
  27. result.zx = harmonics.L21.scale(0.858086);
  28. result.xy = harmonics.L2_2.scale(0.858086);
  29. result.scale(1.0 / Math.PI);
  30. return result;
  31. }
  32. public scale(scale: number)
  33. {
  34. this.x = this.x.scale(scale);
  35. this.y = this.y.scale(scale);
  36. this.z = this.z.scale(scale);
  37. this.xx = this.xx.scale(scale);
  38. this.yy = this.yy.scale(scale);
  39. this.zz = this.zz.scale(scale);
  40. this.yz = this.yz.scale(scale);
  41. this.zx = this.zx.scale(scale);
  42. this.xy = this.xy.scale(scale);
  43. }
  44. }
  45. export class SphericalHarmonics {
  46. public L00: Vector3 = Vector3.Zero();
  47. public L1_1: Vector3 = Vector3.Zero();
  48. public L10: Vector3 = Vector3.Zero();
  49. public L11: Vector3 = Vector3.Zero();
  50. public L2_2: Vector3 = Vector3.Zero();
  51. public L2_1: Vector3 = Vector3.Zero();
  52. public L20: Vector3 = Vector3.Zero();
  53. public L21: Vector3 = Vector3.Zero();
  54. public L22: Vector3 = Vector3.Zero();
  55. public addLight(direction: Vector3, color: Color3, deltaSolidAngle: number): void {
  56. var colorVector = new Vector3(color.r, color.g, color.b);
  57. var c = colorVector.scale(deltaSolidAngle);
  58. this.L00 = this.L00.add(c.scale(0.282095));
  59. this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
  60. this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
  61. this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
  62. this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
  63. this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
  64. this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
  65. this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
  66. this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
  67. }
  68. public scale(scale: number): void {
  69. this.L00 = this.L00.scale(scale);
  70. this.L1_1 = this.L1_1.scale(scale);
  71. this.L10 = this.L10.scale(scale);
  72. this.L11 = this.L11.scale(scale);
  73. this.L2_2 = this.L2_2.scale(scale);
  74. this.L2_1 = this.L2_1.scale(scale);
  75. this.L20 = this.L20.scale(scale);
  76. this.L21 = this.L21.scale(scale);
  77. this.L22 = this.L22.scale(scale);
  78. }
  79. public convertIncidentRadianceToIrradiance(): void
  80. {
  81. // Convert from incident radiance (Li) to irradiance (E) by applying convolution with the cosine-weighted hemisphere.
  82. //
  83. // E_lm = A_l * L_lm
  84. //
  85. // In spherical harmonics this convolution amounts to scaling factors for each frequency band.
  86. // This corresponds to equation 5 in "An Efficient Representation for Irradiance Environment Maps", where
  87. // the scaling factors are given in equation 9.
  88. // Constant (Band 0)
  89. this.L00 = this.L00.scale(3.141593);
  90. // Linear (Band 1)
  91. this.L1_1 = this.L1_1.scale(2.094395);
  92. this.L10 = this.L10.scale(2.094395);
  93. this.L11 = this.L11.scale(2.094395);
  94. // Quadratic (Band 2)
  95. this.L2_2 = this.L2_2.scale(0.785398);
  96. this.L2_1 = this.L2_1.scale(0.785398);
  97. this.L20 = this.L20.scale(0.785398);
  98. this.L21 = this.L21.scale(0.785398);
  99. this.L22 = this.L22.scale(0.785398);
  100. }
  101. public convertIrradianceToLambertianRadiance(): void
  102. {
  103. // Convert from irradiance to outgoing radiance for Lambertian BDRF, suitable for efficient shader evaluation.
  104. // L = (1/pi) * E * rho
  105. //
  106. // This is done by an additional scale by 1/pi, so is a fairly trivial operation but important conceptually.
  107. this.scale(1.0 / Math.PI);
  108. // The resultant SH now represents outgoing radiance, so includes the Lambert 1/pi normalisation factor but without albedo (rho) applied
  109. // (The pixel shader must apply albedo after texture fetches, etc).
  110. }
  111. public static getsphericalHarmonicsFromPolynomial(polynomial: SphericalPolynomial): SphericalHarmonics
  112. {
  113. var result = new SphericalHarmonics();
  114. result.L00 = polynomial.xx.scale(0.376127).add(polynomial.yy.scale(0.376127)).add(polynomial.zz.scale(0.376126));
  115. result.L1_1 = polynomial.y.scale(0.977204);
  116. result.L10 = polynomial.z.scale(0.977204);
  117. result.L11 = polynomial.x.scale(0.977204);
  118. result.L2_2 = polynomial.xy.scale(1.16538);
  119. result.L2_1 = polynomial.yz.scale(1.16538);
  120. result.L20 = polynomial.zz.scale(1.34567).subtract(polynomial.xx.scale(0.672834)).subtract(polynomial.yy.scale(0.672834));
  121. result.L21 = polynomial.zx.scale(1.16538);
  122. result.L22 = polynomial.xx.scale(1.16538).subtract(polynomial.yy.scale(1.16538));
  123. result.scale(Math.PI);
  124. return result;
  125. }
  126. }
  127. }