Three.js Reaction–Diffusion (Gray–Scott on the GPU)
GPU Gray–Scott reaction–diffusion in Three.js using ping-pong render targets, with coral, mitosis, maze and soliton presets, an F × k parameter map, paint-to-seed and a seamless torus display.
Key topics: three.js reaction diffusion, gray scott, turing patterns, gpgpu, render targets, ping pong, morphogenesis
Game Over
What this code does
- Gray–Scott Model: chemical A is fed at rate F, B is removed at rate k + F, and the reaction A + 2B → 3B drives pattern formation.
- Ping-Pong Simulation: two half-float `WebGLRenderTarget`s swap every step, and a fragment shader computes the next state from a 3×3 Laplacian.
- Toroidal Domain: `RepeatWrapping` on the state texture makes the grid wrap, so it maps onto a torus without seams.
- Parameter Map: in map mode F varies along one axis and k along the other, showing every pattern regime in one image.
- Relief Shading: the display shader displaces the surface by B and bends normals with its gradient for lighting.
- Controls: preset, feed and kill rates, steps per frame, torus or plane, relief, palette, brush size and reseed (shift + drag to paint).
Related Demos
Continue Learning
JavaScript Implementation
// Gray–Scott on the GPU: render a full-screen quad into ping-pong render targets
const size = 256
const opts = { type: THREE.HalfFloatType, wrapS: THREE.RepeatWrapping, wrapT: THREE.RepeatWrapping }
const targets = [new THREE.WebGLRenderTarget(size, size, opts), new THREE.WebGLRenderTarget(size, size, opts)]
const simScene = new THREE.Scene()
const simCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1)
const sim = new THREE.ShaderMaterial({
uniforms: { uState: { value: null }, uTexel: { value: new THREE.Vector2(1 / size, 1 / size) } },
vertexShader: 'varying vec2 vUv; void main () { vUv = uv; gl_Position = vec4(position.xy, 0.0, 1.0); }',
fragmentShader: `
uniform sampler2D uState; uniform vec2 uTexel; varying vec2 vUv;
void main () {
vec2 c = texture2D(uState, vUv).rg;
vec2 lap = -c + 0.25 * (texture2D(uState, vUv + vec2(uTexel.x, 0.0)).rg + texture2D(uState, vUv - vec2(uTexel.x, 0.0)).rg
+ texture2D(uState, vUv + vec2(0.0, uTexel.y)).rg + texture2D(uState, vUv - vec2(0.0, uTexel.y)).rg);
float F = 0.0545, k = 0.062, abb = c.r * c.g * c.g;
gl_FragColor = vec4(c.r + 1.0 * lap.r - abb + F * (1.0 - c.r), c.g + 0.5 * lap.g + abb - (k + F) * c.g, 0.0, 1.0);
}`
})
simScene.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 2), sim))
let current = 0
const view = new THREE.Mesh(new THREE.PlaneGeometry(4, 4), new THREE.MeshBasicMaterial())
scene.add(view)
function animate () {
requestAnimationFrame(animate)
for (let i = 0; i < 10; i++) {
sim.uniforms.uState.value = targets[current].texture
renderer.setRenderTarget(targets[1 - current])
renderer.render(simScene, simCamera)
current = 1 - current
}
renderer.setRenderTarget(null)
view.material.map = targets[current].texture
renderer.render(scene, camera)
}
animate()
(seed the first target with A = 1 and a few B = 1 squares before starting)