LightProbeGenerator.js 7.5 KB

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