LightProbeGenerator.js 6.3 KB

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