Playground
Physarum (slime mold)

Transport networks without a plan

Physarum polycephalum The slime mold Physarum polycephalum has no brain, but it connects food sources with efficient networks. The model by Jones (2010) copies this with simple agents. Each agent senses the trail map at three points (left, front, right), turns toward the strongest one, moves one step and leaves a trail. The trail spreads and fades. Networks appear from this loop. Wikipedia

Trail map The trail map is a grid of cells with values from 0 to 1. Every step it is blurred with the 3×3 mean of its neighbors (diffusion) and multiplied by a decay factor. Strong trails attract more agents, which makes them stronger. Weak trails fade away. The map wraps around at the edges. Jones 2010

GPU Parallelization Every frame has three compute passes: the agents (one thread per agent), the diffusion of the map (one thread per cell) and a copy of the new map. Many agents deposit into the same cell at the same time, so the deposit is an atomic addition. Wikipedia

Original work

How it works

Per frame there are three passes with the same order on the GPU and the CPU: agents, diffusion, copy. The default is 65,536 agents on a 512 × 512 map with WebGPU, and 10,240 agents on a 256 × 256 map without it.

  • Agents: each agent is a vec4 [x, y, heading, 0] in map cells. It reads the trail at the left (heading + sensor angle), front and right (heading − sensor angle) sensor, which lie one sensor distance away. It turns by the turn angle toward the stronger side. If both sides beat the front, it turns a random way.
  • Deposit: after the move the agent adds the deposit value to its cell. On the GPU the trail is a fixed-point integer buffer (1.0 = 4096) and the deposit is an atomicAdd, so no deposit is lost when threads meet in one cell.
  • Diffusion: every cell becomes mix(cell, mean of 3×3, diffusion) × decay and is clamped to 0..1. While you press the canvas, cells near the pointer also get +0.5 per frame (food).
  • Copy: the new map is written to a second buffer and copied back, so no cell reads a value that was already updated in the same pass.
  • Without WebGPU: the same three steps run in TypeScript on Float32Arrays. The map is uploaded every frame as an 8-bit texture and shown by a WebGL shader.

Source code:physarum.shader.tsphysarum-gpu.strategy.tsphysarum-cpu-physics.ts

Note Simplifications in this demo:

  • One simulation step per frame: the speed depends on the frame rate of your screen.
  • There is no occupancy test: an agent can move onto a cell that another agent already uses. The model of Jones has this test.
  • The trail is limited to 1, so the deposit slider is a relative strength and not an absolute amount.
Agents
Sensor angle (°)
Sensor distance
Turn angle (°)
Step size
Deposit
Decay
Diffusion

Press and hold the mouse or a finger on the canvas to paint food. The agents follow it.

Frames/s: 0Backend: –Agents: 65,536Trail map: 0 × 0