From bdf33457c85270557eb5924af19513f0ac5b88db Mon Sep 17 00:00:00 2001 From: Michael Herzog Date: Sat, 17 Jan 2026 16:47:46 +0100 Subject: [PATCH] Examples: Add support for reversed depth to post processing modules. (#32773) --- examples/jsm/shaders/SAOShader.js | 15 ++++++++++-- examples/jsm/shaders/SSAOShader.js | 12 +++++++++- .../shaders/ShaderChunk/packing.glsl.js | 24 +++++++++++++++---- .../shadowmap_pars_fragment.glsl.js | 20 ++++------------ 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/examples/jsm/shaders/SAOShader.js b/examples/jsm/shaders/SAOShader.js index dbf9c6ff371f8e..7ee8330457d08d 100644 --- a/examples/jsm/shaders/SAOShader.js +++ b/examples/jsm/shaders/SAOShader.js @@ -86,6 +86,17 @@ const SAOShader = { #include + #ifdef USE_REVERSED_DEPTH_BUFFER + + const float depthThreshold = 0.0 + EPSILON; + + #else + + const float depthThreshold = 1.0 - EPSILON; + + #endif + + vec4 getDefaultColor( const in vec2 screenPosition ) { #if DIFFUSE_TEXTURE == 1 return texture2D( tDiffuse, vUv ); @@ -153,7 +164,7 @@ const SAOShader = { angle += ANGLE_STEP; float sampleDepth = getDepth( sampleUv ); - if( sampleDepth >= ( 1.0 - EPSILON ) ) { + if( sampleDepth >= depthThreshold ) { continue; } @@ -170,7 +181,7 @@ const SAOShader = { void main() { float centerDepth = getDepth( vUv ); - if( centerDepth >= ( 1.0 - EPSILON ) ) { + if( centerDepth >= depthThreshold ) { discard; } diff --git a/examples/jsm/shaders/SSAOShader.js b/examples/jsm/shaders/SSAOShader.js index ab1dc6f25eca7e..c24edfbb0eb1d2 100644 --- a/examples/jsm/shaders/SSAOShader.js +++ b/examples/jsm/shaders/SSAOShader.js @@ -79,6 +79,16 @@ const SSAOShader = { #include + #ifdef USE_REVERSED_DEPTH_BUFFER + + const float depthThreshold = 0.0; + + #else + + const float depthThreshold = 1.0; + + #endif + float getDepth( const in vec2 screenPosition ) { return texture2D( tDepth, screenPosition ).x; @@ -137,7 +147,7 @@ const SSAOShader = { float depth = getDepth( vUv ); - if ( depth == 1.0 ) { + if ( depth == depthThreshold ) { gl_FragColor = vec4( 1.0 ); // don't influence background diff --git a/src/renderers/shaders/ShaderChunk/packing.glsl.js b/src/renderers/shaders/ShaderChunk/packing.glsl.js index d784ad078476a5..af7c2066ec5eaf 100644 --- a/src/renderers/shaders/ShaderChunk/packing.glsl.js +++ b/src/renderers/shaders/ShaderChunk/packing.glsl.js @@ -82,8 +82,16 @@ float viewZToOrthographicDepth( const in float viewZ, const in float near, const } float orthographicDepthToViewZ( const in float depth, const in float near, const in float far ) { - // maps orthographic depth in [ 0, 1 ] to viewZ - return depth * ( near - far ) - near; + + #ifdef USE_REVERSED_DEPTH_BUFFER + + return depth * ( far - near ) - far; + + #else + + return depth * ( near - far ) - near; + + #endif } // NOTE: https://twitter.com/gonnavis/status/1377183786949959682 @@ -94,7 +102,15 @@ float viewZToPerspectiveDepth( const in float viewZ, const in float near, const } float perspectiveDepthToViewZ( const in float depth, const in float near, const in float far ) { - // maps perspective depth in [ 0, 1 ] to viewZ - return ( near * far ) / ( ( far - near ) * depth - far ); + + #ifdef USE_REVERSED_DEPTH_BUFFER + + return ( near * far ) / ( ( near - far ) * depth - near ); + + #else + + return ( near * far ) / ( ( far - near ) * depth - far ); + + #endif } `; diff --git a/src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js b/src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js index 201ae9f70475d9..265ca8e82c2d75 100644 --- a/src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js +++ b/src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js @@ -134,22 +134,12 @@ export default /* glsl */` // Use IGN to rotate sampling pattern per pixel float phi = interleavedGradientNoise( gl_FragCoord.xy ) * PI2; - #ifdef USE_REVERSED_DEPTH_BUFFER - - float dp = 1.0 - shadowCoord.z; - - #else - - float dp = shadowCoord.z; - - #endif - shadow = ( - texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 0, 5, phi ) * radius, dp ) ) + - texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 1, 5, phi ) * radius, dp ) ) + - texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 2, 5, phi ) * radius, dp ) ) + - texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 3, 5, phi ) * radius, dp ) ) + - texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 4, 5, phi ) * radius, dp ) ) + texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 0, 5, phi ) * radius, shadowCoord.z ) ) + + texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 1, 5, phi ) * radius, shadowCoord.z ) ) + + texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 2, 5, phi ) * radius, shadowCoord.z ) ) + + texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 3, 5, phi ) * radius, shadowCoord.z ) ) + + texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 4, 5, phi ) * radius, shadowCoord.z ) ) ) * 0.2; }