Skip to content

Commit 97bece8

Browse files
committed
PMREMGenerator: Reuse Meshes
1 parent 85e39c0 commit 97bece8

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

examples/jsm/renderers/common/extras/PMREMGenerator.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,12 @@ class PMREMGenerator {
106106
this._lodPlanes = [];
107107
this._sizeLods = [];
108108
this._sigmas = [];
109+
this._lodMeshes = [];
109110

110111
this._blurMaterial = null;
111112
this._cubemapMaterial = null;
112113
this._equirectMaterial = null;
114+
this._backgroundBox = null;
113115

114116
}
115117

@@ -210,6 +212,12 @@ class PMREMGenerator {
210212

211213
if ( this._cubemapMaterial !== null ) this._cubemapMaterial.dispose();
212214
if ( this._equirectMaterial !== null ) this._equirectMaterial.dispose();
215+
if ( this._backgroundBox !== null ) {
216+
217+
this._backgroundBox.geometry.dispose();
218+
this._backgroundBox.material.dispose();
219+
220+
}
213221

214222
}
215223

@@ -297,7 +305,7 @@ class PMREMGenerator {
297305
this._pingPongRenderTarget = _createRenderTarget( width, height, params );
298306

299307
const { _lodMax } = this;
300-
( { sizeLods: this._sizeLods, lodPlanes: this._lodPlanes, sigmas: this._sigmas } = _createPlanes( _lodMax ) );
308+
( { sizeLods: this._sizeLods, lodPlanes: this._lodPlanes, sigmas: this._sigmas, lodMeshes: this._lodMeshes } = _createPlanes( _lodMax ) );
301309

302310
this._blurMaterial = _getBlurShader( _lodMax, width, height );
303311

@@ -309,7 +317,9 @@ class PMREMGenerator {
309317

310318
_compileMaterial( material ) {
311319

312-
const tmpMesh = new Mesh( this._lodPlanes[ 0 ], material );
320+
const tmpMesh = this._lodMeshes[ 0 ];
321+
tmpMesh.material = material;
322+
313323
this._renderer.compile( tmpMesh, _flatCamera );
314324

315325
}
@@ -333,14 +343,20 @@ class PMREMGenerator {
333343
renderer.toneMapping = NoToneMapping;
334344
renderer.autoClear = false;
335345

336-
const backgroundMaterial = new MeshBasicMaterial( {
337-
name: 'PMREM.Background',
338-
side: BackSide,
339-
depthWrite: false,
340-
depthTest: false
341-
} );
346+
let backgroundBox = this._backgroundBox;
342347

343-
const backgroundBox = new Mesh( new BoxGeometry(), backgroundMaterial );
348+
if ( backgroundBox === null ) {
349+
350+
const backgroundMaterial = new MeshBasicMaterial( {
351+
name: 'PMREM.Background',
352+
side: BackSide,
353+
depthWrite: false,
354+
depthTest: false
355+
} );
356+
357+
backgroundBox = new Mesh( new BoxGeometry(), backgroundMaterial );
358+
359+
}
344360

345361
let useSolidColor = false;
346362
const background = scene.background;
@@ -349,15 +365,15 @@ class PMREMGenerator {
349365

350366
if ( background.isColor ) {
351367

352-
backgroundMaterial.color.copy( background );
368+
backgroundBox.material.color.copy( background );
353369
scene.background = null;
354370
useSolidColor = true;
355371

356372
}
357373

358374
} else {
359375

360-
backgroundMaterial.color.copy( _clearColor );
376+
backgroundBox.material.color.copy( _clearColor );
361377
useSolidColor = true;
362378

363379
}
@@ -401,9 +417,6 @@ class PMREMGenerator {
401417

402418
}
403419

404-
backgroundBox.geometry.dispose();
405-
backgroundBox.material.dispose();
406-
407420
renderer.toneMapping = toneMapping;
408421
renderer.autoClear = originalAutoClear;
409422
scene.background = background;
@@ -435,10 +448,11 @@ class PMREMGenerator {
435448
}
436449

437450
const material = isCubeTexture ? this._cubemapMaterial : this._equirectMaterial;
438-
const mesh = new Mesh( this._lodPlanes[ 0 ], material );
439-
440451
material.fragmentNode.value = texture;
441452

453+
const mesh = this._lodMeshes[ 0 ];
454+
mesh.material = material;
455+
442456
const size = this._cubeSize;
443457

444458
_setViewport( cubeUVRenderTarget, 0, 0, 3 * size, 2 * size );
@@ -506,15 +520,16 @@ class PMREMGenerator {
506520

507521
if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) {
508522

509-
console.error(
510-
'blur direction must be either latitudinal or longitudinal!' );
523+
console.error( 'blur direction must be either latitudinal or longitudinal!' );
511524

512525
}
513526

514527
// Number of standard deviations at which to cut off the discrete approximation.
515528
const STANDARD_DEVIATIONS = 3;
516529

517-
const blurMesh = new Mesh( this._lodPlanes[ lodOut ], blurMaterial );
530+
const blurMesh = this._lodMeshes[ lodOut ];
531+
blurMesh.material = blurMaterial;
532+
518533
const blurUniforms = blurMaterial.uniforms;
519534

520535
const pixels = this._sizeLods[ lodIn ] - 1;
@@ -557,6 +572,8 @@ class PMREMGenerator {
557572

558573
}
559574

575+
targetIn.texture.frame = ( targetIn.texture.frame || 0 ) + 1;
576+
560577
blurUniforms.envMap.value = targetIn.texture;
561578
blurUniforms.samples.value = samples;
562579
blurUniforms.weights.array = weights;
@@ -589,6 +606,7 @@ function _createPlanes( lodMax ) {
589606
const lodPlanes = [];
590607
const sizeLods = [];
591608
const sigmas = [];
609+
const lodMeshes = [];
592610

593611
let lod = lodMax;
594612

@@ -653,6 +671,7 @@ function _createPlanes( lodMax ) {
653671
planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
654672
planes.setAttribute( 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) );
655673
lodPlanes.push( planes );
674+
lodMeshes.push( new Mesh( planes, null ) );
656675

657676
if ( lod > LOD_MIN ) {
658677

@@ -662,7 +681,7 @@ function _createPlanes( lodMax ) {
662681

663682
}
664683

665-
return { lodPlanes, sizeLods, sigmas };
684+
return { lodPlanes, sizeLods, sigmas, lodMeshes };
666685

667686
}
668687

0 commit comments

Comments
 (0)