LightProbeGenerator.js 5.3 KB

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