PMREMGenerator.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  1. import {
  2. CubeReflectionMapping,
  3. CubeRefractionMapping,
  4. CubeUVReflectionMapping,
  5. LinearFilter,
  6. NoToneMapping,
  7. NoBlending,
  8. RGBAFormat,
  9. HalfFloatType,
  10. BackSide,
  11. LinearSRGBColorSpace
  12. } from '../constants.js';
  13. import { BufferAttribute } from '../core/BufferAttribute.js';
  14. import { BufferGeometry } from '../core/BufferGeometry.js';
  15. import { Mesh } from '../objects/Mesh.js';
  16. import { OrthographicCamera } from '../cameras/OrthographicCamera.js';
  17. import { PerspectiveCamera } from '../cameras/PerspectiveCamera.js';
  18. import { ShaderMaterial } from '../materials/ShaderMaterial.js';
  19. import { Vector3 } from '../math/Vector3.js';
  20. import { Color } from '../math/Color.js';
  21. import { WebGLRenderTarget } from '../renderers/WebGLRenderTarget.js';
  22. import { MeshBasicMaterial } from '../materials/MeshBasicMaterial.js';
  23. import { BoxGeometry } from '../geometries/BoxGeometry.js';
  24. const LOD_MIN = 4;
  25. // The number of extra mips.
  26. // Used for scene blur in fromScene() method.
  27. const EXTRA_LODS = 6;
  28. // The maximum length of the blur for loop. Smaller sigmas will use fewer
  29. // samples and exit early, but not recompile the shader.
  30. // Used for scene blur in fromScene() method.
  31. const MAX_SAMPLES = 20;
  32. // GGX VNDF importance sampling configuration
  33. const GGX_SAMPLES = 256;
  34. const _flatCamera = /*@__PURE__*/ new OrthographicCamera();
  35. const _clearColor = /*@__PURE__*/ new Color();
  36. let _oldTarget = null;
  37. let _oldActiveCubeFace = 0;
  38. let _oldActiveMipmapLevel = 0;
  39. let _oldXrEnabled = false;
  40. const _origin = /*@__PURE__*/ new Vector3();
  41. /**
  42. * This class generates a Prefiltered, Mipmapped Radiance Environment Map
  43. * (PMREM) from a cubeMap environment texture. This allows different levels of
  44. * blur to be quickly accessed based on material roughness. It is packed into a
  45. * special CubeUV format that allows us to perform custom interpolation so that
  46. * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap
  47. * chain, it only goes down to the LOD_MIN level (above), and then creates extra
  48. * even more filtered 'mips' at the same LOD_MIN resolution, associated with
  49. * higher roughness levels. In this way we maintain resolution to smoothly
  50. * interpolate diffuse lighting while limiting sampling computation.
  51. *
  52. * The prefiltering uses GGX VNDF (Visible Normal Distribution Function)
  53. * importance sampling based on "Sampling the GGX Distribution of Visible Normals"
  54. * (Heitz, 2018) to generate environment maps that accurately match the GGX BRDF
  55. * used in material rendering for physically-based image-based lighting.
  56. */
  57. class PMREMGenerator {
  58. /**
  59. * Constructs a new PMREM generator.
  60. *
  61. * @param {WebGLRenderer} renderer - The renderer.
  62. */
  63. constructor( renderer ) {
  64. this._renderer = renderer;
  65. this._pingPongRenderTarget = null;
  66. this._lodMax = 0;
  67. this._cubeSize = 0;
  68. this._sizeLods = [];
  69. this._lodMeshes = [];
  70. this._backgroundBox = null;
  71. this._cubemapMaterial = null;
  72. this._equirectMaterial = null;
  73. this._blurMaterial = null;
  74. this._ggxMaterial = null;
  75. }
  76. /**
  77. * Generates a PMREM from a supplied Scene, which can be faster than using an
  78. * image if networking bandwidth is low. Optional sigma specifies a blur radius
  79. * in radians to be applied to the scene before PMREM generation. Optional near
  80. * and far planes ensure the scene is rendered in its entirety.
  81. *
  82. * @param {Scene} scene - The scene to be captured.
  83. * @param {number} [sigma=0] - The blur radius in radians.
  84. * @param {number} [near=0.1] - The near plane distance.
  85. * @param {number} [far=100] - The far plane distance.
  86. * @param {Object} [options={}] - The configuration options.
  87. * @param {number} [options.size=256] - The texture size of the PMREM.
  88. * @param {Vector3} [options.position=origin] - The position of the internal cube camera that renders the scene.
  89. * @return {WebGLRenderTarget} The resulting PMREM.
  90. */
  91. fromScene( scene, sigma = 0, near = 0.1, far = 100, options = {} ) {
  92. const {
  93. size = 256,
  94. position = _origin,
  95. } = options;
  96. _oldTarget = this._renderer.getRenderTarget();
  97. _oldActiveCubeFace = this._renderer.getActiveCubeFace();
  98. _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel();
  99. _oldXrEnabled = this._renderer.xr.enabled;
  100. this._renderer.xr.enabled = false;
  101. this._setSize( size );
  102. const cubeUVRenderTarget = this._allocateTargets();
  103. cubeUVRenderTarget.depthBuffer = true;
  104. this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget, position );
  105. if ( sigma > 0 ) {
  106. this._blur( cubeUVRenderTarget, 0, 0, sigma );
  107. }
  108. this._applyPMREM( cubeUVRenderTarget );
  109. this._cleanup( cubeUVRenderTarget );
  110. return cubeUVRenderTarget;
  111. }
  112. /**
  113. * Generates a PMREM from an equirectangular texture, which can be either LDR
  114. * or HDR. The ideal input image size is 1k (1024 x 512),
  115. * as this matches best with the 256 x 256 cubemap output.
  116. *
  117. * @param {Texture} equirectangular - The equirectangular texture to be converted.
  118. * @param {?WebGLRenderTarget} [renderTarget=null] - The render target to use.
  119. * @return {WebGLRenderTarget} The resulting PMREM.
  120. */
  121. fromEquirectangular( equirectangular, renderTarget = null ) {
  122. return this._fromTexture( equirectangular, renderTarget );
  123. }
  124. /**
  125. * Generates a PMREM from an cubemap texture, which can be either LDR
  126. * or HDR. The ideal input cube size is 256 x 256,
  127. * as this matches best with the 256 x 256 cubemap output.
  128. *
  129. * @param {Texture} cubemap - The cubemap texture to be converted.
  130. * @param {?WebGLRenderTarget} [renderTarget=null] - The render target to use.
  131. * @return {WebGLRenderTarget} The resulting PMREM.
  132. */
  133. fromCubemap( cubemap, renderTarget = null ) {
  134. return this._fromTexture( cubemap, renderTarget );
  135. }
  136. /**
  137. * Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during
  138. * your texture's network fetch for increased concurrency.
  139. */
  140. compileCubemapShader() {
  141. if ( this._cubemapMaterial === null ) {
  142. this._cubemapMaterial = _getCubemapMaterial();
  143. this._compileMaterial( this._cubemapMaterial );
  144. }
  145. }
  146. /**
  147. * Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during
  148. * your texture's network fetch for increased concurrency.
  149. */
  150. compileEquirectangularShader() {
  151. if ( this._equirectMaterial === null ) {
  152. this._equirectMaterial = _getEquirectMaterial();
  153. this._compileMaterial( this._equirectMaterial );
  154. }
  155. }
  156. /**
  157. * Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class,
  158. * so you should not need more than one PMREMGenerator object. If you do, calling dispose() on
  159. * one of them will cause any others to also become unusable.
  160. */
  161. dispose() {
  162. this._dispose();
  163. if ( this._cubemapMaterial !== null ) this._cubemapMaterial.dispose();
  164. if ( this._equirectMaterial !== null ) this._equirectMaterial.dispose();
  165. if ( this._backgroundBox !== null ) {
  166. this._backgroundBox.geometry.dispose();
  167. this._backgroundBox.material.dispose();
  168. }
  169. }
  170. // private interface
  171. _setSize( cubeSize ) {
  172. this._lodMax = Math.floor( Math.log2( cubeSize ) );
  173. this._cubeSize = Math.pow( 2, this._lodMax );
  174. }
  175. _dispose() {
  176. if ( this._blurMaterial !== null ) this._blurMaterial.dispose();
  177. if ( this._ggxMaterial !== null ) this._ggxMaterial.dispose();
  178. if ( this._pingPongRenderTarget !== null ) this._pingPongRenderTarget.dispose();
  179. for ( let i = 0; i < this._lodMeshes.length; i ++ ) {
  180. this._lodMeshes[ i ].geometry.dispose();
  181. }
  182. }
  183. _cleanup( outputTarget ) {
  184. this._renderer.setRenderTarget( _oldTarget, _oldActiveCubeFace, _oldActiveMipmapLevel );
  185. this._renderer.xr.enabled = _oldXrEnabled;
  186. outputTarget.scissorTest = false;
  187. _setViewport( outputTarget, 0, 0, outputTarget.width, outputTarget.height );
  188. }
  189. _fromTexture( texture, renderTarget ) {
  190. if ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping ) {
  191. this._setSize( texture.image.length === 0 ? 16 : ( texture.image[ 0 ].width || texture.image[ 0 ].image.width ) );
  192. } else { // Equirectangular
  193. this._setSize( texture.image.width / 4 );
  194. }
  195. _oldTarget = this._renderer.getRenderTarget();
  196. _oldActiveCubeFace = this._renderer.getActiveCubeFace();
  197. _oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel();
  198. _oldXrEnabled = this._renderer.xr.enabled;
  199. this._renderer.xr.enabled = false;
  200. const cubeUVRenderTarget = renderTarget || this._allocateTargets();
  201. this._textureToCubeUV( texture, cubeUVRenderTarget );
  202. this._applyPMREM( cubeUVRenderTarget );
  203. this._cleanup( cubeUVRenderTarget );
  204. return cubeUVRenderTarget;
  205. }
  206. _allocateTargets() {
  207. const width = 3 * Math.max( this._cubeSize, 16 * 7 );
  208. const height = 4 * this._cubeSize;
  209. const params = {
  210. magFilter: LinearFilter,
  211. minFilter: LinearFilter,
  212. generateMipmaps: false,
  213. type: HalfFloatType,
  214. format: RGBAFormat,
  215. colorSpace: LinearSRGBColorSpace,
  216. depthBuffer: false
  217. };
  218. const cubeUVRenderTarget = _createRenderTarget( width, height, params );
  219. if ( this._pingPongRenderTarget === null || this._pingPongRenderTarget.width !== width || this._pingPongRenderTarget.height !== height ) {
  220. if ( this._pingPongRenderTarget !== null ) {
  221. this._dispose();
  222. }
  223. this._pingPongRenderTarget = _createRenderTarget( width, height, params );
  224. const { _lodMax } = this;
  225. ( { lodMeshes: this._lodMeshes, sizeLods: this._sizeLods } = _createPlanes( _lodMax ) );
  226. this._blurMaterial = _getBlurShader( _lodMax, width, height );
  227. this._ggxMaterial = _getGGXShader( _lodMax, width, height );
  228. }
  229. return cubeUVRenderTarget;
  230. }
  231. _compileMaterial( material ) {
  232. const mesh = new Mesh( new BufferGeometry(), material );
  233. this._renderer.compile( mesh, _flatCamera );
  234. }
  235. _sceneToCubeUV( scene, near, far, cubeUVRenderTarget, position ) {
  236. const fov = 90;
  237. const aspect = 1;
  238. const cubeCamera = new PerspectiveCamera( fov, aspect, near, far );
  239. const upSign = [ 1, - 1, 1, 1, 1, 1 ];
  240. const forwardSign = [ 1, 1, 1, - 1, - 1, - 1 ];
  241. const renderer = this._renderer;
  242. const originalAutoClear = renderer.autoClear;
  243. const toneMapping = renderer.toneMapping;
  244. renderer.getClearColor( _clearColor );
  245. renderer.toneMapping = NoToneMapping;
  246. renderer.autoClear = false;
  247. // https://github.com/mrdoob/three.js/issues/31413#issuecomment-3095966812
  248. const reversedDepthBuffer = renderer.state.buffers.depth.getReversed();
  249. if ( reversedDepthBuffer ) {
  250. renderer.setRenderTarget( cubeUVRenderTarget );
  251. renderer.clearDepth();
  252. renderer.setRenderTarget( null );
  253. }
  254. if ( this._backgroundBox === null ) {
  255. this._backgroundBox = new Mesh(
  256. new BoxGeometry(),
  257. new MeshBasicMaterial( {
  258. name: 'PMREM.Background',
  259. side: BackSide,
  260. depthWrite: false,
  261. depthTest: false,
  262. } )
  263. );
  264. }
  265. const backgroundBox = this._backgroundBox;
  266. const backgroundMaterial = backgroundBox.material;
  267. let useSolidColor = false;
  268. const background = scene.background;
  269. if ( background ) {
  270. if ( background.isColor ) {
  271. backgroundMaterial.color.copy( background );
  272. scene.background = null;
  273. useSolidColor = true;
  274. }
  275. } else {
  276. backgroundMaterial.color.copy( _clearColor );
  277. useSolidColor = true;
  278. }
  279. for ( let i = 0; i < 6; i ++ ) {
  280. const col = i % 3;
  281. if ( col === 0 ) {
  282. cubeCamera.up.set( 0, upSign[ i ], 0 );
  283. cubeCamera.position.set( position.x, position.y, position.z );
  284. cubeCamera.lookAt( position.x + forwardSign[ i ], position.y, position.z );
  285. } else if ( col === 1 ) {
  286. cubeCamera.up.set( 0, 0, upSign[ i ] );
  287. cubeCamera.position.set( position.x, position.y, position.z );
  288. cubeCamera.lookAt( position.x, position.y + forwardSign[ i ], position.z );
  289. } else {
  290. cubeCamera.up.set( 0, upSign[ i ], 0 );
  291. cubeCamera.position.set( position.x, position.y, position.z );
  292. cubeCamera.lookAt( position.x, position.y, position.z + forwardSign[ i ] );
  293. }
  294. const size = this._cubeSize;
  295. _setViewport( cubeUVRenderTarget, col * size, i > 2 ? size : 0, size, size );
  296. renderer.setRenderTarget( cubeUVRenderTarget );
  297. if ( useSolidColor ) {
  298. renderer.render( backgroundBox, cubeCamera );
  299. }
  300. renderer.render( scene, cubeCamera );
  301. }
  302. renderer.toneMapping = toneMapping;
  303. renderer.autoClear = originalAutoClear;
  304. scene.background = background;
  305. }
  306. _textureToCubeUV( texture, cubeUVRenderTarget ) {
  307. const renderer = this._renderer;
  308. const isCubeTexture = ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping );
  309. if ( isCubeTexture ) {
  310. if ( this._cubemapMaterial === null ) {
  311. this._cubemapMaterial = _getCubemapMaterial();
  312. }
  313. this._cubemapMaterial.uniforms.flipEnvMap.value = ( texture.isRenderTargetTexture === false ) ? - 1 : 1;
  314. } else {
  315. if ( this._equirectMaterial === null ) {
  316. this._equirectMaterial = _getEquirectMaterial();
  317. }
  318. }
  319. const material = isCubeTexture ? this._cubemapMaterial : this._equirectMaterial;
  320. const mesh = this._lodMeshes[ 0 ];
  321. mesh.material = material;
  322. const uniforms = material.uniforms;
  323. uniforms[ 'envMap' ].value = texture;
  324. const size = this._cubeSize;
  325. _setViewport( cubeUVRenderTarget, 0, 0, 3 * size, 2 * size );
  326. renderer.setRenderTarget( cubeUVRenderTarget );
  327. renderer.render( mesh, _flatCamera );
  328. }
  329. _applyPMREM( cubeUVRenderTarget ) {
  330. const renderer = this._renderer;
  331. const autoClear = renderer.autoClear;
  332. renderer.autoClear = false;
  333. const n = this._lodMeshes.length;
  334. // Use GGX VNDF importance sampling
  335. for ( let i = 1; i < n; i ++ ) {
  336. this._applyGGXFilter( cubeUVRenderTarget, i - 1, i );
  337. }
  338. renderer.autoClear = autoClear;
  339. }
  340. /**
  341. * Applies GGX VNDF importance sampling filter to generate a prefiltered environment map.
  342. * Uses Monte Carlo integration with VNDF importance sampling to accurately represent the
  343. * GGX BRDF for physically-based rendering. Reads from the previous LOD level and
  344. * applies incremental roughness filtering to avoid over-blurring.
  345. *
  346. * @private
  347. * @param {WebGLRenderTarget} cubeUVRenderTarget
  348. * @param {number} lodIn - Source LOD level to read from
  349. * @param {number} lodOut - Target LOD level to write to
  350. */
  351. _applyGGXFilter( cubeUVRenderTarget, lodIn, lodOut ) {
  352. const renderer = this._renderer;
  353. const pingPongRenderTarget = this._pingPongRenderTarget;
  354. const ggxMaterial = this._ggxMaterial;
  355. const ggxMesh = this._lodMeshes[ lodOut ];
  356. ggxMesh.material = ggxMaterial;
  357. const ggxUniforms = ggxMaterial.uniforms;
  358. // Calculate incremental roughness between LOD levels
  359. const targetRoughness = lodOut / ( this._lodMeshes.length - 1 );
  360. const sourceRoughness = lodIn / ( this._lodMeshes.length - 1 );
  361. const incrementalRoughness = Math.sqrt( targetRoughness * targetRoughness - sourceRoughness * sourceRoughness );
  362. // Apply blur strength mapping for better quality across the roughness range
  363. const blurStrength = 0.0 + targetRoughness * 1.25;
  364. const adjustedRoughness = incrementalRoughness * blurStrength;
  365. // Calculate viewport position based on output LOD level
  366. const { _lodMax } = this;
  367. const outputSize = this._sizeLods[ lodOut ];
  368. const x = 3 * outputSize * ( lodOut > _lodMax - LOD_MIN ? lodOut - _lodMax + LOD_MIN : 0 );
  369. const y = 4 * ( this._cubeSize - outputSize );
  370. // Read from previous LOD with incremental roughness
  371. ggxUniforms[ 'envMap' ].value = cubeUVRenderTarget.texture;
  372. ggxUniforms[ 'roughness' ].value = adjustedRoughness;
  373. ggxUniforms[ 'mipInt' ].value = _lodMax - lodIn; // Sample from input LOD
  374. _setViewport( pingPongRenderTarget, x, y, 3 * outputSize, 2 * outputSize );
  375. renderer.setRenderTarget( pingPongRenderTarget );
  376. renderer.render( ggxMesh, _flatCamera );
  377. // Copy from pingPong back to cubeUV (simple direct copy)
  378. ggxUniforms[ 'envMap' ].value = pingPongRenderTarget.texture;
  379. ggxUniforms[ 'roughness' ].value = 0.0; // Direct copy
  380. ggxUniforms[ 'mipInt' ].value = _lodMax - lodOut; // Read from the level we just wrote
  381. _setViewport( cubeUVRenderTarget, x, y, 3 * outputSize, 2 * outputSize );
  382. renderer.setRenderTarget( cubeUVRenderTarget );
  383. renderer.render( ggxMesh, _flatCamera );
  384. }
  385. /**
  386. * This is a two-pass Gaussian blur for a cubemap. The blur is performed using
  387. * a spiral kernel (Golden Angle) to ensure good distribution of samples on the
  388. * sphere. We perform two passes with split sigma to improve quality and smoothness.
  389. *
  390. * Used for initial scene blur in fromScene() method when sigma > 0.
  391. *
  392. * @private
  393. * @param {WebGLRenderTarget} cubeUVRenderTarget
  394. * @param {number} lodIn
  395. * @param {number} lodOut
  396. * @param {number} sigma
  397. */
  398. _blur( cubeUVRenderTarget, lodIn, lodOut, sigma ) {
  399. const pingPongRenderTarget = this._pingPongRenderTarget;
  400. const blurSigma = Math.sqrt( sigma * sigma / 2.0 );
  401. this._blurPass(
  402. cubeUVRenderTarget,
  403. pingPongRenderTarget,
  404. lodIn,
  405. lodOut,
  406. blurSigma );
  407. this._blurPass(
  408. pingPongRenderTarget,
  409. cubeUVRenderTarget,
  410. lodOut,
  411. lodOut,
  412. blurSigma );
  413. }
  414. _blurPass( targetIn, targetOut, lodIn, lodOut, sigmaRadians ) {
  415. const renderer = this._renderer;
  416. const blurMaterial = this._blurMaterial;
  417. const blurMesh = this._lodMeshes[ lodOut ];
  418. blurMesh.material = blurMaterial;
  419. const blurUniforms = blurMaterial.uniforms;
  420. blurUniforms[ 'envMap' ].value = targetIn.texture;
  421. blurUniforms[ 'sigma' ].value = sigmaRadians;
  422. blurUniforms[ 'mipInt' ].value = this._lodMax - lodIn;
  423. const outputSize = this._sizeLods[ lodOut ];
  424. const x = 3 * outputSize * ( lodOut > this._lodMax - LOD_MIN ? lodOut - this._lodMax + LOD_MIN : 0 );
  425. const y = 4 * ( this._cubeSize - outputSize );
  426. _setViewport( targetOut, x, y, 3 * outputSize, 2 * outputSize );
  427. renderer.setRenderTarget( targetOut );
  428. renderer.render( blurMesh, _flatCamera );
  429. }
  430. }
  431. function _createPlanes( lodMax ) {
  432. const sizeLods = [];
  433. const lodMeshes = [];
  434. let lod = lodMax;
  435. const totalLods = lodMax - LOD_MIN + 1 + EXTRA_LODS;
  436. for ( let i = 0; i < totalLods; i ++ ) {
  437. const sizeLod = Math.pow( 2, lod );
  438. sizeLods.push( sizeLod );
  439. const texelSize = 1.0 / ( sizeLod - 2 );
  440. const min = - texelSize;
  441. const max = 1 + texelSize;
  442. const uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ];
  443. const cubeFaces = 6;
  444. const vertices = 6;
  445. const positionSize = 3;
  446. const uvSize = 2;
  447. const position = new Float32Array( positionSize * vertices * cubeFaces );
  448. const uv = new Float32Array( uvSize * vertices * cubeFaces );
  449. const outputDirection = new Float32Array( 3 * vertices * cubeFaces );
  450. for ( let face = 0; face < cubeFaces; face ++ ) {
  451. const x = ( face % 3 ) * 2 / 3 - 1;
  452. const y = face > 2 ? 0 : - 1;
  453. const coordinates = [
  454. x, y, 0,
  455. x + 2 / 3, y, 0,
  456. x + 2 / 3, y + 1, 0,
  457. x, y, 0,
  458. x + 2 / 3, y + 1, 0,
  459. x, y + 1, 0
  460. ];
  461. position.set( coordinates, positionSize * vertices * face );
  462. uv.set( uv1, uvSize * vertices * face );
  463. for ( let i = 0; i < vertices; i ++ ) {
  464. const u = uv1[ i * 2 ];
  465. const v = uv1[ i * 2 + 1 ];
  466. const vec = new Vector3();
  467. // logic matching _getCommonVertexShader
  468. const x = u * 2 - 1;
  469. const y = v * 2 - 1;
  470. if ( face === 0 ) {
  471. vec.set( 1, y, x );
  472. } else if ( face === 1 ) {
  473. vec.set( - x, 1, - y );
  474. } else if ( face === 2 ) {
  475. vec.set( - x, y, 1 );
  476. } else if ( face === 3 ) {
  477. vec.set( - 1, y, - x );
  478. } else if ( face === 4 ) {
  479. vec.set( - x, - 1, y );
  480. } else {
  481. vec.set( x, y, - 1 );
  482. }
  483. vec.toArray( outputDirection, ( face * vertices + i ) * 3 );
  484. }
  485. }
  486. const planes = new BufferGeometry();
  487. planes.setAttribute( 'position', new BufferAttribute( position, positionSize ) );
  488. planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
  489. planes.setAttribute( 'outputDirection', new BufferAttribute( outputDirection, 3 ) );
  490. lodMeshes.push( new Mesh( planes, null ) );
  491. if ( lod > LOD_MIN ) {
  492. lod --;
  493. }
  494. }
  495. return { lodMeshes, sizeLods };
  496. }
  497. function _createRenderTarget( width, height, params ) {
  498. const cubeUVRenderTarget = new WebGLRenderTarget( width, height, params );
  499. cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping;
  500. cubeUVRenderTarget.texture.name = 'PMREM.cubeUv';
  501. cubeUVRenderTarget.scissorTest = true;
  502. return cubeUVRenderTarget;
  503. }
  504. function _setViewport( target, x, y, width, height ) {
  505. target.viewport.set( x, y, width, height );
  506. target.scissor.set( x, y, width, height );
  507. }
  508. function _getGGXShader( lodMax, width, height ) {
  509. const shaderMaterial = new ShaderMaterial( {
  510. name: 'PMREMGGXConvolution',
  511. defines: {
  512. 'GGX_SAMPLES': GGX_SAMPLES,
  513. 'CUBEUV_TEXEL_WIDTH': 1.0 / width,
  514. 'CUBEUV_TEXEL_HEIGHT': 1.0 / height,
  515. 'CUBEUV_MAX_MIP': `${lodMax}.0`,
  516. },
  517. uniforms: {
  518. 'envMap': { value: null },
  519. 'roughness': { value: 0.0 },
  520. 'mipInt': { value: 0 }
  521. },
  522. vertexShader: _getCommonVertexShader(),
  523. fragmentShader: /* glsl */`
  524. precision highp float;
  525. precision highp int;
  526. varying vec3 vOutputDirection;
  527. uniform sampler2D envMap;
  528. uniform float roughness;
  529. uniform float mipInt;
  530. #define ENVMAP_TYPE_CUBE_UV
  531. #include <cube_uv_reflection_fragment>
  532. #define PI 3.14159265359
  533. // Van der Corput radical inverse
  534. float radicalInverse_VdC(uint bits) {
  535. bits = (bits << 16u) | (bits >> 16u);
  536. bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
  537. bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
  538. bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
  539. bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
  540. return float(bits) * 2.3283064365386963e-10; // / 0x100000000
  541. }
  542. // Hammersley sequence
  543. vec2 hammersley(uint i, uint N) {
  544. return vec2(float(i) / float(N), radicalInverse_VdC(i));
  545. }
  546. // GGX VNDF importance sampling (Eric Heitz 2018)
  547. // "Sampling the GGX Distribution of Visible Normals"
  548. // https://jcgt.org/published/0007/04/01/
  549. vec3 importanceSampleGGX_VNDF(vec2 Xi, vec3 V, float roughness) {
  550. float alpha = roughness * roughness;
  551. // Section 3.2: Transform view direction to hemisphere configuration
  552. vec3 Vh = normalize(vec3(alpha * V.x, alpha * V.y, V.z));
  553. // Section 4.1: Orthonormal basis
  554. float lensq = Vh.x * Vh.x + Vh.y * Vh.y;
  555. vec3 T1 = lensq > 0.0 ? vec3(-Vh.y, Vh.x, 0.0) / sqrt(lensq) : vec3(1.0, 0.0, 0.0);
  556. vec3 T2 = cross(Vh, T1);
  557. // Section 4.2: Parameterization of projected area
  558. float r = sqrt(Xi.x);
  559. float phi = 2.0 * PI * Xi.y;
  560. float t1 = r * cos(phi);
  561. float t2 = r * sin(phi);
  562. float s = 0.5 * (1.0 + Vh.z);
  563. t2 = (1.0 - s) * sqrt(1.0 - t1 * t1) + s * t2;
  564. // Section 4.3: Reprojection onto hemisphere
  565. vec3 Nh = t1 * T1 + t2 * T2 + sqrt(max(0.0, 1.0 - t1 * t1 - t2 * t2)) * Vh;
  566. // Section 3.4: Transform back to ellipsoid configuration
  567. return normalize(vec3(alpha * Nh.x, alpha * Nh.y, max(0.0, Nh.z)));
  568. }
  569. void main() {
  570. vec3 N = normalize(vOutputDirection);
  571. vec3 V = N; // Assume view direction equals normal for pre-filtering
  572. vec3 prefilteredColor = vec3(0.0);
  573. float totalWeight = 0.0;
  574. // For very low roughness, just sample the environment directly
  575. if (roughness < 0.001) {
  576. gl_FragColor = vec4(bilinearCubeUV(envMap, N, mipInt), 1.0);
  577. return;
  578. }
  579. // Tangent space basis for VNDF sampling
  580. vec3 up = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0);
  581. vec3 tangent = normalize(cross(up, N));
  582. vec3 bitangent = cross(N, tangent);
  583. for(uint i = 0u; i < uint(GGX_SAMPLES); i++) {
  584. vec2 Xi = hammersley(i, uint(GGX_SAMPLES));
  585. // For PMREM, V = N, so in tangent space V is always (0, 0, 1)
  586. vec3 H_tangent = importanceSampleGGX_VNDF(Xi, vec3(0.0, 0.0, 1.0), roughness);
  587. // Transform H back to world space
  588. vec3 H = normalize(tangent * H_tangent.x + bitangent * H_tangent.y + N * H_tangent.z);
  589. vec3 L = normalize(2.0 * dot(V, H) * H - V);
  590. float NdotL = max(dot(N, L), 0.0);
  591. if(NdotL > 0.0) {
  592. // Sample environment at fixed mip level
  593. // VNDF importance sampling handles the distribution filtering
  594. vec3 sampleColor = bilinearCubeUV(envMap, L, mipInt);
  595. // Weight by NdotL for the split-sum approximation
  596. // VNDF PDF naturally accounts for the visible microfacet distribution
  597. prefilteredColor += sampleColor * NdotL;
  598. totalWeight += NdotL;
  599. }
  600. }
  601. if (totalWeight > 0.0) {
  602. prefilteredColor = prefilteredColor / totalWeight;
  603. }
  604. gl_FragColor = vec4(prefilteredColor, 1.0);
  605. }
  606. `,
  607. blending: NoBlending,
  608. depthTest: false,
  609. depthWrite: false
  610. } );
  611. return shaderMaterial;
  612. }
  613. function _getBlurShader( lodMax, width, height ) {
  614. const shaderMaterial = new ShaderMaterial( {
  615. name: 'SphericalGaussianBlur',
  616. defines: {
  617. 'n': MAX_SAMPLES,
  618. 'CUBEUV_TEXEL_WIDTH': 1.0 / width,
  619. 'CUBEUV_TEXEL_HEIGHT': 1.0 / height,
  620. 'CUBEUV_MAX_MIP': `${lodMax}.0`,
  621. },
  622. uniforms: {
  623. 'envMap': { value: null },
  624. 'sigma': { value: 0 },
  625. 'mipInt': { value: 0 },
  626. },
  627. vertexShader: _getCommonVertexShader(),
  628. fragmentShader: /* glsl */`
  629. precision mediump float;
  630. precision mediump int;
  631. varying vec3 vOutputDirection;
  632. uniform sampler2D envMap;
  633. uniform float sigma;
  634. uniform float mipInt;
  635. #define ENVMAP_TYPE_CUBE_UV
  636. #include <cube_uv_reflection_fragment>
  637. void main() {
  638. if ( sigma == 0.0 ) {
  639. gl_FragColor = vec4( bilinearCubeUV( envMap, vOutputDirection, mipInt ), 1.0 );
  640. return;
  641. }
  642. vec3 outputDirection = normalize( vOutputDirection );
  643. vec3 accumColor = vec3( 0.0 );
  644. float accumWeight = 0.0;
  645. vec3 up = abs( outputDirection.z ) < 0.999 ? vec3( 0.0, 0.0, 1.0 ) : vec3( 1.0, 0.0, 0.0 );
  646. vec3 tangent = normalize( cross( up, outputDirection ) );
  647. vec3 bitangent = cross( outputDirection, tangent );
  648. // Golden angle
  649. float phi = 0.0;
  650. const float goldenAngle = 2.3999632;
  651. // Precompute sine/cosine of golden angle for incremental rotation
  652. float sinPhi = sin( goldenAngle );
  653. float cosPhi = cos( goldenAngle );
  654. // Initial rotation (phi = 0)
  655. float cp = 1.0;
  656. float sp = 0.0;
  657. for ( int i = 0; i < n; i ++ ) {
  658. // Spiral radius (0 to 1)
  659. float r = sqrt( ( float( i ) + 0.5 ) / float( n ) );
  660. // Map radius to theta (spread)
  661. // 3 sigma covers ~99.7% of the gaussian
  662. float theta = r * 3.0 * sigma;
  663. // Calculate axis of rotation in tangent plane
  664. // axis = -sin(phi) * tangent + cos(phi) * bitangent
  665. vec3 axis = - sp * tangent + cp * bitangent;
  666. // Rotate N around axis by theta
  667. // Since N is perpendicular to axis, simplified Rodrigues formula:
  668. // result = N * cos(theta) + cross(axis, N) * sin(theta)
  669. // cross(axis, N) = sin(phi) * bitangent + cos(phi) * tangent = dir
  670. // So result = N * cos(theta) + dir * sin(theta)
  671. // However, we can compute the offset vector directly in tangent space
  672. // offset = ( tangent * cp + bitangent * sp ) * sin( theta );
  673. float sinTheta = sin( theta );
  674. float cosTheta = cos( theta );
  675. vec3 sampleDir = outputDirection * cosTheta + ( tangent * cp + bitangent * sp ) * sinTheta;
  676. // Gaussian weight
  677. // We sample 'n' points. We assume they are area-weighted by the spiral pattern.
  678. // We just need the gaussian falloff.
  679. float w = exp( - 0.5 * ( theta * theta ) / ( sigma * sigma ) );
  680. accumColor += w * bilinearCubeUV( envMap, normalize( sampleDir ), mipInt );
  681. accumWeight += w;
  682. // Update phi for next sample
  683. float cp_next = cp * cosPhi - sp * sinPhi;
  684. float sp_next = sp * cosPhi + cp * sinPhi;
  685. cp = cp_next;
  686. sp = sp_next;
  687. }
  688. gl_FragColor = vec4( accumColor / accumWeight, 1.0 );
  689. }
  690. `,
  691. blending: NoBlending,
  692. depthTest: false,
  693. depthWrite: false
  694. } );
  695. return shaderMaterial;
  696. }
  697. function _getEquirectMaterial() {
  698. return new ShaderMaterial( {
  699. name: 'EquirectangularToCubeUV',
  700. uniforms: {
  701. 'envMap': { value: null }
  702. },
  703. vertexShader: _getCommonVertexShader(),
  704. fragmentShader: /* glsl */`
  705. precision mediump float;
  706. precision mediump int;
  707. varying vec3 vOutputDirection;
  708. uniform sampler2D envMap;
  709. #include <common>
  710. void main() {
  711. vec3 outputDirection = normalize( vOutputDirection );
  712. vec2 uv = equirectUv( outputDirection );
  713. gl_FragColor = vec4( texture2D ( envMap, uv ).rgb, 1.0 );
  714. }
  715. `,
  716. blending: NoBlending,
  717. depthTest: false,
  718. depthWrite: false
  719. } );
  720. }
  721. function _getCubemapMaterial() {
  722. return new ShaderMaterial( {
  723. name: 'CubemapToCubeUV',
  724. uniforms: {
  725. 'envMap': { value: null },
  726. 'flipEnvMap': { value: - 1 }
  727. },
  728. vertexShader: _getCommonVertexShader(),
  729. fragmentShader: /* glsl */`
  730. precision mediump float;
  731. precision mediump int;
  732. uniform float flipEnvMap;
  733. varying vec3 vOutputDirection;
  734. uniform samplerCube envMap;
  735. void main() {
  736. gl_FragColor = textureCube( envMap, vec3( flipEnvMap * vOutputDirection.x, vOutputDirection.yz ) );
  737. }
  738. `,
  739. blending: NoBlending,
  740. depthTest: false,
  741. depthWrite: false
  742. } );
  743. }
  744. function _getCommonVertexShader() {
  745. return /* glsl */`
  746. precision mediump float;
  747. precision mediump int;
  748. attribute vec3 outputDirection;
  749. varying vec3 vOutputDirection;
  750. void main() {
  751. vOutputDirection = outputDirection;
  752. gl_Position = vec4( position, 1.0 );
  753. }
  754. `;
  755. }
  756. export { PMREMGenerator };
粤ICP备19079148号