Interactive Vision From Zero · Episode 27

Elemental Jutsu — One Swarm, Five Forms

Elemental Jutsu — One Swarm, Five Forms is Episode 27 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 27
Bonus · Episode 27 · Interactive Vision From Zero

By now you've built a particle swarm that becomes a face (Eps 25–26) and a hand tracker that drives effects (the whole season). This episode connects them. One pool of particles sits between your hands and morphs, on command, into five elements: a face, a blob of water, a glass cube, a column of fire, and a bolt of lightning. Nothing is created or destroyed when you switch — the same particles fly to a new set of targets. That's the entire trick behind "morphing" VFX, and it's why a single shared particle library can be every element.

This is the through-line of the hand-glass-cube project — hand-tracked elemental "jutsu" — rebuilt as one morphing swarm. The demo is alive on load with two synthetic hands; pick a form from jutsu (or let it auto-cycle) and watch the swarm reshape. Hit use my camera and your two real hands position and scale the element between your palms.

0 fps
the swarm forms between your two hands
🔒 runs on-device · nothing leaves your machine
synthetic hands · 7000 particles
Water — a wobbling blob held between your palms.

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

What you're looking at

A fixed pool of 7,000 particles. Every frame, each particle is told where it should be — a target position and a target color — and it eases a fraction of the way there. The only thing that changes between elements is the function that hands out those targets. Switch the form and the targets change; the particles, already chasing, simply flow to the new shape. Morphing isn't a special system — it's the absence of one.

The shapes live in a small local frame (roughly a unit cube around the origin), and the hands decide where that frame sits: the element is centered on the midpoint between your two palms and scaled by how far apart they are. Spread your hands and the element grows; this is the same placement the hand-glass-cube effects use.

hands (synthetic or camera) ──► midpoint C + span S (where the element lives) per particle i, per frame: (target, color) = FORM[current](i, t) // ← the only thing that differs per element worldTarget = C + target * S pos[i] += (worldTarget - pos[i]) * morph // the ease — and the morph, for free col[i] += (color - col[i]) * 0.1 FORM = { 0:face, 1:water, 2:cube, 3:fire, 4:lightning }

The five forms

