Leave surfaces turned away from the sun out of the contact shadows

The contact shadow march darkened the whole lit colour of every pixel whose ray toward the sun passed behind
geometry. On a face turned away from the sun the ray runs into that face at once, so every such face within 80 m
was darkened by the full strength, although it gets no direct sunlight to lose: what was darkened was its ambient
light and the fog the composite had already added. In hazy scenes (the Gerudo sandstorm) the shaded rock faces and
the undersides of overhangs showed as dark smoky silhouettes in the haze, and objects such as a wagon lost their
fogged look. The shader now takes the surface normal from the linear depth and scales the shadow by how far the
surface faces the sun: none for faces turned away, a growing share from a grazing angle on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
itaskaev2andClaude Opus 5.5 committed 2026-11-14 23:35:24 +01:00
1 parent 94eb3d4628
commit a3fee27cfe
3 files changed
+263 -155

No files matched your search

+27 -4
View File
@@ -1,7 +1,8 @@
#version 450
// Screen-space contact shadows (runtime/vk_contact.c). Each pixel of the game's lit scene colour marches a short way
// toward the sun through the linear depth target; when the ray passes behind something close in front of it, the pixel
// is in a contact shadow the shadow maps are too coarse to show, and its colour is darkened.
// Screen-space contact shadows (runtime/vk_contact.c). Each pixel of the game's lit scene colour on a surface facing
// the sun marches a short way toward the sun through the linear depth target; when the ray passes behind something
// close in front of it, the pixel is in a contact shadow the shadow maps are too coarse to show, and its colour is
// darkened.
layout(local_size_x = 8, local_size_y = 8) in;
layout(binding = 0) uniform sampler2D lin_depth; // the game's linear depth: view distance / far
@@ -28,6 +29,22 @@ float ign(vec2 px) { // interleaved gradient noise: a per-pixel start offset t
return fract(52.9829189 * fract(dot(px, vec2(0.06711056, 0.00583715))));
}
vec3 pos_at(ivec2 q) { // the view-space position of a pixel, clamped to the image
q = clamp(q, ivec2(0), ivec2(pc.size.xy) - 1);
return view_pos((vec2(q) + 0.5) * pc.size.zw, texelFetch(lin_depth, q, 0).x * pc.proj.z);
}
// The surface normal from the linear depth: on each axis the neighbour nearer in depth, so an edge pixel takes the
// slope of its own surface rather than the step to what lies behind it. Turned toward the camera.
vec3 normal_at(ivec2 px, vec3 p) {
vec3 l = pos_at(px - ivec2(1, 0)), r = pos_at(px + ivec2(1, 0));
vec3 d = pos_at(px - ivec2(0, 1)), u = pos_at(px + ivec2(0, 1));
vec3 dx = abs(r.z - p.z) < abs(p.z - l.z) ? r - p : p - l;
vec3 dy = abs(u.z - p.z) < abs(p.z - d.z) ? u - p : p - d;
vec3 n = normalize(cross(dx, dy));
return dot(n, p) > 0.0 ? -n : n;
}
void main() {
ivec2 px = ivec2(gl_GlobalInvocationID.xy);
if (px.x >= int(pc.size.x) || px.y >= int(pc.size.y)) return;
@@ -37,6 +54,12 @@ void main() {
if (z > 80.0) return;
vec2 uv = (vec2(px) + 0.5) * pc.size.zw;
vec3 p = view_pos(uv, z);
// A contact shadow can only take away direct sunlight, and a surface turned away from the sun gets none: its march
// runs into the surface itself at once, and darkening it would darken the ambient light and the fog the composite
// already put on it (dark silhouettes of rock faces in the Gerudo sandstorm haze). Faces at a grazing angle to the
// sun get a share that grows with that angle.
float lit = smoothstep(0.0, 0.3, dot(normal_at(px, p), pc.light.xyz));
if (lit <= 0.0) return;
// the march grows with distance, as one pixel covers more ground; thickness keeps thin blades from hiding the sky
float len = pc.light.w * clamp(z / 10.0, 0.4, 3.0);
float thick = 0.15 + z * 0.01;
@@ -56,7 +79,7 @@ void main() {
}
}
float fade = 1.0 - smoothstep(40.0, 80.0, z);
float k = 1.0 - pc.proj.w * occ * fade;
float k = 1.0 - pc.proj.w * occ * fade * lit;
if (pc.opt.x > 0.5) {
imageStore(scene, px, vec4(k, k, k, 1.0));
return;