LightProbeGenerator.js 5.6 KB

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