---
id: "sphere-tracing"
title: "Sphere Tracing"
aliases: [ray marching, raymarching]
domain: "geometry"
stage: "geometry"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, cuda, cpu]
year_introduced: 1996
inputs: [sdf]
outputs: [depth, normals]
style: "both"
dynamic: "dynamic"
cost: "medium"
authority: "Hart, 1996"
url: "https://texelatlas.vercel.app/t/sphere-tracing/"
sources_verified_at: "2026-11-29"
---
# Sphere Tracing (Hart, 1996)

> Renders an implicit surface by marching a ray forward by the distance-field value at each point, which is by definition a safe step that cannot cross the surface. Converges quickly in open space.

## Reach for it when

- want to render an implicit surface in a shader
- need to ray march distance field scenes
- demoscene-style procedural scenes without triangles
- intersect a signed distance function safely

## How it works

Given a ray origin and direction, evaluate the signed distance d at the current point. Because d is a lower bound on the distance to the nearest surface, a sphere of radius d around the point contains no surface, so advancing by d is safe. Repeat until d falls below an epsilon proportional to the distance travelled (hit) or the ray exceeds a maximum range or step count (miss). Normals are the field gradient at the hit; soft shadows and ambient occlusion fall out of the same field cheaply by tracking the closest approach along secondary rays. The method requires the function to be 1-Lipschitz; functions that overestimate distance (twist, scale, domain warp) cause overstepping, usually mitigated by a step scale factor.

## When to use it

