Repository navigation
Expand file tree
/
Copy pathkit.js
More file actions
239 lines (234 loc) · 23.3 KB
/
Copy pathkit.js
File metadata and controls
239 lines (234 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* Kit: helpers shared by the hero's five sets (geometry, textures, light, camera rails). */
(function () {
const T = THREE;
const K = (window.K = {});
/* registry the sets sign into; the engine (hero.js, loaded last) drives it */
const H = (window.H = { defs: {}, U: { tNext: { value: null }, uRes: { value: new T.Vector2(1, 1) } } });
H.def = (id, build) => { H.defs[id] = { id, build, inst: null }; };
K.clamp = (v, a = 0, b = 1) => Math.min(b, Math.max(a, v));
K.lerp = (a, b, t) => a + (b - a) * t;
K.smooth = (a, b, v) => { const t = K.clamp((v - a) / (b - a)); return t * t * (3 - 2 * t); };
K.ease = (t) => { t = K.clamp(t); return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2; };
K.out = (t) => 1 - Math.pow(1 - K.clamp(t), 3);
K.back = (t) => { t = K.clamp(t); const c = 1.9; return 1 + (c + 1) * Math.pow(t - 1, 3) + c * Math.pow(t - 1, 2); };
K.rng = (seed) => () => { seed |= 0; seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
K.col = (c) => new T.Color(c);
K.MONO = '"JetBrains Mono", ui-monospace, "SF Mono", Menlo, monospace';
K.SANS = '"Space Grotesk", system-ui, -apple-system, "Segoe UI", sans-serif';
K.SERIF = '"Fraunces", "Iowan Old Style", Georgia, serif';
/* ---------- geometry ---------- */
K.rr = (w, h, r) => {
const s = new T.Shape(), x = -w / 2, y = -h / 2; r = Math.max(0.0005, Math.min(r, w / 2, h / 2));
s.moveTo(x + r, y); s.lineTo(x + w - r, y); s.absarc(x + w - r, y + r, r, -Math.PI / 2, 0, false);
s.lineTo(x + w, y + h - r); s.absarc(x + w - r, y + h - r, r, 0, Math.PI / 2, false);
s.lineTo(x + r, y + h); s.absarc(x + r, y + h - r, r, Math.PI / 2, Math.PI, false);
s.lineTo(x, y + r); s.absarc(x + r, y + r, r, Math.PI, Math.PI * 1.5, false);
return s;
};
/* box with every edge rounded, centred on the origin */
K.rbox = (w, h, d, r = 0.06, seg = 3) => {
const b = Math.max(0.001, Math.min(r, w / 2 - 0.001, h / 2 - 0.001, d / 2 - 0.001));
const g = new T.ExtrudeGeometry(K.rr(w - 2 * b, h - 2 * b, b * 0.5), { depth: d - 2 * b, bevelEnabled: true, bevelThickness: b, bevelSize: b, bevelSegments: seg, curveSegments: 5, steps: 1 });
g.translate(0, 0, -(d - 2 * b) / 2);
return g;
};
/* flat plate with rounded corners, centred, facing +z */
K.plate = (w, h, r, d = 0.02) => { const g = new T.ExtrudeGeometry(K.rr(w, h, r), { depth: d, bevelEnabled: false, curveSegments: 6 }); g.translate(0, 0, -d / 2); return g; };
/* studio cove: floor running back to a curved rise and a wall, one seamless sheet */
K.cove = (width, zFront, zWall, R, height) => {
const pts = [[0, zFront], [0, zWall + R]];
for (let i = 1; i <= 14; i++) { const a = (i / 14) * Math.PI / 2; pts.push([R - R * Math.cos(a), zWall + R - R * Math.sin(a)]); }
pts.push([height, zWall]);
const pos = [], idx = [];
pts.forEach(([y, z]) => { pos.push(-width / 2, y, z, width / 2, y, z); });
for (let i = 0; i < pts.length - 1; i++) { const a = i * 2; idx.push(a, a + 1, a + 2, a + 1, a + 3, a + 2); }
const g = new T.BufferGeometry(); g.setAttribute('position', new T.Float32BufferAttribute(pos, 3)); g.setIndex(idx); g.computeVertexNormals();
return g;
};
/* a rounded frame (a rounded plate with a rounded window cut through it), centred, facing +z */
K.frame = (w, h, r, bw, d = 0.1, ri) => { const s = K.rr(w, h, r); s.holes.push(K.rr(w - 2 * bw, h - 2 * bw, ri ?? Math.max(0.02, r - bw * 0.5))); const g = new T.ExtrudeGeometry(s, { depth: d, bevelEnabled: true, bevelThickness: 0.015, bevelSize: 0.015, bevelSegments: 2, curveSegments: 6 }); g.translate(0, 0, -d / 2); return g; };
/* soft contact shadow to sit under heavy objects */
K.blob = (w, d, o = {}) => { K.blobT = K.blobT || K.tex(128, 128, (g) => { const r = g.createRadialGradient(64, 64, 6, 64, 64, 64); r.addColorStop(0, 'rgba(0,0,0,1)'); r.addColorStop(0.55, 'rgba(0,0,0,0.55)'); r.addColorStop(1, 'rgba(0,0,0,0)'); g.fillStyle = r; g.fillRect(0, 0, 128, 128); });
const m = new T.Mesh(new T.PlaneGeometry(w, d), new T.MeshBasicMaterial({ map: K.blobT, transparent: true, opacity: o.a ?? 0.32, depthWrite: false })); m.rotation.x = -Math.PI / 2; const p = o.p || [0, 0, 0]; m.position.set(p[0], (p[1] || 0) + 0.014, p[2]); m.rotation.z = o.ry || 0; m.renderOrder = 1; if (o.to) o.to.add(m); return m; };
K.mesh = (geo, mat, o = {}) => {
const m = new T.Mesh(geo, mat);
if (o.p) m.position.set(o.p[0], o.p[1], o.p[2]);
if (o.r) m.rotation.set(o.r[0], o.r[1], o.r[2]);
if (o.s) (typeof o.s === 'number' ? m.scale.setScalar(o.s) : m.scale.set(o.s[0], o.s[1], o.s[2]));
m.castShadow = o.cast !== false; m.receiveShadow = o.receive !== false;
if (o.to) o.to.add(m);
return m;
};
K.std = (color, rough = 0.6, metal = 0, extra) => new T.MeshStandardMaterial(Object.assign({ color, roughness: rough, metalness: metal }, extra || {}));
/* ---------- textures ---------- */
K.tex = (w, h, draw, o = {}) => {
const c = document.createElement('canvas'); c.width = w; c.height = h;
draw(c.getContext('2d'), w, h, c);
const t = new T.CanvasTexture(c); t.colorSpace = o.linear ? T.NoColorSpace : T.SRGBColorSpace; t.anisotropy = 8;
if (o.repeat) { t.wrapS = t.wrapT = T.RepeatWrapping; t.repeat.set(o.repeat[0], o.repeat[1]); }
return t;
};
/* where a file is served from. The deployed site renames every file after its content (build.py writes the table into the page);
the source folder serves files under their own names */
K.url = (() => { let map = null; try { const el = document.getElementById('asset-map'); map = el ? JSON.parse(el.textContent) : null; } catch (e) {} return (p) => (map && map[p]) || p; })();
/* every picture asked for is remembered, so the film can wait for them before it starts */
K.pending = [];
K.img = (src) => { const p = new Promise((res, rej) => { const i = new Image(); i.onload = () => res(i); i.onerror = rej; i.src = K.url(src); }); K.pending.push(p.catch(() => 0)); return p; };
/* a photograph reprinted in two inks, with a coarse halftone, ready to hang on a set wall */
K.duotone = (src, dark, light, o = {}) => {
const A = K.col(dark), B = K.col(light), toS = (c) => { const s = c.clone().convertLinearToSRGB(); return [s.r * 255, s.g * 255, s.b * 255]; };
const a = toS(A), b = toS(B);
const t = K.tex(8, 8, (g) => { g.fillStyle = 'rgb(' + a.map((v, i) => Math.round((v + b[i]) / 2)).join(',') + ')'; g.fillRect(0, 0, 8, 8); });
K.img(src).then((im) => {
const crop = o.crop || [0, 0, 1, 1], sw = im.naturalWidth * crop[2], sh = im.naturalHeight * crop[3];
const w = o.w || 1400, h = Math.round(w * sh / sw), c = document.createElement('canvas'); c.width = w; c.height = h;
const g = c.getContext('2d'); g.drawImage(im, im.naturalWidth * crop[0], im.naturalHeight * crop[1], sw, sh, 0, 0, w, h);
const d = g.getImageData(0, 0, w, h), p = d.data, cell = o.cell || 6, half = o.half ?? 0.45, lo = o.lo ?? 0.04, hi = o.hi ?? 0.92, gam = o.gamma || 1;
const ca = Math.cos(0.5), sa = Math.sin(0.5);
for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i += 4) {
let l = (p[i] * 0.299 + p[i + 1] * 0.587 + p[i + 2] * 0.114) / 255;
l = Math.pow(K.clamp((l - lo) / (hi - lo)), gam);
const u = (x * ca + y * sa) / cell, v = (-x * sa + y * ca) / cell, fx = u - Math.floor(u) - 0.5, fy = v - Math.floor(v) - 0.5;
const dd = Math.sqrt(fx * fx + fy * fy) * 1.9, ink = 1 - K.smooth(-0.12, 0.12, dd - Math.sqrt(1 - l) * 1.25);
const tone = l * (1 - half) + (1 - ink) * half;
p[i] = a[0] + (b[0] - a[0]) * tone; p[i + 1] = a[1] + (b[1] - a[1]) * tone; p[i + 2] = a[2] + (b[2] - a[2]) * tone;
}
g.putImageData(d, 0, 0);
t.dispose(); t.image = c; t.needsUpdate = true; o.done && o.done(t, w / h);
}).catch(() => {});
return t;
};
/* a photograph cut out of its background: colour (jpg) and mask (png) are stored apart and joined here */
K.cutout = (base, o = {}) => {
const t = K.tex(2, 2, () => {});
Promise.all([K.img(base + '.jpg'), K.img(base + '-a.png')]).then(([c, m]) => {
const w = c.naturalWidth, h = c.naturalHeight, cv = document.createElement('canvas'); cv.width = w; cv.height = h;
const g = cv.getContext('2d', { willReadFrequently: true }); g.drawImage(m, 0, 0, w, h); const md = g.getImageData(0, 0, w, h).data;
if (o.flip) { g.save(); g.translate(w, 0); g.scale(-1, 1); } g.drawImage(c, 0, 0, w, h); if (o.flip) g.restore();
const d = g.getImageData(0, 0, w, h), p = d.data, gain = o.gain || 1, lift = o.lift || 0;
for (let y = 0, i = 0; y < h; y++) for (let x = 0; x < w; x++, i += 4) { const mi = o.flip ? (y * w + (w - 1 - x)) * 4 : i; if (gain !== 1 || lift) { p[i] = Math.min(255, p[i] * gain + lift); p[i + 1] = Math.min(255, p[i + 1] * gain + lift); p[i + 2] = Math.min(255, p[i + 2] * gain + lift); } p[i + 3] = md[mi]; }
g.putImageData(d, 0, 0); t.dispose(); t.image = cv; t.needsUpdate = true; o.done && o.done(w / h, t);
}).catch(() => {});
return t;
};
/* a cut-out standing in the set like a stage flat: it is lit, takes shadows and casts its own outline. p is the foot. */
K.flat = (base, o = {}) => {
const hgt = o.h || 5, m = new T.Mesh(new T.PlaneGeometry(1, 1), null);
const tex = K.cutout(base, { flip: o.flip, gain: o.gain, lift: o.lift, done: (asp) => { m.scale.set(hgt * asp, hgt, 1); o.done && o.done(asp); } });
m.material = new T.MeshStandardMaterial({ map: tex, alphaTest: 0.5, side: T.DoubleSide, roughness: 1, metalness: 0, envMapIntensity: o.env ?? 0.6, color: o.color || '#ffffff' }); m.material.alphaToCoverage = true;
m.customDepthMaterial = new T.MeshDepthMaterial({ depthPacking: T.RGBADepthPacking, map: tex, alphaTest: 0.5 });
m.scale.set(hgt * (o.asp || 0.8), hgt, 1); const p = o.p || [0, 0, 0]; m.position.set(p[0], p[1] + hgt / 2, p[2]); m.rotation.y = o.ry || 0; m.userData.ry = o.ry || 0; m.userData.face = o.face ?? 0.6;
m.castShadow = o.cast !== false; m.receiveShadow = o.receive !== false; if (o.to) o.to.add(m);
return m;
};
/* lets something take bites out of a textured material: up to six round bites, set in the texture's own coordinates */
K.bitable = (mat, asp = 1) => { const bites = Array.from({ length: 6 }, () => new T.Vector3(0, 0, 0)), ua = { value: asp };
mat.onBeforeCompile = (sh) => { sh.uniforms.uBite = { value: bites }; sh.uniforms.uBiteAsp = ua; sh.fragmentShader = 'uniform vec3 uBite[6]; uniform float uBiteAsp;\n' + sh.fragmentShader.replace('#include <map_fragment>', '#include <map_fragment>\n for (int i = 0; i < 6; i++) { vec3 b = uBite[i]; if (b.z > 0.0 && length((vMapUv - b.xy) * vec2(uBiteAsp, 1.0)) < b.z) discard; }'); };
mat.userData.bites = bites; mat.userData.biteAsp = ua; return mat; };
/* a cut-out head that eats: the picture is split at the mouth and the top lifts like a lid (the paper-collage trick).
o: { h height, asp, mouth and mx (the mouth line and its centre, as fractions from the top left), fw (face width as a fraction), flip }.
It faces left; its origin is the middle of its mouth. */
K.puppet = (src, o = {}) => {
const h = o.h || 1, mth = o.mouth ?? 0.62, g = new T.Group(), lid = new T.Group(), parts = [];
const mat = new T.MeshStandardMaterial({ alphaTest: 0.5, transparent: true, depthTest: false, depthWrite: false, side: T.DoubleSide, roughness: 1, metalness: 0, envMapIntensity: 0.35, color: '#b4b4b4', emissive: new T.Color(0.36, 0.36, 0.36) });
[[1 - mth, 1], [0, 1 - mth]].forEach(([v0, v1]) => { const geo = new T.PlaneGeometry(1, 1), uv = geo.attributes.uv; for (let j = 0; j < 4; j++) uv.setY(j, uv.getY(j) > 0.5 ? v1 : v0); const m = new T.Mesh(geo, mat); m.castShadow = false; m.receiveShadow = false; parts.push(m); });
/* drawn over the set whatever is in the way, in this order: jaw, tongue, (what is being eaten), the top of the head */
const gap = new T.Mesh(new T.CircleGeometry(0.5, 24, 0, Math.PI), new T.MeshBasicMaterial({ color: '#4a1018', transparent: true, depthTest: false, depthWrite: false })); g.add(gap, parts[1], lid); lid.add(parts[0]); parts[1].renderOrder = 900; gap.renderOrder = 901; parts[0].renderOrder = 903;
const fit = (asp) => { const w = h * asp, mx = o.flip ? 1 - (o.mx ?? 0.5) : (o.mx ?? 0.5), xo = (0.5 - mx) * w; g.userData.w = w; g.userData.gw = w * (o.fw ?? 0.62) * 0.36; lid.position.set(xo + w * 0.32, 0, 0.001); parts[0].scale.set(w, h * mth, 1); parts[0].position.set(-w * 0.32, h * mth / 2, 0); parts[1].scale.set(w, h * (1 - mth), 1); g.userData.x1 = xo; g.userData.y1 = -h * (1 - mth) / 2; parts[1].position.set(xo, g.userData.y1, 0); };
const set = (t) => { mat.map = t; mat.emissiveMap = t; mat.needsUpdate = true; };
if (typeof src === 'string') set(K.cutout(src, { flip: o.flip, done: (asp) => fit(asp) })); else set(src);
fit(o.asp || 0.84);
g.userData.open = (a) => { lid.rotation.z = -a * 0.44; parts[1].rotation.z = a * 0.05; parts[1].position.y = g.userData.y1 - a * h * 0.02; gap.scale.set(g.userData.gw, Math.max(0.0001, a * h * 0.16), 1); gap.position.set(0, -a * h * 0.02, 0); gap.visible = a > 0.03; };
g.userData.open(0); return g;
};
/* something to be eaten: a drawn object or a photo cut-out. Unit plane, scaled to w by w / asp. */
K.emblem = (tex, w, asp) => { const m = new T.Mesh(new T.PlaneGeometry(1, 1), K.bitable(new T.MeshStandardMaterial({ map: tex, emissiveMap: tex, emissive: new T.Color(0.36, 0.36, 0.36), color: '#b4b4b4', alphaTest: 0.5, transparent: true, depthTest: false, depthWrite: false, side: T.DoubleSide, roughness: 0.9, metalness: 0, envMapIntensity: 0.35 }), asp)); m.renderOrder = 902; m.userData.fit = (a) => { m.scale.set(w, w / a, 1); m.material.userData.biteAsp.value = a; }; m.userData.fit(asp); m.castShadow = false; m.receiveShadow = false; return m; };
/* one era eats the last, in front of the lens. Sizes are fractions of the frame's height; x is measured from the optical axis.
o: { camera, eater (a K.puppet), victim (a K.emblem), vw its width, x, y, k0, k1, crumb } → update(k, t, P) */
K.feast = (o) => {
const cam = o.camera, rig = new T.Group(), NB = 5, bt = o.victim.material.userData.bites, D = 1.6, N = 26, o3 = new T.Object3D(), cr = [], R = K.rng(o.seed || 5);
const crumbs = new T.InstancedMesh(new T.PlaneGeometry(1, 1), new T.MeshBasicMaterial({ color: o.crumb || '#f4f1ea', side: T.DoubleSide, transparent: true, depthTest: false, depthWrite: false }), N); crumbs.frustumCulled = false; crumbs.renderOrder = 904;
for (let i = 0; i < N; i++) cr.push({ b: i % NB, vx: (R() - 0.35) * 0.5, vy: 0.08 + R() * 0.36, s: 0.01 + R() * 0.018, r: R() * 6 });
rig.add(o.victim, o.eater, crumbs); cam.add(rig); rig.visible = false; o.eater.position.z = 0.004; crumbs.position.z = 0.006;
return (k, t, P) => {
const u = (k - o.k0) / (o.k1 - o.k0); rig.visible = u > 0 && u < 1; if (!rig.visible) return;
const ms = P.mobile ? 0.6 : 1, fh = 2 * D * Math.tan(cam.fov * Math.PI / 360); rig.position.set(0, 0, -D); rig.scale.setScalar(fh * ms);
const vw = o.vw, vx = P.mobile ? -0.06 / ms : o.x, vy = P.mobile ? (o.my ?? 0.1) / ms : o.y, edge = (P.mobile ? 0.5 : 0.35) * P.aspect / ms;
/* the old emblem drifts by; the new era comes after it, takes it in five bites, and goes */
const drift = K.out(K.clamp(u / 0.26)), b = K.clamp((u - 0.3) / 0.5) * NB, bi = Math.min(NB - 1, Math.floor(b)), f = b - bi, eating = u > 0.3 && u < 0.8, done = u >= 0.8 ? NB : eating ? bi + (f > 0.6 ? 1 : 0) : 0, gone = K.ease(K.clamp((u - 0.84) / 0.16));
o.victim.position.set(K.lerp(edge + vw, vx, drift) + (eating && f > 0.5 && f < 0.72 ? Math.sin(t * 70) * 0.004 : 0), vy + Math.sin(t * 1.4) * 0.008, 0); o.victim.rotation.z = Math.sin(t * 1.1) * 0.03 - (eating ? 0.03 : 0); o.victim.visible = done < NB;
for (let i = 0; i < 6; i++) bt[i].set(1 - (i + 0.5) / NB, 0.5 + (i % 2 ? 0.22 : -0.22), i < done ? 0.64 : 0);
const front = vx + vw / 2 - done * vw / NB, chase = K.out(K.clamp((u - 0.1) / 0.2)), open = eating ? Math.sin(Math.min(1, f / 0.6) * Math.PI) : u < 0.3 ? 0.3 + 0.25 * Math.sin(t * 7) : 0.14 * Math.abs(Math.sin(t * 13)) * (1 - gone), lunge = eating ? (K.smooth(0, 0.4, f) - K.smooth(0.62, 0.9, f)) * vw / NB * 0.75 : 0;
o.eater.position.x = K.lerp(edge + 0.45, front + 0.035 - lunge, chase) + gone * (edge + 0.5 - front); o.eater.position.y = vy + Math.abs(Math.sin(t * 6)) * 0.014 * (1 - chase) + Math.sin(t * 2) * 0.005; o.eater.rotation.z = lunge * 1.6 + Math.sin(t * 1.7) * 0.02 - gone * 0.2; o.eater.userData.open(K.clamp(open));
for (let i = 0; i < N; i++) { const q = cr[i], age = (b - (q.b + 0.6)) * 0.45, on = (eating || u >= 0.8) && age > 0 && age < 0.55; o3.position.set(vx + vw / 2 - (q.b + 0.5) * vw / NB + q.vx * age, vy + q.vy * age - 2.4 * age * age, 0); o3.rotation.set(0, 0, q.r + age * 14); o3.scale.setScalar(on ? q.s : 1e-5); o3.updateMatrix(); crumbs.setMatrixAt(i, o3.matrix); }
crumbs.instanceMatrix.needsUpdate = true;
};
};
/* turn flats part of the way toward the camera so they never go edge-on */
K.faceFlats = (list, cam) => { for (const m of list) { const a = Math.atan2(cam.position.x - m.position.x, cam.position.z - m.position.z); m.rotation.y = m.userData.ry + (a - m.userData.ry) * m.userData.face; } };
/* dust in the light: soft points that drift; they glow on colour instead of sitting on black */
K.dust = (o = {}) => {
const n = o.n || 260, R = K.rng(o.seed || 3), b = o.box || [0, 6, 0, 30, 12, 24], pos = new Float32Array(n * 3), sd = new Float32Array(n);
for (let i = 0; i < n; i++) { pos[i * 3] = b[0] + (R() - 0.5) * b[3]; pos[i * 3 + 1] = b[1] + (R() - 0.5) * b[4]; pos[i * 3 + 2] = b[2] + (R() - 0.5) * b[5]; sd[i] = R(); }
const g = new T.BufferGeometry(); g.setAttribute('position', new T.BufferAttribute(pos, 3)); g.setAttribute('aS', new T.BufferAttribute(sd, 1));
const m = new T.ShaderMaterial({ transparent: true, depthWrite: false, blending: o.blend ?? T.AdditiveBlending, uniforms: { uT: { value: 0 }, uC: { value: K.col(o.color || '#ffffff').multiplyScalar(o.gain ?? 1.2) }, uSize: { value: o.size || 26 }, uRise: { value: o.rise ?? 0.12 } },
vertexShader: 'attribute float aS; uniform float uT; uniform float uSize; uniform float uRise; varying float vA; void main(){ vec3 p = position; p.x += sin(uT * (0.2 + aS * 0.3) + aS * 40.0) * 0.6; p.y += mod(uT * uRise * (0.4 + aS), 3.0) - 1.5 + cos(uT * 0.25 + aS * 17.0) * 0.4; p.z += cos(uT * (0.15 + aS * 0.2) + aS * 23.0) * 0.6; vec4 mv = modelViewMatrix * vec4(p, 1.0); gl_Position = projectionMatrix * mv; gl_PointSize = uSize * (0.4 + aS) / max(1.0, -mv.z) * 3.0; vA = (0.35 + 0.65 * sin(uT * (0.6 + aS) + aS * 30.0) * 0.5 + 0.325) * smoothstep(1.0, 4.0, -mv.z); }',
fragmentShader: 'uniform vec3 uC; varying float vA; void main(){ vec2 d = gl_PointCoord - 0.5; float a = smoothstep(0.5, 0.0, length(d)); gl_FragColor = vec4(uC * a * vA, a * vA); }' });
const pts = new T.Points(g, m); pts.frustumCulled = false; return pts;
};
/* ---------- light ---------- */
K.env = (renderer, o = {}) => {
const s = new T.Scene();
const sky = new T.Mesh(new T.SphereGeometry(60, 32, 16), new T.ShaderMaterial({ side: T.BackSide, uniforms: { a: { value: K.col(o.bottom || '#7d7d7d') }, b: { value: K.col(o.top || '#ffffff') } },
vertexShader: 'varying vec3 d; void main(){ d = normalize(position); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
fragmentShader: 'uniform vec3 a; uniform vec3 b; varying vec3 d; void main(){ gl_FragColor = vec4(mix(a, b, smoothstep(-0.35, 0.75, d.y)), 1.0); }' }));
s.add(sky);
(o.boxes || [{ p: [-14, 16, 12], s: [16, 12], i: 7 }, { p: [16, 7, 8], s: [8, 14], i: 2.6 }, { p: [0, 12, -18], s: [22, 6], i: 1.8 }, { p: [2, 22, 2], s: [14, 14], i: 3 }]).forEach((q) => {
const m = new T.Mesh(new T.PlaneGeometry(q.s[0], q.s[1]), new T.MeshBasicMaterial({ color: K.col(q.c || '#ffffff').multiplyScalar(q.i), side: T.DoubleSide, toneMapped: false }));
m.position.set(q.p[0], q.p[1], q.p[2]); m.lookAt(0, 0, 0); s.add(m);
});
const pm = new T.PMREMGenerator(renderer), tex = pm.fromScene(s, 0.03).texture; pm.dispose();
s.traverse((x) => { if (x.geometry) x.geometry.dispose(); if (x.material) x.material.dispose(); });
return tex;
};
K.lights = (scene, o = {}) => {
const hemi = new T.HemisphereLight(o.sky || 0xffffff, o.ground || 0x808080, o.amb ?? 1.1);
const key = new T.DirectionalLight(o.keyCol || 0xffffff, o.key ?? 2.6);
key.position.set(...(o.pos || [-8, 14, 10])); key.target.position.set(...(o.target || [0, 0, 0]));
key.castShadow = o.shadow !== false;
/* every set stays loaded once the film has started, so on small screens the shadow maps are halved: six at full size is more memory than a phone should be asked for */
const s = key.shadow, e = o.extent || 16, ms = Math.min(o.map || 2048, Math.min(innerWidth, innerHeight) < 620 ? 2048 : 4096); s.mapSize.set(ms, ms);
s.camera.left = -e; s.camera.right = e; s.camera.top = e; s.camera.bottom = -e; s.camera.near = 1; s.camera.far = o.far || 70;
s.bias = o.bias ?? -0.0005; s.normalBias = o.nbias ?? 0.03;
scene.add(hemi, key, key.target); hemi.layers.enableAll(); key.layers.enableAll();
return { hemi, key };
};
/* ---------- camera ---------- */
/* keys: [{ k, p:[x,y,z], l:[x,y,z], f:fov, e:optional easing for the leg that ends on this key }] */
K.rail = (keys) => {
const pos = new T.CatmullRomCurve3(keys.map((q) => new T.Vector3(...q.p)), false, 'catmullrom', 0.5);
const look = new T.CatmullRomCurve3(keys.map((q) => new T.Vector3(...q.l)), false, 'catmullrom', 0.5);
const n = keys.length - 1, out = { pos: new T.Vector3(), look: new T.Vector3(), fov: 35 };
return (k) => {
let i = 0; while (i < n - 1 && k > keys[i + 1].k) i++;
const a = keys[i], b = keys[i + 1]; let f = K.clamp((k - a.k) / (b.k - a.k)); if (b.e) f = b.e(f);
const u = K.clamp((i + f) / n); pos.getPoint(u, out.pos); look.getPoint(u, out.look);
out.fov = K.lerp(a.f ?? 35, b.f ?? a.f ?? 35, f);
return out;
};
};
/* single writer for the camera: the rail pose, then a damped pointer offset on top */
K.aim = (cam, pose, P, amp = 1) => {
cam.position.copy(pose.pos); cam.lookAt(pose.look);
cam.translateX(P.x * 0.55 * amp); cam.translateY(-P.y * 0.3 * amp); cam.lookAt(pose.look);
const widen = K.clamp(1.3 / P.aspect, 1, 1.9), fov = 2 * Math.atan(Math.tan((pose.fov * Math.PI) / 360) * widen) * 180 / Math.PI;
if (Math.abs(cam.fov - fov) > 0.01) { cam.fov = fov; cam.updateProjectionMatrix(); }
};
/* portal surface: shows a flat colour, then the next set, mapped in screen space */
K.portalMat = (U, flat, map, rect) => new T.ShaderMaterial({
uniforms: { tNext: U.tNext, uRes: U.uRes, uMix: { value: 0 }, uFlat: { value: flat.clone() }, tMap: { value: map || null }, uUseMap: { value: map ? 1 : 0 }, uRect: { value: new T.Vector4(...(rect || [0, 0, 1, 1])) } },
vertexShader: 'varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); }',
fragmentShader: 'varying vec2 vUv; uniform sampler2D tNext; uniform sampler2D tMap; uniform vec2 uRes; uniform float uMix; uniform float uUseMap; uniform vec3 uFlat; uniform vec4 uRect; void main(){ vec3 base = uFlat; if (uUseMap > 0.5) base = texture2D(tMap, uRect.xy + vUv * uRect.zw).rgb; vec3 n = texture2D(tNext, gl_FragCoord.xy / uRes).rgb; gl_FragColor = vec4(mix(base, n, uMix), 1.0); }',
side: T.DoubleSide
});
})();