Ray marching is a rendering technique that advances a ray through a scene in repeated discrete steps, sampling the scene at each position to find surfaces, accumulate volumetric effects, or test against a signed distance field. Unlike ray tracing, which computes analytic ray–surface intersections against explicit geometry such as triangles or spheres, ray marching works iteratively: for each ray, the algorithm steps along its direction, checks at each position whether the ray has hit something, and stops when it finds a surface or reaches a step limit.
The Core Loop
Every ray marcher repeats the same basic sequence:
- Set the travel distance
t = 0. - Sample the scene at the current point along the ray.
- If the sample is close enough to a surface, report a hit.
- Otherwise choose a step distance, advance
tby that amount, and repeat. - Stop after a maximum distance or a maximum number of steps.
That loop is surprisingly general. The same structure powers SDF-based surface rendering, volumetric fog accumulation, and procedural shader art. What changes between these applications is only how the step distance is chosen and what the sample tells the algorithm to do next. The full interactive ray marching tutorial explores each flavor with detailed visualizations.
Marching Strategies
There are two common ways to choose the step distance:
Fixed-step marching moves forward by the same small amount every iteration. It is simple to implement and works for any scene representation: you just sample at each position and check for a surface. The drawback is efficiency: small steps are safe near thin details but waste work in empty space, while large steps risk skipping past surfaces. Fixed-step marching is mostly used for volumetric rendering (fog, smoke, fire) where you need to accumulate density along the whole ray rather than find one surface.
Sphere tracing uses a signed distance field (SDF) to decide the step distance adaptively. At each sample point, the SDF returns the shortest distance to the nearest surface. Because no surface exists inside that radius, the ray can jump forward by exactly that distance without overshooting anything. This makes sphere tracing far more efficient than fixed-step marching in typical surface-rendering scenes: the ray takes large jumps through empty space and naturally shrinks its steps when it approaches geometry.
The visual below shows sphere tracing in action. The purple ring at each sample point is the SDF distance at that position: the ray jumps to the edge of that ring, then re-evaluates the field.
Distance-Guided Stepping
Drag the circle or the ray arrow to see how the marcher advances by safe distances.
The update rule for sphere tracing is remarkably compact:
where is the current position, is the SDF distance at that point, and is the ray direction. The next position is simply the current position moved forward by the distance to the nearest surface, in the direction the ray is traveling.
Common Uses
Ray marching appears most often in these areas:
- Procedural scenes and shader art: geometry defined by formulas instead of stored meshes, popularized by sites like Shadertoy.
- Fractal rendering: fractals such as the Mandelbulb lack analytic intersection formulas but have practical distance estimators, making them natural candidates for sphere tracing.
- Volumetric rendering: fog, smoke, clouds, and fire are rendered by accumulating density along the ray using fixed-step marching, with transparency and lighting computed at each sample.
- SDF-based modeling: scenes built by combining primitive signed distance fields (union, intersection, subtraction) with smooth blends and deformations.
For a complete walkthrough of how signed distance fields work, how sphere tracing guarantees safe steps, and how to shade SDF surfaces, see the full interactive ray marching tutorial.