Three.js GPGPU Compute (Cellular Automata on the GPU)
Compute-shader-style GPGPU in Three.js, running Conway's Life, Brian's Brain and a 16-state cyclic automaton on grids up to 2949×2048 with ping-pong render targets, many generations per frame and painting with the pointer.
Key topics: three.js gpgpu, compute shader, cellular automata, game of life gpu, render to texture, ping pong
Game Over
What this code does
- WebGL Compute: WebGL2 has no compute stage, so a full-screen fragment pass into a render target acts as one dispatch, one texel per work item.
- Ping-Pong: two nearest-filtered targets alternate as input and output, so every cell reads the previous generation.
- Rules: Life (B3/S23), Brian's Brain (on → dying → off) and a cyclic CA where cells advance when a neighbour holds the next state.
- Throughput: millions of cell updates per pass; raise steps per frame to fast-forward hundreds of generations.
- Interaction: pointer drags write random cells inside a brush radius in the same pass.
- Controls: automaton, grid size, steps per frame, seed density, brush size and reseed.
Related Demos
Continue Learning
JavaScript Implementation
// WebGL has no compute shaders: a full-screen fragment pass into a render target plays that role.
// Each output texel is one "thread"; ping-pong two targets so a pass reads last frame's state.
const targets = [0, 1].map(() => new THREE.WebGLRenderTarget(1474, 1024, { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter }))
let cur = 0
function dispatch () {
stepMaterial.uniforms.uState.value = targets[cur].texture
renderer.setRenderTarget(targets[1 - cur])
renderer.render(quadScene, quadCamera) // Life rule evaluated for every cell in parallel
cur = 1 - cur
}
for (let i = 0; i < 4; i++) dispatch() // several generations per frame
renderer.setRenderTarget(null)