@@ -44,6 +44,7 @@ const EXTRA_LOD_SIGMA = [ 0.125, 0.215, 0.35, 0.446, 0.526, 0.582 ];
4444const MAX_SAMPLES = 20 ;
4545
4646const _flatCamera = /*@__PURE__ */ new OrthographicCamera ( - 1 , 1 , 1 , - 1 , 0 , 1 ) ;
47+ const _cubeCamera = /*@__PURE__ */ new PerspectiveCamera ( 90 , 1 ) ;
4748const _clearColor = /*@__PURE__ */ new Color ( ) ;
4849let _oldTarget = null ;
4950let _oldActiveCubeFace = 0 ;
@@ -106,10 +107,12 @@ class PMREMGenerator {
106107 this . _lodPlanes = [ ] ;
107108 this . _sizeLods = [ ] ;
108109 this . _sigmas = [ ] ;
110+ this . _lodMeshes = [ ] ;
109111
110112 this . _blurMaterial = null ;
111113 this . _cubemapMaterial = null ;
112114 this . _equirectMaterial = null ;
115+ this . _backgroundBox = null ;
113116
114117 }
115118
@@ -210,6 +213,12 @@ class PMREMGenerator {
210213
211214 if ( this . _cubemapMaterial !== null ) this . _cubemapMaterial . dispose ( ) ;
212215 if ( this . _equirectMaterial !== null ) this . _equirectMaterial . dispose ( ) ;
216+ if ( this . _backgroundBox !== null ) {
217+
218+ this . _backgroundBox . geometry . dispose ( ) ;
219+ this . _backgroundBox . material . dispose ( ) ;
220+
221+ }
213222
214223 }
215224
@@ -297,7 +306,7 @@ class PMREMGenerator {
297306 this . _pingPongRenderTarget = _createRenderTarget ( width , height , params ) ;
298307
299308 const { _lodMax } = this ;
300- ( { sizeLods : this . _sizeLods , lodPlanes : this . _lodPlanes , sigmas : this . _sigmas } = _createPlanes ( _lodMax ) ) ;
309+ ( { sizeLods : this . _sizeLods , lodPlanes : this . _lodPlanes , sigmas : this . _sigmas , lodMeshes : this . _lodMeshes } = _createPlanes ( _lodMax ) ) ;
301310
302311 this . _blurMaterial = _getBlurShader ( _lodMax , width , height ) ;
303312
@@ -309,16 +318,18 @@ class PMREMGenerator {
309318
310319 _compileMaterial ( material ) {
311320
312- const tmpMesh = new Mesh ( this . _lodPlanes [ 0 ] , material ) ;
321+ const tmpMesh = this . _lodMeshes [ 0 ] ;
322+ tmpMesh . material = material ;
323+
313324 this . _renderer . compile ( tmpMesh , _flatCamera ) ;
314325
315326 }
316327
317328 _sceneToCubeUV ( scene , near , far , cubeUVRenderTarget ) {
318329
319- const fov = 90 ;
320- const aspect = 1 ;
321- const cubeCamera = new PerspectiveCamera ( fov , aspect , near , far ) ;
330+ const cubeCamera = _cubeCamera ;
331+ cubeCamera . near = near ;
332+ cubeCamera . far = far ;
322333
323334 // px, py, pz, nx, ny, nz
324335 const upSign = [ - 1 , 1 , - 1 , - 1 , - 1 , - 1 ] ;
@@ -333,14 +344,20 @@ class PMREMGenerator {
333344 renderer . toneMapping = NoToneMapping ;
334345 renderer . autoClear = false ;
335346
336- const backgroundMaterial = new MeshBasicMaterial ( {
337- name : 'PMREM.Background' ,
338- side : BackSide ,
339- depthWrite : false ,
340- depthTest : false
341- } ) ;
347+ let backgroundBox = this . _backgroundBox ;
342348
343- const backgroundBox = new Mesh ( new BoxGeometry ( ) , backgroundMaterial ) ;
349+ if ( backgroundBox === null ) {
350+
351+ const backgroundMaterial = new MeshBasicMaterial ( {
352+ name : 'PMREM.Background' ,
353+ side : BackSide ,
354+ depthWrite : false ,
355+ depthTest : false
356+ } ) ;
357+
358+ backgroundBox = new Mesh ( new BoxGeometry ( ) , backgroundMaterial ) ;
359+
360+ }
344361
345362 let useSolidColor = false ;
346363 const background = scene . background ;
@@ -349,15 +366,15 @@ class PMREMGenerator {
349366
350367 if ( background . isColor ) {
351368
352- backgroundMaterial . color . copy ( background ) ;
369+ backgroundBox . material . color . copy ( background ) ;
353370 scene . background = null ;
354371 useSolidColor = true ;
355372
356373 }
357374
358375 } else {
359376
360- backgroundMaterial . color . copy ( _clearColor ) ;
377+ backgroundBox . material . color . copy ( _clearColor ) ;
361378 useSolidColor = true ;
362379
363380 }
@@ -401,9 +418,6 @@ class PMREMGenerator {
401418
402419 }
403420
404- backgroundBox . geometry . dispose ( ) ;
405- backgroundBox . material . dispose ( ) ;
406-
407421 renderer . toneMapping = toneMapping ;
408422 renderer . autoClear = originalAutoClear ;
409423 scene . background = background ;
@@ -435,10 +449,11 @@ class PMREMGenerator {
435449 }
436450
437451 const material = isCubeTexture ? this . _cubemapMaterial : this . _equirectMaterial ;
438- const mesh = new Mesh ( this . _lodPlanes [ 0 ] , material ) ;
439-
440452 material . fragmentNode . value = texture ;
441453
454+ const mesh = this . _lodMeshes [ 0 ] ;
455+ mesh . material = material ;
456+
442457 const size = this . _cubeSize ;
443458
444459 _setViewport ( cubeUVRenderTarget , 0 , 0 , 3 * size , 2 * size ) ;
@@ -506,15 +521,16 @@ class PMREMGenerator {
506521
507522 if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) {
508523
509- console . error (
510- 'blur direction must be either latitudinal or longitudinal!' ) ;
524+ console . error ( 'blur direction must be either latitudinal or longitudinal!' ) ;
511525
512526 }
513527
514528 // Number of standard deviations at which to cut off the discrete approximation.
515529 const STANDARD_DEVIATIONS = 3 ;
516530
517- const blurMesh = new Mesh ( this . _lodPlanes [ lodOut ] , blurMaterial ) ;
531+ const blurMesh = this . _lodMeshes [ lodOut ] ;
532+ blurMesh . material = blurMaterial ;
533+
518534 const blurUniforms = blurMaterial . uniforms ;
519535
520536 const pixels = this . _sizeLods [ lodIn ] - 1 ;
@@ -557,6 +573,8 @@ class PMREMGenerator {
557573
558574 }
559575
576+ targetIn . texture . frame = ( targetIn . texture . frame || 0 ) + 1 ;
577+
560578 blurUniforms . envMap . value = targetIn . texture ;
561579 blurUniforms . samples . value = samples ;
562580 blurUniforms . weights . array = weights ;
@@ -589,6 +607,7 @@ function _createPlanes( lodMax ) {
589607 const lodPlanes = [ ] ;
590608 const sizeLods = [ ] ;
591609 const sigmas = [ ] ;
610+ const lodMeshes = [ ] ;
592611
593612 let lod = lodMax ;
594613
@@ -653,6 +672,7 @@ function _createPlanes( lodMax ) {
653672 planes . setAttribute ( 'uv' , new BufferAttribute ( uv , uvSize ) ) ;
654673 planes . setAttribute ( 'faceIndex' , new BufferAttribute ( faceIndex , faceIndexSize ) ) ;
655674 lodPlanes . push ( planes ) ;
675+ lodMeshes . push ( new Mesh ( planes , null ) ) ;
656676
657677 if ( lod > LOD_MIN ) {
658678
@@ -662,7 +682,7 @@ function _createPlanes( lodMax ) {
662682
663683 }
664684
665- return { lodPlanes, sizeLods, sigmas } ;
685+ return { lodPlanes, sizeLods, sigmas, lodMeshes } ;
666686
667687}
668688
0 commit comments