Exact Euclidean Distance Transform
Computes the exact squared Euclidean distance from every grid cell to the nearest feature in linear time by running a one-dimensional lower-envelope-of-parabolas pass along each axis.
Also known as EDT, Felzenszwalb distance transform
In the family Geometry, meshing & signed distance fields, distance transform
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 | None found | No built-in linear-time exact Euclidean distance transform utility. |
| Unity | None found | VFX Graph SDF Bake Tool uses Jump Flooding Approximation rather than a linear-time exact EDT. |
| Unreal 5.7 | None found | Mesh Distance Fields are generated for Lumen/DFAO rather than exposing a linear-time exact EDT operator. |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
The squared distance to a set of seeds is the minimum over seeds of a parabola per cell: f(q) = min_p ((q-p)^2 + g(p)). In one dimension, that minimum is the lower envelope of parabolas centred at each sample, which can be built in a single left-to-right sweep that keeps a stack of parabola indices and the boundaries where one overtakes the next. Evaluating the envelope at each grid position is a second sweep. Because squared Euclidean distance is separable, doing the 1D transform along rows, then columns (then depth for 3D) yields the exact result in O(n) total. The signed version runs it on both the shape and its complement and subtracts the square roots.
When to choose it
Reach for it when
- need accurate distance field from a binary image
- baking font or shape sdfs gives lumpy errors
- approximate distance fields are not precise enough
- generate a clean distance volume from voxels
Choose it when you need exact SDFs from binary images or voxel volumes for baking font SDFs, collision fields, Marching Cubes input or ambient occlusion, and when CPU or compute-shader time is acceptable. Prefer Jump Flooding Algorithm for per-frame, GPU-render-pass use where small errors are acceptable.
Field marks
- Distances are to cell centres, not to the sub-cell boundary, so binary inputs yield staircase fields unless anti-aliased input is used
- Values near thin features differ by up to half a cell from the true surface distance
- Memory-bound for large 3D volumes
- Squared values overflow low-precision storage unless scaled
Presets
Toy: the distance field this transform outputs (computed by brute force, not the linear-time algorithm)
{
"N": 14,
"BAND": 0.06
}Show GLSL (23 lines)
// Exact Euclidean distance to the nearest feature (brute-force reference of what the linear-time EDT outputs), shown as banded field.
const int N = 14;
const float BAND = 0.06;
vec2 site(int i) {
float f = float(i);
return vec2(fract(sin(f * 12.9898) * 43758.5), fract(sin(f * 78.233 + 1.0) * 24634.6)) * 0.84 + 0.08;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = fragCoord / iResolution.xy;
float d2 = 1e9; int id = 0;
for (int i = 0; i < N; i++) {
vec2 q = uv - site(i);
float d = dot(q, q);
if (d < d2) { d2 = d; id = i; }
}
float d = sqrt(d2);
vec3 col = 0.5 + 0.5 * cos(6.2831 * (d * 2.2) + vec3(0.0, 0.8, 1.6));
col *= 0.35 + 0.65 * smoothstep(0.0, 0.5, abs(fract(d / BAND) - 0.5) + 0.3);
col = mix(col, vec3(0.0), smoothstep(0.004, 0.0, abs(fract(d / BAND + 0.5) - 0.5) * BAND - 0.001) * 0.6);
col = mix(col, vec3(1.0), smoothstep(0.012, 0.008, d));
fragColor = vec4(pow(col, vec3(0.8)), 1.0);
}
Attribution: written for this index
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| exact-euclidean-distance-transform-specimen | Tesla T4 1920x1080 | 2.74× baseline (light) · 0.090 ms / 0.092 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| exact-euclidean-distance-transform-specimen | NVIDIA L4 1920x1080 | 1.92× baseline (feather) · 0.031 ms / 0.031 ms · 2.9× faster on L4 | opengl-timer-query · Cross-architecture validation |
| exact-euclidean-distance-transform-specimen | NVIDIA A100-SXM4-40GB 1920x1080 | 1.74× baseline (feather) · 0.050 ms / 0.050 ms · 1.8× faster on A100 | opengl-timer-query · Cross-architecture validation |
Crosses
- Marching Cubes: Exact SDF from a binary volume gives a smoother input for contouring.
Sources
- primaryPedro F. Felzenszwalb, Daniel P. Huttenlocher (2012). Distance Transforms of Sampled Functions. Theory of Computing 8(19), 2012.Verified via live page · 2026-11-29