Skip to content

Commit

Permalink
PMREMGenerator: Reuse Meshes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunag committed Mar 24, 2024
1 parent 85e39c0 commit 97bece8
Showing 1 changed file with 39 additions and 20 deletions.
59 changes: 39 additions & 20 deletions examples/jsm/renderers/common/extras/PMREMGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ class PMREMGenerator {
this._lodPlanes = [];
this._sizeLods = [];
this._sigmas = [];
this._lodMeshes = [];

this._blurMaterial = null;
this._cubemapMaterial = null;
this._equirectMaterial = null;
this._backgroundBox = null;

}

Expand Down Expand Up @@ -210,6 +212,12 @@ class PMREMGenerator {

if ( this._cubemapMaterial !== null ) this._cubemapMaterial.dispose();
if ( this._equirectMaterial !== null ) this._equirectMaterial.dispose();
if ( this._backgroundBox !== null ) {

this._backgroundBox.geometry.dispose();
this._backgroundBox.material.dispose();

}

}

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

const { _lodMax } = this;
( { sizeLods: this._sizeLods, lodPlanes: this._lodPlanes, sigmas: this._sigmas } = _createPlanes( _lodMax ) );
( { sizeLods: this._sizeLods, lodPlanes: this._lodPlanes, sigmas: this._sigmas, lodMeshes: this._lodMeshes } = _createPlanes( _lodMax ) );

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

Expand All @@ -309,7 +317,9 @@ class PMREMGenerator {

_compileMaterial( material ) {

const tmpMesh = new Mesh( this._lodPlanes[ 0 ], material );
const tmpMesh = this._lodMeshes[ 0 ];
tmpMesh.material = material;

this._renderer.compile( tmpMesh, _flatCamera );

}
Expand All @@ -333,14 +343,20 @@ class PMREMGenerator {
renderer.toneMapping = NoToneMapping;
renderer.autoClear = false;

const backgroundMaterial = new MeshBasicMaterial( {
name: 'PMREM.Background',
side: BackSide,
depthWrite: false,
depthTest: false
} );
let backgroundBox = this._backgroundBox;

const backgroundBox = new Mesh( new BoxGeometry(), backgroundMaterial );
if ( backgroundBox === null ) {

const backgroundMaterial = new MeshBasicMaterial( {
name: 'PMREM.Background',
side: BackSide,
depthWrite: false,
depthTest: false
} );

backgroundBox = new Mesh( new BoxGeometry(), backgroundMaterial );

}

let useSolidColor = false;
const background = scene.background;
Expand All @@ -349,15 +365,15 @@ class PMREMGenerator {

if ( background.isColor ) {

backgroundMaterial.color.copy( background );
backgroundBox.material.color.copy( background );
scene.background = null;
useSolidColor = true;

}

} else {

backgroundMaterial.color.copy( _clearColor );
backgroundBox.material.color.copy( _clearColor );
useSolidColor = true;

}
Expand Down Expand Up @@ -401,9 +417,6 @@ class PMREMGenerator {

}

backgroundBox.geometry.dispose();
backgroundBox.material.dispose();

renderer.toneMapping = toneMapping;
renderer.autoClear = originalAutoClear;
scene.background = background;
Expand Down Expand Up @@ -435,10 +448,11 @@ class PMREMGenerator {
}

const material = isCubeTexture ? this._cubemapMaterial : this._equirectMaterial;
const mesh = new Mesh( this._lodPlanes[ 0 ], material );

material.fragmentNode.value = texture;

const mesh = this._lodMeshes[ 0 ];
mesh.material = material;

const size = this._cubeSize;

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

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

console.error(
'blur direction must be either latitudinal or longitudinal!' );
console.error( 'blur direction must be either latitudinal or longitudinal!' );

}

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

const blurMesh = new Mesh( this._lodPlanes[ lodOut ], blurMaterial );
const blurMesh = this._lodMeshes[ lodOut ];
blurMesh.material = blurMaterial;

const blurUniforms = blurMaterial.uniforms;

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

}

targetIn.texture.frame = ( targetIn.texture.frame || 0 ) + 1;

blurUniforms.envMap.value = targetIn.texture;
blurUniforms.samples.value = samples;
blurUniforms.weights.array = weights;
Expand Down Expand Up @@ -589,6 +606,7 @@ function _createPlanes( lodMax ) {
const lodPlanes = [];
const sizeLods = [];
const sigmas = [];
const lodMeshes = [];

let lod = lodMax;

Expand Down Expand Up @@ -653,6 +671,7 @@ function _createPlanes( lodMax ) {
planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) );
planes.setAttribute( 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) );
lodPlanes.push( planes );
lodMeshes.push( new Mesh( planes, null ) );

if ( lod > LOD_MIN ) {

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

}

return { lodPlanes, sizeLods, sigmas };
return { lodPlanes, sizeLods, sigmas, lodMeshes };

}

Expand Down

0 comments on commit 97bece8

Please sign in to comment.