Branchwell

Become a Pro / full-access-films · becomeapro.ai / video / compositions / field.html

Raw 25.6 KB on dc219e5391ebe41a7beacd8a2fe698e1aa27522e
1<template>
2 <style>
3 #root {
4 position: absolute;
5 inset: 0;
6 background: #0d1117;
7 }
8 #field-canvas {
9 position: absolute;
10 left: 0;
11 top: 0;
12 width: 1920px;
13 height: 1080px;
14 }
15 </style>
16 <div data-hf-id="hf-qkep" id="root" data-composition-id="field" data-width="1920" data-height="1080">
17 <canvas data-hf-id="hf-vfy6" id="field-canvas" width="1920" height="1080" data-layout-ignore></canvas>
18 </div>
19 <script>
20 (() => {
21 // One WebGL field of outlined triangles, ported from the site's home page. Every frame is a function of
22 // time alone: window.FIELD_CUES says which shape the particles hold, and when they burst into the next.
23 const N = 24000, NA = Math.floor(N * 0.1), M = N - NA;
24 const PX_PER_UNIT = 540 / (12 * Math.tan(Math.PI / 9));
25
26 const seeded = (seed) => () => {
27 seed = (seed + 0x6d2b79f5) | 0;
28 let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
29 t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
30 return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
31 };
32
33 // Sample points from anything drawn on a canvas. Output height is 1, centred on the origin.
34 function maskPts(draw, W, H, seed, depth = 0.1) {
35 const R = seeded(seed);
36 const c = document.createElement("canvas");
37 c.width = W;
38 c.height = H;
39 const g = c.getContext("2d", { willReadFrequently: true });
40 g.fillStyle = "#fff";
41 g.strokeStyle = "#fff";
42 draw(g, W, H, R);
43 const d = g.getImageData(0, 0, W, H).data, px = [];
44 for (let y = 0; y < H; y += 2) for (let x = 0; x < W; x += 2) if (d[(y * W + x) * 4 + 3] > 128) px.push(x, y);
45 const out = new Float32Array(M * 3), n = px.length / 2;
46 for (let i = 0; i < M; i++) {
47 const k = Math.floor(R() * n) * 2;
48 out[i * 3] = (px[k] + R() * 2 - W / 2) / H;
49 out[i * 3 + 1] = (H / 2 - px[k + 1] - R() * 2) / H;
50 out[i * 3 + 2] = (R() - 0.5) * depth;
51 }
52 return out;
53 }
54
55 const gaussian = (R) => {
56 let u = 0, v = 0;
57 while (!u) u = R();
58 while (!v) v = R();
59 return Math.sqrt(-2 * Math.log(u)) * Math.cos(6.2832 * v);
60 };
61
62 // Console windows tiled edge to edge: the old way
63 const pages = (cols, rows) => (g, W, H, R) => {
64 const w = W / cols, h = H / rows, k = h / 180;
65 for (let r = 0; r < rows; r++) for (let c = 0; c < cols; c++) {
66 const x = c * w + (11 + R() * 8) * k, y = r * h + (11 + R() * 8) * k, bw = w - 30 * k, bh = h - 30 * k;
67 g.lineWidth = Math.max(2, 4 * k);
68 g.strokeRect(x, y, bw, bh);
69 g.fillRect(x, y, bw, 18 * k);
70 const lines = 3 + Math.floor(R() * 4);
71 for (let l = 0; l < lines; l++) g.fillRect(x + 18 * k, y + (38 + l * 18) * k, (bw - 36 * k) * (0.3 + R() * 0.65), 6 * k);
72 }
73 };
74
75 const promptGlyph = (g, W, H) => {
76 g.font = `600 ${H * 0.72}px "Geist Mono"`;
77 g.textAlign = "center";
78 g.textBaseline = "middle";
79 g.fillText(">_", W / 2, H / 2);
80 };
81
82 function drawGpu(g, x, y, w, h) {
83 const lw = h * 0.05, fanR = h * 0.3;
84 g.lineWidth = lw;
85 g.beginPath();
86 g.roundRect(x, y, w, h, h * 0.07);
87 g.stroke();
88 for (let f = 0; f < 3; f++) {
89 const cx = x + w * (0.2 + f * 0.3), cy = y + h * 0.5;
90 g.lineWidth = lw * 0.75;
91 g.beginPath();
92 g.arc(cx, cy, fanR, 0, 6.2832);
93 g.stroke();
94 g.beginPath();
95 g.arc(cx, cy, fanR * 0.2, 0, 6.2832);
96 g.fill();
97 for (let b = 0; b < 9; b++) {
98 const a = (b / 9) * 6.2832;
99 g.lineWidth = lw * 0.65;
100 g.beginPath();
101 g.moveTo(cx + Math.cos(a) * fanR * 0.24, cy + Math.sin(a) * fanR * 0.24);
102 g.quadraticCurveTo(cx + Math.cos(a + 0.5) * fanR * 0.7, cy + Math.sin(a + 0.5) * fanR * 0.7, cx + Math.cos(a + 0.95) * fanR * 0.92, cy + Math.sin(a + 0.95) * fanR * 0.92);
103 g.stroke();
104 }
105 }
106 for (let p = 0; p < 12; p++) g.fillRect(x + w * 0.14 + p * w * 0.042, y + h + lw, w * 0.03, h * 0.09);
107 }
108 const gpu = (g, W, H) => drawGpu(g, W * 0.06, H * 0.2, W * 0.88, H * 0.56);
109
110 // Agent, link, GPU, for the three pieces
111 const pieces = (g, W, H) => {
112 g.font = `600 ${H * 0.5}px "Geist Mono"`;
113 g.textAlign = "center";
114 g.textBaseline = "middle";
115 g.fillText(">_", W * 0.13, H * 0.5);
116 g.lineWidth = H * 0.03;
117 for (let i = 0; i < 2; i++) {
118 g.setLineDash([H * 0.06, H * 0.05]);
119 g.beginPath();
120 g.moveTo(W * (i ? 0.545 : 0.255), H * 0.5);
121 g.lineTo(W * (i ? 0.615 : 0.445), H * 0.5);
122 g.stroke();
123 g.setLineDash([]);
124 }
125 g.lineWidth = H * 0.05;
126 g.beginPath();
127 g.arc(W * 0.5, H * 0.5, H * 0.2, 0, 6.2832);
128 g.stroke();
129 g.beginPath();
130 g.arc(W * 0.5, H * 0.5, H * 0.07, 0, 6.2832);
131 g.fill();
132 drawGpu(g, W * 0.635, H * 0.2, W * 0.34, H * 0.56);
133 };
134
135 // Prompt straight to the GPU, with nothing in between
136 const direct = (g, W, H) => {
137 g.font = `600 ${H * 0.5}px "Geist Mono"`;
138 g.textAlign = "center";
139 g.textBaseline = "middle";
140 g.fillText(">_", W * 0.13, H * 0.5);
141 g.lineWidth = H * 0.03;
142 g.setLineDash([H * 0.06, H * 0.05]);
143 g.beginPath();
144 g.moveTo(W * 0.27, H * 0.5);
145 g.lineTo(W * 0.62, H * 0.5);
146 g.stroke();
147 g.setLineDash([]);
148 drawGpu(g, W * 0.635, H * 0.2, W * 0.34, H * 0.56);
149 };
150
151 const server = (g, W, H) => {
152 g.lineWidth = H * 0.06;
153 g.beginPath();
154 g.arc(W / 2, H / 2, H * 0.3, 0, 6.2832);
155 g.stroke();
156 g.beginPath();
157 g.arc(W / 2, H / 2, H * 0.11, 0, 6.2832);
158 g.fill();
159 g.lineWidth = H * 0.04;
160 g.setLineDash([H * 0.07, H * 0.06]);
161 [[0.02, 0.5, 0.17, 0.5], [0.83, 0.5, 0.98, 0.5]].forEach(([x1, y1, x2, y2]) => {
162 g.beginPath();
163 g.moveTo(W * x1, H * y1);
164 g.lineTo(W * x2, H * y2);
165 g.stroke();
166 });
167 g.setLineDash([]);
168 };
169
170 function drawCube(g, cx, cy, r) {
171 const dx = r * 0.87, dy = r * 0.5;
172 g.lineWidth = r * 0.06;
173 g.lineJoin = "round";
174 g.beginPath();
175 g.moveTo(cx, cy - r);
176 g.lineTo(cx + dx, cy - dy);
177 g.lineTo(cx + dx, cy + dy);
178 g.lineTo(cx, cy + r);
179 g.lineTo(cx - dx, cy + dy);
180 g.lineTo(cx - dx, cy - dy);
181 g.closePath();
182 g.moveTo(cx - dx, cy - dy);
183 g.lineTo(cx, cy);
184 g.lineTo(cx + dx, cy - dy);
185 g.moveTo(cx, cy);
186 g.lineTo(cx, cy + r);
187 g.stroke();
188 }
189
190 function drawDisks(g, cx, cy, w, h, levels) {
191 g.lineWidth = h * 0.13;
192 for (let l = levels; l >= 0; l--) {
193 const y = cy + l * h * 0.95;
194 g.beginPath();
195 g.ellipse(cx, y, w / 2, h / 2, 0, 0, l === 0 ? 6.2832 : 3.1416);
196 g.stroke();
197 }
198 g.beginPath();
199 g.moveTo(cx - w / 2, cy);
200 g.lineTo(cx - w / 2, cy + levels * h * 0.95);
201 g.moveTo(cx + w / 2, cy);
202 g.lineTo(cx + w / 2, cy + levels * h * 0.95);
203 g.stroke();
204 }
205 const podVolume = (g, W, H) => {
206 drawCube(g, W * 0.5, H * 0.3, H * 0.26);
207 drawDisks(g, W * 0.5, H * 0.72, W * 0.58, H * 0.11, 2);
208 };
209 const volumeOnly = (g, W, H) => drawDisks(g, W * 0.5, H * 0.72, W * 0.58, H * 0.11, 2);
210
211 const quiet = (g, W, H, R) => {
212 for (let x = 0; x < W; x += 16) {
213 const h = 6 + R() * 12;
214 g.fillRect(x, H / 2 - h / 2, 8, h);
215 }
216 };
217
218 function modelAndCardPts() {
219 const brain = brainPts(18), card = maskPts(gpu, 1000, 420, 19), out = new Float32Array(M * 3);
220 for (let i = 0; i < M; i++) {
221 const isBrain = i % 5 < 3, source = isBrain ? brain : card, s = isBrain ? 0.62 : 1.02, shift = isBrain ? -1.55 : 1.7;
222 out[i * 3] = source[i * 3] * s + shift;
223 out[i * 3 + 1] = source[i * 3 + 1] * s;
224 out[i * 3 + 2] = source[i * 3 + 2] * s;
225 }
226 return out;
227 }
228
229 // The Impossible A, straight from youtube/lockup-dark.svg
230 const mark = (g, W, H) => {
231 g.save();
232 g.scale(W / 64, H / 64);
233 ["M3 49.94 L7.14 57.11 L56.86 57.11 L36.14 21.24 L32 28.41 L44.43 49.94Z", "M56.86 57.11 L61 49.94 L36.14 6.89 L15.43 42.76 L23.71 42.76 L36.14 21.24Z", "M36.14 6.89 L27.86 6.89 L3 49.94 L44.43 49.94 L40.29 42.76 L15.43 42.76Z"].forEach((d) => g.fill(new Path2D(d)));
234 g.restore();
235 };
236
237 // Filled rectangles laid out in frame pixels, so they line up with the DOM labels over them
238 function rectsPts(spec, seed) {
239 const R = seeded(seed), rects = spec.split(";").map((r) => r.split(",").map(Number));
240 const areas = rects.map(([, , w, h]) => w * h), total = areas.reduce((a, b) => a + b, 0);
241 const out = new Float32Array(M * 3);
242 for (let i = 0; i < M; i++) {
243 let pick = R() * total, k = 0;
244 while (pick > areas[k]) pick -= areas[k++];
245 const [x, y, w, h] = rects[k];
246 out[i * 3] = (x + R() * w - 960) / PX_PER_UNIT;
247 out[i * 3 + 1] = (540 - y - R() * h) / PX_PER_UNIT;
248 out[i * 3 + 2] = (R() - 0.5) * 0.04;
249 }
250 return out;
251 }
252
253 function brainPts(seed) {
254 const R = seeded(seed), W = 400, H = 300;
255 const maskField = (draw) => {
256 const c = document.createElement("canvas");
257 c.width = W;
258 c.height = H;
259 const g = c.getContext("2d", { willReadFrequently: true });
260 g.fillStyle = "#fff";
261 draw(g);
262 const a = g.getImageData(0, 0, W, H).data, D = new Float32Array(W * H), pts = [];
263 for (let i = 0; i < W * H; i++) D[i] = a[i * 4 + 3] > 127 ? 1e9 : 0;
264 for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) {
265 const i = y * W + x;
266 if (!D[i]) continue;
267 let d = D[i];
268 d = x > 0 ? Math.min(d, D[i - 1] + 1) : Math.min(d, 1);
269 if (y > 0) {
270 d = Math.min(d, D[i - W] + 1);
271 if (x > 0) d = Math.min(d, D[i - W - 1] + 1.414);
272 if (x < W - 1) d = Math.min(d, D[i - W + 1] + 1.414);
273 } else d = Math.min(d, 1);
274 D[i] = d;
275 }
276 let max = 1;
277 for (let y = H - 1; y >= 0; y--) for (let x = W - 1; x >= 0; x--) {
278 const i = y * W + x;
279 if (!D[i]) continue;
280 let d = D[i];
281 d = x < W - 1 ? Math.min(d, D[i + 1] + 1) : Math.min(d, 1);
282 if (y < H - 1) {
283 d = Math.min(d, D[i + W] + 1);
284 if (x < W - 1) d = Math.min(d, D[i + W + 1] + 1.414);
285 if (x > 0) d = Math.min(d, D[i + W - 1] + 1.414);
286 } else d = Math.min(d, 1);
287 D[i] = d;
288 if (d > max) max = d;
289 pts.push(i);
290 }
291 return { D, pts, max };
292 };
293 const segDist = (px, py, line) => {
294 let best = 1e9;
295 for (let k = 0; k < line.length - 1; k++) {
296 const [ax, ay] = line[k], [bx, by] = line[k + 1], vx = bx - ax, vy = by - ay;
297 const t = Math.max(0, Math.min(1, ((px - ax) * vx + (py - ay) * vy) / (vx * vx + vy * vy)));
298 const dx = px - ax - vx * t, dy = py - ay - vy * t;
299 best = Math.min(best, dx * dx + dy * dy);
300 }
301 return Math.sqrt(best);
302 };
303 const cer = maskField((g) => {
304 g.beginPath();
305 g.moveTo(36, 150);
306 g.bezierCurveTo(30, 95, 62, 48, 125, 33);
307 g.bezierCurveTo(190, 18, 265, 22, 312, 50);
308 g.bezierCurveTo(358, 78, 386, 125, 378, 170);
309 g.bezierCurveTo(372, 198, 352, 214, 326, 216);
310 g.lineTo(296, 213);
311 g.bezierCurveTo(272, 212, 258, 226, 244, 236);
312 g.bezierCurveTo(205, 252, 150, 252, 118, 236);
313 g.bezierCurveTo(98, 226, 90, 206, 104, 193);
314 g.lineTo(118, 186);
315 g.bezierCurveTo(92, 186, 60, 182, 44, 170);
316 g.bezierCurveTo(38, 165, 35, 158, 36, 150);
317 g.closePath();
318 g.fill();
319 });
320 const cbl = maskField((g) => {
321 g.beginPath();
322 g.ellipse(314, 240, 58, 33, -0.12, 0, 6.2832);
323 g.fill();
324 });
325 const stem = maskField((g) => {
326 g.beginPath();
327 g.moveTo(236, 228);
328 g.lineTo(268, 230);
329 g.lineTo(278, 298);
330 g.lineTo(252, 299);
331 g.closePath();
332 g.fill();
333 });
334 const sylvian = [[116, 188], [170, 160], [228, 136], [262, 122], [276, 108]];
335 const central = [[206, 24], [196, 70], [186, 110], [176, 150]];
336 const fold = (x, y, z) => {
337 const s = 3.2, qx = x * s, qy = y * s, qz = z * s;
338 const wx = qx + 0.9 * Math.sin(qy * 1.3 + qz * 0.7), wy = qy + 0.9 * Math.sin(qz * 1.1 + qx * 0.8), wz = qz + 0.9 * Math.sin(qx * 1.2 + qy * 0.9);
339 return Math.sin(wx * 1.1 + wy * 0.3) * Math.cos(wy - wz * 0.6) + 0.6 * Math.sin(wz * 1.3 + wx * 0.5 + wy * 0.4);
340 };
341 const pick = (f) => {
342 const p = f.pts[Math.floor(R() * f.pts.length)];
343 return { p, x: (p % W) + R(), y: Math.floor(p / W) + R() };
344 };
345 const out = new Float32Array(M * 3), k = 3 / 345, gap = 4;
346 let i = 0;
347 while (i < M) {
348 const r = R();
349 let x, y, z;
350 if (r < 0.84) {
351 const q = pick(cer);
352 x = q.x;
353 y = q.y;
354 const dn = cer.D[q.p] / cer.max;
355 let h = 140 * Math.pow(dn, 0.42);
356 if (dn < 0.16 && R() < 0.22) h *= R();
357 const side = R() < 0.5 ? -1 : 1;
358 const ds = Math.min(segDist(x, y, sylvian), segDist(x, y, central) * 1.3);
359 if (ds < 4.5 && R() < 0.93) continue;
360 let groove = 1 - Math.min(1, Math.abs(fold(x / 100, y / 100, (gap + h) / 100)) / 0.42);
361 groove = groove * groove * (3 - 2 * groove);
362 if (groove > 0.45 && R() < 0.95) continue;
363 h = Math.max(0, h - groove * 22);
364 z = side * (gap + h);
365 } else if (r < 0.95) {
366 const q = pick(cbl);
367 x = q.x;
368 y = q.y;
369 const f = Math.abs(Math.sin((y - 0.12 * x) * 0.42));
370 if (f < 0.28 && R() < 0.8) continue;
371 z = (R() < 0.5 ? -1 : 1) * 96 * Math.pow(cbl.D[q.p] / cbl.max, 0.5);
372 } else {
373 const q = pick(stem);
374 x = q.x;
375 y = q.y;
376 z = (R() < 0.5 ? -1 : 1) * 22 * Math.pow(stem.D[q.p] / stem.max, 0.5);
377 }
378 out[i * 3] = (x - 210) * k;
379 out[i * 3 + 1] = -(y - 160) * k;
380 out[i * 3 + 2] = z * k;
381 i++;
382 }
383 return out;
384 }
385
386 function dustPts(seed) {
387 const R = seeded(seed), out = new Float32Array(M * 3);
388 for (let i = 0; i < M; i++) {
389 out[i * 3] = (R() - 0.5) * 2.4;
390 out[i * 3 + 1] = (R() - 0.5) * 1.4;
391 out[i * 3 + 2] = (R() - 0.5) * 1.2;
392 }
393 return out;
394 }
395
396 const named = {
397 pages1: () => maskPts(pages(1, 1), 960, 540, 11, 0.16),
398 pages2: () => maskPts(pages(2, 2), 960, 540, 11, 0.16),
399 pages3: () => maskPts(pages(4, 3), 960, 540, 11, 0.16),
400 pages4: () => maskPts(pages(7, 5), 960, 540, 11, 0.16),
401 prompt: () => maskPts(promptGlyph, 540, 360, 12),
402 gpu: () => maskPts(gpu, 1000, 420, 13),
403 pieces: () => maskPts(pieces, 1200, 400, 14),
404 mark: () => maskPts(mark, 512, 512, 15, 0.04),
405 brain: () => brainPts(16),
406 dust: () => dustPts(17),
407 direct: () => maskPts(direct, 1200, 400, 20),
408 server: () => maskPts(server, 620, 500, 21),
409 podvol: () => maskPts(podVolume, 800, 800, 22),
410 volume: () => maskPts(volumeOnly, 800, 800, 23),
411 quiet: () => maskPts(quiet, 1440, 200, 24),
412 modelcard: modelAndCardPts,
413 };
414 const shapeFor = (id, seed) => (id.startsWith("rects:") ? rectsPts(id.slice(6), seed) : named[id]());
415
416 const VERTEX = `
417 precision highp float;
418 uniform float uTime, uProgress, uStagger, uPx, uBurst, uSnap, uDimA, uDimB, uScaleA, uScaleB, uQuantA, uQuantB, uSpinA, uSpinB, uFlatA, uFlatB;
419 uniform vec2 uWave;
420 uniform vec3 uOffA, uOffB;
421 uniform mat3 uRot;
422 uniform mat4 uProj;
423 attribute vec3 aFrom, aTo;
424 attribute vec4 aRand;
425 attribute float aAmb;
426 varying vec3 vColor;
427 varying float vAlpha, vRot;
428 vec3 spinY(vec3 p, float a) { float c = cos(a), s = sin(a); return vec3(p.x * c + p.z * s, p.y, -p.x * s + p.z * c); }
429 void main() {
430 // Each particle starts its move at a delay set by where it sits, so a change sweeps across the frame
431 vec3 a = spinY(aFrom, uSpinA), b = spinY(aTo, uSpinB);
432 float delay = clamp(0.5 + dot(a.xy * uScaleA + uOffA.xy, uWave) / 15.0, 0.0, 1.0) * uStagger;
433 float m = smoothstep(delay, delay + 1.0 - uStagger, uProgress);
434 float body = 1.0 - aAmb, flat_ = mix(uFlatA, uFlatB, m);
435 vec3 p = aFrom;
436 if (aAmb < 0.5) {
437 vec3 local = mix(a, b, m);
438 float q = m < 0.5 ? uQuantA : uQuantB;
439 if (q > 0.0) local = floor(local / q + 0.5) * q;
440 local *= mix(uScaleA, uScaleB, m);
441 p = uRot * local + mix(uOffA, uOffB, m);
442 }
443 float arc = (uSnap > 0.5 && m > 0.5) ? cos(pow((m - 0.5) * 2.0, 2.0) * 1.5708) : sin(m * 3.14159);
444 float burst = pow(max(arc, 0.0), 1.25) * body * uBurst;
445 vec3 dir = normalize(vec3(aRand.x - 0.5, aRand.y - 0.5, aRand.z - 0.5) + 0.0001);
446 float sw = burst * (aRand.y - 0.5) * 2.4, cs = cos(sw), sn = sin(sw);
447 vec2 fl = vec2(dir.x * cs - dir.y * sn, dir.x * sn + dir.y * cs);
448 p += vec3(fl * (1.7 + aRand.w * 4.2), 0.55 + aRand.w * 3.6) * burst;
449 float t = uTime * (0.25 + aRand.y * 0.5);
450 p += vec3(sin(t + aRand.z * 20.0), cos(t * 1.3 + aRand.x * 20.0), sin(t * 0.7 + aRand.w * 20.0)) * (mix(0.018, 0.004, flat_) * body + aAmb * 0.4);
451 vec3 mv = p - vec3(0.0, 0.0, 12.0);
452 gl_Position = uProj * vec4(mv, 1.0);
453 float sz = (0.55 + aRand.w * 0.95) * uPx * (12.0 / -mv.z) * (1.0 + burst * 0.3);
454 gl_PointSize = min(sz, uPx * 3.6);
455 float g = clamp(0.48 + p.y * 0.09 + p.z * 0.06 + (aRand.y - 0.5) * 0.85, 0.0, 1.0);
456 vec3 deep = vec3(0.07, 0.24, 0.62), brand = vec3(0.275, 0.667, 0.914), ice = vec3(0.75, 0.89, 0.98);
457 vec3 col = g < 0.4 ? mix(deep, brand, g / 0.4) : (g < 0.78 ? mix(brand, ice, (g - 0.4) / 0.38) : mix(ice, vec3(1.0), (g - 0.78) / 0.22));
458 if (aAmb > 0.5 && aRand.x > 0.93) col = aRand.w > 0.5 ? vec3(1.0) : ice;
459 vColor = col;
460 float tw = 0.62 + 0.38 * sin(uTime * (0.7 + aRand.y * 2.2) + aRand.x * 40.0);
461 vAlpha = tw * mix(0.72, 0.34, aAmb) * mix(mix(uDimA, uDimB, m), 1.0, aAmb * 0.4) * mix(1.0, 0.5, burst);
462 vRot = aRand.x * 6.2832 + uTime * (aRand.z - 0.5) * 1.4;
463 }`;
464 const FRAGMENT = `
465 precision highp float;
466 varying vec3 vColor;
467 varying float vAlpha, vRot;
468 float sdTri(vec2 p, float r) {
469 const float k = 1.7320508;
470 p.x = abs(p.x) - r;
471 p.y = p.y + r / k;
472 if (p.x + k * p.y > 0.0) p = vec2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
473 p.x -= clamp(p.x, -2.0 * r, 0.0);
474 return -length(p) * sign(p.y);
475 }
476 void main() {
477 vec2 p = gl_PointCoord * 2.0 - 1.0;
478 p.y = -p.y;
479 float c = cos(vRot), s = sin(vRot);
480 p = mat2(c, -s, s, c) * p;
481 float d = sdTri(p, 0.62);
482 float a = 1.0 - smoothstep(0.06, 0.22, abs(d));
483 if (a < 0.02) discard;
484 gl_FragColor = vec4(vColor, a * vAlpha);
485 }`;
486
487 const canvas = document.getElementById("field-canvas");
488 // Draw at the render's pixel density, so a 4K render stays sharp.
489 const density = window.devicePixelRatio || 1;
490 canvas.width = 1920 * density;
491 canvas.height = 1080 * density;
492 const gl = canvas.getContext("webgl", { antialias: false, alpha: false, preserveDrawingBuffer: true, premultipliedAlpha: false });
493 let draw = () => {};
494
495 function setUp() {
496 const cues = window.FIELD_CUES;
497 const compile = (type, src) => {
498 const s = gl.createShader(type);
499 gl.shaderSource(s, src);
500 gl.compileShader(s);
501 if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s));
502 return s;
503 };
504 const program = gl.createProgram();
505 gl.attachShader(program, compile(gl.VERTEX_SHADER, VERTEX));
506 gl.attachShader(program, compile(gl.FRAGMENT_SHADER, FRAGMENT));
507 gl.linkProgram(program);
508 gl.useProgram(program);
509
510 const R = seeded(7), rand = new Float32Array(N * 4), amb = new Float32Array(N), ambPos = new Float32Array(NA * 3);
511 for (let i = 0; i < N * 4; i++) rand[i] = R();
512 for (let i = 0; i < NA; i++) {
513 amb[i] = 1;
514 ambPos[i * 3] = (R() * 1.24 - 0.62) * 17;
515 ambPos[i * 3 + 1] = (R() * 1.24 - 0.62) * 9.6;
516 ambPos[i * 3 + 2] = R() * 7.5 - 5;
517 }
518 const attr = (name, data, size) => {
519 const buffer = gl.createBuffer(), loc = gl.getAttribLocation(program, name);
520 gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
521 gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
522 gl.enableVertexAttribArray(loc);
523 gl.vertexAttribPointer(loc, size, gl.FLOAT, false, 0, 0);
524 };
525 attr("aRand", rand, 4);
526 attr("aAmb", amb, 1);
527
528 const buffers = {};
529 cues.forEach((cue, k) => {
530 if (buffers[cue.shape]) return;
531 const full = new Float32Array(N * 3);
532 full.set(ambPos, 0);
533 full.set(shapeFor(cue.shape, 100 + k), NA * 3);
534 const buffer = gl.createBuffer();
535 gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
536 gl.bufferData(gl.ARRAY_BUFFER, full, gl.STATIC_DRAW);
537 buffers[cue.shape] = buffer;
538 });
539 const bind = (name, shape) => {
540 const loc = gl.getAttribLocation(program, name);
541 gl.bindBuffer(gl.ARRAY_BUFFER, buffers[shape]);
542 gl.enableVertexAttribArray(loc);
543 gl.vertexAttribPointer(loc, 3, gl.FLOAT, false, 0, 0);
544 };
545
546 const u = (name) => gl.getUniformLocation(program, name);
547 const f = 1 / Math.tan(Math.PI / 9), aspect = 16 / 9, near = 0.1, far = 100;
548 gl.uniformMatrix4fv(u("uProj"), false, new Float32Array([f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, (far + near) / (near - far), -1, 0, 0, (2 * far * near) / (near - far), 0]));
549 gl.uniform1f(u("uPx"), 11 * density);
550 gl.viewport(0, 0, canvas.width, canvas.height);
551 gl.enable(gl.BLEND);
552 gl.blendFunc(gl.SRC_ALPHA, gl.ONE);
553 gl.clearColor(13 / 255, 17 / 255, 23 / 255, 1);
554
555 const defaults = { x: 0, y: 0, s: 3, tilt: 0, turn: 0, sway: 0.14, spin: 0, twist: 0, snap: 0, dim: 1, flat: 0, quant: 0, dur: 1.9, burst: 1, push: 0, wave: [1, 0] };
556 const list = cues.map((c) => ({ ...defaults, ...c }));
557 const smooth = (x) => {
558 const v = Math.min(1, Math.max(0, x));
559 return v * v * (3 - 2 * v);
560 };
561 const lerp = (a, b, m) => a + (b - a) * m;
562 const pose = (c, t) => [c.tilt, c.turn + Math.sin(t * 0.3) * c.sway + c.twist * (t - c.t)];
563 const scaleOf = (c, t) => c.s * (1 + c.push * (t - c.t));
564
565 draw = (t) => {
566 let k = 0;
567 while (k + 1 < list.length && list[k + 1].t <= t) k++;
568 const to = list[k], from = list[Math.max(0, k - 1)];
569 const m = k === 0 ? 1 : Math.min(1, Math.max(0, (t - to.t) / to.dur));
570 bind("aFrom", from.shape);
571 bind("aTo", to.shape);
572 gl.uniform1f(u("uProgress"), m);
573 gl.uniform1f(u("uStagger"), to.wave ? 0.55 : 0);
574 gl.uniform2fv(u("uWave"), to.wave || [0, 0]);
575 gl.uniform1f(u("uTime"), t);
576 gl.uniform1f(u("uBurst"), to.burst);
577 gl.uniform1f(u("uSnap"), to.snap);
578 gl.uniform1f(u("uScaleA"), scaleOf(from, t));
579 gl.uniform1f(u("uScaleB"), scaleOf(to, t));
580 const fading = to.fade ? 1 - smooth((m - to.fade[0]) / (to.fade[1] - to.fade[0])) : null;
581 gl.uniform1f(u("uDimA"), fading ?? from.dim);
582 gl.uniform1f(u("uDimB"), fading ?? to.dim);
583 gl.uniform1f(u("uFlatA"), from.flat);
584 gl.uniform1f(u("uFlatB"), to.flat);
585 gl.uniform1f(u("uSpinA"), from.spin * t);
586 gl.uniform1f(u("uSpinB"), to.spin * t);
587 gl.uniform1f(u("uQuantA"), from.quant);
588 gl.uniform1f(u("uQuantB"), to.quant);
589 gl.uniform3f(u("uOffA"), from.x, from.y, 0);
590 gl.uniform3f(u("uOffB"), to.x, to.y, 0);
591 const eased = smooth(m), A = pose(from, t), B = pose(to, t), tilt = lerp(A[0], B[0], eased), turn = lerp(A[1], B[1], eased);
592 const cx = Math.cos(tilt), sx = Math.sin(tilt), cy = Math.cos(turn), sy = Math.sin(turn);
593 gl.uniformMatrix3fv(u("uRot"), false, new Float32Array([cy, sx * sy, -cx * sy, 0, cx, sx, sy, -sx * cy, cx * cy]));
594 gl.clear(gl.COLOR_BUFFER_BIT);
595 gl.drawArrays(gl.POINTS, 0, N);
596 };
597 }
598
599 window.addEventListener("hf-seek", (e) => draw(e.detail.time));
600 const ready = document.fonts.load('600 100px "Geist Mono"').then(() => {
601 setUp();
602 draw(0);
603 });
604 window.__hf = window.__hf || {};
605 window.__hf.buildReady = window.__hf.buildReady || {};
606 window.__hf.buildReady.field = ready;
607 window.__timelines["field"] = gsap.timeline({ paused: true }).to({ v: 0 }, { v: 1, duration: window.FILM_SECONDS }, 0);
608 })();
609 </script>
610</template>