SphericalHarmonics3.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import { Vector3 } from './Vector3.js';
  2. /**
  3. * @author bhouston / http://clara.io
  4. * @author WestLangley / http://github.com/WestLangley
  5. *
  6. * Primary reference:
  7. * https://graphics.stanford.edu/papers/envmap/envmap.pdf
  8. *
  9. * Secondary reference:
  10. * https://www.ppsloan.org/publications/StupidSH36.pdf
  11. */
  12. // 3-band SH defined by 9 coefficients
  13. function SphericalHarmonics3() {
  14. this.coefficients = [];
  15. for ( var i = 0; i < 9; i ++ ) {
  16. this.coefficients.push( new Vector3() );
  17. }
  18. }
  19. Object.assign( SphericalHarmonics3.prototype, {
  20. isSphericalHarmonics3: true,
  21. set: function ( coefficients ) {
  22. for ( var i = 0; i < 9; i ++ ) {
  23. this.coefficients[ i ].copy( coefficients[ i ] );
  24. }
  25. return this;
  26. },
  27. zero: function () {
  28. for ( var i = 0; i < 9; i ++ ) {
  29. this.coefficients[ i ].set( 0, 0, 0 );
  30. }
  31. return this;
  32. },
  33. // get the radiance in the direction of the normal
  34. // target is a Vector3
  35. getAt: function ( normal, target ) {
  36. // normal is assumed to be unit length
  37. var x = normal.x, y = normal.y, z = normal.z;
  38. var coeff = this.coefficients;
  39. // band 0
  40. target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 );
  41. // band 1
  42. target.addScale( coeff[ 1 ], 0.488603 * y );
  43. target.addScale( coeff[ 2 ], 0.488603 * z );
  44. target.addScale( coeff[ 3 ], 0.488603 * x );
  45. // band 2
  46. target.addScale( coeff[ 4 ], 1.092548 * ( x * y ) );
  47. target.addScale( coeff[ 5 ], 1.092548 * ( y * z ) );
  48. target.addScale( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) );
  49. target.addScale( coeff[ 7 ], 1.092548 * ( x * z ) );
  50. target.addScale( coeff[ 8 ], 0.546274 * ( x * x - y * y ) );
  51. return target;
  52. },
  53. // get the irradiance (radiance convolved with cosine lobe) in the direction of the normal
  54. // target is a Vector3
  55. // https://graphics.stanford.edu/papers/envmap/envmap.pdf
  56. getIrradianceAt: function ( normal, target ) {
  57. // normal is assumed to be unit length
  58. var x = normal.x, y = normal.y, z = normal.z;
  59. var coeff = this.coefficients;
  60. // band 0
  61. target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095
  62. // band 1
  63. target.addScale( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603
  64. target.addScale( coeff[ 2 ], 2.0 * 0.511664 * z );
  65. target.addScale( coeff[ 3 ], 2.0 * 0.511664 * x );
  66. // band 2
  67. target.addScale( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548
  68. target.addScale( coeff[ 5 ], 2.0 * 0.429043 * y * z );
  69. target.addScale( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3
  70. target.addScale( coeff[ 7 ], 2.0 * 0.429043 * x * z );
  71. target.addScale( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274
  72. return target;
  73. },
  74. add: function ( sh ) {
  75. for ( var i = 0; i < 9; i ++ ) {
  76. this.coefficients[ i ].add( sh.coefficients[ i ] );
  77. }
  78. return this;
  79. },
  80. scale: function ( s ) {
  81. for ( var i = 0; i < 9; i ++ ) {
  82. this.coefficients[ i ].multiplyScalar( s );
  83. }
  84. return this;
  85. },
  86. lerp: function ( sh, alpha ) {
  87. for ( var i = 0; i < 9; i ++ ) {
  88. this.coefficients[ i ].lerp( sh.coefficients[ i ], alpha );
  89. }
  90. return this;
  91. },
  92. equals: function ( sh ) {
  93. for ( var i = 0; i < 9; i ++ ) {
  94. if ( ! this.coefficients[ i ].equals( sh.coefficients[ i ] ) ) {
  95. return false;
  96. }
  97. }
  98. return true;
  99. },
  100. copy: function ( sh ) {
  101. return this.set( sh.coefficients );
  102. },
  103. clone: function () {
  104. return new this.constructor().copy( this );
  105. },
  106. fromArray: function ( array, offset ) {
  107. if ( offset === undefined ) offset = 0;
  108. var coefficients = this.coefficients;
  109. for ( var i = 0; i < 9; i ++ ) {
  110. coefficients[ i ].fromArray( array, offset + ( i * 3 ) );
  111. }
  112. return this;
  113. },
  114. toArray: function ( array, offset ) {
  115. if ( array === undefined ) array = [];
  116. if ( offset === undefined ) offset = 0;
  117. var coefficients = this.coefficients;
  118. for ( var i = 0; i < 9; i ++ ) {
  119. coefficients[ i ].toArray( array, offset + ( i * 3 ) );
  120. }
  121. return array;
  122. }
  123. } );
  124. Object.assign( SphericalHarmonics3, {
  125. // evaluate the basis functions
  126. // shBasis is an Array[ 9 ]
  127. getBasisAt: function ( normal, shBasis ) {
  128. // normal is assumed to be unit length
  129. var x = normal.x, y = normal.y, z = normal.z;
  130. // band 0
  131. shBasis[ 0 ] = 0.282095;
  132. // band 1
  133. shBasis[ 1 ] = 0.488603 * y;
  134. shBasis[ 2 ] = 0.488603 * z;
  135. shBasis[ 3 ] = 0.488603 * x;
  136. // band 2
  137. shBasis[ 4 ] = 1.092548 * x * y;
  138. shBasis[ 5 ] = 1.092548 * y * z;
  139. shBasis[ 6 ] = 0.315392 * ( 3 * z * z - 1 );
  140. shBasis[ 7 ] = 1.092548 * x * z;
  141. shBasis[ 8 ] = 0.546274 * ( x * x - y * y );
  142. }
  143. } );
  144. export { SphericalHarmonics3 };