Three.js Conway's Game of Life 3D (Cellular Automata)
Cellular automata in Three.js — Conway's Game of Life stacked through time as a 3D sculpture, plus true 3D Life rules (Bays 4555, 5766, Clouds, crystal growth) over a 26-cell neighbourhood.
Key topics: three.js game of life, cellular automata, conway, 3d life, emergent behavior, instancedmesh
Game Over
What this code does
- Conway's Rules: B3/S23 on a toroidal grid, so a live cell survives with 2–3 neighbours and a dead cell is born with exactly 3.
- Time as Height: each new generation becomes the top layer, so gliders trace diagonal tubes and oscillators make columns.
- 3D Rules: outer-totalistic survive/birth sets over 26 neighbours, such as Bays 4555 or the self-organizing Clouds rule.
- Rendering: up to 90,000 live cells are drawn with one `InstancedMesh`, colored by layer age or distance from center.
- Fixed Timestep: generations advance at a set rate, separate from the display frame rate.
- Controls: mode, 3D rule, generations per second, initial density, grid sizes, time layers, spin and randomize.
Related Demos
Continue Learning
JavaScript Implementation
// Conway's Life on a torus; each generation is stacked as a new layer of cubes
const n = 48
let grid = Uint8Array.from({ length: n * n }, () => (Math.random() < 0.3 ? 1 : 0))
const layers = []
const cubes = new THREE.InstancedMesh(new THREE.BoxGeometry(0.4, 0.4, 0.4), new THREE.MeshStandardMaterial(), 60000)
scene.add(cubes)
function step () {
const next = new Uint8Array(n * n)
for (let y = 0; y < n; y++) {
for (let x = 0; x < n; x++) {
let c = 0
for (let dy = -1; dy <= 1; dy++) {
for (let dx = -1; dx <= 1; dx++) {
if (dx || dy) c += grid[((x + dx + n) % n) + ((y + dy + n) % n) * n]
}
}
const alive = grid[x + y * n]
next[x + y * n] = (alive && (c === 2 || c === 3)) || (!alive && c === 3) ? 1 : 0
}
}
grid = next
layers.unshift(next)
if (layers.length > 30) layers.pop()
let k = 0
const m = new THREE.Matrix4()
layers.forEach((g, layer) => g.forEach((alive, i) => {
if (!alive) return
m.makeTranslation((i % n - n / 2) * 0.42, (30 - layer) * 0.42, (Math.floor(i / n) - n / 2) * 0.42)
cubes.setMatrixAt(k++, m)
}))
cubes.count = k
cubes.instanceMatrix.needsUpdate = true
}
setInterval(step, 120)