Each form is just a target generator over a stable per-particle seed (so particle #2000 is always "the same" particle as it travels between shapes):

  • Face — targets are the synthetic face-mask points from Episode 25 (cool-crown → warm-jaw gradient). The swarm wears a face.
  • Water — each particle owns a fixed point inside a unit sphere; a low-frequency noise wobble jiggles the whole blob like surface tension. Cyan, the Mizu no Jutsu.
  • Glass Cube — points are scattered on the six faces of a cube and the whole frame slowly rotates; edges read brighter. A nod to the original's transmission cube, in particles.
  • Fire — each particle has a looping height phase: it rises from the base, its radius pinching inward as it climbs, recycling at the top. Color runs hot-white → yellow → red → ember by height. A living column, so the targets update every frame.
  • Lightning — a jagged bolt path from your left hand to your right, regenerated every ~70 ms for the flicker; particles snap onto it with a little perpendicular scatter. Electric blue-white.

How the code works

(a) One pool, a per-form target function

The render loop never knows which element it's drawing. It calls the active form's fill(i, t) for a local target + color, places it with the hands, and eases. Stable per-particle randoms (a sphere point, a cube point, a fire phase, a face slot) are computed once at startup so each particle keeps its identity across morphs:

// stable per-particle data, computed once
const sphere = []   // point inside a unit ball (water)
const cube   = []   // point on a cube's surface (glass)
const firePhase = new Float32Array(N);   // 0..1 start height (fire)
const faceSlot  = (i) => i % M;            // which face landmark (face)

function tick(t) {
  const { C, S } = handsFrame();          // midpoint + span from the two hands
  for (let i = 0; i < N; i++) {
    FORMS[current](i, t, tgt, col);       // writes a LOCAL target + color
    const tx = C.x + tgt.x * S, ty = C.y + tgt.y * S, tz = C.z + tgt.z * S;
    pos[i*3]   += (tx - pos[i*3])   * morph;   // ease toward the target…
    pos[i*3+1] += (ty - pos[i*3+1]) * morph;
    pos[i*3+2] += (tz - pos[i*3+2]) * morph;   // …which IS the morph when current changes
  }
}

(b) Two forms, side by side

Water is a fixed sphere point plus a shared wobble; fire is a rising, recycling phase. The shapes are utterly different, yet both are just "write a local xyz + rgb":

// WATER — a jiggling blob (cyan)
function water(i, t, p, c) {
  const s = sphere[i];
  const w = 0.10 * Math.sin(t*1.7 + s.x*4.0);  // surface-tension wobble
  p.set(s.x*(1+w), s.y*(1+w), s.z*(1+w));
  c.set(0.25, 0.7, 1.0);
}

// FIRE — a rising column, hot at the base (targets update every frame)
function fire(i, t, p, c) {
  const h = (firePhase[i] + t*0.45) % 1;        // 0 (base) → 1 (top), looping
  const r = (1 - h) * 0.55 * fireR[i];           // pinch inward as it climbs
  const a = fireA[i] + t*1.5;
  p.set(Math.cos(a)*r + curl(h,i), -1 + h*2.2, Math.sin(a)*r*0.6);
  c.setRGB(1.0, 1.0 - h*0.7, Math.max(0, 0.5 - h));  // white→yellow→red→ember
}

(c) Hands place the element; MediaPipe is lazy

The midpoint + span come from two hand positions — synthetic by default, your real palms once you press the button. The Hand Landmarker is imported only inside the click handler, wrapped in try/catch so a camera-less load is silent and synthetic. Same mirror-X selfie convention as every hand episode:

function handsFrame() {
  const L = toWorld(handL), R = toWorld(handR);       // mirror-X inside toWorld
  return { C: L.clone().add(R).multiplyScalar(0.5),   // midpoint
           S: THREE.MathUtils.clamp(L.distanceTo(R)*0.6, 0.6, 2.4), L, R };
}
// camera path: const { HandLandmarker } = await import(MP_ESM);  numHands: 2
// res.landmarks[0]/[1] → palm point 9 → handL / handR

Common pitfalls

  • Particles "teleport" instead of morphing. You rebuilt the pool on form change, or set positions directly. Keep ONE persistent position buffer and only swap the target function — the ease does the morph.
  • Identity scrambles between shapes. You generated the per-particle randoms inside the per-frame loop, so a particle gets a new sphere/cube point every frame and the morph jitters. Generate them once at startup.
  • Fire freezes / lightning doesn't flicker. Those forms are alive — their targets must be recomputed every frame (fire's height phase, the bolt's jagged path). Only static shapes (cube, sphere) can be cached.
  • The element sits in the wrong place. Mixed-up hand mapping. Place everything as C + local*S and mirror X in toWorld for the selfie view; verify with the synthetic hands before blaming the camera.
  • Lightning bolt is rigid. Regenerate its jagged offsets on a timer (~70 ms) with new random phases — a bolt that updates every frame looks like static; one that never updates looks like a wire.

Exercises

  1. Gesture to switch. Use the fist detector from Episode 05: a closed fist cycles to the next element (with the kanji announcement). Now you change jutsu with a gesture, not a dropdown.
  2. A sixth form. Add earth — particles snap to a rotating icosahedron's vertices, brown/green, slow. You only write one more target function.
  3. Per-element physics. Give water gravity sag, give fire upward buoyancy + curl-noise, give lightning a fast snap (high morph) and the others a slow one — drive morph per form so each element feels different, not just looks different.

What's next

This is the capstone of the Face Particles bonus arc and the bridge to the hand-glass-cube project, where each element is a fully separate effect (a real transmission cube, a MarchingCubes water gel, a multi-layer lightning bolt with sparks and a mini-game). The lesson to carry over: a shared particle pool + a swappable target function is a tiny budget that buys an entire spellbook.