SDF Soft Shadows
While sphere-tracing toward the light, the smallest ratio of distance-to-surface over travelled distance approximates the light's unoccluded cone, giving cheap penumbrae for free.
In the family Visibility, shadows & ambient occlusion, distance field shadows
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | |
| Unity | None found | |
| Unreal 5.7 | Built in | Project Settings > Rendering > Generate Mesh Distance Fields; Light > Mobility = Movable/Stationary > Distance Field Shadows docs |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
A shadow ray is marched through a signed distance field from the shaded point toward the light. At each step, the SDF value d at distance t tells how close the ray passes to geometry; the ratio k*d/t is the sine-like angle of the nearest occluder as seen from the origin. Keeping the minimum of this ratio over the ray, clamped to 0..1, gives a visibility value that is 1 in open space, falls smoothly near silhouettes and reaches 0 when the ray hits geometry. The constant k sets penumbra width. Improvements use the previous step's distance to estimate the closest approach more accurately, remove banding, and smooth the result. The same field also serves AO and global-illumination queries.
When to choose it
Reach for it when
- soft shadows needed in ray marched scenes
- cheap penumbra without many shadow rays
- shadertoy style scene needs realistic shading
- sdf rendering shadows look hard and aliased
Choose it when the scene is already an SDF (raymarched demos, shader-art, Dreams-style content, or a baked or voxelised distance field) and you want cheap soft shadows with no shadow map. Prefer Shadow Mapping with Percentage-Closer Soft Shadows for triangle-mesh scenes, Ray-Traced Shadows for accurate penumbrae against meshes, and Screen-Space Contact Shadows for a screen-space supplement.
Field marks
- Penumbra is an approximation of one light cone and is not physically accurate for large lights
- Banding or light leaks if the step size is clamped too low or too high
- Inexact (Lipschitz-violating) SDFs from domain warping give artifacts
- Self-shadowing at grazing angles needs a normal offset
Presets
Raymarched spheres and box with soft shadows
{
"SHADOW_K": 10,
"SHADOW_STEPS": 48
}Show GLSL (58 lines)
// GLSL ES 3.0, Shadertoy-style: raymarched SDF scene with soft shadows
const float SHADOW_K = 10.0; // penumbra sharpness (higher = harder)
const int SHADOW_STEPS = 48;
const vec3 LIGHT_DIR = normalize(vec3(-0.6, 0.8, 0.4));
float sdSphere(vec3 p, float r) { return length(p) - r; }
float sdBox(vec3 p, vec3 b) { vec3 q = abs(p) - b; return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0); }
float map(vec3 p) {
float ground = p.y + 1.0;
float s = sdSphere(p - vec3(-0.9, -0.1 + 0.15 * sin(iTime), 0.0), 0.9);
float b = sdBox(p - vec3(1.1, -0.4, 0.3), vec3(0.6, 0.6, 0.6));
return min(ground, min(s, b));
}
vec3 calcNormal(vec3 p) {
vec2 e = vec2(0.001, 0.0);
return normalize(vec3(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}
// Soft shadow: track the narrowest cone (min of k*d/t) seen along the ray to the light.
float softShadow(vec3 ro, vec3 rd) {
float res = 1.0;
float t = 0.02;
for (int i = 0; i < SHADOW_STEPS; i++) {
float d = map(ro + rd * t);
res = min(res, SHADOW_K * d / t);
t += clamp(d, 0.02, 0.5);
if (res < 0.001 || t > 12.0) break;
}
res = clamp(res, 0.0, 1.0);
return res * res * (3.0 - 2.0 * res);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
vec3 ro = vec3(0.0, 1.6, -5.5);
vec3 rd = normalize(vec3(uv.x, uv.y - 0.2, 1.6));
float t = 0.0;
bool hit = false;
for (int i = 0; i < 100; i++) {
float d = map(ro + rd * t);
if (d < 0.001) { hit = true; break; }
t += d;
if (t > 30.0) break;
}
vec3 col = mix(vec3(0.75, 0.85, 1.0), vec3(0.3, 0.5, 0.9), clamp(uv.y + 0.6, 0.0, 1.0));
if (hit) {
vec3 p = ro + rd * t;
vec3 n = calcNormal(p);
float diff = clamp(dot(n, LIGHT_DIR), 0.0, 1.0);
float sh = softShadow(p + n * 0.01, LIGHT_DIR);
vec3 albedo = (p.y < -0.99) ? vec3(0.8) * (0.8 + 0.2 * step(0.0, sin(p.x * 3.0) * sin(p.z * 3.0))) : vec3(0.9, 0.45, 0.3);
float sky = 0.5 + 0.5 * n.y;
col = albedo * (vec3(1.0, 0.95, 0.85) * diff * sh * 1.2 + vec3(0.3, 0.4, 0.55) * sky * 0.35);
}
fragColor = vec4(pow(col, vec3(1.0 / 2.2)), 1.0);
}Attribution: written for this index
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| sdf-soft-shadows-spheres-box | Tesla T4 1920x1080 | 24.8× baseline (heavy) · 0.817 ms / 0.842 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| sdf-soft-shadows-spheres-box | NVIDIA L4 1920x1080 | 22.4× baseline (heavy) · 0.357 ms / 0.361 ms · 2.3× faster on L4 | opengl-timer-query · Cross-architecture validation |
| sdf-soft-shadows-spheres-box | NVIDIA A100-SXM4-40GB 1920x1080 | 14.7× baseline (moderate) · 0.422 ms / 0.428 ms · 1.9× faster on A100 | opengl-timer-query · Cross-architecture validation |
Sources
- primaryInigo Quilez. Soft shadows in raymarched SDFs. iquilezles.org.Verified via live page · 2026-11-29