Prompt specification
Build or customize the Three.js 3D scene "Circular cup inventory challenge" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Circular cup inventory challenge
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/circular-cup-inventory-challenge-x0496
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts reusable-cup deposits and returns. Create a festival model with finite cups and timed returns; users place collection points and observe clean, borrowed, and awaiting-wash inventories without creating extra cups. Low-poly.
Working Three.js Starter (circular-cup-inventory-challenge-x0496):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LOOP — Festival Cup Deposit & Return</title>
<style>
:root {
--bg: #09111f;
--panel: rgba(12, 20, 35, 0.82);
--panel-strong: rgba(15, 25, 43, 0.94);
--line: rgba(255, 255, 255, 0.11);
--text: #eef4ff;
--muted: #91a2bc;
--clean: #55dfc0;
--borrowed: #ff727d;
--wash: #ffb657;
--accent: #8da9ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 16px 45px rgba(0, 0, 0, 0.25),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
.brand {
position: absolute;
top: 18px;
left: 18px;
display: flex;
align-items: center;
gap: 12px;
padding: 12px 16px 12px 12px;
pointer-events: none;
}
.brand-mark {
display: grid;
width: 42px;
height: 42px;
place-items: center;
border-radius: 12px;
background: linear-gradient(145deg, var(--clean), #56a9ff);
color: #071620;
font-size: 28px;
font-weight: 900;
box-shadow: 0 7px 22px rgba(85, 223, 192, 0.22);
}
.brand-title {
font-size: 16px;
font-weight: 850;
letter-spacing: 0.14em;
}
.brand-subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.05em;
}
#ledger {
position: absolute;
top: 18px;
right: 18px;
width: 358px;
padding: 16px;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
h2 {
margin: 0;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge {
padding: 5px 8px;
border: 1px solid rgba(85, 223, 192, 0.25);
border-radius: 999px;
background: rgba(85, 223, 192, 0.09);
color: var(--clean);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.metric-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
.metric {
min-width: 0;
padding: 10px 8px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
}
.metric-label {
overflow: hidden;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.07em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.metric-value {
margin-top: 5px;
font-size: 22px;
font-weight: 850;
line-height: 1;
}
.metric.clean .metric-value {
color: var(--clean);
}
.metric.borrowed .metric-value {
color: var(--borrowed);
}
.metric.wash .metric-value {
color: var(--wash);
}
.fleet-bar {
display: flex;
height: 8px;
margin: 14px 0 9px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.06);
}
.fleet-bar span {
height: 100%;
min-width: 0;
transition: width 180ms ease;
}
#cleanBar {
background: var(--clean);
}
#borrowedBar {
background: var(--borrowed);
}
#washBar {
background: var(--wash);
}
.ledger-line {
display: flex;
justify-content: space-between;
gap: 14px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.ledger-line strong {
color: var(--text);
font-weight: 750;
}
.conservation {
margin-top: 12px;
padding: 9px 10px;
border: 1px solid rgba(85, 223, 192, 0.16);
border-radius: 10px;
background: rgba(85, 223, 192, 0.055);
color: #bdeee2;
font-size: 10px;
line-height: 1.45;
}
.event-log {
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.event-title {
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.event {
display: grid;
grid-template-columns: 38px 1fr;
gap: 8px;
padding: 4px 0;
color: #c7d2e6;
font-size: 10px;
line-height: 1.35;
}
.event time {
color: #667a9b;
font-variant-numeric: tabular-nums;
}
#network {
position: absolute;
bottom: 18px;
left: 18px;
width: 326px;
padding: 15px;
}
.button {
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 10px;
background: rgba(255, 255, 255, 0.06);
color: var(--text);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
.button:hover {
border-color: rgba(255, 255, 255, 0.25);
background: rgba(255, 255, 255, 0.11);
}
.button:active {
transform: translateY(1px);
}
.button.active {
border-color: rgba(85, 223, 192, 0.55);
background: rgba(85, 223, 192, 0.14);
color: var(--clean);
}
#placePointBtn {
padding: 7px 10px;
}
#pointList {
display: grid;
gap: 6px;
}
.point-row {
display: grid;
grid-template-columns: 9px 1fr auto;
align-items: center;
gap: 9px;
width: 100%;
padding: 9px 10px;
border: 1px solid transparent;
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
color: var(--text);
cursor: pointer;
text-align: left;
}
.point-row:hover {
background: rgba(255, 255, 255, 0.075);
}
.point-row.selected {
border-color: rgba(141, 169, 255, 0.42);
background: rgba(141, 169, 255, 0.11);
}
.point-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 10px rgba(141, 169, 255, 0.45);
}
.point-name {
overflow: hidden;
font-size: 11px;
font-weight: 750;
text-overflow: ellipsis;
white-space: nowrap;
}
.point-counts {
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
.selected-card {
margin-top: 10px;
padding: 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 11px;
background: rgba(0, 0, 0, 0.15);
}
.selected-name {
margin-bottom: 8px;
font-size: 11px;
font-weight: 800;
}
.selected-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
}
.selected-stat {
padding: 7px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.04);
text-align: center;
}
.selected-stat span {
display: block;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.selected-stat strong {
display: block;
margin-top: 3px;
font-size: 16px;
}
#controls {
position: absolute;
right: 18px;
bottom: 18px;
display: flex;
align-items: center;
gap: 8px;
padding: 9px;
}
.control-button {
min-height: 34px;
padding: 0 12px;
}
.speed-group {
display: flex;
gap: 3px;
padding: 3px;
border-radius: 10px;
background: rgba(0, 0, 0, 0.18);
}
.speed-button {
min-width: 34px;
min-height: 28px;
padding: 0 8px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 10px;
font-weight: 800;
}
.speed-button.active {
background: rgba(141, 169, 255, 0.18);
color: #cbd6ff;
}
#legend {
position: absolute;
right: 18px;
bottom: 78px;
display: flex;
gap: 13px;
padding: 9px 12px;
color: var(--muted);
font-size: 9px;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 3px;
}
#hint {
position: absolute;
bottom: 22px;
left: 50%;
transform: translateX(-50%);
color: rgba(214, 225, 244, 0.62);
font-size: 10px;
text-shadow: 0 2px 7px #000;
white-space: nowrap;
}
#toast {
position: absolute;
top: 92px;
left: 50%;
max-width: 430px;
padding: 10px 15px;
border: 1px solid rgba(85, 223, 192, 0.28);
border-radius: 999px;
background: rgba(8, 19, 31, 0.92);
color: #d9fff5;
font-size: 11px;
font-weight: 650;
opacity: 0;
transform: translate(-50%, -8px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 900px) {
#hint {
display: none;
}
#legend {
right: 10px;
bottom: 68px;
}
#controls {
right: 10px;
bottom: 10px;
}
#network {
bottom: 68px;
left: 10px;
width: 290px;
}
#ledger {
top: 10px;
right: 10px;
width: 325px;
}
.brand {
top: 10px;
left: 10px;
}
}
@media (max-width: 650px) {
.brand {
padding: 9px;
}
.brand-subtitle {
display: none;
}
.brand-title {
font-size: 13px;
}
#ledger {
top: 70px;
width: 250px;
padding: 12px;
}
.metric-grid {
grid-template-columns: repeat(2, 1fr);
}
.event-log,
.conservation {
display: none;
}
#network {
bottom: 65px;
width: 245px;
max-height: 265px;
overflow: auto;
}
.selected-card {
display: none;
}
#legend {
display: none;
}
#controls {
left: 10px;
right: 10px;
justify-content: center;
}
.control-button {
padding: 0 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="hud">
<header class="brand panel">
<div class="brand-mark">↻</div>
<div>
<div class="brand-title">LOOP / FESTIVAL</div>
<div class="brand-subtitle">Deposit, return & wash exchange</div>
</div>
</header>
<section id="ledger" class="panel">
<div class="section-heading">
<h2>Cup ledger</h2>
<span class="badge">Fixed fleet</span>
</div>
<div class="metric-grid">
<div class="metric">
<div class="metric-label">Tracked</div>
<div id="totalCount" class="metric-value">48</div>
</div>
<div class="metric clean">
<div class="metric-label">Clean</div>
<div id="cleanCount" class="metric-value">48</div>
</div>
<div class="metric borrowed">
<div class="metric-label">Borrowed</div>
<div id="borrowedCount" class="metric-value">0</div>
</div>
<div class="metric wash">
<div class="metric-label">Wash</div>
<div id="washCount" class="metric-value">0</div>
</div>
</div>
<div class="fleet-bar" aria-hidden="true">
<span id="cleanBar"></span>
<span id="borrowedBar"></span>
<span id="washBar"></span>
</div>
<div class="ledger-line">
<span>Next timed return</span>
<strong id="nextReturn">—</strong>
</div>
<div class="ledger-line">
<span>Collection points</span>
<strong id="pointTotal">3</strong>
</div>
<div id="conservationText" class="conservation">
48 tracked = 48 clean + 0 borrowed + 0 awaiting wash.
New collection points only redistribute the existing fleet.
</div>
<div class="event-log">
<div class="event-title">Live exchange log</div>
<div id="eventLog"></div>
</div>
</section>
<section id="network" class="panel">
<div class="section-heading">
<h2>Collection points</h2>
<button id="placePointBtn" class="button">+ Place point</button>
</div>
<div id="pointList"></div>
<div class="selected-card">
<div id="selectedName" class="selected-name">Gate Hub</div>
<div class="selected-stats">
<div class="selected-stat">
<span>Clean</span>
<strong id="selectedClean">0</strong>
</div>
<div class="selected-stat">
<span>Wash</span>
<strong id="selectedWash">0</strong>
</div>
<div class="selected-stat">
<span>On loan</span>
<strong id="selectedLoans">0</strong>
</div>
</div>
</div>
</section>
<div id="legend" class="panel">
<span class="legend-item">
<i class="legend-dot" style="background: var(--clean)"></i>Clean
</span>
<span class="legend-item">
<i class="legend-dot" style="background: var(--borrowed)"></i>Borrowed
</span>
<span class="legend-item">
<i class="legend-dot" style="background: var(--wash)"></i>Awaiting wash
</span>
</div>
<nav id="controls" class="panel">
<button id="pauseBtn" class="button control-button">Pause</button>
<div class="speed-group" aria-label="Simulation speed">
<button class="speed-button active" data-speed="1">1×</button>
<button class="speed-button" data-speed="3">3×</button>
<button class="speed-button" data-speed="8">8×</button>
</div>
<button id="cameraBtn" class="button control-button">Reset camera</button>
</nav>
<div id="hint">
Drag to orbit · Shift-drag to pan · Scroll to zoom · Click a kiosk to inspect
</div>
<div id="toast" role="status" aria-live="polite"></div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const TOTAL_CUPS = 48;
const MAX_POINTS = 6;
const COLORS = {
clean: 0x55dfc0,
borrowed: 0xff727d,
wash: 0xffb657,
accent: 0x8da9ff
};
const dom = {
scene: document.querySelector("#scene"),
total: document.querySelector("#totalCount"),
clean: document.querySelector("#cleanCount"),
borrowed: document.querySelector("#borrowedCount"),
wash: document.querySelector("#washCount"),
cleanBar: document.querySelector("#cleanBar"),
borrowedBar: document.querySelector("#borrowedBar"),
washBar: document.querySelector("#washBar"),
nextReturn: document.querySelector("#nextReturn"),
pointTotal: document.querySelector("#pointTotal"),
conservation: document.querySelector("#conservationText"),
eventLog: document.querySelector("#eventLog"),
pointList: document.querySelector("#pointList"),
placePoint: document.querySelector("#placePointBtn"),
selectedName: document.querySelector("#selectedName"),
selectedClean: document.querySelector("#selectedClean"),
selectedWash: document.querySelector("#selectedWash"),
selectedLoans: document.querySelector("#selectedLoans"),
pause: document.querySelector("#pauseBtn"),
camera: document.querySelector("#cameraBtn"),
toast: document.querySelector("#toast")
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x10192b);
scene.fog = new THREE.Fog(0x10192b, 24, 49);
const camera = new THREE.PerspectiveCamera(
46,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
dom.scene.appendChild(renderer.domElement);
const ambient = new THREE.HemisphereLight(0xc4dcff, 0x253b31, 1.65);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xffddb2, 2.5);
sun.position.set(-9, 16, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -21;
sun.shadow.camera.right = 21;
sun.shadow.camera.top = 21;
sun.shadow.camera.bottom = -21;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 48;
sun.shadow.bias = -0.00025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x76a7ff, 1.1);
rimLight.position.set(10, 8, -12);
scene.add(rimLight);
const points = [];
const pointPickMeshes = [];
const cups = [];
const people = [];
const peopleById = new Map();
const events = [];
const boothPositions = [
new THREE.Vector2(-8.2, -1.8),
new THREE.Vector2(8.2, -1.8),
new THREE.Vector2(-8.6, 6.1),
new THREE.Vector2(8.6, 6.1)
];
let pointCounter = 0;
let selectedPointId = null;
let simTime = 0;
let simulationSpeed = 1;
let paused = false;
let placeMode = false;
let nextBorrowAt = 2.5;
const cameraState = {
target: new THREE.Vector3(0, 0.4, 0),
radius: 23,
yaw: 0.62,
pitch: 0.72
};
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.8,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
ground: standardMaterial(0x315648),
islandEdge: standardMaterial(0x1d3431),
path: standardMaterial(0x8e7659),
dance: standardMaterial(0x594a70),
stage: standardMaterial(0x252c42),
stageFront: standardMaterial(0x35415f),
trunk: standardMaterial(0x604638),
treeA: standardMaterial(0x316d59),
treeB: standardMaterial(0x3d8465),
pole: standardMaterial(0x26334b),
speaker: standardMaterial(0x111827),
counter: standardMaterial(0x39435b),
crate: standardMaterial(0x79522f),
cupInside: standardMaterial(0x162332, { roughness: 0.55 })
};
const cupMaterials = {
clean: standardMaterial(COLORS.clean, {
emissive: 0x0d574b,
emissiveIntensity: 0.28,
roughness: 0.55
}),
borrowed: standardMaterial(COLORS.borrowed, {
emissive: 0x681c26,
emissiveIntensity: 0.3,
roughness: 0.58
}),
wash: standardMaterial(COLORS.wash, {
emissive: 0x6b3a08,
emissiveIntensity: 0.25,
roughness: 0.68
})
};
const bulbMaterial = standardMaterial(0xffdf9c, {
emissive: 0xffa53b,
emissiveIntensity: 2.2
});
const wireMaterial = new THREE.LineBasicMaterial({
color: 0x9aa8c7,
transparent: true,
opacity: 0.6
});
function addBox(parent, width, height, depth, material, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
segments,
material,
x,
y,
z
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function buildEnvironment() {
const island = new THREE.Mesh(
new THREE.CylinderGeometry(17, 17, 0.5, 48),
materials.islandEdge
);
island.position.y = -0.3;
island.receiveShadow = true;
scene.add(island);
const ground = new THREE.Mesh(
new THREE.CircleGeometry(16.9, 48),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
ground.name = "placement-ground";
scene.add(ground);
addBox(scene, 29, 0.035, 2.15, materials.path, 0, -0.005, 1.5);
addBox(scene, 2.15, 0.038, 29, materials.path, 0, -0.002, 0);
const danceFloor = new THREE.Mesh(
new THREE.CircleGeometry(3.15, 32),
materials.dance
);
danceFloor.rotation.x = -Math.PI / 2;
danceFloor.position.set(0, 0.025, 2.1);
danceFloor.receiveShadow = true;
scene.add(danceFloor);
buildStage();
buildStalls();
buildTrees();
buildStringLights();
}
function buildStage() {
const stage = new THREE.Group();
stage.position.set(0, 0, -5.7);
scene.add(stage);
addBox(stage, 7.4, 0.55, 4.2, materials.stage, 0, 0.25, 0);
addBox(stage, 7.4, 2.6, 0.3, materials.stageFront, 0, 1.55, -1.95);
addBox(stage, 8, 0.28, 4.8, materials.pole, 0, 3.05, 0);
addBox(stage, 3.2, 0.9, 0.8, materials.stageFront, 0, 1, -0.85);
const sign = makeLabel("FESTIVAL STAGE", 0xd7e1ff, 0x283654);
sign.position.set(0, 2.35, -1.75);
sign.scale.set(4.6, 1.15, 1);
stage.add(sign);
const speakerMaterial = standardMaterial(0x101827);
for (const x of [-3.15, 3.15]) {
addBox(stage, 0.85, 1.65, 0.85, speakerMaterial, x, 1, 0.25);
addBox(stage, 0.65, 1.25, 0.65, speakerMaterial, x, 2, 0.25);
}
const glow = new THREE.PointLight(0xa78bfa, 9, 11, 2);
glow.position.set(0, 3, -3.5);
scene.add(glow);
}
function buildStalls() {
const roofColors = [
0xff766f,
0xf7c956,
0x4ec9b5,
0x9575e8
];
boothPositions.forEach((position, index) => {
const stall = new THREE.Group();
stall.position.set(position.x, 0, position.y);
scene.add(stall);
const accent = standardMaterial(roofColors[index]);
const pale = standardMaterial(0xe2dfd1);
addBox(stall, 2.8, 0.18, 2.2, materials.counter, 0, 0.1, 0);
addBox(stall, 2.3, 1.15, 1.65, pale, 0, 0.75, -0.1);
for (const x of [-1.15, 1.15]) {
for (const z of [-0.7, 0.7]) {
addBox(
stall,
0.09,
2.15,
0.09,
materials.pole,
x,
1.12,
z
);
}
}
const roof = new THREE.Mesh(
new THREE.ConeGeometry(2.1, 1.2, 4),
accent
);
roof.position.y = 2.65;
roof.rotation.y = Math.PI / 4;
roof.castShadow = true;
stall.add(roof);
const label = makeLabel(
["FOOD", "DRINKS", "ART", "INFO"][index],
0xffffff,
roofColors[index]
);
label.position.set(0, 1.65, 0.77);
label.scale.set(2.1, 0.58, 1);
stall.add(label);
});
}
function buildTrees() {
for (let i = 0; i < 22; i += 1) {
const angle = (i / 22) * Math.PI * 2 + 0.12;
const radius = 14.8 + Math.random() * 0.8;
const tree = new THREE.Group();
tree.position.set(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
);
tree.rotation.y = Math.random() * Math.PI;
addCylinder(
tree,
0.18,
0.25,
1.45,
5,
materials.trunk,
0,
0.72,
0
);
const foliage = standardMaterial(
i % 2 ? materials.treeA.color : materials.treeB.color
);
const lower = new THREE.Mesh(
new THREE.ConeGeometry(1.05, 2.2, 6),
foliage
);
lower.position.y = 2.15;
lower.castShadow = true;
tree.add(lower);
const upper = new THREE.Mesh(
new THREE.ConeGeometry(0.72, 1.55, 6),
foliage
);
upper.position.y = 3.05;
upper.castShadow = true;
tree.add(upper);
scene.add(tree);
}
}
function buildStringLights() {
const polePositions = [];
for (let i = 0; i < 8; i += 1) {
const angle = (i / 8) * Math.PI * 2;
const x = Math.cos(angle) * 12.5;
const z = Math.sin(angle) * 12.5;
addCylinder(
scene,
0.08,
0.12,
4.7,
6,
materials.pole,
x,
2.35,
z
);
polePositions.push(new THREE.Vector3(x, 4.55, z));
}
for (let i = 0; i < polePositions.length; i += 1) {
const start = polePositions[i];
const end = polePositions[(i + 1) % polePositions.length];
const middle = start.clone().add(end).multiplyScalar(0.5);
middle.y -= 0.65;
const curve = new THREE.QuadraticBezierCurve3(
start,
middle,
end
);
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(curve.getPoints(20)),
wireMaterial
);
scene.add(line);
for (let j = 1; j <= 5; j += 1) {
const point = curve.getPoint(j / 6);
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 6, 4),
bulbMaterial
);
bulb.position.copy(point);
bulb.position.y -= 0.08;
scene.add(bulb);
}
}
}
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.arcTo(x + width, y, x + width, y + height, r);
context.arcTo(x + width, y + height, x, y + height, r);
context.arcTo(x, y + height, x, y, r);
context.arcTo(x, y, x + width, y, r);
context.closePath();
}
function makeLabel(text, foreground = "#ffffff", background = "#263654") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
roundedRect(context, 5, 5, 502, 118, 28);
context.fillStyle = background;
context.fill();
context.strokeStyle = "rgba(255,255,255,0.18)";
context.lineWidth = 4;
context.stroke();
context.fillStyle = foreground;
context.font = "800 42px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 67);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.renderOrder = 20;
return sprite;
}
function addPoint(name, position, announce = false) {
const id = `point-${pointCounter += 1}`;
const palette = [
0x55dfc0,
0xffb657,
0x8da9ff,
0xff727d,
0xb691f6,
0x67b7ff
];
const accentColor = palette[(pointCounter - 1) % palette.length];
const accent = standardMaterial(accentColor);
const group = new THREE.Group();
group.position.copy(position);
group.rotation.y = Math.atan2(-position.x, -position.z);
addCylinder(
group,
1.25,
1.4,
0.22,
8,
materials.counter,
0,
0.11,
0
);
addBox(group, 1.6, 1.3, 1.05, materials.stageFront, 0, 0.85, -0.18);
addBox(group, 2.25, 0.18, 1.15, materials.counter, 0, 1.43, 0.57);
const roof = new THREE.Mesh(
new THREE.ConeGeometry(1.55, 0.72, 4),
accent
);
roof.position.set(0, 2.35, -0.1);
roof.rotation.y = Math.PI / 4;
roof.castShadow = true;
group.add(roof);
addBox(group, 1.15, 0.48, 0.12, accent, 0, 1.1, 0.39);
const ring = new THREE.Mesh(
new THREE.RingGeometry(1.52, 1.64, 32),
new THREE.MeshBasicMaterial({
color: accentColor,
transparent: true,
opacity: 0.9,
side: THREE.DoubleSide
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.y = 0.035;
ring.visible = false;
group.add(ring);
addBox(group, 1.18, 0.5, 2.15, materials.crate, 1.52, 0.25, -0.18);
const label = makeLabel(name.toUpperCase(), "#ffffff", accentColor);
label.position.set(0, 3.02, -0.05);
label.scale.set(3.15, 0.79, 1);
group.add(label);
const pickMesh = new THREE.Mesh(
new THREE.BoxGeometry(3, 3.25, 3),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
pickMesh.position.y = 1.45;
pickMesh.userData.pointId = id;
group.add(pickMesh);
pointPickMeshes.push(pickMesh);
scene.add(group);
const point = {
id,
name,
position: position.clone(),
group,
ring,
accentColor,
cleanIds: [],
washIds: []
};
points.push(point);
selectPoint(id);
if (announce) {
rebalanceCleanCups();
layoutAllCups();
const moved = point.cleanIds.length;
logEvent(
"network",
`${name} added; ${moved} clean cups redistributed. Fleet remains ${cups.length}.`
);
showToast(
`${name} placed. ${moved} existing clean cups moved there — fleet still ${cups.length}.`
);
}
renderPointList();
return point;
}
function selectPoint(id) {
selectedPointId = id;
for (const point of points) {
point.ring.visible = point.id === id;
}
for (const row of dom.pointList.querySelectorAll(".point-row")) {
row.classList.toggle("selected", row.dataset.pointId === id);
}
}
function createCup(index) {
const id = `C-${String(index + 1).padStart(2, "0")}`;
const mesh = new THREE.Group();
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.145, 0.115, 0.32, 8),
cupMaterials.clean
);
body.castShadow = true;
mesh.add(body);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.025, 4, 8),
cupMaterials.clean
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 0.16;
rim.castShadow = true;
mesh.add(rim);
const inside = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.012, 8),
materials.cupInside
);
inside.position.y = 0.16;
mesh.add(inside);
mesh.scale.setScalar(0.94);
mesh.userData.cupId = id;
scene.add(mesh);
const cup = {
id,
index,
mesh,
body,
rim,
state: "clean",
pointId: null,
originPointId: null,
borrowerId: null,
dueTime: null,
washReady: null
};
cups.push(cup);
return cup;
}
function createPeople() {
const shirts = [
0xf87983,
0x65c9ed,
0xffcb65,
0x9d82e9,
0x65d3ac,
0xf397ba,
0xe2e7ef
];
for (let i = 0; i < 14; i += 1) {
const person = new THREE.Group();
const shirt = standardMaterial(shirts[i % shirts.length]);
const skin = standardMaterial(
[0xe7b18c, 0xb97855, 0x8f5c43, 0xd49a75][i % 4]
);
const trousers = standardMaterial(0x263247);
addCylinder(person, 0.25, 0.33, 0.83, 6, shirt, 0, 1.03, 0);
addBox(person, 0.14, 0.64, 0.16, trousers, -0.15, 0.38, 0);
addBox(person, 0.14, 0.64, 0.16, trousers, 0.15, 0.38, 0);
addBox(person, 0.12, 0.62, 0.13, shirt, -0.36, 1.04, 0);
addBox(person, 0.12, 0.62, 0.13, shirt, 0.36, 1.04, 0);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.225, 0),
skin
);
head.position.y = 1.7;
head.castShadow = true;
person.add(head);
const scale = 0.9 + Math.random() * 0.18;
person.scale.setScalar(scale);
person.rotation.y = Math.random() * Math.PI * 2;
scene.add(person);
const target = chooseWalkTarget();
person.position.set(target.x, 0, target.z);
const data = {
id: `person-${i + 1}`,
group: person,
target: new THREE.Vector3(target.x, 0, target.z),
cupId: null,
phase: Math.random() * Math.PI * 2,
speed: 0.72 + Math.random() * 0.35
};
people.push(data);
peopleById.set(data.id, data);
}
}
function chooseWalkTarget() {
const stage = new THREE.Vector2(0, -5.7);
for (let attempt = 0; attempt < 40; attempt += 1) {
const angle = Math.random() * Math.PI * 2;
const radius = 3.2 + Math.random() * 7.6;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
if (new THREE.Vector2(x, z).distanceTo(stage) < 4.2) {
continue;
}
if (
boothPositions.some(
(booth) => Math.hypot(x - booth.x, z - booth.y) < 2.6
)
) {
continue;
}
if (
points.some(
(point) =>
Math.hypot(
x - point.position.x,
z - point.position.z
) < 1.75
)
) {
continue;
}
return { x, z };
}
return { x: 6, z: 2 };
}
function rebalanceCleanCups() {
const cleanCups = cups
.filter((cup) => cup.state === "clean")
.sort((a, b) => a.index - b.index);
for (const point of points) {
point.cleanIds = [];
}
const base = Math.floor(cleanCups.length / points.length);
const remainder = cleanCups.length % points.length;
let cursor = 0;
points.forEach((point, pointIndex) => {
const amount = base + (pointIndex < remainder ? 1 : 0);
for (let i = 0; i < amount; i += 1) {
const cup = cleanCups[cursor + i];
cup.pointId = point.id;
point.cleanIds.push(cup.id);
}
cursor += amount;
});
}
function layoutPointCups(point) {
point.group.updateMatrixWorld(true);
point.cleanIds.forEach((cupId, index) => {
const cup = cups.find((item) => item.id === cupId);
if (!cup || cup.state !== "clean") return;
const column = index % 4;
const row = Math.floor(index / 4);
const local = new THREE.Vector3(
-0.72 + column * 0.48,
1.66,
0.44 + row * 0.28
);
point.group.localToWorld(local);
cup.mesh.position.copy(local);
cup.mesh.rotation.set(0, 0, 0);
});
point.washIds.forEach((cupId, index) => {
const cup = cups.find((item) => item.id === cupId);
if (!cup || cup.state !== "wash") return;
const column = index % 3;
const row = Math.floor(index / 3);
const local = new THREE.Vector3(
1.15 + column * 0.38,
0.56,
-0.92 + row * 0.34
);
point.group.localToWorld(local);
cup.mesh.position.copy(local);
cup.mesh.rotation.set(0, 0, 0);
});
}
function layoutAllCups() {
for (const point of points) {
layoutPointCups(point);
}
}
function setCupState(cup, state) {
cup.state = state;
cup.body.material = cupMaterials[state];
cup.rim.material = cupMaterials[state];
}
function pickRandom(array) {
return array[Math.floor(Math.random() * array.length)];
}
function borrowCup() {
const availablePoints = points.filter((point) => point.cleanIds.length);
const availablePeople = people.filter((person) => person.cupId === null);
if (!availablePoints.length || !availablePeople.length) {
nextBorrowAt = simTime + 1;
return;
}
const point = pickRandom(availablePoints);
const person = pickRandom(availablePeople);
const cupId = point.cleanIds.pop();
const cup = cups.find((item) => item.id === cupId);
if (!cup) return;
setCupState(cup, "borrowed");
cup.pointId = null;
cup.originPointId = point.id;
cup.borrowerId = person.id;
cup.dueTime = simTime + 17 + Math.random() * 17;
cup.washReady = null;
person.cupId = cup.id;
person.group.add(cup.mesh);
cup.mesh.position.set(0.39, 1.05, 0.12);
cup.mesh.rotation.set(0, 0, -0.18);
layoutPointCups(point);
logEvent(
"borrow",
`${cup.id} deposit taken at ${point.name}; return by ${formatClock(cup.dueTime)}.`
);
}
function returnCup(cup) {
const person = peopleById.get(cup.borrowerId);
if (!person) return;
let nearestPoint = points[0];
let nearestDistance = Infinity;
for (const point of points) {
const distance = Math.hypot(
person.group.position.x - point.position.x,
person.group.position.z - point.position.z
);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestPoint = point;
}
}
person.group.remove(cup.mesh);
scene.add(cup.mesh);
setCupState(cup, "wash");
cup.borrowerId = null;
cup.dueTime = null;
cup.pointId = nearestPoint.id;
cup.originPointId = nearestPoint.id;
cup.washReady = simTime + 5 + Math.random() * 4;
nearestPoint.washIds.push(cup.id);
person.cupId = null;
layoutPointCups(nearestPoint);
logEvent(
"return",
`${cup.id} returned to ${nearestPoint.name}; deposit release and wash queued.`
);
}
function finishWash(cup) {
const point = points.find((item) => item.id === cup.pointId);
if (!point) return;
point.washIds = point.washIds.filter((id) => id !== cup.id);
setCupState(cup, "clean");
cup.pointId = point.id;
cup.washReady = null;
point.cleanIds.push(cup.id);
layoutPointCups(point);
logEvent("wash", `${cup.id} clean and ready to lend at ${point.name}.`);
}
function updateSimulation() {
if (simTime >= nextBorrowAt) {
borrowCup();
nextBorrowAt = simTime + 4.2 + Math.random() * 2.2;
}
for (const cup of cups) {
if (
cup.state === "borrowed" &&
cup.dueTime !== null &&
simTime >= cup.dueTime
) {
returnCup(cup);
}
}
for (const cup of cups) {
if (
cup.state === "wash" &&
cup.washReady !== null &&
simTime >= cup.washReady
) {
finishWash(cup);
}
}
}
function updatePeople(dt) {
for (const person of people) {
const position = person.group.position;
const target = person.target;
const dx = target.x - position.x;
const dz = target.z - position.z;
const distance = Math.hypot(dx, dz);
if (distance < 0.35) {
const next = chooseWalkTarget();
target.set(next.x, 0, next.z);
} else {
const step = Math.min(distance, person.speed * dt);
position.x += (dx / distance) * step;
position.z += (dz / distance) * step;
person.group.rotation.y = Math.atan2(dx, dz);
}
position.y = Math.abs(Math.sin(simTime * 4.5 + person.phase)) * 0.035;
}
}
function getInventory() {
let clean = 0;
let borrowed = 0;
let wash = 0;
for (const cup of cups) {
if (cup.state === "clean") clean += 1;
if (cup.state === "borrowed") borrowed += 1;
if (cup.state === "wash") wash += 1;
}
return { clean, borrowed, wash };
}
function formatClock(seconds) {
const safe = Math.max(0, Math.floor(seconds));
const minutes = Math.floor(safe / 60);
const remainder = String(safe % 60).padStart(2, "0");
return `${minutes}:${remainder}`;
}
function logEvent(type, message) {
events.unshift({
time: formatClock(simTime),
type,
message
});
if (events.length > 5) events.length = 5;
renderEvents();
}
function renderEvents() {
dom.eventLog.replaceChildren();
for (const event of events) {
const row = document.createElement("div");
row.className = "event";
const time = document.createElement("time");
time.textContent = event.time;
const message = document.createElement("span");
message.textContent = event.message;
row.append(time, message);
dom.eventLog.appendChild(row);
}
}
function renderPointList() {
dom.pointList.replaceChildren();
for (const point of points) {
const row = document.createElement("button");
row.className = "point-row";
row.dataset.pointId = point.id;
row.classList.toggle("selected", point.id === selectedPointId);
const dot = document.createElement("span");
dot.className = "point-dot";
dot.style.background = `#${point.accentColor.toString(16).padStart(6, "0")}`;
const name = document.createElement("span");
name.className = "point-name";
name.textContent = point.name;
const counts = document.createElement("span");
counts.className = "point-counts";
row.append(dot, name, counts);
row.addEventListener("click", () => selectPoint(point.id));
dom.pointList.appendChild(row);
}
}
function updateInventoryUI() {
const inventory = getInventory();
const total = cups.length;
dom.total.textContent = `${total} / ${TOTAL_CUPS}`;
dom.clean.textContent = inventory.clean;
dom.borrowed.textContent = inventory.borrowed;
dom.wash.textContent = inventory.wash;
dom.cleanBar.style.width = `${(inventory.clean / TOTAL_CUPS) * 100}%`;
dom.borrowedBar.style.width = `${(inventory.borrowed / TOTAL_CUPS) * 100}%`;
dom.washBar.style.width = `${(inventory.wash / TOTAL_CUPS) * 100}%`;
dom.pointTotal.textContent = points.length;
dom.conservation.textContent =
`${total} tracked = ${inventory.clean} clean + ` +
`${inventory.borrowed} borrowed + ${inventory.wash} awaiting wash. ` +
`New collection points only redistribute the existing fleet.`;
const dueTimes = cups
.filter((cup) => cup.state === "borrowed" && cup.dueTime !== null)
.map((cup) => cup.dueTime);
dom.nextReturn.textContent = dueTimes.length
? formatClock(Math.min(...dueTimes) - simTime)
: "No active loans";
for (const point of points) {
const row = dom.pointList.querySelector(
`[data-point-id="${point.id}"]`
);
if (!row) continue;
const counts = row.querySelector(".point-counts");
const loans = cups.filter(
(cup) =>
cup.state === "borrowed" &&
cup.originPointId === point.id
).length;
counts.textContent =
`${point.cleanIds.length} clean · ${point.washIds.length} wash · ${loans} loan`;
}
const selected = points.find((point) => point.id === selectedPointId);
if (selected) {
const loans = cups.filter(
(cup) =>
cup.state === "borrowed" &&
cup.originPointId === selected.id
).length;
dom.selectedName.textContent = selected.name;
dom.selectedClean.textContent = selected.cleanIds.length;
dom.selectedWash.textContent = selected.washIds.length;
dom.selectedLoans.textContent = loans;
}
}
let toastTimer = null;
function showToast(message) {
dom.toast.textContent = message;
dom.toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
dom.toast.classList.remove("visible");
}, 3200);
}
function updateCamera() {
const sinPitch = Math.sin(cameraState.pitch);
const offset = new THREE.Vector3(
cameraState.radius * sinPitch * Math.sin(cameraState.yaw),
cameraState.radius * Math.cos(cameraState.pitch),
cameraState.radius * sinPitch * Math.cos(cameraState.yaw)
);
camera.position.copy(cameraState.target).add(offset);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.set(0, 0.4, 0);
cameraState.radius = 23;
cameraState.yaw = 0.62;
cameraState.pitch = 0.72;
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragState = null;
function setPointerFromEvent(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function groundPointFromEvent(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObject(
scene.getObjectByName("placement-ground"),
false
)[0];
return hit ? hit.point : null;
}
function selectPointFromEvent(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(pointPickMeshes, false)[0];
if (hit?.object.userData.pointId) {
selectPoint(hit.object.userData.pointId);
}
}
function placePointFromEvent(event) {
const position = groundPointFromEvent(event);
if (!position) {
showToast("Choose a spot on the festival ground.");
return;
}
if (points.length >= MAX_POINTS) {
setPlaceMode(false);
showToast(`The festival supports up to ${MAX_POINTS} collection points.`);
return;
}
const radius = Math.hypot(position.x, position.z);
if (radius > 12.7) {
showToast("Place the collection point closer to the festival.");
return;
}
if (Math.hypot(position.x, position.z - 1.8) < 2.8) {
showToast("Keep the central dance floor clear.");
return;
}
if (Math.hypot(position.x, position.z + 5.7) < 4.1) {
showToast("Choose a spot away from the stage.");
return;
}
if (
boothPositions.some(
(booth) =>
Math.hypot(position.x - booth.x, position.z - booth.y) < 2.5
)
) {
showToast("Choose a spot away from the festival stalls.");
return;
}
if (
points.some(
(point) =>
Math.hypot(
position.x - point.position.x,
position.z - point.position.z
) < 2.6
)
) {
showToast("Leave more space between collection points.");
return;
}
setPlaceMode(false);
addPoint(
`Pop-up Hub ${points.length + 1}`,
new THREE.Vector3(position.x, 0, position.z),
true
);
}
function setPlaceMode(enabled) {
placeMode = enabled;
dom.placePoint.classList.toggle("active", enabled);
dom.placePoint.textContent = enabled
? "Cancel placement"
: "+ Place point";
renderer.domElement.style.cursor = enabled ? "crosshair" : "grab";
}
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
dragState = {
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false,
mode: placeMode
? "place"
: event.shiftKey || event.button === 2
? "pan"
: "orbit"
};
renderer.domElement.style.cursor = dragState.mode === "pan"
? "move"
: "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragState) return;
const dx = event.clientX - dragState.lastX;
const dy = event.clientY - dragState.lastY;
if (
Math.hypot(
event.clientX - dragState.startX,
event.clientY - dragState.startY
) > 4
) {
dragState.moved = true;
}
if (dragState.mode === "orbit") {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.32,
1.27
);
}
if (dragState.mode === "pan") {
const right = new THREE.Vector3();
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
right.crossVectors(forward, camera.up).normalize();
const panScale = cameraState.radius * 0.0017;
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, -dy * panScale);
const targetRadius = Math.hypot(
cameraState.target.x,
cameraState.target.z
);
if (targetRadius > 8) {
cameraState.target.x *= 8 / targetRadius;
cameraState.target.z *= 8 / targetRadius;
}
}
dragState.lastX = event.clientX;
dragState.lastY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!dragState) return;
const wasClick = !dragState.moved;
if (wasClick && dragState.mode === "place") {
placePointFromEvent(event);
} else if (
wasClick &&
dragState.mode === "orbit" &&
event.button === 0
) {
selectPointFromEvent(event);
}
dragState = null;
renderer.domElement.style.cursor = placeMode ? "crosshair" : "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
dragState = null;
renderer.domElement.style.cursor = placeMode ? "crosshair" : "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
9,
38
);
},
{ passive: false }
);
dom.placePoint.addEventListener("click", () => {
if (points.length >= MAX_POINTS) {
showToast(`The festival supports up to ${MAX_POINTS} collection points.`);
return;
}
setPlaceMode(!placeMode);
});
dom.pause.addEventListener("click", () => {
paused = !paused;
dom.pause.textContent = paused ? "Resume" : "Pause";
dom.pause.classList.toggle("active", paused);
});
for (const button of document.querySelectorAll(".speed-button")) {
button.addEventListener("click", () => {
simulationSpeed = Number(button.dataset.speed);
for (const item of document.querySelectorAll(".speed-button")) {
item.classList.toggle("active", item === button);
}
});
}
dom.camera.addEventListener("click", resetCamera);
addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "BUTTON" || tag === "INPUT") return;
if (event.code === "Space") {
event.preventDefault();
dom.pause.click();
}
if (event.key.toLowerCase() === "p") {
if (points.length < MAX_POINTS) setPlaceMode(!placeMode);
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
if (event.key === "Escape") {
setPlaceMode(false);
}
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
buildEnvironment();
addPoint(
"Gate Hub",
new THREE.Vector3(-4.7, 0, 4.2)
);
addPoint(
"Meadow Hub",
new THREE.Vector3(4.7, 0, 4.2)
);
addPoint(
"North Hub",
new THREE.Vector3(0.2, 0, 7.3)
);
createPeople();
for (let i = 0; i < TOTAL_CUPS; i += 1) {
createCup(i);
}
rebalanceCleanCups();
layoutAllCups();
selectPoint(points[0].id);
renderPointList();
logEvent(
"system",
`Fleet initialized with exactly ${TOTAL_CUPS} reusable cups.`
);
updateInventoryUI();
updateCamera();
renderer.domElement.style.cursor = "grab";
const clock = new THREE.Clock();
let uiAccumulator = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
const simulationDt = dt * simulationSpeed;
simTime += simulationDt;
updateSimulation();
updatePeople(simulationDt);
}
updateCamera();
renderer.render(scene, camera);
uiAccumulator += dt;
if (uiAccumulator >= 0.15) {
updateInventoryUI();
uiAccumulator = 0;
}
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```