Skip to content

A million particles in WebGL2, no WebGPU

How Particle Field simulates up to 1M particles in WebGL2 with float textures and multiple render targets, and the tiers that keep phones running.

· Updated · 4 min read

Particle Field, a cloud of blue and violet particles drifting in a curl-noise flow on a dark background

WebGL2 can simulate a million particles on the GPU without compute shaders. You keep every particle's position and velocity in float textures, one texel per particle, and a full-screen shader pass updates all of them each frame. My demo Particle Field does this in raw WebGL2, with no engine.

WebGPU has real compute shaders, which should suit this job better where it is available. I did not build a WebGPU version, so I cannot tell you how much faster it would be. This post covers the WebGL2 version: the data layout, the draw call, and the quality tiers that let it run on a phone.

Why WebGL2 and not WebGPU#

MDN describes WebGPU as the successor to WebGL, with first-class support for general-purpose GPU computation through compute pipelines, where WebGL "does not handle general-purpose GPU (GPGPU) computations very well". When I read that page it still carried a limited-availability label.

Support is still uneven: several 2026 roundups say Chrome, Edge, Firefox on Windows and Apple Silicon Macs, and Safari 26 and later ship it by default, but they disagree on dates and most are blogs, so check caniuse for current numbers. Older Safari and Linux Firefox still need a WebGL2 path. A fallback is needed anyway, so I wrote only the fallback, and it runs on every browser that matters to me.

State lives in textures#

Each particle is one texel. A million particles is a 1024 × 1024 texture, and two textures hold the state: position (x, y, z and a random seed) and velocity. Both are RGBA32F.

Rendering to float textures needs the EXT_color_buffer_float extension. MDN says it is available only in WebGL 2 contexts and lists RGBA32F among the formats it makes color-renderable.

There are two such pairs that swap roles every frame. One full-screen pass reads the old pair and writes the new pair, and it writes position and velocity at the same time using multiple render targets (drawBuffers). Even the first state is made on the GPU, with an init pass, so the CPU never builds or uploads a particle array.

The flow#

The motion is a curl-noise flow. The velocity follows the cross product of the gradients of two 3D simplex noise fields. Because the field is divergence-free, particles fold like smoke without clumping into points. I compute the gradients analytically, so each particle needs two noise calls instead of repeated samples of the field. A soft spherical wall pulls strays back.

The pointer is projected onto a plane through the centre that faces the camera. Within a Gaussian radius it adds a vortex around the view axis, a stir along its own motion and a slight push.

Drawing without a vertex buffer#

The draw is one gl.POINTS call with no vertex buffers. The vertex shader gets its particle from gl_VertexID and texelFetch:

  1. gl_VertexID gives the particle index.
  2. The index becomes a texel coordinate in the state texture.
  3. texelFetch reads the position, and the shader projects it.

Points add up in a half-float buffer, so dense places run hot, and trails darken the previous frame instead of clearing it. Two bloom radii, a filmic curve, a vignette and grain finish the frame.

Shapes from text#

Press and hold, and every particle is pulled to its own point on a shape: the word "VK", a sphere, a torus knot or a galaxy. The shapes are maths inside the shader. The word is different: it is drawn into an offscreen 2D canvas in the site's display font, the filled pixels are sampled into a 128 × 128 float texture (up to 16,384 points), and each particle takes one of them and jitters inside its pixel.

The pull is a damped spring. Each particle starts offset by its random seed, so the shape gathers in a wave rather than all at once.

Tiers and a governor#

A million particles is not a phone workload. The page has three tiers:

TierTextureParticlesWhere it starts
High1024 × 10241MDesktops
Mid512 × 512256KPhones
Low256 × 25664KSoftware renderers, and the bottom step

Desktops start on High and phones on Mid, the GPU's texture limit can push that lower, and software renderers start on Low. On my own machine's GPU the High tier holds 60 fps. A governor then watches the frame time and steps down when the smoothed value stays above 24 ms for two seconds:

export function createGovernor(limitMs = 24, holdMs = 2000, smoothing = 0.1): Governor {
  let avg = 16.7;
  let over = 0;
  return {
    sample(frameMs) {
      if (!(frameMs > 0) || frameMs > 250) return false;
      avg += (frameMs - avg) * smoothing;
      over = avg > limitMs ? over + frameMs : 0;
      if (over >= holdMs) {
        avg = 16.7;
        over = 0;
        return true;
      }
      return false;
    },
    // ...
  };
}

Frames over 250 ms are ignored as outliers. After a step the governor starts over, so it can step down again.

Fewer particles would look dim, so each tier gets a gain that keeps the field about as bright:

export function particleGain(count: number): number {
  return 0.03 * Math.pow((1 << 20) / Math.max(1, count), 0.62);
}

Points on the lower tiers are also drawn a little larger. I covered why software renderers start low in Why a WebGL hero can tank PageSpeed.

Reduced motion and lost contexts#

Under reduced motion there is no idle orbit and no release burst. The field is run forward before the first frame, so the page opens on a still image, and the simulation runs at about a third of the speed and only while you interact.

If the graphics context is lost, the page shows a "Scatter the particles again" state that rebuilds everything.

Caveats on the measurements#

The 60 fps figure with a million particles is from my own machine. I have not run the page on a real phone, so the 256K tier on phones is a plan and not a measurement. I did not compare the WebGL2 version with a WebGPU one.