Generating watertight print meshes in code (three.js, earcut, manifold)
meshwatertightthreejsearcutstlmanifold3dtextgeometry
A slicer needs a closed, consistently wound (manifold) mesh. Generating one from code, especially with text, is full of small traps that pass casual checks. These were found building the parametric templates on 3d.jhampton.io (three.js in the browser) and the printable orrery (Python with manifold3d). All of them now have harness checks.
What to check automatically
- Closed: every edge is shared by exactly two triangles (after welding vertices).
- Consistently wound / outward: signed volume positive, each edge used once in each direction.
- Sitting on the bed: min z = 0, centered on the plate.
- Feature checks against intent, not just topology: e.g. compare the cut-in text footprint with the raised-text footprint, measure the Gridfinity foot on the mesh.
- Look at a close-up render. One bug passed watertightness and area-sum checks and was found only visually (below).
Traps
toFixedgives "-0.0000" on a lathe seam, so vertices that should weld do not. Add+ 0to normalize negative zero before keying.- Do not delete zero-area triangles from
ExtrudeGeometryoutput: collinear slivers are needed for a closed surface. Only drop triangles whose corners coincide. - earcut drops collinear points from hole rims and runs long cap edges past other glyphs' baseline corners, leaving T-junctions a slicer counts as open edges. Fix: strip collinear rim points first, then "stitch" (fan-split) the offending triangles.
- Strip collinear points one at a time. Judging them all at once deleted both copies of a path's start corner, so the letters A, 1 and L lost a limb, identically in the cut body and the inlay, so watertightness and area checks all passed. Only a close-up render showed it.
- A hole corner exactly level with an outline corner (box wall equal to corner radius) makes earcut emit a zero-area bridge triangle and an open seam; run the stitch pass whenever there are holes.
- Layout must include descenders. The tail of a "p" on the last line crossed the tag edge: harmless when raised, breaks the mesh when cut.
- Fonts (three.js Helvetiker) contain degenerate zero-area contours; skip any contour that cleans to fewer than 3 points. Helvetiker has Basic Latin and Greek only, no accented letters; tell the user when a character is missing.
- Bending flat faces onto a sphere (text wrapped on a ball) needs subdivision first, and the split must be per edge, never per triangle, so neighbors agree and the mesh stays closed.
BufferGeometryUtils.mergeGeometriesfails if any input is indexed (LatheGeometry is): call.toNonIndexed()first.- STL normals can be all zeros (many exporters write them that way), which renders solid black in a viewer. Delete and recompute normals on load.
- Unions with coincident faces (manifold3d, 32-bit STL output) can leave zero-area triangles. Build pieces to overlap rather than touch.
Avoiding booleans
Many printable shapes need no CSG: overlapping closed solids are fine for a slicer (it unions them), walls can be lofted between outlines with the same point count, and a cut-in label can be a separate "inlay" part that fills the cut flush in a second color. Cutting text into a slab is the one place real subtraction pays off.
Performance
In the browser, most templates rebuild in under 20 ms; a ball with inset wrapped text takes about 200 ms.
See also
- 3d.jhampton.io (print library and parametric template maker)
- Designing printable involute gear trains (the orrery case)
- three.js in a static site (bundling, viewers, snapshots)