Three.js GPU Particles (GPGPU, 1 Million Particles)
Up to one million particles simulated entirely on the GPU in Three.js with GPUComputationRenderer, using position and velocity float textures, curl-noise flow, a moving attractor and lifetime respawn, drawn as additive points.
Key topics: three.js gpgpu, gpu particles, gpucomputationrenderer, curl noise, million particles, fbo simulation
Game Over
What this code does
- Data in Textures: each texel of a float render target stores one particle's position (xyz + life) or velocity.
- Compute Passes: `GPUComputationRenderer` runs a full-screen fragment shader per variable each frame, ping-ponging targets.
- Curl Noise: the curl of a noise vector field is divergence-free, giving swirling, non-clumping flow.
- Respawn: when life in the w channel runs out, a particle respawns on a sphere with a new random lifetime.
- Rendering: `Points` read their position from the texture in the vertex shader, so the CPU never touches particle data.
- Controls: particle count (65k–1M), curl strength, attractor pull, point size and attractor orbit.
Related Demos
Continue Learning
JavaScript Implementation
import { GPUComputationRenderer } from 'three/examples/jsm/misc/GPUComputationRenderer.js'
const N = 512 // 262,144 particles, one texel each
const gpu = new GPUComputationRenderer(N, N, renderer)
const pos = gpu.addVariable('texturePosition', `
uniform float uDelta;
void main () {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 p = texture2D(texturePosition, uv);
p.xyz += texture2D(textureVelocity, uv).xyz * uDelta;
gl_FragColor = p;
}`, gpu.createTexture())
const vel = gpu.addVariable('textureVelocity', velocityShaderSource, gpu.createTexture())
gpu.setVariableDependencies(pos, [pos, vel])
gpu.setVariableDependencies(vel, [pos, vel])
pos.material.uniforms.uDelta = { value: 0 }
gpu.init()
// each frame: gpu.compute(), then sample gpu.getCurrentRenderTarget(pos).texture in the points' vertex shader