Files
totk-pc-recomp/runtime/shaders/contact_shadow.comp
T
itaskaev2andClaude Opus 5.5 a3fee27cfe 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>
2026-11-14 23:35:24 +01:00

91 lines
4.4 KiB
Plaintext

#version 450
// 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
layout(binding = 1, r11f_g11f_b10f) uniform image2D scene; // the lit scene colour, darkened in place
layout(push_constant) uniform Params {
vec4 proj; // projection x scale, y scale, far plane, strength (0..1)
vec4 light; // view-space direction toward the sun (xyz), march length in metres at 10 m (w)
vec4 size; // width, height, 1/width, 1/height
vec4 opt; // x: debug (1: write the shadow factor as grey)
} pc;
vec3 view_pos(vec2 uv, float z) { // z: distance in front of the camera
vec2 ndc = uv * 2.0 - 1.0;
return vec3(ndc.x * z / pc.proj.x, -ndc.y * z / pc.proj.y, -z);
}
vec2 to_uv(vec3 p) {
float z = -p.z;
return vec2(0.5 + 0.5 * p.x * pc.proj.x / z, 0.5 - 0.5 * p.y * pc.proj.y / z);
}
float ign(vec2 px) { // interleaved gradient noise: a per-pixel start offset that hides the step pattern
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;
float d = texelFetch(lin_depth, px, 0).x;
if (d <= 0.0 || d >= 0.999) return; // nothing drawn, or the sky
float z = d * pc.proj.z;
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;
const int STEPS = 16;
float step_len = len / float(STEPS), t = step_len * (0.25 + ign(vec2(px)));
float occ = 0.0;
for (int i = 0; i < STEPS; i++, t += step_len) {
vec3 q = p + pc.light.xyz * t;
if (q.z >= -0.05) break;
vec2 quv = to_uv(q);
if (any(lessThan(quv, vec2(0.0))) || any(greaterThan(quv, vec2(1.0)))) break;
float sz = texelFetch(lin_depth, ivec2(quv * pc.size.xy), 0).x * pc.proj.z;
float delta = -q.z - sz; // how far the scene at that pixel is in front of the ray point
if (delta > 0.02 + z * 0.002 && delta < thick) {
occ = 1.0 - float(i) / float(STEPS); // a hit close to the pixel shadows more than one at the end
break;
}
}
float fade = 1.0 - smoothstep(40.0, 80.0, z);
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;
}
if (k >= 0.999) return;
vec4 c = imageLoad(scene, px);
imageStore(scene, px, vec4(c.rgb * k, c.a));
}