CSG
Cut, combine and intersect solids with constructive solid geometry, using the manifold-3d library.
Constructive solid geometry builds shapes from booleans: a box with a sphere subtracted from it, two cylinders added together, the overlap of two shapes. wtc-gl doesn't do the booleans itself. manifold-3d, a fast and robust geometry library compiled to WebAssembly, does, and two functions move geometry across: toManifold turns a Geometry into a manifold, and fromManifold turns the result back.
import {
Renderer,
Camera,
Obj,
Mesh,
Lights,
LitProgram,
Box,
Sphere,
toManifold,
fromManifold,
Vec2,
Vec3
} from 'wtc-gl'
import Module from 'manifold-3d'
import wasmUrl from 'manifold-3d/manifold.wasm?url'
// Loaded once, for every copy of the example on the page
let loading = null
const loadManifold = () =>
(loading ??= Module({ locateFile: () => wasmUrl }).then((wasm) => {
wasm.setup()
return wasm
}))
// Normals, and a colour per shape, so the cut faces take the sphere's
const properties = { normal: 3, colour: 3 }
// A sphere orbits through a box and is subtracted from it every frame
export default function (container) {
const renderer = new Renderer({
dpr: Math.min(devicePixelRatio, 2),
alpha: true,
antialias: true
})
const { gl } = renderer
container.appendChild(gl.canvas)
const camera = new Camera({ fov: 35 })
camera.position.reset(2.6, 2, 3.6)
camera.lookAt(new Vec3(0, 0, 0))
const scene = new Obj()
const program = new LitProgram(gl, {
lights: new Lights(),
vertexColours: true,
specular: 0.3
})
// A placeholder geometry, hidden until manifold-3d has loaded
const mesh = new Mesh(gl, { geometry: new Box(gl), program })
mesh.visible = false
mesh.setParent(scene)
let box = null
let ball = null
let elapsed = 0
let disposed = false
const draw = () => {
if (!box) return
const a = elapsed * 0.8
const p = [Math.cos(a) * 0.7, Math.sin(a * 1.3) * 0.3, Math.sin(a) * 0.7]
const moved = ball.translate(p)
const cut = box.subtract(moved)
mesh.geometry.remove()
mesh.geometry = fromManifold(gl, cut, { properties })
mesh.visible = true
// Manifolds live in WASM memory, so free each one when done
moved.delete()
cut.delete()
renderer.render({ scene, camera })
}
loadManifold().then((wasm) => {
if (disposed) return
const make = (geometry, colour) => {
const manifold = toManifold(wasm, geometry, {
properties,
fill: { colour }
})
geometry.remove()
return manifold
}
box = make(
new Box(gl, { width: 1.4, height: 1.4, depth: 1.4 }),
[0.93, 0.91, 0.87]
)
ball = make(
new Sphere(gl, { radius: 0.6, widthSegments: 48 }),
[0.95, 0.42, 0.26]
)
draw()
})
return {
frame(time) {
elapsed = time
draw()
},
resize(width, height) {
renderer.dimensions = new Vec2(width, height)
camera.perspective({ aspect: width / height })
},
dispose() {
disposed = true
box?.delete()
ball?.delete()
mesh.geometry.remove()
program.remove()
renderer.dispose()
gl.canvas.remove()
}
}
}Installing and loading manifold-3d
wtc-gl doesn't depend on manifold-3d, so it costs nothing unless you use it. Install it alongside:
npm install manifold-3dpnpm add manifold-3dyarn add manifold-3dThe package is a JavaScript loader and a .wasm file. Load it once, call setup(), and pass the loaded module to toManifold. With Vite, import the .wasm file's URL so it's copied into the build, and tell the loader where it is:
import Module from 'manifold-3d'
import wasmUrl from 'manifold-3d/manifold.wasm?url'
const wasm = await Module({ locateFile: () => wasmUrl })
wasm.setup()Loading is asynchronous, so start it early and draw something else, or nothing, until it's ready, as the example does. Other bundlers have their own ways to give a .wasm file a URL; locateFile is how manifold-3d asks for it.
Booleans
toManifold(wasm, geometry) returns a manifold-3d Manifold, and from then on it's manifold-3d's own API. Every operation returns a new manifold:
import { Box, Sphere, Mesh, toManifold, fromManifold } from 'wtc-gl'
const box = toManifold(wasm, new Box(gl))
const ball = toManifold(
wasm,
new Sphere(gl, { radius: 0.65, widthSegments: 48 })
)
const carved = box.subtract(ball) // the box with the sphere taken out
const overlap = box.intersect(ball) // only where they overlap
const both = box.add(ball) // the two together
const mesh = new Mesh(gl, { geometry: fromManifold(gl, carved), program })To combine many at once, use the static forms on wasm.Manifold: Manifold.union([a, b, c]), Manifold.difference([a, b, c]) (the first minus the rest) and Manifold.intersection([a, b, c]).
fromManifold(gl, manifold) makes a new indexed Geometry from the result. Where a cut leaves a hard edge, its vertices are split, so the edge stays sharp.
Closed solids only
A manifold has to be a closed solid: a surface with no holes, where every edge is shared by exactly two triangles. Box, Sphere, Cylinder (with its caps), Torus, Capsule and Extrude all qualify, and so does a closed Lathe that starts and ends on the axis. A Plane, an open-ended Cylinder or a Tube with open ends doesn't.
Seams in uvs and normals don't matter, nor does an unindexed geometry: toManifold welds vertices at the same position first. For anything else, it throws:
Error: The geometry isn't a closed solid (…)The reason in brackets is manifold-3d's. Check the geometry has no gaps, and no triangles left over or doubled up.
Moving the pieces
A mesh's position, rotation and scale aren't part of its geometry, so they don't carry into a manifold. Move manifolds with their own transforms, which are cheap: they're stored and applied when the next boolean needs them.
const moved = ball.translate([0.5, 0, 0])
const turned = ball.rotate([0, 45, 0]) // degrees about x, y and z
const bigger = ball.scale(1.5)
const placed = ball.transform(matrix.array) // a Mat4, as 16 numberstransform takes a column-major 4×4 matrix, which is how a wtc-gl Mat4 stores its numbers. Normals follow the transforms: fromManifold rotates them to match and flips them on faces a subtraction turned inside out.
Carrying attributes through
Besides the position, a manifold carries the attributes named in properties, interpolated across any new faces. Each face keeps the attributes of the shape it came from. The default is { normal: 3, uv: 2 }: names, and the number of components each.
Give toManifold, for every geometry you combine, and fromManifold the same properties. If they don't match, fromManifold warns, and the attributes it reads back are wrong.
const properties = { normal: 3, colour: 3 }
const a = toManifold(wasm, geometryA, { properties })
const b = toManifold(wasm, geometryB, { properties })
const result = fromManifold(gl, a.subtract(b), { properties })Colouring the cut
fill gives values for attributes a geometry doesn't have. None of the primitives has a colour, so filling it gives each shape a colour of its own, and the faces a cutter leaves behind take the cutter's. Draw the result with vertexColours: true on LitProgram, which reads the colour attribute:
const properties = { normal: 3, colour: 3 }
const base = toManifold(wasm, new Box(gl), {
properties,
fill: { colour: [0.93, 0.91, 0.87] }
})
const cutter = toManifold(wasm, new Sphere(gl, { radius: 0.65 }), {
properties,
fill: { colour: [0.95, 0.42, 0.26] }
})
const geometry = fromManifold(gl, base.subtract(cutter), { properties })
const program = new LitProgram(gl, { lights, vertexColours: true })Without a fill, a missing attribute is 0s, or 1s for colour, so white.
Missing normals
When normal is listed but a geometry has none, manifold-3d works them out. Edges sharper than sharpAngle degrees (60 by default) stay hard; gentler ones are smoothed.
const m = toManifold(wasm, geometry, { sharpAngle: 30 })Freeing memory
A manifold lives in WebAssembly memory, which JavaScript's garbage collector can't see. Call delete() on every manifold when you're done with it, including the ones operations return along the way. fromManifold copies what it needs, so the manifold can be deleted straight after:
const moved = cutter.translate([x, 0, 0])
const result = base.subtract(moved)
mesh.geometry.remove()
mesh.geometry = fromManifold(gl, result, { properties })
moved.delete()
result.delete()Convert a geometry once and keep its manifold, rather than calling toManifold every frame; then the wtc-gl geometry can be removed. Booleans are fast enough for an animated cut at modest triangle counts, as in the example, which redoes the subtraction every frame.
See also
- The csg demo, with several bases, cutters and operations
- manifold-3d's documentation, for the rest of its API
- Primitives and Tube, Extrude and Lathe, for shapes to start from
- API: toManifold, fromManifold, ToManifoldOptions, ManifoldOptions