From 97bece876ffd8303fc98598738dc4fa629f60efd Mon Sep 17 00:00:00 2001 From: sunag Date: Sun, 24 Mar 2024 11:56:48 -0300 Subject: [PATCH] PMREMGenerator: Reuse Meshes --- .../renderers/common/extras/PMREMGenerator.js | 59 ++++++++++++------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/examples/jsm/renderers/common/extras/PMREMGenerator.js b/examples/jsm/renderers/common/extras/PMREMGenerator.js index 8e04e6aec2b7e5..d0ca98466cb1ed 100644 --- a/examples/jsm/renderers/common/extras/PMREMGenerator.js +++ b/examples/jsm/renderers/common/extras/PMREMGenerator.js @@ -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; } @@ -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(); + + } } @@ -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 ); @@ -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 ); } @@ -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; @@ -349,7 +365,7 @@ class PMREMGenerator { if ( background.isColor ) { - backgroundMaterial.color.copy( background ); + backgroundBox.material.color.copy( background ); scene.background = null; useSolidColor = true; @@ -357,7 +373,7 @@ class PMREMGenerator { } else { - backgroundMaterial.color.copy( _clearColor ); + backgroundBox.material.color.copy( _clearColor ); useSolidColor = true; } @@ -401,9 +417,6 @@ class PMREMGenerator { } - backgroundBox.geometry.dispose(); - backgroundBox.material.dispose(); - renderer.toneMapping = toneMapping; renderer.autoClear = originalAutoClear; scene.background = background; @@ -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 ); @@ -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; @@ -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; @@ -589,6 +606,7 @@ function _createPlanes( lodMax ) { const lodPlanes = []; const sizeLods = []; const sigmas = []; + const lodMeshes = []; let lod = lodMax; @@ -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 ) { @@ -662,7 +681,7 @@ function _createPlanes( lodMax ) { } - return { lodPlanes, sizeLods, sigmas }; + return { lodPlanes, sizeLods, sigmas, lodMeshes }; }