Skip to content

Commit 853740e

Browse files
committed
Merge pull request #91191 from clayjohn/RD-soft-shadows
Properly calculate penumbra for soft shadows with reverse z
2 parents 9bb448f + 4e5e81c commit 853740e

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

servers/rendering/renderer_rd/shaders/forward_clustered/scene_forward_clustered.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2057,7 +2057,7 @@ void fragment_shader(in SceneData scene_data) {
20572057
shadow = 1.0;
20582058
#endif
20592059

2060-
float size_A = sc_use_light_soft_shadows ? directional_lights.data[i].size : 0.0;
2060+
float size_A = sc_use_directional_soft_shadows ? directional_lights.data[i].size : 0.0;
20612061

20622062
light_compute(normal, directional_lights.data[i].direction, normalize(view), size_A,
20632063
#ifndef DEBUG_DRAW_PSSM_SPLITS

servers/rendering/renderer_rd/shaders/scene_forward_lights_inc.glsl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ float sample_directional_soft_shadow(texture2D shadow, vec3 pssm_coord, vec2 tex
375375
for (uint i = 0; i < sc_directional_penumbra_shadow_samples; i++) {
376376
vec2 suv = pssm_coord.xy + (disk_rotation * scene_data_block.data.directional_penumbra_shadow_kernel[i].xy) * tex_scale;
377377
float d = textureLod(sampler2D(shadow, SAMPLER_LINEAR_CLAMP), suv, 0.0).r;
378-
if (d < pssm_coord.z) {
378+
if (d > pssm_coord.z) {
379379
blocker_average += d;
380380
blocker_count += 1.0;
381381
}
@@ -384,7 +384,7 @@ float sample_directional_soft_shadow(texture2D shadow, vec3 pssm_coord, vec2 tex
384384
if (blocker_count > 0.0) {
385385
//blockers found, do soft shadow
386386
blocker_average /= blocker_count;
387-
float penumbra = (pssm_coord.z - blocker_average) / blocker_average;
387+
float penumbra = (-pssm_coord.z + blocker_average) / (1.0 - blocker_average);
388388
tex_scale *= penumbra;
389389

390390
float s = 0.0;
@@ -488,7 +488,7 @@ float light_process_omni_shadow(uint idx, vec3 vertex, vec3 normal) {
488488
if (blocker_count > 0.0) {
489489
//blockers found, do soft shadow
490490
blocker_average /= blocker_count;
491-
float penumbra = (z_norm + blocker_average) / blocker_average;
491+
float penumbra = (-z_norm + blocker_average) / (1.0 - blocker_average);
492492
tangent *= penumbra;
493493
bitangent *= penumbra;
494494

@@ -736,7 +736,7 @@ float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
736736
vec2 suv = shadow_uv + (disk_rotation * scene_data_block.data.penumbra_shadow_kernel[i].xy) * uv_size;
737737
suv = clamp(suv, spot_lights.data[idx].atlas_rect.xy, clamp_max);
738738
float d = textureLod(sampler2D(shadow_atlas, SAMPLER_LINEAR_CLAMP), suv, 0.0).r;
739-
if (d < splane.z) {
739+
if (d > splane.z) {
740740
blocker_average += d;
741741
blocker_count += 1.0;
742742
}
@@ -745,7 +745,7 @@ float light_process_spot_shadow(uint idx, vec3 vertex, vec3 normal) {
745745
if (blocker_count > 0.0) {
746746
//blockers found, do soft shadow
747747
blocker_average /= blocker_count;
748-
float penumbra = (z_norm - blocker_average) / blocker_average;
748+
float penumbra = (-z_norm + blocker_average) / (1.0 - blocker_average);
749749
uv_size *= penumbra;
750750

751751
shadow = 0.0;

0 commit comments

Comments
 (0)