// Street furniture: the Embarcadero's palms, plane trees, redwoods and pines, lamps, and the // parked cars that line every hill. All instanced, in chunks, so the sun's shadow pass only // draws what is near Claudia. import * as THREE from "three"; import { CITY, blockAt, groundAt, heightAt, lawnAt } from "../city.js"; import { hash } from "../rng.js"; import { Builder, vertexColored } from "./kit.js"; const CHUNK = 420; const m4 = new THREE.Matrix4(); const q = new THREE.Quaternion(); const eul = new THREE.Euler(0, 0, 0, "YXZ"); const pos = new THREE.Vector3(); const scl = new THREE.Vector3(); const col = new THREE.Color(); function palmGeometry() { const b = new Builder(); const trunk = 0x8a7a62; const n = 7; for (let k = 0; k < n; k++) { const a0 = (k / n) * Math.PI * 2; const a1 = ((k + 1) / n) * Math.PI * 2; const r0 = 0.42; const r1 = 0.3; b.quad([Math.cos(a0) * r0, 0, Math.sin(a0) * r0], [Math.cos(a1) * r0, 0, Math.sin(a1) * r0], [Math.cos(a1) * r1, 9.2, Math.sin(a1) * r1], [Math.cos(a0) * r1, 9.2, Math.sin(a0) * r1], k % 2 ? trunk : 0x7d6e58); } // the pineapple at the top of the trunk for (let k = 0; k < n; k++) { const a0 = (k / n) * Math.PI * 2; const a1 = ((k + 1) / n) * Math.PI * 2; b.quad([Math.cos(a0) * 0.3, 9.2, Math.sin(a0) * 0.3], [Math.cos(a1) * 0.3, 9.2, Math.sin(a1) * 0.3], [Math.cos(a1) * 0.55, 9.9, Math.sin(a1) * 0.55], [Math.cos(a0) * 0.55, 9.9, Math.sin(a0) * 0.55], 0x6f6a45); } const fronds = 15; for (let k = 0; k < fronds; k++) { const a = (k / fronds) * Math.PI * 2 + (k % 2) * 0.2; const ca = Math.cos(a); const sa = Math.sin(a); const lift = k % 3 === 0 ? 0.9 : k % 3 === 1 ? 0.3 : -0.2; const pts = [ [0, 9.9, 0], [ca * 1.6, 10.6 + lift * 0.6, sa * 1.6], [ca * 3.2, 10.5 + lift * 0.5, sa * 3.2], [ca * 4.4, 9.4 + lift * 0.2, sa * 4.4], ]; const widths = [0.15, 0.55, 0.42, 0.04]; const color = k % 2 ? 0x5b7a3a : 0x67874a; for (let i = 0; i < 3; i++) { const [p0, p1] = [pts[i], pts[i + 1]]; const w0 = widths[i]; const w1 = widths[i + 1]; const l0 = [p0[0] - sa * w0, p0[1], p0[2] + ca * w0]; const r0 = [p0[0] + sa * w0, p0[1], p0[2] - ca * w0]; const l1 = [p1[0] - sa * w1, p1[1], p1[2] + ca * w1]; const r1 = [p1[0] + sa * w1, p1[1], p1[2] - ca * w1]; b.quad(l0, r0, r1, l1, color); b.quad(r0, l0, l1, r1, color); } } return b.geometry(); } function blob(b, cx, cy, cz, r, color) { const g = new THREE.IcosahedronGeometry(r, 0).toNonIndexed(); const p = g.attributes.position; for (let i = 0; i < p.count; i += 3) { const a = [p.getX(i) + cx, p.getY(i) * 0.82 + cy, p.getZ(i) + cz]; const c = [p.getX(i + 1) + cx, p.getY(i + 1) * 0.82 + cy, p.getZ(i + 1) + cz]; const d = [p.getX(i + 2) + cx, p.getY(i + 2) * 0.82 + cy, p.getZ(i + 2) + cz]; b.tri(a, c, d, color); } } function trunk(b, h, r, color) { const n = 5; for (let k = 0; k < n; k++) { const a0 = (k / n) * Math.PI * 2; const a1 = ((k + 1) / n) * Math.PI * 2; b.quad([Math.cos(a0) * r, 0, Math.sin(a0) * r], [Math.cos(a1) * r, 0, Math.sin(a1) * r], [Math.cos(a1) * r * 0.7, h, Math.sin(a1) * r * 0.7], [Math.cos(a0) * r * 0.7, h, Math.sin(a0) * r * 0.7], color); } } /** A fuller plane tree for up close: a forked trunk and a canopy of soft, lumpy clusters, * lighter on top and darker underneath. Smooth normals; the instance color sets the green. */ function leafyTreeGeometry() { const parts = []; const trunkGeo = new THREE.CylinderGeometry(0.15, 0.24, 3.6, 8); trunkGeo.translate(0, 1.8, 0); parts.push([trunkGeo, 0x6d6152]); for (const [ax, az, len] of [[0.5, 0.2, 2.2], [-0.45, -0.3, 2.0], [0.1, -0.55, 1.8]]) { const br = new THREE.CylinderGeometry(0.07, 0.12, len, 6); br.translate(0, len / 2, 0); br.rotateZ(-ax); br.rotateX(az); br.translate(0, 3.2, 0); parts.push([br, 0x6d6152]); } const clusters = [[0, 5.3, 0, 2.3], [1.3, 5.0, 0.5, 1.7], [-1.2, 5.1, -0.4, 1.8], [0.3, 6.4, -0.6, 1.6], [-0.4, 6.2, 0.8, 1.5], [0.9, 6.0, -1.1, 1.4], [-1.0, 4.4, 0.9, 1.3]]; let s = 11; const rnd = () => ((s = (s * 16807) % 2147483647) / 2147483647); for (const [cx, cy, cz, r] of clusters) { const g = new THREE.IcosahedronGeometry(1, 2); const p = g.attributes.position; const nrm = new Float32Array(p.count * 3); const bumps = [rnd() * 6, rnd() * 6, rnd() * 6]; for (let i = 0; i < p.count; i++) { const x = p.getX(i), y = p.getY(i), z = p.getZ(i); const k = 1 + 0.16 * Math.sin(x * 3.1 + bumps[0]) * Math.sin(y * 2.7 + bumps[1]) * Math.sin(z * 3.3 + bumps[2]); p.setXYZ(i, cx + x * r * k, cy + y * r * 0.82 * k, cz + z * r * k); nrm.set([x, y, z], i * 3); } g.setAttribute("normal", new THREE.BufferAttribute(nrm, 3)); parts.push([g, null]); } // merge, coloring leaves by height: darker underneath, lighter on top const pos = []; const norm = []; const colr = []; const c = new THREE.Color(); for (const [g, color] of parts) { const gg = g.index ? g.toNonIndexed() : g; if (!gg.attributes.normal) gg.computeVertexNormals(); const p = gg.attributes.position; const n = gg.attributes.normal; for (let i = 0; i < p.count; i++) { pos.push(p.getX(i), p.getY(i), p.getZ(i)); norm.push(n.getX(i), n.getY(i), n.getZ(i)); if (color !== null) c.set(color); else { const shade = 0.62 + 0.45 * Math.min(1, Math.max(0, (p.getY(i) - 3.8) / 3.4)); c.setRGB(shade, shade, shade * 0.96); } colr.push(c.r, c.g, c.b); } } const geo = new THREE.BufferGeometry(); geo.setAttribute("position", new THREE.Float32BufferAttribute(pos, 3)); geo.setAttribute("normal", new THREE.Float32BufferAttribute(norm, 3)); geo.setAttribute("color", new THREE.Float32BufferAttribute(colr, 3)); geo.computeBoundingSphere(); return geo; } /** A traffic signal: a pole, a mast arm out over the road, and a signal head with three lenses, * one of them lit. Built into shared geometry for every signal in the ride. */ function signals(group) { const body = new Builder(); const lenses = { red: new Builder(), amber: new Builder(), green: new Builder(), off: new Builder() }; const POLE = 0x2b302d; for (const s of CITY.signals) { const { x, y, z, arm, reach } = s; body.box(x - 0.09, y, z - 0.09, x + 0.09, y + 5.6, z + 0.09, POLE); body.box(x - 0.15, y, z - 0.15, x + 0.15, y + 0.5, z + 0.15, POLE); const x1 = x + arm * reach; body.box(Math.min(x, x1), y + 5.3, z - 0.05, Math.max(x, x1), y + 5.42, z + 0.05, POLE); // the head hangs at the arm's end, facing along the street (toward -z or +z) const hx = x1; const face = arm > 0 ? -1 : 1; body.box(hx - 0.17, y + 4.15, z - 0.14, hx + 0.17, y + 5.3, z + 0.14, 0x30382f); body.box(hx - 0.3, y + 4.05, z - 0.03 * face, hx + 0.3, y + 5.4, z + 0.0, 0x151716); const lz = z + face * 0.145; const lit = s.green ? "green" : "red"; for (const [k, ly] of [["red", 5.05], ["amber", 4.72], ["green", 4.39]]) { const b = k === lit ? lenses[k] : lenses.off; b.box(hx - 0.11, y + ly - 0.11, Math.min(lz, lz + face * 0.02), hx + 0.11, y + ly + 0.11, Math.max(lz, lz + face * 0.02), 0xffffff); } // a small one at eye level for the crosswalk body.box(x - 0.13 * arm - 0.12, y + 2.4, z - 0.12, x - 0.13 * arm + 0.12, y + 2.95, z + 0.12, 0x30382f); } const bodyMesh = new THREE.Mesh(body.geometry(), vertexColored({ roughness: 0.6 })); bodyMesh.castShadow = true; bodyMesh.receiveShadow = true; group.add(bodyMesh); const lens = (b, color, intensity) => { if (!b.count) return; const m = new THREE.Mesh(b.geometry(), new THREE.MeshStandardMaterial({ color: 0x111111, emissive: color, emissiveIntensity: intensity, roughness: 0.3 })); group.add(m); }; lens(lenses.red, 0xff2a1a, 2.2); lens(lenses.green, 0x3cff7a, 2.0); lens(lenses.off, 0x222222, 0.2); } function planeTreeGeometry() { const b = new Builder(); trunk(b, 3.4, 0.22, 0x7b7062); blob(b, 0, 4.6, 0, 2.2, 0xffffff); blob(b, 1.0, 5.4, 0.6, 1.7, 0xf2f2f2); blob(b, -0.9, 5.2, -0.5, 1.6, 0xe8e8e8); return b.geometry(); } function cone(b, y0, h, r, n, color) { for (let k = 0; k < n; k++) { const a0 = (k / n) * Math.PI * 2; const a1 = ((k + 1) / n) * Math.PI * 2; b.tri([Math.cos(a1) * r, y0, Math.sin(a1) * r], [Math.cos(a0) * r, y0, Math.sin(a0) * r], [0, y0 + h, 0], color); b.tri([Math.cos(a0) * r, y0, Math.sin(a0) * r], [Math.cos(a1) * r, y0, Math.sin(a1) * r], [0, y0, 0], color); } } function redwoodGeometry() { const b = new Builder(); trunk(b, 6, 0.45, 0x6b4a3a); cone(b, 3.5, 9, 3.0, 7, 0xffffff); cone(b, 8, 8, 2.4, 7, 0xf0f0f0); cone(b, 12, 7, 1.7, 7, 0xe6e6e6); return b.geometry(); } function pineGeometry() { const b = new Builder(); trunk(b, 5, 0.3, 0x6e5d4b); blob(b, 0, 6.2, 0, 2.8, 0xffffff); blob(b, 1.4, 7.0, 0.4, 2.0, 0xeeeeee); return b.geometry(); } function lampGeometry(style) { const b = new Builder(); const pole = 0x37403d; if (style === "harbor") { b.box(-0.07, 0, -0.07, 0.07, 4.4, 0.07, pole); b.box(-0.2, 4.4, -0.2, 0.2, 4.95, 0.2, 0xfff0c8); b.box(-0.24, 4.95, -0.24, 0.24, 5.05, 0.24, pole); } else { b.box(-0.08, 0, -0.08, 0.08, 6.2, 0.08, pole); b.box(-0.22, 6.2, -0.22, 0.22, 6.75, 0.22, 0xfff0c8); b.box(-0.27, 6.75, -0.27, 0.27, 6.85, 0.27, pole); } return b.geometry(); } /** A rounded sedan: a beveled body from a side profile, glass painted around the cabin, four * wheels with hubcaps, lights. The body is white so the instance color paints it. Faces -z. */ function sedanGeometry() { const parts = []; const side = new THREE.Shape(); // the side profile: x here runs front (-) to back (+), y up side.moveTo(-2.18, 0.34); side.lineTo(2.16, 0.34); side.quadraticCurveTo(2.3, 0.36, 2.28, 0.62); side.lineTo(2.22, 0.86); side.quadraticCurveTo(1.9, 0.95, 1.5, 0.97); side.lineTo(0.78, 1.4); side.quadraticCurveTo(0.2, 1.47, -0.38, 1.43); side.lineTo(-1.16, 0.99); side.quadraticCurveTo(-1.8, 0.93, -2.16, 0.84); side.quadraticCurveTo(-2.3, 0.7, -2.25, 0.5); side.lineTo(-2.18, 0.34); const body = new THREE.ExtrudeGeometry(side, { depth: 1.66, bevelEnabled: true, bevelThickness: 0.08, bevelSize: 0.07, bevelSegments: 2, curveSegments: 4 }); body.translate(0, 0, -0.83); parts.push([body, 0xffffff]); const cabin = new THREE.Shape(); cabin.moveTo(1.44, 0.99); cabin.lineTo(0.76, 1.37); cabin.quadraticCurveTo(0.2, 1.435, -0.36, 1.4); cabin.lineTo(-1.08, 1.0); cabin.lineTo(1.44, 0.99); const glass = new THREE.ExtrudeGeometry(cabin, { depth: 1.84, bevelEnabled: false, curveSegments: 4 }); glass.translate(0, 0.005, -0.92); parts.push([glass, 0x1c2027]); for (const [x, z] of [[-1.38, -0.86], [-1.38, 0.86], [1.4, -0.86], [1.4, 0.86]]) { const w = new THREE.CylinderGeometry(0.33, 0.33, 0.24, 14); w.rotateX(Math.PI / 2); w.translate(x, 0.33, z); parts.push([w, 0x18191b]); const hub = new THREE.CylinderGeometry(0.19, 0.19, 0.26, 10); hub.rotateX(Math.PI / 2); hub.translate(x, 0.33, z); parts.push([hub, 0x9a9da2]); } for (const zz of [-0.62, 0.62]) { const head = new THREE.BoxGeometry(0.05, 0.12, 0.34); head.translate(-2.27, 0.72, zz); parts.push([head, 0xfff2d0]); const tail = new THREE.BoxGeometry(0.05, 0.12, 0.38); tail.translate(2.26, 0.76, zz); parts.push([tail, 0x9a1f1f]); } const pos = []; const nrm = []; const col = []; const c = new THREE.Color(); for (const [g, color] of parts) { const gg = g.index ? g.toNonIndexed() : g; gg.computeVertexNormals(); c.set(color); const p = gg.attributes.position; const n = gg.attributes.normal; for (let i = 0; i < p.count; i++) { // turn the profile a quarter (a rotation, not a mirror) so the car faces -z pos.push(-p.getZ(i), p.getY(i), p.getX(i)); nrm.push(-n.getZ(i), n.getY(i), n.getX(i)); col.push(c.r, c.g, c.b); } } const geo = new THREE.BufferGeometry(); geo.setAttribute("position", new THREE.Float32BufferAttribute(pos, 3)); geo.setAttribute("normal", new THREE.Float32BufferAttribute(nrm, 3)); geo.setAttribute("color", new THREE.Float32BufferAttribute(col, 3)); geo.computeBoundingSphere(); return geo; } function carGeometry() { const b = new Builder(); const body = 0xffffff; const glass = 0x26292e; const tire = 0x1d1e20; b.box(-0.9, 0.28, -2.2, 0.9, 0.95, 2.2, body); b.box(-0.8, 0.95, -1.15, 0.8, 1.45, 0.9, glass, body); b.box(-0.82, 0.95, -1.25, 0.82, 1.02, 1.0, body); for (const x of [-0.92, 0.66]) for (const z of [-1.45, 1.35]) b.box(x, 0, z - 0.33, x + 0.26, 0.62, z + 0.33, tire); b.box(-0.7, 0.55, -2.22, -0.35, 0.72, -2.18, 0xfff2d0); b.box(0.35, 0.55, -2.22, 0.7, 0.72, -2.18, 0xfff2d0); b.box(-0.75, 0.6, 2.18, -0.4, 0.75, 2.22, 0x8a1f1f); b.box(0.4, 0.6, 2.18, 0.75, 0.75, 2.22, 0x8a1f1f); return b.geometry(); } /** Instanced meshes of one geometry, split into chunks; each item is { x, y, z, rot, pitch, s, sy, color }. */ function chunked(group, geometry, material, items, { shadow = true } = {}) { const made = []; const chunks = new Map(); for (const it of items) { const k = `${Math.floor(it.x / CHUNK)},${Math.floor(it.z / CHUNK)}`; if (!chunks.has(k)) chunks.set(k, []); chunks.get(k).push(it); } for (const list of chunks.values()) { const mesh = new THREE.InstancedMesh(geometry, material, list.length); list.forEach((it, k) => { eul.set(it.pitch ?? 0, it.rot ?? 0, 0, "YXZ"); q.setFromEuler(eul); m4.compose(pos.set(it.x, it.y, it.z), q, scl.set(it.s ?? 1, it.sy ?? it.s ?? 1, it.s ?? 1)); mesh.setMatrixAt(k, m4); if (it.color !== undefined) mesh.setColorAt(k, col.set(it.color)); }); mesh.computeBoundingSphere(); mesh.castShadow = shadow; mesh.receiveShadow = true; mesh.userData.center = new THREE.Vector3((Math.floor(list[0].x / CHUNK) + 0.5) * CHUNK, 0, (Math.floor(list[0].z / CHUNK) + 0.5) * CHUNK); group.add(mesh); made.push(mesh); } return made; } const LEAVES = [0x6f8b4f, 0x7a9455, 0x657f48, 0x809a5a, 0x5e7a45]; export function buildProps(scene) { const group = new THREE.Group(); const mat = vertexColored({ roughness: 0.9 }); chunked(group, palmGeometry(), mat, CITY.palms.map((p) => ({ x: p.x, y: heightAt(p.x, p.z) + 0.32, z: p.z, rot: p.r, s: p.s, sy: (p.h / 10) * p.s }))); const planes = []; const redwoods = []; const pines = []; CITY.trees.forEach((t, k) => { // park trees stand on the lawn (and Telegraph Hill's mound), street trees in the sidewalk const item = { x: t.x, y: blockAt(t.x, t.z) ? lawnAt(t.x, t.z) : heightAt(t.x, t.z), z: t.z, rot: hash(k, 9) * 6.28, s: t.s ?? 1, sy: ((t.h ?? 6) / 6) * (t.s ?? 1), color: LEAVES[k % LEAVES.length] }; if (t.kind === "redwood") redwoods.push({ ...item, color: 0x3f5b3b, sy: ((t.h ?? 12) / 14) * 1.4 }); else if (t.kind === "pine") pines.push({ ...item, color: 0x4f6a43 }); else planes.push(item); }); const leafy = vertexColored({ roughness: 0.95 }); // full trees near Claudia, simple ones farther off const near = chunked(group, leafyTreeGeometry(), new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.85 }), planes); const far = chunked(group, planeTreeGeometry(), leafy, planes); chunked(group, redwoodGeometry(), leafy, redwoods); chunked(group, pineGeometry(), leafy, pines); const lampMat = vertexColored({ roughness: 0.6, extra: { emissive: 0x000000 } }); chunked(group, lampGeometry("street"), lampMat, CITY.lamps.filter((l) => l.style !== "harbor").map((l) => ({ x: l.x, y: heightAt(l.x, l.z) + 0.15, z: l.z })), { shadow: false }); chunked(group, lampGeometry("harbor"), lampMat, CITY.lamps.filter((l) => l.style === "harbor").map((l) => ({ x: l.x, y: groundAt(l.x, l.z), z: l.z })), { shadow: false }); const cars = CITY.cars.map((c) => { const fx = Math.sin(c.heading); const fz = -Math.cos(c.heading); const front = heightAt(c.x + fx * 2, c.z + fz * 2); const back = heightAt(c.x - fx * 2, c.z - fz * 2); return { x: c.x, y: (front + back) / 2, z: c.z, rot: -c.heading, pitch: Math.atan2(front - back, 4), color: c.color }; }); // shapely cars near the camera, simple boxes far off const paint = new THREE.MeshStandardMaterial({ vertexColors: true, roughness: 0.32, metalness: 0.25 }); const carsNear = chunked(group, sedanGeometry(), paint, cars); const carsFar = chunked(group, carGeometry(), vertexColored({ roughness: 0.45, extra: { metalness: 0.15 } }), cars); near.push(...carsNear); far.push(...carsFar); signals(group); scene.add(group); return { group, /** Swap tree detail by distance from the camera. */ update(camera) { for (let k = 0; k < near.length; k++) { const d = Math.hypot(near[k].userData.center.x - camera.position.x, near[k].userData.center.z - camera.position.z); near[k].visible = d < 420; far[k].visible = d >= 420; } }, }; } export { carGeometry, sedanGeometry };