LightProbeGenerator.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. /**
  14. * Utility class for creating instances of {@link LightProbe}.
  15. *
  16. * @hideconstructor
  17. * @three_import import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js';
  18. */
  19. class LightProbeGenerator {
  20. /**
  21. * Creates a light probe from the given (radiance) environment map.
  22. * The method expects that the environment map is represented as a cube texture.
  23. *
  24. * @param {CubeTexture} cubeTexture - The environment map.
  25. * @return {LightProbe} The created light probe.
  26. */
  27. static fromCubeTexture( cubeTexture ) {
  28. // https://www.ppsloan.org/publications/StupidSH36.pdf
  29. let totalWeight = 0;
  30. const coord = new Vector3();
  31. const dir = new Vector3();
  32. const color = new Color();
  33. const shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  34. const sh = new SphericalHarmonics3();
  35. const shCoefficients = sh.coefficients;
  36. for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
  37. const image = cubeTexture.image[ faceIndex ];
  38. const width = image.width;
  39. const height = image.height;
  40. const canvas = document.createElement( 'canvas' );
  41. canvas.width = width;
  42. canvas.height = height;
  43. const context = canvas.getContext( '2d' );
  44. context.drawImage( image, 0, 0, width, height );
  45. const imageData = context.getImageData( 0, 0, width, height );
  46. const data = imageData.data;
  47. const imageWidth = imageData.width; // assumed to be square
  48. const pixelSize = 2 / imageWidth;
  49. for ( let i = 0, il = data.length; i < il; i += 4 ) { // RGBA assumed
  50. // pixel color
  51. color.setRGB( data[ i ] / 255, data[ i + 1 ] / 255, data[ i + 2 ] / 255 );
  52. // convert to linear color space
  53. convertColorToLinear( color, cubeTexture.colorSpace );
  54. // pixel coordinate on unit cube
  55. const pixelIndex = i / 4;
  56. const col = - 1 + ( pixelIndex % imageWidth + 0.5 ) * pixelSize;
  57. const row = 1 - ( Math.floor( pixelIndex / imageWidth ) + 0.5 ) * pixelSize;
  58. switch ( faceIndex ) {
  59. case 0: coord.set( - 1, row, - col ); break;
  60. case 1: coord.set( 1, row, col ); break;
  61. case 2: coord.set( - col, 1, - row ); break;
  62. case 3: coord.set( - col, - 1, row ); break;
  63. case 4: coord.set( - col, row, 1 ); break;
  64. case 5: coord.set( col, row, - 1 ); break;
  65. }
  66. // weight assigned to this pixel
  67. const lengthSq = coord.lengthSq();
  68. const weight = 4 / ( Math.sqrt( lengthSq ) * lengthSq );
  69. totalWeight += weight;
  70. // direction vector to this pixel
  71. dir.copy( coord ).normalize();
  72. // evaluate SH basis functions in direction dir
  73. SphericalHarmonics3.getBasisAt( dir, shBasis );
  74. // accumulate
  75. for ( let j = 0; j < 9; j ++ ) {
  76. shCoefficients[ j ].x += shBasis[ j ] * color.r * weight;
  77. shCoefficients[ j ].y += shBasis[ j ] * color.g * weight;
  78. shCoefficients[ j ].z += shBasis[ j ] * color.b * weight;
  79. }
  80. }
  81. }
  82. // normalize
  83. const norm = ( 4 * Math.PI ) / totalWeight;
  84. for ( let j = 0; j < 9; j ++ ) {
  85. shCoefficients[ j ].x *= norm;
  86. shCoefficients[ j ].y *= norm;
  87. shCoefficients[ j ].z *= norm;
  88. }
  89. return new LightProbe( sh );
  90. }
  91. /**
  92. * Creates a light probe from the given (radiance) environment map.
  93. * The method expects that the environment map is represented as a cube render target.
  94. *
  95. * The cube render target must be in RGBA so `cubeRenderTarget.texture.format` must be
  96. * set to {@link RGBAFormat}.
  97. *
  98. * @async
  99. * @param {WebGPURenderer|WebGLRenderer} renderer - The renderer.
  100. * @param {CubeRenderTarget|WebGLCubeRenderTarget} cubeRenderTarget - The environment map.
  101. * @return {Promise<LightProbe>} A Promise that resolves with the created light probe.
  102. */
  103. static async fromCubeRenderTarget( renderer, cubeRenderTarget ) {
  104. const flip = renderer.coordinateSystem === WebGLCoordinateSystem ? - 1 : 1;
  105. // The renderTarget must be set to RGBA in order to make readRenderTargetPixels works
  106. let totalWeight = 0;
  107. const coord = new Vector3();
  108. const dir = new Vector3();
  109. const color = new Color();
  110. const shBasis = [ 0, 0, 0, 0, 0, 0, 0, 0, 0 ];
  111. const sh = new SphericalHarmonics3();
  112. const shCoefficients = sh.coefficients;
  113. const dataType = cubeRenderTarget.texture.type;
  114. const imageWidth = cubeRenderTarget.width; // assumed to be square
  115. let data;
  116. if ( renderer.isWebGLRenderer ) {
  117. if ( dataType === HalfFloatType ) {
  118. data = new Uint16Array( imageWidth * imageWidth * 4 );
  119. } else {
  120. // assuming UnsignedByteType
  121. data = new Uint8Array( imageWidth * imageWidth * 4 );
  122. }
  123. }
  124. for ( let faceIndex = 0; faceIndex < 6; faceIndex ++ ) {
  125. if ( renderer.isWebGLRenderer ) {
  126. await renderer.readRenderTargetPixelsAsync( cubeRenderTarget, 0, 0, imageWidth, imageWidth, data, faceIndex );
  127. } else {
  128. data = await renderer.readRenderTargetPixelsAsync( cubeRenderTarget, 0, 0, imageWidth, imageWidth, 0, faceIndex );
  129. }
  130. const pixelSize = 2 / imageWidth;
  131. for ( let i = 0, il = data.length; i < il; i += 4 ) { // RGBA assumed
  132. let r, g, b;
  133. if ( dataType === HalfFloatType ) {
  134. r = DataUtils.fromHalfFloat( data[ i ] );
  135. g = DataUtils.fromHalfFloat( data[ i + 1 ] );
  136. b = DataUtils.fromHalfFloat( data[ i + 2 ] );
  137. } else {
  138. r = data[ i ] / 255;
  139. g = data[ i + 1 ] / 255;
  140. b = data[ i + 2 ] / 255;
  141. }
  142. // pixel color
  143. color.setRGB( r, g, b );
  144. // convert to linear color space
  145. convertColorToLinear( color, cubeRenderTarget.texture.colorSpace );
  146. // pixel coordinate on unit cube
  147. const pixelIndex = i / 4;
  148. const col = ( 1 - ( pixelIndex % imageWidth + 0.5 ) * pixelSize ) * flip;
  149. const row = 1 - ( Math.floor( pixelIndex / imageWidth ) + 0.5 ) * pixelSize;
  150. switch ( faceIndex ) {
  151. case 0: coord.set( - 1 * flip, row, col * flip ); break;
  152. case 1: coord.set( 1 * flip, row, - col * flip ); break;
  153. case 2: coord.set( col, 1, - row ); break;
  154. case 3: coord.set( col, - 1, row ); break;
  155. case 4: coord.set( col, row, 1 ); break;
  156. case 5: coord.set( - col, row, - 1 ); break;
  157. }
  158. // weight assigned to this pixel
  159. const lengthSq = coord.lengthSq();
  160. const weight = 4 / ( Math.sqrt( lengthSq ) * lengthSq );
  161. totalWeight += weight;
  162. // direction vector to this pixel
  163. dir.copy( coord ).normalize();
  164. // evaluate SH basis functions in direction dir
  165. SphericalHarmonics3.getBasisAt( dir, shBasis );
  166. // accumulate
  167. for ( let j = 0; j < 9; j ++ ) {
  168. shCoefficients[ j ].x += shBasis[ j ] * color.r * weight;
  169. shCoefficients[ j ].y += shBasis[ j ] * color.g * weight;
  170. shCoefficients[ j ].z += shBasis[ j ] * color.b * weight;
  171. }
  172. }
  173. }
  174. // normalize
  175. const norm = ( 4 * Math.PI ) / totalWeight;
  176. for ( let j = 0; j < 9; j ++ ) {
  177. shCoefficients[ j ].x *= norm;
  178. shCoefficients[ j ].y *= norm;
  179. shCoefficients[ j ].z *= norm;
  180. }
  181. return new LightProbe( sh );
  182. }
  183. }
  184. function convertColorToLinear( color, colorSpace ) {
  185. switch ( colorSpace ) {
  186. case SRGBColorSpace:
  187. color.convertSRGBToLinear();
  188. break;
  189. case LinearSRGBColorSpace:
  190. case NoColorSpace:
  191. break;
  192. default:
  193. console.warn( 'WARNING: LightProbeGenerator convertColorToLinear() encountered an unsupported color space.' );
  194. break;
  195. }
  196. return color;
  197. }
  198. export { LightProbeGenerator };