# brainrot 004: lava lamp Live: https://brainrot.runfridge.dev/004/ Source: https://github.com/hwhang0917/brainrot/tree/main/004 License: MIT (see LICENSE at repo root); attribution only, no warranty. ## What it is A tapered lava lamp centred on a dark screen. The bulb in the base heats the wax, which rises in blobs, cools and sinks, with a neon halo and rim highlights. A "light" pill button at the bottom switches the bulb off and on; off, the heating stops, the lamp fades dark over ~1.6 s and the wax cools, sinks and settles. Buttons: 📺 CRT and eye (hide controls). No sound, no URL params. ## How it was built - Raw WebGL2 with `EXT_color_buffer_float` (required, throws otherwise). A stable-fluids solver on a 160×320 grid of RGBA16F textures covering only the vessel's bounding box; linear filtering makes it look smooth at screen size. - Fields: velocity (x, y) and dye = (wax, temperature). Per fixed step (1/60 s): advect velocity (dissipation 0.99), add forces (buoyancy 1.2 × temperature minus wax weight 0.35 × wax), advect wax, heat (bulb zone in the bottom 20%, only wax takes heat; global cooling 0.3/s), divergence, 20 Jacobi pressure iterations, subtract gradient. The `vessel()` mask (shared GLSL) zeroes velocity in the glass. - Wax stays immiscible by blurring (0.35) then sharpening (×1.03 per step) around 0.5, which also rounds blobs like surface tension. - From the git history: blur+sharpen is not mass-conserving and thin plumes eroded until the wax vanished, fastest on 120 Hz phones where the per-frame sim ran double speed. The fix: a controller reads the mean wax from the dye texture's 1×1 mip each step and nudges the sharpen threshold (gain 4, clamped ±0.1) to hold the initial mass, and steps are now accumulated from elapsed time (max 4 per frame). Integer constants are spliced as float literals since GLSL ES rejects float * int. - Vibe coded: prompted into existence with a coding agent, iterated commit by commit. ## Files - 004/index.html: page shell, meta, CSS, canvas, the light button, CRT button. - 004/main.ts: everything: constants, all shaders (INIT, ADVECT, FORCES, HEAT, DIVERGENCE, PRESSURE, GRADIENT, DRAW) as template strings, `PingPong` targets, `pass()` helper, `meanWax()`, step and frame loop; imports `mountCrt` (lib/crt.ts), `mountHud` (lib/hud.ts). - 004/sim.test.ts: headless-browser pixel test (test/browser.ts): wax rises above its start level within 20 s, and the lamp's mean luma halves after the light is clicked off. ## Extending it - Run: `npm install && npm run dev`, open http://localhost:5173/004/ . Tests: `npm test`. Typecheck+build: `npm run build`. - Knobs: the constants at the top of main.ts (BUOYANCY, WAX_WEIGHT, HEAT_RATE, COOL_RATE, WAX_SHARPEN, WAX_BLUR, MASS_GAIN, PRESSURE_ITERS, INITIAL_WAX_HEIGHT, colours in DRAW). Splice numbers into GLSL only through `f()`. - Invariants: keep the mass controller and the `generateMipmap` before the wax advect (it refreshes the 1×1 mean), or wax slowly disappears; keep fixed-DT stepping paced by real time, never one step per frame. - The DRAW mapping (vessel 0.84 of canvas height, aspect SIM_W/SIM_H, shrinks on narrow screens) is mirrored by the test's geometry; change both together. - The browser test needs chrome-headless-shell (Playwright cache or `CHROME_PATH`), else it is skipped; it waits in 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(004): message`.