Skip to content

Commit

Permalink
switched to userData
Browse files Browse the repository at this point in the history
  • Loading branch information
elalish committed Dec 19, 2024
1 parent ad5cb2d commit fe882ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
11 changes: 3 additions & 8 deletions src/nodes/pmrem/PMREMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,19 +303,14 @@ export const getBlurParams=(sigmaRadians, cubeRes, maxSamples)=>{

}

const weights = [];
const weights = new Array( maxSamples ).fill( 0 );
let sum = 0;

for ( let i = 0; i < maxSamples; ++ i ) {

if(i>=samples){
weights.push(0);
continue;
}
for ( let i = 0; i < samples; ++ i ) {

const x = i / sigmaPixels;
const weight = Math.exp( - x * x / 2 );
weights.push( weight );
weights[i]= weight;

Check failure on line 313 in src/nodes/pmrem/PMREMUtils.js

View workflow job for this annotation

GitHub Actions / Lint testing

A space is required after '['

Check failure on line 313 in src/nodes/pmrem/PMREMUtils.js

View workflow job for this annotation

GitHub Actions / Lint testing

A space is required before ']'

Check failure on line 313 in src/nodes/pmrem/PMREMUtils.js

View workflow job for this annotation

GitHub Actions / Lint testing

Operator '=' must be spaced

if ( i === 0 ) {

Expand Down
2 changes: 1 addition & 1 deletion src/renderers/common/CubeRenderTarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { equirectUV } from '../../nodes/utils/EquirectUVNode.js';
import { texture as TSL_Texture } from '../../nodes/accessors/TextureNode.js';
import { positionWorldDirection } from '../../nodes/accessors/Position.js';
import NodeMaterial from '../../materials/nodes/NodeMaterial.js';
import { blur, getBlurParams } from '../../../nodes/pmrem/PMREMUtils.js';
import { blur, getBlurParams } from '../../nodes/pmrem/PMREMUtils.js';

import { WebGLCubeRenderTarget } from '../../renderers/WebGLCubeRenderTarget.js';
import { Scene } from '../../scenes/Scene.js';
Expand Down
63 changes: 23 additions & 40 deletions src/renderers/common/extras/PMREMGenerator.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import NodeMaterial from '../../../materials/nodes/NodeMaterial.js';
import { getDirection, blur, bilinearCubeUV, getBlurParams } from '../../../nodes/pmrem/PMREMUtils.js';
import { equirectUV } from '../../../nodes/utils/EquirectUVNode.js';
import { uniform } from '../../../nodes/core/UniformNode.js';
import { uniformArray } from '../../../nodes/accessors/UniformArrayNode.js';
import { userData } from '../../../nodes/accessors/UserDataNode.js';
import { texture } from '../../../nodes/accessors/TextureNode.js';
import { cubeTexture } from '../../../nodes/accessors/CubeTextureNode.js';
import { float, vec3, Fn } from '../../../nodes/tsl/TSLBase.js';
import { float, int, vec3, Fn } from '../../../nodes/tsl/TSLBase.js';
import { uv } from '../../../nodes/accessors/UV.js';
import { attribute } from '../../../nodes/core/AttributeNode.js';

Expand Down Expand Up @@ -622,26 +621,20 @@ class PMREMGenerator {
const blurMesh = this._lodMeshes[ lodOut ];
blurMesh.material = blurMaterial;

const blurUniforms = blurMaterial.uniforms;

const {radiansPerPixel, samples, weights}=getBlurParams(sigmaRadians, this._sizeLods[ lodIn ] - 1, MAX_SAMPLES);

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

blurUniforms.envMap.value = targetIn.texture;
blurUniforms.samples.value = samples;
blurUniforms.weights.array = weights;
blurUniforms.latitudinal.value = direction === 'latitudinal' ? 1 : 0;

if ( poleAxis ) {

blurUniforms.poleAxis.value = poleAxis;

}
blurMaterial._envMap.value = targetIn.texture

Check notice

Code scanning / CodeQL

Semicolon insertion Note

Avoid automated semicolon insertion (93% of all statements in
the enclosing function
have an explicit semicolon).

const { _lodMax } = this;
blurUniforms.dTheta.value = radiansPerPixel;
blurUniforms.mipInt.value = _lodMax - lodIn;
const {radiansPerPixel, samples, weights}=getBlurParams(sigmaRadians, this._sizeLods[ lodIn ] - 1, MAX_SAMPLES);

blurMesh.userData={
samples,
weights,
poleAxis,
latitudinal: direction === 'latitudinal' ? 1 : 0,
dTheta: radiansPerPixel,
mipInt: _lodMax - lodIn
};

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

function _getBlurShader( lodMax, width, height ) {

const weights = uniformArray( new Array( MAX_SAMPLES ).fill( 0 ) );
const poleAxis = uniform( new Vector3( 0, 1, 0 ) );
const dTheta = uniform( 0 );
const weights = userData( 'weights', 'float' );
const poleAxis = userData( 'poleAxis', 'vec3' );
const dTheta = userData( 'dTheta', 'float' );
const n = float( MAX_SAMPLES );
const latitudinal = uniform( 0 ); // false, bool
const samples = uniform( 1 ); // int
const latitudinal = userData( 'latitudinal', 'int' ); // bool
const samples = userData( 'samples', 'int' );
const mipInt = userData( 'mipInt', 'int' );

const envMap = texture( null );
const mipInt = uniform( 0 ); // int
const CUBEUV_TEXEL_WIDTH = float( 1 / width );
const CUBEUV_TEXEL_HEIGHT = float( 1 / height );
const CUBEUV_MAX_MIP = float( lodMax );

const materialUniforms = {
n,
latitudinal,
weights,
poleAxis,
outputDirection,
dTheta,
samples,
envMap,
mipInt
};

const material = _getMaterial( 'blur' );
material.uniforms = materialUniforms; // TODO: Move to outside of the material
material._envMap = envMap;

const cubeUVsampler=Fn(( [ sampleDirection ] )=>{
return bilinearCubeUV( envMap, sampleDirection, mipInt, CUBEUV_TEXEL_WIDTH, CUBEUV_TEXEL_HEIGHT, CUBEUV_MAX_MIP );
});
material.fragmentNode = blur( { ...materialUniforms, latitudinal: latitudinal.equal( 1 ), sampler: cubeUVsampler } );
material.fragmentNode = blur( { n, latitudinal: latitudinal.equal( int ( 1 ) ), poleAxis, outputDirection, weights, samples, dTheta, sampler: cubeUVsampler } );

return material;

Expand Down

0 comments on commit fe882ea

Please sign in to comment.