{
 "id": "segment-tracing",
 "title": "Segment Tracing",
 "aliases": [],
 "domain": "geometry",
 "subdomain": "ray marching",
 "stage": "geometry",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cuda"
 ],
 "year_introduced": 2020,
 "inputs": [
  "sdf"
 ],
 "outputs": [
  "depth",
  "normals"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "Ray marching that uses local Lipschitz bounds over ray segments instead of a global bound, so it can safely trace fields that are not distance functions (warped, displaced, procedural) with long, efficient steps.",
 "how_it_works": "Sphere tracing needs the global Lipschitz constant of the field to be 1. For general functions F, a safe step along the ray is the field value divided by a Lipschitz bound valid on the segment that will be covered. Segment tracing computes that bound locally: it estimates the bound over a segment starting at the current point (from the function's structure, e.g. through interval-friendly primitives and operators that propagate local bounds), and uses it to jump while still not missing the root. Because the bound only needs to hold in the region the step could reach, it is far tighter than a global constant, and long steps are possible far from the surface. It also gives a clean way to handle blending, displacement and domain deformations with provable correctness.",
 "when_to_use": "Choose it when your function is built from operators that break exact distances (smooth blends with large radii, warps, noise displacement) and sphere tracing either overshoots or needs very small step factors. Prefer `sphere-tracing` or `enhanced-sphere-tracing` for exact SDF primitives and `marching-cubes` when you want a mesh rather than per-pixel evaluation.",
 "cost": {
  "class": "medium",
  "notes": "Per-step overhead for the local bound, offset by fewer steps on warped fields; depends heavily on how bounds are propagated in the shader."
 },
 "failure_modes": [
  "Requires the modelling language to expose local Lipschitz bounds for every operator",
  "Implementation is more complex than a one-line step",
  "Bound estimation can be conservative and eat the speed-up on rough noise displacements",
  "Needs per-primitive analysis to avoid very loose bounds"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1111/cgf.13951",
   "type": "paper",
   "title": "Segment Tracing Using Local Lipschitz Bounds",
   "authors": [
    "Eric Galin",
    "Eric Guérin",
    "Axel Paris",
    "Adrien Peytavie"
   ],
   "year": 2020,
   "venue": "Computer Graphics Forum",
   "url": "https://doi.org/10.1111/cgf.13951",
   "ids": {
    "doi": "10.1111/cgf.13951"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13951",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13951",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13951",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13951",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1111/cgf.13951",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "galin2020segment"
  },
  {
   "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"
  }
 ],
 "presets": [
  {
   "id": "segment-tracing-live",
   "title": "Segment Tracing — global-Lipschitz sphere tracing steps (left) vs accelerated local-Lipschitz segment tracing (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_LIPSCHITZ_GAIN": 1,
    "P_HEATMAP": 0.78,
    "P_SPLIT": 0.48,
    "P_WAVE_AMP": 0.85
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_LIPSCHITZ_GAIN = 1.0; // P Local Bound Gain [0.2, 1.5, 0.04]\nconst float P_HEATMAP = 0.78; // P Step Heatmap Mix [0.0, 1.0, 0.02]\nconst float P_SPLIT = 0.48; // P Sphere / Segment [0.0, 1.0, 0.02]\nconst float P_WAVE_AMP = 0.85; // P Field Gradient [0.2, 1.5, 0.04]\n\nmat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\n// Non-unit-Lipschitz implicit surface f(p) = 0 (gyroid-modulated metaball cluster with steep local gradient!)\nfloat evalField(vec3 p, float amp, float spin) {\n  vec3 q = p;\n  q.xz = rot2(spin * 0.32) * q.xz;\n  q.yz = rot2(0.25) * q.yz;\n\n  float dBase = length(q) - 0.82;\n  // High-frequency gyroid displacement whose global Lipschitz constant is large (~1 + 4.5*amp),\n  // but whose local directional gradient |grad(f).rd| is tiny in empty space and along tangent rays!\n  float freq = 5.2;\n  float g = sin(q.x * freq) * cos(q.y * freq) + sin(q.y * freq) * cos(q.z * freq) + sin(q.z * freq) * cos(q.x * freq);\n  float env = exp(-max(dBase, 0.0) * 1.4);\n  return dBase + 0.14 * amp * g * env;\n}\n\n// Analytical directional gradient magnitude |grad(f) . rd| along ray segment for Segment Tracing bound\nfloat localDirLipschitz(vec3 p, vec3 rd, float amp, float spin) {\n  float e = 0.025;\n  float df = abs(evalField(p + rd * e, amp, spin) - evalField(p - rd * e, amp, spin)) / (2.0 * e);\n  // Conservative safety floor over segment interval\n  return max(df * 1.18, 0.72);\n}\n\nvec3 calcNormal(vec3 p, float amp, float spin) {\n  vec2 e = vec2(0.0025, 0.0);\n  return normalize(vec3(\n    evalField(p + e.xyy, amp, spin) - evalField(p - e.xyy, amp, spin),\n    evalField(p + e.yxy, amp, spin) - evalField(p - e.yxy, amp, spin),\n    evalField(p + e.yyx, amp, spin) - evalField(p - e.yyx, amp, spin)\n  ));\n}\n\n// Turbo-like step-count heatmap (deep blue = few fast steps, cyan/green = moderate, amber/red = heavy iteration cost)\nvec3 stepHeatmap(float s) {\n  s = clamp(s, 0.0, 1.0);\n  vec3 c0 = vec3(0.06, 0.14, 0.38);\n  vec3 c1 = vec3(0.10, 0.72, 0.84);\n  vec3 c2 = vec3(0.96, 0.78, 0.16);\n  vec3 c3 = vec3(0.98, 0.18, 0.24);\n  if (s < 0.35) return mix(c0, c1, s / 0.35);\n  if (s < 0.70) return mix(c1, c2, (s - 0.35) / 0.35);\n  return mix(c2, c3, (s - 0.70) / 0.30);\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  float lipGain = clamp(P_LIPSCHITZ_GAIN, 0.15, 1.6);\n  float heatMix = clamp(P_HEATMAP, 0.0, 1.0);\n  float amp = clamp(P_WAVE_AMP, 0.15, 1.6);\n  float spin = iTime;\n\n  vec3 ro = vec3(0.0, 0.05, 2.48);\n  vec3 rd = normalize(vec3(uv, -1.52));\n  bool isSegment = screenX >= split;\n\n  // Global worst-case Lipschitz constant required for conservative Sphere Tracing\n  float globalLip = 1.0 + 2.45 * amp;\n\n  float t = 0.0;\n  bool hit = false;\n  int steps = 0;\n  const int MAX_STEPS = 72;\n\n  for (int i = 0; i < MAX_STEPS; i++) {\n    steps = i + 1;\n    vec3 p = ro + rd * t;\n    float f = evalField(p, amp, spin);\n    if (f < 0.0025) { hit = true; break; }\n\n    float stepSize;\n    if (!isSegment) {\n      // Standard Sphere Tracing: forced to divide by global worst-case Lipschitz bound everywhere!\n      stepSize = f / globalLip;\n    } else {\n      // Segment Tracing: compute tight local directional Lipschitz bound over candidate segment!\n      float locLip = mix(globalLip, localDirLipschitz(p, rd, amp, spin), clamp(lipGain, 0.0, 1.0));\n      stepSize = f / max(locLip, 0.55);\n    }\n    t += max(stepSize, 0.004);\n    if (t > 6.0) break;\n  }\n\n  float stepNorm = float(steps) / float(MAX_STEPS);\n  vec3 heatCol = stepHeatmap(stepNorm);\n\n  // Background shows the marching step-count cost in empty silhouette space (where Segment Tracing shines brightest!)\n  vec3 bgCol = mix(vec3(0.03, 0.04, 0.068), vec3(0.08, 0.11, 0.16), smoothstep(-0.55, 0.55, uv.y));\n  vec3 col = mix(bgCol, heatCol * 0.72, heatMix * 0.85);\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    vec3 n = calcNormal(p, amp, spin);\n    vec3 l1 = normalize(vec3(0.62, 0.86, 0.55));\n    vec3 l2 = normalize(vec3(-0.75, -0.22, 0.52));\n    float diff1 = max(dot(n, l1), 0.0);\n    float diff2 = max(dot(n, l2), 0.0);\n    vec3 hVec = normalize(l1 - rd);\n    float spec = pow(max(dot(n, hVec), 0.0), 42.0) * 0.48;\n    float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.28;\n\n    vec3 surfBase = mix(vec3(0.76, 0.78, 0.82), heatCol, heatMix * 0.78);\n    col = surfBase * (0.26 + 0.84 * diff1 + 0.24 * diff2) + vec3(spec) + vec3(0.45, 0.75, 1.0) * rim;\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "segment-tracing",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:04:37.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7029,
      "std": 0.101,
      "min": 0.474,
      "max": 0.9014
     },
     "image": "/specimens/segment-tracing-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "sphere-tracing",
  "enhanced-sphere-tracing",
  "sdf-csg-smooth-min"
 ],
 "extends": [
  "sphere-tracing"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "sdf-csg-smooth-min",
   "why": "Provides the operators whose local bounds it propagates."
  }
 ],
 "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/segment-tracing.json"
 },
 "solves": [
  "ray marching overshoots on warped or noisy fields",
  "distance function isn't exact so marching artefacts appear",
  "displaced sdf needs tiny steps and runs slowly",
  "smooth blends with large radii break the marcher",
  "trace non-distance fields safely with long steps"
 ],
 "authority": "Galin et al., 2020",
 "url": "https://texelatlas.vercel.app/t/segment-tracing/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 24.77,
  "tier": "heavy",
  "p50_ms": 0.7778,
  "p95_ms": 0.7945,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "segment-tracing-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 23.77,
   "tier": "heavy",
   "p50_ms": 0.3756,
   "p95_ms": 0.3798,
   "speedup_x": 2.07,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 15.46,
   "tier": "moderate",
   "p50_ms": 0.4452,
   "p95_ms": 0.4575,
   "speedup_x": 1.75,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}