Prompt specification
Build or customize the Three.js 3D scene "School Canteen Demand Wave" 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: School Canteen Demand Wave
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/school-canteen-demand-wave-x3155
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts stock levels, reorder thresholds, and replenishment delays. Create a canteen with breakfast and lunch demand peaks, pantry space, and a delivery window. Compare stockouts and leftovers using a deterministic demand schedule. Voxel style.
Working Three.js Starter (school-canteen-demand-wave-x3155):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Canteen Flow — Voxel Supply Simulation</title>
<style>
:root {
--ink: #173d3a;
--muted: #617571;
--paper: #fffdf6;
--teal: #147d74;
--teal-soft: #d9eee8;
--orange: #ef8b3d;
--orange-soft: #fde8d4;
--red: #d95d52;
--red-soft: #f8ded9;
--blue: #4b729d;
--line: rgba(23, 61, 58, 0.14);
--shadow: 0 16px 45px rgba(32, 60, 53, 0.16);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe9df;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input { font: inherit; }
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
.panel {
position: fixed;
z-index: 5;
background: rgba(255, 253, 246, 0.92);
border: 1px solid rgba(255, 255, 255, 0.74);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
header {
top: 18px;
left: 18px;
width: min(560px, calc(100vw - 36px));
padding: 18px 20px 16px;
border-radius: 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 3px;
background: var(--orange);
border-radius: 4px;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.6vw, 34px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 8px 0 14px;
max-width: 480px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.live-strip {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.live-cell {
min-width: 0;
padding: 9px 10px;
border-radius: 11px;
background: #f2f4eb;
}
.live-cell span {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.live-cell strong {
display: block;
overflow: hidden;
color: var(--ink);
font-size: 12px;
line-height: 1.25;
text-overflow: ellipsis;
white-space: nowrap;
}
#policyPanel {
top: 18px;
right: 18px;
width: 306px;
padding: 15px;
border-radius: 18px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
}
.panel-title h2 {
margin: 0;
font-size: 13px;
letter-spacing: -0.01em;
}
.panel-title small {
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.policy-card {
margin-top: 10px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.58);
}
.policy-card.planned {
background: linear-gradient(135deg, rgba(217, 238, 232, 0.82), rgba(255, 255, 255, 0.66));
}
.policy-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 5px;
}
.policy-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 850;
}
.policy-dot {
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--red);
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.12);
}
.planned .policy-dot { background: var(--teal); }
.policy-settings {
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.metric-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: end;
gap: 7px;
margin-top: 12px;
}
.metric-row span {
color: var(--muted);
font-size: 9px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.06em;
}
.metric-row strong {
font-size: 18px;
line-height: 1;
letter-spacing: -0.04em;
}
.bar {
grid-column: 1 / -1;
height: 5px;
overflow: hidden;
border-radius: 10px;
background: #e5e8df;
}
.bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 0.5s ease;
}
.bar.red i { background: var(--red); }
.bar.blue i { background: var(--blue); }
.metric-note {
grid-column: 1 / -1;
margin-top: -2px;
color: var(--muted);
font-size: 8px;
}
#timelinePanel {
left: 18px;
right: 342px;
bottom: 18px;
min-width: 500px;
padding: 14px 16px 10px;
border-radius: 18px;
}
.timeline-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 7px;
}
.transport {
display: flex;
align-items: center;
gap: 9px;
}
#playButton {
display: grid;
width: 34px;
height: 34px;
padding: 0;
place-items: center;
border: 0;
border-radius: 10px;
background: var(--ink);
color: white;
cursor: pointer;
transition: transform 0.15s ease, background 0.15s ease;
}
#playButton:hover {
background: var(--teal);
transform: translateY(-1px);
}
#timeLabel {
min-width: 54px;
font-size: 18px;
font-weight: 850;
letter-spacing: -0.04em;
}
#timeline {
width: min(330px, 35vw);
accent-color: var(--teal);
cursor: pointer;
}
.chart-legend {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 12px;
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.chart-legend span {
display: flex;
align-items: center;
gap: 5px;
}
.chart-legend i {
width: 13px;
height: 3px;
border-radius: 4px;
background: var(--orange);
}
.chart-legend .stock { background: var(--teal); }
.chart-legend .lean { background: var(--red); }
.chart-legend .window { background: #d5e6d7; }
#chart {
display: block;
width: 100%;
height: 142px;
overflow: visible;
}
.control-hint {
position: fixed;
z-index: 4;
left: 22px;
bottom: 220px;
padding: 7px 10px;
border-radius: 8px;
background: rgba(255, 253, 246, 0.74);
color: rgba(23, 61, 58, 0.64);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.11em;
pointer-events: none;
}
#resetCamera {
position: fixed;
z-index: 6;
right: 22px;
bottom: 22px;
width: 40px;
height: 40px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 12px;
background: rgba(255, 253, 246, 0.92);
box-shadow: var(--shadow);
color: var(--ink);
cursor: pointer;
}
#resetCamera:hover { color: var(--teal); }
.scene-note {
position: fixed;
z-index: 4;
right: 22px;
bottom: 70px;
max-width: 280px;
padding: 9px 11px;
border-radius: 10px;
background: rgba(255, 253, 246, 0.78);
color: var(--muted);
font-size: 8px;
line-height: 1.45;
text-align: right;
pointer-events: none;
}
@media (max-width: 1000px) {
#policyPanel {
top: auto;
right: 14px;
bottom: 14px;
width: 276px;
}
#timelinePanel {
left: 14px;
right: 302px;
bottom: 14px;
min-width: 0;
}
.control-hint, .scene-note { display: none; }
#resetCamera {
right: 14px;
bottom: 14px;
}
}
@media (max-width: 760px) {
header {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px 14px;
}
.subtitle { display: none; }
.live-strip { grid-template-columns: repeat(2, 1fr); }
#policyPanel {
top: 142px;
right: 10px;
bottom: auto;
width: 210px;
padding: 10px;
}
.policy-card { padding: 9px; }
.policy-settings { display: none; }
.metric-row { margin-top: 7px; }
.metric-row strong { font-size: 14px; }
.metric-note { display: none; }
#timelinePanel {
left: 10px;
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
padding: 10px;
}
.timeline-head { align-items: flex-start; }
.chart-legend { max-width: 165px; }
#chart { height: 112px; }
#timeline { width: 155px; }
#resetCamera { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="panel">
<div class="eyebrow">Canteen Flow / Supply Twin</div>
<h1>Breakfast to lunch, without the waste.</h1>
<p class="subtitle">
A deterministic half-hour demand schedule tests two replenishment policies.
Pantry crates show live stock; rack markers show each reorder threshold.
</p>
<div class="live-strip">
<div class="live-cell">
<span>Simulation time</span>
<strong id="clockNow">14:00</strong>
</div>
<div class="live-cell">
<span>Demand now</span>
<strong id="demandNow">61 units</strong>
</div>
<div class="live-cell">
<span>Live pantry</span>
<strong id="stockNow">Lean 0 · Planned 19</strong>
</div>
<div class="live-cell">
<span>Delivery window</span>
<strong id="windowNow">Afternoon</strong>
</div>
</div>
</header>
<section id="policyPanel" class="panel">
<div class="panel-title">
<h2>Full-day comparison</h2>
<small>Fixed demand</small>
</div>
<article class="policy-card">
<div class="policy-head">
<div class="policy-name">
<i class="policy-dot"></i>
Lean policy
</div>
<small class="policy-settings">SMALL BATCHES</small>
</div>
<div class="policy-settings">
Reorder at 32 · order 68 · delivery delay 1–2 slots
</div>
<div class="metric-row">
<span>Stockouts</span>
<strong id="leanStockoutValue">—</strong>
<div class="bar red"><i id="leanStockoutBar"></i></div>
<div class="metric-note" id="leanStockoutNote">—</div>
</div>
<div class="metric-row">
<span>End-of-day leftovers</span>
<strong id="leanLeftoverValue">—</strong>
<div class="bar blue"><i id="leanLeftoverBar"></i></div>
<div class="metric-note">Remaining prepared-meal units</div>
</div>
</article>
<article class="policy-card planned">
<div class="policy-head">
<div class="policy-name">
<i class="policy-dot"></i>
Planned policy
</div>
<small class="policy-settings">LARGE BATCHES</small>
</div>
<div class="policy-settings">
Reorder at 72 · order 118 · delivery delay 1–2 slots
</div>
<div class="metric-row">
<span>Stockouts</span>
<strong id="plannedStockoutValue">—</strong>
<div class="bar red"><i id="plannedStockoutBar"></i></div>
<div class="metric-note" id="plannedStockoutNote">—</div>
</div>
<div class="metric-row">
<span>End-of-day leftovers</span>
<strong id="plannedLeftoverValue">—</strong>
<div class="bar blue"><i id="plannedLeftoverBar"></i></div>
<div class="metric-note">Remaining prepared-meal units</div>
</div>
</article>
</section>
<section id="timelinePanel" class="panel">
<div class="timeline-head">
<div class="transport">
<button id="playButton" aria-label="Play simulation">▶</button>
<div id="timeLabel">14:00</div>
<input id="timeline" type="range" min="0" max="31" step="1" value="16">
</div>
<div class="chart-legend">
<span><i></i>Demand</span>
<span><i class="stock"></i>Planned stock</span>
<span><i class="lean"></i>Lean stock</span>
<span><i class="window"></i>Delivery</span>
</div>
</div>
<svg id="chart" viewBox="0 0 820 190" role="img"
aria-label="Demand and pantry stock over the day"></svg>
</section>
<div class="control-hint">DRAG TO ORBIT · SCROLL TO ZOOM</div>
<div class="scene-note">
Each pantry crate represents 6 meal units.<br>
Red shelf markers are reorder thresholds.
</div>
<button id="resetCamera" title="Reset camera" aria-label="Reset camera">⌂</button>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = id => document.getElementById(id);
const clamp = THREE.MathUtils.clamp;
const fmt = n => Math.round(n).toLocaleString();
// ------------------------------------------------------------------
// Deterministic demand and replenishment simulation
// ------------------------------------------------------------------
const DEMAND = [
4, 8, 12, 18, 28, 44, 58, 62,
49, 30, 20, 16, 18, 25, 38, 52,
61, 55, 34, 22, 17, 13, 11, 9,
8, 7, 6, 5, 5, 4, 4, 3
];
const SLOT_COUNT = DEMAND.length;
const CRATE_SIZE = 6;
const DELIVERY_WINDOWS = [
{ start: 4, end: 10, name: "Morning · 08:00–11:00" },
{ start: 14, end: 20, name: "Afternoon · 13:00–16:00" },
{ start: 24, end: 28, name: "Evening · 18:00–20:00" }
];
const POLICIES = {
lean: {
name: "Lean",
initial: 90,
capacity: 120,
threshold: 32,
orderQty: 68,
crates: 20,
columns: 5,
rows: 4,
delays: [2, 2, 1, 2, 2, 1]
},
planned: {
name: "Planned",
initial: 90,
capacity: 180,
threshold: 72,
orderQty: 118,
crates: 30,
columns: 6,
rows: 5,
delays: [1, 1, 2, 1, 1, 2]
}
};
function slotTime(index) {
const minutes = 6 * 60 + index * 30;
const h = Math.floor(minutes / 60);
const m = minutes % 60;
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
}
function deliveryWindowAt(index) {
return DELIVERY_WINDOWS.find(w => index >= w.start && index <= w.end);
}
function nextDeliverySlot(index) {
if (index >= SLOT_COUNT) return SLOT_COUNT;
const current = deliveryWindowAt(index);
if (current) return index;
const next = DELIVERY_WINDOWS.find(w => w.start > index);
return next ? next.start : SLOT_COUNT;
}
function simulate(config) {
let stock = config.initial;
let pending = null;
let orderCount = 0;
let stockoutUnits = 0;
let stockoutEvents = 0;
const states = [];
for (let i = 0; i < SLOT_COUNT; i++) {
let arrival = 0;
if (pending && pending.arrival === i) {
arrival = pending.qty;
stock += arrival;
pending = null;
}
const demand = DEMAND[i];
const fulfilled = Math.min(stock, demand);
const shortage = demand - fulfilled;
stock -= fulfilled;
if (shortage > 0) {
stockoutUnits += shortage;
stockoutEvents++;
}
let ordered = 0;
if (stock <= config.threshold && !pending) {
const qty = Math.min(
config.orderQty,
Math.max(0, config.capacity - stock)
);
if (qty > 0) {
const delay = config.delays[orderCount % config.delays.length];
const rawArrival = i + delay;
const arrivalSlot = nextDeliverySlot(rawArrival);
pending = {
qty,
arrival: arrivalSlot,
orderedAt: i,
rawArrival
};
ordered = qty;
orderCount++;
}
}
states.push({
stock,
demand,
fulfilled,
shortage,
arrival,
ordered,
pending: pending ? { ...pending } : null
});
}
return {
config,
states,
final: {
stock,
leftovers: stock,
stockoutUnits,
stockoutEvents
}
};
}
const leanSim = simulate(POLICIES.lean);
const plannedSim = simulate(POLICIES.planned);
function updatePolicyCards() {
const maxStockout = Math.max(
leanSim.final.stockoutUnits,
plannedSim.final.stockoutUnits
);
const maxLeftover = Math.max(
leanSim.final.leftovers,
plannedSim.final.leftovers
);
$("leanStockoutValue").textContent = fmt(leanSim.final.stockoutUnits);
$("plannedStockoutValue").textContent = fmt(plannedSim.final.stockoutUnits);
$("leanStockoutNote").textContent =
`${leanSim.final.stockoutEvents} half-hour service windows affected`;
$("plannedStockoutNote").textContent =
`${plannedSim.final.stockoutEvents} half-hour service windows affected`;
$("leanLeftoverValue").textContent = fmt(leanSim.final.leftovers);
$("plannedLeftoverValue").textContent = fmt(plannedSim.final.leftovers);
$("leanStockoutBar").style.width =
`${100 * leanSim.final.stockoutUnits / maxStockout}%`;
$("plannedStockoutBar").style.width =
`${100 * plannedSim.final.stockoutUnits / maxStockout}%`;
$("leanLeftoverBar").style.width =
`${100 * leanSim.final.leftovers / maxLeftover}%`;
$("plannedLeftoverBar").style.width =
`${100 * plannedSim.final.leftovers / maxLeftover}%`;
}
updatePolicyCards();
// ------------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe9df);
scene.fog = new THREE.Fog(0xdfe9df, 48, 88);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
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.08;
$("scene").appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraTarget = new THREE.Vector3(0, 1.2, -0.5);
const cameraState = {
radius: 38,
theta: 0.70,
phi: 0.83
};
function updateCamera() {
const { radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// Custom camera controls: no external controls/addons.
const pointer = {
active: false,
id: null,
x: 0,
y: 0
};
renderer.domElement.addEventListener("pointerdown", event => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
cameraState.theta -= dx * 0.005;
cameraState.phi = clamp(cameraState.phi + dy * 0.004, 0.42, 1.24);
});
function stopDrag(event) {
if (event.pointerId === pointer.id) {
pointer.active = false;
pointer.id = null;
}
}
renderer.domElement.addEventListener("pointerup", stopDrag);
renderer.domElement.addEventListener("pointercancel", stopDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius + event.deltaY * 0.025,
23,
58
);
}, { passive: false });
$("resetCamera").addEventListener("click", () => {
cameraState.radius = 38;
cameraState.theta = 0.70;
cameraState.phi = 0.83;
});
scene.add(new THREE.HemisphereLight(0xf7fff4, 0x6d806b, 2.0));
const sun = new THREE.DirectionalLight(0xfff4d8, 3.4);
sun.position.set(-12, 22, 16);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -22;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa7d5d0, 1.0);
fill.position.set(16, 10, -15);
scene.add(fill);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function material(color, extras = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...extras
});
}
function box(
width, height, depth, color,
x, y, z,
parent = scene,
castShadow = true
) {
const mat = color?.isMaterial ? color : material(color);
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(initialLines, {
width = 4.2,
background = "#173d3a",
color = "#fffdf6",
accent = null
} = {}) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(width, width / 4, 1);
function set(lines) {
const rows = Array.isArray(lines) ? lines : [lines];
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (accent) {
ctx.fillStyle = accent;
ctx.fillRect(0, 0, 18, canvas.height);
}
ctx.textAlign = "center";
ctx.textBaseline = "middle";
rows.forEach((row, index) => {
const size = rows.length === 1 ? 72 : index === 0 ? 65 : 48;
ctx.font = `800 ${size}px system-ui, sans-serif`;
ctx.fillStyle = index === 0 ? color : "#d8ebe4";
ctx.fillText(
row,
canvas.width / 2,
rows.length === 1 ? 128 : 82 + index * 94,
940
);
});
texture.needsUpdate = true;
}
set(initialLines);
return { sprite, set };
}
// Building shell and tiled floor.
const floorBase = box(31, 0.55, 21, 0xbfc9b9, 0, -0.38, 0);
floorBase.receiveShadow = true;
const floorMaterials = [
material(0xe9dfca),
material(0xe2d7c0)
];
for (let x = 0; x < 15; x++) {
for (let z = 0; z < 10; z++) {
const tile = box(
1.96, 0.09, 1.96,
floorMaterials[(x + z) % 2],
-14 + x * 2,
-0.035,
-9 + z * 2,
scene,
false
);
tile.receiveShadow = true;
}
}
box(30.4, 4.2, 0.35, 0xf3ecda, 0, 2.05, -9.85);
box(30.4, 0.78, 0.42, 0x5e9f94, 0, 0.45, -9.63);
box(0.35, 3.2, 20.4, 0xe9e0cb, -15.2, 1.55, 0);
box(0.35, 3.2, 20.4, 0xe9e0cb, 15.2, 1.55, 0);
// Back-wall windows.
for (const x of [-11.5, -6.8, 3.3, 8.1, 12.1]) {
box(2.8, 1.5, 0.12, 0x8cb7c3, x, 2.65, -9.63);
box(0.11, 1.55, 0.16, 0xf7f1df, x, 2.65, -9.52);
box(2.85, 0.11, 0.16, 0xf7f1df, x, 2.65, -9.52);
}
// Canteen tables and benches.
const tableXs = [-10, -5, 0, 5, 10];
for (const x of tableXs) {
box(2.25, 0.22, 1.45, 0xc98b54, x, 1.18, 4.65);
box(0.28, 1.1, 0.28, 0x55736b, x, 0.58, 4.65);
box(1.5, 0.15, 0.15, 0x55736b, x, 0.55, 4.65);
box(2.25, 0.25, 0.42, 0xd7a466, x, 0.62, 3.58);
box(2.25, 0.25, 0.42, 0xd7a466, x, 0.62, 5.72);
for (const px of [x - 0.55, x + 0.55]) {
box(0.28, 0.1, 0.28, 0xf7f1df, px, 1.34, 4.35);
box(0.28, 0.1, 0.28, 0xf7f1df, px, 1.34, 4.92);
}
}
// Voxel diners.
const people = [];
const personColors = [
0xef8b3d, 0x4b729d, 0x147d74, 0xc85d56,
0xd4a23b, 0x765f92, 0x4b8e67, 0xe1b34e
];
let personIndex = 0;
for (const x of tableXs) {
for (const [dx, dz] of [
[-0.62, -0.42], [0.62, -0.42],
[-0.62, 0.42], [0.62, 0.42]
]) {
const group = new THREE.Group();
group.position.set(x + dx, 0, 4.65 + dz);
scene.add(group);
const color = personColors[personIndex % personColors.length];
box(0.42, 0.68, 0.32, color, 0, 0.95, 0, group);
box(0.34, 0.34, 0.34, 0xdcae83, 0, 1.48, 0, group);
box(0.43, 0.18, 0.37, 0x314b48, 0, 1.69, 0, group);
box(0.16, 0.44, 0.18, 0x405b68, -0.12, 0.28, 0, group);
box(0.16, 0.44, 0.18, 0x405b68, 0.12, 0.28, 0, group);
group.userData.baseY = 0;
group.userData.phase = personIndex * 0.73;
people.push(group);
personIndex++;
}
}
// Voxel delivery truck and loading pallet.
const truck = new THREE.Group();
truck.position.set(-1.05, 0, -7.45);
scene.add(truck);
box(3.6, 1.65, 2.15, 0xf2eee1, -0.25, 1.48, 0, truck);
box(1.25, 1.75, 2.12, 0xef8b3d, 2.1, 1.48, 0, truck);
box(0.78, 0.63, 0.08, 0x7db1bf, 2.18, 1.93, 1.08, truck);
box(0.08, 0.63, 0.78, 0x7db1bf, 2.74, 1.93, 0, truck);
box(0.45, 0.18, 2.2, 0x536964, 2.1, 0.72, 0, truck);
box(3.8, 0.18, 2.2, 0x415b56, -0.25, 0.72, 0, truck);
for (const wx of [-1.2, 1.7]) {
for (const wz of [-1.08, 1.08]) {
box(0.72, 0.72, 0.28, 0x273b38, wx, 0.58, wz, truck);
box(0.34, 0.34, 0.30, 0x93a39d, wx, 0.58, wz, truck);
}
}
const beaconMaterial = material(0xf0b542, {
emissive: 0xf0a52d,
emissiveIntensity: 0.3
});
box(0.3, 0.25, 0.3, beaconMaterial, 2.0, 2.48, 0, truck);
const pallet = box(
1.25, 0.85, 1.0,
0xd99b54,
-1.05, 0.55, -5.35
);
const deliverySign = makeLabel(
["DELIVERY WINDOW", "08:00–11:00 · 13:00–16:00 · 18:00–20:00"],
{
width: 5.6,
background: "#173d3a",
accent: "#ef8b3d"
}
);
deliverySign.sprite.position.set(-1.05, 4.25, -8.1);
scene.add(deliverySign.sprite);
// Breakfast/lunch peak signs.
const breakfastSign = makeLabel(
["BREAKFAST PEAK", "08:30–10:00"],
{ width: 4.5, background: "#ef8b3d", color: "#173d3a" }
);
breakfastSign.sprite.position.set(-10.0, 4.0, 3.9);
scene.add(breakfastSign.sprite);
const lunchSign = makeLabel(
["LUNCH PEAK", "13:30–15:30"],
{ width: 4.5, background: "#4b729d", color: "#fffdf6" }
);
lunchSign.sprite.position.set(7.3, 4.0, 3.9);
scene.add(lunchSign.sprite);
function createPolicyStation(config, x, accentColor) {
const station = new THREE.Group();
scene.add(station);
// Service counter.
box(6.3, 1.55, 2.05, accentColor, x, 0.84, -2.75, station);
box(6.65, 0.22, 2.3, 0xf6efdc, x, 1.72, -2.75, station);
box(5.85, 0.13, 0.15, 0xf2b66d, x, 1.24, -1.69, station);
// Kitchen equipment behind the counter.
box(1.55, 1.35, 1.4, 0x8caaa0, x - 2.05, 0.72, -4.7, station);
box(1.28, 0.18, 1.12, 0x425a58, x - 2.05, 1.46, -4.7, station);
for (const px of [-2.45, -1.65]) {
box(0.38, 0.18, 0.42, 0x2e3d3b, px, 1.62, -4.7, station);
}
box(1.65, 1.6, 1.25, 0xaec2b7, x + 2.1, 0.84, -4.8, station);
box(1.9, 0.24, 1.45, 0x58716b, x + 2.1, 1.75, -4.8, station);
box(1.35, 0.75, 0.1, 0x7daeb9, x + 2.1, 1.08, -4.13, station);
// Pantry shelving.
const rackZ = -7.0;
const rackWidth = config.columns === 6 ? 7.4 : 6.4;
const rackHeight = config.rows === 5 ? 5.05 : 4.25;
box(rackWidth + 0.25, 0.2, 1.7, 0x49625d, x, 0.48, rackZ, station);
box(0.18, rackHeight, 1.7, 0x49625d, x - rackWidth / 2, rackHeight / 2 + 0.5, rackZ, station);
box(0.18, rackHeight, 1.7, 0x49625d, x + rackWidth / 2, rackHeight / 2 + 0.5, rackZ, station);
box(rackWidth, 0.18, 1.7, 0x49625d, x, rackHeight + 0.5, rackZ, station);
const shelfYs = [];
for (let row = 0; row < config.rows; row++) {
const y = 0.72 + row * (rackHeight - 0.35) / (config.rows - 1);
shelfYs.push(y);
box(rackWidth, 0.12, 1.7, 0x6f8378, x, y, rackZ, station);
}
const crateMeshes = [];
const crateWidth = (rackWidth - 0.8) / config.columns;
const crateHeight = config.rows === 5 ? 0.67 : 0.74;
for (let row = 0; row < config.rows; row++) {
for (let col = 0; col < config.columns; col++) {
const px = x - rackWidth / 2 + 0.4 + crateWidth * (col + 0.5);
const py = shelfYs[row] + crateHeight / 2 + 0.08;
const crate = box(
crateWidth * 0.78,
crateHeight,
1.08,
0x4e9b79,
px,
py,
rackZ + 0.08,
station
);
crate.userData.crateMaterial = crate.material;
crateMeshes.push(crate);
}
}
// Red reorder-threshold marker.
const thresholdY =
0.64 + (config.threshold / config.capacity) * (rackHeight - 0.2);
box(
rackWidth + 0.08, 0.08, 0.09,
0xd95d52,
x, thresholdY, rackZ + 0.89,
station
);
// Tray positions on the serving counter.
const trays = [];
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 6; col++) {
const tray = box(
0.67, 0.11, 0.46,
0xf0a257,
x - 2.55 + col * 1.02,
1.89,
-3.32 + row * 0.55,
station
);
trays.push(tray);
}
}
const pantryLabel = makeLabel(
[`${config.name.toUpperCase()} PANTRY`, `${config.initial} / ${config.capacity} UNITS`],
{
width: 4.7,
background: accentColor === 0xd95d52 ? "#713e39" : "#176b64",
accent: accentColor === 0xd95d52 ? "#ef8b3d" : "#9ed5c9"
}
);
pantryLabel.sprite.position.set(x, rackHeight + 1.28, rackZ + 0.45);
scene.add(pantryLabel.sprite);
const statusLabel = makeLabel(
[`${config.name.toUpperCase()} SERVICE`, "READY"],
{
width: 4.4,
background: "#173d3a",
accent: accentColor === 0xd95d52 ? "#ef8b3d" : "#6ec0b3"
}
);
statusLabel.sprite.position.set(x, 3.08, -1.65);
scene.add(statusLabel.sprite);
return {
config,
crateMeshes,
trays,
pantryLabel,
statusLabel,
thresholdY
};
}
const leanStation = createPolicyStation(POLICIES.lean, -7.55, 0xd95d52);
const plannedStation = createPolicyStation(POLICIES.planned, 5.25, 0x147d74);
// ------------------------------------------------------------------
// SVG timeline chart
// ------------------------------------------------------------------
const chart = $("chart");
const NS = "http://www.w3.org/2000/svg";
function svgElement(tag, attrs = {}) {
const element = document.createElementNS(NS, tag);
Object.entries(attrs).forEach(([key, value]) => {
element.setAttribute(key, value);
});
chart.appendChild(element);
return element;
}
const chartWidth = 820;
const chartHeight = 190;
const plot = {
left: 38,
top: 16,
width: 760,
height: 132,
max: 180
};
const chartX = index =>
plot.left + index / (SLOT_COUNT - 1) * plot.width;
const chartY = value =>
plot.top + plot.height - value / plot.max * plot.height;
// Delivery-window background bands.
for (const window of DELIVERY_WINDOWS) {
svgElement("rect", {
x: chartX(window.start) - 4,
y: plot.top,
width: chartX(window.end) - chartX(window.start) + 8,
height: plot.height,
fill: "#d5e6d7",
opacity: 0.72,
rx: 5
});
}
// Horizontal guides.
for (const value of [0, 60, 120, 180]) {
svgElement("line", {
x1: plot.left,
y1: chartY(value),
x2: plot.left + plot.width,
y2: chartY(value),
stroke: "#173d3a",
"stroke-opacity": 0.09,
"stroke-width": 1
});
const label = svgElement("text", {
x: 31,
y: chartY(value) + 3,
fill: "#71817d",
"font-size": 9,
"text-anchor": "end"
});
label.textContent = value;
}
// Demand bars.
DEMAND.forEach((demand, index) => {
const width = plot.width / SLOT_COUNT * 0.64;
const bar = svgElement("rect", {
x: chartX(index) - width / 2,
y: chartY(demand),
width,
height: chartY(0) - chartY(demand),
rx: 2,
fill: demand >= 50 ? "#ef8b3d" : "#e2bf91",
opacity: demand >= 50 ? 0.88 : 0.74
});
bar.style.transition = "opacity 0.15s ease";
});
function chartLine(states, color, width) {
const points = states.map((state, index) =>
`${chartX(index)},${chartY(state.stock)}`
).join(" ");
svgElement("polyline", {
points,
fill: "none",
stroke: color,
"stroke-width": width,
"stroke-linejoin": "round",
"stroke-linecap": "round"
});
}
chartLine(leanSim.states, "#d95d52", 2.2);
chartLine(plannedSim.states, "#147d74", 2.7);
// Time labels.
for (const hour of [6, 9, 12, 15, 18, 22]) {
const index = (hour - 6) * 2;
const label = svgElement("text", {
x: chartX(index),
y: 169,
fill: "#617571",
"font-size": 10,
"font-weight": 700,
"text-anchor": "middle"
});
label.textContent = `${String(hour).padStart(2, "0")}:00`;
}
const cursorLine = svgElement("line", {
x1: chartX(16),
y1: plot.top,
x2: chartX(16),
y2: plot.top + plot.height,
stroke: "#173d3a",
"stroke-width": 1.5,
"stroke-dasharray": "4 4"
});
const cursorDotLean = svgElement("circle", {
cx: chartX(16),
cy: chartY(leanSim.states[16].stock),
r: 4,
fill: "#d95d52",
stroke: "#fffdf6",
"stroke-width": 2
});
const cursorDotPlanned = svgElement("circle", {
cx: chartX(16),
cy: chartY(plannedSim.states[16].stock),
r: 4,
fill: "#147d74",
stroke: "#fffdf6",
"stroke-width": 2
});
// ------------------------------------------------------------------
// Timeline interaction and live scene state
// ------------------------------------------------------------------
let currentSlot = 16;
let playing = false;
let playAccumulator = 0;
let deliveryFlash = 0;
function updateStation(station, state, index) {
const { config } = station;
const visibleCrates = Math.ceil(state.stock / CRATE_SIZE);
let crateColor;
if (state.stock <= config.threshold) {
crateColor = new THREE.Color(0xd95d52);
} else if (state.stock <= config.threshold + 15) {
crateColor = new THREE.Color(0xe3a33e);
} else {
crateColor = new THREE.Color(
config.name === "Lean" ? 0x4e9b79 : 0x2f927f
);
}
station.crateMeshes.forEach((crate, crateIndex) => {
crate.visible = crateIndex < visibleCrates;
const color = crateColor.clone();
color.offsetHSL(0, 0, ((crateIndex % 3) - 1) * 0.035);
crate.material.color.copy(color);
});
station.pantryLabel.set([
`${config.name.toUpperCase()} PANTRY`,
`${state.stock} / ${config.capacity} UNITS`
]);
const visibleTrays = Math.min(
station.trays.length,
Math.ceil(state.demand / 4)
);
station.trays.forEach((tray, trayIndex) => {
tray.visible = trayIndex < visibleTrays;
const isShortageTray =
state.shortage > 0 &&
trayIndex >= Math.ceil(state.fulfilled / 4);
tray.material.color.set(
isShortageTray ? 0xd95d52 : 0xf0a257
);
});
if (state.pending) {
const arrival = state.pending.arrival;
const eta = arrival >= SLOT_COUNT
? "NEXT DAY"
: slotTime(arrival);
const delayMinutes = Math.max(0, (arrival - index) * 30);
station.statusLabel.set([
`${config.name.toUpperCase()} SERVICE`,
`REPLENISH +${state.pending.qty} · ${eta} · ${delayMinutes}m`
]);
} else if (state.arrival > 0) {
station.statusLabel.set([
`${config.name.toUpperCase()} SERVICE`,
`DELIVERY RECEIVED +${state.arrival}`
]);
} else {
station.statusLabel.set([
`${config.name.toUpperCase()} SERVICE`,
`${state.stock} UNITS READY`
]);
}
}
function updateScene() {
const leanState = leanSim.states[currentSlot];
const plannedState = plannedSim.states[currentSlot];
updateStation(leanStation, leanState, currentSlot);
updateStation(plannedStation, plannedState, currentSlot);
const activePeople = Math.min(
people.length,
Math.ceil(leanState.demand / 3.5)
);
people.forEach((person, index) => {
person.visible = index < activePeople;
});
const time = slotTime(currentSlot);
const window = deliveryWindowAt(currentSlot);
$("clockNow").textContent = time;
$("timeLabel").textContent = time;
$("demandNow").textContent = `${leanState.demand} units`;
$("stockNow").textContent =
`Lean ${leanState.stock} · Planned ${plannedState.stock}`;
$("windowNow").textContent = window
? window.name.split(" · ")[0]
: "Between windows";
$("timeline").value = currentSlot;
cursorLine.setAttribute("x1", chartX(currentSlot));
cursorLine.setAttribute("x2", chartX(currentSlot));
cursorDotLean.setAttribute("cx", chartX(currentSlot));
cursorDotLean.setAttribute("cy", chartY(leanState.stock));
cursorDotPlanned.setAttribute("cx", chartX(currentSlot));
cursorDotPlanned.setAttribute("cy", chartY(plannedState.stock));
if (leanState.arrival || plannedState.arrival) {
deliveryFlash = 1;
}
}
$("timeline").addEventListener("input", event => {
currentSlot = Number(event.target.value);
updateScene();
});
$("playButton").addEventListener("click", () => {
playing = !playing;
playAccumulator = 0;
$("playButton").textContent = playing ? "Ⅱ" : "▶";
$("playButton").setAttribute(
"aria-label",
playing ? "Pause simulation" : "Play simulation"
);
});
updateScene();
// ------------------------------------------------------------------
// Animation
// ------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (playing) {
playAccumulator += delta;
if (playAccumulator >= 1.15) {
playAccumulator -= 1.15;
currentSlot = (currentSlot + 1) % SLOT_COUNT;
updateScene();
}
}
people.forEach(person => {
if (!person.visible) return;
person.position.y =
person.userData.baseY +
Math.sin(elapsed * 2.1 + person.userData.phase) * 0.035;
});
deliveryFlash = Math.max(0, deliveryFlash - delta * 1.7);
beaconMaterial.emissiveIntensity = 0.25 + deliveryFlash * 2.2;
pallet.position.y = 0.55 + Math.sin(elapsed * 2.2) * 0.035;
pallet.rotation.y = Math.sin(elapsed * 0.45) * 0.035;
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```