Ray Marching: Signed Distance Fields, Sphere Tracing, and SDF Shading

EN NL ES PT-BR


Ray marching is a rendering method that finds surfaces by moving along a ray in repeated steps. It often appears in procedural rendering because a scene can be described by functions instead of a stored mesh or a set of closed-form ray-intersection equations. The most common version uses a signed distance field (SDF) to choose each step size. An SDF gives the distance from the current point to the nearest surface, with a sign that says whether the point is outside or inside the shape. If the distance is d, then the nearest surface is exactly d units away, so the ray can move forward by that amount without overshooting it.

The interactive sections below stay in 2D on purpose so that each geometric idea remains visible, and the same logic extends directly to 3D scenes. The next section introduces the algorithm and explains how sphere tracing uses the distance field to choose each step adaptively, then the visualization that follows shows a single ray marching toward a circle guided step by step by that field.

Ray Marching Algorithm

The term ray marching is broad. It refers to advancing along a ray in increments and checking the scene repeatedly. The simplest version uses a fixed step size: move forward a little, sample the scene, and repeat. That is easy to understand, but it can be very inefficient. Small steps are safer near thin details but waste work in empty space, while large steps are faster but can jump past surfaces.

In pseudocode, the core loop repeats four steps:

  1. set travel distance t = 0
  2. sample the scene at rayOrigin + rayDir * t
  3. if the sample is close enough to a surface, report a hit
  4. otherwise add the chosen step distance to t
  5. stop after a maximum distance or maximum number of steps

The version most common in graphics tutorials avoids the fixed-step tradeoff by using sphere tracing over a signed distance field , as described by John C. Hart in Sphere Tracing: A Geometric Method for the Antialiased Ray Tracing of Implicit Surfaces. An SDF returns the shortest distance from a point to the nearest surface, with a sign that tells whether the point is outside (positive), on the surface (zero), or inside the shape (negative).

If the field is accurate, a value of d(p)d(p) means there is no surface closer than that distance from the current sample point pp. That lets the ray jump forward by exactly d(p)d(p) without overshooting the nearest surface. Instead of choosing one fixed step size for the whole ray, the field itself decides how far the ray can safely move next. The efficiency comes from taking large safe steps through empty space while automatically shrinking the steps near surfaces: the next section demonstrates this step by step.

Marching Through a Distance Field

A simple circle makes the marching visible in detail. For a circle with center cc and radius rr, the signed distance field is:

d(p)=pcrd(p) = \lVert p - c \rVert - r

The length pc\lVert p - c \rVert is the distance from the sample point pp to the circle center. Subtracting the radius shifts that distance so the boundary has value zero, outside points have positive values, and inside points have negative values.

Distance-Guided Stepping

Drag the circle or the ray arrow to see how the marcher advances by safe distances.

SDF value 0.00 surface -0.16 +1.25

In the visualization, each purple ring is the distance returned at the current sample point. Because the ring touches the nearest surface but does not cross it, the ray can move to the edge of that ring safely. When the ray points directly toward the circle, the returned distance shrinks quickly and the steps converge to the boundary. When the ray points away, the distances may grow and the algorithm exits without a hit.

That is sphere tracing: the ray moves forward by the distance the field returns, re-evaluates, and repeats until it reaches a surface or gives up. The update rule is simply:

pn+1=pn+d(pn)r^p_{n+1} = p_n + d(p_n)\,\hat{r}

where r^\hat{r} is the ray direction. The step size is not arbitrary; it comes from the geometry of the field itself. That is what separates SDF ray marching from naive fixed-step marching.

In a shader, you typically stop when one of three conditions happens:

  1. the distance falls below a small hit threshold such as 0.001
  2. the accumulated travel distance exceeds a maximum range
  3. the loop reaches a maximum number of steps

All three limits matter. The hit threshold controls precision, the maximum range bounds work for rays that miss, and the step limit protects you from pathological cases or badly behaved distance estimators. The step limit is especially important for rays that graze surfaces at shallow angles , where the perpendicular distance changes slowly as the ray advances and sphere tracing can consume hundreds of steps for a single ray before converging.

Building an SDF Scene

A single circle is useful for intuition, but ray marching becomes powerful when a whole scene can be described as one distance function. You do that by combining primitive SDFs into one combined field, following the approach in Iñigo Quilez’s Distance Functions. In 2D, a circle is the distance to a center minus radius. A line or plane can represent a floor. To combine them, you usually take the minimum distance to all objects because the nearest surface controls the next safe step.

2D Ray Marching Scene

Cast a small fan of rays through circles and a ground line to see how the minimum SDF controls every step.

In the scene explorer, each ray repeatedly queries the minimum of the circle distances and the ground distance. That minimum matters for a simple reason: if one object is close and another is far, the close one defines the largest safe step. Using the wrong larger distance could skip across the nearer surface.

This “minimum of primitives” rule is the basis of constructive SDF modeling . A shader does not need triangle meshes to describe the scene. It only needs a function that answers: “how far am I from the closest surface right now?”

Why the Method Converges

