technique

Ray Marching

Render a scene defined by signed distance functions by stepping camera rays forward by the SDF's own returned distance until they hit a surface or run out of steps.

Also called: Sphere tracing · Ray marching · sphere tracing · raymarching

Run it

Advance by the distance the field returns the sphere-tracing loop · SDF gradient as normal · why the step length is safe
What it looks like when it goes wrong (4)
  • Rays that graze the surface converge slowly — each step is short because the field says something is near — so a small budget runs out before they arrive. They report a miss and punch background-coloured holes exactly where the geometry is most nearly edge-on. The silhouette survives and the interior does not, which is the signature of a budget problem rather than a scene problem.
  • A ray is declared arrived while it is still a fifth of a unit away, so the surface is shaded early and inflates. The object visibly swells and its edges go soft and blocky, because how early a ray stops depends on the angle it approached from. Everything still renders — this is the failure that looks like a modelling choice.
  • The image is correct and costs far more to produce. In the heatmap, hot pixels are ones that took many steps, and they crowd the silhouette where rays creep in asymptotically. Worth reaching for whenever a shader "got slow": the cost is almost never uniform across the frame, and the map tells you where it went rather than that it went.
  • The ray origin starts within the field, where the signed distance is negative. The first test is already below the threshold, so every ray reports an immediate hit and shades a normal measured from inside the object. The frame fills with flat lighting and no silhouette. Signed distance fields are defined inside the shape as well as outside, and a loop that only checks `d < epsilon` cannot tell the two apart.

Clean-room implementation — written from the described algorithm, not from source. Reuse policy

What it is

Ray marching renders a scene by firing one ray from the camera through every output pixel and stepping it forward until it either hits something or runs out of steps. What makes it a distinct technique, rather than just “ray tracing with small steps,” is what tells the ray how far to step each time: a signed distance field evaluated at the ray’s current position. The two are separate entities on purpose — the SDF is the geometry, ray marching is the traversal that walks it — but neither is much use without the other, which is why both “ray marching” and “sphere tracing” name the same loop in the sources behind this page.

How it works

The loop, in the order you’d code it:

  1. Position a camera and, for each output pixel, build a ray from the camera through that pixel.
  2. At the ray’s current point, evaluate the scene’s signed distance field.
  3. Advance the ray by exactly that returned distance, not a fixed small increment. Because an SDF’s value is the true distance to the nearest surface, stepping by the full amount can never carry the ray through a surface — “we know that we can step by that distance without going through the surface. Doing so both speeds up the process and improves accuracy,” per Varun Vachhar’s write-up. This is the “sphere tracing” half of the name.
  4. Repeat. If the step distance collapses to a collision, you’re done. If it doesn’t, keep stepping — the source describes continuing “up to a” maximum, after which the ray counts as a miss rather than a hit.
  5. At a hit, compute the surface normal for shading by taking the gradient of the SDF at that point (∇f) — a calculation separate from the marching itself.

Parameters & tuning

The evidence names exactly two knobs: how many steps a ray gets before it’s declared a miss, and how close a step has to land to a surface to count as a hit. Raising the step budget lets you resolve geometry that sits far from the camera or that a ray only grazes at a shallow angle, at the cost of one more SDF evaluation per pixel per extra step — that trade is the whole tuning problem this loop has. The bundle behind this page doesn’t give specific values for either knob, or describe what a mistuned one looks like on screen, so this page won’t invent numbers it can’t back up.

Where it’s been used

No specific artwork is in evidence here. The two sources behind this page both teach ray marching as a general shader technique rather than crediting a piece: Varun Vachhar walks through the SDF-plus-marching loop directly at varun.ca, and The Book of Shaders treats “ray marching,” “raymarching,” and “sphere tracing” as names for the same technique.

Variants & neighbours

Iridescent material shader comes from the same varun.ca write-up and is worth naming only because it answers a different question: it layers a shifting base colour, a shadow term, and concentric specular bands (optionally blended with Phong) to decide what colour a surface is. Ray marching decides whether there’s a surface there at all, and which way it faces. The two compose — the marching loop hands off a hit point and a normal, and a material shader like this one decides the colour from there — rather than being alternatives to each other.

Go deeper

Connected to

Built from: Signed distance field

Further reading: The Book of Shaders


Revision 1 · 2 sources · Something wrong? Tell us.