// The street things, drawn: foil-wrapped burritos and graphics cards turning slowly over the // road with a glow on the asphalt beneath them, and the men pushing shopping carts. Everything // takes its place from the game state. The items share two instanced meshes; the men are a // small pool of meshes handed to whoever is nearest the camera. import * as THREE from "three"; import { groundAt } from "../city.js"; import { burritoHere, createItems, walkersNear } from "../items.js"; import { rng } from "../rng.js"; import { vertexColored } from "./kit.js"; const V = (x, y, z) => new THREE.Vector3(x, y, z); const UP = V(0, 1, 0); /** A three.js geometry, placed and painted: non-indexed, with a color per vertex. `paint` * may be a color or a function of the vertex position giving one. */ function part(geometry, paint, { at = V(0, 0, 0), rot = null, scale = null } = {}) { const g = geometry.index ? geometry.toNonIndexed() : geometry; if (scale) g.scale(scale.x, scale.y, scale.z); if (rot) g.applyQuaternion(new THREE.Quaternion().setFromEuler(rot)); g.translate(at.x, at.y, at.z); const pos = g.attributes.position; const col = new Float32Array(pos.count * 3); const c = new THREE.Color(); const v = new THREE.Vector3(); for (let i = 0; i < pos.count; i++) { v.fromBufferAttribute(pos, i); c.set(typeof paint === "function" ? paint(v) : paint); col.set([c.r, c.g, c.b], i * 3); } g.setAttribute("color", new THREE.BufferAttribute(col, 3)); for (const k of Object.keys(g.attributes)) if (!["position", "normal", "color"].includes(k)) g.deleteAttribute(k); return g; } /** A round rod from a to b. */ function beam(a, b, r, paint, seg = 6) { const d = new THREE.Vector3().subVectors(b, a); const g = new THREE.CylinderGeometry(r, r, d.length(), seg, 1); g.applyQuaternion(new THREE.Quaternion().setFromUnitVectors(UP, d.clone().normalize())); g.translate((a.x + b.x) / 2, (a.y + b.y) / 2, (a.z + b.z) / 2); return part(g, paint); } function merge(parts) { const n = parts.reduce((s, g) => s + g.attributes.position.count, 0); const out = new THREE.BufferGeometry(); for (const k of ["position", "normal", "color"]) { const arr = new Float32Array(n * 3); let o = 0; for (const g of parts) { arr.set(g.attributes[k].array, o); o += g.attributes[k].array.length; } out.setAttribute(k, new THREE.BufferAttribute(arr, 3)); } return out; } // --------------------------------------------------------------------------------------------- // A burrito: a fat tortilla roll, half in crinkled foil, lying on its side. About 0.5 m long: // bigger than life, so she can see it coming at speed. function burritoGeometry() { const random = rng(4242); const parts = []; const tortilla = (v) => { const spot = Math.sin(v.x * 61 + v.y * 23) * Math.sin(v.z * 47 - v.x * 13); return spot > 0.72 ? 0x9c6a35 : spot > 0.45 ? 0xc79a5c : 0xe3c48d; }; const roll = new THREE.CylinderGeometry(0.1, 0.1, 0.3, 18, 4); parts.push(part(roll, tortilla, { at: V(0.1, 0, 0), rot: new THREE.Euler(0, 0, Math.PI / 2) })); // the open end: a soft dome parts.push(part(new THREE.SphereGeometry(0.1, 18, 8, 0, Math.PI * 2, 0, Math.PI / 2), tortilla, { at: V(0.25, 0, 0), rot: new THREE.Euler(0, 0, -Math.PI / 2), scale: V(1, 0.55, 1) })); // the foil: crinkled, a little bigger than the roll, gathered into a twist at the end const foil = new THREE.CylinderGeometry(0.108, 0.108, 0.24, 20, 6); const fp = foil.attributes.position; for (let i = 0; i < fp.count; i++) { const k = 1 + (random() - 0.5) * 0.12; fp.setX(i, fp.getX(i) * k); fp.setZ(i, fp.getZ(i) * k); } foil.computeVertexNormals(); const silver = (v) => (Math.sin(v.y * 90 + v.z * 40) > 0.3 ? 0xe6e8ec : 0xaeb3bb); parts.push(part(foil, silver, { at: V(-0.12, 0, 0), rot: new THREE.Euler(0, 0, Math.PI / 2) })); parts.push(part(new THREE.ConeGeometry(0.108, 0.11, 20, 2), silver, { at: V(-0.295, 0, 0), rot: new THREE.Euler(0, 0, Math.PI / 2) })); const g = merge(parts); g.scale(1.45, 1.45, 1.45); return g; } // A graphics card: a dark shroud with two fans on its face, a silver backplate, gold contacts // along the bottom edge, the bracket at one end and a strip of cool light along the top. function gpuGeometry() { const parts = []; const L = 0.74; const H = 0.3; const T = 0.13; parts.push(part(new THREE.BoxGeometry(L, H, T), 0x2a2d33)); parts.push(part(new THREE.BoxGeometry(L, H, 0.012), 0xa3a9b2, { at: V(0, 0, -T / 2 - 0.006) })); parts.push(part(new THREE.BoxGeometry(L * 0.92, 0.018, T * 0.7), 0x9ef0ff, { at: V(0, H / 2 + 0.006, 0) })); parts.push(part(new THREE.BoxGeometry(L * 0.55, 0.035, 0.012), 0xd5a948, { at: V(-0.06, -H / 2 - 0.016, -0.01) })); parts.push(part(new THREE.BoxGeometry(0.018, H * 1.15, T * 1.25), 0xc4c8ce, { at: V(L / 2 + 0.01, 0.01, 0) })); parts.push(part(new THREE.BoxGeometry(L * 0.98, 0.02, 0.01), 0x8a9099, { at: V(0, -H * 0.3, T / 2 + 0.005) })); // the fans: a dark well, a ring, a hub and seven blades. (On both faces: it turns as it // floats, and from the back a real card is just a plate.) const face = []; for (const fx of [-0.18, 0.18]) { face.push(part(new THREE.CylinderGeometry(0.115, 0.115, 0.01, 28), 0x15171a, { at: V(fx, 0.01, T / 2 + 0.004), rot: new THREE.Euler(Math.PI / 2, 0, 0) })); face.push(part(new THREE.TorusGeometry(0.118, 0.008, 6, 28), 0x8a9099, { at: V(fx, 0.01, T / 2 + 0.01) })); face.push(part(new THREE.CylinderGeometry(0.035, 0.035, 0.02, 16), 0x3b3f46, { at: V(fx, 0.01, T / 2 + 0.012), rot: new THREE.Euler(Math.PI / 2, 0, 0) })); for (let k = 0; k < 7; k++) { const a = (k / 7) * Math.PI * 2; face.push(part(new THREE.BoxGeometry(0.075, 0.03, 0.006), 0x4b5059, { at: V(fx + Math.cos(a) * 0.07, 0.01 + Math.sin(a) * 0.07, T / 2 + 0.01), rot: new THREE.Euler(0.35, 0, a) })); } } parts.push(...face); for (const g of face) parts.push(g.clone().rotateY(Math.PI).translate(0, 0, -0.013)); return merge(parts); } // A soft glow on the road beneath an item: bright at the middle, fading out. function glowGeometry() { const g = new THREE.CircleGeometry(1, 28); g.rotateX(-Math.PI / 2); const pos = g.attributes.position; const col = new Float32Array(pos.count * 3); for (let i = 0; i < pos.count; i++) { const r = Math.hypot(pos.getX(i), pos.getZ(i)); const k = Math.max(0, 1 - r) ** 1.6; col.set([k, k, k], i * 3); } g.setAttribute("color", new THREE.BufferAttribute(col, 3)); return g; } // --------------------------------------------------------------------------------------------- // A man pushing a shopping cart heaped with what he owns. Drawn plainly, like anyone else in // the city: a beanie, a beard, a long coat over a hoodie, work boots. const COATS = [0x5b4a3a, 0x3f4a3c, 0x4a4f5c, 0x6a5440]; const HATS = [0x6b2f2f, 0x2f3d5c, 0x3a3a3a, 0x7a6a3a]; const SKINS = [0xc89a78, 0x9c6e4e, 0xe0b498, 0x7a5038]; function manGeometry(k) { const coat = COATS[k % COATS.length]; const hat = HATS[(k * 3 + 1) % HATS.length]; const skin = SKINS[(k * 5 + 2) % SKINS.length]; const parts = []; // the coat, long and a little shapeless, with a hoodie at the neck parts.push(part(new THREE.CylinderGeometry(0.2, 0.25, 0.62, 10, 2), coat, { at: V(0, 1.16, 0), scale: V(1, 1, 0.7) })); parts.push(part(new THREE.CylinderGeometry(0.25, 0.27, 0.22, 10, 1), coat, { at: V(0, 0.76, 0), scale: V(1, 1, 0.72) })); parts.push(part(new THREE.SphereGeometry(0.2, 10, 6, 0, Math.PI * 2, 0, Math.PI / 2), coat, { at: V(0, 1.46, 0), scale: V(1.05, 0.45, 0.72) })); parts.push(part(new THREE.TorusGeometry(0.085, 0.04, 6, 12), 0x75787e, { at: V(0, 1.53, 0.01), rot: new THREE.Euler(Math.PI / 2, 0, 0) })); // head: skin, a beard, a beanie parts.push(part(new THREE.SphereGeometry(0.105, 14, 10), skin, { at: V(0, 1.67, -0.01), scale: V(0.92, 1.05, 1) })); parts.push(part(new THREE.SphereGeometry(0.1, 12, 8, 0, Math.PI * 2, Math.PI * 0.45, Math.PI * 0.55), 0x4a4540, { at: V(0, 1.66, -0.025), scale: V(0.98, 1.1, 1.05) })); parts.push(part(new THREE.SphereGeometry(0.112, 14, 8, 0, Math.PI * 2, 0, Math.PI * 0.5), hat, { at: V(0, 1.7, -0.005), scale: V(1, 0.9, 1) })); parts.push(part(new THREE.TorusGeometry(0.105, 0.022, 6, 16), hat, { at: V(0, 1.705, -0.005), rot: new THREE.Euler(Math.PI / 2, 0, 0) })); // arms reaching forward and down to the cart's handle for (const s of [-1, 1]) { const shoulder = V(0.21 * s, 1.4, -0.02); const elbow = V(0.23 * s, 1.18, -0.3); const hand = V(0.19 * s, 1.03, -0.5); parts.push(beam(shoulder, elbow, 0.058, coat, 8)); parts.push(beam(elbow, hand, 0.05, coat, 8)); parts.push(part(new THREE.SphereGeometry(0.042, 8, 6), 0x3a3634, { at: hand })); } return merge(parts); } /** One leg, hanging from the hip at the origin: trousers and a boot. */ function legGeometry() { return merge([ part(new THREE.CylinderGeometry(0.085, 0.07, 0.76, 8, 1), 0x3d3a33, { at: V(0, -0.38, 0) }), part(new THREE.BoxGeometry(0.13, 0.1, 0.25), 0x2b241e, { at: V(0, -0.8, -0.04) }), ]); } function cartGeometry(k) { const random = rng(900 + k); const parts = []; const wire = 0xb8bcc2; // the basket, wider at the top, and its wire grid const y0 = 0.48; const y1 = 0.98; const wb = 0.24; // half-width at the bottom const wt = 0.29; // and at the top const z0 = -0.45; // front const z1 = 0.45; // back, by the handle const corner = (sx, top, front) => V(sx * (top ? wt : wb), top ? y1 : y0, front ? z0 + (top ? 0 : 0.08) : z1); for (const top of [false, true]) { for (const sx of [-1, 1]) parts.push(beam(corner(sx, top, true), corner(sx, top, false), 0.012, wire, 4)); for (const front of [true, false]) parts.push(beam(corner(-1, top, front), corner(1, top, front), 0.012, wire, 4)); } for (const sx of [-1, 1]) for (const front of [true, false]) parts.push(beam(corner(sx, false, front), corner(sx, true, front), 0.012, wire, 4)); for (let i = 1; i < 8; i++) { const t = i / 8; for (const sx of [-1, 1]) { const a = corner(sx, false, true).lerp(corner(sx, false, false), t); const b = corner(sx, true, true).lerp(corner(sx, true, false), t); parts.push(beam(a, b, 0.006, wire, 3)); } } for (let i = 1; i < 5; i++) { const t = i / 5; for (const front of [true, false]) parts.push(beam(corner(-1, false, front).lerp(corner(1, false, front), t), corner(-1, true, front).lerp(corner(1, true, front), t), 0.006, wire, 3)); } // the handle, with its red grip parts.push(beam(V(-0.29, 1.03, 0.56), V(0.29, 1.03, 0.56), 0.02, 0xb03a2e, 8)); for (const sx of [-1, 1]) parts.push(beam(V(sx * 0.29, y1, z1), V(sx * 0.29, 1.03, 0.56), 0.012, wire, 4)); // the chassis, the bottom tray and the wheels for (const sx of [-1, 1]) { parts.push(beam(V(sx * 0.22, 0.09, -0.4), V(sx * 0.24, y0, -0.37), 0.014, wire, 4)); parts.push(beam(V(sx * 0.22, 0.09, 0.38), V(sx * 0.27, y0, z1), 0.014, wire, 4)); parts.push(beam(V(sx * 0.22, 0.12, -0.4), V(sx * 0.22, 0.12, 0.38), 0.012, wire, 4)); for (const z of [-0.4, 0.38]) parts.push(part(new THREE.CylinderGeometry(0.06, 0.06, 0.04, 12), 0x222326, { at: V(sx * 0.22, 0.06, z), rot: new THREE.Euler(0, 0, Math.PI / 2) })); } parts.push(part(new THREE.BoxGeometry(0.44, 0.015, 0.75), 0x9fa4ab, { at: V(0, 0.16, 0) })); // what he carries: a black bag, a blue tarp bundle, a rolled sleeping bag, cardboard const load = [ [new THREE.SphereGeometry(0.2, 10, 8), 0x1d1e21, V(-0.08, 1.02, 0.12), V(1, 0.85, 1.1)], [new THREE.SphereGeometry(0.19, 10, 8), 0x3e5f8a, V(0.1, 1.0, -0.18), V(1.1, 0.8, 1)], [new THREE.CylinderGeometry(0.11, 0.11, 0.5, 12), 0x6b6a4a, V(0, 1.18, 0.05), V(1, 1, 1)], [new THREE.BoxGeometry(0.42, 0.04, 0.5), 0xa88458, V(0.02, 1.06, -0.05), V(1, 1, 1)], [new THREE.SphereGeometry(0.14, 10, 8), 0x7a3a34, V(-0.12, 0.25, 0.05), V(1.3, 0.6, 1.2)], ]; for (const [geo, color, at, scale] of load) parts.push(part(geo, color, { at, scale, rot: new THREE.Euler(0, (random() - 0.5) * 0.8, k % 2 ? Math.PI / 2 : 0.15) })); return merge(parts); } // --------------------------------------------------------------------------------------------- const POOL = 10; // men drawn at once: the nearest to the camera export function buildItems(scene) { const layout = createItems(); const material = vertexColored({ roughness: 0.55 }); const glowMat = new THREE.MeshBasicMaterial({ vertexColors: true, transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, fog: true }); const instanced = (geometry, mat, count) => { const m = new THREE.InstancedMesh(geometry, mat, count); m.instanceMatrix.setUsage(THREE.DynamicDrawUsage); m.frustumCulled = false; scene.add(m); return m; }; const burritos = instanced(burritoGeometry(), material, layout.burritos.length); const gpus = instanced(gpuGeometry(), material, layout.gpus.length); burritos.castShadow = gpus.castShadow = true; const glows = instanced(glowGeometry(), glowMat, layout.burritos.length + layout.gpus.length); glows.userData.noAO = true; const warm = new THREE.Color(0.95, 0.55, 0.18); const cool = new THREE.Color(0.25, 0.75, 1.0); for (let k = 0; k < layout.burritos.length; k++) glows.setColorAt(k, warm); for (let k = 0; k < layout.gpus.length; k++) glows.setColorAt(layout.burritos.length + k, cool); const men = []; const legGeo = legGeometry(); for (let k = 0; k < POOL; k++) { const man = new THREE.Group(); const body = new THREE.Mesh(manGeometry(k), material); const legs = [-1, 1].map((s) => { const leg = new THREE.Mesh(legGeo, material); leg.position.set(0.1 * s, 0.86, 0); man.add(leg); return leg; }); man.add(body); const cart = new THREE.Mesh(cartGeometry(k), material); for (const m of [body, cart, ...legs]) m.castShadow = true; man.rotation.order = cart.rotation.order = "YXZ"; scene.add(man, cart); man.visible = cart.visible = false; men.push({ man, body, legs, cart }); } const m4 = new THREE.Matrix4(); const q = new THREE.Quaternion(); const s = new THREE.Vector3(); const pos = new THREE.Vector3(); const e = new THREE.Euler(); const hidden = new THREE.Matrix4().makeScale(0, 0, 0); return { update(game, camera) { const t = game.time; const items = game.items ?? layout; // burritos and GPUs: turning slowly and bobbing, gone once taken const place = (mesh, list, k, here, lift, size, spin, glowAt, glowSize) => { const o = list[k]; if (!here) { mesh.setMatrixAt(k, hidden); glows.setMatrixAt(glowAt, hidden); return; } const bob = Math.sin(t * 2.2 + k * 1.7) * 0.06; pos.set(o.x, o.y + lift + bob, o.z); e.set(spin[0], t * 1.6 + k, spin[1]); q.setFromEuler(e); s.setScalar(size); mesh.setMatrixAt(k, m4.compose(pos, q, s)); const pulse = glowSize * (1 + 0.08 * Math.sin(t * 3 + k)); pos.set(o.x, o.y + 0.03, o.z); q.identity(); s.set(pulse, 1, pulse); glows.setMatrixAt(glowAt, m4.compose(pos, q, s)); }; layout.burritos.forEach((_, k) => place(burritos, items.burritos, k, k < items.burritos.length && (!items.taken || burritoHere(items, k, t)), 0.55, 1, [0.25, 0.2], k, 1.5)); layout.gpus.forEach((_, k) => place(gpus, items.gpus, k, k < items.gpus.length && !items.taken?.gpus.has(items.gpus[k].id), 0.6, 1, [0, 0.12], layout.burritos.length + k, 1.3)); burritos.instanceMatrix.needsUpdate = gpus.instanceMatrix.needsUpdate = glows.instanceMatrix.needsUpdate = true; // the men: the nearest to the camera get the pool const near = walkersNear(items, camera.position.x, camera.position.z, 170, t); near.sort((a, b) => Math.hypot(a.x - camera.position.x, a.z - camera.position.z) - Math.hypot(b.x - camera.position.x, b.z - camera.position.z)); men.forEach((m, k) => { const w = near[k]; m.man.visible = m.cart.visible = !!w; if (!w) return; const stride = Math.sin(w.stride * Math.PI) * w.moving; const lurch = Math.sin(w.stride * 0.9 + w.w.look * 6) * (0.05 + 0.04 * w.moving); m.man.position.set(w.x, groundAt(w.x, w.z) + Math.abs(stride) * 0.03, w.z); m.man.rotation.set(-0.08 * w.moving, -w.heading, lurch); m.legs[0].rotation.x = stride * 0.45; m.legs[1].rotation.x = -stride * 0.45; const gy = groundAt(w.cx, w.cz); m.cart.position.set(w.cx, gy, w.cz); m.cart.rotation.set(0, -w.heading, 0); }); }, }; }