LightProbeGenerator.js 5.4 KB

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