GPU Particle Field Simulation.
Rendering over 50,000 particle points on the GPU with vertex shader velocity fields and depth fog attenuation.
View code
Run the JavaScript as a module. Keep the import map from the HTML panel.
<script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.180.0/build/three.module.js","three/addons/":"https://cdn.jsdelivr.net/npm/three@0.180.0/examples/jsm/"}}</script>
<canvas aria-label="Animation demo"></canvas>:root { color-scheme: dark; font: 15px system-ui, sans-serif; color: #dbff55; }
* { box-sizing: border-box; }
body { margin: 0; height: 100dvh; display: grid; place-items: center; overflow: hidden; background: transparent; }
.tile { display: grid; place-items: center; width: 68px; aspect-ratio: 1; border-radius: 18px; background: currentColor; }
.tile { background: #dbff55; color: #202415; font-size: 30px; }
canvas { display: block; width: 100%; height: 100%; min-width: 0; min-height: 0; }
button { font: inherit; color: #f3f1e9; background: #191919; border: 1px solid #ffffff66; padding: 8px 14px; border-radius: 24px; cursor: pointer; }
:focus-visible { outline: 2px solid #dbff55; outline-offset: -3px; }
.notice { max-width: 34ch; padding: 16px; color: #fff; }
@media (prefers-reduced-motion: reduce) { .sprite { animation-play-state: paused; } }import * as THREE from 'three';
// The gallery pauses this loop when the preview leaves the viewport.
let active = !matchMedia('(prefers-reduced-motion: reduce)').matches;
if (new URLSearchParams(location.search).has('embed')) active = false;
addEventListener('message', event => {
if (event.source === parent && event.origin === location.origin && event.data?.type === 'motion-play') {
active = event.data.active;
}
});
function loop(draw) {
let time = 0;
let previous;
function frame(now) {
const dt = previous === undefined ? 0 : Math.min((now - previous) / 1000, .05);
previous = now;
if (active && !document.hidden) {
time += dt;
draw(time, dt);
}
requestAnimationFrame(frame);
}
draw(0, 0);
requestAnimationFrame(frame);
}
const canvas = document.querySelector('canvas');
let renderer;
try {
renderer = new THREE.WebGLRenderer({canvas, antialias:true, alpha:true});
} catch {
canvas.replaceWith(Object.assign(document.createElement('p'), {
className: 'notice', textContent: 'This demo needs WebGL 2. Try a browser with hardware acceleration enabled.'
}));
throw new Error('WebGL 2 unavailable');
}
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(42, 1, .1, 120);
camera.position.z = 5;
function resize() {
renderer.setPixelRatio(Math.min(devicePixelRatio || 1, 2));
const width = Math.max(1, canvas.clientWidth);
const height = Math.max(1, canvas.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}
resize();
addEventListener('resize', resize);
const count = 3600;
const positions = new Float32Array(count * 3);
for (let i=0; i<count; i++) {
positions.set([i*.618 % 1, i*.38197 % 1, i*.71353 % 1], i*3);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
const uniforms = {uTime:{value:0}};
const material = new THREE.ShaderMaterial({
uniforms, transparent:true, depthWrite:false, blending:THREE.AdditiveBlending,
vertexShader: [
'uniform float uTime; varying float glow;',
'void main(){ float angle=position.x*6.283+uTime*(.5+position.y);',
'float r=.12+1.6*position.y; float y=1.4*(position.z-.5);',
'vec3 p=vec3(r*cos(angle),y+.34*sin(angle*2.0),r*sin(angle));',
'vec4 eye=modelViewMatrix*vec4(p,1.0);',
'gl_Position=projectionMatrix*eye; gl_PointSize=11.0/-eye.z; glow=.35+.65*position.y; }'
].join('\n'),
fragmentShader: 'varying float glow; void main(){ float d=length(gl_PointCoord-.5); gl_FragColor=vec4(.34,.75,1.0,glow*(1.0-smoothstep(.05,.5,d))); }'
});
const points = new THREE.Points(geometry, material);
points.rotation.x = .55;
scene.add(points);
loop(t => {
uniforms.uTime.value = t;
renderer.render(scene, camera);
});