---
id: "segment-tracing"
title: "Segment Tracing"
domain: "geometry"
stage: "geometry"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, cuda]
year_introduced: 2020
inputs: [sdf]
outputs: [depth, normals]
style: "both"
dynamic: "dynamic"
cost: "medium"
authority: "Galin et al., 2020"
url: "https://texelatlas.vercel.app/t/segment-tracing/"
sources_verified_at: "2026-11-29"
---
# Segment Tracing (Galin et al., 2020)

> 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.

## Reach for it when

- 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

## 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 it

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](https://texelatlas.vercel.app/t/sphere-tracing.md) or [Enhanced Sphere Tracing](https://texelatlas.vercel.app/t/enhanced-sphere-tracing.md) for exact SDF primitives and [Marching Cubes](https://texelatlas.vercel.app/t/marching-cubes.md) when you want a mesh rather than per-pixel evaluation.

## Cost

medium — Per-step overhead for the local bound, offset by fewer steps on warped fields; depends heavily on how bounds are propagated in the shader.
Measured GPU Weight: 24.77× baseline (heavy) — 0.7778 ms median / 0.7945 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 23.77× baseline (heavy) — 0.3756 ms median (2.07× faster on L4).
Ampere A100 scaling: 15.46× baseline (moderate) — 0.4452 ms median (1.75× faster on A100).

## 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

## Dataflow

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

## Presets

### Segment Tracing — global-Lipschitz sphere tracing steps (left) vs accelerated local-Lipschitz segment tracing (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_LIPSCHITZ_GAIN": 1,
  "P_HEATMAP": 0.78,
  "P_SPLIT": 0.48,
  "P_WAVE_AMP": 0.85
}
```

```glsl
const float P_LIPSCHITZ_GAIN = 1.0; // P Local Bound Gain [0.2, 1.5, 0.04]
const float P_HEATMAP = 0.78; // P Step Heatmap Mix [0.0, 1.0, 0.02]
const float P_SPLIT = 0.48; // P Sphere / Segment [0.0, 1.0, 0.02]
const float P_WAVE_AMP = 0.85; // P Field Gradient [0.2, 1.5, 0.04]

mat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }

// Non-unit-Lipschitz implicit surface f(p) = 0 (gyroid-modulated metaball cluster with steep local gradient!)
float evalField(vec3 p, float amp, float spin) {
  vec3 q = p;
  q.xz = rot2(spin * 0.32) * q.xz;
  q.yz = rot2(0.25) * q.yz;

  float dBase = length(q) - 0.82;
  // High-frequency gyroid displacement whose global Lipschitz constant is large (~1 + 4.5*amp),
  // but whose local directional gradient |grad(f).rd| is tiny in empty space and along tangent rays!
  float freq = 5.2;
  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);
  float env = exp(-max(dBase, 0.0) * 1.4);
  return dBase + 0.14 * amp * g * env;
}

// Analytical directional gradient magnitude |grad(f) . rd| along ray segment for Segment Tracing bound
float localDirLipschitz(vec3 p, vec3 rd, float amp, float spin) {
  float e = 0.025;
  float df = abs(evalField(p + rd * e, amp, spin) - evalField(p - rd * e, amp, spin)) / (2.0 * e);
  // Conservative safety floor over segment interval
  return max(df * 1.18, 0.72);
}

vec3 calcNormal(vec3 p, float amp, float spin) {
  vec2 e = vec2(0.0025, 0.0);
  return normalize(vec3(
    evalField(p + e.xyy, amp, spin) - evalField(p - e.xyy, amp, spin),
    evalField(p + e.yxy, amp, spin) - evalField(p - e.yxy, amp, spin),
    evalField(p + e.yyx, amp, spin) - evalField(p - e.yyx, amp, spin)
  ));
}

