2026-12-02 · aogl.cn dev notes · 阿格(niego)
Rule Petri: A 3D Living-Bacteria Dish Built Solo with AI
From grid cellular automata to a swimming, shape-shifting colony sandbox
I built a petri dish that lives in a browser: petri.aogl.cn. It started as a classic two-dimensional cellular automaton — cells, rules, colours, drop a seed and watch the world evolve. Later I tore it down and rebuilt it as a 3D living-bacteria sandbox: a colony of flagellated bacteria swimming in a dish, feeding, splitting, clumping together and competing. This is a build log of that journey — why I changed it, how, and which pitfalls actually cost time.

Why move from 2D grids to 3D living bacteria
A 2D cellular automaton is elegant, but its "life" is abstract: a lit square represents a cell, and rules decide whether it lights up next round. After a while it feels like watching a statistics chart rather than life itself. I wanted something more visceral — bacteria that really swim, turn toward food, look slightly different from one another, get eaten, get pushed aside, split in two. So I kept the core of "custom rules + start from zero" and replaced the grid with a Three.js 3D scene: every bacterium has its own rod-shaped body, beating flagella, and a glow that shifts with nutrient levels.
How to play: from one-click demo to your own experiment
A first-time visitor sees an empty dish and doesn't know what to do. So I added a step-by-step tour whose first line is "Want to see it in action? Click One-click demo". Click it and the app seeds bacteria, lays out nutrients and starts playback — a 3D colony comes alive immediately. To build your own, open the Tools drawer and pick "Start from zero" — a genuinely empty dish where you supply all the food.

The controls are deliberately simple: with "Drop bacteria" active, left-click to seed and right-click to erase; choose "Add nutrient" then click the canvas to feed — the bacteria will find their way there. Drag to rotate the view, scroll to zoom in on the beating flagella. These interactions look plain, but each one hides a coordinate-transformation trap: after zooming and panning, the mouse point and the dish position must still line up. I funnelled the math through a single conversion entry point to avoid drift at different zoom levels.

Presets: six experiments ready to play
Alongside "from zero", several tuned presets play instantly and remain editable:
- Natural colony — one strain swims, feeds and splits; watch the population grow on its own;
- Colony race — green, orange and blue strains compete for the same nutrients; see who expands fastest;
- Nutrient feast / Lean challenge / Closed culture — different food conditions for boom, starvation and steady state.

Under the hood: deterministic rules + Web Worker
All evolution runs in a Web Worker; the main thread only draws. Each iteration is a three-stage pipeline — rules, movement, then reproduction and decay — with each stage fully computed before buffers swap. Nothing rewrites in place while being read; that is the classic timing bug when bacteria attract each other. The engine is deterministic: same seed, same rules, same number of steps, same result. Every random choice derives from the seed's PRNG, so "random seeding" simply rolls a new seed rather than producing unrepeatable chaos. Hard caps keep it honest: at most 8 strains and 32 rules, with precomputed neighbour counts to hold each step under the 8 ms target.

Sharing and offline: one link restores the whole experiment
Experiment configs are encoded into a versioned, base64url seed embedded in the link. Open someone's share link and you restore their dish mid-evolution. Offline, a PWA caches static assets so local features keep working — only server-backed actions like sharing show a friendly notice. The current experiment auto-saves in localStorage, and you can manually keep the last 5 snapshots: two separate mechanisms, one against refresh loss, one for comparison.
Free, and why I made it
Rule Petri is entirely free: custom rules, all presets, seed import/export and unlimited experiments are open, with no ads, no login wall and no paid tier. It is essentially my own technical exercise and portfolio piece — an attempt to build something that both explains the principle clearly and looks good enough to keep playing with for two more minutes. If you are building a similar small tool, I hope the notes on the rule engine, Worker split and coordinate math save you some time.
Frequently asked questions
What is Rule Petri?
A 3D living-bacteria sandbox that runs in your browser at petri.aogl.cn. Start from an empty dish, drop bacteria, add nutrients, and watch the colony swim, feed and split — or pick a preset for an instant demo. Free, no sign-in.
How do I play?
On your first visit, click "One-click demo": the app seeds bacteria, lays out nutrients and starts playback for you. To build your own experiment, open the Tools drawer and pick "Start from zero" — a truly empty dish. Left-click to drop bacteria, right-click to erase; choose "Add nutrient" then click anywhere to feed them. Drag to rotate and scroll to zoom in on the flagella.
Which presets are included?
Start from zero, Natural colony, Colony race, Nutrient feast, Lean challenge and Closed culture. Each preset is a complete experiment config (strains, starting counts, nutrient and movement parameters) — playable immediately and editable afterwards.
What is the tech stack?
Vanilla JavaScript + Vite, Three.js for 3D rendering, a Web Worker for all evolution compute with Transferables to avoid array copies between threads, localStorage for auto-save plus the last 5 snapshots, and a thin PHP + MySQL backend for share-link seeds.
Is it really free?
Yes — no ads, no login wall, no paid features. Custom rules, all presets, seed import/export and unlimited experiments are open. It is a personal developer's technical exercise and portfolio piece.