LightProbeGenerator.js 5.6 KB

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