Three.js From Zero · Article s16-10
Physarum — 65,536 Agents and the Ghost of a Slime Mold
The season finale: a full-GPU slime mold — agents living in textures, vertex-texture-fetch deposits, and a diffusing trail map growing transit networks nobody drew. Part of the MasterAllArts Creative 3D Roadmap.
Part of the MasterAllArts Creative 3D Roadmap .
Season 16 · Article 10 · Beautiful Math · Season Finale
A slime mold has no brain, no eyes, and no plan — and when researchers placed food on a map of Tokyo, it grew a network matching the city's actual rail system. The algorithm it runs is five verbs: sense, turn, move, deposit, evaporate. This finale runs those five verbs for 65,536 agents entirely on the GPU — agents whose positions live inside a texture and never once visit the CPU. Everything this season taught, fused into one organism.
Give it thirty seconds: a formless cloud condenses into veins, veins compete, highways win. Then play taxonomist — sensor angle wide (1.0+) grows nervous, webby meshes; narrow grows long-haul highways. Short memory (0.90) keeps only fresh trails and the network constantly rewires; long memory (0.99) fossilizes the first good solution. Every combination is a different species. respawn deals a new universe.
Five verbs, no brain
Jeff Jones formalized the slime-mold heuristic in 2010. Each agent, each step:
- Sense the trail map at three points ahead — left, center, right.
- Turn toward the strongest reading (with a pinch of randomness).
- Move forward one small step.
- Deposit a drop of trail at the new position.
And the world itself runs verb five: the trail map diffuses and evaporates every frame. That's the entire organism. Agents never see each other — they only read and write the shared map. Biologists call this stigmergy: coordination through traces left in the environment. It's the same mechanism as ant pheromones, and structurally it's boids with the flock's knowledge moved out of the agents and into a reaction–diffusion-style field. The two halves of this season, holding hands.
Step 1 — Agents live in a texture now
Boids kept agent state in JavaScript arrays — fine for 2,500, fatal for 65,536 with a texture read per sensor. The capstone move of GPU programming: the agent array becomes a texture. A 256×256 float texture holds one agent per texel — x in R, y in G, heading angle in B:
const agentTarget = () => new THREE.WebGLRenderTarget(256, 256, {
type: THREE.FloatType, // positions need full float32 precision
format: THREE.RGBAFormat,
depthBuffer: false,
});
let agentRead = agentTarget(), agentWrite = agentTarget();
FloatType. Fields that get overwritten every frame can stay half.
Step 2 — One pass moves everybody
Updating agents is a fragment shader over the agent texture — each fragment IS one agent, exactly like each fragment was one chemistry cell in reaction–diffusion:
vec4 a = texture2D(uAgents, vUv); // me: x, y, angle
float f = sense(a.xy, a.z); // ahead
float l = sense(a.xy, a.z + uSensorAngle); // left whisker
float r = sense(a.xy, a.z - uSensorAngle); // right whisker
float rnd = hash(vUv + fract(uTime)); // GPUs have no random — hash one
if (f >= l && f >= r) { /* hold course */ }
else if (l > r) a.z += uTurn * (0.5 + rnd);
else a.z -= uTurn * (0.5 + rnd);
a.xy = fract(a.xy + dir(a.z) * uSpeed); // step forward, wrap the torus
gl_FragColor = vec4(a.xy, a.z, 1.0);
The hash line matters more than it looks: GLSL has no random(),
so every GPU sim fakes it by hashing something unique-per-agent (its UV) with something
changing-per-frame (time). Without that jitter, ties break identically forever and the
whole population marches in lockstep.
Step 3 — Deposit: points whose positions live in a texture
Now the magic handshake. To stamp 65,536 trail drops, draw a THREE.Points with
65,536 vertices — but the geometry's "position" attribute doesn't hold positions. It holds
each agent's texel address, and the vertex shader looks the real position up:
// vertex shader of the deposit pass:
uniform sampler2D uAgents;
void main() {
vec4 a = texture2D(uAgents, position.xy); // vertex-texture-fetch
gl_Position = vec4(a.xy * 2.0 - 1.0, 0.0, 1.0);
gl_PointSize = 1.0;
}
Render those points into the trail target with additive blending and no clear. The full circle: a fragment pass simulates, a vertex pass reads the simulation, and the agent data completes every frame of its life — sense, turn, move, deposit — without one byte crossing back to JavaScript.
Step 4 — The world forgets, gently
// trail pass: 3×3 mean blur, then fade
vec4 sum = /* nine taps */ / 9.0;
gl_FragColor = sum * uEvaporate; // 0.90 = goldfish, 0.99 = elephant
Diffusion widens trails so followers can find them; evaporation deletes unreinforced ones. Memory with leakage is what makes the network adaptive — turn evaporation off and the first random scribble becomes permanent law.
Step 5 — A frame's choreography
renderer.autoClear = false;
// 1. agents: update pass (read agents+trail → write agents), swap
// 2. trail: diffuse+fade (read trail → write trail), swap
// 3. deposit: draw Points into the trail target, additive, no clear
// 4. display: trail → colormap → screen
Four passes, two ping-pong pairs, one quad and one Points mesh. Order matters: depositing after the blur means fresh trail is sharp for exactly one frame before it starts to spread — that's what keeps vein cores bright.
What the season taught, in one organism
- Simulation state vs. render state (Lorenz) → here state lives in textures.
- Thousands of things, one draw call (phyllotaxis, boids) → a Points mesh addressed by texel.
- Color is data (everywhere) → the display pass is a colormap over trail density.
- Ping-pong fields (reaction–diffusion, waves) → two pairs of them, interleaved.
- Local rules, global form (boids, Gray–Scott) → a transit network nobody drew.
That's Season 16: ten systems from my old math atlas, each rebuilt as a live demo, each leaving a reusable technique in your pocket. The through-line, if there is one: don't paint the picture — build the system, and let the picture happen.
Season complete. All ten Beautiful Math articles live in the Season 16 index. If you came here from the middle, start with the Lorenz attractor — the techniques stack in order.