Interactive Vision From Zero · Episode 25

Face Particles I — Your Face in Points

Face Particles I — Your Face in Points is Episode 25 of Interactive Vision From Zero, a MasterAllArts free interactive lesson for artists learning creative 3D on the web.

Part of the MasterAllArts Creative 3D Roadmap .

← Interactive Vision From Zero Bonus · Episode 25
Bonus · Episode 25 · Interactive Vision From Zero

There's a genre of viral 3D demo where a glittering cloud of particles snaps into the shape of a human face, breathes for a moment, then dissolves and reforms. It looks like it needs a render farm. It does not. Underneath, it's the same trick this whole season teaches: a face is a list of points, MediaPipe hands you those points sixty times a second, and you let a swarm of particles chase them. The "AI" is the tracker you already have. Everything else is one ease toward a target.

This is a from-scratch rebuild of a piece you may have seen on VIVERSE ("FaceParticles") — but pared down to the one idea that makes it work. The demo below is alive the instant it loads, with no webcam and nothing downloaded: a synthetic face, sampled into a few hundred target points and chased by several thousand particles. Step the stage dropdown to watch the cloud go from raw dots to a lit, three-dimensional face. Then hit use my camera and the swarm reassembles itself around your face — because nothing downstream of the landmarks can tell synthetic points from real ones.

0 fps
drag to orbit · scroll to zoom
🔒 runs on-device · nothing leaves your machine
synthetic face · 5000 particles
Lit sphere-sprites, depth-sized, feature-tinted — the face made of light.

🔒 The camera button runs MediaPipe's Face Landmarker entirely in your browser. No frame is ever uploaded; nothing leaves your machine. Until you press it, the demo uses a synthetic face and makes zero network requests.

What you're looking at

A swarm of a few thousand independent particles, each one quietly trying to reach a home assigned to it on a face. The face is described by a few hundred target points — on the camera path those are MediaPipe's 468 face landmarks; in synthetic mode they're sampled procedurally off a face-shaped surface. There are far more particles than targets, so several particles share each target with a little fixed offset, which fattens 468 sparse dots into a dense, skin-like cloud.

Every frame does three cheap things: (1) read the current face points, (2) nudge each particle a fraction of the way toward its target, and (3) draw each particle as a little lit sphere. That's the entire program. Step the stages to see it assembled one decision at a time.

The data model: a face is just target positions

Season 2 taught the face as a 468-point mesh and you reached in for a dozen named features. Here we go the other way: we don't care what any single point means, we just want a cloud of them blanketing the face, each one usable as a destination. So the only thing a particle needs to know is its home — a point in space — and how eagerly to move toward it.

face points ──► target[i % M] (M ≈ 468 landmarks, or synthetic samples) + jitter[i] (fixed per-particle offset, fattens the cloud) = home[i] (where particle i wants to be, this frame) each frame: pos[i] += (home[i] − pos[i]) * follow // ease toward home each frame: draw pos[i] as a depth-sized, lit round sprite

Two coordinate facts make the synthetic and camera paths interchangeable. A MediaPipe landmark is {x, y, z} with x, y normalized to [0,1] in image space (x→right, y→down) and z a relative depth (more negative = closer to the camera). We map both sources through one function into Three.js world space, and we mirror X there so the selfie view isn't backwards. Get that one mapping right and synthetic points and real landmarks become the same thing.

Stage 1 — The target points

Strip everything away and look at the input: a few hundred dots floating where a face is. On the camera path these are the literal landmarks MediaPipe returns; in synthetic mode they're samples on a procedural face surface (with the eyes and mouth pushed back so you can read it). This sparse constellation is all the "face" information the rest of the demo ever gets. No pixels, no mesh — just positions.

Stage 2 — One particle each

Now drop a THREE.Points particle on every target and hold it there. The cloud is recognizably a face but thin and static — there are only as many particles as targets. The point of this stage is to see the 1:1 mapping before we complicate it: one vertex, one home, drawn as a flat little square (the raw GL point).

Stage 3 — Chase the face

Give us more particles than targets, scatter them into a random ball, and let each one ease toward its home instead of teleporting: pos += (home − pos) * follow. The swarm rushes in and assembles the face. Drag the follow slider — low values feel like syrup and trail beautifully, high values snap rigidly. This single lerp is the soul of the effect; everything after it is polish.

Stage 4 — Depth & relief

So far the face is flat-ish. Turn on the z of every point — the nose comes forward, the eye sockets sink back — and scale each particle's screen size by 1 / depth so near points grow and far points shrink. Now the cloud has volume: orbit it with the mouse and it reads as a real head, not a printed mask. Depth is what separates "particles on a photo" from "particles in space."

Stage 5 — Lit & colored

The portfolio look. Each square sprite is carved into a sphere in the fragment shader (a fake normal from the sprite coordinate), then lit with a single light so the cloud gains shading and a soft specular pop. The points sitting on the eyes and mouth are recessed and darkened into hollows — which, against the lit dome, is exactly what makes a pile of dots suddenly read as a face. Same few thousand dots as Stage 1 — but now they look made of light.

How the code works

The architecture is the series' golden rule: one setFaceTargets(points) path feeds both the synthetic face and the real camera. The synthetic generator and the camera both produce an array of {x, y, z} in the same normalized convention, so the particle system never knows or cares which it got.

