# brainrot 003: sand into a glass marble Live: https://brainrot.runfridge.dev/003/ Source: https://github.com/hwhang0917/brainrot/tree/main/003 License: MIT (see LICENSE at repo root); attribution only, no warranty. ## What it is A falling-sand simulation at one cell per CSS pixel: a stream of coloured sand pours from the top, swaying side to side, piles up on a big swirling glass marble in the middle of the screen and spills off its sides; the floor drains so it never fills. Click anywhere off the marble to change the sand colour (golden-ratio hue hop). Click the marble to shake it: it jiggles and the exposed sand sprays off in a fountain, with a vibration pattern on phones. Buttons: 📺 CRT and eye (hide controls). No sound, no URL params. ## How it was built - Raw WebGL2, no Pixi: two GLSL ES 3.00 fragment shaders on a fullscreen triangle. SIM ping-pongs between two RGBA8 textures; DRAW shades the grid and the marble (cat's-eye swirl, diffuse, specular, fresnel). - Cell encoding: r = type (0 empty, 1 sand, 3 spray), g = hue, b = brightness jitter, a = spray energy. The marble is not stored: both shaders treat cells inside its circle as wall (an SDF test in `get()`), so sand piles on it and never enters. - Movement uses a Margolus neighbourhood: 2×2 blocks whose offset alternates every step, so all four cells of a block compute the same result and no two grains can claim one cell. Sand falls, drifts sideways (0.3), slides diagonally (0.8). - Steps are paced by real time: 180 steps/s, at most 12 per frame. Grid size = window size in CSS px (devicePixelRatio ignored on purpose); resize is debounced and resets the sim. - From the git history: clicking the marble first deleted the sand; then shaken grains became "spray" cells that fly outward one cell per step until their energy runs out and fall back as sand. Only the drawn marble jiggles, never the sim's, or it would swallow grains. Latest fix: the shake lasts 2 s of sim time and launches every exposed grain, spray blocked behind other spray waits instead of turning back into sand, and both the shake decay and the emitter sway run on the sim's step clock so a slow device shakes as much. - Vibe coded: prompted into existence with a coding agent, iterated commit by commit. ## Files - 003/index.html: page shell, meta, CSS, the canvas, the hint line, CRT button. - 003/main.ts: everything: constants, the SIM and DRAW shaders as template strings, WebGL setup, pointer handling, frame loop; imports `mountCrt` (lib/crt.ts), `mountHud` (lib/hud.ts). - 003/sim.test.ts: headless-browser pixel test (test/browser.ts): sand piles on the marble and spills; a tap on the marble sprays sand into a halo, then clears most of the pile. ## Extending it - Run: `npm install && npm run dev`, open http://localhost:5173/003/ . Tests: `npm test`. Typecheck+build: `npm run build`. - Knobs: the constants at the top of main.ts; they are spliced into GLSL through `f()`, which forces a float literal (GLSL ES has no int→float promotion and `${1.0}` prints as `1`). Always use `f()` for spliced numbers. - The browser test needs chrome-headless-shell (Playwright cache or `CHROME_PATH`), else it is skipped. It runs under SwiftShader, well behind real time, so it waits in sim steps via the `window.__steps()` hook; keep that hook. Its geometry mirrors main.ts (marble at W/2, H·0.5, radius 0.2·min(W,H)); change both together. - Rules not to break: the sim never writes WALL cells (walls come from the SDF); row 0 drains; anything timed (shake decay, sway) must use the step count, not wall time. - `export {}` keeps main.ts a module so its globals do not collide with other pages under tsc. - Conventions: keep pure simulation in its own module with a `node --test` file beside it (here the logic is GLSL, so the test is a pixel test); match the surrounding code style; Conventional Commits `type(003): message`.