{
 "id": "sphere-tracing",
 "title": "Sphere Tracing",
 "aliases": [
  "ray marching",
  "raymarching"
 ],
 "domain": "geometry",
 "subdomain": "ray marching",
 "stage": "geometry",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cuda",
  "cpu"
 ],
 "year_introduced": 1996,
 "inputs": [
  "sdf"
 ],
 "outputs": [
  "depth",
  "normals"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "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.",
 "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": "Choose it for shader-based rendering of procedural or analytic SDF scenes (demoscene, Shadertoy-style, editors). Prefer `enhanced-sphere-tracing` when you hit grazing-angle slowness or need fewer iterations, `segment-tracing` for non-Euclidean or warped fields with local Lipschitz bounds, and rasterising a mesh from `marching-cubes` when the scene is static and complex.",
 "cost": {
  "class": "medium",
  "notes": "Dozens of field evaluations per pixel; cost scales with scene complexity and with grazing rays that take many small steps."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1007/s003710050084",
   "type": "paper",
   "title": "Sphere tracing: a geometric method for the antialiased ray tracing of implicit surfaces",
   "authors": [
    "John C. Hart"
   ],
   "year": 1996,
   "venue": "The Visual Computer",
   "url": "https://doi.org/10.1007/s003710050084",
   "ids": {
    "doi": "10.1007/s003710050084"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s003710050084",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s003710050084",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s003710050084",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s003710050084",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s003710050084",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "hart1996sphere"
  },
  {
   "role": "tutorial",
   "key": "url:https://iquilezles.org/articles/distfunctions",
   "type": "tutorial",
   "title": "Distance functions",
   "authors": [
    "Inigo Quilez"
   ],
   "year": null,
   "venue": "iquilezles.org",
   "url": "https://iquilezles.org/articles/distfunctions",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261125144804/https://iquilezles.org/articles/distfunctions/",
     "timestamp": "20261125144804"
    },
    "page_title": "Inigo Quilez :: computer graphics, maths, shaders, fractals, demoscene",
    "host": "iquilezles.org",
    "title_confirmed_by_page": false,
    "title_unconfirmed": true
   },
   "provenance": {
    "title": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://iquilezles.org/articles/distfunctions → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://iquilezles.org/articles/distfunctions → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 0,
   "__bib": "quilezdistance"
  },
  {
   "role": "implementation",
   "key": "gh:gokayfem/glsl-studies",
   "ids": {
    "repo": "gokayfem/glsl-studies"
   },
   "type": "code",
   "title": "gokayfem/glsl-studies",
   "authors": [
    "Gökay Aydoğan"
   ],
   "year": 2026,
   "venue": "GitHub",
   "url": "https://github.com/gokayfem/glsl-studies",
   "license": "MIT",
   "description": "454 procedural graphics studies in GLSL ES 3.00 for WebGL2, from woven cloth and animal fur to glacial terrain and atmospheric optics. Each study includes shader source and a rendered preview.",
   "extra": {
    "stars": 924,
    "stars_checked": "2026-11-30",
    "language": "GLSL",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-30T13:13:54Z",
    "homepage": null,
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gokayfem/glsl-studies",
     "checked_at": "2026-11-30T21:49:39.376Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gokayfem/glsl-studies",
     "checked_at": "2026-11-30T21:49:39.376Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gokayfem/glsl-studies",
     "checked_at": "2026-11-30T21:49:39.376Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gokayfem/glsl-studies",
     "checked_at": "2026-11-30T21:49:39.376Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gokayfem/glsl-studies",
     "checked_at": "2026-11-30T21:49:39.376Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gokayfem/glsl-studies",
     "checked_at": "2026-11-30T21:49:39.376Z"
    }
   },
   "status": "verified",
   "title_sim": null,
   "checked_at": "2026-11-30T21:49:39.376Z",
   "__bib": "aydoan2026gokayfem"
  }
 ],
 "presets": [
  {
   "id": "sphere-tracing-specimen",
   "title": "Sphere tracing an SDF scene: shaded half and step-count heat half",
   "runtime": "glsl-fragment",
   "params": {
    "MAX_STEPS": 64,
    "EPS": 0.001
   },
   "code": "// Sphere tracing of an SDF scene: left half shaded, right half shows the step count per pixel.\nconst int MAX_STEPS = 64;\nconst float EPS = 0.001;\nfloat map(vec3 p) {\n    float s = length(p - vec3(-0.9, 0.0, 0.0)) - 0.8;\n    vec2 q = vec2(length(p.xz - vec2(1.0, 0.2)) - 0.8, p.y);\n    float t = length(q) - 0.25;\n    float b = length(max(abs(p - vec3(0.0, -0.9, 0.0)) - vec3(4.0, 0.05, 4.0), 0.0)) - 0.02;\n    return min(min(s, t), b);\n}\nvec3 calcN(vec3 p) {\n    vec2 e = vec2(0.001, 0.0);\n    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)));\n}\nvec3 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); }\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n    vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n    vec3 ro = vec3(0.0, 1.0, -4.0), rd = normalize(vec3(uv.x, uv.y - 0.12, 1.4));\n    float t = 0.0; int n = 0; bool hit = false;\n    for (int i = 0; i < MAX_STEPS; i++) {\n        n = i;\n        float d = map(ro + rd * t);\n        if (d < EPS) { hit = true; break; }\n        t += d;\n        if (t > 20.0) break;\n    }\n    vec3 col = mix(vec3(0.12, 0.14, 0.2), vec3(0.03, 0.04, 0.07), uv.y + 0.5);\n    if (hit) {\n        vec3 p = ro + rd * t, nor = calcN(p);\n        vec3 l = normalize(vec3(-0.5, 0.8, -0.4));\n        float dif = max(dot(nor, l), 0.0);\n        float chk = mod(floor(p.x * 2.0) + floor(p.z * 2.0), 2.0);\n        vec3 alb = p.y < -0.85 ? vec3(0.3 + 0.3 * chk) : vec3(0.85, 0.6, 0.4);\n        col = alb * (0.15 + 0.9 * dif);\n    }\n    if (uv.x > 0.0) col = mix(col * 0.25, heat(float(n) / float(MAX_STEPS)), 0.9);\n    col = mix(col, vec3(1.0), smoothstep(0.003, 0.0, abs(uv.x)));\n    fragColor = vec4(pow(col, vec3(0.45)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "sphere-tracing",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5509,
      "std": 0.2009,
      "unique_colors": 134
     },
     "image": "/specimens/sphere-tracing-specimen.webp",
     "reason": null
    }
   }
  },
  {
   "id": "sphere-tracing-wgsl",
   "title": "SDF sphere tracing with soft shadows (WGSL)",
   "runtime": "webgpu-wgsl",
   "basis": "original",
   "license_note": "written for this index",
   "params": {
    "MAX_STEPS": 64,
    "SOFTNESS": 12
   },
   "code": "// WebGPU WGSL fragment specimen: Lipschitz sphere tracing + soft penumbra shadows\nconst MAX_STEPS: i32 = 64; // P\nconst SOFTNESS: f32 = 12.0000; // P\n\nfn sd_scene(p: vec3f, t: f32) -> f32 {\n  let s = length(p - vec3f(0.0, 0.18 + 0.08 * sin(t), 0.0)) - 0.56;\n  let q = vec2f(length(p.xz) - 0.72, p.y + 0.12);\n  let tor = length(q) - 0.16;\n  let flr = p.y + 0.65;\n  return min(min(s, tor), flr);\n}\n\nfn calc_normal(p: vec3f, t: f32) -> vec3f {\n  let e = vec2f(0.002, -0.002);\n  return normalize(\n    e.xyy * sd_scene(p + e.xyy, t) +\n    e.yyx * sd_scene(p + e.yyx, t) +\n    e.yxy * sd_scene(p + e.yxy, t) +\n    e.xxx * sd_scene(p + e.xxx, t)\n  );\n}\n\nfn soft_shadow(ro: vec3f, rd: vec3f, t: f32) -> f32 {\n  var res: f32 = 1.0;\n  var dist: f32 = 0.03;\n  for (var i: i32 = 0; i < 24; i = i + 1) {\n    let h = sd_scene(ro + rd * dist, t);\n    res = min(res, SOFTNESS * h / dist);\n    dist = dist + clamp(h, 0.02, 0.25);\n    if (res < 0.005 || dist > 6.0) { break; }\n  }\n  return clamp(res, 0.0, 1.0);\n}\n\n@fragment\nfn fs_main(@builtin(position) pos: vec4f) -> @location(0) vec4f {\n  let uv = (pos.xy - 0.5 * u.resolution) / u.resolution.y;\n  let ang = u.time * 0.25;\n  let ro = vec3f(sin(ang) * 2.4, 0.72, cos(ang) * 2.4);\n  let fwd = normalize(-ro);\n  let right = normalize(cross(fwd, vec3f(0.0, 1.0, 0.0)));\n  let up = cross(right, fwd);\n  let rd = normalize(fwd * 1.45 + right * uv.x - up * uv.y);\n\n  var dist: f32 = 0.0;\n  var hit: bool = false;\n  for (var i: i32 = 0; i < MAX_STEPS; i = i + 1) {\n    let d = sd_scene(ro + rd * dist, u.time);\n    if (d < 0.0015) { hit = true; break; }\n    dist = dist + d;\n    if (dist > 14.0) { break; }\n  }\n  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));\n  if (hit) {\n    let p = ro + rd * dist;\n    let n = calc_normal(p, u.time);\n    let l = normalize(vec3f(0.65, 0.85, 0.45));\n    let sh = soft_shadow(p + n * 0.01, l, u.time);\n    let nl = max(dot(n, l), 0.0);\n    let albedo = select(vec3f(0.76, 0.74, 0.70), vec3f(0.24, 0.52, 0.44), p.y > -0.63);\n    col = albedo * (0.22 + 0.95 * nl * sh);\n  }\n  return vec4f(pow(clamp(col, vec3f(0.0), vec3f(1.0)), vec3f(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "wgsl",
    "label": "WebGPU WGSL runtime",
    "ok": true,
    "proof": null
   }
  }
 ],
 "related": [
  "enhanced-sphere-tracing",
  "segment-tracing",
  "sdf-csg-smooth-min"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "sdf-csg-smooth-min",
   "why": "The CSG/smooth-min distance function is what sphere tracing steps through."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/geometry/sphere-tracing.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Hart, 1996",
 "url": "https://texelatlas.vercel.app/t/sphere-tracing/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 12.5,
  "tier": "moderate",
  "p50_ms": 0.4111,
  "p95_ms": 0.4269,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "sphere-tracing-specimen",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 10.03,
   "tier": "moderate",
   "p50_ms": 0.1594,
   "p95_ms": 0.1605,
   "speedup_x": 2.58,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 7.23,
   "tier": "moderate",
   "p50_ms": 0.2081,
   "p95_ms": 0.2088,
   "speedup_x": 1.98,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}