// Every building in the city, drawn as instanced boxes in chunks so whole neighborhoods cost a // few draw calls: bodies with painted windows, white cornices, the bay windows that make a San // Francisco street, and rooftop clutter on the big ones. import * as THREE from "three"; import { CITY, RIDE } from "../city.js"; import { hash } from "../rng.js"; import { Builder, STYLE, bayGeometry, buildingMaterial, unitBox } from "./kit.js"; const CHUNK = 420; const NO_WINDOWS = [0, 1, 0, 0]; const ROOFTOP = [0x8e8b86, 0x9b968f, 0x7f7c78, 0xa39d94]; // canvas awnings over the shops: deep, old-city colors const AWNINGS = [0x2f5d4a, 0x7a2a2e, 0x26324f, 0x1f1f22, 0xb8872f, 0xd8cfbd, 0x5a3b52, 0x2e5f6e]; const ROT = { n: Math.PI, s: 0, e: Math.PI / 2, w: -Math.PI / 2 }; // each kind of building gets its wall finish const FINISH = { house: STYLE.siding, brick: STYLE.brick, flat: STYLE.plain, office: STYLE.stone, tower: STYLE.stone, granite: STYLE.stone, slab: STYLE.stone, glass: STYLE.glass, fins: STYLE.plain }; const m4 = new THREE.Matrix4(); const q = new THREE.Quaternion(); const e = new THREE.Euler(); const v = new THREE.Vector3(); const sc = new THREE.Vector3(); const col = new THREE.Color(); function lighten(hex, k) { col.set(hex); col.offsetHSL(0, 0, k); return col.getHex(); } function instanced(geometry, material, items) { const g = geometry.clone(); const n = items.length; const win = new Float32Array(n * 4); const box = new Float32Array(n * 4); const style = new Float32Array(n * 2); const mesh = new THREE.InstancedMesh(g, material, n); items.forEach((it, k) => { e.set(0, it.rot ?? 0, 0); q.setFromEuler(e); m4.compose(v.set(it.x, it.y, it.z), q, sc.set(it.sx, it.sy, it.sz)); mesh.setMatrixAt(k, m4); mesh.setColorAt(k, col.set(it.color)); win.set(it.win, k * 4); box.set(it.box, k * 4); style[k * 2] = it.style ?? 0; }); g.setAttribute("aWin", new THREE.InstancedBufferAttribute(win, 4)); g.setAttribute("aBox", new THREE.InstancedBufferAttribute(box, 4)); g.setAttribute("aStyle", new THREE.InstancedBufferAttribute(style, 2)); mesh.instanceMatrix.needsUpdate = true; mesh.computeBoundingSphere(); mesh.castShadow = true; mesh.receiveShadow = true; return mesh; } export function buildBuildings(scene) { const material = buildingMaterial(); const box = unitBox(); const bay = bayGeometry(); const chunks = new Map(); const chunk = (x, z) => { const k = `${Math.floor(x / CHUNK)},${Math.floor(z / CHUNK)}`; if (!chunks.has(k)) chunks.set(k, { bodies: [], bays: [], awnings: new Builder() }); return chunks.get(k); }; CITY.buildings.forEach((b, idx) => { const c = chunk(b.x, b.z); const seed = hash(idx, 77); const near = b.x > RIDE.x0 - 300 && b.z < RIDE.z1 + 300; const boxAttr = [b.y0, b.y0 + b.h, b.gf, seed + (b.shop ? 2 : 0)]; const finish = FINISH[b.kind] ?? STYLE.plain; c.bodies.push({ x: b.x, y: b.y0 - b.sink, z: b.z, sx: b.w, sy: b.h + b.sink, sz: b.d, color: b.color, win: b.win ?? NO_WINDOWS, box: boxAttr, style: finish }); if (b.cornice) c.bodies.push({ x: b.x, y: b.y0 + b.h - 0.55, z: b.z, sx: b.w + 0.55, sy: 0.6, sz: b.d + 0.55, color: b.trim, win: NO_WINDOWS, box: boxAttr }); if (b.bay && b.face && near) { const bw = Math.min(b.w * 0.56, 4.4); const hb = b.h - b.gf - 1.1; if (hb > 2.4) { const off = { n: [0, -b.d / 2], s: [0, b.d / 2], e: [b.w / 2, 0], w: [-b.w / 2, 0] }[b.face]; c.bays.push({ x: b.x + off[0], y: b.y0 + b.gf, z: b.z + off[1], sx: bw, sy: hb, sz: 0.95, rot: ROT[b.face], color: lighten(b.color, 0.03), win: [b.win[0], bw / 2.05, 0.62, 0.6], box: boxAttr, style: finish }); } } // an awning over a shopfront that faces the street if (b.shop && b.face && near && b.x > RIDE.x0 - 60 && b.z < RIDE.z1 + 60 && hash(idx, 41) < 0.7) { const color = AWNINGS[Math.floor(hash(idx, 43) * AWNINGS.length)]; const along = b.face === "n" || b.face === "s" ? b.w : b.d; const w = Math.min(along * 0.78, 9); const yTop = b.y0 + b.gf - 0.3; const yLow = yTop - 0.55; const out = 1.5; // local frame: u along the facade, n out from it const [nx, nz] = { n: [0, -1], s: [0, 1], e: [1, 0], w: [-1, 0] }[b.face]; const fx = b.x + nx * (b.w / 2); const fz = b.z + nz * (b.d / 2); const ux = -nz; const uz = nx; const P = (u, o, y) => [fx + ux * u + nx * o, y, fz + uz * u + nz * o]; const a = c.awnings; a.quad(P(-w / 2, 0.02, yTop), P(w / 2, 0.02, yTop), P(w / 2, out, yLow), P(-w / 2, out, yLow), color); a.quad(P(-w / 2, out, yLow), P(w / 2, out, yLow), P(w / 2, out, yLow - 0.32), P(-w / 2, out, yLow - 0.32), color); a.tri(P(-w / 2, 0.02, yTop), P(-w / 2, out, yLow), P(-w / 2, 0.02, yLow), color); a.tri(P(w / 2, 0.02, yTop), P(w / 2, 0.02, yLow), P(w / 2, out, yLow), color); } // rooftop clutter on big roofs if (b.h > 14 && b.w * b.d > 500 && (b.tier === undefined || b.cornice)) { const n = 1 + Math.floor(seed * 3); for (let k = 0; k < n; k++) { const r1 = hash(idx, k, 3); const r2 = hash(idx, k, 5); const s = 2.5 + r1 * 5; c.bodies.push({ x: b.x + (r1 - 0.5) * b.w * 0.5, y: b.y0 + b.h, z: b.z + (r2 - 0.5) * b.d * 0.5, sx: s, sy: 1.6 + r2 * 3, sz: s * (0.7 + r2 * 0.6), color: ROOFTOP[k % ROOFTOP.length], win: NO_WINDOWS, box: boxAttr }); } } }); const group = new THREE.Group(); const canvas = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.85, side: THREE.DoubleSide }); for (const { bodies, bays, awnings } of chunks.values()) { if (bodies.length) group.add(instanced(box, material, bodies)); if (bays.length) group.add(instanced(bay, material, bays)); if (awnings.count) { const m = new THREE.Mesh(awnings.geometry(), canvas); m.castShadow = true; m.receiveShadow = true; group.add(m); } } scene.add(group); return { group, material }; }