Prompt specification
Build or customize the Three.js 3D scene "Neighborhood Production and Waste Network" 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: Neighborhood Production and Waste Network
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-production-and-waste-network-x3127
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. Arrange homes, workshops, shops, and collection points on a small map. Trace one product from material arrival to use and discard without merging it with payment arrows. Voxel style.
Working Three.js Starter (neighborhood-production-and-waste-network-x3127):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Local Loop — A Circular Neighborhood Economy</title>
<style>
:root {
--ink: #26372f;
--muted: #64736b;
--paper: rgba(250, 248, 238, 0.92);
--line: rgba(38, 55, 47, 0.13);
--work: #8b6fd6;
--goods: #159d9d;
--resources: #dfa039;
--payments: #e56759;
--trace: #a7db42;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e6d9;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
background: var(--paper);
box-shadow:
0 18px 50px rgba(44, 56, 47, 0.12),
inset 0 0 0 1px rgba(38, 55, 47, 0.035);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: #718078;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--trace);
content: "";
box-shadow: 0 0 0 4px rgba(167, 219, 66, 0.18);
}
header {
top: 22px;
left: 22px;
width: 280px;
padding: 20px 22px 19px;
pointer-events: none;
}
header h1 {
margin: 0;
font-size: clamp(29px, 3vw, 43px);
font-weight: 760;
letter-spacing: -0.055em;
line-height: 0.95;
}
header p {
margin: 13px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#legend {
top: 22px;
right: 22px;
width: 242px;
padding: 16px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 10px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-title small {
color: #8b968f;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
}
.legend-row {
display: grid;
grid-template-columns: 27px 1fr 18px;
align-items: center;
width: 100%;
margin: 2px 0;
padding: 7px 5px;
border: 0;
border-radius: 10px;
background: transparent;
color: var(--ink);
font: inherit;
text-align: left;
cursor: pointer;
transition: background 160ms ease, opacity 160ms ease;
}
.legend-row:hover {
background: rgba(38, 55, 47, 0.06);
}
.legend-row.off {
opacity: 0.38;
}
.legend-line {
position: relative;
display: block;
width: 21px;
height: 8px;
}
.legend-line::before {
position: absolute;
top: 3px;
left: 0;
width: 21px;
height: 3px;
border-radius: 3px;
background: var(--swatch);
content: "";
}
.legend-line.dashed::before {
background: repeating-linear-gradient(
90deg,
var(--swatch) 0 6px,
transparent 6px 9px
);
}
.legend-line.solid::before {
height: 4px;
box-shadow: 0 0 0 2px rgba(167, 219, 66, 0.15);
}
.legend-copy {
display: flex;
flex-direction: column;
gap: 1px;
}
.legend-copy strong {
font-size: 11px;
font-weight: 750;
}
.legend-copy span {
color: #7b877f;
font-size: 9px;
}
.visibility-dot {
width: 7px;
height: 7px;
margin-left: auto;
border-radius: 50%;
background: var(--swatch);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--swatch) 18%, transparent);
}
.legend-row.off .visibility-dot {
background: transparent;
box-shadow: inset 0 0 0 1px #9ca69f;
}
#inspect {
right: 22px;
bottom: 106px;
width: 242px;
padding: 16px;
pointer-events: none;
}
#inspect .type {
color: #839087;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#inspect h3 {
margin: 6px 0 7px;
font-size: 16px;
letter-spacing: -0.025em;
}
#inspect p {
margin: 0;
color: #68766d;
font-size: 10px;
line-height: 1.55;
}
#trace-panel {
position: fixed;
z-index: 12;
bottom: 21px;
left: 50%;
display: grid;
grid-template-columns: 46px minmax(190px, 390px) auto;
gap: 13px;
align-items: center;
width: min(620px, calc(100vw - 44px));
padding: 12px 13px 12px 12px;
border: 1px solid rgba(255, 255, 255, 0.82);
border-radius: 18px;
background: rgba(250, 248, 238, 0.95);
box-shadow: 0 18px 50px rgba(44, 56, 47, 0.15);
transform: translateX(-50%);
backdrop-filter: blur(16px);
}
.trace-badge {
display: grid;
width: 46px;
height: 46px;
place-items: center;
border-radius: 13px;
background: var(--trace);
color: #26372f;
font-size: 20px;
font-weight: 900;
box-shadow: inset 0 -3px 0 rgba(38, 55, 47, 0.12);
}
.trace-copy {
min-width: 0;
}
.trace-kicker {
margin-bottom: 3px;
color: #77847c;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#trace-stage {
margin-bottom: 3px;
font-size: 14px;
font-weight: 800;
letter-spacing: -0.02em;
}
#trace-description {
overflow: hidden;
color: #68766d;
font-size: 10px;
line-height: 1.35;
text-overflow: ellipsis;
white-space: nowrap;
}
#replay {
padding: 9px 12px;
border: 1px solid rgba(38, 55, 47, 0.12);
border-radius: 10px;
background: white;
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
cursor: pointer;
}
#replay:hover {
border-color: rgba(38, 55, 47, 0.25);
transform: translateY(-1px);
}
#trace-progress {
grid-column: 2 / 4;
height: 3px;
overflow: hidden;
border-radius: 4px;
background: rgba(38, 55, 47, 0.09);
}
#trace-progress span {
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--trace);
transform: scaleX(0);
transform-origin: left;
}
#controls {
position: fixed;
z-index: 11;
bottom: 21px;
left: 22px;
display: flex;
gap: 6px;
padding: 6px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 13px;
background: rgba(250, 248, 238, 0.9);
box-shadow: 0 12px 30px rgba(44, 56, 47, 0.1);
backdrop-filter: blur(12px);
}
#controls button {
width: 32px;
height: 32px;
padding: 0;
border: 0;
border-radius: 8px;
background: transparent;
color: var(--ink);
font: inherit;
font-size: 16px;
font-weight: 800;
cursor: pointer;
}
#controls button:hover {
background: rgba(38, 55, 47, 0.08);
}
#controls .wide {
width: auto;
padding: 0 10px;
font-size: 10px;
}
.world-label {
position: fixed;
z-index: 4;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px 5px 6px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 8px;
background: rgba(250, 248, 238, 0.88);
box-shadow: 0 5px 14px rgba(44, 56, 47, 0.1);
color: var(--ink);
font-size: 9px;
font-weight: 800;
pointer-events: none;
transform: translate(-50%, -100%);
white-space: nowrap;
backdrop-filter: blur(8px);
}
.world-label .category {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--label-color);
}
.world-label .sub {
color: #7e8982;
font-size: 8px;
font-weight: 650;
}
.stage-tag {
gap: 5px;
padding: 4px 7px 4px 4px;
border-color: rgba(167, 219, 66, 0.75);
background: rgba(247, 252, 232, 0.94);
}
.stage-tag b {
display: grid;
width: 19px;
height: 19px;
place-items: center;
border-radius: 5px;
background: var(--trace);
color: #26372f;
font-size: 9px;
}
.stage-tag.active {
box-shadow:
0 0 0 3px rgba(167, 219, 66, 0.2),
0 7px 18px rgba(44, 56, 47, 0.12);
}
.flow-label {
padding: 3px 6px;
border: 0;
border-radius: 5px;
background: rgba(38, 55, 47, 0.76);
color: white;
font-size: 7px;
letter-spacing: 0.12em;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #e8e6d9;
color: #526159;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
header {
width: 225px;
padding: 16px;
}
header h1 {
font-size: 31px;
}
#legend {
width: 205px;
}
#inspect {
display: none;
}
#trace-panel {
grid-template-columns: 40px minmax(160px, 1fr) auto;
width: calc(100vw - 160px);
}
}
@media (max-width: 620px) {
header {
top: 12px;
left: 12px;
width: 190px;
}
header p {
display: none;
}
#legend {
top: 12px;
right: 12px;
width: 168px;
padding: 11px;
}
.legend-copy span,
.legend-row .visibility-dot {
display: none;
}
.legend-row {
grid-template-columns: 26px 1fr;
padding: 5px 2px;
}
#trace-panel {
bottom: 12px;
left: 12px;
grid-template-columns: 37px 1fr auto;
width: calc(100vw - 24px);
transform: none;
}
.trace-badge {
width: 37px;
height: 37px;
}
#trace-description {
display: none;
}
#controls {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel map of a circular local economy"></canvas>
<header class="panel">
<div class="eyebrow">Local Loop / 01</div>
<h1>Neighborhood<br />economy</h1>
<p>
Work, goods, resources, and value circulate through one small neighborhood.
Drag to orbit. Scroll to zoom.
</p>
</header>
<section id="legend" class="panel" aria-label="Flow legend">
<h2 class="panel-title">
Flow key <small>click to show / hide</small>
</h2>
<button class="legend-row" data-layer="work" style="--swatch: var(--work)">
<span class="legend-line dashed"></span>
<span class="legend-copy">
<strong>Work moves</strong>
<span>People and skills</span>
</span>
<span class="visibility-dot"></span>
</button>
<button class="legend-row" data-layer="goods" style="--swatch: var(--goods)">
<span class="legend-line"></span>
<span class="legend-copy">
<strong>Goods</strong>
<span>Products and services</span>
</span>
<span class="visibility-dot"></span>
</button>
<button class="legend-row" data-layer="resources" style="--swatch: var(--resources)">
<span class="legend-line dashed"></span>
<span class="legend-copy">
<strong>Resources</strong>
<span>Materials and reuse</span>
</span>
<span class="visibility-dot"></span>
</button>
<button class="legend-row" data-layer="payments" style="--swatch: var(--payments)">
<span class="legend-line dashed"></span>
<span class="legend-copy">
<strong>Payments</strong>
<span>Separate value rail</span>
</span>
<span class="visibility-dot"></span>
</button>
<button class="legend-row" data-layer="trace" style="--swatch: var(--trace)">
<span class="legend-line solid"></span>
<span class="legend-copy">
<strong>Traced product</strong>
<span>One mug, five stages</span>
</span>
<span class="visibility-dot"></span>
</button>
</section>
<section id="inspect" class="panel">
<div id="inspect-type" class="type">Map note</div>
<h3 id="inspect-title">A circular neighborhood</h3>
<p id="inspect-copy">
Hover over a building to inspect its role. The bright green route follows one
product and never represents payment.
</p>
</section>
<section id="trace-panel" aria-live="polite">
<div class="trace-badge">↻</div>
<div class="trace-copy">
<div class="trace-kicker">Traced product · reclaimed clay mug</div>
<div id="trace-stage">01 — Material arrival</div>
<div id="trace-description">
Collected material is weighed and prepared at the intake bay.
</div>
</div>
<button id="replay" type="button">Replay trace</button>
<div id="trace-progress"><span></span></div>
</section>
<nav id="controls" aria-label="Camera controls">
<button id="view-left" title="Rotate left" type="button">‹</button>
<button id="view-right" title="Rotate right" type="button">›</button>
<button id="zoom-out" title="Zoom out" type="button">−</button>
<button id="zoom-in" title="Zoom in" type="button">+</button>
<button id="reset" class="wide" type="button">Reset view</button>
</nav>
<div id="loading">Building the neighborhood…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#e8e6d9");
scene.fog = new THREE.Fog("#e8e6d9", 38, 72);
const camera = new THREE.OrthographicCamera(-20, 20, 12, -12, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0, 0);
let cameraAzimuth = 0.63;
let cameraElevation = 0.83;
let cameraZoom = 1;
const hemi = new THREE.HemisphereLight("#fff9df", "#819481", 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight("#fff1c9", 3.2);
sun.position.set(-14, 24, 15);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b8d9cf", 0.8);
fill.position.set(18, 9, -16);
scene.add(fill);
// -------------------------------------------------------------------------
// Small helpers
// -------------------------------------------------------------------------
const COLORS = {
ground: "#cbd5b8",
groundEdge: "#aebc9e",
road: "#e7dfcd",
plaza: "#ddd8c5",
dark: "#30443b",
glass: "#8ec3c5",
glassWarm: "#f3c96f",
cream: "#f1e9d3",
white: "#fffaf0",
homeWall: "#eadfc5",
homeRoof: "#bd654b",
workshopWall: "#aab8b0",
workshopRoof: "#526c68",
shopWall: "#e6cfaa",
shopRoof: "#357f7d",
collectionWall: "#b8c3a4",
collectionRoof: "#577a4f",
work: "#8b6fd6",
goods: "#159d9d",
resources: "#dfa039",
payments: "#e56759",
trace: "#a7db42"
};
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshLambertMaterial({ color, flatShading: true })
);
}
return materialCache.get(color);
}
function box(
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function groupAt(x, y, z) {
const group = new THREE.Group();
group.position.set(x, y, z);
scene.add(group);
return group;
}
function updateCamera() {
const distance = 34;
const horizontal = Math.cos(cameraElevation) * distance;
camera.position.set(
cameraTarget.x + Math.sin(cameraAzimuth) * horizontal,
cameraTarget.y + Math.sin(cameraElevation) * distance,
cameraTarget.z + Math.cos(cameraAzimuth) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const viewHeight = Math.max(22, 30 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
updateCamera();
}
// -------------------------------------------------------------------------
// Labels
// -------------------------------------------------------------------------
const worldLabels = [];
function createWorldLabel({
html,
position,
className = "",
color = "#30443b",
layer = null
}) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.style.setProperty("--label-color", color);
element.innerHTML = html;
document.body.appendChild(element);
const label = {
element,
position: position.clone(),
layer
};
worldLabels.push(label);
return label;
}
function createBuildingLabel(data, anchor) {
return createWorldLabel({
html: `
<span class="category"></span>
<span>${data.name}</span>
<span class="sub">${data.type}</span>
`,
position: anchor,
color: data.color
});
}
function createFlowLabel(text, color, x, y, z, layer) {
return createWorldLabel({
html: text,
position: new THREE.Vector3(x, y, z),
className: "flow-label",
color,
layer
});
}
// -------------------------------------------------------------------------
// Ground, roads, common, and landscape
// -------------------------------------------------------------------------
box(29.6, 0.85, 21.6, COLORS.groundEdge, 0, -0.58, 0);
box(29, 0.18, 21, COLORS.ground, 0, -0.07, 0);
for (const z of [-7.6, -0.2, 8.4]) {
box(27.8, 0.075, 1.45, COLORS.road, 0, 0.055, z);
}
for (const x of [-12.1, -4.5, 4.8, 11.1]) {
box(1.4, 0.075, 18.9, COLORS.road, x, 0.057, 0);
}
box(5.2, 0.12, 4.6, COLORS.plaza, 0, 0.09, -0.55);
// Painted town-common details.
box(0.13, 0.035, 4.2, "#efe8d5", -2.15, 0.17, -0.55);
box(0.13, 0.035, 4.2, "#efe8d5", 2.15, 0.17, -0.55);
box(4.2, 0.035, 0.13, "#efe8d5", 0, 0.17, -2.65);
box(4.2, 0.035, 0.13, "#efe8d5", 0, 0.17, 1.55);
function addTree(x, z, scale = 1) {
const tree = groupAt(x, 0, z);
tree.scale.setScalar(scale);
box(0.38, 1.3, 0.38, "#85684b", 0, 0.7, 0, tree);
box(1.55, 0.9, 1.45, "#668e59", 0, 1.75, 0, tree);
box(1.1, 0.8, 1.1, "#7da567", 0.1, 2.35, -0.05, tree);
box(0.72, 0.58, 0.72, "#91b876", -0.28, 2.72, 0.04, tree);
}
[
[-13.2, 8.8, 0.9],
[-7.7, 9.3, 0.75],
[-3.4, 9.3, 0.68],
[8.7, 9.2, 0.82],
[12.6, 6.3, 0.9],
[12.6, -7.9, 0.83],
[6.1, -9.1, 0.74],
[-0.5, -9.2, 0.7],
[-7.2, -9.1, 0.82],
[-13.2, -8.5, 0.85],
[2.9, -9.1, 0.62]
].forEach(([x, z, s]) => addTree(x, z, s));
function addBench(x, z, rotation = 0) {
const bench = groupAt(x, 0, z);
bench.rotation.y = rotation;
box(1.5, 0.18, 0.48, "#9b704d", 0, 0.48, 0, bench);
box(1.5, 0.65, 0.14, "#9b704d", 0, 0.78, -0.2, bench);
box(0.16, 0.45, 0.42, "#4b5d50", -0.52, 0.23, 0, bench);
box(0.16, 0.45, 0.42, "#4b5d50", 0.52, 0.23, 0, bench);
}
addBench(-1.8, 1.05, 0);
addBench(1.9, -1.95, Math.PI);
// Voxel fountain / common marker.
const fountain = groupAt(0, 0, -0.55);
box(2.2, 0.25, 2.2, "#a7a58f", 0, 0.23, 0, fountain);
box(1.5, 0.35, 1.5, "#bfc4af", 0, 0.48, 0, fountain);
box(0.95, 0.14, 0.95, "#76b9b2", 0, 0.71, 0, fountain);
box(0.38, 0.85, 0.38, "#c3c7b4", 0, 0.95, 0, fountain);
box(0.8, 0.18, 0.8, "#d8d8c1", 0, 1.42, 0, fountain);
// -------------------------------------------------------------------------
// Buildings
// -------------------------------------------------------------------------
const buildings = [
{
kind: "home",
name: "Maple House",
type: "Home",
x: -10,
z: 5,
w: 3.2,
d: 2.6,
color: "#bd654b",
description:
"Households provide labor, create demand, and use the products made nearby."
},
{
kind: "home",
name: "Sunroom House",
type: "Home",
x: -5.2,
z: 7,
w: 3.2,
d: 2.5,
color: "#bd654b",
description:
"A second home anchors the beginning of the highlighted product trace."
},
{
kind: "workshop",
name: "Nimble Workshop",
type: "Workshop",
x: 0,
z: 5,
w: 4.1,
d: 3,
color: "#526c68",
description:
"Local skills and recovered materials become useful products here."
},
{
kind: "workshop",
name: "Repair Works",
type: "Workshop",
x: 6.2,
z: 6,
w: 3.7,
d: 2.8,
color: "#526c68",
description:
"Repair, assembly, and maintenance keep materials circulating longer."
},
{
kind: "shop",
name: "Corner Store",
type: "Shop",
x: 8,
z: 1,
w: 3.5,
d: 2.5,
color: "#357f7d",
description:
"Nearby goods are stocked and sold to households and workshops."
},
{
kind: "shop",
name: "Depot Goods",
type: "Shop",
x: 6,
z: -4,
w: 3.5,
d: 2.5,
color: "#357f7d",
description:
"A neighborhood shop connects production with everyday local demand."
},
{
kind: "collection",
name: "Intake Bay",
type: "Collection",
x: -10,
z: -1,
w: 3.2,
d: 2.5,
color: "#577a4f",
description:
"Materials arrive, are sorted, and are routed to local production."
},
{
kind: "collection",
name: "Reuse Depot",
type: "Collection",
x: -10,
z: -6,
w: 3.2,
d: 2.5,
color: "#577a4f",
description:
"Used products and workshop offcuts return here for sorting and reuse."
}
];
const pickables = [];
function createBuilding(data) {
const group = groupAt(data.x, 0, data.z);
const { w, d } = data;
const wallHeight =
data.kind === "collection" ? 1.75 : data.kind === "shop" ? 2.55 : 2.65;
box(w + 0.35, 0.2, d + 0.35, "#a9aa91", 0, 0.13, 0, group);
if (data.kind === "collection") {
// Open-sided collection shed.
box(w, wallHeight, 0.22, COLORS.collectionWall, 0, wallHeight / 2 + 0.2, -d / 2, group);
box(0.22, wallHeight, d, COLORS.collectionWall, -w / 2, wallHeight / 2 + 0.2, 0, group);
box(0.22, wallHeight, d, COLORS.collectionWall, w / 2, wallHeight / 2 + 0.2, 0, group);
box(w + 0.55, 0.3, d + 0.55, COLORS.collectionRoof, 0, wallHeight + 0.32, 0, group);
box(w * 0.72, 0.18, d * 0.72, "#668a5b", 0, wallHeight + 0.53, 0, group);
const binColors = ["#d69d43", "#5f9b91", "#c96e53"];
binColors.forEach((color, index) => {
const x = (index - 1) * 0.78;
box(0.62, 0.65, 0.62, color, x, 0.54, -0.15, group);
box(0.68, 0.1, 0.68, "#40584c", x, 0.89, -0.15, group);
});
box(0.75, 0.55, 0.5, "#c4b88f", -0.78, 0.48, 0.65, group);
box(0.6, 0.45, 0.55, "#8ca074", 0.8, 0.43, 0.55, group);
} else {
const wallColor =
data.kind === "home"
? COLORS.homeWall
: data.kind === "shop"
? COLORS.shopWall
: COLORS.workshopWall;
box(w, wallHeight, d, wallColor, 0, wallHeight / 2 + 0.2, 0, group);
if (data.kind === "home") {
box(w + 0.45, 0.28, d + 0.45, COLORS.homeRoof, 0, wallHeight + 0.31, 0, group);
box(w * 0.68, 0.24, d * 0.66, "#a95542", 0, wallHeight + 0.55, 0, group);
box(0.42, 1.1, 0.42, "#855e4d", w * 0.26, wallHeight + 0.9, -d * 0.18, group);
} else {
box(w + 0.42, 0.3, d + 0.42, data.kind === "shop" ? COLORS.shopRoof : COLORS.workshopRoof, 0, wallHeight + 0.32, 0, group);
box(w * 0.63, 0.18, d * 0.55, data.kind === "shop" ? "#286c6b" : "#405c58", 0, wallHeight + 0.53, 0, group);
}
// Door and front windows.
box(0.68, 1.45, 0.09, "#7a5c45", 0, 0.93, d / 2 + 0.055, group);
box(0.08, 0.08, 0.08, "#e8c46f", 0.22, 0.88, d / 2 + 0.12, group);
for (const x of [-w * 0.29, w * 0.29]) {
box(0.78, 0.76, 0.08, "#4b6258", x, 1.72, d / 2 + 0.055, group);
box(0.58, 0.56, 0.09, data.kind === "home" ? COLORS.glassWarm : COLORS.glass, x, 1.72, d / 2 + 0.105, group);
box(0.05, 0.58, 0.04, "#e8e2ce", x, 1.72, d / 2 + 0.16, group);
}
if (data.kind === "shop") {
// Segmented awning.
const segments = 7;
const segmentWidth = (w + 0.2) / segments;
for (let i = 0; i < segments; i++) {
const x = -w / 2 - 0.1 + segmentWidth * (i + 0.5);
box(
segmentWidth + 0.015,
0.14,
0.62,
i % 2 === 0 ? "#f4e7c9" : "#d47959",
x,
2.25,
d / 2 + 0.3,
group
);
}
box(1.65, 0.4, 0.12, "#356b62", 0, 2.78, d / 2 + 0.08, group);
box(1.12, 0.08, 0.13, "#f1e6c9", 0, 2.78, d / 2 + 0.16, group);
}
if (data.kind === "workshop") {
box(1.35, 1.15, 0.1, "#55645c", 0, 1.35, d / 2 + 0.06, group);
for (let i = 0; i < 5; i++) {
box(1.18, 0.055, 0.12, "#9aa69b", 0, 0.92 + i * 0.2, d / 2 + 0.13, group);
}
box(0.42, 1.15, 0.42, "#6f7770", -w * 0.29, wallHeight + 0.95, -d * 0.2, group);
box(0.58, 0.16, 0.58, "#505c56", -w * 0.29, wallHeight + 1.58, -d * 0.2, group);
}
}
group.traverse((child) => {
if (child.isMesh) {
child.userData.info = data;
pickables.push(child);
}
});
data.group = group;
data.height = wallHeight + 1;
scene.updateMatrixWorld(true);
const anchor = group.localToWorld(new THREE.Vector3(0, data.height + 0.35, 0));
createBuildingLabel(data, anchor);
}
buildings.forEach(createBuilding);
// -------------------------------------------------------------------------
// Flow geometry
// -------------------------------------------------------------------------
function createPolyline(points, closed = false, y = 0.2) {
const vertices = points.map(
([x, z]) => new THREE.Vector3(x, y, z)
);
if (closed && !vertices[0].equals(vertices[vertices.length - 1])) {
vertices.push(vertices[0].clone());
}
const segments = [];
const cumulative = [0];
let total = 0;
for (let i = 0; i < vertices.length - 1; i++) {
const a = vertices[i];
const b = vertices[i + 1];
const length = a.distanceTo(b);
segments.push({ a, b, length, start: total });
total += length;
cumulative.push(total);
}
function sample(distance, position = new THREE.Vector3(), tangent = new THREE.Vector3()) {
const d = Math.max(0, Math.min(total, distance));
let segment = segments[segments.length - 1];
for (const candidate of segments) {
if (d <= candidate.start + candidate.length) {
segment = candidate;
break;
}
}
const t = segment.length === 0
? 0
: (d - segment.start) / segment.length;
position.lerpVectors(segment.a, segment.b, t);
tangent.subVectors(segment.b, segment.a).normalize();
return { position, tangent };
}
return {
vertices,
segments,
cumulative,
total,
sample
};
}
function addChevron(parent, x, z, yaw, color, y = 0.25) {
const arrow = new THREE.Group();
arrow.position.set(x, y, z);
arrow.rotation.y = yaw;
const barGeometry = new THREE.BoxGeometry(0.46, 0.1, 0.12);
const barMaterial = material(color);
const left = new THREE.Mesh(barGeometry, barMaterial);
left.position.set(-0.08, 0, 0.12);
left.rotation.y = Math.PI / 4;
const right = new THREE.Mesh(barGeometry, barMaterial);
right.position.set(-0.08, 0, -0.12);
right.rotation.y = -Math.PI / 4;
arrow.add(left, right);
parent.add(arrow);
}
function createRoute({
points,
closed,
color,
layerGroup,
y = 0.22,
dash = 0.52,
gap = 0.36,
width = 0.13,
arrows = true,
tokenStyle = "box",
tokenCount = 3,
speed = 1.8,
phase = 0
}) {
const path = createPolyline(points, closed, y);
const tokens = [];
for (const segment of path.segments) {
const dx = segment.b.x - segment.a.x;
const dz = segment.b.z - segment.a.z;
const length = segment.length;
const yaw = -Math.atan2(dz, dx);
const spacing = dash + gap;
const count = Math.max(1, Math.floor(length / spacing));
for (let i = 0; i < count; i++) {
const distance = i * spacing + dash * 0.5;
if (distance > length) continue;
const t = distance / length;
const dashMesh = box(
Math.min(dash, length),
0.095,
width,
color,
THREE.MathUtils.lerp(segment.a.x, segment.b.x, t),
y,
THREE.MathUtils.lerp(segment.a.z, segment.b.z, t),
layerGroup
);
dashMesh.rotation.y = yaw;
}
if (arrows && length > 1.2) {
const arrowDistance = Math.min(length * 0.55, 1.4);
const t = arrowDistance / length;
addChevron(
layerGroup,
THREE.MathUtils.lerp(segment.a.x, segment.b.x, t),
THREE.MathUtils.lerp(segment.a.z, segment.b.z, t),
yaw,
color,
y + 0.04
);
}
}
for (let i = 0; i < tokenCount; i++) {
const token = new THREE.Group();
if (tokenStyle === "worker") {
box(0.22, 0.34, 0.18, color, 0, 0.22, 0, token);
box(0.2, 0.2, 0.2, "#f0d3a7", 0, 0.51, 0, token);
} else if (tokenStyle === "coin") {
box(0.25, 0.1, 0.25, color, 0, 0.08, 0, token);
box(0.12, 0.04, 0.12, "#ffd6a4", 0, 0.15, 0, token);
} else {
box(0.3, 0.3, 0.3, color, 0, 0.18, 0, token);
box(0.32, 0.08, 0.32, "#f5e8c9", 0, 0.18, 0, token);
}
layerGroup.add(token);
tokens.push({
object: token,
offset: (i / tokenCount) * path.total,
speed: speed * (0.88 + i * 0.07)
});
}
return { path, tokens };
}
const layerGroups = {
work: new THREE.Group(),
goods: new THREE.Group(),
resources: new THREE.Group(),
payments: new THREE.Group(),
trace: new THREE.Group()
};
Object.values(layerGroups).forEach((group) => scene.add(group));
const animatedRoutes = [];
// Work: a broad dashed circuit through homes, workshops, shops, and collection.
animatedRoutes.push(
createRoute({
points: [
[-10.5, 6.7], [-5.8, 8.7], [0.5, 8.4], [5.4, 7.8],
[9.4, 4.1], [10.6, -0.5], [8.1, -5.3], [3.2, -7.8],
[-4, -8.4], [-11, -7.8], [-13, -3], [-13, 2.3], [-11, 4.7]
],
closed: true,
color: COLORS.work,
layerGroup: layerGroups.work,
y: 0.21,
dash: 0.42,
gap: 0.43,
width: 0.13,
tokenStyle: "worker",
tokenCount: 5,
speed: 1.05,
phase: 4
})
);
// Goods: workshop output moves through shops, homes, and collection.
animatedRoutes.push(
createRoute({
points: [
[1.7, 3.15], [4.4, 2.9], [6.6, 1], [6.1, -2.4],
[4.7, -4.5], [1.3, -4.5], [1.3, -7.4], [-6.5, -7.4],
[-7.5, -4.8], [-12, -3.4], [-12, 3.8], [-8, 4],
[-8, 7.8], [-2.5, 7.8], [0, 6.7]
],
closed: true,
color: COLORS.goods,
layerGroup: layerGroups.goods,
y: 0.24,
dash: 0.62,
gap: 0.22,
width: 0.16,
tokenStyle: "box",
tokenCount: 5,
speed: 1.65,
phase: 7
})
);
// Resources: recovered material moves to workshops and offcuts return.
animatedRoutes.push(
createRoute({
points: [
[-8.2, -1.2], [-5.8, -2.2], [-4.3, 1.4], [-1.4, 3.1],
[2.6, 3.4], [6.3, 4.6], [9.7, 3.2], [10.6, -2],
[8.4, -6.6], [3, -7.7], [-6.4, -6.9], [-8.2, -5.6],
[-11.2, -3.8], [-11.3, -1.1]
],
closed: true,
color: COLORS.resources,
layerGroup: layerGroups.resources,
y: 0.26,
dash: 0.35,
gap: 0.33,
width: 0.14,
tokenStyle: "box",
tokenCount: 5,
speed: 1.35,
phase: 12
})
);
// Payments deliberately use a separate, lower dashed rail.
const paymentPaths = [
[
[-4.1, 5.5], [-3.2, 1.5], [-2.4, -2.4],
[3.8, -2.4], [5, -4.5]
],
[
[-8.3, 4.6], [-7.3, 1.8], [-4.4, 0.8],
[6.7, 0.8], [7.3, 1.4]
],
[
[4.7, -4.8], [3, -4.8], [2.6, -1.8],
[2.6, 2.8], [1.4, 3.4]
],
[
[7.4, 6.6], [9.1, 5.5], [9.1, 3.1], [8.8, 2.7]
]
];
paymentPaths.forEach((points, index) => {
animatedRoutes.push(
createRoute({
points,
closed: false,
color: COLORS.payments,
layerGroup: layerGroups.payments,
y: 0.17,
dash: 0.25,
gap: 0.28,
width: 0.11,
arrows: true,
tokenStyle: "coin",
tokenCount: 2,
speed: 1.25 + index * 0.1,
phase: index * 3.2
})
);
});
createFlowLabel("WORK", COLORS.work, -7.7, 0.7, 8.7, "work");
createFlowLabel("GOODS", COLORS.goods, 4.2, 0.7, -7.35, "goods");
createFlowLabel("RESOURCES", COLORS.resources, -10.3, 0.7, -4.1, "resources");
createFlowLabel("PAYMENTS · SEPARATE", COLORS.payments, 0.1, 0.7, -2.35, "payments");
// -------------------------------------------------------------------------
// One highlighted product: material arrival → make → buy → use → discard
// -------------------------------------------------------------------------
const tracePoints = [
[-8.2, -1.2], // 0: material arrival / Intake Bay
[-5.7, -1.2],
[-5.7, 2.7],
[-2.7, 2.7],
[-1.0, 3.35], // 4: workshop
[2.9, 3.35],
[4.8, 2.35],
[7.0, 2.6], // 7: shop
[4.7, 4.6],
[3.8, 8.8],
[-3.6, 8.8],
[-4.2, 6.0], // 11: home / use
[-3.4, 4.7],
[-3.4, -4.0],
[-6.8, -4.0],
[-6.8, -6.8],
[-8.2, -6.8] // 16: discard / Reuse Depot
];
const tracePath = createPolyline(tracePoints, false, 0.36);
for (const segment of tracePath.segments) {
const dx = segment.b.x - segment.a.x;
const dz = segment.b.z - segment.a.z;
const yaw = -Math.atan2(dz, dx);
const length = segment.length;
const mesh = box(
length,
0.12,
0.18,
COLORS.trace,
(segment.a.x + segment.b.x) / 2,
0.36,
(segment.a.z + segment.b.z) / 2,
layerGroups.trace
);
mesh.rotation.y = yaw;
if (length > 1.6) {
addChevron(
layerGroups.trace,
THREE.MathUtils.lerp(segment.a.x, segment.b.x, 0.55),
THREE.MathUtils.lerp(segment.a.z, segment.b.z, 0.55),
yaw,
COLORS.trace,
0.43
);
}
}
function addStageMarker(index, x, z) {
const marker = new THREE.Group();
marker.position.set(x, 0.41, z);
box(0.92, 0.12, 0.12, COLORS.trace, 0, 0, 0.39, marker);
box(0.92, 0.12, 0.12, COLORS.trace, 0, 0, -0.39, marker);
box(0.12, 0.12, 0.66, COLORS.trace, 0.39, 0, 0, marker);
box(0.12, 0.12, 0.66, COLORS.trace, -0.39, 0, 0, marker);
layerGroups.trace.add(marker);
createWorldLabel({
html: `<b>${index + 1}</b><span>${[
"ARRIVAL",
"MAKE",
"BUY",
"USE",
"DISCARD"
][index]}</span>`,
position: new THREE.Vector3(x, 1.05, z),
className: "stage-tag",
color: COLORS.trace,
layer: "trace"
});
}
const stagePointIndices = [0, 4, 7, 11, 16];
const stageCoordinates = [
[-8.2, -1.2],
[-1.0, 3.35],
[7.0, 2.6],
[-4.2, 6.0],
[-8.2, -6.8]
];
stageCoordinates.forEach(([x, z], index) => addStageMarker(index, x, z));
// The product itself is a terracotta mug, not a payment token.
const product = new THREE.Group();
box(0.48, 0.42, 0.48, "#d97859", 0, 0, 0, product);
box(0.4, 0.045, 0.4, "#f1c18b", 0, 0.235, 0, product);
box(0.17, 0.27, 0.15, "#d97859", 0.31, 0, 0, product);
box(0.12, 0.11, 0.12, "#f1c18b", 0, 0.28, 0, product);
layerGroups.trace.add(product);
// A small square spotlight beneath the mug keeps it easy to find.
const productGlow = new THREE.Group();
box(0.78, 0.045, 0.12, COLORS.trace, 0, 0, 0.31, productGlow);
box(0.78, 0.045, 0.12, COLORS.trace, 0, 0, -0.31, productGlow);
box(0.12, 0.045, 0.5, COLORS.trace, 0.31, 0, 0, productGlow);
box(0.12, 0.045, 0.5, COLORS.trace, -0.31, 0, 0, productGlow);
layerGroups.trace.add(productGlow);
const stageDistances = stagePointIndices.map(
(pointIndex) => tracePath.cumulative[pointIndex]
);
const stageCopy = [
{
title: "01 — Material arrival",
description: "Collected material is weighed and prepared at the intake bay."
},
{
title: "02 — Made locally",
description: "Nimble Workshop forms and fires the reclaimed-clay mug."
},
{
title: "03 — Bought from a neighbor",
description: "Corner Store stocks the mug for nearby households."
},
{
title: "04 — Put to use",
description: "Sunroom House uses the mug for its everyday morning routine."
},
{
title: "05 — Discarded for reuse",
description: "The used mug returns to the depot for sorting and a new cycle."
}
];
const legDurations = [4.8, 4.2, 4.8, 4.6, 4.4];
const traceSchedule = [{ type: "hold", stage: 0, duration: 0.8 }];
let cycleDuration = 0.8;
for (let i = 0; i < stagePointIndices.length - 1; i++) {
traceSchedule.push({
type: "travel",
from: i,
to: i + 1,
duration: legDurations[i]
});
traceSchedule.push({
type: "hold",
stage: i + 1,
duration: i === stagePointIndices.length - 2 ? 2.4 : 1.1
});
cycleDuration += legDurations[i] + (i === stagePointIndices.length - 2 ? 2.4 : 1.1);
}
const traceStageElement = document.querySelector("#trace-stage");
const traceDescriptionElement = document.querySelector("#trace-description");
const traceProgressElement = document.querySelector("#trace-progress span");
const stageElements = [...document.querySelectorAll(".stage-tag")];
let currentTraceStage = -1;
let traceStartTime = performance.now();
function setTraceStage(index) {
if (index === currentTraceStage) return;
currentTraceStage = index;
traceStageElement.textContent = stageCopy[index].title;
traceDescriptionElement.textContent = stageCopy[index].description;
stageElements.forEach((element, stageIndex) => {
element.classList.toggle("active", stageIndex === index);
});
}
document.querySelector("#replay").addEventListener("click", () => {
traceStartTime = performance.now();
});
// -------------------------------------------------------------------------
// Legend toggles
// -------------------------------------------------------------------------
document.querySelectorAll(".legend-row").forEach((button) => {
button.addEventListener("click", () => {
const layerName = button.dataset.layer;
const visible = !layerGroups[layerName].visible;
layerGroups[layerName].visible = visible;
button.classList.toggle("off", !visible);
button.setAttribute("aria-pressed", String(visible));
});
});
// -------------------------------------------------------------------------
// Hovering and selecting buildings
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let hoveredInfo = null;
let selectedInfo = null;
let selectionOutline = null;
const inspectType = document.querySelector("#inspect-type");
const inspectTitle = document.querySelector("#inspect-title");
const inspectCopy = document.querySelector("#inspect-copy");
function setInspect(info) {
if (!info) {
inspectType.textContent = "Map note";
inspectTitle.textContent = "A circular neighborhood";
inspectCopy.textContent =
"Hover over a building to inspect its role. The bright green route follows one product and never represents payment.";
return;
}
inspectType.textContent = info.type;
inspectTitle.textContent = info.name;
inspectCopy.textContent = info.description;
}
function setHoveredInfo(info) {
if (hoveredInfo === info) return;
hoveredInfo = info;
setInspect(info || selectedInfo);
if (selectionOutline) {
scene.remove(selectionOutline);
selectionOutline.geometry.dispose();
selectionOutline.material.dispose();
selectionOutline = null;
}
if (info) {
scene.updateMatrixWorld(true);
const bounds = new THREE.Box3().setFromObject(info.group);
const size = bounds.getSize(new THREE.Vector3());
const center = bounds.getCenter(new THREE.Vector3());
const geometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(size.x + 0.18, size.y + 0.12, size.z + 0.18)
);
const outlineMaterial = new THREE.LineBasicMaterial({
color: "#fffaf0",
transparent: true,
opacity: 0.95
});
selectionOutline = new THREE.LineSegments(geometry, outlineMaterial);
selectionOutline.position.copy(center);
scene.add(selectionOutline);
}
}
function updatePointer(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 pickBuilding() {
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickables, false);
return hits.length ? hits[0].object.userData.info : null;
}
// -------------------------------------------------------------------------
// Custom camera controls
// -------------------------------------------------------------------------
let dragging = false;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
updatePointer(event);
if (dragging) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
if (Math.abs(dx) + Math.abs(dy) > 2) pointerMoved = true;
cameraAzimuth -= dx * 0.006;
cameraElevation = THREE.MathUtils.clamp(
cameraElevation + dy * 0.0045,
0.38,
1.35
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
} else {
setHoveredInfo(pickBuilding());
}
});
canvas.addEventListener("pointerup", (event) => {
if (dragging && !pointerMoved) {
updatePointer(event);
selectedInfo = pickBuilding();
setInspect(selectedInfo || hoveredInfo);
}
dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointerleave", () => {
if (!dragging) setHoveredInfo(null);
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.9
);
updateCamera();
},
{ passive: false }
);
document.querySelector("#zoom-in").addEventListener("click", () => {
cameraZoom = THREE.MathUtils.clamp(cameraZoom * 1.12, 0.65, 1.9);
updateCamera();
});
document.querySelector("#zoom-out").addEventListener("click", () => {
cameraZoom = THREE.MathUtils.clamp(cameraZoom / 1.12, 0.65, 1.9);
updateCamera();
});
document.querySelector("#view-left").addEventListener("click", () => {
cameraAzimuth -= Math.PI / 8;
updateCamera();
});
document.querySelector("#view-right").addEventListener("click", () => {
cameraAzimuth += Math.PI / 8;
updateCamera();
});
document.querySelector("#reset").addEventListener("click", () => {
cameraAzimuth = 0.63;
cameraElevation = 0.83;
cameraZoom = 1;
cameraTarget.set(0, 0, 0);
updateCamera();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const tempPosition = new THREE.Vector3();
const tempTangent = new THREE.Vector3();
function animate() {
const elapsed = clock.getElapsedTime();
// Animate route markers.
for (const route of animatedRoutes) {
for (const token of route.tokens) {
let distance = (elapsed * token.speed + token.offset) % route.path.total;
if (distance < 0) distance += route.path.total;
route.path.sample(distance, tempPosition, tempTangent);
token.object.position.set(tempPosition.x, tempPosition.y + 0.08, tempPosition.z);
if (Math.abs(tempTangent.x) + Math.abs(tempTangent.z) > 0.001) {
token.object.rotation.y = -Math.atan2(tempTangent.z, tempTangent.x);
}
}
}
// Animate the traced product separately from every payment token.
const traceElapsed = ((performance.now() - traceStartTime) / 1000) % cycleDuration;
let remaining = traceElapsed;
let productDistance = stageDistances[0];
let traceStage = 0;
for (const entry of traceSchedule) {
if (remaining <= entry.duration) {
if (entry.type === "hold") {
traceStage = entry.stage;
productDistance = stageDistances[entry.stage];
} else {
const t = THREE.MathUtils.clamp(remaining / entry.duration, 0, 1);
const eased = t * t * (3 - 2 * t);
productDistance = THREE.MathUtils.lerp(
stageDistances[entry.from],
stageDistances[entry.to],
eased
);
traceStage = t < 0.5 ? entry.from : entry.to;
}
break;
}
remaining -= entry.duration;
}
setTraceStage(traceStage);
tracePath.sample(productDistance, tempPosition, tempTangent);
product.position.set(
tempPosition.x,
tempPosition.y + 0.42 + Math.sin(elapsed * 5.5) * 0.035,
tempPosition.z
);
productGlow.position.set(
tempPosition.x,
0.43,
tempPosition.z
);
if (Math.abs(tempTangent.x) + Math.abs(tempTangent.z) > 0.001) {
product.rotation.y = -Math.atan2(tempTangent.z, tempTangent.x);
}
traceProgressElement.style.transform = `scaleX(${traceElapsed / cycleDuration})`;
// Project HTML labels into screen space.
for (const label of worldLabels) {
if (label.layer && !layerGroups[label.layer].visible) {
label.element.style.display = "none";
continue;
}
const projected = label.position.clone().project(camera);
const visible = projected.z >= -1 && projected.z <= 1;
if (!visible) {
label.element.style.display = "none";
continue;
}
label.element.style.display = "flex";
label.element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
renderer.render(scene, camera);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
scene.updateMatrixWorld(true);
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```