(a) One mapping into world space (with the selfie mirror)

Everything funnels through toWorld. It centers the normalized coordinates, flips Y so up is up, and mirrors X so moving right moves the on-screen face right. The same function is applied to synthetic samples and to live landmarks — that's why the swap is seamless:

// normalized image space {x,y in [0,1], z relative depth} → Three.js world
const FACE_W = 3.0, FACE_H = 3.2, FACE_D = 3.0;
function toWorld(p, out) {
  out.x = (0.5 - p.x) * FACE_W;   // mirror X (selfie view)
  out.y = (0.5 - p.y) * FACE_H;   // flip Y: image-down → world-up
  out.z = -p.z * FACE_D;            // MediaPipe z: −closer → +toward camera
  return out;
}

(b) Densify the targets, then chase them

There are more particles than face points, so particle i adopts target i % M plus a small fixed jitter (computed once, so particles don't swim). Each frame we rebuild the M world-space homes from the current face, then lerp every particle toward its home and push the result into the geometry's position attribute:

// homes[]: M world-space targets, rebuilt each frame from the live face points
for (let m = 0; m < M; m++) toWorld(facePts[m], homes[m]);

const pos = geo.attributes.position.array;   // Float32Array, N*3
for (let i = 0; i < N; i++) {
  const h = homes[i % M], j = jitter[i];      // shared home + fixed offset
  const tx = h.x + j.x, ty = h.y + j.y, tz = h.z + j.z;
  pos[i*3]   += (tx - pos[i*3])   * follow;   // the ease — the whole effect
  pos[i*3+1] += (ty - pos[i*3+1]) * follow;
  pos[i*3+2] += (tz - pos[i*3+2]) * follow;
}
geo.attributes.position.needsUpdate = true;

(c) Draw each particle as a lit sphere

A GL point is a flat square. In the fragment shader we turn it into a sphere: build a normal from the sprite's local coordinate, discard outside the circle, and do one diffuse + specular term. The vertex shader sizes points by depth so the face has perspective:

// VERTEX — size by 1/depth so near grows, far shrinks
uniform float uSize; uniform float uDpr;
void main() {
  vec4 mv = modelViewMatrix * vec4(position, 1.0);
  gl_PointSize = uSize * uDpr * 90.0 / -mv.z;
  gl_Position = projectionMatrix * mv;
}

// FRAGMENT — carve the square into a lit sphere
void main() {
  vec2 uv = gl_PointCoord * 2.0 - 1.0; uv.y = -uv.y;
  float r2 = dot(uv, uv);
  if (r2 > 1.0) discard;                         // round sprite
  vec3 n = vec3(uv, sqrt(1.0 - r2));               // fake sphere normal
  float diff = clamp(dot(n, normalize(vec3(0.4,0.7,0.8))) * 0.5 + 0.5, 0.0, 1.0);
  gl_FragColor = vec4(vColor * (0.25 + 0.85 * diff * diff), 1.0);
}

(d) The Face Landmarker, loaded lazily

MediaPipe is never imported at the top of the file — only inside the camera button, wrapped in try/catch so any failure (no webcam, denied permission, file://) falls back to the synthetic face. When a face is found we feed result.faceLandmarks[0] — the 468-array — straight into setFaceTargets, the very same call the synthetic loop makes.

Common pitfalls

  • The cloud is mirrored / backwards. Same selfie gotcha as every face episode: map X as (0.5 − x). Apply the flip in toWorld so it hits both synthetic and camera points identically.
  • Particles "swim" and never settle. Your per-particle jitter is being regenerated every frame. Compute the jitter once at startup and reuse it; only the homes (the face) should change frame to frame.
  • Nothing forms / the face is a tiny dot. Mismatched scale: landmarks live in [0,1] but your world is meters. Center on 0.5 and multiply by a face-sized constant (FACE_W here). Drop to Stage 1 to see where the points actually are.
  • It's flat. You ignored z, or you forgot to size points by depth. Turn on the relief and divide gl_PointSize by -mv.z.
  • Frame rate tanks at high particle counts. You're rebuilding the homes for every particle instead of once per target. Compute M homes, then index i % M — don't call toWorld N times.
  • Camera works in dev but not from a file. getUserMedia needs localhost or HTTPS; opening the file with file:// silently blocks it. The synthetic face still runs — that's the point of the fallback.

Exercises

  1. Mouth-open burst. Read the lip gap (landmarks 13↔ 14) like Episode 13 did, and when it opens past a threshold, briefly drop follow toward zero so the particles fly apart, then snap back. The face "explodes" when you open your mouth.
  2. Color by depth. Pass each particle's z into the shader as a varying and lerp the color from cool (far) to warm (near). Instant rim-light feel with no extra lights.
  3. Trails. Stop clearing the frame fully — render into a faintly faded buffer — and lower follow. The particles leave comet tails as the face moves. (Episode 26 does this properly on the GPU.)

What's next

Episode 26 — Face Particles II: The GPGPU Studio. The CPU loop here tops out around ten thousand particles. Next we move the entire simulation onto the GPU with a ping-pong GPUComputationRenderer — position and velocity textures — and unlock the real piece: a Fluid momentum mode and a Classic curl-noise mode alongside Face, particles that respawn, a pointer/hand force that pushes and sucks the swarm, and the full lit, color-graded studio look. Same face, a hundred thousand particles.