Prompt specification
Build or customize the Three.js 3D scene "Warehouse Policy Comparison" 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: Warehouse Policy Comparison
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/warehouse-policy-comparison-x3156
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Build two otherwise identical miniature warehouses with different reorder thresholds. Run the same demand sequence and compare missing orders, average stock, and overflow without hidden random advantages.
Working Three.js Starter (warehouse-policy-comparison-x3156):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Deterministic Warehouse Replenishment Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07121b;
--panel: #0d1c28;
--panel-2: #112432;
--line: #203747;
--text: #e8f1f5;
--muted: #8da6b4;
--a: #52bfff;
--b: #ffb454;
--good: #54d6a5;
--bad: #ff6b6b;
--overflow: #b48cff;
}
* { box-sizing: border-box; }
body {
margin: 0;
min-height: 100vh;
background:
radial-gradient(circle at 50% 0%, #132b3b 0, transparent 45rem),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, select {
font: inherit;
}
button, select {
min-height: 36px;
border: 1px solid #30495a;
border-radius: 8px;
background: #142836;
color: var(--text);
padding: 7px 11px;
}
button {
cursor: pointer;
transition: background 120ms ease, border-color 120ms ease;
}
button:hover {
background: #1a3545;
border-color: #547286;
}
button.primary {
min-width: 82px;
background: #26759e;
border-color: #479ac1;
font-weight: 700;
}
button:focus-visible,
select:focus-visible {
outline: 2px solid var(--a);
outline-offset: 2px;
}
.app {
width: min(1500px, 100%);
margin: 0 auto;
padding: 14px;
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 4px 3px 13px;
}
.eyebrow {
color: #6cc8f2;
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1 {
margin: 4px 0 3px;
font-size: clamp(1.25rem, 2.1vw, 2rem);
line-height: 1.1;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 0.86rem;
}
.controls {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
align-items: center;
gap: 7px;
}
.controls label {
color: var(--muted);
font-size: 0.78rem;
}
#stage {
position: relative;
height: clamp(390px, 57vh, 660px);
overflow: hidden;
border: 1px solid var(--line);
border-radius: 14px;
background: #0a1721;
box-shadow: 0 18px 55px #0006;
}
#stage > canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#stage > canvas:active {
cursor: grabbing;
}
.stage-note {
position: absolute;
z-index: 2;
top: 13px;
left: 13px;
display: flex;
flex-wrap: wrap;
gap: 8px;
pointer-events: none;
}
.badge {
border: 1px solid #385064;
border-radius: 999px;
background: #0b1823dc;
padding: 7px 10px;
color: #c7d9e3;
font-size: 0.72rem;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.camera-help {
position: absolute;
z-index: 2;
right: 13px;
bottom: 12px;
padding: 7px 10px;
border: 1px solid #294253;
border-radius: 8px;
background: #09151ed9;
color: #91aab8;
font-size: 0.72rem;
pointer-events: none;
}
.hud {
position: absolute;
z-index: 2;
left: 13px;
bottom: 12px;
min-width: 245px;
padding: 11px 13px;
border: 1px solid #29485a;
border-radius: 10px;
background: #081722df;
pointer-events: none;
backdrop-filter: blur(8px);
}
.hud strong {
color: #f2c66d;
}
.hud span {
color: var(--muted);
}
.results-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin: 15px 2px 9px;
}
.results-heading h2 {
margin: 0;
font-size: 1rem;
}
.results-heading p {
margin: 0;
color: var(--muted);
font-size: 0.78rem;
}
.result-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
}
.warehouse-card {
position: relative;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 12px;
background: linear-gradient(145deg, #102330, #0c1b26);
padding: 14px;
}
.warehouse-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--a);
}
.warehouse-card.b::before {
background: var(--b);
}
.card-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
}
.card-title {
display: flex;
align-items: center;
gap: 8px;
font-weight: 850;
letter-spacing: 0.04em;
}
.warehouse-card.a .card-title { color: var(--a); }
.warehouse-card.b .card-title { color: var(--b); }
.policy {
color: var(--muted);
font-size: 0.76rem;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
}
.metric {
min-width: 0;
padding: 10px;
border: 1px solid #1e3544;
border-radius: 9px;
background: #0817229c;
}
.metric-label {
display: block;
color: var(--muted);
font-size: 0.69rem;
text-transform: uppercase;
letter-spacing: 0.06em;
}
.metric strong {
display: block;
margin-top: 3px;
font-size: clamp(1.25rem, 2.2vw, 1.8rem);
line-height: 1;
}
.metric small {
display: block;
min-height: 1.1em;
margin-top: 5px;
color: #8ca4b2;
font-size: 0.68rem;
}
.live-row {
display: flex;
flex-wrap: wrap;
gap: 5px 15px;
margin-top: 10px;
color: var(--muted);
font-size: 0.76rem;
}
.live-row b {
color: var(--text);
}
.comparison {
margin-top: 10px;
padding: 10px 12px;
border: 1px solid #284354;
border-radius: 9px;
background: #102431;
color: #c7d9e2;
font-size: 0.82rem;
line-height: 1.45;
}
.chart-panel {
margin-top: 12px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
}
.chart-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 4px;
}
.chart-header h2 {
margin: 0;
font-size: 0.9rem;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 11px;
color: var(--muted);
font-size: 0.7rem;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 8px;
height: 8px;
display: inline-block;
border-radius: 2px;
background: var(--bad);
}
.legend .b-miss { background: var(--b); }
.legend .overflow { background: var(--overflow); transform: rotate(45deg); }
#demandChart {
display: block;
width: 100%;
height: 150px;
}
details {
margin-top: 10px;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: #0b1a24;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.5;
}
summary {
cursor: pointer;
color: #c8dbe5;
font-weight: 750;
}
details p {
margin: 9px 0;
}
code {
display: block;
overflow-wrap: anywhere;
padding: 9px;
border-radius: 6px;
background: #07131b;
color: #a8d8ec;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.72rem;
}
@media (max-width: 800px) {
.topbar {
align-items: flex-start;
flex-direction: column;
}
.controls {
justify-content: flex-start;
}
.result-grid {
grid-template-columns: 1fr;
}
.results-heading {
align-items: flex-start;
flex-direction: column;
}
.camera-help {
display: none;
}
}
@media (max-width: 480px) {
.app { padding: 8px; }
#stage { height: 430px; }
.metrics {
grid-template-columns: 1fr;
}
.metric {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 2px 8px;
}
.metric strong,
.metric small {
grid-column: 2;
}
.metric-label {
grid-row: 1 / 3;
}
.hud {
min-width: 205px;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div>
<div class="eyebrow">Deterministic Replenishment Lab</div>
<h1>Two Warehouses, One Demand Sequence</h1>
<p class="subtitle">
Identical stock, capacity, order size, and lead time. Only the reorder threshold changes.
</p>
</div>
<div class="controls" aria-label="Simulation controls">
<button id="playBtn" class="primary">Pause</button>
<button id="stepBtn">Step</button>
<button id="resetBtn">Reset</button>
<button id="finishBtn">Finish run</button>
<label for="speedSelect">Speed</label>
<select id="speedSelect">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
<button id="viewBtn">Reset view</button>
</div>
</header>
<main id="stage" aria-label="Interactive 3D warehouse comparison">
<div class="stage-note">
<div class="badge">Shared sequence</div>
<div class="badge">Day <span id="currentDay">0</span> / 30</div>
<div class="badge">Demand <span id="currentDemand">—</span></div>
</div>
<div class="hud">
<strong id="hudStatus">Starting deterministic run</strong><br>
<span>Red diamonds are identical demand arrivals.</span>
</div>
<div class="camera-help">Drag to orbit · scroll to zoom</div>
</main>
<section aria-labelledby="resultsTitle">
<div class="results-heading">
<h2 id="resultsTitle">Full 30-day results</h2>
<p>Average stock is mean end-of-day on-hand inventory.</p>
</div>
<div class="result-grid">
<article class="warehouse-card a">
<div class="card-header">
<div class="card-title">WAREHOUSE A</div>
<div class="policy">Reorder threshold R = 16 units</div>
</div>
<div class="metrics">
<div class="metric">
<span class="metric-label">Missing orders</span>
<strong id="aMissing">—</strong>
<small id="aMissingUnits">—</small>
</div>
<div class="metric">
<span class="metric-label">Average stock</span>
<strong id="aAverage">—</strong>
<small>end-of-day units</small>
</div>
<div class="metric">
<span class="metric-label">Overflow</span>
<strong id="aOverflow">—</strong>
<small id="aFill">—</small>
</div>
</div>
<div class="live-row">
<span>Day <b id="aDay">0</b></span>
<span>Stock <b id="aStock">70</b></span>
<span>On order <b id="aOnOrder">0</b></span>
</div>
</article>
<article class="warehouse-card b">
<div class="card-header">
<div class="card-title">WAREHOUSE B</div>
<div class="policy">Reorder threshold R = 48 units</div>
</div>
<div class="metrics">
<div class="metric">
<span class="metric-label">Missing orders</span>
<strong id="bMissing">—</strong>
<small id="bMissingUnits">—</small>
</div>
<div class="metric">
<span class="metric-label">Average stock</span>
<strong id="bAverage">—</strong>
<small>end-of-day units</small>
</div>
<div class="metric">
<span class="metric-label">Overflow</span>
<strong id="bOverflow">—</strong>
<small id="bFill">—</small>
</div>
</div>
<div class="live-row">
<span>Day <b id="bDay">0</b></span>
<span>Stock <b id="bStock">70</b></span>
<span>On order <b id="bOnOrder">0</b></span>
</div>
</article>
</div>
<div id="comparison" class="comparison"></div>
</section>
<section class="chart-panel" aria-labelledby="chartTitle">
<div class="chart-header">
<h2 id="chartTitle">Common 30-day demand sequence</h2>
<div class="legend">
<span><i></i>A shortage</span>
<span><i class="b-miss"></i>B shortage</span>
<span><i class="overflow"></i>overflow</span>
</div>
</div>
<canvas id="demandChart" aria-label="Demand sequence chart"></canvas>
</section>
<details>
<summary>Audit the experiment</summary>
<p>
Both policies receive the exact same hard-coded demand array. The simulation contains
no random-number generator and no per-policy randomness.
</p>
<p>
Initial stock = 70 units; capacity = 100 units; replenishment lot = 70 units;
replenishment lead time = 2 days. After demand is served, an order is placed when
on-hand stock plus units already on order is at or below the warehouse's threshold.
The order arrives two days later. Inbound units exceeding the 100-unit capacity are
rejected and counted as overflow.
</p>
<p>
A “missing order” means one demand occurrence could not be completely served.
The number of missing units is also shown.
</p>
<code id="sequenceText"></code>
</details>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Deterministic experiment data
// -------------------------------------------------------------------------
const DEMAND = Object.freeze([
18, 8, 4, 30, 26, 22, 34, 18, 28, 20,
24, 32, 16, 30, 36, 21, 26, 34, 19, 28,
31, 17, 25, 33, 22, 29, 37, 18, 29, 32
]);
const PARAMS = Object.freeze({
initialStock: 70,
capacity: 100,
lotSize: 70,
leadDays: 2
});
const POLICIES = Object.freeze({
A: Object.freeze({ label: "A", threshold: 16, accent: 0x52bfff }),
B: Object.freeze({ label: "B", threshold: 48, accent: 0xffb454 })
});
const TOTAL_DEMAND = DEMAND.reduce((sum, value) => sum + value, 0);
const LAST_DAY = DEMAND.length - 1;
const DAY_SECONDS = 0.8;
function simulate(threshold) {
let stock = PARAMS.initialStock;
let pipeline = [];
let missingOrders = 0;
let missingUnits = 0;
let overflowUnits = 0;
let ordersPlaced = 0;
let stockSum = 0;
const records = [];
for (let day = 0; day < DEMAND.length; day++) {
const arriving = pipeline.filter(order => order.arrivalDay === day);
let arrivedUnits = 0;
let overflowToday = 0;
for (const order of arriving) {
const accepted = Math.min(
order.quantity,
Math.max(0, PARAMS.capacity - stock)
);
overflowToday += order.quantity - accepted;
overflowUnits += order.quantity - accepted;
stock += accepted;
arrivedUnits += order.quantity;
}
pipeline = pipeline.filter(order => order.arrivalDay > day);
const demand = DEMAND[day];
const served = Math.min(stock, demand);
const missingToday = demand - served;
stock -= served;
if (missingToday > 0) {
missingOrders++;
missingUnits += missingToday;
}
const onOrderBeforeDecision = pipeline.reduce(
(sum, order) => sum + order.quantity,
0
);
const inventoryPosition = stock + onOrderBeforeDecision;
let ordered = 0;
if (inventoryPosition <= threshold) {
pipeline.push({
quantity: PARAMS.lotSize,
arrivalDay: day + PARAMS.leadDays
});
ordered = PARAMS.lotSize;
ordersPlaced++;
}
const onOrderUnits = pipeline.reduce(
(sum, order) => sum + order.quantity,
0
);
records.push({
day: day + 1,
demand,
served,
missingUnits: missingToday,
stock,
inventoryPosition,
ordered,
arrivedUnits,
overflowUnits: overflowToday,
onOrderUnits,
openOrders: pipeline.length
});
stockSum += stock;
}
return {
records,
summary: {
missingOrders,
missingUnits,
overflowUnits,
ordersPlaced,
averageStock: stockSum / DEMAND.length,
fillRate: 100 * (1 - missingUnits / TOTAL_DEMAND)
}
};
}
const results = {
A: simulate(POLICIES.A.threshold),
B: simulate(POLICIES.B.threshold)
};
// -------------------------------------------------------------------------
// UI and result summaries
// -------------------------------------------------------------------------
const $ = id => document.getElementById(id);
$("sequenceText").textContent =
"[" + DEMAND.join(", ") + "]";
$("aMissing").textContent = results.A.summary.missingOrders;
$("aMissingUnits").textContent =
results.A.summary.missingUnits + " units short";
$("aAverage").textContent =
results.A.summary.averageStock.toFixed(1) + " units";
$("aOverflow").textContent =
results.A.summary.overflowUnits + " units";
$("aFill").textContent =
results.A.summary.fillRate.toFixed(1) + "% fill rate";
$("bMissing").textContent = results.B.summary.missingOrders;
$("bMissingUnits").textContent =
results.B.summary.missingUnits + " units short";
$("bAverage").textContent =
results.B.summary.averageStock.toFixed(1) + " units";
$("bOverflow").textContent =
results.B.summary.overflowUnits + " units";
$("bFill").textContent =
results.B.summary.fillRate.toFixed(1) + "% fill rate";
const averageDifference = (
results.B.summary.averageStock -
results.A.summary.averageStock
).toFixed(1);
const fewerMissing = (
results.A.summary.missingOrders -
results.B.summary.missingOrders
);
$("comparison").textContent =
`Trade-off: Warehouse B has ${fewerMissing} fewer missed-demand occasions and ` +
`serves ${(results.B.summary.fillRate - results.A.summary.fillRate).toFixed(1)} ` +
`percentage points more demand, but it holds ${averageDifference} more units on average ` +
`and rejects ${results.B.summary.overflowUnits} inbound units because of capacity overflow. ` +
`Warehouse A avoids overflow but carries less safety stock.`;
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const stage = $("stage");
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.domElement.setAttribute(
"aria-label",
"Orbitable 3D view of two low-poly warehouses"
);
stage.insertBefore(renderer.domElement, stage.firstChild);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a1721);
scene.fog = new THREE.Fog(0x0a1721, 20, 38);
const camera = new THREE.PerspectiveCamera(
38,
1,
0.1,
100
);
const cameraState = {
yaw: 0.34,
pitch: 0.48,
radius: 18,
target: new THREE.Vector3(0, 1.25, 0)
};
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.cos(pitch) * Math.sin(yaw),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xb9ddff,
0x172028,
1.8
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d6, 3.1);
sun.position.set(-7, 14, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x5fb9ff, 1.1);
rim.position.set(10, 7, -10);
scene.add(rim);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.04,
flatShading: true,
...options
});
}
const materials = {
ground: standardMaterial(0x101f29),
platform: standardMaterial(0x263b49),
floor: standardMaterial(0x324956),
wall: standardMaterial(0x405764),
beam: standardMaterial(0x74818a),
shelf: standardMaterial(0x6c5a45),
crate: standardMaterial(0xc69357),
dark: standardMaterial(0x101b23),
roller: standardMaterial(0x637681),
delivery: standardMaterial(0x55d6a2),
order: standardMaterial(0xffc45b),
demand: standardMaterial(0xff6961),
marker: standardMaterial(0xffd166)
};
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 20),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.31;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(28, 28, 0x2a4656, 0x1b3441);
grid.position.y = -0.295;
grid.material.transparent = true;
grid.material.opacity = 0.35;
scene.add(grid);
const centerLane = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.025, 17),
materials.marker
);
centerLane.position.set(0, -0.285, 0);
scene.add(centerLane);
function box(parent, width, height, depth, x, y, z, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width, y + height,
x + width - radius, y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function hex(color) {
return "#" + color.toString(16).padStart(6, "0");
}
function makeWarehouseSign(label, threshold, accent) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 1008, 240, 28);
ctx.fillStyle = "rgba(8, 20, 28, 0.94)";
ctx.fill();
ctx.lineWidth = 8;
ctx.strokeStyle = hex(accent);
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = "#eef6fa";
ctx.font = "800 53px system-ui, sans-serif";
ctx.fillText("WAREHOUSE " + label, 512, 92);
ctx.fillStyle = hex(accent);
ctx.font = "800 69px system-ui, sans-serif";
ctx.fillText("R = " + threshold, 512, 181);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(4.25, 1.06, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeEventLabel(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 8, 8, 496, 112, 20);
ctx.fillStyle = "rgba(8, 19, 27, 0.94)";
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = hex(color);
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = hex(color);
ctx.font = "900 55px system-ui, sans-serif";
ctx.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.75, 0.44, 1);
sprite.renderOrder = 21;
return sprite;
}
function createWarehouse(policy, xPosition) {
const group = new THREE.Group();
group.position.x = xPosition;
scene.add(group);
// Identical platform and cutaway shell.
box(group, 6.6, 0.30, 4.6, 0, -0.10, 0, materials.platform);
box(group, 6.35, 0.12, 4.35, 0, 0.11, 0, materials.floor);
box(group, 6.35, 2.85, 0.15, 0, 1.60, -2.05, materials.wall);
box(group, 0.15, 2.85, 4.1, -3.10, 1.60, 0, materials.wall);
// Frame.
for (const x of [-3.0, 3.0]) {
for (const z of [-1.9, 1.9]) {
box(group, 0.14, 3.2, 0.14, x, 1.78, z, materials.beam);
}
}
box(group, 6.15, 0.14, 0.14, 0, 3.34, -1.9, materials.beam);
box(group, 6.15, 0.14, 0.14, 0, 3.34, 1.9, materials.beam);
box(group, 0.14, 0.14, 3.9, -3.0, 3.34, 0, materials.beam);
box(group, 0.14, 0.14, 3.9, 3.0, 3.34, 0, materials.beam);
// Four storage levels. Five crate slots per level = twenty crates.
const shelfLevels = [0.72, 1.38, 2.04, 2.70];
const crateX = [-2.25, -1.125, 0, 1.125, 2.25];
const crates = [];
for (const y of shelfLevels) {
box(group, 5.5, 0.10, 0.90, 0, y, -0.55, materials.shelf);
}
for (const x of [-2.75, 2.75]) {
box(group, 0.12, 2.8, 0.12, x, 1.70, -0.95, materials.beam);
}
for (let level = 0; level < shelfLevels.length; level++) {
for (let slot = 0; slot < crateX.length; slot++) {
const crate = box(
group,
0.82,
0.46,
0.68,
crateX[slot],
shelfLevels[level] + 0.30,
-0.55,
materials.crate
);
crate.visible = false;
crates.push({
mesh: crate,
shelfY: shelfLevels[level]
});
}
}
// Inbound conveyor and low-poly rollers.
box(group, 2.7, 0.14, 0.60, 0, 0.42, 1.48, materials.dark);
for (let i = 0; i < 6; i++) {
const roller = new THREE.Mesh(
new THREE.CylinderGeometry(0.10, 0.10, 0.64, 8),
materials.roller
);
roller.rotation.x = Math.PI / 2;
roller.position.set(-1.12 + i * 0.45, 0.51, 1.48);
roller.castShadow = true;
group.add(roller);
}
const deliveryMarker = box(
group, 0.52, 0.52, 0.52,
0.35, 0.78, 1.48,
materials.delivery
);
deliveryMarker.visible = false;
const orderMarker = box(
group, 0.42, 0.42, 0.42,
-0.55, 0.75, 1.48,
materials.order
);
orderMarker.visible = false;
// Stock gauge.
box(group, 0.64, 3.02, 0.12, 2.83, 1.90, 1.52, materials.dark);
const fillMaterial = standardMaterial(0x54d6a5);
const fill = box(
group, 0.39, 1.0, 0.14,
2.83, 1.7, 1.61,
fillMaterial
);
fill.castShadow = false;
const thresholdY = 0.42 + 2.7 * (policy.threshold / PARAMS.capacity);
box(
group, 0.78, 0.075, 0.17,
2.83, thresholdY, 1.69,
materials.marker
);
// Event markers.
const demandMarker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.30, 0),
materials.demand
);
demandMarker.position.set(0, 4.32, 0.75);
demandMarker.castShadow = true;
demandMarker.visible = false;
group.add(demandMarker);
const missLabel = makeEventLabel("MISSED", 0xff6b6b);
missLabel.position.set(-2.15, 3.24, 0.80);
missLabel.visible = false;
group.add(missLabel);
const overflowLabel = makeEventLabel("OVERFLOW", 0xb48cff);
overflowLabel.position.set(2.15, 3.24, 0.80);
overflowLabel.visible = false;
group.add(overflowLabel);
const sign = makeWarehouseSign(
policy.label,
policy.threshold,
policy.accent
);
sign.position.set(0, 3.87, -0.05);
group.add(sign);
return {
group,
crates,
fill,
fillMaterial,
demandMarker,
deliveryMarker,
orderMarker,
missLabel,
overflowLabel,
stock: PARAMS.initialStock
};
}
const warehouseA = createWarehouse(POLICIES.A, -4.0);
const warehouseB = createWarehouse(POLICIES.B, 4.0);
function updateWarehouseVisual(warehouse, record) {
const stock = record ? record.stock : PARAMS.initialStock;
warehouse.stock = stock;
const fullCrates = Math.floor(stock / 5);
const remainder = stock % 5;
warehouse.crates.forEach((crate, index) => {
let scale = 0;
if (index < fullCrates) {
scale = 1;
} else if (index === fullCrates && remainder > 0) {
scale = remainder / 5;
}
crate.mesh.visible = scale > 0;
crate.mesh.scale.set(1, Math.max(scale, 0.001), 1);
crate.mesh.position.y =
crate.shelfY + 0.07 + 0.23 * Math.max(scale, 0.001);
});
const ratio = THREE.MathUtils.clamp(
stock / PARAMS.capacity,
0,
1
);
const fillHeight = 2.7 * ratio;
warehouse.fill.visible = ratio > 0;
warehouse.fill.scale.y = Math.max(fillHeight, 0.001);
warehouse.fill.position.y = 0.42 + fillHeight / 2;
warehouse.fillMaterial.color.setHex(
record && record.missingUnits > 0 ? 0xef6a67 : 0x54d6a5
);
warehouse.deliveryMarker.visible = Boolean(
record && record.arrivedUnits > 0
);
warehouse.orderMarker.visible = Boolean(
record && record.onOrderUnits > 0
);
warehouse.missLabel.visible = Boolean(
record && record.missingUnits > 0
);
warehouse.overflowLabel.visible = Boolean(
record && record.overflowUnits > 0
);
}
// -------------------------------------------------------------------------
// Playback
// -------------------------------------------------------------------------
let currentDay = -1;
let playing = true;
let playbackClock = 0;
let dayAge = 0;
function updateLiveUI() {
const hasRecord = currentDay >= 0;
$("currentDay").textContent = hasRecord ? currentDay + 1 : 0;
$("currentDemand").textContent = hasRecord
? DEMAND[currentDay]
: "—";
$("hudStatus").textContent = !hasRecord
? "Starting deterministic run"
: currentDay === LAST_DAY
? "Run complete — same demand, different threshold"
: `Day ${currentDay + 1}: both warehouses receive ${DEMAND[currentDay]} units`;
for (const key of ["A", "B"]) {
const record = hasRecord ? results[key].records[currentDay] : null;
const prefix = key.toLowerCase();
$(prefix + "Day").textContent = hasRecord ? currentDay + 1 : 0;
$(prefix + "Stock").textContent = record
? record.stock
: PARAMS.initialStock;
$(prefix + "OnOrder").textContent = record
? record.onOrderUnits
: 0;
}
}
function setDay(dayIndex) {
currentDay = THREE.MathUtils.clamp(dayIndex, -1, LAST_DAY);
dayAge = 0;
if (currentDay < 0) {
updateWarehouseVisual(warehouseA, null);
updateWarehouseVisual(warehouseB, null);
} else {
updateWarehouseVisual(
warehouseA,
results.A.records[currentDay]
);
updateWarehouseVisual(
warehouseB,
results.B.records[currentDay]
);
}
updateLiveUI();
drawDemandChart();
if (currentDay === LAST_DAY) {
playing = false;
updatePlayButton();
}
}
function updatePlayButton() {
$("playBtn").textContent = playing ? "Pause" : "Play";
}
$("playBtn").addEventListener("click", () => {
if (currentDay === LAST_DAY) {
setDay(0);
playing = true;
} else {
playing = !playing;
}
updatePlayButton();
});
$("stepBtn").addEventListener("click", () => {
playing = false;
updatePlayButton();
setDay(currentDay + 1);
});
$("resetBtn").addEventListener("click", () => {
playbackClock = 0;
setDay(-1);
playing = true;
updatePlayButton();
});
$("finishBtn").addEventListener("click", () => {
playing = false;
updatePlayButton();
setDay(LAST_DAY);
});
$("viewBtn").addEventListener("click", () => {
cameraState.yaw = 0.34;
cameraState.pitch = 0.48;
cameraState.radius = 18;
cameraState.target.set(0, 1.25, 0);
updateCamera();
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit and zoom, without Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
0.18,
1.12
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
10,
28
);
updateCamera();
}, { passive: false });
// -------------------------------------------------------------------------
// Demand chart
// -------------------------------------------------------------------------
const chartCanvas = $("demandChart");
const chartContext = chartCanvas.getContext("2d");
function drawDemandChart() {
const rect = chartCanvas.getBoundingClientRect();
if (!rect.width || !rect.height) return;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
chartCanvas.width = Math.round(rect.width * dpr);
chartCanvas.height = Math.round(rect.height * dpr);
chartContext.setTransform(dpr, 0, 0, dpr, 0, 0);
const width = rect.width;
const height = rect.height;
const ctx = chartContext;
ctx.clearRect(0, 0, width, height);
const left = 34;
const right = 10;
const top = 15;
const bottom = 52;
const plotWidth = width - left - right;
const plotHeight = height - top - bottom;
const maxDemand = 40;
const step = plotWidth / DEMAND.length;
ctx.strokeStyle = "#294252";
ctx.lineWidth = 1;
for (const value of [0, 10, 20, 30, 40]) {
const y = top + plotHeight * (1 - value / maxDemand);
ctx.beginPath();
ctx.moveTo(left, y);
ctx.lineTo(width - right, y);
ctx.stroke();
ctx.fillStyle = "#68818f";
ctx.font = "10px system-ui, sans-serif";
ctx.textAlign = "right";
ctx.fillText(String(value), left - 7, y + 3);
}
DEMAND.forEach((demand, index) => {
const x = left + step * index + step / 2;
const barHeight = plotHeight * demand / maxDemand;
let color = "#284453";
if (index === currentDay) color = "#ffc96b";
else if (index < currentDay) color = "#497286";
ctx.fillStyle = color;
ctx.fillRect(
x - step * 0.28,
top + plotHeight - barHeight,
Math.max(2, step * 0.56),
barHeight
);
if (index % 5 === 0 || index === LAST_DAY) {
ctx.fillStyle = "#78909d";
ctx.font = "10px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText(String(index + 1), x, height - 6);
}
});
// A shortage markers.
results.A.records.forEach((record, index) => {
if (record.missingUnits <= 0) return;
const x = left + step * index + step / 2;
const y = top + plotHeight + 9;
ctx.fillStyle = "#ff6b6b";
ctx.fillRect(x - 3.5, y - 3.5, 7, 7);
});
// B shortage markers.
results.B.records.forEach((record, index) => {
if (record.missingUnits <= 0) return;
const x = left + step * index + step / 2;
const y = top + plotHeight + 20;
ctx.fillStyle = "#ffb454";
ctx.beginPath();
ctx.moveTo(x, y - 4);
ctx.lineTo(x + 4, y + 3);
ctx.lineTo(x - 4, y + 3);
ctx.closePath();
ctx.fill();
});
// Overflow markers.
for (const key of ["A", "B"]) {
results[key].records.forEach((record, index) => {
if (record.overflowUnits <= 0) return;
const x = left + step * index + step / 2;
const y = top + plotHeight + 32;
ctx.fillStyle = "#b48cff";
ctx.save();
ctx.translate(x, y);
ctx.rotate(Math.PI / 4);
ctx.fillRect(-3.5, -3.5, 7, 7);
ctx.restore();
});
}
if (currentDay >= 0) {
const x = left + step * currentDay + step / 2;
ctx.strokeStyle = "#f4d38a";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x, top - 3);
ctx.lineTo(x, top + plotHeight + 38);
ctx.stroke();
const demand = DEMAND[currentDay];
ctx.fillStyle = "#ffe0a0";
ctx.font = "bold 11px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText(
String(demand),
THREE.MathUtils.clamp(x, left + 12, width - right - 12),
11
);
}
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
drawDemandChart();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resizeObserver.observe(chartCanvas.parentElement);
function animateWarehouseEvents(warehouse, elapsed) {
const demandVisible = currentDay >= 0 && elapsed < 1.25;
warehouse.demandMarker.visible = demandVisible;
if (demandVisible) {
const travel = Math.min(elapsed / 0.9, 1);
warehouse.demandMarker.position.y = 4.35 - travel * 1.45;
warehouse.demandMarker.rotation.x = elapsed * 4;
warehouse.demandMarker.rotation.y = elapsed * 5;
const pulse = 1 + Math.sin(elapsed * 11) * 0.12;
warehouse.demandMarker.scale.setScalar(pulse);
}
if (warehouse.deliveryMarker.visible) {
const scale = 1 + Math.max(0, 1 - elapsed / 1.1) * 0.7;
warehouse.deliveryMarker.scale.setScalar(scale);
warehouse.deliveryMarker.rotation.y = elapsed * 3;
}
if (warehouse.orderMarker.visible) {
warehouse.orderMarker.position.y =
0.75 + Math.sin(elapsed * 4) * 0.035;
}
if (warehouse.missLabel.visible) {
warehouse.missLabel.material.opacity = THREE.MathUtils.clamp(
1.8 - elapsed,
0,
1
);
warehouse.missLabel.position.y = 3.24 + elapsed * 0.08;
}
if (warehouse.overflowLabel.visible) {
warehouse.overflowLabel.material.opacity = THREE.MathUtils.clamp(
1.8 - elapsed,
0,
1
);
warehouse.overflowLabel.position.y = 3.24 + elapsed * 0.08;
}
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
dayAge += delta;
if (playing && currentDay < LAST_DAY) {
playbackClock += delta * Number($("speedSelect").value);
while (
playbackClock >= DAY_SECONDS &&
currentDay < LAST_DAY
) {
playbackClock -= DAY_SECONDS;
setDay(currentDay + 1);
}
}
animateWarehouseEvents(warehouseA, dayAge);
animateWarehouseEvents(warehouseB, dayAge);
renderer.render(scene, camera);
}
setDay(-1);
updatePlayButton();
requestAnimationFrame(resize);
animate();
</script>
</body>
</html>
```