Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/nodes/lighting/ShadowFilterNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ export const PCFSoftShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoo
*/
export const VSMShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoord, depthLayer } ) => {

const occlusion = float( 1 ).toVar();

let distribution = texture( depthTexture ).sample( shadowCoord.xy );

if ( depthTexture.isArrayTexture ) {
Expand All @@ -197,19 +195,28 @@ export const VSMShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoord,

distribution = distribution.rg;

const hardShadow = step( shadowCoord.z, distribution.x );
const mean = distribution.x;
const variance = max( 0.0000001, distribution.y.mul( distribution.y ) );

const hardShadow = step( shadowCoord.z, mean );

If( hardShadow.notEqual( float( 1.0 ) ), () => {
// Early return if fully lit
If( hardShadow.equal( 1.0 ), () => {

const distance = shadowCoord.z.sub( distribution.x );
const variance = max( 0, distribution.y.mul( distribution.y ) );
let softnessProbability = variance.div( variance.add( distance.mul( distance ) ) ); // Chebeyshevs inequality
softnessProbability = clamp( sub( softnessProbability, 0.3 ).div( 0.95 - 0.3 ) );
occlusion.assign( clamp( max( hardShadow, softnessProbability ) ) );
return float( 1.0 );
Copy link
Collaborator

@Mugen87 Mugen87 Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line triggers a new warning in the VSM demo webgpu_shadowmap_vsm:

THREE.TSL: Return statement used in an inline 'Fn()'. Define a layout struct to allow return values.

/cc @sunag


} );

return occlusion;
// Distance from mean
const d = shadowCoord.z.sub( mean );

// Chebyshev's inequality for upper bound on probability
let p_max = variance.div( variance.add( d.mul( d ) ) );

// Reduce light bleeding by remapping [amount, 1] to [0, 1]
p_max = clamp( sub( p_max, 0.3 ).div( 0.65 ) );

return max( hardShadow, p_max );

} );

Expand Down