SphericalHarmonics3.js 4.4 KB

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