Signed distance functions and sphere tracing
A signed distance function d = sdf(p) returns the shortest distance from any point p in space to a shape's surface: negative inside, zero on the boundary, positive outside. Common primitives — sphere, box, torus, capsule — have elegant one-to-five-line GLSL implementations. The key property is the Lipschitz bound: at any point outside all geometry, a ray can safely advance by exactly |sdf(p)| without ever overshooting a surface. Sphere tracing (John C. Hart, 1996) exploits this directly, converging quadratically near smooth surfaces:
t = 0 loop up to MAX_STEPS: pos = ro + t·rd d = scene(pos) if d < EPSILON → HIT at distance t if t > T_FAR → MISS (sky) t += d
Booleans, normals, and shadows for free
Because the whole scene is a function, constructive solid geometry reduces to arithmetic: union is min(a,b), intersection is max(a,b), subtraction is max(−a,b), and a smooth blend uses a polynomial smin(a,b,k) for rounded fillets. Surface normals fall out of the SDF's gradient via a four-sample tetrahedron finite difference. Soft shadows march a secondary ray toward the light and track the minimum SDF-to-distance ratio along the path; ambient occlusion compares the SDF a short distance along the normal against the unoccluded expectation. None of this requires a separate shadow map or AO pass — it's the same distance function, sampled differently.
Fractals and infinite detail
A single mod call on the ray position before evaluating the SDF tiles a scene infinitely at essentially zero extra cost — the basis of ray-marched fractal renderers (Mandelbulbs, Menger sponges) that would be impossible to represent as explicit meshes. In production, tricks like bounding-volume early-outs, reduced step counts for distant geometry, and rendering at half resolution before upscaling keep frame rates real-time even as scene complexity grows.
Frequently asked questions
What is a signed distance function?
A signed distance function d = sdf(p) returns the shortest distance from a point p to the nearest surface of a shape: negative inside, zero on the surface, positive outside. This guarantees a ray can always safely advance by exactly |sdf(p)| without ever overshooting geometry.
How does sphere tracing work?
Sphere tracing (John C. Hart, 1996) steps a ray forward by the scene SDF's value at each point, since that distance is guaranteed safe. It repeats until the distance falls below a small epsilon (a hit) or the ray exceeds a maximum range (a miss), converging quadratically near smooth surfaces.
Why use ray marching instead of traditional triangle rasterisation?
SDF ray marching needs no mesh, UV mapping or polygon budget — smooth boolean operations, soft shadows, ambient occlusion and infinitely detailed fractals fall out of simple arithmetic on distance values. The tradeoff is per-pixel cost: every ray evaluates the scene function dozens of times, so scene complexity is bounded by GPU shader performance rather than triangle count.
Try it live
Everything above runs in your browser — open Ray Marching & SDF and sphere-trace spheres, boxes and tori with soft shadows and smooth boolean blends. Nothing is installed, nothing is uploaded.
▶ Open Ray Marching & SDF simulation