Three.js From Zero · Article s17-04
Metaballs — Geometry That Doesn't Exist Until You Look
Implicit surfaces with the MarchingCubes addon: a lit lava lamp of merging blobs, a wireframe x-ray toggle, and the O(n³) detail cliff measured live. Part of the MasterAllArts Creative 3D Roadmap.
Part of the MasterAllArts Creative 3D Roadmap .
Season 17 · Article 04 · Order & Chaos
Every mesh in this series so far was explicit: we wrote down vertices and the GPU drew them. Metaballs flip the deal. The shape is an equation — a field of influence around drifting points — and the surface is wherever that field crosses a threshold. Blobs merge like mercury, pinch apart like cell division, and no vertex exists until an algorithm from 1987 medical imaging goes hunting for them, sixty times a second.
Orbit the lamp. Watch two blobs approach: their surfaces bulge toward each other and fuse with a smooth waist — no collision logic, no blending code, just field addition. Now the x-ray: tick wireframe and the liquid illusion collapses into the honest truth — a soup of triangles being re-decided every frame. Drag detail down to 28 and watch the blobs go low-poly; drag it to 64 and count the cost in the fps counter. goo controls how far each ball's influence reaches: low is bouncing marbles, high is one ball of dough.
Implicit surfaces: shape as a yes/no question
A metaball field assigns every point in space a number — the summed influence of all the balls, falling off with distance:
f(p) = Σᵢ strengthᵢ / |p − ballᵢ|² // every ball contributes everywhere
surface = { p : f(p) = threshold } // the isosurface
That's the whole representation. Merging is free because addition is doing it: when two balls get close, their fields overlap, the sum crosses the threshold in the gap, and the "neck" appears — not as a special case but as arithmetic. This is the same idea as the reaction–diffusion display pass drawing a contour of a chemical field, promoted from 2D contour to 3D surface. Fluid renderers, CT scanners, and font rasterizers all live on this trick.
Marching cubes, in one paragraph
To draw an implicit surface you must find it. Marching cubes (Lorensen & Cline, 1987 — invented to render CT scans) samples the field on a grid and visits every little cube of 8 neighboring samples. Each corner is either inside (f > threshold) or outside — 2⁸ = 256 possible patterns, each with a known set of 0–4 triangles, looked up from a table, with vertex positions interpolated along the cube edges to land exactly where f crosses the threshold. Glue all the little triangle patches together and the surface simply… appears. No topology decisions, no seams: the lookup table already agrees with itself across cube faces.
Step 1 — The addon, an immediate-mode guest
Three.js ships marching cubes as an addon, and its API is unusual for Three — it works like a drawing context you re-issue every frame:
import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js';
const blobs = new MarchingCubes(48, material, false, false, 100000);
blobs.scale.set(14, 14, 14); // field box is local −1..1; balls are 0..1
scene.add(blobs);
// EVERY frame:
blobs.reset(); // wipe the field
for (let i = 0; i < count; i++) {
blobs.addBall(x[i], y[i], z[i], strength, 12); // pour influence in
}
blobs.update(); // march, build triangles
reset → addBall × N → update is immediate-mode thinking: the field is rebuilt
from scratch each frame, and so is the mesh. It feels wasteful and it's actually the honest
cost model — the surface genuinely is different geometry every frame. (The mesh object
itself persists; only its buffer contents churn, into a buffer pre-sized by that
100000 max-polygon argument.)
maxPolyCount, marching cubes
doesn't crash — it just stops mid-surface, leaving blobs with missing wedges. If
your metaballs develop mysterious holes at high goo + high detail, your polygon budget ran
out, not your math.
Step 2 — Choreography is just sines again
const x = 0.5 + 0.26 * Math.sin(t * 0.97 + i * 2.399); // 2.399 ≈ golden angle
const y = 0.5 + 0.24 * Math.sin(t * 1.31 + i * 1.7) * Math.cos(t * 0.58 + i);
const z = 0.5 + 0.26 * Math.cos(t * 1.13 + i * 3.1);
Incommensurate frequencies per axis (0.97, 1.31, 1.13) keep the orbits from ever repeating — the harmonograph lesson reused as a particle choreographer. The golden-angle phase offset keeps the blobs spread instead of clumping in sync.
Step 3 — Why it reads as liquid
Marching cubes computes normals from the field gradient — the direction f falls off fastest — not from triangle faces. Gradient normals are perfectly smooth even where the triangulation is chunky, which is why the surface looks like polished mercury at detail 36 but its wireframe looks like a rock. Pair that with a glossy material and two colored lights (our house blue from one side, ember from the other) and the eye writes "liquid chrome" all on its own. The skill being taught: where normals come from matters more than how many triangles you have.
What the controls teach
- goo — multiplies every ball's strength: the field threshold stays fixed, so reach grows and necks form earlier. Surface tension as a slider.
- detail — quality vs. cubic cost, measured live in the fps counter. Find your machine's cliff.
- wireframe — the single most honest toggle in 3D graphics: illusion off, algorithm visible. Note the triangle count readout churning every frame.
- blobs — strength auto-scales down as count rises (the classic normalization from the official example) so the lamp never saturates into one blob.
The skeleton
<script type="importmap">
{ "imports": {
"three": "https://unpkg.com/three@0.170.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.170.0/examples/jsm/" } }
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { MarchingCubes } from 'three/addons/objects/MarchingCubes.js';
// MarchingCubes(res, material, false, false, maxPolys) (Step 1)
// per frame: reset → addBall ×N → update (Steps 1–2)
// glossy material + two colored lights = liquid (Step 3)
</script>
Next: S17-05 — The Ising Model. A grid of magnetic spins, a temperature slider, and the most famous phase transition in physics — plus the checkerboard trick that lets randomness run on parallel hardware.