---
id: "sdf-soft-shadows"
title: "SDF Soft Shadows"
domain: "visibility"
stage: "visibility"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, mobile-gpu]
year_introduced: 2008
inputs: [sdf]
outputs: [visibility-mask]
style: "both"
dynamic: "dynamic"
cost: "medium"
authority: "Quilez"
url: "https://texelatlas.vercel.app/t/sdf-soft-shadows/"
sources_verified_at: "2026-11-29"
---
# SDF Soft Shadows (Quilez)

> 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.

## 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

## 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 use it

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](https://texelatlas.vercel.app/t/shadow-mapping.md) with [Percentage-Closer Soft Shadows](https://texelatlas.vercel.app/t/percentage-closer-soft-shadows.md) for triangle-mesh scenes, [Ray-Traced Shadows](https://texelatlas.vercel.app/t/ray-traced-shadows.md) for accurate penumbrae against meshes, and [Screen-Space Contact Shadows](https://texelatlas.vercel.app/t/contact-shadows.md) for a screen-space supplement.

## Cost

medium — Tens of SDF evaluations per pixel; cost depends on the complexity of the field function.
Measured GPU Weight: 24.84× baseline (heavy) — 0.8173 ms median / 0.842 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 22.43× baseline (heavy) — 0.3566 ms median (2.29× faster on L4).
Ampere A100 scaling: 14.67× baseline (moderate) — 0.4224 ms median (1.93× faster on A100).

## Failure modes

- 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

## Engine support

- unreal: builtin — `Project Settings > Rendering > Generate Mesh Distance Fields; Light > Mobility = Movable/Stationary > Distance Field Shadows` (evidence: https://dev.epicgames.com/documentation/en-us/unreal-engine/distance-field-soft-shadows-in-unreal-engine, checked 2026-12-01)

## Dataflow

- Inputs: sdf
- Outputs: visibility-mask
- Platforms: webgl2, webgpu, desktop-gpu, mobile-gpu

## Presets

### Raymarched spheres and box with soft shadows (glsl-fragment; rendered 2026-11-29)

```json
{
  "SHADOW_K": 10,
  "SHADOW_STEPS": 48
}
```

```glsl
// 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

## Sources

1. [primary] Inigo Quilez Soft shadows in raymarched SDFs. iquilezles.org. https://iquilezles.org/articles/rmshadows (verified via http on 2026-11-29)

## Cite this entry

Texel. "SDF Soft Shadows." https://texelatlas.vercel.app/t/sdf-soft-shadows/ (accessed 2026-12-01).
