diff --git a/bin/cli.mjs b/bin/cli.mjs index ba281d7..e7a598f 100644 --- a/bin/cli.mjs +++ b/bin/cli.mjs @@ -59,7 +59,9 @@ if (!/^[1-9]\d*x[1-9]\d*$/.test(frame)) fail(`--size is width x height, like 192 const [w, h] = frame.split('x').map(Number); const maxMB = +num('--max-mb', flag('--max-mb', 9.5)); // GitHub README uploads cap at 10 MB const headed = bool('--headed'); -const style = oneOf('--style', flag('--style', 'pixel'), ['pixel', 'maquette', 'plan', 'nocturne', 'neon']); +// (neon and chip aren't in --help: neon is the first prototype, chip the easter egg a plain visit rolls now and then) +const styleAsked = args.includes('--style'); // if not, the live view leaves the page to pick (and maybe roll chip) +const style = oneOf('--style', flag('--style', 'pixel'), ['pixel', 'maquette', 'plan', 'nocturne', 'neon', 'chip']); const pxOpt = num('--pixel', flag('--pixel')); // pixel style: size of one art pixel (default 1; 2 for chunkier art) const zoomOpt = num('--zoom', flag('--zoom')); // close-up factor (pixel style) const cityOpt = oneOf('--city', flag('--city', 'auto'), ['auto', 'classic', 'farm', 'desert', 'capital', 'mountain', 'university', 'port', 'industrial', 'science', 'themepark', 'resort', @@ -246,7 +248,7 @@ server.listen(port, '127.0.0.1'); server.on('listening', () => { const link = `http://localhost:${port}`; if (!rec) { - const view = `${link}/?style=${style}&city=${cityOpt}${pxOpt ? `&px=${pxOpt}` : ''}${zoomOpt ? `&zoom=${zoomOpt}` : ''}`; + const view = `${link}/?${styleAsked ? `style=${style}&` : ''}city=${cityOpt}${pxOpt ? `&px=${pxOpt}` : ''}${zoomOpt ? `&zoom=${zoomOpt}` : ''}`; console.log(` city open at ${cyan(view)}`); if (!noOpen) openBrowser(view); return; diff --git a/test/layout.mjs b/test/layout.mjs index c15e715..fe168f0 100644 --- a/test/layout.mjs +++ b/test/layout.mjs @@ -59,7 +59,7 @@ for (const name of readdirSync(path.join(HERE, 'snapshots')).filter(f => f.endsW const data = city(JSON.parse(readFileSync(path.join(HERE, 'snapshots', name), 'utf8'))); const gone = data.gone.slice(Math.max(0, data.gone.length - Math.ceil(data.files.length / 4))); if (!gone.length) continue; - for (const kind of CITY_KINDS) { + for (const kind of [...CITY_KINDS, 'board']) { // (board: the chip style's one plan) runs++; const was = plan(data, kind, []), now = plan(data, kind, gone); const say = msg => problems.push(`${name.replace('.json', '')} as ${kind}: ${msg}`); diff --git a/web/chip.js b/web/chip.js new file mode 100644 index 0000000..c54bb10 --- /dev/null +++ b/web/chip.js @@ -0,0 +1,425 @@ +// The easter egg (?style=chip, --style chip): any repo drawn as a circuit board, on the one board +// plan (layouts.js): every file a chip package on green solder mask, every import a copper trace from +// one chip to the other, passives and vias round them, silkscreen outlines, designators and names. +// Only loaded when it's asked for; studio.js builds the rest (light, land, the bodies, picking). +import * as THREE from 'three'; +import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js'; + +// packages lie low and flat: a big file is a thicker package, never a tower +export const chipHeight = (b, unit) => unit * (0.12 + 0.16 * Math.min(1, b.h / (unit * 6))); + +// how far the leads stick out on [x, z]: from the long sides of a long package (a DIP, an SOIC), +// from all four of a squarish one (a QFP) +export function leadMargins(B, unit) { + return B.map(b => { + const s = Math.min(b.w, b.d), p = Math.min(unit * 0.1, s * 0.14); + if (Math.max(b.w, b.d) < s * 1.6) return [p, p]; + return b.w > b.d ? [0, p] : [p, 0]; + }); +} + +// the lid, in the bodies' fragment shader: a pin-1 dimple by one corner, and the part number etched +// along the long side in two rows (left out where a package is too small, or too far, to read) +export const LID = /* glsl */` + { + vec2 c = vObj.xz * vSz.xz, hs = vSz.xz * 0.5; + float s = min(hs.x, hs.y), L = max(hs.x, hs.y), lid = step(0.5, vON.y); + float dimple = 1.0 - smoothstep(s * 0.1, s * 0.13, length(c + hs - s * 0.34)); + vec2 q = hs.x >= hs.y ? c : c.yx; + float row = s * 0.16, ch = q.x / (row * 0.8); + float etch = step(abs(abs(q.y) - row * 0.75), row * 0.26) * step(abs(q.x), L * 0.55) * step(0.2, fract(ch)) + * step(0.3, hash2(vec2(floor(ch), sign(q.y)) + vInfo.x * 17.0)) * step(uWin * 1.8, s) + * (1.0 - smoothstep(0.3, 0.8, fwidth(ch))); + diffuseColor.rgb = mix(diffuseColor.rgb, vec3(0.3), lid * max(dimple * 0.8, etch * 0.6)); + }`; + +// the leads: one flat box under each body as big as its whole footprint, cut in the shader to a row +// of leads in the margin round the body +const LEADS = /* glsl */` + vec2 w = vObj.xz * vSz.xz; // from the package's centre, world units + vec2 fw = fwidth(w) / uPitch; + vec2 hs = vSz.xz * 0.5 - vLead; // the body's half size + vec2 o = abs(w) - hs; // how far beyond the body, along each axis + if ((o.x > 0.0) == (o.y > 0.0)) discard; // under the body, or a corner + bool xSide = o.x > 0.0; // leads on the ±x sides are spaced along z + float along = xSide ? w.y : w.x, span = xSide ? hs.y : hs.x, f = xSide ? fw.y : fw.x; + float n = max(1.0, floor(span * 2.0 / uPitch - 0.3)); + float t = (along + n * uPitch * 0.5) / uPitch; // counted from the first lead + if (t < 0.0 || t > n) discard; + float lead = step(0.22, fract(t)) * step(fract(t), 0.78); + float far = smoothstep(0.35, 0.7, f); // too fine to draw one by one: a dimmer strip + if (far < 0.5 && lead < 0.5) discard; + diffuseColor.rgb *= mix(1.0, 0.6, far); + if (uFocus >= 0.0) diffuseColor.rgb *= mix(0.35, 1.0, vFocus);`; + +export function buildLeads({ bodies, B, H, leads, unit, S, shared, grow }) { + const geo = new THREE.BoxGeometry(1, 1, 1).translate(0, 0.5, 0); + for (const k of ['aInfo', 'aDied', 'aFocus']) geo.setAttribute(k, bodies.geometry.attributes[k]); // the bodies' own buffers + geo.setAttribute('aLead', new THREE.InstancedBufferAttribute(Float32Array.from(leads.flat()), 2)); + const mat = new THREE.MeshStandardMaterial({ color: S.leads, roughness: 0.35, metalness: 0.7 }); + mat.onBeforeCompile = sh => { + Object.assign(sh.uniforms, { uTimeline: shared.uTimeline, uFocus: shared.uFocus, uPitch: { value: unit * 0.09 } }); + sh.vertexShader = sh.vertexShader + .replace('#include ', `#include + attribute vec4 aInfo; attribute vec2 aLead; attribute float aDied, aFocus; uniform float uTimeline; + varying vec3 vObj, vSz; varying vec2 vLead; varying float vFocus;`) + .replace('#include ', `#include + ${grow} + vObj = position; vLead = aLead; vFocus = aFocus; + vSz = vec3(length(instanceMatrix[0].xyz), 0.0, length(instanceMatrix[2].xyz));`); + sh.fragmentShader = sh.fragmentShader + .replace('#include ', `#include + uniform float uFocus, uPitch; varying vec3 vObj, vSz; varying vec2 vLead; varying float vFocus;`) + .replace('#include ', `#include \n${LEADS}`); + }; + const mesh = new THREE.InstancedMesh(geo, mat, B.length); + const m = new THREE.Matrix4(), q = new THREE.Quaternion(), UP = new THREE.Vector3(0, 1, 0); + B.forEach((b, i) => { + q.setFromAxisAngle(UP, b.rot || 0); + m.compose(new THREE.Vector3(b.x, b.y, b.z), q, new THREE.Vector3(b.w, H[i] * 0.3, b.d)); + mesh.setMatrixAt(i, m); + }); + mesh.receiveShadow = true; + mesh.frustumCulled = false; + return mesh; +} + +// A part (or a trace) is on the board from when its file is born until it's deleted: aWhen = (born, died) +function during(material, shared, { focus = false } = {}) { + const prevKey = material.customProgramCacheKey.bind(material); + material.customProgramCacheKey = () => `${prevKey()}|during${focus ? '+focus' : ''}`; + material.onBeforeCompile = sh => { + Object.assign(sh.uniforms, { uTimeline: shared.uTimeline, uFocus: shared.uFocus }); + sh.vertexShader = sh.vertexShader + .replace('#include ', `#include + attribute vec2 aWhen; uniform float uTimeline;${focus ? ' attribute vec2 aEnds; uniform float uFocus; varying float vLit;' : ''}`) + .replace('#include ', `#include + if (uTimeline < aWhen.x * 0.97 + 0.03 || uTimeline > aWhen.y * 0.97 + 0.02) transformed.y -= 1e5;${focus ? ` + vLit = uFocus < 0.0 ? 1.0 : (abs(aEnds.x - uFocus) < 0.5 || abs(aEnds.y - uFocus) < 0.5) ? 1.8 : 0.3;` : ''}`); + if (focus) sh.fragmentShader = sh.fragmentShader + .replace('#include ', '#include \nvarying float vLit;') + .replace('#include ', '#include \ndiffuseColor.rgb *= vLit;'); + }; + return material; +} +const tag = (g, attrs) => { + const n = g.attributes.position.count; + for (const [name, v] of Object.entries(attrs)) { + const a = new Float32Array(n * v.length); + for (let k = 0; k < n; k++) a.set(v, k * v.length); + g.setAttribute(name, new THREE.BufferAttribute(a, v.length)); + } + return g; +}; + +// A small binary heap of (priority, item) for the router +function heap() { + let f = new Float64Array(1024), s = new Int32Array(1024), n = 0; + return { + get size() { return n; }, + clear() { n = 0; }, + push(p, x) { + if (n === f.length) { const f2 = new Float64Array(n * 2), s2 = new Int32Array(n * 2); f2.set(f); s2.set(s); f = f2; s = s2; } + let k = n++; + while (k > 0) { const up = (k - 1) >> 1; if (f[up] <= p) break; f[k] = f[up]; s[k] = s[up]; k = up; } + f[k] = p; s[k] = x; + }, + pop() { + const top = s[0], p = f[--n], x = s[n]; + let k = 0; + for (;;) { + let c = k * 2 + 1; + if (c >= n) break; + if (c + 1 < n && f[c + 1] < f[c]) c++; + if (f[c] >= p) break; + f[k] = f[c]; s[k] = s[c]; k = c; + } + f[k] = p; s[k] = x; + return top; + }, + }; +} + +// The copper of the board, laid out on a grid over it, every part's footprint (its leads included) +// kept clear: +// - each import a signal trace from one chip to the other, routed in 45° steps round the parts in +// between, shortest connections first, a via where it turns square (a trace that can't be routed +// in time is left out) +// - a decoupling capacitor beside every chip, a resistor beside every third, where there's room +// - fan-out: short stubs off some of each chip's leads to a via +// - reference designators in silkscreen: U for the chips, C and R for the passives +export function buildBoard({ B, files, leads, unit, top, S, shared, budgetMs = 900 }) { + const live = B.map((b, i) => !files[i].gone && b.w > 0.01); + const when = i => [B[i].born, B[i].died ?? 2]; + let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity; + B.forEach(b => { if (b.w > 0.01) { x0 = Math.min(x0, b.x - b.w / 2); x1 = Math.max(x1, b.x + b.w / 2); z0 = Math.min(z0, b.z - b.d / 2); z1 = Math.max(z1, b.z + b.d / 2); } }); + const group = new THREE.Group(); + if (!Number.isFinite(x0)) return group; + x0 -= unit; z0 -= unit; x1 += unit; z1 += unit; + const cell = Math.max(unit * 0.07, Math.max(x1 - x0, z1 - z0) / 300); + const nx = Math.ceil((x1 - x0) / cell), nz = Math.ceil((z1 - z0) / cell), NC = nx * nz; + const ix = x => Math.min(nx - 1, Math.max(0, Math.floor((x - x0) / cell))), iz = z => Math.min(nz - 1, Math.max(0, Math.floor((z - z0) / cell))); + const cx = c => x0 + (c % nx + 0.5) * cell, cz = c => z0 + (Math.floor(c / nx) + 0.5) * cell; + const owner = new Int32Array(NC); // 0: free; i + 1: part i's footprint; -1: a passive + const used = new Uint8Array(NC); // copper already there + // the cells whose centres fall inside a rectangle (a part only a little over a cell's edge leaves it free) + const lo = (v, o) => Math.max(0, Math.ceil((v - o) / cell - 0.5)), hi = (v, o, n) => Math.min(n - 1, Math.floor((v - o) / cell - 0.5)); + const rect = (xa, za, xb, zb, fn) => { for (let r = lo(za, z0); r <= hi(zb, z0, nz); r++) for (let c = lo(xa, x0); c <= hi(xb, x0, nx); c++) fn(r * nx + c); }; + // (no clearance round them: inside a block the parts stand only a cell or two apart, and a margin + // would wall the middle ones in) + B.forEach((b, i) => { if (b.w > 0.01) rect(b.x - b.w / 2, b.z - b.d / 2, b.x + b.w / 2, b.z + b.d / 2, c => { if (!owner[c]) owner[c] = i + 1; }); }); + const free = c => owner[c] === 0 && !used[c]; + // the open ground in connected pieces: a route between two parts is only tried when some piece + // touches both (a part packed in tight on every side has no way out, and searching for one is slow) + const piece = new Int32Array(NC).fill(-1), stack = []; + for (let c0 = 0, id = 0; c0 < NC; c0++) { + if (owner[c0] || piece[c0] >= 0) continue; + piece[c0] = id; stack.push(c0); + while (stack.length) { + const c = stack.pop(), x = c % nx; + for (const c2 of [x > 0 ? c - 1 : -1, x < nx - 1 ? c + 1 : -1, c - nx, c + nx]) if (c2 >= 0 && c2 < NC && !owner[c2] && piece[c2] < 0) { piece[c2] = id; stack.push(c2); } + } + id++; + } + const reach = i => { + const b = B[i], out = new Set(); + rect(b.x - b.w / 2 - cell, b.z - b.d / 2 - cell, b.x + b.w / 2 + cell, b.z + b.d / 2 + cell, c => { if (piece[c] >= 0) out.add(piece[c]); }); + return out; + }; + + const h = unit * 0.012, tw = cell * 0.5; + const copper = [], drills = []; + const via = (x, z, r, w, ends = [-1, -1]) => { + copper.push(tag(new THREE.CylinderGeometry(r, r, h * 1.6, 12).translate(x, top + h * 0.8, z), { aWhen: w, aEnds: ends })); + drills.push(tag(new THREE.CylinderGeometry(r * 0.45, r * 0.45, h * 1.8, 8).translate(x, top + h * 0.9, z), { aWhen: w })); + }; + const trace = (ax, az, bx, bz, w, ends = [-1, -1]) => { + const L = Math.hypot(bx - ax, bz - az); + if (L > 1e-6) copper.push(tag(new THREE.BoxGeometry(L + tw, h, tw).rotateY(-Math.atan2(bz - az, bx - ax)).translate((ax + bx) / 2, top + h / 2, (az + bz) / 2), { aWhen: w, aEnds: ends })); + }; + + // ---- signal traces: A* over the cells, each remembering the heading it was reached on, so turns + // cost and a trace only bends by 45° (or, at a cost, 90°) at a time + const DX = [1, 1, 0, -1, -1, -1, 0, 1], DZ = [0, 1, 1, 1, 0, -1, -1, -1]; + const g = new Float32Array(NC), from = new Int32Array(NC), head = new Int8Array(NC), seen = new Int32Array(NC), done = new Int32Array(NC); + const open = heap(); + let stamp = 0; + const pairs = new Map(); + files.forEach((f, i) => { if (live[i]) for (const j of f.imp) if (live[j] && i !== j) pairs.set(i < j ? `${i},${j}` : `${j},${i}`, [i, j]); }); + const edges = [...pairs.values()].sort((p, q) => Math.hypot(B[p[0]].x - B[p[1]].x, B[p[0]].z - B[p[1]].z) - Math.hypot(B[q[0]].x - B[q[1]].x, B[q[0]].z - B[q[1]].z)); + const t0 = performance.now(); + for (const [a, b] of edges.slice(0, 400)) { + if (performance.now() - t0 > budgetMs) break; + const ra = reach(a); + if (![...reach(b)].some(p => ra.has(p))) continue; + const s0 = iz(B[a].z) * nx + ix(B[a].x), goal = iz(B[b].z) * nx + ix(B[b].x); + const gx = goal % nx, gz = Math.floor(goal / nx); + const pass = c => owner[c] === 0 || owner[c] === a + 1 || owner[c] === b + 1 || c === goal; + // (weighted 1.5 over the octile distance: a little longer than the shortest, found many times faster) + const est = c => { const dx = Math.abs(c % nx - gx), dz = Math.abs(Math.floor(c / nx) - gz); return 1.5 * (Math.max(dx, dz) + 0.414 * Math.min(dx, dz)); }; + stamp++; open.clear(); + seen[s0] = stamp; g[s0] = 0; from[s0] = -1; head[s0] = -1; open.push(est(s0), s0); + let found = false, steps = 0; + while (open.size && steps++ < 25000) { + const c = open.pop(); + if (done[c] === stamp) continue; // (an older, costlier entry for a cell already settled) + done[c] = stamp; + if (c === goal) { found = true; break; } + const x = c % nx, z = (c - x) / nx, k = head[c]; + for (let k2 = 0; k2 < 8; k2++) { + const turn = k < 0 ? 0 : Math.abs(((k2 - k + 12) % 8) - 4); // eighths of a turn, either way + if (turn > 2) continue; + const x2 = x + DX[k2], z2 = z + DZ[k2]; + if (x2 < 0 || z2 < 0 || x2 >= nx || z2 >= nz) continue; + const c2 = z2 * nx + x2; + if (done[c2] === stamp || !pass(c2)) continue; + if (k2 & 1 && (!pass(z * nx + x2) || !pass(z2 * nx + x))) continue; // no cutting a part's corner + const cost = g[c] + (k2 & 1 ? 1.414 : 1) + (turn === 1 ? 0.6 : turn === 2 ? 2.5 : 0) + (used[c2] ? 4 : 0); + if (seen[c2] === stamp && g[c2] <= cost) continue; + seen[c2] = stamp; g[c2] = cost; from[c2] = c; head[c2] = k2; + open.push(cost + est(c2), c2); + } + } + if (!found) continue; + // back from the goal: one straight run per heading, a via where it turns square + const path = []; + for (let c = goal; c >= 0; c = from[c]) path.push(c); + path.reverse(); + const w = [Math.max(B[a].born, B[b].born), Math.min(B[a].died ?? 2, B[b].died ?? 2)], ends = [a, b]; + let runStart = path[0]; + for (let p = 1; p < path.length; p++) { + const c = path[p], k = head[c], next = path[p + 1]; + used[c] = 1; + if (next !== undefined && head[next] === k) continue; + trace(cx(runStart), cz(runStart), cx(c), cz(c), w, ends); + if (next !== undefined && (head[next] - k + 8) % 4 === 2 && owner[c] === 0) via(cx(c), cz(c), tw * 1.3, w, ends); + runStart = c; + } + } + + // ---- passives: a capacitor beside each chip, a resistor beside every third, on a side with room + const passives = []; // { x, z, along: 'x' | 'z', L, W, kind: 'C' | 'R', when } + const L = Math.max(unit * 0.16, cell * 2.6), W = L * 0.5; + const fits = (x, z, hx, hz) => { let ok = true; rect(x - hx, z - hz, x + hx, z + hz, c => { if (!free(c)) ok = false; }); return ok; }; + B.forEach((b, i) => { + if (!live[i]) return; + const want = i % 3 === 0 ? ['C', 'R'] : ['C']; + const sides = [[0, -1], [1, 0], [0, 1], [-1, 0]]; + let s = i % 4; + for (const kind of want) { + for (let n = 0; n < 4; n++, s = (s + 1) % 4) { + const [sx, sz] = sides[s], alongX = sz !== 0; + const hx = alongX ? L / 2 : W / 2, hz = alongX ? W / 2 : L / 2; + const t = (((i * 0.618) % 1) - 0.5) * 0.6; // somewhere along the side, not always its middle + const x = b.x + (sx ? sx * (b.w / 2 + cell * 2.4 + hx) : t * b.w), z = b.z + (sz ? sz * (b.d / 2 + cell * 2.4 + hz) : t * b.d); + if (!fits(x, z, hx, hz)) continue; + rect(x - hx, z - hz, x + hx, z + hz, c => { owner[c] = -1; }); + passives.push({ x, z, alongX, kind, when: when(i) }); + s = (s + 1) % 4; + break; + } + } + }); + + // ---- fan-out: every third lead of a chip gets a stub out and a via, where there's room + B.forEach((b, i) => { + if (!live[i] || !leads) return; + const [lx, lz] = leads[i], pitch = unit * 0.09; + for (const [axis, margin] of [['x', lx], ['z', lz]]) { + if (!margin) continue; + const span = axis === 'x' ? b.d / 2 - lz : b.w / 2 - lx; + const n = Math.max(1, Math.floor(span * 2 / pitch - 0.3)); + for (const side of [-1, 1]) { + let made = 0; + for (let m = 1; m < n && made < 4; m += 3) { + const along = (m + 0.5 - n / 2) * pitch, dir = Math.sign(along) || 1; + const out = cell * 1.6, diag = cell * 1.4; + const [ax, az] = axis === 'x' ? [b.x + side * b.w / 2, b.z + along] : [b.x + along, b.z + side * b.d / 2]; + const [bx, bz] = axis === 'x' ? [ax + side * out, az] : [ax, az + side * out]; + const [vx, vz] = axis === 'x' ? [bx + side * diag, bz + dir * diag] : [bx + dir * diag, bz + side * diag]; + const c1 = iz(bz) * nx + ix(bx), c2 = iz(vz) * nx + ix(vx); + if (owner[c2] !== 0 || used[c2] || used[c1] || (owner[c1] !== 0 && owner[c1] !== i + 1)) continue; + used[c1] = used[c2] = 1; + trace(ax, az, bx, bz, when(i), [i, i]); trace(bx, bz, vx, vz, when(i), [i, i]); via(vx, vz, tw * 1.2, when(i), [i, i]); + made++; + } + } + } + }); + + for (const [geos, mat, focus] of [ + [copper, new THREE.MeshStandardMaterial({ color: S.copper, roughness: 0.38, metalness: 0.55 }), true], + [drills, new THREE.MeshStandardMaterial({ color: '#0b0c0b', roughness: 0.9 }), false], + ]) { + if (!geos.length) continue; + const geo = mergeGeometries(geos); + geo.computeBoundingBox(); geo.computeBoundingSphere(); // (three.js wants the bounds after the arrays are gone) + for (const at of Object.values(geo.attributes)) at.onUpload(function () { this.array = null; }); // (the GPU keeps its own copy) + const mesh = new THREE.Mesh(geo, during(mat, shared, { focus })); + mesh.receiveShadow = true; + mesh.frustumCulled = false; + group.add(mesh); + } + if (passives.length) group.add(passiveMeshes(passives, { L, W, top, S, shared })); + + // ---- reference designators, numbered in reading order (top row first, left to right) + const order = list => list.sort((p, q) => Math.round(p.z / unit) - Math.round(q.z / unit) || p.x - q.x); + const chips = order(B.map((b, i) => ({ i, x: b.x, z: b.z })).filter(p => live[p.i])); + const th = Math.max(unit * 0.12, cell * 1.4), m = unit * 0.05; + const marks = chips.map(({ i }, n) => ({ text: `U${n + 1}`, x: B[i].x - B[i].w / 2 - m, z: B[i].z - B[i].d / 2 - m - th * 0.65, h: th, when: when(i) })); + const count = { C: 0, R: 0 }; + for (const p of order(passives)) marks.push({ text: `${p.kind}${++count[p.kind]}`, x: p.x - (p.alongX ? L / 2 : W / 2), z: p.z - (p.alongX ? W / 2 : L / 2) - th * 0.45, h: th * 0.6, when: p.when }); + group.add(silkText(marks, { y: top + unit * 0.012, S, shared })); + return group; +} + +// chip resistors and capacitors: a body between two tinned end caps +function passiveMeshes(list, { L, W, top, S, shared }) { + const group = new THREE.Group(); + const H = W * 0.6, cap = L * 0.2; + const body = new THREE.InstancedMesh(new THREE.BoxGeometry(1, 1, 1).translate(0, 0.5, 0), during(new THREE.MeshStandardMaterial({ roughness: 0.6 }), shared), list.length); + const ends = new THREE.InstancedMesh(new THREE.BoxGeometry(1, 1, 1).translate(0, 0.5, 0), during(new THREE.MeshStandardMaterial({ color: S.leads, roughness: 0.35, metalness: 0.7 }), shared), list.length * 2); + const m = new THREE.Matrix4(), q = new THREE.Quaternion(), c = new THREE.Color(); + const bodyWhen = new Float32Array(list.length * 2), endWhen = new Float32Array(list.length * 4); + list.forEach((p, k) => { + const [ux, uz] = p.alongX ? [1, 0] : [0, 1]; + m.compose(new THREE.Vector3(p.x, top, p.z), q, new THREE.Vector3(p.alongX ? L - cap * 2 : W, H, p.alongX ? W : L - cap * 2)); + body.setMatrixAt(k, m); + body.setColorAt(k, c.set(p.kind === 'C' ? '#b49a6c' : '#1b1b1b')); + bodyWhen.set(p.when, k * 2); + [-1, 1].forEach((s, e) => { + m.compose(new THREE.Vector3(p.x + ux * s * (L - cap) / 2, top, p.z + uz * s * (L - cap) / 2), q, new THREE.Vector3(p.alongX ? cap : W * 1.02, H * 1.02, p.alongX ? W * 1.02 : cap)); + ends.setMatrixAt(k * 2 + e, m); + endWhen.set(p.when, (k * 2 + e) * 2); + }); + }); + body.geometry.setAttribute('aWhen', new THREE.InstancedBufferAttribute(bodyWhen, 2)); + ends.geometry.setAttribute('aWhen', new THREE.InstancedBufferAttribute(endWhen, 2)); + for (const mesh of [body, ends]) { mesh.frustumCulled = false; mesh.receiveShadow = true; group.add(mesh); } + return group; +} + +// silkscreen text, one flat quad per character, read from above (top of the letters towards -z) +const GLYPHS = '0123456789UCR'; +function silkText(marks, { y, S, shared }) { + const cw = 32, chH = 48, canvas = document.createElement('canvas'); + canvas.width = cw * GLYPHS.length; canvas.height = chH; + const g = canvas.getContext('2d'); + g.fillStyle = '#fff'; g.font = `600 40px 'JetBrains Mono', ui-monospace, monospace`; g.textAlign = 'center'; g.textBaseline = 'middle'; + [...GLYPHS].forEach((ch, k) => g.fillText(ch, k * cw + cw / 2, chH / 2 + 2)); + const tex = new THREE.CanvasTexture(canvas); + tex.anisotropy = 4; + const quads = []; + for (const mk of marks) [...mk.text].forEach((ch, k) => quads.push([mk.x + (k + 0.5) * mk.h * 0.62, mk.z, mk.h, GLYPHS.indexOf(ch), ...mk.when])); + const geo = new THREE.PlaneGeometry(1, 1).rotateX(-Math.PI / 2); + const mesh = new THREE.InstancedMesh(geo, new THREE.ShaderMaterial({ + transparent: true, depthWrite: false, + uniforms: { uTimeline: shared.uTimeline, uAtlas: { value: tex }, uColor: { value: new THREE.Color(S.silk) }, uY: { value: y }, uN: { value: GLYPHS.length } }, + vertexShader: `attribute vec4 aGlyph; attribute vec2 aWhen; uniform float uTimeline, uY, uN; varying vec2 vUv; + void main(){ + if (uTimeline < aWhen.x * 0.97 + 0.03 || uTimeline > aWhen.y * 0.97 + 0.02) { gl_Position = vec4(0., 0., -2., 1.); return; } + vUv = vec2((aGlyph.w + uv.x) / uN, uv.y); + vec3 p = vec3(aGlyph.x + position.x * aGlyph.z * 0.62, uY, aGlyph.y + position.z * aGlyph.z); + gl_Position = projectionMatrix * viewMatrix * vec4(p, 1.); + }`, + fragmentShader: `uniform sampler2D uAtlas; uniform vec3 uColor; varying vec2 vUv; + void main(){ float a = texture2D(uAtlas, vUv).a; if (a < 0.05) discard; gl_FragColor = vec4(uColor, a * 0.9); }`, + }), quads.length); + const glyph = new Float32Array(quads.length * 4), w = new Float32Array(quads.length * 2); + quads.forEach((qd, k) => { glyph.set(qd.slice(0, 4), k * 4); w.set(qd.slice(4), k * 2); }); + geo.setAttribute('aGlyph', new THREE.InstancedBufferAttribute(glyph, 4)); + geo.setAttribute('aWhen', new THREE.InstancedBufferAttribute(w, 2)); + mesh.frustumCulled = false; + return mesh; +} + +// silkscreen: an outline round each package's footprint, its pin-1 corner cut off, there while the +// package is +export function buildSilkscreen({ B, unit, S, shared }) { + const pos = [], when = []; + const m = unit * 0.05; + B.forEach(b => { + if (!(b.w > 0.01)) return; + const hw = b.w / 2 + m, hd = b.d / 2 + m, y = b.y + unit * 0.01, k = Math.min(unit * 0.12, hw * 0.6, hd * 0.6); + const c = Math.cos(b.rot || 0), s = Math.sin(b.rot || 0); + const P = ([x, z]) => [b.x + x * c + z * s, y, b.z - x * s + z * c]; + const ring = [[-hw + k, -hd], [hw, -hd], [hw, hd], [-hw, hd], [-hw, -hd + k]]; + ring.forEach((p, j) => { + const q = ring[(j + 1) % ring.length]; + pos.push(...P(p), ...P(q)); + when.push(b.born, b.died ?? 2, b.born, b.died ?? 2); + }); + }); + const g = new THREE.BufferGeometry(); + g.setAttribute('position', new THREE.Float32BufferAttribute(pos, 3)); + g.setAttribute('aWhen', new THREE.Float32BufferAttribute(when, 2)); + const lines = new THREE.LineSegments(g, new THREE.ShaderMaterial({ + transparent: true, depthWrite: false, + uniforms: { uTimeline: shared.uTimeline, uColor: { value: new THREE.Color(S.silk) } }, + vertexShader: `attribute vec2 aWhen; uniform float uTimeline; varying float vA; + void main(){ vA = step(aWhen.x * 0.97 + 0.03, uTimeline) * step(uTimeline, aWhen.y * 0.97 + 0.02); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.); }`, + fragmentShader: `uniform vec3 uColor; varying float vA; void main(){ if (vA <= 0.0) discard; gl_FragColor = vec4(uColor, 0.7); }`, + })); + lines.frustumCulled = false; + return lines; +} diff --git a/web/index.html b/web/index.html index ea7ee76..b9196ee 100644 --- a/web/index.html +++ b/web/index.html @@ -126,6 +126,12 @@ body.style-pixel #links a, body.style-pixel #links button { color: #14121c; } body.style-pixel #links a:hover, body.style-pixel #links button:hover { color: #c0413b; } body.waiting #links { visibility: hidden; } + /* chip: silkscreen type on a dark board */ + body.style-chip, body.style-chip #hud h1, body.style-chip #date .d, body.style-chip .label { font-family: var(--ui-font); } + body.style-chip #hud h1 { font-size: 30px; font-weight: 600; letter-spacing: .02em; } + body.style-chip .label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .14em; } + body.style-chip .label.on { color: #d6a648 !important; } + body.style-chip #track { --on: #c8803f; --off: rgba(236,235,226,.16); --head: var(--ink); } /* pixel names are little signs on posts, in the same paper-and-ink as the rest of the page */ body.style-pixel .label { font-size: 12px; color: #14121c !important; text-shadow: none; background: #f2efe9; border: 2px solid #14121c; box-shadow: 2px 2px 0 #14121c; padding: 0 5px; line-height: 16px; letter-spacing: .02em; text-transform: none; } diff --git a/web/layouts.js b/web/layouts.js index 3b564fd..0427cc0 100644 --- a/web/layouts.js +++ b/web/layouts.js @@ -1205,6 +1205,45 @@ const PLANNERS = { height: (i, rel) => i === ctx.hub ? 2.4 : Math.min(0.35 + 0.25 * Math.pow(rel, 0.4), 0.9), }; }, + + // not a city: the one plan of the chip style (?style=chip), whatever the repo. A square board, each + // directory a rectangular block of it, cut into an even grid of cells with channels between them + // for the traces, the parts packed into the cells in order + board(ds) { + // each block's share counts the channel round it, so a small directory stays a small square + // instead of being squeezed to a sliver by the channels + const PAD = 1.1, share = d => (Math.sqrt(d.area / 0.6) + PAD) ** 2; + const W = Math.sqrt(sum(ds.map(share))) + 1.2; + const root = hierarchy({ children: ds.map(d => ({ d, v: share(d) })) }).sum(x => x.v || 0).sort((a, b) => b.value - a.value); + treemap().tile(treemapSquarify.ratio(1)).size([W, W]).paddingInner(PAD).paddingOuter(0.6)(root); + const lots = [], blocks = [], labels = [], CELL = 6, GAP = 0.45; + for (const leaf of root.leaves()) { + const d = leaf.data.d, w = leaf.x1 - leaf.x0, h = leaf.y1 - leaf.y0; + const cols = Math.max(1, Math.round(w / Math.sqrt(CELL))), rows = Math.max(1, Math.round(h / Math.sqrt(CELL))); + const cw = (w - GAP * (cols - 1)) / cols, ch = (h - GAP * (rows - 1)) / rows; + // each cell takes an even share of what's left of the block's parts, by area, and a part goes in + // if at least half of it fits: a share fixed up front would leave every cell a little short and + // the last one crammed with the rest + const cells = cols * rows; + let k = 0, left = d.area; + for (let n = 0; n < cells && k < d.items.length; n++) { + const take = [], each = left / (cells - n); + let used = 0; + while (k < d.items.length && (n === cells - 1 || !take.length || used + d.items[k].a / 2 <= each)) { used += d.items[k].a; take.push(d.items[k++]); } + left -= used; + const c = n % cols, r = Math.floor(n / cols), x0 = leaf.x0 + c * (cw + GAP), z0 = leaf.y0 + r * (ch + GAP); + blocks.push({ poly: rectPoly(x0, z0, x0 + cw, z0 + ch), type: 'block' }); + lots.push(...packRect(take, x0, z0, x0 + cw, z0 + ch, 0.12)); + } + if (d.name !== '·') labels.push({ text: d.name, x: (leaf.x0 + leaf.x1) / 2, z: leaf.y1 + 0.45 }); + } + // the board itself, on a dark bench + return { + lots, blocks, labels, ground: 'water', land: [rectPoly(-0.5, -0.5, W + 0.5, W + 0.5)], + camera: { az: 90, el: 89, dist: 1.05 }, // from straight above, the board square to the frame + height: (i, rel) => Math.min(0.32 + 0.95 * Math.sqrt(rel), 5), + }; + }, }; // ---------------------------------------------------------------- entry point diff --git a/web/main.js b/web/main.js index 7ee1f9e..bda7c26 100644 --- a/web/main.js +++ b/web/main.js @@ -25,8 +25,11 @@ const fail = msg => { }; addEventListener('error', e => fail(e.message || e.error)); addEventListener('unhandledrejection', e => fail(e.reason?.stack || e.reason)); -// pixel | maquette | plan | nocturne | neon (the original prototype) -const STYLE = params.get('style') || 'pixel'; +// pixel | maquette | plan | nocturne | neon (the original prototype) | chip (the easter egg) +// a visit that names no style is pixel, except one in 100,000 that rolls chip and keeps ?style=chip, so it can be shared +// (never a share card or a recording: those are made, not visited; an empty ?style= names none either) +const STYLE = params.get('style') || (!params.has('card') && !params.has('record') && Math.random() < 1e-5 ? 'chip' : 'pixel'); +if (!params.get('style') && STYLE === 'chip') { params.set('style', STYLE); history.replaceState(null, '', `${location.pathname}?${params}${location.hash}`); } const NEON = STYLE === 'neon'; const PIXEL = STYLE === 'pixel'; // art-pixel size in screen pixels, and how far to zoom in (close-ups frame the city's landmark) @@ -154,10 +157,11 @@ for (const l of leaves) { // when each comes down (2: never), at the end of the timeline at the latest (a deletion can be the last commit) B.forEach((b, i) => { b.died = files[i].gone ? Math.min(1, t01(files[i].died)) : 2; }); -// city planners (lit styles only): ?city=auto picks the repo's own archetype, ?city=classic keeps the treemap +// city planners (lit styles only): ?city=auto picks the repo's own archetype, ?city=classic keeps the treemap; +// a circuit board (?style=chip) has the one plan, whatever the city const CITY = params.get('city') || 'auto'; -const cityKind = NEON || CITY === 'classic' ? null : CITY === 'auto' ? data.identity?.city : CITY; -const layout = cityKind && CITY_KINDS.includes(cityKind) ? timed('layout', () => buildLayout(cityKind, { files, inDeg, hub, SIZE, frontAz: PIXEL ? 45 : undefined })) : null; +const cityKind = NEON ? null : STYLE === 'chip' ? 'board' : CITY === 'classic' ? null : CITY === 'auto' ? data.identity?.city : CITY; +const layout = cityKind && (CITY_KINDS.includes(cityKind) || cityKind === 'board') ? timed('layout', () => buildLayout(cityKind, { files, inDeg, hub, SIZE, frontAz: PIXEL ? 45 : undefined })) : null; if (layout) { unit = layout.unit; plateStep = unit * 0.06; @@ -232,7 +236,7 @@ controls.dampingFactor = 0.06; controls.maxPolarAngle = Math.PI * 0.48; controls.minDistance = unit * 2; controls.maxDistance = SIZE * 2.5; -controls.autoRotate = true; +controls.autoRotate = STYLE !== 'chip'; // a board is read from above, not spun controls.autoRotateSpeed = 0.35; let userMoved = false; // once you've panned or zoomed, a window resize doesn't snap the view back controls.addEventListener('start', () => { controls.autoRotate = false; userMoved = true; }); @@ -426,8 +430,10 @@ if (NEON) { const heat = new Float32Array(N); const focusAttr = new Float32Array(N).fill(1); const glowAttr = new Float32Array(N); // 1 for the buildings of the district under the pointer +// the circuit board's parts are fetched only for it +const chip = STYLE === 'chip' ? await import('./chip.js') : null; const studio = NEON ? null : timed('studio (all)', () => buildStudio({ name: STYLE, scene, renderer, B, files, districts, unit, SIZE, plateStep, cx, cz, - shared, heat, focusAttr, glowAttr, inDeg, hub, t01, layout })); + shared, heat, focusAttr, glowAttr, inDeg, hub, t01, layout, chip })); if (studio) { document.body.classList.add('studio', `style-${STYLE}`); if (studio.style.uiFont) document.body.style.setProperty('--ui-font', studio.style.uiFont); @@ -839,7 +845,7 @@ if (route) { $('#hud h1').replaceChildren(a); $('#gh-repo').href = gh; $('#gh-repo').hidden = false; $('#another').hidden = false; } -const CITY_PHRASE = { farm: 'a farm village', desert: 'a desert boomtown', industrial: 'a factory town', science: 'a science park', themepark: 'a theme park', resort: 'a seaside resort', capital: 'a planned capital', mountain: 'a mountain city', university: 'a university town', port: 'a port city', smalltown: 'a small town', manhattan: 'Manhattan', hongkong: 'Hong Kong', tokyo: 'Tokyo', la: 'Los Angeles', paris: 'Paris', venice: 'Venice', barcelona: 'Barcelona' }; +const CITY_PHRASE = { board: 'a circuit board', farm: 'a farm village', desert: 'a desert boomtown', industrial: 'a factory town', science: 'a science park', themepark: 'a theme park', resort: 'a seaside resort', capital: 'a planned capital', mountain: 'a mountain city', university: 'a university town', port: 'a port city', smalltown: 'a small town', manhattan: 'Manhattan', hongkong: 'Hong Kong', tokyo: 'Tokyo', la: 'Los Angeles', paris: 'Paris', venice: 'Venice', barcelona: 'Barcelona' }; const shareLink = route ? `https://gitropolis.dev/${route[1]}/${route[2]}` : 'https://gitropolis.dev'; $('#share').onclick = async () => { if (!pix || !layout) { // share cards are drawn in the pixel style; elsewhere, the link @@ -1049,8 +1055,8 @@ function updateLabels() { el.style.left = `${boxes[k].x}px`; el.style.top = `${boxes[k].y}px`; el.classList.toggle('on', k === hoverLabel); - // perspective views fade far names; the isometric pixel view has no near and far - el.style.opacity = camera.isOrthographicCamera ? 1 : Math.max(0.15, Math.min(1, 1.6 - camera.position.distanceTo(p) / SIZE)); + // perspective views fade far names; the isometric pixel view has no near and far, nor a board seen from above + el.style.opacity = camera.isOrthographicCamera || STYLE === 'chip' ? 1 : Math.max(0.15, Math.min(1, 1.6 - camera.position.distanceTo(p) / SIZE)); }); } @@ -1291,8 +1297,19 @@ async function record() { aimPixel(FIT.center.clone().lerp(hubPos, push), FIT.zoom * (1 + 2.0 * push * push)); } + // a circuit board is filmed as it's seen, from straight above: a slow push-in on the hub, never turning + function shotBoard(u) { + planCamera(layout.camera); + const k = REC.fixed ? 0 : smooth(0.35, 1, u); + const from = controls.target.clone(), off = camera.position.clone().sub(from); + pivot.copy(from).lerp(hubPos, k); + camera.position.copy(pivot).addScaledVector(off, 1 - 0.6 * k * k); + camera.lookAt(pivot); + } + function shot(u) { if (PIXEL) return shotPixel(u); + if (layout?.kind === 'board') return shotBoard(u); const e = ease(u); pivot.set(0, 0, 0).lerp(hubPos, smooth(0.5, 1, u)); const theta = theta0 + 1.7 * e; diff --git a/web/studio.js b/web/studio.js index a4736bc..14b9046 100644 --- a/web/studio.js +++ b/web/studio.js @@ -2,6 +2,7 @@ // maquette — white architectural model under a low raking sun (Panorama of NYC, Gismondi's Rome) // plan — black-on-paper figure-ground print seen from straight above (Nolli map, Stamen Toner) // nocturne — dark, hazy city where only recently edited files are lit from inside (Hopper, Todd Hido) +// chip — the easter egg: a circuit board, its parts from chip.js (passed in as ctx.chip) import * as THREE from 'three'; import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js'; import { buildingGeometry, shipGeometry } from './buildings.js'; @@ -53,6 +54,17 @@ export const STYLES = { toneMapping: THREE.ACESFilmicToneMapping, exposure: 1.1, bloom: { strength: 0.35, radius: 0.4, threshold: 0.85 }, ink: '#e8e2d6', inkSoft: 'rgba(232,226,214,.55)', font: "'Instrument Serif', Georgia, serif", labelCase: 'none', }, + chip: { + background: '#060d09', fog: ['#060d09', 0.9, 3.0], chip: true, + sun: { color: '#fff6e8', intensity: 2.6, elevation: 55, azimuth: 130 }, + hemi: { sky: '#dfe9e1', ground: '#0b2a1a', intensity: 0.9 }, + building: '#1c1c1f', buildingJitter: 0.03, ground: '#0e4a2b', plate: '#13583a', roughness: 0.5, + rail: '#b9bcc2', water: '#121514', land: '#0e4a2b', yard: '#13583a', dirt: '#8a6136', sand: '#13583a', dune: '#10502f', cliff: '#0c3f25', plaza: '#13583a', park: '#13583a', lawn: '#10502f', hill: '#141416', road: '#c8803f', + copper: '#c8803f', leads: '#c9ccd2', silk: '#ecebe2', + windows: 0, accent: '#d6a648', edges: null, + toneMapping: THREE.ACESFilmicToneMapping, exposure: 1.05, bloom: null, + ink: '#ecebe2', inkSoft: 'rgba(236,235,226,.55)', font: "'JetBrains Mono', ui-monospace, monospace", uiFont: "'JetBrains Mono', ui-monospace, monospace", labelCase: 'upper', + }, }; const UP = new THREE.Vector3(0, 1, 0); @@ -98,7 +110,8 @@ function buildTerrain(scene, layout, S, plateStep, shared, street, blockBorn) { // when each block opens (from the street field); without one, everything is there from the start const bornOf = b => blockBorn ? blockBorn[layout.blocks.indexOf(b)] : 0; const landTop = layout.groundIsWater ? plateStep * 0.8 : 0; - if (layout.groundIsWater) add(slab([...layout.land, ...layout.shore], 0, landTop), S.land, 'grass-or-street'); + // (a circuit board comes up nearly to its blocks, so a trace can run from one to the next) + if (layout.groundIsWater) add(slab([...layout.land, ...layout.shore], 0, S.chip ? landTop + plateStep * 0.92 : landTop), S.land, 'grass-or-street'); // lakes and rivers; on a city whose ground is the sea, they're pools sitting on the land if (layout.water.length) add(slab(layout.water, layout.groundIsWater ? landTop : 0, plateStep * 0.05), S.water, 'water'); const colors = { block: S.plate, park: S.park, plaza: S.plaza, lawn: S.lawn, yard: S.yard, beach: S.sand }; @@ -210,12 +223,12 @@ function buildTerrain(scene, layout, S, plateStep, shared, street, blockBorn) { } export function buildStudio(ctx) { - const { name, scene, renderer, B, files, districts, unit, SIZE, plateStep, cx, cz, shared, heat, focusAttr, inDeg, hub, t01, layout } = ctx; + const { name, scene, renderer, B, files, districts, unit, SIZE, plateStep, cx, cz, shared, heat, focusAttr, inDeg, hub, t01, layout, chip } = ctx; // a desert city stands on sand: the open ground and the lawns between blocks become dunes const S = layout?.sand ? { ...STYLES[name], land: STYLES[name].sand, lawn: STYLES[name].dune } : STYLES[name]; const N = files.length; // each style sets its own skyline proportions: a model city is squat, a night city can soar - const H = B.map(b => S.flat ? plateStep * 0.5 : layout ? b.h : Math.min(b.h * (S.heightScale || 1), unit * (S.maxHeight || 99))); + const H = B.map(b => S.flat ? plateStep * 0.5 : S.chip ? chip.chipHeight(b, unit) : layout ? b.h : Math.min(b.h * (S.heightScale || 1), unit * (S.maxHeight || 99))); renderer.toneMapping = S.toneMapping; renderer.toneMappingExposure = S.exposure; @@ -262,11 +275,12 @@ export function buildStudio(ctx) { } // ---- ground - const groundColor = !layout ? S.ground : { water: S.water, land: S.land, road: S.road }[layout.ground]; + const groundColor = !layout ? S.ground : { water: S.water, land: S.land, road: S.chip ? S.land : S.road }[layout.ground]; // (a board is all solder mask) const groundMat = S.flat ? new THREE.MeshBasicMaterial({ color: groundColor }) - : new THREE.MeshStandardMaterial({ color: groundColor, roughness: layout?.groundIsWater ? 0.35 : 1 }); + : new THREE.MeshStandardMaterial({ color: groundColor, roughness: S.chip ? 0.9 : layout?.groundIsWater ? 0.35 : 1 }); // (chip: the board's bench, no glare) // pixel style: streets come from a distance field over the blocks; asphalt only where streets are - const streets = S.pixel && layout ? timed('streets (distance field)', () => buildStreets({ layout, B, files, inDeg, SIZE, t01 })) : null; + // (chip: copper traces down them) + const streets = (S.pixel || S.chip) && layout ? timed('streets (distance field)', () => buildStreets({ layout, B, files, inDeg, SIZE, t01 })) : null; // only what the street shader needs: copying the whole object would keep the routing closures (and the big field arrays they hold) alive // a campus has footpaths, not roads: paved stone, no centre line const pathC = layout?.paths ? new THREE.Color(layout.paths === 'dirt' ? S.dirt : S.plaza) : null; @@ -289,7 +303,7 @@ export function buildStudio(ctx) { // Venice has no cars, only boats if (streets && layout.paths) { streets.lanes = []; streets.boats = []; } // footpaths: no traffic if (streets && layout.kind === 'venice') { streets.lanes = []; streets.boats = [...streets.boats, ...streets.boats.map(b => ({ ...b, pts: [...b.pts].reverse() }))]; } - const traffic = streets ? timed('traffic', () => buildTraffic({ streets, unit: layout.unit })) : null; + const traffic = S.pixel && streets ? timed('traffic', () => buildTraffic({ streets, unit: layout.unit })) : null; if (traffic) scene.add(traffic.mesh); // the field's lookup tables were only needed to plan routes; the GPU keeps its own copy of the texture const trees = S.pixel && layout ? timed('trees', () => buildTrees({ layout, B, streets, unit: layout.unit, plateStep, shared })) : null; @@ -334,11 +348,14 @@ export function buildStudio(ctx) { const mesh = new THREE.InstancedMesh(geo, mat, N); const m = new THREE.Matrix4(), q = new THREE.Quaternion(), c = new THREE.Color(); const maxChurn = Math.max(1, ...files.map(f => f.churn)); + // chip: the body stands inside its leads, which take the rest of the footprint + const leads = S.chip ? chip.leadMargins(B, unit) : null; files.forEach((f, i) => { const b = B[i]; const h = H[i]; + const [lx, lz] = leads ? leads[i] : [0, 0]; q.setFromAxisAngle(UP, b.rot || 0); - m.compose(new THREE.Vector3(b.x, b.y, b.z), q, new THREE.Vector3(b.w, h, b.d)); + m.compose(new THREE.Vector3(b.x, b.y, b.z), q, new THREE.Vector3(b.w - lx * 2, h, b.d - lz * 2)); mesh.setMatrixAt(i, m); const j = (Math.sin(i * 12.9898) * 43758.5453 % 1) * S.buildingJitter; mesh.setColorAt(i, i === hub ? accent : c.copy(base).offsetHSL(0, 0, j)); @@ -377,7 +394,7 @@ export function buildStudio(ctx) { .replace('#include ', `#include if (uFocus >= 0.0) diffuseColor.rgb *= mix(0.35, 1.0, vFocus); if (abs(vId - uHover) < 0.5) diffuseColor.rgb = mix(diffuseColor.rgb, uAccent, 0.35); - diffuseColor.rgb = mix(diffuseColor.rgb, uAccent, vHeat * 0.6);`) + diffuseColor.rgb = mix(diffuseColor.rgb, uAccent, vHeat * 0.6);${S.chip ? chip.LID : ''}`) .replace('#include ', `#include if (uWindows > 0.0 && abs(vON.y) < 0.5) { float u = abs(vON.x) > 0.5 ? vObj.z * vSz.z : vObj.x * vSz.x; @@ -407,6 +424,11 @@ export function buildStudio(ctx) { mesh.computeBoundingSphere(); mesh.frustumCulled = false; scene.add(mesh); + if (S.chip) { + scene.add(chip.buildLeads({ bodies: mesh, B, H, leads, unit, S, shared, grow: growVertex }), chip.buildSilkscreen({ B, unit, S, shared })); + const top = (layout.groundIsWater ? plateStep * 0.8 : 0) + plateStep; // where the parts stand + scene.add(timed('board (routing)', () => chip.buildBoard({ B, files, leads, unit, top, S, shared }))); + } // pixel style: real architecture per file; the box mesh stays as an invisible picking proxy let cityMesh = null; if (S.pixel && layout) {