Three.js Isosurface from Volume (Surface Nets vs Marching Cubes)

Extract meshes from 3D noise volumes (asteroids, caves, overhanging terrain and gyroid lattices) in Three.js with a hand-written Surface Nets mesher shown next to Marching Cubes on the same field.

Key topics: three.js isosurface, surface nets, marching cubes, voxel mesh, 3d noise, procedural caves

What this code does

- Density Volume: an N³ grid of signed density (negative = solid) is built from fBm, shaped into an asteroid, caves, terrain or a gyroid.
- Surface Nets: one vertex per surface-crossing cell, placed at the mean of its edge crossings, gives shared, well-shaped vertices.
- Quads: every grid edge that changes sign emits a quad joining the four cells around it, with winding from the sign.
- Comparison: Marching Cubes on the same field makes more, thinner triangles with duplicated vertices, shown in the HUD.
- Evolving Field: offsetting the noise in time re-meshes the volume continuously.
- Controls: volume, algorithm, resolution, iso, noise frequency, octaves, flat shading, evolve and reseed.

Continue Learning

JavaScript Implementation

ES6+
// Surface Nets in two passes over a scalar grid `field` (N³, negative = solid)
// 1) every cell whose 8 corners change sign gets ONE vertex at the mean of its edge crossings
// 2) every grid edge whose endpoints change sign emits a quad joining the 4 cells around it
for (const cell of cells) {
  const crossings = cell.edges.filter(e => Math.sign(e.a) !== Math.sign(e.b)).map(e => lerpToZero(e))
  if (crossings.length) cell.vertex = average(crossings)
}
for (const edge of gridEdges) {
  if (Math.sign(edge.start) === Math.sign(edge.end)) continue
  const [c0, c1, c2, c3] = edge.surroundingCells
  triangles.push([c0, c1, c2], [c0, c2, c3]) // flip winding by the sign of edge.start
}
// Result: fewer, better-shaped triangles than marching cubes, with shared vertices