// Streets, sidewalks, curbs, crosswalks, lane lines, the streetcar tracks, the promenade, the // sea walls and the ground of every block, built from the city's grid. Each street between two // crossings is one sloped plane; each crossing is flat. import * as THREE from "three"; import { CALIFORNIA, CITY, CURB, H, LANES, NORTH_SHORE, PIERS, QUAY, XS, ZS, heightAt, lawnAt, surfaceAt } from "../city.js"; import { ASPHALT, CURB_COLOR, PARK, PLAZA, PROMENADE, SIDEWALK, YARD } from "../palette.js"; import { Builder, SURF, surfaceMaterial } from "./kit.js"; const CHUNK = 420; const WHITE = 0xe9e7e1; const YELLOW = 0xe0b440; const RAIL = 0x5d5f63; const TRACKBED = 0x77736d; const PIER_TOP = 0xb8afa0; const PIER_SIDE = 0x4f4b46; const SEAWALL = 0x9a948a; // Which finish each color of the street gets. const GROUND_SURF = new Map([ [ASPHALT, SURF.asphalt], [SIDEWALK, SURF.slab], [PLAZA, SURF.slab], [PROMENADE, SURF.pavers], [PARK, SURF.grass], [YARD, SURF.grass], [0x76935a, SURF.grass], [CURB_COLOR, SURF.concrete], [SEAWALL, SURF.concrete], [0xb3ab9e, SURF.concrete], [PIER_TOP, SURF.planks], [TRACKBED, SURF.track], ]); const groundSurf = (c) => GROUND_SURF.get(c) ?? SURF.none; const markSurf = (c) => (c === WHITE || c === YELLOW ? SURF.paint : SURF.none); class Chunked { constructor() { this.map = new Map(); } at(x, z) { const k = `${Math.floor(x / CHUNK)},${Math.floor(z / CHUNK)}`; if (!this.map.has(k)) this.map.set(k, { ground: new Builder(groundSurf), marks: new Builder(markSurf) }); return this.map.get(k); } } /** A quad turned to face up. */ function up(b, a, c1, c2, d, color) { const ux = c1[0] - a[0], uz = c1[2] - a[2], vx = c2[0] - a[0], vz = c2[2] - a[2]; if (uz * vx - ux * vz >= 0) b.quad(a, c1, c2, d, color); else b.quad(a, d, c2, c1, color); } /** A vertical strip from p to q, `h` tall, turned to face direction (dx, dz). */ function wall(b, p, q, h, dx, dz, color) { const a = [p[0], p[1], p[2]]; const c = [q[0], q[1], q[2]]; const a2 = [p[0], p[1] + h, p[2]]; const c2 = [q[0], q[1] + h, q[2]]; // the normal of quad (a, c, c2, a2) is (c - a) × (c2 - a) const nx = -(c[2] - a[2]) * h; const nz = (c[0] - a[0]) * h; if (nx * dx + nz * dz >= 0) b.quad(a, c, c2, a2, color); else b.quad(c, a, a2, c2, color); } /** Draw in street coordinates: s along the street, w across it. */ function streetFrame(axis) { return axis === "x" ? (s, w, y) => [w, y, s] : (s, w, y) => [s, y, w]; } function stripe(b, P, s0, s1, w0, w1, y0, y1, color) { up(b, P(s0, w0, y0), P(s0, w1, y0), P(s1, w1, y1), P(s1, w0, y1), color); } function segmentLines(b, st, axis, s0, s1, h0, h1) { const P = streetFrame(axis); const lift = 0.025; const y = (s) => h0 + ((h1 - h0) * (s - s0)) / (s1 - s0) + lift; const line = (w, width, color, dash = 0) => { if (!dash) return stripe(b, P, s0 + 1.2, s1 - 1.2, w - width / 2, w + width / 2, y(s0 + 1.2), y(s1 - 1.2), color); for (let s = s0 + 2; s + dash < s1 - 1; s += dash * 2.2) stripe(b, P, s, s + dash, w - width / 2, w + width / 2, y(s), y(s + dash), color); }; if (st.kind === "embarcadero") { line(-47, 0.15, WHITE); line(-13, 0.15, WHITE); line(-41.5, 0.13, WHITE, 3); line(-18.5, 0.13, WHITE, 3); line(-36, 0.12, YELLOW); line(-24, 0.12, YELLOW); for (const t of [LANES.trackSouth, LANES.trackNorth]) for (const r of [-0.72, 0.72]) line(t + r, 0.09, RAIL); return; } if (st.kind === "market") { line(st.c - 0.15, 0.11, YELLOW); line(st.c + 0.15, 0.11, YELLOW); for (const w of [-6.5, 6.5]) line(st.c + w, 0.13, WHITE, 3); for (const w of [-13, 13]) line(st.c + w, 0.15, WHITE); return; } if (st.kind === "cable") { for (const t of [st.c - 1.6, st.c + 1.6]) { for (const r of [-0.72, 0.72]) line(t + r, 0.09, RAIL); line(t, 0.04, 0x26272a); } line(st.c - 3.4, 0.11, YELLOW); line(st.c + 3.4, 0.11, YELLOW); return; } line(st.c - 0.13, 0.1, YELLOW); line(st.c + 0.13, 0.1, YELLOW); } /** One street between two crossings: sidewalks, road, curbs and lines. */ function segment(chunks, st, axis, s0, s1, h0, h1) { const P = streetFrame(axis); const mid = (s0 + s1) / 2; const { ground, marks } = axis === "x" ? chunks.at(st.c, mid) : chunks.at(mid, st.c); const cols = []; if (st.kind === "embarcadero") cols.push([st.b0, st.r0, SIDEWALK, CURB], [st.r0, -36, ASPHALT, 0], [-36, -24, TRACKBED, 0], [-24, st.r1, ASPHALT, 0], [st.r1, st.b1, PROMENADE, CURB]); else if (st.kind === "waterfront") cols.push([st.b0, st.r0, PROMENADE, CURB], [st.r0, st.r1, ASPHALT, 0], [st.r1, st.b1, SIDEWALK, CURB]); else cols.push([st.b0, st.r0, SIDEWALK, CURB], [st.r0, st.r1, ASPHALT, 0], [st.r1, st.b1, SIDEWALK, CURB]); for (const [w0, w1, color, lift] of cols) stripe(ground, P, s0, s1, w0, w1, h0 + lift, h1 + lift, color); // curbs face the road wall(ground, P(s0, st.r0, h0), P(s1, st.r0, h1), CURB, ...(axis === "x" ? [1, 0] : [0, 1]), CURB_COLOR); wall(ground, P(s0, st.r1, h0), P(s1, st.r1, h1), CURB, ...(axis === "x" ? [-1, 0] : [0, -1]), CURB_COLOR); segmentLines(marks, st, axis, s0, s1, h0, h1); } /** One crossing: a flat 3×3 grid of road and sidewalk corners, with crosswalks. */ function crossing(chunks, xs, zs, h) { const { ground, marks } = chunks.at(xs.c, zs.c); const X = [xs.b0, xs.r0, xs.r1, xs.b1]; const Z = [zs.b0, zs.r0, zs.r1, zs.b1]; const kind = []; for (let a = 0; a < 3; a++) { kind.push([]); for (let c = 0; c < 3; c++) kind[a].push(surfaceAt((X[a] + X[a + 1]) / 2, (Z[c] + Z[c + 1]) / 2)); } for (let a = 0; a < 3; a++) for (let c = 0; c < 3; c++) { const walk = kind[a][c] === "walk"; const x0 = X[a], x1 = X[a + 1], z0 = Z[c], z1 = Z[c + 1]; const y = h + (walk ? CURB : 0); const prom = (xs.kind === "embarcadero" && a === 2) || (zs.kind === "waterfront" && c === 0); up(ground, [x0, y, z0], [x1, y, z0], [x1, y, z1], [x0, y, z1], walk ? (prom ? PROMENADE : SIDEWALK) : ASPHALT); if (!walk) continue; if (a > 0 && kind[a - 1][c] === "road") wall(ground, [x0, h, z0], [x0, h, z1], CURB, -1, 0, CURB_COLOR); if (a < 2 && kind[a + 1][c] === "road") wall(ground, [x1, h, z0], [x1, h, z1], CURB, 1, 0, CURB_COLOR); if (c > 0 && kind[a][c - 1] === "road") wall(ground, [x0, h, z0], [x1, h, z0], CURB, 0, -1, CURB_COLOR); if (c < 2 && kind[a][c + 1] === "road") wall(ground, [x0, h, z1], [x1, h, z1], CURB, 0, 1, CURB_COLOR); } // Crosswalks: wide white bars where a road crosses a sidewalk's line. const y = h + 0.025; if (xs.kind !== "embarcadero" || true) { for (const c of [0, 2]) { if (kind[1][c] !== "road" || kind[0][c] !== "walk" || kind[2][c] !== "walk") continue; const z0 = Z[c] + 0.6, z1 = Z[c + 1] - 0.6; for (let x = X[1] + 0.5; x < X[2] - 0.6; x += 1.25) up(marks, [x, y, z0], [x + 0.6, y, z0], [x + 0.6, y, z1], [x, y, z1], WHITE); } for (const a of [0, 2]) { if (kind[a][1] !== "road" || kind[a][0] !== "walk" || kind[a][2] !== "walk") continue; const x0 = X[a] + 0.6, x1 = X[a + 1] - 0.6; for (let z = Z[1] + 0.5; z < Z[2] - 0.6; z += 1.25) up(marks, [x0, y, z], [x1, y, z], [x1, y, z + 0.6], [x0, y, z + 0.6], WHITE); } } } function blockGround(chunks, b) { const { ground } = chunks.at(b.cx, b.cz); const color = { houses: YARD, flats: YARD, coit: PARK, square: PARK, plaza: PLAZA, pyramid: PLAZA }[b.kind] ?? PLAZA; const n = b.kind === "coit" ? 28 : 1; const y = lawnAt; for (let a = 0; a < n; a++) for (let c = 0; c < n; c++) { const x0 = b.x0 + ((b.x1 - b.x0) * a) / n, x1 = b.x0 + ((b.x1 - b.x0) * (a + 1)) / n; const z0 = b.z0 + ((b.z1 - b.z0) * c) / n, z1 = b.z0 + ((b.z1 - b.z0) * (c + 1)) / n; up(ground, [x0, y(x0, z0), z0], [x1, y(x1, z0), z0], [x1, y(x1, z1), z1], [x0, y(x0, z1), z1], color); } if (b.kind === "pyramid" || b.kind === "plaza") { // a lawn in the plaza const g = b.kind === "pyramid" ? [b.x0 + 4, b.z0 + 4, b.x0 + (b.x1 - b.x0) * 0.42, b.z1 - 4] : [b.x0 + 8, b.z0 + 8, b.x1 - 8, b.z1 - 10]; // following the slope, just above the paving, so no edge of it hangs in the air const yy = (x, z) => y(x, z) + 0.03; up(ground, [g[0], yy(g[0], g[1]), g[1]], [g[2], yy(g[2], g[1]), g[1]], [g[2], yy(g[2], g[3]), g[3]], [g[0], yy(g[0], g[3]), g[3]], PARK); } } export function buildStreets(scene) { const chunks = new Chunked(); for (let i = 0; i < XS.length; i++) for (let j = 0; j + 1 < ZS.length; j++) segment(chunks, XS[i], "x", ZS[j].b1, ZS[j + 1].b0, H[i][j], H[i][j + 1]); for (let j = 0; j < ZS.length; j++) for (let i = 0; i + 1 < XS.length; i++) segment(chunks, ZS[j], "z", XS[i].b1, XS[i + 1].b0, H[i][j], H[i + 1][j]); for (let i = 0; i < XS.length; i++) for (let j = 0; j < ZS.length; j++) crossing(chunks, XS[i], ZS[j], H[i][j]); for (const b of CITY.blocks) blockGround(chunks, b); // The median planters on the Embarcadero. for (const p of CITY.planters) { const { ground } = chunks.at(-30, (p.z0 + p.z1) / 2); ground.box(p.x0, QUAY, p.z0, p.x1, QUAY + 0.32, p.z1, 0xb3ab9e, 0x76935a); } // Sea walls: the east waterfront and the north shore. const south = ZS[ZS.length - 1].b1; for (let z = NORTH_SHORE; z < south; z += CHUNK / 2) { const z1 = Math.min(south, z + CHUNK / 2); wall(chunks.at(0, z).ground, [0, -3, z], [0, -3, z1], QUAY + CURB + 3, 1, 0, SEAWALL); } for (let x = XS[0].b0; x < 0; x += CHUNK / 2) { const x1 = Math.min(0, x + CHUNK / 2); wall(chunks.at(x, NORTH_SHORE).ground, [x, -3, NORTH_SHORE], [x1, -3, NORTH_SHORE], QUAY + CURB + 3, 0, -1, SEAWALL); } // Pier decks. for (const p of PIERS) { const { ground } = chunks.at(p.len / 2, p.z); ground.box(0, -0.6, p.deck.z0, p.deck.x1, QUAY + CURB, p.deck.z1, PIER_SIDE, PIER_TOP); } const groundMat = surfaceMaterial({ roughness: 0.95, key: "ground" }); const markMat = surfaceMaterial({ roughness: 0.75, key: "marks", extra: { polygonOffset: true, polygonOffsetFactor: -2, polygonOffsetUnits: -4 } }); const group = new THREE.Group(); for (const { ground, marks } of chunks.map.values()) { if (ground.count) { const m = new THREE.Mesh(ground.geometry(), groundMat); m.receiveShadow = true; group.add(m); } if (marks.count) { const m = new THREE.Mesh(marks.geometry(), markMat); m.receiveShadow = true; group.add(m); } } scene.add(group); return group; } export { CALIFORNIA };