A common beginner question is why sphere tracing does not skip over thin geometry. The answer is that it relies on a conservative distance estimate. If the SDF is exact, the returned value is a guaranteed lower bound on the true distance to the nearest surface, so the next step lands on or before the closest possible surface, never beyond it.

That guarantee weakens when the function is only a distance estimator rather than a perfect signed distance field. Fractal rendering is a classic example: the estimator may still work, but step safety depends on how conservative that estimate is. For more on field quality and when the exact-distance guarantee holds, see the field quality discussion in the signed distance fields article.

SDF Normals And Shading

Rasterized meshes usually store vertex normals or derive them from triangles. A ray-marched SDF surface has no explicit mesh, so the normal has to come from the field itself. The approach is to estimate the gradient of the SDF by sampling the field at tiny offsets: if the right sample is farther outside the object than the left sample, the surface slants to the right, and similarly for up and down. Normalizing that gradient vector gives a usable surface normal.

Gradient arrow Surface normal

The visualization overlays gradient arrows on the SDF heatmap. Every arrow points in the direction of f\nabla f at its sample location, radiating outward from the nearest surface regardless of shape. Hover anywhere over the field and a probe appears showing the signed distance, gradient direction, and gradient magnitude at that exact point. The readout labels the arrow as a surface normal whenever the cursor is near the zero contour, making the gradient-to-normal relationship explicit. Click to pin the probe in place, then switch between circle, box, rounded box, line segment, and a composite union of two circles to confirm that normals emerge from the field structure the same way across every shape.

The gradient is estimated numerically using finite differences: the stencil shows the ±ε\pm\varepsilon sample points along each axis, each labeled with the distance value sampled there. Drag the ε\varepsilon slider to see the tradeoff between noisy gradients at very small spacing and over-smoothed directions at large spacing. This is one of the most useful ideas in SDF rendering: the same field that guides intersection also provides a way to shade the resulting surface.

Typical Shader Loop

A fragment shader version usually looks conceptually like this:

  1. build a camera ray for the current pixel
  2. set t = 0
  3. evaluate sceneSdf(rayOrigin + rayDir * t)
  4. if the distance is below epsilon, register a hit
  5. otherwise increase t by that distance and continue
  6. if the loop ends without a hit, draw the background
  7. if a hit occurs, estimate a normal and compute lighting

The loop is simple, but the surrounding details determine image quality. Camera setup affects distortion and composition. The epsilon threshold affects banding and self-shadow artifacts. The maximum step count determines whether glancing rays terminate cleanly. Lighting quality depends on normal estimation, shadows, ambient terms, reflections, and whatever material model you build afterward.

Advantages and Tradeoffs

Ray marching is attractive because it makes procedural geometry unusually direct. A primitive is just a formula. Scene combination is often a few min and max operations. Smooth blends, repetition, twists, and noise-based deformations can often be expressed analytically without rebuilding mesh topology. That is why the technique became popular in shader art, demoscene production, and procedural rendering experiments.

The tradeoffs are just as real:

  • every pixel may require many scene evaluations
  • thin or highly detailed features can need many steps or careful thresholds
  • soft shadows, ambient occlusion, and reflections add even more marches
  • distance estimators that are not conservative can cause misses or artifacts
  • large scenes need careful acceleration or domain design to stay fast

So ray marching is not a universal replacement for rasterization or hardware triangle tracing. It is best understood as a powerful procedural rendering tool with a specific strength: when geometry is easier to define as a field than as a mesh, in contrast to the standard raster pipeline described in vertex vs fragment shaders in the graphics pipeline . For a detailed side-by-side comparison of performance, quality, scene representation, and use cases, see ray marching vs ray tracing .

Common Extensions

Once the primary ray hit works, many classic effects are built from the same machinery. Shadow rays march from the surface toward the light. Ambient occlusion samples how quickly nearby geometry appears along the normal direction. Reflections spawn another ray from the hit point. When reflected or refracted ray families cluster instead of spreading evenly, the same ray-routing intuition leads to caustics from light concentration . Fog and volumetric effects accumulate contributions as the ray travels. Each one reuses the same central pattern of repeated scene evaluation, and many become more visually useful once you modulate shapes or materials with value noise, Perlin noise, and fractal noise .

That reuse is one reason ray marching is so teachable. The renderer stays conceptually consistent even as you add more features. You are still asking the scene for distance information and using that information to decide how far to move next. The complexity grows, but the underlying mental model does not change.

Summary

Ray marching with signed distance fields works because the scene tells each ray how far it can move safely. That turns intersection into an iterative process guided by geometry rather than by fixed increments. If you keep four ideas in mind, the technique becomes much easier to reason about:

  1. an SDF returns distance to the nearest surface with a sign
  2. the minimum distance in the scene determines the safe next step
  3. repeated safe steps converge toward the surface when the field is conservative
  4. the field gradient gives a usable normal for lighting

Those four pieces are the core of most introductory SDF shader renderers. After that, the work is mainly engineering: choosing thresholds, optimizing scene evaluation, and layering lighting or secondary effects on top.