LightProbeGenerator.js 5.5 KB

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