Skip to content

Commit fe882ea

Browse files
committed
switched to userData
1 parent ad5cb2d commit fe882ea

File tree

3 files changed

+27
-49
lines changed

3 files changed

+27
-49
lines changed

src/nodes/pmrem/PMREMUtils.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -303,19 +303,14 @@ export const getBlurParams=(sigmaRadians, cubeRes, maxSamples)=>{
303303

304304
}
305305

306-
const weights = [];
306+
const weights = new Array( maxSamples ).fill( 0 );
307307
let sum = 0;
308308

309-
for ( let i = 0; i < maxSamples; ++ i ) {
310-
311-
if(i>=samples){
312-
weights.push(0);
313-
continue;
314-
}
309+
for ( let i = 0; i < samples; ++ i ) {
315310

316311
const x = i / sigmaPixels;
317312
const weight = Math.exp( - x * x / 2 );
318-
weights.push( weight );
313+
weights[i]= weight;
319314

320315
if ( i === 0 ) {
321316

src/renderers/common/CubeRenderTarget.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { equirectUV } from '../../nodes/utils/EquirectUVNode.js';
22
import { texture as TSL_Texture } from '../../nodes/accessors/TextureNode.js';
33
import { positionWorldDirection } from '../../nodes/accessors/Position.js';
44
import NodeMaterial from '../../materials/nodes/NodeMaterial.js';
5-
import { blur, getBlurParams } from '../../../nodes/pmrem/PMREMUtils.js';
5+
import { blur, getBlurParams } from '../../nodes/pmrem/PMREMUtils.js';
66

77
import { WebGLCubeRenderTarget } from '../../renderers/WebGLCubeRenderTarget.js';
88
import { Scene } from '../../scenes/Scene.js';

src/renderers/common/extras/PMREMGenerator.js

+23-40
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import NodeMaterial from '../../../materials/nodes/NodeMaterial.js';
22
import { getDirection, blur, bilinearCubeUV, getBlurParams } from '../../../nodes/pmrem/PMREMUtils.js';
33
import { equirectUV } from '../../../nodes/utils/EquirectUVNode.js';
4-
import { uniform } from '../../../nodes/core/UniformNode.js';
5-
import { uniformArray } from '../../../nodes/accessors/UniformArrayNode.js';
4+
import { userData } from '../../../nodes/accessors/UserDataNode.js';
65
import { texture } from '../../../nodes/accessors/TextureNode.js';
76
import { cubeTexture } from '../../../nodes/accessors/CubeTextureNode.js';
8-
import { float, vec3, Fn } from '../../../nodes/tsl/TSLBase.js';
7+
import { float, int, vec3, Fn } from '../../../nodes/tsl/TSLBase.js';
98
import { uv } from '../../../nodes/accessors/UV.js';
109
import { attribute } from '../../../nodes/core/AttributeNode.js';
1110

@@ -622,26 +621,20 @@ class PMREMGenerator {
622621
const blurMesh = this._lodMeshes[ lodOut ];
623622
blurMesh.material = blurMaterial;
624623

625-
const blurUniforms = blurMaterial.uniforms;
626-
627-
const {radiansPerPixel, samples, weights}=getBlurParams(sigmaRadians, this._sizeLods[ lodIn ] - 1, MAX_SAMPLES);
628-
629624
targetIn.texture.frame = ( targetIn.texture.frame || 0 ) + 1;
630-
631-
blurUniforms.envMap.value = targetIn.texture;
632-
blurUniforms.samples.value = samples;
633-
blurUniforms.weights.array = weights;
634-
blurUniforms.latitudinal.value = direction === 'latitudinal' ? 1 : 0;
635-
636-
if ( poleAxis ) {
637-
638-
blurUniforms.poleAxis.value = poleAxis;
639-
640-
}
625+
blurMaterial._envMap.value = targetIn.texture
641626

642627
const { _lodMax } = this;
643-
blurUniforms.dTheta.value = radiansPerPixel;
644-
blurUniforms.mipInt.value = _lodMax - lodIn;
628+
const {radiansPerPixel, samples, weights}=getBlurParams(sigmaRadians, this._sizeLods[ lodIn ] - 1, MAX_SAMPLES);
629+
630+
blurMesh.userData={
631+
samples,
632+
weights,
633+
poleAxis,
634+
latitudinal: direction === 'latitudinal' ? 1 : 0,
635+
dTheta: radiansPerPixel,
636+
mipInt: _lodMax - lodIn
637+
};
645638

646639
const outputSize = this._sizeLods[ lodOut ];
647640
const x = 3 * outputSize * ( lodOut > _lodMax - LOD_MIN ? lodOut - _lodMax + LOD_MIN : 0 );
@@ -771,36 +764,26 @@ function _getMaterial( type ) {
771764

772765
function _getBlurShader( lodMax, width, height ) {
773766

774-
const weights = uniformArray( new Array( MAX_SAMPLES ).fill( 0 ) );
775-
const poleAxis = uniform( new Vector3( 0, 1, 0 ) );
776-
const dTheta = uniform( 0 );
767+
const weights = userData( 'weights', 'float' );
768+
const poleAxis = userData( 'poleAxis', 'vec3' );
769+
const dTheta = userData( 'dTheta', 'float' );
777770
const n = float( MAX_SAMPLES );
778-
const latitudinal = uniform( 0 ); // false, bool
779-
const samples = uniform( 1 ); // int
771+
const latitudinal = userData( 'latitudinal', 'int' ); // bool
772+
const samples = userData( 'samples', 'int' );
773+
const mipInt = userData( 'mipInt', 'int' );
774+
780775
const envMap = texture( null );
781-
const mipInt = uniform( 0 ); // int
782776
const CUBEUV_TEXEL_WIDTH = float( 1 / width );
783777
const CUBEUV_TEXEL_HEIGHT = float( 1 / height );
784778
const CUBEUV_MAX_MIP = float( lodMax );
785779

786-
const materialUniforms = {
787-
n,
788-
latitudinal,
789-
weights,
790-
poleAxis,
791-
outputDirection,
792-
dTheta,
793-
samples,
794-
envMap,
795-
mipInt
796-
};
797-
798780
const material = _getMaterial( 'blur' );
799-
material.uniforms = materialUniforms; // TODO: Move to outside of the material
781+
material._envMap = envMap;
782+
800783
const cubeUVsampler=Fn(( [ sampleDirection ] )=>{
801784
return bilinearCubeUV( envMap, sampleDirection, mipInt, CUBEUV_TEXEL_WIDTH, CUBEUV_TEXEL_HEIGHT, CUBEUV_MAX_MIP );
802785
});
803-
material.fragmentNode = blur( { ...materialUniforms, latitudinal: latitudinal.equal( 1 ), sampler: cubeUVsampler } );
786+
material.fragmentNode = blur( { n, latitudinal: latitudinal.equal( int ( 1 ) ), poleAxis, outputDirection, weights, samples, dTheta, sampler: cubeUVsampler } );
804787

805788
return material;
806789

0 commit comments

Comments
 (0)