// Everything beyond the streets: the rest of San Francisco rolling over its hills, the islands, // Marin and the East Bay, the Golden Gate far off to the northwest, Sutro Tower, the fog bank // pouring in from the Pacific, and boats on the bay. Placed by geography, simplified by distance. import * as THREE from "three"; import { BRIDGE, NORTH_SHORE, XS, ZS, heightAt, terrain } from "../city.js"; import { BRIDGE_ORANGE, CREAMS, PASTELS } from "../palette.js"; import { hash, rng } from "../rng.js"; import { Builder, textTexture, vertexColored } from "./kit.js"; const GRID = { x0: XS[0].b0, x1: 0, z0: NORTH_SHORE, z1: ZS[ZS.length - 1].b1 }; const inGrid = (x, z) => x > GRID.x0 + 2 && x < GRID.x1 && z > GRID.z0 && z < GRID.z1 - 2; /** The north shore beyond the streets runs northwest toward the Golden Gate. */ const shoreZ = (x) => (x > -1500 ? NORTH_SHORE : NORTH_SHORE + (x + 1500) * 0.32); const isLand = (x, z) => x < 0 && z > shoreZ(x) && x > -9200; function farLand(group) { const b = new Builder(); const step = 90; const x0 = -9600; const x1 = 0; const z0 = -3400; const z1 = 7200; const nx = Math.round((x1 - x0) / step); const nz = Math.round((z1 - z0) / step); const H = []; const C = []; for (let i = 0; i <= nx; i++) { H.push([]); C.push([]); for (let j = 0; j <= nz; j++) { const x = x0 + i * step; const z = z0 + j * step; let y; if (inGrid(x, z)) y = heightAt(x, z) - 8; else if (isLand(x, z)) y = terrain(x, z) + (hash(i, j, 3) - 0.5) * 4; else y = -8; H[i].push(y); const n = hash(Math.floor(i / 2), Math.floor(j / 2), 11); const park = (x < -6200 && z < -1200 && z > -2600) || (x < -5200 && z > 1400 && z < 2200) || (Math.hypot(x + 4740, z - 4560) < 700); C[i].push(park ? (n > 0.5 ? 0x7d8f5e : 0x8a9a68) : n < 0.25 ? 0xd9cfbf : n < 0.5 ? 0xcdc6ba : n < 0.75 ? 0xe2d8c6 : 0xc7bfb3); } } for (let i = 0; i < nx; i++) for (let j = 0; j < nz; j++) { const xa = x0 + i * step; const za = z0 + j * step; const corners = [[xa, za], [xa + step, za], [xa + step, za + step], [xa, za + step]]; if (corners.every(([x, z]) => inGrid(x, z))) continue; if (corners.every(([x, z]) => !isLand(x, z))) continue; const p = [[xa, H[i][j], za], [xa + step, H[i + 1][j], za], [xa + step, H[i + 1][j + 1], za + step], [xa, H[i][j + 1], za + step]]; b.tri(p[0], p[3], p[2], C[i][j]); b.tri(p[0], p[2], p[1], C[i][j]); } const mesh = new THREE.Mesh(b.geometry(), vertexColored({ roughness: 1 })); mesh.receiveShadow = false; group.add(mesh); // A carpet of far buildings on the hills. const random = rng(2026); const items = []; for (let k = 0; k < 9000 && items.length < 4200; k++) { const x = -9000 + random() * 9000; const z = -3000 + random() * 9800; if (!isLand(x, z) || inGrid(x, z) || (x > GRID.x0 - 40 && z < GRID.z1 + 40)) continue; if (Math.hypot(x + 4740, z - 4560) < 800) continue; if (x < -6200 && z < -1200 && z > -2600) continue; const w = 12 + random() * 22; const h = 7 + random() * (Math.hypot(x + 900, z - 300) < 2500 ? 40 : 18); items.push({ x, z, w, d: 10 + random() * 20, h, y: terrain(x, z) - 3, color: random() < 0.7 ? CREAMS[Math.floor(random() * CREAMS.length)] : PASTELS[Math.floor(random() * PASTELS.length)] }); } const box = new THREE.BoxGeometry(1, 1, 1); box.translate(0, 0.5, 0); const mesh2 = new THREE.InstancedMesh(box, new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.9, flatShading: true }), items.length); const m4 = new THREE.Matrix4(); const col = new THREE.Color(); items.forEach((it, k) => { m4.makeScale(it.w, it.h, it.d).setPosition(it.x, it.y, it.z); mesh2.setMatrixAt(k, m4); mesh2.setColorAt(k, col.set(it.color)); }); mesh2.computeBoundingSphere(); group.add(mesh2); } /** A low-poly island: rings of noisy heights around a center. */ function island(b, cx, cz, r, h, color, seed, flat = 0) { const rings = 5; const around = 18; const pt = (ring, k) => { if (ring === 0) return [cx, h, cz]; const a = (k / around) * Math.PI * 2; const u = ring / rings; const rr = r * u * (0.82 + hash(seed, ring, k) * 0.3); const y = ring === rings ? -4 : flat ? h * (1 - u * u * 0.3) : h * (1 - u ** 1.7) + (hash(seed, k, ring, 2) - 0.5) * h * 0.15; return [cx + Math.cos(a) * rr, y, cz + Math.sin(a) * rr]; }; for (let ring = 0; ring < rings; ring++) for (let k = 0; k < around; k++) { const a = pt(ring, k); const c = pt(ring + 1, k); const d = pt(ring + 1, (k + 1) % around); const e = pt(ring, (k + 1) % around); const shade = hash(seed, ring, k, 9) < 0.5 ? color : new THREE.Color(color).offsetHSL(0, 0, -0.04).getHex(); if (ring === 0) b.tri(a, d, c, shade); else { b.tri(a, d, c, shade); b.tri(a, e, d, shade); } } } /** A ridge of hills along a polyline, peaks from a profile. */ function ridge(b, pts, width, heights, color, seed) { const n = pts.length; for (let k = 0; k + 1 < n; k++) { const [xa, za] = pts[k]; const [xb, zb] = pts[k + 1]; const dx = xb - xa; const dz = zb - za; const l = Math.hypot(dx, dz); const ox = (-dz / l) * width; const oz = (dx / l) * width; const ha = heights[k]; const hb = heights[k + 1]; const top = (x, z, h) => [x, h, z]; const c = hash(seed, k) < 0.5 ? color : new THREE.Color(color).offsetHSL(0, 0, -0.035).getHex(); b.quad([xa - ox, -5, za - oz], [xb - ox, -5, zb - oz], top(xb, zb, hb), top(xa, za, ha), c); b.quad([xb + ox, -5, zb + oz], [xa + ox, -5, za + oz], top(xa, za, ha), top(xb, zb, hb), c); } } function geography(group) { const b = new Builder(); const green = 0x8c9a72; const dry = 0xa9a27c; // Yerba Buena Island where the bridge lands, Treasure Island beside it. const ybi = BRIDGE.at(BRIDGE.island + 330); island(b, ybi.x, ybi.z, 560, 105, green, 1); island(b, ybi.x + 250, ybi.z - 1250, 820, 5, 0xb9b3a5, 2, true); // Alcatraz, with its cellhouse and lighthouse. island(b, -2570, -3460, 190, 38, 0x9c9585, 3); b.box(-2640, 30, -3500, -2520, 46, -3440, 0xe9e4d8); b.box(-2650, 30, -3420, -2640, 62, -3410, 0xf1ede4); // Angel Island, Marin and Mount Tamalpais to the north. island(b, -1900, -6900, 1300, 240, green, 4); ridge(b, [[-11000, -4600], [-9000, -4300], [-7700, -4100], [-6400, -4800], [-5200, -6200], [-4200, -7600], [-3000, -9200]], 900, [150, 240, 260, 300, 280, 340, 260], dry, 5); island(b, -9800, -10800, 4200, 780, 0x8d9372, 6); // The East Bay hills, across the water. ridge(b, [[11500, -16000], [12200, -9000], [12800, -3000], [13300, 3000], [14000, 9000], [15000, 16000]], 1600, [420, 480, 520, 460, 540, 500], 0x9ea080, 7); island(b, 8500, -2600, 3000, 12, 0xc4bcae, 8, true); const mesh = new THREE.Mesh(b.geometry(), vertexColored({ roughness: 1 })); group.add(mesh); } function goldenGate(group) { const b = new Builder(); const x = -7450; const zs = -2420; const zn = -3700; const deck = 67; const top = 227; for (const z of [zs + 200, zn - 200]) { for (const dx of [-14, 14]) b.box(x + dx - 4, -2, z - 5, x + dx + 4, top, z + 5, BRIDGE_ORANGE); for (const y of [deck + 25, deck + 75, deck + 120, top - 10]) b.box(x - 14, y, z - 3, x + 14, y + 8, z + 3, BRIDGE_ORANGE); } b.box(x - 15, deck - 6, zn - 450, x + 15, deck, zs + 450, BRIDGE_ORANGE); const mesh = new THREE.Mesh(b.geometry(), vertexColored({ roughness: 0.7 })); group.add(mesh); const pts = []; for (const dx of [-14, 14]) { const a = zs + 200; const c = zn - 200; let prev = [x + dx, deck + 10, zs + 450]; const step = (from, to, y0, y1, sag) => { for (let k = 1; k <= 24; k++) { const u = k / 24; const z = from + (to - from) * u; const y = y0 + (y1 - y0) * u - sag * 4 * u * (1 - u); const p = [x + dx, y, z]; pts.push(...prev, ...p); prev = p; } }; step(zs + 450, a, deck + 10, top, 30); step(a, c, top, top, top - deck - 4); step(c, zn - 450, top, deck + 10, 30); } const g = new THREE.BufferGeometry(); g.setAttribute("position", new THREE.Float32BufferAttribute(pts, 3)); group.add(new THREE.LineSegments(g, new THREE.LineBasicMaterial({ color: BRIDGE_ORANGE }))); } function sutroTower(group) { const b = new Builder(); const x = -5190; const z = 4470; const y = terrain(x, z) - 4; const legs = [0, 1, 2].map((k) => [x + Math.cos((k / 3) * Math.PI * 2) * 55, z + Math.sin((k / 3) * Math.PI * 2) * 55]); const top = y + 298; for (const [lx, lz] of legs) { for (let s = 0; s < 6; s++) { const u0 = s / 6; const u1 = (s + 1) / 6; const p0 = [lx + (x - lx) * u0 * 0.85, y + 240 * u0, lz + (z - lz) * u0 * 0.85]; const p1 = [lx + (x - lx) * u1 * 0.85, y + 240 * u1, lz + (z - lz) * u1 * 0.85]; const color = s % 2 ? 0xf2efe8 : BRIDGE_ORANGE; b.box(Math.min(p0[0], p1[0]) - 3, p0[1], Math.min(p0[2], p1[2]) - 3, Math.max(p0[0], p1[0]) + 3, p1[1], Math.max(p0[2], p1[2]) + 3, color); } b.box(lx + (x - lx) * 0.85 - 2, y + 240, lz + (z - lz) * 0.85 - 2, lx + (x - lx) * 0.85 + 2, top, lz + (z - lz) * 0.85 + 2, 0xf2efe8); } for (const yy of [y + 120, y + 200, y + 240]) for (let k = 0; k < 3; k++) { const a = legs[k]; const c = legs[(k + 1) % 3]; const u = (yy - y) / 240; const pa = [a[0] + (x - a[0]) * u * 0.85, a[1] + (z - a[1]) * u * 0.85]; const pc = [c[0] + (x - c[0]) * u * 0.85, c[1] + (z - c[1]) * u * 0.85]; b.box(Math.min(pa[0], pc[0]), yy, Math.min(pa[1], pc[1]), Math.max(pa[0], pc[0]) + 2, yy + 4, Math.max(pa[1], pc[1]) + 2, BRIDGE_ORANGE); } group.add(new THREE.Mesh(b.geometry(), vertexColored({ roughness: 0.8 }))); } function fogBank(group) { const tex = textTexture(256, 256, (ctx, w) => { const g = ctx.createRadialGradient(w / 2, w / 2, 0, w / 2, w / 2, w / 2); g.addColorStop(0, "rgba(255,255,255,0.95)"); g.addColorStop(0.45, "rgba(255,255,255,0.55)"); g.addColorStop(1, "rgba(255,255,255,0)"); ctx.fillStyle = g; ctx.fillRect(0, 0, w, w); }); const puffs = []; const random = rng(66); const anchors = [ [-4740, 4560, 330, 1400], [-5600, 3200, 260, 1500], [-6300, 1600, 220, 1500], [-6900, 0, 200, 1500], [-7300, -1500, 160, 1300], [-7500, -2900, 110, 1100], [-8200, -3600, 140, 1500], [-4200, 5600, 300, 1600], [-3000, 6200, 260, 1500], ]; for (const [ax, az, ay, size] of anchors) for (let k = 0; k < 5; k++) { const mat = new THREE.SpriteMaterial({ map: tex, color: 0xf4f1ec, transparent: true, opacity: 0.4 + random() * 0.25, depthWrite: false, fog: true }); const s = new THREE.Sprite(mat); s.scale.set(size * (0.8 + random() * 0.6), size * (0.35 + random() * 0.2), 1); s.position.set(ax + (random() - 0.5) * size, ay + random() * 80, az + (random() - 0.5) * size); s.userData.base = s.position.clone(); s.userData.phase = random() * 6.28; group.add(s); puffs.push(s); } return puffs; } function boats(group) { const b = new Builder(); b.box(-1.1, 0, -4, 1.1, 1.1, 4, 0xf3f1ea); b.box(-0.06, 1.1, -0.4, 0.06, 11, -0.2, 0xdadada); const sail = new Builder(); sail.tri([0, 1.6, 3.5], [0, 1.6, -0.3], [0, 10.6, -0.3], 0xfbfaf6); sail.tri([0, 1.6, -0.3], [0, 1.6, 3.5], [0, 10.6, -0.3], 0xfbfaf6); sail.tri([0, 1.6, -0.6], [0, 1.6, -3.6], [0, 8.6, -0.6], 0xf4f2ec); sail.tri([0, 1.6, -3.6], [0, 1.6, -0.6], [0, 8.6, -0.6], 0xf4f2ec); const hullGeo = b.geometry(); const sailGeo = sail.geometry(); const mat = vertexColored({ roughness: 0.7 }); const list = []; const random = rng(8); for (let k = 0; k < 9; k++) { const g = new THREE.Group(); g.add(new THREE.Mesh(hullGeo, mat), new THREE.Mesh(sailGeo, mat)); g.userData = { cx: 300 + random() * 2200, cz: -2600 + random() * 2600, r: 120 + random() * 300, w: (random() < 0.5 ? -1 : 1) * (0.012 + random() * 0.01), phase: random() * 6.28 }; group.add(g); list.push(g); } // The ferry, back and forth to the East Bay. const f = new Builder(); f.box(-5, 0, -18, 5, 2.6, 18, 0xf2f0ea); f.box(-4.2, 2.6, -12, 4.2, 5.2, 10, 0xe8e6df); f.box(-4.25, 3.2, -11.5, 4.25, 4.4, 9.5, 0x2a3038); f.box(-1, 5.2, -2, 1, 7.4, 1, 0x2d6f6a); const ferry = new THREE.Mesh(f.geometry(), mat); group.add(ferry); return { list, ferry }; } export function buildDistance(scene) { const group = new THREE.Group(); farLand(group); geography(group); goldenGate(group); sutroTower(group); const puffs = fogBank(group); const { list, ferry } = boats(group); scene.add(group); return { update(t) { for (const s of puffs) { s.position.x = s.userData.base.x + Math.sin(t * 0.02 + s.userData.phase) * 60 + t * 0.8; s.position.z = s.userData.base.z + Math.cos(t * 0.015 + s.userData.phase) * 40; } for (const g of list) { const u = g.userData; const a = u.phase + t * u.w; g.position.set(u.cx + Math.cos(a) * u.r, 0.1, u.cz + Math.sin(a) * u.r); g.rotation.set(0, u.w > 0 ? Math.PI - a : -a, u.w > 0 ? 0.12 : -0.12); } const period = 2 * (4200 / 9 + 60); let v = ((t + 200) % period) / period; const out = v < 0.5; v = out ? v * 2 : (1 - v) * 2; const s = Math.min(1, Math.max(0, (v * period * 0.5 - 30) / (4200 / 9))); ferry.position.set(60 + s * 4200, 0.1, -40 - s * 1400); ferry.rotation.y = -Math.atan2(4200, 1400) + (out ? 0 : Math.PI); }, }; }