From 2090d085e8e0bc2dd9fc38d0eacf8b0095f7196d Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Tue, 3 Nov 2026 00:40:39 -0500 Subject: [PATCH 1/6] Page: clean centre lines on streets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Street centre lines were found per pixel as the ridge of the 8-bit distance field. Rounding left flat spots all over the asphalt, so the test fired at random and the roads were covered in white specks; the dash direction came from an angle that wraps at 0/π and dashes were measured from the world origin, so they broke up. - streets: a second sweep finds each texel's nearest *other* block, and near the middle of a street the distances to the two blocks' outlines give a signed distance to the centre line (B) and a mask of where one may be drawn (A). Distances are to the line of the side a street runs along, so the centre line carries on across the mouth of a narrower side street and stops out in a junction. Streets with a block on one side only (open land, a river bank) have none. - A second RG texture says where the dashes fall: the length round one of the two blocks' outlines, as an angle so it filters without a seam. Dashes keep their spacing round curved streets. - surfaces: the line is a pixel wide (wider with the street, close up); dashes are as before at the default zoom and lengthen zoomed out. Streets build in ~113 ms on flask, up from ~45 ms. Co-Authored-By: Claude Opus 5.5 --- web/streets.js | 112 ++++++++++++++++++++++++++++++++++++++++++++---- web/studio.js | 4 +- web/surfaces.js | 25 +++++------ 3 files changed, 119 insertions(+), 22 deletions(-) diff --git a/web/streets.js b/web/streets.js index f5fa74f..38cb086 100644 --- a/web/streets.js +++ b/web/streets.js @@ -1,8 +1,8 @@ // Streets, derived rather than drawn: rasterise every city block, then a vector distance transform // tells each patch of ground how far it is from the nearest block and in which direction. That one // field gives us, for any city layout, where the streets are (near blocks), where their centre lines -// run (where the distance peaks) and how wide each street is — so we can paint asphalt only where -// streets actually are and drive traffic down the right lanes. +// run (where two blocks are equally far) and how wide each street is — so we can paint asphalt only +// where streets actually are and drive traffic down the right lanes. import * as THREE from 'three'; import { LAND_LEAD } from './surfaces.js'; import { mergeGeometries } from 'three/addons/utils/BufferGeometryUtils.js'; @@ -83,17 +83,14 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { for (let i = 1; i < W; i++) relax(j * W + i, j * W + i - 1, -1, 0); } - // --- pack: R = distance in texels, B = across-street direction (to the nearest block) mod π + // --- pack: R = distance in texels; B, A = the street's centre line (below) const data = new Uint8Array(W * W * 4); const D = new Float32Array(W * W); for (let p = 0; p < W * W; p++) { const d = vx[p] === INF ? 255 : Math.sqrt(d2(p)); D[p] = d; data[p * 4] = Math.min(255, Math.round(d)); - let a = Math.atan2(vz[p], vx[p]); - if (a < 0) a += Math.PI; - data[p * 4 + 2] = Math.round((a / Math.PI) * 255) % 256; - data[p * 4 + 3] = 255; + data[p * 4 + 2] = 255; } const texture = new THREE.DataTexture(data, W, W, THREE.RGBAFormat); texture.magFilter = THREE.LinearFilter; @@ -103,6 +100,105 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { // streets are at most this far from a block (half an LA freeway); farther is open land const maxHalf = Math.min(250, Math.max(3, (layout.unit * 1.2) / tex)); + // --- centre lines: a street's centre is where its two blocks are equally far. A second sweep finds, + // for each texel, the nearest texel of some *other* block than its nearest one; near the centre the + // distances to the two blocks' outlines then give B = signed distance to the centre line (128 on it, + // 1/32 texel a step: signed, so it filters to 0 on a line that falls between texels), A = 255 where + // a line may be drawn, and a second texture where along it the dashes fall: the length round the + // lower-numbered block's outline, as an angle (cos, sin) so it interpolates without a seam, over + // DASH_TURN dashes + const own = new Int32Array(W * W).fill(-1); + for (let p = 0; p < W * W; p++) if (vx[p] !== INF) own[p] = label[p + vz[p] * W + vx[p]]; + const wx = new Int16Array(W * W).fill(INF), wz = new Int16Array(W * W).fill(INF), other = new Int32Array(W * W).fill(-1); + const relax2 = (p, q, ox, oz) => { + const mine = own[p]; + if (vx[q] !== INF && own[q] !== mine) { + const cx = vx[q] + ox, cz = vz[q] + oz; + if (wx[p] === INF || cx * cx + cz * cz < wx[p] * wx[p] + wz[p] * wz[p]) { wx[p] = cx; wz[p] = cz; other[p] = own[q]; } + } + if (wx[q] !== INF && other[q] !== mine) { + const cx = wx[q] + ox, cz = wz[q] + oz; + if (wx[p] === INF || cx * cx + cz * cz < wx[p] * wx[p] + wz[p] * wz[p]) { wx[p] = cx; wz[p] = cz; other[p] = other[q]; } + } + }; + for (let j = 0; j < W; j++) { + for (let i = 0; i < W; i++) { + const p = j * W + i; + if (i > 0) relax2(p, p - 1, -1, 0); + if (j > 0) { relax2(p, p - W, 0, -1); if (i > 0) relax2(p, p - W - 1, -1, -1); if (i < W - 1) relax2(p, p - W + 1, 1, -1); } + } + for (let i = W - 2; i >= 0; i--) relax2(j * W + i, j * W + i + 1, 1, 0); + } + for (let j = W - 1; j >= 0; j--) { + for (let i = W - 1; i >= 0; i--) { + const p = j * W + i; + if (i < W - 1) relax2(p, p + 1, 1, 0); + if (j < W - 1) { relax2(p, p + W, 0, 1); if (i < W - 1) relax2(p, p + W + 1, 1, 1); if (i > 0) relax2(p, p + W - 1, -1, 1); } + } + for (let i = 1; i < W; i++) relax2(j * W + i, j * W + i - 1, -1, 0); + } + // dashes a little over a quarter of a lot long, a power of two so the shader's pixel sizes divide it + const dash = 2 ** Math.round(Math.log2(layout.unit * 0.28)), DASH_TURN = 16; + const along = new Uint8Array(W * W * 2).fill(128); + const cum = new Map(); // block -> running length round its outline at each corner + const outline = id => { + if (!cum.has(id)) { const poly = blocks[id].poly, c = [0]; for (let k = 0; k < poly.length; k++) { const a = poly[k], b = poly[(k + 1) % poly.length]; c.push(c[k] + Math.hypot(b[0] - a[0], b[1] - a[1])); } cum.set(id, c); } + return cum.get(id); + }; + // the side of a block's outline a street runs along at (x, z): of the sides about as near, the one + // running most along the street (across it is (nx, nz)). Distances are to that side's line, which + // carries on across the mouth of a side street, and so does the centre line; `over` is how far + // (x, z) is past the side's end, out into a junction + const ds = new Float64Array(Math.max(1, ...blocks.map(b => b.poly.length))); + const nearSide = (poly, x, z, nx, nz) => { + const n = poly.length; + let dMin = Infinity; + for (let m = 0; m < n; m++) { + const a = poly[m], b = poly[(m + 1) % n], ex = b[0] - a[0], ez = b[1] - a[1], L2 = ex * ex + ez * ez || 1; + const t = Math.max(0, Math.min(1, ((x - a[0]) * ex + (z - a[1]) * ez) / L2)); + const dx = x - a[0] - ex * t, dz = z - a[1] - ez * t; + ds[m] = dx * dx + dz * dz; + if (ds[m] < dMin) dMin = ds[m]; + } + const near = (Math.sqrt(dMin) + tex) ** 2; + let k = 0, bestAcross = Infinity; + for (let m = 0; m < n; m++) { + if (ds[m] > near) continue; + const a = poly[m], b = poly[(m + 1) % n], L = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1; + const across = Math.abs(((b[0] - a[0]) * nx + (b[1] - a[1]) * nz) / L); + if (across < bestAcross) { bestAcross = across; k = m; } + } + const a = poly[k], b = poly[(k + 1) % poly.length], ex = b[0] - a[0], ez = b[1] - a[1], L = Math.hypot(ex, ez) || 1; + const t = ((x - a[0]) * ex + (z - a[1]) * ez) / L; + return { k, t, d: Math.abs((x - a[0]) * ez - (z - a[1]) * ex) / L, over: Math.max(0, -t, t - L) }; + }; + for (let j = 0; j < W; j++) { + for (let i = 0; i < W; i++) { + const p = j * W + i; + if (label[p] >= 0 || wet[p] || wx[p] === INF || own[p] < 0 || other[p] < 0) continue; + const d1 = D[p], dOther = Math.hypot(wx[p], wz[p]); + if (dOther > maxHalf || d1 + dOther < 3 || dOther - d1 > 10) continue; // no block across; an alley; far off the centre + const x = x0 + (i + 0.5) * tex, z = x0 + (j + 0.5) * tex; + let nx = wx[p] - vx[p], nz = wz[p] - vz[p]; + const nL = Math.hypot(nx, nz) || 1; nx /= nL; nz /= nL; + const a = Math.min(own[p], other[p]), b = Math.max(own[p], other[p]); + const sa = nearSide(blocks[a].poly, x, z, nx, nz), sb = nearSide(blocks[b].poly, x, z, nx, nz); + const off = (sb.d - sa.d) / 2 / tex; + data[p * 4 + 2] = Math.max(0, Math.min(255, Math.round(128 + off * 32))); + // across a side street narrower than this one the line carries on; out in a junction it stops. + // (Only near the line, so the mask's edge never falls where B changes sign) + if (Math.abs(off) < 3 && Math.max(sa.over, sb.over) <= (sa.d + sb.d) / 2 * 0.6) data[p * 4 + 3] = 255; + const turn = (outline(a)[sa.k] + sa.t) / (dash * DASH_TURN) * 2 * Math.PI; + along[p * 2] = Math.round(127.5 + 127.5 * Math.cos(turn)); + along[p * 2 + 1] = Math.round(127.5 + 127.5 * Math.sin(turn)); + } + } + const alongTexture = new THREE.DataTexture(along, W, W, THREE.RGFormat); + alongTexture.magFilter = THREE.LinearFilter; + alongTexture.minFilter = THREE.LinearFilter; + alongTexture.unpackAlignment = 1; + alongTexture.needsUpdate = true; + // --- which block each building stands on, for traffic weights and when the block "opens" const sample = (x, z) => { const i = Math.floor(toTex(x)), j = Math.floor(toTex(z)); @@ -184,7 +280,7 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { const at = (x, z) => sample(x, z); const drivable = (x, z) => { const p = at(x, z); return p >= 0 && label[p] < 0 && !wet[p] && D[p] <= maxHalf + 1; }; const navigable = (x, z) => { const p = at(x, z); return p >= 0 && wet[p] === 1; }; - return { texture, min: new THREE.Vector2(x0, x0), size: S, texel: tex, maxHalf, lanes, curbs, boats, drivable, navigable, blockBorn: born, bornAt }; + return { texture, alongTexture, dash, dashTurn: DASH_TURN, min: new THREE.Vector2(x0, x0), size: S, texel: tex, maxHalf, lanes, curbs, boats, drivable, navigable, blockBorn: born, bornAt }; } // push a polygon outward by `d` (mitred corners, clamped so sharp corners don't spike) diff --git a/web/studio.js b/web/studio.js index a4736bc..08a4436 100644 --- a/web/studio.js +++ b/web/studio.js @@ -270,7 +270,7 @@ export function buildStudio(ctx) { // 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; - const street = streets && { texture: streets.texture, min: streets.min, size: streets.size, maxHalf: streets.maxHalf, asphalt: pathC || new THREE.Color(S.road), grass: new THREE.Color(S.land), mark: pathC || new THREE.Color('#e0d7b8') }; + const street = streets && { texture: streets.texture, along: streets.alongTexture, dash: streets.dash, dashTurn: streets.dashTurn, texel: streets.texel, min: streets.min, size: streets.size, maxHalf: streets.maxHalf, asphalt: pathC || new THREE.Color(S.road), grass: new THREE.Color(S.land), mark: pathC || new THREE.Color('#e0d7b8') }; if (S.pixel && layout) addGroundDetail(groundMat, layout.ground === 'water' ? 'water' : 'street', { shared, street }); const ground = new THREE.Mesh(new THREE.PlaneGeometry(SIZE * 14, SIZE * 14).rotateX(-Math.PI / 2), groundMat); ground.receiveShadow = true; @@ -294,7 +294,7 @@ export function buildStudio(ctx) { // 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; if (trees) scene.add(trees); - if (streets) { streets.drivable = streets.navigable = streets.bornAt = null; streets.curbs = null; streets.lanes = streets.boats = null; streets.texture.onUpdate = t => { t.image.data = null; }; } + if (streets) { streets.drivable = streets.navigable = streets.bornAt = null; streets.curbs = null; streets.lanes = streets.boats = null; streets.texture.onUpdate = streets.alongTexture.onUpdate = t => { t.image.data = null; }; } const roadY = layout?.groundIsWater ? plateStep * 0.8 + 0.05 : 0.05; // ---- district plinths (classic treemap only) diff --git a/web/surfaces.js b/web/surfaces.js index a2dabde..aca07ff 100644 --- a/web/surfaces.js +++ b/web/surfaces.js @@ -113,7 +113,7 @@ const GROUND = { vec2 g = fract(c); diffuseColor.rgb = mix(diffuseColor.rgb, diffuseColor.rgb * 0.86, max(step(g.x, 0.12), step(g.y, 0.12)) * sFade(c));`, // the open ground: asphalt only where streets are (near blocks, from a distance field), a dashed - // centre line found as the ridge of that field, grass everywhere else + // centre line where the street's two blocks are equally far, grass everywhere else street: /* glsl */` vec2 suv = (vSurfWP.xz - uStreetMin) / uStreetSize; vec4 st = texture2D(tStreet, suv); @@ -124,15 +124,15 @@ const GROUND = { // (on the same clock as the block rising beside it, so no road appears before its land) if (inside > 0.5 && D > 0.5 && D <= uMaxHalf && st.g * 0.97 - ${LAND_LEAD.toFixed(3)} + 0.012 <= uTimeline) { col = uAsphalt * (1.0 + (sHash(floor(vSurfWP.xz / P(1.5))) - 0.5) * 0.08); - // across-street direction (towards the nearest block); the centre line is where D peaks across it - float a = st.b * 3.14159265; - vec2 n = vec2(cos(a), sin(a)); - float du = uPx * 0.9 / uStreetSize; - float D1 = texture2D(tStreet, suv + n * du).r * 255.0, D2 = texture2D(tStreet, suv - n * du).r * 255.0; - float ridge = step(D1, D + 0.02) * step(D2, D + 0.02) * step(1.5, D); - vec2 along = vec2(-n.y, n.x); - float dash = step(0.55, fract(dot(vSurfWP.xz, along) / P(8.0))); - col = mix(col, uMark, ridge * dash); + // the centre line: a pixel wide (wider with the street, close up), dashed every 8 pixels or every + // dash, whichever is longer; how far along the street we are comes as an angle over uDashTurn dashes + float off = abs(st.b * 255.0 - 128.0) / 32.0 * uTexel; + if (st.a > 0.5 && off < max(P(1.0), uDash * 0.125) * 0.5) { + vec2 cs = texture2D(tAlong, suv).rg * 2.0 - 1.0; + float turn = atan(cs.y, cs.x) / 6.28318531; + float dashes = uDashTurn / min(max(P(8.0), uDash) / uDash, uDashTurn); + col = mix(col, uMark, step(0.55, fract(turn * dashes))); + } } diffuseColor.rgb = col;`, }; @@ -165,9 +165,10 @@ export function addGroundDetail(material, kind, opts) { const s = opts.street; inject(material, { ...opts, withPart: false, fragment: GROUND.street, key: 'street', - decl: '\nuniform sampler2D tStreet; uniform vec2 uStreetMin; uniform float uStreetSize, uMaxHalf; uniform vec3 uAsphalt, uGrass, uMark;', + decl: '\nuniform sampler2D tStreet, tAlong; uniform vec2 uStreetMin; uniform float uStreetSize, uMaxHalf, uTexel, uDash, uDashTurn; uniform vec3 uAsphalt, uGrass, uMark;', uniforms: { - tStreet: { value: s.texture }, uStreetMin: { value: s.min }, uStreetSize: { value: s.size }, + tStreet: { value: s.texture }, tAlong: { value: s.along }, uStreetMin: { value: s.min }, uStreetSize: { value: s.size }, + uTexel: { value: s.texel }, uDash: { value: s.dash }, uDashTurn: { value: s.dashTurn }, uMaxHalf: { value: s.maxHalf }, uAsphalt: { value: s.asphalt }, uGrass: { value: s.grass }, uMark: { value: s.mark }, }, }); From b8dc3561c71da0d4cd4d18a53ebacc7435bfe224 Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Tue, 3 Nov 2026 00:46:09 -0500 Subject: [PATCH 2/6] Streets: find centre lines in a band, not over the whole field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The second distance sweep that told each texel which block is across the street ran over the whole field: on postgres (2555² texels) it took ~320 ms and ~90 MB of arrays, to be read only near street centres. Now the centre is found where two street texels side by side have different nearest blocks, and that block pair spreads 8 texels either side; only that band is measured and drawn. Streets build in 72 ms on flask (was 113) and 186 ms on postgres (was ~730), and the field keeps one 16-bit array instead of four. The two blocks must face each other, both where a line starts and at every texel drawn. Two blocks side by side meet along a boundary that runs on out into the street they both front; without the check that boundary drew false lines across it. With it, the wide street past the ends of flask's rows gets one continuous centre line. Co-Authored-By: Claude Opus 5.5 --- web/streets.js | 112 +++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 51 deletions(-) diff --git a/web/streets.js b/web/streets.js index 38cb086..90dea61 100644 --- a/web/streets.js +++ b/web/streets.js @@ -100,42 +100,53 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { // streets are at most this far from a block (half an LA freeway); farther is open land const maxHalf = Math.min(250, Math.max(3, (layout.unit * 1.2) / tex)); - // --- centre lines: a street's centre is where its two blocks are equally far. A second sweep finds, - // for each texel, the nearest texel of some *other* block than its nearest one; near the centre the - // distances to the two blocks' outlines then give B = signed distance to the centre line (128 on it, - // 1/32 texel a step: signed, so it filters to 0 on a line that falls between texels), A = 255 where - // a line may be drawn, and a second texture where along it the dashes fall: the length round the - // lower-numbered block's outline, as an angle (cos, sin) so it interpolates without a seam, over - // DASH_TURN dashes - const own = new Int32Array(W * W).fill(-1); - for (let p = 0; p < W * W; p++) if (vx[p] !== INF) own[p] = label[p + vz[p] * W + vx[p]]; - const wx = new Int16Array(W * W).fill(INF), wz = new Int16Array(W * W).fill(INF), other = new Int32Array(W * W).fill(-1); - const relax2 = (p, q, ox, oz) => { - const mine = own[p]; - if (vx[q] !== INF && own[q] !== mine) { - const cx = vx[q] + ox, cz = vz[q] + oz; - if (wx[p] === INF || cx * cx + cz * cz < wx[p] * wx[p] + wz[p] * wz[p]) { wx[p] = cx; wz[p] = cz; other[p] = own[q]; } - } - if (wx[q] !== INF && other[q] !== mine) { - const cx = wx[q] + ox, cz = wz[q] + oz; - if (wx[p] === INF || cx * cx + cz * cz < wx[p] * wx[p] + wz[p] * wz[p]) { wx[p] = cx; wz[p] = cz; other[p] = other[q]; } - } + // --- centre lines: a street's centre is where its two blocks are equally far, so it runs where the + // nearest block changes from one texel to the next. From there a band a few texels wide either side + // learns which block is across the street; in it the distances to the two blocks' outlines give + // B = signed distance to the centre line (128 on it, 1/32 texel a step: signed, so it filters to 0 + // on a line that falls between texels), A = 255 where a line may be drawn, and a second texture + // where along it the dashes fall: the length round the lower-numbered block's outline, as an angle + // (cos, sin) so it interpolates without a seam, over DASH_TURN dashes + const own = p => (vx[p] === INF ? -1 : label[p + vz[p] * W + vx[p]]); + const street = p => label[p] < 0 && D[p] <= maxHalf; + const other = new (blocks.length < 32767 ? Int16Array : Int32Array)(W * W).fill(-1); + const band = []; + // the centre: two street texels side by side whose nearest blocks differ, and face each other (not + // two blocks side by side, whose boundary runs on out into the street they both front) + const meet = (p, q) => { + if (!street(q)) return; + const mine = own(p), theirs = own(q); + if (mine < 0 || theirs < 0 || theirs === mine) return; + if (vx[p] * vx[q] + vz[p] * vz[q] > -0.5 * Math.hypot(vx[p], vz[p]) * Math.hypot(vx[q], vz[q])) return; + if (other[p] < 0) { other[p] = theirs; band.push(p); } + if (other[q] < 0) { other[q] = mine; band.push(q); } }; for (let j = 0; j < W; j++) { for (let i = 0; i < W; i++) { const p = j * W + i; - if (i > 0) relax2(p, p - 1, -1, 0); - if (j > 0) { relax2(p, p - W, 0, -1); if (i > 0) relax2(p, p - W - 1, -1, -1); if (i < W - 1) relax2(p, p - W + 1, 1, -1); } + if (!street(p)) continue; + if (i < W - 1) meet(p, p + 1); + if (j < W - 1) meet(p, p + W); } - for (let i = W - 2; i >= 0; i--) relax2(j * W + i, j * W + i + 1, 1, 0); } - for (let j = W - 1; j >= 0; j--) { - for (let i = W - 1; i >= 0; i--) { - const p = j * W + i; - if (i < W - 1) relax2(p, p + 1, 1, 0); - if (j < W - 1) { relax2(p, p + W, 0, 1); if (i < W - 1) relax2(p, p + W + 1, 1, 1); if (i > 0) relax2(p, p + W - 1, -1, 1); } + // out from the centre: far enough that every texel near the line has its neighbours filled in + const spread = (p, q) => { + if (other[q] >= 0 || !street(q)) return; + const theirs = own(q), across = theirs === own(p) ? other[p] : own(p); + if (theirs < 0 || across === theirs) return; + other[q] = across; + band.push(q); + }; + for (let from = 0, depth = 0; depth < 8; depth++) { + const to = band.length; + for (let n = from; n < to; n++) { + const p = band[n], i = p % W; + if (i > 0) spread(p, p - 1); + if (i < W - 1) spread(p, p + 1); + if (p >= W) spread(p, p - W); + if (p < W * W - W) spread(p, p + W); } - for (let i = 1; i < W; i++) relax2(j * W + i, j * W + i - 1, -1, 0); + from = to; } // dashes a little over a quarter of a lot long, a power of two so the shader's pixel sizes divide it const dash = 2 ** Math.round(Math.log2(layout.unit * 0.28)), DASH_TURN = 16; @@ -169,29 +180,28 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { if (across < bestAcross) { bestAcross = across; k = m; } } const a = poly[k], b = poly[(k + 1) % poly.length], ex = b[0] - a[0], ez = b[1] - a[1], L = Math.hypot(ex, ez) || 1; - const t = ((x - a[0]) * ex + (z - a[1]) * ez) / L; - return { k, t, d: Math.abs((x - a[0]) * ez - (z - a[1]) * ex) / L, over: Math.max(0, -t, t - L) }; + const t = ((x - a[0]) * ex + (z - a[1]) * ez) / L, side = ((x - a[0]) * ez - (z - a[1]) * ex) / L; + // (fx, fz): from (x, z) towards the side + return { k, t, d: Math.abs(side), fx: -Math.sign(side) * ez / L, fz: Math.sign(side) * ex / L, over: Math.max(0, -t, t - L) }; }; - for (let j = 0; j < W; j++) { - for (let i = 0; i < W; i++) { - const p = j * W + i; - if (label[p] >= 0 || wet[p] || wx[p] === INF || own[p] < 0 || other[p] < 0) continue; - const d1 = D[p], dOther = Math.hypot(wx[p], wz[p]); - if (dOther > maxHalf || d1 + dOther < 3 || dOther - d1 > 10) continue; // no block across; an alley; far off the centre - const x = x0 + (i + 0.5) * tex, z = x0 + (j + 0.5) * tex; - let nx = wx[p] - vx[p], nz = wz[p] - vz[p]; - const nL = Math.hypot(nx, nz) || 1; nx /= nL; nz /= nL; - const a = Math.min(own[p], other[p]), b = Math.max(own[p], other[p]); - const sa = nearSide(blocks[a].poly, x, z, nx, nz), sb = nearSide(blocks[b].poly, x, z, nx, nz); - const off = (sb.d - sa.d) / 2 / tex; - data[p * 4 + 2] = Math.max(0, Math.min(255, Math.round(128 + off * 32))); - // across a side street narrower than this one the line carries on; out in a junction it stops. - // (Only near the line, so the mask's edge never falls where B changes sign) - if (Math.abs(off) < 3 && Math.max(sa.over, sb.over) <= (sa.d + sb.d) / 2 * 0.6) data[p * 4 + 3] = 255; - const turn = (outline(a)[sa.k] + sa.t) / (dash * DASH_TURN) * 2 * Math.PI; - along[p * 2] = Math.round(127.5 + 127.5 * Math.cos(turn)); - along[p * 2 + 1] = Math.round(127.5 + 127.5 * Math.sin(turn)); - } + for (const p of band) { + if (wet[p]) continue; + const i = p % W, j = (p - i) / W; + const x = x0 + (i + 0.5) * tex, z = x0 + (j + 0.5) * tex; + const nL = Math.hypot(vx[p], vz[p]) || 1, nx = vx[p] / nL, nz = vz[p] / nL; // across the street + const a = Math.min(own(p), other[p]), b = Math.max(own(p), other[p]); + const sa = nearSide(blocks[a].poly, x, z, nx, nz), sb = nearSide(blocks[b].poly, x, z, nx, nz); + const width = (sa.d + sb.d) / tex; + if (width > maxHalf * 2 || width < 3) continue; // no block across; an alley + if (sa.fx * sb.fx + sa.fz * sb.fz > -0.5) continue; // not across the street from each other + const off = (sb.d - sa.d) / 2 / tex; + data[p * 4 + 2] = Math.max(0, Math.min(255, Math.round(128 + off * 32))); + // across a side street narrower than this one the line carries on; out in a junction it stops. + // (Only near the line, so the mask's edge never falls where B changes sign) + if (Math.abs(off) < 3 && Math.max(sa.over, sb.over) <= (sa.d + sb.d) / 2 * 0.6) data[p * 4 + 3] = 255; + const turn = (outline(a)[sa.k] + sa.t) / (dash * DASH_TURN) * 2 * Math.PI; + along[p * 2] = Math.round(127.5 + 127.5 * Math.cos(turn)); + along[p * 2 + 1] = Math.round(127.5 + 127.5 * Math.sin(turn)); } const alongTexture = new THREE.DataTexture(along, W, W, THREE.RGFormat); alongTexture.magFilter = THREE.LinearFilter; From 7eb25bea040c92c65cc2c28132e74eb12c6571f0 Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Tue, 3 Nov 2026 00:59:17 -0500 Subject: [PATCH 3/6] Streets: centre lines on one-sided roads; cars keep off the water A street with a block on one side only had no centre line: the ring road round the city's edge, and roads along a river bank. Those now get one too, halfway between the block's side and the far edge: the river bank, or where the asphalt stops on open land. Lanes follow it, so cars drive their side of the road rather than hugging the block. A car could show on the water where a street ran along a river: its centre was on the street but its front or back hung over the bank (313 of 510,575 car positions on postgres). Each route sample now keeps how far it is from the water, and a car shows only where all of it is clear. On postgres none are over water now, with 175 cars (was 171). Two-sided centre lines also stop where the water comes between the two blocks, so a river no longer gets a line drawn across it. Streets build in 116 ms on flask (was 72) and 232 ms on postgres (was 186). Co-Authored-By: Claude Opus 5.5 --- web/streets.js | 103 +++++++++++++++++++++++++++++++++++++++++++------ web/studio.js | 2 +- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/web/streets.js b/web/streets.js index 90dea61..da743e0 100644 --- a/web/streets.js +++ b/web/streets.js @@ -148,6 +148,46 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { } from = to; } + // how far each street texel is from the water, in texels (8-neighbour steps, never more than the + // true distance; 255 = farther than a street is wide): which streets run along a bank, and how near + // the water a car may come + const toWater = new Uint8Array(W * W).fill(255); + let shore = []; + for (let p = 0; p < W * W; p++) { + if (wet[p] || !street(p)) continue; + const i = p % W; + if ((i > 0 && wet[p - 1]) || (i < W - 1 && wet[p + 1]) || (p >= W && wet[p - W]) || (p < W * W - W && wet[p + W])) { toWater[p] = 1; shore.push(p); } + } + for (let d = 2; d <= Math.min(254, maxHalf) && shore.length; d++) { + const next = []; + for (const p of shore) { + const i = p % W; + for (let dj = -W; dj <= W; dj += W) { + for (let di = -1; di <= 1; di++) { + const q = p + dj + di; + if ((di < 0 && i === 0) || (di > 0 && i === W - 1) || q < 0 || q >= W * W || toWater[q] !== 255 || wet[q] || !street(q)) continue; + toWater[q] = d; + next.push(q); + } + } + } + shore = next; + } + // the nearest point of a river bank or shore to (x, z): how far, and which way (fx, fz) + const banks = layout.groundIsWater ? [...layout.land, ...layout.shore, ...layout.water] : layout.water; + const bank = (x, z) => { + let best = Infinity, fx = 0, fz = 0; + for (const poly of banks) { + for (let m = 0; m < poly.length; m++) { + const a = poly[m], b = poly[(m + 1) % poly.length], ex = b[0] - a[0], ez = b[1] - a[1], L2 = ex * ex + ez * ez || 1; + const t = Math.max(0, Math.min(1, ((x - a[0]) * ex + (z - a[1]) * ez) / L2)); + const px = a[0] + ex * t - x, pz = a[1] + ez * t - z; + if (px * px + pz * pz < best) { best = px * px + pz * pz; fx = px; fz = pz; } + } + } + const d = Math.sqrt(best); + return { d, fx: fx / (d || 1), fz: fz / (d || 1) }; + }; // dashes a little over a quarter of a lot long, a power of two so the shader's pixel sizes divide it const dash = 2 ** Math.round(Math.log2(layout.unit * 0.28)), DASH_TURN = 16; const along = new Uint8Array(W * W * 2).fill(128); @@ -194,6 +234,11 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { const width = (sa.d + sb.d) / tex; if (width > maxHalf * 2 || width < 3) continue; // no block across; an alley if (sa.fx * sb.fx + sa.fz * sb.fz > -0.5) continue; // not across the street from each other + // a river between them: the street runs along its bank (below), not across the river + if (toWater[p] * tex < Math.max(sa.d, sb.d)) { + const w = bank(x, z); + if ((w.fx * sa.fx + w.fz * sa.fz > 0.5 && w.d < sa.d) || (w.fx * sb.fx + w.fz * sb.fz > 0.5 && w.d < sb.d)) continue; + } const off = (sb.d - sa.d) / 2 / tex; data[p * 4 + 2] = Math.max(0, Math.min(255, Math.round(128 + off * 32))); // across a side street narrower than this one the line carries on; out in a junction it stops. @@ -203,6 +248,32 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { along[p * 2] = Math.round(127.5 + 127.5 * Math.cos(turn)); along[p * 2 + 1] = Math.round(127.5 + 127.5 * Math.sin(turn)); } + // streets with a block on one side only: the asphalt stops maxHalf from the block where open land + // lies beyond, or at the bank of a river; the centre line runs halfway across + for (let p = 0; p < W * W; p++) { + if (data[p * 4 + 2] !== 255 || wet[p] || !street(p) || own(p) < 0) continue; + const d = D[p], w = toWater[p]; + const byLand = Math.abs(d - maxHalf / 2) < 6, byWater = w < 255 && w >= d / 1.42 - 6 && w <= d + 6; + if (!byLand && !byWater) continue; + const i = p % W, j = (p - i) / W; + const x = x0 + (i + 0.5) * tex, z = x0 + (j + 0.5) * tex; + const nL = Math.hypot(vx[p], vz[p]) || 1, nx = vx[p] / nL, nz = vz[p] / nL; // towards the block + const a = own(p), sa = nearSide(blocks[a].poly, x, z, nx, nz); + let far = Infinity; + if (byWater) { const b = bank(x, z); if (b.fx * sa.fx + b.fz * sa.fz < -0.5) far = b.d; } + if (byLand) { + // open land past the far edge (not a wide street with a block across it) + const qi = Math.round(i - nx * (maxHalf - d + 2)), qj = Math.round(j - nz * (maxHalf - d + 2)), q = qj * W + qi; + if (qi >= 0 && qj >= 0 && qi < W && qj < W && label[q] < 0 && D[q] > maxHalf) far = Math.min(far, maxHalf * tex - sa.d); + } + if (!(far < Infinity) || (sa.d + far) / tex < 3) continue; + const off = (far - sa.d) / 2 / tex; + data[p * 4 + 2] = Math.max(0, Math.min(255, Math.round(128 + off * 32))); + if (Math.abs(off) < 3 && sa.over <= (sa.d + far) / 2 * 0.6) data[p * 4 + 3] = 255; + const turn = (outline(a)[sa.k] + sa.t) / (dash * DASH_TURN) * 2 * Math.PI; + along[p * 2] = Math.round(127.5 + 127.5 * Math.cos(turn)); + along[p * 2 + 1] = Math.round(127.5 + 127.5 * Math.sin(turn)); + } const alongTexture = new THREE.DataTexture(along, W, W, THREE.RGFormat); alongTexture.magFilter = THREE.LinearFilter; alongTexture.minFilter = THREE.LinearFilter; @@ -252,27 +323,31 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { let area = 0; for (let k = 0, m = poly.length - 1; k < poly.length; m = k++) area += (poly[m][0] - poly[k][0]) * (poly[m][1] + poly[k][1]); const out = area > 0 ? 1 : -1; // sign that turns an edge normal outward - const gaps = []; + const gaps = [], roads = []; for (let k = 0; k < poly.length; k++) { const [ax, az] = poly[k], [bx, bz] = poly[(k + 1) % poly.length]; const L = Math.hypot(bx - ax, bz - az); if (L < tex * 2) continue; const nx = out * (bz - az) / L, nz = -out * (bx - ax) / L; - // walk outward until the distance stops growing: that's the street's centre - let best = 0; - for (let s = 1; s < maxHalf; s++) { + // walk outward until the distance stops growing: that's the street's centre. A street with no + // block across has its centre halfway to the river bank, or to where the asphalt stops + let best = 0, road = 0, s = 1; + for (; s < maxHalf; s++) { const p = sample((ax + bx) / 2 + nx * s * tex, (az + bz) / 2 + nz * s * tex); if (p < 0 || label[p] >= 0) break; + if (wet[p]) { road = s / 2; break; } if (D[p] < best - 0.5) break; best = Math.max(best, D[p]); } gaps.push(best); + roads.push(road || (s >= maxHalf ? maxHalf / 2 : best)); } if (!gaps.length) return; gaps.sort((p, q) => p - q); - const half = gaps[Math.floor(gaps.length * 0.3)]; + roads.sort((p, q) => p - q); + const half = gaps[Math.floor(gaps.length * 0.3)], road = roads[Math.floor(roads.length * 0.3)]; if (half < 1.5) return; // alleys too narrow for a car - lanes.push({ pts: offset(poly, out, half * tex * 0.5), lane: half * tex * 0.5, weight: 0.15 + 0.85 * Math.sqrt(weight[id] / maxW), born: Math.max(0, born[id] - LAND_LEAD) }); // traffic comes with the road + if (road >= 1.5) lanes.push({ pts: offset(poly, out, road * tex * 0.5), lane: road * tex * 0.5, weight: 0.15 + 0.85 * Math.sqrt(weight[id] / maxW), born: Math.max(0, born[id] - LAND_LEAD) }); // traffic comes with the road // a line just off the kerb, where street trees go curbs.push({ pts: offset(poly, out, half * tex * 0.2), half: half * tex, born: born[id] }); }); @@ -290,7 +365,9 @@ export function buildStreets({ layout, B, files, inDeg, SIZE, t01 }) { const at = (x, z) => sample(x, z); const drivable = (x, z) => { const p = at(x, z); return p >= 0 && label[p] < 0 && !wet[p] && D[p] <= maxHalf + 1; }; const navigable = (x, z) => { const p = at(x, z); return p >= 0 && wet[p] === 1; }; - return { texture, alongTexture, dash, dashTurn: DASH_TURN, min: new THREE.Vector2(x0, x0), size: S, texel: tex, maxHalf, lanes, curbs, boats, drivable, navigable, blockBorn: born, bornAt }; + // how far a car at (x, z) is from the water, at least + const clearance = (x, z) => { const p = at(x, z); return p < 0 || toWater[p] === 255 ? Infinity : (toWater[p] - 0.5) * tex; }; + return { texture, alongTexture, dash, dashTurn: DASH_TURN, min: new THREE.Vector2(x0, x0), size: S, texel: tex, maxHalf, lanes, curbs, boats, drivable, navigable, clearance, blockBorn: born, bornAt }; } // push a polygon outward by `d` (mitred corners, clamped so sharp corners don't spike) @@ -358,7 +435,8 @@ export function buildTraffic({ streets, unit }) { const routes = []; const prep = (r, kind) => { const step = Math.max(streets.texel, r.lane * 0.5); - const pts = [], ok = []; + // each sample also keeps how far it is from the water: a car shows only where all of it is clear + const pts = [], ok = [], room = []; for (let k = 0; k < r.pts.length; k++) { const a = r.pts[k], b = r.pts[(k + 1) % r.pts.length]; const L = Math.hypot(b[0] - a[0], b[1] - a[1]), n = Math.max(1, Math.round(L / step)); @@ -366,11 +444,12 @@ export function buildTraffic({ streets, unit }) { const x = a[0] + (b[0] - a[0]) * t / n, z = a[1] + (b[1] - a[1]) * t / n; pts.push([x, z]); ok.push(kind === 'boat' ? streets.navigable(x, z) : streets.drivable(x, z)); + room.push(kind === 'boat' ? Infinity : streets.clearance(x, z)); } } const good = ok.filter(Boolean).length; if (good < 3) return; - routes.push({ ...r, kind, pts, ok, step: null, cum: null, good }); + routes.push({ ...r, kind, pts, ok, room, step: null, cum: null, good }); }; streets.lanes.forEach(r => prep(r, 'car')); streets.boats.forEach(r => prep(r, 'boat')); @@ -405,9 +484,9 @@ export function buildTraffic({ streets, unit }) { const remap = new Map(used.map((ri, k) => [ri, k])); const kept = used.map(ri => { const r = routes[ri], n = r.pts.length; - const xy = new Float32Array(n * 2), cum = new Float32Array(r.cum), ok = new Uint8Array(n); + const xy = new Float32Array(n * 2), cum = new Float32Array(r.cum), ok = new Uint8Array(n), room = new Float32Array(r.room); r.pts.forEach(([x, z], k) => { xy[k * 2] = x; xy[k * 2 + 1] = z; ok[k] = r.ok[k] ? 1 : 0; }); - return { kind: r.kind, born: r.born, L: r.L, n, xy, cum, ok }; + return { kind: r.kind, born: r.born, L: r.L, n, xy, cum, ok, room }; }); for (const v of vehicles) v.ri = remap.get(v.ri); routes.length = 0; @@ -439,7 +518,7 @@ export function buildTraffic({ streets, unit }) { let lo = 0, hi = r.cum.length - 1; while (hi - lo > 1) { const mid = (lo + hi) >> 1; if (r.cum[mid] <= s) lo = mid; else hi = mid; } const nx = (lo + 1) % r.n; - if (!r.ok[lo] || !r.ok[nx]) { mesh.setMatrixAt(k, hidden); return; } + if (!r.ok[lo] || !r.ok[nx] || r.room[lo] < v.len / 2 || r.room[nx] < v.len / 2) { mesh.setMatrixAt(k, hidden); return; } const ax = r.xy[lo * 2], az = r.xy[lo * 2 + 1], bx = r.xy[nx * 2], bz = r.xy[nx * 2 + 1]; const t = (s - r.cum[lo]) / Math.max(1e-6, r.cum[lo + 1] - r.cum[lo]); pos.set(ax + (bx - ax) * t, v.type === 'boat' ? 0 : baseY, az + (bz - az) * t); diff --git a/web/studio.js b/web/studio.js index 08a4436..c49bc98 100644 --- a/web/studio.js +++ b/web/studio.js @@ -294,7 +294,7 @@ export function buildStudio(ctx) { // 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; if (trees) scene.add(trees); - if (streets) { streets.drivable = streets.navigable = streets.bornAt = null; streets.curbs = null; streets.lanes = streets.boats = null; streets.texture.onUpdate = streets.alongTexture.onUpdate = t => { t.image.data = null; }; } + if (streets) { streets.drivable = streets.navigable = streets.clearance = streets.bornAt = null; streets.curbs = null; streets.lanes = streets.boats = null; streets.texture.onUpdate = streets.alongTexture.onUpdate = t => { t.image.data = null; }; } const roadY = layout?.groundIsWater ? plateStep * 0.8 + 0.05 : 0.05; // ---- district plinths (classic treemap only) From 76c2720c8b6f94d0758e205125963b49d694708f Mon Sep 17 00:00:00 2001 From: Fengzdadi <453788063@qq.com> Date: Tue, 3 Nov 2026 01:10:09 -0500 Subject: [PATCH 4/6] Page: a person view of who built what The colour map of builders is gone. In its place, a "who built this city" panel (B, or the people button) lists everyone by buildings built, with their GitHub face. Picking someone lights their buildings: the rest fade to soft grey ghosts, theirs flash once, the camera frames them, and small arrows mark them when there are 24 or fewer. Their card shows commits, years, where they built, and two tiles to switch between buildings they built and buildings where they made the most commits. ?by=[&most] opens the city on that person. Building tooltips name who built it and who made the most commits, with faces. District labels carry the founder's face. The scanner keeps, per author, the latest commit hash, a login when the email is a GitHub noreply address, a commit count and first/last dates, plus the repo's github slug from remote.origin.url. Emails still never leave the scan. Each building also records who made the most of its commits (only when that isn't its builder). Snapshots moved only in that field. A person's buildings share as their own card (landscape, square and story), and on the hosted site ?by= links preview with that card, drawn headless and stored in R2 per person. Co-Authored-By: Claude Opus 5.5 --- deploy/cloudflare/worker/index.js | 77 +++- src/scan.mjs | 65 ++- test/history.mjs | 25 ++ test/snapshot.mjs | 10 +- test/snapshots/You-Dont-Know-JS.json | 6 +- test/snapshots/axum.json | 126 +++--- test/snapshots/flask.json | 166 ++++---- test/snapshots/gallery.json | 544 +++++++++++++------------- test/snapshots/gson.json | 404 +++++++++---------- test/snapshots/jq.json | 78 ++-- test/snapshots/ky.json | 74 ++-- test/snapshots/nanoGPT.json | 2 +- test/snapshots/terraform-aws-eks.json | 122 +++--- test/snapshots/uuid.json | 8 +- web/architecture.js | 8 +- web/card.js | 118 +++++- web/index.html | 66 +++- web/main.js | 350 ++++++++++++++--- web/studio.js | 8 +- 19 files changed, 1374 insertions(+), 883 deletions(-) diff --git a/deploy/cloudflare/worker/index.js b/deploy/cloudflare/worker/index.js index f6438c7..7cf4d1d 100644 --- a/deploy/cloudflare/worker/index.js +++ b/deploy/cloudflare/worker/index.js @@ -1,7 +1,8 @@ // gitropolis.dev: github.com/owner/repo → gitropolis.dev/owner/repo. // // Link previews: /owner/repo pages carry og/twitter tags pointing at /api/card/owner/repo.png, a share -// card drawn by the page itself in a headless browser (?card=og) and kept in R2 per commit. +// card drawn by the page itself in a headless browser (?card=og) and kept in R2 per commit. A link to +// one person's part (?by=Name) previews their card: /api/card/owner/repo.png?by=Name. // // Static pages are the CLI's own web/ folder. The one dynamic route, /api/city/owner/repo, finds the // default branch's head commit on GitHub, serves the city for that commit from R2 if it has been @@ -87,21 +88,28 @@ async function city(owner, repo, req, env, ctx) { const CARD = 2; // bump when the card's look (or its encoding) changes const esc = t => t.replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); const cardKey = (info, v = CARD) => `cards/v${SCANNER}.${v}/${info.full.toLowerCase()}/${info.sha}.png`; +// a person's card: their name hashed, so any name makes a plain key +const personKey = async (info, who) => { + const h = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(`${who.by}\n${who.most}`))); + return `cards/v${SCANNER}.${CARD}/${info.full.toLowerCase()}/${info.sha}/by/${[...h.slice(0, 12)].map(b => b.toString(16).padStart(2, '0')).join('')}.png`; +}; const cityKey = info => `cities/v${SCANNER}/${info.full.toLowerCase()}/${info.sha}.json`; const sleep = ms => new Promise(r => setTimeout(r, ms)); // Draw a repo's share card: the page itself, in a headless browser, in its ?card=og mode. Kept in R2. -async function renderCard(info, origin, env) { +// (who: one person's card instead, with their face looked up here, where GitHub can be asked with a token) +async function renderCard(info, origin, env, who = null) { let browser; try { browser = await puppeteer.launch(env.BROWSER); const page = await browser.newPage(); - await page.goto(new URL(`/${info.full}?card=og`, origin).toString(), { waitUntil: 'domcontentloaded', timeout: 30_000 }); + const q = new URLSearchParams({ card: 'og', ...(who && { by: who.by, ...(who.most && { most: '1' }), ...(who.face && { face: who.face }) }) }); + await page.goto(new URL(`/${info.full}?${q}`, origin).toString(), { waitUntil: 'domcontentloaded', timeout: 30_000 }); await page.waitForFunction('window.__card', { timeout: 60_000 }); const url = await page.evaluate(() => window.__card); if (!url.startsWith('data:image/png;base64,')) throw new Error(url); const png = Uint8Array.from(atob(url.slice(22)), c => c.charCodeAt(0)); - await env.CITIES.put(cardKey(info), png, { httpMetadata: { contentType: 'image/png' } }); + await env.CITIES.put(who ? who.key : cardKey(info), png, { httpMetadata: { contentType: 'image/png' } }); return png; } finally { await browser?.close().catch(() => {}); @@ -113,11 +121,11 @@ async function renderCard(info, origin, env) { // already being drawn, or null when it couldn't be. (The mark is in this data centre's cache, so // crawlers arriving through different data centres can still each draw one.) const BUSY = Symbol('busy'); -async function drawOnce(info, origin, env) { - const busy = new Request(`https://card.busy/${info.full.toLowerCase()}/${info.sha}`); +async function drawOnce(info, origin, env, who = null) { + const busy = new Request(`https://card.busy/${who ? who.key : `${info.full.toLowerCase()}/${info.sha}`}`); if (await caches.default.match(busy)) return BUSY; await caches.default.put(busy, new Response('1', { headers: { 'cache-control': 'max-age=90' } })); - try { return await renderCard(info, origin, env); } catch (err) { console.log('card failed', info.full, String(err)); return null; } + try { return await renderCard(info, origin, env, who); } catch (err) { console.log('card failed', info.full, String(err)); return null; } } // Link-preview crawlers don't wait long, and one that times out remembers "no picture" for days. So a @@ -136,6 +144,8 @@ async function card(owner, repo, req, env, ctx) { try { info = await lookup(owner, repo, env, ctx); } catch { return fallback(); } if (info.missing || info.private) return fallback(); const headers = { 'content-type': 'image/png', 'cache-control': 'public, max-age=3600' }; + const by = new URL(req.url).searchParams.get('by'); + if (by) return personCard(info, by, new URL(req.url).searchParams.has('most'), req, env, ctx); const hit = await env.CITIES.get(cardKey(info)); if (hit) return new Response(hit.body, { headers }); if (!env.BROWSER || !(await env.CITIES.head(cityKey(info)))) return fallback(); @@ -155,6 +165,42 @@ async function card(owner, repo, req, env, ctx) { return png && png !== BUSY ? new Response(png, { headers }) : fallback(); } +// One person's card. Drawn when first asked for (there's one per person, too many to draw ahead; the +// page asks for it as soon as someone opens the share dialog on their buildings, so it's ready by the +// time the link is posted), and only for someone who built part of a city that has been built. +async function personCard(info, by, most, req, env, ctx) { + const repoCard = () => Response.redirect(new URL(`/api/card/${info.full}.png`, req.url).toString(), 302); + const headers = { 'content-type': 'image/png', 'cache-control': 'public, max-age=3600' }; + if (by.length > 200) return repoCard(); + const who = { by, most, key: await personKey(info, { by, most }) }; + const hit = await env.CITIES.get(who.key); + if (hit) return new Response(hit.body, { headers }); + const city = env.BROWSER && await env.CITIES.get(cityKey(info)); + if (!city) return repoCard(); + const { authors = [], faces = [] } = await city.json(); + const k = authors.indexOf(by); + if (k < 0) return repoCard(); + who.face = await faceOf(info, faces[k], env).catch(() => null); + let png = await drawOnce(info, req.url, env, who); + for (let i = 0; png === BUSY && i < 12; i++) { + await sleep(2000); + const done = await env.CITIES.get(who.key); + if (done) png = await done.arrayBuffer(); + } + return png && png !== BUSY ? new Response(png, { headers }) : repoCard(); +} +// "login/id" for a person's GitHub account, from the account that made a commit of theirs: the page +// draws their avatar from it (it can't ask GitHub itself in the headless browser: no token there) +async function faceOf(info, face, env) { + if (!face?.c) return null; + const r = await fetch(`https://api.github.com/repos/${info.full}/commits/${face.c}`, { + headers: { 'user-agent': 'gitropolis', accept: 'application/vnd.github+json', ...(env.GITHUB_TOKEN && { authorization: `Bearer ${env.GITHUB_TOKEN}` }) }, + }); + if (!r.ok) return null; + const a = (await r.json()).author; + return a?.login && Number.isInteger(a.id) ? `${a.login}/${a.id}` : null; +} + // what a badge needs to know about a built city, kept beside it (a city file can run to megabytes), // and again as the repo's latest, which a badge can read without asking GitHub for the head commit const summaryKey = info => `meta/v${SCANNER}/${info.full.toLowerCase()}/${info.sha}.json`; @@ -198,18 +244,23 @@ async function badge(owner, repo, url, env, ctx) { // /owner/repo: the page, with link-preview tags for this repo async function page(owner, repo, req, env) { const res = await env.ASSETS.fetch(new Request(new URL('/', req.url), req)); - const name = `${owner}/${repo}`, url = new URL(`/${name}`, req.url).toString(); - const image = new URL(`/api/card/${name}.png`, req.url).toString(); - const desc = `github.com/${name} as a pixel-art city, built from its code and git history.`; + // ?by=Name: a link to one person's part, previewed with their card + const asked = new URL(req.url).searchParams, by = asked.get('by')?.slice(0, 200), most = by && asked.has('most'); + const q = by ? `?${new URLSearchParams({ by, ...(most && { most: '' }) })}`.replace(/=$/, '') : ''; + const name = `${owner}/${repo}`, url = new URL(`/${name}${q}`, req.url).toString(); + const image = new URL(`/api/card/${name}.png${q}`, req.url).toString(); + const desc = by ? `${by}'s buildings in github.com/${name}, a pixel-art city built from its code and git history.` + : `github.com/${name} as a pixel-art city, built from its code and git history.`; + const title = by ? `${by} in ${name} · gitropolis` : `${name} · gitropolis`; const tags = [ ['property', 'og:type', 'website'], ['property', 'og:site_name', 'gitropolis'], ['property', 'og:url', url], - ['property', 'og:title', `${name} · gitropolis`], ['property', 'og:description', desc], + ['property', 'og:title', title], ['property', 'og:description', desc], ['property', 'og:image', image], ['property', 'og:image:width', '2400'], ['property', 'og:image:height', '1260'], - ['name', 'twitter:card', 'summary_large_image'], ['name', 'twitter:title', `${name} · gitropolis`], + ['name', 'twitter:card', 'summary_large_image'], ['name', 'twitter:title', title], ['name', 'twitter:description', desc], ['name', 'twitter:image', image], ['name', 'description', desc], ].map(([attr, k, v]) => ``).join(''); return new HTMLRewriter() - .on('title', { element(e) { e.setInnerContent(`${name} · gitropolis`); } }) + .on('title', { element(e) { e.setInnerContent(title); } }) .on('meta[property^="og:"], meta[name^="twitter:"], meta[name="description"]', { element(e) { e.remove(); } }) // the front page's .on('head', { element(e) { e.append(tags, { html: true }); } }) .transform(res); diff --git a/src/scan.mjs b/src/scan.mjs index 5e08065..2289167 100644 --- a/src/scan.mjs +++ b/src/scan.mjs @@ -324,6 +324,22 @@ async function birthsBefore(root, boundary, paths) { } // name: the repository's name, when the folder it's cloned into has another (the hosted scanner's) +// the author with the most of a file's commits (a tie goes to whoever built it, then to whoever came first) +function most(h) { + let top = h.by, n = h.who.get(h.by) || 0; + for (const [a, k] of h.who) if (k > n) { top = a; n = k; } + return top; +} +// "+@users.noreply.github.com", or "@…" from before GitHub put ids in them +const NOREPLY = /^(?:\d+\+)?([A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)@users\.noreply\.github\.com$/i; +// "owner/name" when the repo came from GitHub (its origin remote, over https or ssh) +function githubRepo(root) { + try { + const url = git(root, ['config', '--get', 'remote.origin.url']).trim(); + return /^(?:https?:\/\/(?:[^@/]+@)?|ssh:\/\/git@|git@)github\.com[:/]([\w.-]+\/[\w.-]+?)(?:\.git)?\/?$/.exec(url)?.[1] ?? null; + } catch { return null; } +} + export async function scanRepo(repo, { maxCommits = 20000, name } = {}) { repo = path.resolve(repo); const t0 = Date.now(); @@ -340,21 +356,30 @@ export async function scanRepo(repo, { maxCommits = 20000, name } = {}) { const alias = new Map(); // path at this point in history -> history record const dead = new Map(); // path -> the record of the file deleted there (until a file is added there again) let boundary; // the oldest commit read + const face = new Map(); // author -> { c: their latest commit, u: their GitHub login (when their email gives it), n: their commits, t: [first, last] } const history = new Promise((resolve, reject) => { // quotePath=false: non-ASCII names (中文.md, café.js) come back as raw UTF-8, matching ls-files // renameLimit: pinned to git's own default, so pairMoves knows when git gave up (and an older git, // or one configured otherwise, pairs the same files) const proc = spawn('git', ['-C', root, '-c', 'core.quotePath=false', '-c', `diff.renameLimit=${RENAME_LIMIT}`, 'log', `--max-count=${maxCommits}`, '--raw', '--no-abbrev', - '--format=@@%ct %H %aN', '-M', '--reverse'], { stdio: ['ignore', 'pipe', 'ignore'] }); + '--format=@@%ct%x1f%H%x1f%aE%x1f%aN', '-M', '--reverse'], { stdio: ['ignore', 'pipe', 'ignore'] }); const rl = readline.createInterface({ input: proc.stdout }); let cur = null, added = [], deleted = []; rl.on('line', line => { if (line.startsWith('@@')) { - // "@@