// Turbo-like step-count heatmap (deep blue = few fast steps, cyan/green = moderate, amber/red = heavy iteration cost)
vec3 stepHeatmap(float s) {
  s = clamp(s, 0.0, 1.0);
  vec3 c0 = vec3(0.06, 0.14, 0.38);
  vec3 c1 = vec3(0.10, 0.72, 0.84);
  vec3 c2 = vec3(0.96, 0.78, 0.16);
  vec3 c3 = vec3(0.98, 0.18, 0.24);
  if (s < 0.35) return mix(c0, c1, s / 0.35);
  if (s < 0.70) return mix(c1, c2, (s - 0.35) / 0.35);
  return mix(c2, c3, (s - 0.70) / 0.30);
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  float screenX = fragCoord.x / max(iResolution.x, 1.0);
  float split = clamp(P_SPLIT, 0.0, 1.0);
  float lipGain = clamp(P_LIPSCHITZ_GAIN, 0.15, 1.6);
  float heatMix = clamp(P_HEATMAP, 0.0, 1.0);
  float amp = clamp(P_WAVE_AMP, 0.15, 1.6);
  float spin = iTime;

  vec3 ro = vec3(0.0, 0.05, 2.48);
  vec3 rd = normalize(vec3(uv, -1.52));
  bool isSegment = screenX >= split;

  // Global worst-case Lipschitz constant required for conservative Sphere Tracing
  float globalLip = 1.0 + 2.45 * amp;

  float t = 0.0;
  bool hit = false;
  int steps = 0;
  const int MAX_STEPS = 72;

  for (int i = 0; i < MAX_STEPS; i++) {
    steps = i + 1;
    vec3 p = ro + rd * t;
    float f = evalField(p, amp, spin);
    if (f < 0.0025) { hit = true; break; }

    float stepSize;
    if (!isSegment) {
      // Standard Sphere Tracing: forced to divide by global worst-case Lipschitz bound everywhere!
      stepSize = f / globalLip;
    } else {
      // Segment Tracing: compute tight local directional Lipschitz bound over candidate segment!
      float locLip = mix(globalLip, localDirLipschitz(p, rd, amp, spin), clamp(lipGain, 0.0, 1.0));
      stepSize = f / max(locLip, 0.55);
    }
    t += max(stepSize, 0.004);
    if (t > 6.0) break;
  }

  float stepNorm = float(steps) / float(MAX_STEPS);
  vec3 heatCol = stepHeatmap(stepNorm);

  // Background shows the marching step-count cost in empty silhouette space (where Segment Tracing shines brightest!)
  vec3 bgCol = mix(vec3(0.03, 0.04, 0.068), vec3(0.08, 0.11, 0.16), smoothstep(-0.55, 0.55, uv.y));
  vec3 col = mix(bgCol, heatCol * 0.72, heatMix * 0.85);

  if (hit) {
    vec3 p = ro + rd * t;
    vec3 n = calcNormal(p, amp, spin);
    vec3 l1 = normalize(vec3(0.62, 0.86, 0.55));
    vec3 l2 = normalize(vec3(-0.75, -0.22, 0.52));
    float diff1 = max(dot(n, l1), 0.0);
    float diff2 = max(dot(n, l2), 0.0);
    vec3 hVec = normalize(l1 - rd);
    float spec = pow(max(dot(n, hVec), 0.0), 42.0) * 0.48;
    float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.28;

    vec3 surfBase = mix(vec3(0.76, 0.78, 0.82), heatCol, heatMix * 0.78);
    col = surfBase * (0.26 + 0.84 * diff1 + 0.24 * diff2) + vec3(spec) + vec3(0.45, 0.75, 1.0) * rim;
  }

  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
  col = mix(col, vec3(0.95, 0.97, 1.0), sep);

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).

## Lineage

- extends [Sphere Tracing](https://texelatlas.vercel.app/t/sphere-tracing.md)

## Combines with

- [SDF Modeling: CSG and Smooth Minimum](https://texelatlas.vercel.app/t/sdf-csg-smooth-min.md): Provides the operators whose local bounds it propagates.

## Sources

1. [primary] Eric Galin, Eric Guérin, Axel Paris, Adrien Peytavie (2020) Segment Tracing Using Local Lipschitz Bounds. Computer Graphics Forum. https://doi.org/10.1111/cgf.13951 (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)

## Cite this entry

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