{
 "id": "mesh-boolean-manifold",
 "title": "Manifold Mesh Booleans",
 "aliases": [
  "manifold-3d"
 ],
 "domain": "geometry",
 "subdomain": "mesh booleans",
 "stage": "geometry",
 "realtime": "interactive",
 "platforms": [
  "cpu",
  "webgl2",
  "webgpu",
  "python"
 ],
 "year_introduced": 2022,
 "inputs": [
  "mesh"
 ],
 "outputs": [
  "mesh"
 ],
 "style": "both",
 "dynamic": "static",
 "summary": "A mesh boolean library that guarantees manifold output and runs in parallel, using symbolic perturbation instead of tolerance fudging. Union, difference and intersection of triangle meshes in the browser or natively.",
 "how_it_works": "Inputs must be manifold triangle meshes. The library finds all edge-triangle intersections using a spatial structure (a BVH or Morton-sorted hash), then decides inside/outside using a winding-number-like shadow test with symbolic perturbation: vertices are treated as if infinitesimally nudged so that degenerate coincidences, such as coplanar faces or vertices on faces, resolve consistently rather than via epsilons. New vertices are placed at the exact intersection of an edge with a face. Kept triangles from both meshes are stitched along the new intersection edges into a manifold result. The code runs in parallel and ships as a WebAssembly build for JavaScript.",
 "when_to_use": "Choose it for CSG-style modelling on meshes (CAD in the browser, procedural parts, 3D-print prep) where robustness and manifold output matter and inputs are already manifold. Prefer `sdf-csg-smooth-min` when blends or smooth results are wanted, and a mesh clean-up pass or `screened-poisson-surface-reconstruction` when inputs are triangle soups with self-intersections.",
 "cost": {
  "class": "interactive",
  "notes": "Roughly linear to n log n; sub-second for hundreds of thousands of triangles on a modern CPU in native builds, slower in WASM."
 },
 "failure_modes": [
  "Requires manifold, closed inputs; open or self-intersecting meshes are rejected or give undefined results",
  "Exact coincident faces resolve via perturbation, which can leave razor-thin slivers",
  "Single-precision input can limit accuracy of tiny features",
  "Attributes such as UVs need extra bookkeeping to survive the cut"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "gh:elalish/manifold",
   "type": "code",
   "title": "elalish/manifold",
   "description": "Geometry library for topological robustness",
   "authors": [
    "elalish"
   ],
   "year": 2019,
   "venue": "GitHub",
   "url": "https://github.com/elalish/manifold",
   "ids": {
    "repo": "elalish/manifold"
   },
   "license": "Apache-2.0",
   "extra": {
    "stars": 2452,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-29T13:39:34Z",
    "language": "C++",
    "homepage": null,
    "license_name": "Apache License 2.0"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/elalish/manifold",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/elalish/manifold",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/elalish/manifold",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/elalish/manifold",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/elalish/manifold",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/elalish/manifold",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "elalish2019elalish"
  }
 ],
 "presets": [
  {
   "id": "mesh-boolean-manifold-live",
   "title": "Manifold Mesh Booleans — interactive CSG Union / Difference / Intersection with highlighted 1-manifold seam curves",
   "runtime": "glsl-fragment",
   "params": {
    "P_BOOL_OP": 1,
    "P_SEAM_GLOW": 1.15,
    "P_ORBIT_AMP": 0.34,
    "P_ROT_SPEED": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_BOOL_OP = 1.0; // P Bool Op (0:U 1:Diff 2:Int) [0.0, 2.0, 1.0]\nconst float P_SEAM_GLOW = 1.15; // P Seam Curve Glow [0.0, 2.2, 0.05]\nconst float P_ORBIT_AMP = 0.34; // P Cutter Offset [0.05, 0.62, 0.02]\nconst float P_ROT_SPEED = 1.0; // P Orbit Speed [0.0, 2.5, 0.05]\n\nmat2 rot2(float a) { float c = cos(a), s = sin(a); return mat2(c, -s, s, c); }\n\nfloat sdBox(vec3 p, vec3 b) {\n  vec3 q = abs(p) - b;\n  return length(max(q, 0.0)) + min(max(q.x, max(q.y, q.z)), 0.0);\n}\n\nfloat sdTorus(vec3 p, vec2 t) {\n  vec2 q = vec2(length(p.xz) - t.x, p.y);\n  return length(q) - t.y;\n}\n\n// Evaluate both manifold operand SDFs A (chamfered steel cube) & B (cutter sphere + crossing cylinder)\nvoid evalOperands(vec3 p, float spin, float orbitAmp, out float dA, out float dB) {\n  vec3 q = p;\n  q.xz = rot2(spin * 0.38) * q.xz;\n  q.yz = rot2(0.35) * q.yz;\n\n  dA = sdBox(q, vec3(0.56)) - 0.04;\n\n  // Dynamic manifold operand B orbiting through A\n  vec3 bOff = vec3(orbitAmp * cos(spin * 0.75), orbitAmp * 0.65 * sin(spin * 0.92), orbitAmp * sin(spin * 0.75));\n  float dSph = length(q - bOff) - 0.46;\n  vec3 tq = q;\n  tq.xy = rot2(spin * 0.5) * tq.xy;\n  float dTor = sdTorus(tq, vec2(0.52, 0.16));\n  dB = min(dSph, dTor);\n}\n\n// Exact CSG boolean combination returning vec3(distance, matID, seamProximity)\nvec3 mapCSG(vec3 p, float opMode, float spin, float orbitAmp) {\n  float dA, dB;\n  evalOperands(p, spin, orbitAmp, dA, dB);\n\n  float d;\n  float matId;\n  float seam;\n  if (opMode < 0.5) {\n    // Union: A U B\n    d = min(dA, dB);\n    matId = (dA < dB) ? 1.0 : 2.0;\n    seam = abs(dA - dB);\n  } else if (opMode < 1.5) {\n    // Difference: A  B\n    d = max(dA, -dB);\n    matId = (dA > -dB) ? 1.0 : 3.0; // interior carved face gets distinct warm-copper ID\n    seam = abs(dA + dB);\n  } else {\n    // Intersection: A n B\n    d = max(dA, dB);\n    matId = (dA > dB) ? 1.0 : 2.0;\n    seam = abs(dA - dB);\n  }\n  return vec3(d, matId, seam);\n}\n\nvec3 calcNormal(vec3 p, float opMode, float spin, float orbitAmp) {\n  vec2 e = vec2(0.002, 0.0);\n  return normalize(vec3(\n    mapCSG(p + e.xyy, opMode, spin, orbitAmp).x - mapCSG(p - e.xyy, opMode, spin, orbitAmp).x,\n    mapCSG(p + e.yxy, opMode, spin, orbitAmp).x - mapCSG(p - e.yxy, opMode, spin, orbitAmp).x,\n    mapCSG(p + e.yyx, opMode, spin, orbitAmp).x - mapCSG(p - e.yyx, opMode, spin, orbitAmp).x\n  ));\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 opMode = floor(clamp(P_BOOL_OP + 0.5, 0.0, 2.0));\n  float seamGlow = clamp(P_SEAM_GLOW, 0.0, 2.5);\n  float orbitAmp = clamp(P_ORBIT_AMP, 0.04, 0.65);\n  float spin = iTime * max(P_ROT_SPEED, 0.0);\n\n  vec3 ro = vec3(0.0, 0.14, 2.52);\n  vec3 rd = normalize(vec3(uv, -1.52));\n\n  vec3 col = mix(vec3(0.03, 0.04, 0.068), vec3(0.09, 0.115, 0.168), smoothstep(-0.55, 0.55, uv.y));\n  vec2 bgG = abs(fract(uv * 10.0 - 0.5) - 0.5);\n  col += vec3(0.018, 0.028, 0.045) * smoothstep(0.03, 0.0, min(bgG.x, bgG.y));\n\n  float t = 0.0;\n  vec3 res = vec3(0.0);\n  bool hit = false;\n  for (int i = 0; i < 72; i++) {\n    res = mapCSG(ro + rd * t, opMode, spin, orbitAmp);\n    if (res.x < 0.0018) { hit = true; break; }\n    t += max(res.x * 0.85, 0.002);\n    if (t > 9.0) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * t;\n    vec3 n = calcNormal(p, opMode, spin, orbitAmp);\n    float matId = res.y;\n    float seamD = res.z;\n\n    // Distinct crisp materials for operand A (brushed slate-steel), operand B (cobalt), and carved inner B (warm brass)\n    vec3 alb;\n    if (matId < 1.5) alb = vec3(0.68, 0.72, 0.78);\n    else if (matId < 2.5) alb = vec3(0.18, 0.56, 0.88);\n    else alb = vec3(0.92, 0.52, 0.22);\n\n    // Subtle CAD grid on operand surfaces\n    vec3 objP = p;\n    objP.xz = rot2(spin * 0.38) * objP.xz;\n    objP.yz = rot2(0.35) * objP.yz;\n    vec3 gf = abs(fract(objP * 7.5 - 0.5) - 0.5);\n    float grid = smoothstep(0.035, 0.008, min(gf.x, min(gf.y, gf.z)));\n    alb *= (1.0 - 0.12 * grid);\n\n    vec3 l1 = normalize(vec3(0.62, 0.86, 0.55));\n    vec3 l2 = normalize(vec3(-0.75, -0.25, 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), 46.0) * 0.58;\n    float rim = pow(1.0 - max(dot(n, -rd), 0.0), 3.0) * 0.30;\n\n    col = alb * (0.24 + 0.84 * diff1 + 0.25 * diff2) + vec3(spec) + vec3(0.45, 0.75, 1.0) * rim;\n\n    // Crisp glowing exact 1-manifold CSG intersection seam curve!\n    float seamLine = smoothstep(0.022, 0.003, seamD);\n    col = mix(col, vec3(0.28, 1.0, 0.86) * (1.25 + 0.5 * seamGlow), seamLine * min(seamGlow, 1.0));\n    col += vec3(0.18, 0.92, 0.84) * exp(-seamD * 45.0) * 0.35 * seamGlow;\n  }\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": "mesh-boolean-manifold",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:04:34.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.6994,
      "std": 0.1924,
      "min": 0.2063,
      "max": 0.9358
     },
     "image": "/specimens/mesh-boolean-manifold-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "manifold-dual-contouring",
  "sdf-csg-smooth-min",
  "three-mesh-bvh"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "manifold-dual-contouring",
   "why": "MDC output is guaranteed manifold, so it can feed booleans."
  },
  {
   "id": "three-mesh-bvh",
   "why": "three-mesh-bvh can pick and raycast the resulting meshes in the browser."
  }
 ],
 "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/mesh-boolean-manifold.json"
 },
 "solves": [
  "boolean cut leaves holes or non-manifold results",
  "need robust union and difference of meshes in the browser",
  "csg for cad or 3d-print prep",
  "mesh intersections fail from tolerance issues",
  "fast parallel mesh subtraction"
 ],
 "authority": "elalish, 2019",
 "url": "https://texelatlas.vercel.app/t/mesh-boolean-manifold/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 16.18,
  "tier": "moderate",
  "p50_ms": 0.5081,
  "p95_ms": 0.6058,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "mesh-boolean-manifold-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 14.42,
   "tier": "moderate",
   "p50_ms": 0.2279,
   "p95_ms": 0.2455,
   "speedup_x": 2.23,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 10.93,
   "tier": "moderate",
   "p50_ms": 0.3147,
   "p95_ms": 0.3496,
   "speedup_x": 1.61,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}