interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / docs / design / boot / night.html

555 lines · 30106 bytes Raw Blame History
1<!doctype html>
2<html lang="en">
3<head>
4<meta charset="utf-8">
5<meta name="viewport" content="width=device-width, initial-scale=1">
6<title>AienOS boot reference: the night</title>
7<!--
8 The boot reference for the native port (L266). It draws the iMac's startup night: the sky coming out of the
9 dark, two black holes bending the starlight as they circle and spiral in, the merger's ripple and flash, the
10 last black hole ringing and dissolving, and the chirp. Every constant and formula is the kernel's
11 (native/kernel/ui/night.c, chirp.c, allen_ui.c on feat/allen-boot-everywhere, the bytes the iMac runs at
12 aienos_commit 0930240), written out in floating point. docs/design/boot/MATHS.md is the same maths as
13 equations with the kernel line for each.
14
15 URL options: ?t=MS freeze at that time on the night's clock (screenshots)
16 ?measure=1 run the clock without sound and record frame times per stage (window.__night_result)
17 ?w=&h= drawing size (default 1920 x 1080, the design grid)
18 ?hud=1 show the clock and the stage
19 ?reduced=1 reduced motion: a 0.9 s dawn onto the last frame (DESIGN.md, Accessibility)
20 Keys: any key or click starts the night with its sound; R restarts; H shows the clock.
21-->
22<script src="https://cdn.jsdelivr.net/npm/p5@1.11.3/lib/p5.min.js"
23 integrity="sha384-6FJh+LJ4p/43R8LpkZjM6iDKaXx59crdjiBG6n2sioNPNXc+c2iNpBvO2bQ25nSH"
24 crossorigin="anonymous"></script>
25<link rel="stylesheet" href="../tokens/allen-tokens.css">
26<style>
27 html, body { margin: 0; height: 100%; background: var(--allen-colors-night); overflow: hidden; }
28 main { position: fixed; inset: 0; display: grid; place-items: center; }
29 canvas { max-width: 100vw; max-height: 100vh; object-fit: contain; display: block; }
30 #hud { position: fixed; left: 24px; bottom: 20px; font: 500 14px/1.4 ui-monospace, monospace;
31 color: var(--allen-colors-faint); display: none; }
32 #start { position: fixed; left: 0; right: 0; bottom: 96px; text-align: center;
33 font: 500 16px/22px var(--allen-typography-small-fontFamily);
34 letter-spacing: .02em; color: var(--allen-colors-hint); transition: opacity .7s; }
35</style>
36</head>
37<body>
38<main id="stage"></main>
39<div id="start">Click or press a key to start the night with its sound</div>
40<div id="hud"></div>
41<script>
42"use strict";
43p5.disableFriendlyErrors = true;
44
45/* ---- the night's clock and sizes: night.h, chirp.h, allen_ui.c (DESIGN.md boot.* and sound.*) ---- */
46const FADE_END = 6800, HOLES = 2600, SPIRAL = 6000, MERGE = 12000, RIPPLE = 3600, GONE = 15200;
47const ORBIT_MS = 5200; // night.c: one turn of the wide orbit
48const CHIRP = { rate: 48000, from: 200, to: 1000, peak: 0.70, inMs: 40, rise: 600, end: MERGE + 300, tau: 30 };
49const STAGES = [["dark", 0, HOLES], ["holes", HOLES, SPIRAL], ["spiral", SPIRAL, MERGE],
50 ["merger", MERGE, MERGE + RIPPLE], ["settled", MERGE + RIPPLE, MERGE + RIPPLE + 1400]];
51
52const q = new URLSearchParams(location.search);
53const W = +q.get("w") || 1920, H = +q.get("h") || 1080;
54const K = Math.floor(Math.min(W * 256 / 1920, H * 256 / 1080)); // allen_ui.c scale_for
55const S = (v) => Math.floor((v * K + 128) / 256); // design px to px
56const CX = Math.floor(W / 2), CY = Math.floor(H * 40 / 100); // where the holes merge (allen_ui.c 853)
57const FREEZE = q.has("t") ? +q.get("t") : null, MEASURE = q.get("measure") === "1";
58const REDUCED = q.get("reduced") === "1" || matchMedia("(prefers-reduced-motion: reduce)").matches;
59
60/* ---- integer helpers, as paint.c / paint.h ---- */
61const fl = Math.floor, tr = Math.trunc;
62function hash(x, y, seed) {
63 let h = (Math.imul(x, 0x8da6b343) ^ Math.imul(y, 0xd8163841) ^ Math.imul(seed, 0xcb1ab31f)) >>> 0;
64 h = (h ^ (h >>> 13)) >>> 0; h = Math.imul(h, 0x5bd1e995) >>> 0;
65 h = (h ^ (h >>> 15)) >>> 0; h = Math.imul(h, 0x27d4eb2d) >>> 0;
66 return (h ^ (h >>> 16)) >>> 0;
67}
68const clamp = (v, a, b) => (v < a ? a : v > b ? b : v);
69const smoothI = (t) => { t = clamp(t, 0, 256); return fl(t * t * (768 - 2 * t) / 65536); }; // ck_smooth
70function noise(x, y, seed) { // ck_noise: smooth value noise 0..65535
71 const ix = x >> 8, iy = y >> 8, fx = smoothI(x & 255), fy = smoothI(y & 255);
72 const v00 = hash(ix, iy, seed) >>> 16, v10 = hash(ix + 1, iy, seed) >>> 16;
73 const v01 = hash(ix, iy + 1, seed) >>> 16, v11 = hash(ix + 1, iy + 1, seed) >>> 16;
74 const a = v00 * (256 - fx) + v10 * fx, b = v01 * (256 - fx) + v11 * fx;
75 return fl((a * (256 - fy) + b * fy) / 65536);
76}
77function fbm(x, y, seed, oct) { // ck_fbm
78 let sum = 0, total = 0;
79 for (let o = 0; o < oct; o++) {
80 const w = 1 << (oct - 1 - o);
81 sum += noise(x << o, y << o, (seed + o * 7919) >>> 0) * w; total += w;
82 }
83 return fl(sum / total);
84}
85const expNeg = (x) => (x < 4096 ? Math.round(65536 * Math.exp(-x / 256)) : 0); // ck_exp_neg, Q16
86function gauss(v, width) { // exp(-(v/width)^2), Q16
87 const qv = tr(v * 256 / (width || 1));
88 return qv > 4095 || qv < -4095 ? 0 : expNeg(fl(qv * qv / 256));
89}
90const isqrt = (v) => fl(Math.sqrt(v));
91const smooth01 = (t) => { t = clamp(t, 0, 1); return t * t * (3 - 2 * t); };
92const easeOut01 = (t) => { t = 1 - clamp(t, 0, 1); return 1 - t * t * t; };
93const rgb = (r, g, b) => [r, g, b];
94
95/* ---- the sky, made once (night.c clouds, scale_up, stars, galaxy, appear_map) ---- */
96const SKY = new Uint8ClampedArray(W * H * 4); // RGBA, A = 255
97const QW = (W >> 2) + 1, QH = (H >> 2) + 1, AW = (W >> 2) + 1, AH = (H >> 2) + 1;
98const APPEAR = new Uint8Array(AW * AH);
99const TW = []; // the bright stars that twinkle
100const bake = {};
101
102function bandOf() {
103 const b = { x0: tr(-W / 20), y0: tr(H * 90 / 100), dx: tr(W * 110 / 100), dy: tr(-H * 92 / 100), half: tr(H * 16 / 100) };
104 b.len = isqrt(b.dx * b.dx + b.dy * b.dy);
105 return b;
106}
107const bandAt = (b, x, y) => [tr(((x - b.x0) * b.dy - (y - b.y0) * b.dx) / b.len),
108 tr(((x - b.x0) * b.dx + (y - b.y0) * b.dy) / b.len)];
109
110function clouds() { // the soft layer, 16ths of a level
111 const qd = new Uint16Array(QW * QH * 3), b = bandOf();
112 for (let qy = 0; qy < QH; qy++) for (let qx = 0; qx < QW; qx++) {
113 const x = qx * 4, y = qy * 4, [across, along] = bandAt(b, x, y);
114 const core = gauss(across, b.half), wide = gauss(across, b.half * 2);
115 const n1 = fbm(tr(x * 768 / H), tr(y * 768 / H), 101, 5);
116 const n2 = fbm(tr(x * 1792 / H), tr(y * 1792 / H), 202, 4);
117 const lane = gauss(across - tr(H * 2 / 100), tr(H * 5 / 100));
118 const dust = fl(lane * smoothI(tr((n2 - 22000) * 256 / 26000)) / 256);
119 const bright = gauss(along - tr(b.len * 40 / 100), tr(b.len * 38 / 100));
120 let mw = fl(core * (32768 + fl(n1 / 2)) / 65536);
121 mw = fl(mw * (65536 - fl(dust * 52428 / 65536)) / 65536);
122 mw = fl(mw * (29491 + fl(bright * 36045 / 65536)) / 65536);
123 const halo = fl(wide * (39321 + fl(n1 * 26214 / 65536)) / 65536);
124 const n4 = fbm(tr(x * 512 / H), tr(y * 512 / H), 404, 4);
125 const n5 = fbm(tr(x * 666 / H), tr(y * 666 / H), 505, 4);
126 const n6 = fbm(tr(x * 1024 / H), tr(y * 1024 / H), 606, 4);
127 let vi = smoothI(tr((n4 - 36000) * 256 / 29535)), bl = smoothI(tr((n5 - 35000) * 256 / 30535));
128 const ro = fl(smoothI(tr((n6 - 33000) * 256 / 32535)) * wide / 65536);
129 vi = fl(vi * vi / 256); bl = fl(bl * bl / 256);
130 const top = 256 - tr(y * 256 / H);
131 const r = 26 + tr(top / 32) + fl(mw * 44 * 16 / 65536) + fl(halo * 6 * 16 / 65536) + fl(vi * 22 * 16 / 256) + fl(bl * 5 * 16 / 256) + fl(ro * 22 * 16 / 256);
132 const g = 34 + tr(top / 32) + fl(mw * 40 * 16 / 65536) + fl(halo * 8 * 16 / 65536) + fl(vi * 8 * 16 / 256) + fl(bl * 12 * 16 / 256) + fl(ro * 7 * 16 / 256);
133 const bb = 92 + tr(top / 16) + fl(mw * 46 * 16 / 65536) + fl(halo * 16 * 16 / 65536) + fl(vi * 28 * 16 / 256) + fl(bl * 30 * 16 / 256) + fl(ro * 15 * 16 / 256);
134 const o = (qy * QW + qx) * 3;
135 qd[o] = clamp(r, 0, 4080); qd[o + 1] = clamp(g, 0, 4080); qd[o + 2] = clamp(bb, 0, 4080);
136 }
137 return qd;
138}
139
140function scaleUp(qd) { // bilinear, 4 x 4 ordered dither
141 const bayer = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5];
142 for (let y = 0; y < H; y++) {
143 const qy = y >> 2, fy = y & 3;
144 for (let x = 0; x < W; x++) {
145 const qx = x >> 2, fx = x & 3, d = bayer[(y & 3) * 4 + (x & 3)], p = (y * W + x) * 4;
146 for (let k = 0; k < 3; k++) {
147 const r0 = (qy * QW + qx) * 3 + k, r1 = ((qy + 1) * QW + qx) * 3 + k;
148 const a = qd[r0] * (4 - fx) + qd[r0 + 3] * fx, b = qd[r1] * (4 - fx) + qd[r1 + 3] * fx;
149 SKY[p + k] = Math.min(255, (fl((a * (4 - fy) + b * fy) / 16) + d) >> 4);
150 }
151 SKY[p + 3] = 255;
152 }
153 }
154}
155
156function glow(cx, cy, r, c, a) { // ck_glow: (1 - d^2/r^2)^3, 16ths of a px
157 if (r <= 0 || !a) return;
158 const r2 = r * r, x0 = Math.max(0, (cx - r) >> 4), x1 = Math.min(W - 1, (cx + r) >> 4);
159 const y0 = Math.max(0, (cy - r) >> 4), y1 = Math.min(H - 1, (cy + r) >> 4);
160 for (let y = y0; y <= y1; y++) {
161 const dy = y * 16 + 8 - cy, dy2 = dy * dy;
162 if (dy2 >= r2) continue;
163 for (let x = x0; x <= x1; x++) {
164 const dx = x * 16 + 8 - cx, d2 = dx * dx + dy2;
165 if (d2 >= r2) continue;
166 const f = 256 - fl(d2 * 256 / r2), w = fl(fl(f * f / 256) * f / 256), aa = fl(w * a / 256), p = (y * W + x) * 4;
167 SKY[p] += fl(c[0] * aa / 256); SKY[p + 1] += fl(c[1] * aa / 256); SKY[p + 2] += fl(c[2] * aa / 256);
168 }
169 }
170}
171function add(p, c, a) { SKY[p] += fl(c[0] * a / 256); SKY[p + 1] += fl(c[1] * a / 256); SKY[p + 2] += fl(c[2] * a / 256); }
172
173function starColor(h) { // five star colours by share
174 const p = h % 100;
175 return p < 10 ? rgb(170, 192, 255) : p < 55 ? rgb(218, 228, 255) : p < 85 ? rgb(255, 250, 242)
176 : p < 97 ? rgb(255, 234, 196) : rgb(255, 204, 160);
177}
178
179function stars() {
180 const b = bandOf(), area = W * H;
181 for (let i = 0; i < fl(area / 800); i++) { // field stars, denser along the band
182 const x = hash(i, 1, 77) % (W * 16), y = hash(i, 2, 77) % (H * 16), [across] = bandAt(b, x >> 4, y >> 4);
183 if ((hash(i, 3, 77) & 65535) > 36000 + fl(gauss(across, b.half) * 29535 / 65536)) continue;
184 const u = hash(i, 4, 77) & 255, lv = 14 + fl(241 * (u * u * u * u) / 4294967296);
185 glow(x, y, fl((20 + fl(16 * lv / 255)) * K / 256), starColor(hash(i, 5, 77)), lv);
186 }
187 for (let i = 0; i < fl(area / 170); i++) { // star dust in the band
188 const x = hash(i, 6, 78) % (W * 16), y = hash(i, 7, 78) % (H * 16), [across] = bandAt(b, x >> 4, y >> 4);
189 const c = gauss(across, tr(b.half * 8 / 10));
190 if ((hash(i, 8, 78) & 65535) > fl(c * c / 65536)) continue;
191 glow(x, y, fl(20 * K / 256), rgb(226, 230, 255), 10 + hash(i, 9, 78) % 44);
192 }
193 const nb = clamp(fl(area / 50000), 24, 256); // bright stars: a halo each, and they twinkle
194 for (let i = 0; i < nb; i++) {
195 const x = hash(i, 1, 91) % (W * 16), y = hash(i, 2, 91) % (H * 16), c = starColor((hash(i, 3, 91) | 1) >>> 0);
196 const lv = 190 + hash(i, 4, 91) % 66, r = fl(34 * K / 256), period = 2600 + hash(i, 5, 91) % 4200;
197 glow(x, y, r, c, lv); glow(x, y, r * 5, c, 22);
198 TW.push({ x, y, r: r * 2, c, period, phase: hash(i, 6, 91) % period });
199 }
200}
201
202const GDEF = [ // fx, fy (thousandths), radius (design px), tilt, turn (deg), kind, bright: night.c gdef
203 [150, 205, 112, 38, 30, "spiral", 160], [805, 135, 76, 62, -25, "spiral", 140], [265, 705, 42, 45, 60, "round", 130],
204 [870, 750, 88, 84, 15, "edge", 150], [60, 545, 40, 22, 0, "spiral", 120], [600, 95, 28, 52, 70, "spiral", 110]];
205
206function galaxies() {
207 GDEF.forEach(([fx, fy, radius, tilt, turn, kind, bright], i) => {
208 const hb = fl((fl(radius * 13 / 10) * K + 128) / 256) + 1, gxp = fl(W * fx / 1000), gyp = fl(H * fy / 1000);
209 const gx = gxp * 16 + 8, gy = gyp * 16 + 8, rg = S(radius) * 16;
210 const ct = Math.cos(turn * Math.PI / 180), st = Math.sin(turn * Math.PI / 180);
211 const ci = Math.max(600 / 16384, Math.cos(tilt * Math.PI / 180));
212 for (let y = Math.max(0, gyp - hb); y <= Math.min(H - 1, gyp + hb); y++)
213 for (let x = Math.max(0, gxp - hb); x <= Math.min(W - 1, gxp + hb); x++) {
214 const dx = x * 16 + 8 - gx, dy = y * 16 + 8 - gy;
215 const u = dx * ct + dy * st, v = dy * ct - dx * st, vd = v / ci; // back to face-on
216 const rq = Math.hypot(u, vd) * 256 / rg; // radius, 256 = the galaxy's
217 if (rq > 330) continue;
218 const th = (Math.atan2(vd, u) / (2 * Math.PI)) * 65536;
219 const nz = fbm(tr(u * 1536 / rg), tr(vd * 1536 / rg), 700 + i, 3);
220 const fine = fbm(tr(u * 5120 / rg), tr(vd * 5120 / rg), 750 + i, 2);
221 const vb = v * 2 / (1 + ci), rb = Math.hypot(u, vb) * 256 / rg; // the bulge flattens half as much
222 let lb, ld, knot = 0;
223 if (kind === "round") {
224 lb = gauss(rb, 30) * 0.8; ld = expNeg(fl(rb * 1075 / 256)) * 0.6;
225 } else {
226 const lr = (Math.log2(Math.max(rq, 4)) - 8) * 65536; // log2(r), r in 256ths
227 const ph = 2 * th + lr * 30000 / 65536; // two arms, wound about a turn and a half
228 let arm = (1 + Math.cos(ph / 65536 * 2 * Math.PI)) / 2; arm *= arm;
229 const from = smoothI(tr((rq - 34) * 256 / 90)) / 256; // arms begin outside the core
230 arm = 1 - (1 - arm) * from;
231 const clump = 0.5 + nz * 22000 / 65536 / 65536 + fine * 10768 / 65536 / 65536;
232 lb = gauss(rb, 58) * 0.9;
233 ld = expNeg(tr(rq * 12 / 5)) * (0.125 + arm * 0.875 * clump);
234 if (arm > 0.7 && nz > 44000 && rq > 50) knot = ld * (nz - 44000) / 21535;
235 if (kind === "edge" && rq < 240) { // the dust lane of a disc seen edge-on
236 const lane = gauss(v, rg * 5 / 100) / 65536;
237 ld *= 1 - lane * 56000 / 65536; lb *= 1 - lane * 40000 / 65536;
238 }
239 }
240 const edge = 1 - smoothI(tr((rq - 250) * 256 / 80)) / 256; // soft edge at 1.3 radii
241 const p = (y * W + x) * 4;
242 add(p, rgb(255, 222, 178), fl(lb * edge * bright / 65536));
243 add(p, rgb(150, 170, 255), fl(ld * edge * bright / 65536));
244 add(p, rgb(255, 140, 190), fl(knot * edge * bright / 65536 / 2));
245 }
246 });
247}
248
249function appearMap() { // when each 4 x 4 cell comes out of the dark
250 let far = 0;
251 for (const [x, y] of [[0, 0], [W, 0], [0, H], [W, H]]) far = Math.max(far, isqrt((x - CX) ** 2 + (y - CY) ** 2));
252 for (let y = 0; y < AH; y++) for (let x = 0; x < AW; x++) {
253 const px = x * 4, py = y * 4;
254 const a = fbm(tr(px * 640 / H), tr(py * 640 / H), 909, 4) >> 8;
255 const r = tr(isqrt((px - CX) ** 2 + (py - CY) ** 2) * 255 / (far || 1));
256 APPEAR[y * AW + x] = tr((a * 6 + r * 4) / 10);
257 }
258}
259
260function faded(x, y, l, t) { // ck_night_faded: 0..1
261 if (t >= FADE_END) return 1;
262 x = clamp(x, 0, W - 1); y = clamp(y, 0, H - 1);
263 const from = 300 + APPEAR[(y >> 2) * AW + (x >> 2)] * 12 + (255 - l) * 8, dt = t - from;
264 return dt <= 0 ? 0 : dt >= 1365 ? 1 : smooth01(dt * 48 / 256 / 256);
265}
266
267/* ---- the black holes, the ripple and the flash at a time (ck_night_at, allen_ui.c flash) ---- */
268function orbit(t, a0) { // angle in turns, separation in px
269 const from = (SPIRAL - HOLES) / ORBIT_MS;
270 if (t < SPIRAL) return { ang: (t - HOLES) / ORBIT_MS, sep: a0 };
271 const L = (MERGE - t) / (MERGE - SPIRAL); // the time left, 1 to 0
272 const turns = 8 * (MERGE - SPIRAL) / (5 * ORBIT_MS); // the spiral keeps the wide orbit's speed
273 return { ang: from + turns * (1 - Math.pow(L, 5 / 8)), sep: a0 * Math.pow(L, 1 / 4) };
274}
275let FAR = 0;
276for (const [x, y] of [[0, 0], [W, 0], [0, H], [W, H]]) FAR = Math.max(FAR, isqrt((x - CX) ** 2 + (y - CY) ** 2));
277
278function nightAt(t) {
279 const s = { holes: [], ripple: null, flash: null, lens: null };
280 const eFull = S(60), rsFull = S(20), a0 = S(270);
281 const last = orbit(MERGE - 1, a0).ang;
282 if (t >= HOLES && t < MERGE) {
283 const { ang, sep } = orbit(t, a0);
284 const e = eFull * smooth01((t - HOLES) / 2600), rs = rsFull * smooth01((t - HOLES - 1400) / 2000);
285 const hx = sep / 2 * Math.cos(2 * Math.PI * ang), hy = sep / 2 * Math.sin(2 * Math.PI * ang) / 2; // the orbit leans away
286 s.holes.push([CX + 0.5 + hx, CY + 0.5 + hy, e, rs], [CX + 0.5 - hx, CY + 0.5 - hy, e, rs]);
287 } else if (t >= MERGE && t < GONE) {
288 const tm = t - MERGE, e = eFull * 362 / 256, rs0 = rsFull * 323 / 256, rs1 = rsFull * 2;
289 let rs = rs0 + (rs1 - rs0) * smooth01(tm / 180);
290 rs += rs * 0.07 * Math.sin(2 * Math.PI * tm / 95) * Math.exp(-tm / 260); // it rings
291 const keep = 1 - smooth01((t - MERGE - 900) / (GONE - MERGE - 900));
292 s.holes.push([CX + 0.5, CY + 0.5, e * keep, rs * keep * keep]);
293 }
294 for (const [x, y, e, rs] of s.holes) {
295 const reach = Math.max(e * 4.4, rs * 1.6) + 2;
296 const b = [x - reach, y - reach, x + reach, y + reach];
297 s.lens = s.lens ? [Math.min(s.lens[0], b[0]), Math.min(s.lens[1], b[1]), Math.max(s.lens[2], b[2]), Math.max(s.lens[3], b[3])] : b;
298 }
299 if (t >= MERGE && t < MERGE + RIPPLE) {
300 const trp = t - MERGE, w = clamp(S(60), 4, 200), reach = w * 2.2, far = FAR + reach;
301 const R = far * trp / RIPPLE;
302 const env = smooth01(trp / 220) * (1 - smooth01((trp - RIPPLE * 0.6) / (RIPPLE * 0.4)));
303 const amp = S(6) * (1 - (150 / 256) * R / far) * env;
304 s.ripple = { R, w, reach, amp, lambda: Math.max(S(110), 8), env,
305 c2: Math.cos(4 * Math.PI * last), s2: Math.sin(4 * Math.PI * last) };
306 }
307 if (t >= MERGE && t < MERGE + 1600) {
308 const tm = t - MERGE;
309 const rad = S(40) + S(220) * easeOut01(tm / 1200);
310 const a = smooth01(tm / 120) * Math.exp(-Math.max(0, tm - 120) / 450) * 230 / 256;
311 s.flash = { rad, a, keep: 1 - smooth01((tm - 100) / 300), unflash: s.holes.length === 1 && tm >= 100 };
312 }
313 return s;
314}
315
316/* ---- the picture: one pass per pixel ---- */
317const VERT = `
318attribute vec3 aPosition;
319uniform mat4 uProjectionMatrix, uModelViewMatrix;
320void main() { gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(aPosition, 1.0); }`;
321const FRAG = `
322precision highp float;
323uniform sampler2D uSky, uAppear;
324uniform vec2 uRes, uAppearRes, uC;
325uniform float uT, uFadeEnd;
326uniform vec4 uHole0, uHole1; // x, y, Einstein radius, shadow radius (px)
327uniform float uHoles;
328uniform vec4 uRip; // crest radius, width, amplitude, wavelength (px)
329uniform vec4 uRip2; // envelope, cos 2psi, sin 2psi, on
330uniform vec4 uFlash; // radius, strength, kept fraction inside the shadow, on
331uniform vec4 uStar[64]; // x, y, radius (px), strength
332uniform vec3 uStarC[64];
333uniform float uStars;
334
335float gauss(float v, float w) { return exp(-(v / w) * (v / w)); }
336float sm(float t) { t = clamp(t, 0.0, 1.0); return t * t * (3.0 - 2.0 * t); }
337
338// the pull of one hole: a point lens, beta = theta - E^2/theta, eased to nothing from 2.2 E to 4.4 E
339vec2 pull(vec2 p, vec4 h) {
340 float e = h.z;
341 vec2 d = p - h.xy;
342 float d2 = max(dot(d, d), 0.25), outer = 19.36 * e * e, inner = 4.84 * e * e;
343 if (e <= 0.0 || d2 >= outer) return vec2(0.0);
344 float f = e * e / d2;
345 if (d2 > inner) { float w = (outer - d2) / (outer - inner); f *= w * w; }
346 return -d * f;
347}
348
349vec3 sky(vec2 p) { return texture2D(uSky, p / uRes).rgb; }
350
351// shadow and photon ring of one hole (night.c near_px)
352vec3 shadow(vec3 c, vec2 p, vec4 h) {
353 float rs = h.w;
354 if (rs <= 0.0) return c;
355 float r = length(p - h.xy);
356 if (r >= 1.5 * rs) return c;
357 c = mix(c, vec3(0.0), clamp(0.5 - (r - rs), 0.0, 1.0));
358 float ring = abs(r - 1.04 * rs) * 16.0;
359 float a = ring < 22.0 ? (22.0 - ring) * 6.0 / 256.0 : 0.0;
360 if (r > rs) a += 46.0 / 256.0 * (1.5 * rs - r) / (0.5 * rs + 1.0 / 16.0);
361 return c + vec3(1.0, 228.0 / 255.0, 196.0 / 255.0) * a;
362}
363
364vec3 glow(vec3 c, vec2 p, vec2 at, float r, vec3 col, float a) {
365 vec2 d = p - at;
366 float f = 1.0 - dot(d, d) / (r * r);
367 return f <= 0.0 ? c : c + col * f * f * f * a;
368}
369
370void main() {
371 vec2 p = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y); // pixel centre, y down like the kernel
372 vec2 off = vec2(0.0);
373 if (uHoles > 0.5) off += pull(p, uHole0);
374 if (uHoles > 1.5) off += pull(p, uHole1);
375 float light = 0.0;
376 if (uRip2.w > 0.5) { // the ripple: radial, stretching along psi and squeezing across it
377 vec2 d = p - uC;
378 float r = length(d), o = floor(r) - uRip.x;
379 if (r > 0.0 && abs(o) <= 2.2 * uRip.y) {
380 float disp = uRip.z * sin(6.2831853 * o / uRip.w) * gauss(o, uRip.y);
381 float qq = ((d.x * d.x - d.y * d.y) * uRip2.y + 2.0 * d.x * d.y * uRip2.z) / (r * r);
382 off -= disp * (0.35 + 0.65 * qq) * d / r;
383 light = gauss(o, 0.6 * uRip.y) * uRip2.x * 46.0 / 256.0;
384 }
385 }
386 vec3 c = sky(p + off);
387 if (uT < uFadeEnd) { // coming out of the dark: bright light first, in patches
388 float l = max(c.r, max(c.g, c.b)) * 255.0;
389 float ap = texture2D(uAppear, (floor(p / 4.0) + 0.5) / uAppearRes).r * 255.0;
390 float dt = uT - (300.0 + ap * 12.0 + (255.0 - l) * 8.0);
391 c *= dt <= 0.0 ? 0.0 : sm(dt / 1365.33);
392 }
393 c += vec3(86.0, 108.0, 176.0) / 255.0 * light;
394 if (uHoles > 0.5) c = shadow(c, p, uHole0);
395 if (uHoles > 1.5) c = shadow(c, p, uHole1);
396 for (int i = 0; i < 64; i++) { // the bright stars twinkle
397 if (float(i) >= uStars) break;
398 c = glow(c, p, uStar[i].xy, uStar[i].z, uStarC[i], uStar[i].w);
399 }
400 if (uFlash.w > 0.5) { // the merger's flash, then the shadow takes its black back
401 vec3 before = c;
402 c = glow(c, p, uC, uFlash.x, vec3(200.0, 210.0, 255.0) / 255.0, uFlash.y / 3.0);
403 c = glow(c, p, uC, uFlash.x / 3.0, vec3(255.0, 240.0, 224.0) / 255.0, uFlash.y);
404 if (uHoles > 0.5 && uFlash.z < 1.0 && uHole0.w > 1.0) {
405 float r = length(p - uHole0.xy);
406 float inside = clamp(uHole0.w - r, 0.0, 1.0);
407 c *= 1.0 - inside * (1.0 - uFlash.z);
408 }
409 }
410 gl_FragColor = vec4(min(c, vec3(1.0)), 1.0);
411}`;
412
413/* ---- the chirp (chirp.c), as a buffer ---- */
414function chirpBuffer(ctx) {
415 const rate = ctx.sampleRate, start = MERGE - CHIRP.rise, f0 = start - CHIRP.inMs;
416 const n = Math.ceil((CHIRP.end - f0) / 1000 * rate), buf = ctx.createBuffer(1, n, rate), out = buf.getChannelData(0);
417 const T = CHIRP.rise / 1000, Lcap = Math.pow(CHIRP.from / CHIRP.to, 8 / 3); // pitch stops rising at 1000 Hz
418 const steady = CHIRP.peak * Math.pow(Lcap, 1 / 4); // the level at L = 1
419 const phaseIn = CHIRP.from * CHIRP.inMs / 1000; // turns before the sweep
420 const phaseCap = phaseIn + CHIRP.from * T * 8 / 5 * (1 - Math.pow(Lcap, 5 / 8));
421 for (let i = 0; i < n; i++) {
422 const ms = f0 + i * 1000 / rate;
423 let level, phase;
424 if (ms < start) { level = steady * smooth01((ms - f0) / CHIRP.inMs); phase = CHIRP.from * (ms - f0) / 1000; }
425 else {
426 const L = Math.max(0, (MERGE - ms) / CHIRP.rise);
427 if (L > Lcap) { // the inspiral: pitch as L^(-3/8), level as pitch^(2/3)
428 level = CHIRP.peak * Math.pow(Lcap, 1 / 4) / Math.pow(L, 1 / 4);
429 phase = phaseIn + CHIRP.from * T * 8 / 5 * (1 - Math.pow(L, 5 / 8));
430 } else {
431 phase = phaseCap + CHIRP.to * (ms - (MERGE - Lcap * CHIRP.rise)) / 1000;
432 level = ms < MERGE ? CHIRP.peak : CHIRP.peak * Math.exp(-(ms - MERGE) / CHIRP.tau); // rings away, 30 ms
433 }
434 }
435 out[i] = level * Math.sin(2 * Math.PI * phase);
436 }
437 return { buf, at: f0 };
438}
439
440/* ---- the sketch ---- */
441let sh, skyImg, appearImg, t0 = null, audio = null, hudOn = q.get("hud") === "1";
442const frames = []; // [t, interval ms, render ms]
443window.__night_done = false;
444
445function stageOf(t) { for (const s of STAGES) if (t >= s[1] && t < s[2]) return s[0]; return "after"; }
446
447new p5((p) => {
448 p.setup = () => {
449 let m = performance.now();
450 const qd = clouds(); bake.cloudsMs = performance.now() - m; m = performance.now();
451 scaleUp(qd); bake.scaleMs = performance.now() - m; m = performance.now();
452 stars(); bake.starsMs = performance.now() - m; m = performance.now();
453 galaxies(); bake.galaxiesMs = performance.now() - m; m = performance.now();
454 appearMap(); bake.appearMs = performance.now() - m;
455 const cnv = p.createCanvas(W, H, p.WEBGL);
456 cnv.parent("stage");
457 p.pixelDensity(1);
458 p.noStroke();
459 sh = p.createShader(VERT, FRAG);
460 skyImg = p.createImage(W, H); skyImg.loadPixels(); skyImg.pixels.set(SKY); skyImg.updatePixels();
461 appearImg = p.createImage(AW, AH); appearImg.loadPixels();
462 for (let i = 0; i < AW * AH; i++) appearImg.pixels.set([APPEAR[i], 0, 0, 255], i * 4);
463 appearImg.updatePixels();
464 p.textureWrap(p.CLAMP);
465 p.shader(sh);
466 sh.setUniform("uSky", skyImg);
467 sh.setUniform("uAppear", appearImg);
468 p._renderer.getTexture(appearImg).setInterpolation(p.NEAREST, p.NEAREST);
469 const gl = p.drawingContext, ext = gl.getExtension("WEBGL_debug_renderer_info");
470 bake.renderer = ext ? gl.getParameter(ext.UNMASKED_RENDERER_WEBGL) : gl.getParameter(gl.RENDERER);
471 bake.size = `${W}x${H}`; bake.brightStars = TW.length;
472 if (FREEZE !== null || MEASURE) { t0 = performance.now() - (FREEZE || 0); document.getElementById("start").style.opacity = 0; }
473 if (FREEZE !== null) p.noLoop();
474 };
475
476 const begin = () => {
477 if (t0 !== null && FREEZE === null && !MEASURE && audio) return;
478 document.getElementById("start").style.opacity = 0;
479 t0 = performance.now();
480 frames.length = 0;
481 if (!MEASURE && !REDUCED) {
482 try {
483 const ctx = audio ? audio.ctx : new AudioContext();
484 const { buf, at } = chirpBuffer(ctx);
485 const src = ctx.createBufferSource(); src.buffer = buf; src.connect(ctx.destination);
486 src.start(ctx.currentTime + at / 1000);
487 audio = { ctx, src };
488 } catch (e) { audio = { ctx: null }; } // no sound device: the night plays silent
489 }
490 };
491 p.mousePressed = begin;
492 p.keyPressed = () => {
493 if (p.key === "h" || p.key === "H") { hudOn = !hudOn; return; }
494 if (p.key === "r" || p.key === "R") { if (audio && audio.src) try { audio.src.stop(); } catch (e) {} audio = null; t0 = null; }
495 begin();
496 };
497
498 let lastFrame = null;
499 p.draw = () => {
500 const now = performance.now(), m0 = now;
501 let t = t0 === null ? 0 : FREEZE !== null ? FREEZE : now - t0;
502 if (REDUCED && FREEZE === null) t = t0 === null ? 0 : GONE + 1600; // the last frame, eased in by CSS below
503 const s = nightAt(t);
504 sh.setUniform("uRes", [W, H]); sh.setUniform("uAppearRes", [AW, AH]); sh.setUniform("uC", [CX + 0.5, CY + 0.5]);
505 sh.setUniform("uT", t0 === null ? 0 : t); sh.setUniform("uFadeEnd", FADE_END);
506 sh.setUniform("uHoles", s.holes.length);
507 sh.setUniform("uHole0", s.holes[0] || [0, 0, 0, 0]); sh.setUniform("uHole1", s.holes[1] || [0, 0, 0, 0]);
508 const r = s.ripple;
509 sh.setUniform("uRip", r ? [r.R, r.w, r.amp, r.lambda] : [0, 1, 0, 1]);
510 sh.setUniform("uRip2", r ? [r.env, r.c2, r.s2, 1] : [0, 0, 0, 0]);
511 const f = s.flash;
512 sh.setUniform("uFlash", f ? [f.rad, f.a, f.unflash ? f.keep : 1, 1] : [0, 0, 1, 0]);
513 const st = [], sc = [];
514 for (const star of TW) {
515 const x = star.x / 16, y = star.y / 16, rr = star.r / 16;
516 const inLens = s.lens && x + rr > s.lens[0] && x - rr < s.lens[2] && y + rr > s.lens[1] && y - rr < s.lens[3];
517 const tw = (1 + Math.sin(2 * Math.PI * ((t + star.phase) % star.period) / star.period)) / 2;
518 const a = inLens || t0 === null ? 0 : tw * tw * 120 / 256 * faded(x | 0, y | 0, 255, t);
519 st.push(x, y, rr, a); sc.push(star.c[0] / 255, star.c[1] / 255, star.c[2] / 255);
520 if (st.length >= 256) break;
521 }
522 while (st.length < 256) { st.push(0, 0, 1, 0); sc.push(0, 0, 0); }
523 sh.setUniform("uStar", st); sh.setUniform("uStarC", sc); sh.setUniform("uStars", Math.min(64, TW.length));
524 p.rect(-W / 2, -H / 2, W, H);
525 if (MEASURE) {
526 const gl = p.drawingContext, px = new Uint8Array(4); // reading a pixel waits for the whole frame
527 gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, px);
528 if (lastFrame !== null) frames.push([t, now - lastFrame, performance.now() - m0]);
529 if (t > STAGES[STAGES.length - 1][2]) { window.__night_result = report(); window.__night_done = true; p.noLoop(); }
530 }
531 lastFrame = now;
532 if (FREEZE !== null) window.__night_done = true;
533 const hud = document.getElementById("hud");
534 hud.style.display = hudOn ? "block" : "none";
535 if (hudOn) hud.textContent = `${(t / 1000).toFixed(2)} s ${stageOf(t)} ${p.frameRate().toFixed(0)} fps`;
536 };
537}, "stage");
538
539function stats(xs) {
540 const s = [...xs].sort((a, b) => a - b), mean = s.reduce((a, b) => a + b, 0) / (s.length || 1);
541 return { n: s.length, mean: +mean.toFixed(2), p95: +(s[fl(s.length * 0.95)] || 0).toFixed(2), max: +(s[s.length - 1] || 0).toFixed(2) };
542}
543function report() {
544 const out = { bake: Object.fromEntries(Object.entries(bake).map(([k, v]) => [k, typeof v === "number" ? +v.toFixed(1) : v])), stages: {} };
545 for (const [name, a, b] of STAGES) {
546 const fs = frames.filter(([t]) => t >= a && t < b);
547 out.stages[name] = { interval_ms: stats(fs.map((f) => f[1])), render_ms: stats(fs.map((f) => f[2])) };
548 }
549 return out;
550}
551if (REDUCED && FREEZE === null) document.getElementById("stage").animate([{ opacity: 0 }, { opacity: 1 }],
552 { duration: 900, easing: "cubic-bezier(0.3333, 0, 0.6667, 1)", fill: "both" });
553</script>
554</body>
555</html>