Sydney
--:--
33.86° S · 151.21° E
Source
Kart
Jelly
Source
Canvas
LinkedIn
← Back to home
Source
A 30-second film made only of this page's own source code.
Play
Sound off
Replay
Play live
View the code
source.html ·
Download
Close
= 0 && s.charCodeAt(p) === 32) p--; while (q < n && s.charCodeAt(q) === 32) q++; if (KEYWORDS.has(word)) k = K.KW; else if (s.charCodeAt(q) === 40) k = K.FN; else if (s.charCodeAt(p) === 46) k = K.PROP; else if (word.length > 1 && word === word.toUpperCase() && !isDigit(word.charCodeAt(0))) k = K.CONST; fill(i, j, k); i = j; continue; } out[i++] = K.PUN; continue; } if (mode === 1) { // CSS if (s.startsWith(' 0 && !cssValue) { cssValue = true; out[i++] = K.PUN; continue; } if (c === 59) { cssValue = false; out[i++] = K.PUN; continue; } if (c === 34 || c === 39) { const j = s.indexOf(String.fromCharCode(c), i + 1); fill(i, j + 1, K.STR); i = j + 1; continue; } if (depth === 0) { out[i++] = c > 32 && c !== 44 ? K.TAG : K.PUN; continue; } if (!cssValue) { out[i++] = isIdent(c) || c === 45 ? K.PROP : K.PUN; continue; } if (isDigit(c) || c === 35) { let j = i + 1; while (j < n && isIdent(s.charCodeAt(j))) j++; fill(i, j, K.NUM); i = j; continue; } out[i++] = isIdent(c) || c === 45 ? K.ID : K.PUN; continue; } if (s.startsWith('', i); fill(i, j, K.COM); i = j; continue; } if (c === 60 && /[a-zA-Z!/]/.test(s[i + 1] || '')) { // a tag let j = i + 1; while (j < n && /[\w!/-]/.test(s[j])) j++; const name = s.slice(i + 1, j).toLowerCase(); fill(i, j, K.TAG); i = j; while (i < n && s[i] !== '>') { const d = s.charCodeAt(i); if (d === 34 || d === 39) { const e = s.indexOf(s[i], i + 1); fill(i, e + 1, K.STR); i = e + 1; continue; } out[i] = d === 61 ? K.PUN : K.ATTR; i++; } out[i++] = K.TAG; if (name === 'script') mode = 2; else if (name === 'style') { mode = 1; depth = 0; cssValue = false; } continue; } out[i++] = K.ID; // text } return out; } // ---------------------------------------------------- the editor page // The page is laid out like an editor: 80 columns, soft-wrapped, with a // six-column gutter. Units are ems: a glyph is 1 tall, 0.6 wide. const CW = 0.6, LH = 1.32, WRAP = 80, GUT = 6; const VIEW_W = (WRAP + GUT) * CW; // 51.6 const VIEW_H = VIEW_W * 5 / 4; // 64.5 (the 4:5 frame) const CODE_ROWS = 45; // rows visible in frame one const FOV0 = 30; const CAM0 = VIEW_H / 2 / Math.tan(FOV0 * Math.PI / 360); const ROW_Y0 = VIEW_H / 2 - 2.55 * LH; // first code row, below the tab bar const colX = c => -VIEW_W / 2 + (GUT + c + 0.5) * CW; function layoutSource(text, maxGlyphs) { const cls = classify(text); const all = { code: [], row: [], col: [], cls: [], src: [], line: [] }; const lineRow = []; // first visual row of each line let row = 0, col = 0, line = 1; lineRow[1] = 0; for (let i = 0; i < text.length; i++) { const ch = text.charCodeAt(i); if (ch === 10) { row++; col = 0; line++; lineRow[line] = row; continue; } if (ch === 13) continue; if (col >= WRAP) { row++; col = 0; } if (ch === 9) { col += 2; continue; } if (ch !== 32) { all.code.push(ch >= 33 && ch <= 126 ? ch - 33 : 30); // non-ASCII draws as '?' all.row.push(row); all.col.push(col); all.cls.push(cls[i]); all.src.push(i); all.line.push(line); } col++; } // The film is made of its own script, from line 1: "// Find the source". const top = text.indexOf('// Find the source'); const off = Math.max(0, all.src.findIndex(s => s >= top)); const total = all.code.length, N = Math.min(maxGlyphs, total - off); const pick = (a, T) => T.from(a.slice(off, off + N)); const runAt = text.indexOf('\nrun()', top) + 1; const G = { N, text, rows: row + 1, lineRow, code: pick(all.code, Uint8Array), row: pick(all.row, Int32Array), col: pick(all.col, Int16Array), cls: pick(all.cls, Uint8Array), src: pick(all.src, Int32Array), line: pick(all.line, Int32Array), }; G.runGlyph = G.src.findIndex(s => s === runAt); G.runRow = G.row[G.runGlyph]; G.runLine = G.line[G.runGlyph]; G.row0 = G.row[0]; // the frame shows rows row0 .. row0+44 G.line0 = G.line[0]; // the script's line 1 G.off = i => G.row[i] >= G.row0 + CODE_ROWS; // below frame one G.hx = new Float32Array(N); G.hy = new Float32Array(N); for (let i = 0; i < N; i++) { G.hx[i] = colX(G.col[i]); G.hy[i] = ROW_Y0 - (G.row[i] - G.row0) * LH; } // find a glyph by the text it starts, e.g. G.mark('function makeOrca') G.mark = str => { const at = text.lastIndexOf(str); const g = G.src.findIndex(s => s >= at); return g < 0 ? 0 : g; }; return G; } // ================================================================= ATLAS // Every printable ASCII character in four weights of JetBrains Mono, // stored as a signed distance field so glyphs stay crisp at any size // and defocus smoothly. Built once, on the fly, from the web font. const ATLAS = { first: 33, count: 94, cols: 16, rows: 6, cell: 64, px: 44, radius: 12, weights: [100, 300, 500, 800], font: '"JetBrains Mono", ui-monospace, monospace' }; async function loadFont() { const tries = ATLAS.weights.map(w => document.fonts.load(`${w} ${ATLAS.px}px "JetBrains Mono"`, 'Sx{')); await Promise.race([Promise.all(tries), new Promise(r => setTimeout(r, 4000))]); } function buildAtlas() { const A = ATLAS, W = A.cols * A.cell, HL = A.rows * A.cell, H = HL * A.weights.length; const cv = document.createElement('canvas'); cv.width = W; cv.height = H; const g = cv.getContext('2d', { willReadFrequently: true }); g.fillStyle = '#fff'; g.textAlign = 'center'; g.textBaseline = 'middle'; A.weights.forEach((w, L) => { g.font = `${w} ${A.px}px ${A.font}`; for (let k = 0; k < A.count; k++) { const x = (k % A.cols + 0.5) * A.cell; const y = L * HL + (Math.floor(k / A.cols) + 0.5) * A.cell + A.px * 0.04; g.fillText(String.fromCharCode(A.first + k), x, y); } }); return { W, H, data: distanceField(g.getImageData(0, 0, W, H).data, W, H, A.radius) }; } // Anti-aliased alpha to signed distance (after Mapbox's TinySDF). function distanceField(rgba, w, h, radius) { const n = w * h, INF = 1e20; const outer = new Float64Array(n), inner = new Float64Array(n); for (let i = 0; i < n; i++) { const a = rgba[i * 4 + 3] / 255; if (a === 0) { outer[i] = INF; inner[i] = 0; continue; } if (a === 1) { outer[i] = 0; inner[i] = INF; continue; } const d = 0.5 - a; outer[i] = d > 0 ? d * d : 0; inner[i] = d < 0 ? d * d : 0; } const m = Math.max(w, h); const f = new Float64Array(m), z = new Float64Array(m + 1), v = new Uint16Array(m); edt(outer, w, h, f, v, z); edt(inner, w, h, f, v, z); const out = new Uint8Array(n); for (let i = 0; i < n; i++) { const d = Math.sqrt(outer[i]) - Math.sqrt(inner[i]); out[i] = clamp(Math.round(255 - 255 * (d / radius + 0.5)), 0, 255); } return out; } function edt(g, w, h, f, v, z) { for (let x = 0; x < w; x++) edt1(g, x, w, h, f, v, z); for (let y = 0; y < h; y++) edt1(g, y * w, 1, w, f, v, z); } function edt1(g, off, stride, len, f, v, z) { v[0] = 0; z[0] = -1e20; z[1] = 1e20; f[0] = g[off]; for (let q = 1, k = 0, s = 0; q < len; q++) { f[q] = g[off + q * stride]; const q2 = q * q; do { const r = v[k]; s = (f[q] - f[r] + q2 - r * r) / (q - r) / 2; } while (s <= z[k] && --k > -1); k++; v[k] = q; z[k] = s; z[k + 1] = 1e20; } for (let q = 0, k = 0; q < len; q++) { while (z[k + 1] < q) k++; const r = v[k], qr = q - r; g[off + q * stride] = f[r] + qr * qr; } } // =============================================================== SHADERS // One instanced quad per glyph. Each instance carries its position and // size, colour, weight, which character, and how it is oriented: lying // in a plane (yaw, pitch, roll) or turned to face the camera (bill). const GLYPH_VS = /* glsl */` precision highp float; in vec3 position; in vec4 iPos; // x y z size in vec4 iCol; // linear rgb, opacity in vec4 iA; // weight, glyph, roll, billboard in vec4 iB; // yaw, pitch uniform mat4 viewMatrix, projectionMatrix; uniform vec3 cameraPosition; uniform float uPx, uFocus, uAperture, uNear, uFogStart, uFogDen; uniform vec3 uFog; uniform float uMirror, uMirrorAmt, uSeaY, uTime; out vec2 vUv; flat out vec2 vCell; out vec4 vCol; out float vW; out float vSoft; const float QUAD = 64.0 / 44.0; void main() { vec3 C = iPos.xyz; float s = iPos.w; vec4 col = iCol; if (uMirror > 0.5) { // reflection in the water float h = C.y - uSeaY; if (h < 0.02 || col.a <= 0.0) { gl_Position = vec4(2.0, 2.0, 2.0, 1.0); return; } float r = sin(C.x * 0.9 + uTime * 2.7) + sin(C.z * 1.3 - uTime * 2.1); C.y = uSeaY - h; C.x += r * 0.05 * sqrt(h); col.a *= uMirrorAmt * exp(-h * 0.035); s *= 1.0 + 0.25 * clamp(h * 0.1, 0.0, 1.0); } float yaw = iB.x, pitch = iB.y, roll = iA.z, bill = iA.w; vec3 N0 = vec3(sin(yaw), 0.0, cos(yaw)); vec3 pR = vec3(cos(yaw), 0.0, -sin(yaw)); vec3 pU = vec3(0.0, cos(pitch), 0.0) - N0 * sin(pitch); vec3 cR = vec3(viewMatrix[0][0], viewMatrix[1][0], viewMatrix[2][0]); vec3 cU = vec3(viewMatrix[0][1], viewMatrix[1][1], viewMatrix[2][1]); vec3 R = normalize(mix(pR, cR, bill)), U = normalize(mix(pU, cU, bill)); if (dot(cross(R, U), cameraPosition - C) < 0.0) R = -R; // seen from behind: still reads float cr = cos(roll), sr = sin(roll); vec3 R2 = R * cr + U * sr, U2 = U * cr - R * sr; vec4 vp = viewMatrix * vec4(C, 1.0); float z = max(-vp.z, 0.001); // depth of field: circle of confusion in pixels float glyphPx = s * uPx / z, quadPx = max(glyphPx * QUAD, 0.25); float coc = min(uAperture * abs(z - uFocus) / z * uPx / uFocus, 28.0); float grow = 1.0 + min(coc / quadPx, 0.8); vSoft = min(coc * 64.0 / (quadPx * 48.0), 0.48); col.a /= grow * grow * (1.0 + coc * 0.12); col.a *= mix(0.25, 1.0, smoothstep(0.7, 2.6, glyphPx)); // sub-pixel glyphs dim float fog = exp(-max(z - uFogStart, 0.0) * uFogDen); // atmosphere col.rgb = mix(uFog, col.rgb, fog); col.a *= mix(0.2, 1.0, fog); col.a *= smoothstep(uNear, uNear * 3.0, z); // nothing smears the lens vec3 world = C + (R2 * position.x + U2 * position.y) * s * QUAD * grow; gl_Position = projectionMatrix * viewMatrix * vec4(world, 1.0); float g = iA.y; vCell = vec2(mod(g, 16.0), floor(g / 16.0)); vUv = vCell + vec2(0.5 + position.x * grow, 0.5 - position.y * grow); vW = iA.x; vCol = col; }`; const GLYPH_FS = /* glsl */` precision highp float; uniform sampler2D uAtlas; in vec2 vUv; flat in vec2 vCell; in vec4 vCol; in float vW; in float vSoft; out vec4 frag; float field(vec2 uv, float layer) { return texture(uAtlas, vec2(uv.x / 16.0, (uv.y + layer * 6.0) / 24.0)).r; } void main() { vec2 loc = vUv - vCell; if (loc.x < 0.0 || loc.y < 0.0 || loc.x > 1.0 || loc.y > 1.0) discard; float w = clamp(vW, 0.0, 1.0) * 3.0; float L = min(floor(w), 2.0); float d = mix(field(vUv, L), field(vUv, L + 1.0), w - L); float edge = 0.5 - max(vW - 1.0, 0.0) * 0.3; // beyond 800: heavier still float e = fwidth(d) * 0.75 + vSoft; float a = smoothstep(edge - e, edge + e, d); // far out of focus, a glyph becomes a soft round disc of light (bokeh) float disc = 1.0 - smoothstep(0.16, 0.5, length(loc - 0.5)); a = mix(a, disc * 0.45, smoothstep(0.12, 0.42, vSoft)); if (a < 0.002) discard; frag = vec4(vCol.rgb * (vCol.a * a), 0.0); }`; // Final grade: night sky behind the light, soft shoulder, vignette, dither. const GRADE_FS = /* glsl */` uniform sampler2D tDiffuse; uniform vec3 uBgTop, uBgBot; uniform float uVig, uExposure, uSeed; varying vec2 vUv; float shoulder(float x) { return x < 0.75 ? x : 0.75 + 0.25 * (1.0 - exp(-(x - 0.75) / 0.25)); } vec3 toSRGB(vec3 c) { return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, step(0.0031308, c)); } void main() { vec3 c = texture2D(tDiffuse, vUv).rgb * uExposure; c += mix(uBgBot, uBgTop, smoothstep(0.0, 1.0, vUv.y)); c = vec3(shoulder(c.r), shoulder(c.g), shoulder(c.b)); vec2 q = vUv - 0.5; c *= mix(1.0, smoothstep(0.95, 0.25, length(q * vec2(1.0, 0.9))), uVig); c = toSRGB(clamp(c, 0.0, 1.0)); float n = fract(sin(dot(gl_FragCoord.xy + uSeed, vec2(12.9898, 78.233))) * 43758.5453); gl_FragColor = vec4(c + (n - 0.5) / 255.0, 1.0); }`; // ================================================================ SHAPES // Silhouettes are 2-D signed distance fields made of capsules, ellipses // and polygons, hung on a skeleton so they can swim, gallop and leap. function sdCapsule(px, py, ax, ay, bx, by, ra, rb) { const dx = bx - ax, dy = by - ay, h = Math.hypot(dx, dy) || 1e-6; const ux = dx / h, uy = dy / h; const qx = Math.abs((px - ax) * uy - (py - ay) * ux); const qy = (px - ax) * ux + (py - ay) * uy; const b = (ra - rb) / h, a = Math.sqrt(Math.max(1 - b * b, 0)); const k = -qx * b + qy * a; if (k < 0) return Math.hypot(qx, qy) - ra; if (k > a * h) return Math.hypot(qx, qy - h) - rb; return qx * a + qy * b - ra; } function sdEllipse(px, py, cx, cy, rx, ry, rot = 0) { let x = px - cx, y = py - cy; if (rot) { const c = Math.cos(rot), s = Math.sin(rot); [x, y] = [x * c + y * s, -x * s + y * c]; } const k0 = Math.hypot(x / rx, y / ry), k1 = Math.hypot(x / (rx * rx), y / (ry * ry)); return k1 < 1e-9 ? -Math.min(rx, ry) : k0 * (k0 - 1) / k1; } function sdPoly(px, py, v) { const n = v.length / 2; let d = (px - v[0]) ** 2 + (py - v[1]) ** 2, s = 1; for (let i = 0, j = n - 1; i < n; j = i, i++) { const ex = v[2 * j] - v[2 * i], ey = v[2 * j + 1] - v[2 * i + 1]; const wx = px - v[2 * i], wy = py - v[2 * i + 1]; const t = clamp((wx * ex + wy * ey) / (ex * ex + ey * ey), 0, 1); d = Math.min(d, (wx - ex * t) ** 2 + (wy - ey * t) ** 2); const c1 = py >= v[2 * i + 1], c2 = py < v[2 * j + 1], c3 = ex * wy > ey * wx; if ((c1 && c2 && c3) || (!c1 && !c2 && !c3)) s = -s; } return s * Math.sqrt(d); } const smin = (a, b, k) => { const h = sat(0.5 + 0.5 * (b - a) / k); return mix(b, a, h) - k * h * (1 - h); }; // A skeleton of bones, each turning about its pivot, carrying shapes. class Rig { constructor({ bones, parts, k = 0.012 }) { this.k = k; this.bones = bones; this.at = Object.fromEntries(bones.map((b, i) => [b.name, i])); this.parts = parts.map(p => ({ ...p, b: this.at[p.bone] })); this.M = new Float64Array(bones.length * 6); this.pose({}); } part(p, x, y) { const a = p.a; if (p.kind === 'cap') return sdCapsule(x, y, a[0], a[1], a[2], a[3], a[4], a[5]); if (p.kind === 'ell') return sdEllipse(x, y, a[0], a[1], a[2], a[3], a[4] || 0); return sdPoly(x, y, a); } sdf(x, y) { let d = 1e9; for (const p of this.parts) d = smin(d, this.part(p, x, y), this.k); return d; } bounds() { let x0 = 1e9, y0 = 1e9, x1 = -1e9, y1 = -1e9; const add = (x, y, r) => { x0 = Math.min(x0, x - r); x1 = Math.max(x1, x + r); y0 = Math.min(y0, y - r); y1 = Math.max(y1, y + r); }; for (const { kind, a } of this.parts) { if (kind === 'cap') { add(a[0], a[1], a[4]); add(a[2], a[3], a[5]); } else if (kind === 'ell') add(a[0], a[1], Math.max(a[2], a[3])); else for (let i = 0; i < a.length; i += 2) add(a[i], a[i + 1], 0); } const m = 0.03; return [x0 - m, y0 - m, x1 + m, y1 + m]; } // angles: { boneName: radians } - each bone turns about its own pivot pose(angles) { const M = this.M; this.bones.forEach((b, i) => { const a = angles[b.name] || 0, c = Math.cos(a), s = Math.sin(a); const [px, py] = b.pivot; const L = [c, s, -s, c, px - c * px + s * py, py - s * px - c * py]; const o = i * 6; if (b.parent == null) { M.set(L, o); return; } const p = this.at[b.parent] * 6; M[o] = M[p] * L[0] + M[p + 2] * L[1]; M[o + 1] = M[p + 1] * L[0] + M[p + 3] * L[1]; M[o + 2] = M[p] * L[2] + M[p + 2] * L[3]; M[o + 3] = M[p + 1] * L[2] + M[p + 3] * L[3]; M[o + 4] = M[p] * L[4] + M[p + 2] * L[5] + M[p + 4]; M[o + 5] = M[p + 1] * L[4] + M[p + 3] * L[5] + M[p + 5]; }); } } // a jointed limb: segments of [length, angle from straight down, r0, r1] function limb(spec, name, parent, x, y, segs) { let par = parent; segs.forEach(([len, ang, r0, r1], k) => { const bx = x + Math.sin(ang) * len, by = y - Math.cos(ang) * len, id = name + k; spec.bones.push({ name: id, parent: par, pivot: [x, y], rest: ang }); spec.parts.push({ bone: id, kind: 'cap', a: [x, y, bx, by, r0, r1] }); par = id; x = bx; y = by; }); } // absolute limb angles -> bone rotations relative to the rest pose function limbPose(out, spec, name, abs) { let prev = 0; abs.forEach((a, k) => { const rest = spec.bones[spec.at[name + k]].rest; out[name + k] = a - rest - prev; prev = a - rest; }); } // Fill a silhouette with a monospace grid in reading order, sized so it // holds exactly `count` glyphs. Rows run along the body like lines in an // editor, so a run of consecutive source code reads across the animal. function fillShape(sdf, box, count, aspect = 0.6) { const [x0, y0, x1, y1] = box, RES = 384; const gw = (x1 - x0) / RES, gh = (y1 - y0) / RES, raster = new Float32Array((RES + 1) ** 2); for (let j = 0; j <= RES; j++) for (let i = 0; i <= RES; i++) raster[j * (RES + 1) + i] = sdf(x0 + i * gw, y0 + j * gh); const look = (x, y) => { const fx = clamp((x - x0) / gw, 0, RES - 1e-3), fy = clamp((y - y0) / gh, 0, RES - 1e-3); const i = Math.floor(fx), j = Math.floor(fy), u = fx - i, v = fy - j, o = j * (RES + 1) + i; return mix(mix(raster[o], raster[o + 1], u), mix(raster[o + RES + 1], raster[o + RES + 2], u), v); }; const cells = (h, keep) => { const w = h * aspect, nx = Math.floor((x1 - x0) / w), ny = Math.floor((y1 - y0) / h); const ox = x0 + ((x1 - x0) - nx * w + w) / 2, oy = y1 - ((y1 - y0) - ny * h + h) / 2; let n = 0; for (let r = 0; r < ny; r++) for (let c = 0; c < nx; c++) { const x = ox + c * w, y = oy - r * h, d = look(x, y); if (d < 0) { if (keep) keep(x, y, -d); n++; } } return n; }; let lo = 1e-4, hi = Math.max(x1 - x0, y1 - y0); for (let it = 0; it < 48; it++) { const m = Math.sqrt(lo * hi); if (cells(m) >= count) lo = m; else hi = m; } const xs = [], ys = [], ds = []; cells(lo, (x, y, d) => { xs.push(x); ys.push(y); ds.push(d); }); const drop = new Set(), extra = xs.length - count; for (let k = 0; k < extra; k++) drop.add(Math.floor((k + 0.5) * xs.length / extra)); const F = { n: count, h: lo, x: new Float32Array(count), y: new Float32Array(count), d: new Float32Array(count), dmax: 0 }; for (let s = 0, k = 0; s < xs.length && k < count; s++) { if (drop.has(s)) continue; F.x[k] = xs[s]; F.y[k] = ys[s]; F.d[k] = ds[s]; F.dmax = Math.max(F.dmax, ds[s]); k++; } return F; } // Fill a rig and bind every cell to its two nearest bones for skinning. function fillRig(rig, count) { const F = fillShape((x, y) => rig.sdf(x, y), rig.bounds(), count); F.b0 = new Uint8Array(F.n); F.b1 = new Uint8Array(F.n); F.w = new Float32Array(F.n); for (let j = 0; j < F.n; j++) { let d0 = 1e9, d1 = 1e9, b0 = 0, b1 = 0; for (const p of rig.parts) { const d = rig.part(p, F.x[j], F.y[j]); if (d < d0) { if (p.b !== b0) { d1 = d0; b1 = b0; } d0 = d; b0 = p.b; } else if (d < d1 && p.b !== b0) { d1 = d; b1 = p.b; } } F.b0[j] = b0; F.b1[j] = b1; F.w[j] = 1 / (1 + Math.exp((d0 - d1) / 0.012)); } return F; } // skinned position of cell j for the rig's current pose -> SK.x, SK.y const SK = { x: 0, y: 0 }; function skin(rig, F, j) { const M = rig.M, a = F.b0[j] * 6, b = F.b1[j] * 6, w = F.w[j], x = F.x[j], y = F.y[j]; SK.x = w * (M[a] * x + M[a + 2] * y + M[a + 4]) + (1 - w) * (M[b] * x + M[b + 2] * y + M[b + 4]); SK.y = w * (M[a + 1] * x + M[a + 3] * y + M[a + 5]) + (1 - w) * (M[b + 1] * x + M[b + 3] * y + M[b + 5]); return SK; } // The orientation of a plane in the world: yaw about Y, then pitch. function basis(yaw, pitch = 0) { const sy = Math.sin(yaw), cy = Math.cos(yaw), sp = Math.sin(pitch), cp = Math.cos(pitch); return { yaw, pitch, R: [cy, 0, -sy], U: [-sy * sp, cp, -cy * sp], N: [sy * cp, sp, cy * cp] }; } function put(o, B, P, x, y, z) { o.x = P[0] + B.R[0] * x + B.U[0] * y + B.N[0] * z; o.y = P[1] + B.R[1] * x + B.U[1] * y + B.N[1] * z; o.z = P[2] + B.R[2] * x + B.U[2] * y + B.N[2] * z; o.yaw = B.yaw; o.pitch = B.pitch; } const tint = (o, c, k = 1) => { o.r = c[0] * k; o.g = c[1] * k; o.b = c[2] * k; }; const tint2 = (o, c1, c2, t, k = 1) => { o.r = mix(c1[0], c2[0], t) * k; o.g = mix(c1[1], c2[1], t) * k; o.b = mix(c1[2], c2[2], t) * k; }; // A smooth path through timed keys [t, x, y, z] (cubic Hermite). function path(keys) { const n = keys.length; const tan = k => { const a = keys[Math.max(k - 1, 0)], b = keys[Math.min(k + 1, n - 1)]; return [1, 2, 3].map(c => (b[c] - a[c]) / (b[0] - a[0])); }; const T = keys.map((_, k) => tan(k)); return (t, out = [0, 0, 0], vel) => { let k = 0; if (t <= keys[0][0]) k = 0; else if (t >= keys[n - 1][0]) k = n - 2; else while (keys[k + 1][0] < t) k++; const a = keys[k], b = keys[k + 1], h = b[0] - a[0]; const u = clamp((t - a[0]) / h, 0, 1), u2 = u * u, u3 = u2 * u; const h00 = 2 * u3 - 3 * u2 + 1, h10 = u3 - 2 * u2 + u, h01 = -2 * u3 + 3 * u2, h11 = u3 - u2; for (let c = 0; c < 3; c++) { out[c] = h00 * a[c + 1] + h10 * h * T[k][c] + h01 * b[c + 1] + h11 * h * T[k + 1][c]; if (vel) { const d00 = 6 * u2 - 6 * u, d10 = 3 * u2 - 4 * u + 1, d01 = -6 * u2 + 6 * u, d11 = 3 * u2 - 2 * u; vel[c] = (d00 * a[c + 1] + d01 * b[c + 1]) / h + d10 * T[k][c] + d11 * T[k + 1][c]; } } return out; }; } // Tile a stretch of water with flat glyphs, small near the camera's path // and larger far away, so the surface reads as evenly dense on screen. function waterTiles(x0, z0, x1, z1, count, dist) { const ROOT = 24; const walk = (alpha, emit) => { let n = 0; const rec = (cx, cz, w, h, lvl) => { const d = dist(cx, cz); if (d < 0) return; if (h > alpha * d && lvl < 9) { const w2 = w / 2, h2 = h / 2; rec(cx - w2 / 2, cz - h2 / 2, w2, h2, lvl + 1); rec(cx + w2 / 2, cz - h2 / 2, w2, h2, lvl + 1); rec(cx - w2 / 2, cz + h2 / 2, w2, h2, lvl + 1); rec(cx + w2 / 2, cz + h2 / 2, w2, h2, lvl + 1); return; } n++; if (emit) emit(cx, cz, h, d); }; const w = ROOT * 0.6; for (let z = z0; z < z1; z += ROOT) for (let x = x0; x < x1; x += w) rec(x + w / 2, z + ROOT / 2, w, ROOT, 0); return n; }; let lo = 1e-3, hi = 4; for (let it = 0; it < 40; it++) { const m = Math.sqrt(lo * hi); if (walk(m) >= count) lo = m; else hi = m; } const tiles = []; walk(lo, (x, z, h, d) => tiles.push([x, z, h, d])); tiles.sort((a, b) => a[3] - b[3]); tiles.length = Math.min(tiles.length, count); tiles.sort((a, b) => a[1] - b[1] || a[0] - b[0]); // reading order const T = { n: tiles.length, x: new Float32Array(count), z: new Float32Array(count), s: new Float32Array(count) }; tiles.forEach(([x, z, h], k) => { T.x[k] = x; T.z[k] = z; T.s[k] = h; }); return T; } const segDist = (px, pz, pts) => { let best = 1e9; for (let k = 0; k + 1 < pts.length; k++) { const [ax, az] = pts[k], [bx, bz] = pts[k + 1], ex = bx - ax, ez = bz - az; const t = clamp(((px - ax) * ex + (pz - az) * ez) / (ex * ex + ez * ez), 0, 1); best = Math.min(best, Math.hypot(px - ax - ex * t, pz - az - ez * t)); } return best; }; // Give each role a contiguous run of glyphs starting at `start`, so each // silhouette is filled with consecutive lines of this file. function allot(N, start, sizes) { const role = new Uint8Array(N), idx = new Int32Array(N); let g = ((start % N) + N) % N; sizes.forEach((n, r) => { for (let k = 0; k < n; k++) { role[g] = r; idx[g] = k; g = (g + 1) % N; } }); return { role, idx }; } // =============================================================== ANIMALS // Each animal is drawn in a unit box, nose toward +x. // Orca, from the side: tall dorsal fin, paddle flipper, white patches. function orcaRig() { const spine = [['head', null, 0.62], ['t1', 'head', 0.42], ['t2', 't1', 0.28], ['t3', 't2', 0.15], ['fluke', 't3', 0.05]]; const s = { bones: spine.map(([name, parent, x]) => ({ name, parent, pivot: [x, 0] })), parts: [], k: 0.014 }; s.bones.push({ name: 'pec', parent: 'head', pivot: [0.70, -0.08] }); const P = (bone, kind, ...a) => s.parts.push({ bone, kind, a }); P('head', 'cap', 0.975, -0.02, 0.86, -0.004, 0.045, 0.104); P('head', 'cap', 0.86, -0.004, 0.62, 0.006, 0.104, 0.142); P('head', 'cap', 0.62, 0.006, 0.42, 0.01, 0.142, 0.134); P('head', 'poly', 0.585, 0.11, 0.505, 0.37, 0.488, 0.366, 0.468, 0.25, 0.44, 0.11); P('t1', 'cap', 0.42, 0.01, 0.28, 0.004, 0.134, 0.098); P('t2', 'cap', 0.28, 0.004, 0.15, 0, 0.098, 0.052); P('t3', 'cap', 0.15, 0, 0.05, 0, 0.052, 0.026); P('fluke', 'cap', 0.05, 0, -0.065, 0.06, 0.028, 0.009); P('fluke', 'cap', 0.05, 0, -0.065, -0.06, 0.028, 0.009); P('pec', 'cap', 0.70, -0.08, 0.615, -0.205, 0.046, 0.04); return new Rig(s); } // which part of the orca a rest-pose point is: 0 black, 1 white, 2 saddle, 3 eye function orcaPatch(x, y) { const eye = Math.hypot(x - 0.872, y - 0.012); if (eye < 0.017) return 3; if (sdEllipse(x, y, 0.785, 0.045, 0.072, 0.024, -0.14) < 0) return 1; if (y < -0.035 && x > 0.6 && y < -0.035 - (0.97 - x) * 0.05) return 1; if (sdEllipse(x, y, 0.6, -0.1, 0.24, 0.05, 0.05) < 0 && y < -0.03) return 1; if (sdEllipse(x, y, 0.33, -0.04, 0.09, 0.04, 0.55) < 0) return 1; if (sdEllipse(x, y, 0.43, 0.108, 0.085, 0.026, 0.12) < 0) return 2; return 0; } // Sea turtle, from above: a domed teardrop shell, a beaked head, long // scalloped front flippers that sweep like wings, paddle-shaped rear ones. function turtleRig() { const s = { bones: [{ name: 'root', parent: null, pivot: [0.45, 0] }], parts: [], k: 0.02 }; const P = (bone, kind, ...a) => s.parts.push({ bone, kind, a }); P('root', 'ell', 0.45, 0, 0.29, 0.21); P('root', 'ell', 0.24, 0, 0.16, 0.14); P('root', 'poly', 0.1, -0.06, 0.02, 0, 0.1, 0.06); s.bones.push({ name: 'head', parent: 'root', pivot: [0.72, 0] }); P('head', 'cap', 0.7, 0, 0.84, 0, 0.07, 0.062); P('head', 'cap', 0.84, 0, 0.9, 0, 0.055, 0.03); for (const side of [1, -1]) { const f = side > 0 ? 'fl' : 'fr', r = side > 0 ? 'rl' : 'rr'; s.bones.push({ name: f, parent: 'root', pivot: [0.6, 0.15 * side] }, { name: f + '2', parent: f, pivot: [0.5, 0.42 * side] }, { name: r, parent: 'root', pivot: [0.2, 0.12 * side] }); P(f, 'cap', 0.6, 0.15 * side, 0.5, 0.42 * side, 0.085, 0.07); P(f + '2', 'cap', 0.5, 0.42 * side, 0.36, 0.6 * side, 0.07, 0.04); P(f + '2', 'cap', 0.36, 0.6 * side, 0.18, 0.7 * side, 0.04, 0.012); for (let k = 0; k < 4; k++) { // scalloped trailing edge const u = 0.25 + k * 0.2, x = mix(0.56, 0.24, u), y = mix(0.3, 0.68, u) * side; P(f + '2', 'ell', x - 0.03, y - 0.02 * side, 0.035, 0.03); } P(r, 'cap', 0.2, 0.12 * side, 0.1, 0.24 * side, 0.05, 0.045); P(r, 'ell', 0.08, 0.26 * side, 0.055, 0.04, 0.6 * side); } return new Rig(s); } // shell scutes: distance to the nearest plate seam, 0 on a seam const SCUTES = [[0.66, 0], [0.55, 0], [0.44, 0], [0.33, 0], [0.22, 0], [0.6, 0.12], [0.49, 0.14], [0.38, 0.14], [0.27, 0.12], [0.6, -0.12], [0.49, -0.14], [0.38, -0.14], [0.27, -0.12]]; function seam(x, y) { let d0 = 9, d1 = 9; for (const [sx, sy] of SCUTES) { const d = Math.hypot((x - sx) * 1.1, y - sy); if (d < d0) { d1 = d0; d0 = d; } else if (d < d1) d1 = d; } return d1 - d0; } // Koi, from above: a body that bends, and a fan of tail. function koiRig() { const spine = [['root', null, 0.6], ['s1', 'root', 0.45], ['s2', 's1', 0.3], ['s3', 's2', 0.16], ['tail', 's3', 0.07]]; const s = { bones: spine.map(([name, parent, x]) => ({ name, parent, pivot: [x, 0] })), parts: [], k: 0.012 }; const P = (bone, kind, ...a) => s.parts.push({ bone, kind, a }); P('root', 'cap', 0.98, 0, 0.6, 0, 0.045, 0.1); P('s1', 'cap', 0.6, 0, 0.45, 0, 0.1, 0.094); P('s2', 'cap', 0.45, 0, 0.3, 0, 0.094, 0.068); P('s3', 'cap', 0.3, 0, 0.16, 0, 0.068, 0.04); P('tail', 'cap', 0.16, 0, 0.07, 0, 0.04, 0.024); P('tail', 'cap', 0.08, 0, -0.11, 0.1, 0.026, 0.05); P('tail', 'cap', 0.08, 0, -0.11, -0.1, 0.026, 0.05); P('root', 'cap', 0.74, 0.07, 0.63, 0.2, 0.03, 0.042); P('root', 'cap', 0.74, -0.07, 0.63, -0.2, 0.03, 0.042); return new Rig(s); } const side = (sd, pts) => pts.flatMap(([x, y]) => [x * sd, y]); // mirror a half-outline // Manta ray, seen from above: a broad diamond with swept, pointed wings, // curled head fins, a long whip tail and white chevrons on its shoulders. function mantaSpec() { const s = { bones: [{ name: 'root', parent: null, pivot: [0, 0] }], parts: [], k: 0.014 }; const P = (bone, kind, ...a) => s.parts.push({ bone, kind, a }); P('root', 'ell', 0, 0.02, 0.19, 0.3); // body P('root', 'cap', 0.1, 0.28, 0.125, 0.45, 0.042, 0.022); // head fins, curling forward P('root', 'cap', -0.1, 0.28, -0.125, 0.45, 0.042, 0.022); P('root', 'cap', 0, -0.25, 0, -0.95, 0.03, 0.005); // whip tail P('root', 'poly', ...side(1, [[0.12, 0.25], [0.55, 0.13], [1.0, -0.02], [0.62, -0.12], [0.16, -0.22]])); P('root', 'poly', ...side(-1, [[0.12, 0.25], [0.55, 0.13], [1.0, -0.02], [0.62, -0.12], [0.16, -0.22]])); return s; } // seen from below: 0 white belly, 1 gill slit, 2 head fins and mouth, 3 dark margin (set by edge) function mantaPatch(x, y) { const ax = Math.abs(x); if (y > 0.26 && ax < 0.16) return 2; for (let k = 0; k < 5; k++) if (Math.abs(y - (0.16 - k * 0.05)) < 0.009 && ax > 0.09 && ax < 0.15) return 1; return 0; } // Giraffe, from the side, walking: a short deep body sloping steeply from // high withers, a long tapering neck with a dark mane, tufted ossicones, // slender legs with knobbly knees and dark hooves, a long tufted tail. function giraffeSpec() { const s = { bones: [{ name: 'root', parent: null, pivot: [0.42, 0.6] }], parts: [], k: 0.014 }; const P = (bone, kind, ...a) => s.parts.push({ bone, kind, a }); P('root', 'ell', 0.41, 0.61, 0.165, 0.092, 0.3); // body, sloping down to the rump P('root', 'ell', 0.52, 0.665, 0.095, 0.1); // withers P('root', 'ell', 0.535, 0.58, 0.072, 0.075); // deep chest P('root', 'ell', 0.29, 0.565, 0.088, 0.085); // rounded hindquarters s.bones.push({ name: 'neck', parent: 'root', pivot: [0.56, 0.67] }, { name: 'head', parent: 'neck', pivot: [0.715, 0.95] }, { name: 'tail', parent: 'root', pivot: [0.22, 0.6] }); P('neck', 'cap', 0.55, 0.65, 0.715, 0.95, 0.054, 0.021); P('head', 'cap', 0.705, 0.958, 0.805, 0.915, 0.031, 0.016); P('head', 'cap', 0.702, 0.975, 0.697, 1.015, 0.007, 0.011); // ossicones, tufted P('head', 'cap', 0.716, 0.975, 0.718, 1.012, 0.007, 0.011); P('head', 'cap', 0.725, 0.965, 0.762, 0.978, 0.012, 0.005); // ear P('tail', 'cap', 0.215, 0.6, 0.19, 0.36, 0.012, 0.009); P('tail', 'ell', 0.19, 0.335, 0.017, 0.036); limb(s, 'fn', 'root', 0.545, 0.55, [[0.27, 0.06, 0.028, 0.02], [0.26, 0.0, 0.02, 0.014], [0.03, 0.2, 0.018, 0.021]]); limb(s, 'ff', 'root', 0.505, 0.55, [[0.27, -0.1, 0.027, 0.019], [0.26, -0.05, 0.019, 0.013], [0.03, 0.1, 0.017, 0.02]]); limb(s, 'hn', 'root', 0.3, 0.53, [[0.24, 0.12, 0.044, 0.021], [0.27, -0.12, 0.02, 0.014], [0.03, 0.2, 0.018, 0.021]]); limb(s, 'hf', 'root', 0.26, 0.53, [[0.24, -0.05, 0.042, 0.02], [0.27, -0.25, 0.019, 0.013], [0.03, 0.1, 0.017, 0.02]]); return s; } // the reticulated coat: brown patches parted by narrow cream lines function giraffeSeam(x, y, c = 0.036) { let d0 = 9, d1 = 9; const cx = Math.floor(x / c), cy = Math.floor(y / c); for (let i = -1; i <= 1; i++) for (let j = -1; j <= 1; j++) { const gx = cx + i, gy = cy + j, k = gx * 131 + gy * 977; const px = (gx + 0.2 + 0.6 * rnd(k, 1)) * c, py = (gy + 0.2 + 0.6 * rnd(k, 2)) * c; const d = Math.hypot(x - px, y - py); if (d < d0) { d1 = d0; d0 = d; } else if (d < d1) d1 = d; } return d1 - d0; } // Kangaroo, from the side, hopping: upright, long ears, little forearms, // huge hind legs and feet, and a thick tail that balances it. function rooSpec() { const s = { bones: [{ name: 'root', parent: null, pivot: [0.4, 0.4] }], parts: [], k: 0.018 }; const P = (bone, kind, ...a) => s.parts.push({ bone, kind, a }); P('root', 'ell', 0.45, 0.55, 0.13, 0.22, -0.45); // body, leaning forward P('root', 'ell', 0.37, 0.36, 0.13, 0.11, 0.2); // haunch P('root', 'cap', 0.53, 0.66, 0.59, 0.82, 0.07, 0.045); // neck s.bones.push({ name: 'head', parent: 'root', pivot: [0.6, 0.83] }); P('head', 'ell', 0.63, 0.87, 0.065, 0.052, 0.2); P('head', 'cap', 0.66, 0.86, 0.74, 0.82, 0.04, 0.022); // snout P('head', 'cap', 0.6, 0.91, 0.575, 1.03, 0.022, 0.008); // ears P('head', 'cap', 0.62, 0.92, 0.615, 1.035, 0.02, 0.008); s.bones.push({ name: 'arm', parent: 'root', pivot: [0.56, 0.64] }); P('arm', 'cap', 0.56, 0.64, 0.63, 0.53, 0.024, 0.016); s.bones.push({ name: 'tail0', parent: 'root', pivot: [0.3, 0.36] }, { name: 'tail1', parent: 'tail0', pivot: [0.12, 0.13] }); P('tail0', 'cap', 0.3, 0.36, 0.12, 0.13, 0.065, 0.042); P('tail1', 'cap', 0.12, 0.13, -0.12, 0.03, 0.042, 0.016); limb(s, 'hn', 'root', 0.38, 0.38, [[0.206, 0.5, 0.07, 0.045], [0.228, -0.91, 0.036, 0.026], [0.2, 1.37, 0.026, 0.018]]); limb(s, 'hf', 'root', 0.34, 0.38, [[0.206, 0.62, 0.064, 0.042], [0.228, -0.78, 0.033, 0.024], [0.2, 1.45, 0.024, 0.016]]); return s; } const isTusk = (x, y) => x > 0.86 && y > 0.49 && y < 0.6; // Tiger, from the side, drawn mid-leap: forelegs reaching, hind legs // thrown back, tail streaming. function tigerSpec() { const s = { bones: [{ name: 'root', parent: null, pivot: [0.5, 0.5] }], parts: [], k: 0.02 }; const P = (bone, kind, ...a) => s.parts.push({ bone, kind, a }); P('root', 'cap', 0.25, 0.5, 0.73, 0.53, 0.122, 0.138); P('root', 'ell', 0.71, 0.47, 0.12, 0.125); P('root', 'ell', 0.27, 0.5, 0.12, 0.13); s.bones.push({ name: 'head', parent: 'root', pivot: [0.82, 0.57] }); P('head', 'ell', 0.925, 0.595, 0.092, 0.084); P('head', 'cap', 0.95, 0.57, 1.03, 0.548, 0.05, 0.038); P('head', 'cap', 0.885, 0.665, 0.875, 0.712, 0.026, 0.016); P('head', 'cap', 0.95, 0.672, 0.958, 0.716, 0.024, 0.015); const tail = [[0.13, 0.55], [-0.02, 0.5], [-0.16, 0.52], [-0.27, 0.6], [-0.33, 0.7]]; for (let k = 0; k < 4; k++) { s.bones.push({ name: 'tail' + k, parent: k ? 'tail' + (k - 1) : 'root', pivot: tail[k] }); P('tail' + k, 'cap', ...tail[k], ...tail[k + 1], 0.034 - k * 0.004, 0.03 - k * 0.004); } limb(s, 'fn', 'root', 0.75, 0.43, [[0.2, 1.1, 0.062, 0.042], [0.18, 1.32, 0.04, 0.032], [0.06, 1.5, 0.036, 0.03]]); limb(s, 'ff', 'root', 0.69, 0.43, [[0.2, 0.72, 0.058, 0.04], [0.18, 1.0, 0.038, 0.03], [0.06, 1.2, 0.034, 0.028]]); limb(s, 'hn', 'root', 0.27, 0.43, [[0.22, -1.0, 0.085, 0.046], [0.2, -1.3, 0.04, 0.03], [0.07, -1.48, 0.032, 0.028]]); limb(s, 'hf', 'root', 0.33, 0.43, [[0.22, -0.62, 0.08, 0.044], [0.2, -0.98, 0.038, 0.028], [0.07, -1.2, 0.03, 0.026]]); return s; } // =============================================================== SCENES // A scene is { frame(t), glyph(i, t, o) }: frame() poses anything shared // for time t, glyph() writes glyph i's state into o. Same t, same frame. const SEA_Y = -60; // the ocean and the harbour surface const EYE_COL = hex(0xFFD466); // ------------------------------------------------------ 1. EDITOR // The page as it sits in the editor. The caret types run(), Enter lands // on beat 2, and every character lifts off in a ripple from the caret. const TYPE_AT = [0.5, 0.625, 0.75, 0.875, 1.0]; // r u n ( ), in sixteenths const ENTER = 1.5; // beat 3 const UNTYPE_AT = [29.84, 29.8, 29.76, 29.72, 29.68]; function makeEditor(G) { const caretX = colX(G.col[G.runGlyph]) - CW / 2, caretY = G.hy[G.runGlyph]; const ripple = new Float32Array(G.N); for (let i = 0; i < G.N; i++) { const d = Math.hypot(G.hx[i] - caretX, (G.hy[i] - caretY) * 1.1); ripple[i] = ENTER + 0.04 + d / 45; } return { caretX, caretY, ripple, frame() {}, glyph(i, t, o) { const c = G.cls[i]; o.x = G.hx[i]; o.y = G.hy[i]; o.z = 0; o.s = 1; tint(o, SYNTAX[c]); o.a = 1; o.w = WEIGHT[c]; o.roll = 0; o.bill = 0; o.yaw = 0; o.pitch = 0; if (G.off(i)) o.a = 0; // below frame one const k = i - G.runGlyph; if (k >= 0 && k < 5) { o.s = t < 15 ? outBack(ramp(t, TYPE_AT[k], TYPE_AT[k] + 0.09)) : 1 - ramp(t, UNTYPE_AT[k], UNTYPE_AT[k] + 0.05); } if (t > ENTER && t < 15) { // the lift-off ripple const r = ripple[i], u = ramp(t, r, r + 0.9), crest = bump(t, r + 0.05, 0.07); o.z += 9 * outCubic(u) + 1.5 * crest; o.y += 1.4 * u; o.roll += (rnd(i, 3) - 0.5) * 0.9 * u; o.w = mix(o.w, 1.15, crest); const k2 = 1 + 2.2 * crest + 0.3 * u; o.r *= k2; o.g *= k2; o.b *= k2; } }, }; } // ------------------------------------------------------ 2. DEEP // The characters rain into an ocean of rolling code. Sunbeams are glyphs // lit bold. Turtles glide through the light; an orca cuts through a bait // ball, the camera looks into its eye, and it breaches on beat 10. const DEEP = { c: [4, -100, -16], ball: [8, -103, -14], ballR: 11.5 }; const BEAMS = [[-34, -58, 5], [-8, -30, 7], [16, -74, 4.5], [40, -26, 6], [-52, -100, 8], [62, -80, 5]]; const BEAM_DIR = (() => { const v = [0.24, -1, 0.1], l = Math.hypot(...v); return v.map(x => x / l); })(); function beams(x, y, z, t) { let I = 0; for (let k = 0; k < BEAMS.length; k++) { const [bx, bz, w] = BEAMS[k]; const ox = bx + 5 * Math.sin(0.5 * t + k * 1.7); const vx = x - ox, vy = y - SEA_Y, vz = z - bz; const along = vx * BEAM_DIR[0] + vy * BEAM_DIR[1] + vz * BEAM_DIR[2]; if (along < 0) continue; const perp2 = vx * vx + vy * vy + vz * vz - along * along; I += Math.exp(-perp2 / (w * w)) * Math.exp(-along / 110) * (0.75 + 0.25 * Math.sin(t * 2.1 + k)); } return I; } const ORCA_PATH = path([[2.5, -110, -132, -58], [3.9, -66, -120, -36], [5.1, -26, -110, -20], [6.0, 4, -104, -12], [6.8, 26, -96, -6], [7.5, 40, -84, -3], [8.2, 50, -68, -1], [8.8, 56, -50, 0], [9.7, 62, -26, 1]]); const ORCA_L = 40; const TURTLES = [ { path: path([[1.7, -30, -105, 32], [4.0, -6, -100, 36], [6.4, 30, -94, 30], [7.6, 60, -90, 20]]), L: 15, flip: false, ph: 0 }, { path: path([[1.7, 26, -114, 0], [4.5, 2, -109, 4], [6.6, -30, -106, 2], [7.8, -60, -104, 0]]), L: 10, flip: true, ph: 0.4 }, ]; function makeDeep(G) { const N = G.N, orca = orcaRig(), turtle = turtleRig(); const OF = fillRig(orca, Math.round(N * 0.17)), TF = fillRig(turtle, Math.round(N * 0.05)); const patch = Uint8Array.from(OF.x, (x, j) => orcaPatch(x, OF.y[j])); const shell = Float32Array.from(TF.x, (x, j) => (sdEllipse(x, TF.y[j], 0.44, 0, 0.27, 0.19) < 0 || sdEllipse(x, TF.y[j], 0.25, 0, 0.13, 0.12) < 0 ? 1 + seam(x, TF.y[j]) : 0)); const nBait = Math.round(N * 0.09), nSurf = Math.round(N * 0.1), nSnow = Math.round(N * 0.035); const nSwell = N - OF.n - 2 * TF.n - nBait - nSurf - nSnow; const R = allot(N, G.mark('function makeDeep'), [OF.n, nBait, TF.n, TF.n, nSurf, nSwell, nSnow]); // the rolling swell: seven curtains of code, each sized to fill the view const SHEETS = 7, per = Math.ceil(nSwell / SHEETS); const SW = { x: new Float32Array(nSwell), y: new Float32Array(nSwell), z: new Float32Array(nSwell), s: new Float32Array(nSwell), k: new Uint8Array(nSwell) }; for (let sh = 0, j = 0; sh < SHEETS; sh++) { const cnt = Math.min(per, nSwell - sh * per); const z = mix(4, -170, (sh / (SHEETS - 1)) ** 1.2), dist = 64 - z; const half = dist * 0.48, top = Math.min(-98 + half, SEA_Y - 1.5), bot = -98 - half * 1.05; const W = half * 2.5, H = top - bot, h = Math.sqrt(W * H / (cnt * 0.62)), cols = Math.floor(W / (h * 0.62)); for (let q = 0; q < cnt; q++, j++) { SW.x[j] = 6 - W / 2 + ((q % cols) + 0.5) * h * 0.62 + (Math.floor(q / cols) % 2) * h * 0.31; SW.y[j] = top - (Math.floor(q / cols) + 0.5) * h; SW.z[j] = z + (rnd(j, 9) - 0.5) * h * 3; SW.s[j] = h * 0.95; SW.k[j] = sh; } } // the underside of the surface, dense above the camera's path const camPath = [[-2, 50], [4, 62], [20, 48], [40, 30]]; const SF = waterTiles(-90, -190, 110, 80, nSurf, (x, z) => Math.hypot(segDist(x, z, camPath), 30)); // the bait ball: fish circle a common axis in shells of differing speed const ORC = []; // orca centreline, posed per frame const state = { P: [0, 0, 0], V: [0, 0, 0], B: basis(0), th: 0, tp: [] }; const teal = hex(0x0B3550), deep = hex(0x06223A), beamCol = hex(0xC8F4FF), white = hex(0xF4F8FF); const black = hex(0x1E2C58), rim = hex(0x6FA0E8), saddle = hex(0x8C98B8), fishCol = hex(0xBFE9FF); const shellCol = hex(0x8A6A34), shellHi = hex(0xC9A060), seamCol = hex(0x1E1A12), skinCol = hex(0xBFC2A0); function orcaAt(t) { ORCA_PATH(t, state.P, state.V); const h = Math.hypot(state.V[0], state.V[2]); state.B = basis(Math.atan2(-state.V[2], state.V[0])); state.th = Math.atan2(state.V[1], h); const ph = TAU * t * 0.85; orca.pose({ head: -0.03 * Math.sin(ph), t1: 0.09 * Math.sin(ph - 0.6), t2: 0.15 * Math.sin(ph - 1.3), t3: 0.22 * Math.sin(ph - 2.0), fluke: 0.32 * Math.sin(ph - 2.7), pec: 0.18 * Math.sin(ph * 0.5) }); } // orca body point (local unit coords) -> world, into o function orcaPut(o, x, y, z) { const c = Math.cos(state.th), s = Math.sin(state.th); const lx = (x - 0.5) * ORCA_L, ly = y * ORCA_L; put(o, state.B, state.P, lx * c - ly * s, lx * s + ly * c, z); o.roll = state.th; } const eyeO = {}; return { roles: R, eye(t) { orcaAt(t); orcaPut(eyeO, 0.872, 0.012, 1.2); return [eyeO.x, eyeO.y, eyeO.z]; }, orca(t) { orcaAt(t); return state.P.slice(); }, turtle: t => TURTLES[0].path(t), frame(t) { orcaAt(t); ORC.length = 0; for (let k = 0; k <= 6; k++) { const q = {}; orcaPut(q, 0.98 - k * 0.16, 0, 0); ORC.push([q.x, q.y, q.z]); } state.tp = TURTLES.map(T => { const P = T.path(t), V = [0, 0, 0]; T.path(t, [0, 0, 0], V); const ph = TAU * (t * 0.6 + T.ph); turtle.pose({ fl: 0.62 * Math.sin(ph), fr: -0.62 * Math.sin(ph), fl2: -0.42 * Math.sin(ph - 1.0), fr2: 0.42 * Math.sin(ph - 1.0), rl: 0.22 * Math.sin(ph + 1), rr: -0.22 * Math.sin(ph + 1), head: 0.08 * Math.sin(ph * 0.5) }); return { P, M: turtle.M.slice(), roll: Math.atan2(V[1], Math.abs(V[0])) * 0.6 * (T.flip ? -1 : 1) + 0.06 * Math.sin(ph) }; }); }, glyph(i, t, o) { const j = R.idx[i]; o.s = 1; o.a = 1; o.bill = 0; o.roll = 0; o.yaw = 0; o.pitch = 0; switch (R.role[i]) { case 0: { // orca const p = skin(orca, OF, j), d = OF.d[j] / OF.dmax; orcaPut(o, p.x, p.y, Math.sqrt(d) * ORCA_L * 0.1); o.s = OF.h * ORCA_L * 0.98; const P = patch[j], edge = 1 - sat(OF.d[j] / 0.02); const L = 0.85 + 0.6 * beams(o.x, o.y, o.z, t); if (P === 1) { tint(o, white, 1.25 * L); o.w = 1.0; } else if (P === 2) { tint(o, saddle, 0.75 * L); o.w = 0.55; } else if (P === 3) { tint(o, EYE_COL, 0.9); o.w = 0.9; } else { tint2(o, black, rim, edge, (0.75 + edge * 1.8) * L); o.w = 0.3 + edge * 0.6; } return; } case 1: { // bait ball const [cx, cy, cz] = DEEP.ball, r = DEEP.ballR * (0.28 + 0.72 * Math.cbrt(rnd(j, 1))); const ph = Math.acos(2 * rnd(j, 2) - 1), w = (1.5 + rnd(j, 3) * 0.5) * (1.25 - r / DEEP.ballR * 0.5); const th = rnd(j, 4) * TAU + w * t; let x = cx + r * Math.sin(ph) * Math.cos(th), y = cy + r * Math.cos(ph) * 0.72 + Math.sin(t * 2 + j) * 0.4; let z = cz + r * Math.sin(ph) * Math.sin(th); let best = 1e9, bx = 0, by = 0, bz = 0; for (const q of ORC) { const d = Math.hypot(x - q[0], y - q[1], z - q[2]); if (d < best) { best = d; bx = q[0]; by = q[1]; bz = q[2]; } } if (best < 11) { // scatter from the orca const push = (11 - best) * 1.3 / (best + 0.01); x += (x - bx) * push; y += (y - by) * push; z += (z - bz) * push; } o.x = x; o.y = y; o.z = z; o.s = 0.6; o.bill = 1; o.roll = Math.sin(th) * 0.5; tint(o, fishCol, 1.0 + 1.3 * Math.max(0, Math.sin(th * 2 + j)) ** 6 + beams(x, y, z, t)); o.w = 0.7; return; } case 2: case 3: { // two sea turtles const T = TURTLES[R.role[i] - 2], S = state.tp[R.role[i] - 2], M = S.M; const a = TF.b0[j] * 6, b = TF.b1[j] * 6, w = TF.w[j], x0 = TF.x[j], y0 = TF.y[j]; let x = w * (M[a] * x0 + M[a + 2] * y0 + M[a + 4]) + (1 - w) * (M[b] * x0 + M[b + 2] * y0 + M[b + 4]); const y = w * (M[a + 1] * x0 + M[a + 3] * y0 + M[a + 5]) + (1 - w) * (M[b + 1] * x0 + M[b + 3] * y0 + M[b + 5]); if (T.flip) x = 1 - x; const c = Math.cos(S.roll), s = Math.sin(S.roll); const lx = (x - 0.45) * T.L, ly = y * T.L; put(o, basis(0, 1.0), S.P, lx * c - ly * s, lx * s + ly * c, Math.sqrt(TF.d[j] / TF.dmax) * T.L * 0.16); o.roll = S.roll; o.s = TF.h * T.L * 0.98; // dappled light from the surface plays across the shell const caus = Math.max(0, noise(o.x * 0.35 + t * 0.8, o.z * 0.35, t * 0.6)) * 1.6; const L = 0.8 + 0.8 * beams(o.x, o.y, o.z, t) + caus, edge = 1 - sat(TF.d[j] / 0.018); const sh = shell[j]; if (sh > 0) { const sm = sat((sh - 1) / 0.03), core = sat((sh - 1) / 0.09); tint2(o, seamCol, mix(0, 1, core) > 0.5 ? shellHi : shellCol, sm, L * (1.3 + 0.5 * core) + edge * 0.6); o.w = 0.2 + 0.7 * sm; } else { const scale = Math.abs(Math.sin(x0 * 70) * Math.sin(y0 * 70)) > 0.35 ? 1 : 0.55; tint2(o, skinCol, beamCol, edge * 0.5, L * 1.25 * scale); o.w = 0.35 + 0.4 * scale; } return; } case 4: { // surface, from below const x = SF.x[j], z = SF.z[j]; o.x = x; o.z = z; o.y = SEA_Y + 0.7 * Math.sin(0.11 * x + 0.07 * z - 1.6 * t) + 0.5 * noise(x * 0.05, z * 0.05, t * 0.5); o.s = SF.s[j] * 0.95; o.pitch = -Math.PI / 2; const c = noise(x * 0.09, z * 0.09, t * 0.9) * 0.5 + 0.5; tint2(o, teal, beamCol, c * c, 1.1 + c * 1.4); o.w = 0.2 + c * 0.7; return; } case 5: { // the rolling swell const sh = SW.k[j], x = SW.x[j], z = SW.z[j]; const wave = 1.8 * Math.sin(x * 0.07 + z * 0.02 - t * 1.6 + sh) + 0.7 * Math.sin(x * 0.16 - t * 2.4 + sh * 2); const slope = 0.126 * Math.cos(x * 0.07 + z * 0.02 - t * 1.6 + sh) + 0.112 * Math.cos(x * 0.16 - t * 2.4 + sh * 2); o.x = x + 1.2 * Math.sin(SW.y[j] * 0.05 + t); o.y = SW.y[j] + wave + 2 * noise(x * 0.03, SW.y[j] * 0.03, t * 0.4 + sh); o.z = z; o.s = SW.s[j]; o.roll = Math.atan(slope); const L = sat(beams(o.x, o.y, z, t) * 1.4); const c = G.cls[i], depth = sat((SEA_Y - o.y) / 90); o.r = mix(mix(teal[0], SYNTAX[c][0] * 0.35, 0.25), beamCol[0], L) * (1.15 - depth * 0.6 + L * 1.4); o.g = mix(mix(teal[1], SYNTAX[c][1] * 0.35, 0.25), beamCol[1], L) * (1.15 - depth * 0.6 + L * 1.4); o.b = mix(mix(teal[2], SYNTAX[c][2] * 0.35, 0.25), beamCol[2], L) * (1.15 - depth * 0.6 + L * 1.4); o.w = 0.06 + 0.9 * L; return; } default: { // marine snow o.x = -70 + rnd(j, 1) * 150; o.z = -60 + rnd(j, 3) * 130; o.y = -150 + rnd(j, 2) * 86 - (t - 1.5) * 0.9 + Math.sin(t + j) * 0.3; o.s = 0.35 + rnd(j, 4) * 0.4; o.bill = 1; o.roll = rnd(j, 5) * TAU; tint(o, beamCol, 0.5 + beams(o.x, o.y, o.z, t)); o.w = 0.3; } } }, }; } // ------------------------------------------------------ 3. MENAGERIE // The splash becomes a murmuration of character-birds. On every other // beat it condenses into a new animal, each in a world of its own: an // manta ray gliding over a moonlit sea of cloud, a giraffe walking across // a savanna at sunset, a kangaroo hopping across the red outback, a tiger leaping through bamboo, koi in a pond. const POND = [46, SEA_Y + 0.2, -6]; const MORPHS = [9.72, 10.72, 11.72, 12.72, 13.72]; // beats 20 22 24 26 28: manta, giraffe, kangaroo, tiger, koi const MANTA_P = path([[9.5, 40, -32, 16], [10.4, 41, -31, 5], [11.3, 42, -30, -6]]); const GIR_X = path([[10.5, 20, 0, 0], [12.3, 40, 0, 0]]); const ROO_X = path([[11.5, 10, 0, 0], [13.3, 52, 0, 0]]); const TIGER_P = path([[12.7, 30, SEA_Y + 2, 0], [13.1, 36, SEA_Y + 5, 0], [13.5, 47, SEA_Y + 15, 0], [13.9, 58, SEA_Y + 7, 0], [14.3, 64, SEA_Y + 2, 0]]); const KOI = [0xF4F1EA, 0xE8623A, 0xFFB84D, 0x1A1F33, 0x9DB4D8].map(hex); const SUN = [40, SEA_Y + 14, -260]; function makeMenagerie(G) { const N = G.N, nHero = 5 * Math.floor(N * 0.38 / 5); const specs = { manta: mantaSpec(), giraffe: giraffeSpec(), roo: rooSpec(), tiger: tigerSpec() }; const rig = {}; for (const k in specs) { rig[k] = new Rig(specs[k]); specs[k].at = rig[k].at; } rig.koi = koiRig(); const F = { manta: fillRig(rig.manta, nHero), giraffe: fillRig(rig.giraffe, nHero), roo: fillRig(rig.roo, nHero), tiger: fillRig(rig.tiger, nHero), koi: fillRig(rig.koi, nHero / 5) }; const mPatch = Uint8Array.from(F.manta.x, (x, j) => mantaPatch(x, F.manta.y[j])); const nSky = Math.round(N * 0.07), nFeat = Math.round(N * 0.15), nGround = N - nHero - nSky - nFeat; const R = allot(N, G.mark('function makeMenagerie'), [nHero, nGround, nFeat, nSky]); const camPath = [[26, 62], [28, 50], [40, 46], [46, 4]]; const GT = waterTiles(-90, -240, 170, 80, nGround, (x, z) => Math.hypot(segDist(x, z, camPath), 14)); const nMoon = Math.round(nSky * 0.3); const MF = fillShape((x, y) => Math.hypot(x, y) - 1, [-1.02, -1.02, 1.02, 1.02], nMoon, 0.62); // features of each world, laid out once: [x, y, z, size, kind] const plane = (sdf, box, z, n) => { const S = fillShape(sdf, box, n); return Array.from(S.x, (x, k) => [x, S.y[k], z, S.h, S.d[k]]); }; const foam = Array.from({ length: nFeat }, (_, k) => [54 + (rnd(k, 1) - 0.5) * 36, SEA_Y + 0.4 + rnd(k, 2) * 1.5, (rnd(k, 3) - 0.5) * 26, 0.5, 0]); const ridge = x => SEA_Y + 22 + 30 * (0.5 + 0.5 * noise(x * 0.011, 1.3, 0.7)) + 14 * noise(x * 0.04, 2.1, 0.2); const peaks = plane((x, y) => y - ridge(x), [-160, SEA_Y, 240, SEA_Y + 80], -230, nFeat); const acacia = (x, y, cx, h, w) => Math.min(sdCapsule(x, y, cx, SEA_Y, cx + 1, SEA_Y + h, 1.2, 0.7), sdEllipse(x, y, cx + 1, SEA_Y + h + 1.5, w, 2.4), sdCapsule(x, y, cx + 1, SEA_Y + h * 0.6, cx + w * 0.6, SEA_Y + h + 1, 0.5, 0.4)); const hills = x => SEA_Y + 6 + 7 * (0.5 + 0.5 * noise(x * 0.02, 4.4, 0.1)); const savanna = plane((x, y) => Math.min(y - hills(x), acacia(x, y, -20, 14, 13), acacia(x, y, 70, 18, 16), acacia(x, y, 112, 12, 10)), [-90, SEA_Y, 170, SEA_Y + 40], -110, nFeat); const mesa = x => SEA_Y + 4 + 18 * smooth(sat((Math.abs(((x + 400) % 90) - 45) - 8) / -10 + 1)) + 3 * noise(x * 0.05, 7, 0); const mesas = plane((x, y) => y - mesa(x), [-120, SEA_Y, 200, SEA_Y + 30], -170, Math.round(nFeat * 0.35)); const bamboo = []; for (let k = 0, n = 0; n < nFeat; k++) { const x = -10 + rnd(k, 11) * 110, z = [-46, -30, -18, 14, 24][k % 5] + (rnd(k, 12) - 0.5) * 6, h = 34 + rnd(k, 13) * 22; for (let y = 0; y < h && n < nFeat; y += 0.62) for (const dx of [-0.35, 0.35]) { if (n < nFeat) bamboo.push([x + dx, SEA_Y + y, z, 0.6, (y % 4.2) < 0.62 ? 1 : 0, k]); n++; } } const pads = Array.from({ length: nFeat }, (_, k) => { const isPad = k % 2 === 0, c = Math.floor(rnd(k, 21) * 9), a = rnd(k, 22) * TAU, r = Math.sqrt(rnd(k, 23)); if (isPad) { const px = POND[0] + Math.cos(c * 2.1) * (10 + c * 1.6), pz = POND[2] + Math.sin(c * 2.1) * (10 + c * 1.6); return [px + Math.cos(a) * r * 2.6, POND[1] + 0.1, pz + Math.sin(a) * r * 2.6, 0.5, 1]; } const ra = 24.5 + rnd(k, 24) * 4; return [POND[0] + Math.cos(a) * ra, POND[1] + 0.3 + rnd(k, 25) * 0.6, POND[2] + Math.sin(a) * ra, 0.6, 2]; }); const sea = hex(0x0E1F4A), seaHi = hex(0xBFD2FF), star = hex(0xDCE6FF), moon = hex(0xFFF2D6), sunC = hex(0xFF8A3A); const bird = hex(0xD9E6FF), mantaC = hex(0x56688F), mantaW = hex(0xF2F6FF), rim = hex(0x8FB0FF); const girPatch = hex(0x8A4618), girCream = hex(0xFFEBC4), girDark = hex(0x3A2A20), rooC = hex(0xC2683A); const tigerC = hex(0xFF8A2A), stripe = hex(0x14080A), cream = hex(0xFFF1DC), pondC = hex(0x17586A), pad = hex(0x6FD98A), stone = hex(0x7A7F94); const cloudLo = hex(0x1B2A55), cloudHi = hex(0xCFDBFF), grass = hex(0xB98A3A), dusk = hex(0xFF9A4D), dirt = hex(0x5A2814); const crack = hex(0xB8643A), fern = hex(0x1F5A3A), fernHi = hex(0x6FBF6A), snow = hex(0xEAF0FF), rock = hex(0x22305A); const S = { ep: [0, 0, 0], flap: 0, bank: 0, lx: 0, ox: 0, tp: [0, 0, 0], tb: 0, koi: [], bob: 0 }; const SIDE = basis(0), UP = basis(0, Math.PI / 2); function pose(t) { if (t > 9.4 && t < 11.4) { // the manta glides, its wings beating in a wave MANTA_P(t, S.ep); S.flap = TAU * 0.9 * (t - 9.6); S.bank = 0.12 * Math.sin(TAU * 0.4 * (t - 9.6)); } if (t > 10.4 && t < 12.4) { // the giraffe walks, pacing // a pace: both legs on one side move together; each foot stays planted // while the body travels over it, then lifts and swings through const ph = (t - 10.5) / (2 * BEAT), P = {}, A = 0.21; const step = off => { const p = ((ph + off) % 1 + 1) % 1; if (p < 0.6) { const a = A * (1 - 2 * p / 0.6); return [a, a, a]; } const u = (p - 0.6) / 0.4, a = A * (-1 + 2 * smooth(u)), lift = Math.sin(Math.PI * u); return [a + 0.1 * lift, a - 0.55 * lift, a + 0.3 * lift]; }; limbPose(P, specs.giraffe, 'fn', step(0)); limbPose(P, specs.giraffe, 'hn', step(0)); limbPose(P, specs.giraffe, 'ff', step(0.5)); limbPose(P, specs.giraffe, 'hf', step(0.5)); const f = TAU * ph * 2; P.root = 0.012 * Math.sin(f); P.neck = 0.06 * Math.sin(f + 0.8); P.head = -0.05 * Math.sin(f + 0.8); P.tail = 0.15 * Math.sin(TAU * ph + 1); rig.giraffe.pose(P); S.lx = GIR_X(t)[0]; S.lbob = 0.006 * Math.cos(f); } if (t > 11.4 && t < 13.4) { // the kangaroo hops, once a beat // short contact (land, crouch, push off), then a long flight on an arc const ph = (((t - 11.5) / BEAT) % 1 + 1) % 1; const LAND = [0.9, -0.5, 1.4], CROUCH = [0.6, -1.3, 1.5], PUSH = [-0.2, -0.4, 0.6], STRETCH = [-0.5, -0.2, 0.2]; const lerp3 = (a, b, e) => a.map((v, k) => mix(v, b[k], e)); const LEN = [0.206, 0.228, 0.2], reach = L3 => L3.reduce((h, a, k) => h + LEN[k] * Math.cos(a), 0); const rest = reach([0.5, -0.91, 1.37]); let legs, bob, pitch, tail; if (ph < 0.3) { // on the ground: feet stay put const u = ph / 0.3; legs = u < 0.5 ? lerp3(LAND, CROUCH, smooth(u * 2)) : lerp3(CROUCH, PUSH, smooth(u * 2 - 1)); bob = reach(legs) - rest; pitch = -0.12 * Math.sin(Math.PI * u); tail = -0.15; } else { // in the air const u = (ph - 0.3) / 0.7, top = reach(PUSH) - rest; legs = u < 0.45 ? lerp3(PUSH, STRETCH, smooth(u / 0.45)) : lerp3(STRETCH, LAND, smooth((u - 0.45) / 0.55)); bob = mix(top, 0, u) + 1.2 * u * (1 - u); pitch = -0.38 * Math.sin(Math.PI * u); tail = 0.32 * Math.sin(Math.PI * u); } const P = { root: pitch, head: -pitch * 0.6, arm: 0.25, tail0: tail, tail1: tail * 0.6 }; limbPose(P, specs.roo, 'hn', legs); limbPose(P, specs.roo, 'hf', legs.map(v => v + 0.06)); rig.roo.pose(P); S.ox = ROO_X(t)[0]; S.obob = bob; } if (t > 12.4 && t < 14.4) { const u = ramp(t, 12.75, 14.25); const k = (a, b, c, d) => (u < 0.33 ? mix(a, b, smooth(u / 0.33)) : u < 0.66 ? mix(b, c, smooth((u - 0.33) / 0.33)) : mix(c, d, smooth((u - 0.66) / 0.34))); const P = { root: k(0.25, -0.15, -0.05, 0.25), head: k(-0.2, 0.05, 0.0, -0.15) }; const ts = specs.tiger; limbPose(P, ts, 'fn', [k(0.2, 1.1, 1.25, 0.6), k(0.1, 1.3, 1.45, 0.4), k(0.3, 1.5, 1.6, 0.6)]); limbPose(P, ts, 'ff', [k(-0.1, 0.8, 1.0, 0.2), k(-0.3, 1.0, 1.25, 0.0), k(0.0, 1.2, 1.4, 0.3)]); limbPose(P, ts, 'hn', [k(0.4, -1.1, -1.05, -0.2), k(-0.6, -1.35, -1.3, -0.5), k(0.2, -1.5, -1.45, 0.0)]); limbPose(P, ts, 'hf', [k(0.7, -0.7, -0.75, 0.1), k(-0.3, -1.0, -1.05, -0.2), k(0.3, -1.2, -1.2, 0.1)]); for (let q = 0; q < 4; q++) P['tail' + q] = 0.18 * Math.sin(t * 7 - q * 0.9) + (q ? 0.1 : 0); rig.tiger.pose(P); const V = [0, 0, 0]; TIGER_P(t, S.tp, V); S.tb = Math.atan2(V[1], V[0]) * 0.7; } if (t > 13.4) { S.koi = [0, 1, 2, 3, 4].map(k => { const r = 4 + k * 2.8, w = (1.0 - k * 0.1) * (k % 2 ? -1 : 1), a = k * 1.3 + w * (t - 13.7), ph = TAU * (t * 1.6 + k * 0.3); rig.koi.pose({ root: 0.05 * Math.sin(ph), s1: 0.12 * Math.sin(ph - 0.8), s2: 0.18 * Math.sin(ph - 1.6) - 0.08 * Math.sign(w), s3: 0.24 * Math.sin(ph - 2.4) - 0.1 * Math.sign(w), tail: 0.35 * Math.sin(ph - 3.2) }); const head = a + Math.sign(w) * Math.PI / 2; return { P: [POND[0] + r * Math.cos(a), POND[1] - 0.3, POND[2] + r * Math.sin(a)], B: basis(Math.atan2(-Math.sin(head), Math.cos(head)), Math.PI / 2), M: rig.koi.M.slice(), L: 13 - k * 0.6 }; }); } } const depthZ = (Fs, j, L, k) => Math.sqrt(Fs.d[j] / Fs.dmax) * L * k; const farLeg = name => name.length === 3 && name[1] === 'f'; // hero glyph j in shape s: 0 birds, 1 manta, 2 giraffe, 3 kangaroo, 4 tiger, 5 koi function hero(s, j, i, t, o) { o.a = 1; o.bill = 0; o.roll = 0; o.yaw = 0; o.pitch = 0; if (s === 0) { // a murmuration is a sheet of birds that folds over itself: dense where it turns edge-on const u = rnd(j, 1) * 2 - 1, v = rnd(j, 2) * 2 - 1, T = t * 1.2, thick = (rnd(j, 3) - 0.5) * 0.12; let x = u + 0.35 * Math.sin(2.2 * v + T), y = 0.55 * Math.sin(2.8 * u + 1.7 * v - T * 1.3) + thick, z = v + 0.3 * Math.cos(2.4 * u + T * 0.8); const fold = 0.35 * Math.sin(1.5 * x - T); y += fold; x *= 1 - 0.25 * (u * u); const sp = ramp(t, 8.4, 9.2), c = [44 + (t - 8.5) * 2, -38 + (t - 8.5) * 3, 6 - (t - 8.5) * 6]; o.x = c[0] + x * 32 * sp; o.y = c[1] + y * 12 * sp; o.z = c[2] + z * 18 * sp; o.s = 0.36; o.bill = 1; o.roll = 0.6 * Math.sin(t * 20 + j); tint2(o, bird, SYNTAX[G.cls[i]], 0.3, 0.32); o.w = 0.6; return; } if (s === 1) { // manta, from above const Fs = F.manta, p = skin(rig.manta, Fs, j), L = 8, ax = Math.abs(p.x); const wave = Math.sin(S.flap - 1.4 * ax), lz = ax * L * 0.32 * wave + depthZ(Fs, j, L, 0.05); const c = Math.cos(S.bank), sn = Math.sin(S.bank); put(o, UP, S.ep, p.x * L * c - lz * sn, p.y * L, p.x * L * sn + lz * c); o.s = Fs.h * L; // from below: a pale belly glowing in moonlight, dark margins, gill slits const P = mPatch[j], margin = 1 - sat(Fs.d[j] / 0.05), lit = 0.85 + 0.25 * Math.sin(S.flap - 1.4 * ax + 1.2); if (P === 1) { tint(o, mantaC, 0.5); o.w = 0.2; } else if (P === 2) { tint(o, mantaC, 0.8); o.w = 0.4; } else { tint2(o, mantaW, mantaC, margin * 0.85, (1.25 - margin * 0.6) * lit); o.w = 0.85 - margin * 0.45; } return; } if (s === 2) { // giraffe, walking const Fs = F.giraffe, p = skin(rig.giraffe, Fs, j), name = rig.giraffe.bones[Fs.b0[j]].name, L = 30; put(o, SIDE, [S.lx, SEA_Y + 0.2 + S.lbob * L, 0], (p.x - 0.5) * L, p.y * L, depthZ(Fs, j, L, 0.06)); o.s = Fs.h * L; const far = farLeg(name); if (far) o.z -= 1.2; const edge = 1 - sat(Fs.d[j] / 0.012), x0 = Fs.x[j], y0 = Fs.y[j]; const leg = name.length === 3, hoof = leg && name[2] === '2', lowLeg = leg && name[2] === '1' && y0 < 0.22; const cell = name === 'root' ? 0.038 : 0.026, seamK = giraffeSeam(x0, y0, cell) < cell * 0.19; const tuft = (name === 'head' && y0 > 1.0) || (name === 'tail' && y0 < 0.37); const mane = name === 'neck' && Fs.d[j] < 0.012 && y0 - 0.65 > (x0 - 0.55) * 1.75; // the ridge on the back of the neck const sun = 0.7 + 0.6 * sat((y0 - 0.5) * 2.2), belly = name === 'root' && y0 < 0.56 ? 0.7 : 1; // lit from above, belly in shade if (hoof || tuft || mane) { tint2(o, girDark, dusk, edge * 0.3, 0.9 + edge * 0.4); o.w = 0.9; } else if (seamK || lowLeg) { tint2(o, girCream, dusk, edge * 0.5, ((far ? 0.75 : 1.45) + edge * 0.6) * sun * belly); o.w = 0.75; } else { const v = 0.85 + 0.3 * rnd(Math.floor(x0 / cell) * 131 + Math.floor(y0 / cell) * 977, 3); tint2(o, girPatch, dusk, edge * 0.7, ((far ? 0.55 : 1.05) * v + edge * 0.8) * sun * belly); o.w = 0.45 + 0.3 * v; } return; } if (s === 3) { // kangaroo const Fs = F.roo, p = skin(rig.roo, Fs, j), name = rig.roo.bones[Fs.b0[j]].name, L = 22; put(o, SIDE, [S.ox, SEA_Y + 0.2 + S.obob * L, 0], (p.x - 0.5) * L, p.y * L, depthZ(Fs, j, L, 0.07)); o.s = Fs.h * L; const far = farLeg(name); if (far) o.z -= 1.5; const lit = 0.75 + 0.45 * sat((Fs.y[j] - 0.3) * 1.6), edge = 1 - sat(Fs.d[j] / 0.02); const belly = Fs.x[j] > 0.47 && Fs.y[j] > 0.42 && Fs.y[j] < 0.72 && name === 'root'; tint2(o, belly ? cream : rooC, dusk, edge * 0.8, (far ? 0.6 : 1.2) * lit + edge * 0.8); o.w = far ? 0.4 : 0.5 + 0.4 * lit; return; } if (s === 4) { // tiger const Fs = F.tiger, p = skin(rig.tiger, Fs, j), L = 24, c = Math.cos(S.tb), sn = Math.sin(S.tb); const lx = (p.x - 0.5) * L, ly = (p.y - 0.5) * L; put(o, SIDE, S.tp, lx * c - ly * sn, lx * sn + ly * c, depthZ(Fs, j, L, 0.07)); o.roll = S.tb; o.s = Fs.h * L; const x0 = Fs.x[j], y0 = Fs.y[j], name = rig.tiger.bones[Fs.b0[j]].name, far = farLeg(name); const belly = (name === 'root' && y0 < 0.43) || (name === 'head' && y0 < 0.585); const str = (name !== 'head' && Math.sin(x0 * 62 + 4 * noise(x0 * 6, y0 * 6, 1.5)) > 0.45) || (name === 'head' && Math.sin(y0 * 90) > 0.7 && x0 < 0.95); if (str) { tint(o, stripe, 1); o.w = 0.05; } else if (belly) { tint(o, cream, 1.2); o.w = 0.7; } else { tint(o, tigerC, far ? 0.6 : 1.25); o.w = far ? 0.45 : 0.85; } if (far) o.z -= 1.5; return; } const nk = nHero / 5, k = Math.floor(j / nk), q = j - k * nk, K = S.koi[k], M = K.M, Fs = F.koi; const a = Fs.b0[q] * 6, b = Fs.b1[q] * 6, w = Fs.w[q], x0 = Fs.x[q], y0 = Fs.y[q]; const x = w * (M[a] * x0 + M[a + 2] * y0 + M[a + 4]) + (1 - w) * (M[b] * x0 + M[b + 2] * y0 + M[b + 4]); const y = w * (M[a + 1] * x0 + M[a + 3] * y0 + M[a + 5]) + (1 - w) * (M[b + 1] * x0 + M[b + 3] * y0 + M[b + 5]); const dive = ramp(t, 15.85, 16.05); put(o, K.B, [K.P[0], K.P[1] - dive * 3, K.P[2]], (x - 0.5) * K.L, y * K.L, 0.2); o.s = Fs.h * K.L * (1 - dive * 0.5); const blot = noise(x0 * 7 + k * 3, y0 * 7, k) > 0.05; const base = k === 0 ? (blot ? KOI[1] : KOI[0]) : k === 3 ? (blot ? KOI[1] : KOI[3]) : k === 4 ? (blot ? KOI[1] : KOI[4]) : k === 1 ? KOI[2] : (blot ? KOI[3] : KOI[0]); const sh = Math.max(0, Math.sin(x0 * 40 - t * 9 + k)) ** 4; tint2(o, base, vivid(Math.floor(x0 * 20) + k), sh * 0.8, 1.1 + sh * 1.4); o.w = 0.55 + sh * 0.45; } // ground tile j in world w: 0 sea, 1 cloud, 2 savanna, 3 dust, 4 jungle, 5 pond function ground(w, j, t, o) { const x = GT.x[j], z = GT.z[j], s = GT.s[j]; o.x = x; o.z = z; o.s = s * 0.95; o.a = 1; o.bill = 0; o.roll = 0; o.yaw = 0; o.pitch = Math.PI / 2; if (w === 0) { const sw = Math.sin(0.12 * x - 1.7 * t) + 0.6 * Math.sin(0.09 * z + 0.05 * x - 2.3 * t); o.y = SEA_Y + 0.5 * sw + 0.4 * noise(x * 0.06, z * 0.06, t * 0.6); const toM = Math.abs(Math.atan2(x - 40, z - 50) - Math.atan2(150 - 40, -260 - 50)); const glit = Math.exp(-toM * toM * 30) * Math.max(0, noise(x * 0.3, z * 0.3, t * 2)) * 3 + Math.max(0, sw) ** 3 * 0.25; tint2(o, sea, seaHi, sat(glit), 0.9 + 2.2 * glit); o.w = 0.15 + sat(glit) * 0.8; } else if (w === 1) { const n = noise(x * 0.035 + t * 0.05, z * 0.035, 2) + 0.5 * noise(x * 0.09, z * 0.09, 5); o.y = SEA_Y + 4 + 9 * Math.max(n, -0.2); o.s *= 1.25; o.bill = 0.35; tint2(o, cloudLo, cloudHi, sat(n * 0.9 + 0.35), 0.32); o.w = 0.1 + 0.4 * sat(n + 0.3); } else if (w === 2) { const n = noise(x * 0.05, z * 0.05, 3.3); o.y = SEA_Y + 0.2 + 0.15 * n; o.pitch = 1.1; o.bill = 0.2; o.s *= 0.9; o.roll = 0.35 * Math.sin(t * 2.4 + x * 0.15 + z * 0.08) + 0.15 * n; tint2(o, grass, dusk, sat(0.3 - z * 0.004 + n * 0.3), 0.2 + 0.18 * (n + 0.5)); o.w = 0.25 + 0.35 * sat(n + 0.5); } else if (w === 3) { const n = noise(x * 0.08, z * 0.08, 6.1); o.y = SEA_Y + 0.1; const cr = Math.abs(n) < 0.05 ? 1 : 0; tint2(o, dirt, crack, cr * 0.8 + 0.1, 0.9 + cr * 0.8); o.w = 0.2 + cr * 0.6; } else if (w === 4) { const n = noise(x * 0.06, z * 0.06, 8.8); o.y = SEA_Y + 0.3 + 0.8 * Math.max(n, 0); o.pitch = 0; o.bill = 0.5; o.roll = 0.25 * Math.sin(t * 1.8 + x * 0.2) + n; tint2(o, fern, fernHi, sat(n + 0.4), 0.7); o.w = 0.3 + 0.5 * sat(n + 0.3); } else { const r = Math.hypot(x - POND[0], z - POND[2]); if (r < 24) { const ring = Math.sin(r * 1.6 - t * 5) * 0.5 + 0.5; o.y = POND[1] + ring * 0.08; tint(o, pondC, 0.5 + ring * 0.35); o.w = 0.15 + ring * 0.25; } else { o.y = SEA_Y + 0.2; tint2(o, fern, fernHi, sat(noise(x * 0.2, z * 0.2, 1) + 0.2), 0.5); o.w = 0.3; } } } function feature(w, j, t, o) { o.a = 1; o.bill = 0; o.roll = 0; o.yaw = 0; o.pitch = 0; o.w = 0.5; if (w === 0) { const [x, y, z] = foam[j], age = ramp(t, 8.45, 9.6); o.x = x; o.y = y + age * 2; o.z = z; o.s = 0.5; o.bill = 1; tint(o, seaHi, 0.6 * (1 - age) + 0.1); o.w = 0.5; return; } const src = w === 1 ? peaks : w === 2 ? savanna : w === 4 ? bamboo : w === 5 ? pads : null; if (w === 3) { // red dust behind the kangaroo, and mesas if (j < mesas.length) { const [x, y, z, h] = mesas[j]; o.x = x; o.y = y; o.z = z; o.s = h; tint(o, dirt, 1.3 + 0.6 * sat((y - SEA_Y) / 20)); o.w = 0.35; return; } const age = (rnd(j, 31) + t * 1.3) % 1, ox = S.ox || ROO_X(t)[0]; o.x = ox - 2 - age * 22 + (rnd(j, 32) - 0.5) * 6; o.y = SEA_Y + age * 8 * rnd(j, 33) + 0.3; o.z = (rnd(j, 34) - 0.5) * 12; o.s = 0.5 + age; o.bill = 1; o.roll = rnd(j, 35) * TAU; tint(o, crack, 0.9 * (1 - age)); o.w = 0.2; return; } const f = src[j % src.length]; o.x = f[0]; o.y = f[1]; o.z = f[2]; o.s = f[3]; if (w === 1) { const snowy = f[1] > ridge(f[0]) - 5; tint2(o, rock, snow, snowy ? 1 : 0.1, snowy ? 1.2 : 0.7); o.w = snowy ? 0.8 : 0.25; } else if (w === 2) { tint2(o, hex(0x1A0E14), dusk, f[1] < hills(f[0]) ? 0.25 : 0.05, 0.45); o.w = 0.5; } else if (w === 4) { o.x += Math.sin(t * 1.3 + f[5]) * 0.02 * (f[1] - SEA_Y); tint2(o, fern, fernHi, f[4] ? 0.9 : 0.35, f[4] ? 1.5 : 0.9); o.w = f[4] ? 1 : 0.4; o.bill = 0.3; } else { o.pitch = Math.PI / 2; o.s *= 0.7; if (f[4] === 1) { tint(o, pad, 0.4); o.w = 0.7; } else { tint(o, stone, 0.45); o.w = 0.6; o.bill = 0.4; } } } function sky(w, j, t, o) { o.a = 1; o.bill = 1; o.roll = 0; o.yaw = 0; o.pitch = 0; if (j < nMoon) { // the moon; at the savanna, a setting sun const sun = w === 2 || w === 3, r = sun ? 46 : 30, P = sun ? SUN : [150, 30, -260]; o.x = P[0] + MF.x[j] * r; o.y = P[1] + MF.y[j] * r; o.z = P[2]; o.s = MF.h * r; const cr = noise(MF.x[j] * 3, MF.y[j] * 3, 4.1); if (sun) { tint2(o, sunC, hex(0xFFE0A0), sat(1 - Math.hypot(MF.x[j], MF.y[j])), w === 3 ? 0.6 : 1.6); o.w = 0.8; } else { tint(o, moon, (1.1 + 0.6 * cr) * (w === 4 ? 0.7 : 1)); o.w = 0.55 + 0.4 * cr; } o.a = w === 5 ? 0 : 1; return; } const az = rnd(j, 1) * Math.PI * 1.2 - 1.6, el = Math.asin(rnd(j, 2) ** 1.6) * 0.9 + 0.03, r = 300; o.x = 40 + r * Math.cos(el) * Math.sin(az); o.y = SEA_Y + r * Math.sin(el); o.z = 30 - r * Math.cos(el) * Math.cos(az); o.s = 0.9 + rnd(j, 3) * 1.6; const tw = 0.6 + 0.4 * Math.sin(t * 6 + j); if (w === 2 || w === 3) { tint2(o, dusk, hex(0x3A1E40), sat(el * 2), 0.35 * tw); o.w = 0.3; } else { tint2(o, star, SYNTAX[G.cls[j % G.N]], 0.35, (w === 4 ? 0.35 : 0.7) * rnd(j, 4) * tw); o.w = 0.5; } o.a = w === 5 ? 0 : 1; } // which world an environment glyph is in, with a sweep across the frame const A = {}, B = {}; function env(fn, j, i, t, o, x) { const d = sat((x + 60) / 220) * 0.3; let w = 0; while (w < 5 && t >= MORPHS[w] - 0.2 + d) w++; const u = w > 0 ? ramp(t, MORPHS[w - 1] - 0.2 + d, MORPHS[w - 1] + 0.25 + d) : 1; if (u >= 1) return fn(w, j, t, o); fn(w - 1, j, t, A); fn(w, j, t, B); const e = smoother(u); blendState(o, A, B, e); const arc = Math.sin(Math.PI * e); o.y += arc * (4 + rnd(j, 41) * 6); o.x += arc * (rnd(j, 42) - 0.5) * 8; o.bill = mix(o.bill, 1, arc); } return { roles: R, manta: t => MANTA_P(t), giraffe: t => [GIR_X(t)[0], SEA_Y + 15, 0], roo: t => [ROO_X(t)[0], SEA_Y + 10, 0], tiger: t => TIGER_P(t), frame: pose, glyph(i, t, o) { const j = R.idx[i], role = R.role[i]; o.s = 1; o.a = 1; o.w = 0.4; o.bill = 0; o.roll = 0; o.yaw = 0; o.pitch = 0; if (role === 0) { let s = 0; while (s < 5 && t >= MORPHS[s] + 0.5) s++; if (s < 5 && t > MORPHS[s]) { // a murmuration morph const st = MORPHS[s] + (j / nHero) * 0.22, e = smoother(ramp(t, st, st + 0.28)); if (e > 0) { const A2 = {}, B2 = {}; hero(s, j, i, t, A2); hero(s + 1, j, i, t, B2); blendState(o, A2, B2, e); const arc = Math.sin(Math.PI * e), n1 = noise(A2.x * 0.08, A2.y * 0.08, 3.3), n2 = noise(A2.x * 0.08, A2.y * 0.08, 7.7); o.x += arc * n1 * 16; o.y += arc * (n2 * 10 + 3); o.z += arc * noise(A2.x * 0.08, 1, A2.y * 0.08) * 12; o.bill = mix(o.bill, 1, arc); o.w += arc * 0.3; return; } } return hero(s, j, i, t, o); } if (role === 1) return env(ground, j, i, t, o, GT.x[j]); if (role === 2) return env(feature, j, i, t, o, j / nFeat * 200 - 60); return env(sky, j, i, t, o, 80); }, }; } // ------------------------------------------------------ 4. ABSTRACTION // The pond shatters on the drop into a flow field of syntax colour, which // collapses through a tunnel of text rings into a blooming mandala. const AB = [46, -6]; // shared axis (x, z) const FLOW_Y = -44; function makeAbstraction(G, aspect = 0.8) { const N = G.N, wide = Math.max(1, aspect / 0.8); // flow seeds: a jittered grid across the view const cols = Math.ceil(Math.sqrt(N * 0.8)), rows = Math.ceil(N / cols); // rings of the tunnel: annuli of concentric text, stacked downward const RINGS = 7; // mandala: log-polar rings, finest at the centre const per = Math.round(Math.sqrt(N * 2.2)), ringsM = Math.ceil(N / per), q = Math.log(110) / ringsM; return { frame() {}, // flow field: each glyph is carried along the field from its seed flow(i, t, o) { const c = i % cols, r = Math.floor(i / cols); let x = AB[0] + ((c + rnd(i, 1)) / cols - 0.5) * 34 * wide, z = AB[1] + ((r + rnd(i, 2)) / rows - 0.5) * 42; const T = Math.max(0, t - 16.0), steps = 6, h = T * 5 / steps; let ang = 0; for (let s = 0; s < steps; s++) { ang = noise(x * 0.07, z * 0.07, 0.3 * t) * 5 + 0.6; x += Math.cos(ang) * h; z += Math.sin(ang) * h; } const pull = smooth(ramp(t, 16.9, 17.7)); // the field collapses const dx = x - AB[0], dz = z - AB[1]; o.x = AB[0] + dx * (1 - pull * 0.65); o.z = AB[1] + dz * (1 - pull * 0.65); o.y = FLOW_Y + Math.sin(dx * 0.3 + t * 4) * 0.4; o.s = 0.34; o.a = 1; o.bill = 0; o.pitch = Math.PI / 2; o.yaw = 0; o.roll = -ang; const col = VIVID[(G.cls[i] + Math.floor(rnd(i, 4) * 2)) % VIVID.length]; tint(o, col, 0.42 + 0.5 * Math.max(0, Math.sin(ang * 3 + t * 6))); o.w = 0.45 + 0.4 * Math.max(0, Math.sin(i * 0.02 + t * 5)); }, ring(i, t, o) { const per2 = Math.ceil(N / RINGS), k = Math.floor(i / per2), j = i - k * per2; const circles = 18, perC = Math.ceil(per2 / circles), c = Math.floor(j / perC), m = j - c * perC; const r = 6.5 + c * 0.62 + 1.4 * Math.sin(k * 0.9 + t * 3); const a = m / perC * TAU + (k % 2 ? 1 : -1) * (t - 17) * (0.6 + c * 0.015) + k; o.x = AB[0] + r * Math.cos(a); o.z = AB[1] + r * Math.sin(a); o.y = -14 - k * 6.2; o.s = 0.5; o.a = 1; o.bill = 0; o.pitch = Math.PI / 2; o.yaw = 0; o.roll = -a - Math.PI / 2; tint(o, VIVID[(k + c) % VIVID.length], 0.5 + 0.8 * Math.max(0, Math.sin(a * 6 - t * 10))); o.w = 0.35 + 0.55 * Math.max(0, Math.sin(a * 6 - t * 10 + c)); }, mandala(i, t, o) { const k = Math.floor(i / per), m = i - k * per; const bloom = 0.04 + 0.96 * outBack(ramp(t, 17.96, 18.84)); const r0 = 0.5 * Math.exp(k * q); const petals = [6, 8, 12, 16][k % 4]; const a = m / per * TAU + (k % 2 ? 1 : -1) * (t - 18) * 0.35; const r = r0 * bloom * (1 + 0.1 * Math.cos(petals * a + k * 0.7 + t * 2)); o.x = AB[0] + r * Math.cos(a); o.z = AB[1] + r * Math.sin(a); o.y = SEA_Y + 0.15; o.s = r0 * bloom * TAU / per * 1.4; o.a = 1; o.bill = 0; o.pitch = Math.PI / 2; o.yaw = 0; o.roll = -a - Math.PI / 2; const peak = Math.max(0, Math.cos(petals * a + k * 0.7 + t * 2)); tint(o, VIVID[(Math.floor(k / 3)) % VIVID.length], 0.5 + 1.0 * peak); o.w = 0.3 + 0.7 * peak; }, glyph(i, t, o) { // flow -> rings -> mandala, each a soft hand-off staggered by glyph const d1 = 17.04 + rnd(i, 7) * 0.44, d2 = 17.96 + (i / N) * 0.4; const e1 = smoother(ramp(t, d1, d1 + 0.44)), e2 = smoother(ramp(t, d2, d2 + 0.44)); if (e2 >= 1) return this.mandala(i, t, o); if (e1 <= 0) return this.flow(i, t, o); const A = {}, B = {}; if (e2 > 0) { this.ring(i, t, A); this.mandala(i, t, B); blendState(o, A, B, e2); return; } this.flow(i, t, A); this.ring(i, t, B); blendState(o, A, B, e1); o.y += Math.sin(Math.PI * e1) * 6; }, }; } // ------------------------------------------------------ 5. SYDNEY // Sydney Harbour at night, from survey geometry at 1 unit = 8 m, x east, // z south, origin on the Opera House. The skyline, the Harbour Bridge and // the Opera House rise out of the water; a show plays on the sails; the // finale bursts as fireworks over the harbour, mirrored in the water. // The harbour is turned 180 degrees in the world, so the message is seen // facing the same way as the editor and the camera can glide straight home. const SYD = [AB[0] - 105, SEA_Y, AB[1] - 57]; // world position of the origin const W = (x, y, z) => [SYD[0] - x, SYD[1] + y, SYD[2] - z]; // the bridge: 503 m arch span, crown 134 m, deck 49 m, pylons 89 m const BR = { c: [-52, -62], dir: [0.19, -0.982], half: 31.4, deck: 6.1, gap: 1.9, len: 71.8 }; // CBD towers: [x, z, width, depth, height, kind] (positions from lat/long) const TOWERS = [ [-74, 190, 2, 2, 38.6, 'centrepoint'], [-157, 62, 6, 5, 33.9, 'crown'], [-49, 69, 6, 5, 25, 'qqt'], [-42, 101, 5, 5, 31.7, 'gpt'], [-52, 124, 5, 5, 30.5, 'step'], [-60, 145, 5, 5, 28.5, 'oct'], [-90, 107, 5, 5, 21, 'round'], [-44, 120, 4, 4, 30, 'blade'], [-50, 88, 4, 3.5, 17, 'box'], [-140, 75, 6, 5, 27, 'box'], [-138, 92, 5, 5, 22, 'box'], [-136, 108, 5, 5, 21, 'box'], [-38, 58, 4, 4, 14.6, 'box'], [-75, 70, 6, 5, 22.5, 'box'], [-80, 228, 6, 5, 30, 'box'], [-92, 258, 5, 5, 28, 'box'], ]; // The Opera House shells: [hall x, half width, faces (+1 north), back base, tip u, tip y, front base] const SHELLS = [ [-2.6, 2.4, -1, -3.2, -6.6, 6.2, -5.6], [-2.6, 2.4, 1, -5.0, 0.9, 8.4, -1.0], [-2.6, 2.3, 1, -1.9, 3.2, 7.0, 1.6], [-2.6, 2.2, 1, 0.4, 5.2, 5.4, 3.8], [-2.6, 2.1, 1, 2.4, 6.9, 3.9, 6.0], [2.9, 2.1, -1, -3.6, -6.7, 5.4, -5.8], [2.9, 2.1, 1, -5.3, 0.4, 7.4, -1.4], [2.9, 2.0, 1, -2.3, 2.7, 6.1, 1.2], [2.9, 1.9, 1, 0.0, 4.6, 4.7, 3.4], [2.9, 1.8, 1, 2.0, 6.2, 3.4, 5.4], [-4.8, 1.3, -1, -7.4, -9.8, 3.0, -9.2], [-4.8, 1.2, 1, -9.0, -7.1, 2.4, -7.8], ]; const PODIUM = { x0: -7.6, x1: 7.6, u0: -11.6, u1: 11.2, y: 1.4 }; // fireworks: barges in Farm Cove and off Bennelong Point [x, z, burst height, palette] const BURSTS = [[22, 12, 20, 0], [36, 30, 26, 1], [12, 34, 16, 2], [46, 4, 18, 3], [-4, 26, 23, 4], [30, -10, 14, 5], [56, 22, 21, 6]]; function makeSydney(G) { const N = G.N; // --- the skyline: north and west faces of every tower, as lit windows const towers = TOWERS.slice(); for (let k = 0; k < 44; k++) { // the rest of the CBD const x = -128 + rnd(k, 31) * 104, z = 62 + rnd(k, 32) * 210; if (towers.some(T => Math.abs(T[0] - x) < 7 && Math.abs(T[1] - z) < 7)) continue; towers.push([x, z, 3 + rnd(k, 33) * 3.5, 3 + rnd(k, 34) * 3, 7 + rnd(k, 35) ** 1.5 * 17, 'box']); } for (let k = 0; k < 14; k++) towers.push([-74 + rnd(k, 41) * 58, -176 - rnd(k, 42) * 40, 4, 4, 8 + rnd(k, 43) * 11, 'box']); // North Sydney for (let k = 0; k < 16; k++) towers.push([-70 + k * 3.4, 26 + rnd(k, 44) * 8, 3, 3, 2.5 + rnd(k, 45) * 3.5, 'box']); // The Rocks // faces as rectangles [x, z, dx, dz, w, h, tower, normal yaw] const faces = []; towers.forEach((T, ti) => { const [x, z, w, d, h, kind] = T; if (kind === 'centrepoint') return; const taper = kind === 'crown' ? 0.85 : 1; faces.push([x - w / 2, z - d / 2, 1, 0, w * taper, h, ti, Math.PI]); // north face, looking -z faces.push([x - w / 2, z - d / 2, 0, 1, d * taper, h, ti, -Math.PI / 2]); // west face, looking -x }); const area = faces.reduce((s, f) => s + f[4] * f[5], 0); const nSky = Math.round(N * 0.22), nSpire = Math.round(N * 0.012); const cell = Math.sqrt(area / ((nSky - nSpire) * 0.62)); const SKY = []; for (const [x, z, dx, dz, w, h, ti, yaw] of faces) { const cols = Math.max(1, Math.round(w / (cell * 0.62))), rows = Math.max(1, Math.round(h / cell)); for (let r = 0; r < rows; r++) for (let c = 0; c < cols; c++) { const u = (cols - c - 0.5) / cols * w; // the harbour is turned: read left to right SKY.push([x + dx * u, (r + 0.5) / rows * h, z + dz * u, h / rows, yaw, ti, r, c]); } } // Sydney Tower: spindle, golden turret and spire const ct = TOWERS[0]; for (let k = 0; k < nSpire; k++) { const f = k / nSpire, y = f * ct[4], turret = y > 29 && y < 33.4, r = turret ? 2.6 : y > 33.4 ? 0.35 : 0.9; const a = rnd(k, 51) * Math.PI - Math.PI; // the half that faces the harbour SKY.push([ct[0] + r * Math.cos(a), y, ct[1] + r * Math.sin(a), turret ? 0.7 : 0.6, 0, 0, turret ? -1 : -2, k]); } SKY.length = Math.min(SKY.length, nSky); // --- the Harbour Bridge: two arch trusses, hangers, deck, pylons const BRG = [], dens = Math.sqrt(32000 / N); // spacing grows as the glyph count falls const ax = BR.dir[0], az = BR.dir[1], ex = -az, ez = ax; // along the span, and across const at = (s, side, y) => [BR.c[0] + ax * s * BR.half + ex * side, y, BR.c[1] + az * s * BR.half + ez * side]; const yb = s => 2.0 + 12.5 * (1 - s * s), yt = s => yb(s) + 2.0 + 5.2 * s * s; const line = (p, q, kind, sweep) => { const L = Math.hypot(q[0] - p[0], q[1] - p[1], q[2] - p[2]), n = Math.max(2, Math.round(L / (0.42 * dens))); for (let k = 0; k < n; k++) { const f = (k + 0.5) / n; BRG.push([mix(p[0], q[0], f), mix(p[1], q[1], f), mix(p[2], q[2], f), kind, mix(sweep[0], sweep[1], f), Math.atan2(q[1] - p[1], Math.hypot(q[0] - p[0], q[2] - p[2]))]); } }; const PAN = 28; for (const side of [-BR.gap, BR.gap]) { for (let k = 0; k < PAN; k++) { const s0 = -1 + 2 * k / PAN, s1 = -1 + 2 * (k + 1) / PAN; line(at(s0, side, yt(s0)), at(s1, side, yt(s1)), 0, [s0, s1]); // top chord line(at(s0, side, yb(s0)), at(s1, side, yb(s1)), 1, [s0, s1]); // bottom chord line(at(s0, side, yb(s0)), at(s0, side, yt(s0)), 2, [s0, s0]); // vertical line(k % 2 ? at(s0, side, yt(s0)) : at(s0, side, yb(s0)), k % 2 ? at(s1, side, yb(s1)) : at(s1, side, yt(s1)), 2, [s0, s1]); if (yb(s0) > BR.deck + 0.4) line(at(s0, side, BR.deck), at(s0, side, yb(s0)), 3, [s0, s0]); // hangers } } for (const side of [-3.05, 3.05]) { // deck, end to end line(at(-BR.len / BR.half, side, BR.deck), at(BR.len / BR.half, side, BR.deck), 4, [-1.4, 1.4]); line(at(-BR.len / BR.half, side, BR.deck - 0.55), at(BR.len / BR.half, side, BR.deck - 0.55), 4, [-1.4, 1.4]); } for (let k = -9; k <= 9; k++) { // approach piers const s = k / 4.1; if (Math.abs(s) < 1.25 || Math.abs(s) > BR.len / BR.half) continue; line(at(s, 0, 0), at(s, 0, BR.deck - 0.6), 5, [s, s]); } // the four granite pylons, each a stepped tower faced in glyphs const PYL = []; for (const s of [-1.1, 1.1]) for (const side of [-2.4, 2.4]) { const [px, , pz] = at(s, side, 0); for (let y = 0.3; y < 11.1; y += 0.62 * dens) { const w = y > 9.6 ? 1.2 : 1.55; for (let u = -w; u <= w; u += 0.38 * dens) { PYL.push([px + ax * u * 1.6 - ex * 1.25, y, pz + az * u * 1.6 - ez * 1.25, s, side]); PYL.push([px - ex * u * 0.9 - ax * 1.6 * w * Math.sign(s), y, pz - ez * u * 0.9 - az * 1.6 * w * Math.sign(s), s, side]); } } } // --- the Opera House, from its west elevation. Each shell is a sail: a // convex back ridge rising from its pedestal to a tip that leans out over // the mouth, and a mouth edge falling to the podium. Shells cascade from // the tallest, over the stage, down toward the harbour; one faces south. // [faces, back base u, tip u, tip y, mouth base u], u = metres north / 8 const HALLS = [ { x: -3.4, s: [[1, -5.0, 0.0, 8.4, -1.3], [1, -2.0, 2.7, 7.0, 1.4], [1, 0.2, 4.9, 5.5, 3.6], [1, 2.2, 7.0, 4.0, 5.8], [-1, -3.0, -7.1, 6.0, -5.9]] }, { x: 2.2, s: [[1, -4.5, 0.4, 7.4, -0.8], [1, -1.5, 3.1, 6.1, 1.8], [1, 0.6, 5.3, 4.8, 4.0], [1, 2.6, 7.3, 3.5, 6.1], [-1, -2.7, -6.7, 5.3, -5.5]] }, { x: -5.0, s: [[-1, -7.4, -10.3, 3.0, -9.5], [1, -9.7, -6.8, 2.4, -7.8]] }, ]; const quad = (a, c, b, s) => (1 - s) ** 2 * a + 2 * (1 - s) * s * c + s * s * b; const sails = []; HALLS.forEach((H, hi) => H.s.forEach(([dir, u0, ut, yt, u1], k) => { const y0 = PODIUM.y, pts = []; for (let s = 0; s <= 1.0001; s += 1 / 24) pts.push(quad(u0, u0 + (ut - u0) * 0.32, ut, s), quad(y0, y0 + (yt - y0) * 1.0, yt, s)); for (let s = 1 / 16; s <= 1.0001; s += 1 / 16) pts.push(quad(ut, u1 - dir * 0.25, u1, s), quad(yt, y0 + (yt - y0) * 0.42, y0, s)); sails.push({ hall: hi, x: H.x, pts, dir, u0, ut, yt, id: sails.length }); })); const pod = (u, y) => Math.max(Math.abs(u - (PODIUM.u0 + PODIUM.u1) / 2) - (PODIUM.u1 - PODIUM.u0) / 2, Math.abs(y - PODIUM.y / 2) - PODIUM.y / 2); const owner = (u, y) => { for (const S of sails) if (sdPoly(u, y, S.pts) < 0) return S; // west hall first, in order of prominence return null; }; const elev = (u, y) => Math.min(pod(u, y), ...sails.map(S => sdPoly(u, y, S.pts))); const EF = fillShape(elev, [-12.2, 0, 11.8, 8.8], Math.round(N * 0.145), 0.62); const OP = []; for (let k = 0; k < EF.n; k++) { const u = EF.x[k], y = EF.y[k], S = owner(u, y); if (!S) { OP.push([PODIUM.x0, y, -u, -1, 0, 0, -Math.PI / 2, 0, EF.h]); continue; } const edge = sdPoly(u, y, S.pts) > -0.07 ? 1 : 0; const rib = Math.atan2(y - PODIUM.y, (u - S.u0) * S.dir) / (Math.PI * 0.62); const bulge = Math.sqrt(Math.max(0, -sdPoly(u, y, S.pts))) * 0.9; OP.push([S.x - bulge, y, -u, S.id, rib, 1 - (y - PODIUM.y) / (S.yt - PODIUM.y), -Math.PI / 2, 0, EF.h, edge]); } // --- the crowd on the western broadwalk, phones held up const nCrowd = Math.round(N * 0.028); // --- fireworks const nFire = Math.round(N * 0.2); const nWater = N - SKY.length - BRG.length - PYL.length - OP.length - nCrowd - nFire; const R = allot(N, G.mark('function makeSydney'), [nWater, SKY.length, BRG.length, PYL.length, OP.length, nCrowd, nFire]); // the harbour: dense along the camera's glide, coarse far away; no land const glide = [[-135, -72], [-112, -68], [-72, -62], [-50, -58], [-30, -40], [-20, -26]]; const land = (x, z) => (z > -27 && x < -36) || (z > 17 && x < -7) || (x > -8.5 && x < 9 && z > -12.5) || z < -96 || (x > 9 && z > 34); const WT = waterTiles(-230, -150, 70, 60, nWater, (x, z) => (land(x, z) ? -1 : Math.hypot(segDist(x, z, glide), 3.5))); const water = hex(0x0B1636), waterHi = hex(0x9CB7FF), win = hex(0xFFD9A0), winCool = hex(0xBFD4FF); const towerC = hex(0x16224A), gold = hex(0xFFC35C), steel = hex(0x7D88A6), lamp = hex(0xFFE3A3); const granite = hex(0xC9B28C), tile = hex(0xF6EEDC), phone = hex(0xCFE6FF), person = hex(0x2A3155); // fireworks over the harbour function firework(j, t, o) { const nb = BURSTS.length, b = j % nb, k = Math.floor(j / nb), per = Math.ceil(nFire / nb); const [bx, bz, bh, pal] = BURSTS[b], at2 = 25.0 + (b % 3) * 0.12; const launch = at2 - 0.6; const base = W(bx, 0.2, bz); if (t < launch) { // waiting on the barge [o.x, o.y, o.z] = [base[0] + rnd(j, 91) * 0.6, base[1] + 0.1, base[2] + rnd(j, 92) * 0.6]; o.s = 0.2; o.a = 0; o.bill = 1; tint(o, gold, 0); o.w = 0.5; return; } if (t < at2) { // the rocket and its trail const u = ramp(t, launch, at2), trail = k < 36, lag = k / 36 * 0.3, h = bh * outCubic(sat(u - lag)); [o.x, o.y, o.z] = [base[0], base[1] + h, base[2]]; o.s = 0.35; o.bill = 1; tint(o, gold, trail ? 2.2 * (1 - k / 36) : 0); o.a = trail ? 1 : 0; o.w = 0.9; return; } const tau = t - at2; const ga = k * 2.39996, y1 = 1 - 2 * (k + 0.5) / per, rr = Math.sqrt(1 - y1 * y1); const sp = (bh * 0.42) * (0.75 + 0.25 * rnd(j, 93)); const ex2 = outExpo(sat(tau / 0.45)); o.x = base[0] + Math.cos(ga) * rr * sp * ex2; o.y = base[1] + bh + y1 * sp * ex2 - 4 * tau * tau; o.z = base[2] + Math.sin(ga) * rr * sp * ex2; o.s = 0.55; o.bill = 1; o.roll = ga; const col = VIVID[(pal + (k % 3 === 0 ? 3 : 0)) % VIVID.length]; const fade = Math.exp(-tau * 1.6) * (0.75 + 0.25 * Math.sin(tau * 40 + j)); tint2(o, col, [1, 1, 1], bump(tau, 0.03, 0.05), 3 * fade + 0.15); o.a = sat(tau / 0.08); o.w = 0.6 + 0.4 * fade; } // the message: falling fireworks gather into "Find the source" in solid // glyph-filled letters over the harbour, the skyline glowing beneath const MSG = (() => { if (!self.MSG_MASK) { // drawn once, then shared with the workers const cv = document.createElement('canvas'); cv.width = 1024; cv.height = 640; const g = cv.getContext('2d'); g.fillStyle = '#fff'; g.textAlign = 'center'; g.textBaseline = 'middle'; g.font = `800 205px ${ATLAS.font}`; g.lineJoin = 'round'; g.lineWidth = 18; g.strokeStyle = '#fff'; for (const [w, y] of [['Find the', 190], ['source', 440]]) { g.fillText(w, 512, y); g.strokeText(w, 512, y); } const d = g.getImageData(0, 0, 1024, 640).data; self.MSG_MASK = Uint8Array.from({ length: 1024 * 640 }, (_, i) => d[i * 4 + 3]); } const px = self.MSG_MASK; const cover = (x, y) => px[clamp(Math.round(y), 0, 639) * 1024 + clamp(Math.round(x), 0, 1023)] / 255; return fillShape((x, y) => 0.5 - cover((x + 1) * 512, (0.625 - y) * 512), [-1, -0.625, 1, 0.625], nFire, 0.6); })(); const MCAM = W(-24, 9, -98), MDIR = (() => { const d = W(-46, 20, 40).map((v, i) => v - MCAM[i]), l = Math.hypot(...d); return d.map(v => v / l); })(); const MC = MCAM.map((v, i) => v + MDIR[i] * 72), MR = [-MDIR[2], 0, MDIR[0]].map(v => v / Math.hypot(MDIR[0], MDIR[2])); const MS = 22; function message(j, t, o) { const x = MSG.x[j] * MS, y = MSG.y[j] * MS + 15 + 0.4 * Math.sin(t * 2 + MSG.x[j] * 3); o.x = MC[0] + MR[0] * x; o.y = MC[1] + y; o.z = MC[2] + MR[2] * x; o.s = MSG.h * MS * 0.98; o.bill = 1; o.roll = 0; o.yaw = 0; o.pitch = 0; o.a = 1; const wave = Math.max(0, Math.sin(MSG.x[j] * 4 - t * 3)) ** 3; tint2(o, hex(0xFFF3DC), vivid(G.cls[i0(j)] + 1), wave * 0.5, 1.25 + wave * 0.8); o.w = 0.85 + 0.25 * wave; } const i0 = j => j; const FA = {}, FB = {}; function fireMsg(j, t, o) { const g0 = 25.75 + (j / nFire) * 0.4 + rnd(j, 95) * 0.06, gu = ramp(t, g0, g0 + 0.6); if (gu >= 1) return message(j, t, o); if (gu <= 0) return firework(j, t, o); firework(j, t, FA); message(j, t, FB); const e = smoother(gu), arc = Math.sin(Math.PI * e); blendState(o, FA, FB, e); o.y += arc * 4; o.r *= 1 + arc; o.g *= 1 + arc; o.b *= 1 + arc; } return { roles: R, msgX: j => MSG.x[j], fire: 6, frame() {}, glyph(i, t, o) { this.place(i, t, o); o.yaw += Math.PI; }, place(i, t, o) { const j = R.idx[i], role = R.role[i]; o.s = 1; o.a = 1; o.w = 0.4; o.bill = 0; o.roll = 0; o.yaw = 0; o.pitch = 0; switch (role) { case 0: { // the harbour const x = WT.x[j], z = WT.z[j]; const sw = Math.sin(0.25 * x + 0.1 * z - 1.9 * t) * 0.5 + Math.sin(0.17 * z - 0.08 * x - 2.6 * t) * 0.5; [o.x, o.y, o.z] = W(x, 0.05 * sw, z); o.s = WT.s[j] * 0.95; o.pitch = Math.PI / 2; const sp = Math.max(0, sw) ** 4; // the city's light lies on the water in vertical streaks const glow = (z > 0 ? 0.6 : 0.25) * Math.max(0, noise(x * 0.4, z * 0.05, t * 0.8)); const fire = t > 24.95 ? Math.exp(-(t - 25.0) * 1.3) * sat((t - 24.95) * 8) * 4 * Math.max(0, noise(x * 0.3, z * 0.3, t * 3) + 0.2) : 0; tint2(o, water, waterHi, sat(sp * 0.6 + glow + fire * 0.4), 0.8 + 1.4 * (sp + glow) + fire); o.w = 0.15 + 0.6 * sat(sp + glow); return; } case 1: { // the skyline const [x, y, z, hh, yaw, ti, r, c] = SKY[j]; const T = towers[ti] || towers[0]; const rise = smoother(ramp(t, 19.45 + rnd(ti, 71) * 0.6, 20.6 + rnd(ti, 71) * 0.6)); [o.x, o.y, o.z] = W(x, y * rise, z); o.s = (hh > 0 ? hh : 0.6) * 0.95; o.yaw = yaw; if (ti < 0) { // Sydney Tower o.bill = 0.7; tint(o, ti === -1 ? gold : lamp, ti === -1 ? 2.2 : 0.9); o.w = ti === -1 ? 1 : 0.5; return; } const lit = rnd(ti * 977 + r * 31 + c, 73) > (T[5] === 'box' ? 0.55 : 0.45); const on = lit && t > 20.4 + rnd(ti * 97 + r, 74) * 1.0; if (on) { tint(o, rnd(ti, 75) > 0.3 ? win : winCool, (1.5 + rnd(r + c, 76) * 0.8) * (1 + 0.5 * ramp(t, 25.8, 26.6))); o.w = 0.8; } else { tint(o, towerC, 1.1); o.w = 0.18; } if (r === Math.round(T[4] / hh) - 1 || T[5] === 'gpt' && y > T[4] - 3) { tint(o, T[5] === 'crown' ? gold : lamp, 1.6); o.w = 1; } return; } case 2: { // the bridge draws itself const [x, y, z, kind, s, slope] = BRG[j]; const front = kind >= 4 ? mix(-1.6, 1.6, smoother(ramp(t, 20.4, 21.3))) : mix(-1.05, 1.05, smoother(ramp(t, 21.2, 22.4))); const e = smoother(sat((front - s) * 6)); [o.x, o.y, o.z] = W(x, mix(0, y, e), z); o.s = 0.46 * dens; o.yaw = Math.atan2(-az, ax) + Math.PI; o.roll = kind === 2 || kind === 3 || kind === 5 ? Math.PI / 2 : -slope; const head = bump(front - s, 0.06, 0.06); // the drawing front glows const flood = 0.7 + 0.6 * (1 - sat(y / 17)); // floodlit from below const bulb = (kind === 0 || kind === 4) && j % 6 === 0; // lamp strings if (bulb) { tint(o, lamp, 3.2 + head * 3); o.w = 1.0; o.s *= 1.15; } else if (kind === 0 || kind === 1) { tint(o, steel, flood * 1.15 + head * 4); o.w = 0.7; } else if (kind === 4) { tint2(o, steel, lamp, 0.25, flood * 1.2 + head * 3); o.w = 0.75; } else { tint(o, steel, flood * 0.85 + head * 4); o.w = 0.45; } o.a = e > 0.01 ? 1 : 0; return; } case 3: { // pylons const [x, y, z] = PYL[j]; const rise = smoother(ramp(t, 20.3, 21.1)); [o.x, o.y, o.z] = W(x, y * rise, z); o.s = 0.5 * dens; o.yaw = Math.atan2(-az, ax) + Math.PI / 2; o.bill = 0.3; const cornice = y > 9.4 && y < 10.2, slit = Math.abs(((y * 1.3) % 2.6) - 1.3) < 0.25 && Math.abs(((j * 0.37) % 1) - 0.5) < 0.12; tint(o, granite, cornice ? 1.25 : slit ? 0.25 : 0.55 + 0.5 * (1 - sat(y / 11))); o.w = cornice ? 0.85 : 0.5; return; } case 4: { // Opera House const [x, y, z, si, sv, q, yaw, pitch, size, edge] = OP[j]; if (si < 0) { [o.x, o.y, o.z] = W(x, y * smoother(ramp(t, 21.6, 22.2)), z); o.s = size; o.yaw = yaw; tint(o, granite, 0.42 + 0.25 * (y / PODIUM.y)); o.w = 0.45; return; } const rise = smoother(ramp(t, 22.2 + si * 0.06, 23.1 + si * 0.06)); [o.x, o.y, o.z] = W(x, mix(PODIUM.y, y, rise), z); o.s = size * (0.4 + 0.6 * rise); o.yaw = yaw; o.pitch = pitch; o.bill = 0.35; const rib = Math.abs(((sv * 9 + 0.5) % 1) - 0.5) < 0.1; // floodlit tiles: a soft chevron of matte and gloss, brighter low down const tileK = 0.85 + 0.15 * Math.sign(Math.sin(sv * 60 + (1 - q) * 25)); let k = (0.66 + 0.3 * q) * tileK * (edge ? 0.5 : 1); let c = tile; o.w = rib ? 0.85 : 0.6; // the projection show const ribs = ramp(t, 23.95, 24.1) * (1 - ramp(t, 24.3, 24.42)); if (ribs > 0 && rib) { const run = bump((1 - q) - (t - 23.35) * 1.4 % 1, 0, 0.12); c = VIVID[6]; k += ribs * (1 + 5 * run); o.w = 1; } const climb = ramp(t, 24.25 + si * 0.03, 24.45 + si * 0.03) * (1 - ramp(t, 24.62, 24.72)); if (climb > (1 - q) * 0.999 && climb > 0) { c = VIVID[si % VIVID.length]; k = 1.4; o.w = 0.9; } const kal = ramp(t, 24.55, 24.7) * (1 - ramp(t, 25.1, 25.35)); if (kal > 0) { const px = x + 0.0, py = y - 4.5, a = Math.atan2(py, px + (z + 1) * 0.4), rr = Math.hypot(px, py); const spin = (t - 24.6) * (4 + (t - 24.6) * 10); const fold = Math.abs(((a + spin) / (TAU / 8)) % 2 - 1); const band = Math.floor((rr * 1.4 - (t - 24.55) * 6 + fold * 2) % VIVID.length + VIVID.length) % VIVID.length; c = VIVID[band]; k = mix(k, 0.9 + fold * 1.2, kal); o.w = 0.5 + 0.5 * fold; } const flash = bump(t, 25.02, 0.06); tint(o, c, k + flash * 1.5); return; } case 5: { // crowd and phones const row = j % 3, k = Math.floor(j / 3); const per = Math.ceil(nCrowd / 3), zz = mix(-10.5, 9, (Math.floor(k / 2) + rnd(j, 81) * 0.6) / (per / 2)); const isPhone = k % 2 === 1; const x = -8.1 - row * 0.32 - rnd(j, 82) * 0.15; const sway = Math.sin(t * 3 + j) * 0.04; const up = ramp(t, 22.4 + rnd(j, 83) * 0.8, 22.7 + rnd(j, 83) * 0.8); const appear = smoother(ramp(t, 21.8, 22.4)); [o.x, o.y, o.z] = W(x + sway, (isPhone ? 0.55 + 0.32 * up : 0.42) * appear + 0.25, zz); o.s = isPhone ? 0.16 : 0.32; o.yaw = -Math.PI / 2; o.bill = 0.8; if (isPhone) { tint(o, phone, up * (0.8 + 0.6 * Math.max(0, Math.sin(t * 7 + j)))); o.w = 0.9; } else { tint(o, person, 1.0); o.w = 0.6; } return; } default: return fireMsg(j, t, o); // fireworks, then the message } }, }; } // ------------------------------------------------- choreography // Blend two glyph states. Angles take the short way round. function blendState(o, A, B, e) { o.x = mix(A.x, B.x, e); o.y = mix(A.y, B.y, e); o.z = mix(A.z, B.z, e); o.s = mix(A.s, B.s, e); o.a = mix(A.a, B.a, e); o.w = mix(A.w, B.w, e); o.r = mix(A.r, B.r, e); o.g = mix(A.g, B.g, e); o.b = mix(A.b, B.b, e); o.bill = mix(A.bill, B.bill, e); o.roll = A.roll + angDiff(A.roll, B.roll) * e; o.yaw = A.yaw + angDiff(A.yaw, B.yaw) * e; o.pitch = mix(A.pitch, B.pitch, e); } // ================================================================ ENGINE // The film's timeline: each scene hands over to the next as the glyphs // fly, staggered one by one, so there is never a cut. const RAIN = 0, SPLASH = 1, SHATTER = 2, FLATTEN = 3, HOME = 4; const STAGE_Y = VIEW_H / 2 + 8; // just above frame one // the camera: [t, position, target, fov, aperture, hold] function cameraKeys() { return [ [0.0, [0, 0, CAM0], [0, 0, 0], FOV0, 0, 1], [ENTER, [0, 0, CAM0], [0, 0, 0], FOV0, 0, 1], // DEEP: follow the rain down and plunge in [2.6, [0, -24, 104], [0, -48, 0], 32, 0.15], [3.6, [3, -80, 84], [2, -98, 0], 34, 0.3], [4.5, [-6, -94, 70], [-4, -100, 14], 34, 0.45], [5.2, [-4, -100, 58], [4, -104, -12], 34, 0.4], [7.6, [16, -92, 50], [38, -82, -4], 36, 0.3], [8.55, [24, -66, 56], [50, -54, -4], 38, 0.25], // MENAGERIE [9.4, [26, -46, 66], [44, -36, -4], 38, 0.2], [10.4, [41.5, -56, 18], [41, -31, 3.5], 38, 0.15], [10.85, [30, -40, 50], [32, -48, 0], 36, 0.2], [11.4, [22, -48, 64], [34, -48, 0], 36, 0.25], [12.4, [28, -50, 56], [40, -50, 0], 36, 0.25], [13.4, [40, -48, 54], [50, -46, 0], 36, 0.25], [14.35, [46, -16, 12], [46, -60, -4], 38, 0.2], [15.6, [46, -9, 4], [46, -60, -5], 40, 0.15], // ABSTRACTION [16.0, [46, -6, 2], [46, -60, -5], 40, 0.15], [17.1, [46, -2, -4], [46, -60, -5.5], 40, 0.15], [18.5, [46, -36, -5], [46, -60, -6], 44, 0.2], [19.2, [44, -44, -4], [42, -60, -26], 44, 0.2], // SYDNEY: the skyline, the bridge, under it, up to the sails [20.1, W(-128, 3.2, -46), W(-72, 12, 110), 38, 0.2], [20.9, W(-127, 3.1, -50), W(-66, 12, 100), 38, 0.2], [21.7, W(-128, 3.0, -56), W(-56, 9, -62), 38, 0.2], [22.5, W(-96, 2.8, -60), W(-20, 6, -30), 38, 0.25], [23.2, W(-54, 2.3, -58), W(0, 5, -8), 40, 0.25], [24.0, W(-44, 4.0, -8), W(0, 4.6, -1), 40, 0.3], [24.6, W(-35, 5.0, -4), W(0, 4.6, 0), 42, 0.3], [25.15, W(-40, 7.5, -22), W(12, 12, 8), 46, 0.2], // MESSAGE: pull back across the harbour [26.3, W(-24, 9, -98), W(-46, 20, 40), 46, 0.08], [28.35, W(-26, 9.5, -103), W(-46, 20, 40), 46, 0.08], // RETURN [29.1, [-14, -22, 84], [-4, -10, -20], 36, 0.05], [29.62, [0, 0, CAM0], [0, 0, 0], FOV0, 0, 1], [30.0, [0, 0, CAM0], [0, 0, 0], FOV0, 0, 1], ]; } // the look of each world: [t, sky top, sky bottom, bloom, fog colour, fog density, mirror] const LOOK = [ [0.0, 0x070D24, 0x050A1C, 0.22, 0x050A1C, 0, 0], [1.8, 0x070D24, 0x050A1C, 0.3, 0x050A1C, 0, 0], [3.0, 0x0B3150, 0x020812, 0.6, 0x0A2C44, 0.009, 0], [8.2, 0x0B3150, 0x020812, 0.65, 0x0A2C44, 0.009, 0], [9.0, 0x0A1238, 0x060A20, 0.6, 0x0A1030, 0.0035, 0.42], [9.7, 0x0A1238, 0x060A20, 0.6, 0x0A1030, 0.0035, 0.3], [10.1, 0x1A2A5C, 0x0A1230, 0.6, 0x24345E, 0.004, 0], [10.75, 0x1A2A5C, 0x0A1230, 0.6, 0x24345E, 0.004, 0], [11.1, 0x2A1838, 0x6A3420, 0.65, 0x4A2A30, 0.004, 0], [11.75, 0x2A1838, 0x6A3420, 0.65, 0x4A2A30, 0.004, 0], [12.1, 0x1C1A24, 0x3A2A1C, 0.55, 0x3A2E26, 0.006, 0], [12.75, 0x1C1A24, 0x3A2A1C, 0.55, 0x3A2E26, 0.006, 0], [13.1, 0x06201A, 0x0A2A1E, 0.6, 0x0E3426, 0.007, 0], [13.75, 0x06201A, 0x0A2A1E, 0.6, 0x0E3426, 0.007, 0], [14.2, 0x061418, 0x0A2228, 0.6, 0x0A2228, 0.003, 0], [15.8, 0x061418, 0x0A2228, 0.6, 0x0A2228, 0.003, 0], [16.2, 0x0B0726, 0x04030C, 0.85, 0x0B0620, 0.002, 0], [18.6, 0x0B0726, 0x04030C, 0.85, 0x0B0620, 0.002, 0], [19.8, 0x050A20, 0x111D48, 0.75, 0x0D1838, 0.0035, 0.45], [24.8, 0x050A20, 0x111D48, 0.8, 0x0D1838, 0.0035, 0.45], [25.15, 0x0A0A28, 0x1A1E4A, 1.2, 0x0D1838, 0.0035, 0.5], [26.0, 0x060A22, 0x111D48, 0.85, 0x0D1838, 0.003, 0.4], [28.4, 0x060A22, 0x111D48, 0.85, 0x0D1838, 0.003, 0.4], [29.3, 0x070D24, 0x050A1C, 0.3, 0x050A1C, 0, 0], [30.0, 0x070D24, 0x050A1C, 0.22, 0x050A1C, 0, 0], ]; function lookAt(t) { let k = 0; while (k < LOOK.length - 2 && LOOK[k + 1][0] <= t) k++; const a = LOOK[k], b = LOOK[k + 1], e = smooth(ramp(t, a[0], b[0])); const c = (x, y) => hex(x).map((v, i) => mix(v, hex(y)[i], e)); return { top: c(a[1], b[1]), bot: c(a[2], b[2]), bloom: mix(a[3], b[3], e), fog: c(a[4], b[4]), fogDen: mix(a[5], b[5], e), mirror: mix(a[6], b[6], e) }; } // Hermite through the camera keys; held keys have zero velocity so the // film starts and ends on a still frame. function cameraTrack(keys) { const n = keys.length, vec = (k, f) => keys[k][f]; const tangent = (k, f) => { if (keys[k][6]) return [0, 0, 0]; const a = keys[Math.max(k - 1, 0)], b = keys[Math.min(k + 1, n - 1)]; return [0, 1, 2].map(c => (b[f][c] - a[f][c]) / (b[0] - a[0])); }; const T = keys.map((_, k) => [tangent(k, 1), tangent(k, 2)]); return t => { let k = 0; while (k < n - 2 && keys[k + 1][0] <= t) k++; const a = keys[k], b = keys[k + 1], h = b[0] - a[0], u = clamp((t - a[0]) / h, 0, 1); const u2 = u * u, u3 = u2 * u, h00 = 2 * u3 - 3 * u2 + 1, h10 = u3 - 2 * u2 + u, h01 = -2 * u3 + 3 * u2, h11 = u3 - u2; const H = f => [0, 1, 2].map(c => h00 * vec(k, f)[c] + h10 * h * T[k][f - 1][c] + h01 * vec(k + 1, f)[c] + h11 * h * T[k + 1][f - 1][c]); const e = smooth(u); return { pos: H(1), tgt: H(2), fov: mix(a[3], b[3], e), ap: mix(a[4], b[4], e) }; }; } // Editor chrome: tab, line numbers and status bar, also made of glyphs. function makeChrome(G, ed) { const items = []; const ink = hex(0xEDF1FF), dim = hex(0x3A4676), mid = hex(0x7C89B8), caret = hex(0xFFD466); const text = (str, x, y, col, w) => [...str].forEach((ch, k) => { if (ch !== ' ') items.push([x + k * CW, y, ch.charCodeAt(0) - 33, col, w]); }); const top = VIEW_H / 2 - 1.05 * LH, left = -VIEW_W / 2 + 1.5 * CW; text('source.html', left + 1.6, top, ink, 0.62); items.push([left + 0.4, top, '*'.charCodeAt(0) - 33, caret, 0.9]); text('x', left + 1.6 + 12.5 * CW, top, mid, 0.3); text('120 BPM', VIEW_W / 2 - 8.5 * CW, top, mid, 0.3); const rowLine = new Map(); G.lineRow.forEach((r, l) => rowLine.set(r, l)); for (let r = 0; r < CODE_ROWS; r++) { const row = G.row0 + r, line = rowLine.get(row); if (line) text(String(line - G.line0 + 1).padStart(5), -VIEW_W / 2 + 0.3 * CW, ROW_Y0 - r * LH, line === G.runLine ? mid : dim, 0.22); } text(`SOURCE Ln ${G.runLine - G.line0 + 1}, Col 6 UTF-8 JavaScript ${G.N} glyphs`, left, -VIEW_H / 2 + 0.85 * LH, mid, 0.3); items.push([ed.caretX, ed.caretY, '|'.charCodeAt(0) - 33, caret, 0.75]); return { n: items.length, items, glyph(k, t, o) { const [x, y, code, col, w] = items[k]; o.x = x; o.y = y; o.z = 0; o.s = 1; o.w = w; o.bill = 0; o.roll = 0; o.yaw = 0; o.pitch = 0; tint(o, col, 1); const out = t < 15 ? ramp(t, ENTER + 0.05, ENTER + 0.6) : 1 - ramp(t, 29.0, 29.4); o.a = 1 - out; o.z += 8 * out; o.y += 2 * out; if (k === items.length - 1) { // the caret const typed = t < 15 ? TYPE_AT.filter(a => t >= a).length : UNTYPE_AT.filter(a => t < a).length; o.x = ed.caretX + typed * CW; o.s = 1.12; tint(o, col, 1.6); o.a = t < 15 ? 1 - ramp(t, ENTER + 0.02, ENTER + 0.12) : ramp(t, 29.3, 29.36) * (t < 29.9 ? 1 : 0); o.z = 0; o.y = y; } }, }; } // Everything that decides where the glyphs are. It touches no WebGL, so the // same code also runs in workers, each evaluating its own share of glyphs. function buildFilm(text, glyphs, aspect = 0.8) { const G = layoutSource(text, glyphs), N = G.N; const editor = makeEditor(G), deep = makeDeep(G), menag = makeMenagerie(G); const abstr = makeAbstraction(G, aspect), syd = makeSydney(G), chrome = makeChrome(G, editor); const SEGS = [ { a: editor, b: deep, t0: ENTER, kind: RAIN }, { a: deep, b: menag, t0: 8.0, kind: SPLASH }, { a: menag, b: abstr, t0: 15.75, kind: SHATTER }, { a: abstr, b: syd, t0: 18.76, kind: FLATTEN }, { a: syd, b: editor, t0: 28.45, kind: HOME }, ]; // per-glyph start and duration of every hand-off, fixed at load const above = G.off; const maxRow = G.row[N - 1], winEnd = G.row0 + CODE_ROWS; const isMsg = i => syd.roles.role[i] === syd.fire; const shard = new Float32Array(N * 4); const tmp = {}; for (const S of SEGS) { S.start = new Float32Array(N); S.dur = new Float32Array(N); } SEGS[1].a.frame(8.45); SEGS[2].a.frame(16.0); SEGS[3].a.frame(18.84); const seeds = Array.from({ length: 40 }, (_, k) => [POND[0] + (rnd(k, 1) - 0.5) * 60, POND[2] + (rnd(k, 2) - 0.5) * 60]); const c0 = G.hx[0], c1 = G.hx[Math.max(0, G.runGlyph - 1)]; // line 1: "// Find the source" for (let i = 0; i < N; i++) { const f = above(i) ? sat((G.row[i] - winEnd) / Math.max(1, maxRow - winEnd)) : 0; SEGS[0].start[i] = above(i) ? 1.6 + f * 0.9 + rnd(i, 21) * 0.1 : editor.ripple[i] + 0.12; SEGS[0].dur[i] = above(i) ? 1.2 : 1.3; deep.glyph(i, 8.45, tmp); SEGS[1].start[i] = 8.12 + Math.min(Math.hypot(tmp.x - 54, tmp.y - SEA_Y, tmp.z) / 90, 0.6); SEGS[1].dur[i] = 0.7; menag.glyph(i, 16.0, tmp); const isKoi = menag.roles.role[i] === 0; const dP = Math.hypot(tmp.x - POND[0], tmp.z - POND[2]); SEGS[2].start[i] = isKoi ? 15.82 + rnd(i, 22) * 0.1 : 15.98 + Math.min(dP / 160, 0.2); SEGS[2].dur[i] = 0.7; let best = 1e9, sx = 0, sz = 0, sk = 0; seeds.forEach(([x, z], k) => { const d = Math.hypot(tmp.x - x, tmp.z - z); if (d < best) { best = d; sx = x; sz = z; sk = k; } }); const l = Math.hypot(sx - POND[0], sz - POND[2]) + 1; shard[i * 4] = (sx - POND[0]) / l * 14; shard[i * 4 + 1] = 10 + rnd(sk, 3) * 14; shard[i * 4 + 2] = (sz - POND[2]) / l * 14; shard[i * 4 + 3] = (rnd(sk, 4) - 0.5) * 6; abstr.glyph(i, 18.84, tmp); SEGS[3].start[i] = 18.84 + sat(Math.hypot(tmp.x - AB[0], tmp.z - AB[1]) / 45) * 0.4; SEGS[3].dur[i] = 0.75; // home: top lines first; the message last, into line 1 const fr = above(i) ? rnd(i, 23) : sat((G.row[i] - G.row0) / CODE_ROWS); SEGS[4].start[i] = 28.5 + fr * 0.45 + rnd(i, 24) * 0.05; SEGS[4].dur[i] = 0.72; if (isMsg(i)) { SEGS[4].start[i] = 28.85 + rnd(i, 27) * 0.3; SEGS[4].dur[i] = 0.7; } if (G.row[i] === G.row0) { SEGS[4].start[i] = 29.15; SEGS[4].dur[i] = 0.55; } } // the message pours into the comment on line 1 and is absorbed there function intoLine1(B, i) { const j = syd.roles.idx[i]; B.x = mix(c0, c1, (syd.msgX(j) + 1) / 2); B.y = G.hy[0]; B.z = 0; B.s = 0; B.bill = 0; B.roll = 0; B.yaw = 0; B.pitch = 0; B.a = 1; } function stage(o, i) { // off-frame glyphs wait just above it o.y = STAGE_Y + rnd(i, 25) * 40; o.z = -rnd(i, 26) * 24; o.s = 1; } function fly(kind, i, A, B, e, o) { blendState(o, A, B, e); const arc = Math.sin(Math.PI * e); let glow = 0; switch (kind) { case RAIN: o.x += arc * (rnd(i, 11) - 0.5) * 14; o.z += arc * (rnd(i, 12) - 0.5) * 24; o.s *= 1 + arc * 0.3; o.bill = mix(o.bill, 1, arc); o.roll = mix(o.roll, Math.PI / 2, arc); glow = 0.15; break; case SPLASH: o.y += arc * (8 + 18 * rnd(i, 13)); o.x += arc * (rnd(i, 14) - 0.5) * 16; o.z += arc * (rnd(i, 15) - 0.5) * 16; o.roll += arc * (rnd(i, 16) - 0.5) * 5; glow = 0.2; o.a *= 1 - 0.65 * arc; break; case SHATTER: o.x += arc * shard[i * 4]; o.y += arc * shard[i * 4 + 1]; o.z += arc * shard[i * 4 + 2]; o.roll += arc * shard[i * 4 + 3]; glow = 1.0; break; case FLATTEN: { const dx = o.x - AB[0], dz = o.z - AB[1], a = arc * 0.7, c = Math.cos(a), s = Math.sin(a); o.x = AB[0] + dx * c - dz * s; o.z = AB[1] + dx * s + dz * c; o.y += arc * 3; glow = 0.6; break; } default: { // home, in a murmuration const hx = G.hx[i] * 0.05, hy = G.hy[i] * 0.05; o.x += arc * noise(hx, hy, 1.7) * 30; o.y += arc * (noise(hx, hy, 4.2) * 22 + 6); o.z += arc * noise(hx, hy, 8.9) * 30; o.bill = mix(o.bill, 1, arc); glow = 0.25; o.a *= 1 - 0.45 * arc; } } const g = 1 + arc * glow; o.r *= g; o.g *= g; o.b *= g; o.w = Math.min(o.w + arc * 0.25, 1.1); } // instance buffers const M = N + chrome.n; const iPos = new Float32Array(M * 4), iCol = new Float32Array(M * 4), iA = new Float32Array(M * 4), iB = new Float32Array(M * 4); for (let i = 0; i < N; i++) iA[i * 4 + 1] = G.code[i]; for (let k = 0; k < chrome.n; k++) iA[(N + k) * 4 + 1] = chrome.items[k][2]; const o = {}, A = {}, B = {}; function write(i, q) { const p = i * 4; const bad = !(q.x === q.x && q.y === q.y && q.z === q.z && q.s > 0); iPos[p] = q.x; iPos[p + 1] = q.y; iPos[p + 2] = q.z; iPos[p + 3] = bad ? 0 : q.s; iCol[p] = q.r; iCol[p + 1] = q.g; iCol[p + 2] = q.b; iCol[p + 3] = q.a; iA[p] = q.w; iA[p + 2] = q.roll; iA[p + 3] = q.bill; iB[p] = q.yaw; iB[p + 1] = q.pitch; } function evaluate(t, i0 = 0, i1 = N, withChrome = true) { const tt = t < SEGS[0].t0 ? t + LOOP : t; let k = SEGS.length - 1; for (let s = 0; s < SEGS.length; s++) if (tt >= SEGS[s].t0) k = s; const S = SEGS[k]; S.a.frame(t); S.b.frame(t); for (let i = i0; i < i1; i++) { const u = (tt - S.start[i]) / S.dur[i]; if (u <= 0) S.a.glyph(i, t, o); else if (u >= 1) S.b.glyph(i, t, o); else { S.a.glyph(i, t, A); S.b.glyph(i, t, B); if (above(i)) { if (S.kind === RAIN) stage(A, i); if (S.kind === HOME) { if (isMsg(i)) intoLine1(B, i); else stage(B, i); } } fly(S.kind, i, A, B, smoother(u), o); } write(i, o); } if (withChrome) evaluateChrome(t); } function evaluateChrome(t) { for (let c = 0; c < chrome.n; c++) { chrome.glyph(c, t, o); write(N + c, o); } } // camera, with tracking shots layered over the spline const track = cameraTrack(cameraKeys()); const RIGS = [ { t0: 3.0, t1: 5.1, r: 0.5, f: t => { const p = deep.turtle(t); return { pos: [p[0] - 7, p[1] + 26, p[2] + 30], tgt: [p[0] + 2, p[1] - 1, p[2] - 1], ap: 0.3 }; } }, { t0: 5.3, t1: 7.2, r: 0.55, f: t => { const e = deep.eye(t); return { pos: [e[0] - 2.5, e[1] + 1.2, e[2] + 8.5], tgt: e, ap: 0.3 }; } }, { t0: 9.55, t1: 11.05, r: 0.55, f: t => { const e = menag.manta(t); return { pos: [e[0] + 0.5, e[1] - 25, e[2] + 13], tgt: [e[0], e[1], e[2] - 1.5], ap: 0.15 }; } }, { t0: 10.55, t1: 11.95, r: 0.7, f: t => { const h = menag.giraffe(t); return { pos: [h[0] - 10, h[1] + 1, 64], tgt: [h[0] + 2, h[1] + 0.5, 0], ap: 0.3 }; } }, { t0: 11.85, t1: 12.95, r: 0.3, f: t => { const h = menag.roo(t); return { pos: [h[0] - 12, h[1] + 1, 52], tgt: [h[0] + 4, h[1] - 1, 0], ap: 0.3 }; } }, { t0: 12.8, t1: 14.05, r: 0.45, f: t => { const p = menag.tiger(t); return { pos: [p[0] - 12, p[1] + 5, 56], tgt: [p[0] + 3, p[1] + 1, 0], ap: 0.3 }; } }, ]; // On a wide screen the editor is framed by its width: line 1 at the top, // code filling the screen. The film itself is untouched at 4:5. let wideK = 1; function setAspect(a) { wideK = Math.max(1, a / 0.8); } function cameraAt(t) { const c = track(t); const ed = wideK > 1 ? Math.max(1 - smooth(ramp(t, ENTER + 0.1, ENTER + 1.0)), smooth(ramp(t, 29.0, 29.62))) : 0; if (ed > 0) { const tf = Math.tan(FOV0 * Math.PI / 360), d = CAM0 / wideK, top = VIEW_H / 2 - d * tf; // = the poster, width-fitted from the top c.pos = c.pos.map((v, i) => mix(v, [0, top, d][i], ed)); c.tgt = c.tgt.map((v, i) => mix(v, [0, top, 0][i], ed)); } for (const R of RIGS) { const w = smoother(ramp(t, R.t0, R.t0 + R.r)) * (1 - smoother(ramp(t, R.t1 - R.r, R.t1))); if (w <= 0) continue; const q = R.f(t); c.pos = c.pos.map((v, i) => mix(v, q.pos[i], w)); c.tgt = c.tgt.map((v, i) => mix(v, q.tgt[i], w)); c.ap = mix(c.ap, q.ap, w); } c.focus = Math.hypot(c.tgt[0] - c.pos[0], c.tgt[1] - c.pos[1], c.tgt[2] - c.pos[2]); return c; } const stats = 'syd roles ' + [0, 1, 2, 3, 4, 5, 6].map(r => syd.roles.role.filter(x => x === r).length).join(','); setAspect(aspect); return { G, N, M, iPos, iCol, iA, iB, evaluate, evaluateChrome, cameraAt, setAspect, stats, aspect }; } async function createFilm(canvas, opt = {}) { const THREE = await import('three'); const { EffectComposer } = await import('three/addons/postprocessing/EffectComposer.js'); const { RenderPass } = await import('three/addons/postprocessing/RenderPass.js'); const { UnrealBloomPass } = await import('three/addons/postprocessing/UnrealBloomPass.js'); const { ShaderPass } = await import('three/addons/postprocessing/ShaderPass.js'); const [text] = await Promise.all([readSource(), loadFont()]); const F = buildFilm(text, opt.glyphs || 32000, opt.aspect || 0.8); const { G, N, M, iPos, iCol, iA, iB, evaluate, evaluateChrome, cameraAt, stats } = F; // play at once on this thread; hand over to the worker pool when it is ready let pool = null; const poolReady = opt.workers > 1 ? startPool(text, F, opt.workers).then(p => { pool = p; return p; }).catch(() => null) : Promise.resolve(null); // ---------------------------------------------------------- three.js const renderer = new THREE.WebGLRenderer({ canvas, antialias: false, alpha: false, preserveDrawingBuffer: !!opt.preserve, powerPreference: 'high-performance' }); renderer.setClearColor(0x000000, 1); renderer.setPixelRatio(opt.pixelRatio || 1); const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(FOV0, 0.8, 0.3, 4000); const atlas = buildAtlas(); const tex = new THREE.DataTexture(atlas.data, atlas.W, atlas.H, THREE.RedFormat, THREE.UnsignedByteType); tex.minFilter = THREE.LinearMipmapLinearFilter; tex.magFilter = THREE.LinearFilter; tex.generateMipmaps = true; tex.unpackAlignment = 1; tex.anisotropy = renderer.capabilities.getMaxAnisotropy(); tex.needsUpdate = true; const geo = new THREE.InstancedBufferGeometry(); geo.setAttribute('position', new THREE.BufferAttribute(new Float32Array([-0.5, -0.5, 0, 0.5, -0.5, 0, 0.5, 0.5, 0, -0.5, 0.5, 0]), 3)); geo.setIndex([0, 1, 2, 0, 2, 3]); const attr = (name, arr) => { const b = new THREE.InstancedBufferAttribute(arr, 4); b.setUsage(THREE.DynamicDrawUsage); geo.setAttribute(name, b); return b; }; const bufs = [attr('iPos', iPos), attr('iCol', iCol), attr('iA', iA), attr('iB', iB)]; geo.instanceCount = M; const uniforms = { uAtlas: { value: tex }, uPx: { value: 1000 }, uFocus: { value: 100 }, uAperture: { value: 0 }, uNear: { value: 0.8 }, uFogStart: { value: 30 }, uFogDen: { value: 0 }, uFog: { value: new THREE.Vector3() }, uMirror: { value: 0 }, uMirrorAmt: { value: 0 }, uSeaY: { value: SEA_Y }, uTime: { value: 0 }, }; const material = mirror => new THREE.RawShaderMaterial({ glslVersion: THREE.GLSL3, vertexShader: GLYPH_VS, fragmentShader: GLYPH_FS, uniforms: { ...uniforms, uMirror: { value: mirror } }, transparent: true, depthTest: false, depthWrite: false, blending: THREE.CustomBlending, blendEquation: THREE.AddEquation, blendSrc: THREE.OneFactor, blendDst: THREE.OneFactor, }); const glyphs = new THREE.Mesh(geo, material(0)), mirror = new THREE.Mesh(geo, material(1)); glyphs.frustumCulled = mirror.frustumCulled = false; scene.add(mirror, glyphs); const target = new THREE.WebGLRenderTarget(4, 4, { type: THREE.HalfFloatType }); const composer = new EffectComposer(renderer, target); composer.addPass(new RenderPass(scene, camera)); const bloom = new UnrealBloomPass(new THREE.Vector2(4, 4), 0.5, 0.55, 0.8); composer.addPass(bloom); const grade = new ShaderPass({ uniforms: { tDiffuse: { value: null }, uBgTop: { value: new THREE.Vector3() }, uBgBot: { value: new THREE.Vector3() }, uVig: { value: 0.55 }, uExposure: { value: 1 }, uSeed: { value: 0 } }, vertexShader: 'varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }', fragmentShader: GRADE_FS, }); composer.addPass(grade); let pxH = 4, aspect = 0.8; function resize(w, h) { aspect = w / h; F.setAspect(aspect); renderer.setSize(w, h, false); composer.setPixelRatio(renderer.getPixelRatio()); composer.setSize(w, h); pxH = h * renderer.getPixelRatio(); } const setU = (k, v) => { glyphs.material.uniforms[k].value = v; mirror.material.uniforms[k].value = v; }; function render(t) { t = ((t % LOOP) + LOOP) % LOOP; if (!pool) { evaluate(t); draw(t); return; } pool.ask(t); // workers evaluate; we draw their latest frame if (pool.ready !== null) { evaluateChrome(pool.ready); draw(pool.ready); } } function draw(t) { bufs.forEach(b => { b.needsUpdate = true; }); const c = cameraAt(t); // wide screens keep the film's full height; tall screens keep its full width const fov = aspect >= 0.8 ? c.fov : 2 * Math.atan(Math.tan(c.fov * Math.PI / 360) * 0.8 / aspect) * 180 / Math.PI; camera.position.set(...c.pos); camera.fov = fov; camera.aspect = aspect; camera.up.set(0, 1, 0); camera.lookAt(...c.tgt); camera.updateProjectionMatrix(); const L = lookAt(t); setU('uPx', pxH / (2 * Math.tan(fov * Math.PI / 360))); setU('uFocus', c.focus); setU('uAperture', c.ap); setU('uTime', t); setU('uNear', mix(0.8, 6.0, ramp(t, 19.2, 19.9) * (1 - ramp(t, 28.5, 29.0)))); setU('uFogDen', L.fogDen); glyphs.material.uniforms.uFog.value.set(...L.fog); mirror.material.uniforms.uFog.value.set(...L.fog); setU('uMirrorAmt', L.mirror); mirror.visible = L.mirror > 0.01; bloom.strength = L.bloom; grade.uniforms.uBgTop.value.set(...L.top); grade.uniforms.uBgBot.value.set(...L.bot); grade.uniforms.uSeed.value = Math.round(t * FPS) % 97; composer.render(); } return { render, draw, resize, renderer, G, N, M, evaluate, cameraAt, stats, poolReady, get pool() { return pool; } }; } // A pool of workers, each running this same script and evaluating its own // slice of the glyphs; the main thread only draws. Results are identical // to evaluating on one thread, because every scene is a pure function of t. function startPool(text, F, n) { const a = text.indexOf('