LightProbeGenerator.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. ( function () {
  2. class LightProbeGenerator {
  3. // https://www.ppsloan.org/publications/StupidSH36.pdf
  4. static fromCubeTexture( cubeTexture ) {
  5. let totalWeight = 0;
  6. const coord = new THREE.Vector3();
  7. const dir = new THREE.Vector3();
  8. const color = new THREE.Color();
  9. const shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  10. const sh = new THREE.SphericalHarmonics3();
  11. const shCoefficients = sh.coefficients;
  12. for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
  13. const image = cubeTexture.image[ faceIndex ];
  14. const width = image.width;
  15. const height = image.height;
  16. const canvas = document.createElement( 'canvas' );
  17. canvas.width = width;
  18. canvas.height = height;
  19. const context = canvas.getContext( '2d' );
  20. context.drawImage( image, 0, 0, width, height );
  21. const imageData = context.getImageData( 0, 0, width, height );
  22. const data = imageData.data;
  23. const imageWidth = imageData.width; // assumed to be square
  24. const pixelSize = 2 / imageWidth;
  25. for ( let i = 0, il = data.length; i < il; i += 4 ) {
  26. // RGBA assumed
  27. // pixel color
  28. color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
  29. // convert to linear color space
  30. convertColorToLinear( color, cubeTexture.encoding );
  31. // pixel coordinate on unit cube
  32. const pixelIndex = i / 4;
  33. const col = - 1 + ( pixelIndex % imageWidth + 0.5 ) * pixelSize;
  34. const row = 1 - ( Math.floor( pixelIndex / imageWidth ) + 0.5 ) * pixelSize;
  35. switch ( faceIndex ) {
  36. case 0:
  37. coord.set( - 1, row, - col );
  38. break;
  39. case 1:
  40. coord.set( 1, row, col );
  41. break;
  42. case 2:
  43. coord.set( - col, 1, - row );
  44. break;
  45. case 3:
  46. coord.set( - col, - 1, row );
  47. break;
  48. case 4:
  49. coord.set( - col, row, 1 );
  50. break;
  51. case 5:
  52. coord.set( col, row, - 1 );
  53. break;
  54. }
  55. // weight assigned to this pixel
  56. const lengthSq = coord.lengthSq();
  57. const weight = 4 / ( Math.sqrt( lengthSq ) * lengthSq );
  58. totalWeight += weight;
  59. // direction vector to this pixel
  60. dir.copy( coord ).normalize();
  61. // evaluate SH basis functions in direction dir
  62. THREE.SphericalHarmonics3.getBasisAt( dir, shBasis );
  63. // accummuulate
  64. for ( let j = 0; j < 9; j ++ ) {
  65. shCoefficients[ j ].x += shBasis[ j ] * color.r * weight;
  66. shCoefficients[ j ].y += shBasis[ j ] * color.g * weight;
  67. shCoefficients[ j ].z += shBasis[ j ] * color.b * weight;
  68. }
  69. }
  70. }
  71. // normalize
  72. const norm = 4 * Math.PI / totalWeight;
  73. for ( let j = 0; j < 9; j ++ ) {
  74. shCoefficients[ j ].x *= norm;
  75. shCoefficients[ j ].y *= norm;
  76. shCoefficients[ j ].z *= norm;
  77. }
  78. return new THREE.LightProbe( sh );
  79. }
  80. static fromCubeRenderTarget( renderer, cubeRenderTarget ) {
  81. // The renderTarget must be set to RGBA in order to make readRenderTargetPixels works
  82. let totalWeight = 0;
  83. const coord = new THREE.Vector3();
  84. const dir = new THREE.Vector3();
  85. const color = new THREE.Color();
  86. const shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  87. const sh = new THREE.SphericalHarmonics3();
  88. const shCoefficients = sh.coefficients;
  89. for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
  90. const imageWidth = cubeRenderTarget.width; // assumed to be square
  91. const data = new Uint8Array( imageWidth * imageWidth * 4 );
  92. renderer.readRenderTargetPixels( cubeRenderTarget, 0, 0, imageWidth, imageWidth, data, faceIndex );
  93. const pixelSize = 2 / imageWidth;
  94. for ( let i = 0, il = data.length; i < il; i += 4 ) {
  95. // RGBA assumed
  96. // pixel color
  97. color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
  98. // convert to linear color space
  99. convertColorToLinear( color, cubeRenderTarget.texture.encoding );
  100. // pixel coordinate on unit cube
  101. const pixelIndex = i / 4;
  102. const col = - 1 + ( pixelIndex % imageWidth + 0.5 ) * pixelSize;
  103. const row = 1 - ( Math.floor( pixelIndex / imageWidth ) + 0.5 ) * pixelSize;
  104. switch ( faceIndex ) {
  105. case 0:
  106. coord.set( 1, row, - col );
  107. break;
  108. case 1:
  109. coord.set( - 1, row, col );
  110. break;
  111. case 2:
  112. coord.set( col, 1, - row );
  113. break;
  114. case 3:
  115. coord.set( col, - 1, row );
  116. break;
  117. case 4:
  118. coord.set( col, row, 1 );
  119. break;
  120. case 5:
  121. coord.set( - col, row, - 1 );
  122. break;
  123. }
  124. // weight assigned to this pixel
  125. const lengthSq = coord.lengthSq();
  126. const weight = 4 / ( Math.sqrt( lengthSq ) * lengthSq );
  127. totalWeight += weight;
  128. // direction vector to this pixel
  129. dir.copy( coord ).normalize();
  130. // evaluate SH basis functions in direction dir
  131. THREE.SphericalHarmonics3.getBasisAt( dir, shBasis );
  132. // accummuulate
  133. for ( let j = 0; j < 9; j ++ ) {
  134. shCoefficients[ j ].x += shBasis[ j ] * color.r * weight;
  135. shCoefficients[ j ].y += shBasis[ j ] * color.g * weight;
  136. shCoefficients[ j ].z += shBasis[ j ] * color.b * weight;
  137. }
  138. }
  139. }
  140. // normalize
  141. const norm = 4 * Math.PI / totalWeight;
  142. for ( let j = 0; j < 9; j ++ ) {
  143. shCoefficients[ j ].x *= norm;
  144. shCoefficients[ j ].y *= norm;
  145. shCoefficients[ j ].z *= norm;
  146. }
  147. return new THREE.LightProbe( sh );
  148. }
  149. }
  150. function convertColorToLinear( color, encoding ) {
  151. switch ( encoding ) {
  152. case THREE.sRGBEncoding:
  153. color.convertSRGBToLinear();
  154. break;
  155. case THREE.LinearEncoding:
  156. break;
  157. default:
  158. console.warn( 'WARNING: LightProbeGenerator convertColorToLinear() encountered an unsupported encoding.' );
  159. break;
  160. }
  161. return color;
  162. }
  163. THREE.LightProbeGenerator = LightProbeGenerator;
  164. } )();