LightProbeGenerator.js 5.7 KB

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