SDF Modeling: CSG and Smooth Minimum
Builds shapes as signed distance functions combined with min, max and negation for union, intersection and difference, plus smooth-min variants that blend primitives organically. The workhorse of shader-based modelling.
Also known as smooth union, smin
In the family Geometry, meshing & signed distance fields, signed distance fields
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 | CSGShape3D performs polygonal mesh booleans rather than SDF CSG with smooth-minimum blending. |
| Unity | None found | No built-in 3D SDF CSG or smooth-minimum modeling nodes. |
| Unreal 5.7 | None found | |
| Blender 5.2 | Built in | Math node > Smooth Minimum; Geometry Nodes > SDF Grid Boolean docs |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Each primitive (sphere, box, capsule, torus ...) is a function returning signed distance to its surface. Union is min(a,b), intersection is max(a,b), subtraction is max(a,-b); transforms apply to the sample point before evaluating (translation, rotation, repetition, bending). Replacing min with a smooth minimum, such as the polynomial form min(a,b) minus a bump of height k/4 when the two distances are within k, gives a rounded blend whose radius is k. Variants return a blend factor too, so materials and colours can be mixed. Normals are central or tetrahedral gradient differences. Rendering is by sphere tracing or by meshing the field with marching cubes or dual contouring.
When to choose it
Reach for it when
- combine shapes with union and subtraction in a shader
- want organic blobby blending between primitives
- animate procedural shapes by parameters
- hard edges where two shapes meet look harsh
- model objects entirely in code
Choose it for procedural modelling, organic blobs, motion graphics, editor tools and anything where the shape must be animated or edited by parameters. Prefer meshes with Catmull-Clark Subdivision for authored assets, Segment Tracing or Enhanced Sphere Tracing when the field is non-Euclidean (warped), and baked grid SDFs when the scene is too complex to evaluate per sample.
Field marks
- Smooth-min distances are not true distances: blended regions shorten or lengthen steps and may need a step scale below 1
- Min/max of two exact SDFs gives only a bound inside and near intersections, which slows or breaks tracing at shallow angles
- Different smooth-min formulations are not associative, so blend results depend on order
- Domain warping and twisting break Lipschitz-1 and cause missed hits or banding
- Evaluating a large tree per sample is slow unless bounding volumes cull primitives
Presets
Smooth-union SDF scene (orbiting blobs, rounded box, floor)
{
"blend_radius": 0.45,
"max_steps": 96
}Show GLSL (75 lines)
// GLSL ES 3.0, Shadertoy-style. Smooth-union SDF scene, sphere traced and shaded.
const float K = 0.45; // smooth-union blend radius (world units)
const int STEPS = 96; // max sphere-tracing iterations
const float MAX_DIST = 30.0;
const float EPS = 0.0015;
float smin(float a, float b, float k) {
// quadratic polynomial smooth minimum
float h = max(k - abs(a - b), 0.0) / k;
return min(a, b) - h * h * k * 0.25;
}
float sdSphere(vec3 p, float r) { return length(p) - r; }
float sdRoundBox(vec3 p, vec3 b, float r) {
vec3 q = abs(p) - b;
return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;
}
float map(vec3 p) {
float t = iTime;
float d = sdRoundBox(p - vec3(0.0, 0.0, 0.0), vec3(0.55, 0.35, 0.55), 0.12);
for (int i = 0; i < 3; i++) {
float a = t * 0.9 + float(i) * 2.0943951;
vec3 c = vec3(cos(a) * 1.15, 0.25 * sin(t * 1.3 + float(i)), sin(a) * 1.15);
d = smin(d, sdSphere(p - c, 0.45), K);
}
float floorD = p.y + 0.9;
return smin(d, floorD, K * 0.6);
}
vec3 calcNormal(vec3 p) {
vec2 e = vec2(1.0, -1.0) * 0.0015;
return normalize(e.xyy * map(p + e.xyy) + e.yyx * map(p + e.yyx) +
e.yxy * map(p + e.yxy) + e.xxx * map(p + e.xxx));
}
float softShadow(vec3 ro, vec3 rd) {
float res = 1.0, t = 0.03;
for (int i = 0; i < 40; i++) {
float h = map(ro + rd * t);
res = min(res, 8.0 * h / t);
t += clamp(h, 0.02, 0.4);
if (res < 0.001 || t > 8.0) break;
}
return clamp(res, 0.0, 1.0);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
vec3 ro = vec3(3.2, 1.8, 3.6);
vec3 ta = vec3(0.0, -0.1, 0.0);
vec3 w = normalize(ta - ro);
vec3 u = normalize(cross(w, vec3(0.0, 1.0, 0.0)));
vec3 v = cross(u, w);
vec3 rd = normalize(uv.x * u + uv.y * v + 1.8 * w);
vec3 col = mix(vec3(0.75, 0.85, 0.95), vec3(0.25, 0.4, 0.7), clamp(rd.y * 1.5 + 0.3, 0.0, 1.0));
float t = 0.0;
bool hit = false;
for (int i = 0; i < STEPS; i++) {
float d = map(ro + rd * t);
if (d < EPS) { hit = true; break; }
t += d;
if (t > MAX_DIST) break;
}
if (hit) {
vec3 p = ro + rd * t;
vec3 n = calcNormal(p);
vec3 l = normalize(vec3(0.6, 0.8, 0.3));
float dif = clamp(dot(n, l), 0.0, 1.0) * softShadow(p + n * 0.01, l);
float amb = 0.5 + 0.5 * n.y;
float spe = pow(clamp(dot(reflect(-l, n), -rd), 0.0, 1.0), 32.0);
vec3 base = p.y < -0.85 ? vec3(0.55) * (0.6 + 0.4 * mod(floor(p.x * 2.0) + floor(p.z * 2.0), 2.0))
: vec3(0.9, 0.45, 0.25);
col = base * (dif * vec3(1.0, 0.95, 0.85) * 1.2 + amb * vec3(0.25, 0.3, 0.4)) + spe * 0.4 * dif;
col = mix(col, vec3(0.75, 0.85, 0.95), 1.0 - exp(-0.0025 * t * t));
}
fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(0.4545)), 1.0);
}
Attribution: written for this index
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| sdf-smooth-union-scene | Tesla T4 1920x1080 | 59.6× baseline (heavy) · 1.96 ms / 1.99 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| sdf-smooth-union-scene | NVIDIA L4 1920x1080 | 45.9× baseline (heavy) · 0.729 ms / 0.740 ms · 2.7× faster on L4 | opengl-timer-query · Cross-architecture validation |
| sdf-smooth-union-scene | NVIDIA A100-SXM4-40GB 1920x1080 | 35.4× baseline (heavy) · 1.02 ms / 1.03 ms · 1.9× faster on A100 | opengl-timer-query · Cross-architecture validation |
Crosses
- Scanned objects as SDF primitives: Real objects captured as distance fields become primitives in a shader modeller: smooth-blend a scanned statue into procedural rock, carve it with booleans, bend or repeat it, and ray-march the result in real time.
- Sphere Tracing: The SDF supplies the step size for ray marching.
- Jump Flooding Algorithm: Voronoi/distance fields from JFA can be combined with analytic SDFs in the same scene.