LightProbeGenerator.js 5.4 KB

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