Skip to content

Commit 3fbfa2c

Browse files
authored
WebGLRenderer: Fix remaining shadow issues with reversed depth. (#32751)
1 parent a519ddd commit 3fbfa2c

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,24 @@ export default /* glsl */`
132132
float radius = shadowRadius * texelSize.x;
133133
134134
// Use IGN to rotate sampling pattern per pixel
135-
float phi = interleavedGradientNoise( gl_FragCoord.xy ) * 6.28318530718; // 2*PI
135+
float phi = interleavedGradientNoise( gl_FragCoord.xy ) * PI2;
136+
137+
#ifdef USE_REVERSED_DEPTH_BUFFER
138+
139+
float dp = 1.0 - shadowCoord.z;
140+
141+
#else
142+
143+
float dp = shadowCoord.z;
144+
145+
#endif
136146
137147
shadow = (
138-
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 0, 5, phi ) * radius, shadowCoord.z ) ) +
139-
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 1, 5, phi ) * radius, shadowCoord.z ) ) +
140-
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 2, 5, phi ) * radius, shadowCoord.z ) ) +
141-
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 3, 5, phi ) * radius, shadowCoord.z ) ) +
142-
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 4, 5, phi ) * radius, shadowCoord.z ) )
148+
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 0, 5, phi ) * radius, dp ) ) +
149+
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 1, 5, phi ) * radius, dp ) ) +
150+
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 2, 5, phi ) * radius, dp ) ) +
151+
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 3, 5, phi ) * radius, dp ) ) +
152+
texture( shadowMap, vec3( shadowCoord.xy + vogelDiskSample( 4, 5, phi ) * radius, dp ) )
143153
) * 0.2;
144154
145155
}
@@ -167,15 +177,7 @@ export default /* glsl */`
167177
float mean = distribution.x;
168178
float variance = distribution.y * distribution.y;
169179
170-
#ifdef USE_REVERSED_DEPTH_BUFFER
171-
172-
float hard_shadow = step( mean, shadowCoord.z );
173-
174-
#else
175-
176-
float hard_shadow = step( shadowCoord.z, mean );
177-
178-
#endif
180+
float hard_shadow = step( shadowCoord.z, mean );
179181
180182
// Early return if fully lit
181183
if ( hard_shadow == 1.0 ) {
@@ -224,14 +226,12 @@ export default /* glsl */`
224226
225227
#ifdef USE_REVERSED_DEPTH_BUFFER
226228
227-
shadow = step( depth, shadowCoord.z );
228-
229-
#else
230-
231-
shadow = step( shadowCoord.z, depth );
229+
depth = 1.0 - depth;
232230
233231
#endif
234232
233+
shadow = step( shadowCoord.z, depth );
234+
235235
}
236236
237237
return mix( 1.0, shadow, shadowIntensity );
@@ -287,7 +287,7 @@ export default /* glsl */`
287287
vec3 bitangent = cross( bd3D, tangent );
288288
289289
// Use IGN to rotate sampling pattern per pixel
290-
float phi = interleavedGradientNoise( gl_FragCoord.xy ) * 6.28318530718;
290+
float phi = interleavedGradientNoise( gl_FragCoord.xy ) * PI2;
291291
292292
vec2 sample0 = vogelDiskSample( 0, 5, phi );
293293
vec2 sample1 = vogelDiskSample( 1, 5, phi );
@@ -328,16 +328,7 @@ export default /* glsl */`
328328
329329
// viewZ to perspective depth
330330
331-
#ifdef USE_REVERSED_DEPTH_BUFFER
332-
333-
float dp = ( shadowCameraNear * ( shadowCameraFar - viewSpaceZ ) ) / ( viewSpaceZ * ( shadowCameraFar - shadowCameraNear ) );
334-
335-
#else
336-
337-
float dp = ( shadowCameraFar * ( viewSpaceZ - shadowCameraNear ) ) / ( viewSpaceZ * ( shadowCameraFar - shadowCameraNear ) );
338-
339-
#endif
340-
331+
float dp = ( shadowCameraFar * ( viewSpaceZ - shadowCameraNear ) ) / ( viewSpaceZ * ( shadowCameraFar - shadowCameraNear ) );
341332
dp += shadowBias;
342333
343334
// Direction from light to fragment
@@ -347,14 +338,12 @@ export default /* glsl */`
347338
348339
#ifdef USE_REVERSED_DEPTH_BUFFER
349340
350-
shadow = step( depth, dp );
351-
352-
#else
353-
354-
shadow = step( dp, depth );
341+
depth = 1.0 - depth;
355342
356343
#endif
357344
345+
shadow = step( dp, depth );
346+
358347
}
359348
360349
return mix( 1.0, shadow, shadowIntensity );

src/renderers/shaders/ShaderLib/vsm.glsl.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ void main() {
3333
#else
3434
3535
float depth = texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0, uvOffset ) * radius ) / resolution ).r;
36+
37+
#ifdef USE_REVERSED_DEPTH_BUFFER
38+
39+
depth = 1.0 - depth;
40+
41+
#endif
42+
3643
mean += depth;
3744
squared_mean += depth * depth;
3845

0 commit comments

Comments
 (0)