Build and revise procedural 3D assets with your coding agent. Local MCP server, CLI and TypeScript engine with rendered review, editable source and GLB export.
JavaScript 3d3d-assets3d-modelingagent-skillsagent-toolsai-agentsasset-pipelinecligame-assetsglbgltfmcpmcp-servermodel-context-protocolprocedural-generationprocedural-modelingtext-to-3dthreejstypescript
feat(site): add the text-to-3D page and offer asset downloads right after their figures (#337)
Matthew Kissinger5712ff3d8d92
diff --git a/README.md b/README.mdindex beac97a0..913794c7 100644--- a/README.md+++ b/README.md@@ -55,7 +55,8 @@ Editable assets: [Meridian (53)](https://kilnstudio.tools/packs/holodeck/meridia[Golden Gate bridge](https://kilnstudio.tools/gallery/golden-gate-bridge/).[Explore all scenes](https://kilnstudio.tools/scenes/) or-[browse the asset gallery](https://kilnstudio.tools/gallery/).+[browse the asset gallery](https://kilnstudio.tools/gallery/). To make your own, see+[text to 3D with your coding agent](https://kilnstudio.tools/text-to-3d/): a walking mech and a cathedral, prompt to GLB.Scene code and asset content retain their own licences; each pack records its scope.## From asset to scenediff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-1.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/cathedral-1.kiln.jsnew file mode 100644index 00000000..4185a794--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/cathedral-1.kiln.js@@ -0,0 +1,2853 @@+// Gothic cathedral: parametric High Gothic church, true scale, metres.+// +X = west front (entrance) , -X = apse, +Y up, +Z = north.+const meta = { name: 'Gothic Cathedral', role: 'wonder' };++// ==================================================================================+// PARAMETERS: change these and the whole design follows.+// ==================================================================================+const P = {+ preview: 'all', // 'all', or 'stats' (size report), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)+ // Plan+ naveBays: 9, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,+ xWest: 62, // west face of the towers+ towerDepth: 14, towerWidth: 14,+ choirBays: 3, transeptBays: 2,+ apseSides: 7,+ // Elevation+ pierTop: 15, aisleCrown: 21, triforiumSill: 21.8, triforiumTop: 26.6,+ vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,+ aisleWallTop: 23, aisleRoofHigh: 26.2,+ towerTop: 72, spireTip: 80,+ flecheTip: 74,+ // Detail+ jambStatues: 5, kings: 16, seed: 1163,+ storyWindows: 3, clerestoryWindows: 3, // unique window cartoons (more = richer, larger GLB)+ // Glass+ glassGlow: 1.2, glassOuterGlow: 0.2, sunShear: [0.34, 0.86],+ chapelFacets: 5,+};++// ==================================================================================+// MATH+// ==================================================================================+const PI = Math.PI, TAU = PI * 2, rad = (d) => d * PI / 180;+const lerp = (a, b, t) => a + (b - a) * t;+const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));+const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };+const gauss = (x, y = 0) => Math.exp(-(x * x + y * y));+function mkRng(seed) { let s = seed >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }+function keyLin(keys, t) {+ if (t <= keys[0][0]) return keys[0].slice(1);+ for (let i = 1; i < keys.length; i++) if (t <= keys[i][0]) {+ const a = keys[i - 1], b = keys[i], f = (t - a[0]) / (b[0] - a[0]);+ return a.slice(1).map((v, k) => lerp(v, b[k + 1], f));+ }+ return keys[keys.length - 1].slice(1);+}+function keyf(keys, t, w = 0.03) { // smoothed keyframes [[t,v...],...]: no plateaus or kinks at the keys+ const a = keyLin(keys, t - w), b = keyLin(keys, t), c = keyLin(keys, t + w);+ return b.map((v, k) => (a[k] + 2 * v + c[k]) / 4);+}+const M = {+ I: () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],+ T: (x, y, z) => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z],+ S: (x, y = x, z = x) => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0],+ Rx: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0]; },+ Ry: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0]; },+ Rz: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0]; },+ mul: (a, b) => [+ a[0] * b[0] + a[1] * b[4] + a[2] * b[8], a[0] * b[1] + a[1] * b[5] + a[2] * b[9], a[0] * b[2] + a[1] * b[6] + a[2] * b[10], a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3],+ a[4] * b[0] + a[5] * b[4] + a[6] * b[8], a[4] * b[1] + a[5] * b[5] + a[6] * b[9], a[4] * b[2] + a[5] * b[6] + a[6] * b[10], a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7],+ a[8] * b[0] + a[9] * b[4] + a[10] * b[8], a[8] * b[1] + a[9] * b[5] + a[10] * b[9], a[8] * b[2] + a[9] * b[6] + a[10] * b[10], a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11],+ ],+ of: (...ms) => ms.reduce((a, b) => M.mul(a, b)),+};+function normalMat(m) { // inverse transpose of the 3x3, up to scale (cofactors)+ return [+ m[5] * m[10] - m[6] * m[9], m[6] * m[8] - m[4] * m[10], m[4] * m[9] - m[5] * m[8],+ m[2] * m[9] - m[1] * m[10], m[0] * m[10] - m[2] * m[8], m[1] * m[8] - m[0] * m[9],+ m[1] * m[6] - m[2] * m[5], m[2] * m[4] - m[0] * m[6], m[0] * m[5] - m[1] * m[4],+ ];+}+const det3 = (m) => m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8]) + m[2] * (m[4] * m[9] - m[5] * m[8]);++// ==================================================================================+// MESH KIT: one Batch = one mesh with one material; metric UVs, baked shading in COLOR_0+// ==================================================================================+class Batch {+ constructor(tile = 1) { this.P = []; this.N = []; this.UV = []; this.C = []; this.I = []; this.tile = tile; this.setM(M.I()); this.sh = 1; this.rgb = [1, 1, 1]; this.fn = null; this.geo = null; }+ setM(m) { this.m = m; this.nm = normalMat(m); this.det = det3(m); }+ at(m, f) { const old = this.m; this.setM(M.mul(old, m)); f(this); this.setM(old); return this; }+ shade(s, f) { const o = this.sh; this.sh = o * s; f(this); this.sh = o; return this; }+ tint(rgb, f) { const o = this.rgb; this.rgb = rgb; f(this); this.rgb = o; return this; }+ get count() { return this.P.length / 3; }+ v(x, y, z, nx, ny, nz, u, v, s = 1) {+ const m = this.m, n = this.nm;+ const wx = m[0] * x + m[1] * y + m[2] * z + m[3], wy = m[4] * x + m[5] * y + m[6] * z + m[7], wz = m[8] * x + m[9] * y + m[10] * z + m[11];+ let ax = n[0] * nx + n[1] * ny + n[2] * nz, ay = n[3] * nx + n[4] * ny + n[5] * nz, az = n[6] * nx + n[7] * ny + n[8] * nz;+ const l = Math.hypot(ax, ay, az);+ if (l < 1e-10) { ax = 0; ay = 1; az = 0; } else { ax /= l; ay /= l; az /= l; }+ if (this.det < 0) { ax = -ax; ay = -ay; az = -az; }+ if (u === undefined || u === null) {+ const fx = Math.abs(ax), fy = Math.abs(ay), fz = Math.abs(az);+ if (fy > 0.75) { u = wx; v = wz; } else if (fx > fz) { u = wz; v = wy; } else { u = wx; v = wy; }+ }+ let sh = s * this.sh;+ if (this.fn) sh *= this.fn(wx, wy, wz, ax, ay, az, this);+ sh = clamp(sh);+ const rgb = this.rgb;+ this.P.push(wx, wy, wz); this.N.push(ax, ay, az); this.UV.push(u / this.tile, v / this.tile);+ this.C.push(Math.round(255 * sh * rgb[0]), Math.round(255 * sh * rgb[1]), Math.round(255 * sh * rgb[2]));+ return this.P.length / 3 - 1;+ }+ tri(a, b, c) { if (this.det < 0) this.I.push(a, c, b); else this.I.push(a, b, c); }+ quad(a, b, c, d) { this.tri(a, b, c); this.tri(a, c, d); }+ // flat polygon from 3D points (CCW seen from the front); convex fan+ face(pts, s = 1) {+ const a = pts[0], b = pts[1], c = pts[pts.length - 1];+ let nx = (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), ny = (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), nz = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ if (pts.length > 3 && Math.hypot(nx, ny, nz) < 1e-9) { const d = pts[2]; nx = (b[1] - a[1]) * (d[2] - a[2]) - (b[2] - a[2]) * (d[1] - a[1]); ny = (b[2] - a[2]) * (d[0] - a[0]) - (b[0] - a[0]) * (d[2] - a[2]); nz = (b[0] - a[0]) * (d[1] - a[1]) - (b[1] - a[1]) * (d[0] - a[0]); }+ const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l;+ const ids = pts.map((p) => this.v(p[0], p[1], p[2], nx, ny, nz, undefined, undefined, p[3] === undefined ? s : p[3]));+ for (let i = 1; i < ids.length - 1; i++) this.tri(ids[0], ids[i], ids[i + 1]);+ return this;+ }+ box(x0, x1, y0, y1, z0, z1, o = {}) {+ const s = o.s === undefined ? 1 : o.s, sb = o.sb === undefined ? s * 0.62 : o.sb, st = o.st === undefined ? s : o.st;+ const tx = o.taper || 0;+ const X0 = x0 + tx, X1 = x1 - tx, Z0 = z0 + (o.taperZ === undefined ? tx : o.taperZ), Z1 = z1 - (o.taperZ === undefined ? tx : o.taperZ);+ this.face([[x1, y0, z1, s], [x1, y0, z0, s], [X1, y1, Z0, st], [X1, y1, Z1, st]]); // +X+ this.face([[x0, y0, z0, s], [x0, y0, z1, s], [X0, y1, Z1, st], [X0, y1, Z0, st]]); // -X+ this.face([[x0, y0, z1, s], [x1, y0, z1, s], [X1, y1, Z1, st], [X0, y1, Z1, st]]); // +Z+ this.face([[x1, y0, z0, s], [x0, y0, z0, s], [X0, y1, Z0, st], [X1, y1, Z0, st]]); // -Z+ if (!o.noTop) this.face([[X0, y1, Z1, st], [X1, y1, Z1, st], [X1, y1, Z0, st], [X0, y1, Z0, st]]);+ if (!o.noBot) this.face([[x0, y0, z0, sb], [x1, y0, z0, sb], [x1, y0, z1, sb], [x0, y0, z1, sb]]);+ return this;+ }+ // Surface of revolution about Y. prof: [[r,y],...] bottom to top. o.sides, o.faceted, o.crease, o.flat+ lathe(prof, n = 12, o = {}) {+ const a0 = o.phase || 0, cr = Math.cos(rad(o.crease === undefined ? 55 : o.crease));+ const s = o.s === undefined ? 1 : o.s;+ const segs = [];+ for (let j = 0; j < prof.length - 1; j++) {+ const dr = prof[j + 1][0] - prof[j][0], dy = prof[j + 1][1] - prof[j][1], l = Math.hypot(dr, dy) || 1;+ segs.push([dy / l, -dr / l, l]);+ }+ if (o.faceted) { // hard-edged polygon of revolution+ for (let i = 0; i < n; i++) {+ const t0 = a0 + TAU * i / n, t1 = a0 + TAU * (i + 1) / n, tm = (t0 + t1) / 2;+ const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1), cm = Math.cos(tm), sm = Math.sin(tm);+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const p0 = prof[j], p1 = prof[j + 1];+ const w0 = p0[0] * Math.hypot(c1 - c0, s1 - s0), w1 = p1[0] * Math.hypot(c1 - c0, s1 - s0);+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ const a = this.v(p0[0] * c0, p0[1], p0[0] * s0, nr * cm, ny, nr * sm, -w0 / 2, vv, sA);+ const b = this.v(p1[0] * c0, p1[1], p1[0] * s0, nr * cm, ny, nr * sm, -w1 / 2, vv + l, sB);+ const c = this.v(p1[0] * c1, p1[1], p1[0] * s1, nr * cm, ny, nr * sm, w1 / 2, vv + l, sB);+ const d = this.v(p0[0] * c1, p0[1], p0[0] * s1, nr * cm, ny, nr * sm, w0 / 2, vv, sA);+ this.quad(a, b, c, d); vv += l;+ }+ }+ return this;+ }+ // smooth around, creased along the profile when the section turns sharply+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const prev = segs[j - 1], next = segs[j + 1];+ let n0r = nr, n0y = ny, n1r = nr, n1y = ny;+ if (prev && prev[0] * nr + prev[1] * ny > cr) { n0r = prev[0] + nr; n0y = prev[1] + ny; }+ if (next && next[0] * nr + next[1] * ny > cr) { n1r = next[0] + nr; n1y = next[1] + ny; }+ const base = this.count, p0 = prof[j], p1 = prof[j + 1];+ const circ = Math.max(p0[0], p1[0]) * TAU, uScale = Math.max(1, Math.round(circ)) / n;+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ for (let i = 0; i <= n; i++) {+ const t = a0 + TAU * i / n, c = Math.cos(t), sn = Math.sin(t);+ this.v(p0[0] * c, p0[1], p0[0] * sn, n0r * c, n0y, n0r * sn, i * uScale, vv, sA);+ this.v(p1[0] * c, p1[1], p1[0] * sn, n1r * c, n1y, n1r * sn, i * uScale, vv + l, sB);+ }+ for (let i = 0; i < n; i++) this.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+ vv += l;+ }+ return this;+ }+ // Sweep a closed section (a,b CCW; a along in-plane left normal, b along +Z) along a path in the XY plane.+ sweep(path, sec, o = {}) {+ const closedPath = !!o.closed, cr = Math.cos(rad(o.crease === undefined ? 50 : o.crease)), z0 = o.z || 0, s = o.s === undefined ? 1 : o.s;+ const n = path.length, E = [];+ const segCount = closedPath ? n : n - 1;+ for (let i = 0; i < segCount; i++) { const p = path[i], q = path[(i + 1) % n]; const dx = q[0] - p[0], dy = q[1] - p[1], l = Math.hypot(dx, dy) || 1e-6; E.push([dx / l, dy / l, l]); }+ // section normals+ const K = sec.length, SN = [];+ for (let j = 0; j < K; j++) { const a = sec[j], b = sec[(j + 1) % K]; const da = b[0] - a[0], db = b[1] - a[1], l = Math.hypot(da, db) || 1e-6; SN.push([db / l, -da / l, l]); }+ const rings = []; let dist = 0;+ for (let i = 0; i < segCount; i++) {+ const e = E[i], ePrev = closedPath ? E[(i - 1 + segCount) % segCount] : (E[i - 1] || e), eNext = closedPath ? E[(i + 1) % segCount] : (E[i + 1] || e);+ const p = path[i], q = path[(i + 1) % n];+ const miter = (A, B) => { let bx = -(A[1] + B[1]), by = A[0] + B[0]; const l = Math.hypot(bx, by) || 1; bx /= l; by /= l; const nlx = -B[1], nly = B[0]; const c = Math.max(0.35, bx * nlx + by * nly); return [bx / c, by / c]; };+ const m0 = miter(ePrev, e), m1 = miter(e, eNext);+ const smooth0 = ePrev[0] * e[0] + ePrev[1] * e[1] > cr, smooth1 = e[0] * eNext[0] + e[1] * eNext[1] > cr;+ const nl = [-e[1], e[0]];+ const n0 = smooth0 ? [-(e[1] + ePrev[1]), e[0] + ePrev[0]] : nl, n1 = smooth1 ? [-(e[1] + eNext[1]), e[0] + eNext[0]] : nl;+ const ln0 = Math.hypot(n0[0], n0[1]) || 1, ln1 = Math.hypot(n1[0], n1[1]) || 1;+ let perim = 0;+ for (let j = 0; j < K; j++) {+ const A = sec[j], B = sec[(j + 1) % K], sn = SN[j], pv = SN[(j - 1 + K) % K], nx = SN[(j + 1) % K];+ const sA = sn[0] * pv[0] + sn[1] * pv[1] > cr, sB = sn[0] * nx[0] + sn[1] * nx[1] > cr;+ const nA = sA ? [sn[0] + pv[0], sn[1] + pv[1]] : sn, nB = sB ? [sn[0] + nx[0], sn[1] + nx[1]] : sn;+ const norm = (na, nb, nv, ln) => { const l = Math.hypot(na, nb) || 1; const a = na / l, b = nb / l; return [a * nv[0] / ln, a * nv[1] / ln, b]; };+ const na0 = norm(nA[0], nA[1], n0, ln0), nb0 = norm(nB[0], nB[1], n0, ln0), na1 = norm(nA[0], nA[1], n1, ln1), nb1 = norm(nB[0], nB[1], n1, ln1);+ const shA = A[2] === undefined ? s : A[2] * s, shB = B[2] === undefined ? s : B[2] * s;+ const a = this.v(p[0] + A[0] * m0[0], p[1] + A[0] * m0[1], z0 + A[1], na0[0], na0[1], na0[2], dist, perim, shA);+ const b = this.v(p[0] + B[0] * m0[0], p[1] + B[0] * m0[1], z0 + B[1], nb0[0], nb0[1], nb0[2], dist, perim + sn[2], shB);+ const c = this.v(q[0] + B[0] * m1[0], q[1] + B[0] * m1[1], z0 + B[1], nb1[0], nb1[1], nb1[2], dist + e[2], perim + sn[2], shB);+ const d = this.v(q[0] + A[0] * m1[0], q[1] + A[0] * m1[1], z0 + A[1], na1[0], na1[1], na1[2], dist + e[2], perim, shA);+ this.quad(a, b, c, d);+ perim += sn[2];+ }+ dist += e[2];+ rings.push(m0);+ }+ if (!closedPath && o.caps !== false) {+ const capAt = (p, nv, e, flip) => {+ const pts = sec.map((A) => [p[0] + A[0] * nv[0], p[1] + A[0] * nv[1], z0 + A[1]]);+ const idx = triangulate(sec.map((A) => [A[0], A[1]]));+ const ids = pts.map((q) => this.v(q[0], q[1], q[2], flip * e[0], flip * e[1], 0, q[0], q[2], s * 0.8));+ for (const t of idx) { if (flip > 0) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); else this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ };+ capAt(path[0], rings[0], E[0], -1);+ const lastE = E[segCount - 1]; capAt(path[n - 1], [-lastE[1], lastE[0]], lastE, 1);+ }+ return this;+ }+ // Generic tube along a 3D path with parallel transport. radii: number|array|fn(t) -> [ra, rb]; ring: fn(angle,t) multiplier+ tube(path, radii, n = 8, o = {}) {+ const N = path.length, s = o.s === undefined ? 1 : o.s;+ let T = [], U = o.up ? [...o.up] : null;+ for (let i = 0; i < N; i++) {+ const p = path[Math.max(0, i - 1)], q = path[Math.min(N - 1, i + 1)];+ let tx = q[0] - p[0], ty = q[1] - p[1], tz = q[2] - p[2]; const l = Math.hypot(tx, ty, tz) || 1; T.push([tx / l, ty / l, tz / l]);+ }+ if (!U) U = Math.abs(T[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];+ const base = this.count; const rowLen = n + 1; let dist = 0;+ for (let i = 0; i < N; i++) {+ const t = T[i];+ let d = U[0] * t[0] + U[1] * t[1] + U[2] * t[2];+ let ux = U[0] - d * t[0], uy = U[1] - d * t[1], uz = U[2] - d * t[2]; const ul = Math.hypot(ux, uy, uz) || 1; ux /= ul; uy /= ul; uz /= ul; U = [ux, uy, uz];+ const vx = t[1] * uz - t[2] * uy, vy = t[2] * ux - t[0] * uz, vz = t[0] * uy - t[1] * ux;+ const f = i / (N - 1);+ const rr = typeof radii === 'function' ? radii(f) : (Array.isArray(radii) ? radii[i] : radii);+ const ra = Array.isArray(rr) ? rr[0] : rr, rb = Array.isArray(rr) ? rr[1] : rr;+ if (i > 0) dist += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1], path[i][2] - path[i - 1][2]);+ for (let k = 0; k <= n; k++) {+ const a = TAU * k / n, c = Math.cos(a), sn = Math.sin(a);+ const mod = o.ring ? o.ring(a, f) : 1;+ const shade = o.ringS ? o.ringS(a, f) : 1;+ const px = path[i][0] + (ux * c * ra + vx * sn * rb) * mod, py = path[i][1] + (uy * c * ra + vy * sn * rb) * mod, pz = path[i][2] + (uz * c * ra + vz * sn * rb) * mod;+ const nx = ux * c / (ra || 1) + vx * sn / (rb || 1), ny = uy * c / (ra || 1) + vy * sn / (rb || 1), nz = uz * c / (ra || 1) + vz * sn / (rb || 1);+ this.v(px, py, pz, nx, ny, nz, k / n * Math.max(ra, rb) * TAU, dist, s * shade);+ }+ }+ for (let i = 0; i < N - 1; i++) for (let k = 0; k < n; k++) {+ const a = base + i * rowLen + k, b = a + 1, c = a + rowLen + 1, d = a + rowLen;+ this.quad(a, b, c, d);+ }+ if (o.capEnd !== false) { const c = this.v(path[N - 1][0], path[N - 1][1], path[N - 1][2], T[N - 1][0], T[N - 1][1], T[N - 1][2], 0, 0, s); const b2 = base + (N - 1) * rowLen; const ids = []; for (let k = 0; k < n; k++) ids.push(b2 + k); for (let k = 0; k < n; k++) this.tri(c, ids[k], b2 + k + 1); }+ if (o.capStart) { const c = this.v(path[0][0], path[0][1], path[0][2], -T[0][0], -T[0][1], -T[0][2], 0, 0, s); for (let k = 0; k < n; k++) this.tri(c, base + k + 1, base + k); }+ return this;+ }+ // parametric grid: fn(u,v) -> [x,y,z, shade?]; winding u x v = front. Smooth normals from the grid.+ grid(fn, nu, nv, o = {}) {+ const pts = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pts.push(fn(i / nu, j / nv));+ const id = (i, j) => j * (nu + 1) + i, base = this.count, flip = o.flip ? -1 : 1, s = o.s === undefined ? 1 : o.s;+ const wrapU = !!o.wrapU;+ let vLen = 0;+ for (let j = 0; j <= nv; j++) {+ if (j > 0) { const a = pts[id(0, j - 1)], b = pts[id(0, j)]; vLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ let uLen = 0;+ for (let i = 0; i <= nu; i++) {+ if (i > 0) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; uLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ const iP = wrapU ? (i === 0 ? nu - 1 : i - 1) : Math.max(0, i - 1), iN = wrapU ? (i === nu ? 1 : i + 1) : Math.min(nu, i + 1);+ const A = pts[id(iP, j)], B = pts[id(iN, j)], C = pts[id(i, Math.max(0, j - 1))], D = pts[id(i, Math.min(nv, j + 1))];+ const ux = B[0] - A[0], uy = B[1] - A[1], uz = B[2] - A[2], vx = D[0] - C[0], vy = D[1] - C[1], vz = D[2] - C[2];+ const p = pts[id(i, j)];+ const uvU = o.boxUV ? undefined : uLen, uvV = o.boxUV ? undefined : vLen;+ this.v(p[0], p[1], p[2], flip * (uy * vz - uz * vy), flip * (uz * vx - ux * vz), flip * (ux * vy - uy * vx), uvU, uvV, s * (p[3] === undefined ? 1 : p[3]));+ }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) {+ const a = base + id(i, j), b = base + id(i + 1, j), c = base + id(i + 1, j + 1), d = base + id(i, j + 1);+ if (flip > 0) this.quad(a, b, c, d); else this.quad(a, d, c, b);+ }+ return this;+ }+ // 2D outline with holes, extruded along Z from z0 to z1 (front +Z). Reveal sides included.+ slab(outline, holes = [], z0 = 0, z1 = 0.1, o = {}) {+ const so = area2(outline) > 0 ? outline : outline.slice().reverse();+ const hs = holes.map((h) => (area2(h) < 0 ? h : h.slice().reverse()));+ const tris = triangulate(so, hs);+ const all = so.concat(...hs);+ const sf = o.sf === undefined ? 1 : o.sf, sb = o.sb === undefined ? 1 : o.sb, sr = o.sr === undefined ? 0.72 : o.sr;+ if (!o.noFront) { const ids = all.map((p) => this.v(p[0], p[1], z1, 0, 0, 1, undefined, undefined, sf)); for (const t of tris) this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ if (!o.noBack) { const ids = all.map((p) => this.v(p[0], p[1], z0, 0, 0, -1, undefined, undefined, sb)); for (const t of tris) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); }+ const side = (loop, s) => {+ const L = loop.length; if (o.noSides) return;+ for (let i = 0; i < L; i++) {+ const p = loop[i], q = loop[(i + 1) % L]; const dx = q[0] - p[0], dy = q[1] - p[1];+ const l = Math.hypot(dx, dy) || 1, nx = dy / l, ny = -dx / l;+ const a = this.v(p[0], p[1], z0, nx, ny, 0, undefined, undefined, s), b = this.v(q[0], q[1], z0, nx, ny, 0, undefined, undefined, s), c = this.v(q[0], q[1], z1, nx, ny, 0, undefined, undefined, s), d = this.v(p[0], p[1], z1, nx, ny, 0, undefined, undefined, s);+ this.quad(a, b, c, d);+ }+ };+ if (!o.noOuterSides) side(so, o.so === undefined ? 0.85 : o.so);+ hs.forEach((h) => side(h, sr));+ return this;+ }+ // Vertical prism from an (x,z) polygon+ prism(pts, y0, y1, o = {}) {+ let A = 0; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; A += p[0] * q[1] - q[0] * p[1]; }+ const L = A > 0 ? pts : pts.slice().reverse(), s = o.s === undefined ? 1 : o.s, st = o.st === undefined ? s : o.st, n = L.length, k = o.topScale === undefined ? 1 : o.topScale;+ let cx = 0, cz = 0; for (const p of L) { cx += p[0]; cz += p[1]; } cx /= n; cz /= n;+ const top = L.map((p) => [cx + (p[0] - cx) * k, cz + (p[1] - cz) * k]);+ for (let i = 0; i < n; i++) {+ const p = L[i], q = L[(i + 1) % n], P2 = top[i], Q2 = top[(i + 1) % n];+ this.face([[q[0], y0, q[1], s], [p[0], y0, p[1], s], [P2[0], y1, P2[1], st], [Q2[0], y1, Q2[1], st]]);+ }+ if (!o.noTop && k > 1e-4) { const c = top.map((p) => this.v(p[0], y1, p[1], 0, 1, 0, undefined, undefined, st)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i + 1], c[i]); }+ if (o.bot) { const c = L.map((p) => this.v(p[0], y0, p[1], 0, -1, 0, undefined, undefined, s * 0.6)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i], c[i + 1]); }+ return this;+ }+ // Welds vertices that agree in every attribute: sweeps and lathes otherwise export many duplicates+ weld() {+ if (this.welded) return this;+ const n = this.count, map = new Map(), remap = new Int32Array(n), P2 = [], N2 = [], U2 = [], C2 = [];+ const { P: p, N: nn, UV: uv, C: c } = this, col = !this.noColor;+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, i2 = i * 2;+ const key = Math.round(p[i3] * 4000) + ',' + Math.round(p[i3 + 1] * 4000) + ',' + Math.round(p[i3 + 2] * 4000) + ',' + Math.round(nn[i3] * 90) + ',' + Math.round(nn[i3 + 1] * 90) + ',' + Math.round(nn[i3 + 2] * 90) + ',' + Math.round(uv[i2] * 800) + ',' + Math.round(uv[i2 + 1] * 800) + (col ? ',' + c[i3] + ',' + c[i3 + 1] + ',' + c[i3 + 2] : '');+ let k = map.get(key);+ if (k === undefined) { k = P2.length / 3; map.set(key, k); P2.push(p[i3], p[i3 + 1], p[i3 + 2]); N2.push(nn[i3], nn[i3 + 1], nn[i3 + 2]); U2.push(uv[i2], uv[i2 + 1]); C2.push(c[i3], c[i3 + 1], c[i3 + 2]); }+ remap[i] = k;+ }+ const I2 = [];+ for (let t = 0; t < this.I.length; t += 3) {+ const a = remap[this.I[t]], b = remap[this.I[t + 1]], d = remap[this.I[t + 2]];+ if (a !== b && b !== d && a !== d) I2.push(a, b, d);+ }+ this.P = P2; this.N = N2; this.UV = U2; this.C = C2; this.I = I2; this.welded = true;+ return this;+ }+ toGeo() {+ if (this.geo) return this.geo;+ this.weld();+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.P), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(this.N), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.UV), 2));+ if (!this.noColor) g.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(this.C), 3, true));+ g.setIndex(new THREE.BufferAttribute(this.count > 65535 ? new Uint32Array(this.I) : new Uint16Array(this.I), 1));+ this.geo = g;+ return g;+ }+}+function area2(p) { let a = 0; for (let i = 0; i < p.length; i++) { const q = p[(i + 1) % p.length]; a += p[i][0] * q[1] - q[0] * p[i][1]; } return a / 2; }+function triangulate(contour, holes = []) {+ const V = (p) => new THREE.Vector2(p[0], p[1]);+ const tris = THREE.ShapeUtils.triangulateShape(contour.map(V), holes.map((h) => h.map(V)));+ const all = contour.concat(...holes);+ return tris.map((t) => {+ const a = all[t[0]], b = all[t[1]], c = all[t[2]];+ const s = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ return s >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]];+ });+}++// ---- 2D curves -------------------------------------------------------------------+// Two-centred pointed arc: span w, rise h, base y=0 centred at x=0, from left spring to right spring+function pointedArc(w, h, n = 8) {+ const c = (h * h - w * w / 4) / w, r = c + w / 2, pts = [];+ const a0 = PI, a1 = Math.atan2(h, -c);+ for (let i = 0; i <= n; i++) { const a = lerp(a0, a1, i / n); pts.push([c + r * Math.cos(a), r * Math.sin(a)]); }+ for (let i = n - 1; i >= 0; i--) { const a = lerp(a0, a1, i / n); pts.push([-(c + r * Math.cos(a)), r * Math.sin(a)]); }+ pts[n][0] = 0;+ return pts;+}+// CCW outline of a pointed opening: width w, sill y0, spring ys, rise h+function archOutline(w, y0, ys, h, n = 8) {+ const arc = pointedArc(w, h, n).map((p) => [p[0], p[1] + ys]);+ arc.reverse(); // right spring -> apex -> left spring (CCW)+ return [[-w / 2, y0], [w / 2, y0], ...arc];+}+const ringPts = (cx, cy, r, n = 24, ph = 0) => { const a = []; for (let i = 0; i < n; i++) { const t = ph + TAU * i / n; a.push([cx + r * Math.cos(t), cy + r * Math.sin(t)]); } return a; };+const ofsY = (pts, dy) => pts.map((p) => [p[0], p[1] + dy]);+const ofsXY = (pts, dx, dy) => pts.map((p) => [p[0] + dx, p[1] + dy]);++// ---- Sections (a, b, shade?) closed CCW ------------------------------------------+// Moulded arch section with convex rolls: a=0 at the soffit, grows outward; symmetric in b.+function rollSection(w, d, rolls = 3, amp = 0.07, seg = 3) {+ const half = [];+ const A = [w, d / 2], B = [0, d * 0.12], dx = B[0] - A[0], dy = B[1] - A[1], l = Math.hypot(dx, dy);+ for (let k = 0; k < rolls; k++) for (let i = 0; i < seg; i++) {+ const t = (k + i / seg) / rolls, ph = i / seg;+ const off = amp * Math.pow(Math.sin(PI * ph), 0.7), sh = 0.68 + 0.32 * Math.sin(PI * ph);+ half.push([lerp(A[0], B[0], t) + off * dy / l, lerp(A[1], B[1], t) - off * dx / l, sh]);+ }+ half.push([B[0], B[1], 0.8]);+ const back = half.slice().reverse().map((p) => [p[0], -p[1], p[2]]);+ return [[w, -d / 2, 1], ...half, ...back.slice(0, -1)];+}+// Cheapest moulded bar: a hexagon, smooth enough to read as a roll at distance+function hexBar(w, d) { const a = w / 2, b = d / 2; return [[a, -b * 0.5], [a, b * 0.5], [0, b], [-a, b * 0.5], [-a, -b * 0.5], [0, -b]]; }+// Simple chamfered bar centred on the path, width w (in plane), depth d+function barSection(w, d, ch = 0.25) {+ const a = w / 2, b = d / 2, c = Math.min(a, b) * ch * 2;+ return [[-a, -b + c], [-a + c, -b], [a - c, -b], [a, -b + c], [a, b - c], [a - c, b], [-a + c, b], [-a, b - c]];+}+// Tracery bar with a roll on the front and back faces+function traceryBar(w, d) {+ const a = w / 2, b = d / 2, s = [];+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([a * Math.cos(t), b * 0.62 + b * 0.38 * Math.sin(t)]); }+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([-a * Math.cos(t), -b * 0.62 - b * 0.38 * Math.sin(t)]); }+ return s;+}+// String course / cornice projecting along +b from the wall at b=0, height h (a)+function stringSection(h = 0.3, out = 0.25) {+ return [[-h / 2, 0], [-h / 2, out * 0.3, 0.55], [-h * 0.25, out * 0.62, 0.7], [-h * 0.02, out * 0.72, 0.85], [h * 0.12, out, 1], [h * 0.2, out * 0.98, 1], [h / 2, out * 0.05, 1], [h / 2, 0]].reverse();+}++const spline = (pts, sub = 4) => { // Catmull-Rom through 3D points+ const out = [], n = pts.length, g = (i) => pts[clamp(i, 0, n - 1)];+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < sub; k++) {+ const t = k / sub, p0 = g(i - 1), p1 = g(i), p2 = g(i + 1), p3 = g(i + 2), v = [];+ for (let c = 0; c < 3; c++) v.push(0.5 * (2 * p1[c] + (p2[c] - p0[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t * t + (3 * p1[c] - p0[c] - 3 * p2[c] + p3[c]) * t * t * t));+ out.push(v);+ }+ out.push(pts[n - 1]);+ return out;+};+const blob = (b, c, r, nu = 8, nv = 6, sh = 1) => b.grid((u, v) => {+ const ph = -PI + TAU * u, la = -PI / 2 + PI * v;+ return [c[0] + r[0] * Math.cos(la) * Math.cos(ph), c[1] + r[1] * Math.sin(la), c[2] + r[2] * Math.cos(la) * Math.sin(ph), sh];+}, nu, nv, { flip: true, wrapU: true });++// ==================================================================================+// MATERIALS: procedural PBR, with height-derived normals+// ==================================================================================+const MAT = {};+const TILE = { ashlar: 4, fine: 2, lead: 4, pave: 2.4, oak: 1.2, iron: 0.8, gold: 1, carved: 1.6 };++function ashlarOps(rng, S, tone, joint, stains) {+ // Irregular coursed blocks; shared between albedo and height by replaying the same rng seed+ const ops = [], rows = 6, rowH = Math.round(S / rows);+ for (let r = 0; r < rows; r++) {+ const y0 = r * rowH; let x = Math.round(rng() * S / 5) % S;+ const n = 3 + Math.floor(rng() * 2), widths = [];+ for (let i = 0; i < n; i++) widths.push(0.7 + rng() * 0.6);+ const tot = widths.reduce((a, c) => a + c, 0);+ for (let i = 0; i < n; i++) {+ const w = Math.round(widths[i] / tot * S), dv = rng();+ const col = dv > 0.5 ? tone[0] : tone[1];+ const x0 = x % S, wr = Math.min(w, S - x0);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y0, width: wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: Math.round(8 + rng() * 22) });+ if (wr < w) ops.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: y0, width: w - wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: 24 });+ x += w;+ ops.push({ op: 'paint', shape: { kind: 'line', from: [x % S, y0], to: [x % S, y0 + rowH], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y0], to: [S, y0], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ return ops.slice(0, 56).concat(stains || []);+}++async function makeMaterials() {+ const S = 512;+ const noise = (a, b, scale, oct, seed, opacity, blend) => ({ op: 'noise', colorA: a, colorB: b, scale, octaves: oct, seed, opacity, blend: blend || 'normal' });+ const v3 = (usage, base, ops, size = S) => proceduralTexture({ schemaVersion: 3, width: size, height: size, usage, operations: [{ op: 'legacy-v2', spec: { schemaVersion: 2, size, usage, layers: base } }, ...ops] });+ const stamps = (w, h, rgba, count, seed, opacity, size = S) => ({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: w, height: h }, rgba, count, seed, area: { x: 0, y: 0, width: size, height: size }, opacity });++ // Weathered ashlar limestone: irregular coursed blocks, pitting, rain stains+ const ashA = v3('albedo', [{ op: 'solid', color: 0xd6ccb4 }, noise(0xb4a98f, 0xe6decb, 3, 3, 11, 0.85), noise(0x8f8671, 0xdcd3bf, 19, 3, 12, 0.45)], ashlarOps(mkRng(77), S, [[92, 84, 68], [242, 234, 214]], { w: 2, rgba: [96, 88, 74, 255], op: 215 }, [+ stamps(26, 40, [84, 80, 68, 255], 30, 5, 22), stamps(5, 4, [86, 80, 66, 255], 90, 6, 44), stamps(2, 2, [70, 64, 54, 255], 130, 7, 70),+ ]));+ const ashH = v3('albedo', [{ op: 'solid', color: 0xb8b8b8 }, noise(0x8a8a8a, 0xd8d8d8, 5, 3, 21, 0.75), noise(0x707070, 0xd0d0d0, 34, 4, 22, 0.6)], ashlarOps(mkRng(77), S, [[110, 110, 110], [235, 235, 235]], { w: 3, rgba: [18, 18, 18, 255], op: 255 }, [+ stamps(5, 4, [84, 84, 84, 255], 86, 8, 96), stamps(2, 2, [64, 64, 64, 255], 160, 9, 130),+ ]));+ MAT.ashlar = pbrMaterial({ albedo: ashA, normal: normalMapFromHeight(ashH, { strength: 3.2 }), roughness: 0.93, metalness: 0, name: 'Limestone_Ashlar' });++ // Fine dressed limestone (mouldings, tracery, figures): no joints, weathered pits+ const finA = v3('albedo', [{ op: 'solid', color: 0xd9d0ba }, noise(0xbcb29a, 0xe9e2d0, 4, 3, 31, 0.8), noise(0x9d9480, 0xe4dcc8, 23, 3, 32, 0.4)], [+ stamps(2, 2, [112, 104, 90, 255], 170, 33, 40), stamps(20, 30, [106, 100, 86, 255], 30, 34, 18),+ ]);+ const finH = v3('albedo', [{ op: 'solid', color: 0xb0b0b0 }, noise(0x8c8c8c, 0xd4d4d4, 9, 3, 35, 0.7), noise(0x787878, 0xd8d8d8, 44, 4, 36, 0.65)], [+ stamps(4, 4, [96, 96, 96, 255], 50, 37, 90), stamps(2, 2, [80, 80, 80, 255], 200, 38, 120),+ ]);+ MAT.fine = pbrMaterial({ albedo: finA, normal: normalMapFromHeight(finH, { strength: 1.5 }), roughness: 0.9, metalness: 0, name: 'Limestone_Dressed' });++ // Patinated lead: pale bloom over blue-grey, standing seams and lapped sheets, streaks+ const leadOps = (seam, lap, base) => { const ops = []; for (let i = 0; i < 4; i++) ops.push({ op: 'paint', shape: { kind: 'line', from: [i * S / 4 + 20, 0], to: [i * S / 4 + 20, S], width: seam.w }, rgba: seam.rgba, opacity: seam.op }); for (let i = 0; i < 4; i++) for (let k = 0; k < 4; k++) { const y = ((k * S / 4 + (i % 2) * S / 8) | 0) + 16; ops.push({ op: 'paint', shape: { kind: 'line', from: [i * S / 4 + 20, y], to: [Math.min(S, i * S / 4 + 20 + S / 4), y], width: lap.w }, rgba: lap.rgba, opacity: lap.op }); } return ops; };+ const leadA = v3('albedo', [{ op: 'solid', color: 0x7f8a8e }, noise(0x5e6a70, 0xaab4b4, 4, 4, 41, 0.9), noise(0x6b7a7a, 0xc9d1cc, 14, 3, 42, 0.5)], [...leadOps({ w: 3, rgba: [58, 64, 68, 255], op: 170 }, { w: 2, rgba: [66, 72, 76, 255], op: 120 }), stamps(16, 90, [205, 212, 206, 255], 28, 43, 34), stamps(14, 80, [40, 48, 52, 255], 26, 44, 40)]);+ const leadH = v3('albedo', [{ op: 'solid', color: 0x9a9a9a }, noise(0x8a8a8a, 0xb0b0b0, 7, 3, 45, 0.8)], leadOps({ w: 7, rgba: [244, 244, 244, 255], op: 255 }, { w: 3, rgba: [60, 60, 60, 255], op: 200 }));+ MAT.lead = pbrMaterial({ albedo: leadA, normal: normalMapFromHeight(leadH, { strength: 3.4 }), roughness: 0.56, metalness: 0.42, name: 'Lead_Patinated' });++ // Gilding: slight wear so highlights break up+ const gldA = v3('albedo', [{ op: 'solid', color: 0xe4b64a }, noise(0xb5862c, 0xf3cf6a, 12, 3, 51, 0.6)], [], 128);+ const gldH = v3('albedo', [{ op: 'solid', color: 0xa0a0a0 }, noise(0x808080, 0xc0c0c0, 22, 3, 52, 0.8)], [], 128);+ MAT.gold = pbrMaterial({ albedo: gldA, normal: normalMapFromHeight(gldH, { strength: 1 }), roughness: 0.27, metalness: 1, name: 'Gilding' });++ // Aged oak and forged iron from the approved scanned sets+ MAT.oak = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.oak-veneer-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.oak-veneer-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.oak-veneer-arm.v1'), name: 'Oak_Aged' });+ MAT.iron = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.rusted-metal-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.rusted-metal-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.rusted-metal-arm.v1'), name: 'Iron_Forged' });++ // Polished paving: chequered limestone and dark marble+ const pavA = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'checker', colorA: 0xd2cab6, colorB: 0x8c8678, squares: 4 }, noise(0x7c766a, 0xe0dac8, 9, 3, 61, 0.42), { op: 'bricks', brick: 0xffffff, mortar: 0x5a564e, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ const pavH = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'solid', color: 0xc8c8c8 }, noise(0xb0b0b0, 0xdcdcdc, 15, 3, 62, 0.5), { op: 'bricks', brick: 0xffffff, mortar: 0x202020, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ MAT.pave = pbrMaterial({ albedo: pavA, normal: normalMapFromHeight(pavH, { strength: 1.8 }), roughness: 0.38, metalness: 0, name: 'Paving_Polished' });++ // Carved limestone: a lozenge diaper of quatrefoils, its relief carried in the normal map+ const carvedOps = (hi, lo, mid, deep) => {+ const T = 512, ops = [], paint = (shape, rgba, opacity) => ops.push({ op: 'paint', shape, rgba: [rgba, rgba, rgba, 255].map((v, i) => (i === 3 ? 255 : v)), opacity });+ const lat = (a, c) => { paint({ kind: 'line', from: a, to: c, width: 16 }, mid, 255); paint({ kind: 'line', from: a, to: c, width: 7 }, hi, 255); };+ lat([0, 0], [T, T]); lat([T, 0], [0, T]); lat([0, T / 2], [T / 2, 0]); lat([T / 2, T], [T, T / 2]); lat([0, T / 2], [T / 2, T]); lat([T / 2, 0], [T, T / 2]);+ for (const [cx, cy] of [[T / 4, T / 2], [3 * T / 4, T / 2], [T / 2, T / 4], [T / 2, 3 * T / 4]]) {+ const lobe = 44, off = 34;+ for (const [dx, dy] of [[-off, 0], [off, 0], [0, -off], [0, off]]) paint({ kind: 'ellipse', x: cx + dx - lobe, y: cy + dy - lobe, width: lobe * 2, height: lobe * 2 }, mid, 255);+ for (const [dx, dy] of [[-off, 0], [off, 0], [0, -off], [0, off]]) paint({ kind: 'ellipse', x: cx + dx - lobe + 9, y: cy + dy - lobe + 9, width: lobe * 2 - 18, height: lobe * 2 - 18 }, lo, 255);+ paint({ kind: 'ellipse', x: cx - 22, y: cy - 22, width: 44, height: 44 }, hi, 255);+ paint({ kind: 'ellipse', x: cx - 11, y: cy - 11, width: 22, height: 22 }, deep, 255);+ }+ return ops.slice(0, 60);+ };+ const carH = proceduralTexture({ schemaVersion: 3, width: 512, height: 512, usage: 'albedo', background: [120, 120, 120, 255], operations: carvedOps(236, 96, 176, 60) });+ // The albedo only hints at the pattern (dust in the cuts); the relief itself lives in the normals+ const carAlb = proceduralTexture({ schemaVersion: 3, width: 512, height: 512, usage: 'albedo', background: [198, 190, 170, 255], operations: carvedOps(214, 178, 204, 150).map((op) => { const g = op.rgba[0]; return Object.assign({}, op, { rgba: [g, Math.round(g * 0.955), Math.round(g * 0.86), 255] }); }) });+ MAT.carved = pbrMaterial({ albedo: carAlb, normal: normalMapFromHeight(carH, { strength: 4.5 }), roughness: 0.92, metalness: 0, name: 'Limestone_Carved' });++ for (const k of ['ashlar', 'fine', 'carved', 'oak', 'iron', 'pave']) MAT[k].vertexColors = true;++ // Pot-metal glass palette: one texture of hand-mixed colours with streaks and seeds.+ // Panes carry UVs into their swatch, so every window shares a single material.+ const palOps = (usage) => {+ const n = PALETTE_COLS, cell = Math.floor(PAL_SIZE / n), ops = [];+ PALETTE.forEach((p, i) => ops.push({ op: 'paint', shape: { kind: 'rectangle', x: (i % n) * cell, y: Math.floor(i / n) * cell, width: cell, height: cell }, rgba: [p[1], p[2], p[3], 255] }));+ return ops;+ };+ const palStreaks = [];+ const srng = mkRng(4242);+ for (let i = 0; i < 16; i++) { // horizontal streaks: pot metal is never one flat colour+ const y = Math.floor(srng() * PAL_SIZE), dark = srng() > 0.42;+ palStreaks.push({ op: 'paint', shape: { kind: 'line', from: [0, y], to: [PAL_SIZE, Math.min(PAL_SIZE, y + Math.floor(srng() * 26))], width: 3 + Math.floor(srng() * 7) }, rgba: dark ? [0, 0, 0, 255] : [255, 255, 255, 255], opacity: dark ? 40 + Math.floor(srng() * 50) : 16 + Math.floor(srng() * 26) });+ }+ for (let i = 0; i < 12; i++) { // and vertical ones, so each pane samples a different tone+ const x = Math.floor(srng() * PAL_SIZE), dark = srng() > 0.4;+ palStreaks.push({ op: 'paint', shape: { kind: 'line', from: [x, 0], to: [Math.min(PAL_SIZE, x + Math.floor(srng() * 18)), PAL_SIZE], width: 2 + Math.floor(srng() * 6) }, rgba: dark ? [0, 0, 0, 255] : [255, 255, 255, 255], opacity: dark ? 34 + Math.floor(srng() * 44) : 14 + Math.floor(srng() * 22) });+ }+ palStreaks.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 3, height: 3 }, rgba: [255, 255, 255, 255], count: 110, seed: 71, area: { x: 0, y: 0, width: PAL_SIZE, height: PAL_SIZE }, opacity: 34 });+ const palSpec = (usage) => ({ schemaVersion: 3, width: PAL_SIZE, height: PAL_SIZE, usage, background: [10, 10, 12, 255], operations: [...palOps(usage), ...palStreaks] });+ const palE = proceduralTexture(palSpec('emissive')), palA = proceduralTexture(palSpec('albedo'));+ MAT.glassIn = pbrMaterial({ emissive: palE, emissiveIntensity: P.glassGlow, roughness: 0.62, metalness: 0, name: 'StainedGlass_Interior' });+ MAT.glassIn.color.setRGB(0.012, 0.012, 0.012);+ // From outside, stained glass by day is deep and glossy, its colours held back+ MAT.glassOut = pbrMaterial({ albedo: palA, emissive: palE, emissiveIntensity: P.glassOuterGlow, roughness: 0.14, metalness: 0, name: 'StainedGlass_Exterior' });+ MAT.glassOut.color.setRGB(0.62, 0.62, 0.66);+ MAT.pool = pbrMaterial({ emissive: palE, emissiveIntensity: 0.9, roughness: 0.9, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightPool' });+ MAT.pool.color.setRGB(0.01, 0.01, 0.01);+ MAT.pool.opacity = 0.27;+ MAT.pool.depthWrite = false;+ // Statues indoors share their geometry with those outside, under a dimmer stone+ MAT.fineDim = copyMaterial(MAT.fine);+ MAT.fineDim.name = 'Limestone_Dressed_Interior';+ MAT.fineDim.color.setRGB(0.58, 0.55, 0.5);+ MAT.fineDim.vertexColors = true;+}++// Hand-mixed glass colours: [name, r, g, b]+const PALETTE = [+ ['ruby', 176, 16, 28], ['rubyLt', 214, 44, 40], ['cobalt', 16, 38, 158], ['azure', 28, 92, 200], ['sky', 92, 156, 224], ['pale', 178, 212, 234],+ ['emerald', 14, 116, 60], ['green', 64, 148, 62], ['olive', 128, 136, 40], ['gold', 242, 178, 36], ['amber', 222, 130, 26], ['yellow', 252, 222, 96],+ ['white', 240, 234, 206], ['pearl', 208, 208, 190], ['flesh', 240, 194, 152], ['fleshDk', 208, 152, 116], ['violet', 96, 38, 146], ['purple', 140, 48, 124],+ ['rose', 212, 84, 122], ['brown', 112, 64, 34], ['tan', 176, 122, 70], ['grey', 124, 124, 118], ['teal', 18, 112, 124], ['lead', 10, 10, 12],+ ['paint', 34, 22, 16], ['night', 10, 18, 66], ['crimson', 122, 10, 24], ['forest', 8, 72, 40], ['hair', 72, 46, 24], ['bone', 228, 218, 182],+];+const PALETTE_COLS = 6, PAL_SIZE = 252;+const PAL = {}; PALETTE.forEach((p, i) => { PAL[p[0]] = i; });++// Hierarchy helper: every mesh gets a unique role name; geometry is shared between placements.+let ROOT = null; const NAMES = new Map();+function uniq(n) { const c = (NAMES.get(n) || 0) + 1; NAMES.set(n, c); return c === 1 ? n : n + '_' + c; }+const STAT = new Map();+// Exporter costs measured on this host: pos+normal+uv 32 B, COLOR_0 written as 12 B floats+function batchBytes(b) { b.weld(); return b.count * (32 + (b.noColor ? 0 : 12)) + b.I.length * (b.count > 65535 ? 4 : 2); }+function place(name, batch, mat, o = {}) {+ if (!batch || !batch.I.length) return null;+ if (mat === MAT.lead || mat === MAT.gold) batch.noColor = true; // no baked shading needed: saves 12 B a vertex+ if (P.preview === 'unique' && batch.geo) return null; // size test: every geometry once, no instances+ if (P.preview === 'stats') {+ const e = STAT.get(batch) || { name, n: 0 };+ e.n++; STAT.set(batch, e);+ return null;+ }+ return createPart(uniq(name), batch.toGeo(), mat, { position: o.pos || [0, 0, 0], rotation: o.rot || [0, 0, 0], scale: o.scale || [1, 1, 1], parent: o.parent || ROOT });+}+function group(name, parent) { const g = new THREE.Group(); g.name = uniq(name); (parent || ROOT).add(g); return g; }++// ==================================================================================+// SCULPTURE: robed figures with carved drapery. A figure is a full surface, not a block:+// S-curve stance, tube folds breaking at the hem, swags across the body, sleeves, hands,+// modelled faces, hair and beards, and the attributes each saint carries.+// ==================================================================================+const FIGURES = {+ apostle: { beard: 1, hold: 'book', sway: 1, seed: 1 },+ peter: { beard: 1, hold: 'key', sway: -1, seed: 2, bald: 1 },+ paul: { beard: 2, hold: 'sword', sway: 1, seed: 3 },+ bishop: { beard: 0, hold: 'crozier', sway: -1, seed: 4, mitre: 1 },+ martyr: { veil: 1, hold: 'palm', sway: 1, seed: 5, slim: 0.92 },+ deacon: { beard: 0, hold: 'bookOpen', sway: -1, seed: 6, curls: 1 },+ prophet: { beard: 2, hold: 'scroll', sway: 1, seed: 7, cap: 1 },+ virgin: { veil: 1, crown: 1, hold: 'child', sway: 1, seed: 8, slim: 0.94 },+ christ: { beard: 1, hold: 'bless', sway: -1, seed: 9, nimbus: 1 },+ kingA: { beard: 1, crown: 1, hold: 'sceptre', sway: 1, seed: 10, broad: 1.06 },+ kingB: { beard: 2, crown: 1, hold: 'orb', sway: -1, seed: 11, broad: 1.06 },+ kingC: { beard: 0, crown: 1, hold: 'sceptre', sway: -1, seed: 12, broad: 1.04, curls: 1 },+ angel: { beard: 0, hold: 'censer', sway: 1, seed: 13, wings: 1, curls: 1, slim: 0.95 },+};++function addHead(b, o, lod) {+ const nu = lod ? 22 : 8, nv = lod ? 17 : 5;+ b.grid((u, v) => {+ const su = 2 * u - 1, sv = 2 * v - 1;+ // samples crowd towards the face, where the carving is+ const phi = lod ? PI * Math.sign(su) * Math.pow(Math.abs(su), 1.55) : PI * su;+ const lam = lod ? -0.1 + (sv < 0 ? 1.44 : 1.64) * Math.sign(sv) * Math.pow(Math.abs(sv), 1.35) : (PI / 2 - 0.03) * sv;+ const a = Math.abs(phi);+ let d = 0, sh = 1, dy = 0;+ d += 0.034 * gauss(phi / 0.12, (lam + 0.04) / 0.19); // nose+ d += 0.016 * gauss(phi / 0.62, (lam - 0.25) / 0.075); // brow+ d -= 0.015 * gauss((a - 0.42) / 0.16, (lam - 0.1) / 0.08); // sockets+ d += 0.006 * gauss((a - 0.42) / 0.09, (lam - 0.08) / 0.04); // eyeballs+ d += 0.009 * gauss((a - 0.72) / 0.24, (lam + 0.12) / 0.16); // cheekbones+ d -= 0.007 * gauss(phi / 0.27, (lam + 0.4) / 0.028); // mouth+ d += 0.006 * gauss(phi / 0.22, (lam + 0.33) / 0.045); // upper lip+ d += 0.006 * gauss(phi / 0.18, (lam + 0.47) / 0.045); // lower lip+ d += 0.014 * gauss(phi / 0.26, (lam + 0.74) / 0.13); // chin+ sh -= 0.3 * gauss((a - 0.42) / 0.17, (lam - 0.14) / 0.05); // shadow under the brow+ sh -= 0.42 * gauss((a - 0.42) / 0.11, (lam - 0.085) / 0.026); // lid line+ sh -= 0.32 * gauss(phi / 0.25, (lam + 0.4) / 0.022);+ sh -= 0.18 * gauss((a - 0.17) / 0.06, (lam + 0.1) / 0.09); // beside the nose+ sh -= 0.12 * gauss(phi / 0.3, (lam + 0.6) / 0.05); // under the lip+ // Hair+ const line = o.bald ? 0.85 - 1.55 * (1 - Math.cos(phi)) / 2 : 0.56 - 1.2 * (1 - Math.cos(phi)) / 2;+ const hair = sstep(-0.03, 0.06, lam - line) * (o.bald && lam > 0.95 ? 0 : 1);+ const strands = Math.abs(Math.sin(phi * (o.curls ? 11 : 16) + lam * (o.curls ? 9 : 3)));+ d += hair * ((o.curls ? 0.024 : 0.016) + (o.curls ? 0.012 : 0.006) * strands);+ sh -= hair * 0.34 * (1 - strands);+ // Beard: a modelled mass that falls below the chin+ if (o.beard) {+ // starts below the lower lip at the centre, climbs the jaw to the ears; the mouth stays clear+ const startLam = lerp(-0.56, -0.12, sstep(0.25, 1.15, a));+ const bm = sstep(startLam + 0.06, startLam - 0.08, lam) * sstep(1.65, 1.25, a);+ const len = o.beard === 2 ? 0.11 : 0.05;+ const rid = Math.abs(Math.sin(phi * 13 + lam * 1.6));+ d += bm * (0.012 + 0.008 * rid);+ dy -= bm * sstep(-0.5, -1.25, lam) * len;+ sh -= bm * 0.1 * (1 - rid);+ }+ const cl = Math.cos(lam), sl = Math.sin(lam);+ const x = (0.098 + d) * cl * Math.cos(phi), y = (0.122 + d * 0.8) * sl + dy, z = (0.082 + d * 0.9) * cl * Math.sin(phi);+ return [x, y, z, clamp(sh, 0.3, 1)];+ }, nu, nv, { flip: true, wrapU: true });+}++function addVeil(b, lod) { // a hood falling to the shoulders+ const nu = lod ? 24 : 10, nv = lod ? 12 : 5;+ b.grid((u, v) => {+ const a = lerp(0.95, TAU - 0.95, u), t = v; // open at the front (a=0)+ const lam = lerp(-1.25, 1.45, t), cl = Math.cos(lam);+ const fold = Math.abs(Math.sin(a * 5 + t)) * (1 - t);+ const k = 1 + 0.25 * (1 - t) + 0.05 * fold;+ return [(0.125 * k) * cl * Math.cos(a) - 0.012, (0.15 + 0.02 * (1 - t)) * Math.sin(lam) + (t < 0.4 ? -(0.4 - t) * 0.1 : 0), 0.105 * k * cl * Math.sin(a), 0.62 + 0.38 * fold * (1 - t) + 0.38 * t];+ }, nu, nv, { flip: false });+}++function addAttribute(b, g, kind, lod, H) {+ const B = 0.075; // hand scale+ const rod = (p, q, r, n = 6) => b.tube([p, q], r, lod ? n : 4, { capStart: true });+ switch (kind) {+ case 'book': case 'bookOpen': {+ b.at(M.of(M.T(0.24, 1.3, 0.03), M.Rz(-58), M.Ry(10)), () => {+ b.box(-0.015, 0.015, -0.13, 0.13, -0.09, 0.09, { s: 0.85 });+ b.box(0.015, 0.03, -0.122, 0.122, -0.086, 0.086, { s: 1 });+ if (lod) for (let i = -1; i <= 1; i += 2) for (let k = -1; k <= 1; k += 2) b.box(-0.022, 0.022, i * 0.1 - 0.012, i * 0.1 + 0.012, k * 0.065 - 0.012, k * 0.065 + 0.012, { s: 0.7 });+ });+ break;+ }+ case 'key': {+ const y0 = 1.38;+ rod([0.25, y0 - 0.42, 0.12], [0.25, y0 + 0.28, 0.12], 0.016);+ b.at(M.T(0.25, y0 + 0.3, 0.12), () => { b.at(M.Rx(90), () => b.lathe([[0.05, -0.012], [0.06, 0], [0.05, 0.012], [0.03, 0.012], [0.02, 0], [0.03, -0.012], [0.05, -0.012]], lod ? 12 : 6)); });+ b.box(0.24, 0.26, y0 - 0.42, y0 - 0.33, 0.12, 0.2, { s: 0.9 });+ b.box(0.24, 0.26, y0 - 0.3, y0 - 0.26, 0.12, 0.17, { s: 0.9 });+ break;+ }+ case 'sword': {+ rod([0.27, 0.32, 0.17], [0.27, 1.32, 0.17], 0.018, 4);+ b.at(M.T(0.27, 1.33, 0.17), () => { b.box(-0.022, 0.022, -0.0, 0.03, -0.11, 0.11, { s: 0.9 }); b.lathe([[0.01, 0.03], [0.02, 0.08], [0.03, 0.12], [0.02, 0.15], [0, 0.16]], 6); });+ break;+ }+ case 'crozier': {+ rod([0.3, 0.02, 0.2], [0.3, 2.05, 0.2], 0.018);+ const volute = []; for (let i = 0; i <= 14; i++) { const t = i / 14, a = -PI * 0.5 + t * PI * 2.4, r = lerp(0.11, 0.03, t); volute.push([0.3 + (r * Math.cos(a) + r * 0.1), 2.14 + r * Math.sin(a), 0.2]); }+ (g || b).tube(volute, (t) => lerp(0.022, 0.012, t), lod ? 6 : 4, { capStart: true });+ blob(g || b, [0.3, 2.02, 0.2], [0.036, 0.036, 0.036], 6, 4);+ break;+ }+ case 'palm': {+ const stem = []; for (let i = 0; i <= 8; i++) { const t = i / 8; stem.push([0.25 + 0.03 * Math.sin(t * 2), 1.15 + t * 0.85, 0.15 - 0.03 * t]); }+ b.tube(stem, (t) => lerp(0.012, 0.004, t), 4);+ if (lod) for (let i = 2; i < 8; i++) for (let s = -1; s <= 1; s += 2) { const p = stem[i], L = 0.13 * (1 - i / 11); b.tube([p, [p[0] + 0.01, p[1] + L * 0.4, p[2] + s * L * 0.8], [p[0] + 0.015, p[1] + L * 0.2, p[2] + s * L * 1.25]], (t) => lerp(0.01, 0.002, t), 3); }+ break;+ }+ case 'scroll': {+ const path = []; for (let i = 0; i <= 12; i++) { const t = i / 12; path.push([0.25 + 0.03 * Math.sin(t * 5), 1.42 - t * 0.85, 0.08 * Math.sin(t * 3) - 0.04]); }+ b.grid((u, v) => { const k = u * 12, i = Math.min(11, Math.floor(k)), f = k - i, p = path[i], q = path[i + 1]; return [lerp(p[0], q[0], f), lerp(p[1], q[1], f), lerp(p[2], q[2], f) + (v - 0.5) * 0.17, 0.9]; }, lod ? 12 : 4, 1, { flip: false });+ b.grid((u, v) => { const k = u * 12, i = Math.min(11, Math.floor(k)), f = k - i, p = path[i], q = path[i + 1]; return [lerp(p[0], q[0], f) - 0.003, lerp(p[1], q[1], f), lerp(p[2], q[2], f) + (v - 0.5) * 0.17, 0.75]; }, lod ? 12 : 4, 1, { flip: true });+ b.at(M.of(M.T(0.25, 1.44, -0.04), M.Rx(90)), () => b.lathe([[0.024, -0.1], [0.026, 0.1]], 6));+ break;+ }+ case 'sceptre': {+ rod([0.27, 0.95, 0.19], [0.27, 2.0, 0.19], 0.016);+ const c = g || b;+ blob(c, [0.27, 2.03, 0.19], [0.042, 0.042, 0.042], 8, 6);+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; c.tube([[0.27, 2.04, 0.19], [0.27 + 0.05 * Math.cos(a), 2.09, 0.19 + 0.05 * Math.sin(a)], [0.27 + 0.03 * Math.cos(a), 2.14, 0.19 + 0.03 * Math.sin(a)]], (t) => lerp(0.014, 0.003, t), 4); }+ break;+ }+ case 'orb': {+ const c = g || b;+ blob(c, [0.27, 1.34, 0.14], [0.08, 0.08, 0.08], 12, 8);+ c.box(0.262, 0.278, 1.41, 1.52, 0.132, 0.148, { s: 1 }); c.box(0.262, 0.278, 1.46, 1.48, 0.1, 0.18, { s: 1 });+ break;+ }+ case 'child': {+ // The Christ child on the left arm, a small swaddled figure with its own head+ const c = [0.21, 1.3, -0.12];+ b.tube([[c[0] - 0.02, c[1] - 0.16, c[2]], [c[0], c[1], c[2] + 0.02], [c[0] + 0.02, c[1] + 0.18, c[2]]], (t) => [lerp(0.06, 0.075, Math.sin(t * PI)), lerp(0.055, 0.075, Math.sin(t * PI))], lod ? 10 : 5, { capStart: true });+ b.at(M.of(M.T(c[0] + 0.03, c[1] + 0.27, c[2]), M.S(0.56)), () => addHead(b, { curls: 1 }, 0));+ break;+ }+ case 'censer': {+ const c = g || b;+ blob(c, [0.32, 0.78, 0.13], [0.06, 0.055, 0.06], 8, 6);+ c.tube([[0.32, 0.83, 0.13], [0.3, 1.2, 0.13]], 0.005, 3);+ break;+ }+ default: break;+ }+}++function addWing(b, side, lod) {+ const nu = lod ? 12 : 5, nv = lod ? 6 : 2;+ const fn = (off) => (u, v) => {+ const len = lerp(0.1, 1.42, u), wid = 0.34 * Math.sin(PI * Math.pow(u, 0.62)) * (1 - 0.25 * u);+ const feather = Math.abs(Math.sin(v * 7 + u * 6)) * 0.018 + Math.abs(Math.sin(u * 22)) * 0.008 * (1 - v);+ const x = -0.12 - 0.17 * u - v * wid * 0.72 + off, y = 1.72 + 0.22 * Math.sin(u * 1.6) - len * 0.9 * u - v * wid * 0.25, z = side * (0.17 + 0.2 * u + v * wid * 0.42) ;+ return [x + feather, y, z, 0.68 + 7 * feather];+ };+ b.grid(fn(0), nu, nv, { flip: side > 0 });+ b.grid(fn(-0.03), nu, nv, { flip: side < 0 });+}++// Builds one figure; returns { stone, gold }. Local frame: faces +X, feet at y=0, about 2.05 m high.+function makeFigure(spec, lod = 1) {+ const o = typeof spec === 'string' ? FIGURES[spec] : spec;+ const stone = new Batch(TILE.fine), gold = new Batch(TILE.gold);+ const rng = mkRng(o.seed * 991 + 7), ph = []; for (let i = 0; i < 8; i++) ph.push(rng() * TAU);+ const yN = 1.7, nv = lod ? 26 : 8, nu = lod ? 44 : 12;+ const sw = o.sway || 1, slim = o.slim || 1, broad = o.broad || 1;+ const keys = [[0, 0.205, 0.235, 0.25], [0.05, 0.185, 0.215, 0.228], [0.3, 0.15, 0.178, 0.186], [0.52, 0.13, 0.158, 0.166], [0.68, 0.118, 0.135, 0.158], [0.8, 0.122, 0.128, 0.17], [0.875, 0.114, 0.118, 0.172], [0.925, 0.094, 0.098, 0.15], [0.965, 0.066, 0.07, 0.098], [1, 0.048, 0.05, 0.05]];+ const center = (t) => [0.018 * Math.sin(t * PI) * 1.3, sw * (0.055 * Math.sin(t * PI * 0.9) - 0.028 * Math.sin(t * PI * 2))];+ stone.grid((u, v) => {+ const th = -PI + TAU * u, t = 1 - Math.pow(1 - v, 1.35);+ const [af, ab, bs] = keyf(keys, t);+ const front = Math.cos(th) > 0, a = front ? af : ab;+ const c = Math.cos(th), s = Math.sin(th);+ const R = 1 / Math.sqrt((c * c) / (a * a) + (s * s) / (bs * bs * slim * slim * broad * broad));+ // ---- drapery ----+ const lower = sstep(0.72, 0.02, t);+ const f1 = Math.abs(Math.sin(th * 4 + ph[0] + t * 0.8 * sw)), f2 = Math.abs(Math.sin(th * 7 + ph[1] - t * 1.2)), f3 = 0.5;+ let d = lower * (0.052 * (f1 - 0.55) + 0.02 * (f2 - 0.55));+ const backF = sstep(0.6, -0.6, c) * sstep(1.0, 0.75, t);+ d += backF * 0.016 * (Math.abs(Math.sin(th * 5 + ph[3])) - 0.5);+ // hem breaking on the ground+ d += sstep(0.09, 0, t) * (0.03 * Math.sin(th * 7 + ph[4]) + 0.012 * Math.sin(th * 15 + ph[5]));+ // swags: U-shaped folds hanging across the front of the body+ const arc = (th - 0.25 * sw) / 1.05, swagW = gauss(arc * 0.86) * sstep(0.24, 0.42, t) * sstep(0.82, 0.68, t);+ const swag = Math.abs(Math.sin((t + 0.34 * arc * arc) * 30 + ph[6]));+ d += swagW * 0.034 * (swag - 0.45);+ // mantle edge: a diagonal step where the cloak overlaps the tunic+ const edge = 0.5 + 0.26 * Math.sin(th * sw + 0.9) - 0.1 * Math.cos(th * 2);+ const mant = sstep(-0.012, 0.012, t - edge);+ d += mant * 0.014 * (t < 0.92 ? 1 : (1 - t) / 0.08);+ const lip = gauss((t - edge) / 0.012) * 0.006;+ d += lip;+ // belt+ if (!o.noBelt) { const beltBand = gauss((t - 0.655) / 0.012); d += beltBand * 0.006 * (1 - mant); }+ const r = R + d;+ const cc = center(t);+ // baked cavity: the valleys of the folds hold shadow+ const foldH = Math.pow(f1, 0.7) * 0.72 + f2 * 0.28;+ const cav = clamp((0.5 + 0.5 * lerp(1, foldH, lower)) * (1 - swagW * 0.4 * (1 - swag)) - gauss((t - edge) / 0.016) * 0.26 + backF * 0.0, 0.3, 1);+ return [cc[0] + r * c, t * yN, cc[1] + r * s, cav * (0.78 + 0.22 * sstep(0, 0.16, t))];+ }, nu, nv, { flip: true, wrapU: true, boxUV: true });++ const top = center(1);+ // Neck and head with a gentle incline+ const hx = top[0] + 0.02, hz = top[1] + sw * 0.018, hy = yN + 0.135;+ stone.tube([[top[0] - 0.005, yN - 0.06, top[1]], [hx - 0.006, yN + 0.06, hz]], (t) => lerp(0.052, 0.046, t), lod ? 10 : 5, { capEnd: false });+ const headM = M.of(M.T(hx, hy, hz), M.Rx(sw * 7), M.Rz(-6), M.Ry(sw * 9));+ stone.at(headM, () => {+ addHead(stone, o, lod);+ if (o.veil) addVeil(stone, lod);+ if (o.cap) stone.at(M.T(0, 0.11, 0), () => stone.lathe([[0.086, -0.005, 0.8], [0.09, 0.0], [0.075, 0.05], [0.03, 0.095], [0.0, 0.1]], lod ? 14 : 6));+ if (o.mitre) { // pointed mitre, with gilded bands+ stone.grid((u, v) => { const a = -PI + TAU * u, w = 0.094 * (1 - v * 0.62), h = lerp(0.03, 0.27, v) * (1 - 0.32 * Math.abs(Math.sin(a)) * v) ; return [(0.1 - 0.012 * v) * Math.cos(a) * (1 - v * 0.3), 0.075 + h, w * Math.sin(a) * 0.9]; }, lod ? 18 : 8, lod ? 6 : 3, { flip: true, wrapU: true });+ gold.at(M.T(0, 0.095, 0), () => gold.lathe([[0.1, -0.012], [0.104, 0], [0.1, 0.018]], lod ? 18 : 8));+ }+ if (o.crown) { // gilded crown with fleurons+ gold.at(M.T(0, 0.075, 0), () => {+ const n = lod ? 24 : 8, R0 = o.veil ? 0.122 : 0.098;+ gold.lathe([[R0, 0], [R0 + 0.012, 0.004], [R0 + 0.012, 0.032], [R0 + 0.022, 0.05], [R0 + 0.01, 0.052], [R0 - 0.006, 0.04], [R0 - 0.006, 0]], n);+ if (lod) for (let i = 0; i < 8; i++) { const a = i * PI / 4, cx = (R0 + 0.014) * Math.cos(a), cz = (R0 + 0.014) * Math.sin(a); gold.tube([[cx, 0.04, cz], [cx * 1.03, 0.085, cz * 1.03], [cx * 1.1, 0.13, cz * 1.1]], (t) => [lerp(0.024, 0.003, t), lerp(0.024, 0.003, t) * 0.5], 5, { capStart: false }); blob(gold, [cx * 1.11, 0.135, cz * 1.11], [0.011, 0.011, 0.011], 5, 3); }+ });+ }+ if (o.nimbus) { // gilded disc behind the head of Christ+ gold.at(M.of(M.T(-0.13, 0.02, 0), M.Ry(90)), () => gold.slab(ringPts(0, 0, 0.21, lod ? 28 : 12), [], -0.012, 0.012, { sb: 0.8 }));+ }+ });++ // ---- arms ----+ const Rshoulder = 0.145 * slim * broad;+ const arm = (side, pts, cuff = 0.074) => {+ const path = spline(pts, lod ? 3 : 1);+ stone.tube(path, (t) => { const r = lerp(0.048, cuff, sstep(0.1, 0.85, t)) * (t > 0.93 ? lerp(1, 0.62, (t - 0.93) / 0.07) : 1); return [r * lerp(1, 1.28, sstep(0.3, 0.9, t)), r]; }, lod ? 10 : 5, {+ capStart: false,+ capEnd: true,+ ring: (a, t) => 1 + 0.13 * Math.abs(Math.sin(a * 2.5 + t * 4 + side)) * sstep(0.1, 0.5, t),+ ringS: (a, t) => (0.7 + 0.3 * Math.abs(Math.sin(a * 2.5 + t * 4 + side))) * (t > 0.96 ? 0.6 : 1),+ });+ return path[path.length - 1];+ };+ const hand = (p, r = 0.8) => {+ blob(stone, [p[0] + 0.02, p[1], p[2]], [0.05 * r, 0.032 * r, 0.05 * r], lod ? 6 : 4, lod ? 4 : 2);+ if (lod) for (let i = -1; i <= 1; i++) stone.tube([[p[0] + 0.045 * r, p[1], p[2] + i * 0.016 * r], [p[0] + 0.1 * r, p[1] - 0.018 * r, p[2] + i * 0.02 * r]], (t) => lerp(0.013, 0.008, t) * r, 3);+ };+ const y = yN - 0.17;+ const sR = [top[0], y, top[1] + Rshoulder], sL = [top[0], y, top[1] - Rshoulder];+ switch (o.hold) {+ case 'book': case 'bookOpen': {+ const eR = [0.04, 1.19, Rshoulder + 0.07], eL = [0.03, 1.15, -Rshoulder - 0.08];+ hand(arm(1, [sR, eR, [0.2, 1.3, 0.11]]));+ hand(arm(-1, [sL, eL, [0.2, 1.24, -0.07]]));+ break;+ }+ case 'key': hand(arm(1, [sR, [0.03, 1.14, Rshoulder + 0.08], [0.21, 1.37, 0.12]])); hand(arm(-1, [sL, [0.0, 1.06, -Rshoulder - 0.05], [0.14, 0.86, -Rshoulder - 0.03]])); break;+ case 'sword': hand(arm(1, [sR, [0.04, 1.12, Rshoulder + 0.06], [0.22, 1.28, 0.16]])); hand(arm(-1, [sL, [0.03, 1.16, -Rshoulder - 0.07], [0.2, 1.3, -0.08]])); stone.at(M.of(M.T(0.2, 1.36, -0.1), M.Rz(-50)), () => stone.box(-0.012, 0.012, -0.11, 0.11, -0.08, 0.08, { s: 0.85 })); break;+ case 'crozier': case 'sceptre': hand(arm(1, [sR, [0.05, 1.18, Rshoulder + 0.09], [0.24, 1.26, 0.19]])); hand(arm(-1, [sL, [0.02, 1.13, -Rshoulder - 0.06], [0.17, 1.12, -0.05]])); break;+ case 'palm': hand(arm(1, [sR, [0.03, 1.1, Rshoulder + 0.06], [0.21, 1.14, 0.15]])); hand(arm(-1, [sL, [0.03, 1.14, -Rshoulder - 0.06], [0.2, 1.22, -0.06]])); break;+ case 'scroll': hand(arm(1, [sR, [0.04, 1.2, Rshoulder + 0.05], [0.21, 1.4, 0.0]])); hand(arm(-1, [sL, [0.04, 1.02, -Rshoulder - 0.06], [0.22, 0.88, -0.06]])); break;+ case 'orb': hand(arm(1, [sR, [0.05, 1.12, Rshoulder + 0.08], [0.23, 1.22, 0.14]])); hand(arm(-1, [sL, [0.02, 1.13, -Rshoulder - 0.06], [0.17, 1.12, -0.05]])); break;+ case 'child': hand(arm(-1, [sL, [0.06, 1.14, -Rshoulder - 0.04], [0.21, 1.16, -0.09]])); hand(arm(1, [sR, [0.05, 1.12, Rshoulder + 0.03], [0.22, 1.3, 0.04]])); break;+ case 'bless': hand(arm(1, [sR, [0.08, 1.22, Rshoulder + 0.09], [0.21, 1.56, 0.17]])); hand(arm(-1, [sL, [0.04, 1.13, -Rshoulder - 0.07], [0.22, 1.2, -0.06]])); stone.at(M.of(M.T(0.27, 1.24, -0.06), M.Rz(-58)), () => stone.box(-0.015, 0.015, -0.12, 0.12, -0.085, 0.085, { s: 0.85 })); break;+ case 'censer': hand(arm(1, [sR, [0.04, 1.1, Rshoulder + 0.05], [0.26, 1.22, 0.13]])); hand(arm(-1, [sL, [0.03, 1.08, -Rshoulder - 0.05], [0.22, 1.2, -0.12]])); break;+ default: hand(arm(1, [sR, [0.02, 1.08, Rshoulder + 0.05], [0.12, 0.86, Rshoulder - 0.04]])); hand(arm(-1, [sL, [0.02, 1.08, -Rshoulder - 0.05], [0.12, 0.86, -Rshoulder + 0.04]])); break;+ }+ addAttribute(stone, gold, o.hold, lod, yN);+ if (o.wings) { addWing(stone, 1, lod); addWing(stone, -1, lod); }+ // Feet emerge beneath the hem+ if (lod) { for (const s of [-1, 1]) stone.at(M.T(0.2, 0.04, s * 0.085), () => stone.at(M.Rz(-6), () => blob(stone, [0, 0, 0], [0.105, 0.05, 0.05], 8, 5, 0.86))); }+ return { stone, gold };+}++// Cache of shared figure geometry; figures placed on a parent frame+const FIG_CACHE = {};+function figure(name, lod = 1) { const k = name + '|' + lod; if (!FIG_CACHE[k]) FIG_CACHE[k] = makeFigure(name, lod); return FIG_CACHE[k]; }+function placeFigure(name, kind, o = {}) {+ const f = figure(kind, o.lod === undefined ? 1 : o.lod);+ const s = o.scale || 1, sc = [s, s, s];+ const stone = place(name, f.stone, o.indoor ? MAT.fineDim : MAT.fine, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ place(name + '_Gilt', f.gold, MAT.gold, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ return stone;+}++// ---- Foliage and crockets ----------------------------------------------------------+// One carved leaf curling at the tip; local +Y up the surface, +X outward from the host+function addLeaf(b, m, size = 1, o = {}) {+ b.at(m, () => {+ const nu = o.coarse ? 2 : 4, nv = o.coarse ? 3 : 4;+ b.grid((u, v) => {+ const s = (u - 0.5) * 2, W = 0.5 * Math.sin(PI * Math.pow(v, 0.72)) * (1 - 0.35 * v);+ const curl = Math.pow(v, 2.2);+ const lobes = 1 + 0.18 * Math.sin(v * 16) * Math.abs(s);+ const rib = 0.05 * (1 - Math.abs(s));+ return [size * (rib + 0.3 * curl - 0.42 * curl * curl * curl + 0.12 * s * s), size * (v - 0.3 * curl * v), size * (s * W * lobes), 0.72 + 0.28 * (1 - Math.abs(s)) + 0.1 * curl];+ }, nu, nv, { flip: true });+ });+}+// A crocket: a bud of curled foliage that sits on an arris. Local +X away from the arris, +Y up the slope.+function addCrocket(b, m, size = 1, lod = 1) {+ b.at(m, () => {+ const path = [];+ const N = lod ? 5 : 3;+ for (let i = 0; i <= N; i++) { const t = i / N, a = -0.9 + t * 3.8, r = lerp(0.3, 0.12, t) * size; path.push([size * 0.16 + r * Math.cos(a) * 0.95, size * 0.1 + r * Math.sin(a) * 0.9, 0]); }+ b.tube(path, (t) => [lerp(0.14, 0.05, t) * size, lerp(0.17, 0.07, t) * size], lod ? 5 : 3, { capStart: false, capEnd: false, ringS: (a) => 0.8 + 0.2 * Math.cos(a) });+ if (lod) for (const s of [-1, 1]) b.tube([[0.02 * size, 0.02 * size, 0], [0.2 * size, 0.1 * size, s * 0.22 * size], [0.34 * size, 0.26 * size, s * 0.3 * size]], (t) => [lerp(0.07, 0.015, t) * size, lerp(0.05, 0.02, t) * size], 3, { capEnd: false });+ });+}+function addFinial(b, h = 1, lod = 1) { // cross-shaped foliate finial, base at y=0+ b.lathe([[0.12 * h, 0], [0.1 * h, 0.05 * h], [0.14 * h, 0.09 * h], [0.08 * h, 0.14 * h], [0.05 * h, 0.5 * h], [0.09 * h, 0.56 * h], [0.05 * h, 0.62 * h], [0.02 * h, 0.98 * h], [0, h]], lod ? 8 : 5);+ for (let i = 0; i < 4; i++) {+ const a = i * 90 + 45;+ addLeaf(b, M.of(M.T(0, 0.14 * h, 0), M.Ry(a), M.T(0.05 * h, 0, 0)), 0.4 * h, { coarse: 1 });+ if (lod) addLeaf(b, M.of(M.T(0, 0.58 * h, 0), M.Ry(a + 45), M.T(0.03 * h, 0, 0)), 0.3 * h, { coarse: 1 });+ }+}+// Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame+function crocketsAlong(b, p, q, n, size, out, lod = 1) {+ const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);+ const dn = d.map((c) => c / L);+ let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];+ const ol = Math.hypot(ox, oy, oz) || 1; ox /= ol; oy /= ol; oz /= ol;+ const zx = ox * 0, side = [dn[1] * oz - dn[2] * oy, dn[2] * ox - dn[0] * oz, dn[0] * oy - dn[1] * ox];+ // columns: X = out, Y = along edge, Z = side+ for (let i = 0; i < n; i++) {+ const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];+ const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];+ addCrocket(b, m, size * lerp(1, 0.78, t), lod);+ }+}++// Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.+function addPinnacle(b, w, h, o = {}) {+ const hw = w / 2, shaftH = h * 0.42, gableH = w * 0.95, lod = o.lod === undefined ? 1 : o.lod;+ if (o.simple) { // far-away pinnacle: faceted shaft, spirelet, a few crockets, finial+ b.lathe([[hw * 1.414, 0], [hw * 1.414, shaftH], [hw * 1.6, shaftH + w * 0.06], [hw * 1.3, shaftH + w * 0.12], [hw * 0.1, h * 0.86]], 4, { faceted: true, phase: PI / 4 });+ for (let i = 0; i < 4; i++) { const a = rad(i * 90 + 45), c = Math.cos(a), s = Math.sin(a), bR = hw * 1.3 * 1.0; crocketsAlong(b, [c * bR, shaftH + w * 0.14, s * bR], [c * hw * 0.14, h * 0.84, s * hw * 0.14], 2, w * 0.46, [c, 0.15, s], 0); }+ b.at(M.T(0, h * 0.84, 0), () => addFinial(b, h * 0.16, 0));+ return;+ }+ // Shaft with moulded base and blind lancet panels+ b.lathe([[hw * 1.44, 0], [hw * 1.44, w * 0.06], [hw * 1.3, w * 0.12], [hw * 1.3, w * 0.2], [hw * 1.26, w * 0.24]], 4, { faceted: true, phase: PI / 4 });+ b.lathe([[hw * 1.414, w * 0.2], [hw * 1.414, shaftH]], 4, { faceted: true, phase: PI / 4, s: 0.95 });+ for (let i = 0; i < 4; i++) b.at(M.Ry(i * 90), () => {+ const pw = w * 0.62, y0 = w * 0.35, ys = shaftH - w * 0.5;+ b.at(M.T(hw + 0.004, 0, 0), () => b.at(M.Ry(90), () => { // panel recessed look: shaded inset lancet with moulded frame+ b.sweep(ofsXY(pointedArc(pw, pw * 0.85, 3), 0, ys), hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[-pw / 2, y0], [-pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[pw / 2, y0], [pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.face([[-pw / 2, y0, -0.002, 0.5], [pw / 2, y0, -0.002, 0.5], [pw / 2, ys, -0.002, 0.5], [0, ys + pw * 0.8, -0.002, 0.45], [-pw / 2, ys, -0.002, 0.5]]);+ }));+ // Gablet on each face with crockets+ b.at(M.T(hw * 0.96, shaftH, 0), () => {+ b.slabX = true;+ b.at(M.Ry(90), () => b.slab([[-hw * 1.04, 0], [hw * 1.04, 0], [0, gableH]], [], 0, w * 0.08, { sf: 1, sb: 0.8 }));+ crocketsAlong(b, [0.02, 0.02, -hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, -1], 0);+ crocketsAlong(b, [0.02, 0.02, hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, 1], 0);+ b.at(M.T(0.04, gableH * 0.94, 0), () => addFinial(b, w * 0.34, 0));+ });+ });+ // Cornice ring and spirelet+ b.at(M.T(0, shaftH, 0), () => {+ b.lathe([[hw * 1.414, 0], [hw * 1.6, w * 0.06], [hw * 1.6, w * 0.1], [hw * 1.414, w * 0.14]], 4, { faceted: true, phase: PI / 4 });+ const sh = h - shaftH - w * 0.1;+ b.at(M.T(0, w * 0.12, 0), () => {+ b.lathe([[hw * 1.3, 0], [hw * 0.09, sh * 0.82]], 8, { faceted: true, phase: PI / 8 });+ const n = lod ? 5 : 3;+ for (let i = 0; i < 4; i++) {+ const a = rad(i * 90 + 22.5 + 22.5 * 0);+ const c = Math.cos(a + rad(22.5)), s = Math.sin(a + rad(22.5));+ const bR = hw * 1.3 * Math.cos(rad(0)), tR = hw * 0.09;+ crocketsAlong(b, [c * bR, sh * 0.04, s * bR], [c * tR, sh * 0.8, s * tR], n, w * 0.36, [c, 0.12, s], lod);+ }+ b.at(M.T(0, sh * 0.8, 0), () => (o.finial || b) === b ? addFinial(b, sh * 0.22 + w * 0.1, lod) : null);+ });+ });+}++// ---- Gargoyle ------------------------------------------------------------------------+// A crouching beast thrusting out from the wall along +X, root at the origin. About 2.3 m long.+function makeGargoyle(kind = 0) {+ const b = new Batch(TILE.fine), rng = mkRng(kind * 17 + 5);+ const ridge = (kind === 1 ? 0.3 : 0.22);+ const headY = kind === 2 ? 0.1 : 0.34;+ const spine = spline([[-0.3, -0.05, 0], [0.22, 0.2, 0], [0.75, 0.3, 0], [1.2, 0.2, 0], [1.56, headY, 0]], 5);+ b.tube(spine, (t) => { const r = keyf([[0, 0.34, 0.3], [0.25, 0.36, 0.34], [0.55, 0.22, 0.24], [0.82, 0.16, 0.17], [1, 0.19, 0.2]], t); return [r[0], r[1]]; }, 16, {+ capStart: true,+ up: [0, 1, 0],+ ring: (a, t) => 1 + ridge * Math.pow(Math.max(Math.cos(a), 0), 10) * Math.abs(Math.sin(t * 26)) + 0.05 * Math.abs(Math.sin(a * 5 + t * 11)),+ ringS: (a, t) => 0.74 + 0.26 * Math.abs(Math.sin(a * 5 + t * 11)),+ });+ // Skull, snout, gaping jaws+ const hx = 1.56, hy = headY;+ blob(b, [hx + 0.05, hy + 0.03, 0], [0.22, 0.19, 0.2], 14, 10, 0.92);+ b.tube(spline([[hx + 0.1, hy + 0.06, 0], [hx + 0.34, hy + 0.05, 0], [hx + 0.56, hy + 0.1, 0]], 3), (t) => [lerp(0.14, 0.07, t), lerp(0.15, 0.085, t)], 10, { capStart: true, up: [0, 1, 0], ring: (a, t) => 1 + 0.2 * Math.pow(Math.max(Math.cos(a), 0), 6) * Math.abs(Math.sin(t * 9)) });+ b.shade(0.9, () => b.tube(spline([[hx + 0.05, hy - 0.1, 0], [hx + 0.28, hy - 0.2, 0], [hx + 0.5, hy - 0.2, 0]], 3), (t) => [lerp(0.09, 0.045, t), lerp(0.125, 0.07, t)], 10, { capStart: true, up: [0, 1, 0] }));+ b.shade(0.34, () => blob(b, [hx + 0.2, hy - 0.06, 0], [0.16, 0.085, 0.095], 8, 5));+ for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {+ const f = i / 3.5;+ b.tube([[hx + 0.26 + f * 0.27, hy + 0.0 + f * 0.075, s * lerp(0.1, 0.06, f)], [hx + 0.27 + f * 0.27, hy - 0.08 + f * 0.075, s * lerp(0.1, 0.06, f)]], (t) => lerp(0.018, 0.002, t), 4, { capStart: true });+ b.tube([[hx + 0.22 + f * 0.27, hy - 0.15, s * lerp(0.1, 0.05, f)], [hx + 0.22 + f * 0.27, hy - 0.08, s * lerp(0.1, 0.05, f)]], (t) => lerp(0.016, 0.002, t), 4, { capStart: true });+ }+ // Brow, eyes, ears or horns+ for (const s of [-1, 1]) {+ b.tube(spline([[hx + 0.02, hy + 0.2, s * 0.03], [hx + 0.17, hy + 0.17, s * 0.1], [hx + 0.24, hy + 0.12, s * 0.15]], 2), (t) => lerp(0.03, 0.02, t), 5, { capStart: true });+ b.shade(0.38, () => blob(b, [hx + 0.21, hy + 0.09, s * 0.11], [0.05, 0.04, 0.045], 6, 4));+ blob(b, [hx + 0.235, hy + 0.09, s * 0.115], [0.024, 0.022, 0.022], 6, 4);+ if (kind === 1) b.tube(spline([[hx - 0.05, hy + 0.14, s * 0.11], [hx - 0.1, hy + 0.3, s * 0.18], [hx - 0.25, hy + 0.42, s * 0.2], [hx - 0.42, hy + 0.42, s * 0.15]], 3), (t) => lerp(0.05, 0.005, t), 6, { capStart: true });+ else b.grid((u, v) => { const L = 0.3, W = 0.09 * Math.sin(PI * u); return [hx - 0.08 - u * L * 0.5, hy + 0.13 + u * L * 0.7, s * (0.13 + u * 0.09 + (v - 0.5) * W * 2), 0.7 + 0.3 * Math.abs(v - 0.5) * 2]; }, 4, 2, { flip: s > 0 });+ if (kind === 1) { /* also add jowls */ blob(b, [hx + 0.1, hy - 0.04, s * 0.13], [0.09, 0.06, 0.05], 6, 4, 0.9); }+ // Fore leg gripping the ledge+ const sh = [0.5, 0.12, s * 0.3], el = [0.68, -0.28, s * 0.42], wr = [0.98, -0.42, s * 0.3];+ b.tube(spline([sh, el, wr], 4), (t) => keyf([[0, 0.16], [0.4, 0.105], [1, 0.07]], t)[0], 10, { capStart: true, ring: (a, t) => 1 + 0.08 * Math.abs(Math.sin(a * 3 + t * 8)) });+ blob(b, wr, [0.1, 0.06, 0.09], 8, 5);+ for (let i = -1; i <= 1; i++) b.tube(spline([[wr[0] + 0.03, wr[1], wr[2] + i * 0.05], [wr[0] + 0.14, wr[1] - 0.02, wr[2] + i * 0.08], [wr[0] + 0.21, wr[1] - 0.11, wr[2] + i * 0.09]], 2), (t) => lerp(0.032, 0.004, t), 5);+ // Folded wing+ const span = kind === 0 ? 0.95 : 0.7;+ const wingFn = (off) => (u, v) => {+ const L = u * span, W = 0.44 * Math.sin(PI * Math.pow(u, 0.7));+ const memb = Math.abs(Math.sin(v * 7.5 + u * 1.2)) * 0.05 * u;+ return [0.42 - L + (off), 0.3 + 0.48 * Math.sin(u * 1.25) * (1 - v * 0.9) - v * W * 0.75 + memb, s * (0.27 + 0.2 * u + v * W * 0.5), 0.68 + 0.32 * (memb / 0.05)];+ };+ b.grid(wingFn(0), 7, 5, { flip: s < 0 });+ b.grid(wingFn(-0.02), 7, 5, { flip: s > 0 });+ }+ return b;+}+const GARGOYLES = [];+function gargoyle(kind) { if (!GARGOYLES[kind]) GARGOYLES[kind] = makeGargoyle(kind); return GARGOYLES[kind]; }++// ==================================================================================+// STAINED GLASS: a tiny vector-art engine producing real leaded panes.+// Every shape is a pane of pot-metal colour with a came (lead strip) around it; large+// figures get painted grisaille lines. Windows are geometry, so they stay crisp at any distance.+// ==================================================================================+function offsetPoly(pts, d) {+ const n = pts.length, s = area2(pts) >= 0 ? 1 : -1, out = [];+ for (let i = 0; i < n; i++) {+ const a = pts[(i - 1 + n) % n], b = pts[i], c = pts[(i + 1) % n];+ let ax = b[0] - a[0], ay = b[1] - a[1], cx = c[0] - b[0], cy = c[1] - b[1];+ const la = Math.hypot(ax, ay) || 1, lc = Math.hypot(cx, cy) || 1; ax /= la; ay /= la; cx /= lc; cy /= lc;+ let nx = (ay + cy) * s, ny = -(ax + cx) * s; const l = Math.hypot(nx, ny) || 1; nx /= l; ny /= l;+ const k = clamp(1 / Math.max(0.35, Math.abs(nx * ay * s - ny * ax * s)), 1, 2.4);+ out.push([b[0] + nx * d * k, b[1] + ny * d * k]);+ }+ return out;+}+function clipConvex(poly, clip) {+ let out = poly; const sgn = area2(clip) >= 0 ? 1 : -1;+ for (let i = 0; i < clip.length && out.length; i++) {+ const a = clip[i], b = clip[(i + 1) % clip.length], inp = out; out = [];+ const side = (p) => sgn * ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]));+ for (let j = 0; j < inp.length; j++) {+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);+ if (sp >= 0) out.push(p);+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([lerp(p[0], q[0], t), lerp(p[1], q[1], t)]); }+ }+ }+ return out;+}+function isConvex(p) { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = p[(i + 2) % p.length]; const s = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); if (Math.abs(s) < 1e-9) continue; if (!sign) sign = Math.sign(s); else if (Math.sign(s) !== sign) return false; } return true; }++class Glass {+ constructor(seed = 1) { this.b = new Batch(1); this.b.noColor = true; this.r = mkRng(seed * 13 + 3); this.clip = null; this.xf = [1, 0, 0, 1, 0, 0]; this.came = 0.022; }+ xform(a, b, c, d, e, f) { const m = this.xf; return [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]]; }+ withXf(rotDeg, tx, ty, s, fn) { const o = this.xf, c = Math.cos(rad(rotDeg)) * s, sn = Math.sin(rad(rotDeg)) * s; this.xf = this.xform(c, sn, -sn, c, tx, ty); fn(this); this.xf = o; }+ withClip(pts, fn) { const o = this.clip; const m = this.xf; this.clip = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]); fn(this); this.clip = o; }+ raw(pts, color, layer, jitter = 1) {+ const idx = PAL[color], n = PALETTE_COLS, cell = 1 / n;+ const cu = ((idx % n) + 0.5) * cell, cv = (Math.floor(idx / n) + 0.5) * cell;+ const ou = (this.r() - 0.5) * cell * 0.55 * jitter, ov = (this.r() - 0.5) * cell * 0.55 * jitter, sc = cell * 0.09;+ const z = 0.002 + layer * 0.0035;+ let mx = 0, my = 0; for (const p of pts) { mx += p[0]; my += p[1]; } mx /= pts.length; my /= pts.length;+ // UVs stay inside the swatch: a small gradient across the pane, relative to its own centre+ const clampU = (d) => clamp(d * sc * 0.4, -cell * 0.14, cell * 0.14);+ const ids = pts.map((p) => this.b.v(p[0], p[1], z, 0, 0, 1, cu + ou + clampU(p[0] - mx), cv + ov + clampU(p[1] - my), 1));+ if (isConvex(pts)) { const flip = area2(pts) < 0; for (let i = 1; i < ids.length - 1; i++) { if (flip) this.b.tri(ids[0], ids[i + 1], ids[i]); else this.b.tri(ids[0], ids[i], ids[i + 1]); } } else { const t = triangulate(pts); for (const k of t) this.b.tri(ids[k[0]], ids[k[1]], ids[k[2]]); }+ }+ // a pane: colour polygon + lead came under it+ poly(pts, color, layer = 1, o = {}) {+ const m = this.xf; let P2 = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]);+ if (area2(P2) < 0) P2 = P2.slice().reverse();+ const came = o.came === undefined ? this.came : o.came;+ if (came > 0) {+ let C = offsetPoly(P2, came);+ if (this.clip) C = clipConvex(C, this.clip);+ if (C.length >= 3 && Math.abs(area2(C)) > 1e-5) this.raw(C, 'lead', layer - 0.5, 0);+ } else if (came < 0) P2 = offsetPoly(P2, came); // over a lead backing: inset instead of a second polygon+ if (this.clip) P2 = clipConvex(P2, this.clip);+ if (P2.length >= 3 && Math.abs(area2(P2)) > 1e-5) this.raw(P2, color, layer, o.jitter === undefined ? 1 : o.jitter);+ return this;+ }+ disc(cx, cy, r, color, layer = 1, n = 12, o) { return this.poly(ringPts(cx, cy, r, n), color, layer, o); }+ // painted line (grisaille): a thin dark ribbon along a polyline+ line(pts, w = 0.02, color = 'paint', layer = 5) {+ for (let i = 0; i < pts.length - 1; i++) {+ const a = pts[i], b = pts[i + 1]; let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx *= w / 2 / l; dy *= w / 2 / l;+ this.poly([[a[0] - dy, a[1] + dx], [a[0] + dy, a[1] - dx], [b[0] + dy, b[1] - dx], [b[0] - dy, b[1] + dx]], color, layer, { came: 0, jitter: 0 });+ }+ return this;+ }+}++// Diapered mosaic background: lozenges alternating two colours with a jewel at each crossing+function gDiaper(g, x0, y0, x1, y1, cell, c1, c2, c3, layer = 0) {+ const h = cell / 2;+ let row = 0;+ for (let y = y0 - cell; y < y1 + cell; y += h) {+ const ofs = (row % 2) * h;+ for (let x = x0 - cell + ofs; x < x1 + cell; x += cell) {+ const col = (row % 4 < 2) ? c1 : c2;+ g.poly([[x, y - h], [x + h, y], [x, y + h], [x - h, y]], col, layer, { came: -0.016 });+ if (c3 && row % 4 === 0) g.poly([[x + h, y - h * 0.26], [x + h * 1.26, y], [x + h, y + h * 0.26], [x + h * 0.74, y]], c3, layer + 0.6, { came: 0.01 });+ }+ row++;+ }+}++// A figure in glass. (x,y) = feet, h = height. Pose: 'stand' | 'sit' | 'kneel'.+function gFigure(g, x, y, h, o = {}) {+ // sx < 1 elongates the body in the Gothic manner: width scales down, the head keeps its proportion+ const sx = o.sx || 1, hTot = h; h = hTot * sx;+ const f = o.flip ? -1 : 1, L = (u, v) => [x + f * u * h, y + (v <= 0.76 ? v / 0.76 * (hTot - 0.24 * h) : hTot - (1 - v) * h)], PS = (a) => a.map((p) => L(p[0], p[1]));+ const robe = o.robe || 'white', mantle = o.mantle || 'cobalt', skin = o.skin || 'flesh', hair = o.hair || 'hair', big = hTot > 2.2, layer = o.layer === undefined ? 2 : o.layer;+ const sit = o.pose === 'sit', kneel = o.pose === 'kneel';+ const hy = kneel ? 0.67 : sit ? 0.86 : 0.875, hr = 0.066, hx = kneel ? 0.04 : 0;+ if (o.wings) {+ g.poly(PS([[-0.08, hy - 0.1], [-0.34, hy + 0.08], [-0.42, hy - 0.12], [-0.34, 0.34], [-0.1, 0.46]]), o.wings, layer - 0.2);+ g.poly(PS([[0.08, hy - 0.1], [0.3, hy + 0.1], [0.38, hy - 0.1], [0.3, 0.36], [0.1, 0.48]]), o.wings, layer - 0.2);+ if (h > 1) { g.line(PS([[-0.1, hy - 0.08], [-0.34, 0.5]]), h * 0.012); g.line(PS([[0.1, hy - 0.08], [0.31, 0.5]]), h * 0.012); }+ }+ if (o.halo !== false) {+ g.disc(L(hx, hy)[0], L(hx, hy)[1], h * 0.112, o.halo || 'gold', layer - 0.1, big ? 18 : 12);+ if (o.cross) { for (let i = 0; i < 3; i++) { const a = PI / 2 + i * PI / 2 - PI / 2 * 1; g.poly(PS([[hx + 0.112 * Math.cos(a - 0.26), hy + 0.112 * Math.sin(a - 0.26)], [hx + 0.112 * Math.cos(a + 0.26), hy + 0.112 * Math.sin(a + 0.26)], [hx, hy]]), 'ruby', layer, { came: 0.008 }); } }+ }+ // ---- body ----+ if (sit) {+ g.poly(PS([[-0.12, 0.72], [0.12, 0.72], [0.19, 0.44], [0.26, 0.4], [0.27, 0.08], [0.22, 0.0], [-0.22, 0.0], [-0.2, 0.3], [-0.14, 0.44]]), robe, layer);+ g.poly(PS([[-0.13, 0.74], [0.05, 0.74], [0.17, 0.46], [0.24, 0.4], [0.2, 0.18], [-0.06, 0.1], [-0.2, 0.3], [-0.14, 0.46]]), mantle, layer + 0.5);+ if (h > 0.9) g.line(PS([[-0.08, 0.66], [0.14, 0.4]]), h * 0.011), g.line(PS([[-0.14, 0.42], [0.2, 0.26]]), h * 0.011), g.line(PS([[-0.12, 0.3], [0.16, 0.16]]), h * 0.011);+ } else if (kneel) {+ g.poly(PS([[-0.08, 0.56], [0.13, 0.56], [0.16, 0.26], [0.26, 0.1], [0.26, 0], [-0.26, 0], [-0.2, 0.12]]), robe, layer);+ g.poly(PS([[-0.09, 0.57], [0.08, 0.57], [0.13, 0.28], [0.1, 0.1], [-0.2, 0.05], [-0.16, 0.24]]), mantle, layer + 0.5);+ } else {+ const c = o.sway === undefined ? 0.02 : o.sway;+ g.poly(PS([[-0.11 + c, 0.75], [0.11 + c, 0.75], [0.13, 0.46], [0.16, 0.2], [0.22, 0.03], [0.17, 0.0], [0.05, 0.02], [-0.05, 0.0], [-0.18, 0.01], [-0.22, 0.04], [-0.16, 0.22], [-0.13, 0.46]]), robe, layer);+ g.poly(PS([[-0.12 + c, 0.76], [0.03 + c, 0.76], [0.13, 0.5], [0.15, 0.28], [0.1, 0.14], [-0.06, 0.1], [-0.18, 0.2], [-0.14, 0.46]]), mantle, layer + 0.5);+ if (h > 0.9) { g.line(PS([[-0.07, 0.7], [0.1, 0.42]]), h * 0.009); g.line(PS([[-0.1, 0.5], [0.13, 0.28]]), h * 0.009); g.line(PS([[-0.13, 0.34], [0.05, 0.14]]), h * 0.009); g.line(PS([[0.17, 0.2], [0.15, 0.02]]), h * 0.008); g.line(PS([[-0.13, 0.2], [-0.15, 0.03]]), h * 0.008); }+ if (big) { g.line(PS([[0.06, 0.72], [0.12, 0.5]]), h * 0.008); g.line(PS([[-0.05, 0.45], [-0.09, 0.14]]), h * 0.008); g.line(PS([[0.04, 0.42], [0.05, 0.12]]), h * 0.008); g.line(PS([[-0.16, 0.1], [0.16, 0.08]]), h * 0.006, 'lead'); g.line(PS([[-0.13, 0.4], [0.14, 0.4]]), h * 0.006, 'lead'); g.line(PS([[-0.11, 0.6], [0.12, 0.6]]), h * 0.006, 'lead'); }+ g.poly(PS([[-0.1, 0.03], [-0.02, 0.03], [-0.02, -0.01], [-0.12, -0.01]]), o.shoe || 'brown', layer + 0.2, { came: 0.008 });+ g.poly(PS([[0.02, 0.03], [0.1, 0.03], [0.12, -0.01], [0.02, -0.01]]), o.shoe || 'brown', layer + 0.2, { came: 0.008 });+ }+ // ---- arms and hands ----+ const by = kneel ? 0.5 : 0.68, armPose = o.arm || 'hold';+ const ribbon = (a, b, w, col) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy) || 1, nx = -dy / l * w / 2, ny = dx / l * w / 2; g.poly(PS([[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]]), col, layer + 1, { came: 0.012 }); };+ const hand = (p) => g.disc(L(p[0], p[1])[0], L(p[0], p[1])[1], h * 0.024, skin, layer + 1.3, 6, { came: 0.008 });+ const arms = { hold: [[0.13, by - 0.12], [0.03, by - 0.18], [-0.13, by - 0.12], [-0.03, by - 0.2]], up: [[0.17, by - 0.03], [0.27, by + 0.18], [-0.12, by - 0.12], [-0.03, by - 0.22]], out: [[0.19, by - 0.08], [0.32, by - 0.06], [-0.19, by - 0.08], [-0.32, by - 0.06]], pray: [[0.1, by - 0.14], [0.12, by - 0.03], [-0.04, by - 0.14], [0.1, by - 0.03]], point: [[0.17, by - 0.08], [0.33, by + 0.04], [-0.12, by - 0.12], [-0.04, by - 0.2]], orant: [[0.2, by + 0.0], [0.27, by + 0.14], [-0.2, by + 0.0], [-0.27, by + 0.14]] };+ const A = arms[armPose] || arms.hold;+ ribbon([0.1, by], A[0], 0.075, mantle); ribbon(A[0], A[1], 0.06, robe); hand(A[1]);+ ribbon([-0.1, by], A[2], 0.075, mantle); ribbon(A[2], A[3], 0.06, robe); hand(A[3]);+ // ---- attributes ----+ const at = o.attr;+ if (at === 'book') { g.poly(PS([[A[1][0] - 0.1, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] + 0.12], [A[1][0] - 0.1, A[1][1] + 0.12]]), 'ruby', layer + 1.5, { came: 0.01 }); g.poly(PS([[A[1][0] - 0.07, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.09], [A[1][0] - 0.07, A[1][1] + 0.09]]), 'gold', layer + 1.6, { came: 0 }); }+ else if (at === 'staff' || at === 'cross') { const sx = A[1][0]; g.poly(PS([[sx - 0.012, 0], [sx + 0.012, 0], [sx + 0.012, 1.08], [sx - 0.012, 1.08]]), 'gold', layer + 0.9, { came: 0.008 }); if (at === 'cross') g.poly(PS([[sx - 0.07, 0.95], [sx + 0.07, 0.95], [sx + 0.07, 0.98], [sx - 0.07, 0.98]]), 'gold', layer + 0.9, { came: 0.008 }); else g.poly(ringPts(sx + 0.03, 1.1, 0.045, 8).map((p) => L(p[0], p[1])), 'gold', layer + 0.9, { came: 0.008 }); }+ else if (at === 'sword') { const sx = A[1][0]; g.poly(PS([[sx - 0.012, by - 0.08], [sx + 0.012, by - 0.08], [sx, 1.02]]), 'pale', layer + 0.9, { came: 0.008 }); g.poly(PS([[sx - 0.05, by - 0.08], [sx + 0.05, by - 0.08], [sx + 0.05, by - 0.05], [sx - 0.05, by - 0.05]]), 'gold', layer + 1, { came: 0.008 }); }+ else if (at === 'key') { const sx = A[1][0]; g.poly(PS([[sx - 0.01, by - 0.14], [sx + 0.01, by - 0.14], [sx + 0.01, by + 0.2], [sx - 0.01, by + 0.2]]), 'gold', layer + 1, { came: 0.006 }); g.poly(ringPts(sx, by + 0.23, 0.035, 6).map((p) => L(p[0], p[1])), 'gold', layer + 1, { came: 0.006 }); g.poly(PS([[sx, by - 0.14], [sx + 0.05, by - 0.14], [sx + 0.05, by - 0.1], [sx, by - 0.1]]), 'gold', layer + 1, { came: 0.006 }); }+ else if (at === 'lily') { const sx = A[1][0]; g.line(PS([[sx, by - 0.22], [sx + 0.01, by + 0.2]]), h * 0.012, 'green', layer + 1); for (let i = 0; i < 3; i++) { const yy = by + 0.08 + i * 0.07; g.disc(L(sx + 0.02 * (i % 2 ? 1 : -1), yy)[0], L(0, yy)[1], h * 0.024, 'white', layer + 1.1, 5, { came: 0.006 }); } }+ // ---- head ----+ const hc = L(hx, hy);+ g.poly(PS([[hx - 0.022, hy - 0.12], [hx + 0.022, hy - 0.12], [hx + 0.022, hy - 0.04], [hx - 0.022, hy - 0.04]]), skin, layer + 1.5, { came: 0.008 });+ if (o.veil) g.poly(PS([[hx - 0.092, hy + 0.03], [hx - 0.05, hy + 0.09], [hx + 0.05, hy + 0.09], [hx + 0.09, hy + 0.03], [hx + 0.1, hy - 0.08], [hx + 0.06, hy - 0.15], [hx - 0.06, hy - 0.15], [hx - 0.1, hy - 0.08]]), o.veil, layer + 1.55, { came: 0.01 });+ g.disc(hc[0], hc[1], h * hr, skin, layer + 1.8, big ? 12 : 8, { came: 0.01 });+ if (!o.veil) g.poly(PS([[hx - 0.07, hy + 0.012], [hx - 0.05, hy + 0.06], [hx, hy + 0.078], [hx + 0.05, hy + 0.06], [hx + 0.07, hy + 0.012], [hx + 0.045, hy + 0.03], [hx - 0.045, hy + 0.03]]), hair, layer + 2, { came: 0.008 });+ if (o.beard) g.poly(PS([[hx - 0.056, hy - 0.02], [hx - 0.03, hy - 0.04], [hx + 0.03, hy - 0.04], [hx + 0.056, hy - 0.02], [hx + 0.03, hy - (o.beard === 2 ? 0.17 : 0.11)], [hx - 0.03, hy - (o.beard === 2 ? 0.17 : 0.11)]]), o.beardCol || hair, layer + 2, { came: 0.008 });+ if (o.crown) { g.poly(PS([[hx - 0.075, hy + 0.04], [hx + 0.075, hy + 0.04], [hx + 0.09, hy + 0.13], [hx + 0.04, hy + 0.09], [hx, hy + 0.14], [hx - 0.04, hy + 0.09], [hx - 0.09, hy + 0.13]]), 'gold', layer + 2.2, { came: 0.01 }); }+ if (o.mitre) g.poly(PS([[hx - 0.06, hy + 0.04], [hx + 0.06, hy + 0.04], [hx + 0.05, hy + 0.15], [hx, hy + 0.21], [hx - 0.05, hy + 0.15]]), o.mitre, layer + 2.2, { came: 0.01 });+ if (h > 0.75) { // grisaille face+ const lw = h * (big ? 0.0065 : 0.009);+ g.line(PS([[hx - 0.042, hy + 0.012], [hx - 0.012, hy + 0.012]]), lw); g.line(PS([[hx + 0.012, hy + 0.012], [hx + 0.042, hy + 0.012]]), lw);+ g.line(PS([[hx - 0.003, hy + 0.012], [hx + 0.006, hy - 0.022]]), lw * 0.8); g.line(PS([[hx - 0.018, hy - 0.038], [hx + 0.018, hy - 0.038]]), lw * 0.8);+ if (big) { g.line(PS([[hx - 0.044, hy + 0.024], [hx - 0.012, hy + 0.024]]), lw * 0.8); g.line(PS([[hx + 0.012, hy + 0.024], [hx + 0.044, hy + 0.024]]), lw * 0.8); }+ }+}++// ---- Narrative scenes, composed inside a circle of radius r at the origin ---------+const SCENES = {+ annunciation(g, r) { // Angel greets the Virgin; lily between them, dove above+ gFigure(g, -r * 0.46, -r * 0.72, r * 1.34, { robe: 'white', mantle: 'emerald', wings: 'rubyLt', arm: 'point', attr: null, hair: 'amber' });+ gFigure(g, r * 0.46, -r * 0.72, r * 1.3, { flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray', halo: 'gold' });+ g.poly([[-0.1, -r * 0.72], [0.1, -r * 0.72], [0.06, -r * 0.5], [-0.06, -r * 0.5]], 'gold', 3, { came: 0.012 });+ g.line([[0, -r * 0.5], [0, r * 0.05]], 0.025, 'green', 3.2);+ [0.05, -0.1, -0.24].forEach((v, i) => g.disc((i % 2 ? 1 : -1) * r * 0.05, r * v, r * 0.05, 'white', 3.3, 5, { came: 0.008 }));+ g.disc(0, r * 0.62, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.14, r * 0.66], [0, r * 0.6], [r * 0.14, r * 0.66], [0, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ nativity(g, r) { // Virgin, Child in the crib, Joseph, the star, the ox and ass+ g.poly([[-r * 0.8, -r * 0.9], [r * 0.8, -r * 0.9], [r * 0.8, -r * 0.62], [-r * 0.8, -r * 0.62]], 'brown', 1.5);+ gFigure(g, -r * 0.42, -r * 0.62, r * 1.05, { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'out' });+ gFigure(g, r * 0.56, -r * 0.62, r * 1.1, { robe: 'tan', mantle: 'emerald', beard: 1, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'staff', flip: 1, halo: 'yellow' });+ g.poly([[-r * 0.1, -r * 0.62], [r * 0.3, -r * 0.62], [r * 0.34, -r * 0.34], [-r * 0.14, -r * 0.34]], 'amber', 2, { came: 0.014 });+ g.poly([[-r * 0.07, -r * 0.34], [r * 0.27, -r * 0.34], [r * 0.27, -r * 0.22], [-r * 0.07, -r * 0.22]], 'white', 2.5, { came: 0.012 });+ g.disc(r * 0.02, -r * 0.24, r * 0.065, 'gold', 2.5, 8, { came: 0.008 }); g.disc(r * 0.02, -r * 0.24, r * 0.043, 'flesh', 3, 7, { came: 0.008 });+ star(g, 0, r * 0.68, r * 0.13, 'yellow', 3);+ g.poly([[-r * 0.4, r * 0.22], [0, r * 0.52], [r * 0.4, r * 0.22], [r * 0.36, r * 0.14], [0, r * 0.4], [-r * 0.36, r * 0.14]], 'bone', 1.5);+ },+ magi(g, r) { // Three crowned kings bring gifts to the enthroned Virgin+ gFigure(g, r * 0.56, -r * 0.8, r * 1.2, { pose: 'sit', flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'hold' });+ g.disc(r * 0.4, -r * 0.1, r * 0.06, 'gold', 4, 8, { came: 0.008 });+ gFigure(g, -r * 0.05, -r * 0.8, r * 1.18, { pose: 'kneel', robe: 'emerald', mantle: 'ruby', crown: 1, beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'point', halo: false });+ gFigure(g, -r * 0.42, -r * 0.8, r * 1.32, { robe: 'violet', mantle: 'gold', crown: 1, beard: 1, arm: 'point', halo: false });+ gFigure(g, -r * 0.74, -r * 0.8, r * 1.3, { robe: 'teal', mantle: 'rubyLt', crown: 1, arm: 'hold', halo: false });+ star(g, -r * 0.1, r * 0.72, r * 0.11, 'yellow', 3);+ },+ crucifixion(g, r) { // Christ on the cross, the Virgin and Saint John, sun and moon+ g.poly([[-0.09 * r, -r * 0.92], [0.09 * r, -r * 0.92], [0.09 * r, r * 0.76], [-0.09 * r, r * 0.76]], 'emerald', 1.5, { came: 0.016 });+ g.poly([[-r * 0.58, r * 0.28], [r * 0.58, r * 0.28], [r * 0.58, r * 0.45], [-r * 0.58, r * 0.45]], 'emerald', 1.5, { came: 0.016 });+ gFigure(g, 0, -r * 0.84, r * 1.42, { robe: 'white', mantle: 'white', cross: 1, arm: 'out', halo: 'gold', hair: 'hair', beard: 1, sway: 0 });+ gFigure(g, -r * 0.62, -r * 0.88, r * 1.1, { robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray' });+ gFigure(g, r * 0.62, -r * 0.88, r * 1.1, { flip: 1, robe: 'white', mantle: 'rubyLt', arm: 'hold', attr: 'book' });+ g.disc(-r * 0.68, r * 0.66, r * 0.11, 'gold', 2, 8); g.poly([[r * 0.59, r * 0.74], [r * 0.76, r * 0.6], [r * 0.71, r * 0.76]], 'pearl', 2);+ },+ resurrection(g, r) { // Christ rises from the tomb holding the banner; soldiers sleep below+ g.poly([[-r * 0.72, -r * 0.84], [r * 0.72, -r * 0.84], [r * 0.72, -r * 0.42], [-r * 0.72, -r * 0.42]], 'bone', 2);+ g.line([[-r * 0.72, -r * 0.56], [r * 0.72, -r * 0.56]], 0.02, 'lead', 3); [-0.4, 0, 0.4].forEach((k) => g.disc(r * k, -r * 0.68, r * 0.09, 'purple', 3, 8, { came: 0.01 }));+ gFigure(g, 0, -r * 0.42, r * 1.25, { robe: 'white', mantle: 'rubyLt', cross: 1, arm: 'up', hair: 'hair', beard: 1, halo: 'gold' });+ g.poly([[r * 0.34, r * 0.72], [r * 0.34, r * 0.92], [r * 0.7, r * 0.84], [r * 0.34, r * 0.72]], 'white', 3, { came: 0.01 }); g.line([[r * 0.34, r * 0.92], [r * 0.34, -r * 0.1]], 0.02, 'gold', 3.2);+ for (const s of [-1, 1]) { g.disc(s * r * 0.82, -r * 0.5, r * 0.07, 'flesh', 3, 7, { came: 0.01 }); g.poly([[s * r * 0.6, -r * 0.86], [s * r * 0.92, -r * 0.86], [s * r * 0.9, -r * 0.54], [s * r * 0.7, -r * 0.54]], s > 0 ? 'emerald' : 'cobalt', 2.6); }+ },+ supper(g, r) { // The Last Supper: Christ at the centre of a laid table+ [-0.62, -0.31, 0.31, 0.62].forEach((k, i) => gBust(g, r * k, -r * 0.05, r * 0.92, { robe: ['emerald', 'ruby', 'amber', 'violet'][i], beard: i % 2 ? 1 : 0, hair: i === 3 ? 'grey' : 'hair' }));+ gBust(g, 0, -r * 0.02, r * 1.04, { robe: 'rubyLt', beard: 1, cross: 1 });+ g.poly([[-r * 0.92, -r * 0.5], [r * 0.92, -r * 0.5], [r * 0.92, -r * 0.2], [-r * 0.92, -r * 0.2]], 'white', 4);+ g.poly([[-r * 0.86, -r * 0.5], [r * 0.86, -r * 0.5], [r * 0.82, -r * 0.92], [-r * 0.82, -r * 0.92]], 'brown', 3.6);+ [-0.58, -0.2, 0.2, 0.58].forEach((k) => g.disc(r * k, -r * 0.3, r * 0.055, 'gold', 4.5, 6, { came: 0.008 }));+ },+ noah(g, r) { // The Ark on the waters, the dove returning+ [-0.82, -0.56].forEach((v, i) => g.poly([[-r, r * v], [r, r * v], [r, r * (v + 0.22)], [-r, r * (v + 0.18)]], i ? 'azure' : 'cobalt', 2));+ g.poly([[-r * 0.7, -r * 0.4], [r * 0.7, -r * 0.4], [r * 0.5, -r * 0.7], [-r * 0.5, -r * 0.7]], 'brown', 2.5);+ g.poly([[-r * 0.46, -r * 0.4], [r * 0.46, -r * 0.4], [r * 0.46, r * 0.02], [-r * 0.46, r * 0.02]], 'tan', 3);+ g.poly([[-r * 0.56, r * 0.0], [0, r * 0.32], [r * 0.56, r * 0.0]], 'ruby', 3.2);+ [-0.2, 0.2].forEach((k) => g.disc(r * k, -r * 0.18, r * 0.1, 'night', 3.4, 7, { came: 0.012 }));+ gBust(g, r * 0.2, -r * 0.3, r * 0.42, { robe: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', layer: 3.6 });+ g.disc(-r * 0.54, r * 0.64, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.72, r * 0.72], [-r * 0.54, r * 0.62], [-r * 0.36, r * 0.74], [-r * 0.54, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ shepherd(g, r) { // The Good Shepherd carries the lamb; a flock in green pasture+ g.poly([[-r, -r], [r, -r], [r, -r * 0.5], [-r, -r * 0.62]], 'green', 1.5);+ gFigure(g, 0, -r * 0.82, r * 1.5, { robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'hold', attr: 'staff' });+ g.disc(0, r * 0.26, r * 0.11, 'white', 4.5, 8, { came: 0.01 }); g.disc(r * 0.13, r * 0.3, r * 0.05, 'pearl', 4.8, 6, { came: 0.008 });+ for (const [x, y] of [[-0.68, -0.64], [0.66, -0.62], [-0.46, -0.78]]) { g.disc(r * x, r * y, r * 0.11, 'white', 3, 7, { came: 0.01 }); g.disc(r * x + r * 0.12, r * y + r * 0.05, r * 0.05, 'grey', 3.2, 5, { came: 0.008 }); }+ },+ david(g, r) { // King David with his harp+ gFigure(g, -r * 0.14, -r * 0.82, r * 1.42, { pose: 'sit', robe: 'violet', mantle: 'ruby', crown: 1, beard: 1, arm: 'out', halo: false });+ g.poly([[r * 0.26, -r * 0.6], [r * 0.7, r * 0.2], [r * 0.7, r * 0.46], [r * 0.16, -r * 0.36]], 'gold', 3, { came: 0.012 });+ g.line([[r * 0.2, -r * 0.4], [r * 0.2, r * 0.4]], 0.018, 'gold', 3.1); [0.3, 0.4, 0.5, 0.6].forEach((k) => g.line([[r * k, -r * 0.42 + r * (k - 0.2)], [r * k, r * 0.34]], 0.008, 'white', 3.2));+ g.poly([[-r * 0.86, -r * 0.84], [r * 0.5, -r * 0.84], [r * 0.5, -r * 0.7], [-r * 0.86, -r * 0.7]], 'tan', 1.8);+ },+};+function star(g, x, y, r, c, layer) { const p = []; for (let i = 0; i < 16; i++) { const a = i * PI / 8 + PI / 2, q = i % 2 ? r * 0.38 : r * (i % 4 ? 0.7 : 1); p.push([x + q * Math.cos(a), y + q * Math.sin(a)]); } g.poly(p, c, layer, { came: 0.01 }); }+function gBust(g, x, y, h, o = {}) { // head and shoulders+ const L = o.layer || 2;+ if (o.halo !== false) g.disc(x, y + h * 0.56, h * 0.17, o.halo || 'gold', L - 0.1, 10);+ g.poly([[x - h * 0.2, y - h * 0.3], [x + h * 0.2, y - h * 0.3], [x + h * 0.17, y + h * 0.3], [x + h * 0.06, y + h * 0.42], [x - h * 0.06, y + h * 0.42], [x - h * 0.17, y + h * 0.3]], o.robe || 'ruby', L);+ g.disc(x, y + h * 0.56, h * 0.105, 'flesh', L + 0.4, 8, { came: 0.01 });+ g.poly([[x - h * 0.11, y + h * 0.57], [x - h * 0.07, y + h * 0.66], [x + h * 0.07, y + h * 0.66], [x + h * 0.11, y + h * 0.57], [x + h * 0.07, y + h * 0.6], [x - h * 0.07, y + h * 0.6]], o.hair || 'hair', L + 0.6, { came: 0.008 });+ if (o.beard) g.poly([[x - h * 0.08, y + h * 0.53], [x + h * 0.08, y + h * 0.53], [x + h * 0.05, y + h * 0.38], [x - h * 0.05, y + h * 0.38]], o.beardCol || o.hair || 'hair', L + 0.6, { came: 0.008 });+ if (o.cross) for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[x + h * 0.17 * Math.cos(a - 0.24), y + h * 0.56 + h * 0.17 * Math.sin(a - 0.24)], [x + h * 0.17 * Math.cos(a + 0.24), y + h * 0.56 + h * 0.17 * Math.sin(a + 0.24)], [x, y + h * 0.56]], 'ruby', L + 0.2, { came: 0.008 }); }+}++// A jewelled ring of short leaded segments+function gRim(g, cx, cy, r0, r1, cols, n, layer) {+ g.disc(cx, cy, r1 + 0.02, 'lead', layer - 0.3, Math.max(10, n), { came: 0, jitter: 0 });+ for (let i = 0; i < n; i++) {+ const a = i * TAU / n, b = (i + 1) * TAU / n;+ g.poly([[cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)], [cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)], [cx + r1 * Math.cos(b), cy + r1 * Math.sin(b)], [cx + r0 * Math.cos(b), cy + r0 * Math.sin(b)]], cols[i % cols.length], layer, { came: -0.01 });+ }+}+// A lancet told as a stack of medallions over a diapered ground, with a jewelled border+function gLancetStory(g, x, w, y0, ys, rise, scenes, o = {}) {+ const c1 = o.c1 || 'ruby', c2 = o.c2 || 'cobalt', edge = o.edge || ['gold', 'white', 'rubyLt', 'white'];+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse().slice(0)];+ g.withClip(outline, () => {+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, o.cell || 0.6, c1, c2, 'gold');+ const H = ys + rise * 0.5 - y0, N = scenes.length, step = H / N, R = Math.min(w * 0.44, step * 0.47);+ scenes.forEach((nm, i) => {+ const cy = y0 + step * (i + 0.5);+ const ground = (i % 2) ? (o.g1 || 'cobalt') : (o.g2 || 'ruby');+ gRim(g, x, cy, R, R * 1.09, ['gold', 'white', 'rubyLt', 'white', 'azure', 'white'], 18, 1);+ g.disc(x, cy, R, ground, 1.3, 18, { came: 0 });+ g.withClip(ringPts(x, cy, R, 14), () => g.withXf(0, x, cy, 1, () => SCENES[nm](g, R)));+ if (i < N - 1) { const yy = y0 + step * (i + 1); for (const s of [-1, 1]) { g.disc(x + s * w * 0.31, yy, w * 0.1, 'gold', 1.2, 8); g.disc(x + s * w * 0.31, yy, w * 0.055, 'emerald', 1.5, 6, { came: 0.01 }); } }+ });+ // Border bricks follow the outline+ const bw = w * 0.085, stepB = 0.56;+ const stripe = (x0, y1, y2) => { let k = 0; g.poly([[x0, y1], [x0 + bw, y1], [x0 + bw, y2], [x0, y2]], 'lead', 3.5, { came: 0, jitter: 0 }); for (let y = y1; y < y2; y += stepB) g.poly([[x0, y], [x0 + bw, y], [x0 + bw, y + stepB], [x0, y + stepB]], edge[k++ % edge.length], 3.8, { came: -0.014 }); };+ stripe(x - w / 2, y0, ys + rise * 0.1); stripe(x + w / 2 - bw, y0, ys + rise * 0.1);+ // Saddle bars (iron armature)+ for (let y = y0 + 1.1; y < ys + rise * 0.6; y += 1.1) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.045], [x - w / 2, y + 0.045]], 'lead', 7, { came: 0, jitter: 0 });+ });+}+// A lancet filled by one monumental standing figure under a painted canopy (reads from the floor)+function gLancetSaint(g, x, w, y0, ys, rise, fig, o = {}) {+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse()];+ const gnd = o.ground || 'cobalt';+ g.withClip(outline, () => {+ const figY = y0 + 1.05, figH = (ys + rise * 0.1 - figY) * 0.84;+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, 0.8, gnd, o.ground2 || 'ruby', null);+ // Pedestal with an inscription band+ g.poly([[x - w * 0.42, y0], [x + w * 0.42, y0], [x + w * 0.42, y0 + 0.62], [x - w * 0.42, y0 + 0.62]], 'bone', 1.5);+ g.poly([[x - w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 1.02], [x - w * 0.46, y0 + 1.02]], 'gold', 1.6);+ for (let i = 0; i < 6; i++) g.line([[x - w * 0.34 + i * w * 0.13, y0 + 0.28], [x - w * 0.3 + i * w * 0.13, y0 + 0.44]], 0.035, 'paint', 2);+ // Canopy: gable, pinnacles, little windows+ const cy = figY + figH + 0.02, cH = ys + rise * 0.86 - cy;+ g.poly([[x - w * 0.48, cy], [x + w * 0.48, cy], [x + w * 0.48, cy + cH * 0.36], [x - w * 0.48, cy + cH * 0.36]], 'white', 1.6);+ g.poly([[x - w * 0.42, cy + cH * 0.34], [x + w * 0.42, cy + cH * 0.34], [x, cy + cH]], 'yellow', 1.7);+ g.poly([[x - w * 0.2, cy + cH * 0.38], [x + w * 0.2, cy + cH * 0.38], [x, cy + cH * 0.72]], 'ruby', 1.9, { came: 0.016 });+ for (const s of [-1, 1]) { g.poly([[x + s * w * 0.28, cy + cH * 0.02], [x + s * w * 0.42, cy + cH * 0.02], [x + s * w * 0.42, cy + cH * 0.3], [x + s * w * 0.28, cy + cH * 0.3]], 'night', 1.8, { came: 0.014 }); g.poly([[x + s * w * 0.3, cy + cH * 0.36], [x + s * w * 0.46, cy + cH * 0.36], [x + s * w * 0.38, cy + cH * 0.85]], 'white', 1.8); }+ gFigure(g, x, figY, figH, Object.assign({ layer: 2.5, sx: Math.min(1, w * 1.72 / figH) }, fig));+ for (let y = y0 + 1.5; y < ys + rise * 0.6; y += 1.5) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.05], [x - w / 2, y + 0.05]], 'lead', 9, { came: 0, jitter: 0 });+ });+}+function gOculus(g, cx, cy, r, o = {}) { // head of the window: a sexfoil with an angel or Christ+ g.withClip(ringPts(cx, cy, r, 20), () => {+ g.disc(cx, cy, r, o.ground || 'cobalt', 1, 20);+ for (let i = 0; i < 6; i++) { const a = i * PI / 3 + PI / 6; g.disc(cx + r * 0.68 * Math.cos(a), cy + r * 0.68 * Math.sin(a), r * 0.27, i % 2 ? 'ruby' : 'gold', 1.4, 8); }+ g.disc(cx, cy, r * 0.56, o.inner || 'rubyLt', 1.8, 14);+ g.withXf(0, cx, cy, 1, () => { if (o.bust) gBust(g, 0, -r * 0.45, r * 0.95, o.bust); else gFigure(g, 0, -r * 0.5, r * 0.98, { wings: 'gold', robe: 'white', mantle: 'emerald', arm: 'orant' }); });+ });+}++// A complete two-light window: lancets, oculus, base field. Local plane z=0, +Z = interior.+function makeWindowGlass(w, y0, ys, rise, seed, fill) {+ const g = new Glass(seed);+ const outline = archOutline(w, y0, ys, rise, 8);+ g.raw(outline.slice(), 'lead', -0.4, 0);+ g.withClip(outline, () => gDiaper(g, -w / 2, ys, w / 2, ys + rise, 0.55, 'cobalt', 'ruby', 'gold', 0));+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96;+ const syl = ys + rise * 0.02;+ fill.light(g, -lx, lw, y0, syl, lr, 0); fill.light(g, lx, lw, y0, syl, lr, 1);+ const oc = windowHead(w, ys, rise);+ gOculus(g, 0, oc.y, oc.r + 0.03, fill.oculus || {});+ return g.b;+}+function windowHead(w, ys, rise) { const r = w * 0.225; return { r, y: ys + rise * 0.62 }; }++// ==================================================================================+// STONE TRACERY for a two-light window (plane z=0, bars deep along Z)+// ==================================================================================+function addTracery(b, w, y0, ys, rise, o = {}) {+ const bw = o.bar || 0.26, bd = o.depth || 0.42, sec = traceryBar(bw, bd), secT = hexBar(bw * 0.62, bd * 0.8);+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96, syl = ys + rise * 0.02;+ // Central mullion with shaft ring and capital+ b.at(M.T(0, 0, 0), () => {+ b.sweep([[0, y0 - 0.05], [0, syl]], sec, {});+ b.at(M.T(0, syl, 0), () => b.lathe([[bw * 0.52, -0.3], [bw * 0.56, -0.2], [bw * 0.9, 0.02], [bw * 0.5, 0.06]], 6));+ });+ for (const s of [-1, 1]) {+ const arc = pointedArc(lw, lr, 5).map((p) => [s * lx + p[0], syl + p[1]]);+ b.sweep(arc, sec, {});+ // cusped trefoil head inside each lancet+ const cr = lw * 0.3, cy = syl + lr * 0.34;+ const cusp = []; for (let i = 0; i <= 8; i++) { const a = rad(-30 + i * 240 / 8); const R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([s * lx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, { caps: false });+ b.sweep([[s * (lx + lw / 2), cy - cr * 0.4], cusp[0]], secT, { caps: false });+ b.sweep([[s * (lx - lw / 2), cy - cr * 0.4], cusp[8]], secT, { caps: false });+ }+ // Oculus with six foils+ const oc = windowHead(w, ys, rise);+ b.sweep(ringPts(0, oc.y, oc.r, 14), sec, { closed: true });+ const foils = []; for (let i = 0; i < 18; i++) { const a = i * TAU / 18; const R = oc.r * (0.62 + 0.3 * Math.pow(Math.abs(Math.cos(a * 3)), 0.8)); foils.push([R * Math.cos(a), oc.y + R * Math.sin(a)]); }+ b.sweep(foils, secT, { closed: true, crease: 40 });+}++// ==================================================================================+// PIERCED PARAPET with quatrefoil panels+// ==================================================================================+function quatrefoil(cx, cy, r, n = 16) { const p = []; for (let i = 0; i < n; i++) { const a = i * TAU / n; const R = r * (0.72 + 0.28 * Math.abs(Math.cos(2 * a))); p.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); } return p; }+function addParapet(b, x0, x1, y, h, th, o = {}) {+ const n = Math.max(1, Math.round((x1 - x0) / (o.pitch || 1.05))), holes = [];+ const r = Math.min(h * 0.34, (x1 - x0) / n * 0.4);+ for (let i = 0; i < n; i++) holes.push(quatrefoil(x0 + (i + 0.5) * (x1 - x0) / n, y + h * 0.5, r, o.nh || 12));+ b.slab([[x0, y], [x1, y], [x1, y + h], [x0, y + h]], holes, -th / 2, th / 2, { sr: 0.6 });+ const cap = barSection(0.14, th + 0.14);+ b.sweep([[x0, y + h + 0.04], [x1, y + h + 0.04]], cap, {});+ b.sweep([[x0, y - 0.02], [x1, y - 0.02]], barSection(0.1, th + 0.1), { s: 0.8 });+}++// A wimperg: gable over a window with crockets, finial and a blind trefoil+function addGable(b, w, h, z0, th, y, o = {}) {+ const lod = o.lod === undefined ? 1 : o.lod;+ const holes = o.solid ? [] : [ringPts(0, y + h * 0.36, Math.min(w * 0.13, h * 0.2), 14)];+ if (!o.noField) b.slab([[-w / 2, y], [w / 2, y], [0, y + h]], holes, z0, z0 + th, { sf: 1, sb: 0.8 });+ const rake = barSection(0.18, th + 0.14);+ b.sweep([[-w / 2 - 0.06, y - 0.02], [0, y + h + 0.05]], rake, { z: z0 + th / 2 });+ b.sweep([[0, y + h + 0.05], [w / 2 + 0.06, y - 0.02]], rake, { z: z0 + th / 2 });+ const n = o.crockets === undefined ? Math.max(2, Math.round(Math.hypot(w / 2, h) / 1.05)) : o.crockets, sz = o.size || 0.46;+ const zc = z0 + th / 2;+ crocketsAlong(b, [-w / 2, y + 0.05, zc], [-0.03, y + h * 0.97, zc], n, sz, [-h, w / 2, 0], lod);+ crocketsAlong(b, [w / 2, y + 0.05, zc], [0.03, y + h * 0.97, zc], n, sz, [h, w / 2, 0], lod);+ b.at(M.T(0, y + h * 0.98, zc), () => addFinial(b, o.finial || 0.95, lod));+}+// A small blind trefoil-headed arcade on a wall face (plane z=0, moulding stands proud +Z)+function addBlindArcade(b, x0, x1, y0, ys, n, o = {}) {+ const w = (x1 - x0) / n, sec = hexBar(o.bar || 0.12, o.depth || 0.16), rise = w * 0.95;+ for (let i = 0; i < n; i++) {+ const x = x0 + (i + 0.5) * w;+ b.sweep(pointedArc(w * 0.9, rise, 4).map((p) => [x + p[0], ys + p[1]]), sec, { z: 0.04, caps: false });+ }+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * w;+ b.at(M.T(x, 0, 0.06), () => b.lathe([[0.1, y0], [0.065, y0 + 0.18], [0.065, ys - 0.14], [0.13, ys], [0.08, ys + 0.04]], 5));+ }+}++// ==================================================================================+// RIBBED VAULT: pointed-arc ribs, curved webs, carved or gilded boss.+// ==================================================================================+function arcH(L, H, f) { // height of a two-centred arc of span L, rise H at fraction f+ const c = (H * H - L * L / 4) / L, r = c + L / 2, d = Math.abs(f - 0.5) * L;+ return Math.sqrt(Math.max(0, r * r - (d + c) * (d + c)));+}+function addRib(b, A, B, yS, rise, sec, n = 8, half = false) { // rib between plan points A,B in their vertical plane+ const dx = B[0] - A[0], dz = B[1] - A[1], L = Math.hypot(dx, dz), ang = Math.atan2(dz, dx) * 180 / PI;+ const pts = [];+ if (half) for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L * 2, rise, t / 2)]); }+ else for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L, rise, t)]); }+ // plane: local X along A->B, local Y up+ b.at(M.of(M.T(A[0], 0, A[1]), M.Ry(-ang)), () => b.sweep(pts, sec, { caps: false }));+}+// One curved web between a side arc a-b and the crown c, faces downward whichever way the plan runs+function addVaultCell(A, a, b, c, yS, yC, sideRiseV, nS = 8, nT = 6) {+ const La = Math.hypot(c[0] - a[0], c[1] - a[1]), Lb = Math.hypot(c[0] - b[0], c[1] - b[1]), Lab = Math.hypot(b[0] - a[0], b[1] - a[1]), rise = yC - yS;+ const cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ A.grid((s, t) => {+ const px = lerp(a[0], c[0], t) * (1 - s) + lerp(b[0], c[0], t) * s, pz = lerp(a[1], c[1], t) * (1 - s) + lerp(b[1], c[1], t) * s;+ const ruled = lerp(yS + arcH(Lab, sideRiseV, s), yC, t);+ const dA = arcH(La * 2, rise, t / 2) - rise * t, dB = arcH(Lb * 2, rise, t / 2) - rise * t;+ const y = ruled + (1 - s) * (1 - s) * dA + s * s * dB;+ return [px, y, pz, 0.72 + 0.28 * t];+ }, nS, nT, { flip: cross < 0 });+}+function addRibVault(F, A, corners, yS, yC, o = {}) {+ // corners: 4 plan points in order; web cells between each side and the crown+ const c0 = corners, cx = (c0[0][0] + c0[1][0] + c0[2][0] + c0[3][0]) / 4, cz = (c0[0][1] + c0[1][1] + c0[2][1] + c0[3][1]) / 4;+ const crown = [cx, yC, cz], nS = o.nS || 8, nT = o.nT || 6, rise = yC - yS;+ const sideRise = o.sideRise || ((i) => rise * 0.94);+ for (let k = 0; k < 4; k++) {+ if (o.skipSide && o.skipSide[k]) continue;+ addVaultCell(A, c0[k], c0[(k + 1) % 4], [cx, cz], yS, yC, sideRise(k), nS, nT);+ }+ const sec = o.ribSec || rollSection(0.32, 0.42, 2, 0.04, 2);+ for (const c of c0) addRib(F, c, [cx, cz], yS, rise, sec, 8, true);+ for (let k = 0; k < 4; k++) if (!(o.skipRib && o.skipRib[k])) addRib(F, c0[k], c0[(k + 1) % 4], yS, sideRiseRib(o, k, rise), o.sideRibSec || sec, 8);+ return crown;+}+const sideRiseRib = (o, k, rise) => (o.sideRise ? o.sideRise(k) : rise * 0.94);+function addBoss(G, F, c, r = 0.42) { // carved foliate boss with gilding+ G.at(M.T(c[0], c[1] - 0.02, c[2]), () => {+ G.lathe([[r * 0.25, -r * 0.54], [r * 0.6, -r * 0.5], [r * 0.95, -r * 0.3], [r, -r * 0.08], [r * 0.7, r * 0.05]], 10);+ for (let i = 0; i < 8; i++) addLeaf(G, M.of(M.T(0, -r * 0.22, 0), M.Ry(i * 45), M.T(r * 0.72, 0, 0), M.Rz(160)), r * 0.72);+ });+}++// ==================================================================================+// CLUSTERED PIER: a lobed section of shafts around a core, with bases and foliate capitals+// ==================================================================================+function lobedRing(core, shafts, n = 64) {+ const out = [];+ for (let i = 0; i < n; i++) {+ const a = TAU * i / n, c = Math.cos(a), s = Math.sin(a);+ let best = core, nx = c, nz = s;+ for (const sh of shafts) {+ // ray from the origin against each shaft circle+ const b = c * sh[0] + s * sh[1], disc = b * b - (sh[0] * sh[0] + sh[1] * sh[1] - sh[2] * sh[2]);+ if (disc >= 0) { const t = b + Math.sqrt(disc); if (t > best) { best = t; nx = (t * c - sh[0]) / sh[2]; nz = (t * s - sh[1]) / sh[2]; } }+ }+ out.push([best * c, best * s, nx, nz]);+ }+ return out;+}+function addExtrudedRing(b, ring, y0, y1, o = {}) {+ const s0 = o.s0 === undefined ? 1 : o.s0, s1 = o.s1 === undefined ? 1 : o.s1, k0 = o.k0 || 1, k1 = o.k1 || 1, N = ring.length;+ const base = b.count; let perim = 0;+ for (let i = 0; i <= N; i++) {+ const r = ring[i % N], rp = ring[(i + N - 1) % N];+ if (i > 0) perim += Math.hypot(r[0] - rp[0], r[1] - rp[1]);+ const shade = 0.5 + 0.5 * Math.pow(Math.abs(r[2] * r[0] + r[3] * r[1]) / (Math.hypot(r[0], r[1]) || 1), 2);+ b.v(r[0] * k0, y0, r[1] * k0, r[2], 0, r[3], perim, y0, s0 * shade);+ b.v(r[0] * k1, y1, r[1] * k1, r[2], 0, r[3], perim, y1, s1 * shade);+ }+ for (let i = 0; i < N; i++) b.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+}+function addCapital(b, ring, y, h, k, o = {}) { // bell with crocket leaves on each shaft, moulded abacus+ const N = ring.length;+ addExtrudedRing(b, ring, y, y + h * 0.1, { k0: 1.0, k1: 1.03, s0: 0.8 });+ addExtrudedRing(b, ring, y + h * 0.1, y + h * 0.82, { k0: 1.03, k1: k, s0: 0.62, s1: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.82, y + h * 0.9, { k0: k, k1: k + 0.1, s0: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.9, y + h, { k0: k + 0.1, k1: k + 0.12, s0: 0.95 });+ const top = ring.map((r) => [r[0] * (k + 0.12), r[1] * (k + 0.12)]);+ const ids = top.map((p) => b.v(p[0], y + h, p[1], 0, 1, 0, undefined, undefined, 0.9)); for (let i = 1; i < N - 1; i++) b.tri(ids[0], ids[i + 1], ids[i]);+ const leaves = o.leaves || 12;+ for (let i = 0; i < leaves; i++) {+ const a = i * 360 / leaves + (o.phase || 0), idx = Math.round(i / leaves * N) % N, r = ring[idx], R = Math.hypot(r[0], r[1]);+ addLeaf(b, M.of(M.T(0, y + h * 0.1, 0), M.Ry(-a), M.T(R * 1.02, 0, 0)), h * (i % 2 ? 0.72 : 0.56), { coarse: 1 });+ }+}++// The bay plan shared by every straight module (+Z side of the axis)+const zn = P.naveSpan / 2, zaw = zn + P.aisleSpan, BAY = P.bay;+// Window openings follow the bay, so changing the bay length re-proportions the glass and tracery+const WIN = {+ aW: BAY - 1.95, aSill: 4.3, aSp: P.pierTop - 1, aRise: 5.6 * (BAY - 1.95) / 4.65,+ cW: BAY - 1.55, cSill: P.triforiumTop + 0.45, cSp: P.vaultSpring + 4.8, cRise: 6.7 * (BAY - 1.55) / 5.05,+};+const windowArgs = { aisle: [WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise], clere: [WIN.cW, WIN.cSill, WIN.cSp, WIN.cRise] };+function bayShade(x, y, z, nx, ny, nz, b) { // baked light: dim warm interior, weathered exterior+ const px = z + nz * 0.3, py = y + ny * 0.3;+ const inside = (py < P.aisleWallTop - 0.3 && px < zaw) || (px < zn && py < P.eaves + 0.5);+ if (inside) {+ b.rgb = [1, 0.965, 0.915];+ let s = 0.38 + 0.3 * sstep(0, 34, y);+ if (ny < -0.45) s *= 0.8;+ if (ny > 0.6) s *= 1.15;+ return s;+ }+ b.rgb = [1, 0.99, 0.965];+ let s = lerp(0.68, 1, sstep(0, 7, y));+ if (ny < -0.4) s *= 0.55;+ return s * (0.94 + 0.06 * Math.sin(x * 1.7 + y * 0.31));+}+const mkBatches = () => ({ A: new Batch(TILE.ashlar), F: new Batch(TILE.fine), L: new Batch(TILE.lead), G: new Batch(TILE.gold), C: new Batch(TILE.carved), O: new Batch(TILE.oak), I: new Batch(TILE.iron) });+function placeSet(name, set, pos, rot, parent, extra = {}) {+ const o = { pos, rot: [0, rot, 0], parent, scale: extra.scale };+ place(name + '_Ashlar', set.A, MAT.ashlar, o);+ place(name + '_Stone', set.F, MAT.fine, o);+ place(name + '_Carved', set.C, MAT.carved, o);+ place(name + '_Lead', set.L, MAT.lead, o);+ place(name + '_Gilt', set.G, MAT.gold, o);+ place(name + '_Oak', set.O, MAT.oak, o);+ place(name + '_Iron', set.I, MAT.iron, o);+}+// Glass pair: interior face + the same panes mirrored as the exterior face+function placeGlass(name, batch, pos, rotY, parent) {+ place(name + '_In', batch, MAT.glassIn, { pos, rot: [0, rotY, 0], parent });+ place(name + '_Out', batch, MAT.glassOut, { pos, rot: [0, rotY, 0], scale: [1, 1, -1], parent });+}++function buildNaveBay(o = {}) {+ const set = o.into || mkBatches(), { A, F, L, G } = set;+ const shadeFn = o.shade || bayShade;+ A.fn = shadeFn; F.fn = shadeFn; L.fn = (x, y) => 0.86 + 0.14 * sstep(20, 45, y);+ const B = o.B || BAY, ww = o.ww || BAY - 2.1, sp = P.pierTop, archRise = o.archRise || 5.1, thick = 0.85;+ const zw = o.zw === undefined ? zn : o.zw;+ if (o.part !== 'aisle') {+ // ---- Arcade wall with the moulded pointed arch ----+ const arc = pointedArc(ww, archRise, 7).map((p) => [p[0], p[1] + sp]);+ A.at(M.T(0, 0, zw), () => {+ A.slab([[-B / 2, sp], [-ww / 2, sp], ...arc, [ww / 2, sp], [B / 2, sp], [B / 2, P.triforiumSill], [-B / 2, P.triforiumSill]], [], -thick / 2, thick / 2, { noOuterSides: true });+ });+ F.at(M.T(0, 0, zw), () => { F.sweep(arc, rollSection(0.82, thick + 0.25, 2, 0.08, 3), {}); });+ // Hood mould over the arch on the nave side, with carved label stops+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ const hood = pointedArc(ww + 1.9, archRise + 1.0, 6).map((p) => [p[0], p[1] + sp]);+ F.sweep(hood, stringSection(0.16, 0.14), {});+ });+ // ---- Triforium: string course, four cusped arches, dark wall passage ----+ const yT = P.triforiumSill, yT2 = P.triforiumTop, nT = o.nT || 4, tw = o.tw || (BAY - 1.72) / 4, ty = yT + 0.62, tspr = yT2 - 1.5, gapT = nT > 2 ? 0.08 : 0;+ const trfX = (i) => (i + 0.5 - nT / 2) * tw + (i < nT / 2 ? -gapT : gapT);+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ F.sweep([[-B / 2, yT], [B / 2, yT]], stringSection(0.34, 0.3), {});+ F.sweep([[-B / 2, yT2], [B / 2, yT2]], stringSection(0.3, 0.26), {});+ // Colonnette arcade standing in the openings+ for (let i = 0; i < nT; i++) {+ const x = trfX(i);+ const a = pointedArc(tw * 0.92, 0.98, 4).map((p) => [x + p[0], tspr + p[1]]);+ F.sweep(a, hexBar(0.13, 0.26), { z: 0.05 });+ F.sweep(ringPts(x, tspr + 0.32, 0.17, 6), hexBar(0.05, 0.1), { z: 0.05, closed: true, s: 0.9 });+ }+ for (let i = 0; i <= nT; i++) {+ const mid = i === nT / 2;+ const x = (i - nT / 2) * tw + (i < nT / 2 ? -gapT : (mid ? 0 : gapT));+ const r = mid ? 0.14 : 0.085;+ F.at(M.T(x, 0, 0.05), () => F.lathe([[r * 1.7, ty - 0.02], [r, ty + 0.2], [r, tspr - 0.2], [r * 1.9, tspr - 0.01], [r * 1.5, tspr + 0.03]], 6));+ }+ });+ const holes = []; // openings through the inner skin+ for (let i = 0; i < nT; i++) { const x = trfX(i); holes.push(archOutline(tw * 0.92, ty, tspr, 0.98, 6).map((p) => [x + p[0], p[1]])); }+ A.at(M.T(0, 0, zw - thick / 2 + 0.22), () => A.slab([[-B / 2, yT], [B / 2, yT], [B / 2, yT2], [-B / 2, yT2]], holes, -0.22, 0.22, { noOuterSides: true, sf: 0.45, sr: 0.5 }));+ // Passage back wall and floor: seen only through the arcade, kept dark+ A.shade(0.34, () => {+ A.face([[B / 2, yT, zw + 0.92], [-B / 2, yT, zw + 0.92], [-B / 2, yT2 + 0.5, zw + 0.92], [B / 2, yT2 + 0.5, zw + 0.92]]);+ A.face([[-B / 2, yT + 0.02, zw + 0.92], [B / 2, yT + 0.02, zw + 0.92], [B / 2, yT + 0.02, zw - thick / 2 + 0.44], [-B / 2, yT + 0.02, zw - thick / 2 + 0.44]]);+ A.face([[-B / 2, yT2 + 0.2, zw - thick / 2 + 0.44], [B / 2, yT2 + 0.2, zw - thick / 2 + 0.44], [B / 2, yT2 + 0.2, zw + 0.92], [-B / 2, yT2 + 0.2, zw + 0.92]]);+ });+ // ---- Clerestory wall, window hole, tracery ----+ const cw = o.cw || WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = o.cRise || WIN.cRise;+ const yE = P.eaves;+ A.at(M.T(0, 0, zw), () => A.slab([[-B / 2, yT2], [B / 2, yT2], [B / 2, yE], [-B / 2, yE]], [archOutline(cw, cSill, cSp, cRise, 9)], -0.42, 0.42, { noOuterSides: true, sr: 0.66 }));+ F.at(M.T(0, 0, zw), () => {+ if (o.lights === 1) addLancetTracery(F, cw, cSill, cSp, cRise);+ else addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.44 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 9).slice(0), rollSection(0.14, 0.86, 1, 0.03, 2), { closed: true, s: 0.92, crease: 40 });+ });+ // Exterior: hood mould, wimperg gable, cornice, pierced parapet, corbel table+ F.at(M.T(0, 0, zw + 0.42), () => {+ F.sweep(pointedArc(cw + 0.5, cRise + 0.3, 6).map((p) => [p[0], p[1] + cSp]), hexBar(0.22, 0.26), {});+ addGable(F, Math.min(cw + 0.9, B * 0.96), 3.4, 0.02, 0.2, cSp + cRise * 0.52, { size: 0.44, finial: 0.8, lod: 0, crockets: 3 });+ F.sweep([[-B / 2, yT2 - 0.02], [B / 2, yT2 - 0.02]], stringSection(0.3, 0.2), {});+ F.sweep([[-B / 2, yE - 0.12], [B / 2, yE - 0.12]], stringSection(0.34, 0.38), {});+ const nCorb = Math.round(B / 1.1);+ for (let i = 0; i < nCorb; i++) { const x = -B / 2 + (i + 0.5) * B / nCorb; F.box(x - 0.13, x + 0.13, yE - 0.62, yE - 0.32, 0, 0.26, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zw + 0.3), () => addParapet(F, -B / 2, B / 2, yE, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Upper wall top closing the gutter+ A.face([[-B / 2, yE, zw + 0.42], [B / 2, yE, zw + 0.42], [B / 2, yE, zw - 0.42], [-B / 2, yE, zw - 0.42]], 0.7);+ }+ if (o.part !== 'upper') {+ // ---- Aisle roof (lean-to in lead) ----+ if (!o.noAisleRoof) {+ const z0 = zn + 0.42, z1 = zaw + 1.05, y0r = P.aisleRoofHigh, y1r = P.aisleWallTop - 0.2;+ L.face([[-B / 2, y0r, z0], [-B / 2, y1r, z1], [B / 2, y1r, z1], [B / 2, y0r, z0]]);+ }+ if (!o.open) {+ // ---- Aisle wall ----+ const wa = WIN.aW, sill = WIN.aSill, aSp = WIN.aSp, aRise = WIN.aRise, zOut = zaw + 0.45;+ A.at(M.T(0, 0, zOut), () => A.slab([[-B / 2, 0], [B / 2, 0], [B / 2, P.aisleWallTop], [-B / 2, P.aisleWallTop]], [archOutline(wa, sill, aSp, aRise, 9)], -0.45, 0.45, { noOuterSides: true, sr: 0.7 }));+ F.at(M.T(0, 0, zOut), () => {+ addTracery(F, wa, sill, aSp, aRise, { bar: 0.25, depth: 0.42 });+ F.sweep(archOutline(wa, sill, aSp, aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ });+ // Exterior of the aisle wall+ F.at(M.T(0, 0, zaw + 0.9), () => {+ F.sweep([[-B / 2, sill - 0.2], [B / 2, sill - 0.2]], stringSection(0.34, 0.3), {});+ F.sweep(pointedArc(wa + 0.46, aRise + 0.28, 6).map((p) => [p[0], p[1] + aSp]), hexBar(0.22, 0.26), {});+ addGable(F, wa + 0.8, 3.1, 0.02, 0.2, aSp + 2.95, { size: 0.44, finial: 0.8, crockets: 4 });+ F.sweep([[-B / 2, P.aisleWallTop - 0.12], [B / 2, P.aisleWallTop - 0.12]], stringSection(0.32, 0.34), {});+ // Plinth with a chamfered weathering+ F.sweep([[-B / 2, 0.55], [B / 2, 0.55]], stringSection(0.4, 0.42), {});+ F.sweep([[-B / 2, 1.3], [B / 2, 1.3]], stringSection(0.2, 0.2), {});+ addBlindArcade(F, -B / 2 + 0.9, B / 2 - 0.9, 1.45, 2.65, 4, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(0, 0, zaw + 0.72), () => addParapet(F, -B / 2, B / 2, P.aisleWallTop, 1.0, 0.24, { pitch: 1.3 }));+ A.face([[-B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw], [-B / 2, P.aisleWallTop, zaw]], 0.7);+ // Interior dado: blind arcade with a niche for a statue+ F.at(M.of(M.T(0, 0, zaw), M.Ry(180)), () => {+ F.sweep([[-B / 2, sill - 0.14], [B / 2, sill - 0.14]], stringSection(0.26, 0.22), {});+ addBlindArcade(F, -B / 2 + 0.5, -1.05, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ addBlindArcade(F, 1.05, B / 2 - 0.5, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ F.sweep([[-B / 2, 0.42], [B / 2, 0.42]], stringSection(0.4, 0.34), {});+ // Niche canopy over the statue+ addNicheFrame(F, 0, 0.6, 3.9, 1.05);+ });+ }+ // ---- Aisle vault ----+ if (!o.noAisleVault) {+ const z0 = zn + thick / 2, z1 = zaw;+ const crown = addRibVault(F, A, [[-B / 2, z0], [B / 2, z0], [B / 2, z1], [-B / 2, z1]], sp, P.aisleCrown - 0.3, { sideRise: (k) => (k % 2 === 0 ? (k === 0 ? archRise + 0.1 : 5.0) : 5.2), nS: 6, nT: 4, ribSec: rollSection(0.26, 0.34, 2, 0.03, 2) });+ addBoss(G, F, crown, 0.32);+ }+ }+ return set;+}+// Canopied niche frame: moulded opening, gablet with crockets. Statue placed separately.+function addNicheFrame(F, x, y0, yTop, w) {+ F.at(M.T(x, 0, 0), () => {+ F.shade(0.5, () => F.face([[-w / 2, y0, -0.005], [w / 2, y0, -0.005], [w / 2, yTop - 0.75, -0.005], [0, yTop - 0.1, -0.005], [-w / 2, yTop - 0.75, -0.005]]));+ F.box(-w / 2 - 0.05, w / 2 + 0.05, y0 - 0.14, y0 + 0.08, 0, 0.5, { s: 0.92 });+ F.sweep(archOutline(w, y0, yTop - 0.82, 0.72, 5).slice(2), barSection(0.11, 0.2), { z: 0.08 });+ for (const s of [-1, 1]) { F.at(M.T(s * w / 2, 0, 0.1), () => F.lathe([[0.07, y0 + 0.05], [0.05, y0 + 0.12], [0.05, yTop - 0.9], [0.09, yTop - 0.82], [0.05, yTop - 0.78]], 6)); }+ addGable(F, w + 0.24, 0.92, 0.06, 0.1, yTop - 0.98, { size: 0.19, crockets: 2, finial: 0.32, solid: true, lod: 0 });+ });+}++// Clustered nave pier with the vault shafts rising to the springing, on +Z = nave... shafts toward -Z+function buildPier() {+ const set = mkBatches(), { F, A, G } = set;+ F.fn = bayShade; A.fn = bayShade;+ const core = 0.8, big = 0.25, small = 0.14;+ const shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.1) * Math.cos(a), (core + 0.1) * Math.sin(a), big]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; shafts.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 48);+ const yB = 1.15, yC = P.pierTop - 1.05;+ // moulded base: octagonal plinth, roll, scotia, then the lobed shaft+ F.lathe([[1.7, 0], [1.7, 0.56], [1.58, 0.62]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.62, 0.86, { k0: 1.24, k1: 1.27 });+ addExtrudedRing(F, ring, 0.86, yB, { k0: 1.27, k1: 1.0, s0: 0.75, s1: 0.72 });+ addExtrudedRing(F, ring, yB, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.05, 1.2, { leaves: 14 });+ // The vault shafts rising from the capital up the nave wall (toward -Z)+ const upper = [[0, -(core + 0.1), big], [-0.5, -(core - 0.04), small], [0.5, -(core - 0.04), small]];+ for (const u of upper) {+ F.at(M.T(u[0], 0, u[1] + 0.44), () => {+ const r = u[2], yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, P.triforiumSill - 0.1], [r * 1.5, P.triforiumSill + 0.06], [r, P.triforiumSill + 0.22], [r, yS - 0.85], [r * 1.5, yS - 0.8], [r * 1.14, yS - 0.7], [r * 2.2, yS]], 8);+ for (let i = 0; i < 4; i++) addLeaf(F, M.of(M.T(0, yS - 0.72, 0), M.Ry(i * 90 + 45), M.T(r * 1.1, 0, 0)), 0.56, { coarse: 1 });+ });+ }+ // Statue stage on the nave face: corbel below, canopy above (the figure is placed separately)+ const zF = -(core + 0.34);+ F.at(M.T(0, 0, zF), () => {+ F.lathe([[0.1, 5.0], [0.28, 5.25], [0.44, 5.6], [0.5, 5.72], [0.44, 5.78]], 8, { s: 0.85 });+ for (let i = 0; i < 6; i++) addLeaf(F, M.of(M.T(0, 5.04, 0), M.Ry(i * 60), M.T(0.14, 0, 0)), 0.66, { coarse: 1 });+ // Canopy over the statue: three arched gablets and a little spire+ F.at(M.T(0, 8.25, 0), () => {+ F.lathe([[0.5, -0.02], [0.54, 0.06], [0.42, 0.12], [0.3, 0.6], [0.02, 2.0]], 6, { faceted: true, phase: PI / 6 });+ for (let i = 0; i < 3; i++) F.at(M.of(M.Ry(-60 + i * 60 + 180), M.T(0, 0, 0.44)), () => addGable(F, 0.62, 0.62, -0.03, 0.06, 0.02, { size: 0.15, crockets: 1, finial: 0.2, solid: true, lod: 0 }));+ });+ });+ return set;+}+// Flying buttress unit at a bay boundary: massive pier, two arched flyers, tabernacle and pinnacles+function flyerShape(zA, yA, zB, yB, thick) { // A at the pier (outer), B at the wall (inner); returns polygon points in (z,y)+ const N = 12, under = [], top = [];+ for (let i = 0; i <= N; i++) {+ const t = i / N, z = lerp(zA, zB, t), y = yA + (yB - yA) * Math.sin(t * PI / 2);+ under.push([z, y]);+ }+ const topA = [zA - 0.05, yA + thick * 2.05], topB = [zB, yB + thick * 0.6];+ return { under, topA, topB };+}+function buildButtress() {+ const set = mkBatches(), { A, F, G } = set;+ A.fn = bayShade; F.fn = bayShade;+ const hw = 0.88, z0 = zaw + 0.9, zB = 19.2;+ const tiers = [[0, 10, zB], [10, 17.5, zB - 0.5], [17.5, 23.4, zB - 0.9]];+ tiers.forEach((t, i) => {+ const w = hw - i * 0.08;+ A.box(-w, w, t[0], t[1], z0 - 0.05, t[2], { noBot: true });+ if (i < 2) {+ // sloped weathering set-off to the next stage+ const zt = tiers[i + 1][2], w2 = hw - (i + 1) * 0.08;+ A.face([[-w, t[1], t[2]], [w, t[1], t[2]], [w2, t[1] + 1.2, zt], [-w2, t[1] + 1.2, zt]], 1.0);+ F.at(M.of(M.T(0, 0, t[2]), M.Rx(0)), () => F.sweep([[-w - 0.04, t[1] - 0.14], [w + 0.04, t[1] - 0.14]], stringSection(0.26, 0.2), {}));+ }+ });+ // Moulded plinth around the base+ F.at(M.T(0, 0, zB), () => { F.sweep([[-hw - 0.2, 0.75], [hw + 0.2, 0.75]], stringSection(0.5, 0.42), {}); F.box(-hw - 0.3, hw + 0.3, 0, 0.52, -0.02, 0.32, { s: 0.75 }); });+ for (const s of [-1, 1]) F.at(M.of(M.T(s * hw, 0, 0), M.Ry(s * 90)), () => {+ const a = s > 0 ? -zB - 0.2 : z0, c = s > 0 ? -z0 : zB + 0.2;+ F.sweep([[a, 0.75], [c, 0.75]], stringSection(0.5, 0.42), {}); F.box(a, c, 0, 0.52, -0.02, 0.32, { s: 0.75 });+ });+ // Blind tracery on the front of the lower stages+ F.at(M.T(0, 0, zB), () => {+ addBlindArcade(F, -hw + 0.1, hw - 0.1, 2.0, 6.6, 1, { bar: 0.12, depth: 0.14 });+ addGable(F, hw * 1.7, 1.4, 0.02, 0.14, 8.0, { size: 0.3, crockets: 2, finial: 0.5, solid: true, lod: 0 });+ });+ F.at(M.T(0, 0, zB - 0.5), () => { addBlindArcade(F, -hw + 0.16, hw - 0.16, 11.6, 14.6, 1, { bar: 0.12, depth: 0.14 }); });+ // The rear of the pier rises higher to take the flyers, ending in a sloped weathering+ const zStack = 16.25, yStack = 30.6;+ A.box(-hw + 0.14, hw - 0.14, 23.3, yStack, z0 - 0.05, zStack, { noBot: true, noTop: true });+ A.face([[-hw + 0.14, yStack, zStack], [hw - 0.14, yStack, zStack], [hw - 0.14, yStack + 1.1, z0 - 0.05], [-hw + 0.14, yStack + 1.1, z0 - 0.05]]);+ F.at(M.T(0, 0, zStack), () => F.sweep([[-hw + 0.1, yStack - 0.1], [hw - 0.1, yStack - 0.1]], stringSection(0.24, 0.2), {}));+ // Tabernacle of colonnettes at the top of the pier holding an angel+ const zT = 17.3, yT0 = 23.4, yT1 = 28.4;+ F.box(-hw, hw, yT0 - 0.02, yT0 + 0.1, zStack - 0.02, zB - 0.9 + 0.02, { s: 0.95 });+ for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) F.at(M.T(sx * 0.72, 0, zT + sz * 0.72), () => F.lathe([[0.2, yT0], [0.14, yT0 + 0.12], [0.1, yT0 + 0.2], [0.1, yT1 - 0.22], [0.14, yT1 - 0.15], [0.22, yT1]], 8));+ // Canopy: arched, gabled and crowned by the crocketed pinnacle+ for (let i = 0; i < 4; i++) F.at(M.of(M.T(0, 0, zT), M.Ry(i * 90), M.T(0, 0, 0.72)), () => {+ F.sweep(pointedArc(1.3, 1.25, 6).map((p) => [p[0], yT1 - 1.25 + p[1] - 0.02]), traceryBar(0.1, 0.16), {});+ addGable(F, 1.7, 1.25, -0.06, 0.12, yT1, { size: 0.28, crockets: 2, finial: 0.48, lod: 0, solid: true });+ });+ F.box(-0.92, 0.92, yT1 - 0.06, yT1 + 0.18, zT - 0.92, zT + 0.92, { s: 0.9 });+ F.at(M.T(0, yT1 + 0.18, zT), () => addPinnacle(F, 1.42, 8.4, { lod: 1 }));+ // Two flyers. Each is a pierced plate, with a coping and crockets along the top+ const fly = (zA, yA, zB, yB, th) => {+ const { under, topA, topB } = flyerShape(zA, yA, zB, yB, th);+ const outline = [...under, topB, topA];+ const holes = [];+ // Openwork roundels in the spandrel of each flyer+ [0.28, 0.46, 0.62].forEach((t) => { const z = lerp(zA, zB, t), yu = yA + (yB - yA) * Math.sin(t * PI / 2), yt = lerp(topA[1], topB[1], t), r = (yt - yu) * 0.26; if (r > 0.14) holes.push(ringPts(z, (yu + yt) / 2, r, 10)); });+ F.at(M.Ry(-90), () => {+ F.slab(outline, holes, -0.4, 0.4, { sr: 0.62, so: 0.7 });+ // Coping along the top with a channel, crockets marching up+ F.sweep([topA, topB], barSection(0.2, 1.0), { z: 0, s: 1.0 });+ });+ const n = Math.round(Math.hypot(topA[0] - topB[0], topA[1] - topB[1]) / 1.0);+ crocketsAlong(F, [0, topA[1] + 0.1, topA[0]], [0, topB[1] + 0.1, topB[0]], n, 0.62, [0, 1, 0], 0);+ };+ fly(zStack - 0.1, 28.0, zn + 1.12, 35.6, 0.48);+ fly(zStack - 0.1, 22.0, zn + 1.12, 28.6, 0.54);+ // Upper pier on the clerestory wall where the flyers land, with its own pinnacle above the parapet+ const yE = P.eaves;+ A.box(-0.62, 0.62, P.aisleRoofHigh - 1, yE + 1.2, zn + 0.42, zn + 1.14, { s: 0.98, noBot: true });+ A.face([[-0.62, yE + 1.2, zn + 1.14], [0.62, yE + 1.2, zn + 1.14], [0.62, yE + 1.95, zn + 0.42], [-0.62, yE + 1.95, zn + 0.42]]);+ F.at(M.T(0, yE + 1.2, zn + 0.74), () => addPinnacle(F, 0.9, 6.4, { simple: 1 }));+ F.at(M.T(0, 0, zn + 1.14), () => { F.lathe([[0.16, 31.2], [0.1, 31.4], [0.1, 40.5], [0.17, 40.8]], 8); });+ // Interior respond on the aisle wall: a small cluster carrying the aisle vault+ const ringR = lobedRing(0.32, [[0, -0.44, 0.17], [-0.34, -0.26, 0.11], [0.34, -0.26, 0.11]], 26);+ F.at(M.T(0, 0, zaw), () => {+ addExtrudedRing(F, ringR, 0.4, P.pierTop - 0.8, { s0: 0.8 });+ addCapital(F, ringR, P.pierTop - 0.8, 0.8, 1.2, { leaves: 6 });+ F.lathe([[0.62, 0], [0.62, 0.42], [0.42, 0.5]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ });+ // Spout from the aisle gutter+ return set;+}++function addLancetTracery(b, w, y0, ys, rise) { // single-light frame with a cusped head+ const secT = traceryBar(0.13, 0.3), cr = w * 0.32, cy = ys + rise * 0.34, cusp = [];+ for (let i = 0; i <= 12; i++) { const a = rad(-30 + i * 240 / 12), R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, {});+ b.sweep([[w / 2, cy - cr * 0.4], cusp[0]], secT, {});+ b.sweep([[-w / 2, cy - cr * 0.4], cusp[12]], secT, {});+}+function makeLancetGlass(w, y0, ys, rise, seed, fig, o) {+ const g = new Glass(seed);+ g.raw(archOutline(w, y0, ys, rise, 8), 'lead', -0.4, 0);+ gLancetSaint(g, 0, w, y0, ys, rise, fig, o || {});+ return g.b;+}++// ==================================================================================+// PLAN: everything is derived from the parameters at the top of the file.+// ==================================================================================+const LAY = (() => {+ const xW = P.xWest, xTowerE = xW - P.towerDepth, xNaveE = xTowerE - P.naveBays * BAY, xc = xNaveE - zn, xChoirW = xc - zn, xa = xChoirW - P.choirBays * BAY;+ const ridge = P.eaves + (zn + 1) * P.roofPitch;+ return { xW, xTowerE, xNaveE, xc, xChoirW, xa, ridge };+})();+// A rigid module frame: rotation about Y at an origin on the ground+const frame = (th, ox, oz) => {+ const c = Math.cos(rad(th)), s = Math.sin(rad(th));+ return { th, ox, oz, pos: (x, y, z) => [ox + x * c + z * s, y, oz - x * s + z * c], rot: (d = 0) => [0, th + d, 0] };+};+const SAINTS = ['apostle', 'peter', 'paul', 'bishop', 'prophet', 'martyr', 'paul', 'apostle', 'bishop', 'prophet'];++// Baked floor light: dim towards the walls and the corners+function floorShade(x, y, z, nx, ny, nz, b) { b.rgb = [1, 0.95, 0.88]; return 0.52; }++function buildVaultNave() {+ const s = mkBatches(); s.A.fn = bayShade; s.F.fn = bayShade;+ const crown = addRibVault(s.F, s.A, [[-BAY / 2, -zn + 0.42], [BAY / 2, -zn + 0.42], [BAY / 2, zn - 0.42], [-BAY / 2, zn - 0.42]], P.vaultSpring, P.vaultCrown, { sideRise: (k) => (P.vaultCrown - P.vaultSpring) * (k % 2 === 0 ? 0.86 : 0.99), nS: 10, nT: 8 });+ addBoss(s.G, s.F, crown, 0.55);+ return s;+}++// ---- Chevet: hemicycle, ambulatory and a radiating chapel per sector ----------------+const CHEV = (() => {+ const d = 180 / P.apseSides, h = rad(d / 2), Ra = zn * Math.cos(h), hs = zn * Math.sin(h), Ro = zaw * Math.cos(h), hso = zaw * Math.sin(h);+ const Rc = 2.5, zc = Ro + 3.0;+ return { d, h, Ra, hs, Ro, hso, Rc, zc };+})();+function chevetShade(x, y, z, nx, ny, nz, b) {+ const px = x + nx * 0.3, py = y + ny * 0.3, pz = z + nz * 0.3, inHem = pz < CHEV.Ra && py < P.eaves + 0.5;+ const inAmb = pz < CHEV.Ro && py < P.aisleWallTop - 0.3;+ // the chapel itself, and the neck leading into it from the ambulatory+ const inCh = (Math.hypot(px, pz - CHEV.zc) < CHEV.Rc || (pz < CHEV.zc && pz >= CHEV.Ro && Math.abs(px) < 2.25)) && py < 15.8;+ if (inHem || inAmb || inCh) {+ b.rgb = [1, 0.95, 0.87];+ let s = 0.36 + 0.28 * sstep(0, 34, y);+ if (ny < -0.45) s *= 0.8;+ if (ny > 0.6) s *= 1.15;+ return s;+ }+ b.rgb = [1, 0.99, 0.965];+ let s = lerp(0.68, 1, sstep(0, 7, y));+ if (ny < -0.4) s *= 0.55;+ return s;+}+function buildChevetSector() {+ const set = mkBatches(), { A, F, L, G } = set, { Ra, hs, Ro, hso, Rc, zc } = CHEV;+ buildNaveBay({ into: set, part: 'upper', B: 2 * hs, zw: Ra, ww: 1.62, archRise: 5.0, nT: 2, tw: 1.05, cw: 2.05, cRise: 4.3, lights: 1, shade: chevetShade });+ A.fn = chevetShade; F.fn = chevetShade; L.fn = (x, y) => 0.86 + 0.14 * sstep(14, 45, y);+ // Hemicycle vault: one web from this side up to the crown over the apse centre+ const yS = P.vaultSpring, yC = P.vaultCrown;+ addVaultCell(A, [-hs + 0.0, Ra - 0.42], [hs, Ra - 0.42], [0, 0], yS, yC, 9.6, 7, 8);+ const sec = rollSection(0.32, 0.42, 2, 0.04, 2);+ addRib(F, [-hs, Ra - 0.42], [hs, Ra - 0.42], yS, 9.6, sec, 6);+ addRib(F, [-zn * Math.sin(CHEV.h), (zn - 0.42) * Math.cos(CHEV.h)], [0, 0], yS, yC - yS, sec, 9, true);+ // Ambulatory vault (trapezoid)+ const c2 = addRibVault(F, A, [[-hs, Ra + 0.42], [hs, Ra + 0.42], [hso, Ro], [-hso, Ro]], P.pierTop, P.aisleCrown - 0.3, { sideRise: (k) => (k === 0 ? 5.1 : k === 2 ? 5.2 : 5.1), nS: 6, nT: 4, ribSec: rollSection(0.24, 0.32, 2, 0.03, 2) });+ addBoss(G, F, c2, 0.3);+ // Outer wall with the arched chapel mouth+ const yW = P.aisleWallTop, mw = 3.55, mS = 8.6, mR = 4.8;+ const mouth = pointedArc(mw, mR, 8).map((p) => [p[0], p[1] + mS]);+ A.at(M.T(0, 0, Ro + 0.3), () => A.slab([[-hso, 0], [-mw / 2, 0], [-mw / 2, mS], ...mouth, [mw / 2, mS], [mw / 2, 0], [hso, 0], [hso, yW], [-hso, yW]], [], -0.3, 0.3, { noOuterSides: true, sr: 0.7 }));+ // The moulding fills the whole wall thickness: it is also the soffit of the opening+ F.at(M.T(0, 0, Ro + 0.3), () => F.sweep(mouth, rollSection(0.42, 0.96, 2, 0.05, 2), {}));+ F.at(M.T(0, 0, Ro + 0.6), () => { F.sweep([[-hso, yW - 0.12], [hso, yW - 0.12]], stringSection(0.3, 0.3), {}); addParapet(F, -hso, hso, yW, 1.0, 0.24, { pitch: 1.1 }); });+ A.face([[-hso, yW, Ro + 0.6], [hso, yW, Ro + 0.6], [hso, yW, Ro], [-hso, yW, Ro]], 0.7);+ // Ambulatory lean-to roof+ const zR0 = Ra + 0.42, hsR0 = (zR0 / Ra) * hs, zR1 = Ro + 0.9, hsR1 = (zR1 / Ro) * hso;+ L.face([[-hsR0, P.aisleRoofHigh, zR0], [-hsR1, yW - 0.2, zR1], [hsR1, yW - 0.2, zR1], [hsR0, P.aisleRoofHigh, zR0]]);+ // Hipped roof over the hemicycle+ const yE = P.eaves + 0.15, Re = zn + 0.72, hRe = Re * Math.sin(CHEV.h), aRe = Re * Math.cos(CHEV.h);+ L.grid((u, v) => [lerp(-hRe, hRe, u) * (1 - v), lerp(yE, LAY.ridge, v), aRe * (1 - v)], 2, 2, { flip: false });+ // Radiating chapel: five faceted walls, each with a tall lancet, rib vault, conical lead roof+ const nF = P.chapelFacets, span = 216, yC1 = 16.2, dF = span / nF, ap = Rc * Math.cos(rad(dF / 2)), halfF = Rc * Math.sin(rad(dF / 2)), lw = 1.3, sill = 3.2, lSp = 9.9, lR = 2.5;+ const verts = [];+ for (let i = 0; i <= nF; i++) { const f = rad(-span / 2 + i * dF); verts.push([Rc * Math.sin(f), zc + Rc * Math.cos(f)]); }+ for (let i = 0; i < nF; i++) {+ const fm = -span / 2 + (i + 0.5) * dF;+ const m = M.of(M.T(0, 0, zc), M.Ry(fm), M.T(0, 0, ap));+ A.at(m, () => A.slab([[-halfF, 0], [halfF, 0], [halfF, yC1], [-halfF, yC1]], [archOutline(lw, sill, lSp, lR, 7)], 0, 0.5, { noOuterSides: true, sr: 0.68 }));+ F.at(m, () => {+ addLancetTracery(F, lw, sill, lSp, lR);+ F.sweep(archOutline(lw, sill, lSp, lR, 5), hexBar(0.12, 0.56), { closed: true, z: 0.25, s: 0.9, crease: 40 });+ });+ F.at(M.of(m, M.T(0, 0, 0.5)), () => {+ F.sweep([[-halfF - 0.1, sill - 0.14], [halfF + 0.1, sill - 0.14]], stringSection(0.24, 0.2), { caps: false });+ F.sweep([[-halfF - 0.1, 0.5], [halfF + 0.1, 0.5]], stringSection(0.36, 0.3), { caps: false });+ F.sweep([[-halfF - 0.1, yC1 - 0.1], [halfF + 0.1, yC1 - 0.1]], stringSection(0.3, 0.3), { caps: false });+ addGable(F, lw + 0.9, 2.4, 0.01, 0.14, lSp + 2.15, { size: 0.32, crockets: 2, finial: 0.56, lod: 0 });+ addParapet(F, -halfF, halfF, yC1, 0.82, 0.18, { pitch: 1.3, nh: 8 });+ });+ addVaultCell(A, verts[i], verts[i + 1], [0, zc], 9.2, 14.6, 3.6, 5, 4);+ addRib(F, verts[i], verts[i + 1], 9.2, 3.6, rollSection(0.16, 0.22, 1, 0.02, 2), 5);+ }+ addVaultCell(A, verts[nF], verts[0], [0, zc], 9.2, 14.6, 4.2, 5, 4);+ for (let i = 0; i <= nF; i++) {+ addRib(F, verts[i], [0, zc], 9.2, 5.4, rollSection(0.17, 0.24, 1, 0.02, 2), 6, true);+ // Exterior angle buttress and little pinnacle; interior wall shaft+ const f = -span / 2 + i * dF, ve = verts[i];+ F.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => {+ A.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => A.box(-0.24, 0.24, 0, 14.2, 0.26, 1.02, { s: 0.98, noBot: true, taperZ: 0.3 }));+ F.at(M.T(0, 14.18, 0.62), () => addPinnacle(F, 0.62, 3.9, { simple: 1 }));+ F.lathe([[0.17, 0], [0.09, 0.6], [0.09, 9.1], [0.17, 9.2]], 5);+ });+ }+ // The neck joining the chapel to the ambulatory: side walls, a pointed barrel vault, a lead cap+ const zN0 = Ro + 0.6, zN1 = verts[0][1], hN0 = mw / 2, hN1 = Math.abs(verts[0][0]);+ for (const s of [-1, 1]) A.prism([[s * hN0, zN0 - 0.05], [s * hN1, zN1 + 0.05], [s * (hN1 + 0.5), zN1 + 0.05], [s * (hN0 + 0.5), zN0 - 0.05]], 0, yC1);+ // Same subdivision as the chapel's last web so the seam has no cracks; a rib covers the joint+ A.grid((u, v) => { const hw = lerp(hN0, hN1, v); return [lerp(-hw, hw, u), 9.2 + arcH(2 * hw, 4.2, u), lerp(Ro + 0.3, zN1 + 0.02, v), 0.8]; }, 5, 1, { flip: false }); // starts under the arch moulding, so no ray can pass above it+ addRib(F, verts[nF], verts[0], 9.2, 4.2, rollSection(0.16, 0.26, 1, 0.02, 2), 5);+ L.face([[-(hN0 + 0.5), yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN0 - 0.1], [-(hN0 + 0.5), yC1 + 0.03, zN0 - 0.1]]);+ // Lead cone+ L.at(M.T(0, 0, zc), () => { const n = 10, rb = Rc + 0.5; L.grid((u, v) => { const a = -PI + TAU * u, r = rb * (1 - v), y = lerp(yC1 + 0.05, yC1 + 7.4, v); return [r * Math.sin(a), y, r * Math.cos(a)]; }, n, 2, { flip: false, wrapU: true }); });+ G.at(M.T(0, yC1 + 7.3, zc), () => addFinial(G, 1.25, 1));+ return set;+}++// ---- Lead roofs with metric UVs so the seams run down the slope ----------------------+function addRoofRun(L, x0, x1, half, yE, yR) {+ const slope = Math.hypot(half, yR - yE);+ for (const s of [-1, 1]) {+ const a = L.v(x0, yE, s * half, 0, half, s * (yR - yE), x0, 0, 1), b = L.v(x1, yE, s * half, 0, half, s * (yR - yE), x1, 0, 1);+ const c = L.v(x1, yR, 0, 0, half, s * (yR - yE), x1, slope, 1), d = L.v(x0, yR, 0, 0, half, s * (yR - yE), x0, slope, 1);+ if (s > 0) L.quad(a, b, c, d); else L.quad(b, a, d, c);+ }+}+function buildCresting(len) { // gilded fleur-de-lis cresting for a length of ridge+ const G = new Batch(TILE.gold), n = Math.round(len / 0.9);+ G.box(-len / 2, len / 2, -0.04, 0.05, -0.05, 0.05, {});+ for (let i = 0; i < n; i++) {+ const x = -len / 2 + (i + 0.5) * len / n;+ G.at(M.T(x, 0.05, 0), () => {+ G.tube([[0, 0, 0], [0, 0.34, 0], [0, 0.62, 0]], (t) => [lerp(0.05, 0.005, t), lerp(0.035, 0.005, t)], 4);+ for (const s of [-1, 1]) G.tube(spline([[0, 0.14, 0], [s * 0.15, 0.26, 0], [s * 0.2, 0.42, 0], [s * 0.1, 0.46, 0]], 1), (t) => lerp(0.035, 0.008, t), 4);+ G.box(-0.08, 0.08, 0.12, 0.17, -0.03, 0.03, {});+ });+ }+ return G;+}++// ---- Crossing: four great piers, four arches, a gilded boss, the fleche above ----------+function buildCrossingPier() {+ const set = mkBatches(), { F } = set; F.fn = bayShade;+ const core = 1.08, sh = [];+ for (let i = 0; i < 8; i++) { const a = i * PI / 4; sh.push([(core + 0.12) * Math.cos(a), (core + 0.12) * Math.sin(a), i % 2 ? 0.2 : 0.3]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; sh.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), 0.13]); }+ const ring = lobedRing(core, sh, 80), yC = P.vaultSpring - 1.1;+ F.lathe([[2.05, 0], [2.05, 0.6], [1.9, 0.68]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.68, 0.94, { k0: 1.2, k1: 1.23 });+ addExtrudedRing(F, ring, 0.94, 1.25, { k0: 1.23, k1: 1, s0: 0.75, s1: 0.75 });+ addExtrudedRing(F, ring, 1.25, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.1, 1.14, { leaves: 16 });+ return set;+}+function buildAislePier() {+ const set = mkBatches(), { F } = set; F.fn = bayShade;+ const sh = []; for (let i = 0; i < 4; i++) { const a = i * PI / 2; sh.push([0.62 * Math.cos(a), 0.62 * Math.sin(a), 0.2]); }+ const ring = lobedRing(0.54, sh, 40);+ F.lathe([[1.15, 0], [1.15, 0.5], [1.02, 0.56]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.56, 0.86, { k0: 1.28, k1: 1, s0: 0.75 });+ addExtrudedRing(F, ring, 0.86, P.pierTop - 0.9, { s0: 0.8 });+ addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });+ return set;+}+function buildFleche(parent, x, z) {+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold), F = new Batch(TILE.fine);+ L.fn = () => 0.9;+ const y0 = LAY.ridge - 2.5, yB = LAY.ridge + 7, yT = P.flecheTip - 3.2, R = 2.9, n = 8;+ // Octagonal base and open arcaded lantern+ L.lathe([[R + 0.4, y0], [R + 0.25, y0 + 2.8], [R + 0.5, y0 + 3], [R + 0.5, y0 + 3.3], [R, y0 + 3.5]], n, { faceted: true, phase: PI / 8 });+ L.lathe([[R * 0.78, y0 + 3.5], [R * 0.78, yB + 0.3]], n, { faceted: true, phase: PI / 8, s: 0.34 });+ for (let i = 0; i < n; i++) {+ const a = i * 45, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ L.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap)), () => {+ for (const s of [-1, 1]) L.tube([[s * side * 0.5, y0 + 3.5, 0], [s * side * 0.5, yB, 0]], 0.13, 6, {});+ const arc = pointedArc(side * 0.92, 2.2, 6).map((p) => [p[0], yB - 2.45 + p[1]]);+ L.sweep(arc, traceryBar(0.13, 0.2), {});+ G.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap + 0.1)), () => addGable(G, side * 1.04, 1.6, -0.05, 0.08, yB - 0.24, { size: 0.26, crockets: 2, finial: 0.44, lod: 0, solid: true }));+ });+ }+ L.lathe([[R + 0.2, yB - 0.05], [R + 0.34, yB + 0.1], [R + 0.2, yB + 0.26]], n, { faceted: true, phase: PI / 8 });+ // The spire, its ribs and gilded crockets+ L.lathe([[R * 0.98, yB + 0.26], [R * 0.72, yB + 3.2], [0.1, yT]], n, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < n; i++) {+ const a = rad(i * 45 + 22.5 + 22.5), c = Math.cos(a), s = Math.sin(a);+ const pts = [[R * 0.98 * c * 1.0, yB + 0.3, R * 0.98 * s], [R * 0.72 * c, yB + 3.2, R * 0.72 * s], [0.14 * c, yT - 0.2, 0.14 * s]];+ G.tube(pts, (t) => lerp(0.1, 0.03, t), 5);+ crocketsAlong(G, pts[1], pts[2], 7, 0.66, [c, 0.1, s], 0);+ }+ G.at(M.T(0, yT - 0.15, 0), () => {+ G.lathe([[0.14, 0], [0.3, 0.28], [0.34, 0.5], [0.22, 0.72], [0.06, 0.9], [0.05, 3.2]], 10);+ G.box(-0.06, 0.06, 2.2, 3.35, -0.06, 0.06, {}); G.box(-0.6, 0.6, 2.56, 2.72, -0.06, 0.06, {});+ });+ place('Fleche_Lead', L, MAT.lead, { pos: [x, 0, z], parent });+ place('Fleche_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });+}++// Coloured light thrown onto the paving by the south aisle windows (sun from the south-west)+function buildLightPools(xs) {+ const b = new Batch(1); b.noColor = true;+ const rng = mkRng(99), cols = ['ruby', 'cobalt', 'gold', 'rubyLt', 'azure', 'emerald', 'cobalt', 'ruby', 'white', 'amber'];+ const sill = WIN.aSill, sp = WIN.aSp, rise = WIN.aRise, w = WIN.aW, cw = 0.58, ch = 0.92;+ const c = pointedArc(w, rise, 6);+ const halfAt = (v) => { if (v <= sp) return w / 2; const t = (v - sp) / rise; return (w / 2) * Math.sqrt(Math.max(0, 1 - t * t)); };+ for (const xw of xs) {+ for (let v = sill; v < sp + rise; v += ch) {+ const h0 = halfAt(v), h1 = halfAt(v + ch);+ for (let u = -w / 2; u < w / 2 - 1e-3; u += cw) {+ if (Math.abs(u) > h0 || Math.abs(u + cw) > h0) continue;+ if (Math.abs(u + cw / 2) < 0.16) continue; // the mullion throws a shadow+ const col = cols[Math.floor(rng() * cols.length)], idx = PAL[col], n = PALETTE_COLS, cell = 1 / n;+ const cu = ((idx % n) + 0.5) * cell, cv = (Math.floor(idx / n) + 0.5) * cell;+ const P0 = (uu, vv) => [xw + uu + vv * P.sunShear[0], 0.035, -zaw + vv * P.sunShear[1]];+ const pts = [P0(u, v), P0(u, v + ch * 0.96), P0(u + cw * 0.96, v + ch * 0.96), P0(u + cw * 0.96, v)];+ const ids = pts.map((p) => b.v(p[0], p[1], p[2], 0, 1, 0, cu, cv, 1));+ b.quad(ids[0], ids[1], ids[2], ids[3]);+ }+ }+ }+ return b;+}++function storyWindows() { // the narrative aisle windows+ const sets = [+ [['annunciation', 'nativity', 'magi', 'shepherd', 'supper'], ['crucifixion', 'resurrection', 'david', 'noah', 'annunciation']],+ [['noah', 'david', 'annunciation', 'nativity', 'magi'], ['shepherd', 'supper', 'crucifixion', 'resurrection', 'david']],+ [['magi', 'supper', 'noah', 'crucifixion', 'shepherd'], ['nativity', 'david', 'resurrection', 'annunciation', 'supper']],+ [['resurrection', 'shepherd', 'crucifixion', 'david', 'nativity'], ['supper', 'annunciation', 'magi', 'noah', 'resurrection']],+ ];+ const grounds = [['ruby', 'cobalt'], ['cobalt', 'ruby'], ['emerald', 'ruby'], ['violet', 'cobalt']];+ return sets.slice(0, P.storyWindows).map((s, n) => makeWindowGlass(...windowArgs.aisle, 40 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetStory(g, x, w, y0, ys, r, s[i].slice(0, 4), { c1: grounds[n][0], c2: grounds[n][1], g1: grounds[(n + 1) % 4][1], g2: grounds[n][0], cell: 0.86 }),+ oculus: { ground: grounds[n][1], inner: grounds[n][0] },+ }));+}+const CLERE_SAINTS = [+ [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'book' }, { robe: 'emerald', mantle: 'gold', crown: 1, beard: 2, attr: 'cross', arm: 'point', halo: false }],+ [{ robe: 'pale', mantle: 'cobalt', veil: 'azure', arm: 'pray' }, { robe: 'violet', mantle: 'white', mitre: 'white', attr: 'staff', arm: 'up' }],+ [{ robe: 'gold', mantle: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'purple', beard: 1, attr: 'sword', arm: 'point' }],+ [{ robe: 'rubyLt', mantle: 'teal', crown: 1, attr: 'lily', arm: 'point', halo: false }, { robe: 'white', mantle: 'rubyLt', arm: 'orant', wings: 'gold' }],+];+function clereWindows() {+ return CLERE_SAINTS.slice(0, P.clerestoryWindows).map((pair, n) => makeWindowGlass(...windowArgs.clere, 60 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetSaint(g, x, w, y0, ys, r, pair[i], (i + n) % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }),+ oculus: { bust: { robe: 'rubyLt', beard: 1, cross: 1 }, ground: 'cobalt', inner: 'gold' },+ }));+}++function buildBody() {+ const { xTowerE, xNaveE, xc, xChoirW, xa, ridge } = LAY;+ const gNave = group('Nave'), gChoir = group('Choir'), gTN = group('Transept_North'), gTS = group('Transept_South'), gX = group('Crossing'), gApse = group('Apse_Chevet'), gRoof = group('Roofs');+ // The upper elevation is shared by every straight bay; aisles come in full and open-corner versions+ const bayUp = buildNaveBay({ part: 'upper' }), aisleFull = buildNaveBay({ part: 'aisle' }), aisleOpen = buildNaveBay({ part: 'aisle', open: true });+ const vault = buildVaultNave(), pier = buildPier(), but = buildButtress();+ const stories = storyWindows(), cleres = clereWindows();+ SHARED.stories = stories; SHARED.cleres = cleres;+ let counter = 0;+ const statue = (fr, lx, y, lz, kind, faceDeg, scale, parent, indoor = true) => placeFigure('Statue_' + kind + '_' + (counter++), kind, { pos: fr.pos(lx, y, lz), rot: fr.rot(faceDeg), scale, parent, indoor });+ const garg = (fr, lx, y, lz, k, sc, parent) => place('Gargoyle_' + (counter++), gargoyle(k), MAT.fine, { pos: fr.pos(lx, y, lz), rot: fr.rot(-90), scale: [sc, sc, sc], parent });++ // One side of a straight bay: module, vault-side glass, statues, gargoyles+ const placeBay = (name, fr, parent, o = {}) => {+ placeSet(name + '_Upper', bayUp, [fr.ox, 0, fr.oz], fr.th, parent);+ if (!o.noVault) placeSet(name + '_Aisle', o.open ? aisleOpen : aisleFull, [fr.ox, 0, fr.oz], fr.th, parent);+ const c = cleres[(o.i || 0) % cleres.length];+ placeGlass(name + '_Clerestory', c, fr.pos(0, 0, zn), fr.th + 180, parent);+ if (!o.open) {+ placeGlass(name + '_AisleWindow', stories[(o.i || 0) % stories.length], fr.pos(0, 0, zaw + 0.45), fr.th + 180, parent);+ statue(fr, 0, 0.7, zaw - 0.3, SAINTS[(o.i || 0) % SAINTS.length], 90, 0.86, parent);+ if (!o.inTower) garg(fr, 0, 22.5, zaw + 1.05, (o.i || 0) % 3, 0.72, parent);+ }+ if (!o.noUpperGarg) garg(fr, 0, P.eaves - 0.2, zn + 0.62, ((o.i || 0) + 1) % 3, 0.66, parent);+ };+ const placeButtress = (name, fr, parent, i) => {+ placeSet(name, but, [fr.ox, 0, fr.oz], fr.th, parent);+ statue(fr, 0, 23.5, 17.3, 'angel', -90, 1.9, parent, false);+ garg(fr, 0, 16.8, 18.55, i % 3, 1.0, parent);+ };+ const placePier = (name, lfr, parent, kind) => {+ placeSet(name, pier, [lfr.ox, 0, lfr.oz], lfr.th, parent);+ if (kind) statue(lfr, 0, 5.78, -1.14, kind, 90, 1.15, parent);+ };++ // ---- Two more bays carry the nave between the towers to the inner face of the west wall ----+ for (let t = 0; t < 2; t++) {+ const x = xTowerE + (t + 0.5) * BAY;+ placeBay('TowerBay' + t + '_N', frame(0, x, 0), gNave, { i: t + 2, noUpperGarg: true, inTower: true });+ placeBay('TowerBay' + t + '_S', frame(180, x, 0), gNave, { i: t, noUpperGarg: true, inTower: true });+ placeSet('TowerVault' + t, vault, [x, 0, 0], 0, gNave);+ if (t === 0) { placePier('TowerPier_N', frame(0, x + BAY / 2, zn), gNave, SAINTS[6]); placePier('TowerPier_S', frame(180, x + BAY / 2, -zn), gNave, SAINTS[7]); }+ }+ // ---- Nave ----+ for (let i = 0; i < P.naveBays; i++) {+ const x = xTowerE - (i + 0.5) * BAY, isOpen = i === P.naveBays - 1;+ placeBay('NaveBay' + i + '_N', frame(0, x, 0), gNave, { i, open: isOpen });+ placeBay('NaveBay' + i + '_S', frame(180, x, 0), gNave, { i: i + 2, open: isOpen });+ placeSet('NaveVault' + i, vault, [x, 0, 0], 0, gNave);+ }+ for (let k = 0; k <= P.naveBays - 1; k++) {+ const x = xTowerE - k * BAY;+ placePier('NavePier' + k + '_N', frame(0, x, zn), gNave, k ? SAINTS[k % SAINTS.length] : null);+ placePier('NavePier' + k + '_S', frame(180, x, -zn), gNave, k ? SAINTS[(k + 3) % SAINTS.length] : null);+ if (k >= 1 && k < P.naveBays - 1) { placeButtress('NaveButtress' + k + '_N', frame(0, x, 0), gNave, k); placeButtress('NaveButtress' + k + '_S', frame(180, x, 0), gNave, k + 1); }+ }+ // ---- Choir ----+ for (let j = 0; j < P.choirBays; j++) {+ const x = xChoirW - (j + 0.5) * BAY, isOpen = j === 0;+ placeBay('ChoirBay' + j + '_N', frame(0, x, 0), gChoir, { i: j + 1, open: isOpen });+ placeBay('ChoirBay' + j + '_S', frame(180, x, 0), gChoir, { i: j + 3, open: isOpen });+ placeSet('ChoirVault' + j, vault, [x, 0, 0], 0, gChoir);+ }+ for (let k = 1; k <= P.choirBays; k++) {+ const x = xChoirW - k * BAY;+ placePier('ChoirPier' + k + '_N', frame(0, x, zn), gChoir, SAINTS[(k + 5) % SAINTS.length]);+ placePier('ChoirPier' + k + '_S', frame(180, x, -zn), gChoir, SAINTS[(k + 1) % SAINTS.length]);+ placeButtress('ChoirButtress' + k + '_N', frame(0, x, 0), gChoir, k);+ placeButtress('ChoirButtress' + k + '_S', frame(180, x, 0), gChoir, k + 1);+ }+ // ---- Transept arms: the same bay, turned through 90 degrees ----+ for (const s of [1, -1]) {+ const gT = s > 0 ? gTN : gTS, tag = s > 0 ? 'N' : 'S';+ for (let q = 0; q < P.transeptBays + 1; q++) {+ const zc = s * (zn + (q + 0.5) * BAY), isOpen = q === 0;+ placeBay('Transept' + tag + q + '_W', frame(90 * s, xc, zc), gT, { i: q + 1, open: isOpen, noVault: isOpen });+ placeBay('Transept' + tag + q + '_E', frame(-90 * s, xc, zc), gT, { i: q + 3, open: isOpen, noVault: isOpen });+ placeSet('TranseptVault' + tag + q, vault, [xc, 0, zc], 90, gT);+ }+ for (let q = 1; q <= P.transeptBays + 1; q++) {+ const zz = s * (zn + q * BAY);+ placePier('TranseptPier' + tag + q + '_W', frame(90, xc + zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 2) % SAINTS.length] : null);+ placePier('TranseptPier' + tag + q + '_E', frame(-90, xc - zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 4) % SAINTS.length] : null);+ if (q === P.transeptBays) { placeButtress('TranseptButtress' + tag + '_W', frame(90 * s, xc, zz), gT, q); placeButtress('TranseptButtress' + tag + '_E', frame(-90 * s, xc, zz), gT, q + 1); }+ }+ }+ // ---- Crossing ----+ const xPier = buildCrossingPier(), aPier = buildAislePier(), xVault = mkBatches(); xVault.A.fn = bayShade; xVault.F.fn = bayShade;+ const sp = zn - 0.3;+ const crossCrown = addRibVault(xVault.F, xVault.A, [[-sp, -sp], [sp, -sp], [sp, sp], [-sp, sp]], P.vaultSpring, P.vaultCrown + 0.5, { sideRise: () => 12.9, nS: 12, nT: 10 });+ addBoss(xVault.G, xVault.F, crossCrown, 0.95);+ // Great transverse arches and the walls above them+ const arch = pointedArc(2 * (zn - 0.5), 12.9, 7);+ for (let i = 0; i < 4; i++) xVault.F.at(M.of(M.Ry(i * 90), M.T(0, P.vaultSpring, zn - 0.05)), () => xVault.F.sweep(arch, rollSection(0.9, 1.2, 2, 0.09, 3), { caps: false }));+ placeSet('CrossingVault', xVault, [xc, 0, 0], 0, gX);+ for (const [a, b] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {+ placeSet('CrossingPier', xPier, [xc + a * zn, 0, b * zn], 0, gX);+ placeSet('AngleAislePier', aPier, [xc + a * zaw, 0, b * zaw], 0, gX);+ }+ // ---- Chevet ----+ const sector = buildChevetSector();+ const apseSaints = [{ robe: 'gold', mantle: 'cobalt', crown: 1, veil: 'pale', arm: 'pray' }, { robe: 'white', mantle: 'ruby', beard: 2, attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'emerald', beard: 1, attr: 'book' }, { robe: 'white', mantle: 'rubyLt', beard: 1, cross: 1, arm: 'up' }];+ const hemi = apseSaints.slice(0, 3).map((f, i) => makeLancetGlass(2.05, P.triforiumTop + 0.45, P.vaultSpring + 4.8, 4.3, 70 + i, f, i % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }));+ const chapelGlass = ['annunciation', 'crucifixion'].map((nm, i) => { const g = new Glass(80 + i); g.raw(archOutline(1.3, 3.2, 9.9, 2.5, 7), 'lead', -0.4, 0); gLancetStory(g, 0, 1.3, 3.2, 9.9, 2.5, [nm, ['supper', 'magi'][i], ['magi', 'resurrection'][i]], { cell: 0.6, c1: i ? 'cobalt' : 'ruby', c2: i ? 'ruby' : 'cobalt' }); return g.b; });+ for (let k = 0; k < P.apseSides; k++) {+ const th = -(k + 0.5) * CHEV.d, fr = frame(th, xa, 0), gS = group('ChevetSector' + k, gApse);+ placeSet('Sector' + k, sector, [xa, 0, 0], th, gS);+ placeGlass('HemicycleWindow' + k, hemi[(k + (k > 3 ? 1 : 0)) % hemi.length], fr.pos(0, 0, CHEV.Ra), th + 180, gS);+ const nF = P.chapelFacets, dF = 216 / nF, ap = CHEV.Rc * Math.cos(rad(dF / 2));+ for (let i = 0; i < nF; i++) {+ const fm = -108 + (i + 0.5) * dF, lx = ap * Math.sin(rad(fm)), lz = CHEV.zc + ap * Math.cos(rad(fm));+ placeGlass('ChapelWindow' + k + '_' + i, chapelGlass[(i + k) % chapelGlass.length], fr.pos(lx, 0, lz + 0.0), th + fm + 180, gS);+ }+ statue(fr, 0, 0.5, CHEV.zc + CHEV.Rc * 0.55, k === 3 ? 'virgin' : SAINTS[k], 90, 0.92, gS);+ garg(frame(th - 10, xa, 0), 0, 15.5, CHEV.zc + 1.2, k % 3, 0.7, gS);+ if (k >= 1) { const bt = -k * CHEV.d; placeButtress('ChevetButtress' + k, frame(bt, xa, 0), gS, k); }+ }+ // ---- Roofs, ridge cresting, floor ----+ const Lr = new Batch(TILE.lead); Lr.fn = (x, y) => 0.84 + 0.16 * sstep(40, 54, y);+ const yE = P.eaves + 0.15, half = zn + 0.72;+ addRoofRun(Lr, xa, LAY.xW - 1.2, half, yE, ridge);+ Lr.at(M.T(xc, 0, 0), () => Lr.at(M.Ry(90), () => addRoofRun(Lr, -(zn + (P.transeptBays + 1) * BAY + 0.4), zn + (P.transeptBays + 1) * BAY + 0.4, half, yE, ridge)));+ place('MainRoof_Lead', Lr, MAT.lead, { parent: gRoof });+ const crest = buildCresting(BAY);+ const nCrest = Math.floor((LAY.xW - 2 - xa) / BAY);+ for (let i = 0; i < nCrest; i++) place('RidgeCresting' + i, crest, MAT.gold, { pos: [xa + (i + 0.5) * BAY, ridge, 0], parent: gRoof });+ for (const s of [1, -1]) for (let q = 0; q < P.transeptBays + 2; q++) { const zz = s * (0.5 + (q + 0.5) * BAY + 3); if (Math.abs(zz) < zn + (P.transeptBays + 1) * BAY) place('TranseptCresting' + (s > 0 ? 'N' : 'S') + q, crest, MAT.gold, { pos: [xc, ridge, zz], rot: [0, 90, 0], parent: gRoof }); }+ buildFleche(gRoof, xc, 0);+ const fl = new Batch(TILE.pave); fl.fn = floorShade;+ const R13 = zaw + 0.1;+ fl.face([[xa, 0, R13], [LAY.xW - WF.wallT + 0.1, 0, R13], [LAY.xW - WF.wallT + 0.1, 0, -R13], [xa, 0, -R13]]);+ const tz = zn + (P.transeptBays + 1) * BAY + 1;+ fl.face([[xc - R13, 0, tz], [xc + R13, 0, tz], [xc + R13, 0, -tz], [xc - R13, 0, -tz]]);+ for (let k = 0; k < P.apseSides; k++) { const th = rad(-(k + 0.5) * CHEV.d), f = frame(-(k + 0.5) * CHEV.d, xa, 0), rr = CHEV.zc + CHEV.Rc + 0.2, hh = rr * Math.tan(CHEV.h); fl.face([f.pos(0, 0, 0), f.pos(-hh, 0, rr), f.pos(hh, 0, rr)]); }+ place('Paving', fl, MAT.pave, { parent: ROOT });+ // ---- Stained light on the floor from the south aisle ----+ const xs = []; for (let i = 0; i < P.naveBays - 1; i++) xs.push(xTowerE - (i + 0.5) * BAY);+ place('StainedLight_Floor', buildLightPools(xs), MAT.pool, { parent: gNave });+}++// ==================================================================================+// THE GREAT ROSE: real stone tracery, and glass depicting Christ among apostles and angels+// ==================================================================================+const ROSE = { r0: 1.15, rs1: 2.35, r1: 3.05, rs2: 4.05, r2: 4.72, n1: 12, n2: 24 };+const pol = (r, a) => [r * Math.cos(a), r * Math.sin(a)];+function addRoseTracery(F, R) {+ const k = R / 5.5, heavy = traceryBar(0.3 * k, 0.5), mid = hexBar(0.2 * k, 0.42), thin = hexBar(0.14 * k, 0.34);+ const dia = [[0.07 * k, 0], [0, 0.17], [-0.07 * k, 0], [0, -0.17]]; // lightest bar, for the little roundels+ const { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE;+ F.sweep(ringPts(0, 0, R, 36), heavy, { closed: true });+ F.sweep(ringPts(0, 0, r0 * k, 14), mid, { closed: true });+ F.sweep(ringPts(0, 0, r2 * k, 36), mid, { closed: true });+ F.sweep(ringPts(0, 0, r1 * k, 24), mid, { closed: true });+ // Inner wheel: colonnette spokes, each light closed by a pointed trefoil head+ const head = (n, rs, re, a, bar) => {+ const da = PI / n * 0.98, pts = [], N = 6;+ for (let i = 0; i <= N; i++) { const t = -1 + 2 * i / N, h = Math.pow(1 - Math.pow(Math.abs(t), 1.5), 0.72); pts.push(pol((rs + (re - rs) * h) * k, a + t * da)); }+ F.sweep(pts, bar, { caps: false });+ return pts;+ };+ for (let i = 0; i < n1; i++) {+ const a = i * TAU / n1, am = a + PI / n1;+ F.sweep([pol(r0 * k, a), pol(rs1 * k, a)], mid, { caps: false });+ head(n1, rs1, r1 * 0.985, am, thin);+ // the cusp hanging inside the head+ const c = pol((rs1 + 0.34) * k, am); F.sweep(ringPts(c[0], c[1], 0.26 * k, 6), dia, { closed: true, s: 0.95, crease: 80 });+ }+ // Outer wheel of twenty-four lights+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, am = a + PI / n2;+ F.sweep([pol(r1 * k, a), pol(rs2 * k, a)], thin, { caps: false });+ head(n2, rs2, r2 * 0.99, am, thin);+ // Quatrefoil roundels in the outer band+ const c = pol((R / k + r2) / 2 * k, a);+ F.sweep(ringPts(c[0], c[1], 0.3 * k, 6), dia, { closed: true, crease: 80 });+ }+}+const SHARED = {};+// Rose tracery is one mesh, scaled for the smaller transept roses; bright outside, dim inside+function roseTracery() {+ if (!SHARED.rose) { const b = new Batch(TILE.fine); b.fn = (x, y, z, nx, ny, nz) => (nz < -0.25 ? 0.5 : 0.96); addRoseTracery(b, WF.roseR); SHARED.rose = b; }+ return SHARED.rose;+}+function largePinnacle() {+ if (!SHARED.pin) { const b = new Batch(TILE.fine); b.fn = (x, y, z, nx, ny) => (ny < -0.4 ? 0.6 : 1); addPinnacle(b, 2, 10, { lod: 1 }); SHARED.pin = b; }+ return SHARED.pin;+}+function makeRoseGlass(R, seed, o = {}) {+ const g = new Glass(seed), k = R / 5.5, { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE, inner = o.inner || ['ruby', 'cobalt'], outer = o.outer || ['cobalt', 'ruby', 'emerald'];+ g.raw(ringPts(0, 0, R + 0.08, 48), 'lead', -0.4, 0);+ // The band of roundels+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, b = (i + 1) * TAU / n2;+ g.poly([pol(r2 * k, a), pol(R, a), pol(R, b), pol(r2 * k, b)], i % 2 ? 'ruby' : 'cobalt', 0, { came: -0.02 });+ const c = pol((R / k + r2) / 2 * k, a); g.disc(c[0], c[1], 0.3 * k, i % 2 ? 'gold' : 'emerald', 0.6, 8, { came: 0.012 }); g.disc(c[0], c[1], 0.15 * k, 'white', 1, 6, { came: 0.008 });+ }+ // Outer lights: angels and elders turning about the centre+ for (let i = 0; i < n2; i++) {+ const am = (i + 0.5) * TAU / n2, da = PI / n2 * 0.97, col = outer[i % outer.length];+ const wedge = [pol(r1 * k, am - da), pol(r2 * k, am - da * 0.9), pol(r2 * k, am + da * 0.9), pol(r1 * k, am + da)];+ g.poly(wedge, col, 0, { came: -0.02 });+ g.withClip(wedge, () => g.withXf(am * 180 / PI - 90, ((r1 + 0.15) * k) * Math.cos(am), ((r1 + 0.15) * k) * Math.sin(am), k, () => {+ const f = i % 3;+ if (f === 0) gFigure(g, 0, 0, 1.28, { wings: 'gold', robe: 'white', mantle: 'rubyLt', arm: 'orant', hair: 'amber' });+ else if (f === 1) gBust(g, 0, 0.1, 1.2, { robe: 'emerald', beard: 1, crown: 1, halo: 'gold' });+ else gFigure(g, 0, 0, 1.26, { robe: 'gold', mantle: 'violet', beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'book' });+ }));+ }+ // Inner lights: the twelve apostles enthroned+ const scheme = o.figs || [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'key', arm: 'point' }, { robe: 'emerald', mantle: 'white', beard: 2, attr: 'book' }, { robe: 'gold', mantle: 'cobalt', beard: 1, attr: 'sword', arm: 'point' }, { robe: 'white', mantle: 'violet', attr: 'book' }];+ for (let i = 0; i < n1; i++) {+ const am = (i + 0.5) * TAU / n1, da = PI / n1 * 0.97, col = inner[i % inner.length];+ const wedge = [pol(r0 * k, am - da), pol(r1 * k, am - da * 0.92), pol(r1 * k, am + da * 0.92), pol(r0 * k, am + da)];+ g.poly(wedge, col, 0, { came: -0.022 });+ g.withClip(wedge, () => {+ // a mosaic ring behind each figure+ const mid = pol((r0 + r1) / 2 * k, am);+ gRim(g, mid[0], mid[1], 0.74 * k, 0.86 * k, ['gold', 'white', 'rubyLt'], 12, 0.8);+ g.withXf(am * 180 / PI - 90, (r0 + 0.2) * k * Math.cos(am), (r0 + 0.2) * k * Math.sin(am), k, () => gFigure(g, 0, 0, 1.56, Object.assign({ pose: 'sit' }, scheme[i % scheme.length])));+ });+ }+ // The centre: Christ in Majesty in a gilded mandorla, the four-square cross behind+ g.disc(0, 0, r0 * k + 0.06, 'lead', 2.6, 16, { came: 0, jitter: 0 });+ g.disc(0, 0, r0 * k, o.centre || 'cobalt', 3, 16, { came: 0 });+ g.withClip(ringPts(0, 0, r0 * k, 14), () => g.withXf(0, 0, -0.98 * k, k, () => {+ const m = []; for (let i = 0; i < 16; i++) { const t = i * TAU / 16; m.push([0.62 * Math.cos(t) * Math.pow(Math.abs(Math.sin(t)) * 0.3 + 0.7, 1), 1 + 0.96 * Math.sin(t)]); }+ g.poly(m, 'gold', 3.3, { came: 0.02 });+ g.poly(m.map((p) => [p[0] * 0.8, 1 + (p[1] - 1) * 0.88]), 'rubyLt', 3.5, { came: 0.012 });+ gFigure(g, 0, 0.22, 1.62, o.majesty || { pose: 'sit', robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'up', attr: null, layer: 4 });+ }));+ return g.b;+}++// ==================================================================================+// WEST FRONT. Local frame: x across the facade, y up, +z out towards the viewer, z=0 the wall.+// ==================================================================================+const WF = { halfW: 21.5, topY: 47, roseY: 31.8, roseR: 5.5, kingY: 20.2, kingTop: 25.3, galY: 40.6, galTop: 46.2, wallT: 0.8, portalC: { x: 0, w0: 4.3, orders: P.jambStatues, dx: 0.58, dz: 0.62, ys: 7.5 }, portalS: { x: 13.5, w0: 3.7, orders: P.jambStatues - 1, dx: 0.6, dz: 0.66, ys: 7.2 } };+function westShade(x, y, z, nx, ny, nz, b) {+ const pz = z + nz * 0.3;+ if (pz < -WF.wallT + 0.2 && y < 42 && Math.abs(x) < 13.3) { // the inner face of the west wall+ b.rgb = [1, 0.965, 0.915];+ let s = 0.38 + 0.3 * sstep(0, 34, y);+ if (ny < -0.45) s *= 0.8;+ return s;+ }+ b.rgb = [1, 0.99, 0.965];+ let s = lerp(0.72, 1, sstep(0, 6, y));+ if (ny < -0.4) s *= 0.55;+ return s;+}+function setShade(set, fn) { for (const k of ['A', 'F', 'C', 'O', 'I']) set[k].fn = fn; return set; }+// Aged oak door leaf with forged straps, studs and a ring. Hung at x=0, spans to x=w, h high, front +z.+function addDoorLeaf(set, x0, w, h, flip) {+ const { O, I } = set, planks = 6, pw = w / planks, d = flip ? -1 : 1;+ for (let i = 0; i < planks; i++) {+ const a = x0 + d * i * pw, c = x0 + d * (i + 0.97) * pw, xa = Math.min(a, c), xc = Math.max(a, c), tone = 0.46 + 0.16 * Math.abs(Math.sin(i * 2.4 + x0 * 3));+ O.at(M.Ry(0), () => O.box(xa, xc, 0.02, h, -0.05, 0.05, { s: tone }));+ }+ // Oak grain runs up each plank: remap planes with u across and v up is the default box projection+ const strapY = [0.72, h * 0.36, h * 0.64, h - 0.7];+ for (const y of strapY) {+ const reach = w * 0.94, xEnd = x0 + d * reach;+ I.shade(0.34, () => {+ I.box(Math.min(x0, xEnd), Math.max(x0, xEnd), y - 0.075, y + 0.075, 0.05, 0.085, {});+ // scrolled fleur at the strap end+ for (const s of [-1, 1]) I.tube(spline([[xEnd, y, 0.07], [xEnd + d * 0.12, y + s * 0.12, 0.07], [xEnd + d * 0.05, y + s * 0.24, 0.07], [xEnd - d * 0.07, y + s * 0.17, 0.07]], 2), (t) => lerp(0.03, 0.012, t), 4, {});+ for (let k = 0; k < 7; k++) { const sx = x0 + d * (k + 0.5) * reach / 7; I.box(sx - 0.025, sx + 0.025, y - 0.025, y + 0.025, 0.08, 0.115, { taper: 0.012 }); }+ });+ }+ // Ring handle+ I.shade(0.4, () => I.at(M.of(M.T(x0 + d * (w - 0.3), h * 0.42, 0.11), M.Ry(0)), () => {+ const rg = []; for (let i = 0; i <= 12; i++) { const t = i * TAU / 12; rg.push([0.1 * Math.cos(t), 0.1 * Math.sin(t) - 0.1, 0]); }+ I.tube(rg, 0.018, 5, { capEnd: false }); I.box(-0.05, 0.05, -0.05, 0.05, -0.05, 0, {});+ }));+}+function buildPortal(cfg, kinds, tymp) {+ const set = setShade(mkBatches(), westShade), { A, F, C, G, O, I } = set, { w0, orders: N, dx, dz, ys } = cfg;+ // Every order is concentric: the same two arc centres with the radius growing by one step+ const r0 = w0 * 0.86, c0 = (r0 * r0 - w0 * w0 / 4) / w0, wT = (i) => w0 + 2 * i * dx, zT = (i) => i * dz;+ const riseI = (i) => Math.sqrt(Math.pow(c0 + w0 / 2 + i * dx, 2) - c0 * c0);+ const riseOf = (w) => riseI((w - w0) / (2 * dx));+ const figs = [], zF = N * dz, outerW = wT(N), apex = ys + riseOf(outerW), doorH = ys - 0.9;+ // ---- Solid stepped jambs ----+ for (let i = 0; i < N; i++) {+ const depthS = lerp(0.62, 1, i / N);+ for (const s of [-1, 1]) {+ const xa = s * wT(i) / 2, xb = s * wT(i + 1) / 2;+ A.shade(depthS, () => A.box(Math.min(xa, xb), Math.max(xa, xb), 0, ys, -0.1, zT(i + 1), { noBot: true }));+ // Carved socle under the saints+ C.shade(depthS, () => { const mx = (xa + xb) / 2; C.at(M.T(0, 0, zT(i + 1) + 0.012), () => C.face([[Math.min(xa, xb) + 0.03, 0.15, 0], [Math.max(xa, xb) - 0.03, 0.15, 0], [Math.max(xa, xb) - 0.03, 1.25, 0], [Math.min(xa, xb) + 0.03, 1.25, 0]])); });+ // Colonnette on the arris, with a foliate capital; the saint stands against it+ const cx = s * (wT(i) / 2 + 0.02), cz = zT(i) + 0.32;+ F.shade(depthS, () => F.at(M.T(cx, 0, cz), () => {+ F.lathe([[0.2, 1.3], [0.2, 1.42], [0.11, 1.54], [0.11, ys - 0.5], [0.14, ys - 0.46], [0.11, ys - 0.42], [0.26, ys - 0.04], [0.3, ys]], 8);+ for (let l = 0; l < 4; l++) addLeaf(F, M.of(M.T(0, ys - 0.42, 0), M.Ry(l * 90 + 45), M.T(0.1, 0, 0)), 0.38, { coarse: 1 });+ }));+ // Pedestal and canopy for the jamb statue+ const sx = s * (wT(i) / 2 - 0.13), sz = zT(i) + 0.56;+ F.shade(depthS, () => F.at(M.T(sx, 0, sz), () => {+ F.lathe([[0.3, 1.25], [0.32, 1.32], [0.22, 1.4], [0.27, 1.46], [0.24, 1.5]], 6);+ F.at(M.T(0, 4.12, 0), () => {+ // A canopy like a tiny carved building: vaulted soffit, a drum of gablets, thin spirelet+ F.lathe([[0.06, -0.28], [0.34, -0.1], [0.44, 0], [0.44, 0.14], [0.38, 0.18], [0.22, 0.22], [0.22, 0.56], [0.28, 0.6], [0.2, 0.64], [0.09, 0.7], [0.01, 1.02]], 6, { phase: PI / 6, faceted: true });+ const face = s > 0 ? -60 : 60;+ for (const l of [-1, 0, 1]) F.at(M.of(M.Ry(face + l * 60), M.T(0, 0, 0.36)), () => {+ F.face([[-0.2, 0.14, 0.02], [0.2, 0.14, 0.02], [0, 0.5, 0.02]]);+ crocketsAlong(F, [-0.2, 0.16, 0.02], [0, 0.47, 0.02], 1, 0.12, [-1, 0.6, 0], 0);+ crocketsAlong(F, [0.2, 0.16, 0.02], [0, 0.47, 0.02], 1, 0.12, [1, 0.6, 0], 0);+ F.shade(0.5, () => F.face([[-0.1, 0.17, 0.03], [0.1, 0.17, 0.03], [0, 0.34, 0.03]]));+ });+ });+ }));+ const dirX = -s * dz, dirZ = dx * 1.5, l = Math.hypot(dirX, dirZ), th = Math.atan2(-dirZ / l, dirX / l) * 180 / PI;+ figs.push({ kind: kinds[(i + (s > 0 ? 0 : kinds.length >> 1)) % kinds.length], pos: [cfg.x + sx, 1.5, sz], rot: [0, th, 0], scale: 1.27 });+ }+ }+ // ---- Archivolts: moulded orders, each filled with a procession of small figures ----+ for (let i = 0; i < N; i++) {+ const w = wT(i), r = riseI(i), depthS = lerp(0.52, 1, i / N), arc = pointedArc(w, r, 8).map((p) => [p[0], p[1] + ys]);+ // A splayed face stepping out to the next order: this is the hollow the figures stand in+ F.shade(depthS * 0.82, () => F.sweep(arc, [[0, -0.1], [dx, -0.1], [dx, dz], [0, 0]], { z: zT(i), caps: false, crease: 30 }));+ // A roll along the outer edge of each order+ F.shade(depthS + 0.1, () => F.at(M.T(0, 0, zT(i + 1) - 0.02), () => F.sweep(pointedArc(wT(i + 1), riseI(i + 1), 8).map((p) => [p[0], p[1] + ys]), hexBar(0.2, 0.22), { caps: false })));+ // Procession of little figures up each side towards the apex+ const pts = pointedArc(w + dx, riseI(i + 0.5), 16).map((p) => [p[0], p[1] + ys]);+ let len = 0; const seg = []; for (let k = 0; k < pts.length - 1; k++) { const l = Math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]); seg.push(l); len += l; }+ const n = Math.max(6, Math.round(len / 1.32));+ for (let q = 0; q < n; q++) {+ const d = (q + 0.5) / n * len; let acc = 0, k = 0; while (k < seg.length - 1 && acc + seg[k] < d) { acc += seg[k]; k++; }+ const f = (d - acc) / seg[k], px = lerp(pts[k][0], pts[k + 1][0], f), py = lerp(pts[k][1], pts[k + 1][1], f);+ const tx0 = (pts[k + 1][0] - pts[k][0]) / seg[k], ty0 = (pts[k + 1][1] - pts[k][1]) / seg[k], rx = -ty0, ry = tx0; // rx,ry: away from the opening+ const up = d > len / 2 ? -1 : 1, Y = [tx0 * up, ty0 * up, 0];+ // faces out along the splay: towards the opening and the viewer+ const nl = Math.hypot(dz, dx), X = [-rx * dz / nl, -ry * dz / nl, dx / nl];+ const Z = [X[1] * Y[2] - X[2] * Y[1], X[2] * Y[0] - X[0] * Y[2], X[0] * Y[1] - X[1] * Y[0]];+ figs.push({ kind: ['prophet', 'kingA', 'angel', 'apostle'][(i + q) % 4], lod: 0, pos: [cfg.x + px + X[0] * 0.06, py + X[1] * 0.06, zT(i) + dz * 0.5 + X[2] * 0.06], basis: { X, Y, Z }, scale: 0.5 });+ }+ }+ // ---- Outer hood with gable and crockets ----+ const hood = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, zF), () => F.sweep(hood, rollSection(0.34, 0.44, 2, 0.05, 2), { caps: false }));+ // The gable springs from the arch springing; its field is a carved panel with the arch cut out of it+ const gw = outerW + 1.5, gy = ys, gTop = WF.kingY - 0.7, gH = gTop - gy;+ const archCut = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ const field = [[-gw / 2, gy], [-(outerW + 0.1) / 2, gy], ...archCut.slice(1, -1), [(outerW + 0.1) / 2, gy], [gw / 2, gy], [0, gTop]];+ C.at(M.T(0, 0, zF - 0.1), () => C.slab(field, [], -0.1, 0, { noOuterSides: true, noBack: true }));+ A.at(M.T(0, 0, zF - 0.2), () => A.slab(field, [], -zF + 0.3, 0, { noOuterSides: false, noFront: true, noBack: true, so: 0.7 }));+ F.at(M.T(0, 0, zF - 0.14), () => {+ addGable(F, gw, gH, 0, 0.24, gy, { size: 0.72, finial: 0.1, crockets: 6, noField: true, lod: 1 });+ // Trefoil in the gable field above the arch, standing proud of the carved diaper+ const yc = apex + (gTop - apex) * 0.36, rc = Math.min(0.92, (gTop - yc) * 0.42);+ F.sweep(ringPts(0, yc, rc, 16), traceryBar(0.18, 0.24), { closed: true, z: 0.04 });+ const foil = []; for (let i = 0; i < 15; i++) { const a = PI / 2 + i * TAU / 15, R = rc * (0.56 + 0.34 * Math.pow(Math.abs(Math.cos(a * 1.5 - PI * 0.75)), 0.8)); foil.push([R * Math.cos(a), yc + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.09, 0.18), { closed: true, z: 0.04, crease: 40 });+ });+ G.at(M.T(0, gTop + 0.1, zF - 0.02), () => addFinial(G, 1.5, 1));+ // ---- Tympanum, lintel, trumeau and doors ----+ const tw = w0 + 0.04, tr = riseOf(w0);+ C.at(M.T(0, 0, 0.06), () => C.slab(archOutline(tw, ys - 0.05, ys, tr, 9), [], -0.1, 0, { noOuterSides: true, noBack: true }));+ // Lintel with its frieze of small figures+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, ys - 0.02, -0.08, 0.22, { s: 0.9 });+ F.at(M.T(0, 0, 0.22), () => F.sweep([[-tw / 2 - 0.02, ys - 0.02], [tw / 2 + 0.02, ys - 0.02]], stringSection(0.14, 0.12), {}));+ const nL = Math.round(w0 / 0.62);+ for (let i = 0; i < nL; i++) figs.push({ kind: ['apostle', 'prophet', 'kingA'][i % 3], lod: 0, pos: [cfg.x + (-w0 / 2 + (i + 0.5) * w0 / nL), doorH + 0.03, 0.14], rot: [0, -90, 0], scale: 0.4 });+ // Central figure in a gilded mandorla, flanked by angels in registers+ // A true vesica: two arcs meeting in a point above and below+ const mY = ys + 0.06, mH = tr * 0.88, mW = tw * 0.25, mand = [], nM = 8;+ for (let i = 0; i < nM; i++) { const v = i / nM; mand.push([mW * Math.pow(Math.sin(PI * v), 0.72), mY + mH * v]); }+ for (let i = 0; i < nM; i++) { const v = 1 - i / nM; mand.push([-mW * Math.pow(Math.sin(PI * v), 0.72), mY + mH * v]); }+ G.at(M.T(0, 0, 0.1), () => G.sweep(mand, hexBar(0.1, 0.1), { closed: true, crease: 50 }));+ figs.push({ kind: tymp.main, pos: [cfg.x, ys + 0.14, 0.16], rot: [0, -90, 0], scale: Math.min(1.45, mH / 2.25) });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) {+ const xp = s * tw * (0.3 + i * 0.13), yp = ys + 0.1 + (i === 1 ? 0 : 0.05);+ figs.push({ kind: tymp.side[i % tymp.side.length], lod: 0, pos: [cfg.x + xp, yp, 0.14], rot: [0, -90, 0], scale: i === 0 ? 0.9 : 0.62 });+ }+ // Trumeau with its statue+ A.box(-0.32, 0.32, 0, doorH, -0.1, 0.34, { s: 0.85, noBot: true });+ F.at(M.T(0, 0, 0.5), () => {+ F.lathe([[0.42, 0], [0.42, 0.65], [0.28, 0.8], [0.34, 1.0], [0.32, 1.06]], 8, { s: 0.85 });+ F.at(M.T(0, doorH - 1.02, 0), () => F.lathe([[0.1, -0.36], [0.4, -0.1], [0.5, 0], [0.5, 0.16], [0.42, 0.2], [0.26, 0.24], [0.26, 0.62], [0.33, 0.66], [0.3, 0.7], [0.12, 0.74], [0.02, 1.0]], 6, { phase: PI / 6, faceted: true }));+ });+ figs.push({ kind: tymp.trumeau, pos: [cfg.x, 1.06, 0.5], rot: [0, -90, 0], scale: 1.5 });+ // Doors: two leaves either side of the trumeau, recessed in the wall+ const leaf = w0 / 2 - 0.34;+ O.at(M.T(0, 0, -0.14), () => { const set2 = set; I.at(M.T(0, 0, -0.14), () => { addDoorLeaf(set2, w0 / 2 - 0.02, leaf, doorH - 0.04, true); addDoorLeaf(set2, -w0 / 2 + 0.02, leaf, doorH - 0.04, false); }); });+ return { set, figs, apex, zF, outerW };+}+function placePortal(gWest, name, cfg, pr) {+ placeSet(name, pr.set, [cfg.x, 0, 0], 0, gWest);+ pr.figs.forEach((f, i) => {+ const rot = f.rot || eulerFromBasis(f.basis.X, f.basis.Y, f.basis.Z);+ const pos = [f.pos[0] + (cfg.x - pr.baseX), f.pos[1], f.pos[2]];+ placeFigure(name + '_Fig' + i, f.kind, { pos, rot, scale: f.scale, parent: gWest, lod: f.lod });+ });+}+function eulerFromBasis(X, Y, Z) { // degrees, three.js 'XYZ' order+ const m13 = clamp(Z[0], -1, 1), y = Math.asin(m13);+ let x, z;+ if (Math.abs(m13) < 0.9999) { x = Math.atan2(-Z[1], Z[2]); z = Math.atan2(-Y[0], X[0]); } else { x = Math.atan2(Y[2], Y[1]); z = 0; }+ return [x * 180 / PI, y * 180 / PI, z * 180 / PI];+}+// One niche of the Gallery of Kings, shared along the facade+function buildKingNiche(pitch) {+ const set = setShade(mkBatches(), westShade), { F, C, A } = set, h = WF.kingTop - WF.kingY, y0 = WF.kingY;+ C.shade(0.62, () => C.face([[-pitch / 2, y0, 0.02], [pitch / 2, y0, 0.02], [pitch / 2, y0 + h, 0.02], [-pitch / 2, y0 + h, 0.02]]));+ F.box(-pitch / 2, pitch / 2, y0 - 0.5, y0, 0, 1.3, { s: 0.9 });+ F.at(M.T(0, 0, 1.3), () => F.sweep([[-pitch / 2, y0 - 0.38], [pitch / 2, y0 - 0.38]], stringSection(0.3, 0.26), { caps: false }));+ // Colonnettes and a cusped arch with a gablet over each king+ const ys = y0 + 3.15, rise = 1.3;+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0.95), () => F.lathe([[0.17, y0], [0.09, y0 + 0.16], [0.09, ys - 0.2], [0.2, ys]], 8));+ F.at(M.T(0, 0, 0.95), () => {+ const arc = pointedArc(pitch - 0.1, rise, 6).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, rollSection(0.14, 0.36, 1, 0.03, 2), { caps: false });+ // solid spandrel+ A.at(M.T(0, 0, 0.95), () => A.slab([[-pitch / 2, ys], [-(pitch - 0.1) / 2, ys], ...arc, [(pitch - 0.1) / 2, ys], [pitch / 2, ys], [pitch / 2, y0 + h], [-pitch / 2, y0 + h]], [], -0.1, 0.1, { noOuterSides: true }));+ addGable(F, pitch * 0.96, 1.12, 0.1, 0.1, ys + 0.46, { size: 0.26, crockets: 2, finial: 0.34, solid: true, lod: 0 });+ });+ // The roof of the gallery behind the arcade+ F.box(-pitch / 2, pitch / 2, y0 + h - 0.02, y0 + h + 0.12, 0, 1.28, { s: 0.85 });+ return set;+}+// One bay of the open arcade above the rose and the tower windows+function buildGalleryBay(pitch) {+ const set = setShade(mkBatches(), westShade), { F, C, A } = set, y0 = WF.galY, y1 = WF.galTop, ys = y0 + 3.3;+ F.shade(0.4, () => F.face([[-pitch / 2, y0, 0.03], [pitch / 2, y0, 0.03], [pitch / 2, y1, 0.03], [-pitch / 2, y1, 0.03]]));+ const arc = pointedArc(pitch * 0.92, 1.5, 5).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, 0.5), () => {+ F.sweep(arc, traceryBar(0.16, 0.36), { caps: false });+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0), () => F.lathe([[0.2, y0], [0.1, y0 + 0.2], [0.1, ys - 0.22], [0.22, ys]], 8));+ F.sweep(ringPts(0, ys + 0.6, 0.26, 7), hexBar(0.07, 0.14), { closed: true });+ });+ A.at(M.T(0, 0, 0.5), () => A.slab([[-pitch / 2, ys], [-pitch * 0.46, ys], ...arc, [pitch * 0.46, ys], [pitch / 2, ys], [pitch / 2, y1], [-pitch / 2, y1]], [], -0.18, 0.18, { noOuterSides: true }));+ F.box(-pitch / 2, pitch / 2, y0 - 0.2, y0 + 0.02, 0, 0.72, { s: 0.92 });+ F.at(M.T(0, 0, 0.72), () => { F.sweep([[-pitch / 2, y0 - 0.1], [pitch / 2, y0 - 0.1]], stringSection(0.26, 0.22), { caps: false }); F.sweep([[-pitch / 2, y1], [pitch / 2, y1]], stringSection(0.3, 0.3), { caps: false }); });+ F.at(M.T(0, 0, 0.72), () => addParapet(F, -pitch / 2, pitch / 2, y1 + 0.12, 1.05, 0.2, { pitch: 1.1, nh: 8 }));+ return set;+}+// One face of a belfry stage, shared by all eight faces of the two towers+function buildBelfryFace() {+ const set = setShade(mkBatches(), westShade), { A, F, L } = set, hw = 7, zf = 7, y0 = WF.topY, yT = P.towerTop - 2.6;+ const lw = 3.05, lx = 3.3, sill = y0 + 4.2, ys = y0 + 13.6, rise = 4.1, holes = [];+ for (const s of [-1, 1]) holes.push(archOutline(lw, sill, ys, rise, 7).map((p) => [s * lx + p[0], p[1]]));+ A.at(M.T(0, 0, zf), () => A.slab([[-hw, y0], [hw, y0], [hw, yT], [-hw, yT]], holes, -0.9, 0, { noOuterSides: true, noBack: true, sr: 0.66 }));+ // Dark inside the bell chamber, louvres across the lower lights+ F.shade(0.12, () => F.face([[hw, y0, zf - 2.6], [-hw, y0, zf - 2.6], [-hw, yT, zf - 2.6], [hw, yT, zf - 2.6]].reverse()));+ for (const s of [-1, 1]) {+ F.at(M.T(s * lx, 0, zf - 0.45), () => {+ F.sweep(pointedArc(lw, rise, 6).map((p) => [p[0], p[1] + ys]), rollSection(0.2, 0.7, 1, 0.04, 2), { caps: false });+ F.sweep([[0, sill - 0.05], [0, ys + 0.1]], traceryBar(0.2, 0.42), {});+ const lw2 = (lw - 0.1) / 2;+ for (const t of [-1, 1]) F.sweep(pointedArc(lw2, lw2 * 0.9, 4).map((p) => [t * lw2 / 2 + p[0], ys + 0.1 + p[1]]), hexBar(0.14, 0.34), { caps: false });+ F.sweep(ringPts(0, ys + rise * 0.62, 0.48, 10), hexBar(0.14, 0.34), { closed: true });+ for (let k = 0; k < 7; k++) { const y = sill + 0.9 + k * 0.9; L.box(-lw / 2, lw / 2, y, y + 0.12, -0.3, 0.3, { taper: 0 }); }+ });+ F.at(M.T(s * lx, 0, zf), () => {+ F.sweep(pointedArc(lw + 0.5, rise + 0.3, 6).map((p) => [p[0], p[1] + ys]), hexBar(0.24, 0.28), { caps: false });+ addGable(F, lw + 0.9, 3.1, 0.03, 0.18, ys + rise * 0.5, { size: 0.42, finial: 0.8, crockets: 3, lod: 0 });+ });+ }+ F.at(M.T(0, 0, zf), () => {+ F.sweep([[-hw, y0 + 0.2], [hw, y0 + 0.2]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, sill - 0.3], [hw, sill - 0.3]], stringSection(0.32, 0.28), { caps: false });+ F.sweep([[-hw, yT - 0.2], [hw, yT - 0.2]], stringSection(0.4, 0.44), { caps: false });+ for (let i = 0; i < 12; i++) { const x = -hw + (i + 0.5) * 2 * hw / 12; F.box(x - 0.14, x + 0.14, yT - 0.78, yT - 0.42, 0, 0.3, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zf - 0.2), () => addParapet(F, -hw, hw, yT, 1.2, 0.28, { pitch: 1.4, nh: 8 }));+ return set;+}+// Octagonal corner turret and its great pinnacle, reaching the full height of the tower+function buildTowerCorner() {+ const set = setShade(mkBatches(), westShade), { A, F } = set, y0 = WF.topY, yT = P.towerTop - 1.3, R = 1.72;+ A.lathe([[R, y0 - 1], [R, yT]], 8, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < 8; i++) {+ const a = i * 45 + 22.5, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ F.at(M.of(M.Ry(a), M.T(0, 0, ap)), () => {+ if (i % 2 === 0) for (const k of [0, 1]) {+ const yb = y0 + 1 + k * 11;+ F.sweep(pointedArc(side * 0.72, 0.9, 4).map((p) => [p[0], p[1] + yb + 8.2]), hexBar(0.1, 0.12), { caps: false });+ for (const s of [-1, 1]) F.sweep([[s * side * 0.36, yb], [s * side * 0.36, yb + 8.2]], hexBar(0.1, 0.12), { caps: false });+ F.shade(0.55, () => F.face([[-side * 0.36, yb, 0.01], [side * 0.36, yb, 0.01], [side * 0.36, yb + 8.2, 0.01], [0, yb + 9.05, 0.01], [-side * 0.36, yb + 8.2, 0.01]]));+ }+ });+ }+ F.lathe([[R, y0 + 12.3], [R + 0.28, y0 + 12.5], [R + 0.28, y0 + 12.66], [R, y0 + 12.82]], 8, { faceted: true, phase: PI / 8 });+ F.lathe([[R, yT - 0.2], [R + 0.36, yT], [R + 0.36, yT + 0.26], [R, yT + 0.42]], 8, { faceted: true, phase: PI / 8 });+ return set;+}+function buildWestFront() {+ const gWest = group('West_Front');+ gWest.position.set(LAY.xW, 0, 0); gWest.rotation.y = rad(90);+ const set = setShade(mkBatches(), westShade), { A, F, C, L, G } = set, hw = WF.halfW, yTop = WF.topY, tcx = 14.5;+ // ---- The great wall with real openings: doors and rose ----+ const cP = WF.portalC, sP = WF.portalS;+ const doorHole = (x, w, h) => [[x - w / 2, 0.02], [x + w / 2, 0.02], [x + w / 2, h], [x - w / 2, h]];+ const wallOutline = [[-hw, 0], [-sP.x - sP.w0 / 2, 0], [-sP.x - sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, 0], [-cP.w0 / 2, 0], [-cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, 0], [sP.x - sP.w0 / 2, 0], [sP.x - sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]];+ A.slab(wallOutline, [ringPts(0, WF.roseY, WF.roseR, 40)], -WF.wallT, 0, { noOuterSides: true, sr: 0.62 });+ // ---- Rose: moulded ring, tracery, spandrels, relieving arch ----+ place('Great_Rose_Tracery', roseTracery(), MAT.fine, { pos: [0, WF.roseY, -0.4], parent: gWest });+ F.at(M.T(0, WF.roseY, 0), () => {+ F.at(M.T(0, 0, 0), () => {+ const ring = traceryBar(0.46, 0.58);+ F.sweep(ringPts(0, 0, WF.roseR + 0.32, 40), ring, { closed: true, z: 0.14 });+ F.sweep(ringPts(0, 0, WF.roseR + 0.78, 44), rollSection(0.2, 0.3, 1, 0.04, 2), { closed: true, z: 0.1 });+ const ndt = 32; for (let i = 0; i < ndt; i++) { const a = i * 360 / ndt; addCrocket(F, M.of(M.Rz(a), M.T(WF.roseR + 0.98, 0, 0.22)), 0.42, 0); }+ });+ });+ C.at(M.T(0, 0, 0.03), () => {+ // carved spandrels filling the square around the rose+ const R2 = WF.roseR + 0.72, ry = WF.roseY, sq = 6.6;+ for (const [sx, sy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {+ // corner of the square -> along its edge -> round the rose -> along the other edge+ const arcPts = []; for (let i = 0; i <= 8; i++) { const a = i / 8 * PI / 2; arcPts.push([sx * R2 * Math.cos(a), sy * R2 * Math.sin(a)]); }+ const poly = [[sx * sq, sy * sq], [sx * sq, 0], ...arcPts, [0, sy * sq]].map((p) => [p[0], p[1] + ry]);+ C.slab(area2(poly) > 0 ? poly : poly.slice().reverse(), [], -0.04, 0, { noOuterSides: true, noBack: true });+ }+ });+ F.at(M.T(0, 0, 0.14), () => { F.sweep([[-6.6, WF.roseY - 6.6], [6.6, WF.roseY - 6.6]], stringSection(0.3, 0.26), { caps: false }); F.sweep([[-6.6, WF.roseY + 6.6], [6.6, WF.roseY + 6.6]], stringSection(0.3, 0.26), { caps: false }); });+ // ---- Tower lower stages: paired windows with hood and gable, string courses ----+ const cw = WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = WIN.cRise;+ for (const s of [-1, 1]) {+ F.at(M.T(s * tcx, 0, 0.32), () => {+ addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 8), rollSection(0.24, 0.5, 1, 0.04, 2), { closed: true, crease: 40, z: -0.14 });+ F.sweep(pointedArc(cw + 0.8, cRise + 0.4, 7).map((p) => [p[0], p[1] + cSp]), hexBar(0.26, 0.3), { z: 0.1, caps: false });+ addGable(F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 4, lod: 0 });+ });+ F.at(M.T(s * tcx, 0, 0.02), () => { F.sweep([[-6.1, WF.kingTop + 0.24], [6.1, WF.kingTop + 0.24]], stringSection(0.3, 0.22), { caps: false }); });+ }+ // ---- Buttresses: stepped, with statues in tabernacles and pinnacled set-offs ----+ const xB = [-20.6, -7.5, 7.5, 20.6], xLow = [-20.3, -6.5, 6.5, 20.3];+ xLow.forEach((x, i) => {+ const hwb = 1.3;+ A.box(x - hwb, x + hwb, 0, 18.4, -0.02, 3.6, { noBot: true });+ A.face([[x - hwb, 18.4, 3.6], [x + hwb, 18.4, 3.6], [x + hwb, 19.95, 1.0], [x - hwb, 19.95, 1.0]]);+ F.at(M.T(x, 0, 3.6), () => {+ F.sweep([[-hwb - 0.1, 0.75], [hwb + 0.1, 0.75]], stringSection(0.5, 0.36), {});+ F.sweep([[-hwb - 0.04, 5.4], [hwb + 0.04, 5.4]], stringSection(0.26, 0.2), {});+ F.sweep([[-hwb - 0.04, 11.3], [hwb + 0.04, 11.3]], stringSection(0.26, 0.2), {});+ // statue niche in the lower stage+ addNicheFrame(F, 0, 6.1, 11.0, 1.42);+ addBlindArcade(F, -hwb + 0.1, hwb - 0.1, 1.2, 3.6, 2, { bar: 0.1, depth: 0.12 });+ });+ C.at(M.T(x, 0, 3.61), () => C.face([[-hwb + 0.06, 11.6, 0], [hwb - 0.06, 11.6, 0], [hwb - 0.06, 18.1, 0], [-hwb + 0.06, 18.1, 0]]));+ placeFigure('Buttress_Saint' + i, SAINTS[i + 3], { pos: [x, 6.2, 3.98], rot: [0, -90, 0], scale: 1.42, parent: gWest });+ place('Facade_Gargoyle' + i, gargoyle(i % 3), MAT.fine, { pos: [x, 17.0, 3.5], rot: [0, -90, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ });+ xB.forEach((x, i) => {+ const hwb = 0.92, z = 1.5;+ A.box(x - hwb, x + hwb, WF.kingTop + 0.05, yTop, -0.02, z, { noBot: true });+ F.at(M.T(x, 0, z), () => {+ F.sweep([[-hwb - 0.05, 30.0], [hwb + 0.05, 30.0]], stringSection(0.24, 0.2), {});+ addNicheFrame(F, 0, 31, 37.4, 1.3);+ F.sweep([[-hwb - 0.05, 38.6], [hwb + 0.05, 38.6]], stringSection(0.24, 0.2), {});+ });+ placeFigure('Upper_Saint' + i, SAINTS[i + 5], { pos: [x, 31.1, 1.86], rot: [0, -90, 0], scale: 1.32, parent: gWest });+ });+ placeSet('West_Wall', set, [0, 0, 0], 0, gWest);+ // ---- Glass ----+ const roseWest = makeRoseGlass(WF.roseR, 101);+ placeGlass('Great_Rose', roseWest, [0, WF.roseY, -0.4], 180, gWest);+ const twinGlass = SHARED.cleres;+ for (const s of [-1, 1]) place('TowerWindow_Out' + s, twinGlass[s > 0 ? 0 : 1], MAT.glassOut, { pos: [s * tcx, 0, 0.14], rot: [0, 180, 0], parent: gWest, scale: [1, 1, -1] });+ // ---- Three portals ----+ const pC = buildPortal(cP, ['peter', 'apostle', 'prophet', 'martyr', 'bishop', 'paul', 'prophet', 'apostle', 'bishop', 'martyr'], { main: 'christ', side: ['angel', 'prophet'], trumeau: 'christ' });+ const pS = buildPortal(Object.assign({}, sP, { x: 0 }), ['martyr', 'bishop', 'paul', 'prophet', 'apostle', 'peter', 'kingA', 'martyr'], { main: 'virgin', side: ['angel', 'apostle'], trumeau: 'virgin' });+ pC.baseX = cP.x; pS.baseX = 0; SHARED.sidePortal = pS;+ placePortal(gWest, 'Portal_Central', cP, pC);+ placePortal(gWest, 'Portal_South', Object.assign({}, sP, { x: -sP.x }), pS);+ placePortal(gWest, 'Portal_North', Object.assign({}, sP, { x: sP.x }), pS);+ // ---- Gallery of Kings ----+ const nK = P.kings, pitch = 2 * hw / nK, niche = buildKingNiche(pitch), kinds = ['kingA', 'kingB'];+ for (let i = 0; i < nK; i++) {+ const x = (i + 0.5 - nK / 2) * pitch;+ placeSet('King_Niche' + i, niche, [x, 0, 0], 0, gWest);+ placeFigure('King' + i, kinds[i % 2], { pos: [x, WF.kingY + 0.04, 0.5], rot: [0, -90, 0], scale: 1.5, parent: gWest });+ }+ // ---- High gallery ----+ const nG = 20, gp = 2 * hw / nG, gb = buildGalleryBay(gp);+ for (let i = 0; i < nG; i++) placeSet('Gallery' + i, gb, [(i + 0.5 - nG / 2) * gp, 0, 0], 0, gWest);+ // ---- Central gable rising over the gallery, closing the nave roof ----+ const front = setShade(mkBatches(), westShade), yG0 = yTop + 1.2, yR = LAY.ridge + 1.2, gW = 2 * (zn + 1.5);+ front.A.at(M.T(0, 0, -1.2), () => front.A.slab([[-gW / 2, yG0 - 2], [gW / 2, yG0 - 2], [gW / 2, yG0], [0, yR + 1.6], [-gW / 2, yG0]], [ringPts(0, yG0 + 1.8, 1.25, 12)], -0.5, 0.5, { sr: 0.5 }));+ front.F.at(M.T(0, 0, -0.7), () => {+ addGable(front.F, gW + 0.2, yR + 1.6 - yG0, 0.04, 0.26, yG0, { size: 0.86, finial: 0.1, crockets: 7, noField: true, lod: 1 });+ front.F.sweep(ringPts(0, yG0 + 1.8, 1.32, 14), traceryBar(0.26, 0.44), { closed: true });+ // Blind arcade stepping up under the rake+ const nA = 7; for (let i = 0; i < nA; i++) { const x = (i - (nA - 1) / 2) * 1.72, top = yG0 + 0.3 + (gW / 2 - Math.abs(x) - 1) * (yR + 1.6 - yG0) / (gW / 2) * 0.7; if (Math.abs(x) < 1.7) continue; front.F.sweep([[x - 0.62, yG0], [x - 0.62, top], [x, top + 0.75], [x + 0.62, top], [x + 0.62, yG0]], hexBar(0.14, 0.16), { caps: false, crease: 20 }); }+ });+ // Pinnacles crowning the upper buttresses either side of the gable+ for (const x of [-20.6, -7.5, 7.5, 20.6]) place('Facade_Pinnacle', largePinnacle(), MAT.fine, { pos: [x, yTop, 0.75], scale: [0.72, 0.74, 0.72], parent: gWest });+ front.G.at(M.T(0, yR + 1.62, -0.5), () => addFinial(front.G, 2.2, 1));+ placeSet('West_Gable', front, [0, 0, 0], 0, gWest);+ place('West_Gable_Glass', makeLancetRoundel(1.25), MAT.glassOut, { pos: [0, yG0 + 1.8, -0.75], parent: gWest });+ // ---- Towers: shell, belfry faces, corner turrets, lead cap ----+ // One tower shell (side and rear walls of the lower stages), built outward towards +x; the other tower mirrors it+ const shell = setShade(mkBatches(), (x, y, z, nx, ny, nz, b) => { b.rgb = [1, 0.99, 0.965]; let v = lerp(0.72, 1, sstep(0, 6, y)); if (ny < -0.4) v *= 0.55; return v; }), tw = 7;+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(180), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(-90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ // string courses and tall blind windows on the outer side+ shell.F.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => {+ for (const y of [1.0, 19.6, WF.kingTop, WF.galY]) shell.F.sweep([[-tw, y], [tw, y]], stringSection(0.3, 0.26), { caps: false });+ addTracery(shell.F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ shell.F.sweep(archOutline(cw, cSill, cSp, cRise, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ addGable(shell.F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 3, lod: 0 });+ addTracery(shell.F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 });+ shell.F.sweep(archOutline(...windowArgs.aisle, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ });+ shell.A.box(tw - 0.9, tw + 0.9, 0, yTop, -tw - 0.9, -tw + 0.9, {}); // outer angle buttress up the tower side+ for (const s of [-1, 1]) {+ const cx = s * tcx;+ placeSet('Tower_Shell', shell, [cx, 0, 0], 0, gWest, { scale: [s, 1, 1] });+ // Set just proud of the wall face: these towers are solid here, so the glass is seen from outside only+ place('Tower_SideGlass_High' + s, twinGlass[2], MAT.glassOut, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ place('Tower_SideGlass_Low' + s, SHARED.stories[1], MAT.glassOut, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ }+ const face = buildBelfryFace(), corner = buildTowerCorner();+ const cap = new Batch(TILE.lead), capG = new Batch(TILE.gold);+ cap.lathe([[5.9, P.towerTop - 2.5], [0.2, P.towerTop + 4.8]], 8, { faceted: true, phase: PI / 8 });+ capG.at(M.T(0, P.towerTop + 4.7, 0), () => addFinial(capG, 2.4, 1));+ for (const s of [-1, 1]) {+ const gT = group('Tower_' + (s > 0 ? 'North' : 'South'), gWest), cx = s * tcx, cz = -tw;+ for (let i = 0; i < 4; i++) {+ placeSet('Belfry_Face' + i, face, [cx, 0, cz], i * 90, gT);+ const ax = cx + (i === 0 || i === 3 ? 1 : -1) * tw, az = cz + (i < 2 ? 1 : -1) * tw;+ placeSet('Tower_Turret' + i, corner, [ax, 0, az], i * 90, gT);+ // the great corner pinnacles carry the tower to its full height+ const yT = P.towerTop - 1.3 + 0.4, hP = P.spireTip - yT;+ place('Tower_Pinnacle' + i, largePinnacle(), MAT.fine, { pos: [ax, yT, az], scale: [1.16, hP / 10, 1.16], parent: gT });+ }+ place('Tower_Cap_Lead', cap, MAT.lead, { pos: [cx, 0, cz], parent: gT });+ place('Tower_Cap_Gilt', capG, MAT.gold, { pos: [cx, 0, cz], parent: gT });+ }+ // ---- The parvis: a low paved forecourt that seats the front on the ground ----+ const parvis = new Batch(TILE.pave); parvis.fn = (x, y, z) => 0.62;+ parvis.face([[-hw - 2, 0.015, 5.2], [hw + 2, 0.015, 5.2], [hw + 2, 0.015, -0.1], [-hw - 2, 0.015, -0.1]]);+ place('Parvis_Paving', parvis, MAT.pave, { parent: gWest });+ // ---- Inner face: blind niches with statues, a gallery, the back of the doors ----+ const inner = setShade(mkBatches(), westShade);+ inner.F.at(M.of(M.T(0, 0, -WF.wallT), M.Ry(180)), () => {+ inner.F.sweep([[-13.1, 10.2], [13.1, 10.2]], stringSection(0.34, 0.34), { caps: false });+ inner.F.sweep([[-6.5, P.triforiumSill], [6.5, P.triforiumSill]], stringSection(0.34, 0.3), { caps: false });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) addNicheFrame(inner.F, s * (3.65 + i * 1.5), 3.2 + 0, 8.8, 1.2);+ inner.F.sweep(ringPts(0, WF.roseY, WF.roseR + 0.3, 32), traceryBar(0.42, 0.5), { closed: true });+ addBlindArcade(inner.F, -6.5, 6.5, 11, 15.2, 8, { bar: 0.14, depth: 0.2 });+ });+ placeSet('West_InnerFace', inner, [0, 0, 0], 0, gWest);+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) placeFigure('Inner_Saint_' + (s > 0 ? 'N' : 'S') + i, SAINTS[i + (s > 0 ? 1 : 6)], { pos: [s * (3.65 + i * 1.5), 3.25, -WF.wallT - 0.28], rot: [0, 90, 0], scale: 1.32, parent: gWest, indoor: true });+}+function makeLancetRoundel(r) { const g = new Glass(55); g.raw(ringPts(0, 0, r + 0.05, 20), 'lead', -0.4, 0); gOculus(g, 0, 0, r, { ground: 'cobalt', inner: 'gold' }); return g.b; }++// ---- Transept fronts: one portal, a smaller rose, lancets, gable, corner turrets ----------+function buildTransFront(gT, s) {+ const fr = frame(s > 0 ? 0 : 180, LAY.xc, 0), hw = zaw + 1.1, zEnd = zn + (P.transeptBays + 1) * BAY, yTop = P.eaves;+ const set = TRANS.set || (TRANS.set = (() => {+ const q = setShade(mkBatches(), (x, y, z, nx, ny, nz, b) => {+ if (z + nz * 0.3 < zEnd) { b.rgb = [1, 0.965, 0.915]; return (0.38 + 0.3 * sstep(0, 34, y)) * (ny < -0.45 ? 0.8 : 1); }+ b.rgb = [1, 0.99, 0.965]; let v = lerp(0.72, 1, sstep(0, 6, y)); if (ny < -0.4) v *= 0.55; return v;+ });+ const { A, F, C, G } = q, rY = 28.6, R = 4.5, w0 = WF.portalS.w0, dh = WF.portalS.ys - 0.9;+ const at = M.T(0, 0, zEnd);+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-hw, 0], [-w0 / 2, 0], [-w0 / 2, dh], [w0 / 2, dh], [w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]], [ringPts(0, rY, R, 32)], -0.9, 0, { noOuterSides: true, sr: 0.62 }));+ F.at(M.of(at, M.T(0, rY, 0.9)), () => { F.sweep(ringPts(0, 0, R + 0.3, 32), traceryBar(0.4, 0.5), { closed: true }); F.sweep(ringPts(0, 0, R + 0.72, 36), rollSection(0.18, 0.26, 1, 0.03, 2), { closed: true }); });+ // Row of lancets beneath the rose+ F.at(M.of(at, M.T(0, 0, 0.9)), () => {+ F.sweep([[-hw, 0.8], [hw, 0.8]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, 17.4], [hw, 17.4]], stringSection(0.34, 0.3), { caps: false });+ F.sweep([[-hw, 22.8], [hw, 22.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -8, 8, 18, 20.8, 8, { bar: 0.16, depth: 0.2 });+ for (const x of [-9.6, 9.6]) F.at(M.T(x, 0, 0.1), () => { addTracery(F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 }); F.sweep(archOutline(...windowArgs.aisle, 7), hexBar(0.22, 0.42), { closed: true, crease: 40, z: -0.1 }); });+ });+ // The inner face of the end wall: tiers of blind arcading and string courses+ F.at(M.of(at, M.Ry(180)), () => {+ F.sweep([[-zaw, 11], [zaw, 11]], stringSection(0.3, 0.26), { caps: false });+ F.sweep([[-zaw, 16.8], [zaw, 16.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -5.6, 5.6, 1.2, 7.8, 5, { bar: 0.16, depth: 0.2 });+ addBlindArcade(F, -zn, zn, 11.2, 14.8, 8, { bar: 0.12, depth: 0.16 });+ addBlindArcade(F, -zn, zn, 17, 20.6, 8, { bar: 0.12, depth: 0.16 });+ });+ // Gable closing the arm's roof, in front of the lead+ const yG = yTop, gW = 2 * (zn + 1.6), yR = LAY.ridge + 1;+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-gW / 2, yG], [gW / 2, yG], [0, yR]], [ringPts(0, yG + 3.4, 1.5, 12)], -0.9, 0, { noOuterSides: true, sr: 0.5 }));+ F.at(M.of(at, M.T(0, 0, 0.9)), () => { addGable(F, gW + 0.2, yR - yG, 0.02, 0.24, yG, { size: 0.82, finial: 0.1, crockets: 7, noField: true, lod: 1 }); F.sweep(ringPts(0, yG + 3.4, 1.55, 14), traceryBar(0.24, 0.42), { closed: true }); });+ G.at(M.of(at, M.T(0, yR, 1.12)), () => addFinial(G, 2.0, 1));+ F.at(M.of(at, M.T(0, 0, 1.0)), () => addParapet(F, -hw, hw, yTop - 0.1, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Corner buttresses rising to pinnacles+ for (const sx of [-1, 1]) {+ A.box(sx * hw - 1.15, sx * hw + 1.15, 0, yTop + 1, zEnd - 1, zEnd + 2.3, { noBot: true });+ F.at(M.T(sx * hw, 0, zEnd + 2.3), () => { F.sweep([[-1.25, 0.8], [1.25, 0.8]], stringSection(0.5, 0.36), {}); addNicheFrame(F, 0, 16, 22, 1.5); });+ }+ return q;+ })());+ placeSet('Transept_Front', set, [fr.ox, 0, fr.oz], fr.th, gT);+ // Portal (the same carving as the west side portals), statue niches, glass+ const gPortal = group('Transept_Portal', gT);+ gPortal.position.set(...fr.pos(0, 0, zEnd + 0.9)); gPortal.rotation.y = rad(fr.th);+ placePortal(gPortal, 'Portal_Transept', { x: 0 }, SHARED.sidePortal);+ place('Transept_Rose_Tracery', roseTracery(), MAT.fine, { pos: fr.pos(0, 28.6, zEnd + 0.45), rot: fr.rot(0), scale: [4.5 / WF.roseR, 4.5 / WF.roseR, 1], parent: gT });+ for (const sx of [-1, 1]) place('Transept_Pinnacle', largePinnacle(), MAT.fine, { pos: fr.pos(sx * hw, yTop + 1, zEnd + 0.9), scale: [1.05, 1.1, 1.05], parent: gT });+ const rose = TRANS.rose || (TRANS.rose = makeRoseGlass(4.5, 103, { inner: ['cobalt', 'emerald'], outer: ['ruby', 'cobalt', 'violet'], centre: 'ruby', majesty: { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', crown: 1, arm: 'hold', layer: 4 } }));+ placeGlass('Transept_Rose', rose, fr.pos(0, 28.6, zEnd + 0.45), fr.th + 180, gT);+ const low = SHARED.stories[0];+ for (const sx of [-1, 1]) {+ // Aisle-end windows: the same panes on the inner and outer faces of the end wall+ place('Transept_EndWindow_In', low, MAT.glassIn, { pos: fr.pos(sx * 9.6, 0, zEnd - 0.01), rot: fr.rot(180), parent: gT });+ place('Transept_EndWindow_Out', low, MAT.glassOut, { pos: fr.pos(sx * 9.6, 0, zEnd + 0.92), rot: fr.rot(180), scale: [1, 1, -1], parent: gT });+ placeFigure('Transept_Corner_Saint' + sx, SAINTS[sx > 0 ? 2 : 4], { pos: fr.pos(sx * hw, 16.05, zEnd + 2.62), rot: fr.rot(-90), scale: 1.42, parent: gT });+ }+}+const TRANS = {};++// ---- The east end: raised sanctuary, high altar with a gilded retable, oak choir stalls ------+function buildSanctuary() {+ const g = group('Sanctuary'), set = mkBatches(), { F, G, O } = set, { xa, xChoirW } = LAY;+ const dim = (x, y, z, nx, ny, nz, b) => { b.rgb = [1, 0.965, 0.915]; return (ny > 0.6 ? 0.62 : 0.5) * (ny < -0.4 ? 0.7 : 1); };+ F.fn = dim; O.fn = dim;+ // Three broad steps and the altar, all moulded+ for (let i = 0; i < 3; i++) { const r = 5 - i * 0.7; F.at(M.T(xa + 1, 0, 0), () => F.lathe([[r, i * 0.2], [r, (i + 1) * 0.2], [0.01, (i + 1) * 0.2]], 14, { faceted: true, phase: PI / 14 })); }+ const y0 = 0.6, ax = xa + 1;+ F.at(M.T(ax, y0, 0), () => {+ F.box(-0.8, 0.8, 0, 1.0, -1.7, 1.7, {});+ F.box(-0.92, 0.92, 1.0, 1.12, -1.84, 1.84, { sb: 0.5 });+ for (const s of [-1, 1]) for (const t of [-1, 0, 1]) F.at(M.T(s * 0.82, 0, t * 1.1), () => F.lathe([[0.12, 0], [0.07, 0.12], [0.07, 0.88], [0.13, 1.0]], 6));+ });+ // Gilded retable: an arcaded screen with gables, on the east side of the altar+ G.at(M.of(M.T(ax - 1.0, y0 + 1.12, 0), M.Ry(90)), () => {+ G.box(-2.3, 2.3, 0, 1.5, -0.12, 0.12, {});+ for (let i = 0; i < 5; i++) { const x = (i - 2) * 0.92; G.sweep(pointedArc(0.78, 0.6, 3).map((p) => [x + p[0], 0.65 + p[1]]), hexBar(0.07, 0.1), { z: 0.13, caps: false }); addGable(G, 0.88, 0.8, 0.08, 0.04, 1.42, { size: 0.16, crockets: 2, finial: 0.26, solid: true, lod: 0 }); }+ G.at(M.T(0, 2.2, 0), () => { G.box(-0.07, 0.07, 0, 2.1, -0.07, 0.07, {}); G.box(-0.62, 0.62, 1.3, 1.44, -0.07, 0.07, {}); });+ });+ // Candlesticks+ for (const t of [-1.5, -0.9, -0.3, 0.3, 0.9, 1.5]) G.at(M.T(ax - 0.2, y0 + 1.12, t), () => { G.lathe([[0.1, 0], [0.04, 0.06], [0.025, 0.42], [0.06, 0.46], [0.03, 0.5]], 6); F.at(M.T(ax - 0.2, y0 + 1.62, t), () => F.lathe([[0.022, 0], [0.022, 0.34], [0.002, 0.36]], 5, { s: 1.6 })); });+ // Choir stalls: stepped oak benches, moulded backs and a carved canopy rail+ const x0 = xa + 5.5, x1 = xChoirW - 1.2;+ for (const s of [-1, 1]) {+ const z0 = s * 5.0;+ O.box(x0, x1, 0, 0.32, z0 - 0.9, z0 + 0.9, { s: 0.42 });+ O.box(x0, x1, 0.32, 0.5, z0 + (s > 0 ? 0.0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.46 });+ O.box(x0, x1, 0, 2.7, z0 + (s > 0 ? 0.8 : -0.9), z0 + (s > 0 ? 0.9 : -0.8), { s: 0.4 });+ const n = Math.floor((x1 - x0) / 0.85);+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * (x1 - x0) / n;+ O.box(x - 0.045, x + 0.045, 0, 0.98, z0 + (s > 0 ? 0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.5 });+ O.box(x - 0.03, x + 0.03, 0.98, 2.72, z0 + (s > 0 ? 0.74 : -0.8), z0 + (s > 0 ? 0.8 : -0.74), { s: 0.48 });+ O.box(x - 0.08, x + 0.08, 2.72, 3.28, z0 + (s > 0 ? 0.56 : -0.86), z0 + (s > 0 ? 0.86 : -0.56), { s: 0.46, taper: 0.05 });+ }+ O.box(x0, x1, 2.7, 2.84, z0 + (s > 0 ? 0.3 : -0.9), z0 + (s > 0 ? 0.9 : -0.3), { s: 0.44 });+ }+ placeSet('Sanctuary', set, [0, 0, 0], 0, g);+}++function buildCathedral() {+ buildBody();+ buildWestFront();+ const gTN = ROOT.children.find((c) => c.name === 'Transept_North'), gTS = ROOT.children.find((c) => c.name === 'Transept_South');+ buildTransFront(gTN, 1);+ buildTransFront(gTS, -1);+ buildSanctuary();+}++function previewStatues() {+ const names = Object.keys(FIGURES);+ names.forEach((n, i) => placeFigure('Fig_' + n, n, { pos: [0, 0, (i - names.length / 2) * 0.95] }));+ placeFigure('FigLow', 'apostle', { lod: 0, pos: [1.6, 0, 0] });+ [0, 1, 2].forEach((k) => place('Garg' + k, gargoyle(k), MAT.fine, { pos: [0, 3, (k - 1) * 2] }));+ const pin = new Batch(TILE.fine); addPinnacle(pin, 1.1, 7); place('Pin', pin, MAT.fine, { pos: [-3, 0, 0] });+ const base = new Batch(TILE.ashlar); base.box(-6, 3, -0.4, 0, -8, 8, { s: 0.8 }); place('Ground', base, MAT.ashlar);+}++// <<ORNAMENT>>++// <<BUILD>>+async function build() {+ ROOT = createRoot('Cathedral');+ await makeMaterials();+ if (P.preview === 'statues') { previewStatues(); return ROOT; }+ if (P.preview === 'all' || P.preview === 'unique') { buildCathedral(); return ROOT; }+ if (P.preview === 'textures') { // measures the texture payload on its own+ Object.keys(MAT).forEach((k, i) => createPart('Swatch_' + k, boxGeo(1, 1, 1), MAT[k], { position: [i * 1.2, 0.5, 0], parent: ROOT }));+ return ROOT;+ }+ if (P.preview === 'stats') {+ buildCathedral();+ const list = [...STAT.entries()].map(([b, e]) => ({ name: e.name, n: e.n, tris: b.I.length / 3, kb: Math.round(batchBytes(b) / 1024) })).sort((a, c) => c.kb - a.kb);+ let total = 0, draws = 0, rendered = 0; for (const e of list) { total += e.kb; draws += e.n; rendered += e.n * e.tris; }+ createPart('StatProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ group('STAT_TOTAL_kb_' + total + '_draws_' + draws + '_rendered_' + rendered + '_unique_' + list.length);+ list.slice(0, 36).forEach((e, i) => group('STAT_' + String(i).padStart(2, '0') + '_' + e.name + '_x' + e.n + '_tris' + e.tris + '_kb' + e.kb));+ return ROOT;+ }+ return ROOT;+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-2.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/cathedral-2.kiln.jsnew file mode 100644index 00000000..1f1377b2--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/cathedral-2.kiln.js@@ -0,0 +1,3775 @@+// Gothic cathedral: parametric High Gothic church, true scale, metres.+// +X = west front (entrance) , -X = apse, +Y up, +Z = north.+const meta = { name: 'Gothic Cathedral', role: 'wonder' };++// ==================================================================================+// PARAMETERS: change these and the whole design follows.+// ==================================================================================+const P = {+ preview: 'all', // 'all', or 'size' (true buffer bytes, fast), 'stats' (per-mesh list), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)+ // Plan+ naveBays: 9, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,+ xWest: 62, // west face of the towers+ towerDepth: 14, towerWidth: 14,+ choirBays: 3, transeptBays: 2,+ apseSides: 7,+ // Elevation+ pierTop: 15, aisleCrown: 21, triforiumSill: 21.8, triforiumTop: 26.6,+ vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,+ aisleWallTop: 23, aisleRoofHigh: 26.2,+ towerTop: 72, spireTip: 80,+ flecheTip: 74,+ // Detail+ jambStatues: 5, kings: 16, seed: 1163,+ archFigureTris: 720, statueTest: ['apostle', 'bishop', 'king', 'queen', 'angel', 'virgin'],+ storyWindows: 3, clerestoryWindows: 3, // unique window cartoons (more = richer, larger GLB)+ // Glass+ glassGlow: 1.05, glassOuterGlow: 0.12, sunShear: [0.34, 0.86],+ sun: [0.62, 0.45, -0.64], lightVariantBytes: 250000, wallTessellation: 5, // baked low sun from the south-west; bytes allowed for per-side vertex light; metres between lit wall vertices+ chapelFacets: 5,+};++// ==================================================================================+// MATH+// ==================================================================================+const PI = Math.PI, TAU = PI * 2, rad = (d) => d * PI / 180;+const lerp = (a, b, t) => a + (b - a) * t;+const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));+const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };+const gauss = (x, y = 0) => Math.exp(-(x * x + y * y));+function mkRng(seed) { let s = seed >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }+function keyLin(keys, t) {+ if (t <= keys[0][0]) return keys[0].slice(1);+ for (let i = 1; i < keys.length; i++) if (t <= keys[i][0]) {+ const a = keys[i - 1], b = keys[i], f = (t - a[0]) / (b[0] - a[0]);+ return a.slice(1).map((v, k) => lerp(v, b[k + 1], f));+ }+ return keys[keys.length - 1].slice(1);+}+function keyf(keys, t, w = 0.03) { // smoothed keyframes [[t,v...],...]: no plateaus or kinks at the keys+ const a = keyLin(keys, t - w), b = keyLin(keys, t), c = keyLin(keys, t + w);+ return b.map((v, k) => (a[k] + 2 * v + c[k]) / 4);+}+const M = {+ I: () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],+ T: (x, y, z) => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z],+ S: (x, y = x, z = x) => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0],+ Rx: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0]; },+ Ry: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0]; },+ Rz: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0]; },+ mul: (a, b) => [+ a[0] * b[0] + a[1] * b[4] + a[2] * b[8], a[0] * b[1] + a[1] * b[5] + a[2] * b[9], a[0] * b[2] + a[1] * b[6] + a[2] * b[10], a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3],+ a[4] * b[0] + a[5] * b[4] + a[6] * b[8], a[4] * b[1] + a[5] * b[5] + a[6] * b[9], a[4] * b[2] + a[5] * b[6] + a[6] * b[10], a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7],+ a[8] * b[0] + a[9] * b[4] + a[10] * b[8], a[8] * b[1] + a[9] * b[5] + a[10] * b[9], a[8] * b[2] + a[9] * b[6] + a[10] * b[10], a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11],+ ],+ of: (...ms) => ms.reduce((a, b) => M.mul(a, b)),+};+function normalMat(m) { // inverse transpose of the 3x3, up to scale (cofactors)+ return [+ m[5] * m[10] - m[6] * m[9], m[6] * m[8] - m[4] * m[10], m[4] * m[9] - m[5] * m[8],+ m[2] * m[9] - m[1] * m[10], m[0] * m[10] - m[2] * m[8], m[1] * m[8] - m[0] * m[9],+ m[1] * m[6] - m[2] * m[5], m[2] * m[4] - m[0] * m[6], m[0] * m[5] - m[1] * m[4],+ ];+}+const det3 = (m) => m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8]) + m[2] * (m[4] * m[9] - m[5] * m[8]);++// ==================================================================================+// MESH KIT: one Batch = one mesh with one material; metric UVs, baked shading in COLOR_0+// ==================================================================================+class Batch {+ constructor(tile = 1) { this.P = []; this.N = []; this.UV = []; this.C = []; this.I = []; this.tile = tile; this.setM(M.I()); this.sh = 1; this.rgb = [1, 1, 1]; this.geo = null; }+ setM(m) { this.m = m; this.nm = normalMat(m); this.det = det3(m); }+ at(m, f) { const old = this.m; this.setM(M.mul(old, m)); f(this); this.setM(old); return this; }+ shade(s, f) { const o = this.sh; this.sh = o * s; f(this); this.sh = o; return this; }+ tint(rgb, f) { const o = this.rgb; this.rgb = rgb; f(this); this.rgb = o; return this; }+ get count() { return this.P.length / 3; }+ v(x, y, z, nx, ny, nz, u, v, s = 1) {+ const m = this.m, n = this.nm;+ const wx = m[0] * x + m[1] * y + m[2] * z + m[3], wy = m[4] * x + m[5] * y + m[6] * z + m[7], wz = m[8] * x + m[9] * y + m[10] * z + m[11];+ let ax = n[0] * nx + n[1] * ny + n[2] * nz, ay = n[3] * nx + n[4] * ny + n[5] * nz, az = n[6] * nx + n[7] * ny + n[8] * nz;+ const l = Math.hypot(ax, ay, az);+ if (l < 1e-10) { ax = 0; ay = 1; az = 0; } else { ax /= l; ay /= l; az /= l; }+ if (this.det < 0) { ax = -ax; ay = -ay; az = -az; }+ if (u === undefined || u === null) {+ const fx = Math.abs(ax), fy = Math.abs(ay), fz = Math.abs(az);+ if (fy > 0.75) { u = wx; v = wz; } else if (fx > fz) { u = wz; v = wy; } else { u = wx; v = wy; }+ }+ const sh = clamp(s * this.sh); // all light is added later by bakeLight+ const rgb = this.rgb;+ this.P.push(wx, wy, wz); this.N.push(ax, ay, az); this.UV.push(u / this.tile, v / this.tile);+ this.C.push(Math.round(255 * sh * rgb[0]), Math.round(255 * sh * rgb[1]), Math.round(255 * sh * rgb[2]));+ return this.P.length / 3 - 1;+ }+ tri(a, b, c) { if (this.det < 0) this.I.push(a, c, b); else this.I.push(a, b, c); }+ quad(a, b, c, d) { this.tri(a, b, c); this.tri(a, c, d); }+ // flat polygon from 3D points (CCW seen from the front); convex fan+ face(pts, s = 1) {+ const a = pts[0], b = pts[1], c = pts[pts.length - 1];+ let nx = (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), ny = (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), nz = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ if (pts.length > 3 && Math.hypot(nx, ny, nz) < 1e-9) { const d = pts[2]; nx = (b[1] - a[1]) * (d[2] - a[2]) - (b[2] - a[2]) * (d[1] - a[1]); ny = (b[2] - a[2]) * (d[0] - a[0]) - (b[0] - a[0]) * (d[2] - a[2]); nz = (b[0] - a[0]) * (d[1] - a[1]) - (b[1] - a[1]) * (d[0] - a[0]); }+ const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l;+ const ids = pts.map((p) => this.v(p[0], p[1], p[2], nx, ny, nz, undefined, undefined, p[3] === undefined ? s : p[3]));+ for (let i = 1; i < ids.length - 1; i++) this.tri(ids[0], ids[i], ids[i + 1]);+ return this;+ }+ box(x0, x1, y0, y1, z0, z1, o = {}) {+ const s = o.s === undefined ? 1 : o.s, sb = o.sb === undefined ? s * 0.62 : o.sb, st = o.st === undefined ? s : o.st;+ const tx = o.taper || 0;+ const X0 = x0 + tx, X1 = x1 - tx, Z0 = z0 + (o.taperZ === undefined ? tx : o.taperZ), Z1 = z1 - (o.taperZ === undefined ? tx : o.taperZ);+ this.face([[x1, y0, z1, s], [x1, y0, z0, s], [X1, y1, Z0, st], [X1, y1, Z1, st]]); // +X+ this.face([[x0, y0, z0, s], [x0, y0, z1, s], [X0, y1, Z1, st], [X0, y1, Z0, st]]); // -X+ this.face([[x0, y0, z1, s], [x1, y0, z1, s], [X1, y1, Z1, st], [X0, y1, Z1, st]]); // +Z+ this.face([[x1, y0, z0, s], [x0, y0, z0, s], [X0, y1, Z0, st], [X1, y1, Z0, st]]); // -Z+ if (!o.noTop) this.face([[X0, y1, Z1, st], [X1, y1, Z1, st], [X1, y1, Z0, st], [X0, y1, Z0, st]]);+ if (!o.noBot) this.face([[x0, y0, z0, sb], [x1, y0, z0, sb], [x1, y0, z1, sb], [x0, y0, z1, sb]]);+ return this;+ }+ // Surface of revolution about Y. prof: [[r,y],...] bottom to top. o.sides, o.faceted, o.crease, o.flat+ lathe(prof, n = 12, o = {}) {+ const a0 = o.phase || 0, cr = Math.cos(rad(o.crease === undefined ? 55 : o.crease));+ const s = o.s === undefined ? 1 : o.s;+ const segs = [];+ for (let j = 0; j < prof.length - 1; j++) {+ const dr = prof[j + 1][0] - prof[j][0], dy = prof[j + 1][1] - prof[j][1], l = Math.hypot(dr, dy) || 1;+ segs.push([dy / l, -dr / l, l]);+ }+ if (o.faceted) { // hard-edged polygon of revolution+ for (let i = 0; i < n; i++) {+ const t0 = a0 + TAU * i / n, t1 = a0 + TAU * (i + 1) / n, tm = (t0 + t1) / 2;+ const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1), cm = Math.cos(tm), sm = Math.sin(tm);+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const p0 = prof[j], p1 = prof[j + 1];+ const w0 = p0[0] * Math.hypot(c1 - c0, s1 - s0), w1 = p1[0] * Math.hypot(c1 - c0, s1 - s0);+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ const a = this.v(p0[0] * c0, p0[1], p0[0] * s0, nr * cm, ny, nr * sm, -w0 / 2, vv, sA);+ const b = this.v(p1[0] * c0, p1[1], p1[0] * s0, nr * cm, ny, nr * sm, -w1 / 2, vv + l, sB);+ const c = this.v(p1[0] * c1, p1[1], p1[0] * s1, nr * cm, ny, nr * sm, w1 / 2, vv + l, sB);+ const d = this.v(p0[0] * c1, p0[1], p0[0] * s1, nr * cm, ny, nr * sm, w0 / 2, vv, sA);+ this.quad(a, b, c, d); vv += l;+ }+ }+ return this;+ }+ // smooth around, creased along the profile when the section turns sharply+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const prev = segs[j - 1], next = segs[j + 1];+ let n0r = nr, n0y = ny, n1r = nr, n1y = ny;+ if (prev && prev[0] * nr + prev[1] * ny > cr) { n0r = prev[0] + nr; n0y = prev[1] + ny; }+ if (next && next[0] * nr + next[1] * ny > cr) { n1r = next[0] + nr; n1y = next[1] + ny; }+ const base = this.count, p0 = prof[j], p1 = prof[j + 1];+ const circ = Math.max(p0[0], p1[0]) * TAU, uScale = Math.max(1, Math.round(circ)) / n;+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ for (let i = 0; i <= n; i++) {+ const t = a0 + TAU * i / n, c = Math.cos(t), sn = Math.sin(t);+ this.v(p0[0] * c, p0[1], p0[0] * sn, n0r * c, n0y, n0r * sn, i * uScale, vv, sA);+ this.v(p1[0] * c, p1[1], p1[0] * sn, n1r * c, n1y, n1r * sn, i * uScale, vv + l, sB);+ }+ for (let i = 0; i < n; i++) this.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+ vv += l;+ }+ return this;+ }+ // Sweep a closed section (a,b CCW; a along in-plane left normal, b along +Z) along a path in the XY plane.+ sweep(path, sec, o = {}) {+ const closedPath = !!o.closed, cr = Math.cos(rad(o.crease === undefined ? 50 : o.crease)), z0 = o.z || 0, s = o.s === undefined ? 1 : o.s;+ const n = path.length, E = [];+ const segCount = closedPath ? n : n - 1;+ for (let i = 0; i < segCount; i++) { const p = path[i], q = path[(i + 1) % n]; const dx = q[0] - p[0], dy = q[1] - p[1], l = Math.hypot(dx, dy) || 1e-6; E.push([dx / l, dy / l, l]); }+ // section normals+ const K = sec.length, SN = [];+ for (let j = 0; j < K; j++) { const a = sec[j], b = sec[(j + 1) % K]; const da = b[0] - a[0], db = b[1] - a[1], l = Math.hypot(da, db) || 1e-6; SN.push([db / l, -da / l, l]); }+ const rings = []; let dist = 0;+ for (let i = 0; i < segCount; i++) {+ const e = E[i], ePrev = closedPath ? E[(i - 1 + segCount) % segCount] : (E[i - 1] || e), eNext = closedPath ? E[(i + 1) % segCount] : (E[i + 1] || e);+ const p = path[i], q = path[(i + 1) % n];+ const miter = (A, B) => { let bx = -(A[1] + B[1]), by = A[0] + B[0]; const l = Math.hypot(bx, by) || 1; bx /= l; by /= l; const nlx = -B[1], nly = B[0]; const c = Math.max(0.35, bx * nlx + by * nly); return [bx / c, by / c]; };+ const m0 = miter(ePrev, e), m1 = miter(e, eNext);+ const smooth0 = ePrev[0] * e[0] + ePrev[1] * e[1] > cr, smooth1 = e[0] * eNext[0] + e[1] * eNext[1] > cr;+ const nl = [-e[1], e[0]];+ const n0 = smooth0 ? [-(e[1] + ePrev[1]), e[0] + ePrev[0]] : nl, n1 = smooth1 ? [-(e[1] + eNext[1]), e[0] + eNext[0]] : nl;+ const ln0 = Math.hypot(n0[0], n0[1]) || 1, ln1 = Math.hypot(n1[0], n1[1]) || 1;+ let perim = 0;+ for (let j = 0; j < K; j++) {+ const A = sec[j], B = sec[(j + 1) % K], sn = SN[j], pv = SN[(j - 1 + K) % K], nx = SN[(j + 1) % K];+ const sA = sn[0] * pv[0] + sn[1] * pv[1] > cr, sB = sn[0] * nx[0] + sn[1] * nx[1] > cr;+ const nA = sA ? [sn[0] + pv[0], sn[1] + pv[1]] : sn, nB = sB ? [sn[0] + nx[0], sn[1] + nx[1]] : sn;+ const norm = (na, nb, nv, ln) => { const l = Math.hypot(na, nb) || 1; const a = na / l, b = nb / l; return [a * nv[0] / ln, a * nv[1] / ln, b]; };+ const na0 = norm(nA[0], nA[1], n0, ln0), nb0 = norm(nB[0], nB[1], n0, ln0), na1 = norm(nA[0], nA[1], n1, ln1), nb1 = norm(nB[0], nB[1], n1, ln1);+ const shA = A[2] === undefined ? s : A[2] * s, shB = B[2] === undefined ? s : B[2] * s;+ const a = this.v(p[0] + A[0] * m0[0], p[1] + A[0] * m0[1], z0 + A[1], na0[0], na0[1], na0[2], dist, perim, shA);+ const b = this.v(p[0] + B[0] * m0[0], p[1] + B[0] * m0[1], z0 + B[1], nb0[0], nb0[1], nb0[2], dist, perim + sn[2], shB);+ const c = this.v(q[0] + B[0] * m1[0], q[1] + B[0] * m1[1], z0 + B[1], nb1[0], nb1[1], nb1[2], dist + e[2], perim + sn[2], shB);+ const d = this.v(q[0] + A[0] * m1[0], q[1] + A[0] * m1[1], z0 + A[1], na1[0], na1[1], na1[2], dist + e[2], perim, shA);+ this.quad(a, b, c, d);+ perim += sn[2];+ }+ dist += e[2];+ rings.push(m0);+ }+ if (!closedPath && o.caps !== false) {+ const capAt = (p, nv, e, flip) => {+ const pts = sec.map((A) => [p[0] + A[0] * nv[0], p[1] + A[0] * nv[1], z0 + A[1]]);+ const idx = triangulate(sec.map((A) => [A[0], A[1]]));+ const ids = pts.map((q) => this.v(q[0], q[1], q[2], flip * e[0], flip * e[1], 0, q[0], q[2], s * 0.8));+ for (const t of idx) { if (flip > 0) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); else this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ };+ capAt(path[0], rings[0], E[0], -1);+ const lastE = E[segCount - 1]; capAt(path[n - 1], [-lastE[1], lastE[0]], lastE, 1);+ }+ return this;+ }+ // Generic tube along a 3D path with parallel transport. radii: number|array|fn(t) -> [ra, rb]; ring: fn(angle,t) multiplier+ tube(path, radii, n = 8, o = {}) {+ const N = path.length, s = o.s === undefined ? 1 : o.s;+ let T = [], U = o.up ? [...o.up] : null;+ for (let i = 0; i < N; i++) {+ const p = path[Math.max(0, i - 1)], q = path[Math.min(N - 1, i + 1)];+ let tx = q[0] - p[0], ty = q[1] - p[1], tz = q[2] - p[2]; const l = Math.hypot(tx, ty, tz) || 1; T.push([tx / l, ty / l, tz / l]);+ }+ if (!U) U = Math.abs(T[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];+ const base = this.count; const rowLen = n + 1; let dist = 0;+ for (let i = 0; i < N; i++) {+ const t = T[i];+ let d = U[0] * t[0] + U[1] * t[1] + U[2] * t[2];+ let ux = U[0] - d * t[0], uy = U[1] - d * t[1], uz = U[2] - d * t[2]; const ul = Math.hypot(ux, uy, uz) || 1; ux /= ul; uy /= ul; uz /= ul; U = [ux, uy, uz];+ const vx = t[1] * uz - t[2] * uy, vy = t[2] * ux - t[0] * uz, vz = t[0] * uy - t[1] * ux;+ const f = i / (N - 1);+ const rr = typeof radii === 'function' ? radii(f) : (Array.isArray(radii) ? radii[i] : radii);+ const ra = Array.isArray(rr) ? rr[0] : rr, rb = Array.isArray(rr) ? rr[1] : rr;+ if (i > 0) dist += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1], path[i][2] - path[i - 1][2]);+ for (let k = 0; k <= n; k++) {+ const a = TAU * k / n, c = Math.cos(a), sn = Math.sin(a);+ const mod = o.ring ? o.ring(a, f) : 1;+ const shade = o.ringS ? o.ringS(a, f) : 1;+ const px = path[i][0] + (ux * c * ra + vx * sn * rb) * mod, py = path[i][1] + (uy * c * ra + vy * sn * rb) * mod, pz = path[i][2] + (uz * c * ra + vz * sn * rb) * mod;+ const nx = ux * c / (ra || 1) + vx * sn / (rb || 1), ny = uy * c / (ra || 1) + vy * sn / (rb || 1), nz = uz * c / (ra || 1) + vz * sn / (rb || 1);+ this.v(px, py, pz, nx, ny, nz, k / n * Math.max(ra, rb) * TAU, dist, s * shade);+ }+ }+ for (let i = 0; i < N - 1; i++) for (let k = 0; k < n; k++) {+ const a = base + i * rowLen + k, b = a + 1, c = a + rowLen + 1, d = a + rowLen;+ this.quad(a, b, c, d);+ }+ if (o.capEnd !== false) { const c = this.v(path[N - 1][0], path[N - 1][1], path[N - 1][2], T[N - 1][0], T[N - 1][1], T[N - 1][2], 0, 0, s); const b2 = base + (N - 1) * rowLen; const ids = []; for (let k = 0; k < n; k++) ids.push(b2 + k); for (let k = 0; k < n; k++) this.tri(c, ids[k], b2 + k + 1); }+ if (o.capStart) { const c = this.v(path[0][0], path[0][1], path[0][2], -T[0][0], -T[0][1], -T[0][2], 0, 0, s); for (let k = 0; k < n; k++) this.tri(c, base + k + 1, base + k); }+ return this;+ }+ // parametric grid: fn(u,v) -> [x,y,z, shade?]; winding u x v = front. Smooth normals from the grid.+ grid(fn, nu, nv, o = {}) {+ const pts = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pts.push(fn(i / nu, j / nv));+ const id = (i, j) => j * (nu + 1) + i, base = this.count, flip = o.flip ? -1 : 1, s = o.s === undefined ? 1 : o.s;+ const wrapU = !!o.wrapU;+ let vLen = 0;+ for (let j = 0; j <= nv; j++) {+ if (j > 0) { const a = pts[id(0, j - 1)], b = pts[id(0, j)]; vLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ // o.metric: measure across from the middle of each row, so a converging surface keeps true scale instead of a fan of streaks+ let uLen = 0;+ if (o.metric) { let total = 0; for (let i = 1; i <= nu; i++) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; total += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); } uLen = -total / 2; }+ for (let i = 0; i <= nu; i++) {+ if (i > 0) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; uLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ const iP = wrapU ? (i === 0 ? nu - 1 : i - 1) : Math.max(0, i - 1), iN = wrapU ? (i === nu ? 1 : i + 1) : Math.min(nu, i + 1);+ const A = pts[id(iP, j)], B = pts[id(iN, j)], C = pts[id(i, Math.max(0, j - 1))], D = pts[id(i, Math.min(nv, j + 1))];+ const ux = B[0] - A[0], uy = B[1] - A[1], uz = B[2] - A[2], vx = D[0] - C[0], vy = D[1] - C[1], vz = D[2] - C[2];+ const p = pts[id(i, j)];+ const uvU = o.boxUV ? undefined : uLen, uvV = o.boxUV ? undefined : vLen;+ this.v(p[0], p[1], p[2], flip * (uy * vz - uz * vy), flip * (uz * vx - ux * vz), flip * (ux * vy - uy * vx), uvU, uvV, s * (p[3] === undefined ? 1 : p[3]));+ }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) {+ const a = base + id(i, j), b = base + id(i + 1, j), c = base + id(i + 1, j + 1), d = base + id(i, j + 1);+ if (flip > 0) this.quad(a, b, c, d); else this.quad(a, d, c, b);+ }+ return this;+ }+ // 2D outline with holes, extruded along Z from z0 to z1 (front +Z). Reveal sides included.+ slab(outline, holes = [], z0 = 0, z1 = 0.1, o = {}) {+ const so = area2(outline) > 0 ? outline : outline.slice().reverse();+ const hs = holes.map((h) => (area2(h) < 0 ? h : h.slice().reverse()));+ const tris = triangulate(so, hs);+ const all = so.concat(...hs);+ const sf = o.sf === undefined ? 1 : o.sf, sb = o.sb === undefined ? 1 : o.sb, sr = o.sr === undefined ? 0.72 : o.sr;+ if (!o.noFront) { const ids = all.map((p) => this.v(p[0], p[1], z1, 0, 0, 1, undefined, undefined, sf)); for (const t of tris) this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ if (!o.noBack) { const ids = all.map((p) => this.v(p[0], p[1], z0, 0, 0, -1, undefined, undefined, sb)); for (const t of tris) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); }+ const side = (loop, s) => {+ const L = loop.length; if (o.noSides) return;+ for (let i = 0; i < L; i++) {+ const p = loop[i], q = loop[(i + 1) % L]; const dx = q[0] - p[0], dy = q[1] - p[1];+ const l = Math.hypot(dx, dy) || 1, nx = dy / l, ny = -dx / l;+ const a = this.v(p[0], p[1], z0, nx, ny, 0, undefined, undefined, s), b = this.v(q[0], q[1], z0, nx, ny, 0, undefined, undefined, s), c = this.v(q[0], q[1], z1, nx, ny, 0, undefined, undefined, s), d = this.v(p[0], p[1], z1, nx, ny, 0, undefined, undefined, s);+ this.quad(a, b, c, d);+ }+ };+ if (!o.noOuterSides) side(so, o.so === undefined ? 0.85 : o.so);+ hs.forEach((h) => side(h, sr));+ return this;+ }+ // Vertical prism from an (x,z) polygon+ prism(pts, y0, y1, o = {}) {+ let A = 0; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; A += p[0] * q[1] - q[0] * p[1]; }+ const L = A > 0 ? pts : pts.slice().reverse(), s = o.s === undefined ? 1 : o.s, st = o.st === undefined ? s : o.st, n = L.length, k = o.topScale === undefined ? 1 : o.topScale;+ let cx = 0, cz = 0; for (const p of L) { cx += p[0]; cz += p[1]; } cx /= n; cz /= n;+ const top = L.map((p) => [cx + (p[0] - cx) * k, cz + (p[1] - cz) * k]);+ for (let i = 0; i < n; i++) {+ const p = L[i], q = L[(i + 1) % n], P2 = top[i], Q2 = top[(i + 1) % n];+ this.face([[q[0], y0, q[1], s], [p[0], y0, p[1], s], [P2[0], y1, P2[1], st], [Q2[0], y1, Q2[1], st]]);+ }+ if (!o.noTop && k > 1e-4) { const c = top.map((p) => this.v(p[0], y1, p[1], 0, 1, 0, undefined, undefined, st)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i + 1], c[i]); }+ if (o.bot) { const c = L.map((p) => this.v(p[0], y0, p[1], 0, -1, 0, undefined, undefined, s * 0.6)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i], c[i + 1]); }+ return this;+ }+ // Welds vertices that agree in every attribute: sweeps and lathes otherwise export many duplicates+ // Split every edge longer than L. The rule depends only on the edge, so neighbours split alike and no crack opens.+ // Long walls need vertices for the baked light to land on.+ tess(L) {+ this.weld();+ const L2 = L * L, { P: p, N: nn, UV: uv, C: c } = this;+ for (let pass = 0; pass < 7; pass++) {+ const mid = new Map(), I2 = [], I0 = this.I; let changed = false;+ const long = (a, b) => { const x = p[a * 3] - p[b * 3], y = p[a * 3 + 1] - p[b * 3 + 1], z = p[a * 3 + 2] - p[b * 3 + 2]; return x * x + y * y + z * z > L2; };+ const midOf = (a, b) => {+ const lo = Math.min(a, b), hi = Math.max(a, b), key = lo * 4194304 + hi; let m = mid.get(key);+ if (m === undefined) {+ m = p.length / 3; mid.set(key, m);+ p.push((p[lo * 3] + p[hi * 3]) / 2, (p[lo * 3 + 1] + p[hi * 3 + 1]) / 2, (p[lo * 3 + 2] + p[hi * 3 + 2]) / 2);+ let nx = nn[lo * 3] + nn[hi * 3], ny = nn[lo * 3 + 1] + nn[hi * 3 + 1], nz = nn[lo * 3 + 2] + nn[hi * 3 + 2]; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;+ nn.push(nx / l, ny / l, nz / l);+ uv.push((uv[lo * 2] + uv[hi * 2]) / 2, (uv[lo * 2 + 1] + uv[hi * 2 + 1]) / 2);+ c.push((c[lo * 3] + c[hi * 3]) >> 1, (c[lo * 3 + 1] + c[hi * 3 + 1]) >> 1, (c[lo * 3 + 2] + c[hi * 3 + 2]) >> 1);+ }+ return m;+ };+ for (let t = 0; t < I0.length; t += 3) {+ const a = I0[t], b = I0[t + 1], d = I0[t + 2], ab = long(a, b), bd = long(b, d), da = long(d, a);+ if (!ab && !bd && !da) { I2.push(a, b, d); continue; }+ changed = true;+ const m1 = ab ? midOf(a, b) : -1, m2 = bd ? midOf(b, d) : -1, m3 = da ? midOf(d, a) : -1;+ if (ab && bd && da) I2.push(a, m1, m3, m1, b, m2, m3, m2, d, m1, m2, m3);+ else if (ab && bd) I2.push(a, m1, d, m1, m2, d, m1, b, m2);+ else if (bd && da) I2.push(b, m2, a, m2, m3, a, m2, d, m3);+ else if (da && ab) I2.push(d, m3, b, m3, m1, b, m3, a, m1);+ else if (ab) I2.push(a, m1, d, m1, b, d);+ else if (bd) I2.push(b, m2, a, m2, d, a);+ else I2.push(d, m3, b, m3, a, b);+ }+ this.I = I2;+ if (!changed) break;+ }+ this.geo = null;+ return this;+ }+ weld() {+ if (this.welded) return this;+ const n = this.count, map = new Map(), remap = new Int32Array(n), P2 = [], N2 = [], U2 = [], C2 = [];+ const { P: p, N: nn, UV: uv, C: c } = this, col = !this.noColor;+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, i2 = i * 2;+ const key = Math.round(p[i3] * 4000) + ',' + Math.round(p[i3 + 1] * 4000) + ',' + Math.round(p[i3 + 2] * 4000) + ',' + Math.round(nn[i3] * 90) + ',' + Math.round(nn[i3 + 1] * 90) + ',' + Math.round(nn[i3 + 2] * 90) + ',' + Math.round(uv[i2] * 800) + ',' + Math.round(uv[i2 + 1] * 800) + (col ? ',' + c[i3] + ',' + c[i3 + 1] + ',' + c[i3 + 2] : '');+ let k = map.get(key);+ if (k === undefined) { k = P2.length / 3; map.set(key, k); P2.push(p[i3], p[i3 + 1], p[i3 + 2]); N2.push(nn[i3], nn[i3 + 1], nn[i3 + 2]); U2.push(uv[i2], uv[i2 + 1]); C2.push(c[i3], c[i3 + 1], c[i3 + 2]); }+ remap[i] = k;+ }+ const I2 = [];+ for (let t = 0; t < this.I.length; t += 3) {+ const a = remap[this.I[t]], b = remap[this.I[t + 1]], d = remap[this.I[t + 2]];+ if (a !== b && b !== d && a !== d) I2.push(a, b, d);+ }+ this.P = P2; this.N = N2; this.UV = U2; this.C = C2; this.I = I2; this.welded = true;+ return this;+ }+ toGeo() {+ if (this.geo) return this.geo;+ this.weld();+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.P), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(this.N), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.UV), 2));+ if (!this.noColor) g.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(this.C), 3, true));+ g.setIndex(new THREE.BufferAttribute(this.count > 65535 ? new Uint32Array(this.I) : new Uint16Array(this.I), 1));+ this.geo = g;+ return g;+ }+}+function area2(p) { let a = 0; for (let i = 0; i < p.length; i++) { const q = p[(i + 1) % p.length]; a += p[i][0] * q[1] - q[0] * p[i][1]; } return a / 2; }+function triangulate(contour, holes = []) {+ const V = (p) => new THREE.Vector2(p[0], p[1]);+ const tris = THREE.ShapeUtils.triangulateShape(contour.map(V), holes.map((h) => h.map(V)));+ const all = contour.concat(...holes);+ return tris.map((t) => {+ const a = all[t[0]], b = all[t[1]], c = all[t[2]];+ const s = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ return s >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]];+ });+}++// ---- 2D curves -------------------------------------------------------------------+// Two-centred pointed arc: span w, rise h, base y=0 centred at x=0, from left spring to right spring+function pointedArc(w, h, n = 8) {+ const c = (h * h - w * w / 4) / w, r = c + w / 2, pts = [];+ const a0 = PI, a1 = Math.atan2(h, -c);+ for (let i = 0; i <= n; i++) { const a = lerp(a0, a1, i / n); pts.push([c + r * Math.cos(a), r * Math.sin(a)]); }+ for (let i = n - 1; i >= 0; i--) { const a = lerp(a0, a1, i / n); pts.push([-(c + r * Math.cos(a)), r * Math.sin(a)]); }+ pts[n][0] = 0;+ return pts;+}+// CCW outline of a pointed opening: width w, sill y0, spring ys, rise h+function archOutline(w, y0, ys, h, n = 8) {+ const arc = pointedArc(w, h, n).map((p) => [p[0], p[1] + ys]);+ arc.reverse(); // right spring -> apex -> left spring (CCW)+ return [[-w / 2, y0], [w / 2, y0], ...arc];+}+const ringPts = (cx, cy, r, n = 24, ph = 0) => { const a = []; for (let i = 0; i < n; i++) { const t = ph + TAU * i / n; a.push([cx + r * Math.cos(t), cy + r * Math.sin(t)]); } return a; };+const ofsY = (pts, dy) => pts.map((p) => [p[0], p[1] + dy]);+const ofsXY = (pts, dx, dy) => pts.map((p) => [p[0] + dx, p[1] + dy]);++// ---- Sections (a, b, shade?) closed CCW ------------------------------------------+// Moulded arch section with convex rolls: a=0 at the soffit, grows outward; symmetric in b.+function rollSection(w, d, rolls = 3, amp = 0.07, seg = 3) {+ const half = [];+ const A = [w, d / 2], B = [0, d * 0.12], dx = B[0] - A[0], dy = B[1] - A[1], l = Math.hypot(dx, dy);+ for (let k = 0; k < rolls; k++) for (let i = 0; i < seg; i++) {+ const t = (k + i / seg) / rolls, ph = i / seg;+ const off = amp * Math.pow(Math.sin(PI * ph), 0.7), sh = 0.68 + 0.32 * Math.sin(PI * ph);+ half.push([lerp(A[0], B[0], t) + off * dy / l, lerp(A[1], B[1], t) - off * dx / l, sh]);+ }+ half.push([B[0], B[1], 0.8]);+ const back = half.slice().reverse().map((p) => [p[0], -p[1], p[2]]);+ return [[w, -d / 2, 1], ...half, ...back.slice(0, -1)];+}+// Cheapest moulded bar: a hexagon, smooth enough to read as a roll at distance+function hexBar(w, d) { const a = w / 2, b = d / 2; return [[a, -b * 0.5], [a, b * 0.5], [0, b], [-a, b * 0.5], [-a, -b * 0.5], [0, -b]]; }+// Simple chamfered bar centred on the path, width w (in plane), depth d+function barSection(w, d, ch = 0.25) {+ const a = w / 2, b = d / 2, c = Math.min(a, b) * ch * 2;+ return [[-a, -b + c], [-a + c, -b], [a - c, -b], [a, -b + c], [a, b - c], [a - c, b], [-a + c, b], [-a, b - c]];+}+// Tracery bar with a roll on the front and back faces+function traceryBar(w, d) {+ const a = w / 2, b = d / 2, s = [];+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([a * Math.cos(t), b * 0.62 + b * 0.38 * Math.sin(t)]); }+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([-a * Math.cos(t), -b * 0.62 - b * 0.38 * Math.sin(t)]); }+ return s;+}+// String course / cornice projecting along +b from the wall at b=0, height h (a)+function stringSection(h = 0.3, out = 0.25) {+ return [[-h / 2, 0], [-h / 2, out * 0.3, 0.55], [-h * 0.25, out * 0.62, 0.7], [-h * 0.02, out * 0.72, 0.85], [h * 0.12, out, 1], [h * 0.2, out * 0.98, 1], [h / 2, out * 0.05, 1], [h / 2, 0]].reverse();+}++const spline = (pts, sub = 4) => { // Catmull-Rom through 3D points+ const out = [], n = pts.length, g = (i) => pts[clamp(i, 0, n - 1)];+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < sub; k++) {+ const t = k / sub, p0 = g(i - 1), p1 = g(i), p2 = g(i + 1), p3 = g(i + 2), v = [];+ for (let c = 0; c < 3; c++) v.push(0.5 * (2 * p1[c] + (p2[c] - p0[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t * t + (3 * p1[c] - p0[c] - 3 * p2[c] + p3[c]) * t * t * t));+ out.push(v);+ }+ out.push(pts[n - 1]);+ return out;+};+const blob = (b, c, r, nu = 8, nv = 6, sh = 1) => b.grid((u, v) => {+ const ph = -PI + TAU * u, la = -PI / 2 + PI * v;+ return [c[0] + r[0] * Math.cos(la) * Math.cos(ph), c[1] + r[1] * Math.sin(la), c[2] + r[2] * Math.cos(la) * Math.sin(ph), sh];+}, nu, nv, { flip: true, wrapU: true });++// ==================================================================================+// MATERIALS: procedural PBR, with height-derived normals+// ==================================================================================+const MAT = {};+const TILE = { ashlar: 8, fine: 2, lead: 4, pave: 2.4, oak: 1.2, iron: 0.8, gold: 1 };++function ashlarOps(rng, S, tone, joint, stains) {+ // Irregular coursed blocks; shared between albedo and height by replaying the same rng seed+ const ops = [], rows = 7, rowH = Math.floor(S / rows);+ for (let r = 0; r < rows; r++) {+ const y0 = r * rowH; let x = Math.round(rng() * S / 5) % S;+ const n = 3 + Math.floor(rng() * 2), widths = [];+ for (let i = 0; i < n; i++) widths.push(0.7 + rng() * 0.6);+ const tot = widths.reduce((a, c) => a + c, 0);+ for (let i = 0; i < n; i++) {+ const w = Math.round(widths[i] / tot * S), dv = rng(), a = rng();+ const col = tone[Math.floor(dv * tone.length)];+ const x0 = x % S, wr = Math.min(w, S - x0), op = Math.round(10 + a * 34);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y0, width: wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ if (wr < w) ops.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: y0, width: w - wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ x += w;+ ops.push({ op: 'paint', shape: { kind: 'line', from: [x % S, y0], to: [x % S, y0 + rowH], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y0], to: [S, y0], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ return ops.slice(0, 58).concat(stains || []);+}++// Regions of the decal sheet as fractions [u0, v0, u1, v1]; a streak's top edge is at v0+const DECAL_UV = { streak: [0, 0, 0.5, 0.5], soft: [0.5, 0, 1, 0.25], soot: [0.5, 0.25, 1, 0.5], ground: [0, 0.5, 0.5, 1], lichen: [0.75, 0.5, 1, 0.75], drip: [0.5, 0.75, 1, 1] };+async function makeMaterials() {+ const S = 512, repeat = { minFilter: 'linear-mipmap-linear', magFilter: 'linear', wrapS: 'repeat', wrapT: 'repeat' };+ const noise = (a, b, scale, oct, seed, opacity, blend) => ({ op: 'noise', colorA: a, colorB: b, scale, octaves: oct, seed, opacity, blend: blend || 'normal' });+ const spec3 = (usage, base, ops, size = S) => ({ schemaVersion: 3, width: size, height: size, usage, operations: [{ op: 'legacy-v2', spec: { schemaVersion: 2, size, usage, layers: base } }, ...ops] });+ const v3 = (usage, base, ops, size = S) => proceduralTexture(spec3(usage, base, ops, size));+ const stamps = (shape, rgba, count, seed, opacity, area) => ({ op: 'stamps', shape, rgba, count, seed, area: area || { x: 0, y: 0, width: S, height: S }, opacity });+ const ell = (w, h) => ({ kind: 'ellipse', x: 0, y: 0, width: w, height: h });+ const tool = (dx, dy, w = 1) => ({ kind: 'line', from: [0, 0], to: [dx, dy], width: w }); // a stroke of the claw chisel++ // ---- Weathered ashlar: 8 m repeat, every block its own stone. Colour varies by block, chisel marks+ // in the relief instead of round pits, rain and soot already sunk into the surface ----+ const quads = [[0, 0], [S, 0], [0, S], [S, S]];+ const ashAlbedo = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb9ad96 }, noise(0x968b76, 0xcdc3ad, 3, 2, seed + 1, 0.85), noise(0x7f7666, 0xc6bca6, 17, 2, seed + 2, 0.4)],+ ashlarOps(mkRng(seed), S, [[78, 72, 60], [226, 216, 190], [158, 132, 92], [122, 128, 124]], { w: 2, rgba: [70, 64, 54, 255], op: 225 }, [+ stamps(ell(20, 70), [70, 66, 56, 255], 26, seed + 3, 24), stamps(ell(40, 26), [52, 56, 46, 255], 16, seed + 4, 20), stamps(tool(11, 6), [96, 88, 74, 255], 110, seed + 5, 34),+ ]));+ const ashHeight = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb4b4b4 }, noise(0x8c8c8c, 0xd0d0d0, 11, 2, seed + 6, 0.7)],+ ashlarOps(mkRng(seed), S, [[126, 126, 126], [214, 214, 214], [170, 170, 170], [150, 150, 150]], { w: 3, rgba: [12, 12, 12, 255], op: 255 }, [+ stamps(tool(14, 8), [86, 86, 86, 255], 120, seed + 8, 90), stamps(tool(-12, 9), [226, 226, 226, 255], 110, seed + 9, 70),+ ]));+ const atlasOf = async (usage, make, seeds) => textureAtlas({ schemaVersion: 1, width: 2 * S, height: 2 * S, pack: { edge: 2048, gutter: 0 }, usage, sampler: repeat, regions: quads.map((p, i) => ({ name: 'q' + i, content: { x: p[0], y: p[1], width: S, height: S }, gutter: 0, source: { kind: 'procedural', spec: make(p, seeds[i]) } })) });+ const ashA = await atlasOf('albedo', ashAlbedo, [77, 131, 211, 307]);+ const ashH = await atlasOf('albedo', ashHeight, [77, 131, 211, 307]);+ MAT.ashlar = pbrMaterial({ albedo: ashA, normal: normalMapFromHeight(ashH, { strength: 2.6 }), roughness: 0.94, metalness: 0, name: 'Limestone_Ashlar' });++ // Dressed stone: no joints; chisel strokes and mottling live in the colour, because a normal map here would+ // add 16 bytes of tangent to every vertex of every moulding+ const finA = v3('albedo', [{ op: 'solid', color: 0xc2b7a0 }, noise(0xb5ab95, 0xcdc4af, 5, 3, 31, 0.8), noise(0xa79d88, 0xcbc2ad, 23, 3, 32, 0.34)], [+ stamps(tool(9, 5), [120, 112, 96, 255], 130, 33, 34), stamps(tool(-7, 6), [214, 206, 188, 255], 80, 34, 30),+ ]);+ MAT.fine = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name: 'Limestone_Dressed' });+ const figTone = (name, r, g, b) => { const m = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name }); m.color.setRGB(r, g, b); m.vertexColors = true; return m; };+ MAT.figure = figTone('Sculpture_Sunlit', 0.86, 0.8, 0.68); // figures take the same stone under different light+ MAT.figureHalf = figTone('Sculpture_HalfShade', 0.5, 0.49, 0.47);+ MAT.figureDeep = figTone('Sculpture_DeepShade', 0.3, 0.31, 0.34);+ MAT.figureIn = figTone('Sculpture_Interior', 0.62, 0.53, 0.42);++ // ---- Patinated lead: 1 m sheets lapped every 2.5 m, each sheet its own tone, runoff streaks and white bloom ----+ const LS = 512, leadOps = (tones, lapOp, seamOp, rngSeed) => {+ const r = mkRng(rngSeed), ops = [], cols = 4, W = LS / cols, H = LS / 2.5 | 0;+ for (let c = 0; c < cols; c++) {+ const off = c % 2 ? H / 2 | 0 : 0;+ for (let k = -1; k < 3; k++) {+ const y = Math.max(0, k * H + off), yb = Math.min(LS, (k + 1) * H + off);+ if (yb <= y) continue;+ const t = tones[Math.floor(r() * tones.length)];+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: c * W, y, width: W, height: yb - y }, rgba: [t[0], t[1], t[2], 255], opacity: 40 + Math.floor(r() * 90) });+ if (yb < LS) ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, yb], to: [(c + 1) * W, yb], width: lapOp.w }, rgba: lapOp.rgba, opacity: lapOp.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, 0], to: [c * W, LS], width: seamOp.w }, rgba: seamOp.rgba, opacity: seamOp.op });+ }+ return ops;+ };+ const leadArea = { x: 0, y: 0, width: LS, height: LS };+ const leadA = v3('albedo', [{ op: 'solid', color: 0x6f7a7e }, noise(0x566166, 0x8d9896, 5, 2, 41, 0.75)], [+ ...leadOps([[86, 98, 102], [118, 130, 130], [66, 76, 82], [140, 150, 146], [100, 104, 100]], { w: 2, rgba: [36, 42, 46, 255], op: 220 }, { w: 2, rgba: [40, 46, 50, 255], op: 180 }, 44),+ stamps({ kind: 'rectangle', x: 0, y: 0, width: 5, height: 85 }, [208, 214, 206, 255], 90, 45, 30, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 3, height: 130 }, [34, 40, 44, 255], 80, 46, 42, leadArea), stamps(ell(23, 15), [176, 186, 176, 255], 60, 47, 24, leadArea),+ ], LS);+ const leadH = v3('albedo', [{ op: 'solid', color: 0x8c8c8c }, noise(0x848484, 0x989898, 9, 2, 48, 0.8)], [+ ...leadOps([[140, 140, 140], [132, 132, 132], [148, 148, 148]], { w: 3, rgba: [232, 232, 232, 255], op: 255 }, { w: 5, rgba: [250, 250, 250, 255], op: 255 }, 44),+ stamps(ell(17, 11), [116, 116, 116, 255], 110, 49, 60, leadArea),+ ], LS);+ MAT.lead = pbrMaterial({ albedo: leadA, normal: normalMapFromHeight(leadH, { strength: 3 }), roughness: 0.6, metalness: 0.5, name: 'Lead_Patinated' });++ // ---- Gold leaf: a bright warm metal that still reads as gold where the environment is dull ----+ const gldA = v3('albedo', [{ op: 'solid', color: 0xffc644 }, noise(0xf0a830, 0xffd764, 14, 2, 51, 0.6)], [stamps(ell(5, 4), [176, 110, 30, 255], 60, 52, 90, { x: 0, y: 0, width: 128, height: 128 })], 128);+ MAT.gold = pbrMaterial({ albedo: gldA, roughness: 0.25, metalness: 1, name: 'Gilding' });+ MAT.gold.emissive.setRGB(1, 0.6, 0.14); MAT.gold.emissiveIntensity = 0.13;++ // Aged oak and forged iron from the approved scanned sets+ MAT.oak = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.oak-veneer-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.oak-veneer-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.oak-veneer-arm.v1'), name: 'Oak_Aged' });+ MAT.iron = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.rusted-metal-albedo.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.rusted-metal-arm.v1'), name: 'Iron_Forged' });++ // Paving: worn limestone and grey marble, polished by seven centuries of feet+ const pavA = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'checker', colorA: 0xd2cab6, colorB: 0x8c8678, squares: 4 }, noise(0x7c766a, 0xe0dac8, 9, 3, 61, 0.42), { op: 'bricks', brick: 0xffffff, mortar: 0x5a564e, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ const pavH = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'solid', color: 0xc8c8c8 }, noise(0xb0b0b0, 0xdcdcdc, 15, 3, 62, 0.5), { op: 'bricks', brick: 0xffffff, mortar: 0x202020, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ MAT.pave = pbrMaterial({ albedo: pavA, normal: normalMapFromHeight(pavH, { strength: 1.8 }), roughness: 0.42, metalness: 0, name: 'Paving_Worn' });+ for (const k of ['ashlar', 'fine', 'oak', 'iron', 'pave', 'lead']) MAT[k].vertexColors = true;++ // ---- Weathering decals: rain streaks, soot, ground damp and moss, cast shadow. Alpha-blended overlays+ // give crisp detail on walls that have very few vertices ----+ const DS = 256, rd = mkRng(99), polyStreak = (x, y0, L, w, rgb, op) => ({ op: 'paint', shape: { kind: 'polygon', points: [[x, y0], [x + w, y0], [x + w * 0.8 + 1, y0 + L * 0.45], [x + w * 0.55 + 1, y0 + L * 0.8], [x + w * 0.45, y0 + L], [x + w * 0.3, y0 + L * 0.75], [x + w * 0.15, y0 + L * 0.4]].map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: [rgb[0], rgb[1], rgb[2], 255], opacity: op });+ const streakOps = [], dripOps = [];+ for (let i = 0; i < 44; i++) { const x = Math.floor(rd() * (DS - 9)), dark = rd() > 0.26, L = 65 + Math.floor(rd() * 185), w = 2 + Math.floor(rd() * 7); streakOps.push(polyStreak(x, 0, Math.min(L, DS - 2), w, dark ? [44, 40, 34] : [176, 170, 150], dark ? 40 + Math.floor(rd() * 70) : 26 + Math.floor(rd() * 30))); }+ for (let i = 0; i < 12; i++) streakOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 3 + i * 2 }, rgba: [34, 32, 28, 255], opacity: 12 });+ for (let i = 0; i < 38; i++) { const x = Math.floor(rd() * (DS - 8)), L = 20 + Math.floor(rd() * 100), w = 1 + Math.floor(rd() * 6); dripOps.push(polyStreak(x, 0, Math.min(L, 125), w, [38, 36, 30], 44 + Math.floor(rd() * 70))); }+ for (let i = 0; i < 10; i++) dripOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 2 + i * 2 }, rgba: [28, 26, 24, 255], opacity: 16 });+ const softOps = []; for (let i = 0; i < 30; i++) softOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: Math.max(1, Math.round(128 * Math.pow(1 - i / 30, 1.5))) }, rgba: [18, 18, 24, 255], opacity: 15 });+ const dampOps = [];+ for (let i = 0; i < 18; i++) { const h = Math.round(DS * Math.pow((i + 1) / 18, 1.6)); dampOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: DS - h, width: DS, height: h }, rgba: i % 3 ? [38, 42, 28, 255] : [50, 58, 34, 255], opacity: 13 }); }+ dampOps.push(stamps(ell(13, 8), [66, 84, 42, 255], 110, 71, 46, { x: 0, y: 120, width: DS, height: 134 }), stamps(ell(5, 28), [30, 32, 24, 255], 80, 72, 64, { x: 0, y: 60, width: DS, height: 190 }), stamps(ell(3, 3), [120, 132, 76, 255], 60, 73, 90, { x: 0, y: 100, width: DS, height: 150 }));+ const clear = [0, 0, 0, 0];+ const dreg = (name, x, y, w, h, ops, bg) => ({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'albedo', background: bg || clear, operations: ops } } });+ const decalTex = await textureAtlas({ schemaVersion: 1, width: 512, height: 512, usage: 'albedo', regions: [+ dreg('streak', 0, 0, DS, DS, streakOps.slice(0, 64)),+ dreg('soft', 256, 0, DS, 128, softOps),+ dreg('soot', 256, 128, DS, 128, [stamps(ell(35, 27), [24, 22, 20, 255], 150, 74, 20, { x: 20, y: 15, width: 200, height: 90 }), stamps(ell(13, 11), [18, 16, 14, 255], 90, 75, 36, { x: 45, y: 25, width: 160, height: 75 })]),+ dreg('ground', 0, 256, DS, DS, dampOps),+ dreg('lichen', 384, 256, 128, 128, [stamps(ell(7, 5), [172, 164, 92, 255], 110, 76, 90, { x: 5, y: 5, width: 115, height: 115 }), stamps(ell(4, 3), [176, 120, 54, 255], 80, 77, 96, { x: 5, y: 5, width: 115, height: 115 })]),+ dreg('drip', 256, 384, DS, 128, dripOps.slice(0, 64)),+ ] });+ MAT.decal = pbrMaterial({ albedo: decalTex, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Weathering_Decals' });+ MAT.decal.depthWrite = false;++ await makeGlassMaterials();++ // ---- The light of the windows on the floor: a soft shape, painted very small and enlarged, so its edges melt ----+ const poolOps = [], PW = 32, PH = 64, poolCols = [[196, 40, 36], [60, 74, 176], [206, 150, 50], [176, 34, 34], [216, 170, 80], [176, 44, 50]];+ poolCols.forEach((c, i) => poolOps.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 5, height: 7 }, rgba: [c[0], c[1], c[2], 255], count: 34, seed: 5150 + i * 7, area: { x: 0, y: 0, width: PW, height: PH }, opacity: 150 }));+ // window silhouette: pointed head at the top of the image, a shadow down the middle where the mullion stands+ const silh = pointedArc(PW - 4, 20, 6).map((p) => [Math.round(p[0] + PW / 2), Math.round(21 - p[1])]);+ poolOps.push({ op: 'mask', shape: { kind: 'polygon', points: [[2, PH - 1], ...silh, [PW - 2, PH - 1]] } });+ poolOps.push({ op: 'mask', shape: { kind: 'rectangle', x: PW / 2 - 1, y: 8, width: 2, height: PH - 8 }, invert: true });+ const poolSpec = (usage) => ({ schemaVersion: 3, width: PW, height: PH, usage, background: [120, 32, 44, 180], operations: poolOps });+ MAT.pool = pbrMaterial({ albedo: proceduralTexture(poolSpec('albedo')), emissive: proceduralTexture(poolSpec('emissive')), emissiveIntensity: 0.7, roughness: 0.9, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightPool' });+ MAT.pool.opacity = 0.3;+ MAT.pool.depthWrite = false;+}++// ---- Atlas of the glass painter: pot-metal colours, painted drapery, drawn faces, ornament ------+const GL = { w: 1536, h: 1280, cell: 256, small: 128, fam: {}, tile: {}, face: {}, orn: {}, ground: {} };+// [name, base, light, dark]: real pot-metal glass is deep, and no two sheets match+const GLASS_FAMILIES = [+ ['ruby', [118, 6, 14], [172, 14, 22], [66, 3, 10]], ['cobalt', [10, 20, 104], [20, 44, 160], [5, 10, 62]], ['azure', [16, 54, 128], [36, 90, 170], [8, 32, 88]], ['emerald', [6, 62, 38], [14, 96, 54], [3, 38, 24]],+ ['green', [42, 74, 24], [74, 106, 34], [24, 48, 14]], ['gold', [178, 116, 16], [218, 158, 36], [130, 78, 8]], ['amber', [146, 72, 10], [186, 106, 20], [98, 44, 6]], ['purple', [58, 14, 80], [90, 26, 114], [34, 6, 50]],+ ['flesh', [186, 140, 104], [206, 164, 128], [156, 110, 80]], ['white', [146, 150, 122], [182, 184, 154], [112, 118, 94]], ['brown', [70, 40, 18], [100, 58, 28], [46, 24, 12]],+ ['teal', [8, 62, 70], [16, 92, 100], [5, 38, 46]], ['night', [5, 8, 40], [10, 16, 62], [2, 4, 24]],+];+const GLASS_ALIAS = { rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', sky: 'azure', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', yellow: 'gold', tan: 'amber', fleshDk: 'flesh', violet: 'purple', olive: 'green', forest: 'emerald', hair: 'brown', paint: 'lead' };+// Mosaic grounds, each a square of painted lozenges: [name, colour A, colour B, jewel]+const GLASS_GROUNDS = [['rc', 'ruby', 'cobalt', 'gold'], ['cr', 'cobalt', 'ruby', 'white'], ['eg', 'emerald', 'ruby', 'gold'], ['pr', 'purple', 'cobalt', 'gold'], ['tc', 'teal', 'ruby', 'white']];+function groundOps(A, B, J, seed) { // lozenges 64 px across, a lead round each, a jewel on some crossings+ const r = mkRng(seed * 19 + 5), ops = [], cl = (v) => Math.max(0, Math.min(256, v));+ for (let b = 0; b <= 8; b++) for (let a = 0; a <= 8; a++) {+ if ((a + b) % 2 === 0) continue;+ const f = b % 2 ? A : B, x = a * 32, y = b * 32, t = r(), c = t < 0.45 ? f[1] : t < 0.8 ? f[2] : f[3];+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 32)], [cl(x + 32), cl(y)], [cl(x), cl(y + 32)], [cl(x - 32), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -3; k <= 3; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let k = 1; k <= 7; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let i = 0; i < 6; i++) { const x = 32 + (i % 3) * 96, y = 32 + (i % 2) * 96 + (i > 2 ? 64 : 0); ops.push(paintEll(x, y, 17, 17, 255, [4, 4, 5])); ops.push(paintEll(x, y, 11, 11, 255, J[1])); }+ return ops.slice(0, 64);+}+const DRAPE_OF = { ruby: 'ruby', rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', cobalt: 'cobalt', azure: 'cobalt', sky: 'cobalt', night: 'cobalt', emerald: 'emerald', green: 'emerald', teal: 'emerald', forest: 'emerald', olive: 'emerald', gold: 'gold', amber: 'gold', yellow: 'gold', tan: 'gold', brown: 'gold', white: 'white', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', purple: 'purple', violet: 'purple' };+const GROUND_OF = { ruby: 'rc', rubyLt: 'rc', crimson: 'rc', cobalt: 'cr', azure: 'cr', sky: 'cr', emerald: 'eg', green: 'eg', violet: 'pr', purple: 'pr', teal: 'tc' };+const PAINT = [36, 20, 10];+const paintLine = (a, b, w, op = 235) => ({ op: 'paint', shape: { kind: 'line', from: [Math.round(a[0]), Math.round(a[1])], to: [Math.round(b[0]), Math.round(b[1])], width: w }, rgba: [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintPoly = (pts, op, rgb) => ({ op: 'paint', shape: { kind: 'polygon', points: pts.map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintEll = (x, y, w, h, op, rgb) => ({ op: 'paint', shape: { kind: 'ellipse', x: Math.round(x - w / 2), y: Math.round(y - h / 2), width: Math.round(w), height: Math.round(h) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });++function familyOps(f, seed) { // one sheet of pot-metal: streaks, clouds, seeds, a thicker edge+ const r = mkRng(seed * 7 + 3), ops = [], C = GL.small, clampE = (x, w) => Math.max(w / 2, Math.min(C - w / 2, x));+ for (let i = 0; i < 10; i++) { const c = r() > 0.5 ? f[2] : f[3], w = 24 + Math.floor(r() * 60), h = 18 + Math.floor(r() * 44); ops.push(paintEll(clampE(r() * C, w), clampE(r() * C, h), w, h, 60 + Math.floor(r() * 90), c)); }+ for (let i = 0; i < 9; i++) { const c = r() > 0.5 ? f[2] : f[3], y = Math.floor(r() * C); ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y], to: [C, Math.max(0, Math.min(C, y + Math.floor((r() - 0.5) * 30)))], width: 2 + Math.floor(r() * 4) }, rgba: [c[0], c[1], c[2], 255], opacity: 70 + Math.floor(r() * 80) }); }+ ops.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 2, height: 2 }, rgba: [f[2][0], f[2][1], f[2][2], 255], count: 40, seed: seed + 11, area: { x: 0, y: 0, width: C, height: C }, opacity: 180 });+ return ops;+}+function drapeOps(f, seed) { // brown paint on coloured glass: long folds, each with a soft wash beside its trace line+ const r = mkRng(seed * 13 + 1), ops = [], W = 256, H = 512, dk = [Math.round(f[1][0] * 0.3), Math.round(f[1][1] * 0.3), Math.round(f[1][2] * 0.3)];+ for (let i = 0; i < 13; i++) {+ const x = 18 + i * 18 + (r() - 0.5) * 10, sw = (r() - 0.5) * 42, y0 = 4 + r() * 120, y1 = H - r() * 90, ym = (y0 + y1) / 2, wash = 9 + r() * 12;+ ops.push(paintPoly([[x, y0], [x + wash, y0 + 10], [x + sw * 0.6 + wash * 1.4, ym], [x + sw + wash, y1 - 10], [x + sw, y1], [x + sw * 0.6, ym]], 62 + Math.floor(r() * 36), PAINT));+ ops.push(paintLine([x, y0], [x + sw * 0.6, ym], 3)); ops.push(paintLine([x + sw * 0.6, ym], [x + sw, y1], 2));+ if (i % 3 === 0) ops.push(paintLine([x + sw * 0.6 + wash * 1.6, ym - 40], [x + sw + wash * 1.6, y1 - 60], 3, 150));+ }+ for (let i = 0; i < 4; i++) { const y = 60 + i * 110 + r() * 40; ops.push(paintPoly([[0, y], [W, y + 26 * (r() - 0.5)], [W, y + 22], [0, y + 18]], 40, PAINT)); }+ return ops.slice(0, 62);+}+// A drawn medieval head, 256 x 256, centred: trace lines in brown over a flesh pane, with washes for the modelling+function faceOps(kind) {+ const ops = [], c = 128, mir = (p) => [256 - p[0], p[1]], both = (fn) => { fn((p) => p); fn(mir); };+ const hairC = kind === 'elder' ? [150, 146, 122] : kind === 'angel' ? [150, 96, 30] : [66, 40, 18];+ // washes: the side of the face and temple, under the brow, beside the nose, under the lip+ both((m) => {+ ops.push(paintPoly([m([18, 84]), m([52, 74]), m([46, 150]), m([72, 214]), m([34, 196])], 78));+ ops.push(paintEll(m([88, 104])[0], 106, 58, 20, 64));+ ops.push(paintPoly([m([122, 118]), m([112, 160]), m([102, 172]), m([118, 172])], 58));+ });+ ops.push(paintEll(c, 200, 52, 11, 58));+ // hair+ if (kind !== 'virgin') {+ ops.push(paintPoly([[6, 118], [14, 56], [60, 10], [128, 0], [196, 10], [242, 56], [250, 118], [222, 96], [206, 56], [160, 42], [128, 54], [96, 42], [50, 56], [34, 96]], 255, hairC));+ for (let i = 0; i < 7; i++) { const a = 28 + i * 16; ops.push(paintLine([a, 94 - i * 7], [a + 36, 12 + i * 2], 3, 190)); ops.push(paintLine([256 - a, 94 - i * 7], [256 - a - 36, 12 + i * 2], 3, 190)); }+ } else {+ ops.push(paintPoly([[0, 150], [2, 56], [50, 8], [128, 0], [206, 8], [254, 56], [256, 150], [226, 118], [214, 66], [128, 46], [42, 66], [30, 118]], 255, [214, 216, 186]));+ ops.push(paintLine([38, 124], [50, 62], 4)); ops.push(paintLine([50, 62], [128, 44], 4)); ops.push(paintLine([128, 44], [206, 62], 4)); ops.push(paintLine([206, 62], [218, 124], 4));+ }+ // brows, lids, eyes+ both((m) => {+ ops.push(paintLine(m([60, 96]), m([84, 84]), 5)); ops.push(paintLine(m([84, 84]), m([116, 92]), 5));+ ops.push(paintLine(m([64, 116]), m([84, 104]), 4)); ops.push(paintLine(m([84, 104]), m([112, 116]), 4));+ ops.push(paintLine(m([66, 118]), m([90, 126]), 2)); ops.push(paintLine(m([90, 126]), m([112, 116]), 2));+ ops.push(paintEll(m([88, 115])[0], 115, 15, 15, 255));+ });+ // nose, nostril, mouth, chin+ ops.push(paintLine([120, 92], [114, 150], 4)); ops.push(paintLine([114, 150], [106, 166], 4)); ops.push(paintLine([106, 166], [124, 174], 3)); ops.push(paintLine([136, 172], [150, 166], 3));+ ops.push(paintLine([98, 192], [128, 187], 4)); ops.push(paintLine([128, 187], [158, 192], 4));+ ops.push(paintLine([112, 202], [144, 202], 2, 170));+ if (kind === 'elder' || kind === 'christ' || kind === 'king') {+ const bc = kind === 'elder' ? [168, 164, 138] : [76, 46, 20];+ ops.push(paintPoly([[40, 158], [66, 184], [94, 196], [128, 206], [162, 196], [190, 184], [216, 158], [206, 226], [160, 256], [96, 256], [50, 226]], 255, bc));+ for (let i = 0; i < 7; i++) { const x = 64 + i * 21; ops.push(paintLine([x, 192 + Math.abs(i - 3) * -3], [x + (i - 3) * 3, 252], 3, 210)); }+ ops.push(paintLine([78, 180], [120, 176], 4)); ops.push(paintLine([178, 180], [136, 176], 4));+ } else {+ ops.push(paintLine([100, 226], [128, 236], 3, 170)); ops.push(paintLine([128, 236], [156, 226], 3, 170));+ }+ return ops.slice(0, 64);+}+function ornOps(f, kind, seed) { // painted ornament for borders: a running stem with leaves, scratched out of a dark ground+ const ops = [], C = GL.small;+ ops.push(paintPoly([[0, 0], [C, 0], [C, C], [0, C]], 210));+ const lit = f[2];+ for (let i = 0; i < 5; i++) {+ const y = 13 + i * 26, s = i % 2 ? 1 : -1;+ ops.push(paintPoly([[C / 2, y - 11], [C / 2 + s * 36, y - 6], [C / 2 + s * 46, y + 7], [C / 2 + s * 23, y + 11], [C / 2, y + 13]], 255, lit));+ ops.push(paintLine([C / 2 + s * 4, y - 3], [C / 2 + s * 37, y + 4], 2));+ ops.push(paintLine([C / 2 + s * 4, y + 5], [C / 2 + s * 28, y + 6], 1));+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [C / 2, 0], to: [C / 2, C], width: 7 }, rgba: [lit[0], lit[1], lit[2], 255] });+ return ops;+}+async function makeGlassMaterials() {+ const regions = [], C = GL.cell, Sm = GL.small;+ const addR = (name, x, y, w, h, bg, ops) => regions.push({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'emissive', background: [bg[0], bg[1], bg[2], 255], operations: ops } } });+ // Layout: two rows of small cells (pot-metals, then ornament), then drapery, faces and mosaic grounds+ GLASS_FAMILIES.forEach((f, i) => { const x = (i % 12) * Sm, y = Math.floor(i / 12) * Sm; GL.fam[f[0]] = [x, y]; addR('fam_' + f[0], x, y, Sm, Sm, f[1], familyOps(f, i + 1)); });+ [['gold', 'gold'], ['white', 'white']].forEach((q, i) => { const f = GLASS_FAMILIES.find((v) => v[0] === q[0]), x = (i + 2) * Sm, y = Sm; GL.orn[q[1]] = [x, y]; addR('orn_' + q[1], x, y, Sm, Sm, f[1], ornOps(f, q[1], i + 90)); });+ // painted drapery, one tall tile per colour+ ['ruby', 'cobalt', 'emerald', 'gold', 'white', 'purple'].forEach((n, i) => { const f = GLASS_FAMILIES.find((q) => q[0] === n), x = i * C, y = C; GL.tile[n] = [x, y, C, 2 * C]; addR('drape_' + n, x, y, C, 2 * C, f[1], drapeOps(f, i + 40)); });+ // faces+ ['youth', 'elder', 'christ', 'king', 'virgin', 'angel'].forEach((n, i) => { const x = i * C, y = 3 * C; GL.face[n] = [x, y]; addR('face_' + n, x, y, C, C, n === 'virgin' ? [202, 162, 128] : [192, 148, 112], faceOps(n)); });+ // mosaic grounds+ GLASS_GROUNDS.forEach((q, i) => { const fam = (n) => GLASS_FAMILIES.find((v) => v[0] === n), x = i * C, y = 4 * C; GL.ground[q[0]] = [x, y]; addR('ground_' + q[0], x, y, C, C, fam(q[1])[3], groundOps(fam(q[1]), fam(q[2]), fam(q[3]), i + 3)); });+ GL.fam.lead = [1400, 140]; // the empty part of the sheet is black, and the came needs no pixels of its own+ const atlas = await textureAtlas({ schemaVersion: 1, usage: 'emissive', width: GL.w, height: GL.h, pack: { edge: 2048, gutter: 0 }, regions });+ // Seen from inside the glass is the only light: a black pane that shines+ MAT.glassIn = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow, roughness: 1, metalness: 0, name: 'StainedGlass_Interior' });+ MAT.glassIn.color.setRGB(0, 0, 0);+ // Seen from outside by day it is nearly black, a dull sheen and the colour only hinting through.+ // A rough surface keeps grazing angles from washing out to a pale reflection of the sky.+ MAT.glassOut = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior' });+ MAT.glassOut.color.setRGB(0.008, 0.009, 0.012);+}++// Hierarchy helper: every mesh gets a unique role name; geometry is shared between placements.+let ROOT = null; const NAMES = new Map();+function uniq(n) { const c = (NAMES.get(n) || 0) + 1; NAMES.set(n, c); return c === 1 ? n : n + '_' + c; }+const STAT = new Map();+// Exporter costs measured on this host: pos+normal+uv 32 B, COLOR_0 written as 12 B floats+function batchBytes(b) { b.weld(); return b.count * (32 + (b.noColor ? 0 : 12)) + b.I.length * (b.count > 65535 ? 4 : 2); }+function place(name, batch, mat, o = {}) {+ if (!batch || !batch.I.length) return null;+ if (mat === MAT.gold) batch.noColor = true; // metal needs no baked shading: saves 12 B a vertex+ if (P.preview === 'unique' && batch.geo) return null; // size test: every geometry once, no instances+ if (mat === MAT.ashlar && !batch.tessellated && !batch.geo) { batch.tess(P.wallTessellation); batch.tessellated = true; }+ if (P.preview === 'stats') {+ const e = STAT.get(batch) || { name, n: 0, normalMapped: mat === MAT.ashlar || mat === MAT.lead || mat === MAT.pave || mat === MAT.oak };+ e.n++; STAT.set(batch, e);+ return null;+ }+ const part = createPart(uniq(name), batch.toGeo(), mat, { position: o.pos || [0, 0, 0], rotation: o.rot || [0, 0, 0], scale: o.scale || [1, 1, 1], parent: o.parent || ROOT });+ PLACED.push({ part, batch, mat });+ return part;+}+const PLACED = [];+function group(name, parent) { const g = new THREE.Group(); g.name = uniq(name); (parent || ROOT).add(g); return g; }++// ==================================================================================+// SCULPTURE: robed figures with carved drapery. A figure is a full surface, not a block:+// S-curve stance, tube folds breaking at the hem, swags across the body, sleeves, hands,+// modelled faces, hair and beards, and the attributes each saint carries.+// ==================================================================================+// Every figure is a signed-distance volume: a long column of cloth with deep pleats, a mantle+// laid over it, arms under the cloth, and a separately resolved head and hands. A surface-net+// pass extracts the skin, the simplifier keeps the carving and drops the rest, and ambient+// occlusion from the field itself is baked into the valleys of the folds.+const len3 = (x, y, z) => Math.sqrt(x * x + y * y + z * z);+const smin = (a, b, k) => { const h = Math.max(k - Math.abs(a - b), 0) / k; return Math.min(a, b) - h * h * k * 0.25; };+const smax = (a, b, k) => -smin(-a, -b, k);+const sdEll = (x, y, z, rx, ry, rz) => { const a = x / rx, b = y / ry, c = z / rz, k0 = Math.sqrt(a * a + b * b + c * c), d = a / rx, e = b / ry, f = c / rz, k1 = Math.sqrt(d * d + e * e + f * f); return k1 > 1e-9 ? k0 * (k0 - 1) / k1 : -Math.min(rx, ry, rz); };+// Rounded cone between two points: the workhorse of fingers, locks, lids and sleeves+function sdCone(x, y, z, ax, ay, az, bx, by, bz, r1, r2) {+ const px = x - ax, py = y - ay, pz = z - az, dx = bx - ax, dy = by - ay, dz = bz - az;+ const t = clamp((px * dx + py * dy + pz * dz) / (dx * dx + dy * dy + dz * dz));+ return len3(px - dx * t, py - dy * t, pz - dz * t) - (r1 + (r2 - r1) * t);+}+// Surface nets over a sparse grid: only cells near the surface are ever evaluated+function polygonize(F, box, cell, bandK = 1.75) {+ const [x0, y0, z0, x1, y1, z1] = box, nx = Math.ceil((x1 - x0) / cell) + 2, ny = Math.ceil((y1 - y0) / cell) + 2, nz = Math.ceil((z1 - z0) / cell) + 2;+ const N = nx * ny * nz, val = new Float32Array(N), seen = new Uint8Array(N), cellV = new Int32Array(N).fill(-1);+ const b4 = 3.46 * cell * bandK, b2 = 1.73 * cell * bandK, active = [];+ const at = (i, j, k) => { const q = i + nx * (j + ny * k); if (!seen[q]) { seen[q] = 1; val[q] = F(x0 + i * cell, y0 + j * cell, z0 + k * cell); } return val[q]; };+ // Two-level descent: a coarse block, then its octants, then cells, so the field is only asked near its surface+ for (let kc = 0; kc < nz - 1; kc += 4) for (let jc = 0; jc < ny - 1; jc += 4) for (let ic = 0; ic < nx - 1; ic += 4) {+ if (Math.abs(F(x0 + (ic + 2) * cell, y0 + (jc + 2) * cell, z0 + (kc + 2) * cell)) >= b4) continue;+ for (let k2 = kc; k2 < Math.min(kc + 4, nz - 1); k2 += 2) for (let j2 = jc; j2 < Math.min(jc + 4, ny - 1); j2 += 2) for (let i2 = ic; i2 < Math.min(ic + 4, nx - 1); i2 += 2) {+ if (Math.abs(F(x0 + (i2 + 1) * cell, y0 + (j2 + 1) * cell, z0 + (k2 + 1) * cell)) >= b2) continue;+ for (let k = k2; k < Math.min(k2 + 2, nz - 1); k++) for (let j = j2; j < Math.min(j2 + 2, ny - 1); j++) for (let i = i2; i < Math.min(i2 + 2, nx - 1); i++) active.push(i, j, k);+ }+ }+ const P = [], I = [], E = [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]], v = new Float64Array(8);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ let mask = 0;+ for (let c = 0; c < 8; c++) { const d = at(i + (c & 1), j + ((c >> 1) & 1), k + ((c >> 2) & 1)); v[c] = d; if (d < 0) mask |= 1 << c; }+ if (mask === 0 || mask === 255) continue;+ let sx = 0, sy = 0, sz = 0, n = 0;+ for (let e = 0; e < 12; e++) {+ const c0 = E[e][0], c1 = E[e][1], d0 = v[c0], d1 = v[c1];+ if ((d0 < 0) === (d1 < 0)) continue;+ const t = d0 / (d0 - d1);+ sx += lerp(c0 & 1, c1 & 1, t); sy += lerp((c0 >> 1) & 1, (c1 >> 1) & 1, t); sz += lerp((c0 >> 2) & 1, (c1 >> 2) & 1, t); n++;+ }+ cellV[i + nx * (j + ny * k)] = P.length / 3;+ P.push(x0 + (i + sx / n) * cell, y0 + (j + sy / n) * cell, z0 + (k + sz / n) * cell);+ }+ const quad = (a, b, c, d, flip) => { if (a < 0 || b < 0 || c < 0 || d < 0) return; if (flip) I.push(a, c, b, a, d, c); else I.push(a, b, c, a, c, d); };+ const cv = (i, j, k) => (i < 0 || j < 0 || k < 0 ? -1 : cellV[i + nx * (j + ny * k)]);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ const s0 = at(i, j, k) < 0;+ if (s0 !== (at(i + 1, j, k) < 0)) quad(cv(i, j - 1, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i, j - 1, k), !s0);+ if (s0 !== (at(i, j + 1, k) < 0)) quad(cv(i - 1, j, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i - 1, j, k), s0);+ if (s0 !== (at(i, j, k + 1) < 0)) quad(cv(i - 1, j - 1, k), cv(i, j - 1, k), cv(i, j, k), cv(i - 1, j, k), !s0);+ }+ return { P, I };+}+function meshToGeo(m) {+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(m.P), 3));+ g.setIndex(new THREE.BufferAttribute(m.P.length / 3 > 65535 ? new Uint32Array(m.I) : new Uint16Array(m.I), 1));+ g.computeVertexNormals();+ return g;+}+const AO_STEPS = [0.016, 0.05, 0.13];+// Extract, simplify, and write into a batch with field normals and field ambient occlusion+async function carveInto(b, F, box, cell, tris, xf, occ, opt = {}) {+ const raw = polygonize(F, box, cell);+ if (!raw.I.length) return;+ if (opt.cull) { // drop a region that a finer pass will carve instead+ const I2 = [], used = new Int32Array(raw.P.length / 3).fill(-1), P2 = [], mapV = (v) => { if (used[v] < 0) { used[v] = P2.length / 3; P2.push(raw.P[v * 3], raw.P[v * 3 + 1], raw.P[v * 3 + 2]); } return used[v]; };+ for (let t = 0; t < raw.I.length; t += 3) {+ const a = raw.I[t], c = raw.I[t + 1], e = raw.I[t + 2], mx = (raw.P[a * 3] + raw.P[c * 3] + raw.P[e * 3]) / 3, my = (raw.P[a * 3 + 1] + raw.P[c * 3 + 1] + raw.P[e * 3 + 1]) / 3, mz = (raw.P[a * 3 + 2] + raw.P[c * 3 + 2] + raw.P[e * 3 + 2]) / 3;+ if (!opt.cull(mx, my, mz)) I2.push(mapV(a), mapV(c), mapV(e));+ }+ raw.P = P2; raw.I = I2;+ }+ let g = meshToGeo(raw);+ if (raw.I.length / 3 > tris) g = await simplifyGeometry(g, { targetTriangles: tris });+ const pos = g.attributes.position, idx = g.index, base = b.count, h = cell * (opt.smooth || 0.6), aoK = opt.ao === undefined ? 0.5 : opt.ao, aoMin = opt.aoMin === undefined ? 0.16 : opt.aoMin;+ b.at(xf || M.I(), () => {+ for (let i = 0; i < pos.count; i++) {+ let x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i);+ const f0 = F(x, y, z), g0 = F(x + h, y, z) - F(x - h, y, z), g1 = F(x, y + h, z) - F(x, y - h, z), g2 = F(x, y, z + h) - F(x, y, z - h), l = len3(g0, g1, g2) || 1;+ const nx = g0 / l, ny = g1 / l, nz = g2 / l;+ // settle the vertex onto the true surface: this is what keeps fold valleys crisp+ x -= nx * f0; y -= ny * f0; z -= nz * f0;+ const O = occ || F;+ const wx = b.m[0] * x + b.m[1] * y + b.m[2] * z + b.m[3], wy = b.m[4] * x + b.m[5] * y + b.m[6] * z + b.m[7], wz = b.m[8] * x + b.m[9] * y + b.m[10] * z + b.m[11];+ const nm = b.nm, ax = nm[0] * nx + nm[1] * ny + nm[2] * nz, ay = nm[3] * nx + nm[4] * ny + nm[5] * nz, az = nm[6] * nx + nm[7] * ny + nm[8] * nz, al = len3(ax, ay, az) || 1;+ let o = 0, w = 1;+ for (let q = 0; q < 3; q++) { const d = AO_STEPS[q]; o += w * Math.max(0, d - O(wx + ax / al * d, wy + ay / al * d, wz + az / al * d)) / d; w *= 0.66; }+ const ao = clamp(1 - o * aoK, aoMin, 1);+ // UV by a slanted planar projection: no seam on a figure, the grain runs up the stone+ b.v(x, y, z, nx, ny, nz, wz * 0.8 + wx * 0.6, wy, ao);+ }+ for (let t = 0; t < idx.count; t += 3) b.tri(base + idx.getX(t), base + idx.getX(t + 1), base + idx.getX(t + 2));+ });+}++// ---- The head: a real skull, brow, eyes with lids, nose, lips, chin, hair, beard -------------+const LID_U = [[0.0795, 0.0035, 0.0165, 0.003], [0.0848, 0.0112, 0.0238, 0.0038], [0.0866, 0.0152, 0.0338, 0.0043], [0.0838, 0.0122, 0.0438, 0.0036], [0.0755, 0.0062, 0.052, 0.0024]];+const LID_L = [[0.0795, 0.0035, 0.0165, 0.0022], [0.0836, -0.0012, 0.0245, 0.0028], [0.0852, -0.0032, 0.034, 0.003], [0.0822, -0.0006, 0.044, 0.0026], [0.0755, 0.0062, 0.052, 0.0022]];+function headField(o) {+ const sm = o.smile || 0, cheek = o.gaunt ? 0.5 : 1, up = sm * 0.0065;+ const locks = [], rng = mkRng(o.seed * 37 + 11);+ // Long locks fall from the temples and the back of the head to the shoulders+ if (o.hair === 'long' || o.hair === 'curled') {+ const nL = o.hair === 'curled' ? 7 : 9;+ for (let i = 0; i < nL; i++) {+ const f = i / (nL - 1), a = lerp(1.05, 2.95, f), r0 = 0.092, wob = 0.012 + rng() * 0.008, dr = o.hair === 'curled' ? -0.165 : -0.235;+ for (const s of [-1, 1]) {+ const p0 = [r0 * Math.cos(a) - 0.012, 0.02 + rng() * 0.02, s * r0 * Math.sin(a) * 0.9];+ const p1 = [p0[0] * 1.14 - 0.008, p0[1] + dr * 0.5, p0[2] * 1.18 + s * wob];+ const p2 = [p0[0] * 1.02 - 0.03 * (1 - f * 0.5), p0[1] + dr, p0[2] * (1.28 - f * 0.3) - s * wob];+ locks.push([p0, p1, 0.02, 0.016], [p1, p2, 0.016, o.hair === 'curled' ? 0.012 : 0.007]);+ }+ }+ }+ if (o.hair === 'curls') { // rolled waves that frame the face: the smiling angel, the Child+ for (let i = 0; i < 11; i++) { const f = i / 10 - 0.5, a = f * 2.5, p0 = [0.104 * Math.cos(a) * 0.72 + 0.006, 0.094 - f * f * 0.11, 0.088 * Math.sin(a)], p1 = [p0[0] + 0.012, p0[1] - 0.03 - Math.abs(f) * 0.03, p0[2] * 1.16]; locks.push([p0, p1, 0.0165, 0.0115]); }+ for (let i = 0; i < 12; i++) { const a = 1.5 + i * 0.29, s = i % 2 ? 1 : -1, p0 = [0.09 * Math.cos(a) - 0.012, 0.0, s * 0.09 * Math.sin(a)], p1 = [p0[0] * 1.1, -0.062, p0[2] * 1.14]; locks.push([p0, p1, 0.019, 0.013]); }+ }+ const beardL = [];+ if (o.beard) {+ const nB = 7, L = o.beard === 2 ? 0.17 : 0.09;+ for (let i = 0; i < nB; i++) {+ const f = i / (nB - 1) - 0.5, z0 = f * 0.098, x0 = 0.078 - Math.abs(f) * 0.05, wob = (i % 2 ? 1 : -1) * 0.005;+ // separate locks with grooves between them; each one twists and tapers to a point+ beardL.push([[x0, -0.07 - Math.abs(f) * 0.012, z0], [x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], 0.0102, 0.0098]);+ beardL.push([[x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], [x0 - 0.006 + (o.beard === 2 ? 0.008 : 0), -0.07 - L - Math.abs(f) * 0.012, z0 * 0.5], 0.0098, 0.0035]);+ }+ }+ // Each lock is skipped outright when the query point is beyond its bounding sphere+ const sphere = (L) => { const dx = L[0][0] - L[1][0], dy = L[0][1] - L[1][1], dz = L[0][2] - L[1][2], R = Math.sqrt(dx * dx + dy * dy + dz * dz) / 2 + Math.max(L[2], L[3]) + 0.03; return [(L[0][0] + L[1][0]) / 2, (L[0][1] + L[1][1]) / 2, (L[0][2] + L[1][2]) / 2, R * R]; };+ const lockB = locks.map(sphere), beardB = beardL.map(sphere);+ const nearS = (S, x, y, z) => { const a = x - S[0], b = y - S[1], c = z - S[2]; return a * a + b * b + c * c < S[3]; };+ return (x, y, z) => {+ const az = Math.abs(z);+ let d = sdEll(x + 0.012, y - 0.03, z, 0.098, 0.098, 0.08); // cranium+ d = smin(d, sdEll(x - 0.018, y + 0.022, z, 0.074, 0.094, 0.0655), 0.02); // face mass+ d = smin(d, sdEll(x - 0.03, y + 0.076, z, 0.056, 0.046, 0.05 * (o.gaunt ? 0.93 : 1)), 0.018); // jaw+ d = smin(d, sdEll(x - 0.0675, y + 0.1, z, 0.019, 0.0175, 0.02), 0.012); // chin+ d = smin(d, sdCone(x, y, az, 0.0745, 0.031, 0, 0.066, 0.033, 0.05, 0.0118, 0.0088), 0.012); // brow ridge+ d = smin(d, sdEll(x - 0.054, y + 0.018 - up, az - 0.044, 0.026, 0.017 + sm * 0.003, 0.02), 0.015 * cheek); // cheekbone+ if (o.gaunt) d = smax(d, -sdEll(x - 0.05, y + 0.058, az - 0.058, 0.03, 0.022, 0.012), 0.012);+ if (x > 0.04 && y > -0.03 && y < 0.05 && az < 0.082) {+ d = smax(d, -sdEll(x - 0.0875, y - 0.0075, az - 0.034, 0.0225, 0.0118, 0.0215), 0.008); // eye socket+ d = smin(d, len3(x - 0.0655, y - 0.0065, az - 0.034) - 0.0178, 0.0018); // eyeball+ d = smax(d, -(len3(x - 0.0862, y - 0.0065, az - 0.034) - 0.0052), 0.0016); // the drilled pupil+ // lids: a carved almond, the upper rim heavier, meeting at the corners+ for (let i = 0; i < 4; i++) { const a = LID_U[i], c = LID_U[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0026); }+ for (let i = 0; i < 4; i++) { const a = LID_L[i], c = LID_L[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0022); }+ }+ if (x > 0.055 && y > -0.105 && y < 0.045 && az < 0.055) {+ // nose: bridge to tip, wings, nostrils+ d = smin(d, sdCone(x, y, z, 0.0835, 0.0225, 0, 0.105, -0.021, 0, 0.0058, 0.0092), 0.0085);+ d = smin(d, len3(x - 0.0925, y + 0.0295, az - 0.0108) - 0.0064, 0.005);+ d = smax(d, -(len3(x - 0.0975, y + 0.0355, az - 0.0062) - 0.0033), 0.002);+ // philtrum+ d = smax(d, -sdCone(x, y, z, 0.0975, -0.042, 0, 0.0965, -0.052, 0, 0.0028, 0.0034), 0.0035);+ // lips: upper in two wings, a fuller lower, a cut between; corners lift for a smile+ const my = -0.0615;+ d = smin(d, sdCone(x, y, az, 0.0925, my + 0.004, 0, 0.0815, my + 0.001 + up, 0.0215 + sm * 0.003, 0.0058, 0.0026), 0.005);+ d = smin(d, sdCone(x, y, az, 0.0915, my - 0.0085, 0, 0.0825, my - 0.003 + up, 0.0185 + sm * 0.003, 0.0072, 0.0028), 0.005);+ d = smax(d, -sdCone(x, y, az, 0.0965, my - 0.002, 0, 0.0825, my + up * 1.15, 0.0235 + sm * 0.004, 0.0017, 0.0021), 0.0022);+ d = smax(d, -sdEll(x - 0.0915, y + 0.0845, z, 0.012, 0.0055, 0.02), 0.006); // hollow under the lip+ }+ if (sm) d = smin(d, sdEll(x - 0.07, y + 0.044 - up * 2, az - 0.038, 0.02, 0.015, 0.016), 0.012); // the raised cheek of a smile+ // ear+ d = smin(d, sdEll(x + 0.012, y - 0.002, az - 0.08, 0.016, 0.03, 0.0085), 0.004);+ // neck, into the shoulders+ d = smin(d, sdCone(x, y, z, -0.01, -0.075, 0, -0.022, -0.215, 0, 0.041, 0.052), 0.018);+ // ---- hair ----+ if (o.hair && o.hair !== 'none') {+ const faceOpen = sdEll(x - 0.094, y + 0.012, z, 0.08, o.hair === 'curls' ? 0.106 : 0.118, 0.073);+ let h = sdEll(x + 0.014, y - 0.034, z, 0.104, 0.104, 0.09);+ if (o.hair === 'tonsure') h = smax(h, -sdEll(x + 0.012, y - 0.135, z, 0.075, 0.045, 0.062), 0.006);+ const an = Math.atan2(z, x + 0.014);+ if (o.hair !== 'curls') {+ const part = Math.exp(-(z * z) / 0.00007) * sstep(0.03, 0.11, y);+ h += 0.0028 * Math.sin(an * 22 + y * 34) + 0.007 * part;+ } else {+ h += 0.006 * Math.sin(an * 11 + y * 22) * Math.sin(y * 27 + an * 3);+ }+ for (let i = 0; i < locks.length; i++) { if (!nearS(lockB[i], x, y, z)) continue; const L = locks[i]; h = smin(h, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0055); }+ h = smax(h, -faceOpen, 0.0055);+ d = smin(d, h, 0.0025);+ }+ // ---- beard and moustache ----+ if (o.beard) {+ for (let i = 0; i < beardL.length; i++) { if (!nearS(beardB[i], x, y, z)) continue; const L = beardL[i]; d = smin(d, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0022); }+ d = smin(d, sdCone(x, y, az, 0.0955, -0.0475, 0.002, 0.081, -0.0625, 0.031, 0.0046, 0.003), 0.0025);+ d = smin(d, sdEll(x - 0.05, y + 0.058, az - 0.058, 0.022, 0.03, 0.012), 0.008); // sideburn into the jaw+ }+ // ---- veil: falls from the crown to the shoulders, cut open round the face ----+ if (o.veil) {+ // A soft hood: close to the skull, widening over the shoulders, its hem falling in loose folds+ const an = Math.atan2(z, x + 0.02);+ let w = smin(sdEll(x + 0.012, y - 0.032, z, 0.11, 0.108, 0.094), sdCone(x, y, z, -0.022, -0.05, 0, -0.038, -0.3, 0, 0.09, 0.158), 0.05);+ w -= 0.012 * (Math.sqrt(Math.abs(Math.sin(an * 4.5 + y * 2.2))) - 0.55) * sstep(-0.01, -0.16, y);+ w = smax(w, -0.325 + 0.022 * Math.sin(an * 5) - y, 0.008);+ const open = sdEll(x - 0.104, y + 0.016, z, 0.1, 0.116, 0.0715);+ d = smin(d, smax(w, -open, 0.004), 0.0015);+ }+ // ---- mitre: two high peaks of stiff cloth, the bands left proud ----+ if (o.mitre) {+ // seen from the front a pointed arch, wide at the brow; seen from the side, two thin peaks+ const px = x + 0.012, py = y - 0.058, f = clamp(py / 0.27), w0 = 0.098 * (1 - Math.pow(f, 1.9)), dp = 0.102 * (1 - f * 0.86), lens = Math.abs(px) + az * 0.2;+ let m = Math.max(lens - dp, az - w0, -py);+ m = Math.max(m, py - 0.272);+ const band = Math.exp(-((py - 0.018) * (py - 0.018)) / 0.00016);+ m -= 0.0045 * band + 0.0038 * Math.exp(-(z * z) / 0.00012);+ d = smin(d, m, 0.004);+ }+ return d * 0.92;+ };+}++// ---- The hand: palm and jointed fingers that actually close on what they hold -------------+const HAND_POSES = {+ // [curl1, curl2, curl3] per finger (index..little), thumb [x,y,z of tip, spread]+ grip: { f: [[68, 88, 62], [72, 92, 64], [76, 92, 64], [80, 88, 62]], t: 'over' },+ support: { f: [[10, 18, 12], [12, 20, 14], [14, 22, 14], [18, 24, 16]], t: 'open' },+ edge: { f: [[46, 40, 26], [48, 42, 26], [50, 44, 28], [54, 44, 28]], t: 'open' },+ bless: { f: [[6, 8, 6], [8, 10, 8], [62, 90, 60], [68, 92, 60]], t: 'over' },+ rest: { f: [[28, 40, 26], [32, 42, 28], [36, 44, 28], [40, 46, 28]], t: 'open' },+};+function handField(pose, thumb = 1) {+ const P0 = HAND_POSES[pose], kn = [[0.078, 0.003, -0.027], [0.083, 0.004, -0.009], [0.08, 0.003, 0.009], [0.073, 0.001, 0.026]], segL = [[0.036, 0.024, 0.019], [0.04, 0.027, 0.02], [0.037, 0.025, 0.019], [0.029, 0.019, 0.016]], segs = [];+ for (let f = 0; f < 4; f++) {+ let ang = 0, p = kn[f].slice();+ for (let s = 0; s < 3; s++) {+ ang += rad(P0.f[f][s]);+ const q = [p[0] + Math.cos(ang) * segL[f][s], p[1] - Math.sin(ang) * segL[f][s], p[2]];+ segs.push([p, q, s === 0 ? 0.0098 : s === 1 ? 0.0088 : 0.0078, s === 2 ? 0.0066 : s === 0 ? 0.0088 : 0.0078]);+ p = q;+ }+ }+ const tb = [0.022, -0.006, thumb * 0.03], tm = P0.t === 'over' ? [0.052, -0.03, thumb * 0.033] : [0.05, -0.012, thumb * 0.054], tt = P0.t === 'over' ? [0.078, -0.044, thumb * 0.012] : [0.082, -0.016, thumb * 0.056];+ return (x, y, z) => {+ let d = Math.max(Math.abs(x - 0.04) - 0.043, Math.abs(y) - 0.0125, Math.abs(z) - 0.036) - 0.006; // palm, gently rounded+ d = smin(d, sdCone(x, y, z, -0.02, 0, 0, 0.02, 0, 0, 0.026, 0.033), 0.012); // wrist heel+ for (let i = 0; i < segs.length; i++) { const S = segs[i]; d = smin(d, sdCone(x, y, z, S[0][0], S[0][1], S[0][2], S[1][0], S[1][1], S[1][2], S[2], S[3]), 0.0022); }+ d = smin(d, sdCone(x, y, z, tb[0], tb[1], tb[2], tm[0], tm[1], tm[2], 0.0145, 0.0098), 0.006);+ d = smin(d, sdCone(x, y, z, tm[0], tm[1], tm[2], tt[0], tt[1], tt[2], 0.0098, 0.0078), 0.002);+ return d;+ };+}+// Heads are shared meshes placed on their own node: the same face can turn and tilt per figure at no byte cost.+const HEAD_MESH = {};+const HEADS = {+ bearded: { hair: 'long', beard: 1, seed: 2 },+ mitred: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 },+ royal: { hair: 'curled', beard: 2, seed: 10 },+ veiled: { veil: 1, smile: 0.25, seed: 8 },+ angelic: { hair: 'curls', smile: 1, seed: 13 },+};+async function headBatch(key) {+ if (HEAD_MESH[key]) return HEAD_MESH[key];+ const hO = HEADS[key], b = new Batch(TILE.fine), hF = headField(hO);+ const hB = [-0.19, hO.veil ? -0.36 : -0.26, -0.2, 0.17, hO.mitre ? 0.36 : 0.16, 0.2];+ // Hair and skull at a working grid, then the face alone on the finest grid with most of the triangles+ const FB = [0.024, -0.13, -0.08, 0.128, 0.088, 0.08];+ const inFace = (x, y, z) => x > FB[0] + 0.003 && y > FB[1] + 0.003 && y < FB[4] - 0.003 && z > FB[2] + 0.003 && z < FB[5] - 0.003;+ await carveInto(b, hF, hB, 0.0048, hO.veil ? 1300 : hO.beard ? 1900 : 1700, null, hF, { smooth: 1.4, ao: 0.34, aoMin: 0.3, cull: inFace });+ await carveInto(b, hF, FB, 0.0024, hO.beard ? 2500 : 2700, null, hF, { smooth: 0.9, ao: 0.3, aoMin: 0.34 });+ b.isFigure = true;+ HEAD_MESH[key] = b;+ return b;+}+const HAND_MESH = {};+async function handBatch(pose, thumb) { // hands are cached: a grip is a grip, whoever's it is+ const key = pose + thumb;+ if (!HAND_MESH[key]) { const b = new Batch(TILE.fine), F = handField(pose, thumb); await carveInto(b, F, [-0.05, -0.1, -0.1, 0.16, 0.05, 0.1], 0.0034, 520); HAND_MESH[key] = b; }+ return HAND_MESH[key];+}+function copyBatch(dst, src, m, shadeFn) { // transform a finished batch into another+ const base = dst.count;+ dst.at(m, () => {+ for (let i = 0; i < src.count; i++) { const i3 = i * 3, i2 = i * 2; dst.v(src.P[i3], src.P[i3 + 1], src.P[i3 + 2], src.N[i3], src.N[i3 + 1], src.N[i3 + 2], src.UV[i2] * src.tile, src.UV[i2 + 1] * src.tile, (src.C[i3] / 255) * (shadeFn ? shadeFn(src.P[i3], src.P[i3 + 1], src.P[i3 + 2]) : 1)); }+ for (let t = 0; t < src.I.length; t += 3) dst.tri(base + src.I[t], base + src.I[t + 1], base + src.I[t + 2]);+ });+}+const frameOf = (p, X, Y) => { // rigid frame: origin p, local X and Y given, Z = X x Y+ const lx = len3(X[0], X[1], X[2]), xn = [X[0] / lx, X[1] / lx, X[2] / lx], d = Y[0] * xn[0] + Y[1] * xn[1] + Y[2] * xn[2];+ let yy = [Y[0] - d * xn[0], Y[1] - d * xn[1], Y[2] - d * xn[2]]; const ly = len3(yy[0], yy[1], yy[2]); yy = yy.map((c) => c / ly);+ const z = [xn[1] * yy[2] - xn[2] * yy[1], xn[2] * yy[0] - xn[0] * yy[2], xn[0] * yy[1] - xn[1] * yy[0]];+ return [xn[0], yy[0], z[0], p[0], xn[1], yy[1], z[1], p[1], xn[2], yy[2], z[2], p[2]];+};+const invRigid = (m) => (x, y, z) => { const a = x - m[3], b = y - m[7], c = z - m[11]; return [m[0] * a + m[4] * b + m[8] * c, m[1] * a + m[5] * b + m[9] * c, m[2] * a + m[6] * b + m[10] * c]; };++// ---- The people. Bodies are long columns of cloth; each differs in dress, stance, head, gesture ----+const FIGURES = {+ apostle: { seed: 2, sway: 0.018, dress: 'mantle', headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 2 }, hold: 'key' },+ bishop: { seed: 4, sway: 0.008, dress: 'chasuble', headKey: 'mitred', head: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 }, hold: 'crozier' },+ king: { seed: 10, sway: 0.022, dress: 'royal', headKey: 'royal', head: { hair: 'curled', beard: 2, crown: 1, seed: 10 }, hold: 'sceptre' },+ queen: { seed: 5, sway: 0.03, dress: 'royal', slim: 0.93, headKey: 'veiled', head: { veil: 1, crown: 1, seed: 5, turn: 8 }, hold: 'mantle' },+ angel: { seed: 13, sway: 0.05, dress: 'angel', slim: 0.94, wings: 1, headKey: 'angelic', head: { hair: 'curls', smile: 1, seed: 13, tilt: 11, turn: 15 }, hold: 'scroll' },+ virgin: { seed: 8, sway: 0.062, dress: 'mantle', slim: 0.95, headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, tilt: 9, turn: -16 }, hold: 'child' },+};+const FIG_ALIAS = { peter: 'apostle', paul: 'apostle', prophet: 'apostle', deacon: 'apostle', martyr: 'queen', kingA: 'king', kingB: 'king', kingC: 'king', christ: 'seated' };++function bodyField(o) {+ const yN = 1.7, rng = mkRng(o.seed * 991 + 7), ph = []; for (let i = 0; i < 8; i++) ph.push(rng() * TAU);+ const sw = o.sway || 0, sl = o.slim || 1, L = 256;+ const keys = [[0, 0.178, 0.205, 0.218], [0.05, 0.165, 0.192, 0.204], [0.3, 0.138, 0.165, 0.172], [0.52, 0.124, 0.15, 0.158], [0.66, 0.112, 0.128, 0.146], [0.8, 0.116, 0.12, 0.15], [0.885, 0.108, 0.112, 0.15], [0.94, 0.09, 0.094, 0.13], [0.975, 0.066, 0.07, 0.09], [1, 0.044, 0.046, 0.05]];+ const Af = new Float32Array(L), Ab = new Float32Array(L), Bs = new Float32Array(L), Cx = new Float32Array(L), Cz = new Float32Array(L), Amp = new Float32Array(L);+ for (let i = 0; i < L; i++) {+ const t = i / (L - 1), k = keyf(keys, t);+ Af[i] = k[0]; Ab[i] = k[1]; Bs[i] = k[2] * sl;+ Cx[i] = 0.014 * Math.sin(t * PI);+ Cz[i] = sw * (Math.sin(t * PI * 0.92) * 1.15 - 0.62 * Math.sin(t * PI * 2));+ // pleats deepest over the shins, gathered at the belt, slight in the bodice+ Amp[i] = t < 0.645 ? lerp(0.05, 0.03, sstep(0, 0.6, t)) : lerp(0.014, 0.004, sstep(0.66, 0.9, t));+ }+ const n1 = o.dress === 'chasuble' ? 16 : 14, n2 = 25, yB = 0.655 * yN;+ const dress = o.dress, zc = Cz[Math.round(0.86 * (L - 1))], cxS = Cx[Math.round(0.86 * (L - 1))];+ const Rs = 0.142 * sl;+ const arms = armPose(o, Rs, zc, cxS);+ // Feet: pointed medieval shoes emerge from the hem+ const foot = (x, y, z) => { const az = Math.abs(z - Cz[0]); let d = sdCone(x, y, az, 0.02, 0.042, 0.082, 0.2, 0.024, 0.098, 0.05, 0.024); d = smin(d, sdCone(x, y, az, 0.0, 0.05, 0.082, 0.02, 0.2, 0.07, 0.044, 0.04), 0.02); d = smax(d, -sdEll(x - 0.11, y + 0.01, az - 0.09, 0.08, 0.02, 0.1), 0.004); return Math.max(d, -y); };+ return {+ arms,+ F: (x, y, z) => {+ const t = clamp(y / yN), i = (t * (L - 1)) | 0, px = x - Cx[i], pz = z - Cz[i], rho = Math.sqrt(px * px + pz * pz) + 1e-6, c = px / rho, s = pz / rho, a = c > 0 ? Af[i] : Ab[i], b = Bs[i];+ const R = 1 / Math.sqrt((c * c) / (a * a) + (s * s) / (b * b)), th = Math.atan2(pz, px);+ // ---- the tunic: tube pleats with valleys you could put a finger in ----+ const u1 = th * n1 * 0.5 + ph[0] + y * 0.5 * Math.sign(sw || 1), u2 = th * n2 * 0.5 + ph[1] - y * 0.9;+ const p1 = Math.sqrt(Math.abs(Math.sin(u1))), p2 = Math.sqrt(Math.abs(Math.sin(u2)));+ let r = R + Amp[i] * (p1 * 0.76 + p2 * 0.24 - 0.64);+ r += sstep(0.14, 0, y) * (0.018 + 0.016 * Math.sin(th * 7 + ph[4])); // the skirt spreads and breaks at the foot+ r += 0.008 * Math.exp(-((y - yB) * (y - yB)) / 0.00018); // belt+ r -= 0.012 * sstep(0.03, 0, Math.abs(y - yB) - 0.012) * 0.2;+ r += 0.006 * Math.exp(-((y - yN + 0.075) * (y - yN + 0.075)) / 0.00012); // collar+ let d = rho - r;+ const hem = 0.055 + 0.038 * p1 * (c > -0.2 ? 1 : 0.4);+ d = smax(d, hem - y, 0.006);+ // ---- the over-garment ----+ if (dress === 'mantle') {+ const up = 0.78 * yN + 0.2 * yN * Math.max(0, Math.sin(-th * Math.sign(sw || 1) + 0.5)) + 0.25 * sstep(1.2, 2.6, Math.abs(th)), low = 0.33 * yN - 0.18 * yN * sstep(0.5, 2.8, Math.abs(th)) + 0.02 * Math.sin(th * 11 + ph[5]) + 0.012 * Math.sin(th * 23);+ const reg = Math.max(low - y, y - up);+ // Folds pulled on the diagonal from the held hip to the far shoulder: two scales, never in step+ const th0 = 0.25 * Math.sign(sw || 1), q = y + 0.15 * th * Math.sign(sw || 1) + 0.055 * (th - th0) * (th - th0);+ const swag = 0.62 * Math.sqrt(Math.abs(Math.sin(q * 12.5 + ph[6]))) + 0.38 * Math.sqrt(Math.abs(Math.sin(q * 27 + ph[7]))), back = Math.sqrt(Math.abs(Math.sin(th * 5 + ph[3])));+ const fr = sstep(1.9, 1.1, Math.abs(th));+ const rm = R + 0.03 + 0.04 * lerp(back - 0.6, swag - 0.58, fr) * sstep(up + 0.01, up - 0.08, y);+ d = smin(d, smax(rho - rm, reg, 0.007), 0.002);+ } else if (dress === 'chasuble') {+ // alb to the feet, dalmatic to the shin, the bell of the chasuble hanging from the arms in U folds+ const dal = Math.max(0.3 * yN + 0.012 * Math.sin(th * 9) - y, y - 0.86 * yN);+ d = smin(d, smax(rho - (R + 0.016 + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 6 + ph[2]))) - 0.5)), dal, 0.005), 0.0015);+ const lowC = (0.42 + 0.26 * sstep(0.2, 1.5, Math.abs(th)) - 0.2 * sstep(1.7, 3.0, Math.abs(th))) * yN + 0.012 * Math.sin(th * 13);+ const swag = 0.65 * Math.sqrt(Math.abs(Math.sin((y + 0.2 * th * th) * 27 + ph[6]))) + 0.35 * Math.sqrt(Math.abs(Math.sin((y + 0.13 * th * th) * 44 + ph[7])));+ const rc = R + 0.03 * sstep(yN, 0.9 * yN, y) + 0.022 * (swag - 0.58) * sstep(0.84 * yN, 0.74 * yN, y) * (1 - 0.6 * sstep(0, 0.2, 0.2 - Math.abs(th)));+ d = smin(d, smax(rho - rc, lowC - y, 0.007), 0.003);+ // the orphrey: a Y-band across the breast and down the front, left proud+ const py = y - 0.79 * yN;+ if (Math.abs(th) < 1.2 && y > lowC - 0.01 && y < 0.94 * yN) d = smin(d, Math.max(rho - rc - 0.011, py > 0 ? Math.abs(Math.abs(th) - py * 4.2) - 0.085 : Math.abs(th) - 0.09), 0.003);+ } else if (dress === 'royal') {+ // a heavy mantle open down the front, thrown back over the shoulders, deep folds to the floor+ const open = lerp(0.72, 0.28, sstep(0.05, 0.86, t)) + 0.05 * Math.sin(y * 19 + ph[2]);+ const reg = Math.max(open - Math.abs(th - 0.05), 0.05 - y);+ const hv = Math.sqrt(Math.abs(Math.sin(th * 4 + ph[3] + y * 0.3)));+ const collar = 0.022 * Math.exp(-((y - 0.915 * yN) * (y - 0.915 * yN)) / 0.0012); // a soft roll at the neck+ const rm = R + 0.03 * sstep(yN, 0.9 * yN, y) + collar + sstep(0.84 * yN, 0.5 * yN, y) * 0.056 * (hv - 0.62);+ d = smin(d, smax(rho - rm, reg, 0.006), 0.004);+ } else if (dress === 'angel') {+ // a girdled tunic blown a little to one side: fine folds and a bloused top+ const blouse = 0.018 * Math.exp(-((y - yB - 0.06) * (y - yB - 0.06)) / 0.003);+ d -= blouse;+ const low = 0.74 * yN + 0.02 * Math.sin(th * 7) - y;+ d = smin(d, smax(rho - (R + 0.016 * sstep(yN, 0.9 * yN, y) + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 7 + ph[2]))) - 0.5) * sstep(0.9 * yN, 0.8 * yN, y)), low, 0.005), 0.003);+ }+ // ---- shoulders and sleeves ----+ const yS = yN - 0.155;+ for (let k = 0; k < 2; k++) { const sg = k ? 1 : -1; d = smin(d, sdEll(x - cxS, y - yS, z - zc - sg * Rs * 0.98, 0.07, 0.078, 0.058), 0.05); }+ for (let k = 0; k < arms.length; k++) {+ const A = arms[k];+ let ad = sdCone(x, y, z, A.S[0], A.S[1], A.S[2], A.E[0], A.E[1], A.E[2], 0.056, 0.05);+ // forearm in a sleeve that widens to the cuff, with a roll of cloth at the wrist+ const fx = A.W[0] - A.E[0], fy = A.W[1] - A.E[1], fz = A.W[2] - A.E[2], fl = fx * fx + fy * fy + fz * fz, ft = clamp(((x - A.E[0]) * fx + (y - A.E[1]) * fy + (z - A.E[2]) * fz) / fl);+ const qx = x - A.E[0] - fx * ft, qy = y - A.E[1] - fy * ft, qz = z - A.E[2] - fz * ft, ql = len3(qx, qy, qz);+ const rr = lerp(0.05, 0.062, ft) + 0.006 * Math.sin(ft * 15 + k) * (1 - ft) + 0.008 * Math.exp(-((ft - 0.97) * (ft - 0.97)) / 0.002);+ ad = smin(ad, ql - rr, 0.018);+ // the cloth that falls from under the forearm: a hanging curtain of pleats+ // longest under the wrist, thinning as it falls, its lower edge never straight+ if (A.drape) {+ const h = A.E[1] + fy * ft - y;+ if (h > -0.04 && h < A.drape * 1.1) {+ const fall = clamp(h / A.drape), crv = lerp(0.044, 0.016, fall) + 0.014 * (Math.sqrt(Math.abs(Math.sin(ft * 12 + h * 2.5 + k))) - 0.5);+ const curtain = len3(qx * 0.72, 0, qz) - crv;+ const edge = h - A.drape * (0.28 + 0.72 * ft) - 0.022 * Math.sin(ft * 11 + k * 2);+ ad = smin(ad, smax(curtain, edge, 0.012), 0.018);+ }+ }+ d = smin(d, ad, 0.014);+ }+ // ---- feet ----+ d = smin(d, foot(x, y, z), 0.006);+ return Math.max(d, y - yN - 0.03);+ },+ };+}+// Where the arms go, and what the hands do when they get there+function armPose(o, Rs, zc, cx) {+ const y = 1.55, shR = [cx, y, zc + Rs], shL = [cx, y, zc - Rs], arm = (S, E, W, drape) => ({ S, E, W, drape });+ switch (o.hold) {+ case 'key': return [arm(shR, [0.03, 1.2, zc + Rs + 0.05], [0.17, 1.32, zc + 0.12], 0.32), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.17, 1.14, zc - 0.07], 0.34)];+ case 'crozier': return [arm(shR, [0.04, 1.2, zc + Rs + 0.06], [0.2, 1.3, zc + 0.14], 0.3), arm(shL, [0.05, 1.22, zc - Rs - 0.03], [0.2, 1.42, zc - 0.05], 0.3)];+ case 'sceptre': return [arm(shR, [0.04, 1.18, zc + Rs + 0.06], [0.2, 1.28, zc + 0.15], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.03], [0.17, 1.18, zc - 0.05], 0.3)];+ case 'mantle': return [arm(shR, [0.04, 1.14, zc + Rs + 0.05], [0.18, 1.2, zc + 0.04], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.15, 1.4, zc - 0.1], 0.26)];+ case 'scroll': return [arm(shR, [0.05, 1.2, zc + Rs + 0.04], [0.2, 1.34, zc + 0.1], 0.28), arm(shL, [0.05, 1.2, zc - Rs - 0.04], [0.2, 1.22, zc - 0.08], 0.28)];+ case 'child': return [arm(shR, [0.05, 1.14, zc + Rs + 0.04], [0.2, 1.24, zc + 0.03], 0.32), arm(shL, [0.05, 1.14, zc - Rs - 0.02], [0.2, 1.18, zc - 0.13], 0.3)];+ case 'bless': return [arm(shR, [0.07, 1.22, zc + Rs + 0.09], [0.19, 1.5, zc + 0.15], 0.3), arm(shL, [0.03, 1.2, zc - Rs - 0.04], [0.17, 1.2, zc - 0.07], 0.34)];+ default: return [arm(shR, [0.02, 1.12, zc + Rs + 0.04], [0.15, 1.05, zc + 0.08], 0.3), arm(shL, [0.02, 1.12, zc - Rs - 0.04], [0.15, 1.05, zc - 0.08], 0.3)];+ }+}+// ---- Things held: real objects, modelled at the scale of the hands that grip them ---------+function addBook(b, m, o = {}) {+ b.at(m, () => {+ const w = o.w || 0.082, h = o.h || 0.112, th = o.th || 0.027;+ b.box(-h, h, -th, -th + 0.009, -w, w, { s: 0.85 }); b.box(-h, h, th - 0.009, th, -w, w, { s: 1 }); // boards+ b.box(-h + 0.006, h - 0.006, -th + 0.009, th - 0.009, -w + 0.006, w - 0.004, { s: 0.62 }); // pages+ b.box(-h, h, -th, th, -w - 0.009, -w, { s: 0.8 }); // spine+ for (const i of [-1, 1]) for (const k of [-1, 1]) b.at(M.T(i * (h - 0.026), th, k * (w - 0.026)), () => b.lathe([[0.016, 0], [0.012, 0.007], [0.0, 0.009]], 6));+ b.at(M.T(0, th, 0), () => b.lathe([[0.03, 0], [0.024, 0.006], [0.0, 0.009]], 8));+ });+}+function addAttrs(fig, o, arms) {+ const { stone: b, gold: g } = fig, R = arms[0], Lf = arms[1];+ switch (o.hold) {+ case 'key': {+ // The great key of Peter held upright in the fist, the book against the left forearm+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01];+ b.tube([[hp[0], hp[1] - 0.15, hp[2]], [hp[0], hp[1] + 0.2, hp[2]]], 0.0125, 8, { capStart: true });+ b.at(M.T(hp[0], hp[1] + 0.245, hp[2]), () => b.at(M.Rx(90), () => { b.lathe([[0.04, -0.01], [0.05, 0], [0.04, 0.01], [0.026, 0.01], [0.02, 0], [0.026, -0.01], [0.04, -0.01]], 14); }));+ b.box(hp[0] - 0.01, hp[0] + 0.01, hp[1] - 0.15, hp[1] - 0.075, hp[2], hp[2] + 0.07, { s: 0.9 });+ b.box(hp[0] - 0.012, hp[0] + 0.012, hp[1] - 0.112, hp[1] - 0.1, hp[2] + 0.03, hp[2] + 0.048, { s: 0.3 });+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2] + 0.0), M.Rz(-64), M.Ry(8)));+ break;+ }+ case 'crozier': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 2.02;+ b.tube([[hp[0], 0.04, hp[2]], [hp[0], top, hp[2]]], 0.0145, 8, { capStart: true });+ for (const yK of [hp[1] + 0.22, top - 0.05]) b.at(M.T(hp[0], yK, hp[2]), () => b.lathe([[0.016, -0.03], [0.034, 0], [0.016, 0.03]], 8));+ const vol = []; for (let i = 0; i <= 20; i++) { const t = i / 20, a = -PI * 0.5 + t * PI * 2.5, r = lerp(0.115, 0.036, t); vol.push([hp[0] + r * Math.cos(a) * 1.0 + 0.0, top + 0.12 + r * Math.sin(a), hp[2]]); }+ g.tube(vol, (t) => lerp(0.02, 0.012, t), 7, { capStart: true });+ for (let i = 2; i < 16; i += 3) { const p = vol[i]; addCrocket(g, M.of(M.T(p[0], p[1], p[2]), M.Rz((i / 20) * 400 - 90)), 0.06, 0); }+ break;+ }+ case 'sceptre': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 1.95;+ b.tube([[hp[0], hp[1] - 0.2, hp[2]], [hp[0], top, hp[2]]], 0.0125, 8, { capStart: true });+ g.at(M.T(hp[0], top, hp[2]), () => {+ g.lathe([[0.0125, 0], [0.03, 0.022], [0.038, 0.05], [0.026, 0.078], [0.01, 0.086]], 10);+ for (let i = 0; i < 3; i++) { const a = i * 120 + 90; g.at(M.Ry(a), () => g.tube(spline([[0, 0.07, 0], [0.038, 0.1, 0], [0.042, 0.15, 0], [0.018, 0.17, 0]], 2), (t) => [lerp(0.016, 0.003, t), lerp(0.024, 0.004, t)], 6)); }+ });+ break;+ }+ case 'scroll': {+ const p = [Lf.W[0] + 0.07, Lf.W[1], Lf.W[2]];+ const path = []; for (let i = 0; i <= 14; i++) { const t = i / 14; path.push([R.W[0] + 0.09 + 0.02 * Math.sin(t * 5), R.W[1] - t * 0.7 + 0.03, lerp(R.W[2] - 0.03, Lf.W[2] + 0.05, sstep(0, 0.5, t)) + 0.02 * Math.sin(t * 7)]); }+ const sheet = (off, flip) => b.grid((u, v) => { const k = u * 14, i = Math.min(13, Math.floor(k)), f = k - i, q = path[i], w = path[i + 1]; return [lerp(q[0], w[0], f) + off, lerp(q[1], w[1], f), lerp(q[2], w[2], f) + (v - 0.5) * 0.15, 0.96]; }, 14, 1, { flip });+ sheet(0, false); sheet(-0.004, true);+ b.at(M.of(M.T(path[14][0], path[14][1] - 0.015, path[14][2]), M.Rx(90)), () => b.lathe([[0.022, -0.085], [0.024, -0.08], [0.024, 0.08], [0.022, 0.085]], 8));+ break;+ }+ case 'bless': {+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2]), M.Rz(-64), M.Ry(4)));+ break;+ }+ default: break;+ }+}+const CROWN_FLEURS = 8;+function addCrown(g, m, R0, tall = 1) {+ g.at(m, () => {+ g.lathe([[R0, 0], [R0 + 0.009, 0.004], [R0 + 0.009, 0.028], [R0 + 0.015, 0.038], [R0 + 0.006, 0.042], [R0 - 0.004, 0.034], [R0 - 0.004, 0]], 26);+ // jewels set in the band+ for (let i = 0; i < 12; i++) { const a = i * TAU / 12; blob(g, [(R0 + 0.013) * Math.cos(a), 0.02, (R0 + 0.013) * Math.sin(a)], [0.0072, 0.0072, 0.0072], 5, 3); }+ // Fleurs-de-lis cut from sheet: flat plates set on the rim, the way a goldsmith makes them+ const h = 0.05 * tall, fl = [[-0.03, 0], [0.03, 0], [0.024, h * 0.2], [0.04, h * 0.42], [0.031, h * 0.62], [0.017, h * 0.42], [0.009, h * 0.5], [0.012, h * 0.76], [0, h * 1.1], [-0.012, h * 0.76], [-0.009, h * 0.5], [-0.017, h * 0.42], [-0.031, h * 0.62], [-0.04, h * 0.42], [-0.024, h * 0.2]];+ for (let i = 0; i < CROWN_FLEURS; i++) {+ const a = (i + 0.5) * 360 / CROWN_FLEURS;+ g.at(M.of(M.Ry(90 - a), M.T(0, 0.03, R0 + 0.004)), () => g.slab(fl, [], -0.004, 0.004, { so: 1, sr: 1 }));+ }+ });+}++const figF = {}; // fields kept while a figure is carved+const yNeck = 1.7;+async function makeFigure(kind) {+ const o = FIGURES[kind], stone = new Batch(TILE.fine), gold = new Batch(TILE.gold);+ const body = bodyField(o), arms = body.arms, hO = Object.assign({ seed: o.seed }, o.head);+ // Head frame: gently inclined, sitting on the neck above the centre of the shoulders+ const zc = arms[0].S[2] - (0.142 * (o.slim || 1)), cx = arms[0].S[0];+ const headM = M.of(M.T(cx + 0.024, yNeck + 0.116, zc + (o.sway || 0) * 0.3), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(-5));+ const hF = headField(hO), toHead = invRigid(headM);+ const headW = (x, y, z) => { const q = toHead(x, y, z); return hF(q[0], q[1], q[2]); };+ // Hands: frames derived from the sleeve so that the wrist always leaves the cuff+ const hands = [];+ const handFor = (A, side, pose) => {+ const X0 = [A.W[0] - A.E[0], A.W[1] - A.E[1], A.W[2] - A.E[2]];+ let Y0;+ if (pose === 'support') Y0 = [0.2, -1, 0]; // palm up under a book+ else if (pose === 'bless') Y0 = [0, 0.1, side]; // palm turned forward, two fingers raised+ else Y0 = [0, 0, side]; // back of the hand outward+ const X = pose === 'bless' ? [0.25, 1, 0.05] : X0;+ return { m: frameOf(A.W, X, Y0), pose, thumb: pose === 'grip' || pose === 'edge' || pose === 'rest' ? -side : side };+ };+ const handsCfg = { key: ['grip', 'support'], crozier: ['grip', 'rest'], sceptre: ['grip', 'rest'], mantle: ['rest', 'rest'], scroll: ['edge', 'rest'], child: ['rest', 'support'], bless: ['bless', 'support'] }[o.hold] || ['rest', 'rest'];+ hands.push(handFor(arms[0], 1, handsCfg[0]), handFor(arms[1], -1, handsCfg[1]));+ const handFs = hands.map((h) => { const f = handField(h.pose, h.thumb), inv = invRigid(h.m); return (x, y, z) => { const q = inv(x, y, z); return f(q[0], q[1], q[2]); }; });+ // The Child: a real little person on the Virgin's arm, turning to her+ let childF = null, childM = null;+ if (o.hold === 'child') {+ const A = arms[1];+ // sits in the crook of her left arm, turned towards her, one small hand raised to her veil+ childM = M.of(M.T(A.W[0] - 0.015, A.W[1] + 0.082, A.W[2] - 0.0), M.Ry(-34), M.Rz(4));+ const cH = headField({ hair: 'curls', seed: 21, smile: 0.6 }), inv = invRigid(childM), k = 0.47;+ childF = (x, y, z) => {+ const q = inv(x, y, z), X = q[0], Y = q[1], Z = q[2];+ // A seated child in a long shift: torso, thighs across her arm, shins and small feet hanging+ let d = sdCone(X, Y, Z, 0, 0, 0, 0, 0.165, 0, 0.066, 0.05);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.0, s * 0.034, 0.125, -0.012, s * 0.04, 0.038, 0.03), 0.018); // thigh+ d = smin(d, sdCone(X, Y, Z, 0.125, -0.012, s * 0.04, 0.142, -0.135, s * 0.038, 0.03, 0.022), 0.012); // shin+ d = smin(d, sdCone(X, Y, Z, 0.142, -0.15, s * 0.038, 0.19, -0.17, s * 0.04, 0.02, 0.013), 0.006); // foot+ }+ d -= 0.005 * Math.sqrt(Math.abs(Math.sin(Math.atan2(Z, X) * 6 + Y * 4))) * sstep(0.12, 0.0, Y) * sstep(-0.1, 0.0, Y);+ d = smin(d, sdCone(X, Y, Z, 0.005, 0.135, -0.052, 0.06, 0.07, -0.05, 0.02, 0.016), 0.012); // one arm holds an orb on his knee+ d = smin(d, len3(X - 0.1, Y - 0.052, Z + 0.042) - 0.03, 0.003);+ d = smin(d, len3(X - 0.074, Y - 0.056, Z + 0.054) - 0.015, 0.004);+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.145, 0.05, -0.005, 0.2, 0.094, 0.02, 0.015), 0.012); // the other reaches back to touch her veil+ d = smin(d, len3(X + 0.007, Y - 0.214, Z - 0.104) - 0.0155, 0.005);+ d = smin(d, cH((X - 0.008) / k, (Y - 0.262) / k, Z / k) * k, 0.012);+ return d;+ };+ }+ const allF = (x, y, z) => {+ let d = body.F(x, y, z);+ d = Math.min(d, headW(x, y, z));+ for (let i = 0; i < handFs.length; i++) d = Math.min(d, handFs[i](x, y, z));+ if (childF) d = Math.min(d, childF(x, y, z));+ return d;+ };+ // ---- carve ----+ await carveInto(stone, body.F, [-0.42, -0.005, -0.52, 0.42, yNeck + 0.04, 0.52], 0.0105, o.wings ? 3800 : 4400, null, allF);+ const head = await headBatch(o.headKey);+ for (let i = 0; i < hands.length; i++) {+ const hb = await handBatch(hands[i].pose, hands[i].thumb);+ const base = stone.count;+ copyBatch(stone, hb, hands[i].m);+ }+ if (childF) await carveInto(stone, childF, [arms[1].W[0] - 0.24, arms[1].W[1] - 0.2, arms[1].W[2] - 0.26, arms[1].W[0] + 0.3, arms[1].W[1] + 0.56, arms[1].W[2] + 0.26], 0.0042, 3200, null, allF, { smooth: 1, ao: 0.34, aoMin: 0.3 });+ addAttrs({ stone, gold }, o, arms);+ // Crown, nimbus and wings+ if (o.head.crown) addCrown(gold, M.of(headM, M.T(-0.012, o.head.veil ? 0.088 : 0.096, 0)), o.head.veil ? 0.088 : 0.084);+ if (o.nimbus) gold.at(M.of(headM, M.T(-0.16, 0.03, 0), M.Ry(90)), () => { gold.slab(ringPts(0, 0, 0.235, 28), [], -0.014, 0.014, { sb: 0.8 }); });+ if (o.wings) {+ // Feathers in overlapping tiers, carved on a blade that rises from the shoulders and falls to the calf+ const W = (side) => (x, y, z) => {+ const zz = z * side - 0.12 - zc * side, X = x + 0.085 + zz * 0.18, u = clamp((1.86 - y) / 1.32), wid = 0.27 * Math.pow(Math.sin(PI * Math.pow(u, 0.6)), 0.65) * (1 - 0.3 * u);+ const v = zz - 0.03 - u * 0.1 + 0.015;+ let d = Math.abs(X + 0.05 * u) - 0.026 + 0.012 * u;+ d = Math.max(d, Math.abs(v - wid * 0.5) - wid * 0.5, y - 1.87, 0.53 - y);+ // Tiers of feathers: short and scaled over the shoulder, long pointed quills towards the tip+ const row = Math.abs(Math.sin(u * 14 + v * 6)), bars = Math.abs(Math.sin(v * 26 + u * 7));+ d -= 0.015 * Math.sqrt(row) * sstep(0.45, 0.2, u) + 0.016 * Math.sqrt(bars) * sstep(0.2, 0.42, u);+ return d * 0.8;+ };+ for (const side of [-1, 1]) await carveInto(stone, W(side), [-0.3, 0.5, side > 0 ? zc + 0.05 : zc - 0.72, 0.1, 1.9, side > 0 ? zc + 0.72 : zc - 0.05], 0.0105, 900, null, allF);+ }+ stone.isFigure = true;+ return { stone, gold, head, headM };+}+// ---- Seated and kneeling figures for the tympana: same people, other postures -------------+function poseField(kind) {+ if (kind === 'seated') return (x, y, z) => {+ // A figure enthroned: knees forward, the robe falling from them in a curtain to the feet+ const th = Math.atan2(z, x);+ let d = sdCone(x, y, z, -0.04, 0.72, 0, -0.03, 1.38, 0, 0.17, 0.13);+ d = smin(d, sdEll(x, y - 1.46, z, 0.09, 0.12, 0.17), 0.03);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(x, y, z, -0.03, 0.7, s * 0.1, 0.27, 0.68, s * 0.11, 0.1, 0.09), 0.03);+ const cur = sdCone(x, y, z, 0.27, 0.64, s * 0.11, 0.31, 0.04, s * 0.1, 0.09, 0.12);+ d = smin(d, cur - 0.012 * Math.sqrt(Math.abs(Math.sin(z * 34 + x * 4))), 0.03);+ }+ d = smin(d, sdCone(x, y, z, 0.16, 0.45, 0, 0.27, 0.04, 0, 0.11, 0.16) - 0.014 * Math.sqrt(Math.abs(Math.sin(z * 30 + 1))), 0.03);+ // the lap folds run across, the bodice is drawn up+ d -= 0.012 * Math.sqrt(Math.abs(Math.sin((y + 0.12 * z * z * 40) * 28))) * sstep(0.95, 0.7, y) * sstep(0.45, 0.62, y);+ d -= 0.009 * Math.sqrt(Math.abs(Math.sin(th * 7 + y * 2))) * sstep(0.8, 1.0, y);+ return Math.max(d, -y);+ };+ return (x, y, z) => {+ // Kneeling in prayer, turned to the centre+ const th = Math.atan2(z, x);+ let d = sdCone(x, y, z, -0.1, 0.4, 0, 0.03, 1.04, 0, 0.16, 0.12);+ d = smin(d, sdEll(x - 0.05, y - 1.12, z, 0.08, 0.1, 0.15), 0.03);+ d = smin(d, sdCone(x, y, z, -0.14, 0.18, 0, 0.24, 0.13, 0, 0.2, 0.13), 0.05);+ d -= 0.018 * Math.sqrt(Math.abs(Math.sin(th * 6 + y * 1.5))) * sstep(0.72, 0.28, y);+ d -= 0.008 * Math.sqrt(Math.abs(Math.sin(th * 8 - y * 2))) * sstep(0.5, 0.8, y);+ return Math.max(d, -y);+ };+}+async function makePosed(kind, spec) {+ const stone = new Batch(TILE.fine), gold = new Batch(TILE.gold), bF = poseField(kind), hO = Object.assign({ seed: 3 }, spec.head);+ const hy = kind === 'seated' ? 1.74 : 1.36, hx = kind === 'seated' ? -0.01 : 0.075;+ const headM = M.of(M.T(hx, hy, 0), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(hO.nod || -6));+ const allF = bF, head = await headBatch(spec.headKey);+ stone.isFigure = true;+ await carveInto(stone, bF, [-0.42, -0.005, -0.42, 0.56, kind === 'seated' ? 1.62 : 1.26, 0.42], 0.0145, 2600, null, allF);+ if (hO.crown) addCrown(gold, M.of(headM, M.T(-0.014, 0.1, 0)), hO.veil ? 0.106 : 0.098);+ // Forearms and hands: joined in prayer when kneeling, one raised when enthroned+ const hand = await handBatch(kind === 'seated' ? 'bless' : 'support', 1);+ if (kind === 'seated') {+ const A = spec.bless === false ? null : { W: [0.2, 1.32, 0.17] };+ const armF = (x, y, z) => { let d = sdCone(x, y, z, -0.02, 1.42, 0.17, 0.06, 1.02, 0.23, 0.066, 0.055); d = smin(d, sdCone(x, y, z, 0.06, 1.02, 0.23, 0.2, 1.3, 0.17, 0.055, 0.058), 0.02); d = Math.min(d, smin(sdCone(x, y, z, -0.02, 1.42, -0.17, 0.05, 1.02, -0.22, 0.066, 0.055), sdCone(x, y, z, 0.05, 1.02, -0.22, 0.27, 0.92, -0.08, 0.055, 0.058), 0.02)); return d; };+ await carveInto(stone, armF, [-0.12, 0.84, -0.34, 0.36, 1.52, 0.34], 0.0125, 700, null, allF);+ copyBatch(stone, hand, frameOf([0.2, 1.3, 0.17], [0.25, 1, 0.05], [0, 0.1, 1]));+ addBook(stone, M.of(M.T(0.36, 0.92, -0.08), M.Rz(-78)), { w: 0.11, h: 0.15 });+ } else {+ const armF = (x, y, z) => { let d = 1e9; for (const s of [-1, 1]) d = Math.min(d, smin(sdCone(x, y, z, 0.03, 1.03, s * 0.15, 0.1, 0.8, s * 0.17, 0.062, 0.05), sdCone(x, y, z, 0.1, 0.8, s * 0.17, 0.25, 0.97, s * 0.04, 0.05, 0.055), 0.02)); return d; };+ await carveInto(stone, armF, [-0.08, 0.68, -0.3, 0.34, 1.12, 0.3], 0.0125, 600, null, allF);+ for (const s of [-1, 1]) copyBatch(stone, hand, frameOf([0.25, 0.97, s * 0.04], [0.5, 1, -s * 0.05], [1, -0.4, s * 0.9]));+ }+ return { stone, gold, head, headM };+}++// Cache of finished figures. Lower-detail copies for the arches come from the same sculpture.+const FIG_CACHE = {};+async function lightCopy(src, tris) { // the same surface with fewer triangles, keeping its uvs and carved occlusion+ const g = await simplifyGeometry(src.toGeo(), { targetTriangles: tris });+ const T = src.tile, b = new Batch(T), p = g.attributes.position, n = g.attributes.normal, u = g.attributes.uv, c = g.attributes.color, idx = g.index;+ for (let i = 0; i < p.count; i++) b.v(p.getX(i), p.getY(i), p.getZ(i), n.getX(i), n.getY(i), n.getZ(i), u.getX(i) * T, u.getY(i) * T, c ? c.getX(i) : 1);+ for (let t = 0; t < idx.count; t += 3) b.tri(idx.getX(t), idx.getX(t + 1), idx.getX(t + 2));+ return b;+}+async function lowDetail(src, tris) {+ const b = await lightCopy(src.stone, tris);+ b.isFigure = true;+ // A figure high in the arches carries a light copy of its head inside its own mesh: fewer draws, fewer triangles+ if (!src.head.low) src.head.low = await lightCopy(src.head, 420);+ copyBatch(b, src.head.low, src.headM);+ return { stone: b, gold: new Batch(TILE.gold) };+}+async function prepareFigures(kinds) {+ for (const k of kinds) FIG_CACHE[k + '|1'] = await makeFigure(k);+ if (P.preview !== 'statues') {+ for (const k of ['apostle', 'king', 'angel', 'bishop', 'queen']) if (FIG_CACHE[k + '|1']) FIG_CACHE[k + '|0'] = await lowDetail(FIG_CACHE[k + '|1'], P.archFigureTris);+ for (let k = 0; k < 3; k++) GARGOYLES[k] = await lightCopy(makeGargoyle(k), 800);+ FIG_CACHE['seated|1'] = await makePosed('seated', { headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 9, nod: -2 } });+ FIG_CACHE['seatedVirgin|1'] = await makePosed('seated', { headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, turn: -14, tilt: 4 } });+ FIG_CACHE['kneel|1'] = await makePosed('kneel', { headKey: 'veiled', head: { veil: 1, seed: 8, tilt: 0, nod: 8 } });+ FIG_CACHE['kneel|0'] = await lowDetail(FIG_CACHE['kneel|1'], P.archFigureTris);+ }+}+function figure(name, lod = 1) {+ const k = FIG_ALIAS[name] || name;+ return FIG_CACHE[k + '|' + lod] || FIG_CACHE[k + '|1'];+}+// Body and head are separate nodes: lod 0 lightens the body only, a head costs its bytes once however often it appears+function placeFigure(name, kind, o = {}) {+ const f = figure(kind, o.lod === undefined ? 1 : o.lod);+ if (!f) return null;+ const s = o.scale || 1, sc = o.mirror ? [s, s, -s] : [s, s, s];+ const mat = o.indoor ? MAT.figureIn : o.deep ? MAT.figureDeep : o.half ? MAT.figureHalf : MAT.figure;+ const stone = place(name, f.stone, mat, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ place(name + '_Gilt', f.gold, MAT.gold, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ let headPart = null;+ if (f.head) {+ const m = f.headM, e = eulerFromBasis([m[0], m[4], m[8]], [m[1], m[5], m[9]], [m[2], m[6], m[10]]);+ headPart = place(name + '_Head', f.head, mat, { pos: [m[3], m[7], m[11]], rot: e, parent: stone || undefined });+ }+ if (stone && !o.indoor && !o.deep && !o.half) FIG_PLACED.push({ part: stone, head: headPart });+ return stone;+}+const FIG_PLACED = [];++// ---- Foliage and crockets ----------------------------------------------------------+// One carved leaf curling at the tip; local +Y up the surface, +X outward from the host+function addLeaf(b, m, size = 1, o = {}) {+ b.at(m, () => {+ const nu = o.coarse ? 2 : 4, nv = o.coarse ? 3 : 4;+ b.grid((u, v) => {+ const s = (u - 0.5) * 2, W = 0.5 * Math.sin(PI * Math.pow(v, 0.72)) * (1 - 0.35 * v);+ const curl = Math.pow(v, 2.2);+ const lobes = 1 + 0.18 * Math.sin(v * 16) * Math.abs(s);+ const rib = 0.05 * (1 - Math.abs(s));+ return [size * (rib + 0.3 * curl - 0.42 * curl * curl * curl + 0.12 * s * s), size * (v - 0.3 * curl * v), size * (s * W * lobes), 0.72 + 0.28 * (1 - Math.abs(s)) + 0.1 * curl];+ }, nu, nv, { flip: true });+ });+}+// A crocket: a bud of curled foliage that sits on an arris. Local +X away from the arris, +Y up the slope.+function addCrocket(b, m, size = 1, lod = 1) {+ b.at(m, () => {+ const path = [];+ const N = lod ? 5 : 3;+ for (let i = 0; i <= N; i++) { const t = i / N, a = -0.9 + t * 3.8, r = lerp(0.3, 0.12, t) * size; path.push([size * 0.16 + r * Math.cos(a) * 0.95, size * 0.1 + r * Math.sin(a) * 0.9, 0]); }+ b.tube(path, (t) => [lerp(0.14, 0.05, t) * size, lerp(0.17, 0.07, t) * size], lod ? 5 : 3, { capStart: false, capEnd: false, ringS: (a) => 0.8 + 0.2 * Math.cos(a) });+ if (lod) for (const s of [-1, 1]) b.tube([[0.02 * size, 0.02 * size, 0], [0.2 * size, 0.1 * size, s * 0.22 * size], [0.34 * size, 0.26 * size, s * 0.3 * size]], (t) => [lerp(0.07, 0.015, t) * size, lerp(0.05, 0.02, t) * size], 3, { capEnd: false });+ });+}+function addFinial(b, h = 1, lod = 1) { // cross-shaped foliate finial, base at y=0+ b.lathe([[0.12 * h, 0], [0.1 * h, 0.05 * h], [0.14 * h, 0.09 * h], [0.08 * h, 0.14 * h], [0.05 * h, 0.5 * h], [0.09 * h, 0.56 * h], [0.05 * h, 0.62 * h], [0.02 * h, 0.98 * h], [0, h]], lod ? 8 : 5);+ for (let i = 0; i < 4; i++) {+ const a = i * 90 + 45;+ addLeaf(b, M.of(M.T(0, 0.14 * h, 0), M.Ry(a), M.T(0.05 * h, 0, 0)), 0.4 * h, { coarse: 1 });+ if (lod) addLeaf(b, M.of(M.T(0, 0.58 * h, 0), M.Ry(a + 45), M.T(0.03 * h, 0, 0)), 0.3 * h, { coarse: 1 });+ }+}+// Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame+function crocketsAlong(b, p, q, n, size, out, lod = 1) {+ const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);+ const dn = d.map((c) => c / L);+ let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];+ const ol = Math.hypot(ox, oy, oz) || 1; ox /= ol; oy /= ol; oz /= ol;+ const zx = ox * 0, side = [dn[1] * oz - dn[2] * oy, dn[2] * ox - dn[0] * oz, dn[0] * oy - dn[1] * ox];+ // columns: X = out, Y = along edge, Z = side+ for (let i = 0; i < n; i++) {+ const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];+ const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];+ addCrocket(b, m, size * lerp(1, 0.78, t), lod);+ }+}++// Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.+function addPinnacle(b, w, h, o = {}) {+ const hw = w / 2, shaftH = h * 0.42, gableH = w * 0.95, lod = o.lod === undefined ? 1 : o.lod;+ if (o.simple) { // far-away pinnacle: faceted shaft, spirelet, a few crockets, finial+ b.lathe([[hw * 1.414, 0], [hw * 1.414, shaftH], [hw * 1.6, shaftH + w * 0.06], [hw * 1.3, shaftH + w * 0.12], [hw * 0.1, h * 0.86]], 4, { faceted: true, phase: PI / 4 });+ for (let i = 0; i < 4; i++) { const a = rad(i * 90 + 45), c = Math.cos(a), s = Math.sin(a), bR = hw * 1.3 * 1.0; crocketsAlong(b, [c * bR, shaftH + w * 0.14, s * bR], [c * hw * 0.14, h * 0.84, s * hw * 0.14], 2, w * 0.46, [c, 0.15, s], 0); }+ b.at(M.T(0, h * 0.84, 0), () => addFinial(b, h * 0.16, 0));+ return;+ }+ // Shaft with moulded base and blind lancet panels+ b.lathe([[hw * 1.44, 0], [hw * 1.44, w * 0.06], [hw * 1.3, w * 0.12], [hw * 1.3, w * 0.2], [hw * 1.26, w * 0.24]], 4, { faceted: true, phase: PI / 4 });+ b.lathe([[hw * 1.414, w * 0.2], [hw * 1.414, shaftH]], 4, { faceted: true, phase: PI / 4, s: 0.95 });+ for (let i = 0; i < 4; i++) b.at(M.Ry(i * 90), () => {+ const pw = w * 0.62, y0 = w * 0.35, ys = shaftH - w * 0.5;+ b.at(M.T(hw + 0.004, 0, 0), () => b.at(M.Ry(90), () => { // panel recessed look: shaded inset lancet with moulded frame+ b.sweep(ofsXY(pointedArc(pw, pw * 0.85, 3), 0, ys), hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[-pw / 2, y0], [-pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[pw / 2, y0], [pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.face([[-pw / 2, y0, -0.002, 0.5], [pw / 2, y0, -0.002, 0.5], [pw / 2, ys, -0.002, 0.5], [0, ys + pw * 0.8, -0.002, 0.45], [-pw / 2, ys, -0.002, 0.5]]);+ }));+ // Gablet on each face with crockets+ b.at(M.T(hw * 0.96, shaftH, 0), () => {+ b.slabX = true;+ b.at(M.Ry(90), () => b.slab([[-hw * 1.04, 0], [hw * 1.04, 0], [0, gableH]], [], 0, w * 0.08, { sf: 1, sb: 0.8 }));+ crocketsAlong(b, [0.02, 0.02, -hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, -1], 0);+ crocketsAlong(b, [0.02, 0.02, hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, 1], 0);+ b.at(M.T(0.04, gableH * 0.94, 0), () => addFinial(b, w * 0.34, 0));+ });+ });+ // Cornice ring and spirelet+ b.at(M.T(0, shaftH, 0), () => {+ b.lathe([[hw * 1.414, 0], [hw * 1.6, w * 0.06], [hw * 1.6, w * 0.1], [hw * 1.414, w * 0.14]], 4, { faceted: true, phase: PI / 4 });+ const sh = h - shaftH - w * 0.1;+ b.at(M.T(0, w * 0.12, 0), () => {+ b.lathe([[hw * 1.3, 0], [hw * 0.09, sh * 0.82]], 8, { faceted: true, phase: PI / 8 });+ const n = lod ? 5 : 3;+ for (let i = 0; i < 4; i++) {+ const a = rad(i * 90 + 22.5 + 22.5 * 0);+ const c = Math.cos(a + rad(22.5)), s = Math.sin(a + rad(22.5));+ const bR = hw * 1.3 * Math.cos(rad(0)), tR = hw * 0.09;+ crocketsAlong(b, [c * bR, sh * 0.04, s * bR], [c * tR, sh * 0.8, s * tR], n, w * 0.36, [c, 0.12, s], lod);+ }+ b.at(M.T(0, sh * 0.8, 0), () => (o.finial || b) === b ? addFinial(b, sh * 0.22 + w * 0.1, lod) : null);+ });+ });+}++// ---- Gargoyle ------------------------------------------------------------------------+// A crouching beast thrusting out from the wall along +X, root at the origin. About 2.3 m long.+function makeGargoyle(kind = 0) {+ const b = new Batch(TILE.fine), rng = mkRng(kind * 17 + 5);+ const ridge = (kind === 1 ? 0.3 : 0.22);+ const headY = kind === 2 ? 0.1 : 0.34;+ const spine = spline([[-0.3, -0.05, 0], [0.22, 0.2, 0], [0.75, 0.3, 0], [1.2, 0.2, 0], [1.56, headY, 0]], 5);+ b.tube(spine, (t) => { const r = keyf([[0, 0.34, 0.3], [0.25, 0.36, 0.34], [0.55, 0.22, 0.24], [0.82, 0.16, 0.17], [1, 0.19, 0.2]], t); return [r[0], r[1]]; }, 16, {+ capStart: true,+ up: [0, 1, 0],+ ring: (a, t) => 1 + ridge * Math.pow(Math.max(Math.cos(a), 0), 10) * Math.abs(Math.sin(t * 26)) + 0.05 * Math.abs(Math.sin(a * 5 + t * 11)),+ ringS: (a, t) => 0.74 + 0.26 * Math.abs(Math.sin(a * 5 + t * 11)),+ });+ // Skull, snout, gaping jaws+ const hx = 1.56, hy = headY;+ blob(b, [hx + 0.05, hy + 0.03, 0], [0.22, 0.19, 0.2], 14, 10, 0.92);+ b.tube(spline([[hx + 0.1, hy + 0.06, 0], [hx + 0.34, hy + 0.05, 0], [hx + 0.56, hy + 0.1, 0]], 3), (t) => [lerp(0.14, 0.07, t), lerp(0.15, 0.085, t)], 10, { capStart: true, up: [0, 1, 0], ring: (a, t) => 1 + 0.2 * Math.pow(Math.max(Math.cos(a), 0), 6) * Math.abs(Math.sin(t * 9)) });+ b.shade(0.9, () => b.tube(spline([[hx + 0.05, hy - 0.1, 0], [hx + 0.28, hy - 0.2, 0], [hx + 0.5, hy - 0.2, 0]], 3), (t) => [lerp(0.09, 0.045, t), lerp(0.125, 0.07, t)], 10, { capStart: true, up: [0, 1, 0] }));+ b.shade(0.34, () => blob(b, [hx + 0.2, hy - 0.06, 0], [0.16, 0.085, 0.095], 8, 5));+ for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {+ const f = i / 3.5;+ b.tube([[hx + 0.26 + f * 0.27, hy + 0.0 + f * 0.075, s * lerp(0.1, 0.06, f)], [hx + 0.27 + f * 0.27, hy - 0.08 + f * 0.075, s * lerp(0.1, 0.06, f)]], (t) => lerp(0.018, 0.002, t), 4, { capStart: true });+ b.tube([[hx + 0.22 + f * 0.27, hy - 0.15, s * lerp(0.1, 0.05, f)], [hx + 0.22 + f * 0.27, hy - 0.08, s * lerp(0.1, 0.05, f)]], (t) => lerp(0.016, 0.002, t), 4, { capStart: true });+ }+ // Brow, eyes, ears or horns+ for (const s of [-1, 1]) {+ b.tube(spline([[hx + 0.02, hy + 0.2, s * 0.03], [hx + 0.17, hy + 0.17, s * 0.1], [hx + 0.24, hy + 0.12, s * 0.15]], 2), (t) => lerp(0.03, 0.02, t), 5, { capStart: true });+ b.shade(0.38, () => blob(b, [hx + 0.21, hy + 0.09, s * 0.11], [0.05, 0.04, 0.045], 6, 4));+ blob(b, [hx + 0.235, hy + 0.09, s * 0.115], [0.024, 0.022, 0.022], 6, 4);+ if (kind === 1) b.tube(spline([[hx - 0.05, hy + 0.14, s * 0.11], [hx - 0.1, hy + 0.3, s * 0.18], [hx - 0.25, hy + 0.42, s * 0.2], [hx - 0.42, hy + 0.42, s * 0.15]], 3), (t) => lerp(0.05, 0.005, t), 6, { capStart: true });+ else b.grid((u, v) => { const L = 0.3, W = 0.09 * Math.sin(PI * u); return [hx - 0.08 - u * L * 0.5, hy + 0.13 + u * L * 0.7, s * (0.13 + u * 0.09 + (v - 0.5) * W * 2), 0.7 + 0.3 * Math.abs(v - 0.5) * 2]; }, 4, 2, { flip: s > 0 });+ if (kind === 1) { /* also add jowls */ blob(b, [hx + 0.1, hy - 0.04, s * 0.13], [0.09, 0.06, 0.05], 6, 4, 0.9); }+ // Fore leg gripping the ledge+ const sh = [0.5, 0.12, s * 0.3], el = [0.68, -0.28, s * 0.42], wr = [0.98, -0.42, s * 0.3];+ b.tube(spline([sh, el, wr], 4), (t) => keyf([[0, 0.16], [0.4, 0.105], [1, 0.07]], t)[0], 10, { capStart: true, ring: (a, t) => 1 + 0.08 * Math.abs(Math.sin(a * 3 + t * 8)) });+ blob(b, wr, [0.1, 0.06, 0.09], 8, 5);+ for (let i = -1; i <= 1; i++) b.tube(spline([[wr[0] + 0.03, wr[1], wr[2] + i * 0.05], [wr[0] + 0.14, wr[1] - 0.02, wr[2] + i * 0.08], [wr[0] + 0.21, wr[1] - 0.11, wr[2] + i * 0.09]], 2), (t) => lerp(0.032, 0.004, t), 5);+ // Folded wing+ const span = kind === 0 ? 0.95 : 0.7;+ const wingFn = (off) => (u, v) => {+ const L = u * span, W = 0.44 * Math.sin(PI * Math.pow(u, 0.7));+ const memb = Math.abs(Math.sin(v * 7.5 + u * 1.2)) * 0.05 * u;+ return [0.42 - L + (off), 0.3 + 0.48 * Math.sin(u * 1.25) * (1 - v * 0.9) - v * W * 0.75 + memb, s * (0.27 + 0.2 * u + v * W * 0.5), 0.68 + 0.32 * (memb / 0.05)];+ };+ b.grid(wingFn(0), 7, 5, { flip: s < 0 });+ b.grid(wingFn(-0.02), 7, 5, { flip: s > 0 });+ }+ return b;+}+const GARGOYLES = [];+function gargoyle(kind) { if (!GARGOYLES[kind]) GARGOYLES[kind] = makeGargoyle(kind); return GARGOYLES[kind]; }++// ==================================================================================+// STAINED GLASS: a tiny vector-art engine producing real leaded panes.+// Every shape is a pane of pot-metal colour with a came (lead strip) around it; large+// figures get painted grisaille lines. Windows are geometry, so they stay crisp at any distance.+// ==================================================================================+function offsetPoly(pts, d) {+ const n = pts.length, s = area2(pts) >= 0 ? 1 : -1, out = [];+ for (let i = 0; i < n; i++) {+ const a = pts[(i - 1 + n) % n], b = pts[i], c = pts[(i + 1) % n];+ let ax = b[0] - a[0], ay = b[1] - a[1], cx = c[0] - b[0], cy = c[1] - b[1];+ const la = Math.hypot(ax, ay) || 1, lc = Math.hypot(cx, cy) || 1; ax /= la; ay /= la; cx /= lc; cy /= lc;+ let nx = (ay + cy) * s, ny = -(ax + cx) * s; const l = Math.hypot(nx, ny) || 1; nx /= l; ny /= l;+ const k = clamp(1 / Math.max(0.35, Math.abs(nx * ay * s - ny * ax * s)), 1, 2.4);+ out.push([b[0] + nx * d * k, b[1] + ny * d * k]);+ }+ return out;+}+function clipConvex(poly, clip) {+ let out = poly; const sgn = area2(clip) >= 0 ? 1 : -1;+ for (let i = 0; i < clip.length && out.length; i++) {+ const a = clip[i], b = clip[(i + 1) % clip.length], inp = out; out = [];+ const side = (p) => sgn * ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]));+ for (let j = 0; j < inp.length; j++) {+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);+ if (sp >= 0) out.push(p);+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([lerp(p[0], q[0], t), lerp(p[1], q[1], t)]); }+ }+ }+ return out;+}+function isConvex(p) { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = p[(i + 2) % p.length]; const s = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); if (Math.abs(s) < 1e-9) continue; if (!sign) sign = Math.sign(s); else if (Math.sign(s) !== sign) return false; } return true; }++class Glass {+ constructor(seed = 1) { this.b = new Batch(1); this.b.noColor = true; this.r = mkRng(seed * 13 + 3); this.clip = null; this.xf = [1, 0, 0, 1, 0, 0]; this.came = 0.022; }+ xform(a, b, c, d, e, f) { const m = this.xf; return [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]]; }+ withXf(rotDeg, tx, ty, s, fn) { const o = this.xf, c = Math.cos(rad(rotDeg)) * s, sn = Math.sin(rad(rotDeg)) * s; this.xf = this.xform(c, sn, -sn, c, tx, ty); fn(this); this.xf = o; }+ withClip(pts, fn) { const o = this.clip; const m = this.xf; this.clip = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]); fn(this); this.clip = o; }+ toLocal(p) { const m = this.xf, det = m[0] * m[3] - m[1] * m[2], x = p[0] - m[4], y = p[1] - m[5]; return [(m[3] * x - m[2] * y) / det, (-m[1] * x + m[0] * y) / det]; }+ // Writes one flat polygon. map(worldPoint) -> [u,v] in atlas pixels; otherwise a window on the sheet of its colour+ raw(pts, color, layer, jitter = 1, map = null) {+ if (layer === -0.4 && !this.b.outline) this.b.outline = pts; // the first polygon of a window is its whole outline+ const name = GLASS_ALIAS[color] || color, fam = GL.fam[name] || GL.fam.lead, z = 0.002 + layer * 0.0035, C = GL.small;+ // Every pane is cut from a different part of the sheet, so no two panes match;+ // 200 pixels of sheet to the metre, so the streaks and clouds of the pot-metal show in the pane+ const ox = fam[0] + 44 + this.r() * (C - 88), oy = fam[1] + 44 + this.r() * (C - 88), rot = this.r() * 6.28, cr = Math.cos(rot) * 200, sr = Math.sin(rot) * 200;+ let mx = 0, my = 0; for (const p of pts) { mx += p[0]; my += p[1]; } mx /= pts.length; my /= pts.length;+ const ids = pts.map((p) => {+ let u, v;+ if (map) { const q = map(p); u = q[0]; v = q[1]; } else {+ const dx = clamp(p[0] - mx, -0.2, 0.2), dy = clamp(p[1] - my, -0.2, 0.2);+ u = ox + (dx * cr - dy * sr) * jitter; v = oy + (dx * sr + dy * cr) * jitter;+ }+ return this.b.v(p[0], p[1], z, 0, 0, 1, u / GL.w, v / GL.h, 1);+ });+ if (isConvex(pts)) { const flip = area2(pts) < 0; for (let i = 1; i < ids.length - 1; i++) { if (flip) this.b.tri(ids[0], ids[i + 1], ids[i]); else this.b.tri(ids[0], ids[i], ids[i + 1]); } } else { const t = triangulate(pts); for (const k of t) this.b.tri(ids[k[0]], ids[k[1]], ids[k[2]]); }+ }+ // a pane: colour polygon + lead came. o.tile paints drapery across it, o.orn ornament.+ poly(pts, color, layer = 1, o = {}) {+ const m = this.xf; let P2 = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]);+ if (area2(P2) < 0) P2 = P2.slice().reverse();+ const came = o.came === undefined ? this.came : o.came;+ if (came > 0) {+ let C = offsetPoly(P2, came);+ if (this.clip) C = clipConvex(C, this.clip);+ if (C.length >= 3 && Math.abs(area2(C)) > 1e-5) this.raw(C, 'lead', layer - 0.5, 0);+ } else if (came < 0) P2 = offsetPoly(P2, came); // over a lead backing: inset instead of a second polygon+ if (this.clip) P2 = clipConvex(P2, this.clip);+ if (P2.length < 3 || Math.abs(area2(P2)) <= 1e-5) return this;+ let map = o.map || null;+ if (o.tile && GL.tile[o.tile]) { // planar mapping of the painted tile over the local box+ const T = GL.tile[o.tile], B = o.box, f = o.flipU ? -1 : 1;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + (f > 0 ? a : 1 - a) * (T[2] - 4), T[1] + 2 + (1 - c) * (T[3] - 4)]; };+ } else if (o.orn && GL.orn[o.orn]) {+ const T = GL.orn[o.orn], B = o.box;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + a * (GL.small - 4), T[1] + 2 + (1 - c) * (GL.small - 4)]; };+ }+ this.raw(P2, color, layer, o.jitter === undefined ? 1 : o.jitter, map);+ return this;+ }+ disc(cx, cy, r, color, layer = 1, n = 12, o) { return this.poly(ringPts(cx, cy, r, n), color, layer, o); }+ // a head drawn by the glass painter: an oval pane mapped onto a face from the atlas+ face(cx, cy, rx, ry, kind, layer, flip) {+ const F = GL.face[kind] || GL.face.youth, C = GL.cell, pts = []; for (let i = 0; i < 12; i++) { const a = i * TAU / 12; pts.push([cx + rx * Math.cos(a), cy + ry * Math.sin(a)]); }+ return this.poly(pts, 'flesh', layer, { came: 0.012, map: (p) => { const q = this.toLocal(p), a = ((q[0] - cx) / rx) * (flip ? -1 : 1), c = (q[1] - cy) / ry; return [F[0] + C / 2 + clamp(a, -1, 1) * (C / 2 - 3), F[1] + C / 2 - clamp(c, -1, 1) * (C / 2 - 3)]; } });+ }+ // painted line (grisaille): a thin dark ribbon along a polyline+ line(pts, w = 0.02, color = 'lead', layer = 5) {+ for (let i = 0; i < pts.length - 1; i++) {+ const a = pts[i], b = pts[i + 1]; let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx *= w / 2 / l; dy *= w / 2 / l;+ this.poly([[a[0] - dy, a[1] + dx], [a[0] + dy, a[1] - dx], [b[0] + dy, b[1] - dx], [b[0] - dy, b[1] + dx]], color, layer, { came: 0, jitter: 0 });+ }+ return this;+ }+}++// Diapered mosaic background: lozenges of two deep colours, a jewel at each crossing+// (one painted square of leaded lozenges laid down repeatedly: fine as real mosaic, cheap as two triangles a square)+function gDiaper(g, x0, y0, x1, y1, cell, c1, c2, c3, layer = 0) {+ const T = 0.72, G = GL.ground[GROUND_OF[c1] || 'rc'], C = GL.cell;+ for (let y = Math.floor(y0 / T) * T; y < y1; y += T) for (let x = Math.floor(x0 / T) * T; x < x1; x += T) {+ const qx = x, qy = y, map = (p) => [G[0] + 2 + clamp((p[0] - qx) / T) * (C - 4), G[1] + 2 + clamp((p[1] - qy) / T) * (C - 4)];+ g.poly([[x, y], [x + T, y], [x + T, y + T], [x, y + T]], c1, layer, { came: 0, map });+ }+}++// A figure in glass: long Gothic proportions, robes painted in brown, the head drawn by hand. (x,y) = feet.+function gFigure(g, x, y, h, o = {}) {+ const sx = o.sx || 0.82, hTot = h; h = hTot * sx;+ const f = o.flip ? -1 : 1, L = (u, v) => [x + f * u * h, y + (v <= 0.76 ? v / 0.76 * (hTot - 0.24 * h) : hTot - (1 - v) * h)], PS = (a) => a.map((p) => L(p[0], p[1]));+ const robe = o.robe || 'white', mantle = o.mantle || 'cobalt', skin = o.skin || 'flesh', big = hTot > 2.2, layer = o.layer === undefined ? 2 : o.layer;+ const sit = o.pose === 'sit', kneel = o.pose === 'kneel';+ const hy = kneel ? 0.67 : sit ? 0.86 : 0.875, hr = 0.066, hx = kneel ? 0.04 : 0;+ // Box in world units for tile mapping, in the local frame of the Glass object+ const bx = [x - h * 0.34, y, x + h * 0.34, y + hTot * 0.86], tR = DRAPE_OF[robe] || 'white', tM = DRAPE_OF[mantle] || 'cobalt';+ const pane = (pts, col, lay, tile, opt) => g.poly(PS(pts), col, lay, Object.assign({ tile, box: bx, flipU: !!o.flip }, opt || {}));+ const brk = (pts, c1, lay, tile) => { // a robe is many pieces of glass: cut it across with lead+ const P3 = PS(pts); let y0 = 1e9, y1 = -1e9; for (const q of P3) { y0 = Math.min(y0, q[1]); y1 = Math.max(y1, q[1]); }+ const n = big ? 4 : (hTot > 0.9 ? 2 : 1);+ for (let i = 0; i < n; i++) {+ const a = lerp(y0, y1, i / n), c = lerp(y0, y1, (i + 1) / n), sl = big ? 0.12 * h * (i % 2 ? 1 : -1) * 0.5 : 0;+ const piece = clipConvex(clipConvex(P3, [[-1e4, a - sl * 0], [1e4, a + sl], [1e4, 1e4], [-1e4, 1e4]]), [[-1e4, -1e4], [1e4, -1e4], [1e4, c + sl], [-1e4, c - sl * 0]]);+ if (piece.length >= 3) g.poly(piece, c1, lay, { tile, box: bx, flipU: !!o.flip, came: 0.016 });+ }+ };+ if (o.wings) {+ pane([[-0.08, hy - 0.1], [-0.3, hy + 0.1], [-0.4, hy - 0.08], [-0.34, 0.4], [-0.24, 0.2], [-0.1, 0.42]], o.wings, layer - 0.2, null);+ pane([[0.08, hy - 0.1], [0.3, hy + 0.1], [0.4, hy - 0.08], [0.34, 0.4], [0.24, 0.2], [0.1, 0.42]], o.wings, layer - 0.2, null);+ if (hTot > 1) for (const s of [-1, 1]) { g.line(PS([[s * 0.1, hy - 0.08], [s * 0.3, 0.52]]), h * 0.01, 'lead', layer); g.line(PS([[s * 0.2, hy - 0.02], [s * 0.34, 0.42]]), h * 0.008, 'lead', layer); }+ }+ if (o.halo !== false) {+ const c = L(hx, hy);+ g.disc(c[0], c[1], h * 0.116, o.halo || 'gold', layer - 0.1, big ? 18 : 12);+ if (o.cross) { for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[c[0] + h * 0.116 * Math.cos(a - 0.26), c[1] + h * 0.116 * Math.sin(a - 0.26)], [c[0] + h * 0.116 * Math.cos(a + 0.26), c[1] + h * 0.116 * Math.sin(a + 0.26)], c], 'ruby', layer, { came: 0.008 }); } }+ }+ // ---- body ----+ if (sit) {+ brk([[-0.12, 0.72], [0.12, 0.72], [0.19, 0.44], [0.26, 0.4], [0.27, 0.08], [0.22, 0.0], [-0.22, 0.0], [-0.2, 0.3], [-0.14, 0.44]], robe, layer, tR);+ brk([[-0.13, 0.74], [0.05, 0.74], [0.17, 0.46], [0.24, 0.4], [0.2, 0.18], [-0.06, 0.1], [-0.2, 0.3], [-0.14, 0.46]], mantle, layer + 0.5, tM);+ } else if (kneel) {+ brk([[-0.08, 0.56], [0.13, 0.56], [0.16, 0.26], [0.26, 0.1], [0.26, 0], [-0.26, 0], [-0.2, 0.12]], robe, layer, tR);+ brk([[-0.09, 0.57], [0.08, 0.57], [0.13, 0.28], [0.1, 0.1], [-0.2, 0.05], [-0.16, 0.24]], mantle, layer + 0.5, tM);+ } else {+ const c = o.sway === undefined ? 0.02 : o.sway;+ brk([[-0.1 + c, 0.75], [0.1 + c, 0.75], [0.115, 0.46], [0.14, 0.2], [0.2, 0.03], [0.15, 0.0], [0.05, 0.022], [-0.05, 0.0], [-0.16, 0.012], [-0.2, 0.04], [-0.14, 0.22], [-0.115, 0.46]], robe, layer, tR);+ brk([[-0.11 + c, 0.76], [0.03 + c, 0.76], [0.12, 0.5], [0.135, 0.3], [0.09, 0.15], [-0.06, 0.11], [-0.165, 0.22], [-0.125, 0.46]], mantle, layer + 0.5, tM);+ pane([[-0.09, 0.03], [-0.02, 0.03], [-0.02, -0.012], [-0.115, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ pane([[0.02, 0.03], [0.09, 0.03], [0.115, -0.012], [0.02, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ }+ // ---- arms and hands ----+ const by = kneel ? 0.5 : 0.68, armPose = o.arm || 'hold';+ const ribbon = (a, b, w, col, tile) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy) || 1, nx = -dy / l * w / 2, ny = dx / l * w / 2; g.poly(PS([[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]]), col, layer + 1, { came: 0.012, tile, box: bx }); };+ const hand = (p) => { const c = L(p[0], p[1]); g.poly([[c[0] - h * 0.026, c[1] - h * 0.018], [c[0] + h * 0.018, c[1] - h * 0.022], [c[0] + h * 0.03, c[1] + h * 0.016], [c[0] + h * 0.006, c[1] + h * 0.03], [c[0] - h * 0.02, c[1] + h * 0.018]], skin, layer + 1.3, { came: 0.008 }); };+ const arms = { hold: [[0.13, by - 0.12], [0.03, by - 0.18], [-0.13, by - 0.12], [-0.03, by - 0.2]], up: [[0.17, by - 0.03], [0.27, by + 0.18], [-0.12, by - 0.12], [-0.03, by - 0.22]], out: [[0.19, by - 0.08], [0.32, by - 0.06], [-0.19, by - 0.08], [-0.32, by - 0.06]], pray: [[0.1, by - 0.14], [0.12, by - 0.03], [-0.04, by - 0.14], [0.1, by - 0.03]], point: [[0.17, by - 0.08], [0.33, by + 0.04], [-0.12, by - 0.12], [-0.04, by - 0.2]], orant: [[0.2, by + 0.0], [0.27, by + 0.14], [-0.2, by + 0.0], [-0.27, by + 0.14]] };+ const A = arms[armPose] || arms.hold;+ ribbon([0.095, by], A[0], 0.07, mantle, tM); ribbon(A[0], A[1], 0.056, robe, tR); hand(A[1]);+ ribbon([-0.095, by], A[2], 0.07, mantle, tM); ribbon(A[2], A[3], 0.056, robe, tR); hand(A[3]);+ // ---- attributes ----+ const at = o.attr;+ if (at === 'book') { g.poly(PS([[A[1][0] - 0.1, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] + 0.12], [A[1][0] - 0.1, A[1][1] + 0.12]]), 'ruby', layer + 1.5, { came: 0.01 }); g.poly(PS([[A[1][0] - 0.07, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.09], [A[1][0] - 0.07, A[1][1] + 0.09]]), 'gold', layer + 1.6, { came: 0 }); }+ else if (at === 'staff' || at === 'cross') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, 0], [sxx + 0.012, 0], [sxx + 0.012, 1.08], [sxx - 0.012, 1.08]]), 'gold', layer + 0.9, { came: 0.008 }); if (at === 'cross') g.poly(PS([[sxx - 0.07, 0.95], [sxx + 0.07, 0.95], [sxx + 0.07, 0.98], [sxx - 0.07, 0.98]]), 'gold', layer + 0.9, { came: 0.008 }); else g.poly(ringPts(sxx + 0.03, 1.1, 0.045, 8).map((p) => L(p[0], p[1])), 'gold', layer + 0.9, { came: 0.008 }); }+ else if (at === 'sword') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, by - 0.08], [sxx + 0.012, by - 0.08], [sxx, 1.02]]), 'white', layer + 0.9, { came: 0.008 }); g.poly(PS([[sxx - 0.05, by - 0.08], [sxx + 0.05, by - 0.08], [sxx + 0.05, by - 0.05], [sxx - 0.05, by - 0.05]]), 'gold', layer + 1, { came: 0.008 }); }+ else if (at === 'key') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.01, by - 0.14], [sxx + 0.01, by - 0.14], [sxx + 0.01, by + 0.2], [sxx - 0.01, by + 0.2]]), 'gold', layer + 1, { came: 0.006 }); g.poly(ringPts(sxx, by + 0.23, 0.035, 6).map((p) => L(p[0], p[1])), 'gold', layer + 1, { came: 0.006 }); g.poly(PS([[sxx, by - 0.14], [sxx + 0.05, by - 0.14], [sxx + 0.05, by - 0.1], [sxx, by - 0.1]]), 'gold', layer + 1, { came: 0.006 }); }+ else if (at === 'lily') { const sxx = A[1][0]; g.line(PS([[sxx, by - 0.22], [sxx + 0.01, by + 0.2]]), h * 0.012, 'green', layer + 1); for (let i = 0; i < 3; i++) { const yy = by + 0.08 + i * 0.07; const q = L(sxx + 0.02 * (i % 2 ? 1 : -1), yy); g.disc(q[0], q[1], h * 0.024, 'white', layer + 1.1, 5, { came: 0.006 }); } }+ // ---- head: drawn, not a disc ----+ const hc = L(hx, hy);+ const kind = o.face || (o.wings ? 'angel' : o.veil ? 'virgin' : o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ const nkA = L(hx - 0.02, hy - 0.11), nkB = L(hx + 0.02, hy - 0.11), nkC = L(hx + 0.02, hy - 0.04), nkD = L(hx - 0.02, hy - 0.04);+ g.poly([nkA, nkB, nkC, nkD], skin, layer + 1.5, { came: 0.008 });+ g.face(hc[0], hc[1], h * hr * 1.02, h * hr * 1.24, kind, layer + 1.8, !!o.flip);+ if (o.crown) g.poly(PS([[hx - 0.066, hy + 0.05], [hx + 0.066, hy + 0.05], [hx + 0.08, hy + 0.13], [hx + 0.04, hy + 0.095], [hx, hy + 0.14], [hx - 0.04, hy + 0.095], [hx - 0.08, hy + 0.13]]), 'gold', layer + 2.2, { came: 0.01 });+ if (o.mitre) g.poly(PS([[hx - 0.056, hy + 0.05], [hx + 0.056, hy + 0.05], [hx + 0.048, hy + 0.15], [hx, hy + 0.215], [hx - 0.048, hy + 0.15]]), o.mitre, layer + 2.2, { came: 0.01 });+}+// ---- Narrative scenes, composed inside a circle of radius r at the origin ---------+const SCENES = {+ annunciation(g, r) { // Angel greets the Virgin; lily between them, dove above+ gFigure(g, -r * 0.46, -r * 0.72, r * 1.34, { robe: 'white', mantle: 'emerald', wings: 'rubyLt', arm: 'point', attr: null, hair: 'amber' });+ gFigure(g, r * 0.46, -r * 0.72, r * 1.3, { flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray', halo: 'gold' });+ g.poly([[-0.1, -r * 0.72], [0.1, -r * 0.72], [0.06, -r * 0.5], [-0.06, -r * 0.5]], 'gold', 3, { came: 0.012 });+ g.line([[0, -r * 0.5], [0, r * 0.05]], 0.025, 'green', 3.2);+ [0.05, -0.1, -0.24].forEach((v, i) => g.disc((i % 2 ? 1 : -1) * r * 0.05, r * v, r * 0.05, 'white', 3.3, 5, { came: 0.008 }));+ g.disc(0, r * 0.62, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.14, r * 0.66], [0, r * 0.6], [r * 0.14, r * 0.66], [0, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ nativity(g, r) { // Virgin, Child in the crib, Joseph, the star, the ox and ass+ g.poly([[-r * 0.8, -r * 0.9], [r * 0.8, -r * 0.9], [r * 0.8, -r * 0.62], [-r * 0.8, -r * 0.62]], 'brown', 1.5);+ gFigure(g, -r * 0.42, -r * 0.62, r * 1.05, { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'out' });+ gFigure(g, r * 0.56, -r * 0.62, r * 1.1, { robe: 'tan', mantle: 'emerald', beard: 1, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'staff', flip: 1, halo: 'yellow' });+ g.poly([[-r * 0.1, -r * 0.62], [r * 0.3, -r * 0.62], [r * 0.34, -r * 0.34], [-r * 0.14, -r * 0.34]], 'amber', 2, { came: 0.014 });+ g.poly([[-r * 0.07, -r * 0.34], [r * 0.27, -r * 0.34], [r * 0.27, -r * 0.22], [-r * 0.07, -r * 0.22]], 'white', 2.5, { came: 0.012 });+ g.disc(r * 0.02, -r * 0.24, r * 0.065, 'gold', 2.5, 8, { came: 0.008 }); g.disc(r * 0.02, -r * 0.24, r * 0.043, 'flesh', 3, 7, { came: 0.008 });+ star(g, 0, r * 0.68, r * 0.13, 'yellow', 3);+ g.poly([[-r * 0.4, r * 0.22], [0, r * 0.52], [r * 0.4, r * 0.22], [r * 0.36, r * 0.14], [0, r * 0.4], [-r * 0.36, r * 0.14]], 'bone', 1.5);+ },+ magi(g, r) { // Three crowned kings bring gifts to the enthroned Virgin+ gFigure(g, r * 0.56, -r * 0.8, r * 1.2, { pose: 'sit', flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'hold' });+ g.disc(r * 0.4, -r * 0.1, r * 0.06, 'gold', 4, 8, { came: 0.008 });+ gFigure(g, -r * 0.05, -r * 0.8, r * 1.18, { pose: 'kneel', robe: 'emerald', mantle: 'ruby', crown: 1, beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'point', halo: false });+ gFigure(g, -r * 0.42, -r * 0.8, r * 1.32, { robe: 'violet', mantle: 'gold', crown: 1, beard: 1, arm: 'point', halo: false });+ gFigure(g, -r * 0.74, -r * 0.8, r * 1.3, { robe: 'teal', mantle: 'rubyLt', crown: 1, arm: 'hold', halo: false });+ star(g, -r * 0.1, r * 0.72, r * 0.11, 'yellow', 3);+ },+ crucifixion(g, r) { // Christ on the cross, the Virgin and Saint John, sun and moon+ g.poly([[-0.09 * r, -r * 0.92], [0.09 * r, -r * 0.92], [0.09 * r, r * 0.76], [-0.09 * r, r * 0.76]], 'emerald', 1.5, { came: 0.016 });+ g.poly([[-r * 0.58, r * 0.28], [r * 0.58, r * 0.28], [r * 0.58, r * 0.45], [-r * 0.58, r * 0.45]], 'emerald', 1.5, { came: 0.016 });+ gFigure(g, 0, -r * 0.84, r * 1.42, { robe: 'white', mantle: 'white', cross: 1, arm: 'out', halo: 'gold', hair: 'hair', beard: 1, sway: 0 });+ gFigure(g, -r * 0.62, -r * 0.88, r * 1.1, { robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray' });+ gFigure(g, r * 0.62, -r * 0.88, r * 1.1, { flip: 1, robe: 'white', mantle: 'rubyLt', arm: 'hold', attr: 'book' });+ g.disc(-r * 0.68, r * 0.66, r * 0.11, 'gold', 2, 8); g.poly([[r * 0.59, r * 0.74], [r * 0.76, r * 0.6], [r * 0.71, r * 0.76]], 'pearl', 2);+ },+ resurrection(g, r) { // Christ rises from the tomb holding the banner; soldiers sleep below+ g.poly([[-r * 0.72, -r * 0.84], [r * 0.72, -r * 0.84], [r * 0.72, -r * 0.42], [-r * 0.72, -r * 0.42]], 'bone', 2);+ g.line([[-r * 0.72, -r * 0.56], [r * 0.72, -r * 0.56]], 0.02, 'lead', 3); [-0.4, 0, 0.4].forEach((k) => g.disc(r * k, -r * 0.68, r * 0.09, 'purple', 3, 8, { came: 0.01 }));+ gFigure(g, 0, -r * 0.42, r * 1.25, { robe: 'white', mantle: 'rubyLt', cross: 1, arm: 'up', hair: 'hair', beard: 1, halo: 'gold' });+ g.poly([[r * 0.34, r * 0.72], [r * 0.34, r * 0.92], [r * 0.7, r * 0.84], [r * 0.34, r * 0.72]], 'white', 3, { came: 0.01 }); g.line([[r * 0.34, r * 0.92], [r * 0.34, -r * 0.1]], 0.02, 'gold', 3.2);+ for (const s of [-1, 1]) { g.disc(s * r * 0.82, -r * 0.5, r * 0.07, 'flesh', 3, 7, { came: 0.01 }); g.poly([[s * r * 0.6, -r * 0.86], [s * r * 0.92, -r * 0.86], [s * r * 0.9, -r * 0.54], [s * r * 0.7, -r * 0.54]], s > 0 ? 'emerald' : 'cobalt', 2.6); }+ },+ supper(g, r) { // The Last Supper: Christ at the centre of a laid table+ [-0.62, -0.31, 0.31, 0.62].forEach((k, i) => gBust(g, r * k, -r * 0.05, r * 0.92, { robe: ['emerald', 'ruby', 'amber', 'violet'][i], beard: i % 2 ? 1 : 0, hair: i === 3 ? 'grey' : 'hair' }));+ gBust(g, 0, -r * 0.02, r * 1.04, { robe: 'rubyLt', beard: 1, cross: 1 });+ g.poly([[-r * 0.92, -r * 0.5], [r * 0.92, -r * 0.5], [r * 0.92, -r * 0.2], [-r * 0.92, -r * 0.2]], 'white', 4);+ g.poly([[-r * 0.86, -r * 0.5], [r * 0.86, -r * 0.5], [r * 0.82, -r * 0.92], [-r * 0.82, -r * 0.92]], 'brown', 3.6);+ [-0.58, -0.2, 0.2, 0.58].forEach((k) => g.disc(r * k, -r * 0.3, r * 0.055, 'gold', 4.5, 6, { came: 0.008 }));+ },+ noah(g, r) { // The Ark on the waters, the dove returning+ [-0.82, -0.56].forEach((v, i) => g.poly([[-r, r * v], [r, r * v], [r, r * (v + 0.22)], [-r, r * (v + 0.18)]], i ? 'azure' : 'cobalt', 2));+ g.poly([[-r * 0.7, -r * 0.4], [r * 0.7, -r * 0.4], [r * 0.5, -r * 0.7], [-r * 0.5, -r * 0.7]], 'brown', 2.5);+ g.poly([[-r * 0.46, -r * 0.4], [r * 0.46, -r * 0.4], [r * 0.46, r * 0.02], [-r * 0.46, r * 0.02]], 'tan', 3);+ g.poly([[-r * 0.56, r * 0.0], [0, r * 0.32], [r * 0.56, r * 0.0]], 'ruby', 3.2);+ [-0.2, 0.2].forEach((k) => g.disc(r * k, -r * 0.18, r * 0.1, 'night', 3.4, 7, { came: 0.012 }));+ gBust(g, r * 0.2, -r * 0.3, r * 0.42, { robe: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', layer: 3.6 });+ g.disc(-r * 0.54, r * 0.64, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.72, r * 0.72], [-r * 0.54, r * 0.62], [-r * 0.36, r * 0.74], [-r * 0.54, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ shepherd(g, r) { // The Good Shepherd carries the lamb; a flock in green pasture+ g.poly([[-r, -r], [r, -r], [r, -r * 0.5], [-r, -r * 0.62]], 'green', 1.5);+ gFigure(g, 0, -r * 0.82, r * 1.5, { robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'hold', attr: 'staff' });+ g.disc(0, r * 0.26, r * 0.11, 'white', 4.5, 8, { came: 0.01 }); g.disc(r * 0.13, r * 0.3, r * 0.05, 'pearl', 4.8, 6, { came: 0.008 });+ for (const [x, y] of [[-0.68, -0.64], [0.66, -0.62], [-0.46, -0.78]]) { g.disc(r * x, r * y, r * 0.11, 'white', 3, 7, { came: 0.01 }); g.disc(r * x + r * 0.12, r * y + r * 0.05, r * 0.05, 'grey', 3.2, 5, { came: 0.008 }); }+ },+ david(g, r) { // King David with his harp+ gFigure(g, -r * 0.14, -r * 0.82, r * 1.42, { pose: 'sit', robe: 'violet', mantle: 'ruby', crown: 1, beard: 1, arm: 'out', halo: false });+ g.poly([[r * 0.26, -r * 0.6], [r * 0.7, r * 0.2], [r * 0.7, r * 0.46], [r * 0.16, -r * 0.36]], 'gold', 3, { came: 0.012 });+ g.line([[r * 0.2, -r * 0.4], [r * 0.2, r * 0.4]], 0.018, 'gold', 3.1); [0.3, 0.4, 0.5, 0.6].forEach((k) => g.line([[r * k, -r * 0.42 + r * (k - 0.2)], [r * k, r * 0.34]], 0.008, 'white', 3.2));+ g.poly([[-r * 0.86, -r * 0.84], [r * 0.5, -r * 0.84], [r * 0.5, -r * 0.7], [-r * 0.86, -r * 0.7]], 'tan', 1.8);+ },+};+function star(g, x, y, r, c, layer) { const p = []; for (let i = 0; i < 16; i++) { const a = i * PI / 8 + PI / 2, q = i % 2 ? r * 0.38 : r * (i % 4 ? 0.7 : 1); p.push([x + q * Math.cos(a), y + q * Math.sin(a)]); } g.poly(p, c, layer, { came: 0.01 }); }+function gBust(g, x, y, h, o = {}) { // head and shoulders, the head drawn by the painter+ const L = o.layer || 2, bx = [x - h * 0.22, y - h * 0.32, x + h * 0.22, y + h * 0.46], t = DRAPE_OF[o.robe || 'ruby'] || 'ruby';+ if (o.halo !== false) g.disc(x, y + h * 0.58, h * 0.17, o.halo || 'gold', L - 0.1, 10);+ g.poly([[x - h * 0.2, y - h * 0.3], [x + h * 0.2, y - h * 0.3], [x + h * 0.17, y + h * 0.3], [x + h * 0.06, y + h * 0.42], [x - h * 0.06, y + h * 0.42], [x - h * 0.17, y + h * 0.3]], o.robe || 'ruby', L, { tile: t, box: bx });+ const kind = o.face || (o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ g.face(x, y + h * 0.58, h * 0.108, h * 0.132, kind, L + 0.5, false);+ if (o.crown) g.poly([[x - h * 0.1, y + h * 0.66], [x + h * 0.1, y + h * 0.66], [x + h * 0.12, y + h * 0.78], [x + h * 0.05, y + h * 0.72], [x, y + h * 0.8], [x - h * 0.05, y + h * 0.72], [x - h * 0.12, y + h * 0.78]], 'gold', L + 0.7, { came: 0.008 });+ if (o.cross) for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[x + h * 0.17 * Math.cos(a - 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a - 0.24)], [x + h * 0.17 * Math.cos(a + 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a + 0.24)], [x, y + h * 0.58]], 'ruby', L + 0.2, { came: 0.008 }); }+}+// A jewelled ring of short leaded segments+function gRim(g, cx, cy, r0, r1, cols, n, layer) {+ g.disc(cx, cy, r1 + 0.02, 'lead', layer - 0.3, Math.max(10, n), { came: 0, jitter: 0 });+ for (let i = 0; i < n; i++) {+ const a = i * TAU / n, b = (i + 1) * TAU / n;+ g.poly([[cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)], [cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)], [cx + r1 * Math.cos(b), cy + r1 * Math.sin(b)], [cx + r0 * Math.cos(b), cy + r0 * Math.sin(b)]], cols[i % cols.length], layer, { came: -0.01 });+ }+}+// A lancet told as a stack of medallions over a diapered ground, with a jewelled border+function gLancetStory(g, x, w, y0, ys, rise, scenes, o = {}) {+ const c1 = o.c1 || 'ruby', c2 = o.c2 || 'cobalt', edge = o.edge || ['gold', 'ruby', 'white', 'cobalt'];+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse().slice(0)];+ g.withClip(outline, () => {+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, o.cell || 0.6, c1, c2, 'gold');+ const H = ys + rise * 0.5 - y0, N = scenes.length, step = H / N, R = Math.min(w * 0.44, step * 0.47);+ scenes.forEach((nm, i) => {+ const cy = y0 + step * (i + 0.5);+ const ground = (i % 2) ? (o.g1 || 'cobalt') : (o.g2 || 'ruby');+ gRim(g, x, cy, R, R * 1.055, ['gold', 'ruby', 'gold', 'cobalt'], 22, 1);+ g.disc(x, cy, R, ground, 1.3, 18, { came: 0 });+ g.withClip(ringPts(x, cy, R, 14), () => g.withXf(0, x, cy, 1, () => SCENES[nm](g, R)));+ if (i < N - 1) { const yy = y0 + step * (i + 1); for (const s of [-1, 1]) { g.disc(x + s * w * 0.3, yy, w * 0.058, 'gold', 1.2, 8, { came: 0.012 }); g.disc(x + s * w * 0.3, yy, w * 0.032, 'ruby', 1.5, 6, { came: 0.008 }); } }+ });+ // Border bricks follow the outline: thin, and each brick painted with a stem of leaves+ const bw = w * 0.062, stepB = 0.34;+ const stripe = (x0, y1, y2) => { let k = 0; g.poly([[x0, y1], [x0 + bw, y1], [x0 + bw, y2], [x0, y2]], 'lead', 3.5, { came: 0, jitter: 0 }); for (let y = y1; y < y2; y += stepB) { const c = edge[k++ % edge.length]; g.poly([[x0, y], [x0 + bw, y], [x0 + bw, y + stepB], [x0, y + stepB]], c, 3.8, { came: -0.012, orn: c === 'gold' ? 'gold' : c === 'white' ? 'white' : null, box: [x0, y, x0 + bw, y + stepB] }); } };+ stripe(x - w / 2, y0, ys + rise * 0.1); stripe(x + w / 2 - bw, y0, ys + rise * 0.1);+ // Saddle bars (iron armature)+ for (let y = y0 + 1.1; y < ys + rise * 0.6; y += 1.1) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.045], [x - w / 2, y + 0.045]], 'lead', 7, { came: 0, jitter: 0 });+ });+}+// A lancet filled by one monumental standing figure under a painted canopy (reads from the floor)+function gLancetSaint(g, x, w, y0, ys, rise, fig, o = {}) {+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse()];+ const gnd = o.ground || 'cobalt';+ g.withClip(outline, () => {+ const figY = y0 + 1.05, figH = (ys + rise * 0.1 - figY) * 0.84;+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, 0.8, gnd, o.ground2 || 'ruby', null);+ // Pedestal with an inscription band+ g.poly([[x - w * 0.42, y0], [x + w * 0.42, y0], [x + w * 0.42, y0 + 0.62], [x - w * 0.42, y0 + 0.62]], 'bone', 1.5);+ g.poly([[x - w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 1.02], [x - w * 0.46, y0 + 1.02]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.46, y0 + 0.62, x + w * 0.46, y0 + 1.02] });+ for (let i = 0; i < 6; i++) g.line([[x - w * 0.34 + i * w * 0.13, y0 + 0.28], [x - w * 0.3 + i * w * 0.13, y0 + 0.44]], 0.035, 'paint', 2);+ // Canopy: gable, pinnacles, little windows+ const cy = figY + figH + 0.02, cH = ys + rise * 0.86 - cy;+ g.poly([[x - w * 0.48, cy], [x + w * 0.48, cy], [x + w * 0.48, cy + cH * 0.36], [x - w * 0.48, cy + cH * 0.36]], 'white', 1.6, { orn: 'white', box: [x - w * 0.48, cy, x + w * 0.48, cy + cH * 0.36] });+ g.poly([[x - w * 0.42, cy + cH * 0.34], [x + w * 0.42, cy + cH * 0.34], [x, cy + cH]], 'yellow', 1.7, { orn: 'gold', box: [x - w * 0.42, cy + cH * 0.34, x + w * 0.42, cy + cH] });+ g.poly([[x - w * 0.2, cy + cH * 0.38], [x + w * 0.2, cy + cH * 0.38], [x, cy + cH * 0.72]], 'ruby', 1.9, { came: 0.016 });+ for (const s of [-1, 1]) { g.poly([[x + s * w * 0.28, cy + cH * 0.02], [x + s * w * 0.42, cy + cH * 0.02], [x + s * w * 0.42, cy + cH * 0.3], [x + s * w * 0.28, cy + cH * 0.3]], 'night', 1.8, { came: 0.014 }); g.poly([[x + s * w * 0.3, cy + cH * 0.36], [x + s * w * 0.46, cy + cH * 0.36], [x + s * w * 0.38, cy + cH * 0.85]], 'white', 1.8); }+ gFigure(g, x, figY, figH, Object.assign({ layer: 2.5, sx: Math.min(1, w * 1.72 / figH) }, fig));+ for (let y = y0 + 1.5; y < ys + rise * 0.6; y += 1.5) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.05], [x - w / 2, y + 0.05]], 'lead', 9, { came: 0, jitter: 0 });+ });+}+function gOculus(g, cx, cy, r, o = {}) { // head of the window: a sexfoil with an angel or Christ+ g.withClip(ringPts(cx, cy, r, 20), () => {+ g.disc(cx, cy, r, o.ground || 'cobalt', 1, 20);+ for (let i = 0; i < 6; i++) { const a = i * PI / 3 + PI / 6; g.disc(cx + r * 0.68 * Math.cos(a), cy + r * 0.68 * Math.sin(a), r * 0.27, i % 2 ? 'ruby' : 'gold', 1.4, 8); }+ g.disc(cx, cy, r * 0.56, o.inner || 'rubyLt', 1.8, 14);+ g.withXf(0, cx, cy, 1, () => { if (o.bust) gBust(g, 0, -r * 0.45, r * 0.95, o.bust); else gFigure(g, 0, -r * 0.5, r * 0.98, { wings: 'gold', robe: 'white', mantle: 'emerald', arm: 'orant' }); });+ });+}++// A complete two-light window: lancets, oculus, base field. Local plane z=0, +Z = interior.+function makeWindowGlass(w, y0, ys, rise, seed, fill) {+ const g = new Glass(seed);+ const outline = archOutline(w, y0, ys, rise, 8);+ g.raw(outline.slice(), 'lead', -0.4, 0);+ g.withClip(outline, () => gDiaper(g, -w / 2, ys, w / 2, ys + rise, 0.55, 'cobalt', 'ruby', 'gold', 0));+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96;+ const syl = ys + rise * 0.02;+ fill.light(g, -lx, lw, y0, syl, lr, 0); fill.light(g, lx, lw, y0, syl, lr, 1);+ const oc = windowHead(w, ys, rise);+ gOculus(g, 0, oc.y, oc.r + 0.03, fill.oculus || {});+ return g.b;+}+function windowHead(w, ys, rise) { const r = w * 0.225; return { r, y: ys + rise * 0.62 }; }++// ==================================================================================+// STONE TRACERY for a two-light window (plane z=0, bars deep along Z)+// ==================================================================================+function addTracery(b, w, y0, ys, rise, o = {}) {+ const bw = o.bar || 0.26, bd = o.depth || 0.42, sec = traceryBar(bw, bd), secT = hexBar(bw * 0.62, bd * 0.8);+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96, syl = ys + rise * 0.02;+ // Central mullion with shaft ring and capital+ b.at(M.T(0, 0, 0), () => {+ b.sweep([[0, y0 - 0.05], [0, syl]], sec, {});+ b.at(M.T(0, syl, 0), () => b.lathe([[bw * 0.52, -0.3], [bw * 0.56, -0.2], [bw * 0.9, 0.02], [bw * 0.5, 0.06]], 6));+ });+ for (const s of [-1, 1]) {+ const arc = pointedArc(lw, lr, 5).map((p) => [s * lx + p[0], syl + p[1]]);+ b.sweep(arc, sec, {});+ // cusped trefoil head inside each lancet+ const cr = lw * 0.3, cy = syl + lr * 0.34;+ const cusp = []; for (let i = 0; i <= 8; i++) { const a = rad(-30 + i * 240 / 8); const R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([s * lx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, { caps: false });+ b.sweep([[s * (lx + lw / 2), cy - cr * 0.4], cusp[0]], secT, { caps: false });+ b.sweep([[s * (lx - lw / 2), cy - cr * 0.4], cusp[8]], secT, { caps: false });+ }+ // Oculus with six foils+ const oc = windowHead(w, ys, rise);+ b.sweep(ringPts(0, oc.y, oc.r, 14), sec, { closed: true });+ const foils = []; for (let i = 0; i < 18; i++) { const a = i * TAU / 18; const R = oc.r * (0.62 + 0.3 * Math.pow(Math.abs(Math.cos(a * 3)), 0.8)); foils.push([R * Math.cos(a), oc.y + R * Math.sin(a)]); }+ b.sweep(foils, secT, { closed: true, crease: 40 });+}++// ==================================================================================+// PIERCED PARAPET with quatrefoil panels+// ==================================================================================+function quatrefoil(cx, cy, r, n = 16) { const p = []; for (let i = 0; i < n; i++) { const a = i * TAU / n; const R = r * (0.72 + 0.28 * Math.abs(Math.cos(2 * a))); p.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); } return p; }+function addParapet(b, x0, x1, y, h, th, o = {}) {+ const n = Math.max(1, Math.round((x1 - x0) / (o.pitch || 1.05))), holes = [];+ const r = Math.min(h * 0.34, (x1 - x0) / n * 0.4);+ for (let i = 0; i < n; i++) holes.push(quatrefoil(x0 + (i + 0.5) * (x1 - x0) / n, y + h * 0.5, r, o.nh || 12));+ b.slab([[x0, y], [x1, y], [x1, y + h], [x0, y + h]], holes, -th / 2, th / 2, { sr: 0.6 });+ const cap = barSection(0.14, th + 0.14);+ b.sweep([[x0, y + h + 0.04], [x1, y + h + 0.04]], cap, {});+ b.sweep([[x0, y - 0.02], [x1, y - 0.02]], barSection(0.1, th + 0.1), { s: 0.8 });+}++// A wimperg: gable over a window with crockets, finial and a blind trefoil+function addGable(b, w, h, z0, th, y, o = {}) {+ const lod = o.lod === undefined ? 1 : o.lod;+ const holes = o.solid ? [] : [ringPts(0, y + h * 0.36, Math.min(w * 0.13, h * 0.2), 14)];+ if (!o.noField) b.slab([[-w / 2, y], [w / 2, y], [0, y + h]], holes, z0, z0 + th, { sf: 1, sb: 0.8 });+ const rake = barSection(0.18, th + 0.14);+ b.sweep([[-w / 2 - 0.06, y - 0.02], [0, y + h + 0.05]], rake, { z: z0 + th / 2 });+ b.sweep([[0, y + h + 0.05], [w / 2 + 0.06, y - 0.02]], rake, { z: z0 + th / 2 });+ const n = o.crockets === undefined ? Math.max(2, Math.round(Math.hypot(w / 2, h) / 1.05)) : o.crockets, sz = o.size || 0.46;+ const zc = z0 + th / 2;+ crocketsAlong(b, [-w / 2, y + 0.05, zc], [-0.03, y + h * 0.97, zc], n, sz, [-h, w / 2, 0], lod);+ crocketsAlong(b, [w / 2, y + 0.05, zc], [0.03, y + h * 0.97, zc], n, sz, [h, w / 2, 0], lod);+ b.at(M.T(0, y + h * 0.98, zc), () => addFinial(b, o.finial || 0.95, lod));+}+// A small blind trefoil-headed arcade on a wall face (plane z=0, moulding stands proud +Z)+function addBlindArcade(b, x0, x1, y0, ys, n, o = {}) {+ const w = (x1 - x0) / n, sec = hexBar(o.bar || 0.12, o.depth || 0.16), rise = w * 0.95;+ for (let i = 0; i < n; i++) {+ const x = x0 + (i + 0.5) * w;+ b.sweep(pointedArc(w * 0.9, rise, 4).map((p) => [x + p[0], ys + p[1]]), sec, { z: 0.04, caps: false });+ }+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * w;+ b.at(M.T(x, 0, 0.06), () => b.lathe([[0.1, y0], [0.065, y0 + 0.18], [0.065, ys - 0.14], [0.13, ys], [0.08, ys + 0.04]], 5));+ }+}++// ==================================================================================+// RIBBED VAULT: pointed-arc ribs, curved webs, carved or gilded boss.+// ==================================================================================+function arcH(L, H, f) { // height of a two-centred arc of span L, rise H at fraction f+ const c = (H * H - L * L / 4) / L, r = c + L / 2, d = Math.abs(f - 0.5) * L;+ return Math.sqrt(Math.max(0, r * r - (d + c) * (d + c)));+}+function addRib(b, A, B, yS, rise, sec, n = 8, half = false) { // rib between plan points A,B in their vertical plane+ const dx = B[0] - A[0], dz = B[1] - A[1], L = Math.hypot(dx, dz), ang = Math.atan2(dz, dx) * 180 / PI;+ const pts = [];+ if (half) for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L * 2, rise, t / 2)]); }+ else for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L, rise, t)]); }+ // plane: local X along A->B, local Y up+ b.at(M.of(M.T(A[0], 0, A[1]), M.Ry(-ang)), () => b.sweep(pts, sec, { caps: false }));+}+// One curved web between a side arc a-b and the crown c, faces downward whichever way the plan runs+function addVaultCell(A, a, b, c, yS, yC, sideRiseV, nS = 8, nT = 6) {+ const La = Math.hypot(c[0] - a[0], c[1] - a[1]), Lb = Math.hypot(c[0] - b[0], c[1] - b[1]), Lab = Math.hypot(b[0] - a[0], b[1] - a[1]), rise = yC - yS;+ const cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ A.grid((s, t) => {+ const px = lerp(a[0], c[0], t) * (1 - s) + lerp(b[0], c[0], t) * s, pz = lerp(a[1], c[1], t) * (1 - s) + lerp(b[1], c[1], t) * s;+ const ruled = lerp(yS + arcH(Lab, sideRiseV, s), yC, t);+ const dA = arcH(La * 2, rise, t / 2) - rise * t, dB = arcH(Lb * 2, rise, t / 2) - rise * t;+ const y = ruled + (1 - s) * (1 - s) * dA + s * s * dB;+ return [px, y, pz, 0.72 + 0.28 * t];+ }, nS, nT, { flip: cross < 0, metric: true });+}+function addRibVault(F, A, corners, yS, yC, o = {}) {+ // corners: 4 plan points in order; web cells between each side and the crown+ const c0 = corners, cx = (c0[0][0] + c0[1][0] + c0[2][0] + c0[3][0]) / 4, cz = (c0[0][1] + c0[1][1] + c0[2][1] + c0[3][1]) / 4;+ const crown = [cx, yC, cz], nS = o.nS || 8, nT = o.nT || 6, rise = yC - yS;+ const sideRise = o.sideRise || ((i) => rise * 0.94);+ for (let k = 0; k < 4; k++) {+ if (o.skipSide && o.skipSide[k]) continue;+ addVaultCell(A, c0[k], c0[(k + 1) % 4], [cx, cz], yS, yC, sideRise(k), nS, nT);+ }+ const sec = o.ribSec || rollSection(0.32, 0.42, 2, 0.04, 2);+ for (const c of c0) addRib(F, c, [cx, cz], yS, rise, sec, 8, true);+ for (let k = 0; k < 4; k++) if (!(o.skipRib && o.skipRib[k])) addRib(F, c0[k], c0[(k + 1) % 4], yS, sideRiseRib(o, k, rise), o.sideRibSec || sec, 8);+ return crown;+}+const sideRiseRib = (o, k, rise) => (o.sideRise ? o.sideRise(k) : rise * 0.94);+function addBoss(G, F, c, r = 0.42) { // carved foliate boss with gilding+ G.at(M.T(c[0], c[1] - 0.02, c[2]), () => {+ G.lathe([[r * 0.25, -r * 0.54], [r * 0.6, -r * 0.5], [r * 0.95, -r * 0.3], [r, -r * 0.08], [r * 0.7, r * 0.05]], 10);+ for (let i = 0; i < 8; i++) addLeaf(G, M.of(M.T(0, -r * 0.22, 0), M.Ry(i * 45), M.T(r * 0.72, 0, 0), M.Rz(160)), r * 0.72);+ });+}++// ==================================================================================+// CLUSTERED PIER: a lobed section of shafts around a core, with bases and foliate capitals+// ==================================================================================+function lobedRing(core, shafts, n = 64) {+ const out = [];+ for (let i = 0; i < n; i++) {+ const a = TAU * i / n, c = Math.cos(a), s = Math.sin(a);+ let best = core, nx = c, nz = s;+ for (const sh of shafts) {+ // ray from the origin against each shaft circle+ const b = c * sh[0] + s * sh[1], disc = b * b - (sh[0] * sh[0] + sh[1] * sh[1] - sh[2] * sh[2]);+ if (disc >= 0) { const t = b + Math.sqrt(disc); if (t > best) { best = t; nx = (t * c - sh[0]) / sh[2]; nz = (t * s - sh[1]) / sh[2]; } }+ }+ out.push([best * c, best * s, nx, nz]);+ }+ return out;+}+function addExtrudedRing(b, ring, y0, y1, o = {}) {+ const s0 = o.s0 === undefined ? 1 : o.s0, s1 = o.s1 === undefined ? 1 : o.s1, k0 = o.k0 || 1, k1 = o.k1 || 1, N = ring.length;+ const base = b.count; let perim = 0;+ for (let i = 0; i <= N; i++) {+ const r = ring[i % N], rp = ring[(i + N - 1) % N];+ if (i > 0) perim += Math.hypot(r[0] - rp[0], r[1] - rp[1]);+ const shade = 0.5 + 0.5 * Math.pow(Math.abs(r[2] * r[0] + r[3] * r[1]) / (Math.hypot(r[0], r[1]) || 1), 2);+ b.v(r[0] * k0, y0, r[1] * k0, r[2], 0, r[3], perim, y0, s0 * shade);+ b.v(r[0] * k1, y1, r[1] * k1, r[2], 0, r[3], perim, y1, s1 * shade);+ }+ for (let i = 0; i < N; i++) b.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+}+function addCapital(b, ring, y, h, k, o = {}) { // bell with crocket leaves on each shaft, moulded abacus+ const N = ring.length;+ addExtrudedRing(b, ring, y, y + h * 0.1, { k0: 1.0, k1: 1.03, s0: 0.8 });+ addExtrudedRing(b, ring, y + h * 0.1, y + h * 0.82, { k0: 1.03, k1: k, s0: 0.62, s1: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.82, y + h * 0.9, { k0: k, k1: k + 0.1, s0: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.9, y + h, { k0: k + 0.1, k1: k + 0.12, s0: 0.95 });+ const top = ring.map((r) => [r[0] * (k + 0.12), r[1] * (k + 0.12)]);+ const ids = top.map((p) => b.v(p[0], y + h, p[1], 0, 1, 0, undefined, undefined, 0.9)); for (let i = 1; i < N - 1; i++) b.tri(ids[0], ids[i + 1], ids[i]);+ const leaves = o.leaves || 12;+ for (let i = 0; i < leaves; i++) {+ const a = i * 360 / leaves + (o.phase || 0), idx = Math.round(i / leaves * N) % N, r = ring[idx], R = Math.hypot(r[0], r[1]);+ addLeaf(b, M.of(M.T(0, y + h * 0.1, 0), M.Ry(-a), M.T(R * 1.02, 0, 0)), h * (i % 2 ? 0.72 : 0.56), { coarse: 1 });+ }+}++// The bay plan shared by every straight module (+Z side of the axis)+const zn = P.naveSpan / 2, zaw = zn + P.aisleSpan, BAY = P.bay;+// Window openings follow the bay, so changing the bay length re-proportions the glass and tracery+const WIN = {+ aW: BAY - 1.95, aSill: 4.3, aSp: P.pierTop - 1, aRise: 5.6 * (BAY - 1.95) / 4.65,+ cW: BAY - 1.55, cSill: P.triforiumTop + 0.45, cSp: P.vaultSpring + 4.8, cRise: 6.7 * (BAY - 1.55) / 5.05,+};+const windowArgs = { aisle: [WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise], clere: [WIN.cW, WIN.cSill, WIN.cSp, WIN.cRise] };+const mkBatches = () => { const D = new Batch(1); D.noColor = true; return { A: new Batch(TILE.ashlar), F: new Batch(TILE.fine), L: new Batch(TILE.lead), G: new Batch(TILE.gold), O: new Batch(TILE.oak), I: new Batch(TILE.iron), D }; };+// A stain hanging from a ledge on a wall facing local +z: runoff, drips, soft shadow or rising damp. Few vertices, crisp detail.+const STAIN_RNG = mkRng(4242);+function addStain(D, x0, x1, y, z, drop, kind = 'streak') {+ const R = DECAL_UV[kind], n = Math.max(1, Math.round((x1 - x0) / 4)), w = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = lerp(x0, x1, i / n), c = lerp(x0, x1, (i + 1) / n), span = w * Math.min(1, (c - a) / 5), u0 = R[0] + 0.002 + STAIN_RNG() * (w - span - 0.004);+ const q = [D.v(a, y, z, 0, 0, 1, u0, R[1], 1), D.v(a, y - drop, z, 0, 0, 1, u0, R[3], 1), D.v(c, y - drop, z, 0, 0, 1, u0 + span, R[3], 1), D.v(c, y, z, 0, 0, 1, u0 + span, R[1], 1)];+ D.quad(q[0], q[1], q[2], q[3]);+ }+}+// A real recess in a wall facing local +z, whose slab already has the opening cut: back plate, moulded frame, a sill+function addRecess(A, F, outline, depth) {+ const o = area2(outline) > 0 ? outline : outline.slice().reverse();+ let x0 = 1e9, x1 = -1e9, y0 = 1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); }+ A.face(o.map((p) => [p[0], p[1], -depth]));+ F.sweep(o, rollSection(0.1, 0.22, 1, 0.03, 2), { closed: true, crease: 40, z: -0.02 });+ F.sweep([[x0 - 0.16, y0 - 0.06], [x1 + 0.16, y0 - 0.06]], stringSection(0.16, 0.14), {});+}+function placeSet(name, set, pos, rot, parent, extra = {}) {+ const o = { pos, rot: [0, rot, 0], parent, scale: extra.scale };+ place(name + '_Ashlar', set.A, MAT.ashlar, o);+ place(name + '_Stone', set.F, MAT.fine, o);+ place(name + '_Lead', set.L, MAT.lead, o);+ place(name + '_Gilt', set.G, MAT.gold, o);+ place(name + '_Oak', set.O, MAT.oak, o);+ place(name + '_Iron', set.I, MAT.iron, o);+ if (set.D) place(name + '_Weathering', set.D, MAT.decal, o);+}+// Glass pair: interior face + the same panes mirrored as the exterior face+function placeGlass(name, batch, pos, rotY, parent, keepOutside, k = 1) {+ place(name + '_In', batch, MAT.glassIn, { pos, rot: [0, rotY, 0], scale: [k, k, 1], parent });+ place(name + '_Out', keepOutside ? batch : glassOutside(batch), MAT.glassOut, { pos, rot: [0, rotY, 0], scale: [k, k, -1], parent });+}+// By day a window is dark from outside and the story cannot be read, so its outer skin is only the+// window's outline over a mosaic of pot-metals: the colour hints through for a few triangles.+function glassOutside(batch) {+ if (batch.outer) return batch.outer;+ if (!batch.outline) return batch;+ const g = new Glass(7), o = batch.outline;+ let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); }+ g.raw(o, 'lead', -0.4, 0);+ g.withClip(o, () => gDiaper(g, x0, y0, x1, y1, 0, batch.count % 2 ? 'ruby' : 'cobalt', null, null, 0));+ batch.outer = g.b;+ return g.b;+}++function buildNaveBay(o = {}) {+ const set = o.into || mkBatches(), { A, F, L, G } = set;+ const B = o.B || BAY, ww = o.ww || BAY - 2.1, sp = P.pierTop, archRise = o.archRise || 5.1, thick = 0.85;+ const zw = o.zw === undefined ? zn : o.zw;+ if (o.part !== 'aisle') {+ // ---- Arcade wall with the moulded pointed arch ----+ const arc = pointedArc(ww, archRise, 7).map((p) => [p[0], p[1] + sp]);+ A.at(M.T(0, 0, zw), () => {+ A.slab([[-B / 2, sp], [-ww / 2, sp], ...arc, [ww / 2, sp], [B / 2, sp], [B / 2, P.triforiumSill], [-B / 2, P.triforiumSill]], [], -thick / 2, thick / 2, { noOuterSides: true });+ });+ F.at(M.T(0, 0, zw), () => { F.sweep(arc, rollSection(0.82, thick + 0.25, 2, 0.08, 3), {}); });+ // Hood mould over the arch on the nave side, with carved label stops+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ const hood = pointedArc(ww + 1.9, archRise + 1.0, 6).map((p) => [p[0], p[1] + sp]);+ F.sweep(hood, stringSection(0.16, 0.14), {});+ });+ // ---- Triforium: string course, four cusped arches, dark wall passage ----+ const yT = P.triforiumSill, yT2 = P.triforiumTop, nT = o.nT || 4, tw = o.tw || (BAY - 1.72) / 4, ty = yT + 0.62, tspr = yT2 - 1.5, gapT = nT > 2 ? 0.08 : 0;+ const trfX = (i) => (i + 0.5 - nT / 2) * tw + (i < nT / 2 ? -gapT : gapT);+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ F.sweep([[-B / 2, yT], [B / 2, yT]], stringSection(0.34, 0.3), {});+ F.sweep([[-B / 2, yT2], [B / 2, yT2]], stringSection(0.3, 0.26), {});+ // Colonnette arcade standing in the openings+ for (let i = 0; i < nT; i++) {+ const x = trfX(i);+ const a = pointedArc(tw * 0.92, 0.98, 4).map((p) => [x + p[0], tspr + p[1]]);+ F.sweep(a, hexBar(0.13, 0.26), { z: 0.05 });+ F.sweep(ringPts(x, tspr + 0.32, 0.17, 6), hexBar(0.05, 0.1), { z: 0.05, closed: true, s: 0.9 });+ }+ for (let i = 0; i <= nT; i++) {+ const mid = i === nT / 2;+ const x = (i - nT / 2) * tw + (i < nT / 2 ? -gapT : (mid ? 0 : gapT));+ const r = mid ? 0.14 : 0.085;+ F.at(M.T(x, 0, 0.05), () => F.lathe([[r * 1.7, ty - 0.02], [r, ty + 0.2], [r, tspr - 0.2], [r * 1.9, tspr - 0.01], [r * 1.5, tspr + 0.03]], 6));+ }+ });+ const holes = []; // openings through the inner skin+ for (let i = 0; i < nT; i++) { const x = trfX(i); holes.push(archOutline(tw * 0.92, ty, tspr, 0.98, 6).map((p) => [x + p[0], p[1]])); }+ A.at(M.T(0, 0, zw - thick / 2 + 0.22), () => A.slab([[-B / 2, yT], [B / 2, yT], [B / 2, yT2], [-B / 2, yT2]], holes, -0.22, 0.22, { noOuterSides: true, sf: 0.45, sr: 0.5 }));+ // Passage back wall and floor: seen only through the arcade, kept dark+ A.shade(0.34, () => {+ A.face([[B / 2, yT, zw + 0.92], [-B / 2, yT, zw + 0.92], [-B / 2, yT2 + 0.5, zw + 0.92], [B / 2, yT2 + 0.5, zw + 0.92]]);+ A.face([[-B / 2, yT + 0.02, zw + 0.92], [B / 2, yT + 0.02, zw + 0.92], [B / 2, yT + 0.02, zw - thick / 2 + 0.44], [-B / 2, yT + 0.02, zw - thick / 2 + 0.44]]);+ A.face([[-B / 2, yT2 + 0.2, zw - thick / 2 + 0.44], [B / 2, yT2 + 0.2, zw - thick / 2 + 0.44], [B / 2, yT2 + 0.2, zw + 0.92], [-B / 2, yT2 + 0.2, zw + 0.92]]);+ });+ // ---- Clerestory wall, window hole, tracery ----+ const cw = o.cw || WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = o.cRise || WIN.cRise;+ const yE = P.eaves;+ A.at(M.T(0, 0, zw), () => A.slab([[-B / 2, yT2], [B / 2, yT2], [B / 2, yE], [-B / 2, yE]], [archOutline(cw, cSill, cSp, cRise, 9)], -0.42, 0.42, { noOuterSides: true, sr: 0.66 }));+ F.at(M.T(0, 0, zw), () => {+ if (o.lights === 1) addLancetTracery(F, cw, cSill, cSp, cRise);+ else addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.44 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 9).slice(0), rollSection(0.14, 0.86, 1, 0.03, 2), { closed: true, s: 0.92, crease: 40 });+ });+ // Exterior: hood mould, wimperg gable, cornice, pierced parapet, corbel table+ set.D.at(M.T(0, 0, zw + 0.42), () => addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.02, 3.6));+ F.at(M.T(0, 0, zw + 0.42), () => {+ F.sweep(pointedArc(cw + 0.5, cRise + 0.3, 6).map((p) => [p[0], p[1] + cSp]), hexBar(0.22, 0.26), {});+ addGable(F, Math.min(cw + 0.9, B * 0.96), 3.4, 0.02, 0.2, cSp + cRise * 0.52, { size: 0.44, finial: 0.8, lod: 0, crockets: 3 });+ F.sweep([[-B / 2, yT2 - 0.02], [B / 2, yT2 - 0.02]], stringSection(0.3, 0.2), {});+ F.sweep([[-B / 2, yE - 0.12], [B / 2, yE - 0.12]], stringSection(0.34, 0.38), {});+ const nCorb = Math.round(B / 1.1);+ for (let i = 0; i < nCorb; i++) { const x = -B / 2 + (i + 0.5) * B / nCorb; F.box(x - 0.13, x + 0.13, yE - 0.62, yE - 0.32, 0, 0.26, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zw + 0.3), () => addParapet(F, -B / 2, B / 2, yE, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Upper wall top closing the gutter+ A.face([[-B / 2, yE, zw + 0.42], [B / 2, yE, zw + 0.42], [B / 2, yE, zw - 0.42], [-B / 2, yE, zw - 0.42]], 0.7);+ }+ if (o.part !== 'upper') {+ // ---- Aisle roof (lean-to in lead) ----+ if (!o.noAisleRoof) {+ const z0 = zn + 0.42, z1 = zaw + 1.05, y0r = P.aisleRoofHigh, y1r = P.aisleWallTop - 0.2;+ L.face([[-B / 2, y0r, z0], [-B / 2, y1r, z1], [B / 2, y1r, z1], [B / 2, y0r, z0]]);+ }+ if (!o.open) {+ // ---- Aisle wall ----+ const wa = WIN.aW, sill = WIN.aSill, aSp = WIN.aSp, aRise = WIN.aRise, zOut = zaw + 0.45;+ A.at(M.T(0, 0, zOut), () => A.slab([[-B / 2, 0], [B / 2, 0], [B / 2, P.aisleWallTop], [-B / 2, P.aisleWallTop]], [archOutline(wa, sill, aSp, aRise, 9)], -0.45, 0.45, { noOuterSides: true, sr: 0.7 }));+ F.at(M.T(0, 0, zOut), () => {+ addTracery(F, wa, sill, aSp, aRise, { bar: 0.25, depth: 0.42 });+ F.sweep(archOutline(wa, sill, aSp, aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ });+ // Exterior of the aisle wall+ set.D.at(M.T(0, 0, zaw + 0.9), () => {+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.02, 4.4);+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.02, 2.6, 'drip');+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.03, 0.7, 'soft');+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.03, 0.9, 'soft');+ addStain(set.D, -B / 2, B / 2, 5.2, 0.03, 5.2, 'ground');+ addStain(set.D, -B / 2, B / 2, 2.2, 0.04, 1.9, 'lichen');+ });+ F.at(M.T(0, 0, zaw + 0.9), () => {+ F.sweep([[-B / 2, sill - 0.2], [B / 2, sill - 0.2]], stringSection(0.34, 0.3), {});+ F.sweep(pointedArc(wa + 0.46, aRise + 0.28, 6).map((p) => [p[0], p[1] + aSp]), hexBar(0.22, 0.26), {});+ addGable(F, wa + 0.8, 3.1, 0.02, 0.2, aSp + 2.95, { size: 0.44, finial: 0.8, crockets: 4 });+ F.sweep([[-B / 2, P.aisleWallTop - 0.12], [B / 2, P.aisleWallTop - 0.12]], stringSection(0.32, 0.34), {});+ // Plinth with a chamfered weathering+ F.sweep([[-B / 2, 0.55], [B / 2, 0.55]], stringSection(0.4, 0.42), {});+ F.sweep([[-B / 2, 1.3], [B / 2, 1.3]], stringSection(0.2, 0.2), {});+ addBlindArcade(F, -B / 2 + 0.9, B / 2 - 0.9, 1.45, 2.65, 4, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(0, 0, zaw + 0.72), () => addParapet(F, -B / 2, B / 2, P.aisleWallTop, 1.0, 0.24, { pitch: 1.3 }));+ A.face([[-B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw], [-B / 2, P.aisleWallTop, zaw]], 0.7);+ // Interior dado: blind arcade with a niche for a statue+ F.at(M.of(M.T(0, 0, zaw), M.Ry(180)), () => {+ F.sweep([[-B / 2, sill - 0.14], [B / 2, sill - 0.14]], stringSection(0.26, 0.22), {});+ addBlindArcade(F, -B / 2 + 0.5, -1.05, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ addBlindArcade(F, 1.05, B / 2 - 0.5, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ F.sweep([[-B / 2, 0.42], [B / 2, 0.42]], stringSection(0.4, 0.34), {});+ // Niche canopy over the statue+ addNicheFrame(F, 0, 0.6, 3.9, 1.05);+ });+ }+ // ---- Aisle vault ----+ if (!o.noAisleVault) {+ const z0 = zn + thick / 2, z1 = zaw;+ const crown = addRibVault(F, A, [[-B / 2, z0], [B / 2, z0], [B / 2, z1], [-B / 2, z1]], sp, P.aisleCrown - 0.3, { sideRise: (k) => (k % 2 === 0 ? (k === 0 ? archRise + 0.1 : 5.0) : 5.2), nS: 6, nT: 4, ribSec: rollSection(0.26, 0.34, 2, 0.03, 2) });+ addBoss(G, F, crown, 0.32);+ }+ }+ return set;+}+// Canopied niche frame: moulded opening, gablet with crockets. Statue placed separately.+function addNicheFrame(F, x, y0, yTop, w) {+ F.at(M.T(x, 0, 0), () => {+ F.shade(0.5, () => F.face([[-w / 2, y0, -0.005], [w / 2, y0, -0.005], [w / 2, yTop - 0.75, -0.005], [0, yTop - 0.1, -0.005], [-w / 2, yTop - 0.75, -0.005]]));+ F.box(-w / 2 - 0.05, w / 2 + 0.05, y0 - 0.14, y0 + 0.08, 0, 0.5, { s: 0.92 });+ F.sweep(archOutline(w, y0, yTop - 0.82, 0.72, 5).slice(2), barSection(0.11, 0.2), { z: 0.08 });+ for (const s of [-1, 1]) { F.at(M.T(s * w / 2, 0, 0.1), () => F.lathe([[0.07, y0 + 0.05], [0.05, y0 + 0.12], [0.05, yTop - 0.9], [0.09, yTop - 0.82], [0.05, yTop - 0.78]], 6)); }+ addGable(F, w + 0.24, 0.92, 0.06, 0.1, yTop - 0.98, { size: 0.19, crockets: 2, finial: 0.32, solid: true, lod: 0 });+ });+}++// Clustered nave pier with the vault shafts rising to the springing, on +Z = nave... shafts toward -Z+function buildPier() {+ const set = mkBatches(), { F, A, G } = set;+ const core = 0.8, big = 0.25, small = 0.14;+ const shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.1) * Math.cos(a), (core + 0.1) * Math.sin(a), big]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; shafts.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 48);+ const yB = 1.15, yC = P.pierTop - 1.05;+ // moulded base: octagonal plinth, roll, scotia, then the lobed shaft+ F.lathe([[1.7, 0], [1.7, 0.56], [1.58, 0.62]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.62, 0.86, { k0: 1.24, k1: 1.27 });+ addExtrudedRing(F, ring, 0.86, yB, { k0: 1.27, k1: 1.0, s0: 0.75, s1: 0.72 });+ addExtrudedRing(F, ring, yB, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.05, 1.2, { leaves: 14 });+ // The vault shafts rising from the capital up the nave wall (toward -Z)+ const upper = [[0, -(core + 0.1), big], [-0.5, -(core - 0.04), small], [0.5, -(core - 0.04), small]];+ for (const u of upper) {+ F.at(M.T(u[0], 0, u[1] + 0.44), () => {+ const r = u[2], yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, P.triforiumSill - 0.1], [r * 1.5, P.triforiumSill + 0.06], [r, P.triforiumSill + 0.22], [r, yS - 0.85], [r * 1.5, yS - 0.8], [r * 1.14, yS - 0.7], [r * 2.2, yS]], 8);+ for (let i = 0; i < 4; i++) addLeaf(F, M.of(M.T(0, yS - 0.72, 0), M.Ry(i * 90 + 45), M.T(r * 1.1, 0, 0)), 0.56, { coarse: 1 });+ });+ }+ // Statue stage on the nave face: corbel below, canopy above (the figure is placed separately)+ const zF = -(core + 0.34);+ F.at(M.T(0, 0, zF), () => {+ F.lathe([[0.1, 5.0], [0.28, 5.25], [0.44, 5.6], [0.5, 5.72], [0.44, 5.78]], 8, { s: 0.85 });+ for (let i = 0; i < 6; i++) addLeaf(F, M.of(M.T(0, 5.04, 0), M.Ry(i * 60), M.T(0.14, 0, 0)), 0.66, { coarse: 1 });+ // Canopy over the statue: three arched gablets and a little spire+ F.at(M.T(0, 8.25, 0), () => {+ F.lathe([[0.5, -0.02], [0.54, 0.06], [0.42, 0.12], [0.3, 0.6], [0.02, 2.0]], 6, { faceted: true, phase: PI / 6 });+ for (let i = 0; i < 3; i++) F.at(M.of(M.Ry(-60 + i * 60 + 180), M.T(0, 0, 0.44)), () => addGable(F, 0.62, 0.62, -0.03, 0.06, 0.02, { size: 0.15, crockets: 1, finial: 0.2, solid: true, lod: 0 }));+ });+ });+ return set;+}+// The hemicycle's pier: the nave pier's family, slimmer, with one vault shaft running up to the springing+function buildApsePier() {+ const set = mkBatches(), { F } = set, core = 0.5, big = 0.15, small = 0.09, shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.06) * Math.cos(a), (core + 0.06) * Math.sin(a), big]); }+ for (let i = 0; i < 4; i++) { const a = PI / 4 + i * PI / 2; shafts.push([(core + 0.01) * Math.cos(a), (core + 0.01) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 32), yB = 0.86, yC = P.pierTop - 0.82;+ F.lathe([[1.02, 0], [1.02, 0.46], [0.94, 0.52]], 8, { faceted: true, phase: PI / 8 });+ addExtrudedRing(F, ring, 0.52, yB, { k0: 1.26, k1: 1.0 });+ addExtrudedRing(F, ring, yB, yC, {});+ addCapital(F, ring, yC, 0.82, 1.22, { leaves: 10 });+ F.at(M.T(0, 0, -(core + 0.06) + 0.24), () => {+ const r = big, yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, yS - 0.5], [r * 1.5, yS - 0.42], [r * 2.1, yS]], 8);+ });+ return set;+}+// Flying buttress unit at a bay boundary: massive pier, two arched flyers, tabernacle and pinnacles+const flyerRise = (yA, yB, t) => yA + (yB - yA) * lerp(t, Math.sin(t * PI / 2), 0.6);+function flyerShape(zA, yA, zB, yB, thick) { // A at the pier (outer), B at the wall (inner); returns polygon points in (z,y)+ const N = 12, under = [], top = [];+ for (let i = 0; i <= N; i++) {+ const t = i / N, z = lerp(zA, zB, t), y = flyerRise(yA, yB, t);+ under.push([z, y]);+ }+ // The coping is a straight line clear above the arch: deep over the pier, close over the wall, the lights open between+ const topA = [zA - 0.05, yA + 2.5], topB = [zB, yB + 0.72];+ return { under, topA, topB };+}+function buildButtress() {+ const set = mkBatches(), { A, F, G } = set;+ const hw = 0.88, z0 = zaw + 0.9, zB = 19.2;+ const tiers = [[0, 10, zB], [10, 17.5, zB - 0.5], [17.5, 23.4, zB - 0.9]];+ tiers.forEach((t, i) => {+ const w = hw - i * 0.08;+ A.box(-w, w, t[0], t[1], z0 - 0.05, t[2], { noBot: true });+ if (i < 2) {+ // sloped weathering set-off to the next stage+ const zt = tiers[i + 1][2], w2 = hw - (i + 1) * 0.08;+ A.face([[-w, t[1], t[2]], [w, t[1], t[2]], [w2, t[1] + 1.2, zt], [-w2, t[1] + 1.2, zt]], 1.0);+ F.at(M.of(M.T(0, 0, t[2]), M.Rx(0)), () => F.sweep([[-w - 0.04, t[1] - 0.14], [w + 0.04, t[1] - 0.14]], stringSection(0.26, 0.2), {}));+ }+ });+ // Moulded plinth around the base+ F.at(M.T(0, 0, zB), () => { F.sweep([[-hw - 0.2, 0.75], [hw + 0.2, 0.75]], stringSection(0.5, 0.42), {}); F.box(-hw - 0.3, hw + 0.3, 0, 0.52, -0.02, 0.32, { s: 0.75 }); });+ for (const s of [-1, 1]) F.at(M.of(M.T(s * hw, 0, 0), M.Ry(s * 90)), () => {+ const a = s > 0 ? -zB - 0.2 : z0, c = s > 0 ? -z0 : zB + 0.2;+ F.sweep([[a, 0.75], [c, 0.75]], stringSection(0.5, 0.42), {}); F.box(a, c, 0, 0.52, -0.02, 0.32, { s: 0.75 });+ });+ set.D.at(M.T(0, 0, zB), () => { addStain(set.D, -hw, hw, 9.84, 0.02, 3.2); addStain(set.D, -hw, hw, 5, 0.34, 4.4, 'ground'); });+ // Blind tracery on the front of the lower stages+ F.at(M.T(0, 0, zB), () => {+ addBlindArcade(F, -hw + 0.1, hw - 0.1, 2.0, 6.6, 1, { bar: 0.12, depth: 0.14 });+ addGable(F, hw * 1.7, 1.4, 0.02, 0.14, 8.0, { size: 0.3, crockets: 2, finial: 0.5, solid: true, lod: 0 });+ });+ F.at(M.T(0, 0, zB - 0.5), () => { addBlindArcade(F, -hw + 0.16, hw - 0.16, 11.6, 14.6, 1, { bar: 0.12, depth: 0.14 }); });+ // The rear of the pier rises higher to take the flyers, ending in a sloped weathering+ const zStack = 16.25, yStack = 30.6;+ A.box(-hw + 0.14, hw - 0.14, 23.3, yStack, z0 - 0.05, zStack, { noBot: true, noTop: true });+ A.face([[-hw + 0.14, yStack, zStack], [hw - 0.14, yStack, zStack], [hw - 0.14, yStack + 1.1, z0 - 0.05], [-hw + 0.14, yStack + 1.1, z0 - 0.05]]);+ F.at(M.T(0, 0, zStack), () => F.sweep([[-hw + 0.1, yStack - 0.1], [hw - 0.1, yStack - 0.1]], stringSection(0.24, 0.2), {}));+ // Tabernacle of colonnettes at the top of the pier holding an angel+ const zT = 17.3, yT0 = 23.4, yT1 = 28.4;+ F.box(-hw, hw, yT0 - 0.02, yT0 + 0.1, zStack - 0.02, zB - 0.9 + 0.02, { s: 0.95 });+ for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) F.at(M.T(sx * 0.72, 0, zT + sz * 0.72), () => F.lathe([[0.2, yT0], [0.14, yT0 + 0.12], [0.1, yT0 + 0.2], [0.1, yT1 - 0.22], [0.14, yT1 - 0.15], [0.22, yT1]], 8));+ // Canopy: arched, gabled and crowned by the crocketed pinnacle+ for (let i = 0; i < 4; i++) F.at(M.of(M.T(0, 0, zT), M.Ry(i * 90), M.T(0, 0, 0.72)), () => {+ F.sweep(pointedArc(1.3, 1.25, 6).map((p) => [p[0], yT1 - 1.25 + p[1] - 0.02]), traceryBar(0.1, 0.16), {});+ addGable(F, 1.7, 1.25, -0.06, 0.12, yT1, { size: 0.28, crockets: 2, finial: 0.48, lod: 0, solid: true });+ });+ F.box(-0.92, 0.92, yT1 - 0.06, yT1 + 0.18, zT - 0.92, zT + 0.92, { s: 0.9 });+ F.at(M.T(0, yT1 + 0.18, zT), () => addPinnacle(F, 1.42, 8.4, { lod: 1 }));+ // Two flyers, each a true arch: a moulded rib springing from the pier, a thin plate pierced by a row of+ // pointed lights standing on it, and a straight sloped coping with a gabled top shedding the rain+ const fly = (zA, yA, zB, yB, th) => {+ const { under, topA, topB } = flyerShape(zA, yA, zB, yB, th);+ const band = 0.34, upper = under.map((p) => [p[0], p[1] + band]);+ const onTop = (z) => lerp(topA[1], topB[1], (z - topA[0]) / (topB[0] - topA[0]));+ const onRib = (t) => flyerRise(yA, yB, t) + band;+ const holes = [], N = 6;+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.16) / N, t1 = (i + 0.84) / N, z0 = lerp(zA, zB, t0), z1 = lerp(zA, zB, t1), u0 = onRib(t0) + 0.04, u1 = onRib(t1) + 0.04, a0 = onTop(z0) - 0.1, a1 = onTop(z1) - 0.1, h = Math.min(a0 - u0, a1 - u1);+ if (h < 0.22) continue;+ const r = Math.min(h * 0.42, Math.abs(z1 - z0) * 0.8), hole = [[z0, u0], [z1, u1], [z1, a1 - r], [(z0 + z1) / 2, (a0 + a1) / 2], [z0, a0 - r]];+ holes.push(area2(hole) > 0 ? hole : hole.slice().reverse());+ }+ F.at(M.Ry(-90), () => {+ F.slab([...upper, topB, topA], holes, -0.12, 0.12, { sr: 0.7, so: 0.8 });+ F.slab([...under, ...upper.slice().reverse()], [], -0.27, 0.27, {});+ F.sweep(under, rollSection(0.12, 0.34, 1, 0.03, 2), { caps: false });+ });+ const ye = -0.03, yr = 0.17, we = 0.33, a = [topA[1], topA[0]], c = [topB[1], topB[0]];+ F.face([[-we, a[0] + ye, a[1]], [0, a[0] + yr, a[1]], [0, c[0] + yr, c[1]], [-we, c[0] + ye, c[1]]]);+ F.face([[0, a[0] + yr, a[1]], [we, a[0] + ye, a[1]], [we, c[0] + ye, c[1]], [0, c[0] + yr, c[1]]]);+ F.face([[-we, c[0] + ye, c[1]], [we, c[0] + ye, c[1]], [we, a[0] + ye, a[1]], [-we, a[0] + ye, a[1]]]);+ const n = Math.round(Math.hypot(topA[0] - topB[0], topA[1] - topB[1]) / 1.4);+ crocketsAlong(F, [0, topA[1] + 0.14, topA[0]], [0, topB[1] + 0.14, topB[0]], n, 0.5, [0, 1, 0], 0);+ };+ fly(zStack - 0.1, 28.0, zn + 1.12, 35.6, 0.48);+ fly(zStack - 0.1, 22.0, zn + 1.12, 28.6, 0.54);+ // Upper pier on the clerestory wall where the flyers land, with its own pinnacle above the parapet+ const yE = P.eaves;+ A.box(-0.62, 0.62, P.aisleRoofHigh - 1, yE + 1.2, zn + 0.42, zn + 1.14, { s: 0.98, noBot: true });+ A.face([[-0.62, yE + 1.2, zn + 1.14], [0.62, yE + 1.2, zn + 1.14], [0.62, yE + 1.95, zn + 0.42], [-0.62, yE + 1.95, zn + 0.42]]);+ F.at(M.T(0, yE + 1.2, zn + 0.74), () => addPinnacle(F, 0.9, 6.4, { simple: 1 }));+ F.at(M.T(0, 0, zn + 1.14), () => { F.lathe([[0.16, 31.2], [0.1, 31.4], [0.1, 40.5], [0.17, 40.8]], 8); });+ // Interior respond on the aisle wall: a small cluster carrying the aisle vault+ const ringR = lobedRing(0.32, [[0, -0.44, 0.17], [-0.34, -0.26, 0.11], [0.34, -0.26, 0.11]], 26);+ F.at(M.T(0, 0, zaw), () => {+ addExtrudedRing(F, ringR, 0.4, P.pierTop - 0.8, { s0: 0.8 });+ addCapital(F, ringR, P.pierTop - 0.8, 0.8, 1.2, { leaves: 6 });+ F.lathe([[0.62, 0], [0.62, 0.42], [0.42, 0.5]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ });+ // Spout from the aisle gutter+ return set;+}++function addLancetTracery(b, w, y0, ys, rise) { // single-light frame with a cusped head+ const secT = traceryBar(0.13, 0.3), cr = w * 0.32, cy = ys + rise * 0.34, cusp = [];+ for (let i = 0; i <= 12; i++) { const a = rad(-30 + i * 240 / 12), R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, {});+ b.sweep([[w / 2, cy - cr * 0.4], cusp[0]], secT, {});+ b.sweep([[-w / 2, cy - cr * 0.4], cusp[12]], secT, {});+}+function makeLancetGlass(w, y0, ys, rise, seed, fig, o) {+ const g = new Glass(seed);+ g.raw(archOutline(w, y0, ys, rise, 8), 'lead', -0.4, 0);+ gLancetSaint(g, 0, w, y0, ys, rise, fig, o || {});+ return g.b;+}++// ==================================================================================+// PLAN: everything is derived from the parameters at the top of the file.+// ==================================================================================+const LAY = (() => {+ const xW = P.xWest, xTowerE = xW - P.towerDepth, xNaveE = xTowerE - P.naveBays * BAY, xc = xNaveE - zn, xChoirW = xc - zn, xa = xChoirW - P.choirBays * BAY;+ const ridge = P.eaves + (zn + 1) * P.roofPitch;+ return { xW, xTowerE, xNaveE, xc, xChoirW, xa, ridge };+})();+// A rigid module frame: rotation about Y at an origin on the ground+const frame = (th, ox, oz) => {+ const c = Math.cos(rad(th)), s = Math.sin(rad(th));+ return { th, ox, oz, pos: (x, y, z) => [ox + x * c + z * s, y, oz - x * s + z * c], rot: (d = 0) => [0, th + d, 0] };+};+const SAINTS = ['apostle', 'peter', 'paul', 'bishop', 'prophet', 'martyr', 'paul', 'apostle', 'bishop', 'prophet'];++// Baked floor light: dim towards the walls and the corners++function buildVaultNave() {+ const s = mkBatches();+ const crown = addRibVault(s.F, s.A, [[-BAY / 2, -zn + 0.42], [BAY / 2, -zn + 0.42], [BAY / 2, zn - 0.42], [-BAY / 2, zn - 0.42]], P.vaultSpring, P.vaultCrown, { sideRise: (k) => (P.vaultCrown - P.vaultSpring) * (k % 2 === 0 ? 0.86 : 0.99), nS: 10, nT: 8 });+ addBoss(s.G, s.F, crown, 0.55);+ return s;+}++// ---- Chevet: hemicycle, ambulatory and a radiating chapel per sector ----------------+const CHEV = (() => {+ const d = 180 / P.apseSides, h = rad(d / 2), Ra = zn * Math.cos(h), hs = zn * Math.sin(h), Ro = zaw * Math.cos(h), hso = zaw * Math.sin(h);+ const Rc = 2.5, zc = Ro + 3.0;+ return { d, h, Ra, hs, Ro, hso, Rc, zc };+})();+function buildChevetSector() {+ const set = mkBatches(), { A, F, L, G } = set, { Ra, hs, Ro, hso, Rc, zc } = CHEV;+ buildNaveBay({ into: set, part: 'upper', B: 2 * hs, zw: Ra, ww: 1.62, archRise: 5.0, nT: 2, tw: 1.05, cw: 2.05, cRise: 4.3, lights: 1 });+ // Hemicycle vault: one web from this side up to the crown over the apse centre+ const yS = P.vaultSpring, yC = P.vaultCrown;+ addVaultCell(A, [-hs + 0.0, Ra - 0.42], [hs, Ra - 0.42], [0, 0], yS, yC, 9.6, 7, 8);+ const sec = rollSection(0.32, 0.42, 2, 0.04, 2);+ addRib(F, [-hs, Ra - 0.42], [hs, Ra - 0.42], yS, 9.6, sec, 6);+ addRib(F, [-zn * Math.sin(CHEV.h), (zn - 0.42) * Math.cos(CHEV.h)], [0, 0], yS, yC - yS, sec, 9, true);+ // Ambulatory vault (trapezoid)+ const c2 = addRibVault(F, A, [[-hs, Ra + 0.42], [hs, Ra + 0.42], [hso, Ro], [-hso, Ro]], P.pierTop, P.aisleCrown - 0.3, { sideRise: (k) => (k === 0 ? 5.1 : k === 2 ? 5.2 : 5.1), nS: 6, nT: 4, ribSec: rollSection(0.24, 0.32, 2, 0.03, 2) });+ addBoss(G, F, c2, 0.3);+ // Outer wall with the arched chapel mouth+ const yW = P.aisleWallTop, mw = 3.55, mS = 8.6, mR = 4.8;+ const mouth = pointedArc(mw, mR, 8).map((p) => [p[0], p[1] + mS]);+ A.at(M.T(0, 0, Ro + 0.3), () => A.slab([[-hso, 0], [-mw / 2, 0], [-mw / 2, mS], ...mouth, [mw / 2, mS], [mw / 2, 0], [hso, 0], [hso, yW], [-hso, yW]], [], -0.3, 0.3, { noOuterSides: true, sr: 0.7 }));+ // The moulding fills the whole wall thickness: it is also the soffit of the opening+ F.at(M.T(0, 0, Ro + 0.3), () => F.sweep(mouth, rollSection(0.42, 0.96, 2, 0.05, 2), {}));+ F.at(M.T(0, 0, Ro + 0.6), () => { F.sweep([[-hso, yW - 0.12], [hso, yW - 0.12]], stringSection(0.3, 0.3), {}); addParapet(F, -hso, hso, yW, 1.0, 0.24, { pitch: 1.1 }); });+ A.face([[-hso, yW, Ro + 0.6], [hso, yW, Ro + 0.6], [hso, yW, Ro], [-hso, yW, Ro]], 0.7);+ // Ambulatory lean-to roof+ const zR0 = Ra + 0.42, hsR0 = (zR0 / Ra) * hs, zR1 = Ro + 0.9, hsR1 = (zR1 / Ro) * hso;+ L.face([[-hsR0, P.aisleRoofHigh, zR0], [-hsR1, yW - 0.2, zR1], [hsR1, yW - 0.2, zR1], [hsR0, P.aisleRoofHigh, zR0]]);+ // Hipped roof over the hemicycle+ const yE = P.eaves + 0.15, Re = zn + 0.72, hRe = Re * Math.sin(CHEV.h), aRe = Re * Math.cos(CHEV.h);+ L.grid((u, v) => [lerp(-hRe, hRe, u) * (1 - v), lerp(yE, LAY.ridge, v), aRe * (1 - v)], 2, 2, { flip: false });+ // Radiating chapel: five faceted walls, each with a tall lancet, rib vault, conical lead roof+ const nF = P.chapelFacets, span = 216, yC1 = 16.2, dF = span / nF, ap = Rc * Math.cos(rad(dF / 2)), halfF = Rc * Math.sin(rad(dF / 2)), lw = 1.3, sill = 3.2, lSp = 9.9, lR = 2.5;+ const verts = [];+ for (let i = 0; i <= nF; i++) { const f = rad(-span / 2 + i * dF); verts.push([Rc * Math.sin(f), zc + Rc * Math.cos(f)]); }+ for (let i = 0; i < nF; i++) {+ const fm = -span / 2 + (i + 0.5) * dF;+ const m = M.of(M.T(0, 0, zc), M.Ry(fm), M.T(0, 0, ap));+ A.at(m, () => A.slab([[-halfF, 0], [halfF, 0], [halfF, yC1], [-halfF, yC1]], [archOutline(lw, sill, lSp, lR, 7)], 0, 0.5, { noOuterSides: true, sr: 0.68 }));+ F.at(m, () => {+ addLancetTracery(F, lw, sill, lSp, lR);+ F.sweep(archOutline(lw, sill, lSp, lR, 5), hexBar(0.12, 0.56), { closed: true, z: 0.25, s: 0.9, crease: 40 });+ });+ F.at(M.of(m, M.T(0, 0, 0.5)), () => {+ F.sweep([[-halfF - 0.1, sill - 0.14], [halfF + 0.1, sill - 0.14]], stringSection(0.24, 0.2), { caps: false });+ F.sweep([[-halfF - 0.1, 0.5], [halfF + 0.1, 0.5]], stringSection(0.36, 0.3), { caps: false });+ F.sweep([[-halfF - 0.1, yC1 - 0.1], [halfF + 0.1, yC1 - 0.1]], stringSection(0.3, 0.3), { caps: false });+ addGable(F, lw + 0.9, 2.4, 0.01, 0.14, lSp + 2.15, { size: 0.32, crockets: 2, finial: 0.56, lod: 0 });+ addParapet(F, -halfF, halfF, yC1, 0.82, 0.18, { pitch: 1.3, nh: 8 });+ });+ addVaultCell(A, verts[i], verts[i + 1], [0, zc], 9.2, 14.6, 3.6, 5, 4);+ addRib(F, verts[i], verts[i + 1], 9.2, 3.6, rollSection(0.16, 0.22, 1, 0.02, 2), 5);+ }+ addVaultCell(A, verts[nF], verts[0], [0, zc], 9.2, 14.6, 4.2, 5, 4);+ for (let i = 0; i <= nF; i++) {+ addRib(F, verts[i], [0, zc], 9.2, 5.4, rollSection(0.17, 0.24, 1, 0.02, 2), 6, true);+ // Exterior angle buttress and little pinnacle; interior wall shaft+ const f = -span / 2 + i * dF, ve = verts[i];+ F.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => {+ A.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => A.box(-0.24, 0.24, 0, 14.2, 0.26, 1.02, { s: 0.98, noBot: true, taperZ: 0.3 }));+ F.at(M.T(0, 14.18, 0.62), () => addPinnacle(F, 0.62, 3.9, { simple: 1 }));+ F.lathe([[0.17, 0], [0.09, 0.6], [0.09, 9.1], [0.17, 9.2]], 5);+ });+ }+ // The neck joining the chapel to the ambulatory: side walls, a pointed barrel vault, a lead cap+ const zN0 = Ro + 0.6, zN1 = verts[0][1], hN0 = mw / 2, hN1 = Math.abs(verts[0][0]);+ for (const s of [-1, 1]) A.prism([[s * hN0, zN0 - 0.05], [s * hN1, zN1 + 0.05], [s * (hN1 + 0.5), zN1 + 0.05], [s * (hN0 + 0.5), zN0 - 0.05]], 0, yC1);+ // Same subdivision as the chapel's last web so the seam has no cracks; a rib covers the joint+ A.grid((u, v) => { const hw = lerp(hN0, hN1, v); return [lerp(-hw, hw, u), 9.2 + arcH(2 * hw, 4.2, u), lerp(Ro + 0.3, zN1 + 0.02, v), 0.8]; }, 5, 1, { flip: false }); // starts under the arch moulding, so no ray can pass above it+ addRib(F, verts[nF], verts[0], 9.2, 4.2, rollSection(0.16, 0.26, 1, 0.02, 2), 5);+ L.face([[-(hN0 + 0.5), yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN0 - 0.1], [-(hN0 + 0.5), yC1 + 0.03, zN0 - 0.1]]);+ // Lead cone+ L.at(M.T(0, 0, zc), () => { const n = 10, rb = Rc + 0.5; L.grid((u, v) => { const a = -PI + TAU * u, r = rb * (1 - v), y = lerp(yC1 + 0.05, yC1 + 7.4, v); return [r * Math.sin(a), y, r * Math.cos(a)]; }, n, 2, { flip: false, wrapU: true }); });+ G.at(M.T(0, yC1 + 7.3, zc), () => addFinial(G, 1.25, 1));+ return set;+}++// ---- Lead roofs with metric UVs so the seams run down the slope ----------------------+function addRoofRun(L, x0, x1, half, yE, yR) {+ const slope = Math.hypot(half, yR - yE);+ for (const s of [-1, 1]) {+ const a = L.v(x0, yE, s * half, 0, half, s * (yR - yE), x0, 0, 1), b = L.v(x1, yE, s * half, 0, half, s * (yR - yE), x1, 0, 1);+ const c = L.v(x1, yR, 0, 0, half, s * (yR - yE), x1, slope, 1), d = L.v(x0, yR, 0, 0, half, s * (yR - yE), x0, slope, 1);+ if (s > 0) L.quad(a, b, c, d); else L.quad(b, a, d, c);+ }+}+function buildCresting(len) { // gilded fleur-de-lis cresting for a length of ridge+ const G = new Batch(TILE.gold), n = Math.round(len / 0.9);+ G.box(-len / 2, len / 2, -0.04, 0.05, -0.05, 0.05, {});+ for (let i = 0; i < n; i++) {+ const x = -len / 2 + (i + 0.5) * len / n;+ G.at(M.T(x, 0.05, 0), () => {+ G.tube([[0, 0, 0], [0, 0.34, 0], [0, 0.62, 0]], (t) => [lerp(0.05, 0.005, t), lerp(0.035, 0.005, t)], 4);+ for (const s of [-1, 1]) G.tube(spline([[0, 0.14, 0], [s * 0.15, 0.26, 0], [s * 0.2, 0.42, 0], [s * 0.1, 0.46, 0]], 1), (t) => lerp(0.035, 0.008, t), 4);+ G.box(-0.08, 0.08, 0.12, 0.17, -0.03, 0.03, {});+ });+ }+ return G;+}++// ---- Crossing: four great piers, four arches, a gilded boss, the fleche above ----------+function buildCrossingPier() {+ const set = mkBatches(), { F } = set;+ const core = 1.08, sh = [];+ for (let i = 0; i < 8; i++) { const a = i * PI / 4; sh.push([(core + 0.12) * Math.cos(a), (core + 0.12) * Math.sin(a), i % 2 ? 0.2 : 0.3]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; sh.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), 0.13]); }+ const ring = lobedRing(core, sh, 80), yC = P.vaultSpring - 1.1;+ F.lathe([[2.05, 0], [2.05, 0.6], [1.9, 0.68]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.68, 0.94, { k0: 1.2, k1: 1.23 });+ addExtrudedRing(F, ring, 0.94, 1.25, { k0: 1.23, k1: 1, s0: 0.75, s1: 0.75 });+ addExtrudedRing(F, ring, 1.25, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.1, 1.14, { leaves: 16 });+ return set;+}+function buildAislePier() {+ const set = mkBatches(), { F } = set;+ const sh = []; for (let i = 0; i < 4; i++) { const a = i * PI / 2; sh.push([0.62 * Math.cos(a), 0.62 * Math.sin(a), 0.2]); }+ const ring = lobedRing(0.54, sh, 40);+ F.lathe([[1.15, 0], [1.15, 0.5], [1.02, 0.56]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.56, 0.86, { k0: 1.28, k1: 1, s0: 0.75 });+ addExtrudedRing(F, ring, 0.86, P.pierTop - 0.9, { s0: 0.8 });+ addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });+ return set;+}+function buildFleche(parent, x, z) {+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold), F = new Batch(TILE.fine);+ const y0 = LAY.ridge - 2.5, yB = LAY.ridge + 7, yT = P.flecheTip - 3.2, R = 2.9, n = 8;+ // Octagonal base and open arcaded lantern+ L.lathe([[R + 0.4, y0], [R + 0.25, y0 + 2.8], [R + 0.5, y0 + 3], [R + 0.5, y0 + 3.3], [R, y0 + 3.5]], n, { faceted: true, phase: PI / 8 });+ L.lathe([[R * 0.78, y0 + 3.5], [R * 0.78, yB + 0.3]], n, { faceted: true, phase: PI / 8, s: 0.34 });+ for (let i = 0; i < n; i++) {+ const a = i * 45, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ L.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap)), () => {+ for (const s of [-1, 1]) L.tube([[s * side * 0.5, y0 + 3.5, 0], [s * side * 0.5, yB, 0]], 0.13, 6, {});+ const arc = pointedArc(side * 0.92, 2.2, 6).map((p) => [p[0], yB - 2.45 + p[1]]);+ L.sweep(arc, traceryBar(0.13, 0.2), {});+ G.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap + 0.1)), () => addGable(G, side * 1.04, 1.6, -0.05, 0.08, yB - 0.24, { size: 0.26, crockets: 2, finial: 0.44, lod: 0, solid: true }));+ });+ }+ L.lathe([[R + 0.2, yB - 0.05], [R + 0.34, yB + 0.1], [R + 0.2, yB + 0.26]], n, { faceted: true, phase: PI / 8 });+ // The spire, its ribs and gilded crockets+ L.lathe([[R * 0.98, yB + 0.26], [R * 0.72, yB + 3.2], [0.1, yT]], n, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < n; i++) {+ const a = rad(i * 45 + 22.5 + 22.5), c = Math.cos(a), s = Math.sin(a);+ const pts = [[R * 0.98 * c * 1.0, yB + 0.3, R * 0.98 * s], [R * 0.72 * c, yB + 3.2, R * 0.72 * s], [0.14 * c, yT - 0.2, 0.14 * s]];+ G.tube(pts, (t) => lerp(0.1, 0.03, t), 5);+ crocketsAlong(G, pts[1], pts[2], 7, 0.66, [c, 0.1, s], 0);+ }+ G.at(M.T(0, yT - 0.15, 0), () => {+ G.lathe([[0.14, 0], [0.3, 0.28], [0.34, 0.5], [0.22, 0.72], [0.06, 0.9], [0.05, 3.2]], 10);+ G.box(-0.06, 0.06, 2.2, 3.35, -0.06, 0.06, {}); G.box(-0.6, 0.6, 2.56, 2.72, -0.06, 0.06, {});+ });+ place('Fleche_Lead', L, MAT.lead, { pos: [x, 0, z], parent });+ place('Fleche_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });+}++// Coloured light thrown onto the paving by the south aisle windows (sun from the south-west)+function buildLightPools(xs) {+ // A single soft image of the two-light window, thrown through the south aisle windows and sheared across the floor+ const b = new Batch(1); b.noColor = true;+ const sill = WIN.aSill, top = WIN.aSp + WIN.aRise, w = WIN.aW, P0 = (uu, vv) => [xw_ + uu + vv * P.sunShear[0], 0.035, -zaw + vv * P.sunShear[1]];+ let xw_ = 0;+ xs.forEach((xw, i) => {+ xw_ = xw;+ const f = i % 2 ? 1 : 0, u0 = f, u1 = 1 - f;+ const ids = [[-w / 2, sill, u0, 1], [w / 2, sill, u1, 1], [w / 2, top, u1, 0], [-w / 2, top, u0, 0]].map((q) => { const p = P0(q[0], q[1]); return b.v(p[0], p[1], p[2], 0, 1, 0, q[2], q[3], 1); });+ b.quad(ids[0], ids[3], ids[2], ids[1]);+ });+ return b;+}++function storyWindows() { // the narrative aisle windows+ const sets = [+ [['annunciation', 'nativity', 'magi', 'shepherd', 'supper'], ['crucifixion', 'resurrection', 'david', 'noah', 'annunciation']],+ [['noah', 'david', 'annunciation', 'nativity', 'magi'], ['shepherd', 'supper', 'crucifixion', 'resurrection', 'david']],+ [['magi', 'supper', 'noah', 'crucifixion', 'shepherd'], ['nativity', 'david', 'resurrection', 'annunciation', 'supper']],+ [['resurrection', 'shepherd', 'crucifixion', 'david', 'nativity'], ['supper', 'annunciation', 'magi', 'noah', 'resurrection']],+ ];+ const grounds = [['ruby', 'cobalt'], ['cobalt', 'ruby'], ['emerald', 'ruby'], ['violet', 'cobalt']];+ return sets.slice(0, P.storyWindows).map((s, n) => makeWindowGlass(...windowArgs.aisle, 40 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetStory(g, x, w, y0, ys, r, s[i].slice(0, 4), { c1: grounds[n][0], c2: grounds[n][1], g1: grounds[(n + 1) % 4][1], g2: grounds[n][0], cell: 0.86 }),+ oculus: { ground: grounds[n][1], inner: grounds[n][0] },+ }));+}+const CLERE_SAINTS = [+ [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'book' }, { robe: 'emerald', mantle: 'gold', crown: 1, beard: 2, attr: 'cross', arm: 'point', halo: false }],+ [{ robe: 'pale', mantle: 'cobalt', veil: 'azure', arm: 'pray' }, { robe: 'violet', mantle: 'white', mitre: 'white', attr: 'staff', arm: 'up' }],+ [{ robe: 'gold', mantle: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'purple', beard: 1, attr: 'sword', arm: 'point' }],+ [{ robe: 'rubyLt', mantle: 'teal', crown: 1, attr: 'lily', arm: 'point', halo: false }, { robe: 'white', mantle: 'rubyLt', arm: 'orant', wings: 'gold' }],+];+function clereWindows() {+ return CLERE_SAINTS.slice(0, P.clerestoryWindows).map((pair, n) => makeWindowGlass(...windowArgs.clere, 60 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetSaint(g, x, w, y0, ys, r, pair[i], (i + n) % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }),+ oculus: { bust: { robe: 'rubyLt', beard: 1, cross: 1 }, ground: 'cobalt', inner: 'gold' },+ }));+}++function buildBody() {+ const { xTowerE, xNaveE, xc, xChoirW, xa, ridge } = LAY;+ const gNave = group('Nave'), gChoir = group('Choir'), gTN = group('Transept_North'), gTS = group('Transept_South'), gX = group('Crossing'), gApse = group('Apse_Chevet'), gRoof = group('Roofs');+ // The upper elevation is shared by every straight bay; aisles come in full and open-corner versions+ const bayUp = buildNaveBay({ part: 'upper' }), aisleFull = buildNaveBay({ part: 'aisle' }), aisleOpen = buildNaveBay({ part: 'aisle', open: true });+ const vault = buildVaultNave(), pier = buildPier(), but = buildButtress();+ const stories = storyWindows(), cleres = clereWindows();+ SHARED.stories = stories; SHARED.cleres = cleres;+ let counter = 0;+ const statue = (fr, lx, y, lz, kind, faceDeg, scale, parent, indoor = true) => placeFigure('Statue_' + kind + '_' + (counter++), kind, { pos: fr.pos(lx, y, lz), rot: fr.rot(faceDeg), scale, parent, indoor, lod: 0 });+ const garg = (fr, lx, y, lz, k, sc, parent) => place('Gargoyle_' + (counter++), gargoyle(k), MAT.fine, { pos: fr.pos(lx, y, lz), rot: fr.rot(-90), scale: [sc, sc, sc], parent });++ // One side of a straight bay: module, vault-side glass, statues, gargoyles+ const placeBay = (name, fr, parent, o = {}) => {+ placeSet(name + '_Upper', bayUp, [fr.ox, 0, fr.oz], fr.th, parent);+ if (!o.noVault) placeSet(name + '_Aisle', o.open ? aisleOpen : aisleFull, [fr.ox, 0, fr.oz], fr.th, parent);+ const c = cleres[(o.i || 0) % cleres.length];+ placeGlass(name + '_Clerestory', c, fr.pos(0, 0, zn), fr.th + 180, parent);+ if (!o.open) {+ placeGlass(name + '_AisleWindow', stories[(o.i || 0) % stories.length], fr.pos(0, 0, zaw + 0.45), fr.th + 180, parent);+ if ((o.i || 0) % 2 === 0) statue(fr, 0, 0.7, zaw - 0.3, SAINTS[(o.i || 0) % SAINTS.length], 90, 0.86, parent);+ if (!o.inTower) garg(fr, 0, 22.5, zaw + 1.05, (o.i || 0) % 3, 0.72, parent);+ }+ if (!o.noUpperGarg) garg(fr, 0, P.eaves - 0.2, zn + 0.62, ((o.i || 0) + 1) % 3, 0.66, parent);+ };+ const placeButtress = (name, fr, parent, i) => {+ placeSet(name, but, [fr.ox, 0, fr.oz], fr.th, parent);+ statue(fr, 0, 23.5, 17.3, 'angel', -90, 1.9, parent, false);+ garg(fr, 0, 16.8, 18.55, i % 3, 1.0, parent);+ };+ const placePier = (name, lfr, parent, kind) => {+ placeSet(name, pier, [lfr.ox, 0, lfr.oz], lfr.th, parent);+ if (kind) statue(lfr, 0, 5.78, -1.14, kind, 90, 1.15, parent);+ };++ // ---- Two more bays carry the nave between the towers to the inner face of the west wall ----+ for (let t = 0; t < 2; t++) {+ const x = xTowerE + (t + 0.5) * BAY;+ placeBay('TowerBay' + t + '_N', frame(0, x, 0), gNave, { i: t + 2, noUpperGarg: true, inTower: true });+ placeBay('TowerBay' + t + '_S', frame(180, x, 0), gNave, { i: t, noUpperGarg: true, inTower: true });+ placeSet('TowerVault' + t, vault, [x, 0, 0], 0, gNave);+ if (t === 0) { placePier('TowerPier_N', frame(0, x + BAY / 2, zn), gNave, SAINTS[6]); placePier('TowerPier_S', frame(180, x + BAY / 2, -zn), gNave, SAINTS[7]); }+ }+ // ---- Nave ----+ for (let i = 0; i < P.naveBays; i++) {+ const x = xTowerE - (i + 0.5) * BAY, isOpen = i === P.naveBays - 1;+ placeBay('NaveBay' + i + '_N', frame(0, x, 0), gNave, { i, open: isOpen });+ placeBay('NaveBay' + i + '_S', frame(180, x, 0), gNave, { i: i + 2, open: isOpen });+ placeSet('NaveVault' + i, vault, [x, 0, 0], 0, gNave);+ }+ for (let k = 0; k <= P.naveBays - 1; k++) {+ const x = xTowerE - k * BAY;+ placePier('NavePier' + k + '_N', frame(0, x, zn), gNave, k ? SAINTS[k % SAINTS.length] : null);+ placePier('NavePier' + k + '_S', frame(180, x, -zn), gNave, k ? SAINTS[(k + 3) % SAINTS.length] : null);+ if (k >= 1 && k < P.naveBays - 1) { placeButtress('NaveButtress' + k + '_N', frame(0, x, 0), gNave, k); placeButtress('NaveButtress' + k + '_S', frame(180, x, 0), gNave, k + 1); }+ }+ // ---- Choir ----+ for (let j = 0; j < P.choirBays; j++) {+ const x = xChoirW - (j + 0.5) * BAY, isOpen = j === 0;+ placeBay('ChoirBay' + j + '_N', frame(0, x, 0), gChoir, { i: j + 1, open: isOpen });+ placeBay('ChoirBay' + j + '_S', frame(180, x, 0), gChoir, { i: j + 3, open: isOpen });+ placeSet('ChoirVault' + j, vault, [x, 0, 0], 0, gChoir);+ }+ for (let k = 1; k <= P.choirBays; k++) {+ const x = xChoirW - k * BAY;+ placePier('ChoirPier' + k + '_N', frame(0, x, zn), gChoir, SAINTS[(k + 5) % SAINTS.length]);+ placePier('ChoirPier' + k + '_S', frame(180, x, -zn), gChoir, SAINTS[(k + 1) % SAINTS.length]);+ placeButtress('ChoirButtress' + k + '_N', frame(0, x, 0), gChoir, k);+ placeButtress('ChoirButtress' + k + '_S', frame(180, x, 0), gChoir, k + 1);+ }+ // ---- Transept arms: the same bay, turned through 90 degrees ----+ for (const s of [1, -1]) {+ const gT = s > 0 ? gTN : gTS, tag = s > 0 ? 'N' : 'S';+ for (let q = 0; q < P.transeptBays + 1; q++) {+ const zc = s * (zn + (q + 0.5) * BAY), isOpen = q === 0;+ placeBay('Transept' + tag + q + '_W', frame(90 * s, xc, zc), gT, { i: q + 1, open: isOpen, noVault: isOpen });+ placeBay('Transept' + tag + q + '_E', frame(-90 * s, xc, zc), gT, { i: q + 3, open: isOpen, noVault: isOpen });+ placeSet('TranseptVault' + tag + q, vault, [xc, 0, zc], 90, gT);+ }+ for (let q = 1; q <= P.transeptBays; q++) { // the last bay ends in the front wall, which needs no free pier+ const zz = s * (zn + q * BAY);+ placePier('TranseptPier' + tag + q + '_W', frame(90, xc + zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 2) % SAINTS.length] : null);+ placePier('TranseptPier' + tag + q + '_E', frame(-90, xc - zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 4) % SAINTS.length] : null);+ if (q === P.transeptBays) { placeButtress('TranseptButtress' + tag + '_W', frame(90 * s, xc, zz), gT, q); placeButtress('TranseptButtress' + tag + '_E', frame(-90 * s, xc, zz), gT, q + 1); }+ }+ }+ // ---- Crossing ----+ const xPier = buildCrossingPier(), aPier = buildAislePier(), xVault = mkBatches();+ const sp = zn - 0.3;+ const crossCrown = addRibVault(xVault.F, xVault.A, [[-sp, -sp], [sp, -sp], [sp, sp], [-sp, sp]], P.vaultSpring, P.vaultCrown + 0.5, { sideRise: () => 12.9, nS: 12, nT: 10 });+ addBoss(xVault.G, xVault.F, crossCrown, 0.95);+ // Great transverse arches and the walls above them+ const arch = pointedArc(2 * (zn - 0.5), 12.9, 7);+ for (let i = 0; i < 4; i++) xVault.F.at(M.of(M.Ry(i * 90), M.T(0, P.vaultSpring, zn - 0.05)), () => xVault.F.sweep(arch, rollSection(0.9, 1.2, 2, 0.09, 3), { caps: false }));+ placeSet('CrossingVault', xVault, [xc, 0, 0], 0, gX);+ for (const [a, b] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {+ placeSet('CrossingPier', xPier, [xc + a * zn, 0, b * zn], 0, gX);+ placeSet('AngleAislePier', aPier, [xc + a * zaw, 0, b * zaw], 0, gX);+ }+ // ---- Chevet ----+ const sector = buildChevetSector(), apsePier = buildApsePier();+ const apseSaints = [{ robe: 'gold', mantle: 'cobalt', crown: 1, veil: 'pale', arm: 'pray' }, { robe: 'white', mantle: 'ruby', beard: 2, attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'emerald', beard: 1, attr: 'book' }, { robe: 'white', mantle: 'rubyLt', beard: 1, cross: 1, arm: 'up' }];+ const hemi = apseSaints.slice(0, 3).map((f, i) => makeLancetGlass(2.05, P.triforiumTop + 0.45, P.vaultSpring + 4.8, 4.3, 70 + i, f, i % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }));+ const chapelGlass = ['annunciation', 'crucifixion'].map((nm, i) => { const g = new Glass(80 + i); g.raw(archOutline(1.3, 3.2, 9.9, 2.5, 7), 'lead', -0.4, 0); gLancetStory(g, 0, 1.3, 3.2, 9.9, 2.5, [nm, ['supper', 'magi'][i], ['magi', 'resurrection'][i]], { cell: 0.6, c1: i ? 'cobalt' : 'ruby', c2: i ? 'ruby' : 'cobalt' }); return g.b; });+ for (let k = 0; k < P.apseSides; k++) {+ const th = -(k + 0.5) * CHEV.d, fr = frame(th, xa, 0), gS = group('ChevetSector' + k, gApse);+ placeSet('Sector' + k, sector, [xa, 0, 0], th, gS);+ placeGlass('HemicycleWindow' + k, hemi[(k + (k > 3 ? 1 : 0)) % hemi.length], fr.pos(0, 0, CHEV.Ra), th + 180, gS);+ const nF = P.chapelFacets, dF = 216 / nF, ap = CHEV.Rc * Math.cos(rad(dF / 2));+ for (let i = 0; i < nF; i++) {+ const fm = -108 + (i + 0.5) * dF, lx = ap * Math.sin(rad(fm)), lz = CHEV.zc + ap * Math.cos(rad(fm));+ placeGlass('ChapelWindow' + k + '_' + i, chapelGlass[(i + k) % chapelGlass.length], fr.pos(lx, 0, lz + 0.0), th + fm + 180, gS);+ }+ statue(fr, 0, 0.5, CHEV.zc + CHEV.Rc * 0.55, k === 3 ? 'virgin' : SAINTS[k], 90, 0.92, gS);+ garg(frame(th - 10, xa, 0), 0, 15.5, CHEV.zc + 1.2, k % 3, 0.7, gS);+ if (k >= 1) {+ const bt = -k * CHEV.d;+ placeButtress('ChevetButtress' + k, frame(bt, xa, 0), gS, k);+ // A slim clustered pier at each angle of the hemicycle: the arches now spring from something+ placeSet('ApsePier' + k, apsePier, frame(bt, xa, 0).pos(0, 0, zn), bt, gS);+ }+ }+ // ---- Roofs, ridge cresting, floor ----+ const Lr = new Batch(TILE.lead);+ const yE = P.eaves + 0.15, half = zn + 0.72;+ addRoofRun(Lr, xa, LAY.xW - 1.2, half, yE, ridge);+ Lr.at(M.T(xc, 0, 0), () => Lr.at(M.Ry(90), () => addRoofRun(Lr, -(zn + (P.transeptBays + 1) * BAY + 0.4), zn + (P.transeptBays + 1) * BAY + 0.4, half, yE, ridge)));+ place('MainRoof_Lead', Lr, MAT.lead, { parent: gRoof });+ const crest = buildCresting(BAY);+ const nCrest = Math.floor((LAY.xW - 2 - xa) / BAY);+ for (let i = 0; i < nCrest; i++) place('RidgeCresting' + i, crest, MAT.gold, { pos: [xa + (i + 0.5) * BAY, ridge, 0], parent: gRoof });+ for (const s of [1, -1]) for (let q = 0; q < P.transeptBays + 2; q++) { const zz = s * (0.5 + (q + 0.5) * BAY + 3); if (Math.abs(zz) < zn + (P.transeptBays + 1) * BAY) place('TranseptCresting' + (s > 0 ? 'N' : 'S') + q, crest, MAT.gold, { pos: [xc, ridge, zz], rot: [0, 90, 0], parent: gRoof }); }+ buildFleche(gRoof, xc, 0);+ const fl = new Batch(TILE.pave);+ const R13 = zaw + 0.1;+ fl.face([[xa, 0, R13], [LAY.xW - WF.wallT + 0.1, 0, R13], [LAY.xW - WF.wallT + 0.1, 0, -R13], [xa, 0, -R13]]);+ const tz = zn + (P.transeptBays + 1) * BAY + 1;+ fl.face([[xc - R13, 0, tz], [xc + R13, 0, tz], [xc + R13, 0, -tz], [xc - R13, 0, -tz]]);+ for (let k = 0; k < P.apseSides; k++) { const th = rad(-(k + 0.5) * CHEV.d), f = frame(-(k + 0.5) * CHEV.d, xa, 0), rr = CHEV.zc + CHEV.Rc + 0.2, hh = rr * Math.tan(CHEV.h); fl.face([f.pos(0, 0, 0), f.pos(-hh, 0, rr), f.pos(hh, 0, rr)]); }+ place('Paving', fl, MAT.pave, { parent: ROOT });+ // ---- Stained light on the floor from the south aisle ----+ const xs = []; for (let i = 0; i < P.naveBays - 1; i++) xs.push(xTowerE - (i + 0.5) * BAY);+ place('StainedLight_Floor', buildLightPools(xs), MAT.pool, { parent: gNave });+}++// ==================================================================================+// THE GREAT ROSE: real stone tracery, and glass depicting Christ among apostles and angels+// ==================================================================================+const ROSE = { r0: 1.15, rs1: 2.35, r1: 3.05, rs2: 4.05, r2: 4.72, n1: 12, n2: 24 };+const pol = (r, a) => [r * Math.cos(a), r * Math.sin(a)];+function addRoseTracery(F, R) {+ const k = R / 5.5, heavy = traceryBar(0.3 * k, 0.5), mid = hexBar(0.2 * k, 0.42), thin = hexBar(0.14 * k, 0.34);+ const dia = [[0.07 * k, 0], [0, 0.17], [-0.07 * k, 0], [0, -0.17]]; // lightest bar, for the little roundels+ const { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE;+ F.sweep(ringPts(0, 0, R, 36), heavy, { closed: true });+ F.sweep(ringPts(0, 0, r0 * k, 14), mid, { closed: true });+ F.sweep(ringPts(0, 0, r2 * k, 36), mid, { closed: true });+ F.sweep(ringPts(0, 0, r1 * k, 24), mid, { closed: true });+ // Inner wheel: colonnette spokes, each light closed by a pointed trefoil head+ const head = (n, rs, re, a, bar) => {+ const da = PI / n * 0.98, pts = [], N = 6;+ for (let i = 0; i <= N; i++) { const t = -1 + 2 * i / N, h = Math.pow(1 - Math.pow(Math.abs(t), 1.5), 0.72); pts.push(pol((rs + (re - rs) * h) * k, a + t * da)); }+ F.sweep(pts, bar, { caps: false });+ return pts;+ };+ for (let i = 0; i < n1; i++) {+ const a = i * TAU / n1, am = a + PI / n1;+ F.sweep([pol(r0 * k, a), pol(rs1 * k, a)], mid, { caps: false });+ head(n1, rs1, r1 * 0.985, am, thin);+ // the cusp hanging inside the head+ const c = pol((rs1 + 0.34) * k, am); F.sweep(ringPts(c[0], c[1], 0.26 * k, 6), dia, { closed: true, s: 0.95, crease: 80 });+ }+ // Outer wheel of twenty-four lights+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, am = a + PI / n2;+ F.sweep([pol(r1 * k, a), pol(rs2 * k, a)], thin, { caps: false });+ head(n2, rs2, r2 * 0.99, am, thin);+ // Quatrefoil roundels in the outer band+ const c = pol((R / k + r2) / 2 * k, a);+ F.sweep(ringPts(c[0], c[1], 0.3 * k, 6), dia, { closed: true, crease: 80 });+ }+}+const SHARED = {};+// Rose tracery is one mesh, scaled for the smaller transept roses; bright outside, dim inside+function roseTracery() {+ if (!SHARED.rose) { const b = new Batch(TILE.fine); addRoseTracery(b, WF.roseR); SHARED.rose = b; }+ return SHARED.rose;+}+function largePinnacle() {+ if (!SHARED.pin) { const b = new Batch(TILE.fine); addPinnacle(b, 2, 10, { lod: 1 }); SHARED.pin = b; }+ return SHARED.pin;+}+function makeRoseGlass(R, seed, o = {}) {+ const g = new Glass(seed), k = R / 5.5, { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE, inner = o.inner || ['ruby', 'cobalt'], outer = o.outer || ['cobalt', 'ruby', 'emerald'];+ g.raw(ringPts(0, 0, R + 0.08, 48), 'lead', -0.4, 0);+ // A light is never one flat sheet: lay the painted mosaic across it, clipped to its shape+ const ground = (pts, col, layer) => { let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of pts) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); } g.withClip(pts, () => gDiaper(g, x0, y0, x1, y1, 0, col, null, null, layer)); };+ // The band of roundels: jewels in a ring, deep colours set off by small sparks of gold+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, b = (i + 1) * TAU / n2;+ ground([pol(r2 * k, a), pol(R, a), pol(R, b), pol(r2 * k, b)], i % 2 ? 'ruby' : 'cobalt', 0);+ const c = pol((R / k + r2) / 2 * k, a); g.disc(c[0], c[1], 0.27 * k, i % 2 ? 'emerald' : 'ruby', 0.6, 10, { came: 0.016 }); g.disc(c[0], c[1], 0.12 * k, 'gold', 1, 8, { came: 0.01 });+ }+ // Outer lights: angels and elders turning about the centre+ for (let i = 0; i < n2; i++) {+ const am = (i + 0.5) * TAU / n2, da = PI / n2 * 0.97, col = outer[i % outer.length];+ const wedge = [pol(r1 * k, am - da), pol(r2 * k, am - da * 0.9), pol(r2 * k, am + da * 0.9), pol(r1 * k, am + da)];+ ground(wedge, col, 0);+ g.withClip(wedge, () => g.withXf(am * 180 / PI - 90, ((r1 + 0.15) * k) * Math.cos(am), ((r1 + 0.15) * k) * Math.sin(am), k, () => {+ const f = i % 3;+ if (f === 0) gFigure(g, 0, 0, 1.28, { wings: 'gold', robe: 'white', mantle: 'rubyLt', arm: 'orant', hair: 'amber' });+ else if (f === 1) gBust(g, 0, 0.1, 1.2, { robe: 'emerald', beard: 1, crown: 1, halo: 'gold' });+ else gFigure(g, 0, 0, 1.26, { robe: 'gold', mantle: 'violet', beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'book' });+ }));+ }+ // Inner lights: the twelve apostles enthroned+ const scheme = o.figs || [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'key', arm: 'point' }, { robe: 'emerald', mantle: 'white', beard: 2, attr: 'book' }, { robe: 'gold', mantle: 'cobalt', beard: 1, attr: 'sword', arm: 'point' }, { robe: 'white', mantle: 'violet', attr: 'book' }];+ for (let i = 0; i < n1; i++) {+ const am = (i + 0.5) * TAU / n1, da = PI / n1 * 0.97, col = inner[i % inner.length];+ const wedge = [pol(r0 * k, am - da), pol(r1 * k, am - da * 0.92), pol(r1 * k, am + da * 0.92), pol(r0 * k, am + da)];+ ground(wedge, col, 0);+ g.withClip(wedge, () => {+ // each apostle in his own roundel: a thin jewelled rim round a deep field+ const mid = pol((r0 + r1) / 2 * k, am);+ gRim(g, mid[0], mid[1], 0.78 * k, 0.86 * k, ['gold', 'ruby', 'gold', 'cobalt'], 14, 0.8);+ g.disc(mid[0], mid[1], 0.78 * k, i % 2 ? 'cobalt' : 'ruby', 1, 14, { came: 0 });+ g.withXf(am * 180 / PI - 90, (r0 + 0.2) * k * Math.cos(am), (r0 + 0.2) * k * Math.sin(am), k, () => gFigure(g, 0, 0, 1.56, Object.assign({ pose: 'sit' }, scheme[i % scheme.length])));+ });+ }+ // The centre: Christ in Majesty in a gilded mandorla, the four-square cross behind+ g.disc(0, 0, r0 * k + 0.06, 'lead', 2.6, 16, { came: 0, jitter: 0 });+ ground(ringPts(0, 0, r0 * k, 16), o.centre || 'cobalt', 3);+ g.withClip(ringPts(0, 0, r0 * k, 14), () => g.withXf(0, 0, -0.98 * k, k, () => {+ const m = []; for (let i = 0; i < 16; i++) { const t = i * TAU / 16; m.push([0.62 * Math.cos(t) * Math.pow(Math.abs(Math.sin(t)) * 0.3 + 0.7, 1), 1 + 0.96 * Math.sin(t)]); }+ g.poly(m, 'gold', 3.3, { came: 0.02 });+ g.poly(m.map((p) => [p[0] * 0.8, 1 + (p[1] - 1) * 0.88]), 'rubyLt', 3.5, { came: 0.012 });+ gFigure(g, 0, 0.22, 1.62, o.majesty || { pose: 'sit', robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'up', attr: null, layer: 4 });+ }));+ return g.b;+}++// ==================================================================================+// WEST FRONT. Local frame: x across the facade, y up, +z out towards the viewer, z=0 the wall.+// ==================================================================================+const WF = { halfW: 21.5, topY: 47, roseY: 31.8, roseR: 5.5, kingY: 20.2, kingTop: 25.3, galY: 40.6, galTop: 46.2, wallT: 0.8, portalC: { x: 0, w0: 4.3, orders: P.jambStatues, dx: 0.58, dz: 0.62, ys: 7.5 }, portalS: { x: 13.5, w0: 3.7, orders: P.jambStatues - 1, dx: 0.6, dz: 0.66, ys: 7.2 } };+// Aged oak door leaf with forged straps, studs and a ring. Hung at x=0, spans to x=w, h high, front +z.+function addDoorLeaf(set, x0, w, h, flip) {+ const { O, I } = set, planks = 6, pw = w / planks, d = flip ? -1 : 1;+ for (let i = 0; i < planks; i++) {+ const a = x0 + d * i * pw, c = x0 + d * (i + 0.97) * pw, xa = Math.min(a, c), xc = Math.max(a, c), tone = 0.46 + 0.16 * Math.abs(Math.sin(i * 2.4 + x0 * 3));+ O.at(M.Ry(0), () => O.box(xa, xc, 0.02, h, -0.05, 0.05, { s: tone }));+ }+ // Oak grain runs up each plank: remap planes with u across and v up is the default box projection+ const strapY = [0.72, h * 0.36, h * 0.64, h - 0.7];+ for (const y of strapY) {+ const reach = w * 0.94, xEnd = x0 + d * reach;+ I.shade(0.34, () => {+ I.box(Math.min(x0, xEnd), Math.max(x0, xEnd), y - 0.075, y + 0.075, 0.05, 0.085, {});+ // scrolled fleur at the strap end+ for (const s of [-1, 1]) I.tube(spline([[xEnd, y, 0.07], [xEnd + d * 0.12, y + s * 0.12, 0.07], [xEnd + d * 0.05, y + s * 0.24, 0.07], [xEnd - d * 0.07, y + s * 0.17, 0.07]], 2), (t) => lerp(0.03, 0.012, t), 4, {});+ for (let k = 0; k < 4; k++) { const sx = x0 + d * (k + 0.5) * reach / 4; I.box(sx - 0.03, sx + 0.03, y - 0.03, y + 0.03, 0.08, 0.115, { taper: 0.012 }); }+ });+ }+ // Ring handle+ I.shade(0.4, () => I.at(M.of(M.T(x0 + d * (w - 0.3), h * 0.42, 0.11), M.Ry(0)), () => {+ const rg = []; for (let i = 0; i <= 12; i++) { const t = i * TAU / 12; rg.push([0.1 * Math.cos(t), 0.1 * Math.sin(t) - 0.1, 0]); }+ I.tube(rg, 0.018, 5, { capEnd: false }); I.box(-0.05, 0.05, -0.05, 0.05, -0.05, 0, {});+ }));+}+function buildPortal(cfg, kinds, tymp) {+ const set = mkBatches(), { A, F, G, O, I } = set, { w0, orders: N, dx, dz, ys } = cfg;+ // Every order is concentric: the same two arc centres with the radius growing by one step+ const r0 = w0 * 0.86, c0 = (r0 * r0 - w0 * w0 / 4) / w0, wT = (i) => w0 + 2 * i * dx, zT = (i) => i * dz;+ const riseI = (i) => Math.sqrt(Math.pow(c0 + w0 / 2 + i * dx, 2) - c0 * c0);+ const riseOf = (w) => riseI((w - w0) / (2 * dx));+ const figs = [], zF = N * dz, outerW = wT(N), apex = ys + riseOf(outerW), lintelH = 1.2, doorH = ys - lintelH;+ const statueY = 1.5, statueK = 1.22, canopyY = statueY + 2.15 * statueK + 0.54; // the tallest head clears its canopy+ // ---- Solid stepped jambs, each order with a dado of carved blind arches ----+ for (let i = 0; i < N; i++) {+ for (const s of [-1, 1]) {+ const xa = s * wT(i) / 2, xb = s * wT(i + 1) / 2, x0 = Math.min(xa, xb), x1 = Math.max(xa, xb), zf = zT(i + 1);+ A.box(x0, x1, 0, ys, -0.1, zf, { noBot: true });+ // The dado: a plinth, a blind trefoil head on two colonnettes, a moulded table for the statues above+ F.box(x0 - 0.01, x1 + 0.01, 0, 0.26, zf, zf + 0.05, {});+ F.at(M.T((x0 + x1) / 2, 0, zf), () => {+ const w = x1 - x0 - 0.12, y0 = 0.26, ya = 0.86, r = 0.32;+ F.sweep(pointedArc(w, r, 4).map((p) => [p[0], p[1] + ya]), hexBar(0.05, 0.09), { caps: false, z: 0.03 });+ for (const t of [-1, 1]) F.box(t * w / 2 - 0.03, t * w / 2 + 0.03, y0, ya, 0, 0.06, {});+ F.sweep([[-(x1 - x0) / 2, 1.3], [(x1 - x0) / 2, 1.3]], stringSection(0.16, 0.1), { caps: false });+ });+ // Colonnette on the arris, with a foliate capital; the saint stands against it+ const cx = s * (wT(i) / 2 + 0.02), cz = zT(i) + 0.32;+ F.at(M.T(cx, 0, cz), () => {+ F.lathe([[0.2, 1.3], [0.2, 1.42], [0.11, 1.54], [0.11, ys - 0.5], [0.14, ys - 0.46], [0.11, ys - 0.42], [0.26, ys - 0.04], [0.3, ys]], 8);+ for (let l = 0; l < 4; l++) addLeaf(F, M.of(M.T(0, ys - 0.42, 0), M.Ry(l * 90 + 45), M.T(0.1, 0, 0)), 0.38, { coarse: 1 });+ });+ // Carved base under the feet, canopy with room above the head+ const sx = s * (wT(i) / 2 - 0.13), sz = zT(i) + 0.56;+ F.at(M.T(sx, 0, sz), () => {+ F.lathe([[0.3, 1.25], [0.34, 1.32], [0.24, 1.38], [0.3, 1.44], [0.33, statueY]], 8);+ for (let l = 0; l < 2; l++) addLeaf(F, M.of(M.T(0, 1.36, 0), M.Ry(l * 90 + (s > 0 ? -60 : 150)), M.T(0.24, 0, 0)), 0.2, { coarse: 1 });+ F.at(M.T(0, canopyY + 0.28, 0), () => {+ // A canopy like a tiny carved building: vaulted soffit, a drum of gablets, thin spirelet+ F.lathe([[0.05, -0.14], [0.27, -0.06], [0.33, 0], [0.33, 0.1], [0.28, 0.14], [0.17, 0.17], [0.17, 0.5], [0.22, 0.54], [0.15, 0.58], [0.07, 0.64], [0.01, 1.02]], 6, { phase: PI / 6, faceted: true });+ const face = s > 0 ? -60 : 60;+ for (const l of [-1, 0, 1]) F.at(M.of(M.Ry(face + l * 60), M.T(0, 0, 0.27)), () => {+ F.face([[-0.16, 0.11, 0.02], [0.16, 0.11, 0.02], [0, 0.42, 0.02]]);+ crocketsAlong(F, [-0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [-1, 0.6, 0], 0);+ crocketsAlong(F, [0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [1, 0.6, 0], 0);+ F.shade(0.5, () => F.face([[-0.08, 0.14, 0.03], [0.08, 0.14, 0.03], [0, 0.28, 0.03]]));+ });+ });+ });+ const dirX = -s * dz, dirZ = dx * 1.5, l = Math.hypot(dirX, dirZ), th = Math.atan2(-dirZ / l, dirX / l) * 180 / PI;+ figs.push({ kind: kinds[(i + (s > 0 ? 0 : kinds.length >> 1)) % kinds.length], pos: [cfg.x + sx, statueY, sz], rot: [0, th, 0], scale: statueK, mirror: s < 0 });+ }+ }+ // ---- Archivolts: each order a hollow, each hollow a procession of small figures, every one on a console under a canopy ----+ const ARCH_KINDS = ['apostle', 'king', 'angel', 'bishop', 'queen'], ak = 0.62;+ for (let i = 0; i < N; i++) {+ const w = wT(i), r = riseI(i), arc = pointedArc(w, r, 8).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, [[0, -0.1], [dx, -0.1], [dx, dz], [0, 0]], { z: zT(i), caps: false, crease: 30 });+ F.at(M.T(0, 0, zT(i + 1) - 0.02), () => F.sweep(pointedArc(wT(i + 1), riseI(i + 1), 8).map((p) => [p[0], p[1] + ys]), hexBar(0.2, 0.22), { caps: false }));+ const pts = pointedArc(w + dx, riseI(i + 0.5), 16).map((p) => [p[0], p[1] + ys]);+ let len = 0; const seg = []; for (let k = 0; k < pts.length - 1; k++) { const l = Math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]); seg.push(l); len += l; }+ const n = Math.max(4, Math.round(len / 1.52));+ for (let q = 0; q < n; q++) {+ const d = (q + 0.5) / n * len; let acc = 0, k = 0; while (k < seg.length - 1 && acc + seg[k] < d) { acc += seg[k]; k++; }+ const f = (d - acc) / seg[k], px = lerp(pts[k][0], pts[k + 1][0], f), py = lerp(pts[k][1], pts[k + 1][1], f);+ const tx0 = (pts[k + 1][0] - pts[k][0]) / seg[k], ty0 = (pts[k + 1][1] - pts[k][1]) / seg[k], rx = -ty0, ry = tx0;+ const up = d > len / 2 ? -1 : 1, Y = [tx0 * up, ty0 * up, 0];+ // sits on the splay, looking out towards the opening and the viewer+ const nl = Math.hypot(dz, dx), X = [-rx * dz / nl, -ry * dz / nl, dx / nl];+ const Z = [X[1] * Y[2] - X[2] * Y[1], X[2] * Y[0] - X[0] * Y[2], X[0] * Y[1] - X[1] * Y[0]];+ const pos = [px + X[0] * 0.17 - Y[0] * 0.62, py + X[1] * 0.17 - Y[1] * 0.62, zT(i) + dz * 0.5 + X[2] * 0.17];+ figs.push({ kind: ARCH_KINDS[(i + q) % ARCH_KINDS.length], lod: 0, pos: [cfg.x + pos[0], pos[1], pos[2]], basis: { X, Y, Z }, scale: ak });+ F.at(frameOf(pos, X, Y), () => {+ F.lathe([[0.03, -0.24], [0.2, -0.02], [0.22, 0.02]], 5);+ F.lathe([[0.02, 1.25], [0.2, 1.33], [0.2, 1.4], [0.02, 1.62]], 5, { faceted: true });+ });+ }+ }+ // ---- Outer hood with gable and crockets ----+ const hood = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, zF), () => F.sweep(hood, rollSection(0.34, 0.44, 2, 0.05, 2), { caps: false }));+ // The gable springs from the arch springing: plain coursed stone, its carving is real and stands proud of it+ const gw = outerW + 1.5, gy = ys, gTop = WF.kingY - 0.7, gH = gTop - gy;+ const archCut = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ const field = [[-gw / 2, gy], [-(outerW + 0.1) / 2, gy], ...archCut.slice(1, -1), [(outerW + 0.1) / 2, gy], [gw / 2, gy], [0, gTop]];+ A.at(M.T(0, 0, zF - 0.1), () => A.slab(field, [], -0.1, 0, { noOuterSides: true, noBack: true }));+ A.at(M.T(0, 0, zF - 0.2), () => A.slab(field, [], -zF + 0.3, 0, { noOuterSides: false, noFront: true, noBack: true, so: 0.7 }));+ F.at(M.T(0, 0, zF - 0.14), () => {+ addGable(F, gw, gH, 0, 0.24, gy, { size: 0.72, finial: 0.1, crockets: 6, noField: true, lod: 1 });+ const yc = apex + (gTop - apex) * 0.36, rc = Math.min(0.92, (gTop - yc) * 0.42);+ F.sweep(ringPts(0, yc, rc, 16), traceryBar(0.18, 0.24), { closed: true, z: 0.04 });+ const foil = []; for (let i = 0; i < 15; i++) { const a = PI / 2 + i * TAU / 15, R = rc * (0.56 + 0.34 * Math.pow(Math.abs(Math.cos(a * 1.5 - PI * 0.75)), 0.8)); foil.push([R * Math.cos(a), yc + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.09, 0.18), { closed: true, z: 0.04, crease: 40 });+ });+ G.at(M.T(0, gTop + 0.1, zF - 0.02), () => addFinial(G, 1.5, 1));+ // ---- Tympanum: plain stone behind a carved scene ----+ const tw = w0 + 0.04, tr = riseOf(w0), k = w0 / 4.3, floorZ = 0.42;+ F.at(M.T(0, 0, 0.0), () => F.slab(archOutline(tw, ys - 0.05, ys, tr, 9), [], -0.1, 0, { noOuterSides: true, noBack: true }));+ // A shelf for the scene to stand on, thrown forward so the people have depth to overlap in+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, ys - 0.02, ys + 0.1, -0.08, floorZ + 0.04, {});+ F.at(M.T(0, 0, floorZ + 0.04), () => F.sweep([[-tw / 2 - 0.02, ys + 0.03], [tw / 2 + 0.02, ys + 0.03]], stringSection(0.14, 0.1), { caps: false }));+ // Lintel: a recessed frieze, deep enough to hold its people, cornice and sill standing forward of it+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, ys - 0.02, -0.08, -0.02, {});+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, doorH + 0.12, -0.08, 0.42, {});+ F.at(M.T(0, 0, 0.42), () => F.sweep([[-tw / 2 - 0.02, doorH + 0.06], [tw / 2 + 0.02, doorH + 0.06]], stringSection(0.12, 0.06), { caps: false }));+ const nL = Math.round(w0 / 0.37), frieze = ['apostle', 'queen', 'apostle', 'king', 'bishop', 'apostle'];+ for (let i = 0; i < nL; i++) {+ // two ranks, every other figure a step forward: they overlap and each one turns to the middle of the door+ const x = -w0 / 2 + (i + 0.5) * w0 / nL, side = x < 0 ? 1 : -1, deep = i % 2;+ figs.push({ kind: frieze[i % frieze.length], lod: 0, pos: [cfg.x + x, doorH + 0.12, deep ? 0.1 : 0.27], rot: [0, -90 + side * (16 + 10 * (i % 3)), 0], scale: 0.43 });+ }+ // The central figure enthroned in a gilded vesica+ const mY = ys + 0.1, mH = tr * 0.84, mW = tw * 0.17, mand = [], nM = 8;+ // A true vesica: two arcs meeting in a point above and below+ for (let i = 0; i < nM; i++) { const v = i / nM; mand.push([mW * (1 - Math.pow(2 * v - 1, 2)), mY + mH * v]); }+ for (let i = 0; i < nM; i++) { const v = 1 - i / nM; mand.push([-mW * (1 - Math.pow(2 * v - 1, 2)), mY + mH * v]); }+ G.at(M.T(0, 0, 0.14), () => G.sweep(mand, hexBar(0.05, 0.06), { closed: true, crease: 50 }));+ figs.push({ kind: tymp.main, half: 1, pos: [cfg.x, mY, 0.1], rot: [0, -90, 0], scale: 1.08 * k });+ // Worshippers on their knees and angels crowding behind them, every head turned to the throne+ for (const s of [-1, 1]) {+ figs.push({ kind: 'kneel', half: 1, pos: [cfg.x + s * 1.02 * k, mY, 0.24], rot: [0, -90 + s * 55, 0], scale: 0.86 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[0], half: 1, pos: [cfg.x + s * 1.46 * k, mY, 0.03], rot: [0, -90 + s * 38, 0], scale: 0.74 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[1], half: 1, lod: 0, pos: [cfg.x + s * 1.86 * k, mY, 0.2], rot: [0, -90 + s * 60, 0], scale: 0.46 * k, mirror: s < 0 });+ // and two flying in from the point of the arch, arms towards the head of the throne+ const fy = mY + mH * 0.6, ca = Math.cos(rad(58)), sa = Math.sin(rad(58));+ figs.push({ kind: 'angel', half: 1, lod: 0, pos: [cfg.x + s * 1.0 * k, fy, 0.12], basis: { X: [0, 0, 1], Y: [-s * sa, ca, 0], Z: [-ca, -s * sa, 0] }, scale: 0.42 * k });+ }+ // Trumeau with its statue, on a carved base, under a canopy+ A.box(-0.32, 0.32, 0, doorH, -0.1, 0.34, { noBot: true });+ const tK = tymp.trumeauScale || 1.3, tBase = 1.06, tCanopy = tBase + 2.15 * tK + 0.46;+ F.at(M.T(0, 0, 0.5), () => {+ F.lathe([[0.42, 0], [0.42, 0.42], [0.3, 0.56], [0.25, 0.78], [0.34, 0.92], [0.36, tBase]], 8, { faceted: true, phase: PI / 8 });+ for (let l = 0; l < 6; l++) addLeaf(F, M.of(M.T(0, 0.82, 0), M.Ry(l * 60 + 15), M.T(0.27, 0, 0)), 0.24, { coarse: 1 });+ F.at(M.T(0, tCanopy + 0.2, 0), () => F.lathe([[0.06, -0.2], [0.33, -0.08], [0.42, 0], [0.42, 0.14], [0.35, 0.18], [0.22, 0.22], [0.22, 0.58], [0.28, 0.62], [0.2, 0.66], [0.09, 0.72], [0.02, 1.1]], 6, { phase: PI / 6, faceted: true }));+ });+ figs.push({ kind: tymp.trumeau, pos: [cfg.x, tBase, 0.5], rot: [0, -90, 0], scale: tK });+ // Doors: two leaves either side of the trumeau, recessed in the wall+ const leaf = w0 / 2 - 0.34;+ O.at(M.T(0, 0, -0.14), () => { const set2 = set; I.at(M.T(0, 0, -0.14), () => { addDoorLeaf(set2, w0 / 2 - 0.02, leaf, doorH - 0.04, true); addDoorLeaf(set2, -w0 / 2 + 0.02, leaf, doorH - 0.04, false); }); });+ return { set, figs, apex, zF, outerW };+}+function placePortal(gWest, name, cfg, pr, opt = {}) {+ placeSet(name, pr.set, [cfg.x, 0, 0], 0, gWest);+ const gF = group(name + '_Figures', gWest); // the people of one door, as one group of nodes+ pr.figs.forEach((f, i) => {+ const rot = f.rot || eulerFromBasis(f.basis.X, f.basis.Y, f.basis.Z);+ const pos = [f.pos[0] + (cfg.x - pr.baseX), f.pos[1], f.pos[2]];+ // Full sculpture where people stand next to it; opt.light keeps a door's overhead scene, or the whole door, to the lighter copies+ const far = opt.light === 'all' || (opt.light === 'scene' && f.pos[1] > 4);+ placeFigure(name + '_Fig' + i, f.kind, { pos, rot, scale: f.scale, parent: gF, lod: far ? 0 : f.lod, mirror: f.mirror, half: f.half });+ });+}+function eulerFromBasis(X, Y, Z) { // degrees, three.js 'XYZ' order+ const m13 = clamp(Z[0], -1, 1), y = Math.asin(m13);+ let x, z;+ if (Math.abs(m13) < 0.9999) { x = Math.atan2(-Z[1], Z[2]); z = Math.atan2(-Y[0], X[0]); } else { x = Math.atan2(Y[2], Y[1]); z = 0; }+ return [x * 180 / PI, y * 180 / PI, z * 180 / PI];+}+// One niche of the Gallery of Kings, shared along the facade+function buildKingNiche(pitch) {+ const set = mkBatches(), { F, A } = set, h = WF.kingTop - WF.kingY, y0 = WF.kingY;+ A.face([[-pitch / 2, y0, 0.02], [pitch / 2, y0, 0.02], [pitch / 2, y0 + h, 0.02], [-pitch / 2, y0 + h, 0.02]]);+ F.box(-pitch / 2, pitch / 2, y0 - 0.5, y0, 0, 1.3, { s: 0.9 });+ F.at(M.T(0, 0, 1.3), () => F.sweep([[-pitch / 2, y0 - 0.38], [pitch / 2, y0 - 0.38]], stringSection(0.3, 0.26), { caps: false }));+ // Colonnettes and a cusped arch with a gablet over each king+ const ys = y0 + 3.15, rise = 1.3;+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0.95), () => F.lathe([[0.17, y0], [0.09, y0 + 0.16], [0.09, ys - 0.2], [0.2, ys]], 8));+ F.at(M.T(0, 0, 0.95), () => {+ const arc = pointedArc(pitch - 0.1, rise, 6).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, rollSection(0.14, 0.36, 1, 0.03, 2), { caps: false });+ // solid spandrel+ A.at(M.T(0, 0, 0.95), () => A.slab([[-pitch / 2, ys], [-(pitch - 0.1) / 2, ys], ...arc, [(pitch - 0.1) / 2, ys], [pitch / 2, ys], [pitch / 2, y0 + h], [-pitch / 2, y0 + h]], [], -0.1, 0.1, { noOuterSides: true }));+ addGable(F, pitch * 0.96, 1.12, 0.1, 0.1, ys + 0.46, { size: 0.26, crockets: 2, finial: 0.34, solid: true, lod: 0 });+ });+ // The roof of the gallery behind the arcade+ F.box(-pitch / 2, pitch / 2, y0 + h - 0.02, y0 + h + 0.12, 0, 1.28, { s: 0.85 });+ return set;+}+// One bay of the open arcade above the rose and the tower windows+function buildGalleryBay(pitch) {+ const set = mkBatches(), { F, A } = set, y0 = WF.galY, y1 = WF.galTop, ys = y0 + 3.3;+ F.shade(0.4, () => F.face([[-pitch / 2, y0, 0.03], [pitch / 2, y0, 0.03], [pitch / 2, y1, 0.03], [-pitch / 2, y1, 0.03]]));+ const arc = pointedArc(pitch * 0.92, 1.5, 5).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, 0.5), () => {+ F.sweep(arc, traceryBar(0.16, 0.36), { caps: false });+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0), () => F.lathe([[0.2, y0], [0.1, y0 + 0.2], [0.1, ys - 0.22], [0.22, ys]], 8));+ F.sweep(ringPts(0, ys + 0.6, 0.26, 7), hexBar(0.07, 0.14), { closed: true });+ });+ A.at(M.T(0, 0, 0.5), () => A.slab([[-pitch / 2, ys], [-pitch * 0.46, ys], ...arc, [pitch * 0.46, ys], [pitch / 2, ys], [pitch / 2, y1], [-pitch / 2, y1]], [], -0.18, 0.18, { noOuterSides: true }));+ F.box(-pitch / 2, pitch / 2, y0 - 0.2, y0 + 0.02, 0, 0.72, { s: 0.92 });+ F.at(M.T(0, 0, 0.72), () => { F.sweep([[-pitch / 2, y0 - 0.1], [pitch / 2, y0 - 0.1]], stringSection(0.26, 0.22), { caps: false }); F.sweep([[-pitch / 2, y1], [pitch / 2, y1]], stringSection(0.3, 0.3), { caps: false }); });+ F.at(M.T(0, 0, 0.72), () => addParapet(F, -pitch / 2, pitch / 2, y1 + 0.12, 1.05, 0.2, { pitch: 1.1, nh: 8 }));+ return set;+}+// One face of a belfry stage, shared by all eight faces of the two towers+function buildBelfryFace() {+ const set = mkBatches(), { A, F, L } = set, hw = 7, zf = 7, y0 = WF.topY, yT = P.towerTop - 2.6;+ const lw = 3.05, lx = 3.3, sill = y0 + 4.2, ys = y0 + 13.6, rise = 4.1, holes = [];+ for (const s of [-1, 1]) holes.push(archOutline(lw, sill, ys, rise, 7).map((p) => [s * lx + p[0], p[1]]));+ A.at(M.T(0, 0, zf), () => A.slab([[-hw, y0], [hw, y0], [hw, yT], [-hw, yT]], holes, -0.9, 0, { noOuterSides: true, noBack: true, sr: 0.66 }));+ // The bell chamber: a floor behind the openings, and a bell hanging on each side of the frame+ F.box(-hw + 0.8, hw - 0.8, sill - 0.5, sill - 0.2, zf - 5, zf - 0.8, {});+ set.I.at(M.T(0, sill + 3.2, zf - 2.5), () => set.I.lathe([[0.06, 1.72], [0.36, 1.62], [0.5, 1.12], [0.56, 0.7], [0.84, 0.12], [0.92, 0]], 10));+ // Behind the bells the oak frame that carries them closes the view, so no sky shows through the tower+ set.O.face([[-4.2, sill - 0.3, zf - 4], [4.2, sill - 0.3, zf - 4], [4.2, yT, zf - 4], [-4.2, yT, zf - 4]]);+ for (const s of [-1, 1]) {+ F.at(M.T(s * lx, 0, zf - 0.45), () => {+ F.sweep(pointedArc(lw, rise, 6).map((p) => [p[0], p[1] + ys]), rollSection(0.2, 0.7, 1, 0.04, 2), { caps: false });+ F.sweep([[0, sill - 0.05], [0, ys + 0.1]], traceryBar(0.2, 0.42), {});+ const lw2 = (lw - 0.1) / 2;+ for (const t of [-1, 1]) F.sweep(pointedArc(lw2, lw2 * 0.9, 4).map((p) => [t * lw2 / 2 + p[0], ys + 0.1 + p[1]]), hexBar(0.14, 0.34), { caps: false });+ F.sweep(ringPts(0, ys + rise * 0.62, 0.48, 10), hexBar(0.14, 0.34), { closed: true });+ });+ // Louvres: lead-clad boards set across the opening and tilted to throw the rain outward, dark air between them+ L.at(M.T(s * lx, 0, zf - 0.45), () => {+ const slope = 0.42;+ for (let y = sill + 0.2; y < ys + rise * 0.3; y += 0.5) {+ const h = y <= ys ? lw / 2 : (lw / 2) * Math.sqrt(Math.max(0, 1 - Math.pow((y - ys) / rise, 2)));+ L.face([[-h, y, 0.3], [h, y, 0.3], [h, y + slope, -0.3], [-h, y + slope, -0.3]]);+ L.face([[h, y - 0.05, 0.3], [-h, y - 0.05, 0.3], [-h, y + slope - 0.05, -0.3], [h, y + slope - 0.05, -0.3]]);+ }+ });+ F.at(M.T(s * lx, 0, zf), () => {+ F.sweep(pointedArc(lw + 0.5, rise + 0.3, 6).map((p) => [p[0], p[1] + ys]), hexBar(0.24, 0.28), { caps: false });+ addGable(F, lw + 0.9, 3.1, 0.03, 0.18, ys + rise * 0.5, { size: 0.42, finial: 0.8, crockets: 3, lod: 0 });+ });+ }+ F.at(M.T(0, 0, zf), () => {+ F.sweep([[-hw, y0 + 0.2], [hw, y0 + 0.2]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, sill - 0.3], [hw, sill - 0.3]], stringSection(0.32, 0.28), { caps: false });+ F.sweep([[-hw, yT - 0.2], [hw, yT - 0.2]], stringSection(0.4, 0.44), { caps: false });+ for (let i = 0; i < 12; i++) { const x = -hw + (i + 0.5) * 2 * hw / 12; F.box(x - 0.14, x + 0.14, yT - 0.78, yT - 0.42, 0, 0.3, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zf - 0.2), () => addParapet(F, -hw, hw, yT, 1.2, 0.28, { pitch: 1.4, nh: 8 }));+ return set;+}+// Octagonal corner turret and its great pinnacle, reaching the full height of the tower+function buildTowerCorner() {+ const set = mkBatches(), { A, F } = set, y0 = WF.topY, yT = P.towerTop - 1.3, R = 1.72;+ A.lathe([[R, y0 - 1], [R, yT]], 8, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < 8; i++) {+ const a = i * 45 + 22.5, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ F.at(M.of(M.Ry(a), M.T(0, 0, ap)), () => {+ if (i % 2 === 0) for (const k of [0, 1]) {+ const yb = y0 + 1 + k * 11;+ F.sweep(pointedArc(side * 0.72, 0.9, 4).map((p) => [p[0], p[1] + yb + 8.2]), hexBar(0.1, 0.12), { caps: false });+ for (const s of [-1, 1]) F.sweep([[s * side * 0.36, yb], [s * side * 0.36, yb + 8.2]], hexBar(0.1, 0.12), { caps: false });+ F.shade(0.55, () => F.face([[-side * 0.36, yb, 0.01], [side * 0.36, yb, 0.01], [side * 0.36, yb + 8.2, 0.01], [0, yb + 9.05, 0.01], [-side * 0.36, yb + 8.2, 0.01]]));+ }+ });+ }+ F.lathe([[R, y0 + 12.3], [R + 0.28, y0 + 12.5], [R + 0.28, y0 + 12.66], [R, y0 + 12.82]], 8, { faceted: true, phase: PI / 8 });+ F.lathe([[R, yT - 0.2], [R + 0.36, yT], [R + 0.36, yT + 0.26], [R, yT + 0.42]], 8, { faceted: true, phase: PI / 8 });+ return set;+}+function buildWestFront() {+ const gWest = group('West_Front');+ gWest.position.set(LAY.xW, 0, 0); gWest.rotation.y = rad(90);+ const set = mkBatches(), { A, F, L, G, D } = set, hw = WF.halfW, yTop = WF.topY, tcx = 14.5;+ // Real recessed lights flanking the tower windows, cut through the face of the wall+ const towerRecess = [];+ for (const s of [-1, 1]) for (const d of [-1, 1]) towerRecess.push(archOutline(1.5, WIN.cSill + 0.6, WIN.cSp - 0.6, 1.8, 6).map((p) => [s * tcx + d * 3.55 + p[0], p[1]]));+ // ---- The great wall with real openings: doors and rose ----+ const cP = WF.portalC, sP = WF.portalS;+ const doorHole = (x, w, h) => [[x - w / 2, 0.02], [x + w / 2, 0.02], [x + w / 2, h], [x - w / 2, h]];+ const wallOutline = [[-hw, 0], [-sP.x - sP.w0 / 2, 0], [-sP.x - sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, 0], [-cP.w0 / 2, 0], [-cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, 0], [sP.x - sP.w0 / 2, 0], [sP.x - sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]];+ A.slab(wallOutline, [ringPts(0, WF.roseY, WF.roseR, 40), ...towerRecess], -WF.wallT, 0, { noOuterSides: true, sr: 0.62 });+ towerRecess.forEach((o) => addRecess(A, F, o, 0.5));+ // Seven centuries of rain under every ledge of the front, damp rising from the ground+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.02, 3.2);+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.02, 3.6);+ addStain(D, -hw, hw, WF.galY - 0.42, 0.02, 3.4);+ addStain(D, -hw, hw, WF.kingTop + 0.02, 0.02, 2.6, 'drip');+ // damp on the wall only: the door openings are left clear+ for (const span of [[-hw, -sP.x - sP.w0 / 2], [-sP.x + sP.w0 / 2, -cP.w0 / 2], [cP.w0 / 2, sP.x - sP.w0 / 2], [sP.x + sP.w0 / 2, hw]]) addStain(D, span[0], span[1], 5.2, 0.03, 5.2, 'ground');+ // Each ledge throws a soft shadow on the wall just below it; soot gathers where the rain cannot reach+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.03, 0.8, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.03, 1.2, 'soft');+ addStain(D, -hw, hw, WF.galY - 0.42, 0.03, 1.0, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.62, 0.04, 2.4, 'soot');+ for (const s of [-1, 1]) addStain(D, s * tcx - WIN.cW / 2 - 0.4, s * tcx + WIN.cW / 2 + 0.4, WIN.cSill - 0.12, 0.02, 3.4);+ // ---- Rose: moulded ring, tracery, spandrels, relieving arch ----+ place('Great_Rose_Tracery', roseTracery(), MAT.fine, { pos: [0, WF.roseY, -0.4], parent: gWest });+ F.at(M.T(0, WF.roseY, 0), () => {+ F.at(M.T(0, 0, 0), () => {+ const ring = traceryBar(0.46, 0.58);+ F.sweep(ringPts(0, 0, WF.roseR + 0.32, 40), ring, { closed: true, z: 0.14 });+ F.sweep(ringPts(0, 0, WF.roseR + 0.78, 44), rollSection(0.2, 0.3, 1, 0.04, 2), { closed: true, z: 0.1 });+ const ndt = 32; for (let i = 0; i < ndt; i++) { const a = i * 360 / ndt; addCrocket(F, M.of(M.Rz(a), M.T(WF.roseR + 0.98, 0, 0.22)), 0.42, 0); }+ });+ });+ F.at(M.T(0, 0, 0.03), () => {+ // spandrels round the rose: plain stone, each with a real pierced roundel carved in its corner+ const R2 = WF.roseR + 0.72, ry = WF.roseY, sq = 6.6;+ for (const [sx, sy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {+ // corner of the square -> along its edge -> round the rose -> along the other edge+ const arcPts = []; for (let i = 0; i <= 8; i++) { const a = i / 8 * PI / 2; arcPts.push([sx * R2 * Math.cos(a), sy * R2 * Math.sin(a)]); }+ const poly = [[sx * sq, sy * sq], [sx * sq, 0], ...arcPts, [0, sy * sq]].map((p) => [p[0], p[1] + ry]);+ F.slab(area2(poly) > 0 ? poly : poly.slice().reverse(), [], -0.04, 0, { noOuterSides: true, noBack: true });+ const cx = sx * (sq - 1.02), cy = ry + sy * (sq - 1.02);+ F.sweep(ringPts(cx, cy, 0.76, 14), traceryBar(0.13, 0.2), { closed: true, z: 0.05 });+ const foil = []; for (let i = 0; i < 16; i++) { const a = i * TAU / 16, R = 0.68 * (0.62 + 0.32 * Math.abs(Math.cos(a * 2))); foil.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.07, 0.14), { closed: true, z: 0.05, crease: 40 });+ }+ });+ F.at(M.T(0, 0, 0.14), () => { F.sweep([[-6.6, WF.roseY - 6.6], [6.6, WF.roseY - 6.6]], stringSection(0.3, 0.26), { caps: false }); F.sweep([[-6.6, WF.roseY + 6.6], [6.6, WF.roseY + 6.6]], stringSection(0.3, 0.26), { caps: false }); });+ // ---- Tower lower stages: paired windows with hood and gable, string courses ----+ const cw = WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = WIN.cRise;+ for (const s of [-1, 1]) {+ F.at(M.T(s * tcx, 0, 0.32), () => {+ addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 8), rollSection(0.24, 0.5, 1, 0.04, 2), { closed: true, crease: 40, z: -0.14 });+ F.sweep(pointedArc(cw + 0.8, cRise + 0.4, 7).map((p) => [p[0], p[1] + cSp]), hexBar(0.26, 0.3), { z: 0.1, caps: false });+ addGable(F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 4, lod: 0 });+ });+ F.at(M.T(s * tcx, 0, 0.02), () => { F.sweep([[-6.1, WF.kingTop + 0.24], [6.1, WF.kingTop + 0.24]], stringSection(0.3, 0.22), { caps: false }); });+ }+ // ---- Buttresses: stepped, with statues in tabernacles and pinnacled set-offs ----+ const xB = [-20.6, -7.5, 7.5, 20.6], xLow = [-20.3, -6.5, 6.5, 20.3];+ xLow.forEach((x, i) => {+ const hwb = 1.3;+ A.box(x - hwb, x + hwb, 0, 18.4, -0.02, 3.6, { noBot: true });+ A.face([[x - hwb, 18.4, 3.6], [x + hwb, 18.4, 3.6], [x + hwb, 19.95, 1.0], [x - hwb, 19.95, 1.0]]);+ F.at(M.T(x, 0, 3.6), () => {+ F.sweep([[-hwb - 0.1, 0.75], [hwb + 0.1, 0.75]], stringSection(0.5, 0.36), {});+ F.sweep([[-hwb - 0.04, 5.4], [hwb + 0.04, 5.4]], stringSection(0.26, 0.2), {});+ F.sweep([[-hwb - 0.04, 11.3], [hwb + 0.04, 11.3]], stringSection(0.26, 0.2), {});+ // statue niche in the lower stage+ addNicheFrame(F, 0, 6.1, 11.0, 1.42);+ addBlindArcade(F, -hwb + 0.1, hwb - 0.1, 1.2, 3.6, 2, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(x, 0, 3.6), () => addBlindArcade(F, -hwb + 0.1, hwb - 0.1, 11.7, 16.4, 2, { bar: 0.12, depth: 0.14 }));+ D.at(M.T(x, 0, 3.6), () => { addStain(D, -hwb, hwb, 18.3, 0.02, 4.8); addStain(D, -hwb, hwb, 11.1, 0.02, 2.2, 'drip'); addStain(D, -hwb, hwb, 5.2, 0.02, 2.4, 'drip'); addStain(D, -hwb, hwb, 4.6, 0.03, 4.6, 'ground'); addStain(D, -hwb, hwb, 5.25, 0.03, 0.7, 'soft'); addStain(D, -hwb, hwb, 11.15, 0.03, 0.7, 'soft'); addStain(D, -hwb, hwb, 2.6, 0.04, 2.1, 'lichen'); });+ placeFigure('Buttress_Saint' + i, SAINTS[i + 3], { pos: [x, 6.2, 3.98], rot: [0, -90, 0], scale: 1.42, parent: gWest });+ place('Facade_Gargoyle' + i, gargoyle(i % 3), MAT.fine, { pos: [x, 17.0, 3.5], rot: [0, -90, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ });+ xB.forEach((x, i) => {+ const hwb = 0.92, z = 1.5;+ A.box(x - hwb, x + hwb, WF.kingTop + 0.05, yTop, -0.02, z, { noBot: true });+ F.at(M.T(x, 0, z), () => {+ F.sweep([[-hwb - 0.05, 30.0], [hwb + 0.05, 30.0]], stringSection(0.24, 0.2), {});+ addNicheFrame(F, 0, 31, 37.4, 1.3);+ F.sweep([[-hwb - 0.05, 38.6], [hwb + 0.05, 38.6]], stringSection(0.24, 0.2), {});+ });+ placeFigure('Upper_Saint' + i, SAINTS[i + 5], { pos: [x, 31.1, 1.86], rot: [0, -90, 0], scale: 1.32, parent: gWest, lod: 0 });+ });+ placeSet('West_Wall', set, [0, 0, 0], 0, gWest);+ // ---- Glass ----+ const roseWest = makeRoseGlass(WF.roseR, 101);+ SHARED.roseGlass = roseWest;+ placeGlass('Great_Rose', roseWest, [0, WF.roseY, -0.4], 180, gWest, true);+ const twinGlass = SHARED.cleres;+ for (const s of [-1, 1]) place('TowerWindow_Out' + s, glassOutside(twinGlass[s > 0 ? 0 : 1]), MAT.glassOut, { pos: [s * tcx, 0, 0.14], rot: [0, 180, 0], parent: gWest, scale: [1, 1, -1] });+ // ---- Three portals: a bishop, a crowned king, a veiled queen, an apostle with his key, a smiling angel;+ // the Virgin and Child on the central door post, Christ enthroned above her ----+ const pC = buildPortal(cP, ['apostle', 'bishop', 'queen', 'king', 'apostle', 'king', 'angel', 'bishop', 'queen', 'apostle'], { main: 'seated', side: ['angel', 'queen'], trumeau: 'virgin', trumeauScale: 1.4 });+ const pS = buildPortal(Object.assign({}, sP, { x: 0 }), ['queen', 'bishop', 'apostle', 'king', 'angel', 'apostle', 'king', 'queen'], { main: 'seatedVirgin', side: ['angel', 'apostle'], trumeau: 'bishop', trumeauScale: 1.24 });+ pC.baseX = cP.x; pS.baseX = 0; SHARED.sidePortal = pS;+ placePortal(gWest, 'Portal_Central', cP, pC);+ placePortal(gWest, 'Portal_South', Object.assign({}, sP, { x: -sP.x }), pS, { light: 'scene' });+ placePortal(gWest, 'Portal_North', Object.assign({}, sP, { x: sP.x }), pS, { light: 'scene' });+ // ---- Gallery of Kings ----+ const nK = P.kings, pitch = 2 * hw / nK, niche = buildKingNiche(pitch), kinds = ['kingA', 'kingB'];+ for (let i = 0; i < nK; i++) {+ const x = (i + 0.5 - nK / 2) * pitch;+ placeSet('King_Niche' + i, niche, [x, 0, 0], 0, gWest);+ placeFigure('King' + i, kinds[i % 2], { pos: [x, WF.kingY + 0.04, 0.5], rot: [0, -90 + (i % 4 - 1.5) * 7, 0], scale: 1.42, parent: gWest, lod: 0, mirror: i % 2 === 1 });+ }+ // ---- High gallery ----+ const nG = 20, gp = 2 * hw / nG, gb = buildGalleryBay(gp);+ for (let i = 0; i < nG; i++) placeSet('Gallery' + i, gb, [(i + 0.5 - nG / 2) * gp, 0, 0], 0, gWest);+ // ---- Central gable rising over the gallery, closing the nave roof ----+ const front = mkBatches(), yG0 = yTop + 1.2, yR = LAY.ridge + 1.2, gW = 2 * (zn + 1.5);+ front.A.at(M.T(0, 0, -1.2), () => front.A.slab([[-gW / 2, yG0 - 2], [gW / 2, yG0 - 2], [gW / 2, yG0], [0, yR + 1.6], [-gW / 2, yG0]], [ringPts(0, yG0 + 1.8, 1.25, 12)], -0.5, 0.5, { sr: 0.5 }));+ front.F.at(M.T(0, 0, -0.7), () => {+ addGable(front.F, gW + 0.2, yR + 1.6 - yG0, 0.04, 0.26, yG0, { size: 0.86, finial: 0.1, crockets: 7, noField: true, lod: 1 });+ front.F.sweep(ringPts(0, yG0 + 1.8, 1.32, 14), traceryBar(0.26, 0.44), { closed: true });+ // Blind arcade stepping up under the rake+ const nA = 7; for (let i = 0; i < nA; i++) { const x = (i - (nA - 1) / 2) * 1.72, top = yG0 + 0.3 + (gW / 2 - Math.abs(x) - 1) * (yR + 1.6 - yG0) / (gW / 2) * 0.7; if (Math.abs(x) < 1.7) continue; front.F.sweep([[x - 0.62, yG0], [x - 0.62, top], [x, top + 0.75], [x + 0.62, top], [x + 0.62, yG0]], hexBar(0.14, 0.16), { caps: false, crease: 20 }); }+ });+ // Pinnacles crowning the upper buttresses either side of the gable+ for (const x of [-20.6, -7.5, 7.5, 20.6]) place('Facade_Pinnacle', largePinnacle(), MAT.fine, { pos: [x, yTop, 0.75], scale: [0.72, 0.74, 0.72], parent: gWest });+ front.G.at(M.T(0, yR + 1.62, -0.5), () => addFinial(front.G, 2.2, 1));+ placeSet('West_Gable', front, [0, 0, 0], 0, gWest);+ place('West_Gable_Glass', makeLancetRoundel(1.25), MAT.glassOut, { pos: [0, yG0 + 1.8, -0.75], parent: gWest });+ // ---- Towers: shell, belfry faces, corner turrets, lead cap ----+ // One tower shell (side and rear walls of the lower stages), built outward towards +x; the other tower mirrors it+ const shell = mkBatches(), tw = 7;+ // The outer side of the tower: real recessed arcades in two tiers, either side of its windows+ const sideM = M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), sideRecess = [];+ for (const d of [-1, 1]) {+ sideRecess.push(archOutline(2.0, 3.4, 15.6, 2.4, 6).map((p) => [d * 4.45 + p[0], p[1]]));+ sideRecess.push(archOutline(2.0, 26.4, 36.6, 2.4, 6).map((p) => [d * 4.45 + p[0], p[1]]));+ }+ shell.A.at(sideM, () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], sideRecess, -0.8, 0, { noOuterSides: true, noBack: true, sr: 0.62 }));+ shell.A.at(sideM, () => shell.F.at(sideM, () => sideRecess.forEach((o) => addRecess(shell.A, shell.F, o, 0.52))));+ shell.D.at(sideM, () => { for (const y of [19.2, WF.kingTop - 0.34, WF.galY - 0.34]) { addStain(shell.D, -tw, tw, y, 0.02, 3.2); addStain(shell.D, -tw, tw, y, 0.03, 0.8, 'soft'); } addStain(shell.D, -tw, tw, 5.2, 0.03, 5.2, 'ground'); });+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(180), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(-90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ // string courses and tall blind windows on the outer side+ shell.F.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => {+ for (const y of [1.0, 19.6, WF.kingTop, WF.galY]) shell.F.sweep([[-tw, y], [tw, y]], stringSection(0.3, 0.26), { caps: false });+ addTracery(shell.F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ shell.F.sweep(archOutline(cw, cSill, cSp, cRise, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ addGable(shell.F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 3, lod: 0 });+ addTracery(shell.F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 });+ shell.F.sweep(archOutline(...windowArgs.aisle, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ });+ shell.A.box(tw - 0.9, tw + 0.9, 0, yTop, -tw - 0.9, -tw + 0.9, {}); // outer angle buttress up the tower side+ for (const s of [-1, 1]) {+ const cx = s * tcx;+ placeSet('Tower_Shell', shell, [cx, 0, 0], 0, gWest, { scale: [s, 1, 1] });+ // Set just proud of the wall face: these towers are solid here, so the glass is seen from outside only+ place('Tower_SideGlass_High' + s, glassOutside(twinGlass[2]), MAT.glassOut, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ place('Tower_SideGlass_Low' + s, glassOutside(SHARED.stories[1]), MAT.glassOut, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ }+ const face = buildBelfryFace(), corner = buildTowerCorner();+ const cap = new Batch(TILE.lead), capG = new Batch(TILE.gold);+ cap.lathe([[5.9, P.towerTop - 2.5], [0.2, P.towerTop + 4.8]], 8, { faceted: true, phase: PI / 8 });+ capG.at(M.T(0, P.towerTop + 4.7, 0), () => addFinial(capG, 2.4, 1));+ for (const s of [-1, 1]) {+ const gT = group('Tower_' + (s > 0 ? 'North' : 'South'), gWest), cx = s * tcx, cz = -tw;+ for (let i = 0; i < 4; i++) {+ placeSet('Belfry_Face' + i, face, [cx, 0, cz], i * 90, gT);+ const ax = cx + (i === 0 || i === 3 ? 1 : -1) * tw, az = cz + (i < 2 ? 1 : -1) * tw;+ placeSet('Tower_Turret' + i, corner, [ax, 0, az], i * 90, gT);+ // the great corner pinnacles carry the tower to its full height+ const yT = P.towerTop - 1.3 + 0.4, hP = P.spireTip - yT;+ place('Tower_Pinnacle' + i, largePinnacle(), MAT.fine, { pos: [ax, yT, az], scale: [1.16, hP / 10, 1.16], parent: gT });+ }+ place('Tower_Cap_Lead', cap, MAT.lead, { pos: [cx, 0, cz], parent: gT });+ place('Tower_Cap_Gilt', capG, MAT.gold, { pos: [cx, 0, cz], parent: gT });+ }+ // ---- The parvis: a low paved forecourt that seats the front on the ground ----+ const parvis = new Batch(TILE.pave);+ parvis.face([[-hw - 2, 0.015, 5.2], [hw + 2, 0.015, 5.2], [hw + 2, 0.015, -0.1], [-hw - 2, 0.015, -0.1]]);+ place('Parvis_Paving', parvis, MAT.pave, { parent: gWest });+ // ---- Inner face: blind niches with statues, a gallery, the back of the doors ----+ const inner = mkBatches();+ inner.F.at(M.of(M.T(0, 0, -WF.wallT), M.Ry(180)), () => {+ inner.F.sweep([[-13.1, 10.2], [13.1, 10.2]], stringSection(0.34, 0.34), { caps: false });+ inner.F.sweep([[-6.5, P.triforiumSill], [6.5, P.triforiumSill]], stringSection(0.34, 0.3), { caps: false });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) addNicheFrame(inner.F, s * (3.65 + i * 1.5), 3.2 + 0, 8.8, 1.2);+ inner.F.sweep(ringPts(0, WF.roseY, WF.roseR + 0.3, 32), traceryBar(0.42, 0.5), { closed: true });+ addBlindArcade(inner.F, -6.5, 6.5, 11, 15.2, 8, { bar: 0.14, depth: 0.2 });+ });+ placeSet('West_InnerFace', inner, [0, 0, 0], 0, gWest);+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) placeFigure('Inner_Saint_' + (s > 0 ? 'N' : 'S') + i, SAINTS[i + (s > 0 ? 1 : 6)], { pos: [s * (3.65 + i * 1.5), 3.25, -WF.wallT - 0.28], rot: [0, 90, 0], scale: 1.32, parent: gWest, indoor: true });+}+function makeLancetRoundel(r) { const g = new Glass(55); g.raw(ringPts(0, 0, r + 0.05, 20), 'lead', -0.4, 0); gOculus(g, 0, 0, r, { ground: 'cobalt', inner: 'gold' }); return g.b; }++// ---- Transept fronts: one portal, a smaller rose, lancets, gable, corner turrets ----------+function buildTransFront(gT, s) {+ const fr = frame(s > 0 ? 0 : 180, LAY.xc, 0), hw = zaw + 1.1, zEnd = zn + (P.transeptBays + 1) * BAY, yTop = P.eaves;+ const set = TRANS.set || (TRANS.set = (() => {+ const q = mkBatches();+ const { A, F, G } = q, rY = 28.6, R = 4.5, w0 = WF.portalS.w0, dh = WF.portalS.ys - 0.9;+ const at = M.T(0, 0, zEnd);+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-hw, 0], [-w0 / 2, 0], [-w0 / 2, dh], [w0 / 2, dh], [w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]], [ringPts(0, rY, R, 32)], -0.9, 0, { noOuterSides: true, sr: 0.62 }));+ F.at(M.of(at, M.T(0, rY, 0.9)), () => { F.sweep(ringPts(0, 0, R + 0.3, 32), traceryBar(0.4, 0.5), { closed: true }); F.sweep(ringPts(0, 0, R + 0.72, 36), rollSection(0.18, 0.26, 1, 0.03, 2), { closed: true }); });+ q.D.at(M.of(at, M.T(0, 0, 0.9)), () => { addStain(q.D, -hw, hw, 17.2, 0.02, 3.2); addStain(q.D, -hw, hw, 22.6, 0.02, 2.8, 'drip'); addStain(q.D, -hw, -w0 / 2, 5.2, 0.03, 5.2, 'ground'); addStain(q.D, w0 / 2, hw, 5.2, 0.03, 5.2, 'ground'); });+ // Row of lancets beneath the rose+ F.at(M.of(at, M.T(0, 0, 0.9)), () => {+ F.sweep([[-hw, 0.8], [hw, 0.8]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, 17.4], [hw, 17.4]], stringSection(0.34, 0.3), { caps: false });+ F.sweep([[-hw, 22.8], [hw, 22.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -8, 8, 18, 20.8, 8, { bar: 0.16, depth: 0.2 });+ for (const x of [-9.6, 9.6]) F.at(M.T(x, 0, 0.1), () => { addTracery(F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 }); F.sweep(archOutline(...windowArgs.aisle, 7), hexBar(0.22, 0.42), { closed: true, crease: 40, z: -0.1 }); });+ });+ // The inner face of the end wall: tiers of blind arcading and string courses+ F.at(M.of(at, M.Ry(180)), () => {+ F.sweep([[-zaw, 11], [zaw, 11]], stringSection(0.3, 0.26), { caps: false });+ F.sweep([[-zaw, 16.8], [zaw, 16.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -5.6, 5.6, 1.2, 7.8, 5, { bar: 0.16, depth: 0.2 });+ addBlindArcade(F, -zn, zn, 11.2, 14.8, 8, { bar: 0.12, depth: 0.16 });+ addBlindArcade(F, -zn, zn, 17, 20.6, 8, { bar: 0.12, depth: 0.16 });+ });+ // Gable closing the arm's roof, in front of the lead+ const yG = yTop, gW = 2 * (zn + 1.6), yR = LAY.ridge + 1;+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-gW / 2, yG], [gW / 2, yG], [0, yR]], [ringPts(0, yG + 3.4, 1.5, 12)], -0.9, 0, { noOuterSides: true, sr: 0.5 }));+ F.at(M.of(at, M.T(0, 0, 0.9)), () => { addGable(F, gW + 0.2, yR - yG, 0.02, 0.24, yG, { size: 0.82, finial: 0.1, crockets: 7, noField: true, lod: 1 }); F.sweep(ringPts(0, yG + 3.4, 1.55, 14), traceryBar(0.24, 0.42), { closed: true }); });+ G.at(M.of(at, M.T(0, yR, 1.12)), () => addFinial(G, 2.0, 1));+ F.at(M.of(at, M.T(0, 0, 1.0)), () => addParapet(F, -hw, hw, yTop - 0.1, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Corner buttresses rising to pinnacles+ for (const sx of [-1, 1]) {+ A.box(sx * hw - 1.15, sx * hw + 1.15, 0, yTop + 1, zEnd - 1, zEnd + 2.3, { noBot: true });+ F.at(M.T(sx * hw, 0, zEnd + 2.3), () => { F.sweep([[-1.25, 0.8], [1.25, 0.8]], stringSection(0.5, 0.36), {}); addNicheFrame(F, 0, 16, 22, 1.5); });+ }+ return q;+ })());+ placeSet('Transept_Front', set, [fr.ox, 0, fr.oz], fr.th, gT);+ // Portal (the same carving as the west side portals), statue niches, glass+ const gPortal = group('Transept_Portal', gT);+ gPortal.position.set(...fr.pos(0, 0, zEnd + 0.9)); gPortal.rotation.y = rad(fr.th);+ placePortal(gPortal, 'Portal_Transept', { x: 0 }, SHARED.sidePortal, { light: 'all' });+ place('Transept_Rose_Tracery', roseTracery(), MAT.fine, { pos: fr.pos(0, 28.6, zEnd + 0.45), rot: fr.rot(0), scale: [4.5 / WF.roseR, 4.5 / WF.roseR, 1], parent: gT });+ for (const sx of [-1, 1]) place('Transept_Pinnacle', largePinnacle(), MAT.fine, { pos: fr.pos(sx * hw, yTop + 1, zEnd + 0.9), scale: [1.05, 1.1, 1.05], parent: gT });+ placeGlass('Transept_Rose', SHARED.roseGlass, fr.pos(0, 28.6, zEnd + 0.45), fr.th + 180, gT, false, 4.5 / WF.roseR);+ const low = SHARED.stories[0];+ for (const sx of [-1, 1]) {+ // Aisle-end windows: the same panes on the inner and outer faces of the end wall+ place('Transept_EndWindow_In', low, MAT.glassIn, { pos: fr.pos(sx * 9.6, 0, zEnd - 0.01), rot: fr.rot(180), parent: gT });+ place('Transept_EndWindow_Out', low, MAT.glassOut, { pos: fr.pos(sx * 9.6, 0, zEnd + 0.92), rot: fr.rot(180), scale: [1, 1, -1], parent: gT });+ placeFigure('Transept_Corner_Saint' + sx, SAINTS[sx > 0 ? 2 : 4], { pos: fr.pos(sx * hw, 16.05, zEnd + 2.62), rot: fr.rot(-90), scale: 1.42, parent: gT, lod: 0 });+ }+}+const TRANS = {};++// ---- The east end: raised sanctuary, high altar with a gilded retable, oak choir stalls ------+function buildSanctuary() {+ const g = group('Sanctuary'), set = mkBatches(), { F, G, O } = set, { xa, xChoirW } = LAY;+ // Three broad steps and the altar, all moulded+ for (let i = 0; i < 3; i++) { const r = 5 - i * 0.7; F.at(M.T(xa + 1, 0, 0), () => F.lathe([[r, i * 0.2], [r, (i + 1) * 0.2], [0.01, (i + 1) * 0.2]], 14, { faceted: true, phase: PI / 14 })); }+ const y0 = 0.6, ax = xa + 1;+ F.at(M.T(ax, y0, 0), () => {+ F.box(-0.8, 0.8, 0, 1.0, -1.7, 1.7, {});+ F.box(-0.92, 0.92, 1.0, 1.12, -1.84, 1.84, { sb: 0.5 });+ for (const s of [-1, 1]) for (const t of [-1, 0, 1]) F.at(M.T(s * 0.82, 0, t * 1.1), () => F.lathe([[0.12, 0], [0.07, 0.12], [0.07, 0.88], [0.13, 1.0]], 6));+ });+ // A carved retable behind the altar: five stone niches under gables, each with its figure, only the arches gilded+ const retM = M.of(M.T(ax - 1.0, y0 + 1.12, 0), M.Ry(90));+ F.at(retM, () => {+ F.box(-2.46, 2.46, -0.26, 0, -0.2, 0.14, {});+ for (let i = -2; i <= 2; i++) {+ const x = i * 0.96, h = i === 0 ? 2.2 : 1.56;+ F.box(x - 0.47, x + 0.47, 0, h - 0.3, -0.2, -0.1, {});+ addGable(F, 0.96, i === 0 ? 0.8 : 0.66, 0.04, 0.06, h - 0.32, { size: 0.15, crockets: 2, finial: 0.24, solid: true, lod: 0 });+ F.at(M.T(x - 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ if (i === 2) F.at(M.T(x + 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ F.at(M.T(x - 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ if (i === 2) F.at(M.T(x + 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ }+ });+ G.at(retM, () => { for (let i = -2; i <= 2; i++) { const x = i * 0.96, h = i === 0 ? 2.2 : 1.56; G.sweep(pointedArc(0.82, 0.52, 4).map((p) => [x + p[0], h - 0.84 + p[1]]), hexBar(0.04, 0.05), { z: 0.03, caps: false }); } });+ const altarSaints = [['apostle', 0.56], ['bishop', 0.56], ['seatedVirgin', 0.74], ['king', 0.56], ['queen', 0.56]];+ altarSaints.forEach((q, n) => placeFigure('Retable_Figure' + n, q[0], { pos: [ax - 1.04, y0 + 1.14, -(n - 2) * 0.96], rot: [0, 0, 0], scale: q[1], parent: g, indoor: true, lod: q[0] === 'seatedVirgin' ? 1 : 0 }));+ // Candlesticks+ for (const t of [-1.5, -0.9, -0.3, 0.3, 0.9, 1.5]) G.at(M.T(ax - 0.2, y0 + 1.12, t), () => { G.lathe([[0.1, 0], [0.04, 0.06], [0.025, 0.42], [0.06, 0.46], [0.03, 0.5]], 6); F.at(M.T(ax - 0.2, y0 + 1.62, t), () => F.lathe([[0.022, 0], [0.022, 0.34], [0.002, 0.36]], 5, { s: 1.6 })); });+ // Choir stalls: stepped oak benches, moulded backs and a carved canopy rail+ const x0 = xa + 5.5, x1 = xChoirW - 1.2;+ for (const s of [-1, 1]) {+ const z0 = s * 5.0;+ O.box(x0, x1, 0, 0.32, z0 - 0.9, z0 + 0.9, { s: 0.42 });+ O.box(x0, x1, 0.32, 0.5, z0 + (s > 0 ? 0.0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.46 });+ O.box(x0, x1, 0, 2.7, z0 + (s > 0 ? 0.8 : -0.9), z0 + (s > 0 ? 0.9 : -0.8), { s: 0.4 });+ const n = Math.floor((x1 - x0) / 1.5);+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * (x1 - x0) / n;+ O.box(x - 0.045, x + 0.045, 0, 0.98, z0 + (s > 0 ? 0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.5 });+ O.box(x - 0.03, x + 0.03, 0.98, 2.72, z0 + (s > 0 ? 0.74 : -0.8), z0 + (s > 0 ? 0.8 : -0.74), { s: 0.48 });+ O.box(x - 0.08, x + 0.08, 2.72, 3.28, z0 + (s > 0 ? 0.56 : -0.86), z0 + (s > 0 ? 0.86 : -0.56), { s: 0.46, taper: 0.05 });+ }+ O.box(x0, x1, 2.7, 2.84, z0 + (s > 0 ? 0.3 : -0.9), z0 + (s > 0 ? 0.9 : -0.3), { s: 0.44 });+ }+ placeSet('Sanctuary', set, [0, 0, 0], 0, g);+}++function buildCathedral() {+ buildBody();+ buildWestFront();+ const gTN = ROOT.children.find((c) => c.name === 'Transept_North'), gTS = ROOT.children.find((c) => c.name === 'Transept_South');+ buildTransFront(gTN, 1);+ buildTransFront(gTS, -1);+ buildSanctuary();+}++function previewStatues() {+ const names = P.statueTest;+ names.forEach((n, i) => placeFigure('Fig_' + n, n, { pos: [0, 0, (i - (names.length - 1) / 2) * 1.0] }));+ const base = new Batch(TILE.ashlar); base.box(-3, 3, -0.4, 0, -6, 6, { s: 0.8 }); place('Ground', base, MAT.ashlar);+}++// ==================================================================================+// BAKED LIGHT. The viewer casts no shadows, so the shadows are computed here. Every opaque surface is voxelized+// into the world, then each vertex looks at the sun, the sky and the rain:+// - rays towards the sun give the low raking light and the cast shadows;+// - rays over the hemisphere give the depth of portals and galleries, and tell inside from outside;+// - a ray straight up finds what the rain never reaches, where seven centuries of soot have stayed.+// A batch placed many times can have one set of vertex colours per side of the building, within a byte budget.+// Sets beyond the budget share the sunniest colours under a darker material.+// ==================================================================================+const SKY_RAYS = (() => {+ const r = [[0, 1, 0, 1]];+ for (let i = 0; i < 4; i++) { const a = i * TAU / 4 + 0.4, e = rad(42); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 1]); }+ for (let i = 0; i < 5; i++) { const a = i * TAU / 5 + 0.1, e = rad(9); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 0]); }+ return r;+})();+// Large meshes of moulding keep their shape with a quarter fewer triangles: the bytes are better spent up close+async function reduceBatches(share = 0.74, repeatedShare = 0.62, minTris = 2000) {+ const seen = new Map(), uses = new Map();+ for (const e of PLACED) uses.set(e.batch, (uses.get(e.batch) || 0) + 1);+ for (const e of PLACED) {+ const m = e.mat;+ if (e.batch.isFigure || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave) continue;+ let nb = seen.get(e.batch);+ if (nb === undefined) {+ const tris = e.batch.I.length / 3, s = uses.get(e.batch) >= 8 ? repeatedShare : share; // a mesh repeated down the whole building costs the most+ nb = tris > minTris ? await lightCopy(e.batch, Math.round(tris * s)) : null;+ if (nb) nb.noColor = e.batch.noColor;+ seen.set(e.batch, nb);+ }+ if (nb) { e.batch = nb; e.part.geometry = nb.toGeo(); }+ }+}+function samplesOf(b) {+ if (b.samples) return b.samples;+ const P0 = b.P, out = [], lim = 0.32;+ for (let i = 0; i < P0.length; i++) out.push(P0[i]);+ for (let t = 0; t < b.I.length; t += 3) {+ const a = b.I[t] * 3, c = b.I[t + 1] * 3, d = b.I[t + 2] * 3;+ const e1 = len3(P0[a] - P0[c], P0[a + 1] - P0[c + 1], P0[a + 2] - P0[c + 2]), e2 = len3(P0[c] - P0[d], P0[c + 1] - P0[d + 1], P0[c + 2] - P0[d + 2]), e3 = len3(P0[d] - P0[a], P0[d + 1] - P0[a + 1], P0[d + 2] - P0[a + 2]), m = Math.max(e1, e2, e3);+ if (m <= lim) continue;+ const n = Math.ceil(m / lim);+ for (let i = 0; i <= n; i++) for (let j = 0; j <= n - i; j++) { const u = i / n, v = j / n, w = 1 - u - v; out.push(P0[a] * u + P0[c] * v + P0[d] * w, P0[a + 1] * u + P0[c + 1] * v + P0[d + 1] * w, P0[a + 2] * u + P0[c + 2] * v + P0[d + 2] * w); }+ }+ b.samples = new Float32Array(out);+ return b.samples;+}+function bakeLight() {+ ROOT.updateMatrixWorld(true);+ const CELL = 0.4, X0 = -72, Y0 = -2, Z0 = -36, NX = 365, NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;+ const grid = new Uint8Array(NX * NY * NZ), coarse = new Uint8Array(CX * CY * CZ);+ const SUN = (() => { const v = P.sun, l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();+ const SUN2 = [[SUN[0] + 0.06, SUN[1], SUN[2] + 0.05], [SUN[0] - 0.05, SUN[1] + 0.05, SUN[2] - 0.05]].map((v) => { const l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; });+ for (const e of PLACED) {+ if (e.mat.transparent) continue;+ const S = samplesOf(e.batch), m = e.part.matrixWorld.elements;+ for (let i = 0; i < S.length; i += 3) {+ const x = S[i], y = S[i + 1], z = S[i + 2], gi = ((m[0] * x + m[4] * y + m[8] * z + m[12] - X0) / CELL) | 0, gj = ((m[1] * x + m[5] * y + m[9] * z + m[13] - Y0) / CELL) | 0, gk = ((m[2] * x + m[6] * y + m[10] * z + m[14] - Z0) / CELL) | 0;+ if (gi >= 0 && gi < NX && gj >= 0 && gj < NY && gk >= 0 && gk < NZ) { grid[gi + NX * (gj + NY * gk)] = 1; coarse[(gi >> 3) + CX * ((gj >> 3) + CY * (gk >> 3))] = 1; }+ }+ }+ // Distance to the first hit, or 1e9 if the ray leaves the world; capped rays return their cap+ const march = (ox, oy, oz, dx, dy, dz, cap) => {+ let t = 0; const st = CELL * 0.8, far = CELL * CK * 0.6;+ while (t < cap) {+ const i = ((ox + dx * t - X0) / CELL) | 0, j = ((oy + dy * t - Y0) / CELL) | 0, k = ((oz + dz * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return 1e9;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)]) return t;+ t += st;+ }+ return cap;+ };+ const solidAt = (x, y, z) => { const i = ((x - X0) / CELL) | 0, j = ((y - Y0) / CELL) | 0, k = ((z - Z0) / CELL) | 0; return i >= 0 && i < NX && j >= 0 && j < NY && k >= 0 && k < NZ && grid[i + NX * (j + NY * k)] === 1; };+ // A vertex's light: [r,g,b] multipliers, plus how much of it was outdoors+ const light = { r: 1, g: 1, b: 1, out: 0, ndl: 0 };+ let sootFloor = 0.56, dampK = 1, indoorK = 1;+ const lightAt = (x, y, z, nx, ny, nz) => {+ // leave the voxels of the surface itself, then look around+ let ox = x + nx * CELL * 1.2, oy = y + ny * CELL * 1.2, oz = z + nz * CELL * 1.2, buried = false;+ if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 2.4; oy = y + ny * CELL * 2.4; oz = z + nz * CELL * 2.4; if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 3.6; oy = y + ny * CELL * 3.6; oz = z + nz * CELL * 3.6; buried = solidAt(ox, oy, oz); } }+ let esc = 0, skyW = 0, sky = 0, near = 0, nearN = 0, zen = 1e9;+ if (!buried) for (let q = 0; q < SKY_RAYS.length; q++) {+ const R = SKY_RAYS[q], up = R[3] === 1, d = march(ox, oy, oz, R[0], R[1], R[2], up ? 200 : 14);+ if (q === 0) zen = d;+ const w = Math.max(0.12, R[0] * nx + R[1] * ny + R[2] * nz + 0.35);+ skyW += w; if (d >= 1e9) { sky += w; if (up) esc++; } else if (!up && d >= 14) sky += w * 0.6;+ near += Math.min(d, 5) / 5; nearN++;+ }+ const ao = nearN ? near / nearN : 0.3, vis = skyW ? sky / skyW : 0, outdoors = esc >= 1 || vis > 0.22;+ let r, g, b;+ if (outdoors) {+ const ndl = nx * SUN[0] + ny * SUN[1] + nz * SUN[2];+ let sunV = 0;+ if (ndl > 0 && !buried) {+ if (march(ox, oy, oz, SUN[0], SUN[1], SUN[2], 200) >= 1e9) sunV += 0.5;+ if (march(ox, oy, oz, SUN2[0][0], SUN2[0][1], SUN2[0][2], 200) >= 1e9) sunV += 0.25;+ if (march(ox, oy, oz, SUN2[1][0], SUN2[1][1], SUN2[1][2], 200) >= 1e9) sunV += 0.25;+ }+ const amb = 0.2 + 0.44 * vis * (0.55 + 0.45 * ao), s = sunV * Math.max(0, ndl) * 1.02;+ r = amb * 0.84 + s * 1.0; g = amb * 0.88 + s * 0.88; b = amb * 0.98 + s * 0.68;+ // What the rain never touches keeps its soot; what it washes stays pale. Gentle on long walls, whose+ // few vertices would smear it: the crisp streaks under their ledges are decals.+ const shelter = zen < 6 ? 1 - zen / 6 : 0, soot = lerp(1, sootFloor, shelter * (1 - Math.max(0, ny)));+ r *= soot; g *= soot * 0.985; b *= soot * 0.96;+ // Rising damp: darker and greener towards the ground, a slow mottling over the height of the wall+ const damp = lerp(1, sstep(0, 9, y), dampK), mot = 0.93 + 0.07 * Math.sin(x * 0.53 + y * 0.31) * Math.sin(z * 0.47 - y * 0.23);+ r *= lerp(0.56, 1, damp) * mot; g *= lerp(0.64, 1, damp) * mot; b *= lerp(0.46, 1, damp) * mot;+ light.out = 1; light.ndl = ndl;+ } else {+ // Inside: warm stone, dark low in the aisles, lifting into the vault+ const aisle = Math.abs(z) > P.naveSpan / 2 + 0.4 && y < P.aisleCrown + 1 ? 0.76 : 1, s = lerp(0.32, 0.86, sstep(0.5, 34, y)) * lerp(0.5, 1, ao) * aisle * indoorK;+ r = s * 1.06; g = s * 0.87; b = s * 0.64;+ light.out = 0; light.ndl = 0;+ }+ light.r = r; light.g = g; light.b = b;+ return light;+ };+ // ---- group placements of each batch by which way they face the sun ----+ const perBatch = new Map();+ for (const e of PLACED) { if (e.batch.noColor || e.batch.isFigure || e.mat.transparent) continue; let l = perBatch.get(e.batch); if (!l) { l = []; perBatch.set(e.batch, l); } l.push(e); }+ const entries = [];+ for (const [b, list] of perBatch) {+ const groups = new Map();+ for (const e of list) {+ const m = e.part.matrixWorld.elements, sx = len3(m[0], m[1], m[2]) || 1, sy = len3(m[4], m[5], m[6]) || 1, sz = len3(m[8], m[9], m[10]) || 1;+ // the sun in this placement's own frame (mirror included)+ const lx = (m[0] * SUN[0] + m[1] * SUN[1] + m[2] * SUN[2]) / (sx * sx), ly = (m[4] * SUN[0] + m[5] * SUN[1] + m[6] * SUN[2]) / (sy * sy), lz = (m[8] * SUN[0] + m[9] * SUN[1] + m[10] * SUN[2]) / (sz * sz);+ const key = Math.round(lx * 3) + ',' + Math.round(ly * 3) + ',' + Math.round(lz * 3);+ let g = groups.get(key); if (!g) { g = { key, list: [], L: [lx, ly, lz] }; groups.set(key, g); } g.list.push(e);+ }+ const gl = [...groups.values()];+ for (const g of gl) { // how much sun this side receives, by area of normals+ let s = 0; const nn = b.N; for (let i = 0; i < nn.length; i += 3) s += Math.max(0, nn[i] * g.L[0] + nn[i + 1] * g.L[1] + nn[i + 2] * g.L[2]);+ g.expo = s / Math.max(1, nn.length / 3);+ }+ gl.sort((a, c) => c.expo - a.expo);+ entries.push({ b, gl, wall: list[0].mat === MAT.ashlar ? 0 : 1 });+ }+ // Long walls get their own light for each side first: they carry the sense of sun and shade from a distance+ entries.sort((a, c) => a.wall - c.wall || a.b.count - c.b.count);+ let spent = 0, variants = 0, tinted = 0, vbaked = 0;+ const TINTS = new Map();+ const tintOf = (mat, tier) => {+ const key = mat.uuid + '|' + tier; let m = TINTS.get(key);+ if (!m) { m = mat.clone(); m.name = mat.name + (tier === 1 ? '_ShadeSide' : '_DeepShadeSide'); m.color.setRGB(tier === 1 ? 0.84 : 0.68, tier === 1 ? 0.84 : 0.68, tier === 1 ? 0.85 : 0.7); TINTS.set(key, m); }+ return m;+ };+ const bakeGroup = (b, g) => {+ const rep = g.list[Math.floor(g.list.length / 2)], m = rep.part.matrixWorld.elements, n = b.count, out = new Uint8Array(n * 3), PP = b.P, NN = b.N, CC = b.C;+ let outdoor = 0;+ sootFloor = rep.mat === MAT.ashlar ? 0.84 : rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0.9 : 0.56;+ dampK = rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0 : 1;+ indoorK = rep.mat === MAT.oak ? 2.2 : 1; // the oak was given its own dark tone when it was built+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, x = PP[i3], y = PP[i3 + 1], z = PP[i3 + 2], a = NN[i3], c = NN[i3 + 1], d = NN[i3 + 2];+ const wx = m[0] * x + m[4] * y + m[8] * z + m[12], wy = m[1] * x + m[5] * y + m[9] * z + m[13], wz = m[2] * x + m[6] * y + m[10] * z + m[14];+ let nx = m[0] * a + m[4] * c + m[8] * d, ny = m[1] * a + m[5] * c + m[9] * d, nz = m[2] * a + m[6] * c + m[10] * d; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1; nx /= l; ny /= l; nz /= l;+ const L = lightAt(wx, wy, wz, nx, ny, nz);+ outdoor += L.out;+ out[i3] = Math.min(255, CC[i3] * L.r); out[i3 + 1] = Math.min(255, CC[i3 + 1] * L.g); out[i3 + 2] = Math.min(255, CC[i3 + 2] * L.b);+ }+ // Each vertex is one noisy sample of light: blend neighbours on the same surface so no sample can smear a dark+ // triangle across the stone. Edges between planes keep their contrast.+ {+ const sum = new Float32Array(n * 3), w = new Float32Array(n), I = b.I, passes = rep.mat === MAT.ashlar ? 3 : 2;+ const link = (p, q) => { if (NN[p * 3] * NN[q * 3] + NN[p * 3 + 1] * NN[q * 3 + 1] + NN[p * 3 + 2] * NN[q * 3 + 2] < 0.8) return; w[p]++; w[q]++; for (let c = 0; c < 3; c++) { sum[p * 3 + c] += out[q * 3 + c]; sum[q * 3 + c] += out[p * 3 + c]; } };+ for (let pass = 0; pass < passes; pass++) {+ sum.fill(0); w.fill(0);+ for (let t = 0; t < I.length; t += 3) { link(I[t], I[t + 1]); link(I[t + 1], I[t + 2]); link(I[t + 2], I[t]); }+ for (let i = 0; i < n; i++) if (w[i]) for (let c = 0; c < 3; c++) out[i * 3 + c] = out[i * 3 + c] * 0.4 + 0.6 * sum[i * 3 + c] / w[i];+ }+ }+ vbaked += n;+ return { colors: out, outdoor: outdoor / Math.max(1, n) };+ };+ for (const { b, gl } of entries) {+ const base = b.toGeo(), cost = (gl.length - 1) * b.count * 12, full = gl.length === 1 || spent + cost <= P.lightVariantBytes;+ if (full) spent += cost;+ gl.forEach((g, gi) => {+ if (gi === 0 || full) {+ const R = bakeGroup(b, g);+ g.outdoor = R.outdoor;+ if (gi === 0) { base.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true)); return; }+ const v = new THREE.BufferGeometry();+ v.setAttribute('position', base.attributes.position); v.setAttribute('normal', base.attributes.normal); v.setAttribute('uv', base.attributes.uv);+ v.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true));+ if (base.attributes.tangent) v.setAttribute('tangent', base.attributes.tangent);+ v.setIndex(base.index);+ for (const e of g.list) e.part.geometry = v;+ variants++;+ } else {+ // over budget: same colours, a darker stone. Skipped for batches that are mostly indoors+ tinted++;+ if (gl[0].outdoor < 0.3) return;+ const ratio = g.expo / Math.max(1e-3, gl[0].expo);+ if (ratio >= 0.78) return;+ const tier = ratio >= 0.45 ? 1 : 2;+ for (const e of g.list) e.part.material = tintOf(e.mat, tier);+ }+ });+ }+ // ---- figures keep their carved occlusion; the niche they stand in decides how much daylight reaches them ----+ let figN = 0;+ for (const f of FIG_PLACED) {+ const m = f.part.matrixWorld.elements, s = len3(m[0], m[1], m[2]) || 1, fx = m[0] / s, fy = m[1] / s, fz = m[2] / s;+ let ox = m[12] + m[4] * 1.3, oy = m[13] + m[5] * 1.3, oz = m[14] + m[6] * 1.3;+ for (let i = 0; i < 12 && solidAt(ox, oy, oz); i++) { ox += fx * CELL; oy += fy * CELL; oz += fz * CELL; }+ const L = lightAt(ox - fx * 0.05, oy - fy * 0.05, oz - fz * 0.05, fx, fy, fz);+ let mat = MAT.figureIn;+ if (L.out) { const lum = (L.r + L.g + L.b) / 3, facing = fx * SUN[0] + fy * SUN[1] + fz * SUN[2]; mat = lum > 0.56 ? MAT.figure : lum > 0.3 ? MAT.figureHalf : MAT.figureDeep; if (facing < -0.3 && mat === MAT.figure) mat = MAT.figureHalf; }+ f.part.material = mat; if (f.head) f.head.material = mat;+ figN++;+ }+ return { vertices: vbaked, variants, tinted, extraKB: Math.round(spent / 1024), figures: figN };+}++// <<ORNAMENT>>++// <<BUILD>>+async function build() {+ ROOT = createRoot('Cathedral');+ await makeMaterials();+ await prepareFigures(P.preview === 'statues' ? P.statueTest : Object.keys(FIGURES));+ if (P.preview === 'statues') { previewStatues(); return ROOT; }+ if (P.preview === 'all' || P.preview === 'unique') {+ buildCathedral();+ await reduceBatches();+ bakeLight();+ return ROOT;+ }+ if (P.preview === 'size') { // builds and bakes everything, then reports bytes as part names, and exports only a probe: fast+ buildCathedral();+ await reduceBatches();+ const baked = bakeLight(), report = ['bake_variants' + baked.variants + '_tinted' + baked.tinted + '_extraKB' + baked.extraKB];+ // Real buffer sizes, each attribute counted once: pos/normal 12, uv 8, colour 12, tangent 16 a vertex where normal-mapped+ const seenAttr = new Set(), seenTan = new Set(), byMat = {}; let bytes = 0, tris = 0;+ const add = (attr, per, key) => { if (!attr || seenAttr.has(attr)) return; seenAttr.add(attr); const b = attr.count * per; bytes += b; byMat[key] = (byMat[key] || 0) + b; };+ for (const e of PLACED) {+ const g = e.part.geometry, key = e.mat.name, a = g.attributes, m = e.part.material;+ tris += g.index.count / 3;+ add(a.position, 12, key); add(a.normal, 12, key); add(a.uv, 8, key); add(a.color, 12, key);+ add(g.index, g.index.array.BYTES_PER_ELEMENT, key);+ if (m && m.normalMap && !seenTan.has(a.position)) { seenTan.add(a.position); bytes += a.position.count * 16; byMat[key] = (byMat[key] || 0) + a.position.count * 16; }+ }+ report.push('geomKB' + Math.round(bytes / 1024) + '_meshes' + PLACED.length + '_rendered' + Math.round(tris));+ Object.keys(byMat).sort((p, q) => byMat[q] - byMat[p]).forEach((k) => report.push(k + '_KB' + Math.round(byMat[k] / 1024)));+ ROOT.clear();+ createPart('SizeProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ report.forEach((d) => group('SIZE_' + d));+ return ROOT;+ }+ if (P.preview === 'textures') { // measures the texture payload on its own+ Object.keys(MAT).forEach((k, i) => createPart('Swatch_' + k, boxGeo(1, 1, 1), MAT[k], { position: [i * 1.2, 0.5, 0], parent: ROOT }));+ return ROOT;+ }+ if (P.preview === 'stats') {+ buildCathedral();+ const list = [...STAT.entries()].map(([b, e]) => ({ name: e.name, n: e.n, tris: b.I.length / 3, kb: Math.round((batchBytes(b) + (e.normalMapped ? b.count * 16 : 0)) / 1024) })).sort((a, c) => c.kb - a.kb);+ let total = 0, draws = 0, rendered = 0; for (const e of list) { total += e.kb; draws += e.n; rendered += e.n * e.tris; }+ createPart('StatProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ group('STAT_TOTAL_kb_' + total + '_draws_' + draws + '_rendered_' + rendered + '_unique_' + list.length);+ list.slice(0, 40).forEach((e, i) => group('STAT_' + String(i).padStart(2, '0') + '_K' + Math.round(e.n * e.tris / 1000) + '_' + e.name + '_x' + e.n + '_tris' + e.tris + '_kb' + e.kb));+ return ROOT;+ }+ return ROOT;+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-3.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/cathedral-3.kiln.jsnew file mode 100644index 00000000..418d85bd--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/cathedral-3.kiln.js@@ -0,0 +1,3960 @@+// Gothic cathedral: parametric High Gothic church, true scale, metres.+// +X = west front (entrance) , -X = apse, +Y up, +Z = north.+const meta = { name: 'Gothic Cathedral', role: 'wonder' };++// ==================================================================================+// PARAMETERS: change these and the whole design follows.+// ==================================================================================+const P = {+ preview: 'all', // 'all', or 'size' (true buffer bytes, fast), 'stats' (per-mesh list), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)+ // Plan+ naveBays: 9, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,+ xWest: 62, // west face of the towers+ towerDepth: 14, towerWidth: 14,+ choirBays: 3, transeptBays: 2,+ apseSides: 7,+ // Elevation+ pierTop: 15, aisleCrown: 21, triforiumSill: 21.8, triforiumTop: 26.6,+ vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,+ aisleWallTop: 23, aisleRoofHigh: 26.2,+ towerTop: 72, spireTip: 80,+ flecheTip: 74,+ // Detail+ jambStatues: 5, kings: 16, seed: 1163,+ archFigureTris: 720, statueTest: ['apostle', 'bishop', 'king', 'queen', 'angel', 'virgin'],+ storyWindows: 3, clerestoryWindows: 3, // unique window cartoons (more = richer, larger GLB)+ // Glass+ glassGlow: 1.05, glassOuterGlow: 0.12, sunShear: [0.34, 0.86],+ sun: [0.62, 0.45, -0.64], lightVariantBytes: 250000, wallTessellation: 5, // baked low sun from the south-west; bytes allowed for per-side vertex light; metres between lit wall vertices+ chapelFacets: 5,+};++// ==================================================================================+// MATH+// ==================================================================================+const PI = Math.PI, TAU = PI * 2, rad = (d) => d * PI / 180;+const lerp = (a, b, t) => a + (b - a) * t;+const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));+const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };+const gauss = (x, y = 0) => Math.exp(-(x * x + y * y));+function mkRng(seed) { let s = seed >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }+function keyLin(keys, t) {+ if (t <= keys[0][0]) return keys[0].slice(1);+ for (let i = 1; i < keys.length; i++) if (t <= keys[i][0]) {+ const a = keys[i - 1], b = keys[i], f = (t - a[0]) / (b[0] - a[0]);+ return a.slice(1).map((v, k) => lerp(v, b[k + 1], f));+ }+ return keys[keys.length - 1].slice(1);+}+function keyf(keys, t, w = 0.03) { // smoothed keyframes [[t,v...],...]: no plateaus or kinks at the keys+ const a = keyLin(keys, t - w), b = keyLin(keys, t), c = keyLin(keys, t + w);+ return b.map((v, k) => (a[k] + 2 * v + c[k]) / 4);+}+const M = {+ I: () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],+ T: (x, y, z) => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z],+ S: (x, y = x, z = x) => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0],+ Rx: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0]; },+ Ry: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0]; },+ Rz: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0]; },+ mul: (a, b) => [+ a[0] * b[0] + a[1] * b[4] + a[2] * b[8], a[0] * b[1] + a[1] * b[5] + a[2] * b[9], a[0] * b[2] + a[1] * b[6] + a[2] * b[10], a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3],+ a[4] * b[0] + a[5] * b[4] + a[6] * b[8], a[4] * b[1] + a[5] * b[5] + a[6] * b[9], a[4] * b[2] + a[5] * b[6] + a[6] * b[10], a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7],+ a[8] * b[0] + a[9] * b[4] + a[10] * b[8], a[8] * b[1] + a[9] * b[5] + a[10] * b[9], a[8] * b[2] + a[9] * b[6] + a[10] * b[10], a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11],+ ],+ of: (...ms) => ms.reduce((a, b) => M.mul(a, b)),+};+function normalMat(m) { // inverse transpose of the 3x3, up to scale (cofactors)+ return [+ m[5] * m[10] - m[6] * m[9], m[6] * m[8] - m[4] * m[10], m[4] * m[9] - m[5] * m[8],+ m[2] * m[9] - m[1] * m[10], m[0] * m[10] - m[2] * m[8], m[1] * m[8] - m[0] * m[9],+ m[1] * m[6] - m[2] * m[5], m[2] * m[4] - m[0] * m[6], m[0] * m[5] - m[1] * m[4],+ ];+}+const det3 = (m) => m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8]) + m[2] * (m[4] * m[9] - m[5] * m[8]);++// ==================================================================================+// MESH KIT: one Batch = one mesh with one material; metric UVs, baked shading in COLOR_0+// ==================================================================================+class Batch {+ constructor(tile = 1) { this.P = []; this.N = []; this.UV = []; this.C = []; this.I = []; this.tile = tile; this.setM(M.I()); this.sh = 1; this.rgb = [1, 1, 1]; this.geo = null; }+ setM(m) { this.m = m; this.nm = normalMat(m); this.det = det3(m); }+ at(m, f) { const old = this.m; this.setM(M.mul(old, m)); f(this); this.setM(old); return this; }+ shade(s, f) { const o = this.sh; this.sh = o * s; f(this); this.sh = o; return this; }+ tint(rgb, f) { const o = this.rgb; this.rgb = rgb; f(this); this.rgb = o; return this; }+ get count() { return this.P.length / 3; }+ v(x, y, z, nx, ny, nz, u, v, s = 1) {+ const m = this.m, n = this.nm;+ const wx = m[0] * x + m[1] * y + m[2] * z + m[3], wy = m[4] * x + m[5] * y + m[6] * z + m[7], wz = m[8] * x + m[9] * y + m[10] * z + m[11];+ let ax = n[0] * nx + n[1] * ny + n[2] * nz, ay = n[3] * nx + n[4] * ny + n[5] * nz, az = n[6] * nx + n[7] * ny + n[8] * nz;+ const l = Math.hypot(ax, ay, az);+ if (l < 1e-10) { ax = 0; ay = 1; az = 0; } else { ax /= l; ay /= l; az /= l; }+ if (this.det < 0) { ax = -ax; ay = -ay; az = -az; }+ if (u === undefined || u === null) {+ const fx = Math.abs(ax), fy = Math.abs(ay), fz = Math.abs(az);+ if (fy > 0.75) { u = wx; v = wz; } else if (fx > fz) { u = wz; v = wy; } else { u = wx; v = wy; }+ }+ const sh = clamp(s * this.sh); // all light is added later by bakeLight+ const rgb = this.rgb;+ this.P.push(wx, wy, wz); this.N.push(ax, ay, az); this.UV.push(u / this.tile, v / this.tile);+ this.C.push(Math.round(255 * sh * rgb[0]), Math.round(255 * sh * rgb[1]), Math.round(255 * sh * rgb[2]));+ return this.P.length / 3 - 1;+ }+ tri(a, b, c) { if (this.det < 0) this.I.push(a, c, b); else this.I.push(a, b, c); }+ quad(a, b, c, d) { this.tri(a, b, c); this.tri(a, c, d); }+ // flat polygon from 3D points (CCW seen from the front); convex fan+ face(pts, s = 1) {+ const a = pts[0], b = pts[1], c = pts[pts.length - 1];+ let nx = (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), ny = (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), nz = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ if (pts.length > 3 && Math.hypot(nx, ny, nz) < 1e-9) { const d = pts[2]; nx = (b[1] - a[1]) * (d[2] - a[2]) - (b[2] - a[2]) * (d[1] - a[1]); ny = (b[2] - a[2]) * (d[0] - a[0]) - (b[0] - a[0]) * (d[2] - a[2]); nz = (b[0] - a[0]) * (d[1] - a[1]) - (b[1] - a[1]) * (d[0] - a[0]); }+ const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l;+ const ids = pts.map((p) => this.v(p[0], p[1], p[2], nx, ny, nz, undefined, undefined, p[3] === undefined ? s : p[3]));+ for (let i = 1; i < ids.length - 1; i++) this.tri(ids[0], ids[i], ids[i + 1]);+ return this;+ }+ box(x0, x1, y0, y1, z0, z1, o = {}) {+ const s = o.s === undefined ? 1 : o.s, sb = o.sb === undefined ? s * 0.62 : o.sb, st = o.st === undefined ? s : o.st;+ const tx = o.taper || 0;+ const X0 = x0 + tx, X1 = x1 - tx, Z0 = z0 + (o.taperZ === undefined ? tx : o.taperZ), Z1 = z1 - (o.taperZ === undefined ? tx : o.taperZ);+ this.face([[x1, y0, z1, s], [x1, y0, z0, s], [X1, y1, Z0, st], [X1, y1, Z1, st]]); // +X+ this.face([[x0, y0, z0, s], [x0, y0, z1, s], [X0, y1, Z1, st], [X0, y1, Z0, st]]); // -X+ this.face([[x0, y0, z1, s], [x1, y0, z1, s], [X1, y1, Z1, st], [X0, y1, Z1, st]]); // +Z+ this.face([[x1, y0, z0, s], [x0, y0, z0, s], [X0, y1, Z0, st], [X1, y1, Z0, st]]); // -Z+ if (!o.noTop) this.face([[X0, y1, Z1, st], [X1, y1, Z1, st], [X1, y1, Z0, st], [X0, y1, Z0, st]]);+ if (!o.noBot) this.face([[x0, y0, z0, sb], [x1, y0, z0, sb], [x1, y0, z1, sb], [x0, y0, z1, sb]]);+ return this;+ }+ // Surface of revolution about Y. prof: [[r,y],...] bottom to top. o.sides, o.faceted, o.crease, o.flat+ lathe(prof, n = 12, o = {}) {+ const a0 = o.phase || 0, cr = Math.cos(rad(o.crease === undefined ? 55 : o.crease));+ const s = o.s === undefined ? 1 : o.s;+ const segs = [];+ for (let j = 0; j < prof.length - 1; j++) {+ const dr = prof[j + 1][0] - prof[j][0], dy = prof[j + 1][1] - prof[j][1], l = Math.hypot(dr, dy) || 1;+ segs.push([dy / l, -dr / l, l]);+ }+ if (o.faceted) { // hard-edged polygon of revolution+ for (let i = 0; i < n; i++) {+ const t0 = a0 + TAU * i / n, t1 = a0 + TAU * (i + 1) / n, tm = (t0 + t1) / 2;+ const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1), cm = Math.cos(tm), sm = Math.sin(tm);+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const p0 = prof[j], p1 = prof[j + 1];+ const w0 = p0[0] * Math.hypot(c1 - c0, s1 - s0), w1 = p1[0] * Math.hypot(c1 - c0, s1 - s0);+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ const a = this.v(p0[0] * c0, p0[1], p0[0] * s0, nr * cm, ny, nr * sm, -w0 / 2, vv, sA);+ const b = this.v(p1[0] * c0, p1[1], p1[0] * s0, nr * cm, ny, nr * sm, -w1 / 2, vv + l, sB);+ const c = this.v(p1[0] * c1, p1[1], p1[0] * s1, nr * cm, ny, nr * sm, w1 / 2, vv + l, sB);+ const d = this.v(p0[0] * c1, p0[1], p0[0] * s1, nr * cm, ny, nr * sm, w0 / 2, vv, sA);+ this.quad(a, b, c, d); vv += l;+ }+ }+ return this;+ }+ // smooth around, creased along the profile when the section turns sharply+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const prev = segs[j - 1], next = segs[j + 1];+ let n0r = nr, n0y = ny, n1r = nr, n1y = ny;+ if (prev && prev[0] * nr + prev[1] * ny > cr) { n0r = prev[0] + nr; n0y = prev[1] + ny; }+ if (next && next[0] * nr + next[1] * ny > cr) { n1r = next[0] + nr; n1y = next[1] + ny; }+ const base = this.count, p0 = prof[j], p1 = prof[j + 1];+ const circ = Math.max(p0[0], p1[0]) * TAU, uScale = Math.max(1, Math.round(circ)) / n;+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ for (let i = 0; i <= n; i++) {+ const t = a0 + TAU * i / n, c = Math.cos(t), sn = Math.sin(t);+ this.v(p0[0] * c, p0[1], p0[0] * sn, n0r * c, n0y, n0r * sn, i * uScale, vv, sA);+ this.v(p1[0] * c, p1[1], p1[0] * sn, n1r * c, n1y, n1r * sn, i * uScale, vv + l, sB);+ }+ for (let i = 0; i < n; i++) this.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+ vv += l;+ }+ return this;+ }+ // Sweep a closed section (a,b CCW; a along in-plane left normal, b along +Z) along a path in the XY plane.+ sweep(path, sec, o = {}) {+ const closedPath = !!o.closed, cr = Math.cos(rad(o.crease === undefined ? 50 : o.crease)), z0 = o.z || 0, s = o.s === undefined ? 1 : o.s;+ const n = path.length, E = [];+ const segCount = closedPath ? n : n - 1;+ for (let i = 0; i < segCount; i++) { const p = path[i], q = path[(i + 1) % n]; const dx = q[0] - p[0], dy = q[1] - p[1], l = Math.hypot(dx, dy) || 1e-6; E.push([dx / l, dy / l, l]); }+ // section normals+ const K = sec.length, SN = [];+ for (let j = 0; j < K; j++) { const a = sec[j], b = sec[(j + 1) % K]; const da = b[0] - a[0], db = b[1] - a[1], l = Math.hypot(da, db) || 1e-6; SN.push([db / l, -da / l, l]); }+ const rings = []; let dist = 0;+ for (let i = 0; i < segCount; i++) {+ const e = E[i], ePrev = closedPath ? E[(i - 1 + segCount) % segCount] : (E[i - 1] || e), eNext = closedPath ? E[(i + 1) % segCount] : (E[i + 1] || e);+ const p = path[i], q = path[(i + 1) % n];+ const miter = (A, B) => { let bx = -(A[1] + B[1]), by = A[0] + B[0]; const l = Math.hypot(bx, by) || 1; bx /= l; by /= l; const nlx = -B[1], nly = B[0]; const c = Math.max(0.35, bx * nlx + by * nly); return [bx / c, by / c]; };+ const m0 = miter(ePrev, e), m1 = miter(e, eNext);+ const smooth0 = ePrev[0] * e[0] + ePrev[1] * e[1] > cr, smooth1 = e[0] * eNext[0] + e[1] * eNext[1] > cr;+ const nl = [-e[1], e[0]];+ const n0 = smooth0 ? [-(e[1] + ePrev[1]), e[0] + ePrev[0]] : nl, n1 = smooth1 ? [-(e[1] + eNext[1]), e[0] + eNext[0]] : nl;+ const ln0 = Math.hypot(n0[0], n0[1]) || 1, ln1 = Math.hypot(n1[0], n1[1]) || 1;+ let perim = 0;+ for (let j = 0; j < K; j++) {+ const A = sec[j], B = sec[(j + 1) % K], sn = SN[j], pv = SN[(j - 1 + K) % K], nx = SN[(j + 1) % K];+ const sA = sn[0] * pv[0] + sn[1] * pv[1] > cr, sB = sn[0] * nx[0] + sn[1] * nx[1] > cr;+ const nA = sA ? [sn[0] + pv[0], sn[1] + pv[1]] : sn, nB = sB ? [sn[0] + nx[0], sn[1] + nx[1]] : sn;+ const norm = (na, nb, nv, ln) => { const l = Math.hypot(na, nb) || 1; const a = na / l, b = nb / l; return [a * nv[0] / ln, a * nv[1] / ln, b]; };+ const na0 = norm(nA[0], nA[1], n0, ln0), nb0 = norm(nB[0], nB[1], n0, ln0), na1 = norm(nA[0], nA[1], n1, ln1), nb1 = norm(nB[0], nB[1], n1, ln1);+ const shA = A[2] === undefined ? s : A[2] * s, shB = B[2] === undefined ? s : B[2] * s;+ const a = this.v(p[0] + A[0] * m0[0], p[1] + A[0] * m0[1], z0 + A[1], na0[0], na0[1], na0[2], dist, perim, shA);+ const b = this.v(p[0] + B[0] * m0[0], p[1] + B[0] * m0[1], z0 + B[1], nb0[0], nb0[1], nb0[2], dist, perim + sn[2], shB);+ const c = this.v(q[0] + B[0] * m1[0], q[1] + B[0] * m1[1], z0 + B[1], nb1[0], nb1[1], nb1[2], dist + e[2], perim + sn[2], shB);+ const d = this.v(q[0] + A[0] * m1[0], q[1] + A[0] * m1[1], z0 + A[1], na1[0], na1[1], na1[2], dist + e[2], perim, shA);+ this.quad(a, b, c, d);+ perim += sn[2];+ }+ dist += e[2];+ rings.push(m0);+ }+ if (!closedPath && o.caps !== false) {+ const capAt = (p, nv, e, flip) => {+ const pts = sec.map((A) => [p[0] + A[0] * nv[0], p[1] + A[0] * nv[1], z0 + A[1]]);+ const idx = triangulate(sec.map((A) => [A[0], A[1]]));+ const ids = pts.map((q) => this.v(q[0], q[1], q[2], flip * e[0], flip * e[1], 0, q[0], q[2], s * 0.8));+ for (const t of idx) { if (flip > 0) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); else this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ };+ capAt(path[0], rings[0], E[0], -1);+ const lastE = E[segCount - 1]; capAt(path[n - 1], [-lastE[1], lastE[0]], lastE, 1);+ }+ return this;+ }+ // Generic tube along a 3D path with parallel transport. radii: number|array|fn(t) -> [ra, rb]; ring: fn(angle,t) multiplier+ tube(path, radii, n = 8, o = {}) {+ const N = path.length, s = o.s === undefined ? 1 : o.s;+ let T = [], U = o.up ? [...o.up] : null;+ for (let i = 0; i < N; i++) {+ const p = path[Math.max(0, i - 1)], q = path[Math.min(N - 1, i + 1)];+ let tx = q[0] - p[0], ty = q[1] - p[1], tz = q[2] - p[2]; const l = Math.hypot(tx, ty, tz) || 1; T.push([tx / l, ty / l, tz / l]);+ }+ if (!U) U = Math.abs(T[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];+ const base = this.count; const rowLen = n + 1; let dist = 0;+ for (let i = 0; i < N; i++) {+ const t = T[i];+ let d = U[0] * t[0] + U[1] * t[1] + U[2] * t[2];+ let ux = U[0] - d * t[0], uy = U[1] - d * t[1], uz = U[2] - d * t[2]; const ul = Math.hypot(ux, uy, uz) || 1; ux /= ul; uy /= ul; uz /= ul; U = [ux, uy, uz];+ const vx = t[1] * uz - t[2] * uy, vy = t[2] * ux - t[0] * uz, vz = t[0] * uy - t[1] * ux;+ const f = i / (N - 1);+ const rr = typeof radii === 'function' ? radii(f) : (Array.isArray(radii) ? radii[i] : radii);+ const ra = Array.isArray(rr) ? rr[0] : rr, rb = Array.isArray(rr) ? rr[1] : rr;+ if (i > 0) dist += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1], path[i][2] - path[i - 1][2]);+ for (let k = 0; k <= n; k++) {+ const a = TAU * k / n, c = Math.cos(a), sn = Math.sin(a);+ const mod = o.ring ? o.ring(a, f) : 1;+ const shade = o.ringS ? o.ringS(a, f) : 1;+ const px = path[i][0] + (ux * c * ra + vx * sn * rb) * mod, py = path[i][1] + (uy * c * ra + vy * sn * rb) * mod, pz = path[i][2] + (uz * c * ra + vz * sn * rb) * mod;+ const nx = ux * c / (ra || 1) + vx * sn / (rb || 1), ny = uy * c / (ra || 1) + vy * sn / (rb || 1), nz = uz * c / (ra || 1) + vz * sn / (rb || 1);+ this.v(px, py, pz, nx, ny, nz, k / n * Math.max(ra, rb) * TAU, dist, s * shade);+ }+ }+ for (let i = 0; i < N - 1; i++) for (let k = 0; k < n; k++) {+ const a = base + i * rowLen + k, b = a + 1, c = a + rowLen + 1, d = a + rowLen;+ this.quad(a, b, c, d);+ }+ if (o.capEnd !== false) { const c = this.v(path[N - 1][0], path[N - 1][1], path[N - 1][2], T[N - 1][0], T[N - 1][1], T[N - 1][2], 0, 0, s); const b2 = base + (N - 1) * rowLen; const ids = []; for (let k = 0; k < n; k++) ids.push(b2 + k); for (let k = 0; k < n; k++) this.tri(c, ids[k], b2 + k + 1); }+ if (o.capStart) { const c = this.v(path[0][0], path[0][1], path[0][2], -T[0][0], -T[0][1], -T[0][2], 0, 0, s); for (let k = 0; k < n; k++) this.tri(c, base + k + 1, base + k); }+ return this;+ }+ // parametric grid: fn(u,v) -> [x,y,z, shade?]; winding u x v = front. Smooth normals from the grid.+ grid(fn, nu, nv, o = {}) {+ const pts = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pts.push(fn(i / nu, j / nv));+ const id = (i, j) => j * (nu + 1) + i, base = this.count, flip = o.flip ? -1 : 1, s = o.s === undefined ? 1 : o.s;+ const wrapU = !!o.wrapU;+ let vLen = 0;+ for (let j = 0; j <= nv; j++) {+ if (j > 0) { const a = pts[id(0, j - 1)], b = pts[id(0, j)]; vLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ // o.metric: measure across from the middle of each row, so a converging surface keeps true scale instead of a fan of streaks+ let uLen = 0;+ if (o.metric) { let total = 0; for (let i = 1; i <= nu; i++) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; total += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); } uLen = -total / 2; }+ for (let i = 0; i <= nu; i++) {+ if (i > 0) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; uLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ const iP = wrapU ? (i === 0 ? nu - 1 : i - 1) : Math.max(0, i - 1), iN = wrapU ? (i === nu ? 1 : i + 1) : Math.min(nu, i + 1);+ const A = pts[id(iP, j)], B = pts[id(iN, j)], C = pts[id(i, Math.max(0, j - 1))], D = pts[id(i, Math.min(nv, j + 1))];+ const ux = B[0] - A[0], uy = B[1] - A[1], uz = B[2] - A[2], vx = D[0] - C[0], vy = D[1] - C[1], vz = D[2] - C[2];+ const p = pts[id(i, j)];+ const uvU = o.boxUV ? undefined : uLen, uvV = o.boxUV ? undefined : vLen;+ this.v(p[0], p[1], p[2], flip * (uy * vz - uz * vy), flip * (uz * vx - ux * vz), flip * (ux * vy - uy * vx), uvU, uvV, s * (p[3] === undefined ? 1 : p[3]));+ }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) {+ const a = base + id(i, j), b = base + id(i + 1, j), c = base + id(i + 1, j + 1), d = base + id(i, j + 1);+ if (flip > 0) this.quad(a, b, c, d); else this.quad(a, d, c, b);+ }+ return this;+ }+ // 2D outline with holes, extruded along Z from z0 to z1 (front +Z). Reveal sides included.+ slab(outline, holes = [], z0 = 0, z1 = 0.1, o = {}) {+ const so = area2(outline) > 0 ? outline : outline.slice().reverse();+ const hs = holes.map((h) => (area2(h) < 0 ? h : h.slice().reverse()));+ const tris = triangulate(so, hs);+ const all = so.concat(...hs);+ const sf = o.sf === undefined ? 1 : o.sf, sb = o.sb === undefined ? 1 : o.sb, sr = o.sr === undefined ? 0.72 : o.sr;+ if (!o.noFront) { const ids = all.map((p) => this.v(p[0], p[1], z1, 0, 0, 1, undefined, undefined, sf)); for (const t of tris) this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ if (!o.noBack) { const ids = all.map((p) => this.v(p[0], p[1], z0, 0, 0, -1, undefined, undefined, sb)); for (const t of tris) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); }+ const side = (loop, s) => {+ const L = loop.length; if (o.noSides) return;+ for (let i = 0; i < L; i++) {+ const p = loop[i], q = loop[(i + 1) % L]; const dx = q[0] - p[0], dy = q[1] - p[1];+ const l = Math.hypot(dx, dy) || 1, nx = dy / l, ny = -dx / l;+ const a = this.v(p[0], p[1], z0, nx, ny, 0, undefined, undefined, s), b = this.v(q[0], q[1], z0, nx, ny, 0, undefined, undefined, s), c = this.v(q[0], q[1], z1, nx, ny, 0, undefined, undefined, s), d = this.v(p[0], p[1], z1, nx, ny, 0, undefined, undefined, s);+ this.quad(a, b, c, d);+ }+ };+ if (!o.noOuterSides) side(so, o.so === undefined ? 0.85 : o.so);+ hs.forEach((h) => side(h, sr));+ return this;+ }+ // Vertical prism from an (x,z) polygon+ prism(pts, y0, y1, o = {}) {+ let A = 0; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; A += p[0] * q[1] - q[0] * p[1]; }+ const L = A > 0 ? pts : pts.slice().reverse(), s = o.s === undefined ? 1 : o.s, st = o.st === undefined ? s : o.st, n = L.length, k = o.topScale === undefined ? 1 : o.topScale;+ let cx = 0, cz = 0; for (const p of L) { cx += p[0]; cz += p[1]; } cx /= n; cz /= n;+ const top = L.map((p) => [cx + (p[0] - cx) * k, cz + (p[1] - cz) * k]);+ for (let i = 0; i < n; i++) {+ const p = L[i], q = L[(i + 1) % n], P2 = top[i], Q2 = top[(i + 1) % n];+ this.face([[q[0], y0, q[1], s], [p[0], y0, p[1], s], [P2[0], y1, P2[1], st], [Q2[0], y1, Q2[1], st]]);+ }+ if (!o.noTop && k > 1e-4) { const c = top.map((p) => this.v(p[0], y1, p[1], 0, 1, 0, undefined, undefined, st)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i + 1], c[i]); }+ if (o.bot) { const c = L.map((p) => this.v(p[0], y0, p[1], 0, -1, 0, undefined, undefined, s * 0.6)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i], c[i + 1]); }+ return this;+ }+ // Welds vertices that agree in every attribute: sweeps and lathes otherwise export many duplicates+ // Split every edge longer than L. The rule depends only on the edge, so neighbours split alike and no crack opens.+ // Long walls need vertices for the baked light to land on.+ tess(L) {+ this.weld();+ const L2 = L * L, { P: p, N: nn, UV: uv, C: c } = this;+ for (let pass = 0; pass < 7; pass++) {+ const mid = new Map(), I2 = [], I0 = this.I; let changed = false;+ const long = (a, b) => { const x = p[a * 3] - p[b * 3], y = p[a * 3 + 1] - p[b * 3 + 1], z = p[a * 3 + 2] - p[b * 3 + 2]; return x * x + y * y + z * z > L2; };+ const midOf = (a, b) => {+ const lo = Math.min(a, b), hi = Math.max(a, b), key = lo * 4194304 + hi; let m = mid.get(key);+ if (m === undefined) {+ m = p.length / 3; mid.set(key, m);+ p.push((p[lo * 3] + p[hi * 3]) / 2, (p[lo * 3 + 1] + p[hi * 3 + 1]) / 2, (p[lo * 3 + 2] + p[hi * 3 + 2]) / 2);+ let nx = nn[lo * 3] + nn[hi * 3], ny = nn[lo * 3 + 1] + nn[hi * 3 + 1], nz = nn[lo * 3 + 2] + nn[hi * 3 + 2]; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;+ nn.push(nx / l, ny / l, nz / l);+ uv.push((uv[lo * 2] + uv[hi * 2]) / 2, (uv[lo * 2 + 1] + uv[hi * 2 + 1]) / 2);+ c.push((c[lo * 3] + c[hi * 3]) >> 1, (c[lo * 3 + 1] + c[hi * 3 + 1]) >> 1, (c[lo * 3 + 2] + c[hi * 3 + 2]) >> 1);+ }+ return m;+ };+ for (let t = 0; t < I0.length; t += 3) {+ const a = I0[t], b = I0[t + 1], d = I0[t + 2], ab = long(a, b), bd = long(b, d), da = long(d, a);+ if (!ab && !bd && !da) { I2.push(a, b, d); continue; }+ changed = true;+ const m1 = ab ? midOf(a, b) : -1, m2 = bd ? midOf(b, d) : -1, m3 = da ? midOf(d, a) : -1;+ if (ab && bd && da) I2.push(a, m1, m3, m1, b, m2, m3, m2, d, m1, m2, m3);+ else if (ab && bd) I2.push(a, m1, d, m1, m2, d, m1, b, m2);+ else if (bd && da) I2.push(b, m2, a, m2, m3, a, m2, d, m3);+ else if (da && ab) I2.push(d, m3, b, m3, m1, b, m3, a, m1);+ else if (ab) I2.push(a, m1, d, m1, b, d);+ else if (bd) I2.push(b, m2, a, m2, d, a);+ else I2.push(d, m3, b, m3, a, b);+ }+ this.I = I2;+ if (!changed) break;+ }+ this.geo = null;+ return this;+ }+ weld() {+ if (this.welded) return this;+ const n = this.count, map = new Map(), remap = new Int32Array(n), P2 = [], N2 = [], U2 = [], C2 = [];+ const { P: p, N: nn, UV: uv, C: c } = this, col = !this.noColor;+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, i2 = i * 2;+ const key = Math.round(p[i3] * 4000) + ',' + Math.round(p[i3 + 1] * 4000) + ',' + Math.round(p[i3 + 2] * 4000) + ',' + Math.round(nn[i3] * 90) + ',' + Math.round(nn[i3 + 1] * 90) + ',' + Math.round(nn[i3 + 2] * 90) + ',' + Math.round(uv[i2] * 800) + ',' + Math.round(uv[i2 + 1] * 800) + (col ? ',' + c[i3] + ',' + c[i3 + 1] + ',' + c[i3 + 2] : '');+ let k = map.get(key);+ if (k === undefined) { k = P2.length / 3; map.set(key, k); P2.push(p[i3], p[i3 + 1], p[i3 + 2]); N2.push(nn[i3], nn[i3 + 1], nn[i3 + 2]); U2.push(uv[i2], uv[i2 + 1]); C2.push(c[i3], c[i3 + 1], c[i3 + 2]); }+ remap[i] = k;+ }+ const I2 = [];+ for (let t = 0; t < this.I.length; t += 3) {+ const a = remap[this.I[t]], b = remap[this.I[t + 1]], d = remap[this.I[t + 2]];+ if (a !== b && b !== d && a !== d) I2.push(a, b, d);+ }+ this.P = P2; this.N = N2; this.UV = U2; this.C = C2; this.I = I2; this.welded = true;+ return this;+ }+ toGeo() {+ if (this.geo) return this.geo;+ this.weld();+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.P), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(this.N), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.UV), 2));+ if (!this.noColor) g.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(this.C), 3, true));+ g.setIndex(new THREE.BufferAttribute(this.count > 65535 ? new Uint32Array(this.I) : new Uint16Array(this.I), 1));+ this.geo = g;+ return g;+ }+}+function area2(p) { let a = 0; for (let i = 0; i < p.length; i++) { const q = p[(i + 1) % p.length]; a += p[i][0] * q[1] - q[0] * p[i][1]; } return a / 2; }+function triangulate(contour, holes = []) {+ const V = (p) => new THREE.Vector2(p[0], p[1]);+ const tris = THREE.ShapeUtils.triangulateShape(contour.map(V), holes.map((h) => h.map(V)));+ const all = contour.concat(...holes);+ return tris.map((t) => {+ const a = all[t[0]], b = all[t[1]], c = all[t[2]];+ const s = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ return s >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]];+ });+}++// ---- 2D curves -------------------------------------------------------------------+// Two-centred pointed arc: span w, rise h, base y=0 centred at x=0, from left spring to right spring+function pointedArc(w, h, n = 8) {+ const c = (h * h - w * w / 4) / w, r = c + w / 2, pts = [];+ const a0 = PI, a1 = Math.atan2(h, -c);+ for (let i = 0; i <= n; i++) { const a = lerp(a0, a1, i / n); pts.push([c + r * Math.cos(a), r * Math.sin(a)]); }+ for (let i = n - 1; i >= 0; i--) { const a = lerp(a0, a1, i / n); pts.push([-(c + r * Math.cos(a)), r * Math.sin(a)]); }+ pts[n][0] = 0;+ return pts;+}+// CCW outline of a pointed opening: width w, sill y0, spring ys, rise h+function archOutline(w, y0, ys, h, n = 8) {+ const arc = pointedArc(w, h, n).map((p) => [p[0], p[1] + ys]);+ arc.reverse(); // right spring -> apex -> left spring (CCW)+ return [[-w / 2, y0], [w / 2, y0], ...arc];+}+const ringPts = (cx, cy, r, n = 24, ph = 0) => { const a = []; for (let i = 0; i < n; i++) { const t = ph + TAU * i / n; a.push([cx + r * Math.cos(t), cy + r * Math.sin(t)]); } return a; };+const ofsY = (pts, dy) => pts.map((p) => [p[0], p[1] + dy]);+const ofsXY = (pts, dx, dy) => pts.map((p) => [p[0] + dx, p[1] + dy]);++// ---- Sections (a, b, shade?) closed CCW ------------------------------------------+// Moulded arch section with convex rolls: a=0 at the soffit, grows outward; symmetric in b.+function rollSection(w, d, rolls = 3, amp = 0.07, seg = 3) {+ const half = [];+ const A = [w, d / 2], B = [0, d * 0.12], dx = B[0] - A[0], dy = B[1] - A[1], l = Math.hypot(dx, dy);+ for (let k = 0; k < rolls; k++) for (let i = 0; i < seg; i++) {+ const t = (k + i / seg) / rolls, ph = i / seg;+ const off = amp * Math.pow(Math.sin(PI * ph), 0.7), sh = 0.68 + 0.32 * Math.sin(PI * ph);+ half.push([lerp(A[0], B[0], t) + off * dy / l, lerp(A[1], B[1], t) - off * dx / l, sh]);+ }+ half.push([B[0], B[1], 0.8]);+ const back = half.slice().reverse().map((p) => [p[0], -p[1], p[2]]);+ return [[w, -d / 2, 1], ...half, ...back.slice(0, -1)];+}+// Cheapest moulded bar: a hexagon, smooth enough to read as a roll at distance+function hexBar(w, d) { const a = w / 2, b = d / 2; return [[a, -b * 0.5], [a, b * 0.5], [0, b], [-a, b * 0.5], [-a, -b * 0.5], [0, -b]]; }+// Simple chamfered bar centred on the path, width w (in plane), depth d+function barSection(w, d, ch = 0.25) {+ const a = w / 2, b = d / 2, c = Math.min(a, b) * ch * 2;+ return [[-a, -b + c], [-a + c, -b], [a - c, -b], [a, -b + c], [a, b - c], [a - c, b], [-a + c, b], [-a, b - c]];+}+// Tracery bar with a roll on the front and back faces+function traceryBar(w, d) {+ const a = w / 2, b = d / 2, s = [];+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([a * Math.cos(t), b * 0.62 + b * 0.38 * Math.sin(t)]); }+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([-a * Math.cos(t), -b * 0.62 - b * 0.38 * Math.sin(t)]); }+ return s;+}+// String course / cornice projecting along +b from the wall at b=0, height h (a)+function stringSection(h = 0.3, out = 0.25) {+ return [[-h / 2, 0], [-h / 2, out * 0.3, 0.55], [-h * 0.25, out * 0.62, 0.7], [-h * 0.02, out * 0.72, 0.85], [h * 0.12, out, 1], [h * 0.2, out * 0.98, 1], [h / 2, out * 0.05, 1], [h / 2, 0]].reverse();+}++const spline = (pts, sub = 4) => { // Catmull-Rom through 3D points+ const out = [], n = pts.length, g = (i) => pts[clamp(i, 0, n - 1)];+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < sub; k++) {+ const t = k / sub, p0 = g(i - 1), p1 = g(i), p2 = g(i + 1), p3 = g(i + 2), v = [];+ for (let c = 0; c < 3; c++) v.push(0.5 * (2 * p1[c] + (p2[c] - p0[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t * t + (3 * p1[c] - p0[c] - 3 * p2[c] + p3[c]) * t * t * t));+ out.push(v);+ }+ out.push(pts[n - 1]);+ return out;+};+const blob = (b, c, r, nu = 8, nv = 6, sh = 1) => b.grid((u, v) => {+ const ph = -PI + TAU * u, la = -PI / 2 + PI * v;+ return [c[0] + r[0] * Math.cos(la) * Math.cos(ph), c[1] + r[1] * Math.sin(la), c[2] + r[2] * Math.cos(la) * Math.sin(ph), sh];+}, nu, nv, { flip: true, wrapU: true });++// ==================================================================================+// MATERIALS: procedural PBR, with height-derived normals+// ==================================================================================+const MAT = {};+const TILE = { ashlar: 8, fine: 2, lead: 8, pave: 2.4, oak: 1.2, iron: 0.8, gold: 1, flag: 6, earth: 5 };++function ashlarOps(rng, S, tone, joint, stains) {+ // Irregular coursed blocks; shared between albedo and height by replaying the same rng seed+ const ops = [], rows = 7, rowH = Math.floor(S / rows);+ for (let r = 0; r < rows; r++) {+ const y0 = r * rowH; let x = Math.round(rng() * S / 5) % S;+ const n = 3 + Math.floor(rng() * 2), widths = [];+ for (let i = 0; i < n; i++) widths.push(0.7 + rng() * 0.6);+ const tot = widths.reduce((a, c) => a + c, 0);+ for (let i = 0; i < n; i++) {+ const w = Math.round(widths[i] / tot * S), dv = rng(), a = rng();+ const col = tone[Math.floor(dv * tone.length)];+ const x0 = x % S, wr = Math.min(w, S - x0), op = Math.round(10 + a * 34);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y0, width: wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ if (wr < w) ops.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: y0, width: w - wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ x += w;+ ops.push({ op: 'paint', shape: { kind: 'line', from: [x % S, y0], to: [x % S, y0 + rowH], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y0], to: [S, y0], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ return ops.slice(0, 58).concat(stains || []);+}++// Regions of the decal sheet as fractions [u0, v0, u1, v1]; a streak's top edge is at v0+const DECAL_UV = { streak: [0, 0, 0.5, 0.5], soft: [0.5, 0, 1, 0.25], soot: [0.5, 0.25, 1, 0.5], ground: [0, 0.5, 0.5, 1], drip: [0.5, 0.75, 1, 1] };+async function makeMaterials() {+ const S = 512, repeat = { minFilter: 'linear-mipmap-linear', magFilter: 'linear', wrapS: 'repeat', wrapT: 'repeat' };+ const noise = (a, b, scale, oct, seed, opacity, blend) => ({ op: 'noise', colorA: a, colorB: b, scale, octaves: oct, seed, opacity, blend: blend || 'normal' });+ const spec3 = (usage, base, ops, size = S) => ({ schemaVersion: 3, width: size, height: size, usage, operations: [{ op: 'legacy-v2', spec: { schemaVersion: 2, size, usage, layers: base } }, ...ops] });+ const v3 = (usage, base, ops, size = S) => proceduralTexture(spec3(usage, base, ops, size));+ const stamps = (shape, rgba, count, seed, opacity, area) => ({ op: 'stamps', shape, rgba, count, seed, area: area || { x: 0, y: 0, width: S, height: S }, opacity });+ const ell = (w, h) => ({ kind: 'ellipse', x: 0, y: 0, width: w, height: h });+ const tool = (dx, dy, w = 1) => ({ kind: 'line', from: [0, 0], to: [dx, dy], width: w }); // a stroke of the claw chisel++ // ---- Weathered ashlar: 8 m repeat, every block its own stone. Colour varies by block, chisel marks+ // in the relief instead of round pits, rain and soot already sunk into the surface ----+ const quads = [[0, 0], [S, 0], [0, S], [S, S]];+ const ashAlbedo = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb9ad96 }, noise(0x968b76, 0xcdc3ad, 3, 2, seed + 1, 0.85), noise(0x7f7666, 0xc6bca6, 17, 2, seed + 2, 0.4)],+ ashlarOps(mkRng(seed), S, [[78, 72, 60], [226, 216, 190], [158, 132, 92], [122, 128, 124]], { w: 2, rgba: [70, 64, 54, 255], op: 225 }, [+ stamps(ell(20, 70), [70, 66, 56, 255], 26, seed + 3, 24), stamps(ell(40, 26), [52, 56, 46, 255], 16, seed + 4, 20), stamps(tool(11, 6), [96, 88, 74, 255], 110, seed + 5, 34),+ ]));+ const ashHeight = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb4b4b4 }, noise(0x8c8c8c, 0xd0d0d0, 11, 2, seed + 6, 0.7)],+ ashlarOps(mkRng(seed), S, [[126, 126, 126], [214, 214, 214], [170, 170, 170], [150, 150, 150]], { w: 3, rgba: [12, 12, 12, 255], op: 255 }, [+ stamps(tool(14, 8), [86, 86, 86, 255], 120, seed + 8, 90), stamps(tool(-12, 9), [226, 226, 226, 255], 110, seed + 9, 70),+ ]));+ const atlasOf = async (usage, make, seeds) => textureAtlas({ schemaVersion: 1, width: 2 * S, height: 2 * S, pack: { edge: 2048, gutter: 0 }, usage, sampler: repeat, regions: quads.map((p, i) => ({ name: 'q' + i, content: { x: p[0], y: p[1], width: S, height: S }, gutter: 0, source: { kind: 'procedural', spec: make(p, seeds[i]) } })) });+ const ashA = await atlasOf('albedo', ashAlbedo, [77, 131, 211, 307]);+ const ashH = await atlasOf('albedo', ashHeight, [77, 131, 211, 307]);+ MAT.ashlar = pbrMaterial({ albedo: ashA, normal: normalMapFromHeight(ashH, { strength: 2.6 }), roughness: 0.94, metalness: 0, name: 'Limestone_Ashlar' });++ // Dressed stone: no joints; chisel strokes and mottling live in the colour, because a normal map here would+ // add 16 bytes of tangent to every vertex of every moulding+ const finA = v3('albedo', [{ op: 'solid', color: 0xc2b7a0 }, noise(0xb5ab95, 0xcdc4af, 5, 3, 31, 0.8), noise(0xa79d88, 0xcbc2ad, 23, 3, 32, 0.34)], [+ stamps(tool(9, 5), [120, 112, 96, 255], 130, 33, 34), stamps(tool(-7, 6), [214, 206, 188, 255], 80, 34, 30),+ ]);+ MAT.fine = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name: 'Limestone_Dressed' });+ const figTone = (name, r, g, b) => { const m = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name }); m.color.setRGB(r, g, b); m.vertexColors = true; return m; };+ MAT.figure = figTone('Sculpture_Sunlit', 0.86, 0.8, 0.68); // figures take the same stone under different light+ MAT.figureHalf = figTone('Sculpture_HalfShade', 0.5, 0.49, 0.47);+ MAT.figureDeep = figTone('Sculpture_DeepShade', 0.3, 0.31, 0.34);+ MAT.figureIn = figTone('Sculpture_Interior', 0.62, 0.53, 0.42);++ // ---- Patinated lead: long sheets a metre wide running the whole way down the slope. They differ only a little from each+ // other, are lapped once in a long run, and carry the streaks that rain draws down them. The tile repeats every 8 m ----+ const LS = 512, leadOps = (tones, lapOp, seamOp, rngSeed) => {+ const r = mkRng(rngSeed), ops = [], cols = 8, W = LS / cols;+ for (let c = 0; c < cols; c++) {+ const t = tones[Math.floor(r() * tones.length)], lap = Math.floor((c % 2 ? 0.28 : 0.72) * LS + (r() - 0.5) * 40);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: c * W, y: 0, width: W, height: LS }, rgba: [t[0], t[1], t[2], 255], opacity: 12 + Math.floor(r() * 26) });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, lap], to: [(c + 1) * W, lap], width: lapOp.w }, rgba: lapOp.rgba, opacity: lapOp.op });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, 0], to: [c * W, LS], width: seamOp.w }, rgba: seamOp.rgba, opacity: seamOp.op });+ }+ return ops;+ };+ const leadArea = { x: 0, y: 0, width: LS, height: LS };+ const leadA = v3('albedo', [{ op: 'solid', color: 0x6f7a7e }, noise(0x566166, 0x8d9896, 5, 2, 41, 0.75)], [+ ...leadOps([[86, 98, 102], [118, 130, 130], [66, 76, 82], [140, 150, 146], [100, 104, 100]], { w: 2, rgba: [36, 42, 46, 255], op: 220 }, { w: 2, rgba: [40, 46, 50, 255], op: 180 }, 44),+ stamps({ kind: 'rectangle', x: 0, y: 0, width: 4, height: 180 }, [208, 214, 206, 255], 70, 45, 22, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 2, height: 260 }, [34, 40, 44, 255], 90, 46, 36, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 6, height: 120 }, [150, 160, 154, 255], 40, 47, 16, leadArea),+ ], LS);+ const leadH = v3('albedo', [{ op: 'solid', color: 0x8c8c8c }, noise(0x848484, 0x989898, 9, 2, 48, 0.8)], [+ ...leadOps([[140, 140, 140], [132, 132, 132], [148, 148, 148]], { w: 3, rgba: [232, 232, 232, 255], op: 255 }, { w: 5, rgba: [250, 250, 250, 255], op: 255 }, 44),+ stamps(ell(17, 11), [116, 116, 116, 255], 110, 49, 60, leadArea),+ ], LS);+ MAT.lead = pbrMaterial({ albedo: leadA, normal: normalMapFromHeight(leadH, { strength: 3 }), roughness: 0.6, metalness: 0.5, name: 'Lead_Patinated' });++ // ---- Gold leaf: a bright warm metal that still reads as gold where the environment is dull ----+ const gldA = v3('albedo', [{ op: 'solid', color: 0xffc644 }, noise(0xf0a830, 0xffd764, 14, 2, 51, 0.6)], [stamps(ell(5, 4), [176, 110, 30, 255], 60, 52, 90, { x: 0, y: 0, width: 128, height: 128 })], 128);+ MAT.gold = pbrMaterial({ albedo: gldA, roughness: 0.25, metalness: 1, name: 'Gilding' });+ MAT.gold.color.setRGB(0.92, 0.78, 0.56);+ MAT.gold.emissive.setRGB(1, 0.6, 0.14); MAT.gold.emissiveIntensity = 0.1;++ // Aged oak and forged iron from the approved scanned sets+ MAT.oak = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.oak-veneer-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.oak-veneer-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.oak-veneer-arm.v1'), name: 'Oak_Aged' });+ MAT.iron = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.rusted-metal-albedo.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.rusted-metal-arm.v1'), name: 'Iron_Forged' });++ // Paving: worn limestone and grey marble, polished by seven centuries of feet+ const pavA = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'checker', colorA: 0xd2cab6, colorB: 0x8c8678, squares: 4 }, noise(0x7c766a, 0xe0dac8, 9, 3, 61, 0.42), { op: 'bricks', brick: 0xffffff, mortar: 0x5a564e, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ const pavH = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'solid', color: 0xc8c8c8 }, noise(0xb0b0b0, 0xdcdcdc, 15, 3, 62, 0.5), { op: 'bricks', brick: 0xffffff, mortar: 0x202020, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ MAT.pave = pbrMaterial({ albedo: pavA, normal: normalMapFromHeight(pavH, { strength: 1.8 }), roughness: 0.42, metalness: 0, name: 'Paving_Worn' });+ for (const k of ['ashlar', 'fine', 'oak', 'iron', 'pave', 'lead']) MAT[k].vertexColors = true;++ // ---- Weathering decals: rain streaks, soot, ground damp and moss, cast shadow. Alpha-blended overlays+ // give crisp detail on walls that have very few vertices ----+ const DS = 256, rd = mkRng(99), polyStreak = (x, y0, L, w, rgb, op) => ({ op: 'paint', shape: { kind: 'polygon', points: [[x, y0], [x + w, y0], [x + w * 0.8 + 1, y0 + L * 0.45], [x + w * 0.55 + 1, y0 + L * 0.8], [x + w * 0.45, y0 + L], [x + w * 0.3, y0 + L * 0.75], [x + w * 0.15, y0 + L * 0.4]].map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: [rgb[0], rgb[1], rgb[2], 255], opacity: op });+ const streakOps = [], dripOps = [];+ for (let i = 0; i < 44; i++) { const x = Math.floor(rd() * (DS - 9)), dark = rd() > 0.26, L = 65 + Math.floor(rd() * 185), w = 2 + Math.floor(rd() * 7); streakOps.push(polyStreak(x, 0, Math.min(L, DS - 2), w, dark ? [44, 40, 34] : [176, 170, 150], dark ? 40 + Math.floor(rd() * 70) : 26 + Math.floor(rd() * 30))); }+ for (let i = 0; i < 12; i++) streakOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 3 + i * 2 }, rgba: [34, 32, 28, 255], opacity: 12 });+ for (let i = 0; i < 38; i++) { const x = Math.floor(rd() * (DS - 8)), L = 20 + Math.floor(rd() * 100), w = 1 + Math.floor(rd() * 6); dripOps.push(polyStreak(x, 0, Math.min(L, 125), w, [38, 36, 30], 44 + Math.floor(rd() * 70))); }+ for (let i = 0; i < 10; i++) dripOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 2 + i * 2 }, rgba: [28, 26, 24, 255], opacity: 16 });+ const softOps = []; for (let i = 0; i < 30; i++) softOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: Math.max(1, Math.round(128 * Math.pow(1 - i / 30, 1.5))) }, rgba: [18, 18, 24, 255], opacity: 15 });+ const dampOps = [];+ for (let i = 0; i < 18; i++) { const h = Math.round(DS * Math.pow((i + 1) / 18, 1.6)); dampOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: DS - h, width: DS, height: h }, rgba: i % 3 ? [38, 42, 28, 255] : [50, 58, 34, 255], opacity: 9 }); }+ dampOps.push(stamps(ell(13, 8), [66, 84, 42, 255], 110, 71, 46, { x: 0, y: 120, width: DS, height: 134 }), stamps(ell(5, 28), [30, 32, 24, 255], 80, 72, 64, { x: 0, y: 60, width: DS, height: 190 }), stamps(ell(3, 3), [120, 132, 76, 255], 60, 73, 90, { x: 0, y: 100, width: DS, height: 150 }));+ const clear = [0, 0, 0, 0];+ const dreg = (name, x, y, w, h, ops, bg) => ({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'albedo', background: bg || clear, operations: ops } } });+ const decalTex = await textureAtlas({ schemaVersion: 1, width: 512, height: 512, usage: 'albedo', regions: [+ dreg('streak', 0, 0, DS, DS, streakOps.slice(0, 64)),+ dreg('soft', 256, 0, DS, 128, softOps),+ dreg('soot', 256, 128, DS, 128, [stamps(ell(35, 27), [24, 22, 20, 255], 150, 74, 20, { x: 20, y: 15, width: 200, height: 90 }), stamps(ell(13, 11), [18, 16, 14, 255], 90, 75, 36, { x: 45, y: 25, width: 160, height: 75 })]),+ dreg('ground', 0, 256, DS, DS, dampOps),+ dreg('drip', 256, 384, DS, 128, dripOps.slice(0, 64)),+ ] });+ MAT.decal = pbrMaterial({ albedo: decalTex, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Weathering_Decals' });+ MAT.decal.depthWrite = false;++ await makeGlassMaterials();++ // ---- The light of the windows on the floor: a soft shape, painted very small and enlarged, so its edges melt ----+ MAT.pool = pbrMaterial({ roughness: 0.95, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightPool' });+ MAT.pool.vertexColors = true;+ MAT.pool.depthWrite = false;++ // ---- The ground: worn flags in irregular courses, and rough grass over packed earth ----+ const flagTones = [[74, 70, 62], [150, 142, 124], [112, 104, 88], [96, 98, 90]];+ const flagA = v3('albedo', [{ op: 'solid', color: 0x8f8778 }, noise(0x6f695c, 0xa89f8d, 4, 2, 81, 0.8), noise(0x5e594e, 0x9a9282, 19, 2, 82, 0.35)], ashlarOps(mkRng(83), S, flagTones, { w: 4, rgba: [38, 34, 28, 255], op: 235 }, [stamps(ell(30, 18), [168, 160, 140, 255], 40, 84, 26), stamps(tool(8, 3), [70, 66, 58, 255], 70, 85, 30)]));+ const flagH = v3('albedo', [{ op: 'solid', color: 0xb0b0b0 }, noise(0x969696, 0xc4c4c4, 9, 2, 86, 0.6)], ashlarOps(mkRng(83), S, [[140, 140, 140], [190, 190, 190], [165, 165, 165], [150, 150, 150]], { w: 5, rgba: [20, 20, 20, 255], op: 255 }, [stamps(ell(26, 16), [205, 205, 205, 255], 30, 87, 40)]));+ MAT.flag = pbrMaterial({ albedo: flagA, normal: normalMapFromHeight(flagH, { strength: 2.2 }), roughness: 0.9, metalness: 0, name: 'Ground_Flagstone' });+ MAT.flag.vertexColors = true;+ const EA = 256, eArea = { x: 0, y: 0, width: EA, height: EA };+ const earthA = v3('albedo', [{ op: 'solid', color: 0x5d5a3a }, noise(0x4a4a2c, 0x6f6a44, 5, 2, 91, 0.8), noise(0x3f4226, 0x7a7550, 23, 2, 92, 0.4)], [stamps(tool(2, 8), [70, 88, 40, 255], 120, 93, 90, eArea), stamps(tool(-2, 7), [104, 112, 58, 255], 90, 94, 70, eArea), stamps(ell(26, 16), [88, 74, 52, 255], 30, 95, 60, eArea)], EA);+ MAT.earth = pbrMaterial({ albedo: earthA, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Ground_Earth' });+ MAT.earth.vertexColors = true;+ MAT.earth.depthWrite = false;+}++// ---- Atlas of the glass painter: pot-metal colours, painted drapery, drawn faces, ornament ------+const GL = { w: 1536, h: 1280, cell: 256, small: 128, fam: {}, tile: {}, face: {}, orn: {}, ground: {} };+// [name, base, light, dark]: real pot-metal glass is deep, and no two sheets match+const GLASS_FAMILIES = [+ ['ruby', [118, 6, 14], [172, 14, 22], [66, 3, 10]], ['cobalt', [10, 20, 104], [20, 44, 160], [5, 10, 62]], ['azure', [16, 54, 128], [36, 90, 170], [8, 32, 88]], ['emerald', [6, 62, 38], [14, 96, 54], [3, 38, 24]],+ ['green', [42, 74, 24], [74, 106, 34], [24, 48, 14]], ['gold', [178, 116, 16], [218, 158, 36], [130, 78, 8]], ['amber', [146, 72, 10], [186, 106, 20], [98, 44, 6]], ['purple', [58, 14, 80], [90, 26, 114], [34, 6, 50]],+ ['flesh', [186, 140, 104], [206, 164, 128], [156, 110, 80]], ['white', [172, 164, 132], [204, 194, 158], [136, 128, 100]], ['brown', [70, 40, 18], [100, 58, 28], [46, 24, 12]],+ ['teal', [8, 62, 70], [16, 92, 100], [5, 38, 46]], ['night', [5, 8, 40], [10, 16, 62], [2, 4, 24]],+];+const GLASS_ALIAS = { rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', sky: 'azure', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', yellow: 'gold', tan: 'amber', fleshDk: 'flesh', violet: 'purple', olive: 'green', forest: 'emerald', hair: 'brown', paint: 'lead' };+// Mosaic grounds, each a square of painted lozenges: [name, colour A, colour B, jewel]+// A ground stays one colour, in many sheets of it: ruby beside cobalt blends to magenta across a room, ruby on ruby does not+const GLASS_GROUNDS = [['rc', 'ruby', 'ruby', 'gold'], ['cr', 'cobalt', 'cobalt', 'gold'], ['eg', 'emerald', 'emerald', 'gold'], ['pr', 'cobalt', 'azure', 'white']];+// What a leaded window looks like from outside by day: nearly black quarries, each with a hint of its colour, and a faint grey came+function outerGlassOps() {+ const r = mkRng(4711), ops = [], cl = (v) => Math.max(0, Math.min(256, v)), hints = [[88, 10, 16], [12, 20, 96], [12, 20, 70], [70, 10, 14], [22, 26, 34], [66, 44, 12]];+ for (let b = 0; b <= 4; b++) for (let a = 0; a <= 4; a++) {+ if ((a + b) % 2 === 0) continue;+ const c = hints[Math.floor(r() * hints.length)], x = a * 64, y = b * 64;+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 64)], [cl(x + 64), cl(y)], [cl(x), cl(y + 64)], [cl(x - 64), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -1; k <= 1; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ for (let k = 1; k <= 3; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ return ops;+}+function groundOps(A, B, J, seed) { // lozenges 64 px across, a lead round each, a jewel on some crossings+ const r = mkRng(seed * 19 + 5), ops = [], cl = (v) => Math.max(0, Math.min(256, v));+ for (let b = 0; b <= 8; b++) for (let a = 0; a <= 8; a++) {+ if ((a + b) % 2 === 0) continue;+ const f = b % 2 ? A : B, x = a * 32, y = b * 32, t = r(), c = t < 0.45 ? f[1] : t < 0.8 ? f[2] : f[3];+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 32)], [cl(x + 32), cl(y)], [cl(x), cl(y + 32)], [cl(x - 32), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -3; k <= 3; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let k = 1; k <= 7; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let i = 0; i < 4; i++) { const x = 64 + (i % 2) * 128, y = 64 + (i >> 1) * 128; ops.push(paintEll(x, y, 13, 13, 255, [4, 4, 5])); ops.push(paintEll(x, y, 8, 8, 255, J[1])); }+ return ops.slice(0, 64);+}+const DRAPE_OF = { ruby: 'ruby', rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', cobalt: 'cobalt', azure: 'cobalt', sky: 'cobalt', night: 'cobalt', emerald: 'emerald', green: 'emerald', teal: 'emerald', forest: 'emerald', olive: 'emerald', gold: 'gold', amber: 'gold', yellow: 'gold', tan: 'gold', brown: 'gold', white: 'white', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', purple: 'purple', violet: 'purple' };+const GROUND_OF = { ruby: 'rc', rubyLt: 'rc', crimson: 'rc', cobalt: 'cr', azure: 'pr', sky: 'pr', emerald: 'eg', green: 'eg', violet: 'cr', purple: 'cr', teal: 'pr', outer: 'out' };+const PAINT = [36, 20, 10];+const paintLine = (a, b, w, op = 235) => ({ op: 'paint', shape: { kind: 'line', from: [Math.round(a[0]), Math.round(a[1])], to: [Math.round(b[0]), Math.round(b[1])], width: w }, rgba: [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintPoly = (pts, op, rgb) => ({ op: 'paint', shape: { kind: 'polygon', points: pts.map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintEll = (x, y, w, h, op, rgb) => ({ op: 'paint', shape: { kind: 'ellipse', x: Math.round(x - w / 2), y: Math.round(y - h / 2), width: Math.round(w), height: Math.round(h) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });++function familyOps(f, seed) { // one sheet of pot-metal: streaks, clouds, seeds, a thicker edge+ const r = mkRng(seed * 7 + 3), ops = [], C = GL.small, clampE = (x, w) => Math.max(w / 2, Math.min(C - w / 2, x));+ for (let i = 0; i < 10; i++) { const c = r() > 0.5 ? f[2] : f[3], w = 24 + Math.floor(r() * 60), h = 18 + Math.floor(r() * 44); ops.push(paintEll(clampE(r() * C, w), clampE(r() * C, h), w, h, 60 + Math.floor(r() * 90), c)); }+ for (let i = 0; i < 9; i++) { const c = r() > 0.5 ? f[2] : f[3], y = Math.floor(r() * C); ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y], to: [C, Math.max(0, Math.min(C, y + Math.floor((r() - 0.5) * 30)))], width: 2 + Math.floor(r() * 4) }, rgba: [c[0], c[1], c[2], 255], opacity: 70 + Math.floor(r() * 80) }); }+ ops.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 2, height: 2 }, rgba: [f[2][0], f[2][1], f[2][2], 255], count: 40, seed: seed + 11, area: { x: 0, y: 0, width: C, height: C }, opacity: 180 });+ return ops;+}+function drapeOps(f, seed) { // brown paint on coloured glass: long folds, each with a soft wash beside its trace line+ const r = mkRng(seed * 13 + 1), ops = [], W = 256, H = 512, dk = [Math.round(f[1][0] * 0.3), Math.round(f[1][1] * 0.3), Math.round(f[1][2] * 0.3)];+ for (let i = 0; i < 13; i++) {+ const x = 18 + i * 18 + (r() - 0.5) * 10, sw = (r() - 0.5) * 42, y0 = 4 + r() * 120, y1 = H - r() * 90, ym = (y0 + y1) / 2, wash = 9 + r() * 12;+ ops.push(paintPoly([[x, y0], [x + wash, y0 + 10], [x + sw * 0.6 + wash * 1.4, ym], [x + sw + wash, y1 - 10], [x + sw, y1], [x + sw * 0.6, ym]], 62 + Math.floor(r() * 36), PAINT));+ ops.push(paintLine([x, y0], [x + sw * 0.6, ym], 3)); ops.push(paintLine([x + sw * 0.6, ym], [x + sw, y1], 2));+ if (i % 3 === 0) ops.push(paintLine([x + sw * 0.6 + wash * 1.6, ym - 40], [x + sw + wash * 1.6, y1 - 60], 3, 150));+ }+ for (let i = 0; i < 4; i++) { const y = 60 + i * 110 + r() * 40; ops.push(paintPoly([[0, y], [W, y + 26 * (r() - 0.5)], [W, y + 22], [0, y + 18]], 40, PAINT)); }+ return ops.slice(0, 62);+}+// A drawn medieval head, 256 x 256, centred: trace lines in brown over a flesh pane, with washes for the modelling+function faceOps(kind) {+ const ops = [], c = 128, mir = (p) => [256 - p[0], p[1]], both = (fn) => { fn((p) => p); fn(mir); };+ const hairC = kind === 'elder' ? [150, 146, 122] : kind === 'angel' ? [150, 96, 30] : [66, 40, 18];+ // washes: the side of the face and temple, under the brow, beside the nose, under the lip+ both((m) => {+ ops.push(paintPoly([m([18, 84]), m([52, 74]), m([46, 150]), m([72, 214]), m([34, 196])], 78));+ ops.push(paintEll(m([88, 104])[0], 106, 58, 20, 64));+ ops.push(paintPoly([m([122, 118]), m([112, 160]), m([102, 172]), m([118, 172])], 58));+ });+ ops.push(paintEll(c, 200, 52, 11, 58));+ // hair+ if (kind !== 'virgin') {+ ops.push(paintPoly([[6, 118], [14, 56], [60, 10], [128, 0], [196, 10], [242, 56], [250, 118], [222, 96], [206, 56], [160, 42], [128, 54], [96, 42], [50, 56], [34, 96]], 255, hairC));+ for (let i = 0; i < 7; i++) { const a = 28 + i * 16; ops.push(paintLine([a, 94 - i * 7], [a + 36, 12 + i * 2], 3, 190)); ops.push(paintLine([256 - a, 94 - i * 7], [256 - a - 36, 12 + i * 2], 3, 190)); }+ } else {+ ops.push(paintPoly([[0, 150], [2, 56], [50, 8], [128, 0], [206, 8], [254, 56], [256, 150], [226, 118], [214, 66], [128, 46], [42, 66], [30, 118]], 255, [214, 216, 186]));+ ops.push(paintLine([38, 124], [50, 62], 4)); ops.push(paintLine([50, 62], [128, 44], 4)); ops.push(paintLine([128, 44], [206, 62], 4)); ops.push(paintLine([206, 62], [218, 124], 4));+ }+ // brows, lids, eyes+ both((m) => {+ ops.push(paintLine(m([60, 96]), m([84, 84]), 5)); ops.push(paintLine(m([84, 84]), m([116, 92]), 5));+ ops.push(paintLine(m([64, 116]), m([84, 104]), 4)); ops.push(paintLine(m([84, 104]), m([112, 116]), 4));+ ops.push(paintLine(m([66, 118]), m([90, 126]), 2)); ops.push(paintLine(m([90, 126]), m([112, 116]), 2));+ ops.push(paintEll(m([88, 115])[0], 115, 15, 15, 255));+ });+ // nose, nostril, mouth, chin+ ops.push(paintLine([120, 92], [114, 150], 4)); ops.push(paintLine([114, 150], [106, 166], 4)); ops.push(paintLine([106, 166], [124, 174], 3)); ops.push(paintLine([136, 172], [150, 166], 3));+ ops.push(paintLine([98, 192], [128, 187], 4)); ops.push(paintLine([128, 187], [158, 192], 4));+ ops.push(paintLine([112, 202], [144, 202], 2, 170));+ if (kind === 'elder' || kind === 'christ' || kind === 'king') {+ const bc = kind === 'elder' ? [168, 164, 138] : [76, 46, 20];+ ops.push(paintPoly([[40, 158], [66, 184], [94, 196], [128, 206], [162, 196], [190, 184], [216, 158], [206, 226], [160, 256], [96, 256], [50, 226]], 255, bc));+ for (let i = 0; i < 7; i++) { const x = 64 + i * 21; ops.push(paintLine([x, 192 + Math.abs(i - 3) * -3], [x + (i - 3) * 3, 252], 3, 210)); }+ ops.push(paintLine([78, 180], [120, 176], 4)); ops.push(paintLine([178, 180], [136, 176], 4));+ } else {+ ops.push(paintLine([100, 226], [128, 236], 3, 170)); ops.push(paintLine([128, 236], [156, 226], 3, 170));+ }+ return ops.slice(0, 64);+}+function ornOps(f, kind, seed) { // painted ornament for borders: a running stem with leaves, scratched out of a dark ground+ const ops = [], C = GL.small;+ ops.push(paintPoly([[0, 0], [C, 0], [C, C], [0, C]], 210));+ const lit = f[2];+ for (let i = 0; i < 5; i++) {+ const y = 13 + i * 26, s = i % 2 ? 1 : -1;+ ops.push(paintPoly([[C / 2, y - 11], [C / 2 + s * 36, y - 6], [C / 2 + s * 46, y + 7], [C / 2 + s * 23, y + 11], [C / 2, y + 13]], 255, lit));+ ops.push(paintLine([C / 2 + s * 4, y - 3], [C / 2 + s * 37, y + 4], 2));+ ops.push(paintLine([C / 2 + s * 4, y + 5], [C / 2 + s * 28, y + 6], 1));+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [C / 2, 0], to: [C / 2, C], width: 7 }, rgba: [lit[0], lit[1], lit[2], 255] });+ return ops;+}+async function makeGlassMaterials() {+ const regions = [], C = GL.cell, Sm = GL.small;+ const addR = (name, x, y, w, h, bg, ops) => regions.push({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'emissive', background: [bg[0], bg[1], bg[2], 255], operations: ops } } });+ // Layout: two rows of small cells (pot-metals, then ornament), then drapery, faces and mosaic grounds+ GLASS_FAMILIES.forEach((f, i) => { const x = (i % 12) * Sm, y = Math.floor(i / 12) * Sm; GL.fam[f[0]] = [x, y]; addR('fam_' + f[0], x, y, Sm, Sm, f[1], familyOps(f, i + 1)); });+ [['gold', 'gold'], ['white', 'white']].forEach((q, i) => { const f = GLASS_FAMILIES.find((v) => v[0] === q[0]), x = (i + 2) * Sm, y = Sm; GL.orn[q[1]] = [x, y]; addR('orn_' + q[1], x, y, Sm, Sm, f[1], ornOps(f, q[1], i + 90)); });+ // painted drapery, one tall tile per colour+ ['ruby', 'cobalt', 'emerald', 'gold', 'white', 'purple'].forEach((n, i) => { const f = GLASS_FAMILIES.find((q) => q[0] === n), x = i * C, y = C; GL.tile[n] = [x, y, C, 2 * C]; addR('drape_' + n, x, y, C, 2 * C, f[1], drapeOps(f, i + 40)); });+ // faces+ ['youth', 'elder', 'christ', 'king', 'virgin', 'angel'].forEach((n, i) => { const x = i * C, y = 3 * C; GL.face[n] = [x, y]; addR('face_' + n, x, y, C, C, n === 'virgin' ? [202, 162, 128] : [192, 148, 112], faceOps(n)); });+ // mosaic grounds+ GLASS_GROUNDS.forEach((q, i) => { const fam = (n) => GLASS_FAMILIES.find((v) => v[0] === n), x = i * C, y = 4 * C; GL.ground[q[0]] = [x, y]; addR('ground_' + q[0], x, y, C, C, fam(q[1])[3], groundOps(fam(q[1]), fam(q[2]), fam(q[3]), i + 3)); });+ GL.ground.out = [4 * C, 4 * C]; addR('ground_out', 4 * C, 4 * C, C, C, [10, 12, 18], outerGlassOps());+ GL.fam.lead = [1400, 140]; // the empty part of the sheet is black, and the came needs no pixels of its own+ const atlas = await textureAtlas({ schemaVersion: 1, usage: 'emissive', width: GL.w, height: GL.h, pack: { edge: 2048, gutter: 0 }, regions });+ // Seen from inside the glass is the only light: a black pane that shines+ MAT.glassIn = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow, roughness: 1, metalness: 0, name: 'StainedGlass_Interior' });+ MAT.glassIn.color.setRGB(0, 0, 0);+ // Seen from outside by day it is nearly black, a dull sheen and the colour only hinting through.+ // A rough surface keeps grazing angles from washing out to a pale reflection of the sky.+ MAT.glassOut = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.6, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior' });+ MAT.glassOut.color.setRGB(0.008, 0.009, 0.012);+ // Every other window from outside: the outer mosaic only, near black, with the came just catching the eye+ MAT.glassOutWin = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.62, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior_Windows' });+ MAT.glassOutWin.color.setRGB(0.008, 0.009, 0.012);+ // The great rose burns a little brighter than any other window in the building+ MAT.glassRose = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow * 1.22, roughness: 1, metalness: 0, name: 'StainedGlass_Rose' });+ MAT.glassRose.color.setRGB(0, 0, 0);+}++// Hierarchy helper: every mesh gets a unique role name; geometry is shared between placements.+let ROOT = null; const NAMES = new Map();+function uniq(n) { const c = (NAMES.get(n) || 0) + 1; NAMES.set(n, c); return c === 1 ? n : n + '_' + c; }+const STAT = new Map();+// Exporter costs measured on this host: pos+normal+uv 32 B, COLOR_0 written as 12 B floats+function batchBytes(b) { b.weld(); return b.count * (32 + (b.noColor ? 0 : 12)) + b.I.length * (b.count > 65535 ? 4 : 2); }+function place(name, batch, mat, o = {}) {+ if (!batch || !batch.I.length) return null;+ if (mat === MAT.gold) batch.noColor = true; // metal needs no baked shading: saves 12 B a vertex+ if (P.preview === 'unique' && batch.geo) return null; // size test: every geometry once, no instances+ if (mat === MAT.ashlar && !batch.tessellated && !batch.geo) { batch.tess(P.wallTessellation); batch.tessellated = true; }+ if (P.preview === 'stats') {+ const e = STAT.get(batch) || { name, n: 0, normalMapped: mat === MAT.ashlar || mat === MAT.lead || mat === MAT.pave || mat === MAT.oak };+ e.n++; STAT.set(batch, e);+ return null;+ }+ const part = createPart(uniq(name), batch.toGeo(), mat, { position: o.pos || [0, 0, 0], rotation: o.rot || [0, 0, 0], scale: o.scale || [1, 1, 1], parent: o.parent || ROOT });+ PLACED.push({ part, batch, mat });+ return part;+}+const PLACED = [];+function group(name, parent) { const g = new THREE.Group(); g.name = uniq(name); (parent || ROOT).add(g); return g; }++// ==================================================================================+// SCULPTURE: robed figures with carved drapery. A figure is a full surface, not a block:+// S-curve stance, tube folds breaking at the hem, swags across the body, sleeves, hands,+// modelled faces, hair and beards, and the attributes each saint carries.+// ==================================================================================+// Every figure is a signed-distance volume: a long column of cloth with deep pleats, a mantle+// laid over it, arms under the cloth, and a separately resolved head and hands. A surface-net+// pass extracts the skin, the simplifier keeps the carving and drops the rest, and ambient+// occlusion from the field itself is baked into the valleys of the folds.+const len3 = (x, y, z) => Math.sqrt(x * x + y * y + z * z);+const smin = (a, b, k) => { const h = Math.max(k - Math.abs(a - b), 0) / k; return Math.min(a, b) - h * h * k * 0.25; };+const smax = (a, b, k) => -smin(-a, -b, k);+const sdEll = (x, y, z, rx, ry, rz) => { const a = x / rx, b = y / ry, c = z / rz, k0 = Math.sqrt(a * a + b * b + c * c), d = a / rx, e = b / ry, f = c / rz, k1 = Math.sqrt(d * d + e * e + f * f); return k1 > 1e-9 ? k0 * (k0 - 1) / k1 : -Math.min(rx, ry, rz); };+// Rounded cone between two points: the workhorse of fingers, locks, lids and sleeves+function sdCone(x, y, z, ax, ay, az, bx, by, bz, r1, r2) {+ const px = x - ax, py = y - ay, pz = z - az, dx = bx - ax, dy = by - ay, dz = bz - az;+ const t = clamp((px * dx + py * dy + pz * dz) / (dx * dx + dy * dy + dz * dz));+ return len3(px - dx * t, py - dy * t, pz - dz * t) - (r1 + (r2 - r1) * t);+}+// Surface nets over a sparse grid: only cells near the surface are ever evaluated+function polygonize(F, box, cell, bandK = 1.75) {+ const [x0, y0, z0, x1, y1, z1] = box, nx = Math.ceil((x1 - x0) / cell) + 2, ny = Math.ceil((y1 - y0) / cell) + 2, nz = Math.ceil((z1 - z0) / cell) + 2;+ const N = nx * ny * nz, val = new Float32Array(N), seen = new Uint8Array(N), cellV = new Int32Array(N).fill(-1);+ const b4 = 3.46 * cell * bandK, b2 = 1.73 * cell * bandK, active = [], inA = new Uint8Array(N);+ const activate = (i, j, k) => { const q = i + nx * (j + ny * k); if (!inA[q]) { inA[q] = 1; active.push(i, j, k); } };+ const at = (i, j, k) => { const q = i + nx * (j + ny * k); if (!seen[q]) { seen[q] = 1; val[q] = F(x0 + i * cell, y0 + j * cell, z0 + k * cell); } return val[q]; };+ // Two-level descent: a coarse block, then its octants, then cells, so the field is only asked near its surface+ for (let kc = 0; kc < nz - 1; kc += 4) for (let jc = 0; jc < ny - 1; jc += 4) for (let ic = 0; ic < nx - 1; ic += 4) {+ if (Math.abs(F(x0 + (ic + 2) * cell, y0 + (jc + 2) * cell, z0 + (kc + 2) * cell)) >= b4) continue;+ for (let k2 = kc; k2 < Math.min(kc + 4, nz - 1); k2 += 2) for (let j2 = jc; j2 < Math.min(jc + 4, ny - 1); j2 += 2) for (let i2 = ic; i2 < Math.min(ic + 4, nx - 1); i2 += 2) {+ if (Math.abs(F(x0 + (i2 + 1) * cell, y0 + (j2 + 1) * cell, z0 + (k2 + 1) * cell)) >= b2) continue;+ for (let k = k2; k < Math.min(k2 + 2, nz - 1); k++) for (let j = j2; j < Math.min(j2 + 2, ny - 1); j++) for (let i = i2; i < Math.min(i2 + 2, nx - 1); i++) activate(i, j, k);+ }+ }+ const P = [], I = [], E = [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]], v = new Float64Array(8);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ let mask = 0;+ for (let c = 0; c < 8; c++) { const d = at(i + (c & 1), j + ((c >> 1) & 1), k + ((c >> 2) & 1)); v[c] = d; if (d < 0) mask |= 1 << c; }+ if (mask === 0 || mask === 255) continue;+ // Follow the surface through every face it crosses: a steep pleat can fool the coarse test above, and a skipped cell is a hole+ const crosses = (m) => (mask & m) !== 0 && (mask & m) !== m;+ if (i > 0 && crosses(0x55)) activate(i - 1, j, k);+ if (i < nx - 2 && crosses(0xaa)) activate(i + 1, j, k);+ if (j > 0 && crosses(0x33)) activate(i, j - 1, k);+ if (j < ny - 2 && crosses(0xcc)) activate(i, j + 1, k);+ if (k > 0 && crosses(0x0f)) activate(i, j, k - 1);+ if (k < nz - 2 && crosses(0xf0)) activate(i, j, k + 1);+ let sx = 0, sy = 0, sz = 0, n = 0;+ for (let e = 0; e < 12; e++) {+ const c0 = E[e][0], c1 = E[e][1], d0 = v[c0], d1 = v[c1];+ if ((d0 < 0) === (d1 < 0)) continue;+ const t = d0 / (d0 - d1);+ sx += lerp(c0 & 1, c1 & 1, t); sy += lerp((c0 >> 1) & 1, (c1 >> 1) & 1, t); sz += lerp((c0 >> 2) & 1, (c1 >> 2) & 1, t); n++;+ }+ cellV[i + nx * (j + ny * k)] = P.length / 3;+ P.push(x0 + (i + sx / n) * cell, y0 + (j + sy / n) * cell, z0 + (k + sz / n) * cell);+ }+ const quad = (a, b, c, d, flip) => { if (a < 0 || b < 0 || c < 0 || d < 0) return; if (flip) I.push(a, c, b, a, d, c); else I.push(a, b, c, a, c, d); };+ const cv = (i, j, k) => (i < 0 || j < 0 || k < 0 ? -1 : cellV[i + nx * (j + ny * k)]);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ const s0 = at(i, j, k) < 0;+ if (s0 !== (at(i + 1, j, k) < 0)) quad(cv(i, j - 1, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i, j - 1, k), !s0);+ if (s0 !== (at(i, j + 1, k) < 0)) quad(cv(i - 1, j, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i - 1, j, k), s0);+ if (s0 !== (at(i, j, k + 1) < 0)) quad(cv(i - 1, j - 1, k), cv(i, j - 1, k), cv(i, j, k), cv(i - 1, j, k), !s0);+ }+ return { P, I };+}+function meshToGeo(m) {+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(m.P), 3));+ g.setIndex(new THREE.BufferAttribute(m.P.length / 3 > 65535 ? new Uint32Array(m.I) : new Uint16Array(m.I), 1));+ g.computeVertexNormals();+ return g;+}+const AO_STEPS = [0.016, 0.05, 0.13];+// Extract, simplify, and write into a batch with field normals and field ambient occlusion+async function carveInto(b, F, box, cell, tris, xf, occ, opt = {}) {+ const raw = polygonize(F, box, cell);+ if (!raw.I.length) return;+ if (opt.cull) { // drop a region that a finer pass will carve instead+ const I2 = [], used = new Int32Array(raw.P.length / 3).fill(-1), P2 = [], mapV = (v) => { if (used[v] < 0) { used[v] = P2.length / 3; P2.push(raw.P[v * 3], raw.P[v * 3 + 1], raw.P[v * 3 + 2]); } return used[v]; };+ for (let t = 0; t < raw.I.length; t += 3) {+ const a = raw.I[t], c = raw.I[t + 1], e = raw.I[t + 2], mx = (raw.P[a * 3] + raw.P[c * 3] + raw.P[e * 3]) / 3, my = (raw.P[a * 3 + 1] + raw.P[c * 3 + 1] + raw.P[e * 3 + 1]) / 3, mz = (raw.P[a * 3 + 2] + raw.P[c * 3 + 2] + raw.P[e * 3 + 2]) / 3;+ if (!opt.cull(mx, my, mz)) I2.push(mapV(a), mapV(c), mapV(e));+ }+ raw.P = P2; raw.I = I2;+ }+ let g = meshToGeo(raw);+ if (raw.I.length / 3 > tris) g = await simplifyGeometry(g, { targetTriangles: tris });+ const pos = g.attributes.position, idx = g.index, base = b.count, h = cell * (opt.smooth || 0.6), aoK = opt.ao === undefined ? 0.5 : opt.ao, aoMin = opt.aoMin === undefined ? 0.16 : opt.aoMin;+ b.at(xf || M.I(), () => {+ for (let i = 0; i < pos.count; i++) {+ let x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i);+ const f0 = F(x, y, z), g0 = F(x + h, y, z) - F(x - h, y, z), g1 = F(x, y + h, z) - F(x, y - h, z), g2 = F(x, y, z + h) - F(x, y, z - h), l = len3(g0, g1, g2) || 1;+ const nx = g0 / l, ny = g1 / l, nz = g2 / l;+ // settle the vertex onto the true surface: this is what keeps fold valleys crisp+ x -= nx * f0; y -= ny * f0; z -= nz * f0;+ const O = occ || F;+ const wx = b.m[0] * x + b.m[1] * y + b.m[2] * z + b.m[3], wy = b.m[4] * x + b.m[5] * y + b.m[6] * z + b.m[7], wz = b.m[8] * x + b.m[9] * y + b.m[10] * z + b.m[11];+ const nm = b.nm, ax = nm[0] * nx + nm[1] * ny + nm[2] * nz, ay = nm[3] * nx + nm[4] * ny + nm[5] * nz, az = nm[6] * nx + nm[7] * ny + nm[8] * nz, al = len3(ax, ay, az) || 1;+ let o = 0, w = 1;+ for (let q = 0; q < 3; q++) { const d = AO_STEPS[q]; o += w * Math.max(0, d - O(wx + ax / al * d, wy + ay / al * d, wz + az / al * d)) / d; w *= 0.66; }+ const ao = clamp(1 - o * aoK, aoMin, 1);+ // UV by a slanted planar projection: no seam on a figure, the grain runs up the stone+ b.v(x, y, z, nx, ny, nz, wz * 0.8 + wx * 0.6, wy, ao);+ }+ for (let t = 0; t < idx.count; t += 3) b.tri(base + idx.getX(t), base + idx.getX(t + 1), base + idx.getX(t + 2));+ });+}++// ---- The head: a real skull, brow, eyes with lids, nose, lips, chin, hair, beard -------------+const LID_U = [[0.0795, 0.0035, 0.0165, 0.003], [0.0848, 0.0112, 0.0238, 0.0038], [0.0866, 0.0152, 0.0338, 0.0043], [0.0838, 0.0122, 0.0438, 0.0036], [0.0755, 0.0062, 0.052, 0.0024]];+const LID_L = [[0.0795, 0.0035, 0.0165, 0.0022], [0.0836, -0.0012, 0.0245, 0.0028], [0.0852, -0.0032, 0.034, 0.003], [0.0822, -0.0006, 0.044, 0.0026], [0.0755, 0.0062, 0.052, 0.0022]];+function headField(o) {+ const sm = o.smile || 0, cheek = o.gaunt ? 0.5 : 1, up = sm * 0.0065;+ const locks = [], rng = mkRng(o.seed * 37 + 11);+ // Long locks fall from the temples and the back of the head to the shoulders+ if (o.hair === 'long' || o.hair === 'curled') {+ const nL = o.hair === 'curled' ? 7 : 9;+ for (let i = 0; i < nL; i++) {+ const f = i / (nL - 1), a = lerp(1.05, 2.95, f), r0 = 0.092, wob = 0.012 + rng() * 0.008, dr = o.hair === 'curled' ? -0.165 : -0.235;+ for (const s of [-1, 1]) {+ const p0 = [r0 * Math.cos(a) - 0.012, 0.02 + rng() * 0.02, s * r0 * Math.sin(a) * 0.9];+ const p1 = [p0[0] * 1.14 - 0.008, p0[1] + dr * 0.5, p0[2] * 1.18 + s * wob];+ const p2 = [p0[0] * 1.02 - 0.03 * (1 - f * 0.5), p0[1] + dr, p0[2] * (1.28 - f * 0.3) - s * wob];+ locks.push([p0, p1, 0.02, 0.016], [p1, p2, 0.016, o.hair === 'curled' ? 0.012 : 0.007]);+ }+ }+ }+ if (o.hair === 'curls') { // rolled waves that frame the face: the smiling angel, the Child+ for (let i = 0; i < 11; i++) { const f = i / 10 - 0.5, a = f * 2.5, p0 = [0.104 * Math.cos(a) * 0.72 + 0.006, 0.094 - f * f * 0.11, 0.088 * Math.sin(a)], p1 = [p0[0] + 0.012, p0[1] - 0.03 - Math.abs(f) * 0.03, p0[2] * 1.16]; locks.push([p0, p1, 0.0165, 0.0115]); }+ for (let i = 0; i < 12; i++) { const a = 1.5 + i * 0.29, s = i % 2 ? 1 : -1, p0 = [0.09 * Math.cos(a) - 0.012, 0.0, s * 0.09 * Math.sin(a)], p1 = [p0[0] * 1.1, -0.062, p0[2] * 1.14]; locks.push([p0, p1, 0.019, 0.013]); }+ }+ const beardL = [];+ if (o.beard) {+ const nB = 7, L = o.beard === 2 ? 0.17 : 0.09;+ for (let i = 0; i < nB; i++) {+ const f = i / (nB - 1) - 0.5, z0 = f * 0.098, x0 = 0.078 - Math.abs(f) * 0.05, wob = (i % 2 ? 1 : -1) * 0.005;+ // separate locks with grooves between them; each one twists and tapers to a point+ beardL.push([[x0, -0.07 - Math.abs(f) * 0.012, z0], [x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], 0.0102, 0.0098]);+ beardL.push([[x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], [x0 - 0.006 + (o.beard === 2 ? 0.008 : 0), -0.07 - L - Math.abs(f) * 0.012, z0 * 0.5], 0.0098, 0.0035]);+ }+ }+ // Each lock is skipped outright when the query point is beyond its bounding sphere+ const sphere = (L) => { const dx = L[0][0] - L[1][0], dy = L[0][1] - L[1][1], dz = L[0][2] - L[1][2], R = Math.sqrt(dx * dx + dy * dy + dz * dz) / 2 + Math.max(L[2], L[3]) + 0.03; return [(L[0][0] + L[1][0]) / 2, (L[0][1] + L[1][1]) / 2, (L[0][2] + L[1][2]) / 2, R * R]; };+ const lockB = locks.map(sphere), beardB = beardL.map(sphere);+ const nearS = (S, x, y, z) => { const a = x - S[0], b = y - S[1], c = z - S[2]; return a * a + b * b + c * c < S[3]; };+ return (x, y, z) => {+ const az = Math.abs(z);+ let d = sdEll(x + 0.012, y - 0.03, z, 0.098, 0.098, 0.08); // cranium+ d = smin(d, sdEll(x - 0.018, y + 0.022, z, 0.074, 0.094, 0.0655), 0.02); // face mass+ d = smin(d, sdEll(x - 0.03, y + 0.076, z, 0.056, 0.046, 0.05 * (o.gaunt ? 0.93 : 1)), 0.018); // jaw+ d = smin(d, sdEll(x - 0.0675, y + 0.1, z, 0.019, 0.0175, 0.02), 0.012); // chin+ d = smin(d, sdCone(x, y, az, 0.0745, 0.031, 0, 0.066, 0.033, 0.05, 0.0118, 0.0088), 0.012); // brow ridge+ d = smin(d, sdEll(x - 0.054, y + 0.018 - up, az - 0.044, 0.026, 0.017 + sm * 0.003, 0.02), 0.015 * cheek); // cheekbone+ if (o.gaunt) d = smax(d, -sdEll(x - 0.05, y + 0.058, az - 0.058, 0.03, 0.022, 0.012), 0.012);+ if (x > 0.04 && y > -0.03 && y < 0.05 && az < 0.082) {+ d = smax(d, -sdEll(x - 0.0875, y - 0.0075, az - 0.034, 0.0225, 0.0118, 0.0215), 0.008); // eye socket+ d = smin(d, len3(x - 0.0655, y - 0.0065, az - 0.034) - 0.0178, 0.0018); // eyeball+ d = smax(d, -(len3(x - 0.0862, y - 0.0065, az - 0.034) - 0.0052), 0.0016); // the drilled pupil+ // lids: a carved almond, the upper rim heavier, meeting at the corners+ for (let i = 0; i < 4; i++) { const a = LID_U[i], c = LID_U[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0026); }+ for (let i = 0; i < 4; i++) { const a = LID_L[i], c = LID_L[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0022); }+ }+ if (x > 0.055 && y > -0.105 && y < 0.045 && az < 0.055) {+ // nose: bridge to tip, wings, nostrils+ d = smin(d, sdCone(x, y, z, 0.0835, 0.0225, 0, 0.105, -0.021, 0, 0.0058, 0.0092), 0.0085);+ d = smin(d, len3(x - 0.0925, y + 0.0295, az - 0.0108) - 0.0064, 0.005);+ d = smax(d, -(len3(x - 0.0975, y + 0.0355, az - 0.0062) - 0.0033), 0.002);+ // philtrum+ d = smax(d, -sdCone(x, y, z, 0.0975, -0.042, 0, 0.0965, -0.052, 0, 0.0028, 0.0034), 0.0035);+ // lips: upper in two wings, a fuller lower, a cut between; corners lift for a smile+ const my = -0.0615;+ d = smin(d, sdCone(x, y, az, 0.0925, my + 0.004, 0, 0.0815, my + 0.001 + up, 0.0215 + sm * 0.003, 0.0058, 0.0026), 0.005);+ d = smin(d, sdCone(x, y, az, 0.0915, my - 0.0085, 0, 0.0825, my - 0.003 + up, 0.0185 + sm * 0.003, 0.0072, 0.0028), 0.005);+ d = smax(d, -sdCone(x, y, az, 0.0965, my - 0.002, 0, 0.0825, my + up * 1.15, 0.0235 + sm * 0.004, 0.0017, 0.0021), 0.0022);+ d = smax(d, -sdEll(x - 0.0915, y + 0.0845, z, 0.012, 0.0055, 0.02), 0.006); // hollow under the lip+ }+ if (sm) d = smin(d, sdEll(x - 0.07, y + 0.044 - up * 2, az - 0.038, 0.02, 0.015, 0.016), 0.012); // the raised cheek of a smile+ // ear+ d = smin(d, sdEll(x + 0.012, y - 0.002, az - 0.08, 0.016, 0.03, 0.0085), 0.004);+ // neck, into the shoulders+ d = smin(d, sdCone(x, y, z, -0.01, -0.075, 0, -0.022, -0.215, 0, 0.041, 0.052), 0.018);+ // ---- hair ----+ if (o.hair && o.hair !== 'none') {+ const faceOpen = sdEll(x - 0.094, y + 0.012, z, 0.08, o.hair === 'curls' ? 0.106 : 0.118, 0.073);+ let h = sdEll(x + 0.014, y - 0.034, z, 0.104, 0.104, 0.09);+ if (o.hair === 'tonsure') h = smax(h, -sdEll(x + 0.012, y - 0.135, z, 0.075, 0.045, 0.062), 0.006);+ const an = Math.atan2(z, x + 0.014);+ if (o.hair !== 'curls') {+ const part = Math.exp(-(z * z) / 0.00007) * sstep(0.03, 0.11, y);+ h += 0.0028 * Math.sin(an * 22 + y * 34) + 0.007 * part;+ } else {+ h += 0.006 * Math.sin(an * 11 + y * 22) * Math.sin(y * 27 + an * 3);+ }+ for (let i = 0; i < locks.length; i++) { if (!nearS(lockB[i], x, y, z)) continue; const L = locks[i]; h = smin(h, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0055); }+ h = smax(h, -faceOpen, 0.0055);+ d = smin(d, h, 0.0025);+ }+ // ---- beard and moustache ----+ if (o.beard) {+ for (let i = 0; i < beardL.length; i++) { if (!nearS(beardB[i], x, y, z)) continue; const L = beardL[i]; d = smin(d, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0022); }+ d = smin(d, sdCone(x, y, az, 0.0955, -0.0475, 0.002, 0.081, -0.0625, 0.031, 0.0046, 0.003), 0.0025);+ d = smin(d, sdEll(x - 0.05, y + 0.058, az - 0.058, 0.022, 0.03, 0.012), 0.008); // sideburn into the jaw+ }+ // ---- veil: falls from the crown to the shoulders, cut open round the face ----+ if (o.veil) {+ // A soft hood: close to the skull, widening over the shoulders, its hem falling in loose folds+ const an = Math.atan2(z, x + 0.02);+ let w = smin(sdEll(x + 0.012, y - 0.032, z, 0.11, 0.108, 0.094), sdCone(x, y, z, -0.022, -0.05, 0, -0.038, -0.3, 0, 0.09, 0.158), 0.05);+ w -= 0.012 * (Math.sqrt(Math.abs(Math.sin(an * 4.5 + y * 2.2))) - 0.55) * sstep(-0.01, -0.16, y);+ w = smax(w, -0.325 + 0.022 * Math.sin(an * 5) - y, 0.008);+ const open = sdEll(x - 0.104, y + 0.016, z, 0.1, 0.116, 0.0715);+ d = smin(d, smax(w, -open, 0.004), 0.0015);+ }+ // ---- a queen's barbette: a linen band from the crown under the chin, framing a long grave face ----+ if (o.wimple) {+ const e = Math.sqrt(((y + 0.012) * (y + 0.012)) / (0.112 * 0.112) + (z * z) / (0.08 * 0.08)) - 1;+ d = smin(d, Math.max(Math.abs(e) * 0.09 - 0.0075, Math.abs(x - 0.018) - 0.036), 0.002);+ }+ // ---- mitre: two high peaks of stiff cloth, the bands left proud ----+ if (o.mitre) {+ // seen from the front a pointed arch, wide at the brow; seen from the side, two thin peaks+ const px = x + 0.012, py = y - 0.058, f = clamp(py / 0.27), w0 = 0.098 * (1 - Math.pow(f, 1.9)), dp = 0.102 * (1 - f * 0.86), lens = Math.abs(px) + az * 0.2;+ let m = Math.max(lens - dp, az - w0, -py);+ m = Math.max(m, py - 0.272);+ const band = Math.exp(-((py - 0.018) * (py - 0.018)) / 0.00016);+ m -= 0.0045 * band + 0.0038 * Math.exp(-(z * z) / 0.00012);+ d = smin(d, m, 0.004);+ }+ return d * 0.92;+ };+}++// ---- The hand: palm and jointed fingers that actually close on what they hold -------------+const HAND_POSES = {+ // [curl1, curl2, curl3] per finger (index..little), thumb [x,y,z of tip, spread]+ grip: { f: [[68, 88, 62], [72, 92, 64], [76, 92, 64], [80, 88, 62]], t: 'over' },+ support: { f: [[10, 18, 12], [12, 20, 14], [14, 22, 14], [18, 24, 16]], t: 'open' },+ edge: { f: [[46, 40, 26], [48, 42, 26], [50, 44, 28], [54, 44, 28]], t: 'open' },+ bless: { f: [[6, 8, 6], [8, 10, 8], [62, 90, 60], [68, 92, 60]], t: 'over' },+ rest: { f: [[28, 40, 26], [32, 42, 28], [36, 44, 28], [40, 46, 28]], t: 'open' },+};+function handField(pose, thumb = 1) {+ const P0 = HAND_POSES[pose], kn = [[0.078, 0.003, -0.027], [0.083, 0.004, -0.009], [0.08, 0.003, 0.009], [0.073, 0.001, 0.026]], segL = [[0.036, 0.024, 0.019], [0.04, 0.027, 0.02], [0.037, 0.025, 0.019], [0.029, 0.019, 0.016]], segs = [];+ for (let f = 0; f < 4; f++) {+ let ang = 0, p = kn[f].slice();+ for (let s = 0; s < 3; s++) {+ ang += rad(P0.f[f][s]);+ const q = [p[0] + Math.cos(ang) * segL[f][s], p[1] - Math.sin(ang) * segL[f][s], p[2]];+ segs.push([p, q, s === 0 ? 0.0098 : s === 1 ? 0.0088 : 0.0078, s === 2 ? 0.0066 : s === 0 ? 0.0088 : 0.0078]);+ p = q;+ }+ }+ const tb = [0.022, -0.006, thumb * 0.03], tm = P0.t === 'over' ? [0.052, -0.03, thumb * 0.033] : [0.05, -0.012, thumb * 0.054], tt = P0.t === 'over' ? [0.078, -0.044, thumb * 0.012] : [0.082, -0.016, thumb * 0.056];+ return (x, y, z) => {+ let d = Math.max(Math.abs(x - 0.04) - 0.043, Math.abs(y) - 0.0125, Math.abs(z) - 0.036) - 0.006; // palm, gently rounded+ d = smin(d, sdCone(x, y, z, -0.02, 0, 0, 0.02, 0, 0, 0.026, 0.033), 0.012); // wrist heel+ for (let i = 0; i < segs.length; i++) { const S = segs[i]; d = smin(d, sdCone(x, y, z, S[0][0], S[0][1], S[0][2], S[1][0], S[1][1], S[1][2], S[2], S[3]), 0.0022); }+ d = smin(d, sdCone(x, y, z, tb[0], tb[1], tb[2], tm[0], tm[1], tm[2], 0.0145, 0.0098), 0.006);+ d = smin(d, sdCone(x, y, z, tm[0], tm[1], tm[2], tt[0], tt[1], tt[2], 0.0098, 0.0078), 0.002);+ return d;+ };+}+// Heads are shared meshes placed on their own node: the same face can turn and tilt per figure at no byte cost.+const HEAD_MESH = {};+const HEADS = {+ bearded: { hair: 'long', beard: 1, seed: 2 },+ mitred: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 },+ royal: { hair: 'curled', beard: 2, seed: 10 },+ veiled: { veil: 1, smile: 0.3, seed: 8 },+ queen: { veil: 1, gaunt: 1, wimple: 1, seed: 5 },+ angelic: { hair: 'curls', smile: 1, seed: 13 },+};+async function headBatch(key) {+ if (HEAD_MESH[key]) return HEAD_MESH[key];+ const hO = HEADS[key], b = new Batch(TILE.fine), hF = headField(hO);+ const hB = [-0.19, hO.veil ? -0.36 : -0.26, -0.2, 0.17, hO.mitre ? 0.36 : 0.16, 0.2];+ // Hair and skull at a working grid, then the face alone on the finest grid with most of the triangles+ const FB = [0.024, -0.13, -0.08, 0.128, 0.088, 0.08];+ const inFace = (x, y, z) => x > FB[0] + 0.003 && y > FB[1] + 0.003 && y < FB[4] - 0.003 && z > FB[2] + 0.003 && z < FB[5] - 0.003;+ await carveInto(b, hF, hB, 0.0048, hO.veil ? 1300 : hO.beard ? 1900 : 1700, null, hF, { smooth: 1.4, ao: 0.34, aoMin: 0.3, cull: inFace });+ await carveInto(b, hF, FB, 0.0024, hO.beard ? 2500 : 2700, null, hF, { smooth: 0.9, ao: 0.3, aoMin: 0.34 });+ b.isFigure = true;+ HEAD_MESH[key] = b;+ return b;+}+const HAND_MESH = {};+async function handBatch(pose, thumb) { // hands are cached: a grip is a grip, whoever's it is+ const key = pose + thumb;+ if (!HAND_MESH[key]) { const b = new Batch(TILE.fine), F = handField(pose, thumb); await carveInto(b, F, [-0.05, -0.1, -0.1, 0.16, 0.05, 0.1], 0.0034, 520); HAND_MESH[key] = b; }+ return HAND_MESH[key];+}+function copyBatch(dst, src, m, shadeFn) { // transform a finished batch into another+ const base = dst.count;+ dst.at(m, () => {+ for (let i = 0; i < src.count; i++) { const i3 = i * 3, i2 = i * 2; dst.v(src.P[i3], src.P[i3 + 1], src.P[i3 + 2], src.N[i3], src.N[i3 + 1], src.N[i3 + 2], src.UV[i2] * src.tile, src.UV[i2 + 1] * src.tile, (src.C[i3] / 255) * (shadeFn ? shadeFn(src.P[i3], src.P[i3 + 1], src.P[i3 + 2]) : 1)); }+ for (let t = 0; t < src.I.length; t += 3) dst.tri(base + src.I[t], base + src.I[t + 1], base + src.I[t + 2]);+ });+}+const frameOf = (p, X, Y) => { // rigid frame: origin p, local X and Y given, Z = X x Y+ const lx = len3(X[0], X[1], X[2]), xn = [X[0] / lx, X[1] / lx, X[2] / lx], d = Y[0] * xn[0] + Y[1] * xn[1] + Y[2] * xn[2];+ let yy = [Y[0] - d * xn[0], Y[1] - d * xn[1], Y[2] - d * xn[2]]; const ly = len3(yy[0], yy[1], yy[2]); yy = yy.map((c) => c / ly);+ const z = [xn[1] * yy[2] - xn[2] * yy[1], xn[2] * yy[0] - xn[0] * yy[2], xn[0] * yy[1] - xn[1] * yy[0]];+ return [xn[0], yy[0], z[0], p[0], xn[1], yy[1], z[1], p[1], xn[2], yy[2], z[2], p[2]];+};+const invRigid = (m) => (x, y, z) => { const a = x - m[3], b = y - m[7], c = z - m[11]; return [m[0] * a + m[4] * b + m[8] * c, m[1] * a + m[5] * b + m[9] * c, m[2] * a + m[6] * b + m[10] * c]; };++// ---- The people. Bodies are long columns of cloth; each differs in dress, stance, head, gesture ----+const FIGURES = {+ apostle: { seed: 2, sway: 0.018, dress: 'mantle', headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 2 }, hold: 'key' },+ bishop: { seed: 4, sway: 0.008, dress: 'chasuble', headKey: 'mitred', head: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 }, hold: 'crozier' },+ king: { seed: 10, sway: 0.022, dress: 'royal', headKey: 'royal', head: { hair: 'curled', beard: 2, crown: 1, seed: 10 }, hold: 'sceptre' },+ queen: { seed: 5, sway: 0.03, dress: 'royal', slim: 0.93, headKey: 'queen', head: { veil: 1, gaunt: 1, wimple: 1, crown: 1, seed: 5, turn: 8 }, hold: 'mantle' },+ angel: { seed: 13, sway: 0.05, dress: 'angel', slim: 0.94, wings: 1, headKey: 'angelic', head: { hair: 'curls', smile: 1, seed: 13, tilt: 11, turn: 15 }, hold: 'scroll' },+ virgin: { seed: 8, sway: 0.062, dress: 'mantle', slim: 0.95, headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, tilt: 9, turn: -16 }, hold: 'child' },+};+const FIG_ALIAS = { peter: 'apostle', paul: 'apostle', prophet: 'apostle', deacon: 'apostle', martyr: 'queen', kingA: 'king', kingB: 'king', kingC: 'king', christ: 'seated' };++function bodyField(o) {+ const yN = 1.7, rng = mkRng(o.seed * 991 + 7), ph = []; for (let i = 0; i < 8; i++) ph.push(rng() * TAU);+ const sw = o.sway || 0, sl = o.slim || 1, L = 256;+ const keys = [[0, 0.178, 0.205, 0.218], [0.05, 0.165, 0.192, 0.204], [0.3, 0.138, 0.165, 0.172], [0.52, 0.124, 0.15, 0.158], [0.66, 0.112, 0.128, 0.146], [0.8, 0.116, 0.12, 0.15], [0.885, 0.108, 0.112, 0.15], [0.94, 0.09, 0.094, 0.13], [0.975, 0.066, 0.07, 0.09], [1, 0.044, 0.046, 0.05]];+ const Af = new Float32Array(L), Ab = new Float32Array(L), Bs = new Float32Array(L), Cx = new Float32Array(L), Cz = new Float32Array(L), Amp = new Float32Array(L);+ for (let i = 0; i < L; i++) {+ const t = i / (L - 1), k = keyf(keys, t);+ Af[i] = k[0]; Ab[i] = k[1]; Bs[i] = k[2] * sl;+ Cx[i] = 0.014 * Math.sin(t * PI);+ Cz[i] = sw * (Math.sin(t * PI * 0.92) * 1.15 - 0.62 * Math.sin(t * PI * 2));+ // pleats deepest over the shins, gathered at the belt, slight in the bodice+ Amp[i] = t < 0.645 ? lerp(0.05, 0.03, sstep(0, 0.6, t)) : lerp(0.014, 0.004, sstep(0.66, 0.9, t));+ }+ const n1 = o.dress === 'chasuble' ? 16 : 14, n2 = 25, yB = 0.655 * yN;+ const dress = o.dress, zc = Cz[Math.round(0.86 * (L - 1))], cxS = Cx[Math.round(0.86 * (L - 1))];+ const Rs = 0.142 * sl;+ const arms = armPose(o, Rs, zc, cxS);+ // Feet: pointed medieval shoes emerge from the hem+ const foot = (x, y, z) => { const az = Math.abs(z - Cz[0]); let d = sdCone(x, y, az, 0.02, 0.042, 0.082, 0.2, 0.024, 0.098, 0.05, 0.024); d = smin(d, sdCone(x, y, az, 0.0, 0.05, 0.082, 0.02, 0.2, 0.07, 0.044, 0.04), 0.02); d = smax(d, -sdEll(x - 0.11, y + 0.01, az - 0.09, 0.08, 0.02, 0.1), 0.004); return Math.max(d, -y); };+ return {+ arms,+ F: (x, y, z) => {+ const t = clamp(y / yN), i = (t * (L - 1)) | 0, px = x - Cx[i], pz = z - Cz[i], rho = Math.sqrt(px * px + pz * pz) + 1e-6, c = px / rho, s = pz / rho, a = c > 0 ? Af[i] : Ab[i], b = Bs[i];+ const R = 1 / Math.sqrt((c * c) / (a * a) + (s * s) / (b * b)), th = Math.atan2(pz, px);+ // ---- the tunic: tube pleats with valleys you could put a finger in ----+ const u1 = th * n1 * 0.5 + ph[0] + y * 0.5 * Math.sign(sw || 1), u2 = th * n2 * 0.5 + ph[1] - y * 0.9;+ const p1 = Math.sqrt(Math.abs(Math.sin(u1))), p2 = Math.sqrt(Math.abs(Math.sin(u2)));+ let r = R + Amp[i] * (p1 * 0.76 + p2 * 0.24 - 0.64);+ r += sstep(0.14, 0, y) * (0.018 + 0.016 * Math.sin(th * 7 + ph[4])); // the skirt spreads and breaks at the foot+ r += 0.008 * Math.exp(-((y - yB) * (y - yB)) / 0.00018); // belt+ r -= 0.012 * sstep(0.03, 0, Math.abs(y - yB) - 0.012) * 0.2;+ r += 0.006 * Math.exp(-((y - yN + 0.075) * (y - yN + 0.075)) / 0.00012); // collar+ let d = rho - r;+ const hem = 0.055 + 0.038 * p1 * (c > -0.2 ? 1 : 0.4);+ d = smax(d, hem - y, 0.006);+ // ---- the over-garment ----+ if (dress === 'mantle') {+ const up = 0.78 * yN + 0.2 * yN * Math.max(0, Math.sin(-th * Math.sign(sw || 1) + 0.5)) + 0.25 * sstep(1.2, 2.6, Math.abs(th)), low = 0.33 * yN - 0.18 * yN * sstep(0.5, 2.8, Math.abs(th)) + 0.02 * Math.sin(th * 11 + ph[5]) + 0.012 * Math.sin(th * 23);+ const reg = Math.max(low - y, y - up);+ // Folds pulled on the diagonal from the held hip to the far shoulder: two scales, never in step+ const th0 = 0.25 * Math.sign(sw || 1), q = y + 0.15 * th * Math.sign(sw || 1) + 0.055 * (th - th0) * (th - th0);+ const swag = 0.62 * Math.sqrt(Math.abs(Math.sin(q * 12.5 + ph[6]))) + 0.38 * Math.sqrt(Math.abs(Math.sin(q * 27 + ph[7]))), back = Math.sqrt(Math.abs(Math.sin(th * 5 + ph[3])));+ const fr = sstep(1.9, 1.1, Math.abs(th));+ const rm = R + 0.03 + 0.04 * lerp(back - 0.6, swag - 0.58, fr) * sstep(up + 0.01, up - 0.08, y);+ d = smin(d, smax(rho - rm, reg, 0.007), 0.002);+ } else if (dress === 'chasuble') {+ // alb to the feet, dalmatic to the shin, the bell of the chasuble hanging from the arms in U folds+ const dal = Math.max(0.3 * yN + 0.012 * Math.sin(th * 9) - y, y - 0.86 * yN);+ d = smin(d, smax(rho - (R + 0.016 + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 6 + ph[2]))) - 0.5)), dal, 0.005), 0.0015);+ const lowC = (0.42 + 0.26 * sstep(0.2, 1.5, Math.abs(th)) - 0.2 * sstep(1.7, 3.0, Math.abs(th))) * yN + 0.012 * Math.sin(th * 13);+ const swag = 0.65 * Math.sqrt(Math.abs(Math.sin((y + 0.2 * th * th) * 27 + ph[6]))) + 0.35 * Math.sqrt(Math.abs(Math.sin((y + 0.13 * th * th) * 44 + ph[7])));+ const rc = R + 0.03 * sstep(yN, 0.9 * yN, y) + 0.022 * (swag - 0.58) * sstep(0.84 * yN, 0.74 * yN, y) * (1 - 0.6 * sstep(0, 0.2, 0.2 - Math.abs(th)));+ d = smin(d, smax(rho - rc, lowC - y, 0.007), 0.003);+ // the orphrey: a Y-band across the breast and down the front, left proud+ const py = y - 0.79 * yN;+ if (Math.abs(th) < 1.2 && y > lowC - 0.01 && y < 0.94 * yN) d = smin(d, Math.max(rho - rc - 0.011, py > 0 ? Math.abs(Math.abs(th) - py * 4.2) - 0.085 : Math.abs(th) - 0.09), 0.003);+ } else if (dress === 'royal') {+ // a heavy mantle open down the front, thrown back over the shoulders, deep folds to the floor+ const open = lerp(0.72, 0.28, sstep(0.05, 0.86, t)) + 0.05 * Math.sin(y * 19 + ph[2]);+ const reg = Math.max(open - Math.abs(th - 0.05), 0.05 - y);+ const hv = Math.sqrt(Math.abs(Math.sin(th * 4 + ph[3] + y * 0.3)));+ const collar = 0.022 * Math.exp(-((y - 0.915 * yN) * (y - 0.915 * yN)) / 0.0012); // a soft roll at the neck+ const rm = R + 0.03 * sstep(yN, 0.9 * yN, y) + collar + sstep(0.84 * yN, 0.5 * yN, y) * 0.056 * (hv - 0.62);+ d = smin(d, smax(rho - rm, reg, 0.006), 0.004);+ } else if (dress === 'angel') {+ // a girdled tunic blown a little to one side: fine folds and a bloused top+ const blouse = 0.018 * Math.exp(-((y - yB - 0.06) * (y - yB - 0.06)) / 0.003);+ d -= blouse;+ const low = 0.74 * yN + 0.02 * Math.sin(th * 7) - y;+ d = smin(d, smax(rho - (R + 0.016 * sstep(yN, 0.9 * yN, y) + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 7 + ph[2]))) - 0.5) * sstep(0.9 * yN, 0.8 * yN, y)), low, 0.005), 0.003);+ }+ // ---- shoulders and sleeves ----+ const yS = yN - 0.155;+ for (let k = 0; k < 2; k++) { const sg = k ? 1 : -1; d = smin(d, sdEll(x - cxS, y - yS, z - zc - sg * Rs * 0.98, 0.07, 0.078, 0.058), 0.05); }+ for (let k = 0; k < arms.length; k++) {+ const A = arms[k];+ let ad = sdCone(x, y, z, A.S[0], A.S[1], A.S[2], A.E[0], A.E[1], A.E[2], 0.056, 0.05);+ // forearm in a sleeve that widens to the cuff, with a roll of cloth at the wrist+ const fx = A.W[0] - A.E[0], fy = A.W[1] - A.E[1], fz = A.W[2] - A.E[2], fl = fx * fx + fy * fy + fz * fz, ft = clamp(((x - A.E[0]) * fx + (y - A.E[1]) * fy + (z - A.E[2]) * fz) / fl);+ const qx = x - A.E[0] - fx * ft, qy = y - A.E[1] - fy * ft, qz = z - A.E[2] - fz * ft, ql = len3(qx, qy, qz);+ const rr = lerp(0.05, 0.062, ft) + 0.006 * Math.sin(ft * 15 + k) * (1 - ft) + 0.008 * Math.exp(-((ft - 0.97) * (ft - 0.97)) / 0.002);+ ad = smin(ad, ql - rr, 0.018);+ // the cloth that falls from under the forearm: a hanging curtain of pleats+ // longest under the wrist, thinning as it falls, its lower edge never straight+ if (A.drape) {+ const h = A.E[1] + fy * ft - y;+ if (h > -0.04 && h < A.drape * 1.1) {+ const fall = clamp(h / A.drape), crv = lerp(0.044, 0.016, fall) + 0.014 * (Math.sqrt(Math.abs(Math.sin(ft * 12 + h * 2.5 + k))) - 0.5);+ const curtain = len3(qx * 0.72, 0, qz) - crv;+ const edge = h - A.drape * (0.28 + 0.72 * ft) - 0.022 * Math.sin(ft * 11 + k * 2);+ ad = smin(ad, smax(curtain, edge, 0.012), 0.018);+ }+ }+ d = smin(d, ad, 0.014);+ }+ // ---- feet ----+ d = smin(d, foot(x, y, z), 0.006);+ return Math.max(d, y - yN - 0.03);+ },+ };+}+// Where the arms go, and what the hands do when they get there+function armPose(o, Rs, zc, cx) {+ const y = 1.55, shR = [cx, y, zc + Rs], shL = [cx, y, zc - Rs], arm = (S, E, W, drape) => ({ S, E, W, drape });+ switch (o.hold) {+ case 'key': return [arm(shR, [0.03, 1.2, zc + Rs + 0.05], [0.17, 1.32, zc + 0.12], 0.32), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.17, 1.14, zc - 0.07], 0.34)];+ case 'crozier': return [arm(shR, [0.04, 1.2, zc + Rs + 0.06], [0.2, 1.3, zc + 0.14], 0.3), arm(shL, [0.05, 1.22, zc - Rs - 0.03], [0.2, 1.42, zc - 0.05], 0.3)];+ case 'sceptre': return [arm(shR, [0.04, 1.18, zc + Rs + 0.06], [0.2, 1.28, zc + 0.15], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.03], [0.17, 1.18, zc - 0.05], 0.3)];+ case 'mantle': return [arm(shR, [0.04, 1.14, zc + Rs + 0.05], [0.18, 1.2, zc + 0.04], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.15, 1.4, zc - 0.1], 0.26)];+ case 'scroll': return [arm(shR, [0.05, 1.2, zc + Rs + 0.04], [0.2, 1.34, zc + 0.1], 0.28), arm(shL, [0.05, 1.2, zc - Rs - 0.04], [0.2, 1.22, zc - 0.08], 0.28)];+ case 'child': return [arm(shR, [0.05, 1.14, zc + Rs + 0.04], [0.2, 1.24, zc + 0.03], 0.32), arm(shL, [0.05, 1.14, zc - Rs - 0.02], [0.2, 1.18, zc - 0.13], 0.3)];+ case 'bless': return [arm(shR, [0.07, 1.22, zc + Rs + 0.09], [0.19, 1.5, zc + 0.15], 0.3), arm(shL, [0.03, 1.2, zc - Rs - 0.04], [0.17, 1.2, zc - 0.07], 0.34)];+ default: return [arm(shR, [0.02, 1.12, zc + Rs + 0.04], [0.15, 1.05, zc + 0.08], 0.3), arm(shL, [0.02, 1.12, zc - Rs - 0.04], [0.15, 1.05, zc - 0.08], 0.3)];+ }+}+// ---- Things held: real objects, modelled at the scale of the hands that grip them ---------+function addBook(b, m, o = {}) {+ b.at(m, () => {+ const w = o.w || 0.082, h = o.h || 0.112, th = o.th || 0.027;+ b.box(-h, h, -th, -th + 0.009, -w, w, { s: 0.85 }); b.box(-h, h, th - 0.009, th, -w, w, { s: 1 }); // boards+ b.box(-h + 0.006, h - 0.006, -th + 0.009, th - 0.009, -w + 0.006, w - 0.004, { s: 0.62 }); // pages+ b.box(-h, h, -th, th, -w - 0.009, -w, { s: 0.8 }); // spine+ for (const i of [-1, 1]) for (const k of [-1, 1]) b.at(M.T(i * (h - 0.026), th, k * (w - 0.026)), () => b.lathe([[0.016, 0], [0.012, 0.007], [0.0, 0.009]], 6));+ b.at(M.T(0, th, 0), () => b.lathe([[0.03, 0], [0.024, 0.006], [0.0, 0.009]], 8));+ });+}+function addAttrs(fig, o, arms) {+ const { stone: b, gold: g } = fig, R = arms[0], Lf = arms[1];+ switch (o.hold) {+ case 'key': {+ // The great key of Peter held upright in the fist, the book against the left forearm+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01];+ b.tube([[hp[0], hp[1] - 0.15, hp[2]], [hp[0], hp[1] + 0.2, hp[2]]], 0.0125, 8, { capStart: true });+ b.at(M.T(hp[0], hp[1] + 0.245, hp[2]), () => b.at(M.Rx(90), () => { b.lathe([[0.04, -0.01], [0.05, 0], [0.04, 0.01], [0.026, 0.01], [0.02, 0], [0.026, -0.01], [0.04, -0.01]], 14); }));+ b.box(hp[0] - 0.01, hp[0] + 0.01, hp[1] - 0.15, hp[1] - 0.075, hp[2], hp[2] + 0.07, { s: 0.9 });+ b.box(hp[0] - 0.012, hp[0] + 0.012, hp[1] - 0.112, hp[1] - 0.1, hp[2] + 0.03, hp[2] + 0.048, { s: 0.3 });+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2] + 0.0), M.Rz(-64), M.Ry(8)));+ break;+ }+ case 'crozier': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 2.02;+ b.tube([[hp[0], 0.04, hp[2]], [hp[0], top, hp[2]]], 0.0145, 8, { capStart: true });+ for (const yK of [hp[1] + 0.22, top - 0.05]) b.at(M.T(hp[0], yK, hp[2]), () => b.lathe([[0.016, -0.03], [0.034, 0], [0.016, 0.03]], 8));+ const vol = []; for (let i = 0; i <= 20; i++) { const t = i / 20, a = -PI * 0.5 + t * PI * 2.5, r = lerp(0.115, 0.036, t); vol.push([hp[0] + r * Math.cos(a) * 1.0 + 0.0, top + 0.12 + r * Math.sin(a), hp[2]]); }+ g.tube(vol, (t) => lerp(0.02, 0.012, t), 7, { capStart: true });+ for (let i = 2; i < 16; i += 3) { const p = vol[i]; addCrocket(g, M.of(M.T(p[0], p[1], p[2]), M.Rz((i / 20) * 400 - 90)), 0.06, 0); }+ break;+ }+ case 'sceptre': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 1.95;+ b.tube([[hp[0], hp[1] - 0.2, hp[2]], [hp[0], top, hp[2]]], 0.0125, 8, { capStart: true });+ g.at(M.T(hp[0], top, hp[2]), () => {+ g.lathe([[0.0125, 0], [0.03, 0.022], [0.038, 0.05], [0.026, 0.078], [0.01, 0.086]], 10);+ for (let i = 0; i < 3; i++) { const a = i * 120 + 90; g.at(M.Ry(a), () => g.tube(spline([[0, 0.07, 0], [0.038, 0.1, 0], [0.042, 0.15, 0], [0.018, 0.17, 0]], 2), (t) => [lerp(0.016, 0.003, t), lerp(0.024, 0.004, t)], 6)); }+ });+ break;+ }+ case 'scroll': {+ const p = [Lf.W[0] + 0.07, Lf.W[1], Lf.W[2]];+ const path = []; for (let i = 0; i <= 14; i++) { const t = i / 14; path.push([R.W[0] + 0.09 + 0.02 * Math.sin(t * 5), R.W[1] - t * 0.7 + 0.03, lerp(R.W[2] - 0.03, Lf.W[2] + 0.05, sstep(0, 0.5, t)) + 0.02 * Math.sin(t * 7)]); }+ const sheet = (off, flip) => b.grid((u, v) => { const k = u * 14, i = Math.min(13, Math.floor(k)), f = k - i, q = path[i], w = path[i + 1]; return [lerp(q[0], w[0], f) + off, lerp(q[1], w[1], f), lerp(q[2], w[2], f) + (v - 0.5) * 0.15, 0.96]; }, 14, 1, { flip });+ sheet(0, false); sheet(-0.004, true);+ b.at(M.of(M.T(path[14][0], path[14][1] - 0.015, path[14][2]), M.Rx(90)), () => b.lathe([[0.022, -0.085], [0.024, -0.08], [0.024, 0.08], [0.022, 0.085]], 8));+ break;+ }+ case 'bless': {+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2]), M.Rz(-64), M.Ry(4)));+ break;+ }+ default: break;+ }+}+const CROWN_FLEURS = 8;+function addCrown(g, m, R0, tall = 1) {+ g.at(m, () => {+ g.lathe([[R0, 0], [R0 + 0.009, 0.004], [R0 + 0.009, 0.028], [R0 + 0.015, 0.038], [R0 + 0.006, 0.042], [R0 - 0.004, 0.034], [R0 - 0.004, 0]], 26);+ // jewels set in the band+ for (let i = 0; i < 12; i++) { const a = i * TAU / 12; blob(g, [(R0 + 0.013) * Math.cos(a), 0.02, (R0 + 0.013) * Math.sin(a)], [0.0072, 0.0072, 0.0072], 5, 3); }+ // Fleurs-de-lis cut from sheet: flat plates set on the rim, the way a goldsmith makes them+ const h = 0.05 * tall, fl = [[-0.03, 0], [0.03, 0], [0.024, h * 0.2], [0.04, h * 0.42], [0.031, h * 0.62], [0.017, h * 0.42], [0.009, h * 0.5], [0.012, h * 0.76], [0, h * 1.1], [-0.012, h * 0.76], [-0.009, h * 0.5], [-0.017, h * 0.42], [-0.031, h * 0.62], [-0.04, h * 0.42], [-0.024, h * 0.2]];+ for (let i = 0; i < CROWN_FLEURS; i++) {+ const a = (i + 0.5) * 360 / CROWN_FLEURS;+ g.at(M.of(M.Ry(90 - a), M.T(0, 0.03, R0 + 0.004)), () => g.slab(fl, [], -0.004, 0.004, { so: 1, sr: 1 }));+ }+ });+}++const figF = {}; // fields kept while a figure is carved+const yNeck = 1.7;+async function makeFigure(kind) {+ const o = FIGURES[kind], stone = new Batch(TILE.fine), gold = new Batch(TILE.gold);+ const body = bodyField(o), arms = body.arms, hO = Object.assign({ seed: o.seed }, o.head);+ // Head frame: gently inclined, sitting on the neck above the centre of the shoulders+ const zc = arms[0].S[2] - (0.142 * (o.slim || 1)), cx = arms[0].S[0];+ const headM = M.of(M.T(cx + 0.024, yNeck + 0.116, zc + (o.sway || 0) * 0.3), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(-5));+ const hF = headField(hO), toHead = invRigid(headM);+ const headW = (x, y, z) => { const q = toHead(x, y, z); return hF(q[0], q[1], q[2]); };+ // Hands: frames derived from the sleeve so that the wrist always leaves the cuff+ const hands = [];+ const handFor = (A, side, pose) => {+ const X0 = [A.W[0] - A.E[0], A.W[1] - A.E[1], A.W[2] - A.E[2]];+ let Y0;+ if (pose === 'support') Y0 = [0.2, -1, 0]; // palm up under a book+ else if (pose === 'bless') Y0 = [0, 0.1, side]; // palm turned forward, two fingers raised+ else Y0 = [0, 0, side]; // back of the hand outward+ const X = pose === 'bless' ? [0.25, 1, 0.05] : X0;+ return { m: frameOf(A.W, X, Y0), pose, thumb: pose === 'grip' || pose === 'edge' || pose === 'rest' ? -side : side };+ };+ const handsCfg = { key: ['grip', 'support'], crozier: ['grip', 'rest'], sceptre: ['grip', 'rest'], mantle: ['rest', 'rest'], scroll: ['edge', 'rest'], child: ['rest', 'support'], bless: ['bless', 'support'] }[o.hold] || ['rest', 'rest'];+ hands.push(handFor(arms[0], 1, handsCfg[0]), handFor(arms[1], -1, handsCfg[1]));+ const handFs = hands.map((h) => { const f = handField(h.pose, h.thumb), inv = invRigid(h.m); return (x, y, z) => { const q = inv(x, y, z); return f(q[0], q[1], q[2]); }; });+ // The Child: a real little person on the Virgin's arm, turning to her+ let childF = null, childM = null;+ if (o.hold === 'child') {+ const A = arms[1];+ // sits in the crook of her left arm, turned towards her, one small hand raised to her veil+ childM = M.of(M.T(A.W[0] - 0.015, A.W[1] + 0.082, A.W[2] - 0.0), M.Ry(-34), M.Rz(4));+ const cH = headField({ hair: 'curls', seed: 21, smile: 0.6 }), inv = invRigid(childM), k = 0.47;+ childF = (x, y, z) => {+ const q = inv(x, y, z), X = q[0], Y = q[1], Z = q[2];+ // A seated child in a long shift: torso, thighs across her arm, shins and small feet hanging+ let d = sdCone(X, Y, Z, 0, 0, 0, 0, 0.165, 0, 0.066, 0.05);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.0, s * 0.034, 0.125, -0.012, s * 0.04, 0.038, 0.03), 0.018); // thigh+ d = smin(d, sdCone(X, Y, Z, 0.125, -0.012, s * 0.04, 0.142, -0.135, s * 0.038, 0.03, 0.022), 0.012); // shin+ d = smin(d, sdCone(X, Y, Z, 0.142, -0.15, s * 0.038, 0.19, -0.17, s * 0.04, 0.02, 0.013), 0.006); // foot+ }+ d -= 0.005 * Math.sqrt(Math.abs(Math.sin(Math.atan2(Z, X) * 6 + Y * 4))) * sstep(0.12, 0.0, Y) * sstep(-0.1, 0.0, Y);+ d = smin(d, sdCone(X, Y, Z, 0.005, 0.135, -0.052, 0.06, 0.07, -0.05, 0.02, 0.016), 0.012); // one arm holds an orb on his knee+ d = smin(d, len3(X - 0.1, Y - 0.052, Z + 0.042) - 0.03, 0.003);+ d = smin(d, len3(X - 0.074, Y - 0.056, Z + 0.054) - 0.015, 0.004);+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.145, 0.05, -0.005, 0.2, 0.094, 0.02, 0.015), 0.012); // the other reaches back to touch her veil+ d = smin(d, len3(X + 0.007, Y - 0.214, Z - 0.104) - 0.0155, 0.005);+ d = smin(d, cH((X - 0.008) / k, (Y - 0.262) / k, Z / k) * k, 0.012);+ return d;+ };+ }+ const allF = (x, y, z) => {+ let d = body.F(x, y, z);+ d = Math.min(d, headW(x, y, z));+ for (let i = 0; i < handFs.length; i++) d = Math.min(d, handFs[i](x, y, z));+ if (childF) d = Math.min(d, childF(x, y, z));+ return d;+ };+ // ---- carve ----+ await carveInto(stone, body.F, [-0.42, -0.005, -0.52, 0.42, yNeck + 0.04, 0.52], 0.0105, o.wings ? 3800 : 4400, null, allF);+ const head = await headBatch(o.headKey);+ for (let i = 0; i < hands.length; i++) {+ const hb = await handBatch(hands[i].pose, hands[i].thumb);+ const base = stone.count;+ copyBatch(stone, hb, hands[i].m);+ }+ if (childF) await carveInto(stone, childF, [arms[1].W[0] - 0.24, arms[1].W[1] - 0.2, arms[1].W[2] - 0.26, arms[1].W[0] + 0.3, arms[1].W[1] + 0.56, arms[1].W[2] + 0.26], 0.0042, 3200, null, allF, { smooth: 1, ao: 0.34, aoMin: 0.3 });+ addAttrs({ stone, gold }, o, arms);+ // Crown, nimbus and wings+ if (o.head.crown) addCrown(gold, M.of(headM, M.T(-0.012, o.head.veil ? 0.088 : 0.096, 0)), o.head.veil ? 0.088 : 0.084);+ if (o.nimbus) gold.at(M.of(headM, M.T(-0.16, 0.03, 0), M.Ry(90)), () => { gold.slab(ringPts(0, 0, 0.235, 28), [], -0.014, 0.014, { sb: 0.8 }); });+ if (o.wings) {+ // Feathers in overlapping tiers, carved on a blade that rises from the shoulders and falls to the calf+ const W = (side) => (x, y, z) => {+ // The wall side (local -z in every jamb) folds along the stepped stone from the shoulder; the free side stands off behind the back+ const zz = z * side - 0.12 - zc * side, X = side < 0 ? x + 0.01 + 0.6 * (zz - 0.065) : x + 0.085 + zz * 0.18 + 0.05 * clamp((1.86 - y) / 1.32), u = clamp((1.86 - y) / 1.32), wid = 0.27 * Math.pow(Math.sin(PI * Math.pow(u, 0.6)), 0.65) * (1 - 0.3 * u);+ const v = zz - 0.03 - u * 0.1 + 0.015;+ let d = Math.abs(X) - 0.026 + 0.012 * u;+ d = Math.max(d, Math.abs(v - wid * 0.5) - wid * 0.5, y - 1.87, 0.53 - y);+ // Tiers of feathers: short and scaled over the shoulder, long pointed quills towards the tip+ const row = Math.abs(Math.sin(u * 14 + v * 6)), bars = Math.abs(Math.sin(v * 26 + u * 7));+ d -= 0.015 * Math.sqrt(row) * sstep(0.45, 0.2, u) + 0.016 * Math.sqrt(bars) * sstep(0.2, 0.42, u);+ // The wing's arm: it grows out of the shoulder blade, so the blade never hangs in the air behind the body+ return smin(d * 0.8, sdCone(x, y, zz, -0.035, 1.6, -0.06, side < 0 ? -0.045 : -0.12, 1.46, 0.1, 0.05, 0.028), 0.04);+ };+ for (const side of [-1, 1]) await carveInto(stone, W(side), [-0.3, 0.5, side > 0 ? zc + 0.05 : zc - 0.72, 0.1, 1.9, side > 0 ? zc + 0.72 : zc - 0.05], 0.0105, 900, null, allF);+ }+ stone.isFigure = true;+ return { stone, gold, head, headM };+}+// ---- Seated and kneeling figures for the tympana: same people, other postures -------------+function poseField(kind) {+ if (kind === 'seated') return (x, y, z) => {+ // A figure enthroned: knees forward, the robe falling from them in a curtain to the feet+ const th = Math.atan2(z, x);+ let d = sdCone(x, y, z, -0.04, 0.72, 0, -0.03, 1.38, 0, 0.17, 0.13);+ d = smin(d, sdEll(x, y - 1.46, z, 0.09, 0.12, 0.17), 0.03);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(x, y, z, -0.03, 0.7, s * 0.1, 0.27, 0.68, s * 0.11, 0.1, 0.09), 0.03);+ const cur = sdCone(x, y, z, 0.27, 0.64, s * 0.11, 0.31, 0.04, s * 0.1, 0.09, 0.12);+ d = smin(d, cur - 0.012 * Math.sqrt(Math.abs(Math.sin(z * 34 + x * 4))), 0.03);+ }+ d = smin(d, sdCone(x, y, z, 0.16, 0.45, 0, 0.27, 0.04, 0, 0.11, 0.16) - 0.014 * Math.sqrt(Math.abs(Math.sin(z * 30 + 1))), 0.03);+ // the lap folds run across, the bodice is drawn up+ d -= 0.012 * Math.sqrt(Math.abs(Math.sin((y + 0.12 * z * z * 40) * 28))) * sstep(0.95, 0.7, y) * sstep(0.45, 0.62, y);+ d -= 0.009 * Math.sqrt(Math.abs(Math.sin(th * 7 + y * 2))) * sstep(0.8, 1.0, y);+ return Math.max(d, -y);+ };+ return (x, y, z) => {+ // Kneeling in prayer, turned to the centre: a body with a thigh and a folded shin under long cloth. The folds fall straight+ // down from the waist and break on the ground, never radiating round the body like the ribs of a pumpkin+ let d = sdCone(x, y, z, -0.1, 0.46, 0, 0.03, 1.04, 0, 0.15, 0.115);+ d = smin(d, sdEll(x - 0.05, y - 1.12, z, 0.08, 0.1, 0.15), 0.03);+ d = smin(d, sdCone(x, y, z, -0.1, 0.42, 0, 0.28, 0.15, 0, 0.16, 0.11), 0.05);+ d = smin(d, sdCone(x, y, z, 0.28, 0.12, 0, -0.26, 0.1, 0, 0.115, 0.09), 0.04);+ const fall = 0.62 * Math.sqrt(Math.abs(Math.sin(z * 31 + x * 3 + 1.1))) + 0.38 * Math.sqrt(Math.abs(Math.sin(z * 63 + y * 5 + 2.3)));+ d -= 0.017 * (fall - 0.55) * sstep(0.86, 0.42, y);+ d -= 0.006 * Math.sqrt(Math.abs(Math.sin(z * 38 + y * 2))) * sstep(0.6, 0.86, y);+ return Math.max(d, -y);+ };+}+async function makePosed(kind, spec) {+ const stone = new Batch(TILE.fine), gold = new Batch(TILE.gold), bF = poseField(kind), hO = Object.assign({ seed: 3 }, spec.head);+ const hy = kind === 'seated' ? 1.74 : 1.36, hx = kind === 'seated' ? -0.01 : 0.075;+ const headM = M.of(M.T(hx, hy, 0), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(hO.nod || -6));+ const allF = bF, head = await headBatch(spec.headKey);+ stone.isFigure = true;+ await carveInto(stone, bF, [-0.42, -0.005, -0.42, 0.56, kind === 'seated' ? 1.62 : 1.26, 0.42], 0.0145, 2600, null, allF);+ if (hO.crown) addCrown(gold, M.of(headM, M.T(-0.014, 0.1, 0)), hO.veil ? 0.106 : 0.098);+ // Forearms and hands: joined in prayer when kneeling, one raised when enthroned+ const hand = await handBatch(kind === 'seated' ? 'bless' : 'support', 1);+ if (kind === 'seated') {+ const A = spec.bless === false ? null : { W: [0.2, 1.32, 0.17] };+ const armF = (x, y, z) => { let d = sdCone(x, y, z, -0.02, 1.42, 0.17, 0.06, 1.02, 0.23, 0.066, 0.055); d = smin(d, sdCone(x, y, z, 0.06, 1.02, 0.23, 0.2, 1.3, 0.17, 0.055, 0.058), 0.02); d = Math.min(d, smin(sdCone(x, y, z, -0.02, 1.42, -0.17, 0.05, 1.02, -0.22, 0.066, 0.055), sdCone(x, y, z, 0.05, 1.02, -0.22, 0.27, 0.92, -0.08, 0.055, 0.058), 0.02)); return d; };+ await carveInto(stone, armF, [-0.12, 0.84, -0.34, 0.36, 1.52, 0.34], 0.0125, 700, null, allF);+ copyBatch(stone, hand, frameOf([0.2, 1.3, 0.17], [0.25, 1, 0.05], [0, 0.1, 1]));+ addBook(stone, M.of(M.T(0.36, 0.92, -0.08), M.Rz(-78)), { w: 0.11, h: 0.15 });+ } else {+ const armF = (x, y, z) => { let d = 1e9; for (const s of [-1, 1]) d = Math.min(d, smin(sdCone(x, y, z, 0.03, 1.03, s * 0.15, 0.1, 0.8, s * 0.17, 0.062, 0.05), sdCone(x, y, z, 0.1, 0.8, s * 0.17, 0.25, 0.97, s * 0.04, 0.05, 0.055), 0.02)); return d; };+ await carveInto(stone, armF, [-0.08, 0.68, -0.3, 0.34, 1.12, 0.3], 0.0125, 600, null, allF);+ for (const s of [-1, 1]) copyBatch(stone, hand, frameOf([0.25, 0.97, s * 0.04], [0.5, 1, -s * 0.05], [1, -0.4, s * 0.9]));+ }+ return { stone, gold, head, headM };+}++// Cache of finished figures. Lower-detail copies for the arches come from the same sculpture.+const FIG_CACHE = {};+async function lightCopy(src, tris) { // the same surface with fewer triangles, keeping its uvs and carved occlusion+ const g = await simplifyGeometry(src.toGeo(), { targetTriangles: tris });+ const T = src.tile, b = new Batch(T), p = g.attributes.position, n = g.attributes.normal, u = g.attributes.uv, c = g.attributes.color, idx = g.index;+ for (let i = 0; i < p.count; i++) b.v(p.getX(i), p.getY(i), p.getZ(i), n.getX(i), n.getY(i), n.getZ(i), u.getX(i) * T, u.getY(i) * T, c ? c.getX(i) : 1);+ for (let t = 0; t < idx.count; t += 3) b.tri(idx.getX(t), idx.getX(t + 1), idx.getX(t + 2));+ return b;+}+async function lowDetail(src, tris) {+ const b = await lightCopy(src.stone, tris);+ b.isFigure = true;+ // A figure high in the arches carries a light copy of its head inside its own mesh: fewer draws, fewer triangles+ if (!src.head.low) src.head.low = await lightCopy(src.head, 420);+ copyBatch(b, src.head.low, src.headM);+ return { stone: b, gold: new Batch(TILE.gold) };+}+async function prepareFigures(kinds) {+ for (const k of kinds) FIG_CACHE[k + '|1'] = await makeFigure(k);+ if (P.preview !== 'statues') {+ for (const k of ['apostle', 'king', 'angel', 'bishop', 'queen']) if (FIG_CACHE[k + '|1']) FIG_CACHE[k + '|0'] = await lowDetail(FIG_CACHE[k + '|1'], P.archFigureTris);+ for (let k = 0; k < 3; k++) GARGOYLES[k] = await lightCopy(makeGargoyle(k), 800);+ FIG_CACHE['seated|1'] = await makePosed('seated', { headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 9, nod: -2 } });+ FIG_CACHE['seatedVirgin|1'] = await makePosed('seated', { headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, turn: -14, tilt: 4 } });+ FIG_CACHE['kneel|1'] = await makePosed('kneel', { headKey: 'veiled', head: { veil: 1, seed: 8, tilt: 0, nod: 8 } });+ FIG_CACHE['kneel|0'] = await lowDetail(FIG_CACHE['kneel|1'], P.archFigureTris);+ }+}+function figure(name, lod = 1) {+ const k = FIG_ALIAS[name] || name;+ return FIG_CACHE[k + '|' + lod] || FIG_CACHE[k + '|1'];+}+// Body and head are separate nodes: lod 0 lightens the body only, a head costs its bytes once however often it appears+function placeFigure(name, kind, o = {}) {+ const f = figure(kind, o.lod === undefined ? 1 : o.lod);+ if (!f) return null;+ // o.relief squeezes the figure front to back, so it is carved from the wall behind it instead of standing before it+ const s = o.scale || 1, sc = [s * (o.relief || 1), s, o.mirror ? -s : s];+ const mat = o.indoor ? MAT.figureIn : o.deep ? MAT.figureDeep : o.half ? MAT.figureHalf : MAT.figure;+ const stone = place(name, f.stone, mat, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ place(name + '_Gilt', f.gold, MAT.gold, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ let headPart = null;+ if (f.head) {+ const m = f.headM, e = eulerFromBasis([m[0], m[4], m[8]], [m[1], m[5], m[9]], [m[2], m[6], m[10]]);+ headPart = place(name + '_Head', f.head, mat, { pos: [m[3], m[7], m[11]], rot: e, parent: stone || undefined });+ }+ if (stone && !o.indoor && !o.deep && !o.half) FIG_PLACED.push({ part: stone, head: headPart });+ return stone;+}+const FIG_PLACED = [];++// ---- Foliage and crockets ----------------------------------------------------------+// One carved leaf curling at the tip; local +Y up the surface, +X outward from the host+function addLeaf(b, m, size = 1, o = {}) {+ b.at(m, () => {+ const nu = o.coarse ? 2 : 4, nv = o.coarse ? 3 : 4;+ b.grid((u, v) => {+ const s = (u - 0.5) * 2, W = 0.5 * Math.sin(PI * Math.pow(v, 0.72)) * (1 - 0.35 * v);+ const curl = Math.pow(v, 2.2);+ const lobes = 1 + 0.18 * Math.sin(v * 16) * Math.abs(s);+ const rib = 0.05 * (1 - Math.abs(s));+ return [size * (rib + 0.3 * curl - 0.42 * curl * curl * curl + 0.12 * s * s), size * (v - 0.3 * curl * v), size * (s * W * lobes), 0.72 + 0.28 * (1 - Math.abs(s)) + 0.1 * curl];+ }, nu, nv, { flip: true });+ });+}+// A crocket: a bud of curled foliage that sits on an arris. Local +X away from the arris, +Y up the slope.+function addCrocket(b, m, size = 1, lod = 1) {+ b.at(m, () => {+ const path = [];+ const N = lod ? 5 : 3;+ for (let i = 0; i <= N; i++) { const t = i / N, a = -0.9 + t * 3.8, r = lerp(0.3, 0.12, t) * size; path.push([size * 0.16 + r * Math.cos(a) * 0.95, size * 0.1 + r * Math.sin(a) * 0.9, 0]); }+ b.tube(path, (t) => [lerp(0.14, 0.05, t) * size, lerp(0.17, 0.07, t) * size], lod ? 5 : 3, { capStart: false, capEnd: false, ringS: (a) => 0.8 + 0.2 * Math.cos(a) });+ if (lod) for (const s of [-1, 1]) b.tube([[0.02 * size, 0.02 * size, 0], [0.2 * size, 0.1 * size, s * 0.22 * size], [0.34 * size, 0.26 * size, s * 0.3 * size]], (t) => [lerp(0.07, 0.015, t) * size, lerp(0.05, 0.02, t) * size], 3, { capEnd: false });+ });+}+function addFinial(b, h = 1, lod = 1) { // cross-shaped foliate finial, base at y=0+ b.lathe([[0.12 * h, 0], [0.1 * h, 0.05 * h], [0.14 * h, 0.09 * h], [0.08 * h, 0.14 * h], [0.05 * h, 0.5 * h], [0.09 * h, 0.56 * h], [0.05 * h, 0.62 * h], [0.02 * h, 0.98 * h], [0, h]], lod ? 8 : 5);+ for (let i = 0; i < 4; i++) {+ const a = i * 90 + 45;+ addLeaf(b, M.of(M.T(0, 0.14 * h, 0), M.Ry(a), M.T(0.05 * h, 0, 0)), 0.4 * h, { coarse: 1 });+ if (lod) addLeaf(b, M.of(M.T(0, 0.58 * h, 0), M.Ry(a + 45), M.T(0.03 * h, 0, 0)), 0.3 * h, { coarse: 1 });+ }+}+// Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame+function crocketsAlong(b, p, q, n, size, out, lod = 1) {+ const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);+ const dn = d.map((c) => c / L);+ let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];+ const ol = Math.hypot(ox, oy, oz) || 1; ox /= ol; oy /= ol; oz /= ol;+ const zx = ox * 0, side = [dn[1] * oz - dn[2] * oy, dn[2] * ox - dn[0] * oz, dn[0] * oy - dn[1] * ox];+ // columns: X = out, Y = along edge, Z = side+ for (let i = 0; i < n; i++) {+ const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];+ const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];+ addCrocket(b, m, size * lerp(1, 0.78, t), lod);+ }+}++// Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.+function addPinnacle(b, w, h, o = {}) {+ const hw = w / 2, shaftH = h * 0.42, gableH = w * 0.95, lod = o.lod === undefined ? 1 : o.lod;+ if (o.simple) { // far-away pinnacle: faceted shaft, spirelet, a few crockets, finial+ b.lathe([[hw * 1.414, 0], [hw * 1.414, shaftH], [hw * 1.6, shaftH + w * 0.06], [hw * 1.3, shaftH + w * 0.12], [hw * 0.1, h * 0.86]], 4, { faceted: true, phase: PI / 4 });+ for (let i = 0; i < 4; i++) { const a = rad(i * 90 + 45), c = Math.cos(a), s = Math.sin(a), bR = hw * 1.3 * 1.0; crocketsAlong(b, [c * bR, shaftH + w * 0.14, s * bR], [c * hw * 0.14, h * 0.84, s * hw * 0.14], 2, w * 0.46, [c, 0.15, s], 0); }+ b.at(M.T(0, h * 0.84, 0), () => addFinial(b, h * 0.16, 0));+ return;+ }+ // Shaft with moulded base and blind lancet panels+ b.lathe([[hw * 1.44, 0], [hw * 1.44, w * 0.06], [hw * 1.3, w * 0.12], [hw * 1.3, w * 0.2], [hw * 1.26, w * 0.24]], 4, { faceted: true, phase: PI / 4 });+ b.lathe([[hw * 1.414, w * 0.2], [hw * 1.414, shaftH]], 4, { faceted: true, phase: PI / 4, s: 0.95 });+ for (let i = 0; i < 4; i++) b.at(M.Ry(i * 90), () => {+ const pw = w * 0.62, y0 = w * 0.35, ys = shaftH - w * 0.5;+ b.at(M.T(hw + 0.004, 0, 0), () => b.at(M.Ry(90), () => { // panel recessed look: shaded inset lancet with moulded frame+ b.sweep(ofsXY(pointedArc(pw, pw * 0.85, 3), 0, ys), hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[-pw / 2, y0], [-pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[pw / 2, y0], [pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.face([[-pw / 2, y0, -0.002, 0.5], [pw / 2, y0, -0.002, 0.5], [pw / 2, ys, -0.002, 0.5], [0, ys + pw * 0.8, -0.002, 0.45], [-pw / 2, ys, -0.002, 0.5]]);+ }));+ // Gablet on each face with crockets+ b.at(M.T(hw * 0.96, shaftH, 0), () => {+ b.slabX = true;+ b.at(M.Ry(90), () => b.slab([[-hw * 1.04, 0], [hw * 1.04, 0], [0, gableH]], [], 0, w * 0.08, { sf: 1, sb: 0.8 }));+ crocketsAlong(b, [0.02, 0.02, -hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, -1], 0);+ crocketsAlong(b, [0.02, 0.02, hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, 1], 0);+ b.at(M.T(0.04, gableH * 0.94, 0), () => addFinial(b, w * 0.34, 0));+ });+ });+ // Cornice ring and spirelet+ b.at(M.T(0, shaftH, 0), () => {+ b.lathe([[hw * 1.414, 0], [hw * 1.6, w * 0.06], [hw * 1.6, w * 0.1], [hw * 1.414, w * 0.14]], 4, { faceted: true, phase: PI / 4 });+ const sh = h - shaftH - w * 0.1;+ b.at(M.T(0, w * 0.12, 0), () => {+ b.lathe([[hw * 1.3, 0], [hw * 0.09, sh * 0.82]], 8, { faceted: true, phase: PI / 8 });+ const n = lod ? 5 : 3;+ for (let i = 0; i < 4; i++) {+ const a = rad(i * 90 + 22.5 + 22.5 * 0);+ const c = Math.cos(a + rad(22.5)), s = Math.sin(a + rad(22.5));+ const bR = hw * 1.3 * Math.cos(rad(0)), tR = hw * 0.09;+ crocketsAlong(b, [c * bR, sh * 0.04, s * bR], [c * tR, sh * 0.8, s * tR], n, w * 0.36, [c, 0.12, s], lod);+ }+ b.at(M.T(0, sh * 0.8, 0), () => (o.finial || b) === b ? addFinial(b, sh * 0.22 + w * 0.1, lod) : null);+ });+ });+}++// ---- Gargoyle ------------------------------------------------------------------------+// A crouching beast thrusting out from the wall along +X, root at the origin. About 2.3 m long.+function makeGargoyle(kind = 0) {+ const b = new Batch(TILE.fine), rng = mkRng(kind * 17 + 5);+ const ridge = (kind === 1 ? 0.3 : 0.22);+ const headY = kind === 2 ? 0.1 : 0.34;+ const spine = spline([[-0.3, -0.05, 0], [0.22, 0.2, 0], [0.75, 0.3, 0], [1.2, 0.2, 0], [1.56, headY, 0]], 5);+ b.tube(spine, (t) => { const r = keyf([[0, 0.34, 0.3], [0.25, 0.36, 0.34], [0.55, 0.22, 0.24], [0.82, 0.16, 0.17], [1, 0.19, 0.2]], t); return [r[0], r[1]]; }, 16, {+ capStart: true,+ up: [0, 1, 0],+ ring: (a, t) => 1 + ridge * Math.pow(Math.max(Math.cos(a), 0), 10) * Math.abs(Math.sin(t * 26)) + 0.05 * Math.abs(Math.sin(a * 5 + t * 11)),+ ringS: (a, t) => 0.74 + 0.26 * Math.abs(Math.sin(a * 5 + t * 11)),+ });+ // Skull, snout, gaping jaws+ const hx = 1.56, hy = headY;+ blob(b, [hx + 0.05, hy + 0.03, 0], [0.22, 0.19, 0.2], 14, 10, 0.92);+ b.tube(spline([[hx + 0.1, hy + 0.06, 0], [hx + 0.34, hy + 0.05, 0], [hx + 0.56, hy + 0.1, 0]], 3), (t) => [lerp(0.14, 0.07, t), lerp(0.15, 0.085, t)], 10, { capStart: true, up: [0, 1, 0], ring: (a, t) => 1 + 0.2 * Math.pow(Math.max(Math.cos(a), 0), 6) * Math.abs(Math.sin(t * 9)) });+ b.shade(0.9, () => b.tube(spline([[hx + 0.05, hy - 0.1, 0], [hx + 0.28, hy - 0.2, 0], [hx + 0.5, hy - 0.2, 0]], 3), (t) => [lerp(0.09, 0.045, t), lerp(0.125, 0.07, t)], 10, { capStart: true, up: [0, 1, 0] }));+ b.shade(0.34, () => blob(b, [hx + 0.2, hy - 0.06, 0], [0.16, 0.085, 0.095], 8, 5));+ for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {+ const f = i / 3.5;+ b.tube([[hx + 0.26 + f * 0.27, hy + 0.0 + f * 0.075, s * lerp(0.1, 0.06, f)], [hx + 0.27 + f * 0.27, hy - 0.08 + f * 0.075, s * lerp(0.1, 0.06, f)]], (t) => lerp(0.018, 0.002, t), 4, { capStart: true });+ b.tube([[hx + 0.22 + f * 0.27, hy - 0.15, s * lerp(0.1, 0.05, f)], [hx + 0.22 + f * 0.27, hy - 0.08, s * lerp(0.1, 0.05, f)]], (t) => lerp(0.016, 0.002, t), 4, { capStart: true });+ }+ // Brow, eyes, ears or horns+ for (const s of [-1, 1]) {+ b.tube(spline([[hx + 0.02, hy + 0.2, s * 0.03], [hx + 0.17, hy + 0.17, s * 0.1], [hx + 0.24, hy + 0.12, s * 0.15]], 2), (t) => lerp(0.03, 0.02, t), 5, { capStart: true });+ b.shade(0.38, () => blob(b, [hx + 0.21, hy + 0.09, s * 0.11], [0.05, 0.04, 0.045], 6, 4));+ blob(b, [hx + 0.235, hy + 0.09, s * 0.115], [0.024, 0.022, 0.022], 6, 4);+ if (kind === 1) b.tube(spline([[hx - 0.05, hy + 0.14, s * 0.11], [hx - 0.1, hy + 0.3, s * 0.18], [hx - 0.25, hy + 0.42, s * 0.2], [hx - 0.42, hy + 0.42, s * 0.15]], 3), (t) => lerp(0.05, 0.005, t), 6, { capStart: true });+ else b.grid((u, v) => { const L = 0.3, W = 0.09 * Math.sin(PI * u); return [hx - 0.08 - u * L * 0.5, hy + 0.13 + u * L * 0.7, s * (0.13 + u * 0.09 + (v - 0.5) * W * 2), 0.7 + 0.3 * Math.abs(v - 0.5) * 2]; }, 4, 2, { flip: s > 0 });+ if (kind === 1) { /* also add jowls */ blob(b, [hx + 0.1, hy - 0.04, s * 0.13], [0.09, 0.06, 0.05], 6, 4, 0.9); }+ // Fore leg gripping the ledge+ const sh = [0.5, 0.12, s * 0.3], el = [0.68, -0.28, s * 0.42], wr = [0.98, -0.42, s * 0.3];+ b.tube(spline([sh, el, wr], 4), (t) => keyf([[0, 0.16], [0.4, 0.105], [1, 0.07]], t)[0], 10, { capStart: true, ring: (a, t) => 1 + 0.08 * Math.abs(Math.sin(a * 3 + t * 8)) });+ blob(b, wr, [0.1, 0.06, 0.09], 8, 5);+ for (let i = -1; i <= 1; i++) b.tube(spline([[wr[0] + 0.03, wr[1], wr[2] + i * 0.05], [wr[0] + 0.14, wr[1] - 0.02, wr[2] + i * 0.08], [wr[0] + 0.21, wr[1] - 0.11, wr[2] + i * 0.09]], 2), (t) => lerp(0.032, 0.004, t), 5);+ // Folded wing+ const span = kind === 0 ? 0.95 : 0.7;+ const wingFn = (off) => (u, v) => {+ const L = u * span, W = 0.44 * Math.sin(PI * Math.pow(u, 0.7));+ const memb = Math.abs(Math.sin(v * 7.5 + u * 1.2)) * 0.05 * u;+ return [0.42 - L + (off), 0.3 + 0.48 * Math.sin(u * 1.25) * (1 - v * 0.9) - v * W * 0.75 + memb, s * (0.27 + 0.2 * u + v * W * 0.5), 0.68 + 0.32 * (memb / 0.05)];+ };+ b.grid(wingFn(0), 7, 5, { flip: s < 0 });+ b.grid(wingFn(-0.02), 7, 5, { flip: s > 0 });+ }+ return b;+}+const GARGOYLES = [];+function gargoyle(kind) { if (!GARGOYLES[kind]) GARGOYLES[kind] = makeGargoyle(kind); return GARGOYLES[kind]; }++// ==================================================================================+// STAINED GLASS: a tiny vector-art engine producing real leaded panes.+// Every shape is a pane of pot-metal colour with a came (lead strip) around it; large+// figures get painted grisaille lines. Windows are geometry, so they stay crisp at any distance.+// ==================================================================================+function offsetPoly(pts, d) {+ const n = pts.length, s = area2(pts) >= 0 ? 1 : -1, out = [];+ for (let i = 0; i < n; i++) {+ const a = pts[(i - 1 + n) % n], b = pts[i], c = pts[(i + 1) % n];+ let ax = b[0] - a[0], ay = b[1] - a[1], cx = c[0] - b[0], cy = c[1] - b[1];+ const la = Math.hypot(ax, ay) || 1, lc = Math.hypot(cx, cy) || 1; ax /= la; ay /= la; cx /= lc; cy /= lc;+ let nx = (ay + cy) * s, ny = -(ax + cx) * s; const l = Math.hypot(nx, ny) || 1; nx /= l; ny /= l;+ const k = clamp(1 / Math.max(0.35, Math.abs(nx * ay * s - ny * ax * s)), 1, 2.4);+ out.push([b[0] + nx * d * k, b[1] + ny * d * k]);+ }+ return out;+}+function clipConvex(poly, clip) {+ let out = poly; const sgn = area2(clip) >= 0 ? 1 : -1;+ for (let i = 0; i < clip.length && out.length; i++) {+ const a = clip[i], b = clip[(i + 1) % clip.length], inp = out; out = [];+ const side = (p) => sgn * ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]));+ for (let j = 0; j < inp.length; j++) {+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);+ if (sp >= 0) out.push(p);+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([lerp(p[0], q[0], t), lerp(p[1], q[1], t)]); }+ }+ }+ return out;+}+function isConvex(p) { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = p[(i + 2) % p.length]; const s = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); if (Math.abs(s) < 1e-9) continue; if (!sign) sign = Math.sign(s); else if (Math.sign(s) !== sign) return false; } return true; }++class Glass {+ constructor(seed = 1) { this.b = new Batch(1); this.b.noColor = true; this.r = mkRng(seed * 13 + 3); this.clip = null; this.xf = [1, 0, 0, 1, 0, 0]; this.came = 0.022; }+ xform(a, b, c, d, e, f) { const m = this.xf; return [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]]; }+ withXf(rotDeg, tx, ty, s, fn) { const o = this.xf, c = Math.cos(rad(rotDeg)) * s, sn = Math.sin(rad(rotDeg)) * s; this.xf = this.xform(c, sn, -sn, c, tx, ty); fn(this); this.xf = o; }+ withClip(pts, fn) { const o = this.clip; const m = this.xf; this.clip = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]); fn(this); this.clip = o; }+ toLocal(p) { const m = this.xf, det = m[0] * m[3] - m[1] * m[2], x = p[0] - m[4], y = p[1] - m[5]; return [(m[3] * x - m[2] * y) / det, (-m[1] * x + m[0] * y) / det]; }+ // Writes one flat polygon. map(worldPoint) -> [u,v] in atlas pixels; otherwise a window on the sheet of its colour+ raw(pts, color, layer, jitter = 1, map = null) {+ if (layer === -0.4 && !this.b.outline) this.b.outline = pts; // the first polygon of a window is its whole outline+ const name = GLASS_ALIAS[color] || color, fam = GL.fam[name] || GL.fam.lead, z = 0.002 + layer * 0.0035, C = GL.small;+ // Every pane is cut from a different part of the sheet, so no two panes match;+ // 200 pixels of sheet to the metre, so the streaks and clouds of the pot-metal show in the pane+ const ox = fam[0] + 44 + this.r() * (C - 88), oy = fam[1] + 44 + this.r() * (C - 88), rot = this.r() * 6.28, cr = Math.cos(rot) * 200, sr = Math.sin(rot) * 200;+ let mx = 0, my = 0; for (const p of pts) { mx += p[0]; my += p[1]; } mx /= pts.length; my /= pts.length;+ const ids = pts.map((p) => {+ let u, v;+ if (map) { const q = map(p); u = q[0]; v = q[1]; } else {+ const dx = clamp(p[0] - mx, -0.2, 0.2), dy = clamp(p[1] - my, -0.2, 0.2);+ u = ox + (dx * cr - dy * sr) * jitter; v = oy + (dx * sr + dy * cr) * jitter;+ }+ return this.b.v(p[0], p[1], z, 0, 0, 1, u / GL.w, v / GL.h, 1);+ });+ if (isConvex(pts)) { const flip = area2(pts) < 0; for (let i = 1; i < ids.length - 1; i++) { if (flip) this.b.tri(ids[0], ids[i + 1], ids[i]); else this.b.tri(ids[0], ids[i], ids[i + 1]); } } else { const t = triangulate(pts); for (const k of t) this.b.tri(ids[k[0]], ids[k[1]], ids[k[2]]); }+ }+ // a pane: colour polygon + lead came. o.tile paints drapery across it, o.orn ornament.+ poly(pts, color, layer = 1, o = {}) {+ const m = this.xf; let P2 = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]);+ if (area2(P2) < 0) P2 = P2.slice().reverse();+ const came = o.came === undefined ? this.came : o.came;+ if (came > 0) {+ let C = offsetPoly(P2, came);+ if (this.clip) C = clipConvex(C, this.clip);+ if (C.length >= 3 && Math.abs(area2(C)) > 1e-5) this.raw(C, 'lead', layer - 0.5, 0);+ } else if (came < 0) P2 = offsetPoly(P2, came); // over a lead backing: inset instead of a second polygon+ if (this.clip) P2 = clipConvex(P2, this.clip);+ if (P2.length < 3 || Math.abs(area2(P2)) <= 1e-5) return this;+ let map = o.map || null;+ if (o.tile && GL.tile[o.tile]) { // planar mapping of the painted tile over the local box+ const T = GL.tile[o.tile], B = o.box, f = o.flipU ? -1 : 1;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + (f > 0 ? a : 1 - a) * (T[2] - 4), T[1] + 2 + (1 - c) * (T[3] - 4)]; };+ } else if (o.orn && GL.orn[o.orn]) {+ const T = GL.orn[o.orn], B = o.box;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + a * (GL.small - 4), T[1] + 2 + (1 - c) * (GL.small - 4)]; };+ }+ this.raw(P2, color, layer, o.jitter === undefined ? 1 : o.jitter, map);+ return this;+ }+ disc(cx, cy, r, color, layer = 1, n = 12, o) { return this.poly(ringPts(cx, cy, r, n), color, layer, o); }+ // a head drawn by the glass painter: an oval pane mapped onto a face from the atlas+ face(cx, cy, rx, ry, kind, layer, flip) {+ const F = GL.face[kind] || GL.face.youth, C = GL.cell, pts = []; for (let i = 0; i < 12; i++) { const a = i * TAU / 12; pts.push([cx + rx * Math.cos(a), cy + ry * Math.sin(a)]); }+ return this.poly(pts, 'flesh', layer, { came: 0.012, map: (p) => { const q = this.toLocal(p), a = ((q[0] - cx) / rx) * (flip ? -1 : 1), c = (q[1] - cy) / ry; return [F[0] + C / 2 + clamp(a, -1, 1) * (C / 2 - 3), F[1] + C / 2 - clamp(c, -1, 1) * (C / 2 - 3)]; } });+ }+ // painted line (grisaille): a thin dark ribbon along a polyline+ line(pts, w = 0.02, color = 'lead', layer = 5) {+ for (let i = 0; i < pts.length - 1; i++) {+ const a = pts[i], b = pts[i + 1]; let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx *= w / 2 / l; dy *= w / 2 / l;+ this.poly([[a[0] - dy, a[1] + dx], [a[0] + dy, a[1] - dx], [b[0] + dy, b[1] - dx], [b[0] - dy, b[1] + dx]], color, layer, { came: 0, jitter: 0 });+ }+ return this;+ }+}++// Diapered mosaic background: lozenges of two deep colours, a jewel at each crossing+// (one painted square of leaded lozenges laid down repeatedly: fine as real mosaic, cheap as two triangles a square)+function gDiaper(g, x0, y0, x1, y1, cell, c1, c2, c3, layer = 0) {+ const T = c1 === 'outer' ? 1.5 : 0.72, G = GL.ground[GROUND_OF[c1] || 'rc'], C = GL.cell;+ for (let y = Math.floor(y0 / T) * T; y < y1; y += T) for (let x = Math.floor(x0 / T) * T; x < x1; x += T) {+ const qx = x, qy = y, map = (p) => [G[0] + 2 + clamp((p[0] - qx) / T) * (C - 4), G[1] + 2 + clamp((p[1] - qy) / T) * (C - 4)];+ g.poly([[x, y], [x + T, y], [x + T, y + T], [x, y + T]], c1, layer, { came: 0, map });+ }+}++// A figure in glass: long Gothic proportions, robes painted in brown, the head drawn by hand. (x,y) = feet.+function gFigure(g, x, y, h, o = {}) {+ const sx = o.sx || 0.82, hTot = h; h = hTot * sx;+ const f = o.flip ? -1 : 1, L = (u, v) => [x + f * u * h, y + (v <= 0.76 ? v / 0.76 * (hTot - 0.24 * h) : hTot - (1 - v) * h)], PS = (a) => a.map((p) => L(p[0], p[1]));+ const robe = o.robe || 'white', mantle = o.mantle || 'cobalt', skin = o.skin || 'flesh', big = hTot > 2.2, layer = o.layer === undefined ? 2 : o.layer;+ const sit = o.pose === 'sit', kneel = o.pose === 'kneel';+ const hy = kneel ? 0.67 : sit ? 0.86 : 0.875, hr = 0.066, hx = kneel ? 0.04 : 0;+ // Box in world units for tile mapping, in the local frame of the Glass object+ const bx = [x - h * 0.34, y, x + h * 0.34, y + hTot * 0.86], tR = DRAPE_OF[robe] || 'white', tM = DRAPE_OF[mantle] || 'cobalt';+ const pane = (pts, col, lay, tile, opt) => g.poly(PS(pts), col, lay, Object.assign({ tile, box: bx, flipU: !!o.flip }, opt || {}));+ const brk = (pts, c1, lay, tile) => { // a robe is many pieces of glass: cut it across with lead+ const P3 = PS(pts); let y0 = 1e9, y1 = -1e9; for (const q of P3) { y0 = Math.min(y0, q[1]); y1 = Math.max(y1, q[1]); }+ const n = big ? 4 : (hTot > 0.9 ? 2 : 1);+ for (let i = 0; i < n; i++) {+ const a = lerp(y0, y1, i / n), c = lerp(y0, y1, (i + 1) / n), sl = big ? 0.12 * h * (i % 2 ? 1 : -1) * 0.5 : 0;+ const piece = clipConvex(clipConvex(P3, [[-1e4, a - sl * 0], [1e4, a + sl], [1e4, 1e4], [-1e4, 1e4]]), [[-1e4, -1e4], [1e4, -1e4], [1e4, c + sl], [-1e4, c - sl * 0]]);+ if (piece.length >= 3) g.poly(piece, c1, lay, { tile, box: bx, flipU: !!o.flip, came: 0.016 });+ }+ };+ if (o.wings) {+ pane([[-0.08, hy - 0.1], [-0.3, hy + 0.1], [-0.4, hy - 0.08], [-0.34, 0.4], [-0.24, 0.2], [-0.1, 0.42]], o.wings, layer - 0.2, null);+ pane([[0.08, hy - 0.1], [0.3, hy + 0.1], [0.4, hy - 0.08], [0.34, 0.4], [0.24, 0.2], [0.1, 0.42]], o.wings, layer - 0.2, null);+ if (hTot > 1) for (const s of [-1, 1]) { g.line(PS([[s * 0.1, hy - 0.08], [s * 0.3, 0.52]]), h * 0.01, 'lead', layer); g.line(PS([[s * 0.2, hy - 0.02], [s * 0.34, 0.42]]), h * 0.008, 'lead', layer); }+ }+ if (o.halo !== false) {+ const c = L(hx, hy);+ g.disc(c[0], c[1], h * 0.116, o.halo || 'gold', layer - 0.1, big ? 18 : 12);+ if (o.cross) { for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[c[0] + h * 0.116 * Math.cos(a - 0.26), c[1] + h * 0.116 * Math.sin(a - 0.26)], [c[0] + h * 0.116 * Math.cos(a + 0.26), c[1] + h * 0.116 * Math.sin(a + 0.26)], c], 'ruby', layer, { came: 0.008 }); } }+ }+ // ---- body ----+ if (sit) {+ brk([[-0.12, 0.72], [0.12, 0.72], [0.19, 0.44], [0.26, 0.4], [0.27, 0.08], [0.22, 0.0], [-0.22, 0.0], [-0.2, 0.3], [-0.14, 0.44]], robe, layer, tR);+ brk([[-0.13, 0.74], [0.05, 0.74], [0.17, 0.46], [0.24, 0.4], [0.2, 0.18], [-0.06, 0.1], [-0.2, 0.3], [-0.14, 0.46]], mantle, layer + 0.5, tM);+ } else if (kneel) {+ brk([[-0.08, 0.56], [0.13, 0.56], [0.16, 0.26], [0.26, 0.1], [0.26, 0], [-0.26, 0], [-0.2, 0.12]], robe, layer, tR);+ brk([[-0.09, 0.57], [0.08, 0.57], [0.13, 0.28], [0.1, 0.1], [-0.2, 0.05], [-0.16, 0.24]], mantle, layer + 0.5, tM);+ } else {+ const c = o.sway === undefined ? 0.02 : o.sway;+ brk([[-0.1 + c, 0.75], [0.1 + c, 0.75], [0.115, 0.46], [0.14, 0.2], [0.2, 0.03], [0.15, 0.0], [0.05, 0.022], [-0.05, 0.0], [-0.16, 0.012], [-0.2, 0.04], [-0.14, 0.22], [-0.115, 0.46]], robe, layer, tR);+ brk([[-0.11 + c, 0.76], [0.03 + c, 0.76], [0.12, 0.5], [0.135, 0.3], [0.09, 0.15], [-0.06, 0.11], [-0.165, 0.22], [-0.125, 0.46]], mantle, layer + 0.5, tM);+ pane([[-0.09, 0.03], [-0.02, 0.03], [-0.02, -0.012], [-0.115, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ pane([[0.02, 0.03], [0.09, 0.03], [0.115, -0.012], [0.02, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ }+ // ---- arms and hands ----+ const by = kneel ? 0.5 : 0.68, armPose = o.arm || 'hold';+ const ribbon = (a, b, w, col, tile) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy) || 1, nx = -dy / l * w / 2, ny = dx / l * w / 2; g.poly(PS([[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]]), col, layer + 1, { came: 0.012, tile, box: bx }); };+ const hand = (p) => { const c = L(p[0], p[1]); g.poly([[c[0] - h * 0.026, c[1] - h * 0.018], [c[0] + h * 0.018, c[1] - h * 0.022], [c[0] + h * 0.03, c[1] + h * 0.016], [c[0] + h * 0.006, c[1] + h * 0.03], [c[0] - h * 0.02, c[1] + h * 0.018]], skin, layer + 1.3, { came: 0.008 }); };+ const arms = { hold: [[0.13, by - 0.12], [0.03, by - 0.18], [-0.13, by - 0.12], [-0.03, by - 0.2]], up: [[0.17, by - 0.03], [0.27, by + 0.18], [-0.12, by - 0.12], [-0.03, by - 0.22]], out: [[0.19, by - 0.08], [0.32, by - 0.06], [-0.19, by - 0.08], [-0.32, by - 0.06]], pray: [[0.1, by - 0.14], [0.12, by - 0.03], [-0.04, by - 0.14], [0.1, by - 0.03]], point: [[0.17, by - 0.08], [0.33, by + 0.04], [-0.12, by - 0.12], [-0.04, by - 0.2]], orant: [[0.2, by + 0.0], [0.27, by + 0.14], [-0.2, by + 0.0], [-0.27, by + 0.14]] };+ const A = arms[armPose] || arms.hold;+ ribbon([0.095, by], A[0], 0.07, mantle, tM); ribbon(A[0], A[1], 0.056, robe, tR); hand(A[1]);+ ribbon([-0.095, by], A[2], 0.07, mantle, tM); ribbon(A[2], A[3], 0.056, robe, tR); hand(A[3]);+ // ---- attributes ----+ const at = o.attr;+ if (at === 'book') { g.poly(PS([[A[1][0] - 0.1, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] + 0.12], [A[1][0] - 0.1, A[1][1] + 0.12]]), 'ruby', layer + 1.5, { came: 0.01 }); g.poly(PS([[A[1][0] - 0.07, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.09], [A[1][0] - 0.07, A[1][1] + 0.09]]), 'gold', layer + 1.6, { came: 0 }); }+ else if (at === 'staff' || at === 'cross') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, 0], [sxx + 0.012, 0], [sxx + 0.012, 1.08], [sxx - 0.012, 1.08]]), 'gold', layer + 0.9, { came: 0.008 }); if (at === 'cross') g.poly(PS([[sxx - 0.07, 0.95], [sxx + 0.07, 0.95], [sxx + 0.07, 0.98], [sxx - 0.07, 0.98]]), 'gold', layer + 0.9, { came: 0.008 }); else g.poly(ringPts(sxx + 0.03, 1.1, 0.045, 8).map((p) => L(p[0], p[1])), 'gold', layer + 0.9, { came: 0.008 }); }+ else if (at === 'sword') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, by - 0.08], [sxx + 0.012, by - 0.08], [sxx, 1.02]]), 'white', layer + 0.9, { came: 0.008 }); g.poly(PS([[sxx - 0.05, by - 0.08], [sxx + 0.05, by - 0.08], [sxx + 0.05, by - 0.05], [sxx - 0.05, by - 0.05]]), 'gold', layer + 1, { came: 0.008 }); }+ else if (at === 'key') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.01, by - 0.14], [sxx + 0.01, by - 0.14], [sxx + 0.01, by + 0.2], [sxx - 0.01, by + 0.2]]), 'gold', layer + 1, { came: 0.006 }); g.poly(ringPts(sxx, by + 0.23, 0.035, 6).map((p) => L(p[0], p[1])), 'gold', layer + 1, { came: 0.006 }); g.poly(PS([[sxx, by - 0.14], [sxx + 0.05, by - 0.14], [sxx + 0.05, by - 0.1], [sxx, by - 0.1]]), 'gold', layer + 1, { came: 0.006 }); }+ else if (at === 'lily') { const sxx = A[1][0]; g.line(PS([[sxx, by - 0.22], [sxx + 0.01, by + 0.2]]), h * 0.012, 'green', layer + 1); for (let i = 0; i < 3; i++) { const yy = by + 0.08 + i * 0.07; const q = L(sxx + 0.02 * (i % 2 ? 1 : -1), yy); g.disc(q[0], q[1], h * 0.024, 'white', layer + 1.1, 5, { came: 0.006 }); } }+ // ---- head: drawn, not a disc ----+ const hc = L(hx, hy);+ const kind = o.face || (o.wings ? 'angel' : o.veil ? 'virgin' : o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ const nkA = L(hx - 0.02, hy - 0.11), nkB = L(hx + 0.02, hy - 0.11), nkC = L(hx + 0.02, hy - 0.04), nkD = L(hx - 0.02, hy - 0.04);+ g.poly([nkA, nkB, nkC, nkD], skin, layer + 1.5, { came: 0.008 });+ g.face(hc[0], hc[1], h * hr * 1.02, h * hr * 1.24, kind, layer + 1.8, !!o.flip);+ if (o.crown) g.poly(PS([[hx - 0.066, hy + 0.05], [hx + 0.066, hy + 0.05], [hx + 0.08, hy + 0.13], [hx + 0.04, hy + 0.095], [hx, hy + 0.14], [hx - 0.04, hy + 0.095], [hx - 0.08, hy + 0.13]]), 'gold', layer + 2.2, { came: 0.01 });+ if (o.mitre) g.poly(PS([[hx - 0.056, hy + 0.05], [hx + 0.056, hy + 0.05], [hx + 0.048, hy + 0.15], [hx, hy + 0.215], [hx - 0.048, hy + 0.15]]), o.mitre, layer + 2.2, { came: 0.01 });+}+// ---- Narrative scenes, composed inside a circle of radius r at the origin ---------+const SCENES = {+ annunciation(g, r) { // Angel greets the Virgin; lily between them, dove above+ gFigure(g, -r * 0.46, -r * 0.72, r * 1.34, { robe: 'white', mantle: 'emerald', wings: 'rubyLt', arm: 'point', attr: null, hair: 'amber' });+ gFigure(g, r * 0.46, -r * 0.72, r * 1.3, { flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray', halo: 'gold' });+ g.poly([[-0.1, -r * 0.72], [0.1, -r * 0.72], [0.06, -r * 0.5], [-0.06, -r * 0.5]], 'gold', 3, { came: 0.012 });+ g.line([[0, -r * 0.5], [0, r * 0.05]], 0.025, 'green', 3.2);+ [0.05, -0.1, -0.24].forEach((v, i) => g.disc((i % 2 ? 1 : -1) * r * 0.05, r * v, r * 0.05, 'white', 3.3, 5, { came: 0.008 }));+ g.disc(0, r * 0.62, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.14, r * 0.66], [0, r * 0.6], [r * 0.14, r * 0.66], [0, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ nativity(g, r) { // Virgin, Child in the crib, Joseph, the star, the ox and ass+ g.poly([[-r * 0.8, -r * 0.9], [r * 0.8, -r * 0.9], [r * 0.8, -r * 0.62], [-r * 0.8, -r * 0.62]], 'brown', 1.5);+ gFigure(g, -r * 0.42, -r * 0.62, r * 1.05, { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'out' });+ gFigure(g, r * 0.56, -r * 0.62, r * 1.1, { robe: 'tan', mantle: 'emerald', beard: 1, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'staff', flip: 1, halo: 'yellow' });+ g.poly([[-r * 0.1, -r * 0.62], [r * 0.3, -r * 0.62], [r * 0.34, -r * 0.34], [-r * 0.14, -r * 0.34]], 'amber', 2, { came: 0.014 });+ g.poly([[-r * 0.07, -r * 0.34], [r * 0.27, -r * 0.34], [r * 0.27, -r * 0.22], [-r * 0.07, -r * 0.22]], 'white', 2.5, { came: 0.012 });+ g.disc(r * 0.02, -r * 0.24, r * 0.065, 'gold', 2.5, 8, { came: 0.008 }); g.disc(r * 0.02, -r * 0.24, r * 0.043, 'flesh', 3, 7, { came: 0.008 });+ star(g, 0, r * 0.68, r * 0.13, 'yellow', 3);+ g.poly([[-r * 0.4, r * 0.22], [0, r * 0.52], [r * 0.4, r * 0.22], [r * 0.36, r * 0.14], [0, r * 0.4], [-r * 0.36, r * 0.14]], 'bone', 1.5);+ },+ magi(g, r) { // Three crowned kings bring gifts to the enthroned Virgin+ gFigure(g, r * 0.56, -r * 0.8, r * 1.2, { pose: 'sit', flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'hold' });+ g.disc(r * 0.4, -r * 0.1, r * 0.06, 'gold', 4, 8, { came: 0.008 });+ gFigure(g, -r * 0.05, -r * 0.8, r * 1.18, { pose: 'kneel', robe: 'emerald', mantle: 'ruby', crown: 1, beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'point', halo: false });+ gFigure(g, -r * 0.42, -r * 0.8, r * 1.32, { robe: 'violet', mantle: 'gold', crown: 1, beard: 1, arm: 'point', halo: false });+ gFigure(g, -r * 0.74, -r * 0.8, r * 1.3, { robe: 'teal', mantle: 'rubyLt', crown: 1, arm: 'hold', halo: false });+ star(g, -r * 0.1, r * 0.72, r * 0.11, 'yellow', 3);+ },+ crucifixion(g, r) { // Christ on the cross, the Virgin and Saint John, sun and moon+ g.poly([[-0.09 * r, -r * 0.92], [0.09 * r, -r * 0.92], [0.09 * r, r * 0.76], [-0.09 * r, r * 0.76]], 'emerald', 1.5, { came: 0.016 });+ g.poly([[-r * 0.58, r * 0.28], [r * 0.58, r * 0.28], [r * 0.58, r * 0.45], [-r * 0.58, r * 0.45]], 'emerald', 1.5, { came: 0.016 });+ gFigure(g, 0, -r * 0.84, r * 1.42, { robe: 'white', mantle: 'white', cross: 1, arm: 'out', halo: 'gold', hair: 'hair', beard: 1, sway: 0 });+ gFigure(g, -r * 0.62, -r * 0.88, r * 1.1, { robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray' });+ gFigure(g, r * 0.62, -r * 0.88, r * 1.1, { flip: 1, robe: 'white', mantle: 'rubyLt', arm: 'hold', attr: 'book' });+ g.disc(-r * 0.68, r * 0.66, r * 0.11, 'gold', 2, 8); g.poly([[r * 0.59, r * 0.74], [r * 0.76, r * 0.6], [r * 0.71, r * 0.76]], 'pearl', 2);+ },+ resurrection(g, r) { // Christ rises from the tomb holding the banner; soldiers sleep below+ g.poly([[-r * 0.72, -r * 0.84], [r * 0.72, -r * 0.84], [r * 0.72, -r * 0.42], [-r * 0.72, -r * 0.42]], 'bone', 2);+ g.line([[-r * 0.72, -r * 0.56], [r * 0.72, -r * 0.56]], 0.02, 'lead', 3); [-0.4, 0, 0.4].forEach((k) => g.disc(r * k, -r * 0.68, r * 0.09, 'purple', 3, 8, { came: 0.01 }));+ gFigure(g, 0, -r * 0.42, r * 1.25, { robe: 'white', mantle: 'rubyLt', cross: 1, arm: 'up', hair: 'hair', beard: 1, halo: 'gold' });+ g.poly([[r * 0.34, r * 0.72], [r * 0.34, r * 0.92], [r * 0.7, r * 0.84], [r * 0.34, r * 0.72]], 'white', 3, { came: 0.01 }); g.line([[r * 0.34, r * 0.92], [r * 0.34, -r * 0.1]], 0.02, 'gold', 3.2);+ for (const s of [-1, 1]) { g.disc(s * r * 0.82, -r * 0.5, r * 0.07, 'flesh', 3, 7, { came: 0.01 }); g.poly([[s * r * 0.6, -r * 0.86], [s * r * 0.92, -r * 0.86], [s * r * 0.9, -r * 0.54], [s * r * 0.7, -r * 0.54]], s > 0 ? 'emerald' : 'cobalt', 2.6); }+ },+ supper(g, r) { // The Last Supper: Christ at the centre of a laid table+ [-0.62, -0.31, 0.31, 0.62].forEach((k, i) => gBust(g, r * k, -r * 0.05, r * 0.92, { robe: ['emerald', 'ruby', 'amber', 'violet'][i], beard: i % 2 ? 1 : 0, hair: i === 3 ? 'grey' : 'hair' }));+ gBust(g, 0, -r * 0.02, r * 1.04, { robe: 'rubyLt', beard: 1, cross: 1 });+ g.poly([[-r * 0.92, -r * 0.5], [r * 0.92, -r * 0.5], [r * 0.92, -r * 0.2], [-r * 0.92, -r * 0.2]], 'white', 4);+ g.poly([[-r * 0.86, -r * 0.5], [r * 0.86, -r * 0.5], [r * 0.82, -r * 0.92], [-r * 0.82, -r * 0.92]], 'brown', 3.6);+ [-0.58, -0.2, 0.2, 0.58].forEach((k) => g.disc(r * k, -r * 0.3, r * 0.055, 'gold', 4.5, 6, { came: 0.008 }));+ },+ noah(g, r) { // The Ark on the waters, the dove returning+ [-0.82, -0.56].forEach((v, i) => g.poly([[-r, r * v], [r, r * v], [r, r * (v + 0.22)], [-r, r * (v + 0.18)]], i ? 'azure' : 'cobalt', 2));+ g.poly([[-r * 0.7, -r * 0.4], [r * 0.7, -r * 0.4], [r * 0.5, -r * 0.7], [-r * 0.5, -r * 0.7]], 'brown', 2.5);+ g.poly([[-r * 0.46, -r * 0.4], [r * 0.46, -r * 0.4], [r * 0.46, r * 0.02], [-r * 0.46, r * 0.02]], 'tan', 3);+ g.poly([[-r * 0.56, r * 0.0], [0, r * 0.32], [r * 0.56, r * 0.0]], 'ruby', 3.2);+ [-0.2, 0.2].forEach((k) => g.disc(r * k, -r * 0.18, r * 0.1, 'night', 3.4, 7, { came: 0.012 }));+ gBust(g, r * 0.2, -r * 0.3, r * 0.42, { robe: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', layer: 3.6 });+ g.disc(-r * 0.54, r * 0.64, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.72, r * 0.72], [-r * 0.54, r * 0.62], [-r * 0.36, r * 0.74], [-r * 0.54, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ shepherd(g, r) { // The Good Shepherd carries the lamb; a flock in green pasture+ g.poly([[-r, -r], [r, -r], [r, -r * 0.5], [-r, -r * 0.62]], 'green', 1.5);+ gFigure(g, 0, -r * 0.82, r * 1.5, { robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'hold', attr: 'staff' });+ g.disc(0, r * 0.26, r * 0.11, 'white', 4.5, 8, { came: 0.01 }); g.disc(r * 0.13, r * 0.3, r * 0.05, 'pearl', 4.8, 6, { came: 0.008 });+ for (const [x, y] of [[-0.68, -0.64], [0.66, -0.62], [-0.46, -0.78]]) { g.disc(r * x, r * y, r * 0.11, 'white', 3, 7, { came: 0.01 }); g.disc(r * x + r * 0.12, r * y + r * 0.05, r * 0.05, 'grey', 3.2, 5, { came: 0.008 }); }+ },+ david(g, r) { // King David with his harp+ gFigure(g, -r * 0.14, -r * 0.82, r * 1.42, { pose: 'sit', robe: 'violet', mantle: 'ruby', crown: 1, beard: 1, arm: 'out', halo: false });+ g.poly([[r * 0.26, -r * 0.6], [r * 0.7, r * 0.2], [r * 0.7, r * 0.46], [r * 0.16, -r * 0.36]], 'gold', 3, { came: 0.012 });+ g.line([[r * 0.2, -r * 0.4], [r * 0.2, r * 0.4]], 0.018, 'gold', 3.1); [0.3, 0.4, 0.5, 0.6].forEach((k) => g.line([[r * k, -r * 0.42 + r * (k - 0.2)], [r * k, r * 0.34]], 0.008, 'white', 3.2));+ g.poly([[-r * 0.86, -r * 0.84], [r * 0.5, -r * 0.84], [r * 0.5, -r * 0.7], [-r * 0.86, -r * 0.7]], 'tan', 1.8);+ },+};+function star(g, x, y, r, c, layer) { const p = []; for (let i = 0; i < 16; i++) { const a = i * PI / 8 + PI / 2, q = i % 2 ? r * 0.38 : r * (i % 4 ? 0.7 : 1); p.push([x + q * Math.cos(a), y + q * Math.sin(a)]); } g.poly(p, c, layer, { came: 0.01 }); }+function gBust(g, x, y, h, o = {}) { // head and shoulders, the head drawn by the painter+ const L = o.layer || 2, bx = [x - h * 0.22, y - h * 0.32, x + h * 0.22, y + h * 0.46], t = DRAPE_OF[o.robe || 'ruby'] || 'ruby';+ if (o.halo !== false) g.disc(x, y + h * 0.58, h * 0.17, o.halo || 'gold', L - 0.1, 10);+ g.poly([[x - h * 0.2, y - h * 0.3], [x + h * 0.2, y - h * 0.3], [x + h * 0.17, y + h * 0.3], [x + h * 0.06, y + h * 0.42], [x - h * 0.06, y + h * 0.42], [x - h * 0.17, y + h * 0.3]], o.robe || 'ruby', L, { tile: t, box: bx });+ const kind = o.face || (o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ g.face(x, y + h * 0.58, h * 0.108, h * 0.132, kind, L + 0.5, false);+ if (o.crown) g.poly([[x - h * 0.1, y + h * 0.66], [x + h * 0.1, y + h * 0.66], [x + h * 0.12, y + h * 0.78], [x + h * 0.05, y + h * 0.72], [x, y + h * 0.8], [x - h * 0.05, y + h * 0.72], [x - h * 0.12, y + h * 0.78]], 'gold', L + 0.7, { came: 0.008 });+ if (o.cross) for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[x + h * 0.17 * Math.cos(a - 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a - 0.24)], [x + h * 0.17 * Math.cos(a + 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a + 0.24)], [x, y + h * 0.58]], 'ruby', L + 0.2, { came: 0.008 }); }+}+// A jewelled ring of short leaded segments+function gRim(g, cx, cy, r0, r1, cols, n, layer) {+ g.disc(cx, cy, r1 + 0.02, 'lead', layer - 0.3, Math.max(10, n), { came: 0, jitter: 0 });+ for (let i = 0; i < n; i++) {+ const a = i * TAU / n, b = (i + 1) * TAU / n;+ g.poly([[cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)], [cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)], [cx + r1 * Math.cos(b), cy + r1 * Math.sin(b)], [cx + r0 * Math.cos(b), cy + r0 * Math.sin(b)]], cols[i % cols.length], layer, { came: -0.01 });+ }+}+// A lancet told as a stack of medallions over a diapered ground, with a jewelled border+function gLancetStory(g, x, w, y0, ys, rise, scenes, o = {}) {+ const c1 = o.c1 || 'ruby', c2 = o.c2 || 'cobalt', edge = o.edge || ['gold', 'ruby', 'white', 'cobalt'];+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse().slice(0)];+ g.withClip(outline, () => {+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, o.cell || 0.6, c1, c2, 'gold');+ const H = ys + rise * 0.5 - y0, N = scenes.length, step = H / N, R = Math.min(w * 0.44, step * 0.47);+ scenes.forEach((nm, i) => {+ const cy = y0 + step * (i + 0.5);+ const ground = (i % 2) ? (o.g1 || 'cobalt') : (o.g2 || 'ruby');+ gRim(g, x, cy, R, R * 1.055, ['gold', 'ruby', 'gold', 'cobalt'], 22, 1);+ g.disc(x, cy, R, ground, 1.3, 18, { came: 0 });+ g.withClip(ringPts(x, cy, R, 14), () => g.withXf(0, x, cy, 1, () => SCENES[nm](g, R)));+ if (i < N - 1) { const yy = y0 + step * (i + 1); for (const s of [-1, 1]) { g.disc(x + s * w * 0.3, yy, w * 0.058, 'gold', 1.2, 8, { came: 0.012 }); g.disc(x + s * w * 0.3, yy, w * 0.032, 'ruby', 1.5, 6, { came: 0.008 }); } }+ });+ // Border bricks follow the outline: thin, and each brick painted with a stem of leaves+ const bw = w * 0.062, stepB = 0.34;+ const stripe = (x0, y1, y2) => { let k = 0; g.poly([[x0, y1], [x0 + bw, y1], [x0 + bw, y2], [x0, y2]], 'lead', 3.5, { came: 0, jitter: 0 }); for (let y = y1; y < y2; y += stepB) { const c = edge[k++ % edge.length]; g.poly([[x0, y], [x0 + bw, y], [x0 + bw, y + stepB], [x0, y + stepB]], c, 3.8, { came: -0.012, orn: c === 'gold' ? 'gold' : c === 'white' ? 'white' : null, box: [x0, y, x0 + bw, y + stepB] }); } };+ stripe(x - w / 2, y0, ys + rise * 0.1); stripe(x + w / 2 - bw, y0, ys + rise * 0.1);+ // Saddle bars (iron armature)+ for (let y = y0 + 1.1; y < ys + rise * 0.6; y += 1.1) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.045], [x - w / 2, y + 0.045]], 'lead', 7, { came: 0, jitter: 0 });+ });+}+// A lancet filled by one monumental standing figure under a painted canopy (reads from the floor)+function gLancetSaint(g, x, w, y0, ys, rise, fig, o = {}) {+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse()];+ const gnd = o.ground || 'cobalt';+ g.withClip(outline, () => {+ const figY = y0 + 1.05, figH = (ys + rise * 0.1 - figY) * 0.84;+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, 0.8, gnd, o.ground2 || 'ruby', null);+ // Pedestal: a band of painted gold the figure stands on+ g.poly([[x - w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 1.02], [x - w * 0.46, y0 + 1.02]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.46, y0 + 0.62, x + w * 0.46, y0 + 1.02] });+ // Canopy: a thin arch of gold, a gable with its ruby field, amber pinnacles either side+ const cy = figY + figH + 0.02, cH = ys + rise * 0.86 - cy;+ g.poly([[x - w * 0.48, cy], [x + w * 0.48, cy], [x + w * 0.48, cy + cH * 0.1], [x - w * 0.48, cy + cH * 0.1]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.48, cy, x + w * 0.48, cy + cH * 0.1] });+ g.poly([[x - w * 0.42, cy + cH * 0.08], [x + w * 0.42, cy + cH * 0.08], [x, cy + cH]], 'yellow', 1.7, { orn: 'gold', box: [x - w * 0.42, cy + cH * 0.08, x + w * 0.42, cy + cH] });+ g.poly([[x - w * 0.22, cy + cH * 0.14], [x + w * 0.22, cy + cH * 0.14], [x, cy + cH * 0.62]], 'ruby', 1.9, { came: 0.016 });+ for (const s of [-1, 1]) g.poly([[x + s * w * 0.3, cy + cH * 0.12], [x + s * w * 0.46, cy + cH * 0.12], [x + s * w * 0.38, cy + cH * 0.85]], 'amber', 1.8);+ gFigure(g, x, figY, figH, Object.assign({ layer: 2.5, sx: Math.min(1, w * 1.72 / figH) }, fig));+ for (let y = y0 + 1.5; y < ys + rise * 0.6; y += 1.5) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.05], [x - w / 2, y + 0.05]], 'lead', 9, { came: 0, jitter: 0 });+ });+}+function gOculus(g, cx, cy, r, o = {}) { // head of the window: a sexfoil with an angel or Christ+ g.withClip(ringPts(cx, cy, r, 20), () => {+ g.disc(cx, cy, r, o.ground || 'cobalt', 1, 20);+ for (let i = 0; i < 6; i++) { const a = i * PI / 3 + PI / 6; g.disc(cx + r * 0.68 * Math.cos(a), cy + r * 0.68 * Math.sin(a), r * 0.27, i % 2 ? 'ruby' : 'gold', 1.4, 8); }+ g.disc(cx, cy, r * 0.56, o.inner || 'rubyLt', 1.8, 14);+ g.withXf(0, cx, cy, 1, () => { if (o.bust) gBust(g, 0, -r * 0.45, r * 0.95, o.bust); else gFigure(g, 0, -r * 0.5, r * 0.98, { wings: 'gold', robe: 'white', mantle: 'emerald', arm: 'orant' }); });+ });+}++// A complete two-light window: lancets, oculus, base field. Local plane z=0, +Z = interior.+function makeWindowGlass(w, y0, ys, rise, seed, fill) {+ const g = new Glass(seed);+ const outline = archOutline(w, y0, ys, rise, 8);+ g.raw(outline.slice(), 'lead', -0.4, 0);+ g.withClip(outline, () => gDiaper(g, -w / 2, ys, w / 2, ys + rise, 0.55, 'cobalt', 'ruby', 'gold', 0));+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96;+ const syl = ys + rise * 0.02;+ fill.light(g, -lx, lw, y0, syl, lr, 0); fill.light(g, lx, lw, y0, syl, lr, 1);+ const oc = windowHead(w, ys, rise);+ gOculus(g, 0, oc.y, oc.r + 0.03, fill.oculus || {});+ return g.b;+}+function windowHead(w, ys, rise) { const r = w * 0.225; return { r, y: ys + rise * 0.62 }; }++// ==================================================================================+// STONE TRACERY for a two-light window (plane z=0, bars deep along Z)+// ==================================================================================+function addTracery(b, w, y0, ys, rise, o = {}) {+ const bw = o.bar || 0.26, bd = o.depth || 0.42, sec = traceryBar(bw, bd), secT = hexBar(bw * 0.62, bd * 0.8);+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96, syl = ys + rise * 0.02;+ // Central mullion with shaft ring and capital+ b.at(M.T(0, 0, 0), () => {+ b.sweep([[0, y0 - 0.05], [0, syl]], sec, {});+ b.at(M.T(0, syl, 0), () => b.lathe([[bw * 0.52, -0.3], [bw * 0.56, -0.2], [bw * 0.9, 0.02], [bw * 0.5, 0.06]], 6));+ });+ for (const s of [-1, 1]) {+ const arc = pointedArc(lw, lr, 5).map((p) => [s * lx + p[0], syl + p[1]]);+ b.sweep(arc, sec, {});+ // cusped trefoil head inside each lancet+ const cr = lw * 0.3, cy = syl + lr * 0.34;+ const cusp = []; for (let i = 0; i <= 8; i++) { const a = rad(-30 + i * 240 / 8); const R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([s * lx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, { caps: false });+ b.sweep([[s * (lx + lw / 2), cy - cr * 0.4], cusp[0]], secT, { caps: false });+ b.sweep([[s * (lx - lw / 2), cy - cr * 0.4], cusp[8]], secT, { caps: false });+ }+ // Oculus with six foils+ const oc = windowHead(w, ys, rise);+ b.sweep(ringPts(0, oc.y, oc.r, 14), sec, { closed: true });+ const foils = []; for (let i = 0; i < 18; i++) { const a = i * TAU / 18; const R = oc.r * (0.62 + 0.3 * Math.pow(Math.abs(Math.cos(a * 3)), 0.8)); foils.push([R * Math.cos(a), oc.y + R * Math.sin(a)]); }+ b.sweep(foils, secT, { closed: true, crease: 40 });+}++// ==================================================================================+// PIERCED PARAPET with quatrefoil panels+// ==================================================================================+function quatrefoil(cx, cy, r, n = 16) { const p = []; for (let i = 0; i < n; i++) { const a = i * TAU / n; const R = r * (0.72 + 0.28 * Math.abs(Math.cos(2 * a))); p.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); } return p; }+function addParapet(b, x0, x1, y, h, th, o = {}) {+ const n = Math.max(1, Math.round((x1 - x0) / (o.pitch || 1.05))), holes = [];+ const r = Math.min(h * 0.34, (x1 - x0) / n * 0.4);+ for (let i = 0; i < n; i++) holes.push(quatrefoil(x0 + (i + 0.5) * (x1 - x0) / n, y + h * 0.5, r, o.nh || 12));+ b.slab([[x0, y], [x1, y], [x1, y + h], [x0, y + h]], holes, -th / 2, th / 2, { sr: 0.6 });+ const cap = barSection(0.14, th + 0.14);+ b.sweep([[x0, y + h + 0.04], [x1, y + h + 0.04]], cap, {});+ b.sweep([[x0, y - 0.02], [x1, y - 0.02]], barSection(0.1, th + 0.1), { s: 0.8 });+}++// A wimperg: gable over a window with crockets, finial and a blind trefoil+function addGable(b, w, h, z0, th, y, o = {}) {+ const lod = o.lod === undefined ? 1 : o.lod;+ const holes = o.solid ? [] : [ringPts(0, y + h * 0.36, Math.min(w * 0.13, h * 0.2), 14)];+ if (!o.noField) b.slab([[-w / 2, y], [w / 2, y], [0, y + h]], holes, z0, z0 + th, { sf: 1, sb: 0.8 });+ const rake = barSection(0.18, th + 0.14);+ b.sweep([[-w / 2 - 0.06, y - 0.02], [0, y + h + 0.05]], rake, { z: z0 + th / 2 });+ b.sweep([[0, y + h + 0.05], [w / 2 + 0.06, y - 0.02]], rake, { z: z0 + th / 2 });+ const n = o.crockets === undefined ? Math.max(2, Math.round(Math.hypot(w / 2, h) / 1.05)) : o.crockets, sz = o.size || 0.46;+ const zc = z0 + th / 2;+ crocketsAlong(b, [-w / 2, y + 0.05, zc], [-0.03, y + h * 0.97, zc], n, sz, [-h, w / 2, 0], lod);+ crocketsAlong(b, [w / 2, y + 0.05, zc], [0.03, y + h * 0.97, zc], n, sz, [h, w / 2, 0], lod);+ b.at(M.T(0, y + h * 0.98, zc), () => addFinial(b, o.finial || 0.95, lod));+}+// A small blind trefoil-headed arcade on a wall face (plane z=0, moulding stands proud +Z)+function addBlindArcade(b, x0, x1, y0, ys, n, o = {}) {+ const w = (x1 - x0) / n, sec = hexBar(o.bar || 0.12, o.depth || 0.16), rise = w * 0.95;+ for (let i = 0; i < n; i++) {+ const x = x0 + (i + 0.5) * w;+ b.sweep(pointedArc(w * 0.9, rise, 4).map((p) => [x + p[0], ys + p[1]]), sec, { z: 0.04, caps: false });+ }+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * w;+ b.at(M.T(x, 0, 0.06), () => b.lathe([[0.1, y0], [0.065, y0 + 0.18], [0.065, ys - 0.14], [0.13, ys], [0.08, ys + 0.04]], 5));+ }+}++// ==================================================================================+// RIBBED VAULT: pointed-arc ribs, curved webs, carved or gilded boss.+// ==================================================================================+function arcH(L, H, f) { // height of a two-centred arc of span L, rise H at fraction f+ const c = (H * H - L * L / 4) / L, r = c + L / 2, d = Math.abs(f - 0.5) * L;+ return Math.sqrt(Math.max(0, r * r - (d + c) * (d + c)));+}+function addRib(b, A, B, yS, rise, sec, n = 8, half = false) { // rib between plan points A,B in their vertical plane+ const dx = B[0] - A[0], dz = B[1] - A[1], L = Math.hypot(dx, dz), ang = Math.atan2(dz, dx) * 180 / PI;+ const pts = [];+ if (half) for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L * 2, rise, t / 2)]); }+ else for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L, rise, t)]); }+ // plane: local X along A->B, local Y up+ b.at(M.of(M.T(A[0], 0, A[1]), M.Ry(-ang)), () => b.sweep(pts, sec, { caps: false }));+}+// One curved web between a side arc a-b and the crown c, faces downward whichever way the plan runs+function addVaultCell(A, a, b, c, yS, yC, sideRiseV, nS = 8, nT = 6) {+ const La = Math.hypot(c[0] - a[0], c[1] - a[1]), Lb = Math.hypot(c[0] - b[0], c[1] - b[1]), Lab = Math.hypot(b[0] - a[0], b[1] - a[1]), rise = yC - yS;+ const cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ A.grid((s, t) => {+ const px = lerp(a[0], c[0], t) * (1 - s) + lerp(b[0], c[0], t) * s, pz = lerp(a[1], c[1], t) * (1 - s) + lerp(b[1], c[1], t) * s;+ const ruled = lerp(yS + arcH(Lab, sideRiseV, s), yC, t);+ const dA = arcH(La * 2, rise, t / 2) - rise * t, dB = arcH(Lb * 2, rise, t / 2) - rise * t;+ const y = ruled + (1 - s) * (1 - s) * dA + s * s * dB;+ return [px, y, pz, 0.72 + 0.28 * t];+ }, nS, nT, { flip: cross < 0, metric: true });+}+function addRibVault(F, A, corners, yS, yC, o = {}) {+ // corners: 4 plan points in order; web cells between each side and the crown+ const c0 = corners, cx = (c0[0][0] + c0[1][0] + c0[2][0] + c0[3][0]) / 4, cz = (c0[0][1] + c0[1][1] + c0[2][1] + c0[3][1]) / 4;+ const crown = [cx, yC, cz], nS = o.nS || 8, nT = o.nT || 6, rise = yC - yS;+ const sideRise = o.sideRise || ((i) => rise * 0.94);+ for (let k = 0; k < 4; k++) {+ if (o.skipSide && o.skipSide[k]) continue;+ addVaultCell(F, c0[k], c0[(k + 1) % 4], [cx, cz], yS, yC, sideRise(k), nS, nT);+ }+ const sec = o.ribSec || rollSection(0.32, 0.42, 2, 0.04, 2);+ for (const c of c0) addRib(F, c, [cx, cz], yS, rise, sec, 8, true);+ for (let k = 0; k < 4; k++) if (!(o.skipRib && o.skipRib[k])) addRib(F, c0[k], c0[(k + 1) % 4], yS, sideRiseRib(o, k, rise), o.sideRibSec || sec, 8);+ return crown;+}+const sideRiseRib = (o, k, rise) => (o.sideRise ? o.sideRise(k) : rise * 0.94);+function addBoss(G, F, c, r = 0.42) { // carved foliate boss with gilding+ G.at(M.T(c[0], c[1] - 0.02, c[2]), () => {+ G.lathe([[r * 0.25, -r * 0.54], [r * 0.6, -r * 0.5], [r * 0.95, -r * 0.3], [r, -r * 0.08], [r * 0.7, r * 0.05]], 10);+ for (let i = 0; i < 8; i++) addLeaf(G, M.of(M.T(0, -r * 0.22, 0), M.Ry(i * 45), M.T(r * 0.72, 0, 0), M.Rz(160)), r * 0.72);+ });+}++// ==================================================================================+// CLUSTERED PIER: a lobed section of shafts around a core, with bases and foliate capitals+// ==================================================================================+function lobedRing(core, shafts, n = 64) {+ const out = [];+ for (let i = 0; i < n; i++) {+ const a = TAU * i / n, c = Math.cos(a), s = Math.sin(a);+ let best = core, nx = c, nz = s;+ for (const sh of shafts) {+ // ray from the origin against each shaft circle+ const b = c * sh[0] + s * sh[1], disc = b * b - (sh[0] * sh[0] + sh[1] * sh[1] - sh[2] * sh[2]);+ if (disc >= 0) { const t = b + Math.sqrt(disc); if (t > best) { best = t; nx = (t * c - sh[0]) / sh[2]; nz = (t * s - sh[1]) / sh[2]; } }+ }+ out.push([best * c, best * s, nx, nz]);+ }+ return out;+}+function addExtrudedRing(b, ring, y0, y1, o = {}) {+ const s0 = o.s0 === undefined ? 1 : o.s0, s1 = o.s1 === undefined ? 1 : o.s1, k0 = o.k0 || 1, k1 = o.k1 || 1, N = ring.length;+ const base = b.count; let perim = 0;+ for (let i = 0; i <= N; i++) {+ const r = ring[i % N], rp = ring[(i + N - 1) % N];+ if (i > 0) perim += Math.hypot(r[0] - rp[0], r[1] - rp[1]);+ const shade = 0.5 + 0.5 * Math.pow(Math.abs(r[2] * r[0] + r[3] * r[1]) / (Math.hypot(r[0], r[1]) || 1), 2);+ b.v(r[0] * k0, y0, r[1] * k0, r[2], 0, r[3], perim, y0, s0 * shade);+ b.v(r[0] * k1, y1, r[1] * k1, r[2], 0, r[3], perim, y1, s1 * shade);+ }+ for (let i = 0; i < N; i++) b.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+}+function addCapital(b, ring, y, h, k, o = {}) { // bell with crocket leaves on each shaft, moulded abacus+ const N = ring.length;+ addExtrudedRing(b, ring, y, y + h * 0.1, { k0: 1.0, k1: 1.03, s0: 0.8 });+ addExtrudedRing(b, ring, y + h * 0.1, y + h * 0.82, { k0: 1.03, k1: k, s0: 0.62, s1: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.82, y + h * 0.9, { k0: k, k1: k + 0.1, s0: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.9, y + h, { k0: k + 0.1, k1: k + 0.12, s0: 0.95 });+ const top = ring.map((r) => [r[0] * (k + 0.12), r[1] * (k + 0.12)]);+ const ids = top.map((p) => b.v(p[0], y + h, p[1], 0, 1, 0, undefined, undefined, 0.9)); for (let i = 1; i < N - 1; i++) b.tri(ids[0], ids[i + 1], ids[i]);+ const leaves = o.leaves || 12;+ for (let i = 0; i < leaves; i++) {+ const a = i * 360 / leaves + (o.phase || 0), idx = Math.round(i / leaves * N) % N, r = ring[idx], R = Math.hypot(r[0], r[1]);+ addLeaf(b, M.of(M.T(0, y + h * 0.1, 0), M.Ry(-a), M.T(R * 1.02, 0, 0)), h * (i % 2 ? 0.72 : 0.56), { coarse: 1 });+ }+}++// The bay plan shared by every straight module (+Z side of the axis)+const zn = P.naveSpan / 2, zaw = zn + P.aisleSpan, BAY = P.bay;+// Window openings follow the bay, so changing the bay length re-proportions the glass and tracery+const WIN = {+ aW: BAY - 1.95, aSill: 4.3, aSp: P.pierTop - 1, aRise: 5.6 * (BAY - 1.95) / 4.65,+ cW: BAY - 1.55, cSill: P.triforiumTop + 0.45, cSp: P.vaultSpring + 4.8, cRise: 6.7 * (BAY - 1.55) / 5.05,+};+const windowArgs = { aisle: [WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise], clere: [WIN.cW, WIN.cSill, WIN.cSp, WIN.cRise] };+const mkBatches = () => { const D = new Batch(1); D.noColor = true; return { A: new Batch(TILE.ashlar), F: new Batch(TILE.fine), L: new Batch(TILE.lead), G: new Batch(TILE.gold), O: new Batch(TILE.oak), I: new Batch(TILE.iron), D }; };+// A stain hanging from a ledge on a wall facing local +z: runoff, drips, soft shadow or rising damp. Few vertices, crisp detail.+const STAIN_RNG = mkRng(4242);+function addStain(D, x0, x1, y, z, drop, kind = 'streak') {+ const R = DECAL_UV[kind], n = Math.max(1, Math.round((x1 - x0) / 4)), w = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = lerp(x0, x1, i / n), c = lerp(x0, x1, (i + 1) / n), span = w * Math.min(1, (c - a) / 5), u0 = R[0] + 0.002 + STAIN_RNG() * (w - span - 0.004);+ const q = [D.v(a, y, z, 0, 0, 1, u0, R[1], 1), D.v(a, y - drop, z, 0, 0, 1, u0, R[3], 1), D.v(c, y - drop, z, 0, 0, 1, u0 + span, R[3], 1), D.v(c, y, z, 0, 0, 1, u0 + span, R[1], 1)];+ D.quad(q[0], q[1], q[2], q[3]);+ }+}+// A real recess in a wall facing local +z, whose slab already has the opening cut: back plate, moulded frame, a sill+function addRecess(A, F, outline, depth) {+ const o = area2(outline) > 0 ? outline : outline.slice().reverse();+ let x0 = 1e9, x1 = -1e9, y0 = 1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); }+ A.face(o.map((p) => [p[0], p[1], -depth]));+ F.sweep(o, rollSection(0.1, 0.22, 1, 0.03, 2), { closed: true, crease: 40, z: -0.02 });+ F.sweep([[x0 - 0.16, y0 - 0.06], [x1 + 0.16, y0 - 0.06]], stringSection(0.16, 0.14), {});+}+// A real buttress on a wall facing local +z. stages, bottom to top: { y0, y1, w: half width, f: how far its front stands out }.+// Each stage narrows and steps back, and ends in a sloped stone cap running up to the next: a solid wedge of the pier, never+// a sheet. o.back ties it into the masonry behind; the last stage ends in a weathering back to the wall, or flat with o.flat.+function addPier(set, stages, o = {}) {+ const { A, F } = set, zb = -(o.back === undefined ? 0.3 : o.back);+ const face = (m, hw, y0, y1, p) => {+ const out = [[-hw, y0], [hw, y0], [hw, y1], [-hw, y1]];+ if (!p) { A.at(m, () => A.face(out.map((q) => [q[0], q[1], 0]))); return; }+ const hole = archOutline(hw * 2 * p.k, y0 + p.b, y1 - p.t - p.r, p.r, 5);+ A.at(m, () => { A.slab(out, [hole], -0.2, 0, { noOuterSides: true, noBack: true, sr: 0.66 }); F.at(m, () => addRecess(A, F, hole, 0.2)); });+ };+ stages.forEach((s, i) => {+ const n = stages[i + 1], mid = (s.f + zb) / 2, half = (s.f - zb) / 2;+ face(M.T(0, 0, s.f), s.w, s.y0, s.y1, s.panel);+ face(M.of(M.T(s.w, 0, mid), M.Ry(90)), half, s.y0, s.y1, s.sidePanel);+ face(M.of(M.T(-s.w, 0, mid), M.Ry(-90)), half, s.y0, s.y1, s.sidePanel);+ if (s.backFace) A.face([[s.w, s.y0, zb], [-s.w, s.y0, zb], [-s.w, s.y1, zb], [s.w, s.y1, zb]]);+ if (!s.noDrip) F.box(-s.w - 0.05, s.w + 0.05, s.y1 - 0.14, s.y1, zb, s.f + 0.05, { noTop: true });+ if (!n && o.flat) { F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [s.w, s.y1, zb], [-s.w, s.y1, zb]]); return; }+ const ny = n ? n.y0 : s.y1 + (o.capH || (s.f - zb) * 0.9), nw = n ? n.w : s.w, nf = n ? n.f : zb;+ F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [nw, ny, nf], [-nw, ny, nf]]);+ F.face([[s.w, s.y1, s.f], [s.w, s.y1, zb], [nw, ny, zb], [nw, ny, nf]]);+ F.face([[-s.w, s.y1, zb], [-s.w, s.y1, s.f], [-nw, ny, nf], [-nw, ny, zb]]);+ });+}+function placeSet(name, set, pos, rot, parent, extra = {}) {+ const o = { pos, rot: [0, rot, 0], parent, scale: extra.scale };+ place(name + '_Ashlar', set.A, MAT.ashlar, o);+ place(name + '_Stone', set.F, MAT.fine, o);+ place(name + '_Lead', set.L, MAT.lead, o);+ place(name + '_Gilt', set.G, MAT.gold, o);+ place(name + '_Oak', set.O, MAT.oak, o);+ place(name + '_Iron', set.I, MAT.iron, o);+ if (set.D) place(name + '_Weathering', set.D, MAT.decal, o);+}+// Glass pair: interior face + the same panes mirrored as the exterior face+function placeGlass(name, batch, pos, rotY, parent, keepOutside, k = 1) {+ place(name + '_In', batch, keepOutside ? MAT.glassRose : MAT.glassIn, { pos, rot: [0, rotY, 0], scale: [k, k, 1], parent });+ place(name + '_Out', keepOutside ? batch : glassOutside(batch), keepOutside ? MAT.glassOut : MAT.glassOutWin, { pos, rot: [0, rotY, 0], scale: [k, k, -1], parent });+}+// By day a window is dark from outside and the story cannot be read, so its outer skin is only the+// window's outline over a mosaic of pot-metals: the colour hints through for a few triangles.+function glassOutside(batch) {+ if (batch.outer) return batch.outer;+ if (!batch.outline) return batch;+ const g = new Glass(7), o = batch.outline;+ let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); }+ g.raw(o, 'lead', -0.4, 0);+ g.withClip(o, () => gDiaper(g, x0, y0, x1, y1, 0, 'outer', null, null, 0));+ batch.outer = g.b;+ return g.b;+}++function buildNaveBay(o = {}) {+ const set = o.into || mkBatches(), { A, F, L, G } = set;+ const B = o.B || BAY, ww = o.ww || BAY - 2.1, sp = P.pierTop, archRise = o.archRise || 5.1, thick = 0.85;+ const zw = o.zw === undefined ? zn : o.zw;+ if (o.part !== 'aisle') {+ // ---- Arcade wall with the moulded pointed arch ----+ const arc = pointedArc(ww, archRise, 7).map((p) => [p[0], p[1] + sp]);+ A.at(M.T(0, 0, zw), () => {+ A.slab([[-B / 2, sp], [-ww / 2, sp], ...arc, [ww / 2, sp], [B / 2, sp], [B / 2, P.triforiumSill], [-B / 2, P.triforiumSill]], [], -thick / 2, thick / 2, { noOuterSides: true });+ });+ F.at(M.T(0, 0, zw), () => { F.sweep(arc, rollSection(0.82, thick + 0.25, 2, 0.08, 3), {}); });+ // Hood mould over the arch on the nave side, with carved label stops+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ const hood = pointedArc(ww + 1.9, archRise + 1.0, 6).map((p) => [p[0], p[1] + sp]);+ F.sweep(hood, stringSection(0.16, 0.14), {});+ });+ // ---- Triforium: string course, four cusped arches, dark wall passage ----+ const yT = P.triforiumSill, yT2 = P.triforiumTop, nT = o.nT || 4, tw = o.tw || (BAY - 1.72) / 4, ty = yT + 0.62, tspr = yT2 - 1.5, gapT = nT > 2 ? 0.08 : 0;+ const trfX = (i) => (i + 0.5 - nT / 2) * tw + (i < nT / 2 ? -gapT : gapT);+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ F.sweep([[-B / 2, yT], [B / 2, yT]], stringSection(0.34, 0.3), {});+ F.sweep([[-B / 2, yT2], [B / 2, yT2]], stringSection(0.3, 0.26), {});+ // Colonnette arcade standing in the openings+ for (let i = 0; i < nT; i++) {+ const x = trfX(i);+ const a = pointedArc(tw * 0.92, 0.98, 4).map((p) => [x + p[0], tspr + p[1]]);+ F.sweep(a, hexBar(0.13, 0.26), { z: 0.05 });+ F.sweep(ringPts(x, tspr + 0.32, 0.17, 6), hexBar(0.05, 0.1), { z: 0.05, closed: true, s: 0.9 });+ }+ for (let i = 0; i <= nT; i++) {+ const mid = i === nT / 2;+ const x = (i - nT / 2) * tw + (i < nT / 2 ? -gapT : (mid ? 0 : gapT));+ const r = mid ? 0.14 : 0.085;+ F.at(M.T(x, 0, 0.05), () => F.lathe([[r * 1.7, ty - 0.02], [r, ty + 0.2], [r, tspr - 0.2], [r * 1.9, tspr - 0.01], [r * 1.5, tspr + 0.03]], 6));+ }+ });+ const holes = []; // openings through the inner skin+ for (let i = 0; i < nT; i++) { const x = trfX(i); holes.push(archOutline(tw * 0.92, ty, tspr, 0.98, 6).map((p) => [x + p[0], p[1]])); }+ A.at(M.T(0, 0, zw - thick / 2 + 0.22), () => A.slab([[-B / 2, yT], [B / 2, yT], [B / 2, yT2], [-B / 2, yT2]], holes, -0.22, 0.22, { noOuterSides: true, sf: 0.45, sr: 0.5 }));+ // Passage back wall and floor: seen only through the arcade, kept dark+ // Round the apse the passage fans out with the radius, so the walls of neighbouring bays meet at their corners+ const zBk = zw + 0.92, zFr = zw - thick / 2 + 0.44, hB = o.wedge ? (B / 2) * zBk / zw : B / 2, hF = o.wedge ? (B / 2) * zFr / zw : B / 2;+ A.shade(0.34, () => {+ A.face([[hB, yT, zBk], [-hB, yT, zBk], [-hB, yT2 + 0.5, zBk], [hB, yT2 + 0.5, zBk]]);+ A.face([[-hB, yT + 0.02, zBk], [hB, yT + 0.02, zBk], [hF, yT + 0.02, zFr], [-hF, yT + 0.02, zFr]]);+ A.face([[-hF, yT2 + 0.2, zFr], [hF, yT2 + 0.2, zFr], [hB, yT2 + 0.2, zBk], [-hB, yT2 + 0.2, zBk]]);+ });+ // ---- Clerestory wall, window hole, tracery ----+ const cw = o.cw || WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = o.cRise || WIN.cRise;+ const yE = P.eaves;+ A.at(M.T(0, 0, zw), () => A.slab([[-B / 2, yT2], [B / 2, yT2], [B / 2, yE], [-B / 2, yE]], [archOutline(cw, cSill, cSp, cRise, 9)], -0.42, 0.42, { noOuterSides: true, sr: 0.66 }));+ F.at(M.T(0, 0, zw), () => {+ if (o.lights === 1) addLancetTracery(F, cw, cSill, cSp, cRise);+ else addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.44 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 9).slice(0), rollSection(0.14, 0.86, 1, 0.03, 2), { closed: true, s: 0.92, crease: 40 });+ });+ // Exterior: hood mould, wimperg gable, cornice, pierced parapet, corbel table+ set.D.at(M.T(0, 0, zw + 0.42), () => { addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.02, 3.6); addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.03, 0.9, 'soft'); });+ F.at(M.T(0, 0, zw + 0.42), () => {+ F.sweep(pointedArc(cw + 0.5, cRise + 0.3, 6).map((p) => [p[0], p[1] + cSp]), hexBar(0.22, 0.26), {});+ addGable(F, Math.min(cw + 0.9, B * 0.96), 3.4, 0.02, 0.2, cSp + cRise * 0.52, { size: 0.44, finial: 0.8, lod: 0, crockets: 3 });+ F.sweep([[-B / 2, yT2 - 0.02], [B / 2, yT2 - 0.02]], stringSection(0.3, 0.2), {});+ F.sweep([[-B / 2, yE - 0.12], [B / 2, yE - 0.12]], stringSection(0.34, 0.38), {});+ const nCorb = Math.round(B / 1.1);+ for (let i = 0; i < nCorb; i++) { const x = -B / 2 + (i + 0.5) * B / nCorb; F.box(x - 0.13, x + 0.13, yE - 0.62, yE - 0.32, 0, 0.26, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zw + 0.3), () => addParapet(F, -B / 2, B / 2, yE, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Upper wall top closing the gutter+ A.face([[-B / 2, yE, zw + 0.42], [B / 2, yE, zw + 0.42], [B / 2, yE, zw - 0.42], [-B / 2, yE, zw - 0.42]], 0.7);+ }+ if (o.part !== 'upper') {+ // ---- Aisle roof (lean-to in lead) ----+ if (!o.noAisleRoof) {+ const z0 = zn + 0.42, z1 = zaw + 1.05, y0r = P.aisleRoofHigh, y1r = P.aisleWallTop - 0.2;+ L.face([[-B / 2, y0r, z0], [-B / 2, y1r, z1], [B / 2, y1r, z1], [B / 2, y0r, z0]]);+ }+ if (!o.open) {+ // ---- Aisle wall ----+ const wa = WIN.aW, sill = WIN.aSill, aSp = WIN.aSp, aRise = WIN.aRise, zOut = zaw + 0.45;+ A.at(M.T(0, 0, zOut), () => A.slab([[-B / 2, 0], [B / 2, 0], [B / 2, P.aisleWallTop], [-B / 2, P.aisleWallTop]], [archOutline(wa, sill, aSp, aRise, 9)], -0.45, 0.45, { noOuterSides: true, sr: 0.7 }));+ F.at(M.T(0, 0, zOut), () => {+ addTracery(F, wa, sill, aSp, aRise, { bar: 0.25, depth: 0.42 });+ F.sweep(archOutline(wa, sill, aSp, aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ });+ // Exterior of the aisle wall+ set.D.at(M.T(0, 0, zaw + 0.9), () => {+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.02, 4.4);+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.02, 2.6, 'drip');+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.03, 0.7, 'soft');+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.03, 0.9, 'soft');+ addStain(set.D, -B / 2, B / 2, 5.2, 0.03, 5.2, 'ground');+ });+ F.at(M.T(0, 0, zaw + 0.9), () => {+ F.sweep([[-B / 2, sill - 0.2], [B / 2, sill - 0.2]], stringSection(0.34, 0.3), {});+ F.sweep(pointedArc(wa + 0.46, aRise + 0.28, 6).map((p) => [p[0], p[1] + aSp]), hexBar(0.22, 0.26), {});+ addGable(F, wa + 0.8, 3.1, 0.02, 0.2, aSp + 2.95, { size: 0.44, finial: 0.8, crockets: 4 });+ F.sweep([[-B / 2, P.aisleWallTop - 0.12], [B / 2, P.aisleWallTop - 0.12]], stringSection(0.32, 0.34), {});+ // Plinth with a chamfered weathering+ F.sweep([[-B / 2, 0.55], [B / 2, 0.55]], stringSection(0.4, 0.42), {});+ F.sweep([[-B / 2, 1.3], [B / 2, 1.3]], stringSection(0.2, 0.2), {});+ addBlindArcade(F, -B / 2 + 0.9, B / 2 - 0.9, 1.45, 2.65, 4, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(0, 0, zaw + 0.72), () => addParapet(F, -B / 2, B / 2, P.aisleWallTop, 1.0, 0.24, { pitch: 1.3 }));+ A.face([[-B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw], [-B / 2, P.aisleWallTop, zaw]], 0.7);+ // Interior dado: blind arcade with a niche for a statue+ F.at(M.of(M.T(0, 0, zaw), M.Ry(180)), () => {+ F.sweep([[-B / 2, sill - 0.14], [B / 2, sill - 0.14]], stringSection(0.26, 0.22), {});+ addBlindArcade(F, -B / 2 + 0.5, -1.05, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ addBlindArcade(F, 1.05, B / 2 - 0.5, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ F.sweep([[-B / 2, 0.42], [B / 2, 0.42]], stringSection(0.4, 0.34), {});+ // Niche canopy over the statue+ addNicheFrame(F, 0, 0.6, 3.9, 1.05);+ });+ }+ // ---- Aisle vault ----+ if (!o.noAisleVault) {+ const z0 = zn + thick / 2, z1 = zaw;+ const crown = addRibVault(F, A, [[-B / 2, z0], [B / 2, z0], [B / 2, z1], [-B / 2, z1]], sp, P.aisleCrown - 0.3, { sideRise: (k) => (k % 2 === 0 ? (k === 0 ? archRise + 0.1 : 5.0) : 5.2), nS: 6, nT: 4, ribSec: rollSection(0.26, 0.34, 2, 0.03, 2) });+ addBoss(G, F, crown, 0.32);+ }+ }+ return set;+}+// Canopied niche frame: moulded opening, gablet with crockets. Statue placed separately.+function addNicheFrame(F, x, y0, yTop, w) {+ F.at(M.T(x, 0, 0), () => {+ F.shade(0.5, () => F.face([[-w / 2, y0, -0.005], [w / 2, y0, -0.005], [w / 2, yTop - 0.75, -0.005], [0, yTop - 0.1, -0.005], [-w / 2, yTop - 0.75, -0.005]]));+ F.box(-w / 2 - 0.05, w / 2 + 0.05, y0 - 0.14, y0 + 0.08, 0, 0.5, { s: 0.92 });+ F.sweep(archOutline(w, y0, yTop - 0.82, 0.72, 5).slice(2), barSection(0.11, 0.2), { z: 0.08 });+ for (const s of [-1, 1]) { F.at(M.T(s * w / 2, 0, 0.1), () => F.lathe([[0.07, y0 + 0.05], [0.05, y0 + 0.12], [0.05, yTop - 0.9], [0.09, yTop - 0.82], [0.05, yTop - 0.78]], 6)); }+ addGable(F, w + 0.24, 0.92, 0.06, 0.1, yTop - 0.98, { size: 0.19, crockets: 2, finial: 0.32, solid: true, lod: 0 });+ });+}++// Clustered nave pier with the vault shafts rising to the springing, on +Z = nave... shafts toward -Z+function buildPier() {+ const set = mkBatches(), { F, A, G } = set;+ const core = 0.8, big = 0.25, small = 0.14;+ const shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.1) * Math.cos(a), (core + 0.1) * Math.sin(a), big]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; shafts.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 48);+ const yB = 1.15, yC = P.pierTop - 1.05;+ // moulded base: octagonal plinth, roll, scotia, then the lobed shaft+ F.lathe([[1.7, 0], [1.7, 0.56], [1.58, 0.62]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.62, 0.86, { k0: 1.24, k1: 1.27 });+ addExtrudedRing(F, ring, 0.86, yB, { k0: 1.27, k1: 1.0, s0: 0.75, s1: 0.72 });+ addExtrudedRing(F, ring, yB, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.05, 1.2, { leaves: 14 });+ // The vault shafts rising from the capital up the nave wall (toward -Z)+ const upper = [[0, -(core + 0.1), big], [-0.5, -(core - 0.04), small], [0.5, -(core - 0.04), small]];+ for (const u of upper) {+ F.at(M.T(u[0], 0, u[1] + 0.44), () => {+ const r = u[2], yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, P.triforiumSill - 0.1], [r * 1.5, P.triforiumSill + 0.06], [r, P.triforiumSill + 0.22], [r, yS - 0.85], [r * 1.5, yS - 0.8], [r * 1.14, yS - 0.7], [r * 2.2, yS]], 8);+ for (let i = 0; i < 4; i++) addLeaf(F, M.of(M.T(0, yS - 0.72, 0), M.Ry(i * 90 + 45), M.T(r * 1.1, 0, 0)), 0.56, { coarse: 1 });+ });+ }+ // Statue stage on the nave face: corbel below, canopy above (the figure is placed separately)+ const zF = -(core + 0.34);+ F.at(M.T(0, 0, zF), () => {+ F.lathe([[0.1, 5.0], [0.28, 5.25], [0.44, 5.6], [0.5, 5.72], [0.44, 5.78]], 8, { s: 0.85 });+ for (let i = 0; i < 6; i++) addLeaf(F, M.of(M.T(0, 5.04, 0), M.Ry(i * 60), M.T(0.14, 0, 0)), 0.66, { coarse: 1 });+ // Canopy over the statue: three arched gablets and a little spire+ F.at(M.T(0, 8.25, 0), () => {+ F.lathe([[0.5, -0.02], [0.54, 0.06], [0.42, 0.12], [0.3, 0.6], [0.02, 2.0]], 6, { faceted: true, phase: PI / 6 });+ for (let i = 0; i < 3; i++) F.at(M.of(M.Ry(-60 + i * 60 + 180), M.T(0, 0, 0.44)), () => addGable(F, 0.62, 0.62, -0.03, 0.06, 0.02, { size: 0.15, crockets: 1, finial: 0.2, solid: true, lod: 0 }));+ });+ });+ return set;+}+// The hemicycle's pier: the nave pier's family, slimmer, with one vault shaft running up to the springing+function buildApsePier() {+ const set = mkBatches(), { F } = set, core = 0.5, big = 0.15, small = 0.09, shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.06) * Math.cos(a), (core + 0.06) * Math.sin(a), big]); }+ for (let i = 0; i < 4; i++) { const a = PI / 4 + i * PI / 2; shafts.push([(core + 0.01) * Math.cos(a), (core + 0.01) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 32), yB = 0.86, yC = P.pierTop - 0.82;+ F.lathe([[1.02, 0], [1.02, 0.46], [0.94, 0.52]], 8, { faceted: true, phase: PI / 8 });+ addExtrudedRing(F, ring, 0.52, yB, { k0: 1.26, k1: 1.0 });+ addExtrudedRing(F, ring, yB, yC, {});+ addCapital(F, ring, yC, 0.82, 1.22, { leaves: 10 });+ F.at(M.T(0, 0, -(core + 0.06) + 0.24), () => {+ const r = big, yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, yS - 0.5], [r * 1.5, yS - 0.42], [r * 2.1, yS]], 8);+ });+ return set;+}+// Flying buttress unit at a bay boundary: massive pier, two arched flyers, tabernacle and pinnacles+const BUTTRESS = { hw: 0.98, zB: 19.2, zStack: 15.3, zT: 16.3 };+const flyerRise = (yA, yB, t) => yA + (yB - yA) * lerp(t, Math.sin(t * PI / 2), 0.6);+function flyerShape(zA, yA, zB, yB, thick) { // A at the pier (outer), B at the wall (inner); returns polygon points in (z,y)+ const N = 12, under = [], top = [];+ for (let i = 0; i <= N; i++) {+ const t = i / N, z = lerp(zA, zB, t), y = flyerRise(yA, yB, t);+ under.push([z, y]);+ }+ // The coping is a straight line clear above the arch: deep over the pier, close over the wall, the lights open between+ const topA = [zA - 0.05, yA + 2.5], topB = [zB, yB + 0.72];+ return { under, topA, topB };+}+function buildButtress() {+ const set = mkBatches(), { A, F, G } = set;+ const { hw, zB, zStack, zT } = BUTTRESS, z0 = zaw + 0.9, yStack = 30.6;+ // A solid pier tied into the aisle wall: three stages narrowing and stepping back under sloped caps, each with a real+ // recessed panel, then a slimmer stack at the rear that rises above the aisle roof to take the flyers+ const lower = { k: 0.56, b: 1.5, t: 1.6, r: 0.8 }, flank = { k: 0.56, b: 1.5, t: 1.5, r: 1.1 };+ A.at(M.T(0, 0, z0), () => F.at(M.T(0, 0, z0), () => addPier(set, [+ { y0: 0, y1: 8.4, w: hw, f: zB - z0, panel: lower, sidePanel: flank },+ { y0: 9.6, y1: 15.8, w: hw - 0.08, f: zB - z0 - 0.9, sidePanel: flank },+ { y0: 17, y1: 23.4, w: hw - 0.14, f: zT + 1 - z0, noDrip: true },+ { y0: 23.4, y1: yStack, w: hw - 0.24, f: zStack - z0, backFace: true },+ ], { capH: 1.1 })));+ // Moulded plinth around the base: a closed block the pier rises out of+ F.at(M.T(0, 0, z0), () => { F.box(-hw - 0.16, hw + 0.16, 0, 0.52, -0.2, zB - z0 + 0.16, { noBot: true, s: 0.9 }); F.box(-hw - 0.08, hw + 0.08, 0.52, 0.86, -0.2, zB - z0 + 0.08, { noBot: true, noTop: true, taper: 0.06, taperZ: 0.06 }); });+ set.D.at(M.T(0, 0, zB), () => { addStain(set.D, -hw, hw, 8.2, 0.02, 3.2); addStain(set.D, -hw, hw, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -hw, hw, 5, 0.03, 4.4, 'ground'); });+ // Rain and shadow down both flanks of the pier, below each cap+ for (const s of [-1, 1]) {+ const d1 = zB - z0, d2 = d1 - 0.9;+ set.D.at(M.of(M.T(s * hw, 0, z0 + d1 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.02, 3.2); addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -d1 / 2, d1 / 2, 5, 0.03, 4.8, 'ground'); });+ set.D.at(M.of(M.T(s * (hw - 0.08), 0, z0 + d2 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.02, 2.8); addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.03, 0.7, 'soft'); });+ }+ F.at(M.T(0, 0, zB), () => addGable(F, hw * 1.7, 1.4, 0.02, 0.14, 8.0, { size: 0.3, crockets: 2, finial: 0.5, solid: true, lod: 0 }));+ // Tabernacle of colonnettes at the top of the pier holding an angel+ const yT0 = 23.4, yT1 = 28.4;+ F.box(-hw + 0.1, hw - 0.1, yT0 - 0.02, yT0 + 0.1, zStack - 0.02, zT + 1, { s: 0.95 });+ for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) F.at(M.T(sx * 0.72, 0, zT + sz * 0.72), () => F.lathe([[0.2, yT0], [0.14, yT0 + 0.12], [0.1, yT0 + 0.2], [0.1, yT1 - 0.22], [0.14, yT1 - 0.15], [0.22, yT1]], 8));+ // Canopy: arched, gabled and crowned by the crocketed pinnacle+ for (let i = 0; i < 4; i++) F.at(M.of(M.T(0, 0, zT), M.Ry(i * 90), M.T(0, 0, 0.72)), () => {+ F.sweep(pointedArc(1.3, 1.25, 6).map((p) => [p[0], yT1 - 1.25 + p[1] - 0.02]), traceryBar(0.1, 0.16), {});+ addGable(F, 1.7, 1.25, -0.06, 0.12, yT1, { size: 0.28, crockets: 2, finial: 0.48, lod: 0, solid: true });+ });+ F.box(-0.92, 0.92, yT1 - 0.06, yT1 + 0.18, zT - 0.92, zT + 0.92, { s: 0.9 });+ F.at(M.T(0, yT1 + 0.18, zT), () => addPinnacle(F, 1.42, 8.4, { lod: 1 }));+ // Two flyers, each a true arch: a moulded rib springing from the pier, a thin plate pierced by a row of+ // pointed lights standing on it, and a straight sloped coping with a gabled top shedding the rain+ const fly = (zA, yA, zB, yB, th) => {+ const { under, topA, topB } = flyerShape(zA, yA, zB, yB, th);+ const band = 0.34, upper = under.map((p) => [p[0], p[1] + band]);+ const onTop = (z) => lerp(topA[1], topB[1], (z - topA[0]) / (topB[0] - topA[0]));+ const onRib = (t) => flyerRise(yA, yB, t) + band;+ const holes = [], N = 6;+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.16) / N, t1 = (i + 0.84) / N, z0 = lerp(zA, zB, t0), z1 = lerp(zA, zB, t1), u0 = onRib(t0) + 0.04, u1 = onRib(t1) + 0.04, a0 = onTop(z0) - 0.1, a1 = onTop(z1) - 0.1, h = Math.min(a0 - u0, a1 - u1);+ if (h < 0.22) continue;+ const r = Math.min(h * 0.42, Math.abs(z1 - z0) * 0.8), hole = [[z0, u0], [z1, u1], [z1, a1 - r], [(z0 + z1) / 2, (a0 + a1) / 2], [z0, a0 - r]];+ holes.push(area2(hole) > 0 ? hole : hole.slice().reverse());+ }+ F.at(M.Ry(-90), () => {+ F.slab([...upper, topB, topA], holes, -0.12, 0.12, { sr: 0.7, so: 0.8 });+ F.slab([...under, ...upper.slice().reverse()], [], -0.27, 0.27, {});+ F.sweep(under, rollSection(0.12, 0.34, 1, 0.03, 2), { caps: false });+ });+ const ye = -0.03, yr = 0.17, we = 0.33, a = [topA[1], topA[0]], c = [topB[1], topB[0]];+ F.face([[-we, a[0] + ye, a[1]], [0, a[0] + yr, a[1]], [0, c[0] + yr, c[1]], [-we, c[0] + ye, c[1]]]);+ F.face([[0, a[0] + yr, a[1]], [we, a[0] + ye, a[1]], [we, c[0] + ye, c[1]], [0, c[0] + yr, c[1]]]);+ F.face([[-we, c[0] + ye, c[1]], [we, c[0] + ye, c[1]], [we, a[0] + ye, a[1]], [-we, a[0] + ye, a[1]]]);+ const n = Math.round(Math.hypot(topA[0] - topB[0], topA[1] - topB[1]) / 1.4);+ crocketsAlong(F, [0, topA[1] + 0.14, topA[0]], [0, topB[1] + 0.14, topB[0]], n, 0.5, [0, 1, 0], 0);+ };+ fly(zStack - 0.1, 28.0, zn + 1.12, 35.6, 0.48);+ fly(zStack - 0.1, 22.0, zn + 1.12, 28.6, 0.54);+ // Upper pier on the clerestory wall where the flyers land, with its own pinnacle above the parapet+ const yE = P.eaves;+ A.at(M.T(0, 0, zn + 0.42), () => F.at(M.T(0, 0, zn + 0.42), () => addPier(set, [{ y0: P.aisleRoofHigh - 1, y1: yE + 1.2, w: 0.62, f: 0.72, noDrip: true }], { back: 0, capH: 0.75 })));+ F.at(M.T(0, yE + 1.2, zn + 0.74), () => addPinnacle(F, 0.9, 6.4, { simple: 1 }));+ F.at(M.T(0, 0, zn + 1.14), () => { F.lathe([[0.16, 31.2], [0.1, 31.4], [0.1, 40.5], [0.17, 40.8]], 8); });+ // Interior respond on the aisle wall: a small cluster carrying the aisle vault+ const ringR = lobedRing(0.32, [[0, -0.44, 0.17], [-0.34, -0.26, 0.11], [0.34, -0.26, 0.11]], 26);+ F.at(M.T(0, 0, zaw), () => {+ addExtrudedRing(F, ringR, 0.4, P.pierTop - 0.8, { s0: 0.8 });+ addCapital(F, ringR, P.pierTop - 0.8, 0.8, 1.2, { leaves: 6 });+ F.lathe([[0.62, 0], [0.62, 0.42], [0.42, 0.5]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ });+ // Spout from the aisle gutter+ return set;+}++function addLancetTracery(b, w, y0, ys, rise) { // single-light frame with a cusped head+ const secT = traceryBar(0.13, 0.3), cr = w * 0.32, cy = ys + rise * 0.34, cusp = [];+ for (let i = 0; i <= 12; i++) { const a = rad(-30 + i * 240 / 12), R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, {});+ b.sweep([[w / 2, cy - cr * 0.4], cusp[0]], secT, {});+ b.sweep([[-w / 2, cy - cr * 0.4], cusp[12]], secT, {});+}+function makeLancetGlass(w, y0, ys, rise, seed, fig, o) {+ const g = new Glass(seed);+ g.raw(archOutline(w, y0, ys, rise, 8), 'lead', -0.4, 0);+ gLancetSaint(g, 0, w, y0, ys, rise, fig, o || {});+ return g.b;+}++// ==================================================================================+// PLAN: everything is derived from the parameters at the top of the file.+// ==================================================================================+const LAY = (() => {+ const xW = P.xWest, xTowerE = xW - P.towerDepth, xNaveE = xTowerE - P.naveBays * BAY, xc = xNaveE - zn, xChoirW = xc - zn, xa = xChoirW - P.choirBays * BAY;+ const ridge = P.eaves + (zn + 1) * P.roofPitch;+ return { xW, xTowerE, xNaveE, xc, xChoirW, xa, ridge };+})();+// A rigid module frame: rotation about Y at an origin on the ground+const frame = (th, ox, oz) => {+ const c = Math.cos(rad(th)), s = Math.sin(rad(th));+ return { th, ox, oz, pos: (x, y, z) => [ox + x * c + z * s, y, oz - x * s + z * c], rot: (d = 0) => [0, th + d, 0] };+};+const SAINTS = ['apostle', 'peter', 'paul', 'bishop', 'prophet', 'martyr', 'paul', 'apostle', 'bishop', 'prophet'];++// Baked floor light: dim towards the walls and the corners++function buildVaultNave() {+ const s = mkBatches();+ const crown = addRibVault(s.F, s.A, [[-BAY / 2, -zn + 0.42], [BAY / 2, -zn + 0.42], [BAY / 2, zn - 0.42], [-BAY / 2, zn - 0.42]], P.vaultSpring, P.vaultCrown, { sideRise: (k) => (P.vaultCrown - P.vaultSpring) * (k % 2 === 0 ? 0.86 : 0.99), nS: 10, nT: 8 });+ addBoss(s.G, s.F, crown, 0.55);+ return s;+}++// ---- Chevet: hemicycle, ambulatory and a radiating chapel per sector ----------------+const CHEV = (() => {+ const d = 180 / P.apseSides, h = rad(d / 2), Ra = zn * Math.cos(h), hs = zn * Math.sin(h), Ro = zaw * Math.cos(h), hso = zaw * Math.sin(h);+ const Rc = 2.5, zc = Ro + 3.0;+ return { d, h, Ra, hs, Ro, hso, Rc, zc };+})();+function buildChevetSector() {+ const set = mkBatches(), { A, F, L, G } = set, { Ra, hs, Ro, hso, Rc, zc } = CHEV;+ buildNaveBay({ into: set, part: 'upper', B: 2 * hs, zw: Ra, wedge: true, ww: 1.62, archRise: 5.0, nT: 2, tw: 1.05, cw: 2.05, cRise: 4.3, lights: 1 });+ // Hemicycle vault: one web from this side up to the crown over the apse centre+ const yS = P.vaultSpring, yC = P.vaultCrown;+ addVaultCell(F, [-hs + 0.0, Ra - 0.42], [hs, Ra - 0.42], [0, 0], yS, yC, 9.6, 7, 8);+ const sec = rollSection(0.32, 0.42, 2, 0.04, 2);+ addRib(F, [-hs, Ra - 0.42], [hs, Ra - 0.42], yS, 9.6, sec, 6);+ addRib(F, [-zn * Math.sin(CHEV.h), (zn - 0.42) * Math.cos(CHEV.h)], [0, 0], yS, yC - yS, sec, 9, true);+ // Ambulatory vault (trapezoid)+ const c2 = addRibVault(F, A, [[-hs, Ra + 0.42], [hs, Ra + 0.42], [hso, Ro], [-hso, Ro]], P.pierTop, P.aisleCrown - 0.3, { sideRise: (k) => (k === 0 ? 5.1 : k === 2 ? 5.2 : 5.1), nS: 6, nT: 4, ribSec: rollSection(0.24, 0.32, 2, 0.03, 2) });+ addBoss(G, F, c2, 0.3);+ // Outer wall with the arched chapel mouth+ const yW = P.aisleWallTop, mw = 3.55, mS = 8.6, mR = 4.8;+ const mouth = pointedArc(mw, mR, 8).map((p) => [p[0], p[1] + mS]);+ A.at(M.T(0, 0, Ro + 0.3), () => A.slab([[-hso, 0], [-mw / 2, 0], [-mw / 2, mS], ...mouth, [mw / 2, mS], [mw / 2, 0], [hso, 0], [hso, yW], [-hso, yW]], [], -0.3, 0.3, { noOuterSides: true, sr: 0.7 }));+ // The moulding fills the whole wall thickness: it is also the soffit of the opening+ F.at(M.T(0, 0, Ro + 0.3), () => F.sweep(mouth, rollSection(0.42, 0.96, 2, 0.05, 2), {}));+ set.D.at(M.T(0, 0, Ro + 0.6), () => { addStain(set.D, -hso, hso, yW - 0.42, 0.02, 3.4); addStain(set.D, -hso, hso, yW - 0.42, 0.03, 0.8, 'soft'); });+ F.at(M.T(0, 0, Ro + 0.6), () => { F.sweep([[-hso, yW - 0.12], [hso, yW - 0.12]], stringSection(0.3, 0.3), {}); addParapet(F, -hso, hso, yW, 1.0, 0.24, { pitch: 1.1 }); });+ A.face([[-hso, yW, Ro + 0.6], [hso, yW, Ro + 0.6], [hso, yW, Ro], [-hso, yW, Ro]], 0.7);+ // Ambulatory lean-to roof+ const zR0 = Ra + 0.42, hsR0 = (zR0 / Ra) * hs, zR1 = Ro + 0.9, hsR1 = (zR1 / Ro) * hso;+ L.face([[-hsR0, P.aisleRoofHigh, zR0], [-hsR1, yW - 0.2, zR1], [hsR1, yW - 0.2, zR1], [hsR0, P.aisleRoofHigh, zR0]]);+ // Hipped roof over the hemicycle+ const yE = P.eaves + 0.15, Re = zn + 0.72, hRe = Re * Math.sin(CHEV.h), aRe = Re * Math.cos(CHEV.h);+ L.grid((u, v) => [lerp(-hRe, hRe, u) * (1 - v), lerp(yE, LAY.ridge, v), aRe * (1 - v)], 2, 2, { flip: false, metric: true });+ // Radiating chapel: five faceted walls, each with a tall lancet, rib vault, conical lead roof+ const nF = P.chapelFacets, span = 216, yC1 = 16.2, dF = span / nF, ap = Rc * Math.cos(rad(dF / 2)), halfF = Rc * Math.sin(rad(dF / 2)), lw = 1.3, sill = 3.2, lSp = 9.9, lR = 2.5;+ const verts = [];+ for (let i = 0; i <= nF; i++) { const f = rad(-span / 2 + i * dF); verts.push([Rc * Math.sin(f), zc + Rc * Math.cos(f)]); }+ for (let i = 0; i < nF; i++) {+ const fm = -span / 2 + (i + 0.5) * dF;+ const m = M.of(M.T(0, 0, zc), M.Ry(fm), M.T(0, 0, ap));+ A.at(m, () => A.slab([[-halfF, 0], [halfF, 0], [halfF, yC1], [-halfF, yC1]], [archOutline(lw, sill, lSp, lR, 7)], 0, 0.5, { noOuterSides: true, sr: 0.68 }));+ F.at(m, () => {+ addLancetTracery(F, lw, sill, lSp, lR);+ F.sweep(archOutline(lw, sill, lSp, lR, 5), hexBar(0.12, 0.56), { closed: true, z: 0.25, s: 0.9, crease: 40 });+ });+ // Weather on the chapel walls: streaks and shadow under the cornice and the sill, damp at the foot+ set.D.at(M.of(m, M.T(0, 0, 0.5)), () => {+ addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.02, 3); addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.03, 0.7, 'soft');+ addStain(set.D, -halfF, halfF, sill - 0.3, 0.02, 2, 'drip'); addStain(set.D, -halfF, halfF, 3, 0.03, 3, 'ground');+ });+ F.at(M.of(m, M.T(0, 0, 0.5)), () => {+ F.sweep([[-halfF - 0.1, sill - 0.14], [halfF + 0.1, sill - 0.14]], stringSection(0.24, 0.2), { caps: false });+ F.sweep([[-halfF - 0.1, 0.5], [halfF + 0.1, 0.5]], stringSection(0.36, 0.3), { caps: false });+ F.sweep([[-halfF - 0.1, yC1 - 0.1], [halfF + 0.1, yC1 - 0.1]], stringSection(0.3, 0.3), { caps: false });+ addGable(F, lw + 0.9, 2.4, 0.01, 0.14, lSp + 2.15, { size: 0.32, crockets: 2, finial: 0.56, lod: 0 });+ addParapet(F, -halfF, halfF, yC1, 0.82, 0.18, { pitch: 1.3, nh: 8 });+ });+ addVaultCell(F, verts[i], verts[i + 1], [0, zc], 9.2, 14.6, 3.6, 5, 4);+ addRib(F, verts[i], verts[i + 1], 9.2, 3.6, rollSection(0.16, 0.22, 1, 0.02, 2), 5);+ }+ addVaultCell(F, verts[nF], verts[0], [0, zc], 9.2, 14.6, 4.2, 5, 4);+ for (let i = 0; i <= nF; i++) {+ addRib(F, verts[i], [0, zc], 9.2, 5.4, rollSection(0.17, 0.24, 1, 0.02, 2), 6, true);+ // Exterior angle buttress and little pinnacle; interior wall shaft+ const f = -span / 2 + i * dF, ve = verts[i];+ F.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => {+ A.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => addPier(set, [{ y0: 0, y1: 8.8, w: 0.3, f: 1.05 }, { y0: 9.7, y1: 14.2, w: 0.25, f: 0.72, noDrip: true }], { back: 0.1, flat: true })); // F is already in this frame+ F.at(M.T(0, 14.18, 0.62), () => addPinnacle(F, 0.62, 3.9, { simple: 1 }));+ F.lathe([[0.17, 0], [0.09, 0.6], [0.09, 9.1], [0.17, 9.2]], 5);+ });+ }+ // The neck joining the chapel to the ambulatory: side walls, a pointed barrel vault, a lead cap+ const zN0 = Ro + 0.6, zN1 = verts[0][1], hN0 = mw / 2, hN1 = Math.abs(verts[0][0]);+ for (const s of [-1, 1]) A.prism([[s * hN0, zN0 - 0.05], [s * hN1, zN1 + 0.05], [s * (hN1 + 0.5), zN1 + 0.05], [s * (hN0 + 0.5), zN0 - 0.05]], 0, yC1);+ // Same subdivision as the chapel's last web so the seam has no cracks; a rib covers the joint+ A.grid((u, v) => { const hw = lerp(hN0, hN1, v); return [lerp(-hw, hw, u), 9.2 + arcH(2 * hw, 4.2, u), lerp(Ro + 0.3, zN1 + 0.02, v), 0.8]; }, 5, 1, { flip: false }); // starts under the arch moulding, so no ray can pass above it+ addRib(F, verts[nF], verts[0], 9.2, 4.2, rollSection(0.16, 0.26, 1, 0.02, 2), 5);+ L.face([[-(hN0 + 0.5), yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN0 - 0.1], [-(hN0 + 0.5), yC1 + 0.03, zN0 - 0.1]]);+ // Lead cone+ L.at(M.T(0, 0, zc), () => { const n = 10, rb = Rc + 0.5; L.grid((u, v) => { const a = -PI + TAU * u, r = rb * (1 - v), y = lerp(yC1 + 0.05, yC1 + 7.4, v); return [r * Math.sin(a), y, r * Math.cos(a)]; }, n, 2, { flip: false, wrapU: true }); });+ G.at(M.T(0, yC1 + 7.3, zc), () => addFinial(G, 1.25, 1));+ return set;+}++// ---- Lead roofs with metric UVs so the seams run down the slope ----------------------+function addRoofRun(L, x0, x1, half, yE, yR) {+ const slope = Math.hypot(half, yR - yE);+ for (const s of [-1, 1]) {+ const a = L.v(x0, yE, s * half, 0, half, s * (yR - yE), x0, 0, 1), b = L.v(x1, yE, s * half, 0, half, s * (yR - yE), x1, 0, 1);+ const c = L.v(x1, yR, 0, 0, half, s * (yR - yE), x1, slope, 1), d = L.v(x0, yR, 0, 0, half, s * (yR - yE), x0, slope, 1);+ if (s > 0) L.quad(a, b, c, d); else L.quad(b, a, d, c);+ // A raised roll every second seam, running from the eaves to the ridge: a low ridge of two faces standing on the sheet+ const ny = half / slope, nz = s * (yR - yE) / slope, h = 0.08, w = 0.055;+ for (let x = Math.ceil(x0 / 2) * 2; x < x1; x += 2) {+ const A0 = [x - w, yE, s * half], B0 = [x + w, yE, s * half], A1 = [x - w, yR, 0], B1 = [x + w, yR, 0], T0 = [x, yE + ny * h, s * half + nz * h], T1 = [x, yR + ny * h, nz * h];+ if (s > 0) { L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]); } else { L.face([A1, T1, T0, A0]); L.face([T1, B1, B0, T0]); }+ }+ }+}+function buildCresting(len) { // gilded fleur-de-lis cresting for a length of ridge+ const G = new Batch(TILE.gold), n = Math.round(len / 0.9);+ G.box(-len / 2, len / 2, -0.04, 0.05, -0.05, 0.05, {});+ for (let i = 0; i < n; i++) {+ const x = -len / 2 + (i + 0.5) * len / n;+ G.at(M.T(x, 0.05, 0), () => {+ G.tube([[0, 0, 0], [0, 0.34, 0], [0, 0.62, 0]], (t) => [lerp(0.05, 0.005, t), lerp(0.035, 0.005, t)], 4);+ for (const s of [-1, 1]) G.tube(spline([[0, 0.14, 0], [s * 0.15, 0.26, 0], [s * 0.2, 0.42, 0], [s * 0.1, 0.46, 0]], 1), (t) => lerp(0.035, 0.008, t), 4);+ G.box(-0.08, 0.08, 0.12, 0.17, -0.03, 0.03, {});+ });+ }+ return G;+}++// ---- Crossing: four great piers, four arches, a gilded boss, the fleche above ----------+function buildCrossingPier() {+ const set = mkBatches(), { F } = set;+ const core = 1.08, sh = [];+ for (let i = 0; i < 8; i++) { const a = i * PI / 4; sh.push([(core + 0.12) * Math.cos(a), (core + 0.12) * Math.sin(a), i % 2 ? 0.2 : 0.3]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; sh.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), 0.13]); }+ const ring = lobedRing(core, sh, 80), yC = P.vaultSpring - 1.1;+ F.lathe([[2.05, 0], [2.05, 0.6], [1.9, 0.68]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.68, 0.94, { k0: 1.2, k1: 1.23 });+ addExtrudedRing(F, ring, 0.94, 1.25, { k0: 1.23, k1: 1, s0: 0.75, s1: 0.75 });+ addExtrudedRing(F, ring, 1.25, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.1, 1.14, { leaves: 16 });+ return set;+}+function buildAislePier() {+ const set = mkBatches(), { F } = set;+ const sh = []; for (let i = 0; i < 4; i++) { const a = i * PI / 2; sh.push([0.62 * Math.cos(a), 0.62 * Math.sin(a), 0.2]); }+ const ring = lobedRing(0.54, sh, 40);+ F.lathe([[1.15, 0], [1.15, 0.5], [1.02, 0.56]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.56, 0.86, { k0: 1.28, k1: 1, s0: 0.75 });+ addExtrudedRing(F, ring, 0.86, P.pierTop - 0.9, { s0: 0.8 });+ addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });+ return set;+}+function buildFleche(parent, x, z) {+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold), F = new Batch(TILE.fine);+ const y0 = LAY.ridge - 2.5, yB = LAY.ridge + 7, yT = P.flecheTip - 3.2, R = 2.9, n = 8;+ // Octagonal base and open arcaded lantern+ L.lathe([[R + 0.4, y0], [R + 0.25, y0 + 2.8], [R + 0.5, y0 + 3], [R + 0.5, y0 + 3.3], [R, y0 + 3.5]], n, { faceted: true, phase: PI / 8 });+ L.lathe([[R * 0.78, y0 + 3.5], [R * 0.78, yB + 0.3]], n, { faceted: true, phase: PI / 8, s: 0.34 });+ for (let i = 0; i < n; i++) {+ const a = i * 45, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ L.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap)), () => {+ for (const s of [-1, 1]) L.tube([[s * side * 0.5, y0 + 3.5, 0], [s * side * 0.5, yB, 0]], 0.13, 6, {});+ const arc = pointedArc(side * 0.92, 2.2, 6).map((p) => [p[0], yB - 2.45 + p[1]]);+ L.sweep(arc, traceryBar(0.13, 0.2), {});+ G.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap + 0.1)), () => addGable(G, side * 1.04, 1.6, -0.05, 0.08, yB - 0.24, { size: 0.26, crockets: 2, finial: 0.44, lod: 0, solid: true }));+ });+ }+ L.lathe([[R + 0.2, yB - 0.05], [R + 0.34, yB + 0.1], [R + 0.2, yB + 0.26]], n, { faceted: true, phase: PI / 8 });+ // The spire, its ribs and gilded crockets+ L.lathe([[R * 0.98, yB + 0.26], [R * 0.72, yB + 3.2], [0.1, yT]], n, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < n; i++) {+ const a = rad(i * 45 + 22.5 + 22.5), c = Math.cos(a), s = Math.sin(a);+ const pts = [[R * 0.98 * c * 1.0, yB + 0.3, R * 0.98 * s], [R * 0.72 * c, yB + 3.2, R * 0.72 * s], [0.14 * c, yT - 0.2, 0.14 * s]];+ G.tube(pts, (t) => lerp(0.1, 0.03, t), 5);+ crocketsAlong(G, pts[1], pts[2], 7, 0.66, [c, 0.1, s], 0);+ }+ G.at(M.T(0, yT - 0.15, 0), () => {+ G.lathe([[0.14, 0], [0.3, 0.28], [0.34, 0.5], [0.22, 0.72], [0.06, 0.9], [0.05, 3.2]], 10);+ G.box(-0.06, 0.06, 2.2, 3.35, -0.06, 0.06, {}); G.box(-0.6, 0.6, 2.56, 2.72, -0.06, 0.06, {});+ });+ place('Fleche_Lead', L, MAT.lead, { pos: [x, 0, z], parent });+ place('Fleche_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });+}++// Coloured light thrown onto the paving by the south aisle windows (sun from the south-west)+// The light of a two-light window thrown across the floor. Colour and opacity live on the vertices of a fine grid, computed+// from smooth functions, so there is no texture edge anywhere: every change of colour is a gradient.+function buildLightPool(seed) {+ const sill = WIN.aSill, sp = WIN.aSp, rise = WIN.aRise, w = WIN.aW, top = sp + rise, nu = 18, nv = 40, r = mkRng(seed * 977 + 5);+ const PAL = [[0.72, 0.09, 0.1], [0.14, 0.2, 0.62], [0.78, 0.52, 0.14], [0.62, 0.08, 0.1], [0.18, 0.26, 0.68], [0.8, 0.58, 0.2]];+ const blobs = []; for (let i = 0; i < 12; i++) blobs.push({ u: (r() - 0.5) * w * 1.1, v: lerp(sill, top, r()), su: 0.7 + r() * 0.9, sv: 1.6 + r() * 2.2, c: PAL[Math.floor(r() * PAL.length)] });+ const P3 = [], C4 = [], N3 = [], UV = [], I = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) {+ const u = (i / nu - 0.5) * (w + 1.6), v = lerp(sill - 1, top + 0.8, j / nv);+ let cr = 0.05, cg = 0.04, cb = 0.05, tw = 0.1;+ for (const B of blobs) { const e = Math.exp(-((u - B.u) * (u - B.u)) / (B.su * B.su) - ((v - B.v) * (v - B.v)) / (B.sv * B.sv)); cr += B.c[0] * e; cg += B.c[1] * e; cb += B.c[2] * e; tw += e; }+ const hw = v <= sp ? w / 2 : (w / 2) * Math.sqrt(Math.max(0, 1 - ((v - sp) / rise) * ((v - sp) / rise))), edge = sstep(-0.1, 0.75, hw - Math.abs(u)) * sstep(sill - 0.4, sill + 0.9, v) * sstep(top + 0.4, top - 1, v), mull = 1 - 0.62 * Math.exp(-(u * u) / 0.09);+ P3.push(u + v * P.sunShear[0], 0, v * P.sunShear[1]); N3.push(0, 1, 0); UV.push(i / nu, j / nv);+ C4.push(Math.min(1, cr / tw * 1.5), Math.min(1, cg / tw * 1.5), Math.min(1, cb / tw * 1.5), 0.46 * edge * mull);+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) { const a = j * (nu + 1) + i, b = a + 1, c = a + nu + 2, d = a + nu + 1; I.push(a, d, c, a, c, b); }+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(P3), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(N3), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(UV), 2));+ g.setAttribute('color', new THREE.BufferAttribute(new Float32Array(C4), 4));+ g.setIndex(new THREE.BufferAttribute(new Uint16Array(I), 1));+ return g;+}++function storyWindows() { // the narrative aisle windows+ const sets = [+ [['annunciation', 'nativity', 'magi', 'shepherd', 'supper'], ['crucifixion', 'resurrection', 'david', 'noah', 'annunciation']],+ [['noah', 'david', 'annunciation', 'nativity', 'magi'], ['shepherd', 'supper', 'crucifixion', 'resurrection', 'david']],+ [['magi', 'supper', 'noah', 'crucifixion', 'shepherd'], ['nativity', 'david', 'resurrection', 'annunciation', 'supper']],+ [['resurrection', 'shepherd', 'crucifixion', 'david', 'nativity'], ['supper', 'annunciation', 'magi', 'noah', 'resurrection']],+ ];+ const grounds = [['ruby', 'cobalt'], ['cobalt', 'ruby'], ['ruby', 'cobalt'], ['cobalt', 'ruby']];+ return sets.slice(0, P.storyWindows).map((s, n) => makeWindowGlass(...windowArgs.aisle, 40 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetStory(g, x, w, y0, ys, r, s[i].slice(0, 4), { c1: grounds[n][0], c2: grounds[n][1], g1: grounds[(n + 1) % 4][1], g2: grounds[n][0], cell: 0.86 }),+ oculus: { ground: grounds[n][1], inner: grounds[n][0] },+ }));+}+const CLERE_SAINTS = [+ [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'book' }, { robe: 'emerald', mantle: 'gold', crown: 1, beard: 2, attr: 'cross', arm: 'point', halo: false }],+ [{ robe: 'pale', mantle: 'cobalt', veil: 'azure', arm: 'pray' }, { robe: 'violet', mantle: 'white', mitre: 'white', attr: 'staff', arm: 'up' }],+ [{ robe: 'gold', mantle: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'purple', beard: 1, attr: 'sword', arm: 'point' }],+ [{ robe: 'rubyLt', mantle: 'teal', crown: 1, attr: 'lily', arm: 'point', halo: false }, { robe: 'white', mantle: 'rubyLt', arm: 'orant', wings: 'gold' }],+];+function clereWindows() {+ return CLERE_SAINTS.slice(0, P.clerestoryWindows).map((pair, n) => makeWindowGlass(...windowArgs.clere, 60 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetSaint(g, x, w, y0, ys, r, pair[i], (i + n) % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }),+ oculus: { bust: { robe: 'rubyLt', beard: 1, cross: 1 }, ground: 'cobalt', inner: 'gold' },+ }));+}++function buildBody() {+ const { xTowerE, xNaveE, xc, xChoirW, xa, ridge } = LAY;+ const gNave = group('Nave'), gChoir = group('Choir'), gTN = group('Transept_North'), gTS = group('Transept_South'), gX = group('Crossing'), gApse = group('Apse_Chevet'), gRoof = group('Roofs');+ // The upper elevation is shared by every straight bay; aisles come in full and open-corner versions+ const bayUp = buildNaveBay({ part: 'upper' }), aisleFull = buildNaveBay({ part: 'aisle' }), aisleOpen = buildNaveBay({ part: 'aisle', open: true });+ const vault = buildVaultNave(), pier = buildPier(), but = buildButtress();+ const stories = storyWindows(), cleres = clereWindows();+ SHARED.stories = stories; SHARED.cleres = cleres;+ let counter = 0;+ const statue = (fr, lx, y, lz, kind, faceDeg, scale, parent, indoor = true) => placeFigure('Statue_' + kind + '_' + (counter++), kind, { pos: fr.pos(lx, y, lz), rot: fr.rot(faceDeg), scale, parent, indoor, lod: 0 });+ const garg = (fr, lx, y, lz, k, sc, parent) => place('Gargoyle_' + (counter++), gargoyle(k), MAT.fine, { pos: fr.pos(lx, y, lz), rot: fr.rot(-90), scale: [sc, sc, sc], parent });++ // One side of a straight bay: module, vault-side glass, statues, gargoyles+ const placeBay = (name, fr, parent, o = {}) => {+ placeSet(name + '_Upper', bayUp, [fr.ox, 0, fr.oz], fr.th, parent);+ if (!o.noVault) placeSet(name + '_Aisle', o.open ? aisleOpen : aisleFull, [fr.ox, 0, fr.oz], fr.th, parent);+ const c = cleres[(o.i || 0) % cleres.length];+ placeGlass(name + '_Clerestory', c, fr.pos(0, 0, zn), fr.th + 180, parent);+ if (!o.open) {+ placeGlass(name + '_AisleWindow', stories[(o.i || 0) % stories.length], fr.pos(0, 0, zaw + 0.45), fr.th + 180, parent);+ if ((o.i || 0) % 2 === 0) statue(fr, 0, 0.7, zaw - 0.3, SAINTS[(o.i || 0) % SAINTS.length], 90, 0.86, parent);+ if (!o.inTower) garg(fr, 0, 22.5, zaw + 1.05, (o.i || 0) % 3, 0.72, parent);+ }+ if (!o.noUpperGarg) garg(fr, 0, P.eaves - 0.2, zn + 0.62, ((o.i || 0) + 1) % 3, 0.66, parent);+ };+ const placeButtress = (name, fr, parent, i) => {+ placeSet(name, but, [fr.ox, 0, fr.oz], fr.th, parent);+ statue(fr, 0, 23.5, BUTTRESS.zT, 'angel', -90, 1.9, parent, false);+ garg(fr, 0, 15.2, BUTTRESS.zB - 1.05, i % 3, 1.0, parent);+ };+ const placePier = (name, lfr, parent, kind) => {+ placeSet(name, pier, [lfr.ox, 0, lfr.oz], lfr.th, parent);+ if (kind) statue(lfr, 0, 5.78, -1.14, kind, 90, 1.15, parent);+ };++ // ---- Two more bays carry the nave between the towers to the inner face of the west wall ----+ for (let t = 0; t < 2; t++) {+ const x = xTowerE + (t + 0.5) * BAY;+ placeBay('TowerBay' + t + '_N', frame(0, x, 0), gNave, { i: t + 2, noUpperGarg: true, inTower: true });+ placeBay('TowerBay' + t + '_S', frame(180, x, 0), gNave, { i: t, noUpperGarg: true, inTower: true });+ placeSet('TowerVault' + t, vault, [x, 0, 0], 0, gNave);+ if (t === 0) { placePier('TowerPier_N', frame(0, x + BAY / 2, zn), gNave, SAINTS[6]); placePier('TowerPier_S', frame(180, x + BAY / 2, -zn), gNave, SAINTS[7]); }+ }+ // ---- Nave ----+ for (let i = 0; i < P.naveBays; i++) {+ const x = xTowerE - (i + 0.5) * BAY, isOpen = i === P.naveBays - 1;+ placeBay('NaveBay' + i + '_N', frame(0, x, 0), gNave, { i, open: isOpen });+ placeBay('NaveBay' + i + '_S', frame(180, x, 0), gNave, { i: i + 2, open: isOpen });+ placeSet('NaveVault' + i, vault, [x, 0, 0], 0, gNave);+ }+ for (let k = 0; k <= P.naveBays - 1; k++) {+ const x = xTowerE - k * BAY;+ placePier('NavePier' + k + '_N', frame(0, x, zn), gNave, k ? SAINTS[k % SAINTS.length] : null);+ placePier('NavePier' + k + '_S', frame(180, x, -zn), gNave, k ? SAINTS[(k + 3) % SAINTS.length] : null);+ if (k >= 1 && k < P.naveBays - 1) { placeButtress('NaveButtress' + k + '_N', frame(0, x, 0), gNave, k); placeButtress('NaveButtress' + k + '_S', frame(180, x, 0), gNave, k + 1); }+ }+ // ---- Choir ----+ for (let j = 0; j < P.choirBays; j++) {+ const x = xChoirW - (j + 0.5) * BAY, isOpen = j === 0;+ placeBay('ChoirBay' + j + '_N', frame(0, x, 0), gChoir, { i: j + 1, open: isOpen });+ placeBay('ChoirBay' + j + '_S', frame(180, x, 0), gChoir, { i: j + 3, open: isOpen });+ placeSet('ChoirVault' + j, vault, [x, 0, 0], 0, gChoir);+ }+ for (let k = 1; k <= P.choirBays; k++) {+ const x = xChoirW - k * BAY;+ placePier('ChoirPier' + k + '_N', frame(0, x, zn), gChoir, SAINTS[(k + 5) % SAINTS.length]);+ placePier('ChoirPier' + k + '_S', frame(180, x, -zn), gChoir, SAINTS[(k + 1) % SAINTS.length]);+ placeButtress('ChoirButtress' + k + '_N', frame(0, x, 0), gChoir, k);+ placeButtress('ChoirButtress' + k + '_S', frame(180, x, 0), gChoir, k + 1);+ }+ // ---- Transept arms: the same bay, turned through 90 degrees ----+ for (const s of [1, -1]) {+ const gT = s > 0 ? gTN : gTS, tag = s > 0 ? 'N' : 'S';+ for (let q = 0; q < P.transeptBays + 1; q++) {+ const zc = s * (zn + (q + 0.5) * BAY), isOpen = q === 0;+ placeBay('Transept' + tag + q + '_W', frame(90 * s, xc, zc), gT, { i: q + 1, open: isOpen, noVault: isOpen });+ placeBay('Transept' + tag + q + '_E', frame(-90 * s, xc, zc), gT, { i: q + 3, open: isOpen, noVault: isOpen });+ placeSet('TranseptVault' + tag + q, vault, [xc, 0, zc], 90, gT);+ }+ for (let q = 1; q <= P.transeptBays; q++) { // the last bay ends in the front wall, which needs no free pier+ const zz = s * (zn + q * BAY);+ placePier('TranseptPier' + tag + q + '_W', frame(90, xc + zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 2) % SAINTS.length] : null);+ placePier('TranseptPier' + tag + q + '_E', frame(-90, xc - zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 4) % SAINTS.length] : null);+ if (q === P.transeptBays) { placeButtress('TranseptButtress' + tag + '_W', frame(90 * s, xc, zz), gT, q); placeButtress('TranseptButtress' + tag + '_E', frame(-90 * s, xc, zz), gT, q + 1); }+ }+ }+ // ---- Crossing ----+ const xPier = buildCrossingPier(), aPier = buildAislePier(), xVault = mkBatches();+ const sp = zn - 0.3;+ const crossCrown = addRibVault(xVault.F, xVault.A, [[-sp, -sp], [sp, -sp], [sp, sp], [-sp, sp]], P.vaultSpring, P.vaultCrown + 0.5, { sideRise: () => 12.9, nS: 12, nT: 10 });+ addBoss(xVault.G, xVault.F, crossCrown, 0.95);+ // Great transverse arches and the walls above them+ const arch = pointedArc(2 * (zn - 0.5), 12.9, 7);+ for (let i = 0; i < 4; i++) xVault.F.at(M.of(M.Ry(i * 90), M.T(0, P.vaultSpring, zn - 0.05)), () => xVault.F.sweep(arch, rollSection(0.9, 1.2, 2, 0.09, 3), { caps: false }));+ placeSet('CrossingVault', xVault, [xc, 0, 0], 0, gX);+ for (const [a, b] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {+ placeSet('CrossingPier', xPier, [xc + a * zn, 0, b * zn], 0, gX);+ placeSet('AngleAislePier', aPier, [xc + a * zaw, 0, b * zaw], 0, gX);+ }+ // ---- Chevet ----+ const sector = buildChevetSector(), apsePier = buildApsePier();+ const apseSaints = [{ robe: 'gold', mantle: 'cobalt', crown: 1, veil: 'pale', arm: 'pray' }, { robe: 'white', mantle: 'ruby', beard: 2, attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'emerald', beard: 1, attr: 'book' }, { robe: 'white', mantle: 'rubyLt', beard: 1, cross: 1, arm: 'up' }];+ const hemi = apseSaints.slice(0, 3).map((f, i) => makeLancetGlass(2.05, P.triforiumTop + 0.45, P.vaultSpring + 4.8, 4.3, 70 + i, f, i % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }));+ const chapelGlass = ['annunciation', 'crucifixion'].map((nm, i) => { const g = new Glass(80 + i); g.raw(archOutline(1.3, 3.2, 9.9, 2.5, 7), 'lead', -0.4, 0); gLancetStory(g, 0, 1.3, 3.2, 9.9, 2.5, [nm, ['supper', 'magi'][i], ['magi', 'resurrection'][i]], { cell: 0.6, c1: i ? 'cobalt' : 'ruby', c2: i ? 'ruby' : 'cobalt' }); return g.b; });+ for (let k = 0; k < P.apseSides; k++) {+ const th = -(k + 0.5) * CHEV.d, fr = frame(th, xa, 0), gS = group('ChevetSector' + k, gApse);+ placeSet('Sector' + k, sector, [xa, 0, 0], th, gS);+ placeGlass('HemicycleWindow' + k, hemi[(k + (k > 3 ? 1 : 0)) % hemi.length], fr.pos(0, 0, CHEV.Ra), th + 180, gS);+ const nF = P.chapelFacets, dF = 216 / nF, ap = CHEV.Rc * Math.cos(rad(dF / 2));+ for (let i = 0; i < nF; i++) {+ const fm = -108 + (i + 0.5) * dF, lx = ap * Math.sin(rad(fm)), lz = CHEV.zc + ap * Math.cos(rad(fm));+ placeGlass('ChapelWindow' + k + '_' + i, chapelGlass[(i + k) % chapelGlass.length], fr.pos(lx, 0, lz + 0.0), th + fm + 180, gS);+ }+ statue(fr, 0, 0.5, CHEV.zc + CHEV.Rc * 0.55, k === 3 ? 'virgin' : SAINTS[k], 90, 0.92, gS);+ garg(frame(th - 10, xa, 0), 0, 15.5, CHEV.zc + 1.2, k % 3, 0.7, gS);+ if (k >= 1) {+ const bt = -k * CHEV.d;+ placeButtress('ChevetButtress' + k, frame(bt, xa, 0), gS, k);+ // A slim clustered pier at each angle of the hemicycle: the arches now spring from something+ placeSet('ApsePier' + k, apsePier, frame(bt, xa, 0).pos(0, 0, zn), bt, gS);+ }+ }+ // ---- Roofs, ridge cresting, floor ----+ const Lr = new Batch(TILE.lead);+ const yE = P.eaves + 0.15, half = zn + 0.72;+ addRoofRun(Lr, xa, LAY.xW - 1.2, half, yE, ridge);+ Lr.at(M.T(xc, 0, 0), () => Lr.at(M.Ry(90), () => addRoofRun(Lr, -(zn + (P.transeptBays + 1) * BAY + 0.4), zn + (P.transeptBays + 1) * BAY + 0.4, half, yE, ridge)));+ place('MainRoof_Lead', Lr, MAT.lead, { parent: gRoof });+ const crest = buildCresting(BAY);+ const nCrest = Math.floor((LAY.xW - 2 - xa) / BAY);+ for (let i = 0; i < nCrest; i++) place('RidgeCresting' + i, crest, MAT.gold, { pos: [xa + (i + 0.5) * BAY, ridge, 0], parent: gRoof });+ for (const s of [1, -1]) for (let q = 0; q < P.transeptBays + 2; q++) { const zz = s * (0.5 + (q + 0.5) * BAY + 3); if (Math.abs(zz) < zn + (P.transeptBays + 1) * BAY) place('TranseptCresting' + (s > 0 ? 'N' : 'S') + q, crest, MAT.gold, { pos: [xc, ridge, zz], rot: [0, 90, 0], parent: gRoof }); }+ buildFleche(gRoof, xc, 0);+ // The church floor sits just above the ground outside, so the two can never fight+ const fl = new Batch(TILE.pave), fy = 0.06;+ const R13 = zaw + 0.1;+ fl.face([[xa, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, -R13], [xa, fy, -R13]]);+ const tz = zn + (P.transeptBays + 1) * BAY + 1;+ fl.face([[xc - R13, fy, tz], [xc + R13, fy, tz], [xc + R13, fy, -tz], [xc - R13, fy, -tz]]);+ for (let k = 0; k < P.apseSides; k++) { const th = rad(-(k + 0.5) * CHEV.d), f = frame(-(k + 0.5) * CHEV.d, xa, 0), rr = CHEV.zc + CHEV.Rc + 0.2, hh = rr * Math.tan(CHEV.h); fl.face([f.pos(0, fy, 0), f.pos(-hh, fy, rr), f.pos(hh, fy, rr)]); }+ place('Paving', fl, MAT.pave, { parent: ROOT });+ // ---- Stained light on the floor from the south aisle ----+ const pools = [buildLightPool(3), buildLightPool(11)];+ for (let i = 0; i < P.naveBays - 1; i++) createPart(uniq('StainedLight_Pool' + i), pools[i % 2], MAT.pool, { position: [xTowerE - (i + 0.5) * BAY, 0.08 + 0.002 * (i % 3), -zaw], parent: gNave });+}++// ==================================================================================+// THE GREAT ROSE: real stone tracery, and glass depicting Christ among apostles and angels+// ==================================================================================+const ROSE = { r0: 1.15, rs1: 2.35, r1: 3.05, rs2: 4.05, r2: 4.72, n1: 12, n2: 24 };+const pol = (r, a) => [r * Math.cos(a), r * Math.sin(a)];+function addRoseTracery(F, R) {+ const k = R / 5.5, heavy = traceryBar(0.3 * k, 0.5), mid = hexBar(0.2 * k, 0.42), thin = hexBar(0.14 * k, 0.34);+ const dia = [[0.07 * k, 0], [0, 0.17], [-0.07 * k, 0], [0, -0.17]]; // lightest bar, for the little roundels+ const { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE;+ F.sweep(ringPts(0, 0, R, 36), heavy, { closed: true });+ F.sweep(ringPts(0, 0, r0 * k, 14), mid, { closed: true });+ F.sweep(ringPts(0, 0, r2 * k, 36), mid, { closed: true });+ F.sweep(ringPts(0, 0, r1 * k, 24), mid, { closed: true });+ // Inner wheel: colonnette spokes, each light closed by a pointed trefoil head+ const head = (n, rs, re, a, bar) => {+ const da = PI / n * 0.98, pts = [], N = 6;+ for (let i = 0; i <= N; i++) { const t = -1 + 2 * i / N, h = Math.pow(1 - Math.pow(Math.abs(t), 1.5), 0.72); pts.push(pol((rs + (re - rs) * h) * k, a + t * da)); }+ F.sweep(pts, bar, { caps: false });+ return pts;+ };+ for (let i = 0; i < n1; i++) {+ const a = i * TAU / n1, am = a + PI / n1;+ F.sweep([pol(r0 * k, a), pol(rs1 * k, a)], mid, { caps: false });+ head(n1, rs1, r1 * 0.985, am, thin);+ // the cusp hanging inside the head+ const c = pol((rs1 + 0.34) * k, am); F.sweep(ringPts(c[0], c[1], 0.26 * k, 6), dia, { closed: true, s: 0.95, crease: 80 });+ }+ // Outer wheel of twenty-four lights+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, am = a + PI / n2;+ F.sweep([pol(r1 * k, a), pol(rs2 * k, a)], thin, { caps: false });+ head(n2, rs2, r2 * 0.99, am, thin);+ // Quatrefoil roundels in the outer band+ const c = pol((R / k + r2) / 2 * k, a);+ F.sweep(ringPts(c[0], c[1], 0.3 * k, 6), dia, { closed: true, crease: 80 });+ }+}+const SHARED = {};+// Rose tracery is one mesh, scaled for the smaller transept roses; bright outside, dim inside+function roseTracery() {+ if (!SHARED.rose) { const b = new Batch(TILE.fine); addRoseTracery(b, WF.roseR); SHARED.rose = b; }+ return SHARED.rose;+}+function largePinnacle() {+ if (!SHARED.pin) { const b = new Batch(TILE.fine); addPinnacle(b, 2, 10, { lod: 1 }); SHARED.pin = b; }+ return SHARED.pin;+}+function makeRoseGlass(R, seed, o = {}) {+ const g = new Glass(seed), k = R / 5.5, { r0, r1, r2, n1, n2 } = ROSE;+ g.raw(ringPts(0, 0, R + 0.08, 48), 'lead', -0.4, 0);+ // A light is never one flat sheet: lay the painted mosaic across it, clipped to its shape+ const ground = (pts, col, layer) => { let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of pts) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); } g.withClip(pts, () => gDiaper(g, x0, y0, x1, y1, 0, col, null, null, layer)); };+ // A roundel: a thin jewelled rim round a deep field, with everything inside it clipped to the circle+ const roundel = (cx, cy, rad_, field, draw) => {+ gRim(g, cx, cy, rad_, rad_ * 1.13, ['gold', 'ruby', 'gold', 'cobalt'], 16, 1.2);+ g.disc(cx, cy, rad_, field, 1.5, 16, { came: 0 });+ g.withClip(ringPts(cx, cy, rad_, 16), draw);+ };+ // Nothing behind the tracery is left black: the whole wheel is first laid in cobalt mosaic+ ground(ringPts(0, 0, R + 0.04, 32), 'cobalt', -0.25);+ // The outer band: alternating sheets of ruby and cobalt, a jewel in each+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, b = (i + 1) * TAU / n2;+ ground([pol(r2 * k, a), pol(R, a), pol(R, b), pol(r2 * k, b)], i % 2 ? 'ruby' : 'cobalt', 0);+ const c = pol((R / k + r2) / 2 * k, a); g.disc(c[0], c[1], 0.27 * k, i % 2 ? 'emerald' : 'ruby', 0.6, 10, { came: 0.016 }); g.disc(c[0], c[1], 0.12 * k, 'gold', 1, 8, { came: 0.01 });+ }+ // Outer lights: kings, angels and elders, upright in their roundels so the eye can rest on each face+ const busts = [{ robe: 'emerald', beard: 1, crown: 1, halo: 'gold' }, { robe: 'white', face: 'angel', halo: 'gold', wings: 1 }, { robe: 'ruby', beard: 2, halo: 'gold' }];+ for (let i = 0; i < n2; i++) {+ const am = (i + 0.5) * TAU / n2, da = PI / n2, col = i % 2 ? 'cobalt' : 'ruby', field = i % 2 ? 'ruby' : 'cobalt';+ ground([pol(r1 * k, am - da), pol(r2 * k, am - da), pol(r2 * k, am + da), pol(r1 * k, am + da)], col, 0);+ const mr = (r1 + r2) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.46 * k, B = busts[i % busts.length];+ roundel(cx, cy, rr, field, () => {+ if (B.wings) for (const s of [-1, 1]) g.poly([[cx + s * rr * 0.16, cy - rr * 0.1], [cx + s * rr * 0.98, cy + rr * 0.42], [cx + s * rr * 0.86, cy - rr * 0.5], [cx + s * rr * 0.3, cy - rr * 0.62]], 'gold', 1.8, { came: 0.012 });+ gBust(g, cx, cy - rr * 0.46, rr * 1.32, Object.assign({ layer: 2 }, B));+ });+ }+ // Inner lights: the twelve apostles enthroned, each upright in a larger roundel, colours answering round the wheel+ const scheme = o.figs || [{ robe: 'ruby', mantle: 'cobalt', beard: 1, attr: 'key', arm: 'point' }, { robe: 'emerald', mantle: 'ruby', beard: 2, attr: 'book' }, { robe: 'gold', mantle: 'cobalt', beard: 1, attr: 'sword', arm: 'point' }, { robe: 'cobalt', mantle: 'ruby', attr: 'book' }, { robe: 'purple', mantle: 'gold', beard: 1, attr: 'book' }, { robe: 'ruby', mantle: 'emerald', beard: 2, attr: 'key', arm: 'point' }];+ for (let i = 0; i < n1; i++) {+ const am = (i + 0.5) * TAU / n1, da = PI / n1, col = i % 2 ? 'ruby' : 'cobalt', field = i % 2 ? 'cobalt' : 'ruby';+ ground([pol(r0 * k, am - da), pol(r1 * k, am - da), pol(r1 * k, am + da), pol(r0 * k, am + da)], col, 0);+ const mr = (r0 + r1) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.62 * k;+ roundel(cx, cy, rr, field, () => g.withXf(0, cx, cy - rr * 0.92, 1, () => gFigure(g, 0, 0, rr * 1.86, Object.assign({ pose: 'sit', layer: 2 }, scheme[i % scheme.length]))));+ }+ // The centre: Christ in Majesty in a gilded mandorla+ g.disc(0, 0, r0 * k + 0.06, 'lead', 2.6, 16, { came: 0, jitter: 0 });+ ground(ringPts(0, 0, r0 * k, 16), o.centre || 'cobalt', 3);+ g.withClip(ringPts(0, 0, r0 * k, 14), () => g.withXf(0, 0, -0.98 * k, k, () => {+ const m = []; for (let i = 0; i < 16; i++) { const t = i * TAU / 16; m.push([0.62 * Math.cos(t) * Math.pow(Math.abs(Math.sin(t)) * 0.3 + 0.7, 1), 1 + 0.96 * Math.sin(t)]); }+ g.poly(m, 'gold', 3.3, { came: 0.02 });+ g.poly(m.map((p) => [p[0] * 0.8, 1 + (p[1] - 1) * 0.88]), 'rubyLt', 3.5, { came: 0.012 });+ gFigure(g, 0, 0.22, 1.62, o.majesty || { pose: 'sit', robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'up', attr: null, layer: 4 });+ }));+ return g.b;+}++// ==================================================================================+// WEST FRONT. Local frame: x across the facade, y up, +z out towards the viewer, z=0 the wall.+// ==================================================================================+const WF = { halfW: 21.5, topY: 47, roseY: 31.8, roseR: 5.5, kingY: 20.2, kingTop: 25.3, galY: 40.6, galTop: 46.2, wallT: 0.8, portalC: { x: 0, w0: 4.3, orders: P.jambStatues, dx: 0.58, dz: 0.62, ys: 7.5 }, portalS: { x: 13.5, w0: 3.7, orders: P.jambStatues - 1, dx: 0.6, dz: 0.66, ys: 7.2 } };+// Aged oak door leaf with forged straps, studs and a ring. Hung at x=0, spans to x=w, h high, front +z.+function addDoorLeaf(set, x0, w, h, flip) {+ const { O, I } = set, planks = 6, pw = w / planks, d = flip ? -1 : 1;+ for (let i = 0; i < planks; i++) {+ const a = x0 + d * i * pw, c = x0 + d * (i + 0.97) * pw, xa = Math.min(a, c), xc = Math.max(a, c), tone = 0.46 + 0.16 * Math.abs(Math.sin(i * 2.4 + x0 * 3));+ O.at(M.Ry(0), () => O.box(xa, xc, 0.02, h, -0.05, 0.05, { s: tone }));+ }+ // Oak grain runs up each plank: remap planes with u across and v up is the default box projection+ const strapY = [0.72, h * 0.36, h * 0.64, h - 0.7];+ for (const y of strapY) {+ const reach = w * 0.94, xEnd = x0 + d * reach;+ I.shade(0.34, () => {+ I.box(Math.min(x0, xEnd), Math.max(x0, xEnd), y - 0.075, y + 0.075, 0.05, 0.085, {});+ // A forged hinge: the strap swells and ends in a spear with two barbs, flat iron from one piece; a knuckle turns at the door's edge+ const spear = [[xEnd, y - 0.075], [xEnd + d * 0.1, y - 0.15], [xEnd + d * 0.16, y - 0.07], [xEnd + d * 0.4, y], [xEnd + d * 0.16, y + 0.07], [xEnd + d * 0.1, y + 0.15], [xEnd, y + 0.075]];+ I.slab(spear, [], 0.05, 0.085, {});+ I.at(M.T(x0 + d * 0.03, y, 0.07), () => I.lathe([[0.055, -0.12], [0.055, 0.12]], 6, {}));+ for (let k = 0; k < 4; k++) { const sx = x0 + d * (k + 0.5) * reach / 4; I.box(sx - 0.03, sx + 0.03, y - 0.03, y + 0.03, 0.08, 0.115, { taper: 0.012 }); }+ });+ }+ // Ring handle+ I.shade(0.4, () => I.at(M.of(M.T(x0 + d * (w - 0.3), h * 0.42, 0.11), M.Ry(0)), () => {+ const rg = []; for (let i = 0; i <= 12; i++) { const t = i * TAU / 12; rg.push([0.1 * Math.cos(t), 0.1 * Math.sin(t) - 0.1, 0]); }+ I.tube(rg, 0.018, 5, { capEnd: false }); I.box(-0.05, 0.05, -0.05, 0.05, -0.05, 0, {});+ }));+}+// Height of each sculpted figure at scale 1, from its feet to the top of its hair, crown or mitre+const figureTop = (kind) => ({ bishop: 2.15, king: 2.03, queen: 2.03, virgin: 2.03 }[FIG_ALIAS[kind] || kind] || 1.95);+function buildPortal(cfg, kinds, tymp) {+ const set = mkBatches(), { A, F, G, O, I } = set, { w0, orders: N, dx, dz, ys } = cfg;+ // Every order is concentric: the same two arc centres with the radius growing by one step+ const r0 = w0 * 0.86, c0 = (r0 * r0 - w0 * w0 / 4) / w0, wT = (i) => w0 + 2 * i * dx, zT = (i) => i * dz;+ const riseI = (i) => Math.sqrt(Math.pow(c0 + w0 / 2 + i * dx, 2) - c0 * c0);+ const riseOf = (w) => riseI((w - w0) / (2 * dx));+ const figs = [], zF = N * dz, outerW = wT(N), apex = ys + riseOf(outerW), lintelH = 1.2, doorH = ys - lintelH;+ const statueY = 1.5, statueK = 1.22;+ // ---- Solid stepped jambs, each order with a dado of carved blind arches ----+ for (let i = 0; i < N; i++) {+ for (const s of [-1, 1]) {+ const xa = s * wT(i) / 2, xb = s * wT(i + 1) / 2, x0 = Math.min(xa, xb), x1 = Math.max(xa, xb), zf = zT(i + 1);+ A.box(x0, x1, 0, ys, -0.1, zf, { noBot: true });+ // The dado: a plinth, a blind trefoil head on two colonnettes, a moulded table for the statues above+ F.box(x0 - 0.01, x1 + 0.01, 0, 0.26, zf, zf + 0.05, {});+ F.at(M.T((x0 + x1) / 2, 0, zf), () => {+ const w = x1 - x0 - 0.12, y0 = 0.26, ya = 0.86, r = 0.32;+ F.sweep(pointedArc(w, r, 4).map((p) => [p[0], p[1] + ya]), hexBar(0.05, 0.09), { caps: false, z: 0.03 });+ for (const t of [-1, 1]) F.box(t * w / 2 - 0.03, t * w / 2 + 0.03, y0, ya, 0, 0.06, {});+ F.sweep([[-(x1 - x0) / 2, 1.3], [(x1 - x0) / 2, 1.3]], stringSection(0.16, 0.1), { caps: false });+ });+ // Colonnette on the arris, with a foliate capital; the saint stands against it+ const cx = s * (wT(i) / 2 + 0.02), cz = zT(i) + 0.32;+ F.at(M.T(cx, 0, cz), () => {+ F.lathe([[0.2, 1.3], [0.2, 1.42], [0.13, 1.54], [0.13, ys - 0.5], [0.16, ys - 0.46], [0.13, ys - 0.42], [0.26, ys - 0.04], [0.3, ys]], 8);+ for (let l = 0; l < 4; l++) addLeaf(F, M.of(M.T(0, ys - 0.42, 0), M.Ry(l * 90 + 45), M.T(0.1, 0, 0)), 0.38, { coarse: 1 });+ });+ // The figure stands with its back to the column, on a carved base; its canopy is engaged on the same column,+ // a hand's breadth above this figure's own head+ const dirX = -s * dz, dirZ = dx * 1.5, l = Math.hypot(dirX, dirZ), th = Math.atan2(-dirZ / l, dirX / l) * 180 / PI, fx = dirX / l, fz = dirZ / l;+ const kind = kinds[(i + (s > 0 ? 0 : kinds.length >> 1)) % kinds.length], canopyY = statueY + figureTop(kind) * statueK + 0.1;+ const sx = cx + fx * 0.27, sz = cz + fz * 0.27;+ F.at(M.T(sx, 0, sz), () => {+ F.lathe([[0.3, 1.25], [0.34, 1.32], [0.24, 1.38], [0.3, 1.44], [0.33, statueY]], 8);+ for (let l = 0; l < 2; l++) addLeaf(F, M.of(M.T(0, 1.36, 0), M.Ry(l * 90 + (s > 0 ? -60 : 150)), M.T(0.24, 0, 0)), 0.2, { coarse: 1 });+ F.at(M.T(0, canopyY + 0.14, 0), () => {+ // A canopy like a tiny carved building: vaulted soffit, a drum of gablets, thin spirelet+ F.lathe([[0.05, -0.14], [0.27, -0.06], [0.33, 0], [0.33, 0.1], [0.28, 0.14], [0.17, 0.17], [0.17, 0.5], [0.22, 0.54], [0.15, 0.58], [0.07, 0.64], [0.01, 1.02]], 6, { phase: PI / 6, faceted: true });+ const face = s > 0 ? -60 : 60;+ for (const l of [-1, 0, 1]) F.at(M.of(M.Ry(face + l * 60), M.T(0, 0, 0.27)), () => {+ F.face([[-0.16, 0.11, 0.02], [0.16, 0.11, 0.02], [0, 0.42, 0.02]]);+ crocketsAlong(F, [-0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [-1, 0.6, 0], 0);+ crocketsAlong(F, [0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [1, 0.6, 0], 0);+ F.shade(0.5, () => F.face([[-0.08, 0.14, 0.03], [0.08, 0.14, 0.03], [0, 0.28, 0.03]]));+ });+ });+ });+ figs.push({ kind, pos: [cfg.x + sx, statueY, sz], rot: [0, th, 0], scale: statueK, mirror: s < 0 });+ }+ }+ // ---- Archivolts: each order a hollow, each hollow a procession of small figures, every one on a console under a canopy ----+ const ARCH_KINDS = ['apostle', 'king', 'angel', 'bishop', 'queen'], ak = 0.62;+ for (let i = 0; i < N; i++) {+ const w = wT(i), r = riseI(i), arc = pointedArc(w, r, 8).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, [[0, -0.1], [dx, -0.1], [dx, dz], [0, 0]], { z: zT(i), caps: false, crease: 30 });+ F.at(M.T(0, 0, zT(i + 1) - 0.02), () => F.sweep(pointedArc(wT(i + 1), riseI(i + 1), 8).map((p) => [p[0], p[1] + ys]), hexBar(0.2, 0.22), { caps: false }));+ const pts = pointedArc(w + dx, riseI(i + 0.5), 16).map((p) => [p[0], p[1] + ys]);+ let len = 0; const seg = []; for (let k = 0; k < pts.length - 1; k++) { const l = Math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]); seg.push(l); len += l; }+ const n = Math.max(4, Math.round(len / 1.52));+ for (let q = 0; q < n; q++) {+ const d = (q + 0.5) / n * len; let acc = 0, k = 0; while (k < seg.length - 1 && acc + seg[k] < d) { acc += seg[k]; k++; }+ const f = (d - acc) / seg[k], px = lerp(pts[k][0], pts[k + 1][0], f), py = lerp(pts[k][1], pts[k + 1][1], f);+ const tx0 = (pts[k + 1][0] - pts[k][0]) / seg[k], ty0 = (pts[k + 1][1] - pts[k][1]) / seg[k], rx = -ty0, ry = tx0;+ const up = d > len / 2 ? -1 : 1, Y = [tx0 * up, ty0 * up, 0];+ // sits on the splay, looking out towards the opening and the viewer+ const nl = Math.hypot(dz, dx), X = [-rx * dz / nl, -ry * dz / nl, dx / nl];+ const Z = [X[1] * Y[2] - X[2] * Y[1], X[2] * Y[0] - X[0] * Y[2], X[0] * Y[1] - X[1] * Y[0]];+ const pos = [px + X[0] * 0.17 - Y[0] * 0.62, py + X[1] * 0.17 - Y[1] * 0.62, zT(i) + dz * 0.5 + X[2] * 0.17];+ figs.push({ kind: ARCH_KINDS[(i + q) % ARCH_KINDS.length], lod: 0, pos: [cfg.x + pos[0], pos[1], pos[2]], basis: { X, Y, Z }, scale: ak });+ F.at(frameOf(pos, X, Y), () => {+ F.lathe([[0.03, -0.18], [0.16, -0.02], [0.18, 0.02]], 6);+ F.lathe([[0.02, 1.27], [0.16, 1.33], [0.16, 1.38], [0.02, 1.56]], 6, { faceted: true });+ });+ }+ }+ // ---- Outer hood with gable and crockets ----+ const hood = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, zF), () => F.sweep(hood, rollSection(0.34, 0.44, 2, 0.05, 2), { caps: false }));+ // The gable springs from the arch springing: plain coursed stone, its carving is real and stands proud of it+ const gw = outerW + 1.5, gy = ys, gTop = WF.kingY - 0.7, gH = gTop - gy;+ const archCut = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ const field = [[-gw / 2, gy], [-(outerW + 0.1) / 2, gy], ...archCut.slice(1, -1), [(outerW + 0.1) / 2, gy], [gw / 2, gy], [0, gTop]];+ A.at(M.T(0, 0, zF - 0.1), () => A.slab(field, [], -0.1, 0, { noOuterSides: true, noBack: true }));+ A.at(M.T(0, 0, zF - 0.2), () => A.slab(field, [], -zF + 0.3, 0, { noOuterSides: false, noFront: true, noBack: true, so: 0.7 }));+ F.at(M.T(0, 0, zF - 0.14), () => {+ addGable(F, gw, gH, 0, 0.24, gy, { size: 0.72, finial: 0.1, crockets: 6, noField: true, lod: 1 });+ const yc = apex + (gTop - apex) * 0.36, rc = Math.min(0.92, (gTop - yc) * 0.42);+ F.sweep(ringPts(0, yc, rc, 16), traceryBar(0.18, 0.24), { closed: true, z: 0.04 });+ const foil = []; for (let i = 0; i < 15; i++) { const a = PI / 2 + i * TAU / 15, R = rc * (0.56 + 0.34 * Math.pow(Math.abs(Math.cos(a * 1.5 - PI * 0.75)), 0.8)); foil.push([R * Math.cos(a), yc + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.09, 0.18), { closed: true, z: 0.04, crease: 40 });+ });+ G.at(M.T(0, gTop + 0.1, zF - 0.02), () => addFinial(G, 1.5, 1));+ // ---- Tympanum: one block of dressed stone, standing proud of the coursed wall behind it, with the scene carved out of it ----+ const tw = w0 + 0.04, tr = riseOf(w0), k = w0 / 4.3, reliefZ = 0.16, relief = 0.6;+ F.slab(archOutline(tw, ys - 0.05, ys, tr, 9), [], -0.1, reliefZ, { noBack: true, sr: 0.7 });+ // the ground the figures rise from is part of the block, a low moulded base rather than a shelf+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, ys - 0.02, ys + 0.1, -0.08, reliefZ + 0.14, {});+ // Lintel: a recessed frieze, deep enough to hold its people, cornice and sill standing forward of it+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, ys - 0.02, -0.08, -0.02, {});+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, doorH + 0.12, -0.08, 0.42, {});+ F.at(M.T(0, 0, 0.42), () => F.sweep([[-tw / 2 - 0.02, doorH + 0.06], [tw / 2 + 0.02, doorH + 0.06]], stringSection(0.12, 0.06), { caps: false }));+ const nL = Math.round(w0 / 0.37), frieze = ['apostle', 'queen', 'apostle', 'king', 'bishop', 'apostle'];+ for (let i = 0; i < nL; i++) {+ // two ranks, every other figure a step forward: they overlap and each one turns to the middle of the door+ const x = -w0 / 2 + (i + 0.5) * w0 / nL, side = x < 0 ? 1 : -1, deep = i % 2;+ figs.push({ kind: frieze[i % frieze.length], lod: 0, relief: 0.68, pos: [cfg.x + x, doorH + 0.12, deep ? 0.04 : 0.16], rot: [0, -90 + side * (16 + 10 * (i % 3)), 0], scale: 0.43 });+ }+ // The central figure enthroned in a vesica: two arcs meeting in a point above and below, a broad moulded band of stone+ // with its face gilded. Everything in the scene is cut back to the same block, and nothing crosses the frame.+ const mY = ys + 0.1, mH = tr * 0.84, mW = 0.84 * k, nM = 10;+ const vesica = (w, h, y0) => { const out = []; for (let i = 0; i < nM; i++) { const v = i / nM; out.push([w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } for (let i = 0; i < nM; i++) { const v = 1 - i / nM; out.push([-w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } return out; };+ const vo = vesica(mW, mH, mY - 0.06), vi = vesica(mW - 0.2, mH - 0.44, mY + 0.16);+ F.at(M.T(0, 0, reliefZ), () => { F.slab(vo, [vi], -0.02, 0.07, { sr: 0.78 }); F.sweep(vo, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); F.sweep(vi, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); });+ G.at(M.T(0, 0, reliefZ + 0.07), () => G.slab(vesica(mW - 0.025, mH - 0.07, mY - 0.025), [vesica(mW - 0.175, mH - 0.37, mY + 0.125)], 0, 0.05, {}));+ figs.push({ kind: tymp.main, half: 1, relief, pos: [cfg.x, mY, reliefZ + 0.02], rot: [0, -90, 0], scale: 1.0 * k });+ // Worshippers on their knees and angels crowding behind them, every head turned to the throne+ for (const s of [-1, 1]) {+ figs.push({ kind: 'kneel', half: 1, relief, pos: [cfg.x + s * 1.28 * k, mY, reliefZ + 0.12], rot: [0, -90 + s * 50, 0], scale: 0.84 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[0], half: 1, relief, pos: [cfg.x + s * 1.6 * k, mY, reliefZ + 0.01], rot: [0, -90 + s * 34, 0], scale: 0.72 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[1], half: 1, relief, lod: 0, pos: [cfg.x + s * 1.92 * k, mY, reliefZ + 0.08], rot: [0, -90 + s * 55, 0], scale: 0.44 * k, mirror: s < 0 });+ }+ // Trumeau with its statue, on a carved base, under a canopy+ A.box(-0.32, 0.32, 0, doorH, -0.1, 0.34, { noBot: true });+ const tK = tymp.trumeauScale || 1.3, tBase = 1.06, tCanopy = tBase + figureTop(tymp.trumeau) * tK + 0.12;+ F.at(M.T(0, 0, 0.5), () => {+ F.lathe([[0.42, 0], [0.42, 0.42], [0.3, 0.56], [0.25, 0.78], [0.34, 0.92], [0.36, tBase]], 8, { faceted: true, phase: PI / 8 });+ for (let l = 0; l < 6; l++) addLeaf(F, M.of(M.T(0, 0.82, 0), M.Ry(l * 60 + 15), M.T(0.27, 0, 0)), 0.24, { coarse: 1 });+ F.at(M.T(0, tCanopy + 0.2, 0), () => F.lathe([[0.06, -0.2], [0.33, -0.08], [0.42, 0], [0.42, 0.14], [0.35, 0.18], [0.22, 0.22], [0.22, 0.58], [0.28, 0.62], [0.2, 0.66], [0.09, 0.72], [0.02, 1.1]], 6, { phase: PI / 6, faceted: true }));+ });+ figs.push({ kind: tymp.trumeau, pos: [cfg.x, tBase, 0.5], rot: [0, -90, 0], scale: tK });+ // Doors: two leaves either side of the trumeau, recessed in the wall+ const leaf = w0 / 2 - 0.34;+ O.at(M.T(0, 0, -0.14), () => { const set2 = set; I.at(M.T(0, 0, -0.14), () => { addDoorLeaf(set2, w0 / 2 - 0.02, leaf, doorH - 0.04, true); addDoorLeaf(set2, -w0 / 2 + 0.02, leaf, doorH - 0.04, false); }); });+ return { set, figs, apex, zF, outerW };+}+function placePortal(gWest, name, cfg, pr, opt = {}) {+ placeSet(name, pr.set, [cfg.x, 0, 0], 0, gWest);+ const gF = group(name + '_Figures', gWest); // the people of one door, as one group of nodes+ pr.figs.forEach((f, i) => {+ const rot = f.rot || eulerFromBasis(f.basis.X, f.basis.Y, f.basis.Z);+ const pos = [f.pos[0] + (cfg.x - pr.baseX), f.pos[1], f.pos[2]];+ // Full sculpture where people stand next to it; opt.light keeps a door's overhead scene, or the whole door, to the lighter copies+ const far = opt.light === 'all' || (opt.light === 'scene' && f.pos[1] > 4);+ placeFigure(name + '_Fig' + i, f.kind, { pos, rot, scale: f.scale, parent: gF, lod: far ? 0 : f.lod, mirror: f.mirror, half: f.half, relief: f.relief });+ });+}+function eulerFromBasis(X, Y, Z) { // degrees, three.js 'XYZ' order+ const m13 = clamp(Z[0], -1, 1), y = Math.asin(m13);+ let x, z;+ if (Math.abs(m13) < 0.9999) { x = Math.atan2(-Z[1], Z[2]); z = Math.atan2(-Y[0], X[0]); } else { x = Math.atan2(Y[2], Y[1]); z = 0; }+ return [x * 180 / PI, y * 180 / PI, z * 180 / PI];+}+// One niche of the Gallery of Kings, shared along the facade+function buildKingNiche(pitch) {+ const set = mkBatches(), { F, A } = set, h = WF.kingTop - WF.kingY, y0 = WF.kingY;+ A.face([[-pitch / 2, y0, 0.02], [pitch / 2, y0, 0.02], [pitch / 2, y0 + h, 0.02], [-pitch / 2, y0 + h, 0.02]]);+ F.box(-pitch / 2, pitch / 2, y0 - 0.5, y0, 0, 1.3, { s: 0.9 });+ F.at(M.T(0, 0, 1.3), () => F.sweep([[-pitch / 2, y0 - 0.38], [pitch / 2, y0 - 0.38]], stringSection(0.3, 0.26), { caps: false }));+ // Colonnettes and a cusped arch with a gablet over each king+ const ys = y0 + 3.15, rise = 1.3;+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0.95), () => F.lathe([[0.17, y0], [0.09, y0 + 0.16], [0.09, ys - 0.2], [0.2, ys]], 8));+ F.at(M.T(0, 0, 0.95), () => {+ const arc = pointedArc(pitch - 0.1, rise, 6).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, rollSection(0.14, 0.36, 1, 0.03, 2), { caps: false });+ // solid spandrel+ A.at(M.T(0, 0, 0.95), () => A.slab([[-pitch / 2, ys], [-(pitch - 0.1) / 2, ys], ...arc, [(pitch - 0.1) / 2, ys], [pitch / 2, ys], [pitch / 2, y0 + h], [-pitch / 2, y0 + h]], [], -0.1, 0.1, { noOuterSides: true }));+ addGable(F, pitch * 0.96, 1.12, 0.1, 0.1, ys + 0.46, { size: 0.26, crockets: 2, finial: 0.34, solid: true, lod: 0 });+ });+ // The roof of the gallery behind the arcade+ F.box(-pitch / 2, pitch / 2, y0 + h - 0.02, y0 + h + 0.12, 0, 1.28, { s: 0.85 });+ return set;+}+// One bay of the open arcade above the rose and the tower windows+function buildGalleryBay(pitch) {+ const set = mkBatches(), { F, A } = set, y0 = WF.galY, y1 = WF.galTop, ys = y0 + 3.3;+ F.shade(0.4, () => F.face([[-pitch / 2, y0, 0.03], [pitch / 2, y0, 0.03], [pitch / 2, y1, 0.03], [-pitch / 2, y1, 0.03]]));+ const arc = pointedArc(pitch * 0.92, 1.5, 5).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, 0.5), () => {+ F.sweep(arc, traceryBar(0.16, 0.36), { caps: false });+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0), () => F.lathe([[0.2, y0], [0.1, y0 + 0.2], [0.1, ys - 0.22], [0.22, ys]], 8));+ F.sweep(ringPts(0, ys + 0.6, 0.26, 7), hexBar(0.07, 0.14), { closed: true });+ });+ A.at(M.T(0, 0, 0.5), () => A.slab([[-pitch / 2, ys], [-pitch * 0.46, ys], ...arc, [pitch * 0.46, ys], [pitch / 2, ys], [pitch / 2, y1], [-pitch / 2, y1]], [], -0.18, 0.18, { noOuterSides: true }));+ F.box(-pitch / 2, pitch / 2, y0 - 0.2, y0 + 0.02, 0, 0.72, { s: 0.92 });+ F.at(M.T(0, 0, 0.72), () => { F.sweep([[-pitch / 2, y0 - 0.1], [pitch / 2, y0 - 0.1]], stringSection(0.26, 0.22), { caps: false }); F.sweep([[-pitch / 2, y1], [pitch / 2, y1]], stringSection(0.3, 0.3), { caps: false }); });+ F.at(M.T(0, 0, 0.72), () => addParapet(F, -pitch / 2, pitch / 2, y1 + 0.12, 1.05, 0.2, { pitch: 1.1, nh: 8 }));+ return set;+}+// One face of a belfry stage, shared by all eight faces of the two towers+function buildBelfryFace() {+ const set = mkBatches(), { A, F, L } = set, hw = 7, zf = 7, y0 = WF.topY, yT = P.towerTop - 2.6;+ const lw = 3.05, lx = 3.3, sill = y0 + 4.2, ys = y0 + 13.6, rise = 4.1, holes = [];+ for (const s of [-1, 1]) holes.push(archOutline(lw, sill, ys, rise, 7).map((p) => [s * lx + p[0], p[1]]));+ A.at(M.T(0, 0, zf), () => A.slab([[-hw, y0], [hw, y0], [hw, yT], [-hw, yT]], holes, -0.9, 0, { noOuterSides: true, noBack: true, sr: 0.66 }));+ // The bell chamber: a floor behind the openings, and a bell hanging on each side of the frame+ F.box(-hw + 0.8, hw - 0.8, sill - 0.5, sill - 0.2, zf - 5, zf - 0.8, {});+ set.I.at(M.T(0, sill + 3.2, zf - 2.5), () => set.I.lathe([[0.06, 1.72], [0.36, 1.62], [0.5, 1.12], [0.56, 0.7], [0.84, 0.12], [0.92, 0]], 10));+ // Behind the bells the oak frame that carries them closes the view, so no sky shows through the tower+ set.O.face([[-4.2, sill - 0.3, zf - 4], [4.2, sill - 0.3, zf - 4], [4.2, yT, zf - 4], [-4.2, yT, zf - 4]], 0.16);+ for (const s of [-1, 1]) {+ F.at(M.T(s * lx, 0, zf - 0.45), () => {+ F.sweep(pointedArc(lw, rise, 6).map((p) => [p[0], p[1] + ys]), rollSection(0.2, 0.7, 1, 0.04, 2), { caps: false });+ F.sweep([[0, sill - 0.05], [0, ys + 0.1]], traceryBar(0.2, 0.42), {});+ const lw2 = (lw - 0.1) / 2;+ for (const t of [-1, 1]) F.sweep(pointedArc(lw2, lw2 * 0.9, 4).map((p) => [t * lw2 / 2 + p[0], ys + 0.1 + p[1]]), hexBar(0.14, 0.34), { caps: false });+ F.sweep(ringPts(0, ys + rise * 0.62, 0.48, 10), hexBar(0.14, 0.34), { closed: true });+ });+ // Louvres: lead-clad boards set across the opening and tilted to throw the rain outward, dark air between them+ L.at(M.T(s * lx, 0, zf - 0.45), () => {+ const slope = 0.42;+ for (let y = sill + 0.2; y < ys + rise * 0.3; y += 0.5) {+ const h = y <= ys ? lw / 2 : (lw / 2) * Math.sqrt(Math.max(0, 1 - Math.pow((y - ys) / rise, 2)));+ L.face([[-h, y, 0.3], [h, y, 0.3], [h, y + slope, -0.3], [-h, y + slope, -0.3]]);+ L.face([[h, y - 0.05, 0.3], [-h, y - 0.05, 0.3], [-h, y + slope - 0.05, -0.3], [h, y + slope - 0.05, -0.3]]);+ }+ });+ F.at(M.T(s * lx, 0, zf), () => {+ F.sweep(pointedArc(lw + 0.5, rise + 0.3, 6).map((p) => [p[0], p[1] + ys]), hexBar(0.24, 0.28), { caps: false });+ addGable(F, lw + 0.9, 3.1, 0.03, 0.18, ys + rise * 0.5, { size: 0.42, finial: 0.8, crockets: 3, lod: 0 });+ });+ }+ F.at(M.T(0, 0, zf), () => {+ F.sweep([[-hw, y0 + 0.2], [hw, y0 + 0.2]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, sill - 0.3], [hw, sill - 0.3]], stringSection(0.32, 0.28), { caps: false });+ F.sweep([[-hw, yT - 0.2], [hw, yT - 0.2]], stringSection(0.4, 0.44), { caps: false });+ for (let i = 0; i < 12; i++) { const x = -hw + (i + 0.5) * 2 * hw / 12; F.box(x - 0.14, x + 0.14, yT - 0.78, yT - 0.42, 0, 0.3, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zf - 0.2), () => addParapet(F, -hw, hw, yT, 1.2, 0.28, { pitch: 1.4, nh: 8 }));+ return set;+}+// Octagonal corner turret and its great pinnacle, reaching the full height of the tower+function buildTowerCorner() {+ const set = mkBatches(), { A, F } = set, y0 = WF.topY, yT = P.towerTop - 1.3, R = 1.72;+ // The turret grows out of the corner on a tapering corbel, closed underneath; its lower part sits inside the corner pier+ A.lathe([[0.04, y0 - 4.2], [R * 0.66, y0 - 2.6], [R, y0 - 1], [R, yT]], 8, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < 8; i++) {+ const a = i * 45 + 22.5, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ F.at(M.of(M.Ry(a), M.T(0, 0, ap)), () => {+ if (i % 2 === 0) for (const k of [0, 1]) {+ const yb = y0 + 1 + k * 11;+ F.sweep(pointedArc(side * 0.72, 0.9, 4).map((p) => [p[0], p[1] + yb + 8.2]), hexBar(0.1, 0.12), { caps: false });+ for (const s of [-1, 1]) F.sweep([[s * side * 0.36, yb], [s * side * 0.36, yb + 8.2]], hexBar(0.1, 0.12), { caps: false });+ F.shade(0.55, () => F.face([[-side * 0.36, yb, 0.01], [side * 0.36, yb, 0.01], [side * 0.36, yb + 8.2, 0.01], [0, yb + 9.05, 0.01], [-side * 0.36, yb + 8.2, 0.01]]));+ }+ });+ }+ F.lathe([[R, y0 + 12.3], [R + 0.28, y0 + 12.5], [R + 0.28, y0 + 12.66], [R, y0 + 12.82]], 8, { faceted: true, phase: PI / 8 });+ F.lathe([[R, yT - 0.2], [R + 0.36, yT], [R + 0.36, yT + 0.26], [R, yT + 0.42]], 8, { faceted: true, phase: PI / 8 });+ return set;+}+function buildWestFront() {+ const gWest = group('West_Front');+ gWest.position.set(LAY.xW, 0, 0); gWest.rotation.y = rad(90);+ const set = mkBatches(), { A, F, L, G, D } = set, hw = WF.halfW, yTop = WF.topY, tcx = 14.5;+ // Real recessed lights flanking the tower windows, cut through the face of the wall+ const towerRecess = [];+ for (const s of [-1, 1]) for (const d of [-1, 1]) towerRecess.push(archOutline(1.5, WIN.cSill + 0.6, WIN.cSp - 0.6, 1.8, 6).map((p) => [s * tcx + d * 3.55 + p[0], p[1]]));+ // ---- The great wall with real openings: doors and rose ----+ const cP = WF.portalC, sP = WF.portalS;+ const doorHole = (x, w, h) => [[x - w / 2, 0.02], [x + w / 2, 0.02], [x + w / 2, h], [x - w / 2, h]];+ const wallOutline = [[-hw, 0], [-sP.x - sP.w0 / 2, 0], [-sP.x - sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, 0], [-cP.w0 / 2, 0], [-cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, 0], [sP.x - sP.w0 / 2, 0], [sP.x - sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]];+ A.slab(wallOutline, [ringPts(0, WF.roseY, WF.roseR, 40), ...towerRecess], -WF.wallT, 0, { noOuterSides: true, sr: 0.62 });+ towerRecess.forEach((o) => addRecess(A, F, o, 0.5));+ // Seven centuries of rain under every ledge of the front, damp rising from the ground+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.02, 3.2);+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.02, 3.6);+ addStain(D, -hw, hw, WF.galY - 0.42, 0.02, 3.4);+ addStain(D, -hw, hw, WF.kingTop + 0.02, 0.02, 2.6, 'drip');+ // damp on the wall only: the door openings are left clear+ for (const span of [[-hw, -sP.x - sP.w0 / 2], [-sP.x + sP.w0 / 2, -cP.w0 / 2], [cP.w0 / 2, sP.x - sP.w0 / 2], [sP.x + sP.w0 / 2, hw]]) addStain(D, span[0], span[1], 5.2, 0.03, 5.2, 'ground');+ // Each ledge throws a soft shadow on the wall just below it; soot gathers where the rain cannot reach+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.03, 0.8, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.03, 1.2, 'soft');+ addStain(D, -hw, hw, WF.galY - 0.42, 0.03, 1.0, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.62, 0.04, 2.4, 'soot');+ for (const s of [-1, 1]) addStain(D, s * tcx - WIN.cW / 2 - 0.4, s * tcx + WIN.cW / 2 + 0.4, WIN.cSill - 0.12, 0.02, 3.4);+ // ---- Rose: moulded ring, tracery, spandrels, relieving arch ----+ place('Great_Rose_Tracery', roseTracery(), MAT.fine, { pos: [0, WF.roseY, -0.4], parent: gWest });+ F.at(M.T(0, WF.roseY, 0), () => {+ F.at(M.T(0, 0, 0), () => {+ const ring = traceryBar(0.46, 0.58);+ F.sweep(ringPts(0, 0, WF.roseR + 0.32, 40), ring, { closed: true, z: 0.14 });+ F.sweep(ringPts(0, 0, WF.roseR + 0.78, 44), rollSection(0.2, 0.3, 1, 0.04, 2), { closed: true, z: 0.1 });+ });+ });+ F.at(M.T(0, 0, 0.03), () => {+ // spandrels round the rose: plain stone, each with a real pierced roundel carved in its corner+ const R2 = WF.roseR + 0.72, ry = WF.roseY, sq = 6.6;+ for (const [sx, sy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {+ // corner of the square -> along its edge -> round the rose -> along the other edge+ const arcPts = []; for (let i = 0; i <= 8; i++) { const a = i / 8 * PI / 2; arcPts.push([sx * R2 * Math.cos(a), sy * R2 * Math.sin(a)]); }+ const poly = [[sx * sq, sy * sq], [sx * sq, 0], ...arcPts, [0, sy * sq]].map((p) => [p[0], p[1] + ry]);+ F.slab(area2(poly) > 0 ? poly : poly.slice().reverse(), [], -0.04, 0, { noOuterSides: true, noBack: true });+ const cx = sx * (sq - 1.02), cy = ry + sy * (sq - 1.02);+ F.sweep(ringPts(cx, cy, 0.76, 14), traceryBar(0.13, 0.2), { closed: true, z: 0.05 });+ const foil = []; for (let i = 0; i < 16; i++) { const a = i * TAU / 16, R = 0.68 * (0.62 + 0.32 * Math.abs(Math.cos(a * 2))); foil.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.07, 0.14), { closed: true, z: 0.05, crease: 40 });+ }+ });+ F.at(M.T(0, 0, 0.14), () => { F.sweep([[-6.6, WF.roseY - 6.6], [6.6, WF.roseY - 6.6]], stringSection(0.3, 0.26), { caps: false }); F.sweep([[-6.6, WF.roseY + 6.6], [6.6, WF.roseY + 6.6]], stringSection(0.3, 0.26), { caps: false }); });+ // ---- Tower lower stages: paired windows with hood and gable, string courses ----+ const cw = WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = WIN.cRise;+ for (const s of [-1, 1]) {+ F.at(M.T(s * tcx, 0, 0.32), () => {+ addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 8), rollSection(0.24, 0.5, 1, 0.04, 2), { closed: true, crease: 40, z: -0.14 });+ F.sweep(pointedArc(cw + 0.8, cRise + 0.4, 7).map((p) => [p[0], p[1] + cSp]), hexBar(0.26, 0.3), { z: 0.1, caps: false });+ addGable(F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 4, lod: 0 });+ });+ F.at(M.T(s * tcx, 0, 0.02), () => { F.sweep([[-6.1, WF.kingTop + 0.24], [6.1, WF.kingTop + 0.24]], stringSection(0.3, 0.22), { caps: false }); });+ }+ // ---- Buttresses of the front: one continuous pier between the portals, narrowing as it climbs through the kings and+ // the gallery; the outer corners of the towers carry a single diagonal pier instead of two that would collide ----+ const xPier = [-7.5, 7.5], lowPanel = { k: 0.6, b: 1.1, t: 7.2, r: 0.7 };+ xPier.forEach((x, i) => {+ const m = M.T(x, 0, 0);+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: 0, y1: 11.6, w: 1.05, f: 3.6, panel: lowPanel }, { y0: 12.9, y1: 18.4, w: 0.98, f: 2.7 }, { y0: 19.7, y1: 19.72, w: 0.92, f: 1.2, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 3.6), () => { F.box(-1.17, 1.17, 0, 0.74, -3.7, 0.12, { noBot: true }); addNicheFrame(F, 0, 6.1, 11.0, 1.42); });+ // above the kings the pier stands back on the gallery's roof+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: WF.kingTop + 0.1, y1: 38.8, w: 0.92, f: 1.26 }, { y0: 40, y1: yTop + 0.1, w: 0.8, f: 0.96, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 1.26), () => addNicheFrame(F, 0, 31, 37.4, 1.3));+ D.at(M.T(x, 0, 3.6), () => { addStain(D, -1.05, 1.05, 11.4, 0.02, 2.2, 'drip'); addStain(D, -1.05, 1.05, 4.6, 0.03, 4.4, 'ground'); addStain(D, -1.05, 1.05, 11.45, 0.03, 0.7, 'soft'); });+ placeFigure('Buttress_Saint' + i, SAINTS[i + 3], { pos: [x, 6.2, 3.98], rot: [0, -90, 0], scale: 1.42, parent: gWest });+ place('Facade_Gargoyle' + i, gargoyle(i % 3), MAT.fine, { pos: [x, 17.2, 2.55], rot: [0, -90, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ placeFigure('Upper_Saint' + i, SAINTS[i + 5], { pos: [x, 31.1, 1.62], rot: [0, -90, 0], scale: 1.32, parent: gWest, lod: 0 });+ });+ for (const s of [-1, 1]) {+ const k = Math.SQRT1_2, z = 3.98, cx = s * (hw + k * z);+ placeFigure('Corner_Saint' + s, SAINTS[s > 0 ? 3 : 5], { pos: [cx, 6.2, k * z], rot: [0, -90 + s * 45, 0], scale: 1.42, parent: gWest });+ place('Corner_Gargoyle' + s, gargoyle(s > 0 ? 1 : 2), MAT.fine, { pos: [s * (hw + k * 2.65), 26.6, k * 2.65], rot: [0, -90 + s * 45, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ }+ // closing blocks where the gallery of kings dies into the corner piers+ for (const s of [-1, 1]) A.box(Math.min(s * 19.9, s * 20.6), Math.max(s * 19.9, s * 20.6), WF.kingY - 0.5, WF.kingTop + 0.12, 0, 1.3, { noBot: true });+ placeSet('West_Wall', set, [0, 0, 0], 0, gWest);+ // ---- Glass ----+ const roseWest = makeRoseGlass(WF.roseR, 101);+ SHARED.roseGlass = roseWest;+ placeGlass('Great_Rose', roseWest, [0, WF.roseY, -0.4], 180, gWest, true);+ const twinGlass = SHARED.cleres;+ for (const s of [-1, 1]) place('TowerWindow_Out' + s, glassOutside(twinGlass[s > 0 ? 0 : 1]), MAT.glassOutWin, { pos: [s * tcx, 0, 0.14], rot: [0, 180, 0], parent: gWest, scale: [1, 1, -1] });+ // ---- Three portals: a bishop, a crowned king, a veiled queen, an apostle with his key, a smiling angel;+ // the Virgin and Child on the central door post, Christ enthroned above her ----+ const pC = buildPortal(cP, ['apostle', 'bishop', 'queen', 'king', 'apostle', 'king', 'angel', 'bishop', 'queen', 'apostle'], { main: 'seated', side: ['angel', 'queen'], trumeau: 'virgin', trumeauScale: 1.4 });+ const pS = buildPortal(Object.assign({}, sP, { x: 0 }), ['queen', 'bishop', 'apostle', 'king', 'angel', 'apostle', 'king', 'queen'], { main: 'seatedVirgin', side: ['angel', 'apostle'], trumeau: 'bishop', trumeauScale: 1.24 });+ pC.baseX = cP.x; pS.baseX = 0; SHARED.sidePortal = pS;+ placePortal(gWest, 'Portal_Central', cP, pC);+ placePortal(gWest, 'Portal_South', Object.assign({}, sP, { x: -sP.x }), pS, { light: 'scene' });+ placePortal(gWest, 'Portal_North', Object.assign({}, sP, { x: sP.x }), pS, { light: 'scene' });+ // ---- Gallery of Kings ----+ const nK = P.kings, pitch = 2 * 20.1 / nK, niche = buildKingNiche(pitch), kinds = ['kingA', 'kingB'];+ for (let i = 0; i < nK; i++) {+ const x = (i + 0.5 - nK / 2) * pitch;+ placeSet('King_Niche' + i, niche, [x, 0, 0], 0, gWest);+ placeFigure('King' + i, kinds[i % 2], { pos: [x, WF.kingY + 0.04, 0.62], rot: [0, -90 + (i % 4 - 1.5) * 7, 0], scale: 1.7, parent: gWest, mirror: i % 2 === 1 });+ }+ // ---- High gallery ----+ const nG = 20, gp = 2 * 19.75 / nG, gb = buildGalleryBay(gp);+ for (let i = 0; i < nG; i++) placeSet('Gallery' + i, gb, [(i + 0.5 - nG / 2) * gp, 0, 0], 0, gWest);+ // ---- Central gable rising over the gallery, closing the nave roof ----+ const front = mkBatches(), yG0 = yTop + 1.2, yR = LAY.ridge + 1.2, gW = 2 * (zn + 1.5);+ front.A.at(M.T(0, 0, -1.2), () => front.A.slab([[-gW / 2, yG0 - 2], [gW / 2, yG0 - 2], [gW / 2, yG0], [0, yR + 1.6], [-gW / 2, yG0]], [ringPts(0, yG0 + 1.8, 1.25, 12)], -0.5, 0.5, { sr: 0.5 }));+ front.F.at(M.T(0, 0, -0.7), () => {+ addGable(front.F, gW + 0.2, yR + 1.6 - yG0, 0.04, 0.26, yG0, { size: 0.86, finial: 0.1, crockets: 7, noField: true, lod: 1 });+ front.F.sweep(ringPts(0, yG0 + 1.8, 1.32, 14), traceryBar(0.26, 0.44), { closed: true });+ // Blind arcade stepping up under the rake+ const nA = 7; for (let i = 0; i < nA; i++) { const x = (i - (nA - 1) / 2) * 1.72, top = yG0 + 0.3 + (gW / 2 - Math.abs(x) - 1) * (yR + 1.6 - yG0) / (gW / 2) * 0.7; if (Math.abs(x) < 1.7) continue; front.F.sweep([[x - 0.62, yG0], [x - 0.62, top], [x, top + 0.75], [x + 0.62, top], [x + 0.62, yG0]], hexBar(0.14, 0.16), { caps: false, crease: 20 }); }+ });+ front.G.at(M.T(0, yR + 1.62, -0.5), () => addFinial(front.G, 2.2, 1));+ placeSet('West_Gable', front, [0, 0, 0], 0, gWest);+ place('West_Gable_Glass', makeLancetRoundel(1.25), MAT.glassOut, { pos: [0, yG0 + 1.8, -0.75], parent: gWest });+ // ---- Towers: shell, belfry faces, corner turrets, lead cap ----+ // One tower shell (side and rear walls of the lower stages), built outward towards +x; the other tower mirrors it+ const shell = mkBatches(), tw = 7;+ // The outer side of the tower: real recessed arcades in two tiers, either side of its windows+ const sideM = M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), sideRecess = [];+ for (const d of [-1, 1]) {+ sideRecess.push(archOutline(1.2, 3.4, 16.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ sideRecess.push(archOutline(1.2, 26.4, 37.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ }+ shell.A.at(sideM, () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], sideRecess, -0.8, 0, { noOuterSides: true, noBack: true, sr: 0.62 }));+ shell.A.at(sideM, () => shell.F.at(sideM, () => sideRecess.forEach((o) => addRecess(shell.A, shell.F, o, 0.52))));+ shell.D.at(sideM, () => { for (const y of [19.2, WF.kingTop - 0.34, WF.galY - 0.34]) { addStain(shell.D, -tw, tw, y, 0.02, 3.2); addStain(shell.D, -tw, tw, y, 0.03, 0.8, 'soft'); } addStain(shell.D, -tw, tw, 5.2, 0.03, 5.2, 'ground'); });+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(180), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(-90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ // string courses and tall blind windows on the outer side+ shell.F.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => {+ for (const y of [1.0, 19.6, WF.kingTop, WF.galY]) shell.F.sweep([[-tw, y], [tw, y]], stringSection(0.3, 0.26), { caps: false });+ addTracery(shell.F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ shell.F.sweep(archOutline(cw, cSill, cSp, cRise, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ addGable(shell.F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 3, lod: 0 });+ addTracery(shell.F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 });+ shell.F.sweep(archOutline(...windowArgs.aisle, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ });+ // The tower's two outer corners: one diagonal pier each, from the ground to the foot of the turret it carries+ const cornerStages = [{ y0: 0, y1: 13, w: 2.0, f: 3.6 }, { y0: 14.4, y1: 27, w: 1.9, f: 2.8 }, { y0: 28.4, y1: yTop - 1, w: 1.78, f: 2.05 }];+ for (const [zc, turn] of [[0, 45], [-2 * tw, 135]]) {+ const m = M.of(M.T(tw, 0, zc), M.Ry(turn));+ shell.A.at(m, () => shell.F.at(m, () => addPier(shell, cornerStages, { back: 2, flat: true })));+ shell.F.at(m, () => shell.F.box(-2.14, 2.14, 0, 0.74, -2, 3.74, { noBot: true }));+ shell.D.at(M.of(m, M.T(0, 0, 3.6)), () => { addStain(shell.D, -2, 2, 13, 0.02, 3.2); addStain(shell.D, -2, 2, 13.02, 0.03, 0.8, 'soft'); addStain(shell.D, -2, 2, 5, 0.03, 4.3, 'ground'); });+ if (zc === 0) shell.F.at(M.of(m, M.T(0, 0, 3.6)), () => addNicheFrame(shell.F, 0, 6.1, 11, 1.42));+ }+ for (const s of [-1, 1]) {+ const cx = s * tcx;+ placeSet('Tower_Shell', shell, [cx, 0, 0], 0, gWest, { scale: [s, 1, 1] });+ // Set just proud of the wall face: these towers are solid here, so the glass is seen from outside only+ place('Tower_SideGlass_High' + s, glassOutside(twinGlass[2]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ place('Tower_SideGlass_Low' + s, glassOutside(SHARED.stories[1]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ }+ const face = buildBelfryFace(), corner = buildTowerCorner();+ const cap = new Batch(TILE.lead), capG = new Batch(TILE.gold);+ cap.lathe([[5.9, P.towerTop - 2.5], [0.2, P.towerTop + 4.8]], 8, { faceted: true, phase: PI / 8 });+ capG.at(M.T(0, P.towerTop + 4.7, 0), () => addFinial(capG, 2.4, 1));+ for (const s of [-1, 1]) {+ const gT = group('Tower_' + (s > 0 ? 'North' : 'South'), gWest), cx = s * tcx, cz = -tw;+ for (let i = 0; i < 4; i++) {+ placeSet('Belfry_Face' + i, face, [cx, 0, cz], i * 90, gT);+ const ax = cx + (i === 0 || i === 3 ? 1 : -1) * tw, az = cz + (i < 2 ? 1 : -1) * tw;+ placeSet('Tower_Turret' + i, corner, [ax, 0, az], i * 90, gT);+ // the great corner pinnacles carry the tower to its full height+ const yT = P.towerTop - 1.3 + 0.4, hP = P.spireTip - yT;+ place('Tower_Pinnacle' + i, largePinnacle(), MAT.fine, { pos: [ax, yT, az], scale: [1.16, hP / 10, 1.16], parent: gT });+ }+ place('Tower_Cap_Lead', cap, MAT.lead, { pos: [cx, 0, cz], parent: gT });+ place('Tower_Cap_Gilt', capG, MAT.gold, { pos: [cx, 0, cz], parent: gT });+ }+ // ---- Inner face: blind niches with statues, a gallery, the back of the doors ----+ const inner = mkBatches();+ inner.F.at(M.of(M.T(0, 0, -WF.wallT), M.Ry(180)), () => {+ inner.F.sweep([[-13.1, 10.2], [13.1, 10.2]], stringSection(0.34, 0.34), { caps: false });+ inner.F.sweep([[-6.5, P.triforiumSill], [6.5, P.triforiumSill]], stringSection(0.34, 0.3), { caps: false });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) addNicheFrame(inner.F, s * (3.65 + i * 1.5), 3.2 + 0, 8.8, 1.2);+ inner.F.sweep(ringPts(0, WF.roseY, WF.roseR + 0.3, 32), traceryBar(0.42, 0.5), { closed: true });+ addBlindArcade(inner.F, -6.5, 6.5, 11, 15.2, 8, { bar: 0.14, depth: 0.2 });+ });+ placeSet('West_InnerFace', inner, [0, 0, 0], 0, gWest);+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) placeFigure('Inner_Saint_' + (s > 0 ? 'N' : 'S') + i, SAINTS[i + (s > 0 ? 1 : 6)], { pos: [s * (3.65 + i * 1.5), 3.25, -WF.wallT - 0.28], rot: [0, 90, 0], scale: 1.32, parent: gWest, indoor: true });+}+function makeLancetRoundel(r) { const g = new Glass(55); g.raw(ringPts(0, 0, r + 0.05, 20), 'lead', -0.4, 0); gOculus(g, 0, 0, r, { ground: 'cobalt', inner: 'gold' }); return g.b; }++// ---- Transept fronts: one portal, a smaller rose, lancets, gable, corner turrets ----------+function buildTransFront(gT, s) {+ const fr = frame(s > 0 ? 0 : 180, LAY.xc, 0), hw = zaw + 0.9, zEnd = zn + (P.transeptBays + 1) * BAY, yTop = P.eaves;+ const set = TRANS.set || (TRANS.set = (() => {+ const q = mkBatches();+ const { A, F, G } = q, rY = 28.6, R = 4.5, w0 = WF.portalS.w0, dh = WF.portalS.ys - 0.9;+ const at = M.T(0, 0, zEnd);+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-hw, 0], [-w0 / 2, 0], [-w0 / 2, dh], [w0 / 2, dh], [w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]], [ringPts(0, rY, R, 32)], -0.9, 0, { noOuterSides: true, sr: 0.62 }));+ F.at(M.of(at, M.T(0, rY, 0.9)), () => { F.sweep(ringPts(0, 0, R + 0.3, 32), traceryBar(0.4, 0.5), { closed: true }); F.sweep(ringPts(0, 0, R + 0.72, 36), rollSection(0.18, 0.26, 1, 0.03, 2), { closed: true }); });+ q.D.at(M.of(at, M.T(0, 0, 0.9)), () => { for (const [a, c] of [[-hw, -12.3], [-6.9, 6.9], [12.3, hw]]) { addStain(q.D, a, c, 17.2, 0.02, 3.2); addStain(q.D, a, c, 17.2, 0.03, 0.8, 'soft'); } addStain(q.D, -hw, hw, 22.6, 0.02, 2.8, 'drip'); addStain(q.D, -hw, hw, 22.6, 0.03, 0.7, 'soft'); addStain(q.D, -hw, -w0 / 2, 5.2, 0.03, 5.2, 'ground'); addStain(q.D, w0 / 2, hw, 5.2, 0.03, 5.2, 'ground'); });+ // Row of lancets beneath the rose+ F.at(M.of(at, M.T(0, 0, 0.9)), () => {+ F.sweep([[-hw, 0.8], [hw, 0.8]], stringSection(0.42, 0.36), { caps: false });+ // This string runs up to the hood of each window head and stops there: it never crosses the tracery+ const hood = 9.6 - 1.62, outerHood = 9.6 + 1.62;+ F.sweep([[-hood, 17.4], [hood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ for (const t of [-1, 1]) F.sweep(t > 0 ? [[outerHood, 17.4], [hw, 17.4]] : [[-hw, 17.4], [-outerHood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ F.sweep([[-hw, 22.8], [hw, 22.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -8, 8, 18, 20.8, 8, { bar: 0.16, depth: 0.2 });+ for (const x of [-9.6, 9.6]) F.at(M.T(x, 0, 0.1), () => { addTracery(F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 }); F.sweep(archOutline(...windowArgs.aisle, 7), hexBar(0.22, 0.42), { closed: true, crease: 40, z: -0.1 }); });+ });+ // The inner face of the end wall: tiers of blind arcading and string courses+ F.at(M.of(at, M.Ry(180)), () => {+ F.sweep([[-zaw, 11], [zaw, 11]], stringSection(0.3, 0.26), { caps: false });+ F.sweep([[-zaw, 16.8], [zaw, 16.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -5.6, 5.6, 1.2, 7.8, 5, { bar: 0.16, depth: 0.2 });+ addBlindArcade(F, -zn, zn, 11.2, 14.8, 8, { bar: 0.12, depth: 0.16 });+ addBlindArcade(F, -zn, zn, 17, 20.6, 8, { bar: 0.12, depth: 0.16 });+ });+ // Gable closing the arm's roof, in front of the lead+ const yG = yTop, gW = 2 * (zn + 1.6), yR = LAY.ridge + 1;+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-gW / 2, yG], [gW / 2, yG], [0, yR]], [ringPts(0, yG + 3.4, 1.5, 12)], -0.9, 0, { noOuterSides: true, sr: 0.5 }));+ F.at(M.of(at, M.T(0, 0, 0.9)), () => { addGable(F, gW + 0.2, yR - yG, 0.02, 0.24, yG, { size: 0.82, finial: 0.1, crockets: 7, noField: true, lod: 1 }); F.sweep(ringPts(0, yG + 3.4, 1.55, 14), traceryBar(0.24, 0.42), { closed: true }); });+ G.at(M.of(at, M.T(0, yR, 1.12)), () => addFinial(G, 2.0, 1));+ F.at(M.of(at, M.T(0, 0, 1.0)), () => addParapet(F, -hw, hw, yTop - 0.1, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Each corner of the front carries one diagonal pier rising to its pinnacle, tied into both walls+ for (const sx of [-1, 1]) {+ const m = M.of(M.T(sx * hw, 0, zEnd + 0.9), M.Ry(sx * 45));+ A.at(m, () => F.at(m, () => addPier(q, [{ y0: 0, y1: 14.6, w: 1.7, f: 2.7 }, { y0: 15.8, y1: 30.6, w: 1.58, f: 2.05 }, { y0: 31.8, y1: yTop + 1, w: 1.45, f: 1.5 }], { back: 1.7, flat: true })));+ F.at(m, () => F.box(-1.84, 1.84, 0, 0.74, -1.7, 2.84, { noBot: true }));+ F.at(M.of(m, M.T(0, 0, 2.05)), () => addNicheFrame(F, 0, 16.2, 22, 1.5));+ q.D.at(M.of(m, M.T(0, 0, 2.7)), () => { addStain(q.D, -1.7, 1.7, 14.6, 0.02, 3); addStain(q.D, -1.7, 1.7, 14.62, 0.03, 0.8, 'soft'); addStain(q.D, -1.7, 1.7, 5, 0.03, 4.3, 'ground'); });+ }+ return q;+ })());+ placeSet('Transept_Front', set, [fr.ox, 0, fr.oz], fr.th, gT);+ // Portal (the same carving as the west side portals), statue niches, glass+ const gPortal = group('Transept_Portal', gT);+ gPortal.position.set(...fr.pos(0, 0, zEnd + 0.9)); gPortal.rotation.y = rad(fr.th);+ placePortal(gPortal, 'Portal_Transept', { x: 0 }, SHARED.sidePortal, { light: 'all' });+ place('Transept_Rose_Tracery', roseTracery(), MAT.fine, { pos: fr.pos(0, 28.6, zEnd + 0.45), rot: fr.rot(0), scale: [4.5 / WF.roseR, 4.5 / WF.roseR, 1], parent: gT });+ for (const sx of [-1, 1]) place('Transept_Pinnacle', largePinnacle(), MAT.fine, { pos: fr.pos(sx * hw, yTop + 1, zEnd + 0.9), scale: [1.05, 1.1, 1.05], parent: gT });+ placeGlass('Transept_Rose', SHARED.roseGlass, fr.pos(0, 28.6, zEnd + 0.45), fr.th + 180, gT, false, 4.5 / WF.roseR);+ const low = SHARED.stories[0];+ for (const sx of [-1, 1]) {+ // Aisle-end windows: the same panes on the inner and outer faces of the end wall+ place('Transept_EndWindow_In', low, MAT.glassIn, { pos: fr.pos(sx * 9.6, 0, zEnd - 0.01), rot: fr.rot(180), parent: gT });+ place('Transept_EndWindow_Out', glassOutside(low), MAT.glassOutWin, { pos: fr.pos(sx * 9.6, 0, zEnd + 0.92), rot: fr.rot(180), scale: [1, 1, -1], parent: gT });+ const dz = Math.SQRT1_2 * 2.37;+ placeFigure('Transept_Corner_Saint' + sx, SAINTS[sx > 0 ? 2 : 4], { pos: fr.pos(sx * (hw + dz), 16.25, zEnd + 0.9 + dz), rot: fr.rot(-90 + sx * 45), scale: 1.42, parent: gT, lod: 0 });+ }+}+const TRANS = {};++// ---- The east end: raised sanctuary, high altar with a gilded retable, oak choir stalls ------+function buildSanctuary() {+ const g = group('Sanctuary'), set = mkBatches(), { F, G, O } = set, { xa, xChoirW } = LAY;+ // Three broad steps and the altar, all moulded+ for (let i = 0; i < 3; i++) { const r = 5 - i * 0.7; F.at(M.T(xa + 1, 0, 0), () => F.lathe([[r, i * 0.2], [r, (i + 1) * 0.2], [0.01, (i + 1) * 0.2]], 14, { faceted: true, phase: PI / 14 })); }+ const y0 = 0.6, ax = xa + 1;+ F.at(M.T(ax, y0, 0), () => {+ F.box(-0.8, 0.8, 0, 1.0, -1.7, 1.7, {});+ F.box(-0.92, 0.92, 1.0, 1.12, -1.84, 1.84, { sb: 0.5 });+ for (const s of [-1, 1]) for (const t of [-1, 0, 1]) F.at(M.T(s * 0.82, 0, t * 1.1), () => F.lathe([[0.12, 0], [0.07, 0.12], [0.07, 0.88], [0.13, 1.0]], 6));+ });+ // A carved retable behind the altar: five stone niches under gables, each with its figure, only the arches gilded+ const retM = M.of(M.T(ax - 1.0, y0 + 1.12, 0), M.Ry(90));+ F.at(retM, () => {+ F.box(-2.46, 2.46, -0.26, 0, -0.2, 0.14, {});+ for (let i = -2; i <= 2; i++) {+ const x = i * 0.96, h = i === 0 ? 2.2 : 1.56;+ F.box(x - 0.47, x + 0.47, 0, h - 0.3, -0.2, -0.1, {});+ addGable(F, 0.96, i === 0 ? 0.8 : 0.66, 0.04, 0.06, h - 0.32, { size: 0.15, crockets: 2, finial: 0.24, solid: true, lod: 0 });+ F.at(M.T(x - 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ if (i === 2) F.at(M.T(x + 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ F.at(M.T(x - 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ if (i === 2) F.at(M.T(x + 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ }+ });+ G.at(retM, () => { for (let i = -2; i <= 2; i++) { const x = i * 0.96, h = i === 0 ? 2.2 : 1.56; G.sweep(pointedArc(0.82, 0.52, 4).map((p) => [x + p[0], h - 0.84 + p[1]]), hexBar(0.04, 0.05), { z: 0.03, caps: false }); } });+ const altarSaints = [['apostle', 0.56], ['bishop', 0.56], ['seatedVirgin', 0.74], ['king', 0.56], ['queen', 0.56]];+ altarSaints.forEach((q, n) => placeFigure('Retable_Figure' + n, q[0], { pos: [ax - 1.04, y0 + 1.14, -(n - 2) * 0.96], rot: [0, 0, 0], scale: q[1], parent: g, indoor: true, lod: q[0] === 'seatedVirgin' ? 1 : 0 }));+ // Candlesticks+ for (const t of [-1.5, -0.9, -0.3, 0.3, 0.9, 1.5]) G.at(M.T(ax - 0.2, y0 + 1.12, t), () => { G.lathe([[0.1, 0], [0.04, 0.06], [0.025, 0.42], [0.06, 0.46], [0.03, 0.5]], 6); F.at(M.T(ax - 0.2, y0 + 1.62, t), () => F.lathe([[0.022, 0], [0.022, 0.34], [0.002, 0.36]], 5, { s: 1.6 })); });+ // Choir stalls: stepped oak benches, moulded backs and a carved canopy rail+ const x0 = xa + 5.5, x1 = xChoirW - 1.2;+ for (const s of [-1, 1]) {+ const z0 = s * 5.0;+ O.box(x0, x1, 0, 0.32, z0 - 0.9, z0 + 0.9, { s: 0.42 });+ O.box(x0, x1, 0.32, 0.5, z0 + (s > 0 ? 0.0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.46 });+ O.box(x0, x1, 0, 2.7, z0 + (s > 0 ? 0.8 : -0.9), z0 + (s > 0 ? 0.9 : -0.8), { s: 0.4 });+ const n = Math.floor((x1 - x0) / 1.5);+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * (x1 - x0) / n;+ O.box(x - 0.045, x + 0.045, 0, 0.98, z0 + (s > 0 ? 0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.5 });+ O.box(x - 0.03, x + 0.03, 0.98, 2.72, z0 + (s > 0 ? 0.74 : -0.8), z0 + (s > 0 ? 0.8 : -0.74), { s: 0.48 });+ O.box(x - 0.08, x + 0.08, 2.72, 3.28, z0 + (s > 0 ? 0.56 : -0.86), z0 + (s > 0 ? 0.86 : -0.56), { s: 0.46, taper: 0.05 });+ }+ O.box(x0, x1, 2.7, 2.84, z0 + (s > 0 ? 0.3 : -0.9), z0 + (s > 0 ? 0.9 : -0.3), { s: 0.44 });+ }+ placeSet('Sanctuary', set, [0, 0, 0], 0, g);+}++// ==================================================================================+// THE GROUND. One named group, separate from the building and left out of its light, so it can simply be deleted+// when the cathedral is placed in a town. All of it stands a few centimetres above y = 0.+// ==================================================================================+const GROUND = { flagY: 0.03, earthY: 0.015, flags: 20, earth: 33, stepUp: 0.14 };+function footprintDistance(x, z) { // signed distance in plan to the outer walls of the building+ const { xa, xW, xTowerE, xc } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const box = (x0, x1, z0, z1) => { const dx = Math.max(x0 - x, x - x1), dz = Math.max(z0 - z, z - z1); return dx <= 0 && dz <= 0 ? Math.max(dx, dz) : Math.sqrt(Math.max(dx, 0) ** 2 + Math.max(dz, 0) ** 2); };+ return Math.min(box(xa, xW, -14, 14), box(xTowerE, xW, -WF.halfW, WF.halfW), box(xc - 14, xc + 14, -zEnd, zEnd), Math.sqrt((x - xa) * (x - xa) + z * z) - (CHEV.zc + CHEV.Rc + 0.5));+}+function buildGround() {+ const g = group('Ground'), { xa, xW, xc, xTowerE } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const fl = new Batch(TILE.flag), ea = new Batch(TILE.earth), st = new Batch(TILE.flag), gr = new Batch(1); gr.noColor = true;+ fl.noVoxel = ea.noVoxel = st.noVoxel = true;+ const wob = (x, z) => 2.2 * Math.sin(x * 0.31 + 1.3) * Math.cos(z * 0.27) + 1.4 * Math.sin(x * 0.13 + z * 0.19);+ const cell3 = (b, x0, z0, s, y) => { const q = [[x0, z0], [x0, z0 + s], [x0 + s, z0 + s], [x0 + s, z0]].map((p) => b.v(p[0], y(p[0], p[1]), p[1], 0, 1, 0, p[0], p[1], 1)); b.quad(q[0], q[1], q[2], q[3]); };+ // Worn flags from the walls out for about 20 m. Their edge breaks up slab by slab on the earth; nothing is left under the church floor+ const flagKeep = (x, z) => { const d = footprintDistance(x, z); return d > -2.5 && d < GROUND.flags + wob(x, z); };+ for (let gx = Math.floor((xa - 62) / 3); gx * 3 < xW + 62; gx++) for (let gz = -24; gz < 24; gz++) {+ const x0 = gx * 3, z0 = gz * 3, d = footprintDistance(x0 + 1.5, z0 + 1.5), lim = GROUND.flags + wob(x0 + 1.5, z0 + 1.5);+ if (Math.abs(d - lim) < 3.2 && d > 5) { for (const [a, c] of [[0, 0], [0, 1.5], [1.5, 0], [1.5, 1.5]]) if (flagKeep(x0 + a + 0.75, z0 + c + 0.75)) cell3(fl, x0 + a, z0 + c, 1.5, () => GROUND.flagY); } else if (flagKeep(x0 + 1.5, z0 + 1.5)) cell3(fl, x0, z0, 3, () => GROUND.flagY);+ }+ // Rough grass and packed earth under and beyond them, its edge dissolving so it sits on any ground plane without a seam+ ea.fade = (x, z) => sstep(GROUND.earth + 8, GROUND.earth - 3, footprintDistance(x, z) - wob(z, x));+ for (let gx = Math.floor((xa - 62) / 4); gx * 4 < xW + 62; gx++) for (let gz = -18; gz < 18; gz++) {+ const x0 = gx * 4, z0 = gz * 4, d = footprintDistance(x0 + 2, z0 + 2);+ if (d > GROUND.flags - 8 && d < GROUND.earth + 11) cell3(ea, x0, z0, 4, () => GROUND.earthY);+ }+ // A step or two up to every door: a landing that runs in to the door between the jambs, and a lower step in front of it+ const steps = (m, half, zF) => st.at(m, () => {+ st.box(-half - 0.2, half + 0.2, GROUND.flagY, GROUND.flagY + 2 * GROUND.stepUp, -0.2, zF + 0.75, { noBot: true });+ st.box(-half - 0.62, half + 0.62, GROUND.flagY, GROUND.flagY + GROUND.stepUp, zF + 0.75, zF + 1.5, { noBot: true });+ });+ const west = M.of(M.T(xW, 0, 0), M.Ry(90)), cP = WF.portalC, sP = WF.portalS, outer = (c) => (c.w0 + 2 * c.orders * c.dx) / 2;+ steps(west, outer(cP), cP.orders * cP.dz);+ for (const s of [-1, 1]) steps(M.of(west, M.T(s * sP.x, 0, 0)), outer(sP), sP.orders * sP.dz);+ for (const s of [0, 180]) steps(M.of(M.T(xc, 0, 0), M.Ry(s), M.T(0, 0, zEnd)), outer(sP), sP.orders * sP.dz);+ // Dark grime where the walls meet the ground: a soft band lying on the flags along every wall foot+ const strip = (ax, az, bx, bz, w) => {+ const R = DECAL_UV.soft, dx = bx - ax, dz = bz - az, l = Math.sqrt(dx * dx + dz * dz) || 1, nx = dz / l, nz = -dx / l, y = GROUND.flagY + 0.012, n = Math.max(1, Math.round(l / 5)), span = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = i / n, c = (i + 1) / n, u0 = R[0] + (i % 3) * span * 0.22, u1 = u0 + span * 0.3, pa = [ax + dx * a, az + dz * a], pc = [ax + dx * c, az + dz * c];+ const q = [gr.v(pa[0], y, pa[1], 0, 1, 0, u0, R[1], 1), gr.v(pa[0] + nx * w, y, pa[1] + nz * w, 0, 1, 0, u0, R[3], 1), gr.v(pc[0] + nx * w, y, pc[1] + nz * w, 0, 1, 0, u1, R[3], 1), gr.v(pc[0], y, pc[1], 0, 1, 0, u1, R[1], 1)];+ gr.quad(q[0], q[1], q[2], q[3]);+ }+ };+ // every strip runs with the wall on its left as seen from above, so its band falls outward+ const loop = (pts, w) => { for (let i = 0; i < pts.length - 1; i++) strip(pts[i][0], pts[i][1], pts[i + 1][0], pts[i + 1][1], w); };+ const hx = WF.halfW, xE = xc + 14, xCE = xc - 14;+ loop([[xW, hx], [xTowerE, hx], [xTowerE, 14], [xE, 14], [xE, zEnd], [xCE, zEnd], [xCE, 14], [xa, 14]], 1.8);+ loop([[xa, -14], [xCE, -14], [xCE, -zEnd], [xE, -zEnd], [xE, -14], [xTowerE, -14], [xTowerE, -hx], [xW, -hx], [xW, hx]], 1.8);+ for (let k = 1; k < P.apseSides; k++) { const f0 = frame(-k * CHEV.d, xa, 0), a = f0.pos(-0.98, 0, BUTTRESS.zB + 0.2), c = f0.pos(0.98, 0, BUTTRESS.zB + 0.2); strip(a[0], a[2], c[0], c[2], 1.3); }+ for (let k = 0; k < P.apseSides; k++) { const r = CHEV.zc + CHEV.Rc + 0.55, n = 5; for (let i = 0; i < n; i++) { const f0 = frame(-(k + 0.5) * CHEV.d, xa, 0), a0 = rad(-108 + i * 216 / n), a1 = rad(-108 + (i + 1) * 216 / n), R2 = CHEV.Rc + 0.55, p = f0.pos(R2 * Math.sin(a0), 0, CHEV.zc + R2 * Math.cos(a0)), q = f0.pos(R2 * Math.sin(a1), 0, CHEV.zc + R2 * Math.cos(a1)); strip(p[0], p[2], q[0], q[2], 1.2); } }+ place('Ground_Flagstones', fl, MAT.flag, { parent: g });+ place('Ground_Earth', ea, MAT.earth, { parent: g });+ place('Ground_Steps', st, MAT.flag, { parent: g });+ place('Ground_Grime', gr, MAT.decal, { parent: g });+}++function buildCathedral() {+ buildBody();+ buildWestFront();+ const gTN = ROOT.children.find((c) => c.name === 'Transept_North'), gTS = ROOT.children.find((c) => c.name === 'Transept_South');+ buildTransFront(gTN, 1);+ buildTransFront(gTS, -1);+ buildSanctuary();+ buildGround();+}++function previewStatues() {+ const names = P.statueTest;+ names.forEach((n, i) => placeFigure('Fig_' + n, n, { pos: [0, 0, (i - (names.length - 1) / 2) * 1.0] }));+ const base = new Batch(TILE.ashlar); base.box(-3, 3, -0.4, 0, -6, 6, { s: 0.8 }); place('Ground', base, MAT.ashlar);+}++// ==================================================================================+// BAKED LIGHT. The viewer casts no shadows, so the shadows are computed here. Every opaque surface is voxelized+// into the world, then each vertex looks at the sun, the sky and the rain:+// - rays towards the sun give the low raking light and the cast shadows;+// - rays over the hemisphere give the depth of portals and galleries, and tell inside from outside;+// - a ray straight up finds what the rain never reaches, where seven centuries of soot have stayed.+// A batch placed many times can have one set of vertex colours per side of the building, within a byte budget.+// Sets beyond the budget share the sunniest colours under a darker material.+// ==================================================================================+const SKY_RAYS = (() => {+ const r = [[0, 1, 0, 1]];+ for (let i = 0; i < 4; i++) { const a = i * TAU / 4 + 0.4, e = rad(42); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 1]); }+ for (let i = 0; i < 5; i++) { const a = i * TAU / 5 + 0.1, e = rad(9); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 0]); }+ return r;+})();+// Large meshes of moulding keep their shape with a quarter fewer triangles: the bytes are better spent up close+async function reduceBatches(share = 0.74, repeatedShare = 0.62, minTris = 2000) {+ const seen = new Map(), uses = new Map();+ for (const e of PLACED) uses.set(e.batch, (uses.get(e.batch) || 0) + 1);+ for (const e of PLACED) {+ const m = e.mat;+ if (e.batch.isFigure || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.glassOutWin || m === MAT.glassRose || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave || m === MAT.flag || m === MAT.earth) continue;+ let nb = seen.get(e.batch);+ if (nb === undefined) {+ const tris = e.batch.I.length / 3, s = uses.get(e.batch) >= 8 ? repeatedShare : share; // a mesh repeated down the whole building costs the most+ nb = tris > minTris ? await lightCopy(e.batch, Math.round(tris * s)) : null;+ if (nb) nb.noColor = e.batch.noColor;+ seen.set(e.batch, nb);+ }+ if (nb) { e.batch = nb; e.part.geometry = nb.toGeo(); }+ }+}+function samplesOf(b) {+ if (b.samples) return b.samples;+ const P0 = b.P, out = [], lim = 0.32;+ for (let i = 0; i < P0.length; i++) out.push(P0[i]);+ for (let t = 0; t < b.I.length; t += 3) {+ const a = b.I[t] * 3, c = b.I[t + 1] * 3, d = b.I[t + 2] * 3;+ const e1 = len3(P0[a] - P0[c], P0[a + 1] - P0[c + 1], P0[a + 2] - P0[c + 2]), e2 = len3(P0[c] - P0[d], P0[c + 1] - P0[d + 1], P0[c + 2] - P0[d + 2]), e3 = len3(P0[d] - P0[a], P0[d + 1] - P0[a + 1], P0[d + 2] - P0[a + 2]), m = Math.max(e1, e2, e3);+ if (m <= lim) continue;+ const n = Math.ceil(m / lim);+ for (let i = 0; i <= n; i++) for (let j = 0; j <= n - i; j++) { const u = i / n, v = j / n, w = 1 - u - v; out.push(P0[a] * u + P0[c] * v + P0[d] * w, P0[a + 1] * u + P0[c + 1] * v + P0[d + 1] * w, P0[a + 2] * u + P0[c + 2] * v + P0[d + 2] * w); }+ }+ b.samples = new Float32Array(out);+ return b.samples;+}+function bakeLight() {+ ROOT.updateMatrixWorld(true);+ const CELL = 0.4, X0 = -72, Y0 = -2, Z0 = -36, NX = 365, NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;+ const grid = new Uint8Array(NX * NY * NZ), coarse = new Uint8Array(CX * CY * CZ);+ const SUN = (() => { const v = P.sun, l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();+ const SUN2 = [[SUN[0] + 0.06, SUN[1], SUN[2] + 0.05], [SUN[0] - 0.05, SUN[1] + 0.05, SUN[2] - 0.05]].map((v) => { const l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; });+ for (const e of PLACED) {+ if (e.mat.transparent || e.batch.noVoxel) continue;+ const S = samplesOf(e.batch), m = e.part.matrixWorld.elements;+ for (let i = 0; i < S.length; i += 3) {+ const x = S[i], y = S[i + 1], z = S[i + 2], gi = ((m[0] * x + m[4] * y + m[8] * z + m[12] - X0) / CELL) | 0, gj = ((m[1] * x + m[5] * y + m[9] * z + m[13] - Y0) / CELL) | 0, gk = ((m[2] * x + m[6] * y + m[10] * z + m[14] - Z0) / CELL) | 0;+ if (gi >= 0 && gi < NX && gj >= 0 && gj < NY && gk >= 0 && gk < NZ) { grid[gi + NX * (gj + NY * gk)] = 1; coarse[(gi >> 3) + CX * ((gj >> 3) + CY * (gk >> 3))] = 1; }+ }+ }+ // How enclosed a point is: the solid voxels averaged into 1.6 m cells and blurred, read back with trilinear filtering.+ // It is a smooth function of position, so light taken from it cannot have hard edges however few vertices a surface has.+ const DX = (NX >> 2) + 2, DY = (NY >> 2) + 2, DZ = (NZ >> 2) + 2, dens = new Float32Array(DX * DY * DZ);+ for (let k = 0, q = 0; k < NZ; k++) for (let j = 0; j < NY; j++) for (let i = 0; i < NX; i++, q++) if (grid[q]) dens[(i >> 2) + DX * ((j >> 2) + DY * (k >> 2))] += 1 / 64;+ { const tmp = new Float32Array(dens.length), steps = [1, DX, DX * DY];+ for (let pass = 0; pass < 3; pass++) for (const st of steps) {+ tmp.set(dens);+ for (let q = st; q < dens.length - st; q++) dens[q] = 0.25 * tmp[q - st] + 0.5 * tmp[q] + 0.25 * tmp[q + st];+ } }+ const enclosed = (x, y, z) => {+ const fx = clamp((x - X0) / (CELL * 4), 0, DX - 1.001), fy = clamp((y - Y0) / (CELL * 4), 0, DY - 1.001), fz = clamp((z - Z0) / (CELL * 4), 0, DZ - 1.001);+ const i = fx | 0, j = fy | 0, k = fz | 0, a = fx - i, c = fy - j, d = fz - k, q = i + DX * (j + DY * k), P1 = 1, P2 = DX, P4 = DX * DY;+ const lo = (dens[q] * (1 - a) + dens[q + P1] * a) * (1 - c) + (dens[q + P2] * (1 - a) + dens[q + P2 + P1] * a) * c;+ const hi = (dens[q + P4] * (1 - a) + dens[q + P4 + P1] * a) * (1 - c) + (dens[q + P4 + P2] * (1 - a) + dens[q + P4 + P2 + P1] * a) * c;+ return lo * (1 - d) + hi * d;+ };+ // Distance to the first hit, or 1e9 if the ray leaves the world; capped rays return their cap.+ // A ray from ground beyond the voxels first flies in to them, so the building still shadows the far flagstones.+ const X1 = X0 + NX * CELL, Y1 = Y0 + NY * CELL, Z1 = Z0 + NZ * CELL;+ const march = (ox, oy, oz, dx, dy, dz, cap) => {+ let t = 0; const st = CELL * 0.8, far = CELL * CK * 0.6;+ if (ox < X0 || ox >= X1 || oy < Y0 || oy >= Y1 || oz < Z0 || oz >= Z1) {+ let t0 = 0, t1 = cap;+ for (const [o, d, lo, hi] of [[ox, dx, X0, X1], [oy, dy, Y0, Y1], [oz, dz, Z0, Z1]]) {+ if (Math.abs(d) < 1e-9) { if (o < lo || o >= hi) return 1e9; continue; }+ const a = (lo - o) / d, b = (hi - o) / d; t0 = Math.max(t0, Math.min(a, b)); t1 = Math.min(t1, Math.max(a, b));+ }+ if (t0 >= t1) return 1e9;+ t = t0 + 0.01;+ }+ while (t < cap) {+ const i = ((ox + dx * t - X0) / CELL) | 0, j = ((oy + dy * t - Y0) / CELL) | 0, k = ((oz + dz * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return 1e9;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)]) return t;+ t += st;+ }+ return cap;+ };+ const solidAt = (x, y, z) => { const i = ((x - X0) / CELL) | 0, j = ((y - Y0) / CELL) | 0, k = ((z - Z0) / CELL) | 0; return i >= 0 && i < NX && j >= 0 && j < NY && k >= 0 && k < NZ && grid[i + NX * (j + NY * k)] === 1; };+ // A vertex's light: [r,g,b] multipliers, plus how much of it was outdoors+ const light = { r: 1, g: 1, b: 1, out: 0, ndl: 0 };+ let sootFloor = 0.56, dampK = 1, indoorK = 1;+ const lightAt = (x, y, z, nx, ny, nz) => {+ // leave the voxels of the surface itself, then look around+ let ox = x + nx * CELL * 1.2, oy = y + ny * CELL * 1.2, oz = z + nz * CELL * 1.2, buried = false;+ if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 2.4; oy = y + ny * CELL * 2.4; oz = z + nz * CELL * 2.4; if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 3.6; oy = y + ny * CELL * 3.6; oz = z + nz * CELL * 3.6; buried = solidAt(ox, oy, oz); } }+ let esc = 0, skyW = 0, sky = 0, zen = 1e9;+ // A ray counts as sky only if it truly leaves the world: a long look down the nave is still indoors+ if (!buried) for (let q = 0; q < 5; q++) {+ const R = SKY_RAYS[q], d = march(ox, oy, oz, R[0], R[1], R[2], 200);+ if (q === 0) zen = d;+ const w = Math.max(0.12, R[0] * nx + R[1] * ny + R[2] * nz + 0.35);+ skyW += w; if (d >= 1e9) { sky += w; esc++; }+ }+ // The underside of a cornice sees no sky above it but is still outdoors: only there is a long look sideways worth its cost+ if (!buried && esc === 0 && ny < -0.2) for (let q = 5; q < SKY_RAYS.length && esc === 0; q++) { const R = SKY_RAYS[q]; if (march(ox, oy, oz, R[0], R[1], R[2], 90) >= 1e9) esc++; }+ const ao = clamp(1 - enclosed(x + nx, y + ny, z + nz) * 2.6), vis = skyW ? sky / skyW : 0, outdoors = esc >= 1;+ let r, g, b;+ if (outdoors) {+ const ndl = nx * SUN[0] + ny * SUN[1] + nz * SUN[2];+ let sunV = 0;+ if (ndl > 0 && !buried) {+ if (march(ox, oy, oz, SUN[0], SUN[1], SUN[2], 200) >= 1e9) sunV += 0.5;+ if (march(ox, oy, oz, SUN2[0][0], SUN2[0][1], SUN2[0][2], 200) >= 1e9) sunV += 0.25;+ if (march(ox, oy, oz, SUN2[1][0], SUN2[1][1], SUN2[1][2], 200) >= 1e9) sunV += 0.25;+ }+ const amb = 0.2 + 0.44 * vis * (0.55 + 0.45 * ao), s = sunV * Math.max(0, ndl) * 1.02;+ r = amb * 0.84 + s * 0.98; g = amb * 0.88 + s * 0.9; b = amb * 0.98 + s * 0.74;+ // What the rain never touches keeps its soot; what it washes stays pale. Gentle on long walls, whose+ // few vertices would smear it: the crisp streaks under their ledges are decals.+ const shelter = zen < 6 ? 1 - zen / 6 : 0, soot = lerp(1, sootFloor, shelter * (1 - Math.max(0, ny)));+ r *= soot; g *= soot * 0.985; b *= soot * 0.96;+ // Rising damp: darker and greener towards the ground, a slow mottling over the height of the wall+ const damp = lerp(1, sstep(0, 9, y), dampK), mot = 0.93 + 0.07 * Math.sin(x * 0.53 + y * 0.31) * Math.sin(z * 0.47 - y * 0.23);+ r *= lerp(0.56, 1, damp) * mot; g *= lerp(0.64, 1, damp) * mot; b *= lerp(0.46, 1, damp) * mot;+ light.out = 1; light.ndl = ndl;+ } else {+ // Inside: warm stone, dark low in the aisles, lifting into the vault. Every term is a smooth function of position,+ // so the light has only gradients: the aisles of the nave, of the transept arms and round the apse fade in softly+ const low = sstep(P.aisleCrown + 2.5, P.aisleCrown - 0.5, y), axis = zn - 0.1, dxc = Math.abs(x - LAY.xc), arm = sstep(16, 13, dxc);+ const naveA = sstep(axis, axis + 2.4, Math.abs(z)) * (1 - arm), armA = sstep(axis, axis + 2.4, dxc) * arm * sstep(13, 16, Math.abs(z));+ const apse = sstep(LAY.xa + 1.5, LAY.xa - 1.5, x), ambA = sstep(axis, axis + 2.4, Math.sqrt((x - LAY.xa) * (x - LAY.xa) + z * z)) * apse;+ const s = lerp(0.32, 0.86, sstep(0.5, 34, y)) * lerp(0.5, 1, ao) * lerp(1, 0.76, Math.max(naveA * (1 - apse), armA, ambA) * low) * indoorK;+ r = s * 1.06; g = s * 0.87; b = s * 0.64;+ light.out = 0; light.ndl = 0;+ }+ light.r = r; light.g = g; light.b = b;+ return light;+ };+ // ---- group placements of each batch by which way they face the sun ----+ const perBatch = new Map();+ for (const e of PLACED) { if (e.batch.noColor || e.batch.isFigure || (e.mat.transparent && !e.batch.fade)) continue; let l = perBatch.get(e.batch); if (!l) { l = []; perBatch.set(e.batch, l); } l.push(e); }+ const entries = [];+ for (const [b, list] of perBatch) {+ const groups = new Map();+ for (const e of list) {+ const m = e.part.matrixWorld.elements, sx = len3(m[0], m[1], m[2]) || 1, sy = len3(m[4], m[5], m[6]) || 1, sz = len3(m[8], m[9], m[10]) || 1;+ // the sun in this placement's own frame (mirror included)+ const lx = (m[0] * SUN[0] + m[1] * SUN[1] + m[2] * SUN[2]) / (sx * sx), ly = (m[4] * SUN[0] + m[5] * SUN[1] + m[6] * SUN[2]) / (sy * sy), lz = (m[8] * SUN[0] + m[9] * SUN[1] + m[10] * SUN[2]) / (sz * sz);+ const key = Math.round(lx * 3) + ',' + Math.round(ly * 3) + ',' + Math.round(lz * 3);+ let g = groups.get(key); if (!g) { g = { key, list: [], L: [lx, ly, lz] }; groups.set(key, g); } g.list.push(e);+ }+ const gl = [...groups.values()];+ for (const g of gl) { // how much sun this side receives, by area of normals+ let s = 0; const nn = b.N; for (let i = 0; i < nn.length; i += 3) s += Math.max(0, nn[i] * g.L[0] + nn[i + 1] * g.L[1] + nn[i + 2] * g.L[2]);+ g.expo = s / Math.max(1, nn.length / 3);+ }+ gl.sort((a, c) => c.expo - a.expo);+ entries.push({ b, gl, wall: list[0].mat === MAT.ashlar ? 0 : 1 });+ }+ // Long walls get their own light for each side first: they carry the sense of sun and shade from a distance+ entries.sort((a, c) => a.wall - c.wall || a.b.count - c.b.count);+ let spent = 0, variants = 0, tinted = 0, vbaked = 0;+ const TINTS = new Map();+ const tintOf = (mat, tier) => {+ const key = mat.uuid + '|' + tier; let m = TINTS.get(key);+ if (!m) { m = mat.clone(); m.name = mat.name + (tier === 1 ? '_ShadeSide' : '_DeepShadeSide'); m.color.setRGB(tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.76 : 0.53); TINTS.set(key, m); }+ return m;+ };+ const bakeGroup = (b, g) => {+ const rep = g.list[Math.floor(g.list.length / 2)], m = rep.part.matrixWorld.elements, n = b.count, out = new Uint8Array(n * 3), PP = b.P, NN = b.N, CC = b.C;+ let outdoor = 0;+ const onGround = rep.mat === MAT.flag || rep.mat === MAT.earth;+ sootFloor = onGround ? 1 : rep.mat === MAT.ashlar ? 0.84 : rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0.9 : 0.56;+ dampK = onGround || rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0 : 1;+ indoorK = rep.mat === MAT.oak ? 2.2 : 1; // the oak was given its own dark tone when it was built+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, x = PP[i3], y = PP[i3 + 1], z = PP[i3 + 2], a = NN[i3], c = NN[i3 + 1], d = NN[i3 + 2];+ const wx = m[0] * x + m[4] * y + m[8] * z + m[12], wy = m[1] * x + m[5] * y + m[9] * z + m[13], wz = m[2] * x + m[6] * y + m[10] * z + m[14];+ let nx = m[0] * a + m[4] * c + m[8] * d, ny = m[1] * a + m[5] * c + m[9] * d, nz = m[2] * a + m[6] * c + m[10] * d; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1; nx /= l; ny /= l; nz /= l;+ const L = lightAt(wx, wy, wz, nx, ny, nz);+ outdoor += L.out;+ out[i3] = Math.min(255, CC[i3] * L.r); out[i3 + 1] = Math.min(255, CC[i3 + 1] * L.g); out[i3 + 2] = Math.min(255, CC[i3 + 2] * L.b);+ }+ // Each vertex is one noisy sample of light: blend neighbours on the same surface so no sample can smear a dark+ // triangle across the stone. Edges between planes keep their contrast.+ {+ const sum = new Float32Array(n * 3), w = new Float32Array(n), I = b.I, passes = rep.mat === MAT.ashlar ? 3 : 2;+ const link = (p, q) => { if (NN[p * 3] * NN[q * 3] + NN[p * 3 + 1] * NN[q * 3 + 1] + NN[p * 3 + 2] * NN[q * 3 + 2] < 0.8) return; w[p]++; w[q]++; for (let c = 0; c < 3; c++) { sum[p * 3 + c] += out[q * 3 + c]; sum[q * 3 + c] += out[p * 3 + c]; } };+ for (let pass = 0; pass < passes; pass++) {+ sum.fill(0); w.fill(0);+ for (let t = 0; t < I.length; t += 3) { link(I[t], I[t + 1]); link(I[t + 1], I[t + 2]); link(I[t + 2], I[t]); }+ for (let i = 0; i < n; i++) if (w[i]) for (let c = 0; c < 3; c++) out[i * 3 + c] = out[i * 3 + c] * 0.4 + 0.6 * sum[i * 3 + c] / w[i];+ }+ }+ vbaked += n;+ return { colors: out, outdoor: outdoor / Math.max(1, n) };+ };+ for (const { b, gl } of entries) {+ const base = b.toGeo(), cost = (gl.length - 1) * b.count * 12, full = gl.length === 1 || spent + cost <= P.lightVariantBytes;+ if (full) spent += cost;+ gl.forEach((g, gi) => {+ if (gi === 0 || full) {+ const R = bakeGroup(b, g);+ g.outdoor = R.outdoor;+ if (gi === 0 && b.fade) { // a surface that dissolves at its edge carries its opacity with its light+ const rgba = new Float32Array(b.count * 4);+ for (let i = 0; i < b.count; i++) { rgba[i * 4] = R.colors[i * 3] / 255; rgba[i * 4 + 1] = R.colors[i * 3 + 1] / 255; rgba[i * 4 + 2] = R.colors[i * 3 + 2] / 255; rgba[i * 4 + 3] = b.fade(b.P[i * 3], b.P[i * 3 + 2]); }+ base.setAttribute('color', new THREE.BufferAttribute(rgba, 4));+ return;+ }+ if (gi === 0) { base.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true)); return; }+ const v = new THREE.BufferGeometry();+ v.setAttribute('position', base.attributes.position); v.setAttribute('normal', base.attributes.normal); v.setAttribute('uv', base.attributes.uv);+ v.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true));+ if (base.attributes.tangent) v.setAttribute('tangent', base.attributes.tangent);+ v.setIndex(base.index);+ for (const e of g.list) e.part.geometry = v;+ variants++;+ } else {+ // over budget: same colours, a darker stone. Skipped for batches that are mostly indoors+ tinted++;+ if (gl[0].outdoor < 0.3) return;+ const ratio = g.expo / Math.max(1e-3, gl[0].expo);+ if (ratio >= 0.78) return;+ const tier = ratio >= 0.45 ? 1 : 2;+ for (const e of g.list) e.part.material = tintOf(e.mat, tier);+ }+ });+ }+ // ---- figures keep their carved occlusion; the niche they stand in decides how much daylight reaches them ----+ let figN = 0;+ for (const f of FIG_PLACED) {+ const m = f.part.matrixWorld.elements, s = len3(m[0], m[1], m[2]) || 1, fx = m[0] / s, fy = m[1] / s, fz = m[2] / s;+ let ox = m[12] + m[4] * 1.3, oy = m[13] + m[5] * 1.3, oz = m[14] + m[6] * 1.3;+ for (let i = 0; i < 12 && solidAt(ox, oy, oz); i++) { ox += fx * CELL; oy += fy * CELL; oz += fz * CELL; }+ const L = lightAt(ox - fx * 0.05, oy - fy * 0.05, oz - fz * 0.05, fx, fy, fz);+ let mat = MAT.figureIn;+ if (L.out) { const lum = (L.r + L.g + L.b) / 3, facing = fx * SUN[0] + fy * SUN[1] + fz * SUN[2]; mat = lum > 0.56 ? MAT.figure : lum > 0.3 ? MAT.figureHalf : MAT.figureDeep; if (facing < -0.3 && mat === MAT.figure) mat = MAT.figureHalf; }+ f.part.material = mat; if (f.head) f.head.material = mat;+ figN++;+ }+ return { vertices: vbaked, variants, tinted, extraKB: Math.round(spent / 1024), figures: figN };+}++// <<ORNAMENT>>++// <<BUILD>>+async function build() {+ ROOT = createRoot('Cathedral');+ await makeMaterials();+ await prepareFigures(P.preview === 'statues' ? P.statueTest : Object.keys(FIGURES));+ if (P.preview === 'statues') { previewStatues(); return ROOT; }+ if (P.preview === 'all' || P.preview === 'unique') {+ buildCathedral();+ await reduceBatches();+ bakeLight();+ return ROOT;+ }+ if (P.preview === 'size') { // builds and bakes everything, then reports bytes as part names, and exports only a probe: fast+ buildCathedral();+ await reduceBatches();+ const baked = bakeLight(), report = ['bake_variants' + baked.variants + '_tinted' + baked.tinted + '_extraKB' + baked.extraKB];+ // Real buffer sizes, each attribute counted once: pos/normal 12, uv 8, colour 12, tangent 16 a vertex where normal-mapped+ const seenAttr = new Set(), seenTan = new Set(), byMat = {}; let bytes = 0, tris = 0;+ const add = (attr, per, key) => { if (!attr || seenAttr.has(attr)) return; seenAttr.add(attr); const b = attr.count * per; bytes += b; byMat[key] = (byMat[key] || 0) + b; };+ for (const e of PLACED) {+ const g = e.part.geometry, key = e.mat.name, a = g.attributes, m = e.part.material;+ tris += g.index.count / 3;+ add(a.position, 12, key); add(a.normal, 12, key); add(a.uv, 8, key); add(a.color, 12, key);+ add(g.index, g.index.array.BYTES_PER_ELEMENT, key);+ if (m && m.normalMap && !seenTan.has(a.position)) { seenTan.add(a.position); bytes += a.position.count * 16; byMat[key] = (byMat[key] || 0) + a.position.count * 16; }+ }+ report.push('geomKB' + Math.round(bytes / 1024) + '_meshes' + PLACED.length + '_rendered' + Math.round(tris));+ Object.keys(byMat).sort((p, q) => byMat[q] - byMat[p]).forEach((k) => report.push(k + '_KB' + Math.round(byMat[k] / 1024)));+ ROOT.clear();+ createPart('SizeProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ report.forEach((d) => group('SIZE_' + d));+ return ROOT;+ }+ if (P.preview === 'textures') { // measures the texture payload on its own+ Object.keys(MAT).forEach((k, i) => createPart('Swatch_' + k, boxGeo(1, 1, 1), MAT[k], { position: [i * 1.2, 0.5, 0], parent: ROOT }));+ return ROOT;+ }+ if (P.preview === 'stats') {+ buildCathedral();+ const list = [...STAT.entries()].map(([b, e]) => ({ name: e.name, n: e.n, tris: b.I.length / 3, kb: Math.round((batchBytes(b) + (e.normalMapped ? b.count * 16 : 0)) / 1024) })).sort((a, c) => c.kb - a.kb);+ let total = 0, draws = 0, rendered = 0; for (const e of list) { total += e.kb; draws += e.n; rendered += e.n * e.tris; }+ createPart('StatProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ group('STAT_TOTAL_kb_' + total + '_draws_' + draws + '_rendered_' + rendered + '_unique_' + list.length);+ list.slice(0, 40).forEach((e, i) => group('STAT_' + String(i).padStart(2, '0') + '_K' + Math.round(e.n * e.tris / 1000) + '_' + e.name + '_x' + e.n + '_tris' + e.tris + '_kb' + e.kb));+ return ROOT;+ }+ return ROOT;+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-4.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/cathedral-4.kiln.jsnew file mode 100644index 00000000..4ffb4b2c--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/cathedral-4.kiln.js@@ -0,0 +1,4055 @@+// Gothic cathedral: parametric High Gothic church, true scale, metres.+// +X = west front (entrance) , -X = apse, +Y up, +Z = north.+const meta = { name: 'Gothic Cathedral', role: 'wonder' };++// ==================================================================================+// PARAMETERS: change these and the whole design follows.+// ==================================================================================+const P = {+ preview: 'all', // 'all', or 'size' (true buffer bytes, fast), 'stats' (per-mesh list), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)+ // Plan+ naveBays: 11, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,+ xWest: 62, // west face of the towers+ towerDepth: 14, towerWidth: 14,+ choirBays: 3, transeptBays: 2,+ apseSides: 7,+ // Elevation+ pierTop: 15, aisleCrown: 21, triforiumSill: 21.8, triforiumTop: 26.6,+ vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,+ aisleWallTop: 23, aisleRoofHigh: 26.2,+ towerTop: 72, spireTip: 80,+ flecheTip: 100, // top of the cross on the lead spire over the crossing+ // Detail+ jambStatues: 5, kings: 16, seed: 1163,+ archFigureTris: 720, statueTest: ['apostle', 'bishop', 'king', 'queen', 'angel', 'virgin'],+ storyWindows: 3, clerestoryWindows: 3, // unique window cartoons (more = richer, larger GLB)+ // Glass+ glassGlow: 1.05, glassOuterGlow: 0.12, sunShear: [0.34, 0.86],+ sun: [0.62, 0.45, -0.64], lightVariantBytes: 250000, wallTessellation: 5, // baked low sun from the south-west; bytes allowed for per-side vertex light; metres between lit wall vertices+ chapelFacets: 5,+};++// ==================================================================================+// MATH+// ==================================================================================+const PI = Math.PI, TAU = PI * 2, rad = (d) => d * PI / 180;+const lerp = (a, b, t) => a + (b - a) * t;+const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));+const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };+const gauss = (x, y = 0) => Math.exp(-(x * x + y * y));+function mkRng(seed) { let s = seed >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }+function keyLin(keys, t) {+ if (t <= keys[0][0]) return keys[0].slice(1);+ for (let i = 1; i < keys.length; i++) if (t <= keys[i][0]) {+ const a = keys[i - 1], b = keys[i], f = (t - a[0]) / (b[0] - a[0]);+ return a.slice(1).map((v, k) => lerp(v, b[k + 1], f));+ }+ return keys[keys.length - 1].slice(1);+}+function keyf(keys, t, w = 0.03) { // smoothed keyframes [[t,v...],...]: no plateaus or kinks at the keys+ const a = keyLin(keys, t - w), b = keyLin(keys, t), c = keyLin(keys, t + w);+ return b.map((v, k) => (a[k] + 2 * v + c[k]) / 4);+}+const M = {+ I: () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],+ T: (x, y, z) => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z],+ S: (x, y = x, z = x) => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0],+ Rx: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0]; },+ Ry: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0]; },+ Rz: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0]; },+ mul: (a, b) => [+ a[0] * b[0] + a[1] * b[4] + a[2] * b[8], a[0] * b[1] + a[1] * b[5] + a[2] * b[9], a[0] * b[2] + a[1] * b[6] + a[2] * b[10], a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3],+ a[4] * b[0] + a[5] * b[4] + a[6] * b[8], a[4] * b[1] + a[5] * b[5] + a[6] * b[9], a[4] * b[2] + a[5] * b[6] + a[6] * b[10], a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7],+ a[8] * b[0] + a[9] * b[4] + a[10] * b[8], a[8] * b[1] + a[9] * b[5] + a[10] * b[9], a[8] * b[2] + a[9] * b[6] + a[10] * b[10], a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11],+ ],+ of: (...ms) => ms.reduce((a, b) => M.mul(a, b)),+};+function normalMat(m) { // inverse transpose of the 3x3, up to scale (cofactors)+ return [+ m[5] * m[10] - m[6] * m[9], m[6] * m[8] - m[4] * m[10], m[4] * m[9] - m[5] * m[8],+ m[2] * m[9] - m[1] * m[10], m[0] * m[10] - m[2] * m[8], m[1] * m[8] - m[0] * m[9],+ m[1] * m[6] - m[2] * m[5], m[2] * m[4] - m[0] * m[6], m[0] * m[5] - m[1] * m[4],+ ];+}+const det3 = (m) => m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8]) + m[2] * (m[4] * m[9] - m[5] * m[8]);++// ==================================================================================+// MESH KIT: one Batch = one mesh with one material; metric UVs, baked shading in COLOR_0+// ==================================================================================+class Batch {+ constructor(tile = 1) { this.P = []; this.N = []; this.UV = []; this.C = []; this.I = []; this.tile = tile; this.setM(M.I()); this.sh = 1; this.rgb = [1, 1, 1]; this.geo = null; }+ setM(m) { this.m = m; this.nm = normalMat(m); this.det = det3(m); }+ at(m, f) { const old = this.m; this.setM(M.mul(old, m)); f(this); this.setM(old); return this; }+ shade(s, f) { const o = this.sh; this.sh = o * s; f(this); this.sh = o; return this; }+ tint(rgb, f) { const o = this.rgb; this.rgb = rgb; f(this); this.rgb = o; return this; }+ get count() { return this.P.length / 3; }+ v(x, y, z, nx, ny, nz, u, v, s = 1) {+ const m = this.m, n = this.nm;+ const wx = m[0] * x + m[1] * y + m[2] * z + m[3], wy = m[4] * x + m[5] * y + m[6] * z + m[7], wz = m[8] * x + m[9] * y + m[10] * z + m[11];+ let ax = n[0] * nx + n[1] * ny + n[2] * nz, ay = n[3] * nx + n[4] * ny + n[5] * nz, az = n[6] * nx + n[7] * ny + n[8] * nz;+ const l = Math.hypot(ax, ay, az);+ if (l < 1e-10) { ax = 0; ay = 1; az = 0; } else { ax /= l; ay /= l; az /= l; }+ if (this.det < 0) { ax = -ax; ay = -ay; az = -az; }+ if (u === undefined || u === null) {+ const fx = Math.abs(ax), fy = Math.abs(ay), fz = Math.abs(az);+ if (fy > 0.75) { u = wx; v = wz; } else if (fx > fz) { u = wz; v = wy; } else { u = wx; v = wy; }+ }+ const sh = clamp(s * this.sh); // all light is added later by bakeLight+ const rgb = this.rgb;+ this.P.push(wx, wy, wz); this.N.push(ax, ay, az); this.UV.push(u / this.tile, v / this.tile);+ this.C.push(Math.round(255 * sh * rgb[0]), Math.round(255 * sh * rgb[1]), Math.round(255 * sh * rgb[2]));+ return this.P.length / 3 - 1;+ }+ tri(a, b, c) { if (this.det < 0) this.I.push(a, c, b); else this.I.push(a, b, c); }+ quad(a, b, c, d) { this.tri(a, b, c); this.tri(a, c, d); }+ // flat polygon from 3D points (CCW seen from the front); convex fan+ face(pts, s = 1) {+ const a = pts[0], b = pts[1], c = pts[pts.length - 1];+ let nx = (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), ny = (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), nz = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ if (pts.length > 3 && Math.hypot(nx, ny, nz) < 1e-9) { const d = pts[2]; nx = (b[1] - a[1]) * (d[2] - a[2]) - (b[2] - a[2]) * (d[1] - a[1]); ny = (b[2] - a[2]) * (d[0] - a[0]) - (b[0] - a[0]) * (d[2] - a[2]); nz = (b[0] - a[0]) * (d[1] - a[1]) - (b[1] - a[1]) * (d[0] - a[0]); }+ const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l;+ const ids = pts.map((p) => this.v(p[0], p[1], p[2], nx, ny, nz, undefined, undefined, p[3] === undefined ? s : p[3]));+ for (let i = 1; i < ids.length - 1; i++) this.tri(ids[0], ids[i], ids[i + 1]);+ return this;+ }+ box(x0, x1, y0, y1, z0, z1, o = {}) {+ const s = o.s === undefined ? 1 : o.s, sb = o.sb === undefined ? s * 0.62 : o.sb, st = o.st === undefined ? s : o.st;+ const tx = o.taper || 0;+ const X0 = x0 + tx, X1 = x1 - tx, Z0 = z0 + (o.taperZ === undefined ? tx : o.taperZ), Z1 = z1 - (o.taperZ === undefined ? tx : o.taperZ);+ this.face([[x1, y0, z1, s], [x1, y0, z0, s], [X1, y1, Z0, st], [X1, y1, Z1, st]]); // +X+ this.face([[x0, y0, z0, s], [x0, y0, z1, s], [X0, y1, Z1, st], [X0, y1, Z0, st]]); // -X+ this.face([[x0, y0, z1, s], [x1, y0, z1, s], [X1, y1, Z1, st], [X0, y1, Z1, st]]); // +Z+ this.face([[x1, y0, z0, s], [x0, y0, z0, s], [X0, y1, Z0, st], [X1, y1, Z0, st]]); // -Z+ if (!o.noTop) this.face([[X0, y1, Z1, st], [X1, y1, Z1, st], [X1, y1, Z0, st], [X0, y1, Z0, st]]);+ if (!o.noBot) this.face([[x0, y0, z0, sb], [x1, y0, z0, sb], [x1, y0, z1, sb], [x0, y0, z1, sb]]);+ return this;+ }+ // Surface of revolution about Y. prof: [[r,y],...] bottom to top. o.sides, o.faceted, o.crease, o.flat+ lathe(prof, n = 12, o = {}) {+ const a0 = o.phase || 0, cr = Math.cos(rad(o.crease === undefined ? 55 : o.crease));+ const s = o.s === undefined ? 1 : o.s;+ const segs = [];+ for (let j = 0; j < prof.length - 1; j++) {+ const dr = prof[j + 1][0] - prof[j][0], dy = prof[j + 1][1] - prof[j][1], l = Math.hypot(dr, dy) || 1;+ segs.push([dy / l, -dr / l, l]);+ }+ if (o.faceted) { // hard-edged polygon of revolution+ for (let i = 0; i < n; i++) {+ const t0 = a0 + TAU * i / n, t1 = a0 + TAU * (i + 1) / n, tm = (t0 + t1) / 2;+ const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1), cm = Math.cos(tm), sm = Math.sin(tm);+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const p0 = prof[j], p1 = prof[j + 1];+ const w0 = p0[0] * Math.hypot(c1 - c0, s1 - s0), w1 = p1[0] * Math.hypot(c1 - c0, s1 - s0);+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ const a = this.v(p0[0] * c0, p0[1], p0[0] * s0, nr * cm, ny, nr * sm, -w0 / 2, vv, sA);+ const b = this.v(p1[0] * c0, p1[1], p1[0] * s0, nr * cm, ny, nr * sm, -w1 / 2, vv + l, sB);+ const c = this.v(p1[0] * c1, p1[1], p1[0] * s1, nr * cm, ny, nr * sm, w1 / 2, vv + l, sB);+ const d = this.v(p0[0] * c1, p0[1], p0[0] * s1, nr * cm, ny, nr * sm, w0 / 2, vv, sA);+ this.quad(a, b, c, d); vv += l;+ }+ }+ return this;+ }+ // smooth around, creased along the profile when the section turns sharply+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const prev = segs[j - 1], next = segs[j + 1];+ let n0r = nr, n0y = ny, n1r = nr, n1y = ny;+ if (prev && prev[0] * nr + prev[1] * ny > cr) { n0r = prev[0] + nr; n0y = prev[1] + ny; }+ if (next && next[0] * nr + next[1] * ny > cr) { n1r = next[0] + nr; n1y = next[1] + ny; }+ const base = this.count, p0 = prof[j], p1 = prof[j + 1];+ const circ = Math.max(p0[0], p1[0]) * TAU, uScale = Math.max(1, Math.round(circ)) / n;+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ for (let i = 0; i <= n; i++) {+ const t = a0 + TAU * i / n, c = Math.cos(t), sn = Math.sin(t);+ this.v(p0[0] * c, p0[1], p0[0] * sn, n0r * c, n0y, n0r * sn, i * uScale, vv, sA);+ this.v(p1[0] * c, p1[1], p1[0] * sn, n1r * c, n1y, n1r * sn, i * uScale, vv + l, sB);+ }+ for (let i = 0; i < n; i++) this.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+ vv += l;+ }+ return this;+ }+ // Sweep a closed section (a,b CCW; a along in-plane left normal, b along +Z) along a path in the XY plane.+ sweep(path, sec, o = {}) {+ const closedPath = !!o.closed, cr = Math.cos(rad(o.crease === undefined ? 50 : o.crease)), z0 = o.z || 0, s = o.s === undefined ? 1 : o.s;+ const n = path.length, E = [];+ const segCount = closedPath ? n : n - 1;+ for (let i = 0; i < segCount; i++) { const p = path[i], q = path[(i + 1) % n]; const dx = q[0] - p[0], dy = q[1] - p[1], l = Math.hypot(dx, dy) || 1e-6; E.push([dx / l, dy / l, l]); }+ // section normals+ const K = sec.length, SN = [];+ for (let j = 0; j < K; j++) { const a = sec[j], b = sec[(j + 1) % K]; const da = b[0] - a[0], db = b[1] - a[1], l = Math.hypot(da, db) || 1e-6; SN.push([db / l, -da / l, l]); }+ const rings = []; let dist = 0;+ for (let i = 0; i < segCount; i++) {+ const e = E[i], ePrev = closedPath ? E[(i - 1 + segCount) % segCount] : (E[i - 1] || e), eNext = closedPath ? E[(i + 1) % segCount] : (E[i + 1] || e);+ const p = path[i], q = path[(i + 1) % n];+ const miter = (A, B) => { let bx = -(A[1] + B[1]), by = A[0] + B[0]; const l = Math.hypot(bx, by) || 1; bx /= l; by /= l; const nlx = -B[1], nly = B[0]; const c = Math.max(0.35, bx * nlx + by * nly); return [bx / c, by / c]; };+ const m0 = miter(ePrev, e), m1 = miter(e, eNext);+ const smooth0 = ePrev[0] * e[0] + ePrev[1] * e[1] > cr, smooth1 = e[0] * eNext[0] + e[1] * eNext[1] > cr;+ const nl = [-e[1], e[0]];+ const n0 = smooth0 ? [-(e[1] + ePrev[1]), e[0] + ePrev[0]] : nl, n1 = smooth1 ? [-(e[1] + eNext[1]), e[0] + eNext[0]] : nl;+ const ln0 = Math.hypot(n0[0], n0[1]) || 1, ln1 = Math.hypot(n1[0], n1[1]) || 1;+ let perim = 0;+ for (let j = 0; j < K; j++) {+ const A = sec[j], B = sec[(j + 1) % K], sn = SN[j], pv = SN[(j - 1 + K) % K], nx = SN[(j + 1) % K];+ const sA = sn[0] * pv[0] + sn[1] * pv[1] > cr, sB = sn[0] * nx[0] + sn[1] * nx[1] > cr;+ const nA = sA ? [sn[0] + pv[0], sn[1] + pv[1]] : sn, nB = sB ? [sn[0] + nx[0], sn[1] + nx[1]] : sn;+ const norm = (na, nb, nv, ln) => { const l = Math.hypot(na, nb) || 1; const a = na / l, b = nb / l; return [a * nv[0] / ln, a * nv[1] / ln, b]; };+ const na0 = norm(nA[0], nA[1], n0, ln0), nb0 = norm(nB[0], nB[1], n0, ln0), na1 = norm(nA[0], nA[1], n1, ln1), nb1 = norm(nB[0], nB[1], n1, ln1);+ const shA = A[2] === undefined ? s : A[2] * s, shB = B[2] === undefined ? s : B[2] * s;+ const a = this.v(p[0] + A[0] * m0[0], p[1] + A[0] * m0[1], z0 + A[1], na0[0], na0[1], na0[2], dist, perim, shA);+ const b = this.v(p[0] + B[0] * m0[0], p[1] + B[0] * m0[1], z0 + B[1], nb0[0], nb0[1], nb0[2], dist, perim + sn[2], shB);+ const c = this.v(q[0] + B[0] * m1[0], q[1] + B[0] * m1[1], z0 + B[1], nb1[0], nb1[1], nb1[2], dist + e[2], perim + sn[2], shB);+ const d = this.v(q[0] + A[0] * m1[0], q[1] + A[0] * m1[1], z0 + A[1], na1[0], na1[1], na1[2], dist + e[2], perim, shA);+ this.quad(a, b, c, d);+ perim += sn[2];+ }+ dist += e[2];+ rings.push(m0);+ }+ if (!closedPath && o.caps !== false) {+ const capAt = (p, nv, e, flip) => {+ const pts = sec.map((A) => [p[0] + A[0] * nv[0], p[1] + A[0] * nv[1], z0 + A[1]]);+ const idx = triangulate(sec.map((A) => [A[0], A[1]]));+ const ids = pts.map((q) => this.v(q[0], q[1], q[2], flip * e[0], flip * e[1], 0, q[0], q[2], s * 0.8));+ for (const t of idx) { if (flip > 0) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); else this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ };+ capAt(path[0], rings[0], E[0], -1);+ const lastE = E[segCount - 1]; capAt(path[n - 1], [-lastE[1], lastE[0]], lastE, 1);+ }+ return this;+ }+ // Generic tube along a 3D path with parallel transport. radii: number|array|fn(t) -> [ra, rb]; ring: fn(angle,t) multiplier+ tube(path, radii, n = 8, o = {}) {+ const N = path.length, s = o.s === undefined ? 1 : o.s;+ let T = [], U = o.up ? [...o.up] : null;+ for (let i = 0; i < N; i++) {+ const p = path[Math.max(0, i - 1)], q = path[Math.min(N - 1, i + 1)];+ let tx = q[0] - p[0], ty = q[1] - p[1], tz = q[2] - p[2]; const l = Math.hypot(tx, ty, tz) || 1; T.push([tx / l, ty / l, tz / l]);+ }+ if (!U) U = Math.abs(T[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];+ const base = this.count; const rowLen = n + 1; let dist = 0;+ for (let i = 0; i < N; i++) {+ const t = T[i];+ let d = U[0] * t[0] + U[1] * t[1] + U[2] * t[2];+ let ux = U[0] - d * t[0], uy = U[1] - d * t[1], uz = U[2] - d * t[2]; const ul = Math.hypot(ux, uy, uz) || 1; ux /= ul; uy /= ul; uz /= ul; U = [ux, uy, uz];+ const vx = t[1] * uz - t[2] * uy, vy = t[2] * ux - t[0] * uz, vz = t[0] * uy - t[1] * ux;+ const f = i / (N - 1);+ const rr = typeof radii === 'function' ? radii(f) : (Array.isArray(radii) ? radii[i] : radii);+ const ra = Array.isArray(rr) ? rr[0] : rr, rb = Array.isArray(rr) ? rr[1] : rr;+ if (i > 0) dist += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1], path[i][2] - path[i - 1][2]);+ for (let k = 0; k <= n; k++) {+ const a = TAU * k / n, c = Math.cos(a), sn = Math.sin(a);+ const mod = o.ring ? o.ring(a, f) : 1;+ const shade = o.ringS ? o.ringS(a, f) : 1;+ const px = path[i][0] + (ux * c * ra + vx * sn * rb) * mod, py = path[i][1] + (uy * c * ra + vy * sn * rb) * mod, pz = path[i][2] + (uz * c * ra + vz * sn * rb) * mod;+ const nx = ux * c / (ra || 1) + vx * sn / (rb || 1), ny = uy * c / (ra || 1) + vy * sn / (rb || 1), nz = uz * c / (ra || 1) + vz * sn / (rb || 1);+ this.v(px, py, pz, nx, ny, nz, k / n * Math.max(ra, rb) * TAU, dist, s * shade);+ }+ }+ for (let i = 0; i < N - 1; i++) for (let k = 0; k < n; k++) {+ const a = base + i * rowLen + k, b = a + 1, c = a + rowLen + 1, d = a + rowLen;+ this.quad(a, b, c, d);+ }+ if (o.capEnd !== false) { const c = this.v(path[N - 1][0], path[N - 1][1], path[N - 1][2], T[N - 1][0], T[N - 1][1], T[N - 1][2], 0, 0, s); const b2 = base + (N - 1) * rowLen; const ids = []; for (let k = 0; k < n; k++) ids.push(b2 + k); for (let k = 0; k < n; k++) this.tri(c, ids[k], b2 + k + 1); }+ if (o.capStart) { const c = this.v(path[0][0], path[0][1], path[0][2], -T[0][0], -T[0][1], -T[0][2], 0, 0, s); for (let k = 0; k < n; k++) this.tri(c, base + k + 1, base + k); }+ return this;+ }+ // parametric grid: fn(u,v) -> [x,y,z, shade?]; winding u x v = front. Smooth normals from the grid.+ grid(fn, nu, nv, o = {}) {+ const pts = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pts.push(fn(i / nu, j / nv));+ const id = (i, j) => j * (nu + 1) + i, base = this.count, flip = o.flip ? -1 : 1, s = o.s === undefined ? 1 : o.s;+ const wrapU = !!o.wrapU;+ let vLen = 0;+ for (let j = 0; j <= nv; j++) {+ if (j > 0) { const a = pts[id(0, j - 1)], b = pts[id(0, j)]; vLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ // o.metric: measure across from the middle of each row, so a converging surface keeps true scale instead of a fan of streaks+ let uLen = 0;+ if (o.metric) { let total = 0; for (let i = 1; i <= nu; i++) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; total += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); } uLen = -total / 2; }+ for (let i = 0; i <= nu; i++) {+ if (i > 0) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; uLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ const iP = wrapU ? (i === 0 ? nu - 1 : i - 1) : Math.max(0, i - 1), iN = wrapU ? (i === nu ? 1 : i + 1) : Math.min(nu, i + 1);+ const A = pts[id(iP, j)], B = pts[id(iN, j)], C = pts[id(i, Math.max(0, j - 1))], D = pts[id(i, Math.min(nv, j + 1))];+ const ux = B[0] - A[0], uy = B[1] - A[1], uz = B[2] - A[2], vx = D[0] - C[0], vy = D[1] - C[1], vz = D[2] - C[2];+ const p = pts[id(i, j)];+ const uvU = o.boxUV ? undefined : uLen, uvV = o.boxUV ? undefined : vLen;+ this.v(p[0], p[1], p[2], flip * (uy * vz - uz * vy), flip * (uz * vx - ux * vz), flip * (ux * vy - uy * vx), uvU, uvV, s * (p[3] === undefined ? 1 : p[3]));+ }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) {+ const a = base + id(i, j), b = base + id(i + 1, j), c = base + id(i + 1, j + 1), d = base + id(i, j + 1);+ if (flip > 0) this.quad(a, b, c, d); else this.quad(a, d, c, b);+ }+ return this;+ }+ // 2D outline with holes, extruded along Z from z0 to z1 (front +Z). Reveal sides included.+ slab(outline, holes = [], z0 = 0, z1 = 0.1, o = {}) {+ const so = area2(outline) > 0 ? outline : outline.slice().reverse();+ const hs = holes.map((h) => (area2(h) < 0 ? h : h.slice().reverse()));+ const tris = triangulate(so, hs);+ const all = so.concat(...hs);+ const sf = o.sf === undefined ? 1 : o.sf, sb = o.sb === undefined ? 1 : o.sb, sr = o.sr === undefined ? 0.72 : o.sr;+ if (!o.noFront) { const ids = all.map((p) => this.v(p[0], p[1], z1, 0, 0, 1, undefined, undefined, sf)); for (const t of tris) this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ if (!o.noBack) { const ids = all.map((p) => this.v(p[0], p[1], z0, 0, 0, -1, undefined, undefined, sb)); for (const t of tris) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); }+ const side = (loop, s) => {+ const L = loop.length; if (o.noSides) return;+ for (let i = 0; i < L; i++) {+ const p = loop[i], q = loop[(i + 1) % L]; const dx = q[0] - p[0], dy = q[1] - p[1];+ const l = Math.hypot(dx, dy) || 1, nx = dy / l, ny = -dx / l;+ const a = this.v(p[0], p[1], z0, nx, ny, 0, undefined, undefined, s), b = this.v(q[0], q[1], z0, nx, ny, 0, undefined, undefined, s), c = this.v(q[0], q[1], z1, nx, ny, 0, undefined, undefined, s), d = this.v(p[0], p[1], z1, nx, ny, 0, undefined, undefined, s);+ this.quad(a, b, c, d);+ }+ };+ if (!o.noOuterSides) side(so, o.so === undefined ? 0.85 : o.so);+ hs.forEach((h) => side(h, sr));+ return this;+ }+ // Vertical prism from an (x,z) polygon+ prism(pts, y0, y1, o = {}) {+ let A = 0; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; A += p[0] * q[1] - q[0] * p[1]; }+ const L = A > 0 ? pts : pts.slice().reverse(), s = o.s === undefined ? 1 : o.s, st = o.st === undefined ? s : o.st, n = L.length, k = o.topScale === undefined ? 1 : o.topScale;+ let cx = 0, cz = 0; for (const p of L) { cx += p[0]; cz += p[1]; } cx /= n; cz /= n;+ const top = L.map((p) => [cx + (p[0] - cx) * k, cz + (p[1] - cz) * k]);+ for (let i = 0; i < n; i++) {+ const p = L[i], q = L[(i + 1) % n], P2 = top[i], Q2 = top[(i + 1) % n];+ this.face([[q[0], y0, q[1], s], [p[0], y0, p[1], s], [P2[0], y1, P2[1], st], [Q2[0], y1, Q2[1], st]]);+ }+ if (!o.noTop && k > 1e-4) { const c = top.map((p) => this.v(p[0], y1, p[1], 0, 1, 0, undefined, undefined, st)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i + 1], c[i]); }+ if (o.bot) { const c = L.map((p) => this.v(p[0], y0, p[1], 0, -1, 0, undefined, undefined, s * 0.6)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i], c[i + 1]); }+ return this;+ }+ // Welds vertices that agree in every attribute: sweeps and lathes otherwise export many duplicates+ // Split every edge longer than L. The rule depends only on the edge, so neighbours split alike and no crack opens.+ // Long walls need vertices for the baked light to land on.+ tess(L) {+ this.weld();+ const L2 = L * L, { P: p, N: nn, UV: uv, C: c } = this;+ for (let pass = 0; pass < 7; pass++) {+ const mid = new Map(), I2 = [], I0 = this.I; let changed = false;+ const long = (a, b) => { const x = p[a * 3] - p[b * 3], y = p[a * 3 + 1] - p[b * 3 + 1], z = p[a * 3 + 2] - p[b * 3 + 2]; return x * x + y * y + z * z > L2; };+ const midOf = (a, b) => {+ const lo = Math.min(a, b), hi = Math.max(a, b), key = lo * 4194304 + hi; let m = mid.get(key);+ if (m === undefined) {+ m = p.length / 3; mid.set(key, m);+ p.push((p[lo * 3] + p[hi * 3]) / 2, (p[lo * 3 + 1] + p[hi * 3 + 1]) / 2, (p[lo * 3 + 2] + p[hi * 3 + 2]) / 2);+ let nx = nn[lo * 3] + nn[hi * 3], ny = nn[lo * 3 + 1] + nn[hi * 3 + 1], nz = nn[lo * 3 + 2] + nn[hi * 3 + 2]; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;+ nn.push(nx / l, ny / l, nz / l);+ uv.push((uv[lo * 2] + uv[hi * 2]) / 2, (uv[lo * 2 + 1] + uv[hi * 2 + 1]) / 2);+ c.push((c[lo * 3] + c[hi * 3]) >> 1, (c[lo * 3 + 1] + c[hi * 3 + 1]) >> 1, (c[lo * 3 + 2] + c[hi * 3 + 2]) >> 1);+ }+ return m;+ };+ for (let t = 0; t < I0.length; t += 3) {+ const a = I0[t], b = I0[t + 1], d = I0[t + 2], ab = long(a, b), bd = long(b, d), da = long(d, a);+ if (!ab && !bd && !da) { I2.push(a, b, d); continue; }+ changed = true;+ const m1 = ab ? midOf(a, b) : -1, m2 = bd ? midOf(b, d) : -1, m3 = da ? midOf(d, a) : -1;+ if (ab && bd && da) I2.push(a, m1, m3, m1, b, m2, m3, m2, d, m1, m2, m3);+ else if (ab && bd) I2.push(a, m1, d, m1, m2, d, m1, b, m2);+ else if (bd && da) I2.push(b, m2, a, m2, m3, a, m2, d, m3);+ else if (da && ab) I2.push(d, m3, b, m3, m1, b, m3, a, m1);+ else if (ab) I2.push(a, m1, d, m1, b, d);+ else if (bd) I2.push(b, m2, a, m2, d, a);+ else I2.push(d, m3, b, m3, a, b);+ }+ this.I = I2;+ if (!changed) break;+ }+ this.geo = null;+ return this;+ }+ weld() {+ if (this.welded) return this;+ const n = this.count, map = new Map(), remap = new Int32Array(n), P2 = [], N2 = [], U2 = [], C2 = [];+ const { P: p, N: nn, UV: uv, C: c } = this, col = !this.noColor;+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, i2 = i * 2;+ const key = Math.round(p[i3] * 4000) + ',' + Math.round(p[i3 + 1] * 4000) + ',' + Math.round(p[i3 + 2] * 4000) + ',' + Math.round(nn[i3] * 90) + ',' + Math.round(nn[i3 + 1] * 90) + ',' + Math.round(nn[i3 + 2] * 90) + ',' + Math.round(uv[i2] * 800) + ',' + Math.round(uv[i2 + 1] * 800) + (col ? ',' + c[i3] + ',' + c[i3 + 1] + ',' + c[i3 + 2] : '');+ let k = map.get(key);+ if (k === undefined) { k = P2.length / 3; map.set(key, k); P2.push(p[i3], p[i3 + 1], p[i3 + 2]); N2.push(nn[i3], nn[i3 + 1], nn[i3 + 2]); U2.push(uv[i2], uv[i2 + 1]); C2.push(c[i3], c[i3 + 1], c[i3 + 2]); }+ remap[i] = k;+ }+ const I2 = [];+ for (let t = 0; t < this.I.length; t += 3) {+ const a = remap[this.I[t]], b = remap[this.I[t + 1]], d = remap[this.I[t + 2]];+ if (a !== b && b !== d && a !== d) I2.push(a, b, d);+ }+ this.P = P2; this.N = N2; this.UV = U2; this.C = C2; this.I = I2; this.welded = true;+ return this;+ }+ toGeo() {+ if (this.geo) return this.geo;+ this.weld();+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.P), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(this.N), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.UV), 2));+ if (!this.noColor) g.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(this.C), 3, true));+ g.setIndex(new THREE.BufferAttribute(this.count > 65535 ? new Uint32Array(this.I) : new Uint16Array(this.I), 1));+ this.geo = g;+ return g;+ }+}+function area2(p) { let a = 0; for (let i = 0; i < p.length; i++) { const q = p[(i + 1) % p.length]; a += p[i][0] * q[1] - q[0] * p[i][1]; } return a / 2; }+function triangulate(contour, holes = []) {+ const V = (p) => new THREE.Vector2(p[0], p[1]);+ const tris = THREE.ShapeUtils.triangulateShape(contour.map(V), holes.map((h) => h.map(V)));+ const all = contour.concat(...holes);+ return tris.map((t) => {+ const a = all[t[0]], b = all[t[1]], c = all[t[2]];+ const s = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ return s >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]];+ });+}++// ---- 2D curves -------------------------------------------------------------------+// Two-centred pointed arc: span w, rise h, base y=0 centred at x=0, from left spring to right spring+function pointedArc(w, h, n = 8) {+ const c = (h * h - w * w / 4) / w, r = c + w / 2, pts = [];+ const a0 = PI, a1 = Math.atan2(h, -c);+ for (let i = 0; i <= n; i++) { const a = lerp(a0, a1, i / n); pts.push([c + r * Math.cos(a), r * Math.sin(a)]); }+ for (let i = n - 1; i >= 0; i--) { const a = lerp(a0, a1, i / n); pts.push([-(c + r * Math.cos(a)), r * Math.sin(a)]); }+ pts[n][0] = 0;+ return pts;+}+// CCW outline of a pointed opening: width w, sill y0, spring ys, rise h+function archOutline(w, y0, ys, h, n = 8) {+ const arc = pointedArc(w, h, n).map((p) => [p[0], p[1] + ys]);+ arc.reverse(); // right spring -> apex -> left spring (CCW)+ return [[-w / 2, y0], [w / 2, y0], ...arc];+}+const ringPts = (cx, cy, r, n = 24, ph = 0) => { const a = []; for (let i = 0; i < n; i++) { const t = ph + TAU * i / n; a.push([cx + r * Math.cos(t), cy + r * Math.sin(t)]); } return a; };+const ofsY = (pts, dy) => pts.map((p) => [p[0], p[1] + dy]);+const ofsXY = (pts, dx, dy) => pts.map((p) => [p[0] + dx, p[1] + dy]);++// ---- Sections (a, b, shade?) closed CCW ------------------------------------------+// Moulded arch section with convex rolls: a=0 at the soffit, grows outward; symmetric in b.+function rollSection(w, d, rolls = 3, amp = 0.07, seg = 3) {+ const half = [];+ const A = [w, d / 2], B = [0, d * 0.12], dx = B[0] - A[0], dy = B[1] - A[1], l = Math.hypot(dx, dy);+ for (let k = 0; k < rolls; k++) for (let i = 0; i < seg; i++) {+ const t = (k + i / seg) / rolls, ph = i / seg;+ const off = amp * Math.pow(Math.sin(PI * ph), 0.7), sh = 0.68 + 0.32 * Math.sin(PI * ph);+ half.push([lerp(A[0], B[0], t) + off * dy / l, lerp(A[1], B[1], t) - off * dx / l, sh]);+ }+ half.push([B[0], B[1], 0.8]);+ const back = half.slice().reverse().map((p) => [p[0], -p[1], p[2]]);+ return [[w, -d / 2, 1], ...half, ...back.slice(0, -1)];+}+// Cheapest moulded bar: a hexagon, smooth enough to read as a roll at distance+function hexBar(w, d) { const a = w / 2, b = d / 2; return [[a, -b * 0.5], [a, b * 0.5], [0, b], [-a, b * 0.5], [-a, -b * 0.5], [0, -b]]; }+// Simple chamfered bar centred on the path, width w (in plane), depth d+function barSection(w, d, ch = 0.25) {+ const a = w / 2, b = d / 2, c = Math.min(a, b) * ch * 2;+ return [[-a, -b + c], [-a + c, -b], [a - c, -b], [a, -b + c], [a, b - c], [a - c, b], [-a + c, b], [-a, b - c]];+}+// Tracery bar with a roll on the front and back faces+function traceryBar(w, d) {+ const a = w / 2, b = d / 2, s = [];+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([a * Math.cos(t), b * 0.62 + b * 0.38 * Math.sin(t)]); }+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([-a * Math.cos(t), -b * 0.62 - b * 0.38 * Math.sin(t)]); }+ return s;+}+// String course / cornice projecting along +b from the wall at b=0, height h (a)+function stringSection(h = 0.3, out = 0.25) {+ return [[-h / 2, 0], [-h / 2, out * 0.3, 0.55], [-h * 0.25, out * 0.62, 0.7], [-h * 0.02, out * 0.72, 0.85], [h * 0.12, out, 1], [h * 0.2, out * 0.98, 1], [h / 2, out * 0.05, 1], [h / 2, 0]].reverse();+}++const spline = (pts, sub = 4) => { // Catmull-Rom through 3D points+ const out = [], n = pts.length, g = (i) => pts[clamp(i, 0, n - 1)];+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < sub; k++) {+ const t = k / sub, p0 = g(i - 1), p1 = g(i), p2 = g(i + 1), p3 = g(i + 2), v = [];+ for (let c = 0; c < 3; c++) v.push(0.5 * (2 * p1[c] + (p2[c] - p0[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t * t + (3 * p1[c] - p0[c] - 3 * p2[c] + p3[c]) * t * t * t));+ out.push(v);+ }+ out.push(pts[n - 1]);+ return out;+};+const blob = (b, c, r, nu = 8, nv = 6, sh = 1) => b.grid((u, v) => {+ const ph = -PI + TAU * u, la = -PI / 2 + PI * v;+ return [c[0] + r[0] * Math.cos(la) * Math.cos(ph), c[1] + r[1] * Math.sin(la), c[2] + r[2] * Math.cos(la) * Math.sin(ph), sh];+}, nu, nv, { flip: true, wrapU: true });++// ==================================================================================+// MATERIALS: procedural PBR, with height-derived normals+// ==================================================================================+const MAT = {};+const TILE = { ashlar: 8, fine: 2, lead: 8, pave: 2.4, oak: 1.2, iron: 0.8, gold: 1, flag: 6, earth: 5 };++function ashlarOps(rng, S, tone, joint, stains) {+ // Irregular coursed blocks; shared between albedo and height by replaying the same rng seed+ const ops = [], rows = 7, rowH = Math.floor(S / rows);+ for (let r = 0; r < rows; r++) {+ const y0 = r * rowH; let x = Math.round(rng() * S / 5) % S;+ const n = 3 + Math.floor(rng() * 2), widths = [];+ for (let i = 0; i < n; i++) widths.push(0.7 + rng() * 0.6);+ const tot = widths.reduce((a, c) => a + c, 0);+ for (let i = 0; i < n; i++) {+ const w = Math.round(widths[i] / tot * S), dv = rng(), a = rng();+ const col = tone[Math.floor(dv * tone.length)];+ const x0 = x % S, wr = Math.min(w, S - x0), op = Math.round(10 + a * 34);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y0, width: wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ if (wr < w) ops.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: y0, width: w - wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ x += w;+ ops.push({ op: 'paint', shape: { kind: 'line', from: [x % S, y0], to: [x % S, y0 + rowH], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y0], to: [S, y0], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ return ops.slice(0, 58).concat(stains || []);+}++// Regions of the decal sheet as fractions [u0, v0, u1, v1]; a streak's top edge is at v0+const DECAL_UV = { streak: [0, 0, 0.5, 0.5], soft: [0.5, 0, 1, 0.25], soot: [0.5, 0.25, 1, 0.5], ground: [0, 0.5, 0.5, 1], drip: [0.5, 0.75, 1, 1] };+async function makeMaterials() {+ const S = 512, repeat = { minFilter: 'linear-mipmap-linear', magFilter: 'linear', wrapS: 'repeat', wrapT: 'repeat' };+ const noise = (a, b, scale, oct, seed, opacity, blend) => ({ op: 'noise', colorA: a, colorB: b, scale, octaves: oct, seed, opacity, blend: blend || 'normal' });+ const spec3 = (usage, base, ops, size = S) => ({ schemaVersion: 3, width: size, height: size, usage, operations: [{ op: 'legacy-v2', spec: { schemaVersion: 2, size, usage, layers: base } }, ...ops] });+ const v3 = (usage, base, ops, size = S) => proceduralTexture(spec3(usage, base, ops, size));+ const stamps = (shape, rgba, count, seed, opacity, area) => ({ op: 'stamps', shape, rgba, count, seed, area: area || { x: 0, y: 0, width: S, height: S }, opacity });+ const ell = (w, h) => ({ kind: 'ellipse', x: 0, y: 0, width: w, height: h });+ const tool = (dx, dy, w = 1) => ({ kind: 'line', from: [0, 0], to: [dx, dy], width: w }); // a stroke of the claw chisel++ // ---- Weathered ashlar: 8 m repeat, every block its own stone. Colour varies by block, chisel marks+ // in the relief instead of round pits, rain and soot already sunk into the surface ----+ const quads = [[0, 0], [S, 0], [0, S], [S, S]];+ const ashAlbedo = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb9ad96 }, noise(0x968b76, 0xcdc3ad, 3, 2, seed + 1, 0.85), noise(0x7f7666, 0xc6bca6, 17, 2, seed + 2, 0.4)],+ ashlarOps(mkRng(seed), S, [[78, 72, 60], [226, 216, 190], [158, 132, 92], [122, 128, 124]], { w: 2, rgba: [70, 64, 54, 255], op: 225 }, [+ stamps(ell(20, 70), [70, 66, 56, 255], 26, seed + 3, 24), stamps(ell(40, 26), [52, 56, 46, 255], 16, seed + 4, 20), stamps(tool(11, 6), [96, 88, 74, 255], 110, seed + 5, 34),+ ]));+ const ashHeight = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb4b4b4 }, noise(0x8c8c8c, 0xd0d0d0, 11, 2, seed + 6, 0.7)],+ ashlarOps(mkRng(seed), S, [[126, 126, 126], [214, 214, 214], [170, 170, 170], [150, 150, 150]], { w: 3, rgba: [12, 12, 12, 255], op: 255 }, [+ stamps(tool(14, 8), [86, 86, 86, 255], 120, seed + 8, 90), stamps(tool(-12, 9), [226, 226, 226, 255], 110, seed + 9, 70),+ ]));+ const atlasOf = async (usage, make, seeds) => textureAtlas({ schemaVersion: 1, width: 2 * S, height: 2 * S, pack: { edge: 2048, gutter: 0 }, usage, sampler: repeat, regions: quads.map((p, i) => ({ name: 'q' + i, content: { x: p[0], y: p[1], width: S, height: S }, gutter: 0, source: { kind: 'procedural', spec: make(p, seeds[i]) } })) });+ const ashA = await atlasOf('albedo', ashAlbedo, [77, 131, 211, 307]);+ const ashH = await atlasOf('albedo', ashHeight, [77, 131, 211, 307]);+ MAT.ashlar = pbrMaterial({ albedo: ashA, normal: normalMapFromHeight(ashH, { strength: 2.6 }), roughness: 0.94, metalness: 0, name: 'Limestone_Ashlar' });++ // Dressed stone: no joints; chisel strokes and mottling live in the colour, because a normal map here would+ // add 16 bytes of tangent to every vertex of every moulding+ const finA = v3('albedo', [{ op: 'solid', color: 0xc2b7a0 }, noise(0xb5ab95, 0xcdc4af, 5, 3, 31, 0.8), noise(0xa79d88, 0xcbc2ad, 23, 3, 32, 0.34)], [+ stamps(tool(9, 5), [120, 112, 96, 255], 130, 33, 34), stamps(tool(-7, 6), [214, 206, 188, 255], 80, 34, 30),+ ]);+ MAT.fine = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name: 'Limestone_Dressed' });+ const figTone = (name, r, g, b) => { const m = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name }); m.color.setRGB(r, g, b); m.vertexColors = true; return m; };+ MAT.figure = figTone('Sculpture_Sunlit', 0.86, 0.8, 0.68); // figures take the same stone under different light+ MAT.figureHalf = figTone('Sculpture_HalfShade', 0.5, 0.49, 0.47);+ MAT.figureDeep = figTone('Sculpture_DeepShade', 0.3, 0.31, 0.34);+ MAT.figureIn = figTone('Sculpture_Interior', 0.62, 0.53, 0.42);++ // ---- Patinated lead: long sheets a metre wide running the whole way down the slope. They differ only a little from each+ // other, are lapped once in a long run, and carry the streaks that rain draws down them. The tile repeats every 8 m ----+ const LS = 512, leadOps = (tones, lapOp, seamOp, rngSeed) => {+ const r = mkRng(rngSeed), ops = [], cols = 8, W = LS / cols;+ for (let c = 0; c < cols; c++) {+ const t = tones[Math.floor(r() * tones.length)], lap = Math.floor((c % 2 ? 0.28 : 0.72) * LS + (r() - 0.5) * 40);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: c * W, y: 0, width: W, height: LS }, rgba: [t[0], t[1], t[2], 255], opacity: 12 + Math.floor(r() * 26) });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, lap], to: [(c + 1) * W, lap], width: lapOp.w }, rgba: lapOp.rgba, opacity: lapOp.op });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, 0], to: [c * W, LS], width: seamOp.w }, rgba: seamOp.rgba, opacity: seamOp.op });+ }+ return ops;+ };+ const leadArea = { x: 0, y: 0, width: LS, height: LS };+ const leadA = v3('albedo', [{ op: 'solid', color: 0x6f7a7e }, noise(0x566166, 0x8d9896, 5, 2, 41, 0.75)], [+ ...leadOps([[86, 98, 102], [118, 130, 130], [66, 76, 82], [140, 150, 146], [100, 104, 100]], { w: 2, rgba: [36, 42, 46, 255], op: 220 }, { w: 2, rgba: [40, 46, 50, 255], op: 180 }, 44),+ stamps({ kind: 'rectangle', x: 0, y: 0, width: 4, height: 180 }, [208, 214, 206, 255], 70, 45, 22, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 2, height: 260 }, [34, 40, 44, 255], 90, 46, 36, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 6, height: 120 }, [150, 160, 154, 255], 40, 47, 16, leadArea),+ ], LS);+ const leadH = v3('albedo', [{ op: 'solid', color: 0x8c8c8c }, noise(0x848484, 0x989898, 9, 2, 48, 0.8)], [+ ...leadOps([[140, 140, 140], [132, 132, 132], [148, 148, 148]], { w: 3, rgba: [232, 232, 232, 255], op: 255 }, { w: 5, rgba: [250, 250, 250, 255], op: 255 }, 44),+ stamps(ell(17, 11), [116, 116, 116, 255], 110, 49, 60, leadArea),+ ], LS);+ MAT.lead = pbrMaterial({ albedo: leadA, normal: normalMapFromHeight(leadH, { strength: 3 }), roughness: 0.6, metalness: 0.5, name: 'Lead_Patinated' });++ // ---- Gold leaf: a bright warm metal that still reads as gold where the environment is dull ----+ const gldA = v3('albedo', [{ op: 'solid', color: 0xffc644 }, noise(0xf0a830, 0xffd764, 14, 2, 51, 0.6)], [stamps(ell(5, 4), [176, 110, 30, 255], 60, 52, 90, { x: 0, y: 0, width: 128, height: 128 })], 128);+ MAT.gold = pbrMaterial({ albedo: gldA, roughness: 0.25, metalness: 1, name: 'Gilding' });+ MAT.gold.color.setRGB(0.92, 0.78, 0.56);+ MAT.gold.emissive.setRGB(1, 0.6, 0.14); MAT.gold.emissiveIntensity = 0.1;++ // Aged oak and forged iron from the approved scanned sets+ MAT.oak = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.oak-veneer-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.oak-veneer-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.oak-veneer-arm.v1'), name: 'Oak_Aged' });+ MAT.iron = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.rusted-metal-albedo.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.rusted-metal-arm.v1'), name: 'Iron_Forged' });++ // Paving: worn limestone and grey marble, polished by seven centuries of feet+ const pavA = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'checker', colorA: 0xd2cab6, colorB: 0x8c8678, squares: 4 }, noise(0x7c766a, 0xe0dac8, 9, 3, 61, 0.42), { op: 'bricks', brick: 0xffffff, mortar: 0x5a564e, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ const pavH = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'solid', color: 0xc8c8c8 }, noise(0xb0b0b0, 0xdcdcdc, 15, 3, 62, 0.5), { op: 'bricks', brick: 0xffffff, mortar: 0x202020, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ MAT.pave = pbrMaterial({ albedo: pavA, normal: normalMapFromHeight(pavH, { strength: 1.8 }), roughness: 0.42, metalness: 0, name: 'Paving_Worn' });+ for (const k of ['ashlar', 'fine', 'oak', 'iron', 'pave', 'lead']) MAT[k].vertexColors = true;++ // ---- Weathering decals: rain streaks, soot, ground damp and moss, cast shadow. Alpha-blended overlays+ // give crisp detail on walls that have very few vertices ----+ const DS = 256, rd = mkRng(99), polyStreak = (x, y0, L, w, rgb, op) => ({ op: 'paint', shape: { kind: 'polygon', points: [[x, y0], [x + w, y0], [x + w * 0.8 + 1, y0 + L * 0.45], [x + w * 0.55 + 1, y0 + L * 0.8], [x + w * 0.45, y0 + L], [x + w * 0.3, y0 + L * 0.75], [x + w * 0.15, y0 + L * 0.4]].map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: [rgb[0], rgb[1], rgb[2], 255], opacity: op });+ const streakOps = [], dripOps = [];+ for (let i = 0; i < 44; i++) { const x = Math.floor(rd() * (DS - 9)), dark = rd() > 0.26, L = 65 + Math.floor(rd() * 185), w = 2 + Math.floor(rd() * 7); streakOps.push(polyStreak(x, 0, Math.min(L, DS - 2), w, dark ? [44, 40, 34] : [176, 170, 150], dark ? 40 + Math.floor(rd() * 70) : 26 + Math.floor(rd() * 30))); }+ for (let i = 0; i < 12; i++) streakOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 3 + i * 2 }, rgba: [34, 32, 28, 255], opacity: 12 });+ for (let i = 0; i < 38; i++) { const x = Math.floor(rd() * (DS - 8)), L = 20 + Math.floor(rd() * 100), w = 1 + Math.floor(rd() * 6); dripOps.push(polyStreak(x, 0, Math.min(L, 125), w, [38, 36, 30], 44 + Math.floor(rd() * 70))); }+ for (let i = 0; i < 10; i++) dripOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 2 + i * 2 }, rgba: [28, 26, 24, 255], opacity: 16 });+ const softOps = []; for (let i = 0; i < 30; i++) softOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: Math.max(1, Math.round(128 * Math.pow(1 - i / 30, 1.5))) }, rgba: [18, 18, 24, 255], opacity: 15 });+ const dampOps = [];+ for (let i = 0; i < 18; i++) { const h = Math.round(DS * Math.pow((i + 1) / 18, 1.6)); dampOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: DS - h, width: DS, height: h }, rgba: i % 3 ? [38, 42, 28, 255] : [50, 58, 34, 255], opacity: 9 }); }+ dampOps.push(stamps(ell(13, 8), [66, 84, 42, 255], 110, 71, 46, { x: 0, y: 120, width: DS, height: 134 }), stamps(ell(5, 28), [30, 32, 24, 255], 80, 72, 64, { x: 0, y: 60, width: DS, height: 190 }), stamps(ell(3, 3), [120, 132, 76, 255], 60, 73, 90, { x: 0, y: 100, width: DS, height: 150 }));+ const clear = [0, 0, 0, 0];+ const dreg = (name, x, y, w, h, ops, bg) => ({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'albedo', background: bg || clear, operations: ops } } });+ const decalTex = await textureAtlas({ schemaVersion: 1, width: 512, height: 512, usage: 'albedo', regions: [+ dreg('streak', 0, 0, DS, DS, streakOps.slice(0, 64)),+ dreg('soft', 256, 0, DS, 128, softOps),+ dreg('soot', 256, 128, DS, 128, [stamps(ell(35, 27), [24, 22, 20, 255], 150, 74, 20, { x: 20, y: 15, width: 200, height: 90 }), stamps(ell(13, 11), [18, 16, 14, 255], 90, 75, 36, { x: 45, y: 25, width: 160, height: 75 })]),+ dreg('ground', 0, 256, DS, DS, dampOps),+ dreg('drip', 256, 384, DS, 128, dripOps.slice(0, 64)),+ ] });+ MAT.decal = pbrMaterial({ albedo: decalTex, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Weathering_Decals' });+ MAT.decal.depthWrite = false;++ await makeGlassMaterials();++ // ---- The light of the windows on the floor: a soft shape, painted very small and enlarged, so its edges melt ----+ MAT.pool = pbrMaterial({ roughness: 0.95, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightPool' });+ MAT.pool.vertexColors = true;+ MAT.pool.depthWrite = false;++ // ---- The ground: worn flags in irregular courses, and rough grass over packed earth ----+ const flagTones = [[74, 70, 62], [150, 142, 124], [112, 104, 88], [96, 98, 90]];+ const flagA = v3('albedo', [{ op: 'solid', color: 0x8f8778 }, noise(0x6f695c, 0xa89f8d, 4, 2, 81, 0.8), noise(0x5e594e, 0x9a9282, 19, 2, 82, 0.35)], ashlarOps(mkRng(83), S, flagTones, { w: 4, rgba: [38, 34, 28, 255], op: 235 }, [stamps(ell(30, 18), [168, 160, 140, 255], 40, 84, 26), stamps(tool(8, 3), [70, 66, 58, 255], 70, 85, 30)]));+ const flagH = v3('albedo', [{ op: 'solid', color: 0xb0b0b0 }, noise(0x969696, 0xc4c4c4, 9, 2, 86, 0.6)], ashlarOps(mkRng(83), S, [[140, 140, 140], [190, 190, 190], [165, 165, 165], [150, 150, 150]], { w: 5, rgba: [20, 20, 20, 255], op: 255 }, [stamps(ell(26, 16), [205, 205, 205, 255], 30, 87, 40)]));+ MAT.flag = pbrMaterial({ albedo: flagA, normal: normalMapFromHeight(flagH, { strength: 2.2 }), roughness: 0.9, metalness: 0, name: 'Ground_Flagstone' });+ MAT.flag.vertexColors = true;+ const EA = 256, eArea = { x: 0, y: 0, width: EA, height: EA };+ const earthA = v3('albedo', [{ op: 'solid', color: 0x5d5a3a }, noise(0x4a4a2c, 0x6f6a44, 5, 2, 91, 0.8), noise(0x3f4226, 0x7a7550, 23, 2, 92, 0.4)], [stamps(tool(2, 8), [70, 88, 40, 255], 120, 93, 90, eArea), stamps(tool(-2, 7), [104, 112, 58, 255], 90, 94, 70, eArea), stamps(ell(26, 16), [88, 74, 52, 255], 30, 95, 60, eArea)], EA);+ MAT.earth = pbrMaterial({ albedo: earthA, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Ground_Earth' });+ MAT.earth.vertexColors = true;+ MAT.earth.depthWrite = false;+}++// ---- Atlas of the glass painter: pot-metal colours, painted drapery, drawn faces, ornament ------+const GL = { w: 1536, h: 1280, cell: 256, small: 128, fam: {}, tile: {}, face: {}, orn: {}, ground: {} };+// [name, base, light, dark]: real pot-metal glass is deep, and no two sheets match+const GLASS_FAMILIES = [+ ['ruby', [118, 6, 14], [172, 14, 22], [66, 3, 10]], ['cobalt', [10, 20, 104], [20, 44, 160], [5, 10, 62]], ['azure', [16, 54, 128], [36, 90, 170], [8, 32, 88]], ['emerald', [6, 62, 38], [14, 96, 54], [3, 38, 24]],+ ['green', [42, 74, 24], [74, 106, 34], [24, 48, 14]], ['gold', [178, 116, 16], [218, 158, 36], [130, 78, 8]], ['amber', [146, 72, 10], [186, 106, 20], [98, 44, 6]], ['purple', [58, 14, 80], [90, 26, 114], [34, 6, 50]],+ ['flesh', [186, 140, 104], [206, 164, 128], [156, 110, 80]], ['white', [172, 164, 132], [204, 194, 158], [136, 128, 100]], ['brown', [70, 40, 18], [100, 58, 28], [46, 24, 12]],+ ['teal', [8, 62, 70], [16, 92, 100], [5, 38, 46]], ['night', [5, 8, 40], [10, 16, 62], [2, 4, 24]],+];+const GLASS_ALIAS = { rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', sky: 'azure', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', yellow: 'gold', tan: 'amber', fleshDk: 'flesh', violet: 'purple', olive: 'green', forest: 'emerald', hair: 'brown', paint: 'lead' };+// Mosaic grounds, each a square of painted lozenges: [name, colour A, colour B, jewel]+// A ground stays one colour, in many sheets of it: ruby beside cobalt blends to magenta across a room, ruby on ruby does not+const GLASS_GROUNDS = [['rc', 'ruby', 'ruby', 'gold'], ['cr', 'cobalt', 'cobalt', 'gold'], ['eg', 'emerald', 'emerald', 'gold'], ['pr', 'cobalt', 'azure', 'white']];+// What a leaded window looks like from outside by day: nearly black quarries, each with a hint of its colour, and a faint grey came+function outerGlassOps() {+ const r = mkRng(4711), ops = [], cl = (v) => Math.max(0, Math.min(256, v)), hints = [[88, 10, 16], [12, 20, 96], [12, 20, 70], [70, 10, 14], [22, 26, 34], [66, 44, 12]];+ for (let b = 0; b <= 4; b++) for (let a = 0; a <= 4; a++) {+ if ((a + b) % 2 === 0) continue;+ const c = hints[Math.floor(r() * hints.length)], x = a * 64, y = b * 64;+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 64)], [cl(x + 64), cl(y)], [cl(x), cl(y + 64)], [cl(x - 64), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -1; k <= 1; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ for (let k = 1; k <= 3; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ return ops;+}+function groundOps(A, B, J, seed) { // lozenges 64 px across, a lead round each, a jewel on some crossings+ const r = mkRng(seed * 19 + 5), ops = [], cl = (v) => Math.max(0, Math.min(256, v));+ for (let b = 0; b <= 8; b++) for (let a = 0; a <= 8; a++) {+ if ((a + b) % 2 === 0) continue;+ const f = b % 2 ? A : B, x = a * 32, y = b * 32, t = r(), c = t < 0.45 ? f[1] : t < 0.8 ? f[2] : f[3];+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 32)], [cl(x + 32), cl(y)], [cl(x), cl(y + 32)], [cl(x - 32), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -3; k <= 3; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let k = 1; k <= 7; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let i = 0; i < 4; i++) { const x = 64 + (i % 2) * 128, y = 64 + (i >> 1) * 128; ops.push(paintEll(x, y, 13, 13, 255, [4, 4, 5])); ops.push(paintEll(x, y, 8, 8, 255, J[1])); }+ return ops.slice(0, 64);+}+const DRAPE_OF = { ruby: 'ruby', rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', cobalt: 'cobalt', azure: 'cobalt', sky: 'cobalt', night: 'cobalt', emerald: 'emerald', green: 'emerald', teal: 'emerald', forest: 'emerald', olive: 'emerald', gold: 'gold', amber: 'gold', yellow: 'gold', tan: 'gold', brown: 'gold', white: 'white', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', purple: 'purple', violet: 'purple' };+const GROUND_OF = { ruby: 'rc', rubyLt: 'rc', crimson: 'rc', cobalt: 'cr', azure: 'pr', sky: 'pr', emerald: 'eg', green: 'eg', violet: 'cr', purple: 'cr', teal: 'pr', outer: 'out' };+const PAINT = [36, 20, 10];+const paintLine = (a, b, w, op = 235) => ({ op: 'paint', shape: { kind: 'line', from: [Math.round(a[0]), Math.round(a[1])], to: [Math.round(b[0]), Math.round(b[1])], width: w }, rgba: [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintPoly = (pts, op, rgb) => ({ op: 'paint', shape: { kind: 'polygon', points: pts.map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintEll = (x, y, w, h, op, rgb) => ({ op: 'paint', shape: { kind: 'ellipse', x: Math.round(x - w / 2), y: Math.round(y - h / 2), width: Math.round(w), height: Math.round(h) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });++function familyOps(f, seed) { // one sheet of pot-metal: streaks, clouds, seeds, a thicker edge+ const r = mkRng(seed * 7 + 3), ops = [], C = GL.small, clampE = (x, w) => Math.max(w / 2, Math.min(C - w / 2, x));+ for (let i = 0; i < 10; i++) { const c = r() > 0.5 ? f[2] : f[3], w = 24 + Math.floor(r() * 60), h = 18 + Math.floor(r() * 44); ops.push(paintEll(clampE(r() * C, w), clampE(r() * C, h), w, h, 60 + Math.floor(r() * 90), c)); }+ for (let i = 0; i < 9; i++) { const c = r() > 0.5 ? f[2] : f[3], y = Math.floor(r() * C); ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y], to: [C, Math.max(0, Math.min(C, y + Math.floor((r() - 0.5) * 30)))], width: 2 + Math.floor(r() * 4) }, rgba: [c[0], c[1], c[2], 255], opacity: 70 + Math.floor(r() * 80) }); }+ ops.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 2, height: 2 }, rgba: [f[2][0], f[2][1], f[2][2], 255], count: 40, seed: seed + 11, area: { x: 0, y: 0, width: C, height: C }, opacity: 180 });+ return ops;+}+function drapeOps(f, seed) { // brown paint on coloured glass: long folds, each with a soft wash beside its trace line+ const r = mkRng(seed * 13 + 1), ops = [], W = 256, H = 512, dk = [Math.round(f[1][0] * 0.3), Math.round(f[1][1] * 0.3), Math.round(f[1][2] * 0.3)];+ for (let i = 0; i < 13; i++) {+ const x = 18 + i * 18 + (r() - 0.5) * 10, sw = (r() - 0.5) * 42, y0 = 4 + r() * 120, y1 = H - r() * 90, ym = (y0 + y1) / 2, wash = 9 + r() * 12;+ ops.push(paintPoly([[x, y0], [x + wash, y0 + 10], [x + sw * 0.6 + wash * 1.4, ym], [x + sw + wash, y1 - 10], [x + sw, y1], [x + sw * 0.6, ym]], 62 + Math.floor(r() * 36), PAINT));+ ops.push(paintLine([x, y0], [x + sw * 0.6, ym], 3)); ops.push(paintLine([x + sw * 0.6, ym], [x + sw, y1], 2));+ if (i % 3 === 0) ops.push(paintLine([x + sw * 0.6 + wash * 1.6, ym - 40], [x + sw + wash * 1.6, y1 - 60], 3, 150));+ }+ for (let i = 0; i < 4; i++) { const y = 60 + i * 110 + r() * 40; ops.push(paintPoly([[0, y], [W, y + 26 * (r() - 0.5)], [W, y + 22], [0, y + 18]], 40, PAINT)); }+ return ops.slice(0, 62);+}+// A drawn medieval head, 256 x 256, centred: trace lines in brown over a flesh pane, with washes for the modelling+function faceOps(kind) {+ const ops = [], c = 128, mir = (p) => [256 - p[0], p[1]], both = (fn) => { fn((p) => p); fn(mir); };+ const hairC = kind === 'elder' ? [150, 146, 122] : kind === 'angel' ? [150, 96, 30] : [66, 40, 18];+ // washes: the side of the face and temple, under the brow, beside the nose, under the lip+ both((m) => {+ ops.push(paintPoly([m([18, 84]), m([52, 74]), m([46, 150]), m([72, 214]), m([34, 196])], 78));+ ops.push(paintEll(m([88, 104])[0], 106, 58, 20, 64));+ ops.push(paintPoly([m([122, 118]), m([112, 160]), m([102, 172]), m([118, 172])], 58));+ });+ ops.push(paintEll(c, 200, 52, 11, 58));+ // hair+ if (kind !== 'virgin') {+ ops.push(paintPoly([[6, 118], [14, 56], [60, 10], [128, 0], [196, 10], [242, 56], [250, 118], [222, 96], [206, 56], [160, 42], [128, 54], [96, 42], [50, 56], [34, 96]], 255, hairC));+ for (let i = 0; i < 7; i++) { const a = 28 + i * 16; ops.push(paintLine([a, 94 - i * 7], [a + 36, 12 + i * 2], 3, 190)); ops.push(paintLine([256 - a, 94 - i * 7], [256 - a - 36, 12 + i * 2], 3, 190)); }+ } else {+ ops.push(paintPoly([[0, 150], [2, 56], [50, 8], [128, 0], [206, 8], [254, 56], [256, 150], [226, 118], [214, 66], [128, 46], [42, 66], [30, 118]], 255, [214, 216, 186]));+ ops.push(paintLine([38, 124], [50, 62], 4)); ops.push(paintLine([50, 62], [128, 44], 4)); ops.push(paintLine([128, 44], [206, 62], 4)); ops.push(paintLine([206, 62], [218, 124], 4));+ }+ // brows, lids, eyes+ both((m) => {+ ops.push(paintLine(m([60, 96]), m([84, 84]), 5)); ops.push(paintLine(m([84, 84]), m([116, 92]), 5));+ ops.push(paintLine(m([64, 116]), m([84, 104]), 4)); ops.push(paintLine(m([84, 104]), m([112, 116]), 4));+ ops.push(paintLine(m([66, 118]), m([90, 126]), 2)); ops.push(paintLine(m([90, 126]), m([112, 116]), 2));+ ops.push(paintEll(m([88, 115])[0], 115, 15, 15, 255));+ });+ // nose, nostril, mouth, chin+ ops.push(paintLine([120, 92], [114, 150], 4)); ops.push(paintLine([114, 150], [106, 166], 4)); ops.push(paintLine([106, 166], [124, 174], 3)); ops.push(paintLine([136, 172], [150, 166], 3));+ ops.push(paintLine([98, 192], [128, 187], 4)); ops.push(paintLine([128, 187], [158, 192], 4));+ ops.push(paintLine([112, 202], [144, 202], 2, 170));+ if (kind === 'elder' || kind === 'christ' || kind === 'king') {+ const bc = kind === 'elder' ? [168, 164, 138] : [76, 46, 20];+ ops.push(paintPoly([[40, 158], [66, 184], [94, 196], [128, 206], [162, 196], [190, 184], [216, 158], [206, 226], [160, 256], [96, 256], [50, 226]], 255, bc));+ for (let i = 0; i < 7; i++) { const x = 64 + i * 21; ops.push(paintLine([x, 192 + Math.abs(i - 3) * -3], [x + (i - 3) * 3, 252], 3, 210)); }+ ops.push(paintLine([78, 180], [120, 176], 4)); ops.push(paintLine([178, 180], [136, 176], 4));+ } else {+ ops.push(paintLine([100, 226], [128, 236], 3, 170)); ops.push(paintLine([128, 236], [156, 226], 3, 170));+ }+ return ops.slice(0, 64);+}+function ornOps(f, kind, seed) { // painted ornament for borders: a running stem with leaves, scratched out of a dark ground+ const ops = [], C = GL.small;+ ops.push(paintPoly([[0, 0], [C, 0], [C, C], [0, C]], 210));+ const lit = f[2];+ for (let i = 0; i < 5; i++) {+ const y = 13 + i * 26, s = i % 2 ? 1 : -1;+ ops.push(paintPoly([[C / 2, y - 11], [C / 2 + s * 36, y - 6], [C / 2 + s * 46, y + 7], [C / 2 + s * 23, y + 11], [C / 2, y + 13]], 255, lit));+ ops.push(paintLine([C / 2 + s * 4, y - 3], [C / 2 + s * 37, y + 4], 2));+ ops.push(paintLine([C / 2 + s * 4, y + 5], [C / 2 + s * 28, y + 6], 1));+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [C / 2, 0], to: [C / 2, C], width: 7 }, rgba: [lit[0], lit[1], lit[2], 255] });+ return ops;+}+async function makeGlassMaterials() {+ const regions = [], C = GL.cell, Sm = GL.small;+ const addR = (name, x, y, w, h, bg, ops) => regions.push({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'emissive', background: [bg[0], bg[1], bg[2], 255], operations: ops } } });+ // Layout: two rows of small cells (pot-metals, then ornament), then drapery, faces and mosaic grounds+ GLASS_FAMILIES.forEach((f, i) => { const x = (i % 12) * Sm, y = Math.floor(i / 12) * Sm; GL.fam[f[0]] = [x, y]; addR('fam_' + f[0], x, y, Sm, Sm, f[1], familyOps(f, i + 1)); });+ [['gold', 'gold'], ['white', 'white']].forEach((q, i) => { const f = GLASS_FAMILIES.find((v) => v[0] === q[0]), x = (i + 2) * Sm, y = Sm; GL.orn[q[1]] = [x, y]; addR('orn_' + q[1], x, y, Sm, Sm, f[1], ornOps(f, q[1], i + 90)); });+ // painted drapery, one tall tile per colour+ ['ruby', 'cobalt', 'emerald', 'gold', 'white', 'purple'].forEach((n, i) => { const f = GLASS_FAMILIES.find((q) => q[0] === n), x = i * C, y = C; GL.tile[n] = [x, y, C, 2 * C]; addR('drape_' + n, x, y, C, 2 * C, f[1], drapeOps(f, i + 40)); });+ // faces+ ['youth', 'elder', 'christ', 'king', 'virgin', 'angel'].forEach((n, i) => { const x = i * C, y = 3 * C; GL.face[n] = [x, y]; addR('face_' + n, x, y, C, C, n === 'virgin' ? [202, 162, 128] : [192, 148, 112], faceOps(n)); });+ // mosaic grounds+ GLASS_GROUNDS.forEach((q, i) => { const fam = (n) => GLASS_FAMILIES.find((v) => v[0] === n), x = i * C, y = 4 * C; GL.ground[q[0]] = [x, y]; addR('ground_' + q[0], x, y, C, C, fam(q[1])[3], groundOps(fam(q[1]), fam(q[2]), fam(q[3]), i + 3)); });+ GL.ground.out = [4 * C, 4 * C]; addR('ground_out', 4 * C, 4 * C, C, C, [10, 12, 18], outerGlassOps());+ GL.fam.lead = [1400, 140]; // the empty part of the sheet is black, and the came needs no pixels of its own+ const atlas = await textureAtlas({ schemaVersion: 1, usage: 'emissive', width: GL.w, height: GL.h, pack: { edge: 2048, gutter: 0 }, regions });+ // Seen from inside the glass is the only light: a black pane that shines+ MAT.glassIn = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow, roughness: 1, metalness: 0, name: 'StainedGlass_Interior' });+ MAT.glassIn.color.setRGB(0, 0, 0);+ // Seen from outside by day it is nearly black, a dull sheen and the colour only hinting through.+ // A rough surface keeps grazing angles from washing out to a pale reflection of the sky.+ MAT.glassOut = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.6, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior' });+ MAT.glassOut.color.setRGB(0.008, 0.009, 0.012);+ // Every other window from outside: the outer mosaic only, near black, with the came just catching the eye+ MAT.glassOutWin = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.62, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior_Windows' });+ MAT.glassOutWin.color.setRGB(0.008, 0.009, 0.012);+ // The great rose burns a little brighter than any other window in the building+ MAT.glassRose = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow * 1.22, roughness: 1, metalness: 0, name: 'StainedGlass_Rose' });+ MAT.glassRose.color.setRGB(0, 0, 0);+}++// Hierarchy helper: every mesh gets a unique role name; geometry is shared between placements.+let ROOT = null; const NAMES = new Map();+function uniq(n) { const c = (NAMES.get(n) || 0) + 1; NAMES.set(n, c); return c === 1 ? n : n + '_' + c; }+const STAT = new Map();+// Exporter costs measured on this host: pos+normal+uv 32 B, COLOR_0 written as 12 B floats+function batchBytes(b) { b.weld(); return b.count * (32 + (b.noColor ? 0 : 12)) + b.I.length * (b.count > 65535 ? 4 : 2); }+function place(name, batch, mat, o = {}) {+ if (!batch || !batch.I.length) return null;+ if (mat === MAT.gold) batch.noColor = true; // metal needs no baked shading: saves 12 B a vertex+ if (P.preview === 'unique' && batch.geo) return null; // size test: every geometry once, no instances+ if (mat === MAT.ashlar && !batch.tessellated && !batch.geo) { batch.tess(P.wallTessellation); batch.tessellated = true; }+ if (P.preview === 'stats') {+ const e = STAT.get(batch) || { name, n: 0, normalMapped: mat === MAT.ashlar || mat === MAT.lead || mat === MAT.pave || mat === MAT.oak };+ e.n++; STAT.set(batch, e);+ return null;+ }+ const part = createPart(uniq(name), batch.toGeo(), mat, { position: o.pos || [0, 0, 0], rotation: o.rot || [0, 0, 0], scale: o.scale || [1, 1, 1], parent: o.parent || ROOT });+ PLACED.push({ part, batch, mat });+ return part;+}+const PLACED = [];+function group(name, parent) { const g = new THREE.Group(); g.name = uniq(name); (parent || ROOT).add(g); return g; }++// ==================================================================================+// SCULPTURE: robed figures with carved drapery. A figure is a full surface, not a block:+// S-curve stance, tube folds breaking at the hem, swags across the body, sleeves, hands,+// modelled faces, hair and beards, and the attributes each saint carries.+// ==================================================================================+// Every figure is a signed-distance volume: a long column of cloth with deep pleats, a mantle+// laid over it, arms under the cloth, and a separately resolved head and hands. A surface-net+// pass extracts the skin, the simplifier keeps the carving and drops the rest, and ambient+// occlusion from the field itself is baked into the valleys of the folds.+const len3 = (x, y, z) => Math.sqrt(x * x + y * y + z * z);+const smin = (a, b, k) => { const h = Math.max(k - Math.abs(a - b), 0) / k; return Math.min(a, b) - h * h * k * 0.25; };+const smax = (a, b, k) => -smin(-a, -b, k);+const sdEll = (x, y, z, rx, ry, rz) => { const a = x / rx, b = y / ry, c = z / rz, k0 = Math.sqrt(a * a + b * b + c * c), d = a / rx, e = b / ry, f = c / rz, k1 = Math.sqrt(d * d + e * e + f * f); return k1 > 1e-9 ? k0 * (k0 - 1) / k1 : -Math.min(rx, ry, rz); };+// Rounded cone between two points: the workhorse of fingers, locks, lids and sleeves+function sdCone(x, y, z, ax, ay, az, bx, by, bz, r1, r2) {+ const px = x - ax, py = y - ay, pz = z - az, dx = bx - ax, dy = by - ay, dz = bz - az;+ const t = clamp((px * dx + py * dy + pz * dz) / (dx * dx + dy * dy + dz * dz));+ return len3(px - dx * t, py - dy * t, pz - dz * t) - (r1 + (r2 - r1) * t);+}+// Surface nets over a sparse grid: only cells near the surface are ever evaluated+function polygonize(F, box, cell, bandK = 1.75) {+ const [x0, y0, z0, x1, y1, z1] = box, nx = Math.ceil((x1 - x0) / cell) + 2, ny = Math.ceil((y1 - y0) / cell) + 2, nz = Math.ceil((z1 - z0) / cell) + 2;+ const N = nx * ny * nz, val = new Float32Array(N), seen = new Uint8Array(N), cellV = new Int32Array(N).fill(-1);+ const b4 = 3.46 * cell * bandK, b2 = 1.73 * cell * bandK, active = [], inA = new Uint8Array(N);+ const activate = (i, j, k) => { const q = i + nx * (j + ny * k); if (!inA[q]) { inA[q] = 1; active.push(i, j, k); } };+ const at = (i, j, k) => { const q = i + nx * (j + ny * k); if (!seen[q]) { seen[q] = 1; val[q] = F(x0 + i * cell, y0 + j * cell, z0 + k * cell); } return val[q]; };+ // Two-level descent: a coarse block, then its octants, then cells, so the field is only asked near its surface+ for (let kc = 0; kc < nz - 1; kc += 4) for (let jc = 0; jc < ny - 1; jc += 4) for (let ic = 0; ic < nx - 1; ic += 4) {+ if (Math.abs(F(x0 + (ic + 2) * cell, y0 + (jc + 2) * cell, z0 + (kc + 2) * cell)) >= b4) continue;+ for (let k2 = kc; k2 < Math.min(kc + 4, nz - 1); k2 += 2) for (let j2 = jc; j2 < Math.min(jc + 4, ny - 1); j2 += 2) for (let i2 = ic; i2 < Math.min(ic + 4, nx - 1); i2 += 2) {+ if (Math.abs(F(x0 + (i2 + 1) * cell, y0 + (j2 + 1) * cell, z0 + (k2 + 1) * cell)) >= b2) continue;+ for (let k = k2; k < Math.min(k2 + 2, nz - 1); k++) for (let j = j2; j < Math.min(j2 + 2, ny - 1); j++) for (let i = i2; i < Math.min(i2 + 2, nx - 1); i++) activate(i, j, k);+ }+ }+ const P = [], I = [], E = [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]], v = new Float64Array(8);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ let mask = 0;+ for (let c = 0; c < 8; c++) { const d = at(i + (c & 1), j + ((c >> 1) & 1), k + ((c >> 2) & 1)); v[c] = d; if (d < 0) mask |= 1 << c; }+ if (mask === 0 || mask === 255) continue;+ // Follow the surface through every face it crosses: a steep pleat can fool the coarse test above, and a skipped cell is a hole+ const crosses = (m) => (mask & m) !== 0 && (mask & m) !== m;+ if (i > 0 && crosses(0x55)) activate(i - 1, j, k);+ if (i < nx - 2 && crosses(0xaa)) activate(i + 1, j, k);+ if (j > 0 && crosses(0x33)) activate(i, j - 1, k);+ if (j < ny - 2 && crosses(0xcc)) activate(i, j + 1, k);+ if (k > 0 && crosses(0x0f)) activate(i, j, k - 1);+ if (k < nz - 2 && crosses(0xf0)) activate(i, j, k + 1);+ let sx = 0, sy = 0, sz = 0, n = 0;+ for (let e = 0; e < 12; e++) {+ const c0 = E[e][0], c1 = E[e][1], d0 = v[c0], d1 = v[c1];+ if ((d0 < 0) === (d1 < 0)) continue;+ const t = d0 / (d0 - d1);+ sx += lerp(c0 & 1, c1 & 1, t); sy += lerp((c0 >> 1) & 1, (c1 >> 1) & 1, t); sz += lerp((c0 >> 2) & 1, (c1 >> 2) & 1, t); n++;+ }+ cellV[i + nx * (j + ny * k)] = P.length / 3;+ P.push(x0 + (i + sx / n) * cell, y0 + (j + sy / n) * cell, z0 + (k + sz / n) * cell);+ }+ const quad = (a, b, c, d, flip) => { if (a < 0 || b < 0 || c < 0 || d < 0) return; if (flip) I.push(a, c, b, a, d, c); else I.push(a, b, c, a, c, d); };+ const cv = (i, j, k) => (i < 0 || j < 0 || k < 0 ? -1 : cellV[i + nx * (j + ny * k)]);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ const s0 = at(i, j, k) < 0;+ if (s0 !== (at(i + 1, j, k) < 0)) quad(cv(i, j - 1, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i, j - 1, k), !s0);+ if (s0 !== (at(i, j + 1, k) < 0)) quad(cv(i - 1, j, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i - 1, j, k), s0);+ if (s0 !== (at(i, j, k + 1) < 0)) quad(cv(i - 1, j - 1, k), cv(i, j - 1, k), cv(i, j, k), cv(i - 1, j, k), !s0);+ }+ return { P, I };+}+function meshToGeo(m) {+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(m.P), 3));+ g.setIndex(new THREE.BufferAttribute(m.P.length / 3 > 65535 ? new Uint32Array(m.I) : new Uint16Array(m.I), 1));+ g.computeVertexNormals();+ return g;+}+const AO_STEPS = [0.016, 0.05, 0.13];+// Extract, simplify, and write into a batch with field normals and field ambient occlusion+async function carveInto(b, F, box, cell, tris, xf, occ, opt = {}) {+ const raw = polygonize(F, box, cell);+ if (!raw.I.length) return;+ if (opt.cull) { // drop a region that a finer pass will carve instead+ const I2 = [], used = new Int32Array(raw.P.length / 3).fill(-1), P2 = [], mapV = (v) => { if (used[v] < 0) { used[v] = P2.length / 3; P2.push(raw.P[v * 3], raw.P[v * 3 + 1], raw.P[v * 3 + 2]); } return used[v]; };+ for (let t = 0; t < raw.I.length; t += 3) {+ const a = raw.I[t], c = raw.I[t + 1], e = raw.I[t + 2], mx = (raw.P[a * 3] + raw.P[c * 3] + raw.P[e * 3]) / 3, my = (raw.P[a * 3 + 1] + raw.P[c * 3 + 1] + raw.P[e * 3 + 1]) / 3, mz = (raw.P[a * 3 + 2] + raw.P[c * 3 + 2] + raw.P[e * 3 + 2]) / 3;+ if (!opt.cull(mx, my, mz)) I2.push(mapV(a), mapV(c), mapV(e));+ }+ raw.P = P2; raw.I = I2;+ }+ let g = meshToGeo(raw);+ if (raw.I.length / 3 > tris) g = await simplifyGeometry(g, { targetTriangles: tris });+ const pos = g.attributes.position, idx = g.index, base = b.count, h = cell * (opt.smooth || 0.6), aoK = opt.ao === undefined ? 0.5 : opt.ao, aoMin = opt.aoMin === undefined ? 0.16 : opt.aoMin;+ b.at(xf || M.I(), () => {+ for (let i = 0; i < pos.count; i++) {+ let x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i);+ const f0 = F(x, y, z), g0 = F(x + h, y, z) - F(x - h, y, z), g1 = F(x, y + h, z) - F(x, y - h, z), g2 = F(x, y, z + h) - F(x, y, z - h), l = len3(g0, g1, g2) || 1;+ const nx = g0 / l, ny = g1 / l, nz = g2 / l;+ // settle the vertex onto the true surface: this is what keeps fold valleys crisp+ x -= nx * f0; y -= ny * f0; z -= nz * f0;+ const O = occ || F;+ const wx = b.m[0] * x + b.m[1] * y + b.m[2] * z + b.m[3], wy = b.m[4] * x + b.m[5] * y + b.m[6] * z + b.m[7], wz = b.m[8] * x + b.m[9] * y + b.m[10] * z + b.m[11];+ const nm = b.nm, ax = nm[0] * nx + nm[1] * ny + nm[2] * nz, ay = nm[3] * nx + nm[4] * ny + nm[5] * nz, az = nm[6] * nx + nm[7] * ny + nm[8] * nz, al = len3(ax, ay, az) || 1;+ let o = 0, w = 1;+ for (let q = 0; q < 3; q++) { const d = AO_STEPS[q]; o += w * Math.max(0, d - O(wx + ax / al * d, wy + ay / al * d, wz + az / al * d)) / d; w *= 0.66; }+ const ao = clamp(1 - o * aoK, aoMin, 1);+ // UV by a slanted planar projection: no seam on a figure, the grain runs up the stone+ b.v(x, y, z, nx, ny, nz, wz * 0.8 + wx * 0.6, wy, ao);+ }+ for (let t = 0; t < idx.count; t += 3) b.tri(base + idx.getX(t), base + idx.getX(t + 1), base + idx.getX(t + 2));+ });+}++// ---- The head: a real skull, brow, eyes with lids, nose, lips, chin, hair, beard -------------+const LID_U = [[0.0795, 0.0035, 0.0165, 0.003], [0.0848, 0.0112, 0.0238, 0.0038], [0.0866, 0.0152, 0.0338, 0.0043], [0.0838, 0.0122, 0.0438, 0.0036], [0.0755, 0.0062, 0.052, 0.0024]];+const LID_L = [[0.0795, 0.0035, 0.0165, 0.0022], [0.0836, -0.0012, 0.0245, 0.0028], [0.0852, -0.0032, 0.034, 0.003], [0.0822, -0.0006, 0.044, 0.0026], [0.0755, 0.0062, 0.052, 0.0022]];+function headField(o) {+ const sm = o.smile || 0, cheek = o.gaunt ? 0.5 : 1, up = sm * 0.0065;+ const locks = [], rng = mkRng(o.seed * 37 + 11);+ // Long locks fall from the temples and the back of the head to the shoulders+ if (o.hair === 'long' || o.hair === 'curled') {+ const nL = o.hair === 'curled' ? 7 : 9;+ for (let i = 0; i < nL; i++) {+ const f = i / (nL - 1), a = lerp(1.05, 2.95, f), r0 = 0.092, wob = 0.012 + rng() * 0.008, dr = o.hair === 'curled' ? -0.165 : -0.235;+ for (const s of [-1, 1]) {+ const p0 = [r0 * Math.cos(a) - 0.012, 0.02 + rng() * 0.02, s * r0 * Math.sin(a) * 0.9];+ const p1 = [p0[0] * 1.14 - 0.008, p0[1] + dr * 0.5, p0[2] * 1.18 + s * wob];+ const p2 = [p0[0] * 1.02 - 0.03 * (1 - f * 0.5), p0[1] + dr, p0[2] * (1.28 - f * 0.3) - s * wob];+ locks.push([p0, p1, 0.02, 0.016], [p1, p2, 0.016, o.hair === 'curled' ? 0.012 : 0.007]);+ }+ }+ }+ if (o.hair === 'curls') { // rolled waves that frame the face: the smiling angel, the Child+ for (let i = 0; i < 11; i++) { const f = i / 10 - 0.5, a = f * 2.5, p0 = [0.104 * Math.cos(a) * 0.72 + 0.006, 0.094 - f * f * 0.11, 0.088 * Math.sin(a)], p1 = [p0[0] + 0.012, p0[1] - 0.03 - Math.abs(f) * 0.03, p0[2] * 1.16]; locks.push([p0, p1, 0.0165, 0.0115]); }+ for (let i = 0; i < 12; i++) { const a = 1.5 + i * 0.29, s = i % 2 ? 1 : -1, p0 = [0.09 * Math.cos(a) - 0.012, 0.0, s * 0.09 * Math.sin(a)], p1 = [p0[0] * 1.1, -0.062, p0[2] * 1.14]; locks.push([p0, p1, 0.019, 0.013]); }+ }+ const beardL = [];+ if (o.beard) {+ const nB = 7, L = o.beard === 2 ? 0.17 : 0.09;+ for (let i = 0; i < nB; i++) {+ const f = i / (nB - 1) - 0.5, z0 = f * 0.098, x0 = 0.078 - Math.abs(f) * 0.05, wob = (i % 2 ? 1 : -1) * 0.005;+ // separate locks with grooves between them; each one twists and tapers to a point+ beardL.push([[x0, -0.07 - Math.abs(f) * 0.012, z0], [x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], 0.0102, 0.0098]);+ beardL.push([[x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], [x0 - 0.006 + (o.beard === 2 ? 0.008 : 0), -0.07 - L - Math.abs(f) * 0.012, z0 * 0.5], 0.0098, 0.0035]);+ }+ }+ // Each lock is skipped outright when the query point is beyond its bounding sphere+ const sphere = (L) => { const dx = L[0][0] - L[1][0], dy = L[0][1] - L[1][1], dz = L[0][2] - L[1][2], R = Math.sqrt(dx * dx + dy * dy + dz * dz) / 2 + Math.max(L[2], L[3]) + 0.03; return [(L[0][0] + L[1][0]) / 2, (L[0][1] + L[1][1]) / 2, (L[0][2] + L[1][2]) / 2, R * R]; };+ const lockB = locks.map(sphere), beardB = beardL.map(sphere);+ const nearS = (S, x, y, z) => { const a = x - S[0], b = y - S[1], c = z - S[2]; return a * a + b * b + c * c < S[3]; };+ return (x, y, z) => {+ const az = Math.abs(z);+ let d = sdEll(x + 0.012, y - 0.03, z, 0.098, 0.098, 0.08); // cranium+ d = smin(d, sdEll(x - 0.018, y + 0.022, z, 0.074, 0.094, 0.0655), 0.02); // face mass+ d = smin(d, sdEll(x - 0.03, y + 0.076, z, 0.056, 0.046, 0.05 * (o.gaunt ? 0.93 : 1)), 0.018); // jaw+ d = smin(d, sdEll(x - 0.0675, y + 0.1, z, 0.019, 0.0175, 0.02), 0.012); // chin+ d = smin(d, sdCone(x, y, az, 0.0745, 0.031, 0, 0.066, 0.033, 0.05, 0.0118, 0.0088), 0.012); // brow ridge+ d = smin(d, sdEll(x - 0.054, y + 0.018 - up, az - 0.044, 0.026, 0.017 + sm * 0.003, 0.02), 0.015 * cheek); // cheekbone+ if (o.gaunt) d = smax(d, -sdEll(x - 0.05, y + 0.058, az - 0.058, 0.03, 0.022, 0.012), 0.012);+ if (x > 0.04 && y > -0.03 && y < 0.05 && az < 0.082) {+ d = smax(d, -sdEll(x - 0.0875, y - 0.0075, az - 0.034, 0.0225, 0.0118, 0.0215), 0.008); // eye socket+ d = smin(d, len3(x - 0.0655, y - 0.0065, az - 0.034) - 0.0178, 0.0018); // eyeball+ d = smax(d, -(len3(x - 0.0862, y - 0.0065, az - 0.034) - 0.0052), 0.0016); // the drilled pupil+ // lids: a carved almond, the upper rim heavier, meeting at the corners+ for (let i = 0; i < 4; i++) { const a = LID_U[i], c = LID_U[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0026); }+ for (let i = 0; i < 4; i++) { const a = LID_L[i], c = LID_L[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0022); }+ }+ if (x > 0.055 && y > -0.105 && y < 0.045 && az < 0.055) {+ // nose: bridge to tip, wings, nostrils+ d = smin(d, sdCone(x, y, z, 0.0835, 0.0225, 0, 0.105, -0.021, 0, 0.0058, 0.0092), 0.0085);+ d = smin(d, len3(x - 0.0925, y + 0.0295, az - 0.0108) - 0.0064, 0.005);+ d = smax(d, -(len3(x - 0.0975, y + 0.0355, az - 0.0062) - 0.0033), 0.002);+ // philtrum+ d = smax(d, -sdCone(x, y, z, 0.0975, -0.042, 0, 0.0965, -0.052, 0, 0.0028, 0.0034), 0.0035);+ // lips: upper in two wings, a fuller lower, a cut between; corners lift for a smile+ const my = -0.0615;+ d = smin(d, sdCone(x, y, az, 0.0925, my + 0.004, 0, 0.0815, my + 0.001 + up, 0.0215 + sm * 0.003, 0.0058, 0.0026), 0.005);+ d = smin(d, sdCone(x, y, az, 0.0915, my - 0.0085, 0, 0.0825, my - 0.003 + up, 0.0185 + sm * 0.003, 0.0072, 0.0028), 0.005);+ d = smax(d, -sdCone(x, y, az, 0.0965, my - 0.002, 0, 0.0825, my + up * 1.15, 0.0235 + sm * 0.004, 0.0017, 0.0021), 0.0022);+ d = smax(d, -sdEll(x - 0.0915, y + 0.0845, z, 0.012, 0.0055, 0.02), 0.006); // hollow under the lip+ }+ if (sm) d = smin(d, sdEll(x - 0.07, y + 0.044 - up * 2, az - 0.038, 0.02, 0.015, 0.016), 0.012); // the raised cheek of a smile+ // ear+ d = smin(d, sdEll(x + 0.012, y - 0.002, az - 0.08, 0.016, 0.03, 0.0085), 0.004);+ // neck, into the shoulders+ d = smin(d, sdCone(x, y, z, -0.01, -0.075, 0, -0.022, -0.215, 0, 0.041, 0.052), 0.018);+ // ---- hair ----+ if (o.hair && o.hair !== 'none') {+ const faceOpen = sdEll(x - 0.094, y + 0.012, z, 0.08, o.hair === 'curls' ? 0.106 : 0.118, 0.073);+ let h = sdEll(x + 0.014, y - 0.034, z, 0.104, 0.104, 0.09);+ if (o.hair === 'tonsure') h = smax(h, -sdEll(x + 0.012, y - 0.135, z, 0.075, 0.045, 0.062), 0.006);+ const an = Math.atan2(z, x + 0.014);+ if (o.hair !== 'curls') {+ const part = Math.exp(-(z * z) / 0.00007) * sstep(0.03, 0.11, y);+ h += 0.0028 * Math.sin(an * 22 + y * 34) + 0.007 * part;+ } else {+ h += 0.006 * Math.sin(an * 11 + y * 22) * Math.sin(y * 27 + an * 3);+ }+ for (let i = 0; i < locks.length; i++) { if (!nearS(lockB[i], x, y, z)) continue; const L = locks[i]; h = smin(h, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0055); }+ h = smax(h, -faceOpen, 0.0055);+ d = smin(d, h, 0.0025);+ }+ // ---- beard and moustache ----+ if (o.beard) {+ for (let i = 0; i < beardL.length; i++) { if (!nearS(beardB[i], x, y, z)) continue; const L = beardL[i]; d = smin(d, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0022); }+ d = smin(d, sdCone(x, y, az, 0.0955, -0.0475, 0.002, 0.081, -0.0625, 0.031, 0.0046, 0.003), 0.0025);+ d = smin(d, sdEll(x - 0.05, y + 0.058, az - 0.058, 0.022, 0.03, 0.012), 0.008); // sideburn into the jaw+ }+ // ---- veil: falls from the crown to the shoulders, cut open round the face ----+ if (o.veil) {+ // A soft hood: close to the skull, widening over the shoulders, its hem falling in loose folds+ const an = Math.atan2(z, x + 0.02);+ let w = smin(sdEll(x + 0.012, y - 0.032, z, 0.11, 0.108, 0.094), sdCone(x, y, z, -0.022, -0.05, 0, -0.038, -0.3, 0, 0.09, 0.158), 0.05);+ w -= 0.012 * (Math.sqrt(Math.abs(Math.sin(an * 4.5 + y * 2.2))) - 0.55) * sstep(-0.01, -0.16, y);+ w = smax(w, -0.325 + 0.022 * Math.sin(an * 5) - y, 0.008);+ const open = sdEll(x - 0.104, y + 0.016, z, 0.1, 0.116, 0.0715);+ d = smin(d, smax(w, -open, 0.004), 0.0015);+ }+ // ---- a queen's barbette: a linen band from the crown under the chin, framing a long grave face ----+ if (o.wimple) {+ const e = Math.sqrt(((y + 0.012) * (y + 0.012)) / (0.112 * 0.112) + (z * z) / (0.08 * 0.08)) - 1;+ d = smin(d, Math.max(Math.abs(e) * 0.09 - 0.0075, Math.abs(x - 0.018) - 0.036), 0.002);+ }+ // ---- mitre: two high peaks of stiff cloth, the bands left proud ----+ if (o.mitre) {+ // seen from the front a pointed arch, wide at the brow; seen from the side, two thin peaks+ const px = x + 0.012, py = y - 0.058, f = clamp(py / 0.27), w0 = 0.098 * (1 - Math.pow(f, 1.9)), dp = 0.102 * (1 - f * 0.86), lens = Math.abs(px) + az * 0.2;+ let m = Math.max(lens - dp, az - w0, -py);+ m = Math.max(m, py - 0.272);+ const band = Math.exp(-((py - 0.018) * (py - 0.018)) / 0.00016);+ m -= 0.0045 * band + 0.0038 * Math.exp(-(z * z) / 0.00012);+ d = smin(d, m, 0.004);+ }+ return d * 0.92;+ };+}++// ---- The hand: palm and jointed fingers that actually close on what they hold -------------+const HAND_POSES = {+ // [curl1, curl2, curl3] per finger (index..little), thumb [x,y,z of tip, spread]+ grip: { f: [[68, 88, 62], [72, 92, 64], [76, 92, 64], [80, 88, 62]], t: 'over' },+ support: { f: [[10, 18, 12], [12, 20, 14], [14, 22, 14], [18, 24, 16]], t: 'open' },+ edge: { f: [[46, 40, 26], [48, 42, 26], [50, 44, 28], [54, 44, 28]], t: 'open' },+ bless: { f: [[6, 8, 6], [8, 10, 8], [62, 90, 60], [68, 92, 60]], t: 'over' },+ rest: { f: [[28, 40, 26], [32, 42, 28], [36, 44, 28], [40, 46, 28]], t: 'open' },+};+function handField(pose, thumb = 1) {+ const P0 = HAND_POSES[pose], kn = [[0.078, 0.003, -0.027], [0.083, 0.004, -0.009], [0.08, 0.003, 0.009], [0.073, 0.001, 0.026]], segL = [[0.036, 0.024, 0.019], [0.04, 0.027, 0.02], [0.037, 0.025, 0.019], [0.029, 0.019, 0.016]], segs = [];+ for (let f = 0; f < 4; f++) {+ let ang = 0, p = kn[f].slice();+ for (let s = 0; s < 3; s++) {+ ang += rad(P0.f[f][s]);+ const q = [p[0] + Math.cos(ang) * segL[f][s], p[1] - Math.sin(ang) * segL[f][s], p[2]];+ segs.push([p, q, s === 0 ? 0.0098 : s === 1 ? 0.0088 : 0.0078, s === 2 ? 0.0066 : s === 0 ? 0.0088 : 0.0078]);+ p = q;+ }+ }+ const tb = [0.022, -0.006, thumb * 0.03], tm = P0.t === 'over' ? [0.052, -0.03, thumb * 0.033] : [0.05, -0.012, thumb * 0.054], tt = P0.t === 'over' ? [0.078, -0.044, thumb * 0.012] : [0.082, -0.016, thumb * 0.056];+ return (x, y, z) => {+ let d = Math.max(Math.abs(x - 0.04) - 0.043, Math.abs(y) - 0.0125, Math.abs(z) - 0.036) - 0.006; // palm, gently rounded+ d = smin(d, sdCone(x, y, z, -0.02, 0, 0, 0.02, 0, 0, 0.026, 0.033), 0.012); // wrist heel+ for (let i = 0; i < segs.length; i++) { const S = segs[i]; d = smin(d, sdCone(x, y, z, S[0][0], S[0][1], S[0][2], S[1][0], S[1][1], S[1][2], S[2], S[3]), 0.0022); }+ d = smin(d, sdCone(x, y, z, tb[0], tb[1], tb[2], tm[0], tm[1], tm[2], 0.0145, 0.0098), 0.006);+ d = smin(d, sdCone(x, y, z, tm[0], tm[1], tm[2], tt[0], tt[1], tt[2], 0.0098, 0.0078), 0.002);+ return d;+ };+}+// Heads are shared meshes placed on their own node: the same face can turn and tilt per figure at no byte cost.+const HEAD_MESH = {};+const HEADS = {+ bearded: { hair: 'long', beard: 1, seed: 2 },+ mitred: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 },+ royal: { hair: 'curled', beard: 2, seed: 10 },+ veiled: { veil: 1, smile: 0.3, seed: 8 },+ queen: { veil: 1, gaunt: 1, wimple: 1, seed: 5 },+ angelic: { hair: 'curls', smile: 1, seed: 13 },+};+async function headBatch(key) {+ if (HEAD_MESH[key]) return HEAD_MESH[key];+ const hO = HEADS[key], b = new Batch(TILE.fine), hF = headField(hO);+ const hB = [-0.19, hO.veil ? -0.36 : -0.26, -0.2, 0.17, hO.mitre ? 0.36 : 0.16, 0.2];+ // Hair and skull at a working grid, then the face alone on the finest grid with most of the triangles+ const FB = [0.024, -0.13, -0.08, 0.128, 0.088, 0.08];+ const inFace = (x, y, z) => x > FB[0] + 0.003 && y > FB[1] + 0.003 && y < FB[4] - 0.003 && z > FB[2] + 0.003 && z < FB[5] - 0.003;+ await carveInto(b, hF, hB, 0.0048, hO.veil ? 1300 : hO.beard ? 1900 : 1700, null, hF, { smooth: 1.4, ao: 0.34, aoMin: 0.3, cull: inFace });+ await carveInto(b, hF, FB, 0.0024, hO.beard ? 2500 : 2700, null, hF, { smooth: 0.9, ao: 0.3, aoMin: 0.34 });+ b.isFigure = true;+ HEAD_MESH[key] = b;+ return b;+}+const HAND_MESH = {};+async function handBatch(pose, thumb) { // hands are cached: a grip is a grip, whoever's it is+ const key = pose + thumb;+ if (!HAND_MESH[key]) { const b = new Batch(TILE.fine), F = handField(pose, thumb); await carveInto(b, F, [-0.05, -0.1, -0.1, 0.16, 0.05, 0.1], 0.0034, 520); HAND_MESH[key] = b; }+ return HAND_MESH[key];+}+function copyBatch(dst, src, m, shadeFn) { // transform a finished batch into another+ const base = dst.count;+ dst.at(m, () => {+ for (let i = 0; i < src.count; i++) { const i3 = i * 3, i2 = i * 2; dst.v(src.P[i3], src.P[i3 + 1], src.P[i3 + 2], src.N[i3], src.N[i3 + 1], src.N[i3 + 2], src.UV[i2] * src.tile, src.UV[i2 + 1] * src.tile, (src.C[i3] / 255) * (shadeFn ? shadeFn(src.P[i3], src.P[i3 + 1], src.P[i3 + 2]) : 1)); }+ for (let t = 0; t < src.I.length; t += 3) dst.tri(base + src.I[t], base + src.I[t + 1], base + src.I[t + 2]);+ });+}+const frameOf = (p, X, Y) => { // rigid frame: origin p, local X and Y given, Z = X x Y+ const lx = len3(X[0], X[1], X[2]), xn = [X[0] / lx, X[1] / lx, X[2] / lx], d = Y[0] * xn[0] + Y[1] * xn[1] + Y[2] * xn[2];+ let yy = [Y[0] - d * xn[0], Y[1] - d * xn[1], Y[2] - d * xn[2]]; const ly = len3(yy[0], yy[1], yy[2]); yy = yy.map((c) => c / ly);+ const z = [xn[1] * yy[2] - xn[2] * yy[1], xn[2] * yy[0] - xn[0] * yy[2], xn[0] * yy[1] - xn[1] * yy[0]];+ return [xn[0], yy[0], z[0], p[0], xn[1], yy[1], z[1], p[1], xn[2], yy[2], z[2], p[2]];+};+const invRigid = (m) => (x, y, z) => { const a = x - m[3], b = y - m[7], c = z - m[11]; return [m[0] * a + m[4] * b + m[8] * c, m[1] * a + m[5] * b + m[9] * c, m[2] * a + m[6] * b + m[10] * c]; };++// ---- The people. Bodies are long columns of cloth; each differs in dress, stance, head, gesture ----+const FIGURES = {+ apostle: { seed: 2, sway: 0.018, dress: 'mantle', headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 2 }, hold: 'key' },+ bishop: { seed: 4, sway: 0.008, dress: 'chasuble', headKey: 'mitred', head: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 }, hold: 'crozier' },+ king: { seed: 10, sway: 0.022, dress: 'royal', headKey: 'royal', head: { hair: 'curled', beard: 2, crown: 1, seed: 10 }, hold: 'sceptre' },+ queen: { seed: 5, sway: 0.03, dress: 'royal', slim: 0.93, headKey: 'queen', head: { veil: 1, gaunt: 1, wimple: 1, crown: 1, seed: 5, turn: 8 }, hold: 'mantle' },+ angel: { seed: 13, sway: 0.05, dress: 'angel', slim: 0.94, wings: 1, headKey: 'angelic', head: { hair: 'curls', smile: 1, seed: 13, tilt: 11, turn: 15 }, hold: 'scroll' },+ virgin: { seed: 8, sway: 0.062, dress: 'mantle', slim: 0.95, headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, tilt: 9, turn: -16 }, hold: 'child' },+};+const FIG_ALIAS = { peter: 'apostle', paul: 'apostle', prophet: 'apostle', deacon: 'apostle', martyr: 'queen', kingA: 'king', kingB: 'king', kingC: 'king', christ: 'seated' };++function bodyField(o) {+ const yN = 1.7, rng = mkRng(o.seed * 991 + 7), ph = []; for (let i = 0; i < 8; i++) ph.push(rng() * TAU);+ const sw = o.sway || 0, sl = o.slim || 1, L = 256;+ const keys = [[0, 0.178, 0.205, 0.218], [0.05, 0.165, 0.192, 0.204], [0.3, 0.138, 0.165, 0.172], [0.52, 0.124, 0.15, 0.158], [0.66, 0.112, 0.128, 0.146], [0.8, 0.116, 0.12, 0.15], [0.885, 0.108, 0.112, 0.15], [0.94, 0.09, 0.094, 0.13], [0.975, 0.066, 0.07, 0.09], [1, 0.044, 0.046, 0.05]];+ const Af = new Float32Array(L), Ab = new Float32Array(L), Bs = new Float32Array(L), Cx = new Float32Array(L), Cz = new Float32Array(L), Amp = new Float32Array(L);+ for (let i = 0; i < L; i++) {+ const t = i / (L - 1), k = keyf(keys, t);+ Af[i] = k[0]; Ab[i] = k[1]; Bs[i] = k[2] * sl;+ Cx[i] = 0.014 * Math.sin(t * PI);+ Cz[i] = sw * (Math.sin(t * PI * 0.92) * 1.15 - 0.62 * Math.sin(t * PI * 2));+ // pleats deepest over the shins, gathered at the belt, slight in the bodice+ Amp[i] = t < 0.645 ? lerp(0.05, 0.03, sstep(0, 0.6, t)) : lerp(0.014, 0.004, sstep(0.66, 0.9, t));+ }+ const n1 = o.dress === 'chasuble' ? 16 : 14, n2 = 25, yB = 0.655 * yN;+ const dress = o.dress, zc = Cz[Math.round(0.86 * (L - 1))], cxS = Cx[Math.round(0.86 * (L - 1))];+ const Rs = 0.142 * sl;+ const arms = armPose(o, Rs, zc, cxS);+ // Feet: pointed medieval shoes emerge from the hem+ const foot = (x, y, z) => { const az = Math.abs(z - Cz[0]); let d = sdCone(x, y, az, 0.02, 0.042, 0.082, 0.2, 0.024, 0.098, 0.05, 0.024); d = smin(d, sdCone(x, y, az, 0.0, 0.05, 0.082, 0.02, 0.2, 0.07, 0.044, 0.04), 0.02); d = smax(d, -sdEll(x - 0.11, y + 0.01, az - 0.09, 0.08, 0.02, 0.1), 0.004); return Math.max(d, -y); };+ return {+ arms,+ F: (x, y, z) => {+ const t = clamp(y / yN), i = (t * (L - 1)) | 0, px = x - Cx[i], pz = z - Cz[i], rho = Math.sqrt(px * px + pz * pz) + 1e-6, c = px / rho, s = pz / rho, a = c > 0 ? Af[i] : Ab[i], b = Bs[i];+ const R = 1 / Math.sqrt((c * c) / (a * a) + (s * s) / (b * b)), th = Math.atan2(pz, px);+ // ---- the tunic: tube pleats with valleys you could put a finger in ----+ const u1 = th * n1 * 0.5 + ph[0] + y * 0.5 * Math.sign(sw || 1), u2 = th * n2 * 0.5 + ph[1] - y * 0.9;+ const p1 = Math.sqrt(Math.abs(Math.sin(u1))), p2 = Math.sqrt(Math.abs(Math.sin(u2)));+ let r = R + Amp[i] * (p1 * 0.76 + p2 * 0.24 - 0.64);+ r += sstep(0.14, 0, y) * (0.018 + 0.016 * Math.sin(th * 7 + ph[4])); // the skirt spreads and breaks at the foot+ r += 0.008 * Math.exp(-((y - yB) * (y - yB)) / 0.00018); // belt+ r -= 0.012 * sstep(0.03, 0, Math.abs(y - yB) - 0.012) * 0.2;+ r += 0.006 * Math.exp(-((y - yN + 0.075) * (y - yN + 0.075)) / 0.00012); // collar+ let d = rho - r;+ const hem = 0.055 + 0.038 * p1 * (c > -0.2 ? 1 : 0.4);+ d = smax(d, hem - y, 0.006);+ // ---- the over-garment ----+ if (dress === 'mantle') {+ const up = 0.78 * yN + 0.2 * yN * Math.max(0, Math.sin(-th * Math.sign(sw || 1) + 0.5)) + 0.25 * sstep(1.2, 2.6, Math.abs(th)), low = 0.33 * yN - 0.18 * yN * sstep(0.5, 2.8, Math.abs(th)) + 0.02 * Math.sin(th * 11 + ph[5]) + 0.012 * Math.sin(th * 23);+ const reg = Math.max(low - y, y - up);+ // Folds pulled on the diagonal from the held hip to the far shoulder: two scales, never in step+ const th0 = 0.25 * Math.sign(sw || 1), q = y + 0.15 * th * Math.sign(sw || 1) + 0.055 * (th - th0) * (th - th0);+ const swag = 0.62 * Math.sqrt(Math.abs(Math.sin(q * 12.5 + ph[6]))) + 0.38 * Math.sqrt(Math.abs(Math.sin(q * 27 + ph[7]))), back = Math.sqrt(Math.abs(Math.sin(th * 5 + ph[3])));+ const fr = sstep(1.9, 1.1, Math.abs(th));+ const rm = R + 0.03 + 0.04 * lerp(back - 0.6, swag - 0.58, fr) * sstep(up + 0.01, up - 0.08, y);+ d = smin(d, smax(rho - rm, reg, 0.007), 0.002);+ } else if (dress === 'chasuble') {+ // alb to the feet, dalmatic to the shin, the bell of the chasuble hanging from the arms in U folds+ const dal = Math.max(0.3 * yN + 0.012 * Math.sin(th * 9) - y, y - 0.86 * yN);+ d = smin(d, smax(rho - (R + 0.016 + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 6 + ph[2]))) - 0.5)), dal, 0.005), 0.0015);+ const lowC = (0.42 + 0.26 * sstep(0.2, 1.5, Math.abs(th)) - 0.2 * sstep(1.7, 3.0, Math.abs(th))) * yN + 0.012 * Math.sin(th * 13);+ const swag = 0.65 * Math.sqrt(Math.abs(Math.sin((y + 0.2 * th * th) * 27 + ph[6]))) + 0.35 * Math.sqrt(Math.abs(Math.sin((y + 0.13 * th * th) * 44 + ph[7])));+ const rc = R + 0.03 * sstep(yN, 0.9 * yN, y) + 0.022 * (swag - 0.58) * sstep(0.84 * yN, 0.74 * yN, y) * (1 - 0.6 * sstep(0, 0.2, 0.2 - Math.abs(th)));+ d = smin(d, smax(rho - rc, lowC - y, 0.007), 0.003);+ // the orphrey: a Y-band across the breast and down the front, left proud+ const py = y - 0.79 * yN;+ if (Math.abs(th) < 1.2 && y > lowC - 0.01 && y < 0.94 * yN) d = smin(d, Math.max(rho - rc - 0.011, py > 0 ? Math.abs(Math.abs(th) - py * 4.2) - 0.085 : Math.abs(th) - 0.09), 0.003);+ } else if (dress === 'royal') {+ // a heavy mantle open down the front, thrown back over the shoulders, deep folds to the floor+ const open = lerp(0.72, 0.28, sstep(0.05, 0.86, t)) + 0.05 * Math.sin(y * 19 + ph[2]);+ const reg = Math.max(open - Math.abs(th - 0.05), 0.05 - y);+ const hv = Math.sqrt(Math.abs(Math.sin(th * 4 + ph[3] + y * 0.3)));+ const collar = 0.022 * Math.exp(-((y - 0.915 * yN) * (y - 0.915 * yN)) / 0.0012); // a soft roll at the neck+ const rm = R + 0.03 * sstep(yN, 0.9 * yN, y) + collar + sstep(0.84 * yN, 0.5 * yN, y) * 0.056 * (hv - 0.62);+ d = smin(d, smax(rho - rm, reg, 0.006), 0.004);+ } else if (dress === 'angel') {+ // a girdled tunic blown a little to one side: fine folds and a bloused top+ const blouse = 0.018 * Math.exp(-((y - yB - 0.06) * (y - yB - 0.06)) / 0.003);+ d -= blouse;+ const low = 0.74 * yN + 0.02 * Math.sin(th * 7) - y;+ d = smin(d, smax(rho - (R + 0.016 * sstep(yN, 0.9 * yN, y) + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 7 + ph[2]))) - 0.5) * sstep(0.9 * yN, 0.8 * yN, y)), low, 0.005), 0.003);+ }+ // ---- shoulders and sleeves ----+ const yS = yN - 0.155;+ for (let k = 0; k < 2; k++) { const sg = k ? 1 : -1; d = smin(d, sdEll(x - cxS, y - yS, z - zc - sg * Rs * 0.98, 0.07, 0.078, 0.058), 0.05); }+ for (let k = 0; k < arms.length; k++) {+ const A = arms[k];+ let ad = sdCone(x, y, z, A.S[0], A.S[1], A.S[2], A.E[0], A.E[1], A.E[2], 0.056, 0.05);+ // forearm in a sleeve that widens to the cuff, with a roll of cloth at the wrist+ const fx = A.W[0] - A.E[0], fy = A.W[1] - A.E[1], fz = A.W[2] - A.E[2], fl = fx * fx + fy * fy + fz * fz, ft = clamp(((x - A.E[0]) * fx + (y - A.E[1]) * fy + (z - A.E[2]) * fz) / fl);+ const qx = x - A.E[0] - fx * ft, qy = y - A.E[1] - fy * ft, qz = z - A.E[2] - fz * ft, ql = len3(qx, qy, qz);+ const rr = lerp(0.05, 0.062, ft) + 0.006 * Math.sin(ft * 15 + k) * (1 - ft) + 0.008 * Math.exp(-((ft - 0.97) * (ft - 0.97)) / 0.002);+ ad = smin(ad, ql - rr, 0.018);+ // the cloth that falls from under the forearm: a hanging curtain of pleats+ // longest under the wrist, thinning as it falls, its lower edge never straight+ if (A.drape) {+ const h = A.E[1] + fy * ft - y;+ if (h > -0.04 && h < A.drape * 1.1) {+ const fall = clamp(h / A.drape), crv = lerp(0.044, 0.016, fall) + 0.014 * (Math.sqrt(Math.abs(Math.sin(ft * 12 + h * 2.5 + k))) - 0.5);+ const curtain = len3(qx * 0.72, 0, qz) - crv;+ const edge = h - A.drape * (0.28 + 0.72 * ft) - 0.022 * Math.sin(ft * 11 + k * 2);+ ad = smin(ad, smax(curtain, edge, 0.012), 0.018);+ }+ }+ d = smin(d, ad, 0.014);+ }+ // ---- feet ----+ d = smin(d, foot(x, y, z), 0.006);+ return Math.max(d, y - yN - 0.03);+ },+ };+}+// Where the arms go, and what the hands do when they get there+function armPose(o, Rs, zc, cx) {+ const y = 1.55, shR = [cx, y, zc + Rs], shL = [cx, y, zc - Rs], arm = (S, E, W, drape) => ({ S, E, W, drape });+ switch (o.hold) {+ case 'key': return [arm(shR, [0.03, 1.2, zc + Rs + 0.05], [0.17, 1.32, zc + 0.12], 0.32), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.17, 1.14, zc - 0.07], 0.34)];+ case 'crozier': return [arm(shR, [0.04, 1.2, zc + Rs + 0.06], [0.2, 1.3, zc + 0.14], 0.3), arm(shL, [0.05, 1.22, zc - Rs - 0.03], [0.2, 1.42, zc - 0.05], 0.3)];+ case 'sceptre': return [arm(shR, [0.04, 1.18, zc + Rs + 0.06], [0.2, 1.28, zc + 0.15], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.03], [0.17, 1.18, zc - 0.05], 0.3)];+ case 'mantle': return [arm(shR, [0.04, 1.14, zc + Rs + 0.05], [0.18, 1.2, zc + 0.04], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.15, 1.4, zc - 0.1], 0.26)];+ case 'scroll': return [arm(shR, [0.05, 1.2, zc + Rs + 0.04], [0.2, 1.34, zc + 0.1], 0.28), arm(shL, [0.05, 1.2, zc - Rs - 0.04], [0.2, 1.22, zc - 0.08], 0.28)];+ case 'child': return [arm(shR, [0.05, 1.14, zc + Rs + 0.04], [0.2, 1.24, zc + 0.03], 0.32), arm(shL, [0.05, 1.14, zc - Rs - 0.02], [0.2, 1.18, zc - 0.13], 0.3)];+ case 'bless': return [arm(shR, [0.07, 1.22, zc + Rs + 0.09], [0.19, 1.5, zc + 0.15], 0.3), arm(shL, [0.03, 1.2, zc - Rs - 0.04], [0.17, 1.2, zc - 0.07], 0.34)];+ default: return [arm(shR, [0.02, 1.12, zc + Rs + 0.04], [0.15, 1.05, zc + 0.08], 0.3), arm(shL, [0.02, 1.12, zc - Rs - 0.04], [0.15, 1.05, zc - 0.08], 0.3)];+ }+}+// ---- Things held: real objects, modelled at the scale of the hands that grip them ---------+function addBook(b, m, o = {}) {+ b.at(m, () => {+ const w = o.w || 0.082, h = o.h || 0.112, th = o.th || 0.027;+ b.box(-h, h, -th, -th + 0.009, -w, w, { s: 0.85 }); b.box(-h, h, th - 0.009, th, -w, w, { s: 1 }); // boards+ b.box(-h + 0.006, h - 0.006, -th + 0.009, th - 0.009, -w + 0.006, w - 0.004, { s: 0.62 }); // pages+ b.box(-h, h, -th, th, -w - 0.009, -w, { s: 0.8 }); // spine+ for (const i of [-1, 1]) for (const k of [-1, 1]) b.at(M.T(i * (h - 0.026), th, k * (w - 0.026)), () => b.lathe([[0.016, 0], [0.012, 0.007], [0.0, 0.009]], 6));+ b.at(M.T(0, th, 0), () => b.lathe([[0.03, 0], [0.024, 0.006], [0.0, 0.009]], 8));+ });+}+function addAttrs(fig, o, arms) {+ const { stone: b, gold: g } = fig, R = arms[0], Lf = arms[1];+ switch (o.hold) {+ case 'key': {+ // The great key of Peter held upright in the fist, the book against the left forearm+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01];+ b.tube([[hp[0], hp[1] - 0.15, hp[2]], [hp[0], hp[1] + 0.2, hp[2]]], 0.0125, 8, { capStart: true });+ b.at(M.T(hp[0], hp[1] + 0.245, hp[2]), () => b.at(M.Rx(90), () => { b.lathe([[0.04, -0.01], [0.05, 0], [0.04, 0.01], [0.026, 0.01], [0.02, 0], [0.026, -0.01], [0.04, -0.01]], 14); }));+ b.box(hp[0] - 0.01, hp[0] + 0.01, hp[1] - 0.15, hp[1] - 0.075, hp[2], hp[2] + 0.07, { s: 0.9 });+ b.box(hp[0] - 0.012, hp[0] + 0.012, hp[1] - 0.112, hp[1] - 0.1, hp[2] + 0.03, hp[2] + 0.048, { s: 0.3 });+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2] + 0.0), M.Rz(-64), M.Ry(8)));+ break;+ }+ case 'crozier': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 2.02;+ b.tube([[hp[0], 0.04, hp[2]], [hp[0], top, hp[2]]], 0.0145, 8, { capStart: true });+ for (const yK of [hp[1] + 0.22, top - 0.05]) b.at(M.T(hp[0], yK, hp[2]), () => b.lathe([[0.016, -0.03], [0.034, 0], [0.016, 0.03]], 8));+ const vol = []; for (let i = 0; i <= 20; i++) { const t = i / 20, a = -PI * 0.5 + t * PI * 2.5, r = lerp(0.115, 0.036, t); vol.push([hp[0] + r * Math.cos(a) * 1.0 + 0.0, top + 0.12 + r * Math.sin(a), hp[2]]); }+ g.tube(vol, (t) => lerp(0.02, 0.012, t), 7, { capStart: true });+ for (let i = 2; i < 16; i += 3) { const p = vol[i]; addCrocket(g, M.of(M.T(p[0], p[1], p[2]), M.Rz((i / 20) * 400 - 90)), 0.06, 0); }+ break;+ }+ case 'sceptre': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 1.95;+ b.tube([[hp[0], hp[1] - 0.2, hp[2]], [hp[0], top, hp[2]]], 0.0125, 8, { capStart: true });+ g.at(M.T(hp[0], top, hp[2]), () => {+ g.lathe([[0.0125, 0], [0.03, 0.022], [0.038, 0.05], [0.026, 0.078], [0.01, 0.086]], 10);+ for (let i = 0; i < 3; i++) { const a = i * 120 + 90; g.at(M.Ry(a), () => g.tube(spline([[0, 0.07, 0], [0.038, 0.1, 0], [0.042, 0.15, 0], [0.018, 0.17, 0]], 2), (t) => [lerp(0.016, 0.003, t), lerp(0.024, 0.004, t)], 6)); }+ });+ break;+ }+ case 'scroll': {+ const p = [Lf.W[0] + 0.07, Lf.W[1], Lf.W[2]];+ const path = []; for (let i = 0; i <= 14; i++) { const t = i / 14; path.push([R.W[0] + 0.09 + 0.02 * Math.sin(t * 5), R.W[1] - t * 0.7 + 0.03, lerp(R.W[2] - 0.03, Lf.W[2] + 0.05, sstep(0, 0.5, t)) + 0.02 * Math.sin(t * 7)]); }+ const sheet = (off, flip) => b.grid((u, v) => { const k = u * 14, i = Math.min(13, Math.floor(k)), f = k - i, q = path[i], w = path[i + 1]; return [lerp(q[0], w[0], f) + off, lerp(q[1], w[1], f), lerp(q[2], w[2], f) + (v - 0.5) * 0.15, 0.96]; }, 14, 1, { flip });+ sheet(0, false); sheet(-0.004, true);+ b.at(M.of(M.T(path[14][0], path[14][1] - 0.015, path[14][2]), M.Rx(90)), () => b.lathe([[0.022, -0.085], [0.024, -0.08], [0.024, 0.08], [0.022, 0.085]], 8));+ break;+ }+ case 'bless': {+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2]), M.Rz(-64), M.Ry(4)));+ break;+ }+ default: break;+ }+}+const CROWN_FLEURS = 8;+function addCrown(g, m, R0, tall = 1) {+ g.at(m, () => {+ g.lathe([[R0, 0], [R0 + 0.009, 0.004], [R0 + 0.009, 0.028], [R0 + 0.015, 0.038], [R0 + 0.006, 0.042], [R0 - 0.004, 0.034], [R0 - 0.004, 0]], 26);+ // jewels set in the band+ for (let i = 0; i < 12; i++) { const a = i * TAU / 12; blob(g, [(R0 + 0.013) * Math.cos(a), 0.02, (R0 + 0.013) * Math.sin(a)], [0.0072, 0.0072, 0.0072], 5, 3); }+ // Fleurs-de-lis cut from sheet: flat plates set on the rim, the way a goldsmith makes them+ const h = 0.05 * tall, fl = [[-0.03, 0], [0.03, 0], [0.024, h * 0.2], [0.04, h * 0.42], [0.031, h * 0.62], [0.017, h * 0.42], [0.009, h * 0.5], [0.012, h * 0.76], [0, h * 1.1], [-0.012, h * 0.76], [-0.009, h * 0.5], [-0.017, h * 0.42], [-0.031, h * 0.62], [-0.04, h * 0.42], [-0.024, h * 0.2]];+ for (let i = 0; i < CROWN_FLEURS; i++) {+ const a = (i + 0.5) * 360 / CROWN_FLEURS;+ g.at(M.of(M.Ry(90 - a), M.T(0, 0.03, R0 + 0.004)), () => g.slab(fl, [], -0.004, 0.004, { so: 1, sr: 1 }));+ }+ });+}++const figF = {}; // fields kept while a figure is carved+const yNeck = 1.7;+async function makeFigure(kind) {+ const o = FIGURES[kind], stone = new Batch(TILE.fine), gold = new Batch(TILE.gold);+ const body = bodyField(o), arms = body.arms, hO = Object.assign({ seed: o.seed }, o.head);+ // Head frame: gently inclined, sitting on the neck above the centre of the shoulders+ const zc = arms[0].S[2] - (0.142 * (o.slim || 1)), cx = arms[0].S[0];+ const headM = M.of(M.T(cx + 0.024, yNeck + 0.116, zc + (o.sway || 0) * 0.3), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(-5));+ const hF = headField(hO), toHead = invRigid(headM);+ const headW = (x, y, z) => { const q = toHead(x, y, z); return hF(q[0], q[1], q[2]); };+ // Hands: frames derived from the sleeve so that the wrist always leaves the cuff+ const hands = [];+ const handFor = (A, side, pose) => {+ const X0 = [A.W[0] - A.E[0], A.W[1] - A.E[1], A.W[2] - A.E[2]];+ let Y0;+ if (pose === 'support') Y0 = [0.2, -1, 0]; // palm up under a book+ else if (pose === 'bless') Y0 = [0, 0.1, side]; // palm turned forward, two fingers raised+ else Y0 = [0, 0, side]; // back of the hand outward+ const X = pose === 'bless' ? [0.25, 1, 0.05] : X0;+ return { m: frameOf(A.W, X, Y0), pose, thumb: pose === 'grip' || pose === 'edge' || pose === 'rest' ? -side : side };+ };+ const handsCfg = { key: ['grip', 'support'], crozier: ['grip', 'rest'], sceptre: ['grip', 'rest'], mantle: ['rest', 'rest'], scroll: ['edge', 'rest'], child: ['rest', 'support'], bless: ['bless', 'support'] }[o.hold] || ['rest', 'rest'];+ hands.push(handFor(arms[0], 1, handsCfg[0]), handFor(arms[1], -1, handsCfg[1]));+ const handFs = hands.map((h) => { const f = handField(h.pose, h.thumb), inv = invRigid(h.m); return (x, y, z) => { const q = inv(x, y, z); return f(q[0], q[1], q[2]); }; });+ // The Child: a real little person on the Virgin's arm, turning to her+ let childF = null, childM = null;+ if (o.hold === 'child') {+ const A = arms[1];+ // sits in the crook of her left arm, turned towards her, one small hand raised to her veil+ childM = M.of(M.T(A.W[0] - 0.015, A.W[1] + 0.082, A.W[2] - 0.0), M.Ry(-34), M.Rz(4));+ const cH = headField({ hair: 'curls', seed: 21, smile: 0.6 }), inv = invRigid(childM), k = 0.47;+ childF = (x, y, z) => {+ const q = inv(x, y, z), X = q[0], Y = q[1], Z = q[2];+ // A seated child in a long shift: torso, thighs across her arm, shins and small feet hanging+ let d = sdCone(X, Y, Z, 0, 0, 0, 0, 0.165, 0, 0.066, 0.05);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.0, s * 0.034, 0.125, -0.012, s * 0.04, 0.038, 0.03), 0.018); // thigh+ d = smin(d, sdCone(X, Y, Z, 0.125, -0.012, s * 0.04, 0.142, -0.135, s * 0.038, 0.03, 0.022), 0.012); // shin+ d = smin(d, sdCone(X, Y, Z, 0.142, -0.15, s * 0.038, 0.19, -0.17, s * 0.04, 0.02, 0.013), 0.006); // foot+ }+ d -= 0.005 * Math.sqrt(Math.abs(Math.sin(Math.atan2(Z, X) * 6 + Y * 4))) * sstep(0.12, 0.0, Y) * sstep(-0.1, 0.0, Y);+ d = smin(d, sdCone(X, Y, Z, 0.005, 0.135, -0.052, 0.06, 0.07, -0.05, 0.02, 0.016), 0.012); // one arm holds an orb on his knee+ d = smin(d, len3(X - 0.1, Y - 0.052, Z + 0.042) - 0.03, 0.003);+ d = smin(d, len3(X - 0.074, Y - 0.056, Z + 0.054) - 0.015, 0.004);+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.145, 0.05, -0.005, 0.2, 0.094, 0.02, 0.015), 0.012); // the other reaches back to touch her veil+ d = smin(d, len3(X + 0.007, Y - 0.214, Z - 0.104) - 0.0155, 0.005);+ d = smin(d, cH((X - 0.008) / k, (Y - 0.262) / k, Z / k) * k, 0.012);+ return d;+ };+ }+ const allF = (x, y, z) => {+ let d = body.F(x, y, z);+ d = Math.min(d, headW(x, y, z));+ for (let i = 0; i < handFs.length; i++) d = Math.min(d, handFs[i](x, y, z));+ if (childF) d = Math.min(d, childF(x, y, z));+ return d;+ };+ // ---- carve ----+ await carveInto(stone, body.F, [-0.42, -0.005, -0.52, 0.42, yNeck + 0.04, 0.52], 0.0105, o.wings ? 3800 : 4400, null, allF);+ const head = await headBatch(o.headKey);+ for (let i = 0; i < hands.length; i++) {+ const hb = await handBatch(hands[i].pose, hands[i].thumb);+ const base = stone.count;+ copyBatch(stone, hb, hands[i].m);+ }+ if (childF) await carveInto(stone, childF, [arms[1].W[0] - 0.24, arms[1].W[1] - 0.2, arms[1].W[2] - 0.26, arms[1].W[0] + 0.3, arms[1].W[1] + 0.56, arms[1].W[2] + 0.26], 0.0042, 3200, null, allF, { smooth: 1, ao: 0.34, aoMin: 0.3 });+ addAttrs({ stone, gold }, o, arms);+ // Crown, nimbus and wings+ if (o.head.crown) addCrown(gold, M.of(headM, M.T(-0.012, o.head.veil ? 0.088 : 0.096, 0)), o.head.veil ? 0.088 : 0.084);+ if (o.nimbus) gold.at(M.of(headM, M.T(-0.16, 0.03, 0), M.Ry(90)), () => { gold.slab(ringPts(0, 0, 0.235, 28), [], -0.014, 0.014, { sb: 0.8 }); });+ if (o.wings) {+ // Feathers in overlapping tiers, carved on a blade that rises from the shoulders and falls to the calf+ const W = (side) => (x, y, z) => {+ // The wall side (local -z in every jamb) folds along the stepped stone from the shoulder; the free side stands off behind the back+ const zz = z * side - 0.12 - zc * side, X = side < 0 ? x + 0.01 + 0.6 * (zz - 0.065) : x + 0.085 + zz * 0.18 + 0.05 * clamp((1.86 - y) / 1.32), u = clamp((1.86 - y) / 1.32), wid = 0.27 * Math.pow(Math.sin(PI * Math.pow(u, 0.6)), 0.65) * (1 - 0.3 * u);+ const v = zz - 0.03 - u * 0.1 + 0.015;+ let d = Math.abs(X) - 0.026 + 0.012 * u;+ d = Math.max(d, Math.abs(v - wid * 0.5) - wid * 0.5, y - 1.87, 0.53 - y);+ // Tiers of feathers: short and scaled over the shoulder, long pointed quills towards the tip+ const row = Math.abs(Math.sin(u * 14 + v * 6)), bars = Math.abs(Math.sin(v * 26 + u * 7));+ d -= 0.015 * Math.sqrt(row) * sstep(0.45, 0.2, u) + 0.016 * Math.sqrt(bars) * sstep(0.2, 0.42, u);+ // The wing's arm: it grows out of the shoulder blade, so the blade never hangs in the air behind the body+ return smin(d * 0.8, sdCone(x, y, zz, -0.035, 1.6, -0.06, side < 0 ? -0.045 : -0.12, 1.46, 0.1, 0.05, 0.028), 0.04);+ };+ for (const side of [-1, 1]) await carveInto(stone, W(side), [-0.3, 0.5, side > 0 ? zc + 0.05 : zc - 0.72, 0.1, 1.9, side > 0 ? zc + 0.72 : zc - 0.05], 0.0105, 900, null, allF);+ }+ stone.isFigure = true;+ return { stone, gold, head, headM };+}+// ---- Seated and kneeling figures for the tympana: same people, other postures -------------+function poseField(kind) {+ if (kind === 'seated') return (x, y, z) => {+ // A figure enthroned: knees forward, the robe falling from them in a curtain to the feet+ const th = Math.atan2(z, x);+ let d = sdCone(x, y, z, -0.04, 0.72, 0, -0.03, 1.38, 0, 0.17, 0.13);+ d = smin(d, sdEll(x, y - 1.46, z, 0.09, 0.12, 0.17), 0.03);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(x, y, z, -0.03, 0.7, s * 0.1, 0.27, 0.68, s * 0.11, 0.1, 0.09), 0.03);+ const cur = sdCone(x, y, z, 0.27, 0.64, s * 0.11, 0.31, 0.04, s * 0.1, 0.09, 0.12);+ d = smin(d, cur - 0.012 * Math.sqrt(Math.abs(Math.sin(z * 34 + x * 4))), 0.03);+ }+ d = smin(d, sdCone(x, y, z, 0.16, 0.45, 0, 0.27, 0.04, 0, 0.11, 0.16) - 0.014 * Math.sqrt(Math.abs(Math.sin(z * 30 + 1))), 0.03);+ // the lap folds run across, the bodice is drawn up+ d -= 0.012 * Math.sqrt(Math.abs(Math.sin((y + 0.12 * z * z * 40) * 28))) * sstep(0.95, 0.7, y) * sstep(0.45, 0.62, y);+ d -= 0.009 * Math.sqrt(Math.abs(Math.sin(th * 7 + y * 2))) * sstep(0.8, 1.0, y);+ return Math.max(d, -y);+ };+ return (x, y, z) => {+ // Kneeling in prayer, turned to the centre: a body with a thigh and a folded shin under long cloth. The folds fall straight+ // down from the waist and break on the ground, never radiating round the body like the ribs of a pumpkin+ let d = sdCone(x, y, z, -0.1, 0.46, 0, 0.03, 1.04, 0, 0.15, 0.115);+ d = smin(d, sdEll(x - 0.05, y - 1.12, z, 0.08, 0.1, 0.15), 0.03);+ d = smin(d, sdCone(x, y, z, -0.1, 0.42, 0, 0.28, 0.15, 0, 0.16, 0.11), 0.05);+ d = smin(d, sdCone(x, y, z, 0.28, 0.12, 0, -0.26, 0.1, 0, 0.115, 0.09), 0.04);+ const fall = 0.62 * Math.sqrt(Math.abs(Math.sin(z * 31 + x * 3 + 1.1))) + 0.38 * Math.sqrt(Math.abs(Math.sin(z * 63 + y * 5 + 2.3)));+ d -= 0.017 * (fall - 0.55) * sstep(0.86, 0.42, y);+ d -= 0.006 * Math.sqrt(Math.abs(Math.sin(z * 38 + y * 2))) * sstep(0.6, 0.86, y);+ return Math.max(d, -y);+ };+}+async function makePosed(kind, spec) {+ const stone = new Batch(TILE.fine), gold = new Batch(TILE.gold), bF = poseField(kind), hO = Object.assign({ seed: 3 }, spec.head);+ const hy = kind === 'seated' ? 1.74 : 1.36, hx = kind === 'seated' ? -0.01 : 0.075;+ const headM = M.of(M.T(hx, hy, 0), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(hO.nod || -6));+ const allF = bF, head = await headBatch(spec.headKey);+ stone.isFigure = true;+ await carveInto(stone, bF, [-0.42, -0.005, -0.42, 0.56, kind === 'seated' ? 1.62 : 1.26, 0.42], 0.0145, 2600, null, allF);+ if (hO.crown) addCrown(gold, M.of(headM, M.T(-0.014, 0.1, 0)), hO.veil ? 0.106 : 0.098);+ // Forearms and hands: joined in prayer when kneeling, one raised when enthroned+ const hand = await handBatch(kind === 'seated' ? 'bless' : 'support', 1);+ if (kind === 'seated') {+ const A = spec.bless === false ? null : { W: [0.2, 1.32, 0.17] };+ const armF = (x, y, z) => { let d = sdCone(x, y, z, -0.02, 1.42, 0.17, 0.06, 1.02, 0.23, 0.066, 0.055); d = smin(d, sdCone(x, y, z, 0.06, 1.02, 0.23, 0.2, 1.3, 0.17, 0.055, 0.058), 0.02); d = Math.min(d, smin(sdCone(x, y, z, -0.02, 1.42, -0.17, 0.05, 1.02, -0.22, 0.066, 0.055), sdCone(x, y, z, 0.05, 1.02, -0.22, 0.27, 0.92, -0.08, 0.055, 0.058), 0.02)); return d; };+ await carveInto(stone, armF, [-0.12, 0.84, -0.34, 0.36, 1.52, 0.34], 0.0125, 700, null, allF);+ copyBatch(stone, hand, frameOf([0.2, 1.3, 0.17], [0.25, 1, 0.05], [0, 0.1, 1]));+ addBook(stone, M.of(M.T(0.36, 0.92, -0.08), M.Rz(-78)), { w: 0.11, h: 0.15 });+ } else {+ const armF = (x, y, z) => { let d = 1e9; for (const s of [-1, 1]) d = Math.min(d, smin(sdCone(x, y, z, 0.03, 1.03, s * 0.15, 0.1, 0.8, s * 0.17, 0.062, 0.05), sdCone(x, y, z, 0.1, 0.8, s * 0.17, 0.25, 0.97, s * 0.04, 0.05, 0.055), 0.02)); return d; };+ await carveInto(stone, armF, [-0.08, 0.68, -0.3, 0.34, 1.12, 0.3], 0.0125, 600, null, allF);+ for (const s of [-1, 1]) copyBatch(stone, hand, frameOf([0.25, 0.97, s * 0.04], [0.5, 1, -s * 0.05], [1, -0.4, s * 0.9]));+ }+ return { stone, gold, head, headM };+}++// Cache of finished figures. Lower-detail copies for the arches come from the same sculpture.+const FIG_CACHE = {};+async function lightCopy(src, tris) { // the same surface with fewer triangles, keeping its uvs and carved occlusion+ const g = await simplifyGeometry(src.toGeo(), { targetTriangles: tris });+ const T = src.tile, b = new Batch(T), p = g.attributes.position, n = g.attributes.normal, u = g.attributes.uv, c = g.attributes.color, idx = g.index;+ for (let i = 0; i < p.count; i++) b.v(p.getX(i), p.getY(i), p.getZ(i), n.getX(i), n.getY(i), n.getZ(i), u.getX(i) * T, u.getY(i) * T, c ? c.getX(i) : 1);+ for (let t = 0; t < idx.count; t += 3) b.tri(idx.getX(t), idx.getX(t + 1), idx.getX(t + 2));+ return b;+}+async function lowDetail(src, tris) {+ const b = await lightCopy(src.stone, tris);+ b.isFigure = true;+ // A figure high in the arches carries a light copy of its head inside its own mesh: fewer draws, fewer triangles+ if (!src.head.low) src.head.low = await lightCopy(src.head, 420);+ copyBatch(b, src.head.low, src.headM);+ return { stone: b, gold: new Batch(TILE.gold) };+}+async function prepareFigures(kinds) {+ for (const k of kinds) FIG_CACHE[k + '|1'] = await makeFigure(k);+ if (P.preview !== 'statues') {+ for (const k of ['apostle', 'king', 'angel', 'bishop', 'queen']) if (FIG_CACHE[k + '|1']) FIG_CACHE[k + '|0'] = await lowDetail(FIG_CACHE[k + '|1'], P.archFigureTris);+ for (let k = 0; k < 3; k++) GARGOYLES[k] = await lightCopy(makeGargoyle(k), 800);+ FIG_CACHE['seated|1'] = await makePosed('seated', { headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 9, nod: -2 } });+ FIG_CACHE['seatedVirgin|1'] = await makePosed('seated', { headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, turn: -14, tilt: 4 } });+ FIG_CACHE['kneel|1'] = await makePosed('kneel', { headKey: 'veiled', head: { veil: 1, seed: 8, tilt: 0, nod: 8 } });+ FIG_CACHE['kneel|0'] = await lowDetail(FIG_CACHE['kneel|1'], P.archFigureTris);+ }+}+function figure(name, lod = 1) {+ const k = FIG_ALIAS[name] || name;+ return FIG_CACHE[k + '|' + lod] || FIG_CACHE[k + '|1'];+}+// Body and head are separate nodes: lod 0 lightens the body only, a head costs its bytes once however often it appears+function placeFigure(name, kind, o = {}) {+ const f = figure(kind, o.lod === undefined ? 1 : o.lod);+ if (!f) return null;+ // o.relief squeezes the figure front to back, so it is carved from the wall behind it instead of standing before it+ const s = o.scale || 1, sc = [s * (o.relief || 1), s, o.mirror ? -s : s];+ const mat = o.indoor ? MAT.figureIn : o.deep ? MAT.figureDeep : o.half ? MAT.figureHalf : MAT.figure;+ const stone = place(name, f.stone, mat, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ place(name + '_Gilt', f.gold, MAT.gold, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ let headPart = null;+ if (f.head) {+ const m = f.headM, e = eulerFromBasis([m[0], m[4], m[8]], [m[1], m[5], m[9]], [m[2], m[6], m[10]]);+ headPart = place(name + '_Head', f.head, mat, { pos: [m[3], m[7], m[11]], rot: e, parent: stone || undefined });+ }+ if (stone && !o.indoor && !o.deep && !o.half) FIG_PLACED.push({ part: stone, head: headPart });+ return stone;+}+const FIG_PLACED = [];++// ---- Foliage and crockets ----------------------------------------------------------+// One carved leaf curling at the tip; local +Y up the surface, +X outward from the host+function addLeaf(b, m, size = 1, o = {}) {+ b.at(m, () => {+ const nu = o.coarse ? 2 : 4, nv = o.coarse ? 3 : 4;+ b.grid((u, v) => {+ const s = (u - 0.5) * 2, W = 0.5 * Math.sin(PI * Math.pow(v, 0.72)) * (1 - 0.35 * v);+ const curl = Math.pow(v, 2.2);+ const lobes = 1 + 0.18 * Math.sin(v * 16) * Math.abs(s);+ const rib = 0.05 * (1 - Math.abs(s));+ return [size * (rib + 0.3 * curl - 0.42 * curl * curl * curl + 0.12 * s * s), size * (v - 0.3 * curl * v), size * (s * W * lobes), 0.72 + 0.28 * (1 - Math.abs(s)) + 0.1 * curl];+ }, nu, nv, { flip: true });+ });+}+// A crocket: a bud of curled foliage that sits on an arris. Local +X away from the arris, +Y up the slope.+function addCrocket(b, m, size = 1, lod = 1) {+ b.at(m, () => {+ const path = [];+ const N = lod ? 5 : 3;+ for (let i = 0; i <= N; i++) { const t = i / N, a = -0.9 + t * 3.8, r = lerp(0.3, 0.12, t) * size; path.push([size * 0.16 + r * Math.cos(a) * 0.95, size * 0.1 + r * Math.sin(a) * 0.9, 0]); }+ b.tube(path, (t) => [lerp(0.14, 0.05, t) * size, lerp(0.17, 0.07, t) * size], lod ? 5 : 3, { capStart: false, capEnd: false, ringS: (a) => 0.8 + 0.2 * Math.cos(a) });+ if (lod) for (const s of [-1, 1]) b.tube([[0.02 * size, 0.02 * size, 0], [0.2 * size, 0.1 * size, s * 0.22 * size], [0.34 * size, 0.26 * size, s * 0.3 * size]], (t) => [lerp(0.07, 0.015, t) * size, lerp(0.05, 0.02, t) * size], 3, { capEnd: false });+ });+}+function addFinial(b, h = 1, lod = 1) { // cross-shaped foliate finial, base at y=0+ b.lathe([[0.12 * h, 0], [0.1 * h, 0.05 * h], [0.14 * h, 0.09 * h], [0.08 * h, 0.14 * h], [0.05 * h, 0.5 * h], [0.09 * h, 0.56 * h], [0.05 * h, 0.62 * h], [0.02 * h, 0.98 * h], [0, h]], lod ? 8 : 5);+ for (let i = 0; i < 4; i++) {+ const a = i * 90 + 45;+ addLeaf(b, M.of(M.T(0, 0.14 * h, 0), M.Ry(a), M.T(0.05 * h, 0, 0)), 0.4 * h, { coarse: 1 });+ if (lod) addLeaf(b, M.of(M.T(0, 0.58 * h, 0), M.Ry(a + 45), M.T(0.03 * h, 0, 0)), 0.3 * h, { coarse: 1 });+ }+}+// Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame+function crocketsAlong(b, p, q, n, size, out, lod = 1, maker = addCrocket) {+ const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);+ const dn = d.map((c) => c / L);+ let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];+ const ol = Math.hypot(ox, oy, oz) || 1; ox /= ol; oy /= ol; oz /= ol;+ const zx = ox * 0, side = [dn[1] * oz - dn[2] * oy, dn[2] * ox - dn[0] * oz, dn[0] * oy - dn[1] * ox];+ // columns: X = out, Y = along edge, Z = side+ for (let i = 0; i < n; i++) {+ const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];+ const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];+ maker(b, m, size * lerp(1, 0.78, t), lod);+ }+}+// A crocket beaten in lead: one thick leaf turning up off the arris with a lobe either side. Same frame as addCrocket: X out, Y along the edge+function addLeadCrocket(b, m, s = 1) {+ b.at(m, () => {+ b.tube([[-0.04 * s, -0.02 * s, 0], [0.2 * s, 0.1 * s, 0], [0.34 * s, 0.3 * s, 0], [0.3 * s, 0.46 * s, 0]], (t) => [lerp(0.13, 0.015, t) * s, lerp(0.17, 0.02, t) * s], 4, { capEnd: false });+ for (const z of [-1, 1]) b.tube([[0, 0, z * 0.04 * s], [0.16 * s, 0.1 * s, z * 0.2 * s], [0.2 * s, 0.24 * s, z * 0.3 * s]], (t) => lerp(0.07, 0.012, t) * s, 3, { capEnd: false });+ });+}++// Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.+function addPinnacle(b, w, h, o = {}) {+ const hw = w / 2, shaftH = h * 0.42, gableH = w * 0.95, lod = o.lod === undefined ? 1 : o.lod;+ if (o.simple) { // far-away pinnacle: faceted shaft, spirelet, a few crockets, finial+ b.lathe([[hw * 1.414, 0], [hw * 1.414, shaftH], [hw * 1.6, shaftH + w * 0.06], [hw * 1.3, shaftH + w * 0.12], [hw * 0.1, h * 0.86]], 4, { faceted: true, phase: PI / 4 });+ for (let i = 0; i < 4; i++) { const a = rad(i * 90 + 45), c = Math.cos(a), s = Math.sin(a), bR = hw * 1.3 * 1.0; crocketsAlong(b, [c * bR, shaftH + w * 0.14, s * bR], [c * hw * 0.14, h * 0.84, s * hw * 0.14], 2, w * 0.46, [c, 0.15, s], 0); }+ b.at(M.T(0, h * 0.84, 0), () => addFinial(b, h * 0.16, 0));+ return;+ }+ // Shaft with moulded base and blind lancet panels+ b.lathe([[hw * 1.44, 0], [hw * 1.44, w * 0.06], [hw * 1.3, w * 0.12], [hw * 1.3, w * 0.2], [hw * 1.26, w * 0.24]], 4, { faceted: true, phase: PI / 4 });+ b.lathe([[hw * 1.414, w * 0.2], [hw * 1.414, shaftH]], 4, { faceted: true, phase: PI / 4, s: 0.95 });+ for (let i = 0; i < 4; i++) b.at(M.Ry(i * 90), () => {+ const pw = w * 0.62, y0 = w * 0.35, ys = shaftH - w * 0.5;+ b.at(M.T(hw + 0.004, 0, 0), () => b.at(M.Ry(90), () => { // panel recessed look: shaded inset lancet with moulded frame+ b.sweep(ofsXY(pointedArc(pw, pw * 0.85, 3), 0, ys), hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[-pw / 2, y0], [-pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[pw / 2, y0], [pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.face([[-pw / 2, y0, -0.002, 0.5], [pw / 2, y0, -0.002, 0.5], [pw / 2, ys, -0.002, 0.5], [0, ys + pw * 0.8, -0.002, 0.45], [-pw / 2, ys, -0.002, 0.5]]);+ }));+ // Gablet on each face with crockets+ b.at(M.T(hw * 0.96, shaftH, 0), () => {+ b.slabX = true;+ b.at(M.Ry(90), () => b.slab([[-hw * 1.04, 0], [hw * 1.04, 0], [0, gableH]], [], 0, w * 0.08, { sf: 1, sb: 0.8 }));+ crocketsAlong(b, [0.02, 0.02, -hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, -1], 0);+ crocketsAlong(b, [0.02, 0.02, hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, 1], 0);+ b.at(M.T(0.04, gableH * 0.94, 0), () => addFinial(b, w * 0.34, 0));+ });+ });+ // Cornice ring and spirelet+ b.at(M.T(0, shaftH, 0), () => {+ b.lathe([[hw * 1.414, 0], [hw * 1.6, w * 0.06], [hw * 1.6, w * 0.1], [hw * 1.414, w * 0.14]], 4, { faceted: true, phase: PI / 4 });+ const sh = h - shaftH - w * 0.1;+ b.at(M.T(0, w * 0.12, 0), () => {+ b.lathe([[hw * 1.3, 0], [hw * 0.09, sh * 0.82]], 8, { faceted: true, phase: PI / 8 });+ const n = lod ? 5 : 3;+ for (let i = 0; i < 4; i++) {+ const a = rad(i * 90 + 22.5 + 22.5 * 0);+ const c = Math.cos(a + rad(22.5)), s = Math.sin(a + rad(22.5));+ const bR = hw * 1.3 * Math.cos(rad(0)), tR = hw * 0.09;+ crocketsAlong(b, [c * bR, sh * 0.04, s * bR], [c * tR, sh * 0.8, s * tR], n, w * 0.36, [c, 0.12, s], lod);+ }+ b.at(M.T(0, sh * 0.8, 0), () => (o.finial || b) === b ? addFinial(b, sh * 0.22 + w * 0.1, lod) : null);+ });+ });+}++// ---- Gargoyle ------------------------------------------------------------------------+// A crouching beast thrusting out from the wall along +X, root at the origin. About 2.3 m long.+function makeGargoyle(kind = 0) {+ const b = new Batch(TILE.fine), rng = mkRng(kind * 17 + 5);+ const ridge = (kind === 1 ? 0.3 : 0.22);+ const headY = kind === 2 ? 0.1 : 0.34;+ const spine = spline([[-0.3, -0.05, 0], [0.22, 0.2, 0], [0.75, 0.3, 0], [1.2, 0.2, 0], [1.56, headY, 0]], 5);+ b.tube(spine, (t) => { const r = keyf([[0, 0.34, 0.3], [0.25, 0.36, 0.34], [0.55, 0.22, 0.24], [0.82, 0.16, 0.17], [1, 0.19, 0.2]], t); return [r[0], r[1]]; }, 16, {+ capStart: true,+ up: [0, 1, 0],+ ring: (a, t) => 1 + ridge * Math.pow(Math.max(Math.cos(a), 0), 10) * Math.abs(Math.sin(t * 26)) + 0.05 * Math.abs(Math.sin(a * 5 + t * 11)),+ ringS: (a, t) => 0.74 + 0.26 * Math.abs(Math.sin(a * 5 + t * 11)),+ });+ // Skull, snout, gaping jaws+ const hx = 1.56, hy = headY;+ blob(b, [hx + 0.05, hy + 0.03, 0], [0.22, 0.19, 0.2], 14, 10, 0.92);+ b.tube(spline([[hx + 0.1, hy + 0.06, 0], [hx + 0.34, hy + 0.05, 0], [hx + 0.56, hy + 0.1, 0]], 3), (t) => [lerp(0.14, 0.07, t), lerp(0.15, 0.085, t)], 10, { capStart: true, up: [0, 1, 0], ring: (a, t) => 1 + 0.2 * Math.pow(Math.max(Math.cos(a), 0), 6) * Math.abs(Math.sin(t * 9)) });+ b.shade(0.9, () => b.tube(spline([[hx + 0.05, hy - 0.1, 0], [hx + 0.28, hy - 0.2, 0], [hx + 0.5, hy - 0.2, 0]], 3), (t) => [lerp(0.09, 0.045, t), lerp(0.125, 0.07, t)], 10, { capStart: true, up: [0, 1, 0] }));+ b.shade(0.34, () => blob(b, [hx + 0.2, hy - 0.06, 0], [0.16, 0.085, 0.095], 8, 5));+ for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {+ const f = i / 3.5;+ b.tube([[hx + 0.26 + f * 0.27, hy + 0.0 + f * 0.075, s * lerp(0.1, 0.06, f)], [hx + 0.27 + f * 0.27, hy - 0.08 + f * 0.075, s * lerp(0.1, 0.06, f)]], (t) => lerp(0.018, 0.002, t), 4, { capStart: true });+ b.tube([[hx + 0.22 + f * 0.27, hy - 0.15, s * lerp(0.1, 0.05, f)], [hx + 0.22 + f * 0.27, hy - 0.08, s * lerp(0.1, 0.05, f)]], (t) => lerp(0.016, 0.002, t), 4, { capStart: true });+ }+ // Brow, eyes, ears or horns+ for (const s of [-1, 1]) {+ b.tube(spline([[hx + 0.02, hy + 0.2, s * 0.03], [hx + 0.17, hy + 0.17, s * 0.1], [hx + 0.24, hy + 0.12, s * 0.15]], 2), (t) => lerp(0.03, 0.02, t), 5, { capStart: true });+ b.shade(0.38, () => blob(b, [hx + 0.21, hy + 0.09, s * 0.11], [0.05, 0.04, 0.045], 6, 4));+ blob(b, [hx + 0.235, hy + 0.09, s * 0.115], [0.024, 0.022, 0.022], 6, 4);+ if (kind === 1) b.tube(spline([[hx - 0.05, hy + 0.14, s * 0.11], [hx - 0.1, hy + 0.3, s * 0.18], [hx - 0.25, hy + 0.42, s * 0.2], [hx - 0.42, hy + 0.42, s * 0.15]], 3), (t) => lerp(0.05, 0.005, t), 6, { capStart: true });+ else b.grid((u, v) => { const L = 0.3, W = 0.09 * Math.sin(PI * u); return [hx - 0.08 - u * L * 0.5, hy + 0.13 + u * L * 0.7, s * (0.13 + u * 0.09 + (v - 0.5) * W * 2), 0.7 + 0.3 * Math.abs(v - 0.5) * 2]; }, 4, 2, { flip: s > 0 });+ if (kind === 1) { /* also add jowls */ blob(b, [hx + 0.1, hy - 0.04, s * 0.13], [0.09, 0.06, 0.05], 6, 4, 0.9); }+ // Fore leg gripping the ledge+ const sh = [0.5, 0.12, s * 0.3], el = [0.68, -0.28, s * 0.42], wr = [0.98, -0.42, s * 0.3];+ b.tube(spline([sh, el, wr], 4), (t) => keyf([[0, 0.16], [0.4, 0.105], [1, 0.07]], t)[0], 10, { capStart: true, ring: (a, t) => 1 + 0.08 * Math.abs(Math.sin(a * 3 + t * 8)) });+ blob(b, wr, [0.1, 0.06, 0.09], 8, 5);+ for (let i = -1; i <= 1; i++) b.tube(spline([[wr[0] + 0.03, wr[1], wr[2] + i * 0.05], [wr[0] + 0.14, wr[1] - 0.02, wr[2] + i * 0.08], [wr[0] + 0.21, wr[1] - 0.11, wr[2] + i * 0.09]], 2), (t) => lerp(0.032, 0.004, t), 5);+ // Folded wing+ const span = kind === 0 ? 0.95 : 0.7;+ const wingFn = (off) => (u, v) => {+ const L = u * span, W = 0.44 * Math.sin(PI * Math.pow(u, 0.7));+ const memb = Math.abs(Math.sin(v * 7.5 + u * 1.2)) * 0.05 * u;+ return [0.42 - L + (off), 0.3 + 0.48 * Math.sin(u * 1.25) * (1 - v * 0.9) - v * W * 0.75 + memb, s * (0.27 + 0.2 * u + v * W * 0.5), 0.68 + 0.32 * (memb / 0.05)];+ };+ b.grid(wingFn(0), 7, 5, { flip: s < 0 });+ b.grid(wingFn(-0.02), 7, 5, { flip: s > 0 });+ }+ return b;+}+const GARGOYLES = [];+function gargoyle(kind) { if (!GARGOYLES[kind]) GARGOYLES[kind] = makeGargoyle(kind); return GARGOYLES[kind]; }++// ==================================================================================+// STAINED GLASS: a tiny vector-art engine producing real leaded panes.+// Every shape is a pane of pot-metal colour with a came (lead strip) around it; large+// figures get painted grisaille lines. Windows are geometry, so they stay crisp at any distance.+// ==================================================================================+function offsetPoly(pts, d) {+ const n = pts.length, s = area2(pts) >= 0 ? 1 : -1, out = [];+ for (let i = 0; i < n; i++) {+ const a = pts[(i - 1 + n) % n], b = pts[i], c = pts[(i + 1) % n];+ let ax = b[0] - a[0], ay = b[1] - a[1], cx = c[0] - b[0], cy = c[1] - b[1];+ const la = Math.hypot(ax, ay) || 1, lc = Math.hypot(cx, cy) || 1; ax /= la; ay /= la; cx /= lc; cy /= lc;+ let nx = (ay + cy) * s, ny = -(ax + cx) * s; const l = Math.hypot(nx, ny) || 1; nx /= l; ny /= l;+ const k = clamp(1 / Math.max(0.35, Math.abs(nx * ay * s - ny * ax * s)), 1, 2.4);+ out.push([b[0] + nx * d * k, b[1] + ny * d * k]);+ }+ return out;+}+function clipConvex(poly, clip) {+ let out = poly; const sgn = area2(clip) >= 0 ? 1 : -1;+ for (let i = 0; i < clip.length && out.length; i++) {+ const a = clip[i], b = clip[(i + 1) % clip.length], inp = out; out = [];+ const side = (p) => sgn * ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]));+ for (let j = 0; j < inp.length; j++) {+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);+ if (sp >= 0) out.push(p);+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([lerp(p[0], q[0], t), lerp(p[1], q[1], t)]); }+ }+ }+ return out;+}+function isConvex(p) { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = p[(i + 2) % p.length]; const s = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); if (Math.abs(s) < 1e-9) continue; if (!sign) sign = Math.sign(s); else if (Math.sign(s) !== sign) return false; } return true; }++class Glass {+ constructor(seed = 1) { this.b = new Batch(1); this.b.noColor = true; this.r = mkRng(seed * 13 + 3); this.clip = null; this.xf = [1, 0, 0, 1, 0, 0]; this.came = 0.022; }+ xform(a, b, c, d, e, f) { const m = this.xf; return [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]]; }+ withXf(rotDeg, tx, ty, s, fn) { const o = this.xf, c = Math.cos(rad(rotDeg)) * s, sn = Math.sin(rad(rotDeg)) * s; this.xf = this.xform(c, sn, -sn, c, tx, ty); fn(this); this.xf = o; }+ withClip(pts, fn) { const o = this.clip; const m = this.xf; this.clip = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]); fn(this); this.clip = o; }+ toLocal(p) { const m = this.xf, det = m[0] * m[3] - m[1] * m[2], x = p[0] - m[4], y = p[1] - m[5]; return [(m[3] * x - m[2] * y) / det, (-m[1] * x + m[0] * y) / det]; }+ // Writes one flat polygon. map(worldPoint) -> [u,v] in atlas pixels; otherwise a window on the sheet of its colour+ raw(pts, color, layer, jitter = 1, map = null) {+ if (layer === -0.4 && !this.b.outline) this.b.outline = pts; // the first polygon of a window is its whole outline+ const name = GLASS_ALIAS[color] || color, fam = GL.fam[name] || GL.fam.lead, z = 0.002 + layer * 0.0035, C = GL.small;+ // Every pane is cut from a different part of the sheet, so no two panes match;+ // 200 pixels of sheet to the metre, so the streaks and clouds of the pot-metal show in the pane+ const ox = fam[0] + 44 + this.r() * (C - 88), oy = fam[1] + 44 + this.r() * (C - 88), rot = this.r() * 6.28, cr = Math.cos(rot) * 200, sr = Math.sin(rot) * 200;+ let mx = 0, my = 0; for (const p of pts) { mx += p[0]; my += p[1]; } mx /= pts.length; my /= pts.length;+ const ids = pts.map((p) => {+ let u, v;+ if (map) { const q = map(p); u = q[0]; v = q[1]; } else {+ const dx = clamp(p[0] - mx, -0.2, 0.2), dy = clamp(p[1] - my, -0.2, 0.2);+ u = ox + (dx * cr - dy * sr) * jitter; v = oy + (dx * sr + dy * cr) * jitter;+ }+ return this.b.v(p[0], p[1], z, 0, 0, 1, u / GL.w, v / GL.h, 1);+ });+ if (isConvex(pts)) { const flip = area2(pts) < 0; for (let i = 1; i < ids.length - 1; i++) { if (flip) this.b.tri(ids[0], ids[i + 1], ids[i]); else this.b.tri(ids[0], ids[i], ids[i + 1]); } } else { const t = triangulate(pts); for (const k of t) this.b.tri(ids[k[0]], ids[k[1]], ids[k[2]]); }+ }+ // a pane: colour polygon + lead came. o.tile paints drapery across it, o.orn ornament.+ poly(pts, color, layer = 1, o = {}) {+ const m = this.xf; let P2 = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]);+ if (area2(P2) < 0) P2 = P2.slice().reverse();+ const came = o.came === undefined ? this.came : o.came;+ if (came > 0) {+ let C = offsetPoly(P2, came);+ if (this.clip) C = clipConvex(C, this.clip);+ if (C.length >= 3 && Math.abs(area2(C)) > 1e-5) this.raw(C, 'lead', layer - 0.5, 0);+ } else if (came < 0) P2 = offsetPoly(P2, came); // over a lead backing: inset instead of a second polygon+ if (this.clip) P2 = clipConvex(P2, this.clip);+ if (P2.length < 3 || Math.abs(area2(P2)) <= 1e-5) return this;+ let map = o.map || null;+ if (o.tile && GL.tile[o.tile]) { // planar mapping of the painted tile over the local box+ const T = GL.tile[o.tile], B = o.box, f = o.flipU ? -1 : 1;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + (f > 0 ? a : 1 - a) * (T[2] - 4), T[1] + 2 + (1 - c) * (T[3] - 4)]; };+ } else if (o.orn && GL.orn[o.orn]) {+ const T = GL.orn[o.orn], B = o.box;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + a * (GL.small - 4), T[1] + 2 + (1 - c) * (GL.small - 4)]; };+ }+ this.raw(P2, color, layer, o.jitter === undefined ? 1 : o.jitter, map);+ return this;+ }+ disc(cx, cy, r, color, layer = 1, n = 12, o) { return this.poly(ringPts(cx, cy, r, n), color, layer, o); }+ // a head drawn by the glass painter: an oval pane mapped onto a face from the atlas+ face(cx, cy, rx, ry, kind, layer, flip) {+ const F = GL.face[kind] || GL.face.youth, C = GL.cell, pts = []; for (let i = 0; i < 12; i++) { const a = i * TAU / 12; pts.push([cx + rx * Math.cos(a), cy + ry * Math.sin(a)]); }+ return this.poly(pts, 'flesh', layer, { came: 0.012, map: (p) => { const q = this.toLocal(p), a = ((q[0] - cx) / rx) * (flip ? -1 : 1), c = (q[1] - cy) / ry; return [F[0] + C / 2 + clamp(a, -1, 1) * (C / 2 - 3), F[1] + C / 2 - clamp(c, -1, 1) * (C / 2 - 3)]; } });+ }+ // painted line (grisaille): a thin dark ribbon along a polyline+ line(pts, w = 0.02, color = 'lead', layer = 5) {+ for (let i = 0; i < pts.length - 1; i++) {+ const a = pts[i], b = pts[i + 1]; let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx *= w / 2 / l; dy *= w / 2 / l;+ this.poly([[a[0] - dy, a[1] + dx], [a[0] + dy, a[1] - dx], [b[0] + dy, b[1] - dx], [b[0] - dy, b[1] + dx]], color, layer, { came: 0, jitter: 0 });+ }+ return this;+ }+}++// Diapered mosaic background: lozenges of two deep colours, a jewel at each crossing+// (one painted square of leaded lozenges laid down repeatedly: fine as real mosaic, cheap as two triangles a square)+function gDiaper(g, x0, y0, x1, y1, cell, c1, c2, c3, layer = 0) {+ const T = c1 === 'outer' ? 1.5 : 0.72, G = GL.ground[GROUND_OF[c1] || 'rc'], C = GL.cell;+ for (let y = Math.floor(y0 / T) * T; y < y1; y += T) for (let x = Math.floor(x0 / T) * T; x < x1; x += T) {+ const qx = x, qy = y, map = (p) => [G[0] + 2 + clamp((p[0] - qx) / T) * (C - 4), G[1] + 2 + clamp((p[1] - qy) / T) * (C - 4)];+ g.poly([[x, y], [x + T, y], [x + T, y + T], [x, y + T]], c1, layer, { came: 0, map });+ }+}++// A figure in glass: long Gothic proportions, robes painted in brown, the head drawn by hand. (x,y) = feet.+function gFigure(g, x, y, h, o = {}) {+ const sx = o.sx || 0.82, hTot = h; h = hTot * sx;+ const f = o.flip ? -1 : 1, L = (u, v) => [x + f * u * h, y + (v <= 0.76 ? v / 0.76 * (hTot - 0.24 * h) : hTot - (1 - v) * h)], PS = (a) => a.map((p) => L(p[0], p[1]));+ const robe = o.robe || 'white', mantle = o.mantle || 'cobalt', skin = o.skin || 'flesh', big = hTot > 2.2, layer = o.layer === undefined ? 2 : o.layer;+ const sit = o.pose === 'sit', kneel = o.pose === 'kneel';+ const hy = kneel ? 0.67 : sit ? 0.86 : 0.875, hr = 0.066, hx = kneel ? 0.04 : 0;+ // Box in world units for tile mapping, in the local frame of the Glass object+ const bx = [x - h * 0.34, y, x + h * 0.34, y + hTot * 0.86], tR = DRAPE_OF[robe] || 'white', tM = DRAPE_OF[mantle] || 'cobalt';+ const pane = (pts, col, lay, tile, opt) => g.poly(PS(pts), col, lay, Object.assign({ tile, box: bx, flipU: !!o.flip }, opt || {}));+ const brk = (pts, c1, lay, tile) => { // a robe is many pieces of glass: cut it across with lead+ const P3 = PS(pts); let y0 = 1e9, y1 = -1e9; for (const q of P3) { y0 = Math.min(y0, q[1]); y1 = Math.max(y1, q[1]); }+ const n = big ? 4 : (hTot > 0.9 ? 2 : 1);+ for (let i = 0; i < n; i++) {+ const a = lerp(y0, y1, i / n), c = lerp(y0, y1, (i + 1) / n), sl = big ? 0.12 * h * (i % 2 ? 1 : -1) * 0.5 : 0;+ const piece = clipConvex(clipConvex(P3, [[-1e4, a - sl * 0], [1e4, a + sl], [1e4, 1e4], [-1e4, 1e4]]), [[-1e4, -1e4], [1e4, -1e4], [1e4, c + sl], [-1e4, c - sl * 0]]);+ if (piece.length >= 3) g.poly(piece, c1, lay, { tile, box: bx, flipU: !!o.flip, came: 0.016 });+ }+ };+ if (o.wings) {+ pane([[-0.08, hy - 0.1], [-0.3, hy + 0.1], [-0.4, hy - 0.08], [-0.34, 0.4], [-0.24, 0.2], [-0.1, 0.42]], o.wings, layer - 0.2, null);+ pane([[0.08, hy - 0.1], [0.3, hy + 0.1], [0.4, hy - 0.08], [0.34, 0.4], [0.24, 0.2], [0.1, 0.42]], o.wings, layer - 0.2, null);+ if (hTot > 1) for (const s of [-1, 1]) { g.line(PS([[s * 0.1, hy - 0.08], [s * 0.3, 0.52]]), h * 0.01, 'lead', layer); g.line(PS([[s * 0.2, hy - 0.02], [s * 0.34, 0.42]]), h * 0.008, 'lead', layer); }+ }+ if (o.halo !== false) {+ const c = L(hx, hy);+ g.disc(c[0], c[1], h * 0.116, o.halo || 'gold', layer - 0.1, big ? 18 : 12);+ if (o.cross) { for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[c[0] + h * 0.116 * Math.cos(a - 0.26), c[1] + h * 0.116 * Math.sin(a - 0.26)], [c[0] + h * 0.116 * Math.cos(a + 0.26), c[1] + h * 0.116 * Math.sin(a + 0.26)], c], 'ruby', layer, { came: 0.008 }); } }+ }+ // ---- body ----+ if (sit) {+ brk([[-0.12, 0.72], [0.12, 0.72], [0.19, 0.44], [0.26, 0.4], [0.27, 0.08], [0.22, 0.0], [-0.22, 0.0], [-0.2, 0.3], [-0.14, 0.44]], robe, layer, tR);+ brk([[-0.13, 0.74], [0.05, 0.74], [0.17, 0.46], [0.24, 0.4], [0.2, 0.18], [-0.06, 0.1], [-0.2, 0.3], [-0.14, 0.46]], mantle, layer + 0.5, tM);+ } else if (kneel) {+ brk([[-0.08, 0.56], [0.13, 0.56], [0.16, 0.26], [0.26, 0.1], [0.26, 0], [-0.26, 0], [-0.2, 0.12]], robe, layer, tR);+ brk([[-0.09, 0.57], [0.08, 0.57], [0.13, 0.28], [0.1, 0.1], [-0.2, 0.05], [-0.16, 0.24]], mantle, layer + 0.5, tM);+ } else {+ const c = o.sway === undefined ? 0.02 : o.sway;+ brk([[-0.1 + c, 0.75], [0.1 + c, 0.75], [0.115, 0.46], [0.14, 0.2], [0.2, 0.03], [0.15, 0.0], [0.05, 0.022], [-0.05, 0.0], [-0.16, 0.012], [-0.2, 0.04], [-0.14, 0.22], [-0.115, 0.46]], robe, layer, tR);+ brk([[-0.11 + c, 0.76], [0.03 + c, 0.76], [0.12, 0.5], [0.135, 0.3], [0.09, 0.15], [-0.06, 0.11], [-0.165, 0.22], [-0.125, 0.46]], mantle, layer + 0.5, tM);+ pane([[-0.09, 0.03], [-0.02, 0.03], [-0.02, -0.012], [-0.115, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ pane([[0.02, 0.03], [0.09, 0.03], [0.115, -0.012], [0.02, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ }+ // ---- arms and hands ----+ const by = kneel ? 0.5 : 0.68, armPose = o.arm || 'hold';+ const ribbon = (a, b, w, col, tile) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy) || 1, nx = -dy / l * w / 2, ny = dx / l * w / 2; g.poly(PS([[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]]), col, layer + 1, { came: 0.012, tile, box: bx }); };+ const hand = (p) => { const c = L(p[0], p[1]); g.poly([[c[0] - h * 0.026, c[1] - h * 0.018], [c[0] + h * 0.018, c[1] - h * 0.022], [c[0] + h * 0.03, c[1] + h * 0.016], [c[0] + h * 0.006, c[1] + h * 0.03], [c[0] - h * 0.02, c[1] + h * 0.018]], skin, layer + 1.3, { came: 0.008 }); };+ const arms = { hold: [[0.13, by - 0.12], [0.03, by - 0.18], [-0.13, by - 0.12], [-0.03, by - 0.2]], up: [[0.17, by - 0.03], [0.27, by + 0.18], [-0.12, by - 0.12], [-0.03, by - 0.22]], out: [[0.19, by - 0.08], [0.32, by - 0.06], [-0.19, by - 0.08], [-0.32, by - 0.06]], pray: [[0.1, by - 0.14], [0.12, by - 0.03], [-0.04, by - 0.14], [0.1, by - 0.03]], point: [[0.17, by - 0.08], [0.33, by + 0.04], [-0.12, by - 0.12], [-0.04, by - 0.2]], orant: [[0.2, by + 0.0], [0.27, by + 0.14], [-0.2, by + 0.0], [-0.27, by + 0.14]] };+ const A = arms[armPose] || arms.hold;+ ribbon([0.095, by], A[0], 0.07, mantle, tM); ribbon(A[0], A[1], 0.056, robe, tR); hand(A[1]);+ ribbon([-0.095, by], A[2], 0.07, mantle, tM); ribbon(A[2], A[3], 0.056, robe, tR); hand(A[3]);+ // ---- attributes ----+ const at = o.attr;+ if (at === 'book') { g.poly(PS([[A[1][0] - 0.1, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] + 0.12], [A[1][0] - 0.1, A[1][1] + 0.12]]), 'ruby', layer + 1.5, { came: 0.01 }); g.poly(PS([[A[1][0] - 0.07, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.09], [A[1][0] - 0.07, A[1][1] + 0.09]]), 'gold', layer + 1.6, { came: 0 }); }+ else if (at === 'staff' || at === 'cross') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, 0], [sxx + 0.012, 0], [sxx + 0.012, 1.08], [sxx - 0.012, 1.08]]), 'gold', layer + 0.9, { came: 0.008 }); if (at === 'cross') g.poly(PS([[sxx - 0.07, 0.95], [sxx + 0.07, 0.95], [sxx + 0.07, 0.98], [sxx - 0.07, 0.98]]), 'gold', layer + 0.9, { came: 0.008 }); else g.poly(ringPts(sxx + 0.03, 1.1, 0.045, 8).map((p) => L(p[0], p[1])), 'gold', layer + 0.9, { came: 0.008 }); }+ else if (at === 'sword') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, by - 0.08], [sxx + 0.012, by - 0.08], [sxx, 1.02]]), 'white', layer + 0.9, { came: 0.008 }); g.poly(PS([[sxx - 0.05, by - 0.08], [sxx + 0.05, by - 0.08], [sxx + 0.05, by - 0.05], [sxx - 0.05, by - 0.05]]), 'gold', layer + 1, { came: 0.008 }); }+ else if (at === 'key') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.01, by - 0.14], [sxx + 0.01, by - 0.14], [sxx + 0.01, by + 0.2], [sxx - 0.01, by + 0.2]]), 'gold', layer + 1, { came: 0.006 }); g.poly(ringPts(sxx, by + 0.23, 0.035, 6).map((p) => L(p[0], p[1])), 'gold', layer + 1, { came: 0.006 }); g.poly(PS([[sxx, by - 0.14], [sxx + 0.05, by - 0.14], [sxx + 0.05, by - 0.1], [sxx, by - 0.1]]), 'gold', layer + 1, { came: 0.006 }); }+ else if (at === 'lily') { const sxx = A[1][0]; g.line(PS([[sxx, by - 0.22], [sxx + 0.01, by + 0.2]]), h * 0.012, 'green', layer + 1); for (let i = 0; i < 3; i++) { const yy = by + 0.08 + i * 0.07; const q = L(sxx + 0.02 * (i % 2 ? 1 : -1), yy); g.disc(q[0], q[1], h * 0.024, 'white', layer + 1.1, 5, { came: 0.006 }); } }+ // ---- head: drawn, not a disc ----+ const hc = L(hx, hy);+ const kind = o.face || (o.wings ? 'angel' : o.veil ? 'virgin' : o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ const nkA = L(hx - 0.02, hy - 0.11), nkB = L(hx + 0.02, hy - 0.11), nkC = L(hx + 0.02, hy - 0.04), nkD = L(hx - 0.02, hy - 0.04);+ g.poly([nkA, nkB, nkC, nkD], skin, layer + 1.5, { came: 0.008 });+ g.face(hc[0], hc[1], h * hr * 1.02, h * hr * 1.24, kind, layer + 1.8, !!o.flip);+ if (o.crown) g.poly(PS([[hx - 0.066, hy + 0.05], [hx + 0.066, hy + 0.05], [hx + 0.08, hy + 0.13], [hx + 0.04, hy + 0.095], [hx, hy + 0.14], [hx - 0.04, hy + 0.095], [hx - 0.08, hy + 0.13]]), 'gold', layer + 2.2, { came: 0.01 });+ if (o.mitre) g.poly(PS([[hx - 0.056, hy + 0.05], [hx + 0.056, hy + 0.05], [hx + 0.048, hy + 0.15], [hx, hy + 0.215], [hx - 0.048, hy + 0.15]]), o.mitre, layer + 2.2, { came: 0.01 });+}+// ---- Narrative scenes, composed inside a circle of radius r at the origin ---------+const SCENES = {+ annunciation(g, r) { // Angel greets the Virgin; lily between them, dove above+ gFigure(g, -r * 0.46, -r * 0.72, r * 1.34, { robe: 'white', mantle: 'emerald', wings: 'rubyLt', arm: 'point', attr: null, hair: 'amber' });+ gFigure(g, r * 0.46, -r * 0.72, r * 1.3, { flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray', halo: 'gold' });+ g.poly([[-0.1, -r * 0.72], [0.1, -r * 0.72], [0.06, -r * 0.5], [-0.06, -r * 0.5]], 'gold', 3, { came: 0.012 });+ g.line([[0, -r * 0.5], [0, r * 0.05]], 0.025, 'green', 3.2);+ [0.05, -0.1, -0.24].forEach((v, i) => g.disc((i % 2 ? 1 : -1) * r * 0.05, r * v, r * 0.05, 'white', 3.3, 5, { came: 0.008 }));+ g.disc(0, r * 0.62, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.14, r * 0.66], [0, r * 0.6], [r * 0.14, r * 0.66], [0, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ nativity(g, r) { // Virgin, Child in the crib, Joseph, the star, the ox and ass+ g.poly([[-r * 0.8, -r * 0.9], [r * 0.8, -r * 0.9], [r * 0.8, -r * 0.62], [-r * 0.8, -r * 0.62]], 'brown', 1.5);+ gFigure(g, -r * 0.42, -r * 0.62, r * 1.05, { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'out' });+ gFigure(g, r * 0.56, -r * 0.62, r * 1.1, { robe: 'tan', mantle: 'emerald', beard: 1, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'staff', flip: 1, halo: 'yellow' });+ g.poly([[-r * 0.1, -r * 0.62], [r * 0.3, -r * 0.62], [r * 0.34, -r * 0.34], [-r * 0.14, -r * 0.34]], 'amber', 2, { came: 0.014 });+ g.poly([[-r * 0.07, -r * 0.34], [r * 0.27, -r * 0.34], [r * 0.27, -r * 0.22], [-r * 0.07, -r * 0.22]], 'white', 2.5, { came: 0.012 });+ g.disc(r * 0.02, -r * 0.24, r * 0.065, 'gold', 2.5, 8, { came: 0.008 }); g.disc(r * 0.02, -r * 0.24, r * 0.043, 'flesh', 3, 7, { came: 0.008 });+ star(g, 0, r * 0.68, r * 0.13, 'yellow', 3);+ g.poly([[-r * 0.4, r * 0.22], [0, r * 0.52], [r * 0.4, r * 0.22], [r * 0.36, r * 0.14], [0, r * 0.4], [-r * 0.36, r * 0.14]], 'bone', 1.5);+ },+ magi(g, r) { // Three crowned kings bring gifts to the enthroned Virgin+ gFigure(g, r * 0.56, -r * 0.8, r * 1.2, { pose: 'sit', flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'hold' });+ g.disc(r * 0.4, -r * 0.1, r * 0.06, 'gold', 4, 8, { came: 0.008 });+ gFigure(g, -r * 0.05, -r * 0.8, r * 1.18, { pose: 'kneel', robe: 'emerald', mantle: 'ruby', crown: 1, beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'point', halo: false });+ gFigure(g, -r * 0.42, -r * 0.8, r * 1.32, { robe: 'violet', mantle: 'gold', crown: 1, beard: 1, arm: 'point', halo: false });+ gFigure(g, -r * 0.74, -r * 0.8, r * 1.3, { robe: 'teal', mantle: 'rubyLt', crown: 1, arm: 'hold', halo: false });+ star(g, -r * 0.1, r * 0.72, r * 0.11, 'yellow', 3);+ },+ crucifixion(g, r) { // Christ on the cross, the Virgin and Saint John, sun and moon+ g.poly([[-0.09 * r, -r * 0.92], [0.09 * r, -r * 0.92], [0.09 * r, r * 0.76], [-0.09 * r, r * 0.76]], 'emerald', 1.5, { came: 0.016 });+ g.poly([[-r * 0.58, r * 0.28], [r * 0.58, r * 0.28], [r * 0.58, r * 0.45], [-r * 0.58, r * 0.45]], 'emerald', 1.5, { came: 0.016 });+ gFigure(g, 0, -r * 0.84, r * 1.42, { robe: 'white', mantle: 'white', cross: 1, arm: 'out', halo: 'gold', hair: 'hair', beard: 1, sway: 0 });+ gFigure(g, -r * 0.62, -r * 0.88, r * 1.1, { robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray' });+ gFigure(g, r * 0.62, -r * 0.88, r * 1.1, { flip: 1, robe: 'white', mantle: 'rubyLt', arm: 'hold', attr: 'book' });+ g.disc(-r * 0.68, r * 0.66, r * 0.11, 'gold', 2, 8); g.poly([[r * 0.59, r * 0.74], [r * 0.76, r * 0.6], [r * 0.71, r * 0.76]], 'pearl', 2);+ },+ resurrection(g, r) { // Christ rises from the tomb holding the banner; soldiers sleep below+ g.poly([[-r * 0.72, -r * 0.84], [r * 0.72, -r * 0.84], [r * 0.72, -r * 0.42], [-r * 0.72, -r * 0.42]], 'bone', 2);+ g.line([[-r * 0.72, -r * 0.56], [r * 0.72, -r * 0.56]], 0.02, 'lead', 3); [-0.4, 0, 0.4].forEach((k) => g.disc(r * k, -r * 0.68, r * 0.09, 'purple', 3, 8, { came: 0.01 }));+ gFigure(g, 0, -r * 0.42, r * 1.25, { robe: 'white', mantle: 'rubyLt', cross: 1, arm: 'up', hair: 'hair', beard: 1, halo: 'gold' });+ g.poly([[r * 0.34, r * 0.72], [r * 0.34, r * 0.92], [r * 0.7, r * 0.84], [r * 0.34, r * 0.72]], 'white', 3, { came: 0.01 }); g.line([[r * 0.34, r * 0.92], [r * 0.34, -r * 0.1]], 0.02, 'gold', 3.2);+ for (const s of [-1, 1]) { g.disc(s * r * 0.82, -r * 0.5, r * 0.07, 'flesh', 3, 7, { came: 0.01 }); g.poly([[s * r * 0.6, -r * 0.86], [s * r * 0.92, -r * 0.86], [s * r * 0.9, -r * 0.54], [s * r * 0.7, -r * 0.54]], s > 0 ? 'emerald' : 'cobalt', 2.6); }+ },+ supper(g, r) { // The Last Supper: Christ at the centre of a laid table+ [-0.62, -0.31, 0.31, 0.62].forEach((k, i) => gBust(g, r * k, -r * 0.05, r * 0.92, { robe: ['emerald', 'ruby', 'amber', 'violet'][i], beard: i % 2 ? 1 : 0, hair: i === 3 ? 'grey' : 'hair' }));+ gBust(g, 0, -r * 0.02, r * 1.04, { robe: 'rubyLt', beard: 1, cross: 1 });+ g.poly([[-r * 0.92, -r * 0.5], [r * 0.92, -r * 0.5], [r * 0.92, -r * 0.2], [-r * 0.92, -r * 0.2]], 'white', 4);+ g.poly([[-r * 0.86, -r * 0.5], [r * 0.86, -r * 0.5], [r * 0.82, -r * 0.92], [-r * 0.82, -r * 0.92]], 'brown', 3.6);+ [-0.58, -0.2, 0.2, 0.58].forEach((k) => g.disc(r * k, -r * 0.3, r * 0.055, 'gold', 4.5, 6, { came: 0.008 }));+ },+ noah(g, r) { // The Ark on the waters, the dove returning+ [-0.82, -0.56].forEach((v, i) => g.poly([[-r, r * v], [r, r * v], [r, r * (v + 0.22)], [-r, r * (v + 0.18)]], i ? 'azure' : 'cobalt', 2));+ g.poly([[-r * 0.7, -r * 0.4], [r * 0.7, -r * 0.4], [r * 0.5, -r * 0.7], [-r * 0.5, -r * 0.7]], 'brown', 2.5);+ g.poly([[-r * 0.46, -r * 0.4], [r * 0.46, -r * 0.4], [r * 0.46, r * 0.02], [-r * 0.46, r * 0.02]], 'tan', 3);+ g.poly([[-r * 0.56, r * 0.0], [0, r * 0.32], [r * 0.56, r * 0.0]], 'ruby', 3.2);+ [-0.2, 0.2].forEach((k) => g.disc(r * k, -r * 0.18, r * 0.1, 'night', 3.4, 7, { came: 0.012 }));+ gBust(g, r * 0.2, -r * 0.3, r * 0.42, { robe: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', layer: 3.6 });+ g.disc(-r * 0.54, r * 0.64, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.72, r * 0.72], [-r * 0.54, r * 0.62], [-r * 0.36, r * 0.74], [-r * 0.54, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ shepherd(g, r) { // The Good Shepherd carries the lamb; a flock in green pasture+ g.poly([[-r, -r], [r, -r], [r, -r * 0.5], [-r, -r * 0.62]], 'green', 1.5);+ gFigure(g, 0, -r * 0.82, r * 1.5, { robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'hold', attr: 'staff' });+ g.disc(0, r * 0.26, r * 0.11, 'white', 4.5, 8, { came: 0.01 }); g.disc(r * 0.13, r * 0.3, r * 0.05, 'pearl', 4.8, 6, { came: 0.008 });+ for (const [x, y] of [[-0.68, -0.64], [0.66, -0.62], [-0.46, -0.78]]) { g.disc(r * x, r * y, r * 0.11, 'white', 3, 7, { came: 0.01 }); g.disc(r * x + r * 0.12, r * y + r * 0.05, r * 0.05, 'grey', 3.2, 5, { came: 0.008 }); }+ },+ david(g, r) { // King David with his harp+ gFigure(g, -r * 0.14, -r * 0.82, r * 1.42, { pose: 'sit', robe: 'violet', mantle: 'ruby', crown: 1, beard: 1, arm: 'out', halo: false });+ g.poly([[r * 0.26, -r * 0.6], [r * 0.7, r * 0.2], [r * 0.7, r * 0.46], [r * 0.16, -r * 0.36]], 'gold', 3, { came: 0.012 });+ g.line([[r * 0.2, -r * 0.4], [r * 0.2, r * 0.4]], 0.018, 'gold', 3.1); [0.3, 0.4, 0.5, 0.6].forEach((k) => g.line([[r * k, -r * 0.42 + r * (k - 0.2)], [r * k, r * 0.34]], 0.008, 'white', 3.2));+ g.poly([[-r * 0.86, -r * 0.84], [r * 0.5, -r * 0.84], [r * 0.5, -r * 0.7], [-r * 0.86, -r * 0.7]], 'tan', 1.8);+ },+};+function star(g, x, y, r, c, layer) { const p = []; for (let i = 0; i < 16; i++) { const a = i * PI / 8 + PI / 2, q = i % 2 ? r * 0.38 : r * (i % 4 ? 0.7 : 1); p.push([x + q * Math.cos(a), y + q * Math.sin(a)]); } g.poly(p, c, layer, { came: 0.01 }); }+function gBust(g, x, y, h, o = {}) { // head and shoulders, the head drawn by the painter+ const L = o.layer || 2, bx = [x - h * 0.22, y - h * 0.32, x + h * 0.22, y + h * 0.46], t = DRAPE_OF[o.robe || 'ruby'] || 'ruby';+ if (o.halo !== false) g.disc(x, y + h * 0.58, h * 0.17, o.halo || 'gold', L - 0.1, 10);+ g.poly([[x - h * 0.2, y - h * 0.3], [x + h * 0.2, y - h * 0.3], [x + h * 0.17, y + h * 0.3], [x + h * 0.06, y + h * 0.42], [x - h * 0.06, y + h * 0.42], [x - h * 0.17, y + h * 0.3]], o.robe || 'ruby', L, { tile: t, box: bx });+ const kind = o.face || (o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ g.face(x, y + h * 0.58, h * 0.108, h * 0.132, kind, L + 0.5, false);+ if (o.crown) g.poly([[x - h * 0.1, y + h * 0.66], [x + h * 0.1, y + h * 0.66], [x + h * 0.12, y + h * 0.78], [x + h * 0.05, y + h * 0.72], [x, y + h * 0.8], [x - h * 0.05, y + h * 0.72], [x - h * 0.12, y + h * 0.78]], 'gold', L + 0.7, { came: 0.008 });+ if (o.cross) for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[x + h * 0.17 * Math.cos(a - 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a - 0.24)], [x + h * 0.17 * Math.cos(a + 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a + 0.24)], [x, y + h * 0.58]], 'ruby', L + 0.2, { came: 0.008 }); }+}+// A jewelled ring of short leaded segments+function gRim(g, cx, cy, r0, r1, cols, n, layer) {+ g.disc(cx, cy, r1 + 0.02, 'lead', layer - 0.3, Math.max(10, n), { came: 0, jitter: 0 });+ for (let i = 0; i < n; i++) {+ const a = i * TAU / n, b = (i + 1) * TAU / n;+ g.poly([[cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)], [cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)], [cx + r1 * Math.cos(b), cy + r1 * Math.sin(b)], [cx + r0 * Math.cos(b), cy + r0 * Math.sin(b)]], cols[i % cols.length], layer, { came: -0.01 });+ }+}+// A lancet told as a stack of medallions over a diapered ground, with a jewelled border+function gLancetStory(g, x, w, y0, ys, rise, scenes, o = {}) {+ const c1 = o.c1 || 'ruby', c2 = o.c2 || 'cobalt', edge = o.edge || ['gold', 'ruby', 'white', 'cobalt'];+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse().slice(0)];+ g.withClip(outline, () => {+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, o.cell || 0.6, c1, c2, 'gold');+ const H = ys + rise * 0.5 - y0, N = scenes.length, step = H / N, R = Math.min(w * 0.44, step * 0.47);+ scenes.forEach((nm, i) => {+ const cy = y0 + step * (i + 0.5);+ const ground = (i % 2) ? (o.g1 || 'cobalt') : (o.g2 || 'ruby');+ gRim(g, x, cy, R, R * 1.055, ['gold', 'ruby', 'gold', 'cobalt'], 22, 1);+ g.disc(x, cy, R, ground, 1.3, 18, { came: 0 });+ g.withClip(ringPts(x, cy, R, 14), () => g.withXf(0, x, cy, 1, () => SCENES[nm](g, R)));+ if (i < N - 1) { const yy = y0 + step * (i + 1); for (const s of [-1, 1]) { g.disc(x + s * w * 0.3, yy, w * 0.058, 'gold', 1.2, 8, { came: 0.012 }); g.disc(x + s * w * 0.3, yy, w * 0.032, 'ruby', 1.5, 6, { came: 0.008 }); } }+ });+ // Border bricks follow the outline: thin, and each brick painted with a stem of leaves+ const bw = w * 0.062, stepB = 0.34;+ const stripe = (x0, y1, y2) => { let k = 0; g.poly([[x0, y1], [x0 + bw, y1], [x0 + bw, y2], [x0, y2]], 'lead', 3.5, { came: 0, jitter: 0 }); for (let y = y1; y < y2; y += stepB) { const c = edge[k++ % edge.length]; g.poly([[x0, y], [x0 + bw, y], [x0 + bw, y + stepB], [x0, y + stepB]], c, 3.8, { came: -0.012, orn: c === 'gold' ? 'gold' : c === 'white' ? 'white' : null, box: [x0, y, x0 + bw, y + stepB] }); } };+ stripe(x - w / 2, y0, ys + rise * 0.1); stripe(x + w / 2 - bw, y0, ys + rise * 0.1);+ // Saddle bars (iron armature)+ for (let y = y0 + 1.1; y < ys + rise * 0.6; y += 1.1) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.045], [x - w / 2, y + 0.045]], 'lead', 7, { came: 0, jitter: 0 });+ });+}+// A lancet filled by one monumental standing figure under a painted canopy (reads from the floor)+function gLancetSaint(g, x, w, y0, ys, rise, fig, o = {}) {+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse()];+ const gnd = o.ground || 'cobalt';+ g.withClip(outline, () => {+ const figY = y0 + 1.05, figH = (ys + rise * 0.1 - figY) * 0.84;+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, 0.8, gnd, o.ground2 || 'ruby', null);+ // Pedestal: a band of painted gold the figure stands on+ g.poly([[x - w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 1.02], [x - w * 0.46, y0 + 1.02]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.46, y0 + 0.62, x + w * 0.46, y0 + 1.02] });+ // Canopy: a thin arch of gold, a gable with its ruby field, amber pinnacles either side+ const cy = figY + figH + 0.02, cH = ys + rise * 0.86 - cy;+ g.poly([[x - w * 0.48, cy], [x + w * 0.48, cy], [x + w * 0.48, cy + cH * 0.1], [x - w * 0.48, cy + cH * 0.1]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.48, cy, x + w * 0.48, cy + cH * 0.1] });+ g.poly([[x - w * 0.42, cy + cH * 0.08], [x + w * 0.42, cy + cH * 0.08], [x, cy + cH]], 'yellow', 1.7, { orn: 'gold', box: [x - w * 0.42, cy + cH * 0.08, x + w * 0.42, cy + cH] });+ g.poly([[x - w * 0.22, cy + cH * 0.14], [x + w * 0.22, cy + cH * 0.14], [x, cy + cH * 0.62]], 'ruby', 1.9, { came: 0.016 });+ for (const s of [-1, 1]) g.poly([[x + s * w * 0.3, cy + cH * 0.12], [x + s * w * 0.46, cy + cH * 0.12], [x + s * w * 0.38, cy + cH * 0.85]], 'amber', 1.8);+ gFigure(g, x, figY, figH, Object.assign({ layer: 2.5, sx: Math.min(1, w * 1.72 / figH) }, fig));+ for (let y = y0 + 1.5; y < ys + rise * 0.6; y += 1.5) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.05], [x - w / 2, y + 0.05]], 'lead', 9, { came: 0, jitter: 0 });+ });+}+function gOculus(g, cx, cy, r, o = {}) { // head of the window: a sexfoil with an angel or Christ+ g.withClip(ringPts(cx, cy, r, 20), () => {+ g.disc(cx, cy, r, o.ground || 'cobalt', 1, 20);+ for (let i = 0; i < 6; i++) { const a = i * PI / 3 + PI / 6; g.disc(cx + r * 0.68 * Math.cos(a), cy + r * 0.68 * Math.sin(a), r * 0.27, i % 2 ? 'ruby' : 'gold', 1.4, 8); }+ g.disc(cx, cy, r * 0.56, o.inner || 'rubyLt', 1.8, 14);+ g.withXf(0, cx, cy, 1, () => { if (o.bust) gBust(g, 0, -r * 0.45, r * 0.95, o.bust); else gFigure(g, 0, -r * 0.5, r * 0.98, { wings: 'gold', robe: 'white', mantle: 'emerald', arm: 'orant' }); });+ });+}++// A complete two-light window: lancets, oculus, base field. Local plane z=0, +Z = interior.+function makeWindowGlass(w, y0, ys, rise, seed, fill) {+ const g = new Glass(seed);+ const outline = archOutline(w, y0, ys, rise, 8);+ g.raw(outline.slice(), 'lead', -0.4, 0);+ g.withClip(outline, () => gDiaper(g, -w / 2, ys, w / 2, ys + rise, 0.55, 'cobalt', 'ruby', 'gold', 0));+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96;+ const syl = ys + rise * 0.02;+ fill.light(g, -lx, lw, y0, syl, lr, 0); fill.light(g, lx, lw, y0, syl, lr, 1);+ const oc = windowHead(w, ys, rise);+ gOculus(g, 0, oc.y, oc.r + 0.03, fill.oculus || {});+ return g.b;+}+function windowHead(w, ys, rise) { const r = w * 0.225; return { r, y: ys + rise * 0.62 }; }++// ==================================================================================+// STONE TRACERY for a two-light window (plane z=0, bars deep along Z)+// ==================================================================================+function addTracery(b, w, y0, ys, rise, o = {}) {+ const bw = o.bar || 0.26, bd = o.depth || 0.42, sec = traceryBar(bw, bd), secT = hexBar(bw * 0.62, bd * 0.8);+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96, syl = ys + rise * 0.02;+ // Central mullion with shaft ring and capital+ b.at(M.T(0, 0, 0), () => {+ b.sweep([[0, y0 - 0.05], [0, syl]], sec, {});+ b.at(M.T(0, syl, 0), () => b.lathe([[bw * 0.52, -0.3], [bw * 0.56, -0.2], [bw * 0.9, 0.02], [bw * 0.5, 0.06]], 6));+ });+ for (const s of [-1, 1]) {+ const arc = pointedArc(lw, lr, 5).map((p) => [s * lx + p[0], syl + p[1]]);+ b.sweep(arc, sec, {});+ // cusped trefoil head inside each lancet+ const cr = lw * 0.3, cy = syl + lr * 0.34;+ const cusp = []; for (let i = 0; i <= 8; i++) { const a = rad(-30 + i * 240 / 8); const R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([s * lx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, { caps: false });+ b.sweep([[s * (lx + lw / 2), cy - cr * 0.4], cusp[0]], secT, { caps: false });+ b.sweep([[s * (lx - lw / 2), cy - cr * 0.4], cusp[8]], secT, { caps: false });+ }+ // Oculus with six foils+ const oc = windowHead(w, ys, rise);+ b.sweep(ringPts(0, oc.y, oc.r, 14), sec, { closed: true });+ const foils = []; for (let i = 0; i < 18; i++) { const a = i * TAU / 18; const R = oc.r * (0.62 + 0.3 * Math.pow(Math.abs(Math.cos(a * 3)), 0.8)); foils.push([R * Math.cos(a), oc.y + R * Math.sin(a)]); }+ b.sweep(foils, secT, { closed: true, crease: 40 });+}++// ==================================================================================+// PIERCED PARAPET with quatrefoil panels+// ==================================================================================+function quatrefoil(cx, cy, r, n = 16) { const p = []; for (let i = 0; i < n; i++) { const a = i * TAU / n; const R = r * (0.72 + 0.28 * Math.abs(Math.cos(2 * a))); p.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); } return p; }+function addParapet(b, x0, x1, y, h, th, o = {}) {+ const n = Math.max(1, Math.round((x1 - x0) / (o.pitch || 1.05))), holes = [];+ const r = Math.min(h * 0.34, (x1 - x0) / n * 0.4);+ for (let i = 0; i < n; i++) holes.push(quatrefoil(x0 + (i + 0.5) * (x1 - x0) / n, y + h * 0.5, r, o.nh || 12));+ b.slab([[x0, y], [x1, y], [x1, y + h], [x0, y + h]], holes, -th / 2, th / 2, { sr: 0.6 });+ const cap = barSection(0.14, th + 0.14);+ b.sweep([[x0, y + h + 0.04], [x1, y + h + 0.04]], cap, {});+ b.sweep([[x0, y - 0.02], [x1, y - 0.02]], barSection(0.1, th + 0.1), { s: 0.8 });+}++// A wimperg: gable over a window with crockets, finial and a blind trefoil+function addGable(b, w, h, z0, th, y, o = {}) {+ const lod = o.lod === undefined ? 1 : o.lod;+ const holes = o.solid ? [] : [ringPts(0, y + h * 0.36, Math.min(w * 0.13, h * 0.2), 14)];+ if (!o.noField) b.slab([[-w / 2, y], [w / 2, y], [0, y + h]], holes, z0, z0 + th, { sf: 1, sb: 0.8 });+ const rake = barSection(0.18, th + 0.14);+ b.sweep([[-w / 2 - 0.06, y - 0.02], [0, y + h + 0.05]], rake, { z: z0 + th / 2 });+ b.sweep([[0, y + h + 0.05], [w / 2 + 0.06, y - 0.02]], rake, { z: z0 + th / 2 });+ const n = o.crockets === undefined ? Math.max(2, Math.round(Math.hypot(w / 2, h) / 1.05)) : o.crockets, sz = o.size || 0.46;+ const zc = z0 + th / 2;+ crocketsAlong(b, [-w / 2, y + 0.05, zc], [-0.03, y + h * 0.97, zc], n, sz, [-h, w / 2, 0], lod);+ crocketsAlong(b, [w / 2, y + 0.05, zc], [0.03, y + h * 0.97, zc], n, sz, [h, w / 2, 0], lod);+ b.at(M.T(0, y + h * 0.98, zc), () => addFinial(b, o.finial || 0.95, lod));+}+// A small blind trefoil-headed arcade on a wall face (plane z=0, moulding stands proud +Z)+function addBlindArcade(b, x0, x1, y0, ys, n, o = {}) {+ const w = (x1 - x0) / n, sec = hexBar(o.bar || 0.12, o.depth || 0.16), rise = w * 0.95;+ for (let i = 0; i < n; i++) {+ const x = x0 + (i + 0.5) * w;+ b.sweep(pointedArc(w * 0.9, rise, 4).map((p) => [x + p[0], ys + p[1]]), sec, { z: 0.04, caps: false });+ }+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * w;+ b.at(M.T(x, 0, 0.06), () => b.lathe([[0.1, y0], [0.065, y0 + 0.18], [0.065, ys - 0.14], [0.13, ys], [0.08, ys + 0.04]], 5));+ }+}++// ==================================================================================+// RIBBED VAULT: pointed-arc ribs, curved webs, carved or gilded boss.+// ==================================================================================+function arcH(L, H, f) { // height of a two-centred arc of span L, rise H at fraction f+ const c = (H * H - L * L / 4) / L, r = c + L / 2, d = Math.abs(f - 0.5) * L;+ return Math.sqrt(Math.max(0, r * r - (d + c) * (d + c)));+}+function addRib(b, A, B, yS, rise, sec, n = 8, half = false) { // rib between plan points A,B in their vertical plane+ const dx = B[0] - A[0], dz = B[1] - A[1], L = Math.hypot(dx, dz), ang = Math.atan2(dz, dx) * 180 / PI;+ const pts = [];+ if (half) for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L * 2, rise, t / 2)]); }+ else for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L, rise, t)]); }+ // plane: local X along A->B, local Y up+ b.at(M.of(M.T(A[0], 0, A[1]), M.Ry(-ang)), () => b.sweep(pts, sec, { caps: false }));+}+// One curved web between a side arc a-b and the crown c, faces downward whichever way the plan runs+function addVaultCell(A, a, b, c, yS, yC, sideRiseV, nS = 8, nT = 6) {+ const La = Math.hypot(c[0] - a[0], c[1] - a[1]), Lb = Math.hypot(c[0] - b[0], c[1] - b[1]), Lab = Math.hypot(b[0] - a[0], b[1] - a[1]), rise = yC - yS;+ const cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ A.grid((s, t) => {+ const px = lerp(a[0], c[0], t) * (1 - s) + lerp(b[0], c[0], t) * s, pz = lerp(a[1], c[1], t) * (1 - s) + lerp(b[1], c[1], t) * s;+ const ruled = lerp(yS + arcH(Lab, sideRiseV, s), yC, t);+ const dA = arcH(La * 2, rise, t / 2) - rise * t, dB = arcH(Lb * 2, rise, t / 2) - rise * t;+ const y = ruled + (1 - s) * (1 - s) * dA + s * s * dB;+ return [px, y, pz, 0.72 + 0.28 * t];+ }, nS, nT, { flip: cross < 0, metric: true });+}+function addRibVault(F, A, corners, yS, yC, o = {}) {+ // corners: 4 plan points in order; web cells between each side and the crown+ const c0 = corners, cx = (c0[0][0] + c0[1][0] + c0[2][0] + c0[3][0]) / 4, cz = (c0[0][1] + c0[1][1] + c0[2][1] + c0[3][1]) / 4;+ const crown = [cx, yC, cz], nS = o.nS || 8, nT = o.nT || 6, rise = yC - yS;+ const sideRise = o.sideRise || ((i) => rise * 0.94);+ for (let k = 0; k < 4; k++) {+ if (o.skipSide && o.skipSide[k]) continue;+ addVaultCell(F, c0[k], c0[(k + 1) % 4], [cx, cz], yS, yC, sideRise(k), nS, nT);+ }+ const sec = o.ribSec || rollSection(0.32, 0.42, 2, 0.04, 2);+ for (const c of c0) addRib(F, c, [cx, cz], yS, rise, sec, 8, true);+ for (let k = 0; k < 4; k++) if (!(o.skipRib && o.skipRib[k])) addRib(F, c0[k], c0[(k + 1) % 4], yS, sideRiseRib(o, k, rise), o.sideRibSec || sec, 8);+ return crown;+}+const sideRiseRib = (o, k, rise) => (o.sideRise ? o.sideRise(k) : rise * 0.94);+function addBoss(G, F, c, r = 0.42) { // carved foliate boss with gilding+ G.at(M.T(c[0], c[1] - 0.02, c[2]), () => {+ G.lathe([[r * 0.25, -r * 0.54], [r * 0.6, -r * 0.5], [r * 0.95, -r * 0.3], [r, -r * 0.08], [r * 0.7, r * 0.05]], 10);+ for (let i = 0; i < 8; i++) addLeaf(G, M.of(M.T(0, -r * 0.22, 0), M.Ry(i * 45), M.T(r * 0.72, 0, 0), M.Rz(160)), r * 0.72);+ });+}++// ==================================================================================+// CLUSTERED PIER: a lobed section of shafts around a core, with bases and foliate capitals+// ==================================================================================+function lobedRing(core, shafts, n = 64) {+ const out = [];+ for (let i = 0; i < n; i++) {+ const a = TAU * i / n, c = Math.cos(a), s = Math.sin(a);+ let best = core, nx = c, nz = s;+ for (const sh of shafts) {+ // ray from the origin against each shaft circle+ const b = c * sh[0] + s * sh[1], disc = b * b - (sh[0] * sh[0] + sh[1] * sh[1] - sh[2] * sh[2]);+ if (disc >= 0) { const t = b + Math.sqrt(disc); if (t > best) { best = t; nx = (t * c - sh[0]) / sh[2]; nz = (t * s - sh[1]) / sh[2]; } }+ }+ out.push([best * c, best * s, nx, nz]);+ }+ return out;+}+function addExtrudedRing(b, ring, y0, y1, o = {}) {+ const s0 = o.s0 === undefined ? 1 : o.s0, s1 = o.s1 === undefined ? 1 : o.s1, k0 = o.k0 || 1, k1 = o.k1 || 1, N = ring.length;+ const base = b.count; let perim = 0;+ for (let i = 0; i <= N; i++) {+ const r = ring[i % N], rp = ring[(i + N - 1) % N];+ if (i > 0) perim += Math.hypot(r[0] - rp[0], r[1] - rp[1]);+ const shade = 0.5 + 0.5 * Math.pow(Math.abs(r[2] * r[0] + r[3] * r[1]) / (Math.hypot(r[0], r[1]) || 1), 2);+ b.v(r[0] * k0, y0, r[1] * k0, r[2], 0, r[3], perim, y0, s0 * shade);+ b.v(r[0] * k1, y1, r[1] * k1, r[2], 0, r[3], perim, y1, s1 * shade);+ }+ for (let i = 0; i < N; i++) b.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+}+function addCapital(b, ring, y, h, k, o = {}) { // bell with crocket leaves on each shaft, moulded abacus+ const N = ring.length;+ addExtrudedRing(b, ring, y, y + h * 0.1, { k0: 1.0, k1: 1.03, s0: 0.8 });+ addExtrudedRing(b, ring, y + h * 0.1, y + h * 0.82, { k0: 1.03, k1: k, s0: 0.62, s1: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.82, y + h * 0.9, { k0: k, k1: k + 0.1, s0: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.9, y + h, { k0: k + 0.1, k1: k + 0.12, s0: 0.95 });+ const top = ring.map((r) => [r[0] * (k + 0.12), r[1] * (k + 0.12)]);+ const ids = top.map((p) => b.v(p[0], y + h, p[1], 0, 1, 0, undefined, undefined, 0.9)); for (let i = 1; i < N - 1; i++) b.tri(ids[0], ids[i + 1], ids[i]);+ const leaves = o.leaves || 12;+ for (let i = 0; i < leaves; i++) {+ const a = i * 360 / leaves + (o.phase || 0), idx = Math.round(i / leaves * N) % N, r = ring[idx], R = Math.hypot(r[0], r[1]);+ addLeaf(b, M.of(M.T(0, y + h * 0.1, 0), M.Ry(-a), M.T(R * 1.02, 0, 0)), h * (i % 2 ? 0.72 : 0.56), { coarse: 1 });+ }+}++// The bay plan shared by every straight module (+Z side of the axis)+const zn = P.naveSpan / 2, zaw = zn + P.aisleSpan, BAY = P.bay;+// Window openings follow the bay, so changing the bay length re-proportions the glass and tracery+const WIN = {+ aW: BAY - 1.95, aSill: 4.3, aSp: P.pierTop - 1, aRise: 5.6 * (BAY - 1.95) / 4.65,+ cW: BAY - 1.55, cSill: P.triforiumTop + 0.45, cSp: P.vaultSpring + 4.8, cRise: 6.7 * (BAY - 1.55) / 5.05,+};+const windowArgs = { aisle: [WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise], clere: [WIN.cW, WIN.cSill, WIN.cSp, WIN.cRise] };+const mkBatches = () => { const D = new Batch(1); D.noColor = true; return { A: new Batch(TILE.ashlar), F: new Batch(TILE.fine), L: new Batch(TILE.lead), G: new Batch(TILE.gold), O: new Batch(TILE.oak), I: new Batch(TILE.iron), D }; };+// A stain hanging from a ledge on a wall facing local +z: runoff, drips, soft shadow or rising damp. Few vertices, crisp detail.+const STAIN_RNG = mkRng(4242);+function addStain(D, x0, x1, y, z, drop, kind = 'streak') {+ const R = DECAL_UV[kind], n = Math.max(1, Math.round((x1 - x0) / 4)), w = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = lerp(x0, x1, i / n), c = lerp(x0, x1, (i + 1) / n), span = w * Math.min(1, (c - a) / 5), u0 = R[0] + 0.002 + STAIN_RNG() * (w - span - 0.004);+ const q = [D.v(a, y, z, 0, 0, 1, u0, R[1], 1), D.v(a, y - drop, z, 0, 0, 1, u0, R[3], 1), D.v(c, y - drop, z, 0, 0, 1, u0 + span, R[3], 1), D.v(c, y, z, 0, 0, 1, u0 + span, R[1], 1)];+ D.quad(q[0], q[1], q[2], q[3]);+ }+}+// A real recess in a wall facing local +z, whose slab already has the opening cut: back plate, moulded frame, a sill+function addRecess(A, F, outline, depth) {+ const o = area2(outline) > 0 ? outline : outline.slice().reverse();+ let x0 = 1e9, x1 = -1e9, y0 = 1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); }+ A.face(o.map((p) => [p[0], p[1], -depth]));+ F.sweep(o, rollSection(0.1, 0.22, 1, 0.03, 2), { closed: true, crease: 40, z: -0.02 });+ F.sweep([[x0 - 0.16, y0 - 0.06], [x1 + 0.16, y0 - 0.06]], stringSection(0.16, 0.14), {});+}+// A real buttress on a wall facing local +z. stages, bottom to top: { y0, y1, w: half width, f: how far its front stands out }.+// Each stage narrows and steps back, and ends in a sloped stone cap running up to the next: a solid wedge of the pier, never+// a sheet. o.back ties it into the masonry behind; the last stage ends in a weathering back to the wall, or flat with o.flat.+function addPier(set, stages, o = {}) {+ const { A, F } = set, zb = -(o.back === undefined ? 0.3 : o.back);+ const face = (m, hw, y0, y1, p) => {+ const out = [[-hw, y0], [hw, y0], [hw, y1], [-hw, y1]];+ if (!p) { A.at(m, () => A.face(out.map((q) => [q[0], q[1], 0]))); return; }+ const hole = archOutline(hw * 2 * p.k, y0 + p.b, y1 - p.t - p.r, p.r, 5);+ A.at(m, () => { A.slab(out, [hole], -0.2, 0, { noOuterSides: true, noBack: true, sr: 0.66 }); F.at(m, () => addRecess(A, F, hole, 0.2)); });+ };+ stages.forEach((s, i) => {+ const n = stages[i + 1], mid = (s.f + zb) / 2, half = (s.f - zb) / 2;+ face(M.T(0, 0, s.f), s.w, s.y0, s.y1, s.panel);+ face(M.of(M.T(s.w, 0, mid), M.Ry(90)), half, s.y0, s.y1, s.sidePanel);+ face(M.of(M.T(-s.w, 0, mid), M.Ry(-90)), half, s.y0, s.y1, s.sidePanel);+ if (s.backFace) A.face([[s.w, s.y0, zb], [-s.w, s.y0, zb], [-s.w, s.y1, zb], [s.w, s.y1, zb]]);+ if (!s.noDrip) F.box(-s.w - 0.05, s.w + 0.05, s.y1 - 0.14, s.y1, zb, s.f + 0.05, { noTop: true });+ if (!n && o.flat) { F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [s.w, s.y1, zb], [-s.w, s.y1, zb]]); return; }+ const ny = n ? n.y0 : s.y1 + (o.capH || (s.f - zb) * 0.9), nw = n ? n.w : s.w, nf = n ? n.f : zb;+ F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [nw, ny, nf], [-nw, ny, nf]]);+ F.face([[s.w, s.y1, s.f], [s.w, s.y1, zb], [nw, ny, zb], [nw, ny, nf]]);+ F.face([[-s.w, s.y1, zb], [-s.w, s.y1, s.f], [-nw, ny, nf], [-nw, ny, zb]]);+ });+}+function placeSet(name, set, pos, rot, parent, extra = {}) {+ const o = { pos, rot: [0, rot, 0], parent, scale: extra.scale };+ place(name + '_Ashlar', set.A, MAT.ashlar, o);+ place(name + '_Stone', set.F, MAT.fine, o);+ place(name + '_Lead', set.L, MAT.lead, o);+ place(name + '_Gilt', set.G, MAT.gold, o);+ place(name + '_Oak', set.O, MAT.oak, o);+ place(name + '_Iron', set.I, MAT.iron, o);+ if (set.D) place(name + '_Weathering', set.D, MAT.decal, o);+}+// Glass pair: interior face + the same panes mirrored as the exterior face+function placeGlass(name, batch, pos, rotY, parent, keepOutside, k = 1) {+ place(name + '_In', batch, keepOutside ? MAT.glassRose : MAT.glassIn, { pos, rot: [0, rotY, 0], scale: [k, k, 1], parent });+ place(name + '_Out', keepOutside ? batch : glassOutside(batch), keepOutside ? MAT.glassOut : MAT.glassOutWin, { pos, rot: [0, rotY, 0], scale: [k, k, -1], parent });+}+// By day a window is dark from outside and the story cannot be read, so its outer skin is only the+// window's outline over a mosaic of pot-metals: the colour hints through for a few triangles.+function glassOutside(batch) {+ if (batch.outer) return batch.outer;+ if (!batch.outline) return batch;+ const g = new Glass(7), o = batch.outline;+ let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); }+ g.raw(o, 'lead', -0.4, 0);+ g.withClip(o, () => gDiaper(g, x0, y0, x1, y1, 0, 'outer', null, null, 0));+ batch.outer = g.b;+ return g.b;+}++function buildNaveBay(o = {}) {+ const set = o.into || mkBatches(), { A, F, L, G } = set;+ const B = o.B || BAY, ww = o.ww || BAY - 2.1, sp = P.pierTop, archRise = o.archRise || 5.1, thick = 0.85;+ const zw = o.zw === undefined ? zn : o.zw;+ if (o.part !== 'aisle') {+ // ---- Arcade wall with the moulded pointed arch ----+ const arc = pointedArc(ww, archRise, 7).map((p) => [p[0], p[1] + sp]);+ A.at(M.T(0, 0, zw), () => {+ A.slab([[-B / 2, sp], [-ww / 2, sp], ...arc, [ww / 2, sp], [B / 2, sp], [B / 2, P.triforiumSill], [-B / 2, P.triforiumSill]], [], -thick / 2, thick / 2, { noOuterSides: true });+ });+ F.at(M.T(0, 0, zw), () => { F.sweep(arc, rollSection(0.82, thick + 0.25, 2, 0.08, 3), {}); });+ // Hood mould over the arch on the nave side, with carved label stops+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ const hood = pointedArc(ww + 1.9, archRise + 1.0, 6).map((p) => [p[0], p[1] + sp]);+ F.sweep(hood, stringSection(0.16, 0.14), {});+ });+ // ---- Triforium: string course, four cusped arches, dark wall passage ----+ const yT = P.triforiumSill, yT2 = P.triforiumTop, nT = o.nT || 4, tw = o.tw || (BAY - 1.72) / 4, ty = yT + 0.62, tspr = yT2 - 1.5, gapT = nT > 2 ? 0.08 : 0;+ const trfX = (i) => (i + 0.5 - nT / 2) * tw + (i < nT / 2 ? -gapT : gapT);+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ F.sweep([[-B / 2, yT], [B / 2, yT]], stringSection(0.34, 0.3), {});+ F.sweep([[-B / 2, yT2], [B / 2, yT2]], stringSection(0.3, 0.26), {});+ // Colonnette arcade standing in the openings+ for (let i = 0; i < nT; i++) {+ const x = trfX(i);+ const a = pointedArc(tw * 0.92, 0.98, 4).map((p) => [x + p[0], tspr + p[1]]);+ F.sweep(a, hexBar(0.13, 0.26), { z: 0.05 });+ F.sweep(ringPts(x, tspr + 0.32, 0.17, 6), hexBar(0.05, 0.1), { z: 0.05, closed: true, s: 0.9 });+ }+ for (let i = 0; i <= nT; i++) {+ const mid = i === nT / 2;+ const x = (i - nT / 2) * tw + (i < nT / 2 ? -gapT : (mid ? 0 : gapT));+ const r = mid ? 0.14 : 0.085;+ F.at(M.T(x, 0, 0.05), () => F.lathe([[r * 1.7, ty - 0.02], [r, ty + 0.2], [r, tspr - 0.2], [r * 1.9, tspr - 0.01], [r * 1.5, tspr + 0.03]], 6));+ }+ });+ const holes = []; // openings through the inner skin+ for (let i = 0; i < nT; i++) { const x = trfX(i); holes.push(archOutline(tw * 0.92, ty, tspr, 0.98, 6).map((p) => [x + p[0], p[1]])); }+ A.at(M.T(0, 0, zw - thick / 2 + 0.22), () => A.slab([[-B / 2, yT], [B / 2, yT], [B / 2, yT2], [-B / 2, yT2]], holes, -0.22, 0.22, { noOuterSides: true, sf: 0.45, sr: 0.5 }));+ // Passage back wall and floor: seen only through the arcade, kept dark+ // Round the apse the passage fans out with the radius, so the walls of neighbouring bays meet at their corners+ const zBk = zw + 0.92, zFr = zw - thick / 2 + 0.44, hB = o.wedge ? (B / 2) * zBk / zw : B / 2, hF = o.wedge ? (B / 2) * zFr / zw : B / 2;+ A.shade(0.34, () => {+ A.face([[hB, yT, zBk], [-hB, yT, zBk], [-hB, yT2 + 0.5, zBk], [hB, yT2 + 0.5, zBk]]);+ A.face([[-hB, yT + 0.02, zBk], [hB, yT + 0.02, zBk], [hF, yT + 0.02, zFr], [-hF, yT + 0.02, zFr]]);+ A.face([[-hF, yT2 + 0.2, zFr], [hF, yT2 + 0.2, zFr], [hB, yT2 + 0.2, zBk], [-hB, yT2 + 0.2, zBk]]);+ });+ // ---- Clerestory wall, window hole, tracery ----+ const cw = o.cw || WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = o.cRise || WIN.cRise;+ const yE = P.eaves;+ A.at(M.T(0, 0, zw), () => A.slab([[-B / 2, yT2], [B / 2, yT2], [B / 2, yE], [-B / 2, yE]], [archOutline(cw, cSill, cSp, cRise, 9)], -0.42, 0.42, { noOuterSides: true, sr: 0.66 }));+ F.at(M.T(0, 0, zw), () => {+ if (o.lights === 1) addLancetTracery(F, cw, cSill, cSp, cRise);+ else addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.44 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 9).slice(0), rollSection(0.14, 0.86, 1, 0.03, 2), { closed: true, s: 0.92, crease: 40 });+ });+ // Exterior: hood mould, wimperg gable, cornice, pierced parapet, corbel table+ set.D.at(M.T(0, 0, zw + 0.42), () => { addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.02, 3.6); addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.03, 0.9, 'soft'); });+ F.at(M.T(0, 0, zw + 0.42), () => {+ F.sweep(pointedArc(cw + 0.5, cRise + 0.3, 6).map((p) => [p[0], p[1] + cSp]), hexBar(0.22, 0.26), {});+ addGable(F, Math.min(cw + 0.9, B * 0.96), 3.4, 0.02, 0.2, cSp + cRise * 0.52, { size: 0.44, finial: 0.8, lod: 0, crockets: 3 });+ F.sweep([[-B / 2, yT2 - 0.02], [B / 2, yT2 - 0.02]], stringSection(0.3, 0.2), {});+ F.sweep([[-B / 2, yE - 0.12], [B / 2, yE - 0.12]], stringSection(0.34, 0.38), {});+ const nCorb = Math.round(B / 1.1);+ for (let i = 0; i < nCorb; i++) { const x = -B / 2 + (i + 0.5) * B / nCorb; F.box(x - 0.13, x + 0.13, yE - 0.62, yE - 0.32, 0, 0.26, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zw + 0.3), () => addParapet(F, -B / 2, B / 2, yE, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Upper wall top closing the gutter+ A.face([[-B / 2, yE, zw + 0.42], [B / 2, yE, zw + 0.42], [B / 2, yE, zw - 0.42], [-B / 2, yE, zw - 0.42]], 0.7);+ }+ if (o.part !== 'upper') {+ // ---- Aisle roof (lean-to in lead) ----+ if (!o.noAisleRoof) {+ const z0 = zn + 0.42, z1 = zaw + 1.05, y0r = P.aisleRoofHigh, y1r = P.aisleWallTop - 0.2;+ L.face([[-B / 2, y0r, z0], [-B / 2, y1r, z1], [B / 2, y1r, z1], [B / 2, y0r, z0]]);+ }+ if (!o.open) {+ // ---- Aisle wall ----+ const wa = WIN.aW, sill = WIN.aSill, aSp = WIN.aSp, aRise = WIN.aRise, zOut = zaw + 0.45;+ A.at(M.T(0, 0, zOut), () => A.slab([[-B / 2, 0], [B / 2, 0], [B / 2, P.aisleWallTop], [-B / 2, P.aisleWallTop]], [archOutline(wa, sill, aSp, aRise, 9)], -0.45, 0.45, { noOuterSides: true, sr: 0.7 }));+ F.at(M.T(0, 0, zOut), () => {+ addTracery(F, wa, sill, aSp, aRise, { bar: 0.25, depth: 0.42 });+ F.sweep(archOutline(wa, sill, aSp, aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ });+ // Exterior of the aisle wall+ set.D.at(M.T(0, 0, zaw + 0.9), () => {+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.02, 4.4);+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.02, 2.6, 'drip');+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.03, 0.7, 'soft');+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.03, 0.9, 'soft');+ addStain(set.D, -B / 2, B / 2, 5.2, 0.03, 5.2, 'ground');+ });+ F.at(M.T(0, 0, zaw + 0.9), () => {+ F.sweep([[-B / 2, sill - 0.2], [B / 2, sill - 0.2]], stringSection(0.34, 0.3), {});+ F.sweep(pointedArc(wa + 0.46, aRise + 0.28, 6).map((p) => [p[0], p[1] + aSp]), hexBar(0.22, 0.26), {});+ addGable(F, wa + 0.8, 3.1, 0.02, 0.2, aSp + 2.95, { size: 0.44, finial: 0.8, crockets: 4 });+ F.sweep([[-B / 2, P.aisleWallTop - 0.12], [B / 2, P.aisleWallTop - 0.12]], stringSection(0.32, 0.34), {});+ // Plinth with a chamfered weathering+ F.sweep([[-B / 2, 0.55], [B / 2, 0.55]], stringSection(0.4, 0.42), {});+ F.sweep([[-B / 2, 1.3], [B / 2, 1.3]], stringSection(0.2, 0.2), {});+ addBlindArcade(F, -B / 2 + 0.9, B / 2 - 0.9, 1.45, 2.65, 4, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(0, 0, zaw + 0.72), () => addParapet(F, -B / 2, B / 2, P.aisleWallTop, 1.0, 0.24, { pitch: 1.3 }));+ A.face([[-B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw], [-B / 2, P.aisleWallTop, zaw]], 0.7);+ // Interior dado: blind arcade with a niche for a statue+ F.at(M.of(M.T(0, 0, zaw), M.Ry(180)), () => {+ F.sweep([[-B / 2, sill - 0.14], [B / 2, sill - 0.14]], stringSection(0.26, 0.22), {});+ addBlindArcade(F, -B / 2 + 0.5, -1.05, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ addBlindArcade(F, 1.05, B / 2 - 0.5, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ F.sweep([[-B / 2, 0.42], [B / 2, 0.42]], stringSection(0.4, 0.34), {});+ // Niche canopy over the statue+ addNicheFrame(F, 0, 0.6, 3.9, 1.05);+ });+ }+ // ---- Aisle vault ----+ if (!o.noAisleVault) {+ const z0 = zn + thick / 2, z1 = zaw;+ const crown = addRibVault(F, A, [[-B / 2, z0], [B / 2, z0], [B / 2, z1], [-B / 2, z1]], sp, P.aisleCrown - 0.3, { sideRise: (k) => (k % 2 === 0 ? (k === 0 ? archRise + 0.1 : 5.0) : 5.2), nS: 6, nT: 4, ribSec: rollSection(0.26, 0.34, 2, 0.03, 2) });+ addBoss(G, F, crown, 0.32);+ }+ }+ return set;+}+// Canopied niche frame: moulded opening, gablet with crockets. Statue placed separately.+function addNicheFrame(F, x, y0, yTop, w) {+ F.at(M.T(x, 0, 0), () => {+ F.shade(0.5, () => F.face([[-w / 2, y0, -0.005], [w / 2, y0, -0.005], [w / 2, yTop - 0.75, -0.005], [0, yTop - 0.1, -0.005], [-w / 2, yTop - 0.75, -0.005]]));+ F.box(-w / 2 - 0.05, w / 2 + 0.05, y0 - 0.14, y0 + 0.08, 0, 0.5, { s: 0.92 });+ F.sweep(archOutline(w, y0, yTop - 0.82, 0.72, 5).slice(2), barSection(0.11, 0.2), { z: 0.08 });+ for (const s of [-1, 1]) { F.at(M.T(s * w / 2, 0, 0.1), () => F.lathe([[0.07, y0 + 0.05], [0.05, y0 + 0.12], [0.05, yTop - 0.9], [0.09, yTop - 0.82], [0.05, yTop - 0.78]], 6)); }+ addGable(F, w + 0.24, 0.92, 0.06, 0.1, yTop - 0.98, { size: 0.19, crockets: 2, finial: 0.32, solid: true, lod: 0 });+ });+}++// Clustered nave pier with the vault shafts rising to the springing, on +Z = nave... shafts toward -Z+function buildPier() {+ const set = mkBatches(), { F, A, G } = set;+ const core = 0.8, big = 0.25, small = 0.14;+ const shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.1) * Math.cos(a), (core + 0.1) * Math.sin(a), big]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; shafts.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 48);+ const yB = 1.15, yC = P.pierTop - 1.05;+ // moulded base: octagonal plinth, roll, scotia, then the lobed shaft+ F.lathe([[1.7, 0], [1.7, 0.56], [1.58, 0.62]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.62, 0.86, { k0: 1.24, k1: 1.27 });+ addExtrudedRing(F, ring, 0.86, yB, { k0: 1.27, k1: 1.0, s0: 0.75, s1: 0.72 });+ addExtrudedRing(F, ring, yB, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.05, 1.2, { leaves: 14 });+ // The vault shafts rising from the capital up the nave wall (toward -Z)+ const upper = [[0, -(core + 0.1), big], [-0.5, -(core - 0.04), small], [0.5, -(core - 0.04), small]];+ for (const u of upper) {+ F.at(M.T(u[0], 0, u[1] + 0.44), () => {+ const r = u[2], yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, P.triforiumSill - 0.1], [r * 1.5, P.triforiumSill + 0.06], [r, P.triforiumSill + 0.22], [r, yS - 0.85], [r * 1.5, yS - 0.8], [r * 1.14, yS - 0.7], [r * 2.2, yS]], 8);+ for (let i = 0; i < 4; i++) addLeaf(F, M.of(M.T(0, yS - 0.72, 0), M.Ry(i * 90 + 45), M.T(r * 1.1, 0, 0)), 0.56, { coarse: 1 });+ });+ }+ // Statue stage on the nave face: corbel below, canopy above (the figure is placed separately)+ const zF = -(core + 0.34);+ F.at(M.T(0, 0, zF), () => {+ F.lathe([[0.1, 5.0], [0.28, 5.25], [0.44, 5.6], [0.5, 5.72], [0.44, 5.78]], 8, { s: 0.85 });+ for (let i = 0; i < 6; i++) addLeaf(F, M.of(M.T(0, 5.04, 0), M.Ry(i * 60), M.T(0.14, 0, 0)), 0.66, { coarse: 1 });+ // Canopy over the statue: three arched gablets and a little spire+ F.at(M.T(0, 8.25, 0), () => {+ F.lathe([[0.5, -0.02], [0.54, 0.06], [0.42, 0.12], [0.3, 0.6], [0.02, 2.0]], 6, { faceted: true, phase: PI / 6 });+ for (let i = 0; i < 3; i++) F.at(M.of(M.Ry(-60 + i * 60 + 180), M.T(0, 0, 0.44)), () => addGable(F, 0.62, 0.62, -0.03, 0.06, 0.02, { size: 0.15, crockets: 1, finial: 0.2, solid: true, lod: 0 }));+ });+ });+ return set;+}+// The hemicycle's pier: the nave pier's family, slimmer, with one vault shaft running up to the springing+function buildApsePier() {+ const set = mkBatches(), { F } = set, core = 0.5, big = 0.15, small = 0.09, shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.06) * Math.cos(a), (core + 0.06) * Math.sin(a), big]); }+ for (let i = 0; i < 4; i++) { const a = PI / 4 + i * PI / 2; shafts.push([(core + 0.01) * Math.cos(a), (core + 0.01) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 32), yB = 0.86, yC = P.pierTop - 0.82;+ F.lathe([[1.02, 0], [1.02, 0.46], [0.94, 0.52]], 8, { faceted: true, phase: PI / 8 });+ addExtrudedRing(F, ring, 0.52, yB, { k0: 1.26, k1: 1.0 });+ addExtrudedRing(F, ring, yB, yC, {});+ addCapital(F, ring, yC, 0.82, 1.22, { leaves: 10 });+ F.at(M.T(0, 0, -(core + 0.06) + 0.24), () => {+ const r = big, yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, yS - 0.5], [r * 1.5, yS - 0.42], [r * 2.1, yS]], 8);+ });+ return set;+}+// Flying buttress unit at a bay boundary: massive pier, two arched flyers, tabernacle and pinnacles+const BUTTRESS = { hw: 0.98, zB: 19.2, zStack: 15.3, zT: 16.3 };+const flyerRise = (yA, yB, t) => yA + (yB - yA) * lerp(t, Math.sin(t * PI / 2), 0.6);+function flyerShape(zA, yA, zB, yB, thick) { // A at the pier (outer), B at the wall (inner); returns polygon points in (z,y)+ const N = 12, under = [], top = [];+ for (let i = 0; i <= N; i++) {+ const t = i / N, z = lerp(zA, zB, t), y = flyerRise(yA, yB, t);+ under.push([z, y]);+ }+ // The coping is a straight line clear above the arch: deep over the pier, close over the wall, the lights open between+ const topA = [zA - 0.05, yA + 2.5], topB = [zB, yB + 0.72];+ return { under, topA, topB };+}+function buildButtress() {+ const set = mkBatches(), { A, F, G } = set;+ const { hw, zB, zStack, zT } = BUTTRESS, z0 = zaw + 0.9, yStack = 30.6;+ // A solid pier tied into the aisle wall: three stages narrowing and stepping back under sloped caps, each with a real+ // recessed panel, then a slimmer stack at the rear that rises above the aisle roof to take the flyers+ const lower = { k: 0.56, b: 1.5, t: 1.6, r: 0.8 }, flank = { k: 0.56, b: 1.5, t: 1.5, r: 1.1 };+ A.at(M.T(0, 0, z0), () => F.at(M.T(0, 0, z0), () => addPier(set, [+ { y0: 0, y1: 8.4, w: hw, f: zB - z0, panel: lower, sidePanel: flank },+ { y0: 9.6, y1: 15.8, w: hw - 0.08, f: zB - z0 - 0.9, sidePanel: flank },+ { y0: 17, y1: 23.4, w: hw - 0.14, f: zT + 1 - z0, noDrip: true },+ { y0: 23.4, y1: yStack, w: hw - 0.24, f: zStack - z0, backFace: true },+ ], { capH: 1.1 })));+ // Moulded plinth around the base: a closed block the pier rises out of+ F.at(M.T(0, 0, z0), () => { F.box(-hw - 0.16, hw + 0.16, 0, 0.52, -0.2, zB - z0 + 0.16, { noBot: true, s: 0.9 }); F.box(-hw - 0.08, hw + 0.08, 0.52, 0.86, -0.2, zB - z0 + 0.08, { noBot: true, noTop: true, taper: 0.06, taperZ: 0.06 }); });+ set.D.at(M.T(0, 0, zB), () => { addStain(set.D, -hw, hw, 8.2, 0.02, 3.2); addStain(set.D, -hw, hw, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -hw, hw, 5, 0.03, 4.4, 'ground'); });+ // Rain and shadow down both flanks of the pier, below each cap+ for (const s of [-1, 1]) {+ const d1 = zB - z0, d2 = d1 - 0.9;+ set.D.at(M.of(M.T(s * hw, 0, z0 + d1 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.02, 3.2); addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -d1 / 2, d1 / 2, 5, 0.03, 4.8, 'ground'); });+ set.D.at(M.of(M.T(s * (hw - 0.08), 0, z0 + d2 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.02, 2.8); addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.03, 0.7, 'soft'); });+ }+ F.at(M.T(0, 0, zB), () => addGable(F, hw * 1.7, 1.4, 0.02, 0.14, 8.0, { size: 0.3, crockets: 2, finial: 0.5, solid: true, lod: 0 }));+ // Tabernacle of colonnettes at the top of the pier holding an angel+ const yT0 = 23.4, yT1 = 28.4;+ F.box(-hw + 0.1, hw - 0.1, yT0 - 0.02, yT0 + 0.1, zStack - 0.02, zT + 1, { s: 0.95 });+ for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) F.at(M.T(sx * 0.72, 0, zT + sz * 0.72), () => F.lathe([[0.2, yT0], [0.14, yT0 + 0.12], [0.1, yT0 + 0.2], [0.1, yT1 - 0.22], [0.14, yT1 - 0.15], [0.22, yT1]], 8));+ // Canopy: arched, gabled and crowned by the crocketed pinnacle+ for (let i = 0; i < 4; i++) F.at(M.of(M.T(0, 0, zT), M.Ry(i * 90), M.T(0, 0, 0.72)), () => {+ F.sweep(pointedArc(1.3, 1.25, 6).map((p) => [p[0], yT1 - 1.25 + p[1] - 0.02]), traceryBar(0.1, 0.16), {});+ addGable(F, 1.7, 1.25, -0.06, 0.12, yT1, { size: 0.28, crockets: 2, finial: 0.48, lod: 0, solid: true });+ });+ F.box(-0.92, 0.92, yT1 - 0.06, yT1 + 0.18, zT - 0.92, zT + 0.92, { s: 0.9 });+ F.at(M.T(0, yT1 + 0.18, zT), () => addPinnacle(F, 1.42, 8.4, { lod: 1 }));+ // Two flyers, each a true arch: a moulded rib springing from the pier, a thin plate pierced by a row of+ // pointed lights standing on it, and a straight sloped coping with a gabled top shedding the rain+ const fly = (zA, yA, zB, yB, th) => {+ const { under, topA, topB } = flyerShape(zA, yA, zB, yB, th);+ const band = 0.34, upper = under.map((p) => [p[0], p[1] + band]);+ const onTop = (z) => lerp(topA[1], topB[1], (z - topA[0]) / (topB[0] - topA[0]));+ const onRib = (t) => flyerRise(yA, yB, t) + band;+ const holes = [], N = 6;+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.16) / N, t1 = (i + 0.84) / N, z0 = lerp(zA, zB, t0), z1 = lerp(zA, zB, t1), u0 = onRib(t0) + 0.04, u1 = onRib(t1) + 0.04, a0 = onTop(z0) - 0.1, a1 = onTop(z1) - 0.1, h = Math.min(a0 - u0, a1 - u1);+ if (h < 0.22) continue;+ const r = Math.min(h * 0.42, Math.abs(z1 - z0) * 0.8), hole = [[z0, u0], [z1, u1], [z1, a1 - r], [(z0 + z1) / 2, (a0 + a1) / 2], [z0, a0 - r]];+ holes.push(area2(hole) > 0 ? hole : hole.slice().reverse());+ }+ F.at(M.Ry(-90), () => {+ F.slab([...upper, topB, topA], holes, -0.12, 0.12, { sr: 0.7, so: 0.8 });+ F.slab([...under, ...upper.slice().reverse()], [], -0.27, 0.27, {});+ F.sweep(under, rollSection(0.12, 0.34, 1, 0.03, 2), { caps: false });+ });+ const ye = -0.03, yr = 0.17, we = 0.33, a = [topA[1], topA[0]], c = [topB[1], topB[0]];+ F.face([[-we, a[0] + ye, a[1]], [0, a[0] + yr, a[1]], [0, c[0] + yr, c[1]], [-we, c[0] + ye, c[1]]]);+ F.face([[0, a[0] + yr, a[1]], [we, a[0] + ye, a[1]], [we, c[0] + ye, c[1]], [0, c[0] + yr, c[1]]]);+ F.face([[-we, c[0] + ye, c[1]], [we, c[0] + ye, c[1]], [we, a[0] + ye, a[1]], [-we, a[0] + ye, a[1]]]);+ const n = Math.round(Math.hypot(topA[0] - topB[0], topA[1] - topB[1]) / 1.4);+ crocketsAlong(F, [0, topA[1] + 0.14, topA[0]], [0, topB[1] + 0.14, topB[0]], n, 0.5, [0, 1, 0], 0);+ };+ fly(zStack - 0.1, 28.0, zn + 1.12, 35.6, 0.48);+ fly(zStack - 0.1, 22.0, zn + 1.12, 28.6, 0.54);+ // Upper pier on the clerestory wall where the flyers land, with its own pinnacle above the parapet+ const yE = P.eaves;+ A.at(M.T(0, 0, zn + 0.42), () => F.at(M.T(0, 0, zn + 0.42), () => addPier(set, [{ y0: P.aisleRoofHigh - 1, y1: yE + 1.2, w: 0.62, f: 0.72, noDrip: true }], { back: 0, capH: 0.75 })));+ F.at(M.T(0, yE + 1.2, zn + 0.74), () => addPinnacle(F, 0.9, 6.4, { simple: 1 }));+ F.at(M.T(0, 0, zn + 1.14), () => { F.lathe([[0.16, 31.2], [0.1, 31.4], [0.1, 40.5], [0.17, 40.8]], 8); });+ // Interior respond on the aisle wall: a small cluster carrying the aisle vault+ const ringR = lobedRing(0.32, [[0, -0.44, 0.17], [-0.34, -0.26, 0.11], [0.34, -0.26, 0.11]], 26);+ F.at(M.T(0, 0, zaw), () => {+ addExtrudedRing(F, ringR, 0.4, P.pierTop - 0.8, { s0: 0.8 });+ addCapital(F, ringR, P.pierTop - 0.8, 0.8, 1.2, { leaves: 6 });+ F.lathe([[0.62, 0], [0.62, 0.42], [0.42, 0.5]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ });+ // Spout from the aisle gutter+ return set;+}++function addLancetTracery(b, w, y0, ys, rise) { // single-light frame with a cusped head+ const secT = traceryBar(0.13, 0.3), cr = w * 0.32, cy = ys + rise * 0.34, cusp = [];+ for (let i = 0; i <= 12; i++) { const a = rad(-30 + i * 240 / 12), R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, {});+ b.sweep([[w / 2, cy - cr * 0.4], cusp[0]], secT, {});+ b.sweep([[-w / 2, cy - cr * 0.4], cusp[12]], secT, {});+}+function makeLancetGlass(w, y0, ys, rise, seed, fig, o) {+ const g = new Glass(seed);+ g.raw(archOutline(w, y0, ys, rise, 8), 'lead', -0.4, 0);+ gLancetSaint(g, 0, w, y0, ys, rise, fig, o || {});+ return g.b;+}++// ==================================================================================+// PLAN: everything is derived from the parameters at the top of the file.+// ==================================================================================+const LAY = (() => {+ const xW = P.xWest, xTowerE = xW - P.towerDepth, xNaveE = xTowerE - P.naveBays * BAY, xc = xNaveE - zn, xChoirW = xc - zn, xa = xChoirW - P.choirBays * BAY;+ const ridge = P.eaves + (zn + 1) * P.roofPitch;+ return { xW, xTowerE, xNaveE, xc, xChoirW, xa, ridge };+})();+// A rigid module frame: rotation about Y at an origin on the ground+const frame = (th, ox, oz) => {+ const c = Math.cos(rad(th)), s = Math.sin(rad(th));+ return { th, ox, oz, pos: (x, y, z) => [ox + x * c + z * s, y, oz - x * s + z * c], rot: (d = 0) => [0, th + d, 0] };+};+const SAINTS = ['apostle', 'peter', 'paul', 'bishop', 'prophet', 'martyr', 'paul', 'apostle', 'bishop', 'prophet'];++// Baked floor light: dim towards the walls and the corners++function buildVaultNave() {+ const s = mkBatches();+ const crown = addRibVault(s.F, s.A, [[-BAY / 2, -zn + 0.42], [BAY / 2, -zn + 0.42], [BAY / 2, zn - 0.42], [-BAY / 2, zn - 0.42]], P.vaultSpring, P.vaultCrown, { sideRise: (k) => (P.vaultCrown - P.vaultSpring) * (k % 2 === 0 ? 0.86 : 0.99), nS: 10, nT: 8 });+ addBoss(s.G, s.F, crown, 0.55);+ return s;+}++// ---- Chevet: hemicycle, ambulatory and a radiating chapel per sector ----------------+const CHEV = (() => {+ const d = 180 / P.apseSides, h = rad(d / 2), Ra = zn * Math.cos(h), hs = zn * Math.sin(h), Ro = zaw * Math.cos(h), hso = zaw * Math.sin(h);+ const Rc = 2.5, zc = Ro + 3.0;+ return { d, h, Ra, hs, Ro, hso, Rc, zc };+})();+function buildChevetSector() {+ const set = mkBatches(), { A, F, L, G } = set, { Ra, hs, Ro, hso, Rc, zc } = CHEV;+ buildNaveBay({ into: set, part: 'upper', B: 2 * hs, zw: Ra, wedge: true, ww: 1.62, archRise: 5.0, nT: 2, tw: 1.05, cw: 2.05, cRise: 4.3, lights: 1 });+ // Hemicycle vault: one web from this side up to the crown over the apse centre+ const yS = P.vaultSpring, yC = P.vaultCrown;+ addVaultCell(F, [-hs + 0.0, Ra - 0.42], [hs, Ra - 0.42], [0, 0], yS, yC, 9.6, 7, 8);+ const sec = rollSection(0.32, 0.42, 2, 0.04, 2);+ addRib(F, [-hs, Ra - 0.42], [hs, Ra - 0.42], yS, 9.6, sec, 6);+ addRib(F, [-zn * Math.sin(CHEV.h), (zn - 0.42) * Math.cos(CHEV.h)], [0, 0], yS, yC - yS, sec, 9, true);+ // Ambulatory vault (trapezoid)+ const c2 = addRibVault(F, A, [[-hs, Ra + 0.42], [hs, Ra + 0.42], [hso, Ro], [-hso, Ro]], P.pierTop, P.aisleCrown - 0.3, { sideRise: (k) => (k === 0 ? 5.1 : k === 2 ? 5.2 : 5.1), nS: 6, nT: 4, ribSec: rollSection(0.24, 0.32, 2, 0.03, 2) });+ addBoss(G, F, c2, 0.3);+ // Outer wall with the arched chapel mouth+ const yW = P.aisleWallTop, mw = 3.55, mS = 8.6, mR = 4.8;+ const mouth = pointedArc(mw, mR, 8).map((p) => [p[0], p[1] + mS]);+ A.at(M.T(0, 0, Ro + 0.3), () => A.slab([[-hso, 0], [-mw / 2, 0], [-mw / 2, mS], ...mouth, [mw / 2, mS], [mw / 2, 0], [hso, 0], [hso, yW], [-hso, yW]], [], -0.3, 0.3, { noOuterSides: true, sr: 0.7 }));+ // The moulding fills the whole wall thickness: it is also the soffit of the opening+ F.at(M.T(0, 0, Ro + 0.3), () => F.sweep(mouth, rollSection(0.42, 0.96, 2, 0.05, 2), {}));+ set.D.at(M.T(0, 0, Ro + 0.6), () => { addStain(set.D, -hso, hso, yW - 0.42, 0.02, 3.4); addStain(set.D, -hso, hso, yW - 0.42, 0.03, 0.8, 'soft'); });+ F.at(M.T(0, 0, Ro + 0.6), () => { F.sweep([[-hso, yW - 0.12], [hso, yW - 0.12]], stringSection(0.3, 0.3), {}); addParapet(F, -hso, hso, yW, 1.0, 0.24, { pitch: 1.1 }); });+ A.face([[-hso, yW, Ro + 0.6], [hso, yW, Ro + 0.6], [hso, yW, Ro], [-hso, yW, Ro]], 0.7);+ // Ambulatory lean-to roof+ const zR0 = Ra + 0.42, hsR0 = (zR0 / Ra) * hs, zR1 = Ro + 0.9, hsR1 = (zR1 / Ro) * hso;+ L.face([[-hsR0, P.aisleRoofHigh, zR0], [-hsR1, yW - 0.2, zR1], [hsR1, yW - 0.2, zR1], [hsR0, P.aisleRoofHigh, zR0]]);+ // Hipped roof over the hemicycle+ const yE = P.eaves + 0.15, Re = zn + 0.72, hRe = Re * Math.sin(CHEV.h), aRe = Re * Math.cos(CHEV.h);+ L.grid((u, v) => [lerp(-hRe, hRe, u) * (1 - v), lerp(yE, LAY.ridge, v), aRe * (1 - v)], 2, 2, { flip: false, metric: true });+ // Radiating chapel: five faceted walls, each with a tall lancet, rib vault, conical lead roof+ const nF = P.chapelFacets, span = 216, yC1 = 16.2, dF = span / nF, ap = Rc * Math.cos(rad(dF / 2)), halfF = Rc * Math.sin(rad(dF / 2)), lw = 1.3, sill = 3.2, lSp = 9.9, lR = 2.5;+ const verts = [];+ for (let i = 0; i <= nF; i++) { const f = rad(-span / 2 + i * dF); verts.push([Rc * Math.sin(f), zc + Rc * Math.cos(f)]); }+ for (let i = 0; i < nF; i++) {+ const fm = -span / 2 + (i + 0.5) * dF;+ const m = M.of(M.T(0, 0, zc), M.Ry(fm), M.T(0, 0, ap));+ A.at(m, () => A.slab([[-halfF, 0], [halfF, 0], [halfF, yC1], [-halfF, yC1]], [archOutline(lw, sill, lSp, lR, 7)], 0, 0.5, { noOuterSides: true, sr: 0.68 }));+ F.at(m, () => {+ addLancetTracery(F, lw, sill, lSp, lR);+ F.sweep(archOutline(lw, sill, lSp, lR, 5), hexBar(0.12, 0.56), { closed: true, z: 0.25, s: 0.9, crease: 40 });+ });+ // Weather on the chapel walls: streaks and shadow under the cornice and the sill, damp at the foot+ set.D.at(M.of(m, M.T(0, 0, 0.5)), () => {+ addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.02, 3); addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.03, 0.7, 'soft');+ addStain(set.D, -halfF, halfF, sill - 0.3, 0.02, 2, 'drip'); addStain(set.D, -halfF, halfF, 3, 0.03, 3, 'ground');+ });+ F.at(M.of(m, M.T(0, 0, 0.5)), () => {+ F.sweep([[-halfF - 0.1, sill - 0.14], [halfF + 0.1, sill - 0.14]], stringSection(0.24, 0.2), { caps: false });+ F.sweep([[-halfF - 0.1, 0.5], [halfF + 0.1, 0.5]], stringSection(0.36, 0.3), { caps: false });+ F.sweep([[-halfF - 0.1, yC1 - 0.1], [halfF + 0.1, yC1 - 0.1]], stringSection(0.3, 0.3), { caps: false });+ addGable(F, lw + 0.9, 2.4, 0.01, 0.14, lSp + 2.15, { size: 0.32, crockets: 2, finial: 0.56, lod: 0 });+ addParapet(F, -halfF, halfF, yC1, 0.82, 0.18, { pitch: 1.3, nh: 8 });+ });+ addVaultCell(F, verts[i], verts[i + 1], [0, zc], 9.2, 14.6, 3.6, 5, 4);+ addRib(F, verts[i], verts[i + 1], 9.2, 3.6, rollSection(0.16, 0.22, 1, 0.02, 2), 5);+ }+ addVaultCell(F, verts[nF], verts[0], [0, zc], 9.2, 14.6, 4.2, 5, 4);+ for (let i = 0; i <= nF; i++) {+ addRib(F, verts[i], [0, zc], 9.2, 5.4, rollSection(0.17, 0.24, 1, 0.02, 2), 6, true);+ // Exterior angle buttress and little pinnacle; interior wall shaft+ const f = -span / 2 + i * dF, ve = verts[i];+ F.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => {+ A.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => addPier(set, [{ y0: 0, y1: 8.8, w: 0.3, f: 1.05 }, { y0: 9.7, y1: 14.2, w: 0.25, f: 0.72, noDrip: true }], { back: 0.1, flat: true })); // F is already in this frame+ F.at(M.T(0, 14.18, 0.62), () => addPinnacle(F, 0.62, 3.9, { simple: 1 }));+ F.lathe([[0.17, 0], [0.09, 0.6], [0.09, 9.1], [0.17, 9.2]], 5);+ });+ }+ // The neck joining the chapel to the ambulatory: side walls, a pointed barrel vault, a lead cap+ const zN0 = Ro + 0.6, zN1 = verts[0][1], hN0 = mw / 2, hN1 = Math.abs(verts[0][0]);+ for (const s of [-1, 1]) A.prism([[s * hN0, zN0 - 0.05], [s * hN1, zN1 + 0.05], [s * (hN1 + 0.5), zN1 + 0.05], [s * (hN0 + 0.5), zN0 - 0.05]], 0, yC1);+ // Same subdivision as the chapel's last web so the seam has no cracks; a rib covers the joint+ A.grid((u, v) => { const hw = lerp(hN0, hN1, v); return [lerp(-hw, hw, u), 9.2 + arcH(2 * hw, 4.2, u), lerp(Ro + 0.3, zN1 + 0.02, v), 0.8]; }, 5, 1, { flip: false }); // starts under the arch moulding, so no ray can pass above it+ addRib(F, verts[nF], verts[0], 9.2, 4.2, rollSection(0.16, 0.26, 1, 0.02, 2), 5);+ L.face([[-(hN0 + 0.5), yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN0 - 0.1], [-(hN0 + 0.5), yC1 + 0.03, zN0 - 0.1]]);+ // Lead cone+ L.at(M.T(0, 0, zc), () => { const n = 10, rb = Rc + 0.5; L.grid((u, v) => { const a = -PI + TAU * u, r = rb * (1 - v), y = lerp(yC1 + 0.05, yC1 + 7.4, v); return [r * Math.sin(a), y, r * Math.cos(a)]; }, n, 2, { flip: false, wrapU: true }); });+ G.at(M.T(0, yC1 + 7.3, zc), () => addFinial(G, 1.25, 1));+ return set;+}++// ---- Lead roofs with metric UVs so the seams run down the slope ----------------------+function addRoofRun(L, x0, x1, half, yE, yR) {+ const slope = Math.hypot(half, yR - yE);+ for (const s of [-1, 1]) {+ const a = L.v(x0, yE, s * half, 0, half, s * (yR - yE), x0, 0, 1), b = L.v(x1, yE, s * half, 0, half, s * (yR - yE), x1, 0, 1);+ const c = L.v(x1, yR, 0, 0, half, s * (yR - yE), x1, slope, 1), d = L.v(x0, yR, 0, 0, half, s * (yR - yE), x0, slope, 1);+ if (s > 0) L.quad(a, b, c, d); else L.quad(b, a, d, c);+ // A raised roll every second seam, running from the eaves to the ridge: a low ridge of two faces standing on the sheet+ const ny = half / slope, nz = s * (yR - yE) / slope, h = 0.08, w = 0.055;+ for (let x = Math.ceil(x0 / 2) * 2; x < x1; x += 2) {+ const A0 = [x - w, yE, s * half], B0 = [x + w, yE, s * half], A1 = [x - w, yR, 0], B1 = [x + w, yR, 0], T0 = [x, yE + ny * h, s * half + nz * h], T1 = [x, yR + ny * h, nz * h];+ if (s > 0) { L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]); } else { L.face([A1, T1, T0, A0]); L.face([T1, B1, B0, T0]); }+ }+ }+}+function buildCresting(len) { // gilded fleur-de-lis cresting for a length of ridge+ const G = new Batch(TILE.gold), n = Math.round(len / 0.9);+ G.box(-len / 2, len / 2, -0.04, 0.05, -0.05, 0.05, {});+ for (let i = 0; i < n; i++) {+ const x = -len / 2 + (i + 0.5) * len / n;+ G.at(M.T(x, 0.05, 0), () => {+ G.tube([[0, 0, 0], [0, 0.34, 0], [0, 0.62, 0]], (t) => [lerp(0.05, 0.005, t), lerp(0.035, 0.005, t)], 4);+ for (const s of [-1, 1]) G.tube(spline([[0, 0.14, 0], [s * 0.15, 0.26, 0], [s * 0.2, 0.42, 0], [s * 0.1, 0.46, 0]], 1), (t) => lerp(0.035, 0.008, t), 4);+ G.box(-0.08, 0.08, 0.12, 0.17, -0.03, 0.03, {});+ });+ }+ return G;+}++// ---- Crossing: four great piers, four arches, a gilded boss, the fleche above ----------+function buildCrossingPier() {+ const set = mkBatches(), { F } = set;+ const core = 1.08, sh = [];+ for (let i = 0; i < 8; i++) { const a = i * PI / 4; sh.push([(core + 0.12) * Math.cos(a), (core + 0.12) * Math.sin(a), i % 2 ? 0.2 : 0.3]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; sh.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), 0.13]); }+ const ring = lobedRing(core, sh, 80), yC = P.vaultSpring - 1.1;+ F.lathe([[2.05, 0], [2.05, 0.6], [1.9, 0.68]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.68, 0.94, { k0: 1.2, k1: 1.23 });+ addExtrudedRing(F, ring, 0.94, 1.25, { k0: 1.23, k1: 1, s0: 0.75, s1: 0.75 });+ addExtrudedRing(F, ring, 1.25, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.1, 1.14, { leaves: 16 });+ return set;+}+function buildAislePier() {+ const set = mkBatches(), { F } = set;+ const sh = []; for (let i = 0; i < 4; i++) { const a = i * PI / 2; sh.push([0.62 * Math.cos(a), 0.62 * Math.sin(a), 0.2]); }+ const ring = lobedRing(0.54, sh, 40);+ F.lathe([[1.15, 0], [1.15, 0.5], [1.02, 0.56]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.56, 0.86, { k0: 1.28, k1: 1, s0: 0.75 });+ addExtrudedRing(F, ring, 0.86, P.pierTop - 0.9, { s0: 0.8 });+ addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });+ return set;+}+// The spire over the crossing: an oak frame sheathed in lead. A plinth straddles the roofs, two open lantern stages stand on it,+// and a long octagonal needle rises to a gilded orb and cross. Everything follows P.flecheTip, the ridge and the width of the crossing.+function buildFleche(parent, x, z) {+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold);+ L.noReduce = true; // a single careful mesh, its seams and crockets are the design+ const n = 8, PH = PI / 8, cos8 = Math.cos(PH), sin8 = Math.sin(PH), oct = { faceted: true, phase: PH };+ const yR = LAY.ridge, tip = P.flecheTip, y0 = yR - 5.9, yP = yR + 2.7, H = tip - yP, yA = yP + 0.19 * H, yB = yA + 0.15 * H, yN1 = yB + 1.7, yT = tip - 3.3;+ const R0 = zn * 0.73, R1 = zn * 0.66, R2 = zn * 0.5, RN = zn * 0.44;+ const faceFrame = (k, ap) => M.of(M.Ry(k * 45), M.T(0, 0, ap));+ const vertex = (k, R) => { const a = rad((k + 0.5) * 45); return [R * Math.sin(a), R * Math.cos(a)]; };+ // A small gilded knop finishing a gablet or a lucarne+ const knop = (m, s) => G.at(m, () => G.lathe([[0.05 * s, 0], [0.11 * s, 0.1 * s], [0.09 * s, 0.2 * s], [0.03 * s, 0.32 * s], [0.004, 0.72 * s]], 6));+ const gablet = (k, ap, w, h, y, nC, sz) => {+ const fr = faceFrame(k, ap + 0.05);+ L.at(fr, () => {+ L.slab([[-w / 2, y], [w / 2, y], [0, y + h]], [], -0.05, 0.05, { sb: 0.7 });+ L.shade(0.42, () => L.face([[-w * 0.2, y + h * 0.16, 0.06], [w * 0.2, y + h * 0.16, 0.06], [0, y + h * 0.62, 0.06]]));+ const rake = barSection(0.14, 0.2);+ L.sweep([[-w / 2 - 0.05, y - 0.02], [0, y + h + 0.04]], rake, {}); L.sweep([[0, y + h + 0.04], [w / 2 + 0.05, y - 0.02]], rake, {});+ crocketsAlong(L, [-w / 2, y + 0.05, 0], [-0.03, y + h * 0.97, 0], nC, sz, [-h, w / 2, 0], 0, addLeadCrocket);+ crocketsAlong(L, [w / 2, y + 0.05, 0], [0.03, y + h * 0.97, 0], nC, sz, [h, w / 2, 0], 0, addLeadCrocket);+ });+ knop(M.of(fr, M.T(0, y + h - 0.02, 0)), 0.9);+ };+ // One open stage: a dark core, a pointed opening in every face, a clustered post at every angle, a cornice closing the top+ const stage = (R, yBase, yTop, coreR, rPost, open) => {+ const hs = R * sin8, ap = R * cos8, { w, sill, ys, rise, tracery } = open;+ L.shade(0.34, () => L.lathe([[coreR, yBase], [coreR, yTop - 0.4]], n, oct));+ L.shade(0.5, () => { L.lathe([[coreR, yTop - 0.4], [R, yTop - 0.4]], n, oct); L.lathe([[R, yBase + 0.01], [coreR, yBase + 0.01]], n, oct); });+ for (let k = 0; k < n; k++) {+ L.at(faceFrame(k, ap), () => {+ L.slab([[-hs, yBase], [hs, yBase], [hs, yTop], [-hs, yTop]], [archOutline(w, sill, ys, rise, 5)], -0.14, 0.02, { noOuterSides: true, sr: 0.6, sb: 0.5 });+ L.sweep(pointedArc(w + 0.16, rise + 0.16, 5).map((p) => [p[0], p[1] + ys]), hexBar(0.1, 0.14), { z: 0.03, caps: false });+ if (tracery) {+ L.at(M.T(0, 0, -0.06), () => L.lathe([[0.1, sill], [0.065, sill + 0.16], [0.065, ys - 0.08], [0.11, ys]], 5));+ for (const s of [-1, 1]) L.sweep(pointedArc(w / 2 - 0.03, rise * 0.58, 3).map((p) => [s * w / 4 + p[0], ys + p[1]]), hexBar(0.08, 0.13), { z: -0.06, caps: false });+ L.sweep(ringPts(0, ys + rise * 0.76, rise * 0.13, 6), hexBar(0.06, 0.1), { z: -0.06, closed: true });+ }+ });+ const [px, pz] = vertex(k, R + 0.03);+ L.at(M.T(px, 0, pz), () => L.lathe([[rPost * 1.35, yBase], [rPost, yBase + 0.22], [rPost, yTop - 0.3], [rPost * 1.35, yTop - 0.12], [rPost * 1.2, yTop]], 6));+ }+ L.lathe([[R + 0.02, yTop - 0.5], [R + 0.22, yTop - 0.3], [R + 0.22, yTop - 0.12], [R + 0.08, yTop]], n, oct);+ };+ // A raised roll standing on a sloping face, in the face's own frame: two faces, a low ridge+ const roll = (u, ya, yb, apF, h = 0.07, w = 0.05) => {+ const za = apF(ya), zb = apF(yb), dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz) || 1, ny = -dz / l, nz = dy / l;+ const A0 = [u - w, ya, za], B0 = [u + w, ya, za], A1 = [u - w, yb, zb], B1 = [u + w, yb, zb], T0 = [u, ya + ny * h, za + nz * h], T1 = [u, yb + ny * h, zb + nz * h];+ L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]);+ };++ // ---- The plinth: batters up out of the slates of all four roofs, its foot well below their surface all round ----+ L.lathe([[R0, y0], [R0 - 0.16, yP - 0.62], [R0 + 0.12, yP - 0.46], [R0 + 0.12, yP - 0.2], [R0 - 0.2, yP], [R1 * 0.6, yP]], n, oct);+ { const apF = (y) => lerp(R0, R0 - 0.16, (y - y0) / (yP - 0.62 - y0)) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => { for (const u of [-1.2, 0, 1.2]) roll(u, y0, yP - 0.62, apF); }); }++ // ---- First stage: tall two-light openings, a gablet on every face, a pinnacle at every angle, a lead pentice behind them ----+ stage(R1, yP, yA, R1 * 0.6, 0.26, { w: R1 * 0.56, sill: yP + 0.95, ys: yA - 3.35, rise: 2, tracery: true });+ L.lathe([[R1 + 0.08, yA], [R2 + 0.02, yA + 1.55]], n, oct);+ for (let k = 0; k < n; k++) {+ gablet(k, R1 * cos8, R1 * 0.67, 2.6, yA - 0.3, 2, 0.34);+ const [px, pz] = vertex(k, R1 + 0.02);+ L.at(M.of(M.T(px, yA - 0.2, pz), M.Ry((k + 0.5) * 45)), () => addPinnacle(L, 0.62, 5, { simple: true, lod: 0 }));+ }+ // ---- Second stage: slimmer, single lancets ----+ stage(R2, yA + 1.5, yB, R2 * 0.62, 0.18, { w: R2 * 0.53, sill: yA + 2.45, ys: yB - 2.3, rise: 1.5, tracery: false });+ for (let k = 0; k < n; k++) gablet(k, R2 * cos8, R2 * 0.65, 1.9, yB - 0.25, 1, 0.3);++ // ---- The needle: a flared foot, then eight long faces. u stays metric, so the sheets run straight up and are cut by the arrises as they close ----+ const rTip = 0.09, RF = R2 + 0.08, rAt = (y) => (y <= yN1 ? lerp(RF, RN, (y - yB) / (yN1 - yB)) : lerp(RN, rTip, (y - yN1) / (yT - yN1)));+ const levels = [yB, yN1]; for (const t of [0.22, 0.5, 0.75, 1]) levels.push(lerp(yN1, yT, t));+ const apAt = (y) => rAt(y) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => {+ let vv = 0;+ for (let j = 0; j < levels.length - 1; j++) {+ const ya = levels[j], yb = levels[j + 1], za = apAt(ya), zb = apAt(yb), wa = rAt(ya) * sin8, wb = rAt(yb) * sin8, dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz), ny = -dz / l, nz = dy / l;+ const a = L.v(-wa, ya, za, 0, ny, nz, -wa, vv), b = L.v(wa, ya, za, 0, ny, nz, wa, vv), c = L.v(wb, yb, zb, 0, ny, nz, wb, vv + l), d = L.v(-wb, yb, zb, 0, ny, nz, -wb, vv + l);+ L.quad(a, b, c, d); vv += l;+ }+ // Standing seams, each stopping where the closing arrises overtake it+ for (const u of [-0.55, 0, 0.55]) { const rEnd = (Math.abs(u) + 0.1) / sin8; if (rEnd < RN) roll(u, yN1, lerp(yN1, yT, (RN - rEnd) / (RN - rTip)), apAt); }+ });+ // Rolled arrises from the flare to the tip, crockets thinning as they climb+ for (let k = 0; k < n; k++) {+ const vAt = (y) => { const [vx, vz] = vertex(k, rAt(y)); return [vx, y, vz]; };+ L.tube([vAt(yB), vAt(yN1), vAt(yT - 0.05)], (t) => lerp(0.12, 0.04, t), 4, { capEnd: false });+ const [ox, oz] = vertex(k, 1), nC = 11, y1 = yN1 + 1.2, y2 = yT - 1.5;+ for (let i = 0; i < nC; i++) {+ const t = (i + 0.5) / nC, ya = lerp(y1, y2, t - 0.02), yb = lerp(y1, y2, t + 0.02);+ crocketsAlong(L, vAt(ya), vAt(yb), 1, lerp(1.2, 0.42, t), [ox, 0.12, oz], 0, addLeadCrocket);+ }+ }+ // Tie-bands round the needle where the sheets are lapped+ for (const t of [0.22, 0.5, 0.75]) { const y = lerp(yN1, yT, t), r = rAt(y); L.lathe([[rAt(y - 0.1), y - 0.1], [r + 0.08, y - 0.05], [r + 0.08, y + 0.05], [rAt(y + 0.12), y + 0.12]], n, oct); }+ // Two tiers of lucarnes on alternate faces+ const lucarne = (k, yD, a, c, g) => {+ const zf = apAt(yD) + 0.06, yE2 = yD + c, yG = yE2 + g, zb0 = apAt(yD), zbE = apAt(yE2), zbG = apAt(yG);+ L.at(faceFrame(k, 0), () => {+ L.face([[-a, yD, zf], [a, yD, zf], [a, yE2, zf], [0, yG, zf], [-a, yE2, zf]]);+ L.shade(0.22, () => L.face([[-a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yE2, zf + 0.012], [0, yE2 + g * 0.55, zf + 0.012], [-a * 0.5, yE2, zf + 0.012]]));+ L.face([[a, yD, zb0], [a, yE2, zbE], [a, yE2, zf], [a, yD, zf]]);+ L.face([[-a, yD, zb0], [-a, yD, zf], [-a, yE2, zf], [-a, yE2, zbE]]);+ L.face([[a, yE2, zf], [a, yE2, zbE], [0, yG, zbG], [0, yG, zf]]);+ L.face([[-a, yE2, zf], [0, yG, zf], [0, yG, zbG], [-a, yE2, zbE]]);+ });+ knop(M.of(faceFrame(k, zf), M.T(0, yG - 0.02, 0)), 0.8);+ };+ for (let k = 0; k < n; k++) { if (k % 2 === 0) lucarne(k, yN1 + 5, 0.5, 1.25, 0.75); else lucarne(k, yN1 + 12, 0.36, 0.95, 0.55); }++ // ---- The tip: a gilded collar, an orb and a cross, its head at exactly P.flecheTip ----+ G.at(M.T(0, yT - 0.25, 0), () => {+ G.lathe([[0.12, 0], [0.24, 0.12], [0.22, 0.22], [0.13, 0.3], [0.11, 0.44], [0.2, 0.52], [0.36, 0.68], [0.42, 0.9], [0.36, 1.12], [0.2, 1.28], [0.08, 1.36], [0.06, 2.2]], 10);+ G.box(-0.07, 0.07, 2.2, 3.55, -0.07, 0.07, {});+ G.box(-0.62, 0.62, 2.82, 2.98, -0.07, 0.07, {});+ });+ place('CrossingSpire_Lead', L, MAT.lead, { pos: [x, 0, z], parent });+ place('CrossingSpire_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });+}++// Coloured light thrown onto the paving by the south aisle windows (sun from the south-west)+// The light of a two-light window thrown across the floor. Colour and opacity live on the vertices of a fine grid, computed+// from smooth functions, so there is no texture edge anywhere: every change of colour is a gradient.+function buildLightPool(seed) {+ const sill = WIN.aSill, sp = WIN.aSp, rise = WIN.aRise, w = WIN.aW, top = sp + rise, nu = 18, nv = 40, r = mkRng(seed * 977 + 5);+ const PAL = [[0.72, 0.09, 0.1], [0.14, 0.2, 0.62], [0.78, 0.52, 0.14], [0.62, 0.08, 0.1], [0.18, 0.26, 0.68], [0.8, 0.58, 0.2]];+ const blobs = []; for (let i = 0; i < 12; i++) blobs.push({ u: (r() - 0.5) * w * 1.1, v: lerp(sill, top, r()), su: 0.7 + r() * 0.9, sv: 1.6 + r() * 2.2, c: PAL[Math.floor(r() * PAL.length)] });+ const P3 = [], C4 = [], N3 = [], UV = [], I = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) {+ const u = (i / nu - 0.5) * (w + 1.6), v = lerp(sill - 1, top + 0.8, j / nv);+ let cr = 0.05, cg = 0.04, cb = 0.05, tw = 0.1;+ for (const B of blobs) { const e = Math.exp(-((u - B.u) * (u - B.u)) / (B.su * B.su) - ((v - B.v) * (v - B.v)) / (B.sv * B.sv)); cr += B.c[0] * e; cg += B.c[1] * e; cb += B.c[2] * e; tw += e; }+ const hw = v <= sp ? w / 2 : (w / 2) * Math.sqrt(Math.max(0, 1 - ((v - sp) / rise) * ((v - sp) / rise))), edge = sstep(-0.1, 0.75, hw - Math.abs(u)) * sstep(sill - 0.4, sill + 0.9, v) * sstep(top + 0.4, top - 1, v), mull = 1 - 0.62 * Math.exp(-(u * u) / 0.09);+ P3.push(u + v * P.sunShear[0], 0, v * P.sunShear[1]); N3.push(0, 1, 0); UV.push(i / nu, j / nv);+ C4.push(Math.min(1, cr / tw * 1.5), Math.min(1, cg / tw * 1.5), Math.min(1, cb / tw * 1.5), 0.46 * edge * mull);+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) { const a = j * (nu + 1) + i, b = a + 1, c = a + nu + 2, d = a + nu + 1; I.push(a, d, c, a, c, b); }+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(P3), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(N3), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(UV), 2));+ g.setAttribute('color', new THREE.BufferAttribute(new Float32Array(C4), 4));+ g.setIndex(new THREE.BufferAttribute(new Uint16Array(I), 1));+ return g;+}++function storyWindows() { // the narrative aisle windows+ const sets = [+ [['annunciation', 'nativity', 'magi', 'shepherd', 'supper'], ['crucifixion', 'resurrection', 'david', 'noah', 'annunciation']],+ [['noah', 'david', 'annunciation', 'nativity', 'magi'], ['shepherd', 'supper', 'crucifixion', 'resurrection', 'david']],+ [['magi', 'supper', 'noah', 'crucifixion', 'shepherd'], ['nativity', 'david', 'resurrection', 'annunciation', 'supper']],+ [['resurrection', 'shepherd', 'crucifixion', 'david', 'nativity'], ['supper', 'annunciation', 'magi', 'noah', 'resurrection']],+ ];+ const grounds = [['ruby', 'cobalt'], ['cobalt', 'ruby'], ['ruby', 'cobalt'], ['cobalt', 'ruby']];+ return sets.slice(0, P.storyWindows).map((s, n) => makeWindowGlass(...windowArgs.aisle, 40 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetStory(g, x, w, y0, ys, r, s[i].slice(0, 4), { c1: grounds[n][0], c2: grounds[n][1], g1: grounds[(n + 1) % 4][1], g2: grounds[n][0], cell: 0.86 }),+ oculus: { ground: grounds[n][1], inner: grounds[n][0] },+ }));+}+const CLERE_SAINTS = [+ [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'book' }, { robe: 'emerald', mantle: 'gold', crown: 1, beard: 2, attr: 'cross', arm: 'point', halo: false }],+ [{ robe: 'pale', mantle: 'cobalt', veil: 'azure', arm: 'pray' }, { robe: 'violet', mantle: 'white', mitre: 'white', attr: 'staff', arm: 'up' }],+ [{ robe: 'gold', mantle: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'purple', beard: 1, attr: 'sword', arm: 'point' }],+ [{ robe: 'rubyLt', mantle: 'teal', crown: 1, attr: 'lily', arm: 'point', halo: false }, { robe: 'white', mantle: 'rubyLt', arm: 'orant', wings: 'gold' }],+];+function clereWindows() {+ return CLERE_SAINTS.slice(0, P.clerestoryWindows).map((pair, n) => makeWindowGlass(...windowArgs.clere, 60 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetSaint(g, x, w, y0, ys, r, pair[i], (i + n) % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }),+ oculus: { bust: { robe: 'rubyLt', beard: 1, cross: 1 }, ground: 'cobalt', inner: 'gold' },+ }));+}++function buildBody() {+ const { xTowerE, xNaveE, xc, xChoirW, xa, ridge } = LAY;+ const gNave = group('Nave'), gChoir = group('Choir'), gTN = group('Transept_North'), gTS = group('Transept_South'), gX = group('Crossing'), gApse = group('Apse_Chevet'), gRoof = group('Roofs');+ // The upper elevation is shared by every straight bay; aisles come in full and open-corner versions+ const bayUp = buildNaveBay({ part: 'upper' }), aisleFull = buildNaveBay({ part: 'aisle' }), aisleOpen = buildNaveBay({ part: 'aisle', open: true });+ const vault = buildVaultNave(), pier = buildPier(), but = buildButtress();+ const stories = storyWindows(), cleres = clereWindows();+ SHARED.stories = stories; SHARED.cleres = cleres;+ let counter = 0;+ const statue = (fr, lx, y, lz, kind, faceDeg, scale, parent, indoor = true) => placeFigure('Statue_' + kind + '_' + (counter++), kind, { pos: fr.pos(lx, y, lz), rot: fr.rot(faceDeg), scale, parent, indoor, lod: 0 });+ const garg = (fr, lx, y, lz, k, sc, parent) => place('Gargoyle_' + (counter++), gargoyle(k), MAT.fine, { pos: fr.pos(lx, y, lz), rot: fr.rot(-90), scale: [sc, sc, sc], parent });++ // One side of a straight bay: module, vault-side glass, statues, gargoyles+ const placeBay = (name, fr, parent, o = {}) => {+ placeSet(name + '_Upper', bayUp, [fr.ox, 0, fr.oz], fr.th, parent);+ if (!o.noVault) placeSet(name + '_Aisle', o.open ? aisleOpen : aisleFull, [fr.ox, 0, fr.oz], fr.th, parent);+ const c = cleres[(o.i || 0) % cleres.length];+ placeGlass(name + '_Clerestory', c, fr.pos(0, 0, zn), fr.th + 180, parent);+ if (!o.open) {+ placeGlass(name + '_AisleWindow', stories[(o.i || 0) % stories.length], fr.pos(0, 0, zaw + 0.45), fr.th + 180, parent);+ if ((o.i || 0) % 2 === 0) statue(fr, 0, 0.7, zaw - 0.3, SAINTS[(o.i || 0) % SAINTS.length], 90, 0.86, parent);+ if (!o.inTower) garg(fr, 0, 22.5, zaw + 1.05, (o.i || 0) % 3, 0.72, parent);+ }+ if (!o.noUpperGarg) garg(fr, 0, P.eaves - 0.2, zn + 0.62, ((o.i || 0) + 1) % 3, 0.66, parent);+ };+ const placeButtress = (name, fr, parent, i) => {+ placeSet(name, but, [fr.ox, 0, fr.oz], fr.th, parent);+ statue(fr, 0, 23.5, BUTTRESS.zT, 'angel', -90, 1.9, parent, false);+ garg(fr, 0, 15.2, BUTTRESS.zB - 1.05, i % 3, 1.0, parent);+ };+ const placePier = (name, lfr, parent, kind) => {+ placeSet(name, pier, [lfr.ox, 0, lfr.oz], lfr.th, parent);+ if (kind) statue(lfr, 0, 5.78, -1.14, kind, 90, 1.15, parent);+ };++ // ---- Two more bays carry the nave between the towers to the inner face of the west wall ----+ for (let t = 0; t < 2; t++) {+ const x = xTowerE + (t + 0.5) * BAY;+ placeBay('TowerBay' + t + '_N', frame(0, x, 0), gNave, { i: t + 2, noUpperGarg: true, inTower: true });+ placeBay('TowerBay' + t + '_S', frame(180, x, 0), gNave, { i: t, noUpperGarg: true, inTower: true });+ placeSet('TowerVault' + t, vault, [x, 0, 0], 0, gNave);+ if (t === 0) { placePier('TowerPier_N', frame(0, x + BAY / 2, zn), gNave, SAINTS[6]); placePier('TowerPier_S', frame(180, x + BAY / 2, -zn), gNave, SAINTS[7]); }+ }+ // ---- Nave ----+ for (let i = 0; i < P.naveBays; i++) {+ const x = xTowerE - (i + 0.5) * BAY, isOpen = i === P.naveBays - 1;+ placeBay('NaveBay' + i + '_N', frame(0, x, 0), gNave, { i, open: isOpen });+ placeBay('NaveBay' + i + '_S', frame(180, x, 0), gNave, { i: i + 2, open: isOpen });+ placeSet('NaveVault' + i, vault, [x, 0, 0], 0, gNave);+ }+ for (let k = 0; k <= P.naveBays - 1; k++) {+ const x = xTowerE - k * BAY;+ placePier('NavePier' + k + '_N', frame(0, x, zn), gNave, k ? SAINTS[k % SAINTS.length] : null);+ placePier('NavePier' + k + '_S', frame(180, x, -zn), gNave, k ? SAINTS[(k + 3) % SAINTS.length] : null);+ if (k >= 1 && k < P.naveBays - 1) { placeButtress('NaveButtress' + k + '_N', frame(0, x, 0), gNave, k); placeButtress('NaveButtress' + k + '_S', frame(180, x, 0), gNave, k + 1); }+ }+ // ---- Choir ----+ for (let j = 0; j < P.choirBays; j++) {+ const x = xChoirW - (j + 0.5) * BAY, isOpen = j === 0;+ placeBay('ChoirBay' + j + '_N', frame(0, x, 0), gChoir, { i: j + 1, open: isOpen });+ placeBay('ChoirBay' + j + '_S', frame(180, x, 0), gChoir, { i: j + 3, open: isOpen });+ placeSet('ChoirVault' + j, vault, [x, 0, 0], 0, gChoir);+ }+ for (let k = 1; k <= P.choirBays; k++) {+ const x = xChoirW - k * BAY;+ placePier('ChoirPier' + k + '_N', frame(0, x, zn), gChoir, SAINTS[(k + 5) % SAINTS.length]);+ placePier('ChoirPier' + k + '_S', frame(180, x, -zn), gChoir, SAINTS[(k + 1) % SAINTS.length]);+ placeButtress('ChoirButtress' + k + '_N', frame(0, x, 0), gChoir, k);+ placeButtress('ChoirButtress' + k + '_S', frame(180, x, 0), gChoir, k + 1);+ }+ // ---- Transept arms: the same bay, turned through 90 degrees ----+ for (const s of [1, -1]) {+ const gT = s > 0 ? gTN : gTS, tag = s > 0 ? 'N' : 'S';+ for (let q = 0; q < P.transeptBays + 1; q++) {+ const zc = s * (zn + (q + 0.5) * BAY), isOpen = q === 0;+ placeBay('Transept' + tag + q + '_W', frame(90 * s, xc, zc), gT, { i: q + 1, open: isOpen, noVault: isOpen });+ placeBay('Transept' + tag + q + '_E', frame(-90 * s, xc, zc), gT, { i: q + 3, open: isOpen, noVault: isOpen });+ placeSet('TranseptVault' + tag + q, vault, [xc, 0, zc], 90, gT);+ }+ for (let q = 1; q <= P.transeptBays; q++) { // the last bay ends in the front wall, which needs no free pier+ const zz = s * (zn + q * BAY);+ placePier('TranseptPier' + tag + q + '_W', frame(90, xc + zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 2) % SAINTS.length] : null);+ placePier('TranseptPier' + tag + q + '_E', frame(-90, xc - zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 4) % SAINTS.length] : null);+ if (q === P.transeptBays) { placeButtress('TranseptButtress' + tag + '_W', frame(90 * s, xc, zz), gT, q); placeButtress('TranseptButtress' + tag + '_E', frame(-90 * s, xc, zz), gT, q + 1); }+ }+ }+ // ---- Crossing ----+ const xPier = buildCrossingPier(), aPier = buildAislePier(), xVault = mkBatches();+ const sp = zn - 0.3;+ const crossCrown = addRibVault(xVault.F, xVault.A, [[-sp, -sp], [sp, -sp], [sp, sp], [-sp, sp]], P.vaultSpring, P.vaultCrown + 0.5, { sideRise: () => 12.9, nS: 12, nT: 10 });+ addBoss(xVault.G, xVault.F, crossCrown, 0.95);+ // Great transverse arches and the walls above them+ const arch = pointedArc(2 * (zn - 0.5), 12.9, 7);+ for (let i = 0; i < 4; i++) xVault.F.at(M.of(M.Ry(i * 90), M.T(0, P.vaultSpring, zn - 0.05)), () => xVault.F.sweep(arch, rollSection(0.9, 1.2, 2, 0.09, 3), { caps: false }));+ placeSet('CrossingVault', xVault, [xc, 0, 0], 0, gX);+ for (const [a, b] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {+ placeSet('CrossingPier', xPier, [xc + a * zn, 0, b * zn], 0, gX);+ placeSet('AngleAislePier', aPier, [xc + a * zaw, 0, b * zaw], 0, gX);+ }+ // ---- Chevet ----+ const sector = buildChevetSector(), apsePier = buildApsePier();+ const apseSaints = [{ robe: 'gold', mantle: 'cobalt', crown: 1, veil: 'pale', arm: 'pray' }, { robe: 'white', mantle: 'ruby', beard: 2, attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'emerald', beard: 1, attr: 'book' }, { robe: 'white', mantle: 'rubyLt', beard: 1, cross: 1, arm: 'up' }];+ const hemi = apseSaints.slice(0, 3).map((f, i) => makeLancetGlass(2.05, P.triforiumTop + 0.45, P.vaultSpring + 4.8, 4.3, 70 + i, f, i % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }));+ const chapelGlass = ['annunciation', 'crucifixion'].map((nm, i) => { const g = new Glass(80 + i); g.raw(archOutline(1.3, 3.2, 9.9, 2.5, 7), 'lead', -0.4, 0); gLancetStory(g, 0, 1.3, 3.2, 9.9, 2.5, [nm, ['supper', 'magi'][i], ['magi', 'resurrection'][i]], { cell: 0.6, c1: i ? 'cobalt' : 'ruby', c2: i ? 'ruby' : 'cobalt' }); return g.b; });+ for (let k = 0; k < P.apseSides; k++) {+ const th = -(k + 0.5) * CHEV.d, fr = frame(th, xa, 0), gS = group('ChevetSector' + k, gApse);+ placeSet('Sector' + k, sector, [xa, 0, 0], th, gS);+ placeGlass('HemicycleWindow' + k, hemi[(k + (k > 3 ? 1 : 0)) % hemi.length], fr.pos(0, 0, CHEV.Ra), th + 180, gS);+ const nF = P.chapelFacets, dF = 216 / nF, ap = CHEV.Rc * Math.cos(rad(dF / 2));+ for (let i = 0; i < nF; i++) {+ const fm = -108 + (i + 0.5) * dF, lx = ap * Math.sin(rad(fm)), lz = CHEV.zc + ap * Math.cos(rad(fm));+ placeGlass('ChapelWindow' + k + '_' + i, chapelGlass[(i + k) % chapelGlass.length], fr.pos(lx, 0, lz + 0.0), th + fm + 180, gS);+ }+ statue(fr, 0, 0.5, CHEV.zc + CHEV.Rc * 0.55, k === 3 ? 'virgin' : SAINTS[k], 90, 0.92, gS);+ garg(frame(th - 10, xa, 0), 0, 15.5, CHEV.zc + 1.2, k % 3, 0.7, gS);+ if (k >= 1) {+ const bt = -k * CHEV.d;+ placeButtress('ChevetButtress' + k, frame(bt, xa, 0), gS, k);+ // A slim clustered pier at each angle of the hemicycle: the arches now spring from something+ placeSet('ApsePier' + k, apsePier, frame(bt, xa, 0).pos(0, 0, zn), bt, gS);+ }+ }+ // ---- Roofs, ridge cresting, floor ----+ const Lr = new Batch(TILE.lead);+ const yE = P.eaves + 0.15, half = zn + 0.72;+ addRoofRun(Lr, xa, LAY.xW - 1.2, half, yE, ridge);+ Lr.at(M.T(xc, 0, 0), () => Lr.at(M.Ry(90), () => addRoofRun(Lr, -(zn + (P.transeptBays + 1) * BAY + 0.4), zn + (P.transeptBays + 1) * BAY + 0.4, half, yE, ridge)));+ place('MainRoof_Lead', Lr, MAT.lead, { parent: gRoof });+ const crest = buildCresting(BAY);+ const nCrest = Math.floor((LAY.xW - 2 - xa) / BAY);+ for (let i = 0; i < nCrest; i++) place('RidgeCresting' + i, crest, MAT.gold, { pos: [xa + (i + 0.5) * BAY, ridge, 0], parent: gRoof });+ for (const s of [1, -1]) for (let q = 0; q < P.transeptBays + 2; q++) { const zz = s * (0.5 + (q + 0.5) * BAY + 3); if (Math.abs(zz) < zn + (P.transeptBays + 1) * BAY) place('TranseptCresting' + (s > 0 ? 'N' : 'S') + q, crest, MAT.gold, { pos: [xc, ridge, zz], rot: [0, 90, 0], parent: gRoof }); }+ buildFleche(gRoof, xc, 0);+ // The church floor sits just above the ground outside, so the two can never fight+ const fl = new Batch(TILE.pave), fy = 0.06;+ const R13 = zaw + 0.1;+ fl.face([[xa, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, -R13], [xa, fy, -R13]]);+ const tz = zn + (P.transeptBays + 1) * BAY + 1;+ fl.face([[xc - R13, fy, tz], [xc + R13, fy, tz], [xc + R13, fy, -tz], [xc - R13, fy, -tz]]);+ for (let k = 0; k < P.apseSides; k++) { const th = rad(-(k + 0.5) * CHEV.d), f = frame(-(k + 0.5) * CHEV.d, xa, 0), rr = CHEV.zc + CHEV.Rc + 0.2, hh = rr * Math.tan(CHEV.h); fl.face([f.pos(0, fy, 0), f.pos(-hh, fy, rr), f.pos(hh, fy, rr)]); }+ place('Paving', fl, MAT.pave, { parent: ROOT });+ // ---- Stained light on the floor from the south aisle ----+ const pools = [buildLightPool(3), buildLightPool(11)];+ for (let i = 0; i < P.naveBays - 1; i++) createPart(uniq('StainedLight_Pool' + i), pools[i % 2], MAT.pool, { position: [xTowerE - (i + 0.5) * BAY, 0.08 + 0.002 * (i % 3), -zaw], parent: gNave });+}++// ==================================================================================+// THE GREAT ROSE: real stone tracery, and glass depicting Christ among apostles and angels+// ==================================================================================+const ROSE = { r0: 1.15, rs1: 2.35, r1: 3.05, rs2: 4.05, r2: 4.72, n1: 12, n2: 24 };+const pol = (r, a) => [r * Math.cos(a), r * Math.sin(a)];+function addRoseTracery(F, R) {+ const k = R / 5.5, heavy = traceryBar(0.3 * k, 0.5), mid = hexBar(0.2 * k, 0.42), thin = hexBar(0.14 * k, 0.34);+ const dia = [[0.07 * k, 0], [0, 0.17], [-0.07 * k, 0], [0, -0.17]]; // lightest bar, for the little roundels+ const { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE;+ F.sweep(ringPts(0, 0, R, 36), heavy, { closed: true });+ F.sweep(ringPts(0, 0, r0 * k, 14), mid, { closed: true });+ F.sweep(ringPts(0, 0, r2 * k, 36), mid, { closed: true });+ F.sweep(ringPts(0, 0, r1 * k, 24), mid, { closed: true });+ // Inner wheel: colonnette spokes, each light closed by a pointed trefoil head+ const head = (n, rs, re, a, bar) => {+ const da = PI / n * 0.98, pts = [], N = 6;+ for (let i = 0; i <= N; i++) { const t = -1 + 2 * i / N, h = Math.pow(1 - Math.pow(Math.abs(t), 1.5), 0.72); pts.push(pol((rs + (re - rs) * h) * k, a + t * da)); }+ F.sweep(pts, bar, { caps: false });+ return pts;+ };+ for (let i = 0; i < n1; i++) {+ const a = i * TAU / n1, am = a + PI / n1;+ F.sweep([pol(r0 * k, a), pol(rs1 * k, a)], mid, { caps: false });+ head(n1, rs1, r1 * 0.985, am, thin);+ // the cusp hanging inside the head+ const c = pol((rs1 + 0.34) * k, am); F.sweep(ringPts(c[0], c[1], 0.26 * k, 6), dia, { closed: true, s: 0.95, crease: 80 });+ }+ // Outer wheel of twenty-four lights+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, am = a + PI / n2;+ F.sweep([pol(r1 * k, a), pol(rs2 * k, a)], thin, { caps: false });+ head(n2, rs2, r2 * 0.99, am, thin);+ // Quatrefoil roundels in the outer band+ const c = pol((R / k + r2) / 2 * k, a);+ F.sweep(ringPts(c[0], c[1], 0.3 * k, 6), dia, { closed: true, crease: 80 });+ }+}+const SHARED = {};+// Rose tracery is one mesh, scaled for the smaller transept roses; bright outside, dim inside+function roseTracery() {+ if (!SHARED.rose) { const b = new Batch(TILE.fine); addRoseTracery(b, WF.roseR); SHARED.rose = b; }+ return SHARED.rose;+}+function largePinnacle() {+ if (!SHARED.pin) { const b = new Batch(TILE.fine); addPinnacle(b, 2, 10, { lod: 1 }); SHARED.pin = b; }+ return SHARED.pin;+}+function makeRoseGlass(R, seed, o = {}) {+ const g = new Glass(seed), k = R / 5.5, { r0, r1, r2, n1, n2 } = ROSE;+ g.raw(ringPts(0, 0, R + 0.08, 48), 'lead', -0.4, 0);+ // A light is never one flat sheet: lay the painted mosaic across it, clipped to its shape+ const ground = (pts, col, layer) => { let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of pts) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); } g.withClip(pts, () => gDiaper(g, x0, y0, x1, y1, 0, col, null, null, layer)); };+ // A roundel: a thin jewelled rim round a deep field, with everything inside it clipped to the circle+ const roundel = (cx, cy, rad_, field, draw) => {+ gRim(g, cx, cy, rad_, rad_ * 1.13, ['gold', 'ruby', 'gold', 'cobalt'], 16, 1.2);+ g.disc(cx, cy, rad_, field, 1.5, 16, { came: 0 });+ g.withClip(ringPts(cx, cy, rad_, 16), draw);+ };+ // Nothing behind the tracery is left black: the whole wheel is first laid in cobalt mosaic+ ground(ringPts(0, 0, R + 0.04, 32), 'cobalt', -0.25);+ // The outer band: alternating sheets of ruby and cobalt, a jewel in each+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, b = (i + 1) * TAU / n2;+ ground([pol(r2 * k, a), pol(R, a), pol(R, b), pol(r2 * k, b)], i % 2 ? 'ruby' : 'cobalt', 0);+ const c = pol((R / k + r2) / 2 * k, a); g.disc(c[0], c[1], 0.27 * k, i % 2 ? 'emerald' : 'ruby', 0.6, 10, { came: 0.016 }); g.disc(c[0], c[1], 0.12 * k, 'gold', 1, 8, { came: 0.01 });+ }+ // Outer lights: kings, angels and elders, upright in their roundels so the eye can rest on each face+ const busts = [{ robe: 'emerald', beard: 1, crown: 1, halo: 'gold' }, { robe: 'white', face: 'angel', halo: 'gold', wings: 1 }, { robe: 'ruby', beard: 2, halo: 'gold' }];+ for (let i = 0; i < n2; i++) {+ const am = (i + 0.5) * TAU / n2, da = PI / n2, col = i % 2 ? 'cobalt' : 'ruby', field = i % 2 ? 'ruby' : 'cobalt';+ ground([pol(r1 * k, am - da), pol(r2 * k, am - da), pol(r2 * k, am + da), pol(r1 * k, am + da)], col, 0);+ const mr = (r1 + r2) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.46 * k, B = busts[i % busts.length];+ roundel(cx, cy, rr, field, () => {+ if (B.wings) for (const s of [-1, 1]) g.poly([[cx + s * rr * 0.16, cy - rr * 0.1], [cx + s * rr * 0.98, cy + rr * 0.42], [cx + s * rr * 0.86, cy - rr * 0.5], [cx + s * rr * 0.3, cy - rr * 0.62]], 'gold', 1.8, { came: 0.012 });+ gBust(g, cx, cy - rr * 0.46, rr * 1.32, Object.assign({ layer: 2 }, B));+ });+ }+ // Inner lights: the twelve apostles enthroned, each upright in a larger roundel, colours answering round the wheel+ const scheme = o.figs || [{ robe: 'ruby', mantle: 'cobalt', beard: 1, attr: 'key', arm: 'point' }, { robe: 'emerald', mantle: 'ruby', beard: 2, attr: 'book' }, { robe: 'gold', mantle: 'cobalt', beard: 1, attr: 'sword', arm: 'point' }, { robe: 'cobalt', mantle: 'ruby', attr: 'book' }, { robe: 'purple', mantle: 'gold', beard: 1, attr: 'book' }, { robe: 'ruby', mantle: 'emerald', beard: 2, attr: 'key', arm: 'point' }];+ for (let i = 0; i < n1; i++) {+ const am = (i + 0.5) * TAU / n1, da = PI / n1, col = i % 2 ? 'ruby' : 'cobalt', field = i % 2 ? 'cobalt' : 'ruby';+ ground([pol(r0 * k, am - da), pol(r1 * k, am - da), pol(r1 * k, am + da), pol(r0 * k, am + da)], col, 0);+ const mr = (r0 + r1) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.62 * k;+ roundel(cx, cy, rr, field, () => g.withXf(0, cx, cy - rr * 0.92, 1, () => gFigure(g, 0, 0, rr * 1.86, Object.assign({ pose: 'sit', layer: 2 }, scheme[i % scheme.length]))));+ }+ // The centre: Christ in Majesty in a gilded mandorla+ g.disc(0, 0, r0 * k + 0.06, 'lead', 2.6, 16, { came: 0, jitter: 0 });+ ground(ringPts(0, 0, r0 * k, 16), o.centre || 'cobalt', 3);+ g.withClip(ringPts(0, 0, r0 * k, 14), () => g.withXf(0, 0, -0.98 * k, k, () => {+ const m = []; for (let i = 0; i < 16; i++) { const t = i * TAU / 16; m.push([0.62 * Math.cos(t) * Math.pow(Math.abs(Math.sin(t)) * 0.3 + 0.7, 1), 1 + 0.96 * Math.sin(t)]); }+ g.poly(m, 'gold', 3.3, { came: 0.02 });+ g.poly(m.map((p) => [p[0] * 0.8, 1 + (p[1] - 1) * 0.88]), 'rubyLt', 3.5, { came: 0.012 });+ gFigure(g, 0, 0.22, 1.62, o.majesty || { pose: 'sit', robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'up', attr: null, layer: 4 });+ }));+ return g.b;+}++// ==================================================================================+// WEST FRONT. Local frame: x across the facade, y up, +z out towards the viewer, z=0 the wall.+// ==================================================================================+const WF = { halfW: 21.5, topY: 47, roseY: 31.8, roseR: 5.5, kingY: 20.2, kingTop: 25.3, galY: 40.6, galTop: 46.2, wallT: 0.8, portalC: { x: 0, w0: 4.3, orders: P.jambStatues, dx: 0.58, dz: 0.62, ys: 7.5 }, portalS: { x: 13.5, w0: 3.7, orders: P.jambStatues - 1, dx: 0.6, dz: 0.66, ys: 7.2 } };+// Aged oak door leaf with forged straps, studs and a ring. Hung at x=0, spans to x=w, h high, front +z.+function addDoorLeaf(set, x0, w, h, flip) {+ const { O, I } = set, planks = 6, pw = w / planks, d = flip ? -1 : 1;+ for (let i = 0; i < planks; i++) {+ const a = x0 + d * i * pw, c = x0 + d * (i + 0.97) * pw, xa = Math.min(a, c), xc = Math.max(a, c), tone = 0.46 + 0.16 * Math.abs(Math.sin(i * 2.4 + x0 * 3));+ O.at(M.Ry(0), () => O.box(xa, xc, 0.02, h, -0.05, 0.05, { s: tone }));+ }+ // Oak grain runs up each plank: remap planes with u across and v up is the default box projection+ const strapY = [0.72, h * 0.36, h * 0.64, h - 0.7];+ for (const y of strapY) {+ const reach = w * 0.94, xEnd = x0 + d * reach;+ I.shade(0.34, () => {+ I.box(Math.min(x0, xEnd), Math.max(x0, xEnd), y - 0.075, y + 0.075, 0.05, 0.085, {});+ // A forged hinge: the strap swells and ends in a spear with two barbs, flat iron from one piece; a knuckle turns at the door's edge+ const spear = [[xEnd, y - 0.075], [xEnd + d * 0.1, y - 0.15], [xEnd + d * 0.16, y - 0.07], [xEnd + d * 0.4, y], [xEnd + d * 0.16, y + 0.07], [xEnd + d * 0.1, y + 0.15], [xEnd, y + 0.075]];+ I.slab(spear, [], 0.05, 0.085, {});+ I.at(M.T(x0 + d * 0.03, y, 0.07), () => I.lathe([[0.055, -0.12], [0.055, 0.12]], 6, {}));+ for (let k = 0; k < 4; k++) { const sx = x0 + d * (k + 0.5) * reach / 4; I.box(sx - 0.03, sx + 0.03, y - 0.03, y + 0.03, 0.08, 0.115, { taper: 0.012 }); }+ });+ }+ // Ring handle+ I.shade(0.4, () => I.at(M.of(M.T(x0 + d * (w - 0.3), h * 0.42, 0.11), M.Ry(0)), () => {+ const rg = []; for (let i = 0; i <= 12; i++) { const t = i * TAU / 12; rg.push([0.1 * Math.cos(t), 0.1 * Math.sin(t) - 0.1, 0]); }+ I.tube(rg, 0.018, 5, { capEnd: false }); I.box(-0.05, 0.05, -0.05, 0.05, -0.05, 0, {});+ }));+}+// Height of each sculpted figure at scale 1, from its feet to the top of its hair, crown or mitre+const figureTop = (kind) => ({ bishop: 2.15, king: 2.03, queen: 2.03, virgin: 2.03 }[FIG_ALIAS[kind] || kind] || 1.95);+function buildPortal(cfg, kinds, tymp) {+ const set = mkBatches(), { A, F, G, O, I } = set, { w0, orders: N, dx, dz, ys } = cfg;+ // Every order is concentric: the same two arc centres with the radius growing by one step+ const r0 = w0 * 0.86, c0 = (r0 * r0 - w0 * w0 / 4) / w0, wT = (i) => w0 + 2 * i * dx, zT = (i) => i * dz;+ const riseI = (i) => Math.sqrt(Math.pow(c0 + w0 / 2 + i * dx, 2) - c0 * c0);+ const riseOf = (w) => riseI((w - w0) / (2 * dx));+ const figs = [], zF = N * dz, outerW = wT(N), apex = ys + riseOf(outerW), lintelH = 1.2, doorH = ys - lintelH;+ const statueY = 1.5, statueK = 1.22;+ // ---- Solid stepped jambs, each order with a dado of carved blind arches ----+ for (let i = 0; i < N; i++) {+ for (const s of [-1, 1]) {+ const xa = s * wT(i) / 2, xb = s * wT(i + 1) / 2, x0 = Math.min(xa, xb), x1 = Math.max(xa, xb), zf = zT(i + 1);+ A.box(x0, x1, 0, ys, -0.1, zf, { noBot: true });+ // The dado: a plinth, a blind trefoil head on two colonnettes, a moulded table for the statues above+ F.box(x0 - 0.01, x1 + 0.01, 0, 0.26, zf, zf + 0.05, {});+ F.at(M.T((x0 + x1) / 2, 0, zf), () => {+ const w = x1 - x0 - 0.12, y0 = 0.26, ya = 0.86, r = 0.32;+ F.sweep(pointedArc(w, r, 4).map((p) => [p[0], p[1] + ya]), hexBar(0.05, 0.09), { caps: false, z: 0.03 });+ for (const t of [-1, 1]) F.box(t * w / 2 - 0.03, t * w / 2 + 0.03, y0, ya, 0, 0.06, {});+ F.sweep([[-(x1 - x0) / 2, 1.3], [(x1 - x0) / 2, 1.3]], stringSection(0.16, 0.1), { caps: false });+ });+ // Colonnette on the arris, with a foliate capital; the saint stands against it+ const cx = s * (wT(i) / 2 + 0.02), cz = zT(i) + 0.32;+ F.at(M.T(cx, 0, cz), () => {+ F.lathe([[0.2, 1.3], [0.2, 1.42], [0.13, 1.54], [0.13, ys - 0.5], [0.16, ys - 0.46], [0.13, ys - 0.42], [0.26, ys - 0.04], [0.3, ys]], 8);+ for (let l = 0; l < 4; l++) addLeaf(F, M.of(M.T(0, ys - 0.42, 0), M.Ry(l * 90 + 45), M.T(0.1, 0, 0)), 0.38, { coarse: 1 });+ });+ // The figure stands with its back to the column, on a carved base; its canopy is engaged on the same column,+ // a hand's breadth above this figure's own head+ const dirX = -s * dz, dirZ = dx * 1.5, l = Math.hypot(dirX, dirZ), th = Math.atan2(-dirZ / l, dirX / l) * 180 / PI, fx = dirX / l, fz = dirZ / l;+ const kind = kinds[(i + (s > 0 ? 0 : kinds.length >> 1)) % kinds.length], canopyY = statueY + figureTop(kind) * statueK + 0.1;+ const sx = cx + fx * 0.27, sz = cz + fz * 0.27;+ F.at(M.T(sx, 0, sz), () => {+ F.lathe([[0.3, 1.25], [0.34, 1.32], [0.24, 1.38], [0.3, 1.44], [0.33, statueY]], 8);+ for (let l = 0; l < 2; l++) addLeaf(F, M.of(M.T(0, 1.36, 0), M.Ry(l * 90 + (s > 0 ? -60 : 150)), M.T(0.24, 0, 0)), 0.2, { coarse: 1 });+ F.at(M.T(0, canopyY + 0.14, 0), () => {+ // A canopy like a tiny carved building: vaulted soffit, a drum of gablets, thin spirelet+ F.lathe([[0.05, -0.14], [0.27, -0.06], [0.33, 0], [0.33, 0.1], [0.28, 0.14], [0.17, 0.17], [0.17, 0.5], [0.22, 0.54], [0.15, 0.58], [0.07, 0.64], [0.01, 1.02]], 6, { phase: PI / 6, faceted: true });+ const face = s > 0 ? -60 : 60;+ for (const l of [-1, 0, 1]) F.at(M.of(M.Ry(face + l * 60), M.T(0, 0, 0.27)), () => {+ F.face([[-0.16, 0.11, 0.02], [0.16, 0.11, 0.02], [0, 0.42, 0.02]]);+ crocketsAlong(F, [-0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [-1, 0.6, 0], 0);+ crocketsAlong(F, [0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [1, 0.6, 0], 0);+ F.shade(0.5, () => F.face([[-0.08, 0.14, 0.03], [0.08, 0.14, 0.03], [0, 0.28, 0.03]]));+ });+ });+ });+ figs.push({ kind, pos: [cfg.x + sx, statueY, sz], rot: [0, th, 0], scale: statueK, mirror: s < 0 });+ }+ }+ // ---- Archivolts: each order a hollow, each hollow a procession of small figures, every one on a console under a canopy ----+ const ARCH_KINDS = ['apostle', 'king', 'angel', 'bishop', 'queen'], ak = 0.62;+ for (let i = 0; i < N; i++) {+ const w = wT(i), r = riseI(i), arc = pointedArc(w, r, 8).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, [[0, -0.1], [dx, -0.1], [dx, dz], [0, 0]], { z: zT(i), caps: false, crease: 30 });+ F.at(M.T(0, 0, zT(i + 1) - 0.02), () => F.sweep(pointedArc(wT(i + 1), riseI(i + 1), 8).map((p) => [p[0], p[1] + ys]), hexBar(0.2, 0.22), { caps: false }));+ const pts = pointedArc(w + dx, riseI(i + 0.5), 16).map((p) => [p[0], p[1] + ys]);+ let len = 0; const seg = []; for (let k = 0; k < pts.length - 1; k++) { const l = Math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]); seg.push(l); len += l; }+ const n = Math.max(4, Math.round(len / 1.52));+ for (let q = 0; q < n; q++) {+ const d = (q + 0.5) / n * len; let acc = 0, k = 0; while (k < seg.length - 1 && acc + seg[k] < d) { acc += seg[k]; k++; }+ const f = (d - acc) / seg[k], px = lerp(pts[k][0], pts[k + 1][0], f), py = lerp(pts[k][1], pts[k + 1][1], f);+ const tx0 = (pts[k + 1][0] - pts[k][0]) / seg[k], ty0 = (pts[k + 1][1] - pts[k][1]) / seg[k], rx = -ty0, ry = tx0;+ const up = d > len / 2 ? -1 : 1, Y = [tx0 * up, ty0 * up, 0];+ // sits on the splay, looking out towards the opening and the viewer+ const nl = Math.hypot(dz, dx), X = [-rx * dz / nl, -ry * dz / nl, dx / nl];+ const Z = [X[1] * Y[2] - X[2] * Y[1], X[2] * Y[0] - X[0] * Y[2], X[0] * Y[1] - X[1] * Y[0]];+ const pos = [px + X[0] * 0.17 - Y[0] * 0.62, py + X[1] * 0.17 - Y[1] * 0.62, zT(i) + dz * 0.5 + X[2] * 0.17];+ figs.push({ kind: ARCH_KINDS[(i + q) % ARCH_KINDS.length], lod: 0, pos: [cfg.x + pos[0], pos[1], pos[2]], basis: { X, Y, Z }, scale: ak });+ F.at(frameOf(pos, X, Y), () => {+ F.lathe([[0.03, -0.18], [0.16, -0.02], [0.18, 0.02]], 6);+ F.lathe([[0.02, 1.27], [0.16, 1.33], [0.16, 1.38], [0.02, 1.56]], 6, { faceted: true });+ });+ }+ }+ // ---- Outer hood with gable and crockets ----+ const hood = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, zF), () => F.sweep(hood, rollSection(0.34, 0.44, 2, 0.05, 2), { caps: false }));+ // The gable springs from the arch springing: plain coursed stone, its carving is real and stands proud of it+ const gw = outerW + 1.5, gy = ys, gTop = WF.kingY - 0.7, gH = gTop - gy;+ const archCut = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ const field = [[-gw / 2, gy], [-(outerW + 0.1) / 2, gy], ...archCut.slice(1, -1), [(outerW + 0.1) / 2, gy], [gw / 2, gy], [0, gTop]];+ A.at(M.T(0, 0, zF - 0.1), () => A.slab(field, [], -0.1, 0, { noOuterSides: true, noBack: true }));+ A.at(M.T(0, 0, zF - 0.2), () => A.slab(field, [], -zF + 0.3, 0, { noOuterSides: false, noFront: true, noBack: true, so: 0.7 }));+ F.at(M.T(0, 0, zF - 0.14), () => {+ addGable(F, gw, gH, 0, 0.24, gy, { size: 0.72, finial: 0.1, crockets: 6, noField: true, lod: 1 });+ const yc = apex + (gTop - apex) * 0.36, rc = Math.min(0.92, (gTop - yc) * 0.42);+ F.sweep(ringPts(0, yc, rc, 16), traceryBar(0.18, 0.24), { closed: true, z: 0.04 });+ const foil = []; for (let i = 0; i < 15; i++) { const a = PI / 2 + i * TAU / 15, R = rc * (0.56 + 0.34 * Math.pow(Math.abs(Math.cos(a * 1.5 - PI * 0.75)), 0.8)); foil.push([R * Math.cos(a), yc + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.09, 0.18), { closed: true, z: 0.04, crease: 40 });+ });+ G.at(M.T(0, gTop + 0.1, zF - 0.02), () => addFinial(G, 1.5, 1));+ // ---- Tympanum: one block of dressed stone, standing proud of the coursed wall behind it, with the scene carved out of it ----+ const tw = w0 + 0.04, tr = riseOf(w0), k = w0 / 4.3, reliefZ = 0.16, relief = 0.6;+ F.slab(archOutline(tw, ys - 0.05, ys, tr, 9), [], -0.1, reliefZ, { noBack: true, sr: 0.7 });+ // the ground the figures rise from is part of the block, a low moulded base rather than a shelf+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, ys - 0.02, ys + 0.1, -0.08, reliefZ + 0.14, {});+ // Lintel: a recessed frieze, deep enough to hold its people, cornice and sill standing forward of it+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, ys - 0.02, -0.08, -0.02, {});+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, doorH + 0.12, -0.08, 0.42, {});+ F.at(M.T(0, 0, 0.42), () => F.sweep([[-tw / 2 - 0.02, doorH + 0.06], [tw / 2 + 0.02, doorH + 0.06]], stringSection(0.12, 0.06), { caps: false }));+ const nL = Math.round(w0 / 0.37), frieze = ['apostle', 'queen', 'apostle', 'king', 'bishop', 'apostle'];+ for (let i = 0; i < nL; i++) {+ // two ranks, every other figure a step forward: they overlap and each one turns to the middle of the door+ const x = -w0 / 2 + (i + 0.5) * w0 / nL, side = x < 0 ? 1 : -1, deep = i % 2;+ figs.push({ kind: frieze[i % frieze.length], lod: 0, relief: 0.68, pos: [cfg.x + x, doorH + 0.12, deep ? 0.04 : 0.16], rot: [0, -90 + side * (16 + 10 * (i % 3)), 0], scale: 0.43 });+ }+ // The central figure enthroned in a vesica: two arcs meeting in a point above and below, a broad moulded band of stone+ // with its face gilded. Everything in the scene is cut back to the same block, and nothing crosses the frame.+ const mY = ys + 0.1, mH = tr * 0.84, mW = 0.84 * k, nM = 10;+ const vesica = (w, h, y0) => { const out = []; for (let i = 0; i < nM; i++) { const v = i / nM; out.push([w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } for (let i = 0; i < nM; i++) { const v = 1 - i / nM; out.push([-w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } return out; };+ const vo = vesica(mW, mH, mY - 0.06), vi = vesica(mW - 0.2, mH - 0.44, mY + 0.16);+ F.at(M.T(0, 0, reliefZ), () => { F.slab(vo, [vi], -0.02, 0.07, { sr: 0.78 }); F.sweep(vo, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); F.sweep(vi, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); });+ G.at(M.T(0, 0, reliefZ + 0.07), () => G.slab(vesica(mW - 0.025, mH - 0.07, mY - 0.025), [vesica(mW - 0.175, mH - 0.37, mY + 0.125)], 0, 0.05, {}));+ figs.push({ kind: tymp.main, half: 1, relief, pos: [cfg.x, mY, reliefZ + 0.02], rot: [0, -90, 0], scale: 1.0 * k });+ // Worshippers on their knees and angels crowding behind them, every head turned to the throne+ for (const s of [-1, 1]) {+ figs.push({ kind: 'kneel', half: 1, relief, pos: [cfg.x + s * 1.28 * k, mY, reliefZ + 0.12], rot: [0, -90 + s * 50, 0], scale: 0.84 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[0], half: 1, relief, pos: [cfg.x + s * 1.6 * k, mY, reliefZ + 0.01], rot: [0, -90 + s * 34, 0], scale: 0.72 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[1], half: 1, relief, lod: 0, pos: [cfg.x + s * 1.92 * k, mY, reliefZ + 0.08], rot: [0, -90 + s * 55, 0], scale: 0.44 * k, mirror: s < 0 });+ }+ // Trumeau with its statue, on a carved base, under a canopy+ A.box(-0.32, 0.32, 0, doorH, -0.1, 0.34, { noBot: true });+ const tK = tymp.trumeauScale || 1.3, tBase = 1.06, tCanopy = tBase + figureTop(tymp.trumeau) * tK + 0.12;+ F.at(M.T(0, 0, 0.5), () => {+ F.lathe([[0.42, 0], [0.42, 0.42], [0.3, 0.56], [0.25, 0.78], [0.34, 0.92], [0.36, tBase]], 8, { faceted: true, phase: PI / 8 });+ for (let l = 0; l < 6; l++) addLeaf(F, M.of(M.T(0, 0.82, 0), M.Ry(l * 60 + 15), M.T(0.27, 0, 0)), 0.24, { coarse: 1 });+ F.at(M.T(0, tCanopy + 0.2, 0), () => F.lathe([[0.06, -0.2], [0.33, -0.08], [0.42, 0], [0.42, 0.14], [0.35, 0.18], [0.22, 0.22], [0.22, 0.58], [0.28, 0.62], [0.2, 0.66], [0.09, 0.72], [0.02, 1.1]], 6, { phase: PI / 6, faceted: true }));+ });+ figs.push({ kind: tymp.trumeau, pos: [cfg.x, tBase, 0.5], rot: [0, -90, 0], scale: tK });+ // Doors: two leaves either side of the trumeau, recessed in the wall+ const leaf = w0 / 2 - 0.34;+ O.at(M.T(0, 0, -0.14), () => { const set2 = set; I.at(M.T(0, 0, -0.14), () => { addDoorLeaf(set2, w0 / 2 - 0.02, leaf, doorH - 0.04, true); addDoorLeaf(set2, -w0 / 2 + 0.02, leaf, doorH - 0.04, false); }); });+ return { set, figs, apex, zF, outerW };+}+function placePortal(gWest, name, cfg, pr, opt = {}) {+ placeSet(name, pr.set, [cfg.x, 0, 0], 0, gWest);+ const gF = group(name + '_Figures', gWest); // the people of one door, as one group of nodes+ pr.figs.forEach((f, i) => {+ const rot = f.rot || eulerFromBasis(f.basis.X, f.basis.Y, f.basis.Z);+ const pos = [f.pos[0] + (cfg.x - pr.baseX), f.pos[1], f.pos[2]];+ // Full sculpture where people stand next to it; opt.light keeps a door's overhead scene, or the whole door, to the lighter copies+ const far = opt.light === 'all' || (opt.light === 'scene' && f.pos[1] > 4);+ placeFigure(name + '_Fig' + i, f.kind, { pos, rot, scale: f.scale, parent: gF, lod: far ? 0 : f.lod, mirror: f.mirror, half: f.half, relief: f.relief });+ });+}+function eulerFromBasis(X, Y, Z) { // degrees, three.js 'XYZ' order+ const m13 = clamp(Z[0], -1, 1), y = Math.asin(m13);+ let x, z;+ if (Math.abs(m13) < 0.9999) { x = Math.atan2(-Z[1], Z[2]); z = Math.atan2(-Y[0], X[0]); } else { x = Math.atan2(Y[2], Y[1]); z = 0; }+ return [x * 180 / PI, y * 180 / PI, z * 180 / PI];+}+// One niche of the Gallery of Kings, shared along the facade+function buildKingNiche(pitch) {+ const set = mkBatches(), { F, A } = set, h = WF.kingTop - WF.kingY, y0 = WF.kingY;+ A.face([[-pitch / 2, y0, 0.02], [pitch / 2, y0, 0.02], [pitch / 2, y0 + h, 0.02], [-pitch / 2, y0 + h, 0.02]]);+ F.box(-pitch / 2, pitch / 2, y0 - 0.5, y0, 0, 1.3, { s: 0.9 });+ F.at(M.T(0, 0, 1.3), () => F.sweep([[-pitch / 2, y0 - 0.38], [pitch / 2, y0 - 0.38]], stringSection(0.3, 0.26), { caps: false }));+ // Colonnettes and a cusped arch with a gablet over each king+ const ys = y0 + 3.15, rise = 1.3;+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0.95), () => F.lathe([[0.17, y0], [0.09, y0 + 0.16], [0.09, ys - 0.2], [0.2, ys]], 8));+ F.at(M.T(0, 0, 0.95), () => {+ const arc = pointedArc(pitch - 0.1, rise, 6).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, rollSection(0.14, 0.36, 1, 0.03, 2), { caps: false });+ // solid spandrel+ A.at(M.T(0, 0, 0.95), () => A.slab([[-pitch / 2, ys], [-(pitch - 0.1) / 2, ys], ...arc, [(pitch - 0.1) / 2, ys], [pitch / 2, ys], [pitch / 2, y0 + h], [-pitch / 2, y0 + h]], [], -0.1, 0.1, { noOuterSides: true }));+ addGable(F, pitch * 0.96, 1.12, 0.1, 0.1, ys + 0.46, { size: 0.26, crockets: 2, finial: 0.34, solid: true, lod: 0 });+ });+ // The roof of the gallery behind the arcade+ F.box(-pitch / 2, pitch / 2, y0 + h - 0.02, y0 + h + 0.12, 0, 1.28, { s: 0.85 });+ return set;+}+// One bay of the open arcade above the rose and the tower windows+function buildGalleryBay(pitch) {+ const set = mkBatches(), { F, A } = set, y0 = WF.galY, y1 = WF.galTop, ys = y0 + 3.3;+ F.shade(0.4, () => F.face([[-pitch / 2, y0, 0.03], [pitch / 2, y0, 0.03], [pitch / 2, y1, 0.03], [-pitch / 2, y1, 0.03]]));+ const arc = pointedArc(pitch * 0.92, 1.5, 5).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, 0.5), () => {+ F.sweep(arc, traceryBar(0.16, 0.36), { caps: false });+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0), () => F.lathe([[0.2, y0], [0.1, y0 + 0.2], [0.1, ys - 0.22], [0.22, ys]], 8));+ F.sweep(ringPts(0, ys + 0.6, 0.26, 7), hexBar(0.07, 0.14), { closed: true });+ });+ A.at(M.T(0, 0, 0.5), () => A.slab([[-pitch / 2, ys], [-pitch * 0.46, ys], ...arc, [pitch * 0.46, ys], [pitch / 2, ys], [pitch / 2, y1], [-pitch / 2, y1]], [], -0.18, 0.18, { noOuterSides: true }));+ F.box(-pitch / 2, pitch / 2, y0 - 0.2, y0 + 0.02, 0, 0.72, { s: 0.92 });+ F.at(M.T(0, 0, 0.72), () => { F.sweep([[-pitch / 2, y0 - 0.1], [pitch / 2, y0 - 0.1]], stringSection(0.26, 0.22), { caps: false }); F.sweep([[-pitch / 2, y1], [pitch / 2, y1]], stringSection(0.3, 0.3), { caps: false }); });+ F.at(M.T(0, 0, 0.72), () => addParapet(F, -pitch / 2, pitch / 2, y1 + 0.12, 1.05, 0.2, { pitch: 1.1, nh: 8 }));+ return set;+}+// One face of a belfry stage, shared by all eight faces of the two towers+function buildBelfryFace() {+ const set = mkBatches(), { A, F, L } = set, hw = 7, zf = 7, y0 = WF.topY, yT = P.towerTop - 2.6;+ const lw = 3.05, lx = 3.3, sill = y0 + 4.2, ys = y0 + 13.6, rise = 4.1, holes = [];+ for (const s of [-1, 1]) holes.push(archOutline(lw, sill, ys, rise, 7).map((p) => [s * lx + p[0], p[1]]));+ A.at(M.T(0, 0, zf), () => A.slab([[-hw, y0], [hw, y0], [hw, yT], [-hw, yT]], holes, -0.9, 0, { noOuterSides: true, noBack: true, sr: 0.66 }));+ // The bell chamber: a floor behind the openings, and a bell hanging on each side of the frame+ F.box(-hw + 0.8, hw - 0.8, sill - 0.5, sill - 0.2, zf - 5, zf - 0.8, {});+ set.I.at(M.T(0, sill + 3.2, zf - 2.5), () => set.I.lathe([[0.06, 1.72], [0.36, 1.62], [0.5, 1.12], [0.56, 0.7], [0.84, 0.12], [0.92, 0]], 10));+ // Behind the bells the oak frame that carries them closes the view, so no sky shows through the tower+ set.O.face([[-4.2, sill - 0.3, zf - 4], [4.2, sill - 0.3, zf - 4], [4.2, yT, zf - 4], [-4.2, yT, zf - 4]], 0.16);+ for (const s of [-1, 1]) {+ F.at(M.T(s * lx, 0, zf - 0.45), () => {+ F.sweep(pointedArc(lw, rise, 6).map((p) => [p[0], p[1] + ys]), rollSection(0.2, 0.7, 1, 0.04, 2), { caps: false });+ F.sweep([[0, sill - 0.05], [0, ys + 0.1]], traceryBar(0.2, 0.42), {});+ const lw2 = (lw - 0.1) / 2;+ for (const t of [-1, 1]) F.sweep(pointedArc(lw2, lw2 * 0.9, 4).map((p) => [t * lw2 / 2 + p[0], ys + 0.1 + p[1]]), hexBar(0.14, 0.34), { caps: false });+ F.sweep(ringPts(0, ys + rise * 0.62, 0.48, 10), hexBar(0.14, 0.34), { closed: true });+ });+ // Louvres: lead-clad boards set across the opening and tilted to throw the rain outward, dark air between them+ L.at(M.T(s * lx, 0, zf - 0.45), () => {+ const slope = 0.42;+ for (let y = sill + 0.2; y < ys + rise * 0.3; y += 0.5) {+ const h = y <= ys ? lw / 2 : (lw / 2) * Math.sqrt(Math.max(0, 1 - Math.pow((y - ys) / rise, 2)));+ L.face([[-h, y, 0.3], [h, y, 0.3], [h, y + slope, -0.3], [-h, y + slope, -0.3]]);+ L.face([[h, y - 0.05, 0.3], [-h, y - 0.05, 0.3], [-h, y + slope - 0.05, -0.3], [h, y + slope - 0.05, -0.3]]);+ }+ });+ F.at(M.T(s * lx, 0, zf), () => {+ F.sweep(pointedArc(lw + 0.5, rise + 0.3, 6).map((p) => [p[0], p[1] + ys]), hexBar(0.24, 0.28), { caps: false });+ addGable(F, lw + 0.9, 3.1, 0.03, 0.18, ys + rise * 0.5, { size: 0.42, finial: 0.8, crockets: 3, lod: 0 });+ });+ }+ F.at(M.T(0, 0, zf), () => {+ F.sweep([[-hw, y0 + 0.2], [hw, y0 + 0.2]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, sill - 0.3], [hw, sill - 0.3]], stringSection(0.32, 0.28), { caps: false });+ F.sweep([[-hw, yT - 0.2], [hw, yT - 0.2]], stringSection(0.4, 0.44), { caps: false });+ for (let i = 0; i < 12; i++) { const x = -hw + (i + 0.5) * 2 * hw / 12; F.box(x - 0.14, x + 0.14, yT - 0.78, yT - 0.42, 0, 0.3, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zf - 0.2), () => addParapet(F, -hw, hw, yT, 1.2, 0.28, { pitch: 1.4, nh: 8 }));+ return set;+}+// Octagonal corner turret and its great pinnacle, reaching the full height of the tower+function buildTowerCorner() {+ const set = mkBatches(), { A, F } = set, y0 = WF.topY, yT = P.towerTop - 1.3, R = 1.72;+ // The turret grows out of the corner on a tapering corbel, closed underneath; its lower part sits inside the corner pier+ A.lathe([[0.04, y0 - 4.2], [R * 0.66, y0 - 2.6], [R, y0 - 1], [R, yT]], 8, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < 8; i++) {+ const a = i * 45 + 22.5, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ F.at(M.of(M.Ry(a), M.T(0, 0, ap)), () => {+ if (i % 2 === 0) for (const k of [0, 1]) {+ const yb = y0 + 1 + k * 11;+ F.sweep(pointedArc(side * 0.72, 0.9, 4).map((p) => [p[0], p[1] + yb + 8.2]), hexBar(0.1, 0.12), { caps: false });+ for (const s of [-1, 1]) F.sweep([[s * side * 0.36, yb], [s * side * 0.36, yb + 8.2]], hexBar(0.1, 0.12), { caps: false });+ F.shade(0.55, () => F.face([[-side * 0.36, yb, 0.01], [side * 0.36, yb, 0.01], [side * 0.36, yb + 8.2, 0.01], [0, yb + 9.05, 0.01], [-side * 0.36, yb + 8.2, 0.01]]));+ }+ });+ }+ F.lathe([[R, y0 + 12.3], [R + 0.28, y0 + 12.5], [R + 0.28, y0 + 12.66], [R, y0 + 12.82]], 8, { faceted: true, phase: PI / 8 });+ F.lathe([[R, yT - 0.2], [R + 0.36, yT], [R + 0.36, yT + 0.26], [R, yT + 0.42]], 8, { faceted: true, phase: PI / 8 });+ return set;+}+function buildWestFront() {+ const gWest = group('West_Front');+ gWest.position.set(LAY.xW, 0, 0); gWest.rotation.y = rad(90);+ const set = mkBatches(), { A, F, L, G, D } = set, hw = WF.halfW, yTop = WF.topY, tcx = 14.5;+ // Real recessed lights flanking the tower windows, cut through the face of the wall+ const towerRecess = [];+ for (const s of [-1, 1]) for (const d of [-1, 1]) towerRecess.push(archOutline(1.5, WIN.cSill + 0.6, WIN.cSp - 0.6, 1.8, 6).map((p) => [s * tcx + d * 3.55 + p[0], p[1]]));+ // ---- The great wall with real openings: doors and rose ----+ const cP = WF.portalC, sP = WF.portalS;+ const doorHole = (x, w, h) => [[x - w / 2, 0.02], [x + w / 2, 0.02], [x + w / 2, h], [x - w / 2, h]];+ const wallOutline = [[-hw, 0], [-sP.x - sP.w0 / 2, 0], [-sP.x - sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, 0], [-cP.w0 / 2, 0], [-cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, 0], [sP.x - sP.w0 / 2, 0], [sP.x - sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]];+ A.slab(wallOutline, [ringPts(0, WF.roseY, WF.roseR, 40), ...towerRecess], -WF.wallT, 0, { noOuterSides: true, sr: 0.62 });+ towerRecess.forEach((o) => addRecess(A, F, o, 0.5));+ // Seven centuries of rain under every ledge of the front, damp rising from the ground+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.02, 3.2);+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.02, 3.6);+ addStain(D, -hw, hw, WF.galY - 0.42, 0.02, 3.4);+ addStain(D, -hw, hw, WF.kingTop + 0.02, 0.02, 2.6, 'drip');+ // damp on the wall only: the door openings are left clear+ for (const span of [[-hw, -sP.x - sP.w0 / 2], [-sP.x + sP.w0 / 2, -cP.w0 / 2], [cP.w0 / 2, sP.x - sP.w0 / 2], [sP.x + sP.w0 / 2, hw]]) addStain(D, span[0], span[1], 5.2, 0.03, 5.2, 'ground');+ // Each ledge throws a soft shadow on the wall just below it; soot gathers where the rain cannot reach+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.03, 0.8, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.03, 1.2, 'soft');+ addStain(D, -hw, hw, WF.galY - 0.42, 0.03, 1.0, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.62, 0.04, 2.4, 'soot');+ for (const s of [-1, 1]) addStain(D, s * tcx - WIN.cW / 2 - 0.4, s * tcx + WIN.cW / 2 + 0.4, WIN.cSill - 0.12, 0.02, 3.4);+ // ---- Rose: moulded ring, tracery, spandrels, relieving arch ----+ place('Great_Rose_Tracery', roseTracery(), MAT.fine, { pos: [0, WF.roseY, -0.4], parent: gWest });+ F.at(M.T(0, WF.roseY, 0), () => {+ F.at(M.T(0, 0, 0), () => {+ const ring = traceryBar(0.46, 0.58);+ F.sweep(ringPts(0, 0, WF.roseR + 0.32, 40), ring, { closed: true, z: 0.14 });+ F.sweep(ringPts(0, 0, WF.roseR + 0.78, 44), rollSection(0.2, 0.3, 1, 0.04, 2), { closed: true, z: 0.1 });+ });+ });+ F.at(M.T(0, 0, 0.03), () => {+ // spandrels round the rose: plain stone, each with a real pierced roundel carved in its corner+ const R2 = WF.roseR + 0.72, ry = WF.roseY, sq = 6.6;+ for (const [sx, sy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {+ // corner of the square -> along its edge -> round the rose -> along the other edge+ const arcPts = []; for (let i = 0; i <= 8; i++) { const a = i / 8 * PI / 2; arcPts.push([sx * R2 * Math.cos(a), sy * R2 * Math.sin(a)]); }+ const poly = [[sx * sq, sy * sq], [sx * sq, 0], ...arcPts, [0, sy * sq]].map((p) => [p[0], p[1] + ry]);+ F.slab(area2(poly) > 0 ? poly : poly.slice().reverse(), [], -0.04, 0, { noOuterSides: true, noBack: true });+ const cx = sx * (sq - 1.02), cy = ry + sy * (sq - 1.02);+ F.sweep(ringPts(cx, cy, 0.76, 14), traceryBar(0.13, 0.2), { closed: true, z: 0.05 });+ const foil = []; for (let i = 0; i < 16; i++) { const a = i * TAU / 16, R = 0.68 * (0.62 + 0.32 * Math.abs(Math.cos(a * 2))); foil.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.07, 0.14), { closed: true, z: 0.05, crease: 40 });+ }+ });+ F.at(M.T(0, 0, 0.14), () => { F.sweep([[-6.6, WF.roseY - 6.6], [6.6, WF.roseY - 6.6]], stringSection(0.3, 0.26), { caps: false }); F.sweep([[-6.6, WF.roseY + 6.6], [6.6, WF.roseY + 6.6]], stringSection(0.3, 0.26), { caps: false }); });+ // ---- Tower lower stages: paired windows with hood and gable, string courses ----+ const cw = WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = WIN.cRise;+ for (const s of [-1, 1]) {+ F.at(M.T(s * tcx, 0, 0.32), () => {+ addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 8), rollSection(0.24, 0.5, 1, 0.04, 2), { closed: true, crease: 40, z: -0.14 });+ F.sweep(pointedArc(cw + 0.8, cRise + 0.4, 7).map((p) => [p[0], p[1] + cSp]), hexBar(0.26, 0.3), { z: 0.1, caps: false });+ addGable(F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 4, lod: 0 });+ });+ F.at(M.T(s * tcx, 0, 0.02), () => { F.sweep([[-6.1, WF.kingTop + 0.24], [6.1, WF.kingTop + 0.24]], stringSection(0.3, 0.22), { caps: false }); });+ }+ // ---- Buttresses of the front: one continuous pier between the portals, narrowing as it climbs through the kings and+ // the gallery; the outer corners of the towers carry a single diagonal pier instead of two that would collide ----+ const xPier = [-7.5, 7.5], lowPanel = { k: 0.6, b: 1.1, t: 7.2, r: 0.7 };+ xPier.forEach((x, i) => {+ const m = M.T(x, 0, 0);+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: 0, y1: 11.6, w: 1.05, f: 3.6, panel: lowPanel }, { y0: 12.9, y1: 18.4, w: 0.98, f: 2.7 }, { y0: 19.7, y1: 19.72, w: 0.92, f: 1.2, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 3.6), () => { F.box(-1.17, 1.17, 0, 0.74, -3.7, 0.12, { noBot: true }); addNicheFrame(F, 0, 6.1, 11.0, 1.42); });+ // above the kings the pier stands back on the gallery's roof+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: WF.kingTop + 0.1, y1: 38.8, w: 0.92, f: 1.26 }, { y0: 40, y1: yTop + 0.1, w: 0.8, f: 0.96, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 1.26), () => addNicheFrame(F, 0, 31, 37.4, 1.3));+ D.at(M.T(x, 0, 3.6), () => { addStain(D, -1.05, 1.05, 11.4, 0.02, 2.2, 'drip'); addStain(D, -1.05, 1.05, 4.6, 0.03, 4.4, 'ground'); addStain(D, -1.05, 1.05, 11.45, 0.03, 0.7, 'soft'); });+ placeFigure('Buttress_Saint' + i, SAINTS[i + 3], { pos: [x, 6.2, 3.98], rot: [0, -90, 0], scale: 1.42, parent: gWest });+ place('Facade_Gargoyle' + i, gargoyle(i % 3), MAT.fine, { pos: [x, 17.2, 2.55], rot: [0, -90, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ placeFigure('Upper_Saint' + i, SAINTS[i + 5], { pos: [x, 31.1, 1.62], rot: [0, -90, 0], scale: 1.32, parent: gWest, lod: 0 });+ });+ for (const s of [-1, 1]) {+ const k = Math.SQRT1_2, z = 3.98, cx = s * (hw + k * z);+ placeFigure('Corner_Saint' + s, SAINTS[s > 0 ? 3 : 5], { pos: [cx, 6.2, k * z], rot: [0, -90 + s * 45, 0], scale: 1.42, parent: gWest });+ place('Corner_Gargoyle' + s, gargoyle(s > 0 ? 1 : 2), MAT.fine, { pos: [s * (hw + k * 2.65), 26.6, k * 2.65], rot: [0, -90 + s * 45, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ }+ // closing blocks where the gallery of kings dies into the corner piers+ for (const s of [-1, 1]) A.box(Math.min(s * 19.9, s * 20.6), Math.max(s * 19.9, s * 20.6), WF.kingY - 0.5, WF.kingTop + 0.12, 0, 1.3, { noBot: true });+ placeSet('West_Wall', set, [0, 0, 0], 0, gWest);+ // ---- Glass ----+ const roseWest = makeRoseGlass(WF.roseR, 101);+ SHARED.roseGlass = roseWest;+ placeGlass('Great_Rose', roseWest, [0, WF.roseY, -0.4], 180, gWest, true);+ const twinGlass = SHARED.cleres;+ for (const s of [-1, 1]) place('TowerWindow_Out' + s, glassOutside(twinGlass[s > 0 ? 0 : 1]), MAT.glassOutWin, { pos: [s * tcx, 0, 0.14], rot: [0, 180, 0], parent: gWest, scale: [1, 1, -1] });+ // ---- Three portals: a bishop, a crowned king, a veiled queen, an apostle with his key, a smiling angel;+ // the Virgin and Child on the central door post, Christ enthroned above her ----+ const pC = buildPortal(cP, ['apostle', 'bishop', 'queen', 'king', 'apostle', 'king', 'angel', 'bishop', 'queen', 'apostle'], { main: 'seated', side: ['angel', 'queen'], trumeau: 'virgin', trumeauScale: 1.4 });+ const pS = buildPortal(Object.assign({}, sP, { x: 0 }), ['queen', 'bishop', 'apostle', 'king', 'angel', 'apostle', 'king', 'queen'], { main: 'seatedVirgin', side: ['angel', 'apostle'], trumeau: 'bishop', trumeauScale: 1.24 });+ pC.baseX = cP.x; pS.baseX = 0; SHARED.sidePortal = pS;+ placePortal(gWest, 'Portal_Central', cP, pC);+ placePortal(gWest, 'Portal_South', Object.assign({}, sP, { x: -sP.x }), pS, { light: 'scene' });+ placePortal(gWest, 'Portal_North', Object.assign({}, sP, { x: sP.x }), pS, { light: 'scene' });+ // ---- Gallery of Kings ----+ const nK = P.kings, pitch = 2 * 20.1 / nK, niche = buildKingNiche(pitch), kinds = ['kingA', 'kingB'];+ for (let i = 0; i < nK; i++) {+ const x = (i + 0.5 - nK / 2) * pitch;+ placeSet('King_Niche' + i, niche, [x, 0, 0], 0, gWest);+ placeFigure('King' + i, kinds[i % 2], { pos: [x, WF.kingY + 0.04, 0.62], rot: [0, -90 + (i % 4 - 1.5) * 7, 0], scale: 1.7, parent: gWest, mirror: i % 2 === 1 });+ }+ // ---- High gallery ----+ const nG = 20, gp = 2 * 19.75 / nG, gb = buildGalleryBay(gp);+ for (let i = 0; i < nG; i++) placeSet('Gallery' + i, gb, [(i + 0.5 - nG / 2) * gp, 0, 0], 0, gWest);+ // ---- Central gable rising over the gallery, closing the nave roof ----+ const front = mkBatches(), yG0 = yTop + 1.2, yR = LAY.ridge + 1.2, gW = 2 * (zn + 1.5);+ front.A.at(M.T(0, 0, -1.2), () => front.A.slab([[-gW / 2, yG0 - 2], [gW / 2, yG0 - 2], [gW / 2, yG0], [0, yR + 1.6], [-gW / 2, yG0]], [ringPts(0, yG0 + 1.8, 1.25, 12)], -0.5, 0.5, { sr: 0.5 }));+ front.F.at(M.T(0, 0, -0.7), () => {+ addGable(front.F, gW + 0.2, yR + 1.6 - yG0, 0.04, 0.26, yG0, { size: 0.86, finial: 0.1, crockets: 7, noField: true, lod: 1 });+ front.F.sweep(ringPts(0, yG0 + 1.8, 1.32, 14), traceryBar(0.26, 0.44), { closed: true });+ // Blind arcade stepping up under the rake+ const nA = 7; for (let i = 0; i < nA; i++) { const x = (i - (nA - 1) / 2) * 1.72, top = yG0 + 0.3 + (gW / 2 - Math.abs(x) - 1) * (yR + 1.6 - yG0) / (gW / 2) * 0.7; if (Math.abs(x) < 1.7) continue; front.F.sweep([[x - 0.62, yG0], [x - 0.62, top], [x, top + 0.75], [x + 0.62, top], [x + 0.62, yG0]], hexBar(0.14, 0.16), { caps: false, crease: 20 }); }+ });+ front.G.at(M.T(0, yR + 1.62, -0.5), () => addFinial(front.G, 2.2, 1));+ placeSet('West_Gable', front, [0, 0, 0], 0, gWest);+ place('West_Gable_Glass', makeLancetRoundel(1.25), MAT.glassOut, { pos: [0, yG0 + 1.8, -0.75], parent: gWest });+ // ---- Towers: shell, belfry faces, corner turrets, lead cap ----+ // One tower shell (side and rear walls of the lower stages), built outward towards +x; the other tower mirrors it+ const shell = mkBatches(), tw = 7;+ // The outer side of the tower: real recessed arcades in two tiers, either side of its windows+ const sideM = M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), sideRecess = [];+ for (const d of [-1, 1]) {+ sideRecess.push(archOutline(1.2, 3.4, 16.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ sideRecess.push(archOutline(1.2, 26.4, 37.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ }+ shell.A.at(sideM, () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], sideRecess, -0.8, 0, { noOuterSides: true, noBack: true, sr: 0.62 }));+ shell.A.at(sideM, () => shell.F.at(sideM, () => sideRecess.forEach((o) => addRecess(shell.A, shell.F, o, 0.52))));+ shell.D.at(sideM, () => { for (const y of [19.2, WF.kingTop - 0.34, WF.galY - 0.34]) { addStain(shell.D, -tw, tw, y, 0.02, 3.2); addStain(shell.D, -tw, tw, y, 0.03, 0.8, 'soft'); } addStain(shell.D, -tw, tw, 5.2, 0.03, 5.2, 'ground'); });+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(180), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(-90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ // string courses and tall blind windows on the outer side+ shell.F.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => {+ for (const y of [1.0, 19.6, WF.kingTop, WF.galY]) shell.F.sweep([[-tw, y], [tw, y]], stringSection(0.3, 0.26), { caps: false });+ addTracery(shell.F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ shell.F.sweep(archOutline(cw, cSill, cSp, cRise, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ addGable(shell.F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 3, lod: 0 });+ addTracery(shell.F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 });+ shell.F.sweep(archOutline(...windowArgs.aisle, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ });+ // The tower's two outer corners: one diagonal pier each, from the ground to the foot of the turret it carries+ const cornerStages = [{ y0: 0, y1: 13, w: 2.0, f: 3.6 }, { y0: 14.4, y1: 27, w: 1.9, f: 2.8 }, { y0: 28.4, y1: yTop - 1, w: 1.78, f: 2.05 }];+ for (const [zc, turn] of [[0, 45], [-2 * tw, 135]]) {+ const m = M.of(M.T(tw, 0, zc), M.Ry(turn));+ shell.A.at(m, () => shell.F.at(m, () => addPier(shell, cornerStages, { back: 2, flat: true })));+ shell.F.at(m, () => shell.F.box(-2.14, 2.14, 0, 0.74, -2, 3.74, { noBot: true }));+ shell.D.at(M.of(m, M.T(0, 0, 3.6)), () => { addStain(shell.D, -2, 2, 13, 0.02, 3.2); addStain(shell.D, -2, 2, 13.02, 0.03, 0.8, 'soft'); addStain(shell.D, -2, 2, 5, 0.03, 4.3, 'ground'); });+ if (zc === 0) shell.F.at(M.of(m, M.T(0, 0, 3.6)), () => addNicheFrame(shell.F, 0, 6.1, 11, 1.42));+ }+ for (const s of [-1, 1]) {+ const cx = s * tcx;+ placeSet('Tower_Shell', shell, [cx, 0, 0], 0, gWest, { scale: [s, 1, 1] });+ // Set just proud of the wall face: these towers are solid here, so the glass is seen from outside only+ place('Tower_SideGlass_High' + s, glassOutside(twinGlass[2]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ place('Tower_SideGlass_Low' + s, glassOutside(SHARED.stories[1]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ }+ const face = buildBelfryFace(), corner = buildTowerCorner();+ const cap = new Batch(TILE.lead), capG = new Batch(TILE.gold);+ cap.lathe([[5.9, P.towerTop - 2.5], [0.2, P.towerTop + 4.8]], 8, { faceted: true, phase: PI / 8 });+ capG.at(M.T(0, P.towerTop + 4.7, 0), () => addFinial(capG, 2.4, 1));+ for (const s of [-1, 1]) {+ const gT = group('Tower_' + (s > 0 ? 'North' : 'South'), gWest), cx = s * tcx, cz = -tw;+ for (let i = 0; i < 4; i++) {+ placeSet('Belfry_Face' + i, face, [cx, 0, cz], i * 90, gT);+ const ax = cx + (i === 0 || i === 3 ? 1 : -1) * tw, az = cz + (i < 2 ? 1 : -1) * tw;+ placeSet('Tower_Turret' + i, corner, [ax, 0, az], i * 90, gT);+ // the great corner pinnacles carry the tower to its full height+ const yT = P.towerTop - 1.3 + 0.4, hP = P.spireTip - yT;+ place('Tower_Pinnacle' + i, largePinnacle(), MAT.fine, { pos: [ax, yT, az], scale: [1.16, hP / 10, 1.16], parent: gT });+ }+ place('Tower_Cap_Lead', cap, MAT.lead, { pos: [cx, 0, cz], parent: gT });+ place('Tower_Cap_Gilt', capG, MAT.gold, { pos: [cx, 0, cz], parent: gT });+ }+ // ---- Inner face: blind niches with statues, a gallery, the back of the doors ----+ const inner = mkBatches();+ inner.F.at(M.of(M.T(0, 0, -WF.wallT), M.Ry(180)), () => {+ inner.F.sweep([[-13.1, 10.2], [13.1, 10.2]], stringSection(0.34, 0.34), { caps: false });+ inner.F.sweep([[-6.5, P.triforiumSill], [6.5, P.triforiumSill]], stringSection(0.34, 0.3), { caps: false });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) addNicheFrame(inner.F, s * (3.65 + i * 1.5), 3.2 + 0, 8.8, 1.2);+ inner.F.sweep(ringPts(0, WF.roseY, WF.roseR + 0.3, 32), traceryBar(0.42, 0.5), { closed: true });+ addBlindArcade(inner.F, -6.5, 6.5, 11, 15.2, 8, { bar: 0.14, depth: 0.2 });+ });+ placeSet('West_InnerFace', inner, [0, 0, 0], 0, gWest);+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) placeFigure('Inner_Saint_' + (s > 0 ? 'N' : 'S') + i, SAINTS[i + (s > 0 ? 1 : 6)], { pos: [s * (3.65 + i * 1.5), 3.25, -WF.wallT - 0.28], rot: [0, 90, 0], scale: 1.32, parent: gWest, indoor: true });+}+function makeLancetRoundel(r) { const g = new Glass(55); g.raw(ringPts(0, 0, r + 0.05, 20), 'lead', -0.4, 0); gOculus(g, 0, 0, r, { ground: 'cobalt', inner: 'gold' }); return g.b; }++// ---- Transept fronts: one portal, a smaller rose, lancets, gable, corner turrets ----------+function buildTransFront(gT, s) {+ const fr = frame(s > 0 ? 0 : 180, LAY.xc, 0), hw = zaw + 0.9, zEnd = zn + (P.transeptBays + 1) * BAY, yTop = P.eaves;+ const set = TRANS.set || (TRANS.set = (() => {+ const q = mkBatches();+ const { A, F, G } = q, rY = 28.6, R = 4.5, w0 = WF.portalS.w0, dh = WF.portalS.ys - 0.9;+ const at = M.T(0, 0, zEnd);+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-hw, 0], [-w0 / 2, 0], [-w0 / 2, dh], [w0 / 2, dh], [w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]], [ringPts(0, rY, R, 32)], -0.9, 0, { noOuterSides: true, sr: 0.62 }));+ F.at(M.of(at, M.T(0, rY, 0.9)), () => { F.sweep(ringPts(0, 0, R + 0.3, 32), traceryBar(0.4, 0.5), { closed: true }); F.sweep(ringPts(0, 0, R + 0.72, 36), rollSection(0.18, 0.26, 1, 0.03, 2), { closed: true }); });+ q.D.at(M.of(at, M.T(0, 0, 0.9)), () => { for (const [a, c] of [[-hw, -12.3], [-6.9, 6.9], [12.3, hw]]) { addStain(q.D, a, c, 17.2, 0.02, 3.2); addStain(q.D, a, c, 17.2, 0.03, 0.8, 'soft'); } addStain(q.D, -hw, hw, 22.6, 0.02, 2.8, 'drip'); addStain(q.D, -hw, hw, 22.6, 0.03, 0.7, 'soft'); addStain(q.D, -hw, -w0 / 2, 5.2, 0.03, 5.2, 'ground'); addStain(q.D, w0 / 2, hw, 5.2, 0.03, 5.2, 'ground'); });+ // Row of lancets beneath the rose+ F.at(M.of(at, M.T(0, 0, 0.9)), () => {+ F.sweep([[-hw, 0.8], [hw, 0.8]], stringSection(0.42, 0.36), { caps: false });+ // This string runs up to the hood of each window head and stops there: it never crosses the tracery+ const hood = 9.6 - 1.62, outerHood = 9.6 + 1.62;+ F.sweep([[-hood, 17.4], [hood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ for (const t of [-1, 1]) F.sweep(t > 0 ? [[outerHood, 17.4], [hw, 17.4]] : [[-hw, 17.4], [-outerHood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ F.sweep([[-hw, 22.8], [hw, 22.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -8, 8, 18, 20.8, 8, { bar: 0.16, depth: 0.2 });+ for (const x of [-9.6, 9.6]) F.at(M.T(x, 0, 0.1), () => { addTracery(F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 }); F.sweep(archOutline(...windowArgs.aisle, 7), hexBar(0.22, 0.42), { closed: true, crease: 40, z: -0.1 }); });+ });+ // The inner face of the end wall: tiers of blind arcading and string courses+ F.at(M.of(at, M.Ry(180)), () => {+ F.sweep([[-zaw, 11], [zaw, 11]], stringSection(0.3, 0.26), { caps: false });+ F.sweep([[-zaw, 16.8], [zaw, 16.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -5.6, 5.6, 1.2, 7.8, 5, { bar: 0.16, depth: 0.2 });+ addBlindArcade(F, -zn, zn, 11.2, 14.8, 8, { bar: 0.12, depth: 0.16 });+ addBlindArcade(F, -zn, zn, 17, 20.6, 8, { bar: 0.12, depth: 0.16 });+ });+ // Gable closing the arm's roof, in front of the lead+ const yG = yTop, gW = 2 * (zn + 1.6), yR = LAY.ridge + 1;+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-gW / 2, yG], [gW / 2, yG], [0, yR]], [ringPts(0, yG + 3.4, 1.5, 12)], -0.9, 0, { noOuterSides: true, sr: 0.5 }));+ F.at(M.of(at, M.T(0, 0, 0.9)), () => { addGable(F, gW + 0.2, yR - yG, 0.02, 0.24, yG, { size: 0.82, finial: 0.1, crockets: 7, noField: true, lod: 1 }); F.sweep(ringPts(0, yG + 3.4, 1.55, 14), traceryBar(0.24, 0.42), { closed: true }); });+ G.at(M.of(at, M.T(0, yR, 1.12)), () => addFinial(G, 2.0, 1));+ F.at(M.of(at, M.T(0, 0, 1.0)), () => addParapet(F, -hw, hw, yTop - 0.1, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Each corner of the front carries one diagonal pier rising to its pinnacle, tied into both walls+ for (const sx of [-1, 1]) {+ const m = M.of(M.T(sx * hw, 0, zEnd + 0.9), M.Ry(sx * 45));+ A.at(m, () => F.at(m, () => addPier(q, [{ y0: 0, y1: 14.6, w: 1.7, f: 2.7 }, { y0: 15.8, y1: 30.6, w: 1.58, f: 2.05 }, { y0: 31.8, y1: yTop + 1, w: 1.45, f: 1.5 }], { back: 1.7, flat: true })));+ F.at(m, () => F.box(-1.84, 1.84, 0, 0.74, -1.7, 2.84, { noBot: true }));+ F.at(M.of(m, M.T(0, 0, 2.05)), () => addNicheFrame(F, 0, 16.2, 22, 1.5));+ q.D.at(M.of(m, M.T(0, 0, 2.7)), () => { addStain(q.D, -1.7, 1.7, 14.6, 0.02, 3); addStain(q.D, -1.7, 1.7, 14.62, 0.03, 0.8, 'soft'); addStain(q.D, -1.7, 1.7, 5, 0.03, 4.3, 'ground'); });+ }+ return q;+ })());+ placeSet('Transept_Front', set, [fr.ox, 0, fr.oz], fr.th, gT);+ // Portal (the same carving as the west side portals), statue niches, glass+ const gPortal = group('Transept_Portal', gT);+ gPortal.position.set(...fr.pos(0, 0, zEnd + 0.9)); gPortal.rotation.y = rad(fr.th);+ placePortal(gPortal, 'Portal_Transept', { x: 0 }, SHARED.sidePortal, { light: 'all' });+ place('Transept_Rose_Tracery', roseTracery(), MAT.fine, { pos: fr.pos(0, 28.6, zEnd + 0.45), rot: fr.rot(0), scale: [4.5 / WF.roseR, 4.5 / WF.roseR, 1], parent: gT });+ for (const sx of [-1, 1]) place('Transept_Pinnacle', largePinnacle(), MAT.fine, { pos: fr.pos(sx * hw, yTop + 1, zEnd + 0.9), scale: [1.05, 1.1, 1.05], parent: gT });+ placeGlass('Transept_Rose', SHARED.roseGlass, fr.pos(0, 28.6, zEnd + 0.45), fr.th + 180, gT, false, 4.5 / WF.roseR);+ const low = SHARED.stories[0];+ for (const sx of [-1, 1]) {+ // Aisle-end windows: the same panes on the inner and outer faces of the end wall+ place('Transept_EndWindow_In', low, MAT.glassIn, { pos: fr.pos(sx * 9.6, 0, zEnd - 0.01), rot: fr.rot(180), parent: gT });+ place('Transept_EndWindow_Out', glassOutside(low), MAT.glassOutWin, { pos: fr.pos(sx * 9.6, 0, zEnd + 0.92), rot: fr.rot(180), scale: [1, 1, -1], parent: gT });+ const dz = Math.SQRT1_2 * 2.37;+ placeFigure('Transept_Corner_Saint' + sx, SAINTS[sx > 0 ? 2 : 4], { pos: fr.pos(sx * (hw + dz), 16.25, zEnd + 0.9 + dz), rot: fr.rot(-90 + sx * 45), scale: 1.42, parent: gT, lod: 0 });+ }+}+const TRANS = {};++// ---- The east end: raised sanctuary, high altar with a gilded retable, oak choir stalls ------+function buildSanctuary() {+ const g = group('Sanctuary'), set = mkBatches(), { F, G, O } = set, { xa, xChoirW } = LAY;+ // Three broad steps and the altar, all moulded+ for (let i = 0; i < 3; i++) { const r = 5 - i * 0.7; F.at(M.T(xa + 1, 0, 0), () => F.lathe([[r, i * 0.2], [r, (i + 1) * 0.2], [0.01, (i + 1) * 0.2]], 14, { faceted: true, phase: PI / 14 })); }+ const y0 = 0.6, ax = xa + 1;+ F.at(M.T(ax, y0, 0), () => {+ F.box(-0.8, 0.8, 0, 1.0, -1.7, 1.7, {});+ F.box(-0.92, 0.92, 1.0, 1.12, -1.84, 1.84, { sb: 0.5 });+ for (const s of [-1, 1]) for (const t of [-1, 0, 1]) F.at(M.T(s * 0.82, 0, t * 1.1), () => F.lathe([[0.12, 0], [0.07, 0.12], [0.07, 0.88], [0.13, 1.0]], 6));+ });+ // A carved retable behind the altar: five stone niches under gables, each with its figure, only the arches gilded+ const retM = M.of(M.T(ax - 1.0, y0 + 1.12, 0), M.Ry(90));+ F.at(retM, () => {+ F.box(-2.46, 2.46, -0.26, 0, -0.2, 0.14, {});+ for (let i = -2; i <= 2; i++) {+ const x = i * 0.96, h = i === 0 ? 2.2 : 1.56;+ F.box(x - 0.47, x + 0.47, 0, h - 0.3, -0.2, -0.1, {});+ addGable(F, 0.96, i === 0 ? 0.8 : 0.66, 0.04, 0.06, h - 0.32, { size: 0.15, crockets: 2, finial: 0.24, solid: true, lod: 0 });+ F.at(M.T(x - 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ if (i === 2) F.at(M.T(x + 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ F.at(M.T(x - 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ if (i === 2) F.at(M.T(x + 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ }+ });+ G.at(retM, () => { for (let i = -2; i <= 2; i++) { const x = i * 0.96, h = i === 0 ? 2.2 : 1.56; G.sweep(pointedArc(0.82, 0.52, 4).map((p) => [x + p[0], h - 0.84 + p[1]]), hexBar(0.04, 0.05), { z: 0.03, caps: false }); } });+ const altarSaints = [['apostle', 0.56], ['bishop', 0.56], ['seatedVirgin', 0.74], ['king', 0.56], ['queen', 0.56]];+ altarSaints.forEach((q, n) => placeFigure('Retable_Figure' + n, q[0], { pos: [ax - 1.04, y0 + 1.14, -(n - 2) * 0.96], rot: [0, 0, 0], scale: q[1], parent: g, indoor: true, lod: q[0] === 'seatedVirgin' ? 1 : 0 }));+ // Candlesticks+ for (const t of [-1.5, -0.9, -0.3, 0.3, 0.9, 1.5]) G.at(M.T(ax - 0.2, y0 + 1.12, t), () => { G.lathe([[0.1, 0], [0.04, 0.06], [0.025, 0.42], [0.06, 0.46], [0.03, 0.5]], 6); F.at(M.T(ax - 0.2, y0 + 1.62, t), () => F.lathe([[0.022, 0], [0.022, 0.34], [0.002, 0.36]], 5, { s: 1.6 })); });+ // Choir stalls: stepped oak benches, moulded backs and a carved canopy rail+ const x0 = xa + 5.5, x1 = xChoirW - 1.2;+ for (const s of [-1, 1]) {+ const z0 = s * 5.0;+ O.box(x0, x1, 0, 0.32, z0 - 0.9, z0 + 0.9, { s: 0.42 });+ O.box(x0, x1, 0.32, 0.5, z0 + (s > 0 ? 0.0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.46 });+ O.box(x0, x1, 0, 2.7, z0 + (s > 0 ? 0.8 : -0.9), z0 + (s > 0 ? 0.9 : -0.8), { s: 0.4 });+ const n = Math.floor((x1 - x0) / 1.5);+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * (x1 - x0) / n;+ O.box(x - 0.045, x + 0.045, 0, 0.98, z0 + (s > 0 ? 0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.5 });+ O.box(x - 0.03, x + 0.03, 0.98, 2.72, z0 + (s > 0 ? 0.74 : -0.8), z0 + (s > 0 ? 0.8 : -0.74), { s: 0.48 });+ O.box(x - 0.08, x + 0.08, 2.72, 3.28, z0 + (s > 0 ? 0.56 : -0.86), z0 + (s > 0 ? 0.86 : -0.56), { s: 0.46, taper: 0.05 });+ }+ O.box(x0, x1, 2.7, 2.84, z0 + (s > 0 ? 0.3 : -0.9), z0 + (s > 0 ? 0.9 : -0.3), { s: 0.44 });+ }+ placeSet('Sanctuary', set, [0, 0, 0], 0, g);+}++// ==================================================================================+// THE GROUND. One named group, separate from the building and left out of its light, so it can simply be deleted+// when the cathedral is placed in a town. All of it stands a few centimetres above y = 0.+// ==================================================================================+const GROUND = { flagY: 0.03, earthY: 0.015, flags: 20, earth: 33, stepUp: 0.14 };+function footprintDistance(x, z) { // signed distance in plan to the outer walls of the building+ const { xa, xW, xTowerE, xc } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const box = (x0, x1, z0, z1) => { const dx = Math.max(x0 - x, x - x1), dz = Math.max(z0 - z, z - z1); return dx <= 0 && dz <= 0 ? Math.max(dx, dz) : Math.sqrt(Math.max(dx, 0) ** 2 + Math.max(dz, 0) ** 2); };+ return Math.min(box(xa, xW, -14, 14), box(xTowerE, xW, -WF.halfW, WF.halfW), box(xc - 14, xc + 14, -zEnd, zEnd), Math.sqrt((x - xa) * (x - xa) + z * z) - (CHEV.zc + CHEV.Rc + 0.5));+}+function buildGround() {+ const g = group('Ground'), { xa, xW, xc, xTowerE } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const fl = new Batch(TILE.flag), ea = new Batch(TILE.earth), st = new Batch(TILE.flag), gr = new Batch(1); gr.noColor = true;+ fl.noVoxel = ea.noVoxel = st.noVoxel = true;+ const wob = (x, z) => 2.2 * Math.sin(x * 0.31 + 1.3) * Math.cos(z * 0.27) + 1.4 * Math.sin(x * 0.13 + z * 0.19);+ const cell3 = (b, x0, z0, s, y) => { const q = [[x0, z0], [x0, z0 + s], [x0 + s, z0 + s], [x0 + s, z0]].map((p) => b.v(p[0], y(p[0], p[1]), p[1], 0, 1, 0, p[0], p[1], 1)); b.quad(q[0], q[1], q[2], q[3]); };+ // Worn flags from the walls out for about 20 m. Their edge breaks up slab by slab on the earth; nothing is left under the church floor+ const flagKeep = (x, z) => { const d = footprintDistance(x, z); return d > -2.5 && d < GROUND.flags + wob(x, z); };+ for (let gx = Math.floor((xa - 62) / 3); gx * 3 < xW + 62; gx++) for (let gz = -24; gz < 24; gz++) {+ const x0 = gx * 3, z0 = gz * 3, d = footprintDistance(x0 + 1.5, z0 + 1.5), lim = GROUND.flags + wob(x0 + 1.5, z0 + 1.5);+ if (Math.abs(d - lim) < 3.2 && d > 5) { for (const [a, c] of [[0, 0], [0, 1.5], [1.5, 0], [1.5, 1.5]]) if (flagKeep(x0 + a + 0.75, z0 + c + 0.75)) cell3(fl, x0 + a, z0 + c, 1.5, () => GROUND.flagY); } else if (flagKeep(x0 + 1.5, z0 + 1.5)) cell3(fl, x0, z0, 3, () => GROUND.flagY);+ }+ // Rough grass and packed earth under and beyond them, its edge dissolving so it sits on any ground plane without a seam+ ea.fade = (x, z) => sstep(GROUND.earth + 8, GROUND.earth - 3, footprintDistance(x, z) - wob(z, x));+ for (let gx = Math.floor((xa - 62) / 4); gx * 4 < xW + 62; gx++) for (let gz = -18; gz < 18; gz++) {+ const x0 = gx * 4, z0 = gz * 4, d = footprintDistance(x0 + 2, z0 + 2);+ if (d > GROUND.flags - 8 && d < GROUND.earth + 11) cell3(ea, x0, z0, 4, () => GROUND.earthY);+ }+ // A step or two up to every door: a landing that runs in to the door between the jambs, and a lower step in front of it+ const steps = (m, half, zF) => st.at(m, () => {+ st.box(-half - 0.2, half + 0.2, GROUND.flagY, GROUND.flagY + 2 * GROUND.stepUp, -0.2, zF + 0.75, { noBot: true });+ st.box(-half - 0.62, half + 0.62, GROUND.flagY, GROUND.flagY + GROUND.stepUp, zF + 0.75, zF + 1.5, { noBot: true });+ });+ const west = M.of(M.T(xW, 0, 0), M.Ry(90)), cP = WF.portalC, sP = WF.portalS, outer = (c) => (c.w0 + 2 * c.orders * c.dx) / 2;+ steps(west, outer(cP), cP.orders * cP.dz);+ for (const s of [-1, 1]) steps(M.of(west, M.T(s * sP.x, 0, 0)), outer(sP), sP.orders * sP.dz);+ for (const s of [0, 180]) steps(M.of(M.T(xc, 0, 0), M.Ry(s), M.T(0, 0, zEnd)), outer(sP), sP.orders * sP.dz);+ // Dark grime where the walls meet the ground: a soft band lying on the flags along every wall foot+ const strip = (ax, az, bx, bz, w) => {+ const R = DECAL_UV.soft, dx = bx - ax, dz = bz - az, l = Math.sqrt(dx * dx + dz * dz) || 1, nx = dz / l, nz = -dx / l, y = GROUND.flagY + 0.012, n = Math.max(1, Math.round(l / 5)), span = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = i / n, c = (i + 1) / n, u0 = R[0] + (i % 3) * span * 0.22, u1 = u0 + span * 0.3, pa = [ax + dx * a, az + dz * a], pc = [ax + dx * c, az + dz * c];+ const q = [gr.v(pa[0], y, pa[1], 0, 1, 0, u0, R[1], 1), gr.v(pa[0] + nx * w, y, pa[1] + nz * w, 0, 1, 0, u0, R[3], 1), gr.v(pc[0] + nx * w, y, pc[1] + nz * w, 0, 1, 0, u1, R[3], 1), gr.v(pc[0], y, pc[1], 0, 1, 0, u1, R[1], 1)];+ gr.quad(q[0], q[1], q[2], q[3]);+ }+ };+ // every strip runs with the wall on its left as seen from above, so its band falls outward+ const loop = (pts, w) => { for (let i = 0; i < pts.length - 1; i++) strip(pts[i][0], pts[i][1], pts[i + 1][0], pts[i + 1][1], w); };+ const hx = WF.halfW, xE = xc + 14, xCE = xc - 14;+ loop([[xW, hx], [xTowerE, hx], [xTowerE, 14], [xE, 14], [xE, zEnd], [xCE, zEnd], [xCE, 14], [xa, 14]], 1.8);+ loop([[xa, -14], [xCE, -14], [xCE, -zEnd], [xE, -zEnd], [xE, -14], [xTowerE, -14], [xTowerE, -hx], [xW, -hx], [xW, hx]], 1.8);+ for (let k = 1; k < P.apseSides; k++) { const f0 = frame(-k * CHEV.d, xa, 0), a = f0.pos(-0.98, 0, BUTTRESS.zB + 0.2), c = f0.pos(0.98, 0, BUTTRESS.zB + 0.2); strip(a[0], a[2], c[0], c[2], 1.3); }+ for (let k = 0; k < P.apseSides; k++) { const r = CHEV.zc + CHEV.Rc + 0.55, n = 5; for (let i = 0; i < n; i++) { const f0 = frame(-(k + 0.5) * CHEV.d, xa, 0), a0 = rad(-108 + i * 216 / n), a1 = rad(-108 + (i + 1) * 216 / n), R2 = CHEV.Rc + 0.55, p = f0.pos(R2 * Math.sin(a0), 0, CHEV.zc + R2 * Math.cos(a0)), q = f0.pos(R2 * Math.sin(a1), 0, CHEV.zc + R2 * Math.cos(a1)); strip(p[0], p[2], q[0], q[2], 1.2); } }+ place('Ground_Flagstones', fl, MAT.flag, { parent: g });+ place('Ground_Earth', ea, MAT.earth, { parent: g });+ place('Ground_Steps', st, MAT.flag, { parent: g });+ place('Ground_Grime', gr, MAT.decal, { parent: g });+}++function buildCathedral() {+ buildBody();+ buildWestFront();+ const gTN = ROOT.children.find((c) => c.name === 'Transept_North'), gTS = ROOT.children.find((c) => c.name === 'Transept_South');+ buildTransFront(gTN, 1);+ buildTransFront(gTS, -1);+ buildSanctuary();+ buildGround();+}++function previewStatues() {+ const names = P.statueTest;+ names.forEach((n, i) => placeFigure('Fig_' + n, n, { pos: [0, 0, (i - (names.length - 1) / 2) * 1.0] }));+ const base = new Batch(TILE.ashlar); base.box(-3, 3, -0.4, 0, -6, 6, { s: 0.8 }); place('Ground', base, MAT.ashlar);+}++// ==================================================================================+// BAKED LIGHT. The viewer casts no shadows, so the shadows are computed here. Every opaque surface is voxelized+// into the world, then each vertex looks at the sun, the sky and the rain:+// - rays towards the sun give the low raking light and the cast shadows;+// - rays over the hemisphere give the depth of portals and galleries, and tell inside from outside;+// - a ray straight up finds what the rain never reaches, where seven centuries of soot have stayed.+// A batch placed many times can have one set of vertex colours per side of the building, within a byte budget.+// Sets beyond the budget share the sunniest colours under a darker material.+// ==================================================================================+const SKY_RAYS = (() => {+ const r = [[0, 1, 0, 1]];+ for (let i = 0; i < 4; i++) { const a = i * TAU / 4 + 0.4, e = rad(42); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 1]); }+ for (let i = 0; i < 5; i++) { const a = i * TAU / 5 + 0.1, e = rad(9); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 0]); }+ return r;+})();+// Large meshes of moulding keep their shape with a quarter fewer triangles: the bytes are better spent up close+async function reduceBatches(share = 0.74, repeatedShare = 0.62, minTris = 2000) {+ const seen = new Map(), uses = new Map();+ for (const e of PLACED) uses.set(e.batch, (uses.get(e.batch) || 0) + 1);+ for (const e of PLACED) {+ const m = e.mat;+ if (e.batch.isFigure || e.batch.noReduce || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.glassOutWin || m === MAT.glassRose || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave || m === MAT.flag || m === MAT.earth) continue;+ let nb = seen.get(e.batch);+ if (nb === undefined) {+ const tris = e.batch.I.length / 3, s = uses.get(e.batch) >= 8 ? repeatedShare : share; // a mesh repeated down the whole building costs the most+ nb = tris > minTris ? await lightCopy(e.batch, Math.round(tris * s)) : null;+ if (nb) nb.noColor = e.batch.noColor;+ seen.set(e.batch, nb);+ }+ if (nb) { e.batch = nb; e.part.geometry = nb.toGeo(); }+ }+}+function samplesOf(b) {+ if (b.samples) return b.samples;+ const P0 = b.P, out = [], lim = 0.32;+ for (let i = 0; i < P0.length; i++) out.push(P0[i]);+ for (let t = 0; t < b.I.length; t += 3) {+ const a = b.I[t] * 3, c = b.I[t + 1] * 3, d = b.I[t + 2] * 3;+ const e1 = len3(P0[a] - P0[c], P0[a + 1] - P0[c + 1], P0[a + 2] - P0[c + 2]), e2 = len3(P0[c] - P0[d], P0[c + 1] - P0[d + 1], P0[c + 2] - P0[d + 2]), e3 = len3(P0[d] - P0[a], P0[d + 1] - P0[a + 1], P0[d + 2] - P0[a + 2]), m = Math.max(e1, e2, e3);+ if (m <= lim) continue;+ const n = Math.ceil(m / lim);+ for (let i = 0; i <= n; i++) for (let j = 0; j <= n - i; j++) { const u = i / n, v = j / n, w = 1 - u - v; out.push(P0[a] * u + P0[c] * v + P0[d] * w, P0[a + 1] * u + P0[c + 1] * v + P0[d + 1] * w, P0[a + 2] * u + P0[c + 2] * v + P0[d + 2] * w); }+ }+ b.samples = new Float32Array(out);+ return b.samples;+}+function bakeLight() {+ ROOT.updateMatrixWorld(true);+ // The world follows the plan: from beyond the chapels to beyond the west front. A spire tip above the top simply casts no shadow up there+ const CELL = 0.4, X0 = LAY.xa - 27.8, Y0 = -2, Z0 = -36, NX = Math.ceil((P.xWest + 12 - X0) / CELL), NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;+ const grid = new Uint8Array(NX * NY * NZ), coarse = new Uint8Array(CX * CY * CZ);+ const SUN = (() => { const v = P.sun, l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();+ const SUN2 = [[SUN[0] + 0.06, SUN[1], SUN[2] + 0.05], [SUN[0] - 0.05, SUN[1] + 0.05, SUN[2] - 0.05]].map((v) => { const l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; });+ for (const e of PLACED) {+ if (e.mat.transparent || e.batch.noVoxel) continue;+ const S = samplesOf(e.batch), m = e.part.matrixWorld.elements;+ for (let i = 0; i < S.length; i += 3) {+ const x = S[i], y = S[i + 1], z = S[i + 2], gi = ((m[0] * x + m[4] * y + m[8] * z + m[12] - X0) / CELL) | 0, gj = ((m[1] * x + m[5] * y + m[9] * z + m[13] - Y0) / CELL) | 0, gk = ((m[2] * x + m[6] * y + m[10] * z + m[14] - Z0) / CELL) | 0;+ if (gi >= 0 && gi < NX && gj >= 0 && gj < NY && gk >= 0 && gk < NZ) { grid[gi + NX * (gj + NY * gk)] = 1; coarse[(gi >> 3) + CX * ((gj >> 3) + CY * (gk >> 3))] = 1; }+ }+ }+ // How enclosed a point is: the solid voxels averaged into 1.6 m cells and blurred, read back with trilinear filtering.+ // It is a smooth function of position, so light taken from it cannot have hard edges however few vertices a surface has.+ const DX = (NX >> 2) + 2, DY = (NY >> 2) + 2, DZ = (NZ >> 2) + 2, dens = new Float32Array(DX * DY * DZ);+ for (let k = 0, q = 0; k < NZ; k++) for (let j = 0; j < NY; j++) for (let i = 0; i < NX; i++, q++) if (grid[q]) dens[(i >> 2) + DX * ((j >> 2) + DY * (k >> 2))] += 1 / 64;+ { const tmp = new Float32Array(dens.length), steps = [1, DX, DX * DY];+ for (let pass = 0; pass < 3; pass++) for (const st of steps) {+ tmp.set(dens);+ for (let q = st; q < dens.length - st; q++) dens[q] = 0.25 * tmp[q - st] + 0.5 * tmp[q] + 0.25 * tmp[q + st];+ } }+ const enclosed = (x, y, z) => {+ const fx = clamp((x - X0) / (CELL * 4), 0, DX - 1.001), fy = clamp((y - Y0) / (CELL * 4), 0, DY - 1.001), fz = clamp((z - Z0) / (CELL * 4), 0, DZ - 1.001);+ const i = fx | 0, j = fy | 0, k = fz | 0, a = fx - i, c = fy - j, d = fz - k, q = i + DX * (j + DY * k), P1 = 1, P2 = DX, P4 = DX * DY;+ const lo = (dens[q] * (1 - a) + dens[q + P1] * a) * (1 - c) + (dens[q + P2] * (1 - a) + dens[q + P2 + P1] * a) * c;+ const hi = (dens[q + P4] * (1 - a) + dens[q + P4 + P1] * a) * (1 - c) + (dens[q + P4 + P2] * (1 - a) + dens[q + P4 + P2 + P1] * a) * c;+ return lo * (1 - d) + hi * d;+ };+ // Distance to the first hit, or 1e9 if the ray leaves the world; capped rays return their cap.+ // A ray from ground beyond the voxels first flies in to them, so the building still shadows the far flagstones.+ const X1 = X0 + NX * CELL, Y1 = Y0 + NY * CELL, Z1 = Z0 + NZ * CELL;+ const march = (ox, oy, oz, dx, dy, dz, cap) => {+ let t = 0; const st = CELL * 0.8, far = CELL * CK * 0.6;+ if (ox < X0 || ox >= X1 || oy < Y0 || oy >= Y1 || oz < Z0 || oz >= Z1) {+ let t0 = 0, t1 = cap;+ for (const [o, d, lo, hi] of [[ox, dx, X0, X1], [oy, dy, Y0, Y1], [oz, dz, Z0, Z1]]) {+ if (Math.abs(d) < 1e-9) { if (o < lo || o >= hi) return 1e9; continue; }+ const a = (lo - o) / d, b = (hi - o) / d; t0 = Math.max(t0, Math.min(a, b)); t1 = Math.min(t1, Math.max(a, b));+ }+ if (t0 >= t1) return 1e9;+ t = t0 + 0.01;+ }+ while (t < cap) {+ const i = ((ox + dx * t - X0) / CELL) | 0, j = ((oy + dy * t - Y0) / CELL) | 0, k = ((oz + dz * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return 1e9;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)]) return t;+ t += st;+ }+ return cap;+ };+ const solidAt = (x, y, z) => { const i = ((x - X0) / CELL) | 0, j = ((y - Y0) / CELL) | 0, k = ((z - Z0) / CELL) | 0; return i >= 0 && i < NX && j >= 0 && j < NY && k >= 0 && k < NZ && grid[i + NX * (j + NY * k)] === 1; };+ // A vertex's light: [r,g,b] multipliers, plus how much of it was outdoors+ const light = { r: 1, g: 1, b: 1, out: 0, ndl: 0 };+ let sootFloor = 0.56, dampK = 1, indoorK = 1;+ const lightAt = (x, y, z, nx, ny, nz) => {+ // leave the voxels of the surface itself, then look around+ let ox = x + nx * CELL * 1.2, oy = y + ny * CELL * 1.2, oz = z + nz * CELL * 1.2, buried = false;+ if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 2.4; oy = y + ny * CELL * 2.4; oz = z + nz * CELL * 2.4; if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 3.6; oy = y + ny * CELL * 3.6; oz = z + nz * CELL * 3.6; buried = solidAt(ox, oy, oz); } }+ let esc = 0, skyW = 0, sky = 0, zen = 1e9;+ // A ray counts as sky only if it truly leaves the world: a long look down the nave is still indoors+ if (!buried) for (let q = 0; q < 5; q++) {+ const R = SKY_RAYS[q], d = march(ox, oy, oz, R[0], R[1], R[2], 200);+ if (q === 0) zen = d;+ const w = Math.max(0.12, R[0] * nx + R[1] * ny + R[2] * nz + 0.35);+ skyW += w; if (d >= 1e9) { sky += w; esc++; }+ }+ // The underside of a cornice sees no sky above it but is still outdoors: only there is a long look sideways worth its cost+ if (!buried && esc === 0 && ny < -0.2) for (let q = 5; q < SKY_RAYS.length && esc === 0; q++) { const R = SKY_RAYS[q]; if (march(ox, oy, oz, R[0], R[1], R[2], 90) >= 1e9) esc++; }+ const ao = clamp(1 - enclosed(x + nx, y + ny, z + nz) * 2.6), vis = skyW ? sky / skyW : 0, outdoors = esc >= 1;+ let r, g, b;+ if (outdoors) {+ const ndl = nx * SUN[0] + ny * SUN[1] + nz * SUN[2];+ let sunV = 0;+ if (ndl > 0 && !buried) {+ if (march(ox, oy, oz, SUN[0], SUN[1], SUN[2], 200) >= 1e9) sunV += 0.5;+ if (march(ox, oy, oz, SUN2[0][0], SUN2[0][1], SUN2[0][2], 200) >= 1e9) sunV += 0.25;+ if (march(ox, oy, oz, SUN2[1][0], SUN2[1][1], SUN2[1][2], 200) >= 1e9) sunV += 0.25;+ }+ const amb = 0.2 + 0.44 * vis * (0.55 + 0.45 * ao), s = sunV * Math.max(0, ndl) * 1.02;+ r = amb * 0.84 + s * 0.98; g = amb * 0.88 + s * 0.9; b = amb * 0.98 + s * 0.74;+ // What the rain never touches keeps its soot; what it washes stays pale. Gentle on long walls, whose+ // few vertices would smear it: the crisp streaks under their ledges are decals.+ const shelter = zen < 6 ? 1 - zen / 6 : 0, soot = lerp(1, sootFloor, shelter * (1 - Math.max(0, ny)));+ r *= soot; g *= soot * 0.985; b *= soot * 0.96;+ // Rising damp: darker and greener towards the ground, a slow mottling over the height of the wall+ const damp = lerp(1, sstep(0, 9, y), dampK), mot = 0.93 + 0.07 * Math.sin(x * 0.53 + y * 0.31) * Math.sin(z * 0.47 - y * 0.23);+ r *= lerp(0.56, 1, damp) * mot; g *= lerp(0.64, 1, damp) * mot; b *= lerp(0.46, 1, damp) * mot;+ light.out = 1; light.ndl = ndl;+ } else {+ // Inside: warm stone, dark low in the aisles, lifting into the vault. Every term is a smooth function of position,+ // so the light has only gradients: the aisles of the nave, of the transept arms and round the apse fade in softly+ const low = sstep(P.aisleCrown + 2.5, P.aisleCrown - 0.5, y), axis = zn - 0.1, dxc = Math.abs(x - LAY.xc), arm = sstep(16, 13, dxc);+ const naveA = sstep(axis, axis + 2.4, Math.abs(z)) * (1 - arm), armA = sstep(axis, axis + 2.4, dxc) * arm * sstep(13, 16, Math.abs(z));+ const apse = sstep(LAY.xa + 1.5, LAY.xa - 1.5, x), ambA = sstep(axis, axis + 2.4, Math.sqrt((x - LAY.xa) * (x - LAY.xa) + z * z)) * apse;+ const s = lerp(0.32, 0.86, sstep(0.5, 34, y)) * lerp(0.5, 1, ao) * lerp(1, 0.76, Math.max(naveA * (1 - apse), armA, ambA) * low) * indoorK;+ r = s * 1.06; g = s * 0.87; b = s * 0.64;+ light.out = 0; light.ndl = 0;+ }+ light.r = r; light.g = g; light.b = b;+ return light;+ };+ // ---- group placements of each batch by which way they face the sun ----+ const perBatch = new Map();+ for (const e of PLACED) { if (e.batch.noColor || e.batch.isFigure || (e.mat.transparent && !e.batch.fade)) continue; let l = perBatch.get(e.batch); if (!l) { l = []; perBatch.set(e.batch, l); } l.push(e); }+ const entries = [];+ for (const [b, list] of perBatch) {+ const groups = new Map();+ for (const e of list) {+ const m = e.part.matrixWorld.elements, sx = len3(m[0], m[1], m[2]) || 1, sy = len3(m[4], m[5], m[6]) || 1, sz = len3(m[8], m[9], m[10]) || 1;+ // the sun in this placement's own frame (mirror included)+ const lx = (m[0] * SUN[0] + m[1] * SUN[1] + m[2] * SUN[2]) / (sx * sx), ly = (m[4] * SUN[0] + m[5] * SUN[1] + m[6] * SUN[2]) / (sy * sy), lz = (m[8] * SUN[0] + m[9] * SUN[1] + m[10] * SUN[2]) / (sz * sz);+ const key = Math.round(lx * 3) + ',' + Math.round(ly * 3) + ',' + Math.round(lz * 3);+ let g = groups.get(key); if (!g) { g = { key, list: [], L: [lx, ly, lz] }; groups.set(key, g); } g.list.push(e);+ }+ const gl = [...groups.values()];+ for (const g of gl) { // how much sun this side receives, by area of normals+ let s = 0; const nn = b.N; for (let i = 0; i < nn.length; i += 3) s += Math.max(0, nn[i] * g.L[0] + nn[i + 1] * g.L[1] + nn[i + 2] * g.L[2]);+ g.expo = s / Math.max(1, nn.length / 3);+ }+ gl.sort((a, c) => c.expo - a.expo);+ entries.push({ b, gl, wall: list[0].mat === MAT.ashlar ? 0 : 1 });+ }+ // Long walls get their own light for each side first: they carry the sense of sun and shade from a distance+ entries.sort((a, c) => a.wall - c.wall || a.b.count - c.b.count);+ let spent = 0, variants = 0, tinted = 0, vbaked = 0;+ const TINTS = new Map();+ const tintOf = (mat, tier) => {+ const key = mat.uuid + '|' + tier; let m = TINTS.get(key);+ if (!m) { m = mat.clone(); m.name = mat.name + (tier === 1 ? '_ShadeSide' : '_DeepShadeSide'); m.color.setRGB(tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.76 : 0.53); TINTS.set(key, m); }+ return m;+ };+ const bakeGroup = (b, g) => {+ const rep = g.list[Math.floor(g.list.length / 2)], m = rep.part.matrixWorld.elements, n = b.count, out = new Uint8Array(n * 3), PP = b.P, NN = b.N, CC = b.C;+ let outdoor = 0;+ const onGround = rep.mat === MAT.flag || rep.mat === MAT.earth;+ sootFloor = onGround ? 1 : rep.mat === MAT.ashlar ? 0.84 : rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0.9 : 0.56;+ dampK = onGround || rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0 : 1;+ indoorK = rep.mat === MAT.oak ? 2.2 : 1; // the oak was given its own dark tone when it was built+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, x = PP[i3], y = PP[i3 + 1], z = PP[i3 + 2], a = NN[i3], c = NN[i3 + 1], d = NN[i3 + 2];+ const wx = m[0] * x + m[4] * y + m[8] * z + m[12], wy = m[1] * x + m[5] * y + m[9] * z + m[13], wz = m[2] * x + m[6] * y + m[10] * z + m[14];+ let nx = m[0] * a + m[4] * c + m[8] * d, ny = m[1] * a + m[5] * c + m[9] * d, nz = m[2] * a + m[6] * c + m[10] * d; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1; nx /= l; ny /= l; nz /= l;+ const L = lightAt(wx, wy, wz, nx, ny, nz);+ outdoor += L.out;+ out[i3] = Math.min(255, CC[i3] * L.r); out[i3 + 1] = Math.min(255, CC[i3 + 1] * L.g); out[i3 + 2] = Math.min(255, CC[i3 + 2] * L.b);+ }+ // Each vertex is one noisy sample of light: blend neighbours on the same surface so no sample can smear a dark+ // triangle across the stone. Edges between planes keep their contrast.+ {+ const sum = new Float32Array(n * 3), w = new Float32Array(n), I = b.I, passes = rep.mat === MAT.ashlar ? 3 : 2;+ const link = (p, q) => { if (NN[p * 3] * NN[q * 3] + NN[p * 3 + 1] * NN[q * 3 + 1] + NN[p * 3 + 2] * NN[q * 3 + 2] < 0.8) return; w[p]++; w[q]++; for (let c = 0; c < 3; c++) { sum[p * 3 + c] += out[q * 3 + c]; sum[q * 3 + c] += out[p * 3 + c]; } };+ for (let pass = 0; pass < passes; pass++) {+ sum.fill(0); w.fill(0);+ for (let t = 0; t < I.length; t += 3) { link(I[t], I[t + 1]); link(I[t + 1], I[t + 2]); link(I[t + 2], I[t]); }+ for (let i = 0; i < n; i++) if (w[i]) for (let c = 0; c < 3; c++) out[i * 3 + c] = out[i * 3 + c] * 0.4 + 0.6 * sum[i * 3 + c] / w[i];+ }+ }+ vbaked += n;+ return { colors: out, outdoor: outdoor / Math.max(1, n) };+ };+ for (const { b, gl } of entries) {+ const base = b.toGeo(), cost = (gl.length - 1) * b.count * 12, full = gl.length === 1 || spent + cost <= P.lightVariantBytes;+ if (full) spent += cost;+ gl.forEach((g, gi) => {+ if (gi === 0 || full) {+ const R = bakeGroup(b, g);+ g.outdoor = R.outdoor;+ if (gi === 0 && b.fade) { // a surface that dissolves at its edge carries its opacity with its light+ const rgba = new Float32Array(b.count * 4);+ for (let i = 0; i < b.count; i++) { rgba[i * 4] = R.colors[i * 3] / 255; rgba[i * 4 + 1] = R.colors[i * 3 + 1] / 255; rgba[i * 4 + 2] = R.colors[i * 3 + 2] / 255; rgba[i * 4 + 3] = b.fade(b.P[i * 3], b.P[i * 3 + 2]); }+ base.setAttribute('color', new THREE.BufferAttribute(rgba, 4));+ return;+ }+ if (gi === 0) { base.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true)); return; }+ const v = new THREE.BufferGeometry();+ v.setAttribute('position', base.attributes.position); v.setAttribute('normal', base.attributes.normal); v.setAttribute('uv', base.attributes.uv);+ v.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true));+ if (base.attributes.tangent) v.setAttribute('tangent', base.attributes.tangent);+ v.setIndex(base.index);+ for (const e of g.list) e.part.geometry = v;+ variants++;+ } else {+ // over budget: same colours, a darker stone. Skipped for batches that are mostly indoors+ tinted++;+ if (gl[0].outdoor < 0.3) return;+ const ratio = g.expo / Math.max(1e-3, gl[0].expo);+ if (ratio >= 0.78) return;+ const tier = ratio >= 0.45 ? 1 : 2;+ for (const e of g.list) e.part.material = tintOf(e.mat, tier);+ }+ });+ }+ // ---- figures keep their carved occlusion; the niche they stand in decides how much daylight reaches them ----+ let figN = 0;+ for (const f of FIG_PLACED) {+ const m = f.part.matrixWorld.elements, s = len3(m[0], m[1], m[2]) || 1, fx = m[0] / s, fy = m[1] / s, fz = m[2] / s;+ let ox = m[12] + m[4] * 1.3, oy = m[13] + m[5] * 1.3, oz = m[14] + m[6] * 1.3;+ for (let i = 0; i < 12 && solidAt(ox, oy, oz); i++) { ox += fx * CELL; oy += fy * CELL; oz += fz * CELL; }+ const L = lightAt(ox - fx * 0.05, oy - fy * 0.05, oz - fz * 0.05, fx, fy, fz);+ let mat = MAT.figureIn;+ if (L.out) { const lum = (L.r + L.g + L.b) / 3, facing = fx * SUN[0] + fy * SUN[1] + fz * SUN[2]; mat = lum > 0.56 ? MAT.figure : lum > 0.3 ? MAT.figureHalf : MAT.figureDeep; if (facing < -0.3 && mat === MAT.figure) mat = MAT.figureHalf; }+ f.part.material = mat; if (f.head) f.head.material = mat;+ figN++;+ }+ return { vertices: vbaked, variants, tinted, extraKB: Math.round(spent / 1024), figures: figN };+}++// <<ORNAMENT>>++// <<BUILD>>+async function build() {+ ROOT = createRoot('Cathedral');+ await makeMaterials();+ await prepareFigures(P.preview === 'statues' ? P.statueTest : Object.keys(FIGURES));+ if (P.preview === 'statues') { previewStatues(); return ROOT; }+ if (P.preview === 'all' || P.preview === 'unique') {+ buildCathedral();+ await reduceBatches();+ bakeLight();+ return ROOT;+ }+ if (P.preview === 'size') { // builds and bakes everything, then reports bytes as part names, and exports only a probe: fast+ buildCathedral();+ await reduceBatches();+ const baked = bakeLight(), report = ['bake_variants' + baked.variants + '_tinted' + baked.tinted + '_extraKB' + baked.extraKB];+ // Real buffer sizes, each attribute counted once: pos/normal 12, uv 8, colour 12, tangent 16 a vertex where normal-mapped+ const seenAttr = new Set(), seenTan = new Set(), byMat = {}; let bytes = 0, tris = 0;+ const add = (attr, per, key) => { if (!attr || seenAttr.has(attr)) return; seenAttr.add(attr); const b = attr.count * per; bytes += b; byMat[key] = (byMat[key] || 0) + b; };+ for (const e of PLACED) {+ const g = e.part.geometry, key = e.mat.name, a = g.attributes, m = e.part.material;+ tris += g.index.count / 3;+ add(a.position, 12, key); add(a.normal, 12, key); add(a.uv, 8, key); add(a.color, 12, key);+ add(g.index, g.index.array.BYTES_PER_ELEMENT, key);+ if (m && m.normalMap && !seenTan.has(a.position)) { seenTan.add(a.position); bytes += a.position.count * 16; byMat[key] = (byMat[key] || 0) + a.position.count * 16; }+ }+ report.push('geomKB' + Math.round(bytes / 1024) + '_meshes' + PLACED.length + '_rendered' + Math.round(tris));+ Object.keys(byMat).sort((p, q) => byMat[q] - byMat[p]).forEach((k) => report.push(k + '_KB' + Math.round(byMat[k] / 1024)));+ ROOT.clear();+ createPart('SizeProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ report.forEach((d) => group('SIZE_' + d));+ return ROOT;+ }+ if (P.preview === 'textures') { // measures the texture payload on its own+ Object.keys(MAT).forEach((k, i) => createPart('Swatch_' + k, boxGeo(1, 1, 1), MAT[k], { position: [i * 1.2, 0.5, 0], parent: ROOT }));+ return ROOT;+ }+ if (P.preview === 'stats') {+ buildCathedral();+ const list = [...STAT.entries()].map(([b, e]) => ({ name: e.name, n: e.n, tris: b.I.length / 3, kb: Math.round((batchBytes(b) + (e.normalMapped ? b.count * 16 : 0)) / 1024) })).sort((a, c) => c.kb - a.kb);+ let total = 0, draws = 0, rendered = 0; for (const e of list) { total += e.kb; draws += e.n; rendered += e.n * e.tris; }+ createPart('StatProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ group('STAT_TOTAL_kb_' + total + '_draws_' + draws + '_rendered_' + rendered + '_unique_' + list.length);+ list.slice(0, 40).forEach((e, i) => group('STAT_' + String(i).padStart(2, '0') + '_K' + Math.round(e.n * e.tris / 1000) + '_' + e.name + '_x' + e.n + '_tris' + e.tris + '_kb' + e.kb));+ return ROOT;+ }+ return ROOT;+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-5.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/cathedral-5.kiln.jsnew file mode 100644index 00000000..307b8ed2--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/cathedral-5.kiln.js@@ -0,0 +1,4203 @@+// Gothic cathedral: parametric High Gothic church, true scale, metres.+// +X = west front (entrance) , -X = apse, +Y up, +Z = north.+const meta = { name: 'Gothic Cathedral', role: 'wonder' };++// ==================================================================================+// PARAMETERS: change these and the whole design follows.+// ==================================================================================+const P = {+ preview: 'all', // 'all', or 'size' (true buffer bytes, fast), 'stats' (per-mesh list), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)+ // Plan+ naveBays: 11, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,+ xWest: 62, // west face of the towers+ towerDepth: 14, towerWidth: 14,+ choirBays: 3, transeptBays: 2,+ apseSides: 7,+ // Elevation+ pierTop: 15, aisleCrown: 21, triforiumSill: 21.8, triforiumTop: 26.6,+ vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,+ aisleWallTop: 23, aisleRoofHigh: 26.2,+ towerTop: 72, spireTip: 80,+ flecheTip: 100, // top of the cross on the lead spire over the crossing+ // Detail+ jambStatues: 5, kings: 16, seed: 1163,+ archFigureTris: 720, statueTest: ['apostle', 'bishop', 'king', 'queen', 'angel', 'virgin'],+ storyWindows: 3, clerestoryWindows: 3, // unique window cartoons (more = richer, larger GLB)+ // Glass+ glassGlow: 1.05, glassOuterGlow: 0.12,+ sun: [0.62, 0.45, -0.64], lightVariantBytes: 250000, wallTessellation: 5, // baked low sun from the south-west; bytes allowed for per-side vertex light; metres between lit wall vertices+ chapelFacets: 5,+};++// ==================================================================================+// MATH+// ==================================================================================+const PI = Math.PI, TAU = PI * 2, rad = (d) => d * PI / 180;+const lerp = (a, b, t) => a + (b - a) * t;+const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));+const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };+const gauss = (x, y = 0) => Math.exp(-(x * x + y * y));+function mkRng(seed) { let s = seed >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }+function keyLin(keys, t) {+ if (t <= keys[0][0]) return keys[0].slice(1);+ for (let i = 1; i < keys.length; i++) if (t <= keys[i][0]) {+ const a = keys[i - 1], b = keys[i], f = (t - a[0]) / (b[0] - a[0]);+ return a.slice(1).map((v, k) => lerp(v, b[k + 1], f));+ }+ return keys[keys.length - 1].slice(1);+}+function keyf(keys, t, w = 0.03) { // smoothed keyframes [[t,v...],...]: no plateaus or kinks at the keys+ const a = keyLin(keys, t - w), b = keyLin(keys, t), c = keyLin(keys, t + w);+ return b.map((v, k) => (a[k] + 2 * v + c[k]) / 4);+}+const M = {+ I: () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],+ T: (x, y, z) => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z],+ S: (x, y = x, z = x) => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0],+ Rx: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0]; },+ Ry: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0]; },+ Rz: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0]; },+ mul: (a, b) => [+ a[0] * b[0] + a[1] * b[4] + a[2] * b[8], a[0] * b[1] + a[1] * b[5] + a[2] * b[9], a[0] * b[2] + a[1] * b[6] + a[2] * b[10], a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3],+ a[4] * b[0] + a[5] * b[4] + a[6] * b[8], a[4] * b[1] + a[5] * b[5] + a[6] * b[9], a[4] * b[2] + a[5] * b[6] + a[6] * b[10], a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7],+ a[8] * b[0] + a[9] * b[4] + a[10] * b[8], a[8] * b[1] + a[9] * b[5] + a[10] * b[9], a[8] * b[2] + a[9] * b[6] + a[10] * b[10], a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11],+ ],+ of: (...ms) => ms.reduce((a, b) => M.mul(a, b)),+};+function normalMat(m) { // inverse transpose of the 3x3, up to scale (cofactors)+ return [+ m[5] * m[10] - m[6] * m[9], m[6] * m[8] - m[4] * m[10], m[4] * m[9] - m[5] * m[8],+ m[2] * m[9] - m[1] * m[10], m[0] * m[10] - m[2] * m[8], m[1] * m[8] - m[0] * m[9],+ m[1] * m[6] - m[2] * m[5], m[2] * m[4] - m[0] * m[6], m[0] * m[5] - m[1] * m[4],+ ];+}+const det3 = (m) => m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8]) + m[2] * (m[4] * m[9] - m[5] * m[8]);++// ==================================================================================+// MESH KIT: one Batch = one mesh with one material; metric UVs, baked shading in COLOR_0+// ==================================================================================+class Batch {+ constructor(tile = 1) { this.P = []; this.N = []; this.UV = []; this.C = []; this.I = []; this.tile = tile; this.setM(M.I()); this.sh = 1; this.rgb = [1, 1, 1]; this.geo = null; }+ setM(m) { this.m = m; this.nm = normalMat(m); this.det = det3(m); }+ at(m, f) { const old = this.m; this.setM(M.mul(old, m)); f(this); this.setM(old); return this; }+ shade(s, f) { const o = this.sh; this.sh = o * s; f(this); this.sh = o; return this; }+ tint(rgb, f) { const o = this.rgb; this.rgb = rgb; f(this); this.rgb = o; return this; }+ get count() { return this.P.length / 3; }+ v(x, y, z, nx, ny, nz, u, v, s = 1) {+ const m = this.m, n = this.nm;+ const wx = m[0] * x + m[1] * y + m[2] * z + m[3], wy = m[4] * x + m[5] * y + m[6] * z + m[7], wz = m[8] * x + m[9] * y + m[10] * z + m[11];+ let ax = n[0] * nx + n[1] * ny + n[2] * nz, ay = n[3] * nx + n[4] * ny + n[5] * nz, az = n[6] * nx + n[7] * ny + n[8] * nz;+ const l = Math.hypot(ax, ay, az);+ if (l < 1e-10) { ax = 0; ay = 1; az = 0; } else { ax /= l; ay /= l; az /= l; }+ if (this.det < 0) { ax = -ax; ay = -ay; az = -az; }+ if (u === undefined || u === null) {+ const fx = Math.abs(ax), fy = Math.abs(ay), fz = Math.abs(az);+ if (fy > 0.75) { u = wx; v = wz; } else if (fx > fz) { u = wz; v = wy; } else { u = wx; v = wy; }+ }+ const sh = clamp(s * this.sh); // all light is added later by bakeLight+ const rgb = this.rgb;+ this.P.push(wx, wy, wz); this.N.push(ax, ay, az); this.UV.push(u / this.tile, v / this.tile);+ this.C.push(Math.round(255 * sh * rgb[0]), Math.round(255 * sh * rgb[1]), Math.round(255 * sh * rgb[2]));+ return this.P.length / 3 - 1;+ }+ tri(a, b, c) { if (this.det < 0) this.I.push(a, c, b); else this.I.push(a, b, c); }+ quad(a, b, c, d) { this.tri(a, b, c); this.tri(a, c, d); }+ // flat polygon from 3D points (CCW seen from the front); convex fan+ face(pts, s = 1) {+ const a = pts[0], b = pts[1], c = pts[pts.length - 1];+ let nx = (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), ny = (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), nz = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ if (pts.length > 3 && Math.hypot(nx, ny, nz) < 1e-9) { const d = pts[2]; nx = (b[1] - a[1]) * (d[2] - a[2]) - (b[2] - a[2]) * (d[1] - a[1]); ny = (b[2] - a[2]) * (d[0] - a[0]) - (b[0] - a[0]) * (d[2] - a[2]); nz = (b[0] - a[0]) * (d[1] - a[1]) - (b[1] - a[1]) * (d[0] - a[0]); }+ const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l;+ const ids = pts.map((p) => this.v(p[0], p[1], p[2], nx, ny, nz, undefined, undefined, p[3] === undefined ? s : p[3]));+ for (let i = 1; i < ids.length - 1; i++) this.tri(ids[0], ids[i], ids[i + 1]);+ return this;+ }+ box(x0, x1, y0, y1, z0, z1, o = {}) {+ const s = o.s === undefined ? 1 : o.s, sb = o.sb === undefined ? s * 0.62 : o.sb, st = o.st === undefined ? s : o.st;+ const tx = o.taper || 0;+ const X0 = x0 + tx, X1 = x1 - tx, Z0 = z0 + (o.taperZ === undefined ? tx : o.taperZ), Z1 = z1 - (o.taperZ === undefined ? tx : o.taperZ);+ this.face([[x1, y0, z1, s], [x1, y0, z0, s], [X1, y1, Z0, st], [X1, y1, Z1, st]]); // +X+ this.face([[x0, y0, z0, s], [x0, y0, z1, s], [X0, y1, Z1, st], [X0, y1, Z0, st]]); // -X+ this.face([[x0, y0, z1, s], [x1, y0, z1, s], [X1, y1, Z1, st], [X0, y1, Z1, st]]); // +Z+ this.face([[x1, y0, z0, s], [x0, y0, z0, s], [X0, y1, Z0, st], [X1, y1, Z0, st]]); // -Z+ if (!o.noTop) this.face([[X0, y1, Z1, st], [X1, y1, Z1, st], [X1, y1, Z0, st], [X0, y1, Z0, st]]);+ if (!o.noBot) this.face([[x0, y0, z0, sb], [x1, y0, z0, sb], [x1, y0, z1, sb], [x0, y0, z1, sb]]);+ return this;+ }+ // Surface of revolution about Y. prof: [[r,y],...] bottom to top. o.sides, o.faceted, o.crease, o.flat+ lathe(prof, n = 12, o = {}) {+ const a0 = o.phase || 0, cr = Math.cos(rad(o.crease === undefined ? 55 : o.crease));+ const s = o.s === undefined ? 1 : o.s;+ const segs = [];+ for (let j = 0; j < prof.length - 1; j++) {+ const dr = prof[j + 1][0] - prof[j][0], dy = prof[j + 1][1] - prof[j][1], l = Math.hypot(dr, dy) || 1;+ segs.push([dy / l, -dr / l, l]);+ }+ if (o.faceted) { // hard-edged polygon of revolution+ for (let i = 0; i < n; i++) {+ const t0 = a0 + TAU * i / n, t1 = a0 + TAU * (i + 1) / n, tm = (t0 + t1) / 2;+ const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1), cm = Math.cos(tm), sm = Math.sin(tm);+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const p0 = prof[j], p1 = prof[j + 1];+ const w0 = p0[0] * Math.hypot(c1 - c0, s1 - s0), w1 = p1[0] * Math.hypot(c1 - c0, s1 - s0);+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ const a = this.v(p0[0] * c0, p0[1], p0[0] * s0, nr * cm, ny, nr * sm, -w0 / 2, vv, sA);+ const b = this.v(p1[0] * c0, p1[1], p1[0] * s0, nr * cm, ny, nr * sm, -w1 / 2, vv + l, sB);+ const c = this.v(p1[0] * c1, p1[1], p1[0] * s1, nr * cm, ny, nr * sm, w1 / 2, vv + l, sB);+ const d = this.v(p0[0] * c1, p0[1], p0[0] * s1, nr * cm, ny, nr * sm, w0 / 2, vv, sA);+ this.quad(a, b, c, d); vv += l;+ }+ }+ return this;+ }+ // smooth around, creased along the profile when the section turns sharply+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const prev = segs[j - 1], next = segs[j + 1];+ let n0r = nr, n0y = ny, n1r = nr, n1y = ny;+ if (prev && prev[0] * nr + prev[1] * ny > cr) { n0r = prev[0] + nr; n0y = prev[1] + ny; }+ if (next && next[0] * nr + next[1] * ny > cr) { n1r = next[0] + nr; n1y = next[1] + ny; }+ const base = this.count, p0 = prof[j], p1 = prof[j + 1];+ const circ = Math.max(p0[0], p1[0]) * TAU, uScale = Math.max(1, Math.round(circ)) / n;+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ for (let i = 0; i <= n; i++) {+ const t = a0 + TAU * i / n, c = Math.cos(t), sn = Math.sin(t);+ this.v(p0[0] * c, p0[1], p0[0] * sn, n0r * c, n0y, n0r * sn, i * uScale, vv, sA);+ this.v(p1[0] * c, p1[1], p1[0] * sn, n1r * c, n1y, n1r * sn, i * uScale, vv + l, sB);+ }+ for (let i = 0; i < n; i++) this.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+ vv += l;+ }+ return this;+ }+ // Sweep a closed section (a,b CCW; a along in-plane left normal, b along +Z) along a path in the XY plane.+ sweep(path, sec, o = {}) {+ const closedPath = !!o.closed, cr = Math.cos(rad(o.crease === undefined ? 50 : o.crease)), z0 = o.z || 0, s = o.s === undefined ? 1 : o.s;+ const n = path.length, E = [];+ const segCount = closedPath ? n : n - 1;+ for (let i = 0; i < segCount; i++) { const p = path[i], q = path[(i + 1) % n]; const dx = q[0] - p[0], dy = q[1] - p[1], l = Math.hypot(dx, dy) || 1e-6; E.push([dx / l, dy / l, l]); }+ // section normals+ const K = sec.length, SN = [];+ for (let j = 0; j < K; j++) { const a = sec[j], b = sec[(j + 1) % K]; const da = b[0] - a[0], db = b[1] - a[1], l = Math.hypot(da, db) || 1e-6; SN.push([db / l, -da / l, l]); }+ const rings = []; let dist = 0;+ for (let i = 0; i < segCount; i++) {+ const e = E[i], ePrev = closedPath ? E[(i - 1 + segCount) % segCount] : (E[i - 1] || e), eNext = closedPath ? E[(i + 1) % segCount] : (E[i + 1] || e);+ const p = path[i], q = path[(i + 1) % n];+ const miter = (A, B) => { let bx = -(A[1] + B[1]), by = A[0] + B[0]; const l = Math.hypot(bx, by) || 1; bx /= l; by /= l; const nlx = -B[1], nly = B[0]; const c = Math.max(0.35, bx * nlx + by * nly); return [bx / c, by / c]; };+ const m0 = miter(ePrev, e), m1 = miter(e, eNext);+ const smooth0 = ePrev[0] * e[0] + ePrev[1] * e[1] > cr, smooth1 = e[0] * eNext[0] + e[1] * eNext[1] > cr;+ const nl = [-e[1], e[0]];+ const n0 = smooth0 ? [-(e[1] + ePrev[1]), e[0] + ePrev[0]] : nl, n1 = smooth1 ? [-(e[1] + eNext[1]), e[0] + eNext[0]] : nl;+ const ln0 = Math.hypot(n0[0], n0[1]) || 1, ln1 = Math.hypot(n1[0], n1[1]) || 1;+ let perim = 0;+ for (let j = 0; j < K; j++) {+ const A = sec[j], B = sec[(j + 1) % K], sn = SN[j], pv = SN[(j - 1 + K) % K], nx = SN[(j + 1) % K];+ const sA = sn[0] * pv[0] + sn[1] * pv[1] > cr, sB = sn[0] * nx[0] + sn[1] * nx[1] > cr;+ const nA = sA ? [sn[0] + pv[0], sn[1] + pv[1]] : sn, nB = sB ? [sn[0] + nx[0], sn[1] + nx[1]] : sn;+ const norm = (na, nb, nv, ln) => { const l = Math.hypot(na, nb) || 1; const a = na / l, b = nb / l; return [a * nv[0] / ln, a * nv[1] / ln, b]; };+ const na0 = norm(nA[0], nA[1], n0, ln0), nb0 = norm(nB[0], nB[1], n0, ln0), na1 = norm(nA[0], nA[1], n1, ln1), nb1 = norm(nB[0], nB[1], n1, ln1);+ const shA = A[2] === undefined ? s : A[2] * s, shB = B[2] === undefined ? s : B[2] * s;+ const a = this.v(p[0] + A[0] * m0[0], p[1] + A[0] * m0[1], z0 + A[1], na0[0], na0[1], na0[2], dist, perim, shA);+ const b = this.v(p[0] + B[0] * m0[0], p[1] + B[0] * m0[1], z0 + B[1], nb0[0], nb0[1], nb0[2], dist, perim + sn[2], shB);+ const c = this.v(q[0] + B[0] * m1[0], q[1] + B[0] * m1[1], z0 + B[1], nb1[0], nb1[1], nb1[2], dist + e[2], perim + sn[2], shB);+ const d = this.v(q[0] + A[0] * m1[0], q[1] + A[0] * m1[1], z0 + A[1], na1[0], na1[1], na1[2], dist + e[2], perim, shA);+ this.quad(a, b, c, d);+ perim += sn[2];+ }+ dist += e[2];+ rings.push(m0);+ }+ if (!closedPath && o.caps !== false) {+ const capAt = (p, nv, e, flip) => {+ const pts = sec.map((A) => [p[0] + A[0] * nv[0], p[1] + A[0] * nv[1], z0 + A[1]]);+ const idx = triangulate(sec.map((A) => [A[0], A[1]]));+ const ids = pts.map((q) => this.v(q[0], q[1], q[2], flip * e[0], flip * e[1], 0, q[0], q[2], s * 0.8));+ for (const t of idx) { if (flip > 0) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); else this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ };+ capAt(path[0], rings[0], E[0], -1);+ const lastE = E[segCount - 1]; capAt(path[n - 1], [-lastE[1], lastE[0]], lastE, 1);+ }+ return this;+ }+ // Generic tube along a 3D path with parallel transport. radii: number|array|fn(t) -> [ra, rb]; ring: fn(angle,t) multiplier+ tube(path, radii, n = 8, o = {}) {+ const N = path.length, s = o.s === undefined ? 1 : o.s;+ let T = [], U = o.up ? [...o.up] : null;+ for (let i = 0; i < N; i++) {+ const p = path[Math.max(0, i - 1)], q = path[Math.min(N - 1, i + 1)];+ let tx = q[0] - p[0], ty = q[1] - p[1], tz = q[2] - p[2]; const l = Math.hypot(tx, ty, tz) || 1; T.push([tx / l, ty / l, tz / l]);+ }+ if (!U) U = Math.abs(T[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];+ const base = this.count; const rowLen = n + 1; let dist = 0;+ for (let i = 0; i < N; i++) {+ const t = T[i];+ let d = U[0] * t[0] + U[1] * t[1] + U[2] * t[2];+ let ux = U[0] - d * t[0], uy = U[1] - d * t[1], uz = U[2] - d * t[2]; const ul = Math.hypot(ux, uy, uz) || 1; ux /= ul; uy /= ul; uz /= ul; U = [ux, uy, uz];+ const vx = t[1] * uz - t[2] * uy, vy = t[2] * ux - t[0] * uz, vz = t[0] * uy - t[1] * ux;+ const f = i / (N - 1);+ const rr = typeof radii === 'function' ? radii(f) : (Array.isArray(radii) ? radii[i] : radii);+ const ra = Array.isArray(rr) ? rr[0] : rr, rb = Array.isArray(rr) ? rr[1] : rr;+ if (i > 0) dist += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1], path[i][2] - path[i - 1][2]);+ for (let k = 0; k <= n; k++) {+ const a = TAU * k / n, c = Math.cos(a), sn = Math.sin(a);+ const mod = o.ring ? o.ring(a, f) : 1;+ const shade = o.ringS ? o.ringS(a, f) : 1;+ const px = path[i][0] + (ux * c * ra + vx * sn * rb) * mod, py = path[i][1] + (uy * c * ra + vy * sn * rb) * mod, pz = path[i][2] + (uz * c * ra + vz * sn * rb) * mod;+ const nx = ux * c / (ra || 1) + vx * sn / (rb || 1), ny = uy * c / (ra || 1) + vy * sn / (rb || 1), nz = uz * c / (ra || 1) + vz * sn / (rb || 1);+ this.v(px, py, pz, nx, ny, nz, k / n * Math.max(ra, rb) * TAU, dist, s * shade);+ }+ }+ for (let i = 0; i < N - 1; i++) for (let k = 0; k < n; k++) {+ const a = base + i * rowLen + k, b = a + 1, c = a + rowLen + 1, d = a + rowLen;+ this.quad(a, b, c, d);+ }+ if (o.capEnd !== false) { const c = this.v(path[N - 1][0], path[N - 1][1], path[N - 1][2], T[N - 1][0], T[N - 1][1], T[N - 1][2], 0, 0, s); const b2 = base + (N - 1) * rowLen; const ids = []; for (let k = 0; k < n; k++) ids.push(b2 + k); for (let k = 0; k < n; k++) this.tri(c, ids[k], b2 + k + 1); }+ if (o.capStart) { const c = this.v(path[0][0], path[0][1], path[0][2], -T[0][0], -T[0][1], -T[0][2], 0, 0, s); for (let k = 0; k < n; k++) this.tri(c, base + k + 1, base + k); }+ return this;+ }+ // parametric grid: fn(u,v) -> [x,y,z, shade?]; winding u x v = front. Smooth normals from the grid.+ grid(fn, nu, nv, o = {}) {+ const pts = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pts.push(fn(i / nu, j / nv));+ const id = (i, j) => j * (nu + 1) + i, base = this.count, flip = o.flip ? -1 : 1, s = o.s === undefined ? 1 : o.s;+ const wrapU = !!o.wrapU;+ let vLen = 0;+ for (let j = 0; j <= nv; j++) {+ if (j > 0) { const a = pts[id(0, j - 1)], b = pts[id(0, j)]; vLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ // o.metric: measure across from the middle of each row, so a converging surface keeps true scale instead of a fan of streaks+ let uLen = 0;+ if (o.metric) { let total = 0; for (let i = 1; i <= nu; i++) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; total += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); } uLen = -total / 2; }+ for (let i = 0; i <= nu; i++) {+ if (i > 0) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; uLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ const iP = wrapU ? (i === 0 ? nu - 1 : i - 1) : Math.max(0, i - 1), iN = wrapU ? (i === nu ? 1 : i + 1) : Math.min(nu, i + 1);+ const A = pts[id(iP, j)], B = pts[id(iN, j)], C = pts[id(i, Math.max(0, j - 1))], D = pts[id(i, Math.min(nv, j + 1))];+ const ux = B[0] - A[0], uy = B[1] - A[1], uz = B[2] - A[2], vx = D[0] - C[0], vy = D[1] - C[1], vz = D[2] - C[2];+ const p = pts[id(i, j)];+ const uvU = o.boxUV ? undefined : uLen, uvV = o.boxUV ? undefined : vLen;+ this.v(p[0], p[1], p[2], flip * (uy * vz - uz * vy), flip * (uz * vx - ux * vz), flip * (ux * vy - uy * vx), uvU, uvV, s * (p[3] === undefined ? 1 : p[3]));+ }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) {+ const a = base + id(i, j), b = base + id(i + 1, j), c = base + id(i + 1, j + 1), d = base + id(i, j + 1);+ if (flip > 0) this.quad(a, b, c, d); else this.quad(a, d, c, b);+ }+ return this;+ }+ // 2D outline with holes, extruded along Z from z0 to z1 (front +Z). Reveal sides included.+ slab(outline, holes = [], z0 = 0, z1 = 0.1, o = {}) {+ const so = area2(outline) > 0 ? outline : outline.slice().reverse();+ const hs = holes.map((h) => (area2(h) < 0 ? h : h.slice().reverse()));+ const tris = triangulate(so, hs);+ const all = so.concat(...hs);+ const sf = o.sf === undefined ? 1 : o.sf, sb = o.sb === undefined ? 1 : o.sb, sr = o.sr === undefined ? 0.72 : o.sr;+ if (!o.noFront) { const ids = all.map((p) => this.v(p[0], p[1], z1, 0, 0, 1, undefined, undefined, sf)); for (const t of tris) this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ if (!o.noBack) { const ids = all.map((p) => this.v(p[0], p[1], z0, 0, 0, -1, undefined, undefined, sb)); for (const t of tris) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); }+ const side = (loop, s) => {+ const L = loop.length; if (o.noSides) return;+ for (let i = 0; i < L; i++) {+ const p = loop[i], q = loop[(i + 1) % L]; const dx = q[0] - p[0], dy = q[1] - p[1];+ const l = Math.hypot(dx, dy) || 1, nx = dy / l, ny = -dx / l;+ const a = this.v(p[0], p[1], z0, nx, ny, 0, undefined, undefined, s), b = this.v(q[0], q[1], z0, nx, ny, 0, undefined, undefined, s), c = this.v(q[0], q[1], z1, nx, ny, 0, undefined, undefined, s), d = this.v(p[0], p[1], z1, nx, ny, 0, undefined, undefined, s);+ this.quad(a, b, c, d);+ }+ };+ if (!o.noOuterSides) side(so, o.so === undefined ? 0.85 : o.so);+ hs.forEach((h) => side(h, sr));+ return this;+ }+ // Vertical prism from an (x,z) polygon+ prism(pts, y0, y1, o = {}) {+ let A = 0; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; A += p[0] * q[1] - q[0] * p[1]; }+ const L = A > 0 ? pts : pts.slice().reverse(), s = o.s === undefined ? 1 : o.s, st = o.st === undefined ? s : o.st, n = L.length, k = o.topScale === undefined ? 1 : o.topScale;+ let cx = 0, cz = 0; for (const p of L) { cx += p[0]; cz += p[1]; } cx /= n; cz /= n;+ const top = L.map((p) => [cx + (p[0] - cx) * k, cz + (p[1] - cz) * k]);+ for (let i = 0; i < n; i++) {+ const p = L[i], q = L[(i + 1) % n], P2 = top[i], Q2 = top[(i + 1) % n];+ this.face([[q[0], y0, q[1], s], [p[0], y0, p[1], s], [P2[0], y1, P2[1], st], [Q2[0], y1, Q2[1], st]]);+ }+ if (!o.noTop && k > 1e-4) { const c = top.map((p) => this.v(p[0], y1, p[1], 0, 1, 0, undefined, undefined, st)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i + 1], c[i]); }+ if (o.bot) { const c = L.map((p) => this.v(p[0], y0, p[1], 0, -1, 0, undefined, undefined, s * 0.6)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i], c[i + 1]); }+ return this;+ }+ // Welds vertices that agree in every attribute: sweeps and lathes otherwise export many duplicates+ // Split every edge longer than L. The rule depends only on the edge, so neighbours split alike and no crack opens.+ // Long walls need vertices for the baked light to land on.+ tess(L) {+ this.weld();+ const L2 = L * L, { P: p, N: nn, UV: uv, C: c } = this;+ for (let pass = 0; pass < 7; pass++) {+ const mid = new Map(), I2 = [], I0 = this.I; let changed = false;+ const long = (a, b) => { const x = p[a * 3] - p[b * 3], y = p[a * 3 + 1] - p[b * 3 + 1], z = p[a * 3 + 2] - p[b * 3 + 2]; return x * x + y * y + z * z > L2; };+ const midOf = (a, b) => {+ const lo = Math.min(a, b), hi = Math.max(a, b), key = lo * 4194304 + hi; let m = mid.get(key);+ if (m === undefined) {+ m = p.length / 3; mid.set(key, m);+ p.push((p[lo * 3] + p[hi * 3]) / 2, (p[lo * 3 + 1] + p[hi * 3 + 1]) / 2, (p[lo * 3 + 2] + p[hi * 3 + 2]) / 2);+ let nx = nn[lo * 3] + nn[hi * 3], ny = nn[lo * 3 + 1] + nn[hi * 3 + 1], nz = nn[lo * 3 + 2] + nn[hi * 3 + 2]; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;+ nn.push(nx / l, ny / l, nz / l);+ uv.push((uv[lo * 2] + uv[hi * 2]) / 2, (uv[lo * 2 + 1] + uv[hi * 2 + 1]) / 2);+ c.push((c[lo * 3] + c[hi * 3]) >> 1, (c[lo * 3 + 1] + c[hi * 3 + 1]) >> 1, (c[lo * 3 + 2] + c[hi * 3 + 2]) >> 1);+ }+ return m;+ };+ for (let t = 0; t < I0.length; t += 3) {+ const a = I0[t], b = I0[t + 1], d = I0[t + 2], ab = long(a, b), bd = long(b, d), da = long(d, a);+ if (!ab && !bd && !da) { I2.push(a, b, d); continue; }+ changed = true;+ const m1 = ab ? midOf(a, b) : -1, m2 = bd ? midOf(b, d) : -1, m3 = da ? midOf(d, a) : -1;+ if (ab && bd && da) I2.push(a, m1, m3, m1, b, m2, m3, m2, d, m1, m2, m3);+ else if (ab && bd) I2.push(a, m1, d, m1, m2, d, m1, b, m2);+ else if (bd && da) I2.push(b, m2, a, m2, m3, a, m2, d, m3);+ else if (da && ab) I2.push(d, m3, b, m3, m1, b, m3, a, m1);+ else if (ab) I2.push(a, m1, d, m1, b, d);+ else if (bd) I2.push(b, m2, a, m2, d, a);+ else I2.push(d, m3, b, m3, a, b);+ }+ this.I = I2;+ if (!changed) break;+ }+ this.geo = null;+ return this;+ }+ weld() {+ if (this.welded) return this;+ const n = this.count, map = new Map(), remap = new Int32Array(n), P2 = [], N2 = [], U2 = [], C2 = [];+ const { P: p, N: nn, UV: uv, C: c } = this, col = !this.noColor;+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, i2 = i * 2;+ const key = Math.round(p[i3] * 4000) + ',' + Math.round(p[i3 + 1] * 4000) + ',' + Math.round(p[i3 + 2] * 4000) + ',' + Math.round(nn[i3] * 90) + ',' + Math.round(nn[i3 + 1] * 90) + ',' + Math.round(nn[i3 + 2] * 90) + ',' + Math.round(uv[i2] * 800) + ',' + Math.round(uv[i2 + 1] * 800) + (col ? ',' + c[i3] + ',' + c[i3 + 1] + ',' + c[i3 + 2] : '');+ let k = map.get(key);+ if (k === undefined) { k = P2.length / 3; map.set(key, k); P2.push(p[i3], p[i3 + 1], p[i3 + 2]); N2.push(nn[i3], nn[i3 + 1], nn[i3 + 2]); U2.push(uv[i2], uv[i2 + 1]); C2.push(c[i3], c[i3 + 1], c[i3 + 2]); }+ remap[i] = k;+ }+ const I2 = [];+ for (let t = 0; t < this.I.length; t += 3) {+ const a = remap[this.I[t]], b = remap[this.I[t + 1]], d = remap[this.I[t + 2]];+ if (a !== b && b !== d && a !== d) I2.push(a, b, d);+ }+ this.P = P2; this.N = N2; this.UV = U2; this.C = C2; this.I = I2; this.welded = true;+ return this;+ }+ toGeo() {+ if (this.geo) return this.geo;+ this.weld();+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.P), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(this.N), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.UV), 2));+ if (!this.noColor) g.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(this.C), 3, true));+ g.setIndex(new THREE.BufferAttribute(this.count > 65535 ? new Uint32Array(this.I) : new Uint16Array(this.I), 1));+ this.geo = g;+ return g;+ }+}+function area2(p) { let a = 0; for (let i = 0; i < p.length; i++) { const q = p[(i + 1) % p.length]; a += p[i][0] * q[1] - q[0] * p[i][1]; } return a / 2; }+function triangulate(contour, holes = []) {+ const V = (p) => new THREE.Vector2(p[0], p[1]);+ const tris = THREE.ShapeUtils.triangulateShape(contour.map(V), holes.map((h) => h.map(V)));+ const all = contour.concat(...holes);+ return tris.map((t) => {+ const a = all[t[0]], b = all[t[1]], c = all[t[2]];+ const s = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ return s >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]];+ });+}++// ---- 2D curves -------------------------------------------------------------------+// Two-centred pointed arc: span w, rise h, base y=0 centred at x=0, from left spring to right spring+function pointedArc(w, h, n = 8) {+ const c = (h * h - w * w / 4) / w, r = c + w / 2, pts = [];+ const a0 = PI, a1 = Math.atan2(h, -c);+ for (let i = 0; i <= n; i++) { const a = lerp(a0, a1, i / n); pts.push([c + r * Math.cos(a), r * Math.sin(a)]); }+ for (let i = n - 1; i >= 0; i--) { const a = lerp(a0, a1, i / n); pts.push([-(c + r * Math.cos(a)), r * Math.sin(a)]); }+ pts[n][0] = 0;+ return pts;+}+// CCW outline of a pointed opening: width w, sill y0, spring ys, rise h+function archOutline(w, y0, ys, h, n = 8) {+ const arc = pointedArc(w, h, n).map((p) => [p[0], p[1] + ys]);+ arc.reverse(); // right spring -> apex -> left spring (CCW)+ return [[-w / 2, y0], [w / 2, y0], ...arc];+}+const ringPts = (cx, cy, r, n = 24, ph = 0) => { const a = []; for (let i = 0; i < n; i++) { const t = ph + TAU * i / n; a.push([cx + r * Math.cos(t), cy + r * Math.sin(t)]); } return a; };+const ofsY = (pts, dy) => pts.map((p) => [p[0], p[1] + dy]);+const ofsXY = (pts, dx, dy) => pts.map((p) => [p[0] + dx, p[1] + dy]);++// ---- Sections (a, b, shade?) closed CCW ------------------------------------------+// Moulded arch section with convex rolls: a=0 at the soffit, grows outward; symmetric in b.+function rollSection(w, d, rolls = 3, amp = 0.07, seg = 3) {+ const half = [];+ const A = [w, d / 2], B = [0, d * 0.12], dx = B[0] - A[0], dy = B[1] - A[1], l = Math.hypot(dx, dy);+ for (let k = 0; k < rolls; k++) for (let i = 0; i < seg; i++) {+ const t = (k + i / seg) / rolls, ph = i / seg;+ const off = amp * Math.pow(Math.sin(PI * ph), 0.7), sh = 0.68 + 0.32 * Math.sin(PI * ph);+ half.push([lerp(A[0], B[0], t) + off * dy / l, lerp(A[1], B[1], t) - off * dx / l, sh]);+ }+ half.push([B[0], B[1], 0.8]);+ const back = half.slice().reverse().map((p) => [p[0], -p[1], p[2]]);+ return [[w, -d / 2, 1], ...half, ...back.slice(0, -1)];+}+// Cheapest moulded bar: a hexagon, smooth enough to read as a roll at distance+function hexBar(w, d) { const a = w / 2, b = d / 2; return [[a, -b * 0.5], [a, b * 0.5], [0, b], [-a, b * 0.5], [-a, -b * 0.5], [0, -b]]; }+// Simple chamfered bar centred on the path, width w (in plane), depth d+function barSection(w, d, ch = 0.25) {+ const a = w / 2, b = d / 2, c = Math.min(a, b) * ch * 2;+ return [[-a, -b + c], [-a + c, -b], [a - c, -b], [a, -b + c], [a, b - c], [a - c, b], [-a + c, b], [-a, b - c]];+}+// Tracery bar with a roll on the front and back faces+function traceryBar(w, d) {+ const a = w / 2, b = d / 2, s = [];+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([a * Math.cos(t), b * 0.62 + b * 0.38 * Math.sin(t)]); }+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([-a * Math.cos(t), -b * 0.62 - b * 0.38 * Math.sin(t)]); }+ return s;+}+// String course / cornice projecting along +b from the wall at b=0, height h (a)+function stringSection(h = 0.3, out = 0.25) {+ return [[-h / 2, 0], [-h / 2, out * 0.3, 0.55], [-h * 0.25, out * 0.62, 0.7], [-h * 0.02, out * 0.72, 0.85], [h * 0.12, out, 1], [h * 0.2, out * 0.98, 1], [h / 2, out * 0.05, 1], [h / 2, 0]].reverse();+}++const spline = (pts, sub = 4) => { // Catmull-Rom through 3D points+ const out = [], n = pts.length, g = (i) => pts[clamp(i, 0, n - 1)];+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < sub; k++) {+ const t = k / sub, p0 = g(i - 1), p1 = g(i), p2 = g(i + 1), p3 = g(i + 2), v = [];+ for (let c = 0; c < 3; c++) v.push(0.5 * (2 * p1[c] + (p2[c] - p0[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t * t + (3 * p1[c] - p0[c] - 3 * p2[c] + p3[c]) * t * t * t));+ out.push(v);+ }+ out.push(pts[n - 1]);+ return out;+};+const blob = (b, c, r, nu = 8, nv = 6, sh = 1) => b.grid((u, v) => {+ const ph = -PI + TAU * u, la = -PI / 2 + PI * v;+ return [c[0] + r[0] * Math.cos(la) * Math.cos(ph), c[1] + r[1] * Math.sin(la), c[2] + r[2] * Math.cos(la) * Math.sin(ph), sh];+}, nu, nv, { flip: true, wrapU: true });++// ==================================================================================+// MATERIALS: procedural PBR, with height-derived normals+// ==================================================================================+const MAT = {};+const TILE = { ashlar: 8, fine: 2, lead: 8, pave: 2.4, oak: 1.2, iron: 0.8, gold: 1, flag: 6, earth: 5 };++function ashlarOps(rng, S, tone, joint, stains) {+ // Irregular coursed blocks; shared between albedo and height by replaying the same rng seed+ const ops = [], rows = 7, rowH = Math.floor(S / rows);+ for (let r = 0; r < rows; r++) {+ const y0 = r * rowH; let x = Math.round(rng() * S / 5) % S;+ const n = 3 + Math.floor(rng() * 2), widths = [];+ for (let i = 0; i < n; i++) widths.push(0.7 + rng() * 0.6);+ const tot = widths.reduce((a, c) => a + c, 0);+ for (let i = 0; i < n; i++) {+ const w = Math.round(widths[i] / tot * S), dv = rng(), a = rng();+ const col = tone[Math.floor(dv * tone.length)];+ const x0 = x % S, wr = Math.min(w, S - x0), op = Math.round(10 + a * 34);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y0, width: wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ if (wr < w) ops.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: y0, width: w - wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ x += w;+ ops.push({ op: 'paint', shape: { kind: 'line', from: [x % S, y0], to: [x % S, y0 + rowH], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y0], to: [S, y0], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ return ops.slice(0, 58).concat(stains || []);+}++// Regions of the decal sheet as fractions [u0, v0, u1, v1]; a streak's top edge is at v0+const DECAL_UV = { streak: [0, 0, 0.5, 0.5], soft: [0.5, 0, 1, 0.25], soot: [0.5, 0.25, 1, 0.5], ground: [0, 0.5, 0.5, 1], drip: [0.5, 0.75, 1, 1] };+async function makeMaterials() {+ const S = 512, repeat = { minFilter: 'linear-mipmap-linear', magFilter: 'linear', wrapS: 'repeat', wrapT: 'repeat' };+ const noise = (a, b, scale, oct, seed, opacity, blend) => ({ op: 'noise', colorA: a, colorB: b, scale, octaves: oct, seed, opacity, blend: blend || 'normal' });+ const spec3 = (usage, base, ops, size = S) => ({ schemaVersion: 3, width: size, height: size, usage, operations: [{ op: 'legacy-v2', spec: { schemaVersion: 2, size, usage, layers: base } }, ...ops] });+ const v3 = (usage, base, ops, size = S) => proceduralTexture(spec3(usage, base, ops, size));+ const stamps = (shape, rgba, count, seed, opacity, area) => ({ op: 'stamps', shape, rgba, count, seed, area: area || { x: 0, y: 0, width: S, height: S }, opacity });+ const ell = (w, h) => ({ kind: 'ellipse', x: 0, y: 0, width: w, height: h });+ const tool = (dx, dy, w = 1) => ({ kind: 'line', from: [0, 0], to: [dx, dy], width: w }); // a stroke of the claw chisel++ // ---- Weathered ashlar: 8 m repeat, every block its own stone. Colour varies by block, chisel marks+ // in the relief instead of round pits, rain and soot already sunk into the surface ----+ const quads = [[0, 0], [S, 0], [0, S], [S, S]];+ const ashAlbedo = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb9ad96 }, noise(0x968b76, 0xcdc3ad, 3, 2, seed + 1, 0.85), noise(0x7f7666, 0xc6bca6, 17, 2, seed + 2, 0.4)],+ ashlarOps(mkRng(seed), S, [[78, 72, 60], [226, 216, 190], [158, 132, 92], [122, 128, 124]], { w: 2, rgba: [70, 64, 54, 255], op: 225 }, [+ stamps(ell(20, 70), [70, 66, 56, 255], 26, seed + 3, 24), stamps(ell(40, 26), [52, 56, 46, 255], 16, seed + 4, 20), stamps(tool(11, 6), [96, 88, 74, 255], 110, seed + 5, 34),+ ]));+ const ashHeight = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb4b4b4 }, noise(0x8c8c8c, 0xd0d0d0, 11, 2, seed + 6, 0.7)],+ ashlarOps(mkRng(seed), S, [[126, 126, 126], [214, 214, 214], [170, 170, 170], [150, 150, 150]], { w: 3, rgba: [12, 12, 12, 255], op: 255 }, [+ stamps(tool(14, 8), [86, 86, 86, 255], 120, seed + 8, 90), stamps(tool(-12, 9), [226, 226, 226, 255], 110, seed + 9, 70),+ ]));+ const atlasOf = async (usage, make, seeds) => textureAtlas({ schemaVersion: 1, width: 2 * S, height: 2 * S, pack: { edge: 2048, gutter: 0 }, usage, sampler: repeat, regions: quads.map((p, i) => ({ name: 'q' + i, content: { x: p[0], y: p[1], width: S, height: S }, gutter: 0, source: { kind: 'procedural', spec: make(p, seeds[i]) } })) });+ const ashA = await atlasOf('albedo', ashAlbedo, [77, 131, 211, 307]);+ const ashH = await atlasOf('albedo', ashHeight, [77, 131, 211, 307]);+ MAT.ashlar = pbrMaterial({ albedo: ashA, normal: normalMapFromHeight(ashH, { strength: 2.6 }), roughness: 0.94, metalness: 0, name: 'Limestone_Ashlar' });++ // Dressed stone: no joints; chisel strokes and mottling live in the colour, because a normal map here would+ // add 16 bytes of tangent to every vertex of every moulding+ const finA = v3('albedo', [{ op: 'solid', color: 0xc2b7a0 }, noise(0xb5ab95, 0xcdc4af, 5, 3, 31, 0.8), noise(0xa79d88, 0xcbc2ad, 23, 3, 32, 0.34)], [+ stamps(tool(9, 5), [120, 112, 96, 255], 130, 33, 34), stamps(tool(-7, 6), [214, 206, 188, 255], 80, 34, 30),+ ]);+ MAT.fine = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name: 'Limestone_Dressed' });+ const figTone = (name, r, g, b) => { const m = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name }); m.color.setRGB(r, g, b); m.vertexColors = true; return m; };+ MAT.figure = figTone('Sculpture_Sunlit', 0.86, 0.8, 0.68); // figures take the same stone under different light+ MAT.figureHalf = figTone('Sculpture_HalfShade', 0.5, 0.49, 0.47);+ MAT.figureDeep = figTone('Sculpture_DeepShade', 0.3, 0.31, 0.34);+ MAT.figureIn = figTone('Sculpture_Interior', 0.62, 0.53, 0.42);++ // ---- Patinated lead: long sheets a metre wide running the whole way down the slope. They differ only a little from each+ // other, are lapped once in a long run, and carry the streaks that rain draws down them. The tile repeats every 8 m ----+ const LS = 512, leadOps = (tones, lapOp, seamOp, rngSeed) => {+ const r = mkRng(rngSeed), ops = [], cols = 8, W = LS / cols;+ for (let c = 0; c < cols; c++) {+ const t = tones[Math.floor(r() * tones.length)], lap = Math.floor((c % 2 ? 0.28 : 0.72) * LS + (r() - 0.5) * 40);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: c * W, y: 0, width: W, height: LS }, rgba: [t[0], t[1], t[2], 255], opacity: 12 + Math.floor(r() * 26) });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, lap], to: [(c + 1) * W, lap], width: lapOp.w }, rgba: lapOp.rgba, opacity: lapOp.op });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, 0], to: [c * W, LS], width: seamOp.w }, rgba: seamOp.rgba, opacity: seamOp.op });+ }+ return ops;+ };+ const leadArea = { x: 0, y: 0, width: LS, height: LS };+ const leadA = v3('albedo', [{ op: 'solid', color: 0x6f7a7e }, noise(0x566166, 0x8d9896, 5, 2, 41, 0.75)], [+ ...leadOps([[86, 98, 102], [118, 130, 130], [66, 76, 82], [140, 150, 146], [100, 104, 100]], { w: 2, rgba: [36, 42, 46, 255], op: 220 }, { w: 2, rgba: [40, 46, 50, 255], op: 180 }, 44),+ stamps({ kind: 'rectangle', x: 0, y: 0, width: 4, height: 180 }, [208, 214, 206, 255], 70, 45, 22, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 2, height: 260 }, [34, 40, 44, 255], 90, 46, 36, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 6, height: 120 }, [150, 160, 154, 255], 40, 47, 16, leadArea),+ ], LS);+ const leadH = v3('albedo', [{ op: 'solid', color: 0x8c8c8c }, noise(0x848484, 0x989898, 9, 2, 48, 0.8)], [+ ...leadOps([[140, 140, 140], [132, 132, 132], [148, 148, 148]], { w: 3, rgba: [232, 232, 232, 255], op: 255 }, { w: 5, rgba: [250, 250, 250, 255], op: 255 }, 44),+ stamps(ell(17, 11), [116, 116, 116, 255], 110, 49, 60, leadArea),+ ], LS);+ MAT.lead = pbrMaterial({ albedo: leadA, normal: normalMapFromHeight(leadH, { strength: 3 }), roughness: 0.6, metalness: 0.5, name: 'Lead_Patinated' });++ // ---- Gold leaf: a bright warm metal that still reads as gold where the environment is dull ----+ const gldA = v3('albedo', [{ op: 'solid', color: 0xffc644 }, noise(0xf0a830, 0xffd764, 14, 2, 51, 0.6)], [stamps(ell(5, 4), [176, 110, 30, 255], 60, 52, 90, { x: 0, y: 0, width: 128, height: 128 })], 128);+ MAT.gold = pbrMaterial({ albedo: gldA, roughness: 0.25, metalness: 1, name: 'Gilding' });+ MAT.gold.color.setRGB(0.92, 0.78, 0.56);+ MAT.gold.emissive.setRGB(1, 0.6, 0.14); MAT.gold.emissiveIntensity = 0.1;++ // Aged oak and forged iron from the approved scanned sets+ MAT.oak = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.oak-veneer-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.oak-veneer-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.oak-veneer-arm.v1'), name: 'Oak_Aged' });+ MAT.iron = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.rusted-metal-albedo.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.rusted-metal-arm.v1'), name: 'Iron_Forged' });++ // Paving: worn limestone and grey marble, polished by seven centuries of feet+ const pavA = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'checker', colorA: 0xd2cab6, colorB: 0x8c8678, squares: 4 }, noise(0x7c766a, 0xe0dac8, 9, 3, 61, 0.42), { op: 'bricks', brick: 0xffffff, mortar: 0x5a564e, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ const pavH = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'solid', color: 0xc8c8c8 }, noise(0xb0b0b0, 0xdcdcdc, 15, 3, 62, 0.5), { op: 'bricks', brick: 0xffffff, mortar: 0x202020, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ MAT.pave = pbrMaterial({ albedo: pavA, normal: normalMapFromHeight(pavH, { strength: 1.8 }), roughness: 0.42, metalness: 0, name: 'Paving_Worn' });+ for (const k of ['ashlar', 'fine', 'oak', 'iron', 'pave', 'lead']) MAT[k].vertexColors = true;++ // ---- Weathering decals: rain streaks, soot, ground damp and moss, cast shadow. Alpha-blended overlays+ // give crisp detail on walls that have very few vertices ----+ const DS = 256, rd = mkRng(99), polyStreak = (x, y0, L, w, rgb, op) => ({ op: 'paint', shape: { kind: 'polygon', points: [[x, y0], [x + w, y0], [x + w * 0.8 + 1, y0 + L * 0.45], [x + w * 0.55 + 1, y0 + L * 0.8], [x + w * 0.45, y0 + L], [x + w * 0.3, y0 + L * 0.75], [x + w * 0.15, y0 + L * 0.4]].map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: [rgb[0], rgb[1], rgb[2], 255], opacity: op });+ const streakOps = [], dripOps = [];+ for (let i = 0; i < 44; i++) { const x = Math.floor(rd() * (DS - 9)), dark = rd() > 0.26, L = 65 + Math.floor(rd() * 185), w = 2 + Math.floor(rd() * 7); streakOps.push(polyStreak(x, 0, Math.min(L, DS - 2), w, dark ? [44, 40, 34] : [176, 170, 150], dark ? 40 + Math.floor(rd() * 70) : 26 + Math.floor(rd() * 30))); }+ for (let i = 0; i < 12; i++) streakOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 3 + i * 2 }, rgba: [34, 32, 28, 255], opacity: 12 });+ for (let i = 0; i < 38; i++) { const x = Math.floor(rd() * (DS - 8)), L = 20 + Math.floor(rd() * 100), w = 1 + Math.floor(rd() * 6); dripOps.push(polyStreak(x, 0, Math.min(L, 125), w, [38, 36, 30], 44 + Math.floor(rd() * 70))); }+ for (let i = 0; i < 10; i++) dripOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 2 + i * 2 }, rgba: [28, 26, 24, 255], opacity: 16 });+ const softOps = []; for (let i = 0; i < 30; i++) softOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: Math.max(1, Math.round(128 * Math.pow(1 - i / 30, 1.5))) }, rgba: [18, 18, 24, 255], opacity: 15 });+ const dampOps = [];+ for (let i = 0; i < 18; i++) { const h = Math.round(DS * Math.pow((i + 1) / 18, 1.6)); dampOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: DS - h, width: DS, height: h }, rgba: i % 3 ? [38, 42, 28, 255] : [50, 58, 34, 255], opacity: 9 }); }+ dampOps.push(stamps(ell(13, 8), [66, 84, 42, 255], 110, 71, 46, { x: 0, y: 120, width: DS, height: 134 }), stamps(ell(5, 28), [30, 32, 24, 255], 80, 72, 64, { x: 0, y: 60, width: DS, height: 190 }), stamps(ell(3, 3), [120, 132, 76, 255], 60, 73, 90, { x: 0, y: 100, width: DS, height: 150 }));+ const clear = [0, 0, 0, 0];+ const dreg = (name, x, y, w, h, ops, bg) => ({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'albedo', background: bg || clear, operations: ops } } });+ const decalTex = await textureAtlas({ schemaVersion: 1, width: 512, height: 512, usage: 'albedo', regions: [+ dreg('streak', 0, 0, DS, DS, streakOps.slice(0, 64)),+ dreg('soft', 256, 0, DS, 128, softOps),+ dreg('soot', 256, 128, DS, 128, [stamps(ell(35, 27), [24, 22, 20, 255], 150, 74, 20, { x: 20, y: 15, width: 200, height: 90 }), stamps(ell(13, 11), [18, 16, 14, 255], 90, 75, 36, { x: 45, y: 25, width: 160, height: 75 })]),+ dreg('ground', 0, 256, DS, DS, dampOps),+ dreg('drip', 256, 384, DS, 128, dripOps.slice(0, 64)),+ ] });+ MAT.decal = pbrMaterial({ albedo: decalTex, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Weathering_Decals' });+ MAT.decal.depthWrite = false;++ await makeGlassMaterials();++ // ---- The light of the south windows: a blank image here, filled in by bakeWindowLight once the world has been voxelized ----+ WINDOW_LIGHT.tex = proceduralTexture({ schemaVersion: 3, width: WINDOW_LIGHT.W, height: WINDOW_LIGHT.H, usage: 'albedo', operations: [{ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: 1, height: 1 }, rgba: [0, 0, 0, 0] }] });+ MAT.pool = pbrMaterial({ albedo: WINDOW_LIGHT.tex, roughness: 0.95, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightPool' });+ MAT.pool.depthWrite = false;++ // ---- The ground: worn flags in irregular courses, and rough grass over packed earth ----+ const flagTones = [[74, 70, 62], [150, 142, 124], [112, 104, 88], [96, 98, 90]];+ const flagA = v3('albedo', [{ op: 'solid', color: 0x8f8778 }, noise(0x6f695c, 0xa89f8d, 4, 2, 81, 0.8), noise(0x5e594e, 0x9a9282, 19, 2, 82, 0.35)], ashlarOps(mkRng(83), S, flagTones, { w: 4, rgba: [38, 34, 28, 255], op: 235 }, [stamps(ell(30, 18), [168, 160, 140, 255], 40, 84, 26), stamps(tool(8, 3), [70, 66, 58, 255], 70, 85, 30)]));+ const flagH = v3('albedo', [{ op: 'solid', color: 0xb0b0b0 }, noise(0x969696, 0xc4c4c4, 9, 2, 86, 0.6)], ashlarOps(mkRng(83), S, [[140, 140, 140], [190, 190, 190], [165, 165, 165], [150, 150, 150]], { w: 5, rgba: [20, 20, 20, 255], op: 255 }, [stamps(ell(26, 16), [205, 205, 205, 255], 30, 87, 40)]));+ MAT.flag = pbrMaterial({ albedo: flagA, normal: normalMapFromHeight(flagH, { strength: 2.2 }), roughness: 0.9, metalness: 0, name: 'Ground_Flagstone' });+ MAT.flag.vertexColors = true;+ const EA = 256, eArea = { x: 0, y: 0, width: EA, height: EA };+ const earthA = v3('albedo', [{ op: 'solid', color: 0x5d5a3a }, noise(0x4a4a2c, 0x6f6a44, 5, 2, 91, 0.8), noise(0x3f4226, 0x7a7550, 23, 2, 92, 0.4)], [stamps(tool(2, 8), [70, 88, 40, 255], 120, 93, 90, eArea), stamps(tool(-2, 7), [104, 112, 58, 255], 90, 94, 70, eArea), stamps(ell(26, 16), [88, 74, 52, 255], 30, 95, 60, eArea)], EA);+ MAT.earth = pbrMaterial({ albedo: earthA, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Ground_Earth' });+ MAT.earth.vertexColors = true;+ MAT.earth.depthWrite = false;+}++// ---- Atlas of the glass painter: pot-metal colours, painted drapery, drawn faces, ornament ------+const GL = { w: 1536, h: 1280, cell: 256, small: 128, fam: {}, tile: {}, face: {}, orn: {}, ground: {} };+// [name, base, light, dark]: real pot-metal glass is deep, and no two sheets match+const GLASS_FAMILIES = [+ ['ruby', [118, 6, 14], [172, 14, 22], [66, 3, 10]], ['cobalt', [10, 20, 104], [20, 44, 160], [5, 10, 62]], ['azure', [16, 54, 128], [36, 90, 170], [8, 32, 88]], ['emerald', [6, 62, 38], [14, 96, 54], [3, 38, 24]],+ ['green', [42, 74, 24], [74, 106, 34], [24, 48, 14]], ['gold', [178, 116, 16], [218, 158, 36], [130, 78, 8]], ['amber', [146, 72, 10], [186, 106, 20], [98, 44, 6]], ['purple', [58, 14, 80], [90, 26, 114], [34, 6, 50]],+ ['flesh', [186, 140, 104], [206, 164, 128], [156, 110, 80]], ['white', [172, 164, 132], [204, 194, 158], [136, 128, 100]], ['brown', [70, 40, 18], [100, 58, 28], [46, 24, 12]],+ ['teal', [8, 62, 70], [16, 92, 100], [5, 38, 46]], ['night', [5, 8, 40], [10, 16, 62], [2, 4, 24]],+];+const GLASS_ALIAS = { rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', sky: 'azure', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', yellow: 'gold', tan: 'amber', fleshDk: 'flesh', violet: 'purple', olive: 'green', forest: 'emerald', hair: 'brown', paint: 'lead' };+// Mosaic grounds, each a square of painted lozenges: [name, colour A, colour B, jewel]+// A ground stays one colour, in many sheets of it: ruby beside cobalt blends to magenta across a room, ruby on ruby does not+const GLASS_GROUNDS = [['rc', 'ruby', 'ruby', 'gold'], ['cr', 'cobalt', 'cobalt', 'gold'], ['eg', 'emerald', 'emerald', 'gold'], ['pr', 'cobalt', 'azure', 'white']];+// What a leaded window looks like from outside by day: nearly black quarries, each with a hint of its colour, and a faint grey came+function outerGlassOps() {+ const r = mkRng(4711), ops = [], cl = (v) => Math.max(0, Math.min(256, v)), hints = [[88, 10, 16], [12, 20, 96], [12, 20, 70], [70, 10, 14], [22, 26, 34], [66, 44, 12]];+ for (let b = 0; b <= 4; b++) for (let a = 0; a <= 4; a++) {+ if ((a + b) % 2 === 0) continue;+ const c = hints[Math.floor(r() * hints.length)], x = a * 64, y = b * 64;+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 64)], [cl(x + 64), cl(y)], [cl(x), cl(y + 64)], [cl(x - 64), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -1; k <= 1; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ for (let k = 1; k <= 3; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ return ops;+}+function groundOps(A, B, J, seed) { // lozenges 64 px across, a lead round each, a jewel on some crossings+ const r = mkRng(seed * 19 + 5), ops = [], cl = (v) => Math.max(0, Math.min(256, v));+ for (let b = 0; b <= 8; b++) for (let a = 0; a <= 8; a++) {+ if ((a + b) % 2 === 0) continue;+ const f = b % 2 ? A : B, x = a * 32, y = b * 32, t = r(), c = t < 0.45 ? f[1] : t < 0.8 ? f[2] : f[3];+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 32)], [cl(x + 32), cl(y)], [cl(x), cl(y + 32)], [cl(x - 32), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -3; k <= 3; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let k = 1; k <= 7; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let i = 0; i < 4; i++) { const x = 64 + (i % 2) * 128, y = 64 + (i >> 1) * 128; ops.push(paintEll(x, y, 13, 13, 255, [4, 4, 5])); ops.push(paintEll(x, y, 8, 8, 255, J[1])); }+ return ops.slice(0, 64);+}+const DRAPE_OF = { ruby: 'ruby', rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', cobalt: 'cobalt', azure: 'cobalt', sky: 'cobalt', night: 'cobalt', emerald: 'emerald', green: 'emerald', teal: 'emerald', forest: 'emerald', olive: 'emerald', gold: 'gold', amber: 'gold', yellow: 'gold', tan: 'gold', brown: 'gold', white: 'white', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', purple: 'purple', violet: 'purple' };+const GROUND_OF = { ruby: 'rc', rubyLt: 'rc', crimson: 'rc', cobalt: 'cr', azure: 'pr', sky: 'pr', emerald: 'eg', green: 'eg', violet: 'cr', purple: 'cr', teal: 'pr', outer: 'out' };+const PAINT = [36, 20, 10];+const paintLine = (a, b, w, op = 235) => ({ op: 'paint', shape: { kind: 'line', from: [Math.round(a[0]), Math.round(a[1])], to: [Math.round(b[0]), Math.round(b[1])], width: w }, rgba: [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintPoly = (pts, op, rgb) => ({ op: 'paint', shape: { kind: 'polygon', points: pts.map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintEll = (x, y, w, h, op, rgb) => ({ op: 'paint', shape: { kind: 'ellipse', x: Math.round(x - w / 2), y: Math.round(y - h / 2), width: Math.round(w), height: Math.round(h) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });++function familyOps(f, seed) { // one sheet of pot-metal: streaks, clouds, seeds, a thicker edge+ const r = mkRng(seed * 7 + 3), ops = [], C = GL.small, clampE = (x, w) => Math.max(w / 2, Math.min(C - w / 2, x));+ for (let i = 0; i < 10; i++) { const c = r() > 0.5 ? f[2] : f[3], w = 24 + Math.floor(r() * 60), h = 18 + Math.floor(r() * 44); ops.push(paintEll(clampE(r() * C, w), clampE(r() * C, h), w, h, 60 + Math.floor(r() * 90), c)); }+ for (let i = 0; i < 9; i++) { const c = r() > 0.5 ? f[2] : f[3], y = Math.floor(r() * C); ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y], to: [C, Math.max(0, Math.min(C, y + Math.floor((r() - 0.5) * 30)))], width: 2 + Math.floor(r() * 4) }, rgba: [c[0], c[1], c[2], 255], opacity: 70 + Math.floor(r() * 80) }); }+ ops.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 2, height: 2 }, rgba: [f[2][0], f[2][1], f[2][2], 255], count: 40, seed: seed + 11, area: { x: 0, y: 0, width: C, height: C }, opacity: 180 });+ return ops;+}+function drapeOps(f, seed) { // brown paint on coloured glass: long folds, each with a soft wash beside its trace line+ const r = mkRng(seed * 13 + 1), ops = [], W = 256, H = 512, dk = [Math.round(f[1][0] * 0.3), Math.round(f[1][1] * 0.3), Math.round(f[1][2] * 0.3)];+ for (let i = 0; i < 13; i++) {+ const x = 18 + i * 18 + (r() - 0.5) * 10, sw = (r() - 0.5) * 42, y0 = 4 + r() * 120, y1 = H - r() * 90, ym = (y0 + y1) / 2, wash = 9 + r() * 12;+ ops.push(paintPoly([[x, y0], [x + wash, y0 + 10], [x + sw * 0.6 + wash * 1.4, ym], [x + sw + wash, y1 - 10], [x + sw, y1], [x + sw * 0.6, ym]], 62 + Math.floor(r() * 36), PAINT));+ ops.push(paintLine([x, y0], [x + sw * 0.6, ym], 3)); ops.push(paintLine([x + sw * 0.6, ym], [x + sw, y1], 2));+ if (i % 3 === 0) ops.push(paintLine([x + sw * 0.6 + wash * 1.6, ym - 40], [x + sw + wash * 1.6, y1 - 60], 3, 150));+ }+ for (let i = 0; i < 4; i++) { const y = 60 + i * 110 + r() * 40; ops.push(paintPoly([[0, y], [W, y + 26 * (r() - 0.5)], [W, y + 22], [0, y + 18]], 40, PAINT)); }+ return ops.slice(0, 62);+}+// A drawn medieval head, 256 x 256, centred: trace lines in brown over a flesh pane, with washes for the modelling+function faceOps(kind) {+ const ops = [], c = 128, mir = (p) => [256 - p[0], p[1]], both = (fn) => { fn((p) => p); fn(mir); };+ const hairC = kind === 'elder' ? [150, 146, 122] : kind === 'angel' ? [150, 96, 30] : [66, 40, 18];+ // washes: the side of the face and temple, under the brow, beside the nose, under the lip+ both((m) => {+ ops.push(paintPoly([m([18, 84]), m([52, 74]), m([46, 150]), m([72, 214]), m([34, 196])], 78));+ ops.push(paintEll(m([88, 104])[0], 106, 58, 20, 64));+ ops.push(paintPoly([m([122, 118]), m([112, 160]), m([102, 172]), m([118, 172])], 58));+ });+ ops.push(paintEll(c, 200, 52, 11, 58));+ // hair+ if (kind !== 'virgin') {+ ops.push(paintPoly([[6, 118], [14, 56], [60, 10], [128, 0], [196, 10], [242, 56], [250, 118], [222, 96], [206, 56], [160, 42], [128, 54], [96, 42], [50, 56], [34, 96]], 255, hairC));+ for (let i = 0; i < 7; i++) { const a = 28 + i * 16; ops.push(paintLine([a, 94 - i * 7], [a + 36, 12 + i * 2], 3, 190)); ops.push(paintLine([256 - a, 94 - i * 7], [256 - a - 36, 12 + i * 2], 3, 190)); }+ } else {+ ops.push(paintPoly([[0, 150], [2, 56], [50, 8], [128, 0], [206, 8], [254, 56], [256, 150], [226, 118], [214, 66], [128, 46], [42, 66], [30, 118]], 255, [214, 216, 186]));+ ops.push(paintLine([38, 124], [50, 62], 4)); ops.push(paintLine([50, 62], [128, 44], 4)); ops.push(paintLine([128, 44], [206, 62], 4)); ops.push(paintLine([206, 62], [218, 124], 4));+ }+ // brows, lids, eyes+ both((m) => {+ ops.push(paintLine(m([60, 96]), m([84, 84]), 5)); ops.push(paintLine(m([84, 84]), m([116, 92]), 5));+ ops.push(paintLine(m([64, 116]), m([84, 104]), 4)); ops.push(paintLine(m([84, 104]), m([112, 116]), 4));+ ops.push(paintLine(m([66, 118]), m([90, 126]), 2)); ops.push(paintLine(m([90, 126]), m([112, 116]), 2));+ ops.push(paintEll(m([88, 115])[0], 115, 15, 15, 255));+ });+ // nose, nostril, mouth, chin+ ops.push(paintLine([120, 92], [114, 150], 4)); ops.push(paintLine([114, 150], [106, 166], 4)); ops.push(paintLine([106, 166], [124, 174], 3)); ops.push(paintLine([136, 172], [150, 166], 3));+ ops.push(paintLine([98, 192], [128, 187], 4)); ops.push(paintLine([128, 187], [158, 192], 4));+ ops.push(paintLine([112, 202], [144, 202], 2, 170));+ if (kind === 'elder' || kind === 'christ' || kind === 'king') {+ const bc = kind === 'elder' ? [168, 164, 138] : [76, 46, 20];+ ops.push(paintPoly([[40, 158], [66, 184], [94, 196], [128, 206], [162, 196], [190, 184], [216, 158], [206, 226], [160, 256], [96, 256], [50, 226]], 255, bc));+ for (let i = 0; i < 7; i++) { const x = 64 + i * 21; ops.push(paintLine([x, 192 + Math.abs(i - 3) * -3], [x + (i - 3) * 3, 252], 3, 210)); }+ ops.push(paintLine([78, 180], [120, 176], 4)); ops.push(paintLine([178, 180], [136, 176], 4));+ } else {+ ops.push(paintLine([100, 226], [128, 236], 3, 170)); ops.push(paintLine([128, 236], [156, 226], 3, 170));+ }+ return ops.slice(0, 64);+}+function ornOps(f, kind, seed) { // painted ornament for borders: a running stem with leaves, scratched out of a dark ground+ const ops = [], C = GL.small;+ ops.push(paintPoly([[0, 0], [C, 0], [C, C], [0, C]], 210));+ const lit = f[2];+ for (let i = 0; i < 5; i++) {+ const y = 13 + i * 26, s = i % 2 ? 1 : -1;+ ops.push(paintPoly([[C / 2, y - 11], [C / 2 + s * 36, y - 6], [C / 2 + s * 46, y + 7], [C / 2 + s * 23, y + 11], [C / 2, y + 13]], 255, lit));+ ops.push(paintLine([C / 2 + s * 4, y - 3], [C / 2 + s * 37, y + 4], 2));+ ops.push(paintLine([C / 2 + s * 4, y + 5], [C / 2 + s * 28, y + 6], 1));+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [C / 2, 0], to: [C / 2, C], width: 7 }, rgba: [lit[0], lit[1], lit[2], 255] });+ return ops;+}+async function makeGlassMaterials() {+ const regions = [], C = GL.cell, Sm = GL.small;+ const addR = (name, x, y, w, h, bg, ops) => regions.push({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'emissive', background: [bg[0], bg[1], bg[2], 255], operations: ops } } });+ // Layout: two rows of small cells (pot-metals, then ornament), then drapery, faces and mosaic grounds+ GLASS_FAMILIES.forEach((f, i) => { const x = (i % 12) * Sm, y = Math.floor(i / 12) * Sm; GL.fam[f[0]] = [x, y]; addR('fam_' + f[0], x, y, Sm, Sm, f[1], familyOps(f, i + 1)); });+ [['gold', 'gold'], ['white', 'white']].forEach((q, i) => { const f = GLASS_FAMILIES.find((v) => v[0] === q[0]), x = (i + 2) * Sm, y = Sm; GL.orn[q[1]] = [x, y]; addR('orn_' + q[1], x, y, Sm, Sm, f[1], ornOps(f, q[1], i + 90)); });+ // painted drapery, one tall tile per colour+ ['ruby', 'cobalt', 'emerald', 'gold', 'white', 'purple'].forEach((n, i) => { const f = GLASS_FAMILIES.find((q) => q[0] === n), x = i * C, y = C; GL.tile[n] = [x, y, C, 2 * C]; addR('drape_' + n, x, y, C, 2 * C, f[1], drapeOps(f, i + 40)); });+ // faces+ ['youth', 'elder', 'christ', 'king', 'virgin', 'angel'].forEach((n, i) => { const x = i * C, y = 3 * C; GL.face[n] = [x, y]; addR('face_' + n, x, y, C, C, n === 'virgin' ? [202, 162, 128] : [192, 148, 112], faceOps(n)); });+ // mosaic grounds+ GLASS_GROUNDS.forEach((q, i) => { const fam = (n) => GLASS_FAMILIES.find((v) => v[0] === n), x = i * C, y = 4 * C; GL.ground[q[0]] = [x, y]; addR('ground_' + q[0], x, y, C, C, fam(q[1])[3], groundOps(fam(q[1]), fam(q[2]), fam(q[3]), i + 3)); });+ GL.ground.out = [4 * C, 4 * C]; addR('ground_out', 4 * C, 4 * C, C, C, [10, 12, 18], outerGlassOps());+ GL.fam.lead = [1400, 140]; // the empty part of the sheet is black, and the came needs no pixels of its own+ const atlas = await textureAtlas({ schemaVersion: 1, usage: 'emissive', width: GL.w, height: GL.h, pack: { edge: 2048, gutter: 0 }, regions });+ // Seen from inside the glass is the only light: a black pane that shines+ MAT.glassIn = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow, roughness: 1, metalness: 0, name: 'StainedGlass_Interior' });+ MAT.glassIn.color.setRGB(0, 0, 0);+ // Seen from outside by day it is nearly black, a dull sheen and the colour only hinting through.+ // A rough surface keeps grazing angles from washing out to a pale reflection of the sky.+ MAT.glassOut = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.6, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior' });+ MAT.glassOut.color.setRGB(0.008, 0.009, 0.012);+ // Every other window from outside: the outer mosaic only, near black, with the came just catching the eye+ MAT.glassOutWin = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.62, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior_Windows' });+ MAT.glassOutWin.color.setRGB(0.008, 0.009, 0.012);+ // The great rose burns a little brighter than any other window in the building+ MAT.glassRose = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow * 1.22, roughness: 1, metalness: 0, name: 'StainedGlass_Rose' });+ MAT.glassRose.color.setRGB(0, 0, 0);+}++// Hierarchy helper: every mesh gets a unique role name; geometry is shared between placements.+let ROOT = null; const NAMES = new Map();+function uniq(n) { const c = (NAMES.get(n) || 0) + 1; NAMES.set(n, c); return c === 1 ? n : n + '_' + c; }+const STAT = new Map();+// Exporter costs measured on this host: pos+normal+uv 32 B, COLOR_0 written as 12 B floats+function batchBytes(b) { b.weld(); return b.count * (32 + (b.noColor ? 0 : 12)) + b.I.length * (b.count > 65535 ? 4 : 2); }+function place(name, batch, mat, o = {}) {+ if (!batch || !batch.I.length) return null;+ if (mat === MAT.gold) batch.noColor = true; // metal needs no baked shading: saves 12 B a vertex+ if (P.preview === 'unique' && batch.geo) return null; // size test: every geometry once, no instances+ if (mat === MAT.ashlar && !batch.tessellated && !batch.geo) { batch.tess(P.wallTessellation); batch.tessellated = true; }+ if (P.preview === 'stats') {+ const e = STAT.get(batch) || { name, n: 0, normalMapped: mat === MAT.ashlar || mat === MAT.lead || mat === MAT.pave || mat === MAT.oak };+ e.n++; STAT.set(batch, e);+ return null;+ }+ const part = createPart(uniq(name), batch.toGeo(), mat, { position: o.pos || [0, 0, 0], rotation: o.rot || [0, 0, 0], scale: o.scale || [1, 1, 1], parent: o.parent || ROOT });+ PLACED.push({ part, batch, mat });+ return part;+}+const PLACED = [];+function group(name, parent) { const g = new THREE.Group(); g.name = uniq(name); (parent || ROOT).add(g); return g; }++// ==================================================================================+// SCULPTURE: robed figures with carved drapery. A figure is a full surface, not a block:+// S-curve stance, tube folds breaking at the hem, swags across the body, sleeves, hands,+// modelled faces, hair and beards, and the attributes each saint carries.+// ==================================================================================+// Every figure is a signed-distance volume: a long column of cloth with deep pleats, a mantle+// laid over it, arms under the cloth, and a separately resolved head and hands. A surface-net+// pass extracts the skin, the simplifier keeps the carving and drops the rest, and ambient+// occlusion from the field itself is baked into the valleys of the folds.+const len3 = (x, y, z) => Math.sqrt(x * x + y * y + z * z);+const smin = (a, b, k) => { const h = Math.max(k - Math.abs(a - b), 0) / k; return Math.min(a, b) - h * h * k * 0.25; };+const smax = (a, b, k) => -smin(-a, -b, k);+const sdEll = (x, y, z, rx, ry, rz) => { const a = x / rx, b = y / ry, c = z / rz, k0 = Math.sqrt(a * a + b * b + c * c), d = a / rx, e = b / ry, f = c / rz, k1 = Math.sqrt(d * d + e * e + f * f); return k1 > 1e-9 ? k0 * (k0 - 1) / k1 : -Math.min(rx, ry, rz); };+// Rounded cone between two points: the workhorse of fingers, locks, lids and sleeves+function sdCone(x, y, z, ax, ay, az, bx, by, bz, r1, r2) {+ const px = x - ax, py = y - ay, pz = z - az, dx = bx - ax, dy = by - ay, dz = bz - az;+ const t = clamp((px * dx + py * dy + pz * dz) / (dx * dx + dy * dy + dz * dz));+ return len3(px - dx * t, py - dy * t, pz - dz * t) - (r1 + (r2 - r1) * t);+}+// Surface nets over a sparse grid: only cells near the surface are ever evaluated+function polygonize(F, box, cell, bandK = 1.75) {+ const [x0, y0, z0, x1, y1, z1] = box, nx = Math.ceil((x1 - x0) / cell) + 2, ny = Math.ceil((y1 - y0) / cell) + 2, nz = Math.ceil((z1 - z0) / cell) + 2;+ const N = nx * ny * nz, val = new Float32Array(N), seen = new Uint8Array(N), cellV = new Int32Array(N).fill(-1);+ const b4 = 3.46 * cell * bandK, b2 = 1.73 * cell * bandK, active = [], inA = new Uint8Array(N);+ const activate = (i, j, k) => { const q = i + nx * (j + ny * k); if (!inA[q]) { inA[q] = 1; active.push(i, j, k); } };+ const at = (i, j, k) => { const q = i + nx * (j + ny * k); if (!seen[q]) { seen[q] = 1; val[q] = F(x0 + i * cell, y0 + j * cell, z0 + k * cell); } return val[q]; };+ // Two-level descent: a coarse block, then its octants, then cells, so the field is only asked near its surface+ for (let kc = 0; kc < nz - 1; kc += 4) for (let jc = 0; jc < ny - 1; jc += 4) for (let ic = 0; ic < nx - 1; ic += 4) {+ if (Math.abs(F(x0 + (ic + 2) * cell, y0 + (jc + 2) * cell, z0 + (kc + 2) * cell)) >= b4) continue;+ for (let k2 = kc; k2 < Math.min(kc + 4, nz - 1); k2 += 2) for (let j2 = jc; j2 < Math.min(jc + 4, ny - 1); j2 += 2) for (let i2 = ic; i2 < Math.min(ic + 4, nx - 1); i2 += 2) {+ if (Math.abs(F(x0 + (i2 + 1) * cell, y0 + (j2 + 1) * cell, z0 + (k2 + 1) * cell)) >= b2) continue;+ for (let k = k2; k < Math.min(k2 + 2, nz - 1); k++) for (let j = j2; j < Math.min(j2 + 2, ny - 1); j++) for (let i = i2; i < Math.min(i2 + 2, nx - 1); i++) activate(i, j, k);+ }+ }+ const P = [], I = [], E = [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]], v = new Float64Array(8);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ let mask = 0;+ for (let c = 0; c < 8; c++) { const d = at(i + (c & 1), j + ((c >> 1) & 1), k + ((c >> 2) & 1)); v[c] = d; if (d < 0) mask |= 1 << c; }+ if (mask === 0 || mask === 255) continue;+ // Follow the surface through every face it crosses: a steep pleat can fool the coarse test above, and a skipped cell is a hole+ const crosses = (m) => (mask & m) !== 0 && (mask & m) !== m;+ if (i > 0 && crosses(0x55)) activate(i - 1, j, k);+ if (i < nx - 2 && crosses(0xaa)) activate(i + 1, j, k);+ if (j > 0 && crosses(0x33)) activate(i, j - 1, k);+ if (j < ny - 2 && crosses(0xcc)) activate(i, j + 1, k);+ if (k > 0 && crosses(0x0f)) activate(i, j, k - 1);+ if (k < nz - 2 && crosses(0xf0)) activate(i, j, k + 1);+ let sx = 0, sy = 0, sz = 0, n = 0;+ for (let e = 0; e < 12; e++) {+ const c0 = E[e][0], c1 = E[e][1], d0 = v[c0], d1 = v[c1];+ if ((d0 < 0) === (d1 < 0)) continue;+ const t = d0 / (d0 - d1);+ sx += lerp(c0 & 1, c1 & 1, t); sy += lerp((c0 >> 1) & 1, (c1 >> 1) & 1, t); sz += lerp((c0 >> 2) & 1, (c1 >> 2) & 1, t); n++;+ }+ cellV[i + nx * (j + ny * k)] = P.length / 3;+ P.push(x0 + (i + sx / n) * cell, y0 + (j + sy / n) * cell, z0 + (k + sz / n) * cell);+ }+ const quad = (a, b, c, d, flip) => { if (a < 0 || b < 0 || c < 0 || d < 0) return; if (flip) I.push(a, c, b, a, d, c); else I.push(a, b, c, a, c, d); };+ const cv = (i, j, k) => (i < 0 || j < 0 || k < 0 ? -1 : cellV[i + nx * (j + ny * k)]);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ const s0 = at(i, j, k) < 0;+ if (s0 !== (at(i + 1, j, k) < 0)) quad(cv(i, j - 1, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i, j - 1, k), !s0);+ if (s0 !== (at(i, j + 1, k) < 0)) quad(cv(i - 1, j, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i - 1, j, k), s0);+ if (s0 !== (at(i, j, k + 1) < 0)) quad(cv(i - 1, j - 1, k), cv(i, j - 1, k), cv(i, j, k), cv(i - 1, j, k), !s0);+ }+ return { P, I };+}+function meshToGeo(m) {+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(m.P), 3));+ g.setIndex(new THREE.BufferAttribute(m.P.length / 3 > 65535 ? new Uint32Array(m.I) : new Uint16Array(m.I), 1));+ g.computeVertexNormals();+ return g;+}+const AO_STEPS = [0.016, 0.05, 0.13];+// Extract, simplify, and write into a batch with field normals and field ambient occlusion+async function carveInto(b, F, box, cell, tris, xf, occ, opt = {}) {+ const raw = polygonize(F, box, cell);+ if (!raw.I.length) return;+ if (opt.cull) { // drop a region that a finer pass will carve instead+ const I2 = [], used = new Int32Array(raw.P.length / 3).fill(-1), P2 = [], mapV = (v) => { if (used[v] < 0) { used[v] = P2.length / 3; P2.push(raw.P[v * 3], raw.P[v * 3 + 1], raw.P[v * 3 + 2]); } return used[v]; };+ for (let t = 0; t < raw.I.length; t += 3) {+ const a = raw.I[t], c = raw.I[t + 1], e = raw.I[t + 2], mx = (raw.P[a * 3] + raw.P[c * 3] + raw.P[e * 3]) / 3, my = (raw.P[a * 3 + 1] + raw.P[c * 3 + 1] + raw.P[e * 3 + 1]) / 3, mz = (raw.P[a * 3 + 2] + raw.P[c * 3 + 2] + raw.P[e * 3 + 2]) / 3;+ if (!opt.cull(mx, my, mz)) I2.push(mapV(a), mapV(c), mapV(e));+ }+ raw.P = P2; raw.I = I2;+ }+ let g = meshToGeo(raw);+ if (raw.I.length / 3 > tris) g = await simplifyGeometry(g, { targetTriangles: tris });+ const pos = g.attributes.position, idx = g.index, base = b.count, h = cell * (opt.smooth || 0.6), aoK = opt.ao === undefined ? 0.5 : opt.ao, aoMin = opt.aoMin === undefined ? 0.16 : opt.aoMin;+ b.at(xf || M.I(), () => {+ for (let i = 0; i < pos.count; i++) {+ let x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i);+ const f0 = F(x, y, z), g0 = F(x + h, y, z) - F(x - h, y, z), g1 = F(x, y + h, z) - F(x, y - h, z), g2 = F(x, y, z + h) - F(x, y, z - h), l = len3(g0, g1, g2) || 1;+ const nx = g0 / l, ny = g1 / l, nz = g2 / l;+ // settle the vertex onto the true surface: this is what keeps fold valleys crisp+ x -= nx * f0; y -= ny * f0; z -= nz * f0;+ const O = occ || F;+ const wx = b.m[0] * x + b.m[1] * y + b.m[2] * z + b.m[3], wy = b.m[4] * x + b.m[5] * y + b.m[6] * z + b.m[7], wz = b.m[8] * x + b.m[9] * y + b.m[10] * z + b.m[11];+ const nm = b.nm, ax = nm[0] * nx + nm[1] * ny + nm[2] * nz, ay = nm[3] * nx + nm[4] * ny + nm[5] * nz, az = nm[6] * nx + nm[7] * ny + nm[8] * nz, al = len3(ax, ay, az) || 1;+ let o = 0, w = 1;+ for (let q = 0; q < 3; q++) { const d = AO_STEPS[q]; o += w * Math.max(0, d - O(wx + ax / al * d, wy + ay / al * d, wz + az / al * d)) / d; w *= 0.66; }+ const ao = clamp(1 - o * aoK, aoMin, 1);+ // UV by a slanted planar projection: no seam on a figure, the grain runs up the stone+ b.v(x, y, z, nx, ny, nz, wz * 0.8 + wx * 0.6, wy, ao);+ }+ for (let t = 0; t < idx.count; t += 3) b.tri(base + idx.getX(t), base + idx.getX(t + 1), base + idx.getX(t + 2));+ });+}++// ---- The head: a real skull, brow, eyes with lids, nose, lips, chin, hair, beard -------------+const LID_U = [[0.0795, 0.0035, 0.0165, 0.003], [0.0848, 0.0112, 0.0238, 0.0038], [0.0866, 0.0152, 0.0338, 0.0043], [0.0838, 0.0122, 0.0438, 0.0036], [0.0755, 0.0062, 0.052, 0.0024]];+const LID_L = [[0.0795, 0.0035, 0.0165, 0.0022], [0.0836, -0.0012, 0.0245, 0.0028], [0.0852, -0.0032, 0.034, 0.003], [0.0822, -0.0006, 0.044, 0.0026], [0.0755, 0.0062, 0.052, 0.0022]];+function headField(o) {+ const sm = o.smile || 0, cheek = o.gaunt ? 0.5 : 1, up = sm * 0.0065;+ const locks = [], rng = mkRng(o.seed * 37 + 11);+ // Long locks fall from the temples and the back of the head to the shoulders+ if (o.hair === 'long' || o.hair === 'curled') {+ const nL = o.hair === 'curled' ? 7 : 9;+ for (let i = 0; i < nL; i++) {+ const f = i / (nL - 1), a = lerp(1.05, 2.95, f), r0 = 0.092, wob = 0.012 + rng() * 0.008, dr = o.hair === 'curled' ? -0.165 : -0.235;+ for (const s of [-1, 1]) {+ const p0 = [r0 * Math.cos(a) - 0.012, 0.02 + rng() * 0.02, s * r0 * Math.sin(a) * 0.9];+ const p1 = [p0[0] * 1.14 - 0.008, p0[1] + dr * 0.5, p0[2] * 1.18 + s * wob];+ const p2 = [p0[0] * 1.02 - 0.03 * (1 - f * 0.5), p0[1] + dr, p0[2] * (1.28 - f * 0.3) - s * wob];+ locks.push([p0, p1, 0.02, 0.016], [p1, p2, 0.016, o.hair === 'curled' ? 0.012 : 0.007]);+ }+ }+ }+ if (o.hair === 'curls') { // rolled waves that frame the face: the smiling angel, the Child+ for (let i = 0; i < 11; i++) { const f = i / 10 - 0.5, a = f * 2.5, p0 = [0.104 * Math.cos(a) * 0.72 + 0.006, 0.094 - f * f * 0.11, 0.088 * Math.sin(a)], p1 = [p0[0] + 0.012, p0[1] - 0.03 - Math.abs(f) * 0.03, p0[2] * 1.16]; locks.push([p0, p1, 0.0165, 0.0115]); }+ for (let i = 0; i < 12; i++) { const a = 1.5 + i * 0.29, s = i % 2 ? 1 : -1, p0 = [0.09 * Math.cos(a) - 0.012, 0.0, s * 0.09 * Math.sin(a)], p1 = [p0[0] * 1.1, -0.062, p0[2] * 1.14]; locks.push([p0, p1, 0.019, 0.013]); }+ }+ const beardL = [];+ if (o.beard) {+ const nB = 7, L = o.beard === 2 ? 0.17 : 0.09;+ for (let i = 0; i < nB; i++) {+ const f = i / (nB - 1) - 0.5, z0 = f * 0.098, x0 = 0.078 - Math.abs(f) * 0.05, wob = (i % 2 ? 1 : -1) * 0.005;+ // separate locks with grooves between them; each one twists and tapers to a point+ beardL.push([[x0, -0.07 - Math.abs(f) * 0.012, z0], [x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], 0.0102, 0.0098]);+ beardL.push([[x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], [x0 - 0.006 + (o.beard === 2 ? 0.008 : 0), -0.07 - L - Math.abs(f) * 0.012, z0 * 0.5], 0.0098, 0.0035]);+ }+ }+ // Each lock is skipped outright when the query point is beyond its bounding sphere+ const sphere = (L) => { const dx = L[0][0] - L[1][0], dy = L[0][1] - L[1][1], dz = L[0][2] - L[1][2], R = Math.sqrt(dx * dx + dy * dy + dz * dz) / 2 + Math.max(L[2], L[3]) + 0.03; return [(L[0][0] + L[1][0]) / 2, (L[0][1] + L[1][1]) / 2, (L[0][2] + L[1][2]) / 2, R * R]; };+ const lockB = locks.map(sphere), beardB = beardL.map(sphere);+ const nearS = (S, x, y, z) => { const a = x - S[0], b = y - S[1], c = z - S[2]; return a * a + b * b + c * c < S[3]; };+ return (x, y, z) => {+ const az = Math.abs(z);+ let d = sdEll(x + 0.012, y - 0.03, z, 0.098, 0.098, 0.08); // cranium+ d = smin(d, sdEll(x - 0.018, y + 0.022, z, 0.074, 0.094, 0.0655), 0.02); // face mass+ d = smin(d, sdEll(x - 0.03, y + 0.076, z, 0.056, 0.046, 0.05 * (o.gaunt ? 0.93 : 1)), 0.018); // jaw+ d = smin(d, sdEll(x - 0.0675, y + 0.1, z, 0.019, 0.0175, 0.02), 0.012); // chin+ d = smin(d, sdCone(x, y, az, 0.0745, 0.031, 0, 0.066, 0.033, 0.05, 0.0118, 0.0088), 0.012); // brow ridge+ d = smin(d, sdEll(x - 0.054, y + 0.018 - up, az - 0.044, 0.026, 0.017 + sm * 0.003, 0.02), 0.015 * cheek); // cheekbone+ if (o.gaunt) d = smax(d, -sdEll(x - 0.05, y + 0.058, az - 0.058, 0.03, 0.022, 0.012), 0.012);+ if (x > 0.04 && y > -0.03 && y < 0.05 && az < 0.082) {+ d = smax(d, -sdEll(x - 0.0875, y - 0.0075, az - 0.034, 0.0225, 0.0118, 0.0215), 0.008); // eye socket+ d = smin(d, len3(x - 0.0655, y - 0.0065, az - 0.034) - 0.0178, 0.0018); // eyeball+ d = smax(d, -(len3(x - 0.0862, y - 0.0065, az - 0.034) - 0.0052), 0.0016); // the drilled pupil+ // lids: a carved almond, the upper rim heavier, meeting at the corners+ for (let i = 0; i < 4; i++) { const a = LID_U[i], c = LID_U[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0026); }+ for (let i = 0; i < 4; i++) { const a = LID_L[i], c = LID_L[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0022); }+ }+ if (x > 0.055 && y > -0.105 && y < 0.045 && az < 0.055) {+ // nose: bridge to tip, wings, nostrils+ d = smin(d, sdCone(x, y, z, 0.0835, 0.0225, 0, 0.105, -0.021, 0, 0.0058, 0.0092), 0.0085);+ d = smin(d, len3(x - 0.0925, y + 0.0295, az - 0.0108) - 0.0064, 0.005);+ d = smax(d, -(len3(x - 0.0975, y + 0.0355, az - 0.0062) - 0.0033), 0.002);+ // philtrum+ d = smax(d, -sdCone(x, y, z, 0.0975, -0.042, 0, 0.0965, -0.052, 0, 0.0028, 0.0034), 0.0035);+ // lips: upper in two wings, a fuller lower, a cut between; corners lift for a smile+ const my = -0.0615;+ d = smin(d, sdCone(x, y, az, 0.0925, my + 0.004, 0, 0.0815, my + 0.001 + up, 0.0215 + sm * 0.003, 0.0058, 0.0026), 0.005);+ d = smin(d, sdCone(x, y, az, 0.0915, my - 0.0085, 0, 0.0825, my - 0.003 + up, 0.0185 + sm * 0.003, 0.0072, 0.0028), 0.005);+ d = smax(d, -sdCone(x, y, az, 0.0965, my - 0.002, 0, 0.0825, my + up * 1.15, 0.0235 + sm * 0.004, 0.0017, 0.0021), 0.0022);+ d = smax(d, -sdEll(x - 0.0915, y + 0.0845, z, 0.012, 0.0055, 0.02), 0.006); // hollow under the lip+ }+ if (sm) d = smin(d, sdEll(x - 0.07, y + 0.044 - up * 2, az - 0.038, 0.02, 0.015, 0.016), 0.012); // the raised cheek of a smile+ // ear+ d = smin(d, sdEll(x + 0.012, y - 0.002, az - 0.08, 0.016, 0.03, 0.0085), 0.004);+ // neck, into the shoulders+ d = smin(d, sdCone(x, y, z, -0.01, -0.075, 0, -0.022, -0.215, 0, 0.041, 0.052), 0.018);+ // ---- hair ----+ if (o.hair && o.hair !== 'none') {+ const faceOpen = sdEll(x - 0.094, y + 0.012, z, 0.08, o.hair === 'curls' ? 0.106 : 0.118, 0.073);+ let h = sdEll(x + 0.014, y - 0.034, z, 0.104, 0.104, 0.09);+ if (o.hair === 'tonsure') h = smax(h, -sdEll(x + 0.012, y - 0.135, z, 0.075, 0.045, 0.062), 0.006);+ const an = Math.atan2(z, x + 0.014);+ if (o.hair !== 'curls') {+ const part = Math.exp(-(z * z) / 0.00007) * sstep(0.03, 0.11, y);+ h += 0.0028 * Math.sin(an * 22 + y * 34) + 0.007 * part;+ } else {+ h += 0.006 * Math.sin(an * 11 + y * 22) * Math.sin(y * 27 + an * 3);+ }+ for (let i = 0; i < locks.length; i++) { if (!nearS(lockB[i], x, y, z)) continue; const L = locks[i]; h = smin(h, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0055); }+ h = smax(h, -faceOpen, 0.0055);+ d = smin(d, h, 0.0025);+ }+ // ---- beard and moustache ----+ if (o.beard) {+ for (let i = 0; i < beardL.length; i++) { if (!nearS(beardB[i], x, y, z)) continue; const L = beardL[i]; d = smin(d, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0022); }+ d = smin(d, sdCone(x, y, az, 0.0955, -0.0475, 0.002, 0.081, -0.0625, 0.031, 0.0046, 0.003), 0.0025);+ d = smin(d, sdEll(x - 0.05, y + 0.058, az - 0.058, 0.022, 0.03, 0.012), 0.008); // sideburn into the jaw+ }+ // ---- veil: falls from the crown to the shoulders, cut open round the face ----+ if (o.veil) {+ // A soft hood: close to the skull, widening over the shoulders, its hem falling in loose folds+ const an = Math.atan2(z, x + 0.02);+ let w = smin(sdEll(x + 0.012, y - 0.032, z, 0.11, 0.108, 0.094), sdCone(x, y, z, -0.022, -0.05, 0, -0.038, -0.3, 0, 0.09, 0.158), 0.05);+ w -= 0.012 * (Math.sqrt(Math.abs(Math.sin(an * 4.5 + y * 2.2))) - 0.55) * sstep(-0.01, -0.16, y);+ w = smax(w, -0.325 + 0.022 * Math.sin(an * 5) - y, 0.008);+ const open = sdEll(x - 0.104, y + 0.016, z, 0.1, 0.116, 0.0715);+ d = smin(d, smax(w, -open, 0.004), 0.0015);+ }+ // ---- a queen's barbette: a linen band from the crown under the chin, framing a long grave face ----+ if (o.wimple) {+ const e = Math.sqrt(((y + 0.012) * (y + 0.012)) / (0.112 * 0.112) + (z * z) / (0.08 * 0.08)) - 1;+ d = smin(d, Math.max(Math.abs(e) * 0.09 - 0.0075, Math.abs(x - 0.018) - 0.036), 0.002);+ }+ // ---- mitre: two high peaks of stiff cloth, the bands left proud ----+ if (o.mitre) {+ // seen from the front a pointed arch, wide at the brow; seen from the side, two thin peaks+ const px = x + 0.012, py = y - 0.058, f = clamp(py / 0.27), w0 = 0.098 * (1 - Math.pow(f, 1.9)), dp = 0.102 * (1 - f * 0.86), lens = Math.abs(px) + az * 0.2;+ let m = Math.max(lens - dp, az - w0, -py);+ m = Math.max(m, py - 0.272);+ const band = Math.exp(-((py - 0.018) * (py - 0.018)) / 0.00016);+ m -= 0.0045 * band + 0.0038 * Math.exp(-(z * z) / 0.00012);+ d = smin(d, m, 0.004);+ }+ return d * 0.92;+ };+}++// ---- The hand: palm and jointed fingers that actually close on what they hold -------------+const HAND_POSES = {+ // [curl1, curl2, curl3] per finger (index..little), thumb [x,y,z of tip, spread]+ grip: { f: [[68, 88, 62], [72, 92, 64], [76, 92, 64], [80, 88, 62]], t: 'over' },+ support: { f: [[10, 18, 12], [12, 20, 14], [14, 22, 14], [18, 24, 16]], t: 'open' },+ edge: { f: [[46, 40, 26], [48, 42, 26], [50, 44, 28], [54, 44, 28]], t: 'open' },+ bless: { f: [[6, 8, 6], [8, 10, 8], [62, 90, 60], [68, 92, 60]], t: 'over' },+ rest: { f: [[28, 40, 26], [32, 42, 28], [36, 44, 28], [40, 46, 28]], t: 'open' },+};+function handField(pose, thumb = 1) {+ const P0 = HAND_POSES[pose], kn = [[0.078, 0.003, -0.027], [0.083, 0.004, -0.009], [0.08, 0.003, 0.009], [0.073, 0.001, 0.026]], segL = [[0.036, 0.024, 0.019], [0.04, 0.027, 0.02], [0.037, 0.025, 0.019], [0.029, 0.019, 0.016]], segs = [];+ for (let f = 0; f < 4; f++) {+ let ang = 0, p = kn[f].slice();+ for (let s = 0; s < 3; s++) {+ ang += rad(P0.f[f][s]);+ const q = [p[0] + Math.cos(ang) * segL[f][s], p[1] - Math.sin(ang) * segL[f][s], p[2]];+ segs.push([p, q, s === 0 ? 0.0098 : s === 1 ? 0.0088 : 0.0078, s === 2 ? 0.0066 : s === 0 ? 0.0088 : 0.0078]);+ p = q;+ }+ }+ const tb = [0.022, -0.006, thumb * 0.03], tm = P0.t === 'over' ? [0.052, -0.03, thumb * 0.033] : [0.05, -0.012, thumb * 0.054], tt = P0.t === 'over' ? [0.078, -0.044, thumb * 0.012] : [0.082, -0.016, thumb * 0.056];+ return (x, y, z) => {+ let d = Math.max(Math.abs(x - 0.04) - 0.043, Math.abs(y) - 0.0125, Math.abs(z) - 0.036) - 0.006; // palm, gently rounded+ d = smin(d, sdCone(x, y, z, -0.02, 0, 0, 0.02, 0, 0, 0.026, 0.033), 0.012); // wrist heel+ for (let i = 0; i < segs.length; i++) { const S = segs[i]; d = smin(d, sdCone(x, y, z, S[0][0], S[0][1], S[0][2], S[1][0], S[1][1], S[1][2], S[2], S[3]), 0.0022); }+ d = smin(d, sdCone(x, y, z, tb[0], tb[1], tb[2], tm[0], tm[1], tm[2], 0.0145, 0.0098), 0.006);+ d = smin(d, sdCone(x, y, z, tm[0], tm[1], tm[2], tt[0], tt[1], tt[2], 0.0098, 0.0078), 0.002);+ return d;+ };+}+// Heads are shared meshes placed on their own node: the same face can turn and tilt per figure at no byte cost.+const HEAD_MESH = {};+const HEADS = {+ bearded: { hair: 'long', beard: 1, seed: 2 },+ mitred: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 },+ royal: { hair: 'curled', beard: 2, seed: 10 },+ veiled: { veil: 1, smile: 0.3, seed: 8 },+ queen: { veil: 1, gaunt: 1, wimple: 1, seed: 5 },+ angelic: { hair: 'curls', smile: 1, seed: 13 },+};+async function headBatch(key) {+ if (HEAD_MESH[key]) return HEAD_MESH[key];+ const hO = HEADS[key], b = new Batch(TILE.fine), hF = headField(hO);+ const hB = [-0.19, hO.veil ? -0.36 : -0.26, -0.2, 0.17, hO.mitre ? 0.36 : 0.16, 0.2];+ // Hair and skull at a working grid, then the face alone on the finest grid with most of the triangles+ const FB = [0.024, -0.13, -0.08, 0.128, 0.088, 0.08];+ const inFace = (x, y, z) => x > FB[0] + 0.003 && y > FB[1] + 0.003 && y < FB[4] - 0.003 && z > FB[2] + 0.003 && z < FB[5] - 0.003;+ await carveInto(b, hF, hB, 0.0048, hO.veil ? 1300 : hO.beard ? 1900 : 1700, null, hF, { smooth: 1.4, ao: 0.34, aoMin: 0.3, cull: inFace });+ await carveInto(b, hF, FB, 0.0024, hO.beard ? 2500 : 2700, null, hF, { smooth: 0.9, ao: 0.3, aoMin: 0.34 });+ b.isFigure = true;+ HEAD_MESH[key] = b;+ return b;+}+const HAND_MESH = {};+async function handBatch(pose, thumb) { // hands are cached: a grip is a grip, whoever's it is+ const key = pose + thumb;+ if (!HAND_MESH[key]) { const b = new Batch(TILE.fine), F = handField(pose, thumb); await carveInto(b, F, [-0.05, -0.1, -0.1, 0.16, 0.05, 0.1], 0.0034, 520); HAND_MESH[key] = b; }+ return HAND_MESH[key];+}+function copyBatch(dst, src, m, shadeFn) { // transform a finished batch into another+ const base = dst.count;+ dst.at(m, () => {+ for (let i = 0; i < src.count; i++) { const i3 = i * 3, i2 = i * 2; dst.v(src.P[i3], src.P[i3 + 1], src.P[i3 + 2], src.N[i3], src.N[i3 + 1], src.N[i3 + 2], src.UV[i2] * src.tile, src.UV[i2 + 1] * src.tile, (src.C[i3] / 255) * (shadeFn ? shadeFn(src.P[i3], src.P[i3 + 1], src.P[i3 + 2]) : 1)); }+ for (let t = 0; t < src.I.length; t += 3) dst.tri(base + src.I[t], base + src.I[t + 1], base + src.I[t + 2]);+ });+}+const frameOf = (p, X, Y) => { // rigid frame: origin p, local X and Y given, Z = X x Y+ const lx = len3(X[0], X[1], X[2]), xn = [X[0] / lx, X[1] / lx, X[2] / lx], d = Y[0] * xn[0] + Y[1] * xn[1] + Y[2] * xn[2];+ let yy = [Y[0] - d * xn[0], Y[1] - d * xn[1], Y[2] - d * xn[2]]; const ly = len3(yy[0], yy[1], yy[2]); yy = yy.map((c) => c / ly);+ const z = [xn[1] * yy[2] - xn[2] * yy[1], xn[2] * yy[0] - xn[0] * yy[2], xn[0] * yy[1] - xn[1] * yy[0]];+ return [xn[0], yy[0], z[0], p[0], xn[1], yy[1], z[1], p[1], xn[2], yy[2], z[2], p[2]];+};+const invRigid = (m) => (x, y, z) => { const a = x - m[3], b = y - m[7], c = z - m[11]; return [m[0] * a + m[4] * b + m[8] * c, m[1] * a + m[5] * b + m[9] * c, m[2] * a + m[6] * b + m[10] * c]; };++// ---- The people. Bodies are long columns of cloth; each differs in dress, stance, head, gesture ----+const FIGURES = {+ apostle: { seed: 2, sway: 0.018, dress: 'mantle', headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 2 }, hold: 'key' },+ bishop: { seed: 4, sway: 0.008, dress: 'chasuble', headKey: 'mitred', head: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 }, hold: 'crozier' },+ king: { seed: 10, sway: 0.022, dress: 'royal', headKey: 'royal', head: { hair: 'curled', beard: 2, crown: 1, seed: 10 }, hold: 'sceptre' },+ queen: { seed: 5, sway: 0.03, dress: 'royal', slim: 0.93, headKey: 'queen', head: { veil: 1, gaunt: 1, wimple: 1, crown: 1, seed: 5, turn: 8 }, hold: 'mantle' },+ angel: { seed: 13, sway: 0.05, dress: 'angel', slim: 0.94, wings: 1, headKey: 'angelic', head: { hair: 'curls', smile: 1, seed: 13, tilt: 11, turn: 15 }, hold: 'scroll' },+ virgin: { seed: 8, sway: 0.062, dress: 'mantle', slim: 0.95, headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, tilt: 9, turn: -16 }, hold: 'child' },+};+const FIG_ALIAS = { peter: 'apostle', paul: 'apostle', prophet: 'apostle', deacon: 'apostle', martyr: 'queen', kingA: 'king', kingB: 'king', kingC: 'king', christ: 'seated' };++function bodyField(o) {+ const yN = 1.7, rng = mkRng(o.seed * 991 + 7), ph = []; for (let i = 0; i < 8; i++) ph.push(rng() * TAU);+ const sw = o.sway || 0, sl = o.slim || 1, L = 256;+ const keys = [[0, 0.178, 0.205, 0.218], [0.05, 0.165, 0.192, 0.204], [0.3, 0.138, 0.165, 0.172], [0.52, 0.124, 0.15, 0.158], [0.66, 0.112, 0.128, 0.146], [0.8, 0.116, 0.12, 0.15], [0.885, 0.108, 0.112, 0.15], [0.94, 0.09, 0.094, 0.13], [0.975, 0.066, 0.07, 0.09], [1, 0.044, 0.046, 0.05]];+ const Af = new Float32Array(L), Ab = new Float32Array(L), Bs = new Float32Array(L), Cx = new Float32Array(L), Cz = new Float32Array(L), Amp = new Float32Array(L);+ for (let i = 0; i < L; i++) {+ const t = i / (L - 1), k = keyf(keys, t);+ Af[i] = k[0]; Ab[i] = k[1]; Bs[i] = k[2] * sl;+ Cx[i] = 0.014 * Math.sin(t * PI);+ Cz[i] = sw * (Math.sin(t * PI * 0.92) * 1.15 - 0.62 * Math.sin(t * PI * 2));+ // pleats deepest over the shins, gathered at the belt, slight in the bodice+ Amp[i] = t < 0.645 ? lerp(0.05, 0.03, sstep(0, 0.6, t)) : lerp(0.014, 0.004, sstep(0.66, 0.9, t));+ }+ const n1 = o.dress === 'chasuble' ? 16 : 14, n2 = 25, yB = 0.655 * yN;+ const dress = o.dress, zc = Cz[Math.round(0.86 * (L - 1))], cxS = Cx[Math.round(0.86 * (L - 1))];+ const Rs = 0.142 * sl;+ const arms = armPose(o, Rs, zc, cxS);+ // Feet: pointed medieval shoes emerge from the hem+ const foot = (x, y, z) => { const az = Math.abs(z - Cz[0]); let d = sdCone(x, y, az, 0.02, 0.042, 0.082, 0.2, 0.024, 0.098, 0.05, 0.024); d = smin(d, sdCone(x, y, az, 0.0, 0.05, 0.082, 0.02, 0.2, 0.07, 0.044, 0.04), 0.02); d = smax(d, -sdEll(x - 0.11, y + 0.01, az - 0.09, 0.08, 0.02, 0.1), 0.004); return Math.max(d, -y); };+ return {+ arms,+ F: (x, y, z) => {+ const t = clamp(y / yN), i = (t * (L - 1)) | 0, px = x - Cx[i], pz = z - Cz[i], rho = Math.sqrt(px * px + pz * pz) + 1e-6, c = px / rho, s = pz / rho, a = c > 0 ? Af[i] : Ab[i], b = Bs[i];+ const R = 1 / Math.sqrt((c * c) / (a * a) + (s * s) / (b * b)), th = Math.atan2(pz, px);+ // ---- the tunic: tube pleats with valleys you could put a finger in ----+ const u1 = th * n1 * 0.5 + ph[0] + y * 0.5 * Math.sign(sw || 1), u2 = th * n2 * 0.5 + ph[1] - y * 0.9;+ const p1 = Math.sqrt(Math.abs(Math.sin(u1))), p2 = Math.sqrt(Math.abs(Math.sin(u2)));+ let r = R + Amp[i] * (p1 * 0.76 + p2 * 0.24 - 0.64);+ r += sstep(0.14, 0, y) * (0.018 + 0.016 * Math.sin(th * 7 + ph[4])); // the skirt spreads and breaks at the foot+ r += 0.008 * Math.exp(-((y - yB) * (y - yB)) / 0.00018); // belt+ r -= 0.012 * sstep(0.03, 0, Math.abs(y - yB) - 0.012) * 0.2;+ r += 0.006 * Math.exp(-((y - yN + 0.075) * (y - yN + 0.075)) / 0.00012); // collar+ let d = rho - r;+ const hem = 0.055 + 0.038 * p1 * (c > -0.2 ? 1 : 0.4);+ d = smax(d, hem - y, 0.006);+ // ---- the over-garment ----+ if (dress === 'mantle') {+ const up = 0.78 * yN + 0.2 * yN * Math.max(0, Math.sin(-th * Math.sign(sw || 1) + 0.5)) + 0.25 * sstep(1.2, 2.6, Math.abs(th)), low = 0.33 * yN - 0.18 * yN * sstep(0.5, 2.8, Math.abs(th)) + 0.02 * Math.sin(th * 11 + ph[5]) + 0.012 * Math.sin(th * 23);+ const reg = Math.max(low - y, y - up);+ // Folds pulled on the diagonal from the held hip to the far shoulder: two scales, never in step+ const th0 = 0.25 * Math.sign(sw || 1), q = y + 0.15 * th * Math.sign(sw || 1) + 0.055 * (th - th0) * (th - th0);+ const swag = 0.62 * Math.sqrt(Math.abs(Math.sin(q * 12.5 + ph[6]))) + 0.38 * Math.sqrt(Math.abs(Math.sin(q * 27 + ph[7]))), back = Math.sqrt(Math.abs(Math.sin(th * 5 + ph[3])));+ const fr = sstep(1.9, 1.1, Math.abs(th));+ const rm = R + 0.03 + 0.04 * lerp(back - 0.6, swag - 0.58, fr) * sstep(up + 0.01, up - 0.08, y);+ d = smin(d, smax(rho - rm, reg, 0.007), 0.002);+ } else if (dress === 'chasuble') {+ // alb to the feet, dalmatic to the shin, the bell of the chasuble hanging from the arms in U folds+ const dal = Math.max(0.3 * yN + 0.012 * Math.sin(th * 9) - y, y - 0.86 * yN);+ d = smin(d, smax(rho - (R + 0.016 + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 6 + ph[2]))) - 0.5)), dal, 0.005), 0.0015);+ const lowC = (0.42 + 0.26 * sstep(0.2, 1.5, Math.abs(th)) - 0.2 * sstep(1.7, 3.0, Math.abs(th))) * yN + 0.012 * Math.sin(th * 13);+ const swag = 0.65 * Math.sqrt(Math.abs(Math.sin((y + 0.2 * th * th) * 27 + ph[6]))) + 0.35 * Math.sqrt(Math.abs(Math.sin((y + 0.13 * th * th) * 44 + ph[7])));+ const rc = R + 0.03 * sstep(yN, 0.9 * yN, y) + 0.022 * (swag - 0.58) * sstep(0.84 * yN, 0.74 * yN, y) * (1 - 0.6 * sstep(0, 0.2, 0.2 - Math.abs(th)));+ d = smin(d, smax(rho - rc, lowC - y, 0.007), 0.003);+ // the orphrey: a Y-band across the breast and down the front, left proud+ const py = y - 0.79 * yN;+ if (Math.abs(th) < 1.2 && y > lowC - 0.01 && y < 0.94 * yN) d = smin(d, Math.max(rho - rc - 0.011, py > 0 ? Math.abs(Math.abs(th) - py * 4.2) - 0.085 : Math.abs(th) - 0.09), 0.003);+ } else if (dress === 'royal') {+ // a heavy mantle open down the front, thrown back over the shoulders, deep folds to the floor+ const open = lerp(0.72, 0.28, sstep(0.05, 0.86, t)) + 0.05 * Math.sin(y * 19 + ph[2]);+ const reg = Math.max(open - Math.abs(th - 0.05), 0.05 - y);+ const hv = Math.sqrt(Math.abs(Math.sin(th * 4 + ph[3] + y * 0.3)));+ const collar = 0.022 * Math.exp(-((y - 0.915 * yN) * (y - 0.915 * yN)) / 0.0012); // a soft roll at the neck+ const rm = R + 0.03 * sstep(yN, 0.9 * yN, y) + collar + sstep(0.84 * yN, 0.5 * yN, y) * 0.056 * (hv - 0.62);+ d = smin(d, smax(rho - rm, reg, 0.006), 0.004);+ } else if (dress === 'angel') {+ // a girdled tunic blown a little to one side: fine folds and a bloused top+ const blouse = 0.018 * Math.exp(-((y - yB - 0.06) * (y - yB - 0.06)) / 0.003);+ d -= blouse;+ const low = 0.74 * yN + 0.02 * Math.sin(th * 7) - y;+ d = smin(d, smax(rho - (R + 0.016 * sstep(yN, 0.9 * yN, y) + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 7 + ph[2]))) - 0.5) * sstep(0.9 * yN, 0.8 * yN, y)), low, 0.005), 0.003);+ }+ // ---- shoulders and sleeves ----+ const yS = yN - 0.155;+ for (let k = 0; k < 2; k++) { const sg = k ? 1 : -1; d = smin(d, sdEll(x - cxS, y - yS, z - zc - sg * Rs * 0.98, 0.07, 0.078, 0.058), 0.05); }+ for (let k = 0; k < arms.length; k++) {+ const A = arms[k];+ let ad = sdCone(x, y, z, A.S[0], A.S[1], A.S[2], A.E[0], A.E[1], A.E[2], 0.056, 0.05);+ // forearm in a sleeve that widens to the cuff, with a roll of cloth at the wrist+ const fx = A.W[0] - A.E[0], fy = A.W[1] - A.E[1], fz = A.W[2] - A.E[2], fl = fx * fx + fy * fy + fz * fz, ft = clamp(((x - A.E[0]) * fx + (y - A.E[1]) * fy + (z - A.E[2]) * fz) / fl);+ const qx = x - A.E[0] - fx * ft, qy = y - A.E[1] - fy * ft, qz = z - A.E[2] - fz * ft, ql = len3(qx, qy, qz);+ const rr = lerp(0.05, 0.062, ft) + 0.006 * Math.sin(ft * 15 + k) * (1 - ft) + 0.008 * Math.exp(-((ft - 0.97) * (ft - 0.97)) / 0.002);+ ad = smin(ad, ql - rr, 0.018);+ // the cloth that falls from under the forearm: a hanging curtain of pleats+ // longest under the wrist, thinning as it falls, its lower edge never straight+ if (A.drape) {+ const h = A.E[1] + fy * ft - y;+ if (h > -0.04 && h < A.drape * 1.1) {+ const fall = clamp(h / A.drape), crv = lerp(0.044, 0.016, fall) + 0.014 * (Math.sqrt(Math.abs(Math.sin(ft * 12 + h * 2.5 + k))) - 0.5);+ const curtain = len3(qx * 0.72, 0, qz) - crv;+ const edge = h - A.drape * (0.28 + 0.72 * ft) - 0.022 * Math.sin(ft * 11 + k * 2);+ ad = smin(ad, smax(curtain, edge, 0.012), 0.018);+ }+ }+ d = smin(d, ad, 0.014);+ }+ // ---- feet ----+ d = smin(d, foot(x, y, z), 0.006);+ return Math.max(d, y - yN - 0.03);+ },+ };+}+// Where the arms go, and what the hands do when they get there+function armPose(o, Rs, zc, cx) {+ const y = 1.55, shR = [cx, y, zc + Rs], shL = [cx, y, zc - Rs], arm = (S, E, W, drape) => ({ S, E, W, drape });+ switch (o.hold) {+ case 'key': return [arm(shR, [0.03, 1.2, zc + Rs + 0.05], [0.17, 1.32, zc + 0.12], 0.32), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.17, 1.14, zc - 0.07], 0.34)];+ case 'crozier': return [arm(shR, [0.04, 1.2, zc + Rs + 0.06], [0.2, 1.3, zc + 0.14], 0.3), arm(shL, [0.05, 1.22, zc - Rs - 0.03], [0.2, 1.42, zc - 0.05], 0.3)];+ case 'sceptre': return [arm(shR, [0.04, 1.18, zc + Rs + 0.06], [0.2, 1.28, zc + 0.15], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.03], [0.17, 1.18, zc - 0.05], 0.3)];+ case 'mantle': return [arm(shR, [0.04, 1.14, zc + Rs + 0.05], [0.18, 1.2, zc + 0.04], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.15, 1.4, zc - 0.1], 0.26)];+ case 'scroll': return [arm(shR, [0.05, 1.2, zc + Rs + 0.04], [0.2, 1.34, zc + 0.1], 0.28), arm(shL, [0.05, 1.2, zc - Rs - 0.04], [0.2, 1.22, zc - 0.08], 0.28)];+ case 'child': return [arm(shR, [0.05, 1.14, zc + Rs + 0.04], [0.2, 1.24, zc + 0.03], 0.32), arm(shL, [0.05, 1.14, zc - Rs - 0.02], [0.2, 1.18, zc - 0.13], 0.3)];+ case 'bless': return [arm(shR, [0.07, 1.22, zc + Rs + 0.09], [0.19, 1.5, zc + 0.15], 0.3), arm(shL, [0.03, 1.2, zc - Rs - 0.04], [0.17, 1.2, zc - 0.07], 0.34)];+ default: return [arm(shR, [0.02, 1.12, zc + Rs + 0.04], [0.15, 1.05, zc + 0.08], 0.3), arm(shL, [0.02, 1.12, zc - Rs - 0.04], [0.15, 1.05, zc - 0.08], 0.3)];+ }+}+// ---- Things held: real objects, modelled at the scale of the hands that grip them ---------+function addBook(b, m, o = {}) {+ b.at(m, () => {+ const w = o.w || 0.082, h = o.h || 0.112, th = o.th || 0.027;+ b.box(-h, h, -th, -th + 0.009, -w, w, { s: 0.85 }); b.box(-h, h, th - 0.009, th, -w, w, { s: 1 }); // boards+ b.box(-h + 0.006, h - 0.006, -th + 0.009, th - 0.009, -w + 0.006, w - 0.004, { s: 0.62 }); // pages+ b.box(-h, h, -th, th, -w - 0.009, -w, { s: 0.8 }); // spine+ for (const i of [-1, 1]) for (const k of [-1, 1]) b.at(M.T(i * (h - 0.026), th, k * (w - 0.026)), () => b.lathe([[0.016, 0], [0.012, 0.007], [0.0, 0.009]], 6));+ b.at(M.T(0, th, 0), () => b.lathe([[0.03, 0], [0.024, 0.006], [0.0, 0.009]], 8));+ });+}+function addAttrs(fig, o, arms) {+ const { stone: b, gold: g } = fig, R = arms[0], Lf = arms[1];+ switch (o.hold) {+ case 'key': {+ // The great key of Peter held upright in the fist, the book against the left forearm+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01];+ b.tube([[hp[0], hp[1] - 0.15, hp[2]], [hp[0], hp[1] + 0.2, hp[2]]], 0.0125, 8, { capStart: true });+ b.at(M.T(hp[0], hp[1] + 0.245, hp[2]), () => b.at(M.Rx(90), () => { b.lathe([[0.04, -0.01], [0.05, 0], [0.04, 0.01], [0.026, 0.01], [0.02, 0], [0.026, -0.01], [0.04, -0.01]], 14); }));+ b.box(hp[0] - 0.01, hp[0] + 0.01, hp[1] - 0.15, hp[1] - 0.075, hp[2], hp[2] + 0.07, { s: 0.9 });+ b.box(hp[0] - 0.012, hp[0] + 0.012, hp[1] - 0.112, hp[1] - 0.1, hp[2] + 0.03, hp[2] + 0.048, { s: 0.3 });+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2] + 0.0), M.Rz(-64), M.Ry(8)));+ break;+ }+ case 'crozier': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 2.02;+ b.tube([[hp[0], 0.04, hp[2]], [hp[0], top, hp[2]]], 0.0145, 8, { capStart: true });+ for (const yK of [hp[1] + 0.22, top - 0.05]) b.at(M.T(hp[0], yK, hp[2]), () => b.lathe([[0.016, -0.03], [0.034, 0], [0.016, 0.03]], 8));+ const vol = []; for (let i = 0; i <= 20; i++) { const t = i / 20, a = -PI * 0.5 + t * PI * 2.5, r = lerp(0.115, 0.036, t); vol.push([hp[0] + r * Math.cos(a) * 1.0 + 0.0, top + 0.12 + r * Math.sin(a), hp[2]]); }+ g.tube(vol, (t) => lerp(0.02, 0.012, t), 7, { capStart: true });+ for (let i = 2; i < 16; i += 3) { const p = vol[i]; addCrocket(g, M.of(M.T(p[0], p[1], p[2]), M.Rz((i / 20) * 400 - 90)), 0.06, 0); }+ break;+ }+ case 'sceptre': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 1.95;+ b.tube([[hp[0], hp[1] - 0.2, hp[2]], [hp[0], top, hp[2]]], 0.0125, 8, { capStart: true });+ g.at(M.T(hp[0], top, hp[2]), () => {+ g.lathe([[0.0125, 0], [0.03, 0.022], [0.038, 0.05], [0.026, 0.078], [0.01, 0.086]], 10);+ for (let i = 0; i < 3; i++) { const a = i * 120 + 90; g.at(M.Ry(a), () => g.tube(spline([[0, 0.07, 0], [0.038, 0.1, 0], [0.042, 0.15, 0], [0.018, 0.17, 0]], 2), (t) => [lerp(0.016, 0.003, t), lerp(0.024, 0.004, t)], 6)); }+ });+ break;+ }+ case 'scroll': {+ const p = [Lf.W[0] + 0.07, Lf.W[1], Lf.W[2]];+ const path = []; for (let i = 0; i <= 14; i++) { const t = i / 14; path.push([R.W[0] + 0.09 + 0.02 * Math.sin(t * 5), R.W[1] - t * 0.7 + 0.03, lerp(R.W[2] - 0.03, Lf.W[2] + 0.05, sstep(0, 0.5, t)) + 0.02 * Math.sin(t * 7)]); }+ const sheet = (off, flip) => b.grid((u, v) => { const k = u * 14, i = Math.min(13, Math.floor(k)), f = k - i, q = path[i], w = path[i + 1]; return [lerp(q[0], w[0], f) + off, lerp(q[1], w[1], f), lerp(q[2], w[2], f) + (v - 0.5) * 0.15, 0.96]; }, 14, 1, { flip });+ sheet(0, false); sheet(-0.004, true);+ b.at(M.of(M.T(path[14][0], path[14][1] - 0.015, path[14][2]), M.Rx(90)), () => b.lathe([[0.022, -0.085], [0.024, -0.08], [0.024, 0.08], [0.022, 0.085]], 8));+ break;+ }+ case 'bless': {+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2]), M.Rz(-64), M.Ry(4)));+ break;+ }+ default: break;+ }+}+const CROWN_FLEURS = 8;+function addCrown(g, m, R0, tall = 1) {+ g.at(m, () => {+ g.lathe([[R0, 0], [R0 + 0.009, 0.004], [R0 + 0.009, 0.028], [R0 + 0.015, 0.038], [R0 + 0.006, 0.042], [R0 - 0.004, 0.034], [R0 - 0.004, 0]], 26);+ // jewels set in the band+ for (let i = 0; i < 12; i++) { const a = i * TAU / 12; blob(g, [(R0 + 0.013) * Math.cos(a), 0.02, (R0 + 0.013) * Math.sin(a)], [0.0072, 0.0072, 0.0072], 5, 3); }+ // Fleurs-de-lis cut from sheet: flat plates set on the rim, the way a goldsmith makes them+ const h = 0.05 * tall, fl = [[-0.03, 0], [0.03, 0], [0.024, h * 0.2], [0.04, h * 0.42], [0.031, h * 0.62], [0.017, h * 0.42], [0.009, h * 0.5], [0.012, h * 0.76], [0, h * 1.1], [-0.012, h * 0.76], [-0.009, h * 0.5], [-0.017, h * 0.42], [-0.031, h * 0.62], [-0.04, h * 0.42], [-0.024, h * 0.2]];+ for (let i = 0; i < CROWN_FLEURS; i++) {+ const a = (i + 0.5) * 360 / CROWN_FLEURS;+ g.at(M.of(M.Ry(90 - a), M.T(0, 0.03, R0 + 0.004)), () => g.slab(fl, [], -0.004, 0.004, { so: 1, sr: 1 }));+ }+ });+}++const figF = {}; // fields kept while a figure is carved+const yNeck = 1.7;+async function makeFigure(kind) {+ const o = FIGURES[kind], stone = new Batch(TILE.fine), gold = new Batch(TILE.gold);+ const body = bodyField(o), arms = body.arms, hO = Object.assign({ seed: o.seed }, o.head);+ // Head frame: gently inclined, sitting on the neck above the centre of the shoulders+ const zc = arms[0].S[2] - (0.142 * (o.slim || 1)), cx = arms[0].S[0];+ const headM = M.of(M.T(cx + 0.024, yNeck + 0.116, zc + (o.sway || 0) * 0.3), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(-5));+ const hF = headField(hO), toHead = invRigid(headM);+ const headW = (x, y, z) => { const q = toHead(x, y, z); return hF(q[0], q[1], q[2]); };+ // Hands: frames derived from the sleeve so that the wrist always leaves the cuff+ const hands = [];+ const handFor = (A, side, pose) => {+ const X0 = [A.W[0] - A.E[0], A.W[1] - A.E[1], A.W[2] - A.E[2]];+ let Y0;+ if (pose === 'support') Y0 = [0.2, -1, 0]; // palm up under a book+ else if (pose === 'bless') Y0 = [0, 0.1, side]; // palm turned forward, two fingers raised+ else Y0 = [0, 0, side]; // back of the hand outward+ const X = pose === 'bless' ? [0.25, 1, 0.05] : X0;+ return { m: frameOf(A.W, X, Y0), pose, thumb: pose === 'grip' || pose === 'edge' || pose === 'rest' ? -side : side };+ };+ const handsCfg = { key: ['grip', 'support'], crozier: ['grip', 'rest'], sceptre: ['grip', 'rest'], mantle: ['rest', 'rest'], scroll: ['edge', 'rest'], child: ['rest', 'support'], bless: ['bless', 'support'] }[o.hold] || ['rest', 'rest'];+ hands.push(handFor(arms[0], 1, handsCfg[0]), handFor(arms[1], -1, handsCfg[1]));+ const handFs = hands.map((h) => { const f = handField(h.pose, h.thumb), inv = invRigid(h.m); return (x, y, z) => { const q = inv(x, y, z); return f(q[0], q[1], q[2]); }; });+ // The Child: a real little person on the Virgin's arm, turning to her+ let childF = null, childM = null;+ if (o.hold === 'child') {+ const A = arms[1];+ // sits in the crook of her left arm, turned towards her, one small hand raised to her veil+ childM = M.of(M.T(A.W[0] - 0.015, A.W[1] + 0.082, A.W[2] - 0.0), M.Ry(-34), M.Rz(4));+ const cH = headField({ hair: 'curls', seed: 21, smile: 0.6 }), inv = invRigid(childM), k = 0.47;+ childF = (x, y, z) => {+ const q = inv(x, y, z), X = q[0], Y = q[1], Z = q[2];+ // A seated child in a long shift: torso, thighs across her arm, shins and small feet hanging+ let d = sdCone(X, Y, Z, 0, 0, 0, 0, 0.165, 0, 0.066, 0.05);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.0, s * 0.034, 0.125, -0.012, s * 0.04, 0.038, 0.03), 0.018); // thigh+ d = smin(d, sdCone(X, Y, Z, 0.125, -0.012, s * 0.04, 0.142, -0.135, s * 0.038, 0.03, 0.022), 0.012); // shin+ d = smin(d, sdCone(X, Y, Z, 0.142, -0.15, s * 0.038, 0.19, -0.17, s * 0.04, 0.02, 0.013), 0.006); // foot+ }+ d -= 0.005 * Math.sqrt(Math.abs(Math.sin(Math.atan2(Z, X) * 6 + Y * 4))) * sstep(0.12, 0.0, Y) * sstep(-0.1, 0.0, Y);+ d = smin(d, sdCone(X, Y, Z, 0.005, 0.135, -0.052, 0.06, 0.07, -0.05, 0.02, 0.016), 0.012); // one arm holds an orb on his knee+ d = smin(d, len3(X - 0.1, Y - 0.052, Z + 0.042) - 0.03, 0.003);+ d = smin(d, len3(X - 0.074, Y - 0.056, Z + 0.054) - 0.015, 0.004);+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.145, 0.05, -0.005, 0.2, 0.094, 0.02, 0.015), 0.012); // the other reaches back to touch her veil+ d = smin(d, len3(X + 0.007, Y - 0.214, Z - 0.104) - 0.0155, 0.005);+ d = smin(d, cH((X - 0.008) / k, (Y - 0.262) / k, Z / k) * k, 0.012);+ return d;+ };+ }+ const allF = (x, y, z) => {+ let d = body.F(x, y, z);+ d = Math.min(d, headW(x, y, z));+ for (let i = 0; i < handFs.length; i++) d = Math.min(d, handFs[i](x, y, z));+ if (childF) d = Math.min(d, childF(x, y, z));+ return d;+ };+ // ---- carve ----+ await carveInto(stone, body.F, [-0.42, -0.005, -0.52, 0.42, yNeck + 0.04, 0.52], 0.0105, o.wings ? 3800 : 4400, null, allF);+ const head = await headBatch(o.headKey);+ for (let i = 0; i < hands.length; i++) {+ const hb = await handBatch(hands[i].pose, hands[i].thumb);+ const base = stone.count;+ copyBatch(stone, hb, hands[i].m);+ }+ if (childF) await carveInto(stone, childF, [arms[1].W[0] - 0.24, arms[1].W[1] - 0.2, arms[1].W[2] - 0.26, arms[1].W[0] + 0.3, arms[1].W[1] + 0.56, arms[1].W[2] + 0.26], 0.0042, 3200, null, allF, { smooth: 1, ao: 0.34, aoMin: 0.3 });+ addAttrs({ stone, gold }, o, arms);+ // Crown, nimbus and wings+ if (o.head.crown) addCrown(gold, M.of(headM, M.T(-0.012, o.head.veil ? 0.088 : 0.096, 0)), o.head.veil ? 0.088 : 0.084);+ if (o.nimbus) gold.at(M.of(headM, M.T(-0.16, 0.03, 0), M.Ry(90)), () => { gold.slab(ringPts(0, 0, 0.235, 28), [], -0.014, 0.014, { sb: 0.8 }); });+ if (o.wings) {+ // Feathers in overlapping tiers, carved on a blade that rises from the shoulders and falls to the calf+ const W = (side) => (x, y, z) => {+ // The wall side (local -z in every jamb) folds along the stepped stone from the shoulder; the free side stands off behind the back+ const zz = z * side - 0.12 - zc * side, X = side < 0 ? x + 0.01 + 0.6 * (zz - 0.065) : x + 0.085 + zz * 0.18 + 0.05 * clamp((1.86 - y) / 1.32), u = clamp((1.86 - y) / 1.32), wid = 0.27 * Math.pow(Math.sin(PI * Math.pow(u, 0.6)), 0.65) * (1 - 0.3 * u);+ const v = zz - 0.03 - u * 0.1 + 0.015;+ let d = Math.abs(X) - 0.026 + 0.012 * u;+ d = Math.max(d, Math.abs(v - wid * 0.5) - wid * 0.5, y - 1.87, 0.53 - y);+ // Tiers of feathers: short and scaled over the shoulder, long pointed quills towards the tip+ const row = Math.abs(Math.sin(u * 14 + v * 6)), bars = Math.abs(Math.sin(v * 26 + u * 7));+ d -= 0.015 * Math.sqrt(row) * sstep(0.45, 0.2, u) + 0.016 * Math.sqrt(bars) * sstep(0.2, 0.42, u);+ // The wing's arm: it grows out of the shoulder blade, so the blade never hangs in the air behind the body+ return smin(d * 0.8, sdCone(x, y, zz, -0.035, 1.6, -0.06, side < 0 ? -0.045 : -0.12, 1.46, 0.1, 0.05, 0.028), 0.04);+ };+ for (const side of [-1, 1]) await carveInto(stone, W(side), [-0.3, 0.5, side > 0 ? zc + 0.05 : zc - 0.72, 0.1, 1.9, side > 0 ? zc + 0.72 : zc - 0.05], 0.0105, 900, null, allF);+ }+ stone.isFigure = true;+ return { stone, gold, head, headM };+}+// ---- Seated and kneeling figures for the tympana: same people, other postures -------------+function poseField(kind) {+ if (kind === 'seated') return (x, y, z) => {+ // A figure enthroned: knees forward, the robe falling from them in a curtain to the feet+ const th = Math.atan2(z, x);+ let d = sdCone(x, y, z, -0.04, 0.72, 0, -0.03, 1.38, 0, 0.17, 0.13);+ d = smin(d, sdEll(x, y - 1.46, z, 0.09, 0.12, 0.17), 0.03);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(x, y, z, -0.03, 0.7, s * 0.1, 0.27, 0.68, s * 0.11, 0.1, 0.09), 0.03);+ const cur = sdCone(x, y, z, 0.27, 0.64, s * 0.11, 0.31, 0.04, s * 0.1, 0.09, 0.12);+ d = smin(d, cur - 0.012 * Math.sqrt(Math.abs(Math.sin(z * 34 + x * 4))), 0.03);+ }+ d = smin(d, sdCone(x, y, z, 0.16, 0.45, 0, 0.27, 0.04, 0, 0.11, 0.16) - 0.014 * Math.sqrt(Math.abs(Math.sin(z * 30 + 1))), 0.03);+ // the lap folds run across, the bodice is drawn up+ d -= 0.012 * Math.sqrt(Math.abs(Math.sin((y + 0.12 * z * z * 40) * 28))) * sstep(0.95, 0.7, y) * sstep(0.45, 0.62, y);+ d -= 0.009 * Math.sqrt(Math.abs(Math.sin(th * 7 + y * 2))) * sstep(0.8, 1.0, y);+ return Math.max(d, -y);+ };+ return (x, y, z) => {+ // Kneeling in prayer, turned to the centre: a body with a thigh and a folded shin under long cloth. The folds fall straight+ // down from the waist and break on the ground, never radiating round the body like the ribs of a pumpkin+ let d = sdCone(x, y, z, -0.1, 0.46, 0, 0.03, 1.04, 0, 0.15, 0.115);+ d = smin(d, sdEll(x - 0.05, y - 1.12, z, 0.08, 0.1, 0.15), 0.03);+ d = smin(d, sdCone(x, y, z, -0.1, 0.42, 0, 0.28, 0.15, 0, 0.16, 0.11), 0.05);+ d = smin(d, sdCone(x, y, z, 0.28, 0.12, 0, -0.26, 0.1, 0, 0.115, 0.09), 0.04);+ const fall = 0.62 * Math.sqrt(Math.abs(Math.sin(z * 31 + x * 3 + 1.1))) + 0.38 * Math.sqrt(Math.abs(Math.sin(z * 63 + y * 5 + 2.3)));+ d -= 0.017 * (fall - 0.55) * sstep(0.86, 0.42, y);+ d -= 0.006 * Math.sqrt(Math.abs(Math.sin(z * 38 + y * 2))) * sstep(0.6, 0.86, y);+ return Math.max(d, -y);+ };+}+async function makePosed(kind, spec) {+ const stone = new Batch(TILE.fine), gold = new Batch(TILE.gold), bF = poseField(kind), hO = Object.assign({ seed: 3 }, spec.head);+ const hy = kind === 'seated' ? 1.74 : 1.36, hx = kind === 'seated' ? -0.01 : 0.075;+ const headM = M.of(M.T(hx, hy, 0), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(hO.nod || -6));+ const allF = bF, head = await headBatch(spec.headKey);+ stone.isFigure = true;+ await carveInto(stone, bF, [-0.42, -0.005, -0.42, 0.56, kind === 'seated' ? 1.62 : 1.26, 0.42], 0.0145, 2600, null, allF);+ if (hO.crown) addCrown(gold, M.of(headM, M.T(-0.014, 0.1, 0)), hO.veil ? 0.106 : 0.098);+ // Forearms and hands: joined in prayer when kneeling, one raised when enthroned+ const hand = await handBatch(kind === 'seated' ? 'bless' : 'support', 1);+ if (kind === 'seated') {+ const A = spec.bless === false ? null : { W: [0.2, 1.32, 0.17] };+ const armF = (x, y, z) => { let d = sdCone(x, y, z, -0.02, 1.42, 0.17, 0.06, 1.02, 0.23, 0.066, 0.055); d = smin(d, sdCone(x, y, z, 0.06, 1.02, 0.23, 0.2, 1.3, 0.17, 0.055, 0.058), 0.02); d = Math.min(d, smin(sdCone(x, y, z, -0.02, 1.42, -0.17, 0.05, 1.02, -0.22, 0.066, 0.055), sdCone(x, y, z, 0.05, 1.02, -0.22, 0.27, 0.92, -0.08, 0.055, 0.058), 0.02)); return d; };+ await carveInto(stone, armF, [-0.12, 0.84, -0.34, 0.36, 1.52, 0.34], 0.0125, 700, null, allF);+ copyBatch(stone, hand, frameOf([0.2, 1.3, 0.17], [0.25, 1, 0.05], [0, 0.1, 1]));+ addBook(stone, M.of(M.T(0.36, 0.92, -0.08), M.Rz(-78)), { w: 0.11, h: 0.15 });+ } else {+ const armF = (x, y, z) => { let d = 1e9; for (const s of [-1, 1]) d = Math.min(d, smin(sdCone(x, y, z, 0.03, 1.03, s * 0.15, 0.1, 0.8, s * 0.17, 0.062, 0.05), sdCone(x, y, z, 0.1, 0.8, s * 0.17, 0.25, 0.97, s * 0.04, 0.05, 0.055), 0.02)); return d; };+ await carveInto(stone, armF, [-0.08, 0.68, -0.3, 0.34, 1.12, 0.3], 0.0125, 600, null, allF);+ for (const s of [-1, 1]) copyBatch(stone, hand, frameOf([0.25, 0.97, s * 0.04], [0.5, 1, -s * 0.05], [1, -0.4, s * 0.9]));+ }+ return { stone, gold, head, headM };+}++// Cache of finished figures. Lower-detail copies for the arches come from the same sculpture.+const FIG_CACHE = {};+async function lightCopy(src, tris) { // the same surface with fewer triangles, keeping its uvs and carved occlusion+ const g = await simplifyGeometry(src.toGeo(), { targetTriangles: tris });+ const T = src.tile, b = new Batch(T), p = g.attributes.position, n = g.attributes.normal, u = g.attributes.uv, c = g.attributes.color, idx = g.index;+ for (let i = 0; i < p.count; i++) b.v(p.getX(i), p.getY(i), p.getZ(i), n.getX(i), n.getY(i), n.getZ(i), u.getX(i) * T, u.getY(i) * T, c ? c.getX(i) : 1);+ for (let t = 0; t < idx.count; t += 3) b.tri(idx.getX(t), idx.getX(t + 1), idx.getX(t + 2));+ return b;+}+async function lowDetail(src, tris) {+ const b = await lightCopy(src.stone, tris);+ b.isFigure = true;+ // A figure high in the arches carries a light copy of its head inside its own mesh: fewer draws, fewer triangles+ if (!src.head.low) src.head.low = await lightCopy(src.head, 420);+ copyBatch(b, src.head.low, src.headM);+ return { stone: b, gold: new Batch(TILE.gold) };+}+async function prepareFigures(kinds) {+ for (const k of kinds) FIG_CACHE[k + '|1'] = await makeFigure(k);+ if (P.preview !== 'statues') {+ for (const k of ['apostle', 'king', 'angel', 'bishop', 'queen']) if (FIG_CACHE[k + '|1']) FIG_CACHE[k + '|0'] = await lowDetail(FIG_CACHE[k + '|1'], P.archFigureTris);+ for (let k = 0; k < 3; k++) GARGOYLES[k] = await lightCopy(makeGargoyle(k), 800);+ FIG_CACHE['seated|1'] = await makePosed('seated', { headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 9, nod: -2 } });+ FIG_CACHE['seatedVirgin|1'] = await makePosed('seated', { headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, turn: -14, tilt: 4 } });+ FIG_CACHE['kneel|1'] = await makePosed('kneel', { headKey: 'veiled', head: { veil: 1, seed: 8, tilt: 0, nod: 8 } });+ FIG_CACHE['kneel|0'] = await lowDetail(FIG_CACHE['kneel|1'], P.archFigureTris);+ }+}+function figure(name, lod = 1) {+ const k = FIG_ALIAS[name] || name;+ return FIG_CACHE[k + '|' + lod] || FIG_CACHE[k + '|1'];+}+// Body and head are separate nodes: lod 0 lightens the body only, a head costs its bytes once however often it appears+function placeFigure(name, kind, o = {}) {+ const f = figure(kind, o.lod === undefined ? 1 : o.lod);+ if (!f) return null;+ // o.relief squeezes the figure front to back, so it is carved from the wall behind it instead of standing before it+ const s = o.scale || 1, sc = [s * (o.relief || 1), s, o.mirror ? -s : s];+ const mat = o.indoor ? MAT.figureIn : o.deep ? MAT.figureDeep : o.half ? MAT.figureHalf : MAT.figure;+ const stone = place(name, f.stone, mat, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ place(name + '_Gilt', f.gold, MAT.gold, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ let headPart = null;+ if (f.head) {+ const m = f.headM, e = eulerFromBasis([m[0], m[4], m[8]], [m[1], m[5], m[9]], [m[2], m[6], m[10]]);+ headPart = place(name + '_Head', f.head, mat, { pos: [m[3], m[7], m[11]], rot: e, parent: stone || undefined });+ }+ if (stone && !o.indoor && !o.deep && !o.half) FIG_PLACED.push({ part: stone, head: headPart });+ return stone;+}+const FIG_PLACED = [];++// ---- Foliage and crockets ----------------------------------------------------------+// One carved leaf curling at the tip; local +Y up the surface, +X outward from the host+function addLeaf(b, m, size = 1, o = {}) {+ b.at(m, () => {+ const nu = o.coarse ? 2 : 4, nv = o.coarse ? 3 : 4;+ b.grid((u, v) => {+ const s = (u - 0.5) * 2, W = 0.5 * Math.sin(PI * Math.pow(v, 0.72)) * (1 - 0.35 * v);+ const curl = Math.pow(v, 2.2);+ const lobes = 1 + 0.18 * Math.sin(v * 16) * Math.abs(s);+ const rib = 0.05 * (1 - Math.abs(s));+ return [size * (rib + 0.3 * curl - 0.42 * curl * curl * curl + 0.12 * s * s), size * (v - 0.3 * curl * v), size * (s * W * lobes), 0.72 + 0.28 * (1 - Math.abs(s)) + 0.1 * curl];+ }, nu, nv, { flip: true });+ });+}+// A crocket: a bud of curled foliage that sits on an arris. Local +X away from the arris, +Y up the slope.+function addCrocket(b, m, size = 1, lod = 1) {+ b.at(m, () => {+ const path = [];+ const N = lod ? 5 : 3;+ for (let i = 0; i <= N; i++) { const t = i / N, a = -0.9 + t * 3.8, r = lerp(0.3, 0.12, t) * size; path.push([size * 0.16 + r * Math.cos(a) * 0.95, size * 0.1 + r * Math.sin(a) * 0.9, 0]); }+ b.tube(path, (t) => [lerp(0.14, 0.05, t) * size, lerp(0.17, 0.07, t) * size], lod ? 5 : 3, { capStart: false, capEnd: false, ringS: (a) => 0.8 + 0.2 * Math.cos(a) });+ if (lod) for (const s of [-1, 1]) b.tube([[0.02 * size, 0.02 * size, 0], [0.2 * size, 0.1 * size, s * 0.22 * size], [0.34 * size, 0.26 * size, s * 0.3 * size]], (t) => [lerp(0.07, 0.015, t) * size, lerp(0.05, 0.02, t) * size], 3, { capEnd: false });+ });+}+function addFinial(b, h = 1, lod = 1) { // cross-shaped foliate finial, base at y=0+ b.lathe([[0.12 * h, 0], [0.1 * h, 0.05 * h], [0.14 * h, 0.09 * h], [0.08 * h, 0.14 * h], [0.05 * h, 0.5 * h], [0.09 * h, 0.56 * h], [0.05 * h, 0.62 * h], [0.02 * h, 0.98 * h], [0, h]], lod ? 8 : 5);+ for (let i = 0; i < 4; i++) {+ const a = i * 90 + 45;+ addLeaf(b, M.of(M.T(0, 0.14 * h, 0), M.Ry(a), M.T(0.05 * h, 0, 0)), 0.4 * h, { coarse: 1 });+ if (lod) addLeaf(b, M.of(M.T(0, 0.58 * h, 0), M.Ry(a + 45), M.T(0.03 * h, 0, 0)), 0.3 * h, { coarse: 1 });+ }+}+// Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame+function crocketsAlong(b, p, q, n, size, out, lod = 1, maker = addCrocket) {+ const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);+ const dn = d.map((c) => c / L);+ let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];+ const ol = Math.hypot(ox, oy, oz) || 1; ox /= ol; oy /= ol; oz /= ol;+ const zx = ox * 0, side = [dn[1] * oz - dn[2] * oy, dn[2] * ox - dn[0] * oz, dn[0] * oy - dn[1] * ox];+ // columns: X = out, Y = along edge, Z = side+ for (let i = 0; i < n; i++) {+ const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];+ const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];+ maker(b, m, size * lerp(1, 0.78, t), lod);+ }+}+// A crocket beaten in lead: one thick leaf turning up off the arris with a lobe either side. Same frame as addCrocket: X out, Y along the edge+function addLeadCrocket(b, m, s = 1) {+ b.at(m, () => {+ b.tube([[-0.04 * s, -0.02 * s, 0], [0.2 * s, 0.1 * s, 0], [0.34 * s, 0.3 * s, 0], [0.3 * s, 0.46 * s, 0]], (t) => [lerp(0.13, 0.015, t) * s, lerp(0.17, 0.02, t) * s], 4, { capEnd: false });+ for (const z of [-1, 1]) b.tube([[0, 0, z * 0.04 * s], [0.16 * s, 0.1 * s, z * 0.2 * s], [0.2 * s, 0.24 * s, z * 0.3 * s]], (t) => lerp(0.07, 0.012, t) * s, 3, { capEnd: false });+ });+}++// Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.+function addPinnacle(b, w, h, o = {}) {+ const hw = w / 2, shaftH = h * 0.42, gableH = w * 0.95, lod = o.lod === undefined ? 1 : o.lod;+ if (o.simple) { // far-away pinnacle: faceted shaft, spirelet, a few crockets, finial+ b.lathe([[hw * 1.414, 0], [hw * 1.414, shaftH], [hw * 1.6, shaftH + w * 0.06], [hw * 1.3, shaftH + w * 0.12], [hw * 0.1, h * 0.86]], 4, { faceted: true, phase: PI / 4 });+ for (let i = 0; i < 4; i++) { const a = rad(i * 90 + 45), c = Math.cos(a), s = Math.sin(a), bR = hw * 1.3 * 1.0; crocketsAlong(b, [c * bR, shaftH + w * 0.14, s * bR], [c * hw * 0.14, h * 0.84, s * hw * 0.14], 2, w * 0.46, [c, 0.15, s], 0); }+ b.at(M.T(0, h * 0.84, 0), () => addFinial(b, h * 0.16, 0));+ return;+ }+ // Shaft with moulded base and blind lancet panels+ b.lathe([[hw * 1.44, 0], [hw * 1.44, w * 0.06], [hw * 1.3, w * 0.12], [hw * 1.3, w * 0.2], [hw * 1.26, w * 0.24]], 4, { faceted: true, phase: PI / 4 });+ b.lathe([[hw * 1.414, w * 0.2], [hw * 1.414, shaftH]], 4, { faceted: true, phase: PI / 4, s: 0.95 });+ for (let i = 0; i < 4; i++) b.at(M.Ry(i * 90), () => {+ const pw = w * 0.62, y0 = w * 0.35, ys = shaftH - w * 0.5;+ b.at(M.T(hw + 0.004, 0, 0), () => b.at(M.Ry(90), () => { // panel recessed look: shaded inset lancet with moulded frame+ b.sweep(ofsXY(pointedArc(pw, pw * 0.85, 3), 0, ys), hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[-pw / 2, y0], [-pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[pw / 2, y0], [pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.face([[-pw / 2, y0, -0.002, 0.5], [pw / 2, y0, -0.002, 0.5], [pw / 2, ys, -0.002, 0.5], [0, ys + pw * 0.8, -0.002, 0.45], [-pw / 2, ys, -0.002, 0.5]]);+ }));+ // Gablet on each face with crockets+ b.at(M.T(hw * 0.96, shaftH, 0), () => {+ b.slabX = true;+ b.at(M.Ry(90), () => b.slab([[-hw * 1.04, 0], [hw * 1.04, 0], [0, gableH]], [], 0, w * 0.08, { sf: 1, sb: 0.8 }));+ crocketsAlong(b, [0.02, 0.02, -hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, -1], 0);+ crocketsAlong(b, [0.02, 0.02, hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, 1], 0);+ b.at(M.T(0.04, gableH * 0.94, 0), () => addFinial(b, w * 0.34, 0));+ });+ });+ // Cornice ring and spirelet+ b.at(M.T(0, shaftH, 0), () => {+ b.lathe([[hw * 1.414, 0], [hw * 1.6, w * 0.06], [hw * 1.6, w * 0.1], [hw * 1.414, w * 0.14]], 4, { faceted: true, phase: PI / 4 });+ const sh = h - shaftH - w * 0.1;+ b.at(M.T(0, w * 0.12, 0), () => {+ b.lathe([[hw * 1.3, 0], [hw * 0.09, sh * 0.82]], 8, { faceted: true, phase: PI / 8 });+ const n = lod ? 5 : 3;+ for (let i = 0; i < 4; i++) {+ const a = rad(i * 90 + 22.5 + 22.5 * 0);+ const c = Math.cos(a + rad(22.5)), s = Math.sin(a + rad(22.5));+ const bR = hw * 1.3 * Math.cos(rad(0)), tR = hw * 0.09;+ crocketsAlong(b, [c * bR, sh * 0.04, s * bR], [c * tR, sh * 0.8, s * tR], n, w * 0.36, [c, 0.12, s], lod);+ }+ b.at(M.T(0, sh * 0.8, 0), () => (o.finial || b) === b ? addFinial(b, sh * 0.22 + w * 0.1, lod) : null);+ });+ });+}++// ---- Gargoyle ------------------------------------------------------------------------+// A crouching beast thrusting out from the wall along +X, root at the origin. About 2.3 m long.+function makeGargoyle(kind = 0) {+ const b = new Batch(TILE.fine), rng = mkRng(kind * 17 + 5);+ const ridge = (kind === 1 ? 0.3 : 0.22);+ const headY = kind === 2 ? 0.1 : 0.34;+ const spine = spline([[-0.3, -0.05, 0], [0.22, 0.2, 0], [0.75, 0.3, 0], [1.2, 0.2, 0], [1.56, headY, 0]], 5);+ b.tube(spine, (t) => { const r = keyf([[0, 0.34, 0.3], [0.25, 0.36, 0.34], [0.55, 0.22, 0.24], [0.82, 0.16, 0.17], [1, 0.19, 0.2]], t); return [r[0], r[1]]; }, 16, {+ capStart: true,+ up: [0, 1, 0],+ ring: (a, t) => 1 + ridge * Math.pow(Math.max(Math.cos(a), 0), 10) * Math.abs(Math.sin(t * 26)) + 0.05 * Math.abs(Math.sin(a * 5 + t * 11)),+ ringS: (a, t) => 0.74 + 0.26 * Math.abs(Math.sin(a * 5 + t * 11)),+ });+ // Skull, snout, gaping jaws+ const hx = 1.56, hy = headY;+ blob(b, [hx + 0.05, hy + 0.03, 0], [0.22, 0.19, 0.2], 14, 10, 0.92);+ b.tube(spline([[hx + 0.1, hy + 0.06, 0], [hx + 0.34, hy + 0.05, 0], [hx + 0.56, hy + 0.1, 0]], 3), (t) => [lerp(0.14, 0.07, t), lerp(0.15, 0.085, t)], 10, { capStart: true, up: [0, 1, 0], ring: (a, t) => 1 + 0.2 * Math.pow(Math.max(Math.cos(a), 0), 6) * Math.abs(Math.sin(t * 9)) });+ b.shade(0.9, () => b.tube(spline([[hx + 0.05, hy - 0.1, 0], [hx + 0.28, hy - 0.2, 0], [hx + 0.5, hy - 0.2, 0]], 3), (t) => [lerp(0.09, 0.045, t), lerp(0.125, 0.07, t)], 10, { capStart: true, up: [0, 1, 0] }));+ b.shade(0.34, () => blob(b, [hx + 0.2, hy - 0.06, 0], [0.16, 0.085, 0.095], 8, 5));+ for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {+ const f = i / 3.5;+ b.tube([[hx + 0.26 + f * 0.27, hy + 0.0 + f * 0.075, s * lerp(0.1, 0.06, f)], [hx + 0.27 + f * 0.27, hy - 0.08 + f * 0.075, s * lerp(0.1, 0.06, f)]], (t) => lerp(0.018, 0.002, t), 4, { capStart: true });+ b.tube([[hx + 0.22 + f * 0.27, hy - 0.15, s * lerp(0.1, 0.05, f)], [hx + 0.22 + f * 0.27, hy - 0.08, s * lerp(0.1, 0.05, f)]], (t) => lerp(0.016, 0.002, t), 4, { capStart: true });+ }+ // Brow, eyes, ears or horns+ for (const s of [-1, 1]) {+ b.tube(spline([[hx + 0.02, hy + 0.2, s * 0.03], [hx + 0.17, hy + 0.17, s * 0.1], [hx + 0.24, hy + 0.12, s * 0.15]], 2), (t) => lerp(0.03, 0.02, t), 5, { capStart: true });+ b.shade(0.38, () => blob(b, [hx + 0.21, hy + 0.09, s * 0.11], [0.05, 0.04, 0.045], 6, 4));+ blob(b, [hx + 0.235, hy + 0.09, s * 0.115], [0.024, 0.022, 0.022], 6, 4);+ if (kind === 1) b.tube(spline([[hx - 0.05, hy + 0.14, s * 0.11], [hx - 0.1, hy + 0.3, s * 0.18], [hx - 0.25, hy + 0.42, s * 0.2], [hx - 0.42, hy + 0.42, s * 0.15]], 3), (t) => lerp(0.05, 0.005, t), 6, { capStart: true });+ else b.grid((u, v) => { const L = 0.3, W = 0.09 * Math.sin(PI * u); return [hx - 0.08 - u * L * 0.5, hy + 0.13 + u * L * 0.7, s * (0.13 + u * 0.09 + (v - 0.5) * W * 2), 0.7 + 0.3 * Math.abs(v - 0.5) * 2]; }, 4, 2, { flip: s > 0 });+ if (kind === 1) { /* also add jowls */ blob(b, [hx + 0.1, hy - 0.04, s * 0.13], [0.09, 0.06, 0.05], 6, 4, 0.9); }+ // Fore leg gripping the ledge+ const sh = [0.5, 0.12, s * 0.3], el = [0.68, -0.28, s * 0.42], wr = [0.98, -0.42, s * 0.3];+ b.tube(spline([sh, el, wr], 4), (t) => keyf([[0, 0.16], [0.4, 0.105], [1, 0.07]], t)[0], 10, { capStart: true, ring: (a, t) => 1 + 0.08 * Math.abs(Math.sin(a * 3 + t * 8)) });+ blob(b, wr, [0.1, 0.06, 0.09], 8, 5);+ for (let i = -1; i <= 1; i++) b.tube(spline([[wr[0] + 0.03, wr[1], wr[2] + i * 0.05], [wr[0] + 0.14, wr[1] - 0.02, wr[2] + i * 0.08], [wr[0] + 0.21, wr[1] - 0.11, wr[2] + i * 0.09]], 2), (t) => lerp(0.032, 0.004, t), 5);+ // Folded wing+ const span = kind === 0 ? 0.95 : 0.7;+ const wingFn = (off) => (u, v) => {+ const L = u * span, W = 0.44 * Math.sin(PI * Math.pow(u, 0.7));+ const memb = Math.abs(Math.sin(v * 7.5 + u * 1.2)) * 0.05 * u;+ return [0.42 - L + (off), 0.3 + 0.48 * Math.sin(u * 1.25) * (1 - v * 0.9) - v * W * 0.75 + memb, s * (0.27 + 0.2 * u + v * W * 0.5), 0.68 + 0.32 * (memb / 0.05)];+ };+ b.grid(wingFn(0), 7, 5, { flip: s < 0 });+ b.grid(wingFn(-0.02), 7, 5, { flip: s > 0 });+ }+ return b;+}+const GARGOYLES = [];+function gargoyle(kind) { if (!GARGOYLES[kind]) GARGOYLES[kind] = makeGargoyle(kind); return GARGOYLES[kind]; }++// ==================================================================================+// STAINED GLASS: a tiny vector-art engine producing real leaded panes.+// Every shape is a pane of pot-metal colour with a came (lead strip) around it; large+// figures get painted grisaille lines. Windows are geometry, so they stay crisp at any distance.+// ==================================================================================+function offsetPoly(pts, d) {+ const n = pts.length, s = area2(pts) >= 0 ? 1 : -1, out = [];+ for (let i = 0; i < n; i++) {+ const a = pts[(i - 1 + n) % n], b = pts[i], c = pts[(i + 1) % n];+ let ax = b[0] - a[0], ay = b[1] - a[1], cx = c[0] - b[0], cy = c[1] - b[1];+ const la = Math.hypot(ax, ay) || 1, lc = Math.hypot(cx, cy) || 1; ax /= la; ay /= la; cx /= lc; cy /= lc;+ let nx = (ay + cy) * s, ny = -(ax + cx) * s; const l = Math.hypot(nx, ny) || 1; nx /= l; ny /= l;+ const k = clamp(1 / Math.max(0.35, Math.abs(nx * ay * s - ny * ax * s)), 1, 2.4);+ out.push([b[0] + nx * d * k, b[1] + ny * d * k]);+ }+ return out;+}+function clipConvex(poly, clip) {+ let out = poly; const sgn = area2(clip) >= 0 ? 1 : -1;+ for (let i = 0; i < clip.length && out.length; i++) {+ const a = clip[i], b = clip[(i + 1) % clip.length], inp = out; out = [];+ const side = (p) => sgn * ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]));+ for (let j = 0; j < inp.length; j++) {+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);+ if (sp >= 0) out.push(p);+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([lerp(p[0], q[0], t), lerp(p[1], q[1], t)]); }+ }+ }+ return out;+}+function isConvex(p) { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = p[(i + 2) % p.length]; const s = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); if (Math.abs(s) < 1e-9) continue; if (!sign) sign = Math.sign(s); else if (Math.sign(s) !== sign) return false; } return true; }++class Glass {+ constructor(seed = 1) { this.b = new Batch(1); this.b.noColor = true; this.r = mkRng(seed * 13 + 3); this.clip = null; this.xf = [1, 0, 0, 1, 0, 0]; this.came = 0.022; }+ xform(a, b, c, d, e, f) { const m = this.xf; return [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]]; }+ withXf(rotDeg, tx, ty, s, fn) { const o = this.xf, c = Math.cos(rad(rotDeg)) * s, sn = Math.sin(rad(rotDeg)) * s; this.xf = this.xform(c, sn, -sn, c, tx, ty); fn(this); this.xf = o; }+ withClip(pts, fn) { const o = this.clip; const m = this.xf; this.clip = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]); fn(this); this.clip = o; }+ toLocal(p) { const m = this.xf, det = m[0] * m[3] - m[1] * m[2], x = p[0] - m[4], y = p[1] - m[5]; return [(m[3] * x - m[2] * y) / det, (-m[1] * x + m[0] * y) / det]; }+ // Writes one flat polygon. map(worldPoint) -> [u,v] in atlas pixels; otherwise a window on the sheet of its colour+ raw(pts, color, layer, jitter = 1, map = null) {+ if (layer === -0.4 && !this.b.outline) this.b.outline = pts; // the first polygon of a window is its whole outline+ const name = GLASS_ALIAS[color] || color, fam = GL.fam[name] || GL.fam.lead, z = 0.002 + layer * 0.0035, C = GL.small;+ if (this.b.panes) this.b.panes.push([pts.map((p) => [p[0], p[1]]), name, layer]); // the cartoon, for the light this window throws+ // Every pane is cut from a different part of the sheet, so no two panes match;+ // 200 pixels of sheet to the metre, so the streaks and clouds of the pot-metal show in the pane+ const ox = fam[0] + 44 + this.r() * (C - 88), oy = fam[1] + 44 + this.r() * (C - 88), rot = this.r() * 6.28, cr = Math.cos(rot) * 200, sr = Math.sin(rot) * 200;+ let mx = 0, my = 0; for (const p of pts) { mx += p[0]; my += p[1]; } mx /= pts.length; my /= pts.length;+ const ids = pts.map((p) => {+ let u, v;+ if (map) { const q = map(p); u = q[0]; v = q[1]; } else {+ const dx = clamp(p[0] - mx, -0.2, 0.2), dy = clamp(p[1] - my, -0.2, 0.2);+ u = ox + (dx * cr - dy * sr) * jitter; v = oy + (dx * sr + dy * cr) * jitter;+ }+ return this.b.v(p[0], p[1], z, 0, 0, 1, u / GL.w, v / GL.h, 1);+ });+ if (isConvex(pts)) { const flip = area2(pts) < 0; for (let i = 1; i < ids.length - 1; i++) { if (flip) this.b.tri(ids[0], ids[i + 1], ids[i]); else this.b.tri(ids[0], ids[i], ids[i + 1]); } } else { const t = triangulate(pts); for (const k of t) this.b.tri(ids[k[0]], ids[k[1]], ids[k[2]]); }+ }+ // a pane: colour polygon + lead came. o.tile paints drapery across it, o.orn ornament.+ poly(pts, color, layer = 1, o = {}) {+ const m = this.xf; let P2 = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]);+ if (area2(P2) < 0) P2 = P2.slice().reverse();+ const came = o.came === undefined ? this.came : o.came;+ if (came > 0) {+ let C = offsetPoly(P2, came);+ if (this.clip) C = clipConvex(C, this.clip);+ if (C.length >= 3 && Math.abs(area2(C)) > 1e-5) this.raw(C, 'lead', layer - 0.5, 0);+ } else if (came < 0) P2 = offsetPoly(P2, came); // over a lead backing: inset instead of a second polygon+ if (this.clip) P2 = clipConvex(P2, this.clip);+ if (P2.length < 3 || Math.abs(area2(P2)) <= 1e-5) return this;+ let map = o.map || null;+ if (o.tile && GL.tile[o.tile]) { // planar mapping of the painted tile over the local box+ const T = GL.tile[o.tile], B = o.box, f = o.flipU ? -1 : 1;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + (f > 0 ? a : 1 - a) * (T[2] - 4), T[1] + 2 + (1 - c) * (T[3] - 4)]; };+ } else if (o.orn && GL.orn[o.orn]) {+ const T = GL.orn[o.orn], B = o.box;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + a * (GL.small - 4), T[1] + 2 + (1 - c) * (GL.small - 4)]; };+ }+ this.raw(P2, color, layer, o.jitter === undefined ? 1 : o.jitter, map);+ return this;+ }+ disc(cx, cy, r, color, layer = 1, n = 12, o) { return this.poly(ringPts(cx, cy, r, n), color, layer, o); }+ // a head drawn by the glass painter: an oval pane mapped onto a face from the atlas+ face(cx, cy, rx, ry, kind, layer, flip) {+ const F = GL.face[kind] || GL.face.youth, C = GL.cell, pts = []; for (let i = 0; i < 12; i++) { const a = i * TAU / 12; pts.push([cx + rx * Math.cos(a), cy + ry * Math.sin(a)]); }+ return this.poly(pts, 'flesh', layer, { came: 0.012, map: (p) => { const q = this.toLocal(p), a = ((q[0] - cx) / rx) * (flip ? -1 : 1), c = (q[1] - cy) / ry; return [F[0] + C / 2 + clamp(a, -1, 1) * (C / 2 - 3), F[1] + C / 2 - clamp(c, -1, 1) * (C / 2 - 3)]; } });+ }+ // painted line (grisaille): a thin dark ribbon along a polyline+ line(pts, w = 0.02, color = 'lead', layer = 5) {+ for (let i = 0; i < pts.length - 1; i++) {+ const a = pts[i], b = pts[i + 1]; let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx *= w / 2 / l; dy *= w / 2 / l;+ this.poly([[a[0] - dy, a[1] + dx], [a[0] + dy, a[1] - dx], [b[0] + dy, b[1] - dx], [b[0] - dy, b[1] + dx]], color, layer, { came: 0, jitter: 0 });+ }+ return this;+ }+}++// Diapered mosaic background: lozenges of two deep colours, a jewel at each crossing+// (one painted square of leaded lozenges laid down repeatedly: fine as real mosaic, cheap as two triangles a square)+function gDiaper(g, x0, y0, x1, y1, cell, c1, c2, c3, layer = 0) {+ const T = c1 === 'outer' ? 1.5 : 0.72, G = GL.ground[GROUND_OF[c1] || 'rc'], C = GL.cell;+ for (let y = Math.floor(y0 / T) * T; y < y1; y += T) for (let x = Math.floor(x0 / T) * T; x < x1; x += T) {+ const qx = x, qy = y, map = (p) => [G[0] + 2 + clamp((p[0] - qx) / T) * (C - 4), G[1] + 2 + clamp((p[1] - qy) / T) * (C - 4)];+ g.poly([[x, y], [x + T, y], [x + T, y + T], [x, y + T]], c1, layer, { came: 0, map });+ }+}++// A figure in glass: long Gothic proportions, robes painted in brown, the head drawn by hand. (x,y) = feet.+function gFigure(g, x, y, h, o = {}) {+ const sx = o.sx || 0.82, hTot = h; h = hTot * sx;+ const f = o.flip ? -1 : 1, L = (u, v) => [x + f * u * h, y + (v <= 0.76 ? v / 0.76 * (hTot - 0.24 * h) : hTot - (1 - v) * h)], PS = (a) => a.map((p) => L(p[0], p[1]));+ const robe = o.robe || 'white', mantle = o.mantle || 'cobalt', skin = o.skin || 'flesh', big = hTot > 2.2, layer = o.layer === undefined ? 2 : o.layer;+ const sit = o.pose === 'sit', kneel = o.pose === 'kneel';+ const hy = kneel ? 0.67 : sit ? 0.86 : 0.875, hr = 0.066, hx = kneel ? 0.04 : 0;+ // Box in world units for tile mapping, in the local frame of the Glass object+ const bx = [x - h * 0.34, y, x + h * 0.34, y + hTot * 0.86], tR = DRAPE_OF[robe] || 'white', tM = DRAPE_OF[mantle] || 'cobalt';+ const pane = (pts, col, lay, tile, opt) => g.poly(PS(pts), col, lay, Object.assign({ tile, box: bx, flipU: !!o.flip }, opt || {}));+ const brk = (pts, c1, lay, tile) => { // a robe is many pieces of glass: cut it across with lead+ const P3 = PS(pts); let y0 = 1e9, y1 = -1e9; for (const q of P3) { y0 = Math.min(y0, q[1]); y1 = Math.max(y1, q[1]); }+ const n = big ? 4 : (hTot > 0.9 ? 2 : 1);+ for (let i = 0; i < n; i++) {+ const a = lerp(y0, y1, i / n), c = lerp(y0, y1, (i + 1) / n), sl = big ? 0.12 * h * (i % 2 ? 1 : -1) * 0.5 : 0;+ const piece = clipConvex(clipConvex(P3, [[-1e4, a - sl * 0], [1e4, a + sl], [1e4, 1e4], [-1e4, 1e4]]), [[-1e4, -1e4], [1e4, -1e4], [1e4, c + sl], [-1e4, c - sl * 0]]);+ if (piece.length >= 3) g.poly(piece, c1, lay, { tile, box: bx, flipU: !!o.flip, came: 0.016 });+ }+ };+ if (o.wings) {+ pane([[-0.08, hy - 0.1], [-0.3, hy + 0.1], [-0.4, hy - 0.08], [-0.34, 0.4], [-0.24, 0.2], [-0.1, 0.42]], o.wings, layer - 0.2, null);+ pane([[0.08, hy - 0.1], [0.3, hy + 0.1], [0.4, hy - 0.08], [0.34, 0.4], [0.24, 0.2], [0.1, 0.42]], o.wings, layer - 0.2, null);+ if (hTot > 1) for (const s of [-1, 1]) { g.line(PS([[s * 0.1, hy - 0.08], [s * 0.3, 0.52]]), h * 0.01, 'lead', layer); g.line(PS([[s * 0.2, hy - 0.02], [s * 0.34, 0.42]]), h * 0.008, 'lead', layer); }+ }+ if (o.halo !== false) {+ const c = L(hx, hy);+ g.disc(c[0], c[1], h * 0.116, o.halo || 'gold', layer - 0.1, big ? 18 : 12);+ if (o.cross) { for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[c[0] + h * 0.116 * Math.cos(a - 0.26), c[1] + h * 0.116 * Math.sin(a - 0.26)], [c[0] + h * 0.116 * Math.cos(a + 0.26), c[1] + h * 0.116 * Math.sin(a + 0.26)], c], 'ruby', layer, { came: 0.008 }); } }+ }+ // ---- body ----+ if (sit) {+ brk([[-0.12, 0.72], [0.12, 0.72], [0.19, 0.44], [0.26, 0.4], [0.27, 0.08], [0.22, 0.0], [-0.22, 0.0], [-0.2, 0.3], [-0.14, 0.44]], robe, layer, tR);+ brk([[-0.13, 0.74], [0.05, 0.74], [0.17, 0.46], [0.24, 0.4], [0.2, 0.18], [-0.06, 0.1], [-0.2, 0.3], [-0.14, 0.46]], mantle, layer + 0.5, tM);+ } else if (kneel) {+ brk([[-0.08, 0.56], [0.13, 0.56], [0.16, 0.26], [0.26, 0.1], [0.26, 0], [-0.26, 0], [-0.2, 0.12]], robe, layer, tR);+ brk([[-0.09, 0.57], [0.08, 0.57], [0.13, 0.28], [0.1, 0.1], [-0.2, 0.05], [-0.16, 0.24]], mantle, layer + 0.5, tM);+ } else {+ const c = o.sway === undefined ? 0.02 : o.sway;+ brk([[-0.1 + c, 0.75], [0.1 + c, 0.75], [0.115, 0.46], [0.14, 0.2], [0.2, 0.03], [0.15, 0.0], [0.05, 0.022], [-0.05, 0.0], [-0.16, 0.012], [-0.2, 0.04], [-0.14, 0.22], [-0.115, 0.46]], robe, layer, tR);+ brk([[-0.11 + c, 0.76], [0.03 + c, 0.76], [0.12, 0.5], [0.135, 0.3], [0.09, 0.15], [-0.06, 0.11], [-0.165, 0.22], [-0.125, 0.46]], mantle, layer + 0.5, tM);+ pane([[-0.09, 0.03], [-0.02, 0.03], [-0.02, -0.012], [-0.115, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ pane([[0.02, 0.03], [0.09, 0.03], [0.115, -0.012], [0.02, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ }+ // ---- arms and hands ----+ const by = kneel ? 0.5 : 0.68, armPose = o.arm || 'hold';+ const ribbon = (a, b, w, col, tile) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy) || 1, nx = -dy / l * w / 2, ny = dx / l * w / 2; g.poly(PS([[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]]), col, layer + 1, { came: 0.012, tile, box: bx }); };+ const hand = (p) => { const c = L(p[0], p[1]); g.poly([[c[0] - h * 0.026, c[1] - h * 0.018], [c[0] + h * 0.018, c[1] - h * 0.022], [c[0] + h * 0.03, c[1] + h * 0.016], [c[0] + h * 0.006, c[1] + h * 0.03], [c[0] - h * 0.02, c[1] + h * 0.018]], skin, layer + 1.3, { came: 0.008 }); };+ const arms = { hold: [[0.13, by - 0.12], [0.03, by - 0.18], [-0.13, by - 0.12], [-0.03, by - 0.2]], up: [[0.17, by - 0.03], [0.27, by + 0.18], [-0.12, by - 0.12], [-0.03, by - 0.22]], out: [[0.19, by - 0.08], [0.32, by - 0.06], [-0.19, by - 0.08], [-0.32, by - 0.06]], pray: [[0.1, by - 0.14], [0.12, by - 0.03], [-0.04, by - 0.14], [0.1, by - 0.03]], point: [[0.17, by - 0.08], [0.33, by + 0.04], [-0.12, by - 0.12], [-0.04, by - 0.2]], orant: [[0.2, by + 0.0], [0.27, by + 0.14], [-0.2, by + 0.0], [-0.27, by + 0.14]] };+ const A = arms[armPose] || arms.hold;+ ribbon([0.095, by], A[0], 0.07, mantle, tM); ribbon(A[0], A[1], 0.056, robe, tR); hand(A[1]);+ ribbon([-0.095, by], A[2], 0.07, mantle, tM); ribbon(A[2], A[3], 0.056, robe, tR); hand(A[3]);+ // ---- attributes ----+ const at = o.attr;+ if (at === 'book') { g.poly(PS([[A[1][0] - 0.1, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] + 0.12], [A[1][0] - 0.1, A[1][1] + 0.12]]), 'ruby', layer + 1.5, { came: 0.01 }); g.poly(PS([[A[1][0] - 0.07, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.09], [A[1][0] - 0.07, A[1][1] + 0.09]]), 'gold', layer + 1.6, { came: 0 }); }+ else if (at === 'staff' || at === 'cross') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, 0], [sxx + 0.012, 0], [sxx + 0.012, 1.08], [sxx - 0.012, 1.08]]), 'gold', layer + 0.9, { came: 0.008 }); if (at === 'cross') g.poly(PS([[sxx - 0.07, 0.95], [sxx + 0.07, 0.95], [sxx + 0.07, 0.98], [sxx - 0.07, 0.98]]), 'gold', layer + 0.9, { came: 0.008 }); else g.poly(ringPts(sxx + 0.03, 1.1, 0.045, 8).map((p) => L(p[0], p[1])), 'gold', layer + 0.9, { came: 0.008 }); }+ else if (at === 'sword') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, by - 0.08], [sxx + 0.012, by - 0.08], [sxx, 1.02]]), 'white', layer + 0.9, { came: 0.008 }); g.poly(PS([[sxx - 0.05, by - 0.08], [sxx + 0.05, by - 0.08], [sxx + 0.05, by - 0.05], [sxx - 0.05, by - 0.05]]), 'gold', layer + 1, { came: 0.008 }); }+ else if (at === 'key') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.01, by - 0.14], [sxx + 0.01, by - 0.14], [sxx + 0.01, by + 0.2], [sxx - 0.01, by + 0.2]]), 'gold', layer + 1, { came: 0.006 }); g.poly(ringPts(sxx, by + 0.23, 0.035, 6).map((p) => L(p[0], p[1])), 'gold', layer + 1, { came: 0.006 }); g.poly(PS([[sxx, by - 0.14], [sxx + 0.05, by - 0.14], [sxx + 0.05, by - 0.1], [sxx, by - 0.1]]), 'gold', layer + 1, { came: 0.006 }); }+ else if (at === 'lily') { const sxx = A[1][0]; g.line(PS([[sxx, by - 0.22], [sxx + 0.01, by + 0.2]]), h * 0.012, 'green', layer + 1); for (let i = 0; i < 3; i++) { const yy = by + 0.08 + i * 0.07; const q = L(sxx + 0.02 * (i % 2 ? 1 : -1), yy); g.disc(q[0], q[1], h * 0.024, 'white', layer + 1.1, 5, { came: 0.006 }); } }+ // ---- head: drawn, not a disc ----+ const hc = L(hx, hy);+ const kind = o.face || (o.wings ? 'angel' : o.veil ? 'virgin' : o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ const nkA = L(hx - 0.02, hy - 0.11), nkB = L(hx + 0.02, hy - 0.11), nkC = L(hx + 0.02, hy - 0.04), nkD = L(hx - 0.02, hy - 0.04);+ g.poly([nkA, nkB, nkC, nkD], skin, layer + 1.5, { came: 0.008 });+ g.face(hc[0], hc[1], h * hr * 1.02, h * hr * 1.24, kind, layer + 1.8, !!o.flip);+ if (o.crown) g.poly(PS([[hx - 0.066, hy + 0.05], [hx + 0.066, hy + 0.05], [hx + 0.08, hy + 0.13], [hx + 0.04, hy + 0.095], [hx, hy + 0.14], [hx - 0.04, hy + 0.095], [hx - 0.08, hy + 0.13]]), 'gold', layer + 2.2, { came: 0.01 });+ if (o.mitre) g.poly(PS([[hx - 0.056, hy + 0.05], [hx + 0.056, hy + 0.05], [hx + 0.048, hy + 0.15], [hx, hy + 0.215], [hx - 0.048, hy + 0.15]]), o.mitre, layer + 2.2, { came: 0.01 });+}+// ---- Narrative scenes, composed inside a circle of radius r at the origin ---------+const SCENES = {+ annunciation(g, r) { // Angel greets the Virgin; lily between them, dove above+ gFigure(g, -r * 0.46, -r * 0.72, r * 1.34, { robe: 'white', mantle: 'emerald', wings: 'rubyLt', arm: 'point', attr: null, hair: 'amber' });+ gFigure(g, r * 0.46, -r * 0.72, r * 1.3, { flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray', halo: 'gold' });+ g.poly([[-0.1, -r * 0.72], [0.1, -r * 0.72], [0.06, -r * 0.5], [-0.06, -r * 0.5]], 'gold', 3, { came: 0.012 });+ g.line([[0, -r * 0.5], [0, r * 0.05]], 0.025, 'green', 3.2);+ [0.05, -0.1, -0.24].forEach((v, i) => g.disc((i % 2 ? 1 : -1) * r * 0.05, r * v, r * 0.05, 'white', 3.3, 5, { came: 0.008 }));+ g.disc(0, r * 0.62, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.14, r * 0.66], [0, r * 0.6], [r * 0.14, r * 0.66], [0, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ nativity(g, r) { // Virgin, Child in the crib, Joseph, the star, the ox and ass+ g.poly([[-r * 0.8, -r * 0.9], [r * 0.8, -r * 0.9], [r * 0.8, -r * 0.62], [-r * 0.8, -r * 0.62]], 'brown', 1.5);+ gFigure(g, -r * 0.42, -r * 0.62, r * 1.05, { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'out' });+ gFigure(g, r * 0.56, -r * 0.62, r * 1.1, { robe: 'tan', mantle: 'emerald', beard: 1, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'staff', flip: 1, halo: 'yellow' });+ g.poly([[-r * 0.1, -r * 0.62], [r * 0.3, -r * 0.62], [r * 0.34, -r * 0.34], [-r * 0.14, -r * 0.34]], 'amber', 2, { came: 0.014 });+ g.poly([[-r * 0.07, -r * 0.34], [r * 0.27, -r * 0.34], [r * 0.27, -r * 0.22], [-r * 0.07, -r * 0.22]], 'white', 2.5, { came: 0.012 });+ g.disc(r * 0.02, -r * 0.24, r * 0.065, 'gold', 2.5, 8, { came: 0.008 }); g.disc(r * 0.02, -r * 0.24, r * 0.043, 'flesh', 3, 7, { came: 0.008 });+ star(g, 0, r * 0.68, r * 0.13, 'yellow', 3);+ g.poly([[-r * 0.4, r * 0.22], [0, r * 0.52], [r * 0.4, r * 0.22], [r * 0.36, r * 0.14], [0, r * 0.4], [-r * 0.36, r * 0.14]], 'bone', 1.5);+ },+ magi(g, r) { // Three crowned kings bring gifts to the enthroned Virgin+ gFigure(g, r * 0.56, -r * 0.8, r * 1.2, { pose: 'sit', flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'hold' });+ g.disc(r * 0.4, -r * 0.1, r * 0.06, 'gold', 4, 8, { came: 0.008 });+ gFigure(g, -r * 0.05, -r * 0.8, r * 1.18, { pose: 'kneel', robe: 'emerald', mantle: 'ruby', crown: 1, beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'point', halo: false });+ gFigure(g, -r * 0.42, -r * 0.8, r * 1.32, { robe: 'violet', mantle: 'gold', crown: 1, beard: 1, arm: 'point', halo: false });+ gFigure(g, -r * 0.74, -r * 0.8, r * 1.3, { robe: 'teal', mantle: 'rubyLt', crown: 1, arm: 'hold', halo: false });+ star(g, -r * 0.1, r * 0.72, r * 0.11, 'yellow', 3);+ },+ crucifixion(g, r) { // Christ on the cross, the Virgin and Saint John, sun and moon+ g.poly([[-0.09 * r, -r * 0.92], [0.09 * r, -r * 0.92], [0.09 * r, r * 0.76], [-0.09 * r, r * 0.76]], 'emerald', 1.5, { came: 0.016 });+ g.poly([[-r * 0.58, r * 0.28], [r * 0.58, r * 0.28], [r * 0.58, r * 0.45], [-r * 0.58, r * 0.45]], 'emerald', 1.5, { came: 0.016 });+ gFigure(g, 0, -r * 0.84, r * 1.42, { robe: 'white', mantle: 'white', cross: 1, arm: 'out', halo: 'gold', hair: 'hair', beard: 1, sway: 0 });+ gFigure(g, -r * 0.62, -r * 0.88, r * 1.1, { robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray' });+ gFigure(g, r * 0.62, -r * 0.88, r * 1.1, { flip: 1, robe: 'white', mantle: 'rubyLt', arm: 'hold', attr: 'book' });+ g.disc(-r * 0.68, r * 0.66, r * 0.11, 'gold', 2, 8); g.poly([[r * 0.59, r * 0.74], [r * 0.76, r * 0.6], [r * 0.71, r * 0.76]], 'pearl', 2);+ },+ resurrection(g, r) { // Christ rises from the tomb holding the banner; soldiers sleep below+ g.poly([[-r * 0.72, -r * 0.84], [r * 0.72, -r * 0.84], [r * 0.72, -r * 0.42], [-r * 0.72, -r * 0.42]], 'bone', 2);+ g.line([[-r * 0.72, -r * 0.56], [r * 0.72, -r * 0.56]], 0.02, 'lead', 3); [-0.4, 0, 0.4].forEach((k) => g.disc(r * k, -r * 0.68, r * 0.09, 'purple', 3, 8, { came: 0.01 }));+ gFigure(g, 0, -r * 0.42, r * 1.25, { robe: 'white', mantle: 'rubyLt', cross: 1, arm: 'up', hair: 'hair', beard: 1, halo: 'gold' });+ g.poly([[r * 0.34, r * 0.72], [r * 0.34, r * 0.92], [r * 0.7, r * 0.84], [r * 0.34, r * 0.72]], 'white', 3, { came: 0.01 }); g.line([[r * 0.34, r * 0.92], [r * 0.34, -r * 0.1]], 0.02, 'gold', 3.2);+ for (const s of [-1, 1]) { g.disc(s * r * 0.82, -r * 0.5, r * 0.07, 'flesh', 3, 7, { came: 0.01 }); g.poly([[s * r * 0.6, -r * 0.86], [s * r * 0.92, -r * 0.86], [s * r * 0.9, -r * 0.54], [s * r * 0.7, -r * 0.54]], s > 0 ? 'emerald' : 'cobalt', 2.6); }+ },+ supper(g, r) { // The Last Supper: Christ at the centre of a laid table+ [-0.62, -0.31, 0.31, 0.62].forEach((k, i) => gBust(g, r * k, -r * 0.05, r * 0.92, { robe: ['emerald', 'ruby', 'amber', 'violet'][i], beard: i % 2 ? 1 : 0, hair: i === 3 ? 'grey' : 'hair' }));+ gBust(g, 0, -r * 0.02, r * 1.04, { robe: 'rubyLt', beard: 1, cross: 1 });+ g.poly([[-r * 0.92, -r * 0.5], [r * 0.92, -r * 0.5], [r * 0.92, -r * 0.2], [-r * 0.92, -r * 0.2]], 'white', 4);+ g.poly([[-r * 0.86, -r * 0.5], [r * 0.86, -r * 0.5], [r * 0.82, -r * 0.92], [-r * 0.82, -r * 0.92]], 'brown', 3.6);+ [-0.58, -0.2, 0.2, 0.58].forEach((k) => g.disc(r * k, -r * 0.3, r * 0.055, 'gold', 4.5, 6, { came: 0.008 }));+ },+ noah(g, r) { // The Ark on the waters, the dove returning+ [-0.82, -0.56].forEach((v, i) => g.poly([[-r, r * v], [r, r * v], [r, r * (v + 0.22)], [-r, r * (v + 0.18)]], i ? 'azure' : 'cobalt', 2));+ g.poly([[-r * 0.7, -r * 0.4], [r * 0.7, -r * 0.4], [r * 0.5, -r * 0.7], [-r * 0.5, -r * 0.7]], 'brown', 2.5);+ g.poly([[-r * 0.46, -r * 0.4], [r * 0.46, -r * 0.4], [r * 0.46, r * 0.02], [-r * 0.46, r * 0.02]], 'tan', 3);+ g.poly([[-r * 0.56, r * 0.0], [0, r * 0.32], [r * 0.56, r * 0.0]], 'ruby', 3.2);+ [-0.2, 0.2].forEach((k) => g.disc(r * k, -r * 0.18, r * 0.1, 'night', 3.4, 7, { came: 0.012 }));+ gBust(g, r * 0.2, -r * 0.3, r * 0.42, { robe: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', layer: 3.6 });+ g.disc(-r * 0.54, r * 0.64, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.72, r * 0.72], [-r * 0.54, r * 0.62], [-r * 0.36, r * 0.74], [-r * 0.54, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ shepherd(g, r) { // The Good Shepherd carries the lamb; a flock in green pasture+ g.poly([[-r, -r], [r, -r], [r, -r * 0.5], [-r, -r * 0.62]], 'green', 1.5);+ gFigure(g, 0, -r * 0.82, r * 1.5, { robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'hold', attr: 'staff' });+ g.disc(0, r * 0.26, r * 0.11, 'white', 4.5, 8, { came: 0.01 }); g.disc(r * 0.13, r * 0.3, r * 0.05, 'pearl', 4.8, 6, { came: 0.008 });+ for (const [x, y] of [[-0.68, -0.64], [0.66, -0.62], [-0.46, -0.78]]) { g.disc(r * x, r * y, r * 0.11, 'white', 3, 7, { came: 0.01 }); g.disc(r * x + r * 0.12, r * y + r * 0.05, r * 0.05, 'grey', 3.2, 5, { came: 0.008 }); }+ },+ david(g, r) { // King David with his harp+ gFigure(g, -r * 0.14, -r * 0.82, r * 1.42, { pose: 'sit', robe: 'violet', mantle: 'ruby', crown: 1, beard: 1, arm: 'out', halo: false });+ g.poly([[r * 0.26, -r * 0.6], [r * 0.7, r * 0.2], [r * 0.7, r * 0.46], [r * 0.16, -r * 0.36]], 'gold', 3, { came: 0.012 });+ g.line([[r * 0.2, -r * 0.4], [r * 0.2, r * 0.4]], 0.018, 'gold', 3.1); [0.3, 0.4, 0.5, 0.6].forEach((k) => g.line([[r * k, -r * 0.42 + r * (k - 0.2)], [r * k, r * 0.34]], 0.008, 'white', 3.2));+ g.poly([[-r * 0.86, -r * 0.84], [r * 0.5, -r * 0.84], [r * 0.5, -r * 0.7], [-r * 0.86, -r * 0.7]], 'tan', 1.8);+ },+};+function star(g, x, y, r, c, layer) { const p = []; for (let i = 0; i < 16; i++) { const a = i * PI / 8 + PI / 2, q = i % 2 ? r * 0.38 : r * (i % 4 ? 0.7 : 1); p.push([x + q * Math.cos(a), y + q * Math.sin(a)]); } g.poly(p, c, layer, { came: 0.01 }); }+function gBust(g, x, y, h, o = {}) { // head and shoulders, the head drawn by the painter+ const L = o.layer || 2, bx = [x - h * 0.22, y - h * 0.32, x + h * 0.22, y + h * 0.46], t = DRAPE_OF[o.robe || 'ruby'] || 'ruby';+ if (o.halo !== false) g.disc(x, y + h * 0.58, h * 0.17, o.halo || 'gold', L - 0.1, 10);+ g.poly([[x - h * 0.2, y - h * 0.3], [x + h * 0.2, y - h * 0.3], [x + h * 0.17, y + h * 0.3], [x + h * 0.06, y + h * 0.42], [x - h * 0.06, y + h * 0.42], [x - h * 0.17, y + h * 0.3]], o.robe || 'ruby', L, { tile: t, box: bx });+ const kind = o.face || (o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ g.face(x, y + h * 0.58, h * 0.108, h * 0.132, kind, L + 0.5, false);+ if (o.crown) g.poly([[x - h * 0.1, y + h * 0.66], [x + h * 0.1, y + h * 0.66], [x + h * 0.12, y + h * 0.78], [x + h * 0.05, y + h * 0.72], [x, y + h * 0.8], [x - h * 0.05, y + h * 0.72], [x - h * 0.12, y + h * 0.78]], 'gold', L + 0.7, { came: 0.008 });+ if (o.cross) for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[x + h * 0.17 * Math.cos(a - 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a - 0.24)], [x + h * 0.17 * Math.cos(a + 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a + 0.24)], [x, y + h * 0.58]], 'ruby', L + 0.2, { came: 0.008 }); }+}+// A jewelled ring of short leaded segments+function gRim(g, cx, cy, r0, r1, cols, n, layer) {+ g.disc(cx, cy, r1 + 0.02, 'lead', layer - 0.3, Math.max(10, n), { came: 0, jitter: 0 });+ for (let i = 0; i < n; i++) {+ const a = i * TAU / n, b = (i + 1) * TAU / n;+ g.poly([[cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)], [cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)], [cx + r1 * Math.cos(b), cy + r1 * Math.sin(b)], [cx + r0 * Math.cos(b), cy + r0 * Math.sin(b)]], cols[i % cols.length], layer, { came: -0.01 });+ }+}+// A lancet told as a stack of medallions over a diapered ground, with a jewelled border+function gLancetStory(g, x, w, y0, ys, rise, scenes, o = {}) {+ const c1 = o.c1 || 'ruby', c2 = o.c2 || 'cobalt', edge = o.edge || ['gold', 'ruby', 'white', 'cobalt'];+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse().slice(0)];+ g.withClip(outline, () => {+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, o.cell || 0.6, c1, c2, 'gold');+ const H = ys + rise * 0.5 - y0, N = scenes.length, step = H / N, R = Math.min(w * 0.44, step * 0.47);+ scenes.forEach((nm, i) => {+ const cy = y0 + step * (i + 0.5);+ const ground = (i % 2) ? (o.g1 || 'cobalt') : (o.g2 || 'ruby');+ gRim(g, x, cy, R, R * 1.055, ['gold', 'ruby', 'gold', 'cobalt'], 22, 1);+ g.disc(x, cy, R, ground, 1.3, 18, { came: 0 });+ g.withClip(ringPts(x, cy, R, 14), () => g.withXf(0, x, cy, 1, () => SCENES[nm](g, R)));+ if (i < N - 1) { const yy = y0 + step * (i + 1); for (const s of [-1, 1]) { g.disc(x + s * w * 0.3, yy, w * 0.058, 'gold', 1.2, 8, { came: 0.012 }); g.disc(x + s * w * 0.3, yy, w * 0.032, 'ruby', 1.5, 6, { came: 0.008 }); } }+ });+ // Border bricks follow the outline: thin, and each brick painted with a stem of leaves+ const bw = w * 0.062, stepB = 0.34;+ const stripe = (x0, y1, y2) => { let k = 0; g.poly([[x0, y1], [x0 + bw, y1], [x0 + bw, y2], [x0, y2]], 'lead', 3.5, { came: 0, jitter: 0 }); for (let y = y1; y < y2; y += stepB) { const c = edge[k++ % edge.length]; g.poly([[x0, y], [x0 + bw, y], [x0 + bw, y + stepB], [x0, y + stepB]], c, 3.8, { came: -0.012, orn: c === 'gold' ? 'gold' : c === 'white' ? 'white' : null, box: [x0, y, x0 + bw, y + stepB] }); } };+ stripe(x - w / 2, y0, ys + rise * 0.1); stripe(x + w / 2 - bw, y0, ys + rise * 0.1);+ // Saddle bars (iron armature)+ for (let y = y0 + 1.1; y < ys + rise * 0.6; y += 1.1) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.045], [x - w / 2, y + 0.045]], 'lead', 7, { came: 0, jitter: 0 });+ });+}+// A lancet filled by one monumental standing figure under a painted canopy (reads from the floor)+function gLancetSaint(g, x, w, y0, ys, rise, fig, o = {}) {+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse()];+ const gnd = o.ground || 'cobalt';+ g.withClip(outline, () => {+ const figY = y0 + 1.05, figH = (ys + rise * 0.1 - figY) * 0.84;+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, 0.8, gnd, o.ground2 || 'ruby', null);+ // Pedestal: a band of painted gold the figure stands on+ g.poly([[x - w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 1.02], [x - w * 0.46, y0 + 1.02]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.46, y0 + 0.62, x + w * 0.46, y0 + 1.02] });+ // Canopy: a thin arch of gold, a gable with its ruby field, amber pinnacles either side+ const cy = figY + figH + 0.02, cH = ys + rise * 0.86 - cy;+ g.poly([[x - w * 0.48, cy], [x + w * 0.48, cy], [x + w * 0.48, cy + cH * 0.1], [x - w * 0.48, cy + cH * 0.1]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.48, cy, x + w * 0.48, cy + cH * 0.1] });+ g.poly([[x - w * 0.42, cy + cH * 0.08], [x + w * 0.42, cy + cH * 0.08], [x, cy + cH]], 'yellow', 1.7, { orn: 'gold', box: [x - w * 0.42, cy + cH * 0.08, x + w * 0.42, cy + cH] });+ g.poly([[x - w * 0.22, cy + cH * 0.14], [x + w * 0.22, cy + cH * 0.14], [x, cy + cH * 0.62]], 'ruby', 1.9, { came: 0.016 });+ for (const s of [-1, 1]) g.poly([[x + s * w * 0.3, cy + cH * 0.12], [x + s * w * 0.46, cy + cH * 0.12], [x + s * w * 0.38, cy + cH * 0.85]], 'amber', 1.8);+ gFigure(g, x, figY, figH, Object.assign({ layer: 2.5, sx: Math.min(1, w * 1.72 / figH) }, fig));+ for (let y = y0 + 1.5; y < ys + rise * 0.6; y += 1.5) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.05], [x - w / 2, y + 0.05]], 'lead', 9, { came: 0, jitter: 0 });+ });+}+function gOculus(g, cx, cy, r, o = {}) { // head of the window: a sexfoil with an angel or Christ+ g.withClip(ringPts(cx, cy, r, 20), () => {+ g.disc(cx, cy, r, o.ground || 'cobalt', 1, 20);+ for (let i = 0; i < 6; i++) { const a = i * PI / 3 + PI / 6; g.disc(cx + r * 0.68 * Math.cos(a), cy + r * 0.68 * Math.sin(a), r * 0.27, i % 2 ? 'ruby' : 'gold', 1.4, 8); }+ g.disc(cx, cy, r * 0.56, o.inner || 'rubyLt', 1.8, 14);+ g.withXf(0, cx, cy, 1, () => { if (o.bust) gBust(g, 0, -r * 0.45, r * 0.95, o.bust); else gFigure(g, 0, -r * 0.5, r * 0.98, { wings: 'gold', robe: 'white', mantle: 'emerald', arm: 'orant' }); });+ });+}++// A complete two-light window: lancets, oculus, base field. Local plane z=0, +Z = interior.+function makeWindowGlass(w, y0, ys, rise, seed, fill) {+ const g = new Glass(seed);+ if (fill.trackPanes) g.b.panes = [];+ const outline = archOutline(w, y0, ys, rise, 8);+ g.raw(outline.slice(), 'lead', -0.4, 0);+ g.withClip(outline, () => gDiaper(g, -w / 2, ys, w / 2, ys + rise, 0.55, 'cobalt', 'ruby', 'gold', 0));+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96;+ const syl = ys + rise * 0.02;+ fill.light(g, -lx, lw, y0, syl, lr, 0); fill.light(g, lx, lw, y0, syl, lr, 1);+ const oc = windowHead(w, ys, rise);+ gOculus(g, 0, oc.y, oc.r + 0.03, fill.oculus || {});+ return g.b;+}+function windowHead(w, ys, rise) { const r = w * 0.225; return { r, y: ys + rise * 0.62 }; }++// ==================================================================================+// STONE TRACERY for a two-light window (plane z=0, bars deep along Z)+// ==================================================================================+function addTracery(b, w, y0, ys, rise, o = {}) {+ const bw = o.bar || 0.26, bd = o.depth || 0.42, sec = traceryBar(bw, bd), secT = hexBar(bw * 0.62, bd * 0.8);+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96, syl = ys + rise * 0.02;+ // Central mullion with shaft ring and capital+ b.at(M.T(0, 0, 0), () => {+ b.sweep([[0, y0 - 0.05], [0, syl]], sec, {});+ b.at(M.T(0, syl, 0), () => b.lathe([[bw * 0.52, -0.3], [bw * 0.56, -0.2], [bw * 0.9, 0.02], [bw * 0.5, 0.06]], 6));+ });+ for (const s of [-1, 1]) {+ const arc = pointedArc(lw, lr, 5).map((p) => [s * lx + p[0], syl + p[1]]);+ b.sweep(arc, sec, {});+ // cusped trefoil head inside each lancet+ const cr = lw * 0.3, cy = syl + lr * 0.34;+ const cusp = []; for (let i = 0; i <= 8; i++) { const a = rad(-30 + i * 240 / 8); const R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([s * lx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, { caps: false });+ b.sweep([[s * (lx + lw / 2), cy - cr * 0.4], cusp[0]], secT, { caps: false });+ b.sweep([[s * (lx - lw / 2), cy - cr * 0.4], cusp[8]], secT, { caps: false });+ }+ // Oculus with six foils+ const oc = windowHead(w, ys, rise);+ b.sweep(ringPts(0, oc.y, oc.r, 14), sec, { closed: true });+ const foils = []; for (let i = 0; i < 18; i++) { const a = i * TAU / 18; const R = oc.r * (0.62 + 0.3 * Math.pow(Math.abs(Math.cos(a * 3)), 0.8)); foils.push([R * Math.cos(a), oc.y + R * Math.sin(a)]); }+ b.sweep(foils, secT, { closed: true, crease: 40 });+}++// ==================================================================================+// PIERCED PARAPET with quatrefoil panels+// ==================================================================================+function quatrefoil(cx, cy, r, n = 16) { const p = []; for (let i = 0; i < n; i++) { const a = i * TAU / n; const R = r * (0.72 + 0.28 * Math.abs(Math.cos(2 * a))); p.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); } return p; }+function addParapet(b, x0, x1, y, h, th, o = {}) {+ const n = Math.max(1, Math.round((x1 - x0) / (o.pitch || 1.05))), holes = [];+ const r = Math.min(h * 0.34, (x1 - x0) / n * 0.4);+ for (let i = 0; i < n; i++) holes.push(quatrefoil(x0 + (i + 0.5) * (x1 - x0) / n, y + h * 0.5, r, o.nh || 12));+ b.slab([[x0, y], [x1, y], [x1, y + h], [x0, y + h]], holes, -th / 2, th / 2, { sr: 0.6 });+ const cap = barSection(0.14, th + 0.14);+ b.sweep([[x0, y + h + 0.04], [x1, y + h + 0.04]], cap, {});+ b.sweep([[x0, y - 0.02], [x1, y - 0.02]], barSection(0.1, th + 0.1), { s: 0.8 });+}++// A wimperg: gable over a window with crockets, finial and a blind trefoil+function addGable(b, w, h, z0, th, y, o = {}) {+ const lod = o.lod === undefined ? 1 : o.lod;+ const holes = o.solid ? [] : [ringPts(0, y + h * 0.36, Math.min(w * 0.13, h * 0.2), 14)];+ if (!o.noField) b.slab([[-w / 2, y], [w / 2, y], [0, y + h]], holes, z0, z0 + th, { sf: 1, sb: 0.8 });+ const rake = barSection(0.18, th + 0.14);+ b.sweep([[-w / 2 - 0.06, y - 0.02], [0, y + h + 0.05]], rake, { z: z0 + th / 2 });+ b.sweep([[0, y + h + 0.05], [w / 2 + 0.06, y - 0.02]], rake, { z: z0 + th / 2 });+ const n = o.crockets === undefined ? Math.max(2, Math.round(Math.hypot(w / 2, h) / 1.05)) : o.crockets, sz = o.size || 0.46;+ const zc = z0 + th / 2;+ crocketsAlong(b, [-w / 2, y + 0.05, zc], [-0.03, y + h * 0.97, zc], n, sz, [-h, w / 2, 0], lod);+ crocketsAlong(b, [w / 2, y + 0.05, zc], [0.03, y + h * 0.97, zc], n, sz, [h, w / 2, 0], lod);+ b.at(M.T(0, y + h * 0.98, zc), () => addFinial(b, o.finial || 0.95, lod));+}+// A small blind trefoil-headed arcade on a wall face (plane z=0, moulding stands proud +Z)+function addBlindArcade(b, x0, x1, y0, ys, n, o = {}) {+ const w = (x1 - x0) / n, sec = hexBar(o.bar || 0.12, o.depth || 0.16), rise = w * 0.95;+ for (let i = 0; i < n; i++) {+ const x = x0 + (i + 0.5) * w;+ b.sweep(pointedArc(w * 0.9, rise, 4).map((p) => [x + p[0], ys + p[1]]), sec, { z: 0.04, caps: false });+ }+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * w;+ b.at(M.T(x, 0, 0.06), () => b.lathe([[0.1, y0], [0.065, y0 + 0.18], [0.065, ys - 0.14], [0.13, ys], [0.08, ys + 0.04]], 5));+ }+}++// ==================================================================================+// RIBBED VAULT: pointed-arc ribs, curved webs, carved or gilded boss.+// ==================================================================================+function arcH(L, H, f) { // height of a two-centred arc of span L, rise H at fraction f+ const c = (H * H - L * L / 4) / L, r = c + L / 2, d = Math.abs(f - 0.5) * L;+ return Math.sqrt(Math.max(0, r * r - (d + c) * (d + c)));+}+function addRib(b, A, B, yS, rise, sec, n = 8, half = false) { // rib between plan points A,B in their vertical plane+ const dx = B[0] - A[0], dz = B[1] - A[1], L = Math.hypot(dx, dz), ang = Math.atan2(dz, dx) * 180 / PI;+ const pts = [];+ if (half) for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L * 2, rise, t / 2)]); }+ else for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L, rise, t)]); }+ // plane: local X along A->B, local Y up+ b.at(M.of(M.T(A[0], 0, A[1]), M.Ry(-ang)), () => b.sweep(pts, sec, { caps: false }));+}+// One curved web between a side arc a-b and the crown c, faces downward whichever way the plan runs+function addVaultCell(A, a, b, c, yS, yC, sideRiseV, nS = 8, nT = 6) {+ const La = Math.hypot(c[0] - a[0], c[1] - a[1]), Lb = Math.hypot(c[0] - b[0], c[1] - b[1]), Lab = Math.hypot(b[0] - a[0], b[1] - a[1]), rise = yC - yS;+ const cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ A.grid((s, t) => {+ const px = lerp(a[0], c[0], t) * (1 - s) + lerp(b[0], c[0], t) * s, pz = lerp(a[1], c[1], t) * (1 - s) + lerp(b[1], c[1], t) * s;+ const ruled = lerp(yS + arcH(Lab, sideRiseV, s), yC, t);+ const dA = arcH(La * 2, rise, t / 2) - rise * t, dB = arcH(Lb * 2, rise, t / 2) - rise * t;+ const y = ruled + (1 - s) * (1 - s) * dA + s * s * dB;+ return [px, y, pz, 0.72 + 0.28 * t];+ }, nS, nT, { flip: cross < 0, metric: true });+}+function addRibVault(F, A, corners, yS, yC, o = {}) {+ // corners: 4 plan points in order; web cells between each side and the crown+ const c0 = corners, cx = (c0[0][0] + c0[1][0] + c0[2][0] + c0[3][0]) / 4, cz = (c0[0][1] + c0[1][1] + c0[2][1] + c0[3][1]) / 4;+ const crown = [cx, yC, cz], nS = o.nS || 8, nT = o.nT || 6, rise = yC - yS;+ const sideRise = o.sideRise || ((i) => rise * 0.94);+ for (let k = 0; k < 4; k++) {+ if (o.skipSide && o.skipSide[k]) continue;+ addVaultCell(F, c0[k], c0[(k + 1) % 4], [cx, cz], yS, yC, sideRise(k), nS, nT);+ }+ const sec = o.ribSec || rollSection(0.32, 0.42, 2, 0.04, 2);+ for (const c of c0) addRib(F, c, [cx, cz], yS, rise, sec, 8, true);+ for (let k = 0; k < 4; k++) if (!(o.skipRib && o.skipRib[k])) addRib(F, c0[k], c0[(k + 1) % 4], yS, sideRiseRib(o, k, rise), o.sideRibSec || sec, 8);+ return crown;+}+const sideRiseRib = (o, k, rise) => (o.sideRise ? o.sideRise(k) : rise * 0.94);+function addBoss(G, F, c, r = 0.42) { // carved foliate boss with gilding+ G.at(M.T(c[0], c[1] - 0.02, c[2]), () => {+ G.lathe([[r * 0.25, -r * 0.54], [r * 0.6, -r * 0.5], [r * 0.95, -r * 0.3], [r, -r * 0.08], [r * 0.7, r * 0.05]], 10);+ for (let i = 0; i < 8; i++) addLeaf(G, M.of(M.T(0, -r * 0.22, 0), M.Ry(i * 45), M.T(r * 0.72, 0, 0), M.Rz(160)), r * 0.72);+ });+}++// ==================================================================================+// CLUSTERED PIER: a lobed section of shafts around a core, with bases and foliate capitals+// ==================================================================================+function lobedRing(core, shafts, n = 64) {+ const out = [];+ for (let i = 0; i < n; i++) {+ const a = TAU * i / n, c = Math.cos(a), s = Math.sin(a);+ let best = core, nx = c, nz = s;+ for (const sh of shafts) {+ // ray from the origin against each shaft circle+ const b = c * sh[0] + s * sh[1], disc = b * b - (sh[0] * sh[0] + sh[1] * sh[1] - sh[2] * sh[2]);+ if (disc >= 0) { const t = b + Math.sqrt(disc); if (t > best) { best = t; nx = (t * c - sh[0]) / sh[2]; nz = (t * s - sh[1]) / sh[2]; } }+ }+ out.push([best * c, best * s, nx, nz]);+ }+ return out;+}+function addExtrudedRing(b, ring, y0, y1, o = {}) {+ const s0 = o.s0 === undefined ? 1 : o.s0, s1 = o.s1 === undefined ? 1 : o.s1, k0 = o.k0 || 1, k1 = o.k1 || 1, N = ring.length;+ const base = b.count; let perim = 0;+ for (let i = 0; i <= N; i++) {+ const r = ring[i % N], rp = ring[(i + N - 1) % N];+ if (i > 0) perim += Math.hypot(r[0] - rp[0], r[1] - rp[1]);+ const shade = 0.5 + 0.5 * Math.pow(Math.abs(r[2] * r[0] + r[3] * r[1]) / (Math.hypot(r[0], r[1]) || 1), 2);+ b.v(r[0] * k0, y0, r[1] * k0, r[2], 0, r[3], perim, y0, s0 * shade);+ b.v(r[0] * k1, y1, r[1] * k1, r[2], 0, r[3], perim, y1, s1 * shade);+ }+ for (let i = 0; i < N; i++) b.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+}+function addCapital(b, ring, y, h, k, o = {}) { // bell with crocket leaves on each shaft, moulded abacus+ const N = ring.length;+ addExtrudedRing(b, ring, y, y + h * 0.1, { k0: 1.0, k1: 1.03, s0: 0.8 });+ addExtrudedRing(b, ring, y + h * 0.1, y + h * 0.82, { k0: 1.03, k1: k, s0: 0.62, s1: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.82, y + h * 0.9, { k0: k, k1: k + 0.1, s0: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.9, y + h, { k0: k + 0.1, k1: k + 0.12, s0: 0.95 });+ const top = ring.map((r) => [r[0] * (k + 0.12), r[1] * (k + 0.12)]);+ const ids = top.map((p) => b.v(p[0], y + h, p[1], 0, 1, 0, undefined, undefined, 0.9)); for (let i = 1; i < N - 1; i++) b.tri(ids[0], ids[i + 1], ids[i]);+ const leaves = o.leaves || 12;+ for (let i = 0; i < leaves; i++) {+ const a = i * 360 / leaves + (o.phase || 0), idx = Math.round(i / leaves * N) % N, r = ring[idx], R = Math.hypot(r[0], r[1]);+ addLeaf(b, M.of(M.T(0, y + h * 0.1, 0), M.Ry(-a), M.T(R * 1.02, 0, 0)), h * (i % 2 ? 0.72 : 0.56), { coarse: 1 });+ }+}++// The bay plan shared by every straight module (+Z side of the axis)+const zn = P.naveSpan / 2, zaw = zn + P.aisleSpan, BAY = P.bay;+// Window openings follow the bay, so changing the bay length re-proportions the glass and tracery+const WIN = {+ aW: BAY - 1.95, aSill: 4.3, aSp: P.pierTop - 1, aRise: 5.6 * (BAY - 1.95) / 4.65,+ cW: BAY - 1.55, cSill: P.triforiumTop + 0.45, cSp: P.vaultSpring + 4.8, cRise: 6.7 * (BAY - 1.55) / 5.05,+};+const windowArgs = { aisle: [WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise], clere: [WIN.cW, WIN.cSill, WIN.cSp, WIN.cRise] };+const mkBatches = () => { const D = new Batch(1); D.noColor = true; return { A: new Batch(TILE.ashlar), F: new Batch(TILE.fine), L: new Batch(TILE.lead), G: new Batch(TILE.gold), O: new Batch(TILE.oak), I: new Batch(TILE.iron), D }; };+// A stain hanging from a ledge on a wall facing local +z: runoff, drips, soft shadow or rising damp. Few vertices, crisp detail.+const STAIN_RNG = mkRng(4242);+function addStain(D, x0, x1, y, z, drop, kind = 'streak') {+ const R = DECAL_UV[kind], n = Math.max(1, Math.round((x1 - x0) / 4)), w = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = lerp(x0, x1, i / n), c = lerp(x0, x1, (i + 1) / n), span = w * Math.min(1, (c - a) / 5), u0 = R[0] + 0.002 + STAIN_RNG() * (w - span - 0.004);+ const q = [D.v(a, y, z, 0, 0, 1, u0, R[1], 1), D.v(a, y - drop, z, 0, 0, 1, u0, R[3], 1), D.v(c, y - drop, z, 0, 0, 1, u0 + span, R[3], 1), D.v(c, y, z, 0, 0, 1, u0 + span, R[1], 1)];+ D.quad(q[0], q[1], q[2], q[3]);+ }+}+// A real recess in a wall facing local +z, whose slab already has the opening cut: back plate, moulded frame, a sill+function addRecess(A, F, outline, depth) {+ const o = area2(outline) > 0 ? outline : outline.slice().reverse();+ let x0 = 1e9, x1 = -1e9, y0 = 1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); }+ A.face(o.map((p) => [p[0], p[1], -depth]));+ F.sweep(o, rollSection(0.1, 0.22, 1, 0.03, 2), { closed: true, crease: 40, z: -0.02 });+ F.sweep([[x0 - 0.16, y0 - 0.06], [x1 + 0.16, y0 - 0.06]], stringSection(0.16, 0.14), {});+}+// A real buttress on a wall facing local +z. stages, bottom to top: { y0, y1, w: half width, f: how far its front stands out }.+// Each stage narrows and steps back, and ends in a sloped stone cap running up to the next: a solid wedge of the pier, never+// a sheet. o.back ties it into the masonry behind; the last stage ends in a weathering back to the wall, or flat with o.flat.+function addPier(set, stages, o = {}) {+ const { A, F } = set, zb = -(o.back === undefined ? 0.3 : o.back);+ const face = (m, hw, y0, y1, p) => {+ const out = [[-hw, y0], [hw, y0], [hw, y1], [-hw, y1]];+ if (!p) { A.at(m, () => A.face(out.map((q) => [q[0], q[1], 0]))); return; }+ const hole = archOutline(hw * 2 * p.k, y0 + p.b, y1 - p.t - p.r, p.r, 5);+ A.at(m, () => { A.slab(out, [hole], -0.2, 0, { noOuterSides: true, noBack: true, sr: 0.66 }); F.at(m, () => addRecess(A, F, hole, 0.2)); });+ };+ stages.forEach((s, i) => {+ const n = stages[i + 1], mid = (s.f + zb) / 2, half = (s.f - zb) / 2;+ face(M.T(0, 0, s.f), s.w, s.y0, s.y1, s.panel);+ face(M.of(M.T(s.w, 0, mid), M.Ry(90)), half, s.y0, s.y1, s.sidePanel);+ face(M.of(M.T(-s.w, 0, mid), M.Ry(-90)), half, s.y0, s.y1, s.sidePanel);+ if (s.backFace) A.face([[s.w, s.y0, zb], [-s.w, s.y0, zb], [-s.w, s.y1, zb], [s.w, s.y1, zb]]);+ if (!s.noDrip) F.box(-s.w - 0.05, s.w + 0.05, s.y1 - 0.14, s.y1, zb, s.f + 0.05, { noTop: true });+ if (!n && o.flat) { F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [s.w, s.y1, zb], [-s.w, s.y1, zb]]); return; }+ const ny = n ? n.y0 : s.y1 + (o.capH || (s.f - zb) * 0.9), nw = n ? n.w : s.w, nf = n ? n.f : zb;+ F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [nw, ny, nf], [-nw, ny, nf]]);+ F.face([[s.w, s.y1, s.f], [s.w, s.y1, zb], [nw, ny, zb], [nw, ny, nf]]);+ F.face([[-s.w, s.y1, zb], [-s.w, s.y1, s.f], [-nw, ny, nf], [-nw, ny, zb]]);+ });+}+function placeSet(name, set, pos, rot, parent, extra = {}) {+ const o = { pos, rot: [0, rot, 0], parent, scale: extra.scale };+ place(name + '_Ashlar', set.A, MAT.ashlar, o);+ place(name + '_Stone', set.F, MAT.fine, o);+ place(name + '_Lead', set.L, MAT.lead, o);+ place(name + '_Gilt', set.G, MAT.gold, o);+ place(name + '_Oak', set.O, MAT.oak, o);+ place(name + '_Iron', set.I, MAT.iron, o);+ if (set.D) place(name + '_Weathering', set.D, MAT.decal, o);+}+// Glass pair: interior face + the same panes mirrored as the exterior face+function placeGlass(name, batch, pos, rotY, parent, keepOutside, k = 1) {+ place(name + '_In', batch, keepOutside ? MAT.glassRose : MAT.glassIn, { pos, rot: [0, rotY, 0], scale: [k, k, 1], parent });+ place(name + '_Out', keepOutside ? batch : glassOutside(batch), keepOutside ? MAT.glassOut : MAT.glassOutWin, { pos, rot: [0, rotY, 0], scale: [k, k, -1], parent });+}+// By day a window is dark from outside and the story cannot be read, so its outer skin is only the+// window's outline over a mosaic of pot-metals: the colour hints through for a few triangles.+function glassOutside(batch) {+ if (batch.outer) return batch.outer;+ if (!batch.outline) return batch;+ const g = new Glass(7), o = batch.outline;+ let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); }+ g.raw(o, 'lead', -0.4, 0);+ g.withClip(o, () => gDiaper(g, x0, y0, x1, y1, 0, 'outer', null, null, 0));+ batch.outer = g.b;+ return g.b;+}++function buildNaveBay(o = {}) {+ const set = o.into || mkBatches(), { A, F, L, G } = set;+ const B = o.B || BAY, ww = o.ww || BAY - 2.1, sp = P.pierTop, archRise = o.archRise || 5.1, thick = 0.85;+ const zw = o.zw === undefined ? zn : o.zw;+ if (o.part !== 'aisle') {+ // ---- Arcade wall with the moulded pointed arch ----+ const arc = pointedArc(ww, archRise, 7).map((p) => [p[0], p[1] + sp]);+ A.at(M.T(0, 0, zw), () => {+ A.slab([[-B / 2, sp], [-ww / 2, sp], ...arc, [ww / 2, sp], [B / 2, sp], [B / 2, P.triforiumSill], [-B / 2, P.triforiumSill]], [], -thick / 2, thick / 2, { noOuterSides: true });+ });+ F.at(M.T(0, 0, zw), () => { F.sweep(arc, rollSection(0.82, thick + 0.25, 2, 0.08, 3), {}); });+ // Hood mould over the arch on the nave side, with carved label stops+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ const hood = pointedArc(ww + 1.9, archRise + 1.0, 6).map((p) => [p[0], p[1] + sp]);+ F.sweep(hood, stringSection(0.16, 0.14), {});+ });+ // ---- Triforium: string course, four cusped arches, dark wall passage ----+ const yT = P.triforiumSill, yT2 = P.triforiumTop, nT = o.nT || 4, tw = o.tw || (BAY - 1.72) / 4, ty = yT + 0.62, tspr = yT2 - 1.5, gapT = nT > 2 ? 0.08 : 0;+ const trfX = (i) => (i + 0.5 - nT / 2) * tw + (i < nT / 2 ? -gapT : gapT);+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ F.sweep([[-B / 2, yT], [B / 2, yT]], stringSection(0.34, 0.3), {});+ F.sweep([[-B / 2, yT2], [B / 2, yT2]], stringSection(0.3, 0.26), {});+ // Colonnette arcade standing in the openings+ for (let i = 0; i < nT; i++) {+ const x = trfX(i);+ const a = pointedArc(tw * 0.92, 0.98, 4).map((p) => [x + p[0], tspr + p[1]]);+ F.sweep(a, hexBar(0.13, 0.26), { z: 0.05 });+ F.sweep(ringPts(x, tspr + 0.32, 0.17, 6), hexBar(0.05, 0.1), { z: 0.05, closed: true, s: 0.9 });+ }+ for (let i = 0; i <= nT; i++) {+ const mid = i === nT / 2;+ const x = (i - nT / 2) * tw + (i < nT / 2 ? -gapT : (mid ? 0 : gapT));+ const r = mid ? 0.14 : 0.085;+ F.at(M.T(x, 0, 0.05), () => F.lathe([[r * 1.7, ty - 0.02], [r, ty + 0.2], [r, tspr - 0.2], [r * 1.9, tspr - 0.01], [r * 1.5, tspr + 0.03]], 6));+ }+ });+ const holes = []; // openings through the inner skin+ for (let i = 0; i < nT; i++) { const x = trfX(i); holes.push(archOutline(tw * 0.92, ty, tspr, 0.98, 6).map((p) => [x + p[0], p[1]])); }+ A.at(M.T(0, 0, zw - thick / 2 + 0.22), () => A.slab([[-B / 2, yT], [B / 2, yT], [B / 2, yT2], [-B / 2, yT2]], holes, -0.22, 0.22, { noOuterSides: true, sf: 0.45, sr: 0.5 }));+ // Passage back wall and floor: seen only through the arcade, kept dark+ // Round the apse the passage fans out with the radius, so the walls of neighbouring bays meet at their corners+ const zBk = zw + 0.92, zFr = zw - thick / 2 + 0.44, hB = o.wedge ? (B / 2) * zBk / zw : B / 2, hF = o.wedge ? (B / 2) * zFr / zw : B / 2;+ A.shade(0.34, () => {+ A.face([[hB, yT, zBk], [-hB, yT, zBk], [-hB, yT2 + 0.5, zBk], [hB, yT2 + 0.5, zBk]]);+ A.face([[-hB, yT + 0.02, zBk], [hB, yT + 0.02, zBk], [hF, yT + 0.02, zFr], [-hF, yT + 0.02, zFr]]);+ A.face([[-hF, yT2 + 0.2, zFr], [hF, yT2 + 0.2, zFr], [hB, yT2 + 0.2, zBk], [-hB, yT2 + 0.2, zBk]]);+ });+ // ---- Clerestory wall, window hole, tracery ----+ const cw = o.cw || WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = o.cRise || WIN.cRise;+ const yE = P.eaves;+ A.at(M.T(0, 0, zw), () => A.slab([[-B / 2, yT2], [B / 2, yT2], [B / 2, yE], [-B / 2, yE]], [archOutline(cw, cSill, cSp, cRise, 9)], -0.42, 0.42, { noOuterSides: true, sr: 0.66 }));+ F.at(M.T(0, 0, zw), () => {+ if (o.lights === 1) addLancetTracery(F, cw, cSill, cSp, cRise);+ else addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.44 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 9).slice(0), rollSection(0.14, 0.86, 1, 0.03, 2), { closed: true, s: 0.92, crease: 40 });+ });+ // Exterior: hood mould, wimperg gable, cornice, pierced parapet, corbel table+ set.D.at(M.T(0, 0, zw + 0.42), () => { addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.02, 3.6); addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.03, 0.9, 'soft'); });+ F.at(M.T(0, 0, zw + 0.42), () => {+ F.sweep(pointedArc(cw + 0.5, cRise + 0.3, 6).map((p) => [p[0], p[1] + cSp]), hexBar(0.22, 0.26), {});+ addGable(F, Math.min(cw + 0.9, B * 0.96), 3.4, 0.02, 0.2, cSp + cRise * 0.52, { size: 0.44, finial: 0.8, lod: 0, crockets: 3 });+ F.sweep([[-B / 2, yT2 - 0.02], [B / 2, yT2 - 0.02]], stringSection(0.3, 0.2), {});+ F.sweep([[-B / 2, yE - 0.12], [B / 2, yE - 0.12]], stringSection(0.34, 0.38), {});+ const nCorb = Math.round(B / 1.1);+ for (let i = 0; i < nCorb; i++) { const x = -B / 2 + (i + 0.5) * B / nCorb; F.box(x - 0.13, x + 0.13, yE - 0.62, yE - 0.32, 0, 0.26, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zw + 0.3), () => addParapet(F, -B / 2, B / 2, yE, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Upper wall top closing the gutter+ A.face([[-B / 2, yE, zw + 0.42], [B / 2, yE, zw + 0.42], [B / 2, yE, zw - 0.42], [-B / 2, yE, zw - 0.42]], 0.7);+ }+ if (o.part !== 'upper') {+ // ---- Aisle roof (lean-to in lead) ----+ if (!o.noAisleRoof) {+ const z0 = zn + 0.42, z1 = zaw + 1.05, y0r = P.aisleRoofHigh, y1r = P.aisleWallTop - 0.2;+ L.face([[-B / 2, y0r, z0], [-B / 2, y1r, z1], [B / 2, y1r, z1], [B / 2, y0r, z0]]);+ }+ if (!o.open) {+ // ---- Aisle wall ----+ const wa = WIN.aW, sill = WIN.aSill, aSp = WIN.aSp, aRise = WIN.aRise, zOut = zaw + 0.45;+ A.at(M.T(0, 0, zOut), () => A.slab([[-B / 2, 0], [B / 2, 0], [B / 2, P.aisleWallTop], [-B / 2, P.aisleWallTop]], [archOutline(wa, sill, aSp, aRise, 9)], -0.45, 0.45, { noOuterSides: true, sr: 0.7 }));+ F.at(M.T(0, 0, zOut), () => {+ addTracery(F, wa, sill, aSp, aRise, { bar: 0.25, depth: 0.42 });+ F.sweep(archOutline(wa, sill, aSp, aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ });+ // Exterior of the aisle wall+ set.D.at(M.T(0, 0, zaw + 0.9), () => {+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.02, 4.4);+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.02, 2.6, 'drip');+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.03, 0.7, 'soft');+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.03, 0.9, 'soft');+ addStain(set.D, -B / 2, B / 2, 5.2, 0.03, 5.2, 'ground');+ });+ F.at(M.T(0, 0, zaw + 0.9), () => {+ F.sweep([[-B / 2, sill - 0.2], [B / 2, sill - 0.2]], stringSection(0.34, 0.3), {});+ F.sweep(pointedArc(wa + 0.46, aRise + 0.28, 6).map((p) => [p[0], p[1] + aSp]), hexBar(0.22, 0.26), {});+ addGable(F, wa + 0.8, 3.1, 0.02, 0.2, aSp + 2.95, { size: 0.44, finial: 0.8, crockets: 4 });+ F.sweep([[-B / 2, P.aisleWallTop - 0.12], [B / 2, P.aisleWallTop - 0.12]], stringSection(0.32, 0.34), {});+ // Plinth with a chamfered weathering+ F.sweep([[-B / 2, 0.55], [B / 2, 0.55]], stringSection(0.4, 0.42), {});+ F.sweep([[-B / 2, 1.3], [B / 2, 1.3]], stringSection(0.2, 0.2), {});+ addBlindArcade(F, -B / 2 + 0.9, B / 2 - 0.9, 1.45, 2.65, 4, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(0, 0, zaw + 0.72), () => addParapet(F, -B / 2, B / 2, P.aisleWallTop, 1.0, 0.24, { pitch: 1.3 }));+ A.face([[-B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw], [-B / 2, P.aisleWallTop, zaw]], 0.7);+ // Interior dado: blind arcade with a niche for a statue+ F.at(M.of(M.T(0, 0, zaw), M.Ry(180)), () => {+ F.sweep([[-B / 2, sill - 0.14], [B / 2, sill - 0.14]], stringSection(0.26, 0.22), {});+ addBlindArcade(F, -B / 2 + 0.5, -1.05, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ addBlindArcade(F, 1.05, B / 2 - 0.5, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ F.sweep([[-B / 2, 0.42], [B / 2, 0.42]], stringSection(0.4, 0.34), {});+ // Niche canopy over the statue+ addNicheFrame(F, 0, 0.6, 3.9, 1.05);+ });+ }+ // ---- Aisle vault ----+ if (!o.noAisleVault) {+ const z0 = zn + thick / 2, z1 = zaw;+ const crown = addRibVault(F, A, [[-B / 2, z0], [B / 2, z0], [B / 2, z1], [-B / 2, z1]], sp, P.aisleCrown - 0.3, { sideRise: (k) => (k % 2 === 0 ? (k === 0 ? archRise + 0.1 : 5.0) : 5.2), nS: 6, nT: 4, ribSec: rollSection(0.26, 0.34, 2, 0.03, 2) });+ addBoss(G, F, crown, 0.32);+ }+ }+ return set;+}+// Canopied niche frame: moulded opening, gablet with crockets. Statue placed separately.+function addNicheFrame(F, x, y0, yTop, w) {+ F.at(M.T(x, 0, 0), () => {+ F.shade(0.5, () => F.face([[-w / 2, y0, -0.005], [w / 2, y0, -0.005], [w / 2, yTop - 0.75, -0.005], [0, yTop - 0.1, -0.005], [-w / 2, yTop - 0.75, -0.005]]));+ F.box(-w / 2 - 0.05, w / 2 + 0.05, y0 - 0.14, y0 + 0.08, 0, 0.5, { s: 0.92 });+ F.sweep(archOutline(w, y0, yTop - 0.82, 0.72, 5).slice(2), barSection(0.11, 0.2), { z: 0.08 });+ for (const s of [-1, 1]) { F.at(M.T(s * w / 2, 0, 0.1), () => F.lathe([[0.07, y0 + 0.05], [0.05, y0 + 0.12], [0.05, yTop - 0.9], [0.09, yTop - 0.82], [0.05, yTop - 0.78]], 6)); }+ addGable(F, w + 0.24, 0.92, 0.06, 0.1, yTop - 0.98, { size: 0.19, crockets: 2, finial: 0.32, solid: true, lod: 0 });+ });+}++// Clustered nave pier with the vault shafts rising to the springing, on +Z = nave... shafts toward -Z+function buildPier() {+ const set = mkBatches(), { F, A, G } = set;+ const core = 0.8, big = 0.25, small = 0.14;+ const shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.1) * Math.cos(a), (core + 0.1) * Math.sin(a), big]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; shafts.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 48);+ SHARED.pierRing = ring; // the plan of the shaft, which also carries the window light on its north-arcade faces+ const yB = 1.15, yC = P.pierTop - 1.05;+ // moulded base: octagonal plinth, roll, scotia, then the lobed shaft+ F.lathe([[1.7, 0], [1.7, 0.56], [1.58, 0.62]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.62, 0.86, { k0: 1.24, k1: 1.27 });+ addExtrudedRing(F, ring, 0.86, yB, { k0: 1.27, k1: 1.0, s0: 0.75, s1: 0.72 });+ addExtrudedRing(F, ring, yB, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.05, 1.2, { leaves: 14 });+ // The vault shafts rising from the capital up the nave wall (toward -Z)+ const upper = [[0, -(core + 0.1), big], [-0.5, -(core - 0.04), small], [0.5, -(core - 0.04), small]];+ for (const u of upper) {+ F.at(M.T(u[0], 0, u[1] + 0.44), () => {+ const r = u[2], yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, P.triforiumSill - 0.1], [r * 1.5, P.triforiumSill + 0.06], [r, P.triforiumSill + 0.22], [r, yS - 0.85], [r * 1.5, yS - 0.8], [r * 1.14, yS - 0.7], [r * 2.2, yS]], 8);+ for (let i = 0; i < 4; i++) addLeaf(F, M.of(M.T(0, yS - 0.72, 0), M.Ry(i * 90 + 45), M.T(r * 1.1, 0, 0)), 0.56, { coarse: 1 });+ });+ }+ // Statue stage on the nave face: corbel below, canopy above (the figure is placed separately)+ const zF = -(core + 0.34);+ F.at(M.T(0, 0, zF), () => {+ F.lathe([[0.1, 5.0], [0.28, 5.25], [0.44, 5.6], [0.5, 5.72], [0.44, 5.78]], 8, { s: 0.85 });+ for (let i = 0; i < 6; i++) addLeaf(F, M.of(M.T(0, 5.04, 0), M.Ry(i * 60), M.T(0.14, 0, 0)), 0.66, { coarse: 1 });+ // Canopy over the statue: three arched gablets and a little spire+ F.at(M.T(0, 8.25, 0), () => {+ F.lathe([[0.5, -0.02], [0.54, 0.06], [0.42, 0.12], [0.3, 0.6], [0.02, 2.0]], 6, { faceted: true, phase: PI / 6 });+ for (let i = 0; i < 3; i++) F.at(M.of(M.Ry(-60 + i * 60 + 180), M.T(0, 0, 0.44)), () => addGable(F, 0.62, 0.62, -0.03, 0.06, 0.02, { size: 0.15, crockets: 1, finial: 0.2, solid: true, lod: 0 }));+ });+ });+ return set;+}+// The hemicycle's pier: the nave pier's family, slimmer, with one vault shaft running up to the springing+function buildApsePier() {+ const set = mkBatches(), { F } = set, core = 0.5, big = 0.15, small = 0.09, shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.06) * Math.cos(a), (core + 0.06) * Math.sin(a), big]); }+ for (let i = 0; i < 4; i++) { const a = PI / 4 + i * PI / 2; shafts.push([(core + 0.01) * Math.cos(a), (core + 0.01) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 32), yB = 0.86, yC = P.pierTop - 0.82;+ F.lathe([[1.02, 0], [1.02, 0.46], [0.94, 0.52]], 8, { faceted: true, phase: PI / 8 });+ addExtrudedRing(F, ring, 0.52, yB, { k0: 1.26, k1: 1.0 });+ addExtrudedRing(F, ring, yB, yC, {});+ addCapital(F, ring, yC, 0.82, 1.22, { leaves: 10 });+ F.at(M.T(0, 0, -(core + 0.06) + 0.24), () => {+ const r = big, yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, yS - 0.5], [r * 1.5, yS - 0.42], [r * 2.1, yS]], 8);+ });+ return set;+}+// Flying buttress unit at a bay boundary: massive pier, two arched flyers, tabernacle and pinnacles+const BUTTRESS = { hw: 0.98, zB: 19.2, zStack: 15.3, zT: 16.3 };+const flyerRise = (yA, yB, t) => yA + (yB - yA) * lerp(t, Math.sin(t * PI / 2), 0.6);+function flyerShape(zA, yA, zB, yB, thick) { // A at the pier (outer), B at the wall (inner); returns polygon points in (z,y)+ const N = 12, under = [], top = [];+ for (let i = 0; i <= N; i++) {+ const t = i / N, z = lerp(zA, zB, t), y = flyerRise(yA, yB, t);+ under.push([z, y]);+ }+ // The coping is a straight line clear above the arch: deep over the pier, close over the wall, the lights open between+ const topA = [zA - 0.05, yA + 2.5], topB = [zB, yB + 0.72];+ return { under, topA, topB };+}+function buildButtress() {+ const set = mkBatches(), { A, F, G } = set;+ const { hw, zB, zStack, zT } = BUTTRESS, z0 = zaw + 0.9, yStack = 30.6;+ // A solid pier tied into the aisle wall: three stages narrowing and stepping back under sloped caps, each with a real+ // recessed panel, then a slimmer stack at the rear that rises above the aisle roof to take the flyers+ const lower = { k: 0.56, b: 1.5, t: 1.6, r: 0.8 }, flank = { k: 0.56, b: 1.5, t: 1.5, r: 1.1 };+ A.at(M.T(0, 0, z0), () => F.at(M.T(0, 0, z0), () => addPier(set, [+ { y0: 0, y1: 8.4, w: hw, f: zB - z0, panel: lower, sidePanel: flank },+ { y0: 9.6, y1: 15.8, w: hw - 0.08, f: zB - z0 - 0.9, sidePanel: flank },+ { y0: 17, y1: 23.4, w: hw - 0.14, f: zT + 1 - z0, noDrip: true },+ { y0: 23.4, y1: yStack, w: hw - 0.24, f: zStack - z0, backFace: true },+ ], { capH: 1.1 })));+ // Moulded plinth around the base: a closed block the pier rises out of+ F.at(M.T(0, 0, z0), () => { F.box(-hw - 0.16, hw + 0.16, 0, 0.52, -0.2, zB - z0 + 0.16, { noBot: true, s: 0.9 }); F.box(-hw - 0.08, hw + 0.08, 0.52, 0.86, -0.2, zB - z0 + 0.08, { noBot: true, noTop: true, taper: 0.06, taperZ: 0.06 }); });+ set.D.at(M.T(0, 0, zB), () => { addStain(set.D, -hw, hw, 8.2, 0.02, 3.2); addStain(set.D, -hw, hw, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -hw, hw, 5, 0.03, 4.4, 'ground'); });+ // Rain and shadow down both flanks of the pier, below each cap+ for (const s of [-1, 1]) {+ const d1 = zB - z0, d2 = d1 - 0.9;+ set.D.at(M.of(M.T(s * hw, 0, z0 + d1 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.02, 3.2); addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -d1 / 2, d1 / 2, 5, 0.03, 4.8, 'ground'); });+ set.D.at(M.of(M.T(s * (hw - 0.08), 0, z0 + d2 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.02, 2.8); addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.03, 0.7, 'soft'); });+ }+ F.at(M.T(0, 0, zB), () => addGable(F, hw * 1.7, 1.4, 0.02, 0.14, 8.0, { size: 0.3, crockets: 2, finial: 0.5, solid: true, lod: 0 }));+ // Tabernacle of colonnettes at the top of the pier holding an angel+ const yT0 = 23.4, yT1 = 28.4;+ F.box(-hw + 0.1, hw - 0.1, yT0 - 0.02, yT0 + 0.1, zStack - 0.02, zT + 1, { s: 0.95 });+ for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) F.at(M.T(sx * 0.72, 0, zT + sz * 0.72), () => F.lathe([[0.2, yT0], [0.14, yT0 + 0.12], [0.1, yT0 + 0.2], [0.1, yT1 - 0.22], [0.14, yT1 - 0.15], [0.22, yT1]], 8));+ // Canopy: arched, gabled and crowned by the crocketed pinnacle+ for (let i = 0; i < 4; i++) F.at(M.of(M.T(0, 0, zT), M.Ry(i * 90), M.T(0, 0, 0.72)), () => {+ F.sweep(pointedArc(1.3, 1.25, 6).map((p) => [p[0], yT1 - 1.25 + p[1] - 0.02]), traceryBar(0.1, 0.16), {});+ addGable(F, 1.7, 1.25, -0.06, 0.12, yT1, { size: 0.28, crockets: 2, finial: 0.48, lod: 0, solid: true });+ });+ F.box(-0.92, 0.92, yT1 - 0.06, yT1 + 0.18, zT - 0.92, zT + 0.92, { s: 0.9 });+ F.at(M.T(0, yT1 + 0.18, zT), () => addPinnacle(F, 1.42, 8.4, { lod: 1 }));+ // Two flyers, each a true arch: a moulded rib springing from the pier, a thin plate pierced by a row of+ // pointed lights standing on it, and a straight sloped coping with a gabled top shedding the rain+ const fly = (zA, yA, zB, yB, th) => {+ const { under, topA, topB } = flyerShape(zA, yA, zB, yB, th);+ const band = 0.34, upper = under.map((p) => [p[0], p[1] + band]);+ const onTop = (z) => lerp(topA[1], topB[1], (z - topA[0]) / (topB[0] - topA[0]));+ const onRib = (t) => flyerRise(yA, yB, t) + band;+ const holes = [], N = 6;+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.16) / N, t1 = (i + 0.84) / N, z0 = lerp(zA, zB, t0), z1 = lerp(zA, zB, t1), u0 = onRib(t0) + 0.04, u1 = onRib(t1) + 0.04, a0 = onTop(z0) - 0.1, a1 = onTop(z1) - 0.1, h = Math.min(a0 - u0, a1 - u1);+ if (h < 0.22) continue;+ const r = Math.min(h * 0.42, Math.abs(z1 - z0) * 0.8), hole = [[z0, u0], [z1, u1], [z1, a1 - r], [(z0 + z1) / 2, (a0 + a1) / 2], [z0, a0 - r]];+ holes.push(area2(hole) > 0 ? hole : hole.slice().reverse());+ }+ F.at(M.Ry(-90), () => {+ F.slab([...upper, topB, topA], holes, -0.12, 0.12, { sr: 0.7, so: 0.8 });+ F.slab([...under, ...upper.slice().reverse()], [], -0.27, 0.27, {});+ F.sweep(under, rollSection(0.12, 0.34, 1, 0.03, 2), { caps: false });+ });+ const ye = -0.03, yr = 0.17, we = 0.33, a = [topA[1], topA[0]], c = [topB[1], topB[0]];+ F.face([[-we, a[0] + ye, a[1]], [0, a[0] + yr, a[1]], [0, c[0] + yr, c[1]], [-we, c[0] + ye, c[1]]]);+ F.face([[0, a[0] + yr, a[1]], [we, a[0] + ye, a[1]], [we, c[0] + ye, c[1]], [0, c[0] + yr, c[1]]]);+ F.face([[-we, c[0] + ye, c[1]], [we, c[0] + ye, c[1]], [we, a[0] + ye, a[1]], [-we, a[0] + ye, a[1]]]);+ const n = Math.round(Math.hypot(topA[0] - topB[0], topA[1] - topB[1]) / 1.4);+ crocketsAlong(F, [0, topA[1] + 0.14, topA[0]], [0, topB[1] + 0.14, topB[0]], n, 0.5, [0, 1, 0], 0);+ };+ fly(zStack - 0.1, 28.0, zn + 1.12, 35.6, 0.48);+ fly(zStack - 0.1, 22.0, zn + 1.12, 28.6, 0.54);+ // Upper pier on the clerestory wall where the flyers land, with its own pinnacle above the parapet+ const yE = P.eaves;+ A.at(M.T(0, 0, zn + 0.42), () => F.at(M.T(0, 0, zn + 0.42), () => addPier(set, [{ y0: P.aisleRoofHigh - 1, y1: yE + 1.2, w: 0.62, f: 0.72, noDrip: true }], { back: 0, capH: 0.75 })));+ F.at(M.T(0, yE + 1.2, zn + 0.74), () => addPinnacle(F, 0.9, 6.4, { simple: 1 }));+ F.at(M.T(0, 0, zn + 1.14), () => { F.lathe([[0.16, 31.2], [0.1, 31.4], [0.1, 40.5], [0.17, 40.8]], 8); });+ // Interior respond on the aisle wall: a small cluster carrying the aisle vault+ const ringR = lobedRing(0.32, [[0, -0.44, 0.17], [-0.34, -0.26, 0.11], [0.34, -0.26, 0.11]], 26);+ F.at(M.T(0, 0, zaw), () => {+ addExtrudedRing(F, ringR, 0.4, P.pierTop - 0.8, { s0: 0.8 });+ addCapital(F, ringR, P.pierTop - 0.8, 0.8, 1.2, { leaves: 6 });+ F.lathe([[0.62, 0], [0.62, 0.42], [0.42, 0.5]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ });+ // Spout from the aisle gutter+ return set;+}++function addLancetTracery(b, w, y0, ys, rise) { // single-light frame with a cusped head+ const secT = traceryBar(0.13, 0.3), cr = w * 0.32, cy = ys + rise * 0.34, cusp = [];+ for (let i = 0; i <= 12; i++) { const a = rad(-30 + i * 240 / 12), R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, {});+ b.sweep([[w / 2, cy - cr * 0.4], cusp[0]], secT, {});+ b.sweep([[-w / 2, cy - cr * 0.4], cusp[12]], secT, {});+}+function makeLancetGlass(w, y0, ys, rise, seed, fig, o) {+ const g = new Glass(seed);+ g.raw(archOutline(w, y0, ys, rise, 8), 'lead', -0.4, 0);+ gLancetSaint(g, 0, w, y0, ys, rise, fig, o || {});+ return g.b;+}++// ==================================================================================+// PLAN: everything is derived from the parameters at the top of the file.+// ==================================================================================+const LAY = (() => {+ const xW = P.xWest, xTowerE = xW - P.towerDepth, xNaveE = xTowerE - P.naveBays * BAY, xc = xNaveE - zn, xChoirW = xc - zn, xa = xChoirW - P.choirBays * BAY;+ const ridge = P.eaves + (zn + 1) * P.roofPitch;+ return { xW, xTowerE, xNaveE, xc, xChoirW, xa, ridge };+})();+// A rigid module frame: rotation about Y at an origin on the ground+const frame = (th, ox, oz) => {+ const c = Math.cos(rad(th)), s = Math.sin(rad(th));+ return { th, ox, oz, pos: (x, y, z) => [ox + x * c + z * s, y, oz - x * s + z * c], rot: (d = 0) => [0, th + d, 0] };+};+const SAINTS = ['apostle', 'peter', 'paul', 'bishop', 'prophet', 'martyr', 'paul', 'apostle', 'bishop', 'prophet'];++// Baked floor light: dim towards the walls and the corners++function buildVaultNave() {+ const s = mkBatches();+ const crown = addRibVault(s.F, s.A, [[-BAY / 2, -zn + 0.42], [BAY / 2, -zn + 0.42], [BAY / 2, zn - 0.42], [-BAY / 2, zn - 0.42]], P.vaultSpring, P.vaultCrown, { sideRise: (k) => (P.vaultCrown - P.vaultSpring) * (k % 2 === 0 ? 0.86 : 0.99), nS: 10, nT: 8 });+ addBoss(s.G, s.F, crown, 0.55);+ return s;+}++// ---- Chevet: hemicycle, ambulatory and a radiating chapel per sector ----------------+const CHEV = (() => {+ const d = 180 / P.apseSides, h = rad(d / 2), Ra = zn * Math.cos(h), hs = zn * Math.sin(h), Ro = zaw * Math.cos(h), hso = zaw * Math.sin(h);+ const Rc = 2.5, zc = Ro + 3.0;+ return { d, h, Ra, hs, Ro, hso, Rc, zc };+})();+function buildChevetSector() {+ const set = mkBatches(), { A, F, L, G } = set, { Ra, hs, Ro, hso, Rc, zc } = CHEV;+ buildNaveBay({ into: set, part: 'upper', B: 2 * hs, zw: Ra, wedge: true, ww: 1.62, archRise: 5.0, nT: 2, tw: 1.05, cw: 2.05, cRise: 4.3, lights: 1 });+ // Hemicycle vault: one web from this side up to the crown over the apse centre+ const yS = P.vaultSpring, yC = P.vaultCrown;+ addVaultCell(F, [-hs + 0.0, Ra - 0.42], [hs, Ra - 0.42], [0, 0], yS, yC, 9.6, 7, 8);+ const sec = rollSection(0.32, 0.42, 2, 0.04, 2);+ addRib(F, [-hs, Ra - 0.42], [hs, Ra - 0.42], yS, 9.6, sec, 6);+ addRib(F, [-zn * Math.sin(CHEV.h), (zn - 0.42) * Math.cos(CHEV.h)], [0, 0], yS, yC - yS, sec, 9, true);+ // Ambulatory vault (trapezoid)+ const c2 = addRibVault(F, A, [[-hs, Ra + 0.42], [hs, Ra + 0.42], [hso, Ro], [-hso, Ro]], P.pierTop, P.aisleCrown - 0.3, { sideRise: (k) => (k === 0 ? 5.1 : k === 2 ? 5.2 : 5.1), nS: 6, nT: 4, ribSec: rollSection(0.24, 0.32, 2, 0.03, 2) });+ addBoss(G, F, c2, 0.3);+ // Outer wall with the arched chapel mouth+ const yW = P.aisleWallTop, mw = 3.55, mS = 8.6, mR = 4.8;+ const mouth = pointedArc(mw, mR, 8).map((p) => [p[0], p[1] + mS]);+ A.at(M.T(0, 0, Ro + 0.3), () => A.slab([[-hso, 0], [-mw / 2, 0], [-mw / 2, mS], ...mouth, [mw / 2, mS], [mw / 2, 0], [hso, 0], [hso, yW], [-hso, yW]], [], -0.3, 0.3, { noOuterSides: true, sr: 0.7 }));+ // The moulding fills the whole wall thickness: it is also the soffit of the opening+ F.at(M.T(0, 0, Ro + 0.3), () => F.sweep(mouth, rollSection(0.42, 0.96, 2, 0.05, 2), {}));+ set.D.at(M.T(0, 0, Ro + 0.6), () => { addStain(set.D, -hso, hso, yW - 0.42, 0.02, 3.4); addStain(set.D, -hso, hso, yW - 0.42, 0.03, 0.8, 'soft'); });+ F.at(M.T(0, 0, Ro + 0.6), () => { F.sweep([[-hso, yW - 0.12], [hso, yW - 0.12]], stringSection(0.3, 0.3), {}); addParapet(F, -hso, hso, yW, 1.0, 0.24, { pitch: 1.1 }); });+ A.face([[-hso, yW, Ro + 0.6], [hso, yW, Ro + 0.6], [hso, yW, Ro], [-hso, yW, Ro]], 0.7);+ // Ambulatory lean-to roof+ const zR0 = Ra + 0.42, hsR0 = (zR0 / Ra) * hs, zR1 = Ro + 0.9, hsR1 = (zR1 / Ro) * hso;+ L.face([[-hsR0, P.aisleRoofHigh, zR0], [-hsR1, yW - 0.2, zR1], [hsR1, yW - 0.2, zR1], [hsR0, P.aisleRoofHigh, zR0]]);+ // Hipped roof over the hemicycle+ const yE = P.eaves + 0.15, Re = zn + 0.72, hRe = Re * Math.sin(CHEV.h), aRe = Re * Math.cos(CHEV.h);+ L.grid((u, v) => [lerp(-hRe, hRe, u) * (1 - v), lerp(yE, LAY.ridge, v), aRe * (1 - v)], 2, 2, { flip: false, metric: true });+ // Radiating chapel: five faceted walls, each with a tall lancet, rib vault, conical lead roof+ const nF = P.chapelFacets, span = 216, yC1 = 16.2, dF = span / nF, ap = Rc * Math.cos(rad(dF / 2)), halfF = Rc * Math.sin(rad(dF / 2)), lw = 1.3, sill = 3.2, lSp = 9.9, lR = 2.5;+ const verts = [];+ for (let i = 0; i <= nF; i++) { const f = rad(-span / 2 + i * dF); verts.push([Rc * Math.sin(f), zc + Rc * Math.cos(f)]); }+ for (let i = 0; i < nF; i++) {+ const fm = -span / 2 + (i + 0.5) * dF;+ const m = M.of(M.T(0, 0, zc), M.Ry(fm), M.T(0, 0, ap));+ A.at(m, () => A.slab([[-halfF, 0], [halfF, 0], [halfF, yC1], [-halfF, yC1]], [archOutline(lw, sill, lSp, lR, 7)], 0, 0.5, { noOuterSides: true, sr: 0.68 }));+ F.at(m, () => {+ addLancetTracery(F, lw, sill, lSp, lR);+ F.sweep(archOutline(lw, sill, lSp, lR, 5), hexBar(0.12, 0.56), { closed: true, z: 0.25, s: 0.9, crease: 40 });+ });+ // Weather on the chapel walls: streaks and shadow under the cornice and the sill, damp at the foot+ set.D.at(M.of(m, M.T(0, 0, 0.5)), () => {+ addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.02, 3); addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.03, 0.7, 'soft');+ addStain(set.D, -halfF, halfF, sill - 0.3, 0.02, 2, 'drip'); addStain(set.D, -halfF, halfF, 3, 0.03, 3, 'ground');+ });+ F.at(M.of(m, M.T(0, 0, 0.5)), () => {+ F.sweep([[-halfF - 0.1, sill - 0.14], [halfF + 0.1, sill - 0.14]], stringSection(0.24, 0.2), { caps: false });+ F.sweep([[-halfF - 0.1, 0.5], [halfF + 0.1, 0.5]], stringSection(0.36, 0.3), { caps: false });+ F.sweep([[-halfF - 0.1, yC1 - 0.1], [halfF + 0.1, yC1 - 0.1]], stringSection(0.3, 0.3), { caps: false });+ addGable(F, lw + 0.9, 2.4, 0.01, 0.14, lSp + 2.15, { size: 0.32, crockets: 2, finial: 0.56, lod: 0 });+ addParapet(F, -halfF, halfF, yC1, 0.82, 0.18, { pitch: 1.3, nh: 8 });+ });+ addVaultCell(F, verts[i], verts[i + 1], [0, zc], 9.2, 14.6, 3.6, 5, 4);+ addRib(F, verts[i], verts[i + 1], 9.2, 3.6, rollSection(0.16, 0.22, 1, 0.02, 2), 5);+ }+ addVaultCell(F, verts[nF], verts[0], [0, zc], 9.2, 14.6, 4.2, 5, 4);+ for (let i = 0; i <= nF; i++) {+ addRib(F, verts[i], [0, zc], 9.2, 5.4, rollSection(0.17, 0.24, 1, 0.02, 2), 6, true);+ // Exterior angle buttress and little pinnacle; interior wall shaft+ const f = -span / 2 + i * dF, ve = verts[i];+ F.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => {+ A.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => addPier(set, [{ y0: 0, y1: 8.8, w: 0.3, f: 1.05 }, { y0: 9.7, y1: 14.2, w: 0.25, f: 0.72, noDrip: true }], { back: 0.1, flat: true })); // F is already in this frame+ F.at(M.T(0, 14.18, 0.62), () => addPinnacle(F, 0.62, 3.9, { simple: 1 }));+ F.lathe([[0.17, 0], [0.09, 0.6], [0.09, 9.1], [0.17, 9.2]], 5);+ });+ }+ // The neck joining the chapel to the ambulatory: side walls, a pointed barrel vault, a lead cap+ const zN0 = Ro + 0.6, zN1 = verts[0][1], hN0 = mw / 2, hN1 = Math.abs(verts[0][0]);+ for (const s of [-1, 1]) A.prism([[s * hN0, zN0 - 0.05], [s * hN1, zN1 + 0.05], [s * (hN1 + 0.5), zN1 + 0.05], [s * (hN0 + 0.5), zN0 - 0.05]], 0, yC1);+ // Same subdivision as the chapel's last web so the seam has no cracks; a rib covers the joint+ A.grid((u, v) => { const hw = lerp(hN0, hN1, v); return [lerp(-hw, hw, u), 9.2 + arcH(2 * hw, 4.2, u), lerp(Ro + 0.3, zN1 + 0.02, v), 0.8]; }, 5, 1, { flip: false }); // starts under the arch moulding, so no ray can pass above it+ addRib(F, verts[nF], verts[0], 9.2, 4.2, rollSection(0.16, 0.26, 1, 0.02, 2), 5);+ L.face([[-(hN0 + 0.5), yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN0 - 0.1], [-(hN0 + 0.5), yC1 + 0.03, zN0 - 0.1]]);+ // Lead cone+ L.at(M.T(0, 0, zc), () => { const n = 10, rb = Rc + 0.5; L.grid((u, v) => { const a = -PI + TAU * u, r = rb * (1 - v), y = lerp(yC1 + 0.05, yC1 + 7.4, v); return [r * Math.sin(a), y, r * Math.cos(a)]; }, n, 2, { flip: false, wrapU: true }); });+ G.at(M.T(0, yC1 + 7.3, zc), () => addFinial(G, 1.25, 1));+ return set;+}++// ---- Lead roofs with metric UVs so the seams run down the slope ----------------------+function addRoofRun(L, x0, x1, half, yE, yR) {+ const slope = Math.hypot(half, yR - yE);+ for (const s of [-1, 1]) {+ const a = L.v(x0, yE, s * half, 0, half, s * (yR - yE), x0, 0, 1), b = L.v(x1, yE, s * half, 0, half, s * (yR - yE), x1, 0, 1);+ const c = L.v(x1, yR, 0, 0, half, s * (yR - yE), x1, slope, 1), d = L.v(x0, yR, 0, 0, half, s * (yR - yE), x0, slope, 1);+ if (s > 0) L.quad(a, b, c, d); else L.quad(b, a, d, c);+ // A raised roll every second seam, running from the eaves to the ridge: a low ridge of two faces standing on the sheet+ const ny = half / slope, nz = s * (yR - yE) / slope, h = 0.08, w = 0.055;+ for (let x = Math.ceil(x0 / 2) * 2; x < x1; x += 2) {+ const A0 = [x - w, yE, s * half], B0 = [x + w, yE, s * half], A1 = [x - w, yR, 0], B1 = [x + w, yR, 0], T0 = [x, yE + ny * h, s * half + nz * h], T1 = [x, yR + ny * h, nz * h];+ if (s > 0) { L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]); } else { L.face([A1, T1, T0, A0]); L.face([T1, B1, B0, T0]); }+ }+ }+}+function buildCresting(len) { // gilded fleur-de-lis cresting for a length of ridge+ const G = new Batch(TILE.gold), n = Math.round(len / 0.9);+ G.box(-len / 2, len / 2, -0.04, 0.05, -0.05, 0.05, {});+ for (let i = 0; i < n; i++) {+ const x = -len / 2 + (i + 0.5) * len / n;+ G.at(M.T(x, 0.05, 0), () => {+ G.tube([[0, 0, 0], [0, 0.34, 0], [0, 0.62, 0]], (t) => [lerp(0.05, 0.005, t), lerp(0.035, 0.005, t)], 4);+ for (const s of [-1, 1]) G.tube(spline([[0, 0.14, 0], [s * 0.15, 0.26, 0], [s * 0.2, 0.42, 0], [s * 0.1, 0.46, 0]], 1), (t) => lerp(0.035, 0.008, t), 4);+ G.box(-0.08, 0.08, 0.12, 0.17, -0.03, 0.03, {});+ });+ }+ return G;+}++// ---- Crossing: four great piers, four arches, a gilded boss, the fleche above ----------+function buildCrossingPier() {+ const set = mkBatches(), { F } = set;+ const core = 1.08, sh = [];+ for (let i = 0; i < 8; i++) { const a = i * PI / 4; sh.push([(core + 0.12) * Math.cos(a), (core + 0.12) * Math.sin(a), i % 2 ? 0.2 : 0.3]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; sh.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), 0.13]); }+ const ring = lobedRing(core, sh, 80), yC = P.vaultSpring - 1.1;+ F.lathe([[2.05, 0], [2.05, 0.6], [1.9, 0.68]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.68, 0.94, { k0: 1.2, k1: 1.23 });+ addExtrudedRing(F, ring, 0.94, 1.25, { k0: 1.23, k1: 1, s0: 0.75, s1: 0.75 });+ addExtrudedRing(F, ring, 1.25, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.1, 1.14, { leaves: 16 });+ return set;+}+function buildAislePier() {+ const set = mkBatches(), { F } = set;+ const sh = []; for (let i = 0; i < 4; i++) { const a = i * PI / 2; sh.push([0.62 * Math.cos(a), 0.62 * Math.sin(a), 0.2]); }+ const ring = lobedRing(0.54, sh, 40);+ F.lathe([[1.15, 0], [1.15, 0.5], [1.02, 0.56]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.56, 0.86, { k0: 1.28, k1: 1, s0: 0.75 });+ addExtrudedRing(F, ring, 0.86, P.pierTop - 0.9, { s0: 0.8 });+ addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });+ return set;+}+// The spire over the crossing: an oak frame sheathed in lead. A plinth straddles the roofs, two open lantern stages stand on it,+// and a long octagonal needle rises to a gilded orb and cross. Everything follows P.flecheTip, the ridge and the width of the crossing.+function buildFleche(parent, x, z) {+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold);+ L.noReduce = true; // a single careful mesh, its seams and crockets are the design+ const n = 8, PH = PI / 8, cos8 = Math.cos(PH), sin8 = Math.sin(PH), oct = { faceted: true, phase: PH };+ const yR = LAY.ridge, tip = P.flecheTip, y0 = yR - 5.9, yP = yR + 2.7, H = tip - yP, yA = yP + 0.19 * H, yB = yA + 0.15 * H, yN1 = yB + 1.7, yT = tip - 3.3;+ const R0 = zn * 0.73, R1 = zn * 0.66, R2 = zn * 0.5, RN = zn * 0.44;+ const faceFrame = (k, ap) => M.of(M.Ry(k * 45), M.T(0, 0, ap));+ const vertex = (k, R) => { const a = rad((k + 0.5) * 45); return [R * Math.sin(a), R * Math.cos(a)]; };+ // A small gilded knop finishing a gablet or a lucarne+ const knop = (m, s) => G.at(m, () => G.lathe([[0.05 * s, 0], [0.11 * s, 0.1 * s], [0.09 * s, 0.2 * s], [0.03 * s, 0.32 * s], [0.004, 0.72 * s]], 6));+ const gablet = (k, ap, w, h, y, nC, sz) => {+ const fr = faceFrame(k, ap + 0.05);+ L.at(fr, () => {+ L.slab([[-w / 2, y], [w / 2, y], [0, y + h]], [], -0.05, 0.05, { sb: 0.7 });+ L.shade(0.42, () => L.face([[-w * 0.2, y + h * 0.16, 0.06], [w * 0.2, y + h * 0.16, 0.06], [0, y + h * 0.62, 0.06]]));+ const rake = barSection(0.14, 0.2);+ L.sweep([[-w / 2 - 0.05, y - 0.02], [0, y + h + 0.04]], rake, {}); L.sweep([[0, y + h + 0.04], [w / 2 + 0.05, y - 0.02]], rake, {});+ crocketsAlong(L, [-w / 2, y + 0.05, 0], [-0.03, y + h * 0.97, 0], nC, sz, [-h, w / 2, 0], 0, addLeadCrocket);+ crocketsAlong(L, [w / 2, y + 0.05, 0], [0.03, y + h * 0.97, 0], nC, sz, [h, w / 2, 0], 0, addLeadCrocket);+ });+ knop(M.of(fr, M.T(0, y + h - 0.02, 0)), 0.9);+ };+ // One open stage: a dark core, a pointed opening in every face, a clustered post at every angle, a cornice closing the top+ const stage = (R, yBase, yTop, coreR, rPost, open) => {+ const hs = R * sin8, ap = R * cos8, { w, sill, ys, rise, tracery } = open;+ L.shade(0.34, () => L.lathe([[coreR, yBase], [coreR, yTop - 0.4]], n, oct));+ L.shade(0.5, () => { L.lathe([[coreR, yTop - 0.4], [R, yTop - 0.4]], n, oct); L.lathe([[R, yBase + 0.01], [coreR, yBase + 0.01]], n, oct); });+ for (let k = 0; k < n; k++) {+ L.at(faceFrame(k, ap), () => {+ L.slab([[-hs, yBase], [hs, yBase], [hs, yTop], [-hs, yTop]], [archOutline(w, sill, ys, rise, 5)], -0.14, 0.02, { noOuterSides: true, sr: 0.6, sb: 0.5 });+ L.sweep(pointedArc(w + 0.16, rise + 0.16, 5).map((p) => [p[0], p[1] + ys]), hexBar(0.1, 0.14), { z: 0.03, caps: false });+ if (tracery) {+ L.at(M.T(0, 0, -0.06), () => L.lathe([[0.1, sill], [0.065, sill + 0.16], [0.065, ys - 0.08], [0.11, ys]], 5));+ for (const s of [-1, 1]) L.sweep(pointedArc(w / 2 - 0.03, rise * 0.58, 3).map((p) => [s * w / 4 + p[0], ys + p[1]]), hexBar(0.08, 0.13), { z: -0.06, caps: false });+ L.sweep(ringPts(0, ys + rise * 0.76, rise * 0.13, 6), hexBar(0.06, 0.1), { z: -0.06, closed: true });+ }+ });+ const [px, pz] = vertex(k, R + 0.03);+ L.at(M.T(px, 0, pz), () => L.lathe([[rPost * 1.35, yBase], [rPost, yBase + 0.22], [rPost, yTop - 0.3], [rPost * 1.35, yTop - 0.12], [rPost * 1.2, yTop]], 6));+ }+ L.lathe([[R + 0.02, yTop - 0.5], [R + 0.22, yTop - 0.3], [R + 0.22, yTop - 0.12], [R + 0.08, yTop]], n, oct);+ };+ // A raised roll standing on a sloping face, in the face's own frame: two faces, a low ridge+ const roll = (u, ya, yb, apF, h = 0.07, w = 0.05) => {+ const za = apF(ya), zb = apF(yb), dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz) || 1, ny = -dz / l, nz = dy / l;+ const A0 = [u - w, ya, za], B0 = [u + w, ya, za], A1 = [u - w, yb, zb], B1 = [u + w, yb, zb], T0 = [u, ya + ny * h, za + nz * h], T1 = [u, yb + ny * h, zb + nz * h];+ L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]);+ };++ // ---- The plinth: batters up out of the slates of all four roofs, its foot well below their surface all round ----+ L.lathe([[R0, y0], [R0 - 0.16, yP - 0.62], [R0 + 0.12, yP - 0.46], [R0 + 0.12, yP - 0.2], [R0 - 0.2, yP], [R1 * 0.6, yP]], n, oct);+ { const apF = (y) => lerp(R0, R0 - 0.16, (y - y0) / (yP - 0.62 - y0)) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => { for (const u of [-1.2, 0, 1.2]) roll(u, y0, yP - 0.62, apF); }); }++ // ---- First stage: tall two-light openings, a gablet on every face, a pinnacle at every angle, a lead pentice behind them ----+ stage(R1, yP, yA, R1 * 0.6, 0.26, { w: R1 * 0.56, sill: yP + 0.95, ys: yA - 3.35, rise: 2, tracery: true });+ L.lathe([[R1 + 0.08, yA], [R2 + 0.02, yA + 1.55]], n, oct);+ for (let k = 0; k < n; k++) {+ gablet(k, R1 * cos8, R1 * 0.67, 2.6, yA - 0.3, 2, 0.34);+ const [px, pz] = vertex(k, R1 + 0.02);+ L.at(M.of(M.T(px, yA - 0.2, pz), M.Ry((k + 0.5) * 45)), () => addPinnacle(L, 0.62, 5, { simple: true, lod: 0 }));+ }+ // ---- Second stage: slimmer, single lancets ----+ stage(R2, yA + 1.5, yB, R2 * 0.62, 0.18, { w: R2 * 0.53, sill: yA + 2.45, ys: yB - 2.3, rise: 1.5, tracery: false });+ for (let k = 0; k < n; k++) gablet(k, R2 * cos8, R2 * 0.65, 1.9, yB - 0.25, 1, 0.3);++ // ---- The needle: a flared foot, then eight long faces. u stays metric, so the sheets run straight up and are cut by the arrises as they close ----+ const rTip = 0.09, RF = R2 + 0.08, rAt = (y) => (y <= yN1 ? lerp(RF, RN, (y - yB) / (yN1 - yB)) : lerp(RN, rTip, (y - yN1) / (yT - yN1)));+ const levels = [yB, yN1]; for (const t of [0.22, 0.5, 0.75, 1]) levels.push(lerp(yN1, yT, t));+ const apAt = (y) => rAt(y) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => {+ let vv = 0;+ for (let j = 0; j < levels.length - 1; j++) {+ const ya = levels[j], yb = levels[j + 1], za = apAt(ya), zb = apAt(yb), wa = rAt(ya) * sin8, wb = rAt(yb) * sin8, dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz), ny = -dz / l, nz = dy / l;+ const a = L.v(-wa, ya, za, 0, ny, nz, -wa, vv), b = L.v(wa, ya, za, 0, ny, nz, wa, vv), c = L.v(wb, yb, zb, 0, ny, nz, wb, vv + l), d = L.v(-wb, yb, zb, 0, ny, nz, -wb, vv + l);+ L.quad(a, b, c, d); vv += l;+ }+ // Standing seams, each stopping where the closing arrises overtake it+ for (const u of [-0.55, 0, 0.55]) { const rEnd = (Math.abs(u) + 0.1) / sin8; if (rEnd < RN) roll(u, yN1, lerp(yN1, yT, (RN - rEnd) / (RN - rTip)), apAt); }+ });+ // Rolled arrises from the flare to the tip, crockets thinning as they climb+ for (let k = 0; k < n; k++) {+ const vAt = (y) => { const [vx, vz] = vertex(k, rAt(y)); return [vx, y, vz]; };+ L.tube([vAt(yB), vAt(yN1), vAt(yT - 0.05)], (t) => lerp(0.12, 0.04, t), 4, { capEnd: false });+ const [ox, oz] = vertex(k, 1), nC = 11, y1 = yN1 + 1.2, y2 = yT - 1.5;+ for (let i = 0; i < nC; i++) {+ const t = (i + 0.5) / nC, ya = lerp(y1, y2, t - 0.02), yb = lerp(y1, y2, t + 0.02);+ crocketsAlong(L, vAt(ya), vAt(yb), 1, lerp(1.2, 0.42, t), [ox, 0.12, oz], 0, addLeadCrocket);+ }+ }+ // Tie-bands round the needle where the sheets are lapped+ for (const t of [0.22, 0.5, 0.75]) { const y = lerp(yN1, yT, t), r = rAt(y); L.lathe([[rAt(y - 0.1), y - 0.1], [r + 0.08, y - 0.05], [r + 0.08, y + 0.05], [rAt(y + 0.12), y + 0.12]], n, oct); }+ // Two tiers of lucarnes on alternate faces+ const lucarne = (k, yD, a, c, g) => {+ const zf = apAt(yD) + 0.06, yE2 = yD + c, yG = yE2 + g, zb0 = apAt(yD), zbE = apAt(yE2), zbG = apAt(yG);+ L.at(faceFrame(k, 0), () => {+ L.face([[-a, yD, zf], [a, yD, zf], [a, yE2, zf], [0, yG, zf], [-a, yE2, zf]]);+ L.shade(0.22, () => L.face([[-a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yE2, zf + 0.012], [0, yE2 + g * 0.55, zf + 0.012], [-a * 0.5, yE2, zf + 0.012]]));+ L.face([[a, yD, zb0], [a, yE2, zbE], [a, yE2, zf], [a, yD, zf]]);+ L.face([[-a, yD, zb0], [-a, yD, zf], [-a, yE2, zf], [-a, yE2, zbE]]);+ L.face([[a, yE2, zf], [a, yE2, zbE], [0, yG, zbG], [0, yG, zf]]);+ L.face([[-a, yE2, zf], [0, yG, zf], [0, yG, zbG], [-a, yE2, zbE]]);+ });+ knop(M.of(faceFrame(k, zf), M.T(0, yG - 0.02, 0)), 0.8);+ };+ for (let k = 0; k < n; k++) { if (k % 2 === 0) lucarne(k, yN1 + 5, 0.5, 1.25, 0.75); else lucarne(k, yN1 + 12, 0.36, 0.95, 0.55); }++ // ---- The tip: a gilded collar, an orb and a cross, its head at exactly P.flecheTip ----+ G.at(M.T(0, yT - 0.25, 0), () => {+ G.lathe([[0.12, 0], [0.24, 0.12], [0.22, 0.22], [0.13, 0.3], [0.11, 0.44], [0.2, 0.52], [0.36, 0.68], [0.42, 0.9], [0.36, 1.12], [0.2, 1.28], [0.08, 1.36], [0.06, 2.2]], 10);+ G.box(-0.07, 0.07, 2.2, 3.55, -0.07, 0.07, {});+ G.box(-0.62, 0.62, 2.82, 2.98, -0.07, 0.07, {});+ });+ place('CrossingSpire_Lead', L, MAT.lead, { pos: [x, 0, z], parent });+ place('CrossingSpire_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });+}++function storyWindows() { // the narrative aisle windows+ const sets = [+ [['annunciation', 'nativity', 'magi', 'shepherd', 'supper'], ['crucifixion', 'resurrection', 'david', 'noah', 'annunciation']],+ [['noah', 'david', 'annunciation', 'nativity', 'magi'], ['shepherd', 'supper', 'crucifixion', 'resurrection', 'david']],+ [['magi', 'supper', 'noah', 'crucifixion', 'shepherd'], ['nativity', 'david', 'resurrection', 'annunciation', 'supper']],+ [['resurrection', 'shepherd', 'crucifixion', 'david', 'nativity'], ['supper', 'annunciation', 'magi', 'noah', 'resurrection']],+ ];+ const grounds = [['ruby', 'cobalt'], ['cobalt', 'ruby'], ['ruby', 'cobalt'], ['cobalt', 'ruby']];+ return sets.slice(0, P.storyWindows).map((s, n) => makeWindowGlass(...windowArgs.aisle, 40 + n, {+ trackPanes: true,+ light: (g, x, w, y0, ys, r, i) => gLancetStory(g, x, w, y0, ys, r, s[i].slice(0, 4), { c1: grounds[n][0], c2: grounds[n][1], g1: grounds[(n + 1) % 4][1], g2: grounds[n][0], cell: 0.86 }),+ oculus: { ground: grounds[n][1], inner: grounds[n][0] },+ }));+}+const CLERE_SAINTS = [+ [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'book' }, { robe: 'emerald', mantle: 'gold', crown: 1, beard: 2, attr: 'cross', arm: 'point', halo: false }],+ [{ robe: 'pale', mantle: 'cobalt', veil: 'azure', arm: 'pray' }, { robe: 'violet', mantle: 'white', mitre: 'white', attr: 'staff', arm: 'up' }],+ [{ robe: 'gold', mantle: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'purple', beard: 1, attr: 'sword', arm: 'point' }],+ [{ robe: 'rubyLt', mantle: 'teal', crown: 1, attr: 'lily', arm: 'point', halo: false }, { robe: 'white', mantle: 'rubyLt', arm: 'orant', wings: 'gold' }],+];+function clereWindows() {+ return CLERE_SAINTS.slice(0, P.clerestoryWindows).map((pair, n) => makeWindowGlass(...windowArgs.clere, 60 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetSaint(g, x, w, y0, ys, r, pair[i], (i + n) % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }),+ oculus: { bust: { robe: 'rubyLt', beard: 1, cross: 1 }, ground: 'cobalt', inner: 'gold' },+ }));+}++function buildBody() {+ const { xTowerE, xNaveE, xc, xChoirW, xa, ridge } = LAY;+ const gNave = group('Nave'), gChoir = group('Choir'), gTN = group('Transept_North'), gTS = group('Transept_South'), gX = group('Crossing'), gApse = group('Apse_Chevet'), gRoof = group('Roofs');+ // The upper elevation is shared by every straight bay; aisles come in full and open-corner versions+ const bayUp = buildNaveBay({ part: 'upper' }), aisleFull = buildNaveBay({ part: 'aisle' }), aisleOpen = buildNaveBay({ part: 'aisle', open: true });+ const vault = buildVaultNave(), pier = buildPier(), but = buildButtress();+ pier.F.pierShaft = true; // the window light tests these exactly rather than voxel by voxel, so their shadows come out clean+ const stories = storyWindows(), cleres = clereWindows();+ SHARED.stories = stories; SHARED.cleres = cleres;+ let counter = 0;+ const statue = (fr, lx, y, lz, kind, faceDeg, scale, parent, indoor = true) => placeFigure('Statue_' + kind + '_' + (counter++), kind, { pos: fr.pos(lx, y, lz), rot: fr.rot(faceDeg), scale, parent, indoor, lod: 0 });+ const garg = (fr, lx, y, lz, k, sc, parent) => place('Gargoyle_' + (counter++), gargoyle(k), MAT.fine, { pos: fr.pos(lx, y, lz), rot: fr.rot(-90), scale: [sc, sc, sc], parent });++ // One side of a straight bay: module, vault-side glass, statues, gargoyles+ const placeBay = (name, fr, parent, o = {}) => {+ placeSet(name + '_Upper', bayUp, [fr.ox, 0, fr.oz], fr.th, parent);+ if (!o.noVault) placeSet(name + '_Aisle', o.open ? aisleOpen : aisleFull, [fr.ox, 0, fr.oz], fr.th, parent);+ const c = cleres[(o.i || 0) % cleres.length];+ placeGlass(name + '_Clerestory', c, fr.pos(0, 0, zn), fr.th + 180, parent);+ if (!o.open) {+ placeGlass(name + '_AisleWindow', stories[(o.i || 0) % stories.length], fr.pos(0, 0, zaw + 0.45), fr.th + 180, parent);+ if (o.sunLit) SUN_WINDOWS.push({ x: fr.ox, batch: stories[(o.i || 0) % stories.length] });+ if ((o.i || 0) % 2 === 0) statue(fr, 0, 0.7, zaw - 0.3, SAINTS[(o.i || 0) % SAINTS.length], 90, 0.86, parent);+ if (!o.inTower) garg(fr, 0, 22.5, zaw + 1.05, (o.i || 0) % 3, 0.72, parent);+ }+ if (!o.noUpperGarg) garg(fr, 0, P.eaves - 0.2, zn + 0.62, ((o.i || 0) + 1) % 3, 0.66, parent);+ };+ const placeButtress = (name, fr, parent, i) => {+ placeSet(name, but, [fr.ox, 0, fr.oz], fr.th, parent);+ statue(fr, 0, 23.5, BUTTRESS.zT, 'angel', -90, 1.9, parent, false);+ garg(fr, 0, 15.2, BUTTRESS.zB - 1.05, i % 3, 1.0, parent);+ };+ const placePier = (name, lfr, parent, kind) => {+ placeSet(name, pier, [lfr.ox, 0, lfr.oz], lfr.th, parent);+ if (kind) statue(lfr, 0, 5.78, -1.14, kind, 90, 1.15, parent);+ };++ // ---- Two more bays carry the nave between the towers to the inner face of the west wall ----+ for (let t = 0; t < 2; t++) {+ const x = xTowerE + (t + 0.5) * BAY;+ placeBay('TowerBay' + t + '_N', frame(0, x, 0), gNave, { i: t + 2, noUpperGarg: true, inTower: true });+ placeBay('TowerBay' + t + '_S', frame(180, x, 0), gNave, { i: t, noUpperGarg: true, inTower: true });+ placeSet('TowerVault' + t, vault, [x, 0, 0], 0, gNave);+ if (t === 0) { placePier('TowerPier_N', frame(0, x + BAY / 2, zn), gNave, SAINTS[6]); placePier('TowerPier_S', frame(180, x + BAY / 2, -zn), gNave, SAINTS[7]); }+ }+ // ---- Nave ----+ for (let i = 0; i < P.naveBays; i++) {+ const x = xTowerE - (i + 0.5) * BAY, isOpen = i === P.naveBays - 1;+ placeBay('NaveBay' + i + '_N', frame(0, x, 0), gNave, { i, open: isOpen });+ placeBay('NaveBay' + i + '_S', frame(180, x, 0), gNave, { i: i + 2, open: isOpen, sunLit: true });+ placeSet('NaveVault' + i, vault, [x, 0, 0], 0, gNave);+ }+ for (let k = 0; k <= P.naveBays - 1; k++) {+ const x = xTowerE - k * BAY;+ placePier('NavePier' + k + '_N', frame(0, x, zn), gNave, k ? SAINTS[k % SAINTS.length] : null);+ placePier('NavePier' + k + '_S', frame(180, x, -zn), gNave, k ? SAINTS[(k + 3) % SAINTS.length] : null);+ if (k >= 1 && k < P.naveBays - 1) { placeButtress('NaveButtress' + k + '_N', frame(0, x, 0), gNave, k); placeButtress('NaveButtress' + k + '_S', frame(180, x, 0), gNave, k + 1); }+ }+ // ---- Choir ----+ for (let j = 0; j < P.choirBays; j++) {+ const x = xChoirW - (j + 0.5) * BAY, isOpen = j === 0;+ placeBay('ChoirBay' + j + '_N', frame(0, x, 0), gChoir, { i: j + 1, open: isOpen });+ placeBay('ChoirBay' + j + '_S', frame(180, x, 0), gChoir, { i: j + 3, open: isOpen });+ placeSet('ChoirVault' + j, vault, [x, 0, 0], 0, gChoir);+ }+ for (let k = 1; k <= P.choirBays; k++) {+ const x = xChoirW - k * BAY;+ placePier('ChoirPier' + k + '_N', frame(0, x, zn), gChoir, SAINTS[(k + 5) % SAINTS.length]);+ placePier('ChoirPier' + k + '_S', frame(180, x, -zn), gChoir, SAINTS[(k + 1) % SAINTS.length]);+ placeButtress('ChoirButtress' + k + '_N', frame(0, x, 0), gChoir, k);+ placeButtress('ChoirButtress' + k + '_S', frame(180, x, 0), gChoir, k + 1);+ }+ // ---- Transept arms: the same bay, turned through 90 degrees ----+ for (const s of [1, -1]) {+ const gT = s > 0 ? gTN : gTS, tag = s > 0 ? 'N' : 'S';+ for (let q = 0; q < P.transeptBays + 1; q++) {+ const zc = s * (zn + (q + 0.5) * BAY), isOpen = q === 0;+ placeBay('Transept' + tag + q + '_W', frame(90 * s, xc, zc), gT, { i: q + 1, open: isOpen, noVault: isOpen });+ placeBay('Transept' + tag + q + '_E', frame(-90 * s, xc, zc), gT, { i: q + 3, open: isOpen, noVault: isOpen });+ placeSet('TranseptVault' + tag + q, vault, [xc, 0, zc], 90, gT);+ }+ for (let q = 1; q <= P.transeptBays; q++) { // the last bay ends in the front wall, which needs no free pier+ const zz = s * (zn + q * BAY);+ placePier('TranseptPier' + tag + q + '_W', frame(90, xc + zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 2) % SAINTS.length] : null);+ placePier('TranseptPier' + tag + q + '_E', frame(-90, xc - zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 4) % SAINTS.length] : null);+ if (q === P.transeptBays) { placeButtress('TranseptButtress' + tag + '_W', frame(90 * s, xc, zz), gT, q); placeButtress('TranseptButtress' + tag + '_E', frame(-90 * s, xc, zz), gT, q + 1); }+ }+ }+ // ---- Crossing ----+ const xPier = buildCrossingPier(), aPier = buildAislePier(), xVault = mkBatches();+ xPier.F.pierShaft = true;+ const sp = zn - 0.3;+ const crossCrown = addRibVault(xVault.F, xVault.A, [[-sp, -sp], [sp, -sp], [sp, sp], [-sp, sp]], P.vaultSpring, P.vaultCrown + 0.5, { sideRise: () => 12.9, nS: 12, nT: 10 });+ addBoss(xVault.G, xVault.F, crossCrown, 0.95);+ // Great transverse arches and the walls above them+ const arch = pointedArc(2 * (zn - 0.5), 12.9, 7);+ for (let i = 0; i < 4; i++) xVault.F.at(M.of(M.Ry(i * 90), M.T(0, P.vaultSpring, zn - 0.05)), () => xVault.F.sweep(arch, rollSection(0.9, 1.2, 2, 0.09, 3), { caps: false }));+ placeSet('CrossingVault', xVault, [xc, 0, 0], 0, gX);+ for (const [a, b] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {+ placeSet('CrossingPier', xPier, [xc + a * zn, 0, b * zn], 0, gX);+ placeSet('AngleAislePier', aPier, [xc + a * zaw, 0, b * zaw], 0, gX);+ }+ // ---- Chevet ----+ const sector = buildChevetSector(), apsePier = buildApsePier();+ const apseSaints = [{ robe: 'gold', mantle: 'cobalt', crown: 1, veil: 'pale', arm: 'pray' }, { robe: 'white', mantle: 'ruby', beard: 2, attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'emerald', beard: 1, attr: 'book' }, { robe: 'white', mantle: 'rubyLt', beard: 1, cross: 1, arm: 'up' }];+ const hemi = apseSaints.slice(0, 3).map((f, i) => makeLancetGlass(2.05, P.triforiumTop + 0.45, P.vaultSpring + 4.8, 4.3, 70 + i, f, i % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }));+ const chapelGlass = ['annunciation', 'crucifixion'].map((nm, i) => { const g = new Glass(80 + i); g.raw(archOutline(1.3, 3.2, 9.9, 2.5, 7), 'lead', -0.4, 0); gLancetStory(g, 0, 1.3, 3.2, 9.9, 2.5, [nm, ['supper', 'magi'][i], ['magi', 'resurrection'][i]], { cell: 0.6, c1: i ? 'cobalt' : 'ruby', c2: i ? 'ruby' : 'cobalt' }); return g.b; });+ for (let k = 0; k < P.apseSides; k++) {+ const th = -(k + 0.5) * CHEV.d, fr = frame(th, xa, 0), gS = group('ChevetSector' + k, gApse);+ placeSet('Sector' + k, sector, [xa, 0, 0], th, gS);+ placeGlass('HemicycleWindow' + k, hemi[(k + (k > 3 ? 1 : 0)) % hemi.length], fr.pos(0, 0, CHEV.Ra), th + 180, gS);+ const nF = P.chapelFacets, dF = 216 / nF, ap = CHEV.Rc * Math.cos(rad(dF / 2));+ for (let i = 0; i < nF; i++) {+ const fm = -108 + (i + 0.5) * dF, lx = ap * Math.sin(rad(fm)), lz = CHEV.zc + ap * Math.cos(rad(fm));+ placeGlass('ChapelWindow' + k + '_' + i, chapelGlass[(i + k) % chapelGlass.length], fr.pos(lx, 0, lz + 0.0), th + fm + 180, gS);+ }+ statue(fr, 0, 0.5, CHEV.zc + CHEV.Rc * 0.55, k === 3 ? 'virgin' : SAINTS[k], 90, 0.92, gS);+ garg(frame(th - 10, xa, 0), 0, 15.5, CHEV.zc + 1.2, k % 3, 0.7, gS);+ if (k >= 1) {+ const bt = -k * CHEV.d;+ placeButtress('ChevetButtress' + k, frame(bt, xa, 0), gS, k);+ // A slim clustered pier at each angle of the hemicycle: the arches now spring from something+ placeSet('ApsePier' + k, apsePier, frame(bt, xa, 0).pos(0, 0, zn), bt, gS);+ }+ }+ // ---- Roofs, ridge cresting, floor ----+ const Lr = new Batch(TILE.lead);+ const yE = P.eaves + 0.15, half = zn + 0.72;+ addRoofRun(Lr, xa, LAY.xW - 1.2, half, yE, ridge);+ Lr.at(M.T(xc, 0, 0), () => Lr.at(M.Ry(90), () => addRoofRun(Lr, -(zn + (P.transeptBays + 1) * BAY + 0.4), zn + (P.transeptBays + 1) * BAY + 0.4, half, yE, ridge)));+ place('MainRoof_Lead', Lr, MAT.lead, { parent: gRoof });+ const crest = buildCresting(BAY);+ const nCrest = Math.floor((LAY.xW - 2 - xa) / BAY);+ for (let i = 0; i < nCrest; i++) place('RidgeCresting' + i, crest, MAT.gold, { pos: [xa + (i + 0.5) * BAY, ridge, 0], parent: gRoof });+ for (const s of [1, -1]) for (let q = 0; q < P.transeptBays + 2; q++) { const zz = s * (0.5 + (q + 0.5) * BAY + 3); if (Math.abs(zz) < zn + (P.transeptBays + 1) * BAY) place('TranseptCresting' + (s > 0 ? 'N' : 'S') + q, crest, MAT.gold, { pos: [xc, ridge, zz], rot: [0, 90, 0], parent: gRoof }); }+ buildFleche(gRoof, xc, 0);+ // The church floor sits just above the ground outside, so the two can never fight+ const fl = new Batch(TILE.pave), fy = 0.06;+ const R13 = zaw + 0.1;+ fl.face([[xa, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, -R13], [xa, fy, -R13]]);+ const tz = zn + (P.transeptBays + 1) * BAY + 1;+ fl.face([[xc - R13, fy, tz], [xc + R13, fy, tz], [xc + R13, fy, -tz], [xc - R13, fy, -tz]]);+ for (let k = 0; k < P.apseSides; k++) { const th = rad(-(k + 0.5) * CHEV.d), f = frame(-(k + 0.5) * CHEV.d, xa, 0), rr = CHEV.zc + CHEV.Rc + 0.2, hh = rr * Math.tan(CHEV.h); fl.face([f.pos(0, fy, 0), f.pos(-hh, fy, rr), f.pos(hh, fy, rr)]); }+ place('Paving', fl, MAT.pave, { parent: ROOT });+}++// ==================================================================================+// THE GREAT ROSE: real stone tracery, and glass depicting Christ among apostles and angels+// ==================================================================================+const ROSE = { r0: 1.15, rs1: 2.35, r1: 3.05, rs2: 4.05, r2: 4.72, n1: 12, n2: 24 };+const pol = (r, a) => [r * Math.cos(a), r * Math.sin(a)];+function addRoseTracery(F, R) {+ const k = R / 5.5, heavy = traceryBar(0.3 * k, 0.5), mid = hexBar(0.2 * k, 0.42), thin = hexBar(0.14 * k, 0.34);+ const dia = [[0.07 * k, 0], [0, 0.17], [-0.07 * k, 0], [0, -0.17]]; // lightest bar, for the little roundels+ const { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE;+ F.sweep(ringPts(0, 0, R, 36), heavy, { closed: true });+ F.sweep(ringPts(0, 0, r0 * k, 14), mid, { closed: true });+ F.sweep(ringPts(0, 0, r2 * k, 36), mid, { closed: true });+ F.sweep(ringPts(0, 0, r1 * k, 24), mid, { closed: true });+ // Inner wheel: colonnette spokes, each light closed by a pointed trefoil head+ const head = (n, rs, re, a, bar) => {+ const da = PI / n * 0.98, pts = [], N = 6;+ for (let i = 0; i <= N; i++) { const t = -1 + 2 * i / N, h = Math.pow(1 - Math.pow(Math.abs(t), 1.5), 0.72); pts.push(pol((rs + (re - rs) * h) * k, a + t * da)); }+ F.sweep(pts, bar, { caps: false });+ return pts;+ };+ for (let i = 0; i < n1; i++) {+ const a = i * TAU / n1, am = a + PI / n1;+ F.sweep([pol(r0 * k, a), pol(rs1 * k, a)], mid, { caps: false });+ head(n1, rs1, r1 * 0.985, am, thin);+ // the cusp hanging inside the head+ const c = pol((rs1 + 0.34) * k, am); F.sweep(ringPts(c[0], c[1], 0.26 * k, 6), dia, { closed: true, s: 0.95, crease: 80 });+ }+ // Outer wheel of twenty-four lights+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, am = a + PI / n2;+ F.sweep([pol(r1 * k, a), pol(rs2 * k, a)], thin, { caps: false });+ head(n2, rs2, r2 * 0.99, am, thin);+ // Quatrefoil roundels in the outer band+ const c = pol((R / k + r2) / 2 * k, a);+ F.sweep(ringPts(c[0], c[1], 0.3 * k, 6), dia, { closed: true, crease: 80 });+ }+}+const SHARED = {};+// Rose tracery is one mesh, scaled for the smaller transept roses; bright outside, dim inside+function roseTracery() {+ if (!SHARED.rose) { const b = new Batch(TILE.fine); addRoseTracery(b, WF.roseR); SHARED.rose = b; }+ return SHARED.rose;+}+function largePinnacle() {+ if (!SHARED.pin) { const b = new Batch(TILE.fine); addPinnacle(b, 2, 10, { lod: 1 }); SHARED.pin = b; }+ return SHARED.pin;+}+function makeRoseGlass(R, seed, o = {}) {+ const g = new Glass(seed), k = R / 5.5, { r0, r1, r2, n1, n2 } = ROSE;+ g.raw(ringPts(0, 0, R + 0.08, 48), 'lead', -0.4, 0);+ // A light is never one flat sheet: lay the painted mosaic across it, clipped to its shape+ const ground = (pts, col, layer) => { let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of pts) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); } g.withClip(pts, () => gDiaper(g, x0, y0, x1, y1, 0, col, null, null, layer)); };+ // A roundel: a thin jewelled rim round a deep field, with everything inside it clipped to the circle+ const roundel = (cx, cy, rad_, field, draw) => {+ gRim(g, cx, cy, rad_, rad_ * 1.13, ['gold', 'ruby', 'gold', 'cobalt'], 16, 1.2);+ g.disc(cx, cy, rad_, field, 1.5, 16, { came: 0 });+ g.withClip(ringPts(cx, cy, rad_, 16), draw);+ };+ // Nothing behind the tracery is left black: the whole wheel is first laid in cobalt mosaic+ ground(ringPts(0, 0, R + 0.04, 32), 'cobalt', -0.25);+ // The outer band: alternating sheets of ruby and cobalt, a jewel in each+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, b = (i + 1) * TAU / n2;+ ground([pol(r2 * k, a), pol(R, a), pol(R, b), pol(r2 * k, b)], i % 2 ? 'ruby' : 'cobalt', 0);+ const c = pol((R / k + r2) / 2 * k, a); g.disc(c[0], c[1], 0.27 * k, i % 2 ? 'emerald' : 'ruby', 0.6, 10, { came: 0.016 }); g.disc(c[0], c[1], 0.12 * k, 'gold', 1, 8, { came: 0.01 });+ }+ // Outer lights: kings, angels and elders, upright in their roundels so the eye can rest on each face+ const busts = [{ robe: 'emerald', beard: 1, crown: 1, halo: 'gold' }, { robe: 'white', face: 'angel', halo: 'gold', wings: 1 }, { robe: 'ruby', beard: 2, halo: 'gold' }];+ for (let i = 0; i < n2; i++) {+ const am = (i + 0.5) * TAU / n2, da = PI / n2, col = i % 2 ? 'cobalt' : 'ruby', field = i % 2 ? 'ruby' : 'cobalt';+ ground([pol(r1 * k, am - da), pol(r2 * k, am - da), pol(r2 * k, am + da), pol(r1 * k, am + da)], col, 0);+ const mr = (r1 + r2) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.46 * k, B = busts[i % busts.length];+ roundel(cx, cy, rr, field, () => {+ if (B.wings) for (const s of [-1, 1]) g.poly([[cx + s * rr * 0.16, cy - rr * 0.1], [cx + s * rr * 0.98, cy + rr * 0.42], [cx + s * rr * 0.86, cy - rr * 0.5], [cx + s * rr * 0.3, cy - rr * 0.62]], 'gold', 1.8, { came: 0.012 });+ gBust(g, cx, cy - rr * 0.46, rr * 1.32, Object.assign({ layer: 2 }, B));+ });+ }+ // Inner lights: the twelve apostles enthroned, each upright in a larger roundel, colours answering round the wheel+ const scheme = o.figs || [{ robe: 'ruby', mantle: 'cobalt', beard: 1, attr: 'key', arm: 'point' }, { robe: 'emerald', mantle: 'ruby', beard: 2, attr: 'book' }, { robe: 'gold', mantle: 'cobalt', beard: 1, attr: 'sword', arm: 'point' }, { robe: 'cobalt', mantle: 'ruby', attr: 'book' }, { robe: 'purple', mantle: 'gold', beard: 1, attr: 'book' }, { robe: 'ruby', mantle: 'emerald', beard: 2, attr: 'key', arm: 'point' }];+ for (let i = 0; i < n1; i++) {+ const am = (i + 0.5) * TAU / n1, da = PI / n1, col = i % 2 ? 'ruby' : 'cobalt', field = i % 2 ? 'cobalt' : 'ruby';+ ground([pol(r0 * k, am - da), pol(r1 * k, am - da), pol(r1 * k, am + da), pol(r0 * k, am + da)], col, 0);+ const mr = (r0 + r1) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.62 * k;+ roundel(cx, cy, rr, field, () => g.withXf(0, cx, cy - rr * 0.92, 1, () => gFigure(g, 0, 0, rr * 1.86, Object.assign({ pose: 'sit', layer: 2 }, scheme[i % scheme.length]))));+ }+ // The centre: Christ in Majesty in a gilded mandorla+ g.disc(0, 0, r0 * k + 0.06, 'lead', 2.6, 16, { came: 0, jitter: 0 });+ ground(ringPts(0, 0, r0 * k, 16), o.centre || 'cobalt', 3);+ g.withClip(ringPts(0, 0, r0 * k, 14), () => g.withXf(0, 0, -0.98 * k, k, () => {+ const m = []; for (let i = 0; i < 16; i++) { const t = i * TAU / 16; m.push([0.62 * Math.cos(t) * Math.pow(Math.abs(Math.sin(t)) * 0.3 + 0.7, 1), 1 + 0.96 * Math.sin(t)]); }+ g.poly(m, 'gold', 3.3, { came: 0.02 });+ g.poly(m.map((p) => [p[0] * 0.8, 1 + (p[1] - 1) * 0.88]), 'rubyLt', 3.5, { came: 0.012 });+ gFigure(g, 0, 0.22, 1.62, o.majesty || { pose: 'sit', robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'up', attr: null, layer: 4 });+ }));+ return g.b;+}++// ==================================================================================+// WEST FRONT. Local frame: x across the facade, y up, +z out towards the viewer, z=0 the wall.+// ==================================================================================+const WF = { halfW: 21.5, topY: 47, roseY: 31.8, roseR: 5.5, kingY: 20.2, kingTop: 25.3, galY: 40.6, galTop: 46.2, wallT: 0.8, portalC: { x: 0, w0: 4.3, orders: P.jambStatues, dx: 0.58, dz: 0.62, ys: 7.5 }, portalS: { x: 13.5, w0: 3.7, orders: P.jambStatues - 1, dx: 0.6, dz: 0.66, ys: 7.2 } };+// Aged oak door leaf with forged straps, studs and a ring. Hung at x=0, spans to x=w, h high, front +z.+function addDoorLeaf(set, x0, w, h, flip) {+ const { O, I } = set, planks = 6, pw = w / planks, d = flip ? -1 : 1;+ for (let i = 0; i < planks; i++) {+ const a = x0 + d * i * pw, c = x0 + d * (i + 0.97) * pw, xa = Math.min(a, c), xc = Math.max(a, c), tone = 0.46 + 0.16 * Math.abs(Math.sin(i * 2.4 + x0 * 3));+ O.at(M.Ry(0), () => O.box(xa, xc, 0.02, h, -0.05, 0.05, { s: tone }));+ }+ // Oak grain runs up each plank: remap planes with u across and v up is the default box projection+ const strapY = [0.72, h * 0.36, h * 0.64, h - 0.7];+ for (const y of strapY) {+ const reach = w * 0.94, xEnd = x0 + d * reach;+ I.shade(0.34, () => {+ I.box(Math.min(x0, xEnd), Math.max(x0, xEnd), y - 0.075, y + 0.075, 0.05, 0.085, {});+ // A forged hinge: the strap swells and ends in a spear with two barbs, flat iron from one piece; a knuckle turns at the door's edge+ const spear = [[xEnd, y - 0.075], [xEnd + d * 0.1, y - 0.15], [xEnd + d * 0.16, y - 0.07], [xEnd + d * 0.4, y], [xEnd + d * 0.16, y + 0.07], [xEnd + d * 0.1, y + 0.15], [xEnd, y + 0.075]];+ I.slab(spear, [], 0.05, 0.085, {});+ I.at(M.T(x0 + d * 0.03, y, 0.07), () => I.lathe([[0.055, -0.12], [0.055, 0.12]], 6, {}));+ for (let k = 0; k < 4; k++) { const sx = x0 + d * (k + 0.5) * reach / 4; I.box(sx - 0.03, sx + 0.03, y - 0.03, y + 0.03, 0.08, 0.115, { taper: 0.012 }); }+ });+ }+ // Ring handle+ I.shade(0.4, () => I.at(M.of(M.T(x0 + d * (w - 0.3), h * 0.42, 0.11), M.Ry(0)), () => {+ const rg = []; for (let i = 0; i <= 12; i++) { const t = i * TAU / 12; rg.push([0.1 * Math.cos(t), 0.1 * Math.sin(t) - 0.1, 0]); }+ I.tube(rg, 0.018, 5, { capEnd: false }); I.box(-0.05, 0.05, -0.05, 0.05, -0.05, 0, {});+ }));+}+// Height of each sculpted figure at scale 1, from its feet to the top of its hair, crown or mitre+const figureTop = (kind) => ({ bishop: 2.15, king: 2.03, queen: 2.03, virgin: 2.03 }[FIG_ALIAS[kind] || kind] || 1.95);+function buildPortal(cfg, kinds, tymp) {+ const set = mkBatches(), { A, F, G, O, I } = set, { w0, orders: N, dx, dz, ys } = cfg;+ // Every order is concentric: the same two arc centres with the radius growing by one step+ const r0 = w0 * 0.86, c0 = (r0 * r0 - w0 * w0 / 4) / w0, wT = (i) => w0 + 2 * i * dx, zT = (i) => i * dz;+ const riseI = (i) => Math.sqrt(Math.pow(c0 + w0 / 2 + i * dx, 2) - c0 * c0);+ const riseOf = (w) => riseI((w - w0) / (2 * dx));+ const figs = [], zF = N * dz, outerW = wT(N), apex = ys + riseOf(outerW), lintelH = 1.2, doorH = ys - lintelH;+ const statueY = 1.5, statueK = 1.22;+ // ---- Solid stepped jambs, each order with a dado of carved blind arches ----+ for (let i = 0; i < N; i++) {+ for (const s of [-1, 1]) {+ const xa = s * wT(i) / 2, xb = s * wT(i + 1) / 2, x0 = Math.min(xa, xb), x1 = Math.max(xa, xb), zf = zT(i + 1);+ A.box(x0, x1, 0, ys, -0.1, zf, { noBot: true });+ // The dado: a plinth, a blind trefoil head on two colonnettes, a moulded table for the statues above+ F.box(x0 - 0.01, x1 + 0.01, 0, 0.26, zf, zf + 0.05, {});+ F.at(M.T((x0 + x1) / 2, 0, zf), () => {+ const w = x1 - x0 - 0.12, y0 = 0.26, ya = 0.86, r = 0.32;+ F.sweep(pointedArc(w, r, 4).map((p) => [p[0], p[1] + ya]), hexBar(0.05, 0.09), { caps: false, z: 0.03 });+ for (const t of [-1, 1]) F.box(t * w / 2 - 0.03, t * w / 2 + 0.03, y0, ya, 0, 0.06, {});+ F.sweep([[-(x1 - x0) / 2, 1.3], [(x1 - x0) / 2, 1.3]], stringSection(0.16, 0.1), { caps: false });+ });+ // Colonnette on the arris, with a foliate capital; the saint stands against it+ const cx = s * (wT(i) / 2 + 0.02), cz = zT(i) + 0.32;+ F.at(M.T(cx, 0, cz), () => {+ F.lathe([[0.2, 1.3], [0.2, 1.42], [0.13, 1.54], [0.13, ys - 0.5], [0.16, ys - 0.46], [0.13, ys - 0.42], [0.26, ys - 0.04], [0.3, ys]], 8);+ for (let l = 0; l < 4; l++) addLeaf(F, M.of(M.T(0, ys - 0.42, 0), M.Ry(l * 90 + 45), M.T(0.1, 0, 0)), 0.38, { coarse: 1 });+ });+ // The figure stands with its back to the column, on a carved base; its canopy is engaged on the same column,+ // a hand's breadth above this figure's own head+ const dirX = -s * dz, dirZ = dx * 1.5, l = Math.hypot(dirX, dirZ), th = Math.atan2(-dirZ / l, dirX / l) * 180 / PI, fx = dirX / l, fz = dirZ / l;+ const kind = kinds[(i + (s > 0 ? 0 : kinds.length >> 1)) % kinds.length], canopyY = statueY + figureTop(kind) * statueK + 0.1;+ const sx = cx + fx * 0.27, sz = cz + fz * 0.27;+ F.at(M.T(sx, 0, sz), () => {+ F.lathe([[0.3, 1.25], [0.34, 1.32], [0.24, 1.38], [0.3, 1.44], [0.33, statueY]], 8);+ for (let l = 0; l < 2; l++) addLeaf(F, M.of(M.T(0, 1.36, 0), M.Ry(l * 90 + (s > 0 ? -60 : 150)), M.T(0.24, 0, 0)), 0.2, { coarse: 1 });+ F.at(M.T(0, canopyY + 0.14, 0), () => {+ // A canopy like a tiny carved building: vaulted soffit, a drum of gablets, thin spirelet+ F.lathe([[0.05, -0.14], [0.27, -0.06], [0.33, 0], [0.33, 0.1], [0.28, 0.14], [0.17, 0.17], [0.17, 0.5], [0.22, 0.54], [0.15, 0.58], [0.07, 0.64], [0.01, 1.02]], 6, { phase: PI / 6, faceted: true });+ const face = s > 0 ? -60 : 60;+ for (const l of [-1, 0, 1]) F.at(M.of(M.Ry(face + l * 60), M.T(0, 0, 0.27)), () => {+ F.face([[-0.16, 0.11, 0.02], [0.16, 0.11, 0.02], [0, 0.42, 0.02]]);+ crocketsAlong(F, [-0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [-1, 0.6, 0], 0);+ crocketsAlong(F, [0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [1, 0.6, 0], 0);+ F.shade(0.5, () => F.face([[-0.08, 0.14, 0.03], [0.08, 0.14, 0.03], [0, 0.28, 0.03]]));+ });+ });+ });+ figs.push({ kind, pos: [cfg.x + sx, statueY, sz], rot: [0, th, 0], scale: statueK, mirror: s < 0 });+ }+ }+ // ---- Archivolts: each order a hollow, each hollow a procession of small figures, every one on a console under a canopy ----+ const ARCH_KINDS = ['apostle', 'king', 'angel', 'bishop', 'queen'], ak = 0.62;+ for (let i = 0; i < N; i++) {+ const w = wT(i), r = riseI(i), arc = pointedArc(w, r, 8).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, [[0, -0.1], [dx, -0.1], [dx, dz], [0, 0]], { z: zT(i), caps: false, crease: 30 });+ F.at(M.T(0, 0, zT(i + 1) - 0.02), () => F.sweep(pointedArc(wT(i + 1), riseI(i + 1), 8).map((p) => [p[0], p[1] + ys]), hexBar(0.2, 0.22), { caps: false }));+ const pts = pointedArc(w + dx, riseI(i + 0.5), 16).map((p) => [p[0], p[1] + ys]);+ let len = 0; const seg = []; for (let k = 0; k < pts.length - 1; k++) { const l = Math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]); seg.push(l); len += l; }+ const n = Math.max(4, Math.round(len / 1.52));+ for (let q = 0; q < n; q++) {+ const d = (q + 0.5) / n * len; let acc = 0, k = 0; while (k < seg.length - 1 && acc + seg[k] < d) { acc += seg[k]; k++; }+ const f = (d - acc) / seg[k], px = lerp(pts[k][0], pts[k + 1][0], f), py = lerp(pts[k][1], pts[k + 1][1], f);+ const tx0 = (pts[k + 1][0] - pts[k][0]) / seg[k], ty0 = (pts[k + 1][1] - pts[k][1]) / seg[k], rx = -ty0, ry = tx0;+ const up = d > len / 2 ? -1 : 1, Y = [tx0 * up, ty0 * up, 0];+ // sits on the splay, looking out towards the opening and the viewer+ const nl = Math.hypot(dz, dx), X = [-rx * dz / nl, -ry * dz / nl, dx / nl];+ const Z = [X[1] * Y[2] - X[2] * Y[1], X[2] * Y[0] - X[0] * Y[2], X[0] * Y[1] - X[1] * Y[0]];+ const pos = [px + X[0] * 0.17 - Y[0] * 0.62, py + X[1] * 0.17 - Y[1] * 0.62, zT(i) + dz * 0.5 + X[2] * 0.17];+ figs.push({ kind: ARCH_KINDS[(i + q) % ARCH_KINDS.length], lod: 0, pos: [cfg.x + pos[0], pos[1], pos[2]], basis: { X, Y, Z }, scale: ak });+ F.at(frameOf(pos, X, Y), () => {+ F.lathe([[0.03, -0.18], [0.16, -0.02], [0.18, 0.02]], 6);+ F.lathe([[0.02, 1.27], [0.16, 1.33], [0.16, 1.38], [0.02, 1.56]], 6, { faceted: true });+ });+ }+ }+ // ---- Outer hood with gable and crockets ----+ const hood = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, zF), () => F.sweep(hood, rollSection(0.34, 0.44, 2, 0.05, 2), { caps: false }));+ // The gable springs from the arch springing: plain coursed stone, its carving is real and stands proud of it+ const gw = outerW + 1.5, gy = ys, gTop = WF.kingY - 0.7, gH = gTop - gy;+ const archCut = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ const field = [[-gw / 2, gy], [-(outerW + 0.1) / 2, gy], ...archCut.slice(1, -1), [(outerW + 0.1) / 2, gy], [gw / 2, gy], [0, gTop]];+ A.at(M.T(0, 0, zF - 0.1), () => A.slab(field, [], -0.1, 0, { noOuterSides: true, noBack: true }));+ A.at(M.T(0, 0, zF - 0.2), () => A.slab(field, [], -zF + 0.3, 0, { noOuterSides: false, noFront: true, noBack: true, so: 0.7 }));+ F.at(M.T(0, 0, zF - 0.14), () => {+ addGable(F, gw, gH, 0, 0.24, gy, { size: 0.72, finial: 0.1, crockets: 6, noField: true, lod: 1 });+ const yc = apex + (gTop - apex) * 0.36, rc = Math.min(0.92, (gTop - yc) * 0.42);+ F.sweep(ringPts(0, yc, rc, 16), traceryBar(0.18, 0.24), { closed: true, z: 0.04 });+ const foil = []; for (let i = 0; i < 15; i++) { const a = PI / 2 + i * TAU / 15, R = rc * (0.56 + 0.34 * Math.pow(Math.abs(Math.cos(a * 1.5 - PI * 0.75)), 0.8)); foil.push([R * Math.cos(a), yc + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.09, 0.18), { closed: true, z: 0.04, crease: 40 });+ });+ G.at(M.T(0, gTop + 0.1, zF - 0.02), () => addFinial(G, 1.5, 1));+ // ---- Tympanum: one block of dressed stone, standing proud of the coursed wall behind it, with the scene carved out of it ----+ const tw = w0 + 0.04, tr = riseOf(w0), k = w0 / 4.3, reliefZ = 0.16, relief = 0.6;+ F.slab(archOutline(tw, ys - 0.05, ys, tr, 9), [], -0.1, reliefZ, { noBack: true, sr: 0.7 });+ // the ground the figures rise from is part of the block, a low moulded base rather than a shelf+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, ys - 0.02, ys + 0.1, -0.08, reliefZ + 0.14, {});+ // Lintel: a recessed frieze, deep enough to hold its people, cornice and sill standing forward of it+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, ys - 0.02, -0.08, -0.02, {});+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, doorH + 0.12, -0.08, 0.42, {});+ F.at(M.T(0, 0, 0.42), () => F.sweep([[-tw / 2 - 0.02, doorH + 0.06], [tw / 2 + 0.02, doorH + 0.06]], stringSection(0.12, 0.06), { caps: false }));+ const nL = Math.round(w0 / 0.37), frieze = ['apostle', 'queen', 'apostle', 'king', 'bishop', 'apostle'];+ for (let i = 0; i < nL; i++) {+ // two ranks, every other figure a step forward: they overlap and each one turns to the middle of the door+ const x = -w0 / 2 + (i + 0.5) * w0 / nL, side = x < 0 ? 1 : -1, deep = i % 2;+ figs.push({ kind: frieze[i % frieze.length], lod: 0, relief: 0.68, pos: [cfg.x + x, doorH + 0.12, deep ? 0.04 : 0.16], rot: [0, -90 + side * (16 + 10 * (i % 3)), 0], scale: 0.43 });+ }+ // The central figure enthroned in a vesica: two arcs meeting in a point above and below, a broad moulded band of stone+ // with its face gilded. Everything in the scene is cut back to the same block, and nothing crosses the frame.+ const mY = ys + 0.1, mH = tr * 0.84, mW = 0.84 * k, nM = 10;+ const vesica = (w, h, y0) => { const out = []; for (let i = 0; i < nM; i++) { const v = i / nM; out.push([w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } for (let i = 0; i < nM; i++) { const v = 1 - i / nM; out.push([-w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } return out; };+ const vo = vesica(mW, mH, mY - 0.06), vi = vesica(mW - 0.2, mH - 0.44, mY + 0.16);+ F.at(M.T(0, 0, reliefZ), () => { F.slab(vo, [vi], -0.02, 0.07, { sr: 0.78 }); F.sweep(vo, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); F.sweep(vi, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); });+ G.at(M.T(0, 0, reliefZ + 0.07), () => G.slab(vesica(mW - 0.025, mH - 0.07, mY - 0.025), [vesica(mW - 0.175, mH - 0.37, mY + 0.125)], 0, 0.05, {}));+ figs.push({ kind: tymp.main, half: 1, relief, pos: [cfg.x, mY, reliefZ + 0.02], rot: [0, -90, 0], scale: 1.0 * k });+ // Worshippers on their knees and angels crowding behind them, every head turned to the throne+ for (const s of [-1, 1]) {+ figs.push({ kind: 'kneel', half: 1, relief, pos: [cfg.x + s * 1.28 * k, mY, reliefZ + 0.12], rot: [0, -90 + s * 50, 0], scale: 0.84 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[0], half: 1, relief, pos: [cfg.x + s * 1.6 * k, mY, reliefZ + 0.01], rot: [0, -90 + s * 34, 0], scale: 0.72 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[1], half: 1, relief, lod: 0, pos: [cfg.x + s * 1.92 * k, mY, reliefZ + 0.08], rot: [0, -90 + s * 55, 0], scale: 0.44 * k, mirror: s < 0 });+ }+ // Trumeau with its statue, on a carved base, under a canopy+ A.box(-0.32, 0.32, 0, doorH, -0.1, 0.34, { noBot: true });+ const tK = tymp.trumeauScale || 1.3, tBase = 1.06, tCanopy = tBase + figureTop(tymp.trumeau) * tK + 0.12;+ F.at(M.T(0, 0, 0.5), () => {+ F.lathe([[0.42, 0], [0.42, 0.42], [0.3, 0.56], [0.25, 0.78], [0.34, 0.92], [0.36, tBase]], 8, { faceted: true, phase: PI / 8 });+ for (let l = 0; l < 6; l++) addLeaf(F, M.of(M.T(0, 0.82, 0), M.Ry(l * 60 + 15), M.T(0.27, 0, 0)), 0.24, { coarse: 1 });+ F.at(M.T(0, tCanopy + 0.2, 0), () => F.lathe([[0.06, -0.2], [0.33, -0.08], [0.42, 0], [0.42, 0.14], [0.35, 0.18], [0.22, 0.22], [0.22, 0.58], [0.28, 0.62], [0.2, 0.66], [0.09, 0.72], [0.02, 1.1]], 6, { phase: PI / 6, faceted: true }));+ });+ figs.push({ kind: tymp.trumeau, pos: [cfg.x, tBase, 0.5], rot: [0, -90, 0], scale: tK });+ // Doors: two leaves either side of the trumeau, recessed in the wall+ const leaf = w0 / 2 - 0.34;+ O.at(M.T(0, 0, -0.14), () => { const set2 = set; I.at(M.T(0, 0, -0.14), () => { addDoorLeaf(set2, w0 / 2 - 0.02, leaf, doorH - 0.04, true); addDoorLeaf(set2, -w0 / 2 + 0.02, leaf, doorH - 0.04, false); }); });+ return { set, figs, apex, zF, outerW };+}+function placePortal(gWest, name, cfg, pr, opt = {}) {+ placeSet(name, pr.set, [cfg.x, 0, 0], 0, gWest);+ const gF = group(name + '_Figures', gWest); // the people of one door, as one group of nodes+ pr.figs.forEach((f, i) => {+ const rot = f.rot || eulerFromBasis(f.basis.X, f.basis.Y, f.basis.Z);+ const pos = [f.pos[0] + (cfg.x - pr.baseX), f.pos[1], f.pos[2]];+ // Full sculpture where people stand next to it; opt.light keeps a door's overhead scene, or the whole door, to the lighter copies+ const far = opt.light === 'all' || (opt.light === 'scene' && f.pos[1] > 4);+ placeFigure(name + '_Fig' + i, f.kind, { pos, rot, scale: f.scale, parent: gF, lod: far ? 0 : f.lod, mirror: f.mirror, half: f.half, relief: f.relief });+ });+}+function eulerFromBasis(X, Y, Z) { // degrees, three.js 'XYZ' order+ const m13 = clamp(Z[0], -1, 1), y = Math.asin(m13);+ let x, z;+ if (Math.abs(m13) < 0.9999) { x = Math.atan2(-Z[1], Z[2]); z = Math.atan2(-Y[0], X[0]); } else { x = Math.atan2(Y[2], Y[1]); z = 0; }+ return [x * 180 / PI, y * 180 / PI, z * 180 / PI];+}+// One niche of the Gallery of Kings, shared along the facade+function buildKingNiche(pitch) {+ const set = mkBatches(), { F, A } = set, h = WF.kingTop - WF.kingY, y0 = WF.kingY;+ A.face([[-pitch / 2, y0, 0.02], [pitch / 2, y0, 0.02], [pitch / 2, y0 + h, 0.02], [-pitch / 2, y0 + h, 0.02]]);+ F.box(-pitch / 2, pitch / 2, y0 - 0.5, y0, 0, 1.3, { s: 0.9 });+ F.at(M.T(0, 0, 1.3), () => F.sweep([[-pitch / 2, y0 - 0.38], [pitch / 2, y0 - 0.38]], stringSection(0.3, 0.26), { caps: false }));+ // Colonnettes and a cusped arch with a gablet over each king+ const ys = y0 + 3.15, rise = 1.3;+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0.95), () => F.lathe([[0.17, y0], [0.09, y0 + 0.16], [0.09, ys - 0.2], [0.2, ys]], 8));+ F.at(M.T(0, 0, 0.95), () => {+ const arc = pointedArc(pitch - 0.1, rise, 6).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, rollSection(0.14, 0.36, 1, 0.03, 2), { caps: false });+ // solid spandrel+ A.at(M.T(0, 0, 0.95), () => A.slab([[-pitch / 2, ys], [-(pitch - 0.1) / 2, ys], ...arc, [(pitch - 0.1) / 2, ys], [pitch / 2, ys], [pitch / 2, y0 + h], [-pitch / 2, y0 + h]], [], -0.1, 0.1, { noOuterSides: true }));+ addGable(F, pitch * 0.96, 1.12, 0.1, 0.1, ys + 0.46, { size: 0.26, crockets: 2, finial: 0.34, solid: true, lod: 0 });+ });+ // The roof of the gallery behind the arcade+ F.box(-pitch / 2, pitch / 2, y0 + h - 0.02, y0 + h + 0.12, 0, 1.28, { s: 0.85 });+ return set;+}+// One bay of the open arcade above the rose and the tower windows+function buildGalleryBay(pitch) {+ const set = mkBatches(), { F, A } = set, y0 = WF.galY, y1 = WF.galTop, ys = y0 + 3.3;+ F.shade(0.4, () => F.face([[-pitch / 2, y0, 0.03], [pitch / 2, y0, 0.03], [pitch / 2, y1, 0.03], [-pitch / 2, y1, 0.03]]));+ const arc = pointedArc(pitch * 0.92, 1.5, 5).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, 0.5), () => {+ F.sweep(arc, traceryBar(0.16, 0.36), { caps: false });+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0), () => F.lathe([[0.2, y0], [0.1, y0 + 0.2], [0.1, ys - 0.22], [0.22, ys]], 8));+ F.sweep(ringPts(0, ys + 0.6, 0.26, 7), hexBar(0.07, 0.14), { closed: true });+ });+ A.at(M.T(0, 0, 0.5), () => A.slab([[-pitch / 2, ys], [-pitch * 0.46, ys], ...arc, [pitch * 0.46, ys], [pitch / 2, ys], [pitch / 2, y1], [-pitch / 2, y1]], [], -0.18, 0.18, { noOuterSides: true }));+ F.box(-pitch / 2, pitch / 2, y0 - 0.2, y0 + 0.02, 0, 0.72, { s: 0.92 });+ F.at(M.T(0, 0, 0.72), () => { F.sweep([[-pitch / 2, y0 - 0.1], [pitch / 2, y0 - 0.1]], stringSection(0.26, 0.22), { caps: false }); F.sweep([[-pitch / 2, y1], [pitch / 2, y1]], stringSection(0.3, 0.3), { caps: false }); });+ F.at(M.T(0, 0, 0.72), () => addParapet(F, -pitch / 2, pitch / 2, y1 + 0.12, 1.05, 0.2, { pitch: 1.1, nh: 8 }));+ return set;+}+// One face of a belfry stage, shared by all eight faces of the two towers+function buildBelfryFace() {+ const set = mkBatches(), { A, F, L } = set, hw = 7, zf = 7, y0 = WF.topY, yT = P.towerTop - 2.6;+ const lw = 3.05, lx = 3.3, sill = y0 + 4.2, ys = y0 + 13.6, rise = 4.1, holes = [];+ for (const s of [-1, 1]) holes.push(archOutline(lw, sill, ys, rise, 7).map((p) => [s * lx + p[0], p[1]]));+ A.at(M.T(0, 0, zf), () => A.slab([[-hw, y0], [hw, y0], [hw, yT], [-hw, yT]], holes, -0.9, 0, { noOuterSides: true, noBack: true, sr: 0.66 }));+ // The bell chamber: a floor behind the openings, and a bell hanging on each side of the frame+ F.box(-hw + 0.8, hw - 0.8, sill - 0.5, sill - 0.2, zf - 5, zf - 0.8, {});+ set.I.at(M.T(0, sill + 3.2, zf - 2.5), () => set.I.lathe([[0.06, 1.72], [0.36, 1.62], [0.5, 1.12], [0.56, 0.7], [0.84, 0.12], [0.92, 0]], 10));+ // Behind the bells the oak frame that carries them closes the view, so no sky shows through the tower+ set.O.face([[-4.2, sill - 0.3, zf - 4], [4.2, sill - 0.3, zf - 4], [4.2, yT, zf - 4], [-4.2, yT, zf - 4]], 0.16);+ for (const s of [-1, 1]) {+ F.at(M.T(s * lx, 0, zf - 0.45), () => {+ F.sweep(pointedArc(lw, rise, 6).map((p) => [p[0], p[1] + ys]), rollSection(0.2, 0.7, 1, 0.04, 2), { caps: false });+ F.sweep([[0, sill - 0.05], [0, ys + 0.1]], traceryBar(0.2, 0.42), {});+ const lw2 = (lw - 0.1) / 2;+ for (const t of [-1, 1]) F.sweep(pointedArc(lw2, lw2 * 0.9, 4).map((p) => [t * lw2 / 2 + p[0], ys + 0.1 + p[1]]), hexBar(0.14, 0.34), { caps: false });+ F.sweep(ringPts(0, ys + rise * 0.62, 0.48, 10), hexBar(0.14, 0.34), { closed: true });+ });+ // Louvres: lead-clad boards set across the opening and tilted to throw the rain outward, dark air between them+ L.at(M.T(s * lx, 0, zf - 0.45), () => {+ const slope = 0.42;+ for (let y = sill + 0.2; y < ys + rise * 0.3; y += 0.5) {+ const h = y <= ys ? lw / 2 : (lw / 2) * Math.sqrt(Math.max(0, 1 - Math.pow((y - ys) / rise, 2)));+ L.face([[-h, y, 0.3], [h, y, 0.3], [h, y + slope, -0.3], [-h, y + slope, -0.3]]);+ L.face([[h, y - 0.05, 0.3], [-h, y - 0.05, 0.3], [-h, y + slope - 0.05, -0.3], [h, y + slope - 0.05, -0.3]]);+ }+ });+ F.at(M.T(s * lx, 0, zf), () => {+ F.sweep(pointedArc(lw + 0.5, rise + 0.3, 6).map((p) => [p[0], p[1] + ys]), hexBar(0.24, 0.28), { caps: false });+ addGable(F, lw + 0.9, 3.1, 0.03, 0.18, ys + rise * 0.5, { size: 0.42, finial: 0.8, crockets: 3, lod: 0 });+ });+ }+ F.at(M.T(0, 0, zf), () => {+ F.sweep([[-hw, y0 + 0.2], [hw, y0 + 0.2]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, sill - 0.3], [hw, sill - 0.3]], stringSection(0.32, 0.28), { caps: false });+ F.sweep([[-hw, yT - 0.2], [hw, yT - 0.2]], stringSection(0.4, 0.44), { caps: false });+ for (let i = 0; i < 12; i++) { const x = -hw + (i + 0.5) * 2 * hw / 12; F.box(x - 0.14, x + 0.14, yT - 0.78, yT - 0.42, 0, 0.3, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zf - 0.2), () => addParapet(F, -hw, hw, yT, 1.2, 0.28, { pitch: 1.4, nh: 8 }));+ return set;+}+// Octagonal corner turret and its great pinnacle, reaching the full height of the tower+function buildTowerCorner() {+ const set = mkBatches(), { A, F } = set, y0 = WF.topY, yT = P.towerTop - 1.3, R = 1.72;+ // The turret grows out of the corner on a tapering corbel, closed underneath; its lower part sits inside the corner pier+ A.lathe([[0.04, y0 - 4.2], [R * 0.66, y0 - 2.6], [R, y0 - 1], [R, yT]], 8, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < 8; i++) {+ const a = i * 45 + 22.5, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ F.at(M.of(M.Ry(a), M.T(0, 0, ap)), () => {+ if (i % 2 === 0) for (const k of [0, 1]) {+ const yb = y0 + 1 + k * 11;+ F.sweep(pointedArc(side * 0.72, 0.9, 4).map((p) => [p[0], p[1] + yb + 8.2]), hexBar(0.1, 0.12), { caps: false });+ for (const s of [-1, 1]) F.sweep([[s * side * 0.36, yb], [s * side * 0.36, yb + 8.2]], hexBar(0.1, 0.12), { caps: false });+ F.shade(0.55, () => F.face([[-side * 0.36, yb, 0.01], [side * 0.36, yb, 0.01], [side * 0.36, yb + 8.2, 0.01], [0, yb + 9.05, 0.01], [-side * 0.36, yb + 8.2, 0.01]]));+ }+ });+ }+ F.lathe([[R, y0 + 12.3], [R + 0.28, y0 + 12.5], [R + 0.28, y0 + 12.66], [R, y0 + 12.82]], 8, { faceted: true, phase: PI / 8 });+ F.lathe([[R, yT - 0.2], [R + 0.36, yT], [R + 0.36, yT + 0.26], [R, yT + 0.42]], 8, { faceted: true, phase: PI / 8 });+ return set;+}+function buildWestFront() {+ const gWest = group('West_Front');+ gWest.position.set(LAY.xW, 0, 0); gWest.rotation.y = rad(90);+ const set = mkBatches(), { A, F, L, G, D } = set, hw = WF.halfW, yTop = WF.topY, tcx = 14.5;+ // Real recessed lights flanking the tower windows, cut through the face of the wall+ const towerRecess = [];+ for (const s of [-1, 1]) for (const d of [-1, 1]) towerRecess.push(archOutline(1.5, WIN.cSill + 0.6, WIN.cSp - 0.6, 1.8, 6).map((p) => [s * tcx + d * 3.55 + p[0], p[1]]));+ // ---- The great wall with real openings: doors and rose ----+ const cP = WF.portalC, sP = WF.portalS;+ const doorHole = (x, w, h) => [[x - w / 2, 0.02], [x + w / 2, 0.02], [x + w / 2, h], [x - w / 2, h]];+ const wallOutline = [[-hw, 0], [-sP.x - sP.w0 / 2, 0], [-sP.x - sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, 0], [-cP.w0 / 2, 0], [-cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, 0], [sP.x - sP.w0 / 2, 0], [sP.x - sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]];+ A.slab(wallOutline, [ringPts(0, WF.roseY, WF.roseR, 40), ...towerRecess], -WF.wallT, 0, { noOuterSides: true, sr: 0.62 });+ towerRecess.forEach((o) => addRecess(A, F, o, 0.5));+ // Seven centuries of rain under every ledge of the front, damp rising from the ground+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.02, 3.2);+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.02, 3.6);+ addStain(D, -hw, hw, WF.galY - 0.42, 0.02, 3.4);+ addStain(D, -hw, hw, WF.kingTop + 0.02, 0.02, 2.6, 'drip');+ // damp on the wall only: the door openings are left clear+ for (const span of [[-hw, -sP.x - sP.w0 / 2], [-sP.x + sP.w0 / 2, -cP.w0 / 2], [cP.w0 / 2, sP.x - sP.w0 / 2], [sP.x + sP.w0 / 2, hw]]) addStain(D, span[0], span[1], 5.2, 0.03, 5.2, 'ground');+ // Each ledge throws a soft shadow on the wall just below it; soot gathers where the rain cannot reach+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.03, 0.8, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.03, 1.2, 'soft');+ addStain(D, -hw, hw, WF.galY - 0.42, 0.03, 1.0, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.62, 0.04, 2.4, 'soot');+ for (const s of [-1, 1]) addStain(D, s * tcx - WIN.cW / 2 - 0.4, s * tcx + WIN.cW / 2 + 0.4, WIN.cSill - 0.12, 0.02, 3.4);+ // ---- Rose: moulded ring, tracery, spandrels, relieving arch ----+ place('Great_Rose_Tracery', roseTracery(), MAT.fine, { pos: [0, WF.roseY, -0.4], parent: gWest });+ F.at(M.T(0, WF.roseY, 0), () => {+ F.at(M.T(0, 0, 0), () => {+ const ring = traceryBar(0.46, 0.58);+ F.sweep(ringPts(0, 0, WF.roseR + 0.32, 40), ring, { closed: true, z: 0.14 });+ F.sweep(ringPts(0, 0, WF.roseR + 0.78, 44), rollSection(0.2, 0.3, 1, 0.04, 2), { closed: true, z: 0.1 });+ });+ });+ F.at(M.T(0, 0, 0.03), () => {+ // spandrels round the rose: plain stone, each with a real pierced roundel carved in its corner+ const R2 = WF.roseR + 0.72, ry = WF.roseY, sq = 6.6;+ for (const [sx, sy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {+ // corner of the square -> along its edge -> round the rose -> along the other edge+ const arcPts = []; for (let i = 0; i <= 8; i++) { const a = i / 8 * PI / 2; arcPts.push([sx * R2 * Math.cos(a), sy * R2 * Math.sin(a)]); }+ const poly = [[sx * sq, sy * sq], [sx * sq, 0], ...arcPts, [0, sy * sq]].map((p) => [p[0], p[1] + ry]);+ F.slab(area2(poly) > 0 ? poly : poly.slice().reverse(), [], -0.04, 0, { noOuterSides: true, noBack: true });+ const cx = sx * (sq - 1.02), cy = ry + sy * (sq - 1.02);+ F.sweep(ringPts(cx, cy, 0.76, 14), traceryBar(0.13, 0.2), { closed: true, z: 0.05 });+ const foil = []; for (let i = 0; i < 16; i++) { const a = i * TAU / 16, R = 0.68 * (0.62 + 0.32 * Math.abs(Math.cos(a * 2))); foil.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.07, 0.14), { closed: true, z: 0.05, crease: 40 });+ }+ });+ F.at(M.T(0, 0, 0.14), () => { F.sweep([[-6.6, WF.roseY - 6.6], [6.6, WF.roseY - 6.6]], stringSection(0.3, 0.26), { caps: false }); F.sweep([[-6.6, WF.roseY + 6.6], [6.6, WF.roseY + 6.6]], stringSection(0.3, 0.26), { caps: false }); });+ // ---- Tower lower stages: paired windows with hood and gable, string courses ----+ const cw = WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = WIN.cRise;+ for (const s of [-1, 1]) {+ F.at(M.T(s * tcx, 0, 0.32), () => {+ addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 8), rollSection(0.24, 0.5, 1, 0.04, 2), { closed: true, crease: 40, z: -0.14 });+ F.sweep(pointedArc(cw + 0.8, cRise + 0.4, 7).map((p) => [p[0], p[1] + cSp]), hexBar(0.26, 0.3), { z: 0.1, caps: false });+ addGable(F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 4, lod: 0 });+ });+ F.at(M.T(s * tcx, 0, 0.02), () => { F.sweep([[-6.1, WF.kingTop + 0.24], [6.1, WF.kingTop + 0.24]], stringSection(0.3, 0.22), { caps: false }); });+ }+ // ---- Buttresses of the front: one continuous pier between the portals, narrowing as it climbs through the kings and+ // the gallery; the outer corners of the towers carry a single diagonal pier instead of two that would collide ----+ const xPier = [-7.5, 7.5], lowPanel = { k: 0.6, b: 1.1, t: 7.2, r: 0.7 };+ xPier.forEach((x, i) => {+ const m = M.T(x, 0, 0);+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: 0, y1: 11.6, w: 1.05, f: 3.6, panel: lowPanel }, { y0: 12.9, y1: 18.4, w: 0.98, f: 2.7 }, { y0: 19.7, y1: 19.72, w: 0.92, f: 1.2, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 3.6), () => { F.box(-1.17, 1.17, 0, 0.74, -3.7, 0.12, { noBot: true }); addNicheFrame(F, 0, 6.1, 11.0, 1.42); });+ // above the kings the pier stands back on the gallery's roof+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: WF.kingTop + 0.1, y1: 38.8, w: 0.92, f: 1.26 }, { y0: 40, y1: yTop + 0.1, w: 0.8, f: 0.96, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 1.26), () => addNicheFrame(F, 0, 31, 37.4, 1.3));+ D.at(M.T(x, 0, 3.6), () => { addStain(D, -1.05, 1.05, 11.4, 0.02, 2.2, 'drip'); addStain(D, -1.05, 1.05, 4.6, 0.03, 4.4, 'ground'); addStain(D, -1.05, 1.05, 11.45, 0.03, 0.7, 'soft'); });+ placeFigure('Buttress_Saint' + i, SAINTS[i + 3], { pos: [x, 6.2, 3.98], rot: [0, -90, 0], scale: 1.42, parent: gWest });+ place('Facade_Gargoyle' + i, gargoyle(i % 3), MAT.fine, { pos: [x, 17.2, 2.55], rot: [0, -90, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ placeFigure('Upper_Saint' + i, SAINTS[i + 5], { pos: [x, 31.1, 1.62], rot: [0, -90, 0], scale: 1.32, parent: gWest, lod: 0 });+ });+ for (const s of [-1, 1]) {+ const k = Math.SQRT1_2, z = 3.98, cx = s * (hw + k * z);+ placeFigure('Corner_Saint' + s, SAINTS[s > 0 ? 3 : 5], { pos: [cx, 6.2, k * z], rot: [0, -90 + s * 45, 0], scale: 1.42, parent: gWest });+ place('Corner_Gargoyle' + s, gargoyle(s > 0 ? 1 : 2), MAT.fine, { pos: [s * (hw + k * 2.65), 26.6, k * 2.65], rot: [0, -90 + s * 45, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ }+ // closing blocks where the gallery of kings dies into the corner piers+ for (const s of [-1, 1]) A.box(Math.min(s * 19.9, s * 20.6), Math.max(s * 19.9, s * 20.6), WF.kingY - 0.5, WF.kingTop + 0.12, 0, 1.3, { noBot: true });+ placeSet('West_Wall', set, [0, 0, 0], 0, gWest);+ // ---- Glass ----+ const roseWest = makeRoseGlass(WF.roseR, 101);+ SHARED.roseGlass = roseWest;+ placeGlass('Great_Rose', roseWest, [0, WF.roseY, -0.4], 180, gWest, true);+ const twinGlass = SHARED.cleres;+ for (const s of [-1, 1]) place('TowerWindow_Out' + s, glassOutside(twinGlass[s > 0 ? 0 : 1]), MAT.glassOutWin, { pos: [s * tcx, 0, 0.14], rot: [0, 180, 0], parent: gWest, scale: [1, 1, -1] });+ // ---- Three portals: a bishop, a crowned king, a veiled queen, an apostle with his key, a smiling angel;+ // the Virgin and Child on the central door post, Christ enthroned above her ----+ const pC = buildPortal(cP, ['apostle', 'bishop', 'queen', 'king', 'apostle', 'king', 'angel', 'bishop', 'queen', 'apostle'], { main: 'seated', side: ['angel', 'queen'], trumeau: 'virgin', trumeauScale: 1.4 });+ const pS = buildPortal(Object.assign({}, sP, { x: 0 }), ['queen', 'bishop', 'apostle', 'king', 'angel', 'apostle', 'king', 'queen'], { main: 'seatedVirgin', side: ['angel', 'apostle'], trumeau: 'bishop', trumeauScale: 1.24 });+ pC.baseX = cP.x; pS.baseX = 0; SHARED.sidePortal = pS;+ placePortal(gWest, 'Portal_Central', cP, pC);+ placePortal(gWest, 'Portal_South', Object.assign({}, sP, { x: -sP.x }), pS, { light: 'scene' });+ placePortal(gWest, 'Portal_North', Object.assign({}, sP, { x: sP.x }), pS, { light: 'scene' });+ // ---- Gallery of Kings ----+ const nK = P.kings, pitch = 2 * 20.1 / nK, niche = buildKingNiche(pitch), kinds = ['kingA', 'kingB'];+ for (let i = 0; i < nK; i++) {+ const x = (i + 0.5 - nK / 2) * pitch;+ placeSet('King_Niche' + i, niche, [x, 0, 0], 0, gWest);+ placeFigure('King' + i, kinds[i % 2], { pos: [x, WF.kingY + 0.04, 0.62], rot: [0, -90 + (i % 4 - 1.5) * 7, 0], scale: 1.7, parent: gWest, mirror: i % 2 === 1 });+ }+ // ---- High gallery ----+ const nG = 20, gp = 2 * 19.75 / nG, gb = buildGalleryBay(gp);+ for (let i = 0; i < nG; i++) placeSet('Gallery' + i, gb, [(i + 0.5 - nG / 2) * gp, 0, 0], 0, gWest);+ // ---- Central gable rising over the gallery, closing the nave roof ----+ const front = mkBatches(), yG0 = yTop + 1.2, yR = LAY.ridge + 1.2, gW = 2 * (zn + 1.5);+ front.A.at(M.T(0, 0, -1.2), () => front.A.slab([[-gW / 2, yG0 - 2], [gW / 2, yG0 - 2], [gW / 2, yG0], [0, yR + 1.6], [-gW / 2, yG0]], [ringPts(0, yG0 + 1.8, 1.25, 12)], -0.5, 0.5, { sr: 0.5 }));+ front.F.at(M.T(0, 0, -0.7), () => {+ addGable(front.F, gW + 0.2, yR + 1.6 - yG0, 0.04, 0.26, yG0, { size: 0.86, finial: 0.1, crockets: 7, noField: true, lod: 1 });+ front.F.sweep(ringPts(0, yG0 + 1.8, 1.32, 14), traceryBar(0.26, 0.44), { closed: true });+ // Blind arcade stepping up under the rake+ const nA = 7; for (let i = 0; i < nA; i++) { const x = (i - (nA - 1) / 2) * 1.72, top = yG0 + 0.3 + (gW / 2 - Math.abs(x) - 1) * (yR + 1.6 - yG0) / (gW / 2) * 0.7; if (Math.abs(x) < 1.7) continue; front.F.sweep([[x - 0.62, yG0], [x - 0.62, top], [x, top + 0.75], [x + 0.62, top], [x + 0.62, yG0]], hexBar(0.14, 0.16), { caps: false, crease: 20 }); }+ });+ front.G.at(M.T(0, yR + 1.62, -0.5), () => addFinial(front.G, 2.2, 1));+ placeSet('West_Gable', front, [0, 0, 0], 0, gWest);+ place('West_Gable_Glass', makeLancetRoundel(1.25), MAT.glassOut, { pos: [0, yG0 + 1.8, -0.75], parent: gWest });+ // ---- Towers: shell, belfry faces, corner turrets, lead cap ----+ // One tower shell (side and rear walls of the lower stages), built outward towards +x; the other tower mirrors it+ const shell = mkBatches(), tw = 7;+ // The outer side of the tower: real recessed arcades in two tiers, either side of its windows+ const sideM = M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), sideRecess = [];+ for (const d of [-1, 1]) {+ sideRecess.push(archOutline(1.2, 3.4, 16.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ sideRecess.push(archOutline(1.2, 26.4, 37.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ }+ shell.A.at(sideM, () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], sideRecess, -0.8, 0, { noOuterSides: true, noBack: true, sr: 0.62 }));+ shell.A.at(sideM, () => shell.F.at(sideM, () => sideRecess.forEach((o) => addRecess(shell.A, shell.F, o, 0.52))));+ shell.D.at(sideM, () => { for (const y of [19.2, WF.kingTop - 0.34, WF.galY - 0.34]) { addStain(shell.D, -tw, tw, y, 0.02, 3.2); addStain(shell.D, -tw, tw, y, 0.03, 0.8, 'soft'); } addStain(shell.D, -tw, tw, 5.2, 0.03, 5.2, 'ground'); });+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(180), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(-90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ // string courses and tall blind windows on the outer side+ shell.F.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => {+ for (const y of [1.0, 19.6, WF.kingTop, WF.galY]) shell.F.sweep([[-tw, y], [tw, y]], stringSection(0.3, 0.26), { caps: false });+ addTracery(shell.F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ shell.F.sweep(archOutline(cw, cSill, cSp, cRise, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ addGable(shell.F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 3, lod: 0 });+ addTracery(shell.F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 });+ shell.F.sweep(archOutline(...windowArgs.aisle, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ });+ // The tower's two outer corners: one diagonal pier each, from the ground to the foot of the turret it carries+ const cornerStages = [{ y0: 0, y1: 13, w: 2.0, f: 3.6 }, { y0: 14.4, y1: 27, w: 1.9, f: 2.8 }, { y0: 28.4, y1: yTop - 1, w: 1.78, f: 2.05 }];+ for (const [zc, turn] of [[0, 45], [-2 * tw, 135]]) {+ const m = M.of(M.T(tw, 0, zc), M.Ry(turn));+ shell.A.at(m, () => shell.F.at(m, () => addPier(shell, cornerStages, { back: 2, flat: true })));+ shell.F.at(m, () => shell.F.box(-2.14, 2.14, 0, 0.74, -2, 3.74, { noBot: true }));+ shell.D.at(M.of(m, M.T(0, 0, 3.6)), () => { addStain(shell.D, -2, 2, 13, 0.02, 3.2); addStain(shell.D, -2, 2, 13.02, 0.03, 0.8, 'soft'); addStain(shell.D, -2, 2, 5, 0.03, 4.3, 'ground'); });+ if (zc === 0) shell.F.at(M.of(m, M.T(0, 0, 3.6)), () => addNicheFrame(shell.F, 0, 6.1, 11, 1.42));+ }+ for (const s of [-1, 1]) {+ const cx = s * tcx;+ placeSet('Tower_Shell', shell, [cx, 0, 0], 0, gWest, { scale: [s, 1, 1] });+ // Set just proud of the wall face: these towers are solid here, so the glass is seen from outside only+ place('Tower_SideGlass_High' + s, glassOutside(twinGlass[2]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ place('Tower_SideGlass_Low' + s, glassOutside(SHARED.stories[1]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ }+ const face = buildBelfryFace(), corner = buildTowerCorner();+ const cap = new Batch(TILE.lead), capG = new Batch(TILE.gold);+ cap.lathe([[5.9, P.towerTop - 2.5], [0.2, P.towerTop + 4.8]], 8, { faceted: true, phase: PI / 8 });+ capG.at(M.T(0, P.towerTop + 4.7, 0), () => addFinial(capG, 2.4, 1));+ for (const s of [-1, 1]) {+ const gT = group('Tower_' + (s > 0 ? 'North' : 'South'), gWest), cx = s * tcx, cz = -tw;+ for (let i = 0; i < 4; i++) {+ placeSet('Belfry_Face' + i, face, [cx, 0, cz], i * 90, gT);+ const ax = cx + (i === 0 || i === 3 ? 1 : -1) * tw, az = cz + (i < 2 ? 1 : -1) * tw;+ placeSet('Tower_Turret' + i, corner, [ax, 0, az], i * 90, gT);+ // the great corner pinnacles carry the tower to its full height+ const yT = P.towerTop - 1.3 + 0.4, hP = P.spireTip - yT;+ place('Tower_Pinnacle' + i, largePinnacle(), MAT.fine, { pos: [ax, yT, az], scale: [1.16, hP / 10, 1.16], parent: gT });+ }+ place('Tower_Cap_Lead', cap, MAT.lead, { pos: [cx, 0, cz], parent: gT });+ place('Tower_Cap_Gilt', capG, MAT.gold, { pos: [cx, 0, cz], parent: gT });+ }+ // ---- Inner face: blind niches with statues, a gallery, the back of the doors ----+ const inner = mkBatches();+ inner.F.at(M.of(M.T(0, 0, -WF.wallT), M.Ry(180)), () => {+ inner.F.sweep([[-13.1, 10.2], [13.1, 10.2]], stringSection(0.34, 0.34), { caps: false });+ inner.F.sweep([[-6.5, P.triforiumSill], [6.5, P.triforiumSill]], stringSection(0.34, 0.3), { caps: false });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) addNicheFrame(inner.F, s * (3.65 + i * 1.5), 3.2 + 0, 8.8, 1.2);+ inner.F.sweep(ringPts(0, WF.roseY, WF.roseR + 0.3, 32), traceryBar(0.42, 0.5), { closed: true });+ addBlindArcade(inner.F, -6.5, 6.5, 11, 15.2, 8, { bar: 0.14, depth: 0.2 });+ });+ placeSet('West_InnerFace', inner, [0, 0, 0], 0, gWest);+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) placeFigure('Inner_Saint_' + (s > 0 ? 'N' : 'S') + i, SAINTS[i + (s > 0 ? 1 : 6)], { pos: [s * (3.65 + i * 1.5), 3.25, -WF.wallT - 0.28], rot: [0, 90, 0], scale: 1.32, parent: gWest, indoor: true });+}+function makeLancetRoundel(r) { const g = new Glass(55); g.raw(ringPts(0, 0, r + 0.05, 20), 'lead', -0.4, 0); gOculus(g, 0, 0, r, { ground: 'cobalt', inner: 'gold' }); return g.b; }++// ---- Transept fronts: one portal, a smaller rose, lancets, gable, corner turrets ----------+function buildTransFront(gT, s) {+ const fr = frame(s > 0 ? 0 : 180, LAY.xc, 0), hw = zaw + 0.9, zEnd = zn + (P.transeptBays + 1) * BAY, yTop = P.eaves;+ const set = TRANS.set || (TRANS.set = (() => {+ const q = mkBatches();+ const { A, F, G } = q, rY = 28.6, R = 4.5, w0 = WF.portalS.w0, dh = WF.portalS.ys - 0.9;+ const at = M.T(0, 0, zEnd);+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-hw, 0], [-w0 / 2, 0], [-w0 / 2, dh], [w0 / 2, dh], [w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]], [ringPts(0, rY, R, 32)], -0.9, 0, { noOuterSides: true, sr: 0.62 }));+ F.at(M.of(at, M.T(0, rY, 0.9)), () => { F.sweep(ringPts(0, 0, R + 0.3, 32), traceryBar(0.4, 0.5), { closed: true }); F.sweep(ringPts(0, 0, R + 0.72, 36), rollSection(0.18, 0.26, 1, 0.03, 2), { closed: true }); });+ q.D.at(M.of(at, M.T(0, 0, 0.9)), () => { for (const [a, c] of [[-hw, -12.3], [-6.9, 6.9], [12.3, hw]]) { addStain(q.D, a, c, 17.2, 0.02, 3.2); addStain(q.D, a, c, 17.2, 0.03, 0.8, 'soft'); } addStain(q.D, -hw, hw, 22.6, 0.02, 2.8, 'drip'); addStain(q.D, -hw, hw, 22.6, 0.03, 0.7, 'soft'); addStain(q.D, -hw, -w0 / 2, 5.2, 0.03, 5.2, 'ground'); addStain(q.D, w0 / 2, hw, 5.2, 0.03, 5.2, 'ground'); });+ // Row of lancets beneath the rose+ F.at(M.of(at, M.T(0, 0, 0.9)), () => {+ F.sweep([[-hw, 0.8], [hw, 0.8]], stringSection(0.42, 0.36), { caps: false });+ // This string runs up to the hood of each window head and stops there: it never crosses the tracery+ const hood = 9.6 - 1.62, outerHood = 9.6 + 1.62;+ F.sweep([[-hood, 17.4], [hood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ for (const t of [-1, 1]) F.sweep(t > 0 ? [[outerHood, 17.4], [hw, 17.4]] : [[-hw, 17.4], [-outerHood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ F.sweep([[-hw, 22.8], [hw, 22.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -8, 8, 18, 20.8, 8, { bar: 0.16, depth: 0.2 });+ for (const x of [-9.6, 9.6]) F.at(M.T(x, 0, 0.1), () => { addTracery(F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 }); F.sweep(archOutline(...windowArgs.aisle, 7), hexBar(0.22, 0.42), { closed: true, crease: 40, z: -0.1 }); });+ });+ // The inner face of the end wall: tiers of blind arcading and string courses+ F.at(M.of(at, M.Ry(180)), () => {+ F.sweep([[-zaw, 11], [zaw, 11]], stringSection(0.3, 0.26), { caps: false });+ F.sweep([[-zaw, 16.8], [zaw, 16.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -5.6, 5.6, 1.2, 7.8, 5, { bar: 0.16, depth: 0.2 });+ addBlindArcade(F, -zn, zn, 11.2, 14.8, 8, { bar: 0.12, depth: 0.16 });+ addBlindArcade(F, -zn, zn, 17, 20.6, 8, { bar: 0.12, depth: 0.16 });+ });+ // Gable closing the arm's roof, in front of the lead+ const yG = yTop, gW = 2 * (zn + 1.6), yR = LAY.ridge + 1;+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-gW / 2, yG], [gW / 2, yG], [0, yR]], [ringPts(0, yG + 3.4, 1.5, 12)], -0.9, 0, { noOuterSides: true, sr: 0.5 }));+ F.at(M.of(at, M.T(0, 0, 0.9)), () => { addGable(F, gW + 0.2, yR - yG, 0.02, 0.24, yG, { size: 0.82, finial: 0.1, crockets: 7, noField: true, lod: 1 }); F.sweep(ringPts(0, yG + 3.4, 1.55, 14), traceryBar(0.24, 0.42), { closed: true }); });+ G.at(M.of(at, M.T(0, yR, 1.12)), () => addFinial(G, 2.0, 1));+ F.at(M.of(at, M.T(0, 0, 1.0)), () => addParapet(F, -hw, hw, yTop - 0.1, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Each corner of the front carries one diagonal pier rising to its pinnacle, tied into both walls+ for (const sx of [-1, 1]) {+ const m = M.of(M.T(sx * hw, 0, zEnd + 0.9), M.Ry(sx * 45));+ A.at(m, () => F.at(m, () => addPier(q, [{ y0: 0, y1: 14.6, w: 1.7, f: 2.7 }, { y0: 15.8, y1: 30.6, w: 1.58, f: 2.05 }, { y0: 31.8, y1: yTop + 1, w: 1.45, f: 1.5 }], { back: 1.7, flat: true })));+ F.at(m, () => F.box(-1.84, 1.84, 0, 0.74, -1.7, 2.84, { noBot: true }));+ F.at(M.of(m, M.T(0, 0, 2.05)), () => addNicheFrame(F, 0, 16.2, 22, 1.5));+ q.D.at(M.of(m, M.T(0, 0, 2.7)), () => { addStain(q.D, -1.7, 1.7, 14.6, 0.02, 3); addStain(q.D, -1.7, 1.7, 14.62, 0.03, 0.8, 'soft'); addStain(q.D, -1.7, 1.7, 5, 0.03, 4.3, 'ground'); });+ }+ return q;+ })());+ placeSet('Transept_Front', set, [fr.ox, 0, fr.oz], fr.th, gT);+ // Portal (the same carving as the west side portals), statue niches, glass+ const gPortal = group('Transept_Portal', gT);+ gPortal.position.set(...fr.pos(0, 0, zEnd + 0.9)); gPortal.rotation.y = rad(fr.th);+ placePortal(gPortal, 'Portal_Transept', { x: 0 }, SHARED.sidePortal, { light: 'all' });+ place('Transept_Rose_Tracery', roseTracery(), MAT.fine, { pos: fr.pos(0, 28.6, zEnd + 0.45), rot: fr.rot(0), scale: [4.5 / WF.roseR, 4.5 / WF.roseR, 1], parent: gT });+ for (const sx of [-1, 1]) place('Transept_Pinnacle', largePinnacle(), MAT.fine, { pos: fr.pos(sx * hw, yTop + 1, zEnd + 0.9), scale: [1.05, 1.1, 1.05], parent: gT });+ placeGlass('Transept_Rose', SHARED.roseGlass, fr.pos(0, 28.6, zEnd + 0.45), fr.th + 180, gT, false, 4.5 / WF.roseR);+ const low = SHARED.stories[0];+ for (const sx of [-1, 1]) {+ // Aisle-end windows: the same panes on the inner and outer faces of the end wall+ place('Transept_EndWindow_In', low, MAT.glassIn, { pos: fr.pos(sx * 9.6, 0, zEnd - 0.01), rot: fr.rot(180), parent: gT });+ place('Transept_EndWindow_Out', glassOutside(low), MAT.glassOutWin, { pos: fr.pos(sx * 9.6, 0, zEnd + 0.92), rot: fr.rot(180), scale: [1, 1, -1], parent: gT });+ const dz = Math.SQRT1_2 * 2.37;+ placeFigure('Transept_Corner_Saint' + sx, SAINTS[sx > 0 ? 2 : 4], { pos: fr.pos(sx * (hw + dz), 16.25, zEnd + 0.9 + dz), rot: fr.rot(-90 + sx * 45), scale: 1.42, parent: gT, lod: 0 });+ }+}+const TRANS = {};++// ---- The east end: raised sanctuary, high altar with a gilded retable, oak choir stalls ------+function buildSanctuary() {+ const g = group('Sanctuary'), set = mkBatches(), { F, G, O } = set, { xa, xChoirW } = LAY;+ // Three broad steps and the altar, all moulded+ for (let i = 0; i < 3; i++) { const r = 5 - i * 0.7; F.at(M.T(xa + 1, 0, 0), () => F.lathe([[r, i * 0.2], [r, (i + 1) * 0.2], [0.01, (i + 1) * 0.2]], 14, { faceted: true, phase: PI / 14 })); }+ const y0 = 0.6, ax = xa + 1;+ F.at(M.T(ax, y0, 0), () => {+ F.box(-0.8, 0.8, 0, 1.0, -1.7, 1.7, {});+ F.box(-0.92, 0.92, 1.0, 1.12, -1.84, 1.84, { sb: 0.5 });+ for (const s of [-1, 1]) for (const t of [-1, 0, 1]) F.at(M.T(s * 0.82, 0, t * 1.1), () => F.lathe([[0.12, 0], [0.07, 0.12], [0.07, 0.88], [0.13, 1.0]], 6));+ });+ // A carved retable behind the altar: five stone niches under gables, each with its figure, only the arches gilded+ const retM = M.of(M.T(ax - 1.0, y0 + 1.12, 0), M.Ry(90));+ F.at(retM, () => {+ F.box(-2.46, 2.46, -0.26, 0, -0.2, 0.14, {});+ for (let i = -2; i <= 2; i++) {+ const x = i * 0.96, h = i === 0 ? 2.2 : 1.56;+ F.box(x - 0.47, x + 0.47, 0, h - 0.3, -0.2, -0.1, {});+ addGable(F, 0.96, i === 0 ? 0.8 : 0.66, 0.04, 0.06, h - 0.32, { size: 0.15, crockets: 2, finial: 0.24, solid: true, lod: 0 });+ F.at(M.T(x - 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ if (i === 2) F.at(M.T(x + 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ F.at(M.T(x - 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ if (i === 2) F.at(M.T(x + 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ }+ });+ G.at(retM, () => { for (let i = -2; i <= 2; i++) { const x = i * 0.96, h = i === 0 ? 2.2 : 1.56; G.sweep(pointedArc(0.82, 0.52, 4).map((p) => [x + p[0], h - 0.84 + p[1]]), hexBar(0.04, 0.05), { z: 0.03, caps: false }); } });+ const altarSaints = [['apostle', 0.56], ['bishop', 0.56], ['seatedVirgin', 0.74], ['king', 0.56], ['queen', 0.56]];+ altarSaints.forEach((q, n) => placeFigure('Retable_Figure' + n, q[0], { pos: [ax - 1.04, y0 + 1.14, -(n - 2) * 0.96], rot: [0, 0, 0], scale: q[1], parent: g, indoor: true, lod: q[0] === 'seatedVirgin' ? 1 : 0 }));+ // Candlesticks+ for (const t of [-1.5, -0.9, -0.3, 0.3, 0.9, 1.5]) G.at(M.T(ax - 0.2, y0 + 1.12, t), () => { G.lathe([[0.1, 0], [0.04, 0.06], [0.025, 0.42], [0.06, 0.46], [0.03, 0.5]], 6); F.at(M.T(ax - 0.2, y0 + 1.62, t), () => F.lathe([[0.022, 0], [0.022, 0.34], [0.002, 0.36]], 5, { s: 1.6 })); });+ // Choir stalls: stepped oak benches, moulded backs and a carved canopy rail+ const x0 = xa + 5.5, x1 = xChoirW - 1.2;+ for (const s of [-1, 1]) {+ const z0 = s * 5.0;+ O.box(x0, x1, 0, 0.32, z0 - 0.9, z0 + 0.9, { s: 0.42 });+ O.box(x0, x1, 0.32, 0.5, z0 + (s > 0 ? 0.0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.46 });+ O.box(x0, x1, 0, 2.7, z0 + (s > 0 ? 0.8 : -0.9), z0 + (s > 0 ? 0.9 : -0.8), { s: 0.4 });+ const n = Math.floor((x1 - x0) / 1.5);+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * (x1 - x0) / n;+ O.box(x - 0.045, x + 0.045, 0, 0.98, z0 + (s > 0 ? 0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.5 });+ O.box(x - 0.03, x + 0.03, 0.98, 2.72, z0 + (s > 0 ? 0.74 : -0.8), z0 + (s > 0 ? 0.8 : -0.74), { s: 0.48 });+ O.box(x - 0.08, x + 0.08, 2.72, 3.28, z0 + (s > 0 ? 0.56 : -0.86), z0 + (s > 0 ? 0.86 : -0.56), { s: 0.46, taper: 0.05 });+ }+ O.box(x0, x1, 2.7, 2.84, z0 + (s > 0 ? 0.3 : -0.9), z0 + (s > 0 ? 0.9 : -0.3), { s: 0.44 });+ }+ placeSet('Sanctuary', set, [0, 0, 0], 0, g);+}++// ==================================================================================+// THE GROUND. One named group, separate from the building and left out of its light, so it can simply be deleted+// when the cathedral is placed in a town. All of it stands a few centimetres above y = 0.+// ==================================================================================+const GROUND = { flagY: 0.03, earthY: 0.015, flags: 20, earth: 33, stepUp: 0.14 };+function footprintDistance(x, z) { // signed distance in plan to the outer walls of the building+ const { xa, xW, xTowerE, xc } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const box = (x0, x1, z0, z1) => { const dx = Math.max(x0 - x, x - x1), dz = Math.max(z0 - z, z - z1); return dx <= 0 && dz <= 0 ? Math.max(dx, dz) : Math.sqrt(Math.max(dx, 0) ** 2 + Math.max(dz, 0) ** 2); };+ return Math.min(box(xa, xW, -14, 14), box(xTowerE, xW, -WF.halfW, WF.halfW), box(xc - 14, xc + 14, -zEnd, zEnd), Math.sqrt((x - xa) * (x - xa) + z * z) - (CHEV.zc + CHEV.Rc + 0.5));+}+function buildGround() {+ const g = group('Ground'), { xa, xW, xc, xTowerE } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const fl = new Batch(TILE.flag), ea = new Batch(TILE.earth), st = new Batch(TILE.flag), gr = new Batch(1); gr.noColor = true;+ fl.noVoxel = ea.noVoxel = st.noVoxel = true;+ const wob = (x, z) => 2.2 * Math.sin(x * 0.31 + 1.3) * Math.cos(z * 0.27) + 1.4 * Math.sin(x * 0.13 + z * 0.19);+ const cell3 = (b, x0, z0, s, y) => { const q = [[x0, z0], [x0, z0 + s], [x0 + s, z0 + s], [x0 + s, z0]].map((p) => b.v(p[0], y(p[0], p[1]), p[1], 0, 1, 0, p[0], p[1], 1)); b.quad(q[0], q[1], q[2], q[3]); };+ // Worn flags from the walls out for about 20 m. Their edge breaks up slab by slab on the earth; nothing is left under the church floor+ const flagKeep = (x, z) => { const d = footprintDistance(x, z); return d > -2.5 && d < GROUND.flags + wob(x, z); };+ for (let gx = Math.floor((xa - 62) / 3); gx * 3 < xW + 62; gx++) for (let gz = -24; gz < 24; gz++) {+ const x0 = gx * 3, z0 = gz * 3, d = footprintDistance(x0 + 1.5, z0 + 1.5), lim = GROUND.flags + wob(x0 + 1.5, z0 + 1.5);+ if (Math.abs(d - lim) < 3.2 && d > 5) { for (const [a, c] of [[0, 0], [0, 1.5], [1.5, 0], [1.5, 1.5]]) if (flagKeep(x0 + a + 0.75, z0 + c + 0.75)) cell3(fl, x0 + a, z0 + c, 1.5, () => GROUND.flagY); } else if (flagKeep(x0 + 1.5, z0 + 1.5)) cell3(fl, x0, z0, 3, () => GROUND.flagY);+ }+ // Rough grass and packed earth under and beyond them, its edge dissolving so it sits on any ground plane without a seam+ ea.fade = (x, z) => sstep(GROUND.earth + 8, GROUND.earth - 3, footprintDistance(x, z) - wob(z, x));+ for (let gx = Math.floor((xa - 62) / 4); gx * 4 < xW + 62; gx++) for (let gz = -18; gz < 18; gz++) {+ const x0 = gx * 4, z0 = gz * 4, d = footprintDistance(x0 + 2, z0 + 2);+ if (d > GROUND.flags - 8 && d < GROUND.earth + 11) cell3(ea, x0, z0, 4, () => GROUND.earthY);+ }+ // A step or two up to every door: a landing that runs in to the door between the jambs, and a lower step in front of it+ const steps = (m, half, zF) => st.at(m, () => {+ st.box(-half - 0.2, half + 0.2, GROUND.flagY, GROUND.flagY + 2 * GROUND.stepUp, -0.2, zF + 0.75, { noBot: true });+ st.box(-half - 0.62, half + 0.62, GROUND.flagY, GROUND.flagY + GROUND.stepUp, zF + 0.75, zF + 1.5, { noBot: true });+ });+ const west = M.of(M.T(xW, 0, 0), M.Ry(90)), cP = WF.portalC, sP = WF.portalS, outer = (c) => (c.w0 + 2 * c.orders * c.dx) / 2;+ steps(west, outer(cP), cP.orders * cP.dz);+ for (const s of [-1, 1]) steps(M.of(west, M.T(s * sP.x, 0, 0)), outer(sP), sP.orders * sP.dz);+ for (const s of [0, 180]) steps(M.of(M.T(xc, 0, 0), M.Ry(s), M.T(0, 0, zEnd)), outer(sP), sP.orders * sP.dz);+ // Dark grime where the walls meet the ground: a soft band lying on the flags along every wall foot+ const strip = (ax, az, bx, bz, w) => {+ const R = DECAL_UV.soft, dx = bx - ax, dz = bz - az, l = Math.sqrt(dx * dx + dz * dz) || 1, nx = dz / l, nz = -dx / l, y = GROUND.flagY + 0.012, n = Math.max(1, Math.round(l / 5)), span = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = i / n, c = (i + 1) / n, u0 = R[0] + (i % 3) * span * 0.22, u1 = u0 + span * 0.3, pa = [ax + dx * a, az + dz * a], pc = [ax + dx * c, az + dz * c];+ const q = [gr.v(pa[0], y, pa[1], 0, 1, 0, u0, R[1], 1), gr.v(pa[0] + nx * w, y, pa[1] + nz * w, 0, 1, 0, u0, R[3], 1), gr.v(pc[0] + nx * w, y, pc[1] + nz * w, 0, 1, 0, u1, R[3], 1), gr.v(pc[0], y, pc[1], 0, 1, 0, u1, R[1], 1)];+ gr.quad(q[0], q[1], q[2], q[3]);+ }+ };+ // every strip runs with the wall on its left as seen from above, so its band falls outward+ const loop = (pts, w) => { for (let i = 0; i < pts.length - 1; i++) strip(pts[i][0], pts[i][1], pts[i + 1][0], pts[i + 1][1], w); };+ const hx = WF.halfW, xE = xc + 14, xCE = xc - 14;+ loop([[xW, hx], [xTowerE, hx], [xTowerE, 14], [xE, 14], [xE, zEnd], [xCE, zEnd], [xCE, 14], [xa, 14]], 1.8);+ loop([[xa, -14], [xCE, -14], [xCE, -zEnd], [xE, -zEnd], [xE, -14], [xTowerE, -14], [xTowerE, -hx], [xW, -hx], [xW, hx]], 1.8);+ for (let k = 1; k < P.apseSides; k++) { const f0 = frame(-k * CHEV.d, xa, 0), a = f0.pos(-0.98, 0, BUTTRESS.zB + 0.2), c = f0.pos(0.98, 0, BUTTRESS.zB + 0.2); strip(a[0], a[2], c[0], c[2], 1.3); }+ for (let k = 0; k < P.apseSides; k++) { const r = CHEV.zc + CHEV.Rc + 0.55, n = 5; for (let i = 0; i < n; i++) { const f0 = frame(-(k + 0.5) * CHEV.d, xa, 0), a0 = rad(-108 + i * 216 / n), a1 = rad(-108 + (i + 1) * 216 / n), R2 = CHEV.Rc + 0.55, p = f0.pos(R2 * Math.sin(a0), 0, CHEV.zc + R2 * Math.cos(a0)), q = f0.pos(R2 * Math.sin(a1), 0, CHEV.zc + R2 * Math.cos(a1)); strip(p[0], p[2], q[0], q[2], 1.2); } }+ place('Ground_Flagstones', fl, MAT.flag, { parent: g });+ place('Ground_Earth', ea, MAT.earth, { parent: g });+ place('Ground_Steps', st, MAT.flag, { parent: g });+ place('Ground_Grime', gr, MAT.decal, { parent: g });+}++function buildCathedral() {+ buildBody();+ buildWestFront();+ const gTN = ROOT.children.find((c) => c.name === 'Transept_North'), gTS = ROOT.children.find((c) => c.name === 'Transept_South');+ buildTransFront(gTN, 1);+ buildTransFront(gTS, -1);+ buildSanctuary();+ buildGround();+}++function previewStatues() {+ const names = P.statueTest;+ names.forEach((n, i) => placeFigure('Fig_' + n, n, { pos: [0, 0, (i - (names.length - 1) / 2) * 1.0] }));+ const base = new Batch(TILE.ashlar); base.box(-3, 3, -0.4, 0, -6, 6, { s: 0.8 }); place('Ground', base, MAT.ashlar);+}++// ==================================================================================+// BAKED LIGHT. The viewer casts no shadows, so the shadows are computed here. Every opaque surface is voxelized+// into the world, then each vertex looks at the sun, the sky and the rain:+// - rays towards the sun give the low raking light and the cast shadows;+// - rays over the hemisphere give the depth of portals and galleries, and tell inside from outside;+// - a ray straight up finds what the rain never reaches, where seven centuries of soot have stayed.+// A batch placed many times can have one set of vertex colours per side of the building, within a byte budget.+// Sets beyond the budget share the sunniest colours under a darker material.+// ==================================================================================+const SKY_RAYS = (() => {+ const r = [[0, 1, 0, 1]];+ for (let i = 0; i < 4; i++) { const a = i * TAU / 4 + 0.4, e = rad(42); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 1]); }+ for (let i = 0; i < 5; i++) { const a = i * TAU / 5 + 0.1, e = rad(9); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 0]); }+ return r;+})();+// Large meshes of moulding keep their shape with a quarter fewer triangles: the bytes are better spent up close+async function reduceBatches(share = 0.74, repeatedShare = 0.62, minTris = 2000) {+ const seen = new Map(), uses = new Map();+ for (const e of PLACED) uses.set(e.batch, (uses.get(e.batch) || 0) + 1);+ for (const e of PLACED) {+ const m = e.mat;+ if (e.batch.isFigure || e.batch.noReduce || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.glassOutWin || m === MAT.glassRose || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave || m === MAT.flag || m === MAT.earth) continue;+ let nb = seen.get(e.batch);+ if (nb === undefined) {+ const tris = e.batch.I.length / 3, s = uses.get(e.batch) >= 8 ? repeatedShare : share; // a mesh repeated down the whole building costs the most+ nb = tris > minTris ? await lightCopy(e.batch, Math.round(tris * s)) : null;+ if (nb) { nb.noColor = e.batch.noColor; nb.pierShaft = e.batch.pierShaft; }+ seen.set(e.batch, nb);+ }+ if (nb) { e.batch = nb; e.part.geometry = nb.toGeo(); }+ }+}+function samplesOf(b) {+ if (b.samples) return b.samples;+ const P0 = b.P, out = [], lim = 0.32;+ for (let i = 0; i < P0.length; i++) out.push(P0[i]);+ for (let t = 0; t < b.I.length; t += 3) {+ const a = b.I[t] * 3, c = b.I[t + 1] * 3, d = b.I[t + 2] * 3;+ const e1 = len3(P0[a] - P0[c], P0[a + 1] - P0[c + 1], P0[a + 2] - P0[c + 2]), e2 = len3(P0[c] - P0[d], P0[c + 1] - P0[d + 1], P0[c + 2] - P0[d + 2]), e3 = len3(P0[d] - P0[a], P0[d + 1] - P0[a + 1], P0[d + 2] - P0[a + 2]), m = Math.max(e1, e2, e3);+ if (m <= lim) continue;+ const n = Math.ceil(m / lim);+ for (let i = 0; i <= n; i++) for (let j = 0; j <= n - i; j++) { const u = i / n, v = j / n, w = 1 - u - v; out.push(P0[a] * u + P0[c] * v + P0[d] * w, P0[a + 1] * u + P0[c + 1] * v + P0[d + 1] * w, P0[a + 2] * u + P0[c + 2] * v + P0[d + 2] * w); }+ }+ b.samples = new Float32Array(out);+ return b.samples;+}+function bakeLight() {+ ROOT.updateMatrixWorld(true);+ // The world follows the plan: from beyond the chapels to beyond the west front. A spire tip above the top simply casts no shadow up there+ const CELL = 0.4, X0 = LAY.xa - 27.8, Y0 = -2, Z0 = -36, NX = Math.ceil((P.xWest + 12 - X0) / CELL), NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;+ const grid = new Uint8Array(NX * NY * NZ), coarse = new Uint8Array(CX * CY * CZ);+ const SUN = (() => { const v = P.sun, l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();+ const SUN2 = [[SUN[0] + 0.06, SUN[1], SUN[2] + 0.05], [SUN[0] - 0.05, SUN[1] + 0.05, SUN[2] - 0.05]].map((v) => { const l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; });+ for (const e of PLACED) {+ if (e.mat.transparent || e.batch.noVoxel) continue;+ const S = samplesOf(e.batch), m = e.part.matrixWorld.elements, shaft = !!e.batch.pierShaft;+ for (let i = 0; i < S.length; i += 3) {+ const x = S[i], y = S[i + 1], z = S[i + 2], gi = ((m[0] * x + m[4] * y + m[8] * z + m[12] - X0) / CELL) | 0, gj = ((m[1] * x + m[5] * y + m[9] * z + m[13] - Y0) / CELL) | 0, gk = ((m[2] * x + m[6] * y + m[10] * z + m[14] - Z0) / CELL) | 0;+ if (gi >= 0 && gi < NX && gj >= 0 && gj < NY && gk >= 0 && gk < NZ) {+ const q = gi + NX * (gj + NY * gk); // 2 marks a voxel that holds only a pier shaft: solid to the stone, tested exactly by the window light+ if (!shaft) grid[q] = 1; else if (!grid[q]) grid[q] = 2;+ coarse[(gi >> 3) + CX * ((gj >> 3) + CY * (gk >> 3))] = 1;+ }+ }+ }+ // How enclosed a point is: the solid voxels averaged into 1.6 m cells and blurred, read back with trilinear filtering.+ // It is a smooth function of position, so light taken from it cannot have hard edges however few vertices a surface has.+ const DX = (NX >> 2) + 2, DY = (NY >> 2) + 2, DZ = (NZ >> 2) + 2, dens = new Float32Array(DX * DY * DZ);+ for (let k = 0, q = 0; k < NZ; k++) for (let j = 0; j < NY; j++) for (let i = 0; i < NX; i++, q++) if (grid[q]) dens[(i >> 2) + DX * ((j >> 2) + DY * (k >> 2))] += 1 / 64;+ { const tmp = new Float32Array(dens.length), steps = [1, DX, DX * DY];+ for (let pass = 0; pass < 3; pass++) for (const st of steps) {+ tmp.set(dens);+ for (let q = st; q < dens.length - st; q++) dens[q] = 0.25 * tmp[q - st] + 0.5 * tmp[q] + 0.25 * tmp[q + st];+ } }+ const enclosed = (x, y, z) => {+ const fx = clamp((x - X0) / (CELL * 4), 0, DX - 1.001), fy = clamp((y - Y0) / (CELL * 4), 0, DY - 1.001), fz = clamp((z - Z0) / (CELL * 4), 0, DZ - 1.001);+ const i = fx | 0, j = fy | 0, k = fz | 0, a = fx - i, c = fy - j, d = fz - k, q = i + DX * (j + DY * k), P1 = 1, P2 = DX, P4 = DX * DY;+ const lo = (dens[q] * (1 - a) + dens[q + P1] * a) * (1 - c) + (dens[q + P2] * (1 - a) + dens[q + P2 + P1] * a) * c;+ const hi = (dens[q + P4] * (1 - a) + dens[q + P4 + P1] * a) * (1 - c) + (dens[q + P4 + P2] * (1 - a) + dens[q + P4 + P2 + P1] * a) * c;+ return lo * (1 - d) + hi * d;+ };+ // Distance to the first hit, or 1e9 if the ray leaves the world; capped rays return their cap.+ // A ray from ground beyond the voxels first flies in to them, so the building still shadows the far flagstones.+ const X1 = X0 + NX * CELL, Y1 = Y0 + NY * CELL, Z1 = Z0 + NZ * CELL;+ const march = (ox, oy, oz, dx, dy, dz, cap) => {+ let t = 0; const st = CELL * 0.8, far = CELL * CK * 0.6;+ if (ox < X0 || ox >= X1 || oy < Y0 || oy >= Y1 || oz < Z0 || oz >= Z1) {+ let t0 = 0, t1 = cap;+ for (const [o, d, lo, hi] of [[ox, dx, X0, X1], [oy, dy, Y0, Y1], [oz, dz, Z0, Z1]]) {+ if (Math.abs(d) < 1e-9) { if (o < lo || o >= hi) return 1e9; continue; }+ const a = (lo - o) / d, b = (hi - o) / d; t0 = Math.max(t0, Math.min(a, b)); t1 = Math.min(t1, Math.max(a, b));+ }+ if (t0 >= t1) return 1e9;+ t = t0 + 0.01;+ }+ while (t < cap) {+ const i = ((ox + dx * t - X0) / CELL) | 0, j = ((oy + dy * t - Y0) / CELL) | 0, k = ((oz + dz * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return 1e9;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)]) return t;+ t += st;+ }+ return cap;+ };+ const solidAt = (x, y, z) => { const i = ((x - X0) / CELL) | 0, j = ((y - Y0) / CELL) | 0, k = ((z - Z0) / CELL) | 0; return i >= 0 && i < NX && j >= 0 && j < NY && k >= 0 && k < NZ && grid[i + NX * (j + NY * k)] !== 0; };+ // A vertex's light: [r,g,b] multipliers, plus how much of it was outdoors+ const light = { r: 1, g: 1, b: 1, out: 0, ndl: 0 };+ let sootFloor = 0.56, dampK = 1, indoorK = 1;+ const lightAt = (x, y, z, nx, ny, nz) => {+ // leave the voxels of the surface itself, then look around+ let ox = x + nx * CELL * 1.2, oy = y + ny * CELL * 1.2, oz = z + nz * CELL * 1.2, buried = false;+ if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 2.4; oy = y + ny * CELL * 2.4; oz = z + nz * CELL * 2.4; if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 3.6; oy = y + ny * CELL * 3.6; oz = z + nz * CELL * 3.6; buried = solidAt(ox, oy, oz); } }+ let esc = 0, skyW = 0, sky = 0, zen = 1e9;+ // A ray counts as sky only if it truly leaves the world: a long look down the nave is still indoors+ if (!buried) for (let q = 0; q < 5; q++) {+ const R = SKY_RAYS[q], d = march(ox, oy, oz, R[0], R[1], R[2], 200);+ if (q === 0) zen = d;+ const w = Math.max(0.12, R[0] * nx + R[1] * ny + R[2] * nz + 0.35);+ skyW += w; if (d >= 1e9) { sky += w; esc++; }+ }+ // The underside of a cornice sees no sky above it but is still outdoors: only there is a long look sideways worth its cost+ if (!buried && esc === 0 && ny < -0.2) for (let q = 5; q < SKY_RAYS.length && esc === 0; q++) { const R = SKY_RAYS[q]; if (march(ox, oy, oz, R[0], R[1], R[2], 90) >= 1e9) esc++; }+ const ao = clamp(1 - enclosed(x + nx, y + ny, z + nz) * 2.6), vis = skyW ? sky / skyW : 0, outdoors = esc >= 1;+ let r, g, b;+ if (outdoors) {+ const ndl = nx * SUN[0] + ny * SUN[1] + nz * SUN[2];+ let sunV = 0;+ if (ndl > 0 && !buried) {+ if (march(ox, oy, oz, SUN[0], SUN[1], SUN[2], 200) >= 1e9) sunV += 0.5;+ if (march(ox, oy, oz, SUN2[0][0], SUN2[0][1], SUN2[0][2], 200) >= 1e9) sunV += 0.25;+ if (march(ox, oy, oz, SUN2[1][0], SUN2[1][1], SUN2[1][2], 200) >= 1e9) sunV += 0.25;+ }+ const amb = 0.2 + 0.44 * vis * (0.55 + 0.45 * ao), s = sunV * Math.max(0, ndl) * 1.02;+ r = amb * 0.84 + s * 0.98; g = amb * 0.88 + s * 0.9; b = amb * 0.98 + s * 0.74;+ // What the rain never touches keeps its soot; what it washes stays pale. Gentle on long walls, whose+ // few vertices would smear it: the crisp streaks under their ledges are decals.+ const shelter = zen < 6 ? 1 - zen / 6 : 0, soot = lerp(1, sootFloor, shelter * (1 - Math.max(0, ny)));+ r *= soot; g *= soot * 0.985; b *= soot * 0.96;+ // Rising damp: darker and greener towards the ground, a slow mottling over the height of the wall+ const damp = lerp(1, sstep(0, 9, y), dampK), mot = 0.93 + 0.07 * Math.sin(x * 0.53 + y * 0.31) * Math.sin(z * 0.47 - y * 0.23);+ r *= lerp(0.56, 1, damp) * mot; g *= lerp(0.64, 1, damp) * mot; b *= lerp(0.46, 1, damp) * mot;+ light.out = 1; light.ndl = ndl;+ } else {+ // Inside: warm stone, dark low in the aisles, lifting into the vault. Every term is a smooth function of position,+ // so the light has only gradients: the aisles of the nave, of the transept arms and round the apse fade in softly+ const low = sstep(P.aisleCrown + 2.5, P.aisleCrown - 0.5, y), axis = zn - 0.1, dxc = Math.abs(x - LAY.xc), arm = sstep(16, 13, dxc);+ const naveA = sstep(axis, axis + 2.4, Math.abs(z)) * (1 - arm), armA = sstep(axis, axis + 2.4, dxc) * arm * sstep(13, 16, Math.abs(z));+ const apse = sstep(LAY.xa + 1.5, LAY.xa - 1.5, x), ambA = sstep(axis, axis + 2.4, Math.sqrt((x - LAY.xa) * (x - LAY.xa) + z * z)) * apse;+ const s = lerp(0.32, 0.86, sstep(0.5, 34, y)) * lerp(0.5, 1, ao) * lerp(1, 0.76, Math.max(naveA * (1 - apse), armA, ambA) * low) * indoorK;+ r = s * 1.06; g = s * 0.87; b = s * 0.64;+ light.out = 0; light.ndl = 0;+ }+ light.r = r; light.g = g; light.b = b;+ return light;+ };+ // ---- group placements of each batch by which way they face the sun ----+ const perBatch = new Map();+ for (const e of PLACED) { if (e.batch.noColor || e.batch.isFigure || (e.mat.transparent && !e.batch.fade)) continue; let l = perBatch.get(e.batch); if (!l) { l = []; perBatch.set(e.batch, l); } l.push(e); }+ const entries = [];+ for (const [b, list] of perBatch) {+ const groups = new Map();+ for (const e of list) {+ const m = e.part.matrixWorld.elements, sx = len3(m[0], m[1], m[2]) || 1, sy = len3(m[4], m[5], m[6]) || 1, sz = len3(m[8], m[9], m[10]) || 1;+ // the sun in this placement's own frame (mirror included)+ const lx = (m[0] * SUN[0] + m[1] * SUN[1] + m[2] * SUN[2]) / (sx * sx), ly = (m[4] * SUN[0] + m[5] * SUN[1] + m[6] * SUN[2]) / (sy * sy), lz = (m[8] * SUN[0] + m[9] * SUN[1] + m[10] * SUN[2]) / (sz * sz);+ const key = Math.round(lx * 3) + ',' + Math.round(ly * 3) + ',' + Math.round(lz * 3);+ let g = groups.get(key); if (!g) { g = { key, list: [], L: [lx, ly, lz] }; groups.set(key, g); } g.list.push(e);+ }+ const gl = [...groups.values()];+ for (const g of gl) { // how much sun this side receives, by area of normals+ let s = 0; const nn = b.N; for (let i = 0; i < nn.length; i += 3) s += Math.max(0, nn[i] * g.L[0] + nn[i + 1] * g.L[1] + nn[i + 2] * g.L[2]);+ g.expo = s / Math.max(1, nn.length / 3);+ }+ gl.sort((a, c) => c.expo - a.expo);+ entries.push({ b, gl, wall: list[0].mat === MAT.ashlar ? 0 : 1 });+ }+ // Long walls get their own light for each side first: they carry the sense of sun and shade from a distance+ entries.sort((a, c) => a.wall - c.wall || a.b.count - c.b.count);+ let spent = 0, variants = 0, tinted = 0, vbaked = 0;+ const TINTS = new Map();+ const tintOf = (mat, tier) => {+ const key = mat.uuid + '|' + tier; let m = TINTS.get(key);+ if (!m) { m = mat.clone(); m.name = mat.name + (tier === 1 ? '_ShadeSide' : '_DeepShadeSide'); m.color.setRGB(tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.76 : 0.53); TINTS.set(key, m); }+ return m;+ };+ const bakeGroup = (b, g) => {+ const rep = g.list[Math.floor(g.list.length / 2)], m = rep.part.matrixWorld.elements, n = b.count, out = new Uint8Array(n * 3), PP = b.P, NN = b.N, CC = b.C;+ let outdoor = 0;+ const onGround = rep.mat === MAT.flag || rep.mat === MAT.earth;+ sootFloor = onGround ? 1 : rep.mat === MAT.ashlar ? 0.84 : rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0.9 : 0.56;+ dampK = onGround || rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0 : 1;+ indoorK = rep.mat === MAT.oak ? 2.2 : 1; // the oak was given its own dark tone when it was built+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, x = PP[i3], y = PP[i3 + 1], z = PP[i3 + 2], a = NN[i3], c = NN[i3 + 1], d = NN[i3 + 2];+ const wx = m[0] * x + m[4] * y + m[8] * z + m[12], wy = m[1] * x + m[5] * y + m[9] * z + m[13], wz = m[2] * x + m[6] * y + m[10] * z + m[14];+ let nx = m[0] * a + m[4] * c + m[8] * d, ny = m[1] * a + m[5] * c + m[9] * d, nz = m[2] * a + m[6] * c + m[10] * d; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1; nx /= l; ny /= l; nz /= l;+ const L = lightAt(wx, wy, wz, nx, ny, nz);+ outdoor += L.out;+ out[i3] = Math.min(255, CC[i3] * L.r); out[i3 + 1] = Math.min(255, CC[i3 + 1] * L.g); out[i3 + 2] = Math.min(255, CC[i3 + 2] * L.b);+ }+ // Each vertex is one noisy sample of light: blend neighbours on the same surface so no sample can smear a dark+ // triangle across the stone. Edges between planes keep their contrast.+ {+ const sum = new Float32Array(n * 3), w = new Float32Array(n), I = b.I, passes = rep.mat === MAT.ashlar ? 3 : 2;+ const link = (p, q) => { if (NN[p * 3] * NN[q * 3] + NN[p * 3 + 1] * NN[q * 3 + 1] + NN[p * 3 + 2] * NN[q * 3 + 2] < 0.8) return; w[p]++; w[q]++; for (let c = 0; c < 3; c++) { sum[p * 3 + c] += out[q * 3 + c]; sum[q * 3 + c] += out[p * 3 + c]; } };+ for (let pass = 0; pass < passes; pass++) {+ sum.fill(0); w.fill(0);+ for (let t = 0; t < I.length; t += 3) { link(I[t], I[t + 1]); link(I[t + 1], I[t + 2]); link(I[t + 2], I[t]); }+ for (let i = 0; i < n; i++) if (w[i]) for (let c = 0; c < 3; c++) out[i * 3 + c] = out[i * 3 + c] * 0.4 + 0.6 * sum[i * 3 + c] / w[i];+ }+ }+ vbaked += n;+ return { colors: out, outdoor: outdoor / Math.max(1, n) };+ };+ for (const { b, gl } of entries) {+ const base = b.toGeo(), cost = (gl.length - 1) * b.count * 12, full = gl.length === 1 || spent + cost <= P.lightVariantBytes;+ if (full) spent += cost;+ gl.forEach((g, gi) => {+ if (gi === 0 || full) {+ const R = bakeGroup(b, g);+ g.outdoor = R.outdoor;+ if (gi === 0 && b.fade) { // a surface that dissolves at its edge carries its opacity with its light+ const rgba = new Float32Array(b.count * 4);+ for (let i = 0; i < b.count; i++) { rgba[i * 4] = R.colors[i * 3] / 255; rgba[i * 4 + 1] = R.colors[i * 3 + 1] / 255; rgba[i * 4 + 2] = R.colors[i * 3 + 2] / 255; rgba[i * 4 + 3] = b.fade(b.P[i * 3], b.P[i * 3 + 2]); }+ base.setAttribute('color', new THREE.BufferAttribute(rgba, 4));+ return;+ }+ if (gi === 0) { base.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true)); return; }+ const v = new THREE.BufferGeometry();+ v.setAttribute('position', base.attributes.position); v.setAttribute('normal', base.attributes.normal); v.setAttribute('uv', base.attributes.uv);+ v.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true));+ if (base.attributes.tangent) v.setAttribute('tangent', base.attributes.tangent);+ v.setIndex(base.index);+ for (const e of g.list) e.part.geometry = v;+ variants++;+ } else {+ // over budget: same colours, a darker stone. Skipped for batches that are mostly indoors+ tinted++;+ if (gl[0].outdoor < 0.3) return;+ const ratio = g.expo / Math.max(1e-3, gl[0].expo);+ if (ratio >= 0.78) return;+ const tier = ratio >= 0.45 ? 1 : 2;+ for (const e of g.list) e.part.material = tintOf(e.mat, tier);+ }+ });+ }+ // ---- figures keep their carved occlusion; the niche they stand in decides how much daylight reaches them ----+ let figN = 0;+ for (const f of FIG_PLACED) {+ const m = f.part.matrixWorld.elements, s = len3(m[0], m[1], m[2]) || 1, fx = m[0] / s, fy = m[1] / s, fz = m[2] / s;+ let ox = m[12] + m[4] * 1.3, oy = m[13] + m[5] * 1.3, oz = m[14] + m[6] * 1.3;+ for (let i = 0; i < 12 && solidAt(ox, oy, oz); i++) { ox += fx * CELL; oy += fy * CELL; oz += fz * CELL; }+ const L = lightAt(ox - fx * 0.05, oy - fy * 0.05, oz - fz * 0.05, fx, fy, fz);+ let mat = MAT.figureIn;+ if (L.out) { const lum = (L.r + L.g + L.b) / 3, facing = fx * SUN[0] + fy * SUN[1] + fz * SUN[2]; mat = lum > 0.56 ? MAT.figure : lum > 0.3 ? MAT.figureHalf : MAT.figureDeep; if (facing < -0.3 && mat === MAT.figure) mat = MAT.figureHalf; }+ f.part.material = mat; if (f.head) f.head.material = mat;+ figN++;+ }+ const windowLight = bakeWindowLight({ SUN, grid, coarse, NX, NY, NZ, CX, CY, X0, Y0, Z0, CELL, CK });+ return { vertices: vbaked, variants, tinted, extraKB: Math.round(spent / 1024), figures: figN, windowLight };+}++// ==================================================================================+// THE LIGHT OF THE SOUTH WINDOWS, baked from the same sun as the stone. For each point of the floor, and each lit face of the north+// piers, a small cone of rays goes back towards P.sun. A ray that clears the arcade and everything else indoors and reaches a south+// aisle window takes the colour of that window's panes at the point it arrives. Otherwise it brings nothing. The image is one mesh:+// a quad on the paving and a skin on each lit pier. The penumbra grows with distance, so the far panes melt and near piers cut clean.+// ==================================================================================+const WINDOW_LIGHT = { W: 512, H: 172, FH: 124, skinTop: 6.4, coneDeg: 1.2, tex: null };+const SUN_WINDOWS = [];+const inPoly = (pts, x, y) => { let c = false; for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) { const a = pts[i], b = pts[j]; if ((a[1] > y) !== (b[1] > y) && x < ((b[0] - a[0]) * (y - a[1])) / (b[1] - a[1]) + a[0]) c = !c; } return c; };+const FINE = 0.05; // metres per pixel of a window's cartoon+let TRACERY_MASK = null;+function traceryMask(nx, ny, w, y0) { // what the stone bars and the moulded frame cover, seen straight on+ if (TRACERY_MASK) return TRACERY_MASK;+ const t = new Batch(1), mask = new Uint8Array(nx * ny);+ addTracery(t, WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise, { bar: 0.25, depth: 0.42 });+ t.sweep(archOutline(WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ const Q = t.P, I = t.I;+ for (let f = 0; f < I.length; f += 3) {+ const ax = Q[I[f] * 3], ay = Q[I[f] * 3 + 1], bx = Q[I[f + 1] * 3], by = Q[I[f + 1] * 3 + 1], cx = Q[I[f + 2] * 3], cy = Q[I[f + 2] * 3 + 1];+ const d = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax); if (Math.abs(d) < 1e-7) continue;+ const i0 = Math.max(0, Math.floor((Math.min(ax, bx, cx) + w / 2) / FINE)), i1 = Math.min(nx - 1, Math.ceil((Math.max(ax, bx, cx) + w / 2) / FINE)), j0 = Math.max(0, Math.floor((Math.min(ay, by, cy) - y0) / FINE)), j1 = Math.min(ny - 1, Math.ceil((Math.max(ay, by, cy) - y0) / FINE));+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) {+ const px = (i + 0.5) * FINE - w / 2, py = (j + 0.5) * FINE + y0, e0 = ((bx - ax) * (py - ay) - (by - ay) * (px - ax)) / d, e1 = ((cx - bx) * (py - by) - (cy - by) * (px - bx)) / d, e2 = ((ax - cx) * (py - cy) - (ay - cy) * (px - cx)) / d;+ if (e0 >= 0 && e1 >= 0 && e2 >= 0) mask[i + nx * j] = 1;+ }+ }+ TRACERY_MASK = mask;+ return mask;+}+// The window as light passes through it: its panes in their real colours and order, the lead black, the stone bars laid over,+// averaged down to 10 cm in linear light+function windowTransmission(batch) {+ if (batch.transmission) return batch.transmission;+ const w = WIN.aW, y0 = WIN.aSill, top = WIN.aSp + WIN.aRise, nx = Math.ceil(w / FINE), ny = Math.ceil((top - y0) / FINE), fine = new Uint8Array(nx * ny * 3), famRGB = {};+ for (const f of GLASS_FAMILIES) famRGB[f[0]] = f[1];+ const order = batch.panes.map((p, i) => [p, i]).sort((a, c) => a[0][2] - c[0][2] || a[1] - c[1]);+ for (const [pane] of order) {+ const pts = pane[0], rgb = famRGB[pane[1]] || [0, 0, 0];+ let bx0 = 1e9, bx1 = -1e9, by0 = 1e9, by1 = -1e9; for (const p of pts) { bx0 = Math.min(bx0, p[0]); bx1 = Math.max(bx1, p[0]); by0 = Math.min(by0, p[1]); by1 = Math.max(by1, p[1]); }+ const i0 = Math.max(0, Math.floor((bx0 + w / 2) / FINE)), i1 = Math.min(nx - 1, Math.ceil((bx1 + w / 2) / FINE)), j0 = Math.max(0, Math.floor((by0 - y0) / FINE)), j1 = Math.min(ny - 1, Math.ceil((by1 - y0) / FINE));+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) if (inPoly(pts, (i + 0.5) * FINE - w / 2, (j + 0.5) * FINE + y0)) { const q = (i + nx * j) * 3; fine[q] = rgb[0]; fine[q + 1] = rgb[1]; fine[q + 2] = rgb[2]; }+ }+ const stone = traceryMask(nx, ny, w, y0), lin = new Float32Array(256); for (let i = 0; i < 256; i++) lin[i] = Math.pow(i / 255, 2.2);+ const mx = nx >> 1, my = ny >> 1, T = new Float32Array(mx * my * 3);+ for (let j = 0; j < my; j++) for (let i = 0; i < mx; i++) for (let c = 0; c < 3; c++) {+ let s = 0; for (let b = 0; b < 2; b++) for (let a = 0; a < 2; a++) { const q = 2 * i + a + nx * (2 * j + b); if (!stone[q]) s += lin[fine[q * 3 + c]]; }+ T[(i + mx * j) * 3 + c] = s / 4;+ }+ batch.transmission = { T, mx, my, cell: FINE * 2 };+ return batch.transmission;+}+function bakeWindowLight(V) {+ const { SUN, grid, coarse, NX, NY, NZ, CX, CY, X0, Y0, Z0, CELL, CK } = V, { xTowerE, xNaveE, xChoirW, xc } = LAY, WL = WINDOW_LIGHT, data = WL.tex.image.data, W = WL.W, H = WL.H, FH = WL.FH;+ const w = WIN.aW, sill = WIN.aSill, ys = WIN.aSp, rise = WIN.aRise, top = ys + rise, archC = (rise * rise - w * w / 4) / w, archR = archC + w / 2, zW = -(zaw + 0.45), zIn = -zaw + 0.35, wallHalf = 0.45;+ const openAt = (u, y) => y >= sill && y <= top && Math.abs(u) <= w / 2 && (y <= ys || Math.abs(u) + archC <= Math.sqrt(archR * archR - (y - ys) * (y - ys)));+ const wins = []; for (const s of SUN_WINDOWS) wins[Math.round((xTowerE - s.x) / BAY - 0.5)] = { x: s.x, L: windowTransmission(s.batch) };+ // A small cone of rays round the sun, four rotations of it, so the sampling turns into fine grain that the blur removes+ const a0 = (() => { const l = Math.hypot(SUN[2], SUN[0]); return [-SUN[2] / l, 0, SUN[0] / l]; })(), b0 = [SUN[1] * a0[2] - SUN[2] * a0[1], SUN[2] * a0[0] - SUN[0] * a0[2], SUN[0] * a0[1] - SUN[1] * a0[0]], cone = Math.tan(rad(WL.coneDeg)), NR = 12;+ const dirs = [0, 1, 2, 3].map((k) => { const out = []; for (let n = 0; n < NR; n++) { const r = cone * Math.sqrt((n + 0.5) / NR), th = n * 2.39996 + k * 0.5236, ca = r * Math.cos(th), sa = r * Math.sin(th), x = SUN[0] + a0[0] * ca + b0[0] * sa, y = SUN[1] + a0[1] * ca + b0[1] * sa, z = SUN[2] + a0[2] * ca + b0[2] * sa, l = Math.hypot(x, y, z), pl = Math.hypot(x, z) / l; out.push({ d: [x / l, y / l, z / l], pl, sx: x / l / pl, sz: z / l / pl }); } return out; });+ // The piers, exactly: shaft, plinth, and the corbel, statue and canopy on the nave face. Height decides which of them a ray meets+ const hits = (R, px, py, pz, cx, cz, r, yLo, yHi) => { const dx = cx - px, dz = cz - pz, along = dx * R.sx + dz * R.sz; if (along < 0 && dx * dx + dz * dz > r * r) return false; if (Math.abs(dx * R.sz - dz * R.sx) >= r) return false; const y = py + R.d[1] * Math.max(along, 0) / R.pl; return y >= yLo && y <= yHi; };+ const crossing = [[xNaveE, -zn], [xNaveE, zn], [xChoirW, -zn], [xChoirW, zn]];+ const pierBlocks = (R, px, py, pz, skipK) => {+ for (let s = 0; s < 2; s++) {+ const zA = s ? zn : -zn; if (pz < zA - 1.8) continue; // the ray runs towards -z: an arcade north of this point is behind it+ const xA = px + R.sx * (zA - pz) / R.sz, k0 = Math.round((xTowerE - xA) / BAY);+ for (let k = k0 - 1; k <= k0 + 1; k++) {+ if (k < -1 || k >= P.naveBays || (s === 1 && k === skipK)) continue;+ const cx = xTowerE - k * BAY;+ if (hits(R, px, py, pz, cx, zA, 1.02, -1, P.pierTop - 0.8) || hits(R, px, py, pz, cx, zA, 1.62, -1, 0.62) || (k >= 0 && hits(R, px, py, pz, cx, zA + (s ? -1.14 : 1.14), 0.42, 5, 10.2))) return true;+ }+ }+ for (const c of crossing) if (hits(R, px, py, pz, c[0], c[1], 1.3, -1, P.vaultSpring)) return true;+ return false;+ };+ const stoneBlocks = (d, px, py, pz, t0, t1) => { // voxels of real stone between here and the inner face of the wall: the shafts are tested above+ const st = CELL * 0.8, far = CELL * CK * 0.6; let t = t0;+ while (t < t1) {+ const i = ((px + d[0] * t - X0) / CELL) | 0, j = ((py + d[1] * t - Y0) / CELL) | 0, k = ((pz + d[2] * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return false;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)] === 1) return true;+ t += st;+ }+ return false;+ };+ // One ray: adds what it brings to acc. By the rule asked for, the exterior is not tested: the light is whatever comes through the glass,+ // though the buttresses outside would really shade nearly all of it from this sun (measured: under 1% of these rays would clear them)+ const acc = [0, 0, 0];+ const trace = (R, px, py, pz, t0, skipK) => {+ const d = R.d, tW = (zW - pz) / d[2]; if (tW <= 0) return;+ const xw = px + d[0] * tW, yw = py + d[1] * tW, win = wins[Math.round((xTowerE - xw) / BAY - 0.5)]; if (!win) return;+ const u = xw - win.x, L = win.L, ix = ((u + w / 2) / L.cell) | 0, iy = ((yw - sill) / L.cell) | 0; if (u < -w / 2 || ix >= L.mx || yw < sill || iy >= L.my) return;+ const q = (ix + L.mx * iy) * 3, T = L.T; if (T[q] + T[q + 1] + T[q + 2] <= 0) return;+ const reveal = wallHalf / -d[2]; if (!openAt(u - d[0] * reveal, yw - d[1] * reveal) || !openAt(u + d[0] * reveal, yw + d[1] * reveal)) return; // the wall is 0.9 m thick+ if (pierBlocks(R, px, py, pz, skipK)) return;+ if (stoneBlocks(d, px, py, pz, t0, (zIn - pz) / d[2])) return;+ acc[0] += T[q]; acc[1] += T[q + 1]; acc[2] += T[q + 2];+ };+ // ---- The floor ----+ const x0 = xc - zaw - 0.1, x1 = xTowerE, z0 = -8, z1 = zaw, yF = 0.08, L = new Float32Array(W * H * 3), tFloor = (0.46 - yF) / SUN[1];+ for (let j = 0; j < FH; j++) for (let i = 0; i < W; i++) {+ const x = lerp(x0, x1, (i + 0.5) / W), z = lerp(z0, z1, (j + 0.5) / FH), set = dirs[(i + 2 * j) & 3];+ acc[0] = acc[1] = acc[2] = 0;+ for (const R of set) trace(R, x, yF, z, tFloor, -99);+ const q = (i + W * j) * 3; L[q] = acc[0] / NR; L[q + 1] = acc[1] / NR; L[q + 2] = acc[2] / NR;+ }+ // ---- The north piers: the same rays stop on their faces, up to about six metres. A thin skin on each shaft takes them, one cell of the image a pier ----+ const ring = SHARED.pierRing, RN = ring.length, cellW = Math.floor(W / Math.max(1, P.naveBays)), cellH = H - FH - 3, span = rad(210), aS = Math.atan2(SUN[2], SUN[0]), skinTop = WL.skinTop;+ const ringAt = (a) => { const f = ((a / TAU) % 1 + 1) % 1 * RN, i = f | 0, t = f - i, A = ring[i % RN], B = ring[(i + 1) % RN]; return [lerp(A[0], B[0], t), lerp(A[1], B[1], t), lerp(A[2], B[2], t), lerp(A[3], B[3], t)]; };+ const kAt = (y) => (y < 0.86 ? lerp(1.24, 1.27, clamp((y - 0.62) / 0.24)) : y < 1.15 ? lerp(1.27, 1, (y - 0.86) / 0.29) : 1);+ const shaftAt = (a, y) => { const r = ringAt(a), k = kAt(y), l = Math.hypot(r[2], r[3]) || 1, nx = r[2] / l, nz = r[3] / l; return [r[0] * k + nx * 0.03, y, r[1] * k + nz * 0.03, nx, nz]; };+ const plinthAt = (a, y) => { const f = Math.round(a / (PI / 4)) * (PI / 4), r = (1.7 * Math.cos(PI / 8)) / Math.cos(a - f) + 0.03; return [r * Math.cos(a), y, r * Math.sin(a), Math.cos(f), Math.sin(f)]; }; // the octagonal foot of the pier+ const skinAt = (a, y) => { if (y <= 0.56) return plinthAt(a, y); if (y >= 0.62) return shaftAt(a, y); const p = plinthAt(a, 0.56), s = shaftAt(a, 0.62), t = (y - 0.56) / 0.06; return [lerp(p[0], s[0], t), y, lerp(p[2], s[2], t), 0, 0].map((v, i) => (i < 3 ? v : i === 3 ? p[3] : p[4])); };+ const skinLit = [];+ for (let k = 0; k < P.naveBays; k++) {+ const cx = xTowerE - k * BAY; let peak = 0;+ for (let j = 0; j < cellH; j++) for (let i = 0; i < cellW - 2; i++) {+ const a = aS + ((i + 0.5) / (cellW - 2) - 0.5) * span, y = ((j + 0.5) / cellH) * skinTop, p = skinAt(a, y), cosN = p[3] * SUN[0] + p[4] * SUN[2]; if (cosN <= 0.03) continue;+ const set = dirs[(i + 2 * j) & 3]; acc[0] = acc[1] = acc[2] = 0;+ for (const R of set) trace(R, cx + p[0], y, zn + p[2], 0.7, k);+ const q = (k * cellW + 1 + i + W * (FH + 3 + j)) * 3; L[q] = (acc[0] / NR) * cosN; L[q + 1] = (acc[1] / NR) * cosN; L[q + 2] = (acc[2] / NR) * cosN;+ peak = Math.max(peak, L[q], L[q + 1], L[q + 2]);+ }+ if (peak > 0.004) skinLit.push(k);+ }+ // ---- Soften once more and write the image: the hue of the glass over a little of the stone's own tone, opacity rising quickly+ // with the light and then levelling off, so deep ruby and cobalt tell as clearly as the pale panes and the paving always reads through ----+ const tmp = new Float32Array(L.length);+ for (const step of [3, W * 3]) {+ tmp.set(L);+ for (let q = step; q < L.length - step; q++) L[q] = 0.25 * tmp[q - step] + 0.5 * tmp[q] + 0.25 * tmp[q + step];+ }+ // The eye reads shape from the opacity, so the hue can vary more slowly: softened further and held to five bits, it costs far fewer bytes+ const Hue = Float32Array.from(L);+ for (let pass = 0; pass < 2; pass++) for (const step of [3, W * 3]) {+ tmp.set(Hue);+ for (let q = step; q < Hue.length - step; q++) Hue[q] = 0.25 * tmp[q - step] + 0.5 * tmp[q] + 0.25 * tmp[q + step];+ }+ const srgb = (v) => Math.min(248, (Math.round(255 * Math.pow(clamp(v), 1 / 2.2)) + 4) & 0xf8), gain = 1.45, peakOpacity = 0.52, floorTone = [0.045, 0.044, 0.042], pierTone = [0.07, 0.056, 0.035];+ for (let j = 0; j < H; j++) for (let i = 0; i < W; i++) {+ const q = (i + W * j) * 3, o = (i + W * j) * 4, tone = j < FH ? floorTone : pierTone, r = L[q] * gain, g = L[q + 1] * gain, b = L[q + 2] * gain, m = Math.max(r, g, b);+ if (m < 0.002) { data[o] = srgb(tone[0]); data[o + 1] = srgb(tone[1]); data[o + 2] = srgb(tone[2]); data[o + 3] = 0; continue; }+ const a = peakOpacity * (1 - Math.exp(-m / 0.12)), hr = Hue[q], hg = Hue[q + 1], hb = Hue[q + 2], k = 0.62 / (Math.max(hr, hg, hb) || 1);+ data[o] = srgb(tone[0] + hr * k); data[o + 1] = srgb(tone[1] + hg * k); data[o + 2] = srgb(tone[2] + hb * k); data[o + 3] = (Math.round(255 * a) + 2) & 0xfc; // six bits of opacity: the filtering smooths the steps away+ }+ WL.tex.needsUpdate = true;+ // ---- The mesh: one quad on the paving, one skin on each pier that receives anything ----+ const B = new Batch(1); B.noColor = true;+ const vF = FH / H, q0 = B.v(x0, yF, z1, 0, 1, 0, 0, vF), q1 = B.v(x1, yF, z1, 0, 1, 0, 1, vF), q2 = B.v(x1, yF, z0, 0, 1, 0, 1, 0), q3 = B.v(x0, yF, z0, 0, 1, 0, 0, 0);+ B.quad(q0, q1, q2, q3);+ const rows = [yF + 0.01, 0.56, 0.62, 0.86, 1.15, skinTop], cols = 20;+ for (const k of skinLit) {+ const cx = xTowerE - k * BAY, base = B.count;+ for (let c = 0; c <= cols; c++) for (const y of rows) {+ const a = aS + (c / cols - 0.5) * span, p = skinAt(a, y), u = (k * cellW + 1 + (c / cols) * (cellW - 2)) / W, v = (FH + 3 + (y / skinTop) * cellH) / H;+ B.v(cx + p[0], y, zn + p[2], p[3], 0, p[4], u, v);+ }+ const nr = rows.length;+ for (let c = 0; c < cols; c++) for (let r = 0; r < nr - 1; r++) { const a = base + c * nr + r; B.quad(a, a + 1, a + nr + 1, a + nr); }+ }+ place('StainedLight_Nave', B, MAT.pool, { parent: ROOT.children.find((c) => c.name === 'Nave') });+ return { skins: skinLit.length };+}++// <<ORNAMENT>>++// <<BUILD>>+async function build() {+ ROOT = createRoot('Cathedral');+ await makeMaterials();+ await prepareFigures(P.preview === 'statues' ? P.statueTest : Object.keys(FIGURES));+ if (P.preview === 'statues') { previewStatues(); return ROOT; }+ if (P.preview === 'all' || P.preview === 'unique') {+ buildCathedral();+ await reduceBatches();+ bakeLight();+ return ROOT;+ }+ if (P.preview === 'size') { // builds and bakes everything, then reports bytes as part names, and exports only a probe: fast+ buildCathedral();+ await reduceBatches();+ const baked = bakeLight(), report = ['bake_variants' + baked.variants + '_tinted' + baked.tinted + '_extraKB' + baked.extraKB];+ // Real buffer sizes, each attribute counted once: pos/normal 12, uv 8, colour 12, tangent 16 a vertex where normal-mapped+ const seenAttr = new Set(), seenTan = new Set(), byMat = {}; let bytes = 0, tris = 0;+ const add = (attr, per, key) => { if (!attr || seenAttr.has(attr)) return; seenAttr.add(attr); const b = attr.count * per; bytes += b; byMat[key] = (byMat[key] || 0) + b; };+ for (const e of PLACED) {+ const g = e.part.geometry, key = e.mat.name, a = g.attributes, m = e.part.material;+ tris += g.index.count / 3;+ add(a.position, 12, key); add(a.normal, 12, key); add(a.uv, 8, key); add(a.color, 12, key);+ add(g.index, g.index.array.BYTES_PER_ELEMENT, key);+ if (m && m.normalMap && !seenTan.has(a.position)) { seenTan.add(a.position); bytes += a.position.count * 16; byMat[key] = (byMat[key] || 0) + a.position.count * 16; }+ }+ report.push('geomKB' + Math.round(bytes / 1024) + '_meshes' + PLACED.length + '_rendered' + Math.round(tris));+ Object.keys(byMat).sort((p, q) => byMat[q] - byMat[p]).forEach((k) => report.push(k + '_KB' + Math.round(byMat[k] / 1024)));+ ROOT.clear();+ createPart('SizeProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ report.forEach((d) => group('SIZE_' + d));+ return ROOT;+ }+ if (P.preview === 'textures') { // measures the texture payload on its own+ Object.keys(MAT).forEach((k, i) => createPart('Swatch_' + k, boxGeo(1, 1, 1), MAT[k], { position: [i * 1.2, 0.5, 0], parent: ROOT }));+ return ROOT;+ }+ if (P.preview === 'stats') {+ buildCathedral();+ const list = [...STAT.entries()].map(([b, e]) => ({ name: e.name, n: e.n, tris: b.I.length / 3, kb: Math.round((batchBytes(b) + (e.normalMapped ? b.count * 16 : 0)) / 1024) })).sort((a, c) => c.kb - a.kb);+ let total = 0, draws = 0, rendered = 0; for (const e of list) { total += e.kb; draws += e.n; rendered += e.n * e.tris; }+ createPart('StatProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ group('STAT_TOTAL_kb_' + total + '_draws_' + draws + '_rendered_' + rendered + '_unique_' + list.length);+ list.slice(0, 40).forEach((e, i) => group('STAT_' + String(i).padStart(2, '0') + '_K' + Math.round(e.n * e.tris / 1000) + '_' + e.name + '_x' + e.n + '_tris' + e.tris + '_kb' + e.kb));+ return ROOT;+ }+ return ROOT;+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-6.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/cathedral-6.kiln.jsnew file mode 100644index 00000000..f662cf7c--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/cathedral-6.kiln.js@@ -0,0 +1,4240 @@+// Gothic cathedral: parametric High Gothic church, true scale, metres.+// +X = west front (entrance) , -X = apse, +Y up, +Z = north.+const meta = { name: 'Gothic Cathedral', role: 'wonder' };++// ==================================================================================+// PARAMETERS: change these and the whole design follows.+// ==================================================================================+const P = {+ preview: 'all', // 'all', or 'size' (true buffer bytes, fast), 'stats' (per-mesh list), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)+ // Plan+ naveBays: 11, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,+ xWest: 62, // west face of the towers+ towerDepth: 14, towerWidth: 14,+ choirBays: 3, transeptBays: 2,+ apseSides: 7,+ // Elevation+ pierTop: 15, aisleCrown: 21, triforiumSill: 21.8, triforiumTop: 26.6,+ vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,+ aisleWallTop: 23, aisleRoofHigh: 26.2,+ towerTop: 72, spireTip: 80,+ flecheTip: 100, // top of the cross on the lead spire over the crossing+ // Detail+ jambStatues: 5, kings: 16, seed: 1163,+ archFigureTris: 720, statueTest: ['apostle', 'bishop', 'king', 'queen', 'angel', 'virgin'],+ storyWindows: 3, clerestoryWindows: 3, // unique window cartoons (more = richer, larger GLB)+ // Glass+ glassGlow: 1.05, glassOuterGlow: 0.12,+ sun: [0.11, 0.79, -0.61], lightVariantBytes: 250000, wallTessellation: 5, // baked sun, about 52 degrees up and 10 degrees west of due south; bytes allowed for per-side vertex light; metres between lit wall vertices+ chapelFacets: 5,+};++// ==================================================================================+// MATH+// ==================================================================================+const PI = Math.PI, TAU = PI * 2, rad = (d) => d * PI / 180;+const lerp = (a, b, t) => a + (b - a) * t;+const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));+const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };+const gauss = (x, y = 0) => Math.exp(-(x * x + y * y));+function mkRng(seed) { let s = seed >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }+function keyLin(keys, t) {+ if (t <= keys[0][0]) return keys[0].slice(1);+ for (let i = 1; i < keys.length; i++) if (t <= keys[i][0]) {+ const a = keys[i - 1], b = keys[i], f = (t - a[0]) / (b[0] - a[0]);+ return a.slice(1).map((v, k) => lerp(v, b[k + 1], f));+ }+ return keys[keys.length - 1].slice(1);+}+function keyf(keys, t, w = 0.03) { // smoothed keyframes [[t,v...],...]: no plateaus or kinks at the keys+ const a = keyLin(keys, t - w), b = keyLin(keys, t), c = keyLin(keys, t + w);+ return b.map((v, k) => (a[k] + 2 * v + c[k]) / 4);+}+const M = {+ I: () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],+ T: (x, y, z) => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z],+ S: (x, y = x, z = x) => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0],+ Rx: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0]; },+ Ry: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0]; },+ Rz: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0]; },+ mul: (a, b) => [+ a[0] * b[0] + a[1] * b[4] + a[2] * b[8], a[0] * b[1] + a[1] * b[5] + a[2] * b[9], a[0] * b[2] + a[1] * b[6] + a[2] * b[10], a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3],+ a[4] * b[0] + a[5] * b[4] + a[6] * b[8], a[4] * b[1] + a[5] * b[5] + a[6] * b[9], a[4] * b[2] + a[5] * b[6] + a[6] * b[10], a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7],+ a[8] * b[0] + a[9] * b[4] + a[10] * b[8], a[8] * b[1] + a[9] * b[5] + a[10] * b[9], a[8] * b[2] + a[9] * b[6] + a[10] * b[10], a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11],+ ],+ of: (...ms) => ms.reduce((a, b) => M.mul(a, b)),+};+function normalMat(m) { // inverse transpose of the 3x3, up to scale (cofactors)+ return [+ m[5] * m[10] - m[6] * m[9], m[6] * m[8] - m[4] * m[10], m[4] * m[9] - m[5] * m[8],+ m[2] * m[9] - m[1] * m[10], m[0] * m[10] - m[2] * m[8], m[1] * m[8] - m[0] * m[9],+ m[1] * m[6] - m[2] * m[5], m[2] * m[4] - m[0] * m[6], m[0] * m[5] - m[1] * m[4],+ ];+}+const det3 = (m) => m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8]) + m[2] * (m[4] * m[9] - m[5] * m[8]);++// ==================================================================================+// MESH KIT: one Batch = one mesh with one material; metric UVs, baked shading in COLOR_0+// ==================================================================================+class Batch {+ constructor(tile = 1) { this.P = []; this.N = []; this.UV = []; this.C = []; this.I = []; this.tile = tile; this.setM(M.I()); this.sh = 1; this.rgb = [1, 1, 1]; this.geo = null; }+ setM(m) { this.m = m; this.nm = normalMat(m); this.det = det3(m); }+ at(m, f) { const old = this.m; this.setM(M.mul(old, m)); f(this); this.setM(old); return this; }+ shade(s, f) { const o = this.sh; this.sh = o * s; f(this); this.sh = o; return this; }+ tint(rgb, f) { const o = this.rgb; this.rgb = rgb; f(this); this.rgb = o; return this; }+ get count() { return this.P.length / 3; }+ v(x, y, z, nx, ny, nz, u, v, s = 1) {+ const m = this.m, n = this.nm;+ const wx = m[0] * x + m[1] * y + m[2] * z + m[3], wy = m[4] * x + m[5] * y + m[6] * z + m[7], wz = m[8] * x + m[9] * y + m[10] * z + m[11];+ let ax = n[0] * nx + n[1] * ny + n[2] * nz, ay = n[3] * nx + n[4] * ny + n[5] * nz, az = n[6] * nx + n[7] * ny + n[8] * nz;+ const l = Math.hypot(ax, ay, az);+ if (l < 1e-10) { ax = 0; ay = 1; az = 0; } else { ax /= l; ay /= l; az /= l; }+ if (this.det < 0) { ax = -ax; ay = -ay; az = -az; }+ if (u === undefined || u === null) {+ const fx = Math.abs(ax), fy = Math.abs(ay), fz = Math.abs(az);+ if (fy > 0.75) { u = wx; v = wz; } else if (fx > fz) { u = wz; v = wy; } else { u = wx; v = wy; }+ }+ const sh = clamp(s * this.sh); // all light is added later by bakeLight+ const rgb = this.rgb;+ this.P.push(wx, wy, wz); this.N.push(ax, ay, az); this.UV.push(u / this.tile, v / this.tile);+ this.C.push(Math.round(255 * sh * rgb[0]), Math.round(255 * sh * rgb[1]), Math.round(255 * sh * rgb[2]));+ return this.P.length / 3 - 1;+ }+ tri(a, b, c) { if (this.det < 0) this.I.push(a, c, b); else this.I.push(a, b, c); }+ quad(a, b, c, d) { this.tri(a, b, c); this.tri(a, c, d); }+ // flat polygon from 3D points (CCW seen from the front); convex fan+ face(pts, s = 1) {+ const a = pts[0], b = pts[1], c = pts[pts.length - 1];+ let nx = (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), ny = (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), nz = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ if (pts.length > 3 && Math.hypot(nx, ny, nz) < 1e-9) { const d = pts[2]; nx = (b[1] - a[1]) * (d[2] - a[2]) - (b[2] - a[2]) * (d[1] - a[1]); ny = (b[2] - a[2]) * (d[0] - a[0]) - (b[0] - a[0]) * (d[2] - a[2]); nz = (b[0] - a[0]) * (d[1] - a[1]) - (b[1] - a[1]) * (d[0] - a[0]); }+ const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l;+ const ids = pts.map((p) => this.v(p[0], p[1], p[2], nx, ny, nz, undefined, undefined, p[3] === undefined ? s : p[3]));+ for (let i = 1; i < ids.length - 1; i++) this.tri(ids[0], ids[i], ids[i + 1]);+ return this;+ }+ box(x0, x1, y0, y1, z0, z1, o = {}) {+ const s = o.s === undefined ? 1 : o.s, sb = o.sb === undefined ? s * 0.62 : o.sb, st = o.st === undefined ? s : o.st;+ const tx = o.taper || 0;+ const X0 = x0 + tx, X1 = x1 - tx, Z0 = z0 + (o.taperZ === undefined ? tx : o.taperZ), Z1 = z1 - (o.taperZ === undefined ? tx : o.taperZ);+ this.face([[x1, y0, z1, s], [x1, y0, z0, s], [X1, y1, Z0, st], [X1, y1, Z1, st]]); // +X+ this.face([[x0, y0, z0, s], [x0, y0, z1, s], [X0, y1, Z1, st], [X0, y1, Z0, st]]); // -X+ this.face([[x0, y0, z1, s], [x1, y0, z1, s], [X1, y1, Z1, st], [X0, y1, Z1, st]]); // +Z+ this.face([[x1, y0, z0, s], [x0, y0, z0, s], [X0, y1, Z0, st], [X1, y1, Z0, st]]); // -Z+ if (!o.noTop) this.face([[X0, y1, Z1, st], [X1, y1, Z1, st], [X1, y1, Z0, st], [X0, y1, Z0, st]]);+ if (!o.noBot) this.face([[x0, y0, z0, sb], [x1, y0, z0, sb], [x1, y0, z1, sb], [x0, y0, z1, sb]]);+ return this;+ }+ // Surface of revolution about Y. prof: [[r,y],...] bottom to top. o.sides, o.faceted, o.crease, o.flat+ lathe(prof, n = 12, o = {}) {+ const a0 = o.phase || 0, cr = Math.cos(rad(o.crease === undefined ? 55 : o.crease));+ const s = o.s === undefined ? 1 : o.s;+ const segs = [];+ for (let j = 0; j < prof.length - 1; j++) {+ const dr = prof[j + 1][0] - prof[j][0], dy = prof[j + 1][1] - prof[j][1], l = Math.hypot(dr, dy) || 1;+ segs.push([dy / l, -dr / l, l]);+ }+ if (o.faceted) { // hard-edged polygon of revolution+ for (let i = 0; i < n; i++) {+ const t0 = a0 + TAU * i / n, t1 = a0 + TAU * (i + 1) / n, tm = (t0 + t1) / 2;+ const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1), cm = Math.cos(tm), sm = Math.sin(tm);+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const p0 = prof[j], p1 = prof[j + 1];+ const w0 = p0[0] * Math.hypot(c1 - c0, s1 - s0), w1 = p1[0] * Math.hypot(c1 - c0, s1 - s0);+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ const a = this.v(p0[0] * c0, p0[1], p0[0] * s0, nr * cm, ny, nr * sm, -w0 / 2, vv, sA);+ const b = this.v(p1[0] * c0, p1[1], p1[0] * s0, nr * cm, ny, nr * sm, -w1 / 2, vv + l, sB);+ const c = this.v(p1[0] * c1, p1[1], p1[0] * s1, nr * cm, ny, nr * sm, w1 / 2, vv + l, sB);+ const d = this.v(p0[0] * c1, p0[1], p0[0] * s1, nr * cm, ny, nr * sm, w0 / 2, vv, sA);+ this.quad(a, b, c, d); vv += l;+ }+ }+ return this;+ }+ // smooth around, creased along the profile when the section turns sharply+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const prev = segs[j - 1], next = segs[j + 1];+ let n0r = nr, n0y = ny, n1r = nr, n1y = ny;+ if (prev && prev[0] * nr + prev[1] * ny > cr) { n0r = prev[0] + nr; n0y = prev[1] + ny; }+ if (next && next[0] * nr + next[1] * ny > cr) { n1r = next[0] + nr; n1y = next[1] + ny; }+ const base = this.count, p0 = prof[j], p1 = prof[j + 1];+ const circ = Math.max(p0[0], p1[0]) * TAU, uScale = Math.max(1, Math.round(circ)) / n;+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ for (let i = 0; i <= n; i++) {+ const t = a0 + TAU * i / n, c = Math.cos(t), sn = Math.sin(t);+ this.v(p0[0] * c, p0[1], p0[0] * sn, n0r * c, n0y, n0r * sn, i * uScale, vv, sA);+ this.v(p1[0] * c, p1[1], p1[0] * sn, n1r * c, n1y, n1r * sn, i * uScale, vv + l, sB);+ }+ for (let i = 0; i < n; i++) this.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+ vv += l;+ }+ return this;+ }+ // Sweep a closed section (a,b CCW; a along in-plane left normal, b along +Z) along a path in the XY plane.+ sweep(path, sec, o = {}) {+ const closedPath = !!o.closed, cr = Math.cos(rad(o.crease === undefined ? 50 : o.crease)), z0 = o.z || 0, s = o.s === undefined ? 1 : o.s;+ const n = path.length, E = [];+ const segCount = closedPath ? n : n - 1;+ for (let i = 0; i < segCount; i++) { const p = path[i], q = path[(i + 1) % n]; const dx = q[0] - p[0], dy = q[1] - p[1], l = Math.hypot(dx, dy) || 1e-6; E.push([dx / l, dy / l, l]); }+ // section normals+ const K = sec.length, SN = [];+ for (let j = 0; j < K; j++) { const a = sec[j], b = sec[(j + 1) % K]; const da = b[0] - a[0], db = b[1] - a[1], l = Math.hypot(da, db) || 1e-6; SN.push([db / l, -da / l, l]); }+ const rings = []; let dist = 0;+ for (let i = 0; i < segCount; i++) {+ const e = E[i], ePrev = closedPath ? E[(i - 1 + segCount) % segCount] : (E[i - 1] || e), eNext = closedPath ? E[(i + 1) % segCount] : (E[i + 1] || e);+ const p = path[i], q = path[(i + 1) % n];+ const miter = (A, B) => { let bx = -(A[1] + B[1]), by = A[0] + B[0]; const l = Math.hypot(bx, by) || 1; bx /= l; by /= l; const nlx = -B[1], nly = B[0]; const c = Math.max(0.35, bx * nlx + by * nly); return [bx / c, by / c]; };+ const m0 = miter(ePrev, e), m1 = miter(e, eNext);+ const smooth0 = ePrev[0] * e[0] + ePrev[1] * e[1] > cr, smooth1 = e[0] * eNext[0] + e[1] * eNext[1] > cr;+ const nl = [-e[1], e[0]];+ const n0 = smooth0 ? [-(e[1] + ePrev[1]), e[0] + ePrev[0]] : nl, n1 = smooth1 ? [-(e[1] + eNext[1]), e[0] + eNext[0]] : nl;+ const ln0 = Math.hypot(n0[0], n0[1]) || 1, ln1 = Math.hypot(n1[0], n1[1]) || 1;+ let perim = 0;+ for (let j = 0; j < K; j++) {+ const A = sec[j], B = sec[(j + 1) % K], sn = SN[j], pv = SN[(j - 1 + K) % K], nx = SN[(j + 1) % K];+ const sA = sn[0] * pv[0] + sn[1] * pv[1] > cr, sB = sn[0] * nx[0] + sn[1] * nx[1] > cr;+ const nA = sA ? [sn[0] + pv[0], sn[1] + pv[1]] : sn, nB = sB ? [sn[0] + nx[0], sn[1] + nx[1]] : sn;+ const norm = (na, nb, nv, ln) => { const l = Math.hypot(na, nb) || 1; const a = na / l, b = nb / l; return [a * nv[0] / ln, a * nv[1] / ln, b]; };+ const na0 = norm(nA[0], nA[1], n0, ln0), nb0 = norm(nB[0], nB[1], n0, ln0), na1 = norm(nA[0], nA[1], n1, ln1), nb1 = norm(nB[0], nB[1], n1, ln1);+ const shA = A[2] === undefined ? s : A[2] * s, shB = B[2] === undefined ? s : B[2] * s;+ const a = this.v(p[0] + A[0] * m0[0], p[1] + A[0] * m0[1], z0 + A[1], na0[0], na0[1], na0[2], dist, perim, shA);+ const b = this.v(p[0] + B[0] * m0[0], p[1] + B[0] * m0[1], z0 + B[1], nb0[0], nb0[1], nb0[2], dist, perim + sn[2], shB);+ const c = this.v(q[0] + B[0] * m1[0], q[1] + B[0] * m1[1], z0 + B[1], nb1[0], nb1[1], nb1[2], dist + e[2], perim + sn[2], shB);+ const d = this.v(q[0] + A[0] * m1[0], q[1] + A[0] * m1[1], z0 + A[1], na1[0], na1[1], na1[2], dist + e[2], perim, shA);+ this.quad(a, b, c, d);+ perim += sn[2];+ }+ dist += e[2];+ rings.push(m0);+ }+ if (!closedPath && o.caps !== false) {+ const capAt = (p, nv, e, flip) => {+ const pts = sec.map((A) => [p[0] + A[0] * nv[0], p[1] + A[0] * nv[1], z0 + A[1]]);+ const idx = triangulate(sec.map((A) => [A[0], A[1]]));+ const ids = pts.map((q) => this.v(q[0], q[1], q[2], flip * e[0], flip * e[1], 0, q[0], q[2], s * 0.8));+ for (const t of idx) { if (flip > 0) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); else this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ };+ capAt(path[0], rings[0], E[0], -1);+ const lastE = E[segCount - 1]; capAt(path[n - 1], [-lastE[1], lastE[0]], lastE, 1);+ }+ return this;+ }+ // Generic tube along a 3D path with parallel transport. radii: number|array|fn(t) -> [ra, rb]; ring: fn(angle,t) multiplier+ tube(path, radii, n = 8, o = {}) {+ const N = path.length, s = o.s === undefined ? 1 : o.s;+ let T = [], U = o.up ? [...o.up] : null;+ for (let i = 0; i < N; i++) {+ const p = path[Math.max(0, i - 1)], q = path[Math.min(N - 1, i + 1)];+ let tx = q[0] - p[0], ty = q[1] - p[1], tz = q[2] - p[2]; const l = Math.hypot(tx, ty, tz) || 1; T.push([tx / l, ty / l, tz / l]);+ }+ if (!U) U = Math.abs(T[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];+ const base = this.count; const rowLen = n + 1; let dist = 0;+ for (let i = 0; i < N; i++) {+ const t = T[i];+ let d = U[0] * t[0] + U[1] * t[1] + U[2] * t[2];+ let ux = U[0] - d * t[0], uy = U[1] - d * t[1], uz = U[2] - d * t[2]; const ul = Math.hypot(ux, uy, uz) || 1; ux /= ul; uy /= ul; uz /= ul; U = [ux, uy, uz];+ const vx = t[1] * uz - t[2] * uy, vy = t[2] * ux - t[0] * uz, vz = t[0] * uy - t[1] * ux;+ const f = i / (N - 1);+ const rr = typeof radii === 'function' ? radii(f) : (Array.isArray(radii) ? radii[i] : radii);+ const ra = Array.isArray(rr) ? rr[0] : rr, rb = Array.isArray(rr) ? rr[1] : rr;+ if (i > 0) dist += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1], path[i][2] - path[i - 1][2]);+ for (let k = 0; k <= n; k++) {+ const a = TAU * k / n, c = Math.cos(a), sn = Math.sin(a);+ const mod = o.ring ? o.ring(a, f) : 1;+ const shade = o.ringS ? o.ringS(a, f) : 1;+ const px = path[i][0] + (ux * c * ra + vx * sn * rb) * mod, py = path[i][1] + (uy * c * ra + vy * sn * rb) * mod, pz = path[i][2] + (uz * c * ra + vz * sn * rb) * mod;+ const nx = ux * c / (ra || 1) + vx * sn / (rb || 1), ny = uy * c / (ra || 1) + vy * sn / (rb || 1), nz = uz * c / (ra || 1) + vz * sn / (rb || 1);+ this.v(px, py, pz, nx, ny, nz, k / n * Math.max(ra, rb) * TAU, dist, s * shade);+ }+ }+ for (let i = 0; i < N - 1; i++) for (let k = 0; k < n; k++) {+ const a = base + i * rowLen + k, b = a + 1, c = a + rowLen + 1, d = a + rowLen;+ this.quad(a, b, c, d);+ }+ if (o.capEnd !== false) { const c = this.v(path[N - 1][0], path[N - 1][1], path[N - 1][2], T[N - 1][0], T[N - 1][1], T[N - 1][2], 0, 0, s); const b2 = base + (N - 1) * rowLen; const ids = []; for (let k = 0; k < n; k++) ids.push(b2 + k); for (let k = 0; k < n; k++) this.tri(c, ids[k], b2 + k + 1); }+ if (o.capStart) { const c = this.v(path[0][0], path[0][1], path[0][2], -T[0][0], -T[0][1], -T[0][2], 0, 0, s); for (let k = 0; k < n; k++) this.tri(c, base + k + 1, base + k); }+ return this;+ }+ // parametric grid: fn(u,v) -> [x,y,z, shade?]; winding u x v = front. Smooth normals from the grid.+ grid(fn, nu, nv, o = {}) {+ const pts = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pts.push(fn(i / nu, j / nv));+ const id = (i, j) => j * (nu + 1) + i, base = this.count, flip = o.flip ? -1 : 1, s = o.s === undefined ? 1 : o.s;+ const wrapU = !!o.wrapU;+ let vLen = 0;+ for (let j = 0; j <= nv; j++) {+ if (j > 0) { const a = pts[id(0, j - 1)], b = pts[id(0, j)]; vLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ // o.metric: measure across from the middle of each row, so a converging surface keeps true scale instead of a fan of streaks+ let uLen = 0;+ if (o.metric) { let total = 0; for (let i = 1; i <= nu; i++) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; total += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); } uLen = -total / 2; }+ for (let i = 0; i <= nu; i++) {+ if (i > 0) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; uLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ const iP = wrapU ? (i === 0 ? nu - 1 : i - 1) : Math.max(0, i - 1), iN = wrapU ? (i === nu ? 1 : i + 1) : Math.min(nu, i + 1);+ const A = pts[id(iP, j)], B = pts[id(iN, j)], C = pts[id(i, Math.max(0, j - 1))], D = pts[id(i, Math.min(nv, j + 1))];+ const ux = B[0] - A[0], uy = B[1] - A[1], uz = B[2] - A[2], vx = D[0] - C[0], vy = D[1] - C[1], vz = D[2] - C[2];+ const p = pts[id(i, j)];+ const uvU = o.boxUV ? undefined : uLen, uvV = o.boxUV ? undefined : vLen;+ this.v(p[0], p[1], p[2], flip * (uy * vz - uz * vy), flip * (uz * vx - ux * vz), flip * (ux * vy - uy * vx), uvU, uvV, s * (p[3] === undefined ? 1 : p[3]));+ }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) {+ const a = base + id(i, j), b = base + id(i + 1, j), c = base + id(i + 1, j + 1), d = base + id(i, j + 1);+ if (flip > 0) this.quad(a, b, c, d); else this.quad(a, d, c, b);+ }+ return this;+ }+ // 2D outline with holes, extruded along Z from z0 to z1 (front +Z). Reveal sides included.+ slab(outline, holes = [], z0 = 0, z1 = 0.1, o = {}) {+ const so = area2(outline) > 0 ? outline : outline.slice().reverse();+ const hs = holes.map((h) => (area2(h) < 0 ? h : h.slice().reverse()));+ const tris = triangulate(so, hs);+ const all = so.concat(...hs);+ const sf = o.sf === undefined ? 1 : o.sf, sb = o.sb === undefined ? 1 : o.sb, sr = o.sr === undefined ? 0.72 : o.sr;+ if (!o.noFront) { const ids = all.map((p) => this.v(p[0], p[1], z1, 0, 0, 1, undefined, undefined, sf)); for (const t of tris) this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ if (!o.noBack) { const ids = all.map((p) => this.v(p[0], p[1], z0, 0, 0, -1, undefined, undefined, sb)); for (const t of tris) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); }+ const side = (loop, s) => {+ const L = loop.length; if (o.noSides) return;+ for (let i = 0; i < L; i++) {+ const p = loop[i], q = loop[(i + 1) % L]; const dx = q[0] - p[0], dy = q[1] - p[1];+ const l = Math.hypot(dx, dy) || 1, nx = dy / l, ny = -dx / l;+ const a = this.v(p[0], p[1], z0, nx, ny, 0, undefined, undefined, s), b = this.v(q[0], q[1], z0, nx, ny, 0, undefined, undefined, s), c = this.v(q[0], q[1], z1, nx, ny, 0, undefined, undefined, s), d = this.v(p[0], p[1], z1, nx, ny, 0, undefined, undefined, s);+ this.quad(a, b, c, d);+ }+ };+ if (!o.noOuterSides) side(so, o.so === undefined ? 0.85 : o.so);+ hs.forEach((h) => side(h, sr));+ return this;+ }+ // Vertical prism from an (x,z) polygon+ prism(pts, y0, y1, o = {}) {+ let A = 0; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; A += p[0] * q[1] - q[0] * p[1]; }+ const L = A > 0 ? pts : pts.slice().reverse(), s = o.s === undefined ? 1 : o.s, st = o.st === undefined ? s : o.st, n = L.length, k = o.topScale === undefined ? 1 : o.topScale;+ let cx = 0, cz = 0; for (const p of L) { cx += p[0]; cz += p[1]; } cx /= n; cz /= n;+ const top = L.map((p) => [cx + (p[0] - cx) * k, cz + (p[1] - cz) * k]);+ for (let i = 0; i < n; i++) {+ const p = L[i], q = L[(i + 1) % n], P2 = top[i], Q2 = top[(i + 1) % n];+ this.face([[q[0], y0, q[1], s], [p[0], y0, p[1], s], [P2[0], y1, P2[1], st], [Q2[0], y1, Q2[1], st]]);+ }+ if (!o.noTop && k > 1e-4) { const c = top.map((p) => this.v(p[0], y1, p[1], 0, 1, 0, undefined, undefined, st)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i + 1], c[i]); }+ if (o.bot) { const c = L.map((p) => this.v(p[0], y0, p[1], 0, -1, 0, undefined, undefined, s * 0.6)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i], c[i + 1]); }+ return this;+ }+ // Welds vertices that agree in every attribute: sweeps and lathes otherwise export many duplicates+ // Split every edge longer than L. The rule depends only on the edge, so neighbours split alike and no crack opens.+ // Long walls need vertices for the baked light to land on.+ tess(L) {+ this.weld();+ const L2 = L * L, { P: p, N: nn, UV: uv, C: c } = this;+ for (let pass = 0; pass < 7; pass++) {+ const mid = new Map(), I2 = [], I0 = this.I; let changed = false;+ const long = (a, b) => { const x = p[a * 3] - p[b * 3], y = p[a * 3 + 1] - p[b * 3 + 1], z = p[a * 3 + 2] - p[b * 3 + 2]; return x * x + y * y + z * z > L2; };+ const midOf = (a, b) => {+ const lo = Math.min(a, b), hi = Math.max(a, b), key = lo * 4194304 + hi; let m = mid.get(key);+ if (m === undefined) {+ m = p.length / 3; mid.set(key, m);+ p.push((p[lo * 3] + p[hi * 3]) / 2, (p[lo * 3 + 1] + p[hi * 3 + 1]) / 2, (p[lo * 3 + 2] + p[hi * 3 + 2]) / 2);+ let nx = nn[lo * 3] + nn[hi * 3], ny = nn[lo * 3 + 1] + nn[hi * 3 + 1], nz = nn[lo * 3 + 2] + nn[hi * 3 + 2]; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;+ nn.push(nx / l, ny / l, nz / l);+ uv.push((uv[lo * 2] + uv[hi * 2]) / 2, (uv[lo * 2 + 1] + uv[hi * 2 + 1]) / 2);+ c.push((c[lo * 3] + c[hi * 3]) >> 1, (c[lo * 3 + 1] + c[hi * 3 + 1]) >> 1, (c[lo * 3 + 2] + c[hi * 3 + 2]) >> 1);+ }+ return m;+ };+ for (let t = 0; t < I0.length; t += 3) {+ const a = I0[t], b = I0[t + 1], d = I0[t + 2], ab = long(a, b), bd = long(b, d), da = long(d, a);+ if (!ab && !bd && !da) { I2.push(a, b, d); continue; }+ changed = true;+ const m1 = ab ? midOf(a, b) : -1, m2 = bd ? midOf(b, d) : -1, m3 = da ? midOf(d, a) : -1;+ if (ab && bd && da) I2.push(a, m1, m3, m1, b, m2, m3, m2, d, m1, m2, m3);+ else if (ab && bd) I2.push(a, m1, d, m1, m2, d, m1, b, m2);+ else if (bd && da) I2.push(b, m2, a, m2, m3, a, m2, d, m3);+ else if (da && ab) I2.push(d, m3, b, m3, m1, b, m3, a, m1);+ else if (ab) I2.push(a, m1, d, m1, b, d);+ else if (bd) I2.push(b, m2, a, m2, d, a);+ else I2.push(d, m3, b, m3, a, b);+ }+ this.I = I2;+ if (!changed) break;+ }+ this.geo = null;+ return this;+ }+ weld() {+ if (this.welded) return this;+ const n = this.count, map = new Map(), remap = new Int32Array(n), P2 = [], N2 = [], U2 = [], C2 = [];+ const { P: p, N: nn, UV: uv, C: c } = this, col = !this.noColor;+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, i2 = i * 2;+ const key = Math.round(p[i3] * 4000) + ',' + Math.round(p[i3 + 1] * 4000) + ',' + Math.round(p[i3 + 2] * 4000) + ',' + Math.round(nn[i3] * 90) + ',' + Math.round(nn[i3 + 1] * 90) + ',' + Math.round(nn[i3 + 2] * 90) + ',' + Math.round(uv[i2] * 800) + ',' + Math.round(uv[i2 + 1] * 800) + (col ? ',' + c[i3] + ',' + c[i3 + 1] + ',' + c[i3 + 2] : '');+ let k = map.get(key);+ if (k === undefined) { k = P2.length / 3; map.set(key, k); P2.push(p[i3], p[i3 + 1], p[i3 + 2]); N2.push(nn[i3], nn[i3 + 1], nn[i3 + 2]); U2.push(uv[i2], uv[i2 + 1]); C2.push(c[i3], c[i3 + 1], c[i3 + 2]); }+ remap[i] = k;+ }+ const I2 = [];+ for (let t = 0; t < this.I.length; t += 3) {+ const a = remap[this.I[t]], b = remap[this.I[t + 1]], d = remap[this.I[t + 2]];+ if (a !== b && b !== d && a !== d) I2.push(a, b, d);+ }+ this.P = P2; this.N = N2; this.UV = U2; this.C = C2; this.I = I2; this.welded = true;+ return this;+ }+ toGeo() {+ if (this.geo) return this.geo;+ this.weld();+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.P), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(this.N), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.UV), 2));+ if (!this.noColor) g.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(this.C), 3, true));+ g.setIndex(new THREE.BufferAttribute(this.count > 65535 ? new Uint32Array(this.I) : new Uint16Array(this.I), 1));+ this.geo = g;+ return g;+ }+}+function area2(p) { let a = 0; for (let i = 0; i < p.length; i++) { const q = p[(i + 1) % p.length]; a += p[i][0] * q[1] - q[0] * p[i][1]; } return a / 2; }+function triangulate(contour, holes = []) {+ const V = (p) => new THREE.Vector2(p[0], p[1]);+ const tris = THREE.ShapeUtils.triangulateShape(contour.map(V), holes.map((h) => h.map(V)));+ const all = contour.concat(...holes);+ return tris.map((t) => {+ const a = all[t[0]], b = all[t[1]], c = all[t[2]];+ const s = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ return s >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]];+ });+}++// ---- 2D curves -------------------------------------------------------------------+// Two-centred pointed arc: span w, rise h, base y=0 centred at x=0, from left spring to right spring+function pointedArc(w, h, n = 8) {+ const c = (h * h - w * w / 4) / w, r = c + w / 2, pts = [];+ const a0 = PI, a1 = Math.atan2(h, -c);+ for (let i = 0; i <= n; i++) { const a = lerp(a0, a1, i / n); pts.push([c + r * Math.cos(a), r * Math.sin(a)]); }+ for (let i = n - 1; i >= 0; i--) { const a = lerp(a0, a1, i / n); pts.push([-(c + r * Math.cos(a)), r * Math.sin(a)]); }+ pts[n][0] = 0;+ return pts;+}+// CCW outline of a pointed opening: width w, sill y0, spring ys, rise h+function archOutline(w, y0, ys, h, n = 8) {+ const arc = pointedArc(w, h, n).map((p) => [p[0], p[1] + ys]);+ arc.reverse(); // right spring -> apex -> left spring (CCW)+ return [[-w / 2, y0], [w / 2, y0], ...arc];+}+const ringPts = (cx, cy, r, n = 24, ph = 0) => { const a = []; for (let i = 0; i < n; i++) { const t = ph + TAU * i / n; a.push([cx + r * Math.cos(t), cy + r * Math.sin(t)]); } return a; };+const ofsY = (pts, dy) => pts.map((p) => [p[0], p[1] + dy]);+const ofsXY = (pts, dx, dy) => pts.map((p) => [p[0] + dx, p[1] + dy]);++// ---- Sections (a, b, shade?) closed CCW ------------------------------------------+// Moulded arch section with convex rolls: a=0 at the soffit, grows outward; symmetric in b.+function rollSection(w, d, rolls = 3, amp = 0.07, seg = 3) {+ const half = [];+ const A = [w, d / 2], B = [0, d * 0.12], dx = B[0] - A[0], dy = B[1] - A[1], l = Math.hypot(dx, dy);+ for (let k = 0; k < rolls; k++) for (let i = 0; i < seg; i++) {+ const t = (k + i / seg) / rolls, ph = i / seg;+ const off = amp * Math.pow(Math.sin(PI * ph), 0.7), sh = 0.68 + 0.32 * Math.sin(PI * ph);+ half.push([lerp(A[0], B[0], t) + off * dy / l, lerp(A[1], B[1], t) - off * dx / l, sh]);+ }+ half.push([B[0], B[1], 0.8]);+ const back = half.slice().reverse().map((p) => [p[0], -p[1], p[2]]);+ return [[w, -d / 2, 1], ...half, ...back.slice(0, -1)];+}+// Cheapest moulded bar: a hexagon, smooth enough to read as a roll at distance+function hexBar(w, d) { const a = w / 2, b = d / 2; return [[a, -b * 0.5], [a, b * 0.5], [0, b], [-a, b * 0.5], [-a, -b * 0.5], [0, -b]]; }+// Simple chamfered bar centred on the path, width w (in plane), depth d+function barSection(w, d, ch = 0.25) {+ const a = w / 2, b = d / 2, c = Math.min(a, b) * ch * 2;+ return [[-a, -b + c], [-a + c, -b], [a - c, -b], [a, -b + c], [a, b - c], [a - c, b], [-a + c, b], [-a, b - c]];+}+// Tracery bar with a roll on the front and back faces+function traceryBar(w, d) {+ const a = w / 2, b = d / 2, s = [];+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([a * Math.cos(t), b * 0.62 + b * 0.38 * Math.sin(t)]); }+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([-a * Math.cos(t), -b * 0.62 - b * 0.38 * Math.sin(t)]); }+ return s;+}+// String course / cornice projecting along +b from the wall at b=0, height h (a)+function stringSection(h = 0.3, out = 0.25) {+ return [[-h / 2, 0], [-h / 2, out * 0.3, 0.55], [-h * 0.25, out * 0.62, 0.7], [-h * 0.02, out * 0.72, 0.85], [h * 0.12, out, 1], [h * 0.2, out * 0.98, 1], [h / 2, out * 0.05, 1], [h / 2, 0]].reverse();+}++const spline = (pts, sub = 4) => { // Catmull-Rom through 3D points+ const out = [], n = pts.length, g = (i) => pts[clamp(i, 0, n - 1)];+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < sub; k++) {+ const t = k / sub, p0 = g(i - 1), p1 = g(i), p2 = g(i + 1), p3 = g(i + 2), v = [];+ for (let c = 0; c < 3; c++) v.push(0.5 * (2 * p1[c] + (p2[c] - p0[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t * t + (3 * p1[c] - p0[c] - 3 * p2[c] + p3[c]) * t * t * t));+ out.push(v);+ }+ out.push(pts[n - 1]);+ return out;+};+const blob = (b, c, r, nu = 8, nv = 6, sh = 1) => b.grid((u, v) => {+ const ph = -PI + TAU * u, la = -PI / 2 + PI * v;+ return [c[0] + r[0] * Math.cos(la) * Math.cos(ph), c[1] + r[1] * Math.sin(la), c[2] + r[2] * Math.cos(la) * Math.sin(ph), sh];+}, nu, nv, { flip: true, wrapU: true });++// ==================================================================================+// MATERIALS: procedural PBR, with height-derived normals+// ==================================================================================+const MAT = {};+const TILE = { ashlar: 8, fine: 2, lead: 8, pave: 2.4, oak: 1.2, iron: 0.8, gold: 1, flag: 6, earth: 5 };++function ashlarOps(rng, S, tone, joint, stains) {+ // Irregular coursed blocks; shared between albedo and height by replaying the same rng seed+ const ops = [], rows = 7, rowH = Math.floor(S / rows);+ for (let r = 0; r < rows; r++) {+ const y0 = r * rowH; let x = Math.round(rng() * S / 5) % S;+ const n = 3 + Math.floor(rng() * 2), widths = [];+ for (let i = 0; i < n; i++) widths.push(0.7 + rng() * 0.6);+ const tot = widths.reduce((a, c) => a + c, 0);+ for (let i = 0; i < n; i++) {+ const w = Math.round(widths[i] / tot * S), dv = rng(), a = rng();+ const col = tone[Math.floor(dv * tone.length)];+ const x0 = x % S, wr = Math.min(w, S - x0), op = Math.round(10 + a * 34);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y0, width: wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ if (wr < w) ops.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: y0, width: w - wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ x += w;+ ops.push({ op: 'paint', shape: { kind: 'line', from: [x % S, y0], to: [x % S, y0 + rowH], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y0], to: [S, y0], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ return ops.slice(0, 58).concat(stains || []);+}++// Regions of the decal sheet as fractions [u0, v0, u1, v1]; a streak's top edge is at v0+const DECAL_UV = { streak: [0, 0, 0.5, 0.5], soft: [0.5, 0, 1, 0.25], soot: [0.5, 0.25, 1, 0.5], ground: [0, 0.5, 0.5, 1], drip: [0.5, 0.75, 1, 1] };+async function makeMaterials() {+ const S = 512, repeat = { minFilter: 'linear-mipmap-linear', magFilter: 'linear', wrapS: 'repeat', wrapT: 'repeat' };+ const noise = (a, b, scale, oct, seed, opacity, blend) => ({ op: 'noise', colorA: a, colorB: b, scale, octaves: oct, seed, opacity, blend: blend || 'normal' });+ const spec3 = (usage, base, ops, size = S) => ({ schemaVersion: 3, width: size, height: size, usage, operations: [{ op: 'legacy-v2', spec: { schemaVersion: 2, size, usage, layers: base } }, ...ops] });+ const v3 = (usage, base, ops, size = S) => proceduralTexture(spec3(usage, base, ops, size));+ const stamps = (shape, rgba, count, seed, opacity, area) => ({ op: 'stamps', shape, rgba, count, seed, area: area || { x: 0, y: 0, width: S, height: S }, opacity });+ const ell = (w, h) => ({ kind: 'ellipse', x: 0, y: 0, width: w, height: h });+ const tool = (dx, dy, w = 1) => ({ kind: 'line', from: [0, 0], to: [dx, dy], width: w }); // a stroke of the claw chisel++ // ---- Weathered ashlar: 8 m repeat, every block its own stone. Colour varies by block, chisel marks+ // in the relief instead of round pits, rain and soot already sunk into the surface ----+ const quads = [[0, 0], [S, 0], [0, S], [S, S]];+ const ashAlbedo = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb9ad96 }, noise(0x968b76, 0xcdc3ad, 3, 2, seed + 1, 0.85), noise(0x7f7666, 0xc6bca6, 17, 2, seed + 2, 0.4)],+ ashlarOps(mkRng(seed), S, [[78, 72, 60], [226, 216, 190], [158, 132, 92], [122, 128, 124]], { w: 2, rgba: [70, 64, 54, 255], op: 225 }, [+ stamps(ell(20, 70), [70, 66, 56, 255], 26, seed + 3, 24), stamps(ell(40, 26), [52, 56, 46, 255], 16, seed + 4, 20), stamps(tool(11, 6), [96, 88, 74, 255], 110, seed + 5, 34),+ ]));+ const ashHeight = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb4b4b4 }, noise(0x8c8c8c, 0xd0d0d0, 11, 2, seed + 6, 0.7)],+ ashlarOps(mkRng(seed), S, [[126, 126, 126], [214, 214, 214], [170, 170, 170], [150, 150, 150]], { w: 3, rgba: [12, 12, 12, 255], op: 255 }, [+ stamps(tool(14, 8), [86, 86, 86, 255], 120, seed + 8, 90), stamps(tool(-12, 9), [226, 226, 226, 255], 110, seed + 9, 70),+ ]));+ const atlasOf = async (usage, make, seeds) => textureAtlas({ schemaVersion: 1, width: 2 * S, height: 2 * S, pack: { edge: 2048, gutter: 0 }, usage, sampler: repeat, regions: quads.map((p, i) => ({ name: 'q' + i, content: { x: p[0], y: p[1], width: S, height: S }, gutter: 0, source: { kind: 'procedural', spec: make(p, seeds[i]) } })) });+ const ashA = await atlasOf('albedo', ashAlbedo, [77, 131, 211, 307]);+ const ashH = await atlasOf('albedo', ashHeight, [77, 131, 211, 307]);+ MAT.ashlar = pbrMaterial({ albedo: ashA, normal: normalMapFromHeight(ashH, { strength: 2.6 }), roughness: 0.94, metalness: 0, name: 'Limestone_Ashlar' });++ // Dressed stone: no joints; chisel strokes and mottling live in the colour, because a normal map here would+ // add 16 bytes of tangent to every vertex of every moulding+ const finA = v3('albedo', [{ op: 'solid', color: 0xc2b7a0 }, noise(0xb5ab95, 0xcdc4af, 5, 3, 31, 0.8), noise(0xa79d88, 0xcbc2ad, 23, 3, 32, 0.34)], [+ stamps(tool(9, 5), [120, 112, 96, 255], 130, 33, 34), stamps(tool(-7, 6), [214, 206, 188, 255], 80, 34, 30),+ ]);+ MAT.fine = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name: 'Limestone_Dressed' });+ const figTone = (name, r, g, b) => { const m = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name }); m.color.setRGB(r, g, b); m.vertexColors = true; return m; };+ MAT.figure = figTone('Sculpture_Sunlit', 0.86, 0.8, 0.68); // figures take the same stone under different light+ MAT.figureHalf = figTone('Sculpture_HalfShade', 0.5, 0.49, 0.47);+ MAT.figureDeep = figTone('Sculpture_DeepShade', 0.3, 0.31, 0.34);+ MAT.figureIn = figTone('Sculpture_Interior', 0.62, 0.53, 0.42);++ // ---- Patinated lead: long sheets a metre wide running the whole way down the slope. They differ only a little from each+ // other, are lapped once in a long run, and carry the streaks that rain draws down them. The tile repeats every 8 m ----+ const LS = 512, leadOps = (tones, lapOp, seamOp, rngSeed) => {+ const r = mkRng(rngSeed), ops = [], cols = 8, W = LS / cols;+ for (let c = 0; c < cols; c++) {+ const t = tones[Math.floor(r() * tones.length)], lap = Math.floor((c % 2 ? 0.28 : 0.72) * LS + (r() - 0.5) * 40);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: c * W, y: 0, width: W, height: LS }, rgba: [t[0], t[1], t[2], 255], opacity: 12 + Math.floor(r() * 26) });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, lap], to: [(c + 1) * W, lap], width: lapOp.w }, rgba: lapOp.rgba, opacity: lapOp.op });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, 0], to: [c * W, LS], width: seamOp.w }, rgba: seamOp.rgba, opacity: seamOp.op });+ }+ return ops;+ };+ const leadArea = { x: 0, y: 0, width: LS, height: LS };+ const leadA = v3('albedo', [{ op: 'solid', color: 0x6f7a7e }, noise(0x566166, 0x8d9896, 5, 2, 41, 0.75)], [+ ...leadOps([[86, 98, 102], [118, 130, 130], [66, 76, 82], [140, 150, 146], [100, 104, 100]], { w: 2, rgba: [36, 42, 46, 255], op: 220 }, { w: 2, rgba: [40, 46, 50, 255], op: 180 }, 44),+ stamps({ kind: 'rectangle', x: 0, y: 0, width: 4, height: 180 }, [208, 214, 206, 255], 70, 45, 22, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 2, height: 260 }, [34, 40, 44, 255], 90, 46, 36, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 6, height: 120 }, [150, 160, 154, 255], 40, 47, 16, leadArea),+ ], LS);+ const leadH = v3('albedo', [{ op: 'solid', color: 0x8c8c8c }, noise(0x848484, 0x989898, 9, 2, 48, 0.8)], [+ ...leadOps([[140, 140, 140], [132, 132, 132], [148, 148, 148]], { w: 3, rgba: [232, 232, 232, 255], op: 255 }, { w: 5, rgba: [250, 250, 250, 255], op: 255 }, 44),+ stamps(ell(17, 11), [116, 116, 116, 255], 110, 49, 60, leadArea),+ ], LS);+ MAT.lead = pbrMaterial({ albedo: leadA, normal: normalMapFromHeight(leadH, { strength: 3 }), roughness: 0.6, metalness: 0.5, name: 'Lead_Patinated' });++ // ---- Gold leaf: a bright warm metal that still reads as gold where the environment is dull ----+ const gldA = v3('albedo', [{ op: 'solid', color: 0xffc644 }, noise(0xf0a830, 0xffd764, 14, 2, 51, 0.6)], [stamps(ell(5, 4), [176, 110, 30, 255], 60, 52, 90, { x: 0, y: 0, width: 128, height: 128 })], 128);+ MAT.gold = pbrMaterial({ albedo: gldA, roughness: 0.25, metalness: 1, name: 'Gilding' });+ MAT.gold.color.setRGB(0.92, 0.78, 0.56);+ MAT.gold.emissive.setRGB(1, 0.6, 0.14); MAT.gold.emissiveIntensity = 0.1;++ // Aged oak and forged iron from the approved scanned sets+ MAT.oak = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.oak-veneer-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.oak-veneer-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.oak-veneer-arm.v1'), name: 'Oak_Aged' });+ MAT.iron = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.rusted-metal-albedo.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.rusted-metal-arm.v1'), name: 'Iron_Forged' });++ // Paving: worn limestone and grey marble, polished by seven centuries of feet+ const pavA = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'checker', colorA: 0xd2cab6, colorB: 0x8c8678, squares: 4 }, noise(0x7c766a, 0xe0dac8, 9, 3, 61, 0.42), { op: 'bricks', brick: 0xffffff, mortar: 0x5a564e, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ const pavH = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'solid', color: 0xc8c8c8 }, noise(0xb0b0b0, 0xdcdcdc, 15, 3, 62, 0.5), { op: 'bricks', brick: 0xffffff, mortar: 0x202020, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ MAT.pave = pbrMaterial({ albedo: pavA, normal: normalMapFromHeight(pavH, { strength: 1.8 }), roughness: 0.42, metalness: 0, name: 'Paving_Worn' });+ for (const k of ['ashlar', 'fine', 'oak', 'iron', 'pave', 'lead']) MAT[k].vertexColors = true;++ // ---- Weathering decals: rain streaks, soot, ground damp and moss, cast shadow. Alpha-blended overlays+ // give crisp detail on walls that have very few vertices ----+ const DS = 256, rd = mkRng(99), polyStreak = (x, y0, L, w, rgb, op) => ({ op: 'paint', shape: { kind: 'polygon', points: [[x, y0], [x + w, y0], [x + w * 0.8 + 1, y0 + L * 0.45], [x + w * 0.55 + 1, y0 + L * 0.8], [x + w * 0.45, y0 + L], [x + w * 0.3, y0 + L * 0.75], [x + w * 0.15, y0 + L * 0.4]].map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: [rgb[0], rgb[1], rgb[2], 255], opacity: op });+ const streakOps = [], dripOps = [];+ for (let i = 0; i < 44; i++) { const x = Math.floor(rd() * (DS - 9)), dark = rd() > 0.26, L = 65 + Math.floor(rd() * 185), w = 2 + Math.floor(rd() * 7); streakOps.push(polyStreak(x, 0, Math.min(L, DS - 2), w, dark ? [44, 40, 34] : [176, 170, 150], dark ? 40 + Math.floor(rd() * 70) : 26 + Math.floor(rd() * 30))); }+ for (let i = 0; i < 12; i++) streakOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 3 + i * 2 }, rgba: [34, 32, 28, 255], opacity: 12 });+ for (let i = 0; i < 38; i++) { const x = Math.floor(rd() * (DS - 8)), L = 20 + Math.floor(rd() * 100), w = 1 + Math.floor(rd() * 6); dripOps.push(polyStreak(x, 0, Math.min(L, 125), w, [38, 36, 30], 44 + Math.floor(rd() * 70))); }+ for (let i = 0; i < 10; i++) dripOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 2 + i * 2 }, rgba: [28, 26, 24, 255], opacity: 16 });+ const softOps = []; for (let i = 0; i < 30; i++) softOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: Math.max(1, Math.round(128 * Math.pow(1 - i / 30, 1.5))) }, rgba: [18, 18, 24, 255], opacity: 15 });+ const dampOps = [];+ for (let i = 0; i < 18; i++) { const h = Math.round(DS * Math.pow((i + 1) / 18, 1.6)); dampOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: DS - h, width: DS, height: h }, rgba: i % 3 ? [38, 42, 28, 255] : [50, 58, 34, 255], opacity: 9 }); }+ dampOps.push(stamps(ell(13, 8), [66, 84, 42, 255], 110, 71, 46, { x: 0, y: 120, width: DS, height: 134 }), stamps(ell(5, 28), [30, 32, 24, 255], 80, 72, 64, { x: 0, y: 60, width: DS, height: 190 }), stamps(ell(3, 3), [120, 132, 76, 255], 60, 73, 90, { x: 0, y: 100, width: DS, height: 150 }));+ const clear = [0, 0, 0, 0];+ const dreg = (name, x, y, w, h, ops, bg) => ({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'albedo', background: bg || clear, operations: ops } } });+ const decalTex = await textureAtlas({ schemaVersion: 1, width: 512, height: 512, usage: 'albedo', regions: [+ dreg('streak', 0, 0, DS, DS, streakOps.slice(0, 64)),+ dreg('soft', 256, 0, DS, 128, softOps),+ dreg('soot', 256, 128, DS, 128, [stamps(ell(35, 27), [24, 22, 20, 255], 150, 74, 20, { x: 20, y: 15, width: 200, height: 90 }), stamps(ell(13, 11), [18, 16, 14, 255], 90, 75, 36, { x: 45, y: 25, width: 160, height: 75 })]),+ dreg('ground', 0, 256, DS, DS, dampOps),+ dreg('drip', 256, 384, DS, 128, dripOps.slice(0, 64)),+ ] });+ MAT.decal = pbrMaterial({ albedo: decalTex, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Weathering_Decals' });+ MAT.decal.depthWrite = false;++ await makeGlassMaterials();++ // ---- The light of the south windows: the same paving and the same stone, tinted per vertex by bakeWindowLight, so light multiplies what it lands on ----+ MAT.pool = pbrMaterial({ albedo: pavA, roughness: 0.42, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightOnPaving' });+ MAT.pool.vertexColors = true;+ MAT.pool.depthWrite = false;+ MAT.poolStone = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightOnStone' });+ MAT.poolStone.vertexColors = true;+ MAT.poolStone.depthWrite = false;++ // ---- The ground: worn flags in irregular courses, and rough grass over packed earth ----+ const flagTones = [[74, 70, 62], [150, 142, 124], [112, 104, 88], [96, 98, 90]];+ const flagA = v3('albedo', [{ op: 'solid', color: 0x8f8778 }, noise(0x6f695c, 0xa89f8d, 4, 2, 81, 0.8), noise(0x5e594e, 0x9a9282, 19, 2, 82, 0.35)], ashlarOps(mkRng(83), S, flagTones, { w: 4, rgba: [38, 34, 28, 255], op: 235 }, [stamps(ell(30, 18), [168, 160, 140, 255], 40, 84, 26), stamps(tool(8, 3), [70, 66, 58, 255], 70, 85, 30)]));+ const flagH = v3('albedo', [{ op: 'solid', color: 0xb0b0b0 }, noise(0x969696, 0xc4c4c4, 9, 2, 86, 0.6)], ashlarOps(mkRng(83), S, [[140, 140, 140], [190, 190, 190], [165, 165, 165], [150, 150, 150]], { w: 5, rgba: [20, 20, 20, 255], op: 255 }, [stamps(ell(26, 16), [205, 205, 205, 255], 30, 87, 40)]));+ MAT.flag = pbrMaterial({ albedo: flagA, normal: normalMapFromHeight(flagH, { strength: 2.2 }), roughness: 0.9, metalness: 0, name: 'Ground_Flagstone' });+ MAT.flag.vertexColors = true;+ const EA = 256, eArea = { x: 0, y: 0, width: EA, height: EA };+ const earthA = v3('albedo', [{ op: 'solid', color: 0x5d5a3a }, noise(0x4a4a2c, 0x6f6a44, 5, 2, 91, 0.8), noise(0x3f4226, 0x7a7550, 23, 2, 92, 0.4)], [stamps(tool(2, 8), [70, 88, 40, 255], 120, 93, 90, eArea), stamps(tool(-2, 7), [104, 112, 58, 255], 90, 94, 70, eArea), stamps(ell(26, 16), [88, 74, 52, 255], 30, 95, 60, eArea)], EA);+ MAT.earth = pbrMaterial({ albedo: earthA, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Ground_Earth' });+ MAT.earth.vertexColors = true;+ MAT.earth.depthWrite = false;+}++// ---- Atlas of the glass painter: pot-metal colours, painted drapery, drawn faces, ornament ------+const GL = { w: 1536, h: 1280, cell: 256, small: 128, fam: {}, tile: {}, face: {}, orn: {}, ground: {} };+// [name, base, light, dark]: real pot-metal glass is deep, and no two sheets match+const GLASS_FAMILIES = [+ ['ruby', [118, 6, 14], [172, 14, 22], [66, 3, 10]], ['cobalt', [10, 20, 104], [20, 44, 160], [5, 10, 62]], ['azure', [16, 54, 128], [36, 90, 170], [8, 32, 88]], ['emerald', [6, 62, 38], [14, 96, 54], [3, 38, 24]],+ ['green', [42, 74, 24], [74, 106, 34], [24, 48, 14]], ['gold', [178, 116, 16], [218, 158, 36], [130, 78, 8]], ['amber', [146, 72, 10], [186, 106, 20], [98, 44, 6]], ['purple', [58, 14, 80], [90, 26, 114], [34, 6, 50]],+ ['flesh', [186, 140, 104], [206, 164, 128], [156, 110, 80]], ['white', [172, 164, 132], [204, 194, 158], [136, 128, 100]], ['brown', [70, 40, 18], [100, 58, 28], [46, 24, 12]],+ ['teal', [8, 62, 70], [16, 92, 100], [5, 38, 46]], ['night', [5, 8, 40], [10, 16, 62], [2, 4, 24]],+];+const GLASS_ALIAS = { rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', sky: 'azure', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', yellow: 'gold', tan: 'amber', fleshDk: 'flesh', violet: 'purple', olive: 'green', forest: 'emerald', hair: 'brown', paint: 'lead' };+// Mosaic grounds, each a square of painted lozenges: [name, colour A, colour B, jewel]+// A ground stays one colour, in many sheets of it: ruby beside cobalt blends to magenta across a room, ruby on ruby does not+const GLASS_GROUNDS = [['rc', 'ruby', 'ruby', 'gold'], ['cr', 'cobalt', 'cobalt', 'gold'], ['eg', 'emerald', 'emerald', 'gold'], ['pr', 'cobalt', 'azure', 'white']];+// What a leaded window looks like from outside by day: nearly black quarries, each with a hint of its colour, and a faint grey came+function outerGlassOps() {+ const r = mkRng(4711), ops = [], cl = (v) => Math.max(0, Math.min(256, v)), hints = [[88, 10, 16], [12, 20, 96], [12, 20, 70], [70, 10, 14], [22, 26, 34], [66, 44, 12]];+ for (let b = 0; b <= 4; b++) for (let a = 0; a <= 4; a++) {+ if ((a + b) % 2 === 0) continue;+ const c = hints[Math.floor(r() * hints.length)], x = a * 64, y = b * 64;+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 64)], [cl(x + 64), cl(y)], [cl(x), cl(y + 64)], [cl(x - 64), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -1; k <= 1; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ for (let k = 1; k <= 3; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ return ops;+}+function groundOps(A, B, J, seed) { // lozenges 64 px across, a lead round each, a jewel on some crossings+ const r = mkRng(seed * 19 + 5), ops = [], cl = (v) => Math.max(0, Math.min(256, v));+ for (let b = 0; b <= 8; b++) for (let a = 0; a <= 8; a++) {+ if ((a + b) % 2 === 0) continue;+ const f = b % 2 ? A : B, x = a * 32, y = b * 32, t = r(), c = t < 0.45 ? f[1] : t < 0.8 ? f[2] : f[3];+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 32)], [cl(x + 32), cl(y)], [cl(x), cl(y + 32)], [cl(x - 32), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -3; k <= 3; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let k = 1; k <= 7; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let i = 0; i < 4; i++) { const x = 64 + (i % 2) * 128, y = 64 + (i >> 1) * 128; ops.push(paintEll(x, y, 13, 13, 255, [4, 4, 5])); ops.push(paintEll(x, y, 8, 8, 255, J[1])); }+ return ops.slice(0, 64);+}+const DRAPE_OF = { ruby: 'ruby', rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', cobalt: 'cobalt', azure: 'cobalt', sky: 'cobalt', night: 'cobalt', emerald: 'emerald', green: 'emerald', teal: 'emerald', forest: 'emerald', olive: 'emerald', gold: 'gold', amber: 'gold', yellow: 'gold', tan: 'gold', brown: 'gold', white: 'white', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', purple: 'purple', violet: 'purple' };+const GROUND_OF = { ruby: 'rc', rubyLt: 'rc', crimson: 'rc', cobalt: 'cr', azure: 'pr', sky: 'pr', emerald: 'eg', green: 'eg', violet: 'cr', purple: 'cr', teal: 'pr', outer: 'out' };+const PAINT = [36, 20, 10];+const paintLine = (a, b, w, op = 235) => ({ op: 'paint', shape: { kind: 'line', from: [Math.round(a[0]), Math.round(a[1])], to: [Math.round(b[0]), Math.round(b[1])], width: w }, rgba: [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintPoly = (pts, op, rgb) => ({ op: 'paint', shape: { kind: 'polygon', points: pts.map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintEll = (x, y, w, h, op, rgb) => ({ op: 'paint', shape: { kind: 'ellipse', x: Math.round(x - w / 2), y: Math.round(y - h / 2), width: Math.round(w), height: Math.round(h) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });++function familyOps(f, seed) { // one sheet of pot-metal: streaks, clouds, seeds, a thicker edge+ const r = mkRng(seed * 7 + 3), ops = [], C = GL.small, clampE = (x, w) => Math.max(w / 2, Math.min(C - w / 2, x));+ for (let i = 0; i < 10; i++) { const c = r() > 0.5 ? f[2] : f[3], w = 24 + Math.floor(r() * 60), h = 18 + Math.floor(r() * 44); ops.push(paintEll(clampE(r() * C, w), clampE(r() * C, h), w, h, 60 + Math.floor(r() * 90), c)); }+ for (let i = 0; i < 9; i++) { const c = r() > 0.5 ? f[2] : f[3], y = Math.floor(r() * C); ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y], to: [C, Math.max(0, Math.min(C, y + Math.floor((r() - 0.5) * 30)))], width: 2 + Math.floor(r() * 4) }, rgba: [c[0], c[1], c[2], 255], opacity: 70 + Math.floor(r() * 80) }); }+ ops.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 2, height: 2 }, rgba: [f[2][0], f[2][1], f[2][2], 255], count: 40, seed: seed + 11, area: { x: 0, y: 0, width: C, height: C }, opacity: 180 });+ return ops;+}+function drapeOps(f, seed) { // brown paint on coloured glass: long folds, each with a soft wash beside its trace line+ const r = mkRng(seed * 13 + 1), ops = [], W = 256, H = 512, dk = [Math.round(f[1][0] * 0.3), Math.round(f[1][1] * 0.3), Math.round(f[1][2] * 0.3)];+ for (let i = 0; i < 13; i++) {+ const x = 18 + i * 18 + (r() - 0.5) * 10, sw = (r() - 0.5) * 42, y0 = 4 + r() * 120, y1 = H - r() * 90, ym = (y0 + y1) / 2, wash = 9 + r() * 12;+ ops.push(paintPoly([[x, y0], [x + wash, y0 + 10], [x + sw * 0.6 + wash * 1.4, ym], [x + sw + wash, y1 - 10], [x + sw, y1], [x + sw * 0.6, ym]], 62 + Math.floor(r() * 36), PAINT));+ ops.push(paintLine([x, y0], [x + sw * 0.6, ym], 3)); ops.push(paintLine([x + sw * 0.6, ym], [x + sw, y1], 2));+ if (i % 3 === 0) ops.push(paintLine([x + sw * 0.6 + wash * 1.6, ym - 40], [x + sw + wash * 1.6, y1 - 60], 3, 150));+ }+ for (let i = 0; i < 4; i++) { const y = 60 + i * 110 + r() * 40; ops.push(paintPoly([[0, y], [W, y + 26 * (r() - 0.5)], [W, y + 22], [0, y + 18]], 40, PAINT)); }+ return ops.slice(0, 62);+}+// A drawn medieval head, 256 x 256, centred: trace lines in brown over a flesh pane, with washes for the modelling+function faceOps(kind) {+ const ops = [], c = 128, mir = (p) => [256 - p[0], p[1]], both = (fn) => { fn((p) => p); fn(mir); };+ const hairC = kind === 'elder' ? [150, 146, 122] : kind === 'angel' ? [150, 96, 30] : [66, 40, 18];+ // washes: the side of the face and temple, under the brow, beside the nose, under the lip+ both((m) => {+ ops.push(paintPoly([m([18, 84]), m([52, 74]), m([46, 150]), m([72, 214]), m([34, 196])], 78));+ ops.push(paintEll(m([88, 104])[0], 106, 58, 20, 64));+ ops.push(paintPoly([m([122, 118]), m([112, 160]), m([102, 172]), m([118, 172])], 58));+ });+ ops.push(paintEll(c, 200, 52, 11, 58));+ // hair+ if (kind !== 'virgin') {+ ops.push(paintPoly([[6, 118], [14, 56], [60, 10], [128, 0], [196, 10], [242, 56], [250, 118], [222, 96], [206, 56], [160, 42], [128, 54], [96, 42], [50, 56], [34, 96]], 255, hairC));+ for (let i = 0; i < 7; i++) { const a = 28 + i * 16; ops.push(paintLine([a, 94 - i * 7], [a + 36, 12 + i * 2], 3, 190)); ops.push(paintLine([256 - a, 94 - i * 7], [256 - a - 36, 12 + i * 2], 3, 190)); }+ } else {+ ops.push(paintPoly([[0, 150], [2, 56], [50, 8], [128, 0], [206, 8], [254, 56], [256, 150], [226, 118], [214, 66], [128, 46], [42, 66], [30, 118]], 255, [214, 216, 186]));+ ops.push(paintLine([38, 124], [50, 62], 4)); ops.push(paintLine([50, 62], [128, 44], 4)); ops.push(paintLine([128, 44], [206, 62], 4)); ops.push(paintLine([206, 62], [218, 124], 4));+ }+ // brows, lids, eyes+ both((m) => {+ ops.push(paintLine(m([60, 96]), m([84, 84]), 5)); ops.push(paintLine(m([84, 84]), m([116, 92]), 5));+ ops.push(paintLine(m([64, 116]), m([84, 104]), 4)); ops.push(paintLine(m([84, 104]), m([112, 116]), 4));+ ops.push(paintLine(m([66, 118]), m([90, 126]), 2)); ops.push(paintLine(m([90, 126]), m([112, 116]), 2));+ ops.push(paintEll(m([88, 115])[0], 115, 15, 15, 255));+ });+ // nose, nostril, mouth, chin+ ops.push(paintLine([120, 92], [114, 150], 4)); ops.push(paintLine([114, 150], [106, 166], 4)); ops.push(paintLine([106, 166], [124, 174], 3)); ops.push(paintLine([136, 172], [150, 166], 3));+ ops.push(paintLine([98, 192], [128, 187], 4)); ops.push(paintLine([128, 187], [158, 192], 4));+ ops.push(paintLine([112, 202], [144, 202], 2, 170));+ if (kind === 'elder' || kind === 'christ' || kind === 'king') {+ const bc = kind === 'elder' ? [168, 164, 138] : [76, 46, 20];+ ops.push(paintPoly([[40, 158], [66, 184], [94, 196], [128, 206], [162, 196], [190, 184], [216, 158], [206, 226], [160, 256], [96, 256], [50, 226]], 255, bc));+ for (let i = 0; i < 7; i++) { const x = 64 + i * 21; ops.push(paintLine([x, 192 + Math.abs(i - 3) * -3], [x + (i - 3) * 3, 252], 3, 210)); }+ ops.push(paintLine([78, 180], [120, 176], 4)); ops.push(paintLine([178, 180], [136, 176], 4));+ } else {+ ops.push(paintLine([100, 226], [128, 236], 3, 170)); ops.push(paintLine([128, 236], [156, 226], 3, 170));+ }+ return ops.slice(0, 64);+}+function ornOps(f, kind, seed) { // painted ornament for borders: a running stem with leaves, scratched out of a dark ground+ const ops = [], C = GL.small;+ ops.push(paintPoly([[0, 0], [C, 0], [C, C], [0, C]], 210));+ const lit = f[2];+ for (let i = 0; i < 5; i++) {+ const y = 13 + i * 26, s = i % 2 ? 1 : -1;+ ops.push(paintPoly([[C / 2, y - 11], [C / 2 + s * 36, y - 6], [C / 2 + s * 46, y + 7], [C / 2 + s * 23, y + 11], [C / 2, y + 13]], 255, lit));+ ops.push(paintLine([C / 2 + s * 4, y - 3], [C / 2 + s * 37, y + 4], 2));+ ops.push(paintLine([C / 2 + s * 4, y + 5], [C / 2 + s * 28, y + 6], 1));+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [C / 2, 0], to: [C / 2, C], width: 7 }, rgba: [lit[0], lit[1], lit[2], 255] });+ return ops;+}+async function makeGlassMaterials() {+ const regions = [], C = GL.cell, Sm = GL.small;+ const addR = (name, x, y, w, h, bg, ops) => regions.push({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'emissive', background: [bg[0], bg[1], bg[2], 255], operations: ops } } });+ // Layout: two rows of small cells (pot-metals, then ornament), then drapery, faces and mosaic grounds+ GLASS_FAMILIES.forEach((f, i) => { const x = (i % 12) * Sm, y = Math.floor(i / 12) * Sm; GL.fam[f[0]] = [x, y]; addR('fam_' + f[0], x, y, Sm, Sm, f[1], familyOps(f, i + 1)); });+ [['gold', 'gold'], ['white', 'white']].forEach((q, i) => { const f = GLASS_FAMILIES.find((v) => v[0] === q[0]), x = (i + 2) * Sm, y = Sm; GL.orn[q[1]] = [x, y]; addR('orn_' + q[1], x, y, Sm, Sm, f[1], ornOps(f, q[1], i + 90)); });+ // painted drapery, one tall tile per colour+ ['ruby', 'cobalt', 'emerald', 'gold', 'white', 'purple'].forEach((n, i) => { const f = GLASS_FAMILIES.find((q) => q[0] === n), x = i * C, y = C; GL.tile[n] = [x, y, C, 2 * C]; addR('drape_' + n, x, y, C, 2 * C, f[1], drapeOps(f, i + 40)); });+ // faces+ ['youth', 'elder', 'christ', 'king', 'virgin', 'angel'].forEach((n, i) => { const x = i * C, y = 3 * C; GL.face[n] = [x, y]; addR('face_' + n, x, y, C, C, n === 'virgin' ? [202, 162, 128] : [192, 148, 112], faceOps(n)); });+ // mosaic grounds+ GLASS_GROUNDS.forEach((q, i) => { const fam = (n) => GLASS_FAMILIES.find((v) => v[0] === n), x = i * C, y = 4 * C; GL.ground[q[0]] = [x, y]; addR('ground_' + q[0], x, y, C, C, fam(q[1])[3], groundOps(fam(q[1]), fam(q[2]), fam(q[3]), i + 3)); });+ GL.ground.out = [4 * C, 4 * C]; addR('ground_out', 4 * C, 4 * C, C, C, [10, 12, 18], outerGlassOps());+ GL.fam.lead = [1400, 140]; // the empty part of the sheet is black, and the came needs no pixels of its own+ const atlas = await textureAtlas({ schemaVersion: 1, usage: 'emissive', width: GL.w, height: GL.h, pack: { edge: 2048, gutter: 0 }, regions });+ // Seen from inside the glass is the only light: a black pane that shines+ MAT.glassIn = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow, roughness: 1, metalness: 0, name: 'StainedGlass_Interior' });+ MAT.glassIn.color.setRGB(0, 0, 0);+ // Seen from outside by day it is nearly black, a dull sheen and the colour only hinting through.+ // A rough surface keeps grazing angles from washing out to a pale reflection of the sky.+ MAT.glassOut = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.6, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior' });+ MAT.glassOut.color.setRGB(0.008, 0.009, 0.012);+ // Every other window from outside: the outer mosaic only, near black, with the came just catching the eye+ MAT.glassOutWin = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.62, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior_Windows' });+ MAT.glassOutWin.color.setRGB(0.008, 0.009, 0.012);+ // The great rose burns a little brighter than any other window in the building+ MAT.glassRose = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow * 1.22, roughness: 1, metalness: 0, name: 'StainedGlass_Rose' });+ MAT.glassRose.color.setRGB(0, 0, 0);+}++// Hierarchy helper: every mesh gets a unique role name; geometry is shared between placements.+let ROOT = null; const NAMES = new Map();+function uniq(n) { const c = (NAMES.get(n) || 0) + 1; NAMES.set(n, c); return c === 1 ? n : n + '_' + c; }+const STAT = new Map();+// Exporter costs measured on this host: pos+normal+uv 32 B, COLOR_0 written as 12 B floats+function batchBytes(b) { b.weld(); return b.count * (32 + (b.noColor ? 0 : 12)) + b.I.length * (b.count > 65535 ? 4 : 2); }+function place(name, batch, mat, o = {}) {+ if (!batch || !batch.I.length) return null;+ if (mat === MAT.gold) batch.noColor = true; // metal needs no baked shading: saves 12 B a vertex+ if (P.preview === 'unique' && batch.geo) return null; // size test: every geometry once, no instances+ if (mat === MAT.ashlar && !batch.tessellated && !batch.geo) { batch.tess(P.wallTessellation); batch.tessellated = true; }+ if (P.preview === 'stats') {+ const e = STAT.get(batch) || { name, n: 0, normalMapped: mat === MAT.ashlar || mat === MAT.lead || mat === MAT.pave || mat === MAT.oak };+ e.n++; STAT.set(batch, e);+ return null;+ }+ const part = createPart(uniq(name), batch.toGeo(), mat, { position: o.pos || [0, 0, 0], rotation: o.rot || [0, 0, 0], scale: o.scale || [1, 1, 1], parent: o.parent || ROOT });+ PLACED.push({ part, batch, mat });+ return part;+}+const PLACED = [];+function group(name, parent) { const g = new THREE.Group(); g.name = uniq(name); (parent || ROOT).add(g); return g; }++// ==================================================================================+// SCULPTURE: robed figures with carved drapery. A figure is a full surface, not a block:+// S-curve stance, tube folds breaking at the hem, swags across the body, sleeves, hands,+// modelled faces, hair and beards, and the attributes each saint carries.+// ==================================================================================+// Every figure is a signed-distance volume: a long column of cloth with deep pleats, a mantle+// laid over it, arms under the cloth, and a separately resolved head and hands. A surface-net+// pass extracts the skin, the simplifier keeps the carving and drops the rest, and ambient+// occlusion from the field itself is baked into the valleys of the folds.+const len3 = (x, y, z) => Math.sqrt(x * x + y * y + z * z);+const smin = (a, b, k) => { const h = Math.max(k - Math.abs(a - b), 0) / k; return Math.min(a, b) - h * h * k * 0.25; };+const smax = (a, b, k) => -smin(-a, -b, k);+const sdEll = (x, y, z, rx, ry, rz) => { const a = x / rx, b = y / ry, c = z / rz, k0 = Math.sqrt(a * a + b * b + c * c), d = a / rx, e = b / ry, f = c / rz, k1 = Math.sqrt(d * d + e * e + f * f); return k1 > 1e-9 ? k0 * (k0 - 1) / k1 : -Math.min(rx, ry, rz); };+// Rounded cone between two points: the workhorse of fingers, locks, lids and sleeves+function sdCone(x, y, z, ax, ay, az, bx, by, bz, r1, r2) {+ const px = x - ax, py = y - ay, pz = z - az, dx = bx - ax, dy = by - ay, dz = bz - az;+ const t = clamp((px * dx + py * dy + pz * dz) / (dx * dx + dy * dy + dz * dz));+ return len3(px - dx * t, py - dy * t, pz - dz * t) - (r1 + (r2 - r1) * t);+}+// Surface nets over a sparse grid: only cells near the surface are ever evaluated+function polygonize(F, box, cell, bandK = 1.75) {+ const [x0, y0, z0, x1, y1, z1] = box, nx = Math.ceil((x1 - x0) / cell) + 2, ny = Math.ceil((y1 - y0) / cell) + 2, nz = Math.ceil((z1 - z0) / cell) + 2;+ const N = nx * ny * nz, val = new Float32Array(N), seen = new Uint8Array(N), cellV = new Int32Array(N).fill(-1);+ const b4 = 3.46 * cell * bandK, b2 = 1.73 * cell * bandK, active = [], inA = new Uint8Array(N);+ const activate = (i, j, k) => { const q = i + nx * (j + ny * k); if (!inA[q]) { inA[q] = 1; active.push(i, j, k); } };+ const at = (i, j, k) => { const q = i + nx * (j + ny * k); if (!seen[q]) { seen[q] = 1; val[q] = F(x0 + i * cell, y0 + j * cell, z0 + k * cell); } return val[q]; };+ // Two-level descent: a coarse block, then its octants, then cells, so the field is only asked near its surface+ for (let kc = 0; kc < nz - 1; kc += 4) for (let jc = 0; jc < ny - 1; jc += 4) for (let ic = 0; ic < nx - 1; ic += 4) {+ if (Math.abs(F(x0 + (ic + 2) * cell, y0 + (jc + 2) * cell, z0 + (kc + 2) * cell)) >= b4) continue;+ for (let k2 = kc; k2 < Math.min(kc + 4, nz - 1); k2 += 2) for (let j2 = jc; j2 < Math.min(jc + 4, ny - 1); j2 += 2) for (let i2 = ic; i2 < Math.min(ic + 4, nx - 1); i2 += 2) {+ if (Math.abs(F(x0 + (i2 + 1) * cell, y0 + (j2 + 1) * cell, z0 + (k2 + 1) * cell)) >= b2) continue;+ for (let k = k2; k < Math.min(k2 + 2, nz - 1); k++) for (let j = j2; j < Math.min(j2 + 2, ny - 1); j++) for (let i = i2; i < Math.min(i2 + 2, nx - 1); i++) activate(i, j, k);+ }+ }+ const P = [], I = [], E = [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]], v = new Float64Array(8);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ let mask = 0;+ for (let c = 0; c < 8; c++) { const d = at(i + (c & 1), j + ((c >> 1) & 1), k + ((c >> 2) & 1)); v[c] = d; if (d < 0) mask |= 1 << c; }+ if (mask === 0 || mask === 255) continue;+ // Follow the surface through every face it crosses: a steep pleat can fool the coarse test above, and a skipped cell is a hole+ const crosses = (m) => (mask & m) !== 0 && (mask & m) !== m;+ if (i > 0 && crosses(0x55)) activate(i - 1, j, k);+ if (i < nx - 2 && crosses(0xaa)) activate(i + 1, j, k);+ if (j > 0 && crosses(0x33)) activate(i, j - 1, k);+ if (j < ny - 2 && crosses(0xcc)) activate(i, j + 1, k);+ if (k > 0 && crosses(0x0f)) activate(i, j, k - 1);+ if (k < nz - 2 && crosses(0xf0)) activate(i, j, k + 1);+ let sx = 0, sy = 0, sz = 0, n = 0;+ for (let e = 0; e < 12; e++) {+ const c0 = E[e][0], c1 = E[e][1], d0 = v[c0], d1 = v[c1];+ if ((d0 < 0) === (d1 < 0)) continue;+ const t = d0 / (d0 - d1);+ sx += lerp(c0 & 1, c1 & 1, t); sy += lerp((c0 >> 1) & 1, (c1 >> 1) & 1, t); sz += lerp((c0 >> 2) & 1, (c1 >> 2) & 1, t); n++;+ }+ cellV[i + nx * (j + ny * k)] = P.length / 3;+ P.push(x0 + (i + sx / n) * cell, y0 + (j + sy / n) * cell, z0 + (k + sz / n) * cell);+ }+ const quad = (a, b, c, d, flip) => { if (a < 0 || b < 0 || c < 0 || d < 0) return; if (flip) I.push(a, c, b, a, d, c); else I.push(a, b, c, a, c, d); };+ const cv = (i, j, k) => (i < 0 || j < 0 || k < 0 ? -1 : cellV[i + nx * (j + ny * k)]);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ const s0 = at(i, j, k) < 0;+ if (s0 !== (at(i + 1, j, k) < 0)) quad(cv(i, j - 1, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i, j - 1, k), !s0);+ if (s0 !== (at(i, j + 1, k) < 0)) quad(cv(i - 1, j, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i - 1, j, k), s0);+ if (s0 !== (at(i, j, k + 1) < 0)) quad(cv(i - 1, j - 1, k), cv(i, j - 1, k), cv(i, j, k), cv(i - 1, j, k), !s0);+ }+ return { P, I };+}+function meshToGeo(m) {+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(m.P), 3));+ g.setIndex(new THREE.BufferAttribute(m.P.length / 3 > 65535 ? new Uint32Array(m.I) : new Uint16Array(m.I), 1));+ g.computeVertexNormals();+ return g;+}+const AO_STEPS = [0.016, 0.05, 0.13];+// Extract, simplify, and write into a batch with field normals and field ambient occlusion+async function carveInto(b, F, box, cell, tris, xf, occ, opt = {}) {+ const raw = polygonize(F, box, cell);+ if (!raw.I.length) return;+ if (opt.cull) { // drop a region that a finer pass will carve instead+ const I2 = [], used = new Int32Array(raw.P.length / 3).fill(-1), P2 = [], mapV = (v) => { if (used[v] < 0) { used[v] = P2.length / 3; P2.push(raw.P[v * 3], raw.P[v * 3 + 1], raw.P[v * 3 + 2]); } return used[v]; };+ for (let t = 0; t < raw.I.length; t += 3) {+ const a = raw.I[t], c = raw.I[t + 1], e = raw.I[t + 2], mx = (raw.P[a * 3] + raw.P[c * 3] + raw.P[e * 3]) / 3, my = (raw.P[a * 3 + 1] + raw.P[c * 3 + 1] + raw.P[e * 3 + 1]) / 3, mz = (raw.P[a * 3 + 2] + raw.P[c * 3 + 2] + raw.P[e * 3 + 2]) / 3;+ if (!opt.cull(mx, my, mz)) I2.push(mapV(a), mapV(c), mapV(e));+ }+ raw.P = P2; raw.I = I2;+ }+ let g = meshToGeo(raw);+ if (raw.I.length / 3 > tris) g = await simplifyGeometry(g, { targetTriangles: tris });+ const pos = g.attributes.position, idx = g.index, base = b.count, h = cell * (opt.smooth || 0.6), aoK = opt.ao === undefined ? 0.5 : opt.ao, aoMin = opt.aoMin === undefined ? 0.16 : opt.aoMin;+ b.at(xf || M.I(), () => {+ for (let i = 0; i < pos.count; i++) {+ let x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i);+ const f0 = F(x, y, z), g0 = F(x + h, y, z) - F(x - h, y, z), g1 = F(x, y + h, z) - F(x, y - h, z), g2 = F(x, y, z + h) - F(x, y, z - h), l = len3(g0, g1, g2) || 1;+ const nx = g0 / l, ny = g1 / l, nz = g2 / l;+ // settle the vertex onto the true surface: this is what keeps fold valleys crisp+ x -= nx * f0; y -= ny * f0; z -= nz * f0;+ const O = occ || F;+ const wx = b.m[0] * x + b.m[1] * y + b.m[2] * z + b.m[3], wy = b.m[4] * x + b.m[5] * y + b.m[6] * z + b.m[7], wz = b.m[8] * x + b.m[9] * y + b.m[10] * z + b.m[11];+ const nm = b.nm, ax = nm[0] * nx + nm[1] * ny + nm[2] * nz, ay = nm[3] * nx + nm[4] * ny + nm[5] * nz, az = nm[6] * nx + nm[7] * ny + nm[8] * nz, al = len3(ax, ay, az) || 1;+ let o = 0, w = 1;+ for (let q = 0; q < 3; q++) { const d = AO_STEPS[q]; o += w * Math.max(0, d - O(wx + ax / al * d, wy + ay / al * d, wz + az / al * d)) / d; w *= 0.66; }+ const ao = clamp(1 - o * aoK, aoMin, 1);+ // UV by a slanted planar projection: no seam on a figure, the grain runs up the stone+ b.v(x, y, z, nx, ny, nz, wz * 0.8 + wx * 0.6, wy, ao);+ }+ for (let t = 0; t < idx.count; t += 3) b.tri(base + idx.getX(t), base + idx.getX(t + 1), base + idx.getX(t + 2));+ });+}++// ---- The head: a real skull, brow, eyes with lids, nose, lips, chin, hair, beard -------------+const LID_U = [[0.0795, 0.0035, 0.0165, 0.003], [0.0848, 0.0112, 0.0238, 0.0038], [0.0866, 0.0152, 0.0338, 0.0043], [0.0838, 0.0122, 0.0438, 0.0036], [0.0755, 0.0062, 0.052, 0.0024]];+const LID_L = [[0.0795, 0.0035, 0.0165, 0.0022], [0.0836, -0.0012, 0.0245, 0.0028], [0.0852, -0.0032, 0.034, 0.003], [0.0822, -0.0006, 0.044, 0.0026], [0.0755, 0.0062, 0.052, 0.0022]];+function headField(o) {+ const sm = o.smile || 0, cheek = o.gaunt ? 0.5 : 1, up = sm * 0.0065;+ const locks = [], rng = mkRng(o.seed * 37 + 11);+ // Long locks fall from the temples and the back of the head to the shoulders+ if (o.hair === 'long' || o.hair === 'curled') {+ const nL = o.hair === 'curled' ? 7 : 9;+ for (let i = 0; i < nL; i++) {+ const f = i / (nL - 1), a = lerp(1.05, 2.95, f), r0 = 0.092, wob = 0.012 + rng() * 0.008, dr = o.hair === 'curled' ? -0.165 : -0.235;+ for (const s of [-1, 1]) {+ const p0 = [r0 * Math.cos(a) - 0.012, 0.02 + rng() * 0.02, s * r0 * Math.sin(a) * 0.9];+ const p1 = [p0[0] * 1.14 - 0.008, p0[1] + dr * 0.5, p0[2] * 1.18 + s * wob];+ const p2 = [p0[0] * 1.02 - 0.03 * (1 - f * 0.5), p0[1] + dr, p0[2] * (1.28 - f * 0.3) - s * wob];+ locks.push([p0, p1, 0.02, 0.016], [p1, p2, 0.016, o.hair === 'curled' ? 0.012 : 0.007]);+ }+ }+ }+ if (o.hair === 'curls') { // rolled waves that frame the face: the smiling angel, the Child+ for (let i = 0; i < 11; i++) { const f = i / 10 - 0.5, a = f * 2.5, p0 = [0.104 * Math.cos(a) * 0.72 + 0.006, 0.094 - f * f * 0.11, 0.088 * Math.sin(a)], p1 = [p0[0] + 0.012, p0[1] - 0.03 - Math.abs(f) * 0.03, p0[2] * 1.16]; locks.push([p0, p1, 0.0165, 0.0115]); }+ for (let i = 0; i < 12; i++) { const a = 1.5 + i * 0.29, s = i % 2 ? 1 : -1, p0 = [0.09 * Math.cos(a) - 0.012, 0.0, s * 0.09 * Math.sin(a)], p1 = [p0[0] * 1.1, -0.062, p0[2] * 1.14]; locks.push([p0, p1, 0.019, 0.013]); }+ }+ const beardL = [];+ if (o.beard) {+ const nB = 7, L = o.beard === 2 ? 0.17 : 0.09;+ for (let i = 0; i < nB; i++) {+ const f = i / (nB - 1) - 0.5, z0 = f * 0.098, x0 = 0.078 - Math.abs(f) * 0.05, wob = (i % 2 ? 1 : -1) * 0.005;+ // separate locks with grooves between them; each one twists and tapers to a point+ beardL.push([[x0, -0.07 - Math.abs(f) * 0.012, z0], [x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], 0.0102, 0.0098]);+ beardL.push([[x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], [x0 - 0.006 + (o.beard === 2 ? 0.008 : 0), -0.07 - L - Math.abs(f) * 0.012, z0 * 0.5], 0.0098, 0.0035]);+ }+ }+ // Each lock is skipped outright when the query point is beyond its bounding sphere+ const sphere = (L) => { const dx = L[0][0] - L[1][0], dy = L[0][1] - L[1][1], dz = L[0][2] - L[1][2], R = Math.sqrt(dx * dx + dy * dy + dz * dz) / 2 + Math.max(L[2], L[3]) + 0.03; return [(L[0][0] + L[1][0]) / 2, (L[0][1] + L[1][1]) / 2, (L[0][2] + L[1][2]) / 2, R * R]; };+ const lockB = locks.map(sphere), beardB = beardL.map(sphere);+ const nearS = (S, x, y, z) => { const a = x - S[0], b = y - S[1], c = z - S[2]; return a * a + b * b + c * c < S[3]; };+ return (x, y, z) => {+ const az = Math.abs(z);+ let d = sdEll(x + 0.012, y - 0.03, z, 0.098, 0.098, 0.08); // cranium+ d = smin(d, sdEll(x - 0.018, y + 0.022, z, 0.074, 0.094, 0.0655), 0.02); // face mass+ d = smin(d, sdEll(x - 0.03, y + 0.076, z, 0.056, 0.046, 0.05 * (o.gaunt ? 0.93 : 1)), 0.018); // jaw+ d = smin(d, sdEll(x - 0.0675, y + 0.1, z, 0.019, 0.0175, 0.02), 0.012); // chin+ d = smin(d, sdCone(x, y, az, 0.0745, 0.031, 0, 0.066, 0.033, 0.05, 0.0118, 0.0088), 0.012); // brow ridge+ d = smin(d, sdEll(x - 0.054, y + 0.018 - up, az - 0.044, 0.026, 0.017 + sm * 0.003, 0.02), 0.015 * cheek); // cheekbone+ if (o.gaunt) d = smax(d, -sdEll(x - 0.05, y + 0.058, az - 0.058, 0.03, 0.022, 0.012), 0.012);+ if (x > 0.04 && y > -0.03 && y < 0.05 && az < 0.082) {+ d = smax(d, -sdEll(x - 0.0875, y - 0.0075, az - 0.034, 0.0225, 0.0118, 0.0215), 0.008); // eye socket+ d = smin(d, len3(x - 0.0655, y - 0.0065, az - 0.034) - 0.0178, 0.0018); // eyeball+ d = smax(d, -(len3(x - 0.0862, y - 0.0065, az - 0.034) - 0.0052), 0.0016); // the drilled pupil+ // lids: a carved almond, the upper rim heavier, meeting at the corners+ for (let i = 0; i < 4; i++) { const a = LID_U[i], c = LID_U[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0026); }+ for (let i = 0; i < 4; i++) { const a = LID_L[i], c = LID_L[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0022); }+ }+ if (x > 0.055 && y > -0.105 && y < 0.045 && az < 0.055) {+ // nose: bridge to tip, wings, nostrils+ d = smin(d, sdCone(x, y, z, 0.0835, 0.0225, 0, 0.105, -0.021, 0, 0.0058, 0.0092), 0.0085);+ d = smin(d, len3(x - 0.0925, y + 0.0295, az - 0.0108) - 0.0064, 0.005);+ d = smax(d, -(len3(x - 0.0975, y + 0.0355, az - 0.0062) - 0.0033), 0.002);+ // philtrum+ d = smax(d, -sdCone(x, y, z, 0.0975, -0.042, 0, 0.0965, -0.052, 0, 0.0028, 0.0034), 0.0035);+ // lips: upper in two wings, a fuller lower, a cut between; corners lift for a smile+ const my = -0.0615;+ d = smin(d, sdCone(x, y, az, 0.0925, my + 0.004, 0, 0.0815, my + 0.001 + up, 0.0215 + sm * 0.003, 0.0058, 0.0026), 0.005);+ d = smin(d, sdCone(x, y, az, 0.0915, my - 0.0085, 0, 0.0825, my - 0.003 + up, 0.0185 + sm * 0.003, 0.0072, 0.0028), 0.005);+ d = smax(d, -sdCone(x, y, az, 0.0965, my - 0.002, 0, 0.0825, my + up * 1.15, 0.0235 + sm * 0.004, 0.0017, 0.0021), 0.0022);+ d = smax(d, -sdEll(x - 0.0915, y + 0.0845, z, 0.012, 0.0055, 0.02), 0.006); // hollow under the lip+ }+ if (sm) d = smin(d, sdEll(x - 0.07, y + 0.044 - up * 2, az - 0.038, 0.02, 0.015, 0.016), 0.012); // the raised cheek of a smile+ // ear+ d = smin(d, sdEll(x + 0.012, y - 0.002, az - 0.08, 0.016, 0.03, 0.0085), 0.004);+ // neck, into the shoulders+ d = smin(d, sdCone(x, y, z, -0.01, -0.075, 0, -0.022, -0.215, 0, 0.041, 0.052), 0.018);+ // ---- hair ----+ if (o.hair && o.hair !== 'none') {+ const faceOpen = sdEll(x - 0.094, y + 0.012, z, 0.08, o.hair === 'curls' ? 0.106 : 0.118, 0.073);+ let h = sdEll(x + 0.014, y - 0.034, z, 0.104, 0.104, 0.09);+ if (o.hair === 'tonsure') h = smax(h, -sdEll(x + 0.012, y - 0.135, z, 0.075, 0.045, 0.062), 0.006);+ const an = Math.atan2(z, x + 0.014);+ if (o.hair !== 'curls') {+ const part = Math.exp(-(z * z) / 0.00007) * sstep(0.03, 0.11, y);+ h += 0.0028 * Math.sin(an * 22 + y * 34) + 0.007 * part;+ } else {+ h += 0.006 * Math.sin(an * 11 + y * 22) * Math.sin(y * 27 + an * 3);+ }+ for (let i = 0; i < locks.length; i++) { if (!nearS(lockB[i], x, y, z)) continue; const L = locks[i]; h = smin(h, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0055); }+ h = smax(h, -faceOpen, 0.0055);+ d = smin(d, h, 0.0025);+ }+ // ---- beard and moustache ----+ if (o.beard) {+ for (let i = 0; i < beardL.length; i++) { if (!nearS(beardB[i], x, y, z)) continue; const L = beardL[i]; d = smin(d, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0022); }+ d = smin(d, sdCone(x, y, az, 0.0955, -0.0475, 0.002, 0.081, -0.0625, 0.031, 0.0046, 0.003), 0.0025);+ d = smin(d, sdEll(x - 0.05, y + 0.058, az - 0.058, 0.022, 0.03, 0.012), 0.008); // sideburn into the jaw+ }+ // ---- veil: falls from the crown to the shoulders, cut open round the face ----+ if (o.veil) {+ // A soft hood: close to the skull, widening over the shoulders, its hem falling in loose folds+ const an = Math.atan2(z, x + 0.02);+ let w = smin(sdEll(x + 0.012, y - 0.032, z, 0.11, 0.108, 0.094), sdCone(x, y, z, -0.022, -0.05, 0, -0.038, -0.3, 0, 0.09, 0.158), 0.05);+ w -= 0.012 * (Math.sqrt(Math.abs(Math.sin(an * 4.5 + y * 2.2))) - 0.55) * sstep(-0.01, -0.16, y);+ w = smax(w, -0.325 + 0.022 * Math.sin(an * 5) - y, 0.008);+ const open = sdEll(x - 0.104, y + 0.016, z, 0.1, 0.116, 0.0715);+ d = smin(d, smax(w, -open, 0.004), 0.0015);+ }+ // ---- a queen's barbette: a linen band from the crown under the chin, framing a long grave face ----+ if (o.wimple) {+ const e = Math.sqrt(((y + 0.012) * (y + 0.012)) / (0.112 * 0.112) + (z * z) / (0.08 * 0.08)) - 1;+ d = smin(d, Math.max(Math.abs(e) * 0.09 - 0.0075, Math.abs(x - 0.018) - 0.036), 0.002);+ }+ // ---- mitre: two high peaks of stiff cloth, the bands left proud ----+ if (o.mitre) {+ // seen from the front a pointed arch, wide at the brow; seen from the side, two thin peaks+ const px = x + 0.012, py = y - 0.058, f = clamp(py / 0.27), w0 = 0.098 * (1 - Math.pow(f, 1.9)), dp = 0.102 * (1 - f * 0.86), lens = Math.abs(px) + az * 0.2;+ let m = Math.max(lens - dp, az - w0, -py);+ m = Math.max(m, py - 0.272);+ const band = Math.exp(-((py - 0.018) * (py - 0.018)) / 0.00016);+ m -= 0.0045 * band + 0.0038 * Math.exp(-(z * z) / 0.00012);+ d = smin(d, m, 0.004);+ }+ return d * 0.92;+ };+}++// ---- The hand: palm and jointed fingers that actually close on what they hold -------------+const HAND_POSES = {+ // [curl1, curl2, curl3] per finger (index..little), thumb [x,y,z of tip, spread]+ grip: { f: [[68, 88, 62], [72, 92, 64], [76, 92, 64], [80, 88, 62]], t: 'over' },+ support: { f: [[10, 18, 12], [12, 20, 14], [14, 22, 14], [18, 24, 16]], t: 'open' },+ edge: { f: [[46, 40, 26], [48, 42, 26], [50, 44, 28], [54, 44, 28]], t: 'open' },+ bless: { f: [[6, 8, 6], [8, 10, 8], [62, 90, 60], [68, 92, 60]], t: 'over' },+ rest: { f: [[28, 40, 26], [32, 42, 28], [36, 44, 28], [40, 46, 28]], t: 'open' },+};+function handField(pose, thumb = 1) {+ const P0 = HAND_POSES[pose], kn = [[0.078, 0.003, -0.027], [0.083, 0.004, -0.009], [0.08, 0.003, 0.009], [0.073, 0.001, 0.026]], segL = [[0.036, 0.024, 0.019], [0.04, 0.027, 0.02], [0.037, 0.025, 0.019], [0.029, 0.019, 0.016]], segs = [];+ for (let f = 0; f < 4; f++) {+ let ang = 0, p = kn[f].slice();+ for (let s = 0; s < 3; s++) {+ ang += rad(P0.f[f][s]);+ const q = [p[0] + Math.cos(ang) * segL[f][s], p[1] - Math.sin(ang) * segL[f][s], p[2]];+ segs.push([p, q, s === 0 ? 0.0098 : s === 1 ? 0.0088 : 0.0078, s === 2 ? 0.0066 : s === 0 ? 0.0088 : 0.0078]);+ p = q;+ }+ }+ const tb = [0.022, -0.006, thumb * 0.03], tm = P0.t === 'over' ? [0.052, -0.03, thumb * 0.033] : [0.05, -0.012, thumb * 0.054], tt = P0.t === 'over' ? [0.078, -0.044, thumb * 0.012] : [0.082, -0.016, thumb * 0.056];+ return (x, y, z) => {+ let d = Math.max(Math.abs(x - 0.04) - 0.043, Math.abs(y) - 0.0125, Math.abs(z) - 0.036) - 0.006; // palm, gently rounded+ d = smin(d, sdCone(x, y, z, -0.02, 0, 0, 0.02, 0, 0, 0.026, 0.033), 0.012); // wrist heel+ for (let i = 0; i < segs.length; i++) { const S = segs[i]; d = smin(d, sdCone(x, y, z, S[0][0], S[0][1], S[0][2], S[1][0], S[1][1], S[1][2], S[2], S[3]), 0.0022); }+ d = smin(d, sdCone(x, y, z, tb[0], tb[1], tb[2], tm[0], tm[1], tm[2], 0.0145, 0.0098), 0.006);+ d = smin(d, sdCone(x, y, z, tm[0], tm[1], tm[2], tt[0], tt[1], tt[2], 0.0098, 0.0078), 0.002);+ return d;+ };+}+// Heads are shared meshes placed on their own node: the same face can turn and tilt per figure at no byte cost.+const HEAD_MESH = {};+const HEADS = {+ bearded: { hair: 'long', beard: 1, seed: 2 },+ mitred: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 },+ royal: { hair: 'curled', beard: 2, seed: 10 },+ veiled: { veil: 1, smile: 0.3, seed: 8 },+ queen: { veil: 1, gaunt: 1, wimple: 1, seed: 5 },+ angelic: { hair: 'curls', smile: 1, seed: 13 },+};+async function headBatch(key) {+ if (HEAD_MESH[key]) return HEAD_MESH[key];+ const hO = HEADS[key], b = new Batch(TILE.fine), hF = headField(hO);+ const hB = [-0.19, hO.veil ? -0.36 : -0.26, -0.2, 0.17, hO.mitre ? 0.36 : 0.16, 0.2];+ // Hair and skull at a working grid, then the face alone on the finest grid with most of the triangles+ const FB = [0.024, -0.13, -0.08, 0.128, 0.088, 0.08];+ const inFace = (x, y, z) => x > FB[0] + 0.003 && y > FB[1] + 0.003 && y < FB[4] - 0.003 && z > FB[2] + 0.003 && z < FB[5] - 0.003;+ await carveInto(b, hF, hB, 0.0048, hO.veil ? 1300 : hO.beard ? 1900 : 1700, null, hF, { smooth: 1.4, ao: 0.34, aoMin: 0.3, cull: inFace });+ await carveInto(b, hF, FB, 0.0024, hO.beard ? 2500 : 2700, null, hF, { smooth: 0.9, ao: 0.3, aoMin: 0.34 });+ b.isFigure = true;+ HEAD_MESH[key] = b;+ return b;+}+const HAND_MESH = {};+async function handBatch(pose, thumb) { // hands are cached: a grip is a grip, whoever's it is+ const key = pose + thumb;+ if (!HAND_MESH[key]) { const b = new Batch(TILE.fine), F = handField(pose, thumb); await carveInto(b, F, [-0.05, -0.1, -0.1, 0.16, 0.05, 0.1], 0.0034, 520); HAND_MESH[key] = b; }+ return HAND_MESH[key];+}+function copyBatch(dst, src, m, shadeFn) { // transform a finished batch into another+ const base = dst.count;+ dst.at(m, () => {+ for (let i = 0; i < src.count; i++) { const i3 = i * 3, i2 = i * 2; dst.v(src.P[i3], src.P[i3 + 1], src.P[i3 + 2], src.N[i3], src.N[i3 + 1], src.N[i3 + 2], src.UV[i2] * src.tile, src.UV[i2 + 1] * src.tile, (src.C[i3] / 255) * (shadeFn ? shadeFn(src.P[i3], src.P[i3 + 1], src.P[i3 + 2]) : 1)); }+ for (let t = 0; t < src.I.length; t += 3) dst.tri(base + src.I[t], base + src.I[t + 1], base + src.I[t + 2]);+ });+}+const frameOf = (p, X, Y) => { // rigid frame: origin p, local X and Y given, Z = X x Y+ const lx = len3(X[0], X[1], X[2]), xn = [X[0] / lx, X[1] / lx, X[2] / lx], d = Y[0] * xn[0] + Y[1] * xn[1] + Y[2] * xn[2];+ let yy = [Y[0] - d * xn[0], Y[1] - d * xn[1], Y[2] - d * xn[2]]; const ly = len3(yy[0], yy[1], yy[2]); yy = yy.map((c) => c / ly);+ const z = [xn[1] * yy[2] - xn[2] * yy[1], xn[2] * yy[0] - xn[0] * yy[2], xn[0] * yy[1] - xn[1] * yy[0]];+ return [xn[0], yy[0], z[0], p[0], xn[1], yy[1], z[1], p[1], xn[2], yy[2], z[2], p[2]];+};+const invRigid = (m) => (x, y, z) => { const a = x - m[3], b = y - m[7], c = z - m[11]; return [m[0] * a + m[4] * b + m[8] * c, m[1] * a + m[5] * b + m[9] * c, m[2] * a + m[6] * b + m[10] * c]; };++// ---- The people. Bodies are long columns of cloth; each differs in dress, stance, head, gesture ----+const FIGURES = {+ apostle: { seed: 2, sway: 0.018, dress: 'mantle', headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 2 }, hold: 'key' },+ bishop: { seed: 4, sway: 0.008, dress: 'chasuble', headKey: 'mitred', head: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 }, hold: 'crozier' },+ king: { seed: 10, sway: 0.022, dress: 'royal', headKey: 'royal', head: { hair: 'curled', beard: 2, crown: 1, seed: 10 }, hold: 'sceptre' },+ queen: { seed: 5, sway: 0.03, dress: 'royal', slim: 0.93, headKey: 'queen', head: { veil: 1, gaunt: 1, wimple: 1, crown: 1, seed: 5, turn: 8 }, hold: 'mantle' },+ angel: { seed: 13, sway: 0.05, dress: 'angel', slim: 0.94, wings: 1, headKey: 'angelic', head: { hair: 'curls', smile: 1, seed: 13, tilt: 11, turn: 15 }, hold: 'scroll' },+ virgin: { seed: 8, sway: 0.062, dress: 'mantle', slim: 0.95, headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, tilt: 9, turn: -16 }, hold: 'child' },+};+const FIG_ALIAS = { peter: 'apostle', paul: 'apostle', prophet: 'apostle', deacon: 'apostle', martyr: 'queen', kingA: 'king', kingB: 'king', kingC: 'king', christ: 'seated' };++function bodyField(o) {+ const yN = 1.7, rng = mkRng(o.seed * 991 + 7), ph = []; for (let i = 0; i < 8; i++) ph.push(rng() * TAU);+ const sw = o.sway || 0, sl = o.slim || 1, L = 256;+ const keys = [[0, 0.178, 0.205, 0.218], [0.05, 0.165, 0.192, 0.204], [0.3, 0.138, 0.165, 0.172], [0.52, 0.124, 0.15, 0.158], [0.66, 0.112, 0.128, 0.146], [0.8, 0.116, 0.12, 0.15], [0.885, 0.108, 0.112, 0.15], [0.94, 0.09, 0.094, 0.13], [0.975, 0.066, 0.07, 0.09], [1, 0.044, 0.046, 0.05]];+ const Af = new Float32Array(L), Ab = new Float32Array(L), Bs = new Float32Array(L), Cx = new Float32Array(L), Cz = new Float32Array(L), Amp = new Float32Array(L);+ for (let i = 0; i < L; i++) {+ const t = i / (L - 1), k = keyf(keys, t);+ Af[i] = k[0]; Ab[i] = k[1]; Bs[i] = k[2] * sl;+ Cx[i] = 0.014 * Math.sin(t * PI);+ Cz[i] = sw * (Math.sin(t * PI * 0.92) * 1.15 - 0.62 * Math.sin(t * PI * 2));+ // pleats deepest over the shins, gathered at the belt, slight in the bodice+ Amp[i] = t < 0.645 ? lerp(0.05, 0.03, sstep(0, 0.6, t)) : lerp(0.014, 0.004, sstep(0.66, 0.9, t));+ }+ const n1 = o.dress === 'chasuble' ? 16 : 14, n2 = 25, yB = 0.655 * yN;+ const dress = o.dress, zc = Cz[Math.round(0.86 * (L - 1))], cxS = Cx[Math.round(0.86 * (L - 1))];+ const Rs = 0.142 * sl;+ const arms = armPose(o, Rs, zc, cxS);+ // Feet: pointed medieval shoes emerge from the hem+ const foot = (x, y, z) => { const az = Math.abs(z - Cz[0]); let d = sdCone(x, y, az, 0.02, 0.042, 0.082, 0.2, 0.024, 0.098, 0.05, 0.024); d = smin(d, sdCone(x, y, az, 0.0, 0.05, 0.082, 0.02, 0.2, 0.07, 0.044, 0.04), 0.02); d = smax(d, -sdEll(x - 0.11, y + 0.01, az - 0.09, 0.08, 0.02, 0.1), 0.004); return Math.max(d, -y); };+ return {+ arms,+ F: (x, y, z) => {+ const t = clamp(y / yN), i = (t * (L - 1)) | 0, px = x - Cx[i], pz = z - Cz[i], rho = Math.sqrt(px * px + pz * pz) + 1e-6, c = px / rho, s = pz / rho, a = c > 0 ? Af[i] : Ab[i], b = Bs[i];+ const R = 1 / Math.sqrt((c * c) / (a * a) + (s * s) / (b * b)), th = Math.atan2(pz, px);+ // ---- the tunic: tube pleats with valleys you could put a finger in ----+ const u1 = th * n1 * 0.5 + ph[0] + y * 0.5 * Math.sign(sw || 1), u2 = th * n2 * 0.5 + ph[1] - y * 0.9;+ const p1 = Math.sqrt(Math.abs(Math.sin(u1))), p2 = Math.sqrt(Math.abs(Math.sin(u2)));+ let r = R + Amp[i] * (p1 * 0.76 + p2 * 0.24 - 0.64);+ r += sstep(0.14, 0, y) * (0.018 + 0.016 * Math.sin(th * 7 + ph[4])); // the skirt spreads and breaks at the foot+ r += 0.008 * Math.exp(-((y - yB) * (y - yB)) / 0.00018); // belt+ r -= 0.012 * sstep(0.03, 0, Math.abs(y - yB) - 0.012) * 0.2;+ r += 0.006 * Math.exp(-((y - yN + 0.075) * (y - yN + 0.075)) / 0.00012); // collar+ let d = rho - r;+ const hem = 0.055 + 0.038 * p1 * (c > -0.2 ? 1 : 0.4);+ d = smax(d, hem - y, 0.006);+ // ---- the over-garment ----+ if (dress === 'mantle') {+ const up = 0.78 * yN + 0.2 * yN * Math.max(0, Math.sin(-th * Math.sign(sw || 1) + 0.5)) + 0.25 * sstep(1.2, 2.6, Math.abs(th)), low = 0.33 * yN - 0.18 * yN * sstep(0.5, 2.8, Math.abs(th)) + 0.02 * Math.sin(th * 11 + ph[5]) + 0.012 * Math.sin(th * 23);+ const reg = Math.max(low - y, y - up);+ // Folds pulled on the diagonal from the held hip to the far shoulder: two scales, never in step+ const th0 = 0.25 * Math.sign(sw || 1), q = y + 0.15 * th * Math.sign(sw || 1) + 0.055 * (th - th0) * (th - th0);+ const swag = 0.62 * Math.sqrt(Math.abs(Math.sin(q * 12.5 + ph[6]))) + 0.38 * Math.sqrt(Math.abs(Math.sin(q * 27 + ph[7]))), back = Math.sqrt(Math.abs(Math.sin(th * 5 + ph[3])));+ const fr = sstep(1.9, 1.1, Math.abs(th));+ const rm = R + 0.03 + 0.04 * lerp(back - 0.6, swag - 0.58, fr) * sstep(up + 0.01, up - 0.08, y);+ d = smin(d, smax(rho - rm, reg, 0.007), 0.002);+ } else if (dress === 'chasuble') {+ // alb to the feet, dalmatic to the shin, the bell of the chasuble hanging from the arms in U folds+ const dal = Math.max(0.3 * yN + 0.012 * Math.sin(th * 9) - y, y - 0.86 * yN);+ d = smin(d, smax(rho - (R + 0.016 + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 6 + ph[2]))) - 0.5)), dal, 0.005), 0.0015);+ const lowC = (0.42 + 0.26 * sstep(0.2, 1.5, Math.abs(th)) - 0.2 * sstep(1.7, 3.0, Math.abs(th))) * yN + 0.012 * Math.sin(th * 13);+ const swag = 0.65 * Math.sqrt(Math.abs(Math.sin((y + 0.2 * th * th) * 27 + ph[6]))) + 0.35 * Math.sqrt(Math.abs(Math.sin((y + 0.13 * th * th) * 44 + ph[7])));+ const rc = R + 0.03 * sstep(yN, 0.9 * yN, y) + 0.022 * (swag - 0.58) * sstep(0.84 * yN, 0.74 * yN, y) * (1 - 0.6 * sstep(0, 0.2, 0.2 - Math.abs(th)));+ d = smin(d, smax(rho - rc, lowC - y, 0.007), 0.003);+ // the orphrey: a Y-band across the breast and down the front, left proud+ const py = y - 0.79 * yN;+ if (Math.abs(th) < 1.2 && y > lowC - 0.01 && y < 0.94 * yN) d = smin(d, Math.max(rho - rc - 0.011, py > 0 ? Math.abs(Math.abs(th) - py * 4.2) - 0.085 : Math.abs(th) - 0.09), 0.003);+ } else if (dress === 'royal') {+ // a heavy mantle open down the front, thrown back over the shoulders, deep folds to the floor+ const open = lerp(0.72, 0.28, sstep(0.05, 0.86, t)) + 0.05 * Math.sin(y * 19 + ph[2]);+ const reg = Math.max(open - Math.abs(th - 0.05), 0.05 - y);+ const hv = Math.sqrt(Math.abs(Math.sin(th * 4 + ph[3] + y * 0.3)));+ const collar = 0.022 * Math.exp(-((y - 0.915 * yN) * (y - 0.915 * yN)) / 0.0012); // a soft roll at the neck+ const rm = R + 0.03 * sstep(yN, 0.9 * yN, y) + collar + sstep(0.84 * yN, 0.5 * yN, y) * 0.056 * (hv - 0.62);+ d = smin(d, smax(rho - rm, reg, 0.006), 0.004);+ } else if (dress === 'angel') {+ // a girdled tunic blown a little to one side: fine folds and a bloused top+ const blouse = 0.018 * Math.exp(-((y - yB - 0.06) * (y - yB - 0.06)) / 0.003);+ d -= blouse;+ const low = 0.74 * yN + 0.02 * Math.sin(th * 7) - y;+ d = smin(d, smax(rho - (R + 0.016 * sstep(yN, 0.9 * yN, y) + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 7 + ph[2]))) - 0.5) * sstep(0.9 * yN, 0.8 * yN, y)), low, 0.005), 0.003);+ }+ // ---- shoulders and sleeves ----+ const yS = yN - 0.155;+ for (let k = 0; k < 2; k++) { const sg = k ? 1 : -1; d = smin(d, sdEll(x - cxS, y - yS, z - zc - sg * Rs * 0.98, 0.07, 0.078, 0.058), 0.05); }+ for (let k = 0; k < arms.length; k++) {+ const A = arms[k];+ let ad = sdCone(x, y, z, A.S[0], A.S[1], A.S[2], A.E[0], A.E[1], A.E[2], 0.056, 0.05);+ // forearm in a sleeve that widens to the cuff, with a roll of cloth at the wrist+ const fx = A.W[0] - A.E[0], fy = A.W[1] - A.E[1], fz = A.W[2] - A.E[2], fl = fx * fx + fy * fy + fz * fz, ft = clamp(((x - A.E[0]) * fx + (y - A.E[1]) * fy + (z - A.E[2]) * fz) / fl);+ const qx = x - A.E[0] - fx * ft, qy = y - A.E[1] - fy * ft, qz = z - A.E[2] - fz * ft, ql = len3(qx, qy, qz);+ const rr = lerp(0.05, 0.062, ft) + 0.006 * Math.sin(ft * 15 + k) * (1 - ft) + 0.008 * Math.exp(-((ft - 0.97) * (ft - 0.97)) / 0.002);+ ad = smin(ad, ql - rr, 0.018);+ // the cloth that falls from under the forearm: a hanging curtain of pleats+ // longest under the wrist, thinning as it falls, its lower edge never straight+ if (A.drape) {+ const h = A.E[1] + fy * ft - y;+ if (h > -0.04 && h < A.drape * 1.1) {+ const fall = clamp(h / A.drape), crv = lerp(0.044, 0.016, fall) + 0.014 * (Math.sqrt(Math.abs(Math.sin(ft * 12 + h * 2.5 + k))) - 0.5);+ const curtain = len3(qx * 0.72, 0, qz) - crv;+ const edge = h - A.drape * (0.28 + 0.72 * ft) - 0.022 * Math.sin(ft * 11 + k * 2);+ ad = smin(ad, smax(curtain, edge, 0.012), 0.018);+ }+ }+ d = smin(d, ad, 0.014);+ }+ // ---- feet ----+ d = smin(d, foot(x, y, z), 0.006);+ return Math.max(d, y - yN - 0.03);+ },+ };+}+// Where the arms go, and what the hands do when they get there+function armPose(o, Rs, zc, cx) {+ const y = 1.55, shR = [cx, y, zc + Rs], shL = [cx, y, zc - Rs], arm = (S, E, W, drape) => ({ S, E, W, drape });+ switch (o.hold) {+ case 'key': return [arm(shR, [0.03, 1.2, zc + Rs + 0.05], [0.17, 1.32, zc + 0.12], 0.32), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.17, 1.14, zc - 0.07], 0.34)];+ case 'crozier': return [arm(shR, [0.04, 1.2, zc + Rs + 0.06], [0.2, 1.3, zc + 0.14], 0.3), arm(shL, [0.05, 1.22, zc - Rs - 0.03], [0.2, 1.42, zc - 0.05], 0.3)];+ case 'sceptre': return [arm(shR, [0.04, 1.18, zc + Rs + 0.06], [0.2, 1.28, zc + 0.15], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.03], [0.17, 1.18, zc - 0.05], 0.3)];+ case 'mantle': return [arm(shR, [0.04, 1.14, zc + Rs + 0.05], [0.18, 1.2, zc + 0.04], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.15, 1.4, zc - 0.1], 0.26)];+ case 'scroll': return [arm(shR, [0.05, 1.2, zc + Rs + 0.04], [0.2, 1.34, zc + 0.1], 0.28), arm(shL, [0.05, 1.2, zc - Rs - 0.04], [0.2, 1.22, zc - 0.08], 0.28)];+ case 'child': return [arm(shR, [0.05, 1.14, zc + Rs + 0.04], [0.2, 1.24, zc + 0.03], 0.32), arm(shL, [0.05, 1.14, zc - Rs - 0.02], [0.2, 1.18, zc - 0.13], 0.3)];+ case 'bless': return [arm(shR, [0.07, 1.22, zc + Rs + 0.09], [0.19, 1.5, zc + 0.15], 0.3), arm(shL, [0.03, 1.2, zc - Rs - 0.04], [0.17, 1.2, zc - 0.07], 0.34)];+ default: return [arm(shR, [0.02, 1.12, zc + Rs + 0.04], [0.15, 1.05, zc + 0.08], 0.3), arm(shL, [0.02, 1.12, zc - Rs - 0.04], [0.15, 1.05, zc - 0.08], 0.3)];+ }+}+// ---- Things held: real objects, modelled at the scale of the hands that grip them ---------+function addBook(b, m, o = {}) {+ b.at(m, () => {+ const w = o.w || 0.082, h = o.h || 0.112, th = o.th || 0.027;+ b.box(-h, h, -th, -th + 0.009, -w, w, { s: 0.85 }); b.box(-h, h, th - 0.009, th, -w, w, { s: 1 }); // boards+ b.box(-h + 0.006, h - 0.006, -th + 0.009, th - 0.009, -w + 0.006, w - 0.004, { s: 0.62 }); // pages+ b.box(-h, h, -th, th, -w - 0.009, -w, { s: 0.8 }); // spine+ for (const i of [-1, 1]) for (const k of [-1, 1]) b.at(M.T(i * (h - 0.026), th, k * (w - 0.026)), () => b.lathe([[0.016, 0], [0.012, 0.007], [0.0, 0.009]], 6));+ b.at(M.T(0, th, 0), () => b.lathe([[0.03, 0], [0.024, 0.006], [0.0, 0.009]], 8));+ });+}+function addAttrs(fig, o, arms) {+ const { stone: b, gold: g } = fig, R = arms[0], Lf = arms[1];+ switch (o.hold) {+ case 'key': {+ // The great key of Peter held upright in the fist, the book against the left forearm+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01];+ b.tube([[hp[0], hp[1] - 0.15, hp[2]], [hp[0], hp[1] + 0.2, hp[2]]], 0.0125, 8, { capStart: true });+ b.at(M.T(hp[0], hp[1] + 0.245, hp[2]), () => b.at(M.Rx(90), () => { b.lathe([[0.04, -0.01], [0.05, 0], [0.04, 0.01], [0.026, 0.01], [0.02, 0], [0.026, -0.01], [0.04, -0.01]], 14); }));+ b.box(hp[0] - 0.01, hp[0] + 0.01, hp[1] - 0.15, hp[1] - 0.075, hp[2], hp[2] + 0.07, { s: 0.9 });+ b.box(hp[0] - 0.012, hp[0] + 0.012, hp[1] - 0.112, hp[1] - 0.1, hp[2] + 0.03, hp[2] + 0.048, { s: 0.3 });+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2] + 0.0), M.Rz(-64), M.Ry(8)));+ break;+ }+ case 'crozier': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 2.02;+ b.tube([[hp[0], 0.04, hp[2]], [hp[0], top, hp[2]]], 0.0145, 8, { capStart: true });+ for (const yK of [hp[1] + 0.22, top - 0.05]) b.at(M.T(hp[0], yK, hp[2]), () => b.lathe([[0.016, -0.03], [0.034, 0], [0.016, 0.03]], 8));+ const vol = []; for (let i = 0; i <= 20; i++) { const t = i / 20, a = -PI * 0.5 + t * PI * 2.5, r = lerp(0.115, 0.036, t); vol.push([hp[0] + r * Math.cos(a) * 1.0 + 0.0, top + 0.12 + r * Math.sin(a), hp[2]]); }+ g.tube(vol, (t) => lerp(0.02, 0.012, t), 7, { capStart: true });+ for (let i = 2; i < 16; i += 3) { const p = vol[i]; addCrocket(g, M.of(M.T(p[0], p[1], p[2]), M.Rz((i / 20) * 400 - 90)), 0.06, 0); }+ break;+ }+ case 'sceptre': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 1.95;+ b.tube([[hp[0], hp[1] - 0.2, hp[2]], [hp[0], top, hp[2]]], 0.0125, 8, { capStart: true });+ g.at(M.T(hp[0], top, hp[2]), () => {+ g.lathe([[0.0125, 0], [0.03, 0.022], [0.038, 0.05], [0.026, 0.078], [0.01, 0.086]], 10);+ for (let i = 0; i < 3; i++) { const a = i * 120 + 90; g.at(M.Ry(a), () => g.tube(spline([[0, 0.07, 0], [0.038, 0.1, 0], [0.042, 0.15, 0], [0.018, 0.17, 0]], 2), (t) => [lerp(0.016, 0.003, t), lerp(0.024, 0.004, t)], 6)); }+ });+ break;+ }+ case 'scroll': {+ const p = [Lf.W[0] + 0.07, Lf.W[1], Lf.W[2]];+ const path = []; for (let i = 0; i <= 14; i++) { const t = i / 14; path.push([R.W[0] + 0.09 + 0.02 * Math.sin(t * 5), R.W[1] - t * 0.7 + 0.03, lerp(R.W[2] - 0.03, Lf.W[2] + 0.05, sstep(0, 0.5, t)) + 0.02 * Math.sin(t * 7)]); }+ const sheet = (off, flip) => b.grid((u, v) => { const k = u * 14, i = Math.min(13, Math.floor(k)), f = k - i, q = path[i], w = path[i + 1]; return [lerp(q[0], w[0], f) + off, lerp(q[1], w[1], f), lerp(q[2], w[2], f) + (v - 0.5) * 0.15, 0.96]; }, 14, 1, { flip });+ sheet(0, false); sheet(-0.004, true);+ b.at(M.of(M.T(path[14][0], path[14][1] - 0.015, path[14][2]), M.Rx(90)), () => b.lathe([[0.022, -0.085], [0.024, -0.08], [0.024, 0.08], [0.022, 0.085]], 8));+ break;+ }+ case 'bless': {+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2]), M.Rz(-64), M.Ry(4)));+ break;+ }+ default: break;+ }+}+const CROWN_FLEURS = 8;+function addCrown(g, m, R0, tall = 1) {+ g.at(m, () => {+ g.lathe([[R0, 0], [R0 + 0.009, 0.004], [R0 + 0.009, 0.028], [R0 + 0.015, 0.038], [R0 + 0.006, 0.042], [R0 - 0.004, 0.034], [R0 - 0.004, 0]], 26);+ // jewels set in the band+ for (let i = 0; i < 12; i++) { const a = i * TAU / 12; blob(g, [(R0 + 0.013) * Math.cos(a), 0.02, (R0 + 0.013) * Math.sin(a)], [0.0072, 0.0072, 0.0072], 5, 3); }+ // Fleurs-de-lis cut from sheet: flat plates set on the rim, the way a goldsmith makes them+ const h = 0.05 * tall, fl = [[-0.03, 0], [0.03, 0], [0.024, h * 0.2], [0.04, h * 0.42], [0.031, h * 0.62], [0.017, h * 0.42], [0.009, h * 0.5], [0.012, h * 0.76], [0, h * 1.1], [-0.012, h * 0.76], [-0.009, h * 0.5], [-0.017, h * 0.42], [-0.031, h * 0.62], [-0.04, h * 0.42], [-0.024, h * 0.2]];+ for (let i = 0; i < CROWN_FLEURS; i++) {+ const a = (i + 0.5) * 360 / CROWN_FLEURS;+ g.at(M.of(M.Ry(90 - a), M.T(0, 0.03, R0 + 0.004)), () => g.slab(fl, [], -0.004, 0.004, { so: 1, sr: 1 }));+ }+ });+}++const figF = {}; // fields kept while a figure is carved+const yNeck = 1.7;+async function makeFigure(kind) {+ const o = FIGURES[kind], stone = new Batch(TILE.fine), gold = new Batch(TILE.gold);+ const body = bodyField(o), arms = body.arms, hO = Object.assign({ seed: o.seed }, o.head);+ // Head frame: gently inclined, sitting on the neck above the centre of the shoulders+ const zc = arms[0].S[2] - (0.142 * (o.slim || 1)), cx = arms[0].S[0];+ const headM = M.of(M.T(cx + 0.024, yNeck + 0.116, zc + (o.sway || 0) * 0.3), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(-5));+ const hF = headField(hO), toHead = invRigid(headM);+ const headW = (x, y, z) => { const q = toHead(x, y, z); return hF(q[0], q[1], q[2]); };+ // Hands: frames derived from the sleeve so that the wrist always leaves the cuff+ const hands = [];+ const handFor = (A, side, pose) => {+ const X0 = [A.W[0] - A.E[0], A.W[1] - A.E[1], A.W[2] - A.E[2]];+ let Y0;+ if (pose === 'support') Y0 = [0.2, -1, 0]; // palm up under a book+ else if (pose === 'bless') Y0 = [0, 0.1, side]; // palm turned forward, two fingers raised+ else Y0 = [0, 0, side]; // back of the hand outward+ const X = pose === 'bless' ? [0.25, 1, 0.05] : X0;+ return { m: frameOf(A.W, X, Y0), pose, thumb: pose === 'grip' || pose === 'edge' || pose === 'rest' ? -side : side };+ };+ const handsCfg = { key: ['grip', 'support'], crozier: ['grip', 'rest'], sceptre: ['grip', 'rest'], mantle: ['rest', 'rest'], scroll: ['edge', 'rest'], child: ['rest', 'support'], bless: ['bless', 'support'] }[o.hold] || ['rest', 'rest'];+ hands.push(handFor(arms[0], 1, handsCfg[0]), handFor(arms[1], -1, handsCfg[1]));+ const handFs = hands.map((h) => { const f = handField(h.pose, h.thumb), inv = invRigid(h.m); return (x, y, z) => { const q = inv(x, y, z); return f(q[0], q[1], q[2]); }; });+ // The Child: a real little person on the Virgin's arm, turning to her+ let childF = null, childM = null;+ if (o.hold === 'child') {+ const A = arms[1];+ // sits in the crook of her left arm, turned towards her, one small hand raised to her veil+ childM = M.of(M.T(A.W[0] - 0.015, A.W[1] + 0.082, A.W[2] - 0.0), M.Ry(-34), M.Rz(4));+ const cH = headField({ hair: 'curls', seed: 21, smile: 0.6 }), inv = invRigid(childM), k = 0.47;+ childF = (x, y, z) => {+ const q = inv(x, y, z), X = q[0], Y = q[1], Z = q[2];+ // A seated child in a long shift: torso, thighs across her arm, shins and small feet hanging+ let d = sdCone(X, Y, Z, 0, 0, 0, 0, 0.165, 0, 0.066, 0.05);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.0, s * 0.034, 0.125, -0.012, s * 0.04, 0.038, 0.03), 0.018); // thigh+ d = smin(d, sdCone(X, Y, Z, 0.125, -0.012, s * 0.04, 0.142, -0.135, s * 0.038, 0.03, 0.022), 0.012); // shin+ d = smin(d, sdCone(X, Y, Z, 0.142, -0.15, s * 0.038, 0.19, -0.17, s * 0.04, 0.02, 0.013), 0.006); // foot+ }+ d -= 0.005 * Math.sqrt(Math.abs(Math.sin(Math.atan2(Z, X) * 6 + Y * 4))) * sstep(0.12, 0.0, Y) * sstep(-0.1, 0.0, Y);+ d = smin(d, sdCone(X, Y, Z, 0.005, 0.135, -0.052, 0.06, 0.07, -0.05, 0.02, 0.016), 0.012); // one arm holds an orb on his knee+ d = smin(d, len3(X - 0.1, Y - 0.052, Z + 0.042) - 0.03, 0.003);+ d = smin(d, len3(X - 0.074, Y - 0.056, Z + 0.054) - 0.015, 0.004);+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.145, 0.05, -0.005, 0.2, 0.094, 0.02, 0.015), 0.012); // the other reaches back to touch her veil+ d = smin(d, len3(X + 0.007, Y - 0.214, Z - 0.104) - 0.0155, 0.005);+ d = smin(d, cH((X - 0.008) / k, (Y - 0.262) / k, Z / k) * k, 0.012);+ return d;+ };+ }+ const allF = (x, y, z) => {+ let d = body.F(x, y, z);+ d = Math.min(d, headW(x, y, z));+ for (let i = 0; i < handFs.length; i++) d = Math.min(d, handFs[i](x, y, z));+ if (childF) d = Math.min(d, childF(x, y, z));+ return d;+ };+ // ---- carve ----+ await carveInto(stone, body.F, [-0.42, -0.005, -0.52, 0.42, yNeck + 0.04, 0.52], 0.0105, o.wings ? 3800 : 4400, null, allF);+ const head = await headBatch(o.headKey);+ for (let i = 0; i < hands.length; i++) {+ const hb = await handBatch(hands[i].pose, hands[i].thumb);+ const base = stone.count;+ copyBatch(stone, hb, hands[i].m);+ }+ if (childF) await carveInto(stone, childF, [arms[1].W[0] - 0.24, arms[1].W[1] - 0.2, arms[1].W[2] - 0.26, arms[1].W[0] + 0.3, arms[1].W[1] + 0.56, arms[1].W[2] + 0.26], 0.0042, 3200, null, allF, { smooth: 1, ao: 0.34, aoMin: 0.3 });+ addAttrs({ stone, gold }, o, arms);+ // Crown, nimbus and wings+ if (o.head.crown) addCrown(gold, M.of(headM, M.T(-0.012, o.head.veil ? 0.088 : 0.096, 0)), o.head.veil ? 0.088 : 0.084);+ if (o.nimbus) gold.at(M.of(headM, M.T(-0.16, 0.03, 0), M.Ry(90)), () => { gold.slab(ringPts(0, 0, 0.235, 28), [], -0.014, 0.014, { sb: 0.8 }); });+ if (o.wings) {+ // Feathers in overlapping tiers, carved on a blade that rises from the shoulders and falls to the calf+ const W = (side) => (x, y, z) => {+ // The wall side (local -z in every jamb) folds along the stepped stone from the shoulder; the free side stands off behind the back+ const zz = z * side - 0.12 - zc * side, X = side < 0 ? x + 0.01 + 0.6 * (zz - 0.065) : x + 0.085 + zz * 0.18 + 0.05 * clamp((1.86 - y) / 1.32), u = clamp((1.86 - y) / 1.32), wid = 0.27 * Math.pow(Math.sin(PI * Math.pow(u, 0.6)), 0.65) * (1 - 0.3 * u);+ const v = zz - 0.03 - u * 0.1 + 0.015;+ let d = Math.abs(X) - 0.026 + 0.012 * u;+ d = Math.max(d, Math.abs(v - wid * 0.5) - wid * 0.5, y - 1.87, 0.53 - y);+ // Tiers of feathers: short and scaled over the shoulder, long pointed quills towards the tip+ const row = Math.abs(Math.sin(u * 14 + v * 6)), bars = Math.abs(Math.sin(v * 26 + u * 7));+ d -= 0.015 * Math.sqrt(row) * sstep(0.45, 0.2, u) + 0.016 * Math.sqrt(bars) * sstep(0.2, 0.42, u);+ // The wing's arm: it grows out of the shoulder blade, so the blade never hangs in the air behind the body+ return smin(d * 0.8, sdCone(x, y, zz, -0.035, 1.6, -0.06, side < 0 ? -0.045 : -0.12, 1.46, 0.1, 0.05, 0.028), 0.04);+ };+ for (const side of [-1, 1]) await carveInto(stone, W(side), [-0.3, 0.5, side > 0 ? zc + 0.05 : zc - 0.72, 0.1, 1.9, side > 0 ? zc + 0.72 : zc - 0.05], 0.0105, 900, null, allF);+ }+ stone.isFigure = true;+ return { stone, gold, head, headM };+}+// ---- Seated and kneeling figures for the tympana: same people, other postures -------------+function poseField(kind) {+ if (kind === 'seated') return (x, y, z) => {+ // A figure enthroned: knees forward, the robe falling from them in a curtain to the feet+ const th = Math.atan2(z, x);+ let d = sdCone(x, y, z, -0.04, 0.72, 0, -0.03, 1.38, 0, 0.17, 0.13);+ d = smin(d, sdEll(x, y - 1.46, z, 0.09, 0.12, 0.17), 0.03);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(x, y, z, -0.03, 0.7, s * 0.1, 0.27, 0.68, s * 0.11, 0.1, 0.09), 0.03);+ const cur = sdCone(x, y, z, 0.27, 0.64, s * 0.11, 0.31, 0.04, s * 0.1, 0.09, 0.12);+ d = smin(d, cur - 0.012 * Math.sqrt(Math.abs(Math.sin(z * 34 + x * 4))), 0.03);+ }+ d = smin(d, sdCone(x, y, z, 0.16, 0.45, 0, 0.27, 0.04, 0, 0.11, 0.16) - 0.014 * Math.sqrt(Math.abs(Math.sin(z * 30 + 1))), 0.03);+ // the lap folds run across, the bodice is drawn up+ d -= 0.012 * Math.sqrt(Math.abs(Math.sin((y + 0.12 * z * z * 40) * 28))) * sstep(0.95, 0.7, y) * sstep(0.45, 0.62, y);+ d -= 0.009 * Math.sqrt(Math.abs(Math.sin(th * 7 + y * 2))) * sstep(0.8, 1.0, y);+ return Math.max(d, -y);+ };+ return (x, y, z) => {+ // Kneeling in prayer, turned to the centre: a body with a thigh and a folded shin under long cloth. The folds fall straight+ // down from the waist and break on the ground, never radiating round the body like the ribs of a pumpkin+ let d = sdCone(x, y, z, -0.1, 0.46, 0, 0.03, 1.04, 0, 0.15, 0.115);+ d = smin(d, sdEll(x - 0.05, y - 1.12, z, 0.08, 0.1, 0.15), 0.03);+ d = smin(d, sdCone(x, y, z, -0.1, 0.42, 0, 0.28, 0.15, 0, 0.16, 0.11), 0.05);+ d = smin(d, sdCone(x, y, z, 0.28, 0.12, 0, -0.26, 0.1, 0, 0.115, 0.09), 0.04);+ const fall = 0.62 * Math.sqrt(Math.abs(Math.sin(z * 31 + x * 3 + 1.1))) + 0.38 * Math.sqrt(Math.abs(Math.sin(z * 63 + y * 5 + 2.3)));+ d -= 0.017 * (fall - 0.55) * sstep(0.86, 0.42, y);+ d -= 0.006 * Math.sqrt(Math.abs(Math.sin(z * 38 + y * 2))) * sstep(0.6, 0.86, y);+ return Math.max(d, -y);+ };+}+async function makePosed(kind, spec) {+ const stone = new Batch(TILE.fine), gold = new Batch(TILE.gold), bF = poseField(kind), hO = Object.assign({ seed: 3 }, spec.head);+ const hy = kind === 'seated' ? 1.74 : 1.36, hx = kind === 'seated' ? -0.01 : 0.075;+ const headM = M.of(M.T(hx, hy, 0), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(hO.nod || -6));+ const allF = bF, head = await headBatch(spec.headKey);+ stone.isFigure = true;+ await carveInto(stone, bF, [-0.42, -0.005, -0.42, 0.56, kind === 'seated' ? 1.62 : 1.26, 0.42], 0.0145, 2600, null, allF);+ if (hO.crown) addCrown(gold, M.of(headM, M.T(-0.014, 0.1, 0)), hO.veil ? 0.106 : 0.098);+ // Forearms and hands: joined in prayer when kneeling, one raised when enthroned+ const hand = await handBatch(kind === 'seated' ? 'bless' : 'support', 1);+ if (kind === 'seated') {+ const A = spec.bless === false ? null : { W: [0.2, 1.32, 0.17] };+ const armF = (x, y, z) => { let d = sdCone(x, y, z, -0.02, 1.42, 0.17, 0.06, 1.02, 0.23, 0.066, 0.055); d = smin(d, sdCone(x, y, z, 0.06, 1.02, 0.23, 0.2, 1.3, 0.17, 0.055, 0.058), 0.02); d = Math.min(d, smin(sdCone(x, y, z, -0.02, 1.42, -0.17, 0.05, 1.02, -0.22, 0.066, 0.055), sdCone(x, y, z, 0.05, 1.02, -0.22, 0.27, 0.92, -0.08, 0.055, 0.058), 0.02)); return d; };+ await carveInto(stone, armF, [-0.12, 0.84, -0.34, 0.36, 1.52, 0.34], 0.0125, 700, null, allF);+ copyBatch(stone, hand, frameOf([0.2, 1.3, 0.17], [0.25, 1, 0.05], [0, 0.1, 1]));+ addBook(stone, M.of(M.T(0.36, 0.92, -0.08), M.Rz(-78)), { w: 0.11, h: 0.15 });+ } else {+ const armF = (x, y, z) => { let d = 1e9; for (const s of [-1, 1]) d = Math.min(d, smin(sdCone(x, y, z, 0.03, 1.03, s * 0.15, 0.1, 0.8, s * 0.17, 0.062, 0.05), sdCone(x, y, z, 0.1, 0.8, s * 0.17, 0.25, 0.97, s * 0.04, 0.05, 0.055), 0.02)); return d; };+ await carveInto(stone, armF, [-0.08, 0.68, -0.3, 0.34, 1.12, 0.3], 0.0125, 600, null, allF);+ for (const s of [-1, 1]) copyBatch(stone, hand, frameOf([0.25, 0.97, s * 0.04], [0.5, 1, -s * 0.05], [1, -0.4, s * 0.9]));+ }+ return { stone, gold, head, headM };+}++// Cache of finished figures. Lower-detail copies for the arches come from the same sculpture.+const FIG_CACHE = {};+async function lightCopy(src, tris) { // the same surface with fewer triangles, keeping its uvs and carved occlusion+ const g = await simplifyGeometry(src.toGeo(), { targetTriangles: tris });+ const T = src.tile, b = new Batch(T), p = g.attributes.position, n = g.attributes.normal, u = g.attributes.uv, c = g.attributes.color, idx = g.index;+ for (let i = 0; i < p.count; i++) b.v(p.getX(i), p.getY(i), p.getZ(i), n.getX(i), n.getY(i), n.getZ(i), u.getX(i) * T, u.getY(i) * T, c ? c.getX(i) : 1);+ for (let t = 0; t < idx.count; t += 3) b.tri(idx.getX(t), idx.getX(t + 1), idx.getX(t + 2));+ return b;+}+async function lowDetail(src, tris) {+ const b = await lightCopy(src.stone, tris);+ b.isFigure = true;+ // A figure high in the arches carries a light copy of its head inside its own mesh: fewer draws, fewer triangles+ if (!src.head.low) src.head.low = await lightCopy(src.head, 420);+ copyBatch(b, src.head.low, src.headM);+ return { stone: b, gold: new Batch(TILE.gold) };+}+async function prepareFigures(kinds) {+ for (const k of kinds) FIG_CACHE[k + '|1'] = await makeFigure(k);+ if (P.preview !== 'statues') {+ for (const k of ['apostle', 'king', 'angel', 'bishop', 'queen']) if (FIG_CACHE[k + '|1']) FIG_CACHE[k + '|0'] = await lowDetail(FIG_CACHE[k + '|1'], P.archFigureTris);+ for (let k = 0; k < 3; k++) GARGOYLES[k] = await lightCopy(makeGargoyle(k), 800);+ FIG_CACHE['seated|1'] = await makePosed('seated', { headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 9, nod: -2 } });+ FIG_CACHE['seatedVirgin|1'] = await makePosed('seated', { headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, turn: -14, tilt: 4 } });+ FIG_CACHE['kneel|1'] = await makePosed('kneel', { headKey: 'veiled', head: { veil: 1, seed: 8, tilt: 0, nod: 8 } });+ FIG_CACHE['kneel|0'] = await lowDetail(FIG_CACHE['kneel|1'], P.archFigureTris);+ }+}+function figure(name, lod = 1) {+ const k = FIG_ALIAS[name] || name;+ return FIG_CACHE[k + '|' + lod] || FIG_CACHE[k + '|1'];+}+// Body and head are separate nodes: lod 0 lightens the body only, a head costs its bytes once however often it appears+function placeFigure(name, kind, o = {}) {+ const f = figure(kind, o.lod === undefined ? 1 : o.lod);+ if (!f) return null;+ // o.relief squeezes the figure front to back, so it is carved from the wall behind it instead of standing before it+ const s = o.scale || 1, sc = [s * (o.relief || 1), s, o.mirror ? -s : s];+ const mat = o.indoor ? MAT.figureIn : o.deep ? MAT.figureDeep : o.half ? MAT.figureHalf : MAT.figure;+ const stone = place(name, f.stone, mat, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ place(name + '_Gilt', f.gold, MAT.gold, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ let headPart = null;+ if (f.head) {+ const m = f.headM, e = eulerFromBasis([m[0], m[4], m[8]], [m[1], m[5], m[9]], [m[2], m[6], m[10]]);+ headPart = place(name + '_Head', f.head, mat, { pos: [m[3], m[7], m[11]], rot: e, parent: stone || undefined });+ }+ if (stone && !o.indoor && !o.deep) FIG_PLACED.push({ part: stone, head: headPart });+ return stone;+}+const FIG_PLACED = [];++// ---- Foliage and crockets ----------------------------------------------------------+// One carved leaf curling at the tip; local +Y up the surface, +X outward from the host+function addLeaf(b, m, size = 1, o = {}) {+ b.at(m, () => {+ const nu = o.coarse ? 2 : 4, nv = o.coarse ? 3 : 4;+ b.grid((u, v) => {+ const s = (u - 0.5) * 2, W = 0.5 * Math.sin(PI * Math.pow(v, 0.72)) * (1 - 0.35 * v);+ const curl = Math.pow(v, 2.2);+ const lobes = 1 + 0.18 * Math.sin(v * 16) * Math.abs(s);+ const rib = 0.05 * (1 - Math.abs(s));+ return [size * (rib + 0.3 * curl - 0.42 * curl * curl * curl + 0.12 * s * s), size * (v - 0.3 * curl * v), size * (s * W * lobes), 0.72 + 0.28 * (1 - Math.abs(s)) + 0.1 * curl];+ }, nu, nv, { flip: true });+ });+}+// A crocket: a bud of curled foliage that sits on an arris. Local +X away from the arris, +Y up the slope.+function addCrocket(b, m, size = 1, lod = 1) {+ b.at(m, () => {+ const path = [];+ const N = lod ? 5 : 3;+ for (let i = 0; i <= N; i++) { const t = i / N, a = -0.9 + t * 3.8, r = lerp(0.3, 0.12, t) * size; path.push([size * 0.16 + r * Math.cos(a) * 0.95, size * 0.1 + r * Math.sin(a) * 0.9, 0]); }+ b.tube(path, (t) => [lerp(0.14, 0.05, t) * size, lerp(0.17, 0.07, t) * size], lod ? 5 : 3, { capStart: false, capEnd: false, ringS: (a) => 0.8 + 0.2 * Math.cos(a) });+ if (lod) for (const s of [-1, 1]) b.tube([[0.02 * size, 0.02 * size, 0], [0.2 * size, 0.1 * size, s * 0.22 * size], [0.34 * size, 0.26 * size, s * 0.3 * size]], (t) => [lerp(0.07, 0.015, t) * size, lerp(0.05, 0.02, t) * size], 3, { capEnd: false });+ });+}+function addFinial(b, h = 1, lod = 1) { // cross-shaped foliate finial, base at y=0+ b.lathe([[0.12 * h, 0], [0.1 * h, 0.05 * h], [0.14 * h, 0.09 * h], [0.08 * h, 0.14 * h], [0.05 * h, 0.5 * h], [0.09 * h, 0.56 * h], [0.05 * h, 0.62 * h], [0.02 * h, 0.98 * h], [0, h]], lod ? 8 : 5);+ for (let i = 0; i < 4; i++) {+ const a = i * 90 + 45;+ addLeaf(b, M.of(M.T(0, 0.14 * h, 0), M.Ry(a), M.T(0.05 * h, 0, 0)), 0.4 * h, { coarse: 1 });+ if (lod) addLeaf(b, M.of(M.T(0, 0.58 * h, 0), M.Ry(a + 45), M.T(0.03 * h, 0, 0)), 0.3 * h, { coarse: 1 });+ }+}+// Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame+function crocketsAlong(b, p, q, n, size, out, lod = 1, maker = addCrocket) {+ const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);+ const dn = d.map((c) => c / L);+ let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];+ const ol = Math.hypot(ox, oy, oz) || 1; ox /= ol; oy /= ol; oz /= ol;+ const zx = ox * 0, side = [dn[1] * oz - dn[2] * oy, dn[2] * ox - dn[0] * oz, dn[0] * oy - dn[1] * ox];+ // columns: X = out, Y = along edge, Z = side+ for (let i = 0; i < n; i++) {+ const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];+ const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];+ maker(b, m, size * lerp(1, 0.78, t), lod);+ }+}+// A crocket beaten in lead: one thick leaf turning up off the arris with a lobe either side. Same frame as addCrocket: X out, Y along the edge+function addLeadCrocket(b, m, s = 1) {+ b.at(m, () => {+ b.tube([[-0.04 * s, -0.02 * s, 0], [0.2 * s, 0.1 * s, 0], [0.34 * s, 0.3 * s, 0], [0.3 * s, 0.46 * s, 0]], (t) => [lerp(0.13, 0.015, t) * s, lerp(0.17, 0.02, t) * s], 4, { capEnd: false });+ for (const z of [-1, 1]) b.tube([[0, 0, z * 0.04 * s], [0.16 * s, 0.1 * s, z * 0.2 * s], [0.2 * s, 0.24 * s, z * 0.3 * s]], (t) => lerp(0.07, 0.012, t) * s, 3, { capEnd: false });+ });+}++// Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.+function addPinnacle(b, w, h, o = {}) {+ const hw = w / 2, shaftH = h * 0.42, gableH = w * 0.95, lod = o.lod === undefined ? 1 : o.lod;+ if (o.simple) { // far-away pinnacle: faceted shaft, spirelet, a few crockets, finial+ b.lathe([[hw * 1.414, 0], [hw * 1.414, shaftH], [hw * 1.6, shaftH + w * 0.06], [hw * 1.3, shaftH + w * 0.12], [hw * 0.1, h * 0.86]], 4, { faceted: true, phase: PI / 4 });+ for (let i = 0; i < 4; i++) { const a = rad(i * 90 + 45), c = Math.cos(a), s = Math.sin(a), bR = hw * 1.3 * 1.0; crocketsAlong(b, [c * bR, shaftH + w * 0.14, s * bR], [c * hw * 0.14, h * 0.84, s * hw * 0.14], 2, w * 0.46, [c, 0.15, s], 0); }+ b.at(M.T(0, h * 0.84, 0), () => addFinial(b, h * 0.16, 0));+ return;+ }+ // Shaft with moulded base and blind lancet panels+ b.lathe([[hw * 1.44, 0], [hw * 1.44, w * 0.06], [hw * 1.3, w * 0.12], [hw * 1.3, w * 0.2], [hw * 1.26, w * 0.24]], 4, { faceted: true, phase: PI / 4 });+ b.lathe([[hw * 1.414, w * 0.2], [hw * 1.414, shaftH]], 4, { faceted: true, phase: PI / 4, s: 0.95 });+ for (let i = 0; i < 4; i++) b.at(M.Ry(i * 90), () => {+ const pw = w * 0.62, y0 = w * 0.35, ys = shaftH - w * 0.5;+ b.at(M.T(hw + 0.004, 0, 0), () => b.at(M.Ry(90), () => { // panel recessed look: shaded inset lancet with moulded frame+ b.sweep(ofsXY(pointedArc(pw, pw * 0.85, 3), 0, ys), hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[-pw / 2, y0], [-pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[pw / 2, y0], [pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.face([[-pw / 2, y0, -0.002, 0.5], [pw / 2, y0, -0.002, 0.5], [pw / 2, ys, -0.002, 0.5], [0, ys + pw * 0.8, -0.002, 0.45], [-pw / 2, ys, -0.002, 0.5]]);+ }));+ // Gablet on each face with crockets+ b.at(M.T(hw * 0.96, shaftH, 0), () => {+ b.slabX = true;+ b.at(M.Ry(90), () => b.slab([[-hw * 1.04, 0], [hw * 1.04, 0], [0, gableH]], [], 0, w * 0.08, { sf: 1, sb: 0.8 }));+ crocketsAlong(b, [0.02, 0.02, -hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, -1], 0);+ crocketsAlong(b, [0.02, 0.02, hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, 1], 0);+ b.at(M.T(0.04, gableH * 0.94, 0), () => addFinial(b, w * 0.34, 0));+ });+ });+ // Cornice ring and spirelet+ b.at(M.T(0, shaftH, 0), () => {+ b.lathe([[hw * 1.414, 0], [hw * 1.6, w * 0.06], [hw * 1.6, w * 0.1], [hw * 1.414, w * 0.14]], 4, { faceted: true, phase: PI / 4 });+ const sh = h - shaftH - w * 0.1;+ b.at(M.T(0, w * 0.12, 0), () => {+ b.lathe([[hw * 1.3, 0], [hw * 0.09, sh * 0.82]], 8, { faceted: true, phase: PI / 8 });+ const n = lod ? 5 : 3;+ for (let i = 0; i < 4; i++) {+ const a = rad(i * 90 + 22.5 + 22.5 * 0);+ const c = Math.cos(a + rad(22.5)), s = Math.sin(a + rad(22.5));+ const bR = hw * 1.3 * Math.cos(rad(0)), tR = hw * 0.09;+ crocketsAlong(b, [c * bR, sh * 0.04, s * bR], [c * tR, sh * 0.8, s * tR], n, w * 0.36, [c, 0.12, s], lod);+ }+ b.at(M.T(0, sh * 0.8, 0), () => (o.finial || b) === b ? addFinial(b, sh * 0.22 + w * 0.1, lod) : null);+ });+ });+}++// ---- Gargoyle ------------------------------------------------------------------------+// A crouching beast thrusting out from the wall along +X, root at the origin. About 2.3 m long.+function makeGargoyle(kind = 0) {+ const b = new Batch(TILE.fine), rng = mkRng(kind * 17 + 5);+ const ridge = (kind === 1 ? 0.3 : 0.22);+ const headY = kind === 2 ? 0.1 : 0.34;+ const spine = spline([[-0.3, -0.05, 0], [0.22, 0.2, 0], [0.75, 0.3, 0], [1.2, 0.2, 0], [1.56, headY, 0]], 5);+ b.tube(spine, (t) => { const r = keyf([[0, 0.34, 0.3], [0.25, 0.36, 0.34], [0.55, 0.22, 0.24], [0.82, 0.16, 0.17], [1, 0.19, 0.2]], t); return [r[0], r[1]]; }, 16, {+ capStart: true,+ up: [0, 1, 0],+ ring: (a, t) => 1 + ridge * Math.pow(Math.max(Math.cos(a), 0), 10) * Math.abs(Math.sin(t * 26)) + 0.05 * Math.abs(Math.sin(a * 5 + t * 11)),+ ringS: (a, t) => 0.74 + 0.26 * Math.abs(Math.sin(a * 5 + t * 11)),+ });+ // Skull, snout, gaping jaws+ const hx = 1.56, hy = headY;+ blob(b, [hx + 0.05, hy + 0.03, 0], [0.22, 0.19, 0.2], 14, 10, 0.92);+ b.tube(spline([[hx + 0.1, hy + 0.06, 0], [hx + 0.34, hy + 0.05, 0], [hx + 0.56, hy + 0.1, 0]], 3), (t) => [lerp(0.14, 0.07, t), lerp(0.15, 0.085, t)], 10, { capStart: true, up: [0, 1, 0], ring: (a, t) => 1 + 0.2 * Math.pow(Math.max(Math.cos(a), 0), 6) * Math.abs(Math.sin(t * 9)) });+ b.shade(0.9, () => b.tube(spline([[hx + 0.05, hy - 0.1, 0], [hx + 0.28, hy - 0.2, 0], [hx + 0.5, hy - 0.2, 0]], 3), (t) => [lerp(0.09, 0.045, t), lerp(0.125, 0.07, t)], 10, { capStart: true, up: [0, 1, 0] }));+ b.shade(0.34, () => blob(b, [hx + 0.2, hy - 0.06, 0], [0.16, 0.085, 0.095], 8, 5));+ for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {+ const f = i / 3.5;+ b.tube([[hx + 0.26 + f * 0.27, hy + 0.0 + f * 0.075, s * lerp(0.1, 0.06, f)], [hx + 0.27 + f * 0.27, hy - 0.08 + f * 0.075, s * lerp(0.1, 0.06, f)]], (t) => lerp(0.018, 0.002, t), 4, { capStart: true });+ b.tube([[hx + 0.22 + f * 0.27, hy - 0.15, s * lerp(0.1, 0.05, f)], [hx + 0.22 + f * 0.27, hy - 0.08, s * lerp(0.1, 0.05, f)]], (t) => lerp(0.016, 0.002, t), 4, { capStart: true });+ }+ // Brow, eyes, ears or horns+ for (const s of [-1, 1]) {+ b.tube(spline([[hx + 0.02, hy + 0.2, s * 0.03], [hx + 0.17, hy + 0.17, s * 0.1], [hx + 0.24, hy + 0.12, s * 0.15]], 2), (t) => lerp(0.03, 0.02, t), 5, { capStart: true });+ b.shade(0.38, () => blob(b, [hx + 0.21, hy + 0.09, s * 0.11], [0.05, 0.04, 0.045], 6, 4));+ blob(b, [hx + 0.235, hy + 0.09, s * 0.115], [0.024, 0.022, 0.022], 6, 4);+ if (kind === 1) b.tube(spline([[hx - 0.05, hy + 0.14, s * 0.11], [hx - 0.1, hy + 0.3, s * 0.18], [hx - 0.25, hy + 0.42, s * 0.2], [hx - 0.42, hy + 0.42, s * 0.15]], 3), (t) => lerp(0.05, 0.005, t), 6, { capStart: true });+ else b.grid((u, v) => { const L = 0.3, W = 0.09 * Math.sin(PI * u); return [hx - 0.08 - u * L * 0.5, hy + 0.13 + u * L * 0.7, s * (0.13 + u * 0.09 + (v - 0.5) * W * 2), 0.7 + 0.3 * Math.abs(v - 0.5) * 2]; }, 4, 2, { flip: s > 0 });+ if (kind === 1) { /* also add jowls */ blob(b, [hx + 0.1, hy - 0.04, s * 0.13], [0.09, 0.06, 0.05], 6, 4, 0.9); }+ // Fore leg gripping the ledge+ const sh = [0.5, 0.12, s * 0.3], el = [0.68, -0.28, s * 0.42], wr = [0.98, -0.42, s * 0.3];+ b.tube(spline([sh, el, wr], 4), (t) => keyf([[0, 0.16], [0.4, 0.105], [1, 0.07]], t)[0], 10, { capStart: true, ring: (a, t) => 1 + 0.08 * Math.abs(Math.sin(a * 3 + t * 8)) });+ blob(b, wr, [0.1, 0.06, 0.09], 8, 5);+ for (let i = -1; i <= 1; i++) b.tube(spline([[wr[0] + 0.03, wr[1], wr[2] + i * 0.05], [wr[0] + 0.14, wr[1] - 0.02, wr[2] + i * 0.08], [wr[0] + 0.21, wr[1] - 0.11, wr[2] + i * 0.09]], 2), (t) => lerp(0.032, 0.004, t), 5);+ // Folded wing+ const span = kind === 0 ? 0.95 : 0.7;+ const wingFn = (off) => (u, v) => {+ const L = u * span, W = 0.44 * Math.sin(PI * Math.pow(u, 0.7));+ const memb = Math.abs(Math.sin(v * 7.5 + u * 1.2)) * 0.05 * u;+ return [0.42 - L + (off), 0.3 + 0.48 * Math.sin(u * 1.25) * (1 - v * 0.9) - v * W * 0.75 + memb, s * (0.27 + 0.2 * u + v * W * 0.5), 0.68 + 0.32 * (memb / 0.05)];+ };+ b.grid(wingFn(0), 7, 5, { flip: s < 0 });+ b.grid(wingFn(-0.02), 7, 5, { flip: s > 0 });+ }+ return b;+}+const GARGOYLES = [];+function gargoyle(kind) { if (!GARGOYLES[kind]) GARGOYLES[kind] = makeGargoyle(kind); return GARGOYLES[kind]; }++// ==================================================================================+// STAINED GLASS: a tiny vector-art engine producing real leaded panes.+// Every shape is a pane of pot-metal colour with a came (lead strip) around it; large+// figures get painted grisaille lines. Windows are geometry, so they stay crisp at any distance.+// ==================================================================================+function offsetPoly(pts, d) {+ const n = pts.length, s = area2(pts) >= 0 ? 1 : -1, out = [];+ for (let i = 0; i < n; i++) {+ const a = pts[(i - 1 + n) % n], b = pts[i], c = pts[(i + 1) % n];+ let ax = b[0] - a[0], ay = b[1] - a[1], cx = c[0] - b[0], cy = c[1] - b[1];+ const la = Math.hypot(ax, ay) || 1, lc = Math.hypot(cx, cy) || 1; ax /= la; ay /= la; cx /= lc; cy /= lc;+ let nx = (ay + cy) * s, ny = -(ax + cx) * s; const l = Math.hypot(nx, ny) || 1; nx /= l; ny /= l;+ const k = clamp(1 / Math.max(0.35, Math.abs(nx * ay * s - ny * ax * s)), 1, 2.4);+ out.push([b[0] + nx * d * k, b[1] + ny * d * k]);+ }+ return out;+}+function clipConvex(poly, clip) {+ let out = poly; const sgn = area2(clip) >= 0 ? 1 : -1;+ for (let i = 0; i < clip.length && out.length; i++) {+ const a = clip[i], b = clip[(i + 1) % clip.length], inp = out; out = [];+ const side = (p) => sgn * ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]));+ for (let j = 0; j < inp.length; j++) {+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);+ if (sp >= 0) out.push(p);+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([lerp(p[0], q[0], t), lerp(p[1], q[1], t)]); }+ }+ }+ return out;+}+function isConvex(p) { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = p[(i + 2) % p.length]; const s = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); if (Math.abs(s) < 1e-9) continue; if (!sign) sign = Math.sign(s); else if (Math.sign(s) !== sign) return false; } return true; }++class Glass {+ constructor(seed = 1) { this.b = new Batch(1); this.b.noColor = true; this.r = mkRng(seed * 13 + 3); this.clip = null; this.xf = [1, 0, 0, 1, 0, 0]; this.came = 0.022; }+ xform(a, b, c, d, e, f) { const m = this.xf; return [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]]; }+ withXf(rotDeg, tx, ty, s, fn) { const o = this.xf, c = Math.cos(rad(rotDeg)) * s, sn = Math.sin(rad(rotDeg)) * s; this.xf = this.xform(c, sn, -sn, c, tx, ty); fn(this); this.xf = o; }+ withClip(pts, fn) { const o = this.clip; const m = this.xf; this.clip = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]); fn(this); this.clip = o; }+ toLocal(p) { const m = this.xf, det = m[0] * m[3] - m[1] * m[2], x = p[0] - m[4], y = p[1] - m[5]; return [(m[3] * x - m[2] * y) / det, (-m[1] * x + m[0] * y) / det]; }+ // Writes one flat polygon. map(worldPoint) -> [u,v] in atlas pixels; otherwise a window on the sheet of its colour+ raw(pts, color, layer, jitter = 1, map = null) {+ if (layer === -0.4 && !this.b.outline) this.b.outline = pts; // the first polygon of a window is its whole outline+ const name = GLASS_ALIAS[color] || color, fam = GL.fam[name] || GL.fam.lead, z = 0.002 + layer * 0.0035, C = GL.small;+ if (this.b.panes) this.b.panes.push([pts.map((p) => [p[0], p[1]]), name, layer]); // the cartoon, for the light this window throws+ // Every pane is cut from a different part of the sheet, so no two panes match;+ // 200 pixels of sheet to the metre, so the streaks and clouds of the pot-metal show in the pane+ const ox = fam[0] + 44 + this.r() * (C - 88), oy = fam[1] + 44 + this.r() * (C - 88), rot = this.r() * 6.28, cr = Math.cos(rot) * 200, sr = Math.sin(rot) * 200;+ let mx = 0, my = 0; for (const p of pts) { mx += p[0]; my += p[1]; } mx /= pts.length; my /= pts.length;+ const ids = pts.map((p) => {+ let u, v;+ if (map) { const q = map(p); u = q[0]; v = q[1]; } else {+ const dx = clamp(p[0] - mx, -0.2, 0.2), dy = clamp(p[1] - my, -0.2, 0.2);+ u = ox + (dx * cr - dy * sr) * jitter; v = oy + (dx * sr + dy * cr) * jitter;+ }+ return this.b.v(p[0], p[1], z, 0, 0, 1, u / GL.w, v / GL.h, 1);+ });+ if (isConvex(pts)) { const flip = area2(pts) < 0; for (let i = 1; i < ids.length - 1; i++) { if (flip) this.b.tri(ids[0], ids[i + 1], ids[i]); else this.b.tri(ids[0], ids[i], ids[i + 1]); } } else { const t = triangulate(pts); for (const k of t) this.b.tri(ids[k[0]], ids[k[1]], ids[k[2]]); }+ }+ // a pane: colour polygon + lead came. o.tile paints drapery across it, o.orn ornament.+ poly(pts, color, layer = 1, o = {}) {+ const m = this.xf; let P2 = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]);+ if (area2(P2) < 0) P2 = P2.slice().reverse();+ const came = o.came === undefined ? this.came : o.came;+ if (came > 0) {+ let C = offsetPoly(P2, came);+ if (this.clip) C = clipConvex(C, this.clip);+ if (C.length >= 3 && Math.abs(area2(C)) > 1e-5) this.raw(C, 'lead', layer - 0.5, 0);+ } else if (came < 0) P2 = offsetPoly(P2, came); // over a lead backing: inset instead of a second polygon+ if (this.clip) P2 = clipConvex(P2, this.clip);+ if (P2.length < 3 || Math.abs(area2(P2)) <= 1e-5) return this;+ let map = o.map || null;+ if (o.tile && GL.tile[o.tile]) { // planar mapping of the painted tile over the local box+ const T = GL.tile[o.tile], B = o.box, f = o.flipU ? -1 : 1;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + (f > 0 ? a : 1 - a) * (T[2] - 4), T[1] + 2 + (1 - c) * (T[3] - 4)]; };+ } else if (o.orn && GL.orn[o.orn]) {+ const T = GL.orn[o.orn], B = o.box;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + a * (GL.small - 4), T[1] + 2 + (1 - c) * (GL.small - 4)]; };+ }+ this.raw(P2, color, layer, o.jitter === undefined ? 1 : o.jitter, map);+ return this;+ }+ disc(cx, cy, r, color, layer = 1, n = 12, o) { return this.poly(ringPts(cx, cy, r, n), color, layer, o); }+ // a head drawn by the glass painter: an oval pane mapped onto a face from the atlas+ face(cx, cy, rx, ry, kind, layer, flip) {+ const F = GL.face[kind] || GL.face.youth, C = GL.cell, pts = []; for (let i = 0; i < 12; i++) { const a = i * TAU / 12; pts.push([cx + rx * Math.cos(a), cy + ry * Math.sin(a)]); }+ return this.poly(pts, 'flesh', layer, { came: 0.012, map: (p) => { const q = this.toLocal(p), a = ((q[0] - cx) / rx) * (flip ? -1 : 1), c = (q[1] - cy) / ry; return [F[0] + C / 2 + clamp(a, -1, 1) * (C / 2 - 3), F[1] + C / 2 - clamp(c, -1, 1) * (C / 2 - 3)]; } });+ }+ // painted line (grisaille): a thin dark ribbon along a polyline+ line(pts, w = 0.02, color = 'lead', layer = 5) {+ for (let i = 0; i < pts.length - 1; i++) {+ const a = pts[i], b = pts[i + 1]; let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx *= w / 2 / l; dy *= w / 2 / l;+ this.poly([[a[0] - dy, a[1] + dx], [a[0] + dy, a[1] - dx], [b[0] + dy, b[1] - dx], [b[0] - dy, b[1] + dx]], color, layer, { came: 0, jitter: 0 });+ }+ return this;+ }+}++// Diapered mosaic background: lozenges of two deep colours, a jewel at each crossing+// (one painted square of leaded lozenges laid down repeatedly: fine as real mosaic, cheap as two triangles a square)+function gDiaper(g, x0, y0, x1, y1, cell, c1, c2, c3, layer = 0) {+ const T = c1 === 'outer' ? 1.5 : 0.72, G = GL.ground[GROUND_OF[c1] || 'rc'], C = GL.cell;+ for (let y = Math.floor(y0 / T) * T; y < y1; y += T) for (let x = Math.floor(x0 / T) * T; x < x1; x += T) {+ const qx = x, qy = y, map = (p) => [G[0] + 2 + clamp((p[0] - qx) / T) * (C - 4), G[1] + 2 + clamp((p[1] - qy) / T) * (C - 4)];+ g.poly([[x, y], [x + T, y], [x + T, y + T], [x, y + T]], c1, layer, { came: 0, map });+ }+}++// A figure in glass: long Gothic proportions, robes painted in brown, the head drawn by hand. (x,y) = feet.+function gFigure(g, x, y, h, o = {}) {+ const sx = o.sx || 0.82, hTot = h; h = hTot * sx;+ const f = o.flip ? -1 : 1, L = (u, v) => [x + f * u * h, y + (v <= 0.76 ? v / 0.76 * (hTot - 0.24 * h) : hTot - (1 - v) * h)], PS = (a) => a.map((p) => L(p[0], p[1]));+ const robe = o.robe || 'white', mantle = o.mantle || 'cobalt', skin = o.skin || 'flesh', big = hTot > 2.2, layer = o.layer === undefined ? 2 : o.layer;+ const sit = o.pose === 'sit', kneel = o.pose === 'kneel';+ const hy = kneel ? 0.67 : sit ? 0.86 : 0.875, hr = 0.066, hx = kneel ? 0.04 : 0;+ // Box in world units for tile mapping, in the local frame of the Glass object+ const bx = [x - h * 0.34, y, x + h * 0.34, y + hTot * 0.86], tR = DRAPE_OF[robe] || 'white', tM = DRAPE_OF[mantle] || 'cobalt';+ const pane = (pts, col, lay, tile, opt) => g.poly(PS(pts), col, lay, Object.assign({ tile, box: bx, flipU: !!o.flip }, opt || {}));+ const brk = (pts, c1, lay, tile) => { // a robe is many pieces of glass: cut it across with lead+ const P3 = PS(pts); let y0 = 1e9, y1 = -1e9; for (const q of P3) { y0 = Math.min(y0, q[1]); y1 = Math.max(y1, q[1]); }+ const n = big ? 4 : (hTot > 0.9 ? 2 : 1);+ for (let i = 0; i < n; i++) {+ const a = lerp(y0, y1, i / n), c = lerp(y0, y1, (i + 1) / n), sl = big ? 0.12 * h * (i % 2 ? 1 : -1) * 0.5 : 0;+ const piece = clipConvex(clipConvex(P3, [[-1e4, a - sl * 0], [1e4, a + sl], [1e4, 1e4], [-1e4, 1e4]]), [[-1e4, -1e4], [1e4, -1e4], [1e4, c + sl], [-1e4, c - sl * 0]]);+ if (piece.length >= 3) g.poly(piece, c1, lay, { tile, box: bx, flipU: !!o.flip, came: 0.016 });+ }+ };+ if (o.wings) {+ pane([[-0.08, hy - 0.1], [-0.3, hy + 0.1], [-0.4, hy - 0.08], [-0.34, 0.4], [-0.24, 0.2], [-0.1, 0.42]], o.wings, layer - 0.2, null);+ pane([[0.08, hy - 0.1], [0.3, hy + 0.1], [0.4, hy - 0.08], [0.34, 0.4], [0.24, 0.2], [0.1, 0.42]], o.wings, layer - 0.2, null);+ if (hTot > 1) for (const s of [-1, 1]) { g.line(PS([[s * 0.1, hy - 0.08], [s * 0.3, 0.52]]), h * 0.01, 'lead', layer); g.line(PS([[s * 0.2, hy - 0.02], [s * 0.34, 0.42]]), h * 0.008, 'lead', layer); }+ }+ if (o.halo !== false) {+ const c = L(hx, hy);+ g.disc(c[0], c[1], h * 0.116, o.halo || 'gold', layer - 0.1, big ? 18 : 12);+ if (o.cross) { for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[c[0] + h * 0.116 * Math.cos(a - 0.26), c[1] + h * 0.116 * Math.sin(a - 0.26)], [c[0] + h * 0.116 * Math.cos(a + 0.26), c[1] + h * 0.116 * Math.sin(a + 0.26)], c], 'ruby', layer, { came: 0.008 }); } }+ }+ // ---- body ----+ if (sit) {+ brk([[-0.12, 0.72], [0.12, 0.72], [0.19, 0.44], [0.26, 0.4], [0.27, 0.08], [0.22, 0.0], [-0.22, 0.0], [-0.2, 0.3], [-0.14, 0.44]], robe, layer, tR);+ brk([[-0.13, 0.74], [0.05, 0.74], [0.17, 0.46], [0.24, 0.4], [0.2, 0.18], [-0.06, 0.1], [-0.2, 0.3], [-0.14, 0.46]], mantle, layer + 0.5, tM);+ } else if (kneel) {+ brk([[-0.08, 0.56], [0.13, 0.56], [0.16, 0.26], [0.26, 0.1], [0.26, 0], [-0.26, 0], [-0.2, 0.12]], robe, layer, tR);+ brk([[-0.09, 0.57], [0.08, 0.57], [0.13, 0.28], [0.1, 0.1], [-0.2, 0.05], [-0.16, 0.24]], mantle, layer + 0.5, tM);+ } else {+ const c = o.sway === undefined ? 0.02 : o.sway;+ brk([[-0.1 + c, 0.75], [0.1 + c, 0.75], [0.115, 0.46], [0.14, 0.2], [0.2, 0.03], [0.15, 0.0], [0.05, 0.022], [-0.05, 0.0], [-0.16, 0.012], [-0.2, 0.04], [-0.14, 0.22], [-0.115, 0.46]], robe, layer, tR);+ brk([[-0.11 + c, 0.76], [0.03 + c, 0.76], [0.12, 0.5], [0.135, 0.3], [0.09, 0.15], [-0.06, 0.11], [-0.165, 0.22], [-0.125, 0.46]], mantle, layer + 0.5, tM);+ pane([[-0.09, 0.03], [-0.02, 0.03], [-0.02, -0.012], [-0.115, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ pane([[0.02, 0.03], [0.09, 0.03], [0.115, -0.012], [0.02, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ }+ // ---- arms and hands ----+ const by = kneel ? 0.5 : 0.68, armPose = o.arm || 'hold';+ const ribbon = (a, b, w, col, tile) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy) || 1, nx = -dy / l * w / 2, ny = dx / l * w / 2; g.poly(PS([[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]]), col, layer + 1, { came: 0.012, tile, box: bx }); };+ const hand = (p) => { const c = L(p[0], p[1]); g.poly([[c[0] - h * 0.026, c[1] - h * 0.018], [c[0] + h * 0.018, c[1] - h * 0.022], [c[0] + h * 0.03, c[1] + h * 0.016], [c[0] + h * 0.006, c[1] + h * 0.03], [c[0] - h * 0.02, c[1] + h * 0.018]], skin, layer + 1.3, { came: 0.008 }); };+ const arms = { hold: [[0.13, by - 0.12], [0.03, by - 0.18], [-0.13, by - 0.12], [-0.03, by - 0.2]], up: [[0.17, by - 0.03], [0.27, by + 0.18], [-0.12, by - 0.12], [-0.03, by - 0.22]], out: [[0.19, by - 0.08], [0.32, by - 0.06], [-0.19, by - 0.08], [-0.32, by - 0.06]], pray: [[0.1, by - 0.14], [0.12, by - 0.03], [-0.04, by - 0.14], [0.1, by - 0.03]], point: [[0.17, by - 0.08], [0.33, by + 0.04], [-0.12, by - 0.12], [-0.04, by - 0.2]], orant: [[0.2, by + 0.0], [0.27, by + 0.14], [-0.2, by + 0.0], [-0.27, by + 0.14]] };+ const A = arms[armPose] || arms.hold;+ ribbon([0.095, by], A[0], 0.07, mantle, tM); ribbon(A[0], A[1], 0.056, robe, tR); hand(A[1]);+ ribbon([-0.095, by], A[2], 0.07, mantle, tM); ribbon(A[2], A[3], 0.056, robe, tR); hand(A[3]);+ // ---- attributes ----+ const at = o.attr;+ if (at === 'book') { g.poly(PS([[A[1][0] - 0.1, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] + 0.12], [A[1][0] - 0.1, A[1][1] + 0.12]]), 'ruby', layer + 1.5, { came: 0.01 }); g.poly(PS([[A[1][0] - 0.07, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.09], [A[1][0] - 0.07, A[1][1] + 0.09]]), 'gold', layer + 1.6, { came: 0 }); }+ else if (at === 'staff' || at === 'cross') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, 0], [sxx + 0.012, 0], [sxx + 0.012, 1.08], [sxx - 0.012, 1.08]]), 'gold', layer + 0.9, { came: 0.008 }); if (at === 'cross') g.poly(PS([[sxx - 0.07, 0.95], [sxx + 0.07, 0.95], [sxx + 0.07, 0.98], [sxx - 0.07, 0.98]]), 'gold', layer + 0.9, { came: 0.008 }); else g.poly(ringPts(sxx + 0.03, 1.1, 0.045, 8).map((p) => L(p[0], p[1])), 'gold', layer + 0.9, { came: 0.008 }); }+ else if (at === 'sword') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, by - 0.08], [sxx + 0.012, by - 0.08], [sxx, 1.02]]), 'white', layer + 0.9, { came: 0.008 }); g.poly(PS([[sxx - 0.05, by - 0.08], [sxx + 0.05, by - 0.08], [sxx + 0.05, by - 0.05], [sxx - 0.05, by - 0.05]]), 'gold', layer + 1, { came: 0.008 }); }+ else if (at === 'key') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.01, by - 0.14], [sxx + 0.01, by - 0.14], [sxx + 0.01, by + 0.2], [sxx - 0.01, by + 0.2]]), 'gold', layer + 1, { came: 0.006 }); g.poly(ringPts(sxx, by + 0.23, 0.035, 6).map((p) => L(p[0], p[1])), 'gold', layer + 1, { came: 0.006 }); g.poly(PS([[sxx, by - 0.14], [sxx + 0.05, by - 0.14], [sxx + 0.05, by - 0.1], [sxx, by - 0.1]]), 'gold', layer + 1, { came: 0.006 }); }+ else if (at === 'lily') { const sxx = A[1][0]; g.line(PS([[sxx, by - 0.22], [sxx + 0.01, by + 0.2]]), h * 0.012, 'green', layer + 1); for (let i = 0; i < 3; i++) { const yy = by + 0.08 + i * 0.07; const q = L(sxx + 0.02 * (i % 2 ? 1 : -1), yy); g.disc(q[0], q[1], h * 0.024, 'white', layer + 1.1, 5, { came: 0.006 }); } }+ // ---- head: drawn, not a disc ----+ const hc = L(hx, hy);+ const kind = o.face || (o.wings ? 'angel' : o.veil ? 'virgin' : o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ const nkA = L(hx - 0.02, hy - 0.11), nkB = L(hx + 0.02, hy - 0.11), nkC = L(hx + 0.02, hy - 0.04), nkD = L(hx - 0.02, hy - 0.04);+ g.poly([nkA, nkB, nkC, nkD], skin, layer + 1.5, { came: 0.008 });+ g.face(hc[0], hc[1], h * hr * 1.02, h * hr * 1.24, kind, layer + 1.8, !!o.flip);+ if (o.crown) g.poly(PS([[hx - 0.066, hy + 0.05], [hx + 0.066, hy + 0.05], [hx + 0.08, hy + 0.13], [hx + 0.04, hy + 0.095], [hx, hy + 0.14], [hx - 0.04, hy + 0.095], [hx - 0.08, hy + 0.13]]), 'gold', layer + 2.2, { came: 0.01 });+ if (o.mitre) g.poly(PS([[hx - 0.056, hy + 0.05], [hx + 0.056, hy + 0.05], [hx + 0.048, hy + 0.15], [hx, hy + 0.215], [hx - 0.048, hy + 0.15]]), o.mitre, layer + 2.2, { came: 0.01 });+}+// ---- Narrative scenes, composed inside a circle of radius r at the origin ---------+const SCENES = {+ annunciation(g, r) { // Angel greets the Virgin; lily between them, dove above+ gFigure(g, -r * 0.46, -r * 0.72, r * 1.34, { robe: 'white', mantle: 'emerald', wings: 'rubyLt', arm: 'point', attr: null, hair: 'amber' });+ gFigure(g, r * 0.46, -r * 0.72, r * 1.3, { flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray', halo: 'gold' });+ g.poly([[-0.1, -r * 0.72], [0.1, -r * 0.72], [0.06, -r * 0.5], [-0.06, -r * 0.5]], 'gold', 3, { came: 0.012 });+ g.line([[0, -r * 0.5], [0, r * 0.05]], 0.025, 'green', 3.2);+ [0.05, -0.1, -0.24].forEach((v, i) => g.disc((i % 2 ? 1 : -1) * r * 0.05, r * v, r * 0.05, 'white', 3.3, 5, { came: 0.008 }));+ g.disc(0, r * 0.62, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.14, r * 0.66], [0, r * 0.6], [r * 0.14, r * 0.66], [0, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ nativity(g, r) { // Virgin, Child in the crib, Joseph, the star, the ox and ass+ g.poly([[-r * 0.8, -r * 0.9], [r * 0.8, -r * 0.9], [r * 0.8, -r * 0.62], [-r * 0.8, -r * 0.62]], 'brown', 1.5);+ gFigure(g, -r * 0.42, -r * 0.62, r * 1.05, { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'out' });+ gFigure(g, r * 0.56, -r * 0.62, r * 1.1, { robe: 'tan', mantle: 'emerald', beard: 1, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'staff', flip: 1, halo: 'yellow' });+ g.poly([[-r * 0.1, -r * 0.62], [r * 0.3, -r * 0.62], [r * 0.34, -r * 0.34], [-r * 0.14, -r * 0.34]], 'amber', 2, { came: 0.014 });+ g.poly([[-r * 0.07, -r * 0.34], [r * 0.27, -r * 0.34], [r * 0.27, -r * 0.22], [-r * 0.07, -r * 0.22]], 'white', 2.5, { came: 0.012 });+ g.disc(r * 0.02, -r * 0.24, r * 0.065, 'gold', 2.5, 8, { came: 0.008 }); g.disc(r * 0.02, -r * 0.24, r * 0.043, 'flesh', 3, 7, { came: 0.008 });+ star(g, 0, r * 0.68, r * 0.13, 'yellow', 3);+ g.poly([[-r * 0.4, r * 0.22], [0, r * 0.52], [r * 0.4, r * 0.22], [r * 0.36, r * 0.14], [0, r * 0.4], [-r * 0.36, r * 0.14]], 'bone', 1.5);+ },+ magi(g, r) { // Three crowned kings bring gifts to the enthroned Virgin+ gFigure(g, r * 0.56, -r * 0.8, r * 1.2, { pose: 'sit', flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'hold' });+ g.disc(r * 0.4, -r * 0.1, r * 0.06, 'gold', 4, 8, { came: 0.008 });+ gFigure(g, -r * 0.05, -r * 0.8, r * 1.18, { pose: 'kneel', robe: 'emerald', mantle: 'ruby', crown: 1, beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'point', halo: false });+ gFigure(g, -r * 0.42, -r * 0.8, r * 1.32, { robe: 'violet', mantle: 'gold', crown: 1, beard: 1, arm: 'point', halo: false });+ gFigure(g, -r * 0.74, -r * 0.8, r * 1.3, { robe: 'teal', mantle: 'rubyLt', crown: 1, arm: 'hold', halo: false });+ star(g, -r * 0.1, r * 0.72, r * 0.11, 'yellow', 3);+ },+ crucifixion(g, r) { // Christ on the cross, the Virgin and Saint John, sun and moon+ g.poly([[-0.09 * r, -r * 0.92], [0.09 * r, -r * 0.92], [0.09 * r, r * 0.76], [-0.09 * r, r * 0.76]], 'emerald', 1.5, { came: 0.016 });+ g.poly([[-r * 0.58, r * 0.28], [r * 0.58, r * 0.28], [r * 0.58, r * 0.45], [-r * 0.58, r * 0.45]], 'emerald', 1.5, { came: 0.016 });+ gFigure(g, 0, -r * 0.84, r * 1.42, { robe: 'white', mantle: 'white', cross: 1, arm: 'out', halo: 'gold', hair: 'hair', beard: 1, sway: 0 });+ gFigure(g, -r * 0.62, -r * 0.88, r * 1.1, { robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray' });+ gFigure(g, r * 0.62, -r * 0.88, r * 1.1, { flip: 1, robe: 'white', mantle: 'rubyLt', arm: 'hold', attr: 'book' });+ g.disc(-r * 0.68, r * 0.66, r * 0.11, 'gold', 2, 8); g.poly([[r * 0.59, r * 0.74], [r * 0.76, r * 0.6], [r * 0.71, r * 0.76]], 'pearl', 2);+ },+ resurrection(g, r) { // Christ rises from the tomb holding the banner; soldiers sleep below+ g.poly([[-r * 0.72, -r * 0.84], [r * 0.72, -r * 0.84], [r * 0.72, -r * 0.42], [-r * 0.72, -r * 0.42]], 'bone', 2);+ g.line([[-r * 0.72, -r * 0.56], [r * 0.72, -r * 0.56]], 0.02, 'lead', 3); [-0.4, 0, 0.4].forEach((k) => g.disc(r * k, -r * 0.68, r * 0.09, 'purple', 3, 8, { came: 0.01 }));+ gFigure(g, 0, -r * 0.42, r * 1.25, { robe: 'white', mantle: 'rubyLt', cross: 1, arm: 'up', hair: 'hair', beard: 1, halo: 'gold' });+ g.poly([[r * 0.34, r * 0.72], [r * 0.34, r * 0.92], [r * 0.7, r * 0.84], [r * 0.34, r * 0.72]], 'white', 3, { came: 0.01 }); g.line([[r * 0.34, r * 0.92], [r * 0.34, -r * 0.1]], 0.02, 'gold', 3.2);+ for (const s of [-1, 1]) { g.disc(s * r * 0.82, -r * 0.5, r * 0.07, 'flesh', 3, 7, { came: 0.01 }); g.poly([[s * r * 0.6, -r * 0.86], [s * r * 0.92, -r * 0.86], [s * r * 0.9, -r * 0.54], [s * r * 0.7, -r * 0.54]], s > 0 ? 'emerald' : 'cobalt', 2.6); }+ },+ supper(g, r) { // The Last Supper: Christ at the centre of a laid table+ [-0.62, -0.31, 0.31, 0.62].forEach((k, i) => gBust(g, r * k, -r * 0.05, r * 0.92, { robe: ['emerald', 'ruby', 'amber', 'violet'][i], beard: i % 2 ? 1 : 0, hair: i === 3 ? 'grey' : 'hair' }));+ gBust(g, 0, -r * 0.02, r * 1.04, { robe: 'rubyLt', beard: 1, cross: 1 });+ g.poly([[-r * 0.92, -r * 0.5], [r * 0.92, -r * 0.5], [r * 0.92, -r * 0.2], [-r * 0.92, -r * 0.2]], 'white', 4);+ g.poly([[-r * 0.86, -r * 0.5], [r * 0.86, -r * 0.5], [r * 0.82, -r * 0.92], [-r * 0.82, -r * 0.92]], 'brown', 3.6);+ [-0.58, -0.2, 0.2, 0.58].forEach((k) => g.disc(r * k, -r * 0.3, r * 0.055, 'gold', 4.5, 6, { came: 0.008 }));+ },+ noah(g, r) { // The Ark on the waters, the dove returning+ [-0.82, -0.56].forEach((v, i) => g.poly([[-r, r * v], [r, r * v], [r, r * (v + 0.22)], [-r, r * (v + 0.18)]], i ? 'azure' : 'cobalt', 2));+ g.poly([[-r * 0.7, -r * 0.4], [r * 0.7, -r * 0.4], [r * 0.5, -r * 0.7], [-r * 0.5, -r * 0.7]], 'brown', 2.5);+ g.poly([[-r * 0.46, -r * 0.4], [r * 0.46, -r * 0.4], [r * 0.46, r * 0.02], [-r * 0.46, r * 0.02]], 'tan', 3);+ g.poly([[-r * 0.56, r * 0.0], [0, r * 0.32], [r * 0.56, r * 0.0]], 'ruby', 3.2);+ [-0.2, 0.2].forEach((k) => g.disc(r * k, -r * 0.18, r * 0.1, 'night', 3.4, 7, { came: 0.012 }));+ gBust(g, r * 0.2, -r * 0.3, r * 0.42, { robe: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', layer: 3.6 });+ g.disc(-r * 0.54, r * 0.64, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.72, r * 0.72], [-r * 0.54, r * 0.62], [-r * 0.36, r * 0.74], [-r * 0.54, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ shepherd(g, r) { // The Good Shepherd carries the lamb; a flock in green pasture+ g.poly([[-r, -r], [r, -r], [r, -r * 0.5], [-r, -r * 0.62]], 'green', 1.5);+ gFigure(g, 0, -r * 0.82, r * 1.5, { robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'hold', attr: 'staff' });+ g.disc(0, r * 0.26, r * 0.11, 'white', 4.5, 8, { came: 0.01 }); g.disc(r * 0.13, r * 0.3, r * 0.05, 'pearl', 4.8, 6, { came: 0.008 });+ for (const [x, y] of [[-0.68, -0.64], [0.66, -0.62], [-0.46, -0.78]]) { g.disc(r * x, r * y, r * 0.11, 'white', 3, 7, { came: 0.01 }); g.disc(r * x + r * 0.12, r * y + r * 0.05, r * 0.05, 'grey', 3.2, 5, { came: 0.008 }); }+ },+ david(g, r) { // King David with his harp+ gFigure(g, -r * 0.14, -r * 0.82, r * 1.42, { pose: 'sit', robe: 'violet', mantle: 'ruby', crown: 1, beard: 1, arm: 'out', halo: false });+ g.poly([[r * 0.26, -r * 0.6], [r * 0.7, r * 0.2], [r * 0.7, r * 0.46], [r * 0.16, -r * 0.36]], 'gold', 3, { came: 0.012 });+ g.line([[r * 0.2, -r * 0.4], [r * 0.2, r * 0.4]], 0.018, 'gold', 3.1); [0.3, 0.4, 0.5, 0.6].forEach((k) => g.line([[r * k, -r * 0.42 + r * (k - 0.2)], [r * k, r * 0.34]], 0.008, 'white', 3.2));+ g.poly([[-r * 0.86, -r * 0.84], [r * 0.5, -r * 0.84], [r * 0.5, -r * 0.7], [-r * 0.86, -r * 0.7]], 'tan', 1.8);+ },+};+function star(g, x, y, r, c, layer) { const p = []; for (let i = 0; i < 16; i++) { const a = i * PI / 8 + PI / 2, q = i % 2 ? r * 0.38 : r * (i % 4 ? 0.7 : 1); p.push([x + q * Math.cos(a), y + q * Math.sin(a)]); } g.poly(p, c, layer, { came: 0.01 }); }+function gBust(g, x, y, h, o = {}) { // head and shoulders, the head drawn by the painter+ const L = o.layer || 2, bx = [x - h * 0.22, y - h * 0.32, x + h * 0.22, y + h * 0.46], t = DRAPE_OF[o.robe || 'ruby'] || 'ruby';+ if (o.halo !== false) g.disc(x, y + h * 0.58, h * 0.17, o.halo || 'gold', L - 0.1, 10);+ g.poly([[x - h * 0.2, y - h * 0.3], [x + h * 0.2, y - h * 0.3], [x + h * 0.17, y + h * 0.3], [x + h * 0.06, y + h * 0.42], [x - h * 0.06, y + h * 0.42], [x - h * 0.17, y + h * 0.3]], o.robe || 'ruby', L, { tile: t, box: bx });+ const kind = o.face || (o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ g.face(x, y + h * 0.58, h * 0.108, h * 0.132, kind, L + 0.5, false);+ if (o.crown) g.poly([[x - h * 0.1, y + h * 0.66], [x + h * 0.1, y + h * 0.66], [x + h * 0.12, y + h * 0.78], [x + h * 0.05, y + h * 0.72], [x, y + h * 0.8], [x - h * 0.05, y + h * 0.72], [x - h * 0.12, y + h * 0.78]], 'gold', L + 0.7, { came: 0.008 });+ if (o.cross) for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[x + h * 0.17 * Math.cos(a - 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a - 0.24)], [x + h * 0.17 * Math.cos(a + 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a + 0.24)], [x, y + h * 0.58]], 'ruby', L + 0.2, { came: 0.008 }); }+}+// A jewelled ring of short leaded segments+function gRim(g, cx, cy, r0, r1, cols, n, layer) {+ g.disc(cx, cy, r1 + 0.02, 'lead', layer - 0.3, Math.max(10, n), { came: 0, jitter: 0 });+ for (let i = 0; i < n; i++) {+ const a = i * TAU / n, b = (i + 1) * TAU / n;+ g.poly([[cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)], [cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)], [cx + r1 * Math.cos(b), cy + r1 * Math.sin(b)], [cx + r0 * Math.cos(b), cy + r0 * Math.sin(b)]], cols[i % cols.length], layer, { came: -0.01 });+ }+}+// A lancet told as a stack of medallions over a diapered ground, with a jewelled border+function gLancetStory(g, x, w, y0, ys, rise, scenes, o = {}) {+ const c1 = o.c1 || 'ruby', c2 = o.c2 || 'cobalt', edge = o.edge || ['gold', 'ruby', 'white', 'cobalt'];+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse().slice(0)];+ g.withClip(outline, () => {+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, o.cell || 0.6, c1, c2, 'gold');+ const H = ys + rise * 0.5 - y0, N = scenes.length, step = H / N, R = Math.min(w * 0.44, step * 0.47);+ scenes.forEach((nm, i) => {+ const cy = y0 + step * (i + 0.5);+ const ground = (i % 2) ? (o.g1 || 'cobalt') : (o.g2 || 'ruby');+ gRim(g, x, cy, R, R * 1.055, ['gold', 'ruby', 'gold', 'cobalt'], 22, 1);+ g.disc(x, cy, R, ground, 1.3, 18, { came: 0 });+ g.withClip(ringPts(x, cy, R, 14), () => g.withXf(0, x, cy, 1, () => SCENES[nm](g, R)));+ if (i < N - 1) { const yy = y0 + step * (i + 1); for (const s of [-1, 1]) { g.disc(x + s * w * 0.3, yy, w * 0.058, 'gold', 1.2, 8, { came: 0.012 }); g.disc(x + s * w * 0.3, yy, w * 0.032, 'ruby', 1.5, 6, { came: 0.008 }); } }+ });+ // Border bricks follow the outline: thin, and each brick painted with a stem of leaves+ const bw = w * 0.062, stepB = 0.34;+ const stripe = (x0, y1, y2) => { let k = 0; g.poly([[x0, y1], [x0 + bw, y1], [x0 + bw, y2], [x0, y2]], 'lead', 3.5, { came: 0, jitter: 0 }); for (let y = y1; y < y2; y += stepB) { const c = edge[k++ % edge.length]; g.poly([[x0, y], [x0 + bw, y], [x0 + bw, y + stepB], [x0, y + stepB]], c, 3.8, { came: -0.012, orn: c === 'gold' ? 'gold' : c === 'white' ? 'white' : null, box: [x0, y, x0 + bw, y + stepB] }); } };+ stripe(x - w / 2, y0, ys + rise * 0.1); stripe(x + w / 2 - bw, y0, ys + rise * 0.1);+ // Saddle bars (iron armature)+ for (let y = y0 + 1.1; y < ys + rise * 0.6; y += 1.1) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.045], [x - w / 2, y + 0.045]], 'lead', 7, { came: 0, jitter: 0 });+ });+}+// A lancet filled by one monumental standing figure under a painted canopy (reads from the floor)+function gLancetSaint(g, x, w, y0, ys, rise, fig, o = {}) {+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse()];+ const gnd = o.ground || 'cobalt';+ g.withClip(outline, () => {+ const figY = y0 + 1.05, figH = (ys + rise * 0.1 - figY) * 0.84;+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, 0.8, gnd, o.ground2 || 'ruby', null);+ // Pedestal: a band of painted gold the figure stands on+ g.poly([[x - w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 1.02], [x - w * 0.46, y0 + 1.02]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.46, y0 + 0.62, x + w * 0.46, y0 + 1.02] });+ // Canopy: a thin arch of gold, a gable with its ruby field, amber pinnacles either side+ const cy = figY + figH + 0.02, cH = ys + rise * 0.86 - cy;+ g.poly([[x - w * 0.48, cy], [x + w * 0.48, cy], [x + w * 0.48, cy + cH * 0.1], [x - w * 0.48, cy + cH * 0.1]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.48, cy, x + w * 0.48, cy + cH * 0.1] });+ g.poly([[x - w * 0.42, cy + cH * 0.08], [x + w * 0.42, cy + cH * 0.08], [x, cy + cH]], 'yellow', 1.7, { orn: 'gold', box: [x - w * 0.42, cy + cH * 0.08, x + w * 0.42, cy + cH] });+ g.poly([[x - w * 0.22, cy + cH * 0.14], [x + w * 0.22, cy + cH * 0.14], [x, cy + cH * 0.62]], 'ruby', 1.9, { came: 0.016 });+ for (const s of [-1, 1]) g.poly([[x + s * w * 0.3, cy + cH * 0.12], [x + s * w * 0.46, cy + cH * 0.12], [x + s * w * 0.38, cy + cH * 0.85]], 'amber', 1.8);+ gFigure(g, x, figY, figH, Object.assign({ layer: 2.5, sx: Math.min(1, w * 1.72 / figH) }, fig));+ for (let y = y0 + 1.5; y < ys + rise * 0.6; y += 1.5) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.05], [x - w / 2, y + 0.05]], 'lead', 9, { came: 0, jitter: 0 });+ });+}+function gOculus(g, cx, cy, r, o = {}) { // head of the window: a sexfoil with an angel or Christ+ g.withClip(ringPts(cx, cy, r, 20), () => {+ g.disc(cx, cy, r, o.ground || 'cobalt', 1, 20);+ for (let i = 0; i < 6; i++) { const a = i * PI / 3 + PI / 6; g.disc(cx + r * 0.68 * Math.cos(a), cy + r * 0.68 * Math.sin(a), r * 0.27, i % 2 ? 'ruby' : 'gold', 1.4, 8); }+ g.disc(cx, cy, r * 0.56, o.inner || 'rubyLt', 1.8, 14);+ g.withXf(0, cx, cy, 1, () => { if (o.bust) gBust(g, 0, -r * 0.45, r * 0.95, o.bust); else gFigure(g, 0, -r * 0.5, r * 0.98, { wings: 'gold', robe: 'white', mantle: 'emerald', arm: 'orant' }); });+ });+}++// A complete two-light window: lancets, oculus, base field. Local plane z=0, +Z = interior.+function makeWindowGlass(w, y0, ys, rise, seed, fill) {+ const g = new Glass(seed);+ if (fill.trackPanes) g.b.panes = [];+ const outline = archOutline(w, y0, ys, rise, 8);+ g.raw(outline.slice(), 'lead', -0.4, 0);+ g.withClip(outline, () => gDiaper(g, -w / 2, ys, w / 2, ys + rise, 0.55, 'cobalt', 'ruby', 'gold', 0));+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96;+ const syl = ys + rise * 0.02;+ fill.light(g, -lx, lw, y0, syl, lr, 0); fill.light(g, lx, lw, y0, syl, lr, 1);+ const oc = windowHead(w, ys, rise);+ gOculus(g, 0, oc.y, oc.r + 0.03, fill.oculus || {});+ return g.b;+}+function windowHead(w, ys, rise) { const r = w * 0.225; return { r, y: ys + rise * 0.62 }; }++// ==================================================================================+// STONE TRACERY for a two-light window (plane z=0, bars deep along Z)+// ==================================================================================+function addTracery(b, w, y0, ys, rise, o = {}) {+ const bw = o.bar || 0.26, bd = o.depth || 0.42, sec = traceryBar(bw, bd), secT = hexBar(bw * 0.62, bd * 0.8);+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96, syl = ys + rise * 0.02;+ // Central mullion with shaft ring and capital+ b.at(M.T(0, 0, 0), () => {+ b.sweep([[0, y0 - 0.05], [0, syl]], sec, {});+ b.at(M.T(0, syl, 0), () => b.lathe([[bw * 0.52, -0.3], [bw * 0.56, -0.2], [bw * 0.9, 0.02], [bw * 0.5, 0.06]], 6));+ });+ for (const s of [-1, 1]) {+ const arc = pointedArc(lw, lr, 5).map((p) => [s * lx + p[0], syl + p[1]]);+ b.sweep(arc, sec, {});+ // cusped trefoil head inside each lancet+ const cr = lw * 0.3, cy = syl + lr * 0.34;+ const cusp = []; for (let i = 0; i <= 8; i++) { const a = rad(-30 + i * 240 / 8); const R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([s * lx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, { caps: false });+ b.sweep([[s * (lx + lw / 2), cy - cr * 0.4], cusp[0]], secT, { caps: false });+ b.sweep([[s * (lx - lw / 2), cy - cr * 0.4], cusp[8]], secT, { caps: false });+ }+ // Oculus with six foils+ const oc = windowHead(w, ys, rise);+ b.sweep(ringPts(0, oc.y, oc.r, 14), sec, { closed: true });+ const foils = []; for (let i = 0; i < 18; i++) { const a = i * TAU / 18; const R = oc.r * (0.62 + 0.3 * Math.pow(Math.abs(Math.cos(a * 3)), 0.8)); foils.push([R * Math.cos(a), oc.y + R * Math.sin(a)]); }+ b.sweep(foils, secT, { closed: true, crease: 40 });+}++// ==================================================================================+// PIERCED PARAPET with quatrefoil panels+// ==================================================================================+function quatrefoil(cx, cy, r, n = 16) { const p = []; for (let i = 0; i < n; i++) { const a = i * TAU / n; const R = r * (0.72 + 0.28 * Math.abs(Math.cos(2 * a))); p.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); } return p; }+function addParapet(b, x0, x1, y, h, th, o = {}) {+ const n = Math.max(1, Math.round((x1 - x0) / (o.pitch || 1.05))), holes = [];+ const r = Math.min(h * 0.34, (x1 - x0) / n * 0.4);+ for (let i = 0; i < n; i++) holes.push(quatrefoil(x0 + (i + 0.5) * (x1 - x0) / n, y + h * 0.5, r, o.nh || 12));+ b.slab([[x0, y], [x1, y], [x1, y + h], [x0, y + h]], holes, -th / 2, th / 2, { sr: 0.6 });+ const cap = barSection(0.14, th + 0.14);+ b.sweep([[x0, y + h + 0.04], [x1, y + h + 0.04]], cap, {});+ b.sweep([[x0, y - 0.02], [x1, y - 0.02]], barSection(0.1, th + 0.1), { s: 0.8 });+}++// A wimperg: gable over a window with crockets, finial and a blind trefoil+function addGable(b, w, h, z0, th, y, o = {}) {+ const lod = o.lod === undefined ? 1 : o.lod;+ const holes = o.solid ? [] : [ringPts(0, y + h * 0.36, Math.min(w * 0.13, h * 0.2), 14)];+ if (!o.noField) b.slab([[-w / 2, y], [w / 2, y], [0, y + h]], holes, z0, z0 + th, { sf: 1, sb: 0.8 });+ const rake = barSection(0.18, th + 0.14);+ b.sweep([[-w / 2 - 0.06, y - 0.02], [0, y + h + 0.05]], rake, { z: z0 + th / 2 });+ b.sweep([[0, y + h + 0.05], [w / 2 + 0.06, y - 0.02]], rake, { z: z0 + th / 2 });+ const n = o.crockets === undefined ? Math.max(2, Math.round(Math.hypot(w / 2, h) / 1.05)) : o.crockets, sz = o.size || 0.46;+ const zc = z0 + th / 2;+ crocketsAlong(b, [-w / 2, y + 0.05, zc], [-0.03, y + h * 0.97, zc], n, sz, [-h, w / 2, 0], lod);+ crocketsAlong(b, [w / 2, y + 0.05, zc], [0.03, y + h * 0.97, zc], n, sz, [h, w / 2, 0], lod);+ b.at(M.T(0, y + h * 0.98, zc), () => addFinial(b, o.finial || 0.95, lod));+}+// A small blind trefoil-headed arcade on a wall face (plane z=0, moulding stands proud +Z)+function addBlindArcade(b, x0, x1, y0, ys, n, o = {}) {+ const w = (x1 - x0) / n, sec = hexBar(o.bar || 0.12, o.depth || 0.16), rise = w * 0.95;+ for (let i = 0; i < n; i++) {+ const x = x0 + (i + 0.5) * w;+ b.sweep(pointedArc(w * 0.9, rise, 4).map((p) => [x + p[0], ys + p[1]]), sec, { z: 0.04, caps: false });+ }+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * w;+ b.at(M.T(x, 0, 0.06), () => b.lathe([[0.1, y0], [0.065, y0 + 0.18], [0.065, ys - 0.14], [0.13, ys], [0.08, ys + 0.04]], 5));+ }+}++// ==================================================================================+// RIBBED VAULT: pointed-arc ribs, curved webs, carved or gilded boss.+// ==================================================================================+function arcH(L, H, f) { // height of a two-centred arc of span L, rise H at fraction f+ const c = (H * H - L * L / 4) / L, r = c + L / 2, d = Math.abs(f - 0.5) * L;+ return Math.sqrt(Math.max(0, r * r - (d + c) * (d + c)));+}+function addRib(b, A, B, yS, rise, sec, n = 8, half = false) { // rib between plan points A,B in their vertical plane+ const dx = B[0] - A[0], dz = B[1] - A[1], L = Math.hypot(dx, dz), ang = Math.atan2(dz, dx) * 180 / PI;+ const pts = [];+ if (half) for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L * 2, rise, t / 2)]); }+ else for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L, rise, t)]); }+ // plane: local X along A->B, local Y up+ b.at(M.of(M.T(A[0], 0, A[1]), M.Ry(-ang)), () => b.sweep(pts, sec, { caps: false }));+}+// One curved web between a side arc a-b and the crown c, faces downward whichever way the plan runs+function addVaultCell(A, a, b, c, yS, yC, sideRiseV, nS = 8, nT = 6) {+ const La = Math.hypot(c[0] - a[0], c[1] - a[1]), Lb = Math.hypot(c[0] - b[0], c[1] - b[1]), Lab = Math.hypot(b[0] - a[0], b[1] - a[1]), rise = yC - yS;+ const cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ A.grid((s, t) => {+ const px = lerp(a[0], c[0], t) * (1 - s) + lerp(b[0], c[0], t) * s, pz = lerp(a[1], c[1], t) * (1 - s) + lerp(b[1], c[1], t) * s;+ const ruled = lerp(yS + arcH(Lab, sideRiseV, s), yC, t);+ const dA = arcH(La * 2, rise, t / 2) - rise * t, dB = arcH(Lb * 2, rise, t / 2) - rise * t;+ const y = ruled + (1 - s) * (1 - s) * dA + s * s * dB;+ return [px, y, pz, 0.72 + 0.28 * t];+ }, nS, nT, { flip: cross < 0, metric: true });+}+function addRibVault(F, A, corners, yS, yC, o = {}) {+ // corners: 4 plan points in order; web cells between each side and the crown+ const c0 = corners, cx = (c0[0][0] + c0[1][0] + c0[2][0] + c0[3][0]) / 4, cz = (c0[0][1] + c0[1][1] + c0[2][1] + c0[3][1]) / 4;+ const crown = [cx, yC, cz], nS = o.nS || 8, nT = o.nT || 6, rise = yC - yS;+ const sideRise = o.sideRise || ((i) => rise * 0.94);+ for (let k = 0; k < 4; k++) {+ if (o.skipSide && o.skipSide[k]) continue;+ addVaultCell(F, c0[k], c0[(k + 1) % 4], [cx, cz], yS, yC, sideRise(k), nS, nT);+ }+ const sec = o.ribSec || rollSection(0.32, 0.42, 2, 0.04, 2);+ for (const c of c0) addRib(F, c, [cx, cz], yS, rise, sec, 8, true);+ for (let k = 0; k < 4; k++) if (!(o.skipRib && o.skipRib[k])) addRib(F, c0[k], c0[(k + 1) % 4], yS, sideRiseRib(o, k, rise), o.sideRibSec || sec, 8);+ return crown;+}+const sideRiseRib = (o, k, rise) => (o.sideRise ? o.sideRise(k) : rise * 0.94);+function addBoss(G, F, c, r = 0.42) { // carved foliate boss with gilding+ G.at(M.T(c[0], c[1] - 0.02, c[2]), () => {+ G.lathe([[r * 0.25, -r * 0.54], [r * 0.6, -r * 0.5], [r * 0.95, -r * 0.3], [r, -r * 0.08], [r * 0.7, r * 0.05]], 10);+ for (let i = 0; i < 8; i++) addLeaf(G, M.of(M.T(0, -r * 0.22, 0), M.Ry(i * 45), M.T(r * 0.72, 0, 0), M.Rz(160)), r * 0.72);+ });+}++// ==================================================================================+// CLUSTERED PIER: a lobed section of shafts around a core, with bases and foliate capitals+// ==================================================================================+function lobedRing(core, shafts, n = 64) {+ const out = [];+ for (let i = 0; i < n; i++) {+ const a = TAU * i / n, c = Math.cos(a), s = Math.sin(a);+ let best = core, nx = c, nz = s;+ for (const sh of shafts) {+ // ray from the origin against each shaft circle+ const b = c * sh[0] + s * sh[1], disc = b * b - (sh[0] * sh[0] + sh[1] * sh[1] - sh[2] * sh[2]);+ if (disc >= 0) { const t = b + Math.sqrt(disc); if (t > best) { best = t; nx = (t * c - sh[0]) / sh[2]; nz = (t * s - sh[1]) / sh[2]; } }+ }+ out.push([best * c, best * s, nx, nz]);+ }+ return out;+}+function addExtrudedRing(b, ring, y0, y1, o = {}) {+ const s0 = o.s0 === undefined ? 1 : o.s0, s1 = o.s1 === undefined ? 1 : o.s1, k0 = o.k0 || 1, k1 = o.k1 || 1, N = ring.length;+ const base = b.count; let perim = 0;+ for (let i = 0; i <= N; i++) {+ const r = ring[i % N], rp = ring[(i + N - 1) % N];+ if (i > 0) perim += Math.hypot(r[0] - rp[0], r[1] - rp[1]);+ const shade = 0.5 + 0.5 * Math.pow(Math.abs(r[2] * r[0] + r[3] * r[1]) / (Math.hypot(r[0], r[1]) || 1), 2);+ b.v(r[0] * k0, y0, r[1] * k0, r[2], 0, r[3], perim, y0, s0 * shade);+ b.v(r[0] * k1, y1, r[1] * k1, r[2], 0, r[3], perim, y1, s1 * shade);+ }+ for (let i = 0; i < N; i++) b.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+}+function addCapital(b, ring, y, h, k, o = {}) { // bell with crocket leaves on each shaft, moulded abacus+ const N = ring.length;+ addExtrudedRing(b, ring, y, y + h * 0.1, { k0: 1.0, k1: 1.03, s0: 0.8 });+ addExtrudedRing(b, ring, y + h * 0.1, y + h * 0.82, { k0: 1.03, k1: k, s0: 0.62, s1: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.82, y + h * 0.9, { k0: k, k1: k + 0.1, s0: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.9, y + h, { k0: k + 0.1, k1: k + 0.12, s0: 0.95 });+ const top = ring.map((r) => [r[0] * (k + 0.12), r[1] * (k + 0.12)]);+ const ids = top.map((p) => b.v(p[0], y + h, p[1], 0, 1, 0, undefined, undefined, 0.9)); for (let i = 1; i < N - 1; i++) b.tri(ids[0], ids[i + 1], ids[i]);+ const leaves = o.leaves || 12;+ for (let i = 0; i < leaves; i++) {+ const a = i * 360 / leaves + (o.phase || 0), idx = Math.round(i / leaves * N) % N, r = ring[idx], R = Math.hypot(r[0], r[1]);+ addLeaf(b, M.of(M.T(0, y + h * 0.1, 0), M.Ry(-a), M.T(R * 1.02, 0, 0)), h * (i % 2 ? 0.72 : 0.56), { coarse: 1 });+ }+}++// The bay plan shared by every straight module (+Z side of the axis)+const zn = P.naveSpan / 2, zaw = zn + P.aisleSpan, BAY = P.bay;+// Window openings follow the bay, so changing the bay length re-proportions the glass and tracery+const WIN = {+ aW: BAY - 1.95, aSill: 4.3, aSp: P.pierTop - 1, aRise: 5.6 * (BAY - 1.95) / 4.65,+ cW: BAY - 1.55, cSill: P.triforiumTop + 0.45, cSp: P.vaultSpring + 4.8, cRise: 6.7 * (BAY - 1.55) / 5.05,+};+const windowArgs = { aisle: [WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise], clere: [WIN.cW, WIN.cSill, WIN.cSp, WIN.cRise] };+const mkBatches = () => { const D = new Batch(1); D.noColor = true; return { A: new Batch(TILE.ashlar), F: new Batch(TILE.fine), L: new Batch(TILE.lead), G: new Batch(TILE.gold), O: new Batch(TILE.oak), I: new Batch(TILE.iron), D }; };+// A stain hanging from a ledge on a wall facing local +z: runoff, drips, soft shadow or rising damp. Few vertices, crisp detail.+const STAIN_RNG = mkRng(4242);+function addStain(D, x0, x1, y, z, drop, kind = 'streak') {+ const R = DECAL_UV[kind], n = Math.max(1, Math.round((x1 - x0) / 4)), w = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = lerp(x0, x1, i / n), c = lerp(x0, x1, (i + 1) / n), span = w * Math.min(1, (c - a) / 5), u0 = R[0] + 0.002 + STAIN_RNG() * (w - span - 0.004);+ const q = [D.v(a, y, z, 0, 0, 1, u0, R[1], 1), D.v(a, y - drop, z, 0, 0, 1, u0, R[3], 1), D.v(c, y - drop, z, 0, 0, 1, u0 + span, R[3], 1), D.v(c, y, z, 0, 0, 1, u0 + span, R[1], 1)];+ D.quad(q[0], q[1], q[2], q[3]);+ }+}+// A real recess in a wall facing local +z, whose slab already has the opening cut: back plate, moulded frame, a sill+function addRecess(A, F, outline, depth) {+ const o = area2(outline) > 0 ? outline : outline.slice().reverse();+ let x0 = 1e9, x1 = -1e9, y0 = 1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); }+ A.face(o.map((p) => [p[0], p[1], -depth]));+ F.sweep(o, rollSection(0.1, 0.22, 1, 0.03, 2), { closed: true, crease: 40, z: -0.02 });+ F.sweep([[x0 - 0.16, y0 - 0.06], [x1 + 0.16, y0 - 0.06]], stringSection(0.16, 0.14), {});+}+// A real buttress on a wall facing local +z. stages, bottom to top: { y0, y1, w: half width, f: how far its front stands out }.+// Each stage narrows and steps back, and ends in a sloped stone cap running up to the next: a solid wedge of the pier, never+// a sheet. o.back ties it into the masonry behind; the last stage ends in a weathering back to the wall, or flat with o.flat.+function addPier(set, stages, o = {}) {+ const { A, F } = set, zb = -(o.back === undefined ? 0.3 : o.back);+ const face = (m, hw, y0, y1, p) => {+ const out = [[-hw, y0], [hw, y0], [hw, y1], [-hw, y1]];+ if (!p) { A.at(m, () => A.face(out.map((q) => [q[0], q[1], 0]))); return; }+ const hole = archOutline(hw * 2 * p.k, y0 + p.b, y1 - p.t - p.r, p.r, 5);+ A.at(m, () => { A.slab(out, [hole], -0.2, 0, { noOuterSides: true, noBack: true, sr: 0.66 }); F.at(m, () => addRecess(A, F, hole, 0.2)); });+ };+ stages.forEach((s, i) => {+ const n = stages[i + 1], mid = (s.f + zb) / 2, half = (s.f - zb) / 2;+ face(M.T(0, 0, s.f), s.w, s.y0, s.y1, s.panel);+ face(M.of(M.T(s.w, 0, mid), M.Ry(90)), half, s.y0, s.y1, s.sidePanel);+ face(M.of(M.T(-s.w, 0, mid), M.Ry(-90)), half, s.y0, s.y1, s.sidePanel);+ if (s.backFace) A.face([[s.w, s.y0, zb], [-s.w, s.y0, zb], [-s.w, s.y1, zb], [s.w, s.y1, zb]]);+ if (!s.noDrip) F.box(-s.w - 0.05, s.w + 0.05, s.y1 - 0.14, s.y1, zb, s.f + 0.05, { noTop: true });+ if (!n && o.flat) { F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [s.w, s.y1, zb], [-s.w, s.y1, zb]]); return; }+ const ny = n ? n.y0 : s.y1 + (o.capH || (s.f - zb) * 0.9), nw = n ? n.w : s.w, nf = n ? n.f : zb;+ F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [nw, ny, nf], [-nw, ny, nf]]);+ F.face([[s.w, s.y1, s.f], [s.w, s.y1, zb], [nw, ny, zb], [nw, ny, nf]]);+ F.face([[-s.w, s.y1, zb], [-s.w, s.y1, s.f], [-nw, ny, nf], [-nw, ny, zb]]);+ });+}+function placeSet(name, set, pos, rot, parent, extra = {}) {+ const o = { pos, rot: [0, rot, 0], parent, scale: extra.scale };+ place(name + '_Ashlar', set.A, MAT.ashlar, o);+ place(name + '_Stone', set.F, MAT.fine, o);+ place(name + '_Lead', set.L, MAT.lead, o);+ place(name + '_Gilt', set.G, MAT.gold, o);+ place(name + '_Oak', set.O, MAT.oak, o);+ place(name + '_Iron', set.I, MAT.iron, o);+ if (set.D) place(name + '_Weathering', set.D, MAT.decal, o);+}+// Glass pair: interior face + the same panes mirrored as the exterior face+function placeGlass(name, batch, pos, rotY, parent, keepOutside, k = 1) {+ place(name + '_In', batch, keepOutside ? MAT.glassRose : MAT.glassIn, { pos, rot: [0, rotY, 0], scale: [k, k, 1], parent });+ place(name + '_Out', keepOutside ? batch : glassOutside(batch), keepOutside ? MAT.glassOut : MAT.glassOutWin, { pos, rot: [0, rotY, 0], scale: [k, k, -1], parent });+}+// By day a window is dark from outside and the story cannot be read, so its outer skin is only the+// window's outline over a mosaic of pot-metals: the colour hints through for a few triangles.+function glassOutside(batch) {+ if (batch.outer) return batch.outer;+ if (!batch.outline) return batch;+ const g = new Glass(7), o = batch.outline;+ let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); }+ g.raw(o, 'lead', -0.4, 0);+ g.withClip(o, () => gDiaper(g, x0, y0, x1, y1, 0, 'outer', null, null, 0));+ batch.outer = g.b;+ return g.b;+}++function buildNaveBay(o = {}) {+ const set = o.into || mkBatches(), { A, F, L, G } = set;+ const B = o.B || BAY, ww = o.ww || BAY - 2.1, sp = P.pierTop, archRise = o.archRise || 5.1, thick = 0.85;+ const zw = o.zw === undefined ? zn : o.zw;+ if (o.part !== 'aisle') {+ // ---- Arcade wall with the moulded pointed arch ----+ const arc = pointedArc(ww, archRise, 7).map((p) => [p[0], p[1] + sp]);+ A.at(M.T(0, 0, zw), () => {+ A.slab([[-B / 2, sp], [-ww / 2, sp], ...arc, [ww / 2, sp], [B / 2, sp], [B / 2, P.triforiumSill], [-B / 2, P.triforiumSill]], [], -thick / 2, thick / 2, { noOuterSides: true });+ });+ F.at(M.T(0, 0, zw), () => { F.sweep(arc, rollSection(0.82, thick + 0.25, 2, 0.08, 3), {}); });+ // Hood mould over the arch on the nave side, with carved label stops+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ const hood = pointedArc(ww + 1.9, archRise + 1.0, 6).map((p) => [p[0], p[1] + sp]);+ F.sweep(hood, stringSection(0.16, 0.14), {});+ });+ // ---- Triforium: string course, four cusped arches, dark wall passage ----+ const yT = P.triforiumSill, yT2 = P.triforiumTop, nT = o.nT || 4, tw = o.tw || (BAY - 1.72) / 4, ty = yT + 0.62, tspr = yT2 - 1.5, gapT = nT > 2 ? 0.08 : 0;+ const trfX = (i) => (i + 0.5 - nT / 2) * tw + (i < nT / 2 ? -gapT : gapT);+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ F.sweep([[-B / 2, yT], [B / 2, yT]], stringSection(0.34, 0.3), {});+ F.sweep([[-B / 2, yT2], [B / 2, yT2]], stringSection(0.3, 0.26), {});+ // Colonnette arcade standing in the openings+ for (let i = 0; i < nT; i++) {+ const x = trfX(i);+ const a = pointedArc(tw * 0.92, 0.98, 4).map((p) => [x + p[0], tspr + p[1]]);+ F.sweep(a, hexBar(0.13, 0.26), { z: 0.05 });+ F.sweep(ringPts(x, tspr + 0.32, 0.17, 6), hexBar(0.05, 0.1), { z: 0.05, closed: true, s: 0.9 });+ }+ for (let i = 0; i <= nT; i++) {+ const mid = i === nT / 2;+ const x = (i - nT / 2) * tw + (i < nT / 2 ? -gapT : (mid ? 0 : gapT));+ const r = mid ? 0.14 : 0.085;+ F.at(M.T(x, 0, 0.05), () => F.lathe([[r * 1.7, ty - 0.02], [r, ty + 0.2], [r, tspr - 0.2], [r * 1.9, tspr - 0.01], [r * 1.5, tspr + 0.03]], 6));+ }+ });+ const holes = []; // openings through the inner skin+ for (let i = 0; i < nT; i++) { const x = trfX(i); holes.push(archOutline(tw * 0.92, ty, tspr, 0.98, 6).map((p) => [x + p[0], p[1]])); }+ A.at(M.T(0, 0, zw - thick / 2 + 0.22), () => A.slab([[-B / 2, yT], [B / 2, yT], [B / 2, yT2], [-B / 2, yT2]], holes, -0.22, 0.22, { noOuterSides: true, sf: 0.45, sr: 0.5 }));+ // Passage back wall and floor: seen only through the arcade, kept dark+ // Round the apse the passage fans out with the radius, so the walls of neighbouring bays meet at their corners+ const zBk = zw + 0.92, zFr = zw - thick / 2 + 0.44, hB = o.wedge ? (B / 2) * zBk / zw : B / 2, hF = o.wedge ? (B / 2) * zFr / zw : B / 2;+ A.shade(0.34, () => {+ A.face([[hB, yT, zBk], [-hB, yT, zBk], [-hB, yT2 + 0.5, zBk], [hB, yT2 + 0.5, zBk]]);+ A.face([[-hB, yT + 0.02, zBk], [hB, yT + 0.02, zBk], [hF, yT + 0.02, zFr], [-hF, yT + 0.02, zFr]]);+ A.face([[-hF, yT2 + 0.2, zFr], [hF, yT2 + 0.2, zFr], [hB, yT2 + 0.2, zBk], [-hB, yT2 + 0.2, zBk]]);+ });+ // ---- Clerestory wall, window hole, tracery ----+ const cw = o.cw || WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = o.cRise || WIN.cRise;+ const yE = P.eaves;+ A.at(M.T(0, 0, zw), () => A.slab([[-B / 2, yT2], [B / 2, yT2], [B / 2, yE], [-B / 2, yE]], [archOutline(cw, cSill, cSp, cRise, 9)], -0.42, 0.42, { noOuterSides: true, sr: 0.66 }));+ F.at(M.T(0, 0, zw), () => {+ if (o.lights === 1) addLancetTracery(F, cw, cSill, cSp, cRise);+ else addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.44 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 9).slice(0), rollSection(0.14, 0.86, 1, 0.03, 2), { closed: true, s: 0.92, crease: 40 });+ });+ // Exterior: hood mould, wimperg gable, cornice, pierced parapet, corbel table+ set.D.at(M.T(0, 0, zw + 0.42), () => { addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.02, 3.6); addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.03, 0.9, 'soft'); });+ F.at(M.T(0, 0, zw + 0.42), () => {+ F.sweep(pointedArc(cw + 0.5, cRise + 0.3, 6).map((p) => [p[0], p[1] + cSp]), hexBar(0.22, 0.26), {});+ addGable(F, Math.min(cw + 0.9, B * 0.96), 3.4, 0.02, 0.2, cSp + cRise * 0.52, { size: 0.44, finial: 0.8, lod: 0, crockets: 3 });+ F.sweep([[-B / 2, yT2 - 0.02], [B / 2, yT2 - 0.02]], stringSection(0.3, 0.2), {});+ F.sweep([[-B / 2, yE - 0.12], [B / 2, yE - 0.12]], stringSection(0.34, 0.38), {});+ const nCorb = Math.round(B / 1.1);+ for (let i = 0; i < nCorb; i++) { const x = -B / 2 + (i + 0.5) * B / nCorb; F.box(x - 0.13, x + 0.13, yE - 0.62, yE - 0.32, 0, 0.26, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zw + 0.3), () => addParapet(F, -B / 2, B / 2, yE, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Upper wall top closing the gutter+ A.face([[-B / 2, yE, zw + 0.42], [B / 2, yE, zw + 0.42], [B / 2, yE, zw - 0.42], [-B / 2, yE, zw - 0.42]], 0.7);+ }+ if (o.part !== 'upper') {+ // ---- Aisle roof (lean-to in lead) ----+ if (!o.noAisleRoof) {+ const z0 = zn + 0.42, z1 = zaw + 1.05, y0r = P.aisleRoofHigh, y1r = P.aisleWallTop - 0.2;+ L.face([[-B / 2, y0r, z0], [-B / 2, y1r, z1], [B / 2, y1r, z1], [B / 2, y0r, z0]]);+ }+ if (!o.open) {+ // ---- Aisle wall ----+ const wa = WIN.aW, sill = WIN.aSill, aSp = WIN.aSp, aRise = WIN.aRise, zOut = zaw + 0.45;+ A.at(M.T(0, 0, zOut), () => A.slab([[-B / 2, 0], [B / 2, 0], [B / 2, P.aisleWallTop], [-B / 2, P.aisleWallTop]], [archOutline(wa, sill, aSp, aRise, 9)], -0.45, 0.45, { noOuterSides: true, sr: 0.7 }));+ F.at(M.T(0, 0, zOut), () => {+ addTracery(F, wa, sill, aSp, aRise, { bar: 0.25, depth: 0.42 });+ F.sweep(archOutline(wa, sill, aSp, aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ });+ // Exterior of the aisle wall+ set.D.at(M.T(0, 0, zaw + 0.9), () => {+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.02, 4.4);+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.02, 2.6, 'drip');+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.03, 0.7, 'soft');+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.03, 0.9, 'soft');+ addStain(set.D, -B / 2, B / 2, 5.2, 0.03, 5.2, 'ground');+ });+ F.at(M.T(0, 0, zaw + 0.9), () => {+ F.sweep([[-B / 2, sill - 0.2], [B / 2, sill - 0.2]], stringSection(0.34, 0.3), {});+ F.sweep(pointedArc(wa + 0.46, aRise + 0.28, 6).map((p) => [p[0], p[1] + aSp]), hexBar(0.22, 0.26), {});+ addGable(F, wa + 0.8, 3.1, 0.02, 0.2, aSp + 2.95, { size: 0.44, finial: 0.8, crockets: 4 });+ F.sweep([[-B / 2, P.aisleWallTop - 0.12], [B / 2, P.aisleWallTop - 0.12]], stringSection(0.32, 0.34), {});+ // Plinth with a chamfered weathering+ F.sweep([[-B / 2, 0.55], [B / 2, 0.55]], stringSection(0.4, 0.42), {});+ F.sweep([[-B / 2, 1.3], [B / 2, 1.3]], stringSection(0.2, 0.2), {});+ addBlindArcade(F, -B / 2 + 0.9, B / 2 - 0.9, 1.45, 2.65, 4, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(0, 0, zaw + 0.72), () => addParapet(F, -B / 2, B / 2, P.aisleWallTop, 1.0, 0.24, { pitch: 1.3 }));+ A.face([[-B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw], [-B / 2, P.aisleWallTop, zaw]], 0.7);+ // Interior dado: blind arcade with a niche for a statue+ F.at(M.of(M.T(0, 0, zaw), M.Ry(180)), () => {+ F.sweep([[-B / 2, sill - 0.14], [B / 2, sill - 0.14]], stringSection(0.26, 0.22), {});+ addBlindArcade(F, -B / 2 + 0.5, -1.05, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ addBlindArcade(F, 1.05, B / 2 - 0.5, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ F.sweep([[-B / 2, 0.42], [B / 2, 0.42]], stringSection(0.4, 0.34), {});+ // Niche canopy over the statue+ addNicheFrame(F, 0, 0.6, 3.9, 1.05);+ });+ }+ // ---- Aisle vault ----+ if (!o.noAisleVault) {+ const z0 = zn + thick / 2, z1 = zaw;+ const crown = addRibVault(F, A, [[-B / 2, z0], [B / 2, z0], [B / 2, z1], [-B / 2, z1]], sp, P.aisleCrown - 0.3, { sideRise: (k) => (k % 2 === 0 ? (k === 0 ? archRise + 0.1 : 5.0) : 5.2), nS: 6, nT: 4, ribSec: rollSection(0.26, 0.34, 2, 0.03, 2) });+ addBoss(G, F, crown, 0.32);+ }+ }+ return set;+}+// Canopied niche frame: moulded opening, gablet with crockets. Statue placed separately.+function addNicheFrame(F, x, y0, yTop, w) {+ F.at(M.T(x, 0, 0), () => {+ F.shade(0.5, () => F.face([[-w / 2, y0, -0.005], [w / 2, y0, -0.005], [w / 2, yTop - 0.75, -0.005], [0, yTop - 0.1, -0.005], [-w / 2, yTop - 0.75, -0.005]]));+ F.box(-w / 2 - 0.05, w / 2 + 0.05, y0 - 0.14, y0 + 0.08, 0, 0.5, { s: 0.92 });+ F.sweep(archOutline(w, y0, yTop - 0.82, 0.72, 5).slice(2), barSection(0.11, 0.2), { z: 0.08 });+ for (const s of [-1, 1]) { F.at(M.T(s * w / 2, 0, 0.1), () => F.lathe([[0.07, y0 + 0.05], [0.05, y0 + 0.12], [0.05, yTop - 0.9], [0.09, yTop - 0.82], [0.05, yTop - 0.78]], 6)); }+ addGable(F, w + 0.24, 0.92, 0.06, 0.1, yTop - 0.98, { size: 0.19, crockets: 2, finial: 0.32, solid: true, lod: 0 });+ });+}++// Clustered nave pier with the vault shafts rising to the springing, on +Z = nave... shafts toward -Z+function buildPier() {+ const set = mkBatches(), { F, A, G } = set;+ const core = 0.8, big = 0.25, small = 0.14;+ const shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.1) * Math.cos(a), (core + 0.1) * Math.sin(a), big]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; shafts.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 48);+ SHARED.pierRing = ring; // the plan of the shaft, which also carries the window light on its north-arcade faces+ const yB = 1.15, yC = P.pierTop - 1.05;+ // moulded base: octagonal plinth, roll, scotia, then the lobed shaft+ F.lathe([[1.7, 0], [1.7, 0.56], [1.58, 0.62]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.62, 0.86, { k0: 1.24, k1: 1.27 });+ addExtrudedRing(F, ring, 0.86, yB, { k0: 1.27, k1: 1.0, s0: 0.75, s1: 0.72 });+ addExtrudedRing(F, ring, yB, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.05, 1.2, { leaves: 14 });+ // The vault shafts rising from the capital up the nave wall (toward -Z)+ const upper = [[0, -(core + 0.1), big], [-0.5, -(core - 0.04), small], [0.5, -(core - 0.04), small]];+ for (const u of upper) {+ F.at(M.T(u[0], 0, u[1] + 0.44), () => {+ const r = u[2], yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, P.triforiumSill - 0.1], [r * 1.5, P.triforiumSill + 0.06], [r, P.triforiumSill + 0.22], [r, yS - 0.85], [r * 1.5, yS - 0.8], [r * 1.14, yS - 0.7], [r * 2.2, yS]], 8);+ for (let i = 0; i < 4; i++) addLeaf(F, M.of(M.T(0, yS - 0.72, 0), M.Ry(i * 90 + 45), M.T(r * 1.1, 0, 0)), 0.56, { coarse: 1 });+ });+ }+ // Statue stage on the nave face: corbel below, canopy above (the figure is placed separately)+ const zF = -(core + 0.34);+ F.at(M.T(0, 0, zF), () => {+ F.lathe([[0.1, 5.0], [0.28, 5.25], [0.44, 5.6], [0.5, 5.72], [0.44, 5.78]], 8, { s: 0.85 });+ for (let i = 0; i < 6; i++) addLeaf(F, M.of(M.T(0, 5.04, 0), M.Ry(i * 60), M.T(0.14, 0, 0)), 0.66, { coarse: 1 });+ // Canopy over the statue: three arched gablets and a little spire+ F.at(M.T(0, 8.25, 0), () => {+ F.lathe([[0.5, -0.02], [0.54, 0.06], [0.42, 0.12], [0.3, 0.6], [0.02, 2.0]], 6, { faceted: true, phase: PI / 6 });+ for (let i = 0; i < 3; i++) F.at(M.of(M.Ry(-60 + i * 60 + 180), M.T(0, 0, 0.44)), () => addGable(F, 0.62, 0.62, -0.03, 0.06, 0.02, { size: 0.15, crockets: 1, finial: 0.2, solid: true, lod: 0 }));+ });+ });+ return set;+}+// The hemicycle's pier: the nave pier's family, slimmer, with one vault shaft running up to the springing+function buildApsePier() {+ const set = mkBatches(), { F } = set, core = 0.5, big = 0.15, small = 0.09, shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.06) * Math.cos(a), (core + 0.06) * Math.sin(a), big]); }+ for (let i = 0; i < 4; i++) { const a = PI / 4 + i * PI / 2; shafts.push([(core + 0.01) * Math.cos(a), (core + 0.01) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 32), yB = 0.86, yC = P.pierTop - 0.82;+ F.lathe([[1.02, 0], [1.02, 0.46], [0.94, 0.52]], 8, { faceted: true, phase: PI / 8 });+ addExtrudedRing(F, ring, 0.52, yB, { k0: 1.26, k1: 1.0 });+ addExtrudedRing(F, ring, yB, yC, {});+ addCapital(F, ring, yC, 0.82, 1.22, { leaves: 10 });+ F.at(M.T(0, 0, -(core + 0.06) + 0.24), () => {+ const r = big, yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, yS - 0.5], [r * 1.5, yS - 0.42], [r * 2.1, yS]], 8);+ });+ return set;+}+// Flying buttress unit at a bay boundary: massive pier, two arched flyers, tabernacle and pinnacles+const BUTTRESS = { hw: 0.98, zB: 19.2, zStack: 15.3, zT: 16.3 };+const flyerRise = (yA, yB, t) => yA + (yB - yA) * lerp(t, Math.sin(t * PI / 2), 0.6);+function flyerShape(zA, yA, zB, yB, thick) { // A at the pier (outer), B at the wall (inner); returns polygon points in (z,y)+ const N = 12, under = [], top = [];+ for (let i = 0; i <= N; i++) {+ const t = i / N, z = lerp(zA, zB, t), y = flyerRise(yA, yB, t);+ under.push([z, y]);+ }+ // The coping is a straight line clear above the arch: deep over the pier, close over the wall, the lights open between+ const topA = [zA - 0.05, yA + 2.5], topB = [zB, yB + 0.72];+ return { under, topA, topB };+}+function buildButtress() {+ const set = mkBatches(), { A, F, G } = set;+ const { hw, zB, zStack, zT } = BUTTRESS, z0 = zaw + 0.9, yStack = 30.6;+ // A solid pier tied into the aisle wall: three stages narrowing and stepping back under sloped caps, each with a real+ // recessed panel, then a slimmer stack at the rear that rises above the aisle roof to take the flyers+ const lower = { k: 0.56, b: 1.5, t: 1.6, r: 0.8 }, flank = { k: 0.56, b: 1.5, t: 1.5, r: 1.1 };+ A.at(M.T(0, 0, z0), () => F.at(M.T(0, 0, z0), () => addPier(set, [+ { y0: 0, y1: 8.4, w: hw, f: zB - z0, panel: lower, sidePanel: flank },+ { y0: 9.6, y1: 15.8, w: hw - 0.08, f: zB - z0 - 0.9, sidePanel: flank },+ { y0: 17, y1: 23.4, w: hw - 0.14, f: zT + 1 - z0, noDrip: true },+ { y0: 23.4, y1: yStack, w: hw - 0.24, f: zStack - z0, backFace: true },+ ], { capH: 1.1 })));+ // Moulded plinth around the base: a closed block the pier rises out of+ F.at(M.T(0, 0, z0), () => { F.box(-hw - 0.16, hw + 0.16, 0, 0.52, -0.2, zB - z0 + 0.16, { noBot: true, s: 0.9 }); F.box(-hw - 0.08, hw + 0.08, 0.52, 0.86, -0.2, zB - z0 + 0.08, { noBot: true, noTop: true, taper: 0.06, taperZ: 0.06 }); });+ set.D.at(M.T(0, 0, zB), () => { addStain(set.D, -hw, hw, 8.2, 0.02, 3.2); addStain(set.D, -hw, hw, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -hw, hw, 5, 0.03, 4.4, 'ground'); });+ // Rain and shadow down both flanks of the pier, below each cap+ for (const s of [-1, 1]) {+ const d1 = zB - z0, d2 = d1 - 0.9;+ set.D.at(M.of(M.T(s * hw, 0, z0 + d1 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.02, 3.2); addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -d1 / 2, d1 / 2, 5, 0.03, 4.8, 'ground'); });+ set.D.at(M.of(M.T(s * (hw - 0.08), 0, z0 + d2 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.02, 2.8); addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.03, 0.7, 'soft'); });+ }+ F.at(M.T(0, 0, zB), () => addGable(F, hw * 1.7, 1.4, 0.02, 0.14, 8.0, { size: 0.3, crockets: 2, finial: 0.5, solid: true, lod: 0 }));+ // Tabernacle of colonnettes at the top of the pier holding an angel+ const yT0 = 23.4, yT1 = 28.4;+ F.box(-hw + 0.1, hw - 0.1, yT0 - 0.02, yT0 + 0.1, zStack - 0.02, zT + 1, { s: 0.95 });+ for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) F.at(M.T(sx * 0.72, 0, zT + sz * 0.72), () => F.lathe([[0.2, yT0], [0.14, yT0 + 0.12], [0.1, yT0 + 0.2], [0.1, yT1 - 0.22], [0.14, yT1 - 0.15], [0.22, yT1]], 8));+ // Canopy: arched, gabled and crowned by the crocketed pinnacle+ for (let i = 0; i < 4; i++) F.at(M.of(M.T(0, 0, zT), M.Ry(i * 90), M.T(0, 0, 0.72)), () => {+ F.sweep(pointedArc(1.3, 1.25, 6).map((p) => [p[0], yT1 - 1.25 + p[1] - 0.02]), traceryBar(0.1, 0.16), {});+ addGable(F, 1.7, 1.25, -0.06, 0.12, yT1, { size: 0.28, crockets: 2, finial: 0.48, lod: 0, solid: true });+ });+ F.box(-0.92, 0.92, yT1 - 0.06, yT1 + 0.18, zT - 0.92, zT + 0.92, { s: 0.9 });+ F.at(M.T(0, yT1 + 0.18, zT), () => addPinnacle(F, 1.42, 8.4, { lod: 1 }));+ // Two flyers, each a true arch: a moulded rib springing from the pier, a thin plate pierced by a row of+ // pointed lights standing on it, and a straight sloped coping with a gabled top shedding the rain+ const fly = (zA, yA, zB, yB, th) => {+ const { under, topA, topB } = flyerShape(zA, yA, zB, yB, th);+ const band = 0.34, upper = under.map((p) => [p[0], p[1] + band]);+ const onTop = (z) => lerp(topA[1], topB[1], (z - topA[0]) / (topB[0] - topA[0]));+ const onRib = (t) => flyerRise(yA, yB, t) + band;+ const holes = [], N = 6;+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.16) / N, t1 = (i + 0.84) / N, z0 = lerp(zA, zB, t0), z1 = lerp(zA, zB, t1), u0 = onRib(t0) + 0.04, u1 = onRib(t1) + 0.04, a0 = onTop(z0) - 0.1, a1 = onTop(z1) - 0.1, h = Math.min(a0 - u0, a1 - u1);+ if (h < 0.22) continue;+ const r = Math.min(h * 0.42, Math.abs(z1 - z0) * 0.8), hole = [[z0, u0], [z1, u1], [z1, a1 - r], [(z0 + z1) / 2, (a0 + a1) / 2], [z0, a0 - r]];+ holes.push(area2(hole) > 0 ? hole : hole.slice().reverse());+ }+ F.at(M.Ry(-90), () => {+ F.slab([...upper, topB, topA], holes, -0.12, 0.12, { sr: 0.7, so: 0.8 });+ F.slab([...under, ...upper.slice().reverse()], [], -0.27, 0.27, {});+ F.sweep(under, rollSection(0.12, 0.34, 1, 0.03, 2), { caps: false });+ });+ const ye = -0.03, yr = 0.17, we = 0.33, a = [topA[1], topA[0]], c = [topB[1], topB[0]];+ F.face([[-we, a[0] + ye, a[1]], [0, a[0] + yr, a[1]], [0, c[0] + yr, c[1]], [-we, c[0] + ye, c[1]]]);+ F.face([[0, a[0] + yr, a[1]], [we, a[0] + ye, a[1]], [we, c[0] + ye, c[1]], [0, c[0] + yr, c[1]]]);+ F.face([[-we, c[0] + ye, c[1]], [we, c[0] + ye, c[1]], [we, a[0] + ye, a[1]], [-we, a[0] + ye, a[1]]]);+ const n = Math.round(Math.hypot(topA[0] - topB[0], topA[1] - topB[1]) / 1.4);+ crocketsAlong(F, [0, topA[1] + 0.14, topA[0]], [0, topB[1] + 0.14, topB[0]], n, 0.5, [0, 1, 0], 0);+ };+ fly(zStack - 0.1, 28.0, zn + 1.12, 35.6, 0.48);+ fly(zStack - 0.1, 22.0, zn + 1.12, 28.6, 0.54);+ // Upper pier on the clerestory wall where the flyers land, with its own pinnacle above the parapet+ const yE = P.eaves;+ A.at(M.T(0, 0, zn + 0.42), () => F.at(M.T(0, 0, zn + 0.42), () => addPier(set, [{ y0: P.aisleRoofHigh - 1, y1: yE + 1.2, w: 0.62, f: 0.72, noDrip: true }], { back: 0, capH: 0.75 })));+ F.at(M.T(0, yE + 1.2, zn + 0.74), () => addPinnacle(F, 0.9, 6.4, { simple: 1 }));+ F.at(M.T(0, 0, zn + 1.14), () => { F.lathe([[0.16, 31.2], [0.1, 31.4], [0.1, 40.5], [0.17, 40.8]], 8); });+ // Interior respond on the aisle wall: a small cluster carrying the aisle vault+ const ringR = lobedRing(0.32, [[0, -0.44, 0.17], [-0.34, -0.26, 0.11], [0.34, -0.26, 0.11]], 26);+ F.at(M.T(0, 0, zaw), () => {+ addExtrudedRing(F, ringR, 0.4, P.pierTop - 0.8, { s0: 0.8 });+ addCapital(F, ringR, P.pierTop - 0.8, 0.8, 1.2, { leaves: 6 });+ F.lathe([[0.62, 0], [0.62, 0.42], [0.42, 0.5]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ });+ // Spout from the aisle gutter+ return set;+}++function addLancetTracery(b, w, y0, ys, rise) { // single-light frame with a cusped head+ const secT = traceryBar(0.13, 0.3), cr = w * 0.32, cy = ys + rise * 0.34, cusp = [];+ for (let i = 0; i <= 12; i++) { const a = rad(-30 + i * 240 / 12), R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, {});+ b.sweep([[w / 2, cy - cr * 0.4], cusp[0]], secT, {});+ b.sweep([[-w / 2, cy - cr * 0.4], cusp[12]], secT, {});+}+function makeLancetGlass(w, y0, ys, rise, seed, fig, o) {+ const g = new Glass(seed);+ g.raw(archOutline(w, y0, ys, rise, 8), 'lead', -0.4, 0);+ gLancetSaint(g, 0, w, y0, ys, rise, fig, o || {});+ return g.b;+}++// ==================================================================================+// PLAN: everything is derived from the parameters at the top of the file.+// ==================================================================================+const LAY = (() => {+ const xW = P.xWest, xTowerE = xW - P.towerDepth, xNaveE = xTowerE - P.naveBays * BAY, xc = xNaveE - zn, xChoirW = xc - zn, xa = xChoirW - P.choirBays * BAY;+ const ridge = P.eaves + (zn + 1) * P.roofPitch;+ return { xW, xTowerE, xNaveE, xc, xChoirW, xa, ridge };+})();+// A rigid module frame: rotation about Y at an origin on the ground+const frame = (th, ox, oz) => {+ const c = Math.cos(rad(th)), s = Math.sin(rad(th));+ return { th, ox, oz, pos: (x, y, z) => [ox + x * c + z * s, y, oz - x * s + z * c], rot: (d = 0) => [0, th + d, 0] };+};+const SAINTS = ['apostle', 'peter', 'paul', 'bishop', 'prophet', 'martyr', 'paul', 'apostle', 'bishop', 'prophet'];++// Baked floor light: dim towards the walls and the corners++function buildVaultNave() {+ const s = mkBatches();+ const crown = addRibVault(s.F, s.A, [[-BAY / 2, -zn + 0.42], [BAY / 2, -zn + 0.42], [BAY / 2, zn - 0.42], [-BAY / 2, zn - 0.42]], P.vaultSpring, P.vaultCrown, { sideRise: (k) => (P.vaultCrown - P.vaultSpring) * (k % 2 === 0 ? 0.86 : 0.99), nS: 10, nT: 8 });+ addBoss(s.G, s.F, crown, 0.55);+ return s;+}++// ---- Chevet: hemicycle, ambulatory and a radiating chapel per sector ----------------+const CHEV = (() => {+ const d = 180 / P.apseSides, h = rad(d / 2), Ra = zn * Math.cos(h), hs = zn * Math.sin(h), Ro = zaw * Math.cos(h), hso = zaw * Math.sin(h);+ const Rc = 2.5, zc = Ro + 3.0;+ return { d, h, Ra, hs, Ro, hso, Rc, zc };+})();+function buildChevetSector() {+ const set = mkBatches(), { A, F, L, G } = set, { Ra, hs, Ro, hso, Rc, zc } = CHEV;+ buildNaveBay({ into: set, part: 'upper', B: 2 * hs, zw: Ra, wedge: true, ww: 1.62, archRise: 5.0, nT: 2, tw: 1.05, cw: 2.05, cRise: 4.3, lights: 1 });+ // Hemicycle vault: one web from this side up to the crown over the apse centre+ const yS = P.vaultSpring, yC = P.vaultCrown;+ addVaultCell(F, [-hs + 0.0, Ra - 0.42], [hs, Ra - 0.42], [0, 0], yS, yC, 9.6, 7, 8);+ const sec = rollSection(0.32, 0.42, 2, 0.04, 2);+ addRib(F, [-hs, Ra - 0.42], [hs, Ra - 0.42], yS, 9.6, sec, 6);+ addRib(F, [-zn * Math.sin(CHEV.h), (zn - 0.42) * Math.cos(CHEV.h)], [0, 0], yS, yC - yS, sec, 9, true);+ // Ambulatory vault (trapezoid)+ const c2 = addRibVault(F, A, [[-hs, Ra + 0.42], [hs, Ra + 0.42], [hso, Ro], [-hso, Ro]], P.pierTop, P.aisleCrown - 0.3, { sideRise: (k) => (k === 0 ? 5.1 : k === 2 ? 5.2 : 5.1), nS: 6, nT: 4, ribSec: rollSection(0.24, 0.32, 2, 0.03, 2) });+ addBoss(G, F, c2, 0.3);+ // Outer wall with the arched chapel mouth+ const yW = P.aisleWallTop, mw = 3.55, mS = 8.6, mR = 4.8;+ const mouth = pointedArc(mw, mR, 8).map((p) => [p[0], p[1] + mS]);+ A.at(M.T(0, 0, Ro + 0.3), () => A.slab([[-hso, 0], [-mw / 2, 0], [-mw / 2, mS], ...mouth, [mw / 2, mS], [mw / 2, 0], [hso, 0], [hso, yW], [-hso, yW]], [], -0.3, 0.3, { noOuterSides: true, sr: 0.7 }));+ // The moulding fills the whole wall thickness: it is also the soffit of the opening+ F.at(M.T(0, 0, Ro + 0.3), () => F.sweep(mouth, rollSection(0.42, 0.96, 2, 0.05, 2), {}));+ set.D.at(M.T(0, 0, Ro + 0.6), () => { addStain(set.D, -hso, hso, yW - 0.42, 0.02, 3.4); addStain(set.D, -hso, hso, yW - 0.42, 0.03, 0.8, 'soft'); });+ F.at(M.T(0, 0, Ro + 0.6), () => { F.sweep([[-hso, yW - 0.12], [hso, yW - 0.12]], stringSection(0.3, 0.3), {}); addParapet(F, -hso, hso, yW, 1.0, 0.24, { pitch: 1.1 }); });+ A.face([[-hso, yW, Ro + 0.6], [hso, yW, Ro + 0.6], [hso, yW, Ro], [-hso, yW, Ro]], 0.7);+ // Ambulatory lean-to roof+ const zR0 = Ra + 0.42, hsR0 = (zR0 / Ra) * hs, zR1 = Ro + 0.9, hsR1 = (zR1 / Ro) * hso;+ L.face([[-hsR0, P.aisleRoofHigh, zR0], [-hsR1, yW - 0.2, zR1], [hsR1, yW - 0.2, zR1], [hsR0, P.aisleRoofHigh, zR0]]);+ // Hipped roof over the hemicycle+ const yE = P.eaves + 0.15, Re = zn + 0.72, hRe = Re * Math.sin(CHEV.h), aRe = Re * Math.cos(CHEV.h);+ L.grid((u, v) => [lerp(-hRe, hRe, u) * (1 - v), lerp(yE, LAY.ridge, v), aRe * (1 - v)], 2, 2, { flip: false, metric: true });+ // Radiating chapel: five faceted walls, each with a tall lancet, rib vault, conical lead roof+ const nF = P.chapelFacets, span = 216, yC1 = 16.2, dF = span / nF, ap = Rc * Math.cos(rad(dF / 2)), halfF = Rc * Math.sin(rad(dF / 2)), lw = 1.3, sill = 3.2, lSp = 9.9, lR = 2.5;+ const verts = [];+ for (let i = 0; i <= nF; i++) { const f = rad(-span / 2 + i * dF); verts.push([Rc * Math.sin(f), zc + Rc * Math.cos(f)]); }+ for (let i = 0; i < nF; i++) {+ const fm = -span / 2 + (i + 0.5) * dF;+ const m = M.of(M.T(0, 0, zc), M.Ry(fm), M.T(0, 0, ap));+ A.at(m, () => A.slab([[-halfF, 0], [halfF, 0], [halfF, yC1], [-halfF, yC1]], [archOutline(lw, sill, lSp, lR, 7)], 0, 0.5, { noOuterSides: true, sr: 0.68 }));+ F.at(m, () => {+ addLancetTracery(F, lw, sill, lSp, lR);+ F.sweep(archOutline(lw, sill, lSp, lR, 5), hexBar(0.12, 0.56), { closed: true, z: 0.25, s: 0.9, crease: 40 });+ });+ // Weather on the chapel walls: streaks and shadow under the cornice and the sill, damp at the foot+ set.D.at(M.of(m, M.T(0, 0, 0.5)), () => {+ addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.02, 3); addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.03, 0.7, 'soft');+ addStain(set.D, -halfF, halfF, sill - 0.3, 0.02, 2, 'drip'); addStain(set.D, -halfF, halfF, 3, 0.03, 3, 'ground');+ });+ F.at(M.of(m, M.T(0, 0, 0.5)), () => {+ F.sweep([[-halfF - 0.1, sill - 0.14], [halfF + 0.1, sill - 0.14]], stringSection(0.24, 0.2), { caps: false });+ F.sweep([[-halfF - 0.1, 0.5], [halfF + 0.1, 0.5]], stringSection(0.36, 0.3), { caps: false });+ F.sweep([[-halfF - 0.1, yC1 - 0.1], [halfF + 0.1, yC1 - 0.1]], stringSection(0.3, 0.3), { caps: false });+ addGable(F, lw + 0.9, 2.4, 0.01, 0.14, lSp + 2.15, { size: 0.32, crockets: 2, finial: 0.56, lod: 0 });+ addParapet(F, -halfF, halfF, yC1, 0.82, 0.18, { pitch: 1.3, nh: 8 });+ });+ addVaultCell(F, verts[i], verts[i + 1], [0, zc], 9.2, 14.6, 3.6, 5, 4);+ addRib(F, verts[i], verts[i + 1], 9.2, 3.6, rollSection(0.16, 0.22, 1, 0.02, 2), 5);+ }+ addVaultCell(F, verts[nF], verts[0], [0, zc], 9.2, 14.6, 4.2, 5, 4);+ for (let i = 0; i <= nF; i++) {+ addRib(F, verts[i], [0, zc], 9.2, 5.4, rollSection(0.17, 0.24, 1, 0.02, 2), 6, true);+ // Exterior angle buttress and little pinnacle; interior wall shaft+ const f = -span / 2 + i * dF, ve = verts[i];+ F.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => {+ A.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => addPier(set, [{ y0: 0, y1: 8.8, w: 0.3, f: 1.05 }, { y0: 9.7, y1: 14.2, w: 0.25, f: 0.72, noDrip: true }], { back: 0.1, flat: true })); // F is already in this frame+ F.at(M.T(0, 14.18, 0.62), () => addPinnacle(F, 0.62, 3.9, { simple: 1 }));+ F.lathe([[0.17, 0], [0.09, 0.6], [0.09, 9.1], [0.17, 9.2]], 5);+ });+ }+ // The neck joining the chapel to the ambulatory: side walls, a pointed barrel vault, a lead cap+ const zN0 = Ro + 0.6, zN1 = verts[0][1], hN0 = mw / 2, hN1 = Math.abs(verts[0][0]);+ for (const s of [-1, 1]) A.prism([[s * hN0, zN0 - 0.05], [s * hN1, zN1 + 0.05], [s * (hN1 + 0.5), zN1 + 0.05], [s * (hN0 + 0.5), zN0 - 0.05]], 0, yC1);+ // Same subdivision as the chapel's last web so the seam has no cracks; a rib covers the joint+ A.grid((u, v) => { const hw = lerp(hN0, hN1, v); return [lerp(-hw, hw, u), 9.2 + arcH(2 * hw, 4.2, u), lerp(Ro + 0.3, zN1 + 0.02, v), 0.8]; }, 5, 1, { flip: false }); // starts under the arch moulding, so no ray can pass above it+ addRib(F, verts[nF], verts[0], 9.2, 4.2, rollSection(0.16, 0.26, 1, 0.02, 2), 5);+ L.face([[-(hN0 + 0.5), yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN0 - 0.1], [-(hN0 + 0.5), yC1 + 0.03, zN0 - 0.1]]);+ // Lead cone+ L.at(M.T(0, 0, zc), () => { const n = 10, rb = Rc + 0.5; L.grid((u, v) => { const a = -PI + TAU * u, r = rb * (1 - v), y = lerp(yC1 + 0.05, yC1 + 7.4, v); return [r * Math.sin(a), y, r * Math.cos(a)]; }, n, 2, { flip: false, wrapU: true }); });+ G.at(M.T(0, yC1 + 7.3, zc), () => addFinial(G, 1.25, 1));+ return set;+}++// ---- Lead roofs with metric UVs so the seams run down the slope ----------------------+function addRoofRun(L, x0, x1, half, yE, yR) {+ const slope = Math.hypot(half, yR - yE);+ for (const s of [-1, 1]) {+ const a = L.v(x0, yE, s * half, 0, half, s * (yR - yE), x0, 0, 1), b = L.v(x1, yE, s * half, 0, half, s * (yR - yE), x1, 0, 1);+ const c = L.v(x1, yR, 0, 0, half, s * (yR - yE), x1, slope, 1), d = L.v(x0, yR, 0, 0, half, s * (yR - yE), x0, slope, 1);+ if (s > 0) L.quad(a, b, c, d); else L.quad(b, a, d, c);+ // A raised roll every second seam, running from the eaves to the ridge: a low ridge of two faces standing on the sheet+ const ny = half / slope, nz = s * (yR - yE) / slope, h = 0.08, w = 0.055;+ for (let x = Math.ceil(x0 / 2) * 2; x < x1; x += 2) {+ const A0 = [x - w, yE, s * half], B0 = [x + w, yE, s * half], A1 = [x - w, yR, 0], B1 = [x + w, yR, 0], T0 = [x, yE + ny * h, s * half + nz * h], T1 = [x, yR + ny * h, nz * h];+ if (s > 0) { L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]); } else { L.face([A1, T1, T0, A0]); L.face([T1, B1, B0, T0]); }+ }+ }+}+function buildCresting(len) { // gilded fleur-de-lis cresting for a length of ridge+ const G = new Batch(TILE.gold), n = Math.round(len / 0.9);+ G.box(-len / 2, len / 2, -0.04, 0.05, -0.05, 0.05, {});+ for (let i = 0; i < n; i++) {+ const x = -len / 2 + (i + 0.5) * len / n;+ G.at(M.T(x, 0.05, 0), () => {+ G.tube([[0, 0, 0], [0, 0.34, 0], [0, 0.62, 0]], (t) => [lerp(0.05, 0.005, t), lerp(0.035, 0.005, t)], 4);+ for (const s of [-1, 1]) G.tube(spline([[0, 0.14, 0], [s * 0.15, 0.26, 0], [s * 0.2, 0.42, 0], [s * 0.1, 0.46, 0]], 1), (t) => lerp(0.035, 0.008, t), 4);+ G.box(-0.08, 0.08, 0.12, 0.17, -0.03, 0.03, {});+ });+ }+ return G;+}++// ---- Crossing: four great piers, four arches, a gilded boss, the fleche above ----------+function buildCrossingPier() {+ const set = mkBatches(), { F } = set;+ const core = 1.08, sh = [];+ for (let i = 0; i < 8; i++) { const a = i * PI / 4; sh.push([(core + 0.12) * Math.cos(a), (core + 0.12) * Math.sin(a), i % 2 ? 0.2 : 0.3]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; sh.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), 0.13]); }+ const ring = lobedRing(core, sh, 80), yC = P.vaultSpring - 1.1;+ F.lathe([[2.05, 0], [2.05, 0.6], [1.9, 0.68]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.68, 0.94, { k0: 1.2, k1: 1.23 });+ addExtrudedRing(F, ring, 0.94, 1.25, { k0: 1.23, k1: 1, s0: 0.75, s1: 0.75 });+ addExtrudedRing(F, ring, 1.25, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.1, 1.14, { leaves: 16 });+ return set;+}+function buildAislePier() {+ const set = mkBatches(), { F } = set;+ const sh = []; for (let i = 0; i < 4; i++) { const a = i * PI / 2; sh.push([0.62 * Math.cos(a), 0.62 * Math.sin(a), 0.2]); }+ const ring = lobedRing(0.54, sh, 40);+ F.lathe([[1.15, 0], [1.15, 0.5], [1.02, 0.56]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.56, 0.86, { k0: 1.28, k1: 1, s0: 0.75 });+ addExtrudedRing(F, ring, 0.86, P.pierTop - 0.9, { s0: 0.8 });+ addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });+ return set;+}+// The spire over the crossing: an oak frame sheathed in lead. A plinth straddles the roofs, two open lantern stages stand on it,+// and a long octagonal needle rises to a gilded orb and cross. Everything follows P.flecheTip, the ridge and the width of the crossing.+function buildFleche(parent, x, z) {+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold);+ L.noReduce = true; // a single careful mesh, its seams and crockets are the design+ const n = 8, PH = PI / 8, cos8 = Math.cos(PH), sin8 = Math.sin(PH), oct = { faceted: true, phase: PH };+ const yR = LAY.ridge, tip = P.flecheTip, y0 = yR - 5.9, yP = yR + 2.7, H = tip - yP, yA = yP + 0.19 * H, yB = yA + 0.15 * H, yN1 = yB + 1.7, yT = tip - 3.3;+ const R0 = zn * 0.73, R1 = zn * 0.66, R2 = zn * 0.5, RN = zn * 0.44;+ const faceFrame = (k, ap) => M.of(M.Ry(k * 45), M.T(0, 0, ap));+ const vertex = (k, R) => { const a = rad((k + 0.5) * 45); return [R * Math.sin(a), R * Math.cos(a)]; };+ // A small gilded knop finishing a gablet or a lucarne+ const knop = (m, s) => G.at(m, () => G.lathe([[0.05 * s, 0], [0.11 * s, 0.1 * s], [0.09 * s, 0.2 * s], [0.03 * s, 0.32 * s], [0.004, 0.72 * s]], 6));+ const gablet = (k, ap, w, h, y, nC, sz) => {+ const fr = faceFrame(k, ap + 0.05);+ L.at(fr, () => {+ L.slab([[-w / 2, y], [w / 2, y], [0, y + h]], [], -0.05, 0.05, { sb: 0.7 });+ L.shade(0.42, () => L.face([[-w * 0.2, y + h * 0.16, 0.06], [w * 0.2, y + h * 0.16, 0.06], [0, y + h * 0.62, 0.06]]));+ const rake = barSection(0.14, 0.2);+ L.sweep([[-w / 2 - 0.05, y - 0.02], [0, y + h + 0.04]], rake, {}); L.sweep([[0, y + h + 0.04], [w / 2 + 0.05, y - 0.02]], rake, {});+ crocketsAlong(L, [-w / 2, y + 0.05, 0], [-0.03, y + h * 0.97, 0], nC, sz, [-h, w / 2, 0], 0, addLeadCrocket);+ crocketsAlong(L, [w / 2, y + 0.05, 0], [0.03, y + h * 0.97, 0], nC, sz, [h, w / 2, 0], 0, addLeadCrocket);+ });+ knop(M.of(fr, M.T(0, y + h - 0.02, 0)), 0.9);+ };+ // One open stage: a dark core, a pointed opening in every face, a clustered post at every angle, a cornice closing the top+ const stage = (R, yBase, yTop, coreR, rPost, open) => {+ const hs = R * sin8, ap = R * cos8, { w, sill, ys, rise, tracery } = open;+ L.shade(0.34, () => L.lathe([[coreR, yBase], [coreR, yTop - 0.4]], n, oct));+ L.shade(0.5, () => { L.lathe([[coreR, yTop - 0.4], [R, yTop - 0.4]], n, oct); L.lathe([[R, yBase + 0.01], [coreR, yBase + 0.01]], n, oct); });+ for (let k = 0; k < n; k++) {+ L.at(faceFrame(k, ap), () => {+ L.slab([[-hs, yBase], [hs, yBase], [hs, yTop], [-hs, yTop]], [archOutline(w, sill, ys, rise, 5)], -0.14, 0.02, { noOuterSides: true, sr: 0.6, sb: 0.5 });+ L.sweep(pointedArc(w + 0.16, rise + 0.16, 5).map((p) => [p[0], p[1] + ys]), hexBar(0.1, 0.14), { z: 0.03, caps: false });+ if (tracery) {+ L.at(M.T(0, 0, -0.06), () => L.lathe([[0.1, sill], [0.065, sill + 0.16], [0.065, ys - 0.08], [0.11, ys]], 5));+ for (const s of [-1, 1]) L.sweep(pointedArc(w / 2 - 0.03, rise * 0.58, 3).map((p) => [s * w / 4 + p[0], ys + p[1]]), hexBar(0.08, 0.13), { z: -0.06, caps: false });+ L.sweep(ringPts(0, ys + rise * 0.76, rise * 0.13, 6), hexBar(0.06, 0.1), { z: -0.06, closed: true });+ }+ });+ const [px, pz] = vertex(k, R + 0.03);+ L.at(M.T(px, 0, pz), () => L.lathe([[rPost * 1.35, yBase], [rPost, yBase + 0.22], [rPost, yTop - 0.3], [rPost * 1.35, yTop - 0.12], [rPost * 1.2, yTop]], 6));+ }+ L.lathe([[R + 0.02, yTop - 0.5], [R + 0.22, yTop - 0.3], [R + 0.22, yTop - 0.12], [R + 0.08, yTop]], n, oct);+ };+ // A raised roll standing on a sloping face, in the face's own frame: two faces, a low ridge+ const roll = (u, ya, yb, apF, h = 0.07, w = 0.05) => {+ const za = apF(ya), zb = apF(yb), dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz) || 1, ny = -dz / l, nz = dy / l;+ const A0 = [u - w, ya, za], B0 = [u + w, ya, za], A1 = [u - w, yb, zb], B1 = [u + w, yb, zb], T0 = [u, ya + ny * h, za + nz * h], T1 = [u, yb + ny * h, zb + nz * h];+ L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]);+ };++ // ---- The plinth: batters up out of the slates of all four roofs, its foot well below their surface all round ----+ L.lathe([[R0, y0], [R0 - 0.16, yP - 0.62], [R0 + 0.12, yP - 0.46], [R0 + 0.12, yP - 0.2], [R0 - 0.2, yP], [R1 * 0.6, yP]], n, oct);+ { const apF = (y) => lerp(R0, R0 - 0.16, (y - y0) / (yP - 0.62 - y0)) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => { for (const u of [-1.2, 0, 1.2]) roll(u, y0, yP - 0.62, apF); }); }++ // ---- First stage: tall two-light openings, a gablet on every face, a pinnacle at every angle, a lead pentice behind them ----+ stage(R1, yP, yA, R1 * 0.6, 0.26, { w: R1 * 0.56, sill: yP + 0.95, ys: yA - 3.35, rise: 2, tracery: true });+ L.lathe([[R1 + 0.08, yA], [R2 + 0.02, yA + 1.55]], n, oct);+ for (let k = 0; k < n; k++) {+ gablet(k, R1 * cos8, R1 * 0.67, 2.6, yA - 0.3, 2, 0.34);+ const [px, pz] = vertex(k, R1 + 0.02);+ L.at(M.of(M.T(px, yA - 0.2, pz), M.Ry((k + 0.5) * 45)), () => addPinnacle(L, 0.62, 5, { simple: true, lod: 0 }));+ }+ // ---- Second stage: slimmer, single lancets ----+ stage(R2, yA + 1.5, yB, R2 * 0.62, 0.18, { w: R2 * 0.53, sill: yA + 2.45, ys: yB - 2.3, rise: 1.5, tracery: false });+ for (let k = 0; k < n; k++) gablet(k, R2 * cos8, R2 * 0.65, 1.9, yB - 0.25, 1, 0.3);++ // ---- The needle: a flared foot, then eight long faces. u stays metric, so the sheets run straight up and are cut by the arrises as they close ----+ const rTip = 0.09, RF = R2 + 0.08, rAt = (y) => (y <= yN1 ? lerp(RF, RN, (y - yB) / (yN1 - yB)) : lerp(RN, rTip, (y - yN1) / (yT - yN1)));+ const levels = [yB, yN1]; for (const t of [0.22, 0.5, 0.75, 1]) levels.push(lerp(yN1, yT, t));+ const apAt = (y) => rAt(y) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => {+ let vv = 0;+ for (let j = 0; j < levels.length - 1; j++) {+ const ya = levels[j], yb = levels[j + 1], za = apAt(ya), zb = apAt(yb), wa = rAt(ya) * sin8, wb = rAt(yb) * sin8, dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz), ny = -dz / l, nz = dy / l;+ const a = L.v(-wa, ya, za, 0, ny, nz, -wa, vv), b = L.v(wa, ya, za, 0, ny, nz, wa, vv), c = L.v(wb, yb, zb, 0, ny, nz, wb, vv + l), d = L.v(-wb, yb, zb, 0, ny, nz, -wb, vv + l);+ L.quad(a, b, c, d); vv += l;+ }+ // Standing seams, each stopping where the closing arrises overtake it+ for (const u of [-0.55, 0, 0.55]) { const rEnd = (Math.abs(u) + 0.1) / sin8; if (rEnd < RN) roll(u, yN1, lerp(yN1, yT, (RN - rEnd) / (RN - rTip)), apAt); }+ });+ // Rolled arrises from the flare to the tip, crockets thinning as they climb+ for (let k = 0; k < n; k++) {+ const vAt = (y) => { const [vx, vz] = vertex(k, rAt(y)); return [vx, y, vz]; };+ L.tube([vAt(yB), vAt(yN1), vAt(yT - 0.05)], (t) => lerp(0.12, 0.04, t), 4, { capEnd: false });+ const [ox, oz] = vertex(k, 1), nC = 11, y1 = yN1 + 1.2, y2 = yT - 1.5;+ for (let i = 0; i < nC; i++) {+ const t = (i + 0.5) / nC, ya = lerp(y1, y2, t - 0.02), yb = lerp(y1, y2, t + 0.02);+ crocketsAlong(L, vAt(ya), vAt(yb), 1, lerp(1.2, 0.42, t), [ox, 0.12, oz], 0, addLeadCrocket);+ }+ }+ // Tie-bands round the needle where the sheets are lapped+ for (const t of [0.22, 0.5, 0.75]) { const y = lerp(yN1, yT, t), r = rAt(y); L.lathe([[rAt(y - 0.1), y - 0.1], [r + 0.08, y - 0.05], [r + 0.08, y + 0.05], [rAt(y + 0.12), y + 0.12]], n, oct); }+ // Two tiers of lucarnes on alternate faces+ const lucarne = (k, yD, a, c, g) => {+ const zf = apAt(yD) + 0.06, yE2 = yD + c, yG = yE2 + g, zb0 = apAt(yD), zbE = apAt(yE2), zbG = apAt(yG);+ L.at(faceFrame(k, 0), () => {+ L.face([[-a, yD, zf], [a, yD, zf], [a, yE2, zf], [0, yG, zf], [-a, yE2, zf]]);+ L.shade(0.22, () => L.face([[-a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yE2, zf + 0.012], [0, yE2 + g * 0.55, zf + 0.012], [-a * 0.5, yE2, zf + 0.012]]));+ L.face([[a, yD, zb0], [a, yE2, zbE], [a, yE2, zf], [a, yD, zf]]);+ L.face([[-a, yD, zb0], [-a, yD, zf], [-a, yE2, zf], [-a, yE2, zbE]]);+ L.face([[a, yE2, zf], [a, yE2, zbE], [0, yG, zbG], [0, yG, zf]]);+ L.face([[-a, yE2, zf], [0, yG, zf], [0, yG, zbG], [-a, yE2, zbE]]);+ });+ knop(M.of(faceFrame(k, zf), M.T(0, yG - 0.02, 0)), 0.8);+ };+ for (let k = 0; k < n; k++) { if (k % 2 === 0) lucarne(k, yN1 + 5, 0.5, 1.25, 0.75); else lucarne(k, yN1 + 12, 0.36, 0.95, 0.55); }++ // ---- The tip: a gilded collar, an orb and a cross, its head at exactly P.flecheTip ----+ G.at(M.T(0, yT - 0.25, 0), () => {+ G.lathe([[0.12, 0], [0.24, 0.12], [0.22, 0.22], [0.13, 0.3], [0.11, 0.44], [0.2, 0.52], [0.36, 0.68], [0.42, 0.9], [0.36, 1.12], [0.2, 1.28], [0.08, 1.36], [0.06, 2.2]], 10);+ G.box(-0.07, 0.07, 2.2, 3.55, -0.07, 0.07, {});+ G.box(-0.62, 0.62, 2.82, 2.98, -0.07, 0.07, {});+ });+ place('CrossingSpire_Lead', L, MAT.lead, { pos: [x, 0, z], parent });+ place('CrossingSpire_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });+}++function storyWindows() { // the narrative aisle windows+ const sets = [+ [['annunciation', 'nativity', 'magi', 'shepherd', 'supper'], ['crucifixion', 'resurrection', 'david', 'noah', 'annunciation']],+ [['noah', 'david', 'annunciation', 'nativity', 'magi'], ['shepherd', 'supper', 'crucifixion', 'resurrection', 'david']],+ [['magi', 'supper', 'noah', 'crucifixion', 'shepherd'], ['nativity', 'david', 'resurrection', 'annunciation', 'supper']],+ [['resurrection', 'shepherd', 'crucifixion', 'david', 'nativity'], ['supper', 'annunciation', 'magi', 'noah', 'resurrection']],+ ];+ const grounds = [['ruby', 'cobalt'], ['cobalt', 'ruby'], ['ruby', 'cobalt'], ['cobalt', 'ruby']];+ return sets.slice(0, P.storyWindows).map((s, n) => makeWindowGlass(...windowArgs.aisle, 40 + n, {+ trackPanes: true,+ light: (g, x, w, y0, ys, r, i) => gLancetStory(g, x, w, y0, ys, r, s[i].slice(0, 4), { c1: grounds[n][0], c2: grounds[n][1], g1: grounds[(n + 1) % 4][1], g2: grounds[n][0], cell: 0.86 }),+ oculus: { ground: grounds[n][1], inner: grounds[n][0] },+ }));+}+const CLERE_SAINTS = [+ [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'book' }, { robe: 'emerald', mantle: 'gold', crown: 1, beard: 2, attr: 'cross', arm: 'point', halo: false }],+ [{ robe: 'pale', mantle: 'cobalt', veil: 'azure', arm: 'pray' }, { robe: 'violet', mantle: 'white', mitre: 'white', attr: 'staff', arm: 'up' }],+ [{ robe: 'gold', mantle: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'purple', beard: 1, attr: 'sword', arm: 'point' }],+ [{ robe: 'rubyLt', mantle: 'teal', crown: 1, attr: 'lily', arm: 'point', halo: false }, { robe: 'white', mantle: 'rubyLt', arm: 'orant', wings: 'gold' }],+];+function clereWindows() {+ return CLERE_SAINTS.slice(0, P.clerestoryWindows).map((pair, n) => makeWindowGlass(...windowArgs.clere, 60 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetSaint(g, x, w, y0, ys, r, pair[i], (i + n) % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }),+ oculus: { bust: { robe: 'rubyLt', beard: 1, cross: 1 }, ground: 'cobalt', inner: 'gold' },+ }));+}++function buildBody() {+ const { xTowerE, xNaveE, xc, xChoirW, xa, ridge } = LAY;+ const gNave = group('Nave'), gChoir = group('Choir'), gTN = group('Transept_North'), gTS = group('Transept_South'), gX = group('Crossing'), gApse = group('Apse_Chevet'), gRoof = group('Roofs');+ // The upper elevation is shared by every straight bay; aisles come in full and open-corner versions+ const bayUp = buildNaveBay({ part: 'upper' }), aisleFull = buildNaveBay({ part: 'aisle' }), aisleOpen = buildNaveBay({ part: 'aisle', open: true });+ const vault = buildVaultNave(), pier = buildPier(), but = buildButtress();+ pier.F.pierShaft = true; // the window light tests these exactly rather than voxel by voxel, so their shadows come out clean+ const stories = storyWindows(), cleres = clereWindows();+ SHARED.stories = stories; SHARED.cleres = cleres;+ let counter = 0;+ const statue = (fr, lx, y, lz, kind, faceDeg, scale, parent, indoor = true) => placeFigure('Statue_' + kind + '_' + (counter++), kind, { pos: fr.pos(lx, y, lz), rot: fr.rot(faceDeg), scale, parent, indoor, lod: 0 });+ const garg = (fr, lx, y, lz, k, sc, parent) => place('Gargoyle_' + (counter++), gargoyle(k), MAT.fine, { pos: fr.pos(lx, y, lz), rot: fr.rot(-90), scale: [sc, sc, sc], parent });++ // One side of a straight bay: module, vault-side glass, statues, gargoyles+ const placeBay = (name, fr, parent, o = {}) => {+ placeSet(name + '_Upper', bayUp, [fr.ox, 0, fr.oz], fr.th, parent);+ if (!o.noVault) placeSet(name + '_Aisle', o.open ? aisleOpen : aisleFull, [fr.ox, 0, fr.oz], fr.th, parent);+ const c = cleres[(o.i || 0) % cleres.length];+ placeGlass(name + '_Clerestory', c, fr.pos(0, 0, zn), fr.th + 180, parent);+ if (!o.open) {+ placeGlass(name + '_AisleWindow', stories[(o.i || 0) % stories.length], fr.pos(0, 0, zaw + 0.45), fr.th + 180, parent);+ if (o.sunLit) SUN_WINDOWS.push({ x: fr.ox, batch: stories[(o.i || 0) % stories.length] });+ if ((o.i || 0) % 2 === 0) statue(fr, 0, 0.7, zaw - 0.3, SAINTS[(o.i || 0) % SAINTS.length], 90, 0.86, parent);+ if (!o.inTower) garg(fr, 0, 22.5, zaw + 1.05, (o.i || 0) % 3, 0.72, parent);+ }+ if (!o.noUpperGarg) garg(fr, 0, P.eaves - 0.2, zn + 0.62, ((o.i || 0) + 1) % 3, 0.66, parent);+ };+ const placeButtress = (name, fr, parent, i) => {+ placeSet(name, but, [fr.ox, 0, fr.oz], fr.th, parent);+ statue(fr, 0, 23.5, BUTTRESS.zT, 'angel', -90, 1.9, parent, false);+ garg(fr, 0, 15.2, BUTTRESS.zB - 1.05, i % 3, 1.0, parent);+ };+ const placePier = (name, lfr, parent, kind) => {+ placeSet(name, pier, [lfr.ox, 0, lfr.oz], lfr.th, parent);+ if (kind) statue(lfr, 0, 5.78, -1.14, kind, 90, 1.15, parent);+ };++ // ---- Two more bays carry the nave between the towers to the inner face of the west wall ----+ for (let t = 0; t < 2; t++) {+ const x = xTowerE + (t + 0.5) * BAY;+ placeBay('TowerBay' + t + '_N', frame(0, x, 0), gNave, { i: t + 2, noUpperGarg: true, inTower: true });+ placeBay('TowerBay' + t + '_S', frame(180, x, 0), gNave, { i: t, noUpperGarg: true, inTower: true });+ placeSet('TowerVault' + t, vault, [x, 0, 0], 0, gNave);+ if (t === 0) { placePier('TowerPier_N', frame(0, x + BAY / 2, zn), gNave, SAINTS[6]); placePier('TowerPier_S', frame(180, x + BAY / 2, -zn), gNave, SAINTS[7]); }+ }+ // ---- Nave ----+ for (let i = 0; i < P.naveBays; i++) {+ const x = xTowerE - (i + 0.5) * BAY, isOpen = i === P.naveBays - 1;+ placeBay('NaveBay' + i + '_N', frame(0, x, 0), gNave, { i, open: isOpen });+ placeBay('NaveBay' + i + '_S', frame(180, x, 0), gNave, { i: i + 2, open: isOpen, sunLit: true });+ placeSet('NaveVault' + i, vault, [x, 0, 0], 0, gNave);+ }+ for (let k = 0; k <= P.naveBays - 1; k++) {+ const x = xTowerE - k * BAY;+ placePier('NavePier' + k + '_N', frame(0, x, zn), gNave, k ? SAINTS[k % SAINTS.length] : null);+ placePier('NavePier' + k + '_S', frame(180, x, -zn), gNave, k ? SAINTS[(k + 3) % SAINTS.length] : null);+ if (k >= 1 && k < P.naveBays - 1) { placeButtress('NaveButtress' + k + '_N', frame(0, x, 0), gNave, k); placeButtress('NaveButtress' + k + '_S', frame(180, x, 0), gNave, k + 1); }+ }+ // ---- Choir ----+ for (let j = 0; j < P.choirBays; j++) {+ const x = xChoirW - (j + 0.5) * BAY, isOpen = j === 0;+ placeBay('ChoirBay' + j + '_N', frame(0, x, 0), gChoir, { i: j + 1, open: isOpen });+ placeBay('ChoirBay' + j + '_S', frame(180, x, 0), gChoir, { i: j + 3, open: isOpen });+ placeSet('ChoirVault' + j, vault, [x, 0, 0], 0, gChoir);+ }+ for (let k = 1; k <= P.choirBays; k++) {+ const x = xChoirW - k * BAY;+ placePier('ChoirPier' + k + '_N', frame(0, x, zn), gChoir, SAINTS[(k + 5) % SAINTS.length]);+ placePier('ChoirPier' + k + '_S', frame(180, x, -zn), gChoir, SAINTS[(k + 1) % SAINTS.length]);+ placeButtress('ChoirButtress' + k + '_N', frame(0, x, 0), gChoir, k);+ placeButtress('ChoirButtress' + k + '_S', frame(180, x, 0), gChoir, k + 1);+ }+ // ---- Transept arms: the same bay, turned through 90 degrees ----+ for (const s of [1, -1]) {+ const gT = s > 0 ? gTN : gTS, tag = s > 0 ? 'N' : 'S';+ for (let q = 0; q < P.transeptBays + 1; q++) {+ const zc = s * (zn + (q + 0.5) * BAY), isOpen = q === 0;+ placeBay('Transept' + tag + q + '_W', frame(90 * s, xc, zc), gT, { i: q + 1, open: isOpen, noVault: isOpen });+ placeBay('Transept' + tag + q + '_E', frame(-90 * s, xc, zc), gT, { i: q + 3, open: isOpen, noVault: isOpen });+ placeSet('TranseptVault' + tag + q, vault, [xc, 0, zc], 90, gT);+ }+ for (let q = 1; q <= P.transeptBays; q++) { // the last bay ends in the front wall, which needs no free pier+ const zz = s * (zn + q * BAY);+ placePier('TranseptPier' + tag + q + '_W', frame(90, xc + zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 2) % SAINTS.length] : null);+ placePier('TranseptPier' + tag + q + '_E', frame(-90, xc - zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 4) % SAINTS.length] : null);+ if (q === P.transeptBays) { placeButtress('TranseptButtress' + tag + '_W', frame(90 * s, xc, zz), gT, q); placeButtress('TranseptButtress' + tag + '_E', frame(-90 * s, xc, zz), gT, q + 1); }+ }+ }+ // ---- Crossing ----+ const xPier = buildCrossingPier(), aPier = buildAislePier(), xVault = mkBatches();+ xPier.F.pierShaft = true;+ const sp = zn - 0.3;+ const crossCrown = addRibVault(xVault.F, xVault.A, [[-sp, -sp], [sp, -sp], [sp, sp], [-sp, sp]], P.vaultSpring, P.vaultCrown + 0.5, { sideRise: () => 12.9, nS: 12, nT: 10 });+ addBoss(xVault.G, xVault.F, crossCrown, 0.95);+ // Great transverse arches and the walls above them+ const arch = pointedArc(2 * (zn - 0.5), 12.9, 7);+ for (let i = 0; i < 4; i++) xVault.F.at(M.of(M.Ry(i * 90), M.T(0, P.vaultSpring, zn - 0.05)), () => xVault.F.sweep(arch, rollSection(0.9, 1.2, 2, 0.09, 3), { caps: false }));+ placeSet('CrossingVault', xVault, [xc, 0, 0], 0, gX);+ for (const [a, b] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {+ placeSet('CrossingPier', xPier, [xc + a * zn, 0, b * zn], 0, gX);+ placeSet('AngleAislePier', aPier, [xc + a * zaw, 0, b * zaw], 0, gX);+ }+ // ---- Chevet ----+ const sector = buildChevetSector(), apsePier = buildApsePier();+ const apseSaints = [{ robe: 'gold', mantle: 'cobalt', crown: 1, veil: 'pale', arm: 'pray' }, { robe: 'white', mantle: 'ruby', beard: 2, attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'emerald', beard: 1, attr: 'book' }, { robe: 'white', mantle: 'rubyLt', beard: 1, cross: 1, arm: 'up' }];+ const hemi = apseSaints.slice(0, 3).map((f, i) => makeLancetGlass(2.05, P.triforiumTop + 0.45, P.vaultSpring + 4.8, 4.3, 70 + i, f, i % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }));+ const chapelGlass = ['annunciation', 'crucifixion'].map((nm, i) => { const g = new Glass(80 + i); g.raw(archOutline(1.3, 3.2, 9.9, 2.5, 7), 'lead', -0.4, 0); gLancetStory(g, 0, 1.3, 3.2, 9.9, 2.5, [nm, ['supper', 'magi'][i], ['magi', 'resurrection'][i]], { cell: 0.6, c1: i ? 'cobalt' : 'ruby', c2: i ? 'ruby' : 'cobalt' }); return g.b; });+ for (let k = 0; k < P.apseSides; k++) {+ const th = -(k + 0.5) * CHEV.d, fr = frame(th, xa, 0), gS = group('ChevetSector' + k, gApse);+ placeSet('Sector' + k, sector, [xa, 0, 0], th, gS);+ placeGlass('HemicycleWindow' + k, hemi[(k + (k > 3 ? 1 : 0)) % hemi.length], fr.pos(0, 0, CHEV.Ra), th + 180, gS);+ const nF = P.chapelFacets, dF = 216 / nF, ap = CHEV.Rc * Math.cos(rad(dF / 2));+ for (let i = 0; i < nF; i++) {+ const fm = -108 + (i + 0.5) * dF, lx = ap * Math.sin(rad(fm)), lz = CHEV.zc + ap * Math.cos(rad(fm));+ placeGlass('ChapelWindow' + k + '_' + i, chapelGlass[(i + k) % chapelGlass.length], fr.pos(lx, 0, lz + 0.0), th + fm + 180, gS);+ }+ statue(fr, 0, 0.5, CHEV.zc + CHEV.Rc * 0.55, k === 3 ? 'virgin' : SAINTS[k], 90, 0.92, gS);+ garg(frame(th - 10, xa, 0), 0, 15.5, CHEV.zc + 1.2, k % 3, 0.7, gS);+ if (k >= 1) {+ const bt = -k * CHEV.d;+ placeButtress('ChevetButtress' + k, frame(bt, xa, 0), gS, k);+ // A slim clustered pier at each angle of the hemicycle: the arches now spring from something+ placeSet('ApsePier' + k, apsePier, frame(bt, xa, 0).pos(0, 0, zn), bt, gS);+ }+ }+ // ---- Roofs, ridge cresting, floor ----+ const Lr = new Batch(TILE.lead);+ const yE = P.eaves + 0.15, half = zn + 0.72;+ addRoofRun(Lr, xa, LAY.xW - 1.2, half, yE, ridge);+ Lr.at(M.T(xc, 0, 0), () => Lr.at(M.Ry(90), () => addRoofRun(Lr, -(zn + (P.transeptBays + 1) * BAY + 0.4), zn + (P.transeptBays + 1) * BAY + 0.4, half, yE, ridge)));+ place('MainRoof_Lead', Lr, MAT.lead, { parent: gRoof });+ const crest = buildCresting(BAY);+ const nCrest = Math.floor((LAY.xW - 2 - xa) / BAY);+ for (let i = 0; i < nCrest; i++) place('RidgeCresting' + i, crest, MAT.gold, { pos: [xa + (i + 0.5) * BAY, ridge, 0], parent: gRoof });+ for (const s of [1, -1]) for (let q = 0; q < P.transeptBays + 2; q++) { const zz = s * (0.5 + (q + 0.5) * BAY + 3); if (Math.abs(zz) < zn + (P.transeptBays + 1) * BAY) place('TranseptCresting' + (s > 0 ? 'N' : 'S') + q, crest, MAT.gold, { pos: [xc, ridge, zz], rot: [0, 90, 0], parent: gRoof }); }+ buildFleche(gRoof, xc, 0);+ // The church floor sits just above the ground outside, so the two can never fight+ const fl = new Batch(TILE.pave), fy = 0.06;+ const R13 = zaw + 0.1;+ fl.face([[xa, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, -R13], [xa, fy, -R13]]);+ const tz = zn + (P.transeptBays + 1) * BAY + 1;+ fl.face([[xc - R13, fy, tz], [xc + R13, fy, tz], [xc + R13, fy, -tz], [xc - R13, fy, -tz]]);+ for (let k = 0; k < P.apseSides; k++) { const th = rad(-(k + 0.5) * CHEV.d), f = frame(-(k + 0.5) * CHEV.d, xa, 0), rr = CHEV.zc + CHEV.Rc + 0.2, hh = rr * Math.tan(CHEV.h); fl.face([f.pos(0, fy, 0), f.pos(-hh, fy, rr), f.pos(hh, fy, rr)]); }+ place('Paving', fl, MAT.pave, { parent: ROOT });+}++// ==================================================================================+// THE GREAT ROSE: real stone tracery, and glass depicting Christ among apostles and angels+// ==================================================================================+const ROSE = { r0: 1.15, rs1: 2.35, r1: 3.05, rs2: 4.05, r2: 4.72, n1: 12, n2: 24 };+const pol = (r, a) => [r * Math.cos(a), r * Math.sin(a)];+function addRoseTracery(F, R) {+ const k = R / 5.5, heavy = traceryBar(0.3 * k, 0.5), mid = hexBar(0.2 * k, 0.42), thin = hexBar(0.14 * k, 0.34);+ const dia = [[0.07 * k, 0], [0, 0.17], [-0.07 * k, 0], [0, -0.17]]; // lightest bar, for the little roundels+ const { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE;+ F.sweep(ringPts(0, 0, R, 36), heavy, { closed: true });+ F.sweep(ringPts(0, 0, r0 * k, 14), mid, { closed: true });+ F.sweep(ringPts(0, 0, r2 * k, 36), mid, { closed: true });+ F.sweep(ringPts(0, 0, r1 * k, 24), mid, { closed: true });+ // Inner wheel: colonnette spokes, each light closed by a pointed trefoil head+ const head = (n, rs, re, a, bar) => {+ const da = PI / n * 0.98, pts = [], N = 6;+ for (let i = 0; i <= N; i++) { const t = -1 + 2 * i / N, h = Math.pow(1 - Math.pow(Math.abs(t), 1.5), 0.72); pts.push(pol((rs + (re - rs) * h) * k, a + t * da)); }+ F.sweep(pts, bar, { caps: false });+ return pts;+ };+ for (let i = 0; i < n1; i++) {+ const a = i * TAU / n1, am = a + PI / n1;+ F.sweep([pol(r0 * k, a), pol(rs1 * k, a)], mid, { caps: false });+ head(n1, rs1, r1 * 0.985, am, thin);+ // the cusp hanging inside the head+ const c = pol((rs1 + 0.34) * k, am); F.sweep(ringPts(c[0], c[1], 0.26 * k, 6), dia, { closed: true, s: 0.95, crease: 80 });+ }+ // Outer wheel of twenty-four lights+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, am = a + PI / n2;+ F.sweep([pol(r1 * k, a), pol(rs2 * k, a)], thin, { caps: false });+ head(n2, rs2, r2 * 0.99, am, thin);+ // Quatrefoil roundels in the outer band+ const c = pol((R / k + r2) / 2 * k, a);+ F.sweep(ringPts(c[0], c[1], 0.3 * k, 6), dia, { closed: true, crease: 80 });+ }+}+const SHARED = {};+// Rose tracery is one mesh, scaled for the smaller transept roses; bright outside, dim inside+function roseTracery() {+ if (!SHARED.rose) { const b = new Batch(TILE.fine); addRoseTracery(b, WF.roseR); SHARED.rose = b; }+ return SHARED.rose;+}+function largePinnacle() {+ if (!SHARED.pin) { const b = new Batch(TILE.fine); addPinnacle(b, 2, 10, { lod: 1 }); SHARED.pin = b; }+ return SHARED.pin;+}+function makeRoseGlass(R, seed, o = {}) {+ const g = new Glass(seed), k = R / 5.5, { r0, r1, r2, n1, n2 } = ROSE;+ g.raw(ringPts(0, 0, R + 0.08, 48), 'lead', -0.4, 0);+ // A light is never one flat sheet: lay the painted mosaic across it, clipped to its shape+ const ground = (pts, col, layer) => { let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of pts) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); } g.withClip(pts, () => gDiaper(g, x0, y0, x1, y1, 0, col, null, null, layer)); };+ // A roundel: a thin jewelled rim round a deep field, with everything inside it clipped to the circle+ const roundel = (cx, cy, rad_, field, draw) => {+ gRim(g, cx, cy, rad_, rad_ * 1.13, ['gold', 'ruby', 'gold', 'cobalt'], 16, 1.2);+ g.disc(cx, cy, rad_, field, 1.5, 16, { came: 0 });+ g.withClip(ringPts(cx, cy, rad_, 16), draw);+ };+ // Nothing behind the tracery is left black: the whole wheel is first laid in cobalt mosaic+ ground(ringPts(0, 0, R + 0.04, 32), 'cobalt', -0.25);+ // The outer band: alternating sheets of ruby and cobalt, a jewel in each+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, b = (i + 1) * TAU / n2;+ ground([pol(r2 * k, a), pol(R, a), pol(R, b), pol(r2 * k, b)], i % 2 ? 'ruby' : 'cobalt', 0);+ const c = pol((R / k + r2) / 2 * k, a); g.disc(c[0], c[1], 0.27 * k, i % 2 ? 'emerald' : 'ruby', 0.6, 10, { came: 0.016 }); g.disc(c[0], c[1], 0.12 * k, 'gold', 1, 8, { came: 0.01 });+ }+ // Outer lights: kings, angels and elders, upright in their roundels so the eye can rest on each face+ const busts = [{ robe: 'emerald', beard: 1, crown: 1, halo: 'gold' }, { robe: 'white', face: 'angel', halo: 'gold', wings: 1 }, { robe: 'ruby', beard: 2, halo: 'gold' }];+ for (let i = 0; i < n2; i++) {+ const am = (i + 0.5) * TAU / n2, da = PI / n2, col = i % 2 ? 'cobalt' : 'ruby', field = i % 2 ? 'ruby' : 'cobalt';+ ground([pol(r1 * k, am - da), pol(r2 * k, am - da), pol(r2 * k, am + da), pol(r1 * k, am + da)], col, 0);+ const mr = (r1 + r2) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.46 * k, B = busts[i % busts.length];+ roundel(cx, cy, rr, field, () => {+ if (B.wings) for (const s of [-1, 1]) g.poly([[cx + s * rr * 0.16, cy - rr * 0.1], [cx + s * rr * 0.98, cy + rr * 0.42], [cx + s * rr * 0.86, cy - rr * 0.5], [cx + s * rr * 0.3, cy - rr * 0.62]], 'gold', 1.8, { came: 0.012 });+ gBust(g, cx, cy - rr * 0.46, rr * 1.32, Object.assign({ layer: 2 }, B));+ });+ }+ // Inner lights: the twelve apostles enthroned, each upright in a larger roundel, colours answering round the wheel+ const scheme = o.figs || [{ robe: 'ruby', mantle: 'cobalt', beard: 1, attr: 'key', arm: 'point' }, { robe: 'emerald', mantle: 'ruby', beard: 2, attr: 'book' }, { robe: 'gold', mantle: 'cobalt', beard: 1, attr: 'sword', arm: 'point' }, { robe: 'cobalt', mantle: 'ruby', attr: 'book' }, { robe: 'purple', mantle: 'gold', beard: 1, attr: 'book' }, { robe: 'ruby', mantle: 'emerald', beard: 2, attr: 'key', arm: 'point' }];+ for (let i = 0; i < n1; i++) {+ const am = (i + 0.5) * TAU / n1, da = PI / n1, col = i % 2 ? 'ruby' : 'cobalt', field = i % 2 ? 'cobalt' : 'ruby';+ ground([pol(r0 * k, am - da), pol(r1 * k, am - da), pol(r1 * k, am + da), pol(r0 * k, am + da)], col, 0);+ const mr = (r0 + r1) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.62 * k;+ roundel(cx, cy, rr, field, () => g.withXf(0, cx, cy - rr * 0.92, 1, () => gFigure(g, 0, 0, rr * 1.86, Object.assign({ pose: 'sit', layer: 2 }, scheme[i % scheme.length]))));+ }+ // The centre: Christ in Majesty in a gilded mandorla+ g.disc(0, 0, r0 * k + 0.06, 'lead', 2.6, 16, { came: 0, jitter: 0 });+ ground(ringPts(0, 0, r0 * k, 16), o.centre || 'cobalt', 3);+ g.withClip(ringPts(0, 0, r0 * k, 14), () => g.withXf(0, 0, -0.98 * k, k, () => {+ const m = []; for (let i = 0; i < 16; i++) { const t = i * TAU / 16; m.push([0.62 * Math.cos(t) * Math.pow(Math.abs(Math.sin(t)) * 0.3 + 0.7, 1), 1 + 0.96 * Math.sin(t)]); }+ g.poly(m, 'gold', 3.3, { came: 0.02 });+ g.poly(m.map((p) => [p[0] * 0.8, 1 + (p[1] - 1) * 0.88]), 'rubyLt', 3.5, { came: 0.012 });+ gFigure(g, 0, 0.22, 1.62, o.majesty || { pose: 'sit', robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'up', attr: null, layer: 4 });+ }));+ return g.b;+}++// ==================================================================================+// WEST FRONT. Local frame: x across the facade, y up, +z out towards the viewer, z=0 the wall.+// ==================================================================================+const WF = { halfW: 21.5, topY: 47, roseY: 31.8, roseR: 5.5, kingY: 20.2, kingTop: 25.3, galY: 40.6, galTop: 46.2, wallT: 0.8, portalC: { x: 0, w0: 4.3, orders: P.jambStatues, dx: 0.58, dz: 0.62, ys: 7.5 }, portalS: { x: 13.5, w0: 3.7, orders: P.jambStatues - 1, dx: 0.6, dz: 0.66, ys: 7.2 } };+// Aged oak door leaf with forged straps, studs and a ring. Hung at x=0, spans to x=w, h high, front +z.+function addDoorLeaf(set, x0, w, h, flip) {+ const { O, I } = set, planks = 6, pw = w / planks, d = flip ? -1 : 1;+ for (let i = 0; i < planks; i++) {+ const a = x0 + d * i * pw, c = x0 + d * (i + 0.97) * pw, xa = Math.min(a, c), xc = Math.max(a, c), tone = 0.46 + 0.16 * Math.abs(Math.sin(i * 2.4 + x0 * 3));+ O.at(M.Ry(0), () => O.box(xa, xc, 0.02, h, -0.05, 0.05, { s: tone }));+ }+ // Oak grain runs up each plank: remap planes with u across and v up is the default box projection+ const strapY = [0.72, h * 0.36, h * 0.64, h - 0.7];+ for (const y of strapY) {+ const reach = w * 0.94, xEnd = x0 + d * reach;+ I.shade(0.34, () => {+ I.box(Math.min(x0, xEnd), Math.max(x0, xEnd), y - 0.075, y + 0.075, 0.05, 0.085, {});+ // A forged hinge: the strap swells and ends in a spear with two barbs, flat iron from one piece; a knuckle turns at the door's edge+ const spear = [[xEnd, y - 0.075], [xEnd + d * 0.1, y - 0.15], [xEnd + d * 0.16, y - 0.07], [xEnd + d * 0.4, y], [xEnd + d * 0.16, y + 0.07], [xEnd + d * 0.1, y + 0.15], [xEnd, y + 0.075]];+ I.slab(spear, [], 0.05, 0.085, {});+ I.at(M.T(x0 + d * 0.03, y, 0.07), () => I.lathe([[0.055, -0.12], [0.055, 0.12]], 6, {}));+ for (let k = 0; k < 4; k++) { const sx = x0 + d * (k + 0.5) * reach / 4; I.box(sx - 0.03, sx + 0.03, y - 0.03, y + 0.03, 0.08, 0.115, { taper: 0.012 }); }+ });+ }+ // Ring handle+ I.shade(0.4, () => I.at(M.of(M.T(x0 + d * (w - 0.3), h * 0.42, 0.11), M.Ry(0)), () => {+ const rg = []; for (let i = 0; i <= 12; i++) { const t = i * TAU / 12; rg.push([0.1 * Math.cos(t), 0.1 * Math.sin(t) - 0.1, 0]); }+ I.tube(rg, 0.018, 5, { capEnd: false }); I.box(-0.05, 0.05, -0.05, 0.05, -0.05, 0, {});+ }));+}+// Height of each sculpted figure at scale 1, from its feet to the top of its hair, crown or mitre+const figureTop = (kind) => ({ bishop: 2.15, king: 2.03, queen: 2.03, virgin: 2.03 }[FIG_ALIAS[kind] || kind] || 1.95);+function buildPortal(cfg, kinds, tymp) {+ const set = mkBatches(), { A, F, G, O, I } = set, { w0, orders: N, dx, dz, ys } = cfg;+ // Every order is concentric: the same two arc centres with the radius growing by one step+ const r0 = w0 * 0.86, c0 = (r0 * r0 - w0 * w0 / 4) / w0, wT = (i) => w0 + 2 * i * dx, zT = (i) => i * dz;+ const riseI = (i) => Math.sqrt(Math.pow(c0 + w0 / 2 + i * dx, 2) - c0 * c0);+ const riseOf = (w) => riseI((w - w0) / (2 * dx));+ const figs = [], zF = N * dz, outerW = wT(N), apex = ys + riseOf(outerW), lintelH = 1.2, doorH = ys - lintelH;+ const statueY = 1.5, statueK = 1.22;+ // ---- Solid stepped jambs, each order with a dado of carved blind arches ----+ for (let i = 0; i < N; i++) {+ for (const s of [-1, 1]) {+ const xa = s * wT(i) / 2, xb = s * wT(i + 1) / 2, x0 = Math.min(xa, xb), x1 = Math.max(xa, xb), zf = zT(i + 1);+ A.box(x0, x1, 0, ys, -0.1, zf, { noBot: true });+ // The dado: a plinth, a blind trefoil head on two colonnettes, a moulded table for the statues above+ F.box(x0 - 0.01, x1 + 0.01, 0, 0.26, zf, zf + 0.05, {});+ F.at(M.T((x0 + x1) / 2, 0, zf), () => {+ const w = x1 - x0 - 0.12, y0 = 0.26, ya = 0.86, r = 0.32;+ F.sweep(pointedArc(w, r, 4).map((p) => [p[0], p[1] + ya]), hexBar(0.05, 0.09), { caps: false, z: 0.03 });+ for (const t of [-1, 1]) F.box(t * w / 2 - 0.03, t * w / 2 + 0.03, y0, ya, 0, 0.06, {});+ F.sweep([[-(x1 - x0) / 2, 1.3], [(x1 - x0) / 2, 1.3]], stringSection(0.16, 0.1), { caps: false });+ });+ // Colonnette on the arris, with a foliate capital; the saint stands against it+ const cx = s * (wT(i) / 2 + 0.02), cz = zT(i) + 0.32;+ F.at(M.T(cx, 0, cz), () => {+ F.lathe([[0.2, 1.3], [0.2, 1.42], [0.13, 1.54], [0.13, ys - 0.5], [0.16, ys - 0.46], [0.13, ys - 0.42], [0.26, ys - 0.04], [0.3, ys]], 8);+ for (let l = 0; l < 4; l++) addLeaf(F, M.of(M.T(0, ys - 0.42, 0), M.Ry(l * 90 + 45), M.T(0.1, 0, 0)), 0.38, { coarse: 1 });+ });+ // The figure stands with its back to the column, on a carved base; its canopy is engaged on the same column,+ // a hand's breadth above this figure's own head+ const dirX = -s * dz, dirZ = dx * 1.5, l = Math.hypot(dirX, dirZ), th = Math.atan2(-dirZ / l, dirX / l) * 180 / PI, fx = dirX / l, fz = dirZ / l;+ const kind = kinds[(i + (s > 0 ? 0 : kinds.length >> 1)) % kinds.length], canopyY = statueY + figureTop(kind) * statueK + 0.1;+ const sx = cx + fx * 0.27, sz = cz + fz * 0.27;+ F.at(M.T(sx, 0, sz), () => {+ F.lathe([[0.3, 1.25], [0.34, 1.32], [0.24, 1.38], [0.3, 1.44], [0.33, statueY]], 8);+ for (let l = 0; l < 2; l++) addLeaf(F, M.of(M.T(0, 1.36, 0), M.Ry(l * 90 + (s > 0 ? -60 : 150)), M.T(0.24, 0, 0)), 0.2, { coarse: 1 });+ F.at(M.T(0, canopyY + 0.14, 0), () => {+ // A canopy like a tiny carved building: vaulted soffit, a drum of gablets, thin spirelet+ F.lathe([[0.05, -0.14], [0.27, -0.06], [0.33, 0], [0.33, 0.1], [0.28, 0.14], [0.17, 0.17], [0.17, 0.5], [0.22, 0.54], [0.15, 0.58], [0.07, 0.64], [0.01, 1.02]], 6, { phase: PI / 6, faceted: true });+ const face = s > 0 ? -60 : 60;+ for (const l of [-1, 0, 1]) F.at(M.of(M.Ry(face + l * 60), M.T(0, 0, 0.27)), () => {+ F.face([[-0.16, 0.11, 0.02], [0.16, 0.11, 0.02], [0, 0.42, 0.02]]);+ crocketsAlong(F, [-0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [-1, 0.6, 0], 0);+ crocketsAlong(F, [0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [1, 0.6, 0], 0);+ F.shade(0.5, () => F.face([[-0.08, 0.14, 0.03], [0.08, 0.14, 0.03], [0, 0.28, 0.03]]));+ });+ });+ });+ figs.push({ kind, pos: [cfg.x + sx, statueY, sz], rot: [0, th, 0], scale: statueK, mirror: s < 0 });+ }+ }+ // ---- Archivolts: each order a hollow, each hollow a procession of small figures, every one on a console under a canopy ----+ const ARCH_KINDS = ['apostle', 'king', 'angel', 'bishop', 'queen'], ak = 0.62;+ for (let i = 0; i < N; i++) {+ const w = wT(i), r = riseI(i), arc = pointedArc(w, r, 8).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, [[0, -0.1], [dx, -0.1], [dx, dz], [0, 0]], { z: zT(i), caps: false, crease: 30 });+ F.at(M.T(0, 0, zT(i + 1) - 0.02), () => F.sweep(pointedArc(wT(i + 1), riseI(i + 1), 8).map((p) => [p[0], p[1] + ys]), hexBar(0.2, 0.22), { caps: false }));+ const pts = pointedArc(w + dx, riseI(i + 0.5), 16).map((p) => [p[0], p[1] + ys]);+ let len = 0; const seg = []; for (let k = 0; k < pts.length - 1; k++) { const l = Math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]); seg.push(l); len += l; }+ const n = Math.max(4, Math.round(len / 1.52));+ for (let q = 0; q < n; q++) {+ const d = (q + 0.5) / n * len; let acc = 0, k = 0; while (k < seg.length - 1 && acc + seg[k] < d) { acc += seg[k]; k++; }+ const f = (d - acc) / seg[k], px = lerp(pts[k][0], pts[k + 1][0], f), py = lerp(pts[k][1], pts[k + 1][1], f);+ const tx0 = (pts[k + 1][0] - pts[k][0]) / seg[k], ty0 = (pts[k + 1][1] - pts[k][1]) / seg[k], rx = -ty0, ry = tx0;+ const up = d > len / 2 ? -1 : 1, Y = [tx0 * up, ty0 * up, 0];+ // sits on the splay, looking out towards the opening and the viewer+ const nl = Math.hypot(dz, dx), X = [-rx * dz / nl, -ry * dz / nl, dx / nl];+ const Z = [X[1] * Y[2] - X[2] * Y[1], X[2] * Y[0] - X[0] * Y[2], X[0] * Y[1] - X[1] * Y[0]];+ const pos = [px + X[0] * 0.17 - Y[0] * 0.62, py + X[1] * 0.17 - Y[1] * 0.62, zT(i) + dz * 0.5 + X[2] * 0.17];+ figs.push({ kind: ARCH_KINDS[(i + q) % ARCH_KINDS.length], lod: 0, pos: [cfg.x + pos[0], pos[1], pos[2]], basis: { X, Y, Z }, scale: ak });+ F.at(frameOf(pos, X, Y), () => {+ F.lathe([[0.03, -0.18], [0.16, -0.02], [0.18, 0.02]], 6);+ F.lathe([[0.02, 1.27], [0.16, 1.33], [0.16, 1.38], [0.02, 1.56]], 6, { faceted: true });+ });+ }+ }+ // ---- Outer hood with gable and crockets ----+ const hood = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, zF), () => F.sweep(hood, rollSection(0.34, 0.44, 2, 0.05, 2), { caps: false }));+ // The gable springs from the arch springing: plain coursed stone, its carving is real and stands proud of it+ const gw = outerW + 1.5, gy = ys, gTop = WF.kingY - 0.7, gH = gTop - gy;+ const archCut = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ const field = [[-gw / 2, gy], [-(outerW + 0.1) / 2, gy], ...archCut.slice(1, -1), [(outerW + 0.1) / 2, gy], [gw / 2, gy], [0, gTop]];+ A.at(M.T(0, 0, zF - 0.1), () => A.slab(field, [], -0.1, 0, { noOuterSides: true, noBack: true }));+ A.at(M.T(0, 0, zF - 0.2), () => A.slab(field, [], -zF + 0.3, 0, { noOuterSides: false, noFront: true, noBack: true, so: 0.7 }));+ F.at(M.T(0, 0, zF - 0.14), () => {+ addGable(F, gw, gH, 0, 0.24, gy, { size: 0.72, finial: 0.1, crockets: 6, noField: true, lod: 1 });+ const yc = apex + (gTop - apex) * 0.36, rc = Math.min(0.92, (gTop - yc) * 0.42);+ F.sweep(ringPts(0, yc, rc, 16), traceryBar(0.18, 0.24), { closed: true, z: 0.04 });+ const foil = []; for (let i = 0; i < 15; i++) { const a = PI / 2 + i * TAU / 15, R = rc * (0.56 + 0.34 * Math.pow(Math.abs(Math.cos(a * 1.5 - PI * 0.75)), 0.8)); foil.push([R * Math.cos(a), yc + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.09, 0.18), { closed: true, z: 0.04, crease: 40 });+ });+ G.at(M.T(0, gTop + 0.1, zF - 0.02), () => addFinial(G, 1.5, 1));+ // ---- Tympanum: one block of dressed stone, standing proud of the coursed wall behind it, with the scene carved out of it ----+ const tw = w0 + 0.04, tr = riseOf(w0), k = w0 / 4.3, reliefZ = 0.16, relief = 0.6;+ F.slab(archOutline(tw, ys - 0.05, ys, tr, 9), [], -0.1, reliefZ, { noBack: true, sr: 0.7 });+ // the ground the figures rise from is part of the block, a low moulded base rather than a shelf+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, ys - 0.02, ys + 0.1, -0.08, reliefZ + 0.14, {});+ // Lintel: a recessed frieze, deep enough to hold its people, cornice and sill standing forward of it+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, ys - 0.02, -0.08, -0.02, {});+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, doorH + 0.12, -0.08, 0.42, {});+ F.at(M.T(0, 0, 0.42), () => F.sweep([[-tw / 2 - 0.02, doorH + 0.06], [tw / 2 + 0.02, doorH + 0.06]], stringSection(0.12, 0.06), { caps: false }));+ const nL = Math.round(w0 / 0.37), frieze = ['apostle', 'queen', 'apostle', 'king', 'bishop', 'apostle'];+ for (let i = 0; i < nL; i++) {+ // two ranks, every other figure a step forward: they overlap and each one turns to the middle of the door+ const x = -w0 / 2 + (i + 0.5) * w0 / nL, side = x < 0 ? 1 : -1, deep = i % 2;+ figs.push({ kind: frieze[i % frieze.length], lod: 0, relief: 0.68, pos: [cfg.x + x, doorH + 0.12, deep ? 0.04 : 0.16], rot: [0, -90 + side * (16 + 10 * (i % 3)), 0], scale: 0.43 });+ }+ // The central figure enthroned in a vesica: two arcs meeting in a point above and below, a broad moulded band of stone+ // with its face gilded. Everything in the scene is cut back to the same block, and nothing crosses the frame.+ const mY = ys + 0.1, mH = tr * 0.84, mW = 0.84 * k, nM = 10;+ const vesica = (w, h, y0) => { const out = []; for (let i = 0; i < nM; i++) { const v = i / nM; out.push([w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } for (let i = 0; i < nM; i++) { const v = 1 - i / nM; out.push([-w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } return out; };+ const vo = vesica(mW, mH, mY - 0.06), vi = vesica(mW - 0.2, mH - 0.44, mY + 0.16);+ F.at(M.T(0, 0, reliefZ), () => { F.slab(vo, [vi], -0.02, 0.07, { sr: 0.78 }); F.sweep(vo, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); F.sweep(vi, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); });+ G.at(M.T(0, 0, reliefZ + 0.07), () => G.slab(vesica(mW - 0.025, mH - 0.07, mY - 0.025), [vesica(mW - 0.175, mH - 0.37, mY + 0.125)], 0, 0.05, {}));+ figs.push({ kind: tymp.main, half: 1, relief, pos: [cfg.x, mY, reliefZ + 0.02], rot: [0, -90, 0], scale: 1.0 * k });+ // Worshippers on their knees and angels crowding behind them, every head turned to the throne+ for (const s of [-1, 1]) {+ figs.push({ kind: 'kneel', half: 1, relief, pos: [cfg.x + s * 1.28 * k, mY, reliefZ + 0.12], rot: [0, -90 + s * 50, 0], scale: 0.84 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[0], half: 1, relief, pos: [cfg.x + s * 1.6 * k, mY, reliefZ + 0.01], rot: [0, -90 + s * 34, 0], scale: 0.72 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[1], half: 1, relief, lod: 0, pos: [cfg.x + s * 1.92 * k, mY, reliefZ + 0.08], rot: [0, -90 + s * 55, 0], scale: 0.44 * k, mirror: s < 0 });+ }+ // Trumeau with its statue, on a carved base, under a canopy+ A.box(-0.32, 0.32, 0, doorH, -0.1, 0.34, { noBot: true });+ const tK = tymp.trumeauScale || 1.3, tBase = 1.06, tCanopy = tBase + figureTop(tymp.trumeau) * tK + 0.12;+ F.at(M.T(0, 0, 0.5), () => {+ F.lathe([[0.42, 0], [0.42, 0.42], [0.3, 0.56], [0.25, 0.78], [0.34, 0.92], [0.36, tBase]], 8, { faceted: true, phase: PI / 8 });+ for (let l = 0; l < 6; l++) addLeaf(F, M.of(M.T(0, 0.82, 0), M.Ry(l * 60 + 15), M.T(0.27, 0, 0)), 0.24, { coarse: 1 });+ F.at(M.T(0, tCanopy + 0.2, 0), () => F.lathe([[0.06, -0.2], [0.33, -0.08], [0.42, 0], [0.42, 0.14], [0.35, 0.18], [0.22, 0.22], [0.22, 0.58], [0.28, 0.62], [0.2, 0.66], [0.09, 0.72], [0.02, 1.1]], 6, { phase: PI / 6, faceted: true }));+ });+ figs.push({ kind: tymp.trumeau, pos: [cfg.x, tBase, 0.5], rot: [0, -90, 0], scale: tK });+ // Doors: two leaves either side of the trumeau, recessed in the wall+ const leaf = w0 / 2 - 0.34;+ O.at(M.T(0, 0, -0.14), () => { const set2 = set; I.at(M.T(0, 0, -0.14), () => { addDoorLeaf(set2, w0 / 2 - 0.02, leaf, doorH - 0.04, true); addDoorLeaf(set2, -w0 / 2 + 0.02, leaf, doorH - 0.04, false); }); });+ return { set, figs, apex, zF, outerW };+}+function placePortal(gWest, name, cfg, pr, opt = {}) {+ placeSet(name, pr.set, [cfg.x, 0, 0], 0, gWest);+ const gF = group(name + '_Figures', gWest); // the people of one door, as one group of nodes+ pr.figs.forEach((f, i) => {+ const rot = f.rot || eulerFromBasis(f.basis.X, f.basis.Y, f.basis.Z);+ const pos = [f.pos[0] + (cfg.x - pr.baseX), f.pos[1], f.pos[2]];+ // Full sculpture where people stand next to it; opt.light keeps a door's overhead scene, or the whole door, to the lighter copies+ const far = opt.light === 'all' || (opt.light === 'scene' && f.pos[1] > 4);+ placeFigure(name + '_Fig' + i, f.kind, { pos, rot, scale: f.scale, parent: gF, lod: far ? 0 : f.lod, mirror: f.mirror, half: f.half, relief: f.relief });+ });+}+function eulerFromBasis(X, Y, Z) { // degrees, three.js 'XYZ' order+ const m13 = clamp(Z[0], -1, 1), y = Math.asin(m13);+ let x, z;+ if (Math.abs(m13) < 0.9999) { x = Math.atan2(-Z[1], Z[2]); z = Math.atan2(-Y[0], X[0]); } else { x = Math.atan2(Y[2], Y[1]); z = 0; }+ return [x * 180 / PI, y * 180 / PI, z * 180 / PI];+}+// One niche of the Gallery of Kings, shared along the facade+function buildKingNiche(pitch) {+ const set = mkBatches(), { F, A } = set, h = WF.kingTop - WF.kingY, y0 = WF.kingY;+ A.face([[-pitch / 2, y0, 0.02], [pitch / 2, y0, 0.02], [pitch / 2, y0 + h, 0.02], [-pitch / 2, y0 + h, 0.02]]);+ F.box(-pitch / 2, pitch / 2, y0 - 0.5, y0, 0, 1.3, { s: 0.9 });+ F.at(M.T(0, 0, 1.3), () => F.sweep([[-pitch / 2, y0 - 0.38], [pitch / 2, y0 - 0.38]], stringSection(0.3, 0.26), { caps: false }));+ // Colonnettes and a cusped arch with a gablet over each king+ const ys = y0 + 3.15, rise = 1.3;+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0.95), () => F.lathe([[0.17, y0], [0.09, y0 + 0.16], [0.09, ys - 0.2], [0.2, ys]], 8));+ F.at(M.T(0, 0, 0.95), () => {+ const arc = pointedArc(pitch - 0.1, rise, 6).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, rollSection(0.14, 0.36, 1, 0.03, 2), { caps: false });+ // solid spandrel+ A.at(M.T(0, 0, 0.95), () => A.slab([[-pitch / 2, ys], [-(pitch - 0.1) / 2, ys], ...arc, [(pitch - 0.1) / 2, ys], [pitch / 2, ys], [pitch / 2, y0 + h], [-pitch / 2, y0 + h]], [], -0.1, 0.1, { noOuterSides: true }));+ addGable(F, pitch * 0.96, 1.12, 0.1, 0.1, ys + 0.46, { size: 0.26, crockets: 2, finial: 0.34, solid: true, lod: 0 });+ });+ // The roof of the gallery behind the arcade+ F.box(-pitch / 2, pitch / 2, y0 + h - 0.02, y0 + h + 0.12, 0, 1.28, { s: 0.85 });+ return set;+}+// One bay of the open arcade above the rose and the tower windows+function buildGalleryBay(pitch) {+ const set = mkBatches(), { F, A } = set, y0 = WF.galY, y1 = WF.galTop, ys = y0 + 3.3;+ F.shade(0.4, () => F.face([[-pitch / 2, y0, 0.03], [pitch / 2, y0, 0.03], [pitch / 2, y1, 0.03], [-pitch / 2, y1, 0.03]]));+ const arc = pointedArc(pitch * 0.92, 1.5, 5).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, 0.5), () => {+ F.sweep(arc, traceryBar(0.16, 0.36), { caps: false });+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0), () => F.lathe([[0.2, y0], [0.1, y0 + 0.2], [0.1, ys - 0.22], [0.22, ys]], 8));+ F.sweep(ringPts(0, ys + 0.6, 0.26, 7), hexBar(0.07, 0.14), { closed: true });+ });+ A.at(M.T(0, 0, 0.5), () => A.slab([[-pitch / 2, ys], [-pitch * 0.46, ys], ...arc, [pitch * 0.46, ys], [pitch / 2, ys], [pitch / 2, y1], [-pitch / 2, y1]], [], -0.18, 0.18, { noOuterSides: true }));+ F.box(-pitch / 2, pitch / 2, y0 - 0.2, y0 + 0.02, 0, 0.72, { s: 0.92 });+ F.at(M.T(0, 0, 0.72), () => { F.sweep([[-pitch / 2, y0 - 0.1], [pitch / 2, y0 - 0.1]], stringSection(0.26, 0.22), { caps: false }); F.sweep([[-pitch / 2, y1], [pitch / 2, y1]], stringSection(0.3, 0.3), { caps: false }); });+ F.at(M.T(0, 0, 0.72), () => addParapet(F, -pitch / 2, pitch / 2, y1 + 0.12, 1.05, 0.2, { pitch: 1.1, nh: 8 }));+ return set;+}+// One face of a belfry stage, shared by all eight faces of the two towers+function buildBelfryFace() {+ const set = mkBatches(), { A, F, L } = set, hw = 7, zf = 7, y0 = WF.topY, yT = P.towerTop - 2.6;+ const lw = 3.05, lx = 3.3, sill = y0 + 4.2, ys = y0 + 13.6, rise = 4.1, holes = [];+ for (const s of [-1, 1]) holes.push(archOutline(lw, sill, ys, rise, 7).map((p) => [s * lx + p[0], p[1]]));+ A.at(M.T(0, 0, zf), () => A.slab([[-hw, y0], [hw, y0], [hw, yT], [-hw, yT]], holes, -0.9, 0, { noOuterSides: true, noBack: true, sr: 0.66 }));+ // The bell chamber: a floor behind the openings, and a bell hanging on each side of the frame+ F.box(-hw + 0.8, hw - 0.8, sill - 0.5, sill - 0.2, zf - 5, zf - 0.8, {});+ set.I.at(M.T(0, sill + 3.2, zf - 2.5), () => set.I.lathe([[0.06, 1.72], [0.36, 1.62], [0.5, 1.12], [0.56, 0.7], [0.84, 0.12], [0.92, 0]], 10));+ // Behind the bells the oak frame that carries them closes the view, so no sky shows through the tower+ set.O.face([[-4.2, sill - 0.3, zf - 4], [4.2, sill - 0.3, zf - 4], [4.2, yT, zf - 4], [-4.2, yT, zf - 4]], 0.16);+ for (const s of [-1, 1]) {+ F.at(M.T(s * lx, 0, zf - 0.45), () => {+ F.sweep(pointedArc(lw, rise, 6).map((p) => [p[0], p[1] + ys]), rollSection(0.2, 0.7, 1, 0.04, 2), { caps: false });+ F.sweep([[0, sill - 0.05], [0, ys + 0.1]], traceryBar(0.2, 0.42), {});+ const lw2 = (lw - 0.1) / 2;+ for (const t of [-1, 1]) F.sweep(pointedArc(lw2, lw2 * 0.9, 4).map((p) => [t * lw2 / 2 + p[0], ys + 0.1 + p[1]]), hexBar(0.14, 0.34), { caps: false });+ F.sweep(ringPts(0, ys + rise * 0.62, 0.48, 10), hexBar(0.14, 0.34), { closed: true });+ });+ // Louvres: lead-clad boards set across the opening and tilted to throw the rain outward, dark air between them+ L.at(M.T(s * lx, 0, zf - 0.45), () => {+ const slope = 0.42;+ for (let y = sill + 0.2; y < ys + rise * 0.3; y += 0.5) {+ const h = y <= ys ? lw / 2 : (lw / 2) * Math.sqrt(Math.max(0, 1 - Math.pow((y - ys) / rise, 2)));+ L.face([[-h, y, 0.3], [h, y, 0.3], [h, y + slope, -0.3], [-h, y + slope, -0.3]]);+ L.face([[h, y - 0.05, 0.3], [-h, y - 0.05, 0.3], [-h, y + slope - 0.05, -0.3], [h, y + slope - 0.05, -0.3]]);+ }+ });+ F.at(M.T(s * lx, 0, zf), () => {+ F.sweep(pointedArc(lw + 0.5, rise + 0.3, 6).map((p) => [p[0], p[1] + ys]), hexBar(0.24, 0.28), { caps: false });+ addGable(F, lw + 0.9, 3.1, 0.03, 0.18, ys + rise * 0.5, { size: 0.42, finial: 0.8, crockets: 3, lod: 0 });+ });+ }+ F.at(M.T(0, 0, zf), () => {+ F.sweep([[-hw, y0 + 0.2], [hw, y0 + 0.2]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, sill - 0.3], [hw, sill - 0.3]], stringSection(0.32, 0.28), { caps: false });+ F.sweep([[-hw, yT - 0.2], [hw, yT - 0.2]], stringSection(0.4, 0.44), { caps: false });+ for (let i = 0; i < 12; i++) { const x = -hw + (i + 0.5) * 2 * hw / 12; F.box(x - 0.14, x + 0.14, yT - 0.78, yT - 0.42, 0, 0.3, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zf - 0.2), () => addParapet(F, -hw, hw, yT, 1.2, 0.28, { pitch: 1.4, nh: 8 }));+ return set;+}+// Octagonal corner turret and its great pinnacle, reaching the full height of the tower+function buildTowerCorner() {+ const set = mkBatches(), { A, F } = set, y0 = WF.topY, yT = P.towerTop - 1.3, R = 1.72;+ // The turret grows out of the corner on a tapering corbel, closed underneath; its lower part sits inside the corner pier+ A.lathe([[0.04, y0 - 4.2], [R * 0.66, y0 - 2.6], [R, y0 - 1], [R, yT]], 8, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < 8; i++) {+ const a = i * 45 + 22.5, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ F.at(M.of(M.Ry(a), M.T(0, 0, ap)), () => {+ if (i % 2 === 0) for (const k of [0, 1]) {+ const yb = y0 + 1 + k * 11;+ F.sweep(pointedArc(side * 0.72, 0.9, 4).map((p) => [p[0], p[1] + yb + 8.2]), hexBar(0.1, 0.12), { caps: false });+ for (const s of [-1, 1]) F.sweep([[s * side * 0.36, yb], [s * side * 0.36, yb + 8.2]], hexBar(0.1, 0.12), { caps: false });+ F.shade(0.55, () => F.face([[-side * 0.36, yb, 0.01], [side * 0.36, yb, 0.01], [side * 0.36, yb + 8.2, 0.01], [0, yb + 9.05, 0.01], [-side * 0.36, yb + 8.2, 0.01]]));+ }+ });+ }+ F.lathe([[R, y0 + 12.3], [R + 0.28, y0 + 12.5], [R + 0.28, y0 + 12.66], [R, y0 + 12.82]], 8, { faceted: true, phase: PI / 8 });+ F.lathe([[R, yT - 0.2], [R + 0.36, yT], [R + 0.36, yT + 0.26], [R, yT + 0.42]], 8, { faceted: true, phase: PI / 8 });+ return set;+}+function buildWestFront() {+ const gWest = group('West_Front');+ gWest.position.set(LAY.xW, 0, 0); gWest.rotation.y = rad(90);+ const set = mkBatches(), { A, F, L, G, D } = set, hw = WF.halfW, yTop = WF.topY, tcx = 14.5;+ // Real recessed lights flanking the tower windows, cut through the face of the wall+ const towerRecess = [];+ for (const s of [-1, 1]) for (const d of [-1, 1]) towerRecess.push(archOutline(1.5, WIN.cSill + 0.6, WIN.cSp - 0.6, 1.8, 6).map((p) => [s * tcx + d * 3.55 + p[0], p[1]]));+ // ---- The great wall with real openings: doors and rose ----+ const cP = WF.portalC, sP = WF.portalS;+ const doorHole = (x, w, h) => [[x - w / 2, 0.02], [x + w / 2, 0.02], [x + w / 2, h], [x - w / 2, h]];+ const wallOutline = [[-hw, 0], [-sP.x - sP.w0 / 2, 0], [-sP.x - sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, 0], [-cP.w0 / 2, 0], [-cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, 0], [sP.x - sP.w0 / 2, 0], [sP.x - sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]];+ A.slab(wallOutline, [ringPts(0, WF.roseY, WF.roseR, 40), ...towerRecess], -WF.wallT, 0, { noOuterSides: true, sr: 0.62 });+ towerRecess.forEach((o) => addRecess(A, F, o, 0.5));+ // Seven centuries of rain under every ledge of the front, damp rising from the ground+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.02, 3.2);+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.02, 3.6);+ addStain(D, -hw, hw, WF.galY - 0.42, 0.02, 3.4);+ addStain(D, -hw, hw, WF.kingTop + 0.02, 0.02, 2.6, 'drip');+ // damp on the wall only: the door openings are left clear+ for (const span of [[-hw, -sP.x - sP.w0 / 2], [-sP.x + sP.w0 / 2, -cP.w0 / 2], [cP.w0 / 2, sP.x - sP.w0 / 2], [sP.x + sP.w0 / 2, hw]]) addStain(D, span[0], span[1], 5.2, 0.03, 5.2, 'ground');+ // Each ledge throws a soft shadow on the wall just below it; soot gathers where the rain cannot reach+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.03, 0.8, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.03, 1.2, 'soft');+ addStain(D, -hw, hw, WF.galY - 0.42, 0.03, 1.0, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.62, 0.04, 2.4, 'soot');+ for (const s of [-1, 1]) addStain(D, s * tcx - WIN.cW / 2 - 0.4, s * tcx + WIN.cW / 2 + 0.4, WIN.cSill - 0.12, 0.02, 3.4);+ // ---- Rose: moulded ring, tracery, spandrels, relieving arch ----+ place('Great_Rose_Tracery', roseTracery(), MAT.fine, { pos: [0, WF.roseY, -0.4], parent: gWest });+ F.at(M.T(0, WF.roseY, 0), () => {+ F.at(M.T(0, 0, 0), () => {+ const ring = traceryBar(0.46, 0.58);+ F.sweep(ringPts(0, 0, WF.roseR + 0.32, 40), ring, { closed: true, z: 0.14 });+ F.sweep(ringPts(0, 0, WF.roseR + 0.78, 44), rollSection(0.2, 0.3, 1, 0.04, 2), { closed: true, z: 0.1 });+ });+ });+ F.at(M.T(0, 0, 0.03), () => {+ // spandrels round the rose: plain stone, each with a real pierced roundel carved in its corner+ const R2 = WF.roseR + 0.72, ry = WF.roseY, sq = 6.6;+ for (const [sx, sy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {+ // corner of the square -> along its edge -> round the rose -> along the other edge+ const arcPts = []; for (let i = 0; i <= 8; i++) { const a = i / 8 * PI / 2; arcPts.push([sx * R2 * Math.cos(a), sy * R2 * Math.sin(a)]); }+ const poly = [[sx * sq, sy * sq], [sx * sq, 0], ...arcPts, [0, sy * sq]].map((p) => [p[0], p[1] + ry]);+ F.slab(area2(poly) > 0 ? poly : poly.slice().reverse(), [], -0.04, 0, { noOuterSides: true, noBack: true });+ const cx = sx * (sq - 1.02), cy = ry + sy * (sq - 1.02);+ F.sweep(ringPts(cx, cy, 0.76, 14), traceryBar(0.13, 0.2), { closed: true, z: 0.05 });+ const foil = []; for (let i = 0; i < 16; i++) { const a = i * TAU / 16, R = 0.68 * (0.62 + 0.32 * Math.abs(Math.cos(a * 2))); foil.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.07, 0.14), { closed: true, z: 0.05, crease: 40 });+ }+ });+ F.at(M.T(0, 0, 0.14), () => { F.sweep([[-6.6, WF.roseY - 6.6], [6.6, WF.roseY - 6.6]], stringSection(0.3, 0.26), { caps: false }); F.sweep([[-6.6, WF.roseY + 6.6], [6.6, WF.roseY + 6.6]], stringSection(0.3, 0.26), { caps: false }); });+ // ---- Tower lower stages: paired windows with hood and gable, string courses ----+ const cw = WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = WIN.cRise;+ for (const s of [-1, 1]) {+ F.at(M.T(s * tcx, 0, 0.32), () => {+ addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 8), rollSection(0.24, 0.5, 1, 0.04, 2), { closed: true, crease: 40, z: -0.14 });+ F.sweep(pointedArc(cw + 0.8, cRise + 0.4, 7).map((p) => [p[0], p[1] + cSp]), hexBar(0.26, 0.3), { z: 0.1, caps: false });+ addGable(F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 4, lod: 0 });+ });+ F.at(M.T(s * tcx, 0, 0.02), () => { F.sweep([[-6.1, WF.kingTop + 0.24], [6.1, WF.kingTop + 0.24]], stringSection(0.3, 0.22), { caps: false }); });+ }+ // ---- Buttresses of the front: one continuous pier between the portals, narrowing as it climbs through the kings and+ // the gallery; the outer corners of the towers carry a single diagonal pier instead of two that would collide ----+ const xPier = [-7.5, 7.5], lowPanel = { k: 0.6, b: 1.1, t: 7.2, r: 0.7 };+ xPier.forEach((x, i) => {+ const m = M.T(x, 0, 0);+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: 0, y1: 11.6, w: 1.05, f: 3.6, panel: lowPanel }, { y0: 12.9, y1: 18.4, w: 0.98, f: 2.7 }, { y0: 19.7, y1: 19.72, w: 0.92, f: 1.2, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 3.6), () => { F.box(-1.17, 1.17, 0, 0.74, -3.7, 0.12, { noBot: true }); addNicheFrame(F, 0, 6.1, 11.0, 1.42); });+ // above the kings the pier stands back on the gallery's roof+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: WF.kingTop + 0.1, y1: 38.8, w: 0.92, f: 1.26 }, { y0: 40, y1: yTop + 0.1, w: 0.8, f: 0.96, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 1.26), () => addNicheFrame(F, 0, 31, 37.4, 1.3));+ D.at(M.T(x, 0, 3.6), () => { addStain(D, -1.05, 1.05, 11.4, 0.02, 2.2, 'drip'); addStain(D, -1.05, 1.05, 4.6, 0.03, 4.4, 'ground'); addStain(D, -1.05, 1.05, 11.45, 0.03, 0.7, 'soft'); });+ placeFigure('Buttress_Saint' + i, SAINTS[i + 3], { pos: [x, 6.2, 3.98], rot: [0, -90, 0], scale: 1.42, parent: gWest });+ place('Facade_Gargoyle' + i, gargoyle(i % 3), MAT.fine, { pos: [x, 17.2, 2.55], rot: [0, -90, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ placeFigure('Upper_Saint' + i, SAINTS[i + 5], { pos: [x, 31.1, 1.62], rot: [0, -90, 0], scale: 1.32, parent: gWest, lod: 0 });+ });+ for (const s of [-1, 1]) {+ const k = Math.SQRT1_2, z = 3.98, cx = s * (hw + k * z);+ placeFigure('Corner_Saint' + s, SAINTS[s > 0 ? 3 : 5], { pos: [cx, 6.2, k * z], rot: [0, -90 + s * 45, 0], scale: 1.42, parent: gWest });+ place('Corner_Gargoyle' + s, gargoyle(s > 0 ? 1 : 2), MAT.fine, { pos: [s * (hw + k * 2.65), 26.6, k * 2.65], rot: [0, -90 + s * 45, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ }+ // closing blocks where the gallery of kings dies into the corner piers+ for (const s of [-1, 1]) A.box(Math.min(s * 19.9, s * 20.6), Math.max(s * 19.9, s * 20.6), WF.kingY - 0.5, WF.kingTop + 0.12, 0, 1.3, { noBot: true });+ placeSet('West_Wall', set, [0, 0, 0], 0, gWest);+ // ---- Glass ----+ const roseWest = makeRoseGlass(WF.roseR, 101);+ SHARED.roseGlass = roseWest;+ placeGlass('Great_Rose', roseWest, [0, WF.roseY, -0.4], 180, gWest, true);+ const twinGlass = SHARED.cleres;+ for (const s of [-1, 1]) place('TowerWindow_Out' + s, glassOutside(twinGlass[s > 0 ? 0 : 1]), MAT.glassOutWin, { pos: [s * tcx, 0, 0.14], rot: [0, 180, 0], parent: gWest, scale: [1, 1, -1] });+ // ---- Three portals: a bishop, a crowned king, a veiled queen, an apostle with his key, a smiling angel;+ // the Virgin and Child on the central door post, Christ enthroned above her ----+ const pC = buildPortal(cP, ['apostle', 'bishop', 'queen', 'king', 'apostle', 'king', 'angel', 'bishop', 'queen', 'apostle'], { main: 'seated', side: ['angel', 'queen'], trumeau: 'virgin', trumeauScale: 1.4 });+ const pS = buildPortal(Object.assign({}, sP, { x: 0 }), ['queen', 'bishop', 'apostle', 'king', 'angel', 'apostle', 'king', 'queen'], { main: 'seatedVirgin', side: ['angel', 'apostle'], trumeau: 'bishop', trumeauScale: 1.24 });+ pC.baseX = cP.x; pS.baseX = 0; SHARED.sidePortal = pS;+ placePortal(gWest, 'Portal_Central', cP, pC);+ placePortal(gWest, 'Portal_South', Object.assign({}, sP, { x: -sP.x }), pS, { light: 'scene' });+ placePortal(gWest, 'Portal_North', Object.assign({}, sP, { x: sP.x }), pS, { light: 'scene' });+ // ---- Gallery of Kings ----+ const nK = P.kings, pitch = 2 * 20.1 / nK, niche = buildKingNiche(pitch), kinds = ['kingA', 'kingB'];+ for (let i = 0; i < nK; i++) {+ const x = (i + 0.5 - nK / 2) * pitch;+ placeSet('King_Niche' + i, niche, [x, 0, 0], 0, gWest);+ placeFigure('King' + i, kinds[i % 2], { pos: [x, WF.kingY + 0.04, 0.62], rot: [0, -90 + (i % 4 - 1.5) * 7, 0], scale: 1.7, parent: gWest, mirror: i % 2 === 1 });+ }+ // ---- High gallery ----+ const nG = 20, gp = 2 * 19.75 / nG, gb = buildGalleryBay(gp);+ for (let i = 0; i < nG; i++) placeSet('Gallery' + i, gb, [(i + 0.5 - nG / 2) * gp, 0, 0], 0, gWest);+ // ---- Central gable rising over the gallery, closing the nave roof ----+ const front = mkBatches(), yG0 = yTop + 1.2, yR = LAY.ridge + 1.2, gW = 2 * (zn + 1.5);+ front.A.at(M.T(0, 0, -1.2), () => front.A.slab([[-gW / 2, yG0 - 2], [gW / 2, yG0 - 2], [gW / 2, yG0], [0, yR + 1.6], [-gW / 2, yG0]], [ringPts(0, yG0 + 1.8, 1.25, 12)], -0.5, 0.5, { sr: 0.5 }));+ front.F.at(M.T(0, 0, -0.7), () => {+ addGable(front.F, gW + 0.2, yR + 1.6 - yG0, 0.04, 0.26, yG0, { size: 0.86, finial: 0.1, crockets: 7, noField: true, lod: 1 });+ front.F.sweep(ringPts(0, yG0 + 1.8, 1.32, 14), traceryBar(0.26, 0.44), { closed: true });+ // Blind arcade stepping up under the rake+ const nA = 7; for (let i = 0; i < nA; i++) { const x = (i - (nA - 1) / 2) * 1.72, top = yG0 + 0.3 + (gW / 2 - Math.abs(x) - 1) * (yR + 1.6 - yG0) / (gW / 2) * 0.7; if (Math.abs(x) < 1.7) continue; front.F.sweep([[x - 0.62, yG0], [x - 0.62, top], [x, top + 0.75], [x + 0.62, top], [x + 0.62, yG0]], hexBar(0.14, 0.16), { caps: false, crease: 20 }); }+ });+ front.G.at(M.T(0, yR + 1.62, -0.5), () => addFinial(front.G, 2.2, 1));+ placeSet('West_Gable', front, [0, 0, 0], 0, gWest);+ place('West_Gable_Glass', makeLancetRoundel(1.25), MAT.glassOut, { pos: [0, yG0 + 1.8, -0.75], parent: gWest });+ // ---- Towers: shell, belfry faces, corner turrets, lead cap ----+ // One tower shell (side and rear walls of the lower stages), built outward towards +x; the other tower mirrors it+ const shell = mkBatches(), tw = 7;+ // The outer side of the tower: real recessed arcades in two tiers, either side of its windows+ const sideM = M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), sideRecess = [];+ for (const d of [-1, 1]) {+ sideRecess.push(archOutline(1.2, 3.4, 16.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ sideRecess.push(archOutline(1.2, 26.4, 37.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ }+ shell.A.at(sideM, () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], sideRecess, -0.8, 0, { noOuterSides: true, noBack: true, sr: 0.62 }));+ shell.A.at(sideM, () => shell.F.at(sideM, () => sideRecess.forEach((o) => addRecess(shell.A, shell.F, o, 0.52))));+ shell.D.at(sideM, () => { for (const y of [19.2, WF.kingTop - 0.34, WF.galY - 0.34]) { addStain(shell.D, -tw, tw, y, 0.02, 3.2); addStain(shell.D, -tw, tw, y, 0.03, 0.8, 'soft'); } addStain(shell.D, -tw, tw, 5.2, 0.03, 5.2, 'ground'); });+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(180), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(-90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ // string courses and tall blind windows on the outer side+ shell.F.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => {+ for (const y of [1.0, 19.6, WF.kingTop, WF.galY]) shell.F.sweep([[-tw, y], [tw, y]], stringSection(0.3, 0.26), { caps: false });+ addTracery(shell.F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ shell.F.sweep(archOutline(cw, cSill, cSp, cRise, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ addGable(shell.F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 3, lod: 0 });+ addTracery(shell.F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 });+ shell.F.sweep(archOutline(...windowArgs.aisle, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ });+ // The tower's two outer corners: one diagonal pier each, from the ground to the foot of the turret it carries+ const cornerStages = [{ y0: 0, y1: 13, w: 2.0, f: 3.6 }, { y0: 14.4, y1: 27, w: 1.9, f: 2.8 }, { y0: 28.4, y1: yTop - 1, w: 1.78, f: 2.05 }];+ for (const [zc, turn] of [[0, 45], [-2 * tw, 135]]) {+ const m = M.of(M.T(tw, 0, zc), M.Ry(turn));+ shell.A.at(m, () => shell.F.at(m, () => addPier(shell, cornerStages, { back: 2, flat: true })));+ shell.F.at(m, () => shell.F.box(-2.14, 2.14, 0, 0.74, -2, 3.74, { noBot: true }));+ shell.D.at(M.of(m, M.T(0, 0, 3.6)), () => { addStain(shell.D, -2, 2, 13, 0.02, 3.2); addStain(shell.D, -2, 2, 13.02, 0.03, 0.8, 'soft'); addStain(shell.D, -2, 2, 5, 0.03, 4.3, 'ground'); });+ if (zc === 0) shell.F.at(M.of(m, M.T(0, 0, 3.6)), () => addNicheFrame(shell.F, 0, 6.1, 11, 1.42));+ }+ for (const s of [-1, 1]) {+ const cx = s * tcx;+ placeSet('Tower_Shell', shell, [cx, 0, 0], 0, gWest, { scale: [s, 1, 1] });+ // Set just proud of the wall face: these towers are solid here, so the glass is seen from outside only+ place('Tower_SideGlass_High' + s, glassOutside(twinGlass[2]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ place('Tower_SideGlass_Low' + s, glassOutside(SHARED.stories[1]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ }+ const face = buildBelfryFace(), corner = buildTowerCorner();+ const cap = new Batch(TILE.lead), capG = new Batch(TILE.gold);+ cap.lathe([[5.9, P.towerTop - 2.5], [0.2, P.towerTop + 4.8]], 8, { faceted: true, phase: PI / 8 });+ capG.at(M.T(0, P.towerTop + 4.7, 0), () => addFinial(capG, 2.4, 1));+ for (const s of [-1, 1]) {+ const gT = group('Tower_' + (s > 0 ? 'North' : 'South'), gWest), cx = s * tcx, cz = -tw;+ for (let i = 0; i < 4; i++) {+ placeSet('Belfry_Face' + i, face, [cx, 0, cz], i * 90, gT);+ const ax = cx + (i === 0 || i === 3 ? 1 : -1) * tw, az = cz + (i < 2 ? 1 : -1) * tw;+ placeSet('Tower_Turret' + i, corner, [ax, 0, az], i * 90, gT);+ // the great corner pinnacles carry the tower to its full height+ const yT = P.towerTop - 1.3 + 0.4, hP = P.spireTip - yT;+ place('Tower_Pinnacle' + i, largePinnacle(), MAT.fine, { pos: [ax, yT, az], scale: [1.16, hP / 10, 1.16], parent: gT });+ }+ place('Tower_Cap_Lead', cap, MAT.lead, { pos: [cx, 0, cz], parent: gT });+ place('Tower_Cap_Gilt', capG, MAT.gold, { pos: [cx, 0, cz], parent: gT });+ }+ // ---- Inner face: blind niches with statues, a gallery, the back of the doors ----+ const inner = mkBatches();+ inner.F.at(M.of(M.T(0, 0, -WF.wallT), M.Ry(180)), () => {+ inner.F.sweep([[-13.1, 10.2], [13.1, 10.2]], stringSection(0.34, 0.34), { caps: false });+ inner.F.sweep([[-6.5, P.triforiumSill], [6.5, P.triforiumSill]], stringSection(0.34, 0.3), { caps: false });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) addNicheFrame(inner.F, s * (3.65 + i * 1.5), 3.2 + 0, 8.8, 1.2);+ inner.F.sweep(ringPts(0, WF.roseY, WF.roseR + 0.3, 32), traceryBar(0.42, 0.5), { closed: true });+ addBlindArcade(inner.F, -6.5, 6.5, 11, 15.2, 8, { bar: 0.14, depth: 0.2 });+ });+ placeSet('West_InnerFace', inner, [0, 0, 0], 0, gWest);+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) placeFigure('Inner_Saint_' + (s > 0 ? 'N' : 'S') + i, SAINTS[i + (s > 0 ? 1 : 6)], { pos: [s * (3.65 + i * 1.5), 3.25, -WF.wallT - 0.28], rot: [0, 90, 0], scale: 1.32, parent: gWest, indoor: true });+}+function makeLancetRoundel(r) { const g = new Glass(55); g.raw(ringPts(0, 0, r + 0.05, 20), 'lead', -0.4, 0); gOculus(g, 0, 0, r, { ground: 'cobalt', inner: 'gold' }); return g.b; }++// ---- Transept fronts: one portal, a smaller rose, lancets, gable, corner turrets ----------+function buildTransFront(gT, s) {+ const fr = frame(s > 0 ? 0 : 180, LAY.xc, 0), hw = zaw + 0.9, zEnd = zn + (P.transeptBays + 1) * BAY, yTop = P.eaves;+ const set = TRANS.set || (TRANS.set = (() => {+ const q = mkBatches();+ const { A, F, G } = q, rY = 28.6, R = 4.5, w0 = WF.portalS.w0, dh = WF.portalS.ys - 0.9;+ const at = M.T(0, 0, zEnd);+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-hw, 0], [-w0 / 2, 0], [-w0 / 2, dh], [w0 / 2, dh], [w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]], [ringPts(0, rY, R, 32)], -0.9, 0, { noOuterSides: true, sr: 0.62 }));+ F.at(M.of(at, M.T(0, rY, 0.9)), () => { F.sweep(ringPts(0, 0, R + 0.3, 32), traceryBar(0.4, 0.5), { closed: true }); F.sweep(ringPts(0, 0, R + 0.72, 36), rollSection(0.18, 0.26, 1, 0.03, 2), { closed: true }); });+ q.D.at(M.of(at, M.T(0, 0, 0.9)), () => { for (const [a, c] of [[-hw, -12.3], [-6.9, 6.9], [12.3, hw]]) { addStain(q.D, a, c, 17.2, 0.02, 3.2); addStain(q.D, a, c, 17.2, 0.03, 0.8, 'soft'); } addStain(q.D, -hw, hw, 22.6, 0.02, 2.8, 'drip'); addStain(q.D, -hw, hw, 22.6, 0.03, 0.7, 'soft'); addStain(q.D, -hw, -w0 / 2, 5.2, 0.03, 5.2, 'ground'); addStain(q.D, w0 / 2, hw, 5.2, 0.03, 5.2, 'ground'); });+ // Row of lancets beneath the rose+ F.at(M.of(at, M.T(0, 0, 0.9)), () => {+ F.sweep([[-hw, 0.8], [hw, 0.8]], stringSection(0.42, 0.36), { caps: false });+ // This string runs up to the hood of each window head and stops there: it never crosses the tracery+ const hood = 9.6 - 1.62, outerHood = 9.6 + 1.62;+ F.sweep([[-hood, 17.4], [hood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ for (const t of [-1, 1]) F.sweep(t > 0 ? [[outerHood, 17.4], [hw, 17.4]] : [[-hw, 17.4], [-outerHood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ F.sweep([[-hw, 22.8], [hw, 22.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -8, 8, 18, 20.8, 8, { bar: 0.16, depth: 0.2 });+ for (const x of [-9.6, 9.6]) F.at(M.T(x, 0, 0.1), () => { addTracery(F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 }); F.sweep(archOutline(...windowArgs.aisle, 7), hexBar(0.22, 0.42), { closed: true, crease: 40, z: -0.1 }); });+ });+ // The inner face of the end wall: tiers of blind arcading and string courses+ F.at(M.of(at, M.Ry(180)), () => {+ F.sweep([[-zaw, 11], [zaw, 11]], stringSection(0.3, 0.26), { caps: false });+ F.sweep([[-zaw, 16.8], [zaw, 16.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -5.6, 5.6, 1.2, 7.8, 5, { bar: 0.16, depth: 0.2 });+ addBlindArcade(F, -zn, zn, 11.2, 14.8, 8, { bar: 0.12, depth: 0.16 });+ addBlindArcade(F, -zn, zn, 17, 20.6, 8, { bar: 0.12, depth: 0.16 });+ });+ // Gable closing the arm's roof, in front of the lead+ const yG = yTop, gW = 2 * (zn + 1.6), yR = LAY.ridge + 1;+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-gW / 2, yG], [gW / 2, yG], [0, yR]], [ringPts(0, yG + 3.4, 1.5, 12)], -0.9, 0, { noOuterSides: true, sr: 0.5 }));+ F.at(M.of(at, M.T(0, 0, 0.9)), () => { addGable(F, gW + 0.2, yR - yG, 0.02, 0.24, yG, { size: 0.82, finial: 0.1, crockets: 7, noField: true, lod: 1 }); F.sweep(ringPts(0, yG + 3.4, 1.55, 14), traceryBar(0.24, 0.42), { closed: true }); });+ G.at(M.of(at, M.T(0, yR, 1.12)), () => addFinial(G, 2.0, 1));+ F.at(M.of(at, M.T(0, 0, 1.0)), () => addParapet(F, -hw, hw, yTop - 0.1, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Each corner of the front carries one diagonal pier rising to its pinnacle, tied into both walls+ for (const sx of [-1, 1]) {+ const m = M.of(M.T(sx * hw, 0, zEnd + 0.9), M.Ry(sx * 45));+ A.at(m, () => F.at(m, () => addPier(q, [{ y0: 0, y1: 14.6, w: 1.7, f: 2.7 }, { y0: 15.8, y1: 30.6, w: 1.58, f: 2.05 }, { y0: 31.8, y1: yTop + 1, w: 1.45, f: 1.5 }], { back: 1.7, flat: true })));+ F.at(m, () => F.box(-1.84, 1.84, 0, 0.74, -1.7, 2.84, { noBot: true }));+ F.at(M.of(m, M.T(0, 0, 2.05)), () => addNicheFrame(F, 0, 16.2, 22, 1.5));+ q.D.at(M.of(m, M.T(0, 0, 2.7)), () => { addStain(q.D, -1.7, 1.7, 14.6, 0.02, 3); addStain(q.D, -1.7, 1.7, 14.62, 0.03, 0.8, 'soft'); addStain(q.D, -1.7, 1.7, 5, 0.03, 4.3, 'ground'); });+ }+ return q;+ })());+ placeSet('Transept_Front', set, [fr.ox, 0, fr.oz], fr.th, gT);+ // Portal (the same carving as the west side portals), statue niches, glass+ const gPortal = group('Transept_Portal', gT);+ gPortal.position.set(...fr.pos(0, 0, zEnd + 0.9)); gPortal.rotation.y = rad(fr.th);+ placePortal(gPortal, 'Portal_Transept', { x: 0 }, SHARED.sidePortal, { light: 'all' });+ place('Transept_Rose_Tracery', roseTracery(), MAT.fine, { pos: fr.pos(0, 28.6, zEnd + 0.45), rot: fr.rot(0), scale: [4.5 / WF.roseR, 4.5 / WF.roseR, 1], parent: gT });+ for (const sx of [-1, 1]) place('Transept_Pinnacle', largePinnacle(), MAT.fine, { pos: fr.pos(sx * hw, yTop + 1, zEnd + 0.9), scale: [1.05, 1.1, 1.05], parent: gT });+ placeGlass('Transept_Rose', SHARED.roseGlass, fr.pos(0, 28.6, zEnd + 0.45), fr.th + 180, gT, false, 4.5 / WF.roseR);+ const low = SHARED.stories[0];+ for (const sx of [-1, 1]) {+ // Aisle-end windows: the same panes on the inner and outer faces of the end wall+ place('Transept_EndWindow_In', low, MAT.glassIn, { pos: fr.pos(sx * 9.6, 0, zEnd - 0.01), rot: fr.rot(180), parent: gT });+ place('Transept_EndWindow_Out', glassOutside(low), MAT.glassOutWin, { pos: fr.pos(sx * 9.6, 0, zEnd + 0.92), rot: fr.rot(180), scale: [1, 1, -1], parent: gT });+ const dz = Math.SQRT1_2 * 2.37;+ placeFigure('Transept_Corner_Saint' + sx, SAINTS[sx > 0 ? 2 : 4], { pos: fr.pos(sx * (hw + dz), 16.25, zEnd + 0.9 + dz), rot: fr.rot(-90 + sx * 45), scale: 1.42, parent: gT, lod: 0 });+ }+}+const TRANS = {};++// ---- The east end: raised sanctuary, high altar with a gilded retable, oak choir stalls ------+function buildSanctuary() {+ const g = group('Sanctuary'), set = mkBatches(), { F, G, O } = set, { xa, xChoirW } = LAY;+ // Three broad steps and the altar, all moulded+ for (let i = 0; i < 3; i++) { const r = 5 - i * 0.7; F.at(M.T(xa + 1, 0, 0), () => F.lathe([[r, i * 0.2], [r, (i + 1) * 0.2], [0.01, (i + 1) * 0.2]], 14, { faceted: true, phase: PI / 14 })); }+ const y0 = 0.6, ax = xa + 1;+ F.at(M.T(ax, y0, 0), () => {+ F.box(-0.8, 0.8, 0, 1.0, -1.7, 1.7, {});+ F.box(-0.92, 0.92, 1.0, 1.12, -1.84, 1.84, { sb: 0.5 });+ for (const s of [-1, 1]) for (const t of [-1, 0, 1]) F.at(M.T(s * 0.82, 0, t * 1.1), () => F.lathe([[0.12, 0], [0.07, 0.12], [0.07, 0.88], [0.13, 1.0]], 6));+ });+ // A carved retable behind the altar: five stone niches under gables, each with its figure, only the arches gilded+ const retM = M.of(M.T(ax - 1.0, y0 + 1.12, 0), M.Ry(90));+ F.at(retM, () => {+ F.box(-2.46, 2.46, -0.26, 0, -0.2, 0.14, {});+ for (let i = -2; i <= 2; i++) {+ const x = i * 0.96, h = i === 0 ? 2.2 : 1.56;+ F.box(x - 0.47, x + 0.47, 0, h - 0.3, -0.2, -0.1, {});+ addGable(F, 0.96, i === 0 ? 0.8 : 0.66, 0.04, 0.06, h - 0.32, { size: 0.15, crockets: 2, finial: 0.24, solid: true, lod: 0 });+ F.at(M.T(x - 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ if (i === 2) F.at(M.T(x + 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ F.at(M.T(x - 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ if (i === 2) F.at(M.T(x + 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ }+ });+ G.at(retM, () => { for (let i = -2; i <= 2; i++) { const x = i * 0.96, h = i === 0 ? 2.2 : 1.56; G.sweep(pointedArc(0.82, 0.52, 4).map((p) => [x + p[0], h - 0.84 + p[1]]), hexBar(0.04, 0.05), { z: 0.03, caps: false }); } });+ const altarSaints = [['apostle', 0.56], ['bishop', 0.56], ['seatedVirgin', 0.74], ['king', 0.56], ['queen', 0.56]];+ altarSaints.forEach((q, n) => placeFigure('Retable_Figure' + n, q[0], { pos: [ax - 1.04, y0 + 1.14, -(n - 2) * 0.96], rot: [0, 0, 0], scale: q[1], parent: g, indoor: true, lod: q[0] === 'seatedVirgin' ? 1 : 0 }));+ // Candlesticks+ for (const t of [-1.5, -0.9, -0.3, 0.3, 0.9, 1.5]) G.at(M.T(ax - 0.2, y0 + 1.12, t), () => { G.lathe([[0.1, 0], [0.04, 0.06], [0.025, 0.42], [0.06, 0.46], [0.03, 0.5]], 6); F.at(M.T(ax - 0.2, y0 + 1.62, t), () => F.lathe([[0.022, 0], [0.022, 0.34], [0.002, 0.36]], 5, { s: 1.6 })); });+ // Choir stalls: stepped oak benches, moulded backs and a carved canopy rail+ const x0 = xa + 5.5, x1 = xChoirW - 1.2;+ for (const s of [-1, 1]) {+ const z0 = s * 5.0;+ O.box(x0, x1, 0, 0.32, z0 - 0.9, z0 + 0.9, { s: 0.42 });+ O.box(x0, x1, 0.32, 0.5, z0 + (s > 0 ? 0.0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.46 });+ O.box(x0, x1, 0, 2.7, z0 + (s > 0 ? 0.8 : -0.9), z0 + (s > 0 ? 0.9 : -0.8), { s: 0.4 });+ const n = Math.floor((x1 - x0) / 1.5);+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * (x1 - x0) / n;+ O.box(x - 0.045, x + 0.045, 0, 0.98, z0 + (s > 0 ? 0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.5 });+ O.box(x - 0.03, x + 0.03, 0.98, 2.72, z0 + (s > 0 ? 0.74 : -0.8), z0 + (s > 0 ? 0.8 : -0.74), { s: 0.48 });+ O.box(x - 0.08, x + 0.08, 2.72, 3.28, z0 + (s > 0 ? 0.56 : -0.86), z0 + (s > 0 ? 0.86 : -0.56), { s: 0.46, taper: 0.05 });+ }+ O.box(x0, x1, 2.7, 2.84, z0 + (s > 0 ? 0.3 : -0.9), z0 + (s > 0 ? 0.9 : -0.3), { s: 0.44 });+ }+ placeSet('Sanctuary', set, [0, 0, 0], 0, g);+}++// ==================================================================================+// THE GROUND. One named group, separate from the building and left out of its light, so it can simply be deleted+// when the cathedral is placed in a town. All of it stands a few centimetres above y = 0.+// ==================================================================================+const GROUND = { flagY: 0.03, earthY: 0.015, flags: 20, earth: 33, stepUp: 0.14 };+function footprintDistance(x, z) { // signed distance in plan to the outer walls of the building+ const { xa, xW, xTowerE, xc } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const box = (x0, x1, z0, z1) => { const dx = Math.max(x0 - x, x - x1), dz = Math.max(z0 - z, z - z1); return dx <= 0 && dz <= 0 ? Math.max(dx, dz) : Math.sqrt(Math.max(dx, 0) ** 2 + Math.max(dz, 0) ** 2); };+ return Math.min(box(xa, xW, -14, 14), box(xTowerE, xW, -WF.halfW, WF.halfW), box(xc - 14, xc + 14, -zEnd, zEnd), Math.sqrt((x - xa) * (x - xa) + z * z) - (CHEV.zc + CHEV.Rc + 0.5));+}+function buildGround() {+ const g = group('Ground'), { xa, xW, xc, xTowerE } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const fl = new Batch(TILE.flag), ea = new Batch(TILE.earth), st = new Batch(TILE.flag), gr = new Batch(1); gr.noColor = true;+ fl.noVoxel = ea.noVoxel = st.noVoxel = true;+ const wob = (x, z) => 2.2 * Math.sin(x * 0.31 + 1.3) * Math.cos(z * 0.27) + 1.4 * Math.sin(x * 0.13 + z * 0.19);+ const cell3 = (b, x0, z0, s, y) => { const q = [[x0, z0], [x0, z0 + s], [x0 + s, z0 + s], [x0 + s, z0]].map((p) => b.v(p[0], y(p[0], p[1]), p[1], 0, 1, 0, p[0], p[1], 1)); b.quad(q[0], q[1], q[2], q[3]); };+ // Worn flags from the walls out for about 20 m. Their edge breaks up slab by slab on the earth; nothing is left under the church floor+ const flagKeep = (x, z) => { const d = footprintDistance(x, z); return d > -2.5 && d < GROUND.flags + wob(x, z); };+ for (let gx = Math.floor((xa - 62) / 3); gx * 3 < xW + 62; gx++) for (let gz = -24; gz < 24; gz++) {+ const x0 = gx * 3, z0 = gz * 3, d = footprintDistance(x0 + 1.5, z0 + 1.5), lim = GROUND.flags + wob(x0 + 1.5, z0 + 1.5);+ if (Math.abs(d - lim) < 3.2 && d > 5) { for (const [a, c] of [[0, 0], [0, 1.5], [1.5, 0], [1.5, 1.5]]) if (flagKeep(x0 + a + 0.75, z0 + c + 0.75)) cell3(fl, x0 + a, z0 + c, 1.5, () => GROUND.flagY); } else if (flagKeep(x0 + 1.5, z0 + 1.5)) cell3(fl, x0, z0, 3, () => GROUND.flagY);+ }+ // Rough grass and packed earth under and beyond them, its edge dissolving so it sits on any ground plane without a seam+ ea.fade = (x, z) => sstep(GROUND.earth + 8, GROUND.earth - 3, footprintDistance(x, z) - wob(z, x));+ for (let gx = Math.floor((xa - 62) / 4); gx * 4 < xW + 62; gx++) for (let gz = -18; gz < 18; gz++) {+ const x0 = gx * 4, z0 = gz * 4, d = footprintDistance(x0 + 2, z0 + 2);+ if (d > GROUND.flags - 8 && d < GROUND.earth + 11) cell3(ea, x0, z0, 4, () => GROUND.earthY);+ }+ // A step or two up to every door: a landing that runs in to the door between the jambs, and a lower step in front of it+ const steps = (m, half, zF) => st.at(m, () => {+ st.box(-half - 0.2, half + 0.2, GROUND.flagY, GROUND.flagY + 2 * GROUND.stepUp, -0.2, zF + 0.75, { noBot: true });+ st.box(-half - 0.62, half + 0.62, GROUND.flagY, GROUND.flagY + GROUND.stepUp, zF + 0.75, zF + 1.5, { noBot: true });+ });+ const west = M.of(M.T(xW, 0, 0), M.Ry(90)), cP = WF.portalC, sP = WF.portalS, outer = (c) => (c.w0 + 2 * c.orders * c.dx) / 2;+ steps(west, outer(cP), cP.orders * cP.dz);+ for (const s of [-1, 1]) steps(M.of(west, M.T(s * sP.x, 0, 0)), outer(sP), sP.orders * sP.dz);+ for (const s of [0, 180]) steps(M.of(M.T(xc, 0, 0), M.Ry(s), M.T(0, 0, zEnd)), outer(sP), sP.orders * sP.dz);+ // Dark grime where the walls meet the ground: a soft band lying on the flags along every wall foot+ const strip = (ax, az, bx, bz, w) => {+ const R = DECAL_UV.soft, dx = bx - ax, dz = bz - az, l = Math.sqrt(dx * dx + dz * dz) || 1, nx = dz / l, nz = -dx / l, y = GROUND.flagY + 0.012, n = Math.max(1, Math.round(l / 5)), span = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = i / n, c = (i + 1) / n, u0 = R[0] + (i % 3) * span * 0.22, u1 = u0 + span * 0.3, pa = [ax + dx * a, az + dz * a], pc = [ax + dx * c, az + dz * c];+ const q = [gr.v(pa[0], y, pa[1], 0, 1, 0, u0, R[1], 1), gr.v(pa[0] + nx * w, y, pa[1] + nz * w, 0, 1, 0, u0, R[3], 1), gr.v(pc[0] + nx * w, y, pc[1] + nz * w, 0, 1, 0, u1, R[3], 1), gr.v(pc[0], y, pc[1], 0, 1, 0, u1, R[1], 1)];+ gr.quad(q[0], q[1], q[2], q[3]);+ }+ };+ // every strip runs with the wall on its left as seen from above, so its band falls outward+ const loop = (pts, w) => { for (let i = 0; i < pts.length - 1; i++) strip(pts[i][0], pts[i][1], pts[i + 1][0], pts[i + 1][1], w); };+ const hx = WF.halfW, xE = xc + 14, xCE = xc - 14;+ loop([[xW, hx], [xTowerE, hx], [xTowerE, 14], [xE, 14], [xE, zEnd], [xCE, zEnd], [xCE, 14], [xa, 14]], 1.8);+ loop([[xa, -14], [xCE, -14], [xCE, -zEnd], [xE, -zEnd], [xE, -14], [xTowerE, -14], [xTowerE, -hx], [xW, -hx], [xW, hx]], 1.8);+ for (let k = 1; k < P.apseSides; k++) { const f0 = frame(-k * CHEV.d, xa, 0), a = f0.pos(-0.98, 0, BUTTRESS.zB + 0.2), c = f0.pos(0.98, 0, BUTTRESS.zB + 0.2); strip(a[0], a[2], c[0], c[2], 1.3); }+ for (let k = 0; k < P.apseSides; k++) { const r = CHEV.zc + CHEV.Rc + 0.55, n = 5; for (let i = 0; i < n; i++) { const f0 = frame(-(k + 0.5) * CHEV.d, xa, 0), a0 = rad(-108 + i * 216 / n), a1 = rad(-108 + (i + 1) * 216 / n), R2 = CHEV.Rc + 0.55, p = f0.pos(R2 * Math.sin(a0), 0, CHEV.zc + R2 * Math.cos(a0)), q = f0.pos(R2 * Math.sin(a1), 0, CHEV.zc + R2 * Math.cos(a1)); strip(p[0], p[2], q[0], q[2], 1.2); } }+ place('Ground_Flagstones', fl, MAT.flag, { parent: g });+ place('Ground_Earth', ea, MAT.earth, { parent: g });+ place('Ground_Steps', st, MAT.flag, { parent: g });+ place('Ground_Grime', gr, MAT.decal, { parent: g });+}++function buildCathedral() {+ buildBody();+ buildWestFront();+ const gTN = ROOT.children.find((c) => c.name === 'Transept_North'), gTS = ROOT.children.find((c) => c.name === 'Transept_South');+ buildTransFront(gTN, 1);+ buildTransFront(gTS, -1);+ buildSanctuary();+ buildGround();+}++function previewStatues() {+ const names = P.statueTest;+ names.forEach((n, i) => placeFigure('Fig_' + n, n, { pos: [0, 0, (i - (names.length - 1) / 2) * 1.0] }));+ const base = new Batch(TILE.ashlar); base.box(-3, 3, -0.4, 0, -6, 6, { s: 0.8 }); place('Ground', base, MAT.ashlar);+}++// ==================================================================================+// BAKED LIGHT. The viewer casts no shadows, so the shadows are computed here. Every opaque surface is voxelized+// into the world, then each vertex looks at the sun, the sky and the rain:+// - rays towards the sun give the low raking light and the cast shadows;+// - rays over the hemisphere give the depth of portals and galleries, and tell inside from outside;+// - a ray straight up finds what the rain never reaches, where seven centuries of soot have stayed.+// A batch placed many times can have one set of vertex colours per side of the building, within a byte budget.+// Sets beyond the budget share the sunniest colours under a darker material.+// ==================================================================================+const SKY_RAYS = (() => {+ const r = [[0, 1, 0, 1]];+ for (let i = 0; i < 4; i++) { const a = i * TAU / 4 + 0.4, e = rad(42); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 1]); }+ for (let i = 0; i < 5; i++) { const a = i * TAU / 5 + 0.1, e = rad(9); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 0]); }+ return r;+})();+// Large meshes of moulding keep their shape with a quarter fewer triangles: the bytes are better spent up close+async function reduceBatches(share = 0.74, repeatedShare = 0.62, minTris = 2000) {+ const seen = new Map(), uses = new Map();+ for (const e of PLACED) uses.set(e.batch, (uses.get(e.batch) || 0) + 1);+ for (const e of PLACED) {+ const m = e.mat;+ if (e.batch.isFigure || e.batch.noReduce || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.glassOutWin || m === MAT.glassRose || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave || m === MAT.flag || m === MAT.earth) continue;+ let nb = seen.get(e.batch);+ if (nb === undefined) {+ const tris = e.batch.I.length / 3, s = uses.get(e.batch) >= 8 ? repeatedShare : share; // a mesh repeated down the whole building costs the most+ nb = tris > minTris ? await lightCopy(e.batch, Math.round(tris * s)) : null;+ if (nb) { nb.noColor = e.batch.noColor; nb.pierShaft = e.batch.pierShaft; }+ seen.set(e.batch, nb);+ }+ if (nb) { e.batch = nb; e.part.geometry = nb.toGeo(); }+ }+}+function samplesOf(b) {+ if (b.samples) return b.samples;+ const P0 = b.P, out = [], lim = 0.32;+ for (let i = 0; i < P0.length; i++) out.push(P0[i]);+ for (let t = 0; t < b.I.length; t += 3) {+ const a = b.I[t] * 3, c = b.I[t + 1] * 3, d = b.I[t + 2] * 3;+ const e1 = len3(P0[a] - P0[c], P0[a + 1] - P0[c + 1], P0[a + 2] - P0[c + 2]), e2 = len3(P0[c] - P0[d], P0[c + 1] - P0[d + 1], P0[c + 2] - P0[d + 2]), e3 = len3(P0[d] - P0[a], P0[d + 1] - P0[a + 1], P0[d + 2] - P0[a + 2]), m = Math.max(e1, e2, e3);+ if (m <= lim) continue;+ const n = Math.ceil(m / lim);+ for (let i = 0; i <= n; i++) for (let j = 0; j <= n - i; j++) { const u = i / n, v = j / n, w = 1 - u - v; out.push(P0[a] * u + P0[c] * v + P0[d] * w, P0[a + 1] * u + P0[c + 1] * v + P0[d + 1] * w, P0[a + 2] * u + P0[c + 2] * v + P0[d + 2] * w); }+ }+ b.samples = new Float32Array(out);+ return b.samples;+}+function bakeLight() {+ ROOT.updateMatrixWorld(true);+ // The world follows the plan: from beyond the chapels to beyond the west front. A spire tip above the top simply casts no shadow up there+ const CELL = 0.4, X0 = LAY.xa - 27.8, Y0 = -2, Z0 = -36, NX = Math.ceil((P.xWest + 12 - X0) / CELL), NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;+ const grid = new Uint8Array(NX * NY * NZ), coarse = new Uint8Array(CX * CY * CZ);+ const SUN = (() => { const v = P.sun, l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();+ const SUN2 = [[SUN[0] + 0.06, SUN[1], SUN[2] + 0.05], [SUN[0] - 0.05, SUN[1] + 0.05, SUN[2] - 0.05]].map((v) => { const l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; });+ for (const e of PLACED) {+ if (e.mat.transparent || e.batch.noVoxel) continue;+ const S = samplesOf(e.batch), m = e.part.matrixWorld.elements, shaft = !!e.batch.pierShaft;+ for (let i = 0; i < S.length; i += 3) {+ const x = S[i], y = S[i + 1], z = S[i + 2], gi = ((m[0] * x + m[4] * y + m[8] * z + m[12] - X0) / CELL) | 0, gj = ((m[1] * x + m[5] * y + m[9] * z + m[13] - Y0) / CELL) | 0, gk = ((m[2] * x + m[6] * y + m[10] * z + m[14] - Z0) / CELL) | 0;+ if (gi >= 0 && gi < NX && gj >= 0 && gj < NY && gk >= 0 && gk < NZ) {+ const q = gi + NX * (gj + NY * gk); // 2 marks a voxel that holds only a pier shaft: solid to the stone, tested exactly by the window light+ if (!shaft) grid[q] = 1; else if (!grid[q]) grid[q] = 2;+ coarse[(gi >> 3) + CX * ((gj >> 3) + CY * (gk >> 3))] = 1;+ }+ }+ }+ // How enclosed a point is: the solid voxels averaged into 1.6 m cells and blurred, read back with trilinear filtering.+ // It is a smooth function of position, so light taken from it cannot have hard edges however few vertices a surface has.+ const DX = (NX >> 2) + 2, DY = (NY >> 2) + 2, DZ = (NZ >> 2) + 2, dens = new Float32Array(DX * DY * DZ);+ for (let k = 0, q = 0; k < NZ; k++) for (let j = 0; j < NY; j++) for (let i = 0; i < NX; i++, q++) if (grid[q]) dens[(i >> 2) + DX * ((j >> 2) + DY * (k >> 2))] += 1 / 64;+ { const tmp = new Float32Array(dens.length), steps = [1, DX, DX * DY];+ for (let pass = 0; pass < 3; pass++) for (const st of steps) {+ tmp.set(dens);+ for (let q = st; q < dens.length - st; q++) dens[q] = 0.25 * tmp[q - st] + 0.5 * tmp[q] + 0.25 * tmp[q + st];+ } }+ const enclosed = (x, y, z) => {+ const fx = clamp((x - X0) / (CELL * 4), 0, DX - 1.001), fy = clamp((y - Y0) / (CELL * 4), 0, DY - 1.001), fz = clamp((z - Z0) / (CELL * 4), 0, DZ - 1.001);+ const i = fx | 0, j = fy | 0, k = fz | 0, a = fx - i, c = fy - j, d = fz - k, q = i + DX * (j + DY * k), P1 = 1, P2 = DX, P4 = DX * DY;+ const lo = (dens[q] * (1 - a) + dens[q + P1] * a) * (1 - c) + (dens[q + P2] * (1 - a) + dens[q + P2 + P1] * a) * c;+ const hi = (dens[q + P4] * (1 - a) + dens[q + P4 + P1] * a) * (1 - c) + (dens[q + P4 + P2] * (1 - a) + dens[q + P4 + P2 + P1] * a) * c;+ return lo * (1 - d) + hi * d;+ };+ // Distance to the first hit, or 1e9 if the ray leaves the world; capped rays return their cap.+ // A ray from ground beyond the voxels first flies in to them, so the building still shadows the far flagstones.+ const X1 = X0 + NX * CELL, Y1 = Y0 + NY * CELL, Z1 = Z0 + NZ * CELL;+ const march = (ox, oy, oz, dx, dy, dz, cap) => {+ let t = 0; const st = CELL * 0.8, far = CELL * CK * 0.6;+ if (ox < X0 || ox >= X1 || oy < Y0 || oy >= Y1 || oz < Z0 || oz >= Z1) {+ let t0 = 0, t1 = cap;+ for (const [o, d, lo, hi] of [[ox, dx, X0, X1], [oy, dy, Y0, Y1], [oz, dz, Z0, Z1]]) {+ if (Math.abs(d) < 1e-9) { if (o < lo || o >= hi) return 1e9; continue; }+ const a = (lo - o) / d, b = (hi - o) / d; t0 = Math.max(t0, Math.min(a, b)); t1 = Math.min(t1, Math.max(a, b));+ }+ if (t0 >= t1) return 1e9;+ t = t0 + 0.01;+ }+ while (t < cap) {+ const i = ((ox + dx * t - X0) / CELL) | 0, j = ((oy + dy * t - Y0) / CELL) | 0, k = ((oz + dz * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return 1e9;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)]) return t;+ t += st;+ }+ return cap;+ };+ const solidAt = (x, y, z) => { const i = ((x - X0) / CELL) | 0, j = ((y - Y0) / CELL) | 0, k = ((z - Z0) / CELL) | 0; return i >= 0 && i < NX && j >= 0 && j < NY && k >= 0 && k < NZ && grid[i + NX * (j + NY * k)] !== 0; };+ // A vertex's light: [r,g,b] multipliers, plus how much of it was outdoors+ const light = { r: 1, g: 1, b: 1, out: 0, ndl: 0 };+ let sootFloor = 0.56, dampK = 1, indoorK = 1;+ const lightAt = (x, y, z, nx, ny, nz) => {+ // leave the voxels of the surface itself, then look around+ let ox = x + nx * CELL * 1.2, oy = y + ny * CELL * 1.2, oz = z + nz * CELL * 1.2, buried = false;+ if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 2.4; oy = y + ny * CELL * 2.4; oz = z + nz * CELL * 2.4; if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 3.6; oy = y + ny * CELL * 3.6; oz = z + nz * CELL * 3.6; buried = solidAt(ox, oy, oz); } }+ let esc = 0, skyW = 0, sky = 0, zen = 1e9;+ // A ray counts as sky only if it truly leaves the world: a long look down the nave is still indoors+ if (!buried) for (let q = 0; q < 5; q++) {+ const R = SKY_RAYS[q], d = march(ox, oy, oz, R[0], R[1], R[2], 200);+ if (q === 0) zen = d;+ const w = Math.max(0.12, R[0] * nx + R[1] * ny + R[2] * nz + 0.35);+ skyW += w; if (d >= 1e9) { sky += w; esc++; }+ }+ // The underside of a cornice sees no sky above it but is still outdoors: only there is a long look sideways worth its cost+ if (!buried && esc === 0 && ny < -0.2) for (let q = 5; q < SKY_RAYS.length && esc === 0; q++) { const R = SKY_RAYS[q]; if (march(ox, oy, oz, R[0], R[1], R[2], 90) >= 1e9) esc++; }+ const ao = clamp(1 - enclosed(x + nx, y + ny, z + nz) * 2.6), vis = skyW ? sky / skyW : 0, outdoors = esc >= 1;+ let r, g, b;+ if (outdoors) {+ const ndl = nx * SUN[0] + ny * SUN[1] + nz * SUN[2];+ let sunV = 0;+ if (ndl > 0 && !buried) {+ if (march(ox, oy, oz, SUN[0], SUN[1], SUN[2], 200) >= 1e9) sunV += 0.5;+ if (march(ox, oy, oz, SUN2[0][0], SUN2[0][1], SUN2[0][2], 200) >= 1e9) sunV += 0.25;+ if (march(ox, oy, oz, SUN2[1][0], SUN2[1][1], SUN2[1][2], 200) >= 1e9) sunV += 0.25;+ }+ const amb = 0.2 + 0.44 * vis * (0.55 + 0.45 * ao), s = sunV * Math.max(0, ndl) * 1.02;+ r = amb * 0.84 + s * 0.98; g = amb * 0.88 + s * 0.9; b = amb * 0.98 + s * 0.74;+ // What the rain never touches keeps its soot; what it washes stays pale. Gentle on long walls, whose+ // few vertices would smear it: the crisp streaks under their ledges are decals.+ const shelter = zen < 6 ? 1 - zen / 6 : 0, soot = lerp(1, sootFloor, shelter * (1 - Math.max(0, ny)));+ r *= soot; g *= soot * 0.985; b *= soot * 0.96;+ // Rising damp: darker and greener towards the ground, a slow mottling over the height of the wall+ const damp = lerp(1, sstep(0, 9, y), dampK), mot = 0.93 + 0.07 * Math.sin(x * 0.53 + y * 0.31) * Math.sin(z * 0.47 - y * 0.23);+ r *= lerp(0.56, 1, damp) * mot; g *= lerp(0.64, 1, damp) * mot; b *= lerp(0.46, 1, damp) * mot;+ light.out = 1; light.ndl = ndl;+ } else {+ // Inside: warm stone, dark low in the aisles, lifting into the vault. Every term is a smooth function of position,+ // so the light has only gradients: the aisles of the nave, of the transept arms and round the apse fade in softly+ const low = sstep(P.aisleCrown + 2.5, P.aisleCrown - 0.5, y), axis = zn - 0.1, dxc = Math.abs(x - LAY.xc), arm = sstep(16, 13, dxc);+ const naveA = sstep(axis, axis + 2.4, Math.abs(z)) * (1 - arm), armA = sstep(axis, axis + 2.4, dxc) * arm * sstep(13, 16, Math.abs(z));+ const apse = sstep(LAY.xa + 1.5, LAY.xa - 1.5, x), ambA = sstep(axis, axis + 2.4, Math.sqrt((x - LAY.xa) * (x - LAY.xa) + z * z)) * apse;+ const s = lerp(0.32, 0.86, sstep(0.5, 34, y)) * lerp(0.5, 1, ao) * lerp(1, 0.76, Math.max(naveA * (1 - apse), armA, ambA) * low) * indoorK;+ r = s * 1.06; g = s * 0.87; b = s * 0.64;+ light.out = 0; light.ndl = 0;+ }+ light.r = r; light.g = g; light.b = b;+ return light;+ };+ // ---- group placements of each batch by which way they face the sun ----+ const perBatch = new Map();+ for (const e of PLACED) { if (e.batch.noColor || e.batch.isFigure || (e.mat.transparent && !e.batch.fade)) continue; let l = perBatch.get(e.batch); if (!l) { l = []; perBatch.set(e.batch, l); } l.push(e); }+ const entries = [];+ for (const [b, list] of perBatch) {+ const groups = new Map();+ for (const e of list) {+ const m = e.part.matrixWorld.elements, sx = len3(m[0], m[1], m[2]) || 1, sy = len3(m[4], m[5], m[6]) || 1, sz = len3(m[8], m[9], m[10]) || 1;+ // the sun in this placement's own frame (mirror included)+ const lx = (m[0] * SUN[0] + m[1] * SUN[1] + m[2] * SUN[2]) / (sx * sx), ly = (m[4] * SUN[0] + m[5] * SUN[1] + m[6] * SUN[2]) / (sy * sy), lz = (m[8] * SUN[0] + m[9] * SUN[1] + m[10] * SUN[2]) / (sz * sz);+ const key = Math.round(lx * 3) + ',' + Math.round(ly * 3) + ',' + Math.round(lz * 3);+ let g = groups.get(key); if (!g) { g = { key, list: [], L: [lx, ly, lz] }; groups.set(key, g); } g.list.push(e);+ }+ const gl = [...groups.values()];+ for (const g of gl) { // how much sun this side receives, by area of normals+ let s = 0; const nn = b.N; for (let i = 0; i < nn.length; i += 3) s += Math.max(0, nn[i] * g.L[0] + nn[i + 1] * g.L[1] + nn[i + 2] * g.L[2]);+ g.expo = s / Math.max(1, nn.length / 3);+ }+ gl.sort((a, c) => c.expo - a.expo);+ entries.push({ b, gl, wall: list[0].mat === MAT.ashlar ? 0 : 1 });+ }+ // Long walls get their own light for each side first: they carry the sense of sun and shade from a distance+ entries.sort((a, c) => a.wall - c.wall || a.b.count - c.b.count);+ let spent = 0, variants = 0, tinted = 0, vbaked = 0;+ const TINTS = new Map();+ const tintOf = (mat, tier) => {+ const key = mat.uuid + '|' + tier; let m = TINTS.get(key);+ if (!m) { m = mat.clone(); m.name = mat.name + (tier === 1 ? '_ShadeSide' : '_DeepShadeSide'); m.color.setRGB(tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.76 : 0.53); TINTS.set(key, m); }+ return m;+ };+ const bakeGroup = (b, g) => {+ const rep = g.list[Math.floor(g.list.length / 2)], m = rep.part.matrixWorld.elements, n = b.count, out = new Uint8Array(n * 3), PP = b.P, NN = b.N, CC = b.C;+ let outdoor = 0;+ const onGround = rep.mat === MAT.flag || rep.mat === MAT.earth;+ sootFloor = onGround ? 1 : rep.mat === MAT.ashlar ? 0.84 : rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0.9 : 0.56;+ dampK = onGround || rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0 : 1;+ indoorK = rep.mat === MAT.oak ? 2.2 : 1; // the oak was given its own dark tone when it was built+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, x = PP[i3], y = PP[i3 + 1], z = PP[i3 + 2], a = NN[i3], c = NN[i3 + 1], d = NN[i3 + 2];+ const wx = m[0] * x + m[4] * y + m[8] * z + m[12], wy = m[1] * x + m[5] * y + m[9] * z + m[13], wz = m[2] * x + m[6] * y + m[10] * z + m[14];+ let nx = m[0] * a + m[4] * c + m[8] * d, ny = m[1] * a + m[5] * c + m[9] * d, nz = m[2] * a + m[6] * c + m[10] * d; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1; nx /= l; ny /= l; nz /= l;+ const L = lightAt(wx, wy, wz, nx, ny, nz);+ outdoor += L.out;+ out[i3] = Math.min(255, CC[i3] * L.r); out[i3 + 1] = Math.min(255, CC[i3 + 1] * L.g); out[i3 + 2] = Math.min(255, CC[i3 + 2] * L.b);+ }+ // Each vertex is one noisy sample of light: blend neighbours on the same surface so no sample can smear a dark+ // triangle across the stone. Edges between planes keep their contrast.+ {+ const sum = new Float32Array(n * 3), w = new Float32Array(n), I = b.I, passes = rep.mat === MAT.ashlar ? 3 : 2;+ const link = (p, q) => { if (NN[p * 3] * NN[q * 3] + NN[p * 3 + 1] * NN[q * 3 + 1] + NN[p * 3 + 2] * NN[q * 3 + 2] < 0.8) return; w[p]++; w[q]++; for (let c = 0; c < 3; c++) { sum[p * 3 + c] += out[q * 3 + c]; sum[q * 3 + c] += out[p * 3 + c]; } };+ for (let pass = 0; pass < passes; pass++) {+ sum.fill(0); w.fill(0);+ for (let t = 0; t < I.length; t += 3) { link(I[t], I[t + 1]); link(I[t + 1], I[t + 2]); link(I[t + 2], I[t]); }+ for (let i = 0; i < n; i++) if (w[i]) for (let c = 0; c < 3; c++) out[i * 3 + c] = out[i * 3 + c] * 0.4 + 0.6 * sum[i * 3 + c] / w[i];+ }+ }+ vbaked += n;+ return { colors: out, outdoor: outdoor / Math.max(1, n) };+ };+ for (const { b, gl } of entries) {+ const base = b.toGeo(), cost = (gl.length - 1) * b.count * 12, full = gl.length === 1 || spent + cost <= P.lightVariantBytes;+ if (full) spent += cost;+ gl.forEach((g, gi) => {+ if (gi === 0 || full) {+ const R = bakeGroup(b, g);+ g.outdoor = R.outdoor;+ if (gi === 0 && b.fade) { // a surface that dissolves at its edge carries its opacity with its light+ const rgba = new Float32Array(b.count * 4);+ for (let i = 0; i < b.count; i++) { rgba[i * 4] = R.colors[i * 3] / 255; rgba[i * 4 + 1] = R.colors[i * 3 + 1] / 255; rgba[i * 4 + 2] = R.colors[i * 3 + 2] / 255; rgba[i * 4 + 3] = b.fade(b.P[i * 3], b.P[i * 3 + 2]); }+ base.setAttribute('color', new THREE.BufferAttribute(rgba, 4));+ return;+ }+ if (gi === 0) { base.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true)); return; }+ const v = new THREE.BufferGeometry();+ v.setAttribute('position', base.attributes.position); v.setAttribute('normal', base.attributes.normal); v.setAttribute('uv', base.attributes.uv);+ v.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true));+ if (base.attributes.tangent) v.setAttribute('tangent', base.attributes.tangent);+ v.setIndex(base.index);+ for (const e of g.list) e.part.geometry = v;+ variants++;+ } else {+ // over budget: same colours, a darker stone. Skipped for batches that are mostly indoors+ tinted++;+ if (gl[0].outdoor < 0.3) return;+ const ratio = g.expo / Math.max(1e-3, gl[0].expo);+ if (ratio >= 0.78) return;+ const tier = ratio >= 0.45 ? 1 : 2;+ for (const e of g.list) e.part.material = tintOf(e.mat, tier);+ }+ });+ }+ // ---- figures keep their carved occlusion; the niche they stand in decides how much daylight reaches them ----+ let figN = 0;+ for (const f of FIG_PLACED) {+ const m = f.part.matrixWorld.elements, s = len3(m[0], m[1], m[2]) || 1, fx = m[0] / s, fy = m[1] / s, fz = m[2] / s;+ let ox = m[12] + m[4] * 1.3, oy = m[13] + m[5] * 1.3, oz = m[14] + m[6] * 1.3;+ for (let i = 0; i < 12 && solidAt(ox, oy, oz); i++) { ox += fx * CELL; oy += fy * CELL; oz += fz * CELL; }+ const L = lightAt(ox - fx * 0.05, oy - fy * 0.05, oz - fz * 0.05, fx, fy, fz);+ // Each tone is the nearest to the light the stone around it received, so a figure sits in its wall. Every figure here stands outdoors:+ // one whose sky is cut off by a portal is in shade, not in the warm light of the nave (the nave's own figures never come through here)+ const lum = (L.r + L.g + L.b) / 3, facing = fx * SUN[0] + fy * SUN[1] + fz * SUN[2];+ let mat = lum > 0.7 ? MAT.figure : lum > 0.38 ? MAT.figureHalf : MAT.figureDeep;+ if (facing < -0.3 && mat === MAT.figure) mat = MAT.figureHalf;+ f.part.material = mat; if (f.head) f.head.material = mat;+ figN++;+ }+ const windowLight = bakeWindowLight({ SUN, grid, coarse, NX, NY, NZ, CX, CY, X0, Y0, Z0, CELL, CK, march });+ return { vertices: vbaked, variants, tinted, extraKB: Math.round(spent / 1024), figures: figN, windowLight };+}++// ==================================================================================+// THE LIGHT OF THE SOUTH WINDOWS, baked from the same sun as the stone. For each point of the floor, and each lit face of the south+// piers, a small cone of rays goes back towards P.sun. A ray must clear the piers and the stone indoors, pass the wall's reveal, find+// glass in a south aisle window, and then clear everything outside: gable, buttresses, flyers, pinnacles, towers, transepts.+// Light multiplies the stone it lands on, so each overlay carries that stone's own texture and keeps its pattern and its dark joints:+// how much light arrives sets the opacity and draws the window's shape; the panes set only the hue, so pale panes cannot make spots.+// The vertices lie only where light falls. The penumbra grows with distance, so far edges melt and near piers cut clean.+// ==================================================================================+const WINDOW_LIGHT = { cell: 0.33, coneDeg: 1.2, rays: 20, opacity: 0.88, floorLevel: 0.76, stoneLevel: 0.5, hueRadius: 5 };+const SUN_WINDOWS = [];+const inPoly = (pts, x, y) => { let c = false; for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) { const a = pts[i], b = pts[j]; if ((a[1] > y) !== (b[1] > y) && x < ((b[0] - a[0]) * (y - a[1])) / (b[1] - a[1]) + a[0]) c = !c; } return c; };+const FINE = 0.05; // metres per pixel of a window's cartoon+let TRACERY_MASK = null;+function traceryMask(nx, ny, w, y0) { // what the stone bars and the moulded frame cover, seen straight on+ if (TRACERY_MASK) return TRACERY_MASK;+ const t = new Batch(1), mask = new Uint8Array(nx * ny);+ addTracery(t, WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise, { bar: 0.25, depth: 0.42 });+ t.sweep(archOutline(WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ const Q = t.P, I = t.I;+ for (let f = 0; f < I.length; f += 3) {+ const ax = Q[I[f] * 3], ay = Q[I[f] * 3 + 1], bx = Q[I[f + 1] * 3], by = Q[I[f + 1] * 3 + 1], cx = Q[I[f + 2] * 3], cy = Q[I[f + 2] * 3 + 1];+ const d = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax); if (Math.abs(d) < 1e-7) continue;+ const i0 = Math.max(0, Math.floor((Math.min(ax, bx, cx) + w / 2) / FINE)), i1 = Math.min(nx - 1, Math.ceil((Math.max(ax, bx, cx) + w / 2) / FINE)), j0 = Math.max(0, Math.floor((Math.min(ay, by, cy) - y0) / FINE)), j1 = Math.min(ny - 1, Math.ceil((Math.max(ay, by, cy) - y0) / FINE));+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) {+ const px = (i + 0.5) * FINE - w / 2, py = (j + 0.5) * FINE + y0, e0 = ((bx - ax) * (py - ay) - (by - ay) * (px - ax)) / d, e1 = ((cx - bx) * (py - by) - (cy - by) * (px - bx)) / d, e2 = ((ax - cx) * (py - cy) - (ay - cy) * (px - cx)) / d;+ if (e0 >= 0 && e1 >= 0 && e2 >= 0) mask[i + nx * j] = 1;+ }+ }+ TRACERY_MASK = mask;+ return mask;+}+// The window as light passes through it, at 10 cm: G is how much of each cell is glass (lead and stone bars are not). H is the hue there:+// the panes in their real colours and order, each pane counted by how saturated it is, so the deep ruby and cobalt grounds lead and pale+// robes melt into them, then averaged over about a metre of window+function windowTransmission(batch) {+ if (batch.transmission) return batch.transmission;+ const w = WIN.aW, y0 = WIN.aSill, top = WIN.aSp + WIN.aRise, nx = Math.ceil(w / FINE), ny = Math.ceil((top - y0) / FINE), fine = new Uint8Array(nx * ny * 3), famRGB = {};+ for (const f of GLASS_FAMILIES) famRGB[f[0]] = f[1];+ const order = batch.panes.map((p, i) => [p, i]).sort((a, c) => a[0][2] - c[0][2] || a[1] - c[1]);+ for (const [pane] of order) {+ const pts = pane[0], rgb = famRGB[pane[1]] || [0, 0, 0];+ let bx0 = 1e9, bx1 = -1e9, by0 = 1e9, by1 = -1e9; for (const p of pts) { bx0 = Math.min(bx0, p[0]); bx1 = Math.max(bx1, p[0]); by0 = Math.min(by0, p[1]); by1 = Math.max(by1, p[1]); }+ const i0 = Math.max(0, Math.floor((bx0 + w / 2) / FINE)), i1 = Math.min(nx - 1, Math.ceil((bx1 + w / 2) / FINE)), j0 = Math.max(0, Math.floor((by0 - y0) / FINE)), j1 = Math.min(ny - 1, Math.ceil((by1 - y0) / FINE));+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) if (inPoly(pts, (i + 0.5) * FINE - w / 2, (j + 0.5) * FINE + y0)) { const q = (i + nx * j) * 3; fine[q] = rgb[0]; fine[q + 1] = rgb[1]; fine[q + 2] = rgb[2]; }+ }+ const stone = traceryMask(nx, ny, w, y0), mx = nx >> 1, my = ny >> 1, G = new Float32Array(mx * my), kinds = [new Float32Array(mx * my * 4), new Float32Array(mx * my * 4), new Float32Array(mx * my * 4)];+ for (let j = 0; j < my; j++) for (let i = 0; i < mx; i++) {+ let gl = 0; const o = i + mx * j;+ for (let b = 0; b < 2; b++) for (let a = 0; a < 2; a++) {+ const q = 2 * i + a + nx * (2 * j + b), r = fine[q * 3], g = fine[q * 3 + 1], c = fine[q * 3 + 2], m = Math.max(r, g, c);+ if (stone[q] || m === 0) continue;+ const k = (m - Math.min(r, g, c)) / m + 0.15, K = kinds[r === m ? 0 : g === m ? 1 : 2]; // reds and golds, greens, blues+ gl += 0.25; K[o * 4] += k * Math.pow(r / m, 2.2); K[o * 4 + 1] += k * Math.pow(g / m, 2.2); K[o * 4 + 2] += k * Math.pow(c / m, 2.2); K[o * 4 + 3] += k;+ }+ G[o] = gl;+ }+ // Two passes of a box in each direction, each kind of colour on its own: the hue only, never the shape+ const R = WINDOW_LIGHT.hueRadius, tmp = new Float32Array(mx * my * 4);+ for (const W4 of kinds) for (let pass = 0; pass < 2; pass++) {+ for (let j = 0; j < my; j++) for (let c = 0; c < 4; c++) { let s = 0, n = 0; for (let i = -R; i < mx; i++) { if (i + R < mx) { s += W4[(i + R + mx * j) * 4 + c]; n++; } if (i - R - 1 >= 0) { s -= W4[(i - R - 1 + mx * j) * 4 + c]; n--; } if (i >= 0) tmp[(i + mx * j) * 4 + c] = s / n; } }+ for (let i = 0; i < mx; i++) for (let c = 0; c < 4; c++) { let s = 0, n = 0; for (let j = -R; j < my; j++) { if (j + R < my) { s += tmp[(i + mx * (j + R)) * 4 + c]; n++; } if (j - R - 1 >= 0) { s -= tmp[(i + mx * (j - R - 1)) * 4 + c]; n--; } if (j >= 0) W4[(i + mx * j) * 4 + c] = s / n; } }+ }+ // Each spot takes the kind of glass that leads around it, never an average of them: ruby beside cobalt would mix to the magenta of no real pane+ const H = new Float32Array(mx * my * 3);+ for (let o = 0; o < mx * my; o++) { let K = kinds[0]; for (const C of kinds) if (C[o * 4 + 3] > K[o * 4 + 3]) K = C; const t = K[o * 4 + 3] || 1; H[o * 3] = K[o * 4] / t; H[o * 3 + 1] = K[o * 4 + 1] / t; H[o * 3 + 2] = K[o * 4 + 2] / t; }+ batch.transmission = { G, H, mx, my, cell: FINE * 2 };+ return batch.transmission;+}+function bakeWindowLight(V) {+ const { SUN, grid, coarse, NX, NY, NZ, CX, CY, X0, Y0, Z0, CELL, CK, march } = V, { xTowerE, xNaveE, xChoirW } = LAY, WL = WINDOW_LIGHT, parent = ROOT.children.find((c) => c.name === 'Nave');+ const w = WIN.aW, sill = WIN.aSill, ys = WIN.aSp, rise = WIN.aRise, top = ys + rise, archC = (rise * rise - w * w / 4) / w, archR = archC + w / 2, zW = -(zaw + 0.45), zIn = -zaw + 0.35, zOut = -(zaw + 1.32), wallHalf = 0.45;+ const openAt = (u, y) => y >= sill && y <= top && Math.abs(u) <= w / 2 && (y <= ys || Math.abs(u) + archC <= Math.sqrt(archR * archR - (y - ys) * (y - ys)));+ const wins = []; for (const s of SUN_WINDOWS) wins[Math.round((xTowerE - s.x) / BAY - 0.5)] = { x: s.x, L: windowTransmission(s.batch) };+ // One fixed set of rays, each with its own direction inside a small cone and its own start inside the cell: the same pattern at every+ // point, so a vertex cannot differ from its neighbour by chance, and what falls between two vertices is averaged, not missed+ const a0 = (() => { const l = Math.hypot(SUN[2], SUN[0]); return [-SUN[2] / l, 0, SUN[0] / l]; })(), b0 = [SUN[1] * a0[2] - SUN[2] * a0[1], SUN[2] * a0[0] - SUN[0] * a0[2], SUN[0] * a0[1] - SUN[1] * a0[0]], cone = Math.tan(rad(WL.coneDeg)), NR = WL.rays, rays = [];+ for (let n = 0; n < NR; n++) { const r = cone * Math.sqrt((n + 0.5) / NR), th = n * 2.39996, ca = r * Math.cos(th), sa = r * Math.sin(th), x = SUN[0] + a0[0] * ca + b0[0] * sa, y = SUN[1] + a0[1] * ca + b0[1] * sa, z = SUN[2] + a0[2] * ca + b0[2] * sa, l = Math.hypot(x, y, z), pl = Math.hypot(x, z) / l; rays.push({ d: [x / l, y / l, z / l], pl, sx: x / l / pl, sz: z / l / pl, jx: ((n * 0.7549) % 1) - 0.5, jz: ((n * 0.5698) % 1) - 0.5 }); }+ // The piers, exactly: shaft, plinth, and the corbel, statue and canopy on the nave face. Height decides which of them a ray meets+ const hits = (R, px, py, pz, cx, cz, r, yLo, yHi) => { const dx = cx - px, dz = cz - pz, along = dx * R.sx + dz * R.sz; if (along < 0 && dx * dx + dz * dz > r * r) return false; if (Math.abs(dx * R.sz - dz * R.sx) >= r) return false; const y = py + R.d[1] * Math.max(along, 0) / R.pl; return y >= yLo && y <= yHi; };+ const crossing = [[xNaveE, -zn], [xNaveE, zn], [xChoirW, -zn], [xChoirW, zn]];+ const pierBlocks = (R, px, py, pz, skipK) => {+ for (let s = 0; s < 2; s++) {+ const zA = s ? zn : -zn; if (pz < zA - 1.8) continue; // the ray runs towards -z: an arcade north of this point is behind it+ const xA = px + R.sx * (zA - pz) / R.sz, k0 = Math.round((xTowerE - xA) / BAY);+ for (let k = k0 - 1; k <= k0 + 1; k++) {+ if (k < -1 || k >= P.naveBays || (s === 0 && k === skipK)) continue;+ const cx = xTowerE - k * BAY;+ if (hits(R, px, py, pz, cx, zA, 1.02, -1, P.pierTop - 0.8) || hits(R, px, py, pz, cx, zA, 1.62, -1, 0.62) || (k >= 0 && hits(R, px, py, pz, cx, zA + (s ? -1.14 : 1.14), 0.42, 5, 10.2))) return true;+ }+ }+ for (const c of crossing) if (hits(R, px, py, pz, c[0], c[1], 1.3, -1, P.vaultSpring)) return true;+ return false;+ };+ const stoneBlocks = (d, px, py, pz, t0, t1) => { // voxels of real stone between here and the inner face of the wall: the shafts are tested above+ const st = CELL * 0.8, far = CELL * CK * 0.6; let t = t0;+ while (t < t1) {+ const i = ((px + d[0] * t - X0) / CELL) | 0, j = ((py + d[1] * t - Y0) / CELL) | 0, k = ((pz + d[2] * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return false;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)] === 1) return true;+ t += st;+ }+ return false;+ };+ // The window's own gable stands just outside the wall, a plate with a round hole, closer than a voxel can tell: tested exactly.+ // Beyond it the voxels take over, and they hold everything else outside+ const gableW = w + 0.8, gableY = ys + 2.95, gableH = 3.1, gableZ = -(zaw + 1.02), holeY = gableY + gableH * 0.36, holeR = Math.min(gableW * 0.13, gableH * 0.2);+ const gableBlocks = (u, yw, d) => {+ const t = (gableZ - zW) / d[2], x = u + d[0] * t, y = yw + d[1] * t;+ if (y < gableY - 0.09 || y > gableY + gableH + 0.1 || Math.abs(x) > (gableW / 2) * (1 - (y - gableY) / gableH) + 0.09) return false;+ return Math.hypot(x, y - holeY) >= holeR;+ };+ // One ray: adds its hue and its share of glass to acc. Tally: how many reach a window from inside, how many of those also clear the outside+ const acc = [0, 0, 0, 0], tally = { floorAt: 0, floorClear: 0, skinAt: 0, skinClear: 0 };+ let onSkin = false;+ const trace = (R, px, py, pz, t0, skipK) => {+ const d = R.d, tW = (zW - pz) / d[2]; if (tW <= 0) return;+ const xw = px + d[0] * tW, yw = py + d[1] * tW, win = wins[Math.round((xTowerE - xw) / BAY - 0.5)]; if (!win) return;+ const u = xw - win.x, L = win.L, ix = ((u + w / 2) / L.cell) | 0, iy = ((yw - sill) / L.cell) | 0; if (u < -w / 2 || ix >= L.mx || yw < sill || iy >= L.my) return;+ const q = ix + L.mx * iy, g = L.G[q]; if (g <= 0) return;+ const reveal = wallHalf / -d[2]; if (!openAt(u - d[0] * reveal, yw - d[1] * reveal) || !openAt(u + d[0] * reveal, yw + d[1] * reveal)) return; // the wall is 0.9 m thick+ if (pierBlocks(R, px, py, pz, skipK)) return;+ if (stoneBlocks(d, px, py, pz, t0, (zIn - pz) / d[2])) return;+ if (onSkin) tally.skinAt++; else tally.floorAt++;+ if (gableBlocks(u, yw, d)) return;+ const tO = (zOut - zW) / d[2];+ if (march(xw + d[0] * tO, yw + d[1] * tO, zOut, d[0], d[1], d[2], 220) < 1e9) return;+ if (onSkin) tally.skinClear++; else tally.floorClear++;+ acc[0] += g * L.H[q * 3]; acc[1] += g * L.H[q * 3 + 1]; acc[2] += g * L.H[q * 3 + 2]; acc[3] += g;+ };+ // The colour of a point from what arrived: the hue pressed a little further towards pure glass, the opacity from how much arrived+ const finish = (v, level, strength) => {+ const f = Math.min(1, acc[3] / (NR * 0.92)); v.f = f * strength;+ if (acc[3] <= 0) { v.h = null; return; }+ const m = Math.max(acc[0], acc[1], acc[2]) || 1;+ v.h = [Math.pow(acc[0] / m, 1.2), Math.pow(acc[1] / m, 1.2), Math.pow(acc[2] / m, 1.2)]; v.level = level;+ };+ // Write one overlay: only the cells that hold light. A vertex at the edge of a pool has no light of its own, so it borrows its neighbours' hue: its opacity is zero anyway+ const build = (name, mat, verts, cells, level) => {+ const keep = new Int32Array(verts.length).fill(-1), P3 = [], N3 = [], UV = [], C4 = [], I = [];+ const idx = (n) => { if (keep[n] >= 0) return keep[n]; const v = verts[n]; keep[n] = P3.length / 3; P3.push(v.x, v.y, v.z); N3.push(v.nx, v.ny, v.nz); UV.push(v.u, v.v); const h = v.h || v.borrow || [1, 1, 1]; C4.push(level * h[0] + 0.02, level * h[1] + 0.02, level * h[2] + 0.02, WL.opacity * v.f); return keep[n]; };+ for (const c of cells) {+ if (verts[c[0]].f + verts[c[1]].f + verts[c[2]].f + verts[c[3]].f < 0.01) continue;+ for (let a = 0; a < 4; a++) { const v = verts[c[a]]; if (!v.h) for (let b = 0; b < 4; b++) if (verts[c[b]].h) { v.borrow = verts[c[b]].h; break; } }+ const a = idx(c[0]), b = idx(c[1]), e = idx(c[2]), d = idx(c[3]); I.push(a, b, e, a, e, d);+ }+ if (!I.length) return 0;+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(P3), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(N3), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(UV), 2));+ g.setAttribute('color', new THREE.BufferAttribute(new Float32Array(C4), 4));+ g.setIndex(new THREE.BufferAttribute(new Uint16Array(I), 1));+ createPart(uniq(name), g, mat, { parent });+ return P3.length / 3;+ };+ // ---- The floor: from the south aisle wall to just past where the highest light can land ----+ const yF = 0.08, tFloor = (0.46 - yF) / SUN[1], reach = -SUN[2] / SUN[1], drift = SUN[0] / SUN[1], xs = SUN_WINDOWS.map((s) => s.x), h = WL.cell;+ const x0 = Math.min(...xs) - w / 2 - drift * top - 0.8, x1 = Math.max(...xs) + w / 2 + 0.8, z0 = -zaw, z1 = zW + top * reach + 0.8, nu = Math.ceil((x1 - x0) / h), nv = Math.ceil((z1 - z0) / h);+ const fv = [], fc = []; let lastZ = -1e9, litW = 1e9, litE = -1e9;+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) {+ const x = x0 + i * h, z = z0 + j * h; acc[0] = acc[1] = acc[2] = acc[3] = 0;+ for (const R of rays) trace(R, x + R.jx * h, yF, z + R.jz * h, tFloor, -99);+ const v = { x, y: yF, z, nx: 0, ny: 1, nz: 0, u: x / TILE.pave, v: z / TILE.pave }; finish(v, WL.floorLevel, 1); fv.push(v);+ if (v.f > 0.02) { lastZ = Math.max(lastZ, z); litW = Math.min(litW, x); litE = Math.max(litE, x); }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) { const a = i + (nu + 1) * j; fc.push([a + (nu + 1), a + (nu + 1) + 1, a + 1, a]); }+ const floorVerts = build('StainedLight_Floor', MAT.pool, fv, fc, WL.floorLevel);+ // ---- The south piers: rays that stop on their south faces, on a thin skin following the shaft ring point for point ----+ onSkin = true;+ const ring = SHARED.pierRing, RN = ring.length, aS = Math.atan2(SUN[2], SUN[0]), aStep = TAU / RN, nc = Math.round(rad(200) / aStep), c0 = Math.round((aS - rad(100)) / aStep), perim = [0];+ for (let i = 1; i <= RN; i++) perim.push(perim[i - 1] + Math.hypot(ring[i % RN][0] - ring[i - 1][0], ring[i % RN][1] - ring[i - 1][1]));+ const ringAt = (a) => { const f = ((a / TAU) % 1 + 1) % 1 * RN, i = f | 0, t = f - i, A = ring[i % RN], B = ring[(i + 1) % RN]; return [lerp(A[0], B[0], t), lerp(A[1], B[1], t), lerp(A[2], B[2], t), lerp(A[3], B[3], t)]; };+ const kAt = (y) => (y < 0.86 ? lerp(1.24, 1.27, clamp((y - 0.62) / 0.24)) : y < 1.15 ? lerp(1.27, 1, (y - 0.86) / 0.29) : 1);+ const shaftAt = (a, y) => { const r = ringAt(a), k = kAt(y), l = Math.hypot(r[2], r[3]) || 1, nx = r[2] / l, nz = r[3] / l; return [r[0] * k + nx * 0.03, y, r[1] * k + nz * 0.03, nx, nz]; };+ const plinthAt = (a, y) => { const f = Math.round(a / (PI / 4)) * (PI / 4), r = (1.7 * Math.cos(PI / 8)) / Math.cos(a - f) + 0.03; return [r * Math.cos(a), y, r * Math.sin(a), Math.cos(f), Math.sin(f)]; }; // the octagonal foot of the pier+ const skinAt = (a, y) => { if (y <= 0.56) return plinthAt(a, y); if (y >= 0.62) return shaftAt(a, y); const p = plinthAt(a, 0.56), s = shaftAt(a, 0.62), t = (y - 0.56) / 0.06; return [lerp(p[0], s[0], t), y, lerp(p[2], s[2], t), p[3], p[4]]; };+ const rowsY = [yF + 0.01, 0.56, 0.62, 0.86, 1.15]; for (let y = 2.15; y < P.pierTop - 1.2; y += 1) rowsY.push(y);+ const sv = [], sc = [], nr = rowsY.length; let piersLit = 0, topLit = 0;+ for (let k = 0; k < P.naveBays; k++) {+ const cx = xTowerE - k * BAY, base = sv.length; let any = false;+ for (let c = 0; c <= nc; c++) for (let r = 0; r < nr; r++) {+ const a = (c0 + c) * aStep, y = rowsY[r], dy = r + 1 < nr ? rowsY[r + 1] - y : 1, p = skinAt(a, y), cosN = p[3] * SUN[0] + p[4] * SUN[2];+ acc[0] = acc[1] = acc[2] = acc[3] = 0;+ if (cosN > 0.03) for (const R of rays) { const yJ = Math.max(yF + 0.01, y + R.jz * dy), q = skinAt(a + R.jx * aStep, yJ); trace(R, cx + q[0], yJ, -zn + q[2], 0.7, k); }+ const i = (((c0 + c) % RN) + RN) % RN, v = { x: cx + p[0], y, z: -zn + p[2], nx: p[3], ny: 0, nz: p[4], u: perim[(i + (RN >> 1)) % RN] / TILE.fine, v: y / TILE.fine };+ finish(v, WL.stoneLevel, Math.min(1, Math.max(0, cosN) / SUN[1])); sv.push(v); // an upright face turns partly away from this high sun, so it takes less than the floor+ if (v.f > 0.02) { any = true; topLit = Math.max(topLit, y); }+ }+ if (any) piersLit++;+ for (let c = 0; c < nc; c++) for (let r = 0; r < nr - 1; r++) { const a = base + c * nr + r; sc.push([a, a + 1, a + nr + 1, a + nr]); }+ }+ const skinVerts = build('StainedLight_SouthPiers', MAT.poolStone, sv, sc, WL.stoneLevel);+ return { tally, floorVerts, skinVerts, piersLit, topLit, lastZ, litW, litE };+}++// <<ORNAMENT>>++// <<BUILD>>+async function build() {+ ROOT = createRoot('Cathedral');+ await makeMaterials();+ await prepareFigures(P.preview === 'statues' ? P.statueTest : Object.keys(FIGURES));+ if (P.preview === 'statues') { previewStatues(); return ROOT; }+ if (P.preview === 'all' || P.preview === 'unique') {+ buildCathedral();+ await reduceBatches();+ bakeLight();+ return ROOT;+ }+ if (P.preview === 'size') { // builds and bakes everything, then reports bytes as part names, and exports only a probe: fast+ buildCathedral();+ await reduceBatches();+ const baked = bakeLight(), report = ['bake_variants' + baked.variants + '_tinted' + baked.tinted + '_extraKB' + baked.extraKB];+ // Real buffer sizes, each attribute counted once: pos/normal 12, uv 8, colour 12, tangent 16 a vertex where normal-mapped+ const seenAttr = new Set(), seenTan = new Set(), byMat = {}; let bytes = 0, tris = 0;+ const add = (attr, per, key) => { if (!attr || seenAttr.has(attr)) return; seenAttr.add(attr); const b = attr.count * per; bytes += b; byMat[key] = (byMat[key] || 0) + b; };+ for (const e of PLACED) {+ const g = e.part.geometry, key = e.mat.name, a = g.attributes, m = e.part.material;+ tris += g.index.count / 3;+ add(a.position, 12, key); add(a.normal, 12, key); add(a.uv, 8, key); add(a.color, 12, key);+ add(g.index, g.index.array.BYTES_PER_ELEMENT, key);+ if (m && m.normalMap && !seenTan.has(a.position)) { seenTan.add(a.position); bytes += a.position.count * 16; byMat[key] = (byMat[key] || 0) + a.position.count * 16; }+ }+ report.push('geomKB' + Math.round(bytes / 1024) + '_meshes' + PLACED.length + '_rendered' + Math.round(tris));+ Object.keys(byMat).sort((p, q) => byMat[q] - byMat[p]).forEach((k) => report.push(k + '_KB' + Math.round(byMat[k] / 1024)));+ ROOT.clear();+ createPart('SizeProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ report.forEach((d) => group('SIZE_' + d));+ return ROOT;+ }+ if (P.preview === 'textures') { // measures the texture payload on its own+ Object.keys(MAT).forEach((k, i) => createPart('Swatch_' + k, boxGeo(1, 1, 1), MAT[k], { position: [i * 1.2, 0.5, 0], parent: ROOT }));+ return ROOT;+ }+ if (P.preview === 'stats') {+ buildCathedral();+ const list = [...STAT.entries()].map(([b, e]) => ({ name: e.name, n: e.n, tris: b.I.length / 3, kb: Math.round((batchBytes(b) + (e.normalMapped ? b.count * 16 : 0)) / 1024) })).sort((a, c) => c.kb - a.kb);+ let total = 0, draws = 0, rendered = 0; for (const e of list) { total += e.kb; draws += e.n; rendered += e.n * e.tris; }+ createPart('StatProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ group('STAT_TOTAL_kb_' + total + '_draws_' + draws + '_rendered_' + rendered + '_unique_' + list.length);+ list.slice(0, 40).forEach((e, i) => group('STAT_' + String(i).padStart(2, '0') + '_K' + Math.round(e.n * e.tris / 1000) + '_' + e.name + '_x' + e.n + '_tris' + e.tris + '_kb' + e.kb));+ return ROOT;+ }+ return ROOT;+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-7.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/cathedral-7.kiln.jsnew file mode 100644index 00000000..4425c1b4--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/cathedral-7.kiln.js@@ -0,0 +1,4289 @@+// Gothic cathedral: parametric High Gothic church, true scale, metres.+// +X = west front (entrance) , -X = apse, +Y up, +Z = north.+const meta = { name: 'Gothic Cathedral', role: 'wonder' };++// ==================================================================================+// PARAMETERS: change these and the whole design follows.+// ==================================================================================+const P = {+ preview: 'all', // 'all', or 'size' (true buffer bytes, fast), 'stats' (per-mesh list), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)+ // Plan+ naveBays: 11, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,+ xWest: 62, // west face of the towers+ towerDepth: 14, towerWidth: 14,+ choirBays: 3, transeptBays: 2,+ apseSides: 7,+ // Elevation+ pierTop: 15, aisleCrown: 21, triforiumSill: 21.8, triforiumTop: 26.6,+ vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,+ aisleWallTop: 23, aisleRoofHigh: 26.2,+ towerTop: 72, spireTip: 80,+ flecheTip: 100, // top of the cross on the lead spire over the crossing+ // Detail+ jambStatues: 5, kings: 16, seed: 1163,+ archFigureTris: 720, statueTest: ['apostle', 'bishop', 'king', 'queen', 'angel', 'virgin'],+ storyWindows: 3, clerestoryWindows: 3, // unique window cartoons (more = richer, larger GLB)+ // Glass+ glassGlow: 1.05, glassOuterGlow: 0.12,+ westSkyFill: 0.14, westGroundFill: 0.72, // daylight on the west front from the open sky and the sunlit forecourt: the same sun, seen second-hand+ sun: [0.11, 0.79, -0.61], lightVariantBytes: 250000, wallTessellation: 5, // baked sun, about 52 degrees up and 10 degrees west of due south; bytes allowed for per-side vertex light; metres between lit wall vertices+ chapelFacets: 5,+};++// ==================================================================================+// MATH+// ==================================================================================+const PI = Math.PI, TAU = PI * 2, rad = (d) => d * PI / 180;+const lerp = (a, b, t) => a + (b - a) * t;+const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));+const sstep = (a, b, x) => { const t = clamp((x - a) / (b - a)); return t * t * (3 - 2 * t); };+const gauss = (x, y = 0) => Math.exp(-(x * x + y * y));+function mkRng(seed) { let s = seed >>> 0 || 1; return () => { s ^= s << 13; s >>>= 0; s ^= s >>> 17; s ^= s << 5; s >>>= 0; return s / 4294967296; }; }+function keyLin(keys, t) {+ if (t <= keys[0][0]) return keys[0].slice(1);+ for (let i = 1; i < keys.length; i++) if (t <= keys[i][0]) {+ const a = keys[i - 1], b = keys[i], f = (t - a[0]) / (b[0] - a[0]);+ return a.slice(1).map((v, k) => lerp(v, b[k + 1], f));+ }+ return keys[keys.length - 1].slice(1);+}+function keyf(keys, t, w = 0.03) { // smoothed keyframes [[t,v...],...]: no plateaus or kinks at the keys+ const a = keyLin(keys, t - w), b = keyLin(keys, t), c = keyLin(keys, t + w);+ return b.map((v, k) => (a[k] + 2 * v + c[k]) / 4);+}+const M = {+ I: () => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0],+ T: (x, y, z) => [1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z],+ S: (x, y = x, z = x) => [x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0],+ Rx: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [1, 0, 0, 0, 0, c, -s, 0, 0, s, c, 0]; },+ Ry: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, 0, s, 0, 0, 1, 0, 0, -s, 0, c, 0]; },+ Rz: (d) => { const c = Math.cos(rad(d)), s = Math.sin(rad(d)); return [c, -s, 0, 0, s, c, 0, 0, 0, 0, 1, 0]; },+ mul: (a, b) => [+ a[0] * b[0] + a[1] * b[4] + a[2] * b[8], a[0] * b[1] + a[1] * b[5] + a[2] * b[9], a[0] * b[2] + a[1] * b[6] + a[2] * b[10], a[0] * b[3] + a[1] * b[7] + a[2] * b[11] + a[3],+ a[4] * b[0] + a[5] * b[4] + a[6] * b[8], a[4] * b[1] + a[5] * b[5] + a[6] * b[9], a[4] * b[2] + a[5] * b[6] + a[6] * b[10], a[4] * b[3] + a[5] * b[7] + a[6] * b[11] + a[7],+ a[8] * b[0] + a[9] * b[4] + a[10] * b[8], a[8] * b[1] + a[9] * b[5] + a[10] * b[9], a[8] * b[2] + a[9] * b[6] + a[10] * b[10], a[8] * b[3] + a[9] * b[7] + a[10] * b[11] + a[11],+ ],+ of: (...ms) => ms.reduce((a, b) => M.mul(a, b)),+};+function normalMat(m) { // inverse transpose of the 3x3, up to scale (cofactors)+ return [+ m[5] * m[10] - m[6] * m[9], m[6] * m[8] - m[4] * m[10], m[4] * m[9] - m[5] * m[8],+ m[2] * m[9] - m[1] * m[10], m[0] * m[10] - m[2] * m[8], m[1] * m[8] - m[0] * m[9],+ m[1] * m[6] - m[2] * m[5], m[2] * m[4] - m[0] * m[6], m[0] * m[5] - m[1] * m[4],+ ];+}+const det3 = (m) => m[0] * (m[5] * m[10] - m[6] * m[9]) - m[1] * (m[4] * m[10] - m[6] * m[8]) + m[2] * (m[4] * m[9] - m[5] * m[8]);++// ==================================================================================+// MESH KIT: one Batch = one mesh with one material; metric UVs, baked shading in COLOR_0+// ==================================================================================+class Batch {+ constructor(tile = 1) { this.P = []; this.N = []; this.UV = []; this.C = []; this.I = []; this.tile = tile; this.setM(M.I()); this.sh = 1; this.rgb = [1, 1, 1]; this.geo = null; }+ setM(m) { this.m = m; this.nm = normalMat(m); this.det = det3(m); }+ at(m, f) { const old = this.m; this.setM(M.mul(old, m)); f(this); this.setM(old); return this; }+ shade(s, f) { const o = this.sh; this.sh = o * s; f(this); this.sh = o; return this; }+ tint(rgb, f) { const o = this.rgb; this.rgb = rgb; f(this); this.rgb = o; return this; }+ get count() { return this.P.length / 3; }+ v(x, y, z, nx, ny, nz, u, v, s = 1) {+ const m = this.m, n = this.nm;+ const wx = m[0] * x + m[1] * y + m[2] * z + m[3], wy = m[4] * x + m[5] * y + m[6] * z + m[7], wz = m[8] * x + m[9] * y + m[10] * z + m[11];+ let ax = n[0] * nx + n[1] * ny + n[2] * nz, ay = n[3] * nx + n[4] * ny + n[5] * nz, az = n[6] * nx + n[7] * ny + n[8] * nz;+ const l = Math.hypot(ax, ay, az);+ if (l < 1e-10) { ax = 0; ay = 1; az = 0; } else { ax /= l; ay /= l; az /= l; }+ if (this.det < 0) { ax = -ax; ay = -ay; az = -az; }+ if (u === undefined || u === null) {+ const fx = Math.abs(ax), fy = Math.abs(ay), fz = Math.abs(az);+ if (fy > 0.75) { u = wx; v = wz; } else if (fx > fz) { u = wz; v = wy; } else { u = wx; v = wy; }+ }+ const sh = clamp(s * this.sh); // all light is added later by bakeLight+ const rgb = this.rgb;+ this.P.push(wx, wy, wz); this.N.push(ax, ay, az); this.UV.push(u / this.tile, v / this.tile);+ this.C.push(Math.round(255 * sh * rgb[0]), Math.round(255 * sh * rgb[1]), Math.round(255 * sh * rgb[2]));+ return this.P.length / 3 - 1;+ }+ tri(a, b, c) { if (this.det < 0) this.I.push(a, c, b); else this.I.push(a, b, c); }+ quad(a, b, c, d) { this.tri(a, b, c); this.tri(a, c, d); }+ // flat polygon from 3D points (CCW seen from the front); convex fan+ face(pts, s = 1) {+ const a = pts[0], b = pts[1], c = pts[pts.length - 1];+ let nx = (b[1] - a[1]) * (c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]), ny = (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]), nz = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ if (pts.length > 3 && Math.hypot(nx, ny, nz) < 1e-9) { const d = pts[2]; nx = (b[1] - a[1]) * (d[2] - a[2]) - (b[2] - a[2]) * (d[1] - a[1]); ny = (b[2] - a[2]) * (d[0] - a[0]) - (b[0] - a[0]) * (d[2] - a[2]); nz = (b[0] - a[0]) * (d[1] - a[1]) - (b[1] - a[1]) * (d[0] - a[0]); }+ const l = Math.hypot(nx, ny, nz) || 1; nx /= l; ny /= l; nz /= l;+ const ids = pts.map((p) => this.v(p[0], p[1], p[2], nx, ny, nz, undefined, undefined, p[3] === undefined ? s : p[3]));+ for (let i = 1; i < ids.length - 1; i++) this.tri(ids[0], ids[i], ids[i + 1]);+ return this;+ }+ box(x0, x1, y0, y1, z0, z1, o = {}) {+ const s = o.s === undefined ? 1 : o.s, sb = o.sb === undefined ? s * 0.62 : o.sb, st = o.st === undefined ? s : o.st;+ const tx = o.taper || 0;+ const X0 = x0 + tx, X1 = x1 - tx, Z0 = z0 + (o.taperZ === undefined ? tx : o.taperZ), Z1 = z1 - (o.taperZ === undefined ? tx : o.taperZ);+ this.face([[x1, y0, z1, s], [x1, y0, z0, s], [X1, y1, Z0, st], [X1, y1, Z1, st]]); // +X+ this.face([[x0, y0, z0, s], [x0, y0, z1, s], [X0, y1, Z1, st], [X0, y1, Z0, st]]); // -X+ this.face([[x0, y0, z1, s], [x1, y0, z1, s], [X1, y1, Z1, st], [X0, y1, Z1, st]]); // +Z+ this.face([[x1, y0, z0, s], [x0, y0, z0, s], [X0, y1, Z0, st], [X1, y1, Z0, st]]); // -Z+ if (!o.noTop) this.face([[X0, y1, Z1, st], [X1, y1, Z1, st], [X1, y1, Z0, st], [X0, y1, Z0, st]]);+ if (!o.noBot) this.face([[x0, y0, z0, sb], [x1, y0, z0, sb], [x1, y0, z1, sb], [x0, y0, z1, sb]]);+ return this;+ }+ // Surface of revolution about Y. prof: [[r,y],...] bottom to top. o.sides, o.faceted, o.crease, o.flat+ lathe(prof, n = 12, o = {}) {+ const a0 = o.phase || 0, cr = Math.cos(rad(o.crease === undefined ? 55 : o.crease));+ const s = o.s === undefined ? 1 : o.s;+ const segs = [];+ for (let j = 0; j < prof.length - 1; j++) {+ const dr = prof[j + 1][0] - prof[j][0], dy = prof[j + 1][1] - prof[j][1], l = Math.hypot(dr, dy) || 1;+ segs.push([dy / l, -dr / l, l]);+ }+ if (o.faceted) { // hard-edged polygon of revolution+ for (let i = 0; i < n; i++) {+ const t0 = a0 + TAU * i / n, t1 = a0 + TAU * (i + 1) / n, tm = (t0 + t1) / 2;+ const c0 = Math.cos(t0), s0 = Math.sin(t0), c1 = Math.cos(t1), s1 = Math.sin(t1), cm = Math.cos(tm), sm = Math.sin(tm);+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const p0 = prof[j], p1 = prof[j + 1];+ const w0 = p0[0] * Math.hypot(c1 - c0, s1 - s0), w1 = p1[0] * Math.hypot(c1 - c0, s1 - s0);+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ const a = this.v(p0[0] * c0, p0[1], p0[0] * s0, nr * cm, ny, nr * sm, -w0 / 2, vv, sA);+ const b = this.v(p1[0] * c0, p1[1], p1[0] * s0, nr * cm, ny, nr * sm, -w1 / 2, vv + l, sB);+ const c = this.v(p1[0] * c1, p1[1], p1[0] * s1, nr * cm, ny, nr * sm, w1 / 2, vv + l, sB);+ const d = this.v(p0[0] * c1, p0[1], p0[0] * s1, nr * cm, ny, nr * sm, w0 / 2, vv, sA);+ this.quad(a, b, c, d); vv += l;+ }+ }+ return this;+ }+ // smooth around, creased along the profile when the section turns sharply+ let vv = 0;+ for (let j = 0; j < segs.length; j++) {+ const [nr, ny, l] = segs[j];+ const prev = segs[j - 1], next = segs[j + 1];+ let n0r = nr, n0y = ny, n1r = nr, n1y = ny;+ if (prev && prev[0] * nr + prev[1] * ny > cr) { n0r = prev[0] + nr; n0y = prev[1] + ny; }+ if (next && next[0] * nr + next[1] * ny > cr) { n1r = next[0] + nr; n1y = next[1] + ny; }+ const base = this.count, p0 = prof[j], p1 = prof[j + 1];+ const circ = Math.max(p0[0], p1[0]) * TAU, uScale = Math.max(1, Math.round(circ)) / n;+ const sA = p0[2] === undefined ? s : p0[2], sB = p1[2] === undefined ? s : p1[2];+ for (let i = 0; i <= n; i++) {+ const t = a0 + TAU * i / n, c = Math.cos(t), sn = Math.sin(t);+ this.v(p0[0] * c, p0[1], p0[0] * sn, n0r * c, n0y, n0r * sn, i * uScale, vv, sA);+ this.v(p1[0] * c, p1[1], p1[0] * sn, n1r * c, n1y, n1r * sn, i * uScale, vv + l, sB);+ }+ for (let i = 0; i < n; i++) this.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+ vv += l;+ }+ return this;+ }+ // Sweep a closed section (a,b CCW; a along in-plane left normal, b along +Z) along a path in the XY plane.+ sweep(path, sec, o = {}) {+ const closedPath = !!o.closed, cr = Math.cos(rad(o.crease === undefined ? 50 : o.crease)), z0 = o.z || 0, s = o.s === undefined ? 1 : o.s;+ const n = path.length, E = [];+ const segCount = closedPath ? n : n - 1;+ for (let i = 0; i < segCount; i++) { const p = path[i], q = path[(i + 1) % n]; const dx = q[0] - p[0], dy = q[1] - p[1], l = Math.hypot(dx, dy) || 1e-6; E.push([dx / l, dy / l, l]); }+ // section normals+ const K = sec.length, SN = [];+ for (let j = 0; j < K; j++) { const a = sec[j], b = sec[(j + 1) % K]; const da = b[0] - a[0], db = b[1] - a[1], l = Math.hypot(da, db) || 1e-6; SN.push([db / l, -da / l, l]); }+ const rings = []; let dist = 0;+ for (let i = 0; i < segCount; i++) {+ const e = E[i], ePrev = closedPath ? E[(i - 1 + segCount) % segCount] : (E[i - 1] || e), eNext = closedPath ? E[(i + 1) % segCount] : (E[i + 1] || e);+ const p = path[i], q = path[(i + 1) % n];+ const miter = (A, B) => { let bx = -(A[1] + B[1]), by = A[0] + B[0]; const l = Math.hypot(bx, by) || 1; bx /= l; by /= l; const nlx = -B[1], nly = B[0]; const c = Math.max(0.35, bx * nlx + by * nly); return [bx / c, by / c]; };+ const m0 = miter(ePrev, e), m1 = miter(e, eNext);+ const smooth0 = ePrev[0] * e[0] + ePrev[1] * e[1] > cr, smooth1 = e[0] * eNext[0] + e[1] * eNext[1] > cr;+ const nl = [-e[1], e[0]];+ const n0 = smooth0 ? [-(e[1] + ePrev[1]), e[0] + ePrev[0]] : nl, n1 = smooth1 ? [-(e[1] + eNext[1]), e[0] + eNext[0]] : nl;+ const ln0 = Math.hypot(n0[0], n0[1]) || 1, ln1 = Math.hypot(n1[0], n1[1]) || 1;+ let perim = 0;+ for (let j = 0; j < K; j++) {+ const A = sec[j], B = sec[(j + 1) % K], sn = SN[j], pv = SN[(j - 1 + K) % K], nx = SN[(j + 1) % K];+ const sA = sn[0] * pv[0] + sn[1] * pv[1] > cr, sB = sn[0] * nx[0] + sn[1] * nx[1] > cr;+ const nA = sA ? [sn[0] + pv[0], sn[1] + pv[1]] : sn, nB = sB ? [sn[0] + nx[0], sn[1] + nx[1]] : sn;+ const norm = (na, nb, nv, ln) => { const l = Math.hypot(na, nb) || 1; const a = na / l, b = nb / l; return [a * nv[0] / ln, a * nv[1] / ln, b]; };+ const na0 = norm(nA[0], nA[1], n0, ln0), nb0 = norm(nB[0], nB[1], n0, ln0), na1 = norm(nA[0], nA[1], n1, ln1), nb1 = norm(nB[0], nB[1], n1, ln1);+ const shA = A[2] === undefined ? s : A[2] * s, shB = B[2] === undefined ? s : B[2] * s;+ const a = this.v(p[0] + A[0] * m0[0], p[1] + A[0] * m0[1], z0 + A[1], na0[0], na0[1], na0[2], dist, perim, shA);+ const b = this.v(p[0] + B[0] * m0[0], p[1] + B[0] * m0[1], z0 + B[1], nb0[0], nb0[1], nb0[2], dist, perim + sn[2], shB);+ const c = this.v(q[0] + B[0] * m1[0], q[1] + B[0] * m1[1], z0 + B[1], nb1[0], nb1[1], nb1[2], dist + e[2], perim + sn[2], shB);+ const d = this.v(q[0] + A[0] * m1[0], q[1] + A[0] * m1[1], z0 + A[1], na1[0], na1[1], na1[2], dist + e[2], perim, shA);+ this.quad(a, b, c, d);+ perim += sn[2];+ }+ dist += e[2];+ rings.push(m0);+ }+ if (!closedPath && o.caps !== false) {+ const capAt = (p, nv, e, flip) => {+ const pts = sec.map((A) => [p[0] + A[0] * nv[0], p[1] + A[0] * nv[1], z0 + A[1]]);+ const idx = triangulate(sec.map((A) => [A[0], A[1]]));+ const ids = pts.map((q) => this.v(q[0], q[1], q[2], flip * e[0], flip * e[1], 0, q[0], q[2], s * 0.8));+ for (const t of idx) { if (flip > 0) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); else this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ };+ capAt(path[0], rings[0], E[0], -1);+ const lastE = E[segCount - 1]; capAt(path[n - 1], [-lastE[1], lastE[0]], lastE, 1);+ }+ return this;+ }+ // Generic tube along a 3D path with parallel transport. radii: number|array|fn(t) -> [ra, rb]; ring: fn(angle,t) multiplier+ tube(path, radii, n = 8, o = {}) {+ const N = path.length, s = o.s === undefined ? 1 : o.s;+ let T = [], U = o.up ? [...o.up] : null;+ for (let i = 0; i < N; i++) {+ const p = path[Math.max(0, i - 1)], q = path[Math.min(N - 1, i + 1)];+ let tx = q[0] - p[0], ty = q[1] - p[1], tz = q[2] - p[2]; const l = Math.hypot(tx, ty, tz) || 1; T.push([tx / l, ty / l, tz / l]);+ }+ if (!U) U = Math.abs(T[0][1]) < 0.9 ? [0, 1, 0] : [1, 0, 0];+ const base = this.count; const rowLen = n + 1; let dist = 0;+ for (let i = 0; i < N; i++) {+ const t = T[i];+ let d = U[0] * t[0] + U[1] * t[1] + U[2] * t[2];+ let ux = U[0] - d * t[0], uy = U[1] - d * t[1], uz = U[2] - d * t[2]; const ul = Math.hypot(ux, uy, uz) || 1; ux /= ul; uy /= ul; uz /= ul; U = [ux, uy, uz];+ const vx = t[1] * uz - t[2] * uy, vy = t[2] * ux - t[0] * uz, vz = t[0] * uy - t[1] * ux;+ const f = i / (N - 1);+ const rr = typeof radii === 'function' ? radii(f) : (Array.isArray(radii) ? radii[i] : radii);+ const ra = Array.isArray(rr) ? rr[0] : rr, rb = Array.isArray(rr) ? rr[1] : rr;+ if (i > 0) dist += Math.hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1], path[i][2] - path[i - 1][2]);+ for (let k = 0; k <= n; k++) {+ const a = TAU * k / n, c = Math.cos(a), sn = Math.sin(a);+ const mod = o.ring ? o.ring(a, f) : 1;+ const shade = o.ringS ? o.ringS(a, f) : 1;+ const px = path[i][0] + (ux * c * ra + vx * sn * rb) * mod, py = path[i][1] + (uy * c * ra + vy * sn * rb) * mod, pz = path[i][2] + (uz * c * ra + vz * sn * rb) * mod;+ const nx = ux * c / (ra || 1) + vx * sn / (rb || 1), ny = uy * c / (ra || 1) + vy * sn / (rb || 1), nz = uz * c / (ra || 1) + vz * sn / (rb || 1);+ this.v(px, py, pz, nx, ny, nz, k / n * Math.max(ra, rb) * TAU, dist, s * shade);+ }+ }+ for (let i = 0; i < N - 1; i++) for (let k = 0; k < n; k++) {+ const a = base + i * rowLen + k, b = a + 1, c = a + rowLen + 1, d = a + rowLen;+ this.quad(a, b, c, d);+ }+ if (o.capEnd !== false) { const c = this.v(path[N - 1][0], path[N - 1][1], path[N - 1][2], T[N - 1][0], T[N - 1][1], T[N - 1][2], 0, 0, s); const b2 = base + (N - 1) * rowLen; const ids = []; for (let k = 0; k < n; k++) ids.push(b2 + k); for (let k = 0; k < n; k++) this.tri(c, ids[k], b2 + k + 1); }+ if (o.capStart) { const c = this.v(path[0][0], path[0][1], path[0][2], -T[0][0], -T[0][1], -T[0][2], 0, 0, s); for (let k = 0; k < n; k++) this.tri(c, base + k + 1, base + k); }+ return this;+ }+ // parametric grid: fn(u,v) -> [x,y,z, shade?]; winding u x v = front. Smooth normals from the grid.+ grid(fn, nu, nv, o = {}) {+ const pts = [];+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) pts.push(fn(i / nu, j / nv));+ const id = (i, j) => j * (nu + 1) + i, base = this.count, flip = o.flip ? -1 : 1, s = o.s === undefined ? 1 : o.s;+ const wrapU = !!o.wrapU;+ let vLen = 0;+ for (let j = 0; j <= nv; j++) {+ if (j > 0) { const a = pts[id(0, j - 1)], b = pts[id(0, j)]; vLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ // o.metric: measure across from the middle of each row, so a converging surface keeps true scale instead of a fan of streaks+ let uLen = 0;+ if (o.metric) { let total = 0; for (let i = 1; i <= nu; i++) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; total += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); } uLen = -total / 2; }+ for (let i = 0; i <= nu; i++) {+ if (i > 0) { const a = pts[id(i - 1, j)], b = pts[id(i, j)]; uLen += Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]); }+ const iP = wrapU ? (i === 0 ? nu - 1 : i - 1) : Math.max(0, i - 1), iN = wrapU ? (i === nu ? 1 : i + 1) : Math.min(nu, i + 1);+ const A = pts[id(iP, j)], B = pts[id(iN, j)], C = pts[id(i, Math.max(0, j - 1))], D = pts[id(i, Math.min(nv, j + 1))];+ const ux = B[0] - A[0], uy = B[1] - A[1], uz = B[2] - A[2], vx = D[0] - C[0], vy = D[1] - C[1], vz = D[2] - C[2];+ const p = pts[id(i, j)];+ const uvU = o.boxUV ? undefined : uLen, uvV = o.boxUV ? undefined : vLen;+ this.v(p[0], p[1], p[2], flip * (uy * vz - uz * vy), flip * (uz * vx - ux * vz), flip * (ux * vy - uy * vx), uvU, uvV, s * (p[3] === undefined ? 1 : p[3]));+ }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) {+ const a = base + id(i, j), b = base + id(i + 1, j), c = base + id(i + 1, j + 1), d = base + id(i, j + 1);+ if (flip > 0) this.quad(a, b, c, d); else this.quad(a, d, c, b);+ }+ return this;+ }+ // 2D outline with holes, extruded along Z from z0 to z1 (front +Z). Reveal sides included.+ slab(outline, holes = [], z0 = 0, z1 = 0.1, o = {}) {+ const so = area2(outline) > 0 ? outline : outline.slice().reverse();+ const hs = holes.map((h) => (area2(h) < 0 ? h : h.slice().reverse()));+ const tris = triangulate(so, hs);+ const all = so.concat(...hs);+ const sf = o.sf === undefined ? 1 : o.sf, sb = o.sb === undefined ? 1 : o.sb, sr = o.sr === undefined ? 0.72 : o.sr;+ if (!o.noFront) { const ids = all.map((p) => this.v(p[0], p[1], z1, 0, 0, 1, undefined, undefined, sf)); for (const t of tris) this.tri(ids[t[0]], ids[t[1]], ids[t[2]]); }+ if (!o.noBack) { const ids = all.map((p) => this.v(p[0], p[1], z0, 0, 0, -1, undefined, undefined, sb)); for (const t of tris) this.tri(ids[t[0]], ids[t[2]], ids[t[1]]); }+ const side = (loop, s) => {+ const L = loop.length; if (o.noSides) return;+ for (let i = 0; i < L; i++) {+ const p = loop[i], q = loop[(i + 1) % L]; const dx = q[0] - p[0], dy = q[1] - p[1];+ const l = Math.hypot(dx, dy) || 1, nx = dy / l, ny = -dx / l;+ const a = this.v(p[0], p[1], z0, nx, ny, 0, undefined, undefined, s), b = this.v(q[0], q[1], z0, nx, ny, 0, undefined, undefined, s), c = this.v(q[0], q[1], z1, nx, ny, 0, undefined, undefined, s), d = this.v(p[0], p[1], z1, nx, ny, 0, undefined, undefined, s);+ this.quad(a, b, c, d);+ }+ };+ if (!o.noOuterSides) side(so, o.so === undefined ? 0.85 : o.so);+ hs.forEach((h) => side(h, sr));+ return this;+ }+ // Vertical prism from an (x,z) polygon+ prism(pts, y0, y1, o = {}) {+ let A = 0; for (let i = 0; i < pts.length; i++) { const p = pts[i], q = pts[(i + 1) % pts.length]; A += p[0] * q[1] - q[0] * p[1]; }+ const L = A > 0 ? pts : pts.slice().reverse(), s = o.s === undefined ? 1 : o.s, st = o.st === undefined ? s : o.st, n = L.length, k = o.topScale === undefined ? 1 : o.topScale;+ let cx = 0, cz = 0; for (const p of L) { cx += p[0]; cz += p[1]; } cx /= n; cz /= n;+ const top = L.map((p) => [cx + (p[0] - cx) * k, cz + (p[1] - cz) * k]);+ for (let i = 0; i < n; i++) {+ const p = L[i], q = L[(i + 1) % n], P2 = top[i], Q2 = top[(i + 1) % n];+ this.face([[q[0], y0, q[1], s], [p[0], y0, p[1], s], [P2[0], y1, P2[1], st], [Q2[0], y1, Q2[1], st]]);+ }+ if (!o.noTop && k > 1e-4) { const c = top.map((p) => this.v(p[0], y1, p[1], 0, 1, 0, undefined, undefined, st)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i + 1], c[i]); }+ if (o.bot) { const c = L.map((p) => this.v(p[0], y0, p[1], 0, -1, 0, undefined, undefined, s * 0.6)); for (let i = 1; i < n - 1; i++) this.tri(c[0], c[i], c[i + 1]); }+ return this;+ }+ // Welds vertices that agree in every attribute: sweeps and lathes otherwise export many duplicates+ // Split every edge longer than L. The rule depends only on the edge, so neighbours split alike and no crack opens.+ // Long walls need vertices for the baked light to land on.+ tess(L) {+ this.weld();+ const L2 = L * L, { P: p, N: nn, UV: uv, C: c } = this;+ for (let pass = 0; pass < 7; pass++) {+ const mid = new Map(), I2 = [], I0 = this.I; let changed = false;+ const long = (a, b) => { const x = p[a * 3] - p[b * 3], y = p[a * 3 + 1] - p[b * 3 + 1], z = p[a * 3 + 2] - p[b * 3 + 2]; return x * x + y * y + z * z > L2; };+ const midOf = (a, b) => {+ const lo = Math.min(a, b), hi = Math.max(a, b), key = lo * 4194304 + hi; let m = mid.get(key);+ if (m === undefined) {+ m = p.length / 3; mid.set(key, m);+ p.push((p[lo * 3] + p[hi * 3]) / 2, (p[lo * 3 + 1] + p[hi * 3 + 1]) / 2, (p[lo * 3 + 2] + p[hi * 3 + 2]) / 2);+ let nx = nn[lo * 3] + nn[hi * 3], ny = nn[lo * 3 + 1] + nn[hi * 3 + 1], nz = nn[lo * 3 + 2] + nn[hi * 3 + 2]; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1;+ nn.push(nx / l, ny / l, nz / l);+ uv.push((uv[lo * 2] + uv[hi * 2]) / 2, (uv[lo * 2 + 1] + uv[hi * 2 + 1]) / 2);+ c.push((c[lo * 3] + c[hi * 3]) >> 1, (c[lo * 3 + 1] + c[hi * 3 + 1]) >> 1, (c[lo * 3 + 2] + c[hi * 3 + 2]) >> 1);+ }+ return m;+ };+ for (let t = 0; t < I0.length; t += 3) {+ const a = I0[t], b = I0[t + 1], d = I0[t + 2], ab = long(a, b), bd = long(b, d), da = long(d, a);+ if (!ab && !bd && !da) { I2.push(a, b, d); continue; }+ changed = true;+ const m1 = ab ? midOf(a, b) : -1, m2 = bd ? midOf(b, d) : -1, m3 = da ? midOf(d, a) : -1;+ if (ab && bd && da) I2.push(a, m1, m3, m1, b, m2, m3, m2, d, m1, m2, m3);+ else if (ab && bd) I2.push(a, m1, d, m1, m2, d, m1, b, m2);+ else if (bd && da) I2.push(b, m2, a, m2, m3, a, m2, d, m3);+ else if (da && ab) I2.push(d, m3, b, m3, m1, b, m3, a, m1);+ else if (ab) I2.push(a, m1, d, m1, b, d);+ else if (bd) I2.push(b, m2, a, m2, d, a);+ else I2.push(d, m3, b, m3, a, b);+ }+ this.I = I2;+ if (!changed) break;+ }+ this.geo = null;+ return this;+ }+ weld() {+ if (this.welded) return this;+ const n = this.count, map = new Map(), remap = new Int32Array(n), P2 = [], N2 = [], U2 = [], C2 = [];+ const { P: p, N: nn, UV: uv, C: c } = this, col = !this.noColor;+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, i2 = i * 2;+ const key = Math.round(p[i3] * 4000) + ',' + Math.round(p[i3 + 1] * 4000) + ',' + Math.round(p[i3 + 2] * 4000) + ',' + Math.round(nn[i3] * 90) + ',' + Math.round(nn[i3 + 1] * 90) + ',' + Math.round(nn[i3 + 2] * 90) + ',' + Math.round(uv[i2] * 800) + ',' + Math.round(uv[i2 + 1] * 800) + (col ? ',' + c[i3] + ',' + c[i3 + 1] + ',' + c[i3 + 2] : '');+ let k = map.get(key);+ if (k === undefined) { k = P2.length / 3; map.set(key, k); P2.push(p[i3], p[i3 + 1], p[i3 + 2]); N2.push(nn[i3], nn[i3 + 1], nn[i3 + 2]); U2.push(uv[i2], uv[i2 + 1]); C2.push(c[i3], c[i3 + 1], c[i3 + 2]); }+ remap[i] = k;+ }+ const I2 = [];+ for (let t = 0; t < this.I.length; t += 3) {+ const a = remap[this.I[t]], b = remap[this.I[t + 1]], d = remap[this.I[t + 2]];+ if (a !== b && b !== d && a !== d) I2.push(a, b, d);+ }+ this.P = P2; this.N = N2; this.UV = U2; this.C = C2; this.I = I2; this.welded = true;+ return this;+ }+ toGeo() {+ if (this.geo) return this.geo;+ this.weld();+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(this.P), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(this.N), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(this.UV), 2));+ if (!this.noColor) g.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(this.C), 3, true));+ g.setIndex(new THREE.BufferAttribute(this.count > 65535 ? new Uint32Array(this.I) : new Uint16Array(this.I), 1));+ this.geo = g;+ return g;+ }+}+function area2(p) { let a = 0; for (let i = 0; i < p.length; i++) { const q = p[(i + 1) % p.length]; a += p[i][0] * q[1] - q[0] * p[i][1]; } return a / 2; }+function triangulate(contour, holes = []) {+ const V = (p) => new THREE.Vector2(p[0], p[1]);+ const tris = THREE.ShapeUtils.triangulateShape(contour.map(V), holes.map((h) => h.map(V)));+ const all = contour.concat(...holes);+ return tris.map((t) => {+ const a = all[t[0]], b = all[t[1]], c = all[t[2]];+ const s = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ return s >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]];+ });+}++// ---- 2D curves -------------------------------------------------------------------+// Two-centred pointed arc: span w, rise h, base y=0 centred at x=0, from left spring to right spring+function pointedArc(w, h, n = 8) {+ const c = (h * h - w * w / 4) / w, r = c + w / 2, pts = [];+ const a0 = PI, a1 = Math.atan2(h, -c);+ for (let i = 0; i <= n; i++) { const a = lerp(a0, a1, i / n); pts.push([c + r * Math.cos(a), r * Math.sin(a)]); }+ for (let i = n - 1; i >= 0; i--) { const a = lerp(a0, a1, i / n); pts.push([-(c + r * Math.cos(a)), r * Math.sin(a)]); }+ pts[n][0] = 0;+ return pts;+}+// CCW outline of a pointed opening: width w, sill y0, spring ys, rise h+function archOutline(w, y0, ys, h, n = 8) {+ const arc = pointedArc(w, h, n).map((p) => [p[0], p[1] + ys]);+ arc.reverse(); // right spring -> apex -> left spring (CCW)+ return [[-w / 2, y0], [w / 2, y0], ...arc];+}+const ringPts = (cx, cy, r, n = 24, ph = 0) => { const a = []; for (let i = 0; i < n; i++) { const t = ph + TAU * i / n; a.push([cx + r * Math.cos(t), cy + r * Math.sin(t)]); } return a; };+const ofsY = (pts, dy) => pts.map((p) => [p[0], p[1] + dy]);+const ofsXY = (pts, dx, dy) => pts.map((p) => [p[0] + dx, p[1] + dy]);++// ---- Sections (a, b, shade?) closed CCW ------------------------------------------+// Moulded arch section with convex rolls: a=0 at the soffit, grows outward; symmetric in b.+function rollSection(w, d, rolls = 3, amp = 0.07, seg = 3) {+ const half = [];+ const A = [w, d / 2], B = [0, d * 0.12], dx = B[0] - A[0], dy = B[1] - A[1], l = Math.hypot(dx, dy);+ for (let k = 0; k < rolls; k++) for (let i = 0; i < seg; i++) {+ const t = (k + i / seg) / rolls, ph = i / seg;+ const off = amp * Math.pow(Math.sin(PI * ph), 0.7), sh = 0.68 + 0.32 * Math.sin(PI * ph);+ half.push([lerp(A[0], B[0], t) + off * dy / l, lerp(A[1], B[1], t) - off * dx / l, sh]);+ }+ half.push([B[0], B[1], 0.8]);+ const back = half.slice().reverse().map((p) => [p[0], -p[1], p[2]]);+ return [[w, -d / 2, 1], ...half, ...back.slice(0, -1)];+}+// Cheapest moulded bar: a hexagon, smooth enough to read as a roll at distance+function hexBar(w, d) { const a = w / 2, b = d / 2; return [[a, -b * 0.5], [a, b * 0.5], [0, b], [-a, b * 0.5], [-a, -b * 0.5], [0, -b]]; }+// Simple chamfered bar centred on the path, width w (in plane), depth d+function barSection(w, d, ch = 0.25) {+ const a = w / 2, b = d / 2, c = Math.min(a, b) * ch * 2;+ return [[-a, -b + c], [-a + c, -b], [a - c, -b], [a, -b + c], [a, b - c], [a - c, b], [-a + c, b], [-a, b - c]];+}+// Tracery bar with a roll on the front and back faces+function traceryBar(w, d) {+ const a = w / 2, b = d / 2, s = [];+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([a * Math.cos(t), b * 0.62 + b * 0.38 * Math.sin(t)]); }+ for (let i = 0; i <= 4; i++) { const t = PI * i / 4; s.push([-a * Math.cos(t), -b * 0.62 - b * 0.38 * Math.sin(t)]); }+ return s;+}+// String course / cornice projecting along +b from the wall at b=0, height h (a)+function stringSection(h = 0.3, out = 0.25) {+ return [[-h / 2, 0], [-h / 2, out * 0.3, 0.55], [-h * 0.25, out * 0.62, 0.7], [-h * 0.02, out * 0.72, 0.85], [h * 0.12, out, 1], [h * 0.2, out * 0.98, 1], [h / 2, out * 0.05, 1], [h / 2, 0]].reverse();+}++const spline = (pts, sub = 4) => { // Catmull-Rom through 3D points+ const out = [], n = pts.length, g = (i) => pts[clamp(i, 0, n - 1)];+ for (let i = 0; i < n - 1; i++) for (let k = 0; k < sub; k++) {+ const t = k / sub, p0 = g(i - 1), p1 = g(i), p2 = g(i + 1), p3 = g(i + 2), v = [];+ for (let c = 0; c < 3; c++) v.push(0.5 * (2 * p1[c] + (p2[c] - p0[c]) * t + (2 * p0[c] - 5 * p1[c] + 4 * p2[c] - p3[c]) * t * t + (3 * p1[c] - p0[c] - 3 * p2[c] + p3[c]) * t * t * t));+ out.push(v);+ }+ out.push(pts[n - 1]);+ return out;+};+const blob = (b, c, r, nu = 8, nv = 6, sh = 1) => b.grid((u, v) => {+ const ph = -PI + TAU * u, la = -PI / 2 + PI * v;+ return [c[0] + r[0] * Math.cos(la) * Math.cos(ph), c[1] + r[1] * Math.sin(la), c[2] + r[2] * Math.cos(la) * Math.sin(ph), sh];+}, nu, nv, { flip: true, wrapU: true });++// ==================================================================================+// MATERIALS: procedural PBR, with height-derived normals+// ==================================================================================+const MAT = {};+const TILE = { ashlar: 8, fine: 2, lead: 8, pave: 2.4, oak: 1.2, iron: 0.8, gold: 1, flag: 6, earth: 5 };++function ashlarOps(rng, S, tone, joint, stains) {+ // Irregular coursed blocks; shared between albedo and height by replaying the same rng seed+ const ops = [], rows = 7, rowH = Math.floor(S / rows);+ for (let r = 0; r < rows; r++) {+ const y0 = r * rowH; let x = Math.round(rng() * S / 5) % S;+ const n = 3 + Math.floor(rng() * 2), widths = [];+ for (let i = 0; i < n; i++) widths.push(0.7 + rng() * 0.6);+ const tot = widths.reduce((a, c) => a + c, 0);+ for (let i = 0; i < n; i++) {+ const w = Math.round(widths[i] / tot * S), dv = rng(), a = rng();+ const col = tone[Math.floor(dv * tone.length)];+ const x0 = x % S, wr = Math.min(w, S - x0), op = Math.round(10 + a * 34);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y0, width: wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ if (wr < w) ops.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: y0, width: w - wr, height: rowH }, rgba: [col[0], col[1], col[2], 255], opacity: op });+ x += w;+ ops.push({ op: 'paint', shape: { kind: 'line', from: [x % S, y0], to: [x % S, y0 + rowH], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y0], to: [S, y0], width: joint.w }, rgba: joint.rgba, opacity: joint.op });+ }+ return ops.slice(0, 58).concat(stains || []);+}++// Regions of the decal sheet as fractions [u0, v0, u1, v1]; a streak's top edge is at v0+const DECAL_UV = { streak: [0, 0, 0.5, 0.5], soft: [0.5, 0, 1, 0.25], soot: [0.5, 0.25, 1, 0.5], ground: [0, 0.5, 0.5, 1], drip: [0.5, 0.75, 1, 1] };+async function makeMaterials() {+ const S = 512, repeat = { minFilter: 'linear-mipmap-linear', magFilter: 'linear', wrapS: 'repeat', wrapT: 'repeat' };+ const noise = (a, b, scale, oct, seed, opacity, blend) => ({ op: 'noise', colorA: a, colorB: b, scale, octaves: oct, seed, opacity, blend: blend || 'normal' });+ const spec3 = (usage, base, ops, size = S) => ({ schemaVersion: 3, width: size, height: size, usage, operations: [{ op: 'legacy-v2', spec: { schemaVersion: 2, size, usage, layers: base } }, ...ops] });+ const v3 = (usage, base, ops, size = S) => proceduralTexture(spec3(usage, base, ops, size));+ const stamps = (shape, rgba, count, seed, opacity, area) => ({ op: 'stamps', shape, rgba, count, seed, area: area || { x: 0, y: 0, width: S, height: S }, opacity });+ const ell = (w, h) => ({ kind: 'ellipse', x: 0, y: 0, width: w, height: h });+ const tool = (dx, dy, w = 1) => ({ kind: 'line', from: [0, 0], to: [dx, dy], width: w }); // a stroke of the claw chisel++ // ---- Weathered ashlar: 8 m repeat, every block its own stone. Colour varies by block, chisel marks+ // in the relief instead of round pits, rain and soot already sunk into the surface ----+ const quads = [[0, 0], [S, 0], [0, S], [S, S]];+ const ashAlbedo = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb9ad96 }, noise(0x968b76, 0xcdc3ad, 3, 2, seed + 1, 0.85), noise(0x7f7666, 0xc6bca6, 17, 2, seed + 2, 0.4)],+ ashlarOps(mkRng(seed), S, [[78, 72, 60], [226, 216, 190], [158, 132, 92], [122, 128, 124]], { w: 2, rgba: [70, 64, 54, 255], op: 225 }, [+ stamps(ell(20, 70), [70, 66, 56, 255], 26, seed + 3, 24), stamps(ell(40, 26), [52, 56, 46, 255], 16, seed + 4, 20), stamps(tool(11, 6), [96, 88, 74, 255], 110, seed + 5, 34),+ ]));+ const ashHeight = (q, seed) => spec3('albedo', [{ op: 'solid', color: 0xb4b4b4 }, noise(0x8c8c8c, 0xd0d0d0, 11, 2, seed + 6, 0.7)],+ ashlarOps(mkRng(seed), S, [[126, 126, 126], [214, 214, 214], [170, 170, 170], [150, 150, 150]], { w: 3, rgba: [12, 12, 12, 255], op: 255 }, [+ stamps(tool(14, 8), [86, 86, 86, 255], 120, seed + 8, 90), stamps(tool(-12, 9), [226, 226, 226, 255], 110, seed + 9, 70),+ ]));+ const atlasOf = async (usage, make, seeds) => textureAtlas({ schemaVersion: 1, width: 2 * S, height: 2 * S, pack: { edge: 2048, gutter: 0 }, usage, sampler: repeat, regions: quads.map((p, i) => ({ name: 'q' + i, content: { x: p[0], y: p[1], width: S, height: S }, gutter: 0, source: { kind: 'procedural', spec: make(p, seeds[i]) } })) });+ const ashA = await atlasOf('albedo', ashAlbedo, [77, 131, 211, 307]);+ const ashH = await atlasOf('albedo', ashHeight, [77, 131, 211, 307]);+ MAT.ashlar = pbrMaterial({ albedo: ashA, normal: normalMapFromHeight(ashH, { strength: 2.6 }), roughness: 0.94, metalness: 0, name: 'Limestone_Ashlar' });++ // Dressed stone: no joints; chisel strokes and mottling live in the colour, because a normal map here would+ // add 16 bytes of tangent to every vertex of every moulding+ const finA = v3('albedo', [{ op: 'solid', color: 0xc2b7a0 }, noise(0xb5ab95, 0xcdc4af, 5, 3, 31, 0.8), noise(0xa79d88, 0xcbc2ad, 23, 3, 32, 0.34)], [+ stamps(tool(9, 5), [120, 112, 96, 255], 130, 33, 34), stamps(tool(-7, 6), [214, 206, 188, 255], 80, 34, 30),+ ]);+ MAT.fine = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name: 'Limestone_Dressed' });+ const figTone = (name, r, g, b) => { const m = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, name }); m.color.setRGB(r, g, b); m.vertexColors = true; return m; };+ MAT.figure = figTone('Sculpture_Sunlit', 0.86, 0.8, 0.68); // figures take the same stone under different light+ MAT.figureHalf = figTone('Sculpture_HalfShade', 0.5, 0.49, 0.47);+ MAT.figureDeep = figTone('Sculpture_DeepShade', 0.3, 0.31, 0.34);+ MAT.figureIn = figTone('Sculpture_Interior', 0.62, 0.53, 0.42);++ // ---- Patinated lead: long sheets a metre wide running the whole way down the slope. They differ only a little from each+ // other, are lapped once in a long run, and carry the streaks that rain draws down them. The tile repeats every 8 m ----+ const LS = 512, leadOps = (tones, lapOp, seamOp, rngSeed) => {+ const r = mkRng(rngSeed), ops = [], cols = 8, W = LS / cols;+ for (let c = 0; c < cols; c++) {+ const t = tones[Math.floor(r() * tones.length)], lap = Math.floor((c % 2 ? 0.28 : 0.72) * LS + (r() - 0.5) * 40);+ ops.push({ op: 'paint', shape: { kind: 'rectangle', x: c * W, y: 0, width: W, height: LS }, rgba: [t[0], t[1], t[2], 255], opacity: 12 + Math.floor(r() * 26) });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, lap], to: [(c + 1) * W, lap], width: lapOp.w }, rgba: lapOp.rgba, opacity: lapOp.op });+ ops.push({ op: 'paint', shape: { kind: 'line', from: [c * W, 0], to: [c * W, LS], width: seamOp.w }, rgba: seamOp.rgba, opacity: seamOp.op });+ }+ return ops;+ };+ const leadArea = { x: 0, y: 0, width: LS, height: LS };+ const leadA = v3('albedo', [{ op: 'solid', color: 0x6f7a7e }, noise(0x566166, 0x8d9896, 5, 2, 41, 0.75)], [+ ...leadOps([[86, 98, 102], [118, 130, 130], [66, 76, 82], [140, 150, 146], [100, 104, 100]], { w: 2, rgba: [36, 42, 46, 255], op: 220 }, { w: 2, rgba: [40, 46, 50, 255], op: 180 }, 44),+ stamps({ kind: 'rectangle', x: 0, y: 0, width: 4, height: 180 }, [208, 214, 206, 255], 70, 45, 22, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 2, height: 260 }, [34, 40, 44, 255], 90, 46, 36, leadArea), stamps({ kind: 'rectangle', x: 0, y: 0, width: 6, height: 120 }, [150, 160, 154, 255], 40, 47, 16, leadArea),+ ], LS);+ const leadH = v3('albedo', [{ op: 'solid', color: 0x8c8c8c }, noise(0x848484, 0x989898, 9, 2, 48, 0.8)], [+ ...leadOps([[140, 140, 140], [132, 132, 132], [148, 148, 148]], { w: 3, rgba: [232, 232, 232, 255], op: 255 }, { w: 5, rgba: [250, 250, 250, 255], op: 255 }, 44),+ stamps(ell(17, 11), [116, 116, 116, 255], 110, 49, 60, leadArea),+ ], LS);+ MAT.lead = pbrMaterial({ albedo: leadA, normal: normalMapFromHeight(leadH, { strength: 3 }), roughness: 0.6, metalness: 0.5, name: 'Lead_Patinated' });++ // ---- Gold leaf: a bright warm metal that still reads as gold where the environment is dull ----+ const gldA = v3('albedo', [{ op: 'solid', color: 0xffc644 }, noise(0xf0a830, 0xffd764, 14, 2, 51, 0.6)], [stamps(ell(5, 4), [176, 110, 30, 255], 60, 52, 90, { x: 0, y: 0, width: 128, height: 128 })], 128);+ MAT.gold = pbrMaterial({ albedo: gldA, roughness: 0.25, metalness: 1, name: 'Gilding' });+ MAT.gold.color.setRGB(0.92, 0.78, 0.56);+ MAT.gold.emissive.setRGB(1, 0.6, 0.14); MAT.gold.emissiveIntensity = 0.1;++ // Aged oak and forged iron from the approved scanned sets+ MAT.oak = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.oak-veneer-albedo.v1'), normal: await loadApprovedTexture('kiln.texture.oak-veneer-normal.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.oak-veneer-arm.v1'), name: 'Oak_Aged' });+ MAT.iron = pbrMaterial({ albedo: await loadApprovedTexture('kiln.texture.rusted-metal-albedo.v1'), metallicRoughness: await loadApprovedTexture('kiln.texture.rusted-metal-arm.v1'), name: 'Iron_Forged' });++ // Paving: worn limestone and grey marble, polished by seven centuries of feet+ const pavA = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'checker', colorA: 0xd2cab6, colorB: 0x8c8678, squares: 4 }, noise(0x7c766a, 0xe0dac8, 9, 3, 61, 0.42), { op: 'bricks', brick: 0xffffff, mortar: 0x5a564e, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ const pavH = proceduralTexture({ schemaVersion: 2, size: 512, usage: 'albedo', layers: [{ op: 'solid', color: 0xc8c8c8 }, noise(0xb0b0b0, 0xdcdcdc, 15, 3, 62, 0.5), { op: 'bricks', brick: 0xffffff, mortar: 0x202020, rows: 4, cols: 4, mortarWidth: 0.018, stagger: 0, blend: 'multiply' }] });+ MAT.pave = pbrMaterial({ albedo: pavA, normal: normalMapFromHeight(pavH, { strength: 1.8 }), roughness: 0.42, metalness: 0, name: 'Paving_Worn' });+ for (const k of ['ashlar', 'fine', 'oak', 'iron', 'pave', 'lead']) MAT[k].vertexColors = true;++ // ---- Weathering decals: rain streaks, soot, ground damp and moss, cast shadow. Alpha-blended overlays+ // give crisp detail on walls that have very few vertices ----+ const DS = 256, rd = mkRng(99), polyStreak = (x, y0, L, w, rgb, op) => ({ op: 'paint', shape: { kind: 'polygon', points: [[x, y0], [x + w, y0], [x + w * 0.8 + 1, y0 + L * 0.45], [x + w * 0.55 + 1, y0 + L * 0.8], [x + w * 0.45, y0 + L], [x + w * 0.3, y0 + L * 0.75], [x + w * 0.15, y0 + L * 0.4]].map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: [rgb[0], rgb[1], rgb[2], 255], opacity: op });+ const streakOps = [], dripOps = [];+ for (let i = 0; i < 44; i++) { const x = Math.floor(rd() * (DS - 9)), dark = rd() > 0.26, L = 65 + Math.floor(rd() * 185), w = 2 + Math.floor(rd() * 7); streakOps.push(polyStreak(x, 0, Math.min(L, DS - 2), w, dark ? [44, 40, 34] : [176, 170, 150], dark ? 40 + Math.floor(rd() * 70) : 26 + Math.floor(rd() * 30))); }+ for (let i = 0; i < 12; i++) streakOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 3 + i * 2 }, rgba: [34, 32, 28, 255], opacity: 12 });+ for (let i = 0; i < 38; i++) { const x = Math.floor(rd() * (DS - 8)), L = 20 + Math.floor(rd() * 100), w = 1 + Math.floor(rd() * 6); dripOps.push(polyStreak(x, 0, Math.min(L, 125), w, [38, 36, 30], 44 + Math.floor(rd() * 70))); }+ for (let i = 0; i < 10; i++) dripOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: 2 + i * 2 }, rgba: [28, 26, 24, 255], opacity: 16 });+ const softOps = []; for (let i = 0; i < 30; i++) softOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: DS, height: Math.max(1, Math.round(128 * Math.pow(1 - i / 30, 1.5))) }, rgba: [18, 18, 24, 255], opacity: 15 });+ const dampOps = [];+ for (let i = 0; i < 18; i++) { const h = Math.round(DS * Math.pow((i + 1) / 18, 1.6)); dampOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: DS - h, width: DS, height: h }, rgba: i % 3 ? [38, 42, 28, 255] : [50, 58, 34, 255], opacity: 9 }); }+ dampOps.push(stamps(ell(13, 8), [66, 84, 42, 255], 110, 71, 46, { x: 0, y: 120, width: DS, height: 134 }), stamps(ell(5, 28), [30, 32, 24, 255], 80, 72, 64, { x: 0, y: 60, width: DS, height: 190 }), stamps(ell(3, 3), [120, 132, 76, 255], 60, 73, 90, { x: 0, y: 100, width: DS, height: 150 }));+ const clear = [0, 0, 0, 0];+ const dreg = (name, x, y, w, h, ops, bg) => ({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'albedo', background: bg || clear, operations: ops } } });+ const decalTex = await textureAtlas({ schemaVersion: 1, width: 512, height: 512, usage: 'albedo', regions: [+ dreg('streak', 0, 0, DS, DS, streakOps.slice(0, 64)),+ dreg('soft', 256, 0, DS, 128, softOps),+ dreg('soot', 256, 128, DS, 128, [stamps(ell(35, 27), [24, 22, 20, 255], 150, 74, 20, { x: 20, y: 15, width: 200, height: 90 }), stamps(ell(13, 11), [18, 16, 14, 255], 90, 75, 36, { x: 45, y: 25, width: 160, height: 75 })]),+ dreg('ground', 0, 256, DS, DS, dampOps),+ dreg('drip', 256, 384, DS, 128, dripOps.slice(0, 64)),+ ] });+ MAT.decal = pbrMaterial({ albedo: decalTex, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Weathering_Decals' });+ MAT.decal.depthWrite = false;++ await makeGlassMaterials();++ // ---- The light of the south windows: the same paving and the same stone, tinted per vertex by bakeWindowLight, so light multiplies what it lands on ----+ MAT.pool = pbrMaterial({ albedo: pavA, roughness: 0.42, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightOnPaving' });+ MAT.pool.vertexColors = true;+ MAT.pool.depthWrite = false;+ MAT.poolStone = pbrMaterial({ albedo: finA, roughness: 0.92, metalness: 0, alphaMode: 'blend', name: 'StainedGlass_LightOnStone' });+ MAT.poolStone.vertexColors = true;+ MAT.poolStone.depthWrite = false;++ // ---- The ground: worn flags in irregular courses, and rough grass over packed earth ----+ const flagTones = [[74, 70, 62], [150, 142, 124], [112, 104, 88], [96, 98, 90]];+ const flagA = v3('albedo', [{ op: 'solid', color: 0x8f8778 }, noise(0x6f695c, 0xa89f8d, 4, 2, 81, 0.8), noise(0x5e594e, 0x9a9282, 19, 2, 82, 0.35)], ashlarOps(mkRng(83), S, flagTones, { w: 4, rgba: [38, 34, 28, 255], op: 235 }, [stamps(ell(30, 18), [168, 160, 140, 255], 40, 84, 26), stamps(tool(8, 3), [70, 66, 58, 255], 70, 85, 30)]));+ const flagH = v3('albedo', [{ op: 'solid', color: 0xb0b0b0 }, noise(0x969696, 0xc4c4c4, 9, 2, 86, 0.6)], ashlarOps(mkRng(83), S, [[140, 140, 140], [190, 190, 190], [165, 165, 165], [150, 150, 150]], { w: 5, rgba: [20, 20, 20, 255], op: 255 }, [stamps(ell(26, 16), [205, 205, 205, 255], 30, 87, 40)]));+ MAT.flag = pbrMaterial({ albedo: flagA, normal: normalMapFromHeight(flagH, { strength: 2.2 }), roughness: 0.9, metalness: 0, name: 'Ground_Flagstone' });+ MAT.flag.vertexColors = true;+ const EA = 256, eArea = { x: 0, y: 0, width: EA, height: EA };+ const earthA = v3('albedo', [{ op: 'solid', color: 0x5d5a3a }, noise(0x4a4a2c, 0x6f6a44, 5, 2, 91, 0.8), noise(0x3f4226, 0x7a7550, 23, 2, 92, 0.4)], [stamps(tool(2, 8), [70, 88, 40, 255], 120, 93, 90, eArea), stamps(tool(-2, 7), [104, 112, 58, 255], 90, 94, 70, eArea), stamps(ell(26, 16), [88, 74, 52, 255], 30, 95, 60, eArea)], EA);+ MAT.earth = pbrMaterial({ albedo: earthA, roughness: 1, metalness: 0, alphaMode: 'blend', name: 'Ground_Earth' });+ MAT.earth.vertexColors = true;+ MAT.earth.depthWrite = false;+}++// ---- Atlas of the glass painter: pot-metal colours, painted drapery, drawn faces, ornament ------+const GL = { w: 1536, h: 1280, cell: 256, small: 128, fam: {}, tile: {}, face: {}, orn: {}, ground: {} };+// [name, base, light, dark]: real pot-metal glass is deep, and no two sheets match+const GLASS_FAMILIES = [+ ['ruby', [118, 6, 14], [172, 14, 22], [66, 3, 10]], ['cobalt', [10, 20, 104], [20, 44, 160], [5, 10, 62]], ['azure', [16, 54, 128], [36, 90, 170], [8, 32, 88]], ['emerald', [6, 62, 38], [14, 96, 54], [3, 38, 24]],+ ['green', [42, 74, 24], [74, 106, 34], [24, 48, 14]], ['gold', [178, 116, 16], [218, 158, 36], [130, 78, 8]], ['amber', [146, 72, 10], [186, 106, 20], [98, 44, 6]], ['purple', [58, 14, 80], [90, 26, 114], [34, 6, 50]],+ ['flesh', [186, 140, 104], [206, 164, 128], [156, 110, 80]], ['white', [172, 164, 132], [204, 194, 158], [136, 128, 100]], ['brown', [70, 40, 18], [100, 58, 28], [46, 24, 12]],+ ['teal', [8, 62, 70], [16, 92, 100], [5, 38, 46]], ['night', [5, 8, 40], [10, 16, 62], [2, 4, 24]],+];+const GLASS_ALIAS = { rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', sky: 'azure', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', yellow: 'gold', tan: 'amber', fleshDk: 'flesh', violet: 'purple', olive: 'green', forest: 'emerald', hair: 'brown', paint: 'lead' };+// Mosaic grounds, each a square of painted lozenges: [name, colour A, colour B, jewel]+// A ground stays one colour, in many sheets of it: ruby beside cobalt blends to magenta across a room, ruby on ruby does not+const GLASS_GROUNDS = [['rc', 'ruby', 'ruby', 'gold'], ['cr', 'cobalt', 'cobalt', 'gold'], ['eg', 'emerald', 'emerald', 'gold'], ['pr', 'cobalt', 'azure', 'white']];+// What a leaded window looks like from outside by day: nearly black quarries, each with a hint of its colour, and a faint grey came+function outerGlassOps() {+ const r = mkRng(4711), ops = [], cl = (v) => Math.max(0, Math.min(256, v)), hints = [[88, 10, 16], [12, 20, 96], [12, 20, 70], [70, 10, 14], [22, 26, 34], [66, 44, 12]];+ for (let b = 0; b <= 4; b++) for (let a = 0; a <= 4; a++) {+ if ((a + b) % 2 === 0) continue;+ const c = hints[Math.floor(r() * hints.length)], x = a * 64, y = b * 64;+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 64)], [cl(x + 64), cl(y)], [cl(x), cl(y + 64)], [cl(x - 64), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -1; k <= 1; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ for (let k = 1; k <= 3; k++) { const q = k * 128; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [118, 118, 122, 255] }); }+ return ops;+}+function groundOps(A, B, J, seed) { // lozenges 64 px across, a lead round each, a jewel on some crossings+ const r = mkRng(seed * 19 + 5), ops = [], cl = (v) => Math.max(0, Math.min(256, v));+ for (let b = 0; b <= 8; b++) for (let a = 0; a <= 8; a++) {+ if ((a + b) % 2 === 0) continue;+ const f = b % 2 ? A : B, x = a * 32, y = b * 32, t = r(), c = t < 0.45 ? f[1] : t < 0.8 ? f[2] : f[3];+ ops.push({ op: 'paint', shape: { kind: 'polygon', points: [[cl(x), cl(y - 32)], [cl(x + 32), cl(y)], [cl(x), cl(y + 32)], [cl(x - 32), cl(y)]] }, rgba: [c[0], c[1], c[2], 255], opacity: 255 });+ }+ for (let k = -3; k <= 3; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, -q), Math.max(0, q)], to: [Math.min(256, 256 - q), Math.min(256, 256 + q)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let k = 1; k <= 7; k++) { const q = k * 64; ops.push({ op: 'paint', shape: { kind: 'line', from: [Math.max(0, q - 256), Math.min(256, q)], to: [Math.min(256, q), Math.max(0, q - 256)], width: 4 }, rgba: [4, 4, 5, 255] }); }+ for (let i = 0; i < 4; i++) { const x = 64 + (i % 2) * 128, y = 64 + (i >> 1) * 128; ops.push(paintEll(x, y, 13, 13, 255, [4, 4, 5])); ops.push(paintEll(x, y, 8, 8, 255, J[1])); }+ return ops.slice(0, 64);+}+const DRAPE_OF = { ruby: 'ruby', rubyLt: 'ruby', crimson: 'ruby', rose: 'ruby', cobalt: 'cobalt', azure: 'cobalt', sky: 'cobalt', night: 'cobalt', emerald: 'emerald', green: 'emerald', teal: 'emerald', forest: 'emerald', olive: 'emerald', gold: 'gold', amber: 'gold', yellow: 'gold', tan: 'gold', brown: 'gold', white: 'white', pale: 'white', pearl: 'white', bone: 'white', grey: 'white', purple: 'purple', violet: 'purple' };+const GROUND_OF = { ruby: 'rc', rubyLt: 'rc', crimson: 'rc', cobalt: 'cr', azure: 'pr', sky: 'pr', emerald: 'eg', green: 'eg', violet: 'cr', purple: 'cr', teal: 'pr', outer: 'out' };+const PAINT = [36, 20, 10];+const paintLine = (a, b, w, op = 235) => ({ op: 'paint', shape: { kind: 'line', from: [Math.round(a[0]), Math.round(a[1])], to: [Math.round(b[0]), Math.round(b[1])], width: w }, rgba: [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintPoly = (pts, op, rgb) => ({ op: 'paint', shape: { kind: 'polygon', points: pts.map((p) => [Math.round(p[0]), Math.round(p[1])]) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });+const paintEll = (x, y, w, h, op, rgb) => ({ op: 'paint', shape: { kind: 'ellipse', x: Math.round(x - w / 2), y: Math.round(y - h / 2), width: Math.round(w), height: Math.round(h) }, rgba: rgb ? [rgb[0], rgb[1], rgb[2], 255] : [PAINT[0], PAINT[1], PAINT[2], 255], opacity: op });++function familyOps(f, seed) { // one sheet of pot-metal: streaks, clouds, seeds, a thicker edge+ const r = mkRng(seed * 7 + 3), ops = [], C = GL.small, clampE = (x, w) => Math.max(w / 2, Math.min(C - w / 2, x));+ for (let i = 0; i < 10; i++) { const c = r() > 0.5 ? f[2] : f[3], w = 24 + Math.floor(r() * 60), h = 18 + Math.floor(r() * 44); ops.push(paintEll(clampE(r() * C, w), clampE(r() * C, h), w, h, 60 + Math.floor(r() * 90), c)); }+ for (let i = 0; i < 9; i++) { const c = r() > 0.5 ? f[2] : f[3], y = Math.floor(r() * C); ops.push({ op: 'paint', shape: { kind: 'line', from: [0, y], to: [C, Math.max(0, Math.min(C, y + Math.floor((r() - 0.5) * 30)))], width: 2 + Math.floor(r() * 4) }, rgba: [c[0], c[1], c[2], 255], opacity: 70 + Math.floor(r() * 80) }); }+ ops.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 2, height: 2 }, rgba: [f[2][0], f[2][1], f[2][2], 255], count: 40, seed: seed + 11, area: { x: 0, y: 0, width: C, height: C }, opacity: 180 });+ return ops;+}+function drapeOps(f, seed) { // brown paint on coloured glass: long folds, each with a soft wash beside its trace line+ const r = mkRng(seed * 13 + 1), ops = [], W = 256, H = 512, dk = [Math.round(f[1][0] * 0.3), Math.round(f[1][1] * 0.3), Math.round(f[1][2] * 0.3)];+ for (let i = 0; i < 13; i++) {+ const x = 18 + i * 18 + (r() - 0.5) * 10, sw = (r() - 0.5) * 42, y0 = 4 + r() * 120, y1 = H - r() * 90, ym = (y0 + y1) / 2, wash = 9 + r() * 12;+ ops.push(paintPoly([[x, y0], [x + wash, y0 + 10], [x + sw * 0.6 + wash * 1.4, ym], [x + sw + wash, y1 - 10], [x + sw, y1], [x + sw * 0.6, ym]], 62 + Math.floor(r() * 36), PAINT));+ ops.push(paintLine([x, y0], [x + sw * 0.6, ym], 3)); ops.push(paintLine([x + sw * 0.6, ym], [x + sw, y1], 2));+ if (i % 3 === 0) ops.push(paintLine([x + sw * 0.6 + wash * 1.6, ym - 40], [x + sw + wash * 1.6, y1 - 60], 3, 150));+ }+ for (let i = 0; i < 4; i++) { const y = 60 + i * 110 + r() * 40; ops.push(paintPoly([[0, y], [W, y + 26 * (r() - 0.5)], [W, y + 22], [0, y + 18]], 40, PAINT)); }+ return ops.slice(0, 62);+}+// A drawn medieval head, 256 x 256, centred: trace lines in brown over a flesh pane, with washes for the modelling+function faceOps(kind) {+ const ops = [], c = 128, mir = (p) => [256 - p[0], p[1]], both = (fn) => { fn((p) => p); fn(mir); };+ const hairC = kind === 'elder' ? [150, 146, 122] : kind === 'angel' ? [150, 96, 30] : [66, 40, 18];+ // washes: the side of the face and temple, under the brow, beside the nose, under the lip+ both((m) => {+ ops.push(paintPoly([m([18, 84]), m([52, 74]), m([46, 150]), m([72, 214]), m([34, 196])], 78));+ ops.push(paintEll(m([88, 104])[0], 106, 58, 20, 64));+ ops.push(paintPoly([m([122, 118]), m([112, 160]), m([102, 172]), m([118, 172])], 58));+ });+ ops.push(paintEll(c, 200, 52, 11, 58));+ // hair+ if (kind !== 'virgin') {+ ops.push(paintPoly([[6, 118], [14, 56], [60, 10], [128, 0], [196, 10], [242, 56], [250, 118], [222, 96], [206, 56], [160, 42], [128, 54], [96, 42], [50, 56], [34, 96]], 255, hairC));+ for (let i = 0; i < 7; i++) { const a = 28 + i * 16; ops.push(paintLine([a, 94 - i * 7], [a + 36, 12 + i * 2], 3, 190)); ops.push(paintLine([256 - a, 94 - i * 7], [256 - a - 36, 12 + i * 2], 3, 190)); }+ } else {+ ops.push(paintPoly([[0, 150], [2, 56], [50, 8], [128, 0], [206, 8], [254, 56], [256, 150], [226, 118], [214, 66], [128, 46], [42, 66], [30, 118]], 255, [214, 216, 186]));+ ops.push(paintLine([38, 124], [50, 62], 4)); ops.push(paintLine([50, 62], [128, 44], 4)); ops.push(paintLine([128, 44], [206, 62], 4)); ops.push(paintLine([206, 62], [218, 124], 4));+ }+ // brows, lids, eyes+ both((m) => {+ ops.push(paintLine(m([60, 96]), m([84, 84]), 5)); ops.push(paintLine(m([84, 84]), m([116, 92]), 5));+ ops.push(paintLine(m([64, 116]), m([84, 104]), 4)); ops.push(paintLine(m([84, 104]), m([112, 116]), 4));+ ops.push(paintLine(m([66, 118]), m([90, 126]), 2)); ops.push(paintLine(m([90, 126]), m([112, 116]), 2));+ ops.push(paintEll(m([88, 115])[0], 115, 15, 15, 255));+ });+ // nose, nostril, mouth, chin+ ops.push(paintLine([120, 92], [114, 150], 4)); ops.push(paintLine([114, 150], [106, 166], 4)); ops.push(paintLine([106, 166], [124, 174], 3)); ops.push(paintLine([136, 172], [150, 166], 3));+ ops.push(paintLine([98, 192], [128, 187], 4)); ops.push(paintLine([128, 187], [158, 192], 4));+ ops.push(paintLine([112, 202], [144, 202], 2, 170));+ if (kind === 'elder' || kind === 'christ' || kind === 'king') {+ const bc = kind === 'elder' ? [168, 164, 138] : [76, 46, 20];+ ops.push(paintPoly([[40, 158], [66, 184], [94, 196], [128, 206], [162, 196], [190, 184], [216, 158], [206, 226], [160, 256], [96, 256], [50, 226]], 255, bc));+ for (let i = 0; i < 7; i++) { const x = 64 + i * 21; ops.push(paintLine([x, 192 + Math.abs(i - 3) * -3], [x + (i - 3) * 3, 252], 3, 210)); }+ ops.push(paintLine([78, 180], [120, 176], 4)); ops.push(paintLine([178, 180], [136, 176], 4));+ } else {+ ops.push(paintLine([100, 226], [128, 236], 3, 170)); ops.push(paintLine([128, 236], [156, 226], 3, 170));+ }+ return ops.slice(0, 64);+}+function ornOps(f, kind, seed) { // painted ornament for borders: a running stem with leaves, scratched out of a dark ground+ const ops = [], C = GL.small;+ ops.push(paintPoly([[0, 0], [C, 0], [C, C], [0, C]], 210));+ const lit = f[2];+ for (let i = 0; i < 5; i++) {+ const y = 13 + i * 26, s = i % 2 ? 1 : -1;+ ops.push(paintPoly([[C / 2, y - 11], [C / 2 + s * 36, y - 6], [C / 2 + s * 46, y + 7], [C / 2 + s * 23, y + 11], [C / 2, y + 13]], 255, lit));+ ops.push(paintLine([C / 2 + s * 4, y - 3], [C / 2 + s * 37, y + 4], 2));+ ops.push(paintLine([C / 2 + s * 4, y + 5], [C / 2 + s * 28, y + 6], 1));+ }+ ops.push({ op: 'paint', shape: { kind: 'line', from: [C / 2, 0], to: [C / 2, C], width: 7 }, rgba: [lit[0], lit[1], lit[2], 255] });+ return ops;+}+async function makeGlassMaterials() {+ const regions = [], C = GL.cell, Sm = GL.small;+ const addR = (name, x, y, w, h, bg, ops) => regions.push({ name, content: { x, y, width: w, height: h }, gutter: 0, source: { kind: 'procedural', spec: { schemaVersion: 3, width: w, height: h, usage: 'emissive', background: [bg[0], bg[1], bg[2], 255], operations: ops } } });+ // Layout: two rows of small cells (pot-metals, then ornament), then drapery, faces and mosaic grounds+ GLASS_FAMILIES.forEach((f, i) => { const x = (i % 12) * Sm, y = Math.floor(i / 12) * Sm; GL.fam[f[0]] = [x, y]; addR('fam_' + f[0], x, y, Sm, Sm, f[1], familyOps(f, i + 1)); });+ [['gold', 'gold'], ['white', 'white']].forEach((q, i) => { const f = GLASS_FAMILIES.find((v) => v[0] === q[0]), x = (i + 2) * Sm, y = Sm; GL.orn[q[1]] = [x, y]; addR('orn_' + q[1], x, y, Sm, Sm, f[1], ornOps(f, q[1], i + 90)); });+ // painted drapery, one tall tile per colour+ ['ruby', 'cobalt', 'emerald', 'gold', 'white', 'purple'].forEach((n, i) => { const f = GLASS_FAMILIES.find((q) => q[0] === n), x = i * C, y = C; GL.tile[n] = [x, y, C, 2 * C]; addR('drape_' + n, x, y, C, 2 * C, f[1], drapeOps(f, i + 40)); });+ // faces+ ['youth', 'elder', 'christ', 'king', 'virgin', 'angel'].forEach((n, i) => { const x = i * C, y = 3 * C; GL.face[n] = [x, y]; addR('face_' + n, x, y, C, C, n === 'virgin' ? [202, 162, 128] : [192, 148, 112], faceOps(n)); });+ // mosaic grounds+ GLASS_GROUNDS.forEach((q, i) => { const fam = (n) => GLASS_FAMILIES.find((v) => v[0] === n), x = i * C, y = 4 * C; GL.ground[q[0]] = [x, y]; addR('ground_' + q[0], x, y, C, C, fam(q[1])[3], groundOps(fam(q[1]), fam(q[2]), fam(q[3]), i + 3)); });+ GL.ground.out = [4 * C, 4 * C]; addR('ground_out', 4 * C, 4 * C, C, C, [10, 12, 18], outerGlassOps());+ GL.fam.lead = [1400, 140]; // the empty part of the sheet is black, and the came needs no pixels of its own+ const atlas = await textureAtlas({ schemaVersion: 1, usage: 'emissive', width: GL.w, height: GL.h, pack: { edge: 2048, gutter: 0 }, regions });+ // Seen from inside the glass is the only light: a black pane that shines+ MAT.glassIn = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow, roughness: 1, metalness: 0, name: 'StainedGlass_Interior' });+ MAT.glassIn.color.setRGB(0, 0, 0);+ // Seen from outside by day it is nearly black, a dull sheen and the colour only hinting through.+ // A rough surface keeps grazing angles from washing out to a pale reflection of the sky.+ MAT.glassOut = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.6, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior' });+ MAT.glassOut.color.setRGB(0.008, 0.009, 0.012);+ // Every other window from outside: the outer mosaic only, near black, with the came just catching the eye+ MAT.glassOutWin = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassOuterGlow * 0.62, roughness: 0.82, metalness: 0, name: 'StainedGlass_Exterior_Windows' });+ MAT.glassOutWin.color.setRGB(0.008, 0.009, 0.012);+ // The great rose burns a little brighter than any other window in the building+ MAT.glassRose = pbrMaterial({ emissive: atlas, emissiveIntensity: P.glassGlow * 1.22, roughness: 1, metalness: 0, name: 'StainedGlass_Rose' });+ MAT.glassRose.color.setRGB(0, 0, 0);+}++// Hierarchy helper: every mesh gets a unique role name; geometry is shared between placements.+let ROOT = null; const NAMES = new Map();+function uniq(n) { const c = (NAMES.get(n) || 0) + 1; NAMES.set(n, c); return c === 1 ? n : n + '_' + c; }+const STAT = new Map();+// Exporter costs measured on this host: pos+normal+uv 32 B, COLOR_0 written as 12 B floats+function batchBytes(b) { b.weld(); return b.count * (32 + (b.noColor ? 0 : 12)) + b.I.length * (b.count > 65535 ? 4 : 2); }+function place(name, batch, mat, o = {}) {+ if (!batch || !batch.I.length) return null;+ if (mat === MAT.gold) batch.noColor = true; // metal needs no baked shading: saves 12 B a vertex+ if (P.preview === 'unique' && batch.geo) return null; // size test: every geometry once, no instances+ if (mat === MAT.ashlar && !batch.tessellated && !batch.geo) { batch.tess(P.wallTessellation); batch.tessellated = true; }+ if (P.preview === 'stats') {+ const e = STAT.get(batch) || { name, n: 0, normalMapped: mat === MAT.ashlar || mat === MAT.lead || mat === MAT.pave || mat === MAT.oak };+ e.n++; STAT.set(batch, e);+ return null;+ }+ const part = createPart(uniq(name), batch.toGeo(), mat, { position: o.pos || [0, 0, 0], rotation: o.rot || [0, 0, 0], scale: o.scale || [1, 1, 1], parent: o.parent || ROOT });+ PLACED.push({ part, batch, mat });+ return part;+}+const PLACED = [];+function group(name, parent) { const g = new THREE.Group(); g.name = uniq(name); (parent || ROOT).add(g); return g; }++// ==================================================================================+// SCULPTURE: robed figures with carved drapery. A figure is a full surface, not a block:+// S-curve stance, tube folds breaking at the hem, swags across the body, sleeves, hands,+// modelled faces, hair and beards, and the attributes each saint carries.+// ==================================================================================+// Every figure is a signed-distance volume: a long column of cloth with deep pleats, a mantle+// laid over it, arms under the cloth, and a separately resolved head and hands. A surface-net+// pass extracts the skin, the simplifier keeps the carving and drops the rest, and ambient+// occlusion from the field itself is baked into the valleys of the folds.+const len3 = (x, y, z) => Math.sqrt(x * x + y * y + z * z);+const smin = (a, b, k) => { const h = Math.max(k - Math.abs(a - b), 0) / k; return Math.min(a, b) - h * h * k * 0.25; };+const smax = (a, b, k) => -smin(-a, -b, k);+const sdEll = (x, y, z, rx, ry, rz) => { const a = x / rx, b = y / ry, c = z / rz, k0 = Math.sqrt(a * a + b * b + c * c), d = a / rx, e = b / ry, f = c / rz, k1 = Math.sqrt(d * d + e * e + f * f); return k1 > 1e-9 ? k0 * (k0 - 1) / k1 : -Math.min(rx, ry, rz); };+// Rounded cone between two points: the workhorse of fingers, locks, lids and sleeves+function sdCone(x, y, z, ax, ay, az, bx, by, bz, r1, r2) {+ const px = x - ax, py = y - ay, pz = z - az, dx = bx - ax, dy = by - ay, dz = bz - az;+ const t = clamp((px * dx + py * dy + pz * dz) / (dx * dx + dy * dy + dz * dz));+ return len3(px - dx * t, py - dy * t, pz - dz * t) - (r1 + (r2 - r1) * t);+}+// Surface nets over a sparse grid: only cells near the surface are ever evaluated+function polygonize(F, box, cell, bandK = 1.75) {+ const [x0, y0, z0, x1, y1, z1] = box, nx = Math.ceil((x1 - x0) / cell) + 2, ny = Math.ceil((y1 - y0) / cell) + 2, nz = Math.ceil((z1 - z0) / cell) + 2;+ const N = nx * ny * nz, val = new Float32Array(N), seen = new Uint8Array(N), cellV = new Int32Array(N).fill(-1);+ const b4 = 3.46 * cell * bandK, b2 = 1.73 * cell * bandK, active = [], inA = new Uint8Array(N);+ const activate = (i, j, k) => { const q = i + nx * (j + ny * k); if (!inA[q]) { inA[q] = 1; active.push(i, j, k); } };+ const at = (i, j, k) => { const q = i + nx * (j + ny * k); if (!seen[q]) { seen[q] = 1; val[q] = F(x0 + i * cell, y0 + j * cell, z0 + k * cell); } return val[q]; };+ // Two-level descent: a coarse block, then its octants, then cells, so the field is only asked near its surface+ for (let kc = 0; kc < nz - 1; kc += 4) for (let jc = 0; jc < ny - 1; jc += 4) for (let ic = 0; ic < nx - 1; ic += 4) {+ if (Math.abs(F(x0 + (ic + 2) * cell, y0 + (jc + 2) * cell, z0 + (kc + 2) * cell)) >= b4) continue;+ for (let k2 = kc; k2 < Math.min(kc + 4, nz - 1); k2 += 2) for (let j2 = jc; j2 < Math.min(jc + 4, ny - 1); j2 += 2) for (let i2 = ic; i2 < Math.min(ic + 4, nx - 1); i2 += 2) {+ if (Math.abs(F(x0 + (i2 + 1) * cell, y0 + (j2 + 1) * cell, z0 + (k2 + 1) * cell)) >= b2) continue;+ for (let k = k2; k < Math.min(k2 + 2, nz - 1); k++) for (let j = j2; j < Math.min(j2 + 2, ny - 1); j++) for (let i = i2; i < Math.min(i2 + 2, nx - 1); i++) activate(i, j, k);+ }+ }+ const P = [], I = [], E = [[0, 1], [2, 3], [4, 5], [6, 7], [0, 2], [1, 3], [4, 6], [5, 7], [0, 4], [1, 5], [2, 6], [3, 7]], v = new Float64Array(8);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ let mask = 0;+ for (let c = 0; c < 8; c++) { const d = at(i + (c & 1), j + ((c >> 1) & 1), k + ((c >> 2) & 1)); v[c] = d; if (d < 0) mask |= 1 << c; }+ if (mask === 0 || mask === 255) continue;+ // Follow the surface through every face it crosses: a steep pleat can fool the coarse test above, and a skipped cell is a hole+ const crosses = (m) => (mask & m) !== 0 && (mask & m) !== m;+ if (i > 0 && crosses(0x55)) activate(i - 1, j, k);+ if (i < nx - 2 && crosses(0xaa)) activate(i + 1, j, k);+ if (j > 0 && crosses(0x33)) activate(i, j - 1, k);+ if (j < ny - 2 && crosses(0xcc)) activate(i, j + 1, k);+ if (k > 0 && crosses(0x0f)) activate(i, j, k - 1);+ if (k < nz - 2 && crosses(0xf0)) activate(i, j, k + 1);+ let sx = 0, sy = 0, sz = 0, n = 0;+ for (let e = 0; e < 12; e++) {+ const c0 = E[e][0], c1 = E[e][1], d0 = v[c0], d1 = v[c1];+ if ((d0 < 0) === (d1 < 0)) continue;+ const t = d0 / (d0 - d1);+ sx += lerp(c0 & 1, c1 & 1, t); sy += lerp((c0 >> 1) & 1, (c1 >> 1) & 1, t); sz += lerp((c0 >> 2) & 1, (c1 >> 2) & 1, t); n++;+ }+ cellV[i + nx * (j + ny * k)] = P.length / 3;+ P.push(x0 + (i + sx / n) * cell, y0 + (j + sy / n) * cell, z0 + (k + sz / n) * cell);+ }+ const quad = (a, b, c, d, flip) => { if (a < 0 || b < 0 || c < 0 || d < 0) return; if (flip) I.push(a, c, b, a, d, c); else I.push(a, b, c, a, c, d); };+ const cv = (i, j, k) => (i < 0 || j < 0 || k < 0 ? -1 : cellV[i + nx * (j + ny * k)]);+ for (let a = 0; a < active.length; a += 3) {+ const i = active[a], j = active[a + 1], k = active[a + 2];+ const s0 = at(i, j, k) < 0;+ if (s0 !== (at(i + 1, j, k) < 0)) quad(cv(i, j - 1, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i, j - 1, k), !s0);+ if (s0 !== (at(i, j + 1, k) < 0)) quad(cv(i - 1, j, k - 1), cv(i, j, k - 1), cv(i, j, k), cv(i - 1, j, k), s0);+ if (s0 !== (at(i, j, k + 1) < 0)) quad(cv(i - 1, j - 1, k), cv(i, j - 1, k), cv(i, j, k), cv(i - 1, j, k), !s0);+ }+ return { P, I };+}+function meshToGeo(m) {+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(m.P), 3));+ g.setIndex(new THREE.BufferAttribute(m.P.length / 3 > 65535 ? new Uint32Array(m.I) : new Uint16Array(m.I), 1));+ g.computeVertexNormals();+ return g;+}+const AO_STEPS = [0.016, 0.05, 0.13];+// Extract, simplify, and write into a batch with field normals and field ambient occlusion+async function carveInto(b, F, box, cell, tris, xf, occ, opt = {}) {+ const raw = polygonize(F, box, cell);+ if (!raw.I.length) return;+ if (opt.cull) { // drop a region that a finer pass will carve instead+ const I2 = [], used = new Int32Array(raw.P.length / 3).fill(-1), P2 = [], mapV = (v) => { if (used[v] < 0) { used[v] = P2.length / 3; P2.push(raw.P[v * 3], raw.P[v * 3 + 1], raw.P[v * 3 + 2]); } return used[v]; };+ for (let t = 0; t < raw.I.length; t += 3) {+ const a = raw.I[t], c = raw.I[t + 1], e = raw.I[t + 2], mx = (raw.P[a * 3] + raw.P[c * 3] + raw.P[e * 3]) / 3, my = (raw.P[a * 3 + 1] + raw.P[c * 3 + 1] + raw.P[e * 3 + 1]) / 3, mz = (raw.P[a * 3 + 2] + raw.P[c * 3 + 2] + raw.P[e * 3 + 2]) / 3;+ if (!opt.cull(mx, my, mz)) I2.push(mapV(a), mapV(c), mapV(e));+ }+ raw.P = P2; raw.I = I2;+ }+ let g = meshToGeo(raw);+ if (raw.I.length / 3 > tris) g = await simplifyGeometry(g, { targetTriangles: tris });+ const pos = g.attributes.position, idx = g.index, base = b.count, h = cell * (opt.smooth || 0.6), aoK = opt.ao === undefined ? 0.5 : opt.ao, aoMin = opt.aoMin === undefined ? 0.16 : opt.aoMin;+ b.at(xf || M.I(), () => {+ for (let i = 0; i < pos.count; i++) {+ let x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i);+ const f0 = F(x, y, z), g0 = F(x + h, y, z) - F(x - h, y, z), g1 = F(x, y + h, z) - F(x, y - h, z), g2 = F(x, y, z + h) - F(x, y, z - h), l = len3(g0, g1, g2) || 1;+ const nx = g0 / l, ny = g1 / l, nz = g2 / l;+ // settle the vertex onto the true surface: this is what keeps fold valleys crisp+ x -= nx * f0; y -= ny * f0; z -= nz * f0;+ const O = occ || F;+ const wx = b.m[0] * x + b.m[1] * y + b.m[2] * z + b.m[3], wy = b.m[4] * x + b.m[5] * y + b.m[6] * z + b.m[7], wz = b.m[8] * x + b.m[9] * y + b.m[10] * z + b.m[11];+ const nm = b.nm, ax = nm[0] * nx + nm[1] * ny + nm[2] * nz, ay = nm[3] * nx + nm[4] * ny + nm[5] * nz, az = nm[6] * nx + nm[7] * ny + nm[8] * nz, al = len3(ax, ay, az) || 1;+ let o = 0, w = 1;+ for (let q = 0; q < 3; q++) { const d = AO_STEPS[q]; o += w * Math.max(0, d - O(wx + ax / al * d, wy + ay / al * d, wz + az / al * d)) / d; w *= 0.66; }+ const ao = clamp(1 - o * aoK, aoMin, 1);+ // UV by a slanted planar projection: no seam on a figure, the grain runs up the stone+ b.v(x, y, z, nx, ny, nz, wz * 0.8 + wx * 0.6, wy, ao);+ }+ for (let t = 0; t < idx.count; t += 3) b.tri(base + idx.getX(t), base + idx.getX(t + 1), base + idx.getX(t + 2));+ });+}++// ---- The head: a real skull, brow, eyes with lids, nose, lips, chin, hair, beard -------------+const LID_U = [[0.0795, 0.0035, 0.0165, 0.003], [0.0848, 0.0112, 0.0238, 0.0038], [0.0866, 0.0152, 0.0338, 0.0043], [0.0838, 0.0122, 0.0438, 0.0036], [0.0755, 0.0062, 0.052, 0.0024]];+const LID_L = [[0.0795, 0.0035, 0.0165, 0.0022], [0.0836, -0.0012, 0.0245, 0.0028], [0.0852, -0.0032, 0.034, 0.003], [0.0822, -0.0006, 0.044, 0.0026], [0.0755, 0.0062, 0.052, 0.0022]];+function headField(o) {+ const sm = o.smile || 0, cheek = o.gaunt ? 0.5 : 1, up = sm * 0.0065;+ const locks = [], rng = mkRng(o.seed * 37 + 11);+ // Long locks fall from the temples and the back of the head to the shoulders+ if (o.hair === 'long' || o.hair === 'curled') {+ const nL = o.hair === 'curled' ? 7 : 9;+ for (let i = 0; i < nL; i++) {+ const f = i / (nL - 1), a = lerp(1.05, 2.95, f), r0 = 0.092, wob = 0.012 + rng() * 0.008, dr = o.hair === 'curled' ? -0.165 : -0.235;+ for (const s of [-1, 1]) {+ const p0 = [r0 * Math.cos(a) - 0.012, 0.02 + rng() * 0.02, s * r0 * Math.sin(a) * 0.9];+ const p1 = [p0[0] * 1.14 - 0.008, p0[1] + dr * 0.5, p0[2] * 1.18 + s * wob];+ const p2 = [p0[0] * 1.02 - 0.03 * (1 - f * 0.5), p0[1] + dr, p0[2] * (1.28 - f * 0.3) - s * wob];+ locks.push([p0, p1, 0.02, 0.016], [p1, p2, 0.016, o.hair === 'curled' ? 0.012 : 0.007]);+ }+ }+ }+ if (o.hair === 'curls') { // rolled waves that frame the face: the smiling angel, the Child+ for (let i = 0; i < 11; i++) { const f = i / 10 - 0.5, a = f * 2.5, p0 = [0.104 * Math.cos(a) * 0.72 + 0.006, 0.094 - f * f * 0.11, 0.088 * Math.sin(a)], p1 = [p0[0] + 0.012, p0[1] - 0.03 - Math.abs(f) * 0.03, p0[2] * 1.16]; locks.push([p0, p1, 0.0165, 0.0115]); }+ for (let i = 0; i < 12; i++) { const a = 1.5 + i * 0.29, s = i % 2 ? 1 : -1, p0 = [0.09 * Math.cos(a) - 0.012, 0.0, s * 0.09 * Math.sin(a)], p1 = [p0[0] * 1.1, -0.062, p0[2] * 1.14]; locks.push([p0, p1, 0.019, 0.013]); }+ }+ const beardL = [];+ if (o.beard) {+ const nB = 7, L = o.beard === 2 ? 0.17 : 0.09;+ for (let i = 0; i < nB; i++) {+ const f = i / (nB - 1) - 0.5, z0 = f * 0.098, x0 = 0.078 - Math.abs(f) * 0.05, wob = (i % 2 ? 1 : -1) * 0.005;+ // separate locks with grooves between them; each one twists and tapers to a point+ beardL.push([[x0, -0.07 - Math.abs(f) * 0.012, z0], [x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], 0.0102, 0.0098]);+ beardL.push([[x0 + 0.006, -0.07 - L * 0.6 - Math.abs(f) * 0.015, z0 * 0.92 + wob], [x0 - 0.006 + (o.beard === 2 ? 0.008 : 0), -0.07 - L - Math.abs(f) * 0.012, z0 * 0.5], 0.0098, 0.0035]);+ }+ }+ // Each lock is skipped outright when the query point is beyond its bounding sphere+ const sphere = (L) => { const dx = L[0][0] - L[1][0], dy = L[0][1] - L[1][1], dz = L[0][2] - L[1][2], R = Math.sqrt(dx * dx + dy * dy + dz * dz) / 2 + Math.max(L[2], L[3]) + 0.03; return [(L[0][0] + L[1][0]) / 2, (L[0][1] + L[1][1]) / 2, (L[0][2] + L[1][2]) / 2, R * R]; };+ const lockB = locks.map(sphere), beardB = beardL.map(sphere);+ const nearS = (S, x, y, z) => { const a = x - S[0], b = y - S[1], c = z - S[2]; return a * a + b * b + c * c < S[3]; };+ return (x, y, z) => {+ const az = Math.abs(z);+ let d = sdEll(x + 0.012, y - 0.03, z, 0.098, 0.098, 0.08); // cranium+ d = smin(d, sdEll(x - 0.018, y + 0.022, z, 0.074, 0.094, 0.0655), 0.02); // face mass+ d = smin(d, sdEll(x - 0.03, y + 0.076, z, 0.056, 0.046, 0.05 * (o.gaunt ? 0.93 : 1)), 0.018); // jaw+ d = smin(d, sdEll(x - 0.0675, y + 0.1, z, 0.019, 0.0175, 0.02), 0.012); // chin+ d = smin(d, sdCone(x, y, az, 0.0745, 0.031, 0, 0.066, 0.033, 0.05, 0.0118, 0.0088), 0.012); // brow ridge+ d = smin(d, sdEll(x - 0.054, y + 0.018 - up, az - 0.044, 0.026, 0.017 + sm * 0.003, 0.02), 0.015 * cheek); // cheekbone+ if (o.gaunt) d = smax(d, -sdEll(x - 0.05, y + 0.058, az - 0.058, 0.03, 0.022, 0.012), 0.012);+ if (x > 0.04 && y > -0.03 && y < 0.05 && az < 0.082) {+ d = smax(d, -sdEll(x - 0.0875, y - 0.0075, az - 0.034, 0.0225, 0.0118, 0.0215), 0.008); // eye socket+ d = smin(d, len3(x - 0.0655, y - 0.0065, az - 0.034) - 0.0178, 0.0018); // eyeball+ d = smax(d, -(len3(x - 0.0862, y - 0.0065, az - 0.034) - 0.0052), 0.0016); // the drilled pupil+ // lids: a carved almond, the upper rim heavier, meeting at the corners+ for (let i = 0; i < 4; i++) { const a = LID_U[i], c = LID_U[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0026); }+ for (let i = 0; i < 4; i++) { const a = LID_L[i], c = LID_L[i + 1]; d = smin(d, sdCone(x, y, az, a[0], a[1], a[2], c[0], c[1], c[2], a[3], c[3]), 0.0022); }+ }+ if (x > 0.055 && y > -0.105 && y < 0.045 && az < 0.055) {+ // nose: bridge to tip, wings, nostrils+ d = smin(d, sdCone(x, y, z, 0.0835, 0.0225, 0, 0.105, -0.021, 0, 0.0058, 0.0092), 0.0085);+ d = smin(d, len3(x - 0.0925, y + 0.0295, az - 0.0108) - 0.0064, 0.005);+ d = smax(d, -(len3(x - 0.0975, y + 0.0355, az - 0.0062) - 0.0033), 0.002);+ // philtrum+ d = smax(d, -sdCone(x, y, z, 0.0975, -0.042, 0, 0.0965, -0.052, 0, 0.0028, 0.0034), 0.0035);+ // lips: upper in two wings, a fuller lower, a cut between; corners lift for a smile+ const my = -0.0615;+ d = smin(d, sdCone(x, y, az, 0.0925, my + 0.004, 0, 0.0815, my + 0.001 + up, 0.0215 + sm * 0.003, 0.0058, 0.0026), 0.005);+ d = smin(d, sdCone(x, y, az, 0.0915, my - 0.0085, 0, 0.0825, my - 0.003 + up, 0.0185 + sm * 0.003, 0.0072, 0.0028), 0.005);+ d = smax(d, -sdCone(x, y, az, 0.0965, my - 0.002, 0, 0.0825, my + up * 1.15, 0.0235 + sm * 0.004, 0.0017, 0.0021), 0.0022);+ d = smax(d, -sdEll(x - 0.0915, y + 0.0845, z, 0.012, 0.0055, 0.02), 0.006); // hollow under the lip+ }+ if (sm) d = smin(d, sdEll(x - 0.07, y + 0.044 - up * 2, az - 0.038, 0.02, 0.015, 0.016), 0.012); // the raised cheek of a smile+ // ear+ d = smin(d, sdEll(x + 0.012, y - 0.002, az - 0.08, 0.016, 0.03, 0.0085), 0.004);+ // neck, into the shoulders+ d = smin(d, sdCone(x, y, z, -0.01, -0.075, 0, -0.022, -0.215, 0, 0.041, 0.052), 0.018);+ // ---- hair ----+ if (o.hair && o.hair !== 'none') {+ const faceOpen = sdEll(x - 0.094, y + 0.012, z, 0.08, o.hair === 'curls' ? 0.106 : 0.118, 0.073);+ let h = sdEll(x + 0.014, y - 0.034, z, 0.104, 0.104, 0.09);+ if (o.hair === 'tonsure') h = smax(h, -sdEll(x + 0.012, y - 0.135, z, 0.075, 0.045, 0.062), 0.006);+ const an = Math.atan2(z, x + 0.014);+ if (o.hair !== 'curls') {+ const part = Math.exp(-(z * z) / 0.00007) * sstep(0.03, 0.11, y);+ h += 0.0028 * Math.sin(an * 22 + y * 34) + 0.007 * part;+ } else {+ h += 0.006 * Math.sin(an * 11 + y * 22) * Math.sin(y * 27 + an * 3);+ }+ for (let i = 0; i < locks.length; i++) { if (!nearS(lockB[i], x, y, z)) continue; const L = locks[i]; h = smin(h, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0055); }+ h = smax(h, -faceOpen, 0.0055);+ d = smin(d, h, 0.0025);+ }+ // ---- beard and moustache ----+ if (o.beard) {+ for (let i = 0; i < beardL.length; i++) { if (!nearS(beardB[i], x, y, z)) continue; const L = beardL[i]; d = smin(d, sdCone(x, y, z, L[0][0], L[0][1], L[0][2], L[1][0], L[1][1], L[1][2], L[2], L[3]), 0.0022); }+ d = smin(d, sdCone(x, y, az, 0.0955, -0.0475, 0.002, 0.081, -0.0625, 0.031, 0.0046, 0.003), 0.0025);+ d = smin(d, sdEll(x - 0.05, y + 0.058, az - 0.058, 0.022, 0.03, 0.012), 0.008); // sideburn into the jaw+ }+ // ---- veil: falls from the crown to the shoulders, cut open round the face ----+ if (o.veil) {+ // A soft hood: close to the skull, widening over the shoulders, its hem falling in loose folds+ const an = Math.atan2(z, x + 0.02);+ let w = smin(sdEll(x + 0.012, y - 0.032, z, 0.11, 0.108, 0.094), sdCone(x, y, z, -0.022, -0.05, 0, -0.038, -0.3, 0, 0.09, 0.158), 0.05);+ w -= 0.012 * (Math.sqrt(Math.abs(Math.sin(an * 4.5 + y * 2.2))) - 0.55) * sstep(-0.01, -0.16, y);+ w = smax(w, -0.325 + 0.022 * Math.sin(an * 5) - y, 0.008);+ const open = sdEll(x - 0.104, y + 0.016, z, 0.1, 0.116, 0.0715);+ d = smin(d, smax(w, -open, 0.004), 0.0015);+ }+ // ---- a queen's barbette: a linen band from the crown under the chin, framing a long grave face ----+ if (o.wimple) {+ const e = Math.sqrt(((y + 0.012) * (y + 0.012)) / (0.112 * 0.112) + (z * z) / (0.08 * 0.08)) - 1;+ d = smin(d, Math.max(Math.abs(e) * 0.09 - 0.0075, Math.abs(x - 0.018) - 0.036), 0.002);+ }+ // ---- mitre: two high peaks of stiff cloth, the bands left proud ----+ if (o.mitre) {+ // seen from the front a pointed arch, wide at the brow; seen from the side, two thin peaks+ const px = x + 0.012, py = y - 0.058, f = clamp(py / 0.27), w0 = 0.098 * (1 - Math.pow(f, 1.9)), dp = 0.102 * (1 - f * 0.86), lens = Math.abs(px) + az * 0.2;+ let m = Math.max(lens - dp, az - w0, -py);+ m = Math.max(m, py - 0.272);+ const band = Math.exp(-((py - 0.018) * (py - 0.018)) / 0.00016);+ m -= 0.0045 * band + 0.0038 * Math.exp(-(z * z) / 0.00012);+ d = smin(d, m, 0.004);+ }+ return d * 0.92;+ };+}++// ---- The hand: palm and jointed fingers that actually close on what they hold -------------+const HAND_POSES = {+ // [curl1, curl2, curl3] per finger (index..little), thumb [x,y,z of tip, spread]+ grip: { f: [[68, 88, 62], [72, 92, 64], [76, 92, 64], [80, 88, 62]], t: 'over' },+ support: { f: [[10, 18, 12], [12, 20, 14], [14, 22, 14], [18, 24, 16]], t: 'open' },+ edge: { f: [[46, 40, 26], [48, 42, 26], [50, 44, 28], [54, 44, 28]], t: 'open' },+ bless: { f: [[6, 8, 6], [8, 10, 8], [62, 90, 60], [68, 92, 60]], t: 'over' },+ rest: { f: [[28, 40, 26], [32, 42, 28], [36, 44, 28], [40, 46, 28]], t: 'open' },+};+function handField(pose, thumb = 1) {+ const P0 = HAND_POSES[pose], kn = [[0.078, 0.003, -0.027], [0.083, 0.004, -0.009], [0.08, 0.003, 0.009], [0.073, 0.001, 0.026]], segL = [[0.036, 0.024, 0.019], [0.04, 0.027, 0.02], [0.037, 0.025, 0.019], [0.029, 0.019, 0.016]], segs = [];+ for (let f = 0; f < 4; f++) {+ let ang = 0, p = kn[f].slice();+ for (let s = 0; s < 3; s++) {+ ang += rad(P0.f[f][s]);+ const q = [p[0] + Math.cos(ang) * segL[f][s], p[1] - Math.sin(ang) * segL[f][s], p[2]];+ segs.push([p, q, s === 0 ? 0.0098 : s === 1 ? 0.0088 : 0.0078, s === 2 ? 0.0066 : s === 0 ? 0.0088 : 0.0078]);+ p = q;+ }+ }+ const tb = [0.022, -0.006, thumb * 0.03], tm = P0.t === 'over' ? [0.052, -0.03, thumb * 0.033] : [0.05, -0.012, thumb * 0.054], tt = P0.t === 'over' ? [0.078, -0.044, thumb * 0.012] : [0.082, -0.016, thumb * 0.056];+ return (x, y, z) => {+ let d = Math.max(Math.abs(x - 0.04) - 0.043, Math.abs(y) - 0.0125, Math.abs(z) - 0.036) - 0.006; // palm, gently rounded+ d = smin(d, sdCone(x, y, z, -0.02, 0, 0, 0.02, 0, 0, 0.026, 0.033), 0.012); // wrist heel+ for (let i = 0; i < segs.length; i++) { const S = segs[i]; d = smin(d, sdCone(x, y, z, S[0][0], S[0][1], S[0][2], S[1][0], S[1][1], S[1][2], S[2], S[3]), 0.0022); }+ d = smin(d, sdCone(x, y, z, tb[0], tb[1], tb[2], tm[0], tm[1], tm[2], 0.0145, 0.0098), 0.006);+ d = smin(d, sdCone(x, y, z, tm[0], tm[1], tm[2], tt[0], tt[1], tt[2], 0.0098, 0.0078), 0.002);+ return d;+ };+}+// Heads are shared meshes placed on their own node: the same face can turn and tilt per figure at no byte cost.+const HEAD_MESH = {};+const HEADS = {+ bearded: { hair: 'long', beard: 1, seed: 2 },+ mitred: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 },+ royal: { hair: 'curled', beard: 2, seed: 10 },+ veiled: { veil: 1, smile: 0.3, seed: 8 },+ queen: { veil: 1, gaunt: 1, wimple: 1, seed: 5 },+ angelic: { hair: 'curls', smile: 1, seed: 13 },+};+async function headBatch(key) {+ if (HEAD_MESH[key]) return HEAD_MESH[key];+ const hO = HEADS[key], b = new Batch(TILE.fine), hF = headField(hO);+ const hB = [-0.19, hO.veil ? -0.36 : -0.26, -0.2, 0.17, hO.mitre ? 0.36 : 0.16, 0.2];+ // Hair and skull at a working grid, then the face alone on the finest grid with most of the triangles+ const FB = [0.024, -0.13, -0.08, 0.128, 0.088, 0.08];+ const inFace = (x, y, z) => x > FB[0] + 0.003 && y > FB[1] + 0.003 && y < FB[4] - 0.003 && z > FB[2] + 0.003 && z < FB[5] - 0.003;+ await carveInto(b, hF, hB, 0.0048, hO.veil ? 1300 : hO.beard ? 1900 : 1700, null, hF, { smooth: 1.4, ao: 0.34, aoMin: 0.3, cull: inFace });+ await carveInto(b, hF, FB, 0.0024, hO.beard ? 2500 : 2700, null, hF, { smooth: 0.9, ao: 0.3, aoMin: 0.34 });+ b.isFigure = true;+ HEAD_MESH[key] = b;+ return b;+}+const HAND_MESH = {};+async function handBatch(pose, thumb) { // hands are cached: a grip is a grip, whoever's it is+ const key = pose + thumb;+ if (!HAND_MESH[key]) { const b = new Batch(TILE.fine), F = handField(pose, thumb); await carveInto(b, F, [-0.05, -0.1, -0.1, 0.16, 0.05, 0.1], 0.0034, 520); HAND_MESH[key] = b; }+ return HAND_MESH[key];+}+function copyBatch(dst, src, m, shadeFn) { // transform a finished batch into another+ const base = dst.count;+ dst.at(m, () => {+ for (let i = 0; i < src.count; i++) { const i3 = i * 3, i2 = i * 2; dst.v(src.P[i3], src.P[i3 + 1], src.P[i3 + 2], src.N[i3], src.N[i3 + 1], src.N[i3 + 2], src.UV[i2] * src.tile, src.UV[i2 + 1] * src.tile, (src.C[i3] / 255) * (shadeFn ? shadeFn(src.P[i3], src.P[i3 + 1], src.P[i3 + 2]) : 1)); }+ for (let t = 0; t < src.I.length; t += 3) dst.tri(base + src.I[t], base + src.I[t + 1], base + src.I[t + 2]);+ });+}+const frameOf = (p, X, Y) => { // rigid frame: origin p, local X and Y given, Z = X x Y+ const lx = len3(X[0], X[1], X[2]), xn = [X[0] / lx, X[1] / lx, X[2] / lx], d = Y[0] * xn[0] + Y[1] * xn[1] + Y[2] * xn[2];+ let yy = [Y[0] - d * xn[0], Y[1] - d * xn[1], Y[2] - d * xn[2]]; const ly = len3(yy[0], yy[1], yy[2]); yy = yy.map((c) => c / ly);+ const z = [xn[1] * yy[2] - xn[2] * yy[1], xn[2] * yy[0] - xn[0] * yy[2], xn[0] * yy[1] - xn[1] * yy[0]];+ return [xn[0], yy[0], z[0], p[0], xn[1], yy[1], z[1], p[1], xn[2], yy[2], z[2], p[2]];+};+const invRigid = (m) => (x, y, z) => { const a = x - m[3], b = y - m[7], c = z - m[11]; return [m[0] * a + m[4] * b + m[8] * c, m[1] * a + m[5] * b + m[9] * c, m[2] * a + m[6] * b + m[10] * c]; };++// ---- The people. Bodies are long columns of cloth; each differs in dress, stance, head, gesture ----+const FIGURES = {+ apostle: { seed: 2, sway: 0.018, dress: 'mantle', headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 2 }, hold: 'key' },+ bishop: { seed: 4, sway: 0.008, dress: 'chasuble', headKey: 'mitred', head: { hair: 'curled', mitre: 1, gaunt: 1, seed: 4 }, hold: 'crozier' },+ king: { seed: 10, sway: 0.022, dress: 'royal', headKey: 'royal', head: { hair: 'curled', beard: 2, crown: 1, seed: 10 }, hold: 'sceptre' },+ queen: { seed: 5, sway: 0.03, dress: 'royal', slim: 0.93, headKey: 'queen', head: { veil: 1, gaunt: 1, wimple: 1, crown: 1, seed: 5, turn: 8 }, hold: 'mantle' },+ angel: { seed: 13, sway: 0.05, dress: 'angel', slim: 0.94, wings: 1, headKey: 'angelic', head: { hair: 'curls', smile: 1, seed: 13, tilt: 11, turn: 15 }, hold: 'scroll' },+ virgin: { seed: 8, sway: 0.062, dress: 'mantle', slim: 0.95, headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, tilt: 9, turn: -16 }, hold: 'child' },+};+const FIG_ALIAS = { peter: 'apostle', paul: 'apostle', prophet: 'apostle', deacon: 'apostle', martyr: 'queen', kingA: 'king', kingB: 'king', kingC: 'king', christ: 'seated' };++function bodyField(o) {+ const yN = 1.7, rng = mkRng(o.seed * 991 + 7), ph = []; for (let i = 0; i < 8; i++) ph.push(rng() * TAU);+ const sw = o.sway || 0, sl = o.slim || 1, L = 256;+ const keys = [[0, 0.178, 0.205, 0.218], [0.05, 0.165, 0.192, 0.204], [0.3, 0.138, 0.165, 0.172], [0.52, 0.124, 0.15, 0.158], [0.66, 0.112, 0.128, 0.146], [0.8, 0.116, 0.12, 0.15], [0.885, 0.108, 0.112, 0.15], [0.94, 0.09, 0.094, 0.13], [0.975, 0.066, 0.07, 0.09], [1, 0.044, 0.046, 0.05]];+ const Af = new Float32Array(L), Ab = new Float32Array(L), Bs = new Float32Array(L), Cx = new Float32Array(L), Cz = new Float32Array(L), Amp = new Float32Array(L);+ for (let i = 0; i < L; i++) {+ const t = i / (L - 1), k = keyf(keys, t);+ Af[i] = k[0]; Ab[i] = k[1]; Bs[i] = k[2] * sl;+ Cx[i] = 0.014 * Math.sin(t * PI);+ Cz[i] = sw * (Math.sin(t * PI * 0.92) * 1.15 - 0.62 * Math.sin(t * PI * 2));+ // pleats deepest over the shins, gathered at the belt, slight in the bodice+ Amp[i] = t < 0.645 ? lerp(0.05, 0.03, sstep(0, 0.6, t)) : lerp(0.014, 0.004, sstep(0.66, 0.9, t));+ }+ const n1 = o.dress === 'chasuble' ? 16 : 14, n2 = 25, yB = 0.655 * yN;+ const dress = o.dress, zc = Cz[Math.round(0.86 * (L - 1))], cxS = Cx[Math.round(0.86 * (L - 1))];+ const Rs = 0.142 * sl;+ const arms = armPose(o, Rs, zc, cxS);+ // Feet: pointed medieval shoes emerge from the hem+ const foot = (x, y, z) => { const az = Math.abs(z - Cz[0]); let d = sdCone(x, y, az, 0.02, 0.042, 0.082, 0.2, 0.024, 0.098, 0.05, 0.024); d = smin(d, sdCone(x, y, az, 0.0, 0.05, 0.082, 0.02, 0.2, 0.07, 0.044, 0.04), 0.02); d = smax(d, -sdEll(x - 0.11, y + 0.01, az - 0.09, 0.08, 0.02, 0.1), 0.004); return Math.max(d, -y); };+ return {+ arms,+ F: (x, y, z) => {+ const t = clamp(y / yN), i = (t * (L - 1)) | 0, px = x - Cx[i], pz = z - Cz[i], rho = Math.sqrt(px * px + pz * pz) + 1e-6, c = px / rho, s = pz / rho, a = c > 0 ? Af[i] : Ab[i], b = Bs[i];+ const R = 1 / Math.sqrt((c * c) / (a * a) + (s * s) / (b * b)), th = Math.atan2(pz, px);+ // ---- the tunic: tube pleats with valleys you could put a finger in ----+ const u1 = th * n1 * 0.5 + ph[0] + y * 0.5 * Math.sign(sw || 1), u2 = th * n2 * 0.5 + ph[1] - y * 0.9;+ const p1 = Math.sqrt(Math.abs(Math.sin(u1))), p2 = Math.sqrt(Math.abs(Math.sin(u2)));+ let r = R + Amp[i] * (p1 * 0.76 + p2 * 0.24 - 0.64);+ r += sstep(0.14, 0, y) * (0.018 + 0.016 * Math.sin(th * 7 + ph[4])); // the skirt spreads and breaks at the foot+ r += 0.008 * Math.exp(-((y - yB) * (y - yB)) / 0.00018); // belt+ r -= 0.012 * sstep(0.03, 0, Math.abs(y - yB) - 0.012) * 0.2;+ r += 0.006 * Math.exp(-((y - yN + 0.075) * (y - yN + 0.075)) / 0.00012); // collar+ let d = rho - r;+ const hem = 0.055 + 0.038 * p1 * (c > -0.2 ? 1 : 0.4);+ d = smax(d, hem - y, 0.006);+ // ---- the over-garment ----+ if (dress === 'mantle') {+ const up = 0.78 * yN + 0.2 * yN * Math.max(0, Math.sin(-th * Math.sign(sw || 1) + 0.5)) + 0.25 * sstep(1.2, 2.6, Math.abs(th)), low = 0.33 * yN - 0.18 * yN * sstep(0.5, 2.8, Math.abs(th)) + 0.02 * Math.sin(th * 11 + ph[5]) + 0.012 * Math.sin(th * 23);+ const reg = Math.max(low - y, y - up);+ // Folds pulled on the diagonal from the held hip to the far shoulder: two scales, never in step+ const th0 = 0.25 * Math.sign(sw || 1), q = y + 0.15 * th * Math.sign(sw || 1) + 0.055 * (th - th0) * (th - th0);+ const swag = 0.62 * Math.sqrt(Math.abs(Math.sin(q * 12.5 + ph[6]))) + 0.38 * Math.sqrt(Math.abs(Math.sin(q * 27 + ph[7]))), back = Math.sqrt(Math.abs(Math.sin(th * 5 + ph[3])));+ const fr = sstep(1.9, 1.1, Math.abs(th));+ const rm = R + 0.03 + 0.04 * lerp(back - 0.6, swag - 0.58, fr) * sstep(up + 0.01, up - 0.08, y);+ d = smin(d, smax(rho - rm, reg, 0.007), 0.002);+ } else if (dress === 'chasuble') {+ // alb to the feet, dalmatic to the shin, the bell of the chasuble hanging from the arms in U folds+ const dal = Math.max(0.3 * yN + 0.012 * Math.sin(th * 9) - y, y - 0.86 * yN);+ d = smin(d, smax(rho - (R + 0.016 + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 6 + ph[2]))) - 0.5)), dal, 0.005), 0.0015);+ const lowC = (0.42 + 0.26 * sstep(0.2, 1.5, Math.abs(th)) - 0.2 * sstep(1.7, 3.0, Math.abs(th))) * yN + 0.012 * Math.sin(th * 13);+ const swag = 0.65 * Math.sqrt(Math.abs(Math.sin((y + 0.2 * th * th) * 27 + ph[6]))) + 0.35 * Math.sqrt(Math.abs(Math.sin((y + 0.13 * th * th) * 44 + ph[7])));+ const rc = R + 0.03 * sstep(yN, 0.9 * yN, y) + 0.022 * (swag - 0.58) * sstep(0.84 * yN, 0.74 * yN, y) * (1 - 0.6 * sstep(0, 0.2, 0.2 - Math.abs(th)));+ d = smin(d, smax(rho - rc, lowC - y, 0.007), 0.003);+ // the orphrey: a Y-band across the breast and down the front, left proud+ const py = y - 0.79 * yN;+ if (Math.abs(th) < 1.2 && y > lowC - 0.01 && y < 0.94 * yN) d = smin(d, Math.max(rho - rc - 0.011, py > 0 ? Math.abs(Math.abs(th) - py * 4.2) - 0.085 : Math.abs(th) - 0.09), 0.003);+ } else if (dress === 'royal') {+ // a heavy mantle open down the front, thrown back over the shoulders, deep folds to the floor+ const open = lerp(0.72, 0.28, sstep(0.05, 0.86, t)) + 0.05 * Math.sin(y * 19 + ph[2]);+ const reg = Math.max(open - Math.abs(th - 0.05), 0.05 - y);+ const hv = Math.sqrt(Math.abs(Math.sin(th * 4 + ph[3] + y * 0.3)));+ const collar = 0.022 * Math.exp(-((y - 0.915 * yN) * (y - 0.915 * yN)) / 0.0012); // a soft roll at the neck+ const rm = R + 0.03 * sstep(yN, 0.9 * yN, y) + collar + sstep(0.84 * yN, 0.5 * yN, y) * 0.056 * (hv - 0.62);+ d = smin(d, smax(rho - rm, reg, 0.006), 0.004);+ } else if (dress === 'angel') {+ // a girdled tunic blown a little to one side: fine folds and a bloused top+ const blouse = 0.018 * Math.exp(-((y - yB - 0.06) * (y - yB - 0.06)) / 0.003);+ d -= blouse;+ const low = 0.74 * yN + 0.02 * Math.sin(th * 7) - y;+ d = smin(d, smax(rho - (R + 0.016 * sstep(yN, 0.9 * yN, y) + 0.012 * (Math.sqrt(Math.abs(Math.sin(th * 7 + ph[2]))) - 0.5) * sstep(0.9 * yN, 0.8 * yN, y)), low, 0.005), 0.003);+ }+ // ---- shoulders and sleeves ----+ const yS = yN - 0.155;+ for (let k = 0; k < 2; k++) { const sg = k ? 1 : -1; d = smin(d, sdEll(x - cxS, y - yS, z - zc - sg * Rs * 0.98, 0.07, 0.078, 0.058), 0.05); }+ for (let k = 0; k < arms.length; k++) {+ const A = arms[k];+ let ad = sdCone(x, y, z, A.S[0], A.S[1], A.S[2], A.E[0], A.E[1], A.E[2], 0.056, 0.05);+ // forearm in a sleeve that widens to the cuff, with a roll of cloth at the wrist+ const fx = A.W[0] - A.E[0], fy = A.W[1] - A.E[1], fz = A.W[2] - A.E[2], fl = fx * fx + fy * fy + fz * fz, ft = clamp(((x - A.E[0]) * fx + (y - A.E[1]) * fy + (z - A.E[2]) * fz) / fl);+ const qx = x - A.E[0] - fx * ft, qy = y - A.E[1] - fy * ft, qz = z - A.E[2] - fz * ft, ql = len3(qx, qy, qz);+ const rr = lerp(0.05, 0.062, ft) + 0.006 * Math.sin(ft * 15 + k) * (1 - ft) + 0.008 * Math.exp(-((ft - 0.97) * (ft - 0.97)) / 0.002);+ ad = smin(ad, ql - rr, 0.018);+ // the cloth that falls from under the forearm: a hanging curtain of pleats+ // longest under the wrist, thinning as it falls, its lower edge never straight+ if (A.drape) {+ const h = A.E[1] + fy * ft - y;+ if (h > -0.04 && h < A.drape * 1.1) {+ const fall = clamp(h / A.drape), crv = lerp(0.044, 0.016, fall) + 0.014 * (Math.sqrt(Math.abs(Math.sin(ft * 12 + h * 2.5 + k))) - 0.5);+ const curtain = len3(qx * 0.72, 0, qz) - crv;+ const edge = h - A.drape * (0.28 + 0.72 * ft) - 0.022 * Math.sin(ft * 11 + k * 2);+ ad = smin(ad, smax(curtain, edge, 0.012), 0.018);+ }+ }+ d = smin(d, ad, 0.014);+ }+ // ---- feet ----+ d = smin(d, foot(x, y, z), 0.006);+ return Math.max(d, y - yN - 0.03);+ },+ };+}+// Where the arms go, and what the hands do when they get there+function armPose(o, Rs, zc, cx) {+ const y = 1.55, shR = [cx, y, zc + Rs], shL = [cx, y, zc - Rs], arm = (S, E, W, drape) => ({ S, E, W, drape });+ switch (o.hold) {+ case 'key': return [arm(shR, [0.03, 1.2, zc + Rs + 0.05], [0.17, 1.32, zc + 0.12], 0.32), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.17, 1.14, zc - 0.07], 0.34)];+ case 'crozier': return [arm(shR, [0.04, 1.2, zc + Rs + 0.06], [0.2, 1.3, zc + 0.14], 0.3), arm(shL, [0.05, 1.22, zc - Rs - 0.03], [0.2, 1.42, zc - 0.05], 0.3)];+ case 'sceptre': return [arm(shR, [0.04, 1.18, zc + Rs + 0.06], [0.2, 1.28, zc + 0.15], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.03], [0.17, 1.18, zc - 0.05], 0.3)];+ case 'mantle': return [arm(shR, [0.04, 1.14, zc + Rs + 0.05], [0.18, 1.2, zc + 0.04], 0.34), arm(shL, [0.03, 1.2, zc - Rs - 0.05], [0.15, 1.4, zc - 0.1], 0.26)];+ case 'scroll': return [arm(shR, [0.05, 1.2, zc + Rs + 0.04], [0.2, 1.34, zc + 0.1], 0.28), arm(shL, [0.05, 1.2, zc - Rs - 0.04], [0.2, 1.22, zc - 0.08], 0.28)];+ case 'child': return [arm(shR, [0.05, 1.14, zc + Rs + 0.04], [0.2, 1.24, zc + 0.03], 0.32), arm(shL, [0.05, 1.14, zc - Rs - 0.02], [0.2, 1.18, zc - 0.13], 0.3)];+ case 'bless': return [arm(shR, [0.07, 1.22, zc + Rs + 0.09], [0.19, 1.5, zc + 0.15], 0.3), arm(shL, [0.03, 1.2, zc - Rs - 0.04], [0.17, 1.2, zc - 0.07], 0.34)];+ default: return [arm(shR, [0.02, 1.12, zc + Rs + 0.04], [0.15, 1.05, zc + 0.08], 0.3), arm(shL, [0.02, 1.12, zc - Rs - 0.04], [0.15, 1.05, zc - 0.08], 0.3)];+ }+}+// ---- Things held: real objects, modelled at the scale of the hands that grip them ---------+function addBook(b, m, o = {}) {+ b.at(m, () => {+ const w = o.w || 0.082, h = o.h || 0.112, th = o.th || 0.027;+ b.box(-h, h, -th, -th + 0.009, -w, w, { s: 0.85 }); b.box(-h, h, th - 0.009, th, -w, w, { s: 1 }); // boards+ b.box(-h + 0.006, h - 0.006, -th + 0.009, th - 0.009, -w + 0.006, w - 0.004, { s: 0.62 }); // pages+ b.box(-h, h, -th, th, -w - 0.009, -w, { s: 0.8 }); // spine+ for (const i of [-1, 1]) for (const k of [-1, 1]) b.at(M.T(i * (h - 0.026), th, k * (w - 0.026)), () => b.lathe([[0.016, 0], [0.012, 0.007], [0.0, 0.009]], 6));+ b.at(M.T(0, th, 0), () => b.lathe([[0.03, 0], [0.024, 0.006], [0.0, 0.009]], 8));+ });+}+function addAttrs(fig, o, arms) {+ const { stone: b, gold: g } = fig, R = arms[0], Lf = arms[1];+ switch (o.hold) {+ case 'key': {+ // The great key of Peter held upright in the fist, the book against the left forearm+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01];+ b.tube([[hp[0], hp[1] - 0.15, hp[2]], [hp[0], hp[1] + 0.2, hp[2]]], 0.0125, 8, { capStart: true });+ b.at(M.T(hp[0], hp[1] + 0.245, hp[2]), () => b.at(M.Rx(90), () => { b.lathe([[0.04, -0.01], [0.05, 0], [0.04, 0.01], [0.026, 0.01], [0.02, 0], [0.026, -0.01], [0.04, -0.01]], 14); }));+ b.box(hp[0] - 0.01, hp[0] + 0.01, hp[1] - 0.15, hp[1] - 0.075, hp[2], hp[2] + 0.07, { s: 0.9 });+ b.box(hp[0] - 0.012, hp[0] + 0.012, hp[1] - 0.112, hp[1] - 0.1, hp[2] + 0.03, hp[2] + 0.048, { s: 0.3 });+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2] + 0.0), M.Rz(-64), M.Ry(8)));+ break;+ }+ case 'crozier': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 2.02;+ b.tube([[hp[0], 0.04, hp[2]], [hp[0], top, hp[2]]], 0.0145, 8, { capStart: true });+ for (const yK of [hp[1] + 0.22, top - 0.05]) b.at(M.T(hp[0], yK, hp[2]), () => b.lathe([[0.016, -0.03], [0.034, 0], [0.016, 0.03]], 8));+ const vol = []; for (let i = 0; i <= 20; i++) { const t = i / 20, a = -PI * 0.5 + t * PI * 2.5, r = lerp(0.115, 0.036, t); vol.push([hp[0] + r * Math.cos(a) * 1.0 + 0.0, top + 0.12 + r * Math.sin(a), hp[2]]); }+ g.tube(vol, (t) => lerp(0.02, 0.012, t), 7, { capStart: true });+ for (let i = 2; i < 16; i += 3) { const p = vol[i]; addCrocket(g, M.of(M.T(p[0], p[1], p[2]), M.Rz((i / 20) * 400 - 90)), 0.06, 0); }+ break;+ }+ case 'sceptre': {+ const hp = [R.W[0] + 0.085, R.W[1] - 0.005, R.W[2] - 0.01], top = 1.95;+ b.tube([[hp[0], hp[1] - 0.2, hp[2]], [hp[0], top, hp[2]]], 0.0125, 8, { capStart: true });+ g.at(M.T(hp[0], top, hp[2]), () => {+ g.lathe([[0.0125, 0], [0.03, 0.022], [0.038, 0.05], [0.026, 0.078], [0.01, 0.086]], 10);+ for (let i = 0; i < 3; i++) { const a = i * 120 + 90; g.at(M.Ry(a), () => g.tube(spline([[0, 0.07, 0], [0.038, 0.1, 0], [0.042, 0.15, 0], [0.018, 0.17, 0]], 2), (t) => [lerp(0.016, 0.003, t), lerp(0.024, 0.004, t)], 6)); }+ });+ break;+ }+ case 'scroll': {+ const p = [Lf.W[0] + 0.07, Lf.W[1], Lf.W[2]];+ const path = []; for (let i = 0; i <= 14; i++) { const t = i / 14; path.push([R.W[0] + 0.09 + 0.02 * Math.sin(t * 5), R.W[1] - t * 0.7 + 0.03, lerp(R.W[2] - 0.03, Lf.W[2] + 0.05, sstep(0, 0.5, t)) + 0.02 * Math.sin(t * 7)]); }+ const sheet = (off, flip) => b.grid((u, v) => { const k = u * 14, i = Math.min(13, Math.floor(k)), f = k - i, q = path[i], w = path[i + 1]; return [lerp(q[0], w[0], f) + off, lerp(q[1], w[1], f), lerp(q[2], w[2], f) + (v - 0.5) * 0.15, 0.96]; }, 14, 1, { flip });+ sheet(0, false); sheet(-0.004, true);+ b.at(M.of(M.T(path[14][0], path[14][1] - 0.015, path[14][2]), M.Rx(90)), () => b.lathe([[0.022, -0.085], [0.024, -0.08], [0.024, 0.08], [0.022, 0.085]], 8));+ break;+ }+ case 'bless': {+ addBook(b, M.of(M.T(Lf.W[0] + 0.1, Lf.W[1] + 0.03, Lf.W[2]), M.Rz(-64), M.Ry(4)));+ break;+ }+ default: break;+ }+}+const CROWN_FLEURS = 8;+function addCrown(g, m, R0, tall = 1) {+ g.at(m, () => {+ g.lathe([[R0, 0], [R0 + 0.009, 0.004], [R0 + 0.009, 0.028], [R0 + 0.015, 0.038], [R0 + 0.006, 0.042], [R0 - 0.004, 0.034], [R0 - 0.004, 0]], 26);+ // jewels set in the band+ for (let i = 0; i < 12; i++) { const a = i * TAU / 12; blob(g, [(R0 + 0.013) * Math.cos(a), 0.02, (R0 + 0.013) * Math.sin(a)], [0.0072, 0.0072, 0.0072], 5, 3); }+ // Fleurs-de-lis cut from sheet: flat plates set on the rim, the way a goldsmith makes them+ const h = 0.05 * tall, fl = [[-0.03, 0], [0.03, 0], [0.024, h * 0.2], [0.04, h * 0.42], [0.031, h * 0.62], [0.017, h * 0.42], [0.009, h * 0.5], [0.012, h * 0.76], [0, h * 1.1], [-0.012, h * 0.76], [-0.009, h * 0.5], [-0.017, h * 0.42], [-0.031, h * 0.62], [-0.04, h * 0.42], [-0.024, h * 0.2]];+ for (let i = 0; i < CROWN_FLEURS; i++) {+ const a = (i + 0.5) * 360 / CROWN_FLEURS;+ g.at(M.of(M.Ry(90 - a), M.T(0, 0.03, R0 + 0.004)), () => g.slab(fl, [], -0.004, 0.004, { so: 1, sr: 1 }));+ }+ });+}++const figF = {}; // fields kept while a figure is carved+const yNeck = 1.7;+async function makeFigure(kind) {+ const o = FIGURES[kind], stone = new Batch(TILE.fine), gold = new Batch(TILE.gold);+ const body = bodyField(o), arms = body.arms, hO = Object.assign({ seed: o.seed }, o.head);+ // Head frame: gently inclined, sitting on the neck above the centre of the shoulders+ const zc = arms[0].S[2] - (0.142 * (o.slim || 1)), cx = arms[0].S[0];+ const headM = M.of(M.T(cx + 0.024, yNeck + 0.116, zc + (o.sway || 0) * 0.3), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(-5));+ const hF = headField(hO), toHead = invRigid(headM);+ const headW = (x, y, z) => { const q = toHead(x, y, z); return hF(q[0], q[1], q[2]); };+ // Hands: frames derived from the sleeve so that the wrist always leaves the cuff+ const hands = [];+ const handFor = (A, side, pose) => {+ const X0 = [A.W[0] - A.E[0], A.W[1] - A.E[1], A.W[2] - A.E[2]];+ let Y0;+ if (pose === 'support') Y0 = [0.2, -1, 0]; // palm up under a book+ else if (pose === 'bless') Y0 = [0, 0.1, side]; // palm turned forward, two fingers raised+ else Y0 = [0, 0, side]; // back of the hand outward+ const X = pose === 'bless' ? [0.25, 1, 0.05] : X0;+ return { m: frameOf(A.W, X, Y0), pose, thumb: pose === 'grip' || pose === 'edge' || pose === 'rest' ? -side : side };+ };+ const handsCfg = { key: ['grip', 'support'], crozier: ['grip', 'rest'], sceptre: ['grip', 'rest'], mantle: ['rest', 'rest'], scroll: ['edge', 'rest'], child: ['rest', 'support'], bless: ['bless', 'support'] }[o.hold] || ['rest', 'rest'];+ hands.push(handFor(arms[0], 1, handsCfg[0]), handFor(arms[1], -1, handsCfg[1]));+ const handFs = hands.map((h) => { const f = handField(h.pose, h.thumb), inv = invRigid(h.m); return (x, y, z) => { const q = inv(x, y, z); return f(q[0], q[1], q[2]); }; });+ // The Child: a real little person on the Virgin's arm, turning to her+ let childF = null, childM = null;+ if (o.hold === 'child') {+ const A = arms[1];+ // sits in the crook of her left arm, turned towards her, one small hand raised to her veil+ childM = M.of(M.T(A.W[0] - 0.015, A.W[1] + 0.082, A.W[2] - 0.0), M.Ry(-34), M.Rz(4));+ const cH = headField({ hair: 'curls', seed: 21, smile: 0.6 }), inv = invRigid(childM), k = 0.47;+ childF = (x, y, z) => {+ const q = inv(x, y, z), X = q[0], Y = q[1], Z = q[2];+ // A seated child in a long shift: torso, thighs across her arm, shins and small feet hanging+ let d = sdCone(X, Y, Z, 0, 0, 0, 0, 0.165, 0, 0.066, 0.05);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.0, s * 0.034, 0.125, -0.012, s * 0.04, 0.038, 0.03), 0.018); // thigh+ d = smin(d, sdCone(X, Y, Z, 0.125, -0.012, s * 0.04, 0.142, -0.135, s * 0.038, 0.03, 0.022), 0.012); // shin+ d = smin(d, sdCone(X, Y, Z, 0.142, -0.15, s * 0.038, 0.19, -0.17, s * 0.04, 0.02, 0.013), 0.006); // foot+ }+ d -= 0.005 * Math.sqrt(Math.abs(Math.sin(Math.atan2(Z, X) * 6 + Y * 4))) * sstep(0.12, 0.0, Y) * sstep(-0.1, 0.0, Y);+ d = smin(d, sdCone(X, Y, Z, 0.005, 0.135, -0.052, 0.06, 0.07, -0.05, 0.02, 0.016), 0.012); // one arm holds an orb on his knee+ d = smin(d, len3(X - 0.1, Y - 0.052, Z + 0.042) - 0.03, 0.003);+ d = smin(d, len3(X - 0.074, Y - 0.056, Z + 0.054) - 0.015, 0.004);+ d = smin(d, sdCone(X, Y, Z, 0.0, 0.145, 0.05, -0.005, 0.2, 0.094, 0.02, 0.015), 0.012); // the other reaches back to touch her veil+ d = smin(d, len3(X + 0.007, Y - 0.214, Z - 0.104) - 0.0155, 0.005);+ d = smin(d, cH((X - 0.008) / k, (Y - 0.262) / k, Z / k) * k, 0.012);+ return d;+ };+ }+ const allF = (x, y, z) => {+ let d = body.F(x, y, z);+ d = Math.min(d, headW(x, y, z));+ for (let i = 0; i < handFs.length; i++) d = Math.min(d, handFs[i](x, y, z));+ if (childF) d = Math.min(d, childF(x, y, z));+ return d;+ };+ // ---- carve ----+ await carveInto(stone, body.F, [-0.42, -0.005, -0.52, 0.42, yNeck + 0.04, 0.52], 0.0105, o.wings ? 3800 : 4400, null, allF);+ const head = await headBatch(o.headKey);+ for (let i = 0; i < hands.length; i++) {+ const hb = await handBatch(hands[i].pose, hands[i].thumb);+ const base = stone.count;+ copyBatch(stone, hb, hands[i].m);+ }+ if (childF) await carveInto(stone, childF, [arms[1].W[0] - 0.24, arms[1].W[1] - 0.2, arms[1].W[2] - 0.26, arms[1].W[0] + 0.3, arms[1].W[1] + 0.56, arms[1].W[2] + 0.26], 0.0042, 3200, null, allF, { smooth: 1, ao: 0.34, aoMin: 0.3 });+ addAttrs({ stone, gold }, o, arms);+ // Crown, nimbus and wings+ if (o.head.crown) addCrown(gold, M.of(headM, M.T(-0.012, o.head.veil ? 0.088 : 0.096, 0)), o.head.veil ? 0.088 : 0.084);+ if (o.nimbus) gold.at(M.of(headM, M.T(-0.16, 0.03, 0), M.Ry(90)), () => { gold.slab(ringPts(0, 0, 0.235, 28), [], -0.014, 0.014, { sb: 0.8 }); });+ if (o.wings) {+ // Feathers in overlapping tiers, carved on a blade that rises from the shoulders and falls to the calf+ const W = (side) => (x, y, z) => {+ // The wall side (local -z in every jamb) folds along the stepped stone from the shoulder; the free side stands off behind the back+ const zz = z * side - 0.12 - zc * side, X = side < 0 ? x + 0.01 + 0.6 * (zz - 0.065) : x + 0.085 + zz * 0.18 + 0.05 * clamp((1.86 - y) / 1.32), u = clamp((1.86 - y) / 1.32), wid = 0.27 * Math.pow(Math.sin(PI * Math.pow(u, 0.6)), 0.65) * (1 - 0.3 * u);+ const v = zz - 0.03 - u * 0.1 + 0.015;+ let d = Math.abs(X) - 0.026 + 0.012 * u;+ d = Math.max(d, Math.abs(v - wid * 0.5) - wid * 0.5, y - 1.87, 0.53 - y);+ // Tiers of feathers: short and scaled over the shoulder, long pointed quills towards the tip+ const row = Math.abs(Math.sin(u * 14 + v * 6)), bars = Math.abs(Math.sin(v * 26 + u * 7));+ d -= 0.015 * Math.sqrt(row) * sstep(0.45, 0.2, u) + 0.016 * Math.sqrt(bars) * sstep(0.2, 0.42, u);+ // The wing's arm: it grows out of the shoulder blade, so the blade never hangs in the air behind the body+ return smin(d * 0.8, sdCone(x, y, zz, -0.035, 1.6, -0.06, side < 0 ? -0.045 : -0.12, 1.46, 0.1, 0.05, 0.028), 0.04);+ };+ for (const side of [-1, 1]) await carveInto(stone, W(side), [-0.3, 0.5, side > 0 ? zc + 0.05 : zc - 0.72, 0.1, 1.9, side > 0 ? zc + 0.72 : zc - 0.05], 0.0105, 900, null, allF);+ }+ stone.isFigure = true;+ return { stone, gold, head, headM };+}+// ---- Seated and kneeling figures for the tympana: same people, other postures -------------+function poseField(kind) {+ if (kind === 'seated') return (x, y, z) => {+ // A figure enthroned: knees forward, the robe falling from them in a curtain to the feet+ const th = Math.atan2(z, x);+ let d = sdCone(x, y, z, -0.04, 0.72, 0, -0.03, 1.38, 0, 0.17, 0.13);+ d = smin(d, sdEll(x, y - 1.46, z, 0.09, 0.12, 0.17), 0.03);+ for (const s of [-1, 1]) {+ d = smin(d, sdCone(x, y, z, -0.03, 0.7, s * 0.1, 0.27, 0.68, s * 0.11, 0.1, 0.09), 0.03);+ const cur = sdCone(x, y, z, 0.27, 0.64, s * 0.11, 0.31, 0.04, s * 0.1, 0.09, 0.12);+ d = smin(d, cur - 0.012 * Math.sqrt(Math.abs(Math.sin(z * 34 + x * 4))), 0.03);+ }+ d = smin(d, sdCone(x, y, z, 0.16, 0.45, 0, 0.27, 0.04, 0, 0.11, 0.16) - 0.014 * Math.sqrt(Math.abs(Math.sin(z * 30 + 1))), 0.03);+ // the lap folds run across, the bodice is drawn up+ d -= 0.012 * Math.sqrt(Math.abs(Math.sin((y + 0.12 * z * z * 40) * 28))) * sstep(0.95, 0.7, y) * sstep(0.45, 0.62, y);+ d -= 0.009 * Math.sqrt(Math.abs(Math.sin(th * 7 + y * 2))) * sstep(0.8, 1.0, y);+ return Math.max(d, -y);+ };+ return (x, y, z) => {+ // Kneeling in prayer, turned to the centre: a body with a thigh and a folded shin under long cloth. The folds fall straight+ // down from the waist and break on the ground, never radiating round the body like the ribs of a pumpkin+ let d = sdCone(x, y, z, -0.1, 0.46, 0, 0.03, 1.04, 0, 0.15, 0.115);+ d = smin(d, sdEll(x - 0.05, y - 1.12, z, 0.08, 0.1, 0.15), 0.03);+ d = smin(d, sdCone(x, y, z, -0.1, 0.42, 0, 0.28, 0.15, 0, 0.16, 0.11), 0.05);+ d = smin(d, sdCone(x, y, z, 0.28, 0.12, 0, -0.26, 0.1, 0, 0.115, 0.09), 0.04);+ const fall = 0.62 * Math.sqrt(Math.abs(Math.sin(z * 31 + x * 3 + 1.1))) + 0.38 * Math.sqrt(Math.abs(Math.sin(z * 63 + y * 5 + 2.3)));+ d -= 0.017 * (fall - 0.55) * sstep(0.86, 0.42, y);+ d -= 0.006 * Math.sqrt(Math.abs(Math.sin(z * 38 + y * 2))) * sstep(0.6, 0.86, y);+ return Math.max(d, -y);+ };+}+async function makePosed(kind, spec) {+ const stone = new Batch(TILE.fine), gold = new Batch(TILE.gold), bF = poseField(kind), hO = Object.assign({ seed: 3 }, spec.head);+ const hy = kind === 'seated' ? 1.74 : 1.36, hx = kind === 'seated' ? -0.01 : 0.075;+ const headM = M.of(M.T(hx, hy, 0), M.Ry(hO.turn || 0), M.Rx(hO.tilt || 0), M.Rz(hO.nod || -6));+ const allF = bF, head = await headBatch(spec.headKey);+ stone.isFigure = true;+ await carveInto(stone, bF, [-0.42, -0.005, -0.42, 0.56, kind === 'seated' ? 1.62 : 1.26, 0.42], 0.0145, 2600, null, allF);+ if (hO.crown) addCrown(gold, M.of(headM, M.T(-0.014, 0.1, 0)), hO.veil ? 0.106 : 0.098);+ // Forearms and hands: joined in prayer when kneeling, one raised when enthroned+ const hand = await handBatch(kind === 'seated' ? 'bless' : 'support', 1);+ if (kind === 'seated') {+ const A = spec.bless === false ? null : { W: [0.2, 1.32, 0.17] };+ const armF = (x, y, z) => { let d = sdCone(x, y, z, -0.02, 1.42, 0.17, 0.06, 1.02, 0.23, 0.066, 0.055); d = smin(d, sdCone(x, y, z, 0.06, 1.02, 0.23, 0.2, 1.3, 0.17, 0.055, 0.058), 0.02); d = Math.min(d, smin(sdCone(x, y, z, -0.02, 1.42, -0.17, 0.05, 1.02, -0.22, 0.066, 0.055), sdCone(x, y, z, 0.05, 1.02, -0.22, 0.27, 0.92, -0.08, 0.055, 0.058), 0.02)); return d; };+ await carveInto(stone, armF, [-0.12, 0.84, -0.34, 0.36, 1.52, 0.34], 0.0125, 700, null, allF);+ copyBatch(stone, hand, frameOf([0.2, 1.3, 0.17], [0.25, 1, 0.05], [0, 0.1, 1]));+ addBook(stone, M.of(M.T(0.36, 0.92, -0.08), M.Rz(-78)), { w: 0.11, h: 0.15 });+ } else {+ const armF = (x, y, z) => { let d = 1e9; for (const s of [-1, 1]) d = Math.min(d, smin(sdCone(x, y, z, 0.03, 1.03, s * 0.15, 0.1, 0.8, s * 0.17, 0.062, 0.05), sdCone(x, y, z, 0.1, 0.8, s * 0.17, 0.25, 0.97, s * 0.04, 0.05, 0.055), 0.02)); return d; };+ await carveInto(stone, armF, [-0.08, 0.68, -0.3, 0.34, 1.12, 0.3], 0.0125, 600, null, allF);+ for (const s of [-1, 1]) copyBatch(stone, hand, frameOf([0.25, 0.97, s * 0.04], [0.5, 1, -s * 0.05], [1, -0.4, s * 0.9]));+ }+ return { stone, gold, head, headM };+}++// Cache of finished figures. Lower-detail copies for the arches come from the same sculpture.+const FIG_CACHE = {};+async function lightCopy(src, tris) { // the same surface with fewer triangles, keeping its uvs and carved occlusion+ const g = await simplifyGeometry(src.toGeo(), { targetTriangles: tris });+ const T = src.tile, b = new Batch(T), p = g.attributes.position, n = g.attributes.normal, u = g.attributes.uv, c = g.attributes.color, idx = g.index;+ for (let i = 0; i < p.count; i++) b.v(p.getX(i), p.getY(i), p.getZ(i), n.getX(i), n.getY(i), n.getZ(i), u.getX(i) * T, u.getY(i) * T, c ? c.getX(i) : 1);+ for (let t = 0; t < idx.count; t += 3) b.tri(idx.getX(t), idx.getX(t + 1), idx.getX(t + 2));+ return b;+}+async function lowDetail(src, tris) {+ const b = await lightCopy(src.stone, tris);+ b.isFigure = true;+ // A figure high in the arches carries a light copy of its head inside its own mesh: fewer draws, fewer triangles+ if (!src.head.low) src.head.low = await lightCopy(src.head, 420);+ copyBatch(b, src.head.low, src.headM);+ return { stone: b, gold: new Batch(TILE.gold) };+}+async function prepareFigures(kinds) {+ for (const k of kinds) FIG_CACHE[k + '|1'] = await makeFigure(k);+ if (P.preview !== 'statues') {+ for (const k of ['apostle', 'king', 'angel', 'bishop', 'queen']) if (FIG_CACHE[k + '|1']) FIG_CACHE[k + '|0'] = await lowDetail(FIG_CACHE[k + '|1'], P.archFigureTris);+ for (let k = 0; k < 3; k++) GARGOYLES[k] = await lightCopy(makeGargoyle(k), 800);+ FIG_CACHE['seated|1'] = await makePosed('seated', { headKey: 'bearded', head: { hair: 'long', beard: 1, seed: 9, nod: -2 } });+ FIG_CACHE['seatedVirgin|1'] = await makePosed('seated', { headKey: 'veiled', head: { veil: 1, crown: 1, smile: 0.35, seed: 8, turn: -14, tilt: 4 } });+ FIG_CACHE['kneel|1'] = await makePosed('kneel', { headKey: 'veiled', head: { veil: 1, seed: 8, tilt: 0, nod: 8 } });+ FIG_CACHE['kneel|0'] = await lowDetail(FIG_CACHE['kneel|1'], P.archFigureTris);+ }+}+function figure(name, lod = 1) {+ const k = FIG_ALIAS[name] || name;+ return FIG_CACHE[k + '|' + lod] || FIG_CACHE[k + '|1'];+}+// Body and head are separate nodes: lod 0 lightens the body only, a head costs its bytes once however often it appears+function placeFigure(name, kind, o = {}) {+ const f = figure(kind, o.lod === undefined ? 1 : o.lod);+ if (!f) return null;+ // o.relief squeezes the figure front to back, so it is carved from the wall behind it instead of standing before it+ const s = o.scale || 1, sc = [s * (o.relief || 1), s, o.mirror ? -s : s];+ const mat = o.indoor ? MAT.figureIn : o.deep ? MAT.figureDeep : o.half ? MAT.figureHalf : MAT.figure;+ const stone = place(name, f.stone, mat, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ place(name + '_Gilt', f.gold, MAT.gold, { pos: o.pos, rot: o.rot, scale: sc, parent: o.parent });+ let headPart = null;+ if (f.head) {+ const m = f.headM, e = eulerFromBasis([m[0], m[4], m[8]], [m[1], m[5], m[9]], [m[2], m[6], m[10]]);+ headPart = place(name + '_Head', f.head, mat, { pos: [m[3], m[7], m[11]], rot: e, parent: stone || undefined });+ }+ if (stone && !o.indoor && !o.deep) FIG_PLACED.push({ part: stone, head: headPart });+ return stone;+}+const FIG_PLACED = [];++// ---- Foliage and crockets ----------------------------------------------------------+// One carved leaf curling at the tip; local +Y up the surface, +X outward from the host+function addLeaf(b, m, size = 1, o = {}) {+ b.at(m, () => {+ const nu = o.coarse ? 2 : 4, nv = o.coarse ? 3 : 4;+ b.grid((u, v) => {+ const s = (u - 0.5) * 2, W = 0.5 * Math.sin(PI * Math.pow(v, 0.72)) * (1 - 0.35 * v);+ const curl = Math.pow(v, 2.2);+ const lobes = 1 + 0.18 * Math.sin(v * 16) * Math.abs(s);+ const rib = 0.05 * (1 - Math.abs(s));+ return [size * (rib + 0.3 * curl - 0.42 * curl * curl * curl + 0.12 * s * s), size * (v - 0.3 * curl * v), size * (s * W * lobes), 0.72 + 0.28 * (1 - Math.abs(s)) + 0.1 * curl];+ }, nu, nv, { flip: true });+ });+}+// A crocket: a bud of curled foliage that sits on an arris. Local +X away from the arris, +Y up the slope.+function addCrocket(b, m, size = 1, lod = 1) {+ b.at(m, () => {+ const path = [];+ const N = lod ? 5 : 3;+ for (let i = 0; i <= N; i++) { const t = i / N, a = -0.9 + t * 3.8, r = lerp(0.3, 0.12, t) * size; path.push([size * 0.16 + r * Math.cos(a) * 0.95, size * 0.1 + r * Math.sin(a) * 0.9, 0]); }+ b.tube(path, (t) => [lerp(0.14, 0.05, t) * size, lerp(0.17, 0.07, t) * size], lod ? 5 : 3, { capStart: false, capEnd: false, ringS: (a) => 0.8 + 0.2 * Math.cos(a) });+ if (lod) for (const s of [-1, 1]) b.tube([[0.02 * size, 0.02 * size, 0], [0.2 * size, 0.1 * size, s * 0.22 * size], [0.34 * size, 0.26 * size, s * 0.3 * size]], (t) => [lerp(0.07, 0.015, t) * size, lerp(0.05, 0.02, t) * size], 3, { capEnd: false });+ });+}+function addFinial(b, h = 1, lod = 1) { // cross-shaped foliate finial, base at y=0+ b.lathe([[0.12 * h, 0], [0.1 * h, 0.05 * h], [0.14 * h, 0.09 * h], [0.08 * h, 0.14 * h], [0.05 * h, 0.5 * h], [0.09 * h, 0.56 * h], [0.05 * h, 0.62 * h], [0.02 * h, 0.98 * h], [0, h]], lod ? 8 : 5);+ for (let i = 0; i < 4; i++) {+ const a = i * 90 + 45;+ addLeaf(b, M.of(M.T(0, 0.14 * h, 0), M.Ry(a), M.T(0.05 * h, 0, 0)), 0.4 * h, { coarse: 1 });+ if (lod) addLeaf(b, M.of(M.T(0, 0.58 * h, 0), M.Ry(a + 45), M.T(0.03 * h, 0, 0)), 0.3 * h, { coarse: 1 });+ }+}+// Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame+function crocketsAlong(b, p, q, n, size, out, lod = 1, maker = addCrocket) {+ const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);+ const dn = d.map((c) => c / L);+ let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];+ const ol = Math.hypot(ox, oy, oz) || 1; ox /= ol; oy /= ol; oz /= ol;+ const zx = ox * 0, side = [dn[1] * oz - dn[2] * oy, dn[2] * ox - dn[0] * oz, dn[0] * oy - dn[1] * ox];+ // columns: X = out, Y = along edge, Z = side+ for (let i = 0; i < n; i++) {+ const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];+ const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];+ maker(b, m, size * lerp(1, 0.78, t), lod);+ }+}+// A crocket beaten in lead: one thick leaf turning up off the arris with a lobe either side. Same frame as addCrocket: X out, Y along the edge+function addLeadCrocket(b, m, s = 1) {+ b.at(m, () => {+ b.tube([[-0.04 * s, -0.02 * s, 0], [0.2 * s, 0.1 * s, 0], [0.34 * s, 0.3 * s, 0], [0.3 * s, 0.46 * s, 0]], (t) => [lerp(0.13, 0.015, t) * s, lerp(0.17, 0.02, t) * s], 4, { capEnd: false });+ for (const z of [-1, 1]) b.tube([[0, 0, z * 0.04 * s], [0.16 * s, 0.1 * s, z * 0.2 * s], [0.2 * s, 0.24 * s, z * 0.3 * s]], (t) => lerp(0.07, 0.012, t) * s, 3, { capEnd: false });+ });+}++// Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.+function addPinnacle(b, w, h, o = {}) {+ const hw = w / 2, shaftH = h * 0.42, gableH = w * 0.95, lod = o.lod === undefined ? 1 : o.lod;+ if (o.simple) { // far-away pinnacle: faceted shaft, spirelet, a few crockets, finial+ b.lathe([[hw * 1.414, 0], [hw * 1.414, shaftH], [hw * 1.6, shaftH + w * 0.06], [hw * 1.3, shaftH + w * 0.12], [hw * 0.1, h * 0.86]], 4, { faceted: true, phase: PI / 4 });+ for (let i = 0; i < 4; i++) { const a = rad(i * 90 + 45), c = Math.cos(a), s = Math.sin(a), bR = hw * 1.3 * 1.0; crocketsAlong(b, [c * bR, shaftH + w * 0.14, s * bR], [c * hw * 0.14, h * 0.84, s * hw * 0.14], 2, w * 0.46, [c, 0.15, s], 0); }+ b.at(M.T(0, h * 0.84, 0), () => addFinial(b, h * 0.16, 0));+ return;+ }+ // Shaft with moulded base and blind lancet panels+ b.lathe([[hw * 1.44, 0], [hw * 1.44, w * 0.06], [hw * 1.3, w * 0.12], [hw * 1.3, w * 0.2], [hw * 1.26, w * 0.24]], 4, { faceted: true, phase: PI / 4 });+ b.lathe([[hw * 1.414, w * 0.2], [hw * 1.414, shaftH]], 4, { faceted: true, phase: PI / 4, s: 0.95 });+ for (let i = 0; i < 4; i++) b.at(M.Ry(i * 90), () => {+ const pw = w * 0.62, y0 = w * 0.35, ys = shaftH - w * 0.5;+ b.at(M.T(hw + 0.004, 0, 0), () => b.at(M.Ry(90), () => { // panel recessed look: shaded inset lancet with moulded frame+ b.sweep(ofsXY(pointedArc(pw, pw * 0.85, 3), 0, ys), hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[-pw / 2, y0], [-pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.sweep([[pw / 2, y0], [pw / 2, ys]], hexBar(w * 0.07, w * 0.08), { caps: false, s: 0.92 });+ b.face([[-pw / 2, y0, -0.002, 0.5], [pw / 2, y0, -0.002, 0.5], [pw / 2, ys, -0.002, 0.5], [0, ys + pw * 0.8, -0.002, 0.45], [-pw / 2, ys, -0.002, 0.5]]);+ }));+ // Gablet on each face with crockets+ b.at(M.T(hw * 0.96, shaftH, 0), () => {+ b.slabX = true;+ b.at(M.Ry(90), () => b.slab([[-hw * 1.04, 0], [hw * 1.04, 0], [0, gableH]], [], 0, w * 0.08, { sf: 1, sb: 0.8 }));+ crocketsAlong(b, [0.02, 0.02, -hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, -1], 0);+ crocketsAlong(b, [0.02, 0.02, hw * 0.98], [0.02, gableH * 0.96, 0], lod ? 2 : 1, w * 0.26, [0.1, 1, 1], 0);+ b.at(M.T(0.04, gableH * 0.94, 0), () => addFinial(b, w * 0.34, 0));+ });+ });+ // Cornice ring and spirelet+ b.at(M.T(0, shaftH, 0), () => {+ b.lathe([[hw * 1.414, 0], [hw * 1.6, w * 0.06], [hw * 1.6, w * 0.1], [hw * 1.414, w * 0.14]], 4, { faceted: true, phase: PI / 4 });+ const sh = h - shaftH - w * 0.1;+ b.at(M.T(0, w * 0.12, 0), () => {+ b.lathe([[hw * 1.3, 0], [hw * 0.09, sh * 0.82]], 8, { faceted: true, phase: PI / 8 });+ const n = lod ? 5 : 3;+ for (let i = 0; i < 4; i++) {+ const a = rad(i * 90 + 22.5 + 22.5 * 0);+ const c = Math.cos(a + rad(22.5)), s = Math.sin(a + rad(22.5));+ const bR = hw * 1.3 * Math.cos(rad(0)), tR = hw * 0.09;+ crocketsAlong(b, [c * bR, sh * 0.04, s * bR], [c * tR, sh * 0.8, s * tR], n, w * 0.36, [c, 0.12, s], lod);+ }+ b.at(M.T(0, sh * 0.8, 0), () => (o.finial || b) === b ? addFinial(b, sh * 0.22 + w * 0.1, lod) : null);+ });+ });+}++// ---- Gargoyle ------------------------------------------------------------------------+// A crouching beast thrusting out from the wall along +X, root at the origin. About 2.3 m long.+function makeGargoyle(kind = 0) {+ const b = new Batch(TILE.fine), rng = mkRng(kind * 17 + 5);+ const ridge = (kind === 1 ? 0.3 : 0.22);+ const headY = kind === 2 ? 0.1 : 0.34;+ const spine = spline([[-0.3, -0.05, 0], [0.22, 0.2, 0], [0.75, 0.3, 0], [1.2, 0.2, 0], [1.56, headY, 0]], 5);+ b.tube(spine, (t) => { const r = keyf([[0, 0.34, 0.3], [0.25, 0.36, 0.34], [0.55, 0.22, 0.24], [0.82, 0.16, 0.17], [1, 0.19, 0.2]], t); return [r[0], r[1]]; }, 16, {+ capStart: true,+ up: [0, 1, 0],+ ring: (a, t) => 1 + ridge * Math.pow(Math.max(Math.cos(a), 0), 10) * Math.abs(Math.sin(t * 26)) + 0.05 * Math.abs(Math.sin(a * 5 + t * 11)),+ ringS: (a, t) => 0.74 + 0.26 * Math.abs(Math.sin(a * 5 + t * 11)),+ });+ // Skull, snout, gaping jaws+ const hx = 1.56, hy = headY;+ blob(b, [hx + 0.05, hy + 0.03, 0], [0.22, 0.19, 0.2], 14, 10, 0.92);+ b.tube(spline([[hx + 0.1, hy + 0.06, 0], [hx + 0.34, hy + 0.05, 0], [hx + 0.56, hy + 0.1, 0]], 3), (t) => [lerp(0.14, 0.07, t), lerp(0.15, 0.085, t)], 10, { capStart: true, up: [0, 1, 0], ring: (a, t) => 1 + 0.2 * Math.pow(Math.max(Math.cos(a), 0), 6) * Math.abs(Math.sin(t * 9)) });+ b.shade(0.9, () => b.tube(spline([[hx + 0.05, hy - 0.1, 0], [hx + 0.28, hy - 0.2, 0], [hx + 0.5, hy - 0.2, 0]], 3), (t) => [lerp(0.09, 0.045, t), lerp(0.125, 0.07, t)], 10, { capStart: true, up: [0, 1, 0] }));+ b.shade(0.34, () => blob(b, [hx + 0.2, hy - 0.06, 0], [0.16, 0.085, 0.095], 8, 5));+ for (let i = 0; i < 4; i++) for (const s of [-1, 1]) {+ const f = i / 3.5;+ b.tube([[hx + 0.26 + f * 0.27, hy + 0.0 + f * 0.075, s * lerp(0.1, 0.06, f)], [hx + 0.27 + f * 0.27, hy - 0.08 + f * 0.075, s * lerp(0.1, 0.06, f)]], (t) => lerp(0.018, 0.002, t), 4, { capStart: true });+ b.tube([[hx + 0.22 + f * 0.27, hy - 0.15, s * lerp(0.1, 0.05, f)], [hx + 0.22 + f * 0.27, hy - 0.08, s * lerp(0.1, 0.05, f)]], (t) => lerp(0.016, 0.002, t), 4, { capStart: true });+ }+ // Brow, eyes, ears or horns+ for (const s of [-1, 1]) {+ b.tube(spline([[hx + 0.02, hy + 0.2, s * 0.03], [hx + 0.17, hy + 0.17, s * 0.1], [hx + 0.24, hy + 0.12, s * 0.15]], 2), (t) => lerp(0.03, 0.02, t), 5, { capStart: true });+ b.shade(0.38, () => blob(b, [hx + 0.21, hy + 0.09, s * 0.11], [0.05, 0.04, 0.045], 6, 4));+ blob(b, [hx + 0.235, hy + 0.09, s * 0.115], [0.024, 0.022, 0.022], 6, 4);+ if (kind === 1) b.tube(spline([[hx - 0.05, hy + 0.14, s * 0.11], [hx - 0.1, hy + 0.3, s * 0.18], [hx - 0.25, hy + 0.42, s * 0.2], [hx - 0.42, hy + 0.42, s * 0.15]], 3), (t) => lerp(0.05, 0.005, t), 6, { capStart: true });+ else b.grid((u, v) => { const L = 0.3, W = 0.09 * Math.sin(PI * u); return [hx - 0.08 - u * L * 0.5, hy + 0.13 + u * L * 0.7, s * (0.13 + u * 0.09 + (v - 0.5) * W * 2), 0.7 + 0.3 * Math.abs(v - 0.5) * 2]; }, 4, 2, { flip: s > 0 });+ if (kind === 1) { /* also add jowls */ blob(b, [hx + 0.1, hy - 0.04, s * 0.13], [0.09, 0.06, 0.05], 6, 4, 0.9); }+ // Fore leg gripping the ledge+ const sh = [0.5, 0.12, s * 0.3], el = [0.68, -0.28, s * 0.42], wr = [0.98, -0.42, s * 0.3];+ b.tube(spline([sh, el, wr], 4), (t) => keyf([[0, 0.16], [0.4, 0.105], [1, 0.07]], t)[0], 10, { capStart: true, ring: (a, t) => 1 + 0.08 * Math.abs(Math.sin(a * 3 + t * 8)) });+ blob(b, wr, [0.1, 0.06, 0.09], 8, 5);+ for (let i = -1; i <= 1; i++) b.tube(spline([[wr[0] + 0.03, wr[1], wr[2] + i * 0.05], [wr[0] + 0.14, wr[1] - 0.02, wr[2] + i * 0.08], [wr[0] + 0.21, wr[1] - 0.11, wr[2] + i * 0.09]], 2), (t) => lerp(0.032, 0.004, t), 5);+ // Folded wing+ const span = kind === 0 ? 0.95 : 0.7;+ const wingFn = (off) => (u, v) => {+ const L = u * span, W = 0.44 * Math.sin(PI * Math.pow(u, 0.7));+ const memb = Math.abs(Math.sin(v * 7.5 + u * 1.2)) * 0.05 * u;+ return [0.42 - L + (off), 0.3 + 0.48 * Math.sin(u * 1.25) * (1 - v * 0.9) - v * W * 0.75 + memb, s * (0.27 + 0.2 * u + v * W * 0.5), 0.68 + 0.32 * (memb / 0.05)];+ };+ b.grid(wingFn(0), 7, 5, { flip: s < 0 });+ b.grid(wingFn(-0.02), 7, 5, { flip: s > 0 });+ }+ return b;+}+const GARGOYLES = [];+function gargoyle(kind) { if (!GARGOYLES[kind]) GARGOYLES[kind] = makeGargoyle(kind); return GARGOYLES[kind]; }++// ==================================================================================+// STAINED GLASS: a tiny vector-art engine producing real leaded panes.+// Every shape is a pane of pot-metal colour with a came (lead strip) around it; large+// figures get painted grisaille lines. Windows are geometry, so they stay crisp at any distance.+// ==================================================================================+function offsetPoly(pts, d) {+ const n = pts.length, s = area2(pts) >= 0 ? 1 : -1, out = [];+ for (let i = 0; i < n; i++) {+ const a = pts[(i - 1 + n) % n], b = pts[i], c = pts[(i + 1) % n];+ let ax = b[0] - a[0], ay = b[1] - a[1], cx = c[0] - b[0], cy = c[1] - b[1];+ const la = Math.hypot(ax, ay) || 1, lc = Math.hypot(cx, cy) || 1; ax /= la; ay /= la; cx /= lc; cy /= lc;+ let nx = (ay + cy) * s, ny = -(ax + cx) * s; const l = Math.hypot(nx, ny) || 1; nx /= l; ny /= l;+ const k = clamp(1 / Math.max(0.35, Math.abs(nx * ay * s - ny * ax * s)), 1, 2.4);+ out.push([b[0] + nx * d * k, b[1] + ny * d * k]);+ }+ return out;+}+function clipConvex(poly, clip) {+ let out = poly; const sgn = area2(clip) >= 0 ? 1 : -1;+ for (let i = 0; i < clip.length && out.length; i++) {+ const a = clip[i], b = clip[(i + 1) % clip.length], inp = out; out = [];+ const side = (p) => sgn * ((b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]));+ for (let j = 0; j < inp.length; j++) {+ const p = inp[j], q = inp[(j + 1) % inp.length], sp = side(p), sq = side(q);+ if (sp >= 0) out.push(p);+ if ((sp >= 0) !== (sq >= 0)) { const t = sp / (sp - sq); out.push([lerp(p[0], q[0], t), lerp(p[1], q[1], t)]); }+ }+ }+ return out;+}+function isConvex(p) { let sign = 0; for (let i = 0; i < p.length; i++) { const a = p[i], b = p[(i + 1) % p.length], c = p[(i + 2) % p.length]; const s = (b[0] - a[0]) * (c[1] - b[1]) - (b[1] - a[1]) * (c[0] - b[0]); if (Math.abs(s) < 1e-9) continue; if (!sign) sign = Math.sign(s); else if (Math.sign(s) !== sign) return false; } return true; }++class Glass {+ constructor(seed = 1) { this.b = new Batch(1); this.b.noColor = true; this.r = mkRng(seed * 13 + 3); this.clip = null; this.xf = [1, 0, 0, 1, 0, 0]; this.came = 0.022; }+ xform(a, b, c, d, e, f) { const m = this.xf; return [m[0] * a + m[2] * b, m[1] * a + m[3] * b, m[0] * c + m[2] * d, m[1] * c + m[3] * d, m[0] * e + m[2] * f + m[4], m[1] * e + m[3] * f + m[5]]; }+ withXf(rotDeg, tx, ty, s, fn) { const o = this.xf, c = Math.cos(rad(rotDeg)) * s, sn = Math.sin(rad(rotDeg)) * s; this.xf = this.xform(c, sn, -sn, c, tx, ty); fn(this); this.xf = o; }+ withClip(pts, fn) { const o = this.clip; const m = this.xf; this.clip = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]); fn(this); this.clip = o; }+ toLocal(p) { const m = this.xf, det = m[0] * m[3] - m[1] * m[2], x = p[0] - m[4], y = p[1] - m[5]; return [(m[3] * x - m[2] * y) / det, (-m[1] * x + m[0] * y) / det]; }+ // Writes one flat polygon. map(worldPoint) -> [u,v] in atlas pixels; otherwise a window on the sheet of its colour+ raw(pts, color, layer, jitter = 1, map = null) {+ if (layer === -0.4 && !this.b.outline) this.b.outline = pts; // the first polygon of a window is its whole outline+ const name = GLASS_ALIAS[color] || color, fam = GL.fam[name] || GL.fam.lead, z = 0.002 + layer * 0.0035, C = GL.small;+ if (this.b.panes) this.b.panes.push([pts.map((p) => [p[0], p[1]]), name, layer]); // the cartoon, for the light this window throws+ // Every pane is cut from a different part of the sheet, so no two panes match;+ // 200 pixels of sheet to the metre, so the streaks and clouds of the pot-metal show in the pane+ const ox = fam[0] + 44 + this.r() * (C - 88), oy = fam[1] + 44 + this.r() * (C - 88), rot = this.r() * 6.28, cr = Math.cos(rot) * 200, sr = Math.sin(rot) * 200;+ let mx = 0, my = 0; for (const p of pts) { mx += p[0]; my += p[1]; } mx /= pts.length; my /= pts.length;+ const ids = pts.map((p) => {+ let u, v;+ if (map) { const q = map(p); u = q[0]; v = q[1]; } else {+ const dx = clamp(p[0] - mx, -0.2, 0.2), dy = clamp(p[1] - my, -0.2, 0.2);+ u = ox + (dx * cr - dy * sr) * jitter; v = oy + (dx * sr + dy * cr) * jitter;+ }+ return this.b.v(p[0], p[1], z, 0, 0, 1, u / GL.w, v / GL.h, 1);+ });+ if (isConvex(pts)) { const flip = area2(pts) < 0; for (let i = 1; i < ids.length - 1; i++) { if (flip) this.b.tri(ids[0], ids[i + 1], ids[i]); else this.b.tri(ids[0], ids[i], ids[i + 1]); } } else { const t = triangulate(pts); for (const k of t) this.b.tri(ids[k[0]], ids[k[1]], ids[k[2]]); }+ }+ // a pane: colour polygon + lead came. o.tile paints drapery across it, o.orn ornament.+ poly(pts, color, layer = 1, o = {}) {+ const m = this.xf; let P2 = pts.map((p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]);+ if (area2(P2) < 0) P2 = P2.slice().reverse();+ const came = o.came === undefined ? this.came : o.came;+ if (came > 0) {+ let C = offsetPoly(P2, came);+ if (this.clip) C = clipConvex(C, this.clip);+ if (C.length >= 3 && Math.abs(area2(C)) > 1e-5) this.raw(C, 'lead', layer - 0.5, 0);+ } else if (came < 0) P2 = offsetPoly(P2, came); // over a lead backing: inset instead of a second polygon+ if (this.clip) P2 = clipConvex(P2, this.clip);+ if (P2.length < 3 || Math.abs(area2(P2)) <= 1e-5) return this;+ let map = o.map || null;+ if (o.tile && GL.tile[o.tile]) { // planar mapping of the painted tile over the local box+ const T = GL.tile[o.tile], B = o.box, f = o.flipU ? -1 : 1;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + (f > 0 ? a : 1 - a) * (T[2] - 4), T[1] + 2 + (1 - c) * (T[3] - 4)]; };+ } else if (o.orn && GL.orn[o.orn]) {+ const T = GL.orn[o.orn], B = o.box;+ map = (p) => { const q = this.toLocal(p), a = (q[0] - B[0]) / (B[2] - B[0]), c = (q[1] - B[1]) / (B[3] - B[1]); return [T[0] + 2 + a * (GL.small - 4), T[1] + 2 + (1 - c) * (GL.small - 4)]; };+ }+ this.raw(P2, color, layer, o.jitter === undefined ? 1 : o.jitter, map);+ return this;+ }+ disc(cx, cy, r, color, layer = 1, n = 12, o) { return this.poly(ringPts(cx, cy, r, n), color, layer, o); }+ // a head drawn by the glass painter: an oval pane mapped onto a face from the atlas+ face(cx, cy, rx, ry, kind, layer, flip) {+ const F = GL.face[kind] || GL.face.youth, C = GL.cell, pts = []; for (let i = 0; i < 12; i++) { const a = i * TAU / 12; pts.push([cx + rx * Math.cos(a), cy + ry * Math.sin(a)]); }+ return this.poly(pts, 'flesh', layer, { came: 0.012, map: (p) => { const q = this.toLocal(p), a = ((q[0] - cx) / rx) * (flip ? -1 : 1), c = (q[1] - cy) / ry; return [F[0] + C / 2 + clamp(a, -1, 1) * (C / 2 - 3), F[1] + C / 2 - clamp(c, -1, 1) * (C / 2 - 3)]; } });+ }+ // painted line (grisaille): a thin dark ribbon along a polyline+ line(pts, w = 0.02, color = 'lead', layer = 5) {+ for (let i = 0; i < pts.length - 1; i++) {+ const a = pts[i], b = pts[i + 1]; let dx = b[0] - a[0], dy = b[1] - a[1]; const l = Math.hypot(dx, dy) || 1; dx *= w / 2 / l; dy *= w / 2 / l;+ this.poly([[a[0] - dy, a[1] + dx], [a[0] + dy, a[1] - dx], [b[0] + dy, b[1] - dx], [b[0] - dy, b[1] + dx]], color, layer, { came: 0, jitter: 0 });+ }+ return this;+ }+}++// Diapered mosaic background: lozenges of two deep colours, a jewel at each crossing+// (one painted square of leaded lozenges laid down repeatedly: fine as real mosaic, cheap as two triangles a square)+function gDiaper(g, x0, y0, x1, y1, cell, c1, c2, c3, layer = 0) {+ const T = c1 === 'outer' ? 1.5 : 0.72, G = GL.ground[GROUND_OF[c1] || 'rc'], C = GL.cell;+ for (let y = Math.floor(y0 / T) * T; y < y1; y += T) for (let x = Math.floor(x0 / T) * T; x < x1; x += T) {+ const qx = x, qy = y, map = (p) => [G[0] + 2 + clamp((p[0] - qx) / T) * (C - 4), G[1] + 2 + clamp((p[1] - qy) / T) * (C - 4)];+ g.poly([[x, y], [x + T, y], [x + T, y + T], [x, y + T]], c1, layer, { came: 0, map });+ }+}++// A figure in glass: long Gothic proportions, robes painted in brown, the head drawn by hand. (x,y) = feet.+function gFigure(g, x, y, h, o = {}) {+ const sx = o.sx || 0.82, hTot = h; h = hTot * sx;+ const f = o.flip ? -1 : 1, L = (u, v) => [x + f * u * h, y + (v <= 0.76 ? v / 0.76 * (hTot - 0.24 * h) : hTot - (1 - v) * h)], PS = (a) => a.map((p) => L(p[0], p[1]));+ const robe = o.robe || 'white', mantle = o.mantle || 'cobalt', skin = o.skin || 'flesh', big = hTot > 2.2, layer = o.layer === undefined ? 2 : o.layer;+ const sit = o.pose === 'sit', kneel = o.pose === 'kneel';+ const hy = kneel ? 0.67 : sit ? 0.86 : 0.875, hr = 0.066, hx = kneel ? 0.04 : 0;+ // Box in world units for tile mapping, in the local frame of the Glass object+ const bx = [x - h * 0.34, y, x + h * 0.34, y + hTot * 0.86], tR = DRAPE_OF[robe] || 'white', tM = DRAPE_OF[mantle] || 'cobalt';+ const pane = (pts, col, lay, tile, opt) => g.poly(PS(pts), col, lay, Object.assign({ tile, box: bx, flipU: !!o.flip }, opt || {}));+ const brk = (pts, c1, lay, tile) => { // a robe is many pieces of glass: cut it across with lead+ const P3 = PS(pts); let y0 = 1e9, y1 = -1e9; for (const q of P3) { y0 = Math.min(y0, q[1]); y1 = Math.max(y1, q[1]); }+ const n = big ? 4 : (hTot > 0.9 ? 2 : 1);+ for (let i = 0; i < n; i++) {+ const a = lerp(y0, y1, i / n), c = lerp(y0, y1, (i + 1) / n), sl = big ? 0.12 * h * (i % 2 ? 1 : -1) * 0.5 : 0;+ const piece = clipConvex(clipConvex(P3, [[-1e4, a - sl * 0], [1e4, a + sl], [1e4, 1e4], [-1e4, 1e4]]), [[-1e4, -1e4], [1e4, -1e4], [1e4, c + sl], [-1e4, c - sl * 0]]);+ if (piece.length >= 3) g.poly(piece, c1, lay, { tile, box: bx, flipU: !!o.flip, came: 0.016 });+ }+ };+ if (o.wings) {+ pane([[-0.08, hy - 0.1], [-0.3, hy + 0.1], [-0.4, hy - 0.08], [-0.34, 0.4], [-0.24, 0.2], [-0.1, 0.42]], o.wings, layer - 0.2, null);+ pane([[0.08, hy - 0.1], [0.3, hy + 0.1], [0.4, hy - 0.08], [0.34, 0.4], [0.24, 0.2], [0.1, 0.42]], o.wings, layer - 0.2, null);+ if (hTot > 1) for (const s of [-1, 1]) { g.line(PS([[s * 0.1, hy - 0.08], [s * 0.3, 0.52]]), h * 0.01, 'lead', layer); g.line(PS([[s * 0.2, hy - 0.02], [s * 0.34, 0.42]]), h * 0.008, 'lead', layer); }+ }+ if (o.halo !== false) {+ const c = L(hx, hy);+ g.disc(c[0], c[1], h * 0.116, o.halo || 'gold', layer - 0.1, big ? 18 : 12);+ if (o.cross) { for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[c[0] + h * 0.116 * Math.cos(a - 0.26), c[1] + h * 0.116 * Math.sin(a - 0.26)], [c[0] + h * 0.116 * Math.cos(a + 0.26), c[1] + h * 0.116 * Math.sin(a + 0.26)], c], 'ruby', layer, { came: 0.008 }); } }+ }+ // ---- body ----+ if (sit) {+ brk([[-0.12, 0.72], [0.12, 0.72], [0.19, 0.44], [0.26, 0.4], [0.27, 0.08], [0.22, 0.0], [-0.22, 0.0], [-0.2, 0.3], [-0.14, 0.44]], robe, layer, tR);+ brk([[-0.13, 0.74], [0.05, 0.74], [0.17, 0.46], [0.24, 0.4], [0.2, 0.18], [-0.06, 0.1], [-0.2, 0.3], [-0.14, 0.46]], mantle, layer + 0.5, tM);+ } else if (kneel) {+ brk([[-0.08, 0.56], [0.13, 0.56], [0.16, 0.26], [0.26, 0.1], [0.26, 0], [-0.26, 0], [-0.2, 0.12]], robe, layer, tR);+ brk([[-0.09, 0.57], [0.08, 0.57], [0.13, 0.28], [0.1, 0.1], [-0.2, 0.05], [-0.16, 0.24]], mantle, layer + 0.5, tM);+ } else {+ const c = o.sway === undefined ? 0.02 : o.sway;+ brk([[-0.1 + c, 0.75], [0.1 + c, 0.75], [0.115, 0.46], [0.14, 0.2], [0.2, 0.03], [0.15, 0.0], [0.05, 0.022], [-0.05, 0.0], [-0.16, 0.012], [-0.2, 0.04], [-0.14, 0.22], [-0.115, 0.46]], robe, layer, tR);+ brk([[-0.11 + c, 0.76], [0.03 + c, 0.76], [0.12, 0.5], [0.135, 0.3], [0.09, 0.15], [-0.06, 0.11], [-0.165, 0.22], [-0.125, 0.46]], mantle, layer + 0.5, tM);+ pane([[-0.09, 0.03], [-0.02, 0.03], [-0.02, -0.012], [-0.115, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ pane([[0.02, 0.03], [0.09, 0.03], [0.115, -0.012], [0.02, -0.012]], o.shoe || 'brown', layer + 0.2, null, { came: 0.008 });+ }+ // ---- arms and hands ----+ const by = kneel ? 0.5 : 0.68, armPose = o.arm || 'hold';+ const ribbon = (a, b, w, col, tile) => { const dx = b[0] - a[0], dy = b[1] - a[1], l = Math.hypot(dx, dy) || 1, nx = -dy / l * w / 2, ny = dx / l * w / 2; g.poly(PS([[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]]), col, layer + 1, { came: 0.012, tile, box: bx }); };+ const hand = (p) => { const c = L(p[0], p[1]); g.poly([[c[0] - h * 0.026, c[1] - h * 0.018], [c[0] + h * 0.018, c[1] - h * 0.022], [c[0] + h * 0.03, c[1] + h * 0.016], [c[0] + h * 0.006, c[1] + h * 0.03], [c[0] - h * 0.02, c[1] + h * 0.018]], skin, layer + 1.3, { came: 0.008 }); };+ const arms = { hold: [[0.13, by - 0.12], [0.03, by - 0.18], [-0.13, by - 0.12], [-0.03, by - 0.2]], up: [[0.17, by - 0.03], [0.27, by + 0.18], [-0.12, by - 0.12], [-0.03, by - 0.22]], out: [[0.19, by - 0.08], [0.32, by - 0.06], [-0.19, by - 0.08], [-0.32, by - 0.06]], pray: [[0.1, by - 0.14], [0.12, by - 0.03], [-0.04, by - 0.14], [0.1, by - 0.03]], point: [[0.17, by - 0.08], [0.33, by + 0.04], [-0.12, by - 0.12], [-0.04, by - 0.2]], orant: [[0.2, by + 0.0], [0.27, by + 0.14], [-0.2, by + 0.0], [-0.27, by + 0.14]] };+ const A = arms[armPose] || arms.hold;+ ribbon([0.095, by], A[0], 0.07, mantle, tM); ribbon(A[0], A[1], 0.056, robe, tR); hand(A[1]);+ ribbon([-0.095, by], A[2], 0.07, mantle, tM); ribbon(A[2], A[3], 0.056, robe, tR); hand(A[3]);+ // ---- attributes ----+ const at = o.attr;+ if (at === 'book') { g.poly(PS([[A[1][0] - 0.1, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] - 0.02], [A[1][0] + 0.02, A[1][1] + 0.12], [A[1][0] - 0.1, A[1][1] + 0.12]]), 'ruby', layer + 1.5, { came: 0.01 }); g.poly(PS([[A[1][0] - 0.07, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.03], [A[1][0] - 0.01, A[1][1] + 0.09], [A[1][0] - 0.07, A[1][1] + 0.09]]), 'gold', layer + 1.6, { came: 0 }); }+ else if (at === 'staff' || at === 'cross') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, 0], [sxx + 0.012, 0], [sxx + 0.012, 1.08], [sxx - 0.012, 1.08]]), 'gold', layer + 0.9, { came: 0.008 }); if (at === 'cross') g.poly(PS([[sxx - 0.07, 0.95], [sxx + 0.07, 0.95], [sxx + 0.07, 0.98], [sxx - 0.07, 0.98]]), 'gold', layer + 0.9, { came: 0.008 }); else g.poly(ringPts(sxx + 0.03, 1.1, 0.045, 8).map((p) => L(p[0], p[1])), 'gold', layer + 0.9, { came: 0.008 }); }+ else if (at === 'sword') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.012, by - 0.08], [sxx + 0.012, by - 0.08], [sxx, 1.02]]), 'white', layer + 0.9, { came: 0.008 }); g.poly(PS([[sxx - 0.05, by - 0.08], [sxx + 0.05, by - 0.08], [sxx + 0.05, by - 0.05], [sxx - 0.05, by - 0.05]]), 'gold', layer + 1, { came: 0.008 }); }+ else if (at === 'key') { const sxx = A[1][0]; g.poly(PS([[sxx - 0.01, by - 0.14], [sxx + 0.01, by - 0.14], [sxx + 0.01, by + 0.2], [sxx - 0.01, by + 0.2]]), 'gold', layer + 1, { came: 0.006 }); g.poly(ringPts(sxx, by + 0.23, 0.035, 6).map((p) => L(p[0], p[1])), 'gold', layer + 1, { came: 0.006 }); g.poly(PS([[sxx, by - 0.14], [sxx + 0.05, by - 0.14], [sxx + 0.05, by - 0.1], [sxx, by - 0.1]]), 'gold', layer + 1, { came: 0.006 }); }+ else if (at === 'lily') { const sxx = A[1][0]; g.line(PS([[sxx, by - 0.22], [sxx + 0.01, by + 0.2]]), h * 0.012, 'green', layer + 1); for (let i = 0; i < 3; i++) { const yy = by + 0.08 + i * 0.07; const q = L(sxx + 0.02 * (i % 2 ? 1 : -1), yy); g.disc(q[0], q[1], h * 0.024, 'white', layer + 1.1, 5, { came: 0.006 }); } }+ // ---- head: drawn, not a disc ----+ const hc = L(hx, hy);+ const kind = o.face || (o.wings ? 'angel' : o.veil ? 'virgin' : o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ const nkA = L(hx - 0.02, hy - 0.11), nkB = L(hx + 0.02, hy - 0.11), nkC = L(hx + 0.02, hy - 0.04), nkD = L(hx - 0.02, hy - 0.04);+ g.poly([nkA, nkB, nkC, nkD], skin, layer + 1.5, { came: 0.008 });+ g.face(hc[0], hc[1], h * hr * 1.02, h * hr * 1.24, kind, layer + 1.8, !!o.flip);+ if (o.crown) g.poly(PS([[hx - 0.066, hy + 0.05], [hx + 0.066, hy + 0.05], [hx + 0.08, hy + 0.13], [hx + 0.04, hy + 0.095], [hx, hy + 0.14], [hx - 0.04, hy + 0.095], [hx - 0.08, hy + 0.13]]), 'gold', layer + 2.2, { came: 0.01 });+ if (o.mitre) g.poly(PS([[hx - 0.056, hy + 0.05], [hx + 0.056, hy + 0.05], [hx + 0.048, hy + 0.15], [hx, hy + 0.215], [hx - 0.048, hy + 0.15]]), o.mitre, layer + 2.2, { came: 0.01 });+}+// ---- Narrative scenes, composed inside a circle of radius r at the origin ---------+const SCENES = {+ annunciation(g, r) { // Angel greets the Virgin; lily between them, dove above+ gFigure(g, -r * 0.46, -r * 0.72, r * 1.34, { robe: 'white', mantle: 'emerald', wings: 'rubyLt', arm: 'point', attr: null, hair: 'amber' });+ gFigure(g, r * 0.46, -r * 0.72, r * 1.3, { flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray', halo: 'gold' });+ g.poly([[-0.1, -r * 0.72], [0.1, -r * 0.72], [0.06, -r * 0.5], [-0.06, -r * 0.5]], 'gold', 3, { came: 0.012 });+ g.line([[0, -r * 0.5], [0, r * 0.05]], 0.025, 'green', 3.2);+ [0.05, -0.1, -0.24].forEach((v, i) => g.disc((i % 2 ? 1 : -1) * r * 0.05, r * v, r * 0.05, 'white', 3.3, 5, { came: 0.008 }));+ g.disc(0, r * 0.62, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.14, r * 0.66], [0, r * 0.6], [r * 0.14, r * 0.66], [0, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ nativity(g, r) { // Virgin, Child in the crib, Joseph, the star, the ox and ass+ g.poly([[-r * 0.8, -r * 0.9], [r * 0.8, -r * 0.9], [r * 0.8, -r * 0.62], [-r * 0.8, -r * 0.62]], 'brown', 1.5);+ gFigure(g, -r * 0.42, -r * 0.62, r * 1.05, { pose: 'sit', robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'out' });+ gFigure(g, r * 0.56, -r * 0.62, r * 1.1, { robe: 'tan', mantle: 'emerald', beard: 1, hair: 'grey', beardCol: 'pearl', arm: 'hold', attr: 'staff', flip: 1, halo: 'yellow' });+ g.poly([[-r * 0.1, -r * 0.62], [r * 0.3, -r * 0.62], [r * 0.34, -r * 0.34], [-r * 0.14, -r * 0.34]], 'amber', 2, { came: 0.014 });+ g.poly([[-r * 0.07, -r * 0.34], [r * 0.27, -r * 0.34], [r * 0.27, -r * 0.22], [-r * 0.07, -r * 0.22]], 'white', 2.5, { came: 0.012 });+ g.disc(r * 0.02, -r * 0.24, r * 0.065, 'gold', 2.5, 8, { came: 0.008 }); g.disc(r * 0.02, -r * 0.24, r * 0.043, 'flesh', 3, 7, { came: 0.008 });+ star(g, 0, r * 0.68, r * 0.13, 'yellow', 3);+ g.poly([[-r * 0.4, r * 0.22], [0, r * 0.52], [r * 0.4, r * 0.22], [r * 0.36, r * 0.14], [0, r * 0.4], [-r * 0.36, r * 0.14]], 'bone', 1.5);+ },+ magi(g, r) { // Three crowned kings bring gifts to the enthroned Virgin+ gFigure(g, r * 0.56, -r * 0.8, r * 1.2, { pose: 'sit', flip: 1, robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'hold' });+ g.disc(r * 0.4, -r * 0.1, r * 0.06, 'gold', 4, 8, { came: 0.008 });+ gFigure(g, -r * 0.05, -r * 0.8, r * 1.18, { pose: 'kneel', robe: 'emerald', mantle: 'ruby', crown: 1, beard: 2, hair: 'grey', beardCol: 'pearl', arm: 'point', halo: false });+ gFigure(g, -r * 0.42, -r * 0.8, r * 1.32, { robe: 'violet', mantle: 'gold', crown: 1, beard: 1, arm: 'point', halo: false });+ gFigure(g, -r * 0.74, -r * 0.8, r * 1.3, { robe: 'teal', mantle: 'rubyLt', crown: 1, arm: 'hold', halo: false });+ star(g, -r * 0.1, r * 0.72, r * 0.11, 'yellow', 3);+ },+ crucifixion(g, r) { // Christ on the cross, the Virgin and Saint John, sun and moon+ g.poly([[-0.09 * r, -r * 0.92], [0.09 * r, -r * 0.92], [0.09 * r, r * 0.76], [-0.09 * r, r * 0.76]], 'emerald', 1.5, { came: 0.016 });+ g.poly([[-r * 0.58, r * 0.28], [r * 0.58, r * 0.28], [r * 0.58, r * 0.45], [-r * 0.58, r * 0.45]], 'emerald', 1.5, { came: 0.016 });+ gFigure(g, 0, -r * 0.84, r * 1.42, { robe: 'white', mantle: 'white', cross: 1, arm: 'out', halo: 'gold', hair: 'hair', beard: 1, sway: 0 });+ gFigure(g, -r * 0.62, -r * 0.88, r * 1.1, { robe: 'white', mantle: 'cobalt', veil: 'azure', arm: 'pray' });+ gFigure(g, r * 0.62, -r * 0.88, r * 1.1, { flip: 1, robe: 'white', mantle: 'rubyLt', arm: 'hold', attr: 'book' });+ g.disc(-r * 0.68, r * 0.66, r * 0.11, 'gold', 2, 8); g.poly([[r * 0.59, r * 0.74], [r * 0.76, r * 0.6], [r * 0.71, r * 0.76]], 'pearl', 2);+ },+ resurrection(g, r) { // Christ rises from the tomb holding the banner; soldiers sleep below+ g.poly([[-r * 0.72, -r * 0.84], [r * 0.72, -r * 0.84], [r * 0.72, -r * 0.42], [-r * 0.72, -r * 0.42]], 'bone', 2);+ g.line([[-r * 0.72, -r * 0.56], [r * 0.72, -r * 0.56]], 0.02, 'lead', 3); [-0.4, 0, 0.4].forEach((k) => g.disc(r * k, -r * 0.68, r * 0.09, 'purple', 3, 8, { came: 0.01 }));+ gFigure(g, 0, -r * 0.42, r * 1.25, { robe: 'white', mantle: 'rubyLt', cross: 1, arm: 'up', hair: 'hair', beard: 1, halo: 'gold' });+ g.poly([[r * 0.34, r * 0.72], [r * 0.34, r * 0.92], [r * 0.7, r * 0.84], [r * 0.34, r * 0.72]], 'white', 3, { came: 0.01 }); g.line([[r * 0.34, r * 0.92], [r * 0.34, -r * 0.1]], 0.02, 'gold', 3.2);+ for (const s of [-1, 1]) { g.disc(s * r * 0.82, -r * 0.5, r * 0.07, 'flesh', 3, 7, { came: 0.01 }); g.poly([[s * r * 0.6, -r * 0.86], [s * r * 0.92, -r * 0.86], [s * r * 0.9, -r * 0.54], [s * r * 0.7, -r * 0.54]], s > 0 ? 'emerald' : 'cobalt', 2.6); }+ },+ supper(g, r) { // The Last Supper: Christ at the centre of a laid table+ [-0.62, -0.31, 0.31, 0.62].forEach((k, i) => gBust(g, r * k, -r * 0.05, r * 0.92, { robe: ['emerald', 'ruby', 'amber', 'violet'][i], beard: i % 2 ? 1 : 0, hair: i === 3 ? 'grey' : 'hair' }));+ gBust(g, 0, -r * 0.02, r * 1.04, { robe: 'rubyLt', beard: 1, cross: 1 });+ g.poly([[-r * 0.92, -r * 0.5], [r * 0.92, -r * 0.5], [r * 0.92, -r * 0.2], [-r * 0.92, -r * 0.2]], 'white', 4);+ g.poly([[-r * 0.86, -r * 0.5], [r * 0.86, -r * 0.5], [r * 0.82, -r * 0.92], [-r * 0.82, -r * 0.92]], 'brown', 3.6);+ [-0.58, -0.2, 0.2, 0.58].forEach((k) => g.disc(r * k, -r * 0.3, r * 0.055, 'gold', 4.5, 6, { came: 0.008 }));+ },+ noah(g, r) { // The Ark on the waters, the dove returning+ [-0.82, -0.56].forEach((v, i) => g.poly([[-r, r * v], [r, r * v], [r, r * (v + 0.22)], [-r, r * (v + 0.18)]], i ? 'azure' : 'cobalt', 2));+ g.poly([[-r * 0.7, -r * 0.4], [r * 0.7, -r * 0.4], [r * 0.5, -r * 0.7], [-r * 0.5, -r * 0.7]], 'brown', 2.5);+ g.poly([[-r * 0.46, -r * 0.4], [r * 0.46, -r * 0.4], [r * 0.46, r * 0.02], [-r * 0.46, r * 0.02]], 'tan', 3);+ g.poly([[-r * 0.56, r * 0.0], [0, r * 0.32], [r * 0.56, r * 0.0]], 'ruby', 3.2);+ [-0.2, 0.2].forEach((k) => g.disc(r * k, -r * 0.18, r * 0.1, 'night', 3.4, 7, { came: 0.012 }));+ gBust(g, r * 0.2, -r * 0.3, r * 0.42, { robe: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', layer: 3.6 });+ g.disc(-r * 0.54, r * 0.64, r * 0.07, 'white', 3, 6, { came: 0.01 }); g.poly([[-r * 0.72, r * 0.72], [-r * 0.54, r * 0.62], [-r * 0.36, r * 0.74], [-r * 0.54, r * 0.7]], 'pearl', 2.8, { came: 0.01 });+ },+ shepherd(g, r) { // The Good Shepherd carries the lamb; a flock in green pasture+ g.poly([[-r, -r], [r, -r], [r, -r * 0.5], [-r, -r * 0.62]], 'green', 1.5);+ gFigure(g, 0, -r * 0.82, r * 1.5, { robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'hold', attr: 'staff' });+ g.disc(0, r * 0.26, r * 0.11, 'white', 4.5, 8, { came: 0.01 }); g.disc(r * 0.13, r * 0.3, r * 0.05, 'pearl', 4.8, 6, { came: 0.008 });+ for (const [x, y] of [[-0.68, -0.64], [0.66, -0.62], [-0.46, -0.78]]) { g.disc(r * x, r * y, r * 0.11, 'white', 3, 7, { came: 0.01 }); g.disc(r * x + r * 0.12, r * y + r * 0.05, r * 0.05, 'grey', 3.2, 5, { came: 0.008 }); }+ },+ david(g, r) { // King David with his harp+ gFigure(g, -r * 0.14, -r * 0.82, r * 1.42, { pose: 'sit', robe: 'violet', mantle: 'ruby', crown: 1, beard: 1, arm: 'out', halo: false });+ g.poly([[r * 0.26, -r * 0.6], [r * 0.7, r * 0.2], [r * 0.7, r * 0.46], [r * 0.16, -r * 0.36]], 'gold', 3, { came: 0.012 });+ g.line([[r * 0.2, -r * 0.4], [r * 0.2, r * 0.4]], 0.018, 'gold', 3.1); [0.3, 0.4, 0.5, 0.6].forEach((k) => g.line([[r * k, -r * 0.42 + r * (k - 0.2)], [r * k, r * 0.34]], 0.008, 'white', 3.2));+ g.poly([[-r * 0.86, -r * 0.84], [r * 0.5, -r * 0.84], [r * 0.5, -r * 0.7], [-r * 0.86, -r * 0.7]], 'tan', 1.8);+ },+};+function star(g, x, y, r, c, layer) { const p = []; for (let i = 0; i < 16; i++) { const a = i * PI / 8 + PI / 2, q = i % 2 ? r * 0.38 : r * (i % 4 ? 0.7 : 1); p.push([x + q * Math.cos(a), y + q * Math.sin(a)]); } g.poly(p, c, layer, { came: 0.01 }); }+function gBust(g, x, y, h, o = {}) { // head and shoulders, the head drawn by the painter+ const L = o.layer || 2, bx = [x - h * 0.22, y - h * 0.32, x + h * 0.22, y + h * 0.46], t = DRAPE_OF[o.robe || 'ruby'] || 'ruby';+ if (o.halo !== false) g.disc(x, y + h * 0.58, h * 0.17, o.halo || 'gold', L - 0.1, 10);+ g.poly([[x - h * 0.2, y - h * 0.3], [x + h * 0.2, y - h * 0.3], [x + h * 0.17, y + h * 0.3], [x + h * 0.06, y + h * 0.42], [x - h * 0.06, y + h * 0.42], [x - h * 0.17, y + h * 0.3]], o.robe || 'ruby', L, { tile: t, box: bx });+ const kind = o.face || (o.crown && o.beard ? 'king' : o.cross ? 'christ' : o.beard === 2 || o.beardCol ? 'elder' : o.beard ? 'christ' : 'youth');+ g.face(x, y + h * 0.58, h * 0.108, h * 0.132, kind, L + 0.5, false);+ if (o.crown) g.poly([[x - h * 0.1, y + h * 0.66], [x + h * 0.1, y + h * 0.66], [x + h * 0.12, y + h * 0.78], [x + h * 0.05, y + h * 0.72], [x, y + h * 0.8], [x - h * 0.05, y + h * 0.72], [x - h * 0.12, y + h * 0.78]], 'gold', L + 0.7, { came: 0.008 });+ if (o.cross) for (let i = 0; i < 3; i++) { const a = i * PI / 2; g.poly([[x + h * 0.17 * Math.cos(a - 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a - 0.24)], [x + h * 0.17 * Math.cos(a + 0.24), y + h * 0.58 + h * 0.17 * Math.sin(a + 0.24)], [x, y + h * 0.58]], 'ruby', L + 0.2, { came: 0.008 }); }+}+// A jewelled ring of short leaded segments+function gRim(g, cx, cy, r0, r1, cols, n, layer) {+ g.disc(cx, cy, r1 + 0.02, 'lead', layer - 0.3, Math.max(10, n), { came: 0, jitter: 0 });+ for (let i = 0; i < n; i++) {+ const a = i * TAU / n, b = (i + 1) * TAU / n;+ g.poly([[cx + r0 * Math.cos(a), cy + r0 * Math.sin(a)], [cx + r1 * Math.cos(a), cy + r1 * Math.sin(a)], [cx + r1 * Math.cos(b), cy + r1 * Math.sin(b)], [cx + r0 * Math.cos(b), cy + r0 * Math.sin(b)]], cols[i % cols.length], layer, { came: -0.01 });+ }+}+// A lancet told as a stack of medallions over a diapered ground, with a jewelled border+function gLancetStory(g, x, w, y0, ys, rise, scenes, o = {}) {+ const c1 = o.c1 || 'ruby', c2 = o.c2 || 'cobalt', edge = o.edge || ['gold', 'ruby', 'white', 'cobalt'];+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse().slice(0)];+ g.withClip(outline, () => {+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, o.cell || 0.6, c1, c2, 'gold');+ const H = ys + rise * 0.5 - y0, N = scenes.length, step = H / N, R = Math.min(w * 0.44, step * 0.47);+ scenes.forEach((nm, i) => {+ const cy = y0 + step * (i + 0.5);+ const ground = (i % 2) ? (o.g1 || 'cobalt') : (o.g2 || 'ruby');+ gRim(g, x, cy, R, R * 1.055, ['gold', 'ruby', 'gold', 'cobalt'], 22, 1);+ g.disc(x, cy, R, ground, 1.3, 18, { came: 0 });+ g.withClip(ringPts(x, cy, R, 14), () => g.withXf(0, x, cy, 1, () => SCENES[nm](g, R)));+ if (i < N - 1) { const yy = y0 + step * (i + 1); for (const s of [-1, 1]) { g.disc(x + s * w * 0.3, yy, w * 0.058, 'gold', 1.2, 8, { came: 0.012 }); g.disc(x + s * w * 0.3, yy, w * 0.032, 'ruby', 1.5, 6, { came: 0.008 }); } }+ });+ // Border bricks follow the outline: thin, and each brick painted with a stem of leaves+ const bw = w * 0.062, stepB = 0.34;+ const stripe = (x0, y1, y2) => { let k = 0; g.poly([[x0, y1], [x0 + bw, y1], [x0 + bw, y2], [x0, y2]], 'lead', 3.5, { came: 0, jitter: 0 }); for (let y = y1; y < y2; y += stepB) { const c = edge[k++ % edge.length]; g.poly([[x0, y], [x0 + bw, y], [x0 + bw, y + stepB], [x0, y + stepB]], c, 3.8, { came: -0.012, orn: c === 'gold' ? 'gold' : c === 'white' ? 'white' : null, box: [x0, y, x0 + bw, y + stepB] }); } };+ stripe(x - w / 2, y0, ys + rise * 0.1); stripe(x + w / 2 - bw, y0, ys + rise * 0.1);+ // Saddle bars (iron armature)+ for (let y = y0 + 1.1; y < ys + rise * 0.6; y += 1.1) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.045], [x - w / 2, y + 0.045]], 'lead', 7, { came: 0, jitter: 0 });+ });+}+// A lancet filled by one monumental standing figure under a painted canopy (reads from the floor)+function gLancetSaint(g, x, w, y0, ys, rise, fig, o = {}) {+ const arc = pointedArc(w, rise, 8).map((p) => [x + p[0], ys + p[1]]);+ const outline = [[x - w / 2, y0], [x + w / 2, y0], ...arc.slice().reverse()];+ const gnd = o.ground || 'cobalt';+ g.withClip(outline, () => {+ const figY = y0 + 1.05, figH = (ys + rise * 0.1 - figY) * 0.84;+ gDiaper(g, x - w / 2, y0, x + w / 2, ys + rise, 0.8, gnd, o.ground2 || 'ruby', null);+ // Pedestal: a band of painted gold the figure stands on+ g.poly([[x - w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 0.62], [x + w * 0.46, y0 + 1.02], [x - w * 0.46, y0 + 1.02]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.46, y0 + 0.62, x + w * 0.46, y0 + 1.02] });+ // Canopy: a thin arch of gold, a gable with its ruby field, amber pinnacles either side+ const cy = figY + figH + 0.02, cH = ys + rise * 0.86 - cy;+ g.poly([[x - w * 0.48, cy], [x + w * 0.48, cy], [x + w * 0.48, cy + cH * 0.1], [x - w * 0.48, cy + cH * 0.1]], 'gold', 1.6, { orn: 'gold', box: [x - w * 0.48, cy, x + w * 0.48, cy + cH * 0.1] });+ g.poly([[x - w * 0.42, cy + cH * 0.08], [x + w * 0.42, cy + cH * 0.08], [x, cy + cH]], 'yellow', 1.7, { orn: 'gold', box: [x - w * 0.42, cy + cH * 0.08, x + w * 0.42, cy + cH] });+ g.poly([[x - w * 0.22, cy + cH * 0.14], [x + w * 0.22, cy + cH * 0.14], [x, cy + cH * 0.62]], 'ruby', 1.9, { came: 0.016 });+ for (const s of [-1, 1]) g.poly([[x + s * w * 0.3, cy + cH * 0.12], [x + s * w * 0.46, cy + cH * 0.12], [x + s * w * 0.38, cy + cH * 0.85]], 'amber', 1.8);+ gFigure(g, x, figY, figH, Object.assign({ layer: 2.5, sx: Math.min(1, w * 1.72 / figH) }, fig));+ for (let y = y0 + 1.5; y < ys + rise * 0.6; y += 1.5) g.poly([[x - w / 2, y], [x + w / 2, y], [x + w / 2, y + 0.05], [x - w / 2, y + 0.05]], 'lead', 9, { came: 0, jitter: 0 });+ });+}+function gOculus(g, cx, cy, r, o = {}) { // head of the window: a sexfoil with an angel or Christ+ g.withClip(ringPts(cx, cy, r, 20), () => {+ g.disc(cx, cy, r, o.ground || 'cobalt', 1, 20);+ for (let i = 0; i < 6; i++) { const a = i * PI / 3 + PI / 6; g.disc(cx + r * 0.68 * Math.cos(a), cy + r * 0.68 * Math.sin(a), r * 0.27, i % 2 ? 'ruby' : 'gold', 1.4, 8); }+ g.disc(cx, cy, r * 0.56, o.inner || 'rubyLt', 1.8, 14);+ g.withXf(0, cx, cy, 1, () => { if (o.bust) gBust(g, 0, -r * 0.45, r * 0.95, o.bust); else gFigure(g, 0, -r * 0.5, r * 0.98, { wings: 'gold', robe: 'white', mantle: 'emerald', arm: 'orant' }); });+ });+}++// A complete two-light window: lancets, oculus, base field. Local plane z=0, +Z = interior.+function makeWindowGlass(w, y0, ys, rise, seed, fill) {+ const g = new Glass(seed);+ if (fill.trackPanes) g.b.panes = [];+ const outline = archOutline(w, y0, ys, rise, 8);+ g.raw(outline.slice(), 'lead', -0.4, 0);+ g.withClip(outline, () => gDiaper(g, -w / 2, ys, w / 2, ys + rise, 0.55, 'cobalt', 'ruby', 'gold', 0));+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96;+ const syl = ys + rise * 0.02;+ fill.light(g, -lx, lw, y0, syl, lr, 0); fill.light(g, lx, lw, y0, syl, lr, 1);+ const oc = windowHead(w, ys, rise);+ gOculus(g, 0, oc.y, oc.r + 0.03, fill.oculus || {});+ return g.b;+}+function windowHead(w, ys, rise) { const r = w * 0.225; return { r, y: ys + rise * 0.62 }; }++// ==================================================================================+// STONE TRACERY for a two-light window (plane z=0, bars deep along Z)+// ==================================================================================+function addTracery(b, w, y0, ys, rise, o = {}) {+ const bw = o.bar || 0.26, bd = o.depth || 0.42, sec = traceryBar(bw, bd), secT = hexBar(bw * 0.62, bd * 0.8);+ const lw = (w - 0.32) / 2, lx = lw / 2 + 0.08, lr = lw * 0.96, syl = ys + rise * 0.02;+ // Central mullion with shaft ring and capital+ b.at(M.T(0, 0, 0), () => {+ b.sweep([[0, y0 - 0.05], [0, syl]], sec, {});+ b.at(M.T(0, syl, 0), () => b.lathe([[bw * 0.52, -0.3], [bw * 0.56, -0.2], [bw * 0.9, 0.02], [bw * 0.5, 0.06]], 6));+ });+ for (const s of [-1, 1]) {+ const arc = pointedArc(lw, lr, 5).map((p) => [s * lx + p[0], syl + p[1]]);+ b.sweep(arc, sec, {});+ // cusped trefoil head inside each lancet+ const cr = lw * 0.3, cy = syl + lr * 0.34;+ const cusp = []; for (let i = 0; i <= 8; i++) { const a = rad(-30 + i * 240 / 8); const R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([s * lx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, { caps: false });+ b.sweep([[s * (lx + lw / 2), cy - cr * 0.4], cusp[0]], secT, { caps: false });+ b.sweep([[s * (lx - lw / 2), cy - cr * 0.4], cusp[8]], secT, { caps: false });+ }+ // Oculus with six foils+ const oc = windowHead(w, ys, rise);+ b.sweep(ringPts(0, oc.y, oc.r, 14), sec, { closed: true });+ const foils = []; for (let i = 0; i < 18; i++) { const a = i * TAU / 18; const R = oc.r * (0.62 + 0.3 * Math.pow(Math.abs(Math.cos(a * 3)), 0.8)); foils.push([R * Math.cos(a), oc.y + R * Math.sin(a)]); }+ b.sweep(foils, secT, { closed: true, crease: 40 });+}++// ==================================================================================+// PIERCED PARAPET with quatrefoil panels+// ==================================================================================+function quatrefoil(cx, cy, r, n = 16) { const p = []; for (let i = 0; i < n; i++) { const a = i * TAU / n; const R = r * (0.72 + 0.28 * Math.abs(Math.cos(2 * a))); p.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); } return p; }+function addParapet(b, x0, x1, y, h, th, o = {}) {+ const n = Math.max(1, Math.round((x1 - x0) / (o.pitch || 1.05))), holes = [];+ const r = Math.min(h * 0.34, (x1 - x0) / n * 0.4);+ for (let i = 0; i < n; i++) holes.push(quatrefoil(x0 + (i + 0.5) * (x1 - x0) / n, y + h * 0.5, r, o.nh || 12));+ b.slab([[x0, y], [x1, y], [x1, y + h], [x0, y + h]], holes, -th / 2, th / 2, { sr: 0.6 });+ const cap = barSection(0.14, th + 0.14);+ b.sweep([[x0, y + h + 0.04], [x1, y + h + 0.04]], cap, {});+ b.sweep([[x0, y - 0.02], [x1, y - 0.02]], barSection(0.1, th + 0.1), { s: 0.8 });+}++// A wimperg: gable over a window with crockets, finial and a blind trefoil+function addGable(b, w, h, z0, th, y, o = {}) {+ const lod = o.lod === undefined ? 1 : o.lod;+ const holes = o.solid ? [] : [ringPts(0, y + h * 0.36, Math.min(w * 0.13, h * 0.2), 14)];+ if (!o.noField) b.slab([[-w / 2, y], [w / 2, y], [0, y + h]], holes, z0, z0 + th, { sf: 1, sb: 0.8 });+ const rake = barSection(0.18, th + 0.14);+ b.sweep([[-w / 2 - 0.06, y - 0.02], [0, y + h + 0.05]], rake, { z: z0 + th / 2 });+ b.sweep([[0, y + h + 0.05], [w / 2 + 0.06, y - 0.02]], rake, { z: z0 + th / 2 });+ const n = o.crockets === undefined ? Math.max(2, Math.round(Math.hypot(w / 2, h) / 1.05)) : o.crockets, sz = o.size || 0.46;+ const zc = z0 + th / 2;+ crocketsAlong(b, [-w / 2, y + 0.05, zc], [-0.03, y + h * 0.97, zc], n, sz, [-h, w / 2, 0], lod);+ crocketsAlong(b, [w / 2, y + 0.05, zc], [0.03, y + h * 0.97, zc], n, sz, [h, w / 2, 0], lod);+ b.at(M.T(0, y + h * 0.98, zc), () => addFinial(b, o.finial || 0.95, lod));+}+// A small blind trefoil-headed arcade on a wall face (plane z=0, moulding stands proud +Z)+function addBlindArcade(b, x0, x1, y0, ys, n, o = {}) {+ const w = (x1 - x0) / n, sec = hexBar(o.bar || 0.12, o.depth || 0.16), rise = w * 0.95;+ for (let i = 0; i < n; i++) {+ const x = x0 + (i + 0.5) * w;+ b.sweep(pointedArc(w * 0.9, rise, 4).map((p) => [x + p[0], ys + p[1]]), sec, { z: 0.04, caps: false });+ }+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * w;+ b.at(M.T(x, 0, 0.06), () => b.lathe([[0.1, y0], [0.065, y0 + 0.18], [0.065, ys - 0.14], [0.13, ys], [0.08, ys + 0.04]], 5));+ }+}++// ==================================================================================+// RIBBED VAULT: pointed-arc ribs, curved webs, carved or gilded boss.+// ==================================================================================+function arcH(L, H, f) { // height of a two-centred arc of span L, rise H at fraction f+ const c = (H * H - L * L / 4) / L, r = c + L / 2, d = Math.abs(f - 0.5) * L;+ return Math.sqrt(Math.max(0, r * r - (d + c) * (d + c)));+}+function addRib(b, A, B, yS, rise, sec, n = 8, half = false) { // rib between plan points A,B in their vertical plane+ const dx = B[0] - A[0], dz = B[1] - A[1], L = Math.hypot(dx, dz), ang = Math.atan2(dz, dx) * 180 / PI;+ const pts = [];+ if (half) for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L * 2, rise, t / 2)]); }+ else for (let i = 0; i <= n; i++) { const t = i / n; pts.push([t * L, yS + arcH(L, rise, t)]); }+ // plane: local X along A->B, local Y up+ b.at(M.of(M.T(A[0], 0, A[1]), M.Ry(-ang)), () => b.sweep(pts, sec, { caps: false }));+}+// One curved web between a side arc a-b and the crown c, faces downward whichever way the plan runs+function addVaultCell(A, a, b, c, yS, yC, sideRiseV, nS = 8, nT = 6) {+ const La = Math.hypot(c[0] - a[0], c[1] - a[1]), Lb = Math.hypot(c[0] - b[0], c[1] - b[1]), Lab = Math.hypot(b[0] - a[0], b[1] - a[1]), rise = yC - yS;+ const cross = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);+ A.grid((s, t) => {+ const px = lerp(a[0], c[0], t) * (1 - s) + lerp(b[0], c[0], t) * s, pz = lerp(a[1], c[1], t) * (1 - s) + lerp(b[1], c[1], t) * s;+ const ruled = lerp(yS + arcH(Lab, sideRiseV, s), yC, t);+ const dA = arcH(La * 2, rise, t / 2) - rise * t, dB = arcH(Lb * 2, rise, t / 2) - rise * t;+ const y = ruled + (1 - s) * (1 - s) * dA + s * s * dB;+ return [px, y, pz, 0.72 + 0.28 * t];+ }, nS, nT, { flip: cross < 0, metric: true });+}+function addRibVault(F, A, corners, yS, yC, o = {}) {+ // corners: 4 plan points in order; web cells between each side and the crown+ const c0 = corners, cx = (c0[0][0] + c0[1][0] + c0[2][0] + c0[3][0]) / 4, cz = (c0[0][1] + c0[1][1] + c0[2][1] + c0[3][1]) / 4;+ const crown = [cx, yC, cz], nS = o.nS || 8, nT = o.nT || 6, rise = yC - yS;+ const sideRise = o.sideRise || ((i) => rise * 0.94);+ for (let k = 0; k < 4; k++) {+ if (o.skipSide && o.skipSide[k]) continue;+ addVaultCell(F, c0[k], c0[(k + 1) % 4], [cx, cz], yS, yC, sideRise(k), nS, nT);+ }+ const sec = o.ribSec || rollSection(0.32, 0.42, 2, 0.04, 2);+ for (const c of c0) addRib(F, c, [cx, cz], yS, rise, sec, 8, true);+ for (let k = 0; k < 4; k++) if (!(o.skipRib && o.skipRib[k])) addRib(F, c0[k], c0[(k + 1) % 4], yS, sideRiseRib(o, k, rise), o.sideRibSec || sec, 8);+ return crown;+}+const sideRiseRib = (o, k, rise) => (o.sideRise ? o.sideRise(k) : rise * 0.94);+function addBoss(G, F, c, r = 0.42) { // carved foliate boss with gilding+ G.at(M.T(c[0], c[1] - 0.02, c[2]), () => {+ G.lathe([[r * 0.25, -r * 0.54], [r * 0.6, -r * 0.5], [r * 0.95, -r * 0.3], [r, -r * 0.08], [r * 0.7, r * 0.05]], 10);+ for (let i = 0; i < 8; i++) addLeaf(G, M.of(M.T(0, -r * 0.22, 0), M.Ry(i * 45), M.T(r * 0.72, 0, 0), M.Rz(160)), r * 0.72);+ });+}++// ==================================================================================+// CLUSTERED PIER: a lobed section of shafts around a core, with bases and foliate capitals+// ==================================================================================+function lobedRing(core, shafts, n = 64) {+ const out = [];+ for (let i = 0; i < n; i++) {+ const a = TAU * i / n, c = Math.cos(a), s = Math.sin(a);+ let best = core, nx = c, nz = s;+ for (const sh of shafts) {+ // ray from the origin against each shaft circle+ const b = c * sh[0] + s * sh[1], disc = b * b - (sh[0] * sh[0] + sh[1] * sh[1] - sh[2] * sh[2]);+ if (disc >= 0) { const t = b + Math.sqrt(disc); if (t > best) { best = t; nx = (t * c - sh[0]) / sh[2]; nz = (t * s - sh[1]) / sh[2]; } }+ }+ out.push([best * c, best * s, nx, nz]);+ }+ return out;+}+function addExtrudedRing(b, ring, y0, y1, o = {}) {+ const s0 = o.s0 === undefined ? 1 : o.s0, s1 = o.s1 === undefined ? 1 : o.s1, k0 = o.k0 || 1, k1 = o.k1 || 1, N = ring.length;+ const base = b.count; let perim = 0;+ for (let i = 0; i <= N; i++) {+ const r = ring[i % N], rp = ring[(i + N - 1) % N];+ if (i > 0) perim += Math.hypot(r[0] - rp[0], r[1] - rp[1]);+ const shade = 0.5 + 0.5 * Math.pow(Math.abs(r[2] * r[0] + r[3] * r[1]) / (Math.hypot(r[0], r[1]) || 1), 2);+ b.v(r[0] * k0, y0, r[1] * k0, r[2], 0, r[3], perim, y0, s0 * shade);+ b.v(r[0] * k1, y1, r[1] * k1, r[2], 0, r[3], perim, y1, s1 * shade);+ }+ for (let i = 0; i < N; i++) b.quad(base + 2 * i, base + 2 * i + 1, base + 2 * i + 3, base + 2 * i + 2);+}+function addCapital(b, ring, y, h, k, o = {}) { // bell with crocket leaves on each shaft, moulded abacus+ const N = ring.length;+ addExtrudedRing(b, ring, y, y + h * 0.1, { k0: 1.0, k1: 1.03, s0: 0.8 });+ addExtrudedRing(b, ring, y + h * 0.1, y + h * 0.82, { k0: 1.03, k1: k, s0: 0.62, s1: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.82, y + h * 0.9, { k0: k, k1: k + 0.1, s0: 0.86 });+ addExtrudedRing(b, ring, y + h * 0.9, y + h, { k0: k + 0.1, k1: k + 0.12, s0: 0.95 });+ const top = ring.map((r) => [r[0] * (k + 0.12), r[1] * (k + 0.12)]);+ const ids = top.map((p) => b.v(p[0], y + h, p[1], 0, 1, 0, undefined, undefined, 0.9)); for (let i = 1; i < N - 1; i++) b.tri(ids[0], ids[i + 1], ids[i]);+ const leaves = o.leaves || 12;+ for (let i = 0; i < leaves; i++) {+ const a = i * 360 / leaves + (o.phase || 0), idx = Math.round(i / leaves * N) % N, r = ring[idx], R = Math.hypot(r[0], r[1]);+ addLeaf(b, M.of(M.T(0, y + h * 0.1, 0), M.Ry(-a), M.T(R * 1.02, 0, 0)), h * (i % 2 ? 0.72 : 0.56), { coarse: 1 });+ }+}++// The bay plan shared by every straight module (+Z side of the axis)+const zn = P.naveSpan / 2, zaw = zn + P.aisleSpan, BAY = P.bay;+// Window openings follow the bay, so changing the bay length re-proportions the glass and tracery+const WIN = {+ aW: BAY - 1.95, aSill: 4.3, aSp: P.pierTop - 1, aRise: 5.6 * (BAY - 1.95) / 4.65,+ cW: BAY - 1.55, cSill: P.triforiumTop + 0.45, cSp: P.vaultSpring + 4.8, cRise: 6.7 * (BAY - 1.55) / 5.05,+};+const windowArgs = { aisle: [WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise], clere: [WIN.cW, WIN.cSill, WIN.cSp, WIN.cRise] };+const mkBatches = () => { const D = new Batch(1); D.noColor = true; return { A: new Batch(TILE.ashlar), F: new Batch(TILE.fine), L: new Batch(TILE.lead), G: new Batch(TILE.gold), O: new Batch(TILE.oak), I: new Batch(TILE.iron), D }; };+// A stain hanging from a ledge on a wall facing local +z: runoff, drips, soft shadow or rising damp. Few vertices, crisp detail.+const STAIN_RNG = mkRng(4242);+function addStain(D, x0, x1, y, z, drop, kind = 'streak') {+ const R = DECAL_UV[kind], n = Math.max(1, Math.round((x1 - x0) / 4)), w = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = lerp(x0, x1, i / n), c = lerp(x0, x1, (i + 1) / n), span = w * Math.min(1, (c - a) / 5), u0 = R[0] + 0.002 + STAIN_RNG() * (w - span - 0.004);+ const q = [D.v(a, y, z, 0, 0, 1, u0, R[1], 1), D.v(a, y - drop, z, 0, 0, 1, u0, R[3], 1), D.v(c, y - drop, z, 0, 0, 1, u0 + span, R[3], 1), D.v(c, y, z, 0, 0, 1, u0 + span, R[1], 1)];+ D.quad(q[0], q[1], q[2], q[3]);+ }+}+// A real recess in a wall facing local +z, whose slab already has the opening cut: back plate, moulded frame, a sill+function addRecess(A, F, outline, depth) {+ const o = area2(outline) > 0 ? outline : outline.slice().reverse();+ let x0 = 1e9, x1 = -1e9, y0 = 1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); }+ A.face(o.map((p) => [p[0], p[1], -depth]));+ F.sweep(o, rollSection(0.1, 0.22, 1, 0.03, 2), { closed: true, crease: 40, z: -0.02 });+ F.sweep([[x0 - 0.16, y0 - 0.06], [x1 + 0.16, y0 - 0.06]], stringSection(0.16, 0.14), {});+}+// A real buttress on a wall facing local +z. stages, bottom to top: { y0, y1, w: half width, f: how far its front stands out }.+// Each stage narrows and steps back, and ends in a sloped stone cap running up to the next: a solid wedge of the pier, never+// a sheet. o.back ties it into the masonry behind; the last stage ends in a weathering back to the wall, or flat with o.flat.+function addPier(set, stages, o = {}) {+ const { A, F } = set, zb = -(o.back === undefined ? 0.3 : o.back);+ const face = (m, hw, y0, y1, p) => {+ const out = [[-hw, y0], [hw, y0], [hw, y1], [-hw, y1]];+ if (!p) { A.at(m, () => A.face(out.map((q) => [q[0], q[1], 0]))); return; }+ const hole = archOutline(hw * 2 * p.k, y0 + p.b, y1 - p.t - p.r, p.r, 5);+ A.at(m, () => { A.slab(out, [hole], -0.2, 0, { noOuterSides: true, noBack: true, sr: 0.66 }); F.at(m, () => addRecess(A, F, hole, 0.2)); });+ };+ stages.forEach((s, i) => {+ const n = stages[i + 1], mid = (s.f + zb) / 2, half = (s.f - zb) / 2;+ face(M.T(0, 0, s.f), s.w, s.y0, s.y1, s.panel);+ face(M.of(M.T(s.w, 0, mid), M.Ry(90)), half, s.y0, s.y1, s.sidePanel);+ face(M.of(M.T(-s.w, 0, mid), M.Ry(-90)), half, s.y0, s.y1, s.sidePanel);+ if (s.backFace) A.face([[s.w, s.y0, zb], [-s.w, s.y0, zb], [-s.w, s.y1, zb], [s.w, s.y1, zb]]);+ if (!s.noDrip) F.box(-s.w - 0.05, s.w + 0.05, s.y1 - 0.14, s.y1, zb, s.f + 0.05, { noTop: true });+ if (!n && o.flat) { F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [s.w, s.y1, zb], [-s.w, s.y1, zb]]); return; }+ const ny = n ? n.y0 : s.y1 + (o.capH || (s.f - zb) * 0.9), nw = n ? n.w : s.w, nf = n ? n.f : zb;+ F.face([[-s.w, s.y1, s.f], [s.w, s.y1, s.f], [nw, ny, nf], [-nw, ny, nf]]);+ F.face([[s.w, s.y1, s.f], [s.w, s.y1, zb], [nw, ny, zb], [nw, ny, nf]]);+ F.face([[-s.w, s.y1, zb], [-s.w, s.y1, s.f], [-nw, ny, nf], [-nw, ny, zb]]);+ });+}+function placeSet(name, set, pos, rot, parent, extra = {}) {+ const o = { pos, rot: [0, rot, 0], parent, scale: extra.scale };+ place(name + '_Ashlar', set.A, MAT.ashlar, o);+ place(name + '_Stone', set.F, MAT.fine, o);+ place(name + '_Lead', set.L, MAT.lead, o);+ place(name + '_Gilt', set.G, MAT.gold, o);+ place(name + '_Oak', set.O, MAT.oak, o);+ place(name + '_Iron', set.I, MAT.iron, o);+ if (set.D) place(name + '_Weathering', set.D, MAT.decal, o);+}+// Glass pair: interior face + the same panes mirrored as the exterior face+function placeGlass(name, batch, pos, rotY, parent, keepOutside, k = 1) {+ place(name + '_In', batch, keepOutside ? MAT.glassRose : MAT.glassIn, { pos, rot: [0, rotY, 0], scale: [k, k, 1], parent });+ place(name + '_Out', keepOutside ? batch : glassOutside(batch), keepOutside ? MAT.glassOut : MAT.glassOutWin, { pos, rot: [0, rotY, 0], scale: [k, k, -1], parent });+}+// By day a window is dark from outside and the story cannot be read, so its outer skin is only the+// window's outline over a mosaic of pot-metals: the colour hints through for a few triangles.+function glassOutside(batch) {+ if (batch.outer) return batch.outer;+ if (!batch.outline) return batch;+ const g = new Glass(7), o = batch.outline;+ let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of o) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); }+ g.raw(o, 'lead', -0.4, 0);+ g.withClip(o, () => gDiaper(g, x0, y0, x1, y1, 0, 'outer', null, null, 0));+ batch.outer = g.b;+ return g.b;+}++function buildNaveBay(o = {}) {+ const set = o.into || mkBatches(), { A, F, L, G } = set;+ const B = o.B || BAY, ww = o.ww || BAY - 2.1, sp = P.pierTop, archRise = o.archRise || 5.1, thick = 0.85;+ const zw = o.zw === undefined ? zn : o.zw;+ if (o.part !== 'aisle') {+ // ---- Arcade wall with the moulded pointed arch ----+ const arc = pointedArc(ww, archRise, 7).map((p) => [p[0], p[1] + sp]);+ A.at(M.T(0, 0, zw), () => {+ A.slab([[-B / 2, sp], [-ww / 2, sp], ...arc, [ww / 2, sp], [B / 2, sp], [B / 2, P.triforiumSill], [-B / 2, P.triforiumSill]], [], -thick / 2, thick / 2, { noOuterSides: true });+ });+ F.at(M.T(0, 0, zw), () => { F.sweep(arc, rollSection(0.82, thick + 0.25, 2, 0.08, 3), {}); });+ // Hood mould over the arch on the nave side, with carved label stops+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ const hood = pointedArc(ww + 1.9, archRise + 1.0, 6).map((p) => [p[0], p[1] + sp]);+ F.sweep(hood, stringSection(0.16, 0.14), {});+ });+ // ---- Triforium: string course, four cusped arches, dark wall passage ----+ const yT = P.triforiumSill, yT2 = P.triforiumTop, nT = o.nT || 4, tw = o.tw || (BAY - 1.72) / 4, ty = yT + 0.62, tspr = yT2 - 1.5, gapT = nT > 2 ? 0.08 : 0;+ const trfX = (i) => (i + 0.5 - nT / 2) * tw + (i < nT / 2 ? -gapT : gapT);+ F.at(M.of(M.T(0, 0, zw - thick / 2), M.Ry(180)), () => {+ F.sweep([[-B / 2, yT], [B / 2, yT]], stringSection(0.34, 0.3), {});+ F.sweep([[-B / 2, yT2], [B / 2, yT2]], stringSection(0.3, 0.26), {});+ // Colonnette arcade standing in the openings+ for (let i = 0; i < nT; i++) {+ const x = trfX(i);+ const a = pointedArc(tw * 0.92, 0.98, 4).map((p) => [x + p[0], tspr + p[1]]);+ F.sweep(a, hexBar(0.13, 0.26), { z: 0.05 });+ F.sweep(ringPts(x, tspr + 0.32, 0.17, 6), hexBar(0.05, 0.1), { z: 0.05, closed: true, s: 0.9 });+ }+ for (let i = 0; i <= nT; i++) {+ const mid = i === nT / 2;+ const x = (i - nT / 2) * tw + (i < nT / 2 ? -gapT : (mid ? 0 : gapT));+ const r = mid ? 0.14 : 0.085;+ F.at(M.T(x, 0, 0.05), () => F.lathe([[r * 1.7, ty - 0.02], [r, ty + 0.2], [r, tspr - 0.2], [r * 1.9, tspr - 0.01], [r * 1.5, tspr + 0.03]], 6));+ }+ });+ const holes = []; // openings through the inner skin+ for (let i = 0; i < nT; i++) { const x = trfX(i); holes.push(archOutline(tw * 0.92, ty, tspr, 0.98, 6).map((p) => [x + p[0], p[1]])); }+ A.at(M.T(0, 0, zw - thick / 2 + 0.22), () => A.slab([[-B / 2, yT], [B / 2, yT], [B / 2, yT2], [-B / 2, yT2]], holes, -0.22, 0.22, { noOuterSides: true, sf: 0.45, sr: 0.5 }));+ // Passage back wall and floor: seen only through the arcade, kept dark+ // Round the apse the passage fans out with the radius, so the walls of neighbouring bays meet at their corners+ const zBk = zw + 0.92, zFr = zw - thick / 2 + 0.44, hB = o.wedge ? (B / 2) * zBk / zw : B / 2, hF = o.wedge ? (B / 2) * zFr / zw : B / 2;+ A.shade(0.34, () => {+ A.face([[hB, yT, zBk], [-hB, yT, zBk], [-hB, yT2 + 0.5, zBk], [hB, yT2 + 0.5, zBk]]);+ A.face([[-hB, yT + 0.02, zBk], [hB, yT + 0.02, zBk], [hF, yT + 0.02, zFr], [-hF, yT + 0.02, zFr]]);+ A.face([[-hF, yT2 + 0.2, zFr], [hF, yT2 + 0.2, zFr], [hB, yT2 + 0.2, zBk], [-hB, yT2 + 0.2, zBk]]);+ });+ // ---- Clerestory wall, window hole, tracery ----+ const cw = o.cw || WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = o.cRise || WIN.cRise;+ const yE = P.eaves;+ A.at(M.T(0, 0, zw), () => A.slab([[-B / 2, yT2], [B / 2, yT2], [B / 2, yE], [-B / 2, yE]], [archOutline(cw, cSill, cSp, cRise, 9)], -0.42, 0.42, { noOuterSides: true, sr: 0.66 }));+ F.at(M.T(0, 0, zw), () => {+ if (o.lights === 1) addLancetTracery(F, cw, cSill, cSp, cRise);+ else addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.44 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 9).slice(0), rollSection(0.14, 0.86, 1, 0.03, 2), { closed: true, s: 0.92, crease: 40 });+ });+ // Exterior: hood mould, wimperg gable, cornice, pierced parapet, corbel table+ set.D.at(M.T(0, 0, zw + 0.42), () => { addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.02, 3.6); addStain(set.D, -B / 2, B / 2, yE - 0.66, 0.03, 0.9, 'soft'); });+ F.at(M.T(0, 0, zw + 0.42), () => {+ F.sweep(pointedArc(cw + 0.5, cRise + 0.3, 6).map((p) => [p[0], p[1] + cSp]), hexBar(0.22, 0.26), {});+ addGable(F, Math.min(cw + 0.9, B * 0.96), 3.4, 0.02, 0.2, cSp + cRise * 0.52, { size: 0.44, finial: 0.8, lod: 0, crockets: 3 });+ F.sweep([[-B / 2, yT2 - 0.02], [B / 2, yT2 - 0.02]], stringSection(0.3, 0.2), {});+ F.sweep([[-B / 2, yE - 0.12], [B / 2, yE - 0.12]], stringSection(0.34, 0.38), {});+ const nCorb = Math.round(B / 1.1);+ for (let i = 0; i < nCorb; i++) { const x = -B / 2 + (i + 0.5) * B / nCorb; F.box(x - 0.13, x + 0.13, yE - 0.62, yE - 0.32, 0, 0.26, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zw + 0.3), () => addParapet(F, -B / 2, B / 2, yE, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Upper wall top closing the gutter+ A.face([[-B / 2, yE, zw + 0.42], [B / 2, yE, zw + 0.42], [B / 2, yE, zw - 0.42], [-B / 2, yE, zw - 0.42]], 0.7);+ }+ if (o.part !== 'upper') {+ // ---- Aisle roof (lean-to in lead) ----+ if (!o.noAisleRoof) {+ const z0 = zn + 0.42, z1 = zaw + 1.05, y0r = P.aisleRoofHigh, y1r = P.aisleWallTop - 0.2;+ L.face([[-B / 2, y0r, z0], [-B / 2, y1r, z1], [B / 2, y1r, z1], [B / 2, y0r, z0]]);+ }+ if (!o.open) {+ // ---- Aisle wall ----+ const wa = WIN.aW, sill = WIN.aSill, aSp = WIN.aSp, aRise = WIN.aRise, zOut = zaw + 0.45;+ A.at(M.T(0, 0, zOut), () => A.slab([[-B / 2, 0], [B / 2, 0], [B / 2, P.aisleWallTop], [-B / 2, P.aisleWallTop]], [archOutline(wa, sill, aSp, aRise, 9)], -0.45, 0.45, { noOuterSides: true, sr: 0.7 }));+ F.at(M.T(0, 0, zOut), () => {+ addTracery(F, wa, sill, aSp, aRise, { bar: 0.25, depth: 0.42 });+ F.sweep(archOutline(wa, sill, aSp, aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ });+ // Exterior of the aisle wall+ set.D.at(M.T(0, 0, zaw + 0.9), () => {+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.02, 4.4);+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.02, 2.6, 'drip');+ addStain(set.D, -B / 2, B / 2, sill - 0.42, 0.03, 0.7, 'soft');+ addStain(set.D, -B / 2, B / 2, P.aisleWallTop - 0.42, 0.03, 0.9, 'soft');+ addStain(set.D, -B / 2, B / 2, 5.2, 0.03, 5.2, 'ground');+ });+ F.at(M.T(0, 0, zaw + 0.9), () => {+ F.sweep([[-B / 2, sill - 0.2], [B / 2, sill - 0.2]], stringSection(0.34, 0.3), {});+ F.sweep(pointedArc(wa + 0.46, aRise + 0.28, 6).map((p) => [p[0], p[1] + aSp]), hexBar(0.22, 0.26), {});+ addGable(F, wa + 0.8, 3.1, 0.02, 0.2, aSp + 2.95, { size: 0.44, finial: 0.8, crockets: 4 });+ F.sweep([[-B / 2, P.aisleWallTop - 0.12], [B / 2, P.aisleWallTop - 0.12]], stringSection(0.32, 0.34), {});+ // Plinth with a chamfered weathering+ F.sweep([[-B / 2, 0.55], [B / 2, 0.55]], stringSection(0.4, 0.42), {});+ F.sweep([[-B / 2, 1.3], [B / 2, 1.3]], stringSection(0.2, 0.2), {});+ addBlindArcade(F, -B / 2 + 0.9, B / 2 - 0.9, 1.45, 2.65, 4, { bar: 0.1, depth: 0.12 });+ });+ F.at(M.T(0, 0, zaw + 0.72), () => addParapet(F, -B / 2, B / 2, P.aisleWallTop, 1.0, 0.24, { pitch: 1.3 }));+ A.face([[-B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw + 0.9], [B / 2, P.aisleWallTop, zaw], [-B / 2, P.aisleWallTop, zaw]], 0.7);+ // Interior dado: blind arcade with a niche for a statue+ F.at(M.of(M.T(0, 0, zaw), M.Ry(180)), () => {+ F.sweep([[-B / 2, sill - 0.14], [B / 2, sill - 0.14]], stringSection(0.26, 0.22), {});+ addBlindArcade(F, -B / 2 + 0.5, -1.05, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ addBlindArcade(F, 1.05, B / 2 - 0.5, 0.6, 2.6, 2, { bar: 0.1, depth: 0.14 });+ F.sweep([[-B / 2, 0.42], [B / 2, 0.42]], stringSection(0.4, 0.34), {});+ // Niche canopy over the statue+ addNicheFrame(F, 0, 0.6, 3.9, 1.05);+ });+ }+ // ---- Aisle vault ----+ if (!o.noAisleVault) {+ const z0 = zn + thick / 2, z1 = zaw;+ const crown = addRibVault(F, A, [[-B / 2, z0], [B / 2, z0], [B / 2, z1], [-B / 2, z1]], sp, P.aisleCrown - 0.3, { sideRise: (k) => (k % 2 === 0 ? (k === 0 ? archRise + 0.1 : 5.0) : 5.2), nS: 6, nT: 4, ribSec: rollSection(0.26, 0.34, 2, 0.03, 2) });+ addBoss(G, F, crown, 0.32);+ }+ }+ return set;+}+// Canopied niche frame: moulded opening, gablet with crockets. Statue placed separately.+function addNicheFrame(F, x, y0, yTop, w) {+ F.at(M.T(x, 0, 0), () => {+ F.shade(0.5, () => F.face([[-w / 2, y0, -0.005], [w / 2, y0, -0.005], [w / 2, yTop - 0.75, -0.005], [0, yTop - 0.1, -0.005], [-w / 2, yTop - 0.75, -0.005]]));+ F.box(-w / 2 - 0.05, w / 2 + 0.05, y0 - 0.14, y0 + 0.08, 0, 0.5, { s: 0.92 });+ F.sweep(archOutline(w, y0, yTop - 0.82, 0.72, 5).slice(2), barSection(0.11, 0.2), { z: 0.08 });+ for (const s of [-1, 1]) { F.at(M.T(s * w / 2, 0, 0.1), () => F.lathe([[0.07, y0 + 0.05], [0.05, y0 + 0.12], [0.05, yTop - 0.9], [0.09, yTop - 0.82], [0.05, yTop - 0.78]], 6)); }+ addGable(F, w + 0.24, 0.92, 0.06, 0.1, yTop - 0.98, { size: 0.19, crockets: 2, finial: 0.32, solid: true, lod: 0 });+ });+}++// Clustered nave pier with the vault shafts rising to the springing, on +Z = nave... shafts toward -Z+function buildPier() {+ const set = mkBatches(), { F, A, G } = set;+ const core = 0.8, big = 0.25, small = 0.14;+ const shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.1) * Math.cos(a), (core + 0.1) * Math.sin(a), big]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; shafts.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 48);+ SHARED.pierRing = ring; // the plan of the shaft, which also carries the window light on its north-arcade faces+ const yB = 1.15, yC = P.pierTop - 1.05;+ // moulded base: octagonal plinth, roll, scotia, then the lobed shaft+ F.lathe([[1.7, 0], [1.7, 0.56], [1.58, 0.62]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.62, 0.86, { k0: 1.24, k1: 1.27 });+ addExtrudedRing(F, ring, 0.86, yB, { k0: 1.27, k1: 1.0, s0: 0.75, s1: 0.72 });+ addExtrudedRing(F, ring, yB, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.05, 1.2, { leaves: 14 });+ // The vault shafts rising from the capital up the nave wall (toward -Z)+ const upper = [[0, -(core + 0.1), big], [-0.5, -(core - 0.04), small], [0.5, -(core - 0.04), small]];+ for (const u of upper) {+ F.at(M.T(u[0], 0, u[1] + 0.44), () => {+ const r = u[2], yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, P.triforiumSill - 0.1], [r * 1.5, P.triforiumSill + 0.06], [r, P.triforiumSill + 0.22], [r, yS - 0.85], [r * 1.5, yS - 0.8], [r * 1.14, yS - 0.7], [r * 2.2, yS]], 8);+ for (let i = 0; i < 4; i++) addLeaf(F, M.of(M.T(0, yS - 0.72, 0), M.Ry(i * 90 + 45), M.T(r * 1.1, 0, 0)), 0.56, { coarse: 1 });+ });+ }+ // Statue stage on the nave face: corbel below, canopy above (the figure is placed separately)+ const zF = -(core + 0.34);+ F.at(M.T(0, 0, zF), () => {+ F.lathe([[0.1, 5.0], [0.28, 5.25], [0.44, 5.6], [0.5, 5.72], [0.44, 5.78]], 8, { s: 0.85 });+ for (let i = 0; i < 6; i++) addLeaf(F, M.of(M.T(0, 5.04, 0), M.Ry(i * 60), M.T(0.14, 0, 0)), 0.66, { coarse: 1 });+ // Canopy over the statue: three arched gablets and a little spire+ F.at(M.T(0, 8.25, 0), () => {+ F.lathe([[0.5, -0.02], [0.54, 0.06], [0.42, 0.12], [0.3, 0.6], [0.02, 2.0]], 6, { faceted: true, phase: PI / 6 });+ for (let i = 0; i < 3; i++) F.at(M.of(M.Ry(-60 + i * 60 + 180), M.T(0, 0, 0.44)), () => addGable(F, 0.62, 0.62, -0.03, 0.06, 0.02, { size: 0.15, crockets: 1, finial: 0.2, solid: true, lod: 0 }));+ });+ });+ return set;+}+// The hemicycle's pier: the nave pier's family, slimmer, with one vault shaft running up to the springing+function buildApsePier() {+ const set = mkBatches(), { F } = set, core = 0.5, big = 0.15, small = 0.09, shafts = [];+ for (let i = 0; i < 4; i++) { const a = i * PI / 2; shafts.push([(core + 0.06) * Math.cos(a), (core + 0.06) * Math.sin(a), big]); }+ for (let i = 0; i < 4; i++) { const a = PI / 4 + i * PI / 2; shafts.push([(core + 0.01) * Math.cos(a), (core + 0.01) * Math.sin(a), small]); }+ const ring = lobedRing(core, shafts, 32), yB = 0.86, yC = P.pierTop - 0.82;+ F.lathe([[1.02, 0], [1.02, 0.46], [0.94, 0.52]], 8, { faceted: true, phase: PI / 8 });+ addExtrudedRing(F, ring, 0.52, yB, { k0: 1.26, k1: 1.0 });+ addExtrudedRing(F, ring, yB, yC, {});+ addCapital(F, ring, yC, 0.82, 1.22, { leaves: 10 });+ F.at(M.T(0, 0, -(core + 0.06) + 0.24), () => {+ const r = big, yS = P.vaultSpring;+ F.lathe([[r * 1.5, P.pierTop], [r, P.pierTop + 0.25], [r, yS - 0.5], [r * 1.5, yS - 0.42], [r * 2.1, yS]], 8);+ });+ return set;+}+// Flying buttress unit at a bay boundary: massive pier, two arched flyers, tabernacle and pinnacles+const BUTTRESS = { hw: 0.98, zB: 19.2, zStack: 15.3, zT: 16.3 };+const flyerRise = (yA, yB, t) => yA + (yB - yA) * lerp(t, Math.sin(t * PI / 2), 0.6);+function flyerShape(zA, yA, zB, yB, thick) { // A at the pier (outer), B at the wall (inner); returns polygon points in (z,y)+ const N = 12, under = [], top = [];+ for (let i = 0; i <= N; i++) {+ const t = i / N, z = lerp(zA, zB, t), y = flyerRise(yA, yB, t);+ under.push([z, y]);+ }+ // The coping is a straight line clear above the arch: deep over the pier, close over the wall, the lights open between+ const topA = [zA - 0.05, yA + 2.5], topB = [zB, yB + 0.72];+ return { under, topA, topB };+}+function buildButtress() {+ const set = mkBatches(), { A, F, G } = set;+ const { hw, zB, zStack, zT } = BUTTRESS, z0 = zaw + 0.9, yStack = 30.6;+ // A solid pier tied into the aisle wall: three stages narrowing and stepping back under sloped caps, each with a real+ // recessed panel, then a slimmer stack at the rear that rises above the aisle roof to take the flyers+ const lower = { k: 0.56, b: 1.5, t: 1.6, r: 0.8 }, flank = { k: 0.56, b: 1.5, t: 1.5, r: 1.1 };+ A.at(M.T(0, 0, z0), () => F.at(M.T(0, 0, z0), () => addPier(set, [+ { y0: 0, y1: 8.4, w: hw, f: zB - z0, panel: lower, sidePanel: flank },+ { y0: 9.6, y1: 15.8, w: hw - 0.08, f: zB - z0 - 0.9, sidePanel: flank },+ { y0: 17, y1: 23.4, w: hw - 0.14, f: zT + 1 - z0, noDrip: true },+ { y0: 23.4, y1: yStack, w: hw - 0.24, f: zStack - z0, backFace: true },+ ], { capH: 1.1 })));+ // Moulded plinth around the base: a closed block the pier rises out of+ F.at(M.T(0, 0, z0), () => { F.box(-hw - 0.16, hw + 0.16, 0, 0.52, -0.2, zB - z0 + 0.16, { noBot: true, s: 0.9 }); F.box(-hw - 0.08, hw + 0.08, 0.52, 0.86, -0.2, zB - z0 + 0.08, { noBot: true, noTop: true, taper: 0.06, taperZ: 0.06 }); });+ set.D.at(M.T(0, 0, zB), () => { addStain(set.D, -hw, hw, 8.2, 0.02, 3.2); addStain(set.D, -hw, hw, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -hw, hw, 5, 0.03, 4.4, 'ground'); });+ // Rain and shadow down both flanks of the pier, below each cap+ for (const s of [-1, 1]) {+ const d1 = zB - z0, d2 = d1 - 0.9;+ set.D.at(M.of(M.T(s * hw, 0, z0 + d1 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.02, 3.2); addStain(set.D, -d1 / 2, d1 / 2, 8.26, 0.03, 0.8, 'soft'); addStain(set.D, -d1 / 2, d1 / 2, 5, 0.03, 4.8, 'ground'); });+ set.D.at(M.of(M.T(s * (hw - 0.08), 0, z0 + d2 / 2), M.Ry(s * 90)), () => { addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.02, 2.8); addStain(set.D, -d2 / 2, d2 / 2, 15.66, 0.03, 0.7, 'soft'); });+ }+ F.at(M.T(0, 0, zB), () => addGable(F, hw * 1.7, 1.4, 0.02, 0.14, 8.0, { size: 0.3, crockets: 2, finial: 0.5, solid: true, lod: 0 }));+ // Tabernacle of colonnettes at the top of the pier holding an angel+ const yT0 = 23.4, yT1 = 28.4;+ F.box(-hw + 0.1, hw - 0.1, yT0 - 0.02, yT0 + 0.1, zStack - 0.02, zT + 1, { s: 0.95 });+ for (const [sx, sz] of [[-1, -1], [1, -1], [-1, 1], [1, 1]]) F.at(M.T(sx * 0.72, 0, zT + sz * 0.72), () => F.lathe([[0.2, yT0], [0.14, yT0 + 0.12], [0.1, yT0 + 0.2], [0.1, yT1 - 0.22], [0.14, yT1 - 0.15], [0.22, yT1]], 8));+ // Canopy: arched, gabled and crowned by the crocketed pinnacle+ for (let i = 0; i < 4; i++) F.at(M.of(M.T(0, 0, zT), M.Ry(i * 90), M.T(0, 0, 0.72)), () => {+ F.sweep(pointedArc(1.3, 1.25, 6).map((p) => [p[0], yT1 - 1.25 + p[1] - 0.02]), traceryBar(0.1, 0.16), {});+ addGable(F, 1.7, 1.25, -0.06, 0.12, yT1, { size: 0.28, crockets: 2, finial: 0.48, lod: 0, solid: true });+ });+ F.box(-0.92, 0.92, yT1 - 0.06, yT1 + 0.18, zT - 0.92, zT + 0.92, { s: 0.9 });+ F.at(M.T(0, yT1 + 0.18, zT), () => addPinnacle(F, 1.42, 8.4, { lod: 1 }));+ // Two flyers, each a true arch: a moulded rib springing from the pier, a thin plate pierced by a row of+ // pointed lights standing on it, and a straight sloped coping with a gabled top shedding the rain+ const fly = (zA, yA, zB, yB, th) => {+ const { under, topA, topB } = flyerShape(zA, yA, zB, yB, th);+ const band = 0.34, upper = under.map((p) => [p[0], p[1] + band]);+ const onTop = (z) => lerp(topA[1], topB[1], (z - topA[0]) / (topB[0] - topA[0]));+ const onRib = (t) => flyerRise(yA, yB, t) + band;+ const holes = [], N = 6;+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.16) / N, t1 = (i + 0.84) / N, z0 = lerp(zA, zB, t0), z1 = lerp(zA, zB, t1), u0 = onRib(t0) + 0.04, u1 = onRib(t1) + 0.04, a0 = onTop(z0) - 0.1, a1 = onTop(z1) - 0.1, h = Math.min(a0 - u0, a1 - u1);+ if (h < 0.22) continue;+ const r = Math.min(h * 0.42, Math.abs(z1 - z0) * 0.8), hole = [[z0, u0], [z1, u1], [z1, a1 - r], [(z0 + z1) / 2, (a0 + a1) / 2], [z0, a0 - r]];+ holes.push(area2(hole) > 0 ? hole : hole.slice().reverse());+ }+ F.at(M.Ry(-90), () => {+ F.slab([...upper, topB, topA], holes, -0.12, 0.12, { sr: 0.7, so: 0.8 });+ F.slab([...under, ...upper.slice().reverse()], [], -0.27, 0.27, {});+ F.sweep(under, rollSection(0.12, 0.34, 1, 0.03, 2), { caps: false });+ });+ const ye = -0.03, yr = 0.17, we = 0.33, a = [topA[1], topA[0]], c = [topB[1], topB[0]];+ F.face([[-we, a[0] + ye, a[1]], [0, a[0] + yr, a[1]], [0, c[0] + yr, c[1]], [-we, c[0] + ye, c[1]]]);+ F.face([[0, a[0] + yr, a[1]], [we, a[0] + ye, a[1]], [we, c[0] + ye, c[1]], [0, c[0] + yr, c[1]]]);+ F.face([[-we, c[0] + ye, c[1]], [we, c[0] + ye, c[1]], [we, a[0] + ye, a[1]], [-we, a[0] + ye, a[1]]]);+ const n = Math.round(Math.hypot(topA[0] - topB[0], topA[1] - topB[1]) / 1.4);+ crocketsAlong(F, [0, topA[1] + 0.14, topA[0]], [0, topB[1] + 0.14, topB[0]], n, 0.5, [0, 1, 0], 0);+ };+ fly(zStack - 0.1, 28.0, zn + 1.12, 35.6, 0.48);+ fly(zStack - 0.1, 22.0, zn + 1.12, 28.6, 0.54);+ // Upper pier on the clerestory wall where the flyers land, with its own pinnacle above the parapet+ const yE = P.eaves;+ A.at(M.T(0, 0, zn + 0.42), () => F.at(M.T(0, 0, zn + 0.42), () => addPier(set, [{ y0: P.aisleRoofHigh - 1, y1: yE + 1.2, w: 0.62, f: 0.72, noDrip: true }], { back: 0, capH: 0.75 })));+ F.at(M.T(0, yE + 1.2, zn + 0.74), () => addPinnacle(F, 0.9, 6.4, { simple: 1 }));+ F.at(M.T(0, 0, zn + 1.14), () => { F.lathe([[0.16, 31.2], [0.1, 31.4], [0.1, 40.5], [0.17, 40.8]], 8); });+ // Interior respond on the aisle wall: a small cluster carrying the aisle vault+ const ringR = lobedRing(0.32, [[0, -0.44, 0.17], [-0.34, -0.26, 0.11], [0.34, -0.26, 0.11]], 26);+ F.at(M.T(0, 0, zaw), () => {+ addExtrudedRing(F, ringR, 0.4, P.pierTop - 0.8, { s0: 0.8 });+ addCapital(F, ringR, P.pierTop - 0.8, 0.8, 1.2, { leaves: 6 });+ F.lathe([[0.62, 0], [0.62, 0.42], [0.42, 0.5]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ });+ // Spout from the aisle gutter+ return set;+}++function addLancetTracery(b, w, y0, ys, rise) { // single-light frame with a cusped head+ const secT = traceryBar(0.13, 0.3), cr = w * 0.32, cy = ys + rise * 0.34, cusp = [];+ for (let i = 0; i <= 12; i++) { const a = rad(-30 + i * 240 / 12), R = cr * (0.86 + 0.22 * Math.abs(Math.sin(a * 1.5 + 0.4))); cusp.push([R * Math.cos(a), cy + R * Math.sin(a)]); }+ b.sweep(cusp, secT, {});+ b.sweep([[w / 2, cy - cr * 0.4], cusp[0]], secT, {});+ b.sweep([[-w / 2, cy - cr * 0.4], cusp[12]], secT, {});+}+function makeLancetGlass(w, y0, ys, rise, seed, fig, o) {+ const g = new Glass(seed);+ g.raw(archOutline(w, y0, ys, rise, 8), 'lead', -0.4, 0);+ gLancetSaint(g, 0, w, y0, ys, rise, fig, o || {});+ return g.b;+}++// ==================================================================================+// PLAN: everything is derived from the parameters at the top of the file.+// ==================================================================================+const LAY = (() => {+ const xW = P.xWest, xTowerE = xW - P.towerDepth, xNaveE = xTowerE - P.naveBays * BAY, xc = xNaveE - zn, xChoirW = xc - zn, xa = xChoirW - P.choirBays * BAY;+ const ridge = P.eaves + (zn + 1) * P.roofPitch;+ return { xW, xTowerE, xNaveE, xc, xChoirW, xa, ridge };+})();+// A rigid module frame: rotation about Y at an origin on the ground+const frame = (th, ox, oz) => {+ const c = Math.cos(rad(th)), s = Math.sin(rad(th));+ return { th, ox, oz, pos: (x, y, z) => [ox + x * c + z * s, y, oz - x * s + z * c], rot: (d = 0) => [0, th + d, 0] };+};+const SAINTS = ['apostle', 'peter', 'paul', 'bishop', 'prophet', 'martyr', 'paul', 'apostle', 'bishop', 'prophet'];++// Baked floor light: dim towards the walls and the corners++function buildVaultNave() {+ const s = mkBatches();+ const crown = addRibVault(s.F, s.A, [[-BAY / 2, -zn + 0.42], [BAY / 2, -zn + 0.42], [BAY / 2, zn - 0.42], [-BAY / 2, zn - 0.42]], P.vaultSpring, P.vaultCrown, { sideRise: (k) => (P.vaultCrown - P.vaultSpring) * (k % 2 === 0 ? 0.86 : 0.99), nS: 10, nT: 8 });+ addBoss(s.G, s.F, crown, 0.55);+ return s;+}++// ---- Chevet: hemicycle, ambulatory and a radiating chapel per sector ----------------+const CHEV = (() => {+ const d = 180 / P.apseSides, h = rad(d / 2), Ra = zn * Math.cos(h), hs = zn * Math.sin(h), Ro = zaw * Math.cos(h), hso = zaw * Math.sin(h);+ const Rc = 2.5, zc = Ro + 3.0;+ return { d, h, Ra, hs, Ro, hso, Rc, zc };+})();+function buildChevetSector() {+ const set = mkBatches(), { A, F, L, G } = set, { Ra, hs, Ro, hso, Rc, zc } = CHEV;+ buildNaveBay({ into: set, part: 'upper', B: 2 * hs, zw: Ra, wedge: true, ww: 1.62, archRise: 5.0, nT: 2, tw: 1.05, cw: 2.05, cRise: 4.3, lights: 1 });+ // Hemicycle vault: one web from this side up to the crown over the apse centre+ const yS = P.vaultSpring, yC = P.vaultCrown;+ addVaultCell(F, [-hs + 0.0, Ra - 0.42], [hs, Ra - 0.42], [0, 0], yS, yC, 9.6, 7, 8);+ const sec = rollSection(0.32, 0.42, 2, 0.04, 2);+ addRib(F, [-hs, Ra - 0.42], [hs, Ra - 0.42], yS, 9.6, sec, 6);+ addRib(F, [-zn * Math.sin(CHEV.h), (zn - 0.42) * Math.cos(CHEV.h)], [0, 0], yS, yC - yS, sec, 9, true);+ // Ambulatory vault (trapezoid)+ const c2 = addRibVault(F, A, [[-hs, Ra + 0.42], [hs, Ra + 0.42], [hso, Ro], [-hso, Ro]], P.pierTop, P.aisleCrown - 0.3, { sideRise: (k) => (k === 0 ? 5.1 : k === 2 ? 5.2 : 5.1), nS: 6, nT: 4, ribSec: rollSection(0.24, 0.32, 2, 0.03, 2) });+ addBoss(G, F, c2, 0.3);+ // Outer wall with the arched chapel mouth+ const yW = P.aisleWallTop, mw = 3.55, mS = 8.6, mR = 4.8;+ const mouth = pointedArc(mw, mR, 8).map((p) => [p[0], p[1] + mS]);+ A.at(M.T(0, 0, Ro + 0.3), () => A.slab([[-hso, 0], [-mw / 2, 0], [-mw / 2, mS], ...mouth, [mw / 2, mS], [mw / 2, 0], [hso, 0], [hso, yW], [-hso, yW]], [], -0.3, 0.3, { noOuterSides: true, sr: 0.7 }));+ // The moulding fills the whole wall thickness: it is also the soffit of the opening+ F.at(M.T(0, 0, Ro + 0.3), () => F.sweep(mouth, rollSection(0.42, 0.96, 2, 0.05, 2), {}));+ set.D.at(M.T(0, 0, Ro + 0.6), () => { addStain(set.D, -hso, hso, yW - 0.42, 0.02, 3.4); addStain(set.D, -hso, hso, yW - 0.42, 0.03, 0.8, 'soft'); });+ F.at(M.T(0, 0, Ro + 0.6), () => { F.sweep([[-hso, yW - 0.12], [hso, yW - 0.12]], stringSection(0.3, 0.3), {}); addParapet(F, -hso, hso, yW, 1.0, 0.24, { pitch: 1.1 }); });+ A.face([[-hso, yW, Ro + 0.6], [hso, yW, Ro + 0.6], [hso, yW, Ro], [-hso, yW, Ro]], 0.7);+ // Ambulatory lean-to roof+ const zR0 = Ra + 0.42, hsR0 = (zR0 / Ra) * hs, zR1 = Ro + 0.9, hsR1 = (zR1 / Ro) * hso;+ L.face([[-hsR0, P.aisleRoofHigh, zR0], [-hsR1, yW - 0.2, zR1], [hsR1, yW - 0.2, zR1], [hsR0, P.aisleRoofHigh, zR0]]);+ // Hipped roof over the hemicycle+ const yE = P.eaves + 0.15, Re = zn + 0.72, hRe = Re * Math.sin(CHEV.h), aRe = Re * Math.cos(CHEV.h);+ L.grid((u, v) => [lerp(-hRe, hRe, u) * (1 - v), lerp(yE, LAY.ridge, v), aRe * (1 - v)], 2, 2, { flip: false, metric: true });+ // Radiating chapel: five faceted walls, each with a tall lancet, rib vault, conical lead roof+ const nF = P.chapelFacets, span = 216, yC1 = 16.2, dF = span / nF, ap = Rc * Math.cos(rad(dF / 2)), halfF = Rc * Math.sin(rad(dF / 2)), lw = 1.3, sill = 3.2, lSp = 9.9, lR = 2.5;+ const verts = [];+ for (let i = 0; i <= nF; i++) { const f = rad(-span / 2 + i * dF); verts.push([Rc * Math.sin(f), zc + Rc * Math.cos(f)]); }+ for (let i = 0; i < nF; i++) {+ const fm = -span / 2 + (i + 0.5) * dF;+ const m = M.of(M.T(0, 0, zc), M.Ry(fm), M.T(0, 0, ap));+ A.at(m, () => A.slab([[-halfF, 0], [halfF, 0], [halfF, yC1], [-halfF, yC1]], [archOutline(lw, sill, lSp, lR, 7)], 0, 0.5, { noOuterSides: true, sr: 0.68 }));+ F.at(m, () => {+ addLancetTracery(F, lw, sill, lSp, lR);+ F.sweep(archOutline(lw, sill, lSp, lR, 5), hexBar(0.12, 0.56), { closed: true, z: 0.25, s: 0.9, crease: 40 });+ });+ // Weather on the chapel walls: streaks and shadow under the cornice and the sill, damp at the foot+ set.D.at(M.of(m, M.T(0, 0, 0.5)), () => {+ addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.02, 3); addStain(set.D, -halfF, halfF, yC1 - 0.24, 0.03, 0.7, 'soft');+ addStain(set.D, -halfF, halfF, sill - 0.3, 0.02, 2, 'drip'); addStain(set.D, -halfF, halfF, 3, 0.03, 3, 'ground');+ });+ F.at(M.of(m, M.T(0, 0, 0.5)), () => {+ F.sweep([[-halfF - 0.1, sill - 0.14], [halfF + 0.1, sill - 0.14]], stringSection(0.24, 0.2), { caps: false });+ F.sweep([[-halfF - 0.1, 0.5], [halfF + 0.1, 0.5]], stringSection(0.36, 0.3), { caps: false });+ F.sweep([[-halfF - 0.1, yC1 - 0.1], [halfF + 0.1, yC1 - 0.1]], stringSection(0.3, 0.3), { caps: false });+ addGable(F, lw + 0.9, 2.4, 0.01, 0.14, lSp + 2.15, { size: 0.32, crockets: 2, finial: 0.56, lod: 0 });+ addParapet(F, -halfF, halfF, yC1, 0.82, 0.18, { pitch: 1.3, nh: 8 });+ });+ addVaultCell(F, verts[i], verts[i + 1], [0, zc], 9.2, 14.6, 3.6, 5, 4);+ addRib(F, verts[i], verts[i + 1], 9.2, 3.6, rollSection(0.16, 0.22, 1, 0.02, 2), 5);+ }+ addVaultCell(F, verts[nF], verts[0], [0, zc], 9.2, 14.6, 4.2, 5, 4);+ for (let i = 0; i <= nF; i++) {+ addRib(F, verts[i], [0, zc], 9.2, 5.4, rollSection(0.17, 0.24, 1, 0.02, 2), 6, true);+ // Exterior angle buttress and little pinnacle; interior wall shaft+ const f = -span / 2 + i * dF, ve = verts[i];+ F.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => {+ A.at(M.of(M.T(ve[0], 0, ve[1]), M.Ry(f)), () => addPier(set, [{ y0: 0, y1: 8.8, w: 0.3, f: 1.05 }, { y0: 9.7, y1: 14.2, w: 0.25, f: 0.72, noDrip: true }], { back: 0.1, flat: true })); // F is already in this frame+ F.at(M.T(0, 14.18, 0.62), () => addPinnacle(F, 0.62, 3.9, { simple: 1 }));+ F.lathe([[0.17, 0], [0.09, 0.6], [0.09, 9.1], [0.17, 9.2]], 5);+ });+ }+ // The neck joining the chapel to the ambulatory: side walls, a pointed barrel vault, a lead cap+ const zN0 = Ro + 0.6, zN1 = verts[0][1], hN0 = mw / 2, hN1 = Math.abs(verts[0][0]);+ for (const s of [-1, 1]) A.prism([[s * hN0, zN0 - 0.05], [s * hN1, zN1 + 0.05], [s * (hN1 + 0.5), zN1 + 0.05], [s * (hN0 + 0.5), zN0 - 0.05]], 0, yC1);+ // Same subdivision as the chapel's last web so the seam has no cracks; a rib covers the joint+ A.grid((u, v) => { const hw = lerp(hN0, hN1, v); return [lerp(-hw, hw, u), 9.2 + arcH(2 * hw, 4.2, u), lerp(Ro + 0.3, zN1 + 0.02, v), 0.8]; }, 5, 1, { flip: false }); // starts under the arch moulding, so no ray can pass above it+ addRib(F, verts[nF], verts[0], 9.2, 4.2, rollSection(0.16, 0.26, 1, 0.02, 2), 5);+ L.face([[-(hN0 + 0.5), yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN1 + 0.1], [hN0 + 0.5, yC1 + 0.03, zN0 - 0.1], [-(hN0 + 0.5), yC1 + 0.03, zN0 - 0.1]]);+ // Lead cone+ L.at(M.T(0, 0, zc), () => { const n = 10, rb = Rc + 0.5; L.grid((u, v) => { const a = -PI + TAU * u, r = rb * (1 - v), y = lerp(yC1 + 0.05, yC1 + 7.4, v); return [r * Math.sin(a), y, r * Math.cos(a)]; }, n, 2, { flip: false, wrapU: true }); });+ G.at(M.T(0, yC1 + 7.3, zc), () => addFinial(G, 1.25, 1));+ return set;+}++// ---- Lead roofs with metric UVs so the seams run down the slope ----------------------+function addRoofRun(L, x0, x1, half, yE, yR) {+ const slope = Math.hypot(half, yR - yE);+ for (const s of [-1, 1]) {+ const a = L.v(x0, yE, s * half, 0, half, s * (yR - yE), x0, 0, 1), b = L.v(x1, yE, s * half, 0, half, s * (yR - yE), x1, 0, 1);+ const c = L.v(x1, yR, 0, 0, half, s * (yR - yE), x1, slope, 1), d = L.v(x0, yR, 0, 0, half, s * (yR - yE), x0, slope, 1);+ if (s > 0) L.quad(a, b, c, d); else L.quad(b, a, d, c);+ // A raised roll every second seam, running from the eaves to the ridge: a low ridge of two faces standing on the sheet+ const ny = half / slope, nz = s * (yR - yE) / slope, h = 0.08, w = 0.055;+ for (let x = Math.ceil(x0 / 2) * 2; x < x1; x += 2) {+ const A0 = [x - w, yE, s * half], B0 = [x + w, yE, s * half], A1 = [x - w, yR, 0], B1 = [x + w, yR, 0], T0 = [x, yE + ny * h, s * half + nz * h], T1 = [x, yR + ny * h, nz * h];+ if (s > 0) { L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]); } else { L.face([A1, T1, T0, A0]); L.face([T1, B1, B0, T0]); }+ }+ }+}+function buildCresting(len) { // gilded fleur-de-lis cresting for a length of ridge+ const G = new Batch(TILE.gold), n = Math.round(len / 0.9);+ G.box(-len / 2, len / 2, -0.04, 0.05, -0.05, 0.05, {});+ for (let i = 0; i < n; i++) {+ const x = -len / 2 + (i + 0.5) * len / n;+ G.at(M.T(x, 0.05, 0), () => {+ G.tube([[0, 0, 0], [0, 0.34, 0], [0, 0.62, 0]], (t) => [lerp(0.05, 0.005, t), lerp(0.035, 0.005, t)], 4);+ for (const s of [-1, 1]) G.tube(spline([[0, 0.14, 0], [s * 0.15, 0.26, 0], [s * 0.2, 0.42, 0], [s * 0.1, 0.46, 0]], 1), (t) => lerp(0.035, 0.008, t), 4);+ G.box(-0.08, 0.08, 0.12, 0.17, -0.03, 0.03, {});+ });+ }+ return G;+}++// ---- Crossing: four great piers, four arches, a gilded boss, the fleche above ----------+function buildCrossingPier() {+ const set = mkBatches(), { F } = set;+ const core = 1.08, sh = [];+ for (let i = 0; i < 8; i++) { const a = i * PI / 4; sh.push([(core + 0.12) * Math.cos(a), (core + 0.12) * Math.sin(a), i % 2 ? 0.2 : 0.3]); }+ for (let i = 0; i < 8; i++) { const a = PI / 8 + i * PI / 4; sh.push([(core + 0.02) * Math.cos(a), (core + 0.02) * Math.sin(a), 0.13]); }+ const ring = lobedRing(core, sh, 80), yC = P.vaultSpring - 1.1;+ F.lathe([[2.05, 0], [2.05, 0.6], [1.9, 0.68]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.68, 0.94, { k0: 1.2, k1: 1.23 });+ addExtrudedRing(F, ring, 0.94, 1.25, { k0: 1.23, k1: 1, s0: 0.75, s1: 0.75 });+ addExtrudedRing(F, ring, 1.25, yC, { s0: 0.8 });+ addCapital(F, ring, yC, 1.1, 1.14, { leaves: 16 });+ return set;+}+function buildAislePier() {+ const set = mkBatches(), { F } = set;+ const sh = []; for (let i = 0; i < 4; i++) { const a = i * PI / 2; sh.push([0.62 * Math.cos(a), 0.62 * Math.sin(a), 0.2]); }+ const ring = lobedRing(0.54, sh, 40);+ F.lathe([[1.15, 0], [1.15, 0.5], [1.02, 0.56]], 8, { faceted: true, phase: PI / 8, s: 0.85 });+ addExtrudedRing(F, ring, 0.56, 0.86, { k0: 1.28, k1: 1, s0: 0.75 });+ addExtrudedRing(F, ring, 0.86, P.pierTop - 0.9, { s0: 0.8 });+ addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });+ return set;+}+// The spire over the crossing: an oak frame sheathed in lead. A plinth straddles the roofs, two open lantern stages stand on it,+// and a long octagonal needle rises to a gilded orb and cross. Everything follows P.flecheTip, the ridge and the width of the crossing.+function buildFleche(parent, x, z) {+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold);+ L.noReduce = true; // a single careful mesh, its seams and crockets are the design+ const n = 8, PH = PI / 8, cos8 = Math.cos(PH), sin8 = Math.sin(PH), oct = { faceted: true, phase: PH };+ const yR = LAY.ridge, tip = P.flecheTip, y0 = yR - 5.9, yP = yR + 2.7, H = tip - yP, yA = yP + 0.19 * H, yB = yA + 0.15 * H, yN1 = yB + 1.7, yT = tip - 3.3;+ const R0 = zn * 0.73, R1 = zn * 0.66, R2 = zn * 0.5, RN = zn * 0.44;+ const faceFrame = (k, ap) => M.of(M.Ry(k * 45), M.T(0, 0, ap));+ const vertex = (k, R) => { const a = rad((k + 0.5) * 45); return [R * Math.sin(a), R * Math.cos(a)]; };+ // A small gilded knop finishing a gablet or a lucarne+ const knop = (m, s) => G.at(m, () => G.lathe([[0.05 * s, 0], [0.11 * s, 0.1 * s], [0.09 * s, 0.2 * s], [0.03 * s, 0.32 * s], [0.004, 0.72 * s]], 6));+ const gablet = (k, ap, w, h, y, nC, sz) => {+ const fr = faceFrame(k, ap + 0.05);+ L.at(fr, () => {+ L.slab([[-w / 2, y], [w / 2, y], [0, y + h]], [], -0.05, 0.05, { sb: 0.7 });+ L.shade(0.42, () => L.face([[-w * 0.2, y + h * 0.16, 0.06], [w * 0.2, y + h * 0.16, 0.06], [0, y + h * 0.62, 0.06]]));+ const rake = barSection(0.14, 0.2);+ L.sweep([[-w / 2 - 0.05, y - 0.02], [0, y + h + 0.04]], rake, {}); L.sweep([[0, y + h + 0.04], [w / 2 + 0.05, y - 0.02]], rake, {});+ crocketsAlong(L, [-w / 2, y + 0.05, 0], [-0.03, y + h * 0.97, 0], nC, sz, [-h, w / 2, 0], 0, addLeadCrocket);+ crocketsAlong(L, [w / 2, y + 0.05, 0], [0.03, y + h * 0.97, 0], nC, sz, [h, w / 2, 0], 0, addLeadCrocket);+ });+ knop(M.of(fr, M.T(0, y + h - 0.02, 0)), 0.9);+ };+ // One open stage: a dark core, a pointed opening in every face, a clustered post at every angle, a cornice closing the top+ const stage = (R, yBase, yTop, coreR, rPost, open) => {+ const hs = R * sin8, ap = R * cos8, { w, sill, ys, rise, tracery } = open;+ L.shade(0.34, () => L.lathe([[coreR, yBase], [coreR, yTop - 0.4]], n, oct));+ L.shade(0.5, () => { L.lathe([[coreR, yTop - 0.4], [R, yTop - 0.4]], n, oct); L.lathe([[R, yBase + 0.01], [coreR, yBase + 0.01]], n, oct); });+ for (let k = 0; k < n; k++) {+ L.at(faceFrame(k, ap), () => {+ L.slab([[-hs, yBase], [hs, yBase], [hs, yTop], [-hs, yTop]], [archOutline(w, sill, ys, rise, 5)], -0.14, 0.02, { noOuterSides: true, sr: 0.6, sb: 0.5 });+ L.sweep(pointedArc(w + 0.16, rise + 0.16, 5).map((p) => [p[0], p[1] + ys]), hexBar(0.1, 0.14), { z: 0.03, caps: false });+ if (tracery) {+ L.at(M.T(0, 0, -0.06), () => L.lathe([[0.1, sill], [0.065, sill + 0.16], [0.065, ys - 0.08], [0.11, ys]], 5));+ for (const s of [-1, 1]) L.sweep(pointedArc(w / 2 - 0.03, rise * 0.58, 3).map((p) => [s * w / 4 + p[0], ys + p[1]]), hexBar(0.08, 0.13), { z: -0.06, caps: false });+ L.sweep(ringPts(0, ys + rise * 0.76, rise * 0.13, 6), hexBar(0.06, 0.1), { z: -0.06, closed: true });+ }+ });+ const [px, pz] = vertex(k, R + 0.03);+ L.at(M.T(px, 0, pz), () => L.lathe([[rPost * 1.35, yBase], [rPost, yBase + 0.22], [rPost, yTop - 0.3], [rPost * 1.35, yTop - 0.12], [rPost * 1.2, yTop]], 6));+ }+ L.lathe([[R + 0.02, yTop - 0.5], [R + 0.22, yTop - 0.3], [R + 0.22, yTop - 0.12], [R + 0.08, yTop]], n, oct);+ };+ // A raised roll standing on a sloping face, in the face's own frame: two faces, a low ridge+ const roll = (u, ya, yb, apF, h = 0.07, w = 0.05) => {+ const za = apF(ya), zb = apF(yb), dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz) || 1, ny = -dz / l, nz = dy / l;+ const A0 = [u - w, ya, za], B0 = [u + w, ya, za], A1 = [u - w, yb, zb], B1 = [u + w, yb, zb], T0 = [u, ya + ny * h, za + nz * h], T1 = [u, yb + ny * h, zb + nz * h];+ L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]);+ };++ // ---- The plinth: batters up out of the slates of all four roofs, its foot well below their surface all round ----+ L.lathe([[R0, y0], [R0 - 0.16, yP - 0.62], [R0 + 0.12, yP - 0.46], [R0 + 0.12, yP - 0.2], [R0 - 0.2, yP], [R1 * 0.6, yP]], n, oct);+ { const apF = (y) => lerp(R0, R0 - 0.16, (y - y0) / (yP - 0.62 - y0)) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => { for (const u of [-1.2, 0, 1.2]) roll(u, y0, yP - 0.62, apF); }); }++ // ---- First stage: tall two-light openings, a gablet on every face, a pinnacle at every angle, a lead pentice behind them ----+ stage(R1, yP, yA, R1 * 0.6, 0.26, { w: R1 * 0.56, sill: yP + 0.95, ys: yA - 3.35, rise: 2, tracery: true });+ L.lathe([[R1 + 0.08, yA], [R2 + 0.02, yA + 1.55]], n, oct);+ for (let k = 0; k < n; k++) {+ gablet(k, R1 * cos8, R1 * 0.67, 2.6, yA - 0.3, 2, 0.34);+ const [px, pz] = vertex(k, R1 + 0.02);+ L.at(M.of(M.T(px, yA - 0.2, pz), M.Ry((k + 0.5) * 45)), () => addPinnacle(L, 0.62, 5, { simple: true, lod: 0 }));+ }+ // ---- Second stage: slimmer, single lancets ----+ stage(R2, yA + 1.5, yB, R2 * 0.62, 0.18, { w: R2 * 0.53, sill: yA + 2.45, ys: yB - 2.3, rise: 1.5, tracery: false });+ for (let k = 0; k < n; k++) gablet(k, R2 * cos8, R2 * 0.65, 1.9, yB - 0.25, 1, 0.3);++ // ---- The needle: a flared foot, then eight long faces. u stays metric, so the sheets run straight up and are cut by the arrises as they close ----+ const rTip = 0.09, RF = R2 + 0.08, rAt = (y) => (y <= yN1 ? lerp(RF, RN, (y - yB) / (yN1 - yB)) : lerp(RN, rTip, (y - yN1) / (yT - yN1)));+ const levels = [yB, yN1]; for (const t of [0.22, 0.5, 0.75, 1]) levels.push(lerp(yN1, yT, t));+ const apAt = (y) => rAt(y) * cos8;+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => {+ let vv = 0;+ for (let j = 0; j < levels.length - 1; j++) {+ const ya = levels[j], yb = levels[j + 1], za = apAt(ya), zb = apAt(yb), wa = rAt(ya) * sin8, wb = rAt(yb) * sin8, dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz), ny = -dz / l, nz = dy / l;+ const a = L.v(-wa, ya, za, 0, ny, nz, -wa, vv), b = L.v(wa, ya, za, 0, ny, nz, wa, vv), c = L.v(wb, yb, zb, 0, ny, nz, wb, vv + l), d = L.v(-wb, yb, zb, 0, ny, nz, -wb, vv + l);+ L.quad(a, b, c, d); vv += l;+ }+ // Standing seams, each stopping where the closing arrises overtake it+ for (const u of [-0.55, 0, 0.55]) { const rEnd = (Math.abs(u) + 0.1) / sin8; if (rEnd < RN) roll(u, yN1, lerp(yN1, yT, (RN - rEnd) / (RN - rTip)), apAt); }+ });+ // Rolled arrises from the flare to the tip, crockets thinning as they climb+ for (let k = 0; k < n; k++) {+ const vAt = (y) => { const [vx, vz] = vertex(k, rAt(y)); return [vx, y, vz]; };+ L.tube([vAt(yB), vAt(yN1), vAt(yT - 0.05)], (t) => lerp(0.12, 0.04, t), 4, { capEnd: false });+ const [ox, oz] = vertex(k, 1), nC = 11, y1 = yN1 + 1.2, y2 = yT - 1.5;+ for (let i = 0; i < nC; i++) {+ const t = (i + 0.5) / nC, ya = lerp(y1, y2, t - 0.02), yb = lerp(y1, y2, t + 0.02);+ crocketsAlong(L, vAt(ya), vAt(yb), 1, lerp(1.2, 0.42, t), [ox, 0.12, oz], 0, addLeadCrocket);+ }+ }+ // Tie-bands round the needle where the sheets are lapped+ for (const t of [0.22, 0.5, 0.75]) { const y = lerp(yN1, yT, t), r = rAt(y); L.lathe([[rAt(y - 0.1), y - 0.1], [r + 0.08, y - 0.05], [r + 0.08, y + 0.05], [rAt(y + 0.12), y + 0.12]], n, oct); }+ // Two tiers of lucarnes on alternate faces+ const lucarne = (k, yD, a, c, g) => {+ const zf = apAt(yD) + 0.06, yE2 = yD + c, yG = yE2 + g, zb0 = apAt(yD), zbE = apAt(yE2), zbG = apAt(yG);+ L.at(faceFrame(k, 0), () => {+ L.face([[-a, yD, zf], [a, yD, zf], [a, yE2, zf], [0, yG, zf], [-a, yE2, zf]]);+ L.shade(0.22, () => L.face([[-a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yE2, zf + 0.012], [0, yE2 + g * 0.55, zf + 0.012], [-a * 0.5, yE2, zf + 0.012]]));+ L.face([[a, yD, zb0], [a, yE2, zbE], [a, yE2, zf], [a, yD, zf]]);+ L.face([[-a, yD, zb0], [-a, yD, zf], [-a, yE2, zf], [-a, yE2, zbE]]);+ L.face([[a, yE2, zf], [a, yE2, zbE], [0, yG, zbG], [0, yG, zf]]);+ L.face([[-a, yE2, zf], [0, yG, zf], [0, yG, zbG], [-a, yE2, zbE]]);+ });+ knop(M.of(faceFrame(k, zf), M.T(0, yG - 0.02, 0)), 0.8);+ };+ for (let k = 0; k < n; k++) { if (k % 2 === 0) lucarne(k, yN1 + 5, 0.5, 1.25, 0.75); else lucarne(k, yN1 + 12, 0.36, 0.95, 0.55); }++ // ---- The tip: a gilded collar, an orb and a cross, its head at exactly P.flecheTip ----+ G.at(M.T(0, yT - 0.25, 0), () => {+ G.lathe([[0.12, 0], [0.24, 0.12], [0.22, 0.22], [0.13, 0.3], [0.11, 0.44], [0.2, 0.52], [0.36, 0.68], [0.42, 0.9], [0.36, 1.12], [0.2, 1.28], [0.08, 1.36], [0.06, 2.2]], 10);+ G.box(-0.07, 0.07, 2.2, 3.55, -0.07, 0.07, {});+ G.box(-0.62, 0.62, 2.82, 2.98, -0.07, 0.07, {});+ });+ place('CrossingSpire_Lead', L, MAT.lead, { pos: [x, 0, z], parent });+ place('CrossingSpire_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });+}++function storyWindows() { // the narrative aisle windows+ const sets = [+ [['annunciation', 'nativity', 'magi', 'shepherd', 'supper'], ['crucifixion', 'resurrection', 'david', 'noah', 'annunciation']],+ [['noah', 'david', 'annunciation', 'nativity', 'magi'], ['shepherd', 'supper', 'crucifixion', 'resurrection', 'david']],+ [['magi', 'supper', 'noah', 'crucifixion', 'shepherd'], ['nativity', 'david', 'resurrection', 'annunciation', 'supper']],+ [['resurrection', 'shepherd', 'crucifixion', 'david', 'nativity'], ['supper', 'annunciation', 'magi', 'noah', 'resurrection']],+ ];+ const grounds = [['ruby', 'cobalt'], ['cobalt', 'ruby'], ['ruby', 'cobalt'], ['cobalt', 'ruby']];+ return sets.slice(0, P.storyWindows).map((s, n) => makeWindowGlass(...windowArgs.aisle, 40 + n, {+ trackPanes: true,+ light: (g, x, w, y0, ys, r, i) => gLancetStory(g, x, w, y0, ys, r, s[i].slice(0, 4), { c1: grounds[n][0], c2: grounds[n][1], g1: grounds[(n + 1) % 4][1], g2: grounds[n][0], cell: 0.86 }),+ oculus: { ground: grounds[n][1], inner: grounds[n][0] },+ }));+}+const CLERE_SAINTS = [+ [{ robe: 'white', mantle: 'ruby', beard: 1, attr: 'book' }, { robe: 'emerald', mantle: 'gold', crown: 1, beard: 2, attr: 'cross', arm: 'point', halo: false }],+ [{ robe: 'pale', mantle: 'cobalt', veil: 'azure', arm: 'pray' }, { robe: 'violet', mantle: 'white', mitre: 'white', attr: 'staff', arm: 'up' }],+ [{ robe: 'gold', mantle: 'emerald', beard: 2, hair: 'grey', beardCol: 'pearl', attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'purple', beard: 1, attr: 'sword', arm: 'point' }],+ [{ robe: 'rubyLt', mantle: 'teal', crown: 1, attr: 'lily', arm: 'point', halo: false }, { robe: 'white', mantle: 'rubyLt', arm: 'orant', wings: 'gold' }],+];+function clereWindows() {+ return CLERE_SAINTS.slice(0, P.clerestoryWindows).map((pair, n) => makeWindowGlass(...windowArgs.clere, 60 + n, {+ light: (g, x, w, y0, ys, r, i) => gLancetSaint(g, x, w, y0, ys, r, pair[i], (i + n) % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }),+ oculus: { bust: { robe: 'rubyLt', beard: 1, cross: 1 }, ground: 'cobalt', inner: 'gold' },+ }));+}++function buildBody() {+ const { xTowerE, xNaveE, xc, xChoirW, xa, ridge } = LAY;+ const gNave = group('Nave'), gChoir = group('Choir'), gTN = group('Transept_North'), gTS = group('Transept_South'), gX = group('Crossing'), gApse = group('Apse_Chevet'), gRoof = group('Roofs');+ // The upper elevation is shared by every straight bay; aisles come in full and open-corner versions+ const bayUp = buildNaveBay({ part: 'upper' }), aisleFull = buildNaveBay({ part: 'aisle' }), aisleOpen = buildNaveBay({ part: 'aisle', open: true });+ const vault = buildVaultNave(), pier = buildPier(), but = buildButtress();+ pier.F.pierShaft = true; // the window light tests these exactly rather than voxel by voxel, so their shadows come out clean+ const stories = storyWindows(), cleres = clereWindows();+ SHARED.stories = stories; SHARED.cleres = cleres;+ let counter = 0;+ const statue = (fr, lx, y, lz, kind, faceDeg, scale, parent, indoor = true) => placeFigure('Statue_' + kind + '_' + (counter++), kind, { pos: fr.pos(lx, y, lz), rot: fr.rot(faceDeg), scale, parent, indoor, lod: 0 });+ const garg = (fr, lx, y, lz, k, sc, parent) => place('Gargoyle_' + (counter++), gargoyle(k), MAT.fine, { pos: fr.pos(lx, y, lz), rot: fr.rot(-90), scale: [sc, sc, sc], parent });++ // One side of a straight bay: module, vault-side glass, statues, gargoyles+ const placeBay = (name, fr, parent, o = {}) => {+ placeSet(name + '_Upper', bayUp, [fr.ox, 0, fr.oz], fr.th, parent);+ if (!o.noVault) placeSet(name + '_Aisle', o.open ? aisleOpen : aisleFull, [fr.ox, 0, fr.oz], fr.th, parent);+ const c = cleres[(o.i || 0) % cleres.length];+ placeGlass(name + '_Clerestory', c, fr.pos(0, 0, zn), fr.th + 180, parent);+ if (!o.open) {+ placeGlass(name + '_AisleWindow', stories[(o.i || 0) % stories.length], fr.pos(0, 0, zaw + 0.45), fr.th + 180, parent);+ if (o.sunLit) SUN_WINDOWS.push({ x: fr.ox, batch: stories[(o.i || 0) % stories.length] });+ if ((o.i || 0) % 2 === 0) statue(fr, 0, 0.7, zaw - 0.3, SAINTS[(o.i || 0) % SAINTS.length], 90, 0.86, parent);+ if (!o.inTower) garg(fr, 0, 22.5, zaw + 1.05, (o.i || 0) % 3, 0.72, parent);+ }+ if (!o.noUpperGarg) garg(fr, 0, P.eaves - 0.2, zn + 0.62, ((o.i || 0) + 1) % 3, 0.66, parent);+ };+ const placeButtress = (name, fr, parent, i) => {+ placeSet(name, but, [fr.ox, 0, fr.oz], fr.th, parent);+ statue(fr, 0, 23.5, BUTTRESS.zT, 'angel', -90, 1.9, parent, false);+ garg(fr, 0, 15.2, BUTTRESS.zB - 1.05, i % 3, 1.0, parent);+ };+ const placePier = (name, lfr, parent, kind) => {+ placeSet(name, pier, [lfr.ox, 0, lfr.oz], lfr.th, parent);+ if (kind) statue(lfr, 0, 5.78, -1.14, kind, 90, 1.15, parent);+ };++ // ---- Two more bays carry the nave between the towers to the inner face of the west wall ----+ for (let t = 0; t < 2; t++) {+ const x = xTowerE + (t + 0.5) * BAY;+ placeBay('TowerBay' + t + '_N', frame(0, x, 0), gNave, { i: t + 2, noUpperGarg: true, inTower: true });+ placeBay('TowerBay' + t + '_S', frame(180, x, 0), gNave, { i: t, noUpperGarg: true, inTower: true });+ placeSet('TowerVault' + t, vault, [x, 0, 0], 0, gNave);+ if (t === 0) { placePier('TowerPier_N', frame(0, x + BAY / 2, zn), gNave, SAINTS[6]); placePier('TowerPier_S', frame(180, x + BAY / 2, -zn), gNave, SAINTS[7]); }+ }+ // ---- Nave ----+ for (let i = 0; i < P.naveBays; i++) {+ const x = xTowerE - (i + 0.5) * BAY, isOpen = i === P.naveBays - 1;+ placeBay('NaveBay' + i + '_N', frame(0, x, 0), gNave, { i, open: isOpen });+ placeBay('NaveBay' + i + '_S', frame(180, x, 0), gNave, { i: i + 2, open: isOpen, sunLit: true });+ placeSet('NaveVault' + i, vault, [x, 0, 0], 0, gNave);+ }+ for (let k = 0; k <= P.naveBays - 1; k++) {+ const x = xTowerE - k * BAY;+ placePier('NavePier' + k + '_N', frame(0, x, zn), gNave, k ? SAINTS[k % SAINTS.length] : null);+ placePier('NavePier' + k + '_S', frame(180, x, -zn), gNave, k ? SAINTS[(k + 3) % SAINTS.length] : null);+ if (k >= 1 && k < P.naveBays - 1) { placeButtress('NaveButtress' + k + '_N', frame(0, x, 0), gNave, k); placeButtress('NaveButtress' + k + '_S', frame(180, x, 0), gNave, k + 1); }+ }+ // ---- Choir ----+ for (let j = 0; j < P.choirBays; j++) {+ const x = xChoirW - (j + 0.5) * BAY, isOpen = j === 0;+ placeBay('ChoirBay' + j + '_N', frame(0, x, 0), gChoir, { i: j + 1, open: isOpen });+ placeBay('ChoirBay' + j + '_S', frame(180, x, 0), gChoir, { i: j + 3, open: isOpen });+ placeSet('ChoirVault' + j, vault, [x, 0, 0], 0, gChoir);+ }+ for (let k = 1; k <= P.choirBays; k++) {+ const x = xChoirW - k * BAY;+ placePier('ChoirPier' + k + '_N', frame(0, x, zn), gChoir, SAINTS[(k + 5) % SAINTS.length]);+ placePier('ChoirPier' + k + '_S', frame(180, x, -zn), gChoir, SAINTS[(k + 1) % SAINTS.length]);+ placeButtress('ChoirButtress' + k + '_N', frame(0, x, 0), gChoir, k);+ placeButtress('ChoirButtress' + k + '_S', frame(180, x, 0), gChoir, k + 1);+ }+ // ---- Transept arms: the same bay, turned through 90 degrees ----+ for (const s of [1, -1]) {+ const gT = s > 0 ? gTN : gTS, tag = s > 0 ? 'N' : 'S';+ for (let q = 0; q < P.transeptBays + 1; q++) {+ const zc = s * (zn + (q + 0.5) * BAY), isOpen = q === 0;+ placeBay('Transept' + tag + q + '_W', frame(90 * s, xc, zc), gT, { i: q + 1, open: isOpen, noVault: isOpen });+ placeBay('Transept' + tag + q + '_E', frame(-90 * s, xc, zc), gT, { i: q + 3, open: isOpen, noVault: isOpen });+ placeSet('TranseptVault' + tag + q, vault, [xc, 0, zc], 90, gT);+ }+ for (let q = 1; q <= P.transeptBays; q++) { // the last bay ends in the front wall, which needs no free pier+ const zz = s * (zn + q * BAY);+ placePier('TranseptPier' + tag + q + '_W', frame(90, xc + zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 2) % SAINTS.length] : null);+ placePier('TranseptPier' + tag + q + '_E', frame(-90, xc - zn, zz), gT, q <= P.transeptBays ? SAINTS[(q + 4) % SAINTS.length] : null);+ if (q === P.transeptBays) { placeButtress('TranseptButtress' + tag + '_W', frame(90 * s, xc, zz), gT, q); placeButtress('TranseptButtress' + tag + '_E', frame(-90 * s, xc, zz), gT, q + 1); }+ }+ }+ // ---- Crossing ----+ const xPier = buildCrossingPier(), aPier = buildAislePier(), xVault = mkBatches();+ xPier.F.pierShaft = true;+ const sp = zn - 0.3;+ const crossCrown = addRibVault(xVault.F, xVault.A, [[-sp, -sp], [sp, -sp], [sp, sp], [-sp, sp]], P.vaultSpring, P.vaultCrown + 0.5, { sideRise: () => 12.9, nS: 12, nT: 10 });+ addBoss(xVault.G, xVault.F, crossCrown, 0.95);+ // Great transverse arches and the walls above them+ const arch = pointedArc(2 * (zn - 0.5), 12.9, 7);+ for (let i = 0; i < 4; i++) xVault.F.at(M.of(M.Ry(i * 90), M.T(0, P.vaultSpring, zn - 0.05)), () => xVault.F.sweep(arch, rollSection(0.9, 1.2, 2, 0.09, 3), { caps: false }));+ placeSet('CrossingVault', xVault, [xc, 0, 0], 0, gX);+ for (const [a, b] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {+ placeSet('CrossingPier', xPier, [xc + a * zn, 0, b * zn], 0, gX);+ placeSet('AngleAislePier', aPier, [xc + a * zaw, 0, b * zaw], 0, gX);+ }+ // ---- Chevet ----+ const sector = buildChevetSector(), apsePier = buildApsePier();+ const apseSaints = [{ robe: 'gold', mantle: 'cobalt', crown: 1, veil: 'pale', arm: 'pray' }, { robe: 'white', mantle: 'ruby', beard: 2, attr: 'key', arm: 'point' }, { robe: 'white', mantle: 'emerald', beard: 1, attr: 'book' }, { robe: 'white', mantle: 'rubyLt', beard: 1, cross: 1, arm: 'up' }];+ const hemi = apseSaints.slice(0, 3).map((f, i) => makeLancetGlass(2.05, P.triforiumTop + 0.45, P.vaultSpring + 4.8, 4.3, 70 + i, f, i % 2 ? { ground: 'ruby', ground2: 'cobalt' } : { ground: 'cobalt', ground2: 'ruby' }));+ const chapelGlass = ['annunciation', 'crucifixion'].map((nm, i) => { const g = new Glass(80 + i); g.raw(archOutline(1.3, 3.2, 9.9, 2.5, 7), 'lead', -0.4, 0); gLancetStory(g, 0, 1.3, 3.2, 9.9, 2.5, [nm, ['supper', 'magi'][i], ['magi', 'resurrection'][i]], { cell: 0.6, c1: i ? 'cobalt' : 'ruby', c2: i ? 'ruby' : 'cobalt' }); return g.b; });+ for (let k = 0; k < P.apseSides; k++) {+ const th = -(k + 0.5) * CHEV.d, fr = frame(th, xa, 0), gS = group('ChevetSector' + k, gApse);+ placeSet('Sector' + k, sector, [xa, 0, 0], th, gS);+ placeGlass('HemicycleWindow' + k, hemi[(k + (k > 3 ? 1 : 0)) % hemi.length], fr.pos(0, 0, CHEV.Ra), th + 180, gS);+ const nF = P.chapelFacets, dF = 216 / nF, ap = CHEV.Rc * Math.cos(rad(dF / 2));+ for (let i = 0; i < nF; i++) {+ const fm = -108 + (i + 0.5) * dF, lx = ap * Math.sin(rad(fm)), lz = CHEV.zc + ap * Math.cos(rad(fm));+ placeGlass('ChapelWindow' + k + '_' + i, chapelGlass[(i + k) % chapelGlass.length], fr.pos(lx, 0, lz + 0.0), th + fm + 180, gS);+ }+ statue(fr, 0, 0.5, CHEV.zc + CHEV.Rc * 0.55, k === 3 ? 'virgin' : SAINTS[k], 90, 0.92, gS);+ garg(frame(th - 10, xa, 0), 0, 15.5, CHEV.zc + 1.2, k % 3, 0.7, gS);+ if (k >= 1) {+ const bt = -k * CHEV.d;+ placeButtress('ChevetButtress' + k, frame(bt, xa, 0), gS, k);+ // A slim clustered pier at each angle of the hemicycle: the arches now spring from something+ placeSet('ApsePier' + k, apsePier, frame(bt, xa, 0).pos(0, 0, zn), bt, gS);+ }+ }+ // ---- Roofs, ridge cresting, floor ----+ const Lr = new Batch(TILE.lead);+ const yE = P.eaves + 0.15, half = zn + 0.72;+ addRoofRun(Lr, xa, LAY.xW - 1.2, half, yE, ridge);+ Lr.at(M.T(xc, 0, 0), () => Lr.at(M.Ry(90), () => addRoofRun(Lr, -(zn + (P.transeptBays + 1) * BAY + 0.4), zn + (P.transeptBays + 1) * BAY + 0.4, half, yE, ridge)));+ place('MainRoof_Lead', Lr, MAT.lead, { parent: gRoof });+ const crest = buildCresting(BAY);+ const nCrest = Math.floor((LAY.xW - 2 - xa) / BAY);+ for (let i = 0; i < nCrest; i++) place('RidgeCresting' + i, crest, MAT.gold, { pos: [xa + (i + 0.5) * BAY, ridge, 0], parent: gRoof });+ for (const s of [1, -1]) for (let q = 0; q < P.transeptBays + 2; q++) { const zz = s * (0.5 + (q + 0.5) * BAY + 3); if (Math.abs(zz) < zn + (P.transeptBays + 1) * BAY) place('TranseptCresting' + (s > 0 ? 'N' : 'S') + q, crest, MAT.gold, { pos: [xc, ridge, zz], rot: [0, 90, 0], parent: gRoof }); }+ buildFleche(gRoof, xc, 0);+ // The church floor sits just above the ground outside, so the two can never fight+ const fl = new Batch(TILE.pave), fy = 0.06;+ const R13 = zaw + 0.1;+ fl.face([[xa, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, R13], [LAY.xW - WF.wallT + 0.1, fy, -R13], [xa, fy, -R13]]);+ const tz = zn + (P.transeptBays + 1) * BAY + 1;+ fl.face([[xc - R13, fy, tz], [xc + R13, fy, tz], [xc + R13, fy, -tz], [xc - R13, fy, -tz]]);+ for (let k = 0; k < P.apseSides; k++) { const th = rad(-(k + 0.5) * CHEV.d), f = frame(-(k + 0.5) * CHEV.d, xa, 0), rr = CHEV.zc + CHEV.Rc + 0.2, hh = rr * Math.tan(CHEV.h); fl.face([f.pos(0, fy, 0), f.pos(-hh, fy, rr), f.pos(hh, fy, rr)]); }+ place('Paving', fl, MAT.pave, { parent: ROOT });+}++// ==================================================================================+// THE GREAT ROSE: real stone tracery, and glass depicting Christ among apostles and angels+// ==================================================================================+const ROSE = { r0: 1.15, rs1: 2.35, r1: 3.05, rs2: 4.05, r2: 4.72, n1: 12, n2: 24 };+const pol = (r, a) => [r * Math.cos(a), r * Math.sin(a)];+function addRoseTracery(F, R) {+ const k = R / 5.5, heavy = traceryBar(0.3 * k, 0.5), mid = hexBar(0.2 * k, 0.42), thin = hexBar(0.14 * k, 0.34);+ const dia = [[0.07 * k, 0], [0, 0.17], [-0.07 * k, 0], [0, -0.17]]; // lightest bar, for the little roundels+ const { r0, rs1, r1, rs2, r2, n1, n2 } = ROSE;+ F.sweep(ringPts(0, 0, R, 36), heavy, { closed: true });+ F.sweep(ringPts(0, 0, r0 * k, 14), mid, { closed: true });+ F.sweep(ringPts(0, 0, r2 * k, 36), mid, { closed: true });+ F.sweep(ringPts(0, 0, r1 * k, 24), mid, { closed: true });+ // Inner wheel: colonnette spokes, each light closed by a pointed trefoil head+ const head = (n, rs, re, a, bar) => {+ const da = PI / n * 0.98, pts = [], N = 6;+ for (let i = 0; i <= N; i++) { const t = -1 + 2 * i / N, h = Math.pow(1 - Math.pow(Math.abs(t), 1.5), 0.72); pts.push(pol((rs + (re - rs) * h) * k, a + t * da)); }+ F.sweep(pts, bar, { caps: false });+ return pts;+ };+ for (let i = 0; i < n1; i++) {+ const a = i * TAU / n1, am = a + PI / n1;+ F.sweep([pol(r0 * k, a), pol(rs1 * k, a)], mid, { caps: false });+ head(n1, rs1, r1 * 0.985, am, thin);+ // the cusp hanging inside the head+ const c = pol((rs1 + 0.34) * k, am); F.sweep(ringPts(c[0], c[1], 0.26 * k, 6), dia, { closed: true, s: 0.95, crease: 80 });+ }+ // Outer wheel of twenty-four lights+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, am = a + PI / n2;+ F.sweep([pol(r1 * k, a), pol(rs2 * k, a)], thin, { caps: false });+ head(n2, rs2, r2 * 0.99, am, thin);+ // Quatrefoil roundels in the outer band+ const c = pol((R / k + r2) / 2 * k, a);+ F.sweep(ringPts(c[0], c[1], 0.3 * k, 6), dia, { closed: true, crease: 80 });+ }+}+const SHARED = {};+// Rose tracery is one mesh, scaled for the smaller transept roses; bright outside, dim inside+function roseTracery() {+ if (!SHARED.rose) { const b = new Batch(TILE.fine); addRoseTracery(b, WF.roseR); SHARED.rose = b; }+ return SHARED.rose;+}+function largePinnacle() {+ if (!SHARED.pin) { const b = new Batch(TILE.fine); addPinnacle(b, 2, 10, { lod: 1 }); SHARED.pin = b; }+ return SHARED.pin;+}+function makeRoseGlass(R, seed, o = {}) {+ const g = new Glass(seed), k = R / 5.5, { r0, r1, r2, n1, n2 } = ROSE;+ g.raw(ringPts(0, 0, R + 0.08, 48), 'lead', -0.4, 0);+ // A light is never one flat sheet: lay the painted mosaic across it, clipped to its shape+ const ground = (pts, col, layer) => { let x0 = 1e9, x1 = -1e9, y0 = 1e9, y1 = -1e9; for (const p of pts) { x0 = Math.min(x0, p[0]); x1 = Math.max(x1, p[0]); y0 = Math.min(y0, p[1]); y1 = Math.max(y1, p[1]); } g.withClip(pts, () => gDiaper(g, x0, y0, x1, y1, 0, col, null, null, layer)); };+ // A roundel: a thin jewelled rim round a deep field, with everything inside it clipped to the circle+ const roundel = (cx, cy, rad_, field, draw) => {+ gRim(g, cx, cy, rad_, rad_ * 1.13, ['gold', 'ruby', 'gold', 'cobalt'], 16, 1.2);+ g.disc(cx, cy, rad_, field, 1.5, 16, { came: 0 });+ g.withClip(ringPts(cx, cy, rad_, 16), draw);+ };+ // Nothing behind the tracery is left black: the whole wheel is first laid in cobalt mosaic+ ground(ringPts(0, 0, R + 0.04, 32), 'cobalt', -0.25);+ // The outer band: alternating sheets of ruby and cobalt, a jewel in each+ for (let i = 0; i < n2; i++) {+ const a = i * TAU / n2, b = (i + 1) * TAU / n2;+ ground([pol(r2 * k, a), pol(R, a), pol(R, b), pol(r2 * k, b)], i % 2 ? 'ruby' : 'cobalt', 0);+ const c = pol((R / k + r2) / 2 * k, a); g.disc(c[0], c[1], 0.27 * k, i % 2 ? 'emerald' : 'ruby', 0.6, 10, { came: 0.016 }); g.disc(c[0], c[1], 0.12 * k, 'gold', 1, 8, { came: 0.01 });+ }+ // Outer lights: kings, angels and elders, upright in their roundels so the eye can rest on each face+ const busts = [{ robe: 'emerald', beard: 1, crown: 1, halo: 'gold' }, { robe: 'white', face: 'angel', halo: 'gold', wings: 1 }, { robe: 'ruby', beard: 2, halo: 'gold' }];+ for (let i = 0; i < n2; i++) {+ const am = (i + 0.5) * TAU / n2, da = PI / n2, col = i % 2 ? 'cobalt' : 'ruby', field = i % 2 ? 'ruby' : 'cobalt';+ ground([pol(r1 * k, am - da), pol(r2 * k, am - da), pol(r2 * k, am + da), pol(r1 * k, am + da)], col, 0);+ const mr = (r1 + r2) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.46 * k, B = busts[i % busts.length];+ roundel(cx, cy, rr, field, () => {+ if (B.wings) for (const s of [-1, 1]) g.poly([[cx + s * rr * 0.16, cy - rr * 0.1], [cx + s * rr * 0.98, cy + rr * 0.42], [cx + s * rr * 0.86, cy - rr * 0.5], [cx + s * rr * 0.3, cy - rr * 0.62]], 'gold', 1.8, { came: 0.012 });+ gBust(g, cx, cy - rr * 0.46, rr * 1.32, Object.assign({ layer: 2 }, B));+ });+ }+ // Inner lights: the twelve apostles enthroned, each upright in a larger roundel, colours answering round the wheel+ const scheme = o.figs || [{ robe: 'ruby', mantle: 'cobalt', beard: 1, attr: 'key', arm: 'point' }, { robe: 'emerald', mantle: 'ruby', beard: 2, attr: 'book' }, { robe: 'gold', mantle: 'cobalt', beard: 1, attr: 'sword', arm: 'point' }, { robe: 'cobalt', mantle: 'ruby', attr: 'book' }, { robe: 'purple', mantle: 'gold', beard: 1, attr: 'book' }, { robe: 'ruby', mantle: 'emerald', beard: 2, attr: 'key', arm: 'point' }];+ for (let i = 0; i < n1; i++) {+ const am = (i + 0.5) * TAU / n1, da = PI / n1, col = i % 2 ? 'ruby' : 'cobalt', field = i % 2 ? 'cobalt' : 'ruby';+ ground([pol(r0 * k, am - da), pol(r1 * k, am - da), pol(r1 * k, am + da), pol(r0 * k, am + da)], col, 0);+ const mr = (r0 + r1) / 2 * k, cx = mr * Math.cos(am), cy = mr * Math.sin(am), rr = 0.62 * k;+ roundel(cx, cy, rr, field, () => g.withXf(0, cx, cy - rr * 0.92, 1, () => gFigure(g, 0, 0, rr * 1.86, Object.assign({ pose: 'sit', layer: 2 }, scheme[i % scheme.length]))));+ }+ // The centre: Christ in Majesty in a gilded mandorla+ g.disc(0, 0, r0 * k + 0.06, 'lead', 2.6, 16, { came: 0, jitter: 0 });+ ground(ringPts(0, 0, r0 * k, 16), o.centre || 'cobalt', 3);+ g.withClip(ringPts(0, 0, r0 * k, 14), () => g.withXf(0, 0, -0.98 * k, k, () => {+ const m = []; for (let i = 0; i < 16; i++) { const t = i * TAU / 16; m.push([0.62 * Math.cos(t) * Math.pow(Math.abs(Math.sin(t)) * 0.3 + 0.7, 1), 1 + 0.96 * Math.sin(t)]); }+ g.poly(m, 'gold', 3.3, { came: 0.02 });+ g.poly(m.map((p) => [p[0] * 0.8, 1 + (p[1] - 1) * 0.88]), 'rubyLt', 3.5, { came: 0.012 });+ gFigure(g, 0, 0.22, 1.62, o.majesty || { pose: 'sit', robe: 'white', mantle: 'ruby', cross: 1, beard: 1, arm: 'up', attr: null, layer: 4 });+ }));+ return g.b;+}++// ==================================================================================+// WEST FRONT. Local frame: x across the facade, y up, +z out towards the viewer, z=0 the wall.+// ==================================================================================+const WF = { halfW: 21.5, topY: 47, roseY: 31.8, roseR: 5.5, kingY: 20.2, kingTop: 25.3, galY: 40.6, galTop: 46.2, wallT: 0.8, portalC: { x: 0, w0: 4.3, orders: P.jambStatues, dx: 0.58, dz: 0.62, ys: 7.5 }, portalS: { x: 13.5, w0: 3.7, orders: P.jambStatues - 1, dx: 0.6, dz: 0.66, ys: 7.2 } };+// Aged oak door leaf with forged straps, studs and a ring. Hung at x=0, spans to x=w, h high, front +z.+function addDoorLeaf(set, x0, w, h, flip) {+ const { O, I } = set, planks = 6, pw = w / planks, d = flip ? -1 : 1;+ for (let i = 0; i < planks; i++) {+ const a = x0 + d * i * pw, c = x0 + d * (i + 0.97) * pw, xa = Math.min(a, c), xc = Math.max(a, c), tone = 0.46 + 0.16 * Math.abs(Math.sin(i * 2.4 + x0 * 3));+ O.at(M.Ry(0), () => O.box(xa, xc, 0.02, h, -0.05, 0.05, { s: tone }));+ }+ // Oak grain runs up each plank: remap planes with u across and v up is the default box projection+ const strapY = [0.72, h * 0.36, h * 0.64, h - 0.7];+ for (const y of strapY) {+ const reach = w * 0.94, xEnd = x0 + d * reach;+ I.shade(0.34, () => {+ I.box(Math.min(x0, xEnd), Math.max(x0, xEnd), y - 0.075, y + 0.075, 0.05, 0.085, {});+ // A forged hinge: the strap swells and ends in a spear with two barbs, flat iron from one piece; a knuckle turns at the door's edge+ const spear = [[xEnd, y - 0.075], [xEnd + d * 0.1, y - 0.15], [xEnd + d * 0.16, y - 0.07], [xEnd + d * 0.4, y], [xEnd + d * 0.16, y + 0.07], [xEnd + d * 0.1, y + 0.15], [xEnd, y + 0.075]];+ I.slab(spear, [], 0.05, 0.085, {});+ I.at(M.T(x0 + d * 0.03, y, 0.07), () => I.lathe([[0.055, -0.12], [0.055, 0.12]], 6, {}));+ for (let k = 0; k < 4; k++) { const sx = x0 + d * (k + 0.5) * reach / 4; I.box(sx - 0.03, sx + 0.03, y - 0.03, y + 0.03, 0.08, 0.115, { taper: 0.012 }); }+ });+ }+ // Ring handle+ I.shade(0.4, () => I.at(M.of(M.T(x0 + d * (w - 0.3), h * 0.42, 0.11), M.Ry(0)), () => {+ const rg = []; for (let i = 0; i <= 12; i++) { const t = i * TAU / 12; rg.push([0.1 * Math.cos(t), 0.1 * Math.sin(t) - 0.1, 0]); }+ I.tube(rg, 0.018, 5, { capEnd: false }); I.box(-0.05, 0.05, -0.05, 0.05, -0.05, 0, {});+ }));+}+// Height of each sculpted figure at scale 1, from its feet to the top of its hair, crown or mitre+const figureTop = (kind) => ({ bishop: 2.15, king: 2.03, queen: 2.03, virgin: 2.03 }[FIG_ALIAS[kind] || kind] || 1.95);+function buildPortal(cfg, kinds, tymp) {+ const set = mkBatches(), { A, F, G, O, I } = set, { w0, orders: N, dx, dz, ys } = cfg;+ // Every order is concentric: the same two arc centres with the radius growing by one step+ const r0 = w0 * 0.86, c0 = (r0 * r0 - w0 * w0 / 4) / w0, wT = (i) => w0 + 2 * i * dx, zT = (i) => i * dz;+ const riseI = (i) => Math.sqrt(Math.pow(c0 + w0 / 2 + i * dx, 2) - c0 * c0);+ const riseOf = (w) => riseI((w - w0) / (2 * dx));+ const figs = [], zF = N * dz, outerW = wT(N), apex = ys + riseOf(outerW), lintelH = 1.2, doorH = ys - lintelH;+ const statueY = 1.5, statueK = 1.22;+ // ---- Solid stepped jambs, each order with a dado of carved blind arches ----+ for (let i = 0; i < N; i++) {+ for (const s of [-1, 1]) {+ const xa = s * wT(i) / 2, xb = s * wT(i + 1) / 2, x0 = Math.min(xa, xb), x1 = Math.max(xa, xb), zf = zT(i + 1);+ A.box(x0, x1, 0, ys, -0.1, zf, { noBot: true });+ // The dado: a plinth, a blind trefoil head on two colonnettes, a moulded table for the statues above+ F.box(x0 - 0.01, x1 + 0.01, 0, 0.26, zf, zf + 0.05, {});+ F.at(M.T((x0 + x1) / 2, 0, zf), () => {+ const w = x1 - x0 - 0.12, y0 = 0.26, ya = 0.86, r = 0.32;+ F.sweep(pointedArc(w, r, 4).map((p) => [p[0], p[1] + ya]), hexBar(0.05, 0.09), { caps: false, z: 0.03 });+ for (const t of [-1, 1]) F.box(t * w / 2 - 0.03, t * w / 2 + 0.03, y0, ya, 0, 0.06, {});+ F.sweep([[-(x1 - x0) / 2, 1.3], [(x1 - x0) / 2, 1.3]], stringSection(0.16, 0.1), { caps: false });+ });+ // Colonnette on the arris, with a foliate capital; the saint stands against it+ const cx = s * (wT(i) / 2 + 0.02), cz = zT(i) + 0.32;+ F.at(M.T(cx, 0, cz), () => {+ F.lathe([[0.2, 1.3], [0.2, 1.42], [0.13, 1.54], [0.13, ys - 0.5], [0.16, ys - 0.46], [0.13, ys - 0.42], [0.26, ys - 0.04], [0.3, ys]], 8);+ for (let l = 0; l < 4; l++) addLeaf(F, M.of(M.T(0, ys - 0.42, 0), M.Ry(l * 90 + 45), M.T(0.1, 0, 0)), 0.38, { coarse: 1 });+ });+ // The figure stands with its back to the column, on a carved base; its canopy is engaged on the same column,+ // a hand's breadth above this figure's own head+ const dirX = -s * dz, dirZ = dx * 1.5, l = Math.hypot(dirX, dirZ), th = Math.atan2(-dirZ / l, dirX / l) * 180 / PI, fx = dirX / l, fz = dirZ / l;+ const kind = kinds[(i + (s > 0 ? 0 : kinds.length >> 1)) % kinds.length], canopyY = statueY + figureTop(kind) * statueK + 0.1;+ const sx = cx + fx * 0.27, sz = cz + fz * 0.27;+ F.at(M.T(sx, 0, sz), () => {+ F.lathe([[0.3, 1.25], [0.34, 1.32], [0.24, 1.38], [0.3, 1.44], [0.33, statueY]], 8);+ for (let l = 0; l < 2; l++) addLeaf(F, M.of(M.T(0, 1.36, 0), M.Ry(l * 90 + (s > 0 ? -60 : 150)), M.T(0.24, 0, 0)), 0.2, { coarse: 1 });+ F.at(M.T(0, canopyY + 0.14, 0), () => {+ // A canopy like a tiny carved building: vaulted soffit, a drum of gablets, thin spirelet+ F.lathe([[0.05, -0.14], [0.27, -0.06], [0.33, 0], [0.33, 0.1], [0.28, 0.14], [0.17, 0.17], [0.17, 0.5], [0.22, 0.54], [0.15, 0.58], [0.07, 0.64], [0.01, 1.02]], 6, { phase: PI / 6, faceted: true });+ const face = s > 0 ? -60 : 60;+ for (const l of [-1, 0, 1]) F.at(M.of(M.Ry(face + l * 60), M.T(0, 0, 0.27)), () => {+ F.face([[-0.16, 0.11, 0.02], [0.16, 0.11, 0.02], [0, 0.42, 0.02]]);+ crocketsAlong(F, [-0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [-1, 0.6, 0], 0);+ crocketsAlong(F, [0.16, 0.13, 0.02], [0, 0.4, 0.02], 1, 0.1, [1, 0.6, 0], 0);+ F.shade(0.5, () => F.face([[-0.08, 0.14, 0.03], [0.08, 0.14, 0.03], [0, 0.28, 0.03]]));+ });+ });+ });+ figs.push({ kind, pos: [cfg.x + sx, statueY, sz], rot: [0, th, 0], scale: statueK, mirror: s < 0 });+ }+ }+ // ---- Archivolts: each order a hollow, each hollow a procession of small figures, every one on a console under a canopy ----+ const ARCH_KINDS = ['apostle', 'king', 'angel', 'bishop', 'queen'], ak = 0.62;+ for (let i = 0; i < N; i++) {+ const w = wT(i), r = riseI(i), arc = pointedArc(w, r, 8).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, [[0, -0.1], [dx, -0.1], [dx, dz], [0, 0]], { z: zT(i), caps: false, crease: 30 });+ F.at(M.T(0, 0, zT(i + 1) - 0.02), () => F.sweep(pointedArc(wT(i + 1), riseI(i + 1), 8).map((p) => [p[0], p[1] + ys]), hexBar(0.2, 0.22), { caps: false }));+ const pts = pointedArc(w + dx, riseI(i + 0.5), 16).map((p) => [p[0], p[1] + ys]);+ let len = 0; const seg = []; for (let k = 0; k < pts.length - 1; k++) { const l = Math.hypot(pts[k + 1][0] - pts[k][0], pts[k + 1][1] - pts[k][1]); seg.push(l); len += l; }+ const n = Math.max(4, Math.round(len / 1.52));+ for (let q = 0; q < n; q++) {+ const d = (q + 0.5) / n * len; let acc = 0, k = 0; while (k < seg.length - 1 && acc + seg[k] < d) { acc += seg[k]; k++; }+ const f = (d - acc) / seg[k], px = lerp(pts[k][0], pts[k + 1][0], f), py = lerp(pts[k][1], pts[k + 1][1], f);+ const tx0 = (pts[k + 1][0] - pts[k][0]) / seg[k], ty0 = (pts[k + 1][1] - pts[k][1]) / seg[k], rx = -ty0, ry = tx0;+ const up = d > len / 2 ? -1 : 1, Y = [tx0 * up, ty0 * up, 0];+ // sits on the splay, looking out towards the opening and the viewer+ const nl = Math.hypot(dz, dx), X = [-rx * dz / nl, -ry * dz / nl, dx / nl];+ const Z = [X[1] * Y[2] - X[2] * Y[1], X[2] * Y[0] - X[0] * Y[2], X[0] * Y[1] - X[1] * Y[0]];+ const pos = [px + X[0] * 0.17 - Y[0] * 0.62, py + X[1] * 0.17 - Y[1] * 0.62, zT(i) + dz * 0.5 + X[2] * 0.17];+ figs.push({ kind: ARCH_KINDS[(i + q) % ARCH_KINDS.length], lod: 0, pos: [cfg.x + pos[0], pos[1], pos[2]], basis: { X, Y, Z }, scale: ak });+ F.at(frameOf(pos, X, Y), () => {+ F.lathe([[0.03, -0.18], [0.16, -0.02], [0.18, 0.02]], 6);+ F.lathe([[0.02, 1.27], [0.16, 1.33], [0.16, 1.38], [0.02, 1.56]], 6, { faceted: true });+ });+ }+ }+ // ---- Outer hood with gable and crockets ----+ const hood = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, zF), () => F.sweep(hood, rollSection(0.34, 0.44, 2, 0.05, 2), { caps: false }));+ // The gable springs from the arch springing: plain coursed stone, its carving is real and stands proud of it+ const gw = outerW + 1.5, gy = ys, gTop = WF.kingY - 0.7, gH = gTop - gy;+ const archCut = pointedArc(outerW + 0.1, riseOf(outerW) + 0.1, 9).map((p) => [p[0], p[1] + ys]);+ const field = [[-gw / 2, gy], [-(outerW + 0.1) / 2, gy], ...archCut.slice(1, -1), [(outerW + 0.1) / 2, gy], [gw / 2, gy], [0, gTop]];+ A.at(M.T(0, 0, zF - 0.1), () => A.slab(field, [], -0.1, 0, { noOuterSides: true, noBack: true }));+ A.at(M.T(0, 0, zF - 0.2), () => A.slab(field, [], -zF + 0.3, 0, { noOuterSides: false, noFront: true, noBack: true, so: 0.7 }));+ F.at(M.T(0, 0, zF - 0.14), () => {+ addGable(F, gw, gH, 0, 0.24, gy, { size: 0.72, finial: 0.1, crockets: 6, noField: true, lod: 1 });+ const yc = apex + (gTop - apex) * 0.36, rc = Math.min(0.92, (gTop - yc) * 0.42);+ F.sweep(ringPts(0, yc, rc, 16), traceryBar(0.18, 0.24), { closed: true, z: 0.04 });+ const foil = []; for (let i = 0; i < 15; i++) { const a = PI / 2 + i * TAU / 15, R = rc * (0.56 + 0.34 * Math.pow(Math.abs(Math.cos(a * 1.5 - PI * 0.75)), 0.8)); foil.push([R * Math.cos(a), yc + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.09, 0.18), { closed: true, z: 0.04, crease: 40 });+ });+ G.at(M.T(0, gTop + 0.1, zF - 0.02), () => addFinial(G, 1.5, 1));+ // ---- Tympanum: one block of dressed stone, standing proud of the coursed wall behind it, with the scene carved out of it ----+ const tw = w0 + 0.04, tr = riseOf(w0), k = w0 / 4.3, reliefZ = 0.16, relief = 0.6;+ F.slab(archOutline(tw, ys - 0.05, ys, tr, 9), [], -0.1, reliefZ, { noBack: true, sr: 0.7 });+ // the ground the figures rise from is part of the block, a low moulded base rather than a shelf+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, ys - 0.02, ys + 0.1, -0.08, reliefZ + 0.14, {});+ // Lintel: a recessed frieze, deep enough to hold its people, cornice and sill standing forward of it+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, ys - 0.02, -0.08, -0.02, {});+ F.box(-tw / 2 - 0.02, tw / 2 + 0.02, doorH, doorH + 0.12, -0.08, 0.42, {});+ F.at(M.T(0, 0, 0.42), () => F.sweep([[-tw / 2 - 0.02, doorH + 0.06], [tw / 2 + 0.02, doorH + 0.06]], stringSection(0.12, 0.06), { caps: false }));+ const nL = Math.round(w0 / 0.37), frieze = ['apostle', 'queen', 'apostle', 'king', 'bishop', 'apostle'];+ for (let i = 0; i < nL; i++) {+ // two ranks, every other figure a step forward: they overlap and each one turns to the middle of the door+ const x = -w0 / 2 + (i + 0.5) * w0 / nL, side = x < 0 ? 1 : -1, deep = i % 2;+ figs.push({ kind: frieze[i % frieze.length], lod: 0, relief: 0.68, pos: [cfg.x + x, doorH + 0.12, deep ? 0.04 : 0.16], rot: [0, -90 + side * (16 + 10 * (i % 3)), 0], scale: 0.43 });+ }+ // The central figure enthroned in a vesica: two arcs meeting in a point above and below, a broad moulded band of stone+ // with its face gilded. Everything in the scene is cut back to the same block, and nothing crosses the frame.+ const mY = ys + 0.1, mH = tr * 0.84, mW = 0.84 * k, nM = 10;+ const vesica = (w, h, y0) => { const out = []; for (let i = 0; i < nM; i++) { const v = i / nM; out.push([w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } for (let i = 0; i < nM; i++) { const v = 1 - i / nM; out.push([-w * (1 - Math.pow(Math.abs(2 * v - 1), 2.6)), y0 + h * v]); } return out; };+ const vo = vesica(mW, mH, mY - 0.06), vi = vesica(mW - 0.2, mH - 0.44, mY + 0.16);+ F.at(M.T(0, 0, reliefZ), () => { F.slab(vo, [vi], -0.02, 0.07, { sr: 0.78 }); F.sweep(vo, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); F.sweep(vi, hexBar(0.05, 0.06), { closed: true, crease: 50, z: 0.1 }); });+ G.at(M.T(0, 0, reliefZ + 0.07), () => G.slab(vesica(mW - 0.025, mH - 0.07, mY - 0.025), [vesica(mW - 0.175, mH - 0.37, mY + 0.125)], 0, 0.05, {}));+ figs.push({ kind: tymp.main, half: 1, relief, pos: [cfg.x, mY, reliefZ + 0.02], rot: [0, -90, 0], scale: 1.0 * k });+ // Worshippers on their knees and angels crowding behind them, every head turned to the throne+ for (const s of [-1, 1]) {+ figs.push({ kind: 'kneel', half: 1, relief, pos: [cfg.x + s * 1.28 * k, mY, reliefZ + 0.12], rot: [0, -90 + s * 50, 0], scale: 0.84 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[0], half: 1, relief, pos: [cfg.x + s * 1.6 * k, mY, reliefZ + 0.01], rot: [0, -90 + s * 34, 0], scale: 0.72 * k, mirror: s < 0 });+ figs.push({ kind: tymp.side[1], half: 1, relief, lod: 0, pos: [cfg.x + s * 1.92 * k, mY, reliefZ + 0.08], rot: [0, -90 + s * 55, 0], scale: 0.44 * k, mirror: s < 0 });+ }+ // Trumeau with its statue, on a carved base, under a canopy+ A.box(-0.32, 0.32, 0, doorH, -0.1, 0.34, { noBot: true });+ const tK = tymp.trumeauScale || 1.3, tBase = 1.06, tCanopy = tBase + figureTop(tymp.trumeau) * tK + 0.12;+ F.at(M.T(0, 0, 0.5), () => {+ F.lathe([[0.42, 0], [0.42, 0.42], [0.3, 0.56], [0.25, 0.78], [0.34, 0.92], [0.36, tBase]], 8, { faceted: true, phase: PI / 8 });+ for (let l = 0; l < 6; l++) addLeaf(F, M.of(M.T(0, 0.82, 0), M.Ry(l * 60 + 15), M.T(0.27, 0, 0)), 0.24, { coarse: 1 });+ F.at(M.T(0, tCanopy + 0.2, 0), () => F.lathe([[0.06, -0.2], [0.33, -0.08], [0.42, 0], [0.42, 0.14], [0.35, 0.18], [0.22, 0.22], [0.22, 0.58], [0.28, 0.62], [0.2, 0.66], [0.09, 0.72], [0.02, 1.1]], 6, { phase: PI / 6, faceted: true }));+ });+ figs.push({ kind: tymp.trumeau, pos: [cfg.x, tBase, 0.5], rot: [0, -90, 0], scale: tK });+ // Doors: two leaves either side of the trumeau, recessed in the wall+ const leaf = w0 / 2 - 0.34;+ O.at(M.T(0, 0, -0.14), () => { const set2 = set; I.at(M.T(0, 0, -0.14), () => { addDoorLeaf(set2, w0 / 2 - 0.02, leaf, doorH - 0.04, true); addDoorLeaf(set2, -w0 / 2 + 0.02, leaf, doorH - 0.04, false); }); });+ return { set, figs, apex, zF, outerW };+}+function placePortal(gWest, name, cfg, pr, opt = {}) {+ placeSet(name, pr.set, [cfg.x, 0, 0], 0, gWest);+ const gF = group(name + '_Figures', gWest); // the people of one door, as one group of nodes+ pr.figs.forEach((f, i) => {+ const rot = f.rot || eulerFromBasis(f.basis.X, f.basis.Y, f.basis.Z);+ const pos = [f.pos[0] + (cfg.x - pr.baseX), f.pos[1], f.pos[2]];+ // Full sculpture where people stand next to it; opt.light keeps a door's overhead scene, or the whole door, to the lighter copies+ const far = opt.light === 'all' || (opt.light === 'scene' && f.pos[1] > 4);+ placeFigure(name + '_Fig' + i, f.kind, { pos, rot, scale: f.scale, parent: gF, lod: far ? 0 : f.lod, mirror: f.mirror, half: f.half, relief: f.relief });+ });+}+function eulerFromBasis(X, Y, Z) { // degrees, three.js 'XYZ' order+ const m13 = clamp(Z[0], -1, 1), y = Math.asin(m13);+ let x, z;+ if (Math.abs(m13) < 0.9999) { x = Math.atan2(-Z[1], Z[2]); z = Math.atan2(-Y[0], X[0]); } else { x = Math.atan2(Y[2], Y[1]); z = 0; }+ return [x * 180 / PI, y * 180 / PI, z * 180 / PI];+}+// One niche of the Gallery of Kings, shared along the facade+function buildKingNiche(pitch) {+ const set = mkBatches(), { F, A } = set, h = WF.kingTop - WF.kingY, y0 = WF.kingY;+ A.face([[-pitch / 2, y0, 0.02], [pitch / 2, y0, 0.02], [pitch / 2, y0 + h, 0.02], [-pitch / 2, y0 + h, 0.02]]);+ F.box(-pitch / 2, pitch / 2, y0 - 0.5, y0, 0, 1.3, { s: 0.9 });+ F.at(M.T(0, 0, 1.3), () => F.sweep([[-pitch / 2, y0 - 0.38], [pitch / 2, y0 - 0.38]], stringSection(0.3, 0.26), { caps: false }));+ // Colonnettes and a cusped arch with a gablet over each king+ const ys = y0 + 3.15, rise = 1.3;+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0.95), () => F.lathe([[0.17, y0], [0.09, y0 + 0.16], [0.09, ys - 0.2], [0.2, ys]], 8));+ F.at(M.T(0, 0, 0.95), () => {+ const arc = pointedArc(pitch - 0.1, rise, 6).map((p) => [p[0], p[1] + ys]);+ F.sweep(arc, rollSection(0.14, 0.36, 1, 0.03, 2), { caps: false });+ // solid spandrel+ A.at(M.T(0, 0, 0.95), () => A.slab([[-pitch / 2, ys], [-(pitch - 0.1) / 2, ys], ...arc, [(pitch - 0.1) / 2, ys], [pitch / 2, ys], [pitch / 2, y0 + h], [-pitch / 2, y0 + h]], [], -0.1, 0.1, { noOuterSides: true }));+ addGable(F, pitch * 0.96, 1.12, 0.1, 0.1, ys + 0.46, { size: 0.26, crockets: 2, finial: 0.34, solid: true, lod: 0 });+ });+ // The roof of the gallery behind the arcade+ F.box(-pitch / 2, pitch / 2, y0 + h - 0.02, y0 + h + 0.12, 0, 1.28, { s: 0.85 });+ return set;+}+// One bay of the open arcade above the rose and the tower windows+function buildGalleryBay(pitch) {+ const set = mkBatches(), { F, A } = set, y0 = WF.galY, y1 = WF.galTop, ys = y0 + 3.3;+ F.shade(0.4, () => F.face([[-pitch / 2, y0, 0.03], [pitch / 2, y0, 0.03], [pitch / 2, y1, 0.03], [-pitch / 2, y1, 0.03]]));+ const arc = pointedArc(pitch * 0.92, 1.5, 5).map((p) => [p[0], p[1] + ys]);+ F.at(M.T(0, 0, 0.5), () => {+ F.sweep(arc, traceryBar(0.16, 0.36), { caps: false });+ for (const s of [-1, 1]) F.at(M.T(s * pitch / 2, 0, 0), () => F.lathe([[0.2, y0], [0.1, y0 + 0.2], [0.1, ys - 0.22], [0.22, ys]], 8));+ F.sweep(ringPts(0, ys + 0.6, 0.26, 7), hexBar(0.07, 0.14), { closed: true });+ });+ A.at(M.T(0, 0, 0.5), () => A.slab([[-pitch / 2, ys], [-pitch * 0.46, ys], ...arc, [pitch * 0.46, ys], [pitch / 2, ys], [pitch / 2, y1], [-pitch / 2, y1]], [], -0.18, 0.18, { noOuterSides: true }));+ F.box(-pitch / 2, pitch / 2, y0 - 0.2, y0 + 0.02, 0, 0.72, { s: 0.92 });+ F.at(M.T(0, 0, 0.72), () => { F.sweep([[-pitch / 2, y0 - 0.1], [pitch / 2, y0 - 0.1]], stringSection(0.26, 0.22), { caps: false }); F.sweep([[-pitch / 2, y1], [pitch / 2, y1]], stringSection(0.3, 0.3), { caps: false }); });+ F.at(M.T(0, 0, 0.72), () => addParapet(F, -pitch / 2, pitch / 2, y1 + 0.12, 1.05, 0.2, { pitch: 1.1, nh: 8 }));+ return set;+}+// One face of a belfry stage, shared by all eight faces of the two towers+function buildBelfryFace() {+ const set = mkBatches(), { A, F, L } = set, hw = 7, zf = 7, y0 = WF.topY, yT = P.towerTop - 2.6;+ const lw = 3.05, lx = 3.3, sill = y0 + 4.2, ys = y0 + 13.6, rise = 4.1, holes = [];+ for (const s of [-1, 1]) holes.push(archOutline(lw, sill, ys, rise, 7).map((p) => [s * lx + p[0], p[1]]));+ A.at(M.T(0, 0, zf), () => A.slab([[-hw, y0], [hw, y0], [hw, yT], [-hw, yT]], holes, -0.9, 0, { noOuterSides: true, noBack: true, sr: 0.66 }));+ // The bell chamber: a floor behind the openings, and a bell hanging on each side of the frame+ F.box(-hw + 0.8, hw - 0.8, sill - 0.5, sill - 0.2, zf - 5, zf - 0.8, {});+ set.I.at(M.T(0, sill + 3.2, zf - 2.5), () => set.I.lathe([[0.06, 1.72], [0.36, 1.62], [0.5, 1.12], [0.56, 0.7], [0.84, 0.12], [0.92, 0]], 10));+ // Behind the bells the oak frame that carries them closes the view, so no sky shows through the tower+ set.O.face([[-4.2, sill - 0.3, zf - 4], [4.2, sill - 0.3, zf - 4], [4.2, yT, zf - 4], [-4.2, yT, zf - 4]], 0.16);+ for (const s of [-1, 1]) {+ F.at(M.T(s * lx, 0, zf - 0.45), () => {+ F.sweep(pointedArc(lw, rise, 6).map((p) => [p[0], p[1] + ys]), rollSection(0.2, 0.7, 1, 0.04, 2), { caps: false });+ F.sweep([[0, sill - 0.05], [0, ys + 0.1]], traceryBar(0.2, 0.42), {});+ const lw2 = (lw - 0.1) / 2;+ for (const t of [-1, 1]) F.sweep(pointedArc(lw2, lw2 * 0.9, 4).map((p) => [t * lw2 / 2 + p[0], ys + 0.1 + p[1]]), hexBar(0.14, 0.34), { caps: false });+ F.sweep(ringPts(0, ys + rise * 0.62, 0.48, 10), hexBar(0.14, 0.34), { closed: true });+ });+ // Louvres: lead-clad boards set across the opening and tilted to throw the rain outward, dark air between them+ L.at(M.T(s * lx, 0, zf - 0.45), () => {+ const slope = 0.42;+ for (let y = sill + 0.2; y < ys + rise * 0.3; y += 0.5) {+ const h = y <= ys ? lw / 2 : (lw / 2) * Math.sqrt(Math.max(0, 1 - Math.pow((y - ys) / rise, 2)));+ L.face([[-h, y, 0.3], [h, y, 0.3], [h, y + slope, -0.3], [-h, y + slope, -0.3]]);+ L.face([[h, y - 0.05, 0.3], [-h, y - 0.05, 0.3], [-h, y + slope - 0.05, -0.3], [h, y + slope - 0.05, -0.3]]);+ }+ });+ F.at(M.T(s * lx, 0, zf), () => {+ F.sweep(pointedArc(lw + 0.5, rise + 0.3, 6).map((p) => [p[0], p[1] + ys]), hexBar(0.24, 0.28), { caps: false });+ addGable(F, lw + 0.9, 3.1, 0.03, 0.18, ys + rise * 0.5, { size: 0.42, finial: 0.8, crockets: 3, lod: 0 });+ });+ }+ F.at(M.T(0, 0, zf), () => {+ F.sweep([[-hw, y0 + 0.2], [hw, y0 + 0.2]], stringSection(0.42, 0.36), { caps: false });+ F.sweep([[-hw, sill - 0.3], [hw, sill - 0.3]], stringSection(0.32, 0.28), { caps: false });+ F.sweep([[-hw, yT - 0.2], [hw, yT - 0.2]], stringSection(0.4, 0.44), { caps: false });+ for (let i = 0; i < 12; i++) { const x = -hw + (i + 0.5) * 2 * hw / 12; F.box(x - 0.14, x + 0.14, yT - 0.78, yT - 0.42, 0, 0.3, { s: 0.85, sb: 0.5 }); }+ });+ F.at(M.T(0, 0, zf - 0.2), () => addParapet(F, -hw, hw, yT, 1.2, 0.28, { pitch: 1.4, nh: 8 }));+ return set;+}+// Octagonal corner turret and its great pinnacle, reaching the full height of the tower+function buildTowerCorner() {+ const set = mkBatches(), { A, F } = set, y0 = WF.topY, yT = P.towerTop - 1.3, R = 1.72;+ // The turret grows out of the corner on a tapering corbel, closed underneath; its lower part sits inside the corner pier+ A.lathe([[0.04, y0 - 4.2], [R * 0.66, y0 - 2.6], [R, y0 - 1], [R, yT]], 8, { faceted: true, phase: PI / 8 });+ for (let i = 0; i < 8; i++) {+ const a = i * 45 + 22.5, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));+ F.at(M.of(M.Ry(a), M.T(0, 0, ap)), () => {+ if (i % 2 === 0) for (const k of [0, 1]) {+ const yb = y0 + 1 + k * 11;+ F.sweep(pointedArc(side * 0.72, 0.9, 4).map((p) => [p[0], p[1] + yb + 8.2]), hexBar(0.1, 0.12), { caps: false });+ for (const s of [-1, 1]) F.sweep([[s * side * 0.36, yb], [s * side * 0.36, yb + 8.2]], hexBar(0.1, 0.12), { caps: false });+ F.shade(0.55, () => F.face([[-side * 0.36, yb, 0.01], [side * 0.36, yb, 0.01], [side * 0.36, yb + 8.2, 0.01], [0, yb + 9.05, 0.01], [-side * 0.36, yb + 8.2, 0.01]]));+ }+ });+ }+ F.lathe([[R, y0 + 12.3], [R + 0.28, y0 + 12.5], [R + 0.28, y0 + 12.66], [R, y0 + 12.82]], 8, { faceted: true, phase: PI / 8 });+ F.lathe([[R, yT - 0.2], [R + 0.36, yT], [R + 0.36, yT + 0.26], [R, yT + 0.42]], 8, { faceted: true, phase: PI / 8 });+ return set;+}+function buildWestFront() {+ const gWest = group('West_Front');+ gWest.position.set(LAY.xW, 0, 0); gWest.rotation.y = rad(90);+ const set = mkBatches(), { A, F, L, G, D } = set, hw = WF.halfW, yTop = WF.topY, tcx = 14.5;+ // Real recessed lights flanking the tower windows, cut through the face of the wall+ const towerRecess = [];+ for (const s of [-1, 1]) for (const d of [-1, 1]) towerRecess.push(archOutline(1.5, WIN.cSill + 0.6, WIN.cSp - 0.6, 1.8, 6).map((p) => [s * tcx + d * 3.55 + p[0], p[1]]));+ // ---- The great wall with real openings: doors and rose ----+ const cP = WF.portalC, sP = WF.portalS;+ const doorHole = (x, w, h) => [[x - w / 2, 0.02], [x + w / 2, 0.02], [x + w / 2, h], [x - w / 2, h]];+ const wallOutline = [[-hw, 0], [-sP.x - sP.w0 / 2, 0], [-sP.x - sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, sP.ys - 0.9], [-sP.x + sP.w0 / 2, 0], [-cP.w0 / 2, 0], [-cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, cP.ys - 0.9], [cP.w0 / 2, 0], [sP.x - sP.w0 / 2, 0], [sP.x - sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, sP.ys - 0.9], [sP.x + sP.w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]];+ A.slab(wallOutline, [ringPts(0, WF.roseY, WF.roseR, 40), ...towerRecess], -WF.wallT, 0, { noOuterSides: true, sr: 0.62 });+ towerRecess.forEach((o) => addRecess(A, F, o, 0.5));+ // Seven centuries of rain under every ledge of the front, damp rising from the ground+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.02, 3.2);+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.02, 3.6);+ addStain(D, -hw, hw, WF.galY - 0.42, 0.02, 3.4);+ addStain(D, -hw, hw, WF.kingTop + 0.02, 0.02, 2.6, 'drip');+ // damp on the wall only: the door openings are left clear+ for (const span of [[-hw, -sP.x - sP.w0 / 2], [-sP.x + sP.w0 / 2, -cP.w0 / 2], [cP.w0 / 2, sP.x - sP.w0 / 2], [sP.x + sP.w0 / 2, hw]]) addStain(D, span[0], span[1], 5.2, 0.03, 5.2, 'ground');+ // Each ledge throws a soft shadow on the wall just below it; soot gathers where the rain cannot reach+ addStain(D, -6.6, 6.6, WF.roseY - 6.9, 0.03, 0.8, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.56, 0.03, 1.2, 'soft');+ addStain(D, -hw, hw, WF.galY - 0.42, 0.03, 1.0, 'soft');+ addStain(D, -hw, hw, WF.kingY - 0.62, 0.04, 2.4, 'soot');+ for (const s of [-1, 1]) addStain(D, s * tcx - WIN.cW / 2 - 0.4, s * tcx + WIN.cW / 2 + 0.4, WIN.cSill - 0.12, 0.02, 3.4);+ // ---- Rose: moulded ring, tracery, spandrels, relieving arch ----+ place('Great_Rose_Tracery', roseTracery(), MAT.fine, { pos: [0, WF.roseY, -0.4], parent: gWest });+ F.at(M.T(0, WF.roseY, 0), () => {+ F.at(M.T(0, 0, 0), () => {+ const ring = traceryBar(0.46, 0.58);+ F.sweep(ringPts(0, 0, WF.roseR + 0.32, 40), ring, { closed: true, z: 0.14 });+ F.sweep(ringPts(0, 0, WF.roseR + 0.78, 44), rollSection(0.2, 0.3, 1, 0.04, 2), { closed: true, z: 0.1 });+ });+ });+ F.at(M.T(0, 0, 0.03), () => {+ // spandrels round the rose: plain stone, each with a real pierced roundel carved in its corner+ const R2 = WF.roseR + 0.72, ry = WF.roseY, sq = 6.6;+ for (const [sx, sy] of [[1, 1], [-1, 1], [1, -1], [-1, -1]]) {+ // corner of the square -> along its edge -> round the rose -> along the other edge+ const arcPts = []; for (let i = 0; i <= 8; i++) { const a = i / 8 * PI / 2; arcPts.push([sx * R2 * Math.cos(a), sy * R2 * Math.sin(a)]); }+ const poly = [[sx * sq, sy * sq], [sx * sq, 0], ...arcPts, [0, sy * sq]].map((p) => [p[0], p[1] + ry]);+ F.slab(area2(poly) > 0 ? poly : poly.slice().reverse(), [], -0.04, 0, { noOuterSides: true, noBack: true });+ const cx = sx * (sq - 1.02), cy = ry + sy * (sq - 1.02);+ F.sweep(ringPts(cx, cy, 0.76, 14), traceryBar(0.13, 0.2), { closed: true, z: 0.05 });+ const foil = []; for (let i = 0; i < 16; i++) { const a = i * TAU / 16, R = 0.68 * (0.62 + 0.32 * Math.abs(Math.cos(a * 2))); foil.push([cx + R * Math.cos(a), cy + R * Math.sin(a)]); }+ F.sweep(foil, hexBar(0.07, 0.14), { closed: true, z: 0.05, crease: 40 });+ }+ });+ F.at(M.T(0, 0, 0.14), () => { F.sweep([[-6.6, WF.roseY - 6.6], [6.6, WF.roseY - 6.6]], stringSection(0.3, 0.26), { caps: false }); F.sweep([[-6.6, WF.roseY + 6.6], [6.6, WF.roseY + 6.6]], stringSection(0.3, 0.26), { caps: false }); });+ // ---- Tower lower stages: paired windows with hood and gable, string courses ----+ const cw = WIN.cW, cSill = WIN.cSill, cSp = WIN.cSp, cRise = WIN.cRise;+ for (const s of [-1, 1]) {+ F.at(M.T(s * tcx, 0, 0.32), () => {+ addTracery(F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ F.sweep(archOutline(cw, cSill, cSp, cRise, 8), rollSection(0.24, 0.5, 1, 0.04, 2), { closed: true, crease: 40, z: -0.14 });+ F.sweep(pointedArc(cw + 0.8, cRise + 0.4, 7).map((p) => [p[0], p[1] + cSp]), hexBar(0.26, 0.3), { z: 0.1, caps: false });+ addGable(F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 4, lod: 0 });+ });+ F.at(M.T(s * tcx, 0, 0.02), () => { F.sweep([[-6.1, WF.kingTop + 0.24], [6.1, WF.kingTop + 0.24]], stringSection(0.3, 0.22), { caps: false }); });+ }+ // ---- Buttresses of the front: one continuous pier between the portals, narrowing as it climbs through the kings and+ // the gallery; the outer corners of the towers carry a single diagonal pier instead of two that would collide ----+ const xPier = [-7.5, 7.5], lowPanel = { k: 0.6, b: 1.1, t: 7.2, r: 0.7 };+ xPier.forEach((x, i) => {+ const m = M.T(x, 0, 0);+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: 0, y1: 11.6, w: 1.05, f: 3.6, panel: lowPanel }, { y0: 12.9, y1: 18.4, w: 0.98, f: 2.7 }, { y0: 19.7, y1: 19.72, w: 0.92, f: 1.2, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 3.6), () => { F.box(-1.17, 1.17, 0, 0.74, -3.7, 0.12, { noBot: true }); addNicheFrame(F, 0, 6.1, 11.0, 1.42); });+ // above the kings the pier stands back on the gallery's roof+ A.at(m, () => F.at(m, () => addPier(set, [{ y0: WF.kingTop + 0.1, y1: 38.8, w: 0.92, f: 1.26 }, { y0: 40, y1: yTop + 0.1, w: 0.8, f: 0.96, noDrip: true }], { flat: true })));+ F.at(M.T(x, 0, 1.26), () => addNicheFrame(F, 0, 31, 37.4, 1.3));+ D.at(M.T(x, 0, 3.6), () => { addStain(D, -1.05, 1.05, 11.4, 0.02, 2.2, 'drip'); addStain(D, -1.05, 1.05, 4.6, 0.03, 4.4, 'ground'); addStain(D, -1.05, 1.05, 11.45, 0.03, 0.7, 'soft'); });+ placeFigure('Buttress_Saint' + i, SAINTS[i + 3], { pos: [x, 6.2, 3.98], rot: [0, -90, 0], scale: 1.42, parent: gWest });+ place('Facade_Gargoyle' + i, gargoyle(i % 3), MAT.fine, { pos: [x, 17.2, 2.55], rot: [0, -90, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ placeFigure('Upper_Saint' + i, SAINTS[i + 5], { pos: [x, 31.1, 1.62], rot: [0, -90, 0], scale: 1.32, parent: gWest, lod: 0 });+ });+ for (const s of [-1, 1]) {+ const k = Math.SQRT1_2, z = 3.98, cx = s * (hw + k * z);+ placeFigure('Corner_Saint' + s, SAINTS[s > 0 ? 3 : 5], { pos: [cx, 6.2, k * z], rot: [0, -90 + s * 45, 0], scale: 1.42, parent: gWest });+ place('Corner_Gargoyle' + s, gargoyle(s > 0 ? 1 : 2), MAT.fine, { pos: [s * (hw + k * 2.65), 26.6, k * 2.65], rot: [0, -90 + s * 45, 0], scale: [0.95, 0.95, 0.95], parent: gWest });+ }+ // closing blocks where the gallery of kings dies into the corner piers+ for (const s of [-1, 1]) A.box(Math.min(s * 19.9, s * 20.6), Math.max(s * 19.9, s * 20.6), WF.kingY - 0.5, WF.kingTop + 0.12, 0, 1.3, { noBot: true });+ placeSet('West_Wall', set, [0, 0, 0], 0, gWest);+ // ---- Glass ----+ const roseWest = makeRoseGlass(WF.roseR, 101);+ SHARED.roseGlass = roseWest;+ placeGlass('Great_Rose', roseWest, [0, WF.roseY, -0.4], 180, gWest, true);+ const twinGlass = SHARED.cleres;+ for (const s of [-1, 1]) place('TowerWindow_Out' + s, glassOutside(twinGlass[s > 0 ? 0 : 1]), MAT.glassOutWin, { pos: [s * tcx, 0, 0.14], rot: [0, 180, 0], parent: gWest, scale: [1, 1, -1] });+ // ---- Three portals: a bishop, a crowned king, a veiled queen, an apostle with his key, a smiling angel;+ // the Virgin and Child on the central door post, Christ enthroned above her ----+ const pC = buildPortal(cP, ['apostle', 'bishop', 'queen', 'king', 'apostle', 'king', 'angel', 'bishop', 'queen', 'apostle'], { main: 'seated', side: ['angel', 'queen'], trumeau: 'virgin', trumeauScale: 1.4 });+ const pS = buildPortal(Object.assign({}, sP, { x: 0 }), ['queen', 'bishop', 'apostle', 'king', 'angel', 'apostle', 'king', 'queen'], { main: 'seatedVirgin', side: ['angel', 'apostle'], trumeau: 'bishop', trumeauScale: 1.24 });+ pC.baseX = cP.x; pS.baseX = 0; SHARED.sidePortal = pS;+ placePortal(gWest, 'Portal_Central', cP, pC);+ placePortal(gWest, 'Portal_South', Object.assign({}, sP, { x: -sP.x }), pS, { light: 'scene' });+ placePortal(gWest, 'Portal_North', Object.assign({}, sP, { x: sP.x }), pS, { light: 'scene' });+ // ---- Gallery of Kings ----+ const nK = P.kings, pitch = 2 * 20.1 / nK, niche = buildKingNiche(pitch), kinds = ['kingA', 'kingB'];+ for (let i = 0; i < nK; i++) {+ const x = (i + 0.5 - nK / 2) * pitch;+ placeSet('King_Niche' + i, niche, [x, 0, 0], 0, gWest);+ placeFigure('King' + i, kinds[i % 2], { pos: [x, WF.kingY + 0.04, 0.62], rot: [0, -90 + (i % 4 - 1.5) * 7, 0], scale: 1.7, parent: gWest, mirror: i % 2 === 1 });+ }+ // ---- High gallery ----+ const nG = 20, gp = 2 * 19.75 / nG, gb = buildGalleryBay(gp);+ for (let i = 0; i < nG; i++) placeSet('Gallery' + i, gb, [(i + 0.5 - nG / 2) * gp, 0, 0], 0, gWest);+ // ---- Central gable rising over the gallery, closing the nave roof ----+ const front = mkBatches(), yG0 = yTop + 1.2, yR = LAY.ridge + 1.2, gW = 2 * (zn + 1.5);+ front.A.at(M.T(0, 0, -1.2), () => front.A.slab([[-gW / 2, yG0 - 2], [gW / 2, yG0 - 2], [gW / 2, yG0], [0, yR + 1.6], [-gW / 2, yG0]], [ringPts(0, yG0 + 1.8, 1.25, 12)], -0.5, 0.5, { sr: 0.5 }));+ front.F.at(M.T(0, 0, -0.7), () => {+ addGable(front.F, gW + 0.2, yR + 1.6 - yG0, 0.04, 0.26, yG0, { size: 0.86, finial: 0.1, crockets: 7, noField: true, lod: 1 });+ front.F.sweep(ringPts(0, yG0 + 1.8, 1.32, 14), traceryBar(0.26, 0.44), { closed: true });+ // Blind arcade stepping up under the rake+ const nA = 7; for (let i = 0; i < nA; i++) { const x = (i - (nA - 1) / 2) * 1.72, top = yG0 + 0.3 + (gW / 2 - Math.abs(x) - 1) * (yR + 1.6 - yG0) / (gW / 2) * 0.7; if (Math.abs(x) < 1.7) continue; front.F.sweep([[x - 0.62, yG0], [x - 0.62, top], [x, top + 0.75], [x + 0.62, top], [x + 0.62, yG0]], hexBar(0.14, 0.16), { caps: false, crease: 20 }); }+ });+ front.G.at(M.T(0, yR + 1.62, -0.5), () => addFinial(front.G, 2.2, 1));+ placeSet('West_Gable', front, [0, 0, 0], 0, gWest);+ place('West_Gable_Glass', makeLancetRoundel(1.25), MAT.glassOut, { pos: [0, yG0 + 1.8, -0.75], parent: gWest });+ // ---- Towers: shell, belfry faces, corner turrets, lead cap ----+ // One tower shell (side and rear walls of the lower stages), built outward towards +x; the other tower mirrors it+ const shell = mkBatches(), tw = 7;+ // The outer side of the tower: real recessed arcades in two tiers, either side of its windows+ const sideM = M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), sideRecess = [];+ for (const d of [-1, 1]) {+ sideRecess.push(archOutline(1.2, 3.4, 16.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ sideRecess.push(archOutline(1.2, 26.4, 37.2, 1.6, 6).map((p) => [d * 3.35 + p[0], p[1]]));+ }+ shell.A.at(sideM, () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], sideRecess, -0.8, 0, { noOuterSides: true, noBack: true, sr: 0.62 }));+ shell.A.at(sideM, () => shell.F.at(sideM, () => sideRecess.forEach((o) => addRecess(shell.A, shell.F, o, 0.52))));+ shell.D.at(sideM, () => { for (const y of [19.2, WF.kingTop - 0.34, WF.galY - 0.34]) { addStain(shell.D, -tw, tw, y, 0.02, 3.2); addStain(shell.D, -tw, tw, y, 0.03, 0.8, 'soft'); } addStain(shell.D, -tw, tw, 5.2, 0.03, 5.2, 'ground'); });+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(180), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ shell.A.at(M.of(M.T(0, 0, -tw), M.Ry(-90), M.T(0, 0, tw)), () => shell.A.slab([[-tw, 0], [tw, 0], [tw, yTop], [-tw, yTop]], [], -0.8, 0, { noOuterSides: true, noBack: true }));+ // string courses and tall blind windows on the outer side+ shell.F.at(M.of(M.T(0, 0, -tw), M.Ry(90), M.T(0, 0, tw)), () => {+ for (const y of [1.0, 19.6, WF.kingTop, WF.galY]) shell.F.sweep([[-tw, y], [tw, y]], stringSection(0.3, 0.26), { caps: false });+ addTracery(shell.F, cw, cSill, cSp, cRise, { bar: 0.26, depth: 0.32 });+ shell.F.sweep(archOutline(cw, cSill, cSp, cRise, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ addGable(shell.F, cw + 1.4, 3.3, -0.16, 0.2, cSp + cRise * 0.5, { size: 0.46, finial: 0.8, crockets: 3, lod: 0 });+ addTracery(shell.F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 });+ shell.F.sweep(archOutline(...windowArgs.aisle, 8), hexBar(0.24, 0.5), { closed: true, crease: 40, z: -0.14 });+ });+ // The tower's two outer corners: one diagonal pier each, from the ground to the foot of the turret it carries+ const cornerStages = [{ y0: 0, y1: 13, w: 2.0, f: 3.6 }, { y0: 14.4, y1: 27, w: 1.9, f: 2.8 }, { y0: 28.4, y1: yTop - 1, w: 1.78, f: 2.05 }];+ for (const [zc, turn] of [[0, 45], [-2 * tw, 135]]) {+ const m = M.of(M.T(tw, 0, zc), M.Ry(turn));+ shell.A.at(m, () => shell.F.at(m, () => addPier(shell, cornerStages, { back: 2, flat: true })));+ shell.F.at(m, () => shell.F.box(-2.14, 2.14, 0, 0.74, -2, 3.74, { noBot: true }));+ shell.D.at(M.of(m, M.T(0, 0, 3.6)), () => { addStain(shell.D, -2, 2, 13, 0.02, 3.2); addStain(shell.D, -2, 2, 13.02, 0.03, 0.8, 'soft'); addStain(shell.D, -2, 2, 5, 0.03, 4.3, 'ground'); });+ if (zc === 0) shell.F.at(M.of(m, M.T(0, 0, 3.6)), () => addNicheFrame(shell.F, 0, 6.1, 11, 1.42));+ }+ for (const s of [-1, 1]) {+ const cx = s * tcx;+ placeSet('Tower_Shell', shell, [cx, 0, 0], 0, gWest, { scale: [s, 1, 1] });+ // Set just proud of the wall face: these towers are solid here, so the glass is seen from outside only+ place('Tower_SideGlass_High' + s, glassOutside(twinGlass[2]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ place('Tower_SideGlass_Low' + s, glassOutside(SHARED.stories[1]), MAT.glassOutWin, { pos: [cx + s * (tw + 0.02), 0, -tw], rot: [0, s * 90 + 180, 0], parent: gWest, scale: [1, 1, -1] });+ }+ const face = buildBelfryFace(), corner = buildTowerCorner();+ const cap = new Batch(TILE.lead), capG = new Batch(TILE.gold);+ cap.lathe([[5.9, P.towerTop - 2.5], [0.2, P.towerTop + 4.8]], 8, { faceted: true, phase: PI / 8 });+ capG.at(M.T(0, P.towerTop + 4.7, 0), () => addFinial(capG, 2.4, 1));+ for (const s of [-1, 1]) {+ const gT = group('Tower_' + (s > 0 ? 'North' : 'South'), gWest), cx = s * tcx, cz = -tw;+ for (let i = 0; i < 4; i++) {+ placeSet('Belfry_Face' + i, face, [cx, 0, cz], i * 90, gT);+ const ax = cx + (i === 0 || i === 3 ? 1 : -1) * tw, az = cz + (i < 2 ? 1 : -1) * tw;+ placeSet('Tower_Turret' + i, corner, [ax, 0, az], i * 90, gT);+ // the great corner pinnacles carry the tower to its full height+ const yT = P.towerTop - 1.3 + 0.4, hP = P.spireTip - yT;+ place('Tower_Pinnacle' + i, largePinnacle(), MAT.fine, { pos: [ax, yT, az], scale: [1.16, hP / 10, 1.16], parent: gT });+ }+ place('Tower_Cap_Lead', cap, MAT.lead, { pos: [cx, 0, cz], parent: gT });+ place('Tower_Cap_Gilt', capG, MAT.gold, { pos: [cx, 0, cz], parent: gT });+ }+ // ---- Inner face: blind niches with statues, a gallery, the back of the doors ----+ const inner = mkBatches();+ inner.F.at(M.of(M.T(0, 0, -WF.wallT), M.Ry(180)), () => {+ inner.F.sweep([[-13.1, 10.2], [13.1, 10.2]], stringSection(0.34, 0.34), { caps: false });+ inner.F.sweep([[-6.5, P.triforiumSill], [6.5, P.triforiumSill]], stringSection(0.34, 0.3), { caps: false });+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) addNicheFrame(inner.F, s * (3.65 + i * 1.5), 3.2 + 0, 8.8, 1.2);+ inner.F.sweep(ringPts(0, WF.roseY, WF.roseR + 0.3, 32), traceryBar(0.42, 0.5), { closed: true });+ addBlindArcade(inner.F, -6.5, 6.5, 11, 15.2, 8, { bar: 0.14, depth: 0.2 });+ });+ placeSet('West_InnerFace', inner, [0, 0, 0], 0, gWest);+ for (const s of [-1, 1]) for (let i = 0; i < 2; i++) placeFigure('Inner_Saint_' + (s > 0 ? 'N' : 'S') + i, SAINTS[i + (s > 0 ? 1 : 6)], { pos: [s * (3.65 + i * 1.5), 3.25, -WF.wallT - 0.28], rot: [0, 90, 0], scale: 1.32, parent: gWest, indoor: true });+}+function makeLancetRoundel(r) { const g = new Glass(55); g.raw(ringPts(0, 0, r + 0.05, 20), 'lead', -0.4, 0); gOculus(g, 0, 0, r, { ground: 'cobalt', inner: 'gold' }); return g.b; }++// ---- Transept fronts: one portal, a smaller rose, lancets, gable, corner turrets ----------+function buildTransFront(gT, s) {+ const fr = frame(s > 0 ? 0 : 180, LAY.xc, 0), hw = zaw + 0.9, zEnd = zn + (P.transeptBays + 1) * BAY, yTop = P.eaves;+ const set = TRANS.set || (TRANS.set = (() => {+ const q = mkBatches();+ const { A, F, G } = q, rY = 28.6, R = 4.5, w0 = WF.portalS.w0, dh = WF.portalS.ys - 0.9;+ const at = M.T(0, 0, zEnd);+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-hw, 0], [-w0 / 2, 0], [-w0 / 2, dh], [w0 / 2, dh], [w0 / 2, 0], [hw, 0], [hw, yTop], [-hw, yTop]], [ringPts(0, rY, R, 32)], -0.9, 0, { noOuterSides: true, sr: 0.62 }));+ F.at(M.of(at, M.T(0, rY, 0.9)), () => { F.sweep(ringPts(0, 0, R + 0.3, 32), traceryBar(0.4, 0.5), { closed: true }); F.sweep(ringPts(0, 0, R + 0.72, 36), rollSection(0.18, 0.26, 1, 0.03, 2), { closed: true }); });+ q.D.at(M.of(at, M.T(0, 0, 0.9)), () => { for (const [a, c] of [[-hw, -12.3], [-6.9, 6.9], [12.3, hw]]) { addStain(q.D, a, c, 17.2, 0.02, 3.2); addStain(q.D, a, c, 17.2, 0.03, 0.8, 'soft'); } addStain(q.D, -hw, hw, 22.6, 0.02, 2.8, 'drip'); addStain(q.D, -hw, hw, 22.6, 0.03, 0.7, 'soft'); addStain(q.D, -hw, -w0 / 2, 5.2, 0.03, 5.2, 'ground'); addStain(q.D, w0 / 2, hw, 5.2, 0.03, 5.2, 'ground'); });+ // Row of lancets beneath the rose+ F.at(M.of(at, M.T(0, 0, 0.9)), () => {+ F.sweep([[-hw, 0.8], [hw, 0.8]], stringSection(0.42, 0.36), { caps: false });+ // This string runs up to the hood of each window head and stops there: it never crosses the tracery+ const hood = 9.6 - 1.62, outerHood = 9.6 + 1.62;+ F.sweep([[-hood, 17.4], [hood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ for (const t of [-1, 1]) F.sweep(t > 0 ? [[outerHood, 17.4], [hw, 17.4]] : [[-hw, 17.4], [-outerHood, 17.4]], stringSection(0.34, 0.3), { caps: false });+ F.sweep([[-hw, 22.8], [hw, 22.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -8, 8, 18, 20.8, 8, { bar: 0.16, depth: 0.2 });+ for (const x of [-9.6, 9.6]) F.at(M.T(x, 0, 0.1), () => { addTracery(F, ...windowArgs.aisle, { bar: 0.25, depth: 0.3 }); F.sweep(archOutline(...windowArgs.aisle, 7), hexBar(0.22, 0.42), { closed: true, crease: 40, z: -0.1 }); });+ });+ // The inner face of the end wall: tiers of blind arcading and string courses+ F.at(M.of(at, M.Ry(180)), () => {+ F.sweep([[-zaw, 11], [zaw, 11]], stringSection(0.3, 0.26), { caps: false });+ F.sweep([[-zaw, 16.8], [zaw, 16.8]], stringSection(0.3, 0.26), { caps: false });+ addBlindArcade(F, -5.6, 5.6, 1.2, 7.8, 5, { bar: 0.16, depth: 0.2 });+ addBlindArcade(F, -zn, zn, 11.2, 14.8, 8, { bar: 0.12, depth: 0.16 });+ addBlindArcade(F, -zn, zn, 17, 20.6, 8, { bar: 0.12, depth: 0.16 });+ });+ // Gable closing the arm's roof, in front of the lead+ const yG = yTop, gW = 2 * (zn + 1.6), yR = LAY.ridge + 1;+ A.at(M.of(at, M.T(0, 0, 0.9)), () => A.slab([[-gW / 2, yG], [gW / 2, yG], [0, yR]], [ringPts(0, yG + 3.4, 1.5, 12)], -0.9, 0, { noOuterSides: true, sr: 0.5 }));+ F.at(M.of(at, M.T(0, 0, 0.9)), () => { addGable(F, gW + 0.2, yR - yG, 0.02, 0.24, yG, { size: 0.82, finial: 0.1, crockets: 7, noField: true, lod: 1 }); F.sweep(ringPts(0, yG + 3.4, 1.55, 14), traceryBar(0.24, 0.42), { closed: true }); });+ G.at(M.of(at, M.T(0, yR, 1.12)), () => addFinial(G, 2.0, 1));+ F.at(M.of(at, M.T(0, 0, 1.0)), () => addParapet(F, -hw, hw, yTop - 0.1, 1.1, 0.24, { pitch: 1.3, nh: 8 }));+ // Each corner of the front carries one diagonal pier rising to its pinnacle, tied into both walls+ for (const sx of [-1, 1]) {+ const m = M.of(M.T(sx * hw, 0, zEnd + 0.9), M.Ry(sx * 45));+ A.at(m, () => F.at(m, () => addPier(q, [{ y0: 0, y1: 14.6, w: 1.7, f: 2.7 }, { y0: 15.8, y1: 30.6, w: 1.58, f: 2.05 }, { y0: 31.8, y1: yTop + 1, w: 1.45, f: 1.5 }], { back: 1.7, flat: true })));+ F.at(m, () => F.box(-1.84, 1.84, 0, 0.74, -1.7, 2.84, { noBot: true }));+ F.at(M.of(m, M.T(0, 0, 2.05)), () => addNicheFrame(F, 0, 16.2, 22, 1.5));+ q.D.at(M.of(m, M.T(0, 0, 2.7)), () => { addStain(q.D, -1.7, 1.7, 14.6, 0.02, 3); addStain(q.D, -1.7, 1.7, 14.62, 0.03, 0.8, 'soft'); addStain(q.D, -1.7, 1.7, 5, 0.03, 4.3, 'ground'); });+ }+ return q;+ })());+ placeSet('Transept_Front', set, [fr.ox, 0, fr.oz], fr.th, gT);+ // Portal (the same carving as the west side portals), statue niches, glass+ const gPortal = group('Transept_Portal', gT);+ gPortal.position.set(...fr.pos(0, 0, zEnd + 0.9)); gPortal.rotation.y = rad(fr.th);+ placePortal(gPortal, 'Portal_Transept', { x: 0 }, SHARED.sidePortal, { light: 'all' });+ place('Transept_Rose_Tracery', roseTracery(), MAT.fine, { pos: fr.pos(0, 28.6, zEnd + 0.45), rot: fr.rot(0), scale: [4.5 / WF.roseR, 4.5 / WF.roseR, 1], parent: gT });+ for (const sx of [-1, 1]) place('Transept_Pinnacle', largePinnacle(), MAT.fine, { pos: fr.pos(sx * hw, yTop + 1, zEnd + 0.9), scale: [1.05, 1.1, 1.05], parent: gT });+ placeGlass('Transept_Rose', SHARED.roseGlass, fr.pos(0, 28.6, zEnd + 0.45), fr.th + 180, gT, false, 4.5 / WF.roseR);+ const low = SHARED.stories[0];+ for (const sx of [-1, 1]) {+ // Aisle-end windows: the same panes on the inner and outer faces of the end wall+ place('Transept_EndWindow_In', low, MAT.glassIn, { pos: fr.pos(sx * 9.6, 0, zEnd - 0.01), rot: fr.rot(180), parent: gT });+ place('Transept_EndWindow_Out', glassOutside(low), MAT.glassOutWin, { pos: fr.pos(sx * 9.6, 0, zEnd + 0.92), rot: fr.rot(180), scale: [1, 1, -1], parent: gT });+ const dz = Math.SQRT1_2 * 2.37;+ placeFigure('Transept_Corner_Saint' + sx, SAINTS[sx > 0 ? 2 : 4], { pos: fr.pos(sx * (hw + dz), 16.25, zEnd + 0.9 + dz), rot: fr.rot(-90 + sx * 45), scale: 1.42, parent: gT, lod: 0 });+ }+}+const TRANS = {};++// ---- The east end: raised sanctuary, high altar with a gilded retable, oak choir stalls ------+function buildSanctuary() {+ const g = group('Sanctuary'), set = mkBatches(), { F, G, O } = set, { xa, xChoirW } = LAY;+ // Three broad steps and the altar, all moulded+ for (let i = 0; i < 3; i++) { const r = 5 - i * 0.7; F.at(M.T(xa + 1, 0, 0), () => F.lathe([[r, i * 0.2], [r, (i + 1) * 0.2], [0.01, (i + 1) * 0.2]], 14, { faceted: true, phase: PI / 14 })); }+ const y0 = 0.6, ax = xa + 1;+ F.at(M.T(ax, y0, 0), () => {+ F.box(-0.8, 0.8, 0, 1.0, -1.7, 1.7, {});+ F.box(-0.92, 0.92, 1.0, 1.12, -1.84, 1.84, { sb: 0.5 });+ for (const s of [-1, 1]) for (const t of [-1, 0, 1]) F.at(M.T(s * 0.82, 0, t * 1.1), () => F.lathe([[0.12, 0], [0.07, 0.12], [0.07, 0.88], [0.13, 1.0]], 6));+ });+ // A carved retable behind the altar: five stone niches under gables, each with its figure, only the arches gilded+ const retM = M.of(M.T(ax - 1.0, y0 + 1.12, 0), M.Ry(90));+ F.at(retM, () => {+ F.box(-2.46, 2.46, -0.26, 0, -0.2, 0.14, {});+ for (let i = -2; i <= 2; i++) {+ const x = i * 0.96, h = i === 0 ? 2.2 : 1.56;+ F.box(x - 0.47, x + 0.47, 0, h - 0.3, -0.2, -0.1, {});+ addGable(F, 0.96, i === 0 ? 0.8 : 0.66, 0.04, 0.06, h - 0.32, { size: 0.15, crockets: 2, finial: 0.24, solid: true, lod: 0 });+ F.at(M.T(x - 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ if (i === 2) F.at(M.T(x + 0.48, 0, 0), () => F.lathe([[0.075, 0], [0.05, 0.08], [0.05, h - 0.42], [0.08, h - 0.32]], 6));+ F.at(M.T(x - 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ if (i === 2) F.at(M.T(x + 0.48, h - 0.32, 0), () => addPinnacle(F, 0.13, 0.78, { simple: 1 }));+ }+ });+ G.at(retM, () => { for (let i = -2; i <= 2; i++) { const x = i * 0.96, h = i === 0 ? 2.2 : 1.56; G.sweep(pointedArc(0.82, 0.52, 4).map((p) => [x + p[0], h - 0.84 + p[1]]), hexBar(0.04, 0.05), { z: 0.03, caps: false }); } });+ const altarSaints = [['apostle', 0.56], ['bishop', 0.56], ['seatedVirgin', 0.74], ['king', 0.56], ['queen', 0.56]];+ altarSaints.forEach((q, n) => placeFigure('Retable_Figure' + n, q[0], { pos: [ax - 1.04, y0 + 1.14, -(n - 2) * 0.96], rot: [0, 0, 0], scale: q[1], parent: g, indoor: true, lod: q[0] === 'seatedVirgin' ? 1 : 0 }));+ // Candlesticks+ for (const t of [-1.5, -0.9, -0.3, 0.3, 0.9, 1.5]) G.at(M.T(ax - 0.2, y0 + 1.12, t), () => { G.lathe([[0.1, 0], [0.04, 0.06], [0.025, 0.42], [0.06, 0.46], [0.03, 0.5]], 6); F.at(M.T(ax - 0.2, y0 + 1.62, t), () => F.lathe([[0.022, 0], [0.022, 0.34], [0.002, 0.36]], 5, { s: 1.6 })); });+ // Choir stalls: stepped oak benches, moulded backs and a carved canopy rail+ const x0 = xa + 5.5, x1 = xChoirW - 1.2;+ for (const s of [-1, 1]) {+ const z0 = s * 5.0;+ O.box(x0, x1, 0, 0.32, z0 - 0.9, z0 + 0.9, { s: 0.42 });+ O.box(x0, x1, 0.32, 0.5, z0 + (s > 0 ? 0.0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.46 });+ O.box(x0, x1, 0, 2.7, z0 + (s > 0 ? 0.8 : -0.9), z0 + (s > 0 ? 0.9 : -0.8), { s: 0.4 });+ const n = Math.floor((x1 - x0) / 1.5);+ for (let i = 0; i <= n; i++) {+ const x = x0 + i * (x1 - x0) / n;+ O.box(x - 0.045, x + 0.045, 0, 0.98, z0 + (s > 0 ? 0 : -0.9), z0 + (s > 0 ? 0.9 : 0), { s: 0.5 });+ O.box(x - 0.03, x + 0.03, 0.98, 2.72, z0 + (s > 0 ? 0.74 : -0.8), z0 + (s > 0 ? 0.8 : -0.74), { s: 0.48 });+ O.box(x - 0.08, x + 0.08, 2.72, 3.28, z0 + (s > 0 ? 0.56 : -0.86), z0 + (s > 0 ? 0.86 : -0.56), { s: 0.46, taper: 0.05 });+ }+ O.box(x0, x1, 2.7, 2.84, z0 + (s > 0 ? 0.3 : -0.9), z0 + (s > 0 ? 0.9 : -0.3), { s: 0.44 });+ }+ placeSet('Sanctuary', set, [0, 0, 0], 0, g);+}++// ==================================================================================+// THE GROUND. One named group, separate from the building and left out of its light, so it can simply be deleted+// when the cathedral is placed in a town. All of it stands a few centimetres above y = 0.+// ==================================================================================+const GROUND = { flagY: 0.03, earthY: 0.015, flags: 20, earth: 33, stepUp: 0.14 };+function footprintDistance(x, z) { // signed distance in plan to the outer walls of the building+ const { xa, xW, xTowerE, xc } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const box = (x0, x1, z0, z1) => { const dx = Math.max(x0 - x, x - x1), dz = Math.max(z0 - z, z - z1); return dx <= 0 && dz <= 0 ? Math.max(dx, dz) : Math.sqrt(Math.max(dx, 0) ** 2 + Math.max(dz, 0) ** 2); };+ return Math.min(box(xa, xW, -14, 14), box(xTowerE, xW, -WF.halfW, WF.halfW), box(xc - 14, xc + 14, -zEnd, zEnd), Math.sqrt((x - xa) * (x - xa) + z * z) - (CHEV.zc + CHEV.Rc + 0.5));+}+function buildGround() {+ const g = group('Ground'), { xa, xW, xc, xTowerE } = LAY, zEnd = zn + (P.transeptBays + 1) * BAY + 0.9;+ const fl = new Batch(TILE.flag), ea = new Batch(TILE.earth), st = new Batch(TILE.flag), gr = new Batch(1); gr.noColor = true;+ fl.noVoxel = ea.noVoxel = st.noVoxel = true;+ const wob = (x, z) => 2.2 * Math.sin(x * 0.31 + 1.3) * Math.cos(z * 0.27) + 1.4 * Math.sin(x * 0.13 + z * 0.19);+ const cell3 = (b, x0, z0, s, y) => { const q = [[x0, z0], [x0, z0 + s], [x0 + s, z0 + s], [x0 + s, z0]].map((p) => b.v(p[0], y(p[0], p[1]), p[1], 0, 1, 0, p[0], p[1], 1)); b.quad(q[0], q[1], q[2], q[3]); };+ // Worn flags from the walls out for about 20 m. Their edge breaks up slab by slab on the earth; nothing is left under the church floor+ const flagKeep = (x, z) => { const d = footprintDistance(x, z); return d > -2.5 && d < GROUND.flags + wob(x, z); };+ for (let gx = Math.floor((xa - 62) / 3); gx * 3 < xW + 62; gx++) for (let gz = -24; gz < 24; gz++) {+ const x0 = gx * 3, z0 = gz * 3, d = footprintDistance(x0 + 1.5, z0 + 1.5), lim = GROUND.flags + wob(x0 + 1.5, z0 + 1.5);+ if (Math.abs(d - lim) < 3.2 && d > 5) { for (const [a, c] of [[0, 0], [0, 1.5], [1.5, 0], [1.5, 1.5]]) if (flagKeep(x0 + a + 0.75, z0 + c + 0.75)) cell3(fl, x0 + a, z0 + c, 1.5, () => GROUND.flagY); } else if (flagKeep(x0 + 1.5, z0 + 1.5)) cell3(fl, x0, z0, 3, () => GROUND.flagY);+ }+ // Rough grass and packed earth under and beyond them, its edge dissolving so it sits on any ground plane without a seam+ ea.fade = (x, z) => sstep(GROUND.earth + 8, GROUND.earth - 3, footprintDistance(x, z) - wob(z, x));+ for (let gx = Math.floor((xa - 62) / 4); gx * 4 < xW + 62; gx++) for (let gz = -18; gz < 18; gz++) {+ const x0 = gx * 4, z0 = gz * 4, d = footprintDistance(x0 + 2, z0 + 2);+ if (d > GROUND.flags - 8 && d < GROUND.earth + 11) cell3(ea, x0, z0, 4, () => GROUND.earthY);+ }+ // A step or two up to every door: a landing that runs in to the door between the jambs, and a lower step in front of it+ const steps = (m, half, zF) => st.at(m, () => {+ st.box(-half - 0.2, half + 0.2, GROUND.flagY, GROUND.flagY + 2 * GROUND.stepUp, -0.2, zF + 0.75, { noBot: true });+ st.box(-half - 0.62, half + 0.62, GROUND.flagY, GROUND.flagY + GROUND.stepUp, zF + 0.75, zF + 1.5, { noBot: true });+ });+ const west = M.of(M.T(xW, 0, 0), M.Ry(90)), cP = WF.portalC, sP = WF.portalS, outer = (c) => (c.w0 + 2 * c.orders * c.dx) / 2;+ steps(west, outer(cP), cP.orders * cP.dz);+ for (const s of [-1, 1]) steps(M.of(west, M.T(s * sP.x, 0, 0)), outer(sP), sP.orders * sP.dz);+ for (const s of [0, 180]) steps(M.of(M.T(xc, 0, 0), M.Ry(s), M.T(0, 0, zEnd)), outer(sP), sP.orders * sP.dz);+ // Dark grime where the walls meet the ground: a soft band lying on the flags along every wall foot+ const strip = (ax, az, bx, bz, w) => {+ const R = DECAL_UV.soft, dx = bx - ax, dz = bz - az, l = Math.sqrt(dx * dx + dz * dz) || 1, nx = dz / l, nz = -dx / l, y = GROUND.flagY + 0.012, n = Math.max(1, Math.round(l / 5)), span = R[2] - R[0];+ for (let i = 0; i < n; i++) {+ const a = i / n, c = (i + 1) / n, u0 = R[0] + (i % 3) * span * 0.22, u1 = u0 + span * 0.3, pa = [ax + dx * a, az + dz * a], pc = [ax + dx * c, az + dz * c];+ const q = [gr.v(pa[0], y, pa[1], 0, 1, 0, u0, R[1], 1), gr.v(pa[0] + nx * w, y, pa[1] + nz * w, 0, 1, 0, u0, R[3], 1), gr.v(pc[0] + nx * w, y, pc[1] + nz * w, 0, 1, 0, u1, R[3], 1), gr.v(pc[0], y, pc[1], 0, 1, 0, u1, R[1], 1)];+ gr.quad(q[0], q[1], q[2], q[3]);+ }+ };+ // every strip runs with the wall on its left as seen from above, so its band falls outward+ const loop = (pts, w) => { for (let i = 0; i < pts.length - 1; i++) strip(pts[i][0], pts[i][1], pts[i + 1][0], pts[i + 1][1], w); };+ const hx = WF.halfW, xE = xc + 14, xCE = xc - 14;+ loop([[xW, hx], [xTowerE, hx], [xTowerE, 14], [xE, 14], [xE, zEnd], [xCE, zEnd], [xCE, 14], [xa, 14]], 1.8);+ loop([[xa, -14], [xCE, -14], [xCE, -zEnd], [xE, -zEnd], [xE, -14], [xTowerE, -14], [xTowerE, -hx], [xW, -hx], [xW, hx]], 1.8);+ for (let k = 1; k < P.apseSides; k++) { const f0 = frame(-k * CHEV.d, xa, 0), a = f0.pos(-0.98, 0, BUTTRESS.zB + 0.2), c = f0.pos(0.98, 0, BUTTRESS.zB + 0.2); strip(a[0], a[2], c[0], c[2], 1.3); }+ for (let k = 0; k < P.apseSides; k++) { const r = CHEV.zc + CHEV.Rc + 0.55, n = 5; for (let i = 0; i < n; i++) { const f0 = frame(-(k + 0.5) * CHEV.d, xa, 0), a0 = rad(-108 + i * 216 / n), a1 = rad(-108 + (i + 1) * 216 / n), R2 = CHEV.Rc + 0.55, p = f0.pos(R2 * Math.sin(a0), 0, CHEV.zc + R2 * Math.cos(a0)), q = f0.pos(R2 * Math.sin(a1), 0, CHEV.zc + R2 * Math.cos(a1)); strip(p[0], p[2], q[0], q[2], 1.2); } }+ place('Ground_Flagstones', fl, MAT.flag, { parent: g });+ place('Ground_Earth', ea, MAT.earth, { parent: g });+ place('Ground_Steps', st, MAT.flag, { parent: g });+ place('Ground_Grime', gr, MAT.decal, { parent: g });+}++function buildCathedral() {+ buildBody();+ buildWestFront();+ const gTN = ROOT.children.find((c) => c.name === 'Transept_North'), gTS = ROOT.children.find((c) => c.name === 'Transept_South');+ buildTransFront(gTN, 1);+ buildTransFront(gTS, -1);+ buildSanctuary();+ buildGround();+}++function previewStatues() {+ const names = P.statueTest;+ names.forEach((n, i) => placeFigure('Fig_' + n, n, { pos: [0, 0, (i - (names.length - 1) / 2) * 1.0] }));+ const base = new Batch(TILE.ashlar); base.box(-3, 3, -0.4, 0, -6, 6, { s: 0.8 }); place('Ground', base, MAT.ashlar);+}++// ==================================================================================+// BAKED LIGHT. The viewer casts no shadows, so the shadows are computed here. Every opaque surface is voxelized+// into the world, then each vertex looks at the sun, the sky and the rain:+// - rays towards the sun give the low raking light and the cast shadows;+// - rays over the hemisphere give the depth of portals and galleries, and tell inside from outside;+// - a ray straight up finds what the rain never reaches, where seven centuries of soot have stayed.+// A batch placed many times can have one set of vertex colours per side of the building, within a byte budget.+// Sets beyond the budget share the sunniest colours under a darker material.+// ==================================================================================+const SKY_RAYS = (() => {+ const r = [[0, 1, 0, 1]];+ for (let i = 0; i < 4; i++) { const a = i * TAU / 4 + 0.4, e = rad(42); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 1]); }+ for (let i = 0; i < 5; i++) { const a = i * TAU / 5 + 0.1, e = rad(9); r.push([Math.cos(a) * Math.cos(e), Math.sin(e), Math.sin(a) * Math.cos(e), 0]); }+ return r;+})();+// Large meshes of moulding keep their shape with a quarter fewer triangles: the bytes are better spent up close+async function reduceBatches(share = 0.74, repeatedShare = 0.62, minTris = 2000) {+ const seen = new Map(), uses = new Map();+ for (const e of PLACED) uses.set(e.batch, (uses.get(e.batch) || 0) + 1);+ for (const e of PLACED) {+ const m = e.mat;+ if (e.batch.isFigure || e.batch.noReduce || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.glassOutWin || m === MAT.glassRose || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave || m === MAT.flag || m === MAT.earth) continue;+ let nb = seen.get(e.batch);+ if (nb === undefined) {+ const tris = e.batch.I.length / 3, s = uses.get(e.batch) >= 8 ? repeatedShare : share; // a mesh repeated down the whole building costs the most+ nb = tris > minTris ? await lightCopy(e.batch, Math.round(tris * s)) : null;+ if (nb) { nb.noColor = e.batch.noColor; nb.pierShaft = e.batch.pierShaft; }+ seen.set(e.batch, nb);+ }+ if (nb) { e.batch = nb; e.part.geometry = nb.toGeo(); }+ }+}+function samplesOf(b) {+ if (b.samples) return b.samples;+ const P0 = b.P, out = [], lim = 0.32;+ for (let i = 0; i < P0.length; i++) out.push(P0[i]);+ for (let t = 0; t < b.I.length; t += 3) {+ const a = b.I[t] * 3, c = b.I[t + 1] * 3, d = b.I[t + 2] * 3;+ const e1 = len3(P0[a] - P0[c], P0[a + 1] - P0[c + 1], P0[a + 2] - P0[c + 2]), e2 = len3(P0[c] - P0[d], P0[c + 1] - P0[d + 1], P0[c + 2] - P0[d + 2]), e3 = len3(P0[d] - P0[a], P0[d + 1] - P0[a + 1], P0[d + 2] - P0[a + 2]), m = Math.max(e1, e2, e3);+ if (m <= lim) continue;+ const n = Math.ceil(m / lim);+ for (let i = 0; i <= n; i++) for (let j = 0; j <= n - i; j++) { const u = i / n, v = j / n, w = 1 - u - v; out.push(P0[a] * u + P0[c] * v + P0[d] * w, P0[a + 1] * u + P0[c + 1] * v + P0[d + 1] * w, P0[a + 2] * u + P0[c + 2] * v + P0[d + 2] * w); }+ }+ b.samples = new Float32Array(out);+ return b.samples;+}+function bakeLight() {+ ROOT.updateMatrixWorld(true);+ // The world follows the plan: from beyond the chapels to beyond the west front. A spire tip above the top simply casts no shadow up there+ const CELL = 0.4, X0 = LAY.xa - 27.8, Y0 = -2, Z0 = -36, NX = Math.ceil((P.xWest + 12 - X0) / CELL), NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;+ const grid = new Uint8Array(NX * NY * NZ), coarse = new Uint8Array(CX * CY * CZ);+ const SUN = (() => { const v = P.sun, l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();+ const SUN2 = [[SUN[0] + 0.06, SUN[1], SUN[2] + 0.05], [SUN[0] - 0.05, SUN[1] + 0.05, SUN[2] - 0.05]].map((v) => { const l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; });+ for (const e of PLACED) {+ if (e.mat.transparent || e.batch.noVoxel) continue;+ const S = samplesOf(e.batch), m = e.part.matrixWorld.elements, shaft = !!e.batch.pierShaft;+ for (let i = 0; i < S.length; i += 3) {+ const x = S[i], y = S[i + 1], z = S[i + 2], gi = ((m[0] * x + m[4] * y + m[8] * z + m[12] - X0) / CELL) | 0, gj = ((m[1] * x + m[5] * y + m[9] * z + m[13] - Y0) / CELL) | 0, gk = ((m[2] * x + m[6] * y + m[10] * z + m[14] - Z0) / CELL) | 0;+ if (gi >= 0 && gi < NX && gj >= 0 && gj < NY && gk >= 0 && gk < NZ) {+ const q = gi + NX * (gj + NY * gk); // 2 marks a voxel that holds only a pier shaft: solid to the stone, tested exactly by the window light+ if (!shaft) grid[q] = 1; else if (!grid[q]) grid[q] = 2;+ coarse[(gi >> 3) + CX * ((gj >> 3) + CY * (gk >> 3))] = 1;+ }+ }+ }+ // How enclosed a point is: the solid voxels averaged into 1.6 m cells and blurred, read back with trilinear filtering.+ // It is a smooth function of position, so light taken from it cannot have hard edges however few vertices a surface has.+ const DX = (NX >> 2) + 2, DY = (NY >> 2) + 2, DZ = (NZ >> 2) + 2, dens = new Float32Array(DX * DY * DZ);+ for (let k = 0, q = 0; k < NZ; k++) for (let j = 0; j < NY; j++) for (let i = 0; i < NX; i++, q++) if (grid[q]) dens[(i >> 2) + DX * ((j >> 2) + DY * (k >> 2))] += 1 / 64;+ { const tmp = new Float32Array(dens.length), steps = [1, DX, DX * DY];+ for (let pass = 0; pass < 3; pass++) for (const st of steps) {+ tmp.set(dens);+ for (let q = st; q < dens.length - st; q++) dens[q] = 0.25 * tmp[q - st] + 0.5 * tmp[q] + 0.25 * tmp[q + st];+ } }+ const enclosed = (x, y, z) => {+ const fx = clamp((x - X0) / (CELL * 4), 0, DX - 1.001), fy = clamp((y - Y0) / (CELL * 4), 0, DY - 1.001), fz = clamp((z - Z0) / (CELL * 4), 0, DZ - 1.001);+ const i = fx | 0, j = fy | 0, k = fz | 0, a = fx - i, c = fy - j, d = fz - k, q = i + DX * (j + DY * k), P1 = 1, P2 = DX, P4 = DX * DY;+ const lo = (dens[q] * (1 - a) + dens[q + P1] * a) * (1 - c) + (dens[q + P2] * (1 - a) + dens[q + P2 + P1] * a) * c;+ const hi = (dens[q + P4] * (1 - a) + dens[q + P4 + P1] * a) * (1 - c) + (dens[q + P4 + P2] * (1 - a) + dens[q + P4 + P2 + P1] * a) * c;+ return lo * (1 - d) + hi * d;+ };+ // Distance to the first hit, or 1e9 if the ray leaves the world; capped rays return their cap.+ // A ray from ground beyond the voxels first flies in to them, so the building still shadows the far flagstones.+ const X1 = X0 + NX * CELL, Y1 = Y0 + NY * CELL, Z1 = Z0 + NZ * CELL;+ const march = (ox, oy, oz, dx, dy, dz, cap) => {+ let t = 0; const st = CELL * 0.8, far = CELL * CK * 0.6;+ if (ox < X0 || ox >= X1 || oy < Y0 || oy >= Y1 || oz < Z0 || oz >= Z1) {+ let t0 = 0, t1 = cap;+ for (const [o, d, lo, hi] of [[ox, dx, X0, X1], [oy, dy, Y0, Y1], [oz, dz, Z0, Z1]]) {+ if (Math.abs(d) < 1e-9) { if (o < lo || o >= hi) return 1e9; continue; }+ const a = (lo - o) / d, b = (hi - o) / d; t0 = Math.max(t0, Math.min(a, b)); t1 = Math.min(t1, Math.max(a, b));+ }+ if (t0 >= t1) return 1e9;+ t = t0 + 0.01;+ }+ while (t < cap) {+ const i = ((ox + dx * t - X0) / CELL) | 0, j = ((oy + dy * t - Y0) / CELL) | 0, k = ((oz + dz * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return 1e9;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)]) return t;+ t += st;+ }+ return cap;+ };+ const solidAt = (x, y, z) => { const i = ((x - X0) / CELL) | 0, j = ((y - Y0) / CELL) | 0, k = ((z - Z0) / CELL) | 0; return i >= 0 && i < NX && j >= 0 && j < NY && k >= 0 && k < NZ && grid[i + NX * (j + NY * k)] !== 0; };+ // A vertex's light: [r,g,b] multipliers, plus how much of it was outdoors+ const light = { r: 1, g: 1, b: 1, out: 0, ndl: 0 };+ let sootFloor = 0.56, dampK = 1, indoorK = 1;+ const lightAt = (x, y, z, nx, ny, nz) => {+ // leave the voxels of the surface itself, then look around+ let ox = x + nx * CELL * 1.2, oy = y + ny * CELL * 1.2, oz = z + nz * CELL * 1.2, buried = false;+ if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 2.4; oy = y + ny * CELL * 2.4; oz = z + nz * CELL * 2.4; if (solidAt(ox, oy, oz)) { ox = x + nx * CELL * 3.6; oy = y + ny * CELL * 3.6; oz = z + nz * CELL * 3.6; buried = solidAt(ox, oy, oz); } }+ let esc = 0, skyW = 0, sky = 0, zen = 1e9;+ // A ray counts as sky only if it truly leaves the world: a long look down the nave is still indoors+ if (!buried) for (let q = 0; q < 5; q++) {+ const R = SKY_RAYS[q], d = march(ox, oy, oz, R[0], R[1], R[2], 200);+ if (q === 0) zen = d;+ const w = Math.max(0.12, R[0] * nx + R[1] * ny + R[2] * nz + 0.35);+ skyW += w; if (d >= 1e9) { sky += w; esc++; }+ }+ // The underside of a cornice sees no sky above it but is still outdoors: only there is a long look sideways worth its cost+ if (!buried && esc === 0 && ny < -0.2) for (let q = 5; q < SKY_RAYS.length && esc === 0; q++) { const R = SKY_RAYS[q]; if (march(ox, oy, oz, R[0], R[1], R[2], 90) >= 1e9) esc++; }+ const ao = clamp(1 - enclosed(x + nx, y + ny, z + nz) * 2.6), vis = skyW ? sky / skyW : 0, outdoors = esc >= 1;+ let r, g, b;+ if (outdoors) {+ const ndl = nx * SUN[0] + ny * SUN[1] + nz * SUN[2];+ let sunV = 0;+ if (ndl > 0 && !buried) {+ if (march(ox, oy, oz, SUN[0], SUN[1], SUN[2], 200) >= 1e9) sunV += 0.5;+ if (march(ox, oy, oz, SUN2[0][0], SUN2[0][1], SUN2[0][2], 200) >= 1e9) sunV += 0.25;+ if (march(ox, oy, oz, SUN2[1][0], SUN2[1][1], SUN2[1][2], 200) >= 1e9) sunV += 0.25;+ }+ const amb = 0.2 + 0.44 * vis * (0.55 + 0.45 * ao), s = sunV * Math.max(0, ndl) * 1.02;+ r = amb * 0.84 + s * 0.98; g = amb * 0.88 + s * 0.9; b = amb * 0.98 + s * 0.74;+ // The west front turns away from the sun, but it looks out on the open sky and on the sunlit forecourt. That light is real, and it is+ // shut out of a portal: what sees little sky takes little of either, so the recesses stay deep+ const facesWest = nx > 0 && x > LAY.xTowerE ? sstep(0.05, 0.6, nx) * sstep(LAY.xTowerE, LAY.xTowerE + 6, x) : 0;+ if (facesWest > 0) {+ const skyFill = facesWest * P.westSkyFill * vis * (0.55 + 0.45 * ao), groundFill = facesWest * P.westGroundFill * 0.5 * (1 - ny) * (0.1 + 0.9 * vis * vis);+ r += skyFill * 0.84 + groundFill; g += skyFill * 0.88 + groundFill * 0.93; b += skyFill * 0.98 + groundFill * 0.8;+ }+ // What the rain never touches keeps its soot; what it washes stays pale. Gentle on long walls, whose+ // few vertices would smear it: the crisp streaks under their ledges are decals.+ const shelter = zen < 6 ? 1 - zen / 6 : 0, soot = lerp(1, sootFloor, shelter * (1 - Math.max(0, ny)));+ r *= soot; g *= soot * 0.985; b *= soot * 0.96;+ // Rising damp: darker and greener towards the ground, a slow mottling over the height of the wall+ const damp = lerp(1, sstep(0, 9, y), dampK), mot = 0.93 + 0.07 * Math.sin(x * 0.53 + y * 0.31) * Math.sin(z * 0.47 - y * 0.23);+ r *= lerp(0.56, 1, damp) * mot; g *= lerp(0.64, 1, damp) * mot; b *= lerp(0.46, 1, damp) * mot;+ light.out = 1; light.ndl = ndl;+ } else {+ // Inside: warm stone, dark low in the aisles, lifting into the vault. Every term is a smooth function of position,+ // so the light has only gradients: the aisles of the nave, of the transept arms and round the apse fade in softly+ const low = sstep(P.aisleCrown + 2.5, P.aisleCrown - 0.5, y), axis = zn - 0.1, dxc = Math.abs(x - LAY.xc), arm = sstep(16, 13, dxc);+ const naveA = sstep(axis, axis + 2.4, Math.abs(z)) * (1 - arm), armA = sstep(axis, axis + 2.4, dxc) * arm * sstep(13, 16, Math.abs(z));+ const apse = sstep(LAY.xa + 1.5, LAY.xa - 1.5, x), ambA = sstep(axis, axis + 2.4, Math.sqrt((x - LAY.xa) * (x - LAY.xa) + z * z)) * apse;+ const s = lerp(0.32, 0.86, sstep(0.5, 34, y)) * lerp(0.5, 1, ao) * lerp(1, 0.76, Math.max(naveA * (1 - apse), armA, ambA) * low) * indoorK;+ r = s * 1.06; g = s * 0.87; b = s * 0.64;+ light.out = 0; light.ndl = 0;+ }+ light.r = r; light.g = g; light.b = b;+ return light;+ };+ // ---- group placements of each batch by which way they face the sun ----+ const perBatch = new Map();+ for (const e of PLACED) { if (e.batch.noColor || e.batch.isFigure || (e.mat.transparent && !e.batch.fade)) continue; let l = perBatch.get(e.batch); if (!l) { l = []; perBatch.set(e.batch, l); } l.push(e); }+ const entries = [];+ for (const [b, list] of perBatch) {+ const groups = new Map();+ for (const e of list) {+ const m = e.part.matrixWorld.elements, sx = len3(m[0], m[1], m[2]) || 1, sy = len3(m[4], m[5], m[6]) || 1, sz = len3(m[8], m[9], m[10]) || 1;+ // the sun in this placement's own frame (mirror included)+ const lx = (m[0] * SUN[0] + m[1] * SUN[1] + m[2] * SUN[2]) / (sx * sx), ly = (m[4] * SUN[0] + m[5] * SUN[1] + m[6] * SUN[2]) / (sy * sy), lz = (m[8] * SUN[0] + m[9] * SUN[1] + m[10] * SUN[2]) / (sz * sz);+ const key = Math.round(lx * 3) + ',' + Math.round(ly * 3) + ',' + Math.round(lz * 3);+ let g = groups.get(key); if (!g) { g = { key, list: [], L: [lx, ly, lz] }; groups.set(key, g); } g.list.push(e);+ }+ const gl = [...groups.values()];+ for (const g of gl) { // how much sun this side receives, by area of normals+ let s = 0; const nn = b.N; for (let i = 0; i < nn.length; i += 3) s += Math.max(0, nn[i] * g.L[0] + nn[i + 1] * g.L[1] + nn[i + 2] * g.L[2]);+ g.expo = s / Math.max(1, nn.length / 3);+ }+ gl.sort((a, c) => c.expo - a.expo);+ entries.push({ b, gl, wall: list[0].mat === MAT.ashlar ? 0 : 1 });+ }+ // Long walls get their own light for each side first: they carry the sense of sun and shade from a distance+ entries.sort((a, c) => a.wall - c.wall || a.b.count - c.b.count);+ let spent = 0, variants = 0, tinted = 0, vbaked = 0;+ const TINTS = new Map();+ const tintOf = (mat, tier) => {+ const key = mat.uuid + '|' + tier; let m = TINTS.get(key);+ if (!m) { m = mat.clone(); m.name = mat.name + (tier === 1 ? '_ShadeSide' : '_DeepShadeSide'); m.color.setRGB(tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.74 : 0.5, tier === 1 ? 0.76 : 0.53); TINTS.set(key, m); }+ return m;+ };+ const bakeGroup = (b, g) => {+ const rep = g.list[Math.floor(g.list.length / 2)], m = rep.part.matrixWorld.elements, n = b.count, out = new Uint8Array(n * 3), PP = b.P, NN = b.N, CC = b.C;+ let outdoor = 0;+ const onGround = rep.mat === MAT.flag || rep.mat === MAT.earth;+ sootFloor = onGround ? 1 : rep.mat === MAT.ashlar ? 0.84 : rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0.9 : 0.56;+ dampK = onGround || rep.mat === MAT.oak || rep.mat === MAT.iron || rep.mat === MAT.lead ? 0 : 1;+ indoorK = rep.mat === MAT.oak ? 2.2 : 1; // the oak was given its own dark tone when it was built+ for (let i = 0; i < n; i++) {+ const i3 = i * 3, x = PP[i3], y = PP[i3 + 1], z = PP[i3 + 2], a = NN[i3], c = NN[i3 + 1], d = NN[i3 + 2];+ const wx = m[0] * x + m[4] * y + m[8] * z + m[12], wy = m[1] * x + m[5] * y + m[9] * z + m[13], wz = m[2] * x + m[6] * y + m[10] * z + m[14];+ let nx = m[0] * a + m[4] * c + m[8] * d, ny = m[1] * a + m[5] * c + m[9] * d, nz = m[2] * a + m[6] * c + m[10] * d; const l = Math.sqrt(nx * nx + ny * ny + nz * nz) || 1; nx /= l; ny /= l; nz /= l;+ const L = lightAt(wx, wy, wz, nx, ny, nz);+ outdoor += L.out;+ out[i3] = Math.min(255, CC[i3] * L.r); out[i3 + 1] = Math.min(255, CC[i3 + 1] * L.g); out[i3 + 2] = Math.min(255, CC[i3 + 2] * L.b);+ }+ // Each vertex is one noisy sample of light: blend neighbours on the same surface so no sample can smear a dark+ // triangle across the stone. Edges between planes keep their contrast.+ {+ const sum = new Float32Array(n * 3), w = new Float32Array(n), I = b.I, passes = rep.mat === MAT.ashlar ? 3 : 2;+ const link = (p, q) => { if (NN[p * 3] * NN[q * 3] + NN[p * 3 + 1] * NN[q * 3 + 1] + NN[p * 3 + 2] * NN[q * 3 + 2] < 0.8) return; w[p]++; w[q]++; for (let c = 0; c < 3; c++) { sum[p * 3 + c] += out[q * 3 + c]; sum[q * 3 + c] += out[p * 3 + c]; } };+ for (let pass = 0; pass < passes; pass++) {+ sum.fill(0); w.fill(0);+ for (let t = 0; t < I.length; t += 3) { link(I[t], I[t + 1]); link(I[t + 1], I[t + 2]); link(I[t + 2], I[t]); }+ for (let i = 0; i < n; i++) if (w[i]) for (let c = 0; c < 3; c++) out[i * 3 + c] = out[i * 3 + c] * 0.4 + 0.6 * sum[i * 3 + c] / w[i];+ }+ }+ vbaked += n;+ return { colors: out, outdoor: outdoor / Math.max(1, n) };+ };+ for (const { b, gl } of entries) {+ const base = b.toGeo(), cost = (gl.length - 1) * b.count * 12, full = gl.length === 1 || spent + cost <= P.lightVariantBytes;+ if (full) spent += cost;+ gl.forEach((g, gi) => {+ if (gi === 0 || full) {+ const R = bakeGroup(b, g);+ g.outdoor = R.outdoor;+ if (gi === 0 && b.fade) { // a surface that dissolves at its edge carries its opacity with its light+ const rgba = new Float32Array(b.count * 4);+ for (let i = 0; i < b.count; i++) { rgba[i * 4] = R.colors[i * 3] / 255; rgba[i * 4 + 1] = R.colors[i * 3 + 1] / 255; rgba[i * 4 + 2] = R.colors[i * 3 + 2] / 255; rgba[i * 4 + 3] = b.fade(b.P[i * 3], b.P[i * 3 + 2]); }+ base.setAttribute('color', new THREE.BufferAttribute(rgba, 4));+ return;+ }+ if (gi === 0) { base.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true)); return; }+ const v = new THREE.BufferGeometry();+ v.setAttribute('position', base.attributes.position); v.setAttribute('normal', base.attributes.normal); v.setAttribute('uv', base.attributes.uv);+ v.setAttribute('color', new THREE.BufferAttribute(R.colors, 3, true));+ if (base.attributes.tangent) v.setAttribute('tangent', base.attributes.tangent);+ v.setIndex(base.index);+ for (const e of g.list) e.part.geometry = v;+ variants++;+ } else {+ // over budget: same colours, a darker stone. Skipped for batches that are mostly indoors+ tinted++;+ if (gl[0].outdoor < 0.3) return;+ const ratio = g.expo / Math.max(1e-3, gl[0].expo);+ if (ratio >= 0.78) return;+ // The copies that look west stand in the soft daylight of the front: they are not put in shade with the rest+ if (g.list.some((e) => { const m = e.part.matrixWorld.elements; return m[12] > LAY.xTowerE && m[8] > 0.7; })) return;+ const tier = ratio >= 0.45 ? 1 : 2;+ for (const e of g.list) e.part.material = tintOf(e.mat, tier);+ }+ });+ }+ // ---- figures keep their carved occlusion; the niche they stand in decides how much daylight reaches them ----+ let figN = 0;+ for (const f of FIG_PLACED) {+ const m = f.part.matrixWorld.elements, s = len3(m[0], m[1], m[2]) || 1, fx = m[0] / s, fy = m[1] / s, fz = m[2] / s;+ let ox = m[12] + m[4] * 1.3, oy = m[13] + m[5] * 1.3, oz = m[14] + m[6] * 1.3;+ for (let i = 0; i < 12 && solidAt(ox, oy, oz); i++) { ox += fx * CELL; oy += fy * CELL; oz += fz * CELL; }+ const L = lightAt(ox - fx * 0.05, oy - fy * 0.05, oz - fz * 0.05, fx, fy, fz);+ // Each tone is the nearest to the light the stone around it received, so a figure sits in its wall. Every figure here stands outdoors:+ // one whose sky is cut off by a portal is in shade, not in the warm light of the nave (the nave's own figures never come through here)+ const lum = (L.r + L.g + L.b) / 3, facing = fx * SUN[0] + fy * SUN[1] + fz * SUN[2];+ let mat = lum > 0.7 ? MAT.figure : lum > 0.38 ? MAT.figureHalf : MAT.figureDeep;+ if (facing < -0.3 && mat === MAT.figure) mat = MAT.figureHalf;+ f.part.material = mat; if (f.head) f.head.material = mat;+ figN++;+ }+ const windowLight = bakeWindowLight({ SUN, grid, coarse, NX, NY, NZ, CX, CY, X0, Y0, Z0, CELL, CK, march });+ return { vertices: vbaked, variants, tinted, extraKB: Math.round(spent / 1024), figures: figN, windowLight };+}++// ==================================================================================+// THE LIGHT OF THE SOUTH WINDOWS, baked from the same sun as the stone. For each point of the floor, and each lit face of the south+// piers, a small cone of rays goes back towards P.sun. A ray must clear the piers and the stone indoors, pass the wall's reveal, find+// glass in a south aisle window, and then clear everything outside: gable, buttresses, flyers, pinnacles, towers, transepts.+// Light multiplies the stone it lands on, so each overlay carries that stone's own texture and keeps its pattern and its dark joints:+// how much light arrives sets the opacity and draws the window's shape; the panes set only the hue, so pale panes cannot make spots.+// The vertices lie only where light falls. The penumbra grows with distance, so far edges melt and near piers cut clean.+// ==================================================================================+const WINDOW_LIGHT = { cell: 0.33, coneDeg: 1.2, rays: 20, opacity: 0.88, floorLevel: 0.76, pierLight: 0.4, pierOpacity: 0.85, hueRadius: 5 };+const SUN_WINDOWS = [];+const inPoly = (pts, x, y) => { let c = false; for (let i = 0, j = pts.length - 1; i < pts.length; j = i++) { const a = pts[i], b = pts[j]; if ((a[1] > y) !== (b[1] > y) && x < ((b[0] - a[0]) * (y - a[1])) / (b[1] - a[1]) + a[0]) c = !c; } return c; };+const FINE = 0.05; // metres per pixel of a window's cartoon+let TRACERY_MASK = null;+function traceryMask(nx, ny, w, y0) { // what the stone bars and the moulded frame cover, seen straight on+ if (TRACERY_MASK) return TRACERY_MASK;+ const t = new Batch(1), mask = new Uint8Array(nx * ny);+ addTracery(t, WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise, { bar: 0.25, depth: 0.42 });+ t.sweep(archOutline(WIN.aW, WIN.aSill, WIN.aSp, WIN.aRise, 9), rollSection(0.13, 0.92, 1, 0.03, 2), { closed: true, s: 0.9, crease: 40 });+ const Q = t.P, I = t.I;+ for (let f = 0; f < I.length; f += 3) {+ const ax = Q[I[f] * 3], ay = Q[I[f] * 3 + 1], bx = Q[I[f + 1] * 3], by = Q[I[f + 1] * 3 + 1], cx = Q[I[f + 2] * 3], cy = Q[I[f + 2] * 3 + 1];+ const d = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax); if (Math.abs(d) < 1e-7) continue;+ const i0 = Math.max(0, Math.floor((Math.min(ax, bx, cx) + w / 2) / FINE)), i1 = Math.min(nx - 1, Math.ceil((Math.max(ax, bx, cx) + w / 2) / FINE)), j0 = Math.max(0, Math.floor((Math.min(ay, by, cy) - y0) / FINE)), j1 = Math.min(ny - 1, Math.ceil((Math.max(ay, by, cy) - y0) / FINE));+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) {+ const px = (i + 0.5) * FINE - w / 2, py = (j + 0.5) * FINE + y0, e0 = ((bx - ax) * (py - ay) - (by - ay) * (px - ax)) / d, e1 = ((cx - bx) * (py - by) - (cy - by) * (px - bx)) / d, e2 = ((ax - cx) * (py - cy) - (ay - cy) * (px - cx)) / d;+ if (e0 >= 0 && e1 >= 0 && e2 >= 0) mask[i + nx * j] = 1;+ }+ }+ TRACERY_MASK = mask;+ return mask;+}+// The window as light passes through it, at 10 cm: G is how much of each cell is glass (lead and stone bars are not). H is the hue there:+// the panes in their real colours and order, each pane counted by how saturated it is, so the deep ruby and cobalt grounds lead and pale+// robes melt into them, then averaged over about a metre of window+function windowTransmission(batch) {+ if (batch.transmission) return batch.transmission;+ const w = WIN.aW, y0 = WIN.aSill, top = WIN.aSp + WIN.aRise, nx = Math.ceil(w / FINE), ny = Math.ceil((top - y0) / FINE), fine = new Uint8Array(nx * ny * 3), famRGB = {};+ for (const f of GLASS_FAMILIES) famRGB[f[0]] = f[1];+ const order = batch.panes.map((p, i) => [p, i]).sort((a, c) => a[0][2] - c[0][2] || a[1] - c[1]);+ for (const [pane] of order) {+ const pts = pane[0], rgb = famRGB[pane[1]] || [0, 0, 0];+ let bx0 = 1e9, bx1 = -1e9, by0 = 1e9, by1 = -1e9; for (const p of pts) { bx0 = Math.min(bx0, p[0]); bx1 = Math.max(bx1, p[0]); by0 = Math.min(by0, p[1]); by1 = Math.max(by1, p[1]); }+ const i0 = Math.max(0, Math.floor((bx0 + w / 2) / FINE)), i1 = Math.min(nx - 1, Math.ceil((bx1 + w / 2) / FINE)), j0 = Math.max(0, Math.floor((by0 - y0) / FINE)), j1 = Math.min(ny - 1, Math.ceil((by1 - y0) / FINE));+ for (let j = j0; j <= j1; j++) for (let i = i0; i <= i1; i++) if (inPoly(pts, (i + 0.5) * FINE - w / 2, (j + 0.5) * FINE + y0)) { const q = (i + nx * j) * 3; fine[q] = rgb[0]; fine[q + 1] = rgb[1]; fine[q + 2] = rgb[2]; }+ }+ const stone = traceryMask(nx, ny, w, y0), mx = nx >> 1, my = ny >> 1, G = new Float32Array(mx * my), kinds = [new Float32Array(mx * my * 4), new Float32Array(mx * my * 4), new Float32Array(mx * my * 4)];+ for (let j = 0; j < my; j++) for (let i = 0; i < mx; i++) {+ let gl = 0; const o = i + mx * j;+ for (let b = 0; b < 2; b++) for (let a = 0; a < 2; a++) {+ const q = 2 * i + a + nx * (2 * j + b), r = fine[q * 3], g = fine[q * 3 + 1], c = fine[q * 3 + 2], m = Math.max(r, g, c);+ if (stone[q] || m === 0) continue;+ const k = (m - Math.min(r, g, c)) / m + 0.15, K = kinds[r === m ? 0 : g === m ? 1 : 2]; // reds and golds, greens, blues+ gl += 0.25; K[o * 4] += k * Math.pow(r / m, 2.2); K[o * 4 + 1] += k * Math.pow(g / m, 2.2); K[o * 4 + 2] += k * Math.pow(c / m, 2.2); K[o * 4 + 3] += k;+ }+ G[o] = gl;+ }+ // Two passes of a box in each direction, each kind of colour on its own: the hue only, never the shape+ const R = WINDOW_LIGHT.hueRadius, tmp = new Float32Array(mx * my * 4);+ for (const W4 of kinds) for (let pass = 0; pass < 2; pass++) {+ for (let j = 0; j < my; j++) for (let c = 0; c < 4; c++) { let s = 0, n = 0; for (let i = -R; i < mx; i++) { if (i + R < mx) { s += W4[(i + R + mx * j) * 4 + c]; n++; } if (i - R - 1 >= 0) { s -= W4[(i - R - 1 + mx * j) * 4 + c]; n--; } if (i >= 0) tmp[(i + mx * j) * 4 + c] = s / n; } }+ for (let i = 0; i < mx; i++) for (let c = 0; c < 4; c++) { let s = 0, n = 0; for (let j = -R; j < my; j++) { if (j + R < my) { s += tmp[(i + mx * (j + R)) * 4 + c]; n++; } if (j - R - 1 >= 0) { s -= tmp[(i + mx * (j - R - 1)) * 4 + c]; n--; } if (j >= 0) W4[(i + mx * j) * 4 + c] = s / n; } }+ }+ // Each spot takes the kind of glass that leads around it, never an average of them: ruby beside cobalt would mix to the magenta of no real pane+ const H = new Float32Array(mx * my * 3);+ for (let o = 0; o < mx * my; o++) { let K = kinds[0]; for (const C of kinds) if (C[o * 4 + 3] > K[o * 4 + 3]) K = C; const t = K[o * 4 + 3] || 1; H[o * 3] = K[o * 4] / t; H[o * 3 + 1] = K[o * 4 + 1] / t; H[o * 3 + 2] = K[o * 4 + 2] / t; }+ batch.transmission = { G, H, mx, my, cell: FINE * 2 };+ return batch.transmission;+}+function bakeWindowLight(V) {+ const { SUN, grid, coarse, NX, NY, NZ, CX, CY, X0, Y0, Z0, CELL, CK, march } = V, { xTowerE, xNaveE, xChoirW } = LAY, WL = WINDOW_LIGHT, parent = ROOT.children.find((c) => c.name === 'Nave');+ const w = WIN.aW, sill = WIN.aSill, ys = WIN.aSp, rise = WIN.aRise, top = ys + rise, archC = (rise * rise - w * w / 4) / w, archR = archC + w / 2, zW = -(zaw + 0.45), zIn = -zaw + 0.35, zOut = -(zaw + 1.32), wallHalf = 0.45;+ const openAt = (u, y) => y >= sill && y <= top && Math.abs(u) <= w / 2 && (y <= ys || Math.abs(u) + archC <= Math.sqrt(archR * archR - (y - ys) * (y - ys)));+ const wins = []; for (const s of SUN_WINDOWS) wins[Math.round((xTowerE - s.x) / BAY - 0.5)] = { x: s.x, L: windowTransmission(s.batch) };+ // One fixed set of rays, each with its own direction inside a small cone and its own start inside the cell: the same pattern at every+ // point, so a vertex cannot differ from its neighbour by chance, and what falls between two vertices is averaged, not missed+ const a0 = (() => { const l = Math.hypot(SUN[2], SUN[0]); return [-SUN[2] / l, 0, SUN[0] / l]; })(), b0 = [SUN[1] * a0[2] - SUN[2] * a0[1], SUN[2] * a0[0] - SUN[0] * a0[2], SUN[0] * a0[1] - SUN[1] * a0[0]], cone = Math.tan(rad(WL.coneDeg)), NR = WL.rays, rays = [];+ for (let n = 0; n < NR; n++) { const r = cone * Math.sqrt((n + 0.5) / NR), th = n * 2.39996, ca = r * Math.cos(th), sa = r * Math.sin(th), x = SUN[0] + a0[0] * ca + b0[0] * sa, y = SUN[1] + a0[1] * ca + b0[1] * sa, z = SUN[2] + a0[2] * ca + b0[2] * sa, l = Math.hypot(x, y, z), pl = Math.hypot(x, z) / l; rays.push({ d: [x / l, y / l, z / l], pl, sx: x / l / pl, sz: z / l / pl, jx: ((n * 0.7549) % 1) - 0.5, jz: ((n * 0.5698) % 1) - 0.5 }); }+ // The piers, exactly: shaft, plinth, and the corbel, statue and canopy on the nave face. Height decides which of them a ray meets+ const hits = (R, px, py, pz, cx, cz, r, yLo, yHi) => { const dx = cx - px, dz = cz - pz, along = dx * R.sx + dz * R.sz; if (along < 0 && dx * dx + dz * dz > r * r) return false; if (Math.abs(dx * R.sz - dz * R.sx) >= r) return false; const y = py + R.d[1] * Math.max(along, 0) / R.pl; return y >= yLo && y <= yHi; };+ const crossing = [[xNaveE, -zn], [xNaveE, zn], [xChoirW, -zn], [xChoirW, zn]];+ const pierBlocks = (R, px, py, pz, skipK) => {+ for (let s = 0; s < 2; s++) {+ const zA = s ? zn : -zn; if (pz < zA - 1.8) continue; // the ray runs towards -z: an arcade north of this point is behind it+ const xA = px + R.sx * (zA - pz) / R.sz, k0 = Math.round((xTowerE - xA) / BAY);+ for (let k = k0 - 1; k <= k0 + 1; k++) {+ if (k < -1 || k >= P.naveBays || (s === 0 && k === skipK)) continue;+ const cx = xTowerE - k * BAY;+ if (hits(R, px, py, pz, cx, zA, 1.02, -1, P.pierTop - 0.8) || hits(R, px, py, pz, cx, zA, 1.62, -1, 0.62) || (k >= 0 && hits(R, px, py, pz, cx, zA + (s ? -1.14 : 1.14), 0.42, 5, 10.2))) return true;+ }+ }+ for (const c of crossing) if (hits(R, px, py, pz, c[0], c[1], 1.3, -1, P.vaultSpring)) return true;+ return false;+ };+ const stoneBlocks = (d, px, py, pz, t0, t1) => { // voxels of real stone between here and the inner face of the wall: the shafts are tested above+ const st = CELL * 0.8, far = CELL * CK * 0.6; let t = t0;+ while (t < t1) {+ const i = ((px + d[0] * t - X0) / CELL) | 0, j = ((py + d[1] * t - Y0) / CELL) | 0, k = ((pz + d[2] * t - Z0) / CELL) | 0;+ if (i < 0 || i >= NX || j < 0 || j >= NY || k < 0 || k >= NZ) return false;+ if (!coarse[(i >> 3) + CX * ((j >> 3) + CY * (k >> 3))]) { t += far; continue; }+ if (grid[i + NX * (j + NY * k)] === 1) return true;+ t += st;+ }+ return false;+ };+ // The window's own gable stands just outside the wall, a plate with a round hole, closer than a voxel can tell: tested exactly.+ // Beyond it the voxels take over, and they hold everything else outside+ const gableW = w + 0.8, gableY = ys + 2.95, gableH = 3.1, gableZ = -(zaw + 1.02), holeY = gableY + gableH * 0.36, holeR = Math.min(gableW * 0.13, gableH * 0.2);+ const gableBlocks = (u, yw, d) => {+ const t = (gableZ - zW) / d[2], x = u + d[0] * t, y = yw + d[1] * t;+ if (y < gableY - 0.09 || y > gableY + gableH + 0.1 || Math.abs(x) > (gableW / 2) * (1 - (y - gableY) / gableH) + 0.09) return false;+ return Math.hypot(x, y - holeY) >= holeR;+ };+ // One ray: adds its hue and its share of glass to acc. Tally: how many reach a window from inside, how many of those also clear the outside+ const acc = [0, 0, 0, 0], tally = { floorAt: 0, floorClear: 0, skinAt: 0, skinClear: 0 };+ let onSkin = false;+ const trace = (R, px, py, pz, t0, skipK) => {+ const d = R.d, tW = (zW - pz) / d[2]; if (tW <= 0) return;+ const xw = px + d[0] * tW, yw = py + d[1] * tW, win = wins[Math.round((xTowerE - xw) / BAY - 0.5)]; if (!win) return;+ const u = xw - win.x, L = win.L, ix = ((u + w / 2) / L.cell) | 0, iy = ((yw - sill) / L.cell) | 0; if (u < -w / 2 || ix >= L.mx || yw < sill || iy >= L.my) return;+ const q = ix + L.mx * iy, g = L.G[q]; if (g <= 0) return;+ const reveal = wallHalf / -d[2]; if (!openAt(u - d[0] * reveal, yw - d[1] * reveal) || !openAt(u + d[0] * reveal, yw + d[1] * reveal)) return; // the wall is 0.9 m thick+ if (pierBlocks(R, px, py, pz, skipK)) return;+ if (stoneBlocks(d, px, py, pz, t0, (zIn - pz) / d[2])) return;+ if (onSkin) tally.skinAt++; else tally.floorAt++;+ if (gableBlocks(u, yw, d)) return;+ const tO = (zOut - zW) / d[2];+ if (march(xw + d[0] * tO, yw + d[1] * tO, zOut, d[0], d[1], d[2], 220) < 1e9) return;+ if (onSkin) tally.skinClear++; else tally.floorClear++;+ acc[0] += g * L.H[q * 3]; acc[1] += g * L.H[q * 3 + 1]; acc[2] += g * L.H[q * 3 + 2]; acc[3] += g;+ };+ // The colour of a point from what arrived: the hue pressed a little further towards pure glass, the opacity from how much arrived+ const finish = (v, level, strength) => {+ const f = Math.min(1, acc[3] / (NR * 0.92)); v.f = f * strength;+ if (acc[3] <= 0) { v.h = null; return; }+ const m = Math.max(acc[0], acc[1], acc[2]) || 1;+ v.h = [Math.pow(acc[0] / m, 1.2), Math.pow(acc[1] / m, 1.2), Math.pow(acc[2] / m, 1.2)]; v.level = level;+ };+ // Write one overlay: only the cells that hold light. A vertex at the edge of a pool has no light of its own, so it borrows its neighbours' hue: its opacity is zero anyway+ const build = (name, mat, verts, cells, level, paint) => {+ const keep = new Int32Array(verts.length).fill(-1), P3 = [], N3 = [], UV = [], C4 = [], I = [];+ const idx = (n) => { if (keep[n] >= 0) return keep[n]; const v = verts[n]; keep[n] = P3.length / 3; P3.push(v.x, v.y, v.z); N3.push(v.nx, v.ny, v.nz); UV.push(v.u, v.v); const h = v.h || v.borrow || [1, 1, 1]; if (paint) C4.push(...paint(v)); else C4.push(level * h[0] + 0.02, level * h[1] + 0.02, level * h[2] + 0.02, WL.opacity * v.f); return keep[n]; };+ for (const c of cells) {+ if (verts[c[0]].f + verts[c[1]].f + verts[c[2]].f + verts[c[3]].f < 0.01) continue;+ for (let a = 0; a < 4; a++) { const v = verts[c[a]]; if (!v.h) for (let b = 0; b < 4; b++) if (verts[c[b]].h) { v.borrow = verts[c[b]].h; break; } }+ const a = idx(c[0]), b = idx(c[1]), e = idx(c[2]), d = idx(c[3]); I.push(a, b, e, a, e, d);+ }+ if (!I.length) return 0;+ const g = new THREE.BufferGeometry();+ g.setAttribute('position', new THREE.BufferAttribute(new Float32Array(P3), 3));+ g.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(N3), 3));+ g.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(UV), 2));+ g.setAttribute('color', new THREE.BufferAttribute(new Float32Array(C4), 4));+ g.setIndex(new THREE.BufferAttribute(new Uint16Array(I), 1));+ createPart(uniq(name), g, mat, { parent });+ return P3.length / 3;+ };+ // ---- The floor: from the south aisle wall to just past where the highest light can land ----+ const yF = 0.08, tFloor = (0.46 - yF) / SUN[1], reach = -SUN[2] / SUN[1], drift = SUN[0] / SUN[1], xs = SUN_WINDOWS.map((s) => s.x), h = WL.cell;+ const x0 = Math.min(...xs) - w / 2 - drift * top - 0.8, x1 = Math.max(...xs) + w / 2 + 0.8, z0 = -zaw, z1 = zW + top * reach + 0.8, nu = Math.ceil((x1 - x0) / h), nv = Math.ceil((z1 - z0) / h);+ const fv = [], fc = []; let lastZ = -1e9, litW = 1e9, litE = -1e9;+ for (let j = 0; j <= nv; j++) for (let i = 0; i <= nu; i++) {+ const x = x0 + i * h, z = z0 + j * h; acc[0] = acc[1] = acc[2] = acc[3] = 0;+ for (const R of rays) trace(R, x + R.jx * h, yF, z + R.jz * h, tFloor, -99);+ const v = { x, y: yF, z, nx: 0, ny: 1, nz: 0, u: x / TILE.pave, v: z / TILE.pave }; finish(v, WL.floorLevel, 1); fv.push(v);+ if (v.f > 0.02) { lastZ = Math.max(lastZ, z); litW = Math.min(litW, x); litE = Math.max(litE, x); }+ }+ for (let j = 0; j < nv; j++) for (let i = 0; i < nu; i++) { const a = i + (nu + 1) * j; fc.push([a + (nu + 1), a + (nu + 1) + 1, a + 1, a]); }+ const floorVerts = build('StainedLight_Floor', MAT.pool, fv, fc, WL.floorLevel);+ // ---- The south piers: rays that stop on their south faces, on a thin skin following the shaft ring point for point ----+ onSkin = true;+ const ring = SHARED.pierRing, RN = ring.length, aS = Math.atan2(SUN[2], SUN[0]), aStep = TAU / RN, nc = Math.round(rad(200) / aStep), c0 = Math.round((aS - rad(100)) / aStep), perim = [0];+ for (let i = 1; i <= RN; i++) perim.push(perim[i - 1] + Math.hypot(ring[i % RN][0] - ring[i - 1][0], ring[i % RN][1] - ring[i - 1][1]));+ const ringAt = (a) => { const f = ((a / TAU) % 1 + 1) % 1 * RN, i = f | 0, t = f - i, A = ring[i % RN], B = ring[(i + 1) % RN]; return [lerp(A[0], B[0], t), lerp(A[1], B[1], t), lerp(A[2], B[2], t), lerp(A[3], B[3], t)]; };+ const kAt = (y) => (y < 0.86 ? lerp(1.24, 1.27, clamp((y - 0.62) / 0.24)) : y < 1.15 ? lerp(1.27, 1, (y - 0.86) / 0.29) : 1);+ const shaftAt = (a, y) => { const r = ringAt(a), k = kAt(y), l = Math.hypot(r[2], r[3]) || 1, nx = r[2] / l, nz = r[3] / l, off = 0.03 + 0.06 * sstep(1.4, 1.1, y); return [r[0] * k + nx * off, y, r[1] * k + nz * off, nx, nz]; }; // further off the base mouldings, whose facets would poke through+ const plinthAt = (a, y) => { const f = Math.round(a / (PI / 4)) * (PI / 4), r = (1.7 * Math.cos(PI / 8)) / Math.cos(a - f) + 0.03; return [r * Math.cos(a), y, r * Math.sin(a), Math.cos(f), Math.sin(f)]; }; // the octagonal foot of the pier+ const skinAt = (a, y) => { if (y <= 0.56) return plinthAt(a, y); if (y >= 0.62) return shaftAt(a, y); const p = plinthAt(a, 0.56), s = shaftAt(a, 0.62), t = (y - 0.56) / 0.06; return [lerp(p[0], s[0], t), y, lerp(p[2], s[2], t), p[3], p[4]]; };+ const rowsY = [yF + 0.01, 0.56, 0.62, 0.86, 1.15]; for (let y = 2.15; y < P.pierTop - 1.2; y += 1) rowsY.push(y);+ const sv = [], sc = [], nr = rowsY.length, cols = nc + 1; let piersLit = 0, topLit = 0;+ // The pier's own baked colour under this point: an overlay of that colour changes nothing, so the light is simply added on top and the+ // stone can only get brighter. A shaft has vertices only at its foot and under its capital, so its colour runs in a straight line between them+ const yFoot = 1.15, yCap = P.pierTop - 1.05, tables = new Map();+ const stoneTable = (geo) => {+ let t = tables.get(geo); if (t) return t;+ const pos = geo.attributes.position, col = geo.attributes.color; t = { foot: [], cap: [], low: [], col };+ for (let i = 0; i < pos.count; i++) { const x = pos.getX(i), y = pos.getY(i), z = pos.getZ(i), e = [x, y, z, i]; if (Math.abs(y - yFoot) < 0.02) t.foot.push(e); if (Math.abs(y - yCap) < 0.02) t.cap.push(e); if (y < yFoot + 0.02) t.low.push(e); }+ tables.set(geo, t); return t;+ };+ // Several surfaces meet at a moulding, some of them in their own shade underneath: the visible face is the brightest one close by+ const nearest = (t, list, x, y, z, flat, reach) => {+ let bd = 1e9; for (const e of list) { const dx = e[0] - x, dy = flat ? 0 : e[1] - y, dz = e[2] - z, d = dx * dx + dy * dy + dz * dz; if (d < bd) bd = d; }+ const lim = (Math.sqrt(bd) + reach) * (Math.sqrt(bd) + reach); let best = [0, 0, 0], top = -1;+ for (const e of list) { const dx = e[0] - x, dy = flat ? 0 : e[1] - y, dz = e[2] - z; if (dx * dx + dy * dy + dz * dz > lim) continue; const r = t.col.getX(e[3]), g = t.col.getY(e[3]), b = t.col.getZ(e[3]), s = r + g + b; if (s > top) { top = s; best = [r, g, b]; } }+ return best;+ };+ const stoneColour = (t, lx, y, lz) => {+ if (y < yFoot) return nearest(t, t.low, lx, y, lz, false, 0.25);+ const A = nearest(t, t.foot, lx, y, lz, true, 0.12), B = nearest(t, t.cap, lx, y, lz, true, 0.12), s = clamp((y - yFoot) / (yCap - yFoot));+ return [lerp(A[0], B[0], s), lerp(A[1], B[1], s), lerp(A[2], B[2], s)];+ };+ const tent = (F, stride, count, lines, lineStride, passes) => { // soften along one direction of a pier's grid+ const row = new Float32Array(count);+ for (let pass = 0; pass < passes; pass++) for (let l = 0; l < lines; l++) for (let ch = 0; ch < 4; ch++) {+ for (let i = 0; i < count; i++) row[i] = F[(l * lineStride + i * stride) * 4 + ch];+ for (let i = 0; i < count; i++) F[(l * lineStride + i * stride) * 4 + ch] = 0.25 * row[Math.max(0, i - 1)] + 0.5 * row[i] + 0.25 * row[Math.min(count - 1, i + 1)];+ }+ };+ for (let k = 0; k < P.naveBays; k++) {+ const cx = xTowerE - k * BAY, base = sv.length, part = PLACED.find((e) => e.batch.pierShaft && e.mat === MAT.fine && Math.abs(e.part.matrixWorld.elements[12] - cx) < 0.01 && Math.abs(e.part.matrixWorld.elements[14] + zn) < 0.01), m = part.part.matrixWorld.elements, table = stoneTable(part.part.geometry);+ const F = new Float32Array(cols * nr * 4); let any = false;+ for (let c = 0; c <= nc; c++) for (let r = 0; r < nr; r++) {+ const a = (c0 + c) * aStep, y = rowsY[r], dy = r + 1 < nr ? rowsY[r + 1] - y : 1;+ acc[0] = acc[1] = acc[2] = acc[3] = 0;+ if (Math.cos(a - aS) > 0.02) for (const R of rays) { const yJ = Math.max(yF + 0.01, y + R.jz * dy), q = skinAt(a + R.jx * aStep, yJ); trace(R, cx + q[0], yJ, -zn + q[2], 0.7, k); }+ const o = (c * nr + r) * 4; F[o] = acc[0]; F[o + 1] = acc[1]; F[o + 2] = acc[2]; F[o + 3] = acc[3];+ }+ // Light on a column is one smooth band: softened round the shaft and up it, and turned by the column as a whole, not by each of its flutes+ tent(F, nr, cols, nr, 1, 4); tent(F, 1, nr, cols, nr, 2);+ for (let c = 0; c <= nc; c++) for (let r = 0; r < nr; r++) {+ const a = (c0 + c) * aStep, y = rowsY[r], p = skinAt(a, y), o = (c * nr + r) * 4, i = (((c0 + c) % RN) + RN) % RN, wx = cx + p[0], wz = -zn + p[2];+ const v = { x: wx, y, z: wz, nx: p[3], ny: 0, nz: p[4], u: perim[(i + (RN >> 1)) % RN] / TILE.fine, v: y / TILE.fine };+ const dx = wx - m[12], dz = wz - m[14], f = Math.min(1, F[o + 3] / (NR * 0.92)), turn = Math.max(0, Math.cos(a - aS));+ v.stone = stoneColour(table, dx * m[0] + dz * m[2], y, dx * m[8] + dz * m[10]);+ v.f = sstep(0.02, 0.8, f) * sstep(0, 0.5, turn); v.light = WL.pierLight * turn;+ const top = Math.max(F[o], F[o + 1], F[o + 2]); v.h = top > 0 ? [Math.pow(F[o] / top, 1.2), Math.pow(F[o + 1] / top, 1.2), Math.pow(F[o + 2] / top, 1.2)] : null;+ sv.push(v);+ if (v.f > 0.02) { any = true; topLit = Math.max(topLit, y); }+ }+ if (any) piersLit++;+ for (let c = 0; c < nc; c++) for (let r = 0; r < nr - 1; r++) { const a = base + c * nr + r; sc.push([a, a + 1, a + nr + 1, a + nr]); }+ }+ const skinVerts = build('StainedLight_SouthPiers', MAT.poolStone, sv, sc, 1, (v) => { const h = v.h || v.borrow || [0, 0, 0], s = v.stone; return [Math.min(1, s[0] + v.light * h[0]), Math.min(1, s[1] + v.light * h[1]), Math.min(1, s[2] + v.light * h[2]), WL.pierOpacity * v.f]; });+ return { tally, floorVerts, skinVerts, piersLit, topLit, lastZ, litW, litE };+}++// <<ORNAMENT>>++// <<BUILD>>+async function build() {+ ROOT = createRoot('Cathedral');+ await makeMaterials();+ await prepareFigures(P.preview === 'statues' ? P.statueTest : Object.keys(FIGURES));+ if (P.preview === 'statues') { previewStatues(); return ROOT; }+ if (P.preview === 'all' || P.preview === 'unique') {+ buildCathedral();+ await reduceBatches();+ bakeLight();+ return ROOT;+ }+ if (P.preview === 'size') { // builds and bakes everything, then reports bytes as part names, and exports only a probe: fast+ buildCathedral();+ await reduceBatches();+ const baked = bakeLight(), report = ['bake_variants' + baked.variants + '_tinted' + baked.tinted + '_extraKB' + baked.extraKB];+ // Real buffer sizes, each attribute counted once: pos/normal 12, uv 8, colour 12, tangent 16 a vertex where normal-mapped+ const seenAttr = new Set(), seenTan = new Set(), byMat = {}; let bytes = 0, tris = 0;+ const add = (attr, per, key) => { if (!attr || seenAttr.has(attr)) return; seenAttr.add(attr); const b = attr.count * per; bytes += b; byMat[key] = (byMat[key] || 0) + b; };+ for (const e of PLACED) {+ const g = e.part.geometry, key = e.mat.name, a = g.attributes, m = e.part.material;+ tris += g.index.count / 3;+ add(a.position, 12, key); add(a.normal, 12, key); add(a.uv, 8, key); add(a.color, 12, key);+ add(g.index, g.index.array.BYTES_PER_ELEMENT, key);+ if (m && m.normalMap && !seenTan.has(a.position)) { seenTan.add(a.position); bytes += a.position.count * 16; byMat[key] = (byMat[key] || 0) + a.position.count * 16; }+ }+ report.push('geomKB' + Math.round(bytes / 1024) + '_meshes' + PLACED.length + '_rendered' + Math.round(tris));+ Object.keys(byMat).sort((p, q) => byMat[q] - byMat[p]).forEach((k) => report.push(k + '_KB' + Math.round(byMat[k] / 1024)));+ ROOT.clear();+ createPart('SizeProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ report.forEach((d) => group('SIZE_' + d));+ return ROOT;+ }+ if (P.preview === 'textures') { // measures the texture payload on its own+ Object.keys(MAT).forEach((k, i) => createPart('Swatch_' + k, boxGeo(1, 1, 1), MAT[k], { position: [i * 1.2, 0.5, 0], parent: ROOT }));+ return ROOT;+ }+ if (P.preview === 'stats') {+ buildCathedral();+ const list = [...STAT.entries()].map(([b, e]) => ({ name: e.name, n: e.n, tris: b.I.length / 3, kb: Math.round((batchBytes(b) + (e.normalMapped ? b.count * 16 : 0)) / 1024) })).sort((a, c) => c.kb - a.kb);+ let total = 0, draws = 0, rendered = 0; for (const e of list) { total += e.kb; draws += e.n; rendered += e.n * e.tris; }+ createPart('StatProbe', boxGeo(1, 1, 1), gameMaterial(0x888888), { parent: ROOT });+ group('STAT_TOTAL_kb_' + total + '_draws_' + draws + '_rendered_' + rendered + '_unique_' + list.length);+ list.slice(0, 40).forEach((e, i) => group('STAT_' + String(i).padStart(2, '0') + '_K' + Math.round(e.n * e.tris / 1000) + '_' + e.name + '_x' + e.n + '_tris' + e.tris + '_kb' + e.kb));+ return ROOT;+ }+ return ROOT;+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-1000.avifnew file mode 100644index 00000000..05733719Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-1000.webpnew file mode 100644index 00000000..70df9e11Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-480.avifnew file mode 100644index 00000000..ac377a49Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-480.webpnew file mode 100644index 00000000..42beb78aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-768.avifnew file mode 100644index 00000000..5bde57feBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-768.webpnew file mode 100644index 00000000..c5e91971Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-after.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after.webpnew file mode 100644index 00000000..0b4929d6Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-after.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.avifnew file mode 100644index 00000000..2468279aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.webpnew file mode 100644index 00000000..f52ab53bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.avifnew file mode 100644index 00000000..57160523Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.webpnew file mode 100644index 00000000..c75e0b36Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-768.avifnew file mode 100644index 00000000..08621142Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-768.webpnew file mode 100644index 00000000..317b82eeBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1.webpnew file mode 100644index 00000000..fc75485aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-1.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.avifnew file mode 100644index 00000000..a1d68eccBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.webpnew file mode 100644index 00000000..a2cd4f95Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.avifnew file mode 100644index 00000000..52f8d2baBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.webpnew file mode 100644index 00000000..6379aeb4Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-768.avifnew file mode 100644index 00000000..b18d34a2Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-768.webpnew file mode 100644index 00000000..0460bb5fBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2.webpnew file mode 100644index 00000000..c2422107Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-art-2.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-1000.avifnew file mode 100644index 00000000..ac34692bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-1000.webpnew file mode 100644index 00000000..59747341Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-480.avifnew file mode 100644index 00000000..e532fa16Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-480.webpnew file mode 100644index 00000000..921c092eBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-768.avifnew file mode 100644index 00000000..0689d6a1Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-768.webpnew file mode 100644index 00000000..89192960Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-before.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before.webpnew file mode 100644index 00000000..deccfeb5Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-before.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.avifnew file mode 100644index 00000000..cd782977Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.webpnew file mode 100644index 00000000..35cabcf1Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.avifnew file mode 100644index 00000000..03449fcdBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.webpnew file mode 100644index 00000000..cad4a82aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.avifnew file mode 100644index 00000000..e18904e1Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.webpnew file mode 100644index 00000000..fab04899Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after.webpnew file mode 100644index 00000000..eab75ea5Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-after.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.avifnew file mode 100644index 00000000..7b447ea0Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.webpnew file mode 100644index 00000000..5b0ec4b6Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.avifnew file mode 100644index 00000000..8e6da66aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.webpnew file mode 100644index 00000000..4afd92dcBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.avifnew file mode 100644index 00000000..1ec86c9cBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.webpnew file mode 100644index 00000000..5102a8b4Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before.webpnew file mode 100644index 00000000..ab98431fBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-daylight-before.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1024.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1024.avifnew file mode 100644index 00000000..23849b48Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1024.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1024.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1024.webpnew file mode 100644index 00000000..f5b7d59cBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1024.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1440.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1440.avifnew file mode 100644index 00000000..d928244cBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1440.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1440.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1440.webpnew file mode 100644index 00000000..9dfeba67Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-1440.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-480.avifnew file mode 100644index 00000000..8cd92ffeBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-480.webpnew file mode 100644index 00000000..df2aed57Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-768.avifnew file mode 100644index 00000000..8b191b0dBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-768.webpnew file mode 100644index 00000000..671f05dcBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-final.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final.webpnew file mode 100644index 00000000..e7aa3493Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-final.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.avifnew file mode 100644index 00000000..3cd3f477Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.webpnew file mode 100644index 00000000..951a9d3bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.avifnew file mode 100644index 00000000..f0950244Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.webpnew file mode 100644index 00000000..f918af03Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.avifnew file mode 100644index 00000000..59a508fdBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.webpnew file mode 100644index 00000000..cb3700a3Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-768.avifnew file mode 100644index 00000000..f25756a5Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-768.webpnew file mode 100644index 00000000..c40fcd64Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look.webpnew file mode 100644index 00000000..a2d7fdf5Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-first-look.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1024.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1024.avifnew file mode 100644index 00000000..cd69c7f8Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1024.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1024.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1024.webpnew file mode 100644index 00000000..75e2cbddBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1024.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1440.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1440.avifnew file mode 100644index 00000000..0313f6a3Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1440.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1440.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1440.webpnew file mode 100644index 00000000..eb8e3e3cBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-1440.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-480.avifnew file mode 100644index 00000000..bb26cc3bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-480.webpnew file mode 100644index 00000000..9abc2c93Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-768.avifnew file mode 100644index 00000000..d4a15c09Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-768.webpnew file mode 100644index 00000000..bc9a4e67Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero.webpnew file mode 100644index 00000000..8de6fa10Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-hero.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.avifnew file mode 100644index 00000000..b06d348fBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.webpnew file mode 100644index 00000000..eeb434c1Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.avifnew file mode 100644index 00000000..acc27690Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.webpnew file mode 100644index 00000000..f8770d05Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-768.avifnew file mode 100644index 00000000..e205ab2bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-768.webpnew file mode 100644index 00000000..5b616a65Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after.webpnew file mode 100644index 00000000..72ce3214Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-after.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.avifnew file mode 100644index 00000000..ef491f5bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.webpnew file mode 100644index 00000000..ca40fe2eBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.avifnew file mode 100644index 00000000..04e15e2eBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.webpnew file mode 100644index 00000000..896723e0Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-768.avifnew file mode 100644index 00000000..ab07b000Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-768.webpnew file mode 100644index 00000000..c4c7a07aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before.webpnew file mode 100644index 00000000..2104d600Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-light-before.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1024.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1024.avifnew file mode 100644index 00000000..76df55aeBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1024.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1024.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1024.webpnew file mode 100644index 00000000..8327d8c2Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1024.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1440.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1440.avifnew file mode 100644index 00000000..f8fa88ddBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1440.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1440.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1440.webpnew file mode 100644index 00000000..81f72975Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-1440.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-480.avifnew file mode 100644index 00000000..d5bce54eBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-480.webpnew file mode 100644index 00000000..3a5dca9aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-768.avifnew file mode 100644index 00000000..84b85527Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-768.webpnew file mode 100644index 00000000..9bb93571Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave.webpnew file mode 100644index 00000000..96061ec5Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-nave.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.avifnew file mode 100644index 00000000..982960f6Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.webpnew file mode 100644index 00000000..26aa9f5dBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.avifnew file mode 100644index 00000000..a555a5d6Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.webpnew file mode 100644index 00000000..91ab84eeBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.avifnew file mode 100644index 00000000..4ff7ae7cBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.webpnew file mode 100644index 00000000..83203090Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after.webpnew file mode 100644index 00000000..5ec166adBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-after.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.avifnew file mode 100644index 00000000..041f5d00Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.webpnew file mode 100644index 00000000..55630ee5Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.avifnew file mode 100644index 00000000..b825c8f4Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.webpnew file mode 100644index 00000000..1d75bbe6Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.avifnew file mode 100644index 00000000..513f5300Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.webpnew file mode 100644index 00000000..805125a2Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before.webpnew file mode 100644index 00000000..3364101eBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-sun-before.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.avifnew file mode 100644index 00000000..aa500e49Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.webpnew file mode 100644index 00000000..c16c1cddBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.avifnew file mode 100644index 00000000..329c96f7Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.webpnew file mode 100644index 00000000..2f11f3d6Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-768.avifnew file mode 100644index 00000000..91a99e47Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-768.webpnew file mode 100644index 00000000..e4218e09Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2.webpnew file mode 100644index 00000000..de867a12Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-2.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.avifnew file mode 100644index 00000000..8a617eabBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.webpnew file mode 100644index 00000000..fb1bcfd9Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.avifnew file mode 100644index 00000000..464368a5Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.webpnew file mode 100644index 00000000..b3478100Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-768.avif b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-768.avifnew file mode 100644index 00000000..06dee55eBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-768.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-768.webpnew file mode 100644index 00000000..627a44cbBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3.webp b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3.webpnew file mode 100644index 00000000..68e2470aBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/cathedral-west-3.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-1.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/mech-1.kiln.jsnew file mode 100644index 00000000..3aa6b4d8--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/mech-1.kiln.js@@ -0,0 +1,1430 @@+// Heavy walking mech, ~6 m. Metres, +X forward, +Y up, +Z right. L = -Z, R = +Z.+// Every joint rests at identity rotation with asset-aligned axes, so a hinge is+// simply node.rotation.z (positive tips +X up to +Y; a hanging limb swings forward).+const meta = { name: 'Heavy Walker Mech', role: 'vehicle' };++const SK = {+ hip: [0, 3.42], knee: [0.70, 2.17], ankle: [-0.60, 1.12], toe: [0, 0.22],+ tipX: 0.95, toeH: 0.22, legZ: 0.86,+ waistY: 3.86,+ shoulder: [0.02, 5.00], elbow: [-0.12, 4.00], armZ: 1.60,+ cannon: [1.00, 4.00], wrist: [0.95, 4.00],+};++const WALK = { T: 2.4, keys: 120, duty: 0.64, stanceLen: 1.72, lift: 0.46, xC: 0.95, height: 3.34, bob: 0.055, sway: 0.11, surge: 0.03 };+WALK.stride = WALK.stanceLen / WALK.duty;+WALK.speed = WALK.stride / WALK.T;++const D2R = Math.PI / 180;+const TILE = 2.2;++// ---------------------------------------------------------------- vectors+const vAdd = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];+const vSub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];+const vMul = (a, k) => [a[0] * k, a[1] * k, a[2] * k];+const vDot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];+const vCross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];+const vLen = (a) => Math.hypot(a[0], a[1], a[2]);+const vUnit = (a) => { const l = vLen(a) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };+const vMix = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];+const vPerp = (d) => { const a = Math.abs(d[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]; return vUnit(vCross(a, d)); };++// ---------------------------------------------------------------- colour (linear)+const lin = (hex) => {+ const f = (c) => { const x = c / 255; return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); };+ return [f((hex >> 16) & 255), f((hex >> 8) & 255), f(hex & 255)];+};+const tone = (c, k) => [c[0] * k, c[1] * k, c[2] * k];+const PAINT = lin(0x87886f), PAINT2 = lin(0x6e715d), DARK = lin(0x4b4e4f), GUN = lin(0x404447), BONE = lin(0xcdc6ad), RUST = lin(0xa84a2c), PISTON = lin(0x6f7376), DUST = lin(0x9a8a6c), DRIP = lin(0x3b2a1d);+const HAZ_Y = lin(0xe8ae1c), HAZ_K = lin(0x1a1b1d), SEAM = lin(0x0a0b0c), WEAR = lin(0xe6e3d4), STEEL = lin(0xbcc1c5), STEEL_D = lin(0x7f8387);+const RUB = lin(0x1c1d1f), CAB_R = lin(0xa8361f), CAB_Y = lin(0xc99a1e), CAB_K = lin(0x202225), CAB_B = lin(0x2d4f63), SUIT = lin(0x8f4f22), WHITE = [1, 1, 1];++// ---------------------------------------------------------------- walk solve+function pchipPeriodic(keys) {+ const n = keys.length, xs = keys.map((k) => k[0]), ys = keys.map((k) => k[1]);+ const h = (i) => (i + 1 < n ? xs[i + 1] : xs[0] + 1) - xs[i];+ const sl = (i) => (ys[(i + 1) % n] - ys[i]) / h(i);+ const m = xs.map((_, i) => {+ const p = (i + n - 1) % n, s0 = sl(p), s1 = sl(i);+ if (s0 * s1 <= 0) return 0;+ const h0 = h(p), h1 = h(i), w1 = 2 * h1 + h0, w2 = h1 + 2 * h0;+ return (w1 + w2) / (w1 / s0 + w2 / s1);+ });+ return (fIn) => {+ const f = fIn - Math.floor(fIn);+ let i = n - 1;+ for (let j = 0; j < n; j++) if (xs[j] <= f) i = j;+ const hh = h(i), fx = f < xs[i] ? f + 1 : f, t = (fx - xs[i]) / hh, t2 = t * t, t3 = t2 * t;+ return (2 * t3 - 3 * t2 + 1) * ys[i] + (t3 - 2 * t2 + t) * m[i] * hh + (-2 * t3 + 3 * t2) * ys[(i + 1) % n] + (t3 - t2) * m[(i + 1) % n] * hh;+ };+}+const b0 = WALK.duty;+const swingF = (s) => b0 + (1 - b0) * s;+// Foot pitch never goes positive: the toes stay lowest and set down first, then the sole rolls flat.+const PSI = pchipPeriodic([[0, 0], [0.50, 0], [b0, -24], [swingF(0.22), -27], [swingF(0.5), -13], [swingF(0.78), -4], [swingF(0.93), -0.8]]);+const GAMMA = pchipPeriodic([[0, 50], [0.32, 34], [0.50, 23], [b0, 22], [swingF(0.38), 50], [swingF(0.72), 57]]);++function footState(f) {+ const xf = WALK.xC + WALK.stanceLen / 2, S = WALK.stride;+ let cx, cy = 0;+ if (f < b0) cx = xf - S * f;+ else {+ const s = (f - b0) / (1 - b0), x0 = xf - WALK.stanceLen, x1 = xf, m = -S * (1 - b0), s2 = s * s, s3 = s2 * s;+ cx = (2 * s3 - 3 * s2 + 1) * x0 + (s3 - 2 * s2 + s) * m + (-2 * s3 + 3 * s2) * x1 + (s3 - s2) * m;+ // Zero height velocity at both ends with finite curvature; the peak falls early in the swing.+ const b = Math.sin(Math.PI * s * (1.35 - 0.35 * s));+ cy = WALK.lift * b * b;+ }+ return { cx, cy, psi: PSI(f) * D2R };+}++const wrapA = (a) => { let x = a; while (x > Math.PI) x -= 2 * Math.PI; while (x < -Math.PI) x += 2 * Math.PI; return x; };+const bone2 = (a, b) => [b[0] - a[0], b[1] - a[1]];+const BT = bone2(SK.hip, SK.knee), BS = bone2(SK.knee, SK.ankle), BM = bone2(SK.ankle, SK.toe);+const LT = Math.hypot(BT[0], BT[1]), LS = Math.hypot(BS[0], BS[1]), LM = Math.hypot(BM[0], BM[1]);+const AT0 = Math.atan2(BT[1], BT[0]), AS0 = Math.atan2(BS[1], BS[0]), AM0 = Math.atan2(BM[1], BM[0]);++function eulerQ(xDeg, yDeg, zDeg) {+ return new THREE.Quaternion().setFromEuler(new THREE.Euler(xDeg * D2R, yDeg * D2R, zDeg * D2R, 'YXZ'));+}+const qx = (r) => new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), r);+const qz = (r) => new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), r);++function solveLeg(sgn, f, pelvisPos, qP) {+ const foot = footState(f);+ const cp = Math.cos(foot.psi), sp = Math.sin(foot.psi);+ const px = foot.cx + (-SK.tipX * cp - SK.toeH * sp);+ const py = foot.cy + (-SK.tipX * sp + SK.toeH * cp);+ const pz = sgn * SK.legZ;+ const hipOff = new THREE.Vector3(0, 0, sgn * SK.legZ).applyQuaternion(qP);+ const dx = px - (pelvisPos[0] + hipOff.x), dy = py - (pelvisPos[1] + hipOff.y), dz = pz - (pelvisPos[2] + hipOff.z);+ const y2 = -Math.hypot(dy, dz);+ const rho = Math.atan2(dz / y2, dy / y2);+ const g = GAMMA(f) * D2R;+ const ax = dx - LM * Math.sin(g), ay = y2 + LM * Math.cos(g);+ const d = Math.min(Math.hypot(ax, ay), (LT + LS) * 0.995);+ const a = Math.acos(Math.max(-1, Math.min(1, (LT * LT + d * d - LS * LS) / (2 * LT * d))));+ const aT = Math.atan2(ay, ax) + a;+ const kx = LT * Math.cos(aT), ky = LT * Math.sin(aT);+ const aS = Math.atan2(ay - ky, ax - kx);+ const aM = Math.atan2(-Math.cos(g), Math.sin(g));+ const tH = wrapA(aT - AT0), tS = wrapA(aS - AS0), tM = wrapA(aM - AM0);+ const qHip = qP.clone().invert().multiply(qx(rho)).multiply(qz(tH));+ const qToe = qx(rho).multiply(qz(tM)).invert().multiply(qz(foot.psi));+ return { hip: qHip, knee: qz(wrapA(tS - tH)), ankle: qz(wrapA(tM - tS)), toe: qToe };+}++function walkPose(phi) {+ const tau = 2 * Math.PI, c1 = (a) => Math.cos(tau * (phi - a)), c2 = (a) => Math.cos(2 * tau * (phi - a));+ const pelvisPos = [WALK.surge * c2(0.20), WALK.height + WALK.bob * c2(0.34), -WALK.sway * c1(0.34)];+ const qP = eulerQ(-2.6 * c1(0.30), -5.5 * c1(0), -2.0 + 0.5 * c2(0.25));+ const fL = phi % 1, fR = (phi + 0.5) % 1;+ const shL = -8 * c1(0.04), shR = 6.5 * c1(0.04);+ return {+ pelvisPos, pelvisQ: qP,+ L: solveLeg(-1, fL, pelvisPos, qP), R: solveLeg(1, fR, pelvisPos, qP),+ waist: eulerQ(-1.9 * c1(0.38), 12.5 * c1(0.04), 2.0 + 0.4 * c2(0.30)),+ shoulderL: qz(shL * D2R), shoulderR: qz(shR * D2R),+ elbowL: qz(0.55 * 8 * c1(0.12) * D2R), elbowR: qz(-0.55 * 6.5 * c1(0.12) * D2R),+ pod: qz(1.1 * c2(0.42) * D2R),+ };+}++function rigNodes(root) {+ const n = (s) => root.getObjectByName('Joint_' + s);+ const o = { pelvis: n('Pelvis'), waist: n('Waist'), pod: n('MissilePod'), shoulderL: n('Shoulder_L'), shoulderR: n('Shoulder_R'), elbowL: n('Elbow_L'), elbowR: n('Elbow_R') };+ for (const s of ['L', 'R']) o[s] = { hip: n('Hip_' + s), knee: n('Knee_' + s), ankle: n('Ankle_' + s), toe: n('Toe_' + s) };+ return o;+}+function applyPose(N, p) {+ N.pelvis.position.set(p.pelvisPos[0], p.pelvisPos[1], p.pelvisPos[2]);+ N.pelvis.quaternion.copy(p.pelvisQ);+ N.waist.quaternion.copy(p.waist);+ N.pod.quaternion.copy(p.pod);+ N.shoulderL.quaternion.copy(p.shoulderL); N.shoulderR.quaternion.copy(p.shoulderR);+ N.elbowL.quaternion.copy(p.elbowL); N.elbowR.quaternion.copy(p.elbowR);+ for (const s of ['L', 'R']) for (const j of ['hip', 'knee', 'ankle', 'toe']) N[s][j].quaternion.copy(p[s][j]);+}+function rigList(N) {+ const out = [N.pelvis, N.waist, N.pod, N.shoulderL, N.shoulderR, N.elbowL, N.elbowR];+ for (const s of ['L', 'R']) for (const j of ['hip', 'knee', 'ankle', 'toe']) out.push(N[s][j]);+ return out;+}++// ---------------------------------------------------------------- mesh kit+function newell(P) {+ let x = 0, y = 0, z = 0;+ for (let i = 0; i < P.length; i++) {+ const a = P[i], b = P[(i + 1) % P.length];+ x += (a[1] - b[1]) * (a[2] + b[2]); y += (a[2] - b[2]) * (a[0] + b[0]); z += (a[0] - b[0]) * (a[1] + b[1]);+ }+ return vUnit([x, y, z]);+}+function centroid(P) { let c = [0, 0, 0]; for (const p of P) c = vAdd(c, p); return vMul(c, 1 / P.length); }+function triangulate(P, n) {+ const m = P.length;+ if (m === 3) return [[0, 1, 2]];+ let convex = true;+ for (let i = 0; i < m; i++) {+ const a = P[i], b = P[(i + 1) % m], c = P[(i + 2) % m];+ if (vDot(vCross(vSub(b, a), vSub(c, b)), n) < -1e-7) { convex = false; break; }+ }+ const out = [];+ if (convex) { for (let i = 1; i < m - 1; i++) out.push([0, i, i + 1]); return out; }+ const e1 = vUnit(vSub(P[1], P[0])), e2 = vCross(n, e1);+ const contour = P.map((p) => new THREE.Vector2(vDot(vSub(p, P[0]), e1), vDot(vSub(p, P[0]), e2)));+ for (const t of THREE.ShapeUtils.triangulateShape(contour, [])) {+ const tn = vCross(vSub(P[t[1]], P[t[0]]), vSub(P[t[2]], P[t[0]]));+ out.push(vDot(tn, n) >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]]);+ }+ return out;+}+function insetPoly(P, n, d) {+ const m = P.length, out = [];+ for (let i = 0; i < m; i++) {+ const p = P[i], a = P[(i + m - 1) % m], b = P[(i + 1) % m];+ const n1 = vCross(n, vUnit(vSub(p, a))), n2 = vCross(n, vUnit(vSub(b, p)));+ const mt = vUnit(vAdd(n1, n2)), den = Math.max(0.35, vDot(mt, n1));+ out.push(vAdd(p, vMul(mt, d / den)));+ }+ return out;+}++function createKit(root, MAT, COLORED) {+ let G = null, matKey = 'armour', F = null, mir = 1, hullC = null, uo = 0, vo = 0, seed = 90125;+ const groups = [], pistons = [];+ const rnd = () => { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 4294967296; };+ const T = (p) => {+ let x = p[0], y = p[1], z = p[2];+ if (F) {+ const X = F.o[0] + F.u[0] * x + F.v[0] * y + F.w[0] * z;+ const Y = F.o[1] + F.u[1] * x + F.v[1] * y + F.w[1] * z;+ const Z = F.o[2] + F.u[2] * x + F.v[2] * y + F.w[2] * z;+ x = X; y = Y; z = Z;+ }+ return [x, y, mir < 0 ? -z : z];+ };+ const TN = (n) => {+ let x = n[0], y = n[1], z = n[2];+ if (F) {+ const X = F.u[0] * x + F.v[0] * y + F.w[0] * z, Y = F.u[1] * x + F.v[1] * y + F.w[1] * z, Z = F.u[2] * x + F.v[2] * y + F.w[2] * z;+ x = X; y = Y; z = Z;+ }+ return [x, y, mir < 0 ? -z : z];+ };+ const bucket = (key) => {+ const k = key || matKey;+ if (!G.b[k]) G.b[k] = { pos: [], nor: [], uv: [], col: [], idx: [] };+ return G.b[k];+ };+ const addV = (b, a, n, c, u, v) => {+ b.pos.push(a[0], a[1], a[2]); b.nor.push(n[0], n[1], n[2]); b.uv.push(u, v);+ const q = (x) => Math.max(0, Math.min(255, Math.round(x * 255)));+ // Road dust settles on everything near the ground (rest-pose height).+ const dk = a[1] < 1.3 ? 0.42 * Math.pow(1 - a[1] / 1.3, 1.4) : 0;+ b.col.push(q(c[0] + (DUST[0] - c[0]) * dk), q(c[1] + (DUST[1] - c[1]) * dk), q(c[2] + (DUST[2] - c[2]) * dk), 255);+ };+ const tri = (b, i, j, k) => { if (mir < 0) b.idx.push(i, k, j); else b.idx.push(i, j, k); };+ const faceBasis = (n) => {+ let e2 = vSub([0, 1, 0], vMul(n, n[1]));+ if (vLen(e2) < 0.25) e2 = vSub([1, 0, 0], vMul(n, n[0]));+ e2 = vUnit(e2);+ return [vCross(e2, n), e2];+ };+ const emit = (pts, n, cols, tris, key) => {+ const b = bucket(key), base = b.pos.length / 3, an = TN(n), [e1, e2] = faceBasis(an);+ pts.forEach((p, i) => {+ const a = T(p), c = Array.isArray(cols[0]) ? cols[i] : cols;+ addV(b, a, an, c, vDot(a, e1) / TILE + uo, vDot(a, e2) / TILE + vo);+ });+ for (const t of tris) tri(b, base + t[0], base + t[1], base + t[2]);+ };++ const kit = {};+ kit.rnd = rnd;+ kit.use = (g) => { G = g; };+ kit.mat = (k) => { matKey = k; };+ kit.frame = (f) => { F = f; };+ kit.mirror = (s) => { mir = s; };+ kit.hull = (p) => { hullC = p; };+ kit.bone = (a, b, z) => {+ const u = vUnit([b[0] - a[0], b[1] - a[1], 0]), w = [0, 0, 1];+ return { o: [a[0], a[1], z], u, v: vCross(w, u), w, L: Math.hypot(b[0] - a[0], b[1] - a[1]) };+ };+ kit.at = (f, u, v, w) => [f.o[0] + f.u[0] * u + f.v[0] * v, f.o[1] + f.u[1] * u + f.v[1] * v, f.o[2] + w];+ kit.group = (name, parent, origin, label) => {+ const rel = parent ? vSub(origin, parent.origin) : origin;+ const node = createPivot(name, rel, parent ? parent.node : root);+ const g = { name, label: label || name, node, origin, b: {} };+ groups.push(g);+ return g;+ };++ kit.poly = (ptsIn, col, o = {}) => {+ let pts = ptsIn, n = newell(pts);+ if (o.out && vDot(n, o.out) < 0) { pts = [...pts].reverse(); n = vMul(n, -1); if (Array.isArray(col[0])) col = [...col].reverse(); }+ emit(pts, n, col, triangulate(pts, n), o.mat);+ return n;+ };++ // Bevelled armour plate: front face at pts, bevel down to depth t. Bevel carries wear at the+ // top edge and contact shadow at the foot, so layered plates read with no extra texture.+ kit.plate = (ptsIn, t = 0.05, o = {}) => {+ let P = ptsIn, n = newell(P);+ const out = o.out || (hullC ? vSub(centroid(P), hullC) : null);+ if (out && vDot(n, out) < 0) { P = [...P].reverse(); n = vMul(n, -1); }+ const c = o.c !== undefined ? o.c : Math.min(t * 0.8, 0.028);+ const inner = insetPoly(P, n, c);+ const base = tone(o.col || PAINT, 0.92 + rnd() * 0.15);+ const edgeCol = o.edge || WEAR, low = tone(vMix(base, RUST, 0.18), 0.30);+ const ys = P.map((p) => T(p)[1]), y0 = Math.min(...ys), y1 = Math.max(...ys);+ const shade = (p) => tone(base, 0.84 + 0.22 * (T(p)[1] - y0) / Math.max(0.05, y1 - y0));+ uo = rnd() * 7; vo = rnd() * 7;+ emit(inner, n, inner.map(shade), triangulate(inner, n), o.mat);+ // Bevel in short runs with uneven wear, so edges chip in patches instead of reading as an outline.+ const m = P.length, drop = vMul(n, t);+ const wear = () => { const r = rnd(); return r < 0.45 ? 0.04 + r * 0.25 : 0.2 + r * 0.55; };+ const nodeW = P.map(() => wear());+ for (let i = 0; i < m; i++) {+ const j = (i + 1) % m, runs = Math.max(1, Math.min(4, Math.round(vLen(vSub(P[j], P[i])) / 0.3)));+ let w0 = nodeW[i];+ for (let k = 0; k < runs; k++) {+ const w1 = k === runs - 1 ? nodeW[j] : wear();+ const a0 = vMix(P[i], P[j], k / runs), a1 = vMix(P[i], P[j], (k + 1) / runs);+ const i0 = vMix(inner[i], inner[j], k / runs), i1 = vMix(inner[i], inner[j], (k + 1) / runs);+ const q = [vSub(a0, drop), vSub(a1, drop), i1, i0];+ emit(q, newell(q), [low, low, vMix(base, edgeCol, w1), vMix(base, edgeCol, w0)], [[0, 1, 2], [0, 2, 3]], o.mat);+ w0 = w1;+ }+ }+ // Close the plate so an overhang is solid when seen from behind.+ const backPts = [...P].reverse().map((p) => vSub(p, drop)), backN = vMul(n, -1);+ emit(backPts, backN, low, triangulate(backPts, backN), o.mat);+ const h = { P, inner, n, t, col: base };+ if (!o.noDrip && !o.mat) kit.drips(h);+ return h;+ };++ // Rust and grime runs: narrow tapered strips that follow gravity (rest pose) down a plate and fade into the paint.+ kit.drips = (h) => {+ const dn = F ? [-F.u[1], -F.v[1], -F.w[1]] : [0, -1, 0];+ const flat = vSub(dn, vMul(h.n, vDot(dn, h.n)));+ if (vLen(flat) < 0.35) return;+ const d = vUnit(flat), inn = h.inner, m = inn.length, c = centroid(inn);+ let area = 0;+ for (let i = 0; i < m; i++) area += vDot(vCross(vSub(inn[i], c), vSub(inn[(i + 1) % m], c)), h.n) / 2;+ if (area < 0.05) return;+ const inside = (p) => inn.every((v, i) => vDot(vCross(vSub(inn[(i + 1) % m], v), vSub(p, v)), h.n) >= 0);+ const lift = vMul(h.n, 0.003), count = Math.max(1, Math.min(6, Math.round(area * 14)));+ for (let i = 0; i < count; i++) {+ let s = vMix(vMix(inn[Math.floor(rnd() * m)], inn[Math.floor(rnd() * m)], rnd()), c, 0.14);+ if (vDot(vSub(s, c), d) > 0) s = vSub(c, vSub(s, c));+ if (!inside(s)) continue;+ let e = vAdd(s, vMul(d, 0.14 + rnd() * 0.42));+ for (let k = 0; k < 6 && !inside(e); k++) e = vMix(s, e, 0.72);+ const w = 0.007 + rnd() * rnd() * 0.05, side = vMul(vCross(h.n, d), w / 2);+ const A = vAdd(s, lift), B = vAdd(e, lift), heavy = 0.35 + rnd() * 0.4;+ const topC = vMix(h.col, DRIP, heavy), botC = vMix(h.col, DRIP, 0.03);+ emit([vAdd(A, side), vAdd(B, vMul(side, 0.45)), vSub(B, vMul(side, 0.45)), vSub(A, side)], h.n, [topC, botC, botC, topC], [[0, 1, 2], [0, 2, 3]]);+ }+ };+ // Panel line: a dark groove with a thin lit lip on one side, so it holds up at mid distance.+ kit.seam = (h, a, b, w = 0.016, col = SEAM) => {+ const d = vUnit(vSub(b, a)), s = vMul(vCross(h.n, d), w / 2), l = vMul(h.n, 0.004);+ const A = vAdd(a, l), B = vAdd(b, l);+ emit([vSub(A, s), vSub(B, s), vAdd(B, s), vAdd(A, s)], h.n, col, [[0, 1, 2], [0, 2, 3]]);+ const lip = vMul(vUnit(s), 0.006), lit = vMix(h.col, WEAR, 0.38);+ emit([vAdd(A, s), vAdd(B, s), vAdd(vAdd(B, s), lip), vAdd(vAdd(A, s), lip)], h.n, lit, [[0, 1, 2], [0, 2, 3]]);+ };+ kit.loop = (h, d = 0.05, w = 0.016) => {+ const pts = insetPoly(h.P, h.n, d);+ pts.forEach((p, i) => kit.seam(h, p, pts[(i + 1) % pts.length], w));+ };+ kit.bolt = (p, n, r = 0.022, col = STEEL_D) => {+ const e1 = vPerp(n), e2 = vCross(n, e1), l = vAdd(p, vMul(n, 0.007)), pts = [];+ for (let i = 0; i < 6; i++) { const a = (i / 6) * 2 * Math.PI; pts.push(vAdd(l, vAdd(vMul(e1, Math.cos(a) * r), vMul(e2, Math.sin(a) * r)))); }+ emit(pts, n, col, [[0, 1, 2], [0, 2, 3], [0, 3, 4], [0, 4, 5]]);+ };+ kit.bolts = (h, d = 0.05, each = 1, r = 0.02) => {+ const pts = insetPoly(h.P, h.n, d);+ pts.forEach((p, i) => {+ const q = pts[(i + 1) % pts.length];+ for (let k = 0; k < each; k++) kit.bolt(vMix(p, q, k / each), h.n, r);+ });+ };+ kit.hazard = (h, sw = 0.09, skewK = 1) => {+ const q0 = h.inner[0], q1 = h.inner[1], q2 = h.inner[2], q3 = h.inner[3];+ const L = vLen(vSub(q1, q0)), W = vLen(vSub(q3, q0)), ns = Math.max(2, Math.round(L / (2 * sw))), ds = 1 / (2 * ns), skew = (W / L) * skewK;+ const lift = vMul(h.n, 0.004);+ const map = (s, t) => vAdd(vMix(vMix(q0, q1, s), vMix(q3, q2, s), t), lift);+ for (let k = -ns - 2; k < ns + 2; k++) {+ const s0 = k * 2 * ds + ds / 2;+ let poly2 = [[s0, 0], [s0 + ds, 0], [s0 + ds + skew, 1], [s0 + skew, 1]];+ for (const [lim, sign] of [[0, 1], [1, -1]]) {+ const out = [];+ for (let i = 0; i < poly2.length; i++) {+ const a = poly2[i], b = poly2[(i + 1) % poly2.length];+ const da = (a[0] - lim) * sign, db = (b[0] - lim) * sign;+ if (da >= 0) out.push(a);+ if (da * db < 0) { const u = da / (da - db); out.push([a[0] + (b[0] - a[0]) * u, a[1] + (b[1] - a[1]) * u]); }+ }+ poly2 = out;+ if (!poly2.length) break;+ }+ if (poly2.length >= 3) {+ const pts = poly2.map((p) => map(p[0], p[1]));+ const c = tone(HAZ_Y, 0.82 + rnd() * 0.24);+ const n = newell(pts);+ if (vDot(n, h.n) < 0) pts.reverse();+ emit(pts, h.n, c, triangulate(pts, h.n));+ }+ }+ };+ kit.hazardPlate = (pts, t, o = {}) => {+ const h = kit.plate(pts, t, { ...o, col: HAZ_K, edge: lin(0x77776c), noDrip: true });+ kit.hazard(h, o.sw || 0.09, o.skew === undefined ? 1 : o.skew);+ return h;+ };++ const SEG7 = { 0: 'abcdef', 1: 'bc', 2: 'abged', 3: 'abgcd', 4: 'fgbc', 5: 'afgcd', 6: 'afgedc', 7: 'abc', 8: 'abcdefg', 9: 'abcdfg', A: 'abcefg', E: 'afged', H: 'fgbec', K: 'fgecb', L: 'fed', M: 'abcef', R: 'abcefg', T: 'afe', '-': 'g' };+ kit.stencil = (o, rIn, u, str, hgt, col, nOut) => {+ const r = mir < 0 ? vMul(rIn, -1) : rIn, adv = hgt * 0.72, w = hgt * 0.5, th = hgt * 0.14;+ const start = mir < 0 ? vAdd(o, vMul(rIn, adv * str.length)) : o;+ const seg = (A, B, C, D) => {+ const lift = vMul(nOut, 0.005), pts = [A, B, C, D].map((p) => vAdd(p, lift));+ kit.poly(pts, col, { out: nOut });+ };+ [...str].forEach((ch, i) => {+ const org = vAdd(start, vMul(r, adv * i)), P = (x, y) => vAdd(org, vAdd(vMul(r, x * w), vMul(u, y * hgt)));+ const defs = { a: [0, 1, 1, 1], g: [0, 0.5, 1, 0.5], d: [0, 0, 1, 0], f: [0, 1, 0, 0.5], b: [1, 1, 1, 0.5], e: [0, 0.5, 0, 0], c: [1, 0.5, 1, 0] };+ for (const s of SEG7[ch] || '') {+ const [x0, y0, x1, y1] = defs[s], tx = th / w / 2, ty = th / hgt / 2;+ if (y0 === y1) seg(P(x0 - tx, y0 - ty), P(x1 + tx, y0 - ty), P(x1 + tx, y0 + ty), P(x0 - tx, y0 + ty));+ else seg(P(x0 - tx, y1 - ty), P(x0 + tx, y1 - ty), P(x0 + tx, y0 + ty), P(x0 - tx, y0 + ty));+ }+ });+ };++ // Revolved profile about an arbitrary axis; [d, r, col] entries, hard crease between bands.+ kit.lathe = (a, dirIn, prof, o = {}) => {+ const dir = vUnit(dirIn), seg = o.seg || 16, b = bucket(o.mat), col0 = o.col || GUN;+ const e1 = vPerp(dir), e2 = vCross(dir, e1);+ uo = rnd() * 7; vo = rnd() * 7;+ for (let i = 0; i < prof.length - 1; i++) {+ const [d0, r0, c0] = prof[i], [d1, r1] = prof[i + 1];+ if (r0 < 1e-4 && r1 < 1e-4) continue;+ const dd = d1 - d0, dr = r1 - r0, l = Math.hypot(dd, dr) || 1, base = b.pos.length / 3, col = c0 || col0;+ for (let k = 0; k <= seg; k++) {+ const th = (k / seg) * 2 * Math.PI, cs = Math.cos(th), sn = Math.sin(th);+ const rad = vAdd(vMul(e1, cs), vMul(e2, sn));+ const nn = vUnit(vAdd(vMul(rad, dd / l), vMul(dir, -dr / l)));+ const an = TN(nn);+ const u = (th * Math.max(r0, r1, 0.05)) / TILE + uo;+ addV(b, T(vAdd(vAdd(a, vMul(dir, d0)), vMul(rad, r0))), an, col, u, d0 / TILE + vo);+ addV(b, T(vAdd(vAdd(a, vMul(dir, d1)), vMul(rad, r1))), an, col, u, d1 / TILE + vo);+ }+ for (let k = 0; k < seg; k++) {+ const i0 = base + k * 2, i1 = i0 + 1, i2 = i0 + 2, i3 = i0 + 3;+ if (r0 >= 1e-4 && r1 >= 1e-4) { tri(b, i0, i2, i3); tri(b, i0, i3, i1); }+ else if (r1 < 1e-4) tri(b, i0, i2, i1);+ else tri(b, i0, i3, i1);+ }+ }+ };+ kit.cyl = (a, c, r0, r1, o = {}) => {+ const L = vLen(vSub(c, a));+ kit.lathe(a, vSub(c, a), [[0, 0], [0, r0], [L, r1], [L, 0]], { seg: o.seg || 8, col: o.col, mat: o.mat });+ };++ const crPt = (p0, p1, p2, p3, t) => {+ const t2 = t * t, t3 = t2 * t, f = (i) => 0.5 * (2 * p1[i] + (p2[i] - p0[i]) * t + (2 * p0[i] - 5 * p1[i] + 4 * p2[i] - p3[i]) * t2 + (3 * p1[i] - p0[i] - 3 * p2[i] + p3[i]) * t3);+ return [f(0), f(1), f(2)];+ };+ kit.tube = (ctrl, r, o = {}) => {+ const sp = o.sp || 3, seg = o.seg || 6, col = o.col || RUB, b = bucket(o.mat || 'rubber');+ const P = [ctrl[0], ...ctrl, ctrl[ctrl.length - 1]], path = [];+ for (let i = 1; i < P.length - 2; i++) for (let k = 0; k < sp; k++) path.push(crPt(P[i - 1], P[i], P[i + 1], P[i + 2], k / sp));+ path.push(ctrl[ctrl.length - 1]);+ let nrm = vPerp(vUnit(vSub(path[1], path[0]))), acc = 0;+ const base = b.pos.length / 3;+ path.forEach((p, i) => {+ const t = vUnit(vSub(path[Math.min(i + 1, path.length - 1)], path[Math.max(i - 1, 0)]));+ nrm = vUnit(vSub(nrm, vMul(t, vDot(nrm, t))));+ const bn = vCross(t, nrm);+ if (i) acc += vLen(vSub(p, path[i - 1]));+ for (let k = 0; k <= seg; k++) {+ const th = (k / seg) * 2 * Math.PI, rad = vAdd(vMul(nrm, Math.cos(th)), vMul(bn, Math.sin(th)));+ addV(b, T(vAdd(p, vMul(rad, r))), TN(rad), col, k / seg, acc / TILE);+ }+ });+ const stride = seg + 1;+ for (let i = 0; i < path.length - 1; i++) for (let k = 0; k < seg; k++) {+ const a0 = base + i * stride + k, a1 = a0 + 1, c0 = a0 + stride, c1 = c0 + 1;+ tri(b, a0, a1, c1); tri(b, a0, c1, c0);+ }+ };+ kit.cables = (ctrl, spread, spec) => {+ spec.forEach((c, i) => {+ const off = (i - (spec.length - 1) / 2) * spread;+ const pts = ctrl.map((p, k) => (k === 0 || k === ctrl.length - 1 ? p : vAdd(p, [off * 0.7, off, 0])));+ kit.tube(pts, c.r, { col: c.col });+ });+ };++ kit.block = (c, s, o = {}) => {+ const ax = o.ax || [[1, 0, 0], [0, 1, 0], [0, 0, 1]], col = o.col || GUN, ch = o.c === undefined ? 0.012 : o.c, h = [s[0] / 2, s[1] / 2, s[2] / 2];+ const edge = o.edge || vMix(col, WEAR, 0.45), key = o.mat;+ const P = (x, y, z) => vAdd(c, vAdd(vAdd(vMul(ax[0], x), vMul(ax[1], y)), vMul(ax[2], z)));+ const q = (a, b, cc) => { const v = [0, 0, 0]; v[0] = a; v[1] = b; v[2] = cc; return P(v[0], v[1], v[2]); };+ uo = rnd() * 7; vo = rnd() * 7;+ for (let a = 0; a < 3; a++) for (const sg of [-1, 1]) {+ const b1 = (a + 1) % 3, b2 = (a + 2) % 3, pts = [];+ for (const [s1, s2] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) {+ const v = [0, 0, 0]; v[a] = sg * h[a]; v[b1] = s1 * (h[b1] - ch); v[b2] = s2 * (h[b2] - ch);+ pts.push(q(v[0], v[1], v[2]));+ }+ kit.poly(pts, col, { out: vMul(ax[a], sg), mat: key });+ }+ if (ch > 0) {+ for (let a = 0; a < 3; a++) {+ const b1 = (a + 1) % 3, b2 = (a + 2) % 3;+ for (const s1 of [-1, 1]) for (const s2 of [-1, 1]) {+ const V = (w, t1, t2) => { const v = [0, 0, 0]; v[a] = w * (h[a] - ch); v[b1] = s1 * (h[b1] - t1); v[b2] = s2 * (h[b2] - t2); return q(v[0], v[1], v[2]); };+ kit.poly([V(-1, 0, ch), V(1, 0, ch), V(1, ch, 0), V(-1, ch, 0)], edge, { out: vAdd(vMul(ax[b1], s1), vMul(ax[b2], s2)), mat: key });+ }+ }+ for (const sx of [-1, 1]) for (const sy of [-1, 1]) for (const sz of [-1, 1]) {+ kit.poly([P(sx * h[0], sy * (h[1] - ch), sz * (h[2] - ch)), P(sx * (h[0] - ch), sy * h[1], sz * (h[2] - ch)), P(sx * (h[0] - ch), sy * (h[1] - ch), sz * h[2])], edge, { out: vAdd(vAdd(vMul(ax[0], sx), vMul(ax[1], sy)), vMul(ax[2], sz)), mat: key });+ }+ }+ };+ kit.beam = (a, b, s1, s2, o = {}) => {+ const u = vUnit(vSub(b, a)), up = o.up || (Math.abs(u[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]), w = vUnit(vCross(u, up)), v = vCross(w, u);+ kit.block(vMix(a, b, 0.5), [vLen(vSub(b, a)), s1, s2], { ...o, ax: [u, v, w] });+ };++ kit.loft = (ringsIn, o = {}) => {+ let rings = ringsIn;+ const d = vSub(centroid(rings[1]), centroid(rings[0]));+ if (vDot(newell(rings[0]), d) < 0) rings = rings.map((r) => [...r].reverse());+ const col = o.col || DARK;+ uo = rnd() * 7; vo = rnd() * 7;+ for (let i = 0; i < rings.length - 1; i++) {+ const A = rings[i], B = rings[i + 1], m = A.length;+ for (let j = 0; j < m; j++) {+ const k = (j + 1) % m, q = [A[j], A[k], B[k], B[j]];+ if (vLen(vSub(A[j], A[k])) < 1e-5 && vLen(vSub(B[j], B[k])) < 1e-5) continue;+ emit(q, newell(q), o.cols ? o.cols[i] || col : col, [[0, 1, 2], [0, 2, 3]], o.mat);+ }+ }+ if (o.capStart !== false) { const r = rings[0], n = vMul(newell(r), -1), R = [...r].reverse(); emit(R, n, col, triangulate(R, n), o.mat); }+ if (o.capEnd !== false) { const r = rings[rings.length - 1], n = newell(r); emit(r, n, col, triangulate(r, n), o.mat); }+ };++ kit.drum = (c, axisIn, r, w, o = {}) => {+ const axis = vUnit(axisIn), seg = o.seg || Math.max(14, Math.round(r * 56)), a = vAdd(c, vMul(axis, -w / 2));+ const body = o.col || GUN, rec = o.rec || tone(body, 0.5), boss = o.boss || STEEL;+ const rim = Math.min(0.035, w * 0.14), dep = Math.min(0.03, w * 0.1), bright = vMix(body, WEAR, 0.6);+ kit.lathe(a, axis, [+ [0, 0, boss], [0, r * 0.30, boss], [dep, r * 0.30, rec], [dep, r * 0.74, rec], [0, r * 0.80, body], [0, r * 0.96, bright], [rim, r, body],+ [w - rim, r, bright], [w, r * 0.96, body], [w, r * 0.80, rec], [w - dep, r * 0.74, rec], [w - dep, r * 0.30, boss], [w, r * 0.30, boss], [w, 0, boss],+ ], { seg, mat: o.mat || 'steel' });+ const prev = matKey; matKey = o.mat || 'steel';+ const e1 = vPerp(axis), e2 = vCross(axis, e1);+ const nb = o.bolts === undefined ? 8 : o.bolts;+ for (let i = 0; i < nb; i++) {+ const th = (i / nb) * 2 * Math.PI, rad = vAdd(vMul(e1, Math.cos(th)), vMul(e2, Math.sin(th)));+ for (const sg of [-1, 1]) kit.bolt(vAdd(vAdd(c, vMul(axis, sg * (w / 2 - dep))), vMul(rad, r * 0.53)), vMul(axis, sg), Math.min(0.022, r * 0.08), STEEL_D);+ }+ if (o.gear) {+ const tw = ((2 * Math.PI * r) / o.gear) * 0.5;+ for (let i = 0; i < o.gear; i++) {+ const th = (i / o.gear) * 2 * Math.PI, rad = vAdd(vMul(e1, Math.cos(th)), vMul(e2, Math.sin(th))), tan = vCross(axis, rad);+ kit.block(vAdd(c, vMul(rad, r + 0.012)), [0.034, tw, w * (o.gearW || 0.5)], { ax: [rad, tan, axis], c: 0, col: STEEL_D });+ }+ }+ matKey = prev;+ };++ kit.vent = (q0, q1, q2, q3, o = {}) => {+ const n = newell([q0, q1, q2, q3]), N = o.n || 5, slat = o.col || GUN;+ kit.poly([q0, q1, q2, q3], o.base || tone(GUN, 0.25), { out: n, mat: o.glow });+ const lift = vMul(n, o.depth || 0.022);+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.08) / N, t1 = (i + 0.72) / N;+ const a = vAdd(vMix(q0, q3, t0), lift), b = vAdd(vMix(q1, q2, t0), lift), c = vMix(q1, q2, t1), d = vMix(q0, q3, t1);+ kit.poly([a, b, vAdd(c, vMul(lift, 0.25)), vAdd(d, vMul(lift, 0.25))], [slat, slat, vMix(slat, WEAR, 0.3), vMix(slat, WEAR, 0.3)], {});+ }+ };++ // Clevis ears either side of a piston pin, from the anchor back to the bone surface.+ kit.lug = (A, base, zc, o = {}) => {+ const w = o.w || 0.085, ear = o.ear || 0.085, foot = o.foot || 0.12, t = o.t || 0.03;+ const d = vUnit(vSub(base, A)), p = [-d[1], d[0], 0];+ const pts = (z) => [+ [A[0] + p[0] * ear, A[1] + p[1] * ear, z], [A[0] - d[0] * ear, A[1] - d[1] * ear, z], [A[0] - p[0] * ear, A[1] - p[1] * ear, z],+ [base[0] - p[0] * foot, base[1] - p[1] * foot, z], [base[0] + p[0] * foot, base[1] + p[1] * foot, z],+ ];+ const col = o.col || GUN;+ kit.plate(pts(zc + w), t, { out: [0, 0, 1], col, c: 0.012 });+ kit.plate(pts(zc - w), t, { out: [0, 0, -1], col, c: 0.012 });+ };++ kit.piston = (name, gA, A, gB, B, o = {}) => { pistons.push({ name, gA, A, gB, B, mir, o, F }); };+ kit.pistonList = () => pistons;++ kit.buildPiston = (p, minSpan, maxSpan) => {+ const { name, gA, A, gB, B, o } = p;+ mir = p.mir; F = null;+ const sgn = mir;+ const actual = (q) => [q[0], q[1], q[2] * sgn];+ const cylG = kit.group('Piston_' + name + '_Cyl', gA, actual(A), 'Piston_' + name + '_Cyl');+ const rodG = kit.group('Piston_' + name + '_Rod', gB, actual(B), 'Piston_' + name + '_Rod');+ const dir = vUnit(vSub(B, A)), back = vMul(dir, -1);+ const r = o.r || 0.09, rr = o.rod || r * 0.56;+ // Equal cylinder and rod: the rod stays inserted past full Walk extension, the gland clears the rod end at its shortest.+ const Lc = Math.max(0.2, Math.min((maxSpan + Math.max(0.18, 0.2 * maxSpan)) / 2, minSpan - 0.05)), Lr = Lc;+ const zAxis = [0, 0, 1], body = PISTON;+ // Cylinder+ kit.use(cylG); kit.mat('steel');+ kit.drum(A, zAxis, r * 0.72, 0.13, { bolts: 0, seg: 12, boss: STEEL });+ const col1 = vMix(body, WEAR, 0.5);+ kit.lathe(A, dir, [+ [r * 0.5, 0, STEEL_D], [r * 0.5, r * 1.16, STEEL_D], [r * 1.5, r * 1.16, col1], [r * 1.65, r, body],+ [Lc - r * 1.3, r, col1], [Lc - r * 1.3, r * 1.14, STEEL_D], [Lc - r * 0.35, r * 1.14, col1], [Lc, r * 0.75, STEEL], [Lc, 0, STEEL],+ ], { seg: o.seg || 14 });+ const s2 = vUnit(vCross(dir, [0, 0, 1]));+ const hoseOff = vMul(s2, r + 0.016);+ const hp = (d) => vAdd(vAdd(A, vMul(dir, d)), hoseOff);+ kit.tube([vAdd(A, vMul(dir, r * 1.2)), hp(r * 1.7), hp(Lc * 0.5), hp(Lc - r * 1.6), vAdd(A, vMul(dir, Lc - r * 0.9))], Math.max(0.011, r * 0.14), { col: o.hose || CAB_K, sp: 2, seg: 5 });+ // Rod+ kit.use(rodG); kit.mat('steel');+ kit.drum(B, zAxis, rr * 1.16, 0.12, { bolts: 0, seg: 12, boss: STEEL });+ kit.lathe(B, back, [[rr * 0.8, 0, STEEL_D], [rr * 0.8, rr * 1.24, STEEL_D], [rr * 2.1, rr * 1.24, STEEL_D], [rr * 2.3, rr, STEEL_D], [rr * 2.3, 0, STEEL_D]], { seg: 12 });+ kit.mat('chrome');+ kit.lathe(B, back, [[rr * 2.3, rr], [Lr, rr], [Lr, 0]], { seg: o.seg || 12, mat: 'chrome' });+ // Aim constraint data, usable from runtime code as well as by the baked clip.+ const dA = actual(dir);+ cylG.node.userData = { aimAt: rodG.node.name, aimRestDir: dA };+ rodG.node.userData = { aimAt: cylG.node.name, aimRestDir: vMul(dA, -1) };+ };++ kit.flush = () => {+ for (const g of groups) {+ for (const key of Object.keys(g.b)) {+ const b = g.b[key];+ if (!b.idx.length) continue;+ const n = b.pos.length / 3, pos = new Float32Array(n * 3);+ for (let i = 0; i < n; i++) {+ pos[i * 3] = b.pos[i * 3] - g.origin[0]; pos[i * 3 + 1] = b.pos[i * 3 + 1] - g.origin[1]; pos[i * 3 + 2] = b.pos[i * 3 + 2] - g.origin[2];+ }+ const geo = new THREE.BufferGeometry();+ geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));+ geo.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(b.nor), 3));+ geo.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(b.uv), 2));+ if (COLORED[key]) geo.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(b.col), 4, true));+ geo.setIndex(b.idx);+ geo.computeBoundingBox(); geo.computeBoundingSphere();+ createPart(g.label + '_' + key, geo, MAT[key], { parent: g.node });+ }+ }+ };+ return kit;+}++// ---------------------------------------------------------------- textures+function makeTextures() {+ const prng = (s0) => { let s = s0 >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; };+ const chip = (w, h) => ({ kind: 'polygon', points: [[0, Math.round(h * 0.4)], [Math.round(w * 0.3), 0], [Math.round(w * 0.75), Math.round(h * 0.1)], [w, Math.round(h * 0.5)], [Math.round(w * 0.7), h], [Math.round(w * 0.25), Math.round(h * 0.85)]] });+ const area = { x: 24, y: 24, width: 464, height: 464 };+ const chips = [{ s: chip(6, 4), n: 66, seed: 11 }, { s: chip(7, 5), n: 86, seed: 22 }, { s: chip(14, 8), n: 14, seed: 33 }, { s: chip(4, 3), n: 90, seed: 44 }];+ const rand = prng(7);+ const scratches = [];+ for (let i = 0; i < 16; i++) {+ const x = 20 + Math.round(rand() * 470), y = 20 + Math.round(rand() * 470), a = rand() * 6.28, l = 30 + rand() * 120;+ const x1 = Math.max(0, Math.min(511, Math.round(x + Math.cos(a) * l))), y1 = Math.max(0, Math.min(511, Math.round(y + Math.sin(a) * l)));+ scratches.push({ kind: 'line', from: [x, y], to: [x1, y1], width: 1 });+ }+ const streaks = [];+ for (let i = 0; i < 14; i++) {+ const x = Math.round(rand() * 511);+ streaks.push({ op: 'paint', shape: { kind: 'line', from: [x, 0], to: [x, 511], width: 1 + Math.round(rand() * 3) }, rgba: [84, 76, 66, 255], opacity: 18 + Math.round(rand() * 26) });+ }+ const albedo = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'albedo', name: 'Armour_Wear_Albedo',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 512, usage: 'albedo', layers: [+ { op: 'solid', color: 0xf2f2ee },+ { op: 'noise', colorA: 0xa8a69a, colorB: 0xffffff, scale: 4, octaves: 4, seed: 41, blend: 'multiply', opacity: 0.9 },+ { op: 'noise', colorA: 0xd8d6cc, colorB: 0xffffff, scale: 34, octaves: 2, seed: 77, blend: 'multiply', opacity: 0.55 },+ ] } },+ ...streaks,+ { op: 'stamps', shape: chips[0].s, rgba: [122, 110, 96, 255], count: chips[0].n, seed: chips[0].seed, area },+ { op: 'stamps', shape: chips[1].s, rgba: [232, 230, 220, 255], count: chips[1].n, seed: chips[1].seed, area },+ { op: 'stamps', shape: chips[2].s, rgba: [150, 112, 84, 255], count: chips[2].n, seed: chips[2].seed, area },+ { op: 'stamps', shape: chips[3].s, rgba: [232, 230, 220, 255], count: chips[3].n, seed: chips[3].seed, area },+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [255, 255, 250, 255], opacity: 120 })),+ ],+ palette: { colors: [0x7a6e60, 0x967054, 0x8c867c, 0x9c988e, 0xa8a59a, 0xb4b1a6, 0xbfbcb1, 0xc9c6bb, 0xd2cfc5, 0xdad8ce, 0xe2e0d6, 0xe9e7de, 0xf0eee6, 0xf6f5ee, 0xfffffa], dither: 'none' },+ });+ const mr = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'metallicRoughness', name: 'Armour_Wear_MR',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 512, usage: 'metallicRoughness', layers: [+ { op: 'solid', color: 0xffa014 },+ { op: 'noise', colorA: 0xff8410, colorB: 0xffbe1c, scale: 6, octaves: 3, seed: 41 },+ ] } },+ { op: 'stamps', shape: chips[0].s, rgba: [255, 200, 20, 255], count: chips[0].n, seed: chips[0].seed, area },+ { op: 'stamps', shape: chips[1].s, rgba: [255, 130, 120, 255], count: chips[1].n, seed: chips[1].seed, area },+ { op: 'stamps', shape: chips[2].s, rgba: [255, 215, 30, 255], count: chips[2].n, seed: chips[2].seed, area },+ { op: 'stamps', shape: chips[3].s, rgba: [255, 130, 120, 255], count: chips[3].n, seed: chips[3].seed, area },+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [255, 110, 150, 255] })),+ ],+ });+ const seamOps = [];+ const H = (y, x0, x1) => { seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y, width: x1 - x0, height: 2 }, rgba: [128, 72, 235, 255] }); seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y + 2, width: x1 - x0, height: 2 }, rgba: [128, 184, 235, 255] }); };+ const Vt = (x, y0, y1) => { seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x, y: y0, width: 2, height: y1 - y0 }, rgba: [72, 128, 235, 255] }); seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x + 2, y: y0, width: 2, height: y1 - y0 }, rgba: [184, 128, 235, 255] }); };+ H(70, 0, 512); H(255, 60, 400); H(410, 0, 512); Vt(90, 70, 410); Vt(330, 0, 255); Vt(460, 255, 512);+ const nrm = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'normal', name: 'Armour_Wear_Normal', background: [128, 128, 255, 255],+ operations: [+ ...seamOps,+ { op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 6, height: 6 }, rgba: [150, 150, 245, 255], count: 90, seed: 55, area },+ { op: 'stamps', shape: chips[2].s, rgba: [104, 150, 244, 255], count: chips[2].n, seed: chips[2].seed, area },+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [104, 152, 248, 255] })),+ ],+ });+ return { albedo, mr, nrm };+}++// ---------------------------------------------------------------- legs+function buildLeg(kit, side, pelvisG) {+ const z0 = SK.legZ, s = side === 'L' ? -1 : 1;+ kit.mirror(s);+ const hipG = kit.group('Hip_' + side, pelvisG, [SK.hip[0], SK.hip[1], s * z0], 'Thigh_' + side);+ const kneeG = kit.group('Knee_' + side, hipG, [SK.knee[0], SK.knee[1], s * z0], 'Shin_' + side);+ const ankleG = kit.group('Ankle_' + side, kneeG, [SK.ankle[0], SK.ankle[1], s * z0], 'Meta_' + side);+ const toeG = kit.group('Toe_' + side, ankleG, [SK.toe[0], SK.toe[1], s * z0], 'Foot_' + side);+ [hipG, kneeG, ankleG, toeG].forEach((g) => { g.node.userData = { hinge: g === hipG ? 'ball (z=flex)' : g === toeG ? 'ball (z=flex)' : 'z' }; });++ const thF = kit.bone(SK.hip, SK.knee, z0), shF = kit.bone(SK.knee, SK.ankle, z0), mtF = kit.bone(SK.ankle, SK.toe, z0);+ const ring = (u, vf, vr, wh) => [[u, vf, wh], [u, vf, -wh], [u, vr, -wh], [u, vr, wh]];+ const sideP = (w, pts, t, o = {}) => kit.plate(pts.map((p) => [p[0], p[1], w]), t, { out: [0, 0, Math.sign(w)], ...o });++ // ---- Thigh+ kit.use(hipG); kit.frame(thF); kit.hull([thF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.40, 0.56, { bolts: 10, gear: 26, gearW: 0.4 });+ kit.drum([0, 0, 0.30], [0, 0, 1], 0.16, 0.08, { bolts: 0, seg: 14, col: DARK });+ kit.mat('armour');+ kit.loft([ring(-0.05, 0.26, -0.28, 0.24), ring(0.40, 0.30, -0.30, 0.25), ring(1.0, 0.22, -0.24, 0.21), ring(thF.L + 0.02, 0.15, -0.15, 0.15)], { col: DARK });+ const t1 = sideP(0.29, [[0.34, -0.28], [1.02, -0.23], [1.22, -0.10], [1.22, 0.10], [1.02, 0.22], [0.40, 0.30], [0.30, 0.14], [0.30, -0.12]], 0.05);+ kit.loop(t1, 0.045); kit.bolts(t1, 0.03, 1, 0.016);+ const t2 = sideP(0.325, [[0.50, -0.20], [0.94, -0.16], [0.94, 0.16], [0.50, 0.21]], 0.035, { col: PAINT2 });+ kit.bolts(t2, 0.03);+ kit.seam(t2, [0.74, -0.18, 0.325], [0.74, 0.18, 0.325]);+ kit.stencil([0.67, -0.05, 0.325], [0, 1, 0], [-1, 0, 0], '07', 0.13, BONE, [0, 0, 1]);+ const t3 = kit.hazardPlate([[1.00, -0.19, 0.325], [1.00, 0.19, 0.325], [1.17, 0.09, 0.325], [1.17, -0.09, 0.325]], 0.03, { out: [0, 0, 1], sw: 0.05 });+ kit.seam(t3, t3.inner[0], t3.inner[1], 0.008);+ const t4 = sideP(-0.28, [[0.34, -0.27], [1.05, -0.20], [1.18, -0.08], [1.18, 0.08], [1.05, 0.20], [0.40, 0.29], [0.32, 0.12], [0.32, -0.12]], 0.04, { col: PAINT2 });+ kit.loop(t4, 0.05);+ const tf = kit.plate([[0.38, 0.335, 0.235], [1.04, 0.245, 0.185], [1.04, 0.245, -0.185], [0.38, 0.335, -0.235]], 0.05, { out: [0, 1, 0] });+ kit.seam(tf, [0.62, 0.30, 0.22], [0.62, 0.30, -0.22]); kit.seam(tf, [0.84, 0.27, 0.20], [0.84, 0.27, -0.20]);+ kit.bolts(tf, 0.03, 1, 0.016);+ const tb = kit.plate([[0.36, -0.315, 0.235], [1.10, -0.255, 0.185], [1.10, -0.255, -0.185], [0.36, -0.315, -0.235]], 0.04, { out: [0, -1, 0], col: GUN });+ kit.loop(tb, 0.05);+ // Servo pack on the outer rear of the thigh+ kit.mat('steel');+ kit.cyl([0.46, -0.205, 0.40], [0.86, -0.17, 0.40], 0.066, 0.066, { seg: 10, col: STEEL_D });+ for (const u of [0.52, 0.60, 0.68, 0.76]) kit.lathe([u, -0.205 + (u - 0.46) * 0.09, 0.40], [1, 0.09, 0], [[0, 0.07], [0, 0.082, GUN], [0.018, 0.082, GUN], [0.018, 0.07]], { seg: 10, col: GUN });+ kit.mat('rubber');+ kit.cables([[0, 0, 0.345], [0.30, 0.10, 0.37], [0.70, 0.235, 0.385], [1.05, 0.215, 0.375], [1.30, 0.08, 0.345], [thF.L, 0, 0.32]], 0.026, [{ r: 0.019, col: CAB_K }, { r: 0.015, col: CAB_R }, { r: 0.019, col: CAB_K }]);+ kit.mat('armour');++ const hipLugT = kit.at(thF, 0.62, -0.42, 0), hipLugB = kit.at(thF, 0.70, -0.27, 0);+ const kneeA = kit.at(thF, 0.30, 0.50, 0), kneeAb = kit.at(thF, 0.42, 0.31, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hipLugT, hipLugB, z0, { col: GUN });+ kit.lug(kneeA, kneeAb, z0, { col: GUN, ear: 0.10, w: 0.09, foot: 0.15 });+ kit.frame(thF); kit.hull([thF.L / 2, 0, 0]);++ // ---- Shin+ kit.use(kneeG); kit.frame(shF); kit.hull([shF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.30, 0.56, { bolts: 8, gear: 22, gearW: 0.4 });+ kit.drum([0, 0, 0.30], [0, 0, 1], 0.13, 0.07, { bolts: 0, seg: 12, col: DARK });+ kit.mat('armour');+ kit.loft([ring(0.05, 0.25, -0.21, 0.23), ring(0.70, 0.25, -0.19, 0.22), ring(1.25, 0.19, -0.16, 0.19), ring(shF.L + 0.02, 0.13, -0.13, 0.14)], { col: DARK });+ const s1 = sideP(0.275, [[0.24, -0.20], [1.28, -0.17], [1.50, -0.10], [1.50, 0.11], [1.30, 0.19], [0.58, 0.27], [0.26, 0.24]], 0.05);+ kit.loop(s1, 0.045); kit.bolts(s1, 0.03, 1, 0.016);+ const s2 = sideP(0.31, [[0.42, -0.15], [1.05, -0.13], [1.05, 0.15], [0.58, 0.20], [0.42, 0.17]], 0.035, { col: PAINT2 });+ kit.bolts(s2, 0.03); kit.seam(s2, [0.76, -0.13, 0.31], [0.76, 0.17, 0.31]);+ const s2b = sideP(0.31, [[1.10, -0.13], [1.40, -0.09], [1.40, 0.09], [1.10, 0.14]], 0.03, { col: RUST });+ kit.bolts(s2b, 0.025, 1, 0.014);+ sideP(-0.27, [[0.24, -0.19], [1.30, -0.16], [1.50, -0.09], [1.50, 0.10], [1.30, 0.18], [0.58, 0.25], [0.26, 0.22]], 0.04, { col: PAINT2 });+ const sg = kit.plate([[0.34, 0.33, 0.24], [1.38, 0.22, 0.185], [1.38, 0.22, -0.185], [0.34, 0.33, -0.24]], 0.055, { out: [0, 1, 0] });+ kit.seam(sg, [0.70, 0.295, 0.22], [0.70, 0.295, -0.22]); kit.seam(sg, [1.05, 0.26, 0.20], [1.05, 0.26, -0.20]); kit.bolts(sg, 0.03, 2, 0.016);+ const sg2 = kit.plate([[0.74, 0.32, 0.12], [1.02, 0.29, 0.11], [1.02, 0.29, -0.11], [0.74, 0.32, -0.12]], 0.035, { out: [0, 1, 0], col: PAINT2 });+ kit.bolts(sg2, 0.02, 1, 0.012);+ kit.plate([[0.30, -0.235, 0.22], [1.30, -0.185, 0.18], [1.30, -0.185, -0.18], [0.30, -0.235, -0.22]], 0.04, { out: [0, -1, 0], col: GUN });+ // Knee guard and piston horn+ const kg = kit.plate([[-0.46, 0.235, 0.27], [0.32, 0.415, 0.285], [0.32, 0.415, -0.285], [-0.46, 0.235, -0.27]], 0.06, { out: [0, 1, 0], col: PAINT });+ kit.loop(kg, 0.05); kit.bolts(kg, 0.03, 1, 0.018);+ kit.hazardPlate([[-0.34, 0.325, 0.17], [-0.34, 0.325, -0.17], [0.06, 0.42, -0.17], [0.06, 0.42, 0.17]], 0.04, { out: [0, 1, 0], sw: 0.05 });+ sideP(0.255, [[-0.46, 0.02], [-0.46, 0.24], [0.20, 0.40], [0.22, 0.20], [-0.10, 0.02]], 0.03, { col: GUN });+ sideP(-0.255, [[-0.46, 0.02], [-0.46, 0.24], [0.20, 0.40], [0.22, 0.20], [-0.10, 0.02]], 0.03, { col: GUN });+ kit.mat('rubber');+ kit.cables([[0, 0, 0.335], [0.34, 0.03, 0.355], [0.80, -0.055, 0.365], [1.20, 0.01, 0.36], [1.50, -0.10, 0.33], [shF.L, 0, 0.27]], 0.024, [{ r: 0.017, col: CAB_K }, { r: 0.017, col: CAB_B }]);+ kit.mat('armour');+ const hornK = kit.at(shF, -0.396, 0.115, 0), hornKb = kit.at(shF, -0.14, 0.14, 0);+ const calfA = kit.at(shF, 0.50, -0.50, 0), calfAb = kit.at(shF, 0.62, -0.20, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hornK, hornKb, z0, { ear: 0.09, w: 0.11, foot: 0.16, t: 0.04 });+ kit.lug(calfA, calfAb, z0, { col: GUN, ear: 0.10, w: 0.095, foot: 0.16 });+ kit.frame(shF); kit.hull([shF.L / 2, 0, 0]);++ // ---- Metatarsus+ kit.use(ankleG); kit.frame(mtF); kit.hull([mtF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.22, 0.46, { bolts: 6, gear: 18, gearW: 0.4 });+ kit.drum([0, 0, 0.25], [0, 0, 1], 0.10, 0.06, { bolts: 0, seg: 12, col: DARK });+ kit.block([0.56, 0, 0.105], [0.90, 0.17, 0.065], { col: DARK, c: 0.01 });+ kit.block([0.56, 0, -0.105], [0.90, 0.17, 0.065], { col: DARK, c: 0.01 });+ kit.cyl([0.14, 0, 0], [0.98, 0, 0], 0.052, 0.052, { seg: 10, col: STEEL_D });+ kit.mat('armour');+ const m1 = kit.plate([[0.14, 0.17, 0.175], [0.92, 0.13, 0.145], [0.92, 0.13, -0.145], [0.14, 0.17, -0.175]], 0.05, { out: [0, 1, 0] });+ kit.seam(m1, [0.40, 0.155, 0.16], [0.40, 0.155, -0.16]); kit.bolts(m1, 0.028, 1, 0.014);+ const m2 = sideP(0.16, [[0.16, -0.13], [0.88, -0.11], [0.98, -0.05], [0.98, 0.07], [0.88, 0.12], [0.16, 0.15]], 0.03, { col: PAINT2 });+ kit.loop(m2, 0.035, 0.009);+ sideP(-0.16, [[0.16, -0.13], [0.88, -0.11], [0.98, -0.05], [0.98, 0.07], [0.88, 0.12], [0.16, 0.15]], 0.03, { col: PAINT2 });+ kit.hazardPlate([[0.62, 0.155, 0.13], [0.62, 0.155, -0.13], [0.88, 0.145, -0.12], [0.88, 0.145, 0.12]], 0.025, { out: [0, 1, 0], sw: 0.04 });+ kit.plate([[0.16, -0.135, 0.16], [0.92, -0.115, 0.13], [0.92, -0.115, -0.13], [0.16, -0.135, -0.16]], 0.03, { out: [0, -1, 0], col: GUN });+ sideP(0.235, [[-0.46, -0.16], [-0.46, -0.04], [0.14, 0.17], [0.14, -0.17]], 0.03, { col: GUN });+ sideP(-0.235, [[-0.46, -0.16], [-0.46, -0.04], [0.14, 0.17], [0.14, -0.17]], 0.03, { col: GUN });+ kit.mat('rubber');+ kit.tube([[0, 0, 0.28], [0.25, -0.02, 0.205], [0.62, 0.04, 0.20], [0.95, 0, 0.215], [mtF.L, 0, 0.24]], 0.016, { col: CAB_Y });+ kit.tube([[0, 0, 0.28], [0.30, 0.05, 0.205], [0.66, -0.05, 0.20], [0.92, 0.03, 0.215], [mtF.L, 0, 0.24]], 0.014, { col: CAB_K });+ kit.mat('armour');+ const hornA = kit.at(mtF, -0.38, -0.10, 0), hornAb = kit.at(mtF, -0.10, -0.08, 0);+ const toeUp = kit.at(mtF, 0.62, 0.20, 0), toeUpb = kit.at(mtF, 0.70, 0.13, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hornA, hornAb, z0, { ear: 0.09, w: 0.11, foot: 0.14, t: 0.04 });+ kit.lug(toeUp, toeUpb, z0, { col: GUN, ear: 0.06, w: 0.055, foot: 0.08, t: 0.02 });++ // ---- Foot (sole at y = 0, centre toe tip at SK.tipX)+ kit.use(toeG); kit.frame(null); kit.mat('armour');+ const toePiece = (deg, len, wid, x0, dz, col, hBase) => {+ const c = Math.cos(deg * D2R), sn = Math.sin(deg * D2R);+ const P = (x, y, z) => [x0 + x * c + z * sn, y, z0 + dz - x * sn + z * c];+ const w = wid / 2;+ const outline = [[-0.05, w], [len * 0.62, w], [len * 0.88, w * 0.62], [len, 0], [len * 0.88, -w * 0.62], [len * 0.62, -w], [-0.05, -w]];+ const hgt = (x) => hBase - (hBase - 0.09) * Math.max(0, x / len);+ const ring0 = outline.map(([x, z]) => P(x, 0, z));+ const ring1 = outline.map(([x, z]) => P(x, 0.06, z));+ const ring2 = outline.map(([x, z]) => P(Math.min(x, len * 0.96) * 0.985, hgt(x), z * 0.80));+ kit.loft([ring0, ring1], { col: RUB, mat: 'rubber', capEnd: false });+ kit.loft([ring1, ring2], { col: DARK, capStart: false });+ const top = ring2.map((p) => [p[0], p[1] + 0.012, p[2]]);+ const h = kit.plate(top, 0.03, { out: [0, 1, 0], col });+ kit.bolt(P(len * 0.22, hgt(len * 0.22) + 0.012, w * 0.55), [0, 1, 0], 0.016);+ kit.bolt(P(len * 0.22, hgt(len * 0.22) + 0.012, -w * 0.55), [0, 1, 0], 0.016);+ return { h, P, hgt };+ };+ const ct = toePiece(0, SK.tipX, 0.42, 0, 0, PAINT, 0.30);+ kit.seam(ct.h, ct.P(0.45, ct.hgt(0.45) + 0.012, 0.15), ct.P(0.45, ct.hgt(0.45) + 0.012, -0.15));+ const hzA = ct.P(0.52, ct.hgt(0.52) + 0.018, 0.14), hzB = ct.P(0.52, ct.hgt(0.52) + 0.018, -0.14), hzC = ct.P(0.78, ct.hgt(0.78) + 0.018, -0.10), hzD = ct.P(0.78, ct.hgt(0.78) + 0.018, 0.10);+ kit.hazardPlate([hzA, hzB, hzC, hzD], 0.012, { out: [0, 1, 0], sw: 0.04, c: 0.008 });+ toePiece(24, 0.78, 0.27, 0.04, -0.14, PAINT2, 0.24);+ toePiece(-24, 0.78, 0.27, 0.04, 0.14, PAINT2, 0.24);+ toePiece(180, 0.62, 0.26, -0.02, 0, GUN, 0.22);+ kit.mat('steel');+ for (const [dz, len] of [[0, SK.tipX], [-0.14, 0.78], [0.14, 0.78]]) {+ const deg = dz === 0 ? 0 : dz < 0 ? 24 : -24, c = Math.cos(deg * D2R), sn = Math.sin(deg * D2R), x0 = dz === 0 ? 0 : 0.04;+ const P = (x, y, z) => [x0 + x * c + z * sn, y, z0 + dz - x * sn + z * c];+ const l0 = len * 0.80;+ kit.poly([P(l0, 0.125, 0.09), P(l0, 0.125, -0.09), P(len, 0.02, 0)], STEEL, { out: [0.4, 1, 0] });+ }+ kit.drum([0, SK.toeH, z0], [0, 0, 1], 0.17, 0.46, { bolts: 6, seg: 16 });+ kit.mat('armour');+ kit.block([0.04, 0.17, z0], [0.46, 0.27, 0.42], { col: GUN, c: 0.03 });+ const ftTop = kit.plate([[-0.14, 0.325, z0 + 0.20], [0.27, 0.27, z0 + 0.20], [0.27, 0.27, z0 - 0.20], [-0.14, 0.325, z0 - 0.20]], 0.03, { out: [0, 1, 0], col: PAINT2 });+ kit.bolts(ftTop, 0.025, 1, 0.014);+ const toeB = [0.38, 0.30, 0], toeBb = [0.42, 0.20, 0];+ kit.lug([toeB[0], toeB[1], 0], [toeBb[0], toeBb[1], 0], z0, { col: GUN, ear: 0.055, w: 0.055, foot: 0.08, t: 0.02 });++ // ---- Leg hydraulics: each spans one joint and is aim-constrained at both ends.+ const pelvisAnchor = [-0.72, 3.50, z0];+ kit.piston('Hip_' + side, pelvisG, pelvisAnchor, hipG, hipLugT, { r: 0.085, rod: 0.05 });+ kit.piston('Knee_' + side, hipG, kneeA, kneeG, hornK, { r: 0.098, rod: 0.058, hose: CAB_R });+ kit.piston('Ankle_' + side, kneeG, calfA, ankleG, hornA, { r: 0.098, rod: 0.058, hose: CAB_R });+ kit.piston('Toe_' + side, ankleG, toeUp, toeG, [toeB[0], toeB[1], z0], { r: 0.052, rod: 0.03, seg: 10 });+ kit.mirror(1);+ return { hipG, kneeG, ankleG, toeG };+}++// ---------------------------------------------------------------- pelvis+function buildPelvis(kit, pelvisG) {+ const z0 = SK.legZ, y0 = SK.hip[1];+ kit.use(pelvisG); kit.frame(null); kit.mirror(1); kit.hull([0, y0, 0]); kit.mat('armour');+ const oct = (y, xf, xr, hz, cut) => [[xr + cut, y, hz], [xf - cut, y, hz], [xf, y, hz - cut], [xf, y, -hz + cut], [xf - cut, y, -hz], [xr + cut, y, -hz], [xr, y, -hz + cut], [xr, y, hz - cut]];+ kit.loft([oct(3.00, 0.34, -0.34, 0.34, 0.08), oct(3.18, 0.56, -0.58, 0.50, 0.12), oct(3.62, 0.60, -0.66, 0.52, 0.12), oct(3.80, 0.50, -0.54, 0.50, 0.10)], { col: DARK });+ const f0 = kit.plate([[0.635, 3.66, 0.27], [0.635, 3.66, -0.27], [0.60, 3.20, -0.24], [0.60, 3.20, 0.24]], 0.06, { col: BONE });+ kit.loop(f0, 0.05); kit.bolts(f0, 0.03, 1, 0.016);+ kit.stencil([0.612, 3.34, 0.13], [0, 0, -1], [0.076, 0.997, 0], 'H-7', 0.13, RUST, [0.997, -0.076, 0]);+ kit.hazardPlate([[0.60, 3.19, 0.24], [0.60, 3.19, -0.24], [0.38, 2.99, -0.16], [0.38, 2.99, 0.16]], 0.04, { out: [1, -0.8, 0], sw: 0.05 });+ const rr0 = kit.plate([[-0.70, 3.66, 0.30], [-0.70, 3.66, -0.30], [-0.62, 3.20, -0.26], [-0.62, 3.20, 0.26]], 0.05, { col: PAINT2 });+ kit.vent([-0.692, 3.58, 0.20], [-0.692, 3.58, -0.20], [-0.642, 3.30, -0.17], [-0.642, 3.30, 0.17], { n: 6 });+ kit.seam(rr0, [-0.63, 3.26, 0.24], [-0.63, 3.26, -0.24]);+ kit.plate([[-0.62, 3.19, 0.26], [-0.62, 3.19, -0.26], [-0.38, 2.99, -0.18], [-0.38, 2.99, 0.18]], 0.04, { col: GUN, out: [-1, -0.8, 0] });+ for (const s of [1, -1]) {+ kit.mirror(s); kit.mat('steel');+ kit.drum([0, y0, 0.53], [0, 0, 1], 0.46, 0.13, { gear: 32, bolts: 12, gearW: 0.7, seg: 26 });+ kit.mat('armour');+ const a = kit.plate([[0.635, 3.68, 0.28], [0.635, 3.68, 0.525], [0.60, 3.20, 0.505], [0.60, 3.20, 0.25]], 0.05);+ kit.bolts(a, 0.03, 1, 0.016);+ kit.plate([[0.60, 3.19, 0.25], [0.60, 3.19, 0.505], [0.36, 3.02, 0.345], [0.38, 2.99, 0.17]], 0.04, { col: PAINT2, out: [1, -0.8, 0.3] });+ kit.plate([[-0.70, 3.68, 0.31], [-0.70, 3.68, 0.53], [-0.62, 3.20, 0.51], [-0.62, 3.20, 0.27]], 0.05, { col: PAINT2 });+ kit.plate([[-0.62, 3.19, 0.27], [-0.62, 3.19, 0.51], [-0.36, 3.02, 0.345], [-0.38, 2.99, 0.19]], 0.04, { col: GUN, out: [-1, -0.8, 0.3] });+ const t = kit.plate([[0.50, 3.84, 0.53], [-0.56, 3.84, 0.53], [-0.60, 3.68, 0.555], [0.56, 3.68, 0.555]], 0.04, { col: PAINT, out: [0, 0.7, 1] });+ kit.seam(t, [0.2, 3.76, 0.545], [-0.2, 3.76, 0.545]);+ // Outrigger carrying the hip piston's upper pin+ kit.mat('steel');+ kit.block([-0.70, 3.50, 0.63], [0.13, 0.15, 0.60], { col: GUN, c: 0.02 });+ kit.mat('rubber');+ kit.tube([[-0.58, 3.62, 0.30], [-0.70, 3.62, 0.42], [-0.76, 3.54, 0.62], [-0.74, 3.42, 0.78], [-0.46, 3.36, 0.72], [-0.1, 3.42, 0.60], [0, y0, 0.60]], 0.017, { col: CAB_K, sp: 3 });+ kit.mat('armour');+ kit.lug([-0.72, 3.50, 0], [-0.70, 3.50, 0], z0, { col: GUN, ear: 0.085, foot: 0.085, w: 0.085 });+ }+ kit.mirror(1); kit.mat('steel');+ kit.lathe([0, 3.76, 0], [0, 1, 0], [[0, 0.46, GUN], [0.02, 0.60, STEEL_D], [0.09, 0.60, GUN], [0.11, 0.52, STEEL_D], [0.15, 0.50, STEEL_D], [0.15, 0, STEEL_D]], { seg: 30 });+ const NT = 40;+ for (let i = 0; i < NT; i++) {+ const th = (i / NT) * 2 * Math.PI, rad = [Math.cos(th), 0, Math.sin(th)], tan = [-Math.sin(th), 0, Math.cos(th)];+ kit.block(vAdd([0, 3.825, 0], vMul(rad, 0.612)), [0.03, 0.05, 0.05], { ax: [rad, [0, 1, 0], tan], c: 0, col: STEEL_D });+ }+ kit.mat('armour');+}++// ---------------------------------------------------------------- torso, cockpit, backpack+function buildTorso(kit, waistG) {+ kit.use(waistG); kit.frame(null); kit.mirror(1); kit.hull([0, 4.6, 0]); kit.mat('armour');+ const tRing = (y, f, n, r, h) => {+ const c = Math.max(0.56, h * 0.62);+ return [[r + 0.18, y, h], [f - 0.30, y, h], [f, y, c], [f, y, 0.52], [n, y, 0.50], [n, y, -0.50], [f, y, -0.52], [f, y, -c], [f - 0.30, y, -h], [r + 0.18, y, -h], [r, y, -h * 0.55], [r, y, h * 0.55]];+ };+ kit.loft([tRing(3.96, 0.50, 0.50, -0.50, 0.60), tRing(4.25, 0.72, 0.72, -0.70, 0.88), tRing(4.56, 0.86, 0.86, -0.84, 1.10), tRing(4.60, 0.87, 0.40, -0.85, 1.12), tRing(5.10, 0.84, 0.40, -0.92, 1.18), tRing(5.40, 0.62, 0.40, -0.88, 1.02), tRing(5.44, 0.60, 0.60, -0.88, 1.00), tRing(5.62, 0.25, 0.25, -0.70, 0.72)], { col: DARK });+ kit.mat('steel');+ kit.lathe([0, 3.90, 0], [0, 1, 0], [[0, 0, STEEL_D], [0, 0.50, STEEL_D], [0.04, 0.50, GUN], [0.08, 0.58, GUN]], { seg: 28 });+ for (const s of [1, -1]) { kit.mirror(s); kit.drum([0.34, 3.97, 0.36], [0, 1, 0], 0.09, 0.14, { bolts: 0, seg: 12 }); }+ kit.mirror(1); kit.mat('armour');++ // Abdomen bands+ const ab1 = kit.plate([[0.76, 4.27, 0.56], [0.76, 4.27, -0.56], [0.63, 4.13, -0.50], [0.63, 4.13, 0.50]], 0.05, { col: PAINT2 });+ kit.bolts(ab1, 0.026, 2, 0.014);+ const ab2 = kit.plate([[0.67, 4.15, 0.48], [0.67, 4.15, -0.48], [0.55, 4.02, -0.42], [0.55, 4.02, 0.42]], 0.05, { col: GUN });+ kit.loop(ab2, 0.03, 0.009);+ kit.hazardPlate([[0.56, 4.035, 0.40], [0.56, 4.035, -0.40], [0.50, 3.94, -0.36], [0.50, 3.94, 0.36]], 0.04, { sw: 0.05 });+ const rb1 = kit.plate([[-0.72, 4.26, 0.56], [-0.72, 4.26, -0.56], [-0.52, 3.98, -0.40], [-0.52, 3.98, 0.40]], 0.05, { col: GUN });+ kit.bolts(rb1, 0.03, 1, 0.014);++ for (const s of [1, -1]) {+ kit.mirror(s);+ // Cheek intake block+ const c0 = kit.plate([[0.915, 4.62, 0.53], [0.915, 4.62, 0.70], [0.885, 5.08, 0.74], [0.885, 5.08, 0.53]], 0.05, { col: BONE });+ kit.bolts(c0, 0.026, 1, 0.014);+ const ob = kit.plate([[0.915, 4.60, 0.71], [0.60, 4.60, 1.15], [0.58, 5.09, 1.215], [0.89, 5.09, 0.75]], 0.06, { col: PAINT });+ kit.vent([0.885, 4.66, 0.745], [0.635, 4.66, 1.095], [0.625, 4.92, 1.135], [0.875, 4.92, 0.77], { n: 6, col: GUN });+ kit.seam(ob, [0.885, 4.97, 0.76], [0.62, 4.97, 1.15]); kit.bolts(ob, 0.03, 1, 0.016);+ kit.mat('glowAmber');+ kit.lathe([0.916, 4.75, 0.615], [1, 0, 0], [[0, 0.03], [0.012, 0.022], [0.012, 0]], { seg: 8, mat: 'glowAmber' });+ kit.mat('armour');+ const br = kit.plate([[0.90, 5.11, 0.53], [0.90, 5.11, 0.77], [0.66, 5.41, 0.68], [0.66, 5.41, 0.53]], 0.055, { col: PAINT2 });+ kit.bolts(br, 0.028, 1, 0.014);+ const br2 = kit.plate([[0.60, 5.12, 1.22], [0.89, 5.12, 0.79], [0.66, 5.41, 0.70], [0.42, 5.41, 1.03]], 0.05, { col: PAINT });+ kit.loop(br2, 0.04);+ kit.plate([[0.885, 4.575, 0.50], [0.885, 4.575, 0.71], [0.745, 4.255, 0.565], [0.745, 4.255, 0.50]], 0.05, { col: GUN });+ const fl = kit.plate([[0.885, 4.575, 0.72], [0.585, 4.575, 1.125], [0.43, 4.255, 0.90], [0.745, 4.255, 0.575]], 0.055, { col: PAINT });+ kit.seam(fl, [0.81, 4.415, 0.635], [0.51, 4.415, 1.01]); kit.bolts(fl, 0.03, 1, 0.016);+ const sl = kit.plate([[0.42, 4.26, 0.915], [-0.42, 4.26, 0.915], [-0.54, 4.56, 1.135], [0.56, 4.56, 1.135]], 0.05, { col: PAINT2 });+ kit.seam(sl, [0.1, 4.28, 0.93], [0.14, 4.54, 1.125]); kit.seam(sl, [-0.2, 4.28, 0.93], [-0.24, 4.54, 1.125]);+ kit.hazardPlate([[0.40, 4.225, 0.875], [-0.40, 4.225, 0.875], [-0.42, 4.14, 0.80], [0.42, 4.14, 0.80]], 0.03, { sw: 0.06, out: [0, -0.5, 1] });+ kit.plate([[0.52, 4.60, 1.155], [0.52, 5.09, 1.215], [0.30, 5.09, 1.215], [0.30, 4.60, 1.155]], 0.04, { col: PAINT });+ const srr = kit.plate([[-0.26, 4.60, 1.155], [-0.26, 5.09, 1.215], [-0.70, 5.09, 1.215], [-0.70, 4.60, 1.155]], 0.05, { col: PAINT });+ kit.loop(srr, 0.04); kit.bolts(srr, 0.025, 2, 0.014);+ const rf = kit.plate([[0.38, 5.46, 1.015], [-0.66, 5.46, 1.015], [-0.53, 5.64, 0.735], [0.10, 5.64, 0.735]], 0.05, { col: PAINT });+ kit.bolts(rf, 0.03, 2, 0.014); kit.seam(rf, [-0.12, 5.47, 1.0], [-0.2, 5.63, 0.74]);+ // Shoulder yoke+ kit.mat('steel');+ kit.drum([0.02, 5.00, 1.255], [0, 0, 1], 0.40, 0.17, { gear: 30, bolts: 10, gearW: 0.6, seg: 24 });+ kit.block([0.02, 4.70, 1.20], [0.34, 0.34, 0.13], { col: GUN, c: 0.03 });+ kit.mat('rubber');+ kit.tube([[-0.74, 5.12, 1.05], [-0.68, 5.18, 1.26], [-0.34, 5.27, 1.37], [-0.05, 5.1, 1.37], [0.02, 5.0, 1.36]], 0.02, { col: CAB_K });+ kit.tube([[-0.74, 5.05, 1.05], [-0.70, 5.08, 1.27], [-0.40, 5.15, 1.37], [-0.08, 5.05, 1.37], [0.02, 5.0, 1.36]], 0.016, { col: CAB_R });+ kit.mat('armour');+ }+ kit.mirror(1);+ // Brow, roof, hatch+ const fh = kit.plate([[0.625, 5.46, 0.60], [0.625, 5.46, -0.60], [0.27, 5.645, -0.43], [0.27, 5.645, 0.43]], 0.055, { col: PAINT2 });+ kit.bolts(fh, 0.03, 2, 0.014);+ const rft = kit.plate([[0.26, 5.645, 0.70], [0.26, 5.645, -0.70], [-0.52, 5.645, -0.70], [-0.52, 5.645, 0.70]], 0.04, { col: PAINT2 });+ kit.loop(rft, 0.06);+ kit.mat('steel');+ kit.lathe([-0.14, 5.645, 0.12], [0, 1, 0], [[0, 0.24, GUN], [0.035, 0.24, GUN], [0.05, 0.21, STEEL_D], [0.05, 0, STEEL_D]], { seg: 16 });+ kit.block([-0.14, 5.71, 0.12], [0.30, 0.03, 0.04], { col: STEEL });+ // Sensor pod and whip antenna+ kit.block([0.05, 5.70, -0.34], [0.26, 0.11, 0.28], { col: GUN, c: 0.02 });+ kit.cyl([-0.42, 5.65, -0.52], [-0.42, 5.98, -0.52], 0.014, 0.006, { seg: 6, col: RUB });+ kit.cyl([-0.42, 5.65, -0.40], [-0.42, 5.84, -0.40], 0.012, 0.005, { seg: 6, col: RUB });+ kit.lathe([0.18, 5.70, -0.34], [1, 0, 0], [[0, 0.04, GUN], [0.02, 0.04, GUN], [0.02, 0.028, GUN]], { seg: 10 });+ kit.mat('glowCyan');+ kit.lathe([0.20, 5.70, -0.34], [1, 0, 0], [[0, 0.028], [0.006, 0.02], [0.006, 0]], { seg: 10, mat: 'glowCyan' });+ kit.mat('armour');++ // ---- Chin tub+ kit.hull([1.0, 4.40, 0]);+ const chinRing = (x, yt, yb, z, cut) => [[x, yt, z], [x, yt, -z], [x, yb + cut, -z], [x, yb, -z + cut], [x, yb, z - cut], [x, yb + cut, z]];+ kit.loft([chinRing(0.66, 4.60, 4.10, 0.50, 0.10), chinRing(1.12, 4.60, 4.33, 0.47, 0.08), chinRing(1.43, 4.62, 4.49, 0.25, 0.05)], { col: DARK });+ const ch1 = kit.plate([[1.15, 4.34, 0.45], [1.15, 4.34, -0.45], [0.70, 4.09, -0.47], [0.70, 4.09, 0.47]], 0.05, { col: PAINT });+ kit.bolts(ch1, 0.03, 2, 0.014); kit.seam(ch1, [0.92, 4.21, 0.44], [0.92, 4.21, -0.44]);+ const ch2 = kit.plate([[1.45, 4.49, 0.235], [1.45, 4.49, -0.235], [1.17, 4.33, -0.40], [1.17, 4.33, 0.40]], 0.05, { col: PAINT2 });+ kit.bolts(ch2, 0.025, 1, 0.012);+ kit.hazardPlate([[1.455, 4.63, 0.235], [1.455, 4.63, -0.235], [1.455, 4.50, -0.235], [1.455, 4.50, 0.235]], 0.03, { sw: 0.05, out: [1, 0, 0] });+ for (const s of [1, -1]) {+ kit.mirror(s);+ const cs = kit.plate([[1.13, 4.61, 0.485], [0.68, 4.61, 0.515], [0.68, 4.15, 0.515], [1.13, 4.37, 0.485]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.seam(cs, [0.90, 4.60, 0.50], [0.90, 4.24, 0.50]); kit.bolts(cs, 0.026, 1, 0.014);+ kit.plate([[1.44, 4.62, 0.26], [1.15, 4.61, 0.47], [1.15, 4.37, 0.47], [1.44, 4.51, 0.26]], 0.04, { col: GUN, out: [0.5, 0, 1] });+ // Lamps+ kit.mat('steel');+ kit.lathe([1.22, 4.42, 0.335], [1, 0.1, 0.25], [[0, 0, GUN], [0, 0.05, GUN], [0.05, 0.05, GUN], [0.05, 0.036, GUN]], { seg: 10 });+ kit.mat('glowCyan');+ kit.lathe(vAdd([1.22, 4.42, 0.335], vMul(vUnit([1, 0.1, 0.25]), 0.04)), [1, 0.1, 0.25], [[0, 0.036], [0.006, 0]], { seg: 10, mat: 'glowCyan' });+ kit.mat('armour');+ }+ kit.mirror(1);++ // ---- Cockpit interior+ kit.hull(null); kit.mat('armour');+ kit.poly([[0.40, 4.606, 0.49], [1.14, 4.606, 0.46], [1.39, 4.606, 0.24], [1.39, 4.606, -0.24], [1.14, 4.606, -0.46], [0.40, 4.606, -0.49]], lin(0x2a2c2e), { out: [0, 1, 0] });+ kit.mat('steel');+ kit.block([0.46, 4.97, 0], [0.09, 0.66, 0.44], { col: GUN, c: 0.02, edge: tone(GUN, 1.25) });+ kit.block([0.47, 5.27, 0], [0.08, 0.13, 0.20], { col: GUN, c: 0.015, edge: tone(GUN, 1.25) });+ kit.block([0.70, 4.70, 0], [0.42, 0.10, 0.44], { col: GUN, c: 0.02, edge: tone(GUN, 1.25) });+ kit.mat('rubber');+ // Seated pilot: rounded limbs in a flight suit, dark visor+ kit.lathe([0.62, 4.74, 0], [0, 1, 0], [[0, 0, SUIT], [0, 0.11, SUIT], [0.10, 0.135, SUIT], [0.27, 0.16, SUIT], [0.35, 0.12, SUIT], [0.37, 0.05, RUB], [0.43, 0.04, RUB], [0.43, 0, RUB]], { seg: 10, mat: 'rubber' });+ for (const sz of [1, -1]) {+ kit.tube([[0.64, 5.04, sz * 0.15], [0.70, 4.92, sz * 0.21], [0.84, 4.88, sz * 0.19], [0.95, 4.85, sz * 0.13]], 0.046, { col: SUIT, seg: 7, sp: 2 });+ kit.tube([[0.66, 4.78, sz * 0.09], [0.84, 4.82, sz * 0.10], [0.99, 4.80, sz * 0.10], [1.05, 4.64, sz * 0.10]], 0.066, { col: tone(SUIT, 0.85), seg: 7, sp: 2 });+ }+ kit.lathe([0.64, 5.12, 0], [0, 1, 0], [[0, 0, BONE], [0, 0.07, BONE], [0.05, 0.115, BONE], [0.14, 0.12, BONE], [0.20, 0.075, BONE], [0.22, 0, BONE]], { seg: 12, mat: 'rubber' });+ kit.poly([[0.76, 5.26, 0.085], [0.76, 5.26, -0.085], [0.76, 5.18, -0.085], [0.76, 5.18, 0.085]], tone(RUB, 0.5), { out: [1, 0, 0], mat: 'rubber' });+ kit.mat('steel');+ kit.block([1.08, 4.76, 0], [0.32, 0.30, 0.62], { col: GUN, c: 0.02, ax: [vUnit([1, 0.45, 0]), vUnit([-0.45, 1, 0]), [0, 0, 1]] });+ kit.mat('glowCyan');+ const scr = (z0, w) => kit.poly([[0.985, 4.93, z0 + w], [0.985, 4.93, z0 - w], [0.915, 4.80, z0 - w], [0.915, 4.80, z0 + w]].map((p) => [p[0] + 0.012, p[1] + 0.012, p[2]]), WHITE, { out: [-1, 0.5, 0], mat: 'glowCyan' });+ scr(0, 0.10); kit.mat('glowAmber');+ kit.poly([[1.00, 4.93, 0.27], [1.00, 4.93, 0.15], [0.93, 4.80, 0.15], [0.93, 4.80, 0.27]], WHITE, { out: [-1, 0.5, 0], mat: 'glowAmber' });+ kit.mat('glowCyan');+ kit.poly([[1.00, 4.93, -0.15], [1.00, 4.93, -0.27], [0.93, 4.80, -0.27], [0.93, 4.80, -0.15]], WHITE, { out: [-1, 0.5, 0], mat: 'glowCyan' });+ kit.mat('armour');++ // ---- Glass canopy and frame+ const A0b = [0.80, 4.60, 0.50], A0s = [0.80, 5.18, 0.50], A0t = [0.80, 5.42, 0.26];+ const A1b = [1.14, 4.60, 0.47], A1s = [1.14, 5.04, 0.44], A1t = [1.14, 5.20, 0.20];+ const A2b = [1.40, 4.62, 0.25], A2t = [1.40, 4.84, 0.12];+ const mz = (p) => [p[0], p[1], -p[2]];+ const cc = [1.0, 4.9, 0];+ const pane = (pts) => kit.poly(pts, WHITE, { out: vSub(centroid(pts), cc), mat: 'glass' });+ pane([A0t, A1t, mz(A1t), mz(A0t)]);+ pane([A1t, A2t, mz(A2t), mz(A1t)]);+ pane([A2t, A2b, mz(A2b), mz(A2t)]);+ for (const f of [(p) => p, mz]) {+ pane([f(A0s), f(A1s), f(A1t), f(A0t)]);+ pane([f(A0b), f(A1b), f(A1s), f(A0s)]);+ pane([f(A1b), f(A2b), f(A2t), f(A1s)]);+ pane([f(A1s), f(A2t), f(A1t)]);+ }+ kit.mat('steel');+ const bar = (a, b, r = 0.026) => kit.cyl(a, b, r, r, { seg: 6, col: GUN });+ for (const f of [(p) => p, mz]) {+ bar(f(A0b), f(A0s), 0.038); bar(f(A0s), f(A0t), 0.038);+ bar(f(A1b), f(A1s)); bar(f(A1s), f(A1t));+ bar(f(A0s), f(A1s), 0.016); bar(f(A0t), f(A1t)); bar(f(A1t), f(A2t)); bar(f(A1s), f(A2t), 0.016); bar(f(A1b), f(A2b)); bar(f(A0b), f(A1b)); bar(f(A2b), f(A2t));+ }+ bar(A0t, mz(A0t), 0.03); bar(A1t, mz(A1t)); bar(A2t, mz(A2t)); bar(A2b, mz(A2b));+ kit.mat('armour');++ // ---- Backpack: reactor block, radiators, stacks+ kit.hull([-1.1, 4.8, 0]);+ const bRing = (x, y0, y1, z) => [[x, y1, z], [x, y1, -z], [x, y0, -z], [x, y0, z]];+ kit.loft([bRing(-0.78, 4.24, 5.46, 0.86), bRing(-1.22, 4.32, 5.40, 0.78), bRing(-1.46, 4.48, 5.22, 0.60)], { col: DARK });+ const bt = kit.plate([[-0.80, 5.49, 0.82], [-0.80, 5.49, -0.82], [-1.25, 5.43, -0.76], [-1.25, 5.43, 0.76]], 0.05, { col: PAINT });+ kit.loop(bt, 0.05); kit.bolts(bt, 0.026, 2, 0.014);+ const br0 = kit.hazardPlate([[-1.26, 5.43, 0.74], [-1.26, 5.43, -0.74], [-1.46, 5.25, -0.58], [-1.46, 5.25, 0.58]], 0.03, { sw: 0.08, out: [-1, 1, 0] });+ kit.bolts(br0, 0.018, 1, 0.012);+ const rearP = kit.plate([[-1.49, 5.20, 0.56], [-1.49, 5.20, -0.56], [-1.49, 4.50, -0.56], [-1.49, 4.50, 0.56]], 0.03, { col: PAINT2, out: [-1, 0, 0] });+ kit.bolts(rearP, 0.03, 2, 0.016);+ for (const z of [0.28, -0.28]) {+ kit.vent([-1.50, 4.60, z - 0.20], [-1.50, 4.60, z + 0.20], [-1.50, 5.08, z + 0.20], [-1.50, 5.08, z - 0.20], { n: 7, col: GUN, base: WHITE, glow: 'glowAmber', depth: 0.02 });+ }+ kit.stencil([-1.505, 4.52, 0.08], [0, 0, -1], [0, 1, 0], 'HE', 0.06, HAZ_Y, [-1, 0, 0]);+ for (const s of [1, -1]) {+ kit.mirror(s);+ const sd = kit.plate([[-0.82, 5.36, 0.875], [-1.20, 5.32, 0.795], [-1.20, 5.02, 0.795], [-0.82, 5.02, 0.875]], 0.04, { col: PAINT2, out: [0, 0, 1] });+ kit.bolts(sd, 0.024, 1, 0.012);+ kit.mat('steel');+ // Radiator bank: fins over a dark core, exposed on the flank+ kit.block([-1.02, 4.66, 0.82], [0.44, 0.60, 0.04], { col: tone(GUN, 0.4), c: 0 });+ for (let i = 0; i < 11; i++) kit.block([-0.82 - i * 0.04, 4.66, 0.86], [0.012, 0.56, 0.05], { col: STEEL_D, c: 0 });+ // Stack+ kit.lathe([-1.32, 5.30, 0.44], [-0.12, 1, 0], [[0, 0.11, GUN], [0.40, 0.095, GUN], [0.40, 0.12, STEEL_D], [0.50, 0.12, STEEL_D], [0.50, 0.08, tone(RUB, 0.6)], [0.42, 0.075, tone(RUB, 0.6)], [0.42, 0, tone(RUB, 0.6)]], { seg: 12 });+ // Coolant lines to the waist axis+ kit.mat('rubber');+ kit.tube([[-0.92, 4.26, 0.46], [-0.86, 4.04, 0.42], [-0.60, 3.96, 0.30], [-0.26, 3.95, 0.14], [0, 3.94, 0.02]], 0.024, { col: CAB_K });+ kit.tube([[-0.98, 4.26, 0.34], [-0.90, 4.03, 0.30], [-0.62, 3.95, 0.20], [-0.26, 3.95, 0.09], [0, 3.94, 0.01]], 0.018, { col: CAB_B });+ kit.mat('armour');+ }+ kit.mirror(1);++ // ---- Right shoulder sensor / counterweight (torso mounted)+ kit.hull(null);+ kit.block([-0.08, 5.51, 0.92], [0.50, 0.18, 0.38], { col: PAINT2, c: 0.03 });+ kit.mat('steel');+ for (const dz of [-0.09, 0.09]) {+ kit.lathe([0.17, 5.53, 0.92 + dz], [1, 0, 0], [[-0.02, 0, GUN], [-0.02, 0.06, GUN], [0.07, 0.066, GUN], [0.07, 0.042, tone(GUN, 0.4)], [0.05, 0.042, tone(GUN, 0.4)]], { seg: 12 });+ kit.mat('glowCyan');+ kit.lathe([0.22, 5.53, 0.92 + dz], [1, 0, 0], [[0, 0.042], [0.006, 0], [0.006, 0]], { seg: 12, mat: 'glowCyan' });+ kit.mat('steel');+ }+ kit.mat('armour');+ // Paint on the rear hull facets and backpack flanks+ kit.hull([-1.0, 4.8, 0]);+ for (const s of [1, -1]) {+ kit.mirror(s);+ const rh = kit.plate([[-0.66, 4.60, 1.125], [-0.82, 4.60, 0.90], [-0.87, 5.09, 0.90], [-0.71, 5.09, 1.19]], 0.05, { col: PAINT2, out: [-1, 0, 1] });+ kit.bolts(rh, 0.026, 1, 0.014);+ const bf = kit.plate([[-1.24, 5.38, 0.80], [-1.24, 4.34, 0.80], [-1.48, 4.50, 0.615], [-1.48, 5.20, 0.615]], 0.04, { col: PAINT, out: [-0.6, 0, 1] });+ kit.loop(bf, 0.04); kit.bolts(bf, 0.022, 1, 0.012);+ kit.plate([[-0.80, 4.21, 0.84], [-1.22, 4.29, 0.76], [-1.22, 4.29, 0.04], [-0.80, 4.21, 0.04]], 0.03, { col: PAINT2, out: [0, -1, 0] });+ }+ kit.mirror(1);+ const rs = kit.plate([[-0.52, 5.647, 0.70], [-0.52, 5.647, -0.70], [-0.78, 5.50, -0.84], [-0.78, 5.50, 0.84]], 0.05, { col: PAINT, out: [-0.5, 1, 0] });+ kit.bolts(rs, 0.03, 2, 0.014); kit.seam(rs, [-0.65, 5.575, 0.76], [-0.65, 5.575, -0.76]);+ kit.hull(null);+ // Pod pylon on the left shoulder line+ kit.mat('steel');+ kit.beam([-0.05, 5.44, -0.80], [-0.05, 5.50, -1.34], 0.13, 0.20, { col: GUN, c: 0.02, up: [0, 1, 0] });+ kit.mat('armour');+}++// ---------------------------------------------------------------- arms+function buildUpperArm(kit, side, waistG) {+ const s = side === 'L' ? -1 : 1, z0 = SK.armZ;+ kit.mirror(s);+ const G = kit.group('Shoulder_' + side, waistG, [SK.shoulder[0], SK.shoulder[1], s * z0], 'UpperArm_' + side);+ G.node.userData = { hinge: 'z' };+ const F = kit.bone(SK.shoulder, SK.elbow, z0);+ const ring = (u, vf, vr, wh) => [[u, vf, wh], [u, vf, -wh], [u, vr, -wh], [u, vr, wh]];+ kit.use(G); kit.frame(F); kit.hull([0.2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, -0.02], [0, 0, 1], 0.33, 0.50, { bolts: 8, seg: 22 });+ kit.mat('armour');+ kit.loft([ring(0.05, 0.25, -0.26, 0.25), ring(0.55, 0.23, -0.24, 0.23), ring(F.L, 0.17, -0.17, 0.17)], { col: DARK });+ const sideP = (w, pts, t, o = {}) => kit.plate(pts.map((p) => [p[0], p[1], w]), t, { out: [0, 0, Math.sign(w)], ...o });+ // Pauldron stack+ const pOuter = sideP(0.335, [[-0.40, -0.34], [0.32, -0.36], [0.52, -0.22], [0.52, 0.20], [0.32, 0.36], [-0.40, 0.38], [-0.48, 0.02]], 0.06, { col: PAINT });+ kit.loop(pOuter, 0.05); kit.bolts(pOuter, 0.03, 1, 0.02);+ const pIn = sideP(0.375, [[-0.26, -0.22], [0.26, -0.24], [0.36, -0.12], [0.36, 0.14], [0.26, 0.25], [-0.26, 0.25]], 0.04, { col: BONE });+ kit.bolts(pIn, 0.026, 1, 0.014);+ kit.stencil([0.13, -0.14, 0.375], [0, 1, 0], [-1, 0, 0], side === 'R' ? '07' : '12', 0.21, RUST, [0, 0, 1]);+ kit.hazardPlate([[0.40, -0.21, 0.375], [0.40, 0.20, 0.375], [0.50, 0.15, 0.375], [0.50, -0.16, 0.375]], 0.02, { out: [0, 0, 1], sw: 0.05 });+ const pTop = kit.plate([[-0.46, 0.40, 0.36], [-0.46, 0.40, -0.30], [-0.46, -0.36, -0.30], [-0.46, -0.36, 0.36]], 0.06, { col: PAINT, out: [-1, 0, 0] });+ kit.loop(pTop, 0.06); kit.bolts(pTop, 0.03, 1, 0.018);+ const pF = kit.plate([[-0.42, 0.42, 0.34], [0.34, 0.385, 0.34], [0.34, 0.385, -0.26], [-0.42, 0.42, -0.26]], 0.05, { col: PAINT2, out: [0, 1, 0] });+ kit.bolts(pF, 0.028, 1, 0.016); kit.seam(pF, [-0.02, 0.405, 0.32], [-0.02, 0.405, -0.24]);+ kit.plate([[-0.42, -0.385, 0.34], [0.34, -0.385, 0.34], [0.34, -0.385, -0.26], [-0.42, -0.385, -0.26]], 0.05, { col: GUN, out: [0, -1, 0] });+ kit.plate([[-0.42, 0.38, -0.30], [-0.42, -0.36, -0.30], [0.26, -0.34, -0.30], [0.26, 0.36, -0.30]], 0.04, { col: PAINT2, out: [0, 0, -1] });+ // Lower arm plating+ const la = sideP(0.25, [[0.56, -0.20], [0.92, -0.15], [0.92, 0.15], [0.56, 0.20]], 0.04, { col: PAINT });+ kit.seam(la, [0.74, -0.18, 0.25], [0.74, 0.18, 0.25]);+ sideP(-0.25, [[0.40, -0.22], [0.92, -0.15], [0.92, 0.15], [0.40, 0.22]], 0.04, { col: PAINT2 });+ kit.plate([[0.56, 0.255, 0.21], [0.92, 0.195, 0.17], [0.92, 0.195, -0.17], [0.56, 0.255, -0.21]], 0.04, { col: PAINT2, out: [0, 1, 0] });+ kit.mat('rubber');+ kit.cables([[0.12, -0.05, 0.25], [0.46, -0.30, 0.31], [0.70, -0.26, 0.285], [0.92, -0.17, 0.27], [F.L, 0, 0.29]], 0.025, [{ r: 0.018, col: CAB_K }, { r: 0.014, col: CAB_R }]);+ kit.mat('armour');+ const A = kit.at(F, 0.25, -0.46, 0), Ab = kit.at(F, 0.32, -0.26, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(A, Ab, z0, { col: GUN, w: 0.08 });+ kit.mirror(1);+ return { G, A };+}++function forearmBase(kit, s, z0) {+ kit.mirror(s);+ kit.frame(null); kit.mat('steel');+ kit.drum([SK.elbow[0], SK.elbow[1], z0], [0, 0, 1], 0.27, 0.56, { bolts: 8, gear: 20, gearW: 0.35, seg: 20 });+ kit.mat('armour');+}++function buildCannonArm(kit, parent, upperA) {+ const z0 = SK.armZ;+ const G = kit.group('Elbow_R', parent, [SK.elbow[0], SK.elbow[1], z0], 'Forearm_R');+ G.node.userData = { hinge: 'z' };+ kit.use(G); forearmBase(kit, 1, z0);+ const y = SK.elbow[1];+ const ring = (x, hy, hz) => [[x, y + hy, z0 + hz], [x, y + hy, z0 - hz], [x, y - hy, z0 - hz], [x, y - hy, z0 + hz]];+ kit.hull([0.4, y, z0]);+ kit.loft([ring(0.02, 0.24, 0.27), ring(0.72, 0.28, 0.30), ring(0.98, 0.21, 0.24)], { col: DARK });+ const top = kit.plate([[0.04, y + 0.27, z0 + 0.28], [0.74, y + 0.31, z0 + 0.31], [0.74, y + 0.31, z0 - 0.31], [0.04, y + 0.27, z0 - 0.28]], 0.05, { col: PAINT, out: [0, 1, 0] });+ kit.loop(top, 0.05); kit.bolts(top, 0.03, 1, 0.016); kit.seam(top, [0.38, y + 0.29, z0 + 0.28], [0.38, y + 0.29, z0 - 0.28]);+ kit.hazardPlate([[0.76, y + 0.285, z0 + 0.28], [0.76, y + 0.285, z0 - 0.28], [0.99, y + 0.225, z0 - 0.22], [0.99, y + 0.225, z0 + 0.22]], 0.04, { sw: 0.06, out: [0.4, 1, 0] });+ const so = kit.plate([[0.04, y - 0.22, z0 + 0.31], [0.74, y - 0.26, z0 + 0.335], [0.74, y + 0.26, z0 + 0.335], [0.04, y + 0.22, z0 + 0.31]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.bolts(so, 0.03, 1, 0.016);+ const so2 = kit.plate([[0.18, y - 0.13, z0 + 0.36], [0.60, y - 0.15, z0 + 0.365], [0.60, y + 0.15, z0 + 0.365], [0.18, y + 0.13, z0 + 0.36]], 0.03, { col: RUST, out: [0, 0, 1] });+ kit.bolts(so2, 0.02, 1, 0.012);+ const si = kit.plate([[0.04, y + 0.22, z0 - 0.31], [0.74, y + 0.26, z0 - 0.335], [0.74, y - 0.26, z0 - 0.335], [0.04, y - 0.22, z0 - 0.31]], 0.05, { col: PAINT2, out: [0, 0, -1] });+ kit.loop(si, 0.05);+ kit.plate([[0.76, y + 0.205, z0 + 0.255], [0.99, y + 0.17, z0 + 0.21], [0.99, y - 0.17, z0 + 0.21], [0.76, y - 0.205, z0 + 0.255]], 0.03, { col: GUN, out: [0.3, 0, 1] });+ kit.plate([[0.76, y + 0.205, z0 - 0.255], [0.99, y + 0.17, z0 - 0.21], [0.99, y - 0.17, z0 - 0.21], [0.76, y - 0.205, z0 - 0.255]], 0.03, { col: GUN, out: [0.3, 0, -1] });+ kit.plate([[0.04, y - 0.25, z0 + 0.28], [0.74, y - 0.285, z0 + 0.30], [0.74, y - 0.285, z0 - 0.30], [0.04, y - 0.25, z0 - 0.28]], 0.04, { col: GUN, out: [0, -1, 0] });+ // Ammo drum and feed+ kit.mat('steel');+ const dc = [0.40, y - 0.52, z0];+ kit.drum(dc, [0, 0, 1], 0.27, 0.46, { bolts: 8, seg: 22, col: PAINT2, gear: 16, gearW: 0.8 });+ kit.block([0.40, y - 0.29, z0], [0.30, 0.14, 0.34], { col: GUN, c: 0.02 });+ for (let i = 0; i < 5; i++) kit.lathe([0.40, y - 0.52, z0 - 0.17 + i * 0.085], [0, 0, 1], [[0, 0.272], [0, 0.285, STEEL_D], [0.018, 0.285, STEEL_D], [0.018, 0.272]], { seg: 22, col: STEEL_D });+ kit.mat('armour');+ kit.hazardPlate([[0.14, y - 0.70, z0 + 0.24], [0.66, y - 0.70, z0 + 0.24], [0.66, y - 0.82, z0 + 0.24], [0.14, y - 0.82, z0 + 0.24]], 0.012, { sw: 0.05, out: [0, 0, 1], c: 0.006 });+ kit.mat('steel');+ // Barrel bearing+ kit.lathe([0.96, y, z0], [1, 0, 0], [[0, 0.22, GUN], [0.05, 0.255, STEEL_D], [0.15, 0.255, GUN], [0.15, 0.14, tone(GUN, 0.5)]], { seg: 22 });+ kit.mat('rubber');+ kit.tube([[0.66, y - 0.42, z0 + 0.26], [0.86, y - 0.34, z0 + 0.30], [0.98, y - 0.24, z0 + 0.20], [1.03, y - 0.15, z0 + 0.05], [1.05, y, z0]], 0.03, { col: CAB_K, seg: 8 });+ kit.tube([[0.10, y + 0.02, z0 + 0.335], [0.34, y + 0.20, z0 + 0.375], [0.70, y + 0.21, z0 + 0.375], [0.90, y + 0.10, z0 + 0.28], [1.04, y + 0.03, z0 + 0.12], [1.05, y, z0]], 0.016, { col: CAB_R });+ kit.tube([[0.10, y - 0.02, z0 + 0.335], [0.34, y - 0.19, z0 + 0.375], [0.70, y - 0.20, z0 + 0.375], [0.90, y - 0.10, z0 + 0.28], [1.04, y - 0.03, z0 + 0.12], [1.05, y, z0]], 0.016, { col: CAB_Y });+ kit.mat('armour');+ const horn = [-0.46, y + 0.10, z0];+ kit.hull(null);+ kit.lug(horn, [-0.16, y + 0.06, z0], z0, { ear: 0.075, w: 0.10, foot: 0.13, t: 0.04 });+ kit.piston('Elbow_R', parent, [upperA[0], upperA[1], z0], G, horn, { r: 0.075, rod: 0.044 });++ // Rotary barrels spin about local X+ const spinG = kit.group('CannonSpin_R', G, [SK.cannon[0], SK.cannon[1], z0], 'CannonBarrels_R');+ spinG.node.userData = { hinge: 'x' };+ kit.use(spinG); kit.mat('steel');+ const sx = SK.cannon[0], R = 0.135, Lb = 1.36;+ kit.lathe([sx + 0.05, y, z0], [1, 0, 0], [[0, 0, STEEL_D], [0, 0.21, GUN], [0.10, 0.21, STEEL_D], [0.12, 0.16, GUN], [0.32, 0.16, GUN], [0.34, 0.06, STEEL_D], [Lb, 0.06, STEEL_D], [Lb, 0, STEEL_D]], { seg: 18 });+ for (let i = 0; i < 6; i++) {+ const th = (i / 6) * 2 * Math.PI, cy = y + R * Math.cos(th), cz = z0 + R * Math.sin(th);+ kit.lathe([sx + 0.28, cy, cz], [1, 0, 0], [[0, 0.046, STEEL_D], [Lb - 0.20, 0.046, STEEL_D], [Lb - 0.20, 0.058, GUN], [Lb - 0.06, 0.058, GUN], [Lb - 0.04, 0.05, STEEL], [Lb - 0.04, 0.028, RUB], [Lb - 0.16, 0.028, RUB], [Lb - 0.16, 0, RUB]], { seg: 9 });+ }+ for (const d of [0.62, 1.00]) {+ kit.lathe([sx + d, y, z0], [1, 0, 0], [[0, 0, GUN], [0, 0.205, GUN], [0.014, 0.218, vMix(GUN, WEAR, 0.5)], [0.05, 0.218, GUN], [0.064, 0.205, vMix(GUN, WEAR, 0.5)], [0.064, 0, GUN]], { seg: 18 });+ }+ kit.lathe([sx + Lb - 0.09, y, z0], [1, 0, 0], [[0, 0.22, STEEL_D], [0.05, 0.22, GUN]], { seg: 18 });+ kit.mat('armour');+ return { G, spinG };+}++function buildClawArm(kit, parent, upperA) {+ const z0 = SK.armZ;+ const G = kit.group('Elbow_L', parent, [SK.elbow[0], SK.elbow[1], -z0], 'Forearm_L');+ G.node.userData = { hinge: 'z' };+ kit.use(G); forearmBase(kit, -1, z0);+ const y = SK.elbow[1];+ const ring = (x, hy, hz) => [[x, y + hy, z0 + hz], [x, y + hy, z0 - hz], [x, y - hy, z0 - hz], [x, y - hy, z0 + hz]];+ kit.hull([0.4, y, z0]);+ kit.loft([ring(0.02, 0.25, 0.27), ring(0.56, 0.31, 0.33), ring(0.96, 0.20, 0.23)], { col: DARK });+ const top = kit.plate([[0.04, y + 0.28, z0 + 0.29], [0.58, y + 0.345, z0 + 0.345], [0.58, y + 0.345, z0 - 0.345], [0.04, y + 0.28, z0 - 0.29]], 0.05, { col: PAINT, out: [0, 1, 0] });+ kit.bolts(top, 0.03, 1, 0.016); kit.loop(top, 0.05);+ const top2 = kit.plate([[0.60, y + 0.335, z0 + 0.33], [0.95, y + 0.235, z0 + 0.235], [0.95, y + 0.235, z0 - 0.235], [0.60, y + 0.335, z0 - 0.33]], 0.05, { col: PAINT2, out: [0.3, 1, 0] });+ kit.bolts(top2, 0.03, 1, 0.014);+ const so = kit.plate([[0.04, y - 0.24, z0 + 0.31], [0.58, y - 0.30, z0 + 0.365], [0.95, y - 0.19, z0 + 0.255], [0.95, y + 0.19, z0 + 0.255], [0.58, y + 0.30, z0 + 0.365], [0.04, y + 0.24, z0 + 0.31]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.bolts(so, 0.03, 1, 0.016);+ const so2 = kit.plate([[0.14, y - 0.15, z0 + 0.39], [0.62, y - 0.18, z0 + 0.39], [0.62, y + 0.18, z0 + 0.39], [0.14, y + 0.15, z0 + 0.39]], 0.03, { col: PAINT2, out: [0, 0, 1] });+ kit.bolts(so2, 0.022, 1, 0.012);+ kit.stencil([0.26, y - 0.07, z0 + 0.39], [1, 0, 0], [0, 1, 0], 'H-7', 0.14, BONE, [0, 0, 1]);+ kit.hazardPlate([[0.66, y - 0.16, z0 + 0.32], [0.92, y - 0.13, z0 + 0.27], [0.92, y + 0.13, z0 + 0.27], [0.66, y + 0.16, z0 + 0.32]], 0.02, { sw: 0.05, out: [0.2, 0, 1] });+ kit.plate([[0.04, y + 0.24, z0 - 0.31], [0.58, y + 0.30, z0 - 0.365], [0.95, y + 0.19, z0 - 0.255], [0.95, y - 0.19, z0 - 0.255], [0.58, y - 0.30, z0 - 0.365], [0.04, y - 0.24, z0 - 0.31]], 0.05, { col: PAINT2, out: [0, 0, -1] });+ kit.plate([[0.04, y - 0.265, z0 + 0.28], [0.58, y - 0.335, z0 + 0.34], [0.95, y - 0.215, z0 + 0.23], [0.95, y - 0.215, z0 - 0.23], [0.58, y - 0.335, z0 - 0.34], [0.04, y - 0.265, z0 - 0.28]], 0.04, { col: GUN, out: [0, -1, 0] });+ kit.mat('rubber');+ kit.cables([[0.10, y - 0.02, z0 + 0.345], [0.32, y - 0.25, z0 + 0.40], [0.66, y - 0.22, z0 + 0.39], [0.86, y - 0.20, z0 + 0.25], [0.96, y, z0 + 0.18]], 0.022, [{ r: 0.018, col: CAB_K }, { r: 0.015, col: CAB_Y }]);+ kit.mat('armour');+ const horn = [-0.46, y + 0.10, z0];+ kit.hull(null);+ kit.lug(horn, [-0.16, y + 0.06, z0], z0, { ear: 0.075, w: 0.10, foot: 0.13, t: 0.04 });+ kit.piston('Elbow_L', parent, [upperA[0], upperA[1], z0], G, horn, { r: 0.075, rod: 0.044 });++ // Wrist and claw (mirrored into -Z)+ const wG = kit.group('Wrist_L', G, [SK.wrist[0], SK.wrist[1], -z0], 'Palm_L');+ wG.node.userData = { hinge: 'x' };+ kit.use(wG); kit.mat('steel');+ const wx = SK.wrist[0];+ kit.lathe([wx - 0.02, y, z0], [1, 0, 0], [[0, 0.19, GUN], [0.06, 0.20, STEEL_D], [0.11, 0.17, GUN], [0.13, 0.15, STEEL_D]], { seg: 20 });+ kit.mat('armour');+ kit.hull([wx + 0.2, y, z0]);+ kit.block([wx + 0.24, y, z0], [0.26, 0.38, 0.46], { col: DARK, c: 0.04 });+ const pt = kit.plate([[wx + 0.11, y + 0.215, z0 + 0.24], [wx + 0.37, y + 0.215, z0 + 0.24], [wx + 0.37, y + 0.215, z0 - 0.24], [wx + 0.11, y + 0.215, z0 - 0.24]], 0.03, { col: PAINT, out: [0, 1, 0] });+ kit.bolts(pt, 0.02, 1, 0.012);+ kit.hazardPlate([[wx + 0.11, y - 0.14, z0 + 0.245], [wx + 0.37, y - 0.14, z0 + 0.245], [wx + 0.37, y + 0.14, z0 + 0.245], [wx + 0.11, y + 0.14, z0 + 0.245]], 0.02, { sw: 0.04, out: [0, 0, 1] });+ kit.plate([[wx + 0.11, y + 0.14, z0 - 0.245], [wx + 0.37, y + 0.14, z0 - 0.245], [wx + 0.37, y - 0.14, z0 - 0.245], [wx + 0.11, y - 0.14, z0 - 0.245]], 0.02, { col: GUN, out: [0, 0, -1] });+ const hx = wx + 0.35;+ const finger = (name, hy, hz, sgnY, wid) => {+ const fG = kit.group(name, wG, [hx, y + hy, -(z0 + hz)], 'Claw_' + name);+ fG.node.userData = { hinge: 'z' };+ kit.use(fG); kit.mat('steel');+ kit.drum([hx, y + hy, z0 + hz], [0, 0, 1], 0.075, wid + 0.03, { bolts: 0, seg: 12 });+ kit.mat('armour');+ const pts = [[-0.07, 0.07], [0.30, 0.15], [0.60, 0.11], [0.84, -0.09], [0.90, -0.24], [0.74, -0.15], [0.54, -0.05], [0.30, -0.035], [-0.07, -0.07]];+ const map = (w) => pts.map(([x, py]) => [hx + x, y + hy + sgnY * py, z0 + hz + w]);+ kit.plate(map(wid / 2), wid / 2, { out: [0, 0, 1], col: PAINT2, c: 0.02 });+ kit.plate(map(-wid / 2), wid / 2, { out: [0, 0, -1], col: PAINT2, c: 0.02 });+ const ba = [[-0.03, 0.095], [0.30, 0.165], [0.58, 0.13], [0.40, 0.10]].map(([x, py]) => [hx + x, y + hy + sgnY * py + sgnY * 0.002, z0 + hz + wid * 0.34]);+ const bb = ba.map((p) => [p[0], p[1], z0 + hz - wid * 0.34]);+ kit.poly([ba[0], ba[1], bb[1], bb[0]], HAZ_K, { out: [0, sgnY, 0] });+ kit.mat('steel');+ const tipA = [[0.70, -0.02], [0.88, -0.12], [0.905, -0.25], [0.74, -0.155]].map(([x, py]) => [hx + x, y + hy + sgnY * py, z0 + hz]);+ kit.plate(tipA.map((p) => [p[0], p[1], p[2] + wid / 2 + 0.008]), wid / 2 + 0.008, { out: [0, 0, 1], col: STEEL, c: 0.02 });+ kit.plate(tipA.map((p) => [p[0], p[1], p[2] - wid / 2 - 0.008]), wid / 2 + 0.008, { out: [0, 0, -1], col: STEEL, c: 0.02 });+ kit.mat('armour');+ return fG;+ };+ kit.frame(null);+ finger('ClawUpperOuter_L', 0.13, 0.16, 1, 0.13);+ finger('ClawUpperInner_L', 0.13, -0.16, 1, 0.13);+ finger('ClawThumb_L', -0.13, 0, -1, 0.16);+ kit.mirror(1);+ return { G };+}++// ---------------------------------------------------------------- missile pod+function buildPod(kit, waistG) {+ const G = kit.group('MissilePod', waistG, [-0.05, 5.50, -1.42], 'MissilePod');+ G.node.userData = { hinge: 'z' };+ kit.use(G); kit.frame(null); kit.mirror(-1);+ const zc = 1.46, x0 = -0.62, x1 = 0.62, y0 = 5.54, y1 = 5.97, hz = 0.42;+ kit.hull([0, (y0 + y1) / 2, zc]); kit.mat('armour');+ const r = (x, ya, yb, h) => [[x, yb, zc + h], [x, yb, zc - h], [x, ya, zc - h], [x, ya, zc + h]];+ kit.loft([r(x0, y0 + 0.04, y1 - 0.06, hz - 0.06), r(x0 + 0.12, y0, y1, hz), r(x1 - 0.03, y0, y1, hz), r(x1, y0 + 0.02, y1 - 0.02, hz - 0.02)], { col: DARK });+ const top = kit.plate([[x0 + 0.12, y1 + 0.02, zc + hz], [x1 - 0.14, y1 + 0.02, zc + hz], [x1 - 0.14, y1 + 0.02, zc - hz], [x0 + 0.12, y1 + 0.02, zc - hz]], 0.03, { col: PAINT, out: [0, 1, 0] });+ kit.loop(top, 0.05); kit.bolts(top, 0.028, 2, 0.014);+ kit.hazardPlate([[x1 - 0.13, y1 + 0.02, zc + hz], [x1 - 0.13, y1 + 0.02, zc - hz], [x1 + 0.02, y1 + 0.005, zc - hz], [x1 + 0.02, y1 + 0.005, zc + hz]], 0.02, { sw: 0.07, out: [0, 1, 0] });+ for (const sg of [1, -1]) {+ const so = kit.plate([[x0 + 0.14, y1 - 0.03, zc + sg * (hz + 0.025)], [x1 - 0.06, y1 - 0.03, zc + sg * (hz + 0.025)], [x1 - 0.06, y0 + 0.03, zc + sg * (hz + 0.025)], [x0 + 0.14, y0 + 0.03, zc + sg * (hz + 0.025)]], 0.03, { col: sg > 0 ? PAINT : PAINT2, out: [0, 0, sg] });+ kit.loop(so, 0.045); kit.bolts(so, 0.026, 2, 0.014);+ if (sg > 0) {+ const s2 = kit.plate([[x0 + 0.26, y1 - 0.12, zc + hz + 0.05], [x0 + 0.72, y1 - 0.12, zc + hz + 0.05], [x0 + 0.72, y0 + 0.11, zc + hz + 0.05], [x0 + 0.26, y0 + 0.11, zc + hz + 0.05]], 0.025, { col: RUST, out: [0, 0, 1] });+ kit.bolts(s2, 0.018, 1, 0.01);+ kit.stencil([x0 + 0.84, y0 + 0.13, zc + hz + 0.025], [1, 0, 0], [0, 1, 0], 'HE', 0.14, BONE, [0, 0, 1]);+ }+ }+ kit.plate([[x0 + 0.1, y0 - 0.015, zc + hz], [x1 - 0.03, y0 - 0.015, zc + hz], [x1 - 0.03, y0 - 0.015, zc - hz], [x0 + 0.1, y0 - 0.015, zc - hz]], 0.02, { col: GUN, out: [0, -1, 0] });+ // Rear vent+ kit.vent([x0 - 0.008, y0 + 0.08, zc - 0.28], [x0 - 0.008, y0 + 0.08, zc + 0.28], [x0 - 0.008, y1 - 0.10, zc + 0.28], [x0 - 0.008, y1 - 0.10, zc - 0.28], { n: 5, col: GUN });+ // Launch tubes, two rows of four+ const front = x1 + 0.002;+ kit.poly([[front, y1 - 0.02, zc + hz - 0.02], [front, y1 - 0.02, zc - hz + 0.02], [front, y0 + 0.02, zc - hz + 0.02], [front, y0 + 0.02, zc + hz - 0.02]], GUN, { out: [1, 0, 0] });+ kit.mat('steel');+ for (let row = 0; row < 2; row++) for (let col = 0; col < 4; col++) {+ const cy = (y0 + y1) / 2 + (row - 0.5) * 0.205, cz = zc + (col - 1.5) * 0.195;+ kit.lathe([front, cy, cz], [1, 0, 0], [[0, 0.088, STEEL_D], [0.022, 0.088, vMix(STEEL_D, WEAR, 0.4)], [0.022, 0.072, tone(GUN, 0.4)], [-0.06, 0.072, tone(GUN, 0.4)]], { seg: 12 });+ kit.mat('rubber');+ kit.lathe([front - 0.05, cy, cz], [1, 0, 0], [[0, 0.064, BONE], [0.03, 0.062, BONE], [0.055, 0.044, RUST], [0.08, 0.018, RUST], [0.088, 0, RUST]], { seg: 10, mat: 'rubber' });+ kit.mat('steel');+ }+ // Trunnion+ kit.drum([-0.05, 5.50, 1.42], [0, 0, 1], 0.09, 0.22, { bolts: 0, seg: 14 });+ kit.mat('rubber');+ kit.tube([[-0.40, y0 - 0.01, zc + 0.10], [-0.32, 5.50, 1.42], [-0.15, 5.47, 1.38], [-0.05, 5.50, 1.36]], 0.018, { col: CAB_K });+ kit.mat('armour');+ kit.mirror(1);+}++// ---------------------------------------------------------------- build+async function build() {+ const root = createRoot('HeavyWalkerMech');+ const tex = makeTextures();+ const MAT = {+ armour: pbrMaterial({ albedo: tex.albedo, metallicRoughness: tex.mr, normal: tex.nrm, roughness: 1, metalness: 1, name: 'Armour_WeatheredPaint' }),+ // Metalness stays below 1 so machinery keeps a diffuse read in scenes without an environment map.+ steel: pbrMaterial({ albedo: tex.albedo, normal: tex.nrm, roughness: 0.5, metalness: 0.55, name: 'Steel_Machinery' }),+ chrome: gameMaterial(0xf0f2f4, { metalness: 0.72, roughness: 0.22, flatShading: false, name: 'Chrome_PistonRod' }),+ rubber: gameMaterial(0xffffff, { metalness: 0, roughness: 0.78, flatShading: false, name: 'Rubber_CableAndTrim' }),+ glass: glassMaterial(0x4f8598, { opacity: 0.32, roughness: 0.05, metalness: 0.2, name: 'Canopy_Glass' }),+ glowCyan: gameMaterial(0x06131a, { emissive: 0x2cc4f5, emissiveIntensity: 1.5, metalness: 0, roughness: 0.4, flatShading: false, name: 'Glow_Cyan' }),+ glowAmber: gameMaterial(0x1a0c02, { emissive: 0xff6408, emissiveIntensity: 1.15, metalness: 0, roughness: 0.4, flatShading: false, name: 'Glow_Amber' }),+ };+ const kit = createKit(root, MAT, { armour: true, steel: true, rubber: true });++ const pelvisG = kit.group('Pelvis', null, [SK.hip[0], SK.hip[1], 0], 'Pelvis');+ buildPelvis(kit, pelvisG);+ buildLeg(kit, 'L', pelvisG);+ buildLeg(kit, 'R', pelvisG);+ const waistG = kit.group('Waist', pelvisG, [0, SK.waistY, 0], 'Torso');+ waistG.node.userData = { hinge: 'y (yaw); clip also rolls/pitches slightly' };+ buildTorso(kit, waistG);+ const ua = { L: buildUpperArm(kit, 'L', waistG), R: buildUpperArm(kit, 'R', waistG) };+ buildCannonArm(kit, ua.R.G, ua.R.A);+ buildClawArm(kit, ua.L.G, ua.L.A);+ buildPod(kit, waistG);++ // Size each piston from its real span across the Walk cycle (with margin for code-driven poses).+ const N = rigNodes(root);+ const rest = rigList(N).map((n) => [n, n.position.clone(), n.quaternion.clone()]);+ const worldA = new THREE.Vector3(), worldB = new THREE.Vector3();+ const stats = kit.pistonList().map((p) => {+ const s = p.mir;+ const la = new THREE.Vector3(p.A[0] - p.gA.origin[0], p.A[1] - p.gA.origin[1], p.A[2] * s - p.gA.origin[2]);+ const lb = new THREE.Vector3(p.B[0] - p.gB.origin[0], p.B[1] - p.gB.origin[1], p.B[2] * s - p.gB.origin[2]);+ return { p, la, lb, min: Infinity, max: 0 };+ });+ for (let i = 0; i < 30; i++) {+ applyPose(N, walkPose(i / 30));+ root.updateMatrixWorld(true);+ for (const st of stats) {+ const d = st.p.gA.node.localToWorld(worldA.copy(st.la)).distanceTo(st.p.gB.node.localToWorld(worldB.copy(st.lb)));+ st.min = Math.min(st.min, d); st.max = Math.max(st.max, d);+ }+ }+ rest.forEach(([n, pos, q]) => { n.position.copy(pos); n.quaternion.copy(q); });+ root.updateMatrixWorld(true);+ for (const st of stats) kit.buildPiston(st.p, st.min - 0.04, st.max + 0.05);++ kit.flush();+ root.userData = { rig: { clip: 'Walk', walkSpeedMps: Number(WALK.speed.toFixed(4)), strideM: Number(WALK.stride.toFixed(4)), durationS: WALK.T, forward: '+X', hingeAxis: 'local Z unless the joint says otherwise' } };+ return root;+}++// ---------------------------------------------------------------- Walk clip+function animate(root) {+ const N = rigNodes(root);+ const aimers = [];+ root.traverse((n) => { if (n.userData && n.userData.aimAt) aimers.push(n); });+ const nodes = rigList(N);+ const rest = nodes.concat(aimers).map((n) => [n, n.position.clone(), n.quaternion.clone()]);+ const names = nodes.concat(aimers);+ const keyRot = new Map(names.map((n) => [n.name, []]));+ const keyPos = [];+ const eul = new THREE.Euler(), wp = new THREE.Vector3(), dir = new THREE.Vector3(), restDir = new THREE.Vector3();+ for (let i = 0; i <= WALK.keys; i++) {+ const phi = (i % WALK.keys) / WALK.keys, t = (WALK.T * i) / WALK.keys;+ applyPose(N, walkPose(phi));+ root.updateMatrixWorld(true);+ for (const n of aimers) {+ const target = root.getObjectByName(n.userData.aimAt);+ target.getWorldPosition(wp);+ n.parent.worldToLocal(wp);+ dir.copy(wp).sub(n.position).normalize();+ const r = n.userData.aimRestDir;+ restDir.set(r[0], r[1], r[2]);+ n.quaternion.setFromUnitVectors(restDir, dir);+ }+ for (const n of names) {+ eul.setFromQuaternion(n.quaternion, 'XYZ');+ keyRot.get(n.name).push({ time: t, rotation: [eul.x / D2R, eul.y / D2R, eul.z / D2R] });+ }+ keyPos.push({ time: t, position: [N.pelvis.position.x, N.pelvis.position.y, N.pelvis.position.z] });+ }+ rest.forEach(([n, pos, q]) => { n.position.copy(pos); n.quaternion.copy(q); });+ root.updateMatrixWorld(true);+ const tracks = [positionTrack(N.pelvis.name, keyPos)];+ for (const n of names) tracks.push(rotationTrack(n.name, keyRot.get(n.name)));+ return [createClip('Walk', WALK.T, tracks, { loop: true })];+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-2.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/mech-2.kiln.jsnew file mode 100644index 00000000..a1ddfac0--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/mech-2.kiln.js@@ -0,0 +1,1763 @@+// Heavy walking mech, ~6 m. Metres, +X forward, +Y up, +Z right. L = -Z, R = +Z.+// Every joint rests at identity rotation with asset-aligned axes, so a hinge is+// simply node.rotation.z (positive tips +X up to +Y; a hanging limb swings forward).+const meta = { name: 'Heavy Walker Mech', role: 'vehicle' };++const SK = {+ hip: [0, 3.42], knee: [0.70, 2.17], ankle: [-0.60, 1.12], toe: [0, 0.22],+ tipX: 0.95, toeH: 0.22, legZ: 0.86,+ waistY: 3.86,+ shoulder: [0.02, 5.00], elbow: [-0.12, 4.00], armZ: 1.60,+ cannon: [1.00, 4.00], wrist: [0.95, 4.00],+};++const WALK = { T: 2.4, keys: 120, duty: 0.64, stanceLen: 1.72, lift: 0.46, xC: 0.95, height: 3.34, bob: 0.055, sway: 0.11, surge: 0.03 };+WALK.stride = WALK.stanceLen / WALK.duty;+WALK.speed = WALK.stride / WALK.T;++const D2R = Math.PI / 180;+const TILE = 2.2;++// ---------------------------------------------------------------- vectors+const vAdd = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];+const vSub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];+const vMul = (a, k) => [a[0] * k, a[1] * k, a[2] * k];+const vDot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];+const vCross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];+const vLen = (a) => Math.hypot(a[0], a[1], a[2]);+const vUnit = (a) => { const l = vLen(a) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };+const vMix = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];+const vPerp = (d) => { const a = Math.abs(d[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]; return vUnit(vCross(a, d)); };++// ---------------------------------------------------------------- colour (linear)+const lin = (hex) => {+ const f = (c) => { const x = c / 255; return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); };+ return [f((hex >> 16) & 255), f((hex >> 8) & 255), f(hex & 255)];+};+const tone = (c, k) => [c[0] * k, c[1] * k, c[2] * k];+const PAINT = lin(0x87886f), PAINT2 = lin(0x6e715d), DARK = lin(0x4b4e4f), GUN = lin(0x404447), BONE = lin(0xcdc6ad), RUST = lin(0xa84a2c), PISTON = lin(0x6f7376), SUN = lin(0xaeac96);+// Gunmetal for machinery, and the wear palette+const GM = lin(0x7a8089), GM_D = lin(0x43474c), GM_L = lin(0xa4abb3), GM_H = lin(0xc9ced3), HEAT_A = lin(0x515c78), BARE = lin(0xa2a6ab);+const BATCH_A = lin(0x7c8a62), BATCH_B = lin(0x90866a), DUST_D = lin(0x7a6c56), DUST_L = lin(0x8d7f68), OIL = lin(0x0d0b09), RUST_D = lin(0x74401f), GRIME = lin(0x15110d), SOOT = lin(0x0b0b0c);+// Wear atlas rows in texture V (see makeTextures), and the steel map's polished band+const CHIP_V = 0.195, CHIP_DEPTH = 0.135, GRIME_V0 = 0.25, GRIME_MID = 0.3125, GRIME_END = 0.375, RUN_V0 = 0.376, RUN_V1 = 0.874, DUST_V0 = 0.876, POLISH_V = 0.04, STEEL_V = 0.13;+const HAZ_Y = lin(0xe8ae1c), HAZ_K = lin(0x1a1b1d), SEAM = lin(0x0a0b0c), WEAR = lin(0xe6e3d4), STEEL = lin(0xbcc1c5), STEEL_D = lin(0x7f8387);+const RUB = lin(0x1c1d1f), CAB_R = lin(0xa8361f), CAB_Y = lin(0xc99a1e), CAB_K = lin(0x202225), CAB_B = lin(0x2d4f63), SUIT = lin(0x8f4f22), WHITE = [1, 1, 1];++// ---------------------------------------------------------------- walk solve+function pchipPeriodic(keys) {+ const n = keys.length, xs = keys.map((k) => k[0]), ys = keys.map((k) => k[1]);+ const h = (i) => (i + 1 < n ? xs[i + 1] : xs[0] + 1) - xs[i];+ const sl = (i) => (ys[(i + 1) % n] - ys[i]) / h(i);+ const m = xs.map((_, i) => {+ const p = (i + n - 1) % n, s0 = sl(p), s1 = sl(i);+ if (s0 * s1 <= 0) return 0;+ const h0 = h(p), h1 = h(i), w1 = 2 * h1 + h0, w2 = h1 + 2 * h0;+ return (w1 + w2) / (w1 / s0 + w2 / s1);+ });+ return (fIn) => {+ const f = fIn - Math.floor(fIn);+ let i = n - 1;+ for (let j = 0; j < n; j++) if (xs[j] <= f) i = j;+ const hh = h(i), fx = f < xs[i] ? f + 1 : f, t = (fx - xs[i]) / hh, t2 = t * t, t3 = t2 * t;+ return (2 * t3 - 3 * t2 + 1) * ys[i] + (t3 - 2 * t2 + t) * m[i] * hh + (-2 * t3 + 3 * t2) * ys[(i + 1) % n] + (t3 - t2) * m[(i + 1) % n] * hh;+ };+}+const b0 = WALK.duty;+const swingF = (s) => b0 + (1 - b0) * s;+// Foot pitch never goes positive: the toes stay lowest and set down first, then the sole rolls flat.+const PSI = pchipPeriodic([[0, 0], [0.50, 0], [b0, -24], [swingF(0.22), -27], [swingF(0.5), -13], [swingF(0.78), -4], [swingF(0.93), -0.8]]);+const GAMMA = pchipPeriodic([[0, 50], [0.32, 34], [0.50, 23], [b0, 22], [swingF(0.38), 50], [swingF(0.72), 57]]);++function footState(f) {+ const xf = WALK.xC + WALK.stanceLen / 2, S = WALK.stride;+ let cx, cy = 0;+ if (f < b0) cx = xf - S * f;+ else {+ const s = (f - b0) / (1 - b0), x0 = xf - WALK.stanceLen, x1 = xf, m = -S * (1 - b0), s2 = s * s, s3 = s2 * s;+ cx = (2 * s3 - 3 * s2 + 1) * x0 + (s3 - 2 * s2 + s) * m + (-2 * s3 + 3 * s2) * x1 + (s3 - s2) * m;+ // Zero height velocity at both ends with finite curvature; the peak falls early in the swing.+ const b = Math.sin(Math.PI * s * (1.35 - 0.35 * s));+ cy = WALK.lift * b * b;+ }+ return { cx, cy, psi: PSI(f) * D2R };+}++const wrapA = (a) => { let x = a; while (x > Math.PI) x -= 2 * Math.PI; while (x < -Math.PI) x += 2 * Math.PI; return x; };+const bone2 = (a, b) => [b[0] - a[0], b[1] - a[1]];+const BT = bone2(SK.hip, SK.knee), BS = bone2(SK.knee, SK.ankle), BM = bone2(SK.ankle, SK.toe);+const LT = Math.hypot(BT[0], BT[1]), LS = Math.hypot(BS[0], BS[1]), LM = Math.hypot(BM[0], BM[1]);+const AT0 = Math.atan2(BT[1], BT[0]), AS0 = Math.atan2(BS[1], BS[0]), AM0 = Math.atan2(BM[1], BM[0]);++function eulerQ(xDeg, yDeg, zDeg) {+ return new THREE.Quaternion().setFromEuler(new THREE.Euler(xDeg * D2R, yDeg * D2R, zDeg * D2R, 'YXZ'));+}+const qx = (r) => new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), r);+const qz = (r) => new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), r);++function solveLeg(sgn, f, pelvisPos, qP) {+ const foot = footState(f);+ const cp = Math.cos(foot.psi), sp = Math.sin(foot.psi);+ const px = foot.cx + (-SK.tipX * cp - SK.toeH * sp);+ const py = foot.cy + (-SK.tipX * sp + SK.toeH * cp);+ const pz = sgn * SK.legZ;+ const hipOff = new THREE.Vector3(0, 0, sgn * SK.legZ).applyQuaternion(qP);+ const dx = px - (pelvisPos[0] + hipOff.x), dy = py - (pelvisPos[1] + hipOff.y), dz = pz - (pelvisPos[2] + hipOff.z);+ const y2 = -Math.hypot(dy, dz);+ const rho = Math.atan2(dz / y2, dy / y2);+ const g = GAMMA(f) * D2R;+ const ax = dx - LM * Math.sin(g), ay = y2 + LM * Math.cos(g);+ const d = Math.min(Math.hypot(ax, ay), (LT + LS) * 0.995);+ const a = Math.acos(Math.max(-1, Math.min(1, (LT * LT + d * d - LS * LS) / (2 * LT * d))));+ const aT = Math.atan2(ay, ax) + a;+ const kx = LT * Math.cos(aT), ky = LT * Math.sin(aT);+ const aS = Math.atan2(ay - ky, ax - kx);+ const aM = Math.atan2(-Math.cos(g), Math.sin(g));+ const tH = wrapA(aT - AT0), tS = wrapA(aS - AS0), tM = wrapA(aM - AM0);+ const qHip = qP.clone().invert().multiply(qx(rho)).multiply(qz(tH));+ const qToe = qx(rho).multiply(qz(tM)).invert().multiply(qz(foot.psi));+ return { hip: qHip, knee: qz(wrapA(tS - tH)), ankle: qz(wrapA(tM - tS)), toe: qToe };+}++function walkPose(phi) {+ const tau = 2 * Math.PI, c1 = (a) => Math.cos(tau * (phi - a)), c2 = (a) => Math.cos(2 * tau * (phi - a));+ const pelvisPos = [WALK.surge * c2(0.20), WALK.height + WALK.bob * c2(0.34), -WALK.sway * c1(0.34)];+ const qP = eulerQ(-2.6 * c1(0.30), -5.5 * c1(0), -2.0 + 0.5 * c2(0.25));+ const fL = phi % 1, fR = (phi + 0.5) % 1;+ const shL = -8 * c1(0.04), shR = 6.5 * c1(0.04);+ return {+ pelvisPos, pelvisQ: qP,+ L: solveLeg(-1, fL, pelvisPos, qP), R: solveLeg(1, fR, pelvisPos, qP),+ waist: eulerQ(-1.9 * c1(0.38), 12.5 * c1(0.04), 2.0 + 0.4 * c2(0.30)),+ shoulderL: qz(shL * D2R), shoulderR: qz(shR * D2R),+ elbowL: qz(0.55 * 8 * c1(0.12) * D2R), elbowR: qz(-0.55 * 6.5 * c1(0.12) * D2R),+ pod: qz(1.1 * c2(0.42) * D2R),+ };+}++function rigNodes(root) {+ const n = (s) => root.getObjectByName('Joint_' + s);+ const o = { pelvis: n('Pelvis'), waist: n('Waist'), pod: n('MissilePod'), shoulderL: n('Shoulder_L'), shoulderR: n('Shoulder_R'), elbowL: n('Elbow_L'), elbowR: n('Elbow_R') };+ for (const s of ['L', 'R']) o[s] = { hip: n('Hip_' + s), knee: n('Knee_' + s), ankle: n('Ankle_' + s), toe: n('Toe_' + s) };+ return o;+}+function applyPose(N, p) {+ N.pelvis.position.set(p.pelvisPos[0], p.pelvisPos[1], p.pelvisPos[2]);+ N.pelvis.quaternion.copy(p.pelvisQ);+ N.waist.quaternion.copy(p.waist);+ N.pod.quaternion.copy(p.pod);+ N.shoulderL.quaternion.copy(p.shoulderL); N.shoulderR.quaternion.copy(p.shoulderR);+ N.elbowL.quaternion.copy(p.elbowL); N.elbowR.quaternion.copy(p.elbowR);+ for (const s of ['L', 'R']) for (const j of ['hip', 'knee', 'ankle', 'toe']) N[s][j].quaternion.copy(p[s][j]);+}+function rigList(N) {+ const out = [N.pelvis, N.waist, N.pod, N.shoulderL, N.shoulderR, N.elbowL, N.elbowR];+ for (const s of ['L', 'R']) for (const j of ['hip', 'knee', 'ankle', 'toe']) out.push(N[s][j]);+ return out;+}++// ---------------------------------------------------------------- mesh kit+function newell(P) {+ let x = 0, y = 0, z = 0;+ for (let i = 0; i < P.length; i++) {+ const a = P[i], b = P[(i + 1) % P.length];+ x += (a[1] - b[1]) * (a[2] + b[2]); y += (a[2] - b[2]) * (a[0] + b[0]); z += (a[0] - b[0]) * (a[1] + b[1]);+ }+ return vUnit([x, y, z]);+}+function centroid(P) { let c = [0, 0, 0]; for (const p of P) c = vAdd(c, p); return vMul(c, 1 / P.length); }+function triangulate(P, n) {+ const m = P.length;+ if (m === 3) return [[0, 1, 2]];+ let convex = true;+ for (let i = 0; i < m; i++) {+ const a = P[i], b = P[(i + 1) % m], c = P[(i + 2) % m];+ if (vDot(vCross(vSub(b, a), vSub(c, b)), n) < -1e-7) { convex = false; break; }+ }+ const out = [];+ if (convex) { for (let i = 1; i < m - 1; i++) out.push([0, i, i + 1]); return out; }+ const e1 = vUnit(vSub(P[1], P[0])), e2 = vCross(n, e1);+ const contour = P.map((p) => new THREE.Vector2(vDot(vSub(p, P[0]), e1), vDot(vSub(p, P[0]), e2)));+ for (const t of THREE.ShapeUtils.triangulateShape(contour, [])) {+ const tn = vCross(vSub(P[t[1]], P[t[0]]), vSub(P[t[2]], P[t[0]]));+ out.push(vDot(tn, n) >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]]);+ }+ return out;+}+function insetPoly(P, n, d) {+ const m = P.length, out = [];+ for (let i = 0; i < m; i++) {+ const p = P[i], a = P[(i + m - 1) % m], b = P[(i + 1) % m];+ const n1 = vCross(n, vUnit(vSub(p, a))), n2 = vCross(n, vUnit(vSub(b, p)));+ const mt = vUnit(vAdd(n1, n2)), den = Math.max(0.35, vDot(mt, n1));+ out.push(vAdd(p, vMul(mt, d / den)));+ }+ return out;+}++function createKit(root, MAT, COLORED, TEXTURED) {+ let G = null, matKey = 'armour', F = null, mir = 1, hullC = null, seed = 90125;+ const groups = [], pistons = [];+ const rnd = () => { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 4294967296; };+ const T = (p) => {+ let x = p[0], y = p[1], z = p[2];+ if (F) {+ const X = F.o[0] + F.u[0] * x + F.v[0] * y + F.w[0] * z;+ const Y = F.o[1] + F.u[1] * x + F.v[1] * y + F.w[1] * z;+ const Z = F.o[2] + F.u[2] * x + F.v[2] * y + F.w[2] * z;+ x = X; y = Y; z = Z;+ }+ return [x, y, mir < 0 ? -z : z];+ };+ const TN = (n) => {+ let x = n[0], y = n[1], z = n[2];+ if (F) {+ const X = F.u[0] * x + F.v[0] * y + F.w[0] * z, Y = F.u[1] * x + F.v[1] * y + F.w[1] * z, Z = F.u[2] * x + F.v[2] * y + F.w[2] * z;+ x = X; y = Y; z = Z;+ }+ return [x, y, mir < 0 ? -z : z];+ };+ const bucket = (key) => {+ const k = key || matKey;+ if (!G.b[k]) G.b[k] = { key: k, pos: [], nor: [], uv: [], col: [], idx: [] };+ return G.b[k];+ };+ const q8 = (x) => Math.max(0, Math.min(255, Math.round(x * 255)));+ // Colour grade by rest-pose height and facing: undersides sink into shadow, and everything below the+ // thigh goes progressively darker and dustier so the lower legs read as the dirtiest part of the machine.+ const addV = (b, a, n, c, u, v, al = 1) => {+ b.pos.push(a[0], a[1], a[2]); b.nor.push(n[0], n[1], n[2]); b.uv.push(u, v);+ let r = c[0], gr = c[1], bl = c[2];+ if (b.key !== 'decal') {+ // Sun bleaches paint on anything facing up.+ if (b.key === 'armour' && n[1] > 0.25) { const s = (n[1] - 0.25) * 0.34; r += (SUN[0] - r) * s; gr += (SUN[1] - gr) * s; bl += (SUN[2] - bl) * s; }+ if (b.key === 'armour' || b.key === 'steel') { const ao = 0.66 + 0.34 * (n[1] * 0.5 + 0.5); r *= ao; gr *= ao; bl *= ao; }+ if (a[1] < 2.6) {+ const rub = b.key === 'rubber', t = 1 - a[1] / 2.6, dark = 1 - (rub ? 0.08 : 0.44) * t, mix = (rub ? 0.14 : 0.58) * t * t;+ r = r * dark * (1 - mix) + DUST_D[0] * mix; gr = gr * dark * (1 - mix) + DUST_D[1] * mix; bl = bl * dark * (1 - mix) + DUST_D[2] * mix;+ }+ }+ b.col.push(q8(r), q8(gr), q8(bl), q8(al));+ };+ const tri = (b, i, j, k) => { if (mir < 0) b.idx.push(i, k, j); else b.idx.push(i, j, k); };+ const QUAD = [[0, 1, 2], [0, 2, 3]];+ const faceBasis = (n) => {+ let e2 = vSub([0, 1, 0], vMul(n, n[1]));+ if (vLen(e2) < 0.25) e2 = vSub([1, 0, 0], vMul(n, n[0]));+ e2 = vUnit(e2);+ return [vCross(e2, n), e2];+ };+ // Each chart sits somewhere inside the unit square so UVs can be stored as 16-bit. Steel keeps the+ // top of its map clear: that band is the polished finish for machined chamfers and bolt heads.+ const chartFit = (vals) => {+ const lo = Math.min(...vals), hi = Math.max(...vals), span = hi - lo, k = span > 1 ? 1 / span : 1, off = span < 1 ? rnd() * (1 - span) : 0;+ return (x) => (x - lo) * k + off;+ };+ const emit = (pts, n, cols, tris, key, polish) => {+ const b = bucket(key), base = b.pos.length / 3, an = TN(n), [e1, e2] = faceBasis(an);+ const abs = pts.map(T), us = abs.map((a) => vDot(a, e1) / TILE), vs = abs.map((a) => vDot(a, e2) / TILE);+ const fu = chartFit(us), fv = chartFit(vs);+ abs.forEach((a, i) => {+ const c = Array.isArray(cols[0]) ? cols[i] : cols;+ let v = fv(vs[i]);+ if (b.key === 'steel') v = polish ? POLISH_V : STEEL_V + v * (1 - STEEL_V);+ addV(b, a, an, c, fu(us[i]), v);+ });+ for (const t of tris) tri(b, base + t[0], base + t[1], base + t[2]);+ };+ // Alpha-blended overlay from the wear atlas; keeps authored UVs and uses vertex alpha for soft fades.+ const decal = (pts, n, uvs, cols, alphas, tris) => {+ const b = bucket('decal'), base = b.pos.length / 3, an = TN(n);+ pts.forEach((p, i) => addV(b, T(p), an, Array.isArray(cols[0]) ? cols[i] : cols, uvs[i][0], uvs[i][1], alphas[i]));+ for (const t of tris || QUAD) tri(b, base + t[0], base + t[1], base + t[2]);+ };++ const kit = {};+ kit.rnd = rnd;+ kit.use = (g) => { G = g; };+ kit.mat = (k) => { matKey = k; };+ kit.frame = (f) => { F = f; };+ kit.mirror = (s) => { mir = s; };+ kit.hull = (p) => { hullC = p; };+ kit.bone = (a, b, z) => {+ const u = vUnit([b[0] - a[0], b[1] - a[1], 0]), w = [0, 0, 1];+ return { o: [a[0], a[1], z], u, v: vCross(w, u), w, L: Math.hypot(b[0] - a[0], b[1] - a[1]) };+ };+ kit.at = (f, u, v, w) => [f.o[0] + f.u[0] * u + f.v[0] * v, f.o[1] + f.u[1] * u + f.v[1] * v, f.o[2] + w];+ kit.group = (name, parent, origin, label) => {+ const rel = parent ? vSub(origin, parent.origin) : origin;+ const node = createPivot(name, rel, parent ? parent.node : root);+ const g = { name, label: label || name, node, origin, b: {} };+ groups.push(g);+ return g;+ };++ kit.poly = (ptsIn, col, o = {}) => {+ let pts = ptsIn, n = newell(pts);+ if (o.out && vDot(n, o.out) < 0) { pts = [...pts].reverse(); n = vMul(n, -1); if (Array.isArray(col[0])) col = [...col].reverse(); }+ emit(pts, n, col, triangulate(pts, n), o.mat, o.polish);+ return n;+ };++ // Bevelled armour plate: front face at pts, bevel down to depth t. Some bevel runs are worn through to+ // bare metal and carry a chipped-paint decal onto the face; the rest stay painted with a lightly rubbed crest.+ kit.plate = (ptsIn, t = 0.05, o = {}) => {+ let P = ptsIn, n = newell(P);+ const out = o.out || (hullC ? vSub(centroid(P), hullC) : null);+ if (out && vDot(n, out) < 0) { P = [...P].reverse(); n = vMul(n, -1); }+ const c = o.c !== undefined ? o.c : Math.min(t * 0.8, 0.028);+ const inner = insetPoly(P, n, c);+ const painted = !o.mat && matKey === 'armour';+ const raw = o.col || PAINT;+ // Repaint batches: neighbouring plates never quite match.+ const batch = raw === PAINT || raw === PAINT2 ? vMix(raw, rnd() < 0.5 ? BATCH_A : BATCH_B, rnd() * 0.8) : raw;+ const base = tone(batch, 0.8 + rnd() * 0.34);+ const edgeCol = o.edge || WEAR, low = tone(vMix(base, RUST_D, 0.25), 0.28);+ const ys = P.map((p) => T(p)[1]), y0 = Math.min(...ys), y1 = Math.max(...ys);+ const shade = (p) => tone(base, 0.8 + 0.28 * (T(p)[1] - y0) / Math.max(0.05, y1 - y0));+ emit(inner, n, inner.map(shade), triangulate(inner, n), o.mat, o.polish);+ const m = P.length, drop = vMul(n, t), cen = centroid(inner);+ let area = 0, perim = 0;+ for (let i = 0; i < m; i++) {+ const a = inner[i], b = inner[(i + 1) % m];+ area += vDot(vCross(vSub(a, cen), vSub(b, cen)), n) / 2; perim += vLen(vSub(b, a));+ }+ const depth = Math.min(CHIP_DEPTH, (2 * area / Math.max(perim, 0.01)) * 0.9);+ const inside = (p) => inner.every((v, i) => vDot(vCross(vSub(inner[(i + 1) % m], v), vSub(p, v)), n) >= -1e-6);+ const chip = (a, b) => {+ const len = vLen(vSub(b, a));+ if (len < 0.05) return;+ const d = vUnit(vSub(b, a)), inw = vCross(n, d), lift = vMul(n, 0.0035);+ let D = depth;+ const fits = (dd) => inside(vAdd(a, vMul(inw, dd))) && inside(vAdd(b, vMul(inw, dd)));+ if (!fits(D)) D *= 0.5;+ if (!fits(D)) return;+ const ul = Math.min(1, len), u0 = rnd() * (1 - ul);+ decal([a, b, vAdd(b, vMul(inw, D)), vAdd(a, vMul(inw, D))].map((p) => vAdd(p, lift)), n,+ [[u0, 0], [u0 + ul, 0], [u0 + ul, CHIP_V], [u0, CHIP_V]], WHITE, [1, 1, 1, 1]);+ };+ const wearP = painted ? (o.wear === undefined ? 0.42 : o.wear) : 0;+ for (let i = 0; i < m; i++) {+ const j = (i + 1) % m, runs = Math.max(1, Math.min(3, Math.round(vLen(vSub(P[j], P[i])) / 0.42)));+ for (let k = 0; k < runs; k++) {+ const a0 = vMix(P[i], P[j], k / runs), a1 = vMix(P[i], P[j], (k + 1) / runs);+ const i0 = vMix(inner[i], inner[j], k / runs), i1 = vMix(inner[i], inner[j], (k + 1) / runs);+ const q = [vSub(a0, drop), vSub(a1, drop), i1, i0];+ if (rnd() < wearP && depth > 0.018) {+ // Bare metal that wears away along the run, so a long edge never reads as one bright bar.+ const dull = vMix(base, BARE, 0.3), flip = rnd() < 0.5;+ emit(q, newell(q), [tone(BARE, 0.34), tone(BARE, 0.34), flip ? BARE : dull, flip ? dull : BARE], QUAD, 'steel');+ chip(i0, i1);+ } else {+ const crest = vMix(base, edgeCol, 0.06 + rnd() * 0.14);+ emit(q, newell(q), [low, low, crest, crest], QUAD, o.mat, o.polish);+ }+ }+ }+ // Close the plate so an overhang stays solid when seen from behind.+ if (!o.noBack) {+ const backPts = [...P].reverse().map((p) => vSub(p, drop)), backN = vMul(n, -1);+ emit(backPts, backN, low, triangulate(backPts, backN), o.mat);+ }+ const Q = ptsIn;+ const h = { P, inner, n, t, col: base, painted, at: (s, tt, l = 0) => vAdd(vMix(vMix(Q[0], Q[1], s), vMix(Q[3], Q[2], s), tt), vMul(n, l)) };+ if (painted) kit.weather(h, y0, area, o.noDrip);+ return h;+ };++ // Plate-level weathering: dirt on the low edge, runs from the top, dust on anything near the ground.+ kit.weather = (h, y0, area, noDrip) => {+ if (area < 0.012) return;+ const inn = h.inner, m = inn.length, c = centroid(inn), lift = vMul(h.n, 0.003);+ let low = 0, lowY = Infinity;+ for (let i = 0; i < m; i++) { const y = (T(inn[i])[1] + T(inn[(i + 1) % m])[1]) / 2; if (y < lowY) { lowY = y; low = i; } }+ const a = inn[low], b = inn[(low + 1) % m], len = vLen(vSub(b, a));+ if (len > 0.06) {+ const d = vUnit(vSub(b, a)), inw = vCross(h.n, d), D = Math.min(0.075, Math.sqrt(area) * 0.3), ul = Math.min(1, len), u0 = rnd() * (1 - ul);+ decal([a, b, vAdd(b, vMul(inw, D)), vAdd(a, vMul(inw, D))].map((p) => vAdd(p, lift)), h.n,+ [[u0, GRIME_MID], [u0 + ul, GRIME_MID], [u0 + ul, GRIME_END], [u0, GRIME_END]], GRIME, [0.9, 0.9, 0.9, 0.9]);+ }+ if (!noDrip) {+ const count = Math.max(1, Math.min(5, Math.round(area * 11)));+ for (let i = 0; i < count; i++) {+ const s = vMix(vMix(inn[Math.floor(rnd() * m)], inn[Math.floor(rnd() * m)], rnd()), c, 0.16);+ const r = rnd(), kind = r < 0.5 ? 'rust' : r < 0.82 ? 'grime' : 'oil';+ kit.streak(h, s, 0.22 + rnd() * 0.55, 0.05 + rnd() * rnd() * 0.16, kind, true);+ }+ }+ if (y0 < 1.75 && !noDrip) {+ const e1 = vUnit(vSub(inn[1], inn[0])), e2 = vCross(h.n, e1), uvs = [], alphas = [];+ const u0 = 0.25 + rnd() * 0.5, v0 = 0.5;+ for (const p of inn) {+ const rel = vSub(p, c), su = Math.max(0, Math.min(1, u0 + vDot(rel, e1) / 1.6)), sv = Math.max(0, Math.min(1, v0 + vDot(rel, e2) / 0.8));+ uvs.push([su, DUST_V0 + sv * (1 - DUST_V0)]);+ alphas.push(Math.max(0, Math.min(1, (1.75 - T(p)[1]) / 1.1)) * 0.78);+ }+ decal(inn.map((p) => vAdd(p, vMul(h.n, 0.0025))), h.n, uvs, DUST_L, alphas, triangulate(inn, h.n));+ }+ };++ // A run down a plate along rest-pose gravity: 'rust', 'grime', 'oil' or 'soot'. Fades out by vertex alpha.+ kit.streak = (h, sIn, len, w, kind, fromTop) => {+ const dn = F ? [-F.u[1], -F.v[1], -F.w[1]] : [0, -1, 0];+ const flat = vSub(dn, vMul(h.n, vDot(dn, h.n)));+ if (vLen(flat) < 0.3) return;+ const d = vUnit(flat), inn = h.inner, m = inn.length, c = centroid(inn);+ const inside = (p) => inn.every((v, i) => vDot(vCross(vSub(inn[(i + 1) % m], v), vSub(p, v)), h.n) >= -1e-6);+ let s = sIn;+ if (fromTop && vDot(vSub(s, c), d) > 0) s = vSub(c, vSub(s, c));+ for (let k = 0; k < 6 && !inside(s); k++) s = vMix(s, c, 0.35);+ if (!inside(s)) return;+ let e = vAdd(s, vMul(d, len));+ for (let k = 0; k < 8 && !inside(e); k++) e = vMix(s, e, 0.75);+ if (vLen(vSub(e, s)) < 0.03) return;+ const sideDir = vCross(h.n, d);+ let hw = w / 2;+ for (let k = 0; k < 3 && !(inside(vAdd(s, vMul(sideDir, hw))) && inside(vSub(s, vMul(sideDir, hw)))); k++) hw *= 0.6;+ const side = vMul(sideDir, hw), lift = vMul(h.n, 0.003), A = vAdd(s, lift), B = vAdd(e, lift);+ const colIdx = kind === 'oil' ? 5 + Math.floor(rnd() * 3) : kind === 'soot' || kind === 'grime' ? 4 : Math.floor(rnd() * 4);+ const x0 = colIdx / 8 + 0.002, x1 = (colIdx + 1) / 8 - 0.002;+ const col = kind === 'oil' ? OIL : kind === 'soot' ? SOOT : kind === 'grime' ? GRIME : RUST_D;+ const al = kind === 'oil' ? 1 : kind === 'soot' ? 0.95 : 0.6 + rnd() * 0.3;+ decal([vSub(A, side), vSub(B, vMul(side, 0.6)), vAdd(B, vMul(side, 0.6)), vAdd(A, side)], h.n,+ [[x0, RUN_V0], [x0, RUN_V1], [x1, RUN_V1], [x1, RUN_V0]], col, [al, al * 0.22, al * 0.22, al]);+ };++ // Panel line: a dark groove with a thin lit lip, and grime settled into it, heavier on the low side.+ kit.seam = (h, a, b, w = 0.016, col = SEAM) => {+ const d = vUnit(vSub(b, a)), s = vMul(vCross(h.n, d), w / 2), l = vMul(h.n, 0.004);+ const A = vAdd(a, l), B = vAdd(b, l);+ emit([vSub(A, s), vSub(B, s), vAdd(B, s), vAdd(A, s)], h.n, col, QUAD);+ const lip = vMul(vUnit(s), 0.006), lit = vMix(h.col, WEAR, 0.3);+ emit([vAdd(A, s), vAdd(B, s), vAdd(vAdd(B, s), lip), vAdd(vAdd(A, s), lip)], h.n, lit, QUAD);+ if (!h.painted) return;+ const len = vLen(vSub(b, a));+ if (len < 0.05) return;+ const sd = vUnit(s), dn = F ? [-F.u[1], -F.v[1], -F.w[1]] : [0, -1, 0], shift = vMul(sd, vDot(sd, dn) >= 0 ? 0.012 : -0.012), half = vMul(sd, 0.05);+ const G0 = vAdd(vAdd(a, shift), vMul(h.n, 0.0032)), G1 = vAdd(vAdd(b, shift), vMul(h.n, 0.0032));+ const ul = Math.min(1, len), u0 = rnd() * (1 - ul);+ decal([vSub(G0, half), vSub(G1, half), vAdd(G1, half), vAdd(G0, half)], h.n,+ [[u0, GRIME_V0], [u0 + ul, GRIME_V0], [u0 + ul, GRIME_END], [u0, GRIME_END]], GRIME, [1, 1, 1, 1]);+ };+ kit.loop = (h, d = 0.05, w = 0.016) => {+ const pts = insetPoly(h.P, h.n, d);+ pts.forEach((p, i) => kit.seam(h, p, pts[(i + 1) % pts.length], w));+ };+ // Hex bolt head on the polished steel band. Larger heads get a bevel skirt so they catch a glint from any side.+ kit.bolt = (p, n, r = 0.022, col = GM) => {+ const b = bucket('steel'), base = b.pos.length / 3, e1 = vPerp(n), e2 = vCross(n, e1), an = TN(n);+ const rad = (i) => { const a = (i / 6) * 2 * Math.PI; return vAdd(vMul(e1, Math.cos(a)), vMul(e2, Math.sin(a))); };+ const domed = r >= 0.017, top = vAdd(p, vMul(n, domed ? 0.013 : 0.007));+ for (let i = 0; i < 6; i++) addV(b, T(vAdd(top, vMul(rad(i), r * (domed ? 0.6 : 1)))), an, col, (i + 0.5) / 6, domed ? POLISH_V : 0.5);+ for (let i = 1; i < 5; i++) tri(b, base, base + i, base + i + 1);+ if (domed) {+ const foot = b.pos.length / 3, bot = vAdd(p, vMul(n, 0.002));+ for (let i = 0; i < 6; i++) addV(b, T(vAdd(bot, vMul(rad(i), r))), TN(vUnit(vAdd(vMul(n, 0.5), vMul(rad(i), 0.87)))), tone(col, 0.72), (i + 0.5) / 6, POLISH_V);+ for (let i = 0; i < 6; i++) { const j = (i + 1) % 6; tri(b, foot + i, foot + j, base + j); tri(b, foot + i, base + j, base + i); }+ }+ };+ kit.bolts = (h, d = 0.05, each = 1, r = 0.02) => {+ const pts = insetPoly(h.P, h.n, d);+ pts.forEach((p, i) => {+ const q = pts[(i + 1) % pts.length];+ for (let k = 0; k < each; k++) {+ const at = vMix(p, q, k / each);+ kit.bolt(at, h.n, r);+ if (h.painted && rnd() < 0.36) kit.streak(h, at, 0.14 + rnd() * 0.3, 0.05 + rnd() * 0.06, 'rust');+ }+ });+ };+ kit.hazard = (h, sw = 0.09, skewK = 1) => {+ const q0 = h.inner[0], q1 = h.inner[1], q2 = h.inner[2], q3 = h.inner[3];+ const L = vLen(vSub(q1, q0)), W = vLen(vSub(q3, q0)), ns = Math.max(2, Math.round(L / (2 * sw))), ds = 1 / (2 * ns), skew = (W / L) * skewK;+ const lift = vMul(h.n, 0.004);+ const map = (s, t) => vAdd(vMix(vMix(q0, q1, s), vMix(q3, q2, s), t), lift);+ for (let k = -ns - 2; k < ns + 2; k++) {+ const s0 = k * 2 * ds + ds / 2;+ let poly2 = [[s0, 0], [s0 + ds, 0], [s0 + ds + skew, 1], [s0 + skew, 1]];+ for (const [lim, sign] of [[0, 1], [1, -1]]) {+ const out = [];+ for (let i = 0; i < poly2.length; i++) {+ const a = poly2[i], b = poly2[(i + 1) % poly2.length];+ const da = (a[0] - lim) * sign, db = (b[0] - lim) * sign;+ if (da >= 0) out.push(a);+ if (da * db < 0) { const u = da / (da - db); out.push([a[0] + (b[0] - a[0]) * u, a[1] + (b[1] - a[1]) * u]); }+ }+ poly2 = out;+ if (!poly2.length) break;+ }+ if (poly2.length >= 3) {+ const pts = poly2.map((p) => map(p[0], p[1]));+ const c = tone(HAZ_Y, 0.82 + rnd() * 0.24);+ const n = newell(pts);+ if (vDot(n, h.n) < 0) pts.reverse();+ emit(pts, h.n, c, triangulate(pts, h.n));+ }+ }+ };+ kit.hazardPlate = (pts, t, o = {}) => {+ const h = kit.plate(pts, t, { ...o, col: HAZ_K, edge: lin(0x77776c), noDrip: true });+ kit.hazard(h, o.sw || 0.09, o.skew === undefined ? 1 : o.skew);+ return h;+ };++ const SEG7 = { 0: 'abcdef', 1: 'bc', 2: 'abged', 3: 'abgcd', 4: 'fgbc', 5: 'afgcd', 6: 'afgedc', 7: 'abc', 8: 'abcdefg', 9: 'abcdfg', A: 'abcefg', E: 'afged', H: 'fgbec', K: 'fgecb', L: 'fed', M: 'abcef', R: 'abcefg', T: 'afe', '-': 'g' };+ kit.stencil = (o, rIn, u, str, hgt, col, nOut) => {+ const r = mir < 0 ? vMul(rIn, -1) : rIn, adv = hgt * 0.72, w = hgt * 0.5, th = hgt * 0.14;+ const start = mir < 0 ? vAdd(o, vMul(rIn, adv * str.length)) : o;+ const seg = (A, B, C, D) => {+ const lift = vMul(nOut, 0.005), pts = [A, B, C, D].map((p) => vAdd(p, lift));+ kit.poly(pts, col, { out: nOut });+ };+ [...str].forEach((ch, i) => {+ const org = vAdd(start, vMul(r, adv * i)), P = (x, y) => vAdd(org, vAdd(vMul(r, x * w), vMul(u, y * hgt)));+ const defs = { a: [0, 1, 1, 1], g: [0, 0.5, 1, 0.5], d: [0, 0, 1, 0], f: [0, 1, 0, 0.5], b: [1, 1, 1, 0.5], e: [0, 0.5, 0, 0], c: [1, 0.5, 1, 0] };+ for (const s of SEG7[ch] || '') {+ const [x0, y0, x1, y1] = defs[s], tx = th / w / 2, ty = th / hgt / 2;+ if (y0 === y1) seg(P(x0 - tx, y0 - ty), P(x1 + tx, y0 - ty), P(x1 + tx, y0 + ty), P(x0 - tx, y0 + ty));+ else seg(P(x0 - tx, y1 - ty), P(x0 + tx, y1 - ty), P(x0 + tx, y0 + ty), P(x0 - tx, y0 + ty));+ }+ });+ };++ // Revolved profile about an arbitrary axis; [d, r, col] entries, hard crease between bands.+ kit.lathe = (a, dirIn, prof, o = {}) => {+ const dir = vUnit(dirIn), seg = o.seg || 16, b = bucket(o.mat), col0 = o.col || GUN, isSteel = b.key === 'steel';+ const e1 = vPerp(dir), e2 = vCross(dir, e1);+ for (let i = 0; i < prof.length - 1; i++) {+ // [distance, radius, colour, polished?, colour at far end of this band]+ const [d0, r0, c0, pol, c1] = prof[i], [d1, r1] = prof[i + 1];+ if (r0 < 1e-4 && r1 < 1e-4) continue;+ const dd = d1 - d0, dr = r1 - r0, l = Math.hypot(dd, dr) || 1, base = b.pos.length / 3, colA = c0 || col0, colB = c1 || colA;+ const uK = Math.min(1, (2 * Math.PI * Math.max(r0, r1, 0.05)) / TILE), span = Math.min(1, Math.abs(dd) / TILE), v0 = rnd() * (1 - span);+ const vA = isSteel ? (pol ? POLISH_V : STEEL_V + v0 * (1 - STEEL_V)) : v0, vB = isSteel ? (pol ? POLISH_V : STEEL_V + (v0 + span) * (1 - STEEL_V)) : v0 + span;+ for (let k = 0; k <= seg; k++) {+ const th = (k / seg) * 2 * Math.PI, cs = Math.cos(th), sn = Math.sin(th);+ const rad = vAdd(vMul(e1, cs), vMul(e2, sn));+ const nn = vUnit(vAdd(vMul(rad, dd / l), vMul(dir, -dr / l)));+ const an = TN(nn), u = (k / seg) * uK;+ addV(b, T(vAdd(vAdd(a, vMul(dir, d0)), vMul(rad, r0))), an, colA, u, vA);+ addV(b, T(vAdd(vAdd(a, vMul(dir, d1)), vMul(rad, r1))), an, colB, u, vB);+ }+ for (let k = 0; k < seg; k++) {+ const i0 = base + k * 2, i1 = i0 + 1, i2 = i0 + 2, i3 = i0 + 3;+ if (r0 >= 1e-4 && r1 >= 1e-4) { tri(b, i0, i2, i3); tri(b, i0, i3, i1); }+ else if (r1 < 1e-4) tri(b, i0, i2, i1);+ else tri(b, i0, i3, i1);+ }+ }+ };+ kit.cyl = (a, c, r0, r1, o = {}) => {+ const L = vLen(vSub(c, a));+ kit.lathe(a, vSub(c, a), [[0, 0], [0, r0], [L, r1], [L, 0]], { seg: o.seg || 8, col: o.col, mat: o.mat });+ };++ const crPt = (p0, p1, p2, p3, t) => {+ const t2 = t * t, t3 = t2 * t, f = (i) => 0.5 * (2 * p1[i] + (p2[i] - p0[i]) * t + (2 * p0[i] - 5 * p1[i] + 4 * p2[i] - p3[i]) * t2 + (3 * p1[i] - p0[i] - 3 * p2[i] + p3[i]) * t3);+ return [f(0), f(1), f(2)];+ };+ kit.tube = (ctrl, r, o = {}) => {+ const sp = o.sp || 3, seg = o.seg || 6, col = o.col || RUB, b = bucket(o.mat || 'rubber');+ const P = [ctrl[0], ...ctrl, ctrl[ctrl.length - 1]], path = [];+ for (let i = 1; i < P.length - 2; i++) for (let k = 0; k < sp; k++) path.push(crPt(P[i - 1], P[i], P[i + 1], P[i + 2], k / sp));+ path.push(ctrl[ctrl.length - 1]);+ let nrm = vPerp(vUnit(vSub(path[1], path[0]))), acc = 0;+ const base = b.pos.length / 3;+ path.forEach((p, i) => {+ const t = vUnit(vSub(path[Math.min(i + 1, path.length - 1)], path[Math.max(i - 1, 0)]));+ nrm = vUnit(vSub(nrm, vMul(t, vDot(nrm, t))));+ const bn = vCross(t, nrm);+ if (i) acc += vLen(vSub(p, path[i - 1]));+ for (let k = 0; k <= seg; k++) {+ const th = (k / seg) * 2 * Math.PI, rad = vAdd(vMul(nrm, Math.cos(th)), vMul(bn, Math.sin(th)));+ addV(b, T(vAdd(p, vMul(rad, r))), TN(rad), col, k / seg, acc / TILE);+ }+ });+ const stride = seg + 1;+ for (let i = 0; i < path.length - 1; i++) for (let k = 0; k < seg; k++) {+ const a0 = base + i * stride + k, a1 = a0 + 1, c0 = a0 + stride, c1 = c0 + 1;+ tri(b, a0, a1, c1); tri(b, a0, c1, c0);+ }+ };+ kit.cables = (ctrl, spread, spec) => {+ spec.forEach((c, i) => {+ const off = (i - (spec.length - 1) / 2) * spread;+ const pts = ctrl.map((p, k) => (k === 0 || k === ctrl.length - 1 ? p : vAdd(p, [off * 0.7, off, 0])));+ kit.tube(pts, c.r, { col: c.col });+ });+ };++ kit.block = (c, s, o = {}) => {+ const ax = o.ax || [[1, 0, 0], [0, 1, 0], [0, 0, 1]], col = o.col || GUN, ch = o.c === undefined ? 0.012 : o.c, h = [s[0] / 2, s[1] / 2, s[2] / 2];+ const edge = o.edge || vMix(col, WEAR, 0.45), key = o.mat;+ const P = (x, y, z) => vAdd(c, vAdd(vAdd(vMul(ax[0], x), vMul(ax[1], y)), vMul(ax[2], z)));+ const q = (a, b, cc) => { const v = [0, 0, 0]; v[0] = a; v[1] = b; v[2] = cc; return P(v[0], v[1], v[2]); };+ for (let a = 0; a < 3; a++) for (const sg of [-1, 1]) {+ const b1 = (a + 1) % 3, b2 = (a + 2) % 3, pts = [];+ for (const [s1, s2] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) {+ const v = [0, 0, 0]; v[a] = sg * h[a]; v[b1] = s1 * (h[b1] - ch); v[b2] = s2 * (h[b2] - ch);+ pts.push(q(v[0], v[1], v[2]));+ }+ kit.poly(pts, col, { out: vMul(ax[a], sg), mat: key, polish: o.polish });+ }+ if (ch > 0) {+ for (let a = 0; a < 3; a++) {+ const b1 = (a + 1) % 3, b2 = (a + 2) % 3;+ for (const s1 of [-1, 1]) for (const s2 of [-1, 1]) {+ const V = (w, t1, t2) => { const v = [0, 0, 0]; v[a] = w * (h[a] - ch); v[b1] = s1 * (h[b1] - t1); v[b2] = s2 * (h[b2] - t2); return q(v[0], v[1], v[2]); };+ kit.poly([V(-1, 0, ch), V(1, 0, ch), V(1, ch, 0), V(-1, ch, 0)], edge, { out: vAdd(vMul(ax[b1], s1), vMul(ax[b2], s2)), mat: key, polish: o.polish });+ }+ }+ for (const sx of [-1, 1]) for (const sy of [-1, 1]) for (const sz of [-1, 1]) {+ kit.poly([P(sx * h[0], sy * (h[1] - ch), sz * (h[2] - ch)), P(sx * (h[0] - ch), sy * h[1], sz * (h[2] - ch)), P(sx * (h[0] - ch), sy * (h[1] - ch), sz * h[2])], edge, { out: vAdd(vAdd(vMul(ax[0], sx), vMul(ax[1], sy)), vMul(ax[2], sz)), mat: key, polish: o.polish });+ }+ }+ };+ kit.beam = (a, b, s1, s2, o = {}) => {+ const u = vUnit(vSub(b, a)), up = o.up || (Math.abs(u[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]), w = vUnit(vCross(u, up)), v = vCross(w, u);+ kit.block(vMix(a, b, 0.5), [vLen(vSub(b, a)), s1, s2], { ...o, ax: [u, v, w] });+ };++ kit.loft = (ringsIn, o = {}) => {+ let rings = ringsIn;+ const d = vSub(centroid(rings[1]), centroid(rings[0]));+ if (vDot(newell(rings[0]), d) < 0) rings = rings.map((r) => [...r].reverse());+ const col = o.col || DARK;+ for (let i = 0; i < rings.length - 1; i++) {+ const A = rings[i], B = rings[i + 1], m = A.length;+ for (let j = 0; j < m; j++) {+ const k = (j + 1) % m, q = [A[j], A[k], B[k], B[j]];+ if (vLen(vSub(A[j], A[k])) < 1e-5 && vLen(vSub(B[j], B[k])) < 1e-5) continue;+ emit(q, newell(q), o.cols ? o.cols[i] || col : col, [[0, 1, 2], [0, 2, 3]], o.mat);+ }+ }+ if (o.capStart !== false) { const r = rings[0], n = vMul(newell(r), -1), R = [...r].reverse(); emit(R, n, col, triangulate(R, n), o.mat); }+ if (o.capEnd !== false) { const r = rings[rings.length - 1], n = newell(r); emit(r, n, col, triangulate(r, n), o.mat); }+ };++ // Machined joint drum in gunmetal: recessed face, boss, polished chamfers that take a sharp highlight.+ kit.drum = (c, axisIn, r, w, o = {}) => {+ const axis = vUnit(axisIn), seg = o.seg || Math.max(16, Math.round(r * 64)), a = vAdd(c, vMul(axis, -w / 2));+ const body = o.col || GM, rec = o.rec || GM_D, boss = o.boss || GM_L;+ const rim = Math.min(0.02, w * 0.12), dep = Math.min(0.03, w * 0.1), key = o.mat || 'steel';+ kit.lathe(a, axis, [+ [0, 0, boss], [0, r * 0.25, GM_H, 1], [dep * 0.3, r * 0.30, rec], [dep, r * 0.30, rec], [dep, r * 0.74, body], [0, r * 0.80, body], [0, r * 0.94, GM_H, 1], [rim, r, body],+ [w - rim, r, GM_H, 1], [w, r * 0.94, body], [w, r * 0.80, body], [w - dep, r * 0.74, rec], [w - dep, r * 0.30, boss], [w, r * 0.30, boss], [w, 0, boss],+ ], { seg, mat: key, polish: o.polish });+ const prev = matKey; matKey = key;+ const e1 = vPerp(axis), e2 = vCross(axis, e1);+ const nb = o.bolts === undefined ? 8 : o.bolts;+ // Bolts on the +axis face only: every drum's other face is buried in the limb.+ for (let i = 0; i < nb; i++) {+ const th = (i / nb) * 2 * Math.PI, rad = vAdd(vMul(e1, Math.cos(th)), vMul(e2, Math.sin(th)));+ kit.bolt(vAdd(vAdd(c, vMul(axis, w / 2 - dep)), vMul(rad, r * 0.53)), axis, Math.min(0.024, r * 0.09), GM_L);+ }+ if (o.gear) {+ const tw = ((2 * Math.PI * r) / o.gear) * 0.5;+ for (let i = 0; i < o.gear; i++) {+ const th = (i / o.gear) * 2 * Math.PI, rad = vAdd(vMul(e1, Math.cos(th)), vMul(e2, Math.sin(th))), tan = vCross(axis, rad);+ kit.block(vAdd(c, vMul(rad, r + 0.012)), [0.034, tw, w * (o.gearW || 0.5)], { ax: [rad, tan, axis], c: 0, col: GM_L, polish: true, mat: key, polish: o.polish });+ }+ }+ matKey = prev;+ };++ kit.vent = (q0, q1, q2, q3, o = {}) => {+ const n = newell([q0, q1, q2, q3]), N = o.n || 5, slat = o.col || GUN;+ kit.poly([q0, q1, q2, q3], o.base || tone(GUN, 0.25), { out: n, mat: o.glow });+ const lift = vMul(n, o.depth || 0.022);+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.08) / N, t1 = (i + 0.72) / N;+ const a = vAdd(vMix(q0, q3, t0), lift), b = vAdd(vMix(q1, q2, t0), lift), c = vMix(q1, q2, t1), d = vMix(q0, q3, t1);+ kit.poly([a, b, vAdd(c, vMul(lift, 0.25)), vAdd(d, vMul(lift, 0.25))], [slat, slat, vMix(slat, WEAR, 0.3), vMix(slat, WEAR, 0.3)], {});+ }+ };+++ // Louvre vent set into a quad plate, addressed in the plate's own s,t.+ kit.ventOn = (h, s0, s1, t0, t1, o = {}) => {+ const l = 0.004;+ let q = [h.at(s0, t0, l), h.at(s1, t0, l), h.at(s1, t1, l), h.at(s0, t1, l)];+ if (vDot(newell(q), h.n) < 0) q = [q[1], q[0], q[3], q[2]];+ kit.vent(q[0], q[1], q[2], q[3], o);+ q.forEach((p, i) => kit.seam({ ...h, painted: false }, p, q[(i + 1) % 4], 0.012));+ };++ // Raised sub-plate on a quad plate, addressed in its s,t. Keeps the layering consistent wherever it is used.+ kit.onPlate = (h, s0, s1, t0, t1, lift, o = {}) => {+ const q = [h.at(s0, t0, lift), h.at(s1, t0, lift), h.at(s1, t1, lift), h.at(s0, t1, lift)];+ return kit.plate(q, lift + 0.012, { out: h.n, noBack: true, ...o });+ };++ // Thin heat fins standing off a quad plate.+ kit.fins = (h, s0, s1, t0, t1, count, stand = 0.05, col = GM) => {+ const prev = matKey; matKey = 'steel';+ const A = h.at(s0, t0), B = h.at(s1, t0), Cc = h.at(s0, t1), run = vUnit(vSub(B, A)), acr = vSub(Cc, A), lenS = vLen(vSub(B, A)), lenT = vLen(acr), across = vUnit(acr);+ for (let i = 0; i < count; i++) {+ const c = vAdd(vAdd(vMix(A, B, (i + 0.5) / count), vMul(acr, 0.5)), vMul(h.n, stand / 2));+ kit.block(c, [0.012, lenT, stand], { ax: [run, across, h.n], c: 0, col, polish: i % 2 === 0 });+ }+ matKey = prev;+ return lenS;+ };++ // Faceted armour shell over a list of sections: a dark core, then a bevelled plate per facet with a small gap+ // at each station. Section: { u, vf, vr, wo, wi, c } in the current frame. Returns { out, rear, in, front } per span.+ kit.shell = (st, o = {}) => {+ const ring = (s) => [[s.u, s.vf, s.wo - s.c], [s.u, s.vf - s.c, s.wo], [s.u, s.vr + s.c, s.wo], [s.u, s.vr, s.wo - s.c], [s.u, s.vr, -s.wi + s.c], [s.u, s.vr + s.c, -s.wi], [s.u, s.vf - s.c, -s.wi], [s.u, s.vf, -s.wi + s.c]];+ kit.loft(st.map(ring), { col: o.core || DARK });+ const names = { 1: 'out', 3: 'rear', 5: 'in', 7: 'front' }, t = o.t || 0.03, gap = o.gap || 0.012, cols = o.cols || {}, out = [];+ for (let i = 0; i < st.length - 1; i++) {+ const A = ring(st[i]), B = ring(st[i + 1]), g = gap / Math.max(0.05, Math.abs(st[i + 1].u - st[i].u)), sec = {};+ const ax = [(st[i].u + st[i + 1].u) / 2, (st[i].vf + st[i].vr + st[i + 1].vf + st[i + 1].vr) / 4, (st[i].wo - st[i].wi + st[i + 1].wo - st[i + 1].wi) / 4];+ for (let j = 0; j < 8; j++) {+ const k = (j + 1) % 8, Q0 = [vMix(A[j], B[j], g), vMix(A[k], B[k], g), vMix(A[k], B[k], 1 - g), vMix(A[j], B[j], 1 - g)];+ const fo = vSub(centroid(Q0), ax), nn = newell(Q0), lift = vMul(nn, (vDot(nn, fo) < 0 ? -1 : 1) * t), Q = Q0.map((p) => vAdd(p, lift));+ if (names[j]) sec[names[j]] = kit.plate(Q, t + 0.012, { out: fo, col: cols[names[j]] || PAINT, noBack: true });+ else kit.plate(Q, t + 0.012, { out: fo, col: cols.chamfer || PAINT2, noBack: true, c: 0.01, wear: 0.6, noDrip: true });+ }+ out.push(sec);+ }+ return out;+ };+ // Clevis ears either side of a piston pin, from the anchor back to the bone surface.+ kit.lug = (A, base, zc, o = {}) => {+ const w = o.w || 0.085, ear = o.ear || 0.085, foot = o.foot || 0.12, t = o.t || 0.03;+ const d = vUnit(vSub(base, A)), p = [-d[1], d[0], 0];+ const pts = (z) => [+ [A[0] + p[0] * ear, A[1] + p[1] * ear, z], [A[0] - d[0] * ear, A[1] - d[1] * ear, z], [A[0] - p[0] * ear, A[1] - p[1] * ear, z],+ [base[0] - p[0] * foot, base[1] - p[1] * foot, z], [base[0] + p[0] * foot, base[1] + p[1] * foot, z],+ ];+ const col = o.col || GUN;+ kit.plate(pts(zc + w), t, { out: [0, 0, 1], col, c: 0.012 });+ kit.plate(pts(zc - w), t, { out: [0, 0, -1], col, c: 0.012 });+ };++ kit.piston = (name, gA, A, gB, B, o = {}) => { pistons.push({ name, gA, A, gB, B, mir, o, F }); };+ kit.pistonList = () => pistons;++ kit.buildPiston = (p, minSpan, maxSpan) => {+ const { name, gA, A, gB, B, o } = p;+ mir = p.mir; F = null;+ const sgn = mir;+ const actual = (q) => [q[0], q[1], q[2] * sgn];+ const cylG = kit.group('Piston_' + name + '_Cyl', gA, actual(A), 'Piston_' + name + '_Cyl');+ const rodG = kit.group('Piston_' + name + '_Rod', gB, actual(B), 'Piston_' + name + '_Rod');+ const dir = vUnit(vSub(B, A)), back = vMul(dir, -1);+ const r = o.r || 0.09, rr = o.rod || r * 0.56;+ // Equal cylinder and rod: the rod stays inserted past full Walk extension, the gland clears the rod end at its shortest.+ const Lc = Math.max(0.2, Math.min((maxSpan + Math.max(0.18, 0.2 * maxSpan)) / 2, minSpan - 0.05)), Lr = Lc;+ const zAxis = [0, 0, 1], body = PISTON;+ // Cylinder+ kit.use(cylG); kit.mat('steel');+ kit.drum(A, zAxis, r * 0.72, 0.13, { bolts: 0, seg: 12, boss: STEEL });+ const col1 = vMix(body, WEAR, 0.5);+ kit.lathe(A, dir, [+ [r * 0.5, 0, GM_D], [r * 0.5, r * 1.16, GM], [r * 1.5, r * 1.16, GM_H, 1], [r * 1.65, r, body],+ [Lc - r * 1.3, r, GM_D], [Lc - r * 1.3, r * 1.14, GM], [Lc - r * 0.35, r * 1.14, GM_H, 1], [Lc, r * 0.75, GM_L], [Lc, 0, GM_L],+ ], { seg: o.seg || 14 });+ const s2 = vUnit(vCross(dir, [0, 0, 1]));+ const hoseOff = vMul(s2, r + 0.016);+ const hp = (d) => vAdd(vAdd(A, vMul(dir, d)), hoseOff);+ kit.tube([vAdd(A, vMul(dir, r * 1.2)), hp(r * 1.7), hp(Lc * 0.5), hp(Lc - r * 1.6), vAdd(A, vMul(dir, Lc - r * 0.9))], Math.max(0.011, r * 0.14), { col: o.hose || CAB_K, sp: 2, seg: 5 });+ // Rod+ kit.use(rodG); kit.mat('steel');+ kit.drum(B, zAxis, rr * 1.16, 0.12, { bolts: 0, seg: 12, boss: STEEL });+ kit.lathe(B, back, [[rr * 0.8, 0, STEEL_D], [rr * 0.8, rr * 1.24, STEEL_D], [rr * 2.1, rr * 1.24, STEEL_D], [rr * 2.3, rr, STEEL_D], [rr * 2.3, 0, STEEL_D]], { seg: 12 });+ kit.mat('chrome');+ kit.lathe(B, back, [[rr * 2.3, rr], [Lr, rr], [Lr, 0]], { seg: o.seg || 12, mat: 'chrome' });+ // Aim constraint data, usable from runtime code as well as by the baked clip.+ const dA = actual(dir);+ cylG.node.userData = { aimAt: rodG.node.name, aimRestDir: dA };+ rodG.node.userData = { aimAt: cylG.node.name, aimRestDir: vMul(dA, -1) };+ };++ kit.flush = () => {+ for (const g of groups) {+ for (const key of Object.keys(g.b)) {+ const b = g.b[key];+ if (!b.idx.length) continue;+ const n = b.pos.length / 3, pos = new Float32Array(n * 3);+ for (let i = 0; i < n; i++) {+ pos[i * 3] = b.pos[i * 3] - g.origin[0]; pos[i * 3 + 1] = b.pos[i * 3 + 1] - g.origin[1]; pos[i * 3 + 2] = b.pos[i * 3 + 2] - g.origin[2];+ }+ const geo = new THREE.BufferGeometry();+ geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));+ geo.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(b.nor), 3));+ // 16-bit normalised UVs, and none at all on untextured materials: most of the file is vertex data.+ if (TEXTURED[key]) {+ const uv16 = new Uint16Array(b.uv.length);+ for (let i = 0; i < b.uv.length; i++) uv16[i] = Math.round(Math.max(0, Math.min(1, b.uv[i])) * 65535);+ geo.setAttribute('uv', new THREE.BufferAttribute(uv16, 2, true));+ }+ if (COLORED[key]) geo.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(b.col), 4, true));+ geo.setIndex(b.idx);+ geo.computeBoundingBox(); geo.computeBoundingSphere();+ createPart(g.label + '_' + key, geo, MAT[key], { parent: g.node });+ }+ }+ };+ return kit;+}++// ---------------------------------------------------------------- textures+function makeTextures() {+ const prng = (s0) => { let s = s0 >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; };+ const rand = prng(7);++ // ---- Paint: broad sun-fade, mottling and rub marks, deliberately no spots. Chips live in the wear atlas.+ const fades = [];+ for (let i = 0; i < 12; i++) {+ const w = 90 + Math.round(rand() * 170), h = 70 + Math.round(rand() * 150), x = Math.round(rand() * (512 - w)), y = Math.round(rand() * (512 - h)), light = rand() < 0.5;+ fades.push({ op: 'paint', shape: { kind: 'ellipse', x, y, width: w, height: h }, rgba: light ? [255, 255, 250, 255] : [118, 114, 102, 255], opacity: light ? 28 : 24 });+ }+ const strokes = (n, wMin, wMax, lMin, lMax) => {+ const out = [];+ for (let i = 0; i < n; i++) {+ const x = 20 + Math.round(rand() * 470), y = 20 + Math.round(rand() * 470), a = rand() * 6.28, l = lMin + rand() * (lMax - lMin);+ out.push({ kind: 'line', from: [x, y], to: [Math.max(0, Math.min(511, Math.round(x + Math.cos(a) * l))), Math.max(0, Math.min(511, Math.round(y + Math.sin(a) * l)))], width: wMin + Math.round(rand() * (wMax - wMin)) });+ }+ return out;+ };+ const rubs = strokes(10, 4, 14, 30, 110), scratches = strokes(14, 1, 1, 30, 150);+ const ramp = [];+ for (let i = 0; i < 16; i++) { const k = 148 + i * 7; ramp.push((k << 16) | (k << 8) | Math.round(k * 0.972)); }+ const albedo = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'albedo', name: 'Armour_Paint_Albedo',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 512, usage: 'albedo', layers: [+ { op: 'solid', color: 0xf4f4f0 },+ { op: 'noise', colorA: 0x9e9c90, colorB: 0xffffff, scale: 4, octaves: 4, seed: 41, blend: 'multiply', opacity: 0.95 },+ { op: 'noise', colorA: 0xd2d0c6, colorB: 0xffffff, scale: 30, octaves: 2, seed: 77, blend: 'multiply', opacity: 0.6 },+ ] } },+ ...fades,+ ...rubs.map((s) => ({ op: 'paint', shape: s, rgba: [255, 255, 250, 255], opacity: 34 })),+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [255, 255, 250, 255], opacity: 80 })),+ ],+ palette: { colors: ramp, dither: 'none' },+ });+ const mr = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'metallicRoughness', name: 'Armour_Paint_MR',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 512, usage: 'metallicRoughness', layers: [+ { op: 'solid', color: 0xffa80c },+ { op: 'noise', colorA: 0xff8808, colorB: 0xffc410, scale: 5, octaves: 2, seed: 41 },+ ] } },+ ...rubs.map((s) => ({ op: 'paint', shape: s, rgba: [255, 118, 40, 255], opacity: 90 })),+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [255, 110, 120, 255] })),+ ],+ });+ const seamOps = [];+ const H = (y, x0, x1) => { seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y, width: x1 - x0, height: 2 }, rgba: [128, 72, 235, 255] }); seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y + 2, width: x1 - x0, height: 2 }, rgba: [128, 184, 235, 255] }); };+ const Vt = (x, y0, y1) => { seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x, y: y0, width: 2, height: y1 - y0 }, rgba: [72, 128, 235, 255] }); seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x + 2, y: y0, width: 2, height: y1 - y0 }, rgba: [184, 128, 235, 255] }); };+ H(70, 0, 512); H(255, 60, 400); H(410, 0, 512); Vt(90, 70, 410); Vt(330, 0, 255); Vt(460, 255, 512);+ const area = { x: 24, y: 24, width: 464, height: 464 };+ const nrm = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'normal', name: 'Armour_Normal', background: [128, 128, 255, 255],+ operations: [+ ...seamOps,+ { op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 26, height: 18 }, rgba: [112, 144, 246, 255], count: 24, seed: 55, area },+ { op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 6, height: 6 }, rgba: [150, 150, 245, 255], count: 90, seed: 56, area },+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [104, 152, 248, 255] })),+ ],+ });++ // ---- Gunmetal: real metal with brushed variation; the top band is the polished finish for chamfers and bolts.+ const steelMR = proceduralTexture({+ schemaVersion: 3, width: 256, height: 256, usage: 'metallicRoughness', name: 'Gunmetal_MR',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 256, usage: 'metallicRoughness', layers: [+ { op: 'solid', color: 0xff60be },+ { op: 'noise', colorA: 0xff50b4, colorB: 0xff78c8, scale: 9, octaves: 2, seed: 3 },+ { op: 'stripes', colorA: 0xff58ba, colorB: 0xff6cc2, count: 48, angleDeg: 90, opacity: 0.5 },+ ] } },+ { op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: 256, height: 28 }, rgba: [255, 34, 226, 255] },+ ],+ });++ // ---- Wear atlas. Rows, in texture V: chipped edge, panel grime, eight run columns (rust x5, oil x3), dust.+ const chipShape = (w, h) => ({ kind: 'polygon', points: [[0, Math.round(h * 0.4)], [Math.round(w * 0.3), 0], [Math.round(w * 0.75), Math.round(h * 0.1)], [w, Math.round(h * 0.5)], [Math.round(w * 0.7), h], [Math.round(w * 0.25), Math.round(h * 0.85)]] });+ // Chipped band: the straight edge is the plate edge (y = 0), the jagged side bites into the paint in clusters.+ const jag = (seed, maxD) => {+ const r = prng(seed), p1 = r() * 6.28, p2 = r() * 6.28, p3 = r() * 6.28, pts = [[0, 0]], n = 60;+ for (let i = 0; i <= n; i++) {+ const x = Math.max(0, Math.min(512, Math.round(((i + (i > 0 && i < n ? (r() - 0.5) * 0.8 : 0)) / n) * 512))), th = (x / 512) * 6.283;+ const env = Math.max(0, 0.5 * Math.sin(th * 3 + p1) + 0.35 * Math.sin(th * 7 + p2) + 0.18 * Math.sin(th * 13 + p3) + 0.36);+ const q = r(), tooth = 0.22 + 0.95 * q * Math.sqrt(q);+ pts.push([x, Math.round(maxD * Math.min(1, env) * tooth)]);+ }+ pts.push([512, 0]);+ return { kind: 'polygon', points: pts };+ };+ const band = (seed, cy, half) => {+ const r = prng(seed), n = 30, top = [], bot = [];+ for (let i = 0; i <= n; i++) {+ const x = Math.round((i / n) * 512);+ top.push([x, Math.round(cy - half * (0.45 + 0.55 * r()))]);+ bot.push([x, Math.round(cy + half * (0.45 + 0.55 * r()))]);+ }+ return { kind: 'polygon', points: [...top, ...bot.reverse()] };+ };+ const taper = (r, cx, y0, len, w0) => {+ const L = [], R = [], n = 8;+ for (let i = 0; i <= n; i++) {+ const t = i / n, w = w0 * (0.4 + 0.6 * Math.min(1, t * 4)) * Math.pow(1 - t, 0.7) + 1, y = Math.round(y0 + t * len), j = (r() - 0.5) * w0 * 0.2;+ L.push([Math.round(cx - w / 2 + j), y]); R.push([Math.round(cx + w / 2 + j), y]);+ }+ return { kind: 'polygon', points: [...L, ...R.reverse()] };+ };+ const WHITE8 = [255, 255, 255, 255], METAL8 = [164, 168, 172, 255], PRIMER8 = [84, 78, 66, 255], fleck = { x: 4, y: 2, width: 500, height: 66 };+ const wearOps = [+ { op: 'paint', shape: jag(11, 90), rgba: PRIMER8 },+ { op: 'paint', shape: jag(11, 66), rgba: METAL8 },+ { op: 'stamps', shape: chipShape(6, 4), rgba: METAL8, count: 34, seed: 41, area: { x: 4, y: 2, width: 500, height: 34 } },+ { op: 'stamps', shape: chipShape(13, 9), rgba: PRIMER8, count: 30, seed: 42, area: fleck },+ { op: 'stamps', shape: chipShape(9, 6), rgba: METAL8, count: 30, seed: 42, area: fleck },+ { op: 'paint', shape: band(21, 160, 30), rgba: WHITE8, opacity: 76 },+ { op: 'paint', shape: band(22, 160, 18), rgba: WHITE8, opacity: 110 },+ { op: 'paint', shape: band(23, 160, 8), rgba: WHITE8, opacity: 180 },+ ];+ const r2 = prng(91);+ for (let c = 0; c < 8; c++) {+ const cx = 32 + c * 64, oil = c >= 5;+ wearOps.push({ op: 'paint', shape: taper(r2, cx, 192, 180 + r2() * 70, 22 + r2() * 18), rgba: WHITE8, opacity: oil ? 205 : 165 });+ wearOps.push({ op: 'paint', shape: taper(r2, cx + (r2() - 0.5) * 8, 192, 90 + r2() * 70, 8 + r2() * 8), rgba: WHITE8, opacity: oil ? 225 : 185 });+ for (let k = 0; k < 2; k++) {+ const ox = Math.round(cx + (r2() - 0.5) * 34);+ wearOps.push({ op: 'paint', shape: { kind: 'line', from: [ox, 192], to: [Math.round(ox + (r2() - 0.5) * 8), Math.round(300 + r2() * 145)], width: 1 + Math.round(r2() * 2) }, rgba: WHITE8, opacity: oil ? 235 : 195 });+ }+ }+ wearOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 448, width: 512, height: 64 }, rgba: WHITE8, opacity: 58 });+ // This row is 4:1 stretched on the mech, so the grit is drawn wide and flat to land round.+ wearOps.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 6, height: 2 }, rgba: WHITE8, count: 90, seed: 61, area: { x: 2, y: 450, width: 504, height: 58 }, opacity: 90 });+ wearOps.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 3, height: 1 }, rgba: WHITE8, count: 30, seed: 62, area: { x: 2, y: 450, width: 504, height: 58 }, opacity: 120 });+ const wearA = proceduralTexture({ schemaVersion: 3, width: 512, height: 512, usage: 'albedo', name: 'Wear_Atlas', operations: wearOps });+ // Bare metal in the chip row and glossy oil in the oil columns; matte everywhere else.+ const wearMR = proceduralTexture({+ schemaVersion: 3, width: 64, height: 64, usage: 'metallicRoughness', name: 'Wear_Atlas_MR', background: [255, 216, 0, 255],+ operations: [+ { op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: 64, height: 13 }, rgba: [255, 84, 165, 255] },+ { op: 'paint', shape: { kind: 'rectangle', x: 40, y: 24, width: 24, height: 32 }, rgba: [255, 44, 26, 255] },+ ],+ });+ return { albedo, mr, nrm, steelMR, wearA, wearMR };+}++// ---------------------------------------------------------------- legs+function buildLeg(kit, side, pelvisG) {+ const z0 = SK.legZ, s = side === 'L' ? -1 : 1;+ kit.mirror(s);+ const hipG = kit.group('Hip_' + side, pelvisG, [SK.hip[0], SK.hip[1], s * z0], 'Thigh_' + side);+ const kneeG = kit.group('Knee_' + side, hipG, [SK.knee[0], SK.knee[1], s * z0], 'Shin_' + side);+ const ankleG = kit.group('Ankle_' + side, kneeG, [SK.ankle[0], SK.ankle[1], s * z0], 'Meta_' + side);+ const toeG = kit.group('Toe_' + side, ankleG, [SK.toe[0], SK.toe[1], s * z0], 'Foot_' + side);+ [hipG, kneeG, ankleG, toeG].forEach((g) => { g.node.userData = { hinge: g === hipG ? 'ball (z=flex)' : g === toeG ? 'ball (z=flex)' : 'z' }; });++ const thF = kit.bone(SK.hip, SK.knee, z0), shF = kit.bone(SK.knee, SK.ankle, z0), mtF = kit.bone(SK.ankle, SK.toe, z0);+ const ring = (u, vf, vr, wh) => [[u, vf, wh], [u, vf, -wh], [u, vr, -wh], [u, vr, wh]];+ const sideP = (w, pts, t, o = {}) => kit.plate(pts.map((p) => [p[0], p[1], w]), t, { out: [0, 0, Math.sign(w)], ...o });++ // ---- Thigh+ kit.use(hipG); kit.frame(thF); kit.hull([thF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.40, 0.56, { bolts: 10, gear: 26, gearW: 0.4 });+ kit.drum([0, 0, 0.30], [0, 0, 1], 0.16, 0.08, { bolts: 0, seg: 14, col: DARK });+ kit.mat('armour');+ kit.loft([ring(-0.05, 0.26, -0.28, 0.24), ring(0.40, 0.30, -0.30, 0.25), ring(1.0, 0.22, -0.24, 0.21), ring(thF.L + 0.02, 0.15, -0.15, 0.15)], { col: DARK });+ const t1 = sideP(0.29, [[0.34, -0.28], [1.02, -0.23], [1.22, -0.10], [1.22, 0.10], [1.02, 0.22], [0.40, 0.30], [0.30, 0.14], [0.30, -0.12]], 0.05);+ kit.loop(t1, 0.045); kit.bolts(t1, 0.03, 1, 0.016);+ kit.streak(t1, [0.36, 0.04, 0.29], 0.66, 0.12, 'oil'); kit.streak(t1, [0.40, -0.16, 0.29], 0.42, 0.06, 'rust');+ const t2 = sideP(0.325, [[0.50, -0.20], [0.94, -0.16], [0.94, 0.16], [0.50, 0.21]], 0.035, { col: PAINT2 });+ kit.bolts(t2, 0.03);+ kit.seam(t2, [0.74, -0.18, 0.325], [0.74, 0.18, 0.325]);+ kit.stencil([0.67, -0.05, 0.325], [0, 1, 0], [-1, 0, 0], '07', 0.13, BONE, [0, 0, 1]);+ const t3 = kit.hazardPlate([[1.00, -0.19, 0.325], [1.00, 0.19, 0.325], [1.17, 0.09, 0.325], [1.17, -0.09, 0.325]], 0.03, { out: [0, 0, 1], sw: 0.05 });+ kit.seam(t3, t3.inner[0], t3.inner[1], 0.008);+ const t4 = sideP(-0.28, [[0.34, -0.27], [1.05, -0.20], [1.18, -0.08], [1.18, 0.08], [1.05, 0.20], [0.40, 0.29], [0.32, 0.12], [0.32, -0.12]], 0.04, { col: PAINT2 });+ kit.loop(t4, 0.05);+ const tf = kit.plate([[0.38, 0.335, 0.235], [1.04, 0.245, 0.185], [1.04, 0.245, -0.185], [0.38, 0.335, -0.235]], 0.05, { out: [0, 1, 0] });+ kit.seam(tf, [0.62, 0.30, 0.22], [0.62, 0.30, -0.22]); kit.seam(tf, [0.84, 0.27, 0.20], [0.84, 0.27, -0.20]);+ kit.bolts(tf, 0.03, 1, 0.016);+ const tb = kit.plate([[0.36, -0.315, 0.235], [1.10, -0.255, 0.185], [1.10, -0.255, -0.185], [0.36, -0.315, -0.235]], 0.04, { out: [0, -1, 0], col: GUN });+ kit.loop(tb, 0.05);+ // Servo pack on the outer rear of the thigh+ kit.mat('steel');+ kit.cyl([0.46, -0.205, 0.40], [0.86, -0.17, 0.40], 0.066, 0.066, { seg: 10, col: STEEL_D });+ for (const u of [0.52, 0.60, 0.68, 0.76]) kit.lathe([u, -0.205 + (u - 0.46) * 0.09, 0.40], [1, 0.09, 0], [[0, 0.07], [0, 0.082, GUN], [0.018, 0.082, GUN], [0.018, 0.07]], { seg: 10, col: GUN });+ kit.mat('rubber');+ kit.cables([[0, 0, 0.345], [0.30, 0.10, 0.37], [0.70, 0.235, 0.385], [1.05, 0.215, 0.375], [1.30, 0.08, 0.345], [thF.L, 0, 0.32]], 0.026, [{ r: 0.019, col: CAB_K }, { r: 0.015, col: CAB_R }, { r: 0.019, col: CAB_K }]);+ kit.mat('armour');++ const hipLugT = kit.at(thF, 0.62, -0.42, 0), hipLugB = kit.at(thF, 0.70, -0.27, 0);+ const kneeA = kit.at(thF, 0.30, 0.50, 0), kneeAb = kit.at(thF, 0.42, 0.31, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hipLugT, hipLugB, z0, { col: GUN });+ kit.lug(kneeA, kneeAb, z0, { col: GUN, ear: 0.10, w: 0.09, foot: 0.15 });+ kit.frame(thF); kit.hull([thF.L / 2, 0, 0]);++ // ---- Shin+ kit.use(kneeG); kit.frame(shF); kit.hull([shF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.30, 0.56, { bolts: 8, gear: 22, gearW: 0.4 });+ kit.drum([0, 0, 0.30], [0, 0, 1], 0.13, 0.07, { bolts: 0, seg: 12, col: DARK });+ kit.mat('armour');+ kit.loft([ring(0.05, 0.25, -0.21, 0.23), ring(0.70, 0.25, -0.19, 0.22), ring(1.25, 0.19, -0.16, 0.19), ring(shF.L + 0.02, 0.13, -0.13, 0.14)], { col: DARK });+ const s1 = sideP(0.275, [[0.24, -0.20], [1.28, -0.17], [1.50, -0.10], [1.50, 0.11], [1.30, 0.19], [0.58, 0.27], [0.26, 0.24]], 0.05);+ kit.loop(s1, 0.045); kit.bolts(s1, 0.03, 1, 0.016);+ kit.streak(s1, [0.30, 0.02, 0.275], 0.7, 0.14, 'oil'); kit.streak(s1, [0.48, -0.12, 0.275], 0.5, 0.07, 'rust');+ const s2 = sideP(0.31, [[0.42, -0.15], [1.05, -0.13], [1.05, 0.15], [0.58, 0.20], [0.42, 0.17]], 0.035, { col: PAINT2 });+ kit.bolts(s2, 0.03); kit.seam(s2, [0.76, -0.13, 0.31], [0.76, 0.17, 0.31]);+ const s2b = sideP(0.31, [[1.10, -0.13], [1.40, -0.09], [1.40, 0.09], [1.10, 0.14]], 0.03, { col: RUST });+ kit.bolts(s2b, 0.025, 1, 0.014);+ sideP(-0.27, [[0.24, -0.19], [1.30, -0.16], [1.50, -0.09], [1.50, 0.10], [1.30, 0.18], [0.58, 0.25], [0.26, 0.22]], 0.04, { col: PAINT2 });+ const sg = kit.plate([[0.34, 0.33, 0.24], [1.38, 0.22, 0.185], [1.38, 0.22, -0.185], [0.34, 0.33, -0.24]], 0.055, { out: [0, 1, 0] });+ kit.seam(sg, [0.70, 0.295, 0.22], [0.70, 0.295, -0.22]); kit.seam(sg, [1.05, 0.26, 0.20], [1.05, 0.26, -0.20]); kit.bolts(sg, 0.03, 2, 0.016);+ const sg2 = kit.plate([[0.74, 0.32, 0.12], [1.02, 0.29, 0.11], [1.02, 0.29, -0.11], [0.74, 0.32, -0.12]], 0.035, { out: [0, 1, 0], col: PAINT2 });+ kit.bolts(sg2, 0.02, 1, 0.012);+ kit.plate([[0.30, -0.235, 0.22], [1.30, -0.185, 0.18], [1.30, -0.185, -0.18], [0.30, -0.235, -0.22]], 0.04, { out: [0, -1, 0], col: GUN });+ // Knee guard and piston horn+ const kg = kit.plate([[-0.46, 0.235, 0.27], [0.32, 0.415, 0.285], [0.32, 0.415, -0.285], [-0.46, 0.235, -0.27]], 0.06, { out: [0, 1, 0], col: PAINT });+ kit.loop(kg, 0.05); kit.bolts(kg, 0.03, 1, 0.018);+ kit.hazardPlate([[-0.34, 0.325, 0.17], [-0.34, 0.325, -0.17], [0.06, 0.42, -0.17], [0.06, 0.42, 0.17]], 0.04, { out: [0, 1, 0], sw: 0.05 });+ sideP(0.245, [[-0.46, 0.02], [-0.46, 0.24], [0.20, 0.40], [0.22, 0.20], [-0.10, 0.02]], 0.03, { col: GUN });+ sideP(-0.245, [[-0.46, 0.02], [-0.46, 0.24], [0.20, 0.40], [0.22, 0.20], [-0.10, 0.02]], 0.03, { col: GUN });+ kit.mat('rubber');+ kit.cables([[0, 0, 0.335], [0.34, 0.03, 0.355], [0.80, -0.055, 0.365], [1.20, 0.01, 0.36], [1.50, -0.10, 0.33], [shF.L, 0, 0.27]], 0.024, [{ r: 0.017, col: CAB_K }, { r: 0.017, col: CAB_B }]);+ kit.mat('armour');+ const hornK = kit.at(shF, -0.396, 0.115, 0), hornKb = kit.at(shF, -0.14, 0.14, 0);+ const calfA = kit.at(shF, 0.50, -0.50, 0), calfAb = kit.at(shF, 0.62, -0.20, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hornK, hornKb, z0, { ear: 0.09, w: 0.11, foot: 0.16, t: 0.04 });+ kit.lug(calfA, calfAb, z0, { col: GUN, ear: 0.10, w: 0.095, foot: 0.16 });+ kit.frame(shF); kit.hull([shF.L / 2, 0, 0]);++ // ---- Metatarsus+ kit.use(ankleG); kit.frame(mtF); kit.hull([mtF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.22, 0.46, { bolts: 6, gear: 18, gearW: 0.4 });+ kit.drum([0, 0, 0.25], [0, 0, 1], 0.10, 0.06, { bolts: 0, seg: 12, col: DARK });+ kit.block([0.56, 0, 0.105], [0.90, 0.17, 0.065], { col: DARK, c: 0.01 });+ kit.block([0.56, 0, -0.105], [0.90, 0.17, 0.065], { col: DARK, c: 0.01 });+ kit.cyl([0.14, 0, 0], [0.98, 0, 0], 0.052, 0.052, { seg: 10, col: STEEL_D });+ kit.mat('armour');+ const m1 = kit.plate([[0.14, 0.17, 0.175], [0.92, 0.13, 0.145], [0.92, 0.13, -0.145], [0.14, 0.17, -0.175]], 0.05, { out: [0, 1, 0] });+ kit.seam(m1, [0.40, 0.155, 0.16], [0.40, 0.155, -0.16]); kit.bolts(m1, 0.028, 1, 0.014);+ const m2 = sideP(0.16, [[0.16, -0.13], [0.88, -0.11], [0.98, -0.05], [0.98, 0.07], [0.88, 0.12], [0.16, 0.15]], 0.03, { col: PAINT2 });+ kit.loop(m2, 0.035, 0.009);+ kit.streak(m2, [0.2, 0.02, 0.16], 0.5, 0.09, 'oil');+ sideP(-0.16, [[0.16, -0.13], [0.88, -0.11], [0.98, -0.05], [0.98, 0.07], [0.88, 0.12], [0.16, 0.15]], 0.03, { col: PAINT2 });+ kit.hazardPlate([[0.62, 0.155, 0.13], [0.62, 0.155, -0.13], [0.88, 0.145, -0.12], [0.88, 0.145, 0.12]], 0.025, { out: [0, 1, 0], sw: 0.04 });+ kit.plate([[0.16, -0.135, 0.16], [0.92, -0.115, 0.13], [0.92, -0.115, -0.13], [0.16, -0.135, -0.16]], 0.03, { out: [0, -1, 0], col: GUN });+ sideP(0.24, [[-0.46, -0.16], [-0.46, -0.04], [-0.15, 0.13], [-0.15, -0.20]], 0.03, { col: GUN });+ sideP(-0.24, [[-0.46, -0.16], [-0.46, -0.04], [-0.15, 0.13], [-0.15, -0.20]], 0.03, { col: GUN });+ kit.mat('rubber');+ kit.tube([[0, 0, 0.28], [0.25, -0.02, 0.205], [0.62, 0.04, 0.20], [0.95, 0, 0.215], [mtF.L, 0, 0.24]], 0.016, { col: CAB_Y });+ kit.tube([[0, 0, 0.28], [0.30, 0.05, 0.205], [0.66, -0.05, 0.20], [0.92, 0.03, 0.215], [mtF.L, 0, 0.24]], 0.014, { col: CAB_K });+ kit.mat('armour');+ const hornA = kit.at(mtF, -0.38, -0.10, 0), hornAb = kit.at(mtF, -0.10, -0.08, 0);+ const toeUp = kit.at(mtF, 0.62, 0.20, 0), toeUpb = kit.at(mtF, 0.70, 0.13, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hornA, hornAb, z0, { ear: 0.09, w: 0.11, foot: 0.14, t: 0.04 });+ kit.lug(toeUp, toeUpb, z0, { col: GUN, ear: 0.06, w: 0.055, foot: 0.08, t: 0.02 });++ // ---- Foot: armoured toes over rubber soles. Tread reaches y = 0; centre toe tip stays at SK.tipX.+ kit.use(toeG); kit.frame(null); kit.hull(null); kit.mat('armour');+ const toePiece = (deg, len, wid, x0, dz, cols, hBase, segs, rows) => {+ const c = Math.cos(deg * D2R), sn = Math.sin(deg * D2R), w = wid / 2;+ const P = (x, y, z) => [x0 + x * c + z * sn, y, z0 + dz - x * sn + z * c];+ const dirX = [c, 0, -sn], dirZ = [sn, 0, c];+ const halfW = (x) => (x < len * 0.62 ? w : x < len * 0.88 ? w * (1 - 0.34 * (x - len * 0.62) / (len * 0.26)) : w * (0.66 - 0.40 * (x - len * 0.88) / (len * 0.12)));+ const hgt = (x) => hBase - (hBase - 0.125) * Math.max(0, x / len);+ const stations = [-0.05, len * 0.62, len * 0.88, len];+ const outline = [...stations.map((x) => [x, halfW(x)]), ...[...stations].reverse().map((x) => [x, -halfW(x)])];+ // Rubber sole: a band standing proud of the armour, with cross-lugs that actually carry the foot.+ kit.loft([outline.map(([x, z]) => P(x, 0.022, z)), outline.map(([x, z]) => P(x, 0.085, z))], { col: RUB, mat: 'rubber', capEnd: false });+ const pitch = (len + 0.03) / rows, lw = pitch * 0.58;+ for (let i = 0; i < rows; i++) {+ const xc = i === rows - 1 ? len - lw / 2 : -0.03 + pitch * (i + 0.5);+ kit.block(P(xc, 0.0125, 0), [lw, 0.025, 2 * halfW(xc) * 0.9], { ax: [dirX, [0, 1, 0], dirZ], c: 0, col: tone(RUB, 1.25), mat: 'rubber' });+ }+ // Core, then side skirts, shingled knuckle plates and a steel toe cap.+ kit.loft([outline.map(([x, z]) => P(x, 0.085, z * 0.93)), outline.map(([x, z]) => P(Math.min(x, len * 0.97), hgt(x), z * 0.74))], { col: DARK, capStart: false });+ for (const s of [1, -1]) {+ const sd = vAdd(vMul(dirZ, s), [0, 0.3, 0]);+ const sk = kit.plate([P(0, 0.095, s * w * 0.97), P(len * 0.6, 0.095, s * w * 0.97), P(len * 0.6, hgt(len * 0.6) - 0.014, s * w * 0.8), P(0, hgt(0) - 0.014, s * w * 0.8)], 0.024, { out: sd, col: cols.side, noBack: true, c: 0.012 });+ kit.bolts(sk, 0.022, 1, 0.014);+ kit.plate([P(len * 0.62, 0.095, s * w * 0.97), P(len * 0.9, 0.095, s * halfW(len * 0.9) * 0.97), P(len * 0.9, hgt(len * 0.9) - 0.012, s * halfW(len * 0.9) * 0.76), P(len * 0.62, hgt(len * 0.62) - 0.012, s * w * 0.78)], 0.02, { out: vAdd(sd, vMul(dirX, 0.5)), col: cols.a, noBack: true, c: 0.01 });+ }+ const capAt = len * 0.72, step = (capAt + 0.03) / segs, lap = 0.04;+ let last = null;+ for (let i = 0; i < segs; i++) {+ const a = -0.03 + i * step, b = a + step + (i < segs - 1 ? lap : 0), wa = halfW(a) * 0.74 + 0.012, wb = halfW(b) * 0.74 + 0.012, ya = hgt(a) + 0.018, yb = hgt(b) + 0.048;+ last = kit.plate([P(a, ya, wa), P(b, yb, wb), P(b, yb, -wb), P(a, ya, -wa)], 0.04, { out: [0, 1, 0], col: i % 2 ? cols.b : cols.a, c: 0.018 });+ kit.bolts(last, 0.024, 1, 0.017);+ kit.seam(last, last.at(0.5, 0.06), last.at(0.5, 0.94));+ }+ const yc = hgt(capAt) + 0.03, capW = halfW(capAt) * 0.74 + 0.012, tipW = halfW(len) * 0.86;+ kit.plate([P(capAt, yc, capW), P(len * 0.995, 0.128, tipW), P(len * 0.995, 0.128, -tipW), P(capAt, yc, -capW)], 0.04, { out: [0.4, 1, 0], mat: 'steel', col: GM_L, c: 0.016 });+ kit.plate([P(len * 0.998, 0.128, tipW), P(len * 0.998, 0.09, halfW(len) * 0.94), P(len * 0.998, 0.09, -halfW(len) * 0.94), P(len * 0.998, 0.128, -tipW)], 0.03, { out: dirX, mat: 'steel', col: GM, c: 0.008 });+ return { last };+ };+ const ct = toePiece(0, SK.tipX, 0.44, 0, 0, { a: PAINT, b: PAINT2, side: PAINT2 }, 0.31, 2, 7);+ const hz = ct.last;+ kit.hazardPlate([hz.at(0.42, 0.1, 0.008), hz.at(0.96, 0.1, 0.008), hz.at(0.96, 0.9, 0.008), hz.at(0.42, 0.9, 0.008)], 0.02, { out: hz.n, sw: 0.04, c: 0.006, noBack: true });+ toePiece(24, 0.78, 0.29, 0.04, -0.14, { a: PAINT2, b: PAINT, side: GUN }, 0.25, 2, 5);+ toePiece(-24, 0.78, 0.29, 0.04, 0.14, { a: PAINT2, b: PAINT, side: GUN }, 0.25, 2, 5);+ toePiece(180, 0.62, 0.28, -0.02, 0, { a: GUN, b: PAINT2, side: GUN }, 0.23, 1, 4);+ kit.mat('steel');+ kit.drum([0, SK.toeH, z0], [0, 0, 1], 0.17, 0.46, { bolts: 6, seg: 18 });+ kit.mat('armour');+ kit.block([0.04, 0.17, z0], [0.46, 0.27, 0.42], { col: GUN, c: 0.03 });+ const ftTop = kit.plate([[-0.14, 0.325, z0 + 0.20], [0.27, 0.27, z0 + 0.20], [0.27, 0.27, z0 - 0.20], [-0.14, 0.325, z0 - 0.20]], 0.03, { out: [0, 1, 0], col: PAINT2 });+ kit.bolts(ftTop, 0.024, 1, 0.016);+ kit.ventOn(ftTop, 0.22, 0.78, 0.2, 0.8, { n: 4 });+ const ftFront = kit.plate([[0.20, 0.312, z0 + 0.215], [0.335, 0.225, z0 + 0.215], [0.335, 0.225, z0 - 0.215], [0.20, 0.312, z0 - 0.215]], 0.03, { out: [0.6, 1, 0], col: PAINT });+ kit.bolts(ftFront, 0.022, 1, 0.014);+ const ftRear = kit.plate([[-0.15, 0.335, z0 + 0.205], [-0.15, 0.335, z0 - 0.205], [-0.28, 0.20, z0 - 0.195], [-0.28, 0.20, z0 + 0.195]], 0.03, { out: [-0.7, 1, 0], col: GUN });+ kit.bolts(ftRear, 0.022, 1, 0.014);+ kit.streak(ftRear, [-0.17, 0.32, z0 + 0.08], 0.22, 0.07, 'oil');+ const toeB = [0.38, 0.30, 0], toeBb = [0.42, 0.20, 0];+ kit.lug([toeB[0], toeB[1], 0], [toeBb[0], toeBb[1], 0], z0, { col: GUN, ear: 0.055, w: 0.055, foot: 0.08, t: 0.02 });++ // ---- Leg hydraulics: each spans one joint and is aim-constrained at both ends.+ const pelvisAnchor = [-0.72, 3.50, z0];+ kit.piston('Hip_' + side, pelvisG, pelvisAnchor, hipG, hipLugT, { r: 0.085, rod: 0.05 });+ kit.piston('Knee_' + side, hipG, kneeA, kneeG, hornK, { r: 0.098, rod: 0.058, hose: CAB_R });+ kit.piston('Ankle_' + side, kneeG, calfA, ankleG, hornA, { r: 0.098, rod: 0.058, hose: CAB_R });+ kit.piston('Toe_' + side, ankleG, toeUp, toeG, [toeB[0], toeB[1], z0], { r: 0.052, rod: 0.03, seg: 10 });+ kit.mirror(1);+ return { hipG, kneeG, ankleG, toeG };+}++// ---------------------------------------------------------------- pelvis+function buildPelvis(kit, pelvisG) {+ const z0 = SK.legZ, y0 = SK.hip[1];+ kit.use(pelvisG); kit.frame(null); kit.mirror(1); kit.hull([0, y0, 0]); kit.mat('armour');+ const oct = (y, xf, xr, hz, cut) => [[xr + cut, y, hz], [xf - cut, y, hz], [xf, y, hz - cut], [xf, y, -hz + cut], [xf - cut, y, -hz], [xr + cut, y, -hz], [xr, y, -hz + cut], [xr, y, hz - cut]];+ kit.loft([oct(3.00, 0.34, -0.34, 0.34, 0.08), oct(3.18, 0.56, -0.58, 0.50, 0.12), oct(3.62, 0.60, -0.66, 0.52, 0.12), oct(3.80, 0.50, -0.54, 0.50, 0.10)], { col: DARK });+ const f0 = kit.plate([[0.635, 3.66, 0.27], [0.635, 3.66, -0.27], [0.60, 3.20, -0.24], [0.60, 3.20, 0.24]], 0.06, { col: BONE });+ kit.loop(f0, 0.05); kit.bolts(f0, 0.03, 1, 0.016);+ kit.stencil([0.612, 3.34, 0.13], [0, 0, -1], [0.076, 0.997, 0], 'H-7', 0.13, RUST, [0.997, -0.076, 0]);+ kit.hazardPlate([[0.60, 3.19, 0.24], [0.60, 3.19, -0.24], [0.38, 2.99, -0.16], [0.38, 2.99, 0.16]], 0.04, { out: [1, -0.8, 0], sw: 0.05 });+ const rr0 = kit.plate([[-0.70, 3.66, 0.30], [-0.70, 3.66, -0.30], [-0.62, 3.20, -0.26], [-0.62, 3.20, 0.26]], 0.05, { col: PAINT2 });+ kit.vent([-0.692, 3.58, 0.20], [-0.692, 3.58, -0.20], [-0.642, 3.30, -0.17], [-0.642, 3.30, 0.17], { n: 6 });+ kit.seam(rr0, [-0.63, 3.26, 0.24], [-0.63, 3.26, -0.24]);+ kit.plate([[-0.62, 3.19, 0.26], [-0.62, 3.19, -0.26], [-0.38, 2.99, -0.18], [-0.38, 2.99, 0.18]], 0.04, { col: GUN, out: [-1, -0.8, 0] });+ for (const s of [1, -1]) {+ kit.mirror(s); kit.mat('steel');+ kit.drum([0, y0, 0.53], [0, 0, 1], 0.46, 0.13, { gear: 32, bolts: 12, gearW: 0.7, seg: 26 });+ kit.mat('armour');+ const a = kit.plate([[0.635, 3.68, 0.28], [0.635, 3.68, 0.525], [0.60, 3.20, 0.505], [0.60, 3.20, 0.25]], 0.05);+ kit.bolts(a, 0.03, 1, 0.016);+ kit.streak(a, [0.63, 3.64, 0.40], 0.42, 0.09, 'oil');+ kit.plate([[0.60, 3.19, 0.25], [0.60, 3.19, 0.505], [0.36, 3.02, 0.345], [0.38, 2.99, 0.17]], 0.04, { col: PAINT2, out: [1, -0.8, 0.3] });+ kit.plate([[-0.70, 3.68, 0.31], [-0.70, 3.68, 0.53], [-0.62, 3.20, 0.51], [-0.62, 3.20, 0.27]], 0.05, { col: PAINT2 });+ kit.plate([[-0.62, 3.19, 0.27], [-0.62, 3.19, 0.51], [-0.36, 3.02, 0.345], [-0.38, 2.99, 0.19]], 0.04, { col: GUN, out: [-1, -0.8, 0.3] });+ const t = kit.plate([[0.50, 3.84, 0.53], [-0.56, 3.84, 0.53], [-0.60, 3.68, 0.555], [0.56, 3.68, 0.555]], 0.04, { col: PAINT, out: [0, 0.7, 1] });+ kit.seam(t, [0.2, 3.76, 0.545], [-0.2, 3.76, 0.545]);+ // Outrigger carrying the hip piston's upper pin+ kit.mat('steel');+ kit.block([-0.70, 3.50, 0.63], [0.13, 0.15, 0.60], { col: GUN, c: 0.02 });+ kit.mat('rubber');+ kit.tube([[-0.58, 3.62, 0.30], [-0.70, 3.62, 0.42], [-0.76, 3.54, 0.62], [-0.74, 3.42, 0.78], [-0.46, 3.36, 0.72], [-0.1, 3.42, 0.60], [0, y0, 0.60]], 0.017, { col: CAB_K, sp: 3 });+ kit.mat('armour');+ kit.lug([-0.72, 3.50, 0], [-0.70, 3.50, 0], z0, { col: GUN, ear: 0.085, foot: 0.085, w: 0.085 });+ }+ kit.mirror(1); kit.mat('steel');+ kit.lathe([0, 3.76, 0], [0, 1, 0], [[0, 0.46, GM_D], [0.02, 0.60, GM_H, 1], [0.03, 0.60, GM], [0.09, 0.60, GM_H, 1], [0.11, 0.52, GM_D], [0.15, 0.50, GM], [0.15, 0, GM]], { seg: 32 });+ const NT = 40;+ for (let i = 0; i < NT; i++) {+ const th = (i / NT) * 2 * Math.PI, rad = [Math.cos(th), 0, Math.sin(th)], tan = [-Math.sin(th), 0, Math.cos(th)];+ kit.block(vAdd([0, 3.825, 0], vMul(rad, 0.612)), [0.03, 0.05, 0.05], { ax: [rad, [0, 1, 0], tan], c: 0, col: GM_L, polish: true });+ }+ kit.mat('armour');+}++// ---------------------------------------------------------------- torso, cockpit, backpack+function buildTorso(kit, waistG) {+ kit.use(waistG); kit.frame(null); kit.mirror(1); kit.hull([0, 4.6, 0]); kit.mat('armour');+ const tRing = (y, f, n, r, h) => {+ const c = Math.max(0.56, h * 0.62);+ return [[r + 0.18, y, h], [f - 0.30, y, h], [f, y, c], [f, y, 0.52], [n, y, 0.50], [n, y, -0.50], [f, y, -0.52], [f, y, -c], [f - 0.30, y, -h], [r + 0.18, y, -h], [r, y, -h * 0.55], [r, y, h * 0.55]];+ };+ kit.loft([tRing(3.96, 0.50, 0.50, -0.50, 0.60), tRing(4.25, 0.72, 0.72, -0.70, 0.88), tRing(4.56, 0.86, 0.86, -0.84, 1.10), tRing(4.60, 0.87, 0.40, -0.85, 1.12), tRing(5.10, 0.84, 0.40, -0.92, 1.18), tRing(5.40, 0.62, 0.40, -0.88, 1.02), tRing(5.44, 0.60, 0.60, -0.88, 1.00), tRing(5.62, 0.25, 0.25, -0.70, 0.72)], { col: DARK });+ kit.mat('steel');+ kit.lathe([0, 3.90, 0], [0, 1, 0], [[0, 0, STEEL_D], [0, 0.50, STEEL_D], [0.04, 0.50, GUN], [0.08, 0.58, GUN]], { seg: 28 });+ for (const s of [1, -1]) { kit.mirror(s); kit.drum([0.34, 3.97, 0.36], [0, 1, 0], 0.09, 0.14, { bolts: 0, seg: 12 }); }+ kit.mirror(1); kit.mat('armour');++ // Abdomen bands+ const ab1 = kit.plate([[0.76, 4.27, 0.56], [0.76, 4.27, -0.56], [0.63, 4.13, -0.50], [0.63, 4.13, 0.50]], 0.05, { col: PAINT2 });+ kit.bolts(ab1, 0.026, 2, 0.014);+ const ab2 = kit.plate([[0.67, 4.15, 0.48], [0.67, 4.15, -0.48], [0.55, 4.02, -0.42], [0.55, 4.02, 0.42]], 0.05, { col: GUN });+ kit.loop(ab2, 0.03, 0.009);+ kit.hazardPlate([[0.56, 4.035, 0.40], [0.56, 4.035, -0.40], [0.50, 3.94, -0.36], [0.50, 3.94, 0.36]], 0.04, { sw: 0.05 });+ const rb1 = kit.plate([[-0.72, 4.26, 0.56], [-0.72, 4.26, -0.56], [-0.52, 3.98, -0.40], [-0.52, 3.98, 0.40]], 0.05, { col: GUN });+ kit.bolts(rb1, 0.03, 1, 0.014);++ for (const s of [1, -1]) {+ kit.mirror(s);+ // Cheek intake block+ const c0 = kit.plate([[0.915, 4.62, 0.53], [0.915, 4.62, 0.70], [0.885, 5.08, 0.74], [0.885, 5.08, 0.53]], 0.05, { col: BONE });+ kit.bolts(c0, 0.026, 1, 0.014);+ const ob = kit.plate([[0.915, 4.60, 0.71], [0.60, 4.60, 1.15], [0.58, 5.09, 1.215], [0.89, 5.09, 0.75]], 0.06, { col: PAINT });+ kit.vent([0.885, 4.66, 0.745], [0.635, 4.66, 1.095], [0.625, 4.92, 1.135], [0.875, 4.92, 0.77], { n: 6, col: GUN });+ kit.seam(ob, [0.885, 4.97, 0.76], [0.62, 4.97, 1.15]); kit.bolts(ob, 0.03, 1, 0.016);+ kit.mat('glowAmber');+ kit.lathe([0.916, 4.75, 0.615], [1, 0, 0], [[0, 0.03], [0.012, 0.022], [0.012, 0]], { seg: 8, mat: 'glowAmber' });+ kit.mat('armour');+ const br = kit.plate([[0.90, 5.11, 0.53], [0.90, 5.11, 0.77], [0.66, 5.41, 0.68], [0.66, 5.41, 0.53]], 0.055, { col: PAINT2 });+ kit.bolts(br, 0.028, 1, 0.014);+ const br2 = kit.plate([[0.60, 5.12, 1.22], [0.89, 5.12, 0.79], [0.66, 5.41, 0.70], [0.42, 5.41, 1.03]], 0.05, { col: PAINT });+ kit.loop(br2, 0.04);+ kit.plate([[0.885, 4.575, 0.50], [0.885, 4.575, 0.71], [0.745, 4.255, 0.565], [0.745, 4.255, 0.50]], 0.05, { col: GUN });+ const fl = kit.plate([[0.885, 4.575, 0.72], [0.585, 4.575, 1.125], [0.43, 4.255, 0.90], [0.745, 4.255, 0.575]], 0.055, { col: PAINT });+ kit.seam(fl, [0.81, 4.415, 0.635], [0.51, 4.415, 1.01]); kit.bolts(fl, 0.03, 1, 0.016);+ const sl = kit.plate([[0.42, 4.26, 0.915], [-0.42, 4.26, 0.915], [-0.54, 4.56, 1.135], [0.56, 4.56, 1.135]], 0.05, { col: PAINT2 });+ kit.seam(sl, [0.1, 4.28, 0.93], [0.14, 4.54, 1.125]); kit.seam(sl, [-0.2, 4.28, 0.93], [-0.24, 4.54, 1.125]);+ kit.hazardPlate([[0.40, 4.225, 0.875], [-0.40, 4.225, 0.875], [-0.42, 4.14, 0.80], [0.42, 4.14, 0.80]], 0.03, { sw: 0.06, out: [0, -0.5, 1] });+ kit.plate([[0.52, 4.60, 1.155], [0.52, 5.09, 1.215], [0.30, 5.09, 1.215], [0.30, 4.60, 1.155]], 0.04, { col: PAINT });+ const srr = kit.plate([[-0.26, 4.60, 1.155], [-0.26, 5.09, 1.215], [-0.70, 5.09, 1.215], [-0.70, 4.60, 1.155]], 0.05, { col: PAINT });+ kit.loop(srr, 0.04); kit.bolts(srr, 0.025, 2, 0.014);+ kit.streak(srr, [-0.32, 5.04, 1.21], 0.4, 0.1, 'oil');+ const rf = kit.plate([[0.38, 5.46, 1.015], [-0.66, 5.46, 1.015], [-0.53, 5.64, 0.735], [0.10, 5.64, 0.735]], 0.05, { col: PAINT });+ kit.bolts(rf, 0.03, 2, 0.014); kit.seam(rf, [-0.12, 5.47, 1.0], [-0.2, 5.63, 0.74]);+ // Shoulder yoke+ kit.mat('steel');+ kit.drum([0.02, 5.00, 1.255], [0, 0, 1], 0.40, 0.17, { gear: 30, bolts: 10, gearW: 0.6, seg: 24 });+ kit.block([0.02, 4.70, 1.20], [0.34, 0.34, 0.13], { col: GUN, c: 0.03 });+ kit.mat('rubber');+ kit.tube([[-0.74, 5.12, 1.05], [-0.68, 5.18, 1.26], [-0.34, 5.27, 1.37], [-0.05, 5.1, 1.37], [0.02, 5.0, 1.36]], 0.02, { col: CAB_K });+ kit.tube([[-0.74, 5.05, 1.05], [-0.70, 5.08, 1.27], [-0.40, 5.15, 1.37], [-0.08, 5.05, 1.37], [0.02, 5.0, 1.36]], 0.016, { col: CAB_R });+ kit.mat('armour');+ }+ kit.mirror(1);+ // Brow, roof, hatch+ const fh = kit.plate([[0.625, 5.46, 0.60], [0.625, 5.46, -0.60], [0.27, 5.645, -0.43], [0.27, 5.645, 0.43]], 0.055, { col: PAINT2 });+ kit.bolts(fh, 0.03, 2, 0.014);+ const rft = kit.plate([[0.26, 5.645, 0.70], [0.26, 5.645, -0.70], [-0.52, 5.645, -0.70], [-0.52, 5.645, 0.70]], 0.04, { col: PAINT2 });+ kit.loop(rft, 0.06);+ kit.mat('steel');+ kit.lathe([-0.14, 5.645, 0.12], [0, 1, 0], [[0, 0.24, GUN], [0.035, 0.24, GUN], [0.05, 0.21, STEEL_D], [0.05, 0, STEEL_D]], { seg: 16 });+ kit.block([-0.14, 5.71, 0.12], [0.30, 0.03, 0.04], { col: STEEL });+ // Sensor pod and whip antenna+ kit.block([0.05, 5.70, -0.34], [0.26, 0.11, 0.28], { col: GUN, c: 0.02 });+ kit.cyl([-0.42, 5.65, -0.52], [-0.42, 5.98, -0.52], 0.014, 0.006, { seg: 6, col: RUB });+ kit.cyl([-0.42, 5.65, -0.40], [-0.42, 5.84, -0.40], 0.012, 0.005, { seg: 6, col: RUB });+ kit.lathe([0.18, 5.70, -0.34], [1, 0, 0], [[0, 0.04, GUN], [0.02, 0.04, GUN], [0.02, 0.028, GUN]], { seg: 10 });+ kit.mat('glowCyan');+ kit.lathe([0.20, 5.70, -0.34], [1, 0, 0], [[0, 0.028], [0.006, 0.02], [0.006, 0]], { seg: 10, mat: 'glowCyan' });+ kit.mat('armour');++ // ---- Chin tub+ kit.hull([1.0, 4.40, 0]);+ const chinRing = (x, yt, yb, z, cut) => [[x, yt, z], [x, yt, -z], [x, yb + cut, -z], [x, yb, -z + cut], [x, yb, z - cut], [x, yb + cut, z]];+ kit.loft([chinRing(0.66, 4.60, 4.10, 0.50, 0.10), chinRing(1.12, 4.60, 4.33, 0.47, 0.08), chinRing(1.43, 4.62, 4.49, 0.25, 0.05)], { col: DARK });+ const ch1 = kit.plate([[1.15, 4.34, 0.45], [1.15, 4.34, -0.45], [0.70, 4.09, -0.47], [0.70, 4.09, 0.47]], 0.05, { col: PAINT });+ kit.bolts(ch1, 0.03, 2, 0.014); kit.seam(ch1, [0.92, 4.21, 0.44], [0.92, 4.21, -0.44]);+ const ch2 = kit.plate([[1.45, 4.49, 0.235], [1.45, 4.49, -0.235], [1.17, 4.33, -0.40], [1.17, 4.33, 0.40]], 0.05, { col: PAINT2 });+ kit.bolts(ch2, 0.025, 1, 0.012);+ kit.hazardPlate([[1.455, 4.63, 0.235], [1.455, 4.63, -0.235], [1.455, 4.50, -0.235], [1.455, 4.50, 0.235]], 0.03, { sw: 0.05, out: [1, 0, 0] });+ for (const s of [1, -1]) {+ kit.mirror(s);+ const cs = kit.plate([[1.13, 4.61, 0.485], [0.68, 4.61, 0.515], [0.68, 4.15, 0.515], [1.13, 4.37, 0.485]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.seam(cs, [0.90, 4.60, 0.50], [0.90, 4.24, 0.50]); kit.bolts(cs, 0.026, 1, 0.014);+ kit.plate([[1.44, 4.62, 0.26], [1.15, 4.61, 0.47], [1.15, 4.37, 0.47], [1.44, 4.51, 0.26]], 0.04, { col: GUN, out: [0.5, 0, 1] });+ // Lamps+ kit.mat('steel');+ kit.lathe([1.22, 4.42, 0.335], [1, 0.1, 0.25], [[0, 0, GUN], [0, 0.05, GUN], [0.05, 0.05, GUN], [0.05, 0.036, GUN]], { seg: 10 });+ kit.mat('glowCyan');+ kit.lathe(vAdd([1.22, 4.42, 0.335], vMul(vUnit([1, 0.1, 0.25]), 0.04)), [1, 0.1, 0.25], [[0, 0.036], [0.006, 0]], { seg: 10, mat: 'glowCyan' });+ kit.mat('armour');+ }+ kit.mirror(1);++ // ---- Cockpit interior+ kit.hull(null); kit.mat('armour');+ kit.poly([[0.40, 4.606, 0.49], [1.14, 4.606, 0.46], [1.39, 4.606, 0.24], [1.39, 4.606, -0.24], [1.14, 4.606, -0.46], [0.40, 4.606, -0.49]], lin(0x2a2c2e), { out: [0, 1, 0] });+ kit.mat('steel');+ kit.block([0.46, 4.97, 0], [0.09, 0.66, 0.44], { col: GUN, c: 0.02, edge: tone(GUN, 1.25) });+ kit.block([0.47, 5.27, 0], [0.08, 0.13, 0.20], { col: GUN, c: 0.015, edge: tone(GUN, 1.25) });+ kit.block([0.70, 4.70, 0], [0.42, 0.10, 0.44], { col: GUN, c: 0.02, edge: tone(GUN, 1.25) });+ kit.mat('rubber');+ // Seated pilot: rounded limbs in a flight suit, dark visor+ kit.lathe([0.62, 4.74, 0], [0, 1, 0], [[0, 0, SUIT], [0, 0.11, SUIT], [0.10, 0.135, SUIT], [0.27, 0.16, SUIT], [0.35, 0.12, SUIT], [0.37, 0.05, RUB], [0.43, 0.04, RUB], [0.43, 0, RUB]], { seg: 10, mat: 'rubber' });+ for (const sz of [1, -1]) {+ kit.tube([[0.64, 5.04, sz * 0.15], [0.70, 4.92, sz * 0.21], [0.84, 4.88, sz * 0.19], [0.95, 4.85, sz * 0.13]], 0.046, { col: SUIT, seg: 7, sp: 2 });+ kit.tube([[0.66, 4.78, sz * 0.09], [0.84, 4.82, sz * 0.10], [0.99, 4.80, sz * 0.10], [1.05, 4.64, sz * 0.10]], 0.066, { col: tone(SUIT, 0.85), seg: 7, sp: 2 });+ }+ kit.lathe([0.64, 5.12, 0], [0, 1, 0], [[0, 0, BONE], [0, 0.07, BONE], [0.05, 0.115, BONE], [0.14, 0.12, BONE], [0.20, 0.075, BONE], [0.22, 0, BONE]], { seg: 12, mat: 'rubber' });+ kit.poly([[0.76, 5.26, 0.085], [0.76, 5.26, -0.085], [0.76, 5.18, -0.085], [0.76, 5.18, 0.085]], tone(RUB, 0.5), { out: [1, 0, 0], mat: 'rubber' });+ kit.mat('steel');+ kit.block([1.08, 4.76, 0], [0.32, 0.30, 0.62], { col: GUN, c: 0.02, ax: [vUnit([1, 0.45, 0]), vUnit([-0.45, 1, 0]), [0, 0, 1]] });+ kit.mat('glowCyan');+ const scr = (z0, w) => kit.poly([[0.985, 4.93, z0 + w], [0.985, 4.93, z0 - w], [0.915, 4.80, z0 - w], [0.915, 4.80, z0 + w]].map((p) => [p[0] + 0.012, p[1] + 0.012, p[2]]), WHITE, { out: [-1, 0.5, 0], mat: 'glowCyan' });+ scr(0, 0.10); kit.mat('glowAmber');+ kit.poly([[1.00, 4.93, 0.27], [1.00, 4.93, 0.15], [0.93, 4.80, 0.15], [0.93, 4.80, 0.27]], WHITE, { out: [-1, 0.5, 0], mat: 'glowAmber' });+ kit.mat('glowCyan');+ kit.poly([[1.00, 4.93, -0.15], [1.00, 4.93, -0.27], [0.93, 4.80, -0.27], [0.93, 4.80, -0.15]], WHITE, { out: [-1, 0.5, 0], mat: 'glowCyan' });+ kit.mat('armour');++ // ---- Glass canopy and frame+ const A0b = [0.80, 4.60, 0.50], A0s = [0.80, 5.18, 0.50], A0t = [0.80, 5.42, 0.26];+ const A1b = [1.14, 4.60, 0.47], A1s = [1.14, 5.04, 0.44], A1t = [1.14, 5.20, 0.20];+ const A2b = [1.40, 4.62, 0.25], A2t = [1.40, 4.84, 0.12];+ const mz = (p) => [p[0], p[1], -p[2]];+ const cc = [1.0, 4.9, 0];+ const pane = (pts) => kit.poly(pts, WHITE, { out: vSub(centroid(pts), cc), mat: 'glass' });+ pane([A0t, A1t, mz(A1t), mz(A0t)]);+ pane([A1t, A2t, mz(A2t), mz(A1t)]);+ pane([A2t, A2b, mz(A2b), mz(A2t)]);+ for (const f of [(p) => p, mz]) {+ pane([f(A0s), f(A1s), f(A1t), f(A0t)]);+ pane([f(A0b), f(A1b), f(A1s), f(A0s)]);+ pane([f(A1b), f(A2b), f(A2t), f(A1s)]);+ pane([f(A1s), f(A2t), f(A1t)]);+ }+ kit.mat('steel');+ const bar = (a, b, r = 0.026) => kit.cyl(a, b, r, r, { seg: 6, col: GUN });+ for (const f of [(p) => p, mz]) {+ bar(f(A0b), f(A0s), 0.038); bar(f(A0s), f(A0t), 0.038);+ bar(f(A1b), f(A1s)); bar(f(A1s), f(A1t));+ bar(f(A0s), f(A1s), 0.016); bar(f(A0t), f(A1t)); bar(f(A1t), f(A2t)); bar(f(A1s), f(A2t), 0.016); bar(f(A1b), f(A2b)); bar(f(A0b), f(A1b)); bar(f(A2b), f(A2t));+ }+ bar(A0t, mz(A0t), 0.03); bar(A1t, mz(A1t)); bar(A2t, mz(A2t)); bar(A2b, mz(A2b));+ kit.mat('armour');++ // ---- Backpack: reactor block, radiators, stacks+ kit.hull([-1.1, 4.8, 0]);+ const bRing = (x, y0, y1, z) => [[x, y1, z], [x, y1, -z], [x, y0, -z], [x, y0, z]];+ kit.loft([bRing(-0.78, 4.24, 5.46, 0.86), bRing(-1.22, 4.32, 5.40, 0.78), bRing(-1.46, 4.48, 5.22, 0.60)], { col: DARK });+ const bt = kit.plate([[-0.80, 5.49, 0.82], [-0.80, 5.49, -0.82], [-1.25, 5.43, -0.76], [-1.25, 5.43, 0.76]], 0.05, { col: PAINT });+ kit.loop(bt, 0.05); kit.bolts(bt, 0.026, 2, 0.014);+ kit.streak(bt, [-0.92, 5.48, 0.44], 0.34, 0.2, 'soot'); kit.streak(bt, [-0.92, 5.48, -0.44], 0.34, 0.2, 'soot');+ const br0 = kit.hazardPlate([[-1.26, 5.43, 0.74], [-1.26, 5.43, -0.74], [-1.46, 5.25, -0.58], [-1.46, 5.25, 0.58]], 0.03, { sw: 0.08, out: [-1, 1, 0] });+ kit.bolts(br0, 0.018, 1, 0.012);+ const rearP = kit.plate([[-1.49, 5.20, 0.56], [-1.49, 5.20, -0.56], [-1.49, 4.50, -0.56], [-1.49, 4.50, 0.56]], 0.03, { col: PAINT2, out: [-1, 0, 0] });+ kit.bolts(rearP, 0.03, 2, 0.016);+ kit.streak(rearP, [-1.49, 4.60, 0.28], 0.1, 0.26, 'soot'); kit.streak(rearP, [-1.49, 4.60, -0.28], 0.1, 0.26, 'soot');+ for (const z of [0.28, -0.28]) {+ kit.vent([-1.50, 4.60, z - 0.20], [-1.50, 4.60, z + 0.20], [-1.50, 5.08, z + 0.20], [-1.50, 5.08, z - 0.20], { n: 7, col: GUN, base: WHITE, glow: 'glowAmber', depth: 0.02 });+ }+ kit.stencil([-1.505, 4.52, 0.08], [0, 0, -1], [0, 1, 0], 'HE', 0.06, HAZ_Y, [-1, 0, 0]);+ for (const s of [1, -1]) {+ kit.mirror(s);+ const sd = kit.plate([[-0.82, 5.36, 0.875], [-1.20, 5.32, 0.795], [-1.20, 5.02, 0.795], [-0.82, 5.02, 0.875]], 0.04, { col: PAINT2, out: [0, 0, 1] });+ kit.bolts(sd, 0.024, 1, 0.012);+ kit.mat('steel');+ // Radiator bank: fins over a dark core, exposed on the flank+ kit.block([-1.02, 4.66, 0.82], [0.44, 0.60, 0.04], { col: tone(GUN, 0.4), c: 0 });+ for (let i = 0; i < 11; i++) kit.block([-0.82 - i * 0.04, 4.66, 0.86], [0.012, 0.56, 0.05], { col: STEEL_D, c: 0 });+ // Stack+ kit.lathe([-1.32, 5.30, 0.44], [-0.12, 1, 0], [[0, 0.11, GUN], [0.40, 0.095, GUN], [0.40, 0.12, STEEL_D], [0.50, 0.12, STEEL_D], [0.50, 0.08, tone(RUB, 0.6)], [0.42, 0.075, tone(RUB, 0.6)], [0.42, 0, tone(RUB, 0.6)]], { seg: 12 });+ // Coolant lines to the waist axis+ kit.mat('rubber');+ kit.tube([[-0.92, 4.26, 0.46], [-0.86, 4.04, 0.42], [-0.60, 3.96, 0.30], [-0.26, 3.95, 0.14], [0, 3.94, 0.02]], 0.024, { col: CAB_K });+ kit.tube([[-0.98, 4.26, 0.34], [-0.90, 4.03, 0.30], [-0.62, 3.95, 0.20], [-0.26, 3.95, 0.09], [0, 3.94, 0.01]], 0.018, { col: CAB_B });+ kit.mat('armour');+ }+ kit.mirror(1);++ // ---- Right shoulder sensor / counterweight (torso mounted)+ kit.hull(null);+ kit.block([-0.08, 5.51, 0.92], [0.50, 0.18, 0.38], { col: PAINT2, c: 0.03 });+ kit.mat('steel');+ for (const dz of [-0.09, 0.09]) {+ kit.lathe([0.17, 5.53, 0.92 + dz], [1, 0, 0], [[-0.02, 0, GUN], [-0.02, 0.06, GUN], [0.07, 0.066, GUN], [0.07, 0.042, tone(GUN, 0.4)], [0.05, 0.042, tone(GUN, 0.4)]], { seg: 12 });+ kit.mat('glowCyan');+ kit.lathe([0.22, 5.53, 0.92 + dz], [1, 0, 0], [[0, 0.042], [0.006, 0], [0.006, 0]], { seg: 12, mat: 'glowCyan' });+ kit.mat('steel');+ }+ kit.mat('armour');+ // Paint on the rear hull facets and backpack flanks+ kit.hull([-1.0, 4.8, 0]);+ for (const s of [1, -1]) {+ kit.mirror(s);+ const rh = kit.plate([[-0.66, 4.60, 1.125], [-0.82, 4.60, 0.90], [-0.87, 5.09, 0.90], [-0.71, 5.09, 1.19]], 0.05, { col: PAINT2, out: [-1, 0, 1] });+ kit.bolts(rh, 0.026, 1, 0.014);+ const bf = kit.plate([[-1.24, 5.38, 0.80], [-1.24, 4.34, 0.80], [-1.48, 4.50, 0.615], [-1.48, 5.20, 0.615]], 0.04, { col: PAINT, out: [-0.6, 0, 1] });+ kit.loop(bf, 0.04); kit.bolts(bf, 0.022, 1, 0.012);+ kit.plate([[-0.80, 4.21, 0.84], [-1.22, 4.29, 0.76], [-1.22, 4.29, 0.04], [-0.80, 4.21, 0.04]], 0.03, { col: PAINT2, out: [0, -1, 0] });+ }+ kit.mirror(1);+ const rs = kit.plate([[-0.52, 5.647, 0.70], [-0.52, 5.647, -0.70], [-0.78, 5.50, -0.84], [-0.78, 5.50, 0.84]], 0.05, { col: PAINT, out: [-0.5, 1, 0] });+ kit.bolts(rs, 0.03, 2, 0.014); kit.seam(rs, [-0.65, 5.575, 0.76], [-0.65, 5.575, -0.76]);+ kit.hull(null);+ // Pod pylon on the left shoulder line+ kit.mat('steel');+ kit.beam([-0.05, 5.44, -0.80], [-0.05, 5.50, -1.34], 0.13, 0.20, { col: GUN, c: 0.02, up: [0, 1, 0] });+ kit.mat('armour');+}++// ---------------------------------------------------------------- arms+function buildUpperArm(kit, side, waistG) {+ const s = side === 'L' ? -1 : 1, z0 = SK.armZ;+ kit.mirror(s);+ const G = kit.group('Shoulder_' + side, waistG, [SK.shoulder[0], SK.shoulder[1], s * z0], 'UpperArm_' + side);+ G.node.userData = { hinge: 'z' };+ const F = kit.bone(SK.shoulder, SK.elbow, z0);+ kit.use(G); kit.frame(F); kit.hull(null); kit.mat('steel');+ kit.drum([0, 0, -0.02], [0, 0, 1], 0.33, 0.50, { bolts: 10, seg: 24 });+ kit.mat('armour');+ // Pauldron core: the dark shadow gap behind every armour layer.+ kit.block([-0.02, 0.01, 0.05], [0.84, 0.74, 0.62], { col: DARK, c: 0.05 });++ // Crown: a cap and four sloped facets, each its own bevelled plate, with a bolted spine on top.+ const crown = kit.plate([[-0.47, -0.19, 0.19], [-0.47, 0.21, 0.19], [-0.47, 0.21, -0.19], [-0.47, -0.19, -0.19]], 0.05, { col: PAINT, out: [-1, 0, 0] });+ kit.bolts(crown, 0.024, 1, 0.017);+ const spine = kit.plate([[-0.476, -0.15, 0.05], [-0.476, 0.17, 0.05], [-0.476, 0.17, -0.05], [-0.476, -0.15, -0.05]], 0.012, { col: GUN, out: [-1, 0, 0], noBack: true, c: 0.01 });+ const cO = kit.plate([[-0.47, -0.19, 0.195], [-0.47, 0.21, 0.195], [-0.34, 0.35, 0.345], [-0.34, -0.33, 0.345]], 0.05, { col: PAINT2, out: [-0.6, 0, 0.8] });+ kit.bolts(cO, 0.026, 1, 0.017); kit.seam(cO, cO.at(0.5, 0.06), cO.at(0.5, 0.94));+ const cF = kit.plate([[-0.47, 0.215, 0.19], [-0.47, 0.215, -0.19], [-0.33, 0.41, -0.27], [-0.33, 0.41, 0.335]], 0.05, { col: PAINT, out: [-0.6, 0.8, 0] });+ kit.bolts(cF, 0.026, 1, 0.017);+ const cR = kit.plate([[-0.47, -0.195, -0.19], [-0.47, -0.195, 0.19], [-0.33, -0.38, 0.335], [-0.33, -0.38, -0.27]], 0.05, { col: PAINT2, out: [-0.6, -0.8, 0] });+ kit.bolts(cR, 0.026, 1, 0.017);+ const cI = kit.plate([[-0.47, 0.21, -0.195], [-0.47, -0.19, -0.195], [-0.33, -0.36, -0.295], [-0.33, 0.39, -0.295]], 0.05, { col: PAINT2, out: [-0.6, 0, -0.8] });+ kit.bolts(cI, 0.026, 1, 0.017);++ // Outer face: three lames lapped like shingles, flaring outward toward the bottom.+ const L1 = kit.plate([[-0.35, -0.345, 0.345], [-0.35, 0.365, 0.345], [-0.05, 0.41, 0.39], [-0.05, -0.385, 0.39]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.bolts(L1, 0.03, 2, 0.019);+ kit.seam(L1, L1.at(0.34, 0.05), L1.at(0.34, 0.95));+ kit.ventOn(L1, 0.5, 0.9, 0.2, 0.78, { n: 4 });+ const L2 = kit.plate([[-0.10, -0.37, 0.37], [-0.10, 0.395, 0.37], [0.20, 0.40, 0.41], [0.20, -0.385, 0.41]], 0.05, { col: PAINT2, out: [0, 0, 1] });+ kit.bolts(L2, 0.03, 2, 0.019);+ const num = kit.plate([[-0.03, -0.20, 0.395], [-0.03, 0.22, 0.395], [0.17, 0.22, 0.423], [0.17, -0.20, 0.423]], 0.022, { col: BONE, out: [0, 0, 1], noBack: true, c: 0.01 });+ kit.bolts(num, 0.018, 1, 0.012);+ kit.stencil(num.at(0.1, 0.92, 0), [0, 1, 0], [-0.99, 0, -0.14], side === 'R' ? '07' : '12', 0.15, RUST, num.n);+ const L3 = kit.plate([[0.15, -0.365, 0.39], [0.15, 0.38, 0.39], [0.46, 0.27, 0.43], [0.46, -0.25, 0.43]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.bolts(L3, 0.03, 1, 0.019);+ kit.hazardPlate([L3.at(0.1, 0.58, 0.008), L3.at(0.9, 0.58, 0.008), L3.at(0.9, 0.94, 0.008), L3.at(0.1, 0.94, 0.008)], 0.02, { out: L3.n, sw: 0.05, c: 0.008, noBack: true });++ // Front: main plate and intake louvres. Rear: heat-sink fins. Inner: bolted cover. Underside: dark closing plate.+ const pF = kit.plate([[-0.33, 0.425, 0.335], [0.30, 0.405, 0.38], [0.30, 0.405, -0.26], [-0.33, 0.425, -0.27]], 0.05, { col: PAINT2, out: [0, 1, 0] });+ kit.bolts(pF, 0.03, 1, 0.019);+ kit.ventOn(pF, 0.06, 0.52, 0.14, 0.86, { n: 5 });+ kit.seam(pF, pF.at(0.6, 0.05), pF.at(0.6, 0.95));+ const pR = kit.plate([[-0.33, -0.395, 0.335], [0.22, -0.385, 0.38], [0.22, -0.385, -0.26], [-0.33, -0.395, -0.27]], 0.05, { col: GUN, out: [0, -1, 0] });+ kit.bolts(pR, 0.028, 1, 0.017);+ kit.fins(pR, 0.1, 0.62, 0.18, 0.82, 7, 0.05, GM);+ const pI = kit.plate([[-0.33, 0.39, -0.30], [0.26, 0.37, -0.30], [0.26, -0.35, -0.30], [-0.33, -0.36, -0.30]], 0.04, { col: PAINT2, out: [0, 0, -1] });+ kit.bolts(pI, 0.03, 1, 0.017); kit.loop(pI, 0.06);+ kit.plate([[0.32, 0.38, 0.38], [0.32, -0.36, 0.38], [0.32, -0.34, -0.24], [0.32, 0.36, -0.24]], 0.03, { col: GUN, out: [1, 0, 0], noDrip: true });++ // Lower arm: faceted shell below the pauldron.+ const sec = kit.shell([+ { u: 0.40, vf: 0.24, vr: -0.25, wo: 0.25, wi: 0.25, c: 0.05 },+ { u: 0.72, vf: 0.20, vr: -0.21, wo: 0.22, wi: 0.22, c: 0.05 },+ { u: 0.96, vf: 0.17, vr: -0.17, wo: 0.19, wi: 0.19, c: 0.04 },+ ], { cols: { out: PAINT, rear: GUN, in: PAINT2, front: PAINT2, chamfer: PAINT2 } });+ kit.bolts(sec[1].out, 0.025, 1, 0.016); kit.seam(sec[1].out, sec[1].out.at(0.08, 0.5), sec[1].out.at(0.92, 0.5));+ kit.bolts(sec[1].front, 0.024, 1, 0.014);+ kit.bolts(sec[0].front, 0.024, 1, 0.014);+ kit.streak(sec[1].out, [0.56, -0.02, 0.25], 0.28, 0.07, 'oil');+ kit.mat('rubber');+ kit.cables([[0.40, -0.30, 0.33], [0.56, -0.29, 0.30], [0.74, -0.24, 0.285], [0.92, -0.18, 0.265], [F.L, 0, 0.29]], 0.025, [{ r: 0.018, col: CAB_K }, { r: 0.014, col: CAB_R }]);+ kit.mat('armour');+ const A = kit.at(F, 0.25, -0.46, 0), Ab = kit.at(F, 0.32, -0.26, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(A, Ab, z0, { col: GUN, w: 0.08 });+ kit.mirror(1);+ return { G, A };+}++function forearmBase(kit, s, z0) {+ kit.mirror(s);+ kit.frame(null); kit.mat('steel');+ kit.drum([SK.elbow[0], SK.elbow[1], z0], [0, 0, 1], 0.27, 0.56, { bolts: 8, gear: 20, gearW: 0.35, seg: 22 });+ kit.mat('armour');+}++function buildCannonArm(kit, parent, upperA) {+ const z0 = SK.armZ, y = SK.elbow[1];+ const G = kit.group('Elbow_R', parent, [SK.elbow[0], y, z0], 'Forearm_R');+ G.node.userData = { hinge: 'z' };+ kit.use(G); forearmBase(kit, 1, z0);+ kit.frame({ o: [0, y, z0], u: [1, 0, 0], v: [0, 1, 0], w: [0, 0, 1] });+ // Faceted gun housing: breech, receiver, taper and barrel shroud.+ const sec = kit.shell([+ { u: 0.04, vf: 0.22, vr: -0.22, wo: 0.26, wi: 0.26, c: 0.05 },+ { u: 0.38, vf: 0.27, vr: -0.25, wo: 0.31, wi: 0.30, c: 0.06 },+ { u: 0.74, vf: 0.27, vr: -0.25, wo: 0.31, wi: 0.30, c: 0.06 },+ { u: 0.96, vf: 0.24, vr: -0.22, wo: 0.26, wi: 0.26, c: 0.06 },+ { u: 1.10, vf: 0.255, vr: -0.255, wo: 0.255, wi: 0.255, c: 0.07 },+ ], { cols: { front: PAINT, out: PAINT, rear: GUN, in: PAINT2, chamfer: PAINT2 }, t: 0.028 });+ const [s0, s1, s2, s3] = sec;+ kit.ventOn(s0.front, 0.18, 0.82, 0.2, 0.8, { n: 5 });+ kit.bolts(s0.front, 0.024, 1, 0.017);+ const sp = kit.onPlate(s1.front, 0.2, 0.8, 0.05, 0.95, 0.016, { col: PAINT2 });+ kit.bolts(sp, 0.024, 2, 0.018);+ kit.seam(sp, sp.at(0.5, 0.04), sp.at(0.5, 0.96));+ kit.hazardPlate([s2.front.at(0.08, 0.12, 0.012), s2.front.at(0.92, 0.12, 0.012), s2.front.at(0.92, 0.88, 0.012), s2.front.at(0.08, 0.88, 0.012)], 0.024, { out: s2.front.n, sw: 0.06, noBack: true, c: 0.01 });+ kit.bolts(s3.front, 0.02, 1, 0.017);+ const rp = kit.onPlate(s1.out, 0.14, 0.86, 0.1, 0.9, 0.018, { col: RUST });+ kit.bolts(rp, 0.024, 1, 0.018);+ kit.ventOn(s0.out, 0.18, 0.82, 0.24, 0.8, { n: 4 });+ kit.ventOn(s2.out, 0.2, 0.8, 0.2, 0.8, { n: 3 });+ kit.bolts(s3.out, 0.02, 1, 0.017);+ kit.loop(s1.in, 0.05); kit.bolts(s1.in, 0.028, 1, 0.017); kit.bolts(s3.in, 0.02, 1, 0.014);+ kit.seam(s1.rear, s1.rear.at(0.5, 0.05), s1.rear.at(0.5, 0.95)); kit.bolts(s1.rear, 0.025, 1, 0.014);++ // Ammo drum and feed+ kit.frame(null); kit.hull(null); kit.mat('steel');+ const dc = [0.40, y - 0.52, z0];+ kit.drum(dc, [0, 0, 1], 0.27, 0.46, { bolts: 10, seg: 24, gear: 16, gearW: 0.8 });+ kit.block([0.40, y - 0.29, z0], [0.30, 0.14, 0.34], { col: GM_D, c: 0.02, edge: GM_L });+ for (let i = 0; i < 5; i++) kit.lathe([0.40, y - 0.52, z0 - 0.17 + i * 0.085], [0, 0, 1], [[0, 0.272, GM], [0, 0.285, GM_H, 1], [0.018, 0.285, GM_D], [0.018, 0.272, GM_D]], { seg: 24 });+ kit.mat('armour');+ kit.hazardPlate([[0.14, y - 0.70, z0 + 0.24], [0.66, y - 0.70, z0 + 0.24], [0.66, y - 0.82, z0 + 0.24], [0.14, y - 0.82, z0 + 0.24]], 0.012, { sw: 0.05, out: [0, 0, 1], c: 0.006, noBack: true });+ kit.mat('rubber');+ kit.tube([[0.66, y - 0.42, z0 + 0.26], [0.86, y - 0.34, z0 + 0.30], [0.98, y - 0.24, z0 + 0.20], [1.03, y - 0.15, z0 + 0.05], [1.05, y, z0]], 0.03, { col: CAB_K, seg: 8 });+ kit.tube([[0.10, y + 0.02, z0 + 0.335], [0.34, y + 0.20, z0 + 0.378], [0.70, y + 0.21, z0 + 0.378], [0.90, y + 0.10, z0 + 0.30], [1.04, y + 0.03, z0 + 0.12], [1.05, y, z0]], 0.016, { col: CAB_R });+ kit.tube([[0.10, y - 0.02, z0 + 0.335], [0.34, y - 0.19, z0 + 0.378], [0.70, y - 0.20, z0 + 0.378], [0.90, y - 0.10, z0 + 0.30], [1.04, y - 0.03, z0 + 0.12], [1.05, y, z0]], 0.016, { col: CAB_Y });+ kit.mat('armour');+ const horn = [-0.46, y + 0.10, z0];+ kit.lug(horn, [-0.16, y + 0.06, z0], z0, { ear: 0.075, w: 0.10, foot: 0.13, t: 0.04 });+ kit.piston('Elbow_R', parent, [upperA[0], upperA[1], z0], G, horn, { r: 0.075, rod: 0.044 });++ // Rotary barrels spin about local X. Bare gunmetal, heat-blued toward the muzzle, polished at every shoulder.+ const spinG = kit.group('CannonSpin_R', G, [SK.cannon[0], SK.cannon[1], z0], 'CannonBarrels_R');+ spinG.node.userData = { hinge: 'x' };+ kit.use(spinG); kit.mat('steel');+ const sx = SK.cannon[0], R = 0.135, Lb = 1.36;+ kit.lathe([sx + 0.05, y, z0], [1, 0, 0], [+ [0, 0, GM_L], [0, 0.205, GM_H, 1], [0.012, 0.22, GM], [0.10, 0.22, GM_H, 1], [0.115, 0.205, GM_D], [0.12, 0.16, GM], [0.32, 0.16, GM_H, 1], [0.335, 0.14, GM_D],+ [0.34, 0.06, GM_L], [Lb, 0.06, GM_L], [Lb, 0, GM_L],+ ], { seg: 22 });+ for (const d of [0.16, 0.22, 0.28]) kit.lathe([sx + d, y, z0], [1, 0, 0], [[0, 0.16, GM_D], [0.004, 0.178, GM_H, 1], [0.016, 0.178, GM_D], [0.02, 0.16, GM_D]], { seg: 22 });+ for (let i = 0; i < 6; i++) {+ const th = (i / 6) * 2 * Math.PI, cy = y + R * Math.cos(th), cz = z0 + R * Math.sin(th);+ kit.lathe([sx + 0.28, cy, cz], [1, 0, 0], [+ [0, 0.046, GM, 0, GM], [Lb - 0.62, 0.046, GM, 0, HEAT_A], [Lb - 0.20, 0.046, GM_D], [Lb - 0.20, 0.058, GM_H, 1], [Lb - 0.19, 0.062, GM_D, 0, SOOT], [Lb - 0.07, 0.062, GM_H, 1],+ [Lb - 0.055, 0.052, GM_H, 1], [Lb - 0.05, 0.03, SOOT], [Lb - 0.16, 0.03, SOOT], [Lb - 0.16, 0, SOOT],+ ], { seg: 12 });+ }+ for (const d of [0.62, 1.00]) {+ kit.lathe([sx + d, y, z0], [1, 0, 0], [[0, 0, GM], [0, 0.205, GM_H, 1], [0.012, 0.218, GM], [0.052, 0.218, GM_H, 1], [0.064, 0.205, GM_D], [0.064, 0, GM_D]], { seg: 22 });+ for (let k = 0; k < 6; k++) {+ const a = ((k + 0.5) / 6) * 2 * Math.PI;+ kit.bolt([sx + d - 0.004, y + 0.192 * Math.cos(a), z0 + 0.192 * Math.sin(a)], [-1, 0, 0], 0.016, GM_L);+ }+ }+ kit.lathe([sx + Lb - 0.09, y, z0], [1, 0, 0], [[0, 0.222, GM_H, 1], [0.012, 0.222, GM_D], [0.05, 0.222, GM_H, 1], [0.06, 0.205, GM_D]], { seg: 22 });+ kit.mat('armour');+ return { G, spinG };+}++function buildClawArm(kit, parent, upperA) {+ const z0 = SK.armZ, y = SK.elbow[1];+ const G = kit.group('Elbow_L', parent, [SK.elbow[0], y, -z0], 'Forearm_L');+ G.node.userData = { hinge: 'z' };+ kit.use(G); forearmBase(kit, -1, z0);+ kit.frame({ o: [0, y, z0], u: [1, 0, 0], v: [0, 1, 0], w: [0, 0, 1] });+ const [s0, s1, s2] = kit.shell([+ { u: 0.04, vf: 0.24, vr: -0.24, wo: 0.26, wi: 0.26, c: 0.05 },+ { u: 0.54, vf: 0.31, vr: -0.31, wo: 0.33, wi: 0.31, c: 0.07 },+ { u: 0.78, vf: 0.28, vr: -0.28, wo: 0.30, wi: 0.28, c: 0.07 },+ { u: 0.96, vf: 0.20, vr: -0.20, wo: 0.23, wi: 0.23, c: 0.05 },+ ], { cols: { front: PAINT, out: PAINT, rear: GUN, in: PAINT2, chamfer: PAINT2 } });+ kit.ventOn(s0.front, 0.2, 0.8, 0.14, 0.5, { n: 4 });+ const tp = kit.onPlate(s0.front, 0.14, 0.86, 0.56, 0.96, 0.016, { col: PAINT2 });+ kit.bolts(tp, 0.022, 1, 0.017);+ const sp = kit.onPlate(s1.front, 0.16, 0.84, 0.08, 0.92, 0.016, { col: PAINT2 });+ kit.bolts(sp, 0.022, 1, 0.018);+ kit.bolts(s2.front, 0.024, 1, 0.017);+ const so = kit.onPlate(s0.out, 0.14, 0.86, 0.14, 0.86, 0.016, { col: PAINT2 });+ kit.bolts(so, 0.024, 1, 0.018);+ const so0 = so.at(0.8, 0.28, 0), rD = vUnit(vSub(so.at(0.8, 0.8, 0), so0)), uD = vUnit(vSub(so.at(0.2, 0.28, 0), so0));+ kit.stencil(so0, rD, uD, 'H-7', 0.13, BONE, so.n);+ kit.hazardPlate([s1.out.at(0.1, 0.12, 0.012), s1.out.at(0.9, 0.12, 0.012), s1.out.at(0.9, 0.88, 0.012), s1.out.at(0.1, 0.88, 0.012)], 0.024, { out: s1.out.n, sw: 0.055, noBack: true, c: 0.01 });+ kit.ventOn(s2.out, 0.2, 0.8, 0.2, 0.8, { n: 3 });+ kit.loop(s0.in, 0.05); kit.bolts(s0.in, 0.028, 1, 0.017); kit.bolts(s1.in, 0.024, 1, 0.014);+ kit.seam(s0.rear, s0.rear.at(0.5, 0.05), s0.rear.at(0.5, 0.95)); kit.bolts(s0.rear, 0.025, 1, 0.014);+ kit.frame(null); kit.hull(null);+ kit.mat('rubber');+ kit.cables([[0.10, y - 0.02, z0 + 0.345], [0.32, y - 0.25, z0 + 0.40], [0.66, y - 0.25, z0 + 0.39], [0.86, y - 0.20, z0 + 0.27], [0.96, y, z0 + 0.18]], 0.022, [{ r: 0.018, col: CAB_K }, { r: 0.015, col: CAB_Y }]);+ kit.mat('armour');+ const horn = [-0.46, y + 0.10, z0];+ kit.lug(horn, [-0.16, y + 0.06, z0], z0, { ear: 0.075, w: 0.10, foot: 0.13, t: 0.04 });+ kit.piston('Elbow_L', parent, [upperA[0], upperA[1], z0], G, horn, { r: 0.075, rod: 0.044 });++ // Wrist and claw (mirrored into -Z)+ const wG = kit.group('Wrist_L', G, [SK.wrist[0], SK.wrist[1], -z0], 'Palm_L');+ wG.node.userData = { hinge: 'x' };+ kit.use(wG); kit.mat('steel');+ const wx = SK.wrist[0];+ kit.lathe([wx - 0.02, y, z0], [1, 0, 0], [[0, 0.19, GM], [0.05, 0.20, GM_H, 1], [0.06, 0.20, GM_D], [0.11, 0.17, GM_H, 1], [0.13, 0.15, GM_D]], { seg: 22 });+ kit.mat('armour');+ kit.hull([wx + 0.2, y, z0]);+ kit.block([wx + 0.24, y, z0], [0.26, 0.38, 0.46], { col: DARK, c: 0.04 });+ const pt = kit.plate([[wx + 0.11, y + 0.215, z0 + 0.24], [wx + 0.37, y + 0.215, z0 + 0.24], [wx + 0.37, y + 0.215, z0 - 0.24], [wx + 0.11, y + 0.215, z0 - 0.24]], 0.03, { col: PAINT, out: [0, 1, 0] });+ kit.bolts(pt, 0.02, 1, 0.016);+ kit.ventOn(pt, 0.2, 0.8, 0.22, 0.78, { n: 3 });+ kit.hazardPlate([[wx + 0.11, y - 0.14, z0 + 0.245], [wx + 0.37, y - 0.14, z0 + 0.245], [wx + 0.37, y + 0.14, z0 + 0.245], [wx + 0.11, y + 0.14, z0 + 0.245]], 0.02, { sw: 0.04, out: [0, 0, 1] });+ const pi = kit.plate([[wx + 0.11, y + 0.14, z0 - 0.245], [wx + 0.37, y + 0.14, z0 - 0.245], [wx + 0.37, y - 0.14, z0 - 0.245], [wx + 0.11, y - 0.14, z0 - 0.245]], 0.02, { col: GUN, out: [0, 0, -1] });+ kit.bolts(pi, 0.02, 1, 0.014);+ const pb = kit.plate([[wx + 0.11, y - 0.205, z0 + 0.23], [wx + 0.37, y - 0.205, z0 + 0.23], [wx + 0.37, y - 0.205, z0 - 0.23], [wx + 0.11, y - 0.205, z0 - 0.23]], 0.03, { col: GUN, out: [0, -1, 0] });+ kit.bolts(pb, 0.02, 1, 0.014);+ const hx = wx + 0.35;+ const finger = (name, hy, hz, sgnY, wid) => {+ const fG = kit.group(name, wG, [hx, y + hy, -(z0 + hz)], 'Claw_' + name);+ fG.node.userData = { hinge: 'z' };+ kit.use(fG); kit.mat('steel');+ kit.drum([hx, y + hy, z0 + hz], [0, 0, 1], 0.075, wid + 0.03, { bolts: 0, seg: 14 });+ kit.mat('armour');+ const pts = [[-0.07, 0.07], [0.30, 0.15], [0.60, 0.11], [0.84, -0.09], [0.90, -0.24], [0.74, -0.15], [0.54, -0.05], [0.30, -0.035], [-0.07, -0.07]];+ const map = (w) => pts.map(([x, py]) => [hx + x, y + hy + sgnY * py, z0 + hz + w]);+ kit.plate(map(wid / 2), wid / 2, { out: [0, 0, 1], col: PAINT2, c: 0.02 });+ kit.plate(map(-wid / 2), wid / 2, { out: [0, 0, -1], col: PAINT2, c: 0.02 });+ // Shingled back plates along the finger+ const zc = z0 + hz, wz = wid * 0.56;+ const seg = (x0, h0, x1, h1) => [[hx + x0, y + hy + sgnY * h0, zc + wz], [hx + x1, y + hy + sgnY * h1, zc + wz], [hx + x1, y + hy + sgnY * h1, zc - wz], [hx + x0, y + hy + sgnY * h0, zc - wz]];+ const d1 = kit.plate(seg(-0.02, 0.095, 0.33, 0.185), 0.04, { out: [0, sgnY, 0], col: PAINT, c: 0.014 });+ kit.bolts(d1, 0.02, 1, 0.015);+ const d2 = kit.plate(seg(0.28, 0.155, 0.60, 0.15), 0.04, { out: [0, sgnY, 0], col: PAINT2, c: 0.014 });+ kit.hazardPlate([d2.at(0.3, 0.1, 0.006), d2.at(0.95, 0.1, 0.006), d2.at(0.95, 0.9, 0.006), d2.at(0.3, 0.9, 0.006)], 0.016, { out: d2.n, sw: 0.03, noBack: true, c: 0.005 });+ kit.mat('steel');+ const tipA = [[0.70, -0.02], [0.88, -0.12], [0.905, -0.25], [0.74, -0.155]].map(([x, py]) => [hx + x, y + hy + sgnY * py, zc]);+ kit.plate(tipA.map((p) => [p[0], p[1], p[2] + wid / 2 + 0.008]), wid / 2 + 0.008, { out: [0, 0, 1], col: GM_L, c: 0.02, polish: true });+ kit.plate(tipA.map((p) => [p[0], p[1], p[2] - wid / 2 - 0.008]), wid / 2 + 0.008, { out: [0, 0, -1], col: GM_L, c: 0.02, polish: true });+ kit.mat('armour');+ return fG;+ };+ kit.frame(null);+ finger('ClawUpperOuter_L', 0.13, 0.16, 1, 0.13);+ finger('ClawUpperInner_L', 0.13, -0.16, 1, 0.13);+ finger('ClawThumb_L', -0.13, 0, -1, 0.16);+ kit.mirror(1);+ return { G };+}++// ---------------------------------------------------------------- missile pod+function buildPod(kit, waistG) {+ const G = kit.group('MissilePod', waistG, [-0.05, 5.50, -1.42], 'MissilePod');+ G.node.userData = { hinge: 'z' };+ kit.use(G); kit.frame(null); kit.mirror(-1);+ const zc = 1.46, x0 = -0.62, x1 = 0.62, y0 = 5.54, y1 = 5.97, hz = 0.42;+ kit.hull([0, (y0 + y1) / 2, zc]); kit.mat('armour');+ const r = (x, ya, yb, h) => [[x, yb, zc + h], [x, yb, zc - h], [x, ya, zc - h], [x, ya, zc + h]];+ kit.loft([r(x0, y0 + 0.04, y1 - 0.06, hz - 0.06), r(x0 + 0.12, y0, y1, hz), r(x1 - 0.03, y0, y1, hz), r(x1, y0 + 0.02, y1 - 0.02, hz - 0.02)], { col: DARK });+ const top = kit.plate([[x0 + 0.12, y1 + 0.02, zc + hz], [x1 - 0.14, y1 + 0.02, zc + hz], [x1 - 0.14, y1 + 0.02, zc - hz], [x0 + 0.12, y1 + 0.02, zc - hz]], 0.03, { col: PAINT, out: [0, 1, 0] });+ kit.loop(top, 0.05); kit.bolts(top, 0.028, 2, 0.014);+ kit.hazardPlate([[x1 - 0.13, y1 + 0.02, zc + hz], [x1 - 0.13, y1 + 0.02, zc - hz], [x1 + 0.02, y1 + 0.005, zc - hz], [x1 + 0.02, y1 + 0.005, zc + hz]], 0.02, { sw: 0.07, out: [0, 1, 0] });+ for (const sg of [1, -1]) {+ const so = kit.plate([[x0 + 0.14, y1 - 0.03, zc + sg * (hz + 0.025)], [x1 - 0.06, y1 - 0.03, zc + sg * (hz + 0.025)], [x1 - 0.06, y0 + 0.03, zc + sg * (hz + 0.025)], [x0 + 0.14, y0 + 0.03, zc + sg * (hz + 0.025)]], 0.03, { col: sg > 0 ? PAINT : PAINT2, out: [0, 0, sg] });+ kit.loop(so, 0.045); kit.bolts(so, 0.026, 2, 0.014);+ if (sg > 0) {+ const s2 = kit.plate([[x0 + 0.26, y1 - 0.12, zc + hz + 0.05], [x0 + 0.72, y1 - 0.12, zc + hz + 0.05], [x0 + 0.72, y0 + 0.11, zc + hz + 0.05], [x0 + 0.26, y0 + 0.11, zc + hz + 0.05]], 0.025, { col: RUST, out: [0, 0, 1] });+ kit.bolts(s2, 0.018, 1, 0.01);+ kit.stencil([x0 + 0.84, y0 + 0.13, zc + hz + 0.025], [1, 0, 0], [0, 1, 0], 'HE', 0.14, BONE, [0, 0, 1]);+ }+ }+ kit.plate([[x0 + 0.1, y0 - 0.015, zc + hz], [x1 - 0.03, y0 - 0.015, zc + hz], [x1 - 0.03, y0 - 0.015, zc - hz], [x0 + 0.1, y0 - 0.015, zc - hz]], 0.02, { col: GUN, out: [0, -1, 0] });+ // Rear vent+ kit.vent([x0 - 0.008, y0 + 0.08, zc - 0.28], [x0 - 0.008, y0 + 0.08, zc + 0.28], [x0 - 0.008, y1 - 0.10, zc + 0.28], [x0 - 0.008, y1 - 0.10, zc - 0.28], { n: 5, col: GUN });+ // Launch tubes, two rows of four+ const front = x1 + 0.002;+ kit.poly([[front, y1 - 0.02, zc + hz - 0.02], [front, y1 - 0.02, zc - hz + 0.02], [front, y0 + 0.02, zc - hz + 0.02], [front, y0 + 0.02, zc + hz - 0.02]], GUN, { out: [1, 0, 0] });+ kit.mat('steel');+ for (let row = 0; row < 2; row++) for (let col = 0; col < 4; col++) {+ const cy = (y0 + y1) / 2 + (row - 0.5) * 0.205, cz = zc + (col - 1.5) * 0.195;+ kit.lathe([front, cy, cz], [1, 0, 0], [[0, 0.088, STEEL_D], [0.022, 0.088, vMix(STEEL_D, WEAR, 0.4)], [0.022, 0.072, tone(GUN, 0.4)], [-0.06, 0.072, tone(GUN, 0.4)]], { seg: 12 });+ kit.mat('rubber');+ kit.lathe([front - 0.05, cy, cz], [1, 0, 0], [[0, 0.064, BONE], [0.03, 0.062, BONE], [0.055, 0.044, RUST], [0.08, 0.018, RUST], [0.088, 0, RUST]], { seg: 10, mat: 'rubber' });+ kit.mat('steel');+ }+ // Trunnion+ kit.drum([-0.05, 5.50, 1.42], [0, 0, 1], 0.09, 0.22, { bolts: 0, seg: 14 });+ kit.mat('rubber');+ kit.tube([[-0.40, y0 - 0.01, zc + 0.10], [-0.32, 5.50, 1.42], [-0.15, 5.47, 1.38], [-0.05, 5.50, 1.36]], 0.018, { col: CAB_K });+ kit.mat('armour');+ kit.mirror(1);+}++// ---------------------------------------------------------------- build+async function build() {+ const root = createRoot('HeavyWalkerMech');+ const tex = makeTextures();+ const MAT = {+ armour: pbrMaterial({ albedo: tex.albedo, metallicRoughness: tex.mr, normal: tex.nrm, roughness: 1, metalness: 1, name: 'Armour_WeatheredPaint' }),+ // Gunmetal: colour comes from vertex colours, finish from the map. Metalness stays a little under 1 so+ // machinery keeps some diffuse read in scenes with no environment map; the polished band takes the sharp highlights.+ steel: pbrMaterial({ metallicRoughness: tex.steelMR, roughness: 1, metalness: 1, name: 'Gunmetal_Machinery' }),+ decal: pbrMaterial({ albedo: tex.wearA, metallicRoughness: tex.wearMR, roughness: 1, metalness: 1, alphaMode: 'blend', name: 'Wear_Overlay' }),+ chrome: gameMaterial(0xf0f2f4, { metalness: 0.72, roughness: 0.22, flatShading: false, name: 'Chrome_PistonRod' }),+ rubber: gameMaterial(0xffffff, { metalness: 0, roughness: 0.78, flatShading: false, name: 'Rubber_CableAndTrim' }),+ glass: glassMaterial(0x4f8598, { opacity: 0.32, roughness: 0.05, metalness: 0.2, name: 'Canopy_Glass' }),+ glowCyan: gameMaterial(0x06131a, { emissive: 0x2cc4f5, emissiveIntensity: 1.5, metalness: 0, roughness: 0.4, flatShading: false, name: 'Glow_Cyan' }),+ glowAmber: gameMaterial(0x1a0c02, { emissive: 0xff6408, emissiveIntensity: 1.15, metalness: 0, roughness: 0.4, flatShading: false, name: 'Glow_Amber' }),+ };+ const kit = createKit(root, MAT, { armour: true, steel: true, rubber: true, decal: true }, { armour: true, steel: true, decal: true });++ const pelvisG = kit.group('Pelvis', null, [SK.hip[0], SK.hip[1], 0], 'Pelvis');+ buildPelvis(kit, pelvisG);+ buildLeg(kit, 'L', pelvisG);+ buildLeg(kit, 'R', pelvisG);+ const waistG = kit.group('Waist', pelvisG, [0, SK.waistY, 0], 'Torso');+ waistG.node.userData = { hinge: 'y (yaw); clip also rolls/pitches slightly' };+ buildTorso(kit, waistG);+ const ua = { L: buildUpperArm(kit, 'L', waistG), R: buildUpperArm(kit, 'R', waistG) };+ buildCannonArm(kit, ua.R.G, ua.R.A);+ buildClawArm(kit, ua.L.G, ua.L.A);+ buildPod(kit, waistG);++ // Size each piston from its real span across the Walk cycle (with margin for code-driven poses).+ const N = rigNodes(root);+ const rest = rigList(N).map((n) => [n, n.position.clone(), n.quaternion.clone()]);+ const worldA = new THREE.Vector3(), worldB = new THREE.Vector3();+ const stats = kit.pistonList().map((p) => {+ const s = p.mir;+ const la = new THREE.Vector3(p.A[0] - p.gA.origin[0], p.A[1] - p.gA.origin[1], p.A[2] * s - p.gA.origin[2]);+ const lb = new THREE.Vector3(p.B[0] - p.gB.origin[0], p.B[1] - p.gB.origin[1], p.B[2] * s - p.gB.origin[2]);+ return { p, la, lb, min: Infinity, max: 0 };+ });+ for (let i = 0; i < 30; i++) {+ applyPose(N, walkPose(i / 30));+ root.updateMatrixWorld(true);+ for (const st of stats) {+ const d = st.p.gA.node.localToWorld(worldA.copy(st.la)).distanceTo(st.p.gB.node.localToWorld(worldB.copy(st.lb)));+ st.min = Math.min(st.min, d); st.max = Math.max(st.max, d);+ }+ }+ rest.forEach(([n, pos, q]) => { n.position.copy(pos); n.quaternion.copy(q); });+ root.updateMatrixWorld(true);+ for (const st of stats) kit.buildPiston(st.p, st.min - 0.04, st.max + 0.05);++ kit.flush();+ root.userData = { rig: { clip: 'Walk', walkSpeedMps: Number(WALK.speed.toFixed(4)), strideM: Number(WALK.stride.toFixed(4)), durationS: WALK.T, forward: '+X', hingeAxis: 'local Z unless the joint says otherwise' } };+ return root;+}++// ---------------------------------------------------------------- Walk clip+function animate(root) {+ const N = rigNodes(root);+ const aimers = [];+ root.traverse((n) => { if (n.userData && n.userData.aimAt) aimers.push(n); });+ const nodes = rigList(N);+ const rest = nodes.concat(aimers).map((n) => [n, n.position.clone(), n.quaternion.clone()]);+ const names = nodes.concat(aimers);+ const keyRot = new Map(names.map((n) => [n.name, []]));+ const keyPos = [];+ const eul = new THREE.Euler(), wp = new THREE.Vector3(), dir = new THREE.Vector3(), restDir = new THREE.Vector3();+ for (let i = 0; i <= WALK.keys; i++) {+ const phi = (i % WALK.keys) / WALK.keys, t = (WALK.T * i) / WALK.keys;+ applyPose(N, walkPose(phi));+ root.updateMatrixWorld(true);+ for (const n of aimers) {+ const target = root.getObjectByName(n.userData.aimAt);+ target.getWorldPosition(wp);+ n.parent.worldToLocal(wp);+ dir.copy(wp).sub(n.position).normalize();+ const r = n.userData.aimRestDir;+ restDir.set(r[0], r[1], r[2]);+ n.quaternion.setFromUnitVectors(restDir, dir);+ }+ for (const n of names) {+ eul.setFromQuaternion(n.quaternion, 'XYZ');+ keyRot.get(n.name).push({ time: t, rotation: [eul.x / D2R, eul.y / D2R, eul.z / D2R] });+ }+ keyPos.push({ time: t, position: [N.pelvis.position.x, N.pelvis.position.y, N.pelvis.position.z] });+ }+ rest.forEach(([n, pos, q]) => { n.position.copy(pos); n.quaternion.copy(q); });+ root.updateMatrixWorld(true);+ const tracks = [positionTrack(N.pelvis.name, keyPos)];+ for (const n of names) tracks.push(rotationTrack(n.name, keyRot.get(n.name)));+ return [createClip('Walk', WALK.T, tracks, { loop: true })];+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-3.kiln.js b/site/public/text-to-3d/5833329625b2f4ed/mech-3.kiln.jsnew file mode 100644index 00000000..7559510a--- /dev/null+++ b/site/public/text-to-3d/5833329625b2f4ed/mech-3.kiln.js@@ -0,0 +1,1814 @@+// Heavy walking mech, ~6 m. Metres, +X forward, +Y up, +Z right. L = -Z, R = +Z.+// Every joint rests at identity rotation with asset-aligned axes, so a hinge is+// simply node.rotation.z (positive tips +X up to +Y; a hanging limb swings forward).+const meta = { name: 'Heavy Walker Mech', role: 'vehicle' };++const SK = {+ hip: [0, 3.42], knee: [0.70, 2.17], ankle: [-0.60, 1.12], toe: [0, 0.22],+ tipX: 0.95, toeH: 0.22, legZ: 0.86,+ waistY: 3.86,+ shoulder: [0.02, 5.00], elbow: [-0.12, 4.00], armZ: 1.60,+ cannon: [1.00, 4.00], wrist: [0.95, 4.00],+};++const WALK = { T: 2.4, keys: 120, duty: 0.64, stanceLen: 1.72, lift: 0.46, xC: 0.95, height: 3.34, bob: 0.055, sway: 0.11, surge: 0.03 };+WALK.stride = WALK.stanceLen / WALK.duty;+WALK.speed = WALK.stride / WALK.T;++const D2R = Math.PI / 180;+const TILE = 2.2;+const RAIL_SEED = 41077, SEED_AFTER_WRIST = 3018216793;++// ---------------------------------------------------------------- vectors+const vAdd = (a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[2]];+const vSub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];+const vMul = (a, k) => [a[0] * k, a[1] * k, a[2] * k];+const vDot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];+const vCross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];+const vLen = (a) => Math.hypot(a[0], a[1], a[2]);+const vUnit = (a) => { const l = vLen(a) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };+const vMix = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];+const vPerp = (d) => { const a = Math.abs(d[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]; return vUnit(vCross(a, d)); };++// ---------------------------------------------------------------- colour (linear)+const lin = (hex) => {+ const f = (c) => { const x = c / 255; return x <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4); };+ return [f((hex >> 16) & 255), f((hex >> 8) & 255), f(hex & 255)];+};+const tone = (c, k) => [c[0] * k, c[1] * k, c[2] * k];+const PAINT = lin(0x87886f), PAINT2 = lin(0x6e715d), DARK = lin(0x4b4e4f), GUN = lin(0x404447), BONE = lin(0xcdc6ad), RUST = lin(0xa84a2c), PISTON = lin(0x6f7376), SUN = lin(0xaeac96);+// Gunmetal for machinery, and the wear palette+const GM = lin(0x7a8089), GM_D = lin(0x43474c), GM_L = lin(0xa4abb3), GM_H = lin(0xc9ced3), HEAT_A = lin(0x515c78), BARE = lin(0xa2a6ab), COPPER = lin(0x5a3019);+const BATCH_A = lin(0x7c8a62), BATCH_B = lin(0x90866a), DUST_D = lin(0x7a6c56), DUST_L = lin(0x8d7f68), OIL = lin(0x0d0b09), RUST_D = lin(0x74401f), GRIME = lin(0x15110d), SOOT = lin(0x0b0b0c);+// Wear atlas rows in texture V (see makeTextures), and the steel map's polished band+const CHIP_V = 0.195, CHIP_DEPTH = 0.135, GRIME_V0 = 0.25, GRIME_MID = 0.3125, GRIME_END = 0.375, RUN_V0 = 0.376, RUN_V1 = 0.874, DUST_V0 = 0.876, POLISH_V = 0.04, STEEL_V = 0.13;+const HAZ_Y = lin(0xe8ae1c), HAZ_K = lin(0x1a1b1d), SEAM = lin(0x0a0b0c), WEAR = lin(0xe6e3d4), STEEL = lin(0xbcc1c5), STEEL_D = lin(0x7f8387);+const RUB = lin(0x1c1d1f), CAB_R = lin(0xa8361f), CAB_Y = lin(0xc99a1e), CAB_K = lin(0x202225), CAB_B = lin(0x2d4f63), SUIT = lin(0x8f4f22), WHITE = [1, 1, 1];++// ---------------------------------------------------------------- walk solve+function pchipPeriodic(keys) {+ const n = keys.length, xs = keys.map((k) => k[0]), ys = keys.map((k) => k[1]);+ const h = (i) => (i + 1 < n ? xs[i + 1] : xs[0] + 1) - xs[i];+ const sl = (i) => (ys[(i + 1) % n] - ys[i]) / h(i);+ const m = xs.map((_, i) => {+ const p = (i + n - 1) % n, s0 = sl(p), s1 = sl(i);+ if (s0 * s1 <= 0) return 0;+ const h0 = h(p), h1 = h(i), w1 = 2 * h1 + h0, w2 = h1 + 2 * h0;+ return (w1 + w2) / (w1 / s0 + w2 / s1);+ });+ return (fIn) => {+ const f = fIn - Math.floor(fIn);+ let i = n - 1;+ for (let j = 0; j < n; j++) if (xs[j] <= f) i = j;+ const hh = h(i), fx = f < xs[i] ? f + 1 : f, t = (fx - xs[i]) / hh, t2 = t * t, t3 = t2 * t;+ return (2 * t3 - 3 * t2 + 1) * ys[i] + (t3 - 2 * t2 + t) * m[i] * hh + (-2 * t3 + 3 * t2) * ys[(i + 1) % n] + (t3 - t2) * m[(i + 1) % n] * hh;+ };+}+const b0 = WALK.duty;+const swingF = (s) => b0 + (1 - b0) * s;+// Foot pitch never goes positive: the toes stay lowest and set down first, then the sole rolls flat.+const PSI = pchipPeriodic([[0, 0], [0.50, 0], [b0, -24], [swingF(0.22), -27], [swingF(0.5), -13], [swingF(0.78), -4], [swingF(0.93), -0.8]]);+const GAMMA = pchipPeriodic([[0, 50], [0.32, 34], [0.50, 23], [b0, 22], [swingF(0.38), 50], [swingF(0.72), 57]]);++function footState(f) {+ const xf = WALK.xC + WALK.stanceLen / 2, S = WALK.stride;+ let cx, cy = 0;+ if (f < b0) cx = xf - S * f;+ else {+ const s = (f - b0) / (1 - b0), x0 = xf - WALK.stanceLen, x1 = xf, m = -S * (1 - b0), s2 = s * s, s3 = s2 * s;+ cx = (2 * s3 - 3 * s2 + 1) * x0 + (s3 - 2 * s2 + s) * m + (-2 * s3 + 3 * s2) * x1 + (s3 - s2) * m;+ // Zero height velocity at both ends with finite curvature; the peak falls early in the swing.+ const b = Math.sin(Math.PI * s * (1.35 - 0.35 * s));+ cy = WALK.lift * b * b;+ }+ return { cx, cy, psi: PSI(f) * D2R };+}++const wrapA = (a) => { let x = a; while (x > Math.PI) x -= 2 * Math.PI; while (x < -Math.PI) x += 2 * Math.PI; return x; };+const bone2 = (a, b) => [b[0] - a[0], b[1] - a[1]];+const BT = bone2(SK.hip, SK.knee), BS = bone2(SK.knee, SK.ankle), BM = bone2(SK.ankle, SK.toe);+const LT = Math.hypot(BT[0], BT[1]), LS = Math.hypot(BS[0], BS[1]), LM = Math.hypot(BM[0], BM[1]);+const AT0 = Math.atan2(BT[1], BT[0]), AS0 = Math.atan2(BS[1], BS[0]), AM0 = Math.atan2(BM[1], BM[0]);++function eulerQ(xDeg, yDeg, zDeg) {+ return new THREE.Quaternion().setFromEuler(new THREE.Euler(xDeg * D2R, yDeg * D2R, zDeg * D2R, 'YXZ'));+}+const qx = (r) => new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1, 0, 0), r);+const qz = (r) => new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), r);++function solveLeg(sgn, f, pelvisPos, qP) {+ const foot = footState(f);+ const cp = Math.cos(foot.psi), sp = Math.sin(foot.psi);+ const px = foot.cx + (-SK.tipX * cp - SK.toeH * sp);+ const py = foot.cy + (-SK.tipX * sp + SK.toeH * cp);+ const pz = sgn * SK.legZ;+ const hipOff = new THREE.Vector3(0, 0, sgn * SK.legZ).applyQuaternion(qP);+ const dx = px - (pelvisPos[0] + hipOff.x), dy = py - (pelvisPos[1] + hipOff.y), dz = pz - (pelvisPos[2] + hipOff.z);+ const y2 = -Math.hypot(dy, dz);+ const rho = Math.atan2(dz / y2, dy / y2);+ const g = GAMMA(f) * D2R;+ const ax = dx - LM * Math.sin(g), ay = y2 + LM * Math.cos(g);+ const d = Math.min(Math.hypot(ax, ay), (LT + LS) * 0.995);+ const a = Math.acos(Math.max(-1, Math.min(1, (LT * LT + d * d - LS * LS) / (2 * LT * d))));+ const aT = Math.atan2(ay, ax) + a;+ const kx = LT * Math.cos(aT), ky = LT * Math.sin(aT);+ const aS = Math.atan2(ay - ky, ax - kx);+ const aM = Math.atan2(-Math.cos(g), Math.sin(g));+ const tH = wrapA(aT - AT0), tS = wrapA(aS - AS0), tM = wrapA(aM - AM0);+ const qHip = qP.clone().invert().multiply(qx(rho)).multiply(qz(tH));+ const qToe = qx(rho).multiply(qz(tM)).invert().multiply(qz(foot.psi));+ return { hip: qHip, knee: qz(wrapA(tS - tH)), ankle: qz(wrapA(tM - tS)), toe: qToe };+}++function walkPose(phi) {+ const tau = 2 * Math.PI, c1 = (a) => Math.cos(tau * (phi - a)), c2 = (a) => Math.cos(2 * tau * (phi - a));+ const pelvisPos = [WALK.surge * c2(0.20), WALK.height + WALK.bob * c2(0.34), -WALK.sway * c1(0.34)];+ const qP = eulerQ(-2.6 * c1(0.30), -5.5 * c1(0), -2.0 + 0.5 * c2(0.25));+ const fL = phi % 1, fR = (phi + 0.5) % 1;+ const shL = -8 * c1(0.04), shR = 6.5 * c1(0.04);+ return {+ pelvisPos, pelvisQ: qP,+ L: solveLeg(-1, fL, pelvisPos, qP), R: solveLeg(1, fR, pelvisPos, qP),+ waist: eulerQ(-1.9 * c1(0.38), 12.5 * c1(0.04), 2.0 + 0.4 * c2(0.30)),+ shoulderL: qz(shL * D2R), shoulderR: qz(shR * D2R),+ elbowL: qz(0.55 * 8 * c1(0.12) * D2R), elbowR: qz(-0.55 * 6.5 * c1(0.12) * D2R),+ pod: qz(1.1 * c2(0.42) * D2R),+ };+}++function rigNodes(root) {+ const n = (s) => root.getObjectByName('Joint_' + s);+ const o = { pelvis: n('Pelvis'), waist: n('Waist'), pod: n('MissilePod'), shoulderL: n('Shoulder_L'), shoulderR: n('Shoulder_R'), elbowL: n('Elbow_L'), elbowR: n('Elbow_R') };+ for (const s of ['L', 'R']) o[s] = { hip: n('Hip_' + s), knee: n('Knee_' + s), ankle: n('Ankle_' + s), toe: n('Toe_' + s) };+ return o;+}+function applyPose(N, p) {+ N.pelvis.position.set(p.pelvisPos[0], p.pelvisPos[1], p.pelvisPos[2]);+ N.pelvis.quaternion.copy(p.pelvisQ);+ N.waist.quaternion.copy(p.waist);+ N.pod.quaternion.copy(p.pod);+ N.shoulderL.quaternion.copy(p.shoulderL); N.shoulderR.quaternion.copy(p.shoulderR);+ N.elbowL.quaternion.copy(p.elbowL); N.elbowR.quaternion.copy(p.elbowR);+ for (const s of ['L', 'R']) for (const j of ['hip', 'knee', 'ankle', 'toe']) N[s][j].quaternion.copy(p[s][j]);+}+function rigList(N) {+ const out = [N.pelvis, N.waist, N.pod, N.shoulderL, N.shoulderR, N.elbowL, N.elbowR];+ for (const s of ['L', 'R']) for (const j of ['hip', 'knee', 'ankle', 'toe']) out.push(N[s][j]);+ return out;+}++// ---------------------------------------------------------------- mesh kit+function newell(P) {+ let x = 0, y = 0, z = 0;+ for (let i = 0; i < P.length; i++) {+ const a = P[i], b = P[(i + 1) % P.length];+ x += (a[1] - b[1]) * (a[2] + b[2]); y += (a[2] - b[2]) * (a[0] + b[0]); z += (a[0] - b[0]) * (a[1] + b[1]);+ }+ return vUnit([x, y, z]);+}+function centroid(P) { let c = [0, 0, 0]; for (const p of P) c = vAdd(c, p); return vMul(c, 1 / P.length); }+function triangulate(P, n) {+ const m = P.length;+ if (m === 3) return [[0, 1, 2]];+ let convex = true;+ for (let i = 0; i < m; i++) {+ const a = P[i], b = P[(i + 1) % m], c = P[(i + 2) % m];+ if (vDot(vCross(vSub(b, a), vSub(c, b)), n) < -1e-7) { convex = false; break; }+ }+ const out = [];+ if (convex) { for (let i = 1; i < m - 1; i++) out.push([0, i, i + 1]); return out; }+ const e1 = vUnit(vSub(P[1], P[0])), e2 = vCross(n, e1);+ const contour = P.map((p) => new THREE.Vector2(vDot(vSub(p, P[0]), e1), vDot(vSub(p, P[0]), e2)));+ for (const t of THREE.ShapeUtils.triangulateShape(contour, [])) {+ const tn = vCross(vSub(P[t[1]], P[t[0]]), vSub(P[t[2]], P[t[0]]));+ out.push(vDot(tn, n) >= 0 ? [t[0], t[1], t[2]] : [t[0], t[2], t[1]]);+ }+ return out;+}+function insetPoly(P, n, d) {+ const m = P.length, out = [];+ for (let i = 0; i < m; i++) {+ const p = P[i], a = P[(i + m - 1) % m], b = P[(i + 1) % m];+ const n1 = vCross(n, vUnit(vSub(p, a))), n2 = vCross(n, vUnit(vSub(b, p)));+ const mt = vUnit(vAdd(n1, n2)), den = Math.max(0.35, vDot(mt, n1));+ out.push(vAdd(p, vMul(mt, d / den)));+ }+ return out;+}++function createKit(root, MAT, COLORED, TEXTURED) {+ let G = null, matKey = 'armour', F = null, mir = 1, hullC = null, seed = 90125;+ const groups = [], pistons = [];+ const rnd = () => { seed = (seed * 1664525 + 1013904223) >>> 0; return seed / 4294967296; };+ const T = (p) => {+ let x = p[0], y = p[1], z = p[2];+ if (F) {+ const X = F.o[0] + F.u[0] * x + F.v[0] * y + F.w[0] * z;+ const Y = F.o[1] + F.u[1] * x + F.v[1] * y + F.w[1] * z;+ const Z = F.o[2] + F.u[2] * x + F.v[2] * y + F.w[2] * z;+ x = X; y = Y; z = Z;+ }+ return [x, y, mir < 0 ? -z : z];+ };+ const TN = (n) => {+ let x = n[0], y = n[1], z = n[2];+ if (F) {+ const X = F.u[0] * x + F.v[0] * y + F.w[0] * z, Y = F.u[1] * x + F.v[1] * y + F.w[1] * z, Z = F.u[2] * x + F.v[2] * y + F.w[2] * z;+ x = X; y = Y; z = Z;+ }+ return [x, y, mir < 0 ? -z : z];+ };+ const bucket = (key) => {+ const k = key || matKey;+ if (!G.b[k]) G.b[k] = { key: k, pos: [], nor: [], uv: [], col: [], idx: [] };+ return G.b[k];+ };+ const q8 = (x) => Math.max(0, Math.min(255, Math.round(x * 255)));+ // Colour grade by rest-pose height and facing: undersides sink into shadow, and everything below the+ // thigh goes progressively darker and dustier so the lower legs read as the dirtiest part of the machine.+ const addV = (b, a, n, c, u, v, al = 1) => {+ b.pos.push(a[0], a[1], a[2]); b.nor.push(n[0], n[1], n[2]); b.uv.push(u, v);+ let r = c[0], gr = c[1], bl = c[2];+ if (b.key !== 'decal') {+ // Sun bleaches paint on anything facing up.+ if (b.key === 'armour' && n[1] > 0.25) { const s = (n[1] - 0.25) * 0.34; r += (SUN[0] - r) * s; gr += (SUN[1] - gr) * s; bl += (SUN[2] - bl) * s; }+ if (b.key === 'armour' || b.key === 'steel') { const ao = 0.66 + 0.34 * (n[1] * 0.5 + 0.5); r *= ao; gr *= ao; bl *= ao; }+ if (a[1] < 2.6) {+ const rub = b.key === 'rubber', t = 1 - a[1] / 2.6, dark = 1 - (rub ? 0.08 : 0.44) * t, mix = (rub ? 0.14 : 0.58) * t * t;+ r = r * dark * (1 - mix) + DUST_D[0] * mix; gr = gr * dark * (1 - mix) + DUST_D[1] * mix; bl = bl * dark * (1 - mix) + DUST_D[2] * mix;+ }+ }+ b.col.push(q8(r), q8(gr), q8(bl), q8(al));+ };+ const tri = (b, i, j, k) => { if (mir < 0) b.idx.push(i, k, j); else b.idx.push(i, j, k); };+ const QUAD = [[0, 1, 2], [0, 2, 3]];+ const faceBasis = (n) => {+ let e2 = vSub([0, 1, 0], vMul(n, n[1]));+ if (vLen(e2) < 0.25) e2 = vSub([1, 0, 0], vMul(n, n[0]));+ e2 = vUnit(e2);+ return [vCross(e2, n), e2];+ };+ // Each chart sits somewhere inside the unit square so UVs can be stored as 16-bit. Steel keeps the+ // top of its map clear: that band is the polished finish for machined chamfers and bolt heads.+ const chartFit = (vals) => {+ const lo = Math.min(...vals), hi = Math.max(...vals), span = hi - lo, k = span > 1 ? 1 / span : 1, off = span < 1 ? rnd() * (1 - span) : 0;+ return (x) => (x - lo) * k + off;+ };+ const emit = (pts, n, cols, tris, key, polish) => {+ const b = bucket(key), base = b.pos.length / 3, an = TN(n), [e1, e2] = faceBasis(an);+ const abs = pts.map(T), us = abs.map((a) => vDot(a, e1) / TILE), vs = abs.map((a) => vDot(a, e2) / TILE);+ const fu = chartFit(us), fv = chartFit(vs);+ abs.forEach((a, i) => {+ const c = Array.isArray(cols[0]) ? cols[i] : cols;+ let v = fv(vs[i]);+ if (b.key === 'steel') v = polish ? POLISH_V : STEEL_V + v * (1 - STEEL_V);+ addV(b, a, an, c, fu(us[i]), v);+ });+ for (const t of tris) tri(b, base + t[0], base + t[1], base + t[2]);+ };+ // Alpha-blended overlay from the wear atlas; keeps authored UVs and uses vertex alpha for soft fades.+ const decal = (pts, n, uvs, cols, alphas, tris) => {+ const b = bucket('decal'), base = b.pos.length / 3, an = TN(n);+ pts.forEach((p, i) => addV(b, T(p), an, Array.isArray(cols[0]) ? cols[i] : cols, uvs[i][0], uvs[i][1], alphas[i]));+ for (const t of tris || QUAD) tri(b, base + t[0], base + t[1], base + t[2]);+ };++ const kit = {};+ kit.rnd = rnd;+ kit.reseed = (s) => { seed = s; };+ kit.use = (g) => { G = g; };+ kit.mat = (k) => { matKey = k; };+ kit.frame = (f) => { F = f; };+ kit.mirror = (s) => { mir = s; };+ kit.hull = (p) => { hullC = p; };+ kit.bone = (a, b, z) => {+ const u = vUnit([b[0] - a[0], b[1] - a[1], 0]), w = [0, 0, 1];+ return { o: [a[0], a[1], z], u, v: vCross(w, u), w, L: Math.hypot(b[0] - a[0], b[1] - a[1]) };+ };+ kit.at = (f, u, v, w) => [f.o[0] + f.u[0] * u + f.v[0] * v, f.o[1] + f.u[1] * u + f.v[1] * v, f.o[2] + w];+ kit.group = (name, parent, origin, label) => {+ const rel = parent ? vSub(origin, parent.origin) : origin;+ const node = createPivot(name, rel, parent ? parent.node : root);+ const g = { name, label: label || name, node, origin, b: {} };+ groups.push(g);+ return g;+ };++ kit.poly = (ptsIn, col, o = {}) => {+ let pts = ptsIn, n = newell(pts);+ if (o.out && vDot(n, o.out) < 0) { pts = [...pts].reverse(); n = vMul(n, -1); if (Array.isArray(col[0])) col = [...col].reverse(); }+ emit(pts, n, col, triangulate(pts, n), o.mat, o.polish);+ return n;+ };++ // Bevelled armour plate: front face at pts, bevel down to depth t. Some bevel runs are worn through to+ // bare metal and carry a chipped-paint decal onto the face; the rest stay painted with a lightly rubbed crest.+ kit.plate = (ptsIn, t = 0.05, o = {}) => {+ let P = ptsIn, n = newell(P);+ const out = o.out || (hullC ? vSub(centroid(P), hullC) : null);+ if (out && vDot(n, out) < 0) { P = [...P].reverse(); n = vMul(n, -1); }+ const c = o.c !== undefined ? o.c : Math.min(t * 0.8, 0.028);+ const inner = insetPoly(P, n, c);+ const painted = !o.mat && matKey === 'armour';+ const raw = o.col || PAINT;+ // Repaint batches: neighbouring plates never quite match.+ const batch = raw === PAINT || raw === PAINT2 ? vMix(raw, rnd() < 0.5 ? BATCH_A : BATCH_B, rnd() * 0.8) : raw;+ const base = tone(batch, 0.8 + rnd() * 0.34);+ const edgeCol = o.edge || WEAR, low = tone(vMix(base, RUST_D, 0.25), 0.28);+ const ys = P.map((p) => T(p)[1]), y0 = Math.min(...ys), y1 = Math.max(...ys);+ const shade = (p) => tone(base, 0.8 + 0.28 * (T(p)[1] - y0) / Math.max(0.05, y1 - y0));+ emit(inner, n, inner.map(shade), triangulate(inner, n), o.mat, o.polish);+ const m = P.length, drop = vMul(n, t), cen = centroid(inner);+ let area = 0, perim = 0;+ for (let i = 0; i < m; i++) {+ const a = inner[i], b = inner[(i + 1) % m];+ area += vDot(vCross(vSub(a, cen), vSub(b, cen)), n) / 2; perim += vLen(vSub(b, a));+ }+ const depth = Math.min(CHIP_DEPTH, (2 * area / Math.max(perim, 0.01)) * 0.9);+ const inside = (p) => inner.every((v, i) => vDot(vCross(vSub(inner[(i + 1) % m], v), vSub(p, v)), n) >= -1e-6);+ const chip = (a, b) => {+ const len = vLen(vSub(b, a));+ if (len < 0.05) return;+ const d = vUnit(vSub(b, a)), inw = vCross(n, d), lift = vMul(n, 0.0035);+ let D = depth;+ const fits = (dd) => inside(vAdd(a, vMul(inw, dd))) && inside(vAdd(b, vMul(inw, dd)));+ if (!fits(D)) D *= 0.5;+ if (!fits(D)) return;+ const ul = Math.min(1, len), u0 = rnd() * (1 - ul);+ decal([a, b, vAdd(b, vMul(inw, D)), vAdd(a, vMul(inw, D))].map((p) => vAdd(p, lift)), n,+ [[u0, 0], [u0 + ul, 0], [u0 + ul, CHIP_V], [u0, CHIP_V]], WHITE, [1, 1, 1, 1]);+ };+ const wearP = painted ? (o.wear === undefined ? 0.42 : o.wear) : 0;+ for (let i = 0; i < m; i++) {+ const j = (i + 1) % m, runs = Math.max(1, Math.min(3, Math.round(vLen(vSub(P[j], P[i])) / 0.42)));+ for (let k = 0; k < runs; k++) {+ const a0 = vMix(P[i], P[j], k / runs), a1 = vMix(P[i], P[j], (k + 1) / runs);+ const i0 = vMix(inner[i], inner[j], k / runs), i1 = vMix(inner[i], inner[j], (k + 1) / runs);+ const q = [vSub(a0, drop), vSub(a1, drop), i1, i0];+ if (rnd() < wearP && depth > 0.018) {+ // Bare metal that wears away along the run, so a long edge never reads as one bright bar.+ const dull = vMix(base, BARE, 0.3), flip = rnd() < 0.5;+ emit(q, newell(q), [tone(BARE, 0.34), tone(BARE, 0.34), flip ? BARE : dull, flip ? dull : BARE], QUAD, 'steel');+ chip(i0, i1);+ } else {+ const crest = vMix(base, edgeCol, 0.06 + rnd() * 0.14);+ emit(q, newell(q), [low, low, crest, crest], QUAD, o.mat, o.polish);+ }+ }+ }+ // Close the plate so an overhang stays solid when seen from behind.+ if (!o.noBack) {+ const backPts = [...P].reverse().map((p) => vSub(p, drop)), backN = vMul(n, -1);+ emit(backPts, backN, low, triangulate(backPts, backN), o.mat);+ }+ const Q = ptsIn;+ const h = { P, inner, n, t, col: base, painted, at: (s, tt, l = 0) => vAdd(vMix(vMix(Q[0], Q[1], s), vMix(Q[3], Q[2], s), tt), vMul(n, l)) };+ if (painted) kit.weather(h, y0, area, o.noDrip);+ return h;+ };++ // Plate-level weathering: dirt on the low edge, runs from the top, dust on anything near the ground.+ kit.weather = (h, y0, area, noDrip) => {+ if (area < 0.012) return;+ const inn = h.inner, m = inn.length, c = centroid(inn), lift = vMul(h.n, 0.003);+ let low = 0, lowY = Infinity;+ for (let i = 0; i < m; i++) { const y = (T(inn[i])[1] + T(inn[(i + 1) % m])[1]) / 2; if (y < lowY) { lowY = y; low = i; } }+ const a = inn[low], b = inn[(low + 1) % m], len = vLen(vSub(b, a));+ if (len > 0.06) {+ const d = vUnit(vSub(b, a)), inw = vCross(h.n, d), D = Math.min(0.075, Math.sqrt(area) * 0.3), ul = Math.min(1, len), u0 = rnd() * (1 - ul);+ decal([a, b, vAdd(b, vMul(inw, D)), vAdd(a, vMul(inw, D))].map((p) => vAdd(p, lift)), h.n,+ [[u0, GRIME_MID], [u0 + ul, GRIME_MID], [u0 + ul, GRIME_END], [u0, GRIME_END]], GRIME, [0.9, 0.9, 0.9, 0.9]);+ }+ if (!noDrip) {+ const count = Math.max(1, Math.min(5, Math.round(area * 11)));+ for (let i = 0; i < count; i++) {+ const s = vMix(vMix(inn[Math.floor(rnd() * m)], inn[Math.floor(rnd() * m)], rnd()), c, 0.16);+ const r = rnd(), kind = r < 0.5 ? 'rust' : r < 0.82 ? 'grime' : 'oil';+ kit.streak(h, s, 0.22 + rnd() * 0.55, 0.05 + rnd() * rnd() * 0.16, kind, true);+ }+ }+ if (y0 < 1.75 && !noDrip) {+ const e1 = vUnit(vSub(inn[1], inn[0])), e2 = vCross(h.n, e1), uvs = [], alphas = [];+ const u0 = 0.25 + rnd() * 0.5, v0 = 0.5;+ for (const p of inn) {+ const rel = vSub(p, c), su = Math.max(0, Math.min(1, u0 + vDot(rel, e1) / 1.6)), sv = Math.max(0, Math.min(1, v0 + vDot(rel, e2) / 0.8));+ uvs.push([su, DUST_V0 + sv * (1 - DUST_V0)]);+ alphas.push(Math.max(0, Math.min(1, (1.75 - T(p)[1]) / 1.1)) * 0.78);+ }+ decal(inn.map((p) => vAdd(p, vMul(h.n, 0.0025))), h.n, uvs, DUST_L, alphas, triangulate(inn, h.n));+ }+ };++ // A run down a plate along rest-pose gravity: 'rust', 'grime', 'oil' or 'soot'. Fades out by vertex alpha.+ kit.streak = (h, sIn, len, w, kind, fromTop) => {+ const dn = F ? [-F.u[1], -F.v[1], -F.w[1]] : [0, -1, 0];+ const flat = vSub(dn, vMul(h.n, vDot(dn, h.n)));+ if (vLen(flat) < 0.3) return;+ const d = vUnit(flat), inn = h.inner, m = inn.length, c = centroid(inn);+ const inside = (p) => inn.every((v, i) => vDot(vCross(vSub(inn[(i + 1) % m], v), vSub(p, v)), h.n) >= -1e-6);+ let s = sIn;+ if (fromTop && vDot(vSub(s, c), d) > 0) s = vSub(c, vSub(s, c));+ for (let k = 0; k < 6 && !inside(s); k++) s = vMix(s, c, 0.35);+ if (!inside(s)) return;+ let e = vAdd(s, vMul(d, len));+ for (let k = 0; k < 8 && !inside(e); k++) e = vMix(s, e, 0.75);+ if (vLen(vSub(e, s)) < 0.03) return;+ const sideDir = vCross(h.n, d);+ let hw = w / 2;+ for (let k = 0; k < 3 && !(inside(vAdd(s, vMul(sideDir, hw))) && inside(vSub(s, vMul(sideDir, hw)))); k++) hw *= 0.6;+ const side = vMul(sideDir, hw), lift = vMul(h.n, 0.003), A = vAdd(s, lift), B = vAdd(e, lift);+ const colIdx = kind === 'oil' ? 5 + Math.floor(rnd() * 3) : kind === 'soot' || kind === 'grime' ? 4 : Math.floor(rnd() * 4);+ const x0 = colIdx / 8 + 0.002, x1 = (colIdx + 1) / 8 - 0.002;+ const col = kind === 'oil' ? OIL : kind === 'soot' ? SOOT : kind === 'grime' ? GRIME : RUST_D;+ const al = kind === 'oil' ? 1 : kind === 'soot' ? 0.95 : 0.6 + rnd() * 0.3;+ decal([vSub(A, side), vSub(B, vMul(side, 0.6)), vAdd(B, vMul(side, 0.6)), vAdd(A, side)], h.n,+ [[x0, RUN_V0], [x0, RUN_V1], [x1, RUN_V1], [x1, RUN_V0]], col, [al, al * 0.22, al * 0.22, al]);+ };++ // Panel line: a dark groove with a thin lit lip, and grime settled into it, heavier on the low side.+ kit.seam = (h, a, b, w = 0.016, col = SEAM) => {+ const d = vUnit(vSub(b, a)), s = vMul(vCross(h.n, d), w / 2), l = vMul(h.n, 0.004);+ const A = vAdd(a, l), B = vAdd(b, l);+ emit([vSub(A, s), vSub(B, s), vAdd(B, s), vAdd(A, s)], h.n, col, QUAD);+ const lip = vMul(vUnit(s), 0.006), lit = vMix(h.col, WEAR, 0.3);+ emit([vAdd(A, s), vAdd(B, s), vAdd(vAdd(B, s), lip), vAdd(vAdd(A, s), lip)], h.n, lit, QUAD);+ if (!h.painted) return;+ const len = vLen(vSub(b, a));+ if (len < 0.05) return;+ const sd = vUnit(s), dn = F ? [-F.u[1], -F.v[1], -F.w[1]] : [0, -1, 0], shift = vMul(sd, vDot(sd, dn) >= 0 ? 0.012 : -0.012), half = vMul(sd, 0.05);+ const G0 = vAdd(vAdd(a, shift), vMul(h.n, 0.0032)), G1 = vAdd(vAdd(b, shift), vMul(h.n, 0.0032));+ const ul = Math.min(1, len), u0 = rnd() * (1 - ul);+ decal([vSub(G0, half), vSub(G1, half), vAdd(G1, half), vAdd(G0, half)], h.n,+ [[u0, GRIME_V0], [u0 + ul, GRIME_V0], [u0 + ul, GRIME_END], [u0, GRIME_END]], GRIME, [1, 1, 1, 1]);+ };+ kit.loop = (h, d = 0.05, w = 0.016) => {+ const pts = insetPoly(h.P, h.n, d);+ pts.forEach((p, i) => kit.seam(h, p, pts[(i + 1) % pts.length], w));+ };+ // Hex bolt head on the polished steel band. Larger heads get a bevel skirt so they catch a glint from any side.+ kit.bolt = (p, n, r = 0.022, col = GM) => {+ const b = bucket('steel'), base = b.pos.length / 3, e1 = vPerp(n), e2 = vCross(n, e1), an = TN(n);+ const rad = (i) => { const a = (i / 6) * 2 * Math.PI; return vAdd(vMul(e1, Math.cos(a)), vMul(e2, Math.sin(a))); };+ const domed = r >= 0.017, top = vAdd(p, vMul(n, domed ? 0.013 : 0.007));+ for (let i = 0; i < 6; i++) addV(b, T(vAdd(top, vMul(rad(i), r * (domed ? 0.6 : 1)))), an, col, (i + 0.5) / 6, domed ? POLISH_V : 0.5);+ for (let i = 1; i < 5; i++) tri(b, base, base + i, base + i + 1);+ if (domed) {+ const foot = b.pos.length / 3, bot = vAdd(p, vMul(n, 0.002));+ for (let i = 0; i < 6; i++) addV(b, T(vAdd(bot, vMul(rad(i), r))), TN(vUnit(vAdd(vMul(n, 0.5), vMul(rad(i), 0.87)))), tone(col, 0.72), (i + 0.5) / 6, POLISH_V);+ for (let i = 0; i < 6; i++) { const j = (i + 1) % 6; tri(b, foot + i, foot + j, base + j); tri(b, foot + i, base + j, base + i); }+ }+ };+ kit.bolts = (h, d = 0.05, each = 1, r = 0.02) => {+ const pts = insetPoly(h.P, h.n, d);+ pts.forEach((p, i) => {+ const q = pts[(i + 1) % pts.length];+ for (let k = 0; k < each; k++) {+ const at = vMix(p, q, k / each);+ kit.bolt(at, h.n, r);+ if (h.painted && rnd() < 0.36) kit.streak(h, at, 0.14 + rnd() * 0.3, 0.05 + rnd() * 0.06, 'rust');+ }+ });+ };+ kit.hazard = (h, sw = 0.09, skewK = 1) => {+ const q0 = h.inner[0], q1 = h.inner[1], q2 = h.inner[2], q3 = h.inner[3];+ const L = vLen(vSub(q1, q0)), W = vLen(vSub(q3, q0)), ns = Math.max(2, Math.round(L / (2 * sw))), ds = 1 / (2 * ns), skew = (W / L) * skewK;+ const lift = vMul(h.n, 0.004);+ const map = (s, t) => vAdd(vMix(vMix(q0, q1, s), vMix(q3, q2, s), t), lift);+ for (let k = -ns - 2; k < ns + 2; k++) {+ const s0 = k * 2 * ds + ds / 2;+ let poly2 = [[s0, 0], [s0 + ds, 0], [s0 + ds + skew, 1], [s0 + skew, 1]];+ for (const [lim, sign] of [[0, 1], [1, -1]]) {+ const out = [];+ for (let i = 0; i < poly2.length; i++) {+ const a = poly2[i], b = poly2[(i + 1) % poly2.length];+ const da = (a[0] - lim) * sign, db = (b[0] - lim) * sign;+ if (da >= 0) out.push(a);+ if (da * db < 0) { const u = da / (da - db); out.push([a[0] + (b[0] - a[0]) * u, a[1] + (b[1] - a[1]) * u]); }+ }+ poly2 = out;+ if (!poly2.length) break;+ }+ if (poly2.length >= 3) {+ const pts = poly2.map((p) => map(p[0], p[1]));+ const c = tone(HAZ_Y, 0.82 + rnd() * 0.24);+ const n = newell(pts);+ if (vDot(n, h.n) < 0) pts.reverse();+ emit(pts, h.n, c, triangulate(pts, h.n));+ }+ }+ };+ kit.hazardPlate = (pts, t, o = {}) => {+ const h = kit.plate(pts, t, { ...o, col: HAZ_K, edge: lin(0x77776c), noDrip: true });+ kit.hazard(h, o.sw || 0.09, o.skew === undefined ? 1 : o.skew);+ return h;+ };++ const SEG7 = { 0: 'abcdef', 1: 'bc', 2: 'abged', 3: 'abgcd', 4: 'fgbc', 5: 'afgcd', 6: 'afgedc', 7: 'abc', 8: 'abcdefg', 9: 'abcdfg', A: 'abcefg', E: 'afged', H: 'fgbec', K: 'fgecb', L: 'fed', M: 'abcef', R: 'abcefg', T: 'afe', '-': 'g' };+ kit.stencil = (o, rIn, u, str, hgt, col, nOut) => {+ const r = mir < 0 ? vMul(rIn, -1) : rIn, adv = hgt * 0.72, w = hgt * 0.5, th = hgt * 0.14;+ const start = mir < 0 ? vAdd(o, vMul(rIn, adv * str.length)) : o;+ const seg = (A, B, C, D) => {+ const lift = vMul(nOut, 0.005), pts = [A, B, C, D].map((p) => vAdd(p, lift));+ kit.poly(pts, col, { out: nOut });+ };+ [...str].forEach((ch, i) => {+ const org = vAdd(start, vMul(r, adv * i)), P = (x, y) => vAdd(org, vAdd(vMul(r, x * w), vMul(u, y * hgt)));+ const defs = { a: [0, 1, 1, 1], g: [0, 0.5, 1, 0.5], d: [0, 0, 1, 0], f: [0, 1, 0, 0.5], b: [1, 1, 1, 0.5], e: [0, 0.5, 0, 0], c: [1, 0.5, 1, 0] };+ for (const s of SEG7[ch] || '') {+ const [x0, y0, x1, y1] = defs[s], tx = th / w / 2, ty = th / hgt / 2;+ if (y0 === y1) seg(P(x0 - tx, y0 - ty), P(x1 + tx, y0 - ty), P(x1 + tx, y0 + ty), P(x0 - tx, y0 + ty));+ else seg(P(x0 - tx, y1 - ty), P(x0 + tx, y1 - ty), P(x0 + tx, y0 + ty), P(x0 - tx, y0 + ty));+ }+ });+ };++ // Revolved profile about an arbitrary axis; [d, r, col] entries, hard crease between bands.+ kit.lathe = (a, dirIn, prof, o = {}) => {+ const dir = vUnit(dirIn), seg = o.seg || 16, b = bucket(o.mat), col0 = o.col || GUN, isSteel = b.key === 'steel';+ const e1 = vPerp(dir), e2 = vCross(dir, e1);+ for (let i = 0; i < prof.length - 1; i++) {+ // [distance, radius, colour, polished?, colour at far end of this band]+ const [d0, r0, c0, pol, c1] = prof[i], [d1, r1] = prof[i + 1];+ if (r0 < 1e-4 && r1 < 1e-4) continue;+ const dd = d1 - d0, dr = r1 - r0, l = Math.hypot(dd, dr) || 1, base = b.pos.length / 3, colA = c0 || col0, colB = c1 || colA;+ const uK = Math.min(1, (2 * Math.PI * Math.max(r0, r1, 0.05)) / TILE), span = Math.min(1, Math.abs(dd) / TILE), v0 = rnd() * (1 - span);+ const vA = isSteel ? (pol ? POLISH_V : STEEL_V + v0 * (1 - STEEL_V)) : v0, vB = isSteel ? (pol ? POLISH_V : STEEL_V + (v0 + span) * (1 - STEEL_V)) : v0 + span;+ for (let k = 0; k <= seg; k++) {+ const th = (k / seg) * 2 * Math.PI, cs = Math.cos(th), sn = Math.sin(th);+ const rad = vAdd(vMul(e1, cs), vMul(e2, sn));+ const nn = vUnit(vAdd(vMul(rad, dd / l), vMul(dir, -dr / l)));+ const an = TN(nn), u = (k / seg) * uK;+ addV(b, T(vAdd(vAdd(a, vMul(dir, d0)), vMul(rad, r0))), an, colA, u, vA);+ addV(b, T(vAdd(vAdd(a, vMul(dir, d1)), vMul(rad, r1))), an, colB, u, vB);+ }+ for (let k = 0; k < seg; k++) {+ const i0 = base + k * 2, i1 = i0 + 1, i2 = i0 + 2, i3 = i0 + 3;+ if (r0 >= 1e-4 && r1 >= 1e-4) { tri(b, i0, i2, i3); tri(b, i0, i3, i1); }+ else if (r1 < 1e-4) tri(b, i0, i2, i1);+ else tri(b, i0, i3, i1);+ }+ }+ };+ kit.cyl = (a, c, r0, r1, o = {}) => {+ const L = vLen(vSub(c, a));+ kit.lathe(a, vSub(c, a), [[0, 0], [0, r0], [L, r1], [L, 0]], { seg: o.seg || 8, col: o.col, mat: o.mat });+ };++ const crPt = (p0, p1, p2, p3, t) => {+ const t2 = t * t, t3 = t2 * t, f = (i) => 0.5 * (2 * p1[i] + (p2[i] - p0[i]) * t + (2 * p0[i] - 5 * p1[i] + 4 * p2[i] - p3[i]) * t2 + (3 * p1[i] - p0[i] - 3 * p2[i] + p3[i]) * t3);+ return [f(0), f(1), f(2)];+ };+ kit.tube = (ctrl, r, o = {}) => {+ const sp = o.sp || 3, seg = o.seg || 6, col = o.col || RUB, b = bucket(o.mat || 'rubber');+ const P = [ctrl[0], ...ctrl, ctrl[ctrl.length - 1]], path = [];+ for (let i = 1; i < P.length - 2; i++) for (let k = 0; k < sp; k++) path.push(crPt(P[i - 1], P[i], P[i + 1], P[i + 2], k / sp));+ path.push(ctrl[ctrl.length - 1]);+ let nrm = vPerp(vUnit(vSub(path[1], path[0]))), acc = 0;+ const base = b.pos.length / 3;+ path.forEach((p, i) => {+ const t = vUnit(vSub(path[Math.min(i + 1, path.length - 1)], path[Math.max(i - 1, 0)]));+ nrm = vUnit(vSub(nrm, vMul(t, vDot(nrm, t))));+ const bn = vCross(t, nrm);+ if (i) acc += vLen(vSub(p, path[i - 1]));+ for (let k = 0; k <= seg; k++) {+ const th = (k / seg) * 2 * Math.PI, rad = vAdd(vMul(nrm, Math.cos(th)), vMul(bn, Math.sin(th)));+ addV(b, T(vAdd(p, vMul(rad, r))), TN(rad), col, k / seg, acc / TILE);+ }+ });+ const stride = seg + 1;+ for (let i = 0; i < path.length - 1; i++) for (let k = 0; k < seg; k++) {+ const a0 = base + i * stride + k, a1 = a0 + 1, c0 = a0 + stride, c1 = c0 + 1;+ tri(b, a0, a1, c1); tri(b, a0, c1, c0);+ }+ };+ kit.cables = (ctrl, spread, spec) => {+ spec.forEach((c, i) => {+ const off = (i - (spec.length - 1) / 2) * spread;+ const pts = ctrl.map((p, k) => (k === 0 || k === ctrl.length - 1 ? p : vAdd(p, [off * 0.7, off, 0])));+ kit.tube(pts, c.r, { col: c.col });+ });+ };++ kit.block = (c, s, o = {}) => {+ const ax = o.ax || [[1, 0, 0], [0, 1, 0], [0, 0, 1]], col = o.col || GUN, ch = o.c === undefined ? 0.012 : o.c, h = [s[0] / 2, s[1] / 2, s[2] / 2];+ const edge = o.edge || vMix(col, WEAR, 0.45), key = o.mat;+ const P = (x, y, z) => vAdd(c, vAdd(vAdd(vMul(ax[0], x), vMul(ax[1], y)), vMul(ax[2], z)));+ const q = (a, b, cc) => { const v = [0, 0, 0]; v[0] = a; v[1] = b; v[2] = cc; return P(v[0], v[1], v[2]); };+ for (let a = 0; a < 3; a++) for (const sg of [-1, 1]) {+ const b1 = (a + 1) % 3, b2 = (a + 2) % 3, pts = [];+ for (const [s1, s2] of [[-1, -1], [1, -1], [1, 1], [-1, 1]]) {+ const v = [0, 0, 0]; v[a] = sg * h[a]; v[b1] = s1 * (h[b1] - ch); v[b2] = s2 * (h[b2] - ch);+ pts.push(q(v[0], v[1], v[2]));+ }+ kit.poly(pts, col, { out: vMul(ax[a], sg), mat: key, polish: o.polish });+ }+ if (ch > 0) {+ for (let a = 0; a < 3; a++) {+ const b1 = (a + 1) % 3, b2 = (a + 2) % 3;+ for (const s1 of [-1, 1]) for (const s2 of [-1, 1]) {+ const V = (w, t1, t2) => { const v = [0, 0, 0]; v[a] = w * (h[a] - ch); v[b1] = s1 * (h[b1] - t1); v[b2] = s2 * (h[b2] - t2); return q(v[0], v[1], v[2]); };+ kit.poly([V(-1, 0, ch), V(1, 0, ch), V(1, ch, 0), V(-1, ch, 0)], edge, { out: vAdd(vMul(ax[b1], s1), vMul(ax[b2], s2)), mat: key, polish: o.polish });+ }+ }+ for (const sx of [-1, 1]) for (const sy of [-1, 1]) for (const sz of [-1, 1]) {+ kit.poly([P(sx * h[0], sy * (h[1] - ch), sz * (h[2] - ch)), P(sx * (h[0] - ch), sy * h[1], sz * (h[2] - ch)), P(sx * (h[0] - ch), sy * (h[1] - ch), sz * h[2])], edge, { out: vAdd(vAdd(vMul(ax[0], sx), vMul(ax[1], sy)), vMul(ax[2], sz)), mat: key, polish: o.polish });+ }+ }+ };+ kit.beam = (a, b, s1, s2, o = {}) => {+ const u = vUnit(vSub(b, a)), up = o.up || (Math.abs(u[1]) < 0.9 ? [0, 1, 0] : [1, 0, 0]), w = vUnit(vCross(u, up)), v = vCross(w, u);+ kit.block(vMix(a, b, 0.5), [vLen(vSub(b, a)), s1, s2], { ...o, ax: [u, v, w] });+ };++ kit.loft = (ringsIn, o = {}) => {+ let rings = ringsIn;+ const d = vSub(centroid(rings[1]), centroid(rings[0]));+ if (vDot(newell(rings[0]), d) < 0) rings = rings.map((r) => [...r].reverse());+ const col = o.col || DARK;+ for (let i = 0; i < rings.length - 1; i++) {+ const A = rings[i], B = rings[i + 1], m = A.length;+ for (let j = 0; j < m; j++) {+ const k = (j + 1) % m, q = [A[j], A[k], B[k], B[j]];+ if (vLen(vSub(A[j], A[k])) < 1e-5 && vLen(vSub(B[j], B[k])) < 1e-5) continue;+ emit(q, newell(q), o.cols ? o.cols[i] || col : col, [[0, 1, 2], [0, 2, 3]], o.mat);+ }+ }+ if (o.capStart !== false) { const r = rings[0], n = vMul(newell(r), -1), R = [...r].reverse(); emit(R, n, col, triangulate(R, n), o.mat); }+ if (o.capEnd !== false) { const r = rings[rings.length - 1], n = newell(r); emit(r, n, col, triangulate(r, n), o.mat); }+ };++ // Machined joint drum in gunmetal: recessed face, boss, polished chamfers that take a sharp highlight.+ kit.drum = (c, axisIn, r, w, o = {}) => {+ const axis = vUnit(axisIn), seg = o.seg || Math.max(16, Math.round(r * 64)), a = vAdd(c, vMul(axis, -w / 2));+ const body = o.col || GM, rec = o.rec || GM_D, boss = o.boss || GM_L;+ const rim = Math.min(0.02, w * 0.12), dep = Math.min(0.03, w * 0.1), key = o.mat || 'steel';+ kit.lathe(a, axis, [+ [0, 0, boss], [0, r * 0.25, GM_H, 1], [dep * 0.3, r * 0.30, rec], [dep, r * 0.30, rec], [dep, r * 0.74, body], [0, r * 0.80, body], [0, r * 0.94, GM_H, 1], [rim, r, body],+ [w - rim, r, GM_H, 1], [w, r * 0.94, body], [w, r * 0.80, body], [w - dep, r * 0.74, rec], [w - dep, r * 0.30, boss], [w, r * 0.30, boss], [w, 0, boss],+ ], { seg, mat: key, polish: o.polish });+ const prev = matKey; matKey = key;+ const e1 = vPerp(axis), e2 = vCross(axis, e1);+ const nb = o.bolts === undefined ? 8 : o.bolts;+ // Bolts on the +axis face only: every drum's other face is buried in the limb.+ for (let i = 0; i < nb; i++) {+ const th = (i / nb) * 2 * Math.PI, rad = vAdd(vMul(e1, Math.cos(th)), vMul(e2, Math.sin(th)));+ kit.bolt(vAdd(vAdd(c, vMul(axis, w / 2 - dep)), vMul(rad, r * 0.53)), axis, Math.min(0.024, r * 0.09), GM_L);+ }+ if (o.gear) {+ const tw = ((2 * Math.PI * r) / o.gear) * 0.5;+ for (let i = 0; i < o.gear; i++) {+ const th = (i / o.gear) * 2 * Math.PI, rad = vAdd(vMul(e1, Math.cos(th)), vMul(e2, Math.sin(th))), tan = vCross(axis, rad);+ kit.block(vAdd(c, vMul(rad, r + 0.012)), [0.034, tw, w * (o.gearW || 0.5)], { ax: [rad, tan, axis], c: 0, col: GM_L, polish: true, mat: key, polish: o.polish });+ }+ }+ matKey = prev;+ };++ kit.vent = (q0, q1, q2, q3, o = {}) => {+ const n = newell([q0, q1, q2, q3]), N = o.n || 5, slat = o.col || GUN;+ kit.poly([q0, q1, q2, q3], o.base || tone(GUN, 0.25), { out: n, mat: o.glow });+ const lift = vMul(n, o.depth || 0.022);+ for (let i = 0; i < N; i++) {+ const t0 = (i + 0.08) / N, t1 = (i + 0.72) / N;+ const a = vAdd(vMix(q0, q3, t0), lift), b = vAdd(vMix(q1, q2, t0), lift), c = vMix(q1, q2, t1), d = vMix(q0, q3, t1);+ kit.poly([a, b, vAdd(c, vMul(lift, 0.25)), vAdd(d, vMul(lift, 0.25))], [slat, slat, vMix(slat, WEAR, 0.3), vMix(slat, WEAR, 0.3)], {});+ }+ };+++ // Louvre vent set into a quad plate, addressed in the plate's own s,t.+ kit.ventOn = (h, s0, s1, t0, t1, o = {}) => {+ const l = 0.004;+ let q = [h.at(s0, t0, l), h.at(s1, t0, l), h.at(s1, t1, l), h.at(s0, t1, l)];+ if (vDot(newell(q), h.n) < 0) q = [q[1], q[0], q[3], q[2]];+ kit.vent(q[0], q[1], q[2], q[3], o);+ q.forEach((p, i) => kit.seam({ ...h, painted: false }, p, q[(i + 1) % 4], 0.012));+ };++ // Raised sub-plate on a quad plate, addressed in its s,t. Keeps the layering consistent wherever it is used.+ kit.onPlate = (h, s0, s1, t0, t1, lift, o = {}) => {+ const q = [h.at(s0, t0, lift), h.at(s1, t0, lift), h.at(s1, t1, lift), h.at(s0, t1, lift)];+ return kit.plate(q, lift + 0.012, { out: h.n, noBack: true, ...o });+ };++ // Thin heat fins standing off a quad plate.+ kit.fins = (h, s0, s1, t0, t1, count, stand = 0.05, col = GM) => {+ const prev = matKey; matKey = 'steel';+ const A = h.at(s0, t0), B = h.at(s1, t0), Cc = h.at(s0, t1), run = vUnit(vSub(B, A)), acr = vSub(Cc, A), lenS = vLen(vSub(B, A)), lenT = vLen(acr), across = vUnit(acr);+ for (let i = 0; i < count; i++) {+ const c = vAdd(vAdd(vMix(A, B, (i + 0.5) / count), vMul(acr, 0.5)), vMul(h.n, stand / 2));+ kit.block(c, [0.012, lenT, stand], { ax: [run, across, h.n], c: 0, col, polish: i % 2 === 0 });+ }+ matKey = prev;+ return lenS;+ };++ // Faceted armour shell over a list of sections: a dark core, then a bevelled plate per facet with a small gap+ // at each station. Section: { u, vf, vr, wo, wi, c } in the current frame. Returns { out, rear, in, front } per span.+ kit.shell = (st, o = {}) => {+ const ring = (s) => [[s.u, s.vf, s.wo - s.c], [s.u, s.vf - s.c, s.wo], [s.u, s.vr + s.c, s.wo], [s.u, s.vr, s.wo - s.c], [s.u, s.vr, -s.wi + s.c], [s.u, s.vr + s.c, -s.wi], [s.u, s.vf - s.c, -s.wi], [s.u, s.vf, -s.wi + s.c]];+ kit.loft(st.map(ring), { col: o.core || DARK });+ const names = { 1: 'out', 3: 'rear', 5: 'in', 7: 'front' }, t = o.t || 0.03, gap = o.gap || 0.012, cols = o.cols || {}, out = [];+ for (let i = 0; i < st.length - 1; i++) {+ const A = ring(st[i]), B = ring(st[i + 1]), g = gap / Math.max(0.05, Math.abs(st[i + 1].u - st[i].u)), sec = {};+ const ax = [(st[i].u + st[i + 1].u) / 2, (st[i].vf + st[i].vr + st[i + 1].vf + st[i + 1].vr) / 4, (st[i].wo - st[i].wi + st[i + 1].wo - st[i + 1].wi) / 4];+ for (let j = 0; j < 8; j++) {+ const k = (j + 1) % 8, Q0 = [vMix(A[j], B[j], g), vMix(A[k], B[k], g), vMix(A[k], B[k], 1 - g), vMix(A[j], B[j], 1 - g)];+ const fo = vSub(centroid(Q0), ax), nn = newell(Q0), lift = vMul(nn, (vDot(nn, fo) < 0 ? -1 : 1) * t), Q = Q0.map((p) => vAdd(p, lift));+ if (names[j]) sec[names[j]] = kit.plate(Q, t + 0.012, { out: fo, col: cols[names[j]] || PAINT, noBack: true });+ else kit.plate(Q, t + 0.012, { out: fo, col: cols.chamfer || PAINT2, noBack: true, c: 0.01, wear: 0.6, noDrip: true });+ }+ out.push(sec);+ }+ return out;+ };+ // Clevis ears either side of a piston pin, from the anchor back to the bone surface.+ kit.lug = (A, base, zc, o = {}) => {+ const w = o.w || 0.085, ear = o.ear || 0.085, foot = o.foot || 0.12, t = o.t || 0.03;+ const d = vUnit(vSub(base, A)), p = [-d[1], d[0], 0];+ const pts = (z) => [+ [A[0] + p[0] * ear, A[1] + p[1] * ear, z], [A[0] - d[0] * ear, A[1] - d[1] * ear, z], [A[0] - p[0] * ear, A[1] - p[1] * ear, z],+ [base[0] - p[0] * foot, base[1] - p[1] * foot, z], [base[0] + p[0] * foot, base[1] + p[1] * foot, z],+ ];+ const col = o.col || GUN;+ kit.plate(pts(zc + w), t, { out: [0, 0, 1], col, c: 0.012 });+ kit.plate(pts(zc - w), t, { out: [0, 0, -1], col, c: 0.012 });+ };++ kit.piston = (name, gA, A, gB, B, o = {}) => { pistons.push({ name, gA, A, gB, B, mir, o, F }); };+ kit.pistonList = () => pistons;++ kit.buildPiston = (p, minSpan, maxSpan) => {+ const { name, gA, A, gB, B, o } = p;+ mir = p.mir; F = null;+ const sgn = mir;+ const actual = (q) => [q[0], q[1], q[2] * sgn];+ const cylG = kit.group('Piston_' + name + '_Cyl', gA, actual(A), 'Piston_' + name + '_Cyl');+ const rodG = kit.group('Piston_' + name + '_Rod', gB, actual(B), 'Piston_' + name + '_Rod');+ const dir = vUnit(vSub(B, A)), back = vMul(dir, -1);+ const r = o.r || 0.09, rr = o.rod || r * 0.56;+ // Equal cylinder and rod: the rod stays inserted past full Walk extension, the gland clears the rod end at its shortest.+ const Lc = Math.max(0.2, Math.min((maxSpan + Math.max(0.18, 0.2 * maxSpan)) / 2, minSpan - 0.05)), Lr = Lc;+ const zAxis = [0, 0, 1], body = PISTON;+ // Cylinder+ kit.use(cylG); kit.mat('steel');+ kit.drum(A, zAxis, r * 0.72, 0.13, { bolts: 0, seg: 12, boss: STEEL });+ const col1 = vMix(body, WEAR, 0.5);+ kit.lathe(A, dir, [+ [r * 0.5, 0, GM_D], [r * 0.5, r * 1.16, GM], [r * 1.5, r * 1.16, GM_H, 1], [r * 1.65, r, body],+ [Lc - r * 1.3, r, GM_D], [Lc - r * 1.3, r * 1.14, GM], [Lc - r * 0.35, r * 1.14, GM_H, 1], [Lc, r * 0.75, GM_L], [Lc, 0, GM_L],+ ], { seg: o.seg || 14 });+ const s2 = vUnit(vCross(dir, [0, 0, 1]));+ const hoseOff = vMul(s2, r + 0.016);+ const hp = (d) => vAdd(vAdd(A, vMul(dir, d)), hoseOff);+ kit.tube([vAdd(A, vMul(dir, r * 1.2)), hp(r * 1.7), hp(Lc * 0.5), hp(Lc - r * 1.6), vAdd(A, vMul(dir, Lc - r * 0.9))], Math.max(0.011, r * 0.14), { col: o.hose || CAB_K, sp: 2, seg: 5 });+ // Rod+ kit.use(rodG); kit.mat('steel');+ kit.drum(B, zAxis, rr * 1.16, 0.12, { bolts: 0, seg: 12, boss: STEEL });+ kit.lathe(B, back, [[rr * 0.8, 0, STEEL_D], [rr * 0.8, rr * 1.24, STEEL_D], [rr * 2.1, rr * 1.24, STEEL_D], [rr * 2.3, rr, STEEL_D], [rr * 2.3, 0, STEEL_D]], { seg: 12 });+ kit.mat('chrome');+ kit.lathe(B, back, [[rr * 2.3, rr], [Lr, rr], [Lr, 0]], { seg: o.seg || 12, mat: 'chrome' });+ // Aim constraint data, usable from runtime code as well as by the baked clip.+ const dA = actual(dir);+ cylG.node.userData = { aimAt: rodG.node.name, aimRestDir: dA };+ rodG.node.userData = { aimAt: cylG.node.name, aimRestDir: vMul(dA, -1) };+ };++ kit.flush = () => {+ for (const g of groups) {+ for (const key of Object.keys(g.b)) {+ const b = g.b[key];+ if (!b.idx.length) continue;+ const n = b.pos.length / 3, pos = new Float32Array(n * 3);+ for (let i = 0; i < n; i++) {+ pos[i * 3] = b.pos[i * 3] - g.origin[0]; pos[i * 3 + 1] = b.pos[i * 3 + 1] - g.origin[1]; pos[i * 3 + 2] = b.pos[i * 3 + 2] - g.origin[2];+ }+ const geo = new THREE.BufferGeometry();+ geo.setAttribute('position', new THREE.BufferAttribute(pos, 3));+ geo.setAttribute('normal', new THREE.BufferAttribute(new Float32Array(b.nor), 3));+ // 16-bit normalised UVs, and none at all on untextured materials: most of the file is vertex data.+ if (TEXTURED[key]) {+ const uv16 = new Uint16Array(b.uv.length);+ for (let i = 0; i < b.uv.length; i++) uv16[i] = Math.round(Math.max(0, Math.min(1, b.uv[i])) * 65535);+ geo.setAttribute('uv', new THREE.BufferAttribute(uv16, 2, true));+ }+ if (COLORED[key]) geo.setAttribute('color', new THREE.BufferAttribute(new Uint8Array(b.col), 4, true));+ geo.setIndex(b.idx);+ geo.computeBoundingBox(); geo.computeBoundingSphere();+ createPart(g.label + '_' + key, geo, MAT[key], { parent: g.node });+ }+ }+ };+ return kit;+}++// ---------------------------------------------------------------- textures+function makeTextures() {+ const prng = (s0) => { let s = s0 >>> 0; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; }; };+ const rand = prng(7);++ // ---- Paint: broad sun-fade, mottling and rub marks, deliberately no spots. Chips live in the wear atlas.+ const fades = [];+ for (let i = 0; i < 12; i++) {+ const w = 90 + Math.round(rand() * 170), h = 70 + Math.round(rand() * 150), x = Math.round(rand() * (512 - w)), y = Math.round(rand() * (512 - h)), light = rand() < 0.5;+ fades.push({ op: 'paint', shape: { kind: 'ellipse', x, y, width: w, height: h }, rgba: light ? [255, 255, 250, 255] : [118, 114, 102, 255], opacity: light ? 28 : 24 });+ }+ const strokes = (n, wMin, wMax, lMin, lMax) => {+ const out = [];+ for (let i = 0; i < n; i++) {+ const x = 20 + Math.round(rand() * 470), y = 20 + Math.round(rand() * 470), a = rand() * 6.28, l = lMin + rand() * (lMax - lMin);+ out.push({ kind: 'line', from: [x, y], to: [Math.max(0, Math.min(511, Math.round(x + Math.cos(a) * l))), Math.max(0, Math.min(511, Math.round(y + Math.sin(a) * l)))], width: wMin + Math.round(rand() * (wMax - wMin)) });+ }+ return out;+ };+ const rubs = strokes(10, 4, 14, 30, 110), scratches = strokes(14, 1, 1, 30, 150);+ const ramp = [];+ for (let i = 0; i < 16; i++) { const k = 148 + i * 7; ramp.push((k << 16) | (k << 8) | Math.round(k * 0.972)); }+ const albedo = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'albedo', name: 'Armour_Paint_Albedo',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 512, usage: 'albedo', layers: [+ { op: 'solid', color: 0xf4f4f0 },+ { op: 'noise', colorA: 0x9e9c90, colorB: 0xffffff, scale: 4, octaves: 4, seed: 41, blend: 'multiply', opacity: 0.95 },+ { op: 'noise', colorA: 0xd2d0c6, colorB: 0xffffff, scale: 30, octaves: 2, seed: 77, blend: 'multiply', opacity: 0.6 },+ ] } },+ ...fades,+ ...rubs.map((s) => ({ op: 'paint', shape: s, rgba: [255, 255, 250, 255], opacity: 34 })),+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [255, 255, 250, 255], opacity: 80 })),+ ],+ palette: { colors: ramp, dither: 'none' },+ });+ const mr = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'metallicRoughness', name: 'Armour_Paint_MR',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 512, usage: 'metallicRoughness', layers: [+ { op: 'solid', color: 0xffa80c },+ { op: 'noise', colorA: 0xff8808, colorB: 0xffc410, scale: 5, octaves: 2, seed: 41 },+ ] } },+ ...rubs.map((s) => ({ op: 'paint', shape: s, rgba: [255, 118, 40, 255], opacity: 90 })),+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [255, 110, 120, 255] })),+ ],+ });+ const seamOps = [];+ const H = (y, x0, x1) => { seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y, width: x1 - x0, height: 2 }, rgba: [128, 72, 235, 255] }); seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x0, y: y + 2, width: x1 - x0, height: 2 }, rgba: [128, 184, 235, 255] }); };+ const Vt = (x, y0, y1) => { seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x, y: y0, width: 2, height: y1 - y0 }, rgba: [72, 128, 235, 255] }); seamOps.push({ op: 'paint', shape: { kind: 'rectangle', x: x + 2, y: y0, width: 2, height: y1 - y0 }, rgba: [184, 128, 235, 255] }); };+ H(70, 0, 512); H(255, 60, 400); H(410, 0, 512); Vt(90, 70, 410); Vt(330, 0, 255); Vt(460, 255, 512);+ const area = { x: 24, y: 24, width: 464, height: 464 };+ const nrm = proceduralTexture({+ schemaVersion: 3, width: 512, height: 512, usage: 'normal', name: 'Armour_Normal', background: [128, 128, 255, 255],+ operations: [+ ...seamOps,+ { op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 26, height: 18 }, rgba: [112, 144, 246, 255], count: 24, seed: 55, area },+ { op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 6, height: 6 }, rgba: [150, 150, 245, 255], count: 90, seed: 56, area },+ ...scratches.map((s) => ({ op: 'paint', shape: s, rgba: [104, 152, 248, 255] })),+ ],+ });++ // ---- Gunmetal: real metal with brushed variation; the top band is the polished finish for chamfers and bolts.+ const steelMR = proceduralTexture({+ schemaVersion: 3, width: 256, height: 256, usage: 'metallicRoughness', name: 'Gunmetal_MR',+ operations: [+ { op: 'legacy-v2', spec: { schemaVersion: 2, size: 256, usage: 'metallicRoughness', layers: [+ { op: 'solid', color: 0xff60be },+ { op: 'noise', colorA: 0xff50b4, colorB: 0xff78c8, scale: 9, octaves: 2, seed: 3 },+ { op: 'stripes', colorA: 0xff58ba, colorB: 0xff6cc2, count: 48, angleDeg: 90, opacity: 0.5 },+ ] } },+ { op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: 256, height: 28 }, rgba: [255, 34, 226, 255] },+ ],+ });++ // ---- Wear atlas. Rows, in texture V: chipped edge, panel grime, eight run columns (rust x5, oil x3), dust.+ const chipShape = (w, h) => ({ kind: 'polygon', points: [[0, Math.round(h * 0.4)], [Math.round(w * 0.3), 0], [Math.round(w * 0.75), Math.round(h * 0.1)], [w, Math.round(h * 0.5)], [Math.round(w * 0.7), h], [Math.round(w * 0.25), Math.round(h * 0.85)]] });+ // Chipped band: the straight edge is the plate edge (y = 0), the jagged side bites into the paint in clusters.+ const jag = (seed, maxD) => {+ const r = prng(seed), p1 = r() * 6.28, p2 = r() * 6.28, p3 = r() * 6.28, pts = [[0, 0]], n = 60;+ for (let i = 0; i <= n; i++) {+ const x = Math.max(0, Math.min(512, Math.round(((i + (i > 0 && i < n ? (r() - 0.5) * 0.8 : 0)) / n) * 512))), th = (x / 512) * 6.283;+ const env = Math.max(0, 0.5 * Math.sin(th * 3 + p1) + 0.35 * Math.sin(th * 7 + p2) + 0.18 * Math.sin(th * 13 + p3) + 0.36);+ const q = r(), tooth = 0.22 + 0.95 * q * Math.sqrt(q);+ pts.push([x, Math.round(maxD * Math.min(1, env) * tooth)]);+ }+ pts.push([512, 0]);+ return { kind: 'polygon', points: pts };+ };+ const band = (seed, cy, half) => {+ const r = prng(seed), n = 30, top = [], bot = [];+ for (let i = 0; i <= n; i++) {+ const x = Math.round((i / n) * 512);+ top.push([x, Math.round(cy - half * (0.45 + 0.55 * r()))]);+ bot.push([x, Math.round(cy + half * (0.45 + 0.55 * r()))]);+ }+ return { kind: 'polygon', points: [...top, ...bot.reverse()] };+ };+ const taper = (r, cx, y0, len, w0) => {+ const L = [], R = [], n = 8;+ for (let i = 0; i <= n; i++) {+ const t = i / n, w = w0 * (0.4 + 0.6 * Math.min(1, t * 4)) * Math.pow(1 - t, 0.7) + 1, y = Math.round(y0 + t * len), j = (r() - 0.5) * w0 * 0.2;+ L.push([Math.round(cx - w / 2 + j), y]); R.push([Math.round(cx + w / 2 + j), y]);+ }+ return { kind: 'polygon', points: [...L, ...R.reverse()] };+ };+ const WHITE8 = [255, 255, 255, 255], METAL8 = [164, 168, 172, 255], PRIMER8 = [84, 78, 66, 255], fleck = { x: 4, y: 2, width: 500, height: 66 };+ const wearOps = [+ { op: 'paint', shape: jag(11, 90), rgba: PRIMER8 },+ { op: 'paint', shape: jag(11, 66), rgba: METAL8 },+ { op: 'stamps', shape: chipShape(6, 4), rgba: METAL8, count: 34, seed: 41, area: { x: 4, y: 2, width: 500, height: 34 } },+ { op: 'stamps', shape: chipShape(13, 9), rgba: PRIMER8, count: 30, seed: 42, area: fleck },+ { op: 'stamps', shape: chipShape(9, 6), rgba: METAL8, count: 30, seed: 42, area: fleck },+ { op: 'paint', shape: band(21, 160, 30), rgba: WHITE8, opacity: 76 },+ { op: 'paint', shape: band(22, 160, 18), rgba: WHITE8, opacity: 110 },+ { op: 'paint', shape: band(23, 160, 8), rgba: WHITE8, opacity: 180 },+ ];+ const r2 = prng(91);+ for (let c = 0; c < 8; c++) {+ const cx = 32 + c * 64, oil = c >= 5;+ wearOps.push({ op: 'paint', shape: taper(r2, cx, 192, 180 + r2() * 70, 22 + r2() * 18), rgba: WHITE8, opacity: oil ? 205 : 165 });+ wearOps.push({ op: 'paint', shape: taper(r2, cx + (r2() - 0.5) * 8, 192, 90 + r2() * 70, 8 + r2() * 8), rgba: WHITE8, opacity: oil ? 225 : 185 });+ for (let k = 0; k < 2; k++) {+ const ox = Math.round(cx + (r2() - 0.5) * 34);+ wearOps.push({ op: 'paint', shape: { kind: 'line', from: [ox, 192], to: [Math.round(ox + (r2() - 0.5) * 8), Math.round(300 + r2() * 145)], width: 1 + Math.round(r2() * 2) }, rgba: WHITE8, opacity: oil ? 235 : 195 });+ }+ }+ wearOps.push({ op: 'paint', shape: { kind: 'rectangle', x: 0, y: 448, width: 512, height: 64 }, rgba: WHITE8, opacity: 58 });+ // This row is 4:1 stretched on the mech, so the grit is drawn wide and flat to land round.+ wearOps.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 6, height: 2 }, rgba: WHITE8, count: 90, seed: 61, area: { x: 2, y: 450, width: 504, height: 58 }, opacity: 90 });+ wearOps.push({ op: 'stamps', shape: { kind: 'ellipse', x: 0, y: 0, width: 3, height: 1 }, rgba: WHITE8, count: 30, seed: 62, area: { x: 2, y: 450, width: 504, height: 58 }, opacity: 120 });+ const wearA = proceduralTexture({ schemaVersion: 3, width: 512, height: 512, usage: 'albedo', name: 'Wear_Atlas', operations: wearOps });+ // Bare metal in the chip row and glossy oil in the oil columns; matte everywhere else.+ const wearMR = proceduralTexture({+ schemaVersion: 3, width: 64, height: 64, usage: 'metallicRoughness', name: 'Wear_Atlas_MR', background: [255, 216, 0, 255],+ operations: [+ { op: 'paint', shape: { kind: 'rectangle', x: 0, y: 0, width: 64, height: 13 }, rgba: [255, 84, 165, 255] },+ { op: 'paint', shape: { kind: 'rectangle', x: 40, y: 24, width: 24, height: 32 }, rgba: [255, 44, 26, 255] },+ ],+ });+ return { albedo, mr, nrm, steelMR, wearA, wearMR };+}++// ---------------------------------------------------------------- legs+function buildLeg(kit, side, pelvisG) {+ const z0 = SK.legZ, s = side === 'L' ? -1 : 1;+ kit.mirror(s);+ const hipG = kit.group('Hip_' + side, pelvisG, [SK.hip[0], SK.hip[1], s * z0], 'Thigh_' + side);+ const kneeG = kit.group('Knee_' + side, hipG, [SK.knee[0], SK.knee[1], s * z0], 'Shin_' + side);+ const ankleG = kit.group('Ankle_' + side, kneeG, [SK.ankle[0], SK.ankle[1], s * z0], 'Meta_' + side);+ const toeG = kit.group('Toe_' + side, ankleG, [SK.toe[0], SK.toe[1], s * z0], 'Foot_' + side);+ [hipG, kneeG, ankleG, toeG].forEach((g) => { g.node.userData = { hinge: g === hipG ? 'ball (z=flex)' : g === toeG ? 'ball (z=flex)' : 'z' }; });++ const thF = kit.bone(SK.hip, SK.knee, z0), shF = kit.bone(SK.knee, SK.ankle, z0), mtF = kit.bone(SK.ankle, SK.toe, z0);+ const ring = (u, vf, vr, wh) => [[u, vf, wh], [u, vf, -wh], [u, vr, -wh], [u, vr, wh]];+ const sideP = (w, pts, t, o = {}) => kit.plate(pts.map((p) => [p[0], p[1], w]), t, { out: [0, 0, Math.sign(w)], ...o });++ // ---- Thigh+ kit.use(hipG); kit.frame(thF); kit.hull([thF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.40, 0.56, { bolts: 10, gear: 26, gearW: 0.4 });+ kit.drum([0, 0, 0.30], [0, 0, 1], 0.16, 0.08, { bolts: 0, seg: 14, col: DARK });+ kit.mat('armour');+ kit.loft([ring(-0.05, 0.26, -0.28, 0.24), ring(0.40, 0.30, -0.30, 0.25), ring(1.0, 0.22, -0.24, 0.21), ring(thF.L + 0.02, 0.15, -0.15, 0.15)], { col: DARK });+ const t1 = sideP(0.29, [[0.34, -0.28], [1.02, -0.23], [1.22, -0.10], [1.22, 0.10], [1.02, 0.22], [0.40, 0.30], [0.30, 0.14], [0.30, -0.12]], 0.05);+ kit.loop(t1, 0.045); kit.bolts(t1, 0.03, 1, 0.016);+ kit.streak(t1, [0.36, 0.04, 0.29], 0.66, 0.12, 'oil'); kit.streak(t1, [0.40, -0.16, 0.29], 0.42, 0.06, 'rust');+ const t2 = sideP(0.325, [[0.50, -0.20], [0.94, -0.16], [0.94, 0.16], [0.50, 0.21]], 0.035, { col: PAINT2 });+ kit.bolts(t2, 0.03);+ kit.seam(t2, [0.74, -0.18, 0.325], [0.74, 0.18, 0.325]);+ kit.stencil([0.67, -0.05, 0.325], [0, 1, 0], [-1, 0, 0], '07', 0.13, BONE, [0, 0, 1]);+ const t3 = kit.hazardPlate([[1.00, -0.19, 0.325], [1.00, 0.19, 0.325], [1.17, 0.09, 0.325], [1.17, -0.09, 0.325]], 0.03, { out: [0, 0, 1], sw: 0.05 });+ kit.seam(t3, t3.inner[0], t3.inner[1], 0.008);+ const t4 = sideP(-0.28, [[0.34, -0.27], [1.05, -0.20], [1.18, -0.08], [1.18, 0.08], [1.05, 0.20], [0.40, 0.29], [0.32, 0.12], [0.32, -0.12]], 0.04, { col: PAINT2 });+ kit.loop(t4, 0.05);+ const tf = kit.plate([[0.38, 0.335, 0.235], [1.04, 0.245, 0.185], [1.04, 0.245, -0.185], [0.38, 0.335, -0.235]], 0.05, { out: [0, 1, 0] });+ kit.seam(tf, [0.62, 0.30, 0.22], [0.62, 0.30, -0.22]); kit.seam(tf, [0.84, 0.27, 0.20], [0.84, 0.27, -0.20]);+ kit.bolts(tf, 0.03, 1, 0.016);+ const tb = kit.plate([[0.36, -0.315, 0.235], [1.10, -0.255, 0.185], [1.10, -0.255, -0.185], [0.36, -0.315, -0.235]], 0.04, { out: [0, -1, 0], col: GUN });+ kit.loop(tb, 0.05);+ // Servo pack on the outer rear of the thigh+ kit.mat('steel');+ kit.cyl([0.46, -0.205, 0.40], [0.86, -0.17, 0.40], 0.066, 0.066, { seg: 10, col: STEEL_D });+ for (const u of [0.52, 0.60, 0.68, 0.76]) kit.lathe([u, -0.205 + (u - 0.46) * 0.09, 0.40], [1, 0.09, 0], [[0, 0.07], [0, 0.082, GUN], [0.018, 0.082, GUN], [0.018, 0.07]], { seg: 10, col: GUN });+ kit.mat('rubber');+ kit.cables([[0, 0, 0.345], [0.30, 0.10, 0.37], [0.70, 0.235, 0.385], [1.05, 0.215, 0.375], [1.30, 0.08, 0.345], [thF.L, 0, 0.32]], 0.026, [{ r: 0.019, col: CAB_K }, { r: 0.015, col: CAB_R }, { r: 0.019, col: CAB_K }]);+ kit.mat('armour');++ const hipLugT = kit.at(thF, 0.62, -0.42, 0), hipLugB = kit.at(thF, 0.70, -0.27, 0);+ const kneeA = kit.at(thF, 0.30, 0.50, 0), kneeAb = kit.at(thF, 0.42, 0.31, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hipLugT, hipLugB, z0, { col: GUN });+ kit.lug(kneeA, kneeAb, z0, { col: GUN, ear: 0.10, w: 0.09, foot: 0.15 });+ kit.frame(thF); kit.hull([thF.L / 2, 0, 0]);++ // ---- Shin+ kit.use(kneeG); kit.frame(shF); kit.hull([shF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.30, 0.56, { bolts: 8, gear: 22, gearW: 0.4 });+ kit.drum([0, 0, 0.30], [0, 0, 1], 0.13, 0.07, { bolts: 0, seg: 12, col: DARK });+ kit.mat('armour');+ kit.loft([ring(0.05, 0.25, -0.21, 0.23), ring(0.70, 0.25, -0.19, 0.22), ring(1.25, 0.19, -0.16, 0.19), ring(shF.L + 0.02, 0.13, -0.13, 0.14)], { col: DARK });+ const s1 = sideP(0.275, [[0.24, -0.20], [1.28, -0.17], [1.50, -0.10], [1.50, 0.11], [1.30, 0.19], [0.58, 0.27], [0.26, 0.24]], 0.05);+ kit.loop(s1, 0.045); kit.bolts(s1, 0.03, 1, 0.016);+ kit.streak(s1, [0.30, 0.02, 0.275], 0.7, 0.14, 'oil'); kit.streak(s1, [0.48, -0.12, 0.275], 0.5, 0.07, 'rust');+ const s2 = sideP(0.31, [[0.42, -0.15], [1.05, -0.13], [1.05, 0.15], [0.58, 0.20], [0.42, 0.17]], 0.035, { col: PAINT2 });+ kit.bolts(s2, 0.03); kit.seam(s2, [0.76, -0.13, 0.31], [0.76, 0.17, 0.31]);+ const s2b = sideP(0.31, [[1.10, -0.13], [1.40, -0.09], [1.40, 0.09], [1.10, 0.14]], 0.03, { col: RUST });+ kit.bolts(s2b, 0.025, 1, 0.014);+ sideP(-0.27, [[0.24, -0.19], [1.30, -0.16], [1.50, -0.09], [1.50, 0.10], [1.30, 0.18], [0.58, 0.25], [0.26, 0.22]], 0.04, { col: PAINT2 });+ const sg = kit.plate([[0.34, 0.33, 0.24], [1.38, 0.22, 0.185], [1.38, 0.22, -0.185], [0.34, 0.33, -0.24]], 0.055, { out: [0, 1, 0] });+ kit.seam(sg, [0.70, 0.295, 0.22], [0.70, 0.295, -0.22]); kit.seam(sg, [1.05, 0.26, 0.20], [1.05, 0.26, -0.20]); kit.bolts(sg, 0.03, 2, 0.016);+ const sg2 = kit.plate([[0.74, 0.32, 0.12], [1.02, 0.29, 0.11], [1.02, 0.29, -0.11], [0.74, 0.32, -0.12]], 0.035, { out: [0, 1, 0], col: PAINT2 });+ kit.bolts(sg2, 0.02, 1, 0.012);+ kit.plate([[0.30, -0.235, 0.22], [1.30, -0.185, 0.18], [1.30, -0.185, -0.18], [0.30, -0.235, -0.22]], 0.04, { out: [0, -1, 0], col: GUN });+ // Knee guard and piston horn+ const kg = kit.plate([[-0.46, 0.235, 0.27], [0.32, 0.415, 0.285], [0.32, 0.415, -0.285], [-0.46, 0.235, -0.27]], 0.06, { out: [0, 1, 0], col: PAINT });+ kit.loop(kg, 0.05); kit.bolts(kg, 0.03, 1, 0.018);+ kit.hazardPlate([[-0.34, 0.325, 0.17], [-0.34, 0.325, -0.17], [0.06, 0.42, -0.17], [0.06, 0.42, 0.17]], 0.04, { out: [0, 1, 0], sw: 0.05 });+ sideP(0.245, [[-0.46, 0.02], [-0.46, 0.24], [0.20, 0.40], [0.22, 0.20], [-0.10, 0.02]], 0.03, { col: GUN });+ sideP(-0.245, [[-0.46, 0.02], [-0.46, 0.24], [0.20, 0.40], [0.22, 0.20], [-0.10, 0.02]], 0.03, { col: GUN });+ kit.mat('rubber');+ kit.cables([[0, 0, 0.335], [0.34, 0.03, 0.355], [0.80, -0.055, 0.365], [1.20, 0.01, 0.36], [1.50, -0.10, 0.33], [shF.L, 0, 0.27]], 0.024, [{ r: 0.017, col: CAB_K }, { r: 0.017, col: CAB_B }]);+ kit.mat('armour');+ const hornK = kit.at(shF, -0.396, 0.115, 0), hornKb = kit.at(shF, -0.14, 0.14, 0);+ const calfA = kit.at(shF, 0.50, -0.50, 0), calfAb = kit.at(shF, 0.62, -0.20, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hornK, hornKb, z0, { ear: 0.09, w: 0.11, foot: 0.16, t: 0.04 });+ kit.lug(calfA, calfAb, z0, { col: GUN, ear: 0.10, w: 0.095, foot: 0.16 });+ kit.frame(shF); kit.hull([shF.L / 2, 0, 0]);++ // ---- Metatarsus+ kit.use(ankleG); kit.frame(mtF); kit.hull([mtF.L / 2, 0, 0]); kit.mat('steel');+ kit.drum([0, 0, 0], [0, 0, 1], 0.22, 0.46, { bolts: 6, gear: 18, gearW: 0.4 });+ kit.drum([0, 0, 0.25], [0, 0, 1], 0.10, 0.06, { bolts: 0, seg: 12, col: DARK });+ kit.block([0.56, 0, 0.105], [0.90, 0.17, 0.065], { col: DARK, c: 0.01 });+ kit.block([0.56, 0, -0.105], [0.90, 0.17, 0.065], { col: DARK, c: 0.01 });+ kit.cyl([0.14, 0, 0], [0.98, 0, 0], 0.052, 0.052, { seg: 10, col: STEEL_D });+ kit.mat('armour');+ const m1 = kit.plate([[0.14, 0.17, 0.175], [0.92, 0.13, 0.145], [0.92, 0.13, -0.145], [0.14, 0.17, -0.175]], 0.05, { out: [0, 1, 0] });+ kit.seam(m1, [0.40, 0.155, 0.16], [0.40, 0.155, -0.16]); kit.bolts(m1, 0.028, 1, 0.014);+ const m2 = sideP(0.16, [[0.16, -0.13], [0.88, -0.11], [0.98, -0.05], [0.98, 0.07], [0.88, 0.12], [0.16, 0.15]], 0.03, { col: PAINT2 });+ kit.loop(m2, 0.035, 0.009);+ kit.streak(m2, [0.2, 0.02, 0.16], 0.5, 0.09, 'oil');+ sideP(-0.16, [[0.16, -0.13], [0.88, -0.11], [0.98, -0.05], [0.98, 0.07], [0.88, 0.12], [0.16, 0.15]], 0.03, { col: PAINT2 });+ kit.hazardPlate([[0.62, 0.155, 0.13], [0.62, 0.155, -0.13], [0.88, 0.145, -0.12], [0.88, 0.145, 0.12]], 0.025, { out: [0, 1, 0], sw: 0.04 });+ kit.plate([[0.16, -0.135, 0.16], [0.92, -0.115, 0.13], [0.92, -0.115, -0.13], [0.16, -0.135, -0.16]], 0.03, { out: [0, -1, 0], col: GUN });+ sideP(0.24, [[-0.46, -0.16], [-0.46, -0.04], [-0.15, 0.13], [-0.15, -0.20]], 0.03, { col: GUN });+ sideP(-0.24, [[-0.46, -0.16], [-0.46, -0.04], [-0.15, 0.13], [-0.15, -0.20]], 0.03, { col: GUN });+ kit.mat('rubber');+ kit.tube([[0, 0, 0.28], [0.25, -0.02, 0.205], [0.62, 0.04, 0.20], [0.95, 0, 0.215], [mtF.L, 0, 0.24]], 0.016, { col: CAB_Y });+ kit.tube([[0, 0, 0.28], [0.30, 0.05, 0.205], [0.66, -0.05, 0.20], [0.92, 0.03, 0.215], [mtF.L, 0, 0.24]], 0.014, { col: CAB_K });+ kit.mat('armour');+ const hornA = kit.at(mtF, -0.38, -0.10, 0), hornAb = kit.at(mtF, -0.10, -0.08, 0);+ const toeUp = kit.at(mtF, 0.62, 0.20, 0), toeUpb = kit.at(mtF, 0.70, 0.13, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(hornA, hornAb, z0, { ear: 0.09, w: 0.11, foot: 0.14, t: 0.04 });+ kit.lug(toeUp, toeUpb, z0, { col: GUN, ear: 0.06, w: 0.055, foot: 0.08, t: 0.02 });++ // ---- Foot: armoured toes over rubber soles. Tread reaches y = 0; centre toe tip stays at SK.tipX.+ kit.use(toeG); kit.frame(null); kit.hull(null); kit.mat('armour');+ const toePiece = (deg, len, wid, x0, dz, cols, hBase, segs, rows) => {+ const c = Math.cos(deg * D2R), sn = Math.sin(deg * D2R), w = wid / 2;+ const P = (x, y, z) => [x0 + x * c + z * sn, y, z0 + dz - x * sn + z * c];+ const dirX = [c, 0, -sn], dirZ = [sn, 0, c];+ const halfW = (x) => (x < len * 0.62 ? w : x < len * 0.88 ? w * (1 - 0.34 * (x - len * 0.62) / (len * 0.26)) : w * (0.66 - 0.40 * (x - len * 0.88) / (len * 0.12)));+ const hgt = (x) => hBase - (hBase - 0.125) * Math.max(0, x / len);+ const stations = [-0.05, len * 0.62, len * 0.88, len];+ const outline = [...stations.map((x) => [x, halfW(x)]), ...[...stations].reverse().map((x) => [x, -halfW(x)])];+ // Rubber sole: a band standing proud of the armour, with cross-lugs that actually carry the foot.+ kit.loft([outline.map(([x, z]) => P(x, 0.022, z)), outline.map(([x, z]) => P(x, 0.085, z))], { col: RUB, mat: 'rubber', capEnd: false });+ const pitch = (len + 0.03) / rows, lw = pitch * 0.58;+ for (let i = 0; i < rows; i++) {+ const xc = i === rows - 1 ? len - lw / 2 : -0.03 + pitch * (i + 0.5);+ kit.block(P(xc, 0.0125, 0), [lw, 0.025, 2 * halfW(xc) * 0.9], { ax: [dirX, [0, 1, 0], dirZ], c: 0, col: tone(RUB, 1.25), mat: 'rubber' });+ }+ // Core, then side skirts, shingled knuckle plates and a steel toe cap.+ kit.loft([outline.map(([x, z]) => P(x, 0.085, z * 0.93)), outline.map(([x, z]) => P(Math.min(x, len * 0.97), hgt(x), z * 0.74))], { col: DARK, capStart: false });+ for (const s of [1, -1]) {+ const sd = vAdd(vMul(dirZ, s), [0, 0.3, 0]);+ const sk = kit.plate([P(0, 0.095, s * w * 0.97), P(len * 0.6, 0.095, s * w * 0.97), P(len * 0.6, hgt(len * 0.6) - 0.014, s * w * 0.8), P(0, hgt(0) - 0.014, s * w * 0.8)], 0.024, { out: sd, col: cols.side, noBack: true, c: 0.012 });+ kit.bolts(sk, 0.022, 1, 0.014);+ kit.plate([P(len * 0.62, 0.095, s * w * 0.97), P(len * 0.9, 0.095, s * halfW(len * 0.9) * 0.97), P(len * 0.9, hgt(len * 0.9) - 0.012, s * halfW(len * 0.9) * 0.76), P(len * 0.62, hgt(len * 0.62) - 0.012, s * w * 0.78)], 0.02, { out: vAdd(sd, vMul(dirX, 0.5)), col: cols.a, noBack: true, c: 0.01 });+ }+ const capAt = len * 0.72, step = (capAt + 0.03) / segs, lap = 0.04;+ let last = null;+ for (let i = 0; i < segs; i++) {+ const a = -0.03 + i * step, b = a + step + (i < segs - 1 ? lap : 0), wa = halfW(a) * 0.74 + 0.012, wb = halfW(b) * 0.74 + 0.012, ya = hgt(a) + 0.018, yb = hgt(b) + 0.048;+ last = kit.plate([P(a, ya, wa), P(b, yb, wb), P(b, yb, -wb), P(a, ya, -wa)], 0.04, { out: [0, 1, 0], col: i % 2 ? cols.b : cols.a, c: 0.018 });+ kit.bolts(last, 0.024, 1, 0.017);+ kit.seam(last, last.at(0.5, 0.06), last.at(0.5, 0.94));+ }+ const yc = hgt(capAt) + 0.03, capW = halfW(capAt) * 0.74 + 0.012, tipW = halfW(len) * 0.86;+ kit.plate([P(capAt, yc, capW), P(len * 0.995, 0.128, tipW), P(len * 0.995, 0.128, -tipW), P(capAt, yc, -capW)], 0.04, { out: [0.4, 1, 0], mat: 'steel', col: GM_L, c: 0.016 });+ kit.plate([P(len * 0.998, 0.128, tipW), P(len * 0.998, 0.09, halfW(len) * 0.94), P(len * 0.998, 0.09, -halfW(len) * 0.94), P(len * 0.998, 0.128, -tipW)], 0.03, { out: dirX, mat: 'steel', col: GM, c: 0.008 });+ return { last };+ };+ const ct = toePiece(0, SK.tipX, 0.44, 0, 0, { a: PAINT, b: PAINT2, side: PAINT2 }, 0.31, 2, 7);+ const hz = ct.last;+ kit.hazardPlate([hz.at(0.42, 0.1, 0.008), hz.at(0.96, 0.1, 0.008), hz.at(0.96, 0.9, 0.008), hz.at(0.42, 0.9, 0.008)], 0.02, { out: hz.n, sw: 0.04, c: 0.006, noBack: true });+ toePiece(24, 0.78, 0.29, 0.04, -0.14, { a: PAINT2, b: PAINT, side: GUN }, 0.25, 2, 5);+ toePiece(-24, 0.78, 0.29, 0.04, 0.14, { a: PAINT2, b: PAINT, side: GUN }, 0.25, 2, 5);+ toePiece(180, 0.62, 0.28, -0.02, 0, { a: GUN, b: PAINT2, side: GUN }, 0.23, 1, 4);+ kit.mat('steel');+ kit.drum([0, SK.toeH, z0], [0, 0, 1], 0.17, 0.46, { bolts: 6, seg: 18 });+ kit.mat('armour');+ kit.block([0.04, 0.17, z0], [0.46, 0.27, 0.42], { col: GUN, c: 0.03 });+ const ftTop = kit.plate([[-0.14, 0.325, z0 + 0.20], [0.27, 0.27, z0 + 0.20], [0.27, 0.27, z0 - 0.20], [-0.14, 0.325, z0 - 0.20]], 0.03, { out: [0, 1, 0], col: PAINT2 });+ kit.bolts(ftTop, 0.024, 1, 0.016);+ kit.ventOn(ftTop, 0.22, 0.78, 0.2, 0.8, { n: 4 });+ const ftFront = kit.plate([[0.20, 0.312, z0 + 0.215], [0.335, 0.225, z0 + 0.215], [0.335, 0.225, z0 - 0.215], [0.20, 0.312, z0 - 0.215]], 0.03, { out: [0.6, 1, 0], col: PAINT });+ kit.bolts(ftFront, 0.022, 1, 0.014);+ const ftRear = kit.plate([[-0.15, 0.335, z0 + 0.205], [-0.15, 0.335, z0 - 0.205], [-0.28, 0.20, z0 - 0.195], [-0.28, 0.20, z0 + 0.195]], 0.03, { out: [-0.7, 1, 0], col: GUN });+ kit.bolts(ftRear, 0.022, 1, 0.014);+ kit.streak(ftRear, [-0.17, 0.32, z0 + 0.08], 0.22, 0.07, 'oil');+ const toeB = [0.38, 0.30, 0], toeBb = [0.42, 0.20, 0];+ kit.lug([toeB[0], toeB[1], 0], [toeBb[0], toeBb[1], 0], z0, { col: GUN, ear: 0.055, w: 0.055, foot: 0.08, t: 0.02 });++ // ---- Leg hydraulics: each spans one joint and is aim-constrained at both ends.+ const pelvisAnchor = [-0.72, 3.50, z0];+ kit.piston('Hip_' + side, pelvisG, pelvisAnchor, hipG, hipLugT, { r: 0.085, rod: 0.05 });+ kit.piston('Knee_' + side, hipG, kneeA, kneeG, hornK, { r: 0.098, rod: 0.058, hose: CAB_R });+ kit.piston('Ankle_' + side, kneeG, calfA, ankleG, hornA, { r: 0.098, rod: 0.058, hose: CAB_R });+ kit.piston('Toe_' + side, ankleG, toeUp, toeG, [toeB[0], toeB[1], z0], { r: 0.052, rod: 0.03, seg: 10 });+ kit.mirror(1);+ return { hipG, kneeG, ankleG, toeG };+}++// ---------------------------------------------------------------- pelvis+function buildPelvis(kit, pelvisG) {+ const z0 = SK.legZ, y0 = SK.hip[1];+ kit.use(pelvisG); kit.frame(null); kit.mirror(1); kit.hull([0, y0, 0]); kit.mat('armour');+ const oct = (y, xf, xr, hz, cut) => [[xr + cut, y, hz], [xf - cut, y, hz], [xf, y, hz - cut], [xf, y, -hz + cut], [xf - cut, y, -hz], [xr + cut, y, -hz], [xr, y, -hz + cut], [xr, y, hz - cut]];+ kit.loft([oct(3.00, 0.34, -0.34, 0.34, 0.08), oct(3.18, 0.56, -0.58, 0.50, 0.12), oct(3.62, 0.60, -0.66, 0.52, 0.12), oct(3.80, 0.50, -0.54, 0.50, 0.10)], { col: DARK });+ const f0 = kit.plate([[0.635, 3.66, 0.27], [0.635, 3.66, -0.27], [0.60, 3.20, -0.24], [0.60, 3.20, 0.24]], 0.06, { col: BONE });+ kit.loop(f0, 0.05); kit.bolts(f0, 0.03, 1, 0.016);+ kit.stencil([0.612, 3.34, 0.13], [0, 0, -1], [0.076, 0.997, 0], 'H-7', 0.13, RUST, [0.997, -0.076, 0]);+ kit.hazardPlate([[0.60, 3.19, 0.24], [0.60, 3.19, -0.24], [0.38, 2.99, -0.16], [0.38, 2.99, 0.16]], 0.04, { out: [1, -0.8, 0], sw: 0.05 });+ const rr0 = kit.plate([[-0.70, 3.66, 0.30], [-0.70, 3.66, -0.30], [-0.62, 3.20, -0.26], [-0.62, 3.20, 0.26]], 0.05, { col: PAINT2 });+ kit.vent([-0.692, 3.58, 0.20], [-0.692, 3.58, -0.20], [-0.642, 3.30, -0.17], [-0.642, 3.30, 0.17], { n: 6 });+ kit.seam(rr0, [-0.63, 3.26, 0.24], [-0.63, 3.26, -0.24]);+ kit.plate([[-0.62, 3.19, 0.26], [-0.62, 3.19, -0.26], [-0.38, 2.99, -0.18], [-0.38, 2.99, 0.18]], 0.04, { col: GUN, out: [-1, -0.8, 0] });+ for (const s of [1, -1]) {+ kit.mirror(s); kit.mat('steel');+ kit.drum([0, y0, 0.53], [0, 0, 1], 0.46, 0.13, { gear: 32, bolts: 12, gearW: 0.7, seg: 26 });+ kit.mat('armour');+ const a = kit.plate([[0.635, 3.68, 0.28], [0.635, 3.68, 0.525], [0.60, 3.20, 0.505], [0.60, 3.20, 0.25]], 0.05);+ kit.bolts(a, 0.03, 1, 0.016);+ kit.streak(a, [0.63, 3.64, 0.40], 0.42, 0.09, 'oil');+ kit.plate([[0.60, 3.19, 0.25], [0.60, 3.19, 0.505], [0.36, 3.02, 0.345], [0.38, 2.99, 0.17]], 0.04, { col: PAINT2, out: [1, -0.8, 0.3] });+ kit.plate([[-0.70, 3.68, 0.31], [-0.70, 3.68, 0.53], [-0.62, 3.20, 0.51], [-0.62, 3.20, 0.27]], 0.05, { col: PAINT2 });+ kit.plate([[-0.62, 3.19, 0.27], [-0.62, 3.19, 0.51], [-0.36, 3.02, 0.345], [-0.38, 2.99, 0.19]], 0.04, { col: GUN, out: [-1, -0.8, 0.3] });+ const t = kit.plate([[0.50, 3.84, 0.53], [-0.56, 3.84, 0.53], [-0.60, 3.68, 0.555], [0.56, 3.68, 0.555]], 0.04, { col: PAINT, out: [0, 0.7, 1] });+ kit.seam(t, [0.2, 3.76, 0.545], [-0.2, 3.76, 0.545]);+ // Outrigger carrying the hip piston's upper pin+ kit.mat('steel');+ kit.block([-0.70, 3.50, 0.63], [0.13, 0.15, 0.60], { col: GUN, c: 0.02 });+ kit.mat('rubber');+ kit.tube([[-0.58, 3.62, 0.30], [-0.70, 3.62, 0.42], [-0.76, 3.54, 0.62], [-0.74, 3.42, 0.78], [-0.46, 3.36, 0.72], [-0.1, 3.42, 0.60], [0, y0, 0.60]], 0.017, { col: CAB_K, sp: 3 });+ kit.mat('armour');+ kit.lug([-0.72, 3.50, 0], [-0.70, 3.50, 0], z0, { col: GUN, ear: 0.085, foot: 0.085, w: 0.085 });+ }+ kit.mirror(1); kit.mat('steel');+ kit.lathe([0, 3.76, 0], [0, 1, 0], [[0, 0.46, GM_D], [0.02, 0.60, GM_H, 1], [0.03, 0.60, GM], [0.09, 0.60, GM_H, 1], [0.11, 0.52, GM_D], [0.15, 0.50, GM], [0.15, 0, GM]], { seg: 32 });+ const NT = 40;+ for (let i = 0; i < NT; i++) {+ const th = (i / NT) * 2 * Math.PI, rad = [Math.cos(th), 0, Math.sin(th)], tan = [-Math.sin(th), 0, Math.cos(th)];+ kit.block(vAdd([0, 3.825, 0], vMul(rad, 0.612)), [0.03, 0.05, 0.05], { ax: [rad, [0, 1, 0], tan], c: 0, col: GM_L, polish: true });+ }+ kit.mat('armour');+}++// ---------------------------------------------------------------- torso, cockpit, backpack+function buildTorso(kit, waistG) {+ kit.use(waistG); kit.frame(null); kit.mirror(1); kit.hull([0, 4.6, 0]); kit.mat('armour');+ const tRing = (y, f, n, r, h) => {+ const c = Math.max(0.56, h * 0.62);+ return [[r + 0.18, y, h], [f - 0.30, y, h], [f, y, c], [f, y, 0.52], [n, y, 0.50], [n, y, -0.50], [f, y, -0.52], [f, y, -c], [f - 0.30, y, -h], [r + 0.18, y, -h], [r, y, -h * 0.55], [r, y, h * 0.55]];+ };+ kit.loft([tRing(3.96, 0.50, 0.50, -0.50, 0.60), tRing(4.25, 0.72, 0.72, -0.70, 0.88), tRing(4.56, 0.86, 0.86, -0.84, 1.10), tRing(4.60, 0.87, 0.40, -0.85, 1.12), tRing(5.10, 0.84, 0.40, -0.92, 1.18), tRing(5.40, 0.62, 0.40, -0.88, 1.02), tRing(5.44, 0.60, 0.60, -0.88, 1.00), tRing(5.62, 0.25, 0.25, -0.70, 0.72)], { col: DARK });+ kit.mat('steel');+ kit.lathe([0, 3.90, 0], [0, 1, 0], [[0, 0, STEEL_D], [0, 0.50, STEEL_D], [0.04, 0.50, GUN], [0.08, 0.58, GUN]], { seg: 28 });+ for (const s of [1, -1]) { kit.mirror(s); kit.drum([0.34, 3.97, 0.36], [0, 1, 0], 0.09, 0.14, { bolts: 0, seg: 12 }); }+ kit.mirror(1); kit.mat('armour');++ // Abdomen bands+ const ab1 = kit.plate([[0.76, 4.27, 0.56], [0.76, 4.27, -0.56], [0.63, 4.13, -0.50], [0.63, 4.13, 0.50]], 0.05, { col: PAINT2 });+ kit.bolts(ab1, 0.026, 2, 0.014);+ const ab2 = kit.plate([[0.67, 4.15, 0.48], [0.67, 4.15, -0.48], [0.55, 4.02, -0.42], [0.55, 4.02, 0.42]], 0.05, { col: GUN });+ kit.loop(ab2, 0.03, 0.009);+ kit.hazardPlate([[0.56, 4.035, 0.40], [0.56, 4.035, -0.40], [0.50, 3.94, -0.36], [0.50, 3.94, 0.36]], 0.04, { sw: 0.05 });+ const rb1 = kit.plate([[-0.72, 4.26, 0.56], [-0.72, 4.26, -0.56], [-0.52, 3.98, -0.40], [-0.52, 3.98, 0.40]], 0.05, { col: GUN });+ kit.bolts(rb1, 0.03, 1, 0.014);++ for (const s of [1, -1]) {+ kit.mirror(s);+ // Cheek intake block+ const c0 = kit.plate([[0.915, 4.62, 0.53], [0.915, 4.62, 0.70], [0.885, 5.08, 0.74], [0.885, 5.08, 0.53]], 0.05, { col: BONE });+ kit.bolts(c0, 0.026, 1, 0.014);+ const ob = kit.plate([[0.915, 4.60, 0.71], [0.60, 4.60, 1.15], [0.58, 5.09, 1.215], [0.89, 5.09, 0.75]], 0.06, { col: PAINT });+ kit.vent([0.885, 4.66, 0.745], [0.635, 4.66, 1.095], [0.625, 4.92, 1.135], [0.875, 4.92, 0.77], { n: 6, col: GUN });+ kit.seam(ob, [0.885, 4.97, 0.76], [0.62, 4.97, 1.15]); kit.bolts(ob, 0.03, 1, 0.016);+ kit.mat('glowAmber');+ kit.lathe([0.916, 4.75, 0.615], [1, 0, 0], [[0, 0.03], [0.012, 0.022], [0.012, 0]], { seg: 8, mat: 'glowAmber' });+ kit.mat('armour');+ const br = kit.plate([[0.90, 5.11, 0.53], [0.90, 5.11, 0.77], [0.66, 5.41, 0.68], [0.66, 5.41, 0.53]], 0.055, { col: PAINT2 });+ kit.bolts(br, 0.028, 1, 0.014);+ const br2 = kit.plate([[0.60, 5.12, 1.22], [0.89, 5.12, 0.79], [0.66, 5.41, 0.70], [0.42, 5.41, 1.03]], 0.05, { col: PAINT });+ kit.loop(br2, 0.04);+ kit.plate([[0.885, 4.575, 0.50], [0.885, 4.575, 0.71], [0.745, 4.255, 0.565], [0.745, 4.255, 0.50]], 0.05, { col: GUN });+ const fl = kit.plate([[0.885, 4.575, 0.72], [0.585, 4.575, 1.125], [0.43, 4.255, 0.90], [0.745, 4.255, 0.575]], 0.055, { col: PAINT });+ kit.seam(fl, [0.81, 4.415, 0.635], [0.51, 4.415, 1.01]); kit.bolts(fl, 0.03, 1, 0.016);+ const sl = kit.plate([[0.42, 4.26, 0.915], [-0.42, 4.26, 0.915], [-0.54, 4.56, 1.135], [0.56, 4.56, 1.135]], 0.05, { col: PAINT2 });+ kit.seam(sl, [0.1, 4.28, 0.93], [0.14, 4.54, 1.125]); kit.seam(sl, [-0.2, 4.28, 0.93], [-0.24, 4.54, 1.125]);+ kit.hazardPlate([[0.40, 4.225, 0.875], [-0.40, 4.225, 0.875], [-0.42, 4.14, 0.80], [0.42, 4.14, 0.80]], 0.03, { sw: 0.06, out: [0, -0.5, 1] });+ kit.plate([[0.52, 4.60, 1.155], [0.52, 5.09, 1.215], [0.30, 5.09, 1.215], [0.30, 4.60, 1.155]], 0.04, { col: PAINT });+ const srr = kit.plate([[-0.26, 4.60, 1.155], [-0.26, 5.09, 1.215], [-0.70, 5.09, 1.215], [-0.70, 4.60, 1.155]], 0.05, { col: PAINT });+ kit.loop(srr, 0.04); kit.bolts(srr, 0.025, 2, 0.014);+ kit.streak(srr, [-0.32, 5.04, 1.21], 0.4, 0.1, 'oil');+ const rf = kit.plate([[0.38, 5.46, 1.015], [-0.66, 5.46, 1.015], [-0.53, 5.64, 0.735], [0.10, 5.64, 0.735]], 0.05, { col: PAINT });+ kit.bolts(rf, 0.03, 2, 0.014); kit.seam(rf, [-0.12, 5.47, 1.0], [-0.2, 5.63, 0.74]);+ // Shoulder yoke+ kit.mat('steel');+ kit.drum([0.02, 5.00, 1.255], [0, 0, 1], 0.40, 0.17, { gear: 30, bolts: 10, gearW: 0.6, seg: 24 });+ kit.block([0.02, 4.70, 1.20], [0.34, 0.34, 0.13], { col: GUN, c: 0.03 });+ kit.mat('rubber');+ kit.tube([[-0.74, 5.12, 1.05], [-0.68, 5.18, 1.26], [-0.34, 5.27, 1.37], [-0.05, 5.1, 1.37], [0.02, 5.0, 1.36]], 0.02, { col: CAB_K });+ kit.tube([[-0.74, 5.05, 1.05], [-0.70, 5.08, 1.27], [-0.40, 5.15, 1.37], [-0.08, 5.05, 1.37], [0.02, 5.0, 1.36]], 0.016, { col: CAB_R });+ kit.mat('armour');+ }+ kit.mirror(1);+ // Brow, roof, hatch+ const fh = kit.plate([[0.625, 5.46, 0.60], [0.625, 5.46, -0.60], [0.27, 5.645, -0.43], [0.27, 5.645, 0.43]], 0.055, { col: PAINT2 });+ kit.bolts(fh, 0.03, 2, 0.014);+ const rft = kit.plate([[0.26, 5.645, 0.70], [0.26, 5.645, -0.70], [-0.52, 5.645, -0.70], [-0.52, 5.645, 0.70]], 0.04, { col: PAINT2 });+ kit.loop(rft, 0.06);+ kit.mat('steel');+ kit.lathe([-0.14, 5.645, 0.12], [0, 1, 0], [[0, 0.24, GUN], [0.035, 0.24, GUN], [0.05, 0.21, STEEL_D], [0.05, 0, STEEL_D]], { seg: 16 });+ kit.block([-0.14, 5.71, 0.12], [0.30, 0.03, 0.04], { col: STEEL });+ // Sensor pod and whip antenna+ kit.block([0.05, 5.70, -0.34], [0.26, 0.11, 0.28], { col: GUN, c: 0.02 });+ kit.cyl([-0.42, 5.65, -0.52], [-0.42, 5.98, -0.52], 0.014, 0.006, { seg: 6, col: RUB });+ kit.cyl([-0.42, 5.65, -0.40], [-0.42, 5.84, -0.40], 0.012, 0.005, { seg: 6, col: RUB });+ kit.lathe([0.18, 5.70, -0.34], [1, 0, 0], [[0, 0.04, GUN], [0.02, 0.04, GUN], [0.02, 0.028, GUN]], { seg: 10 });+ kit.mat('glowCyan');+ kit.lathe([0.20, 5.70, -0.34], [1, 0, 0], [[0, 0.028], [0.006, 0.02], [0.006, 0]], { seg: 10, mat: 'glowCyan' });+ kit.mat('armour');++ // ---- Chin tub+ kit.hull([1.0, 4.40, 0]);+ const chinRing = (x, yt, yb, z, cut) => [[x, yt, z], [x, yt, -z], [x, yb + cut, -z], [x, yb, -z + cut], [x, yb, z - cut], [x, yb + cut, z]];+ kit.loft([chinRing(0.66, 4.60, 4.10, 0.50, 0.10), chinRing(1.12, 4.60, 4.33, 0.47, 0.08), chinRing(1.43, 4.62, 4.49, 0.25, 0.05)], { col: DARK });+ const ch1 = kit.plate([[1.15, 4.34, 0.45], [1.15, 4.34, -0.45], [0.70, 4.09, -0.47], [0.70, 4.09, 0.47]], 0.05, { col: PAINT });+ kit.bolts(ch1, 0.03, 2, 0.014); kit.seam(ch1, [0.92, 4.21, 0.44], [0.92, 4.21, -0.44]);+ const ch2 = kit.plate([[1.45, 4.49, 0.235], [1.45, 4.49, -0.235], [1.17, 4.33, -0.40], [1.17, 4.33, 0.40]], 0.05, { col: PAINT2 });+ kit.bolts(ch2, 0.025, 1, 0.012);+ kit.hazardPlate([[1.455, 4.63, 0.235], [1.455, 4.63, -0.235], [1.455, 4.50, -0.235], [1.455, 4.50, 0.235]], 0.03, { sw: 0.05, out: [1, 0, 0] });+ for (const s of [1, -1]) {+ kit.mirror(s);+ const cs = kit.plate([[1.13, 4.61, 0.485], [0.68, 4.61, 0.515], [0.68, 4.15, 0.515], [1.13, 4.37, 0.485]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.seam(cs, [0.90, 4.60, 0.50], [0.90, 4.24, 0.50]); kit.bolts(cs, 0.026, 1, 0.014);+ kit.plate([[1.44, 4.62, 0.26], [1.15, 4.61, 0.47], [1.15, 4.37, 0.47], [1.44, 4.51, 0.26]], 0.04, { col: GUN, out: [0.5, 0, 1] });+ // Lamps+ kit.mat('steel');+ kit.lathe([1.22, 4.42, 0.335], [1, 0.1, 0.25], [[0, 0, GUN], [0, 0.05, GUN], [0.05, 0.05, GUN], [0.05, 0.036, GUN]], { seg: 10 });+ kit.mat('glowCyan');+ kit.lathe(vAdd([1.22, 4.42, 0.335], vMul(vUnit([1, 0.1, 0.25]), 0.04)), [1, 0.1, 0.25], [[0, 0.036], [0.006, 0]], { seg: 10, mat: 'glowCyan' });+ kit.mat('armour');+ }+ kit.mirror(1);++ // ---- Cockpit interior+ kit.hull(null); kit.mat('armour');+ kit.poly([[0.40, 4.606, 0.49], [1.14, 4.606, 0.46], [1.39, 4.606, 0.24], [1.39, 4.606, -0.24], [1.14, 4.606, -0.46], [0.40, 4.606, -0.49]], lin(0x2a2c2e), { out: [0, 1, 0] });+ kit.mat('steel');+ kit.block([0.46, 4.97, 0], [0.09, 0.66, 0.44], { col: GUN, c: 0.02, edge: tone(GUN, 1.25) });+ kit.block([0.47, 5.27, 0], [0.08, 0.13, 0.20], { col: GUN, c: 0.015, edge: tone(GUN, 1.25) });+ kit.block([0.70, 4.70, 0], [0.42, 0.10, 0.44], { col: GUN, c: 0.02, edge: tone(GUN, 1.25) });+ kit.mat('rubber');+ // Seated pilot: rounded limbs in a flight suit, dark visor+ kit.lathe([0.62, 4.74, 0], [0, 1, 0], [[0, 0, SUIT], [0, 0.11, SUIT], [0.10, 0.135, SUIT], [0.27, 0.16, SUIT], [0.35, 0.12, SUIT], [0.37, 0.05, RUB], [0.43, 0.04, RUB], [0.43, 0, RUB]], { seg: 10, mat: 'rubber' });+ for (const sz of [1, -1]) {+ kit.tube([[0.64, 5.04, sz * 0.15], [0.70, 4.92, sz * 0.21], [0.84, 4.88, sz * 0.19], [0.95, 4.85, sz * 0.13]], 0.046, { col: SUIT, seg: 7, sp: 2 });+ kit.tube([[0.66, 4.78, sz * 0.09], [0.84, 4.82, sz * 0.10], [0.99, 4.80, sz * 0.10], [1.05, 4.64, sz * 0.10]], 0.066, { col: tone(SUIT, 0.85), seg: 7, sp: 2 });+ }+ kit.lathe([0.64, 5.12, 0], [0, 1, 0], [[0, 0, BONE], [0, 0.07, BONE], [0.05, 0.115, BONE], [0.14, 0.12, BONE], [0.20, 0.075, BONE], [0.22, 0, BONE]], { seg: 12, mat: 'rubber' });+ kit.poly([[0.76, 5.26, 0.085], [0.76, 5.26, -0.085], [0.76, 5.18, -0.085], [0.76, 5.18, 0.085]], tone(RUB, 0.5), { out: [1, 0, 0], mat: 'rubber' });+ kit.mat('steel');+ kit.block([1.08, 4.76, 0], [0.32, 0.30, 0.62], { col: GUN, c: 0.02, ax: [vUnit([1, 0.45, 0]), vUnit([-0.45, 1, 0]), [0, 0, 1]] });+ kit.mat('glowCyan');+ const scr = (z0, w) => kit.poly([[0.985, 4.93, z0 + w], [0.985, 4.93, z0 - w], [0.915, 4.80, z0 - w], [0.915, 4.80, z0 + w]].map((p) => [p[0] + 0.012, p[1] + 0.012, p[2]]), WHITE, { out: [-1, 0.5, 0], mat: 'glowCyan' });+ scr(0, 0.10); kit.mat('glowAmber');+ kit.poly([[1.00, 4.93, 0.27], [1.00, 4.93, 0.15], [0.93, 4.80, 0.15], [0.93, 4.80, 0.27]], WHITE, { out: [-1, 0.5, 0], mat: 'glowAmber' });+ kit.mat('glowCyan');+ kit.poly([[1.00, 4.93, -0.15], [1.00, 4.93, -0.27], [0.93, 4.80, -0.27], [0.93, 4.80, -0.15]], WHITE, { out: [-1, 0.5, 0], mat: 'glowCyan' });+ kit.mat('armour');++ // ---- Glass canopy and frame+ const A0b = [0.80, 4.60, 0.50], A0s = [0.80, 5.18, 0.50], A0t = [0.80, 5.42, 0.26];+ const A1b = [1.14, 4.60, 0.47], A1s = [1.14, 5.04, 0.44], A1t = [1.14, 5.20, 0.20];+ const A2b = [1.40, 4.62, 0.25], A2t = [1.40, 4.84, 0.12];+ const mz = (p) => [p[0], p[1], -p[2]];+ const cc = [1.0, 4.9, 0];+ const pane = (pts) => kit.poly(pts, WHITE, { out: vSub(centroid(pts), cc), mat: 'glass' });+ pane([A0t, A1t, mz(A1t), mz(A0t)]);+ pane([A1t, A2t, mz(A2t), mz(A1t)]);+ pane([A2t, A2b, mz(A2b), mz(A2t)]);+ for (const f of [(p) => p, mz]) {+ pane([f(A0s), f(A1s), f(A1t), f(A0t)]);+ pane([f(A0b), f(A1b), f(A1s), f(A0s)]);+ pane([f(A1b), f(A2b), f(A2t), f(A1s)]);+ pane([f(A1s), f(A2t), f(A1t)]);+ }+ kit.mat('steel');+ const bar = (a, b, r = 0.026) => kit.cyl(a, b, r, r, { seg: 6, col: GUN });+ for (const f of [(p) => p, mz]) {+ bar(f(A0b), f(A0s), 0.038); bar(f(A0s), f(A0t), 0.038);+ bar(f(A1b), f(A1s)); bar(f(A1s), f(A1t));+ bar(f(A0s), f(A1s), 0.016); bar(f(A0t), f(A1t)); bar(f(A1t), f(A2t)); bar(f(A1s), f(A2t), 0.016); bar(f(A1b), f(A2b)); bar(f(A0b), f(A1b)); bar(f(A2b), f(A2t));+ }+ bar(A0t, mz(A0t), 0.03); bar(A1t, mz(A1t)); bar(A2t, mz(A2t)); bar(A2b, mz(A2b));+ kit.mat('armour');++ // ---- Backpack: reactor block, radiators, stacks+ kit.hull([-1.1, 4.8, 0]);+ const bRing = (x, y0, y1, z) => [[x, y1, z], [x, y1, -z], [x, y0, -z], [x, y0, z]];+ kit.loft([bRing(-0.78, 4.24, 5.46, 0.86), bRing(-1.22, 4.32, 5.40, 0.78), bRing(-1.46, 4.48, 5.22, 0.60)], { col: DARK });+ const bt = kit.plate([[-0.80, 5.49, 0.82], [-0.80, 5.49, -0.82], [-1.25, 5.43, -0.76], [-1.25, 5.43, 0.76]], 0.05, { col: PAINT });+ kit.loop(bt, 0.05); kit.bolts(bt, 0.026, 2, 0.014);+ kit.streak(bt, [-0.92, 5.48, 0.44], 0.34, 0.2, 'soot'); kit.streak(bt, [-0.92, 5.48, -0.44], 0.34, 0.2, 'soot');+ const br0 = kit.hazardPlate([[-1.26, 5.43, 0.74], [-1.26, 5.43, -0.74], [-1.46, 5.25, -0.58], [-1.46, 5.25, 0.58]], 0.03, { sw: 0.08, out: [-1, 1, 0] });+ kit.bolts(br0, 0.018, 1, 0.012);+ const rearP = kit.plate([[-1.49, 5.20, 0.56], [-1.49, 5.20, -0.56], [-1.49, 4.50, -0.56], [-1.49, 4.50, 0.56]], 0.03, { col: PAINT2, out: [-1, 0, 0] });+ kit.bolts(rearP, 0.03, 2, 0.016);+ kit.streak(rearP, [-1.49, 4.60, 0.28], 0.1, 0.26, 'soot'); kit.streak(rearP, [-1.49, 4.60, -0.28], 0.1, 0.26, 'soot');+ for (const z of [0.28, -0.28]) {+ kit.vent([-1.50, 4.60, z - 0.20], [-1.50, 4.60, z + 0.20], [-1.50, 5.08, z + 0.20], [-1.50, 5.08, z - 0.20], { n: 7, col: GUN, base: WHITE, glow: 'glowAmber', depth: 0.02 });+ }+ kit.stencil([-1.505, 4.52, 0.08], [0, 0, -1], [0, 1, 0], 'HE', 0.06, HAZ_Y, [-1, 0, 0]);+ for (const s of [1, -1]) {+ kit.mirror(s);+ const sd = kit.plate([[-0.82, 5.36, 0.875], [-1.20, 5.32, 0.795], [-1.20, 5.02, 0.795], [-0.82, 5.02, 0.875]], 0.04, { col: PAINT2, out: [0, 0, 1] });+ kit.bolts(sd, 0.024, 1, 0.012);+ kit.mat('steel');+ // Radiator bank: fins over a dark core, exposed on the flank+ kit.block([-1.02, 4.66, 0.82], [0.44, 0.60, 0.04], { col: tone(GUN, 0.4), c: 0 });+ for (let i = 0; i < 11; i++) kit.block([-0.82 - i * 0.04, 4.66, 0.86], [0.012, 0.56, 0.05], { col: STEEL_D, c: 0 });+ // Stack+ kit.lathe([-1.32, 5.30, 0.44], [-0.12, 1, 0], [[0, 0.11, GUN], [0.40, 0.095, GUN], [0.40, 0.12, STEEL_D], [0.50, 0.12, STEEL_D], [0.50, 0.08, tone(RUB, 0.6)], [0.42, 0.075, tone(RUB, 0.6)], [0.42, 0, tone(RUB, 0.6)]], { seg: 12 });+ // Coolant lines to the waist axis+ kit.mat('rubber');+ kit.tube([[-0.92, 4.26, 0.46], [-0.86, 4.04, 0.42], [-0.60, 3.96, 0.30], [-0.26, 3.95, 0.14], [0, 3.94, 0.02]], 0.024, { col: CAB_K });+ kit.tube([[-0.98, 4.26, 0.34], [-0.90, 4.03, 0.30], [-0.62, 3.95, 0.20], [-0.26, 3.95, 0.09], [0, 3.94, 0.01]], 0.018, { col: CAB_B });+ kit.mat('armour');+ }+ kit.mirror(1);++ // ---- Right shoulder sensor / counterweight (torso mounted)+ kit.hull(null);+ kit.block([-0.08, 5.51, 0.92], [0.50, 0.18, 0.38], { col: PAINT2, c: 0.03 });+ kit.mat('steel');+ for (const dz of [-0.09, 0.09]) {+ kit.lathe([0.17, 5.53, 0.92 + dz], [1, 0, 0], [[-0.02, 0, GUN], [-0.02, 0.06, GUN], [0.07, 0.066, GUN], [0.07, 0.042, tone(GUN, 0.4)], [0.05, 0.042, tone(GUN, 0.4)]], { seg: 12 });+ kit.mat('glowCyan');+ kit.lathe([0.22, 5.53, 0.92 + dz], [1, 0, 0], [[0, 0.042], [0.006, 0], [0.006, 0]], { seg: 12, mat: 'glowCyan' });+ kit.mat('steel');+ }+ kit.mat('armour');+ // Paint on the rear hull facets and backpack flanks+ kit.hull([-1.0, 4.8, 0]);+ for (const s of [1, -1]) {+ kit.mirror(s);+ const rh = kit.plate([[-0.66, 4.60, 1.125], [-0.82, 4.60, 0.90], [-0.87, 5.09, 0.90], [-0.71, 5.09, 1.19]], 0.05, { col: PAINT2, out: [-1, 0, 1] });+ kit.bolts(rh, 0.026, 1, 0.014);+ const bf = kit.plate([[-1.24, 5.38, 0.80], [-1.24, 4.34, 0.80], [-1.48, 4.50, 0.615], [-1.48, 5.20, 0.615]], 0.04, { col: PAINT, out: [-0.6, 0, 1] });+ kit.loop(bf, 0.04); kit.bolts(bf, 0.022, 1, 0.012);+ kit.plate([[-0.80, 4.21, 0.84], [-1.22, 4.29, 0.76], [-1.22, 4.29, 0.04], [-0.80, 4.21, 0.04]], 0.03, { col: PAINT2, out: [0, -1, 0] });+ }+ kit.mirror(1);+ const rs = kit.plate([[-0.52, 5.647, 0.70], [-0.52, 5.647, -0.70], [-0.78, 5.50, -0.84], [-0.78, 5.50, 0.84]], 0.05, { col: PAINT, out: [-0.5, 1, 0] });+ kit.bolts(rs, 0.03, 2, 0.014); kit.seam(rs, [-0.65, 5.575, 0.76], [-0.65, 5.575, -0.76]);+ kit.hull(null);+ // Pod pylon on the left shoulder line+ kit.mat('steel');+ kit.beam([-0.05, 5.44, -0.80], [-0.05, 5.50, -1.34], 0.13, 0.20, { col: GUN, c: 0.02, up: [0, 1, 0] });+ kit.mat('armour');+}++// ---------------------------------------------------------------- arms+function buildUpperArm(kit, side, waistG) {+ const s = side === 'L' ? -1 : 1, z0 = SK.armZ;+ kit.mirror(s);+ const G = kit.group('Shoulder_' + side, waistG, [SK.shoulder[0], SK.shoulder[1], s * z0], 'UpperArm_' + side);+ G.node.userData = { hinge: 'z' };+ const F = kit.bone(SK.shoulder, SK.elbow, z0);+ kit.use(G); kit.frame(F); kit.hull(null); kit.mat('steel');+ kit.drum([0, 0, -0.02], [0, 0, 1], 0.33, 0.50, { bolts: 10, seg: 24 });+ kit.mat('armour');+ // Pauldron core: the dark shadow gap behind every armour layer.+ kit.block([-0.02, 0.01, 0.05], [0.84, 0.74, 0.62], { col: DARK, c: 0.05 });++ // Crown: a cap and four sloped facets, each its own bevelled plate, with a bolted spine on top.+ const crown = kit.plate([[-0.47, -0.19, 0.19], [-0.47, 0.21, 0.19], [-0.47, 0.21, -0.19], [-0.47, -0.19, -0.19]], 0.05, { col: PAINT, out: [-1, 0, 0] });+ kit.bolts(crown, 0.024, 1, 0.017);+ const spine = kit.plate([[-0.476, -0.15, 0.05], [-0.476, 0.17, 0.05], [-0.476, 0.17, -0.05], [-0.476, -0.15, -0.05]], 0.012, { col: GUN, out: [-1, 0, 0], noBack: true, c: 0.01 });+ const cO = kit.plate([[-0.47, -0.19, 0.195], [-0.47, 0.21, 0.195], [-0.34, 0.35, 0.345], [-0.34, -0.33, 0.345]], 0.05, { col: PAINT2, out: [-0.6, 0, 0.8] });+ kit.bolts(cO, 0.026, 1, 0.017); kit.seam(cO, cO.at(0.5, 0.06), cO.at(0.5, 0.94));+ const cF = kit.plate([[-0.47, 0.215, 0.19], [-0.47, 0.215, -0.19], [-0.33, 0.41, -0.27], [-0.33, 0.41, 0.335]], 0.05, { col: PAINT, out: [-0.6, 0.8, 0] });+ kit.bolts(cF, 0.026, 1, 0.017);+ const cR = kit.plate([[-0.47, -0.195, -0.19], [-0.47, -0.195, 0.19], [-0.33, -0.38, 0.335], [-0.33, -0.38, -0.27]], 0.05, { col: PAINT2, out: [-0.6, -0.8, 0] });+ kit.bolts(cR, 0.026, 1, 0.017);+ const cI = kit.plate([[-0.47, 0.21, -0.195], [-0.47, -0.19, -0.195], [-0.33, -0.36, -0.295], [-0.33, 0.39, -0.295]], 0.05, { col: PAINT2, out: [-0.6, 0, -0.8] });+ kit.bolts(cI, 0.026, 1, 0.017);++ // Outer face: three lames lapped like shingles, flaring outward toward the bottom.+ const L1 = kit.plate([[-0.35, -0.345, 0.345], [-0.35, 0.365, 0.345], [-0.05, 0.41, 0.39], [-0.05, -0.385, 0.39]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.bolts(L1, 0.03, 2, 0.019);+ kit.seam(L1, L1.at(0.34, 0.05), L1.at(0.34, 0.95));+ kit.ventOn(L1, 0.5, 0.9, 0.2, 0.78, { n: 4 });+ const L2 = kit.plate([[-0.10, -0.37, 0.37], [-0.10, 0.395, 0.37], [0.20, 0.40, 0.41], [0.20, -0.385, 0.41]], 0.05, { col: PAINT2, out: [0, 0, 1] });+ kit.bolts(L2, 0.03, 2, 0.019);+ const num = kit.plate([[-0.03, -0.20, 0.395], [-0.03, 0.22, 0.395], [0.17, 0.22, 0.423], [0.17, -0.20, 0.423]], 0.022, { col: BONE, out: [0, 0, 1], noBack: true, c: 0.01 });+ kit.bolts(num, 0.018, 1, 0.012);+ kit.stencil(num.at(0.1, 0.92, 0), [0, 1, 0], [-0.99, 0, -0.14], side === 'R' ? '07' : '12', 0.15, RUST, num.n);+ const L3 = kit.plate([[0.15, -0.365, 0.39], [0.15, 0.38, 0.39], [0.46, 0.27, 0.43], [0.46, -0.25, 0.43]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.bolts(L3, 0.03, 1, 0.019);+ kit.hazardPlate([L3.at(0.1, 0.58, 0.008), L3.at(0.9, 0.58, 0.008), L3.at(0.9, 0.94, 0.008), L3.at(0.1, 0.94, 0.008)], 0.02, { out: L3.n, sw: 0.05, c: 0.008, noBack: true });++ // Front: main plate and intake louvres. Rear: heat-sink fins. Inner: bolted cover. Underside: dark closing plate.+ const pF = kit.plate([[-0.33, 0.425, 0.335], [0.30, 0.405, 0.38], [0.30, 0.405, -0.26], [-0.33, 0.425, -0.27]], 0.05, { col: PAINT2, out: [0, 1, 0] });+ kit.bolts(pF, 0.03, 1, 0.019);+ kit.ventOn(pF, 0.06, 0.52, 0.14, 0.86, { n: 5 });+ kit.seam(pF, pF.at(0.6, 0.05), pF.at(0.6, 0.95));+ const pR = kit.plate([[-0.33, -0.395, 0.335], [0.22, -0.385, 0.38], [0.22, -0.385, -0.26], [-0.33, -0.395, -0.27]], 0.05, { col: GUN, out: [0, -1, 0] });+ kit.bolts(pR, 0.028, 1, 0.017);+ kit.fins(pR, 0.1, 0.62, 0.18, 0.82, 7, 0.05, GM);+ const pI = kit.plate([[-0.33, 0.39, -0.30], [0.26, 0.37, -0.30], [0.26, -0.35, -0.30], [-0.33, -0.36, -0.30]], 0.04, { col: PAINT2, out: [0, 0, -1] });+ kit.bolts(pI, 0.03, 1, 0.017); kit.loop(pI, 0.06);+ kit.plate([[0.32, 0.38, 0.38], [0.32, -0.36, 0.38], [0.32, -0.34, -0.24], [0.32, 0.36, -0.24]], 0.03, { col: GUN, out: [1, 0, 0], noDrip: true });++ // Lower arm: faceted shell below the pauldron.+ const sec = kit.shell([+ { u: 0.40, vf: 0.24, vr: -0.25, wo: 0.25, wi: 0.25, c: 0.05 },+ { u: 0.72, vf: 0.20, vr: -0.21, wo: 0.22, wi: 0.22, c: 0.05 },+ { u: 0.96, vf: 0.17, vr: -0.17, wo: 0.19, wi: 0.19, c: 0.04 },+ ], { cols: { out: PAINT, rear: GUN, in: PAINT2, front: PAINT2, chamfer: PAINT2 } });+ kit.bolts(sec[1].out, 0.025, 1, 0.016); kit.seam(sec[1].out, sec[1].out.at(0.08, 0.5), sec[1].out.at(0.92, 0.5));+ kit.bolts(sec[1].front, 0.024, 1, 0.014);+ kit.bolts(sec[0].front, 0.024, 1, 0.014);+ kit.streak(sec[1].out, [0.56, -0.02, 0.25], 0.28, 0.07, 'oil');+ kit.mat('rubber');+ kit.cables([[0.40, -0.30, 0.33], [0.56, -0.29, 0.30], [0.74, -0.24, 0.285], [0.92, -0.18, 0.265], [F.L, 0, 0.29]], 0.025, [{ r: 0.018, col: CAB_K }, { r: 0.014, col: CAB_R }]);+ kit.mat('armour');+ const A = kit.at(F, 0.25, -0.46, 0), Ab = kit.at(F, 0.32, -0.26, 0);+ kit.frame(null); kit.hull(null);+ kit.lug(A, Ab, z0, { col: GUN, w: 0.08 });+ kit.mirror(1);+ return { G, A };+}++function forearmBase(kit, s, z0) {+ kit.mirror(s);+ kit.frame(null); kit.mat('steel');+ kit.drum([SK.elbow[0], SK.elbow[1], z0], [0, 0, 1], 0.27, 0.56, { bolts: 8, gear: 20, gearW: 0.35, seg: 22 });+ kit.mat('armour');+}++function buildCannonArm(kit, parent, upperA) {+ const z0 = SK.armZ, y = SK.elbow[1];+ const G = kit.group('Elbow_R', parent, [SK.elbow[0], y, z0], 'Forearm_R');+ G.node.userData = { hinge: 'z' };+ kit.use(G); forearmBase(kit, 1, z0);+ kit.frame({ o: [0, y, z0], u: [1, 0, 0], v: [0, 1, 0], w: [0, 0, 1] });+ // Faceted gun housing: breech, receiver, taper and barrel shroud.+ const sec = kit.shell([+ { u: 0.04, vf: 0.22, vr: -0.22, wo: 0.26, wi: 0.26, c: 0.05 },+ { u: 0.38, vf: 0.27, vr: -0.25, wo: 0.31, wi: 0.30, c: 0.06 },+ { u: 0.74, vf: 0.27, vr: -0.25, wo: 0.31, wi: 0.30, c: 0.06 },+ { u: 0.96, vf: 0.24, vr: -0.22, wo: 0.26, wi: 0.26, c: 0.06 },+ { u: 1.10, vf: 0.255, vr: -0.255, wo: 0.255, wi: 0.255, c: 0.07 },+ ], { cols: { front: PAINT, out: PAINT, rear: GUN, in: PAINT2, chamfer: PAINT2 }, t: 0.028 });+ const [s0, s1, s2, s3] = sec;+ kit.ventOn(s0.front, 0.18, 0.82, 0.2, 0.8, { n: 5 });+ kit.bolts(s0.front, 0.024, 1, 0.017);+ const sp = kit.onPlate(s1.front, 0.2, 0.8, 0.05, 0.95, 0.016, { col: PAINT2 });+ kit.bolts(sp, 0.024, 2, 0.018);+ kit.seam(sp, sp.at(0.5, 0.04), sp.at(0.5, 0.96));+ kit.hazardPlate([s2.front.at(0.08, 0.12, 0.012), s2.front.at(0.92, 0.12, 0.012), s2.front.at(0.92, 0.88, 0.012), s2.front.at(0.08, 0.88, 0.012)], 0.024, { out: s2.front.n, sw: 0.06, noBack: true, c: 0.01 });+ kit.bolts(s3.front, 0.02, 1, 0.017);+ const rp = kit.onPlate(s1.out, 0.14, 0.86, 0.1, 0.9, 0.018, { col: RUST });+ kit.bolts(rp, 0.024, 1, 0.018);+ kit.ventOn(s0.out, 0.18, 0.82, 0.24, 0.8, { n: 4 });+ kit.ventOn(s2.out, 0.2, 0.8, 0.2, 0.8, { n: 3 });+ kit.bolts(s3.out, 0.02, 1, 0.017);+ kit.loop(s1.in, 0.05); kit.bolts(s1.in, 0.028, 1, 0.017); kit.bolts(s3.in, 0.02, 1, 0.014);+ kit.seam(s1.rear, s1.rear.at(0.5, 0.05), s1.rear.at(0.5, 0.95)); kit.bolts(s1.rear, 0.025, 1, 0.014);++ // Ammo drum and feed+ kit.frame(null); kit.hull(null); kit.mat('steel');+ const dc = [0.40, y - 0.52, z0];+ kit.drum(dc, [0, 0, 1], 0.27, 0.46, { bolts: 10, seg: 24, gear: 16, gearW: 0.8 });+ kit.block([0.40, y - 0.29, z0], [0.30, 0.14, 0.34], { col: GM_D, c: 0.02, edge: GM_L });+ for (let i = 0; i < 5; i++) kit.lathe([0.40, y - 0.52, z0 - 0.17 + i * 0.085], [0, 0, 1], [[0, 0.272, GM], [0, 0.285, GM_H, 1], [0.018, 0.285, GM_D], [0.018, 0.272, GM_D]], { seg: 24 });+ kit.mat('armour');+ kit.hazardPlate([[0.14, y - 0.70, z0 + 0.24], [0.66, y - 0.70, z0 + 0.24], [0.66, y - 0.82, z0 + 0.24], [0.14, y - 0.82, z0 + 0.24]], 0.012, { sw: 0.05, out: [0, 0, 1], c: 0.006, noBack: true });+ kit.mat('rubber');+ kit.tube([[0.66, y - 0.42, z0 + 0.26], [0.86, y - 0.34, z0 + 0.30], [0.98, y - 0.24, z0 + 0.20], [1.03, y - 0.15, z0 + 0.05], [1.05, y, z0]], 0.03, { col: CAB_K, seg: 8 });+ kit.tube([[0.10, y + 0.02, z0 + 0.335], [0.34, y + 0.20, z0 + 0.378], [0.70, y + 0.21, z0 + 0.378], [0.90, y + 0.10, z0 + 0.30], [1.04, y + 0.03, z0 + 0.12], [1.05, y, z0]], 0.016, { col: CAB_R });+ kit.tube([[0.10, y - 0.02, z0 + 0.335], [0.34, y - 0.19, z0 + 0.378], [0.70, y - 0.20, z0 + 0.378], [0.90, y - 0.10, z0 + 0.30], [1.04, y - 0.03, z0 + 0.12], [1.05, y, z0]], 0.016, { col: CAB_Y });+ kit.mat('armour');+ const horn = [-0.46, y + 0.10, z0];+ kit.lug(horn, [-0.16, y + 0.06, z0], z0, { ear: 0.075, w: 0.10, foot: 0.13, t: 0.04 });+ kit.piston('Elbow_R', parent, [upperA[0], upperA[1], z0], G, horn, { r: 0.075, rod: 0.044 });++ // Rotary barrels spin about local X. Bare gunmetal, heat-blued toward the muzzle, polished at every shoulder.+ const spinG = kit.group('CannonSpin_R', G, [SK.cannon[0], SK.cannon[1], z0], 'CannonBarrels_R');+ spinG.node.userData = { hinge: 'x' };+ kit.use(spinG); kit.mat('steel');+ const sx = SK.cannon[0], R = 0.135, Lb = 1.36;+ kit.lathe([sx + 0.05, y, z0], [1, 0, 0], [+ [0, 0, GM_L], [0, 0.205, GM_H, 1], [0.012, 0.22, GM], [0.10, 0.22, GM_H, 1], [0.115, 0.205, GM_D], [0.12, 0.16, GM], [0.32, 0.16, GM_H, 1], [0.335, 0.14, GM_D],+ [0.34, 0.06, GM_L], [Lb, 0.06, GM_L], [Lb, 0, GM_L],+ ], { seg: 22 });+ for (const d of [0.16, 0.22, 0.28]) kit.lathe([sx + d, y, z0], [1, 0, 0], [[0, 0.16, GM_D], [0.004, 0.178, GM_H, 1], [0.016, 0.178, GM_D], [0.02, 0.16, GM_D]], { seg: 22 });+ for (let i = 0; i < 6; i++) {+ const th = (i / 6) * 2 * Math.PI, cy = y + R * Math.cos(th), cz = z0 + R * Math.sin(th);+ kit.lathe([sx + 0.28, cy, cz], [1, 0, 0], [+ [0, 0.046, GM, 0, GM], [Lb - 0.62, 0.046, GM, 0, HEAT_A], [Lb - 0.20, 0.046, GM_D], [Lb - 0.20, 0.058, GM_H, 1], [Lb - 0.19, 0.062, GM_D, 0, SOOT], [Lb - 0.07, 0.062, GM_H, 1],+ [Lb - 0.055, 0.052, GM_H, 1], [Lb - 0.05, 0.03, SOOT], [Lb - 0.16, 0.03, SOOT], [Lb - 0.16, 0, SOOT],+ ], { seg: 12 });+ }+ for (const d of [0.62, 1.00]) {+ kit.lathe([sx + d, y, z0], [1, 0, 0], [[0, 0, GM], [0, 0.205, GM_H, 1], [0.012, 0.218, GM], [0.052, 0.218, GM_H, 1], [0.064, 0.205, GM_D], [0.064, 0, GM_D]], { seg: 22 });+ for (let k = 0; k < 6; k++) {+ const a = ((k + 0.5) / 6) * 2 * Math.PI;+ kit.bolt([sx + d - 0.004, y + 0.192 * Math.cos(a), z0 + 0.192 * Math.sin(a)], [-1, 0, 0], 0.016, GM_L);+ }+ }+ kit.lathe([sx + Lb - 0.09, y, z0], [1, 0, 0], [[0, 0.222, GM_H, 1], [0.012, 0.222, GM_D], [0.05, 0.222, GM_H, 1], [0.06, 0.205, GM_D]], { seg: 22 });+ kit.mat('armour');+ return { G, spinG };+}++function buildRailCannonArm(kit, parent, upperA) {+ const z0 = SK.armZ, y = SK.elbow[1];+ const G = kit.group('Elbow_L', parent, [SK.elbow[0], y, -z0], 'Forearm_L');+ G.node.userData = { hinge: 'z' };+ kit.use(G); forearmBase(kit, -1, z0);+ kit.frame({ o: [0, y, z0], u: [1, 0, 0], v: [0, 1, 0], w: [0, 0, 1] });+ const [s0, s1, s2] = kit.shell([+ { u: 0.04, vf: 0.24, vr: -0.24, wo: 0.26, wi: 0.26, c: 0.05 },+ { u: 0.54, vf: 0.31, vr: -0.31, wo: 0.33, wi: 0.31, c: 0.07 },+ { u: 0.78, vf: 0.28, vr: -0.28, wo: 0.30, wi: 0.28, c: 0.07 },+ { u: 0.96, vf: 0.20, vr: -0.20, wo: 0.23, wi: 0.23, c: 0.05 },+ ], { cols: { front: PAINT, out: PAINT, rear: GUN, in: PAINT2, chamfer: PAINT2 } });+ kit.ventOn(s0.front, 0.2, 0.8, 0.14, 0.5, { n: 4 });+ const tp = kit.onPlate(s0.front, 0.14, 0.86, 0.56, 0.96, 0.016, { col: PAINT2 });+ kit.bolts(tp, 0.022, 1, 0.017);+ const sp = kit.onPlate(s1.front, 0.16, 0.84, 0.08, 0.92, 0.016, { col: PAINT2 });+ kit.bolts(sp, 0.022, 1, 0.018);+ kit.bolts(s2.front, 0.024, 1, 0.017);+ const so = kit.onPlate(s0.out, 0.14, 0.86, 0.14, 0.86, 0.016, { col: PAINT2 });+ kit.bolts(so, 0.024, 1, 0.018);+ const so0 = so.at(0.8, 0.28, 0), rD = vUnit(vSub(so.at(0.8, 0.8, 0), so0)), uD = vUnit(vSub(so.at(0.2, 0.28, 0), so0));+ kit.stencil(so0, rD, uD, 'H-7', 0.13, BONE, so.n);+ kit.hazardPlate([s1.out.at(0.1, 0.12, 0.012), s1.out.at(0.9, 0.12, 0.012), s1.out.at(0.9, 0.88, 0.012), s1.out.at(0.1, 0.88, 0.012)], 0.024, { out: s1.out.n, sw: 0.055, noBack: true, c: 0.01 });+ kit.ventOn(s2.out, 0.2, 0.8, 0.2, 0.8, { n: 3 });+ kit.loop(s0.in, 0.05); kit.bolts(s0.in, 0.028, 1, 0.017); kit.bolts(s1.in, 0.024, 1, 0.014);+ kit.seam(s0.rear, s0.rear.at(0.5, 0.05), s0.rear.at(0.5, 0.95)); kit.bolts(s0.rear, 0.025, 1, 0.014);+ kit.frame(null); kit.hull(null);+ kit.mat('rubber');+ kit.cables([[0.10, y - 0.02, z0 + 0.345], [0.32, y - 0.25, z0 + 0.40], [0.66, y - 0.25, z0 + 0.39], [0.86, y - 0.20, z0 + 0.27], [0.96, y, z0 + 0.18]], 0.022, [{ r: 0.018, col: CAB_K }, { r: 0.015, col: CAB_Y }]);+ kit.mat('armour');+ const horn = [-0.46, y + 0.10, z0];+ kit.lug(horn, [-0.16, y + 0.06, z0], z0, { ear: 0.075, w: 0.10, foot: 0.13, t: 0.04 });+ kit.piston('Elbow_L', parent, [upperA[0], upperA[1], z0], G, horn, { r: 0.075, rod: 0.044 });++ // Rail cannon on the wrist (mirrored into -Z). It draws on its own random sequence, then restores the one the+ // missile pod and pistons were authored against, so this weapon cannot repaint anything built after it.+ kit.reseed(RAIL_SEED);+ const wG = kit.group('Wrist_L', G, [SK.wrist[0], SK.wrist[1], -z0], 'RailCannon_L');+ wG.node.userData = { hinge: 'x' };+ kit.use(wG); kit.mat('steel');+ const wx = SK.wrist[0];+ kit.lathe([wx - 0.02, y, z0], [1, 0, 0], [[0, 0.19, GM], [0.05, 0.20, GM_H, 1], [0.06, 0.20, GM_D], [0.11, 0.17, GM_H, 1], [0.13, 0.15, GM_D]], { seg: 22 });+ kit.mat('armour');+ // Everything below is in a frame on the barrel axis: u along the barrel, v up, w outboard.+ kit.frame({ o: [0, y, z0], u: [1, 0, 0], v: [0, 1, 0], w: [0, 0, 1] }); kit.hull(null);+ const paint = { front: PAINT, out: PAINT, rear: GUN, in: PAINT2, chamfer: PAINT2 };+ // Breech: armoured rear block, a dark core holding the capacitor bank, fixed side armour, and a nose.+ const [rb] = kit.shell([{ u: 1.06, vf: 0.21, vr: -0.21, wo: 0.24, wi: 0.24, c: 0.06 }, { u: 1.27, vf: 0.24, vr: -0.24, wo: 0.30, wi: 0.30, c: 0.07 }], { cols: paint, t: 0.024 });+ kit.bolts(rb.out, 0.02, 1, 0.016); kit.bolts(rb.front, 0.02, 1, 0.016); kit.bolts(rb.in, 0.02, 1, 0.014);+ kit.block([1.61, 0, 0], [0.70, 0.24, 0.46], { col: DARK, c: 0.02 });+ for (let i = 0; i < 3; i++) for (const sw of [-1, 1]) for (const sv of [-1, 1]) kit.block([1.44 + i * 0.19, sv * 0.125, sw * 0.14], [0.13, 0.012, 0.17], { col: WHITE, c: 0, mat: 'glowCoil' });+ const sO = kit.plate([[1.27, -0.135, 0.31], [1.96, -0.125, 0.275], [1.96, 0.125, 0.275], [1.27, 0.135, 0.31]], 0.05, { col: PAINT, out: [0, 0, 1] });+ kit.bolts(sO, 0.022, 2, 0.017);+ const sP = kit.onPlate(sO, 0.06, 0.5, 0.14, 0.86, 0.014, { col: PAINT2 });+ const st0 = sP.at(0.14, 0.2, 0), stR = vUnit(vSub(sP.at(0.9, 0.2, 0), st0)), stU = vUnit(vSub(sP.at(0.14, 0.8, 0), st0));+ kit.stencil(st0, stR, stU, 'A-12', 0.085, BONE, sP.n);+ kit.ventOn(sO, 0.56, 0.93, 0.18, 0.82, { n: 4 });+ const sI = kit.plate([[1.27, 0.135, -0.31], [1.96, 0.125, -0.275], [1.96, -0.125, -0.275], [1.27, -0.135, -0.31]], 0.05, { col: PAINT2, out: [0, 0, -1] });+ kit.bolts(sI, 0.022, 2, 0.017); kit.loop(sI, 0.04);+ const [nose] = kit.shell([{ u: 1.95, vf: 0.20, vr: -0.20, wo: 0.25, wi: 0.25, c: 0.05 }, { u: 2.06, vf: 0.205, vr: -0.205, wo: 0.205, wi: 0.205, c: 0.06 }], { cols: paint, t: 0.024 });+ kit.bolts(nose.front, 0.016, 1, 0.014); kit.bolts(nose.out, 0.016, 1, 0.014);+ // Twin exposed rails with copper contact faces, running through every coil and out past the muzzle collar as a fork.+ kit.mat('steel');+ for (const sv of [-1, 1]) {+ kit.block([2.95, sv * 0.08, 0], [1.94, 0.056, 0.13], { col: GM, c: 0.012, edge: GM_H });+ kit.block([2.95, sv * 0.0495, 0], [1.90, 0.008, 0.09], { col: COPPER, c: 0 });+ kit.block([3.905, sv * 0.08, 0], [0.03, 0.064, 0.14], { col: GM_L, c: 0.01, polish: true });+ }+ // Stanchions carry the spine down onto the upper rail, in the gaps between coils.+ for (const u of [2.225, 2.395, 2.565, 3.015, 3.185, 3.355]) kit.block([u, 0.1665, 0], [0.03, 0.117, 0.06], { col: GM_D, c: 0, edge: GM });+ // Coils: a gunmetal spool with polished flanges, wound with four separate turns of glowing wire over a dark core.++ const coil = (cu) => {+ kit.mat('steel');+ kit.lathe([cu - 0.065, 0, 0], [1, 0, 0], [[0, 0.10, GM_D], [0, 0.168, GM_H, 1], [0.006, 0.178, GM], [0.012, 0.178, GM_D], [0.012, 0.132, GM_D], [0.118, 0.132, GM_D], [0.118, 0.178, GM], [0.124, 0.178, GM_H, 1], [0.13, 0.168, GM_D], [0.13, 0.10, GM_D]], { seg: 14 });+ const turns = 4, n = turns * 12, pts = [];+ for (let i = 0; i <= n; i++) { const t = i / n, a = t * turns * 2 * Math.PI; pts.push([cu - 0.048 + t * 0.096, 0.141 * Math.cos(a), 0.141 * Math.sin(a)]); }+ kit.tube(pts, 0.0078, { mat: 'glowCoil', seg: 5, sp: 1 });+ };+ for (const cu of [2.14, 2.31, 2.48, 2.65, 2.93, 3.10, 3.27, 3.44]) coil(cu);+ kit.mat('armour');+ // Collars tie the barrel together; a spine and two power conduits run the length between them.+ const collar = (u0, u1) => kit.shell([{ u: u0, vf: 0.205, vr: -0.205, wo: 0.205, wi: 0.205, c: 0.06 }, { u: u1, vf: 0.205, vr: -0.205, wo: 0.205, wi: 0.205, c: 0.06 }], { cols: paint, t: 0.024 })[0];+ const cM = collar(2.74, 2.84), cZ = collar(3.53, 3.70);+ kit.bolts(cM.out, 0.014, 1, 0.012); kit.bolts(cM.front, 0.014, 1, 0.012); kit.bolts(cZ.front, 0.016, 1, 0.014);+ for (const f of [cZ.out, cZ.in]) kit.hazardPlate([f.at(0.04, 0.12, 0.008), f.at(0.96, 0.12, 0.008), f.at(0.96, 0.88, 0.008), f.at(0.04, 0.88, 0.008)], 0.02, { out: f.n, sw: 0.04, noBack: true, c: 0.008 });+ const spine = kit.plate([[2.06, 0.275, 0.05], [3.60, 0.275, 0.05], [3.60, 0.275, -0.05], [2.06, 0.275, -0.05]], 0.05, { col: GUN, out: [0, 1, 0], c: 0.014 });+ for (const u of [2.5, 3.1]) kit.seam(spine, [u, 0.275, 0.045], [u, 0.275, -0.045], 0.012);+ for (let u = 2.14; u < 3.6; u += 0.26) for (const sw of [-1, 1]) kit.bolt([u, 0.275, sw * 0.03], [0, 1, 0], 0.012);+ kit.mat('rubber');+ kit.tube([[1.96, -0.16, 0.07], [2.2, -0.225, 0.07], [2.8, -0.225, 0.07], [3.3, -0.225, 0.07], [3.54, -0.19, 0.07]], 0.018, { col: CAB_B, sp: 2 });+ kit.tube([[1.96, -0.16, -0.07], [2.2, -0.225, -0.07], [2.8, -0.225, -0.07], [3.3, -0.225, -0.07], [3.54, -0.19, -0.07]], 0.018, { col: CAB_K, sp: 2 });+ // Muzzle: a glowing ring and bore behind the fork, with side cheeks tying the prongs together.+ kit.lathe([3.70, 0, 0], [1, 0, 0], [[0, 0.165], [0.012, 0.15], [0.012, 0.125]], { seg: 16, mat: 'glowCoil' });+ kit.block([3.715, 0, 0], [0.01, 0.10, 0.12], { col: WHITE, c: 0, mat: 'glowCoil' });+ kit.mat('steel');+ // Cheeks with venting ports either side of the fork.+ for (const sw of [-1, 1]) {+ kit.block([3.81, 0, sw * 0.072], [0.20, 0.205, 0.016], { col: GM, c: 0.005, edge: GM_H });+ for (const u of [3.75, 3.81, 3.87]) kit.block([u, 0, sw * 0.081], [0.026, 0.13, 0.004], { col: tone(GM_D, 0.3), c: 0 });+ }+ kit.mat('armour');+ kit.frame(null);+ // The three hatch pivots keep the claw's joint names and rest positions. Each carries an armour hatch over the+ // capacitor bank: positive rotation.z lifts the two upper hatches, negative rotation.z drops the lower one.+ const hx = wx + 0.35;+ const hatch = (name, label, hy, hz, sgnY, halfW, col) => {+ const fG = kit.group(name, wG, [hx, y + hy, -(z0 + hz)], label);+ fG.node.userData = { hinge: 'z' };+ kit.use(fG); kit.mat('steel');+ const zc = z0 + hz, Y = (h) => y + hy + sgnY * h;+ kit.drum([hx, y + hy, zc], [0, 0, 1], 0.04, halfW * 1.6, { bolts: 0, seg: 10 });+ kit.mat('armour');+ const h = kit.plate([[hx - 0.03, Y(0.055), zc + halfW], [hx + 0.64, Y(0.035), zc + halfW], [hx + 0.64, Y(0.035), zc - halfW], [hx - 0.03, Y(0.055), zc - halfW]], 0.05, { out: [0, sgnY, 0], col });+ kit.bolts(h, 0.024, 1, 0.017);+ // Dark liner on the inside face, seen when the hatch is open.+ kit.poly([[hx - 0.028, Y(0.0035), zc + halfW * 0.98], [hx + 0.638, Y(-0.0165), zc + halfW * 0.98], [hx + 0.638, Y(-0.0165), zc - halfW * 0.98], [hx - 0.028, Y(0.0035), zc - halfW * 0.98]], GM_D, { out: [0, -sgnY, 0], mat: 'steel' });+ return h;+ };+ for (const [name, label, hz] of [['ClawUpperOuter_L', 'RailHatch_UpperOuter_L', 0.16], ['ClawUpperInner_L', 'RailHatch_UpperInner_L', -0.16]]) {+ const h = hatch(name, label, 0.13, hz, 1, 0.145, PAINT);+ kit.ventOn(h, 0.12, 0.5, 0.2, 0.8, { n: 3 });+ kit.seam(h, h.at(0.56, 0.06), h.at(0.56, 0.94));+ }+ const hL = hatch('ClawThumb_L', 'RailHatch_Lower_L', -0.13, 0, -1, 0.30, GUN);+ kit.hazardPlate([hL.at(0.5, 0.08, 0.008), hL.at(0.95, 0.08, 0.008), hL.at(0.95, 0.92, 0.008), hL.at(0.5, 0.92, 0.008)], 0.02, { out: hL.n, sw: 0.05, noBack: true, c: 0.008 });+ kit.reseed(SEED_AFTER_WRIST);+ kit.mirror(1);+ return { G };+}++// ---------------------------------------------------------------- missile pod+function buildPod(kit, waistG) {+ const G = kit.group('MissilePod', waistG, [-0.05, 5.50, -1.42], 'MissilePod');+ G.node.userData = { hinge: 'z' };+ kit.use(G); kit.frame(null); kit.mirror(-1);+ const zc = 1.46, x0 = -0.62, x1 = 0.62, y0 = 5.54, y1 = 5.97, hz = 0.42;+ kit.hull([0, (y0 + y1) / 2, zc]); kit.mat('armour');+ const r = (x, ya, yb, h) => [[x, yb, zc + h], [x, yb, zc - h], [x, ya, zc - h], [x, ya, zc + h]];+ kit.loft([r(x0, y0 + 0.04, y1 - 0.06, hz - 0.06), r(x0 + 0.12, y0, y1, hz), r(x1 - 0.03, y0, y1, hz), r(x1, y0 + 0.02, y1 - 0.02, hz - 0.02)], { col: DARK });+ const top = kit.plate([[x0 + 0.12, y1 + 0.02, zc + hz], [x1 - 0.14, y1 + 0.02, zc + hz], [x1 - 0.14, y1 + 0.02, zc - hz], [x0 + 0.12, y1 + 0.02, zc - hz]], 0.03, { col: PAINT, out: [0, 1, 0] });+ kit.loop(top, 0.05); kit.bolts(top, 0.028, 2, 0.014);+ kit.hazardPlate([[x1 - 0.13, y1 + 0.02, zc + hz], [x1 - 0.13, y1 + 0.02, zc - hz], [x1 + 0.02, y1 + 0.005, zc - hz], [x1 + 0.02, y1 + 0.005, zc + hz]], 0.02, { sw: 0.07, out: [0, 1, 0] });+ for (const sg of [1, -1]) {+ const so = kit.plate([[x0 + 0.14, y1 - 0.03, zc + sg * (hz + 0.025)], [x1 - 0.06, y1 - 0.03, zc + sg * (hz + 0.025)], [x1 - 0.06, y0 + 0.03, zc + sg * (hz + 0.025)], [x0 + 0.14, y0 + 0.03, zc + sg * (hz + 0.025)]], 0.03, { col: sg > 0 ? PAINT : PAINT2, out: [0, 0, sg] });+ kit.loop(so, 0.045); kit.bolts(so, 0.026, 2, 0.014);+ if (sg > 0) {+ const s2 = kit.plate([[x0 + 0.26, y1 - 0.12, zc + hz + 0.05], [x0 + 0.72, y1 - 0.12, zc + hz + 0.05], [x0 + 0.72, y0 + 0.11, zc + hz + 0.05], [x0 + 0.26, y0 + 0.11, zc + hz + 0.05]], 0.025, { col: RUST, out: [0, 0, 1] });+ kit.bolts(s2, 0.018, 1, 0.01);+ kit.stencil([x0 + 0.84, y0 + 0.13, zc + hz + 0.025], [1, 0, 0], [0, 1, 0], 'HE', 0.14, BONE, [0, 0, 1]);+ }+ }+ kit.plate([[x0 + 0.1, y0 - 0.015, zc + hz], [x1 - 0.03, y0 - 0.015, zc + hz], [x1 - 0.03, y0 - 0.015, zc - hz], [x0 + 0.1, y0 - 0.015, zc - hz]], 0.02, { col: GUN, out: [0, -1, 0] });+ // Rear vent+ kit.vent([x0 - 0.008, y0 + 0.08, zc - 0.28], [x0 - 0.008, y0 + 0.08, zc + 0.28], [x0 - 0.008, y1 - 0.10, zc + 0.28], [x0 - 0.008, y1 - 0.10, zc - 0.28], { n: 5, col: GUN });+ // Launch tubes, two rows of four+ const front = x1 + 0.002;+ kit.poly([[front, y1 - 0.02, zc + hz - 0.02], [front, y1 - 0.02, zc - hz + 0.02], [front, y0 + 0.02, zc - hz + 0.02], [front, y0 + 0.02, zc + hz - 0.02]], GUN, { out: [1, 0, 0] });+ kit.mat('steel');+ for (let row = 0; row < 2; row++) for (let col = 0; col < 4; col++) {+ const cy = (y0 + y1) / 2 + (row - 0.5) * 0.205, cz = zc + (col - 1.5) * 0.195;+ kit.lathe([front, cy, cz], [1, 0, 0], [[0, 0.088, STEEL_D], [0.022, 0.088, vMix(STEEL_D, WEAR, 0.4)], [0.022, 0.072, tone(GUN, 0.4)], [-0.06, 0.072, tone(GUN, 0.4)]], { seg: 12 });+ kit.mat('rubber');+ kit.lathe([front - 0.05, cy, cz], [1, 0, 0], [[0, 0.064, BONE], [0.03, 0.062, BONE], [0.055, 0.044, RUST], [0.08, 0.018, RUST], [0.088, 0, RUST]], { seg: 10, mat: 'rubber' });+ kit.mat('steel');+ }+ // Trunnion+ kit.drum([-0.05, 5.50, 1.42], [0, 0, 1], 0.09, 0.22, { bolts: 0, seg: 14 });+ kit.mat('rubber');+ kit.tube([[-0.40, y0 - 0.01, zc + 0.10], [-0.32, 5.50, 1.42], [-0.15, 5.47, 1.38], [-0.05, 5.50, 1.36]], 0.018, { col: CAB_K });+ kit.mat('armour');+ kit.mirror(1);+}++// ---------------------------------------------------------------- build+async function build() {+ const root = createRoot('HeavyWalkerMech');+ const tex = makeTextures();+ const MAT = {+ armour: pbrMaterial({ albedo: tex.albedo, metallicRoughness: tex.mr, normal: tex.nrm, roughness: 1, metalness: 1, name: 'Armour_WeatheredPaint' }),+ // Gunmetal: colour comes from vertex colours, finish from the map. Metalness stays a little under 1 so+ // machinery keeps some diffuse read in scenes with no environment map; the polished band takes the sharp highlights.+ steel: pbrMaterial({ metallicRoughness: tex.steelMR, roughness: 1, metalness: 1, name: 'Gunmetal_Machinery' }),+ decal: pbrMaterial({ albedo: tex.wearA, metallicRoughness: tex.wearMR, roughness: 1, metalness: 1, alphaMode: 'blend', name: 'Wear_Overlay' }),+ chrome: gameMaterial(0xf0f2f4, { metalness: 0.72, roughness: 0.22, flatShading: false, name: 'Chrome_PistonRod' }),+ rubber: gameMaterial(0xffffff, { metalness: 0, roughness: 0.78, flatShading: false, name: 'Rubber_CableAndTrim' }),+ glass: glassMaterial(0x4f8598, { opacity: 0.32, roughness: 0.05, metalness: 0.2, name: 'Canopy_Glass' }),+ glowCyan: gameMaterial(0x06131a, { emissive: 0x2cc4f5, emissiveIntensity: 1.5, metalness: 0, roughness: 0.4, flatShading: false, name: 'Glow_Cyan' }),+ glowAmber: gameMaterial(0x1a0c02, { emissive: 0xff6408, emissiveIntensity: 1.15, metalness: 0, roughness: 0.4, flatShading: false, name: 'Glow_Amber' }),+ // Separate from Glow_Cyan so a game can drive rail charge-up without touching the cockpit lights.+ glowCoil: gameMaterial(0x06131a, { emissive: 0x2cc8ff, emissiveIntensity: 2.2, metalness: 0, roughness: 0.4, flatShading: false, name: 'Glow_RailCoil' }),+ };+ const kit = createKit(root, MAT, { armour: true, steel: true, rubber: true, decal: true }, { armour: true, steel: true, decal: true });++ const pelvisG = kit.group('Pelvis', null, [SK.hip[0], SK.hip[1], 0], 'Pelvis');+ buildPelvis(kit, pelvisG);+ buildLeg(kit, 'L', pelvisG);+ buildLeg(kit, 'R', pelvisG);+ const waistG = kit.group('Waist', pelvisG, [0, SK.waistY, 0], 'Torso');+ waistG.node.userData = { hinge: 'y (yaw); clip also rolls/pitches slightly' };+ buildTorso(kit, waistG);+ const ua = { L: buildUpperArm(kit, 'L', waistG), R: buildUpperArm(kit, 'R', waistG) };+ buildCannonArm(kit, ua.R.G, ua.R.A);+ buildRailCannonArm(kit, ua.L.G, ua.L.A);+ buildPod(kit, waistG);++ // Size each piston from its real span across the Walk cycle (with margin for code-driven poses).+ const N = rigNodes(root);+ const rest = rigList(N).map((n) => [n, n.position.clone(), n.quaternion.clone()]);+ const worldA = new THREE.Vector3(), worldB = new THREE.Vector3();+ const stats = kit.pistonList().map((p) => {+ const s = p.mir;+ const la = new THREE.Vector3(p.A[0] - p.gA.origin[0], p.A[1] - p.gA.origin[1], p.A[2] * s - p.gA.origin[2]);+ const lb = new THREE.Vector3(p.B[0] - p.gB.origin[0], p.B[1] - p.gB.origin[1], p.B[2] * s - p.gB.origin[2]);+ return { p, la, lb, min: Infinity, max: 0 };+ });+ for (let i = 0; i < 30; i++) {+ applyPose(N, walkPose(i / 30));+ root.updateMatrixWorld(true);+ for (const st of stats) {+ const d = st.p.gA.node.localToWorld(worldA.copy(st.la)).distanceTo(st.p.gB.node.localToWorld(worldB.copy(st.lb)));+ st.min = Math.min(st.min, d); st.max = Math.max(st.max, d);+ }+ }+ rest.forEach(([n, pos, q]) => { n.position.copy(pos); n.quaternion.copy(q); });+ root.updateMatrixWorld(true);+ for (const st of stats) kit.buildPiston(st.p, st.min - 0.04, st.max + 0.05);++ kit.flush();+ root.userData = { rig: { clip: 'Walk', walkSpeedMps: Number(WALK.speed.toFixed(4)), strideM: Number(WALK.stride.toFixed(4)), durationS: WALK.T, forward: '+X', hingeAxis: 'local Z unless the joint says otherwise' } };+ return root;+}++// ---------------------------------------------------------------- Walk clip+function animate(root) {+ const N = rigNodes(root);+ const aimers = [];+ root.traverse((n) => { if (n.userData && n.userData.aimAt) aimers.push(n); });+ const nodes = rigList(N);+ const rest = nodes.concat(aimers).map((n) => [n, n.position.clone(), n.quaternion.clone()]);+ const names = nodes.concat(aimers);+ const keyRot = new Map(names.map((n) => [n.name, []]));+ const keyPos = [];+ const eul = new THREE.Euler(), wp = new THREE.Vector3(), dir = new THREE.Vector3(), restDir = new THREE.Vector3();+ for (let i = 0; i <= WALK.keys; i++) {+ const phi = (i % WALK.keys) / WALK.keys, t = (WALK.T * i) / WALK.keys;+ applyPose(N, walkPose(phi));+ root.updateMatrixWorld(true);+ for (const n of aimers) {+ const target = root.getObjectByName(n.userData.aimAt);+ target.getWorldPosition(wp);+ n.parent.worldToLocal(wp);+ dir.copy(wp).sub(n.position).normalize();+ const r = n.userData.aimRestDir;+ restDir.set(r[0], r[1], r[2]);+ n.quaternion.setFromUnitVectors(restDir, dir);+ }+ for (const n of names) {+ eul.setFromQuaternion(n.quaternion, 'XYZ');+ keyRot.get(n.name).push({ time: t, rotation: [eul.x / D2R, eul.y / D2R, eul.z / D2R] });+ }+ keyPos.push({ time: t, position: [N.pelvis.position.x, N.pelvis.position.y, N.pelvis.position.z] });+ }+ rest.forEach(([n, pos, q]) => { n.position.copy(pos); n.quaternion.copy(q); });+ root.updateMatrixWorld(true);+ const tracks = [positionTrack(N.pelvis.name, keyPos)];+ for (const n of names) tracks.push(rotationTrack(n.name, keyRot.get(n.name)));+ return [createClip('Walk', WALK.T, tracks, { loop: true })];+}diff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-after-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-after-1000.avifnew file mode 100644index 00000000..873b0332Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-after-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-after-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-after-1000.webpnew file mode 100644index 00000000..d3a0e307Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-after-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-after-480.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-after-480.avifnew file mode 100644index 00000000..eaab1357Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-after-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-after-480.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-after-480.webpnew file mode 100644index 00000000..00da9523Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-after-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-after-768.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-after-768.avifnew file mode 100644index 00000000..b5b0821bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-after-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-after-768.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-after-768.webpnew file mode 100644index 00000000..2748d9efBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-after-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-after.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-after.webpnew file mode 100644index 00000000..41deb647Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-after.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-before-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-before-1000.avifnew file mode 100644index 00000000..d894ca4dBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-before-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-before-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-before-1000.webpnew file mode 100644index 00000000..2504f7aeBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-before-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-before-480.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-before-480.avifnew file mode 100644index 00000000..aa99e3fcBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-before-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-before-480.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-before-480.webpnew file mode 100644index 00000000..1a1badf7Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-before-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-before-768.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-before-768.avifnew file mode 100644index 00000000..61dc9369Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-before-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-before-768.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-before-768.webpnew file mode 100644index 00000000..000843f7Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-before-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-before.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-before.webpnew file mode 100644index 00000000..c14ba4c1Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-before.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-1024.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1024.avifnew file mode 100644index 00000000..5672883cBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1024.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-1024.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1024.webpnew file mode 100644index 00000000..af553bc2Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1024.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-1440.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1440.avifnew file mode 100644index 00000000..690ff331Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1440.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-1440.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1440.webpnew file mode 100644index 00000000..e11957fcBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-1440.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-480.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-final-480.avifnew file mode 100644index 00000000..42b4f834Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-480.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-final-480.webpnew file mode 100644index 00000000..b14bcfe9Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-768.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-final-768.avifnew file mode 100644index 00000000..7923ea70Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final-768.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-final-768.webpnew file mode 100644index 00000000..689af8a0Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-final.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-final.webpnew file mode 100644index 00000000..39feff50Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-final.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.avifnew file mode 100644index 00000000..11115e67Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.webpnew file mode 100644index 00000000..67b77f7dBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.avifnew file mode 100644index 00000000..e5a7f72fBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.webpnew file mode 100644index 00000000..1689dbffBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.avifnew file mode 100644index 00000000..f9e17b7eBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.webpnew file mode 100644index 00000000..f4c01eb4Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-768.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-768.avifnew file mode 100644index 00000000..fe616e25Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-768.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-768.webpnew file mode 100644index 00000000..f48e36eeBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet.webpnew file mode 100644index 00000000..a450b877Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-make-sheet.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-1000.avifnew file mode 100644index 00000000..6d7e5aedBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-1000.webpnew file mode 100644index 00000000..741965d3Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-480.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-480.avifnew file mode 100644index 00000000..e81f9dc4Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-480.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-480.webpnew file mode 100644index 00000000..bf649bcfBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-768.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-768.avifnew file mode 100644index 00000000..d27d9099Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-768.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-768.webpnew file mode 100644index 00000000..bffd5ef0Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after.webpnew file mode 100644index 00000000..0298b1b8Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-after.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-1000.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-1000.avifnew file mode 100644index 00000000..260747e0Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-1000.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-1000.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-1000.webpnew file mode 100644index 00000000..aec12d76Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-1000.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-480.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-480.avifnew file mode 100644index 00000000..983273e9Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-480.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-480.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-480.webpnew file mode 100644index 00000000..f531bc6cBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-480.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-768.avif b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-768.avifnew file mode 100644index 00000000..518f5e9bBinary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-768.avif differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-768.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-768.webpnew file mode 100644index 00000000..83c32438Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before-768.webp differdiff --git a/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before.webp b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before.webpnew file mode 100644index 00000000..e885a8e9Binary files /dev/null and b/site/public/text-to-3d/5833329625b2f4ed/mech-paint-before.webp differdiff --git a/site/scripts/build-site-media.mjs b/site/scripts/build-site-media.mjsindex 2e60b175..45b09d66 100644--- a/site/scripts/build-site-media.mjs+++ b/site/scripts/build-site-media.mjs@@ -94,7 +94,8 @@ async function buildMedia() {const hero = { poster: shots.packs[curation.featuredWorlds[0]][1] };const archiveIndex = join(publicDir, 'assets/index.json');const archive = existsSync(archiveIndex) ? await json(archiveIndex) : [];- const cards = socialCards({ packsEnabled, release, farm, vehicles, bridge, hero, foundryFloor, archive });+ const textTo3d = await json(join(site, 'src/data/text-to-3d-card.json'));+ const cards = socialCards({ packsEnabled, release, farm, vehicles, bridge, hero, foundryFloor, archive, textTo3d });const archivoData = (await readFile(join(publicDir, 'fonts/archivo-latin-full-normal.woff2'))).toString('base64');const plexData = (await readFile(join(publicDir, 'fonts/ibm-plex-mono-latin-400-normal.woff2'))).toString('base64');let prior = {};@@ -121,7 +122,8 @@ async function buildMedia() {records[card.slug] = digest;const output = join(publicDir, 'social', `${card.slug}${SOCIAL_CARD_EXTENSION}`);if (prior[card.slug] === digest && existsSync(output)) continue;- if (!browser) browser = await puppeteer.launch({ executablePath: chromeExecutable(), headless: true, pipe: true, args: ['--no-sandbox', '--disable-dev-shm-usage'] });+ // A fresh window is smaller than a card, and a capture can catch the page before the viewport below applies, which wraps the picture around.+ if (!browser) browser = await puppeteer.launch({ executablePath: chromeExecutable(), headless: true, pipe: true, args: ['--no-sandbox', '--disable-dev-shm-usage', '--window-size=1200,800'] });const page = await browser.newPage();try {await page.setViewport({ width: 1200, height: 630, deviceScaleFactor: 1 });diff --git a/site/scripts/build-site-media.test.ts b/site/scripts/build-site-media.test.tsindex d8615173..2c249172 100644--- a/site/scripts/build-site-media.test.ts+++ b/site/scripts/build-site-media.test.ts@@ -1,4 +1,6 @@import { expect, test } from 'bun:test';+import { readFile } from 'node:fs/promises';+import { SOCIAL_CARD } from '../src/lib/social-cards.mjs';import { SOCIAL_CARD_EXTENSION, pngIco, staleSocialCards } from './build-site-media.mjs';// Share cards are JPEG: every network's crawler reads JPEG and PNG, and the previous site's fixture required a PNG after two@@ -7,6 +9,14 @@ test('share cards are JPEG files', () => {expect(SOCIAL_CARD_EXTENSION).toBe('.jpg');});+// Chrome 150 sizes a fresh window 780 x 580, and about one first capture in twelve came back with the card wrapped around at that size.+test('the browser that captures a card has a window at least the size of the card', async () => {+ const source = await readFile(new URL('./build-site-media.mjs', import.meta.url), 'utf8');+ const [, width, height] = source.match(/--window-size=(\d+),(\d+)/) ?? [];+ expect(Number(width)).toBeGreaterThanOrEqual(SOCIAL_CARD.width);+ expect(Number(height)).toBeGreaterThanOrEqual(SOCIAL_CARD.height);+});+test('social cards no page uses are the ones left over from an earlier build', () => {const cards = [{ slug: 'home' }, { slug: 'foundry-floor' }, { slug: 'archive-windmill' }];const onDisk = ['archive-windmill.jpg', 'foundry-floor.jpg', 'home.jpg', 'old-name.jpg', 'zeta.jpg'];diff --git a/site/scripts/finalize-site.mjs b/site/scripts/finalize-site.mjsindex ffd204d3..de7e9a83 100644--- a/site/scripts/finalize-site.mjs+++ b/site/scripts/finalize-site.mjs@@ -31,6 +31,7 @@ export function buildIndexFiles(pages, { lastmod } = {}) {const home = published.find((page) => page.route === '/');const docs = published.filter((page) => page.route.startsWith('/docs/'));const catalog = published.filter((page) => /^\/(?:packs|gallery|scenes)\/(?:[^/]+\/)?$/.test(page.route) && page.route !== ARCHIVE_INDEX_ROUTE);+ const guides = published.filter((page) => page.route === '/text-to-3d/');const archive = published.find((page) => page.route === ARCHIVE_INDEX_ROUTE);const sitemap = `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${published.map((page) => {const date = lastmod?.(page.route);@@ -44,6 +45,7 @@ export function buildIndexFiles(pages, { lastmod } = {}) {'# Kiln', '',home?.description ?? 'Build and revise 3D assets with your coding agent.', '','## Documentation', '', ...docs.map(link), '',+ ...(guides.length ? ['## Guides', '', ...guides.map(link), ''] : []),'## Assets and scenes', '', ...catalog.map(link), '',...(archive ? ['## Archive', '',diff --git a/site/scripts/gallery-asset-page.test.ts b/site/scripts/gallery-asset-page.test.tsnew file mode 100644index 00000000..dbdb7cb7--- /dev/null+++ b/site/scripts/gallery-asset-page.test.ts@@ -0,0 +1,30 @@+import { expect, test } from 'bun:test';+import { readFile } from 'node:fs/promises';+import bridge from '../src/data/standalone/golden-gate-bridge.json';++const page = await readFile(new URL('../src/pages/gallery/[slug].astro', import.meta.url), 'utf8');+const at = (text: string) => {+ const index = page.indexOf(text);+ expect(index).toBeGreaterThan(-1);+ return index;+};++test('an asset page offers its downloads right after the model and its figures', () => {+ const downloads = at('id="downloads"');+ for (const before of ['<AssetViewer', '<StatRow']) expect(at(before)).toBeLessThan(downloads);+ for (const after of ['<VehicleTiers', 'id="review"', 'id="source"', 'id="history"', 'id="reference"']) expect(at(after)).toBeGreaterThan(downloads);+});++test('the engine note follows the downloads it describes', () => {+ expect(at('<EngineNote')).toBeGreaterThan(at('id="downloads"'));+});++test('an asset page keeps its revision history closed until asked for, under the same heading and anchor', () => {+ expect(page).toMatch(/<details[^>]*>\s*<summary[^>]*>\s*<h2 id="history"[^>]*>Follow the saved revisions\.<\/h2>\s*<\/summary>/);+});++test('Golden Gate offers its full detail first, with the other levels of the same bridge in one closed row', () => {+ expect(bridge.tiers.map(({ tier }) => tier)).toEqual(['full', 'web', 'far']);+ expect(page).toMatch(/<AssetTierDownloads \{\.\.\.fullTier!\} \/>\s*<details[^>]*>\s*<summary[^>]*>Other detail levels<\/summary>/);+ expect(page).toMatch(/otherTiers\.map\(\(tier\) => <AssetTierDownloads \{\.\.\.tier\} \/>\)/);+});diff --git a/site/scripts/render-text-to-3d.mjs b/site/scripts/render-text-to-3d.mjsnew file mode 100644index 00000000..c01104ab--- /dev/null+++ b/site/scripts/render-text-to-3d.mjs@@ -0,0 +1,61 @@+import { mkdir, readFile, readdir, writeFile } from 'node:fs/promises';+import { join, resolve } from 'node:path';+import { inspectGlb } from './generate-commons.mjs';+import { connectRig, fitCamera, orbitDirection, sha256Of } from './rig-render.mjs';+import { mergeGlbs } from './stage-text-to-3d.mjs';++/**+ * The stills for /text-to-3d/: the gallery's review rig applied to the exact saved GLBs, with nothing composited or retouched.+ * One still may set a second saved GLB in the scene at its true size: `with` names its workspace, its saved revision by+ * position, a name for the placing node, and `translation`. Nothing else about either model changes.+ *+ * bun scripts/render-text-to-3d.mjs <workspace> --service http://127.0.0.1:8017 --engine <installed package>+ *+ * The render service and `--engine` are the published package. <workspace>/run/stills.json lists the views: name, step (a saved revision by position, 1-based), size, then+ * an orbit `[azimuth, elevation]`, a `direction` or an exact perspective `camera`, padding, alt and caption.+ */+const args = process.argv.slice(2);+const option = (name) => (args.includes(name) ? args[args.indexOf(name) + 1] : undefined);+const workspace = args[0] && resolve(args[0]);+const service = option('--service');+const engineDir = option('--engine') && resolve(option('--engine'));+if (!workspace || !service || !engineDir) throw Error('Usage: bun scripts/render-text-to-3d.mjs <workspace> --service URL --engine PACKAGE_DIR');++const only = option('--only')?.split(',');+async function savedRevisions(directory) {+ const assets = join(directory, 'assets/kiln');+ const [assetId] = await readdir(assets);+ const manifests = await Promise.all((await readdir(join(assets, assetId, 'revisions'))).map(async (id) => JSON.parse(await readFile(join(assets, assetId, 'revisions', id, 'manifest.json'), 'utf8'))));+ const revisions = manifests.sort((a, b) => a.createdAt.localeCompare(b.createdAt));+ const glbOf = (revision) => readFile(join(assets, assetId, 'revisions', revision.revisionId, 'asset.glb'));+ return { assetId, revisions, glbOf };+}+const { assetId, revisions, glbOf } = await savedRevisions(workspace);+const stills = JSON.parse(await readFile(join(workspace, 'run/stills.json'), 'utf8'));++// An installed package is not a git tree: its identity is its version.+const rig = await connectRig({ url: service, engineDir, provenance: () => null });+const out = join(workspace, 'renders');+await mkdir(out, { recursive: true });+const manifestPath = join(out, 'manifest.json');+const previous = only ? JSON.parse(await readFile(manifestPath, 'utf8').catch(() => '{"images":[]}')).images : [];+const images = previous.filter((image) => !stills.some((still) => still.name === image.name && only?.includes(still.name)));+for (const still of stills.filter((candidate) => !only || only.includes(candidate.name))) {+ const revision = revisions[still.step - 1];+ if (!revision) throw Error(`${still.name}: no saved revision ${still.step}`);+ const saved = await glbOf(revision);+ const other = still.with && await savedRevisions(resolve(workspace, still.with.workspace));+ const placed = other && other.revisions[still.with.step - 1];+ if (other && !placed) throw Error(`${still.name}: no saved revision ${still.with.step} in ${still.with.workspace}`);+ const glb = placed ? mergeGlbs(saved, await other.glbOf(placed), { name: still.with.name, translation: still.with.translation }) : saved;+ const facts = inspectGlb(saved);+ const camera = still.camera+ ? rig.engine.camera.validateResolvedAssetCamera({ version: 'kiln.camera.v1', projection: 'perspective', up: [0, 1, 0], aspect: still.width / still.height, ...still.camera })+ : fitCamera(rig.engine, { min: facts.boundsMin, max: facts.boundsMax }, still.direction ?? orbitDirection(still.orbit[0], still.orbit[1]), { aspect: still.width / still.height, padding: still.padding ?? 1.02 });+ const { pngs, receipt } = await rig.capture({ glb, cameras: [camera], width: still.width, height: still.height });+ const file = `${still.name}.png`;+ await writeFile(join(out, file), pngs[0]);+ images.push({ name: still.name, file, alt: still.alt, caption: still.caption, revisionId: revision.revisionId, ...(placed ? { with: { revisionId: placed.revisionId, translation: still.with.translation } } : {}), origin: 'review-rig', receipt: { ...receipt, sha256: sha256Of(pngs[0]) } });+ console.log(`${file}: ${still.width}x${still.height}, ${pngs[0].length} bytes, revision ${revision.revisionId}`);+}+await writeFile(manifestPath, `${JSON.stringify({ assetId, renderer: rig.service.rendererId, images }, null, 2)}\n`);diff --git a/site/scripts/rig-render.mjs b/site/scripts/rig-render.mjsindex 42ad27b8..ed33c0e5 100644--- a/site/scripts/rig-render.mjs+++ b/site/scripts/rig-render.mjs@@ -30,11 +30,11 @@ export function engineProvenance(engineDir) {return { commit: git('rev-parse', 'HEAD'), subject: git('log', '-1', '--format=%s'), dirty: git('status', '--porcelain', '--untracked-files=no') !== '' };}-export async function loadRigEngine(engineDir = resolveRigEngine()) {+export async function loadRigEngine(engineDir = resolveRigEngine(), { provenance = engineProvenance } = {}) {if (!existsSync(join(engineDir, 'src/render-service-client.ts'))) throw new Error('No compatible Kiln engine tree. Set KILN_RIG_ENGINE_DIR to a tree containing the review rig.');const load = (file) => import(pathToFileURL(join(engineDir, file)).href);const [client, mode, camera] = await Promise.all([load('src/render-service-client.ts'), load('src/cli-render-mode.ts'), load('src/views/camera.ts')]);- return { engineDir, client, mode, camera, provenance: engineProvenance(engineDir) };+ return { engineDir, client, mode, camera, provenance: provenance(engineDir) };}/**@@ -43,8 +43,8 @@ export async function loadRigEngine(engineDir = resolveRigEngine()) {* renderer attested full-material fidelity for exactly the GLB sent, echoed every camera unchanged, and did* not change identity while it worked.*/-export async function connectRig({ url = process.env.KILN_RENDER_SERVICE_URL ?? 'http://127.0.0.1:8000', token = process.env.RENDER_SERVICE_TOKEN, engineDir } = {}) {- const engine = await loadRigEngine(engineDir);+export async function connectRig({ url = process.env.KILN_RENDER_SERVICE_URL ?? 'http://127.0.0.1:8000', token = process.env.RENDER_SERVICE_TOKEN, engineDir, provenance } = {}) {+ const engine = await loadRigEngine(engineDir, { provenance });const sourceFingerprint = engine.client.packagedRendererSourceFingerprint();const identity = await engine.mode.probeCaptureIdentity(url, token, sourceFingerprint);if (!identity) throw new Error(`The render service at ${url} did not provide an exact build identity for this engine (start it from ${engine.engineDir}).`);diff --git a/site/scripts/stage-text-to-3d.mjs b/site/scripts/stage-text-to-3d.mjsnew file mode 100644index 00000000..5536d30f--- /dev/null+++ b/site/scripts/stage-text-to-3d.mjs@@ -0,0 +1,381 @@+import { spawnSync } from 'node:child_process';+import { createHash } from 'node:crypto';+import { existsSync } from 'node:fs';+import { mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises';+import { dirname, join, resolve } from 'node:path';+import { fileURLToPath } from 'node:url';+import sharp from 'sharp';+import { Box3, Matrix4, Quaternion, Vector3 } from 'three';+import { inspectGlb } from './generate-commons.mjs';+import { ASSET_BASE } from './mirror-core.mjs';++/**+ * Stage the saved revisions behind /text-to-3d/. The record this writes holds what that page shows and nothing else, so what+ * the page says about the models and can't show is checked here instead, against the saved GLBs and programs. Nothing here+ * renders, edits or invents an output.+ *+ * node scripts/stage-text-to-3d.mjs <directory>+ *+ * <directory>/tools.json names the tools. <directory>/<subject>/ is a workspace holding `assets/`, `run/` with the prompts and+ * `steps.json`, which lists each step's prompt and saved revision, and `renders/manifest.json` from+ * scripts/render-text-to-3d.mjs.+ *+ * Images and programs are committed under public/text-to-3d/. The two GLBs go to the asset host, the way Golden Gate's do:+ * each is named by its own hash, pinned in the mirror manifest and copied to .localdata/asset-delivery with an upload list+ * for scripts/publish-assets.mjs. A new revision of a model gets a new path, and staging again is all it takes.+ */+const site = resolve(dirname(fileURLToPath(import.meta.url)), '..');+const SUBJECTS = ['mech', 'cathedral'];+const WIDTHS = [480, 768, 1024, 1440];+const hash = (bytes) => createHash('sha256').update(bytes).digest('hex');+const json = async (file) => JSON.parse(await readFile(file, 'utf8'));+export const lineCount = (text) => text.split('\n').length - (text.endsWith('\n') || !text ? 1 : 0);++/** A GLB's JSON chunk, and a reader for the float accessors its animation samplers point at. */+export function openGlb(bytes) {+ if (bytes.readUInt32LE(0) !== 0x46546c67) throw Error('Not a GLB');+ const jsonLength = bytes.readUInt32LE(12);+ const gltf = JSON.parse(bytes.subarray(20, 20 + jsonLength).toString('utf8'));+ const bin = bytes.subarray(20 + jsonLength + 8);+ const floats = (index) => {+ const accessor = gltf.accessors[index];+ if (accessor.componentType !== 5126) throw Error(`Accessor ${index} is not float data`);+ const start = (gltf.bufferViews[accessor.bufferView].byteOffset ?? 0) + (accessor.byteOffset ?? 0);+ const length = accessor.count * { SCALAR: 1, VEC3: 3, VEC4: 4 }[accessor.type];+ return new Float32Array(bin.buffer.slice(bin.byteOffset + start, bin.byteOffset + start + length * 4));+ };+ return { gltf, floats };+}++/**+ * Two saved GLBs as one scene, for a picture of both at their true sizes. Every mesh, material and texture stays as saved:+ * the only new thing is one node that places the second model. Its clips are dropped, because the picture is a still.+ */+export function mergeGlbs(first, second, { name, translation = [0, 0, 0], rotation = [0, 0, 0, 1] }) {+ const a = openGlb(first).gltf;+ const b = openGlb(second).gltf;+ const binary = (bytes) => bytes.subarray(20 + bytes.readUInt32LE(12) + 8);+ const start = Math.ceil(binary(first).length / 4) * 4;+ const count = (key) => a[key]?.length ?? 0;+ const list = (key, shift) => [...(a[key] ?? []), ...(b[key] ?? []).map(shift)];+ const shifted = (value, by) => (value === undefined ? {} : { [by[0]]: value + by[1] });+ const offsetTextures = (value) => {+ for (const [key, inner] of Object.entries(value)) {+ if (!inner || typeof inner !== 'object') continue;+ if (key.endsWith('Texture')) inner.index += count('textures');+ offsetTextures(inner);+ }+ return value;+ };+ const scene = a.scenes[a.scene ?? 0].nodes;+ const nodes = list('nodes', (node) => ({ ...node, ...shifted(node.mesh, ['mesh', count('meshes')]), ...(node.children ? { children: node.children.map((child) => child + count('nodes')) } : {}) }));+ nodes.push({ name, translation, rotation, children: b.scenes[b.scene ?? 0].nodes.map((index) => index + count('nodes')) });+ const gltf = {+ ...a,+ accessors: list('accessors', (accessor) => ({ ...accessor, bufferView: accessor.bufferView + count('bufferViews') })),+ bufferViews: list('bufferViews', (view) => ({ ...view, byteOffset: (view.byteOffset ?? 0) + start })),+ samplers: list('samplers', (sampler) => sampler),+ images: list('images', (image) => ({ ...image, ...shifted(image.bufferView, ['bufferView', count('bufferViews')]) })),+ textures: list('textures', (texture) => ({ ...texture, ...shifted(texture.source, ['source', count('images')]), ...shifted(texture.sampler, ['sampler', count('samplers')]) })),+ materials: list('materials', (material) => offsetTextures(structuredClone(material))),+ meshes: list('meshes', (mesh) => ({+ ...mesh,+ primitives: mesh.primitives.map((primitive) => ({+ ...primitive,+ attributes: Object.fromEntries(Object.entries(primitive.attributes).map(([key, accessor]) => [key, accessor + count('accessors')])),+ ...shifted(primitive.indices, ['indices', count('accessors')]),+ ...shifted(primitive.material, ['material', count('materials')]),+ })),+ })),+ nodes,+ scenes: [{ nodes: [...scene, nodes.length - 1] }],+ scene: 0,+ buffers: [{ byteLength: start + binary(second).length }],+ extensionsUsed: [...new Set([...(a.extensionsUsed ?? []), ...(b.extensionsUsed ?? [])])],+ };+ const chunk = (bytes, type, fill) => {+ const padded = Buffer.alloc(Math.ceil(bytes.length / 4) * 4, fill);+ bytes.copy(padded);+ const head = Buffer.alloc(8);+ head.writeUInt32LE(padded.length, 0);+ head.write(type, 4);+ return Buffer.concat([head, padded]);+ };+ const bin = Buffer.alloc(start + binary(second).length);+ binary(first).copy(bin);+ binary(second).copy(bin, start);+ const chunks = [chunk(Buffer.from(JSON.stringify(gltf)), 'JSON', 0x20), chunk(bin, 'BIN\0', 0)];+ const header = Buffer.alloc(12);+ header.write('glTF', 0);+ header.writeUInt32LE(2, 4);+ header.writeUInt32LE(12 + chunks[0].length + chunks[1].length, 8);+ return Buffer.concat([header, ...chunks]);+}++/** Each clip in a GLB by name, with a digest of its targets and keyframes. */+export function clipDigests(bytes) {+ const { gltf, floats } = openGlb(bytes);+ const digest = (clip) => {+ const sum = createHash('sha256');+ for (const channel of clip.channels) {+ const sampler = clip.samplers[channel.sampler];+ sum.update(`${gltf.nodes[channel.target.node].name}/${channel.target.path}/${sampler.interpolation}`);+ sum.update(new Uint8Array(floats(sampler.input).buffer));+ sum.update(new Uint8Array(floats(sampler.output).buffer));+ }+ return sum.digest('hex');+ };+ return new Map((gltf.animations ?? []).map((clip) => [clip.name, digest(clip)]));+}++/** Every mesh node in a GLB by name, with where it sits in the world and a digest of its vertex positions alone: baked colours may share the buffer. */+function placedMeshes(bytes) {+ const { gltf } = openGlb(bytes);+ const bin = bytes.subarray(20 + bytes.readUInt32LE(12) + 8);+ const meshes = new Map();+ const visit = (index, parent) => {+ const node = gltf.nodes[index];+ const local = node.matrix+ ? new Matrix4().fromArray(node.matrix)+ : new Matrix4().compose(new Vector3(...(node.translation ?? [0, 0, 0])), new Quaternion(...(node.rotation ?? [0, 0, 0, 1])), new Vector3(...(node.scale ?? [1, 1, 1])));+ const world = parent.clone().multiply(local);+ if (node.mesh !== undefined) {+ const sum = createHash('sha256');+ const bounds = new Box3();+ const materials = [];+ for (const primitive of gltf.meshes[node.mesh].primitives) {+ const position = gltf.accessors[primitive.attributes.POSITION];+ const view = gltf.bufferViews[position.bufferView];+ const stride = view.byteStride ?? 12;+ const start = (view.byteOffset ?? 0) + (position.byteOffset ?? 0);+ for (let vertex = 0; vertex < position.count; vertex++) sum.update(bin.subarray(start + vertex * stride, start + vertex * stride + 12));+ for (let corner = 0; corner < 8; corner++) bounds.expandByPoint(new Vector3(...[0, 1, 2].map((axis) => ((corner >> axis) & 1 ? position.max[axis] : position.min[axis]))).applyMatrix4(world));+ materials.push(gltf.materials?.[primitive.material]?.name);+ }+ const seen = [...meshes.keys()].filter((key) => key === node.name || key.startsWith(`${node.name}#`)).length;+ meshes.set(seen ? `${node.name}#${seen + 1}` : node.name, { name: node.name, at: world.elements.slice(12, 15), digest: sum.digest('hex'), bounds, materials });+ }+ for (const child of node.children ?? []) visit(child, world);+ };+ for (const index of gltf.scenes[gltf.scene ?? 0].nodes) visit(index, new Matrix4());+ return meshes;+}++/**+ * What a revision did to its parent, from the two saved GLBs alone. Kiln's own comparison refuses a model past 2,000,000+ * placed vertices.+ */+export function comparePlacement(before, after) {+ const a = placedMeshes(before);+ const b = placedMeshes(after);+ const round = (value) => Number(value.toFixed(3)) + 0;+ let unmoved = 0, moved = 0, reshaped = 0;+ const offsets = new Map();+ for (const [key, mesh] of a) {+ const other = b.get(key);+ if (!other) continue;+ if (mesh.digest !== other.digest) { reshaped += 1; continue; }+ const by = other.at.map((value, axis) => round(value - mesh.at[axis]));+ if (by.every((value) => value === 0)) { unmoved += 1; continue; }+ moved += 1;+ offsets.set(by.join(), (offsets.get(by.join()) ?? 0) + 1);+ }+ const [offset, count] = [...offsets].sort((x, y) => y[1] - x[1])[0] ?? [null, 0];+ // Names can't say what stayed on their own: a revision that renumbers the statues renames stone that never moved.+ const centre = ({ bounds }) => bounds.getCenter(new Vector3());+ const byShape = new Map();+ for (const mesh of b.values()) byShape.set(mesh.digest, [...(byShape.get(mesh.digest) ?? []), centre(mesh)]);+ const shift = new Vector3(...(offset ? offset.split(',').map(Number) : [0, 0, 0]));+ const standsAt = (point, places) => places.some((place) => [0, 1, 2].every((axis) => Math.abs(place.getComponent(axis) - point.getComponent(axis)) < 0.05));+ const geometry = { parent: a.size, inPlace: 0, moved: 0, other: [] };+ for (const mesh of a.values()) {+ const places = byShape.get(mesh.digest) ?? [];+ if (standsAt(centre(mesh), places)) geometry.inPlace += 1;+ else if (standsAt(centre(mesh).add(shift), places)) geometry.moved += 1;+ else geometry.other.push(mesh.name);+ }+ const extent = (meshes) => {+ const box = new Box3();+ for (const { name, bounds } of meshes.values()) if (!/ground/i.test(name)) box.union(bounds);+ return { min: box.min.toArray().map(round), max: box.max.toArray().map(round) };+ };+ const materials = (meshes) => [...new Set([...meshes.values()].flatMap((mesh) => mesh.materials))].sort().join();+ return {+ unmoved,+ moved,+ movedTogether: offset ? { by: offset.split(',').map(Number), count } : null,+ geometry,+ reshaped,+ gone: [...a.keys()].filter((key) => !b.has(key)).length,+ added: [...b.keys()].filter((key) => !a.has(key)).length,+ extent: { before: extent(a), after: extent(b) },+ materialsSame: materials(a) === materials(b),+ };+}++async function unifiedDiff(from, to, labelFrom, labelTo) {+ const directory = join(site, '.cache/text-to-3d');+ await mkdir(directory, { recursive: true });+ await writeFile(join(directory, 'a.js'), from);+ await writeFile(join(directory, 'b.js'), to);+ const result = spawnSync('diff', ['-u', '--label', labelFrom, '--label', labelTo, join(directory, 'a.js'), join(directory, 'b.js')], { encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 });+ if (result.status !== 1) throw Error(result.stderr || 'Expected the revision sources to differ');+ return result.stdout;+}++/**+ * What the page says about a model and can't show, against its saved revisions. It fails rather than writes a record the page+ * would then be wrong about.+ */+function checkClaims(subject, { plan, glbs, sources, placementOf, renders }) {+ const claim = (holds, what) => { if (!holds) throw Error(`${subject}: ${what}`); };+ const at = (id) => plan.steps.findIndex((step) => step.id === id);+ const nodes = (glb) => openGlb(glb).gltf.nodes.map(({ name }) => name);+ for (const { name, revisionId } of renders) if (['hero', 'final', 'nave'].includes(name)) claim(revisionId === plan.steps.at(-1).revision, `the ${name} picture isn't of the final revision`);+ if (subject === 'mech') {+ const walk = glbs.map((glb) => clipDigests(glb).get('Walk'));+ claim(walk[0] && new Set(walk).size === 1, 'the Walk clip changed between revisions');+ const pivots = (glb) => nodes(glb).filter((name) => /hip|knee|ankle|toe|waist|shoulder|elbow/i.test(name));+ for (const joint of [/hip/i, /knee/i, /ankle/i, /toe/i, /waist/i, /shoulder/i, /elbow/i]) claim(pivots(glbs.at(-1)).some((name) => joint.test(name)), `the mech has no ${joint.source} joint`);+ const kept = new Set(pivots(glbs.at(-1)));+ for (const name of pivots(glbs.at(-2))) if (!/claw/i.test(name)) claim(kept.has(name), `the last revision lost ${name}`);+ }+ if (subject === 'cathedral') {+ const revise = at('revise');+ claim(/naveBays: 9\b/.test(sources[revise - 1]) && /naveBays: 11\b/.test(sources[revise]), 'the revision is not two more bays');+ const { movedTogether, extent, materialsSame } = placementOf('revise');+ claim(movedTogether.by.join() === '-13.2,0,0', 'the moved meshes did not move east together'); // The program's +X is west.+ claim(materialsSame, 'the revision changed the materials');+ claim(Math.abs(extent.after.max[1] - 100) < 0.5, 'the spire is not 100 m');+ for (const id of ['light', 'sun', 'daylight']) {+ const { geometry } = placementOf(id);+ claim(geometry.moved === 0 && geometry.other.every((name) => /StainedLight/.test(name)), `the ${id} revision changed more than the light`);+ }+ const { min, max } = placementOf('daylight').extent.after;+ claim(Math.round(max[0] - min[0]) === 144, 'the building is not 144 m long');+ claim(/sun: \[0\.11, 0\.79, -0\.61\]/.test(sources.at(-1)), 'the final program bakes another sun');+ claim(!nodes(glbs[at('direct')]).includes('Ground') && nodes(glbs[at('direct-2')]).includes('Ground') && nodes(glbs.at(-1)).includes('Ground'), 'the ground is not its own part');+ }+}++/** Read one subject's plan and return its record plus the files to publish under its name. */+async function stageSubject(run, subject) {+ const directory = join(run, subject);+ const assets = join(directory, 'assets/kiln');+ const [assetId] = await readdir(assets);+ const plan = await json(join(directory, 'run/steps.json'));+ const steps = [], sources = [], glbs = [];+ const placements = new Map();+ const placementOf = (id) => {+ if (!placements.has(id)) {+ const index = plan.steps.findIndex((step) => step.id === id);+ placements.set(id, comparePlacement(glbs[index - 1], glbs[index]));+ }+ return placements.get(id);+ };+ for (const step of plan.steps) {+ const revision = join(assets, assetId, 'revisions', step.revision);+ const manifest = await json(join(revision, 'manifest.json'));+ const glb = await readFile(join(revision, 'asset.glb'));+ const source = await readFile(join(revision, 'source.kiln.js'));+ if (`sha256:${hash(glb)}` !== manifest.files['asset.glb'].sha256 || `sha256:${hash(source)}` !== manifest.files['source.kiln.js'].sha256) throw Error(`${subject}/${step.id}: saved files differ from their manifest`);+ const parent = plan.steps[steps.length - 1];+ if (parent && manifest.parentRevision !== parent.revision) throw Error(`${subject}/${step.id}: its parent is ${manifest.parentRevision}, not the previous step's ${parent.revision}`);+ sources.push(source.toString('utf8'));+ glbs.push(glb);+ steps.push({+ id: step.id,+ prompt: (await readFile(join(directory, 'run', step.prompt), 'utf8')).trim(),+ sourceFile: `${subject}-${steps.length + 1}.kiln.js`,+ sourceLines: lineCount(sources.at(-1)),+ });+ if (step.geometry) {+ const { parent: count, inPlace, moved } = placementOf(step.id).geometry;+ steps.at(-1).geometry = { parent: count, inPlace, moved };+ }+ }+ const files = steps.map(({ sourceFile }, index) => ({ name: sourceFile, bytes: Buffer.from(sources[index]) }));+ const { images: renders } = await json(join(directory, 'renders/manifest.json'));+ checkClaims(subject, { plan, glbs, sources, placementOf, renders });+ const images = {};+ for (const render of renders) {+ const png = await readFile(join(directory, 'renders', render.file));+ const meta = await sharp(png).metadata();+ const name = `${subject}-${render.name}`;+ const variants = { webp: [], avif: [] };+ for (const width of WIDTHS.filter((value) => value < meta.width).concat(meta.width <= 1440 ? [meta.width] : [1440])) {+ const resized = sharp(png).resize({ width });+ files.push({ name: `${name}-${width}.webp`, bytes: await resized.clone().webp({ quality: 86 }).toBuffer() });+ files.push({ name: `${name}-${width}.avif`, bytes: await resized.clone().avif({ quality: 58 }).toBuffer() });+ variants.webp.push([`${name}-${width}.webp`, width]);+ variants.avif.push([`${name}-${width}.avif`, width]);+ }+ files.push({ name: `${name}.webp`, bytes: await sharp(png).resize({ width: Math.min(meta.width, 1440) }).webp({ quality: 88 }).toBuffer() });+ images[render.name] = { name, width: meta.width, height: meta.height, alt: render.alt, ...(render.caption ? { caption: render.caption } : {}), variants };+ }+ const diffAt = plan.steps.findIndex((step) => step.id === plan.diffStep);+ const { triangles } = inspectGlb(glbs.at(-1));+ return {+ record: {+ id: subject,+ name: plan.name,+ glbBytes: glbs.at(-1).length,+ triangles,+ steps,+ ...(plan.diffStep ? { diff: await unifiedDiff(sources[diffAt - 1], sources[diffAt], steps[diffAt - 1].sourceFile, steps[diffAt].sourceFile) } : {}),+ images,+ },+ files,+ model: glbs.at(-1),+ hero: renders.find(({ name }) => name === 'hero'),+ finalRevision: plan.steps.at(-1).revision,+ };+}++if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {+ const run = process.argv[2] && resolve(process.argv[2]);+ if (!run) throw Error('Usage: node scripts/stage-text-to-3d.mjs <directory>');+ const staged = await Promise.all(SUBJECTS.map((subject) => stageSubject(run, subject)));+ const { hero } = staged.find(({ record }) => record.id === 'cathedral');+ if (hero.with?.revisionId !== staged.find(({ record }) => record.id === 'mech').finalRevision) throw Error('The hero does not place the final mech');+ const files = staged.flatMap((subject) => subject.files);+ const digest = hash(Buffer.concat(files.flatMap(({ name, bytes }) => [Buffer.from(name), bytes]))).slice(0, 16);+ const base = `/text-to-3d/${digest}/`;+ const publicRoot = join(site, 'public/text-to-3d');+ if (existsSync(publicRoot)) await rm(publicRoot, { recursive: true });+ await mkdir(join(site, 'public', base), { recursive: true });+ for (const { name, bytes } of files) await writeFile(join(site, 'public', base, name), bytes);+ const models = new Map(staged.map(({ record, model }) => [record.id, { path: `text-to-3d/${hash(model).slice(0, 16)}/${record.id}.glb`, bytes: model.length, sha256: hash(model), model }]));+ const delivery = join(site, '.localdata/asset-delivery');+ for (const { path, model } of models.values()) {+ await mkdir(dirname(join(delivery, path)), { recursive: true });+ await writeFile(join(delivery, path), model);+ }+ const modelPins = [...models.values()].map(({ path, bytes, sha256 }) => ({ path, bytes, sha256 }));+ await writeFile(join(delivery, 'text-to-3d-upload.json'), `${JSON.stringify(modelPins, null, 2)}\n`);+ const writeData = (file, value) => writeFile(join(site, 'src/data', file), `${JSON.stringify(value, null, 2)}\n`);+ const isOurs = (path) => path.startsWith('text-to-3d/');+ const pins = await json(join(site, 'src/data/mirror-manifest.json'));+ await writeData('mirror-manifest.json', { ...pins, files: [...pins.files.filter(({ path }) => !isOurs(path)), ...modelPins] });+ const plan = await json(join(site, 'src/data/commons-build.json'));+ await writeData('commons-build.json', { ...plan, viewerModels: [...plan.viewerModels.filter((path) => !isOurs(path)), ...modelPins.map(({ path }) => path)] });+ const srcset = (list) => list.map(([name, width]) => `${base}${name} ${width}w`).join(', ');+ const subjects = staged.map(({ record }) => ({+ ...record,+ glbHref: `${ASSET_BASE}${models.get(record.id).path}`,+ steps: record.steps.map(({ sourceFile, ...step }) => ({ ...step, sourceHref: `${base}${sourceFile}` })),+ images: Object.fromEntries(Object.entries(record.images).map(([key, { name, variants, ...image }]) => [key, {+ ...image,+ src: `${base}${name}.webp`,+ srcsetWebp: srcset(variants.webp),+ srcsetAvif: srcset(variants.avif),+ }])),+ }));+ const lock = await json(join(run, 'kiln-install/package-lock.json'));+ const record = { tools: { kiln: lock.packages['node_modules/@instruktlabs/kiln'].version, ...(await json(join(run, 'tools.json'))) }, subjects };+ await writeFile(join(site, 'src/data/text-to-3d.json'), `${JSON.stringify(record, null, 2)}\n`);+ // Every page's layout reads the share card, so its picture gets a record far smaller than the page's.+ const card = subjects.find(({ id }) => id === 'cathedral').images.hero;+ await writeFile(join(site, 'src/data/text-to-3d-card.json'), `${JSON.stringify({ src: card.src, alt: card.alt }, null, 2)}\n`);+ console.log(`Staged ${files.length} files, ${files.reduce((sum, file) => sum + file.bytes.length, 0)} bytes, at ${base}`);+}diff --git a/site/scripts/text-to-3d-stage.test.ts b/site/scripts/text-to-3d-stage.test.tsnew file mode 100644index 00000000..a56fd3b3--- /dev/null+++ b/site/scripts/text-to-3d-stage.test.ts@@ -0,0 +1,117 @@+import { expect, test } from 'bun:test';+import { comparePlacement, lineCount, mergeGlbs, openGlb } from './stage-text-to-3d.mjs';++/** The smallest GLB that holds named meshes: each part is one triangle at its own offset, with a node translation and a material. */+function glb(parts: { name: string; at?: number[]; size?: number; material?: string; colour?: number }[]) {+ const stride = 16;+ const bin = Buffer.alloc(parts.length * 3 * stride);+ const materials = [...new Set(parts.map((part) => part.material ?? 'Stone'))];+ parts.forEach(({ size = 1, colour = 0 }, index) => {+ [[0, 0, 0], [size, 0, 0], [0, size, 0]].forEach((corner, vertex) => {+ const offset = (index * 3 + vertex) * stride;+ corner.forEach((value, axis) => bin.writeFloatLE(value, offset + axis * 4));+ bin.writeFloatLE(colour, offset + 12);+ });+ });+ const json = {+ asset: { version: '2.0' },+ scene: 0,+ scenes: [{ nodes: parts.map((_, index) => index) }],+ nodes: parts.map(({ name, at = [0, 0, 0] }, index) => ({ name, mesh: index, translation: at })),+ materials: materials.map((name) => ({ name })),+ meshes: parts.map(({ material = 'Stone' }, index) => ({ primitives: [{ attributes: { POSITION: index }, material: materials.indexOf(material) }] })),+ accessors: parts.map(({ size = 1 }, index) => ({ bufferView: 0, byteOffset: index * 3 * stride, componentType: 5126, count: 3, type: 'VEC3', min: [0, 0, 0], max: [size, size, 0] })),+ bufferViews: [{ buffer: 0, byteOffset: 0, byteLength: bin.length, byteStride: stride }],+ buffers: [{ byteLength: bin.length }],+ };+ const text = Buffer.from(JSON.stringify(json).padEnd(Math.ceil(JSON.stringify(json).length / 4) * 4, ' '));+ const header = Buffer.alloc(12);+ header.write('glTF', 0);+ header.writeUInt32LE(2, 4);+ header.writeUInt32LE(12 + 8 + text.length + 8 + bin.length, 8);+ const chunk = (bytes: Buffer, type: string) => {+ const head = Buffer.alloc(8);+ head.writeUInt32LE(bytes.length, 0);+ head.write(type, 4);+ return Buffer.concat([head, bytes]);+ };+ return Buffer.concat([header, chunk(text, 'JSON'), chunk(bin, 'BIN\0')]);+}++test('two saved GLBs make one scene, with every mesh kept and the second one placed by a single new node', () => {+ const church = glb([{ name: 'Tower' }, { name: 'Apse', at: [-10, 0, 0], material: 'Lead' }]);+ const walker = glb([{ name: 'Leg', size: 2, material: 'Paint' }]);+ const scene = mergeGlbs(church, walker, { name: 'Placed', translation: [5, 0, 7] });+ const { gltf } = openGlb(scene);+ // The first model is exactly where it was and the second has only moved.+ const first = comparePlacement(church, scene);+ expect(first).toMatchObject({ unmoved: 2, moved: 0, reshaped: 0, gone: 0, added: 1 });+ const second = comparePlacement(walker, scene);+ expect(second).toMatchObject({ unmoved: 0, moved: 1, reshaped: 0, gone: 0, added: 2 });+ expect(second.movedTogether).toEqual({ by: [5, 0, 7], count: 1 });+ expect(gltf.materials.map((material: { name: string }) => material.name)).toEqual(['Stone', 'Lead', 'Paint']);+ const leg = gltf.nodes.find((node: { name: string }) => node.name === 'Leg');+ expect(gltf.materials[gltf.meshes[leg.mesh].primitives[0].material].name).toBe('Paint');+ expect(gltf.scenes[gltf.scene].nodes.map((index: number) => gltf.nodes[index].name)).toEqual(['Tower', 'Apse', 'Placed']);+});++test('merging keeps the second model\'s textures on its own materials and drops its animation', () => {+ const textured = (image: string) => {+ const bytes = glb([{ name: image }]);+ const length = bytes.readUInt32LE(12);+ const gltf = JSON.parse(bytes.subarray(20, 20 + length).toString('utf8'));+ gltf.images = [{ name: image, bufferView: 0, mimeType: 'image/png' }];+ gltf.textures = [{ source: 0, sampler: 0 }];+ gltf.samplers = [{ magFilter: 9729 }];+ gltf.materials[0].pbrMetallicRoughness = { baseColorTexture: { index: 0 } };+ gltf.materials[0].normalTexture = { index: 0 };+ gltf.animations = [{ name: 'Walk', channels: [], samplers: [] }];+ gltf.extensionsUsed = [`EXT_${image}`];+ const text = JSON.stringify(gltf);+ const padded = Buffer.from(text.padEnd(Math.ceil(text.length / 4) * 4, ' '));+ const head = Buffer.alloc(8);+ head.writeUInt32LE(padded.length, 0);+ head.write('JSON', 4);+ return Buffer.concat([bytes.subarray(0, 12), head, padded, bytes.subarray(20 + length)]);+ };+ const { gltf } = openGlb(mergeGlbs(textured('a'), textured('b'), { name: 'Placed' }));+ expect(gltf.images.map((image: { name: string }) => image.name)).toEqual(['a', 'b']);+ expect(gltf.images[1].bufferView).toBe(gltf.bufferViews.length - 1);+ expect(gltf.textures[1]).toEqual({ source: 1, sampler: 1 });+ expect(gltf.materials[1].pbrMetallicRoughness.baseColorTexture.index).toBe(1);+ expect(gltf.materials[1].normalTexture.index).toBe(1);+ expect(gltf.extensionsUsed).toEqual(['EXT_a', 'EXT_b']);+ expect(gltf.animations).toHaveLength(1);+ expect(gltf.animations[0].name).toBe('Walk');+});++test('comparing two saved GLBs separates what stayed, what moved and what changed shape', () => {+ const before = glb([{ name: 'Tower' }, { name: 'Apse', at: [-10, 0, 0] }, { name: 'Choir', at: [-20, 0, 0] }, { name: 'Roof', size: 2 }, { name: 'Old' }]);+ const after = glb([{ name: 'Tower', colour: 1 }, { name: 'Apse', at: [-23.2, 0, 0] }, { name: 'Choir', at: [-33.2, 0, 0] }, { name: 'Roof', size: 3 }, { name: 'New' }]);+ const placement = comparePlacement(before, after);+ expect(placement.unmoved).toBe(1);+ expect(placement.moved).toBe(2);+ expect(placement.movedTogether).toEqual({ by: [-13.2, 0, 0], count: 2 });+ expect(placement.reshaped).toBe(1);+ expect(placement.gone).toBe(1);+ expect(placement.added).toBe(1);+ expect(placement.materialsSame).toBe(true);+});++test('matching by geometry and place sees through a part that was only renamed', () => {+ const before = glb([{ name: 'Tower' }, { name: 'Statue_3', at: [5, 0, 0], size: 2 }, { name: 'Choir', at: [-10, 0, 0], size: 3 }, { name: 'Apse', at: [-20, 0, 0], size: 4 }, { name: 'Roof', size: 5 }, { name: 'Fleche', at: [-5, 0, 0], size: 6 }]);+ const after = glb([{ name: 'Tower' }, { name: 'Statue_4', at: [5, 0, 0], size: 2 }, { name: 'Choir', at: [-23.2, 0, 0], size: 3 }, { name: 'Apse', at: [-33.2, 0, 0], size: 4 }, { name: 'Roof', size: 7 }, { name: 'Spire', size: 8 }]);+ const { geometry } = comparePlacement(before, after);+ expect(geometry).toEqual({ parent: 6, inPlace: 2, moved: 2, other: ['Roof', 'Fleche'] });+});++test('a program\'s line count is what wc -l says, whether or not the file ends in a newline', () => {+ expect(lineCount('a\nb\n')).toBe(2);+ expect(lineCount('a\nb')).toBe(2);+ expect(lineCount('')).toBe(0);+});++test('the ground is left out of a building\'s extent', () => {+ const placement = comparePlacement(glb([{ name: 'Tower', at: [5, 0, 0] }, { name: 'Mesh_Ground_Earth', at: [90, 0, 0], size: 9 }]), glb([{ name: 'Tower', at: [5, 0, 0] }, { name: 'Mesh_Ground_Earth', at: [90, 0, 0], size: 9 }]));+ expect(placement.extent.before).toEqual({ min: [5, 0, 0], max: [6, 1, 0] });+});diff --git a/site/scripts/text-to-3d.test.ts b/site/scripts/text-to-3d.test.tsnew file mode 100644index 00000000..ce2ecf4c--- /dev/null+++ b/site/scripts/text-to-3d.test.ts@@ -0,0 +1,223 @@+import { expect, test } from 'bun:test';+import { createHash } from 'node:crypto';+import { existsSync, readdirSync } from 'node:fs';+import { readFile } from 'node:fs/promises';+import { resolve } from 'node:path';+import { LOOK_INSIDE, sourceExcerpt, wordCount } from '../src/lib/text-to-3d';+import * as THREE from 'three';+import { socialCards } from '../src/lib/social-cards.mjs';+import { openGlb } from './stage-text-to-3d.mjs';++const site = resolve(import.meta.dir, '..');+const record = JSON.parse(await readFile(resolve(site, 'src/data/text-to-3d.json'), 'utf8'));+const page = await readFile(resolve(site, 'src/pages/text-to-3d.astro'), 'utf8');+const component = await readFile(resolve(site, 'src/components/TextTo3dRun.astro'), 'utf8');+const served = (href: string) => readFile(resolve(site, `public${href}`));+const subject = (id: string) => record.subjects.find((candidate: { id: string }) => candidate.id === id);+const stepOf = (id: string, step: string) => subject(id).steps.find((candidate: { id: string }) => candidate.id === step);+const pins = JSON.parse(await readFile(resolve(site, 'src/data/mirror-manifest.json'), 'utf8'));+const plan = JSON.parse(await readFile(resolve(site, 'src/data/commons-build.json'), 'utf8'));+const modelPath = ({ glbHref }: { glbHref: string }) => new URL(glbHref).pathname.slice(1);+/** The GLBs are on the asset host. A packs build keeps them in its verified mirror, and staging keeps its own copy; CI has neither. */+const localModel = (candidate: { glbHref: string }) => ['.cache/commons/mirror', '.localdata/asset-delivery'].map((directory) => resolve(site, directory, modelPath(candidate))).find((file) => existsSync(file));+const stagedFiles = () => readdirSync(resolve(site, 'public/text-to-3d'), { recursive: true, withFileTypes: true }).filter((entry) => entry.isFile()).map((entry) => resolve(entry.parentPath, entry.name));++test('the record holds what the page shows and nothing else', () => {+ expect(Object.keys(record).sort()).toEqual(['subjects', 'tools']);+ expect(Object.keys(record.tools).sort()).toEqual(['effort', 'harness', 'harnessVersion', 'kiln', 'model']);+ for (const { images, steps, ...rest } of record.subjects) {+ for (const key of Object.keys(rest)) expect(['id', 'name', 'glbHref', 'glbBytes', 'triangles', 'diff']).toContain(key);+ for (const step of steps) for (const key of Object.keys(step)) expect(['id', 'prompt', 'sourceHref', 'sourceLines', 'geometry']).toContain(key);+ for (const image of Object.values(images) as object[]) for (const key of Object.keys(image)) expect(['width', 'height', 'alt', 'caption', 'src', 'srcsetWebp', 'srcsetAvif']).toContain(key);+ }+});++test('the page says nothing about what the models took to make', () => {+ for (const text of [page, component]) {+ expect(text).not.toMatch(/\$\d|\b\d+(\.\d+)? ?(min|minutes?|hours?)\b/i);+ // A reader's "Can I" is fine; the page itself has no unnamed author.+ expect(text.replaceAll('Can I ', '')).not.toMatch(/\bI\b|\bmy\s/);+ }+});++test('every staged file is one the page uses, and everything it uses is there', () => {+ const used = new Set<string>();+ for (const { steps, images } of record.subjects) {+ for (const { sourceHref } of steps) used.add(sourceHref);+ for (const image of Object.values(images) as { src: string; srcsetWebp: string; srcsetAvif: string }[]) {+ used.add(image.src);+ for (const list of [image.srcsetWebp, image.srcsetAvif]) for (const candidate of list.split(', ')) used.add(candidate.split(' ')[0]!);+ }+ }+ const staged = stagedFiles();+ expect(staged.sort()).toEqual([...used].map((href) => resolve(site, `public${href}`)).sort());+});++test('the viewer and the download use the pinned GLB, on the asset host under a name from its own hash', async () => {+ for (const candidate of record.subjects) {+ const path = modelPath(candidate);+ const pin = pins.files.find((file: { path: string }) => file.path === path);+ expect(candidate.glbHref).toBe(`https://assets.kilnstudio.tools/${path}`);+ expect(path).toBe(`text-to-3d/${pin.sha256.slice(0, 16)}/${candidate.id}.glb`);+ expect(pin.bytes).toBe(candidate.glbBytes);+ expect(plan.viewerModels).toContain(path);+ expect(candidate.glbBytes).toBeLessThanOrEqual(16 * 1024 * 1024);+ const local = localModel(candidate);+ if (local) expect(createHash('sha256').update(await readFile(local)).digest('hex')).toBe(pin.sha256);+ }+});++test('only the page\'s two final models are pinned, and neither is in the repository', () => {+ const ours = pins.files.filter(({ path }: { path: string }) => path.startsWith('text-to-3d/')).map(({ path }: { path: string }) => path).sort();+ expect(ours).toEqual(record.subjects.map(modelPath).sort());+ expect(stagedFiles().some((file) => file.endsWith('.glb'))).toBe(false);+});++test('the shown diff is two more bays and a spire, between the two saved programs', async () => {+ const { diff, steps } = subject('cathedral');+ const at = steps.findIndex((step: { id: string }) => step.id === 'revise');+ const before = (await served(steps[at - 1].sourceHref)).toString('utf8');+ const after = (await served(steps[at].sourceHref)).toString('utf8');+ const lines: string[] = diff.split('\n');+ const added = lines.filter((line) => line.startsWith('+') && !line.startsWith('+++')).map((line) => line.slice(1));+ const removed = lines.filter((line) => line.startsWith('-') && !line.startsWith('---')).map((line) => line.slice(1));+ expect(added.length).toBeGreaterThan(0);+ for (const line of added) expect(after).toContain(line);+ for (const line of removed) expect(before).toContain(line);+ expect(diff).toContain('+ naveBays: 11');+});++test.skipIf(!localModel(subject('mech')))('the shipped mech has the named joints and the Walk clip the page offers', async () => {+ const { gltf } = openGlb(await readFile(localModel(subject('mech'))!));+ const names = gltf.nodes.map((node: { name: string }) => node.name).join(' ');+ for (const joint of [/hip/i, /knee/i, /ankle/i, /toe/i, /waist/i, /shoulder/i, /elbow/i]) expect(names).toMatch(joint);+ expect(gltf.animations.map((clip: { name: string }) => clip.name)).toContain('Walk');+});++test.skipIf(!localModel(subject('mech')))('played through a plain three.js mixer, the shipped mech keeps a planted foot where it was put', async () => {+ const { gltf, floats } = openGlb(await readFile(localModel(subject('mech'))!));+ const objects = gltf.nodes.map((node: { name: string; translation?: number[]; rotation?: number[] }) => {+ const object = new THREE.Object3D();+ object.name = node.name;+ if (node.translation) object.position.fromArray(node.translation);+ if (node.rotation) object.quaternion.fromArray(node.rotation);+ return object;+ });+ gltf.nodes.forEach((node: { children?: number[] }, index: number) => (node.children ?? []).forEach((child) => objects[index].add(objects[child])));+ const walk = gltf.animations.find((clip: { name: string }) => clip.name === 'Walk');+ const tracks = walk.channels.map((channel: { sampler: number; target: { node: number; path: string } }) => {+ const sampler = walk.samplers[channel.sampler];+ const rotation = channel.target.path === 'rotation';+ const Track = rotation ? THREE.QuaternionKeyframeTrack : THREE.VectorKeyframeTrack;+ return new Track(`${gltf.nodes[channel.target.node].name}.${rotation ? 'quaternion' : 'position'}`, floats(sampler.input), floats(sampler.output));+ });+ const rootIndex = gltf.scenes[0].nodes[0];+ const root = objects[rootIndex];+ const clip = new THREE.AnimationClip('Walk', -1, tracks);+ const mixer = new THREE.AnimationMixer(root);+ mixer.clipAction(clip).play();+ const speed = gltf.nodes[rootIndex].extras.rig.walkSpeedMps;+ const samples = 1200;+ for (const side of ['L', 'R']) {+ const toe = root.getObjectByName(`Joint_Toe_${side}`)!;+ const poses = Array.from({ length: samples }, (_, index) => {+ const time = (clip.duration * index) / samples;+ mixer.setTime(time);+ root.updateMatrixWorld(true);+ const { x, y, z } = toe.getWorldPosition(new THREE.Vector3());+ // The clip walks in place, so a foot on the ground is fixed once the body's own travel is added back.+ return { x: x + speed * time, y, z };+ });+ const ground = Math.min(...poses.map(({ y }) => y));+ let planted: typeof poses = [];+ let run: typeof poses = [];+ for (const pose of poses) {+ if (pose.y < ground + 0.0005) run.push(pose);+ else run = [];+ if (run.length > planted.length) planted = [...run];+ }+ const spread = (axis: 'x' | 'z') => Math.max(...planted.map((pose) => pose[axis])) - Math.min(...planted.map((pose) => pose[axis]));+ expect(planted.length / samples).toBeGreaterThan(0.45);+ expect(spread('x')).toBeLessThan(0.0005);+ expect(spread('z')).toBeLessThan(0.0005);+ }+});++test('the cathedral stays within its budget', () => {+ expect(subject('cathedral').glbBytes).toBeLessThanOrEqual(15_000_000);+});++test.skipIf(!localModel(subject('cathedral')))('the cathedral carries real textures and its ground is a part of its own', async () => {+ const { gltf } = openGlb(await readFile(localModel(subject('cathedral'))!));+ expect(gltf.images.length).toBeGreaterThan(0);+ expect(gltf.nodes.map((node: { name: string }) => node.name)).toContain('Ground');+});++test('a prompt\'s word count is what the card says', () => {+ expect(wordCount('one two\n\n three ')).toBe(3);+});++test('a source excerpt is a real slice of the program, and a missing one fails the build', () => {+ expect(sourceExcerpt('const a = 1;\nconst b = 2;\nconst c = 3;', 'const b', '= 2;')).toBe('const b = 2;');+ expect(() => sourceExcerpt('const a = 1;', 'const b', ';')).toThrow();+});++test('nothing published names a path on the machine that made it', async () => {+ const private_ = /\/home\/|\/tmp\//;+ expect(JSON.stringify(record)).not.toMatch(private_);+ for (const file of stagedFiles()) if (/\.(js|json)$/.test(file)) expect(await readFile(file, 'utf8')).not.toMatch(private_);+});++test('the numbers the revision cards give are the measured ones', () => {+ expect(stepOf('cathedral', 'revise').geometry).toEqual({ parent: 1621, inPlace: 971, moved: 639 });+ expect(stepOf('cathedral', 'light').geometry).toEqual({ parent: 1715, inPlace: 1705, moved: 0 });+ expect(page).toContain('kept their exact shape');+ expect(page).toContain('moved east together');+});++test('the final program bakes the sun the page describes', async () => {+ const final = (await served(subject('cathedral').steps.at(-1).sourceHref)).toString('utf8');+ expect(final).toContain('sun: [0.11, 0.79, -0.61]');+});++test('line counts are what wc -l says', async () => {+ for (const { steps } of record.subjects) for (const step of steps) expect(step.sourceLines).toBe((await served(step.sourceHref)).toString('utf8').trimEnd().split('\n').length);+});++test('the Look inside view starts inside the building, within the viewer\'s orbit limits', () => {+ // The final GLB's bounds with its ground, then the building alone, as staging measures them.+ const radius = Math.hypot(224, 100, 144) / 2;+ const building = { min: [-77.304, 0, -30.509], max: [66.578, 100, 30.509] };+ const distance = Math.hypot(...LOOK_INSIDE.position.map((value, axis) => value - LOOK_INSIDE.target[axis]!));+ expect(distance).toBeGreaterThan(radius * 0.3);+ expect(distance).toBeLessThan(radius * 14);+ for (const point of [LOOK_INSIDE.position, LOOK_INSIDE.target]) for (const axis of [0, 1, 2]) {+ expect(point[axis]).toBeGreaterThanOrEqual(building.min[axis]!);+ expect(point[axis]).toBeLessThanOrEqual(building.max[axis]!);+ }+});++test('the page takes its numbers and prompts from the record and has no leftover draft copy', () => {+ expect(page).not.toContain('RUN-COPY');+ expect(page).not.toContain('PREVIEW');+ expect(page).toContain("from \"../data/text-to-3d.json\"");+ for (const { steps } of record.subjects) for (const step of steps) expect(page).not.toContain(step.prompt);+});++test('the share card pictures the page hero, from a record small enough for every page to import', async () => {+ const { hero } = subject('cathedral').images;+ const small = JSON.parse(await readFile(resolve(site, 'src/data/text-to-3d-card.json'), 'utf8'));+ expect(small).toEqual({ src: hero.src, alt: hero.alt });+ const data = { packsEnabled: false, release: { version: '1.6.0' }, farm: {}, vehicles: {}, bridge: {}, hero: {}, foundryFloor: {} };+ const card = socialCards({ ...data, textTo3d: small }).find((candidate: { slug: string }) => candidate.slug === 'text-to-3d');+ expect(card?.poster?.src).toBe(hero.src);+ expect(page).toContain('cathedral.images.hero!');+ expect(socialCards(data).some((candidate: { slug: string }) => candidate.slug === 'text-to-3d')).toBe(false);+});++test('the guardrails hold: home keeps its title and description, and links to this page', async () => {+ const home = await readFile(resolve(site, 'src/pages/index.astro'), 'utf8');+ expect(home).toContain('title="Kiln | build and revise 3D assets with your coding agent"');+ expect(home).toContain('description="Your coding agent writes JavaScript. Kiln renders views, checks geometry and saves editable 3D assets as GLBs with their source. Runs locally; MIT licensed."');+ expect(home).toContain('href="/text-to-3d/"');+});diff --git a/site/scripts/viewer-framing.test.ts b/site/scripts/viewer-framing.test.tsindex 9129ac61..2d211914 100644--- a/site/scripts/viewer-framing.test.ts+++ b/site/scripts/viewer-framing.test.ts@@ -1,6 +1,7 @@import { expect, test } from 'bun:test';+import { readFile } from 'node:fs/promises';import * as THREE from 'three';-import { AZIMUTH, frameAsset, groundGrid, meshTops, orbitDirection, wantsMeshTops } from '../src/components/viewer-framing';+import { AZIMUTH, frameAsset, groundGrid, meshTops, orbitDirection, showView, wantsMeshTops } from '../src/components/viewer-framing';test('a very large model offers each mesh\'s top, so a slim spire stays in the frame', () => {const body = new THREE.Mesh(new THREE.BoxGeometry(100, 40, 40));@@ -35,6 +36,16 @@ test('only a model past a million vertices offers its mesh tops', () => {expect(wantsMeshTops(2_489_344)).toBe(true);});+test('a saved view puts the camera and the orbit target where the page says, and nothing else moves', () => {+ const camera = new THREE.PerspectiveCamera(34, 2, 0.1, 5000);+ camera.position.set(600, 200, 400);+ const target = new THREE.Vector3(2, 40, 0);+ showView(camera, target, { label: 'Look inside', position: [46, 2, 0], target: [-40, 20, 0] });+ expect(camera.position.toArray()).toEqual([46, 2, 0]);+ expect(target.toArray()).toEqual([-40, 20, 0]);+ expect(camera.fov).toBe(34);+});+// One unit in the last place of a 32-bit float, which is what the grid's shader has.const precisionAt = (value: number) => 2 ** (Math.floor(Math.log2(value)) - 23);@@ -70,3 +81,15 @@ for (const aspect of [0.59, 1, 2.4]) {}});}++const runtime = await readFile(new URL('../src/components/AssetViewerRuntime.tsx', import.meta.url), 'utf8');++test('a saved view waits for the model, because framing the model when it arrives would undo the click', () => {+ expect(runtime).toMatch(/views\?\.map\(\(view\) => \(\s*<button[^>]*disabled=\{!ready\}/);+});++test('a resize leaves a saved view where it is, and reset still frames the whole model', () => {+ expect(runtime).toMatch(/if \(!controls \|\| viewShowing\.current\) return;/);+ expect(runtime).toMatch(/viewShowing\.current = true;\s*setSpin\(false\)/);+ expect(runtime).toMatch(/viewShowing\.current = false;\s*setFraming\(/);+});diff --git a/site/src/components/AssetTierDownloads.astro b/site/src/components/AssetTierDownloads.astronew file mode 100644index 00000000..f47f2609--- /dev/null+++ b/site/src/components/AssetTierDownloads.astro@@ -0,0 +1,11 @@+---+import AssetDownloads from './AssetDownloads.astro';+import type { DownloadFile } from '../lib/download-choices';+interface Props { tier: string; note: string; runtime: DownloadFile; editable: DownloadFile }+const { tier, note, runtime, editable } = Astro.props;+---+<article>+ <h3 class="text-title capitalize">{tier} detail</h3>+ <p class="mt-2 text-dim">{note}</p>+ <AssetDownloads runtime={runtime} editable={editable} />+</article>diff --git a/site/src/components/AssetViewer.astro b/site/src/components/AssetViewer.astroindex 139c8ece..f99e29ad 100644--- a/site/src/components/AssetViewer.astro+++ b/site/src/components/AssetViewer.astro@@ -1,4 +1,6 @@---+import type { SavedView } from "./viewer-framing";+interface Props {name: string;modelUrl: string;@@ -13,6 +15,10 @@ interface Props {frameClass?: string;/** Exact allowlisted Vehicles catalog ID; omitted on all other assets. */paintAssetId?: string;+ /** Saved cameras to offer as buttons once the viewer is open; omitted on most assets. */+ views?: SavedView[];+ /** What the open button says, when the page wants the download's size on it. */+ openLabel?: string;}const {name,@@ -25,6 +31,8 @@ const {srcsetAvif,frameClass = "bg-backdrop",paintAssetId,+ views,+ openLabel = "Open 3D view",} = Astro.props;// A single-file poster has no candidates to choose from, so it declares no sizes.const sizes = srcsetWebp || srcsetAvif ? "(max-width: 768px) 100vw, 70vw" : undefined;@@ -34,6 +42,7 @@ const sizes = srcsetWebp || srcsetAvif ? "(max-width: 768px) 100vw, 70vw" : undedata-name={name}data-model={modelUrl}data-paint-asset={paintAssetId}+ data-views={views && JSON.stringify(views)}class:list={["block border border-rule", frameClass]}><div data-poster>@@ -59,7 +68,7 @@ const sizes = srcsetWebp || srcsetAvif ? "(max-width: 768px) 100vw, 70vw" : unde</div><div class="flex flex-wrap items-center gap-4 border-t border-rule bg-sheet p-4"><button type="button" data-open hidden class="btn">- Open 3D view+ {openLabel}</button><button type="button" data-close hidden class="btn btn-secondary">Close 3D view@@ -96,6 +105,7 @@ const sizes = srcsetWebp || srcsetAvif ? "(max-width: 768px) 100vw, 70vw" : undename: this.dataset.name!,modelUrl: this.dataset.model!,paintAssetId: this.dataset.paintAsset,+ views: this.dataset.views ? JSON.parse(this.dataset.views) : undefined,onError: (error) => {session.stop();status.textContent = viewerErrorMessage(error);diff --git a/site/src/components/AssetViewerRuntime.tsx b/site/src/components/AssetViewerRuntime.tsxindex 249610a5..6b34c831 100644--- a/site/src/components/AssetViewerRuntime.tsx+++ b/site/src/components/AssetViewerRuntime.tsx@@ -1,6 +1,6 @@import { Canvas, useFrame, useThree } from '@react-three/fiber';import { ContactShadows, Grid, OrbitControls, useGLTF, useProgress } from '@react-three/drei';-import { Component, Suspense, useEffect, useMemo, useState, type ReactNode } from 'react';+import { Component, Suspense, useEffect, useMemo, useRef, useState, type ReactNode } from 'react';import { createRoot } from 'react-dom/client';import * as THREE from 'three';import { REVIEW_RIG } from '../lib/review-rig';@@ -19,6 +19,8 @@ import {groundGrid,meshTops,orbitDirection,+ type SavedView,+ showView,wantsMeshTops,} from './viewer-framing';import { clearingOnError } from './viewer-retry';@@ -103,6 +105,7 @@ function Model({url,wireframe,framing,+ viewShowing,onBounds,clipIndex,paused,@@ -116,6 +119,7 @@ function Model({onReady: () => void;wireframe: boolean;framing: number;+ viewShowing: { current: boolean };onBounds: (radius: number) => void;clipIndex: number;paused: boolean;@@ -210,7 +214,7 @@ function Model({// job.// biome-ignore lint/correctness/useExhaustiveDependencies: framing is a trigger, not an input.useEffect(() => {- if (!controls) return;+ if (!controls || viewShowing.current) return;const box = new THREE.Box3().setFromObject(scene);const size = box.getSize(new THREE.Vector3());const center = box.getCenter(new THREE.Vector3());@@ -234,7 +238,7 @@ function Model({controls.maxDistance = radius * 14;controls.update();onBounds(radius);- }, [scene, points, camera, controls, framing, onBounds, width, height]);+ }, [scene, points, camera, controls, framing, viewShowing, onBounds, width, height]);useEffect(() => {const touched: THREE.Material[] = [];@@ -262,6 +266,8 @@ interface ViewerProps {modelUrl: string;onError: (error: Error) => void;paintAssetId?: string;+ /** Saved cameras to offer as buttons. Most models have none. */+ views?: SavedView[];}class ViewerBoundary extends Component<@@ -299,13 +305,20 @@ function ContextMonitor({ onError }: { onError: (error: Error) => void }) {return null;}-type CameraCommand = { action: 'left' | 'right' | 'up' | 'down' | 'in' | 'out'; sequence: number };+type CameraCommand =+ | { action: 'left' | 'right' | 'up' | 'down' | 'in' | 'out'; sequence: number }+ | { action: 'view'; view: SavedView; sequence: number };function KeyboardCamera({ command }: { command: CameraCommand | undefined }) {const camera = useThree((state) => state.camera);const controls = useThree((state) => state.controls) as OrbitLike | null;useEffect(() => {if (!command || !controls) return;+ if (command.action === 'view') {+ showView(camera, controls.target, command.view);+ controls.update();+ return;+ }const offset = camera.position.clone().sub(controls.target);const spherical = new THREE.Spherical().setFromVector3(offset);if (command.action === 'left') spherical.theta -= 0.15;@@ -322,7 +335,7 @@ function KeyboardCamera({ command }: { command: CameraCommand | undefined }) {return null;}-function Viewer({ name, modelUrl, onError, paintAssetId }: ViewerProps) {+function Viewer({ name, modelUrl, onError, paintAssetId, views }: ViewerProps) {const [wireframe, setWireframe] = useState(false);const [grid, setGrid] = useState(true);const [spin, setSpin] = useState(false);@@ -337,8 +350,15 @@ function Viewer({ name, modelUrl, onError, paintAssetId }: ViewerProps) {const [paintColour, setPaintColour] = useState<string | null>(null);const [originalPaint, setOriginalPaint] = useState<string>();const onReady = useMemo(() => () => setReady(true), []);- const move = (action: CameraCommand['action']) =>+ const move = (action: Exclude<CameraCommand['action'], 'view'>) =>setCommand((previous) => ({ action, sequence: (previous?.sequence ?? 0) + 1 }));+ // A saved camera stays until reset: refitting the whole model on a resize or a phone's turn would throw it back outside.+ const viewShowing = useRef(false);+ const look = (view: SavedView) => {+ viewShowing.current = true;+ setSpin(false);+ setCommand((previous) => ({ action: 'view', view, sequence: (previous?.sequence ?? 0) + 1 }));+ };return (<div><section className="relative h-viewer" aria-label={`Interactive ${name} model`}>@@ -370,6 +390,7 @@ function Viewer({ name, modelUrl, onError, paintAssetId }: ViewerProps) {url={modelUrl}wireframe={wireframe}framing={framing}+ viewShowing={viewShowing}onBounds={setRadius}clipIndex={clipIndex}paused={paused}@@ -452,6 +473,17 @@ function Viewer({ name, modelUrl, onError, paintAssetId }: ViewerProps) {className="flex flex-wrap gap-2 border-t border-rule bg-sheet p-4"aria-label="3D inspection controls">+ {views?.map((view) => (+ <button+ key={view.label}+ type="button"+ className="btn"+ disabled={!ready}+ onClick={() => look(view)}+ >+ {view.label}+ </button>+ ))}<buttontype="button"className="btn btn-secondary"@@ -529,6 +561,7 @@ function Viewer({ name, modelUrl, onError, paintAssetId }: ViewerProps) {className="btn btn-secondary"onClick={() => {setSpin(false);+ viewShowing.current = false;setFraming((value) => value + 1);}}>diff --git a/site/src/components/TextTo3dRun.astro b/site/src/components/TextTo3dRun.astronew file mode 100644index 00000000..9ec6851a--- /dev/null+++ b/site/src/components/TextTo3dRun.astro@@ -0,0 +1,138 @@+---+import AssetViewer from "./AssetViewer.astro";+import CodeBlock from "./CodeBlock.astro";+import ResponsiveImage from "./ResponsiveImage.astro";+import type { SavedView } from "./viewer-framing";+import { number } from "../lib/catalog";+import { megabytes, wordCount, type TextTo3dSubject } from "../lib/text-to-3d";++interface RevisionCard {+ /** The step of the record this card shows. */+ step: string;+ title: string;+ /** One line: what changed, and why it's worth seeing. */+ line: string;+ /** Pictures by name in the record: one, or a before and an after. */+ figures: string[];+}+interface Props {+ subject: TextTo3dSubject;+ /** The subject's short name, for labels that must fit on a phone beside a Copy button. */+ short: string;+ title: string;+ intro: string;+ /** Something a reader should know before downloading this model. */+ note?: string;+ /** Said with the licence, for what the model takes from elsewhere. */+ credit?: string;+ /** Said once, under the viewer: what to try in it. */+ tryIt: string;+ /** Saved cameras the viewer offers as buttons. */+ views?: SavedView[];+ /** A second picture worth stopping for, by name in the record, and what it shows. */+ feature?: { image: string; title: string; line: string };+ /** Closed until asked for: someone may only want the model. */+ revisions: RevisionCard[];+ /** Code worth reading, closed for the same reason. */+ code: { summary: string; code: string; label: string; lang?: "javascript" | "diff"; previewLines?: number }[];+}+const { subject, short, title, intro, note, credit, tryIt, views, feature, revisions, code } = Astro.props;+const headingId = `${subject.id}-heading`;+const final = subject.steps.at(-1)!;+const finalImage = subject.images.final!;+const featureImage = feature && subject.images[feature.image]!;+const stepOf = (id: string) => subject.steps.find((step) => step.id === id)!;+const stats = [+ ["Prompts", subject.steps.length],+ ["Triangles", subject.triangles >= 1_000_000 ? `${(subject.triangles / 1_000_000).toFixed(1)} M` : number(subject.triangles)],+ ["GLB", `${megabytes(subject.glbBytes)} MB`],+] as const;+const THUMB_SIZES = "(max-width: 639px) 3.5rem, 5rem";+const FEATURE_SIZES = "(max-width: 1023px) calc(100vw - 2rem), 66vw";+const CARD_SIZES = "(max-width: 767px) calc(100vw - 2rem), (max-width: 1279px) calc(50vw - 2rem), 26rem";+const PAIR_SIZES = "(max-width: 767px) calc(50vw - 1rem), (max-width: 1279px) calc(25vw - 1rem), 13rem";+---+<section id={subject.id} class="container section-space" aria-labelledby={headingId}>+ <div class="grid gap-x-8 gap-y-6 border-t border-ink pt-8 lg:grid-cols-3 lg:grid-rows-[auto_1fr]">+ <div class="min-w-0 lg:col-start-3">+ <h2 id={headingId}>{title}</h2>+ <p class="mt-4 text-dim">{intro}</p>+ {note && <p class="mt-4 border-l-2 border-ember pl-4">{note}</p>}+ </div>+ <div class="drawing-sheet min-w-0 lg:col-span-2 lg:col-start-1 lg:row-span-2 lg:row-start-1 lg:self-start">+ <AssetViewer+ name={subject.name}+ modelUrl={subject.glbHref}+ poster={finalImage.src}+ posterAlt={finalImage.alt}+ posterWidth={finalImage.width}+ posterHeight={finalImage.height}+ srcsetWebp={finalImage.srcsetWebp}+ srcsetAvif={finalImage.srcsetAvif}+ views={views}+ openLabel={`Open 3D view, ${megabytes(subject.glbBytes)} MB`}+ />+ <p class="border-t border-ink bg-sheet px-5 py-4">{tryIt}</p>+ </div>+ <div class="min-w-0 lg:col-start-3">+ <div class="grid gap-3">+ <a class="btn" href={subject.glbHref} download>Download GLB, {megabytes(subject.glbBytes)} MB<span class="sr-only">, {subject.name}</span> <span aria-hidden="true">↓</span></a>+ <a class="btn btn-secondary" href={final.sourceHref} download>Download source, {number(final.sourceLines)} lines<span class="sr-only">, {subject.name}</span> <span aria-hidden="true">↓</span></a>+ </div>+ <p class="mt-3 text-caption text-dim"><a class="text-link" href="https://creativecommons.org/publicdomain/zero/1.0/">CC0-1.0 <span aria-hidden="true">↗</span></a>: use both for anything.{credit && ` ${credit}`}</p>+ <dl class="mt-5 grid grid-cols-3 border-y border-ink">+ {stats.map(([label, value]) => <div class="min-w-0 border-r border-rule px-3 py-4 last:border-r-0 lg:px-2 xl:px-3"><dt class="font-mono text-xs uppercase tracking-wide text-dim">{label}</dt><dd class="mt-2 text-xl font-semibold leading-7 sm:text-2xl lg:text-lg xl:text-2xl">{value}</dd></div>)}+ </dl>+ </div>+ </div>+ {feature && featureImage && (+ <div class="mt-8 grid gap-x-8 gap-y-4 lg:grid-cols-3">+ <figure class="min-w-0 lg:col-span-2">+ <ResponsiveImage image={featureImage} sizes={FEATURE_SIZES} class="w-full bg-backdrop" />+ </figure>+ <div>+ <h3 class="text-subheading">{feature.title}</h3>+ <p class="mt-4 text-dim">{feature.line}</p>+ </div>+ </div>+ )}+ <div class="mt-8 border-t border-rule">+ <details class="group border-b border-rule">+ <summary class="font-semibold">+ See all {subject.steps.length} revisions and their prompts+ <span class="mt-3 flex flex-wrap gap-2 group-open:hidden">+ {revisions.map((card) => <ResponsiveImage image={{ ...subject.images[card.figures.at(-1)!]!, alt: "" }} sizes={THUMB_SIZES} class="size-14 bg-backdrop object-cover sm:size-20" />)}+ </span>+ </summary>+ <ol class="grid list-none gap-8 px-4 pb-8 pt-2 md:grid-cols-2 xl:grid-cols-3">+ {revisions.map((card) => {+ const step = stepOf(card.step);+ const revision = subject.steps.indexOf(step) + 1;+ const { prompt } = step;+ const figures = card.figures.map((name) => subject.images[name]!);+ return (+ <li class="min-w-0">+ <div class={figures.length > 1 ? "grid grid-cols-2 gap-2" : undefined}>+ {figures.map((image) => <ResponsiveImage image={image} sizes={figures.length > 1 ? PAIR_SIZES : CARD_SIZES} class="w-full bg-backdrop" />)}+ </div>+ <p class="mt-3 font-mono text-code text-dim">Revision {revision}</p>+ <h3 class="text-title">{card.title}</h3>+ <p class="mt-1 text-dim">{card.line}</p>+ <a class="text-link" href={step.sourceHref} download>Its source, {number(step.sourceLines)} lines<span class="sr-only">, {short} revision {revision}</span> <span aria-hidden="true">↓</span></a>+ <details class="mt-1 border border-rule bg-sheet">+ <summary class="font-mono text-code">Read the prompt, {number(wordCount(prompt))} words</summary>+ <div class="px-2 pb-2"><CodeBlock code={prompt} lang="text" label={`${short}, prompt ${revision}`} /></div>+ </details>+ </li>+ );+ })}+ </ol>+ </details>+ {code.map((block) => (+ <details class="border-b border-rule">+ <summary class="font-semibold">{block.summary}</summary>+ <div class="min-w-0 px-4 pb-6"><CodeBlock code={block.code} lang={block.lang ?? "javascript"} label={block.label} previewLines={block.previewLines} /></div>+ </details>+ ))}+ </div>+</section>diff --git a/site/src/components/viewer-framing.ts b/site/src/components/viewer-framing.tsindex aebdd333..9f74f6c7 100644--- a/site/src/components/viewer-framing.ts+++ b/site/src/components/viewer-framing.ts@@ -36,6 +36,19 @@ export function groundGrid(radius: number) {};}+/** A saved camera a page can offer beside the orbit controls, in the model's own metres. */+export interface SavedView {+ label: string;+ position: [number, number, number];+ target: [number, number, number];+}++/** Moves the camera and the orbit target to a saved view; the orbit controls take it from there. */+export function showView(camera: THREE.Camera, target: THREE.Vector3, view: SavedView) {+ camera.position.fromArray(view.position);+ target.fromArray(view.target);+}+/** Past this many vertices, the surface sample below is coarse enough to step over a slim part altogether. */const MESH_TOPS_ABOVE_VERTICES = 1_000_000;export const wantsMeshTops = (vertices: number) => vertices > MESH_TOPS_ABOVE_VERTICES;diff --git a/site/src/data/commons-build.json b/site/src/data/commons-build.jsonindex abd1b94b..f16854eb 100644--- a/site/src/data/commons-build.json+++ b/site/src/data/commons-build.json@@ -189,7 +189,9 @@"packs/holodeck/objects/afca29899c682afbcdc08e6a00ab9cff72e67a9d39ec0034c6d5e2f4e7b940b8/meridian-balustrade-4.glb","packs/holodeck/objects/3f2c302d07380e20fafe794a56f7f3b7ca0ecc886529accb6fee7eed0bece6f4/meridian-cube-mirror-far.glb","packs/holodeck/objects/bc5ba472b23aa8aa40b3bcf84d4f83284617f6480858b7955d9bcb47fad9684b/meridian-statue-figure.glb",- "packs/holodeck/objects/fc54ddf1a7d1f0d70b8c6b414758ed7ddd690c6650d7053e1506ab95c94ccd94/meridian-statue-whale.glb"+ "packs/holodeck/objects/fc54ddf1a7d1f0d70b8c6b414758ed7ddd690c6650d7053e1506ab95c94ccd94/meridian-statue-whale.glb",+ "text-to-3d/9b1b004c460cc0a9/mech.glb",+ "text-to-3d/6f1ee50efb3aa2ab/cathedral.glb"],"images": [{diff --git a/site/src/data/mirror-manifest.json b/site/src/data/mirror-manifest.jsonindex d92c4a6a..cc57ed02 100644--- a/site/src/data/mirror-manifest.json+++ b/site/src/data/mirror-manifest.json@@ -6535,6 +6535,16 @@"path": "media/holodeck/scenes/fcb5b7444adbd92180e943b0207ef923c222134c340b5cda4f281494d352f71d/meridian-sanctuary-arrival.png","bytes": 1974966,"sha256": "fcb5b7444adbd92180e943b0207ef923c222134c340b5cda4f281494d352f71d"+ },+ {+ "path": "text-to-3d/9b1b004c460cc0a9/mech.glb",+ "bytes": 4777424,+ "sha256": "9b1b004c460cc0a924f6a4d2d218a2d292ebbd507add1653cfc5152799d96df5"+ },+ {+ "path": "text-to-3d/6f1ee50efb3aa2ab/cathedral.glb",+ "bytes": 14948932,+ "sha256": "6f1ee50efb3aa2ab733c9159a108a02b43862eb880a9a3e0ac7eb805b533d64c"}]}diff --git a/site/src/data/text-to-3d-card.json b/site/src/data/text-to-3d-card.jsonnew file mode 100644index 00000000..e6e4fb52--- /dev/null+++ b/site/src/data/text-to-3d-card.json@@ -0,0 +1,4 @@+{+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-hero.webp",+ "alt": "A Gothic cathedral seen from the ground at its south-west corner: twin west towers about 80 m tall, three deep portals, a rose window, flying buttresses and a lead spire over the crossing. In front of the south tower stands a 6 m walking mech in olive armour, small against the building, with a rotary cannon and a rail cannon with glowing coils."+}diff --git a/site/src/data/text-to-3d-mech-use.js b/site/src/data/text-to-3d-mech-use.jsnew file mode 100644index 00000000..a61da741--- /dev/null+++ b/site/src/data/text-to-3d-mech-use.js@@ -0,0 +1,41 @@+import * as THREE from 'three';+import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';++const gltf = await new GLTFLoader().loadAsync('mech.glb');+scene.add(gltf.scene);++const mech = gltf.scene.getObjectByName('HeavyWalkerMech');+const mixer = new THREE.AnimationMixer(mech);+mixer.clipAction(THREE.AnimationClip.findByName(gltf.animations, 'Walk')).play();++const { walkSpeedMps } = mech.userData.rig;+const waist = mech.getObjectByName('Joint_Waist');+const barrels = mech.getObjectByName('Joint_CannonSpin_R');+const coils = new Set();+mech.traverse((node) => {+ if (node.material?.name === 'Glow_RailCoil') coils.add(node.material);+});++// Once a frame. Walk plays in place, so your game moves the body.+function update(dt, { aim = 0, spin = 0, charge = 2.2 } = {}) {+ mixer.update(dt);+ waist.rotateY(aim); // Walk sets the waist, so add your aim after the mixer.+ barrels.rotateX(spin * dt); // Walk leaves the barrels, wrist and hatches to you.+ for (const coil of coils) coil.emissiveIntensity = charge;+ gltf.scene.translateX(walkSpeedMps * dt); // +X is forward.+}++// Pose the legs yourself and the pistons need aiming again.+// Each one stores what it follows.+const pistons = [];+mech.traverse((node) => {+ if (node.userData.aimAt) pistons.push(node);+});+function aimPistons() {+ mech.updateMatrixWorld(true);+ for (const piston of pistons) {+ const target = mech.getObjectByName(piston.userData.aimAt).getWorldPosition(new THREE.Vector3());+ const direction = piston.parent.worldToLocal(target).sub(piston.position).normalize();+ piston.quaternion.setFromUnitVectors(new THREE.Vector3().fromArray(piston.userData.aimRestDir), direction);+ }+}diff --git a/site/src/data/text-to-3d.json b/site/src/data/text-to-3d.jsonnew file mode 100644index 00000000..0520deed--- /dev/null+++ b/site/src/data/text-to-3d.json@@ -0,0 +1,281 @@+{+ "tools": {+ "kiln": "1.6.0",+ "harness": "claude",+ "harnessVersion": "2.1.319",+ "model": "claude-opus-5-5",+ "effort": "xhigh"+ },+ "subjects": [+ {+ "id": "mech",+ "name": "Heavy Walker Mech",+ "glbBytes": 4777424,+ "triangles": 68604,+ "steps": [+ {+ "id": "make",+ "prompt": "Make a heavy walking mech for a three.js game, about 6 m tall. Two digitigrade legs, an armoured torso that turns at the waist, a cockpit with a glass canopy, a shoulder-mounted missile pod and two heavy arms: a rotary cannon on the right and a claw on the left. Every joint has to be its own named pivot (hip, knee, ankle, toe, waist, shoulder, elbow) so I can drive it from code, and the hydraulic pistons on the legs should really follow the joints. Add one looping Walk clip where the feet plant without sliding and the torso sways and counter-rotates. It should look like it came out of a AAA concept: layered armour plates with panel lines, weathered paint, hazard stripes, exposed cables and machinery at the joints. Keep the GLB under 5 MB so it loads in a browser. Save it when you're happy with it.",+ "sourceLines": 1430,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/mech-1.kiln.js"+ },+ {+ "id": "direct",+ "prompt": "Good bones. Three notes from me.\n\nThe paint still reads as flat olive with specks on it. I want wear you can believe: chipped paint showing bare metal along plate edges, grime settled into the panel lines, oil and rust streaks running down from the joints and bolts, and lower legs that are darker and dustier than the torso.\n\nThe shoulders, forearms and feet are plain boxes and wedges next to the detail on the legs. Give them layered plates, bevelled edges, bolts and vents, and make the feet read as armoured toes with rubber soles.\n\nThe cannon barrels and joint machinery should read as real gunmetal, with some sharp highlights.\n\nKeep the height, every joint name, the Walk clip and the 5 MB limit, and save it as a new revision of the same asset.",+ "sourceLines": 1763,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/mech-2.kiln.js"+ },+ {+ "id": "revise",+ "prompt": "Swap the left claw for a long rail cannon with glowing coils along the barrel. Keep the Walk clip, every joint name and every other part as they are, and save it as a new revision of the same asset.",+ "sourceLines": 1814,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/mech-3.kiln.js"+ }+ ],+ "images": {+ "final": {+ "width": 1440,+ "height": 1440,+ "alt": "The finished walking mech in three-quarter view: olive armour with chipped edges and hazard stripes, a rotary cannon on its right arm, a long rail cannon with glowing cyan coils on its left, a glass cockpit and a shoulder missile pod, standing on two digitigrade legs with hydraulic pistons.",+ "src": "/text-to-3d/5833329625b2f4ed/mech-final.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/mech-final-480.webp 480w, /text-to-3d/5833329625b2f4ed/mech-final-768.webp 768w, /text-to-3d/5833329625b2f4ed/mech-final-1024.webp 1024w, /text-to-3d/5833329625b2f4ed/mech-final-1440.webp 1440w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/mech-final-480.avif 480w, /text-to-3d/5833329625b2f4ed/mech-final-768.avif 768w, /text-to-3d/5833329625b2f4ed/mech-final-1024.avif 1024w, /text-to-3d/5833329625b2f4ed/mech-final-1440.avif 1440w"+ },+ "paint-before": {+ "width": 1000,+ "height": 1000,+ "alt": "Close view of the shoulder armour on the first revision: flat olive paint with a few specks.",+ "src": "/text-to-3d/5833329625b2f4ed/mech-paint-before.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/mech-paint-before-480.webp 480w, /text-to-3d/5833329625b2f4ed/mech-paint-before-768.webp 768w, /text-to-3d/5833329625b2f4ed/mech-paint-before-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/mech-paint-before-480.avif 480w, /text-to-3d/5833329625b2f4ed/mech-paint-before-768.avif 768w, /text-to-3d/5833329625b2f4ed/mech-paint-before-1000.avif 1000w"+ },+ "paint-after": {+ "width": 1000,+ "height": 1000,+ "alt": "The same shoulder on the second revision: chipped edges down to bare metal, grime in the panel lines, rust streaks, bolts, louvred vents and layered plates.",+ "src": "/text-to-3d/5833329625b2f4ed/mech-paint-after.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/mech-paint-after-480.webp 480w, /text-to-3d/5833329625b2f4ed/mech-paint-after-768.webp 768w, /text-to-3d/5833329625b2f4ed/mech-paint-after-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/mech-paint-after-480.avif 480w, /text-to-3d/5833329625b2f4ed/mech-paint-after-768.avif 768w, /text-to-3d/5833329625b2f4ed/mech-paint-after-1000.avif 1000w"+ },+ "before": {+ "width": 1000,+ "height": 1000,+ "alt": "The mech on revision 2, with a three-fingered claw on its left arm.",+ "src": "/text-to-3d/5833329625b2f4ed/mech-before.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/mech-before-480.webp 480w, /text-to-3d/5833329625b2f4ed/mech-before-768.webp 768w, /text-to-3d/5833329625b2f4ed/mech-before-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/mech-before-480.avif 480w, /text-to-3d/5833329625b2f4ed/mech-before-768.avif 768w, /text-to-3d/5833329625b2f4ed/mech-before-1000.avif 1000w"+ },+ "after": {+ "width": 1000,+ "height": 1000,+ "alt": "The mech on revision 3 from the same camera, with a long rail cannon and glowing coils in place of the claw.",+ "src": "/text-to-3d/5833329625b2f4ed/mech-after.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/mech-after-480.webp 480w, /text-to-3d/5833329625b2f4ed/mech-after-768.webp 768w, /text-to-3d/5833329625b2f4ed/mech-after-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/mech-after-480.avif 480w, /text-to-3d/5833329625b2f4ed/mech-after-768.avif 768w, /text-to-3d/5833329625b2f4ed/mech-after-1000.avif 1000w"+ },+ "make-sheet": {+ "width": 1168,+ "height": 780,+ "alt": "The agent's six-view contact sheet of the first saved revision: front, right, back, left, top and three-quarter, each with a yellow label.",+ "src": "/text-to-3d/5833329625b2f4ed/mech-make-sheet.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.webp 480w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-768.webp 768w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.webp 1024w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.webp 1168w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/mech-make-sheet-480.avif 480w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-768.avif 768w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-1024.avif 1024w, /text-to-3d/5833329625b2f4ed/mech-make-sheet-1168.avif 1168w"+ }+ },+ "glbHref": "https://assets.kilnstudio.tools/text-to-3d/9b1b004c460cc0a9/mech.glb"+ },+ {+ "id": "cathedral",+ "name": "Gothic cathedral",+ "glbBytes": 14948932,+ "triangles": 2635224,+ "steps": [+ {+ "id": "make",+ "prompt": "Build a massive Gothic cathedral, the kind of model that makes a professional 3D artist stop and look. True scale: about 130 m long with twin west towers about 80 m tall. A nave of nine bays with side aisles, transepts, and an apse ringed with radiating chapels. Flying buttresses with crocketed pinnacles on every bay, and gargoyles along the walls. A west front with three deep portals, robed saints in the door jambs, carved tympana over the doors, a gallery of kings, and a great rose window with real stone tracery. The statues have to read as sculpted figures, not blocks. Stained glass in every window, as real art: the rose and the big windows should depict figures and scenes in leaded panes, not just coloured squares. Use real textured PBR materials: weathered limestone with carved detail in the normals, patinated lead roofs, aged oak doors with iron straps, gilded accents. The inside has to hold up too: clustered columns, triforium, clerestory and ribbed vaults, statues in niches and glass that colours the view. Keep the GLB under 15 MB so it can load in a browser. Build it from parameters so the design can change later. Save it when you're happy with it.",+ "sourceLines": 2853,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/cathedral-1.kiln.js"+ },+ {+ "id": "direct",+ "prompt": "Strong bones. The plan, the vaults and the view down the nave are right, and I believe the scale. As art it isn't there yet, and this has to stop a professional 3D artist. I looked at it from the ground outside, up close at the portals and from inside. My notes, most important first.\n\nThe statues look like melted candles. Faces have no real features, hands are mittens, feet are two balls and the heads jam straight into the canopies. The figures in the door arches are thin cut-outs floating on the stone, and the lintel's reliefs are buried so only stray bits poke out. Sculpt them like the real thing at Chartres and Reims: long columnar bodies, deep folds you could put a finger in, real faces with brow, nose, lips, hair and beards, hands that grip what they hold, feet on a carved base and room above the head. Make them different people: a bishop in a mitre, a crowned king, a veiled queen, an apostle with a key, a smiling angel, the Virgin and Child on the central door post. I would rather have fewer figure types that are truly good than many that are poor. Carve the tympana as scenes, with figures that overlap and turn to each other, not a row of people on wallpaper.\n\nThe glass looks like clip art: flat primary colours and cartoon people. Real medieval glass is deep ruby and cobalt with no two panes quite alike, black lead lines, and brown painted shading that draws faces, hair and drapery. The rose should be the most beautiful thing in the building. From outside, glass should read dark and deep with the colour only hinting through. Right now the windows are as bright as toys outside, and some wash out to pale pink at an angle. The colour belongs inside.\n\nThe stone has no age and no depth. Outside it is one clean cream everywhere, so you can't feel how deep the portals are. This building has stood for 700 years: soot in the recesses, rain streaks under every ledge and sill, each block a slightly different colour, darker and greener toward the ground. The viewer has no cast shadows, so bake the light in: a low sun from one side, so the portals go deep, the galleries go dark behind their columns and every ledge throws a shadow below it. The printed quatrefoil pattern on the portal walls and tympana looks like wallpaper, so carve it or remove it. The tower walls are flat ashlar with thin lines stuck on, so give them real blind arcades and relief. Inside, warm the stone, and let it go darker low in the aisles and lighter up in the vault. The coloured light on the floor looks like painted tiles: make it soft pools with the shape of the windows.\n\nMaterials should hold up close: lead roofs with visible sheets, seams and streaked patina instead of stretched noise, stone with tool marks instead of round pockmarks, and gold that looks like gold instead of mustard. The altar's gold box should become a carved retable or go.\n\nFaults I can see. A free-standing column sticks out of the north transept front, between the portal and the big window. The flying buttresses are thick flat boards: make them real arches, thin and pierced, with a sloped top. The bell openings are dark walls with nothing in them: make them real openings with louvres. The arches around the apse look like fangs hanging from above instead of springing from the piers.\n\nKeep the plan, the dimensions and the parameters. Keep the GLB under 15 MB and leave about 1 MB free, because I will want changes later. Textures are only 1.4 MB of the file, so spend your bytes on what gets looked at closely. Save it as a new revision of the same asset.",+ "sourceLines": 3775,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/cathedral-2.kiln.js"+ },+ {+ "id": "direct-2",+ "prompt": "I'm back, and thanks for the honest report. The nave, the vaults and the rose seen from outside are what I wanted, and the stone finally looks old. Here's what comes next, most important first.\n\nA correction before that. I told you the build limit was 120 seconds, and your kiln_save still failed after about a minute. I don't know why yet, so plan on 60. With this machine quiet, your saved revision builds in 32 seconds, so most of those failures were other jobs on the machine rather than your program. You have more room than you thought.\n\nIt has no ground. Apart from a paved patch at the west doors, the building floats. Give it real ground all the way round: worn flagstones for about 20 m, a step or two up to each door, rough grass or packed earth at the edge, and dark grime where the walls meet it. Make the ground its own named part, so it can be taken away if the building is later placed in a town. Keep its top a few centimetres above y = 0, so it doesn't fight with a viewer's ground plane.\n\nThe corners aren't real architecture. At the corners of the towers and transepts, thick wall plates stick out past the corner and overlap each other, and high on the towers some hang in the air with a slanted edge. Around the apse and along the sides, the buttresses are huge plain boxes, and the set-offs on the west front's buttresses are thin sheets that stick out like awnings. Build real buttresses: solid piers tied into the wall, narrower as they rise, stepping back in two or three stages, each step a sloped stone cap that belongs to the pier. Where two meet at a corner they should meet cleanly, or run diagonally as one. From any angle, nothing should float, overlap or pass through anything else.\n\nInside. Fix the light bug you found: the piers on one side are still blue-grey. The shadows on the vaults and piers have hard-edged bands, and the light pools on the floor break into hard-edged islands up close. Make them smooth gradients.\n\nThe portals, up close. The canopies over the door statues float far above their heads on thin sticks, like hanging lamps. Bring each one down to just above its head, with the figure standing against its column. The tympanum figures stand on shelves like dolls, in front of a wall of blocks. Carve them out of the stone as relief, with no block joints behind them. The gold frame around Christ is a thin wire with angels passing through it, so make it a carved frame, gilded. The kneeling figures have pumpkin-shaped skirts. From the ground the gallery kings are faceless slivers, so give them bodies and crowns. The queen and the Virgin need different faces. The door straps end in curls that read as the number 3, so make them forged hinges. Remove the yellow blotches like coins at the foot of the portal walls, and the little L-shaped chips scattered around the rose.\n\nThe glass. From outside, the windows read as flat purple with red specks. Make them nearly black, with faint lead lines and only a hint of colour, the way the rose already does. Inside, the grounds lean magenta, so keep them ruby and cobalt. The high apse windows have a white ladder across the middle: take it out. I still want the rose to be the most beautiful thing in the building, so look at it from inside and make it so.\n\nThe roofs look like a patchwork quilt from above. Make long lead sheets running down the slope, with raised seams and streaks.\n\nThen, if there's room, what your report left open: rain streaks on the apse, chapels and buttress sides, and a shadow under every main ledge.\n\nKeep the plan, the parameters and the height. Keep the GLB under 15 MB and leave at least 1 MB and some build time free, because I'll ask for a change that adds geometry afterwards. Save it as a new revision of the same asset, then tell me plainly what's done and what isn't.",+ "sourceLines": 3960,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/cathedral-3.kiln.js"+ },+ {+ "id": "revise",+ "prompt": "Add two more bays to the nave and raise a lead-covered spire over the crossing, about 100 m to the tip. Leave everything else as it is and save it as a new revision of the same asset.",+ "sourceLines": 4055,+ "geometry": {+ "parent": 1621,+ "inPlace": 971,+ "moved": 639+ },+ "sourceHref": "/text-to-3d/5833329625b2f4ed/cathedral-4.kiln.js"+ },+ {+ "id": "light",+ "prompt": "One fault, from looking inside. The coloured light on the nave floor isn't light. It's painted patches: they lean the wrong way for the sun you baked into the stone, their colours aren't the colours of the glass, and they run straight through the piers as if the piers weren't there.\n\nMake it real, and baked like the rest. Use the same sun as the stone, P.sun, low from the south-west. For each point of the floor, trace back towards the sun. If the ray clears the south arcade without hitting a pier and comes through a south aisle window, the floor takes that window's colour at that height, from its actual panes or a close average of them. Otherwise it takes nothing. So each window throws a long slanted pool that runs north and east across the floor, with the shape of its lancets and tracery softened, and the piers cut clean shadows through it. Keep the edges soft, and keep it subtle enough that the paving still reads through it. If the same rays land on the north piers or walls and you can bake that cheaply, do it. If not, leave it and say so.\n\nLeave everything else as it is: the plan, the height, the stone and the glass. Keep the GLB under 15 MB. You have about 0.3 MB free, so take the budget out of the old patches. Keep the build inside the limit. Save it as a new revision of the same asset, then tell me plainly what's done and what isn't.",+ "sourceLines": 4203,+ "geometry": {+ "parent": 1715,+ "inPlace": 1705,+ "moved": 0+ },+ "sourceHref": "/text-to-3d/5833329625b2f4ed/cathedral-5.kiln.js"+ },+ {+ "id": "sun",+ "prompt": "I looked at the floor light again and it still isn't right. At this sun it runs the whole way across the nave, past the columns and up the north piers, and your own number says almost none of it could get past the buttresses outside.\n\nFix it by moving the sun. Set P.sun to about [0.11, 0.79, -0.61]: about 52 degrees up and about 10 degrees west of due south. I tried a few suns on a scratch copy of your source, with the outside counted. With this one each south aisle window lands as its own window-shaped pool that passes between the piers and stops around the middle of the nave, short of the north piers. Further west, the buttresses and piers cut the pools into thin streaks. Adjust it a little if your own checks say so, and tell me if you do.\n\nCount everything in the trace, including the outside: buttresses, flyers, pinnacles, towers and transepts. No light passes through stone. Tell me what share of the rays reaching a window now clear everything.\n\nStart the floor image at the south aisle wall, because light now lands in the aisle, and end it just past where the last light falls, so the texels go where the light is. Where rays stop on a pier, that's now the south faces of the south piers, so put the pier skins there. The north piers should get nothing.\n\nThe colour reads as pink and lavender with pale blobs. Make it read as ruby and cobalt light falling on stone, with the paving still showing through, and soften the pale patches so they don't look like spots.\n\nRe-bake the stone from this same sun, inside and out, and keep the portals deep. The west front now only gets raking light. Don't add a second sun to brighten it. If it goes dull, tell me.\n\nLeave the plan, the height and the glass as they are. Keep the GLB under 15 MB and the build inside the limit. Save it as a new revision of the same asset, then tell me plainly what's done and what isn't.",+ "sourceLines": 4240,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/cathedral-6.kiln.js"+ },+ {+ "id": "daylight",+ "prompt": "Two things from looking at this revision. The floor is right, so don't change it.\n\nThe light on the south piers isn't right. Up close it reads as dark red and blue paint running up the column, blotchy and darker than the stone around it. Light can't make stone darker. Make it read as light falling on stone: brighter than the stone it lands on, smooth, with soft edges. If the overlay can't do that at any height, fade it out low on the pier, by about 3 m, or take it off and say so. Check it close up from the south aisle, camera at [24, 2, -10.5] looking at [0, 3, -5], as well as from the nave.\n\nThe west front is what people see first, and it has gone dull. Keep the one sun, but shaded walls really do get light from the sky and off the sunlit paving. Lift that on the west front until it reads as soft daylight, with the portals still deep. Check it from [165, 1.6, -115] looking at [5, 66.6, 9].\n\nEverything else stays. Keep the GLB under 15 MB and the build inside the limit. Save it as a new revision, then tell me plainly what's done and what isn't.",+ "sourceLines": 4289,+ "sourceHref": "/text-to-3d/5833329625b2f4ed/cathedral-7.kiln.js"+ }+ ],+ "diff": "--- cathedral-3.kiln.js\n+++ cathedral-4.kiln.js\n@@ -8,7 +8,7 @@\n const P = {\n preview: 'all', // 'all', or 'size' (true buffer bytes, fast), 'stats' (per-mesh list), 'textures' (map payload), 'unique' (no instances), 'statues' (test row)\n // Plan\n- naveBays: 9, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,\n+ naveBays: 11, bay: 6.6, naveSpan: 13, aisleSpan: 6.6,\n xWest: 62, // west face of the towers\n towerDepth: 14, towerWidth: 14,\n choirBays: 3, transeptBays: 2,\n@@ -18,7 +18,7 @@\n vaultSpring: 26.6, vaultCrown: 39.6, eaves: 41.4, roofPitch: 1.6,\n aisleWallTop: 23, aisleRoofHigh: 26.2,\n towerTop: 72, spireTip: 80,\n- flecheTip: 74,\n+ flecheTip: 100, // top of the cross on the lead spire over the crossing\n // Detail\n jambStatues: 5, kings: 16, seed: 1163,\n archFigureTris: 720, statueTest: ['apostle', 'bishop', 'king', 'queen', 'angel', 'virgin'],\n@@ -1528,7 +1528,7 @@\n }\n }\n // Crockets up an edge from p to q (3D), with `dir` the outward direction in the frame\n-function crocketsAlong(b, p, q, n, size, out, lod = 1) {\n+function crocketsAlong(b, p, q, n, size, out, lod = 1, maker = addCrocket) {\n const d = [q[0] - p[0], q[1] - p[1], q[2] - p[2]], L = Math.hypot(...d);\n const dn = d.map((c) => c / L);\n let ox = out[0], oy = out[1], oz = out[2]; const od = ox * dn[0] + oy * dn[1] + oz * dn[2]; ox -= od * dn[0]; oy -= od * dn[1]; oz -= od * dn[2];\n@@ -1538,9 +1538,16 @@\n for (let i = 0; i < n; i++) {\n const t = (i + 0.5) / n, pz = [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)];\n const m = [ox, dn[0], -side[0], pz[0], oy, dn[1], -side[1], pz[1], oz, dn[2], -side[2], pz[2]];\n- addCrocket(b, m, size * lerp(1, 0.78, t), lod);\n+ maker(b, m, size * lerp(1, 0.78, t), lod);\n }\n }\n+// A crocket beaten in lead: one thick leaf turning up off the arris with a lobe either side. Same frame as addCrocket: X out, Y along the edge\n+function addLeadCrocket(b, m, s = 1) {\n+ b.at(m, () => {\n+ b.tube([[-0.04 * s, -0.02 * s, 0], [0.2 * s, 0.1 * s, 0], [0.34 * s, 0.3 * s, 0], [0.3 * s, 0.46 * s, 0]], (t) => [lerp(0.13, 0.015, t) * s, lerp(0.17, 0.02, t) * s], 4, { capEnd: false });\n+ for (const z of [-1, 1]) b.tube([[0, 0, z * 0.04 * s], [0.16 * s, 0.1 * s, z * 0.2 * s], [0.2 * s, 0.24 * s, z * 0.3 * s]], (t) => lerp(0.07, 0.012, t) * s, 3, { capEnd: false });\n+ });\n+}\n \n // Crocketed pinnacle: square shaft with gablets, a spirelet with crockets and a finial.\n function addPinnacle(b, w, h, o = {}) {\n@@ -2665,36 +2672,123 @@\n addCapital(F, ring, P.pierTop - 0.9, 0.9, 1.18, { leaves: 8 });\n return set;\n }\n+// The spire over the crossing: an oak frame sheathed in lead. A plinth straddles the roofs, two open lantern stages stand on it,\n+// and a long octagonal needle rises to a gilded orb and cross. Everything follows P.flecheTip, the ridge and the width of the crossing.\n function buildFleche(parent, x, z) {\n- const L = new Batch(TILE.lead), G = new Batch(TILE.gold), F = new Batch(TILE.fine);\n- const y0 = LAY.ridge - 2.5, yB = LAY.ridge + 7, yT = P.flecheTip - 3.2, R = 2.9, n = 8;\n- // Octagonal base and open arcaded lantern\n- L.lathe([[R + 0.4, y0], [R + 0.25, y0 + 2.8], [R + 0.5, y0 + 3], [R + 0.5, y0 + 3.3], [R, y0 + 3.5]], n, { faceted: true, phase: PI / 8 });\n- L.lathe([[R * 0.78, y0 + 3.5], [R * 0.78, yB + 0.3]], n, { faceted: true, phase: PI / 8, s: 0.34 });\n- for (let i = 0; i < n; i++) {\n- const a = i * 45, ap = R * Math.cos(rad(22.5)), side = 2 * R * Math.sin(rad(22.5));\n- L.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap)), () => {\n- for (const s of [-1, 1]) L.tube([[s * side * 0.5, y0 + 3.5, 0], [s * side * 0.5, yB, 0]], 0.13, 6, {});\n- const arc = pointedArc(side * 0.92, 2.2, 6).map((p) => [p[0], yB - 2.45 + p[1]]);\n- L.sweep(arc, traceryBar(0.13, 0.2), {});\n- G.at(M.of(M.Ry(a + 22.5), M.T(0, 0, ap + 0.1)), () => addGable(G, side * 1.04, 1.6, -0.05, 0.08, yB - 0.24, { size: 0.26, crockets: 2, finial: 0.44, lod: 0, solid: true }));\n+ const L = new Batch(TILE.lead), G = new Batch(TILE.gold);\n+ L.noReduce = true; // a single careful mesh, its seams and crockets are the design\n+ const n = 8, PH = PI / 8, cos8 = Math.cos(PH), sin8 = Math.sin(PH), oct = { faceted: true, phase: PH };\n+ const yR = LAY.ridge, tip = P.flecheTip, y0 = yR - 5.9, yP = yR + 2.7, H = tip - yP, yA = yP + 0.19 * H, yB = yA + 0.15 * H, yN1 = yB + 1.7, yT = tip - 3.3;\n+ const R0 = zn * 0.73, R1 = zn * 0.66, R2 = zn * 0.5, RN = zn * 0.44;\n+ const faceFrame = (k, ap) => M.of(M.Ry(k * 45), M.T(0, 0, ap));\n+ const vertex = (k, R) => { const a = rad((k + 0.5) * 45); return [R * Math.sin(a), R * Math.cos(a)]; };\n+ // A small gilded knop finishing a gablet or a lucarne\n+ const knop = (m, s) => G.at(m, () => G.lathe([[0.05 * s, 0], [0.11 * s, 0.1 * s], [0.09 * s, 0.2 * s], [0.03 * s, 0.32 * s], [0.004, 0.72 * s]], 6));\n+ const gablet = (k, ap, w, h, y, nC, sz) => {\n+ const fr = faceFrame(k, ap + 0.05);\n+ L.at(fr, () => {\n+ L.slab([[-w / 2, y], [w / 2, y], [0, y + h]], [], -0.05, 0.05, { sb: 0.7 });\n+ L.shade(0.42, () => L.face([[-w * 0.2, y + h * 0.16, 0.06], [w * 0.2, y + h * 0.16, 0.06], [0, y + h * 0.62, 0.06]]));\n+ const rake = barSection(0.14, 0.2);\n+ L.sweep([[-w / 2 - 0.05, y - 0.02], [0, y + h + 0.04]], rake, {}); L.sweep([[0, y + h + 0.04], [w / 2 + 0.05, y - 0.02]], rake, {});\n+ crocketsAlong(L, [-w / 2, y + 0.05, 0], [-0.03, y + h * 0.97, 0], nC, sz, [-h, w / 2, 0], 0, addLeadCrocket);\n+ crocketsAlong(L, [w / 2, y + 0.05, 0], [0.03, y + h * 0.97, 0], nC, sz, [h, w / 2, 0], 0, addLeadCrocket);\n });\n- }\n- L.lathe([[R + 0.2, yB - 0.05], [R + 0.34, yB + 0.1], [R + 0.2, yB + 0.26]], n, { faceted: true, phase: PI / 8 });\n- // The spire, its ribs and gilded crockets\n- L.lathe([[R * 0.98, yB + 0.26], [R * 0.72, yB + 3.2], [0.1, yT]], n, { faceted: true, phase: PI / 8 });\n- for (let i = 0; i < n; i++) {\n- const a = rad(i * 45 + 22.5 + 22.5), c = Math.cos(a), s = Math.sin(a);\n- const pts = [[R * 0.98 * c * 1.0, yB + 0.3, R * 0.98 * s], [R * 0.72 * c, yB + 3.2, R * 0.72 * s], [0.14 * c, yT - 0.2, 0.14 * s]];\n- G.tube(pts, (t) => lerp(0.1, 0.03, t), 5);\n- crocketsAlong(G, pts[1], pts[2], 7, 0.66, [c, 0.1, s], 0);\n- }\n- G.at(M.T(0, yT - 0.15, 0), () => {\n- G.lathe([[0.14, 0], [0.3, 0.28], [0.34, 0.5], [0.22, 0.72], [0.06, 0.9], [0.05, 3.2]], 10);\n- G.box(-0.06, 0.06, 2.2, 3.35, -0.06, 0.06, {}); G.box(-0.6, 0.6, 2.56, 2.72, -0.06, 0.06, {});\n+ knop(M.of(fr, M.T(0, y + h - 0.02, 0)), 0.9);\n+ };\n+ // One open stage: a dark core, a pointed opening in every face, a clustered post at every angle, a cornice closing the top\n+ const stage = (R, yBase, yTop, coreR, rPost, open) => {\n+ const hs = R * sin8, ap = R * cos8, { w, sill, ys, rise, tracery } = open;\n+ L.shade(0.34, () => L.lathe([[coreR, yBase], [coreR, yTop - 0.4]], n, oct));\n+ L.shade(0.5, () => { L.lathe([[coreR, yTop - 0.4], [R, yTop - 0.4]], n, oct); L.lathe([[R, yBase + 0.01], [coreR, yBase + 0.01]], n, oct); });\n+ for (let k = 0; k < n; k++) {\n+ L.at(faceFrame(k, ap), () => {\n+ L.slab([[-hs, yBase], [hs, yBase], [hs, yTop], [-hs, yTop]], [archOutline(w, sill, ys, rise, 5)], -0.14, 0.02, { noOuterSides: true, sr: 0.6, sb: 0.5 });\n+ L.sweep(pointedArc(w + 0.16, rise + 0.16, 5).map((p) => [p[0], p[1] + ys]), hexBar(0.1, 0.14), { z: 0.03, caps: false });\n+ if (tracery) {\n+ L.at(M.T(0, 0, -0.06), () => L.lathe([[0.1, sill], [0.065, sill + 0.16], [0.065, ys - 0.08], [0.11, ys]], 5));\n+ for (const s of [-1, 1]) L.sweep(pointedArc(w / 2 - 0.03, rise * 0.58, 3).map((p) => [s * w / 4 + p[0], ys + p[1]]), hexBar(0.08, 0.13), { z: -0.06, caps: false });\n+ L.sweep(ringPts(0, ys + rise * 0.76, rise * 0.13, 6), hexBar(0.06, 0.1), { z: -0.06, closed: true });\n+ }\n+ });\n+ const [px, pz] = vertex(k, R + 0.03);\n+ L.at(M.T(px, 0, pz), () => L.lathe([[rPost * 1.35, yBase], [rPost, yBase + 0.22], [rPost, yTop - 0.3], [rPost * 1.35, yTop - 0.12], [rPost * 1.2, yTop]], 6));\n+ }\n+ L.lathe([[R + 0.02, yTop - 0.5], [R + 0.22, yTop - 0.3], [R + 0.22, yTop - 0.12], [R + 0.08, yTop]], n, oct);\n+ };\n+ // A raised roll standing on a sloping face, in the face's own frame: two faces, a low ridge\n+ const roll = (u, ya, yb, apF, h = 0.07, w = 0.05) => {\n+ const za = apF(ya), zb = apF(yb), dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz) || 1, ny = -dz / l, nz = dy / l;\n+ const A0 = [u - w, ya, za], B0 = [u + w, ya, za], A1 = [u - w, yb, zb], B1 = [u + w, yb, zb], T0 = [u, ya + ny * h, za + nz * h], T1 = [u, yb + ny * h, zb + nz * h];\n+ L.face([A0, T0, T1, A1]); L.face([T0, B0, B1, T1]);\n+ };\n+\n+ // ---- The plinth: batters up out of the slates of all four roofs, its foot well below their surface all round ----\n+ L.lathe([[R0, y0], [R0 - 0.16, yP - 0.62], [R0 + 0.12, yP - 0.46], [R0 + 0.12, yP - 0.2], [R0 - 0.2, yP], [R1 * 0.6, yP]], n, oct);\n+ { const apF = (y) => lerp(R0, R0 - 0.16, (y - y0) / (yP - 0.62 - y0)) * cos8;\n+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => { for (const u of [-1.2, 0, 1.2]) roll(u, y0, yP - 0.62, apF); }); }\n+\n+ // ---- First stage: tall two-light openings, a gablet on every face, a pinnacle at every angle, a lead pentice behind them ----\n+ stage(R1, yP, yA, R1 * 0.6, 0.26, { w: R1 * 0.56, sill: yP + 0.95, ys: yA - 3.35, rise: 2, tracery: true });\n+ L.lathe([[R1 + 0.08, yA], [R2 + 0.02, yA + 1.55]], n, oct);\n+ for (let k = 0; k < n; k++) {\n+ gablet(k, R1 * cos8, R1 * 0.67, 2.6, yA - 0.3, 2, 0.34);\n+ const [px, pz] = vertex(k, R1 + 0.02);\n+ L.at(M.of(M.T(px, yA - 0.2, pz), M.Ry((k + 0.5) * 45)), () => addPinnacle(L, 0.62, 5, { simple: true, lod: 0 }));\n+ }\n+ // ---- Second stage: slimmer, single lancets ----\n+ stage(R2, yA + 1.5, yB, R2 * 0.62, 0.18, { w: R2 * 0.53, sill: yA + 2.45, ys: yB - 2.3, rise: 1.5, tracery: false });\n+ for (let k = 0; k < n; k++) gablet(k, R2 * cos8, R2 * 0.65, 1.9, yB - 0.25, 1, 0.3);\n+\n+ // ---- The needle: a flared foot, then eight long faces. u stays metric, so the sheets run straight up and are cut by the arrises as they close ----\n+ const rTip = 0.09, RF = R2 + 0.08, rAt = (y) => (y <= yN1 ? lerp(RF, RN, (y - yB) / (yN1 - yB)) : lerp(RN, rTip, (y - yN1) / (yT - yN1)));\n+ const levels = [yB, yN1]; for (const t of [0.22, 0.5, 0.75, 1]) levels.push(lerp(yN1, yT, t));\n+ const apAt = (y) => rAt(y) * cos8;\n+ for (let k = 0; k < n; k++) L.at(faceFrame(k, 0), () => {\n+ let vv = 0;\n+ for (let j = 0; j < levels.length - 1; j++) {\n+ const ya = levels[j], yb = levels[j + 1], za = apAt(ya), zb = apAt(yb), wa = rAt(ya) * sin8, wb = rAt(yb) * sin8, dy = yb - ya, dz = zb - za, l = Math.hypot(dy, dz), ny = -dz / l, nz = dy / l;\n+ const a = L.v(-wa, ya, za, 0, ny, nz, -wa, vv), b = L.v(wa, ya, za, 0, ny, nz, wa, vv), c = L.v(wb, yb, zb, 0, ny, nz, wb, vv + l), d = L.v(-wb, yb, zb, 0, ny, nz, -wb, vv + l);\n+ L.quad(a, b, c, d); vv += l;\n+ }\n+ // Standing seams, each stopping where the closing arrises overtake it\n+ for (const u of [-0.55, 0, 0.55]) { const rEnd = (Math.abs(u) + 0.1) / sin8; if (rEnd < RN) roll(u, yN1, lerp(yN1, yT, (RN - rEnd) / (RN - rTip)), apAt); }\n+ });\n+ // Rolled arrises from the flare to the tip, crockets thinning as they climb\n+ for (let k = 0; k < n; k++) {\n+ const vAt = (y) => { const [vx, vz] = vertex(k, rAt(y)); return [vx, y, vz]; };\n+ L.tube([vAt(yB), vAt(yN1), vAt(yT - 0.05)], (t) => lerp(0.12, 0.04, t), 4, { capEnd: false });\n+ const [ox, oz] = vertex(k, 1), nC = 11, y1 = yN1 + 1.2, y2 = yT - 1.5;\n+ for (let i = 0; i < nC; i++) {\n+ const t = (i + 0.5) / nC, ya = lerp(y1, y2, t - 0.02), yb = lerp(y1, y2, t + 0.02);\n+ crocketsAlong(L, vAt(ya), vAt(yb), 1, lerp(1.2, 0.42, t), [ox, 0.12, oz], 0, addLeadCrocket);\n+ }\n+ }\n+ // Tie-bands round the needle where the sheets are lapped\n+ for (const t of [0.22, 0.5, 0.75]) { const y = lerp(yN1, yT, t), r = rAt(y); L.lathe([[rAt(y - 0.1), y - 0.1], [r + 0.08, y - 0.05], [r + 0.08, y + 0.05], [rAt(y + 0.12), y + 0.12]], n, oct); }\n+ // Two tiers of lucarnes on alternate faces\n+ const lucarne = (k, yD, a, c, g) => {\n+ const zf = apAt(yD) + 0.06, yE2 = yD + c, yG = yE2 + g, zb0 = apAt(yD), zbE = apAt(yE2), zbG = apAt(yG);\n+ L.at(faceFrame(k, 0), () => {\n+ L.face([[-a, yD, zf], [a, yD, zf], [a, yE2, zf], [0, yG, zf], [-a, yE2, zf]]);\n+ L.shade(0.22, () => L.face([[-a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yD + 0.16, zf + 0.012], [a * 0.5, yE2, zf + 0.012], [0, yE2 + g * 0.55, zf + 0.012], [-a * 0.5, yE2, zf + 0.012]]));\n+ L.face([[a, yD, zb0], [a, yE2, zbE], [a, yE2, zf], [a, yD, zf]]);\n+ L.face([[-a, yD, zb0], [-a, yD, zf], [-a, yE2, zf], [-a, yE2, zbE]]);\n+ L.face([[a, yE2, zf], [a, yE2, zbE], [0, yG, zbG], [0, yG, zf]]);\n+ L.face([[-a, yE2, zf], [0, yG, zf], [0, yG, zbG], [-a, yE2, zbE]]);\n+ });\n+ knop(M.of(faceFrame(k, zf), M.T(0, yG - 0.02, 0)), 0.8);\n+ };\n+ for (let k = 0; k < n; k++) { if (k % 2 === 0) lucarne(k, yN1 + 5, 0.5, 1.25, 0.75); else lucarne(k, yN1 + 12, 0.36, 0.95, 0.55); }\n+\n+ // ---- The tip: a gilded collar, an orb and a cross, its head at exactly P.flecheTip ----\n+ G.at(M.T(0, yT - 0.25, 0), () => {\n+ G.lathe([[0.12, 0], [0.24, 0.12], [0.22, 0.22], [0.13, 0.3], [0.11, 0.44], [0.2, 0.52], [0.36, 0.68], [0.42, 0.9], [0.36, 1.12], [0.2, 1.28], [0.08, 1.36], [0.06, 2.2]], 10);\n+ G.box(-0.07, 0.07, 2.2, 3.55, -0.07, 0.07, {});\n+ G.box(-0.62, 0.62, 2.82, 2.98, -0.07, 0.07, {});\n });\n- place('Fleche_Lead', L, MAT.lead, { pos: [x, 0, z], parent });\n- place('Fleche_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });\n+ place('CrossingSpire_Lead', L, MAT.lead, { pos: [x, 0, z], parent });\n+ place('CrossingSpire_Gilt', G, MAT.gold, { pos: [x, 0, z], parent });\n }\n \n // Coloured light thrown onto the paving by the south aisle windows (sun from the south-west)\n@@ -3673,7 +3767,7 @@\n for (const e of PLACED) uses.set(e.batch, (uses.get(e.batch) || 0) + 1);\n for (const e of PLACED) {\n const m = e.mat;\n- if (e.batch.isFigure || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.glassOutWin || m === MAT.glassRose || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave || m === MAT.flag || m === MAT.earth) continue;\n+ if (e.batch.isFigure || e.batch.noReduce || m.transparent || m === MAT.glassIn || m === MAT.glassOut || m === MAT.glassOutWin || m === MAT.glassRose || m === MAT.ashlar || m === MAT.oak || m === MAT.iron || m === MAT.pave || m === MAT.flag || m === MAT.earth) continue;\n let nb = seen.get(e.batch);\n if (nb === undefined) {\n const tris = e.batch.I.length / 3, s = uses.get(e.batch) >= 8 ? repeatedShare : share; // a mesh repeated down the whole building costs the most\n@@ -3700,7 +3794,8 @@\n }\n function bakeLight() {\n ROOT.updateMatrixWorld(true);\n- const CELL = 0.4, X0 = -72, Y0 = -2, Z0 = -36, NX = 365, NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;\n+ // The world follows the plan: from beyond the chapels to beyond the west front. A spire tip above the top simply casts no shadow up there\n+ const CELL = 0.4, X0 = LAY.xa - 27.8, Y0 = -2, Z0 = -36, NX = Math.ceil((P.xWest + 12 - X0) / CELL), NY = 220, NZ = 180, CK = 8, CX = (NX >> 3) + 1, CY = (NY >> 3) + 1, CZ = (NZ >> 3) + 1;\n const grid = new Uint8Array(NX * NY * NZ), coarse = new Uint8Array(CX * CY * CZ);\n const SUN = (() => { const v = P.sun, l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; })();\n const SUN2 = [[SUN[0] + 0.06, SUN[1], SUN[2] + 0.05], [SUN[0] - 0.05, SUN[1] + 0.05, SUN[2] - 0.05]].map((v) => { const l = len3(v[0], v[1], v[2]); return [v[0] / l, v[1] / l, v[2] / l]; });\n",+ "images": {+ "art-1": {+ "width": 1000,+ "height": 1000,+ "alt": "The central west portal in revision 1: clean pale stone, smooth melted-looking statues in the door jambs and flat figures in the arches.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-art-1.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-art-1-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-art-1-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-art-1-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-art-1-1000.avif 1000w"+ },+ "art-2": {+ "width": 1000,+ "height": 1000,+ "alt": "The same portal in revision 2, from the same camera: weathered stone with soot and rain streaks, sculpted figures in the jambs and arches, and canopies floating high above the statues.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-art-2.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-art-2-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-art-2-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-art-2-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-art-2-1000.avif 1000w"+ },+ "west-2": {+ "width": 1000,+ "height": 1000,+ "alt": "The west front in revision 2 from the ground, floating with no ground around it, with flat wall plates at the tower corners and thin sheets sticking out over the portals.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-west-2.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-west-2-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-west-2-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-west-2-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-west-2-1000.avif 1000w"+ },+ "west-3": {+ "width": 1000,+ "height": 1000,+ "alt": "The same view in revision 3: paved ground, buttresses that step back as they rise with sloped caps, and diagonal piers at the corners.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-west-3.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-west-3-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-west-3-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-west-3-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-west-3-1000.avif 1000w"+ },+ "before": {+ "width": 1000,+ "height": 1000,+ "alt": "The whole south side of the cathedral in revision 3, with its low flèche over the crossing.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-before.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-before-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-before-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-before-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-before-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-before-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-before-1000.avif 1000w"+ },+ "after": {+ "width": 1000,+ "height": 1000,+ "alt": "The same view after the revision: a longer nave and a tall lead spire over the crossing.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-after.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-after-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-after-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-after-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-after-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-after-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-after-1000.avif 1000w"+ },+ "light-before": {+ "width": 1000,+ "height": 1000,+ "alt": "The nave floor before the light revision: flat painted bands of pink and blue laid across the paving, running under the piers.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-light-before.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-light-before-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-light-before-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-light-before-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-light-before-1000.avif 1000w"+ },+ "light-after": {+ "width": 1000,+ "height": 1000,+ "alt": "The nave floor after the light revision: soft slanted lanes of red and blue light from the south windows, with the shadows of the piers cut through them and colour climbing the feet of the north piers.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-light-after.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-light-after-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-light-after-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-light-after-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-light-after-1000.avif 1000w"+ },+ "hero": {+ "width": 1600,+ "height": 1067,+ "alt": "A Gothic cathedral seen from the ground at its south-west corner: twin west towers about 80 m tall, three deep portals, a rose window, flying buttresses and a lead spire over the crossing. In front of the south tower stands a 6 m walking mech in olive armour, small against the building, with a rotary cannon and a rail cannon with glowing coils.",+ "caption": "Both models below, placed together for this picture at their true sizes: a 6 m mech in front of a 100 m spire.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-hero.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-hero-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-hero-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-hero-1024.webp 1024w, /text-to-3d/5833329625b2f4ed/cathedral-hero-1440.webp 1440w, /text-to-3d/5833329625b2f4ed/cathedral-hero-1440.webp 1440w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-hero-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-hero-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-hero-1024.avif 1024w, /text-to-3d/5833329625b2f4ed/cathedral-hero-1440.avif 1440w, /text-to-3d/5833329625b2f4ed/cathedral-hero-1440.avif 1440w"+ },+ "final": {+ "width": 1440,+ "height": 1440,+ "alt": "The finished Gothic cathedral from the ground at the south-west: twin west towers, three deep portals, a rose window, stepped buttresses, flying buttresses and a lead spire over the crossing, in weathered stone with the sun baked in.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-final.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-final-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-final-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-final-1024.webp 1024w, /text-to-3d/5833329625b2f4ed/cathedral-final-1440.webp 1440w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-final-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-final-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-final-1024.avif 1024w, /text-to-3d/5833329625b2f4ed/cathedral-final-1440.avif 1440w"+ },+ "nave": {+ "width": 1600,+ "height": 1200,+ "alt": "The view down the cathedral nave from the floor: clustered piers, ribbed vaults, statues on the piers, stained glass in the apse, and ruby and cobalt light from the south windows lying on the paving between the piers on the right.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-nave.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-nave-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-nave-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-nave-1024.webp 1024w, /text-to-3d/5833329625b2f4ed/cathedral-nave-1440.webp 1440w, /text-to-3d/5833329625b2f4ed/cathedral-nave-1440.webp 1440w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-nave-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-nave-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-nave-1024.avif 1024w, /text-to-3d/5833329625b2f4ed/cathedral-nave-1440.avif 1440w, /text-to-3d/5833329625b2f4ed/cathedral-nave-1440.avif 1440w"+ },+ "sun-before": {+ "width": 1000,+ "height": 1000,+ "alt": "The nave floor from above the west end in revision 5: long slanted bands of coloured light run the whole way across the nave and onto the far piers.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-sun-before.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-sun-before-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-sun-before-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-sun-before-1000.avif 1000w"+ },+ "sun-after": {+ "width": 1000,+ "height": 1000,+ "alt": "The same view in revision 6: each south window lands as its own ruby or cobalt pool, passing between the piers and stopping around the middle of the nave.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-sun-after.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-sun-after-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-sun-after-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-sun-after-1000.avif 1000w"+ },+ "daylight-before": {+ "width": 1000,+ "height": 1000,+ "alt": "The west front in revision 6 from the ground: with the sun now high and to the south, the front is dull and flat.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-daylight-before.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-daylight-before-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-before-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-before-1000.avif 1000w"+ },+ "daylight-after": {+ "width": 1000,+ "height": 1000,+ "alt": "The same view in revision 7: the west front reads as soft daylight, paler and clearer, with the portals still deep.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-daylight-after.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.webp 1000w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-daylight-after-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-after-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-daylight-after-1000.avif 1000w"+ },+ "first-look": {+ "width": 1812,+ "height": 1812,+ "alt": "Four labelled views the agent rendered of its first revision: the rose window in flat primary colours, the pale west front, the view down the nave with blocky coloured light on the floor, and the east end.",+ "src": "/text-to-3d/5833329625b2f4ed/cathedral-first-look.webp",+ "srcsetWebp": "/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.webp 480w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-768.webp 768w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.webp 1024w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.webp 1440w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.webp 1440w",+ "srcsetAvif": "/text-to-3d/5833329625b2f4ed/cathedral-first-look-480.avif 480w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-768.avif 768w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-1024.avif 1024w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.avif 1440w, /text-to-3d/5833329625b2f4ed/cathedral-first-look-1440.avif 1440w"+ }+ },+ "glbHref": "https://assets.kilnstudio.tools/text-to-3d/6f1ee50efb3aa2ab/cathedral.glb"+ }+ ]+}diff --git a/site/src/layouts/BaseLayout.astro b/site/src/layouts/BaseLayout.astroindex 66f2d25c..49b20c48 100644--- a/site/src/layouts/BaseLayout.astro+++ b/site/src/layouts/BaseLayout.astro@@ -266,6 +266,9 @@ const structured = [<a class="text-link" href="/packs/">Kiln Commons</a>+ <a class="text-link" href="/text-to-3d/">+ Text to 3D+ </a><a class="text-link" href="/llms.txt">llms.txt</a>diff --git a/site/src/lib/social-cards.mjs b/site/src/lib/social-cards.mjsindex 2d079c6a..de6768be 100644--- a/site/src/lib/social-cards.mjs+++ b/site/src/lib/social-cards.mjs@@ -17,7 +17,7 @@ const poster = (image) => (image ? { src: image.src, alt: image.alt } : undefine* Every card the build draws, from the parsed catalog files; the archive list is `public/assets/index.json`, or an empty* list when the archive has not been built.*/-export function socialCards({ packsEnabled, release, farm, vehicles, bridge, hero, foundryFloor, archive = [] }) {+export function socialCards({ packsEnabled, release, farm, vehicles, bridge, hero, foundryFloor, archive = [], textTo3d }) {const version = release.version.replace(/\.0$/, '');const heroPoster = packsEnabled ? poster(hero.poster) : undefined;const cards = [@@ -31,6 +31,8 @@ export function socialCards({ packsEnabled, release, farm, vehicles, bridge, her{ slug: 'gallery', title: 'Read the source. Inspect the asset.', note: 'The Kiln gallery', poster: heroPoster },{ slug: 'archive', title: 'Earlier Kiln examples.', note: 'Unreviewed historical examples' },];+ // Its picture is committed with the page, so the card is the same with or without Commons packs.+ if (textTo3d) cards.push({ slug: 'text-to-3d', title: 'Text to 3D with your coding agent.', note: 'A walking mech and a cathedral, prompt to GLB', poster: poster(textTo3d) });if (packsEnabled) {for (const asset of [...farm.assets, ...vehicles.assets, bridge]) {cards.push({ slug: asset.slug, title: asset.name, note: asset.pack ? `${PACK_NAMES[asset.pack]} · Kiln Commons` : 'Standalone · Kiln Commons', poster: poster(asset.poster) });diff --git a/site/src/lib/social.ts b/site/src/lib/social.tsindex 47d1135f..8b67edd6 100644--- a/site/src/lib/social.ts+++ b/site/src/lib/social.ts@@ -6,6 +6,7 @@ import { featuredWorlds, worldCover } from './site-curation';import { farm, vehicles, bridge } from './catalog';import { PACKS_ENABLED, SITE } from './config';import { SOCIAL_CARD, socialCardAlt, socialCardPath, socialCards } from './social-cards.mjs';+import textTo3d from '../data/text-to-3d-card.json';/** The cards `scripts/build-site-media.mjs` drew for this build, from the same inputs (it runs before the Astro build). */const archiveIndex = resolve('public/assets/index.json');@@ -19,6 +20,7 @@ const cards = new Map(hero: { poster: worldCover(featuredWorlds[0]!.id)! },foundryFloor,archive: existsSync(archiveIndex) ? JSON.parse(readFileSync(archiveIndex, 'utf8')) : [],+ textTo3d,}).map((card) => [card.slug, card]),);diff --git a/site/src/lib/text-to-3d.ts b/site/src/lib/text-to-3d.tsnew file mode 100644index 00000000..8d319a9a--- /dev/null+++ b/site/src/lib/text-to-3d.ts@@ -0,0 +1,41 @@+import type { SavedView } from '../components/viewer-framing';+import type { CatalogImage } from './catalog';++export interface TextTo3dStep {+ id: string;+ prompt: string;+ sourceHref: string;+ sourceLines: number;+ /** On a revision the page gives numbers for: its parent's meshes that kept their shape, matched by vertex positions and place. */+ geometry?: { parent: number; inPlace: number; moved: number };+}+export interface TextTo3dSubject {+ id: string;+ name: string;+ glbHref: string;+ glbBytes: number;+ triangles: number;+ steps: TextTo3dStep[];+ diff?: string;+ images: Record<string, CatalogImage & { caption?: string }>;+}++/** A real slice of the saved program, from one piece of text to the end of another. It fails the build if the program no longer holds them. */+export function sourceExcerpt(source: string, from: string, to: string): string {+ const start = source.indexOf(from);+ if (start < 0) throw new Error(`The program has no "${from}"`);+ const end = source.indexOf(to, start);+ if (end < 0) throw new Error(`The program has no "${to}" after "${from}"`);+ return source.slice(start, end + to.length);+}++export const wordCount = (text: string): number => text.trim().split(/\s+/).length;++/** Stands where the nave still was taken. The viewer's lens is narrower, so it aims lower to keep the floor light in frame. */+export const LOOK_INSIDE: SavedView = {+ label: 'Look inside',+ position: [46, 2, 0],+ target: [-40, 12, 0],+};++export const megabytes = (value: number): string => (value / 1_000_000).toFixed(1);diff --git a/site/src/pages/gallery/[slug].astro b/site/src/pages/gallery/[slug].astroindex e4c99146..997ff826 100644--- a/site/src/pages/gallery/[slug].astro+++ b/site/src/pages/gallery/[slug].astro@@ -1,6 +1,7 @@---import GalleryTags from "../../components/GalleryTags.astro";import AssetDownloads from '../../components/AssetDownloads.astro';+import AssetTierDownloads from '../../components/AssetTierDownloads.astro';import { assetDelivery } from '../../lib/delivery';import PageHeader from "../../components/PageHeader.astro";import StatRow from "../../components/StatRow.astro";@@ -118,6 +119,13 @@ const tierNotes: Record<string, string> = {web: "The 3D preview uses this lighter version, with reduced segment counts and paired thin suspender ropes.",far: "A distance version with single suspender ropes; it has no under-deck framing and no expansion joints.",};+// One bridge at three levels of detail: the full one is what most people came for, and the lighter two wait in one closed row.+const [fullTier, ...otherTiers] = isBridge+ ? bridge.tiers.map((tier) => {+ const { runtimeDownload, editableDownload } = assetDelivery('golden-gate-bridge', tier.tier === 'full' ? 'golden-gate-bridge' : `golden-gate-bridge-${tier.tier}`);+ return { tier: tier.tier, note: tierNotes[tier.tier]!, runtime: runtimeDownload, editable: editableDownload };+ })+ : [];const description = `${asset.description.split(/(?<=\.)\s/)[0]} ${number(asset.metrics.triangles)} triangles. Source, GLB and revision history.`.replace(/^(.{0,154})\s[\s\S]*$/, '$1');const schema = {"@context": "https://schema.org",@@ -209,7 +217,6 @@ const schema = {The 3D view applies the same rig ({REVIEW_RIG.id}): its light directionsand strengths, exposure, tone mapping and backdrop.</p>- <EngineNote vehicle={Boolean(vehicle)} /><StatRow values={[{ label: vehicle ? "Triangles · LOD0 + wheels" : "Triangles", value: number(asset.metrics.triangles) }, { label: "Meshes drawn", value: asset.metrics.meshes }, { label: "Materials", value: asset.metrics.materials }]} /><dl class="grid grid-cols-1 gap-5 border-b border-rule py-6 md:grid-cols-2"><div>@@ -225,6 +232,20 @@ const schema = {</dd></div></dl>+ <section class="pt-12" aria-labelledby="downloads">+ <h2 id="downloads">Download the assets</h2>+ {isBridge ? (+ <div class="mt-6">+ <AssetTierDownloads {...fullTier!} />+ <details class="mt-6 border-y border-rule">+ <summary class="font-semibold">Other detail levels</summary>+ <div class="space-y-8 px-4 pb-6">{otherTiers.map((tier) => <AssetTierDownloads {...tier} />)}</div>+ </details>+ </div>+ ) : <AssetDownloads runtime={downloads.runtimeDownload} editable={downloads.editableDownload} />}+ <p class="mt-6 max-w-reading text-dim" data-verbatim={isBridge ? undefined : ""}>{isBridge ? bridge.licenseScope : vehicle ? vehicles.licenseScope : farm.licenseScope}</p>+ <EngineNote vehicle={Boolean(vehicle)} />+ </section>{vehicle && <VehicleTiers asset={vehicle} />}<section class="grid gap-8 pt-12 lg:grid-cols-12" aria-labelledby="review"><div class="lg:col-span-4">@@ -273,7 +294,10 @@ const schema = {</details></section><section class="pt-16" aria-labelledby="history">- <h2 id="history">Follow the saved revisions.</h2>+ <details>+ <summary class="p-0 marker:text-2xl">+ <h2 id="history" class="inline">Follow the saved revisions.</h2>+ </summary><p class="mt-4 max-w-reading">Originally authored by {asset.provenance.originalModel}.{" "}{asset.provenance.refinementModels.length@@ -428,6 +452,7 @@ const schema = {Displayed revision’s parent: {asset.parentRevision}</p>)}+ </details></section>{isBridge && (<section class="pt-16" aria-labelledby="reference">@@ -473,13 +498,5 @@ const schema = {</a></section>)}- <section class="pt-16" aria-labelledby="downloads">- <h2 id="downloads">Download the assets</h2>- {isBridge ? <div class="mt-6 space-y-8">{bridge.tiers.map(tier => {- const tierDownloads=assetDelivery('golden-gate-bridge', tier.tier === 'full' ? 'golden-gate-bridge' : 'golden-gate-bridge-'+tier.tier);- return <article><h3 class="text-title capitalize">{tier.tier} detail</h3><p class="mt-2 text-dim">{tierNotes[tier.tier]}</p><AssetDownloads runtime={tierDownloads.runtimeDownload} editable={tierDownloads.editableDownload} /></article>;- })}</div> : <AssetDownloads runtime={downloads.runtimeDownload} editable={downloads.editableDownload} />}- <p class="mt-6 max-w-reading text-dim" data-verbatim={isBridge ? undefined : ""}>{isBridge ? bridge.licenseScope : vehicle ? vehicles.licenseScope : farm.licenseScope}</p>- </section></div></BaseLayout>diff --git a/site/src/pages/gallery/index.astro b/site/src/pages/gallery/index.astroindex f7b73e5b..62eb5bef 100644--- a/site/src/pages/gallery/index.astro+++ b/site/src/pages/gallery/index.astro@@ -24,6 +24,7 @@ const categories = galleryCategories.filter((category) => assets.some((asset) =><a href="/gallery/archive/" class="text-link mt-3">Browse the earlier archive <span aria-hidden="true">→</span></a>+ <p class="mt-3 max-w-reading text-dim">To make your own, see <a class="text-link" href="/text-to-3d/">text to 3D with your coding agent</a>.</p></div></div>{assets.length > 0 ? (diff --git a/site/src/pages/index.astro b/site/src/pages/index.astroindex bb771ece..021dafbf 100644--- a/site/src/pages/index.astro+++ b/site/src/pages/index.astro@@ -119,7 +119,7 @@ const CARD_SIZES = "(max-width: 767px) calc(100vw - 32px), (max-width: 1279px) c<div class="drawing-sheet"><div class="grid gap-2 px-5 pt-6 md:px-8 md:pt-8 xl:grid-cols-2 xl:items-center xl:gap-12 xl:pt-4"><div class="py-4 md:py-6 xl:py-12">- <p class="mb-4 inline-block border border-rule px-2 py-1 font-mono text-caption text-dim">text-to-3d</p>+ <p class="mb-4"><a href="/text-to-3d/" class="inline-block border border-rule px-2 py-1 font-mono text-caption text-dim underline decoration-rule">text-to-3d</a></p><PageHeader class="max-w-lead">Build and revise 3D assets with your coding agent.</PageHeader>diff --git a/site/src/pages/text-to-3d.astro b/site/src/pages/text-to-3d.astronew file mode 100644index 00000000..8dc3f6a1--- /dev/null+++ b/site/src/pages/text-to-3d.astro@@ -0,0 +1,227 @@+---+import { readFile } from "node:fs/promises";+import { resolve } from "node:path";+import BaseLayout from "../layouts/BaseLayout.astro";+import PageHeader from "../components/PageHeader.astro";+import CodeBlock from "../components/CodeBlock.astro";+import ResponsiveImage from "../components/ResponsiveImage.astro";+import TextTo3dRun from "../components/TextTo3dRun.astro";+import { HARNESSES, INSTALL_COMMAND, REPO, SITE, VERSION, VERSION_SHORT } from "../lib/config";+import { harnessWithVersion, modelName } from "../lib/attribution";+import { number } from "../lib/catalog";+import { holodeckWorldUrl } from "../lib/holodeck";+import { featuredWorlds } from "../lib/site-curation";+import { LOOK_INSIDE, sourceExcerpt, type TextTo3dSubject } from "../lib/text-to-3d";+import record from "../data/text-to-3d.json";+import mechUse from "../data/text-to-3d-mech-use.js?raw";++const subjects = record.subjects as unknown as TextTo3dSubject[];+const { tools } = record;+const mech = subjects.find((subject) => subject.id === "mech")!;+const cathedral = subjects.find((subject) => subject.id === "cathedral")!;+const hero = cathedral.images.hero!;+const geometry = cathedral.steps.find((step) => step.id === "revise")!.geometry!;+const lightGeometry = cathedral.steps.find((step) => step.id === "light")!.geometry!;+const finalProgram = (subject: TextTo3dSubject) => readFile(resolve("public", subject.steps.at(-1)!.sourceHref.slice(1)), "utf8");+const [mechProgram, cathedralProgram] = await Promise.all([finalProgram(mech), finalProgram(cathedral)]);+const leadWorld = featuredWorlds[0]!;+// Said once, near the top of the cathedral's section. When the polished revision lands, delete this and its prop.+const POLISH_NOTE = "The cathedral isn't fully polished yet. A better revision will replace this one here.";+const title = "Text to 3D with your coding agent";+const description = "Text to 3D through Claude Code, Codex or another coding agent: it writes a program, Kiln runs it over MCP, and you keep a GLB with editable source.";+const url = new URL("/text-to-3d/", SITE).href;+const absolute = (path: string) => new URL(path, SITE).href;+const model3d = (subject: TextTo3dSubject, summary: string) => ({+ "@type": "3DModel",+ name: subject.name,+ description: summary,+ license: "https://creativecommons.org/publicdomain/zero/1.0/",+ url: `${url}#${subject.id}`,+ image: absolute(subject.images.final!.src),+ encoding: {+ "@type": "MediaObject",+ contentUrl: subject.glbHref,+ encodingFormat: "model/gltf-binary",+ contentSize: String(subject.glbBytes),+ },+});+const schema = {+ "@context": "https://schema.org",+ "@graph": [+ {+ "@type": "TechArticle",+ headline: title,+ description,+ url,+ image: absolute(hero.src),+ datePublished: "2026-11-07",+ dateModified: "2026-11-07",+ author: { "@type": "Organization", name: "Instrukt Labs", url: "https://github.com/instruktlabs" },+ about: { "@type": "SoftwareSourceCode", name: "Kiln", codeRepository: REPO, version: VERSION },+ },+ model3d(mech, "A 6 m walking mech with named joints, pistons that follow them and a Walk clip, written as a JavaScript program by a coding agent using Kiln."),+ model3d(cathedral, "A Gothic cathedral about 144 m long, with towers about 80 m tall and a 100 m lead spire, written as one JavaScript program by a coding agent using Kiln."),+ ],+};+const HERO_SIZES = "(max-width: 1279px) calc(100vw - 2rem), 60vw";+const installCommand = `mkdir kiln-install\ncd kiln-install\nnpm init -y\n${INSTALL_COMMAND}\nnpx --offline --no -- kiln-init ../my-assets --harness claude\ncd ../my-assets\nclaude`;+const how = [+ ["You describe it", "In your own words, with the numbers and part names you care about, or from a photo, sketch or concept art."],+ ["Your agent writes a program", "JavaScript against Kiln's geometry, material and animation helpers. Nine bays means nine bays, and named parts and pivots are there for your code to find."],+ ["Kiln shows it what it built", "Rendered views, part paths and measurements go back to the agent, which edits the source."],+ ["You keep the source and the GLB", "Both stay in your workspace, on your disk. A later change is an edit to that program, with a diff."],+];+const questions = [+ ["Can Claude Code make 3D models?", "Yes, given a tool that lets it build a model and look at the result. Kiln gives it that, over MCP or the CLI."],+ ["What can it make?", "Anything an agent can build from parts: buildings, vehicles, machines, props and rigged things that move. Kiln is working towards creatures."],+ ["Does it work with Codex, Cursor and other agents?", `kiln-init ships setups for ${HARNESSES.length} coding agents, including both of those. Results vary by agent and model.`],+ ["Can it work from a photo, sketch or concept art?", "Yes, as a reference. Hand your agent the image and it uses it for silhouette, materials and art direction, then builds the model as a program rather than tracing the picture."],+ ["Does it need an API key?", "Kiln needs no key or account of its own. Your agent signs in the way it already does."],+ ["Can I use the result in a game or in Blender?", "The output is a standard GLB, and the mech's three.js code above drives the file you download.", { href: "/docs/engine-handoff/", label: "The Blender and Unity guide" }],+] as const;+---++<BaseLayout+ title={`${title} · Kiln`}+ description={description}+ breadcrumbs={[{ label: title, href: "/text-to-3d/" }]}+ card="text-to-3d"+ jsonLd={schema}+ preloadImage={{ src: hero.src, srcset: hero.srcsetAvif, sizes: HERO_SIZES }}+>+ <div class="container">+ <div class="drawing-sheet">+ <div class="grid px-5 pt-6 md:px-8 md:pt-8 xl:grid-cols-5 xl:gap-x-12 xl:pt-4">+ <div class="pt-4 md:pt-6 xl:col-span-2 xl:pt-12">+ <PageHeader class="max-w-lead">{title}.</PageHeader>+ <p class="lead mt-6">+ Describe a 3D model, or hand your agent a photo, sketch or concept+ art. It writes the model as a program, Kiln shows it what it built,+ and you keep a GLB with its source.+ </p>+ </div>+ <figure class="min-w-0 py-6 xl:col-span-3 xl:col-start-3 xl:row-span-2 xl:row-start-1 xl:self-center xl:py-8">+ <ResponsiveImage image={hero} sizes={HERO_SIZES} eager class="w-full bg-backdrop" />+ <figcaption class="mt-2 font-mono text-code text-dim">{hero.caption}</figcaption>+ </figure>+ <div class="pb-8 xl:col-span-2 xl:pb-12 xl:pt-2">+ <div class="grid gap-3 sm:flex sm:flex-wrap">+ <a href="#mech" class="btn">See the models <span aria-hidden="true">↓</span></a>+ <a href="#try" class="btn btn-secondary">Try it <span aria-hidden="true">↓</span></a>+ </div>+ <p class="mt-6 max-w-lead font-mono text-code text-dim">+ MIT · runs on your machine · MCP, CLI and TypeScript library · setups for {HARNESSES.length} coding agents+ </p>+ </div>+ </div>+ <p class="border-t border-rule px-5 py-4 text-dim md:px-8">+ Text to 3D is one way into Kiln. <a class="text-link" href={holodeckWorldUrl(leadWorld.id)}>Walk {leadWorld.name} <span aria-hidden="true">↗</span></a>, a world built with Kiln assets, or browse the <a class="text-link" href="/gallery/">gallery</a>.+ </p>+ </div>+ </div>++ <TextTo3dRun+ subject={mech}+ short="Mech"+ title="A walking mech you can drive from code."+ intro="A 6 m walker with named joints, pistons that follow them and a Walk clip whose feet don't slide. All of it is there for your game code to find."+ tryIt="Open it, then choose Walk in the Animation menu."+ revisions={[+ { step: "make", title: "First prompt", line: "A rigged walker with a Walk clip. Each toe stays within 0.4 mm of where it lands.", figures: ["make-sheet"] },+ { step: "direct", title: "Art note", line: "Chipped paint, grime in the panel lines, rust from the joints and real gunmetal. Height, joints and clip untouched.", figures: ["paint-before", "paint-after"] },+ { step: "revise", title: "One swap", line: "The claw becomes a rail cannon. The Walk data stays identical, byte for byte.", figures: ["before", "after"] },+ ]}+ code={[+ { summary: "How the feet stay planted", code: sourceExcerpt(mechProgram, "function solveLeg(", "toe: qToe };\n}"), label: "Solving a leg", previewLines: 18 },+ { summary: "Drive it from three.js", code: mechUse, label: "three.js, for mech.glb", previewLines: 24 },+ ]}+ />++ <TextTo3dRun+ subject={cathedral}+ short="Cathedral"+ title="A 144 m Gothic cathedral, written as one program."+ intro="Towers about 80 m tall, a 100 m lead spire, ribbed vaults, carved portals and stained glass, inside and out. Because it's one program, two more bays took one short prompt."+ note={POLISH_NOTE}+ credit="Its oak and iron textures come from Poly Haven, also CC0."+ tryIt="Open it, then choose Look inside to stand in the nave. It's a heavy model, so it needs a reasonable graphics card, and it hasn't been tried on a phone."+ views={[LOOK_INSIDE]}+ feature={{ image: "nave", title: "Stand in the nave.", line: "The sun baked into the stone also comes through the south windows, so each one lands on the floor in its own colours, between the columns." }}+ revisions={[+ { step: "make", title: "First prompt", line: "The plan, the vaults, the statues and the stained glass.", figures: ["first-look"] },+ { step: "direct", title: "First art note", line: "Sculpted saints with real faces, medieval glass, and a sun baked into the stone.", figures: ["art-1", "art-2"] },+ { step: "direct-2", title: "Second art note", line: "Real ground, stepped buttresses, carved portals and glass that reads dark from outside.", figures: ["west-2", "west-3"] },+ { step: "revise", title: "Two bays and a spire", line: `One short prompt. Of the ${number(geometry.parent)} meshes before it, ${number(geometry.inPlace + geometry.moved)} kept their exact shape: ${number(geometry.inPlace)} stayed put and ${number(geometry.moved)} moved east together.`, figures: ["before", "after"] },+ { step: "light", title: "Light from the windows", line: `Light traced from the south windows falls on the floor, with the piers' shadows cut through it. ${number(lightGeometry.inPlace)} of ${number(lightGeometry.parent)} meshes didn't move.`, figures: ["light-before", "light-after"] },+ { step: "sun", title: "A higher sun", line: "Each window now lands between the columns, and all the stone re-baked to match.", figures: ["sun-before", "sun-after"] },+ { step: "daylight", title: "Softer daylight", line: "The west front takes light from the sky and the sunlit forecourt, with the portals still deep.", figures: ["daylight-before", "daylight-after"] },+ ]}+ code={[+ { summary: "The parameters: change a number, change the building", code: sourceExcerpt(cathedralProgram, "const P = {", "chapelFacets: 5,\n};"), label: "The parameters" },+ { summary: "The diff for two bays and a spire", code: cathedral.diff!, lang: "diff", label: "Two bays and a spire", previewLines: 22 },+ ]}+ />++ <section class="container" aria-labelledby="how-heading">+ <div class="grid gap-6 border-t border-ink pt-8 lg:grid-cols-2 lg:gap-12">+ <h2 id="how-heading">How text to 3D works with Claude Code, Codex or Cursor.</h2>+ <p class="max-w-reading text-dim">+ Kiln is a local MCP server and CLI. Your coding agent writes 3D models as+ code and calls Kiln to build them and export GLBs. No model runs inside Kiln.+ </p>+ </div>+ <ol class="mt-8 grid list-none gap-6 p-0 md:grid-cols-2 md:gap-8 xl:grid-cols-4">+ {how.map(([heading, body], index) => (+ <li class="border-t border-rule pt-5">+ <p class="font-mono text-code text-dim">{String(index + 1).padStart(2, "0")}</p>+ <h3 class="mt-2 text-title">{heading}</h3>+ <p class="mt-3 text-dim">{body}</p>+ </li>+ ))}+ </ol>+ </section>++ <section id="try" class="container section-space" aria-labelledby="try-heading">+ <div class="grid gap-8 border-t border-ink pt-8 lg:grid-cols-12 lg:gap-12">+ <div class="lg:col-span-4">+ <h2 id="try-heading" class="text-subheading">Try the mech prompt.</h2>+ <p class="mt-5 max-w-reading">+ Install the package, create a workspace and open your agent in it, on+ Node.js 22 or 24 LTS. To match these models, start it with <code>claude --model {tools.model} --effort {tools.effort}</code>,+ and approve Kiln's tools when asked.+ </p>+ <p class="mt-4 max-w-reading">+ With no supported GPU, the agent sees shape and flat colour only.+ </p>+ <p class="mt-4 max-w-reading text-dim">+ Using another agent? Change <code>--harness claude</code> and put its own command on the last line.+ </p>+ <a href="/docs/install/" class="text-link mt-3">Read the installation guide <span aria-hidden="true">→</span></a>+ </div>+ <div class="min-w-0 space-y-4 lg:col-span-8">+ <CodeBlock code={installCommand} lang="bash" label={`Install Kiln ${VERSION_SHORT} and open a workspace`} />+ <CodeBlock code={mech.steps[0]!.prompt} lang="text" label="Then paste the mech prompt" />+ </div>+ </div>+ </section>++ <section class="container" aria-labelledby="questions-heading">+ <div class="grid gap-8 border-t border-ink pt-8 lg:grid-cols-12 lg:gap-12">+ <h2 id="questions-heading" class="text-subheading lg:col-span-4">Questions.</h2>+ <div class="min-w-0 lg:col-span-8">+ {questions.map(([question, answer, link]) => (+ <details class="border-b border-rule">+ <summary class="font-semibold">{question}</summary>+ <p class="max-w-reading px-4 pb-4 text-dim">{answer}{link && <> <a class="text-link" href={link.href}>{link.label}</a></>}</p>+ </details>+ ))}+ </div>+ </div>+ <p class="mt-10 max-w-none text-caption text-dim">+ Kiln {tools.kiln}, {harnessWithVersion(tools.harness, tools.harnessVersion)}, {modelName(tools.model)} at {tools.effort}. CC0-1.0 to the extent of Instrukt Labs' rights.+ </p>+ </section>+</BaseLayout>+<style is:global>+ .code-block[data-language="text"] .code-scroll pre { width: auto; white-space: pre-wrap; overflow-wrap: anywhere; }+</style>diff --git a/site/src/styles/global.css b/site/src/styles/global.cssindex 6f4263da..14386f6b 100644--- a/site/src/styles/global.css+++ b/site/src/styles/global.css@@ -1,4 +1,8 @@@import "tailwindcss";+/* The page's record and programs hold prompts and code comments, and a word in them must not become a rule on every page. */+@source not "../data/text-to-3d.json";+@source not "../../public/text-to-3d";+@source not inline("shadow");@plugin "@tailwindcss/typography";@theme {--color-*: initial;