Pixel Splats

Lab / Pixel Splats

Each of these is a small 3D scene you can drag around, and every frame of it reads as pixel art. Nothing in them is a model file or a sprite sheet: each scene is a list of simple shapes, written by hand in a single HTML page with no libraries. Below the scenes is how one is made, and what making them taught me.

The scenes

  • Night Watch

    /lab/night-watch/

    A lighthouse on a rock in a square of night sea, its beam going round over a cottage with the lamps lit, washing on the line and a boat at the dock, where Fry sits watching a fishing float. The most detailed splat so far: 299 shapes, 280,000 splats, a sea that moves, a beam worked out through the air, and two-tone and isometric switches.

  • The Catch

    /lab/trapeze/

    Under a striped big top, a red panda hanging by her knees and a banana in sneakers swing in on two trapezes, hands out, a breath from the catch. The scene is 102 simple shapes melted into a distance field, scattered with Gaussian splats and drawn as pixel art. Drag to look around.

  • Carry On

    /lab/road-brothers/

    Two brothers lean on a long black sedan on a back road at dusk, headlights on, a motel sign buzzing behind them. The scene is 114 simple shapes melted into a distance field, scattered with Gaussian splats and drawn as pixel art under a dithered sky. Drag to look around.

  • Knight vs. Dragon

    /lab/knight-dragon/

    A knight on a crag holds his sword up into a dragon's fire. The scene is 148 simple shapes melted into a distance field, scattered with Gaussian splats and drawn as pixel art. Flip between the clay, the splats and the pixels, and drag to look around.

How a splat is made

Every scene goes through the same three steps, and every page has three buttons under the picture, SDF, Splats and Pixels, so you can stop at any one of them. Here is Knight vs. Dragon at each:

  1. The knight and the dragon as smooth grey clay over a floor of rings.
    1 · SDF
  2. The same scene in full color, made of thousands of soft overlapping dots.
    2 · Splats
  3. The same scene as chunky pixel art with a dithered glow behind it.
    3 · Pixels

1. Sculpt it as a distance field

A signed distance field is a function that takes any point in space and says how far it is from the surface: positive outside, negative inside, zero on the skin. The scene is one list of primitives, mostly round cones (two end points, two radii, good for limbs, tails, swords and necks) and ellipsoids (heads, bodies, rocks). Later scenes added rounded boxes and cylinders, and shapes that carve out of what came before them, like a car's wheel wells.

The shapes are melted together one after another with a polynomial smooth minimum, where each shape has its own blend radius k: a big one where a neck flows into a body, almost none where a sword meets a hand. Each one also carries a material tag (fur, armor, wing, banana peel) and a bounding sphere, so a point far from it can skip it. The same packed list drives two copies of the distance function written line for line alike: one in JavaScript, used to place the splats, and one in GLSL, which raymarches the clay you see on the SDF button, with ambient occlusion taken from the field itself and distance rings drawn on the floor.

The first scene started from the recommended 25 to 35 shapes. They ended up between about 100 and 150, because a dragon made of a few dozen blobs looks like a balloon animal.

2. Scatter splats on the surface

A coarse grid (about 44 cells a side) finds the cells the surface passes through. Random points in those cells are kept only if they're close to the surface, then pulled onto it with two Newton steps along the field's gradient, which is also their normal. Each point becomes a splat: a small, soft, flat ellipse lying on the surface, colored by the material of the nearest shape plus some procedural detail (stripes, spots, a bib, the glint in an eye), with ambient occlusion baked in by sampling the field a little way out along the normal. Fur and rough surfaces get a bit of jitter in size, brightness and height so they don't look like plastic.

The random numbers are seeded, so the scene comes out the same on every load. The splats are made in slices of about 9 ms a frame, which is why a scene fills in over a second or two instead of freezing the page. Computers get roughly 70,000 to 130,000 splats; phones get about half.

To draw them, every frame sorts the splats back to front with a 16-bit counting sort on depth and draws them as instanced quads, each with a Gaussian falloff and premultiplied alpha, so they blend like paint. All the motion (a tail, wings, a cape, a swing on a trapeze) happens in the vertex shader by moving splats, with smooth weights so nothing tears. That's the Splats button.

3. Draw it as pixels

The splats are drawn again into a tiny framebuffer, a fraction of the screen's size (the pixel size slider; 6 is the default, the chunky look I like), and one full-screen pass blows it back up with nearest-neighbor sampling. In that pass a 4x4 Bayer matrix decides which pixels count as covered, and every covered pixel snaps to the nearest color in the scene's palette of 20 or 30 hand-picked colors, with a little Bayer offset so gradients break into dither instead of bands. Empty pixels never go through that search: they pick from a few background steps by how close they are to the glow behind the subject, so the backdrop stays clean. Skies, tents and far fields are painted per pixel by view direction in the same pass.

What building them taught me

  • Sculpt real anatomy. The first dragon got a "make it a lot better" from me. The second one has a spine, jaws with teeth, horns and bat wings, and it's the one on the page. Plan the parts like a figure, not a blob.
  • Hard things need hard shapes. A car built from round cones looked inflatable. Rounded boxes and cylinders, with cuts for the wheel wells, made it look like steel.
  • Faces have to face out. Two characters looking only at each other are invisible from the side. Turning them a little toward the camera reads better than the "right" pose.
  • Over blending eats fire. Sorted back to front, the cool outer flames hid the hot core and the jet came out one flat orange. Sorting the fire hotter on top keeps the core bright from every side.
  • Dither coverage punches holes. Thresholding raw splat alpha with Bayer left pinholes inside solid bodies, so the pixel pass boosts it first. And low, even alpha (dust, mist) turns into a regular dot grid: vary the alpha per particle, or make dust single opaque pixels that twinkle.
  • Keep the ambient warm. A blue ambient light makes shaded cream and orange snap to the wrong palette colors. Neutral to warm keeps the ramps honest.
  • Start the camera where the story is. A wide scene is tiny on a portrait phone, so tall screens start from an angle that looks along it, and every page holds a front view under reduced motion.
  • Phones get less. Half the splats, fewer particles and a 30 fps cap keep a phone cool. A headless browser renders WebGL in software, so it can check what a frame looks like but says nothing about speed on a real phone.
  • Open in the middle. The sliders start mid-range so there's room to go both ways, and debug parameters (?stage=, ?t=, ?yaw=, ?px=, ?sync) freeze any moment for a screenshot or a link preview.

Off the page

The same recipe made the slice of pepperoni pizza on the 1986 desk. That desk is built of crisp blocks, so the third step changes: instead of a low-res framebuffer, the splats that land in each cell of the desk's block grid are averaged and snapped to the palette, and a cell is filled when enough of them cover it. No dither, to match everything around it.