0
0

LightProbeGenerator.js 5.6 KB

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