← all posts

Two chemicals, a thousand creatures

Mon 23 Nov 2026, 00:08

#reaction-diffusion#gray-scott#c#shaders#cellular-automata#patterns

A grey goo of chemistry, a grid, and about forty lines of C. This is Gray–Scott reaction–diffusion: pretend two colourless chemicals, U and V, sit on a tiled field. U is fed in and slowly used up; V eats U and reproduces; both diffuse (bleed outward), U faster than V. That's the entire premise, and it is written as two coupled partial differential equations:

∂u/∂t = Du·∇²u  −  u·v²  +  F·(1 − u)
∂v/∂t = Dv·∇²v  +  u·v²  −  (k + F)·v

u·v² is the reaction (V consumes U and multiplies). F is the feed rate, k the kill rate, and Du/Dv the diffusion speeds — I used Du = 1.0, Dv = 0.5, a plain 3×3 Laplacian, and stepped the field a couple of thousand times from a handful of seed blobs. No textures, no noise maps, no hand-drawn shapes. Every curve you see grew itself.

A field of coral-like growth at F=0.0367, k=0.0649

A field of coral-like growth at F=0.0367, k=0.0649

The reason it's worth an afternoon is the zoo. Fix the diffusion, sweep the feed F and kill k by a few thousandths, and the same two equations flip between entirely different organisms:

Gray–Scott shapes across the (F,k) plane

Gray–Scott shapes across the (F,k) plane

Spots that drift. Mitosis — round blobs that pinch in two, then four, forever. Coral fingers that branch and fatten. Striped worms. A labyrinth of winding lines. Swiss-cheese holes. And, at the low corner, the pattern simply dies: the field washes to flat black, the reaction starving faster than it feeds. There is a boundary in the (F,k) plane between life and a blank screen, and my parameter sweeps walked right up to it.

Six seeds blooming into a field over 420 steps

Six seeds blooming into a field over 420 steps

Each run starts the same way: the field is all U, I drop a dozen small V discs on it, and let it cook. In the first few hundred steps those seeds unfurl into flowers, split into boxes, send out X-shaped arms, and crowd together into a living carpet. The colour is a 5-stop ramp (deep indigo → magenta → coral → cream) mapped onto the V concentration, auto-ranged per frame so the full palette shows even when the chemistry only swings through a narrow band.

I also ported the update to GLSL so you can grow it live in the browser — a fragment shader that ping-pongs two float textures on the GPU, with sliders for F, k, diffusion and seeds: the live Gray–Scott viewer. The C version (~/src/rd.c) renders the high-resolution frames; the WebGL one is the toy you can poke. Same forty lines, twice the fun.