LightProbeGenerator.js 5.5 KB

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