Prompt specification
Build or customize the Three.js 3D scene "Compost Batch Provenance and Sorting Garden" 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: Compost Batch Provenance and Sorting Garden
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compost-batch-provenance-and-sorting-garden-x3927
Scene Description Prompt:
Compost batch provenance and sorting garden. Build an original garden station tracking three labeled material batches from collection through sorting and processing. Preserve batch identity, remove a known unsuitable item, and display synthetic age records separately. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (compost-batch-provenance-and-sorting-garden-x3927):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Provenance Garden — Compost Batch Station</title>
<style>
:root {
--ink: #193a31;
--ink-soft: #486359;
--paper: #f7f4e9;
--paper-deep: #eee9da;
--line: #d8d1bd;
--leaf: #9fbe4f;
--amber: #df9552;
--plum: #9a718f;
--danger: #c64d3f;
--gold: #efd66a;
--shadow: 0 14px 35px rgba(39, 55, 43, 0.12);
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
.topbar {
height: 72px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 24px;
background: #fbf9f0;
border-bottom: 1px solid var(--line);
position: relative;
z-index: 20;
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.voxel-mark {
width: 42px;
height: 42px;
position: relative;
transform: rotate(-2deg);
}
.voxel-mark i {
position: absolute;
width: 16px;
height: 16px;
display: block;
background: var(--leaf);
border: 2px solid rgba(25, 58, 49, 0.28);
box-shadow: inset -3px -3px 0 rgba(25, 58, 49, 0.1);
}
.voxel-mark i:nth-child(1) { left: 1px; top: 2px; }
.voxel-mark i:nth-child(2) { left: 19px; top: 2px; background: #6f9b4b; }
.voxel-mark i:nth-child(3) { left: 10px; top: 20px; background: #c0d467; }
.brand-copy { min-width: 0; }
.eyebrow {
margin: 0 0 3px;
color: #728077;
font-size: 10px;
font-weight:800;
letter-spacing: .16em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: 19px;
line-height: 1.1;
letter-spacing: -.035em;
white-space: nowrap;
}
.top-status {
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
justify-content: flex-end;
}
.chip {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 30px;
padding: 6px 10px;
border: 1px solid var(--line);
border-radius: 7px;
background: #fffdf5;
color: var(--ink-soft);
font-size: 10px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.chip::before {
content: "";
width: 7px;
height: 7px;
background: var(--leaf);
box-shadow: 0 0 0 3px rgba(159, 190, 79, .18);
}
.chip.alert::before {
background: var(--danger);
box-shadow: 0 0 0 3px rgba(198, 77, 63, .14);
}
.workspace {
height: calc(100vh - 72px);
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
grid-template-rows: minmax(0, 1fr) 184px;
}
.scene-shell {
grid-column: 1;
grid-row: 1;
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #dce3d2;
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active { cursor: grabbing; }
.scene-top {
position: absolute;
z-index: 5;
top: 18px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.scene-heading {
padding: 12px 14px;
border: 1px solid rgba(255, 255, 255, .54);
border-radius: 10px;
background: rgba(250, 248, 237, .82);
box-shadow: 0 8px 28px rgba(31, 57, 44, .08);
backdrop-filter: blur(8px);
}
.scene-heading strong {
display: block;
font-size: 13px;
letter-spacing: -.01em;
}
.scene-heading span {
display: block;
margin-top: 4px;
color: #637268;
font-size: 11px;
}
.view-controls {
display: flex;
gap: 6px;
padding: 5px;
border: 1px solid rgba(255, 255, 255, .58);
border-radius: 10px;
background: rgba(250, 248, 237, .86);
box-shadow: 0 8px 28px rgba(31, 57, 44, .08);
backdrop-filter: blur(8px);
pointer-events: auto;
}
.view-controls button,
.reset-button {
border: 0;
border-radius: 6px;
padding: 8px 10px;
background: transparent;
color: var(--ink-soft);
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: .06em;
text-transform: uppercase;
}
.view-controls button:hover,
.reset-button:hover {
background: #e6ead7;
color: var(--ink);
}
.scene-bottom {
position: absolute;
z-index: 4;
left: 18px;
bottom: 16px;
display: flex;
align-items: center;
gap: 8px;
flex-wrap: wrap;
pointer-events: none;
}
.scene-hint,
.scene-legend {
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, .55);
border-radius: 8px;
background: rgba(250, 248, 237, .78);
color: #53675c;
font-size: 10px;
font-weight: 700;
backdrop-filter: blur(7px);
}
.scene-legend {
display: flex;
align-items: center;
gap: 10px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend-dot {
width: 8px;
height: 8px;
border-radius: 2px;
}
.inspector {
grid-column: 2;
grid-row: 1 / span 2;
min-height: 0;
overflow: auto;
background: #fcfaf2;
border-left: 1px solid var(--line);
scrollbar-width: thin;
scrollbar-color: #c9c2af transparent;
}
.panel-heading {
padding: 20px 20px 15px;
border-bottom: 1px solid var(--line);
}
.panel-heading-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.panel-heading h2 {
margin: 0;
font-size: 16px;
letter-spacing: -.025em;
}
.count-badge {
min-width: 30px;
height: 24px;
display: grid;
place-items: center;
border-radius: 6px;
background: var(--ink);
color: #fffdf4;
font-size: 11px;
font-weight: 800;
}
.panel-heading p {
margin: 7px 0 0;
color: #758078;
font-size: 11px;
line-height: 1.45;
}
.batch-list {
display: grid;
gap: 7px;
padding: 12px;
border-bottom: 1px solid var(--line);
}
.batch-card {
width: 100%;
display: grid;
grid-template-columns: 12px minmax(0, 1fr) auto;
align-items: center;
gap: 10px;
padding: 11px;
border: 1px solid #ded8c8;
border-radius: 9px;
background: #fffdf6;
color: var(--ink);
text-align: left;
cursor: pointer;
transition: border-color .16s, transform .16s, background .16s;
}
.batch-card:hover {
transform: translateX(-2px);
border-color: #aeb8a6;
}
.batch-card.active {
border-color: var(--ink);
background: #f1f3e5;
box-shadow: inset 3px 0 0 var(--batch-color);
}
.batch-swatch {
width: 11px;
height: 28px;
border-radius: 3px;
background: var(--batch-color);
}
.batch-card strong {
display: block;
font-size: 12px;
}
.batch-card small {
display: block;
margin-top: 3px;
color: #748078;
font-size: 10px;
}
.batch-card .stage {
padding: 4px 6px;
border-radius: 5px;
background: #e9e7dc;
color: #627067;
font-size: 8px;
font-weight: 900;
letter-spacing: .07em;
text-transform: uppercase;
}
.batch-card .stage.alert {
background: #f5ded7;
color: #a53b31;
}
.detail {
padding: 18px 20px 24px;
}
.detail-title-row {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.batch-id {
display: inline-flex;
align-items: center;
min-height: 24px;
padding: 4px 8px;
border-radius: 5px;
background: var(--batch-color);
color: #fff;
font-size: 10px;
font-weight: 900;
letter-spacing: .08em;
}
.detail h3 {
margin: 9px 0 4px;
font-size: 22px;
letter-spacing: -.045em;
}
.detail-subtitle {
margin: 0;
color: #748078;
font-size: 11px;
line-height: 1.5;
}
.icon-button {
width: 32px;
height: 32px;
border: 1px solid var(--line);
border-radius: 7px;
background: #fffdf6;
color: var(--ink-soft);
cursor: pointer;
font-size: 15px;
}
.icon-button:hover { background: #e9eadf; }
.facts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 17px 0;
}
.fact {
min-height: 60px;
padding: 10px;
border: 1px solid #dfd9c9;
border-radius: 8px;
background: #f8f5ea;
}
.fact span {
display: block;
color: #849087;
font-size: 8px;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
}
.fact strong {
display: block;
margin-top: 6px;
font-size: 12px;
line-height: 1.25;
}
.progress {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 4px;
margin-bottom: 8px;
}
.progress-step {
position: relative;
padding-top: 18px;
color: #8d958e;
font-size: 8px;
font-weight: 800;
letter-spacing: .05em;
text-transform: uppercase;
text-align: center;
}
.progress-step::before {
content: "";
position: absolute;
top: 2px;
left: 0;
right: 0;
height: 7px;
border-radius: 2px;
background: #dddccf;
box-shadow: inset 0 -2px 0 rgba(25, 58, 49, .08);
}
.progress-step.done { color: var(--ink); }
.progress-step.done::before { background: var(--leaf); }
.progress-step.current::before { background: var(--gold); }
.action-stack {
display: grid;
gap: 8px;
margin: 17px 0 20px;
}
.primary-action,
.reject-action {
min-height: 42px;
border-radius: 8px;
cursor: pointer;
font-size: 11px;
font-weight: 900;
letter-spacing: .055em;
text-transform: uppercase;
}
.primary-action {
border: 1px solid var(--ink);
background: var(--ink);
color: #fffdf4;
box-shadow: 0 5px 0 #0f2a23;
}
.primary-action:hover:not(:disabled) { transform: translateY(-1px); }
.primary-action:disabled {
cursor: not-allowed;
border-color: #c9c9bd;
background: #d9d9cf;
color: #838a82;
box-shadow: none;
}
.reject-action {
border: 1px solid #cf7568;
background: #fff6f0;
color: #a33d32;
}
.reject-action:hover { background: #f7ded6; }
.action-note {
color: #8b918a;
font-size: 9px;
line-height: 1.5;
text-align: center;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin: 20px 0 11px;
color: #596c61;
font-size: 9px;
font-weight: 900;
letter-spacing: .13em;
text-transform: uppercase;
}
.section-label::after {
content: "";
flex: 1;
height: 1px;
background: var(--line);
}
.timeline {
position: relative;
display: grid;
gap: 14px;
padding-left: 18px;
}
.timeline::before {
content: "";
position: absolute;
left: 5px;
top: 6px;
bottom: 6px;
width: 1px;
background: #cfd3c5;
}
.event {
position: relative;
}
.event::before {
content: "";
position: absolute;
left: -17px;
top: 4px;
width: 7px;
height: 7px;
border: 2px solid #fcfaf2;
border-radius: 2px;
background: var(--leaf);
box-shadow: 0 0 0 1px #91a279;
}
.event.alert::before { background: var(--danger); box-shadow: 0 0 0 1px #cf786c; }
.event time {
display: block;
color: #929b93;
font-size: 8px;
font-weight: 800;
letter-spacing: .08em;
}
.event strong {
display: block;
margin-top: 3px;
font-size: 11px;
}
.event p {
margin: 3px 0 0;
color: #768078;
font-size: 9px;
line-height: 1.45;
}
.age-panel {
grid-column: 1;
grid-row: 2;
min-width: 0;
overflow: hidden;
background: #f8f5e9;
border-top: 1px solid var(--line);
}
.age-header {
height: 48px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 0 16px;
border-bottom: 1px solid var(--line);
}
.age-title {
display: flex;
align-items: center;
gap: 10px;
min-width: 0;
}
.age-title strong {
font-size: 11px;
letter-spacing: .025em;
white-space: nowrap;
}
.age-title span {
color: #7c877e;
font-size: 9px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.synthetic-tag {
flex: none;
padding: 5px 8px;
border: 1px solid #c8ae58;
border-radius: 5px;
background: #f2e8b9;
color: #6c5a20;
font-size: 8px;
font-weight: 900;
letter-spacing: .11em;
text-transform: uppercase;
}
.age-content {
height: calc(100% - 48px);
display: grid;
grid-template-columns: 1fr 160px;
overflow: auto;
}
.age-table-wrap { overflow: auto; }
table {
width: 100%;
border-collapse: collapse;
font-size: 10px;
}
th {
position: sticky;
top: 0;
padding: 9px 14px;
background: #efecdf;
color: #7c857d;
font-size: 8px;
letter-spacing: .1em;
text-align: left;
text-transform: uppercase;
}
td {
padding: 8px 14px;
border-bottom: 1px solid #e4dfd0;
color: #526359;
white-space: nowrap;
}
td:first-child {
color: var(--ink);
font-weight: 800;
}
.age-cell {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--ink);
font-weight: 800;
}
.age-cell::before {
content: "";
width: 32px;
height: 5px;
border-radius: 2px;
background: linear-gradient(90deg, var(--leaf) var(--age-fill), #dedbcf var(--age-fill));
}
.age-side {
padding: 12px 15px;
border-left: 1px solid var(--line);
background: #f2efe3;
}
.age-side strong {
display: block;
font-size: 9px;
letter-spacing: .09em;
text-transform: uppercase;
}
.age-side p {
margin: 6px 0 10px;
color: #7a857d;
font-size: 9px;
line-height: 1.5;
}
.age-side button {
width: 100%;
min-height: 32px;
border: 1px solid #bfc6b8;
border-radius: 6px;
background: #fffdf6;
color: var(--ink);
cursor: pointer;
font-size: 9px;
font-weight: 900;
letter-spacing: .06em;
text-transform: uppercase;
}
.age-side button:hover { background: #e9ecdf; }
.toast {
position: fixed;
z-index: 100;
left: 50%;
bottom: 24px;
max-width: min(520px, calc(100vw - 32px));
padding: 11px 15px;
border-radius: 8px;
background: #173a31;
color: #fffdf4;
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition: opacity .2s, transform .2s;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.toast.error { background: #9b3f34; }
@media (max-width: 980px) {
body { overflow: auto; }
.workspace {
height: auto;
min-height: calc(100vh - 72px);
grid-template-columns: 1fr;
grid-template-rows: 58vh auto 220px;
}
.scene-shell { grid-column: 1; grid-row: 1; }
.inspector { grid-column: 1; grid-row: 2; border-left: 0; }
.age-panel { grid-column: 1; grid-row: 3; }
.top-status .chip:first-child { display: none; }
}
@media (max-width: 620px) {
.topbar { padding: 0 14px; }
.brand h1 { font-size: 16px; }
.top-status { display: none; }
.scene-heading { display: none; }
.scene-top { justify-content: flex-end; }
.view-controls button { padding: 8px; }
.scene-legend { display: none; }
.age-content { grid-template-columns: 1fr; }
.age-side { display: none; }
.age-title span { display: none; }
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="voxel-mark" aria-hidden="true"><i></i><i></i><i></i></div>
<div class="brand-copy">
<p class="eyebrow">Material recovery station · demo 05/18</p>
<h1>Provenance Garden</h1>
</div>
</div>
<div class="top-status">
<span class="chip">Three tracked identities</span>
<span class="chip" id="topAlert">One unsuitable item</span>
<button class="reset-button" id="resetButton" type="button">Reset garden</button>
</div>
</header>
<main class="workspace">
<section class="scene-shell" aria-label="Interactive voxel compost garden">
<div id="viewport"></div>
<div class="scene-top">
<div class="scene-heading">
<strong>Voxel Batch Station</strong>
<span id="sceneStatus">2 sorting · 1 collection</span>
</div>
<div class="view-controls" aria-label="Camera views">
<button type="button" data-view="overview">Overview</button>
<button type="button" data-view="collection">Collect</button>
<button type="button" data-view="sorting">Sort</button>
<button type="button" data-view="processing">Process</button>
</div>
</div>
<div class="scene-bottom">
<div class="scene-hint">Drag to orbit · right-drag to pan · wheel to zoom · click a batch</div>
<div class="scene-legend">
<span class="legend-item"><i class="legend-dot" style="background:#719a55"></i>A-17</span>
<span class="legend-item"><i class="legend-dot" style="background:#df9552"></i>B-04</span>
<span class="legend-item"><i class="legend-dot" style="background:#9a718f"></i>C-09</span>
<span class="legend-item"><i class="legend-dot" style="background:#2865d6"></i>reject</span>
</div>
</div>
</section>
<aside class="inspector">
<div class="panel-heading">
<div class="panel-heading-row">
<h2>Batch provenance</h2>
<span class="count-badge">3</span>
</div>
<p>Identity stays attached to every batch as it moves through the station.</p>
</div>
<div class="batch-list" id="batchList"></div>
<div class="detail" id="detail"></div>
</aside>
<section class="age-panel" aria-label="Separate synthetic age records">
<div class="age-header">
<div class="age-title">
<strong>Synthetic age ledger</strong>
<span>Separate fixture records · not provenance timestamps or sensor telemetry</span>
</div>
<span class="synthetic-tag">Synthetic data</span>
</div>
<div class="age-content">
<div class="age-table-wrap">
<table>
<thead>
<tr>
<th>Record</th>
<th>Batch</th>
<th>Synthetic age</th>
<th>Captured</th>
<th>Fixture source</th>
</tr>
</thead>
<tbody id="ageRows"></tbody>
</table>
</div>
<div class="age-side">
<strong>Independent age fixtures</strong>
<p>Age values are generated demonstration records. They never change material counts or provenance events.</p>
<button type="button" id="addAgeButton">Add synthetic sample</button>
</div>
</div>
</section>
</main>
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const batchListElement = document.getElementById("batchList");
const detailElement = document.getElementById("detail");
const ageRowsElement = document.getElementById("ageRows");
const toastElement = document.getElementById("toast");
const sceneStatusElement = document.getElementById("sceneStatus");
const topAlertElement = document.getElementById("topAlert");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce3d2);
scene.fog = new THREE.Fog(0xdce3d2, 34, 68);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 120);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff7dc, 0x536a50, 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d4, 3.2);
sun.position.set(-10, 19, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d8e5, 0.7);
fill.position.set(12, 8, -14);
scene.add(fill);
const materialCache = new Map();
const stationGroup = new THREE.Group();
const batchWorldGroup = new THREE.Group();
const rejectWorldGroup = new THREE.Group();
scene.add(stationGroup, batchWorldGroup, rejectWorldGroup);
const stageOrder = ["collection", "sorting", "processing", "finished"];
const stageNames = {
collection: "Collection",
sorting: "Sorting",
processing: "Processing",
finished: "Finished"
};
const stagePositions = {
collection: { x: -10.2, y: 0.14 },
sorting: { x: -2.6, y: 1.16 },
processing: { x: 5.25, y: 0.2 },
finished: { x: 11.15, y: 0.14 }
};
const laneZ = {
"A-17": -3.45,
"B-04": 0,
"C-09": 3.45
};
const batchColors = {
"A-17": 0x719a55,
"B-04": 0xdf9552,
"C-09": 0x9a718f
};
let batches = {};
let visuals = {};
let selectedId = "B-04";
let rejectClones = [];
let demoMinute = 9 * 60 + 10;
let toastTimer = 0;
let ageRecords = [
{ id: "AGE-001", batch: "A-17", age: 1.2, captured: "08:10", source: "fixture-leaf-v2" },
{ id: "AGE-002", batch: "B-04", age: 0.8, captured: "08:16", source: "fixture-citrus-v1" },
{ id: "AGE-003", batch: "C-09", age: 0.3, captured: "08:27", source: "fixture-filter-v2" }
];
const tweens = [];
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function material(color, options = {}) {
const key = `${color}-${options.emissive || 0}-${options.emissiveIntensity || 0}-${options.transparent || false}-${options.opacity || 1}`;
if (!materialCache.has(key)) {
materialCache.set(key, new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.88,
metalness: options.metalness ?? 0,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1
}));
}
return materialCache.get(key);
}
function box(width, height, depth, color, x, y, z, parent = stationGroup, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeLabel(text, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 224;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.shadowColor = "rgba(20, 42, 32, .22)";
ctx.shadowBlur = 18;
ctx.shadowOffsetY = 8;
roundedRect(ctx, 18, 18, 988, 188, 25);
ctx.fillStyle = options.background || "#183d32";
ctx.fill();
ctx.shadowColor = "transparent";
if (options.accent) {
ctx.fillStyle = options.accent;
roundedRect(ctx, 18, 18, 22, 188, 8);
ctx.fill();
}
ctx.strokeStyle = options.border || "rgba(255,255,255,.32)";
ctx.lineWidth = 5;
roundedRect(ctx, 20, 20, 984, 184, 23);
ctx.stroke();
ctx.fillStyle = options.color || "#fffbed";
ctx.font = `800 ${options.fontSize || 52}px ui-sans-serif, system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
let label = text;
while (ctx.measureText(label).width > 890 && label.length > 8) {
label = label.slice(0, -2);
}
if (label !== text) label += "…";
ctx.fillText(label, 520, 114);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.renderOrder = 20;
return sprite;
}
function addStationLabel(text, x, z, width = 4.1) {
const label = makeLabel(text, {
background: "#244b3e",
color: "#fff9e8",
accent: "#a9c45e",
fontSize: 49
});
label.position.set(x, 3.9, z);
label.scale.set(width, width * 0.219, 1);
stationGroup.add(label);
return label;
}
function buildWorld() {
box(32, 0.55, 16, 0x7d9270, 0, -0.38, 0, stationGroup);
const tileColors = [0xc9d1b8, 0xd4d8c0, 0xbfc9b0, 0xcdd1b9];
let tileIndex = 0;
for (let x = -15; x <= 15; x += 2) {
for (let z = -7; z <= 7; z += 2) {
if (Math.abs(z) === 1 || Math.abs(z) === 3) {
box(1.86, 0.06, 1.86, tileColors[tileIndex++ % tileColors.length], x, -0.06, z, stationGroup);
}
}
}
// Garden border and block-built fence.
for (let x = -15; x <= 15; x += 2.5) {
box(0.22, 1.35, 0.22, 0x8e7855, x, 0.55, -7.15, stationGroup);
}
box(30.5, 0.16, 0.16, 0xb69a68, 0, 0.68, -7.15, stationGroup);
box(30.5, 0.16, 0.16, 0xb69a68, 0, 1.13, -7.15, stationGroup);
// Raised soil beds.
const bedPositions = [
[-12.7, -5.2], [12.7, -5.2], [-12.7, 5.2], [12.7, 5.2]
];
bedPositions.forEach(([x, z], index) => {
box(3.1, 0.38, 2.05, 0x6f533b, x, 0.13, z, stationGroup);
box(2.72, 0.16, 1.7, 0x4c3d2d, x, 0.35, z, stationGroup);
for (let i = 0; i < 7; i++) {
const px = x + (i % 3 - 1) * 0.55;
const pz = z + (Math.floor(i / 3) - 0.5) * 0.58;
box(0.38, 0.36 + (i % 2) * 0.12, 0.38,
index % 2 ? 0x668f4e : 0x83a755, px, 0.6, pz, stationGroup);
}
});
// Four station lanes with distinct surfaces.
Object.entries(laneZ).forEach(([id, z]) => {
const accent = batchColors[id];
// 01 Collection crate.
box(2.85, 0.18, 2.55, 0x9e825d, -10.2, 0.03, z, stationGroup);
box(2.65, 0.48, 0.14, 0xb69a6d, -10.2, 0.35, z - 1.1, stationGroup);
box(2.65, 0.48, 0.14, 0xb69a6d, -10.2, 0.35, z + 1.1, stationGroup);
box(0.14, 0.48, 2.25, 0xa98b60, -11.4, 0.35, z, stationGroup);
box(0.14, 0.48, 2.25, 0xa98b60, -9.0, 0.35, z, stationGroup);
box(0.38, 0.16, 0.38, accent, -11.38, 0.73, z - 1.08, stationGroup);
// 02 Sorting table and gantry.
box(3.05, 0.22, 2.65, 0xd5c8a4, -2.6, 1.05, z, stationGroup);
[-1.25, 1.25].forEach(dz => {
box(0.16, 0.93, 0.16, 0x6e7d67, -3.75, 0.48, z + dz, stationGroup);
box(0.16, 0.93, 0.16, 0x6e7d67, -1.45, 0.48, z + dz, stationGroup);
box(0.14, 1.4, 0.14, 0x42685b, -0.25, 1.82, z + dz, stationGroup);
});
box(0.16, 0.14, 2.65, 0x42685b, -0.25, 2.5, z, stationGroup);
for (let i = -1; i <= 1; i++) {
box(0.12, 0.08, 0.25, 0xb9df69, -0.25, 2.42, z + i * 0.7, stationGroup, {
emissive: 0x88bd42,
emissiveIntensity: 0.9
});
}
for (let i = -3; i <= 3; i++) {
box(0.08, 0.03, 1.75, 0x6d7767, -3.45 + i * 0.56, 1.175, z, stationGroup);
}
box(0.36, 0.18, 0.36, accent, -3.85, 1.26, z - 1.05, stationGroup);
// 03 Open processing chamber.
box(2.9, 0.2, 2.65, 0x53645b, 5.25, 0.05, z, stationGroup);
box(2.9, 0.72, 0.16, 0x728274, 5.25, 0.48, z - 1.18, stationGroup);
box(0.16, 0.55, 2.55, 0x687a6e, 3.83, 0.42, z, stationGroup);
box(0.16, 0.55, 2.55, 0x687a6e, 6.67, 0.42, z, stationGroup);
box(2.9, 0.18, 0.18, accent, 5.25, 0.82, z + 1.18, stationGroup);
box(0.2, 0.18, 0.2, 0xb9df69, 6.7, 0.92, z + 1.17, stationGroup, {
emissive: 0x88bd42,
emissiveIntensity: 0.7
});
// 04 Finished compost pad.
box(2.85, 0.18, 2.55, 0x8e7656, 11.15, 0.03, z, stationGroup);
box(2.5, 0.16, 2.2, 0x4a3b2a, 11.15, 0.2, z, stationGroup);
box(0.34, 0.16, 0.34, accent, 12.32, 0.35, z - 1.0, stationGroup);
});
addStationLabel("01 / COLLECTION", -10.2, -5.75, 4.35);
addStationLabel("02 / SORTING", -2.6, -5.75, 4.1);
addStationLabel("03 / PROCESSING", 5.25, -5.75, 4.45);
addStationLabel("04 / FINISHED", 11.15, -5.75, 4.2);
// Voxel arrows between stations.
[-6.4, 1.25, 8.35].forEach(x => {
for (let i = 0; i < 4; i++) {
box(0.3, 0.09, 0.45, 0x9dbd55, x - 0.6 + i * 0.4, 0.03, 0, stationGroup);
}
box(0.3, 0.09, 0.95, 0x9dbd55, x + 0.65, 0.03, 0, stationGroup);
box(0.3, 0.09, 0.58, 0x9dbd55, x + 1.02, 0.03, 0, stationGroup);
});
// Known reject station.
box(1.55, 0.65, 1.25, 0x8d6a50, -6.15, 0.28, 5.35, stationGroup);
box(1.32, 0.16, 1.05, 0x322e29, -6.15, 0.66, 5.35, stationGroup);
const rejectLabel = makeLabel("REJECT BIN", {
background: "#863f35",
color: "#fff5e9",
accent: "#efd66a",
fontSize: 48
});
rejectLabel.position.set(-6.15, 1.55, 5.35);
rejectLabel.scale.set(2.35, 0.515, 1);
stationGroup.add(rejectLabel);
// Decorative block plants around the garden.
const plants = [
[-14.1, -3.7, 0x486f48], [-13.2, 4.5, 0x789c50],
[14.1, -3.4, 0x5f864b], [13.2, 4.5, 0x466f4c]
];
plants.forEach(([x, z, color]) => {
box(0.24, 1.0, 0.24, 0x7b6743, x, 0.42, z, stationGroup);
box(0.72, 0.72, 0.72, color, x, 1.15, z, stationGroup);
box(0.54, 0.54, 0.54, color, x + 0.42, 0.92, z + 0.18, stationGroup);
box(0.48, 0.48, 0.48, color, x - 0.35, 0.88, z - 0.2, stationGroup);
});
}
function defaultBatches() {
return {
"A-17": {
id: "A-17",
name: "Leaf & vine mix",
shortName: "Leaf mix",
color: "#719a55",
source: "North hedge / green caddy",
collected: "07:42",
originalCount: 7,
usableCount: 7,
stage: "sorting",
contamination: null,
palette: [0x668c4f, 0x82a95e, 0x9abb65, 0x557c48, 0x789f52],
events: [
{ time: "07:42", title: "Collected at source", detail: "North hedge · green caddy · source weight 1.8 kg", kind: "" },
{ time: "08:06", title: "Accepted at sorting", detail: "7 usable pieces · batch identity A-17 retained", kind: "" }
]
},
"B-04": {
id: "B-04",
name: "Citrus & coffee",
shortName: "Citrus mix",
color: "#df9552",
source: "Market café / compost caddy",
collected: "07:55",
originalCount: 9,
usableCount: 8,
stage: "sorting",
contamination: {
name: "Blue plastic cap",
detectedAt: "08:19"
},
palette: [0xde8d45, 0xefa958, 0xc96f39, 0xf2bb6c, 0xb96835],
events: [
{ time: "07:55", title: "Collected at source", detail: "Market café · compost caddy · source weight 2.4 kg", kind: "" },
{ time: "08:18", title: "Accepted at sorting", detail: "8 usable pieces · batch identity B-04 retained", kind: "" },
{ time: "08:19", title: "Unsuitable item detected", detail: "Blue plastic cap · known contaminant", kind: "alert" }
]
},
"C-09": {
id: "C-09",
name: "Paper filter stack",
shortName: "Filter stack",
color: "#9a718f",
source: "Corner kiosk / bean cart",
collected: "08:03",
originalCount: 6,
usableCount: 6,
stage: "collection",
contamination: null,
palette: [0x9b718f, 0xb28aa2, 0x815d78, 0xc19aaf, 0x76566f],
events: [
{ time: "08:03", title: "Collected at source", detail: "Corner kiosk · bean cart · source weight 0.9 kg", kind: "" }
]
}
};
}
function currentPieceCount(batch) {
return batch.usableCount + (batch.contamination ? 1 : 0);
}
function nextDemoTime() {
demoMinute += 1;
const hour = Math.floor(demoMinute / 60);
const minute = demoMinute % 60;
return `${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`;
}
function makePiece(batch, index, parent) {
const piece = new THREE.Group();
piece.userData.batchId = batch.id;
const color = batch.palette[index % batch.palette.length];
if (batch.id === "A-17") {
box(0.46, 0.11, 0.32, color, 0, 0.08, 0, piece);
box(0.27, 0.10, 0.19, color, 0.15, 0.17, 0.05, piece);
} else if (batch.id === "B-04") {
box(0.29, 0.25, 0.28, color, 0, 0.13, 0, piece);
box(0.18, 0.16, 0.18, color, 0.13, 0.29, 0.03, piece);
} else {
box(0.38, 0.07, 0.29, color, 0, 0.06, 0, piece);
box(0.31, 0.06, 0.24, color, 0.04, 0.14, 0.02, piece);
box(0.22, 0.05, 0.18, color, -0.06, 0.21, -0.03, piece);
}
const column = index % 4;
const row = Math.floor(index / 4);
piece.position.set(
(column - 1.5) * 0.42 + (row % 2 ? 0.08 : 0),
0.08 + (index % 3) * 0.04,
(row - 1) * 0.43
);
piece.rotation.y = ((index * 37) % 11 - 5) * 0.025;
parent.add(piece);
return piece;
}
function makeSelectionMarker(batch) {
const marker = new THREE.Group();
const yellow = new THREE.MeshBasicMaterial({ color: 0xf4dc69 });
const positions = [];
for (let i = 0; i < 6; i++) {
const t = -0.95 + i * 0.38;
positions.push([t, -0.83], [t, 0.83]);
}
for (let i = 1; i < 4; i++) {
const t = -0.64 + i * 0.32;
positions.push([-1.12, t], [1.12, t]);
}
positions.forEach(([x, z]) => {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(0.16, 0.055, 0.16), yellow);
mesh.position.set(x, 0.035, z);
marker.add(mesh);
});
marker.userData.batchId = batch.id;
return marker;
}
function makeContaminant(batch) {
const contaminant = new THREE.Group();
contaminant.userData.batchId = batch.id;
contaminant.userData.contaminant = batch.id;
box(0.43, 0.14, 0.43, 0x2865d6, 0, 0.08, 0, contaminant);
box(0.31, 0.15, 0.31, 0x3979e8, 0, 0.22, 0, contaminant);
box(0.14, 0.13, 0.27, 0x2255bd, 0.24, 0.15, 0, contaminant);
const label = makeLabel("PLASTIC CAP", {
background: "#b44739",
color: "#fff5e9",
accent: "#efd66a",
fontSize: 48
});
label.position.set(0, 0.72, 0);
label.scale.set(1.9, 0.416, 1);
contaminant.add(label);
contaminant.position.set(0.9, 0.62, 0.68);
contaminant.userData.baseY = contaminant.position.y;
return contaminant;
}
function makeBatchVisual(batch) {
const group = new THREE.Group();
group.userData.batchId = batch.id;
group.position.set(
stagePositions[batch.stage].x,
stagePositions[batch.stage].y,
laneZ[batch.id]
);
const tray = box(2.18, 0.1, 1.88, batch.color, 0, 0.01, 0, group);
tray.material = tray.material.clone();
tray.material.transparent = true;
tray.material.opacity = 0.28;
for (let i = 0; i < batch.usableCount; i++) {
makePiece(batch, i, group);
}
const label = makeLabel(`${batch.id} · ${batch.shortName.toUpperCase()}`, {
background: "#183d32",
color: "#fffbed",
accent: batch.color,
fontSize: 50
});
label.position.set(0, 2.17, 0);
label.scale.set(3.85, 0.843, 1);
group.add(label);
const selection = makeSelectionMarker(batch);
selection.visible = false;
group.add(selection);
let contaminant = null;
if (batch.contamination) {
contaminant = makeContaminant(batch);
group.add(contaminant);
}
batchWorldGroup.add(group);
visuals[batch.id] = { group, selection, contaminant, pieces: [] };
}
function disposeGroup(group) {
group.traverse(object => {
if (object.geometry) object.geometry.dispose();
});
group.removeFromParent();
}
function resetGarden() {
Object.values(visuals).forEach(visual => disposeGroup(visual.group));
rejectClones.forEach(clone => disposeGroup(clone));
visuals = {};
rejectClones = [];
batches = defaultBatches();
ageRecords = [
{ id: "AGE-001", batch: "A-17", age: 1.2, captured: "08:10", source: "fixture-leaf-v2" },
{ id: "AGE-002", batch: "B-04", age: 0.8, captured: "08:16", source: "fixture-citrus-v1" },
{ id: "AGE-003", batch: "C-09", age: 0.3, captured: "08:27", source: "fixture-filter-v2" }
];
demoMinute = 9 * 60 + 10;
selectedId = "B-04";
Object.values(batches).forEach(makeBatchVisual);
renderUI();
setView("overview", false);
showToast("Garden reset. Three batch identities restored.");
}
function tweenPosition(object, target, duration = 950, onComplete = null) {
tweens.push({
object,
from: object.position.clone(),
to: target.clone(),
start: performance.now(),
duration,
onComplete
});
}
function selectBatch(id) {
if (!batches[id]) return;
selectedId = id;
renderUI();
}
function advanceBatch(id) {
const batch = batches[id];
if (!batch) return;
if (batch.stage === "sorting" && batch.contamination) {
showToast("Resolve the blue plastic cap before processing B-04.", true);
return;
}
const index = stageOrder.indexOf(batch.stage);
if (index >= stageOrder.length - 1) return;
const previous = batch.stage;
const next = stageOrder[index + 1];
batch.stage = next;
const eventText = {
sorting: `Sorted ${batch.usableCount} usable pieces · identity ${batch.id} retained`,
processing: `Loaded into processor lane ${laneZ[id] > 0 ? "3" : laneZ[id] < 0 ? "1" : "2"} · identity ${batch.id} retained`,
finished: `Cured and finished · identity ${batch.id} retained`
}[next];
batch.events.push({
time: nextDemoTime(),
title: next === "sorting" ? "Moved into sorting" : next === "processing" ? "Processing started" : "Batch finished",
detail: eventText,
kind: ""
});
tweenPosition(
visuals[id].group,
new THREE.Vector3(stagePositions[next].x, stagePositions[next].y, laneZ[id]),
1050
);
const syntheticAge = next === "finished" ? 3.0 : next === "processing" ? 1.6 : 0.7;
addAgeRecord(batch, syntheticAge, `fixture-${next}-v1`);
renderUI();
showToast(`${batch.id} moved from ${stageNames[previous]} to ${stageNames[next]}. Identity preserved.`);
}
function removeContaminant(id) {
const batch = batches[id];
const visual = visuals[id];
if (!batch?.contamination || !visual?.contaminant) return;
const contaminant = visual.contaminant;
const worldPosition = new THREE.Vector3();
contaminant.getWorldPosition(worldPosition);
const clone = contaminant.clone(true);
rejectWorldGroup.add(clone);
clone.position.copy(worldPosition);
clone.quaternion.copy(contaminant.getWorldQuaternion(new THREE.Quaternion()));
clone.scale.copy(contaminant.getWorldScale(new THREE.Vector3()));
clone.userData.removed = true;
rejectClones.push(clone);
contaminant.visible = false;
batch.contamination = null;
batch.events.push({
time: nextDemoTime(),
title: "Unsuitable item removed",
detail: `Blue plastic cap moved to reject bin · ${batch.id} retained · current count ${batch.usableCount}`,
kind: "alert"
});
tweenPosition(
clone,
new THREE.Vector3(-6.15, 0.77, 5.35),
1150,
() => {
clone.rotation.z = -0.22;
clone.rotation.y = 0.35;
}
);
renderUI();
showToast(`${batch.id}: blue plastic cap removed and recorded as a reject.`);
}
function addAgeRecord(batch, age, source) {
const nextNumber = ageRecords.length + 1;
ageRecords.unshift({
id: `AGE-${String(nextNumber).padStart(3, "0")}`,
batch: batch.id,
age: Number(Number(age).toFixed(1)),
captured: nextDemoTime(),
source
});
renderAgeTable();
}
function renderBatchList() {
batchListElement.innerHTML = Object.values(batches).map(batch => {
const alert = Boolean(batch.contamination);
return `
<button class="batch-card ${selectedId === batch.id ? "active" : ""}"
style="--batch-color:${batch.color}"
type="button" data-select="${batch.id}">
<span class="batch-swatch"></span>
<span>
<strong>${batch.id} · ${batch.shortName}</strong>
<small>${currentPieceCount(batch)} pieces · ${batch.source.split(" / ")[0]}</small>
</span>
<span class="stage ${alert ? "alert" : ""}">${alert ? "Flagged" : stageNames[batch.stage]}</span>
</button>
`;
}).join("");
}
function renderDetail() {
const batch = batches[selectedId];
const stageIndex = stageOrder.indexOf(batch.stage);
const hasContaminant = Boolean(batch.contamination);
const primaryLabels = {
collection: "Move to sorting",
sorting: hasContaminant ? "Resolve cap to continue" : "Move to processing",
processing: "Finish & cure",
finished: "Batch complete"
};
const primaryDisabled = hasContaminant || batch.stage === "finished";
detailElement.innerHTML = `
<div class="detail-title-row">
<div>
<span class="batch-id" style="--batch-color:${batch.color}">${batch.id}</span>
<h3>${batch.name}</h3>
<p class="detail-subtitle">${batch.source}</p>
</div>
<button class="icon-button" type="button" id="focusBatch" title="Focus camera on batch" aria-label="Focus camera on batch">⌖</button>
</div>
<div class="facts">
<div class="fact">
<span>Collected</span>
<strong>${batch.collected} · source caddy</strong>
</div>
<div class="fact">
<span>Current pieces</span>
<strong>${currentPieceCount(batch)} total · ${batch.usableCount} usable</strong>
</div>
<div class="fact">
<span>Original intake</span>
<strong>${batch.originalCount} pieces</strong>
</div>
<div class="fact">
<span>Current station</span>
<strong>${stageNames[batch.stage]} bay</strong>
</div>
</div>
<div class="progress" aria-label="Batch progress">
${stageOrder.map((stage, index) => `
<div class="progress-step ${index < stageIndex ? "done" : index === stageIndex ? "current" : ""}">
${stageNames[stage]}
</div>
`).join("")}
</div>
<div class="action-stack">
<button class="primary-action" type="button" id="primaryAction"
data-advance="${batch.id}" ${primaryDisabled ? "disabled" : ""}>
${primaryLabels[batch.stage]}
</button>
${hasContaminant ? `
<button class="reject-action" type="button" id="removeAction" data-remove="${batch.id}">
Remove blue plastic cap
</button>
` : ""}
<div class="action-note">
${hasContaminant
? "Known contaminant blocks processing. Removing it preserves batch ${batch.id}."
: "The ID, source, and intake count stay attached during every move."}
</div>
</div>
<div class="section-label">Provenance trail</div>
<div class="timeline">
${batch.events.map(event => `
<div class="event ${event.kind === "alert" ? "alert" : ""}">
<time>${event.time}</time>
<strong>${event.title}</strong>
<p>${event.detail}</p>
</div>
`).join("")}
</div>
`;
}
function renderAgeTable() {
ageRowsElement.innerHTML = ageRecords.map(record => {
const fill = Math.max(14, Math.min(100, record.age / 3 * 100));
return `
<tr>
<td>${record.id}</td>
<td><span class="legend-dot" style="display:inline-block;background:${batches[record.batch]?.color || "#719a55"}"></span> ${record.batch}</td>
<td><span class="age-cell" style="--age-fill:${fill}%">${record.age.toFixed(1)} d</span></td>
<td>18 May · ${record.captured}</td>
<td>${record.source}</td>
</tr>
`;
}).join("");
}
function renderStatus() {
const counts = { collection: 0, sorting: 0, processing: 0, finished: 0 };
let contaminationCount = 0;
Object.values(batches).forEach(batch => {
counts[batch.stage]++;
if (batch.contamination) contaminationCount++;
});
sceneStatusElement.textContent =
`${counts.collection} collection · ${counts.sorting} sorting · ${counts.processing} processing · ${counts.finished} finished`;
topAlertElement.textContent = contaminationCount
? `${contaminationCount} unsuitable item`
: "No unsuitable items";
topAlertElement.classList.toggle("alert", contaminationCount > 0);
}
function renderUI() {
renderBatchList();
renderDetail();
renderAgeTable();
renderStatus();
}
function showToast(message, error = false) {
clearTimeout(toastTimer);
toastElement.textContent = message;
toastElement.classList.toggle("error", error);
toastElement.classList.add("show");
toastTimer = setTimeout(() => toastElement.classList.remove("show"), 3100);
}
batchListElement.addEventListener("click", event => {
const button = event.target.closest("[data-select]");
if (button) selectBatch(button.dataset.select);
});
detailElement.addEventListener("click", event => {
const advance = event.target.closest("[data-advance]");
const remove = event.target.closest("[data-remove]");
if (advance) advanceBatch(advance.dataset.advance);
if (remove) removeContaminant(remove.dataset.remove);
if (event.target.closest("#focusBatch")) {
setView(batches[selectedId].stage, true);
}
});
document.getElementById("resetButton").addEventListener("click", resetGarden);
document.getElementById("addAgeButton").addEventListener("click", () => {
const batch = batches[selectedId];
const stage = stageOrder.indexOf(batch.stage);
const age = 0.4 + stage * 0.72 + batch.usableCount * 0.035;
addAgeRecord(batch, age, `fixture-manual-${ageRecords.length + 1}`);
showToast(`Added a separate synthetic age record for ${batch.id}.`);
});
// Custom orbit / pan / zoom controls. No camera-control addons are used.
const control = {
target: new THREE.Vector3(0, 0.7, 0),
radius: 27,
theta: 0.58,
phi: 0.86
};
const current = {
target: control.target.clone(),
radius: control.radius,
theta: control.theta,
phi: control.phi
};
const pointerState = {
active: false,
button: 0,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: 0
};
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
pointerState.active = true;
pointerState.button = event.button;
pointerState.startX = event.lastX;
pointerState.startY = event.lastY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
pointerState.moved = 0;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!pointerState.active) return;
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
pointerState.moved += Math.abs(dx) + Math.abs(dy);
if (pointerState.button === 0) {
control.theta -= dx * 0.006;
control.phi = THREE.MathUtils.clamp(control.phi - dy * 0.005, 0.42, 1.38);
} else {
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 scale = control.radius * 0.0013;
control.target.addScaledVector(right, -dx * scale);
control.target.y = THREE.MathUtils.clamp(control.target.y + dy * scale, -1, 6);
}
});
canvas.addEventListener("pointerup", event => {
const wasClick = pointerState.moved < 7 && pointerState.button === 0;
pointerState.active = false;
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
if (wasClick) handleSceneClick(event);
});
canvas.addEventListener("pointercancel", () => {
pointerState.active = false;
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
control.radius = THREE.MathUtils.clamp(
control.radius * Math.exp(event.deltaY * 0.001),
10,
42
);
}, { passive: false });
function objectIsVisible(object) {
let currentObject = object;
while (currentObject) {
if (!currentObject.visible) return false;
currentObject = currentObject.parent;
}
return true;
}
function findAncestorData(object, key) {
let currentObject = object;
while (currentObject) {
if (currentObject.userData?.[key]) return currentObject.userData[key];
currentObject = currentObject.parent;
}
return null;
}
function handleSceneClick(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const roots = Object.values(visuals).map(visual => visual.group);
const hits = raycaster.intersectObjects(roots, true);
for (const hit of hits) {
if (!objectIsVisible(hit.object)) continue;
const contaminantId = findAncestorData(hit.object, "contaminant");
if (contaminantId) {
removeContaminant(contaminantId);
return;
}
const batchId = findAncestorData(hit.object, "batchId");
if (batchId) {
selectBatch(batchId);
return;
}
}
}
const views = {
overview: { target: [0, 0.7, 0], radius: 27, theta: 0.58, phi: 0.86 },
collection: { target: [-10.2, 0.8, 0], radius: 13.5, theta: 0.7, phi: 0.9 },
sorting: { target: [-2.6, 1.0, 0], radius: 13.5, theta: 0.68, phi: 0.88 },
processing: { target: [5.25, 0.8, 0], radius: 13.5, theta: 0.65, phi: 0.88 }
};
function setView(name, announce = true) {
const view = views[name];
if (!view) return;
control.target.set(...view.target);
control.radius = view.radius;
control.theta = view.theta;
control.phi = view.phi;
if (announce) showToast(`Camera: ${name} station.`);
}
document.querySelectorAll("[data-view]").forEach(button => {
button.addEventListener("click", () => setView(button.dataset.view));
});
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
if (!width || !height) return;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
const spherical = new THREE.Spherical();
let lastTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min(0.05, (now - lastTime) / 1000);
lastTime = now;
for (let i = tweens.length - 1; i >= 0; i--) {
const tween = tweens[i];
const progress = THREE.MathUtils.clamp((now - tween.start) / tween.duration, 0, 1);
const eased = 1 - Math.pow(1 - progress, 3);
tween.object.position.lerpVectors(tween.from, tween.to, eased);
if (progress >= 1) {
tween.object.position.copy(tween.to);
tweens.splice(i, 1);
tween.onComplete?.();
}
}
const smoothing = 1 - Math.pow(0.001, delta);
current.target.lerp(control.target, smoothing);
current.radius = THREE.MathUtils.lerp(current.radius, control.radius, smoothing);
current.theta = THREE.MathUtils.lerp(current.theta, control.theta, smoothing);
current.phi = THREE.MathUtils.lerp(current.phi, control.phi, smoothing);
spherical.set(current.radius, current.phi, current.theta);
camera.position.setFromSpherical(spherical).add(current.target);
camera.lookAt(current.target);
Object.values(visuals).forEach((visual, index) => {
visual.selection.visible = visual.group.userData.batchId === selectedId;
const pulse = 1 + Math.sin(now * 0.004 + index) * 0.035;
visual.selection.scale.setScalar(pulse);
if (visual.contaminant?.visible) {
visual.contaminant.position.y =
visual.contaminant.userData.baseY + Math.sin(now * 0.004 + index) * 0.045;
}
});
renderer.render(scene, camera);
}
buildWorld();
resetGarden();
resize();
requestAnimationFrame(animate);
</script>
</body>
</html>
```