From 098f248576bd9ef0b8da1a5c70c390ea9efa9023 Mon Sep 17 00:00:00 2001 From: Keeley Hammond Date: Thu, 19 Nov 2026 14:54:56 -0800 Subject: fix(theme-examples): shorten the /custom-theme description (#7152) Squash merge of 1 commit --- plugins/theme-examples/.claude-plugin/plugin.json | 2 +- plugins/theme-examples/hooks/register.tsx | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) (limited to 'plugins/theme-examples') diff --git a/plugins/theme-examples/.claude-plugin/plugin.json b/plugins/theme-examples/.claude-plugin/plugin.json index c7965507..006a01c9 100644 --- a/plugins/theme-examples/.claude-plugin/plugin.json +++ b/plugins/theme-examples/.claude-plugin/plugin.json @@ -1,6 +1,6 @@ { "name": "theme-examples", - "version": "0.4.3", + "version": "0.4.4", "description": "An example plugin to read, copy and change: twenty animated backdrops behind the chat in the desktop app, with frosted-glass panels: one per project or per session. /custom-theme list names them. Fireworks go off over the theme when Claude says \"merged\"; /fireworks sets them off by hand.", "author": { "name": "Anthropic", diff --git a/plugins/theme-examples/hooks/register.tsx b/plugins/theme-examples/hooks/register.tsx index ffbb7035..847fd5e3 100644 --- a/plugins/theme-examples/hooks/register.tsx +++ b/plugins/theme-examples/hooks/register.tsx @@ -883,8 +883,7 @@ export const register: Register = on => { try { await $.command.register({ name: COMMAND, - description: - 'A backdrop behind the chat: alone it shows the card; with a theme\'s name it applies that theme to this project (add "session" for this session only); "list" names them, "off" clears.', + description: 'Pick a theme. Add a name to apply it, "list" to see all, "off" to clear.', }) } catch (error) { void $.ui.toast(`theme-examples: /${COMMAND} is not available (${String(error)})`) @@ -893,8 +892,7 @@ export const register: Register = on => { try { await $.command.register({ name: FIREWORKS_COMMAND, - description: - 'Fireworks behind the chat for a few seconds, over the theme if there is one (they also go off when Claude says "merged": /fireworks auto off stops that)', + description: 'Set off fireworks. Add "auto off" to stop the automatic ones, "auto on" to resume.', }) } catch (error) { void $.ui.toast(`theme-examples: /${FIREWORKS_COMMAND} is not available (${String(error)})`) -- cgit From 4f2303046518925cde9907d25e23bf85645849eb Mon Sep 17 00:00:00 2001 From: Chaoshuai Lyu Date: Fri, 20 Nov 2026 10:56:06 -0800 Subject: feat(theme-examples): add nine interactive shader scenes (#7189) Squash merge of 3 commits --- plugins/theme-examples/.claude-plugin/plugin.json | 4 +- plugins/theme-examples/README.md | 16 +- plugins/theme-examples/hooks/fireworks.ts | 75 +- plugins/theme-examples/hooks/interactive.tsx | 48 + plugins/theme-examples/hooks/register.tsx | 58 +- .../theme-examples/hooks/scenes/duck-pond/scene.ts | 623 ++++++++++ .../theme-examples/hooks/scenes/duck-pond/still.ts | 6 + .../hooks/scenes/falling-sand/scene.ts | 699 +++++++++++ .../hooks/scenes/falling-sand/still.ts | 6 + .../hooks/scenes/field-lines/scene.ts | 249 ++++ .../hooks/scenes/field-lines/still.ts | 6 + .../hooks/scenes/garden-beetles/scene.ts | 738 +++++++++++ .../hooks/scenes/garden-beetles/still.ts | 6 + .../theme-examples/hooks/scenes/koi-pond/scene.ts | 1315 ++++++++++++++++++++ .../theme-examples/hooks/scenes/koi-pond/still.ts | 6 + .../hooks/scenes/ripple-tank/scene.ts | 666 ++++++++++ .../hooks/scenes/ripple-tank/still.ts | 6 + .../hooks/scenes/sea-sparkle/scene.ts | 618 +++++++++ .../hooks/scenes/sea-sparkle/still.ts | 6 + .../hooks/scenes/semicolony/scene.ts | 486 ++++++++ .../hooks/scenes/semicolony/still.ts | 6 + .../hooks/scenes/star-chart/scene.ts | 773 ++++++++++++ .../hooks/scenes/star-chart/still.ts | 6 + plugins/theme-examples/hooks/themes.ts | 205 ++- 24 files changed, 6587 insertions(+), 40 deletions(-) create mode 100644 plugins/theme-examples/hooks/interactive.tsx create mode 100644 plugins/theme-examples/hooks/scenes/duck-pond/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/duck-pond/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/falling-sand/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/falling-sand/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/field-lines/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/field-lines/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/garden-beetles/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/garden-beetles/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/koi-pond/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/koi-pond/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/ripple-tank/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/ripple-tank/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/sea-sparkle/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/sea-sparkle/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/semicolony/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/semicolony/still.ts create mode 100644 plugins/theme-examples/hooks/scenes/star-chart/scene.ts create mode 100644 plugins/theme-examples/hooks/scenes/star-chart/still.ts (limited to 'plugins/theme-examples') diff --git a/plugins/theme-examples/.claude-plugin/plugin.json b/plugins/theme-examples/.claude-plugin/plugin.json index 006a01c9..446b36d6 100644 --- a/plugins/theme-examples/.claude-plugin/plugin.json +++ b/plugins/theme-examples/.claude-plugin/plugin.json @@ -1,7 +1,7 @@ { "name": "theme-examples", - "version": "0.4.4", - "description": "An example plugin to read, copy and change: twenty animated backdrops behind the chat in the desktop app, with frosted-glass panels: one per project or per session. /custom-theme list names them. Fireworks go off over the theme when Claude says \"merged\"; /fireworks sets them off by hand.", + "version": "0.5.0", + "description": "An example plugin to read, copy and change: thirty-eight animated backdrops behind the chat in the desktop app, with frosted-glass panels: one per project or per session. /custom-theme list names them. Fireworks go off over the theme when Claude says \"merged\"; /fireworks sets them off by hand.", "author": { "name": "Anthropic", "email": "support@anthropic.com" diff --git a/plugins/theme-examples/README.md b/plugins/theme-examples/README.md index 59b52b11..71bac932 100644 --- a/plugins/theme-examples/README.md +++ b/plugins/theme-examples/README.md @@ -1,6 +1,6 @@ # Theme examples -Twenty scenes to paint behind your Claude Code desktop window, light and dark. +Thirty-eight scenes to paint behind your Claude Code desktop window, light and dark. Pick a scene for a project or for one session: a lagoon seen from above, a night reef, a koi pond, halftone sunrises, pen-plotter tides, paper gardens, a departure board, a starfield in braille dots and more. Each scene is drawn for a dark or for a light window and is animated. Its motion stops when your system asks for reduced motion; see Motion and comfort below. @@ -9,7 +9,7 @@ This plugin is an example of what a plugin can do with the backdrop of the deskt ## Use - `/custom-theme` shows a card with every theme, under the kind of window each is made for, with a preview button. -- `/custom-theme list` names all twenty, under the kind of window each is made for. +- `/custom-theme list` names all thirty-eight, under the kind of window each is made for. - `/custom-theme ` applies one to this project. Other open sessions of the project that have no choice of their own take it at their next turn. The name can be typed as the list prints it or as its title, in any case. - `/custom-theme session` applies it to this session only. - `/custom-theme off` clears the project's theme. Other open sessions of the project that have no choice of their own clear at their next turn. @@ -19,7 +19,15 @@ This plugin is an example of what a plugin can do with the backdrop of the deskt ## Light and dark windows -Each theme is made for a light or for a dark window; the card, the list and the answer to applying one say which. Eight of the twenty come in both kinds, and each of those names its twin. Whether the window follows a theme's kind is up to the app and depends on its version. Where the window keeps your own setting, pick a theme made for it, or change the setting under Settings, General, Appearance. +Each theme is made for a light or for a dark window; the card, the list and the answer to applying one say which. Twenty-six of the thirty-eight come in both kinds, and each of those names its twin. Whether the window follows a theme's kind is up to the app and depends on its version. Where the window keeps your own setting, pick a theme made for it, or change the setting under Settings, General, Appearance. + +## Scenes that answer the pointer + +Eighteen of the thirty-eight answer the pointer. They are nine pictures, each listed once for light windows and once, with ", Night" after its title, for dark ones: Duck Pond, Falling Sand, Field Lines, Garden Beetles, Koi Pond, Ripple Tank, Sea Sparkle, Semicolony, Star Chart. Each is a shader, a small program for the GPU, drawn over a still picture. Move the pointer over the margins beside the text, press, and drag. + +Each draws itself light or dark to match the window, whichever entry you picked (Koi Pond's water is deep green in a light window and darker in a dark one): where the window keeps your own setting, its two entries draw the same picture and differ only in the tint of the glass panels. These scenes also need a Claude Code and a desktop app that play shaders. Where a shader does not play (an older Claude Code or desktop app, reduced motion, a GPU too weak for the scene), the still picture shows alone; it is a finished picture by itself. `claude plugin validate` prints what each shader costs. The app itself gives these scenes the pointer's position; the mod never receives it. + +If you copy from this mod: a theme's row in `hooks/themes.ts` names its shader. Claude Code finds shader files by reading the source, so `hooks/interactive.tsx` writes each path out as a string. In a scene's SVG, give every `id` a hyphen, as in `k-deep`. The desktop app removes an id that is also the name of something built into a web page, such as `open` or `clear`, and says nothing, so a gradient, a mask or a clip that the picture refers to by that id goes missing. No such built-in name has a hyphen. ## What it needs @@ -43,6 +51,8 @@ In a new session and after /clear, no theme shows until you send a message. In a Whether a theme shows is asked of the session, not of each window on it, and at the moment the theme is set. A window that connects afterwards can draw the theme all the same, and so can a window that has not told Claude Code that it draws themes. +A fireworks show over one of the scenes that answer the pointer lasts nine and a half seconds, two and a half more than over the other scenes. The scene does not move or answer the pointer during the show and for two or three seconds after it. Where the app holds scenes still, a show over one of them has no bursts. + With another backdrop mod installed, a fireworks show or a preview ends on a bare window until that mod sets its backdrop again. A session has one theme at a time, so on newer versions of Claude Code clearing a theme clears whichever one stands, another plugin's too. A session that runs elsewhere, such as over SSH or in WSL, shares no saved theme with the sessions on this machine. diff --git a/plugins/theme-examples/hooks/fireworks.ts b/plugins/theme-examples/hooks/fireworks.ts index 1eba4b6a..46196f76 100644 --- a/plugins/theme-examples/hooks/fireworks.ts +++ b/plugins/theme-examples/hooks/fireworks.ts @@ -11,6 +11,20 @@ export const NIGHT_GLASS = { blur: 10, opacity: 0.45 } type Burst = { x: number; y: number; hue: number; delay: number; size: number } +// The bursts below are laid out for a picture 1600 wide. +const LAID_OUT_WIDTH = 1600 +// The spokes' width in such a picture. +const SPOKE_WIDTH = 3 +// A burst's lightness over a pale ground and over a dark one. +const DEEP = 40 +const BRIGHT = 68 + +/** + * What a show's bursts are laid over. 'light' and 'dark': a picture that is pale or dark, whatever the window. 'still': + * the still under a shader, pale in a light window and dark in a dark one. + */ +type Ground = Theme['window'] | 'still' + // Bursts at the edges, and along the top edge over the conversation, so no spoke crosses the text being read. // FOR PEOPLE SENSITIVE TO FLASHING LIGHT: each burst plays once, and their starts stand 1 s apart. Keep it so: a burst // added, or a start moved nearer, makes the whole picture change more often. @@ -28,11 +42,11 @@ const BURSTS: readonly Burst[] = [ // holds a scene still (no GPU, or animation off) the bursts then stand for the show, not vanish. // Held still, only the bursts to the right of the words' column rest open (`fw-shut` rests the others closed): // an open burst over the conversation or under the sidebar's labels would sit on text for as long as the -// scene is held. Running, the keyframes name every moment, so the class changes nothing. -// A person who asked their system for less motion asked for no picture either: there every burst rests closed. -const STYLE = +// scene is held. Over a theme with a shader none rests open: `layer` says why. Running, the keyframes name +// every moment, so the class changes nothing. Where a person asked their system for less motion, all rest closed. +const sheet = (spokeWidth: number): string => '\n" + ) +} diff --git a/plugins/theme-examples/hooks/scenes/falling-sand/scene.ts b/plugins/theme-examples/hooks/scenes/falling-sand/scene.ts new file mode 100644 index 00000000..17836792 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/falling-sand/scene.ts @@ -0,0 +1,699 @@ +// Falling Sand: coarse pixel-art sand on a 6 point grid. One buffer texel is one grain. +// Three small buffers at one texel a cell, then the picture. The first holds what stands in each cell. The second +// keeps the grains: a block automaton that moves things only by swapping two cells, so nothing is lost or doubled +// in motion. The third paints the pixel art. The picture only enlarges the third, so the buffers cost the same at +// any sharpness. Light and dark come from the still Svg's middle pixel. +import { + abs, buffer, clamp, composer, dot, dt, floor, fract, first, fresh, int, max, min, mix, mod, + picture, pointers, res, scene, shader, sin, size, sqrt, step, time, turn, unsigned, vec2, vec3, vec4, +} from 'claude-code/shader' +import type { Buffer } from 'claude-code/shader' + +const CELL = 6 + +// A number from 0 to 1 out of three whole numbers, for everything here that is left to chance. The mix is this file's own: +// the three are folded in one at a time, and each fold is stirred by adding a turned copy of the word to itself, +// between multiplies by odd constants. +const rnd = (a: number, b: number, c: number): number => { + let h = int(int(a) * 58479 + 6251) + h = int(h ^ (h << 7)) + h = int(h + int(b) * 60027) + h = int(h + ((h << 13) | (h >>> 19))) + h = int(h ^ (int(c) * 28355)) + h = int(h * 1419110893) + h = int(h + ((h << 19) | (h >>> 13))) + h = int(h * 918269209) + h = int(h ^ (h >>> 14)) + return unsigned(h) / 4294967296 +} + +// Where the fixed things stand, in cells. G is the grid's size. +// Each side margin holds a broken pillar and a stair of stone shelves. Shelves hang by turns from the window's edge +// and from the pillar, so a heap on one can only spill from its free end, onto the free end of the shelf below. +// A nozzle on each pillar's inner side feeds a wooden chute that ends over the message box, a few cells in from its +// end. On the right the nozzle sits high: a short chute tips its trickle into a field of pegs above the long one. +const world = (G: vec2) => { + const known = composer.z - composer.x > 0.02 + const half = min(61, G.x * 0.4) + const cl = known ? floor(composer.x * G.x + 0.5) : floor(G.x * 0.5 - half) + const cr = known ? floor(composer.z * G.x + 0.5) : floor(G.x * 0.5 + half) + const ct = known ? floor(composer.w * G.y + 0.5) : 0 + const lt = known ? ct : floor(G.y * 0.13) + const mL = max(cl, 0) + const mR = max(G.x - cr, 0) + const pL = floor(mL * 0.55) + const eL = floor(mL * 0.31) + const fL = eL - 3 + const a1 = floor(G.y * 0.75) + const a2 = floor(G.y * 0.58) + const a3 = floor(G.y * 0.41) + const a4 = floor(G.y * 0.25) + const topL = floor(G.y * 0.9) + const spL = lt + 12 + max(cl + 7 - (pL + 5), 0) + const sL = floor(mL * 0.14) + const pR = G.x - floor(mR * 0.5) - 5 + const eR = G.x - floor(mR * 0.3) + const fR = eR + 3 + const b1 = floor(G.y * 0.71) + const b2 = floor(G.y * 0.5) + const b3 = floor(G.y * 0.29) + const topR = floor(G.y * 0.79) + const spR = lt + 16 + max(pR - (cr - 8), 0) + const upR = min(spR + 46, topR - 4) + const pegRows = min(26, upR - spR - 20) + const sR = G.x - 1 - floor(mR * 0.14) + const dune = (x: number): number => floor(5 + 3.2 * sin(x * 0.07 + 0.6) + 1.3 * sin(x * 0.19 + 2.1)) + const slab = (y: number, top: number): boolean => y < top && y >= top - 3 + // 0 nothing, 1 outside, 2 dune, 3 message box, 4 shelf, 5 chute, 6 pillar + const what = (x: number, y: number): number => { + let w = 0 + if (x < 0 || x >= G.x || y < 0 || y >= G.y || (x < 0.5 && y > G.y - 1.5)) w = 1 + else if (y < dune(x)) w = 2 + else if (known && x >= cl && x < cr && y < ct) w = 3 + else if (x < pL) { + if (x < eL && (slab(y, a1) || slab(y, a3))) w = 4 + else if (x >= fL && (slab(y, a2) || slab(y, a4))) w = 4 + } else if (x < pL + 5) { + const d = x - pL + if (y < topL - (d < 0.5 ? 0 : d < 1.5 ? 2 : d < 2.5 ? 1 : d < 3.5 ? 4 : 3)) w = 6 + } else if (x <= cl + 7) { + const line = spL - 4 - (x - (pL + 5)) + if (y <= line && y > line - 2 && line > lt + 5) w = 5 + } else if (x >= pR + 5) { + if (x >= eR && (slab(y, b1) || slab(y, b3))) w = 4 + else if (x < fR && slab(y, b2)) w = 4 + } else if (x >= pR) { + const d = x - pR + if (y < topR - (d < 0.5 ? 3 : d < 1.5 ? 0 : d < 2.5 ? 1 : d < 3.5 ? 2 : 5)) w = 6 + } else if (x >= cr - 8) { + const line = spR - 4 - (pR - 1 - x) + const high = upR - 4 - (pR - 1 - x) + const gx = x - (cr + 3) + const gy = y - (spR + 6) + if (y <= line && y > line - 2 && line > lt + 5) w = 5 + else if (x >= pR - 9 && y <= high && y > high - 2) w = 5 + // a field of pegs between the two chutes: two cells of peg, two of gap, each row set off by two, so nothing + // falls straight through + else if (gx >= 0 && x <= pR - 3 && gy >= 0 && gy < pegRows && mod(gy, 5) < 0.5 && mod(gx + 2 * mod(floor(gy / 5), 2), 4) < 1.5) w = 4 + } + return w + } + return { known, cl, cr, ct, lt, mL, mR, pL, eL, fL, a1, a2, a3, a4, topL, spL, sL, pR, eR, fR, b1, b2, b3, topR, spR, upR, sR, what, dune } +} + +// What stands in each cell, worked out once a frame and read from here by the sand and the art (each of which asks +// about a dozen neighbours of every cell). +const fixed = buffer({ scale: 1 / CELL, filter: 'nearest' }, p => vec4(world(res).what(p.cell.x, p.cell.y) / 8, 0, 0, 1)) +const standsAt = (G: vec2, x: number, y: number): number => (x < 0 || x >= G.x || y < 0 || y >= G.y ? 1 : floor(fixed.at(vec2(x, y)).r * 8 + 0.5)) + +// kinds: 0 air, 1 grain, 2 crab walking right, 3 crab walking left, 4 a mote of dust (which counts as air), 9 fixed +const air = (k: number): boolean => k < 0.5 || (k > 3.5 && k < 4.5) +const mote = (k: number): boolean => k > 3.5 && k < 4.5 +const grain = (k: number): boolean => k > 0.5 && k < 1.5 +const crabR = (k: number): boolean => k > 1.5 && k < 2.5 +const crabL = (k: number): boolean => k > 2.5 && k < 3.5 +const heavy = (k: number): boolean => k > 0.5 && k < 3.5 + +// Sixty steps a second, so speeds do not depend on the frame rate: one step a frame at 60 frames a second, two at +// 30, three at 20. The buffer runs three times a frame, and a run with no step due hands every cell back as it was. +// A cell holds: r the kind, g a grain's colour (or a crab's own number), b how lately it moved, a a life or a mark. +const sand: Buffer = buffer({ scale: 1 / CELL, filter: 'nearest', repeat: 3 }, (p): vec4 => { + const G = res + const W = world(G) + const cx = p.cell.x + const cy = p.cell.y + // The top left texel is the clock. It counts the steps itself (lane a is the step in four, lane g the fours), so + // the automaton never depends on how large the frame number has grown. Lane b carries the part of a step left + // over from the last frame, in sixteenths, so the steps keep to sixty a second at any frame rate, and one bit for + // the crab arrival it last saw, so none is missed on a frame that takes no step. + const clock = sand.at(vec2(0, G.y - 1)) + const beat = floor(clock.a * 4 + 0.5) + const tick = floor(clock.g * 255 + 0.5) * 4 + beat + const stamp = tick * 8 + 8192 * mod(floor(time / 34), 512) + const banked = floor(clock.b * 32 + 0.5) + const carry = mod(banked, 16) + const seenSlot = floor(banked / 16) + const owed = carry + floor(dt * 960 + 0.5) + const due = min(floor(owed / 16), 3) + const isClock = cx < 0.5 && cy > G.y - 1.5 + if (turn > due - 0.5 && !(first || fresh)) { + // no step is due on this run: every cell stays as it is, and on the frame's last run the clock banks the remainder + const kept = sand.at(vec2(cx, cy)) + return isClock && turn > 1.5 ? vec4(kept.r, kept.g, (min(owed - due * 16, 15) + 16 * seenSlot) / 32, kept.a) : kept + } + // four block offsets in turn, so that every pair of neighbours shares a block at some step + const oy = mod(beat, 2) + const ox = floor(beat / 2) + const bx = floor((cx + ox) / 2) * 2 - ox + const by = floor((cy + oy) / 2) * 2 - oy + const qx = cx - bx + const qy = cy - by + const kindOf = (x: number, y: number, v: vec4): number => (standsAt(G, x, y) > 0.5 ? 9 : floor(v.r * 5 + 0.5)) + const vBL = sand.at(vec2(bx, by)) + const vBR = sand.at(vec2(bx + 1, by)) + const vTL = sand.at(vec2(bx, by + 1)) + const vTR = sand.at(vec2(bx + 1, by + 1)) + const kBL = kindOf(bx, by, vBL) + const kBR = kindOf(bx + 1, by, vBR) + const kTL = kindOf(bx, by + 1, vTL) + const kTR = kindOf(bx + 1, by + 1, vTR) + // cells beside and below the block: read to decide, never written from here + const beyond = (ix: number, iy: number): number => kindOf(bx + ix, by + iy, sand.at(vec2(bx + ix, by + iy))) + const eW0 = beyond(-1, 0) + const eE0 = beyond(2, 0) + const eW1 = beyond(-1, 1) + const eE1 = beyond(2, 1) + const eWd = beyond(-1, -1) + const eD0 = beyond(0, -1) + const eD1 = beyond(1, -1) + const eEd = beyond(2, -1) + const r1 = rnd(bx, by, stamp + 1) + const r2 = rnd(bx, by, stamp + 2) + const cellPts = size.x / G.x + + // A hovering pointer parts falling sand: near it a lone grain steps aside, away from the pointer. + // A pressed pointer on bare backdrop scares the crabs under and beside it: they turn away and run (scare is -1 for + // run left, 1 for run right). + let jitL = 0.04 + let jitR = 0.04 + let scare = 0 + for (const f of pointers) { + const hx = f.at.x / cellPts - (bx + 1) + const hy = f.at.y / cellPts - (by + 1) + if (f.down < 0.5 && f.gone < 0.001 && hx * hx + hy * hy < 60) { + jitL = hx < 0 ? 0.8 : 0 + jitR = hx < 0 ? 0 : 0.8 + } + if (f.down > 0.5 && f.over < 0.5 && f.gone < 0.001 && abs(hx) < 16 && hy > -3) scare = hx < 0 ? 1 : -1 + } + + const fallL0 = heavy(kTL) && air(kBL) + const fallR0 = heavy(kTR) && air(kBR) + const jL = fallL0 && grain(kTL) && air(kTR) && air(kBR) && r1 < jitL + const jR = fallR0 && grain(kTR) && air(kTL) && air(kBL) && r1 < jitR + const dLR0 = jL || (grain(kTL) && !air(kBL) && air(kTR) && air(kBR)) + const dRL0 = jR || (grain(kTR) && !air(kBR) && air(kTL) && air(kBL)) + const flat = !air(kBL) && !air(kBR) + const under = !air(eD0) && !air(eD1) + const open = air(kTL) && air(kTR) + const creep = 0.65 + const cTop0 = (grain(kTL) && air(kTR) && flat && air(eE0) && r2 < creep) || (grain(kTR) && air(kTL) && flat && air(eW0) && r2 < creep) + const cBot0 = (grain(kBL) && air(kBR) && open && under && air(eEd) && r2 < creep) || (grain(kBR) && air(kBL) && open && under && air(eWd) && r2 < creep) + + // Crabs walk in short bursts, and only in a block where no grain moves this step. Each crab rests now and then + // for a couple of seconds, by its own number. + const quiet = !(fallL0 || fallR0 || dLR0 || dRL0 || cTop0 || cBot0) + const walking = mod(tick, 24) < (abs(scare) > 0.5 ? 16 : 4) && quiet + const isCrab = (k: number): boolean => k > 1.5 && k < 3.5 + const ownNumber = isCrab(kBL) ? vBL.g : isCrab(kBR) ? vBR.g : isCrab(kTL) ? vTL.g : vTR.g + const awake = walking && (abs(scare) > 0.5 || rnd(ownNumber * 255, floor(time / 2.5), 31) < 0.72) + // a right walker, by where it stands in the block + const rBL = crabR(kBL) && awake + const rTL = crabR(kTL) && awake + const rBR = crabR(kBR) && awake + const rWalkBot = rBL && air(kBR) + const rClimb = rBL && !air(kBR) && open + const rDigL = rBL && !air(kBR) && grain(kTL) + const rDown = rTL && !air(kBL) && air(kTR) && air(kBR) + const rWalkTop = rTL && air(kTR) && !air(kBR) + const rHop = rBR && !air(eE0) && air(eE1) && air(kTR) + const rTurn = (rBR && !air(eE0) && !air(eE1)) || (rBL && !air(kBR) && !air(kTR) && !grain(kTL)) + const rActs = rWalkBot || rClimb || rDigL || rDown || rWalkTop || rHop + // a left walker, mirrored + const lBR = crabL(kBR) && awake && !rActs + const lTR = crabL(kTR) && awake && !rActs + const lBL = crabL(kBL) && awake && !rActs + const lWalkBot = lBR && air(kBL) + const lClimb = lBR && !air(kBL) && open + const lDigR = lBR && !air(kBL) && grain(kTR) + const lDown = lTR && !air(kBR) && air(kTL) && air(kBL) + const lWalkTop = lTR && air(kTL) && !air(kBL) + const lHop = lBL && !air(eW0) && air(eW1) && air(kTL) + const lTurn = (lBL && !air(eW0) && !air(eW1)) || (lBR && !air(kBL) && !air(kTL) && !grain(kTR)) + + // dust drifts on the wind where nothing else moves: mostly sideways, sometimes up + const calm = quiet && !(rActs || lWalkBot || lClimb || lDigR || lDown || lWalkTop || lHop) + const gust = 0.18 + 0.15 * sin(time * 0.13) + const dTop = calm && r1 < gust && mote(kTL) && kTR < 0.5 + const dBot = calm && r1 < gust && mote(kBL) && kBR < 0.5 && !dTop + const dUpL = calm && r1 > 0.9 && mote(kBL) && kTL < 0.5 + const dUpR = calm && r1 > 0.9 && mote(kBR) && kTR < 0.5 + + // a crab in mid air steps onto the ledge beside it instead of falling back + const fallL = fallL0 && !jL && !(rWalkTop && crabR(kTL)) + const fallR = fallR0 && !jR && !(lWalkTop && crabL(kTR)) + const upL = rDigL || lHop || dUpL + const upR = lDigR || rHop || dUpR + const dLR = dLR0 || rDown || lClimb + const dRL = dRL0 || lDown || rClimb + const cTop = cTop0 || rWalkTop || lWalkTop || dTop + const cBot = cBot0 || rWalkBot || lWalkBot || dBot + + const vert = (qx < 0.5 && (fallL || upL)) || (qx > 0.5 && (fallR || upR)) + const diag = (dLR && abs(qx - qy) > 0.5) || (dRL && abs(qx - qy) < 0.5) + const hori = (cTop && qy > 0.5) || (cBot && qy < 0.5) + let sx = qx + let sy = qy + if (vert) sy = 1 - sy + else if (diag) { + sx = 1 - sx + sy = 1 - sy + } else if (hori) sx = 1 - sx + const moved = vert || diag || hori + const lo = sx < 0.5 ? vBL : vBR + const hi = sx < 0.5 ? vTL : vTR + const src = sy < 0.5 ? lo : hi + const kLo = sx < 0.5 ? kBL : kBR + const kHi = sx < 0.5 ? kTL : kTR + let k = sy < 0.5 ? kLo : kHi + // Lane b remembers motion: 1 for a fall, a little less for a slide, fading over about a tenth of a second. The art + // draws a grain in motion in a quiet tone, so sand on the move never makes a cell blink. + const stir = moved && grain(k) ? (vert || jL || jR ? 1 : 0.85) : max(src.b - 0.12, 0) + let oR = src.r + let oG = src.g + let oB = stir + let oA = src.a + + // a crab turns round where it stands + const turnHere = (rTurn && ((qx > 0.5 && qy < 0.5 && crabR(kBR)) || (qx < 0.5 && qy < 0.5 && crabR(kBL)))) || (lTurn && ((qx < 0.5 && qy < 0.5 && crabL(kBL)) || (qx > 0.5 && qy < 0.5 && crabL(kBR)))) + const kHere = qy < 0.5 ? (qx < 0.5 ? kBL : kBR) : qx < 0.5 ? kTL : kTR + const flee = (crabR(kHere) && scare < -0.5) || (crabL(kHere) && scare > 0.5) + if ((turnHere || flee) && !moved) { + k = 5 - k + oR = k * 0.2 + } + // crabs leave at the window's sides and tire after a few minutes + if (k > 1.5 && k < 3.5) { + if (mod(tick, 64) < 0.5) oA = oA - 0.004 + if (cx < 0.5 || cx > G.x - 1.5 || oA < 0.01) k = 0 + } + + const me = standsAt(G, cx, cy) + if (me > 0) k = 9 + + // Wind takes resting grains from the top of a heap, so the page returns to calm: hardly at all up among the + // shelves, slowly on the message box, faster over the dunes the higher a mound stands, and much more slowly for + // sand a person poured (lane a is 0.5 for it). + const kUp = qx < 0.5 ? kTL : kTR + if (qy < 0.5 && !moved && grain(k) && air(kUp) && rnd(cx, cy, stamp + 5) < (cy >= (cx < G.x * 0.5 ? W.a4 : W.b3) - 4 ? 0.0004 : oA < 0.75 ? 0.001 : W.known && cx >= W.cl && cx < W.cr ? 0.003 : 0.006 * (1 + (cy - W.dune(cx)) / 5))) { + k = 4 + oR = 0.8 + oB = 0 + oA = 1 + } + // a mote thins out and is gone in a few seconds + if (mote(k)) { + oA = oA - 0.006 + if (oA < 0.02) k = 0 + } + + if (k < 0.5 || k > 8) { + oR = 0 + oG = 0 + oB = 0 + oA = 0 + } + + // New things. Grains and dust are born on every second step (the two falls of sand on every fourth): from the falls, + // the nozzles and pressed pointers, and as puffs. A crab comes in at a side every seven seconds. + if (k < 0.5) { + const even = mod(beat, 2) < 0.5 + const r3 = rnd(cx, cy, stamp + 3) + const r4 = rnd(cx, cy, stamp + 4) + const flowL = clamp(0.6 + 0.55 * sin(time * 0.21) + 0.25 * sin(time * 0.53 + 1), 0, 1) + const flowR = clamp(0.6 + 0.55 * sin(time * 0.17 + 2) + 0.25 * sin(time * 0.47 + 4), 0, 1) + let born = 0 + let poured = 0 + let hue = time * 0.09 + if (abs(cy - (G.y - 2)) < 0.5 && beat < 0.5) { + if (abs(cx - W.sL) < 0.5 && r3 < flowL) born = 1 + if (abs(cx - W.sR) < 0.5 && r3 < flowR) born = 1 + } + // the nozzles trickle + if (even && abs(cx - (W.pL + 5)) < 0.5 && abs(cy - W.spL) < 0.5 && r3 < 0.06 + 0.2 * flowR) born = 1 + if (even && abs(cx - (W.pR - 1)) < 0.5 && abs(cy - W.upR) < 0.5 && r3 < 0.06 + 0.2 * flowL) born = 1 + for (const f of pointers) { + const w = even ? f.down * (1 - f.over) * (f.gone > 0 ? 0 : 1) : 0 + // The stroke the pointer drew since the last frame, in cells: where it began, how long it is, which way it heads. + // This cell is then some way along the stroke and some way off to its side. + const fromX = f.was.x / cellPts + const fromY = f.was.y / cellPts + const runX = f.at.x / cellPts - fromX + const runY = f.at.y / cellPts - fromY + const reach = sqrt(runX * runX + runY * runY) + const perCell = 1 / max(reach, 0.01) + const offX = cx + 0.5 - fromX + const offY = cy + 0.5 - fromY + const along = (offX * runX + offY * runY) * perCell + const aside = abs(offX * runY - offY * runX) * perCell + const past = max(max(-along, along - reach), 0) + // a pointer that has not moved is a dot, not a stroke + const away = reach < 0.01 ? sqrt(offX * offX + offY * offY) : sqrt(aside * aside + past * past) + // the faster the stroke, the less it pours: a hard scrub only scatters a few grains + const chance = (w * (away < 0.8 ? 0.6 : 0.2)) / (1 + 0.35 * reach * reach) + if (away < 1.6 && r3 < chance) { + born = 1 + poured = 1 + hue = time * 0.3 + (time - f.age) * 0.77 + } + } + // a crab comes in from a side now and then + const slot = floor((time + 3) / 7) + const pick = rnd(slot, 5, 41) + if (abs(mod(slot, 2) - seenSlot) > 0.5) { + if (pick < 0.5 && abs(cx - 1) < 0.5 && abs(cy - W.dune(1) - 1) < 0.5) born = 2 + if (pick > 0.5 && abs(cx - (G.x - 2)) < 0.5 && abs(cy - W.dune(G.x - 2) - 1) < 0.5) born = 3 + } + // a puff of dust beside a grain that has just come to rest + const nL = sand.at(vec2(cx - 1, cy)) + const nR = sand.at(vec2(cx + 1, cy)) + const landedL = nL.r > 0.1 && nL.r < 0.3 && nL.b > 0.93 && !air(kindOf(cx - 1, cy - 1, sand.at(vec2(cx - 1, cy - 1)))) + const landedR = nR.r > 0.1 && nR.r < 0.3 && nR.b > 0.93 && !air(kindOf(cx + 1, cy - 1, sand.at(vec2(cx + 1, cy - 1)))) + if (even && born < 0.5 && (landedL || landedR) && r4 > 0.9) { + born = 4 + hue = landedL ? nL.g : nR.g + } + if (born > 0.5) { + oR = born * 0.2 + oG = born < 1.5 ? (floor(mod(hue, 4)) + r4) / 4 : born > 3.5 ? hue : r4 + oB = born < 1.5 ? 1 : 0 + oA = born > 3.5 ? 0.6 : born < 1.5 && poured > 0.5 ? 0.5 : 1 + } + } + + if (first || fresh) { + oR = 0 + oG = 0 + oB = 0 + oA = 0 + const r5 = rnd(cx, cy, 77) + const strata = (floor(mod(cy * 0.5 + cx * 0.13 + r5 * 0.8, 4)) + r5) / 4 + const on = (top: number, h: number): boolean => cy >= top && cy < top + h + let heap = false + if (cx < W.pL) { + const hE = min(10, floor((W.eL - 1 - cx) / 2)) + const hP = floor((cx - W.fL) / 2) + heap = (cx < W.eL && (on(W.a1, hE) || on(W.a3, hE))) || (cx >= W.fL && (on(W.a2, hP) || on(W.a4, hP))) + } else if (cx >= W.pR + 5) { + const hE = min(10, floor((cx - W.eR) / 2)) + const hP = floor((W.fR - 1 - cx) / 2) + heap = (cx >= W.eR && (on(W.b1, hE) || on(W.b3, hE))) || (cx < W.fR && on(W.b2, hP)) + } + if (me < 1 && heap) { + oR = 0.2 + oG = strata + oA = 1 + } + // two crabs on each side to begin with, one grown and one young, all walking inward + const crabAt = (at: number, kind: number, size: number) => { + if (abs(cx - at) < 0.5 && abs(cy - W.dune(at)) < 0.5) { + oR = kind * 0.2 + oG = size + oA = 1 + } + } + crabAt(floor(W.mL * 0.22), 2, 0.8) + crabAt(floor(W.mL * 0.07) + 1, 2, 0.2) + crabAt(G.x - floor(W.mR * 0.24), 3, 0.7) + crabAt(G.x - floor(W.mR * 0.06) - 2, 3, 0.3) + } + if (isClock) { + const fresh0 = first || fresh + oR = 0 + const nowSlot = mod(floor((time + 3) / 7), 2) + oB = fresh0 ? 0 : ((turn > 1.5 ? owed - 48 : carry) + 16 * nowSlot) / 32 + oA = fresh0 ? 0 : mod(beat + 1, 4) / 4 + oG = fresh0 ? 0 : mod(floor(clock.g * 255 + 0.5) + (beat > 2.5 ? 1 : 0), 256) / 255 + } + return vec4(oR, oG, oB, oA) +}) + +// A hand-made 4 by 4 threshold order that fills along diagonals, so shading comes out as a slanted hatch. +const hatch = (x: number, y: number): number => { + const u = mod(x - y, 4) + const a = u < 0.5 ? 0 : u < 1.5 ? 8 : u < 2.5 ? 12 : 4 + return (a + mod(y, 4) + 0.5) / 16 +} + +// The crab, 9 by 6, two bits a cell: 1 shell, 2 dark, 3 light. Rows from the feet up, two poses. +const crabRow = (j: number, pose: number): number => { + const a = j < 0.5 ? 139298 : j < 1.5 ? 38232 : j < 2.5 ? 21844 : j < 3.5 ? 87381 : j < 4.5 ? 73761 : 12336 + const b = j < 0.5 ? 34952 : j < 1.5 ? 38232 : j < 2.5 ? 87381 : j < 3.5 ? 87381 : j < 4.5 ? 8224 : 12336 + return pose < 0.5 ? a : b +} +// A young crab, 5 by 3 inside the same 9 columns. +const smallRow = (j: number, pose: number): number => { + const a = j < 0.5 ? 2176 : j < 1.5 ? 5456 : j < 2.5 ? 3264 : 0 + const b = j < 0.5 ? 8736 : j < 1.5 ? 5456 : j < 2.5 ? 3264 : 0 + return pose < 0.5 ? a : b +} + +// The gecko on the right pillar, 5 by 9, tail first, two poses. +const geckoRow = (j: number, pose: number): number => { + const a = j < 0.5 ? 64 : j < 1.5 ? 16 : j < 2.5 ? 530 : j < 3.5 ? 152 : j < 4.5 ? 84 : j < 5.5 ? 152 : j < 6.5 ? 530 : j < 7.5 ? 220 : 84 + const b = j < 0.5 ? 4 : j < 1.5 ? 16 : j < 2.5 ? 152 : j < 3.5 ? 530 : j < 4.5 ? 84 : j < 5.5 ? 530 : j < 6.5 ? 152 : j < 7.5 ? 220 : 84 + return pose < 0.5 ? a : b +} + +const art = buffer({ scale: 1 / CELL, filter: 'linear' }, p => { + const G = res + const W = world(G) + const x = p.cell.x + const y = p.cell.y + // light or dark: the Svg's middle is bare sky in the shown mode + const m = step(0.45, dot(scene.sample(vec2(0.5, 0.5)).rgb, vec3(0.3, 0.6, 0.1))) + const ink = (d: number, l: number): number => mix(d, l, m) + const v = sand.at(vec2(x, y)) + const k = floor(v.r * 5 + 0.5) + const w = standsAt(G, x, y) + const th = hatch(x, y) + + // sky: two flat tones meeting in a hatched seam, a far line of dunes, a few specks + const seam = G.y * 0.34 + let col = ink(vec3(0.11, 0.08, 0.14), vec3(0.96, 0.92, 0.84)) + if (th < clamp((seam - y) / 16, 0, 1)) col = ink(vec3(0.15, 0.1, 0.17), vec3(0.94, 0.88, 0.78)) + const far = 13 + 5 * sin(x * 0.045 + 2) + 2 * sin(x * 0.11) + if (y < far - 1 || (y < far && th < 0.5)) col = ink(vec3(0.19, 0.12, 0.18), vec3(0.92, 0.85, 0.74)) + if (y > seam && rnd(x, y, 9) < 0.004) col = ink(vec3(0.33, 0.27, 0.36), vec3(0.9, 0.83, 0.71)) + + // a big quiet disc between the left pillar and the text, three quarters of the way up: the moon by night, the sun by day + const discR = min(12, (W.cl - W.pL - 5) * 0.42) + const dxs = x + 0.5 - (W.pL + 5 + (W.cl - W.pL - 5) * 0.5) + const dys = y + 0.5 - G.y * 0.72 + const dd = dxs * dxs + dys * dys + if (dd < discR * discR && (dd < (discR - 1.5) * (discR - 1.5) || th < 0.5) && (dxs - dys < discR * 0.35 || th < 0.6)) col = ink(vec3(0.2, 0.15, 0.23), vec3(0.99, 0.96, 0.9)) + + // stone: the pillars and the shelves, a quiet tone near the sky's, lit from the upper left + const stone = ink(vec3(0.23, 0.17, 0.24), vec3(0.84, 0.76, 0.66)) + const stoneLit = ink(vec3(0.31, 0.24, 0.31), vec3(0.9, 0.83, 0.74)) + const stoneDim = ink(vec3(0.16, 0.11, 0.17), vec3(0.75, 0.66, 0.56)) + if (w > 5.5) { + const d = x < G.x * 0.5 ? x - W.pL : x - W.pR + col = d < 0.5 ? stoneLit : d > 3.5 ? stoneDim : stone + // drum joints, a chipped top, a few pits, and a sign carved into most drums (3 by 5 shapes of its own) + const drum = floor(y / 11) + const inDrum = y - drum * 11 + if (inDrum < 0.5 || standsAt(G, x, y + 1) < 5.5) col = d > 3.5 ? stone : stoneDim + if (rnd(x, y, 13) < 0.03 && d > 0.5) col = stoneDim + const sign = floor(rnd(drum, 1, x < G.x * 0.5 ? 51 : 52) * 9) + const carved = sign < 0.5 ? 25814 : sign < 1.5 ? 13715 : sign < 2.5 ? 17492 : sign < 3.5 ? 5393 : sign < 4.5 ? 18569 : sign < 5.5 ? 3640 : sign < 6.5 ? 1041 : 0 + if (d > 0.5 && d < 3.5 && inDrum > 2.5 && inDrum < 7.5 && unsigned(int((int(carved) >>> int((inDrum - 3) * 3 + d - 1)) & 1)) > 0.5) col = stoneDim + } + if (w > 3.5 && w < 4.5) { + col = stone + const roof = standsAt(G, x, y + 1) < 3.5 || standsAt(G, x, y + 1) > 4.5 + const foot = standsAt(G, x, y - 1) < 3.5 || standsAt(G, x, y - 1) > 4.5 + if (roof && foot) col = ink(vec3(0.5, 0.36, 0.24), vec3(0.72, 0.56, 0.4)) + else if (roof) col = stoneLit + else if (standsAt(G, x, y - 1) < 3.5 || standsAt(G, x, y - 1) > 4.5 || mod(x * 3 + y * 5, 11) < 0.5) col = stoneDim + } + // stone brackets under the shelves (drawn only) + if (w < 0.5) { + const under = (top: number): number => (y < top - 3 && y >= top - 6 ? top - 3 - y : 9) + const eL = min(under(W.a1), under(W.a3)) + const iL = min(under(W.a2), under(W.a4)) + const eR = min(under(W.b1), under(W.b3)) + const iR = under(W.b2) + if ((x < 4 - eL && x < W.eL) || (x >= W.pL - 4 + iL && x < W.pL && x >= W.fL) || (x >= G.x - 4 + eR && x >= W.eR) || (x < W.pR + 9 - iR && x >= W.pR + 5 && x < W.fR)) col = stoneDim + } + // wooden chutes, and the nozzles that feed them + if (w > 4.5 && w < 5.5) { + col = ink(vec3(0.36, 0.24, 0.2), vec3(0.66, 0.5, 0.37)) + if (standsAt(G, x, y + 1) > 4.5 || mod(x, 6) < 0.5) col = ink(vec3(0.24, 0.16, 0.16), vec3(0.52, 0.38, 0.29)) + } + const nozzleL = abs(x - (W.pL + 5)) < 0.5 && abs(y - W.spL - 1) < 1 + const nozzleR = abs(x - (W.pR - 1)) < 0.5 && abs(y - W.upR - 1) < 1 + if ((nozzleL || nozzleR) && w < 0.5) col = y > (nozzleL ? W.spL : W.upR) + 0.5 ? stoneLit : stoneDim + + // the dunes along the bottom: old sand, hatched darker with depth, a lit crest + if (w > 1.5 && w < 2.5) { + const depth = W.dune(x) - 1 - y + col = ink(vec3(0.45, 0.29, 0.19), vec3(0.86, 0.72, 0.52)) + if (depth < 0.5) col = ink(vec3(0.62, 0.42, 0.22), vec3(0.92, 0.8, 0.61)) + else if (th < clamp((depth - 1) / 7, 0, 0.85)) col = ink(vec3(0.34, 0.21, 0.16), vec3(0.78, 0.62, 0.42)) + } + + // things that grow, drawn only on bare sky: vines hang under the shelves and sway a cell, a cactus stands on each side + if (w < 0.5) { + const green = ink(vec3(0.22, 0.3, 0.24), vec3(0.55, 0.6, 0.42)) + // how far this cell hangs under a shelf, and which shelf (its top row tells the vines of one shelf from another's) + let hang = 99 + let shelf = 0 + const under = (top: number, spans: boolean) => { + if (spans && y < top - 3 && y >= top - 13) { + hang = top - 3 - y + shelf = top + } + } + under(W.a1, x < W.eL) + under(W.a3, x < W.eL) + under(W.a2, x >= W.fL && x < W.pL) + under(W.a4, x >= W.fL && x < W.pL) + under(W.b1, x >= W.eR) + under(W.b3, x >= W.eR) + under(W.b2, x >= W.pR + 5 && x < W.fR) + if (hang < 50) { + for (const s of [-1, 0, 1]) { + const root = x - s + const len = 2 + floor(9 * rnd(root, shelf, 23)) + const lean = floor(0.5 + 0.9 * sin(time * 0.7 + root * 1.7) * (hang / 9)) + if (rnd(root, shelf, 29) < 0.16 && hang <= len && abs(lean - s) < 0.5) col = mod(hang + root, 3) < 0.5 ? ink(vec3(0.3, 0.4, 0.3), vec3(0.44, 0.52, 0.36)) : green + } + } + const cxl = floor(W.pL * 0.78) + const cxr = G.x - 5 + const plantAt = x < G.x * 0.5 ? cxl : cxr + const px = x - plantAt + 2 + const py = y - W.dune(plantAt) + const prow = py < 0.5 ? 16 : py < 1.5 ? 16 : py < 2.5 ? 21 : py < 3.5 ? 337 : py < 4.5 ? 273 : py < 5.5 ? 272 : 16 + if (px > -0.5 && px < 4.5 && py > -0.5 && py < 6.5 && unsigned(int((int(prow) >>> int(px * 2)) & 3)) > 0.5) col = px > 2.5 ? ink(vec3(0.17, 0.24, 0.2), vec3(0.45, 0.5, 0.35)) : green + } + + // dry tufts on the dune crests (drawn only, sand buries them) + const crest = W.dune(x) + if (w < 0.5 && rnd(x, 2, 17) < 0.07 && y >= crest && y < crest + 1 + floor(2 * rnd(x, 4, 17))) col = ink(vec3(0.3, 0.24, 0.16), vec3(0.66, 0.56, 0.38)) + + // a gecko goes up and down the right pillar, a cell at a time, and rests at each end + const lap = fract(time / 64) + const climb = clamp(abs(lap * 2 - 1) * 1.5 - 0.25, 0, 1) + const gLow = W.spR + 6 + const gAt = floor(gLow + (W.topR - 16 - gLow) * climb) + const gx = x - W.pR + const gy0 = y - gAt + const gy = lap < 0.5 ? 8 - gy0 : gy0 + let gecko = 0 + if (gx > -0.5 && gx < 4.5 && gy0 > -0.5 && gy0 < 8.5) gecko = unsigned(int((int(geckoRow(gy, mod(gAt + floor(time * 0.4), 2))) >>> int(gx * 2)) & 3)) + + // grains + const kAt = (ix: number, iy: number): number => { + const n = sand.at(vec2(x + ix, y + iy)) + return standsAt(G, x + ix, y + iy) > 0 ? 9 : floor(n.r * 5 + 0.5) + } + const tone = (g: number): number => { + const ci = floor(g * 4) + const d = ci < 0.5 ? vec3(0.89, 0.65, 0.29) : ci < 1.5 ? vec3(0.79, 0.52, 0.23) : ci < 2.5 ? vec3(0.65, 0.35, 0.2) : vec3(0.95, 0.86, 0.68) + const l = ci < 0.5 ? vec3(0.77, 0.44, 0.24) : ci < 1.5 ? vec3(0.83, 0.6, 0.25) : ci < 2.5 ? vec3(0.56, 0.35, 0.24) : vec3(0.86, 0.66, 0.51) + return ink(d, l) + } + // A grain in motion wears a quiet tone of its colour: close to the sky in brightness (about 0.15 of linear + // luminance away, whatever the colour), and told from it by hue. It takes its full colour once it has lain still + // for a moment. That keeps every fall, pour and slide from making the cells it crosses blink. + const quiet = (g: number): number => { + const ci = floor(g * 4) + const d = ci < 0.5 ? vec3(0.62, 0.38, 0.1) : ci < 1.5 ? vec3(0.58, 0.4, 0.14) : ci < 2.5 ? vec3(0.58, 0.32, 0.18) : vec3(0.5, 0.43, 0.28) + const l = ci < 0.5 ? vec3(1, 0.81, 0.64) : ci < 1.5 ? vec3(0.98, 0.84, 0.5) : ci < 2.5 ? vec3(0.94, 0.82, 0.76) : vec3(1, 0.8, 0.74) + return ink(d, l) + } + const ground = col + const below = sand.at(vec2(x, y - 1)) + if (w < 0.5 && grain(k)) { + const base = tone(v.g) + const up = kAt(0, 1) + const lf = kAt(-1, 0) + const rt = kAt(1, 0) + const up2 = kAt(0, 2) + const glint = ink(vec3(1, 0.93, 0.76), vec3(1, 0.95, 0.85)) + const murk = ink(vec3(0.2, 0.1, 0.12), vec3(0.42, 0.24, 0.18)) + col = mix(murk, base, 0.95) + if (v.b > 0.3) col = quiet(v.g) + else if (air(up)) col = ink(mix(base, glint, 0.38), mix(murk, base, 0.86)) + else if (air(lf)) col = ink(mix(base, glint, 0.2), mix(murk, base, 0.8)) + else if (air(rt)) col = mix(murk, base, ink(0.74, 0.62)) + else if (fract(v.g * 4) > 0.97) col = mix(base, glint, ink(0.45, 0.3)) + else if (!air(up2) && th < 0.19) col = mix(murk, base, 0.86) + } else if (w < 0.5 && mote(k)) { + col = mix(ground, quiet(v.g), v.a > 0.5 ? 0.8 : 0.45) + } else if (w < 0.5 && air(k)) { + // the tail of a falling grain, so a steady pour reads as a ribbon and not as blinking dots + const low = sand.at(vec2(x, y - 2)) + const lower = sand.at(vec2(x, y - 3)) + if (below.r > 0.1 && below.r < 0.3 && below.b > 0.93) col = mix(ground, quiet(below.g), 0.85) + else if (low.r > 0.1 && low.r < 0.3 && low.b > 0.93 && below.r < 0.1) col = mix(ground, quiet(low.g), 0.65) + else if (lower.r > 0.1 && lower.r < 0.3 && lower.b > 0.93 && below.r < 0.1 && low.r < 0.1) col = mix(ground, quiet(lower.g), 0.45) + } + + if (gecko > 0.5 && !grain(k)) col = gecko < 1.5 ? ink(vec3(0.2, 0.6, 0.55), vec3(0.13, 0.48, 0.45)) : gecko < 2.5 ? ink(vec3(0.09, 0.33, 0.35), vec3(0.08, 0.3, 0.3)) : ink(vec3(0.95, 0.86, 0.68), vec3(1, 0.96, 0.88)) + + // crabs, drawn over everything: look for a crab's foot cell under and beside this cell + // (two crabs may stand close enough for their pictures to overlap, so two are remembered) + let kindA = 0 + let acrossA = 0 + let rowA = 0 + let ownA = 0 + let kindB = 0 + let acrossB = 0 + let rowB = 0 + let ownB = 0 + for (let j = 0; j < 6; j++) { + // (a read below the grid gives the bottom row again, so a crab standing on the bottom row would be found once + // more for every row read below it; rows below the grid are not counted) + const inGrid = y - j >= 0 + for (let i = 0; i < 9; i++) { + const n = sand.at(vec2(x + i - 4, y - j)) + const nk = floor(n.r * 5 + 0.5) + if (inGrid && nk > 1.5 && nk < 3.5) { + if (kindA < 0.5) { + kindA = nk + acrossA = i + rowA = j + ownA = n.g + } else { + kindB = nk + acrossB = i + rowB = j + ownB = n.g + } + } + } + } + // which cell of a crab's picture this is: a right walker's is mirrored, and the legs change with each step + const crabCell = (kind: number, across: number, row: number, own: number): number => { + const pose = mod(x + across - 4 + floor(time * 1.5), 2) + const col9 = kind < 2.5 ? 8 - across : across + return kind > 0.5 ? unsigned(int((int(own < 0.4 ? smallRow(row, pose) : crabRow(row, pose)) >>> int(col9 * 2)) & 3)) : 0 + } + const cellA = crabCell(kindA, acrossA, rowA, ownA) + const cellB = crabCell(kindB, acrossB, rowB, ownB) + const code = cellB > 0.5 ? cellB : cellA + const hitRow = cellB > 0.5 ? rowB : rowA + if (code > 0.5) { + col = code < 1.5 ? ink(vec3(0.25, 0.72, 0.66), vec3(0.16, 0.57, 0.53)) : code < 2.5 ? ink(vec3(0.07, 0.27, 0.3), vec3(0.07, 0.23, 0.24)) : ink(vec3(0.95, 0.86, 0.68), vec3(1, 0.96, 0.88)) + if (code < 1.5 && hitRow < 1.5) col = ink(vec3(0.16, 0.55, 0.52), vec3(0.11, 0.44, 0.42)) + } + return vec4(col, 1) +}) + +const view = picture(p => { + // Enlarge the art with crisp cell edges. A pixel reads the middle of its cell, which is that cell's pure colour, + // except within half a picture pixel of the cell's edge, where it slides toward the neighbour so the edge is one + // picture pixel soft at any sharpness. + const spot = p.uv * art.size + const cell = floor(spot) + const inCell = spot - cell + const crisp = size.x / (art.size.x * p.px) + const towardLow = clamp(inCell * crisp - 0.5, -0.5, 0) + const towardHigh = clamp((inCell - 1) * crisp + 0.5, 0, 0.5) + return vec4(art.sample((cell + 0.5 + towardLow + towardHigh) / art.size).rgb, 1) +}) + +export default shader({ fps: 60, passes: [fixed, sand, art, view] }) diff --git a/plugins/theme-examples/hooks/scenes/falling-sand/still.ts b/plugins/theme-examples/hooks/scenes/falling-sand/still.ts new file mode 100644 index 00000000..7f000172 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/falling-sand/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Falling Sand, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function fallingSandSvg(): string { + return ( + "Falling SandPixel-art sand pouring down stone shelves onto low dunes, with a few small crabs.\n" + ) +} diff --git a/plugins/theme-examples/hooks/scenes/field-lines/scene.ts b/plugins/theme-examples/hooks/scenes/field-lines/scene.ts new file mode 100644 index 00000000..7168674b --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/field-lines/scene.ts @@ -0,0 +1,249 @@ +// Field Lines. Technical pencil on lab graph paper; chalk on slate in the dark. +// One pencil dash in every paper square turns along a magnetic field. Two poles wander in the side margins, +// plotting compasses sit at the sheet's edges, and graphite dust gathers and creeps where the field is strong. +// Every pointer is one more pole. A pointer lights nothing: it adds a thin ring under itself, draws dust +// to itself and presses nearby dashes a little harder; everything else only turns, lengthens and shifts. +import { + PI, abs, clamp, dot, floor, fract, int, length, max, min, mix, picture, pointers, range, + scene, shader, sin, size, smoothstep, time, unsigned, vec2, vec3, vec4, +} from 'claude-code/shader' + +// One paper square, in points. Rules run every square, heavier every fifth. +const G = 28 +// Where the rules start, so that a 1512x945 window's middle falls in the middle of a square. +const OFF_X = 14 +const OFF_Y = 10.5 +// A pole is softened so the field stays finite on top of it (points squared). +const SOFT = 500 +// How far a pole's field reaches: a lone pole's reads as a little under half strength at this many points. +const NEAR = 160 + +// The scene's own integer stir: two lanes folded into each other, then one value out. +const stir = (ix: number, iy: number) => { + let a = int(int(ix) * 0x2f6b4a9d + 0x51a3) + const b = int((int(iy) * 0x6d1c85e3) ^ (a >>> 9)) + a = int((a ^ (b << 5)) * 0x19e4b7a5) + a = int(a ^ (a >>> 14)) + a = int((a + b) * 0x4b3d92c7) + return int(a ^ (a >>> 17)) +} +// A fresh word from an old one, for the next handful of bits. +const again = (h: number) => { + const a = int((h ^ (h >>> 12)) * 0x35a9c6d3 + 0x2b71) + return int(a ^ (a >>> 15)) +} +// Some bits of a word as a number from 0 up to, not including, 1. +const slice = (h: number, shift: number, mask: number) => + unsigned(int((h >>> shift) & mask)) / (mask + 1) + +// Distance from q to a straight stroke, given by its middle, its way (a unit vector) and half its length. +// Worked in the stroke's own two axes, the same way a dash is drawn below. +const toStroke = (q: vec2, mid: vec2, way: vec2, halfLong: number) => { + const d = vec2(q - mid) + return length(vec2(max(abs(dot(d, way)) - halfLong, 0), d.x * way.y - d.y * way.x)) +} +// How strong a field of this size reads, 0 far from every pole to 1 beside one. +const grade = (pull: number) => clamp((pull * NEAR / (pull * NEAR + 1) - 0.3) / 0.45, 0, 1) + +const sheet = picture(p => { + const at = p.point + const perPixel = 1 / p.px + // How much of a pixel a mark covers: gap is the distance to its middle line, half its half width. + // A mark thinner than a pixel is drawn a pixel wide and fainter, so it never breaks into dots. + const cover = (gap: number, half: number) => { + const wide = max(half, 0.5 * p.px) + return clamp((wide - gap) * perPixel + 0.5, 0, 1) * min(1, 2 * half * perPixel) + } + // The same for a filled shape: edge is the distance outside it. + const inside = (edge: number) => clamp(0.5 - edge * perPixel, 0, 1) + // Evenly spaced hatching: the distance to the nearest of a family of parallel strokes. + const hatch = (v: number, every: number) => abs(fract(v / every + 0.5) - 0.5) * every + const between = (v: number, low: number, high: number) => (v > low && v < high ? 1 : 0) + + // Light or dark comes from the still picture's paper, read at the window's middle where it is kept bare. + const under = scene.sample(vec2(0.5, 0.5)) + const dark = 1 - smoothstep(0.3, 0.6, dot(vec3(under.rgb), vec3(0.3, 0.6, 0.1))) + const paper = vec3(mix(vec3(0.953, 0.945, 0.906), vec3(0.133, 0.153, 0.165), dark)) + const ruled = vec3(mix(vec3(0.56, 0.73, 0.9), vec3(0.25, 0.32, 0.37), dark)) + const lead = vec3(mix(vec3(0.27, 0.28, 0.33), vec3(0.91, 0.9, 0.86), dark)) + const redLead = vec3(mix(vec3(0.79, 0.23, 0.16), vec3(0.92, 0.45, 0.37), dark)) + + // Paper tooth: one faint grain, also what breaks the graphite. + const tooth = slice(stir(floor(at.x / 1.5) + 11, floor(at.y / 1.5) + 5), 3, 255) + + // Printed rules. + const fine = max(cover(hatch(at.x - OFF_X, G), 0.4), cover(hatch(at.y - OFF_Y, G), 0.4)) + const bold = max(cover(hatch(at.x - OFF_X, 5 * G), 0.55), cover(hatch(at.y - OFF_Y, 5 * G), 0.55)) + + // The two wandering poles, each in a lane of its own side margin. In a narrow window the lane is held + // clear of the compasses' lane at the edge (a compass case and a pole's ring, with a little room). + const edgeLane = max(0.06 * size.x, 56) + 70 + const northAt = vec2( + max(size.x * (0.155 + 0.04 * sin(0.043 * time + 0.6)), edgeLane), + size.y * (0.52 + 0.31 * sin(0.031 * time + 1.1)), + ) + const southAt = vec2( + min(size.x * (0.845 + 0.04 * sin(0.037 * time + 2.4)), size.x - edgeLane), + size.y * (0.46 + 0.31 * sin(0.027 * time + 4)), + ) + + // Plotting compasses, each in the middle of a paper square, in a lane outside the poles' own, + // and never so near the window's edge that the case is cut. + const seatX = (share: number) => (floor((clamp(size.x * share, 56, size.x - 56) - OFF_X) / G) + 0.5) * G + OFF_X + const seatY = (share: number) => (floor((clamp(size.y * share, 56, size.y - 56) - OFF_Y) / G) + 0.5) * G + OFF_Y + const hubs = [ + vec2(seatX(0.04), seatY(0.82)), + vec2(seatX(0.06), seatY(0.47)), + vec2(seatX(0.04), seatY(0.14)), + vec2(seatX(0.94), seatY(0.9)), + vec2(seatX(0.96), seatY(0.6)), + vec2(seatX(0.94), seatY(0.25)), + ] + const square = vec2(floor((at.x - OFF_X) / G), floor((at.y - OFF_Y) / G)) + const middle = vec2((square.x + 0.5) * G + OFF_X, (square.y + 0.5) * G + OFF_Y) + // Where the field is read: the square's middle, or the compass's pivot inside a compass's nine squares. + let hub = middle + let onHub = 0 + let hubNo = 0 + for (const n of range(6)) { + const d = vec2(at - hubs[n]!) + if (max(abs(d.x), abs(d.y)) < 1.5 * G) { + hub = hubs[n]! + onHub = 1 + hubNo = n + } + } + + // The field at that place: each pole pushes along the line from itself, falling off with distance. + let field = vec2(0) + const pole = (where: vec2, charge: number) => { + const d = vec2(hub - where) + field = vec2(field + d * (charge / (dot(d, d) + SOFT))) + } + pole(northAt, 1) + pole(southAt, -1) + const calm = grade(length(field)) + // Pointers: a hover is a north pole, a press a south pole, and each is turned over once for every + // earlier pointer still there. Slots are in arrival order, so fingers alternate, and because the + // turning is weighted by the earlier pointer's fade, nothing jumps when a faded entry leaves its slot. + // (The list holds four. A fifth entry inside half a second of a lift pushes a fading one out early, + // and the dashes round it then step once.) + let turned = 1 + let ringLead = 0 + let ringRed = 0 + for (const finger of pointers) { + const here = clamp(finger.age / 0.35, 0, 1) * (1 - clamp(finger.gone / 0.5, 0, 1)) + const charge = (1 - 2 * finger.down) * turned + // A pole flung across the paper is gone before the needles can follow: its pull is halved at 84 points + // a frame (about 2500 a second at 30 frames a second) and keeps falling, so a hard scrub stirs the sheet + // only a little. Measured by the frame and not by dt, which the engine smooths and which lags after + // slow frames. + const rush = length(vec2(finger.at - finger.was)) / 84 + const pull = here * mix(1.4, 0.5, finger.over) / (1 + rush * rush) + pole(finger.at, charge * pull) + const ring = cover(abs(length(vec2(at - finger.at)) - 12), 0.5) * pull * 0.45 + ringRed = max(ringRed, ring * max(charge, 0)) + ringLead = max(ringLead, ring * max(-charge, 0)) + turned = turned * (1 - 2 * here) + } + const strength = length(field) + // Where the poles cancel exactly the field has no direction; a dash there lies level. + const dir = vec2(strength > 1e-12 ? vec2(clamp(field / max(strength, 1e-12), -1, 1)) : vec2(1, 0)) + const stirred = grade(strength) + // How hard the pencil pressed: by the wandering poles' field, and only a little more for a pointer, + // so that a pointer turns the dashes and does not darken the paper under itself. + const heavy = calm * calm + 0.35 * max(stirred * stirred - calm * calm, 0) + + // The square's dash: set down by hand (a little off the middle), longer and darker where the field is strong. + const h = stir(square.x, square.y) + const nudge = vec2(slice(h, 0, 15) - 0.5, slice(h, 4, 15) - 0.5) * (3 - 3 * heavy) + const rel = vec2(at - middle - nudge) + const along = dot(rel, dir) + const reach = 6.2 + 5.8 * heavy + 0.8 * slice(h, 8, 7) + const stop = clamp(along, -reach, reach) + const across = rel.x * dir.y - rel.y * dir.x + 0.5 * sin(along * 0.16 + 6 * slice(h, 11, 7)) + const lift = stop / reach + const half = (0.52 + 0.78 * heavy + 0.2 * slice(h, 14, 7)) * (1 - 0.5 * lift * lift) + const press = 0.33 + 0.48 * heavy + 0.14 * slice(h, 17, 7) + const dash = cover(length(vec2(along - stop, across)), half) * press * (0.68 + 0.32 * tooth) + + // Graphite dust: up to four specks a square, shown where the field is strong, creeping along it. + let dust = 0 + let word = h + for (const n of range(4)) { + word = again(word) + const home = vec2(slice(word, 0, 31) - 0.5, slice(word, 5, 31) - 0.5) * 16 + const beat = fract(time * 0.08 + slice(word, 10, 31)) + const speck = vec2(middle + home + dir * ((beat - 0.5) * 10)) + const need = 0.15 + 0.62 * slice(word, 18, 15) + const shown = clamp((stirred - need) * 6, 0, 1) * sin(PI * beat) + const grit = 0.6 + 0.9 * slice(word, 15, 7) + dust = max(dust, clamp((grit - length(vec2(at - speck))) * perPixel + 0.5, 0, 1) * shown) + } + + // No dash or dust is drawn across a wandering pole's own mark. + const a = vec2(at - northAt) + const b = vec2(at - southAt) + const clear = clamp((min(length(a), length(b)) - 16.5) * 0.5, 0, 1) + let inkLead = max(dash, dust * 0.8) * (1 - onHub) * clear + let inkRed = 0 + + // A compass: two rings, sixteen ticks, pencil shading on the far side, a diamond needle with a red + // north half and a hatched south half, and a pivot. + if (onHub > 0.5) { + const c = vec2(at - hub) + const r = length(c) + const rim = max(cover(abs(r - 33), 0.8), cover(abs(r - 29.5), 0.3) * 0.7) + const far = max(abs(c.x), abs(c.y)) + const near = min(abs(c.x), abs(c.y)) + const ticks = max( + cover(near, 0.55) * between(r, 23.5, 29.5), + max( + cover((far - near) * 0.707, 0.4) * between(r, 25, 29.5), + cover(abs(far * 0.3827 - near * 0.9239), 0.3) * between(r, 26.6, 29.5), + ), + ) + const shade = + r < 28.6 && length(vec2(c - vec2(-6, 6))) > 27.5 ? cover(hatch(c.x + c.y, 3.6) * 0.707, 0.3) * 0.5 : 0 + const sway = 0.045 * sin(time * 1.7 + hubNo * 2.1) + 0.025 * sin(time * 2.9 + hubNo) + const aim = vec2(dir.x - dir.y * sway, dir.y + dir.x * sway) + const na = dot(c, aim) + const nc = c.x * aim.y - c.y * aim.x + const edge = (abs(nc) * 24 + abs(na) * 5 - 120) / 24.5 + const hole = r > 2.6 ? 1 : 0 + const body = inside(edge) * hole + const north = na > 0 ? 1 : 0 + const outline = cover(abs(edge), 0.45) * hole + const southHatch = cover(hatch(na, 2.8), 0.3) * 0.55 * (1 - north) + const crease = cover(abs(nc), 0.25) * 0.45 + const pivot = max(cover(abs(r - 2.6), 0.45), inside(r - 0.9)) + inkRed = body * north * (nc > 0 ? 0.95 : 0.72) * (0.84 + 0.16 * tooth) + inkLead = max( + max(rim * 0.82, max(ticks * 0.7, shade)), + max(max(outline * 0.85, body * max(southHatch, crease)), pivot * 0.9), + ) + } + + // The wandering poles' marks: a ringed N in red pencil, a ringed S in graphite. + const letterN = min( + toStroke(a, vec2(-4, 0), vec2(0, 1), 6), + min(toStroke(a, vec2(0, 0), vec2(0.5547, -0.8321), 7.21), toStroke(a, vec2(4, 0), vec2(0, 1), 6)), + ) + const markN = max(cover(abs(length(a) - 15), 0.85), cover(letterN, 0.7)) + const up = vec2(b - vec2(0, 3.2)) + const low = vec2(b + vec2(0, 3.2)) + const upGap = up.x > 0 && up.y < 0 ? 9 : abs(length(up) - 3.2) + const lowGap = low.x < 0 && low.y > 0 ? 9 : abs(length(low) - 3.2) + const markS = max(cover(abs(length(b) - 15), 0.85), cover(min(upGap, lowGap), 0.7)) + + inkRed = max(inkRed, max(markN * 0.88, ringRed)) + inkLead = max(inkLead, max(markS * 0.88, ringLead)) + + let color = vec3(paper * (1 - 0.035 * (tooth - 0.5))) + color = vec3(mix(color, ruled, max(fine * 0.45, bold * 0.8))) + color = vec3(mix(color, lead, inkLead)) + color = vec3(mix(color, redLead, inkRed)) + return vec4(color, 1) +}) + +export default shader({ fps: 30, passes: [sheet] }) diff --git a/plugins/theme-examples/hooks/scenes/field-lines/still.ts b/plugins/theme-examples/hooks/scenes/field-lines/still.ts new file mode 100644 index 00000000..ded2ad23 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/field-lines/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Field Lines, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function fieldLinesSvg(): string { + return ( + "Field Lines" + ) +} diff --git a/plugins/theme-examples/hooks/scenes/garden-beetles/scene.ts b/plugins/theme-examples/hooks/scenes/garden-beetles/scene.ts new file mode 100644 index 00000000..19e3d1e8 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/garden-beetles/scene.ts @@ -0,0 +1,738 @@ +// Garden Beetles: a handheld-console pixel garden behind the chat. +// +// Nine beetles and ladybirds patrol the side margins one pixel step at a time, leaving dotted +// footprints. Three of them push a keycap. Bring a pointer near and they leave it where it is, +// bolt to the nearest plant and tuck under it, two eyes among the leaves watching the pointer; +// when it is quiet a head shows, and they walk back. The stag beetle alone stands its ground. +// Around them: two files of ants along the cracks, two butterflies hopping between plants, dew, +// moss, and a few motes on the air. +// +// How it is drawn: the whole window is one small screen of 6-point pixels. A tiny 8-bit buffer +// (sim) remembers, for each bug, where it is on its loop and how scared it is. A second small +// buffer (draw) holds the finished pixel picture, one texel per big pixel, and keeps the +// footprints in its alpha lane. The picture pass only enlarges that with crisp pixel edges, so +// nearly all of the cost stays the same whatever the sharpness of the screen. +// Nothing here is a bitmap: each bug is a row-by-row girth profile plus a few rules, each plant +// three or four discs, and every colour one of sixteen. +import { + abs, buffer, clamp, composer, dt, first, floor, fract, frame, int, max, min, mix, mod, picture, pointers, + px, scene, shader, sign, size, sqrt, step, table, time, unsigned, vec2, vec3, vec4, +} from 'claude-code/shader' +import type { Buffer } from 'claude-code/shader' + +/** Points per big pixel. */ +const PX = 6 +const BUGS = 9 +const LEAVES = 12 +const MOTES = 6 +const WINGS = 2 +/** Paving tile, in big pixels. */ +const TILE = 16 + +// The two 16-colour palettes, 0 to 255: rows 0 to 15 are "Moss at dusk" (dark), 16 to 31 "Sunlit patio" (light). +// 0 tile 1 tile, second tone 2 joint 3 fresh footprint +// 4 leaf shade 5 leaf 6 leaf light 7 ink (the outline) 8 cream +// 9 red 10 orange 11 teal 12 mint 13 amber 14 plum 15 brown +const INK = 7 +const CREAM = 8 +const paint = table([ + [38, 56, 57], [41, 61, 62], [29, 44, 46], [68, 96, 94], + [24, 68, 48], [40, 106, 74], [70, 148, 92], [7, 12, 14], [242, 233, 207], + [217, 84, 63], [241, 154, 91], [43, 156, 151], [143, 224, 196], [232, 176, 74], [138, 99, 194], [154, 95, 60], + [236, 229, 211], [230, 222, 201], [214, 204, 179], [191, 179, 148], + [60, 122, 72], [99, 168, 90], [156, 207, 120], [42, 40, 54], [255, 250, 240], + [216, 79, 60], [243, 154, 98], [42, 157, 150], [159, 227, 205], [230, 169, 58], [139, 98, 196], [163, 100, 62], +]) + +// One row per bug: where it lives (0 left margin, 1 right margin, 2 the whole window), +// its kind (0 ladybird, 1 scarab, 2 stag beetle), its shell colour and its glint colour. +const cast = table([ + [0, 0, 9, 10], [0, 1, 11, 12], [0, 0, 13, 8], [0, 2, 15, 13], + [1, 0, 13, 8], [1, 0, 9, 10], [1, 1, 14, 12], [1, 1, 11, 12], + [0, 1, 14, 12], +]) +// One row per bug: pixels walked a second, where on its loop it starts (0 to 1), +// the keycap it pushes (0 none, 1 semicolon, 2 brace, 3 bracket), seconds between rests. +// Bugs 3 and 8 share one loop half a lap apart, so they share a pace and a rhythm. +const gait = table([ + [7, 0.1, 0, 9], [5, 0.55, 0, 13], [7, 0.3, 0, 8], [5, 0.08, 2, 16], + [6, 0.35, 0, 11], [7, 0.05, 0, 7], [5, 0.6, 0, 14], [5, 0.3, 3, 12], + [5, 0.58, 1, 16], +]) +// Each bug's loop: six corners as shares of its home (across, up), then the first again. +// Every leg of a loop runs straight across or straight up. In each margin three bugs keep to +// a patch of their own, one above the other, and the keycap pushers walk the lane around them, +// so no two loops cross. The lane keeps a little clear of the text column. +const route = table([ + [0.34, 0.86], [0.66, 0.86], [0.66, 0.68], [0.5, 0.68], [0.5, 0.76], [0.34, 0.76], [0.34, 0.86], + [0.66, 0.59], [0.34, 0.59], [0.34, 0.41], [0.66, 0.41], [0.66, 0.41], [0.66, 0.59], [0.66, 0.59], + [0.34, 0.32], [0.66, 0.32], [0.66, 0.14], [0.5, 0.14], [0.5, 0.22], [0.34, 0.22], [0.34, 0.32], + [0.125, 0.95], [0.86, 0.95], [0.86, 0.05], [0.125, 0.05], [0.125, 0.05], [0.125, 0.95], [0.125, 0.95], + [0.66, 0.86], [0.34, 0.86], [0.34, 0.68], [0.66, 0.68], [0.66, 0.68], [0.66, 0.86], [0.66, 0.86], + [0.34, 0.59], [0.5, 0.59], [0.5, 0.51], [0.66, 0.51], [0.66, 0.41], [0.34, 0.41], [0.34, 0.59], + [0.66, 0.32], [0.34, 0.32], [0.34, 0.14], [0.66, 0.14], [0.66, 0.14], [0.66, 0.32], [0.66, 0.32], + [0.875, 0.95], [0.14, 0.95], [0.14, 0.05], [0.875, 0.05], [0.875, 0.05], [0.875, 0.95], [0.875, 0.95], + [0.125, 0.95], [0.86, 0.95], [0.86, 0.05], [0.125, 0.05], [0.125, 0.05], [0.125, 0.95], [0.125, 0.95], +]) +// The big plants the bugs hide under: home, across, up, and which way it is turned +// (0 to 3 are quarter turns of a three-leaf plant; 4 is the lucky one). Five to a margin, one +// in the middle of each patch and one in each lane, so cover is never far; two more peep in at +// the top and bottom of the text column. +const hides = table([ + [0, 0.5, 0.77, 0], [0, 0.5, 0.5, 1], [0, 0.5, 0.23, 2], [0, 0.125, 0.36, 3], [0, 0.8, 0.64, 2], + [1, 0.5, 0.77, 1], [1, 0.5, 0.5, 3], [1, 0.5, 0.23, 4], [1, 0.875, 0.64, 0], [1, 0.2, 0.36, 2], + [2, 0.45, 0.99, 2], [2, 0.6, 0.01, 0], +]) +// Half the width of each bug, row by row from the head: ladybird, scarab, stag beetle. +const girth = table([ + [0, 0, 0], [0, 2, 0], [3, 3, 3], [4, 4, 4], [5, 5, 3], [6, 5, 5], [6, 5, 5], + [6, 5, 5], [6, 5, 5], [6, 5, 5], [5, 4, 4], [4, 3, 3], [0, 0, 0], +]) +// The rows the three pairs of legs leave from, for each kind. +const hips = table([[5, 7, 9], [3, 6, 9], [3, 6, 9]]) + +/** One of three numbers, by kind. */ +function pick(v: vec3, kind: number): number { + return kind < 0.5 ? v.x : kind < 1.5 ? v.y : v.z +} + +/** + * A bit mixer made for this scene: shifts, adds and exclusive-ors only. + * Gives 0 to 1 for a pair of whole numbers. + */ +function grit(a: number, b: number): number { + let h = int(a) + (int(b) << 11) + 7717 + h += h << 14 + h ^= h >>> 8 + h += h << 7 + h ^= h >>> 20 + h += h << 18 + h ^= h >>> 22 + h += h << 2 + h ^= h >>> 9 + h += h << 23 + h ^= h >>> 7 + return unsigned(h) / 4294967296 +} + +/** The screen in big pixels. */ +function screenW(): number { return floor(size.x / PX + 0.5) } +function screenH(): number { return floor(size.y / PX + 0.5) } + +/** + * How wide a home is, in big pixels. The margins run from the window's edge to the prompt box, + * and are never taken as less than 36. + */ +function room(home: number, W: number): number { + const known = composer.z - composer.x > 0.05 + return home < 0.5 ? max((known ? composer.x : 0.26) * W, 36) : home < 1.5 ? max((known ? 1 - composer.z : 0.26) * W, 36) : W +} + +/** Across, in big pixels, for a share of a home. */ +function across(home: number, share: number, W: number): number { + const wide = room(home, W) + return floor(home < 0.5 ? share * wide : home < 1.5 ? W - wide + share * wide : share * W) +} + +/** The pixel a given number of steps along bug i's loop, and which way the loop heads there: [x, y, headX, headY]. */ +function spot(i: number, home: number, steps: number, W: number, H: number): [number, number, number, number] { + let x = 0 + let y = 0 + let headX = 0 + let headY = 1 + let done = 0 + for (let k = 0; k < 6; k++) { + const a = route.at(i * 7 + k) + const b = route.at(i * 7 + k + 1) + const ax = across(home, a.x, W) + const ay = floor(a.y * H) + const bx = across(home, b.x, W) + const by = floor(b.y * H) + const leg = abs(bx - ax) + abs(by - ay) + if (steps >= done && steps < done + leg) { + const into = floor(steps - done) + headX = sign(bx - ax) + headY = sign(by - ay) + x = ax + headX * into + y = ay + headY * into + } + done += leg + } + return [x, y, headX, headY] +} + +/** Where an ant road runs in a margin: the up-and-down joint nearest the margin's middle. */ +function antLane(road: number, W: number): number { + return floor(across(road, 0.5, W) / TILE + 0.5) * TILE +} + +/** + * An amount to add to an 8-bit lane this frame, as a whole number of its 255 steps: one more on + * some frames than on others (by the roll of the mixer), so that slow rates come out right at + * any frame rate and never stall. + */ +function drip(amount: number, salt: number): number { + return floor(amount * 255 + grit(frame, salt)) / 255 +} + +/** Two bytes for a whole number up to 65535. */ +function low(n: number): number { return (n - floor(n / 256) * 256) / 255 } +function high(n: number): number { return floor(n / 256) / 255 } +function whole(lo: number, hi: number): number { return floor(lo * 255 + 0.5) + floor(hi * 255 + 0.5) * 256 } + +// sim: 16 by 5 texels of 8 bits. Column i is bug i. Row 0 keeps its state from frame to frame: +// (place on the loop, high byte and low byte; dash, 0 walking to 1 tucked under a leaf; +// nerves, 1 just frightened falling to 0 calm). Rows 1 to 4 are this frame's pose for draw to +// read: where the bug is, where it left its loop, which way it faces (with whether it is bolting +// and whether it is airing its wings), and where its keycap is. +// The seven columns past the bugs hold the ants' wariness (see below). +const sim: Buffer = buffer({ size: [16, 5] }, (p): vec4 => { + const i = p.cell.x + const W = screenW() + const H = screenH() + const who = cast.at(i) + const how = gait.at(i) + const kept = sim.at(vec2(i, 0)) + let place = whole(kept.g, kept.r) / 65536 + let dash = kept.b + let nerves = kept.a + if (first) { + place = how.y + dash = 0 + nerves = 0 + } + + // The loop's length, then where this place on it falls. + let lap = 0 + for (let k = 0; k < 6; k++) { + const a = route.at(i * 7 + k) + const b = route.at(i * 7 + k + 1) + lap += abs(across(who.x, b.x, W) - across(who.x, a.x, W)) + abs(floor(b.y * H) - floor(a.y * H)) + } + const walked = place * lap + const [homeX, homeY, headX, headY] = spot(i, who.x, walked, W, H) + // The keycap a bug pushes rides eleven steps further along the same loop, so it turns each corner first. + const [capX, capY] = spot(i, who.x, mod(walked + 11, max(lap, 1)), W, H) + + // The nearest plant, counted in steps across plus steps up. + let leafX = homeX + let leafY = homeY + let nearest = 100000 + for (let j = 0; j < LEAVES; j++) { + const h = hides.at(j) + const x = across(h.x, h.y, W) + const y = floor(h.z * H) + const far = abs(x - homeX) + abs(y - homeY) + if (far < nearest) { + nearest = far + leafX = x + leafY = y + } + } + // Each bug has its own spot under a plant, so two that share one do not sit on the same pixels. + leafX += (mod(i, 3) - 1) * 3 + leafY += (floor(i / 3) - 1) * 2 + + // The dash is L-shaped: the longer leg first, then the shorter. + const runX = leafX - homeX + const runY = leafY - homeY + const runLength = abs(runX) + abs(runY) + const wide = abs(runX) >= abs(runY) + const far = floor(dash * runLength + 0.5) + const firstLeg = wide ? abs(runX) : abs(runY) + const onFirst = min(far, firstLeg) + const onSecond = far - onFirst + const x = homeX + sign(runX) * (wide ? onFirst : onSecond) + const y = homeY + sign(runY) * (wide ? onSecond : onFirst) + const inFirst = far < firstLeg || runLength - firstLeg < 0.5 + const runHeadX = (wide ? inFirst : !inFirst) ? sign(runX) : 0 + const runHeadY = (wide ? inFirst : !inFirst) ? 0 : sign(runY) + + // Who is near? Each live pointer frightens a circle; a press reaches further, a pointer over + // the app's own panels less far, and every extra pointer widens all of them. + let live = 0 + for (const finger of pointers) live += 1 - step(0.3, finger.gone) + let fright = 0 + for (const finger of pointers) { + const fx = finger.at.x / PX + const fy = finger.at.y / PX + const reach = (17 + 9 * finger.down) * (1 - 0.3 * finger.over) * (1 + 0.25 * max(live - 1, 0)) + const fromBug = sqrt((fx - x) * (fx - x) + (fy - y) * (fy - y)) + const fromHome = sqrt((fx - homeX) * (fx - homeX) + (fy - homeY) * (fy - homeY)) + if (finger.gone < 0.3 && (fromBug < reach || fromHome < reach * 0.8)) fright = 1 + } + + // Frightened: nerves jump to 1. Left alone they settle: tucked until 0.5, peeking until 0.14, then home. + // The stag beetle is the brave one: it never runs, it stops where it is and snaps its jaws. + nerves = fright > 0.5 ? 1 : max(0, nerves - drip(dt / 3.6, i)) + const brave = who.y > 1.5 + const peek = clamp(1 - 8 / max(runLength, 1), 0, 1) + const goal = brave ? 0 : nerves > 0.5 ? 1 : nerves > 0.14 ? peek : 0 + // It bolts at 90 pixels a second however far the plant is, edges out to peek at 16, and + // walks home at its own pace. + const pace = drip((goal > dash ? 90 : goal > 0.001 ? 16 : how.x) * dt / max(runLength, 1), i + 40) + dash += clamp(goal - dash, -pace, pace) + + // Walk on only when calm, with a short rest every so often. + const beat = fract(time / how.w + how.y * 7) + let stride = step(0.2, beat) + // Bugs 3 and 8 share a loop: whichever comes up close behind the other waits for it to move on. + if (i == 3 || i == 8) { + const mate = sim.at(vec2(i == 3 ? 8 : 3, 0)) + const ahead = fract(whole(mate.g, mate.r) / 65536 - place) * lap + if (ahead < 32) stride = 0 + } + // In the middle of a rest a ladybird lifts its wing cases for a moment. + const calm = dash < 0.002 && nerves < (brave ? 0.5 : 0.002) + const airing = calm && beat > 0.05 && beat < 0.15 ? 1 : 0 + if (calm) place = fract(place + stride * how.x * dt / max(lap, 1)) + + // Which way it faces: along its loop, along the dash on the way in, and outward while tucked or coming out. + let faceX = headX + let faceY = headY + if (dash > 0.002 && runLength > 0.5) { + const inward = goal > dash + 0.002 ? 1 : -1 + faceX = runHeadX * inward + faceY = runHeadY * inward + } + const facing = faceY > 0.5 ? 0 : faceX > 0.5 ? 1 : faceY < -0.5 ? 2 : 3 + + // The seven spare columns remember how wary each stretch of the two ant roads is: four + // stretches to a texel, each four pixels of road. Wariness jumps up near a pointer and ebbs + // away slowly, so a pointer swept back and forth parts the file once instead of shaking it. + if (i > 8.5) { + if (p.cell.y > 3.5) return vec4(0, 0, 0, 0) + const road = p.cell.y < 1.5 ? 0 : 1 + const first4 = ((i - 9) * 2 + p.cell.y - road * 2) * 4 + const was = sim.at(p.cell) + const wary = [was.r, was.g, was.b, was.a] + const lane = antLane(road, W) + for (const k of [0, 1, 2, 3]) { + const up = (first4 + k) * 4 + 2 + const rx = lane + mod(floor(up / TILE), 2) * (TILE / 2) + let near = 0 + for (const finger of pointers) { + const ax = finger.at.x / PX - rx + const ay = finger.at.y / PX - up + if (finger.gone < 0.3 && ax * ax + ay * ay < 170) near = 1 + } + wary[k] = first ? 0 : wary[k]! + clamp(near - wary[k]!, -drip(dt / 1.6, i * 8 + k), dt / 0.12) + } + return vec4(wary[0]!, wary[1]!, wary[2]!, wary[3]!) + } + + const steps = floor(place * 65536) + if (p.cell.y < 0.5) return vec4(high(steps), low(steps), dash, nerves) + if (p.cell.y < 1.5) return vec4(low(x), high(x), low(y), high(y)) + if (p.cell.y < 2.5) return vec4(low(homeX), high(homeX), low(homeY), high(homeY)) + if (p.cell.y < 3.5) return vec4(facing / 3, goal > 0.999 ? 1 : 0, 0, airing) + return vec4(low(capX), high(capX), low(capY), high(capY)) +}) + +// The drawing rules. Each returns a code, not a colour: 0 nothing, 1 ink, 2 body, 3 sheen, 4 cream, +// and for a bug 5 is a leg or a feeler. + +/** + * One pixel of a bug seen from above with its head up. side is pixels right of its middle, + * row counts from 0 at the head to 12 at the tail. legs is 0 or 1, the two frames of its gait. + * twitch is 1 while the antenna on this side is flicked. airing is 1 while a ladybird holds its wing cases open. + */ +function bugPixel(kind: number, side: number, row: number, legs: number, twitch: number, airing: number): number { + const out = abs(side) + const wide = pick(girth.at(row), kind) + const above = pick(girth.at(row - 1), kind) + const below = pick(girth.at(row + 1), kind) + let code = 0 + if (row > 0.5 && row < 11.5 && out < wide) { + const rim = out > wide - 1.5 || out >= above || out >= below + code = rim ? 1 : 2 + if (!rim) { + if (kind < 0.5) { + // Ladybird: dark head with two pale eyes, a seam and three spots a side. + if (row < 3.5) code = 1 + if (row == 3 && out == 2) code = 4 + if (row > 3.5 && out == 0) code = 1 + if ((row == 5 && out == 2) || (row == 7 && out == 3) || (row == 9 && out == 2)) code = 1 + // Wing cases lifted: the wings show down the middle. + if (airing > 0.5 && row > 5.5 && out == 1) code = 3 + } else if (kind < 1.5) { + // Scarab: small dark head, a bright collar, a seam and one groove of sheen down each wing case. + if (row < 2.5) code = 1 + if (row == 2 && out == 1) code = 4 + if (row > 2.5 && row < 4.5) code = 3 + if (row > 4.5 && out == 0) code = 1 + // The sheen slides a row with every step, as it would on a polished shell. + if (row > 5.5 + legs && row < 8.5 + legs && out == 2) code = 3 + } else { + // Stag beetle: broad head with pale eyes, a narrow waist, a seam, sheen on the shoulders. + if (row == 3 && out == 2) code = 4 + if (row == 4) code = 1 + if (row > 4.5 && out == 0) code = 1 + if (row > 5.5 && row < 8.5 && out == 3) code = 3 + } + } + // A ladybird airing its wings shows their tips past its tail. + if (kind < 0.5 && airing > 0.5 && row == 11 && out < 2.5 && out > 0.5) code = 4 + } + // Legs: one pixel at the hip, one beyond it a row ahead or behind. Front and hind legs of one + // side move with the middle leg of the other, and the two frames swap them. + const right = side > 0 ? 1 : 0 + const hip = hips.at(kind) + for (let j = 0; j < 3; j++) { + const at = j < 0.5 ? hip.x : j < 1.5 ? hip.y : hip.z + const reach = pick(girth.at(at), kind) + const swing = mod(j + right + legs, 2) * 2 - 1 + if ((row == at && out == reach) || (row == at + swing && out == reach + 1)) code = 5 + } + // Feelers (and the stag's jaws, which close when it twitches). + if (kind < 0.5) { + if (row == 1 && out == 1) code = 5 + if (row == 0 && out == 2 - twitch) code = 5 + } else if (kind < 1.5) { + if (row == 0 && out == 2 && twitch < 0.5) code = 5 + if (row == 1 && out == 3 && twitch > 0.5) code = 5 + } else { + // The jaws take the sheen colour so they show on dark paving too. + if (row == 1 && out == 3) code = 3 + if (row == 0 && (out == 3 - twitch || out == 2 - twitch)) code = 3 + } + return code +} + +/** A pale keycap seven pixels wide and nine tall with a sign on it. x and y count from its lower left. */ +function keycap(mark: number, x: number, y: number): number { + if (x < 0 || x > 6 || y < 0 || y > 8) return 0 + const edgeX = x == 0 || x == 6 + const edgeY = y == 0 || y == 8 + if (edgeX && edgeY) return 0 + if (edgeX || edgeY) return 1 + const col = x - 2 + const rowUp = y - 2 + let on = 0 + if (col >= 0 && col <= 2 && rowUp >= 0 && rowUp <= 4) { + if (mark < 1.5) { + // a semicolon + on = (col == 1 && (rowUp == 4 || rowUp == 2 || rowUp == 1)) || (col == 0 && rowUp == 0) ? 1 : 0 + } else if (mark < 2.5) { + // an opening brace + on = (col == 2 && (rowUp == 4 || rowUp == 0)) || (col == 1 && rowUp != 2) || (col == 0 && rowUp == 2) ? 1 : 0 + } else { + // a closing bracket + on = col == 2 || (col == 1 && (rowUp == 4 || rowUp == 0)) ? 1 : 0 + } + } + return on > 0.5 ? 1 : 4 +} + +/** + * One pixel of a big plant, measured from its middle. turn 0 to 3 is a quarter turn; 4 has four leaves. + * Codes: 1 shade, 2 leaf, 3 light, 4 a glinting drop of dew (only while dew is 1). + */ +function plant(turn: number, x0: number, y0: number, dew: number): number { + const x = turn < 0.5 ? x0 : turn < 1.5 ? -y0 : turn < 2.5 ? -x0 : turn < 3.5 ? y0 : x0 + const y = turn < 0.5 ? y0 : turn < 1.5 ? x0 : turn < 2.5 ? -y0 : turn < 3.5 ? -x0 : y0 + const lucky = turn > 3.5 + // Squared distance to each leaflet's middle. + const top = x * x + (y - (lucky ? 5 : 4)) * (y - (lucky ? 5 : 4)) + const west = lucky ? (x + 5) * (x + 5) + y * y : (x + 4) * (x + 4) + (y + 3) * (y + 3) + const east = lucky ? (x - 5) * (x - 5) + y * y : (x - 4) * (x - 4) + (y + 3) * (y + 3) + const south = lucky ? x * x + (y + 5) * (y + 5) : 1000 + const near = min(min(top, west), min(east, south)) + let code = 0 + if (near <= 17) { + code = near >= 11 ? 1 : 2 + // A dark crease where two leaflets of the three meet: where the nearest two are all but equally near. + const second = top + west + east - near - max(max(top, west), east) + if (!lucky && second - near < 2.5) code = 1 + // The pale chevron each leaflet wears, two pixels out from the middle of the plant. + if (code == 2 && near >= 3 && near <= 6 && ((top == near && y < (lucky ? 5 : 4)) || (south == near && y > -5) || (west == near && x > (lucky ? -5 : -4)) || (east == near && x < (lucky ? 5 : 4)))) code = 3 + } + if (dew > 0.5 && x == -1 && y == (lucky ? 6 : 5)) code = 4 + // The stalk, from the middle out between the two lower leaflets. + if (!lucky && code == 0 && ((x == 0 && y < 0 && y > -7.5) || (x == 1 && y == -8))) code = 1 + return code +} + +/** A small plant sprig, five pixels square, from its lower left: 1 dark, 2 leaf. */ +function sprig(x: number, y: number): number { + if (x < 0 || x > 4 || y < 0 || y > 4) return 0 + if (x == 2 && y < 2.5) return 1 + if ((y == 3 && x != 2) || (y == 2 && (x == 1 || x == 3)) || (y == 4 && (x == 0 || x == 4))) return 2 + return 0 +} + +// draw: the finished pixel picture, one texel per big pixel. rgb is the colour; alpha is how +// fresh the footprint on that pixel is. +const draw: Buffer = buffer({ scale: 1 / PX, filter: 'linear' }, (p): vec4 => { + const x = p.cell.x + const y = p.cell.y + const W = screenW() + const H = screenH() + + // Light or dark? The Svg under the shader knows; its middle pixel is bare paving in either mode. + const under = scene.sample(vec2(0.5, 0.5)) + const day = step(0.4, under.r * 0.2126 + under.g * 0.7152 + under.b * 0.0722) + + // Paving: rows of tiles, every other row shifted half a tile, one tile in three a shade off. + const course = floor(y / TILE) + const shifted = x + mod(course, 2) * (TILE / 2) + const tile = floor(shifted / TILE) + const inX = shifted - tile * TILE + const inY = y - course * TILE + const roll = grit(tile, course) + let slot = roll < 0.3 ? 1 : 0 + if (inX == 0 || inY == 0) slot = 2 + // A chipped corner here and there, a hairline crack across one tile in ten, and moss in the joints. + if (roll > 0.86 && ((inX == 1 && inY == 1) || (inX == 2 && inY == 1) || (inX == 1 && inY == 2))) slot = 2 + const flaw = grit(tile + 53, course + 7) + if (flaw < 0.1 && inX > 3.5 && inX < 10.5 && inX + floor(flaw * 60) == inY + 4 + floor(inX / 4)) slot = 2 + const mossy = slot == 2 && flaw > 0.8 && grit(x, y) < 0.2 + + // Moss is the quiet green of each palette: the pale one by day, the deep one at dusk. + if (mossy) slot = day > 0.5 ? 6 : 4 + + // Footprints fade in two steps. They lie on the paving, under everything that grows or walks. + // A print lasts about seven seconds. + let print = max(draw.at(p.cell).a - drip(dt / 7, x * 7 + y * 131), 0) + if (print > 0.5) slot = 3 + else if (print > 0.1 && slot != 2) slot = 2 + + // Small plant sprigs grow out of some joints. + const lot = grit(tile + 31, course + 17) + if (lot < 0.16) { + const s = sprig(inX - floor(lot * 60), inY + 2 - floor(lot * 12)) + if (s > 0.5) slot = 5 + } + const lot2 = grit(tile + 5, course + 71) + if (lot2 < 0.08) { + const s = sprig(inX - 6 - floor(lot2 * 90), inY - 7 - floor(lot2 * 60)) + if (s > 0.5) slot = s < 1.5 ? 5 : 6 + } + // And, rarely, a four-petalled flower. + const lot3 = grit(tile + 97, course + 41) + if (lot3 < 0.07) { + const petalX = abs(inX - 4 - floor(lot3 * 120)) + const petalY = abs(inY - 5 - floor(lot3 * 100)) + if (petalX + petalY == 1) slot = 14 + if (petalX + petalY == 0) slot = 13 + } + + // Two files of ants follow the cracks up one margin and down the other: up a joint, along to the + // next, up again. Near a pointer they break file to either side, and drift back into line when + // it has gone. + for (let side = 0; side < 2; side++) { + const lane = antLane(side, W) + const jog = mod(course, 2) * (TILE / 2) + const off = x - lane - jog + // How wary this stretch of road is, from sim's spare texels. + const stretch = min(floor(y / 4), 55) + const kept = sim.at(vec2(9 + floor(stretch / 8), side * 2 + floor(mod(stretch, 8) / 4))) + const which = mod(stretch, 4) + const shy = which < 0.5 ? kept.r : which < 1.5 ? kept.g : which < 2.5 ? kept.b : kept.a + // How far along the road this pixel is: 24 to a course, 16 up the joint and 8 along to the next. + const onJoint = inY > 0.5 || off == 0 + const along = onJoint ? course * 24 + inY : course * 24 - 8 + (mod(course, 2) > 0.5 ? x - lane : lane + TILE / 2 - x) + const onLink = inY == 0 && x >= lane && x <= lane + TILE / 2 + // The road is cut into places of seven pixels; seven in ten hold an ant, somewhere in the + // first four pixels of its place, so the file is ragged and not a dashed line. + const march = along + (side < 0.5 ? -1 : 1) * time * 5 + const ant = floor(march / 7) + const luck = grit(ant, side + 3) + const part = march - ant * 7 - mod(floor(luck * 29), 4) + const stray = floor(shy * 3 + 0.5) * (mod(ant, 2) * 2 - 1) + if (luck > 0.3 && ((onJoint && off == stray) || (onLink && !onJoint && shy < 0.1))) { + // Tail, then head, then (for one in three) the crumb it carries in front. + const front = side < 0.5 ? part : 2.999 - part + if (front >= 0 && front < 1) slot = 15 + else if (front >= 1 && front < 2) slot = day > 0.5 ? INK : 15 + else if (front >= 2 && front < 3 && luck > 0.75) slot = luck > 0.92 ? 12 : CREAM + } + } + + // Shadows first: every plant, keycap and bug darkens the paving one pixel down and to the right. + let shade = 0 + for (let j = 0; j < LEAVES; j++) { + const h = hides.at(j) + const lx = x - 1 - across(h.x, h.y, W) + const ly = y + 1 - floor(h.z * H) + if (abs(lx) < 10.5 && abs(ly) < 10.5 && plant(h.w, lx, ly, 0) > 0.5) shade = 1 + } + + // Where the pointers are, taken together: tucked bugs glance that way. + let seenX = 0 + let seen = 0 + for (const finger of pointers) { + const live = finger.gone < 0.3 ? 1 : 0 + seenX += live * finger.at.x / PX + seen += live + } + + let alarm = 0 + let watching = 0 + let limb = 0 + for (let i = 0; i < BUGS; i++) { + const state = sim.at(vec2(i, 0)) + const here = sim.at(vec2(i, 1)) + const home = sim.at(vec2(i, 2)) + const pose = sim.at(vec2(i, 3)) + const who = cast.at(i) + const how = gait.at(i) + const bx = whole(here.r, here.g) + const by = whole(here.b, here.a) + const hx = whole(home.r, home.g) + const hy = whole(home.b, home.a) + const dash = state.b + const facing = floor(pose.r * 3 + 0.5) + const fx = facing == 1 ? 1 : facing == 3 ? -1 : 0 + const fy = facing == 0 ? 1 : facing == 2 ? -1 : 0 + const dx = x - bx + const dy = y - by + // Where a margin is too slim for a lane around the patches, the keycap pushers stay away. + const shown = how.z < 0.5 || room(who.x, W) > 50 + + // Stamp a footprint under the tail on every third step, left foot and right foot in turn. + const trod = fx != 0 ? bx : by + const foot = mod(floor(trod / 3), 2) * 2 - 1 + if (shown && dash < 0.97 && mod(trod, 3) < 0.5 && dx == -3 * fx + 2 * foot * fy && dy == -3 * fy - 2 * foot * fx) print = 1 + + // The keycap it pushes: sim keeps it eleven steps ahead on the loop, and there it stays when the bug bolts. + if (shown && how.z > 0.5) { + const cap = sim.at(vec2(i, 4)) + const kx = whole(cap.r, cap.g) + const ky = whole(cap.b, cap.a) + const k = keycap(how.z, x - kx + 3, y - ky + 4) + if (k > 0.5) slot = k > 3.5 ? CREAM : INK + else if (keycap(how.z, x - kx + 2, y - ky + 5) > 0.5) shade = 1 + } + // Four specks of dust lie where it bolted from while a pointer is near, and for about a second after. + const bolted = shown && state.a > 0.75 && dash > 0.05 + if (bolted && ((x == hx - 6 && y == hy - 1) || (x == hx + 5 && y == hy + 2) || (x == hx - 1 && y == hy - 6) || (x == hx + 6 && y == hy - 2))) slot = 3 + + if (shown && abs(dx) < 7.5 && abs(dy) < 7.5) { + const row = 6 - (dx * fx + dy * fy) + const side = dx * fy - dy * fx + // Feelers flick now and then, each side in its own time. A stag beetle with a pointer near snaps its jaws. + const beat = grit(floor(time + i * 0.37), i * 13 + 3) + const flick = side < 0 ? step(0.82, beat) : 1 - step(0.16, beat) + const twitch = who.y > 1.5 && state.a > 0.5 ? step(0.5, fract(time)) : flick + // Its legs swap every four pixels walked, feet to the ground: under one full swing a second at + // every pace a bug walks its loop or walks home. (Edging out to peek is quicker, for eight pixels.) + const stepping = mod(floor((bx + by) / 4), 2) + // Bolting, it is drawn as a smear: a dark head and the rest of its shape in its shell colour, + // with no outline, legs or markings. + const bolting = pose.g > 0.5 && dash < 0.97 + const code = bugPixel(who.y, side, row, stepping, twitch, pose.a) + // Its shadow: bare paving is shaded where the bug covers the pixel up and to the left of it. + if (code < 0.5 && bugPixel(who.y, side - fy - fx, row + fx - fy, stepping, twitch, pose.a) > 0.5) shade = 1 + if (bolting && code > 0.5) { + shade = 0 + if (code < 4.5) slot = row < (who.y < 0.5 ? 3.5 : 2.5) ? INK : who.z + } else if (code > 0.5) { + shade = 0 + // (A ladybird's only sheen is its lifted wings, which are orange on the red ones and the amber ones alike.) + // Legs and feelers are ink by day and brown at dusk, where ink would vanish into the paving. + slot = code < 1.5 ? INK : code < 2.5 ? who.z : code < 3.5 ? (who.y < 0.5 ? 10 : who.w) : code < 4.5 ? CREAM : day > 0.5 ? INK : 15 + // One glint, always from the upper left of the screen whichever way the bug is turned. + const gx = dx + 2 * fx + const gy = dy + 2 * fy + if (code == 2 && ((gx == -2 && gy == 2) || (gx == -3 && gy == 1))) slot = who.w + // And a shade along the lower right, borrowed from a darker colour of the palette. + if (code == 2 && gx - gy > 3.5) slot = who.z == 9 ? 15 : who.z == 13 ? 10 : who.z == 11 ? 5 : who.z + } + } + // Tucked under its plant it keeps watch: two pale eyes among the leaves that blink and glance + // toward the pointers, and the tips of its feelers above them. + if (shown && dash > 0.97) { + const glance = seen < 0.5 ? 0 : seenX / seen - bx > 6 ? 1 : seenX / seen - bx < -6 ? -1 : 0 + if (y == by && abs(dx - glance) == 1 && fract(time * 0.37 + i * 0.31) > 0.07) watching = 1 + if (y == by + 2 && abs(dx) == 2) limb = 1 + } + // A startled mark stands where it bolted from, and stays there as long as the dust does. + if (bolted) { + const ex = x - hx - 6 + const ey = y - hy - 5 + if (ex >= 0 && ex <= 2 && ey >= 0 && ey <= 6) alarm = ex == 1 && (ey == 1 || ey > 2.5) && ey < 5.5 ? 2 : 1 + } + } + + // A shadow only darkens bare paving. + if (shade > 0.5 && (slot < 1.5 || slot == 3)) slot = 2 + + // The big plants lie over everything that walks. Now and then a drop of dew on one catches the light. + for (let j = 0; j < LEAVES; j++) { + const h = hides.at(j) + const lx = x - across(h.x, h.y, W) + const ly = y - floor(h.z * H) + if (abs(lx) < 10.5 && abs(ly) < 10.5) { + const code = plant(h.w, lx, ly, fract(time * 0.13 + j * 0.37) < 0.06 ? 1 : 0) + if (code > 0.5) slot = code < 1.5 ? 4 : code < 2.5 ? 5 : code < 3.5 ? 6 : CREAM + } + } + if (limb > 0.5) slot = day > 0.5 ? INK : 15 + if (watching > 0.5) slot = CREAM + if (alarm > 0.5) slot = alarm > 1.5 ? 13 : INK + + // Two butterflies (moths, by night) flit from plant to plant in their own margin: a few seconds + // perched with slow wings, then a hop in an arc. A pointer nearby pushes them off course. + for (let b = 0; b < WINGS; b++) { + const hop = time / 9 + b * 0.45 + const leg = floor(hop) + const part = fract(hop) + const from = hides.at(b * 5 + mod(leg, 5)) + const to = hides.at(b * 5 + mod(leg + 1, 5)) + const flight = clamp((part - 0.6) / 0.4, 0, 1) + const glide = flight * flight * (3 - 2 * flight) + const lift = 4 * glide * (1 - glide) + let wx = mix(across(from.x, from.y, W), across(to.x, to.y, W), glide) + 1 + let wy = mix(floor(from.z * H), floor(to.z * H), glide) + 3 + lift * 9 + for (const finger of pointers) { + const ax = wx - finger.at.x / PX + const ay = wy - finger.at.y / PX + const away = sqrt(ax * ax + ay * ay) + // (The push eases off over the second a lifted pointer lingers, so the butterfly glides back.) + const push = clamp(1 - finger.gone, 0, 1) * clamp(1 - away / 24, 0, 1) * 12 / max(away, 1) + wx += ax * push + wy += ay * push + } + const ux = x - floor(wx) + const uy = y - floor(wy) + // Wings beat quickly in flight and slowly at rest: wide open, or folded to a sliver. + const open = fract(time * (flight > 0.001 && flight < 0.999 ? 1 : 0.6) + b * 0.3) < 0.55 + const spread = abs(ux) + // Its shadow falls further away the higher it flies. + const height = 2 + floor(lift * 3) + const sx = abs(ux - height) + const sy = uy + height + if (sy >= -1 && sy <= 1 && (sx == 0 || (open ? sx <= 2 && !(sx == 2 && sy == -1) : sx == 1 && sy >= 0)) && (slot < 1.5 || slot == 3)) slot = 2 + if (uy >= -1 && uy <= 1) { + if (spread == 0) slot = INK + else if (open && spread <= 2 && !(spread == 2 && uy == -1)) slot = spread == 2 && uy == 1 ? CREAM : b < 0.5 ? 10 : 12 + else if (!open && spread == 1 && uy >= 0) slot = b < 0.5 ? 10 : 12 + } + } + + // Six motes drift across on the air, one pixel each: fireflies at dusk, pollen by day. Each blinks out now and then. + for (let m = 0; m < MOTES; m++) { + const from = grit(m, 11) + const level = grit(m, 29) + const speed = (from - 0.5) * 3 + const mx = floor(mod(from * W + time * speed, W)) + const my = floor(level * H + abs(fract(time * 0.11 + from) - 0.5) * 12) + if (x == mx && y == my && fract(time * 0.19 + level) < 0.72) slot = 13 + } + + const colour = mix(paint.at(slot), paint.at(slot + 16), day) + return vec4(colour.r / 255, colour.g / 255, colour.b / 255, print) +}) + +// The picture: each big pixel enlarged, with edges one screen pixel soft so they stay crisp at any sharpness. +const view = picture(p => { + const at = vec2(p.uv * draw.size) + const cell = vec2(floor(at)) + const part = vec2(at - cell) + const grow = PX / px + const lean = vec2(clamp(part * grow, 0, 0.5) - 0.5 + clamp((part - 1) * grow + 0.5, 0, 0.5)) + return vec4(draw.sample(vec2((cell + 0.5 + lean) / draw.size)).rgb, 1) +}) + +export default shader({ fps: 60, passes: [sim, draw, view] }) diff --git a/plugins/theme-examples/hooks/scenes/garden-beetles/still.ts b/plugins/theme-examples/hooks/scenes/garden-beetles/still.ts new file mode 100644 index 00000000..93caec9a --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/garden-beetles/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Garden Beetles, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function gardenBeetlesSvg(): string { + return ( + "\nGarden BeetlesPixel beetles and ladybirds on a paved garden path.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + ) +} diff --git a/plugins/theme-examples/hooks/scenes/koi-pond/scene.ts b/plugins/theme-examples/hooks/scenes/koi-pond/scene.ts new file mode 100644 index 00000000..eb2ac8fc --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/koi-pond/scene.ts @@ -0,0 +1,1315 @@ +// Koi Pond: a pond seen from above, with real travelling ripples, koi that each steer themselves, and feeding. +import { + abs, atan, buffer, clamp, composer, cos, dot, dt, exp, first, floor, fract, frame, int, length, max, min, mix, + mod, normalize, PI, picture, pointers, pow, px, res, scene, shader, shapes, sin, size, smoothstep, sqrt, step, + time, unsigned, values, vec2, vec3, vec4, +} from 'claude-code/shader' +import type { Buffer } from 'claude-code/shader' + +// One layout unit: a point in a 1512x945 window, smaller in a smaller one, so the pond keeps its proportions. +const unitOf = () => sqrt(size.x * size.y) / 1195 + +// Integer mixing of a lattice point and a seed: three odd multipliers folded in, then two rounds of +// xor-shift and multiply, and a last xor-shift. The constants and shifts give an even, unstreaked grain. +const mixI = (x: number, y: number, k: number) => { + let h = int(int(x) * 0x43e2d1dd + int(y) * 0x6d2a9e3b + int(k) * 0x2c7f0a8d) + h = int(h ^ (h >>> 14)) + h = int(h * 0x4e9d3a67) + h = int(h ^ (h >>> 11)) + h = int(h * 0x35a6c2d9) + h = int(h ^ (h >>> 17)) + return h +} +// Four numbers from 0 to 1 out of one mixed int, a byte each. +const four = (h: int): vec4 => vec4(vec4(unsigned(h & 255), unsigned((h >>> 8) & 255), unsigned((h >>> 16) & 255), unsigned(h >>> 24)) / 256) +const one = (x: number, y: number, k: number): number => unsigned(mixI(x, y, k)) / 4294967296 + +// Smooth lattice noise, 0 to 1. +const lattice = (at: vec2, seed: number): number => { + const corner = vec2(floor(at)) + const ease = vec2(smoothstep(0, 1, vec2(at - corner))) + const low = mix(one(corner.x, corner.y, seed), one(corner.x + 1, corner.y, seed), ease.x) + const high = mix(one(corner.x, corner.y + 1, seed), one(corner.x + 1, corner.y + 1, seed), ease.x) + return mix(low, high, ease.y) +} +// Three sizes of it laid over each other, for mottling. +const layered = (at: vec2, seed: number): number => lattice(at, seed) * 0.5 + lattice(vec2(at * 2.03 + 17), seed + 1) * 0.3 + lattice(vec2(at * 4.1 + 31), seed + 2) * 0.2 + +const SUN = vec2(-0.55, 0.83) + +// How far a point is from the stroke that runs from a to b: its offset across the stroke, and how far it +// overshoots either end along it. +const strokeAway = (pt: vec2, a: vec2, b: vec2): number => { + const run = vec2(b - a) + const len = length(run) + const along = vec2(run / max(len, 0.001)) + const rel = vec2(pt - a) + const t = dot(rel, along) + const across = rel.x * along.y - rel.y * along.x + const past = max(max(-t, t - len), 0) + return sqrt(across * across + past * past) +} + +// Where the bottom rises into shallow shelves: 1 on the shelves beside the text, 0 in the deep middle. +const shelfOf = (uv: vec2, wob: number): number => { + const l = 1 - smoothstep(0.1, 0.36, uv.x + wob - 0.05 * uv.y) + const r = 1 - smoothstep(0.08, 0.34, 1 - uv.x + wob - 0.08 * (1 - uv.y)) + return max(l, r) +} + +// Light and dark reach the shader only through the Svg under this picture, a still drawing of the same pond: +// its middle is plain water in the mode's own tone. +const modeBuf = buffer({ size: [1, 1], once: true }, p => { + const c = scene.sample(vec2(0.5, 0.5)) + const lum = dot(c.rgb, vec3(0.3, 0.6, 0.1)) + return vec4(smoothstep(0.15, 0.23, lum), 0, 0, 1) +}) + +// The bottom, painted once: silt, three sizes of stone, sunken maple leaves. Alpha holds the depth. +const STONE = [vec3(0.46, 0.46, 0.43), vec3(0.5, 0.44, 0.33), vec3(0.43, 0.32, 0.24), vec3(0.34, 0.38, 0.4), vec3(0.58, 0.55, 0.46), vec3(0.27, 0.27, 0.26)] +const stoneLayer = (col: vec3, q: vec2, cell: number, seed: number, u: number, thin: number): vec3 => { + const g = vec2(q / cell) + const gi = floor(g) + let best = 4 + let bd = vec2(1, 0) + let bp = vec4(0) + for (let j = -1; j <= 1; j++) { + for (let i = -1; i <= 1; i++) { + const cx = gi.x + i + const cy = gi.y + j + const r = four(mixI(cx, cy, seed)) + const c = vec2(cx + 0.25 + 0.5 * r.x, cy + 0.25 + 0.5 * r.y) + const dens = max(shelfOf(vec2(c * cell * u / size), 0.03 * sin(c.y * 1.7)), 0.13) * thin + const d = vec2(g - c) + const a = r.z * 3.1 + const ca = cos(a) + const sa = sin(a) + const e = vec2(ca * d.x + sa * d.y, (ca * d.y - sa * d.x) * (1.1 + 0.5 * r.w)) + const rad = 0.2 + 0.3 * r.w + const gone = step(dens, fract(r.x * 13.7 + r.y * 5.3)) + const t = length(e) / rad + gone * 9 + if (t < best) { + best = t + bd = d + bp = r + } + } + } + // No stone is an ellipse: its outline is pushed in and out, and its foot lies under the silt. + const grain = lattice(vec2(q / (cell * 0.06)), seed + 5) + const fuzz = lattice(vec2(q / (cell * 0.3)), seed + 7) + const rim = best + (fuzz - 0.5) * 0.34 + const inside = smoothstep(1, 0.8, rim) + const dn = normalize(bd) + const lam = dot(dn, SUN) * min(best, 1.6) + const dome = sqrt(max(0, 1 - best * best)) + const idx = floor(bp.w * 5.99) + const pal = idx < 1 ? STONE[0]! : idx < 2 ? STONE[1]! : idx < 3 ? STONE[2]! : idx < 4 ? STONE[3]! : idx < 5 ? STONE[4]! : STONE[5]! + let sc = vec3(pal * (0.84 + 0.1 * dome + 0.15 * lam) * (0.86 + 0.28 * grain)) + // Green fuzz furs the tops; silt buries the edges. + sc = mix(sc, vec3(vec3(0.29, 0.35, 0.19) * (0.75 + 0.5 * grain)), smoothstep(0.42, 0.8, fuzz) * 0.6) + sc = mix(sc, col, smoothstep(0.45, 1, rim) * 0.7) + const cast = smoothstep(1.5, 1, best) * (1 - inside) * clamp(0.5 - 0.6 * lam, 0, 1) + return vec3(mix(col * (1 - 0.34 * cast), sc, inside)) +} +const sunkLeaf = (col: vec3, q: vec2, seed: number): vec3 => { + const cell = 190 + const g = vec2(q / cell) + const gi = floor(g) + let out = col + for (let j = -1; j <= 0; j++) { + for (let i = -1; i <= 0; i++) { + const cx = gi.x + i + step(0.5, g.x - gi.x) + const cy = gi.y + j + step(0.5, g.y - gi.y) + const r = four(mixI(cx, cy, seed)) + const c = vec2(cx + 0.2 + 0.6 * r.x, cy + 0.2 + 0.6 * r.y) + const d = vec2((g - c) * cell) + const R = 15 + 12 * r.z + const rr = length(d) + const a = atan(d.y, d.x) - r.w * 6.3 + const lobe = abs(cos(a * 2.5)) + const edge = R * (0.3 + 0.7 * pow(lobe, 1.1)) * (0.62 + 0.38 * cos(a)) + const m = smoothstep(edge, edge - 1.5, rr) * step(fract(r.z * 9.1 + r.w * 3.3), 0.62) + const vein = pow(lobe, 20) * smoothstep(0.1, 0.5, rr / R) + const tone = mix(vec3(0.36, 0.23, 0.13), vec3(0.5, 0.36, 0.15), r.x) + const lc = vec3(tone * (0.85 + 0.3 * lattice(vec2(d / 5 + r.w * 40), 19)) + vein * 0.07) + const sh = smoothstep(edge + 4, edge - 1, length(vec2(d - vec2(1.5, -2)))) * 0.22 + out = vec3(mix(out * (1 - sh * (1 - m)), lc, m * 0.9)) + } + } + return out +} +const bed = buffer({ scale: 0.7, once: true, filter: 'mipmap' }, p => { + const u = unitOf() + const q = vec2(p.point / u) + const n0 = layered(vec2(q / 260), 11) + const sh = shelfOf(p.uv, (n0 - 0.5) * 0.14) + const n1 = layered(vec2(q / 64), 23) + const n2 = lattice(vec2(q / 7), 31) + let col = mix(vec3(0.3, 0.29, 0.21), vec3(0.41, 0.37, 0.26), n1) + col = mix(col, vec3(0.24, 0.29, 0.21), smoothstep(0.45, 0.8, n0) * 0.55) + col = vec3(col * (0.9 + 0.2 * n2) * (0.84 + 0.32 * n0)) + // Grit: pale and dark specks too small to be stones. + const grit = one(floor(q.x / 2.6), floor(q.y / 2.6), 37) + col = vec3(col * (1 + 0.2 * step(0.9, grit) - 0.2 * step(grit, 0.08))) + col = stoneLayer(col, q, 22, 3, u, 1.25) + col = stoneLayer(col, q, 58, 5, u, 0.8) + col = sunkLeaf(col, q, 7) + col = stoneLayer(col, q, 150, 9, u, 0.42) + // A dusting of silt settles over everything and ties the stones to the bottom. + col = mix(col, vec3(0.36, 0.34, 0.25), 0.12 + 0.2 * smoothstep(0.4, 0.9, n1) * (1 - sh)) + return vec4(col, clamp(0.76 - 0.56 * sh + 0.16 * (n0 - 0.5), 0, 1)) +}) + +// What lies on or over the surface, painted once, premultiplied: bank stones, pads, two lilies, reeds, +// duckweed, a maple bough. The picture lights it and drops its shadow into the water. +const over = (acc: vec4, rgb: vec3, a: number): vec4 => vec4(acc.rgb * (1 - a) + rgb * a, acc.a * (1 - a) + a) +const dim = (acc: vec4, k: number): vec4 => vec4(acc.rgb * (1 - k), acc.a) + +const pad = (acc: vec4, pt: vec2, c: vec2, r: number, notch: number, beads: number, u: number): vec4 => { + // Each pad has a character of its own, 0 to 1, which sets its squeeze, its green, its age and its notch. + const k = one(r, notch * 10, 7) + // No pad is a circle: each is squeezed a little one way or the other. + const d0 = vec2((pt - c) / u) + const d = vec2(d0.x / (1 - 0.2 * k), d0.y / (0.8 + 0.2 * k)) + const rr = length(d) + let out = acc + if (rr < r + 9) { + const ang = atan(d.y, d.x) + const da = abs(mod(ang - notch + PI, 2 * PI) - PI) + const rough = lattice(vec2(d / 9 + k * 23), 47) + const edge = r * (1 + 0.06 * sin(ang * 3 + k * 9) + 0.03 * sin(ang * 5 + k * 4) + 0.07 * (rough - 0.5)) + const slit = 1 - (1 - smoothstep(0.04 + 0.1 * k, 0.1 + 0.2 * k, da - 0.6 / (rr + 4))) * smoothstep(0.05 * r, 0.12 * r, rr) + // Something has had a bite out of the rim. + const off = abs(mod(ang - notch - 2.3 - k * 4 + PI, 2 * PI) - PI) + const nib = smoothstep(0.11 * r, 0.11 * r + 2.2, length(vec2(rr - 1.03 * r, off * r))) + const m = smoothstep(edge, edge - 2.2, rr) * slit * nib + const rel = rr / r + const vv = abs(sin((ang - notch) * 8)) + const vw = 7 / (rr + 6) + const vein = (1 - smoothstep(vw * 0.5, vw * 1.5, vv)) * smoothstep(1, 0.78, rel) * smoothstep(0.04, 0.2, rel) + const n = lattice(vec2(d / 22 + k * 31), 41) + const n2 = lattice(vec2(d / 5 + k * 17), 43) + const dn = normalize(d) + let g = mix(vec3(0.16, 0.31, 0.14), vec3(0.31, 0.43, 0.17), k) + g = vec3(g * (0.78 + 0.44 * n) * (1.08 - 0.2 * smoothstep(0.55, 1, rel))) + // An older leaf yellows and browns from the rim inward, and never evenly. + const old = smoothstep(0.55, 1.05, rel + 0.5 * (n - 0.5) + 0.35 * (k - 0.5)) + g = mix(g, vec3(0.47, 0.45, 0.17), old * 0.55) + g = mix(g, vec3(0.43, 0.53, 0.25), vein * 0.26) + g = mix(g, vec3(0.36, 0.27, 0.13), smoothstep(0.72, 0.86, n2) * 0.6) + g = mix(g, vec3(0.33, 0.22, 0.12), smoothstep(edge - 3.4, edge - 1, rr) * 0.3 * (0.4 + 1.2 * n)) + // The waxy top takes a broad soft shine from the sky on its sunward side. + const shine = smoothstep(1.1, 0.1, length(vec2(d / r - SUN * 0.4))) * (0.5 + 0.5 * rough) + g = vec3(g + vec3(0.13, 0.16, 0.15) * shine) + g = vec3(g + 0.1 * smoothstep(edge - 3, edge - 1.2, rr) * max(0, dot(dn, SUN))) + // Beads of water sit on some pads, one or two: a lens with a bright point and a dark foot. + for (const s of [1, -1]) { + if (beads >= 1.5 - 0.5 * s) { + const o = vec2(cos(notch + 1.9 * s + k * 3) * 0.5 * r, sin(notch + 1.9 * s + k * 3) * 0.46 * r) + const dd = length(vec2(d - o)) + const br = 2.6 + k * 1.6 + g = mix(g, vec3(g * 1.18 + 0.05), smoothstep(br, br - 0.8, dd)) + g = vec3(g * (1 - 0.35 * smoothstep(br + 0.9, br - 0.2, length(vec2(d - o - vec2(0.9, -1.2)))) * smoothstep(br - 0.9, br, dd))) + g = vec3(g + 0.75 * smoothstep(1.1, 0.3, length(vec2(d - o - vec2(-0.9, 1))))) + } + } + out = over(dim(out, smoothstep(edge + 6, edge - 1, length(vec2(d - vec2(2, -3)))) * 0.3 * (1 - m)), g, m) + } + return out +} + +const lily = (acc: vec4, pt: vec2, c: vec2, R: number, rot: number, pink: number, u: number): vec4 => { + const d = vec2((pt - c) / u) + const rr = length(d) / R + let out = acc + if (rr < 1.3) { + const ang = atan(d.y, d.x) + out = dim(out, 0.4 * smoothstep(1.25, 0.7, length(vec2(d - vec2(3, -4.5))) / R)) + for (const k of [0, 1, 2, 3]) { + const Rk = 1 - 0.23 * k + const pa = ang + rot + k * 0.41 + const cs = abs(cos(pa * (4 - 0.5 * k))) + const edge = Rk * (0.56 + 0.44 * pow(cs, 0.34)) + const m = smoothstep(edge, edge - 0.06, rr) + const t = rr / edge + const blush = mix(vec3(0.96, 0.62, 0.74), vec3(0.99, 0.96, 0.93), smoothstep(0.15, 0.8 - 0.5 * pink, t)) + const petal = vec3(blush * (0.8 + 0.2 * cs) * (0.84 + 0.16 * t) * (1 - 0.2 * smoothstep(0.84, 1, t))) + out = over(dim(out, 0.25 * smoothstep(edge + 0.12, edge, rr) * (1 - m)), petal, m) + } + const heart = smoothstep(0.26, 0.19, rr) + const dots = 0.8 + 0.2 * sin(ang * 14) * smoothstep(0.05, 0.15, rr) + out = over(out, vec3(vec3(0.97, 0.72, 0.15) * dots), heart) + } + return out +} + +const blade = (acc: vec4, pt: vec2, b: vec2, tip: vec2, bend: number, w: number, k: number): vec4 => { + const ax = vec2(tip - b) + const len = length(ax) + const dir = vec2(ax / len) + const r = vec2(pt - b) + const a = dot(r, dir) / len + const cr = r.x * dir.y - r.y * dir.x + let out = acc + if (a > -0.02 && a < 1.02) { + const off = bend * len * a * a + const hw = w * pow(max(1 - a, 0), 0.6) * min(1, 0.35 + a * 6) + const dd = abs(cr - off) - hw + const m = smoothstep(1.6, -1.6, dd) * step(0, a) * step(a, 1) + const mid = smoothstep(hw * 0.3, 0, abs(cr - off)) + const g0 = mix(vec3(0.2, 0.34, 0.13), vec3(0.45, 0.47, 0.18), a * a * (0.4 + 0.6 * k)) + const g = vec3(g0 * (0.84 + 0.26 * mid) * (0.88 + 0.24 * k) * (0.7 + 0.4 * a)) + out = over(dim(out, smoothstep(6, 0, abs(cr - off - 2.5) - hw) * 0.2 * (1 - m) * step(0, a) * step(a, 1)), g, m) + } + return out +} + +const mapleLeaf = (acc: vec4, pt: vec2, c: vec2, R: number, rot: number, hue: number, u: number): vec4 => { + const d = vec2((pt - c) / u) + const rr = length(d) + let out = acc + if (rr < R * 1.25) { + const a = atan(d.y, d.x) - rot + const lobe = abs(cos(a * 2.5)) + const edge = R * (0.3 + 0.7 * pow(lobe, 0.8 + hue)) * (0.62 + 0.38 * cos(a)) * (1 + 0.05 * sin(a * 24)) * (1 - 0.16 * hue * sin(a + hue * 9)) + const m = smoothstep(edge, edge - 2.6, rr) + const vein = pow(lobe, 26) * smoothstep(0.08, 0.4, rr / R) + const warm = mix(mix(vec3(0.74, 0.15, 0.1), vec3(0.9, 0.4, 0.1), smoothstep(0, 0.5, hue)), vec3(0.93, 0.68, 0.18), smoothstep(0.5, 1, hue)) + const worn = vec3(mix(warm, vec3(0.45, 0.33, 0.2), 0.45) * (0.84 + 0.26 * sin(a))) + const g = vec3(worn * (0.8 + 0.34 * lattice(vec2(d / 6 + rot * 7), 51)) * (1.06 - 0.26 * rr / R) * (1 + 0.3 * vein)) + out = over(dim(out, 0.28 * smoothstep(edge + 5, edge - 1, length(vec2(d - vec2(2, -3)))) * (1 - m)), g, m) + } + return out +} + +const rock = (acc: vec4, pt: vec2, c: vec2, rx: number, ry: number, rot: number, k: number, u: number): vec4 => { + const d0 = vec2((pt - c) / u) + const ca = cos(rot) + const sa = sin(rot) + const e = vec2((ca * d0.x + sa * d0.y) / rx, (ca * d0.y - sa * d0.x) / ry) + const t0 = length(e) + let out = acc + if (t0 < 1.3) { + const n = layered(vec2(d0 / 38 + k * 13), 61) + const t = t0 + (n - 0.5) * 0.24 + const m = smoothstep(1, 0.97, t) + const dome = sqrt(max(0, 1 - t * t)) + const lam = dot(normalize(d0), SUN) * min(t, 1) + const sp = lattice(vec2(d0 / 3.5), 63) + let g = vec3(mix(vec3(0.34, 0.34, 0.33), vec3(0.43, 0.41, 0.37), k) * (0.62 + 0.3 * dome + 0.3 * lam) * (0.88 + 0.24 * sp)) + const moss = smoothstep(0.46, 0.7, layered(vec2(d0 / 26 + 5 + k * 7), 67)) * smoothstep(0.98, 0.5, t) + g = mix(g, vec3(vec3(0.25, 0.36, 0.14) * (0.75 + 0.5 * sp)), moss * 0.85) + g = mix(g, vec3(g * 0.5), smoothstep(0.84, 1, t)) + out = over(out, g, m) + } + return out +} + +const top = buffer({ scale: 0.8, once: true, filter: 'mipmap' }, p => { + const u = unitOf() + const pt = p.point + const q = vec2(pt / u) + const at = (ax: number, ay: number, dx: number, dy: number): vec2 => vec2(ax * size.x + dx * u, ay * size.y + dy * u) + let acc = vec4(0) + + // Duckweed: tiny leaves packed into ragged mats along the banks and against the pad clusters; each + // frond is the nearest of a loose scatter and has a size and a green of its own, so no grid shows. + const ragged = layered(vec2(q / 70), 73) + const bank = smoothstep(0.09, 0.035, min(p.uv.x, 1 - p.uv.x) + 0.03 * sin(p.uv.y * 11)) + const byPads = max(smoothstep(215, 120, length(vec2(pt - at(0, 1, 170, -225))) / u), smoothstep(215, 120, length(vec2(pt - at(1, 0, -200, 235))) / u)) + const raft = smoothstep(0.46, 0.6, ragged) * max(bank, byPads * smoothstep(0.5, 0.62, ragged)) + const wq = vec2(q / 5) + const wi = floor(wq) + let nearest = 9 + let frond = vec4(0) + for (let j = -1; j <= 1; j++) { + for (let i = -1; i <= 1; i++) { + const r = four(mixI(wi.x + i, wi.y + j, 71)) + const away = length(vec2(wq - wi - vec2(i, j) - 0.1 - 0.8 * r.xy)) / (0.24 + 0.34 * r.z) + if (away < nearest) { + nearest = away + frond = r + } + } + } + const speck = smoothstep(1, 0.72, nearest) * step(fract(frond.w * 7.7 + frond.z * 3.1), raft) + acc = over(acc, vec3(mix(vec3(0.24, 0.35, 0.14), vec3(0.4, 0.46, 0.2), frond.w) * (0.78 + 0.4 * frond.z)), speck) + + // Pollen and dust lie on the water in a thin scatter, thicker toward the banks. + const fq = vec2(q / 15) + const fc = floor(fq) + const fr = four(mixI(fc.x, fc.y, 111)) + const dust = smoothstep(0.1 + 0.06 * fr.z, 0.04, length(vec2(fq - fc - 0.2 - 0.6 * fr.xy))) * step(fr.w, 0.015 + 0.3 * smoothstep(0.3, 0.95, shelfOf(p.uv, 0))) + acc = over(acc, vec3(0.8, 0.78, 0.6), dust * 0.3) + + // Lily pads, upper left and lower right, and two strays. + acc = pad(acc, pt, at(0, 1, 150, -175), 74, 0.6, 1, u) + acc = pad(acc, pt, at(0, 1, 92, -318), 58, 2.4, 0, u) + acc = pad(acc, pt, at(0, 1, 268, -118), 52, 4.1, 2, u) + acc = pad(acc, pt, at(0, 1, 222, -272), 40, 5.4, 0, u) + acc = lily(acc, pt, at(0, 1, 214, -196), 31, 0.3, 0.35, u) + acc = pad(acc, pt, at(1, 0, -145, 172), 80, 3.6, 1, u) + acc = pad(acc, pt, at(1, 0, -92, 330), 55, 5.9, 0, u) + acc = pad(acc, pt, at(1, 0, -278, 128), 49, 0.9, 1, u) + acc = pad(acc, pt, at(1, 0, -236, 262), 37, 2.2, 0, u) + acc = lily(acc, pt, at(1, 0, -212, 200), 25, 1.1, 0.9, u) + acc = pad(acc, pt, at(0, 0, 262, 196), 33, 1.9, 0, u) + acc = pad(acc, pt, at(1, 1, -300, -96), 41, 3.1, 1, u) + + // Bank stones in two corners. + acc = rock(acc, pt, at(0, 0, 196, -14), 92, 62, 0.3, 0.2, u) + acc = rock(acc, pt, at(0, 0, 22, 52), 118, 96, -0.4, 0.8, u) + acc = rock(acc, pt, at(1, 1, -212, 24), 84, 58, 0.2, 0.6, u) + acc = rock(acc, pt, at(1, 1, -28, -36), 112, 92, 0.5, 0.1, u) + + // Reeds lean out from the lower left bank. + acc = blade(acc, pt, at(0, 0, 62, 96), at(0, 0, 238, 318), 0.16, 5.5 * u, 0.2) + acc = blade(acc, pt, at(0, 0, 38, 120), at(0, 0, 118, 402), -0.12, 6 * u, 0.7) + acc = blade(acc, pt, at(0, 0, 96, 78), at(0, 0, 322, 226), 0.2, 5 * u, 0.5) + acc = blade(acc, pt, at(0, 0, 20, 134), at(0, 0, 44, 372), 0.1, 5 * u, 0.9) + acc = blade(acc, pt, at(0, 0, 118, 54), at(0, 0, 356, 132), -0.14, 4.6 * u, 0.35) + acc = blade(acc, pt, at(0, 0, 74, 110), at(0, 0, 176, 352), 0.05, 4.4 * u, 1) + acc = blade(acc, pt, at(0, 0, 140, 40), at(0, 0, 300, 60), 0.25, 4 * u, 0.6) + + // A maple bough reaches in over the upper right corner. + const b0 = at(1, 1, -40, -70) + const b1 = at(1, 1, -330, -208) + const bx = vec2(b1 - b0) + const bl = length(bx) + const bdir = vec2(bx / bl) + const br = vec2(pt - b0) + const ba = clamp(dot(br, bdir) / bl, 0, 1) + const bcr = abs(br.x * bdir.y - br.y * bdir.x - sin(ba * 5) * 9 * u) - (7 - 4.5 * ba) * u + acc = over(acc, vec3(0.17, 0.13, 0.1), smoothstep(3.5 * u, -3.5 * u, bcr) * step(dot(br, bdir), bl) * 0.9) + const LEAVES = [ + [-86, -120, 38, 0.5, 0.2], [-150, -150, 24, 3.6, 0.05], [-196, -118, 32, 1.2, 0.8], [-256, -146, 20, 2.9, 0.15], + [-276, -206, 36, 0.2, 0.65], [-336, -236, 26, 3.9, 0.95], [-104, -176, 18, 5.7, 0.45], [-236, -228, 28, 2.5, 0.7], + [-388, -262, 22, 0.9, 0.6], + ] + for (const l of LEAVES) { + acc = mapleLeaf(acc, pt, at(1, 1, l[0]!, l[1]!), l[2]!, l[3]!, l[4]!, u) + } + return acc +}) + +// The session: what a mod may tell the pond about the session it sits behind, as the `values` of its Shader +// element. A few slow states; a Shader element given none reads every one as 0, and the pond is as it always is. +// busy: a turn is running. pace: how busy it is, 0 to 4 (the pond answers up to 3). waiting: the person is being +// waited for. making: something has just been written. trouble: something failed and is not yet mended. +// helpers: how many helpers are at work, up to 4. +const session = values({ busy: [0, 1], pace: [0, 4], waiting: [0, 1], making: [0, 1], trouble: [0, 1], helpers: [0, 4] }) + +// What the pond keeps of the session from frame to frame, in two texels. +// Texel 0. x: was a turn running a frame ago. y: seconds since a turn last ended. z: a clock that ticks once +// for each midge that touches the water, faster as the pace rises. w: a clock that ticks once for each leaf +// that falls from the bough while something is being written. +// Texel 1. x: was there trouble a frame ago. y: seconds since trouble last cleared. +const mood: Buffer = buffer({ size: [2, 1], float: 32, filter: 'nearest' }, (p): vec4 => { + const m = mood.at(vec2(0, 0)) + const t = mood.at(vec2(1, 0)) + const isBusy = step(0.5, session.busy) + const inTrouble = step(0.5, session.trouble) + let since = m.x > 0.5 && isBusy < 0.5 ? 0 : min(m.y + dt, 99) + let mended = t.x > 0.5 && inTrouble < 0.5 ? 0 : min(t.y + dt, 99) + // Work goes on while a turn runs and while helpers are at work. (The pace lingers after a turn ends, so it + // only says how lively the work is, not whether there is any.) + const working = max(isBusy, step(0.5, session.helpers)) + let midge = mod(m.z + dt * working * (0.16 + 0.18 * min(session.pace, 3)), 4096) + let leaf = mod(m.w + dt * 0.2 * step(0.5, session.making), 4096) + if (first) { + since = 99 + mended = 99 + midge = 0 + leaf = 0 + } + return p.cell.x < 0.5 ? vec4(isBusy, since, midge, leaf) : vec4(inTrouble, mended, 0, 0) +}) + +// The koi steer themselves: a tiny buffer holds each one's place, heading, speed and mood from frame to frame. +const pick7 = (l: readonly T[], i: number): T => (i < 0.5 ? l[0]! : i < 1.5 ? l[1]! : i < 2.5 ? l[2]! : i < 3.5 ? l[3]! : i < 4.5 ? l[4]! : i < 5.5 ? l[5]! : l[6]!) +const K_LEN = [235, 205, 180, 165, 150, 135, 105] +const K_CRUISE = [30, 34, 38, 40, 42, 44, 50] +const K_HOME = [vec2(0.15, 0.42), vec2(0.86, 0.6), vec2(0.5, 0.5), vec2(0.2, 0.64), vec2(0.81, 0.36), vec2(0.5, 0.44), vec2(0.5, 0.56)] +const K_AMP = [vec2(0.09, 0.3), vec2(0.08, 0.28), vec2(0.42, 0.3), vec2(0.13, 0.25), vec2(0.12, 0.25), vec2(0.4, 0.32), vec2(0.44, 0.36)] +// Where each starts (as shares of the window) and which way it faces: the same poses the still Svg shows. +const K_INIT = [vec3(0.16, 0.42, 1.2), vec3(0.86, 0.62, -1.9), vec3(0.7, 0.27, 2.8), vec3(0.25, 0.72, 0.3), vec3(0.79, 0.36, 2.4), vec3(0.33, 0.2, 0.5), vec3(0.63, 0.8, 3.4)] +// How fast a ring travels, in layout units a second: unhurried water. Each push on it sends a single ring, +// not a train. +const SPEED_C = 84 + +const fish: Buffer = buffer({ size: [8, 4], float: 32, filter: 'nearest' }, (p): vec4 => { + const u = unitOf() + const i = p.cell.x + const s0 = fish.at(vec2(i, 0)) + const s1 = fish.at(vec2(i, 1)) + const L = pick7(K_LEN, i) * u + let pos = vec2(s0.x, s0.y) + let th = s0.z + let v = s0.w + let ph = s1.x + let trn = s1.y + let gulp = s1.z + let rise = s1.w + // The message box, and this koi's place at it (used when the person is waited for). The app's text lies + // over the middle of the pond like frosted glass, so the four koi that keep to the banks take places in + // the open water at the two sides of it, and the three roamers a fan above the box itself. All of them + // face the box. + const known = step(0.001, composer.z - composer.x) + const boxL = mix(0.257, composer.x, known) * size.x + const boxR = mix(0.743, composer.z, known) * size.x + const boxTop = mix(0.13, composer.w, known) * size.y + const boxMid = vec2((boxL + boxR) * 0.5, boxTop) + const fanR = (boxR - boxL) * 0.3 + const slotAt = pick7( + [ + vec2(boxL - 34 * u, boxTop + 165 * u), + vec2(boxR + 34 * u, boxTop + 205 * u), + vec2(boxMid + vec2(0, fanR)), + vec2(boxL - 22 * u, boxTop + 330 * u), + vec2(boxR + 22 * u, boxTop + 365 * u), + vec2(boxMid + vec2(0.5, 0.8) * fanR), + vec2(boxMid + vec2(-0.5, 0.8) * fanR), + ], + i, + ) + const faceBox = vec2(normalize(vec2(boxMid - slotAt))) + if (first) { + // A start in the middle of a wait (the engine starts a scene afresh now and then) begins with the koi + // already at the box, so nothing is seen to jump. + const init = pick7(K_INIT, i) + const atBox = step(0.5, session.waiting) + pos = vec2(mix(vec2(init.xy * size), vec2(slotAt - faceBox * L * 0.33), atBox)) + th = mix(init.z, atan(faceBox.y, faceBox.x), atBox) + v = 8 * u + ph = i * 1.3 + trn = 0 + gulp = 0 + rise = pick7([0.62, 0.3, 0.7, 0.78, 0.5, 0.2, 0.84], i) + } + const dir = vec2(cos(th), sin(th)) + const mouth = vec2(pos + dir * L * 0.33) + // A slow waypoint on a closed path of its own; the koi follows it, and idles when it has caught up. + const home = pick7(K_HOME, i) + const amp = pick7(K_AMP, i) + const idleWp = vec2(home + amp * vec2(sin(time * (0.043 + 0.009 * i) + i * 1.7), sin(time * (0.061 + 0.007 * i) + i * 2.9))) + // While work goes on the koi have somewhere to be: they string out nose to tail on one slow circuit of the pond. + const round = time * 0.11 - i * 0.62 + const working = max(step(0.5, session.busy), step(0.5, session.helpers)) + const wp = vec2(mix(idleWp, vec2(0.5 + 0.37 * cos(round), 0.5 + 0.35 * sin(round)), working) * size) + let goal = wp + let want = pick7(K_CRUISE, i) * u * (1 + 0.15 * working) * smoothstep(30 * u, 230 * u, length(vec2(wp - pos))) + 6 * u + // Food: each koi fancies a pellet by how near it is, with a taste of its own so they do not all pick the same one. + let bestD = 90000 + let foodAt = vec2(0) + let hasFood = 0 + let bite = 0 + for (let k = 0; k < 15; k++) { + const f = food.at(vec2(k, 0)) + // A pellet is 1. A midge a koi would take is 0.9, and it is worth only a short swim; one at 0.8 will fly off. + // The koi take it in turns to go for food: four of the seven at a time, and while work goes on only two + // or three leave the circuit. Seven at one spot is a scrum, and a scrum flickers. + const alive = step(0.85, f.w) * mix(step(mod(i + floor(time / 9), 7), 3.5), step(mod(i + floor(time / 7), 3), 0.5), working) + const dm = length(vec2(f.xy - mouth)) + // While work goes on, the koi that keep to a bank do not leave the circuit to cross the pond for + // food; the roamers still go anywhere for it. + const reach = pick7([300, 300, 760, 380, 380, 760, 760], i) * u + const d = dm + one(i, k, 5) * 170 * u + (1 - alive) * 90000 + step(f.w, 0.95) * step(420 * u, dm) * 90000 + step(reach, dm) * 90000 * working + bite = max(bite, step(0.25, f.w) * step(dm, 15 * u)) + if (d < bestD) { + bestD = d + foodAt = f.xy + hasFood = alive + } + } + const dFood = length(vec2(foodAt - mouth)) + if (hasFood > 0.5) { + goal = vec2(foodAt - dir * L * 0.3) + want = mix(18, mix(60, 52, working), smoothstep(15 * u, 190 * u, dFood)) * u + } + // The session. The message box is the bank where a person stands. When the person is waited for, every koi + // comes and hovers in a fan round the box, facing it, mouths up, as pond koi do for someone at the edge. + // Food still comes first. + const called = step(0.5, session.waiting) * (1 - hasFood) + const toSlot = length(vec2(slotAt - mouth)) + const there = called * smoothstep(90 * u, 25 * u, toSlot) + goal = mix(goal, vec2(slotAt - dir * L * 0.3), called) + want = mix(want, 95 * u * smoothstep(12 * u, 260 * u, toSlot), called) + // When a turn ends, the gold one rolls at the surface, and then they all glide off from the box for a while. + const since = mood.at(vec2(0, 0)).y + const roll = (i > 1.5 && i < 2.5 ? 1 : 0) * step(since, 1.3) * sin(PI * clamp(since / 1.3, 0, 1)) + const leaving = smoothstep(0.9, 1.6, since) * smoothstep(7, 4, since) * (1 - hasFood) + const fromBox = vec2(pos - boxMid) + goal = vec2(goal + (fromBox / max(length(fromBox), 1)) * 300 * u * leaving) + want = want + 26 * u * leaving + // A fast hovering pointer makes a near koi shy away. + let flee = vec2(0) + for (const f of pointers) { + const live = (1 - step(0.0001, f.gone)) * (1 - f.down) + const sp = length(vec2(f.at - f.was)) / max(dt, 0.004) + const away = vec2(pos - f.at) + const dd = length(away) + const k = live * smoothstep(150, 800, sp) * smoothstep(200 * u, 70 * u, dd) + flee = vec2(flee + (away / max(dd, 1)) * k) + } + const fear = min(length(flee), 1) + // Keep a little room from the others, less so at the food. + let sep = vec2(0) + for (let j = 0; j < 7; j++) { + const o = fish.at(vec2(j, 0)) + const dv = vec2(pos - o.xy) + const dd = length(dv) + const reach = (L + pick7(K_LEN, j) * u) * 0.32 + const k = smoothstep(reach, reach * 0.3, dd) * step(0.5, abs(j - i)) + sep = vec2(sep + (dv / max(dd, 1)) * k) + } + const mrg = 30 * u + const inward = vec2( + smoothstep(mrg, -mrg, pos.x) - smoothstep(size.x - mrg, size.x + mrg, pos.x), + smoothstep(mrg, -mrg, pos.y) - smoothstep(size.y - mrg, size.y + mrg, pos.y), + ) + const to = vec2(goal - pos) + const toBox = vec2(boxMid - pos) + const toN = vec2(mix(vec2(to / max(length(to), 1)), vec2(toBox / max(length(toBox), 1)), there)) + // A lazy swerve so that no path is a straight line. + const wa = (0.6 * sin(time * 0.21 + i * 2.3) + 0.35 * sin(time * 0.37 + i * 4.1)) * (1 - hasFood) * (1 - called) + const cw = cos(wa) + const sw = sin(wa) + const des = vec2(vec2(toN.x * cw - toN.y * sw, toN.x * sw + toN.y * cw) + sep * (1.1 - mix(0.4, 0.1, working) * hasFood) + flee * 2.5 + inward * 3) + const err = atan(dir.x * des.y - dir.y * des.x, dot(dir, des)) + // A koi going for food turns no faster than one at ease: a big fish that swings half round on the spot sweeps its + // markings across the water faster than is kind to the eye. + const maxT = (0.8 + 1.4 * fear) * (0.6 + (70 * u) / L) + const tgt = clamp(err * 2, -maxT, maxT) + trn = trn + (tgt - trn) * (1 - exp(-dt * 3.5)) + th = mod(th + trn * dt + PI, 2 * PI) - PI + const down = step(0.5, session.trouble) * (1 - hasFood) + want = (want + fear * 70 * u) * (1 - 0.6 * down) + v = v + (want * (0.8 + 0.2 * sin(ph * 0.5)) - v) * (1 - exp(-dt * 1.3)) + pos = vec2(pos + dir * v * dt) + // A slow beat: big koi are languid, and a tail that sweeps less often is also kinder to the eye. + ph = mod(ph + dt * (1.8 + (v / u) * 0.018), 4 * PI) + // A waiting koi opens its mouth at the box now and then. + const beg = there * step(0.992, sin(time * 0.7 + i * 2.4)) + // Trouble sends every koi down, dim and slow. When it is mended they come up one after another, and each + // takes a gulp of air at the top: a line of rings across the pond. + const mended = mood.at(vec2(1, 0)).y + const cheer = step(0.25 + 0.16 * i, mended) * step(mended, 0.4 + 0.16 * i) + gulp = max(gulp - dt / 0.8, max(bite, max(beg, cheer))) + const calm = pick7([0.62, 0.3, 0.7, 0.78, 0.5, 0.2, 0.84], i) + 0.14 * sin(time * 0.13 + i * 1.9) + const up = step(mended, 1.8) + const riseTo = mix(mix(mix(calm, 1, hasFood * smoothstep(210 * u, 60 * u, dFood)), 1, max(max(there * 0.9, roll), up)), 0.04, down) + // Going down for trouble and coming up from it are slow, so trouble that comes and goes quickly barely shows. + rise = rise + (riseTo - rise) * (1 - exp(-dt * mix(1.4, 0.45, max(down, up)))) + // Row 2, for the water: where the mouth is now, the kick of a gulp, and the push of a back breaking the surface. + const splash = 24 * step(0.5, roll + step(since, 0.45) * (i > 1.5 && i < 2.5 ? 1 : 0)) * step(since, 0.45) * sin(since * 7) + const lips = vec4(pos + vec2(cos(th), sin(th)) * L * 0.34, 9 * step(0.8, gulp) + splash, (1.3 * smoothstep(0.6, 1, rise) * v) / (90 * u)) + // Row 3: how far through its roll, and whether it has reached its place at the box. + return p.cell.y < 0.5 ? vec4(pos, th, v) : p.cell.y < 1.5 ? vec4(ph, trn, gulp, rise) : p.cell.y < 2.5 ? lips : vec4(roll, there, 0, 0) +}) + +// Food: fifteen slots in a ring (x, y, age, state) and a counter in the last texel. +// State 1 is a pellet afloat, 0.5 is one being swallowed, 0 is gone. While work goes on, midges touch the water +// in the margins and take slots too: 0.9 is one a koi will rise to, 0.8 is one that flies off again. +const food: Buffer = buffer({ size: [16, 1], float: 32, filter: 'nearest' }, (p): vec4 => { + const u = unitOf() + const k = p.cell.x + const f = food.at(vec2(k, 0)) + const cnt = food.at(vec2(15, 0)).x + let pos = vec2(f.x, f.y) + let age = f.z + dt + let state = f.w + let spawned = 0 + const tick = floor(mood.at(vec2(0, 0)).z) + const newMidge = step(0.5, tick - food.at(vec2(15, 0)).y) + for (const fg of pointers) { + const live = (1 - step(0.0001, fg.gone)) * fg.down * (1 - fg.over) + const n0 = floor(fg.age / 0.7) + const n1 = floor((fg.age - dt) / 0.7) + const many = live * step(0.5, n0 - n1) * (n0 < 0.5 ? 3 : 1) + const rel = mod(k - cnt - spawned, 15) + if (rel < many - 0.5 && k < 14.5) { + const jit = vec2(one(frame, k, 3) - 0.5, one(frame, k, 4) - 0.5) + pos = vec2(fg.at + jit * 46 * u * step(0.5, rel) + jit * 8 * u) + age = 0 + state = 1 + } + spawned = spawned + many + } + const relM = mod(k - cnt - spawned, 15) + if (relM < newMidge - 0.5 && k < 14.5) { + const side = one(tick, 1, 61) + pos = vec2(vec2(side < 0.5 ? 0.04 + 0.4 * side : 1.16 - 0.4 * side, 0.14 + 0.72 * one(tick, 2, 61)) * size) + age = 0 + state = one(tick, 3, 61) < 0.55 ? 0.9 : 0.8 + } + spawned = spawned + newMidge + // A midge does not stay: it is taken within a few seconds or it is gone. + if (state > 0.75 && state < 0.95 && age > 4.5) { + state = 0 + } + // A pellet near a mouth is taken; it lingers half a second as it goes down. + let taken = 0 + for (let j = 0; j < 7; j++) { + const s = fish.at(vec2(j, 0)) + const mouth = vec2(s.xy + vec2(cos(s.z), sin(s.z)) * pick7(K_LEN, j) * u * 0.33) + taken = max(taken, step(length(vec2(mouth - pos)), 12 * u)) + } + if (state > 0.85 && (taken > 0.5 || age > 45)) { + state = 0.5 + age = 0 + } + if (state < 0.75 && state > 0.25 && age > 0.45) { + state = 0 + } + // Pellets ride the ripples a little and drift with a slow eddy. + const uvp = vec2(pos / size) + const e = vec2(1.5 / wave.size.x, 1.5 / wave.size.y) + const gx = wave.sample(vec2(uvp + vec2(e.x, 0))).r - wave.sample(vec2(uvp - vec2(e.x, 0))).r + const gy = wave.sample(vec2(uvp + vec2(0, e.y))).r - wave.sample(vec2(uvp - vec2(0, e.y))).r + const eddy = vec2(sin(pos.y / (140 * u) + time * 0.11), cos(pos.x / (170 * u) - time * 0.09)) + pos = vec2(pos + (eddy * 2.2 * u - vec2(gx, gy) * 26 * u) * dt) + return k > 14.5 ? vec4(mod(cnt + spawned, 15), tick, 0, 0) : vec4(pos, age, state) +}) + +// The small players are placed once a frame in a strip of texels: two water striders (x, y, kick) and five +// fireflies (x, y, glow). A strider skates from one spot to the next in short hops; a firefly drifts on a slow +// loop of its own and glows and fades. +const FLY = [vec3(0.1, 0.42, 1), vec3(0.2, 0.72, 2.3), vec3(0.84, 0.24, 3.9), vec3(0.9, 0.7, 5.1), vec3(0.27, 0.2, 0.4)] +const cast = buffer({ size: [8, 1], float: 32, filter: 'nearest' }, p => { + const u = unitOf() + const k = p.cell.x + const every = 4.3 + 1.8 * k + const beat = time / every + 0.37 * k + const n = floor(beat) + const fr = (beat - n) * every + const from = vec2(one(n, 1, 43 + k), one(n, 2, 43 + k)) + const to = vec2(one(n + 1, 1, 43 + k), one(n + 1, 2, 43 + k)) + const a = vec2(mix(from, to, smoothstep(0, 0.45, fr))) + // Keep striders to the margins: fold the width out to the two sides. + const x = a.x < 0.5 ? a.x * 0.44 + 0.02 : 1 - (1 - a.x) * 0.44 - 0.02 + const strider = vec4(vec2(x, 0.1 + 0.8 * a.y) * size, step(fr, 0.1) + step(0.45, fr) * step(fr, 0.55), 0) + const f = k < 2.5 ? FLY[0]! : k < 3.5 ? FLY[1]! : k < 4.5 ? FLY[2]! : k < 5.5 ? FLY[3]! : FLY[4]! + const at = vec2(vec2(f.x, f.y) * size + vec2(sin(time * 0.23 + f.z * 3), cos(time * 0.17 + f.z * 5)) * 46 * u) + const fly = vec4(at, smoothstep(-0.2, 0.9, sin(time * 0.9 + f.z * 2.1)), 0) + return k < 1.5 ? strider : fly +}) +// Where and when the last four presses began (x, y, time, strength), and a counter in the last texel. A +// press is a plop at the place it began, and its rings leave that place whether the pointer stays or lifts. +const plops: Buffer = buffer({ size: [5, 1], float: 32, filter: 'nearest' }, (p): vec4 => { + const k = p.cell.x + const old = plops.at(vec2(k, 0)) + const cnt = plops.at(vec2(4, 0)).x + let made = 0 + let out = first ? vec4(0, 0, -99, 0) : old + for (const f of pointers) { + const live = (1 - step(0.0001, f.gone)) * f.down + // A pointer is new from the moment it presses: its age is 0 on the frame of the press and on no later one. + const began = live * (1 - step(0.5 * dt, f.age)) + // A pressed pointer that leaves the circle of 45 units round its own press (the press as old as the + // pointer) is dragging, and that press sends nothing more: what is left of its plop, and its two later + // echoes (see stir), are dropped. A pointer that was never inside the circle drops nothing, and neither + // does one that came down more than 0.08 seconds before or after the press. Two that come down together + // cannot be told apart: either drops the other's press too if it leaves that press's circle. + const reach = 45 * unitOf() + if (live > 0.5 && began < 0.5 && abs(time - f.age - out.z) < 0.08 && length(vec2(f.was - out.xy)) <= reach && length(vec2(f.at - out.xy)) > reach && time - out.z < 2.2) { + out = vec4(out.xy, out.z, 0) + } + if (began > 0.5 && abs(mod(cnt + made, 4) - k) < 0.5) { + out = vec4(f.at, time, 1 - 0.5 * f.over) + } + made = made + began + } + return k > 3.5 ? vec4(first ? 0 : mod(cnt + made, 4), 0, 0, 0) : out +}) + +// What stirs the water this frame, gathered once: pointers, mouths, the striders' feet. +const stir = buffer({ scale: 0.2, float: 16, filter: 'nearest' }, p => { + const u = unitOf() + let push = 0 + for (const f of pointers) { + const live = 1 - step(0.0001, f.gone) + const ds = strokeAway(p.point, f.was, f.at) / (22 * u) + const sp = length(vec2(f.at - f.was)) / max(dt, 0.004) + const soft = 1 - 0.5 * f.over + // A steady, gentle push, so a drag leaves one wake and no train of rings. + push = push - 6.5 * smoothstep(20, 500, sp) * live * soft * exp(-ds * ds) + } + // A press is a plop and two echoes of it, each weaker and nine tenths of a second after the last: three + // rings leave the spot, stately, with clear water between them. Each lifts as much water as it presses + // down, so no bowl stays. + for (const k of [0, 1, 2, 3]) { + const pl = plops.at(vec2(k, 0)) + const age = time - pl.z + const beat = step(age, 0.32) * sin(age * 9.8) + 0.6 * step(0.9, age) * step(age, 1.22) * sin((age - 0.9) * 9.8) + 0.36 * step(1.8, age) * step(age, 2.12) * sin((age - 1.8) * 9.8) + const d0 = length(vec2(p.point - pl.xy)) / (21 * u) + push = push - 68 * pl.w * beat * (1 - d0 * d0) * exp(-d0 * d0) + } + for (let j = 0; j < 7; j++) { + const lips = fish.at(vec2(j, 2)) + const dm = length(vec2(p.point - lips.xy)) / (20 * u) + push = push - (lips.z * (1 - dm * dm) + lips.w) * exp(-dm * dm) + } + for (const k of [0, 1]) { + const s = cast.at(vec2(k, 0)) + const dd = length(vec2(p.point - s.xy)) / (17 * u) + push = push - 2 * s.z * exp(-dd * dd) + } + // A midge's touch: a small clean ring where the newest one has just landed. + const newest = food.at(vec2(mod(food.at(vec2(15, 0)).x + 14, 15), 0)) + const dn = length(vec2(p.point - newest.xy)) / (16 * u) + push = push - 16 * step(newest.z, 0.1) * step(0.75, newest.w) * step(newest.w, 0.95) * (1 - dn * dn) * exp(-dn * dn) + // A leaf that has just dropped from the bough. + const lk = mod(floor(mood.at(vec2(0, 0)).w), 4) + const dl = length(vec2(p.point - afloat.at(vec2(lk, 0)).xy)) / (20 * u) + push = push - 18 * step(afloat.at(vec2(lk, 1)).z, 0.12) * (1 - dl * dl) * exp(-dl * dl) + return vec4(push, 0, 0, 1) +}) + +// The water: the wave equation on a grid of five-point cells. r is height, g is its speed, b and a its slope. +const wave: Buffer = buffer({ scale: 0.2, float: 16, filter: 'linear', repeat: 2 }, (p): vec4 => { + const u = unitOf() + const c = wave.at(p.cell) + const l = wave.at(vec2(p.cell - vec2(1, 0))).r + const r = wave.at(vec2(p.cell + vec2(1, 0))).r + const d = wave.at(vec2(p.cell - vec2(0, 1))).r + const t = wave.at(vec2(p.cell + vec2(0, 1))).r + const tl = wave.at(vec2(p.cell + vec2(-1, 1))).r + const tr = wave.at(vec2(p.cell + vec2(1, 1))).r + const bl = wave.at(vec2(p.cell + vec2(-1, -1))).r + const br = wave.at(vec2(p.cell + vec2(1, -1))).r + // All eight neighbours, the four nearest weighing four times the corners: rings come out round, not square. + const lap = (4 * (l + r + d + t) + tl + tr + bl + br - 20 * c.r) / 6 + const cs = SPEED_C * u + // Half a frame a turn, never more than the grid can carry, so a slow frame slows the water and cannot break it. + const tau = min(dt / 2, (0.62 * px) / cs) + const k2 = (cs * cs) / (px * px) + const ed = min(min(p.cell.x, res.x - 1 - p.cell.x), min(p.cell.y, res.y - 1 - p.cell.y)) + const soak = 1 - smoothstep(0, 9, ed) + let vel = (c.g + k2 * lap * tau) * exp(-tau * (0.4 + 7 * soak)) + const push = stir.at(p.cell).r + const h = clamp(c.r + (vel + push) * tau + 1.5 * tau * lap, -1.5, 1.5) + // b and a carry the slope across and up, taken over all eight neighbours so that it turns smoothly; the + // picture reads the slope from here. + return vec4(h, vel, (2 * (r - l) + tr - tl + br - bl) / 4, (2 * (t - d) + tl - bl + tr - br) / 4) +}) + +// Fallen leaves afloat: four maple leaves that drift on a slow eddy, slide down the face of a ripple, +// and are carried off when a pointer stirs the water beside them. Row 0 is place and speed; row 1 is +// turn, spin, seconds since it landed, and the last tick of the leaf clock it saw. +const pick4 = (l: readonly T[], i: number): T => (i < 0.5 ? l[0]! : i < 1.5 ? l[1]! : i < 2.5 ? l[2]! : l[3]!) +const LEAF_AT = [vec3(0.7, 0.65, 2.2), vec3(0.92, 0.54, 4.9), vec3(0.09, 0.44, 0.7), vec3(0.27, 0.12, 3.3)] +const afloat: Buffer = buffer({ size: [4, 2], float: 32, filter: 'nearest' }, (p): vec4 => { + const u = unitOf() + const k = p.cell.x + const s = afloat.at(vec2(k, 0)) + const r = afloat.at(vec2(k, 1)) + let pos = vec2(s.x, s.y) + let vel = vec2(s.z, s.w) + let ang = r.x + let spin = r.y + let landed = r.z + dt + if (first) { + const init = pick4(LEAF_AT, k) + pos = vec2(init.xy * size) + vel = vec2(0) + ang = init.z + spin = 0 + landed = 99 + } + // While something is being written, a leaf lets go of the bough now and then: the four take it in turn to + // be that leaf, and land on the water under it. + const fall = floor(mood.at(vec2(0, 0)).w) + if (fall > r.w + 0.5 && abs(mod(fall, 4) - k) < 0.5) { + pos = vec2(vec2(0.76 + 0.18 * one(fall, 1, 67), 0.66 + 0.2 * one(fall, 2, 67)) * size) + vel = vec2(0) + landed = 0 + } + const uvp = vec2(pos / size) + const e = vec2(1.5 / wave.size.x, 1.5 / wave.size.y) + const gx = wave.sample(vec2(uvp + vec2(e.x, 0))).r - wave.sample(vec2(uvp - vec2(e.x, 0))).r + const gy = wave.sample(vec2(uvp + vec2(0, e.y))).r - wave.sample(vec2(uvp - vec2(0, e.y))).r + const eddy = vec2(sin(pos.y / (150 * u) + time * 0.07 + k), cos(pos.x / (180 * u) - time * 0.06 + k * 2)) + vel = vec2(vel + (eddy * 3 * u - vel) * (1 - exp(-dt * 0.5)) - vec2(gx, gy) * 420 * u * dt) + for (const f of pointers) { + const live = 1 - step(0.0001, f.gone) + const mv = vec2((f.at - f.was) / max(dt, 0.004)) + const sp = length(mv) + const near = live * smoothstep(80 * u, 25 * u, length(vec2(pos - f.at))) + const carry = vec2((mv / max(sp, 1)) * min(sp, 260) * 0.4) + vel = vec2(vel + (carry - vel) * near * (1 - exp(-dt * 9))) + spin = spin + near * min(sp, 400) * 0.004 * (k < 1.5 ? 1 : -1) * dt * 30 + } + // Stay on the water: a soft push back from the edges. + const mrg = 40 * u + vel = vec2(vel + vec2(smoothstep(mrg, 0, pos.x) - smoothstep(size.x - mrg, size.x, pos.x), smoothstep(mrg, 0, pos.y) - smoothstep(size.y - mrg, size.y, pos.y)) * 30 * u * dt) + pos = vec2(pos + vel * dt) + spin = clamp(spin, -2, 2) * exp(-dt * 0.8) + ang = mod(ang + (spin + 0.03) * dt, 2 * PI) + return p.cell.y < 0.5 ? vec4(pos, vel) : vec4(ang, spin, min(landed, 99), fall) +}) + +// The koi, drawn as one quad each into a layer of their own, so the picture can bend them through the +// surface and drop their shadows. Local frame: x runs along the body in body lengths (nose at 0.35), y across. +const pick6 = (l: readonly T[], i: number): T => (i < 0.5 ? l[0]! : i < 1.5 ? l[1]! : i < 2.5 ? l[2]! : i < 3.5 ? l[3]! : i < 4.5 ? l[4]! : l[5]!) +const WHITE = vec3(0.89, 0.88, 0.81) +const RED = vec3(0.9, 0.3, 0.11) +const INK = vec3(0.1, 0.1, 0.12) +const GOLD = vec3(0.96, 0.74, 0.22) +const SLATE = vec3(0.42, 0.53, 0.64) +const ORANGE = vec3(0.95, 0.47, 0.12) +// The seven koi by variety, in order: kohaku, showa, ogon, sanke, tancho, asagi, hi +const K_BASE = [WHITE, INK, GOLD, WHITE, WHITE, SLATE, ORANGE] +const K_PATCH = [RED, RED, vec3(1, 0.86, 0.42), RED, WHITE, SLATE, vec3(0.98, 0.62, 0.2)] +const K_THR = [0.47, 0.5, 0.45, 0.56, 2, 2, 0.5] +const K_INKT = [2, 2, 2, 0.66, 2, 2, 2] +const K_FIN = [WHITE, vec3(0.8, 0.8, 0.8), vec3(1, 0.86, 0.45), WHITE, WHITE, vec3(0.95, 0.55, 0.2), vec3(1, 0.66, 0.3)] +const K_FINLEN = [1, 1, 1.5, 1, 1.1, 1, 1.15] + +const koiLayer = buffer({ filter: 'mipmap' }) + +// One paired fin: a rayed teardrop hinged at (s0, y0), swept back by the angle beta. +const fin = (sN: number, ay: number, s0: number, y0: number, beta: number, len: number, wid: number, aa: number): number => { + const rx = sN - s0 + const ry = ay - y0 + const cb = cos(beta) + const sb = sin(beta) + const al = rx * cb + ry * sb + const ac = ry * cb - rx * sb + const t = clamp(al / len, 0, 1) + const hw = wid * sqrt(t) * sqrt(1.12 - t) + const m = smoothstep(aa, -aa, abs(ac) - hw) * step(0, al) * smoothstep(1, 0.9, al / len) + const rays = 0.5 + 0.5 * cos((ac / (al + 0.03)) * 26) + return m * (0.36 + 0.16 * rays) * (1 - 0.6 * t) +} + +const koi = shapes( + { count: 6, instances: 7, cover: 0.26, draw: 'triangles', into: koiLayer, clear: true }, + (v, i) => { + const u = unitOf() + const s0 = fish.at(vec2(i, 0)) + const s1 = fish.at(vec2(i, 1)) + const L = pick7(K_LEN, i) * u + const sx = pick6([0, 1, 0, 1, 1, 0], v) + const sy = pick6([-1, -1, 1, -1, 1, 1], v) + const fl = pick7(K_FINLEN, i) + // The quad leans with the turn: its tail end is shifted sideways by the bend there, so a turning fish stays + // inside a narrow quad. (The draw is charged by the area it may cover, and is dropped whole if it covers more.) + const lx = mix(-0.65 - 0.36 * fl, 0.38, sx) + const lean = clamp(s1.y, -1.5, 1.5) * 0.11 * pow(0.78 + 0.36 * fl, 2) * (1 - sx) + // A koi rolling at the surface is seen edge on for a moment. + const roll = fish.at(vec2(i, 3)).x + const ly = sy * (0.37 + 0.16 * (fl - 1)) * (1 - 0.62 * roll) + lean + const dir = vec2(cos(s0.z), sin(s0.z)) + const w = vec2(s0.xy + dir * lx * L + vec2(-dir.y, dir.x) * ly * L) + // The roll rides with the koi's number, as a fraction under a half. + return { at: vec2((w / size) * 2 - 1), a: vec4(lx, ly, i + 0.45 * roll, s1.x), b: vec4(s1.y, s1.z, s1.w, dir.x * SUN.y - dir.y * SUN.x) } + }, + s => { + const u = unitOf() + const id = floor(s.a.z + 0.5) + const L = pick7(K_LEN, id) * u + // A fish lower in the water is seen less sharply. + const aa = ((0.8 * s.px) / L) * (1 + 1.6 * (1 - s.b.z)) + 0.005 + 0.014 * (1 - s.b.z) + const ph = s.a.w + const trn = s.b.x + const gulp = s.b.y + const rise = s.b.z + const fl = pick7(K_FINLEN, id) + const sN = 0.35 - s.a.x + // The spine: a travelling wave that grows toward the tail, and a bend into the turn. + const env = 0.05 + 0.95 * min(sN * sN, 1.6) + const bendAt = max(sN - 0.22, 0) + const yc = 0.05 * env * sin(ph - 5.2 * sN) + clamp(trn, -1.5, 1.5) * 0.13 * bendAt * bendAt + const y = s.a.y - yc + const ay = abs(y) + const nose = max(0.3 - sN, 0) / 0.3 + const hw = 0.148 * pow(clamp(1 - nose * nose, 0, 1), 0.62) * mix(1, 0.2, smoothstep(0.3, 1, sN)) + const body0 = smoothstep(aa, -aa, ay - hw) * step(0, sN) * smoothstep(1.02, 0.97, sN) + const vN = clamp(y / max(hw, 0.004), -1, 1) + const eyeAt = length(vec2(sN - 0.1, ay - 0.088)) + const body = max(body0, smoothstep(0.012 + aa, 0.012 - aa * 0.5, eyeAt) * 0.85) + + // Pattern: patches that belong to this one fish. + // Each is a few slow waves laid over each other, bent a little so that no border runs straight. + const own = id * 2.4 + 1 + const sW = sN + 0.025 * sin(vN * 6 + own * 5) + const vW = vN + 0.16 * sin(sN * 21 + own * 3) + const pn = 0.5 + 0.27 * (sin(sW * 7.5 + own * 1.7) * cos(vW * 1.9 + own) + 0.7 * sin(sW * 12.3 - vW * 2.6 + own * 2.9)) + const pn2 = 0.5 + 0.3 * (sin(sW * 19 + own * 3.1) * sin(vW * 4.3 + own * 1.3) + 0.6 * cos(sW * 31 - vW * 3 + own)) + const base = pick7(K_BASE, id) + const thr = pick7(K_THR, id) + const su = sN / 0.03 + const row = floor(su) + const sv = y / 0.026 + 0.5 * mod(row, 2) + let c = mix(base, pick7(K_PATCH, id), smoothstep(thr - 0.1, thr + 0.1, pn)) + // Showa: white breaks through the ink. Sanke: small ink marks. Tancho: one red cap. Asagi: orange flanks. + c = mix(c, WHITE, (id > 0.5 && id < 1.5 ? 1 : 0) * smoothstep(0.54, 0.72, pn2)) + const inkAt = pick7(K_INKT, id) + c = mix(c, INK, smoothstep(inkAt - 0.03, inkAt + 0.1, pn2)) + c = mix(c, RED, (id > 3.5 && id < 4.5 ? 1 : 0) * smoothstep(0.075, 0.045, length(vec2(sN - 0.16, y)) + 0.05 * (pn - 0.5))) + const asagi = id > 4.5 && id < 5.5 ? 1 : 0 + c = mix(c, ORANGE, asagi * max(smoothstep(0.5, 0.86, abs(vN)), smoothstep(0.2, 0.1, sN) * 0.7)) + // Scales: offset rows of arcs, faint except on the asagi; mostly they show in the wet shine. + const arc = length(vec2(su - row - 0.2, sv - floor(sv) - 0.5)) + const net = smoothstep(0.34, 0.52, arc) * smoothstep(0.2, 0.28, sN) * (1 - 0.5 * vN * vN) + c = vec3(c * (1 - net * (0.085 + 0.07 * asagi)) * (1 - 0.09 * exp(-pow((sN - 0.225 - 0.05 * vN * vN) / 0.016, 2)))) + // Volume: a rounded back lit from the sun's side, with a wet streak. + const sunSide = s.b.w + const z = sqrt(max(1 - vN * vN, 0)) + // The back is round and a little brighter along the sun's side of the spine, with a thin wet line there on + // a fish near the surface; its flanks fall away, and the rest of the light on it is the net of light's, + // laid on in the picture pass. + const back = exp(-pow((vN - 0.3 * sunSide) / 0.38, 2)) * z + const wetLine = pow(back, 12) * (1 - 0.5 * net) + c = vec3(c * (0.44 + 0.56 * z + 0.26 * sunSide * vN) * (0.93 + 0.14 * pn2) * (1 + 0.1 * back) + 0.14 * wetLine * smoothstep(0.35, 0.8, rise)) + // Head: eyes, and a mouth that opens for food. + // Eyes: small dark half-bulges on the edge of the head. + c = mix(c, vec3(0.06, 0.06, 0.07), smoothstep(0.028 + aa, 0.008, eyeAt) * 0.9) + const open = smoothstep(0.1, 0.6, gulp) + const lips = length(vec2((sN - 0.012) * 1.3, y)) + c = mix(c, vec3(0.98, 0.88, 0.84), open * smoothstep(0.034 + aa, 0.034, lips)) + c = mix(c, vec3(0.3, 0.1, 0.1), open * smoothstep(0.02 + aa, 0.02, lips)) + + // Fins under the body: pectoral and pelvic pairs that scull, and a forked tail that fans with the stroke. + const scull = 0.25 * sin(ph * 0.5 + id) + const fp = fin(sN, ay, 0.26, 0.09, 1.02 + scull, 0.26 * fl, 0.095 * fl, aa) + const fv = fin(sN, ay, 0.56, 0.07, 1.1 + 0.5 * scull, 0.1 * fl, 0.036 * fl, aa) + const tN = (sN - 0.96) / (0.36 * fl) + const spread = (0.8 + 0.2 * abs(cos(ph - 5.2))) * fl + const tw = 0.026 + 0.15 * spread * pow(clamp(tN, 0, 1), 0.8) + const fork = 1 - 0.3 * pow(clamp(1 - ay / max(tw, 0.004), 0, 1), 1.5) + const tail = smoothstep(aa, -aa, ay - tw) * step(0, tN) * smoothstep(fork, fork - 0.1, tN) + const trays = 0.5 + 0.5 * cos((y / max(tw, 0.004)) * 17) + const ft = tail * (0.36 + 0.14 * trays) * (1 - 0.6 * clamp(tN, 0, 1)) + const fa = max(max(fp, fv), ft) + const finC = pick7(K_FIN, id) + // The dorsal fin: a narrow ridge along the back. + const dt2 = clamp((sN - 0.36) / 0.36, 0, 1) + const dors = smoothstep(aa, -aa, abs(y) - 0.05 * dt2 * (1 - dt2)) * step(0.36, sN) * step(sN, 0.72) + c = vec3(c * (1 - 0.14 * dors)) + // Barbels: two pale whiskers at the mouth. + // They are drawn only where the fish is seen sharply enough to show them. + const barb = smoothstep(0.0045 + aa, 0.0045, strokeAway(vec2(sN, ay), vec2(0.012, 0.028), vec2(0.052, 0.078))) * smoothstep(0.014, 0.007, aa) + + // The water takes a fish's color by how deep it lies, red first, and gives back its own green; the tail + // hangs a little lower than the head, and the flanks turn away into the water. + const down = (1 - rise) * (0.75 + 0.5 * smoothstep(0.3, 1.2, sN)) + const water = vec3(0.26, 0.42, 0.38) + const kept = vec3(exp(-0.62 * down), exp(-0.3 * down), exp(-0.27 * down)) + const veil = 1 - exp(-0.55 * down) + // A koi rolling at the surface shows its pale belly. + const rolled = clamp((s.a.z - id - 0.01) / 0.44, 0, 1) + const bodyC = mix(vec3(mix(mix(c, vec3(0.93, 0.9, 0.8), 0.7 * rolled), water, 0.26 * abs(vN * vN * vN)) * kept), water, veil) + const finA = fa * (0.7 + 0.3 * rise) * (1 - body) + const finRGB = mix(vec3(finC * kept), water, min(veil + 0.12, 1)) + const barbA = barb * (1 - body) * (1 - finA) * 0.8 + // A thin soft shadow hugs the body, so a fish reads as lying over its own fins and over a fish beneath it. + const hug = smoothstep(0.04, 0, ay - hw) * (1 - body) * 0.3 * step(0, sN) * smoothstep(1.02, 0.97, sN) + const under = vec3(finRGB * finA + vec3(0.95, 0.82, 0.74) * barbA) + // A tail flung wide in a hard turn fades out at the quad's edge instead of being cut. + const lean = clamp(trn, -1.5, 1.5) * 0.11 * pow(0.78 + 0.36 * fl, 2) * (0.38 - s.a.x) / (1.03 + 0.36 * fl) + const keep = smoothstep(1, 0.86, abs(s.a.y - lean) / (0.37 + 0.16 * (fl - 1))) + return vec4(vec4(bodyC * body + under * (1 - hug), body + (finA + barbA) * (1 - hug) + hug) * keep) + }, +) + +// Helpers. Each helper at work brings a school of three fry, which keeps to a loop of its own in open +// water. They are drawn into the koi's layer, so the water bends, lights and shades them as it does the koi. +const fry = shapes( + { count: 1, instances: 12, cover: 0.012, draw: 'points', into: koiLayer }, + (v, i) => { + const u = unitOf() + const school = floor(i / 3) + const shown = clamp(session.helpers - school, 0, 1) + const own = i * 2.1 + const mid = pick4([vec2(0.3, 0.3), vec2(0.72, 0.7), vec2(0.3, 0.64), vec2(0.7, 0.34)], school) + const t = time * 0.5 + school * 1.7 + const at = vec2(mid * size + (vec2(cos(t), sin(t * 1.3)) * 60 + vec2(cos(t * 2.3 + own), sin(t * 1.9 + own)) * 16) * u) + const going = normalize(vec2(-sin(t) * 30 - sin(t * 2.3 + own) * 18.4, cos(t * 1.3) * 39 + cos(t * 1.9 + own) * 15.2)) + return { at: vec2((at / size) * 2 - 1), size: clamp((26 * u) / px, 1, 64), a: vec4(going, shown, 0), hide: shown < 0.02 } + }, + s => { + const local = vec2((s.coord.x - 0.5) * 26, (0.5 - s.coord.y) * 26) + const along = local.x * s.a.x + local.y * s.a.y + const across = local.y * s.a.x - local.x * s.a.y + const body = smoothstep(1, 0.6, length(vec2(along / 10, across / (1.1 + 1.7 * smoothstep(-10, 3, along))))) + return vec4(vec3(0.5, 0.54, 0.46) * body * s.a.z * 0.9, body * s.a.z * 0.9) + }, +) + +// Pellets and midges: one point each. +const pellets = shapes( + { count: 1, instances: 15, cover: 0.03, draw: 'points' }, + (v, i) => { + const u = unitOf() + const f = food.at(vec2(i, 0)) + const grow = smoothstep(0, 0.2, f.z) + const k = f.w > 0.75 ? 0.6 + 0.4 * grow : 1 - smoothstep(0, 0.4, f.z) + return { at: vec2((f.xy / size) * 2 - 1), size: clamp((22 * u) / px, 1, 64), a: vec4(k, f.w, i, 0), hide: f.w < 0.25 } + }, + s => { + const m = modeBuf.at(vec2(0, 0)).r + const d = vec2((s.coord - 0.5) * 2) + const r = length(d) + const k = s.a.x + const midge = step(0.75, s.a.y) * step(s.a.y, 0.95) + const rad = mix(0.36 * k, 0.1, midge) + const body = smoothstep(rad + 0.12, rad - 0.05, r) + const lit = 0.75 + 0.4 * dot(d, vec2(-0.55, -0.83)) / max(rad, 0.05) + const c = vec3(mix(vec3(0.8, 0.58, 0.3), vec3(0.1, 0.08, 0.06), midge) * clamp(lit, 0.45, 1.3) * mix(0.5, 1, m)) + // The dimple the pellet presses into the surface: a soft dark foot round it. + const ring = smoothstep(0.98, 0.7, r) * smoothstep(rad - 0.05, rad + 0.25, r) * k + const a = body + (1 - body) * ring * 0.22 + return vec4(c * body, a) + }, +) + +// The resting swell: two sets of slow sine trains from all round the compass, one long and one short (the short +// set is the long one turned a quarter turn and drawn finer). +// Each gives the surface a curvature, which focuses the light on the bottom: threads of light lie where the +// sheet of light folds, which is where the determinant below passes through zero. The net is the finer of the +// two sets' folds, the short set a little fainter, so a bold net carries a faint one inside its cells. The long +// set also gives the slope that bends what is seen. All of it is worked once a frame on a coarse grid: slope +// in r and g, the net broadly in b and as fine threads in a. +const swell = (lambda: number, deg: number, speed: number, focus: number) => { + const k = (2 * PI) / lambda + const a = (deg * PI) / 180 + return { kx: k * cos(a), ky: k * sin(a), om: k * speed, tilt: (focus * lambda * lambda) / (6400 * PI), fold: focus / (k * k) } +} +const LONG = [swell(205, 17, 13, 0.78), swell(164, 71, 11, 0.72), swell(232, 133, 12, 0.78), swell(150, 186, 9, 0.7), swell(190, 247, 10, 0.74), swell(138, 309, 8, 0.66)] +const glim = buffer({ scale: 0.3, filter: 'linear' }, p => { + const u = unitOf() + const q = vec2(p.point / u) + let gx = 0 + let gy = 0 + let hxx = 0 + let hxy = 0 + let hyy = 0 + let sxx = 0 + let sxy = 0 + let syy = 0 + for (const w of LONG) { + const th = q.x * w.kx + q.y * w.ky - time * w.om + const sn = sin(th) * w.fold + const cs = cos(th) * w.tilt + gx += w.kx * cs + gy += w.ky * cs + hxx -= w.kx * w.kx * sn + hxy -= w.kx * w.ky * sn + hyy -= w.ky * w.ky * sn + // The short set: the same trains turned a quarter turn, 1.62 times as fine and a little slower. + const s2 = sin((q.y * w.kx - q.x * w.ky) * 1.62 - time * w.om * 0.8) * w.fold * 0.92 + sxx -= w.ky * w.ky * s2 + sxy += w.kx * w.ky * s2 + syy -= w.kx * w.kx * s2 + } + const det = min(abs((1 + hxx) * (1 + hyy) - hxy * hxy), abs((1 + sxx) * (1 + syy) - sxy * sxy) + 0.12) + return vec4(gx * 2 + 0.5, gy * 2 + 0.5, clamp(0.3 / (det + 0.3), 0, 1), clamp(0.125 / (det + 0.07), 0, 1)) +}) +const MOON_AT = vec2(0.942, 0.58) + +// What hangs over the pond, painted once and small. r: what the surface mirrors, which is open sky (1) +// over the middle of the pond and the dark of boughs (0) over its banks, with a ragged leafy border +// between. g: the shade the boughs throw, which the sun carries off to one side of them in broad soft +// patches. b: cloud, which makes the sky's brightness uneven. a: roughly how deep the water is there. +const overhead = buffer({ scale: 0.25, once: true, filter: 'linear' }, p => { + const q = vec2(p.point / unitOf()) + const ragged = layered(vec2(q / 230), 101) - 0.5 + const leafy = lattice(vec2(q / 26), 103) - 0.5 + const mid = 1 - abs(p.uv.x - 0.5) * 2 + const sky = smoothstep(0.2, 0.62, mid + ragged * 0.8 + leafy * 0.22) + const shade = layered(vec2(q / 340 + 9), 107) - 0.3 * (p.uv.x + p.uv.y - 1) + const deep = clamp(0.76 - 0.56 * shelfOf(p.uv, (layered(vec2(q / 260), 11) - 0.5) * 0.14), 0, 1) + return vec4(sky, smoothstep(0.48, 0.74, shade), layered(vec2(q / 420 + 5), 113), deep) +}) + +// Film grain: one small tile of scatter, laid over the picture and never changed. +const grain = buffer({ size: [128, 128], once: true, filter: 'nearest', wrap: 'repeat' }, p => vec4(one(p.cell.x, p.cell.y, 131), 0, 0, 1)) + +// The picture: bottom, light, koi and surface put together through the moving water. +const view = picture(p => { + const u = unitOf() + const m = modeBuf.at(vec2(0, 0)).r + const rest = glim.sample(p.uv) + const gx = (rest.r - 0.5) * 0.5 + const gy = (rest.g - 0.5) * 0.5 + // The net of light: wide dim cells, thin bright threads, and knots where threads cross. + const net = 0.8 + 0.3 * clamp(rest.b * 2 - 0.4, 0, 1.5) + 0.5 * clamp(rest.a * 2.4 - 0.65, 0, 1.6) + // The live ripples from the wave buffer: height and slope. + // Two reads half a cell apart, averaged, so the slope turns smoothly across the grid's cells. + const half = vec2(0.5 / wave.size.x, 0.5 / wave.size.y) + const ripple = vec4((wave.sample(vec2(p.uv + half)) + wave.sample(vec2(p.uv - half))) * 0.5) + const live = vec2(clamp(ripple.b, -3, 3) * 0.8, clamp(ripple.a, -3, 3) * 0.8) + const g0 = vec2(vec2(gx, gy) + live * 5) + // The clamp changes nothing (the quotient cannot pass 0.22); it tells the validator's cost count how near the reads stay. + const g = clamp(vec2(g0 / (1 + 4.5 * length(g0))), -0.22, 0.22) + // Refraction: everything under the surface is seen shifted by the slope above it, deeper things more. + const bend = vec2((g * u) / size) + // The bottom blurs a little with depth: a coarser level of its picture is read where the water is deeper. + const up = vec2(p.uv + g * 0.12) + const over2 = overhead.sample(up) + const b = bed.sample(vec2(p.uv + bend * 58), over2.a * 1.1) + const depth = b.a + // The koi lie nearer the surface than the bottom does, so even the steepest ripple moves the sight of them only + // about five units. + const ko = koiLayer.sample(vec2(p.uv + bend * 48 * min(1, 0.1 / max(length(g), 0.001)))) + // One sun. Shadows fall away from it onto the bottom, farther and softer the deeper the bottom lies: + // down and right by day, down and left under the moon. Pads throw the crisp ones, koi the soft ones. + const fall = vec2((mix(vec2(-30, -40), vec2(38, -52), m) * u) / size) + const kSh = koiLayer.sample(vec2(p.uv + bend * 58 - fall * (0.7 + depth)), 1.5 + 2.4 * depth).a + // What floats is not nailed down: a passing slope carries pads and leaves a few points with it. + const tp = top.sample(vec2(p.uv + bend * 16)) + const tSh = top.sample(vec2(p.uv + bend * 42 - fall * (0.3 + 1.1 * depth)), 1.4 + 2 * depth).a + const shadow = 1 - (1 - 0.72 * (1 - 0.45 * depth) * kSh) * (1 - 0.8 * tSh) + // The boughs overhead shade the water in broad soft patches; the sun and its net of light fail there. + const shade = over2.g + const sunlit = (1 - shadow) * (1 - 0.86 * shade) + // By night a lantern on the left bank throws a warm pool across the water; beyond it there is only the moon. + const toLamp = vec2((p.point - vec2(-0.03, 0.52) * size) / (400 * u)) + const lamp = (1 - m) / (1 + dot(toLamp, toLamp) * 1.6) + // Caustics: light gathers where the surface curves like a lens. Only the slow swell draws the net, so a + // pointer moved hard to and fro cannot make it flicker. + const causAt = 1 + (net - 1) * max(1.5 - 1.7 * depth, 0.14) * mix(0.15, 1, m) + // Sun is warm and about two stops over the cool shade. + const sun = vec3(mix(vec3(0.2, 0.25, 0.4), vec3(1.02, 0.9, 0.68), m) + vec3(1, 0.6, 0.26) * lamp) + const amb = vec3(mix(vec3(0.07, 0.095, 0.16), vec3(0.3, 0.37, 0.43), m) * (1 - 0.2 * shade)) + const light = vec3(amb + sun * causAt * sunlit) + const clear = exp(-depth * mix(2.3, 1.75, m)) + const murk = vec3(mix(vec3(0.012, 0.03, 0.055), vec3(0.045, 0.13, 0.095), m) * (0.62 + 0.38 * sunlit) * (0.94 + 0.06 * net)) + let col = mix(murk, vec3(b.rgb * light), clear) + // Koi, lit from above, with the net of light playing over their backs and the shade of pads and boughs on them. + const koiLight = vec3((amb * 1.15 + sun * (0.3 + 0.7 * causAt) * (1 - 0.6 * tSh) * (1 - 0.7 * shade)) * mix(0.8, 1, m)) + col = vec3(col * (1 - ko.a) + ko.rgb * koiLight) + // The surface itself is seen only by what it mirrors, and from straight above calm water mirrors little: + // a faint pale sky over the middle of the pond, broken by cloud, and the dark of boughs by the banks. A + // slope mirrors far more, so every ripple carries a band of sky. The mirror is looked up only a little way + // along the slope and its borders are broad, so a pointer moved hard to and fro cannot swing any spot's + // brightness far. + const open = over2.r + const sky = vec3(mix(vec3(0.13, 0.18, 0.3), vec3(0.66, 0.8, 0.93), m) * (0.78 + 0.44 * over2.b)) + const boughs = mix(vec3(0.015, 0.025, 0.045), vec3(0.07, 0.11, 0.07), m) + // The slope's share is signed: the flank of a ripple that faces the light carries sky, the other is clear. + const facing = max(dot(g, SUN), 0) + const wet = (1 - tp.a) * clamp(0.02 + 0.07 * open * (0.3 + over2.b) + 0.85 * facing, 0, 0.26) + col = vec3(col * (1 - 0.5 * wet) + mix(boughs, sky, open) * wet) + // Each ripple also has a face toward the light and a face away from it. + const tilt = dot(live, SUN) * 18 + const flank = (tilt / (1 + abs(tilt))) * (1 - tp.a) + col = vec3(col + mix(vec3(0.03, 0.038, 0.06), vec3(0.03, 0.033, 0.03), m) * flank * (1.15 - col.g)) + // Pads, reeds, stones and leaves sit on top, in the air; by night they lose most of their color. + const topLight = vec3(amb * 1.25 + sun * 0.8 * (1 - 0.82 * shade)) + const topC = mix(vec3(dot(tp.rgb, vec3(0.3, 0.6, 0.1))), tp.rgb, mix(0.45, 1, m)) + col = vec3(col * (1 - tp.a) + topC * topLight) + // The camera: light falls off away from the sun's side and toward the corners, color is a little spent, + // and a fixed fine grain lies over everything. + const off = vec2(p.uv - 0.5) + col = vec3(col * (1 + 0.09 * dot(off, SUN) - 0.1 * dot(off, off))) + col = mix(vec3(dot(col, vec3(0.3, 0.6, 0.1))), col, 0.9) + // A soft shoulder, as film has: sunlit stones and white koi come close to clipping and do not clip. + col = clamp(vec3((col * 1.36) / (1 + 0.38 * col)), 0, 1) + // Then a gentle S, so deep water and the shade under pads go near black and sunlit things stand out. + col = mix(col, vec3(col * col * (3 - 2 * col)), 0.28) + col = vec3(col + (grain.sample(vec2(p.pixel / 128)).r - 0.5) * 0.028) + return vec4(col, 1) +}) + +// The floating leaves, one quad each, turned in the vertex stage. +const leaves = shapes( + { count: 6, instances: 4, cover: 0.03, draw: 'triangles' }, + (v, i) => { + const u = unitOf() + const s = afloat.at(vec2(i, 0)) + const r = afloat.at(vec2(i, 1)) + const sx = pick6([-1, 1, -1, 1, 1, -1], v) + const sy = pick6([-1, -1, 1, -1, 1, 1], v) + const half = 26 * u * (0.25 + 0.75 * smoothstep(0, 0.4, r.z)) + const ca = cos(r.x) + const sa = sin(r.x) + const w = vec2(s.xy + vec2(sx * ca - sy * sa, sx * sa + sy * ca) * half) + return { at: vec2((w / size) * 2 - 1), a: vec4(sx * 26, sy * 26, i, 0) } + }, + s => { + const m = modeBuf.at(vec2(0, 0)).r + const id = floor(s.a.z + 0.5) + const d = vec2(s.a.x, s.a.y) + const R = 17 - id + const rr = length(d) + const a = atan(d.y, d.x) + const lobe = abs(cos(a * 2.5)) + const edge = R * (0.3 + 0.7 * pow(lobe, 1.1)) * (0.62 + 0.38 * cos(a)) * (1 + 0.05 * sin(a * 24)) + const mk = smoothstep(edge, edge - 1.3, rr) + const vein = pow(lobe, 26) * smoothstep(0.08, 0.4, rr / R) + const hue = pick4([0.5, 0.15, 0.85, 0.3], id) + const warm = mix(mix(vec3(0.74, 0.15, 0.1), vec3(0.9, 0.4, 0.1), smoothstep(0, 0.5, hue)), vec3(0.93, 0.68, 0.18), smoothstep(0.5, 1, hue)) + const lit = mix(vec3(0.3, 0.4, 0.62), vec3(1, 1, 1), m) + const c = vec3(mix(warm, vec3(0.45, 0.33, 0.2), 0.4) * (0.8 + 0.34 * lattice(vec2(d / 6 + id * 7), 51)) * (1.06 - 0.26 * rr / R) * (1 + 0.3 * vein) * lit) + // The leaf dents the water: a soft dark foot all round it. + const foot = smoothstep(edge + 6, edge - 1, rr) * 0.25 * (1 - mk) + return vec4(c * mk, mk + foot) + }, +) + +// By night two lights lie on the water as broken columns of glitter: the moon's, cool, on the right, and +// the lantern's, warm, reaching out from the left bank. The swell breaks them into soft moving glints. +// One quad each, so the rest of the picture does not pay for them. +const lights = shapes( + { count: 6, instances: 2, cover: 0.13, draw: 'triangles', blend: 'add' }, + (v, i) => { + const u = unitOf() + const sx = pick6([-1, 1, -1, 1, 1, -1], v) + const sy = pick6([-1, -1, 1, -1, 1, 1], v) + const half = i < 0.5 ? vec2(64, 230) : vec2(260, 52) + const mid = i < 0.5 ? vec2(MOON_AT * size) : vec2(vec2(0.07, 0.52) * size) + const w = vec2(mid + vec2(sx * half.x, sy * half.y) * u) + const night = 1 - modeBuf.at(vec2(0, 0)).r + return { at: vec2((w / size) * 2 - 1), a: vec4(sx * half.x, sy * half.y, i, night), hide: night < 0.5 } + }, + s => { + // A glint shows wherever the swell tilts the water just so toward the light, so the glints are soft + // patches that come and go and drift with the swell. + const rest = glim.sample(s.uv) + const tiltA = vec2(vec2(rest.r - 0.5, rest.g - 0.5) * 0.5 - vec2(0.03, 0.045)) + const tiltB = vec2(vec2(rest.r - 0.5, rest.g - 0.5) * 0.5 + vec2(0.05, -0.015)) + const threads = exp(-dot(tiltA, tiltA) * 1400) + exp(-dot(tiltB, tiltB) * 1400) + const isMoon = s.a.z < 0.5 ? 1 : 0 + const along = mix(s.a.x, s.a.y, isMoon) + const across = mix(s.a.y, s.a.x, isMoon) + const glow = exp(-pow(across / mix(30, 34, isMoon), 2) - pow(along / mix(130, 115, isMoon), 2)) * (0.12 + 0.75 * threads) * mix(1, 1.15, isMoon) + return vec4(mix(vec3(0.75, 0.48, 0.2), vec3(0.62, 0.68, 0.8), isMoon) * glow * s.a.w, 0) + }, +) + +// Water striders. What shows of one from above is a small brown smudge and, beside it, the four round +// shadows its feet press into the light on the bottom. +const striders = shapes( + { count: 1, instances: 2, cover: 0.012, draw: 'points' }, + (v, i) => { + const s = cast.at(vec2(i, 0)) + return { at: vec2((s.xy / size) * 2 - 1), size: clamp((34 * unitOf()) / px, 1, 64) } + }, + s => { + const dv = vec2((s.coord.x - 0.5) * 34, (s.coord.y - 0.5) * 34) + const body = smoothstep(1.6, 0.4, length(vec2(dv.x * 0.3 + 1.2, dv.y + 1))) + const foot = vec2(abs(dv.x - 4) - 7.5, abs(dv.y - 4.5) - 5) + const dimples = smoothstep(3.4, 1.2, length(foot)) + return vec4(vec3(0.16, 0.1, 0.05) * body * 0.7, clamp(0.7 * body + 0.3 * dimples, 0, 1)) + }, +) + +// Fireflies drift over the margins at night, each on a slow loop of its own, glowing and fading. +const flies = shapes( + { count: 1, instances: 5, cover: 0.02, draw: 'points', blend: 'add' }, + (v, i) => { + const f = cast.at(vec2(i + 2, 0)) + const night = 1 - modeBuf.at(vec2(0, 0)).r + return { at: vec2((f.xy / size) * 2 - 1), size: clamp((44 * unitOf()) / px, 1, 64), a: vec4(f.z * night, 0, 0, 0), hide: night < 0.5 } + }, + s => { + const d = length(vec2(s.coord - 0.5)) * 2 + const g = s.a.x * (clamp(1.6 - d * 11, 0, 1) * 0.8 + (0.16 / (1 + d * d * 6)) * smoothstep(1, 0.6, d)) + return vec4(vec3(0.8, 1, 0.4) * g, 0) + }, +) + +export default shader({ fps: 60, passes: [modeBuf, bed, top, overhead, grain, mood, plops, fish, food, cast, stir, wave, afloat, glim, koiLayer, koi, fry, view, lights, leaves, pellets, striders, flies] }) diff --git a/plugins/theme-examples/hooks/scenes/koi-pond/still.ts b/plugins/theme-examples/hooks/scenes/koi-pond/still.ts new file mode 100644 index 00000000..991ed8fa --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/koi-pond/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Koi Pond, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function koiPondSvg(): string { + return ( + "\nKoi Pond\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + ) +} diff --git a/plugins/theme-examples/hooks/scenes/ripple-tank/scene.ts b/plugins/theme-examples/hooks/scenes/ripple-tank/scene.ts new file mode 100644 index 00000000..c15cfa65 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/ripple-tank/scene.ts @@ -0,0 +1,666 @@ +// Ripple Tank. A two-ink print of a pond's skin: a teal plate for the water and a coral plate for what lives on it. +// The water is a real wave field kept in a small buffer, so every pointer is a dipper and several make true interference. +// Three passes do most of the work. "cast" is a tiny table of where every creature is, worked out once a frame. +// "tank" is the wave field. "water" prints the field as halftone rings; the creatures are small shapes laid over it. +import { + PI, abs, atan, buffer, clamp, cos, dot, dt, exp, floor, int, length, max, min, mix, normalize, picture, pointerCount, + pointers, scene, shader, shapes, sin, size, smoothstep, sqrt, step, table, time, unsigned, vec2, vec3, vec4, +} from 'claude-code/shader' +import type { Buffer } from 'claude-code/shader' + +// A small integer mixer: two whole numbers in, 32 scrambled bits out +const blend = (a: int, b: int): int => { + let h = int(a * 1830299339 + b * -1588511639) + h = int(h ^ (h >>> 15)) + h = int(h * -2018788877) + h = int(h ^ (h >>> 12)) + h = int(h * 458427541) + return int(h ^ (h >>> 17)) +} +const low = (h: int) => unsigned(int(h & 65535)) / 65536 +const high = (h: int) => unsigned(int(h >>> 16)) / 65536 + +// The cast +// One row each. Striders: home x, home y (shares of the window), seconds between darts, seed. +// Leaves: home x, home y, seed, how fast it turns (radians a second). Rain: middle x, middle y, seconds between +// drops, seed. Whirligig beetles: home x, home y, seed, how fast it circles (radians a second, the sign its way round). +// Minnows: place in the shoal along and across, seed, size. Midges, in the air over the water: home x, home y, seed, size. +// Leaves and beetles have their homes in the gaps between the striders' homes, so they seldom sit on one another. +const STRIDERS = 8 +const LEAVES = 3 +const DROPS = 2 +const BEETLES = 3 +const MINNOWS = 9 +const MIDGES = 5 +const LEAF0 = STRIDERS +const DROP0 = LEAF0 + LEAVES +const BEETLE0 = DROP0 + DROPS +const MINNOW0 = BEETLE0 + BEETLES +const MIDGE0 = MINNOW0 + MINNOWS +const ROLL = table([ + [0.075, 0.83, 3.1, 0.13], + [0.19, 0.61, 2.6, 0.71], + [0.07, 0.4, 3.7, 0.37], + [0.18, 0.16, 2.9, 0.55], + [0.93, 0.86, 3.4, 0.91], + [0.815, 0.65, 2.7, 0.29], + [0.925, 0.4, 3.9, 0.63], + [0.825, 0.15, 3.2, 0.47], + [0.062, 0.615, 1.3, 0.03], + [0.94, 0.63, 4.1, -0.024], + [0.205, 0.385, 2.6, 0.037], + [0.13, 0.52, 2.3, 0.2], + [0.87, 0.5, 3.1, 0.6], + [0.205, 0.85, 0.3, 2.1], + [0.775, 0.39, 1.7, -2.4], + [0.8, 0.87, 2.9, 1.9], + [0.9, 0, 0.1, 1.1], + [0.45, 0.7, 0.9, 0.9], + [0.35, -0.8, 1.7, 1], + [0, 0.15, 2.3, 1.2], + [-0.2, -0.6, 3.1, 0.85], + [-0.5, 0.75, 3.8, 1], + [-0.7, -0.1, 4.6, 0.95], + [-1, 0.5, 5.2, 0.8], + [-1.1, -0.7, 6, 0.9], + [0.25, 0.27, 0.4, 1.5], + [0.1, 0.6, 1.9, 1.3], + [0.74, 0.86, 3.3, 1.65], + [0.9, 0.26, 4.7, 1.35], + [0.77, 0.5, 5.9, 1.5], +]) +// Sizes that the shapes need where they are placed. +const STRIDER_SIZE = [1.3, 1.1, 1.4, 1.2, 1.2, 1.35, 1.1, 1.3] +const LEAF_LONG = [50, 42, 33] +const LEAF_WIDE = [0.5, 0.58, 0.46] +const LEAF_BITE = [1, 0, 0] + +const ROAM = 46 +// In a narrow or a short window every creature is drawn a little smaller, so the margins do not crowd. +const zoomOf = () => clamp(min(size.x / 1200, size.y / 760), 0.62, 1) + +// One turn of a wave: up, then down, over "span" seconds, then nothing. +const plop = (age: number, span: number) => sin(2 * PI * min(age / span, 1)) + +// How the water leans at a place. (The tank is declared further down.) +const leanAt = (uv: vec2) => { + const tx = 1 / tank.size.x + const ty = 1 / tank.size.y + return vec2( + tank.sample(vec2(uv.x + tx, uv.y)).r - tank.sample(vec2(uv.x - tx, uv.y)).r, + tank.sample(vec2(uv.x, uv.y + ty)).r - tank.sample(vec2(uv.x, uv.y - ty)).r, + ) +} + +// A creature gives way to the pointers: the push it would like, never large, and none when a pointer is right on it. +const giveWay = (at: vec2, wide: number, far: number) => { + let shove = vec2(0, 0) + for (const finger of pointers) { + const away = vec2(at - finger.at) + const hold = clamp(1 - finger.gone, 0, 1) + shove = vec2(shove + away * (1.1 * exp(-dot(away, away) / (wide * wide)) * hold * hold)) + } + return vec2(shove / (1 + length(shove) / far)) +} + +// Three texels a creature. Top row: where it is (x, y) and which way it faces (x, y). The rows under it: what else it +// remembers from frame to frame (how far a pointer has pushed it, how far the water has carried it). +const cast: Buffer = buffer({ size: [MIDGE0 + MIDGES, 3], float: 32, filter: 'nearest' }, (p): vec4 => { + const who = p.cell.x + const card = ROLL.at(who) + const kept = cast.at(vec2(who, 1)) + const carried = cast.at(vec2(who, 2)).xy + + // A strider: rest, dart, glide, on a path that is a function of the clock + const home = vec2(size.x * card.x, size.y * card.y) + const g = time / card.z + card.w + const n = floor(g) + const f = g - n + const stop = (k: number) => { + const h = blend(int(k), int(card.w * 1000)) + const a = k * 1.9 + low(h) * 1.2 + card.w * 6 + const r = ROAM * zoomOf() * (0.45 + 0.55 * high(h)) + return vec2(home.x + cos(a) * r, home.y + sin(a) * r) + } + const a = stop(n) + const b = stop(n + 1) + const c = stop(n + 2) + const left = 1 - min(f / 0.36, 1) + const base = vec2(mix(a, b, 1 - left * left * left)) + const ahead = normalize(vec2(b - a)) + const facing = normalize(vec2(mix(ahead, normalize(vec2(c - b)), smoothstep(0.58, 0.96, f)))) + const stroke = smoothstep(0, 0.06, f) * (1 - smoothstep(0.08, 0.36, f)) + // It gives way to a pointer with a little inertia, and slides with the slope of the water under it. + const striderShove = vec2(kept.zw + (giveWay(base, 110, 44) - kept.zw) * (1 - exp(-dt * 6))) + const striderRide = vec2(carried + (clamp(leanAt(vec2(base / size)) * -70, -8, 8) - carried) * (1 - exp(-dt * 4))) + const striderTop = vec4(base + striderShove + striderRide, facing) + const striderBottom = vec4(stroke, f * card.z, striderShove) + + // A leaf: a slow drift and a slow turn; a pointer nudges it, the water rocks it + const leafBase = vec2(home.x + 22 * sin(time * 0.05 + card.z), home.y + 30 * sin(time * 0.037 + card.z * 2)) + const leafShove = vec2(kept.zw + (giveWay(leafBase, 130, 56) - kept.zw) * (1 - exp(-dt * 1.6))) + const leafLean = leanAt(vec2(leafBase / size)) + const leafRide = vec2(carried + (clamp(leafLean * -80, -9, 9) - carried) * (1 - exp(-dt * 2.5))) + const leafTurn = card.z * 2 + card.w * time + 0.4 * sin(time * 0.11 + card.z) + clamp((leafRide.x - leafRide.y) * 0.02, -0.2, 0.2) + const leafTop = vec4(leafBase + leafShove + leafRide, cos(leafTurn), sin(leafTurn)) + const leafBottom = vec4(0, 0, leafShove) + + // A raindrop source: it wanders the margin, one drop every few seconds + const dg = time / card.z + card.w + const dk = floor(dg) + // Rain does not keep time: each drop falls a little late, by its own amount, and now and then one does not fall. + const dh = blend(int(dk), int(card.w * 77)) + const fallen = high(dh) < 0.18 ? 99 : max((dg - dk) * card.z - low(dh) * 0.5 * card.z, 0) + const dropTop = vec4( + size.x * (card.x + 0.08 * sin(dk * 2.4 + card.w * 9)), + size.y * (card.y + 0.34 * sin(dk * 1.7 + card.w * 5)), + fallen, + dk, + ) + // A drop falls only if no pointer is there when it lands; once it has landed, it finishes. + const landed = fallen < 0.07 ? (pointerCount > 0 ? 0 : 1) : kept.x + + // A whirligig beetle: round and round, faster when a pointer comes near + const ring = vec2(home.x + 26 * sin(time * 0.04 + card.z), home.y + 34 * sin(time * 0.031 + card.z * 2)) + let alarm = 0 + for (const finger of pointers) { + const away = vec2(ring - finger.at) + alarm = max(alarm, exp(-dot(away, away) / 26000) * clamp(1 - finger.gone, 0, 1)) + } + const calmer = kept.y + (alarm - kept.y) * (1 - exp(-dt * 3)) + const swing = kept.x + card.w * (1 + 2.5 * calmer) * dt + const angle = swing - floor(swing / (2 * PI)) * 2 * PI + const wide = 17 + 6 * sin(time * 0.6 + card.z * 3) + const spin = card.w < 0 ? -1 : 1 + const beetleTop = vec4(ring.x + wide * cos(angle), ring.y + wide * sin(angle), -sin(angle) * spin, cos(angle) * spin) + const beetleBottom = vec4(angle, calmer, 0, 0) + + // A minnow: a place in a shoal that cruises the whole tank, under the surface + const cruise = vec2(time * 0.043 + 1, time * 0.071) + const shoal = vec2(size.x * (0.5 + 0.41 * sin(cruise.x)), size.y * (0.5 + 0.34 * sin(cruise.y))) + const heading = normalize(vec2(size.x * 0.41 * 0.043 * cos(cruise.x), size.y * 0.34 * 0.071 * cos(cruise.y) + 0.01)) + const across = vec2(-heading.y, heading.x) + const place = vec2( + shoal + heading * (card.x * 62 + 7 * sin(time * 0.7 + card.z)) + across * (card.y * 44 + 6 * sin(time * 0.53 + card.z * 2)), + ) + const minnowShove = vec2(kept.zw + (giveWay(place, 150, 80) - kept.zw) * (1 - exp(-dt * 4))) + const nose = normalize(vec2(heading + across * (0.25 * sin(time * 0.53 + card.z * 2)) + minnowShove * 0.02)) + const minnowTop = vec4(place + minnowShove, nose) + const minnowBottom = vec4(card.z, card.w, minnowShove) + + // A midge: a slow looping glide in the air over one patch of water + const s = card.z + const patch = vec2(home.x + 34 * sin(time * 0.05 + s), home.y + 44 * sin(time * 0.037 + s * 2)) + const dance = vec2(14 * sin(time * 1.3 + s * 7) + 5 * sin(time * 2.9 + s * 3), 12 * cos(time * 1.1 + s * 5) + 5 * sin(time * 2.3 + s)) + const going = normalize(vec2(18 * cos(time * 1.3 + s * 7) + 14.5 * cos(time * 2.9 + s * 3) + 0.01, -13 * sin(time * 1.1 + s * 5) + 11.5 * cos(time * 2.3 + s))) + const midgeShove = vec2(kept.zw + (giveWay(patch, 120, 70) - kept.zw) * (1 - exp(-dt * 8))) + const midgeTop = vec4(patch + dance + midgeShove, going) + const midgeBottom = vec4(card.w, 0, midgeShove) + + const top = + who < LEAF0 ? striderTop : who < DROP0 ? leafTop : who < BEETLE0 ? dropTop : who < MINNOW0 ? beetleTop : who < MIDGE0 ? minnowTop : midgeTop + const bottom = + who < LEAF0 + ? striderBottom + : who < DROP0 + ? leafBottom + : who < BEETLE0 + ? vec4(landed, 0, 0, 0) + : who < MINNOW0 + ? beetleBottom + : who < MIDGE0 + ? minnowBottom + : midgeBottom + const third = who < LEAF0 ? striderRide : who < DROP0 ? leafRide : vec2(0, 0) + return p.cell.y < 0.5 ? top : p.cell.y < 1.5 ? bottom : vec4(third, 0, 0) +}) + +// The tank: a wave field of one texel to every four points, two time levels +const tank: Buffer = buffer({ scale: 0.25, float: 16, filter: 'linear', repeat: 2 }, (p): vec4 => { + const c = p.cell + const me = tank.at(c).rg + const plus = vec2( + tank.at(vec2(c.x + 1, c.y)).rg + tank.at(vec2(c.x - 1, c.y)).rg + tank.at(vec2(c.x, c.y + 1)).rg + tank.at(vec2(c.x, c.y - 1)).rg, + ) + const cross = vec2( + tank.at(vec2(c.x + 1, c.y + 1)).rg + tank.at(vec2(c.x - 1, c.y + 1)).rg + tank.at(vec2(c.x + 1, c.y - 1)).rg + tank.at(vec2(c.x - 1, c.y - 1)).rg, + ) + const bend = vec2((plus * 4 + cross - me * 20) / 6) + // A beach round the rim soaks the waves up. + const rim = min(min(p.uv.x, 1 - p.uv.x), min(p.uv.y, 1 - p.uv.y)) + const beach = 1 - smoothstep(0, 0.07, rim) + // Two runs a frame; "pace" keeps the waves' speed the same when frames come slower than thirty a second. + const pace = clamp(dt * 30, 0.6, 1.5) + const push = pace * pace + const drag = 0.0045 * pace + 0.2 * beach * beach + let next = me.r + (1 - drag) * (me.r - me.g) + 0.15 * push * bend.r + 0.03 * (bend.r - bend.g) + + // Each pointer is a dipper: the water under it is held to a height that swings down and up. Holding to a height, not + // adding to it, means nothing can pile up however hard a finger scrubs. The dipper is drawn from where the pointer was + // to where it is, so a fast one leaves a line, not beads, and the faster it goes the lighter it presses. + for (const finger of pointers) { + const moved = length(vec2(finger.at - finger.was)) + // With no move to speak of, "aim" is nearly nothing and the stroke below is a single spot, as it should be. + const aim = vec2((finger.at - finger.was) / max(moved, 0.01)) + const from = vec2(p.point - finger.was) + const along = dot(from, aim) + const beyond = max(max(-along, along - moved), 0) + const away = max(dot(from, from) - along * along, 0) + beyond * beyond + const light = 1 / (1 + moved * moved / 400) + const hold = 1 - smoothstep(0, 0.35, finger.gone) + const press = (0.35 + 0.65 * finger.down) * (1 - 0.6 * finger.over) + const want = -0.55 * press * cos(2 * PI * 1.2 * finger.age) + next += (want - next) * 0.5 * exp(-away / 200) * hold * light + } + + // With nobody there, two raindrops wander the margins. + for (let j = DROP0; j < BEETLE0; j++) { + const drop = cast.at(vec2(j, 0)) + const off = vec2(p.point - drop.xy) + next += cast.at(vec2(j, 1)).x * push * 0.036 * plop(drop.z, 0.62) * exp(-dot(off, off) / 220) + } + // Each strider's push-off drops a small ripple. + for (let j = 0; j < STRIDERS; j++) { + const off = vec2(p.point - cast.at(vec2(j, 0)).xy) + next += push * 0.007 * plop(cast.at(vec2(j, 1)).y, 0.4) * exp(-dot(off, off) / 90) + } + // Each whirligig trails fine ripples. + for (let j = BEETLE0; j < MINNOW0; j++) { + const off = vec2(p.point - cast.at(vec2(j, 0)).xy) + next += (0.05 * sin(time * 15 + j) - next) * 0.3 * exp(-dot(off, off) / 60) + } + return vec4(clamp(next, -2, 2), me.r, 0, 1) +}) + +// Printing +const LIGHT = { + ground: vec3(214, 231, 224) / 255, + ringTeal: vec3(170, 213, 203) / 255, + ringCoral: vec3(243, 195, 182) / 255, + teal: vec3(22, 131, 125) / 255, + coral: vec3(238, 106, 85) / 255, + over: vec3(91, 58, 68) / 255, + shade: vec3(186, 215, 206) / 255, + minnow: vec3(140, 196, 185) / 255, +} +const DARK = { + ground: vec3(17, 46, 52) / 255, + ringTeal: vec3(35, 100, 96) / 255, + ringCoral: vec3(90, 50, 48) / 255, + teal: vec3(82, 199, 184) / 255, + coral: vec3(245, 132, 111) / 255, + over: vec3(59, 36, 51) / 255, + shade: vec3(9, 28, 33) / 255, + minnow: vec3(42, 117, 111) / 255, +} +// The Svg under the shader is printed in the window's mode; one corner of it tells day from night. +const dayOf = () => step(0.5, dot(scene.sample(vec2(0.004, 0.004)).rgb, vec3(0.3, 0.6, 0.1))) + +// 1 inside a shape whose edge is at "d" = 0, fading to 0 across the edge. "crisp" is one over the edge's full width. +const inside = (d: number, crisp: number) => clamp(0.5 - d * crisp, 0, 1) +// An edge about a pixel and a half wide, never finer than a point. +const crispOf = (px: number) => 1 / max(px * 1.4, 0.9) + +// One halftone screen: dots on a turned grid, each as big as the tone under it asks. +const screenDots = (at: vec2, ca: number, sa: number, pitch: number, tone: number, crisp: number) => { + const r = vec2((at.x * ca + at.y * sa) / pitch, (at.y * ca - at.x * sa) / pitch) + const cell = floor(r) + const off = vec2(r - cell - 0.5) + const wobble = low(blend(int(cell.x), int(cell.y))) + const radius = pitch * 0.31 * sqrt(tone) * (0.85 + 0.25 * wobble) + const d = length(off) * pitch + return inside(d - radius, crisp) * step(0.01, tone) +} + +// How far "at" is from the stroke that runs from a to b. Measured in the stroke's own terms: how far along it the +// point lies, how far it overshoots either end, and how far to the side. +const toLine = (at: vec2, a: vec2, b: vec2) => { + const reach = length(vec2(b - a)) + const aim = vec2((b - a) / reach) + const from = vec2(at - a) + const along = dot(from, aim) + const beyond = max(max(-along, along - reach), 0) + const aside = max(dot(from, from) - along * along, 0) + return sqrt(aside + beyond * beyond) +} + +// Paper tooth: the solid inks do not quite cover. Two numbers, one for each plate. +const toothAt = (at: vec2) => { + const t = high(blend(int(at.x / 1.4), int(at.y / 1.4))) + return vec2(1 - 0.4 * step(0.9, t), 1 - 0.4 * step(t, 0.1)) +} + +// The two plates, laid down as one premultiplied color. +const printed = (inkT: number, inkC: number, at: vec2) => { + const day = dayOf() + const tooth = toothAt(at) + const t = inkT * tooth.x + const c = inkC * tooth.y + return vec4( + mix(DARK.teal, LIGHT.teal, day) * t * (1 - c) + mix(DARK.coral, LIGHT.coral, day) * c * (1 - t) + mix(DARK.over, LIGHT.over, day) * t * c, + 1 - (1 - t) * (1 - c), + ) +} + +const water = picture(p => { + const crisp = crispOf(p.px) + const day = dayOf() + const ground = vec3(mix(DARK.ground, LIGHT.ground, day)) + const ringTeal = vec3(mix(DARK.ringTeal, LIGHT.ringTeal, day)) + const ringCoral = vec3(mix(DARK.ringCoral, LIGHT.ringCoral, day)) + + // A ripple tank shows waves by how they bend light: a crest is a lens. So what prints is not the water's height but + // how sharply it curves: its height here against the water a step away on four sides. Long slow swells curve too + // gently to print at all; only true crests and troughs do. + const ux = 12 / size.x + const uy = 12 / size.y + const height = tank.sample(p.uv).r + const east = tank.sample(vec2(p.uv.x + ux, p.uv.y)).r + const west = tank.sample(vec2(p.uv.x - ux, p.uv.y)).r + const north = tank.sample(vec2(p.uv.x, p.uv.y + uy)).r + const south = tank.sample(vec2(p.uv.x, p.uv.y - uy)).r + const curve = height - (east + west + north + south) / 4 + const sway = vec2(clamp(vec2(east - west, north - south) * 26, -6, 6)) + // Crests print on the teal screen, troughs fainter on the coral screen; the sharper the crest, the bigger its dots. + const crestDots = screenDots(p.point, 0.966, 0.259, 7.5, smoothstep(0.006, 0.07, curve), crisp) + const troughDots = screenDots(p.point, 0.259, 0.966, 7.5, smoothstep(0.006, 0.07, -curve) * 0.6, crisp) + + // Pebbles on the bed, seen through the water: a flat tint, bent by the waves over it. + const bq = vec2((p.point + sway * 2.4) / 118) + const bcell = floor(bq) + const bh = blend(int(int(bcell.x) + 31), int(int(bcell.y) + 17)) + const bturn = high(bh) * 6.3 + const brel = vec2((bq - bcell - vec2(0.3 + 0.4 * low(bh), 0.3 + 0.4 * high(bh))) * 118) + const bsize = 6 + 9 * (low(bh) * 5.7 - floor(low(bh) * 5.7)) + const blong = vec2(brel.x * cos(bturn) + brel.y * sin(bturn), (brel.y * cos(bturn) - brel.x * sin(bturn)) * 1.5) + const pebble = inside(length(blong) - bsize, 0.4) * step(0.55, high(bh) * 3.1 - floor(high(bh) * 3.1)) + + // A film of pollen: fine specks that ride the slope, so a passing ring is seen moving through them. + const q = vec2(p.point + sway * 2 + vec2(time * 3.2, time * -1.9)) + const dq = vec2(q / 23) + const dcell = floor(dq) + const dh = blend(int(int(dcell.x) + 7), int(dcell.y)) + const dust = + inside(length(vec2(dq - dcell - vec2(0.25 + 0.5 * low(dh), 0.25 + 0.5 * high(dh)))) * 23 - 0.75, crisp) * + step(0.5, low(dh) * 7.3 - floor(low(dh) * 7.3)) + + // Small things afloat, one to a cell: bubbles, seeds, a pair of duckweed fronds. The water moves them but, unlike + // the pollen, hardly bends them. + const fq = vec2((p.point + sway * 0.6 + vec2(time * 3.2, time * -1.9)) / 70) + const fcell = floor(fq) + const fh = blend(int(fcell.x), int(fcell.y)) + const kind = low(fh) * 13.7 - floor(low(fh) * 13.7) + const frel = vec2((fq - fcell - vec2(0.22 + 0.56 * low(fh), 0.22 + 0.56 * high(fh))) * 70) + const fsize = high(fh) * 9.1 - floor(high(fh) * 9.1) + const bubble = inside(abs(length(frel) - 1.9 - 1.6 * fsize) - 0.55, crisp) * step(0.42, kind) * step(kind, 0.68) + const lean = vec2(cos(fsize * 6.3), sin(fsize * 6.3)) + const lies = dot(frel, lean) + const spill = max(abs(lies) - 2.6, 0) + const seed = inside(sqrt(max(dot(frel, frel) - lies * lies, 0) + spill * spill) - 0.9, crisp) * step(0.68, kind) * step(kind, 0.9) + const frond = inside(min(length(vec2(frel - lean * 1.5)), length(vec2(frel + lean * 1.6)) + 0.5) - 1.7, crisp) * step(0.9, kind) + + let color = vec3(mix(ground, mix(DARK.shade, LIGHT.shade, day), pebble * 0.4)) + color = vec3(mix(color, ringTeal, crestDots)) + color = vec3(mix(color, ringCoral, troughDots)) + color = vec3(mix(color, ringTeal, dust * 0.8)) + color = vec3(mix(color, mix(ringTeal, mix(DARK.teal, LIGHT.teal, day), 0.5), max(bubble, frond))) + color = vec3(mix(color, mix(ringCoral, mix(DARK.coral, LIGHT.coral, day), 0.6), seed)) + return vec4(color, 1) +}) + +// The creatures, each a small square laid where the cast says +// Each pass says how much of a window its squares may cover, and is not drawn if they cover more. The engine counts +// every square with a pixel and a half added on each edge, so the figures below leave room for coarse pixels. + +// The corner of a square for one of its six vertices (two triangles). +const cornerOf = (v: number) => vec2(v == 1 || v == 2 || v == 4 ? 1 : -1, v == 2 || v == 4 || v == 5 ? 1 : -1) +const squareAt = (middle: vec2, corner: vec2, half: number) => vec2((middle + corner * half) / size * 2 - 1) +// Light falls from the upper left, so a shadow on the bed lies this far to the lower right, in points. +const SHADOW = vec2(9, -11) +// What the water's slope does to a place seen through it. +const swayAt = (uv: vec2) => vec2(clamp(leanAt(uv) * 40, -6, 6)) +// A shadow on the bed: a flat tint, premultiplied. +const shaded = (shadow: number) => vec4(mix(DARK.shade, LIGHT.shade, dayOf()) * shadow * 0.75, shadow * 0.75) + +// A strider's body and feet, in its own frame (x ahead, y out to the side, mirrored). "row" is how far into a stroke. +const bodyOf = (at: vec2) => min((length(vec2((at.x + 2) / 12.5, at.y / 2.6)) - 1) * 2.6, length(vec2(at.x - 11.8, at.y)) - 2.1) +const midKneeOf = (row: number) => vec2(6.5 - row, 13) +const midFootOf = (row: number) => vec2(13 - 12 * row, 29) +const hindKnee = vec2(-13, 10) +const hindFootOf = (row: number) => vec2(-27 + 3 * row, 19) +const foreFoot = vec2(15.5, 7) +// A leaf's blade in its own frame, one unit from middle to tip: round at the stalk, pointed at the tip, fine-toothed. +// Below zero is inside, in points. +const bladeOf = (u: vec2, long: number, wide: number) => { + const x = clamp(u.x, -1, 1) + const half = wide * sqrt(1 + x) * (1 - x) * (0.918 + 0.03 * sin(x * 26)) + return (abs(u.y) - half + max(abs(u.x) - 1, 0)) * long +} +const BEETLE_BIG = 1.5 + +// Minnows, under the surface: flat tints, bent by the water over them. +const minnows = shapes( + { count: 6, instances: MINNOWS, cover: 0.05, draw: 'triangles' }, + (v, i) => { + const who = MINNOW0 + i + const top = cast.at(vec2(who, 0)) + const bottom = cast.at(vec2(who, 1)) + const corner = cornerOf(v) + return { at: squareAt(top.xy, corner, 30), a: vec4(corner * 30, top.zw), b: vec4(bottom.x, bottom.y * zoomOf(), 0, 0) } + }, + spot => { + const crisp = crispOf(spot.px) + const rel = vec2((spot.a.xy + swayAt(spot.uv) * 1.8) / spot.b.y) + const nose = spot.a.zw + const x = dot(rel, nose) + const wag = sin(time * 6 + spot.b.x * 5) + // The tail swings; the body follows a little. + const back = clamp((2 - x) / 14, 0, 1) + const y = nose.x * rel.y - nose.y * rel.x - wag * 4.5 * back * back + const body = (length(vec2((x - 3) / 9.5, y / 3.1)) - 1) * 3.1 + // A forked tail: a wedge with a notch cut into its end. + const tail = max(abs(y) - 0.5 - clamp((-6 - x) / 7, 0, 1) * 4.2, max(-13 + (4.2 - abs(y)) * 0.7 - x, x + 5)) + const fins = toLine(vec2(x, abs(y)), vec2(5, 2.6), vec2(1.5, 6)) - 0.8 + const ink = inside(min(min(body, tail), fins), crisp) * 0.9 + return vec4(mix(DARK.minnow, LIGHT.minnow, dayOf()) * ink, ink) + }, +) + +// Shadows lie on the bed, under everything that floats, so they are laid down before the creatures on the surface. +const leafShadows = shapes( + { count: 6, instances: LEAVES, cover: 0.042, draw: 'triangles' }, + (v, i) => { + const top = cast.at(vec2(LEAF0 + i, 0)) + const corner = cornerOf(v) + const long = LEAF_LONG[i]! * zoomOf() + return { at: squareAt(vec2(top.xy + SHADOW * 1.3), corner, long * 1.05 + 14), a: vec4(corner * (long * 1.05 + 14), top.zw), b: vec4(long, LEAF_WIDE[i]!, 0, 0) } + }, + spot => { + const from = vec2(spot.a.xy + swayAt(spot.uv) * 1.6) + const u = vec2((from.x * spot.a.z + from.y * spot.a.w) / spot.b.x, (from.y * spot.a.z - from.x * spot.a.w) / spot.b.x) + return shaded(inside(bladeOf(u, spot.b.x, spot.b.y), 0.2)) + }, +) + +const striderShadows = shapes( + { count: 6, instances: STRIDERS, cover: 0.125, draw: 'triangles' }, + (v, i) => { + const top = cast.at(vec2(i, 0)) + const corner = cornerOf(v) + const big = STRIDER_SIZE[i]! * zoomOf() + return { at: squareAt(vec2(top.xy + SHADOW), corner, 41 * big + 12), a: vec4(corner * (41 * big + 12), top.zw), b: vec4(cast.at(vec2(i, 1)).x, big, 0, 0) } + }, + spot => { + const big = spot.b.y + const row = spot.b.x + const nose = spot.a.zw + const rel = vec2((spot.a.xy + swayAt(spot.uv) * 1.6) / big) + const s = vec2(dot(rel, nose), abs(nose.x * rel.y - nose.y * rel.x)) + // Each dimple throws a round shadow much bigger than the foot, and the body a thin one. + const edge = big / 4.4 + return shaded( + max( + max(inside(length(vec2(s - midFootOf(row))) - 7.5, edge), inside(length(vec2(s - hindFootOf(row))) - 6.5, edge)), + max(inside(length(vec2(s - foreFoot)) - 3.8, edge), inside(bodyOf(s) - 0.5, edge) * 0.7), + ), + ) + }, +) + +// The beetles' shadows, and the midges', which fall far from them because they are in the air. +const smallShadows = shapes( + { count: 6, instances: BEETLES + MIDGES, cover: 0.02, draw: 'triangles' }, + (v, i) => { + const beetle = i < BEETLES + const top = cast.at(vec2(BEETLE0 + i + (beetle ? 0 : MINNOWS), 0)) + const corner = cornerOf(v) + const big = (beetle ? BEETLE_BIG : cast.at(vec2(BEETLE0 + i + MINNOWS, 1)).x) * zoomOf() + const half = beetle ? 24 : 15 + const long = beetle ? 8 * big : 4.2 * big + return { at: squareAt(vec2(top.xy + SHADOW * (beetle ? 0.7 * big : 1.7)), corner, half), a: vec4(corner * half, top.zw), b: vec4(long, beetle ? 1 : 1.6, 0, 0) } + }, + spot => { + const from = vec2(spot.a.xy + swayAt(spot.uv)) + const nose = spot.a.zw + return shaded(inside(length(vec2(dot(from, nose) / spot.b.y, nose.x * from.y - nose.y * from.x)) - spot.b.x / spot.b.y, 0.3) * 0.9) + }, +) + +// A crown of droplets where a raindrop has just landed. +const crowns = shapes( + { count: 6, instances: DROPS, cover: 0.024, draw: 'triangles' }, + (v, i) => { + const drop = cast.at(vec2(DROP0 + i, 0)) + const corner = cornerOf(v) + return { at: squareAt(drop.xy, corner, 46), a: vec4(corner * 46, drop.zw), b: vec4(cast.at(vec2(DROP0 + i, 1)).x, 0, 0, 0) } + }, + spot => { + const crisp = crispOf(spot.px) + const rel = spot.a.xy + const fly = clamp(spot.a.z / 0.8, 0, 1) + const spoke = floor(atan(rel.y, rel.x) / 0.898 + 0.5) + const luck = low(blend(int(spoke), int(spot.a.w))) + const far = 5 + (16 + 16 * luck) * (1 - (1 - fly) * (1 - fly)) + const bead = length(vec2(rel - vec2(cos(spoke * 0.898), sin(spoke * 0.898)) * far)) + const big = (1.3 + 1.3 * luck) * (1 - fly) + return printed(spot.b.x * inside(bead - big, crisp) * step(fly, 0.98) * step(0.001, fly), 0, spot.point) + }, +) + +const leaves = shapes( + { count: 6, instances: LEAVES, cover: 0.05, draw: 'triangles' }, + (v, i) => { + const top = cast.at(vec2(LEAF0 + i, 0)) + const corner = cornerOf(v) + const long = LEAF_LONG[i]! * zoomOf() + return { at: squareAt(top.xy, corner, long * 1.38 + 6), a: vec4(corner * (long * 1.38 + 6), top.zw), b: vec4(long, LEAF_WIDE[i]!, LEAF_BITE[i]!, i) } + }, + spot => { + const crisp = crispOf(spot.px) + const long = spot.b.x + const wide = spot.b.y + const ca = spot.a.z + const sa = spot.a.w + const frame = (from: vec2) => vec2((from.x * ca + from.y * sa) / long, (from.y * ca - from.x * sa) / long) + const nib = (u: vec2) => spot.b.z * inside((length(vec2(u.x - 0.2, u.y - wide * 0.86)) - 0.2) * long, crisp) + // The teal plate lies true; the coral plate is a little out of register, more so where the water leans. + const ut = frame(spot.a.xy) + const uc = frame(vec2(spot.a.xy + vec2(1.5, -1.1) + swayAt(spot.uv) * 0.35)) + const fill = inside(bladeOf(uc, long, wide), crisp) * (1 - nib(uc)) + const onLeaf = inside(bladeOf(ut, long, wide), crisp) * (1 - nib(ut)) + const within = inside(bladeOf(ut, long, wide) + 2.8, 0.625) * (1 - nib(ut)) + const rib = inside(abs(ut.y) * long - 1, crisp) * step(-1.32, ut.x) * step(ut.x, 0.86) + const lane = (ut.x - 1.15 * abs(ut.y)) * 3.3 + const vein = inside(abs(lane - floor(lane) - 0.5) * long / 5 - 0.7, crisp) * within * step(-0.8, ut.x) + const shade = screenDots(spot.point, 0.966, 0.259, 5.5, 0.62, crisp) * onLeaf * step(ut.y, 0) + // Two beads of water sit on the blade. + const beadAt = (m: vec2, r: number) => inside(abs(length(vec2(ut - m)) * long - r) - 0.7, crisp) * step(0.25, length(vec2(ut - m - vec2(0.03, 0.03))) * long / r) + const beads = max(beadAt(vec2(0.3, -wide * 0.4), 3.2), beadAt(vec2(-0.45, wide * 0.45), 2.3)) * onLeaf + return printed(max(max(rib, vein), max(shade, beads)), fill, spot.point) + }, +) + +const beetles = shapes( + { count: 6, instances: BEETLES, cover: 0.01, draw: 'triangles' }, + (v, i) => { + const top = cast.at(vec2(BEETLE0 + i, 0)) + const corner = cornerOf(v) + return { at: squareAt(top.xy, corner, 22), a: vec4(corner * 22, top.zw), b: vec4(BEETLE_BIG * zoomOf(), 0, 0, 0) } + }, + spot => { + const big = spot.b.x + const w = crispOf(spot.px) * big + const nose = spot.a.zw + const at = vec2(dot(spot.a.xy, nose) / big, abs(nose.x * spot.a.y - nose.y * spot.a.x) / big) + const shell = (length(vec2(at.x / 7.4, at.y / 4.8)) - 1) * 4.8 + // A seam down the back, a collar and two pairs of oar legs are the coral plate's share. + const seam = inside(at.y - 0.6, w) * inside(shell + 1, w) * step(at.x, 3) + const collar = inside(abs(at.x - 3.4) - 0.55, w) * inside(shell + 1, w) + const oars = inside(min(toLine(at, vec2(-1.5, 4), vec2(-7, 9.5)), toLine(at, vec2(3, 4), vec2(6.5, 8))) - 0.7, w) + return printed(inside(shell, w), max(max(seam, collar), oars), spot.point) + }, +) + +const striders = shapes( + { count: 6, instances: STRIDERS, cover: 0.1, draw: 'triangles' }, + (v, i) => { + const top = cast.at(vec2(i, 0)) + const corner = cornerOf(v) + const big = STRIDER_SIZE[i]! * zoomOf() + return { at: squareAt(top.xy, corner, 41 * big + 4), a: vec4(corner * (41 * big + 4), top.zw), b: vec4(cast.at(vec2(i, 1)).x, big, 0, 0) } + }, + spot => { + const big = spot.b.y + const w = crispOf(spot.px) * big + const row = spot.b.x + const nose = spot.a.zw + // Its own frame: x ahead, y out to the side, mirrored. Teal plate true, coral plate out of register. + const own = (rel: vec2) => vec2(dot(rel, nose), abs(nose.x * rel.y - nose.y * rel.x)) + const t = own(vec2(spot.a.xy / big)) + const c = own(vec2((spot.a.xy + vec2(1.5, -1.1) + swayAt(spot.uv) * 0.35) / big)) + const midKnee = midKneeOf(row) + const midFoot = midFootOf(row) + const hindFoot = hindFootOf(row) + const legs = min( + min(toLine(c, vec2(2.5, 1.4), midKnee), toLine(c, midKnee, midFoot)), + min(min(toLine(c, vec2(-3, 1.4), hindKnee), toLine(c, hindKnee, hindFoot)), toLine(c, vec2(7.5, 1.2), foreFoot)), + ) + const knees = min(length(vec2(c - midKnee)), length(vec2(c - hindKnee))) - 1.35 + const feeler = toLine(c, vec2(13, 0.9), vec2(19, 4.6)) - 0.4 + const coral = max(max(inside(bodyOf(c), w), inside(legs - 0.8, w)), max(inside(knees, w), inside(feeler, w))) + // A dimple under each foot: a ring, heavier on its outer side. + const dimple = (foot: vec2, r: number) => inside(abs(length(vec2(t - foot)) - r) - 1, w) * (0.5 + 0.5 * clamp((t.y - foot.y) / r + 0.3, -1, 1)) + // The teal plate also carries a stripe down the back, three bands across the tail, and the eyes. + const stripe = inside(length(vec2(t.x - clamp(t.x, -1, 7), t.y)) - 0.6, w) + const bands = inside(abs(t.x + 7.5 - 3 * clamp(floor((t.x + 7.5) / 3 + 0.5), -1, 1)) - 0.45, w) * inside(t.y - 2.2, w) + const eyes = inside(length(vec2(t.x - 12.6, t.y - 1.6)) - 0.95, w) + const teal = max(max(max(dimple(midFoot, 5.4), dimple(hindFoot, 4.8)), max(dimple(foreFoot, 3), stripe)), max(bands, eyes)) + return printed(teal, coral, spot.point) + }, +) + +// Midges dance in the air over the water. +const midges = shapes( + { count: 6, instances: MIDGES, cover: 0.01, draw: 'triangles' }, + (v, i) => { + const top = cast.at(vec2(MIDGE0 + i, 0)) + const corner = cornerOf(v) + const big = cast.at(vec2(MIDGE0 + i, 1)).x * zoomOf() + return { at: squareAt(top.xy, corner, 8 * big + 3), a: vec4(corner * (8 * big + 3), top.zw), b: vec4(big, i, 0, 0) } + }, + spot => { + const big = spot.b.x + const w = crispOf(spot.px) * big + const nose = spot.a.zw + const at = vec2(dot(spot.a.xy, nose) / big, abs(nose.x * spot.a.y - nose.y * spot.a.x) / big) + const body = (length(vec2(at.x / 3.6, at.y / 1.2)) - 1) * 1.2 + // Wings beat too fast to see: each prints as a pale fan. + const wing = (length(vec2((at.x + 1.2 - at.y * 0.45) / 2.3, (at.y - 3) / 3)) - 1) * 2.3 + return printed(inside(wing, w) * 0.7, inside(body, w), spot.point) + }, +) + +export default shader({ + fps: 30, + passes: [cast, tank, water, minnows, leafShadows, striderShadows, smallShadows, crowns, leaves, beetles, striders, midges], +}) diff --git a/plugins/theme-examples/hooks/scenes/ripple-tank/still.ts b/plugins/theme-examples/hooks/scenes/ripple-tank/still.ts new file mode 100644 index 00000000..a4a74549 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/ripple-tank/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Ripple Tank, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function rippleTankSvg(): string { + return ( + "Ripple TankWater striders and drifting leaves on rippled water, printed in teal and coral halftone." + ) +} diff --git a/plugins/theme-examples/hooks/scenes/sea-sparkle/scene.ts b/plugins/theme-examples/hooks/scenes/sea-sparkle/scene.ts new file mode 100644 index 00000000..f26fab16 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/sea-sparkle/scene.ts @@ -0,0 +1,618 @@ +// Sea Sparkle. A three-ink linocut of a night sea seen from above: two dark inks and one luminous ink, all flat. +// Light is never a soft bloom here. It is stipple: carved grains whose size and count carry the brightness. +// Passes: "seed" works out once the wakes the swimmers laid before the start, "school" keeps where the swimmers are (a +// few texels of state, so they can shy from a pointer and drift back), "wake" keeps how stirred the water is (a +// quarter-scale buffer that fades over a few seconds), "sea" draws the ground and the plankton over every pixel, and +// four small "shapes" draws lay the moon jellies, the fry, the ray and the fish on top. +import { + abs, atan, buffer, clamp, composer, cos, dot, dt, exp, first, floor, int, length, max, min, mix, mod, normalize, + picture, pointer, pointers, scene, shader, shapes, sin, size, smoothstep, sqrt, step, table, time, unsigned, vec2, + vec3, vec4, +} from 'claude-code/shader' +import type { Buffer } from 'claude-code/shader' + +// Inks. +// Midnight proof: deep ink, slate ink, luminous mint. Morning proof: the same blocks pulled on pale paper, +// with deep teal where the luminous ink was. +const inks = () => { + // There is no light or dark input. The Svg under the shader is written in the shown mode, so read three spots of it. + const seen = vec3( + scene.sample(vec2(0.03, 0.97)).rgb + scene.sample(vec2(0.97, 0.97)).rgb + scene.sample(vec2(0.5, 0.02)).rgb, + ) + const day = smoothstep(1.2, 1.8, seen.r * 0.3 + seen.g * 0.6 + seen.b * 0.1) + return { + day, + deep: mix(vec3(0.035, 0.055, 0.11), vec3(0.925, 0.94, 0.932), day), + slate: mix(vec3(0.1, 0.165, 0.31), vec3(0.78, 0.85, 0.875), day), + glow: mix(vec3(0.59, 0.96, 0.84), vec3(0.115, 0.415, 0.5), day), + } +} +// The luminous block is printed a little out of register, in paper space. +const SLIP = vec2(1.6, -1.1) + +// Numbers from cells. +// An integer mix: two odd multipliers fold the cell in, then a rotate-xor, a multiply, an add-shift and a last +// multiply-xor. +const turned = (h: number, by: number) => (h << by) | (h >>> (32 - by)) +const cellMix = (ix: number, iy: number, salt: number) => { + let h = int(ix) * 0x181f02b7 + int(iy) * 0x317adc0d + salt + h = (h ^ turned(h, 9) ^ turned(h, 22)) * 0x75ffbd1f + h = h + (h >>> 12) + h = (h * 0x7a31bd2b) ^ (h >>> 18) + return h +} +// A second draw of numbers from a mix. +const again = (h: number) => (h * 0x1ed77a81) ^ (h >>> 11) +// Ten bits of a mix as a number from 0 to 1. +const share = (h: number, from: number) => unsigned((h >>> from) & 1023) / 1024 +// A soft-edged cut: 1 where "inside" (in points) is positive, blended over one pixel. "sharp" is pixels a point. +const cutOf = (inside: number, sharp: number) => clamp(0.5 + inside * sharp, 0, 1) + +// Where the swimmers are. +// Each fish: how far behind the leader along the path (radians), how far to the side of the path (points), where +// round a held finger it comes to feed (an angle), and its size. Neighbors swim on opposite sides of the path and a +// body length apart along it, so no two bodies overlap anywhere on the lap. +const FISH = table([ + [0, 20, 0, 1.8], + [0.13, -30, 1.05, 1.5], + [0.26, 34, 2.1, 1.6], + [0.39, -22, 3.14, 1.3], + [0.52, 30, 4.19, 1.9], + [0.65, -36, 5.24, 1.4], + [3.2, 4, 0.5, 1.7], + [3.33, -32, 2.6, 1.3], + [3.46, 30, 4.7, 1.5], +]) +const FISH_COUNT = 9 +const RAY_SLOT = 9 +const SWARM_SLOT = 10 +const FRY_COUNT = 22 +// The ray is drawn in its own units, this many points each. +const RAY_BIG = 1.4 + +// Moon jellies: where each drifts about (shares of the window), its size in points, its own phase. +// They keep to the four corners and the middles of the two loops the ray flies, clear of its wings. +const JELLY = table([ + [0.205, 0.5, 20, 0.4], + [0.07, 0.1, 15, 2.1], + [0.785, 0.52, 22, 3.3], + [0.94, 0.1, 16, 4.9], + [0.07, 0.9, 17, 5.6], + [0.94, 0.88, 18, 1.3], +]) +const JELLY_COUNT = 6 +const jellyHome = (row: vec4) => + vec2(size.x * (row.x + 0.02 * sin(time * 0.021 + row.w)), size.y * (row.y + 0.03 * cos(time * 0.017 + 2 * row.w))) +// The bell beats slowly. +const jellyBig = (row: vec4) => row.z * (1 + 0.1 * sin(time * 1.4 + row.w * 3)) + +// The ray glides a figure of eight that loops in both side margins, about two and a quarter minutes a lap. It crosses +// the middle, where the words are, a little faster than it rounds the margins. +const rayHome = (at: number) => { + const along = 0.95 + at * 0.046 + const a = along + 0.15 * sin(2 * along) + const pos = vec2(size.x * (0.5 + 0.42 * sin(a)), size.y * (0.5 + 0.27 * sin(2 * a))) + const dir = normalize(vec2(size.x * 0.42 * cos(a), size.y * 0.54 * cos(2 * a))) + return { pos, dir } +} +// The fish go round the window the other way on a lumpy ring. +const fishHome = (lag: number, at: number) => { + const a = 2.4 - at * 0.06 + lag + const pos = vec2(size.x * (0.5 + 0.41 * cos(a)), size.y * (0.5 + 0.32 * sin(a) + 0.06 * sin(3 * a + 1))) + const dir = normalize(vec2(size.x * 0.41 * sin(a), -size.y * (0.32 * cos(a) + 0.18 * cos(3 * a + 1)))) + return { pos, dir } +} + +// A swarm of fry wanders a slow path of its own; each fry circles the swarm's middle at its own pace. +const swarmHome = (at: number) => { + const a = 5.5 + at * 0.031 + return vec2(size.x * (0.5 + 0.43 * sin(a)), size.y * (0.5 + 0.3 * sin(1.7 * a + 1))) +} + +// Row 0: where each swimmer is and which way it faces. Row 1: how far it has shied from the pointers, eased twice +// over (a push moves the first stage, the first moves the second), so a scrubbed pointer cannot shake it or spin it. +const school: Buffer = buffer({ size: [11, 2], float: 32, filter: 'nearest' }, (p): vec4 => { + const slot = p.cell.x + const row = FISH.at(slot) + let home = vec2(0, 0) + let dir = vec2(1, 0) + let reach = 150 + let far = 46 + let ease = 0.4 + let lured = 1 + if (slot > 9.5) { + home = swarmHome(time) + reach = 170 + far = 60 + ease = 0.6 + } else if (slot > 8.5) { + const ray = rayHome(time) + home = ray.pos + dir = ray.dir + reach = 230 + far = 36 + ease = 0.9 + lured = 0 + } else { + const fish = fishHome(row.x, time) + const bob = vec2(sin(time * 0.6 + slot * 2.1), cos(time * 0.47 + slot * 1.3)) * 5 + home = vec2(fish.pos + vec2(-fish.dir.y, fish.dir.x) * row.y + bob) + dir = fish.dir + } + const kept = school.at(vec2(slot, 1)) + const shied = kept.zw + // Its own place round a held finger, 56 points out. + const place = vec2(cos(row.z), sin(row.z)) * 56 + let push = vec2(0, 0) + for (const finger of pointers) { + const away = vec2(home + shied - finger.at) + const d = length(away) + const hold = 1 - smoothstep(0, 0.8, finger.gone) + // A pointer scares, moving or hovering. A finger pressed and held nearly still draws the fish and fry in to feed + // on what it stirs: each leaves its path by up to 80 points for its own place beside the finger. + const speed = length(vec2(finger.at - finger.was)) / max(dt, 0.004) + const calm = lured * finger.down * smoothstep(0.3, 1.2, finger.age) * exp(-speed / 60) + const lure = vec2(finger.at + place - home) + const gap = length(lure) + push = vec2(push + (away / max(d, 1)) * far * exp(-(d * d) / (reach * reach)) * hold * (1 - calm)) + push = vec2(push + (lure / max(gap, 1)) * min(gap, 80) * exp(-(gap * gap) / 90000) * hold * calm) + } + push = vec2(push * min(1, 80 / max(length(push), 1))) + const gain = 1 - exp(-dt / ease) + const early = vec2(kept.xy + (push - kept.xy) * gain) + const next = vec2(shied + (early - shied) * gain) + if (p.cell.y > 0.5) return vec4(early, next) + // It turns a little toward where it is drifting, by the eased stages only. + return vec4(home + next, normalize(dir + (early - next) * 0.03)) +}) + +// How far a point is outside the column of text, 0 to 1: the column is as wide as the prompt box and stands over it. +// Before the app has measured the box, take a column 784 points wide in the middle. The swimmers' wakes and the +// plankton's own breathing are held down inside the column, so what passes behind the words stays gentle. +const marginOf = (x: number) => { + const known = step(0.001, composer.z - composer.x) + const middle = mix(0.5, (composer.x + composer.z) * 0.5, known) * size.x + const half = mix(392, (composer.z - composer.x) * 0.5 * size.x + 24, known) + return smoothstep(half - 70, half + 90, abs(x - middle)) +} + +// How far a point is from the straight stretch between two places, in the stretch's own frame: across it, and along +// it only past either end. The second measure covers a stretch of no length, which has no frame. +const farFromStretch = (point: vec2, from: vec2, to: vec2) => { + const run = vec2(to - from) + const long = length(run) + const heading = vec2(run / max(long, 0.001)) + const off = vec2(point - from) + const along = dot(off, heading) + const across = dot(off, vec2(-heading.y, heading.x)) + const overshoot = along - clamp(along, 0, long) + return max(length(vec2(overshoot, across)), length(off) - long) +} + +// How stirred the water is. +// What the ray's trailing edge stirs at one place: a ribbon off the whole edge, strongest at the wingtips. +const rayStir = (q: vec2, pos: vec2, dir: vec2) => { + const rel = vec2(q - pos) / RAY_BIG + const u = dot(rel, dir) + const v = abs(dot(rel, vec2(-dir.y, dir.x))) + const out = smoothstep(0.75, 1, v / 74) + const off = length(vec2(u + 28 - 8 * out, max(v - 74, 0))) * RAY_BIG + return mix(0.75, 1.3, out) * exp(-off / mix(14, 18, out)) +} +// The wakes the swimmers would have laid down before the scene began, worked out once from where their paths ran over +// the seconds before the start, so the first frame already has its veil and trails, as the Svg under the shader does. +const SEED_STEPS = 28 +const seed = buffer({ scale: 0.25, float: 16, filter: 'linear', once: true }, p => { + const q = p.point + let trail = 0 + for (let k = 0; k < SEED_STEPS; k++) { + const ago = k * 0.5 + const faded = exp(-ago / 6) + const ray = rayHome(-ago) + trail = max(trail, rayStir(q, ray.pos, ray.dir) * faded) + for (let i = 0; i < FISH_COUNT; i++) { + const row = FISH.at(i) + const now = fishHome(row.x, -ago) + const before = fishHome(row.x, -ago - 0.5) + const d = farFromStretch( + q, + vec2(now.pos + vec2(-now.dir.y, now.dir.x) * row.y - now.dir * 16 * row.w), + vec2(before.pos + vec2(-before.dir.y, before.dir.x) * row.y - before.dir * 16 * row.w), + ) + trail = max(trail, 1.2 * exp(-d / 10) * faded) + } + } + return vec4(0, trail * (0.4 + 0.6 * marginOf(q.x)), 0, 0) +}) + +const PAIRS = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]] +const wake: Buffer = buffer({ scale: 0.25, float: 16, filter: 'linear' }, (p): vec4 => { + const q = p.point + // Slow eddies carry the old wake, so it curls and frays as it dies. + const eddy = + vec2( + sin(q.y * 0.021 + time * 0.3) + 0.6 * sin(q.y * 0.047 - time * 0.4 + q.x * 0.013), + cos(q.x * 0.019 - time * 0.25) + 0.6 * cos(q.x * 0.043 + time * 0.35 - q.y * 0.011), + ) * 15 + vec2(3, 0) + const was = wake.sample(vec2(p.uv - (eddy * dt) / size)) + let stir = was.r * exp(-dt / 2.6) + let trail = first ? seed.at(p.cell).g : was.g * exp(-dt / 6) + // Ripples are kept apart and die fast, so a ring reads as a ring traveling and not as a filled pool. + let ripple = was.b * exp(-dt / 0.45) + // The ring itself, as a line for the slate ink: it dies faster still, so the line stays thin. + let ringLine = was.a * exp(-dt / 0.1) + // Inside the column of text every stamp is held down, the pointer's too, so the words stay easy to read. + const hush = 0.4 + 0.6 * marginOf(q.x) + // Each pointer stamps the stretch it covered since last frame, so a fast stroke leaves no gaps. + for (const finger of pointers) { + const d = farFromStretch(q, finger.was, finger.at) + const here = 1 - step(0.0001, finger.gone) + const reach = 14 + 10 * finger.down * smoothstep(0, 1.5, finger.age) + // Stirred past full: the grains read at most 1, so water stirred again and again stays lit and does not blink. + const power = (0.9 + 0.9 * finger.down) * (1 - 0.55 * finger.over) * hush + stir = max(stir, power * here * exp(-d / reach)) + // A press sends out a ripple, and another every 0.9 s while it is held still, two in the water at a time. A tap's + // ripple keeps traveling through the second its pointer lingers after lifting. The ring is cut in the slate ink, + // which is close to the ground in tone, and wakes only a thin line of plankton as it passes, so that ripples + // crossing a spot one after another never add up to a flash. + const speed = length(vec2(finger.at - finger.was)) / max(dt, 0.004) + const beat = mod(finger.age, 0.9) + const reachOut = length(vec2(q - finger.at)) + const lasting = + finger.down * (1 - smoothstep(0.6, 1, finger.gone)) * (1 - 0.55 * finger.over) * exp(-speed / 60) * hush + for (const older of [0, 1]) { + const lived = beat + 0.9 * older + const ring = abs(reachOut - 16 - 75 * lived) + const alive = lasting * step(0.9 * older, finger.age) * exp(-lived * 0.5) + ripple = max(ripple, 0.8 * alive * exp(-ring / 4)) + ringLine = max(ringLine, alive * exp(-ring / 3.5)) + } + } + // Two or more pressed pointers: a thread of stirred water is strung between each pair. + for (const pair of PAIRS) { + const one = pointer(pair[0]!) + const two = pointer(pair[1]!) + // Only between fingers that rest or move slowly: under four fast fingers the threads would fill the window. + const hurry = (length(vec2(one.at - one.was)) + length(vec2(two.at - two.was))) / max(dt, 0.004) + const both = + one.down * two.down * (1 - step(0.0001, one.gone)) * (1 - step(0.0001, two.gone)) * exp(-hurry / 240) + stir = max(stir, 0.9 * both * hush * exp(-farFromStretch(q, one.at, two.at) / 7)) + } + // The swimmers' own wakes, held down the same way. + const old = trail + // The ray sheds a veil of stirred water off its whole trailing edge, strongest at the wingtips. + const ray = school.at(vec2(RAY_SLOT, 0)) + trail = max(trail, rayStir(q, ray.xy, ray.zw)) + for (let i = 0; i < FISH_COUNT; i++) { + const fish = school.at(vec2(i, 0)) + const d = length(vec2(q - fish.xy + fish.zw * 16 * FISH.at(i).w)) + trail = max(trail, 1.2 * exp(-d / 10)) + } + // The fry stir a faint cloud about their swarm. + const swarm = length(vec2(q - school.at(vec2(SWARM_SLOT, 0)).xy)) + trail = max(trail, 0.42 * exp(-(swarm * swarm) / 2500)) + // Each jelly keeps a faint halo of stirred water about its rim. + for (let i = 0; i < JELLY_COUNT; i++) { + const row = JELLY.at(i) + const rim = abs(length(vec2(q - jellyHome(row))) - jellyBig(row) * 1.3) + trail = max(trail, 0.5 * exp(-rim / 7)) + } + trail = max(old, trail * hush) + return vec4(stir, trail, ripple, ringLine) +}) + +// The sea: ground and plankton. +const sea = picture(p => { + const q = p.point + const ink = inks() + const sharp = 1 / p.px + + // Swell cuts: rows of long lens-shaped gouges in the slate ink, sliding a few points a second, row against row. + const rowHeight = 54 + const row = floor(q.y / rowHeight + 0.5) + const side = mod(row, 2) * 2 - 1 + const xs = q.x + side * time * 5 + row * 131 + const stirred = wake.sample(p.uv) + // The cuts ride up a little on stirred water, and settle as it calms. + const lift = 6 * min(stirred.r, 1) + const mid = row * rowHeight + 8 * sin(xs * 0.011 + row * 1.7) + 3.5 * sin(xs * 0.027 - row) + lift + const fat = 3.1 * sin(xs * 0.018 + row * 2.3) + 1.3 * sin(xs * 0.049 + row) - 0.7 + const fine = 1.5 * sin(xs * 0.023 + row * 4.1 + 2) - 0.8 + const swell = max(cutOf(fat - abs(q.y - mid), sharp), cutOf(fine - abs(q.y - mid + 12), sharp)) + // Current nicks: short thin cuts of the same ink between the rows, drifting slowly the one way. + const qn = vec2(q.x + 2.2 * time, q.y) + const cn = floor(vec2(qn / vec2(46, 18))) + const hn = cellMix(cn.x, cn.y, 71) + const hm = again(hn) + const nickAt = vec2((cn + 0.5) * vec2(46, 18) + (vec2(share(hn, 10), share(hn, 20)) - 0.5) * vec2(26, 9)) + const nickHalf = 4 + 5 * share(hm, 0) + const nx = qn.x - nickAt.x + const nick = + cutOf(0.8 * (1 - abs(nx) / nickHalf) - abs(qn.y - nickAt.y - (share(hm, 10) - 0.5) * 0.16 * nx), sharp) * + step(share(hn, 0), 0.42) + // Ripples from a press: rings in the slate ink, narrower as they spread. On pale paper that ink stands well off the + // ground, so there the rings print faint: a ring that sweeps a spot again and again must never count as a flash. + const rings = cutOf((stirred.a - 0.4) * 7, sharp) * (0.9 - 0.55 * ink.day) + let color = mix(ink.deep, ink.slate, max(max(swell, rings), nick * 0.8)) + + const ql = vec2(q + SLIP) + // Grains breathe on and off by themselves, most of all in slow drifting clouds where the plankton gather. + const cloud = + 0.2 + + 0.8 * + smoothstep( + -0.1, + 0.8, + sin(q.x * 0.0052 + time * 0.031 + 1) * sin(q.y * 0.0075 - time * 0.023) + + 0.35 * sin((q.x + q.y) * 0.011 + time * 0.04), + ) + const field = min(min(stirred.r, 1) + min(stirred.g, 1) + stirred.b, 1) + // Water stirred hard prints its grains a little fatter. + const hot = 1 + 0.3 * smoothstep(1, 1.7, stirred.r) + // The tide's breath: every two minutes a soft front crosses the sea, and plankton wake along it as it passes. + // It crosses 0.8 x + 0.6 y from 400 before the window to 400 past it, so it comes and goes out of sight. + const front = + q.x * 0.8 + q.y * 0.6 + 40 * sin(q.y * 0.008 + time * 0.05) + 400 - + mod(time * 22 + 920, 0.8 * size.x + 0.6 * size.y + 800) + const quiet = 0.55 + 0.45 * marginOf(q.x) + const tide = exp(-(front * front) / 12000) * quiet + const roused = cloud * cloud * 0.28 * quiet + 0.3 * tide + // Inside the front a grain spends more of its slow beat lit. + const wakeful = 0.86 - 0.5 * tide + + // Pecks, one to a cell of 7.5 points. Each layer's lattice is turned so that no rows line up with the window. + const qa = vec2(ql.x * 0.956 + ql.y * 0.292 - 3.2 * time, ql.y * 0.956 - ql.x * 0.292 + 1.1 * time) + const ca = floor(vec2(qa / 7.5)) + const ha = cellMix(ca.x, ca.y, 17) + const hb = again(ha) + const centerA = vec2((ca + 0.5) * 7.5 + (vec2(share(ha, 0), share(ha, 10)) - 0.5) * 5.4) + const breathA = + smoothstep(0, 0.1, 0.06 + roused - share(hb, 0)) * + max(0, sin(time * (0.25 + 0.3 * share(hb, 10)) + share(hb, 20) * 6.283) - wakeful) * 7 + const litA = smoothstep(0, 0.3, field + breathA - 0.03 - 0.95 * share(ha, 20)) + const radA = (1 + 1.2 * share(hb, 22)) * litA * hot + const peck = cutOf(radA - length(vec2(qa - centerA)), sharp) * min(1, litA * 5) + + // Larger rice-grain cuts, one to a cell of 23 points, slanted the way the carver's hand sat. + const qb = vec2(ql.x * 0.875 - ql.y * 0.485 + 4.6 * time, ql.y * 0.875 + ql.x * 0.485 - 0.8 * time) + const cb = floor(vec2(qb / 23)) + const hc = cellMix(cb.x, cb.y, 91) + const hd = again(hc) + const centerB = vec2((cb + 0.5) * 23 + (vec2(share(hc, 0), share(hc, 10)) - 0.5) * 13) + const breathB = + smoothstep(0, 0.1, 0.12 + 2 * roused - share(hd, 0)) * + max(0, sin(time * (0.2 + 0.3 * share(hd, 10)) + share(hd, 20) * 6.283) - wakeful) * 7 + const litB = smoothstep(0, 0.3, field + breathB - 0.05 - 0.95 * share(hc, 20)) + const d = vec2(qb - centerB) + const lean = 0.8 + 0.5 * (share(hd, 22) - 0.5) + const ex = d.x * 0.6 + d.y * lean + const ey = d.y * 0.6 - d.x * lean + const half = 4 + 3 * share(hc, 22) + const wide = 1.6 + share(hd, 12) + // A gouge mark: blunt where the tool went in, pointed where it came out. + const taper = 1 - 0.45 * clamp(ex / half, -1, 1) + const grain = cutOf(wide * litB * hot - length(vec2((ex * wide) / half, ey / taper)), sharp) * min(1, litB * 5) + // Some grains take the ink thin. + const thin = 1 - 0.4 * step(0.8, share(hd, 2)) + + // Sparks: where the water is stirred hard, a few four-pointed nicks, two crossed cuts each. + const qc = vec2(ql.x * 0.766 + ql.y * 0.643 + 1.5 * time, ql.y * 0.766 - ql.x * 0.643 + 2.4 * time) + const cc = floor(vec2(qc / 41)) + const he = cellMix(cc.x, cc.y, 53) + const centerC = vec2((cc + 0.5) * 41 + (vec2(share(he, 0), share(he, 10)) - 0.5) * 26) + const litC = smoothstep(0, 0.35, field - 0.25 - 0.7 * share(he, 20)) + const dc = vec2(abs(qc - centerC)) + const reachC = (5 + 3 * share(he, 22)) * litC + const star = sqrt(dc.x) + sqrt(dc.y) + const spark = cutOf(reachC - star * star, sharp * 0.5) * min(1, litC * 5) + + color = mix(color, ink.glow, max(max(peck, grain * thin), spark)) + return vec4(color, 1) +}) + +// The moon jellies. +const CORNERS = table([[-1, -1], [1, -1], [1, 1], [-1, -1], [1, 1], [-1, 1]]) +const jellyDraw = shapes( + { count: 6, instances: JELLY_COUNT, cover: 0.035 }, + (vertex, instance) => { + const row = JELLY.at(instance) + const big = jellyBig(row) + const local = vec2(CORNERS.at(vertex) * 1.36) + return { at: vec2(((jellyHome(row) + local * big) / size) * 2 - 1), a: vec4(local, instance, big) } + }, + spot => { + const ink = inks() + const big = spot.a.w + const sharp = 1 / spot.px + const spin = JELLY.at(spot.a.z).w + time * 0.04 + // The slate block: a scalloped disc, seen from above. + const r = length(vec2(spot.a.xy)) + const disc = cutOf((1 + 0.05 * cos(8 * (atan(spot.a.y, spot.a.x) - spin)) - r) * big, sharp) + // The luminous block, slipped: the rim, a rosette of four rings, sixteen rim ticks and a fringe of pecks. + const pl = vec2(spot.a.xy - SLIP / big) + const rl = length(pl) + const around = ((atan(pl.y, pl.x) - spin) * 16) / 6.2832 + const rim = cutOf(0.6 - abs(rl - 0.97 - 0.0485 * cos(around * 3.1416)) * big, sharp) + const folded = vec2(abs(pl.x * cos(spin) + pl.y * sin(spin)), abs(pl.y * cos(spin) - pl.x * sin(spin))) + const rosette = cutOf(0.6 - abs(length(vec2(folded - 0.2546)) - 0.2) * big, sharp) + const spoke = abs(mod(around, 1) - 0.5) * 0.3927 * rl * big + const ticks = cutOf(0.4 - spoke, sharp) * step(0.68, rl) * step(rl, 0.86) + const between = (mod(around + 0.5, 1) - 0.5) * 0.3927 * rl * big + const fringe = cutOf(0.9 - length(vec2((rl - 1.18) * big, between)), sharp) + const lit = max(max(rim, rosette), max(ticks, fringe)) + const cover = max(disc * 0.85, lit) + return vec4(mix(ink.slate, ink.glow, lit) * cover, cover) + }, +) + +// The fry. +// Where fry number i is about the swarm's middle, and which way it heads: each circles on an oval of its own, at its +// own pace, weaving in and out. +const fryAbout = (i: number) => { + const reach = (12 + 34 * mod(i * 0.37 + 0.11, 1)) * (1 + 0.28 * sin(time * 0.9 + i * 1.7)) + const turn = i * 2.4 + time * (0.25 + 0.3 * mod(i * 0.73, 1)) + const flat = 0.5 + 0.45 * mod(i * 0.53, 1) + return { at: vec2(cos(turn), flat * sin(turn)) * reach, dir: normalize(vec2(-sin(turn), flat * cos(turn))) } +} +const fryDraw = shapes( + { count: 6, instances: FRY_COUNT, cover: 0.03 }, + (vertex, instance) => { + const corner = CORNERS.at(vertex) + const fry = fryAbout(instance) + const local = vec2(-0.75 + corner.x * 6.75, corner.y * 3.2) + const world = vec2( + school.at(vec2(SWARM_SLOT, 0)).xy + fry.at + (fry.dir * local.x + vec2(-fry.dir.y, fry.dir.x) * local.y) * 1.5, + ) + return { at: vec2((world / size) * 2 - 1), a: vec4(local, instance, 0) } + }, + spot => { + const ink = inks() + const u = spot.a.x + // A sliver with a forked tail, wriggling. + const v = abs(spot.a.y - 0.7 * clamp((2 - u) / 6, 0, 1) * sin(u * 0.9 + time * 11 + spot.a.z * 2.3)) + const t = clamp((u + 3.5) / 9, 0, 1) + const body = min(6.2 * t * (1 - t) * (0.5 + 0.5 * t) - v, u + 3.5) + const back = clamp((-3 - u) / 3, 0, 1) + const tail = min(min(0.35 + 1.5 * back - v, v - (back - 0.45) * 1.6), min(-3 - u, u + 6)) + const cover = cutOf(max(body, tail), 1.5 / spot.px) * 0.9 + return vec4(ink.glow * cover, cover) + }, +) + +// The ray. +// The ray's outline in its own units: positive inside. Swept wings with hooked tips, a broad head with the mouth bitten +// out between two head fins, a whip tail that sways. "flap" runs -1 to 1 over a wingbeat. +const rayShape = (u: number, v: number, flap: number) => { + const span = 76 + 7 * flap + const back = 5 - 5 * flap + const s = abs(v) / span + const s2 = s * s + const lead = 30 - (22 + back) * s2 - 22 * s2 * s2 + const rear = -34 + 34 * s - back * s2 - 14 * s2 * s + const wing = min(lead - u, u - rear) * 0.8 + const mouth = (1 - length(vec2((u - 46) / 9, v / 8.5))) * 8.5 + const head = min((1 - length(vec2((u - 24) / 20, v / 17))) * 17, -mouth) + const down = clamp((-30 - u) / 92, 0, 1) + const sway = 11 * down * down * sin(u * 0.05 + time * 1.3 + 0.6) + const tail = min(2.7 - 2.4 * down - abs(v - sway), min(-28 - u, u + 122)) + // The knife wanders a little: a small wobble on every edge. + const wobble = 0.5 * sin(u * 0.83 + v * 0.31) * sin(v * 0.71 - u * 0.27) + // Two small pelvic fins at the root of the tail. + const pelvic = (1 - length(vec2((u + 36) / 7, (abs(v) - 6.5 + 0.25 * (u + 36)) / 3.4))) * 3.4 + return { inside: max(max(wing, head), max(tail, pelvic)) + wobble, s, lead, rear, down, sway } +} +const rayDraw = shapes( + { count: 6, cover: 0.08 }, + (vertex, instance) => { + const corner = CORNERS.at(vertex) + const ray = school.at(vec2(RAY_SLOT, 0)) + const across = vec2(-ray.w, ray.z) + const local = vec2(-39 + corner.x * 90, corner.y * 90) + const world = vec2(ray.xy + (ray.zw * local.x + across * local.y) * RAY_BIG) + // The luminous block's slip, turned into the ray's own frame. + const slip = vec2(dot(SLIP, ray.zw), dot(SLIP, across)) / RAY_BIG + return { at: vec2((world / size) * 2 - 1), a: vec4(local, slip) } + }, + spot => { + const ink = inks() + const edge = RAY_BIG / spot.px + const flap = sin(time * 1.6 + 0.58) + // The slate block: the silhouette, with a spine and four gill slits a side carved back to the deep ink. + const u = spot.a.x + const v = spot.a.y + const body = cutOf(rayShape(u, v, flap).inside, edge) + const spine = cutOf(0.6 - abs(v), edge) * step(-30, u) * step(u, 14) + const slit = abs(mod(u + 0.35 * abs(v) - 1, 4.5) - 2.25) + const gills = cutOf(0.6 - slit, edge) * step(3, u) * step(u, 21) * step(6, abs(v)) * step(abs(v), 13.5) + // The luminous block, slipped. Under the slate it prints the whole outline a touch fat, so a bright sliver shows + // along one side; over it, a dashed rim on the leading edges, eyes, stipple on the back and beads down the tail. + const ul = u - spot.a.z + const vl = v - spot.a.w + const lum = rayShape(ul, vl, flap) + const under = cutOf(lum.inside + 0.8, edge) + const behind = lum.lead - ul + const dash = step(mod(lum.s * 11, 1), 0.7) * step(0.16, lum.s) * step(lum.s, 0.98) + const rim = cutOf(1.7 * (1 - 0.5 * lum.s) - abs(behind - 2.8), edge) * dash + const eyes = cutOf(1.8 - length(vec2(ul - 33, abs(vl) - 11)), edge) + // Stipple shading on the back, densest just behind the rim, thinning toward the trailing edge. + const cell = floor(vec2(vec2(ul, vl) / 5)) + const hs = cellMix(cell.x, cell.y, 29) + const place = vec2((cell + 0.5) * 5 + (vec2(share(hs, 0), share(hs, 10)) - 0.5) * 2.4) + const deepIn = clamp(behind / max(lum.lead - lum.rear, 1), 0, 1) + const take = + step(share(hs, 20), 1 - 1.4 * deepIn) * step(5.5, behind) * step(0.14, lum.s) * step(lum.s, 1) * step(lum.rear + 3, ul) + const stipple = cutOf(0.65 + 0.65 * share(hs, 22) - length(vec2(vec2(ul, vl) - place)), edge) * take + const tipBeads = + cutOf(1.9 - 1.4 * lum.down - abs(vl - lum.sway), edge) * step(0.45, lum.down) * step(-122, ul) * step(mod(ul, 8), 4.5) + // Two remoras keep station behind the wings: small fish, fatter at the head, with a forked tail. + const along = (ul + 54) / 8 + const aside = abs(abs(vl) - 15 - 0.5 * sin(ul * 0.5 + time * 3)) + const fork = clamp((-0.8 - along) / 0.6, 0, 1) + const remora = cutOf( + max( + min(1.9 * (1 - along * along) * (1 + 0.3 * along) - aside, 1 - abs(along)), + min(min(2 * fork - aside, aside - (fork - 0.55) * 2.2), min(-0.8 - along, along + 1.4)), + ), + edge, + ) + const lit = max(max(rim, eyes), max(max(stipple * 0.75, tipBeads), remora)) + let color = mix(ink.glow, mix(ink.slate, ink.deep, max(spine, gills) * 0.9), body) + color = mix(color, ink.glow, lit) + const cover = max(max(body, under), lit) + return vec4(color * cover, cover) + }, +) + +// The fish. +// A fish's outline in its own units: positive inside. The body swims: a wave runs down it, widest at the tail. +const fishShape = (u: number, v0: number, beat: number) => { + const bend = clamp((7 - u) / 24, 0, 1) + const v = v0 - 3.4 * bend * bend * sin(u * 0.3 + beat) + const side = abs(v) + const t = clamp((u + 11) / 24, 0, 1) + const girth = max(9.3 * t * sqrt(1 - t) * (0.4 + 0.6 * t), 0.65) + const body = min(girth - side, min(13 - u, u + 11)) + const tt = clamp((-10 - u) / 9, 0, 1) + const tail = min(min(0.65 + 3.5 * tt - side, side - (tt - 0.5) * 4.2), min(-10 - u, u + 19)) + const fa = -(u - 5.5) * 0.64 + (side - 2.9) * 0.77 + const fb = (u - 5.5) * 0.77 + (side - 2.9) * 0.64 + const fin = min(0.95 * (1 - fa / 5.2) - abs(fb), min(fa, 5.2 - fa)) + return { inside: max(max(body, tail), fin), v, side, girth } +} +const fishDraw = shapes( + { count: 6, instances: FISH_COUNT, cover: 0.05 }, + (vertex, instance) => { + const corner = CORNERS.at(vertex) + const fish = school.at(vec2(instance, 0)) + const big = FISH.at(instance).w + const across = vec2(-fish.w, fish.z) + const local = vec2(-3.5 + corner.x * 20, corner.y * 12) + const world = vec2(fish.xy + (fish.zw * local.x + across * local.y) * big + SLIP) + const slip = vec2(dot(SLIP, fish.zw), dot(SLIP, across)) / big + return { at: vec2((world / size) * 2 - 1), a: vec4(local, instance, big), b: vec4(slip, 0, 0) } + }, + spot => { + const ink = inks() + const u = spot.a.x + const edge = spot.a.w / spot.px + const beat = time * 6.5 + spot.a.z * 1.9 + 0.8 + // The luminous block: the fish itself, with a spine, a gill cut, two eyes and three scale nicks a side carved out. + const lum = fishShape(u, spot.a.y, beat) + const shape = cutOf(lum.inside, edge) + const spine = cutOf(0.42 - lum.side, edge) * step(-7, u) * step(u, 4.5) + const gill = cutOf(0.42 - abs(u - 8.2 + 0.14 * lum.v * lum.v), edge) * step(lum.side, 2.6) + const eye = cutOf(0.55 - length(vec2(u - 9.7, lum.side - 1.55)), edge) + const slant = u + 0.556 * (lum.side - 0.95) - 0.5 + const scales = + cutOf(0.3 - abs(mod(slant - 0.7, 3) - 1.5), edge) * + step(0.95, lum.side) * step(lum.side, min(2.3, lum.girth * 0.78)) * step(-4.3, slant) * step(slant, 2.7) + // The slate block prints the same fish a little fat underneath, so each one keeps a dark keyline among the plankton. + const key = cutOf(fishShape(u + spot.b.x, spot.a.y + spot.b.y, beat).inside + 1.5 / spot.a.w, edge) + let color = mix(ink.slate, ink.glow, shape) + color = mix(color, ink.deep, max(max(spine, gill), max(eye, scales)) * shape) + const cover = max(key, shape) + return vec4(color * cover, cover) + }, +) + +export default shader({ fps: 30, passes: [seed, school, wake, sea, jellyDraw, fryDraw, rayDraw, fishDraw] }) diff --git a/plugins/theme-examples/hooks/scenes/sea-sparkle/still.ts b/plugins/theme-examples/hooks/scenes/sea-sparkle/still.ts new file mode 100644 index 00000000..6bf3c370 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/sea-sparkle/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Sea Sparkle, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function seaSparkleSvg(): string { + return ( + "\nSea Sparkle\nA linocut of a night sea from above: A ray, fish, and moon jellies trail wakes of glowing plankton.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + ) +} diff --git a/plugins/theme-examples/hooks/scenes/semicolony/scene.ts b/plugins/theme-examples/hooks/scenes/semicolony/scene.ts new file mode 100644 index 00000000..13ad28ab --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/semicolony/scene.ts @@ -0,0 +1,486 @@ +// semicolony: a 1-bit specimen sheet where code punctuation lives as small creatures. +// Three passes: where the loupes have rested, the sheet drawn small (one texel per art pixel, 4 points wide), and the picture, +// which reads both and lays the ink and the sheet's faint printing over the paper colour it finds in the Svg. +import { + abs, atan, buffer, clamp, dot, dt, exp, floor, fract, int, length, max, min, mix, mod, picture, pointers, px, range, res, scene, shader, sign, signed, sin, size, smoothstep, step, still, time, vec2, vec3, vec4, +} from 'claude-code/shader' +import type { Buffer } from 'claude-code/shader' + +// An integer mixer of this file's own: two whole numbers in, 32 scrambled bits out. +const scramble = (a: number, b: number) => { + let h = int(int(a) * 3471823 + int(b) * 5908417 + 733) + h = int(h ^ (h >>> 17)) + h = int(h * 93827461) + h = int(h ^ (h >>> 11)) + h = int(h * 2753197) + h = int(h ^ (h >>> 14)) + return h +} +// Eight bits of a scramble as a number from 0 to 1. +const part = (h: number, shift: number) => signed(int((int(h) >>> shift) & 255)) / 256 +// 1 where a whole number equals another. +const is = (x: number, y: number) => step(abs(x - y), 0.25) +// 1 where low <= x <= high, for whole numbers. +const within = (x: number, low: number, high: number) => step(low - 0.25, x) * step(x, high + 0.25) + +// The clock starts here, at a moment whose first frame is well composed: the Svg shows that frame. +const START = 147 + +// This pass remembers where a loupe has rested. Red: how long each art pixel has lain under a loupe lately. Green: +// whether it is in focus. Blue: how much of a loupe's rim it carries this frame. An art pixel comes into focus after +// about a third of a second under glass and leaves focus about four tenths of a second after the glass has gone. +// Those two waits keep the focus itself from switching any spot more than three times in a second, however the pointer is shaken. +const dwell: Buffer = buffer({ scale: 0.25, filter: 'nearest' }, (p): vec4 => { + const cellX = floor(p.pixel.x) + const cellY = floor(p.pixel.y) + let under = 0 + let rim = 0 + for (const finger of pointers) { + // A loupe opens as its pointer arrives. After a hover leaves it lingers a moment before it closes; after a press + // lifts it closes within a fifth of a second. + const w = clamp(finger.age * 3.3, 0, 1) * (1 - smoothstep(0.35 * (1 - finger.down), 1 - 0.8 * finger.down, finger.gone)) + const overX = p.pixel.x - finger.at.x * res.x / size.x + const overY = p.pixel.y - finger.at.y * res.y / size.y + const away = length(vec2(overX, overY)) + // The disc opens from nothing and closes to nothing; at rest its radius is 88 points. + const reach = 22 * (1 - (1 - w) * (1 - w)) + under = max(under, step(away, reach) * step(0.02, w)) + // The rim: a ring one art pixel wide of beads, two on and two off, that step round it, and four ticks that point + // in at the middle and are longer while the pointer is pressed. + const beads = abs(overX) > abs(overY) ? cellY * sign(overX) : -cellX * sign(overY) + const ring = step(abs(away - reach + 0.5), 0.5) * step(mod(beads + floor(time * 2.5), 4), 1.5) + const ticks = max(is(floor(overX), 0), is(floor(overY), 0)) * step(reach - 4 - 3 * finger.down, away) * step(away, reach) + rim = max(rim, max(ring, ticks) * w) + } + const was = dwell.at(p.cell) + // The count runs fast below three tenths and above seven, and slowly between them: 0.32 s to climb the middle, 0.4 s to fall. + const climbed = was.r < 0.295 ? min(0.302, was.r + dt * 6) : (was.r < 0.695 ? min(0.702, was.r + dt * 1.25) : min(1, was.r + dt * 6)) + const fallen = was.r > 0.71 ? max(0.702, was.r - dt * 6) : (was.r > 0.31 ? max(0.302, was.r - dt) : max(0, was.r - dt * 6)) + const filled = under > 0.5 ? climbed : fallen + // Focus switches on at the top of the middle and off at the bottom, and keeps its last answer in between. + const focus = filled >= 0.7 ? 1 : (filled <= 0.305 ? 0 : step(0.5, was.g)) + return vec4(filled, focus, rim, 1) +}) + +// 1 where the sheet is in focus at a place given in points. +const big = (atX: number, atY: number): number => step(0.5, dwell.sample(vec2(atX / size.x, atY / size.y)).g) +// A creature that grows in focus is drawn at twice its size: this turns a distance in art pixels into one in the creature's own pixels. +const near = (d: number, grown: number): number => floor(d / (1 + grown)) + +// The sheet itself: a small 1-bit frame, one texel for each art pixel of 4 points. It keeps nothing from frame to frame; +// it is drawn small so that every creature is worked out once per art pixel, not once per screen pixel. +// Red holds ink (a half for ink that shows only in focus), green which creature the ink belongs to, blue the sheet's +// faint printing, alpha whether that creature is drawn in focus. +const art = buffer({ scale: 0.25, filter: 'nearest' }, p => { + // Under Reduce Motion the sheet stays at the first frame, the one the Svg shows. + const now = time * (1 - still) + START + const a = px + const qx = floor(p.pixel.x) + const qy = floor(p.pixel.y) + const atX = (qx + 0.5) * a + const atY = (qy + 0.5) * a + + let ink = 0 + let kind = 0 + // Ink that shows only where the sheet is in focus. + let hidden = 0 + // Whether the ink here belongs to a creature that is drawn in focus: such a creature is coloured all over. + let tinted = 0 + const put = (on: number, id: number, grown: number) => { + ink = max(ink, on) + kind = mix(kind, id, on) + tinted = mix(tinted, grown, on) + } + + // Brace jellies rise in the gutters, opening and closing. + { + const lane = floor(atX / 168 + 0.5) + const laneHash = scramble(lane, 7) + const rise = now * (5 + 4 * part(laneHash, 0)) + 470 * part(laneHash, 8) + const cell = floor((atY - rise) / 470) + const h = scramble(lane, cell + 288) + const beat = now / (2.4 + part(h, 0)) + part(h, 8) + const swell = sin(6.2832 * beat) + const cx = lane * 168 + 3 * sin(now * 0.31 + 6 * part(h, 16)) + const cy = (cell + 0.3 + 0.4 * part(h, 16)) * 470 + rise + 3 * sin(6.2832 * beat - 1.2) + const grown = big(cx, cy + 20) + const lx = near(qx - floor(cx / a), grown) + 0.5 + const ly = near(qy - floor(cy / a), grown) + const half = floor(2.2 + 1.2 * swell) + const col = abs(lx) - 0.5 - half + const row = min(ly, 10 - ly) + // The brace, drawn once: six rows of four, mirrored top to bottom and left to right. + const bits = int(3 + 4 * 16 + 4 * 256 + 4 * 4096 + 4 * 65536 + 8 * 1048576) + let body = 0 + if (col >= 0 && col < 4 && row >= 0) { + body = signed(int((bits >>> int(row * 4 + col)) & 1)) + } + const blink = step(fract(now * 0.23 + part(h, 24)), 0.95) + const eyes = is(ly, 5) * (is(abs(lx), 1.5) * step(1.5, half) * blink + is(abs(lx), 0.5) * step(half, 1.5)) + // Three dotted strands trail below and sway. + const depth = -1 - ly + const sway = floor(sin(now * 1.6 + depth * 0.7 + 6 * part(h, 24)) * depth * 0.22 + 0.5) + const sx = lx - 0.5 - sway + const strand = within(depth, 1, 7 - 2 * step(1, abs(sx))) * is(fract(depth * 0.5), 0.5) * (is(sx, 0) + is(abs(sx), 3)) + // Two bubbles leave on each beat. + let fizz = 0 + for (const side of [-1, 1]) { + const u = fract(beat + 0.25 * side) + const far = abs(ly - 12 - floor(u * 18)) + abs(lx - 0.5 - side * 2 - floor(sin(u * 9 + side) + 0.5)) + fizz = max(fizz, is(far, 0) * step(u, 0.8) + is(far, 1) * step(0.8, u) * step(u, 0.88)) + } + const alive = step(part(h, 4), 0.02 + 1.25 * abs(lane * 168 / size.x - 0.5)) * step(40, lane * 168) * step(lane * 168, size.x - 40) + put(body * alive, 1, grown) + put(eyes * alive, 0, grown) + put(strand * alive, 6, grown) + put(fizz * alive, 6, grown) + } + + // Semicolon tadpoles climb on a slant in loose schools, tails wriggling. + { + const u = atX * 0.6 + atY * 0.8 + const v = atX * 0.8 - atY * 0.6 + const lane = floor(v / 250) + const laneHash = scramble(lane, 91) + const run = now * (9 + 5 * part(laneHash, 0)) + 560 * part(laneHash, 8) + const cell = floor((u - run) / 560) + const h = scramble(lane, cell + 323) + const uc = (cell + 0.35 + 0.3 * part(h, 4)) * 560 + run + const vc = (lane + 0.3 + 0.4 * part(h, 0)) * 250 + const sx = 0.6 * uc + 0.8 * vc + const sy = 0.8 * uc - 0.6 * vc + const alive = step(part(h, 8), 0.4) + const count = 2 + floor(part(h, 16) * 2.99) + for (const [i, ox, oy] of [[0, 0, 0], [1, -28, -20], [2, 22, -34], [3, -6, -56]] as const) { + const ph = part(h, 24) * 6 + i * 1.9 + const cx = sx + ox + 4 * sin(now * 0.7 + ph) + const cy = sy + oy + 3 * sin(now * 0.9 + ph * 1.3) + const grown = big(cx + 4, cy + 16) + const lx = near(qx - floor(cx / a), grown) + const ly = near(qy - floor(cy / a), grown) + const wide = within(lx, 0, 1) + const d = 3 - ly + const tx = floor(1 - 0.34 * d + 0.45 * d * sin(now * 3.4 + ph - d * 1.1) + 0.5) + const tail = within(ly, 0, 2) * is(lx, tx) + const body = max(wide * (within(ly, 6, 7) + within(ly, 3, 4)), tail) + const b = fract(now * 0.3 + ph) + const fizz = is(ly, 10 + floor(b * 12)) * is(lx, -2 - floor(b * 3)) * step(b, 0.7) + const there = alive * step(i, count - 0.5) + put(body * there, 2, grown) + put(fizz * there, 6, grown) + } + } + + // Arrow darters cross the sheet in darts and rests, stretching while they dart. + { + const lane = floor(atY / 189) + const laneHash = scramble(lane, 17) + const dir = part(laneHash, 0) < 0.5 ? -1 : 1 + const beat = now / 3.4 + part(laneHash, 8) + const f = fract(beat) + const slow = 1 - min(f / 0.38, 1) + const run = dir * 96 * (floor(beat) + 1 - slow * slow * slow) + 700 * part(laneHash, 16) + const cell = floor((atX - run) / 700) + const h = scramble(cell, lane + 500) + const cx = (cell + 0.3 + 0.4 * part(h, 0)) * 700 + run + const cy = (lane + 0.5) * 189 + const grown = big(cx - dir * 12, cy) + const lx = near((qx - floor(cx / a)) * dir, grown) + const ry = near(qy - floor(cy / a), grown) + const ly = abs(ry) + const fast = step(f, 0.22) + const fat = step(0.5, part(h, 16)) + const head = is(lx + ly, 0) * step(ly, 2.25 + fat) + const len = 5 + fat + 3 * fast + const onShaft = is(ly, fat) + const shaft = onShaft * within(lx, -len, -1 - fat) + const flick = step(0.5, fract(now * 0.9 + part(h, 4))) * (1 - fast) + const fin = is(lx, -len - 1) * is(ly, fat + 1) + flick * is(lx, -len - 2) * is(ly, fat + 2) + const streak = fast * onShaft * (within(lx, -len - 5, -len - 4) + is(lx, -len - 8)) + const b = fract(now * 0.37 + part(h, 4)) + const fizz = is(ry, 3 + floor(b * 11)) * is(lx, -len - floor(b * 5)) * step(b, 0.7) + const alive = step(part(h, 24), 0.55) * step(0.3, part(laneHash, 24)) + put(max(max(head, fin), max(shaft, streak)) * alive, 3, grown) + put(fizz * alive, 6, grown) + } + + // Tilde eels: a slow travelling wave of pixels, on the baselines the arrows leave free. + { + const lane = floor(atY / 189) + const laneHash = scramble(lane, 17) + const dir = part(laneHash, 0) < 0.5 ? 1 : -1 + const run = dir * now * (5 + 3 * part(laneHash, 8)) + 1100 * part(laneHash, 16) + const cell = floor((atX - run) / 1100) + const h = scramble(cell, lane + 700) + const cx = (cell + 0.3 + 0.4 * part(h, 0)) * 1100 + run + const cy = (lane + 0.5) * 189 + const grown = big(cx - dir * 30, cy) + const lx = near((qx - floor(cx / a)) * dir, grown) + const ly = near(qy - floor(cy / a), grown) + const wave = floor(1.45 * sin(lx * 0.62 + now * 2.2 + 6 * part(h, 16)) + 0.5) + const body = is(ly, wave) * within(lx, -15, 0) + const head = is(ly, wave + 1) * within(lx, -1, 0) + const alive = step(part(h, 24), 0.6) * step(part(laneHash, 24), 0.3) + const b = fract(now * 0.33 + part(h, 4)) + const fizz = is(ly, 4 + floor(b * 9)) * is(lx, 1 - floor(b * 4)) * step(b, 0.7) + put(max(body, head) * alive, 4, grown) + put(fizz * alive, 6, grown) + } + + // Plus-sign stars sit on the register crosses and turn to a times sign and back, with a sparkle at each turn. + { + const cxi = floor(atX / 168) + const cyi = floor(atY / 189) + const h = scramble(cxi, cyi + 929) + const cx = (cxi + 0.5) * 168 + const cy = (cyi + 0.5) * 189 + const grown = big(cx, cy) + const lx = abs(near(qx - floor(cx / a), grown)) + const ly = abs(near(qy - floor(cy / a), grown)) + const turn = step(0.5, fract(now * 0.17 + part(h, 16))) + const far = max(lx, ly) + const plus = is(min(lx, ly), 0) + const cross = is(lx, ly) + const spark = step(fract(2 * (now * 0.17 + part(h, 16))), 0.1) + const on = mix(plus, cross, turn) * (step(far, 2.25) + spark * is(far, 4)) + put(min(on, 1) * step(part(h, 0), 0.1), 5, grown) + } + + // Loose bubbles: single pixels rising everywhere, slowly. + { + const lane = floor(atX / 84) + const laneHash = scramble(lane, 29) + const rise = now * (4 + 5 * part(laneHash, 0)) + 90 * part(laneHash, 8) + const cell = floor((atY - rise) / 90) + const h = scramble(lane, cell + 1300) + const cx = (lane + 0.15 + 0.7 * part(h, 0)) * 84 + 3 * sin(now * 0.8 + 6 * part(h, 8)) + const cy = (cell + 0.5) * 90 + rise + put(is(qx, floor(cx / a)) * is(qy, floor(cy / a)) * step(part(h, 16), 0.24), 6, 0) + } + + // Pipe sea fern grows from the ruler at the foot of the sheet, taller toward the sheet's edges, and sways. + { + const lane = floor(atX / 28) + const h = scramble(lane, 2100) + const edge = abs((lane + 0.5) * 28 / size.x - 0.5) * 2 + const tall = floor((4 + 20 * part(h, 0)) * (0.3 + 0.7 * edge * edge)) + const up = qy - 3 + const lean = floor(sin(now * 0.7 + 6 * part(h, 8) + up * 0.16) * up * 0.09 + 0.5) + const lx = qx - floor(((lane + 0.5) * 28 + 6 * (part(h, 16) - 0.5)) / a) - lean + // Stacked pipes: three pixels of stalk, one of gap, and a loose tip. + const bud = is(mod(up, 4), 3) * is(lx, mod(up, 8) < 4 ? 1 : -1) * within(up, 3, tall - 1) + const stalk = is(lx, 0) * within(up, 0, tall) * (1 - is(mod(up, 4), 3)) + is(lx, 0) * is(up, tall + 2) + bud + put(min(stalk, 1) * step(part(h, 24), 0.25 + 0.45 * edge), 4, 0) + } + + // At-sign snails creep along the ruler, feelers waving, three dots of trail behind. + { + const run = now * 2.2 + const cell = floor((atX - run) / 540) + const h = scramble(cell, 2469) + const cx = (cell + 0.2 + 0.6 * part(h, 0)) * 540 + run + const grown = big(cx, 24) + const lx = near(floor(cx / a) - qx, grown) + 4 + const ly = near(qy - 3, grown) + // The shell is six rows of six; then come the foot and the head. + const rows = [30, 33, 45, 41, 57, 62] + let shell = 0 + if (lx >= 0 && lx < 6 && ly >= 0 && ly < 6) { + shell = signed(int((int(rows[5 - ly]!) >>> int(lx)) & 1)) + } + const foot = is(ly, 0) * within(lx, -4, -1) + is(ly, 1) * is(lx, -4) + const wave = step(0.5, fract(now * 0.9 + part(h, 8))) + const feelers = is(lx, -5) * is(ly, 2 + wave) + is(lx, -3) * is(ly, 3 - wave) + const trail = is(ly, 0) * within(lx, 8, 12) * is(mod(lx, 2), 0) + put(min(shell + foot + feelers + trail, 1) * step(part(h, 16), 0.6), 7, grown) + } + + // Question-mark seahorses hang in the lower water, bobbing, a fin flickering at the back. + { + const cxi = floor(atX / 216) + const h = scramble(cxi, 2520) + const edge = abs((cxi + 0.5) * 216 / size.x - 0.5) * 2 + const cx = (cxi + 0.25 + 0.5 * part(h, 0)) * 216 + 3 * sin(now * 0.4 + 6 * part(h, 8)) + const cy = 70 + 150 * part(h, 16) + 7 * sin(now * 0.9 + 6 * part(h, 8)) + const grown = big(cx + 12, cy + 20) + const lx = near(qx - floor(cx / a), grown) + const ly = near(qy - floor(cy / a), grown) + // A question mark drawn as a seahorse: crest, snout, long neck, belly. Eight rows of six, never mirrored, + // so that it stays a question mark. + const rows = [8, 24, 8, 16, 32, 35, 18, 12] + let body = 0 + if (lx >= 0 && lx < 6 && ly >= 2 && ly < 10) { + body = signed(int((int(rows[ly - 2]!) >>> int(lx)) & 1)) + } + // Its dot is a curled tail tip that flicks, and a fin flickers behind the neck. + const flick = step(0.5, fract(now * 0.45 + part(h, 8))) + const tail = is(ly, 0) * within(lx, 2 + flick, 3 + flick) + const fin = is(lx, 6) * is(ly, 5 + step(0.5, fract(now * 0.9 + part(h, 8)))) + const b = fract(now * 0.28 + part(h, 24)) + const fizz = is(lx, -2 - floor(b * 2)) * is(ly, 9 + floor(b * 12)) * step(b, 0.75) + const alive = step(part(h, 12), 0.15 + 0.6 * edge) + put(min(body + tail + fin, 1) * alive, 3, grown) + put(fizz * alive, 6, grown) + } + + // Plankton: small swarms of single pixels, each mote circling its own spot in the swarm. + { + const lane = floor(atY / 270) + const laneHash = scramble(lane, 2700) + const run = now * (2 + 3 * part(laneHash, 0)) * (part(laneHash, 4) < 0.5 ? -1 : 1) + 400 * part(laneHash, 8) + const cell = floor((atX - run) / 400) + const h = scramble(cell, lane + 2800) + const cx = (cell + 0.3 + 0.4 * part(h, 0)) * 400 + run + const cy = (lane + 0.3 + 0.4 * part(h, 8)) * 270 + 10 * sin(now * 0.23 + 6 * part(h, 16)) + // Each swarm has the same loose shape, flipped and scaled. + const spread = (part(h, 4) < 0.5 ? -1 : 1) * (0.7 + 0.5 * part(h, 12)) + let motes = 0 + for (const i of range(9)) { + const swing = now * (0.9 - 0.06 * i) + i * 2.4 + 6 * part(h, 16) + const mx = cx + spread * 30 * sin(i * 2.4) + 6 * sin(swing + 1.571) + const my = cy + spread * 20 * sin(i * 1.7 + 2) + 5 * sin(swing) + motes = max(motes, is(qx, floor(mx / a)) * is(qy, floor(my / a))) + } + put(motes * step(part(h, 24), 0.55), 6, 0) + } + + // Colon fry: shoals of small colons in a loose wavering line. + { + const lane = floor(atY / 236) + const laneHash = scramble(lane, 3100) + const dir = part(laneHash, 0) < 0.5 ? -1 : 1 + const run = dir * now * (7 + 3 * part(laneHash, 8)) + 700 * part(laneHash, 16) + const cell = floor((atX - run) / 700) + const h = scramble(cell, lane + 3200) + const cx = (cell + 0.3 + 0.4 * part(h, 0)) * 700 + run + const cy = (lane + 0.5) * 236 + 60 * (part(h, 8) - 0.5) + let shoal = 0 + for (const i of range(7)) { + const fx = cx - dir * i * 14 + const fy = cy + 7 * sin(now * 1.3 + i * 0.8 + 6 * part(h, 16)) + 5 * mod(i, 2) + const ly = qy - floor(fy / a) + shoal = max(shoal, is(qx, floor(fx / a)) * (is(ly, 0) + is(ly, 2))) + } + put(shoal * step(part(h, 24), 0.5), 2, 0) + } + + // The queen: a semicolon set at a larger size and crowned, climbing slowly on the same slant as the schools. + // Queens are rare: the sheet seldom holds more than one. + { + const u = atX * 0.6 + atY * 0.8 + const v = atX * 0.8 - atY * 0.6 + const lane = floor(v / 520) + const run = now * 6 + 4000 * part(scramble(lane, 4355), 8) + const cell = floor((u - run) / 4000) + const h = scramble(lane, cell + 4200) + const uc = (cell + 0.5) * 4000 + run + const vc = (lane + 0.3 + 0.4 * part(h, 0)) * 520 + const lx = qx - floor((0.6 * uc + 0.8 * vc + 5 * sin(now * 0.5)) / a) + const ly = qy - floor((0.8 * uc - 0.6 * vc) / a) + // Head and body are two rings with a row between them; the head has an eye. + const rings = step(abs(length(vec2(lx - 4, ly - 19)) - 3), 0.55) + step(abs(length(vec2(lx - 4, ly - 11)) - 3), 0.55) + const eye = is(lx, 5) * is(ly, 20) + // The tail is two pixels thick and waves more toward its tip. + const d = 8 - ly + const tx = floor(4 - 0.3 * d + 0.3 * d * sin(now * 2.4 - d * 0.6) + 0.5) + const tail = within(ly, 0, 7) * within(lx - tx, 0, 1) + const crown = is(ly, 23) * within(lx, 2, 6) + is(ly, 24) * within(lx, 2, 6) * is(mod(lx, 2), 0) + const b = fract(now * 0.25 + part(h, 8)) + const fizz = is(lx, -2 - floor(b * 3)) * is(ly, 24 + floor(b * 14)) * step(b, 0.75) + const alive = step(part(h, 16), 0.8) + put(min(rings + eye + tail, 1) * alive, 2, 0) + put(min(crown, 1) * alive, 1, 0) + put(fizz * alive, 6, 0) + } + + // Caret moths: small life seen only where the sheet is in focus, one to each patch of the sheet, so that a loupe + // seldom finds none. Each flaps between a caret and its upside-down and wanders its patch, drawn at twice its size. + { + const cxi = floor(atX / 100) + const cyi = floor(atY / 100) + const h = scramble(cxi, cyi + 5100) + const cx = (cxi + 0.5) * 100 + 30 * sin(now * 0.21 + 6 * part(h, 0)) + const cy = (cyi + 0.5) * 100 + 30 * sin(now * 0.17 + 6 * part(h, 8)) + const lx = near(qx - floor(cx / a), 1) + const ly = near(qy - floor(cy / a), 1) + const flap = step(0.5, fract(now * 0.8 + part(h, 16))) + hidden = within(ly, 0, 1) * is(abs(lx), mix(1 - ly, ly, flap)) + } + + // A press sends a ring of eight bubbles outward from the pointer; they slow, and pop. + { + let burst = 0 + for (const finger of pointers) { + const reach = 5 + 22 * (1 - exp(-2.2 * finger.age)) + const fx = finger.at.x * res.x / size.x + const fy = finger.at.y * res.y / size.y + const live = finger.down * step(finger.age, 1) + // Each bubble keeps to its own eighth of the circle, so only the one on this side needs looking at. + const i = mod(floor(atan(qy + 0.5 - fy, qx + 0.5 - fx) / 0.7854 + 0.5), 8) + const turn = i * 0.7854 + 0.3 * sin(i * 5.1) + const out = reach * (1 + 0.14 * sin(i * 3.3)) + const far = abs(qx - floor(fx + out * sin(turn + 1.571))) + abs(qy - floor(fy + out * sin(turn))) + burst = max(burst, live * (is(far, 0) * step(finger.age, 0.85) + is(far, 1) * step(0.85, finger.age))) + } + put(burst, 6, 0) + } + + // The sheet's printing: dotted gutter rules, dotted baselines, a register cross in each column, a tally on each baseline. + const gx = floor(abs(atX - floor(atX / 168 + 0.5) * 168) / a) - floor(36 / a) + const gy = qy - floor((floor(atY / 189) + 0.5) * 189 / a) + const dotted = is(fract((qx + qy) * 0.5), 0) + const rule = min(is(gx, 0) * dotted + is(gy, 0) * dotted * is(fract(qx * 0.25), 0), 1) + const ux = qx - floor((floor(atX / 168) + 0.5) * 168 / a) + const cross = step(abs(ux), 1.25) * step(abs(gy), 1.25) * max(is(ux, 0), is(gy, 0)) + const count = mod(floor(atY / 189), 5) + 1 + const tally = within(qx, 3, 1 + 2 * count) * is(fract(qx * 0.5), 0.5) * within(gy, 2, 4) + // The foot of the sheet is a ruler: a line, a tick every third pixel, a longer one every fifteenth. + const ruler = is(qy, 2) + is(mod(qx, 3), 0) * within(qy, 0, 1) * step(1, qy + is(mod(qx, 15), 0)) + // Above the ruler lies a little silt, shaded the 1-bit way: an ordered scatter of dots that thins as it rises. + const silt = within(qy, 3, 12) * step(mod(qx * 5 + qy * 3, 8) + 0.5, 7 - 0.75 * qy) * 0.5 + // Nothing swims over the ruler. Full ink is seen always; half ink is the life that only a place in focus shows. + const open = step(2.5, qy) + const veiled = hidden * (1 - ink) * open + return vec4(ink * open + 0.5 * veiled, mix(kind, 5, veiled) / 8, max(max(0.5 * rule, silt), max(max(cross, tally), min(ruler, 1))), tinted) +}) + +const sheet = picture(p => { + // Where this picture pixel is on the sheet, in art pixels. + const spotX = p.uv.x * art.size.x + const spotY = p.uv.y * art.size.y + const lens = dwell.sample(p.uv) + const glass = step(0.5, lens.g) + const rim = lens.b + const seen = art.sample(p.uv) + // Full ink shows everywhere; half ink is the life that only a place in focus shows. + const ink = seen.r > 0.75 ? 1 : (seen.r > 0.25 ? glass : 0) + const kind = floor(seen.g * 8 + 0.5) + const print = seen.b + + // The palette follows the paper of the Svg, read just off the sheet's middle. In a 1512 by 945 window that spot + // is 22 of the Svg's units right of its centre and 24 above; in a wider or taller window it slides toward one axis or the other, + // and the Svg keeps every spot it can slide to bare. + const paper = vec3(scene.sample(vec2(0.5146, 0.5254)).rgb) + const day = step(0.4, dot(paper, vec3(0.3, 0.6, 0.1))) + const inkTone = vec3(mix(vec3(0.66, 0.69, 0.76), vec3(0.3, 0.32, 0.4), day)) + // In focus each creature takes its syntax colour. + let hue = inkTone + if (kind > 0.5) hue = vec3(mix(vec3(0.9, 0.76, 0.42), vec3(0.6, 0.42, 0.04), day)) + if (kind > 1.5) hue = vec3(mix(vec3(0.93, 0.56, 0.7), vec3(0.72, 0.22, 0.42), day)) + if (kind > 2.5) hue = vec3(mix(vec3(0.45, 0.8, 0.88), vec3(0.05, 0.45, 0.58), day)) + if (kind > 3.5) hue = vec3(mix(vec3(0.6, 0.84, 0.52), vec3(0.22, 0.5, 0.16), day)) + if (kind > 4.5) hue = vec3(mix(vec3(0.74, 0.64, 0.96), vec3(0.45, 0.3, 0.75), day)) + if (kind > 5.5) hue = vec3(mix(vec3(0.62, 0.8, 0.95), vec3(0.26, 0.42, 0.62), day)) + if (kind > 6.5) hue = vec3(mix(vec3(0.95, 0.62, 0.4), vec3(0.72, 0.36, 0.1), day)) + // Ink is coloured where the sheet is in focus, and all over a creature that is drawn in focus, even where it pokes out. + const tone = vec3(mix(inkTone, hue, max(glass, step(0.5, seen.a)))) + + // In focus the sheet also shows its pixel grid, one faint dot at each art pixel's corner. + const grid = glass * step(fract(spotX), 0.25) * step(fract(spotY), 0.25) + // On light paper the loupe's own marks are drawn fainter: a mark that follows the pointer must not change a spot's brightness much. + const faint = 0.13 * step(0.25, print) + 0.09 * step(0.75, print) + (0.12 - 0.02 * day) * grid + (0.24 - 0.1 * day) * rim + const ground = vec3(mix(paper, inkTone, faint)) + return vec4(vec3(mix(ground, tone, ink)), 1) +}) + +export default shader({ fps: 60, passes: [dwell, art, sheet] }) diff --git a/plugins/theme-examples/hooks/scenes/semicolony/still.ts b/plugins/theme-examples/hooks/scenes/semicolony/still.ts new file mode 100644 index 00000000..f8a602ca --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/semicolony/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Semicolony, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function semicolonySvg(): string { + return ( + "\n Semicolony\n A one-bit specimen sheet on which code punctuation lives as small creatures: brace jellies, semicolon tadpoles and their crowned queen, arrow darters, tilde eels, plus-sign stars, question-mark seahorses, at-sign snails, and pipe sea fern.\n \n \n \n \n\n" + ) +} diff --git a/plugins/theme-examples/hooks/scenes/star-chart/scene.ts b/plugins/theme-examples/hooks/scenes/star-chart/scene.ts new file mode 100644 index 00000000..d52ae2e2 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/star-chart/scene.ts @@ -0,0 +1,773 @@ +// Star Chart: a copperplate celestial chart. Stars in three engraved sizes flare now and then, three drawing hands rule +// figure lines between them and let them fade, a slow meteor crosses with a dotted tail, and three small planets creep +// along a dotted ecliptic. +// Touch: the stars near a pointer rule stippled copper lines to it, the chart's own lines near it show in copper, and a +// press sets a graduated circle down. Two to four pointers are joined by a copper thread that runs from star to star. +// The picture is a chart of 1512 by 945 points laid over the window the way the still Svg is (centred, filling it), and +// its first frame is the still Svg, which is drawn from the same numbers. +// The ground is the picture pass. Everything on it is drawn as shapes, so a pixel pays only for the ink that lands on it. +// Each shapes pass declares the most of the window its shapes can cover: counted as the engine counts them, with +// their spare pixels, on a screen of one pixel to two points, plus a third. +import { + PI, abs, atan, buffer, clamp, cos, dot, dt, exp, floor, fract, int, length, max, min, mix, mod, picture, pointer, + pointerCount, pointers, px, scene, shader, shapes, sin, size, smoothstep, step, table, time, unsigned, vec2, vec3, vec4, +} from 'claude-code/shader' +import type { Buffer } from 'claude-code/shader' + +const W = 1512 +const H = 945 +// One star lives in every cell of this many points. +const CELL = 120 +// A pointer rules lines to the stars within REACH, and lights the chart's own lines within GLOW. +const REACH = 175 +const GLOW = 210 +// Lines stop this far short of a star, as engraved lines do, and this far short of a pointer. +const CLEAR = 12 +const HAND = 7 +// A drawing hand rules the lines of the stars within this far of the spot it picked. +const FIGURE = 240 +const METEOR_EVERY = 21 +const METEOR_LEAD = 5.5 +const METEOR_SPEED = 50 + +// This scene's own integer mixer: two multiplies fold the pair, then two rounds of a doubled xor-shift and a multiply, +// and one last xor-shift. +const weave = (x: number, y: number) => { + let h = int(x * 0x2d8a61b7 + y * 0x6e13c94d + 1309) + h = int(h ^ (h >>> 9) ^ (h >>> 22)) + h = int(h * 0x3b52a7e9) + h = int(h ^ (h >>> 12) ^ (h << 7)) + h = int(h * 0x51f6d24b) + h = int(h ^ (h >>> 14)) + return h +} +const low = (h: number) => unsigned(int(h & 0xffff)) / 65536 +const high = (h: number) => unsigned(int(h >>> 16)) / 65536 +const sat = (x: number) => clamp(x, 0, 1) + +// The chart fills the window as the Svg does: one scale for both axes, centred, cropped at the longer side. +const zoom = () => max(size.x / W, size.y / H) +const toChart = (at: number) => vec2((at - size * 0.5) / zoom() + vec2(W / 2, H / 2)) +// A place in the part of the chart that shows: (0, 0) its bottom left, (1, 1) its top right. +const inView = (share: number) => vec2(vec2(W / 2, H / 2) + ((share - 0.5) * size) / zoom()) +// A chart place as a shape's vertex wants it, -1 to 1 across the window. +const toClip = (at: number) => vec2(((at - vec2(W / 2, H / 2)) * 2 * zoom()) / size) + +// Where the star of cell (i, j) is, on the chart. +const starPos = (i: number, j: number) => { + const h = weave(int(i), int(j)) + return vec2((i + 0.05 + 0.9 * low(h)) * CELL, (j + 0.05 + 0.9 * high(h)) * CELL) +} + +// How much a pointer counts: it eases in when it comes, out over the second after it lifts, and is dimmer over the app's own text. +const hold = (f: { gone: number; age: number; over: number }) => + (1 - smoothstep(0, 1, f.gone)) * smoothstep(0, 0.2, f.age) * (1 - 0.35 * f.over) + +// How strongly a pointer at "at" rules a line to the star at s. +const pull = (s: number, at: number) => { + const r = length(vec2(at - s)) / REACH + return sat(1 - r * r * r * r) +} + +// The thread between two consecutive pointers runs through one star in each column between them (each row, for a steep pair). +// This lays the pair out along x, left to right, whichever way it really runs. +const pairOf = (k: number) => { + const a = pointer(k) + const b = pointer(k + 1) + const pa = toChart(a.at) + const pb = toChart(b.at) + const d = vec2(pb - pa) + const wide = abs(d.x) >= abs(d.y) + const lean = abs(abs(d.x) - abs(d.y)) / max(abs(d.x) + abs(d.y), 1) + const strength = + step(k + 1.5, pointerCount) * hold(a) * hold(b) * smoothstep(70, 150, length(d)) * smoothstep(0, 0.12, lean) + const A = wide ? pa : vec2(pa.y, pa.x) + const B = wide ? pb : vec2(pb.y, pb.x) + const flip = A.x > B.x + const L = flip ? B : A + const R = flip ? A : B + return { wide, strength, L, R, first: floor(L.x / CELL), last: floor(R.x / CELL), slope: (R.y - L.y) / max(R.x - L.x, 1) } +} +type Pair = ReturnType +// Which row the pair's thread takes in a column, as a row number with its fraction. +const rowAt = (pair: Pair, col: number) => + (pair.L.y + pair.slope * (clamp((col + 0.5) * CELL, pair.L.x, pair.R.x) - pair.L.x)) / CELL +// A thread lets go of a star as its line nears the edge of the star's row, so that it never jumps from one star to the next. +const grip = (row: number) => smoothstep(0, 0.15, min(row - floor(row), 1 - row + floor(row))) + +// How strongly the threads hold the star of cell (i, j). +const threaded = (i: number, j: number) => { + let best = 0 + for (const k of [0, 1, 2]) { + const pair = pairOf(k) + const ci = pair.wide ? i : j + const cj = pair.wide ? j : i + const row = rowAt(pair, ci) + best = max(best, ci >= pair.first && ci <= pair.last && cj === floor(row) ? pair.strength * grip(row) : 0) + } + return best +} + +const GRID = { size: [16, 16], float: 16, filter: 'nearest' } as const + +// One texel: 1 on parchment, 0 on the ink-blue plate, read from the Svg's ground at the middle of the window, which the +// still keeps bare. Two of three nearby readings decide, so one stray pixel cannot turn the inks over. +const plate = buffer({ size: [1, 1], filter: 'nearest' }, p => { + let votes = 0 + for (const nudge of [-0.004, 0, 0.004]) { + votes += step(0.4, dot(scene.sample(vec2(0.5 + nudge, 0.5 - nudge)).rgb, vec3(0.3, 0.6, 0.1))) + } + return vec4(step(1.5, votes), 0, 0, 1) +}) + +// What the pointers have inked at each star, eased in time so that a hand rushing past leaves only a trace: +// how lit the star's figure line is, how strong its lines to pointers may be, and how strong its links in a thread. +const ease = (was: number, goal: number, up: number, down: number) => + was + (goal - was) * (1 - exp(-dt * (goal > was ? up : down))) +const glow: Buffer = buffer(GRID, (p): vec4 => { + const i = p.cell.x - 1 + const j = p.cell.y - 1 + const s = starPos(i, j) + let lit = 0 + let fan = 0 + for (const f of pointers) { + const at = toChart(f.at) + const d = length(vec2(at - s)) / GLOW + lit = max(lit, sat(1 - d * d * d) * hold(f)) + fan = max(fan, pull(s, at) * hold(f)) + } + const thread = threaded(i, j) + const was = glow.at(p.cell) + return vec4(ease(was.r, max(lit, thread), 5, 1.3), ease(was.g, fan, 6, 4), ease(was.b, thread, 6, 4), 1) +}) +const glowAt = (i: number, j: number) => glow.at(vec2(i + 1, j + 1)) + +// Two inks over a flat ground: the chart's ink and a copper ink, each with a dark plate and a light one. +const plated = (dark: number, light: number) => vec3(mix(dark, light, plate.at(vec2(0, 0)).r)) +const groundTone = () => plated(vec3(0.063, 0.106, 0.188), vec3(0.925, 0.886, 0.792)) +const arcInk = () => plated(vec3(0.3, 0.4, 0.56), vec3(0.55, 0.45, 0.3)) +const lineInk = () => plated(vec3(0.62, 0.71, 0.86), vec3(0.24, 0.29, 0.42)) +const starInk = () => plated(vec3(0.96, 0.9, 0.74), vec3(0.13, 0.16, 0.26)) +const copperInk = () => plated(vec3(0.93, 0.6, 0.36), vec3(0.74, 0.33, 0.18)) + +// The ground: flat, with the chart's coordinate arcs and a band of hatching. +const ground = picture(p => { + const z = zoom() + const pt = vec2((p.point - size * 0.5) / z + vec2(W / 2, H / 2)) + const grain = p.px / z + const ipx = 1 / grain + // Circles and meridians about a pole above the chart, with a ladder of ticks on the circles. + const rel = vec2(pt - vec2(W * 0.8, H * 1.32)) + const rad = length(rel) + const off = mod(rad + 80, 160) - 80 + const ang = atan(rel.y, rel.x) + const sect = PI / 12 + const merid = abs(fract(ang / sect + 0.5) - 0.5) * sect * rad + const tick = abs(fract((ang * 6) / sect + 0.5) - 0.5) * (sect / 6) * rad + const arcs = max( + max(sat((0.5 - min(abs(off), abs(off - 5))) * ipx + 0.5), sat((0.5 - merid) * ipx + 0.5)), + sat((0.45 - tick) * ipx + 0.5) * step(0, off) * step(off, 5), + ) + // The way an engraver shades the Milky Way. Too fine for a coarse picture, so it turns to a flat tone there. + const bandAt = dot(vec2(pt - vec2(W * 0.87, H * 0.5)), vec2(0.978, -0.208)) + 40 * sin(dot(pt, vec2(0.208, 0.978)) / 170) + const band = 1 - smoothstep(50, 115, abs(bandAt)) + const hatch = sat((0.4 - abs(fract((pt.x - pt.y) / 9.9) - 0.5) * 7) * ipx + 0.5) + const shade = band * mix(0.11, hatch, sat(1.7 - grain)) + return vec4(mix(groundTone(), arcInk(), max(arcs * 0.24, shade * 0.16)), 1) +}) + +// The two triangles of a quad: which end and which side each of the six vertices is. +const ENDS = [0, 1, 0, 1, 1, 0] +const SIDES = [-1, -1, 1, -1, 1, 1] +// A quad around a stroke from "from" along the unit heading "dir": inked from lo to hi and reaching half to either side, +// with a pixel to spare all round for a soft edge. Its a.xy is the place in the stroke's own frame. +const strokeQuad = (v: number, from: number, dir: vec2, lo: number, hi: number, half: number) => { + const grain = px / zoom() + const along = mix(lo - grain, hi + grain, ENDS[v]!) + const side = SIDES[v]! * (half + grain) + return { at: toClip(vec2(from + dir * along + vec2(-dir.y, dir.x) * side)), along, side } +} +// How much of a pixel a stroke of half-width "half" inks, between lo and hi along it. Its ends taper, as a burin's cut does. +const swell = (t: number, n: number, lo: number, hi: number, half: number, ipx: number) => { + const inside = min(t - lo, hi - t) + return sat((half * (0.6 + 0.4 * sat(inside * 0.06)) - abs(n)) * ipx + 0.5) * sat(inside * ipx + 0.5) +} + +// Stipple: fine specks on a grid of their own, breathing slowly. +const specks = shapes( + { count: 693, cover: 0.07, draw: 'points' }, + v => { + const si = mod(v, 33) + const sj = floor(v / 33) + const hd = weave(int(int(si) + 911), int(int(sj) - 577)) + const r = 0.7 + 0.7 * fract(low(hd) * 13.7) + const shown = step(fract(high(hd) * 7.3), 0.7) * (0.55 + 0.25 * sin(time * 0.6 + low(hd) * 40)) + const grain = px / zoom() + const span = 2 * r + 2 * grain + return { + at: toClip(vec2((si + 0.12 + 0.76 * low(hd)) * 46, (sj + 0.12 + 0.76 * high(hd)) * 46)), + size: span / grain, + a: vec4(r, shown, span, 0), + hide: shown < 0.01, + } + }, + s => { + const cover = sat(((s.a.x - length(vec2(s.coord - 0.5)) * s.a.z) * zoom()) / s.px + 0.5) * s.a.y + return vec4(starInk() * cover, cover) + }, +) + +// Each star's one figure line to a neighbour. Three drawing hands rule them; pointers light them in copper. +const figureLines = shapes( + { count: 6, instances: 150, cover: 0.35 }, + (v, n) => { + const i = mod(n, 15) - 1 + const j = floor(n / 15) - 1 + const s = starPos(i, j) + const h2 = weave(weave(int(i), int(j)), 7) + // Even columns may rule east, north-east, north or north-west; odd ones east or north. + const pick = low(h2) + const q = mod(i, 2) < 0.5 ? floor(pick * 4) : floor(pick * 2) * 2 + const dx = q < 1.5 ? 1 : q < 2.5 ? 0 : -1 + const dy = q < 0.5 ? 0 : 1 + const d = vec2(starPos(i + dx, j + dy) - s) + const len = length(d) + const rank = high(h2) + // The two hands with the strongest claim on this line: how strong each is, and how much of the line it has ruled. + let a1 = 0 + let g1 = 0 + let a2 = 0 + let g2 = 0 + // Each hand picks a spot in a side margin and rules the lines near it outward, then the figure fades. + for (const k of [0, 1, 2]) { + const period = [34, 43, 55][k]! + const tt = time + [12, 10.5, 0][k]! + const turn = floor(tt / period) + const tau = tt - turn * period + const hs = weave(int(turn), int([8, 12, 130][k]!)) + const mx = 0.04 + 0.2 * low(hs) + const share = vec2(mod(turn + [0, 1, 1][k]!, 2) > 0.5 ? 1 - mx : mx, 0.18 + 0.64 * high(hs)) + // The first figures stand where the still shows them; later ones keep to the part of the chart that shows. + const seed = turn < 0.5 ? vec2(share * vec2(W, H)) : inView(share) + const dist = length(vec2(s - seed)) + const member = (1 - smoothstep(0.7 * FIGURE, FIGURE, dist)) * step(rank, 0.85) + const gk = sat((tau - 1 - (6 * dist) / FIGURE) / 1.8) + const ak = member * (1 - smoothstep(20, 27, tau)) * step(0.001, gk) + if (ak > a1) { + a2 = a1 + g2 = g1 + a1 = ak + g1 = gk + } else if (ak > a2) { + a2 = ak + g2 = gk + } + } + const lit = min(glowAt(i, j).r, glowAt(i + dx, j + dy).r) + // A line being ruled gets a wider quad, for the burr beside it. + const working = max( + step(0.004, a1) * step(0.002, g1) * step(g1, 0.998), + step(0.004, a2) * step(0.002, g2) * step(g2, 0.998), + ) + const quad = strokeQuad(v, s, vec2(d / max(len, 0.01)), CLEAR, len - CLEAR, 1 + 3.5 * working) + return { + at: quad.at, + a: vec4(quad.along, quad.side, len - CLEAR, lit), + b: vec4(a1, g1, a2, g2), + hide: a1 + lit < 0.004, + } + }, + s => { + const ipx = zoom() / s.px + const t = s.a.x + const n = s.a.y + const full = s.a.z + // What one hand has made of the line: its ink as far as it has ruled, the burin at the tip while it rules, + // and the burr the burin throws up to either side, which stays where it fell and fades as the tip moves on. + const ruledBy = (strength: number, share: number) => { + const to = CLEAR + (full - CLEAR) * share + const working = strength * step(0.002, share) * step(share, 0.998) + const nib = working * sat((2 - abs(t - to) - abs(n)) * ipx + 0.5) + const behind = to - t + const beat = fract(t / 10) + const burr = + working * step(1, behind) * sat(1 - behind / 40) * 0.8 * + sat((0.9 - length(vec2((abs(beat - 0.5) - 0.25) * 10, n - (beat < 0.5 ? 2.4 : -2.4)))) * ipx + 0.5) + return { ink: strength * 0.85 * swell(t, n, CLEAR, to, 0.6, ipx), tool: max(nib, burr) } + } + // Where two hands' figures share a line, the eye gets the stronger ink at each place, so no line ever starts over. + const one = ruledBy(s.b.x, s.b.y) + const two = ruledBy(s.b.z, s.b.a) + const chart = max(one.ink, two.ink) + const copper = max(s.a.a * 0.9 * swell(t, n, CLEAR, full, 0.55, ipx), max(one.tool, two.tool)) + return vec4(mix(lineInk() * chart, copperInk(), copper), chart + copper - chart * copper) + }, +) + +// The thread's links: for each pair of pointers, one link back from each column's star toward the pointer the thread +// starts from, and one on from the last star to the pointer at the far end. +const threadLinks = shapes( + { count: 6, instances: 48, cover: 0.18 }, + (v, n) => { + const pair = pairOf(floor(n / 16)) + const c = mod(n, 16) + const isLast = c > 14.5 + const ci = isLast ? pair.last : c - 1 + const row = rowAt(pair, ci) + const before = rowAt(pair, ci - 1) + const isFirst = ci === pair.first + const cell = pair.wide ? vec2(ci, floor(row)) : vec2(floor(row), ci) + const s = starPos(cell.x, cell.y) + const prev = pair.wide ? starPos(ci - 1, floor(before)) : starPos(floor(before), ci - 1) + const toHand = isLast || isFirst + const end = isLast ? pair.R : pair.L + const target = toHand ? (pair.wide ? end : vec2(end.y, end.x)) : prev + const w = + (ci >= pair.first && ci <= pair.last ? 1 : 0) * pair.strength * grip(row) * (toHand ? 1 : grip(before)) + const alpha = min(w, glowAt(cell.x, cell.y).b) + const d = vec2(target - s) + const len = length(d) + const reach = len - (toHand ? HAND : CLEAR) + const quad = strokeQuad(v, s, vec2(d / max(len, 0.01)), CLEAR, reach, 1) + return { at: quad.at, a: vec4(quad.along, quad.side, reach, alpha), hide: alpha < 0.004 } + }, + s => { + const cover = s.a.a * 0.9 * swell(s.a.x, s.a.y, CLEAR, s.a.z, 0.75, zoom() / s.px) + return vec4(copperInk() * cover, cover) + }, +) + +// Each pointer's lines to the stars around it: the sixteen cells about the pointer hold every star within REACH. +const fanLines = shapes( + { count: 6, instances: 64, cover: 0.1 }, + (v, n) => { + const f = pointer(floor(n / 16)) + const slot = mod(n, 16) + const at = toChart(f.at) + const ci = floor(at.x / CELL - 0.5) - 1 + mod(slot, 4) + const cj = floor(at.y / CELL - 0.5) - 1 + floor(slot / 4) + const s = starPos(ci, cj) + const d = vec2(at - s) + const len = length(d) + const alpha = min(pull(s, at) * hold(f) * step(floor(n / 16) + 0.5, pointerCount), glowAt(ci, cj).g) + const quad = strokeQuad(v, s, vec2(d / max(len, 0.01)), CLEAR, len - HAND, 1.3) + return { at: quad.at, a: vec4(quad.along, quad.side, len - HAND, alpha), hide: alpha < 0.004 } + }, + s => { + // A hairline with a row of dots on it that runs toward the pointer, slowly enough that no spot on the line + // sees a dot pass more than about once a second. + const ipx = zoom() / s.px + const t = s.a.x + const inside = sat(min(t - CLEAR, s.a.z - t) * ipx + 0.5) + const dots = sat((1.15 - length(vec2((fract(t / 7 - time * 0.9) - 0.5) * 7, s.a.y))) * ipx + 0.5) + const hair = 0.4 * sat((0.3 - abs(s.a.y)) * ipx + 0.5) + const cover = s.a.a * 0.9 * inside * max(dots, hair) + return vec4(copperInk() * cover, cover) + }, +) + +// A star with four long points and four short ones, folded into one eighth: two edges, each a unit normal and an offset. +const STAR_BIG = [0.147, 0.989, 1.543, 0.912, -0.411, 2.056] +const STAR_MID = [0.138, 0.99, 0.912, 0.971, -0.238, 1.451] +const pointedStar = (a: number, b: number, S: readonly number[]) => + min(a * S[0]! + b * S[1]! - S[2]!, a * S[3]! + b * S[4]! - S[5]!) + +// The stars, in three engraved sizes: eight-point with a glory of fine strokes, four-point, and a small ring. +// Now and then one flares, and the ones a pointer has linked flare too. +const starGlyphs = shapes( + { count: 6, instances: 150, cover: 0.35 }, + (v, n) => { + const i = mod(n, 15) - 1 + const j = floor(n / 15) - 1 + const h3 = weave(weave(int(i), int(j)), 19) + const rank = low(h3) + const every = 7 + 12 * high(h3) + const flare = sin(PI * sat((fract(time / every + rank * 7.3) * every) / 2.6)) + const lit = glowAt(i, j) + const reach = max(flare * flare, max(lit.g, lit.b)) + const tip = rank < 0.16 ? 10.5 : 6.6 + const half = (rank < 0.16 || reach > 0.01 ? tip + 6 + 7 * reach : tip + 1) + px / zoom() + const corner = vec2(ENDS[v]! * 2 - 1, SIDES[v]!) * half + return { at: toClip(vec2(starPos(i, j) + corner)), a: vec4(corner, reach, rank) } + }, + s => { + const ipx = zoom() / s.px + const reach = s.a.z + const isBig = s.a.a < 0.16 + const near = length(s.a.xy) + const q = abs(s.a.xy) + const qa = max(q.x, q.y) + const qb = min(q.x, q.y) + const tip = isBig ? 10.5 : 6.6 + const pointed = + sat(-(isBig ? pointedStar(qa, qb, STAR_BIG) : pointedStar(qa, qb, STAR_MID)) * ipx + 0.5) * + (isBig ? sat((near - 1.5) * ipx + 0.5) : 1) + const ring = sat((0.75 - abs(near - 3)) * ipx + 0.5) + const taper = sat(1 - (qa - tip) / (9 * reach + 0.01)) + const ray = sat((0.65 * taper - qb) * ipx + 0.5) * step(tip - 0.5, qa) * sat(taper * 6) + // The glory: sixteen short strokes around the star, which lengthen when it flares. + const across = abs(fract(atan(qb, qa) / (PI / 8)) - 0.5) * (PI / 8) * near + const glory = + sat((0.45 - across) * ipx + 0.5) * step(tip + 2.5, near) * sat((tip + 6 + 7 * reach - near) * 0.5) * (isBig ? 1 : reach) + const cover = s.a.a > 0.58 ? max(ring, glory * reach) : max(pointed, max(ray, glory * 0.85)) + return vec4(starInk() * cover, cover) + }, +) + +// The meteor, in its own frame: u runs along its path from where it began, v across it. +const meteor = shapes( + { count: 6, cover: 0.03 }, + v => { + const turn = floor((time + METEOR_LEAD) / METEOR_EVERY) + const tau = time + METEOR_LEAD - turn * METEOR_EVERY + const h = weave(int(turn), 4934) + const h2 = weave(h, 99) + const mx = 0.07 + 0.13 * low(h) + const share = vec2(mod(turn, 2) > 0.5 ? 1 - mx : mx, 0.85 + 0.2 * high(h)) + // The first meteor is where the still shows it; later ones keep to the part of the chart that shows. + const start = turn < 0.5 ? vec2(share * vec2(W, H)) : inView(share) + const ang = -PI / 2 + (low(h2) - 0.5) * 0.7 + const life = smoothstep(0, 1, tau) * (1 - smoothstep(10.5, 12, tau)) + const uh = METEOR_SPEED * tau + const quad = strokeQuad(v, start, vec2(cos(ang), sin(ang)), uh - 360, uh + 13, 26) + return { at: quad.at, a: vec4(quad.along, quad.side, uh, life), b: vec4(unsigned(int(h2 >>> 22)), 0, 0, 0), hide: life < 0.004 } + }, + s => { + const ipx = zoom() / s.px + const u = s.a.x + const v = s.a.y + const uh = s.a.z + const back = uh - u + const av = abs(v) + // Its head is an eight-point star with a pricked centre. + const head = + sat(-pointedStar(max(abs(back), av), min(abs(back), av), STAR_BIG) * ipx + 0.5) * + sat((length(vec2(back, v)) - 1.5) * ipx + 0.5) + // Five engraved strokes in its wake, the middle one longest. + const lane = clamp(floor(v / 3.2 + 0.5), -2, 2) + const wakeLen = 84 - 28 * abs(lane) + const wake = sat((0.45 - abs(v - lane * 3.2)) * ipx + 0.5) * step(12, back) * sat((wakeLen - back) / (0.5 * wakeLen)) + // The dotted tail: dots left on the path, each shrinking with its age. + const uk = (floor(u / 13) + 0.5) * 13 + const young = step(0, uh - 14 - uk) * sat(1 - (uh - 14 - uk) / METEOR_SPEED / 6) * step(0, u) + const dots = sat((0.7 + 2.3 * young - length(vec2(u - uk, v))) * ipx + 0.5) * step(0.001, young) + // Stippled dust in copper: three sets of finer specks that drift off the path as they age. + let dust = 0 + for (const layer of [0, 1, 2]) { + const gap = 5 + 2 * layer + const k2 = floor(u / gap) + const hk = weave(int(k2), int(floor(s.b.x + 0.5) + layer * 57)) + const uk2 = (k2 + 0.2 + 0.6 * low(hk)) * gap + const age = (uh - uk2) / METEOR_SPEED + const vk = (high(hk) - 0.5) * 2 * (3 + 18 * sat(age / 7)) + dust = max( + dust, + sat((0.9 + 0.7 * fract(low(hk) * 9.1) - length(vec2(u - uk2, v - vk))) * ipx + 0.5) * + step(0, age) * sat(1 - age / 7) * step(0, u), + ) + } + const copper = dust * 0.85 * s.a.a + const dark = max(head, max(wake * 0.7, dots * 0.9)) * s.a.a + return vec4(mix(copperInk() * copper, starInk(), dark), copper + dark - copper * dark) + }, +) + +// The ecliptic: a dotted copper circle across the chart, every sixth dot a small ring. +const ECLIPTIC = { x: 529, y: -1040, r: 1600, from: 1.9077, span: 0.9984 } +const eclipticAt = (turn: number) => vec2(ECLIPTIC.x + ECLIPTIC.r * cos(turn), ECLIPTIC.y + ECLIPTIC.r * sin(turn)) +const ecliptic = shapes( + { count: 161, cover: 0.03, draw: 'points' }, + v => { + const r = mod(v, 6) < 0.5 ? 2.1 : 0.75 + const grain = px / zoom() + const span = 2 * r + 2 * grain + return { at: toClip(eclipticAt(ECLIPTIC.from - v * 0.00624)), size: span / grain, a: vec4(r, span, 0, 0) } + }, + s => { + const d = length(vec2(s.coord - 0.5)) * s.a.y + const ipx = zoom() / s.px + const cover = 0.75 * (s.a.x > 1 ? sat((0.4 - abs(d - 1.6)) * ipx + 0.5) : sat((s.a.x - d) * ipx + 0.5)) + return vec4(copperInk() * cover, cover) + }, +) + +// Three wanderers creep along the ecliptic, each a small engraved planet with a few dots in its wake. +const wanderers = shapes( + { count: 6, instances: 3, cover: 0.02 }, + (v, n) => { + const along = fract(time / [140, 230, 330][n]! + [0.1, 0.815, 0.67][n]!) + const turn = ECLIPTIC.from - along * ECLIPTIC.span + const quad = strokeQuad(v, eclipticAt(turn), vec2(sin(turn), -cos(turn)), -50, 18, 16) + return { + at: quad.at, + a: vec4(quad.along, quad.side, n, smoothstep(0, 0.03, along) * (1 - smoothstep(0.97, 1, along))), + } + }, + s => { + // Drawn at 1.8 times the size the numbers below are written in. + const ipx = (1.8 * zoom()) / s.px + const x = s.a.x / 1.8 + const y = s.a.y / 1.8 + const r = length(vec2(x, y)) + const kind = floor(s.a.z + 0.5) + // A ringed one: a disc, and an ellipse that passes behind it. + const ringed = max( + sat((3 - r) * ipx + 0.5), + sat((0.45 - abs(length(vec2(x, y * 2.8)) - 7.2)) * ipx + 0.5) * max(step(3.9, r), step(y, 0)), + ) + // A crescent: a disc with a bite out of it. + const crescent = sat((4.2 - r) * ipx + 0.5) * sat((length(vec2(x - 1.9, y - 0.9)) - 3.7) * ipx + 0.5) + // One with a moon: a small disc, a dotted orbit, and a moon going round it. + const turn = atan(y, x) + const moon = max( + max(sat((2.6 - r) * ipx + 0.5), sat((1 - length(vec2(x - 6.8 * cos(time * 0.7), y - 6.8 * sin(time * 0.7)))) * ipx + 0.5)), + 0.7 * sat((0.45 - length(vec2(abs(fract(turn / (PI / 5) + 0.5) - 0.5) * (PI / 5) * r, r - 6.8))) * ipx + 0.5), + ) + const body = kind < 0.5 ? ringed : kind < 1.5 ? crescent : moon + // Dots in its wake, smaller the further back. + const back = -x - 8.5 + const wake = + step(0, back) * step(back, 20) * (1 - back / 26) * + sat((1 - back / 40 - length(vec2((fract(back / 5) - 0.5) * 5, y))) * ipx + 0.5) + const cover = max(body, wake) * s.a.a * 0.9 + return vec4(copperInk() * cover, cover) + }, +) + +// Letters beside the brighter stars, as a star chart names them. Each is three rows on a grid of 4 by 6. A row is a +// stroke from (x, y) to (x, y); or a ring, written as its centre, then its radius as a negative number, then 0; +// or four nines, which is no stroke at all. +const LETTERS = table([ + [0, 0, 2, 3], + [0.8, 6, 4, 0], + [9, 9, 9, 9], + [0, 4.5, 1.8, 0], + [1.8, 0, 4, 4.5], + [9, 9, 9, 9], + [0, 4.6, 4, 4.6], + [2, 4.6, 2, 0.7], + [2, 0.7, 3.1, 0], + [0, 4.6, 4, 4.6], + [1, 4.6, 0.8, 0], + [3, 4.6, 3.3, 0], + [0, 5, 4, 0], + [4, 5, 0, 0], + [9, 9, 9, 9], + [2, 4.6, 2, 0.8], + [2, 0.8, 3.1, 0], + [9, 9, 9, 9], + [2, 2.3, -2, 0], + [9, 9, 9, 9], + [9, 9, 9, 9], + [2, 2, -2, 0], + [1, 3.8, 3.4, 6], + [9, 9, 9, 9], + [0, 4.8, 2, 2], + [4, 4.8, 2, 2], + [2, 2, 2, 0], + [0.6, 4.6, 0.6, 0], + [3.8, 4.6, 0.6, 2.2], + [1.7, 2.9, 4, 0], + [1.9, 2, -1.9, 0], + [1.9, 3.9, 4, 3.9], + [9, 9, 9, 9], +]) +const letters = shapes( + { count: 6, instances: 150, cover: 0.05 }, + (v, n) => { + const i = mod(n, 15) - 1 + const j = floor(n / 15) - 1 + const h3 = weave(weave(int(i), int(j)), 19) + const h4 = weave(h3, 5) + const rank = low(h3) + const tip = rank < 0.16 ? 10.5 : 6.6 + const corner = vec2(ENDS[v]! * 2 - 1, SIDES[v]!) * (vec2(3.8, 5.3) + px / zoom()) + return { + // Far enough out that a flaring star's strokes stop short of it. + at: toClip(vec2(starPos(i, j) + vec2(tip + 11.5, -tip * 0.5 - 11.5) + corner)), + a: vec4(corner, floor(high(h4) * 11), 0), + hide: rank > 0.58 || (rank > 0.16 && low(h4) > 0.5), + } + }, + s => { + const g = vec2(s.a.xy / 1.5 + vec2(2, 3)) + const id = floor(s.a.z + 0.5) + // How far this pixel is from the letter's nearest stroke, in grid units. + let off = 99 + for (const k of [0, 1, 2]) { + const row = LETTERS.at(id * 3 + k) + const rel = vec2(g - row.xy) + // The pixel in the stroke's own frame, as the line quads have it: along the stroke and to its side. + const run = vec2(row.zw - row.xy) + const reach = length(run) + const heading = vec2(run / max(reach, 0.001)) + const along = rel.x * heading.x + rel.y * heading.y + const side = rel.x * heading.y - rel.y * heading.x + const stroke = length(vec2(along - clamp(along, 0, reach), side)) + const ring = abs(length(rel) + row.z) + off = row.x > 8.5 ? off : min(off, row.z < 0 ? ring : stroke) + } + const cover = 0.8 * sat(((0.5 - off * 1.5) * zoom()) / s.px + 0.5) + return vec4(lineInk() * cover, cover) + }, +) + +// Hours in Roman numerals where the meridians cross two of the circles. A row is the numeral's strokes +// (base 4, lowest first: 1 an I, 2 a V, 3 an X) and its width. +const NUMERALS = table([ + [1, 2.4], + [5, 5.6], + [21, 8.8], + [9, 7.8], + [2, 4.6], + [6, 7.8], + [22, 11], + [86, 14.2], + [13, 7.8], + [3, 4.6], + [7, 7.8], + [23, 11], + [87, 14.2], + [39, 13.2], + [11, 10], + [27, 13.2], + [91, 16.4], + [347, 19.6], + [55, 13.2], + [15, 10], + [31, 13.2], + [95, 16.4], + [351, 19.6], +]) +const numerals = shapes( + { count: 6, instances: 46, cover: 0.03 }, + (v, n) => { + const hour = mod(n, 23) + 1 + const turn = (-hour * PI) / 12 + const radial = vec2(cos(turn), sin(turn)) + const tangent = vec2(-radial.y, radial.x) + const row = NUMERALS.at(hour - 1) + const grain = px / zoom() + // Drawn at 1.35 times the size the strokes are written in. + const lx = (ENDS[v]! * 2 - 1) * ((row.y * 0.5 + 1.2) * 1.35 + grain) + const ly = SIDES[v]! * (4.4 * 1.35 + grain) + const centre = vec2(vec2(W * 0.8, H * 1.32) + radial * ((n < 22.5 ? 800 : 1280) + 15) + tangent * (row.y * 0.675 + 5)) + return { at: toClip(vec2(centre + tangent * lx - radial * ly)), a: vec4(lx / 1.35, ly / 1.35, row.x, row.y) } + }, + s => { + const x = s.a.x + const y = s.a.y + const half = s.a.a * 0.5 + let pen = -half + let code = floor(s.a.z + 0.5) + let d = 99 + for (const k of [0, 1, 2, 3, 4]) { + const sym = mod(code, 4) + code = floor(code / 4) + const wide = sym > 1.5 + // An I is one upright; a V is two strokes to a foot; an X is two that cross. + const mid = pen + (wide ? 2.3 : 1.2) + const lean = wide ? 2 : 0 + const foot = sym > 2.5 ? lean : 0 + const t = sat((3 - y) / 6) + const one = abs(x - (mid - lean + (lean + foot) * t)) + const two = abs(x - (mid + lean - (lean + foot) * t)) + d = sym > 0.5 ? min(d, min(one, two)) : d + pen += sym > 0.5 ? (wide ? 4.6 : 2.4) + 0.8 : 0 + } + const ipx = (1.35 * zoom()) / s.px + const strokes = sat((0.4 - d) * ipx + 0.5) * sat((3 - abs(y)) * ipx + 0.5) + const bars = sat((0.32 - abs(abs(y) - 3.3)) * ipx + 0.5) * sat((half + 0.7 - abs(x)) * ipx + 0.5) + const cover = 0.7 * max(strokes, bars) + return vec4(arcInk() * cover, cover) + }, +) + +// A few clusters, marked the way a star chart marks them: a dashed circle with a handful of dots inside. +const CLUSTER = [[0, 0.4], [3.4, 1.6], [-2.6, 3.1], [-3, -2.6], [2.1, -3.6], [0.3, 4.6]] +const clusters = shapes( + { count: 6, instances: 24, cover: 0.02 }, + (v, n) => { + const i = mod(n, 6) + const j = floor(n / 6) + const h = weave(int(int(i) + 73), int(int(j) + 401)) + const turn = low(h) * 40 + const corner = vec2(vec2(ENDS[v]! * 2 - 1, SIDES[v]!) * (10 + px / zoom())) + const at = vec2((i + 0.15 + 0.7 * low(h)) * 270, (j + 0.15 + 0.7 * high(h)) * 260) + // A mark that would sit on a star is left out. + let clear = 1e4 + for (const oy of [-1, 0, 1]) { + for (const ox of [-1, 0, 1]) { + clear = min(clear, length(vec2(starPos(floor(at.x / CELL) + ox, floor(at.y / CELL) + oy) - at))) + } + } + return { + at: toClip(vec2(at + corner)), + a: vec4(corner.x * cos(turn) + corner.y * sin(turn), corner.y * cos(turn) - corner.x * sin(turn), 0, 0), + hide: fract(high(h) * 5.7) > 0.4 || clear < 32, + } + }, + s => { + const ipx = zoom() / s.px + const r = length(s.a.xy) + let cover = sat((0.4 - abs(r - 8.5)) * ipx + 0.5) * step(abs(fract(atan(s.a.y, s.a.x) / (PI / 6)) - 0.5), 0.3) + for (const d of CLUSTER) cover = max(cover, sat((0.85 - length(vec2(s.a.xy - vec2(d[0]!, d[1]!)))) * ipx + 0.5)) + return vec4(lineInk() * cover * 0.8, cover * 0.8) + }, +) + +// Pressing sets a pair of dividers down: a graduated copper circle about the pointer, inked over the first half second of the press. +const dividers = shapes( + { count: 6, instances: 4, cover: 0.09 }, + (v, n) => { + const f = pointer(n) + const w = f.down * hold(f) * step(n + 0.5, pointerCount) * smoothstep(0.1, 0.5, f.age) + const corner = vec2(ENDS[v]! * 2 - 1, SIDES[v]!) * (67 + px / zoom()) + return { at: toClip(vec2(toChart(f.at) + corner)), a: vec4(corner, w, 0), hide: w < 0.004 } + }, + s => { + const ipx = zoom() / s.px + const r = length(s.a.xy) + const turn = atan(s.a.y, s.a.x) + const circle = sat((0.4 - abs(r - 58)) * ipx + 0.5) + const isLong = step(abs(fract(turn / (PI / 4) + 0.5) - 0.5) * (PI / 4) * r, 1.5) + const ticks = + sat((0.4 - abs(fract(turn / (PI / 12) + 0.5) - 0.5) * (PI / 12) * r) * ipx + 0.5) * step(58, r) * step(r, 61.5 + 3.5 * isLong) + const stipple = 0.8 * sat((0.7 - length(vec2(abs(fract(turn / (PI / 30) + 0.5) - 0.5) * (PI / 30) * r, r - 51))) * ipx + 0.5) + const cover = s.a.z * 0.8 * max(circle, max(ticks, stipple)) + return vec4(copperInk() * cover, cover) + }, +) + +// Two faint shooting specks, each a head with a short row of dots left on its path, gone in a breath. +const sparks = shapes( + { count: 6, instances: 2, cover: 0.01 }, + (v, n) => { + const every = [4.7, 7.3][n]! + const tt = time + [2.2, 4.1][n]! + const turn = floor(tt / every) + const tau = tt - turn * every + const h = weave(int(turn), int(n * 31 + 7)) + const h2 = weave(h, 3) + const heading = -PI / 2 + (low(h2) - 0.5) * 2.2 + const uh = 90 * tau + const quad = strokeQuad(v, inView(vec2(0.03 + 0.94 * low(h), 0.2 + 0.8 * high(h))), vec2(cos(heading), sin(heading)), uh - 46, uh + 3, 2) + const life = sin(PI * sat(tau / 1.3)) + return { at: quad.at, a: vec4(uh - quad.along, quad.side, life, quad.along), hide: life < 0.01 } + }, + s => { + const ipx = zoom() / s.px + const back = s.a.x + // The dots stay where they were left and shrink as the head moves on. + const dots = + sat((1.2 * (1 - back / 46) - length(vec2((fract(s.a.a / 6) - 0.5) * 6, s.a.y))) * ipx + 0.5) * step(2, back) * step(0, s.a.a) + const head = sat((1.3 - length(vec2(back, s.a.y))) * ipx + 0.5) + const cover = max(dots, head) * s.a.z * 0.8 + return vec4(starInk() * cover, cover) + }, +) + +export default shader({ + fps: 60, + passes: [plate, glow, ground, numerals, ecliptic, specks, clusters, figureLines, letters, wanderers, threadLinks, fanLines, dividers, starGlyphs, sparks, meteor], +}) diff --git a/plugins/theme-examples/hooks/scenes/star-chart/still.ts b/plugins/theme-examples/hooks/scenes/star-chart/still.ts new file mode 100644 index 00000000..0969a379 --- /dev/null +++ b/plugins/theme-examples/hooks/scenes/star-chart/still.ts @@ -0,0 +1,6 @@ +/** The still picture of Star Chart, as Svg source: what shows alone wherever the shader does not play, and under it where it does. */ +export function starChartSvg(): string { + return ( + "\nStar Chart\nAn engraved star chart: stars in three sizes with letters beside the brighter ones, two ruled figures, coordinate arcs with hours in Roman numerals, a dotted ecliptic with three small planets, and a meteor with a dotted tail.\n\n\n\n\n\n\n\n\n\n\n\n" + ) +} diff --git a/plugins/theme-examples/hooks/themes.ts b/plugins/theme-examples/hooks/themes.ts index a6490cd8..e5a77e1c 100644 --- a/plugins/theme-examples/hooks/themes.ts +++ b/plugins/theme-examples/hooks/themes.ts @@ -14,9 +14,31 @@ import { plotterTideSvg, plotterTideNightSvg } from './scenes/plotter-tide' import { snowPinesSvg } from './scenes/snow-pines' import { teletextCloudsSvg } from './scenes/teletext-clouds' import { vectorRangeSvg } from './scenes/vector-range' +import { duckPondSvg } from './scenes/duck-pond/still' +import { fallingSandSvg } from './scenes/falling-sand/still' +import { fieldLinesSvg } from './scenes/field-lines/still' +import { gardenBeetlesSvg } from './scenes/garden-beetles/still' +import { koiPondSvg } from './scenes/koi-pond/still' +import { rippleTankSvg } from './scenes/ripple-tank/still' +import { seaSparkleSvg } from './scenes/sea-sparkle/still' +import { semicolonySvg } from './scenes/semicolony/still' +import { starChartSvg } from './scenes/star-chart/still' -/** One theme: what the card and the list say, the glass over it, and its scene: a function that builds one SVG whose - * own CSS animates it. A scene says whether it is dark or light with its tint, picked from it. */ +/** The scenes a shader plays over. Each name is a folder under ./scenes, which holds the shader and its still. */ +export type ShaderScene = + | 'duck-pond' + | 'falling-sand' + | 'field-lines' + | 'garden-beetles' + | 'koi-pond' + | 'ripple-tank' + | 'sea-sparkle' + | 'semicolony' + | 'star-chart' + +/** One theme: what the card and the list say, the glass over it, and its scene: a function that builds one SVG. The SVG's + * own CSS animates it, or it is a still that a shader plays over (see ./interactive). A scene says whether it is dark or + * light with its tint, picked from it. */ export type Theme = { id: string title: string @@ -27,8 +49,25 @@ export type Theme = { twin?: string glass: ThemeBackdropGlass scene: () => string + /** The shader that plays over the scene, where there is one: the scene is then its still, and follows the window. */ + shader?: ShaderScene + /** Set where the picture is dark, or pale, whatever the window: a show's bursts are then bright, or deep, in both. */ + ground?: Theme['window'] } +/** What a scene's two entries share: both of its rows below spread one of these, so they cannot differ. */ +type Picture = Required> & Pick + +const DUCK_POND: Picture = { scene: duckPondSvg, shader: 'duck-pond' } +const FALLING_SAND: Picture = { scene: fallingSandSvg, shader: 'falling-sand' } +const FIELD_LINES: Picture = { scene: fieldLinesSvg, shader: 'field-lines' } +const GARDEN_BEETLES: Picture = { scene: gardenBeetlesSvg, shader: 'garden-beetles' } +const KOI_POND: Picture = { scene: koiPondSvg, shader: 'koi-pond', ground: 'dark' } +const RIPPLE_TANK: Picture = { scene: rippleTankSvg, shader: 'ripple-tank' } +const SEA_SPARKLE: Picture = { scene: seaSparkleSvg, shader: 'sea-sparkle' } +const SEMICOLONY: Picture = { scene: semicolonySvg, shader: 'semicolony' } +const STAR_CHART: Picture = { scene: starChartSvg, shader: 'star-chart' } + export const THEMES: readonly Theme[] = [ { id: 'honu-lagoon', @@ -198,6 +237,168 @@ export const THEMES: readonly Theme[] = [ glass: { blur: 12, tint: '#2a0d12', opacity: 0.3 }, scene: emberSvg, }, + { + id: 'duck-pond', + title: 'Duck Pond', + blurb: 'Rubber ducks on a pixel pond, and a press drops bugs for them to eat.', + window: 'light', + twin: 'duck-pond-night', + glass: { blur: 16, tint: '#e8ecd9', opacity: 0.6 }, + ...DUCK_POND, + }, + { + id: 'duck-pond-night', + title: 'Duck Pond, Night', + blurb: 'Rubber ducks on a pixel pond, and a press drops bugs for them to eat.', + window: 'dark', + twin: 'duck-pond', + glass: { blur: 16, tint: '#0f2433', opacity: 0.6 }, + ...DUCK_POND, + }, + { + id: 'falling-sand', + title: 'Falling Sand', + blurb: 'Pixel sand pours down stone shelves, and a press pours more.', + window: 'light', + twin: 'falling-sand-night', + glass: { blur: 16, tint: '#f5ebd6', opacity: 0.6 }, + ...FALLING_SAND, + }, + { + id: 'falling-sand-night', + title: 'Falling Sand, Night', + blurb: 'Pixel sand pours down stone shelves, and a press pours more.', + window: 'dark', + twin: 'falling-sand', + glass: { blur: 16, tint: '#1c1424', opacity: 0.6 }, + ...FALLING_SAND, + }, + { + id: 'field-lines', + title: 'Field Lines', + blurb: 'A magnetic field drawn in short dashes that turn toward your pointer.', + window: 'light', + twin: 'field-lines-night', + glass: { blur: 16, tint: '#f3f1e7', opacity: 0.6 }, + ...FIELD_LINES, + }, + { + id: 'field-lines-night', + title: 'Field Lines, Night', + blurb: 'A magnetic field drawn in short dashes that turn toward your pointer.', + window: 'dark', + twin: 'field-lines', + glass: { blur: 16, tint: '#22282b', opacity: 0.6 }, + ...FIELD_LINES, + }, + { + id: 'garden-beetles', + title: 'Garden Beetles', + blurb: 'Pixel beetles on a garden path that hide when your pointer comes near.', + window: 'light', + twin: 'garden-beetles-night', + glass: { blur: 16, tint: '#ece5d3', opacity: 0.6 }, + ...GARDEN_BEETLES, + }, + { + id: 'garden-beetles-night', + title: 'Garden Beetles, Night', + blurb: 'Pixel beetles on a garden path that hide when your pointer comes near.', + window: 'dark', + twin: 'garden-beetles', + glass: { blur: 16, tint: '#27393a', opacity: 0.6 }, + ...GARDEN_BEETLES, + }, + { + id: 'koi-pond', + title: 'Koi Pond', + blurb: 'A pond from above with ripples at your pointer and koi fed by a press.', + window: 'light', + twin: 'koi-pond-night', + glass: { blur: 16, tint: '#d7e4e0', opacity: 0.6 }, + ...KOI_POND, + }, + { + id: 'koi-pond-night', + title: 'Koi Pond, Night', + blurb: 'A pond from above with ripples at your pointer and koi fed by a press.', + window: 'dark', + twin: 'koi-pond', + glass: { blur: 16, tint: '#0e1824', opacity: 0.6 }, + ...KOI_POND, + }, + { + id: 'ripple-tank', + title: 'Ripple Tank', + blurb: 'Water striders on a printed pond, where your pointer sends out rings.', + window: 'light', + twin: 'ripple-tank-night', + glass: { blur: 16, tint: '#d6e7e0', opacity: 0.6 }, + ...RIPPLE_TANK, + }, + { + id: 'ripple-tank-night', + title: 'Ripple Tank, Night', + blurb: 'Water striders on a printed pond, where your pointer sends out rings.', + window: 'dark', + twin: 'ripple-tank', + glass: { blur: 16, tint: '#112e34', opacity: 0.6 }, + ...RIPPLE_TANK, + }, + { + id: 'sea-sparkle', + title: 'Sea Sparkle', + blurb: 'A night sea in three inks, where your pointer stirs plankton to glow.', + window: 'light', + twin: 'sea-sparkle-night', + glass: { blur: 16, tint: '#ecf0ee', opacity: 0.6 }, + ...SEA_SPARKLE, + }, + { + id: 'sea-sparkle-night', + title: 'Sea Sparkle, Night', + blurb: 'A night sea in three inks, where your pointer stirs plankton to glow.', + window: 'dark', + twin: 'sea-sparkle', + glass: { blur: 16, tint: '#090e1c', opacity: 0.6 }, + ...SEA_SPARKLE, + }, + { + id: 'semicolony', + title: 'Semicolony', + blurb: 'Code punctuation living as small creatures, magnified at your pointer.', + window: 'light', + twin: 'semicolony-night', + glass: { blur: 16, tint: '#f1ece0', opacity: 0.6 }, + ...SEMICOLONY, + }, + { + id: 'semicolony-night', + title: 'Semicolony, Night', + blurb: 'Code punctuation living as small creatures, magnified at your pointer.', + window: 'dark', + twin: 'semicolony', + glass: { blur: 16, tint: '#13161c', opacity: 0.6 }, + ...SEMICOLONY, + }, + { + id: 'star-chart', + title: 'Star Chart', + blurb: 'An engraved sky map that links nearby stars to your pointer.', + window: 'light', + twin: 'star-chart-night', + glass: { blur: 16, tint: '#ece2ca', opacity: 0.6 }, + ...STAR_CHART, + }, + { + id: 'star-chart-night', + title: 'Star Chart, Night', + blurb: 'An engraved sky map that links nearby stars to your pointer.', + window: 'dark', + twin: 'star-chart', + glass: { blur: 16, tint: '#101b30', opacity: 0.6 }, + ...STAR_CHART, + }, ] const built = new Map() -- cgit