Kanwar Pal Singh
All notes

Note

How Bubble’s liquid look works

One small shader, no 3D models and no textures.

Bubble is drawn by a single fragment shader on the graphics card. For each pixel of its small canvas, the shader sends a ray from a virtual camera and steps along it until it reaches a surface. This is ray marching: the surface is described by a distance function instead of a mesh of triangles.

The body starts as a sphere. A few sine waves, each moving through space and time at its own speed, push the surface in and out by a few percent. They never line up the same way twice, so the surface keeps moving like liquid.

Nothing around Bubble exists either. Its reflections come from a made-up studio: white above, grey below, a few dark vertical strips and two coloured lamps, all worked out from the direction each ray bounces. The studio turns slowly, so the reflections keep moving even when Bubble is still. The soap look adds the rainbow of a thin film, strongest where the surface turns away from you; chrome, mercury and the module look each change the body and the finish.

The eyes, the four outfits and the sleepy squint are more distance functions in the same scene, so they move, blink and reflect with the body. Outfits and the look are numbers the page passes to the shader, which is why a click can change them instantly.

Compiling a shader this size can take a few seconds on a slow phone. Bubble asks the browser to compile it in the background where it can, shows a still image until the live version is ready, and only then switches over, so the page never waits for it.

Meet Bubble on the home page