Three.js Marching Cubes Playground (Metaballs & Implicit Surfaces)

Interactive marching cubes in Three.js polygonizing metaballs, gyroids, Schwarz P minimal surfaces, torus pairs and noise blobs, with live iso level, grid resolution and per-vertex colors.

Key topics: three.js marching cubes, metaballs, isosurface, gyroid, implicit surface, polygonization

What this code does

- Scalar Field: every grid corner stores f(x, y, z), and the surface is where f equals the iso level.
- Marching Cubes: each cell's 8 corner signs index a 256-case table of triangle configurations, with vertices interpolated along edges.
- Metaballs: `addBall` sums falloff blobs so nearby balls merge smoothly.
- Implicit Surfaces: gyroid and Schwarz P triply periodic minimal surfaces are clipped by a spherical envelope.
- Normals: computed from the field gradient, not from triangles, which gives smooth shading at low resolution.
- Controls: field, grid resolution, iso, ball count, animation speed, wireframe, flat shading and bounds.

Continue Learning

JavaScript Implementation

ES6+
import { MarchingCubes } from 'three/examples/jsm/objects/MarchingCubes.js'

const blobs = new MarchingCubes(48, new THREE.MeshStandardMaterial({ color: 0x22d3ee }), false, false, 100000)
blobs.isolation = 80
scene.add(blobs)

function animate (time) {
  requestAnimationFrame(animate)
  const t = time / 1000
  blobs.reset()
  for (let i = 0; i < 8; i++) {
    blobs.addBall(0.5 + Math.sin(t + i) * 0.27, 0.5 + Math.cos(t * 1.3 + i) * 0.27, 0.5 + Math.sin(t * 0.7 + i * 2) * 0.27, 0.5, 12)
  }
  blobs.update() // polygonize the field into triangles
  renderer.render(scene, camera)
}
animate(0)