Prompt specification
Build or customize the Three.js 3D scene "Worker Bakery Household Loop" 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: Worker Bakery Household Loop
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/worker-bakery-household-loop-x3126
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts circular flows of work, goods, and resources in a local economy. Show a fictional household supplying labor to a bakery and purchasing bread. Animate goods and payments in opposite directions with a clearly bounded toy accounting model. Voxel style.
Working Three.js Starter (worker-bakery-household-loop-x3126):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Common Loop — A Bounded Local Economy</title>
<style>
:root {
--ink: #18332f;
--muted: #63736d;
--paper: rgba(250, 248, 239, 0.94);
--paper-solid: #faf8ef;
--line: rgba(24, 51, 47, 0.15);
--coral: #ed6b5d;
--gold: #f2b844;
--bread: #d8944e;
--flour: #70a873;
--teal: #318d83;
--shadow: 0 16px 50px rgba(31, 58, 50, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe8e2;
color: var(--ink);
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;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 48% 42%, transparent 42%, rgba(38, 66, 58, 0.08) 100%),
linear-gradient(180deg, rgba(255, 255, 255, 0.05), transparent 35%);
z-index: 1;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 3;
}
.card {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.intro {
position: absolute;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 20px 21px 17px;
border-radius: 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.pixel-mark {
display: grid;
grid-template-columns: repeat(3, 5px);
grid-template-rows: repeat(3, 5px);
gap: 2px;
transform: rotate(45deg);
}
.pixel-mark i {
background: var(--teal);
}
.pixel-mark i:nth-child(2),
.pixel-mark i:nth-child(4),
.pixel-mark i:nth-child(6),
.pixel-mark i:nth-child(8) {
background: var(--coral);
}
h1 {
margin: 9px 0 4px;
font-size: clamp(30px, 4vw, 47px);
line-height: 0.92;
letter-spacing: -0.055em;
font-weight: 850;
}
h1 span {
color: var(--teal);
}
.intro p {
margin: 10px 0 15px;
max-width: 330px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.bounds {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
}
.bound {
min-width: 0;
padding: 8px 7px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.44);
}
.bound b {
display: block;
font-size: 14px;
line-height: 1;
}
.bound small {
display: block;
margin-top: 4px;
overflow: hidden;
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.control-hint {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel {
position: absolute;
top: 22px;
right: 22px;
width: 360px;
max-height: calc(100vh - 44px);
overflow: auto;
padding: 18px;
border-radius: 20px;
pointer-events: auto;
scrollbar-width: thin;
scrollbar-color: rgba(24, 51, 47, 0.25) transparent;
}
.panel-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.status::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: #9aa8a2;
content: "";
box-shadow: 0 0 0 4px rgba(154, 168, 162, 0.12);
}
.status[data-state="active"]::before {
background: var(--coral);
box-shadow: 0 0 0 4px rgba(237, 107, 93, 0.13);
}
.status[data-state="done"]::before {
background: var(--flour);
box-shadow: 0 0 0 4px rgba(112, 168, 115, 0.13);
}
.status[data-state="paused"]::before {
background: var(--gold);
box-shadow: 0 0 0 4px rgba(242, 184, 68, 0.14);
}
.cash-total {
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.cash-total b {
margin-left: 4px;
color: var(--ink);
font-size: 12px;
}
.progress {
height: 4px;
margin: 13px 0 17px;
overflow: hidden;
border-radius: 10px;
background: rgba(24, 51, 47, 0.09);
}
.progress > div {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--coral), var(--gold), var(--flour));
transition: width 80ms linear;
}
.transfer {
display: grid;
grid-template-columns: 12px 1fr;
gap: 10px;
min-height: 58px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.48);
}
.transfer-dot {
width: 8px;
height: 8px;
margin-top: 4px;
border-radius: 2px;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(49, 141, 131, 0.12);
}
.transfer b {
display: block;
font-size: 13px;
line-height: 1.25;
}
.transfer small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 17px 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.accounts {
border: 1px solid var(--line);
border-radius: 13px;
overflow: hidden;
background: rgba(255, 255, 255, 0.42);
}
.account {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 10px;
min-height: 57px;
padding: 10px 11px;
}
.account + .account {
border-top: 1px solid var(--line);
}
.account-name {
display: flex;
align-items: center;
gap: 9px;
min-width: 0;
}
.account-icon {
display: grid;
flex: 0 0 28px;
width: 28px;
height: 28px;
place-items: center;
border-radius: 8px;
color: white;
font-size: 10px;
font-weight: 850;
}
.account[data-account="house"] .account-icon {
background: var(--coral);
}
.account[data-account="bakery"] .account-icon {
background: var(--teal);
}
.account[data-account="coop"] .account-icon {
background: var(--flour);
}
.account-name strong {
display: block;
font-size: 11px;
}
.account-name small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.account-numbers {
display: flex;
align-items: flex-end;
flex-direction: column;
gap: 3px;
}
.account-numbers b {
font-size: 13px;
}
.units {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 3px;
}
.units span {
padding: 2px 5px;
border-radius: 4px;
background: rgba(24, 51, 47, 0.07);
color: var(--muted);
font-size: 8px;
font-weight: 750;
white-space: nowrap;
}
.units .empty {
background: transparent;
opacity: 0.5;
}
.ledger {
max-height: 142px;
margin: 0;
padding: 0 3px 0 0;
overflow: auto;
list-style: none;
scrollbar-width: thin;
}
.ledger li {
display: grid;
grid-template-columns: 24px 1fr;
gap: 8px;
padding: 7px 2px;
border-bottom: 1px dashed rgba(24, 51, 47, 0.12);
}
.ledger li:last-child {
border-bottom: 0;
}
.ledger li > span {
padding-top: 1px;
color: #9aa7a1;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.ledger b {
display: block;
font-size: 10px;
line-height: 1.2;
}
.ledger small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.model-note {
margin-top: 14px;
padding: 11px 12px;
border-left: 3px solid var(--gold);
border-radius: 0 9px 9px 0;
background: rgba(242, 184, 68, 0.11);
color: #59665f;
font-size: 10px;
line-height: 1.48;
}
.model-note strong {
color: var(--ink);
}
.buttons {
display: grid;
grid-template-columns: 1fr 78px 70px;
gap: 7px;
margin-top: 14px;
}
button {
min-height: 38px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.58);
color: var(--ink);
font-size: 10px;
font-weight: 800;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease, opacity 150ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
background: white;
}
button:disabled {
cursor: default;
opacity: 0.42;
}
#runBtn {
border-color: var(--ink);
background: var(--ink);
color: #fffdf4;
}
#runBtn:hover:not(:disabled) {
background: #254a43;
}
.legend {
position: absolute;
bottom: 22px;
left: 22px;
display: flex;
align-items: center;
gap: 14px;
padding: 10px 13px;
border-radius: 13px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 9px;
height: 9px;
border-radius: 2px;
}
.world-labels {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
overflow: hidden;
}
.station-tag,
.flow-tag {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -100%);
white-space: nowrap;
will-change: transform, left, top;
}
.station-tag {
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 9px;
background: rgba(250, 248, 239, 0.88);
box-shadow: 0 7px 18px rgba(36, 64, 57, 0.13);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: background 180ms ease, transform 180ms ease;
}
.station-tag small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 7px;
font-weight: 700;
letter-spacing: 0.06em;
}
.station-tag.active {
background: #fffdf4;
transform: translate(-50%, -100%) scale(1.06);
}
.flow-tag {
padding: 4px 7px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-left-width: 3px;
border-radius: 5px;
background: rgba(250, 248, 239, 0.83);
box-shadow: 0 5px 12px rgba(36, 64, 57, 0.09);
color: var(--muted);
font-size: 7px;
font-weight: 850;
letter-spacing: 0.08em;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: #dfe8e2;
color: var(--ink);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
.intro {
top: 10px;
left: 10px;
width: min(350px, calc(100vw - 20px));
padding: 15px;
}
.intro p,
.control-hint {
display: none;
}
.bounds {
margin-top: 13px;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
max-height: 48vh;
padding: 14px;
border-radius: 16px;
}
.ledger,
.model-note {
display: none;
}
.account {
min-height: 48px;
}
.legend {
display: none;
}
.flow-tag {
display: none;
}
.station-tag {
padding: 5px 7px;
font-size: 7px;
}
.station-tag small {
display: none;
}
}
@media (max-height: 690px) and (min-width: 821px) {
.panel {
top: 10px;
right: 10px;
max-height: calc(100vh - 20px);
}
.intro {
top: 10px;
left: 10px;
}
.ledger {
max-height: 84px;
}
.model-note {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<div class="world-labels" id="worldLabels">
<div class="station-tag" id="tag-house">
Household
<small>worker · buyer</small>
</div>
<div class="station-tag" id="tag-bakery">
Bakery
<small>employer · producer</small>
</div>
<div class="station-tag" id="tag-coop">
Resource Co-op
<small>local input</small>
</div>
</div>
<div class="hud">
<section class="intro card">
<div class="eyebrow">
<span class="pixel-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
</span>
Voxel economy / round 01
</div>
<h1>COMMON<br /><span>LOOP</span></h1>
<p>
One household supplies work to one bakery and buys one loaf.
Every exchange stays inside the diorama.
</p>
<div class="bounds">
<div class="bound"><b>$3</b><small>Cash cap</small></div>
<div class="bound"><b>1h</b><small>Work</small></div>
<div class="bound"><b>1</b><small>Flour bag</small></div>
<div class="bound"><b>1</b><small>Loaf out</small></div>
</div>
<div class="control-hint">
<span>Drag to orbit</span>
<span>Scroll to zoom</span>
<span>Double-click to reset view</span>
</div>
</section>
<aside class="panel card">
<div class="panel-top">
<div class="status" id="status" data-state="idle">Ready to trade</div>
<div class="cash-total">Cash <b id="cashTotal">$3</b></div>
</div>
<div class="progress"><div id="progressFill"></div></div>
<div class="transfer">
<div class="transfer-dot" id="transferDot"></div>
<div>
<b id="transferTitle">A bounded seven-step round</b>
<small id="transferDetail">
Work, bread, flour, and payments move around the same three accounts.
</small>
</div>
</div>
<div class="section-title">
<span>Account balances</span>
<span>After completed transfers</span>
</div>
<div class="accounts">
<div class="account" data-account="house">
<div class="account-name">
<div class="account-icon">H</div>
<div>
<strong>Household</strong>
<small>worker · buyer</small>
</div>
</div>
<div class="account-numbers">
<b data-cash>$2</b>
<div class="units" data-units></div>
</div>
</div>
<div class="account" data-account="bakery">
<div class="account-name">
<div class="account-icon">B</div>
<div>
<strong>Bakery</strong>
<small>employer · producer</small>
</div>
</div>
<div class="account-numbers">
<b data-cash>$1</b>
<div class="units" data-units></div>
</div>
</div>
<div class="account" data-account="coop">
<div class="account-name">
<div class="account-icon">C</div>
<div>
<strong>Resource Co-op</strong>
<small>flour supplier</small>
</div>
</div>
<div class="account-numbers">
<b data-cash>$0</b>
<div class="units" data-units></div>
</div>
</div>
</div>
<div class="section-title">
<span>Round ledger</span>
<span>Cash closed</span>
</div>
<ol class="ledger" id="ledger"></ol>
<div class="model-note">
<strong>Accounting boundary:</strong> the $3 opening cash supply is conserved.
The one labor hour and one flour bag are each consumed once.
The round closes with one loaf and no outside trade.
</div>
<div class="buttons">
<button id="runBtn" type="button">Run bounded round</button>
<button id="resetBtn" type="button">Reset</button>
<button id="pauseBtn" type="button" disabled>Pause</button>
</div>
</aside>
<div class="legend card">
<span><i style="background: var(--coral)"></i>Work</span>
<span><i style="background: var(--gold)"></i>Payment</span>
<span><i style="background: var(--bread)"></i>Goods</span>
<span><i style="background: var(--flour)"></i>Resources</span>
</div>
</div>
<div id="loading">Building the local loop…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const COLORS = {
ink: 0x18332f,
ground: 0xd9e3dc,
path: 0xeee5d2,
coral: 0xed6b5d,
gold: 0xf2b844,
bread: 0xd8944e,
flour: 0x70a873,
teal: 0x318d83,
cream: 0xf4e6c6,
wall: 0xeccb8c,
roof: 0xc95e50,
darkRoof: 0x285b4d,
window: 0x83c9d0,
brown: 0x7b5741,
pale: 0xf8f0d9
};
const ui = {
status: document.querySelector("#status"),
cashTotal: document.querySelector("#cashTotal"),
progress: document.querySelector("#progressFill"),
transferTitle: document.querySelector("#transferTitle"),
transferDetail: document.querySelector("#transferDetail"),
transferDot: document.querySelector("#transferDot"),
ledger: document.querySelector("#ledger"),
run: document.querySelector("#runBtn"),
reset: document.querySelector("#resetBtn"),
pause: document.querySelector("#pauseBtn")
};
// -------------------------------------------------------------------------
// Renderer, camera, lights
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe8e2);
scene.fog = new THREE.Fog(0xdfe8e2, 30, 52);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf7fbf4, 0x789087, 2.05);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4d6, 3.2);
sun.position.set(-8, 16, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -16;
sun.shadow.camera.right = 16;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbdd8d1, 0.75);
fill.position.set(9, 7, -8);
scene.add(fill);
// -------------------------------------------------------------------------
// Small voxel construction helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function box(parent, size, position, color, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
typeof color === "number" ? material(color) : color
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeSign(parent, text, bg, fg, width, height, position) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.fillStyle = bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "rgba(255,255,255,.36)";
ctx.lineWidth = 8;
ctx.strokeRect(8, 8, canvas.width - 16, canvas.height - 16);
ctx.fillStyle = fg;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "800 56px system-ui, sans-serif";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({ map: texture })
);
sign.position.set(position[0], position[1], position[2]);
parent.add(sign);
return sign;
}
function windowBlock(parent, x, y, z, w = 0.62, h = 0.65) {
box(parent, [w + 0.12, h + 0.12, 0.08], [x, y, z], 0x3e504b);
box(parent, [w, h, 0.06], [x, y, z + 0.055], COLORS.window);
box(parent, [0.055, h, 0.075], [x, y, z + 0.07], 0xf7f0d9);
}
function makeTree(parent, x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
parent.add(tree);
box(tree, [0.32, 1.25, 0.32], [0, 0.63, 0], COLORS.brown);
box(tree, [1.25, 0.82, 1.05], [0, 1.35, 0], 0x4f8b63);
box(tree, [0.95, 0.72, 0.9], [0.18, 1.95, -0.05], 0x65a36d);
box(tree, [0.72, 0.56, 0.72], [-0.3, 2.37, 0.08], 0x78b478);
}
// -------------------------------------------------------------------------
// Diorama ground
// -------------------------------------------------------------------------
const world = new THREE.Group();
scene.add(world);
box(world, [19.2, 0.58, 14.1], [0, -0.34, 0], 0xb9c9c0, false);
box(world, [18.8, 0.12, 13.7], [0, -0.01, 0], COLORS.ground, false);
let seed = 9137;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
const tileColors = [0xe3e8dd, 0xdde6dc, 0xe9e4d5, 0xd6e0d8];
for (let x = -8.4; x <= 8.4; x += 1.25) {
for (let z = -5.9; z <= 5.9; z += 1.25) {
const nearHouse = x < -3.4 && x > -7.1 && z > -0.2 && z < 3.7;
const nearBakery = x > 1.5 && x < 6.1 && z > -4.5 && z < -1.0;
const nearCoop = x > 1.5 && x < 6.1 && z > 3.3 && z > 3.3 && z < 6.6;
const nearCenter = Math.hypot(x, z) < 2.7;
if (nearHouse || nearBakery || nearCoop || nearCenter) continue;
if (random() < 0.28) continue;
box(
world,
[0.66, 0.035, 0.66],
[x + (random() - 0.5) * 0.12, 0.075, z + (random() - 0.5) * 0.12],
tileColors[Math.floor(random() * tileColors.length)],
false
);
}
}
// Pixelated central plaza.
const plaza = new THREE.Group();
world.add(plaza);
box(plaza, [1.55, 0.22, 1.55], [0, 0.17, 0], 0xc8d1c7);
box(plaza, [1.18, 0.12, 1.18], [0, 0.34, 0], 0xf1ead8);
for (let i = 0; i < 28; i++) {
const angle = (i / 28) * Math.PI * 2;
const block = box(
plaza,
[0.48, 0.10, 0.28],
[Math.cos(angle) * 2.38, 0.09, Math.sin(angle) * 2.38],
i % 2 ? 0xb8c9be : 0xcbd7cc
);
block.rotation.y = -angle;
}
box(plaza, [0.25, 0.3, 0.25], [-0.27, 0.54, 0.05], COLORS.coral);
box(plaza, [0.25, 0.3, 0.25], [0, 0.54, 0.05], COLORS.gold);
box(plaza, [0.25, 0.3, 0.25], [0.27, 0.54, 0.05], COLORS.flour);
makeSign(plaza, "LOCAL", "#f7f1df", "#18332f", 1.15, 0.28, [0, 0.88, 0.08]);
// -------------------------------------------------------------------------
// The three economic actors
// -------------------------------------------------------------------------
const stationRoots = {};
const stationSelections = {};
const stationColors = {
house: COLORS.coral,
bakery: COLORS.teal,
coop: COLORS.flour
};
function addStation(name, x, z) {
const root = new THREE.Group();
root.position.set(x, 0, z);
world.add(root);
stationRoots[name] = root;
const selection = new THREE.Mesh(
new THREE.BoxGeometry(3.6, 0.035, 3.0),
new THREE.MeshBasicMaterial({
color: stationColors[name],
transparent: true,
opacity: 0,
depthWrite: false
})
);
selection.position.y = 0.095;
root.add(selection);
stationSelections[name] = selection;
return root;
}
const house = addStation("house", -5.25, 1.65);
const bakery = addStation("bakery", 3.65, -2.75);
const coop = addStation("coop", 3.65, 4.95);
// Household cottage.
box(house, [3.2, 0.18, 2.9], [0, 0.12, 0], 0xa88e6f);
box(house, [2.65, 1.75, 2.25], [0, 1.03, 0], COLORS.wall);
box(house, [3.08, 0.28, 2.68], [0, 2.03, 0], COLORS.roof);
box(house, [2.48, 0.28, 2.08], [0, 2.31, 0], 0xd96b59);
box(house, [1.75, 0.28, 1.48], [0, 2.59, 0], 0xe17b64);
box(house, [0.38, 1.08, 0.38], [0.82, 2.48, -0.46], 0x895c48);
box(house, [0.64, 1.15, 0.10], [-0.38, 0.72, 1.16], 0x4d6c61);
box(house, [0.49, 1.02, 0.07], [-0.38, 0.73, 1.23], 0x83a99c);
box(house, [0.05, 0.05, 0.05], [-0.19, 0.73, 1.28], COLORS.gold);
windowBlock(house, -0.83, 1.28, 1.15);
windowBlock(house, 0.76, 1.28, 1.15);
makeSign(house, "HOME", "#fff4d8", "#18332f", 1.02, 0.26, [0, 1.85, 1.15]);
// Tiny resident at the front of the house.
const resident = new THREE.Group();
resident.position.set(1.45, 0.22, 1.47);
house.add(resident);
box(resident, [0.28, 0.42, 0.22], [0, 0.35, 0], COLORS.coral);
box(resident, [0.25, 0.25, 0.25], [0, 0.72, 0], 0xe4b48a);
box(resident, [0.10, 0.25, 0.12], [-0.09, 0.08, 0], 0x294b49);
box(resident, [0.10, 0.25, 0.12], [0.09, 0.08, 0], 0x294b49);
// Bakery.
box(bakery, [3.85, 0.18, 3.25], [0, 0.12, 0], 0x9c8d73);
box(bakery, [3.4, 1.95, 2.7], [0, 1.12, 0], COLORS.cream);
box(bakery, [3.92, 0.28, 3.22], [0, 2.22, 0], 0x287c73);
box(bakery, [3.22, 0.28, 2.58], [0, 2.5, 0], 0x36958a);
box(bakery, [2.3, 0.26, 1.9], [0, 2.76, 0], 0x4ca397);
box(bakery, [0.42, 1.25, 0.42], [1.05, 2.62, -0.5], 0x9c7054);
box(bakery, [0.62, 0.16, 0.62], [1.05, 3.3, -0.5], 0x6d5145);
// Striped awning.
for (let i = 0; i < 8; i++) {
box(
bakery,
[0.46, 0.16, 0.62],
[-1.55 + i * 0.44, 1.69, 1.53],
i % 2 ? 0xf3d576 : COLORS.coral
);
}
box(bakery, [0.72, 1.28, 0.10], [-0.86, 0.78, 1.39], 0x396b62);
windowBlock(bakery, 0.66, 1.22, 1.39, 0.82, 0.72);
// Bread visible in the bakery window.
box(bakery, [0.28, 0.13, 0.09], [0.47, 1.16, 1.47], 0xd69a58);
box(bakery, [0.28, 0.13, 0.09], [0.77, 1.16, 1.47], 0xe0aa68);
box(bakery, [0.28, 0.13, 0.09], [0.62, 1.38, 1.47], 0xc78343);
makeSign(bakery, "BAKERY", "#fff1cf", "#18332f", 1.55, 0.35, [0, 2.06, 1.39]);
// Resource co-op.
box(coop, [3.35, 0.18, 2.85], [0, 0.12, 0], 0x9c8d73);
box(coop, [2.95, 1.52, 2.38], [0, 0.91, 0], 0x86a970);
box(coop, [3.42, 0.28, 2.87], [0, 1.78, 0], COLORS.darkRoof);
box(coop, [2.72, 0.28, 2.25], [0, 2.06, 0], 0x386b55);
box(coop, [0.75, 1.12, 0.10], [-0.58, 0.72, 1.23], 0x5d7b4d);
windowBlock(coop, 0.76, 1.02, 1.23, 0.68, 0.55);
makeSign(coop, "CO-OP", "#e9f0d6", "#18332f", 1.25, 0.32, [0, 1.62, 1.23]);
// Co-op crates and flour sacks.
for (let i = 0; i < 3; i++) {
const crate = new THREE.Group();
crate.position.set(-1.68 + i * 0.38, 0.26, 0.9 + (i % 2) * 0.25);
crate.rotation.y = (i - 1) * 0.12;
coop.add(crate);
box(crate, [0.42, 0.42, 0.42], [0, 0.22, 0], 0xb88756);
box(crate, [0.46, 0.07, 0.08], [0, 0.24, 0.22], 0x76543f);
box(crate, [0.31, 0.28, 0.30], [0, 0.57, 0], i === 1 ? 0xd8ddba : 0x8eb579);
}
// Fences, trees, and small details.
for (let i = 0; i < 10; i++) {
box(world, [0.12, 0.72, 0.12], [-7.3 + i * 0.75, 0.42, -6.15], 0x9d7958);
if (i < 9) {
box(world, [0.75, 0.09, 0.10], [-6.93 + i * 0.75, 0.43, -6.15], 0xb18a64);
box(world, [0.75, 0.09, 0.10], [-6.93 + i * 0.75, 0.70, -6.15], 0xb18a64);
}
}
makeTree(world, -8.0, -4.75, 1.05);
makeTree(world, -7.75, 5.55, 0.92);
makeTree(world, 7.65, -5.25, 1.08);
makeTree(world, 7.85, 3.65, 0.82);
makeTree(world, -2.3, -5.65, 0.72);
// -------------------------------------------------------------------------
// Curved flow paths. Each trade has a payment route and a goods/work route
// travelling in the opposite direction.
// -------------------------------------------------------------------------
const nodes = {
house: new THREE.Vector3(-5.25, 2.36, 1.65),
bakery: new THREE.Vector3(3.65, 2.86, -2.75),
coop: new THREE.Vector3(3.65, 2.18, 4.95)
};
function makeRoute(from, to, lateral, arch) {
const a = nodes[from].clone();
const b = nodes[to].clone();
const direction = b.clone().sub(a);
direction.y = 0;
direction.normalize();
const normal = new THREE.Vector3(-direction.z, 0, direction.x);
const c1 = a.clone().addScaledVector(direction, 0.28);
const c2 = b.clone().addScaledVector(direction, -0.28);
c1.addScaledVector(normal, lateral * 0.38);
c2.addScaledVector(normal, lateral);
c1.y = arch * 0.76;
c2.y = arch * 0.9;
const curve = new THREE.CubicBezierCurve3(a, c1, c2, b);
curve.arcLengthDivisions = 180;
return curve;
}
const routeColors = {
laborOut: COLORS.coral,
wage: COLORS.gold,
purchase: COLORS.gold,
bread: COLORS.bread,
resourceFee: COLORS.gold,
flour: COLORS.flour
};
const routes = {
laborOut: makeRoute("house", "bakery", -0.74, 4.42),
wage: makeRoute("bakery", "house", 0.74, 4.02),
purchase: makeRoute("house", "bakery", 1.33, 3.42),
bread: makeRoute("bakery", "house", -1.33, 3.02),
resourceFee: makeRoute("bakery", "coop", 0.72, 3.48),
flour: makeRoute("coop", "bakery", -0.72, 3.08)
};
function makeArrow(color) {
const arrow = new THREE.Group();
const mat = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.72,
depthWrite: false
});
box(arrow, [0.48, 0.075, 0.10], [-0.08, 0, 0], mat, false);
const finA = box(arrow, [0.22, 0.075, 0.10], [0.14, 0, 0.15], mat, false);
const finB = box(arrow, [0.22, 0.075, 0.10], [0.14, 0, -0.15], mat, false);
finA.rotation.y = 0.66;
finB.rotation.y = -0.66;
return arrow;
}
for (const [key, curve] of Object.entries(routes)) {
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, 0.025, 5, false),
new THREE.MeshBasicMaterial({
color: routeColors[key],
transparent: true,
opacity: 0.38,
depthWrite: false
})
);
world.add(tube);
const arrow = makeArrow(routeColors[key]);
arrow.position.copy(curve.getPointAt(0.64));
const tangent = curve.getTangentAt(0.64);
arrow.rotation.y = -Math.atan2(tangent.z, tangent.x);
world.add(arrow);
}
// -------------------------------------------------------------------------
// DOM labels projected from the 3D scene
// -------------------------------------------------------------------------
const labelDefs = {
house: {
element: document.querySelector("#tag-house"),
anchor: new THREE.Vector3(-5.25, 4.15, 1.65)
},
bakery: {
element: document.querySelector("#tag-bakery"),
anchor: new THREE.Vector3(3.65, 4.55, -2.75)
},
coop: {
element: document.querySelector("#tag-coop"),
anchor: new THREE.Vector3(3.65, 3.78, 4.95)
}
};
const flowLabelDefs = [
{ route: "laborOut", text: "1h WORK", color: "#ed6b5d" },
{ route: "wage", text: "$1 WAGE", color: "#f2b844" },
{ route: "purchase", text: "$2 ORDER", color: "#f2b844" },
{ route: "bread", text: "1 LOAF", color: "#d8944e" },
{ route: "resourceFee", text: "$1 INPUT", color: "#f2b844" },
{ route: "flour", text: "1 FLOUR", color: "#70a873" }
];
const flowLabelElements = flowLabelDefs.map((def) => {
const element = document.createElement("div");
element.className = "flow-tag";
element.textContent = def.text;
element.style.borderLeftColor = def.color;
document.querySelector("#worldLabels").appendChild(element);
return {
...def,
element,
anchor: routes[def.route].getPointAt(0.49).add(new THREE.Vector3(0, 0.22, 0))
};
});
function projectLabel(element, anchor) {
const projected = anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.visibility = visible ? "visible" : "hidden";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateLabels() {
for (const def of Object.values(labelDefs)) {
projectLabel(def.element, def.anchor);
}
for (const def of flowLabelElements) {
projectLabel(def.element, def.anchor);
}
}
// -------------------------------------------------------------------------
// Moving voxel tokens
// -------------------------------------------------------------------------
function createToken(kind) {
const token = new THREE.Group();
if (kind === "labor") {
box(token, [0.24, 0.34, 0.18], [0, 0, 0], COLORS.coral);
box(token, [0.20, 0.20, 0.20], [0, 0.27, 0], 0xe8b08b);
box(token, [0.10, 0.18, 0.12], [-0.08, -0.25, 0], 0x274845);
box(token, [0.10, 0.18, 0.12], [0.08, -0.25, 0], 0x274845);
}
if (kind === "cash") {
box(token, [0.25, 0.12, 0.25], [0, 0, 0], COLORS.gold);
box(token, [0.13, 0.14, 0.13], [0, 0, 0], 0xffd975);
}
if (kind === "bread") {
box(token, [0.42, 0.17, 0.25], [0, -0.04, 0], 0xc9803e);
box(token, [0.30, 0.14, 0.19], [0, 0.10, 0], 0xe0a35e);
box(token, [0.06, 0.015, 0.20], [0, 0.18, 0], 0xf1c98f);
}
if (kind === "flour") {
box(token, [0.29, 0.34, 0.25], [0, 0, 0], 0x8db57d);
box(token, [0.31, 0.10, 0.27], [0, 0.02, 0], 0xe6e3bf);
box(token, [0.11, 0.08, 0.08], [0, 0.21, 0], 0x719c69);
}
token.traverse((child) => {
if (child.isMesh) child.castShadow = true;
});
return token;
}
// -------------------------------------------------------------------------
// Bounded toy accounting model
//
// Opening balances:
// Household: $2 cash, 1 labor hour
// Bakery: $1 cash operating float
// Co-op: 1 flour bag
//
// Cash is conserved at $3. Labor and flour are consumed once.
// -------------------------------------------------------------------------
const openingBalances = {
house: { cash: 2, labor: 1, flour: 0, bread: 0 },
bakery: { cash: 1, labor: 0, flour: 0, bread: 0 },
coop: { cash: 0, labor: 0, flour: 1, bread: 0 }
};
let balances = {};
let ledgerEntries = [];
let elapsed = 0;
let completedSegments = 0;
let currentSegmentIndex = -1;
let running = false;
let paused = false;
let activeToken = null;
let autoStartTimer = null;
function cloneBalances() {
return {
house: { ...openingBalances.house },
bakery: { ...openingBalances.bakery },
coop: { ...openingBalances.coop }
};
}
const segments = [
{
key: "laborOut",
route: "laborOut",
token: "labor",
duration: 1.65,
focus: ["house", "bakery"],
title: "Work moves to the bakery",
detail: "1 labor hour · non-cash transfer",
log: "Labor supplied",
logDetail: "Household → Bakery · 1 hour",
color: "#ed6b5d",
apply() {
balances.house.labor -= 1;
balances.bakery.labor += 1;
}
},
{
key: "wage",
route: "wage",
token: "cash",
duration: 1.25,
focus: ["bakery", "house"],
title: "The bakery pays the household",
detail: "$1 wage · opposite the work flow",
log: "Wage paid",
logDetail: "Bakery → Household · $1",
color: "#f2b844",
apply() {
balances.bakery.cash -= 1;
balances.house.cash += 1;
}
},
{
key: "purchase",
route: "purchase",
token: "cash",
duration: 1.25,
focus: ["house", "bakery"],
title: "The household buys one loaf",
detail: "$2 bread payment · opposite the loaf flow",
log: "Bread purchased",
logDetail: "Household → Bakery · $2",
color: "#f2b844",
apply() {
balances.house.cash -= 2;
balances.bakery.cash += 2;
}
},
{
key: "resourceFee",
route: "resourceFee",
token: "cash",
duration: 1.15,
focus: ["bakery", "coop"],
title: "The bakery pays for flour",
detail: "$1 resource payment · opposite the flour flow",
log: "Flour fee paid",
logDetail: "Bakery → Resource Co-op · $1",
color: "#f2b844",
apply() {
balances.bakery.cash -= 1;
balances.coop.cash += 1;
}
},
{
key: "flour",
route: "flour",
token: "flour",
duration: 1.15,
focus: ["coop", "bakery"],
title: "One flour bag reaches the bakery",
detail: "1 local resource · consumed in the bake",
log: "Flour received",
logDetail: "Resource Co-op → Bakery · 1 bag",
color: "#70a873",
apply() {
balances.coop.flour -= 1;
balances.bakery.flour += 1;
}
},
{
key: "bake",
token: "bread",
duration: 0.95,
focus: ["bakery"],
title: "The bakery bakes one loaf",
detail: "1 labor hour + 1 flour bag → 1 bread",
log: "Production completed",
logDetail: "Bakery · 1h + 1 bag → 1 loaf",
color: "#318d83",
apply() {
balances.bakery.labor -= 1;
balances.bakery.flour -= 1;
balances.bakery.bread += 1;
}
},
{
key: "bread",
route: "bread",
token: "bread",
duration: 1.55,
focus: ["bakery", "house"],
title: "One loaf returns to the household",
detail: "1 bread · opposite the $2 purchase",
log: "Bread delivered",
logDetail: "Bakery → Household · 1 loaf",
color: "#d8944e",
apply() {
balances.bakery.bread -= 1;
balances.house.bread += 1;
}
}
];
let timelineCursor = 0;
for (const segment of segments) {
segment.start = timelineCursor;
timelineCursor += segment.duration;
segment.end = timelineCursor;
segment.applied = false;
}
const totalDuration = timelineCursor;
function cashTotal() {
return (
balances.house.cash +
balances.bakery.cash +
balances.coop.cash
);
}
function renderAccounts() {
for (const [name, account] of Object.entries(balances)) {
const row = document.querySelector(`.account[data-account="${name}"]`);
row.querySelector("[data-cash]").textContent = `$${account.cash}`;
const units = [];
if (account.labor) units.push(`<span>${account.labor}h labor</span>`);
if (account.flour) units.push(`<span>${account.flour} flour</span>`);
if (account.bread) units.push(`<span>${account.bread} loaf</span>`);
row.querySelector("[data-units]").innerHTML =
units.length > 0 ? units.join("") : '<span class="empty">—</span>';
}
ui.cashTotal.textContent = `$${cashTotal()}`;
ui.cashTotal.style.color = cashTotal() === 3 ? "" : "#c14f44";
}
function renderLedger() {
ui.ledger.innerHTML = ledgerEntries
.map(
(entry, index) => `
<li>
<span>${String(index + 1).padStart(2, "0")}</span>
<div>
<b>${entry.title}</b>
<small>${entry.detail}</small>
</div>
</li>
`
)
.join("");
ui.ledger.scrollTop = ui.ledger.scrollHeight;
}
function setStatus(text, state = "active") {
ui.status.textContent = text;
ui.status.dataset.state = state;
}
function removeActiveToken() {
if (activeToken) {
scene.remove(activeToken);
activeToken = null;
}
}
function setFocus(stations) {
for (const [name, def] of Object.entries(labelDefs)) {
def.element.classList.toggle("active", stations.includes(name));
}
}
function resetRound() {
if (autoStartTimer) {
clearTimeout(autoStartTimer);
autoStartTimer = null;
}
balances = cloneBalances();
ledgerEntries = [
{
title: "Opening cash",
detail: "Household $2 + Bakery $1 = $3 total"
},
{
title: "Opening inputs",
detail: "1 labor hour + 1 flour bag"
}
];
elapsed = 0;
completedSegments = 0;
currentSegmentIndex = -1;
running = false;
paused = false;
for (const segment of segments) segment.applied = false;
removeActiveToken();
setFocus([]);
ui.progress.style.width = "0%";
ui.transferTitle.textContent = "A bounded seven-step round";
ui.transferDetail.textContent =
"Work, bread, flour, and payments move around the same three accounts.";
ui.transferDot.style.background = "#318d83";
ui.run.disabled = false;
ui.run.textContent = "Run bounded round";
ui.pause.disabled = true;
ui.pause.textContent = "Pause";
setStatus("Ready to trade", "idle");
renderAccounts();
renderLedger();
}
function startRound() {
resetRound();
running = true;
ui.run.disabled = true;
ui.pause.disabled = false;
setStatus("Round in progress", "active");
}
function finishRound() {
running = false;
paused = false;
removeActiveToken();
setFocus([]);
ui.progress.style.width = "100%";
ui.transferTitle.textContent = "The round is closed and balanced";
ui.transferDetail.textContent =
"$3 cash conserved · 1 loaf delivered · no external inputs";
ui.transferDot.style.background = "#70a873";
ui.run.disabled = false;
ui.run.textContent = "Replay bounded round";
ui.pause.disabled = true;
ui.pause.textContent = "Pause";
setStatus("Round closed · balanced", "done");
renderAccounts();
}
function setupSegment(segment) {
removeActiveToken();
if (segment.token) {
activeToken = createToken(segment.token);
scene.add(activeToken);
}
ui.transferTitle.textContent = segment.title;
ui.transferDetail.textContent = segment.detail;
ui.transferDot.style.background = segment.color;
setFocus(segment.focus);
}
function applySegment(segment) {
if (segment.applied) return;
segment.applied = true;
segment.apply();
ledgerEntries.push({
title: segment.log,
detail: segment.logDetail
});
renderAccounts();
renderLedger();
}
function easeOutCubic(t) {
return 1 - Math.pow(1 - t, 3);
}
function animateSegment(segment, localTime, dt) {
if (!activeToken) return;
if (segment.key === "bake") {
const t = easeOutCubic(localTime);
activeToken.position.copy(nodes.bakery).add(new THREE.Vector3(0, 0.26, 0));
activeToken.scale.setScalar(0.22 + t * 0.78);
activeToken.rotation.y = localTime * Math.PI * 2;
activeToken.position.y += Math.sin(localTime * Math.PI) * 0.14;
return;
}
const curve = routes[segment.route];
const t = THREE.MathUtils.clamp(localTime, 0, 1);
activeToken.position.copy(curve.getPointAt(t));
activeToken.position.y += Math.sin(t * Math.PI) * 0.13;
activeToken.rotation.y += dt * 2.8;
activeToken.rotation.z = Math.sin(t * Math.PI) * 0.08;
}
ui.run.addEventListener("click", startRound);
ui.reset.addEventListener("click", resetRound);
ui.pause.addEventListener("click", () => {
if (!running) return;
paused = !paused;
ui.pause.textContent = paused ? "Resume" : "Pause";
setStatus(paused ? "Round paused" : "Round in progress", paused ? "paused" : "active");
});
// -------------------------------------------------------------------------
// Direct camera controls, implemented without Three.js add-ons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-0.25, 0.9, 0.2);
const defaultCamera = { yaw: 0.57, pitch: 0.71, radius: 23.5 };
let cameraYaw = defaultCamera.yaw;
let cameraPitch = defaultCamera.pitch;
let cameraRadius = defaultCamera.radius;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const responsiveRadius = cameraRadius * (aspect < 0.9 ? 1.45 : 1);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * Math.cos(cameraPitch) * responsiveRadius,
cameraTarget.y + Math.sin(cameraPitch) * responsiveRadius,
cameraTarget.z + Math.cos(cameraYaw) * Math.cos(cameraPitch) * responsiveRadius
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(cameraPitch + dy * 0.0045, 0.36, 1.23);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
16,
34
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraYaw = defaultCamera.yaw;
cameraPitch = defaultCamera.pitch;
cameraRadius = defaultCamera.radius;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
updateCamera();
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function frame() {
requestAnimationFrame(frame);
const dt = Math.min(clock.getDelta(), 0.05);
if (running && !paused) {
elapsed = Math.min(totalDuration, elapsed + dt);
// Apply transfers whose destination has been reached.
while (
completedSegments < segments.length &&
elapsed >= segments[completedSegments].end
) {
applySegment(segments[completedSegments]);
completedSegments += 1;
}
if (completedSegments >= segments.length) {
finishRound();
} else {
const segment = segments[completedSegments];
if (currentSegmentIndex !== completedSegments) {
currentSegmentIndex = completedSegments;
setupSegment(segment);
}
const localTime = THREE.MathUtils.clamp(
(elapsed - segment.start) / segment.duration,
0,
1
);
animateSegment(segment, localTime, dt);
ui.progress.style.width = `${(elapsed / totalDuration) * 100}%`;
}
}
// Subtle station selection pulse.
const pulse = 1 + Math.sin(clock.elapsedTime * 4.2) * 0.018;
for (const [name, selection] of Object.entries(stationSelections)) {
const active = labelDefs[name].element.classList.contains("active");
selection.material.opacity = active ? 0.13 + Math.sin(clock.elapsedTime * 5) * 0.025 : 0;
selection.scale.setScalar(active ? pulse : 1);
}
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
resetRound();
updateCamera();
frame();
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
// One automatic demonstration round, which can be reset or replayed.
autoStartTimer = setTimeout(startRound, 850);
</script>
</body>
</html>
```