Prompt specification
Build or customize the Three.js 3D scene "Reusable Container Completes Two Loops" 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: Reusable Container Completes Two Loops
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/reusable-container-completes-two-loops-x3950
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing material lifecycles and reusable packaging. Animate a container moving through use, return, cleaning placeholder, and reuse. Count trips and losses explicitly while leaving real hygiene requirements to verified sources. Voxel style.
Working Three.js Starter (reusable-container-completes-two-loops-x3950):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Packaging Loop — Reusable Material Lifecycle</title>
<style>
:root {
--paper: #f3eee4;
--panel: #fffdf7;
--ink: #14213d;
--muted: #667085;
--line: #d8d1c4;
--coral: #ff6b57;
--teal: #22b8ad;
--yellow: #f2c14e;
--blue: #4b74c9;
--dark: #17233d;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
body {
padding: 18px;
}
button,
a {
font: inherit;
}
.app {
max-width: 1500px;
margin: 0 auto;
}
header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 14px;
}
.eyebrow {
margin: 0 0 6px;
color: var(--coral);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 48px);
line-height: 0.96;
letter-spacing: -0.055em;
}
.subtitle {
max-width: 660px;
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.model-badge {
flex: 0 0 auto;
border: 2px solid var(--ink);
background: var(--yellow);
box-shadow: 4px 4px 0 var(--ink);
padding: 10px 12px;
font-size: 11px;
font-weight: 800;
line-height: 1.5;
text-transform: uppercase;
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 365px;
gap: 14px;
align-items: stretch;
}
.viewport {
position: relative;
min-height: 660px;
overflow: hidden;
border: 2px solid var(--ink);
background: #e9e1d5;
box-shadow: 6px 6px 0 var(--ink);
isolation: isolate;
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
image-rendering: pixelated;
}
#scene:active {
cursor: grabbing;
}
.viewport-top {
position: absolute;
z-index: 5;
top: 14px;
left: 14px;
right: 14px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.help-chip,
.view-chip {
border: 1px solid rgba(20, 33, 61, 0.65);
background: rgba(255, 253, 247, 0.9);
padding: 8px 10px;
font-size: 10px;
font-weight: 700;
line-height: 1.4;
text-transform: uppercase;
}
.view-chip {
text-align: right;
}
.viewport-bottom {
position: absolute;
z-index: 5;
left: 14px;
right: 14px;
bottom: 14px;
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
gap: 12px;
align-items: end;
pointer-events: none;
}
.status-card {
width: min(440px, 100%);
border: 2px solid var(--ink);
background: rgba(255, 253, 247, 0.95);
box-shadow: 4px 4px 0 rgba(20, 33, 61, 0.85);
padding: 11px 12px;
}
.status-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
font-size: 11px;
font-weight: 800;
text-transform: uppercase;
}
#phase-name {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
#sim-time {
flex: 0 0 auto;
color: var(--muted);
}
.progress-track {
height: 10px;
border: 1px solid var(--ink);
background: #ded6c9;
overflow: hidden;
}
#progress-fill {
width: 0;
height: 100%;
background: var(--coral);
transition: width 80ms linear;
}
.scale-note {
max-width: 160px;
border: 1px solid var(--ink);
background: rgba(20, 33, 61, 0.9);
color: white;
padding: 8px 10px;
font-size: 9px;
line-height: 1.5;
text-transform: uppercase;
}
.stage-label {
position: absolute;
z-index: 4;
top: 0;
left: 0;
min-width: 120px;
border: 2px solid var(--ink);
background: rgba(255, 253, 247, 0.96);
box-shadow: 3px 3px 0 rgba(20, 33, 61, 0.7);
padding: 7px 9px;
pointer-events: none;
will-change: transform;
}
.stage-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -7px;
width: 10px;
height: 10px;
border-right: 2px solid var(--ink);
border-bottom: 2px solid var(--ink);
background: var(--panel);
transform: translateX(-50%) rotate(45deg);
}
.stage-label .label-top {
display: flex;
align-items: center;
gap: 7px;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.stage-label .dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border: 1px solid var(--ink);
background: var(--stage-color, var(--coral));
}
.stage-label .label-state {
margin-top: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
text-transform: uppercase;
}
.stage-label.active {
outline: 3px solid rgba(255, 107, 87, 0.38);
}
aside {
display: flex;
flex-direction: column;
gap: 12px;
}
.card {
border: 2px solid var(--ink);
background: var(--panel);
box-shadow: 5px 5px 0 var(--ink);
padding: 15px;
}
.card h2 {
margin: 0 0 12px;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.counters {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.counter {
min-height: 100px;
border: 1px solid var(--line);
background: #f7f2e9;
padding: 10px;
}
.counter .number {
margin: 5px 0 3px;
font-size: clamp(28px, 4vw, 42px);
font-weight: 900;
line-height: 1;
letter-spacing: -0.07em;
}
.counter .name {
font-size: 10px;
font-weight: 900;
text-transform: uppercase;
}
.counter .definition {
margin-top: 5px;
color: var(--muted);
font-size: 8px;
line-height: 1.4;
}
.counter.trips .number {
color: var(--coral);
}
.counter.losses .number {
color: var(--blue);
}
.counter.active .number {
color: #168f87;
}
.timeline {
display: grid;
gap: 7px;
}
.stage-step {
position: relative;
display: grid;
grid-template-columns: 30px 1fr;
gap: 9px;
align-items: center;
border: 1px solid var(--line);
padding: 9px;
opacity: 0.58;
transition: opacity 180ms, background 180ms, transform 180ms;
}
.stage-step.active {
border-color: var(--ink);
background: #fff3dd;
opacity: 1;
transform: translateX(3px);
}
.stage-step .index {
display: grid;
place-items: center;
width: 28px;
height: 28px;
border: 1px solid var(--ink);
background: var(--step-color);
font-size: 10px;
font-weight: 900;
}
.stage-step strong {
display: block;
font-size: 10px;
text-transform: uppercase;
}
.stage-step small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
line-height: 1.35;
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 39px;
border: 1px solid var(--ink);
background: var(--panel);
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 900;
text-transform: uppercase;
}
button:hover:not(:disabled) {
background: var(--yellow);
}
button:active:not(:disabled) {
transform: translateY(1px);
}
button:focus-visible,
a:focus-visible {
outline: 3px solid var(--coral);
outline-offset: 2px;
}
button:disabled {
cursor: not-allowed;
opacity: 0.45;
}
.primary {
background: var(--ink);
color: white;
}
.primary:hover:not(:disabled) {
background: var(--coral);
color: white;
}
.loss-button {
background: #e8effd;
}
.ledger {
margin: 0;
padding: 0;
list-style: none;
border-top: 1px solid var(--line);
}
.ledger li {
display: grid;
grid-template-columns: 48px 1fr;
gap: 8px;
border-bottom: 1px solid var(--line);
padding: 8px 0;
font-size: 9px;
line-height: 1.4;
}
.ledger time {
color: var(--muted);
font-weight: 800;
}
.ledger .loss-entry {
color: var(--blue);
font-weight: 800;
}
.ledger .trip-entry {
color: #b73c2d;
font-weight: 800;
}
.note {
margin: 0;
color: var(--muted);
font-size: 9px;
line-height: 1.55;
}
.note strong {
color: var(--ink);
}
.source-list {
margin: 8px 0 0;
padding-left: 17px;
font-size: 9px;
line-height: 1.7;
}
.source-list a {
color: var(--blue);
}
.hygiene-note {
border-left: 5px solid var(--teal);
background: #eef8f5;
}
footer {
margin: 18px 0 4px;
color: var(--muted);
font-size: 9px;
line-height: 1.6;
}
@media (max-width: 980px) {
body {
padding: 10px;
}
header {
align-items: flex-start;
}
.layout {
grid-template-columns: 1fr;
}
.viewport {
min-height: 610px;
}
aside {
display: grid;
grid-template-columns: 1fr 1fr;
align-items: start;
}
.hygiene-note {
grid-column: 1 / -1;
}
}
@media (max-width: 620px) {
header {
display: block;
}
.model-badge {
display: inline-block;
margin-top: 14px;
}
.viewport {
min-height: 540px;
}
aside {
display: flex;
}
.viewport-bottom {
grid-template-columns: 1fr;
}
.scale-note {
display: none;
}
.stage-label {
min-width: 96px;
padding: 5px 7px;
}
.stage-label .label-state {
display: none;
}
.view-chip {
display: none;
}
}
</style>
</head>
<body>
<main class="app">
<header>
<div>
<p class="eyebrow">Material lifecycle / unit accounting</p>
<h1>PACKAGING<br>LOOP</h1>
<p class="subtitle">
One reusable container is animated through use, return, a deliberately
unspecified cleaning placeholder, and reuse. Counts below are explicit
model events—not estimates of real-world loss or hygiene performance.
</p>
</div>
<div class="model-badge">Voxel model<br>Illustrative ledger</div>
</header>
<section class="layout">
<div class="viewport" id="viewport">
<canvas id="scene" aria-label="Interactive voxel animation of a reusable packaging container"></canvas>
<div class="viewport-top">
<div class="help-chip">Drag: orbit<br>Wheel: zoom</div>
<div class="view-chip">Orthographic scene<br>12-unit example fleet</div>
</div>
<div id="labels" aria-hidden="true"></div>
<div class="viewport-bottom">
<div class="status-card">
<div class="status-line">
<span id="phase-name">Use · filled</span>
<span id="sim-time">T+ 00:00</span>
</div>
<div class="progress-track">
<div id="progress-fill"></div>
</div>
</div>
<div class="scale-note">
Cleaning stage is a placeholder only. No procedure is implied.
</div>
</div>
</div>
<aside>
<section class="card">
<h2>Explicit unit ledger</h2>
<div class="counters">
<div class="counter trips">
<div class="name">Trips</div>
<div class="number" id="trip-count" aria-live="polite">000</div>
<div class="definition">Completed use → return legs by the highlighted unit.</div>
</div>
<div class="counter losses">
<div class="name">Loss events</div>
<div class="number" id="loss-count" aria-live="polite">000</div>
<div class="definition">Packaging units removed from the active fleet.</div>
</div>
<div class="counter active">
<div class="name">Active units</div>
<div class="number" id="active-count">012</div>
<div class="definition">Active = total issued units − recorded losses.</div>
</div>
<div class="counter">
<div class="name">Total units</div>
<div class="number" id="total-count">012</div>
<div class="definition">Initial fleet in this illustrative scenario.</div>
</div>
</div>
</section>
<section class="card">
<h2>Lifecycle sequence</h2>
<div class="timeline" id="timeline">
<div class="stage-step active" data-stage="0" style="--step-color: var(--coral)">
<div class="index">01</div>
<div>
<strong>Use</strong>
<small>Fill and issue the container.</small>
</div>
</div>
<div class="stage-step" data-stage="1" style="--step-color: var(--blue)">
<div class="index">02</div>
<div>
<strong>Return</strong>
<small>Receive and count the returned unit.</small>
</div>
</div>
<div class="stage-step" data-stage="2" style="--step-color: var(--teal)">
<div class="index">03</div>
<div>
<strong>Cleaning placeholder</strong>
<small>Hold only; no cleaning or sanitation procedure is modeled.</small>
</div>
</div>
<div class="stage-step" data-stage="3" style="--step-color: var(--yellow)">
<div class="index">04</div>
<div>
<strong>Reuse</strong>
<small>Refill and reissue for the next use cycle.</small>
</div>
</div>
</div>
</section>
<section class="card">
<h2>Simulation controls</h2>
<div class="controls">
<button id="pause-button" class="primary" type="button">Pause</button>
<button id="speed-button" type="button">Speed: 1×</button>
<button id="loss-button" class="loss-button" type="button">Log 1 unit loss</button>
<button id="reset-button" type="button">Reset model</button>
</div>
<p class="note" style="margin-top: 11px">
<strong>Trip definition:</strong> one completed movement from use to
return. <strong>Loss definition:</strong> one packaging unit is
removed from the active fleet and recorded in the event ledger.
</p>
</section>
<section class="card">
<h2>Event ledger</h2>
<ul class="ledger" id="event-ledger" aria-live="polite"></ul>
</section>
<section class="card hygiene-note">
<h2>Hygiene boundary</h2>
<p class="note">
<strong>The “cleaning” station is an unlabeled hold/placeholder.</strong>
It does not depict or validate washing, sanitation, inspection,
drying, temperature, chemical, or acceptance requirements. Real
requirements vary by material, contents, use, supplier, and
jurisdiction. Verify current applicable rules with authoritative
sources before reuse.
</p>
<ul class="source-list">
<li>
Official reference:
<a href="https://www.fda.gov/food/fda-food-code/food-code-2022"
target="_blank" rel="noopener noreferrer">FDA Food Code 2022</a>
(US; where applicable).
</li>
<li>
Official reference:
<a href="https://eur-lex.europa.eu/eli/reg/2004/852/oj"
target="_blank" rel="noopener noreferrer">Regulation (EC) No. 852/2004</a>
(EU; where applicable).
</li>
</ul>
</section>
</aside>
</section>
<footer>
Model scope: material flow, trip counting, and illustrative packaging-unit
loss accounting only. No hygiene performance, expected life, failure rate,
food safety, or environmental benefit is claimed by this visualization.
</footer>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// DOM and renderer
// ------------------------------------------------------------
const canvas = document.getElementById("scene");
const viewport = document.getElementById("viewport");
const labelsRoot = document.getElementById("labels");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: false,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.5));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.BasicShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e1d5);
scene.fog = new THREE.Fog(0xe9e1d5, 38, 70);
const camera = new THREE.OrthographicCamera(-15, 15, 11, -11, 0.1, 100);
const hemi = new THREE.HemisphereLight(0xfff7e8, 0x7a879d, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d8, 2.8);
sun.position.set(-10, 18, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.bias = -0.0004;
scene.add(sun);
const palette = {
ink: 0x17233d,
floor: 0xe9e1d5,
floorLine: 0xd1c7b8,
conveyor: 0x34435f,
rail: 0x17233d,
coral: 0xff6b57,
teal: 0x22b8ad,
yellow: 0xf2c14e,
blue: 0x4b74c9,
cream: 0xfff8e9,
white: 0xffffff,
green: 0x65a765,
orange: 0xe98b49
};
function material(color, options = {}) {
return new THREE.MeshLambertMaterial({
color,
flatShading: true,
...options
});
}
function box(
width,
height,
depth,
x,
y,
z,
color,
parent = scene,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Direct camera controls: no external controls/addons
// ------------------------------------------------------------
const cameraState = {
theta: 0.68,
phi: 0.92,
radius: 31,
viewHeight: 22,
target: new THREE.Vector3(0, 0.7, 0),
dragging: false,
pointerX: 0,
pointerY: 0
};
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const aspect = width / Math.max(height, 1);
renderer.setSize(width, height, false);
camera.left = (-cameraState.viewHeight * aspect) / 2;
camera.right = (cameraState.viewHeight * aspect) / 2;
camera.top = cameraState.viewHeight / 2;
camera.bottom = -cameraState.viewHeight / 2;
camera.updateProjectionMatrix();
}
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
canvas.addEventListener("pointerdown", (event) => {
cameraState.dragging = true;
cameraState.pointerX = event.clientX;
cameraState.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!cameraState.dragging) return;
const dx = event.clientX - cameraState.pointerX;
const dy = event.clientY - cameraState.pointerY;
cameraState.pointerX = event.clientX;
cameraState.pointerY = event.clientY;
cameraState.theta -= dx * 0.007;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.52,
1.23
);
updateCamera();
});
function endDrag(event) {
cameraState.dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const factor = Math.exp(event.deltaY * 0.001);
cameraState.viewHeight = THREE.MathUtils.clamp(
cameraState.viewHeight * factor,
13,
31
);
resize();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraState.theta = 0.68;
cameraState.phi = 0.92;
cameraState.viewHeight = 22;
resize();
updateCamera();
});
window.addEventListener("resize", resize);
// ------------------------------------------------------------
// Voxel environment
// ------------------------------------------------------------
box(31, 0.45, 19, 0, -0.24, 0, palette.floor);
for (let x = -14; x <= 14; x += 2) {
box(0.025, 0.018, 18, x, 0.005, 0, palette.floorLine);
}
for (let z = -8; z <= 8; z += 2) {
box(30, 0.018, 0.025, 0, 0.006, z, palette.floorLine);
}
// Main conveyor.
box(24, 0.36, 1.8, 0, 0.22, -3, palette.conveyor);
box(24, 0.16, 0.16, 0, 0.52, -3.78, palette.rail);
box(24, 0.16, 0.16, 0, 0.52, -2.22, palette.rail);
// Return-to-use conveyor behind the stations.
box(24, 0.36, 1.55, 0, 0.22, 4.2, palette.conveyor);
box(24, 0.16, 0.16, 0, 0.52, 4.86, palette.rail);
box(24, 0.16, 0.16, 0, 0.52, 3.54, palette.rail);
// Side connections.
box(1.6, 0.36, 5.7, -10.7, 0.22, 0.45, palette.conveyor);
box(1.6, 0.36, 5.7, 10.7, 0.22, 0.45, palette.conveyor);
// Voxel arrows on the forward conveyor.
for (const arrowX of [-7.8, -0.8, 6.1]) {
box(0.35, 0.06, 0.85, arrowX, 0.43, -3, palette.yellow);
box(0.35, 0.06, 0.58, arrowX + 0.28, 0.43, -3, palette.yellow);
box(0.35, 0.06, 0.3, arrowX + 0.56, 0.43, -3, palette.yellow);
}
// Reverse-direction blocks on the rear conveyor.
for (const arrowX of [-7, 0, 7]) {
box(0.95, 0.06, 0.28, arrowX, 0.43, 4.2, palette.teal);
box(0.62, 0.06, 0.28, arrowX - 0.33, 0.43, 4.2, palette.teal);
box(0.3, 0.06, 0.28, arrowX - 0.66, 0.43, 4.2, palette.teal);
}
const stations = {
use: new THREE.Vector3(-10, 0.66, -3),
return: new THREE.Vector3(-3, 0.66, -3),
clean: new THREE.Vector3(3, 0.66, -3),
reuse: new THREE.Vector3(9, 0.66, -3)
};
function stationPad(x, color) {
box(3.15, 0.08, 2.45, x, 0.43, -3, color);
}
stationPad(-10, palette.coral);
stationPad(-3, palette.blue);
stationPad(3, palette.teal);
stationPad(9, palette.yellow);
// Use / issue station.
box(2.25, 0.22, 2.35, -10, 0.52, -3, palette.coral);
box(0.18, 2.1, 0.18, -10.92, 1.43, -3, palette.ink);
box(0.18, 2.1, 0.18, -9.08, 1.43, -3, palette.ink);
box(2.0, 0.22, 0.36, -10, 2.4, -3, palette.ink);
box(0.58, 0.42, 0.58, -10, 2.05, -3, palette.yellow);
// Return gate / counting station.
box(0.22, 2.1, 0.22, -4.0, 1.43, -3.7, palette.ink);
box(0.22, 2.1, 0.22, -2.0, 1.43, -3.7, palette.ink);
box(2.2, 0.22, 0.28, -3, 2.4, -3.7, palette.ink);
box(1.1, 0.48, 0.85, -3, 0.72, -4.25, palette.blue);
box(0.14, 1.2, 0.14, -3, 1.25, -4.65, palette.ink);
// Cleaning placeholder: an empty frame, deliberately not a wash process.
box(0.15, 2.25, 0.15, 1.95, 1.53, -3.75, palette.teal);
box(0.15, 2.25, 0.15, 4.05, 1.53, -3.75, palette.teal);
box(2.25, 0.15, 0.15, 3, 2.56, -3.75, palette.teal);
box(2.25, 0.15, 0.15, 3, 0.52, -3.75, palette.teal);
const cleanMarker = new THREE.Group();
cleanMarker.position.set(3, 1.55, -3);
scene.add(cleanMarker);
const markerCubeA = box(0.36, 0.36, 0.36, 0, 0, 0, palette.cream, cleanMarker);
const markerCubeB = box(0.18, 0.18, 0.18, 0.48, 0.3, 0, palette.yellow, cleanMarker);
const markerCubeC = box(0.16, 0.16, 0.16, -0.48, -0.28, 0, palette.blue, cleanMarker);
// Reuse / refill station.
box(0.25, 2.35, 0.25, 7.95, 1.58, -3.8, palette.ink);
box(0.25, 2.35, 0.25, 10.05, 1.58, -3.8, palette.ink);
box(2.35, 0.24, 0.36, 9, 2.67, -3.8, palette.ink);
box(1.05, 0.55, 0.85, 9, 0.67, -4.15, palette.yellow);
box(0.8, 0.42, 0.7, 10, 0.61, -4.15, palette.green);
// Fleet staging area.
box(25.5, 0.06, 3.1, 0, 0.03, 8.15, 0xded5c6);
for (let x = -12; x <= 12; x += 2) {
box(0.03, 0.02, 2.7, x, 0.075, 8.15, 0xcabfac);
}
// ------------------------------------------------------------
// Container geometry
// ------------------------------------------------------------
function createTote(parent, bodyColor = palette.coral, detailed = true) {
const group = new THREE.Group();
box(1.42, 0.16, 1.12, 0, 0.08, 0, bodyColor, group);
box(0.14, 0.82, 1.12, -0.64, 0.49, 0, bodyColor, group);
box(0.14, 0.82, 1.12, 0.64, 0.49, 0, bodyColor, group);
box(1.12, 0.82, 0.14, 0, 0.49, -0.49, bodyColor, group);
box(1.12, 0.82, 0.14, 0, 0.49, 0.49, bodyColor, group);
if (detailed) {
box(0.34, 0.12, 0.055, 0, 0.67, 0.535, palette.ink, group);
box(0.62, 0.32, 0.055, 0, 0.31, 0.535, palette.cream, group);
box(0.18, 0.12, 0.055, 0, 0.33, 0.568, bodyColor, group);
}
parent.add(group);
return group;
}
const hero = createTote(scene, palette.coral, true);
const contents = new THREE.Group();
hero.add(contents);
const contentCube = box(0.76, 0.54, 0.7, 0, 0.5, 0, palette.yellow, contents);
const contentTop = box(0.5, 0.18, 0.48, 0.08, 0.84, 0.02, palette.green, contents);
// ------------------------------------------------------------
// Fleet and explicit loss animation
// ------------------------------------------------------------
const TOTAL_UNITS = 12;
let activeUnits = TOTAL_UNITS;
let lossCount = 0;
let tripCount = 0;
let fleetTotes = [];
let fallingTotes = [];
const fleetPositions = [
[-11.1, 7.25], [-8.9, 7.25], [-6.7, 7.25],
[-4.5, 7.25], [-2.3, 7.25], [-0.1, 7.25],
[-10, 9.0], [-7.8, 9.0], [-5.6, 9.0],
[-3.4, 9.0], [-1.2, 9.0]
];
function clearFleet() {
for (const item of fleetTotes) {
scene.remove(item.group);
}
for (const item of fallingTotes) {
scene.remove(item.group);
}
fleetTotes = [];
fallingTotes = [];
}
function createFleet() {
clearFleet();
fleetPositions.forEach((position, index) => {
const group = new THREE.Group();
const colors = [palette.coral, palette.blue, palette.teal, palette.yellow];
const body = createTote(group, colors[index % colors.length], false);
body.scale.setScalar(0.56);
body.position.set(position[0], 0.08, position[1]);
body.userData.lost = false;
group.userData.tote = body;
scene.add(group);
fleetTotes.push({ group, body, lost: false });
});
}
// ------------------------------------------------------------
// Timeline and animation
// ------------------------------------------------------------
const phases = [
{
stage: 0,
type: "hold",
duration: 4.5,
name: "Use · filled",
position: stations.use
},
{
stage: 1,
type: "line",
duration: 2.4,
name: "Moving to return",
from: stations.use,
to: stations.return
},
{
stage: 1,
type: "hold",
duration: 3.5,
name: "Return · received and counted",
position: stations.return
},
{
stage: 2,
type: "line",
duration: 2.4,
name: "Moving to cleaning placeholder",
from: stations.return,
to: stations.clean
},
{
stage: 2,
type: "hold",
duration: 4.0,
name: "Cleaning placeholder · procedure not modeled",
position: stations.clean
},
{
stage: 3,
type: "line",
duration: 2.4,
name: "Moving to reuse",
from: stations.clean,
to: stations.reuse
},
{
stage: 3,
type: "hold",
duration: 4.0,
name: "Reuse · refill and reissue",
position: stations.reuse
},
{
stage: 3,
type: "loop",
duration: 6.0,
name: "Returning to use · next cycle",
position: stations.reuse
}
];
let phaseIndex = 0;
let phaseElapsed = 0;
let simulationTime = 0;
let paused = false;
let speed = 1;
function currentPhase() {
return phases[phaseIndex];
}
function phaseProgress() {
return THREE.MathUtils.clamp(
phaseElapsed / currentPhase().duration,
0,
1
);
}
function loopPosition(t, target) {
const reuseCorner = new THREE.Vector3(9, 0.66, 4.2);
const useCorner = new THREE.Vector3(-10, 0.66, 4.2);
if (t < 0.22) {
return target.lerpVectors(stations.reuse, reuseCorner, t / 0.22);
}
if (t < 0.72) {
return target.lerpVectors(
reuseCorner,
useCorner,
(t - 0.22) / 0.5
);
}
return target.lerpVectors(
useCorner,
stations.use,
(t - 0.72) / 0.28
);
}
function updateHero() {
const phase = currentPhase();
const t = phaseProgress();
const target = new THREE.Vector3();
if (phase.type === "hold") {
target.copy(phase.position);
} else if (phase.type === "line") {
target.lerpVectors(phase.from, phase.to, t);
} else {
loopPosition(t, target);
}
const idleBob = phase.type === "hold" ? Math.sin(simulationTime * 3.1) * 0.025 : 0;
hero.position.set(target.x, target.y + idleBob, target.z);
if (phase.type === "line") {
hero.rotation.y = Math.sin(t * Math.PI) * 0.08;
} else if (phase.type === "loop") {
hero.rotation.y = Math.sin(t * Math.PI * 2) * 0.07;
} else {
hero.rotation.y *= 0.85;
}
const shouldShowContents =
phaseIndex === 0 ||
phaseIndex === 1 ||
(phaseIndex === 6 && t > 0.48) ||
phaseIndex === 7;
contents.visible = shouldShowContents;
if (shouldShowContents) {
const fillScale = phaseIndex === 0
? 0.75 + Math.min(t * 1.4, 1) * 0.25
: 1;
contentCube.scale.y = fillScale;
contentTop.position.y = 0.46 + fillScale * 0.36;
contentTop.visible = fillScale > 0.9;
}
}
function enterPhase(index) {
phaseIndex = index;
// Arrival at return completes one use-to-return trip.
if (index === 2) {
tripCount += 1;
updateCounters();
addLedger(
`TRIP ${String(tripCount).padStart(3, "0")} counted`,
"trip-entry"
);
}
updateTimeline();
}
// ------------------------------------------------------------
// UI and event ledger
// ------------------------------------------------------------
const tripCountElement = document.getElementById("trip-count");
const lossCountElement = document.getElementById("loss-count");
const activeCountElement = document.getElementById("active-count");
const totalCountElement = document.getElementById("total-count");
const phaseNameElement = document.getElementById("phase-name");
const simTimeElement = document.getElementById("sim-time");
const progressElement = document.getElementById("progress-fill");
const ledgerElement = document.getElementById("event-ledger");
const lossButton = document.getElementById("loss-button");
function updateCounters() {
tripCountElement.textContent = String(tripCount).padStart(3, "0");
lossCountElement.textContent = String(lossCount).padStart(3, "0");
activeCountElement.textContent = String(activeUnits).padStart(3, "0");
totalCountElement.textContent = String(TOTAL_UNITS).padStart(3, "0");
lossButton.disabled = activeUnits <= 1;
lossButton.textContent = activeUnits <= 1
? "Only hero unit remains"
: "Log 1 unit loss";
}
function activeStage() {
if (phaseIndex === 7) return 3;
return currentPhase().stage;
}
function updateTimeline() {
const stage = activeStage();
document.querySelectorAll(".stage-step").forEach((element) => {
element.classList.toggle(
"active",
Number(element.dataset.stage) === stage
);
});
phaseNameElement.textContent = currentPhase().name;
labelStates[0].textContent = phaseIndex === 7
? "returning to use"
: "fill / issue";
labelStates[1].textContent = "receive / count";
labelStates[2].textContent = "procedure not modeled";
labelStates[3].textContent = "refill / reissue";
}
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const remainder = Math.floor(seconds % 60);
return `${String(minutes).padStart(2, "0")}:${String(remainder).padStart(2, "0")}`;
}
function addLedger(message, className = "") {
const item = document.createElement("li");
const time = document.createElement("time");
time.textContent = `T+${formatTime(simulationTime)}`;
const text = document.createElement("span");
text.textContent = message;
if (className) text.className = className;
item.append(time, text);
ledgerElement.prepend(item);
while (ledgerElement.children.length > 5) {
ledgerElement.lastElementChild.remove();
}
}
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const labelStates = [];
const labelDefinitions = [
{
title: "01 Use",
color: "#ff6b57",
position: new THREE.Vector3(-10, 3.0, -3)
},
{
title: "02 Return",
color: "#4b74c9",
position: new THREE.Vector3(-3, 3.0, -3)
},
{
title: "03 Cleaning placeholder",
color: "#22b8ad",
position: new THREE.Vector3(3, 3.15, -3)
},
{
title: "04 Reuse",
color: "#f2c14e",
position: new THREE.Vector3(9, 3.15, -3)
},
{
title: "Active fleet",
color: "#17233d",
position: new THREE.Vector3(0, 1.1, 8.1),
noArrow: true
}
];
const projectedLabels = labelDefinitions.map((definition) => {
const element = document.createElement("div");
element.className = "stage-label";
element.style.setProperty("--stage-color", definition.color);
const top = document.createElement("div");
top.className = "label-top";
const dot = document.createElement("span");
dot.className = "dot";
const title = document.createElement("span");
title.textContent = definition.title;
top.append(dot, title);
element.append(top);
let state = null;
if (definition.title !== "Active fleet") {
state = document.createElement("div");
state.className = "label-state";
element.append(state);
labelStates.push(state);
} else {
element.style.minWidth = "112px";
}
if (definition.noArrow) {
element.style.setProperty("--no-arrow", "1");
element.querySelector("::after");
}
labelsRoot.append(element);
return { element, position: definition.position };
});
const labelVector = new THREE.Vector3();
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
projectedLabels.forEach((label) => {
labelVector.copy(label.position).project(camera);
const visible =
labelVector.z > -1 &&
labelVector.z < 1 &&
labelVector.x > -1.15 &&
labelVector.x < 1.15 &&
labelVector.y > -1.15 &&
labelVector.y < 1.15;
label.element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (labelVector.x * 0.5 + 0.5) * width;
const y = (-labelVector.y * 0.5 + 0.5) * height;
label.element.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -100%)`;
});
}
// ------------------------------------------------------------
// Loss event handling
// ------------------------------------------------------------
function logLoss() {
const available = fleetTotes.find((item) => !item.lost);
if (!available || activeUnits <= 1) return;
available.lost = true;
activeUnits -= 1;
lossCount += 1;
fallingTotes.push({
group: available.group,
age: 0,
startY: available.group.position.y
});
addLedger(
`LOSS ${String(lossCount).padStart(3, "0")} · 1 unit removed`,
"loss-entry"
);
updateCounters();
}
function updateFallingTotes(delta) {
for (let index = fallingTotes.length - 1; index >= 0; index--) {
const item = fallingTotes[index];
item.age += delta;
const t = THREE.MathUtils.clamp(item.age / 0.9, 0, 1);
item.group.position.y = item.startY - t * 1.6;
item.group.rotation.z = t * 0.7;
item.group.scale.setScalar(1 - t * 0.75);
if (t >= 1) {
scene.remove(item.group);
fallingTotes.splice(index, 1);
}
}
}
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
document.getElementById("pause-button").addEventListener("click", (event) => {
paused = !paused;
event.currentTarget.textContent = paused ? "Resume" : "Pause";
});
document.getElementById("speed-button").addEventListener("click", (event) => {
const speeds = [1, 2, 4];
const currentIndex = speeds.indexOf(speed);
speed = speeds[(currentIndex + 1) % speeds.length];
event.currentTarget.textContent = `Speed: ${speed}×`;
});
lossButton.addEventListener("click", logLoss);
document.getElementById("reset-button").addEventListener("click", () => {
tripCount = 0;
lossCount = 0;
activeUnits = TOTAL_UNITS;
phaseIndex = 0;
phaseElapsed = 0;
simulationTime = 0;
paused = false;
speed = 1;
document.getElementById("pause-button").textContent = "Pause";
document.getElementById("speed-button").textContent = "Speed: 1×";
createFleet();
ledgerElement.innerHTML = "";
addLedger("Ledger initialized · 12 units · 0 losses");
updateCounters();
updateTimeline();
updateHero();
});
// ------------------------------------------------------------
// Main animation loop
// ------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const realDelta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
const delta = realDelta * speed;
simulationTime += delta;
phaseElapsed += delta;
while (phaseElapsed >= currentPhase().duration) {
phaseElapsed -= currentPhase().duration;
const nextIndex = (phaseIndex + 1) % phases.length;
enterPhase(nextIndex);
}
updateHero();
updateFallingTotes(delta);
const cycleProgress =
(phaseIndex + phaseProgress()) / phases.length;
progressElement.style.width = `${cycleProgress * 100}%`;
simTimeElement.textContent = `T+ ${formatTime(simulationTime)}`;
phaseNameElement.textContent = currentPhase().name;
}
// The placeholder marker is abstract, not a cleaning operation.
cleanMarker.rotation.y += realDelta * 0.8;
markerCubeA.rotation.x += realDelta * 0.45;
markerCubeB.position.y = 0.3 + Math.sin(now * 0.002) * 0.14;
markerCubeC.position.y = -0.28 + Math.cos(now * 0.002) * 0.12;
updateLabels();
renderer.render(scene, camera);
}
// Initial state.
createFleet();
addLedger("Ledger initialized · 12 units · 0 losses");
updateCounters();
updateTimeline();
updateHero();
resize();
updateCamera();
requestAnimationFrame(animate);
</script>
</body>
</html>
```