← Iris

2D Cross-Section
Shape Sphere
Rays 12
Max steps 64
Total marches 0
Drag to move camera · Scroll to zoom
3D Rendered
Shape:
Number of rays 12
Max march steps 64
Camera distance 5.0
Light angle 45°

What is ray marching?

Ray marching (also called sphere tracing) is a rendering technique where each pixel's color is determined by casting a ray from the camera and stepping it forward through the scene. Unlike ray tracing, which computes exact ray-surface intersections, ray marching uses a signed distance function (SDF) to determine how far it can safely step without passing through any surface. At each point along the ray, the SDF returns the distance to the nearest surface. We advance the ray by this distance, then query the SDF again. When the SDF returns a value below a small epsilon, we've hit a surface.

Signed distance functions

An SDF maps every point in space to the signed distance to the nearest surface: positive outside, negative inside, zero on the surface. Simple shapes have elegant SDFs — a sphere of radius r centered at the origin is simply |p| − r. A box is max(|p.x|−b.x, |p.y|−b.y, |p.z|−b.z). The beauty of SDFs is that they compose: the union of two shapes is min(d1, d2), intersection is max(d1, d2), and subtraction is max(d1, −d2). Smooth blending uses log-sum-exp or polynomial smooth minimum functions.

Computing normals and lighting

The surface normal at a hit point is simply the gradient of the SDF, computed by finite differences: sample the SDF at small offsets along each axis and take the normalized result. This gives perfect smooth normals even for complex implicit surfaces. With the normal in hand, standard Phong or Blinn-Phong lighting models apply. Shadows are computed by marching a ray from the hit point toward the light — if it hits another surface before reaching the light, the point is in shadow. Soft shadows use the ratio of SDF values to march distances.

Why the circles?

In the 2D cross-section view, each marching step is shown as a circle whose radius equals the SDF value at that point. This is the key insight of sphere tracing: since the SDF gives the distance to the nearest surface, a sphere (circle in 2D) of that radius centered at the current point is guaranteed to be empty. We can safely jump to the edge of this sphere along our ray direction. Near surfaces, the circles shrink rapidly, giving sub-pixel precision. Far from surfaces, the circles are large, allowing fast traversal of empty space.