{
 "id": "sdf-csg-smooth-min",
 "title": "SDF Modeling: CSG and Smooth Minimum",
 "aliases": [
  "smooth union",
  "smin"
 ],
 "domain": "geometry",
 "subdomain": "signed distance fields",
 "stage": "geometry",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cpu"
 ],
 "year_introduced": 2013,
 "inputs": [
  "sdf"
 ],
 "outputs": [
  "sdf",
  "normals"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "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.",
 "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_use": "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.",
 "cost": {
  "class": "medium",
  "notes": "Cost per sample is the sum of all primitives evaluated; scene complexity multiplies with marching steps per pixel."
 },
 "failure_modes": [
  "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "url:https://iquilezles.org/articles/smin",
   "type": "web",
   "title": "Smooth minimum",
   "authors": [
    "Inigo Quilez"
   ],
   "year": null,
   "venue": "iquilezles.org",
   "url": "https://iquilezles.org/articles/smin",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20261018235735/https://iquilezles.org/articles/smin/",
     "timestamp": "20261018235735"
    },
    "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/smin → 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/smin → 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": "quilezsmooth"
  },
  {
   "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": "sdf-smooth-union-scene",
   "title": "Smooth-union SDF scene (orbiting blobs, rounded box, floor)",
   "runtime": "glsl-fragment",
   "params": {
    "blend_radius": 0.45,
    "max_steps": 96
   },
   "code": "// GLSL ES 3.0, Shadertoy-style. Smooth-union SDF scene, sphere traced and shaded.\nconst float K = 0.45;        // smooth-union blend radius (world units)\nconst int   STEPS = 96;      // max sphere-tracing iterations\nconst float MAX_DIST = 30.0;\nconst float EPS = 0.0015;\n\nfloat smin(float a, float b, float k) {\n    // quadratic polynomial smooth minimum\n    float h = max(k - abs(a - b), 0.0) / k;\n    return min(a, b) - h * h * k * 0.25;\n}\nfloat sdSphere(vec3 p, float r) { return length(p) - r; }\nfloat sdRoundBox(vec3 p, vec3 b, float r) {\n    vec3 q = abs(p) - b;\n    return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0) - r;\n}\nfloat map(vec3 p) {\n    float t = iTime;\n    float d = sdRoundBox(p - vec3(0.0, 0.0, 0.0), vec3(0.55, 0.35, 0.55), 0.12);\n    for (int i = 0; i < 3; i++) {\n        float a = t * 0.9 + float(i) * 2.0943951;\n        vec3 c = vec3(cos(a) * 1.15, 0.25 * sin(t * 1.3 + float(i)), sin(a) * 1.15);\n        d = smin(d, sdSphere(p - c, 0.45), K);\n    }\n    float floorD = p.y + 0.9;\n    return smin(d, floorD, K * 0.6);\n}\nvec3 calcNormal(vec3 p) {\n    vec2 e = vec2(1.0, -1.0) * 0.0015;\n    return normalize(e.xyy * map(p + e.xyy) + e.yyx * map(p + e.yyx) +\n                     e.yxy * map(p + e.yxy) + e.xxx * map(p + e.xxx));\n}\nfloat softShadow(vec3 ro, vec3 rd) {\n    float res = 1.0, t = 0.03;\n    for (int i = 0; i < 40; i++) {\n        float h = map(ro + rd * t);\n        res = min(res, 8.0 * h / t);\n        t += clamp(h, 0.02, 0.4);\n        if (res < 0.001 || t > 8.0) break;\n    }\n    return clamp(res, 0.0, 1.0);\n}\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n    vec2 uv = (2.0 * fragCoord - iResolution.xy) / iResolution.y;\n    vec3 ro = vec3(3.2, 1.8, 3.6);\n    vec3 ta = vec3(0.0, -0.1, 0.0);\n    vec3 w = normalize(ta - ro);\n    vec3 u = normalize(cross(w, vec3(0.0, 1.0, 0.0)));\n    vec3 v = cross(u, w);\n    vec3 rd = normalize(uv.x * u + uv.y * v + 1.8 * w);\n\n    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));\n    float t = 0.0;\n    bool hit = false;\n    for (int i = 0; i < STEPS; i++) {\n        float d = map(ro + rd * t);\n        if (d < EPS) { hit = true; break; }\n        t += d;\n        if (t > MAX_DIST) break;\n    }\n    if (hit) {\n        vec3 p = ro + rd * t;\n        vec3 n = calcNormal(p);\n        vec3 l = normalize(vec3(0.6, 0.8, 0.3));\n        float dif = clamp(dot(n, l), 0.0, 1.0) * softShadow(p + n * 0.01, l);\n        float amb = 0.5 + 0.5 * n.y;\n        float spe = pow(clamp(dot(reflect(-l, n), -rd), 0.0, 1.0), 32.0);\n        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))\n                                : vec3(0.9, 0.45, 0.25);\n        col = base * (dif * vec3(1.0, 0.95, 0.85) * 1.2 + amb * vec3(0.25, 0.3, 0.4)) + spe * 0.4 * dif;\n        col = mix(col, vec3(0.75, 0.85, 0.95), 1.0 - exp(-0.0025 * t * t));\n    }\n    fragColor = vec4(pow(clamp(col, 0.0, 1.0), vec3(0.4545)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "sdf-csg-smooth-min",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.7739,
      "std": 0.1006,
      "unique_colors": 170
     },
     "image": "/specimens/sdf-smooth-union-scene.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "sphere-tracing",
  "enhanced-sphere-tracing",
  "segment-tracing",
  "marching-cubes"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "sphere-tracing",
   "why": "The SDF supplies the step size for ray marching."
  },
  {
   "id": "jump-flooding-algorithm",
   "why": "Voronoi/distance fields from JFA can be combined with analytic SDFs in the same scene."
  }
 ],
 "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/sdf-csg-smooth-min.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Quilez",
 "url": "https://texelatlas.vercel.app/t/sdf-csg-smooth-min/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 59.62,
  "tier": "heavy",
  "p50_ms": 1.9614,
  "p95_ms": 1.9922,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "sdf-smooth-union-scene",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 45.87,
   "tier": "heavy",
   "p50_ms": 0.7293,
   "p95_ms": 0.74,
   "speedup_x": 2.69,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 35.42,
   "tier": "heavy",
   "p50_ms": 1.0202,
   "p95_ms": 1.0258,
   "speedup_x": 1.92,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}