LightProbeGenerator.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import {
  2. Color,
  3. LightProbe,
  4. LinearSRGBColorSpace,
  5. SphericalHarmonics3,
  6. Vector3,
  7. SRGBColorSpace,
  8. NoColorSpace,
  9. HalfFloatType,
  10. DataUtils
  11. } from 'three';
  12. class LightProbeGenerator {
  13. // https://www.ppsloan.org/publications/StupidSH36.pdf
  14. static fromCubeTexture( cubeTexture ) {
  15. let totalWeight = 0;
  16. const coord = new Vector3();
  17. const dir = new Vector3();
  18. const color = new Color();
  19. const shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  20. const sh = new SphericalHarmonics3();
  21. const shCoefficients = sh.coefficients;
  22. for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
  23. const image = cubeTexture.image[ faceIndex ];
  24. const width = image.width;
  25. const height = image.height;
  26. const canvas = document.createElement( 'canvas' );
  27. canvas.width = width;
  28. canvas.height = height;
  29. const context = canvas.getContext( '2d' );
  30. context.drawImage( image, 0, 0, width, height );
  31. const imageData = context.getImageData( 0, 0, width, height );
  32. const data = imageData.data;
  33. const imageWidth = imageData.width; // assumed to be square
  34. const pixelSize = 2 / imageWidth;
  35. for ( let 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.colorSpace );
  40. // pixel coordinate on unit cube
  41. const pixelIndex = i / 4;
  42. const col = - 1 + ( pixelIndex % imageWidth + 0.5 ) * pixelSize;
  43. const 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. const lengthSq = coord.lengthSq();
  54. const 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 ( let 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. const norm = ( 4 * Math.PI ) / totalWeight;
  70. for ( let 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. static fromCubeRenderTarget( renderer, cubeRenderTarget ) {
  78. // The renderTarget must be set to RGBA in order to make readRenderTargetPixels works
  79. let totalWeight = 0;
  80. const coord = new Vector3();
  81. const dir = new Vector3();
  82. const color = new Color();
  83. const shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  84. const sh = new SphericalHarmonics3();
  85. const shCoefficients = sh.coefficients;
  86. const dataType = cubeRenderTarget.texture.type;
  87. for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
  88. const imageWidth = cubeRenderTarget.width; // assumed to be square
  89. let data;
  90. if ( dataType === HalfFloatType ) {
  91. data = new Uint16Array( imageWidth * imageWidth * 4 );
  92. } else {
  93. // assuming UnsignedByteType
  94. data = new Uint8Array( imageWidth * imageWidth * 4 );
  95. }
  96. renderer.readRenderTargetPixels( cubeRenderTarget, 0, 0, imageWidth, imageWidth, data, faceIndex );
  97. const pixelSize = 2 / imageWidth;
  98. for ( let i = 0, il = data.length; i < il; i += 4 ) { // RGBA assumed
  99. let r, g, b;
  100. if ( dataType === HalfFloatType ) {
  101. r = DataUtils.fromHalfFloat( data[ i ] );
  102. g = DataUtils.fromHalfFloat( data[ i + 1 ] );
  103. b = DataUtils.fromHalfFloat( data[ i + 2 ] );
  104. } else {
  105. r = data[ i ] / 255;
  106. g = data[ i + 1 ] / 255;
  107. b = data[ i + 2 ] / 255;
  108. }
  109. // pixel color
  110. color.setRGB( r, g, b );
  111. // convert to linear color space
  112. convertColorToLinear( color, cubeRenderTarget.texture.colorSpace );
  113. // pixel coordinate on unit cube
  114. const pixelIndex = i / 4;
  115. const col = - 1 + ( pixelIndex % imageWidth + 0.5 ) * pixelSize;
  116. const row = 1 - ( Math.floor( pixelIndex / imageWidth ) + 0.5 ) * pixelSize;
  117. switch ( faceIndex ) {
  118. case 0: coord.set( 1, row, - col ); break;
  119. case 1: coord.set( - 1, row, col ); break;
  120. case 2: coord.set( col, 1, - row ); break;
  121. case 3: coord.set( col, - 1, row ); break;
  122. case 4: coord.set( col, row, 1 ); break;
  123. case 5: coord.set( - col, row, - 1 ); break;
  124. }
  125. // weight assigned to this pixel
  126. const lengthSq = coord.lengthSq();
  127. const weight = 4 / ( Math.sqrt( lengthSq ) * lengthSq );
  128. totalWeight += weight;
  129. // direction vector to this pixel
  130. dir.copy( coord ).normalize();
  131. // evaluate SH basis functions in direction dir
  132. SphericalHarmonics3.getBasisAt( dir, shBasis );
  133. // accummuulate
  134. for ( let j = 0; j < 9; j ++ ) {
  135. shCoefficients[ j ].x += shBasis[ j ] * color.r * weight;
  136. shCoefficients[ j ].y += shBasis[ j ] * color.g * weight;
  137. shCoefficients[ j ].z += shBasis[ j ] * color.b * weight;
  138. }
  139. }
  140. }
  141. // normalize
  142. const norm = ( 4 * Math.PI ) / totalWeight;
  143. for ( let j = 0; j < 9; j ++ ) {
  144. shCoefficients[ j ].x *= norm;
  145. shCoefficients[ j ].y *= norm;
  146. shCoefficients[ j ].z *= norm;
  147. }
  148. return new LightProbe( sh );
  149. }
  150. }
  151. function convertColorToLinear( color, colorSpace ) {
  152. switch ( colorSpace ) {
  153. case SRGBColorSpace:
  154. color.convertSRGBToLinear();
  155. break;
  156. case LinearSRGBColorSpace:
  157. case NoColorSpace:
  158. break;
  159. default:
  160. console.warn( 'WARNING: LightProbeGenerator convertColorToLinear() encountered an unsupported color space.' );
  161. break;
  162. }
  163. return color;
  164. }
  165. export { LightProbeGenerator };