Sphere Tracing
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.
Also known as ray marching, raymarching
In the family Geometry, meshing & signed distance fields, ray marching
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | Built in | Environment.sdfgi_enabled = true (sdfgi_direct_light.glsl sphere-traces 3D SDF cascades by advancing by sampled SDF distance) docs |
| Unity | None found | |
| Unreal 5.7 | None found | |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
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 choose it
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
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.
Field marks
- 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
Presets
Sphere tracing an SDF scene: shaded half and step-count heat half
{
"MAX_STEPS": 64,
"EPS": 0.001
}Show GLSL (40 lines)
// 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)
{
"MAX_STEPS": 64,
"SOFTNESS": 12
}Show WGSL (64 lines)
// 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
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| sphere-tracing-specimen | Tesla T4 1920x1080 | 12.5× baseline (moderate) · 0.411 ms / 0.427 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| sphere-tracing-specimen | NVIDIA L4 1920x1080 | 10.0× baseline (moderate) · 0.159 ms / 0.161 ms · 2.6× faster on L4 | opengl-timer-query · Cross-architecture validation |
| sphere-tracing-specimen | NVIDIA A100-SXM4-40GB 1920x1080 | 7.23× baseline (moderate) · 0.208 ms / 0.209 ms · 2.0× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): cited by DeepSDF (neural signed distance functions), Ray-Traced Prism Displacement Mapping.
Crosses
- SDF Modeling: CSG and Smooth Minimum: The CSG/smooth-min distance function is what sphere tracing steps through.
Sources
- primaryJohn C. Hart (1996). Sphere tracing: a geometric method for the antialiased ray tracing of implicit surfaces. The Visual Computer.Verified via crossref · 2026-11-29doi:10.1007/s003710050084
- implementationGökay Aydoğan (2026). gokayfem/glsl-studies. 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.GitHub API · license MIT · 2026-11-30