Three.js CSG Boolean Operations (Union, Subtract, Intersect)
Real-time constructive solid geometry in Three.js using three-bvh-csg, combining shapes with union, subtraction, intersection and XOR while the tool animates, with per-operand materials and timing.
Key topics: three.js csg, boolean operations, three-bvh-csg, constructive solid geometry, mesh subtraction, bvh
Game Over
What this code does
- Brushes: each operand is a `Brush` (a Mesh) whose world matrix positions it before evaluation.
- BVH Acceleration: three-mesh-bvh finds intersecting triangle pairs quickly, so the boolean re-runs every frame.
- Triangle Splitting: intersecting triangles are split along the cut and classified inside or outside the other solid.
- Operations: union keeps outside parts of both, subtract keeps A outside B plus B's inside faces flipped, intersect keeps the overlap and XOR keeps the non-overlapping parts.
- Material Groups: the result keeps one material group per input, so the cut faces show the tool's color.
- Controls: operation, base and tool shapes, tessellation, tool animation, ghost and wireframe.
Related Demos
Continue Learning
JavaScript Implementation
import { Brush, Evaluator, SUBTRACTION } from 'three-bvh-csg'
const box = new Brush(new THREE.BoxGeometry(2, 2, 2), new THREE.MeshStandardMaterial({ color: 0x60a5fa }))
const sphere = new Brush(new THREE.SphereGeometry(1.25, 48, 32), new THREE.MeshStandardMaterial({ color: 0xf97316 }))
sphere.position.set(0.6, 0.4, 0.5)
sphere.updateMatrixWorld()
const evaluator = new Evaluator()
const result = evaluator.evaluate(box, sphere, SUBTRACTION) // a Mesh with one material group per input
scene.add(result)