nemr.app

EngineeringIndie Games

How the Particle Morph on This Site Works (Three.js, 11,000 Points)

· 5 min read · by Elias Nemr

A walkthrough of the three.js particle scene on nemr.app: sampling shapes, per-particle easing, a custom shader, pointer interaction and a small bundle.

The home page of this site is one WebGL canvas with about 11,000 particles that reshape themselves into a planet, a chain of blocks, a candlestick chart, a neural network, a lightning bolt, a space invader and a magnifying glass as you scroll. There's no model, no texture and no asset file behind any of it: every shape is generated from a few lines of maths. Here's how it works.

The core idea: same particles, different targets

The trick that makes morphing easy is to give every shape the same number of points. Particle #4,217 is always particle #4,217; only its target position changes. To morph, each particle just moves toward its new target. There's no matching problem, no mesh topology, no keyframes.

So the whole scene is: one Points object, one position buffer that's updated every frame, and a list of precomputed target buffers — one per shape.

Generating shapes by sampling

Each shape is a function that returns one random point on (or in) that shape. Call it N times and you have a point cloud. A few examples:

  • Sphere. Pick y uniformly in [−1, 1], an angle uniformly in [0, 2π), and compute the ring radius √(1−y²). That's uniform on the sphere's surface, which avoids bunching at the poles that naive latitude/longitude sampling causes.
  • Wireframe cubes. Choose one of a cube's 12 edges at random and a random position along it. Rotate and translate per cube to make the chain.
  • Box surfaces (candles, parcel). Choose a face with probability proportional to its area, then a random point on it.
  • Lightning bolt. Define the bolt as a 7-vertex polygon, then use rejection sampling: pick random points in its bounding box and keep the ones that pass an even-odd point-in-polygon test. No triangulation needed.
  • Space invader. An 11×8 bitmap. Choose a filled cell at random and a random point inside it, slightly shrunk so the pixels read as separate voxels.
// uniform point on a sphere's surface
const y = 1 - 2 * rnd();
const r = Math.sqrt(1 - y * y);
const th = Math.PI * 2 * rnd();
return [Math.cos(th) * r * R, y * R, Math.sin(th) * r * R];

Because the random number generator is seeded (a tiny mulberry32), the shapes are identical on every load. Each sampler also returns a 0–1 value that picks a colour along that world's two-colour gradient — for the finance chart it's literally "up candle or down candle".

Morphing: per-particle exponential easing

On each frame, every particle moves a fraction of the way to its target:

const k = 1 - Math.exp(-dt * rate[i]);   // frame-rate independent
cur[j]     += (dx - dz * swirl[i]) * k;
cur[j + 1] += dy * k;
cur[j + 2] += (dz + dx * swirl[i]) * k;

Two details make it feel alive instead of mechanical:

  • Every particle has its own speed (rate, randomly 1.4–5.0 per second), so the cloud stretches and then snaps into shape instead of arriving as a block.
  • A small swirl term adds a component of the displacement perpendicular to the direction of travel (around the Y axis). Particles spiral toward their targets rather than flying in straight lines. When the world changes, a small random burst is added to the positions first, so it reads as an event.

1 - exp(-dt·rate) is the frame-rate-independent version of "move 10% closer each frame": it behaves the same on a 60 Hz and a 144 Hz display. Colours ease the same way. Once every particle is within a tiny threshold of its target, a moving flag turns off and the buffers stop being re-uploaded to the GPU, so a settled scene costs almost nothing.

The shader: soft, additive, slightly alive

Particles are drawn as gl_POINTS with a custom ShaderMaterial:

  • Additive blending with depth writing off. Overlapping particles add their light, so dense regions glow and sparse ones stay faint — free depth cues without sorting.
  • Soft round points. The fragment shader computes the distance from the point's centre and applies a smooth falloff (pow(smoothstep(0.5, 0.0, d), 1.6)), so each point is a small glow rather than a hard square.
  • Size attenuation. gl_PointSize scales with 10.0 / -mv.z, so nearer points look bigger. A per-particle random seed varies size and brightness, which stops the cloud looking like a regular grid.
  • Idle shimmer in the vertex shader. A few sines offset each point by a hair, keyed by its seed. It costs nothing on the CPU because it never touches the buffers.
  • Pointer repulsion. The pointer's position is converted to view space on the CPU, and the vertex shader pushes nearby points away with a smooth falloff. Doing it in view space keeps it correct however the model is rotated.

Driving it from scroll

The page is ordinary HTML sections. An IntersectionObserver with a root margin of -50% 0px -50% 0px collapses the viewport to a single line through its middle, so "which section is active?" is simply "which one is crossing that line". That index selects the target shape and the accent colour (a registered CSS custom property, so it transitions smoothly). Hovering the industry chips previews a world using the same call.

Keeping it fast and robust

  • Small bundle. Importing from the full three.js package would pull in far more than needed. I bundled only the classes the scene uses with esbuild, which comes to about 520 KB minified and 133 KB gzipped, served from the same origin with no third-party requests.
  • Fewer particles on phones (6,500 instead of 11,000), and the device pixel ratio is capped at 2.
  • Reduced motion. If the user prefers reduced motion, shapes snap into place and the sway and shimmer are turned off.
  • Progressive enhancement. The scene loads with a dynamic import() inside a try/catch. If WebGL fails, the canvas is hidden and the page still works.

Why it's good for SEO too

A WebGL hero can be a trap for search: if the content lives in the canvas, crawlers see nothing. Here the canvas is purely decorative and aria-hidden; all the text is real, server-delivered HTML with proper headings, so search engines and AI crawlers read the page exactly as a person would. The animation is an enhancement, not the content. (More on this in the technical SEO checklist.)

What I'd do differently

CPU-side easing for 11,000 points is perfectly fine, but at 100,000+ you'd want the morph on the GPU: store targets in textures or attributes, interpolate in the vertex shader with a single time uniform, and never touch the buffers per frame. You'd lose the per-frame swirl feedback loop but gain two orders of magnitude of headroom. For a landing page, the simple version is the right one.

The whole thing is a few hundred lines. If you build your own, start with just two shapes and a lerp; everything above is refinement on that one idea.

Elias Nemr is a software engineer who has built and shipped products across Blockchain, Finance, E-Commerce, AI, Energy and Indie Games. More about him →

Found this useful? Buy me a coffee

Keep reading