Choose it for shader-based rendering of procedural or analytic SDF scenes (demoscene, Shadertoy-style, editors). Prefer [Enhanced Sphere Tracing](https://texelatlas.vercel.app/t/enhanced-sphere-tracing.md) when you hit grazing-angle slowness or need fewer iterations, [Segment Tracing](https://texelatlas.vercel.app/t/segment-tracing.md) for non-Euclidean or warped fields with local Lipschitz bounds, and rasterising a mesh from [Marching Cubes](https://texelatlas.vercel.app/t/marching-cubes.md) when the scene is static and complex.

## Cost

medium — Dozens of field evaluations per pixel; cost scales with scene complexity and with grazing rays that take many small steps.
Measured GPU Weight: 12.5× baseline (moderate) — 0.4111 ms median / 0.4269 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 10.03× baseline (moderate) — 0.1594 ms median (2.58× faster on L4).
Ampere A100 scaling: 7.23× baseline (moderate) — 0.2081 ms median (1.98× faster on A100).

## Failure modes

- Many tiny steps along rays grazing a surface produce slow pixels and banding in step counts
- Overstepping with non-Lipschitz fields shows as holes and thin sliver gaps near silhouettes
- Fixed epsilon creates visible self-intersection or staircase at far distance; scale epsilon by distance
- Thin objects can be missed if a step jumps across them due to bounding approximations
- Step limit exhaustion produces noisy bright or dark outlines

## Engine support

- godot: builtin — `Environment.sdfgi_enabled = true (sdfgi_direct_light.glsl sphere-traces 3D SDF cascades by advancing by sampled SDF distance)` (evidence: https://github.com/godotengine/godot/blob/4.7.2-stable/servers/rendering/renderer_rd/shaders/environment/sdfgi_direct_light.glsl, checked 2026-12-01)

## Dataflow

- Inputs: sdf
- Outputs: depth, normals
- Platforms: webgl2, webgpu, desktop-gpu, cuda, cpu

## Presets

### Sphere tracing an SDF scene: shaded half and step-count heat half (glsl-fragment; rendered 2026-11-29)

```json
{
  "MAX_STEPS": 64,
  "EPS": 0.001
}
```

```glsl
// Sphere tracing of an SDF scene: left half shaded, right half shows the step count per pixel.
const int MAX_STEPS = 64;
const float EPS = 0.001;
float map(vec3 p) {
    float s = length(p - vec3(-0.9, 0.0, 0.0)) - 0.8;
    vec2 q = vec2(length(p.xz - vec2(1.0, 0.2)) - 0.8, p.y);
    float t = length(q) - 0.25;
    float b = length(max(abs(p - vec3(0.0, -0.9, 0.0)) - vec3(4.0, 0.05, 4.0), 0.0)) - 0.02;
    return min(min(s, t), b);
}
vec3 calcN(vec3 p) {
    vec2 e = vec2(0.001, 0.0);
    return normalize(vec3(map(p + e.xyy) - map(p - e.xyy), map(p + e.yxy) - map(p - e.yxy), map(p + e.yyx) - map(p - e.yyx)));
}
vec3 heat(float x) { return clamp(vec3(1.5 * x, 1.5 - abs(3.0 * x - 1.5), 1.5 - 1.5 * x) * vec3(1.0, 1.0, 1.0), 0.0, 1.0); }
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
    vec3 ro = vec3(0.0, 1.0, -4.0), rd = normalize(vec3(uv.x, uv.y - 0.12, 1.4));
    float t = 0.0; int n = 0; bool hit = false;
    for (int i = 0; i < MAX_STEPS; i++) {
        n = i;
        float d = map(ro + rd * t);
        if (d < EPS) { hit = true; break; }
        t += d;
        if (t > 20.0) break;
    }
    vec3 col = mix(vec3(0.12, 0.14, 0.2), vec3(0.03, 0.04, 0.07), uv.y + 0.5);
    if (hit) {
        vec3 p = ro + rd * t, nor = calcN(p);
        vec3 l = normalize(vec3(-0.5, 0.8, -0.4));
        float dif = max(dot(nor, l), 0.0);
        float chk = mod(floor(p.x * 2.0) + floor(p.z * 2.0), 2.0);
        vec3 alb = p.y < -0.85 ? vec3(0.3 + 0.3 * chk) : vec3(0.85, 0.6, 0.4);
        col = alb * (0.15 + 0.9 * dif);
    }
    if (uv.x > 0.0) col = mix(col * 0.25, heat(float(n) / float(MAX_STEPS)), 0.9);
    col = mix(col, vec3(1.0), smoothstep(0.003, 0.0, abs(uv.x)));
    fragColor = vec4(pow(col, vec3(0.45)), 1.0);
}

```

Attribution: written for this index

### SDF sphere tracing with soft shadows (WGSL) (webgpu-wgsl; WebGPU WGSL runtime)

```json
{
  "MAX_STEPS": 64,
  "SOFTNESS": 12
}
```

```wgsl
// WebGPU WGSL fragment specimen: Lipschitz sphere tracing + soft penumbra shadows
const MAX_STEPS: i32 = 64; // P
const SOFTNESS: f32 = 12.0000; // P

fn sd_scene(p: vec3f, t: f32) -> f32 {
  let s = length(p - vec3f(0.0, 0.18 + 0.08 * sin(t), 0.0)) - 0.56;
  let q = vec2f(length(p.xz) - 0.72, p.y + 0.12);
  let tor = length(q) - 0.16;
  let flr = p.y + 0.65;
  return min(min(s, tor), flr);
}

fn calc_normal(p: vec3f, t: f32) -> vec3f {
  let e = vec2f(0.002, -0.002);
  return normalize(
    e.xyy * sd_scene(p + e.xyy, t) +
    e.yyx * sd_scene(p + e.yyx, t) +
    e.yxy * sd_scene(p + e.yxy, t) +
    e.xxx * sd_scene(p + e.xxx, t)
  );
}

fn soft_shadow(ro: vec3f, rd: vec3f, t: f32) -> f32 {
  var res: f32 = 1.0;
  var dist: f32 = 0.03;
  for (var i: i32 = 0; i < 24; i = i + 1) {
    let h = sd_scene(ro + rd * dist, t);
    res = min(res, SOFTNESS * h / dist);
    dist = dist + clamp(h, 0.02, 0.25);
    if (res < 0.005 || dist > 6.0) { break; }
  }
  return clamp(res, 0.0, 1.0);
}

@fragment
fn fs_main(@builtin(position) pos: vec4f) -> @location(0) vec4f {
  let uv = (pos.xy - 0.5 * u.resolution) / u.resolution.y;
  let ang = u.time * 0.25;
  let ro = vec3f(sin(ang) * 2.4, 0.72, cos(ang) * 2.4);
  let fwd = normalize(-ro);
  let right = normalize(cross(fwd, vec3f(0.0, 1.0, 0.0)));
  let up = cross(right, fwd);
  let rd = normalize(fwd * 1.45 + right * uv.x - up * uv.y);

  var dist: f32 = 0.0;
  var hit: bool = false;
  for (var i: i32 = 0; i < MAX_STEPS; i = i + 1) {
    let d = sd_scene(ro + rd * dist, u.time);
    if (d < 0.0015) { hit = true; break; }
    dist = dist + d;
    if (dist > 14.0) { break; }
  }
  var col = mix(vec3f(0.84, 0.86, 0.84), vec3f(0.68, 0.72, 0.76), clamp(-uv.y + 0.5, 0.0, 1.0));
  if (hit) {
    let p = ro + rd * dist;
    let n = calc_normal(p, u.time);
    let l = normalize(vec3f(0.65, 0.85, 0.45));
    let sh = soft_shadow(p + n * 0.01, l, u.time);
    let nl = max(dot(n, l), 0.0);
    let albedo = select(vec3f(0.76, 0.74, 0.70), vec3f(0.24, 0.52, 0.44), p.y > -0.63);
    col = albedo * (0.22 + 0.95 * nl * sh);
  }
  return vec4f(pow(clamp(col, vec3f(0.0), vec3f(1.0)), vec3f(1.0 / 2.2)), 1.0);
}
```

Attribution: written for this index

## Lineage

- extended by [Enhanced Sphere Tracing](https://texelatlas.vercel.app/t/enhanced-sphere-tracing.md)
- extended by [Segment Tracing](https://texelatlas.vercel.app/t/segment-tracing.md)

## Combines with

- [SDF Modeling: CSG and Smooth Minimum](https://texelatlas.vercel.app/t/sdf-csg-smooth-min.md): The CSG/smooth-min distance function is what sphere tracing steps through.

## Sources

1. [primary] John C. Hart (1996) Sphere tracing: a geometric method for the antialiased ray tracing of implicit surfaces. The Visual Computer. https://doi.org/10.1007/s003710050084 (verified via crossref on 2026-11-29)
2. [tutorial] Inigo Quilez Distance functions. iquilezles.org. https://iquilezles.org/articles/distfunctions (verified via http on 2026-11-29)
3. [implementation] Gökay Aydoğan (2026) gokayfem/glsl-studies. https://github.com/gokayfem/glsl-studies License: MIT. (verified via github on 2026-11-30)

## Cite this entry

Texel. "Sphere Tracing." https://texelatlas.vercel.app/t/sphere-tracing/ (accessed 2026-12-01).
