SphericalHarmonics3.js 4.5 KB

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