Prompt specification
Build or customize the Three.js 3D scene "Delivery Window Scheduling Puzzle" 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: Delivery Window Scheduling Puzzle
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/delivery-window-scheduling-puzzle-x2160
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this low-poly scene: Create a bounded planning game assigning deliveries to time windows and bays. Validate capacity and walking clearances, showing conflicts rather than treating every schedule as feasible.
Working Three.js Starter (delivery-window-scheduling-puzzle-x2160):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Dockyard — Delivery Planning Game</title>
<style>
:root {
color-scheme: dark;
--bg: #091117;
--panel: #111c24;
--panel-2: #16242e;
--panel-3: #1c2d38;
--line: #263b48;
--text: #edf5f7;
--muted: #8fa5b1;
--teal: #5bd6c4;
--teal-dark: #174f4b;
--orange: #ffac57;
--red: #ff6b6b;
--blue: #78a9ff;
--radius: 12px;
--shadow: 0 16px 45px rgba(0, 0, 0, .28);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: 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 {
color: inherit;
}
button:focus-visible,
select:focus-visible {
outline: 2px solid var(--teal);
outline-offset: 2px;
}
#app {
display: grid;
grid-template-rows: 70px minmax(0, 1fr);
width: 100%;
height: 100%;
}
header {
z-index: 5;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 0 22px;
border-bottom: 1px solid var(--line);
background: rgba(12, 21, 28, .96);
box-shadow: 0 5px 22px rgba(0, 0, 0, .2);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 230px;
}
.brand-mark {
position: relative;
width: 36px;
height: 36px;
border: 2px solid var(--teal);
border-radius: 9px;
transform: rotate(-7deg);
}
.brand-mark::before,
.brand-mark::after {
position: absolute;
content: "";
background: var(--teal);
border-radius: 2px;
}
.brand-mark::before {
width: 20px;
height: 5px;
left: 6px;
top: 8px;
}
.brand-mark::after {
width: 5px;
height: 17px;
left: 14px;
top: 13px;
}
.brand h1 {
margin: 0;
font-size: 17px;
letter-spacing: .05em;
}
.brand p {
margin: 2px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: .13em;
text-transform: uppercase;
}
.metrics {
display: flex;
align-items: stretch;
justify-content: center;
gap: 8px;
}
.metric {
min-width: 112px;
padding: 8px 14px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel);
}
.metric span {
display: block;
color: var(--muted);
font-size: 10px;
letter-spacing: .09em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 2px;
font-size: 17px;
font-variant-numeric: tabular-nums;
}
.metric.danger strong {
color: var(--red);
}
.metric.success strong {
color: var(--teal);
}
.header-actions {
display: flex;
justify-content: flex-end;
min-width: 230px;
}
.ghost-button,
.secondary-button,
.primary-button {
border-radius: 8px;
cursor: pointer;
transition:
background .15s ease,
border-color .15s ease,
transform .15s ease;
}
.ghost-button {
padding: 9px 12px;
border: 1px solid var(--line);
background: transparent;
color: var(--muted);
font-size: 12px;
}
.ghost-button:hover {
color: var(--text);
border-color: #49606e;
background: var(--panel-2);
}
main {
display: grid;
grid-template-columns: 282px minmax(440px, 1fr) 320px;
min-height: 0;
}
aside {
min-height: 0;
overflow: auto;
background: var(--panel);
scrollbar-width: thin;
scrollbar-color: #38505e transparent;
}
.left-panel {
border-right: 1px solid var(--line);
}
.right-panel {
border-left: 1px solid var(--line);
}
.section {
padding: 17px;
border-bottom: 1px solid var(--line);
}
.section:last-child {
border-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.section-heading h2 {
margin: 0;
font-size: 12px;
letter-spacing: .1em;
text-transform: uppercase;
}
.section-heading small {
color: var(--muted);
font-size: 11px;
}
.objective {
position: relative;
overflow: hidden;
padding: 15px;
border: 1px solid #29574f;
border-radius: 10px;
background:
linear-gradient(135deg, rgba(91, 214, 196, .11), transparent 70%),
#10262a;
}
.objective::after {
position: absolute;
right: -19px;
bottom: -30px;
width: 88px;
height: 88px;
content: "";
border: 16px solid rgba(91, 214, 196, .07);
border-radius: 50%;
}
.objective strong {
display: block;
margin-bottom: 5px;
color: var(--teal);
font-size: 13px;
}
.objective p {
margin: 0;
color: #a9bdc3;
font-size: 12px;
line-height: 1.45;
}
.progress-track {
height: 5px;
margin-top: 14px;
overflow: hidden;
border-radius: 99px;
background: #233940;
}
.progress-track > div {
width: 50%;
height: 100%;
border-radius: inherit;
background: var(--teal);
transition: width .25s ease;
}
.rules {
display: grid;
gap: 8px;
margin: 0;
padding: 0;
list-style: none;
}
.rules li {
display: grid;
grid-template-columns: 18px 1fr;
gap: 8px;
color: var(--muted);
font-size: 12px;
line-height: 1.35;
}
.rules b {
color: var(--text);
}
.rule-icon {
display: grid;
width: 17px;
height: 17px;
place-items: center;
border-radius: 5px;
background: var(--panel-3);
color: var(--teal);
font-size: 10px;
}
.delivery-list {
display: grid;
gap: 8px;
}
.delivery-card {
position: relative;
width: 100%;
padding: 12px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel-2);
text-align: left;
cursor: pointer;
transition:
border-color .15s ease,
background .15s ease,
transform .15s ease;
}
.delivery-card::before {
position: absolute;
inset: 0 auto 0 0;
width: 3px;
content: "";
background: var(--cargo-color, var(--teal));
}
.delivery-card:hover {
transform: translateX(2px);
border-color: #486170;
background: #1a2b35;
}
.delivery-card.selected {
border-color: var(--teal);
background: #173039;
box-shadow: inset 0 0 0 1px rgba(91, 214, 196, .1);
}
.delivery-card.conflicted {
border-color: rgba(255, 107, 107, .5);
}
.card-top,
.card-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.card-top strong {
font-size: 12px;
letter-spacing: .03em;
}
.status {
padding: 3px 6px;
border-radius: 5px;
background: rgba(91, 214, 196, .12);
color: var(--teal);
font-size: 9px;
font-weight: 800;
letter-spacing: .06em;
text-transform: uppercase;
}
.status.error {
background: rgba(255, 107, 107, .13);
color: #ff9494;
}
.card-name {
margin: 6px 0 9px;
color: #b8c9d0;
font-size: 11px;
}
.card-bottom {
color: var(--muted);
font-size: 10px;
}
.card-bottom b {
color: #dce9ed;
font-weight: 600;
}
.stage {
position: relative;
display: grid;
grid-template-rows: minmax(0, 1fr) 112px;
min-width: 0;
min-height: 0;
background: #0b141a;
}
#scene-wrap {
position: relative;
min-height: 0;
overflow: hidden;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.scene-topbar {
position: absolute;
z-index: 2;
top: 15px;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.scene-label {
padding: 10px 12px;
border: 1px solid rgba(79, 104, 117, .45);
border-radius: 9px;
background: rgba(10, 20, 26, .83);
backdrop-filter: blur(8px);
}
.scene-label strong {
display: block;
font-size: 12px;
}
.scene-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: .04em;
}
.camera-tools {
display: flex;
gap: 5px;
pointer-events: auto;
}
.camera-tools button {
width: 34px;
height: 34px;
padding: 0;
border: 1px solid rgba(79, 104, 117, .5);
border-radius: 8px;
background: rgba(10, 20, 26, .88);
cursor: pointer;
}
.camera-tools button:hover {
border-color: var(--teal);
color: var(--teal);
}
.legend {
position: absolute;
z-index: 2;
left: 16px;
bottom: 15px;
display: flex;
gap: 12px;
padding: 8px 10px;
border: 1px solid rgba(79, 104, 117, .4);
border-radius: 8px;
background: rgba(10, 20, 26, .8);
color: var(--muted);
font-size: 10px;
backdrop-filter: blur(8px);
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 2px;
}
.timeline-panel {
position: relative;
z-index: 3;
padding: 12px 16px 14px;
border-top: 1px solid var(--line);
background: #101b22;
}
.timeline-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.timeline-heading strong {
font-size: 11px;
letter-spacing: .1em;
text-transform: uppercase;
}
.timeline-heading span {
color: var(--muted);
font-size: 10px;
}
.timeline {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
.slot-button {
position: relative;
min-width: 0;
padding: 9px 10px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel-2);
text-align: left;
cursor: pointer;
}
.slot-button::after {
position: absolute;
inset: auto 0 0;
height: 3px;
content: "";
background: var(--teal);
transform: scaleX(0);
transform-origin: left;
transition: transform .2s ease;
}
.slot-button.active {
border-color: var(--teal);
background: #173039;
}
.slot-button.active::after {
transform: scaleX(1);
}
.slot-button.has-error {
border-color: rgba(255, 107, 107, .5);
}
.slot-button strong,
.slot-button small {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.slot-button strong {
font-size: 11px;
}
.slot-button small {
margin-top: 4px;
color: var(--muted);
font-size: 9px;
}
.slot-button .slot-dot {
position: absolute;
top: 10px;
right: 9px;
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--teal);
}
.slot-button.has-error .slot-dot {
background: var(--red);
}
.field {
display: grid;
gap: 6px;
margin-bottom: 12px;
}
.field label {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: .08em;
text-transform: uppercase;
}
select {
width: 100%;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 8px;
background: #0d171d;
color: var(--text);
cursor: pointer;
}
.delivery-summary {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin: 10px 0 14px;
}
.summary-cell {
padding: 9px;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel-2);
}
.summary-cell span {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: .06em;
text-transform: uppercase;
}
.summary-cell strong {
display: block;
margin-top: 3px;
font-size: 12px;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.primary-button,
.secondary-button {
min-height: 39px;
padding: 9px 11px;
border: 1px solid transparent;
font-size: 11px;
font-weight: 800;
letter-spacing: .035em;
}
.primary-button {
border-color: var(--teal);
background: var(--teal);
color: #09221f;
}
.primary-button:hover {
background: #78eadb;
transform: translateY(-1px);
}
.secondary-button {
border-color: #36505d;
background: var(--panel-3);
color: #c5d5db;
}
.secondary-button:hover {
border-color: var(--teal);
color: var(--teal);
}
.preview {
display: grid;
gap: 6px;
margin-top: 14px;
}
.check-row {
display: grid;
grid-template-columns: 16px 1fr auto;
gap: 7px;
align-items: center;
padding: 7px 8px;
border-radius: 7px;
background: var(--panel-2);
color: #b7c7ce;
font-size: 10px;
}
.check-icon {
display: grid;
width: 15px;
height: 15px;
place-items: center;
border-radius: 50%;
background: rgba(91, 214, 196, .13);
color: var(--teal);
font-size: 9px;
font-weight: 900;
}
.check-row.fail {
color: #ffb0b0;
}
.check-row.fail .check-icon {
background: rgba(255, 107, 107, .14);
color: var(--red);
}
.check-row b {
color: inherit;
font-size: 10px;
}
.conflict-list {
display: grid;
gap: 8px;
}
.conflict-card {
width: 100%;
padding: 10px;
border: 1px solid rgba(255, 107, 107, .32);
border-left: 3px solid var(--red);
border-radius: 8px;
background: rgba(255, 107, 107, .055);
text-align: left;
cursor: pointer;
}
.conflict-card:hover {
background: rgba(255, 107, 107, .1);
}
.conflict-card.warning {
border-color: rgba(255, 172, 87, .4);
border-left-color: var(--orange);
background: rgba(255, 172, 87, .06);
}
.conflict-card strong {
display: block;
font-size: 11px;
}
.conflict-card span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.clear-state {
padding: 15px;
border: 1px solid rgba(91, 214, 196, .35);
border-radius: 9px;
background: rgba(91, 214, 196, .08);
color: var(--teal);
text-align: center;
}
.clear-state strong {
display: block;
font-size: 13px;
}
.clear-state span {
display: block;
margin-top: 4px;
color: #a9c9c6;
font-size: 10px;
}
.hint {
margin: 8px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
bottom: 28px;
max-width: min(430px, calc(100vw - 30px));
padding: 11px 15px;
border: 1px solid var(--line);
border-radius: 9px;
background: #1d303b;
box-shadow: var(--shadow);
color: var(--text);
font-size: 12px;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 14px);
transition:
opacity .2s ease,
transform .2s ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
border-color: rgba(255, 107, 107, .55);
}
@media (max-width: 1100px) {
main {
grid-template-columns: 250px minmax(380px, 1fr) 285px;
}
.metric {
min-width: 90px;
}
.brand,
.header-actions {
min-width: 190px;
}
}
@media (max-width: 860px) {
html,
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100%;
grid-template-rows: auto auto;
}
header {
min-height: 70px;
flex-wrap: wrap;
padding: 12px;
}
.metrics {
order: 3;
width: 100%;
}
.metric {
flex: 1;
}
main {
grid-template-columns: 1fr;
}
.stage {
grid-row: 1;
height: 66vh;
min-height: 520px;
}
aside {
overflow: visible;
}
.left-panel {
border-right: 0;
}
.right-panel {
border-left: 0;
}
.delivery-list {
grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>DOCKYARD</h1>
<p>Constrained delivery planning</p>
</div>
</div>
<div class="metrics" aria-label="Schedule metrics">
<div class="metric danger" id="metric-conflicts">
<span>Hard conflicts</span>
<strong id="conflict-count">0</strong>
</div>
<div class="metric success" id="metric-ready">
<span>Ready orders</span>
<strong id="ready-count">0 / 6</strong>
</div>
<div class="metric">
<span>Slot load</span>
<strong id="slot-load">0%</strong>
</div>
</div>
<div class="header-actions">
<button class="ghost-button" id="reset-button" type="button">
Reset scenario
</button>
</div>
</header>
<main>
<aside class="left-panel">
<section class="section">
<div class="objective">
<strong>Objective: certify the shift</strong>
<p>
Move every delivery into a legal time window and bay.
A green schedule means no time, capacity, or walking-clearance conflicts.
</p>
<div class="progress-track"><div id="objective-progress"></div></div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Planning rules</h2>
<small>Hard constraints</small>
</div>
<ul class="rules">
<li>
<span class="rule-icon">T</span>
<span><b>Time:</b> the selected slot must fall inside the delivery window.</span>
</li>
<li>
<span class="rule-icon">C</span>
<span><b>Capacity:</b> pallet count and weight cannot exceed the bay limit.</span>
</li>
<li>
<span class="rule-icon">W</span>
<span><b>Walking:</b> cargo footprint must fit the bay's clear aisle width.</span>
</li>
</ul>
</section>
<section class="section">
<div class="section-heading">
<h2>Deliveries</h2>
<small id="delivery-count">6 orders</small>
</div>
<div class="delivery-list" id="delivery-list"></div>
</section>
</aside>
<section class="stage">
<div id="scene-wrap">
<canvas id="scene" aria-label="Interactive 3D dockyard planning scene"></canvas>
<div class="scene-topbar">
<div class="scene-label">
<strong id="scene-slot-title">10:00 – 11:00</strong>
<span>6 bounded bays · live occupancy preview</span>
</div>
<div class="camera-tools" aria-label="Camera controls">
<button id="camera-zoom-in" type="button" title="Zoom in">+</button>
<button id="camera-zoom-out" type="button" title="Zoom out">−</button>
<button id="camera-home" type="button" title="Reset camera">⌂</button>
</div>
</div>
<div class="legend">
<span><i style="background:#5bd6c4"></i> Valid</span>
<span><i style="background:#ffac57"></i> Time conflict</span>
<span><i style="background:#ff6b6b"></i> Capacity / clearance</span>
</div>
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<strong>Shift timeline</strong>
<span>Select a slot to inspect the yard</span>
</div>
<div class="timeline" id="timeline"></div>
</div>
</section>
<aside class="right-panel">
<section class="section">
<div class="section-heading">
<h2>Assignment inspector</h2>
<small>Manual planning</small>
</div>
<div class="field">
<label for="delivery-select">Delivery</label>
<select id="delivery-select"></select>
</div>
<div class="delivery-summary" id="delivery-summary"></div>
<div class="field">
<label for="slot-select">Arrival slot</label>
<select id="slot-select"></select>
</div>
<div class="field">
<label for="bay-select">Loading bay</label>
<select id="bay-select"></select>
</div>
<div class="button-row">
<button class="primary-button" id="assign-button" type="button">
Assign delivery
</button>
<button class="secondary-button" id="suggest-button" type="button">
Find legal slot
</button>
</div>
<div class="preview" id="preview"></div>
<p class="hint">
Tip: click a bay in the 3D view to select it. “Find legal slot”
checks all available windows and bays against the current schedule.
</p>
</section>
<section class="section">
<div class="section-heading">
<h2>Conflict monitor</h2>
<small id="conflict-summary">0 issues</small>
</div>
<div class="conflict-list" id="conflict-list"></div>
</section>
</aside>
</main>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Game data
// ---------------------------------------------------------------------
const SLOTS = [
{ id: "S1", label: "08:00", range: "08:00 – 09:00" },
{ id: "S2", label: "09:00", range: "09:00 – 10:00" },
{ id: "S3", label: "10:00", range: "10:00 – 11:00" },
{ id: "S4", label: "11:00", range: "11:00 – 12:00" }
];
const BAYS = [
{
id: "B1",
name: "Bay 01",
x: -25,
capPallets: 12,
capKg: 4800,
clearance: 2.4
},
{
id: "B2",
name: "Bay 02",
x: -15,
capPallets: 14,
capKg: 5600,
clearance: 2.4
},
{
id: "B3",
name: "Bay 03",
x: -5,
capPallets: 16,
capKg: 6500,
clearance: 3.6
},
{
id: "B4",
name: "Bay 04",
x: 5,
capPallets: 8,
capKg: 3200,
clearance: 2.8
},
{
id: "B5",
name: "Bay 05",
x: 15,
capPallets: 14,
capKg: 6000,
clearance: 2.2
},
{
id: "B6",
name: "Bay 06",
x: 25,
capPallets: 12,
capKg: 4800,
clearance: 3.2
}
];
const DELIVERIES = [
{
id: "D-101",
name: "Fresh produce",
customer: "North Market Co.",
pallets: 4,
kg: 1200,
footprint: 2.2,
windows: [0, 1],
color: "#62d6a5",
initial: { slot: 0, bay: "B1" }
},
{
id: "D-102",
name: "Retail fixtures",
customer: "Harbor Home Stores",
pallets: 6,
kg: 2100,
footprint: 2.4,
windows: [1, 2],
color: "#78a9ff",
initial: { slot: 1, bay: "B4" }
},
{
id: "D-103",
name: "Beverage cases",
customer: "Union Grocers",
pallets: 5,
kg: 1600,
footprint: 2.3,
windows: [0, 1, 2, 3],
color: "#c58cff",
initial: { slot: 0, bay: "B2" }
},
{
id: "D-104",
name: "Steel coils",
customer: "West Fabrication",
pallets: 8,
kg: 5200,
footprint: 3.5,
windows: [1, 2, 3],
color: "#ff9d55",
initial: { slot: 2, bay: "B5" }
},
{
id: "D-105",
name: "Office supplies",
customer: "Civic Offices",
pallets: 4,
kg: 900,
footprint: 2.1,
windows: [0],
color: "#e3d36f",
initial: { slot: 1, bay: "B1" }
},
{
id: "D-106",
name: "Machine housings",
customer: "Atlas Robotics",
pallets: 7,
kg: 6100,
footprint: 2.4,
windows: [1, 2],
color: "#ef6f91",
initial: { slot: 2, bay: "B2" }
}
];
let assignments = {};
let selectedDelivery = "D-104";
let selectedSlot = 2;
let editSlot = 2;
let editBay = "B5";
let validation = null;
let toastTimer = null;
const byId = (id) => BAYS.find((bay) => bay.id === id);
const deliveryById = (id) => DELIVERIES.find((delivery) => delivery.id === id);
function resetAssignments() {
assignments = Object.fromEntries(
DELIVERIES.map((delivery) => [
delivery.id,
{ ...delivery.initial }
])
);
selectedDelivery = "D-104";
selectedSlot = 2;
editSlot = 2;
editBay = "B5";
}
resetAssignments();
// ---------------------------------------------------------------------
// Validation — all constraints are hard failures
// ---------------------------------------------------------------------
function buildBucket(slot, bayId, ignoreId = null) {
const bucket = {
pallets: 0,
kg: 0,
ids: []
};
for (const delivery of DELIVERIES) {
const assignment = assignments[delivery.id];
if (
delivery.id === ignoreId ||
assignment.slot !== slot ||
assignment.bay !== bayId
) {
continue;
}
bucket.pallets += delivery.pallets;
bucket.kg += delivery.kg;
bucket.ids.push(delivery.id);
}
return bucket;
}
function validateCandidate(delivery, slot, bayId, ignoreId = delivery.id) {
const issues = [];
const bay = byId(bayId);
if (!delivery.windows.includes(slot)) {
issues.push({
type: "time",
title: "Outside delivery window",
detail:
`${delivery.id} may arrive only during ` +
delivery.windows.map((index) => SLOTS[index].label).join(", ") +
`.`
});
}
if (delivery.footprint > bay.clearance) {
issues.push({
type: "clearance",
title: "Walking clearance blocked",
detail:
`${delivery.id} needs ${delivery.footprint.toFixed(1)} m of aisle, ` +
`but ${bay.name} provides ${bay.clearance.toFixed(1)} m.`
});
}
const bucket = buildBucket(slot, bayId, ignoreId);
const projectedPallets = bucket.pallets + delivery.pallets;
const projectedKg = bucket.kg + delivery.kg;
if (
projectedPallets > bay.capPallets ||
projectedKg > bay.capKg
) {
issues.push({
type: "capacity",
title: "Bay capacity exceeded",
detail:
`${bay.name} would hold ${projectedPallets}/${bay.capPallets} pallets ` +
`and ${projectedKg.toLocaleString()}/${bay.capKg.toLocaleString()} kg.`
});
}
return issues;
}
function validateSchedule() {
const issues = [];
const byIdMap = {};
const buckets = new Map();
for (const delivery of DELIVERIES) {
const assignment = assignments[delivery.id];
const bay = byId(assignment.bay);
const key = `${assignment.slot}:${assignment.bay}`;
if (!buckets.has(key)) {
buckets.set(key, {
slot: assignment.slot,
bay: assignment.bay,
pallets: 0,
kg: 0,
ids: []
});
}
const bucket = buckets.get(key);
bucket.pallets += delivery.pallets;
bucket.kg += delivery.kg;
bucket.ids.push(delivery.id);
}
for (const delivery of DELIVERIES) {
const assignment = assignments[delivery.id];
const bay = byId(assignment.bay);
const deliveryIssues = [];
if (!delivery.windows.includes(assignment.slot)) {
deliveryIssues.push({
id: delivery.id,
type: "time",
title: "Outside delivery window",
detail:
`${delivery.id} is assigned to ${SLOTS[assignment.slot].range}, ` +
`outside its allowed window.`,
slot: assignment.slot,
bay: assignment.bay
});
}
if (delivery.footprint > bay.clearance) {
deliveryIssues.push({
id: delivery.id,
type: "clearance",
title: "Walking clearance conflict",
detail:
`${delivery.id} needs ${delivery.footprint.toFixed(1)} m; ` +
`${bay.name} has ${bay.clearance.toFixed(1)} m clear.`,
slot: assignment.slot,
bay: assignment.bay
});
}
const bucket = buckets.get(`${assignment.slot}:${assignment.bay}`);
if (
bucket.pallets > bay.capPallets ||
bucket.kg > bay.capKg
) {
deliveryIssues.push({
id: delivery.id,
type: "capacity",
title: "Bay capacity conflict",
detail:
`${bay.name} at ${SLOTS[assignment.slot].label} holds ` +
`${bucket.pallets}/${bay.capPallets} pallets and ` +
`${bucket.kg.toLocaleString()}/${bay.capKg.toLocaleString()} kg.`,
slot: assignment.slot,
bay: assignment.bay
});
}
byIdMap[delivery.id] = deliveryIssues;
issues.push(...deliveryIssues);
}
return {
issues,
byId: byIdMap,
buckets
};
}
// ---------------------------------------------------------------------
// DOM rendering
// ---------------------------------------------------------------------
const deliveryList = document.querySelector("#delivery-list");
const deliverySelect = document.querySelector("#delivery-select");
const slotSelect = document.querySelector("#slot-select");
const baySelect = document.querySelector("#bay-select");
const timeline = document.querySelector("#timeline");
const conflictList = document.querySelector("#conflict-list");
deliverySelect.innerHTML = DELIVERIES.map(
(delivery) =>
`<option value="${delivery.id}">${delivery.id} — ${delivery.name}</option>`
).join("");
slotSelect.innerHTML = SLOTS.map(
(slot, index) =>
`<option value="${index}">${slot.range}</option>`
).join("");
baySelect.innerHTML = BAYS.map(
(bay) =>
`<option value="${bay.id}">${bay.name} · ${bay.capPallets} pl / ` +
`${bay.capKg.toLocaleString()} kg / ${bay.clearance.toFixed(1)} m clear</option>`
).join("");
function renderDeliveries() {
deliveryList.innerHTML = DELIVERIES.map((delivery) => {
const assignment = assignments[delivery.id];
const deliveryIssues = validation.byId[delivery.id];
const conflictTypes = [...new Set(deliveryIssues.map((issue) => issue.type))];
let status = "Ready";
if (conflictTypes.includes("capacity")) status = "Capacity";
else if (conflictTypes.includes("clearance")) status = "Clearance";
else if (conflictTypes.includes("time")) status = "Time window";
return `
<button
class="delivery-card ${selectedDelivery === delivery.id ? "selected" : ""}
${deliveryIssues.length ? "conflicted" : ""}"
style="--cargo-color:${delivery.color}"
data-delivery="${delivery.id}"
type="button"
>
<div class="card-top">
<strong>${delivery.id}</strong>
<span class="status ${deliveryIssues.length ? "error" : ""}">${status}</span>
</div>
<div class="card-name">${delivery.name} · ${delivery.customer}</div>
<div class="card-bottom">
<span><b>${SLOTS[assignment.slot].label}</b> / ${assignment.bay}</span>
<span>${delivery.pallets} pl · ${delivery.kg.toLocaleString()} kg</span>
</div>
</button>
`;
}).join("");
deliveryList.querySelectorAll("[data-delivery]").forEach((button) => {
button.addEventListener("click", () => {
selectDelivery(button.dataset.delivery);
});
});
}
function renderTimeline() {
timeline.innerHTML = SLOTS.map((slot, slotIndex) => {
const orders = DELIVERIES.filter(
(delivery) => assignments[delivery.id].slot === slotIndex
);
const pallets = orders.reduce((sum, delivery) => sum + delivery.pallets, 0);
const errors = validation.issues.filter(
(issue) => issue.slot === slotIndex
).length;
return `
<button
class="slot-button ${selectedSlot === slotIndex ? "active" : ""}
${errors ? "has-error" : ""}"
data-slot="${slotIndex}"
type="button"
>
<strong>${slot.label} – ${String(Number(slot.label.slice(0, 2)) + 1).padStart(2, "0")}:00</strong>
<small>${orders.length} orders · ${pallets} pallets</small>
<i class="slot-dot"></i>
</button>
`;
}).join("");
timeline.querySelectorAll("[data-slot]").forEach((button) => {
button.addEventListener("click", () => {
selectedSlot = Number(button.dataset.slot);
editSlot = selectedSlot;
renderAll();
});
});
}
function renderInspector() {
const delivery = deliveryById(selectedDelivery);
const assignment = assignments[selectedDelivery];
deliverySelect.value = selectedDelivery;
slotSelect.value = String(editSlot);
baySelect.value = editBay;
document.querySelector("#delivery-summary").innerHTML = `
<div class="summary-cell">
<span>Pallets</span><strong>${delivery.pallets}</strong>
</div>
<div class="summary-cell">
<span>Weight</span><strong>${delivery.kg.toLocaleString()} kg</strong>
</div>
<div class="summary-cell">
<span>Footprint</span><strong>${delivery.footprint.toFixed(1)} m</strong>
</div>
<div class="summary-cell">
<span>Allowed</span><strong>${delivery.windows.map((i) => SLOTS[i].label).join(" / ")}</strong>
</div>
`;
const bay = byId(editBay);
const bucket = buildBucket(editSlot, editBay, selectedDelivery);
const projectedPallets = bucket.pallets + delivery.pallets;
const projectedKg = bucket.kg + delivery.kg;
const previewIssues = validateCandidate(
delivery,
editSlot,
editBay,
selectedDelivery
);
const checks = [
{
pass: delivery.windows.includes(editSlot),
label: "Time window",
value: delivery.windows.includes(editSlot)
? "Inside window"
: "Outside window"
},
{
pass: delivery.footprint <= bay.clearance,
label: "Walking clearance",
value:
`${delivery.footprint.toFixed(1)} m / ` +
`${bay.clearance.toFixed(1)} m`
},
{
pass:
projectedPallets <= bay.capPallets &&
projectedKg <= bay.capKg,
label: "Bay capacity",
value:
`${projectedPallets}/${bay.capPallets} pl · ` +
`${projectedKg.toLocaleString()}/${bay.capKg.toLocaleString()} kg`
}
];
document.querySelector("#preview").innerHTML = checks.map(
(check) => `
<div class="check-row ${check.pass ? "" : "fail"}">
<span class="check-icon">${check.pass ? "✓" : "!"}</span>
<span>${check.label}</span>
<b>${check.value}</b>
</div>
`
).join("");
const assignmentHint = document.querySelector("#assign-button");
assignmentHint.textContent = previewIssues.length
? "Assign anyway — conflict"
: "Assign delivery";
}
function renderConflicts() {
document.querySelector("#conflict-summary").textContent =
`${validation.issues.length} issue${validation.issues.length === 1 ? "" : "s"}`;
if (!validation.issues.length) {
conflictList.innerHTML = `
<div class="clear-state">
<strong>Schedule certified</strong>
<span>All six deliveries satisfy every hard constraint.</span>
</div>
`;
return;
}
conflictList.innerHTML = validation.issues.map((issue, index) => `
<button
class="conflict-card ${issue.type === "time" ? "warning" : ""}"
data-conflict="${index}"
type="button"
>
<strong>${issue.title} · ${issue.id}</strong>
<span>${issue.detail}</span>
</button>
`).join("");
conflictList.querySelectorAll("[data-conflict]").forEach((button) => {
button.addEventListener("click", () => {
const issue = validation.issues[Number(button.dataset.conflict)];
selectedDelivery = issue.id;
selectedSlot = issue.slot;
editSlot = issue.slot;
editBay = issue.bay;
renderAll();
});
});
}
function renderMetrics() {
const ready = DELIVERIES.filter(
(delivery) => !validation.byId[delivery.id].length
).length;
document.querySelector("#conflict-count").textContent =
validation.issues.length;
document.querySelector("#ready-count").textContent =
`${ready} / ${DELIVERIES.length}`;
document.querySelector("#objective-progress").style.width =
`${(ready / DELIVERIES.length) * 100}%`;
const selectedOrders = DELIVERIES.filter(
(delivery) => assignments[delivery.id].slot === selectedSlot
);
const pallets = selectedOrders.reduce(
(sum, delivery) => sum + delivery.pallets,
0
);
const slotCapacity = BAYS.reduce(
(sum, bay) => sum + bay.capPallets,
0
);
document.querySelector("#slot-load").textContent =
`${Math.round((pallets / slotCapacity) * 100)}%`;
document.querySelector("#scene-slot-title").textContent =
SLOTS[selectedSlot].range;
}
function selectDelivery(id) {
selectedDelivery = id;
const assignment = assignments[id];
selectedSlot = assignment.slot;
editSlot = assignment.slot;
editBay = assignment.bay;
renderAll();
}
function showToast(message, error = false) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.toggle("error", error);
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2800);
}
// ---------------------------------------------------------------------
// Three.js scene
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0c171d);
scene.fog = new THREE.Fog(0x0c171d, 72, 125);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
180
);
const target = new THREE.Vector3(0, 0, -3);
const controls = {
theta: 0.55,
phi: 0.83,
radius: 68
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
target.x + controls.radius * sinPhi * Math.sin(controls.theta),
target.y + controls.radius * Math.cos(controls.phi),
target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xaedfff, 0x24352f, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe4bd, 3.1);
sun.position.set(-26, 45, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
sun.shadow.camera.far = 110;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x65bfff, 1.15);
rim.position.set(32, 18, -28);
scene.add(rim);
const dynamicGroup = new THREE.Group();
scene.add(dynamicGroup);
const animatedWorkers = [];
const animatedForklifts = [];
const beacons = [];
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.78,
metalness: options.metalness ?? 0.05,
flatShading: options.flatShading ?? true,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
side: options.side ?? THREE.FrontSide,
depthWrite: options.depthWrite ?? true
});
}
function addBox(
parent,
size,
position,
color,
options = {}
) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(
geometry,
material(color, options)
);
mesh.position.set(...position);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
color,
segments = 10
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material(color)
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextTexture(text, bg = "#14252e", fg = "#eaf5f5") {
const textCanvas = document.createElement("canvas");
textCanvas.width = 768;
textCanvas.height = 160;
const context = textCanvas.getContext("2d");
context.clearRect(0, 0, textCanvas.width, textCanvas.height);
context.fillStyle = bg;
context.beginPath();
if (context.roundRect) {
context.roundRect(4, 4, 760, 152, 18);
} else {
context.rect(4, 4, 760, 152);
}
context.fill();
context.fillStyle = fg;
context.font = "700 54px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 384, 82, 720);
const texture = new THREE.CanvasTexture(textCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
function addTextPanel(
parent,
text,
width,
height,
position,
bg = "#14252e",
fg = "#eaf5f5"
) {
const texture = makeTextTexture(text, bg, fg);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
side: THREE.DoubleSide,
depthWrite: false
})
);
mesh.position.set(...position);
parent.add(mesh);
return mesh;
}
function createWorker(color) {
const group = new THREE.Group();
addBox(group, [0.45, 0.72, 0.28], [0, 1.15, 0], color);
addCylinder(group, 0.19, 0.22, 0.34, [0, 1.73, 0], 0xe5b78d, 8);
addCylinder(
group,
0.24,
0.24,
0.12,
[0, 1.92, 0],
0xffcf45,
8
);
addBox(group, [0.14, 0.7, 0.15], [-0.13, 0.42, 0], 0x26313a);
addBox(group, [0.14, 0.7, 0.15], [0.13, 0.42, 0], 0x26313a);
return group;
}
function createForklift() {
const group = new THREE.Group();
addBox(group, [1.65, 0.55, 2.05], [0, 0.58, 0], 0xf2a637);
addBox(group, [1.2, 0.9, 0.8], [0, 1.25, -0.35], 0x35434b);
addBox(group, [0.85, 0.7, 0.6], [0, 1.35, 0.55], 0xffc55a);
addBox(group, [0.12, 2.3, 0.12], [-0.55, 1.55, -0.95], 0x27343a);
addBox(group, [0.12, 2.3, 0.12], [0.55, 1.55, -0.95], 0x27343a);
addBox(group, [1.3, 0.12, 0.12], [0, 0.32, -1.35], 0x27343a);
addBox(group, [0.16, 0.12, 1.15], [-0.42, 0.28, -1.68], 0x27343a);
addBox(group, [0.16, 0.12, 1.15], [0.42, 0.28, -1.68], 0x27343a);
for (const x of [-0.78, 0.78]) {
for (const z of [-0.65, 0.68]) {
const wheel = addCylinder(
group,
0.35,
0.35,
0.24,
[x, 0.36, z],
0x182027,
10
);
wheel.rotation.z = Math.PI / 2;
}
}
return group;
}
function buildSite() {
const site = new THREE.Group();
scene.add(site);
// Site slab and bounded apron.
addBox(
site,
[68, 0.35, 48],
[0, -0.25, 0],
0x26383e,
{ receiveShadow: true }
);
const grid = new THREE.GridHelper(64, 32, 0x45616a, 0x344a52);
grid.position.y = -0.055;
grid.material.transparent = true;
grid.material.opacity = 0.28;
site.add(grid);
// Warehouse shell.
addBox(site, [68, 8.8, 1.2], [0, 4.25, -22.2], 0x35474f);
addBox(site, [69, 0.35, 3.2], [0, 8.65, -21.1], 0x25353d);
addBox(site, [68, 0.35, 0.6], [0, 0.65, -20.8], 0x26373e);
addTextPanel(
site,
"DOCKYARD / WEST LOGISTICS",
19,
2.2,
[0, 7.1, -21.5],
"#1d3038",
"#c7e7e5"
);
// Truck approach lane.
addBox(
site,
[63, 0.035, 7.3],
[0, 0.01, -6.2],
0x303f43,
{ castShadow: false }
);
for (let x = -29; x <= 29; x += 6) {
addBox(
site,
[3.2, 0.025, 0.12],
[x, 0.045, -6.2],
0x839397,
{ castShadow: false }
);
}
// Pedestrian corridor.
addBox(
site,
[63, 0.055, 3.8],
[0, 0.035, 5.4],
0x214d4c,
{ castShadow: false }
);
for (let x = -30; x <= 30; x += 4) {
addBox(
site,
[1.7, 0.025, 0.13],
[x, 0.08, 3.55],
0x6fc5b8,
{ castShadow: false }
);
addBox(
site,
[1.7, 0.025, 0.13],
[x, 0.08, 7.25],
0x6fc5b8,
{ castShadow: false }
);
}
const walkwayLabel = addTextPanel(
site,
"PEDESTRIAN // KEEP CLEAR",
15,
1.45,
[0, 0.095, 5.45],
"#214d4c",
"#9eddd3"
);
walkwayLabel.rotation.x = -Math.PI / 2;
// Fence around the bounded yard.
const fenceColor = 0x61747b;
for (const x of [-32, 32]) {
for (let z = -20; z <= 20; z += 5) {
addBox(site, [0.13, 2.5, 0.13], [x, 1.25, z], fenceColor);
}
addBox(site, [0.1, 0.1, 41], [x, 2.25, 0], fenceColor);
addBox(site, [0.1, 0.1, 41], [x, 1.1, 0], fenceColor);
}
for (let x = -30; x <= 30; x += 5) {
if (Math.abs(x) < 7) continue;
addBox(site, [0.13, 2.5, 0.13], [x, 1.25, 20.5], fenceColor);
}
addBox(site, [24, 0.1, 0.1], [-19, 2.25, 20.5], fenceColor);
addBox(site, [24, 0.1, 0.1], [19, 2.25, 20.5], fenceColor);
addBox(site, [24, 0.1, 0.1], [-19, 1.1, 20.5], fenceColor);
addBox(site, [24, 0.1, 0.1], [19, 1.1, 20.5], fenceColor);
// Gate.
addBox(site, [0.5, 6.5, 0.5], [-6.5, 3.25, 20.5], 0x27363d);
addBox(site, [0.5, 6.5, 0.5], [6.5, 3.25, 20.5], 0x27363d);
addBox(site, [13, 0.6, 0.6], [0, 6.45, 20.5], 0xffb24b);
// Six bays.
for (const bay of BAYS) {
const platform = addBox(
site,
[7.8, 0.18, 8.5],
[bay.x, 0.03, -16.15],
0x3a4b50
);
platform.userData.bayId = bay.id;
const door = addBox(
site,
[5.7, 5.1, 0.28],
[bay.x, 2.7, -21.48],
0x1d2c32
);
door.userData.bayId = bay.id;
addBox(site, [0.28, 5.5, 0.55], [bay.x - 3.15, 2.8, -21.1], 0x6b7a7e);
addBox(site, [0.28, 5.5, 0.55], [bay.x + 3.15, 2.8, -21.1], 0x6b7a7e);
addBox(site, [6.6, 0.28, 0.55], [bay.x, 5.45, -21.1], 0x6b7a7e);
addBox(site, [7.9, 0.18, 0.9], [bay.x, 5.75, -20.85], 0x27363d);
addTextPanel(
site,
`${bay.id.replace("B", "BAY ")}`,
4.3,
0.9,
[bay.x, 6.15, -21.02],
"#15242b",
"#d9f3ef"
);
for (let stripe = -3; stripe <= 3; stripe += 1.5) {
addBox(
site,
[0.12, 0.025, 5.7],
[bay.x + stripe, 0.14, -16.2],
0x536269,
{ castShadow: false }
);
}
// Bollards near the pedestrian-facing edge.
for (const side of [-1, 1]) {
addCylinder(
site,
0.16,
0.2,
1.1,
[bay.x + side * 3.4, 0.55, -11.6],
bay.id === "B5" ? 0xffad58 : 0xe6b94f,
8
);
}
}
// Light poles.
for (const x of [-29, 29]) {
for (const z of [-2, 13]) {
addCylinder(site, 0.12, 0.18, 8, [x, 4, z], 0x52636a, 8);
addBox(site, [1.6, 0.18, 0.7], [x, 8, z], 0x677980);
addBox(
site,
[1.25, 0.08, 0.5],
[x, 7.86, z],
0xffe7a2,
{ castShadow: false }
);
}
}
// Pedestrians.
const workerColors = [0xffb54f, 0x69b9e8, 0x8ad6b0];
for (let index = 0; index < 3; index++) {
const worker = createWorker(workerColors[index]);
worker.position.set(-20 + index * 15, 0.1, 5.2 + (index % 2) * 0.8);
worker.userData.baseZ = worker.position.z;
worker.userData.speed = 1.1 + index * 0.16;
worker.userData.offset = index * 17;
site.add(worker);
animatedWorkers.push(worker);
}
// Forklifts stay inside the loading side of the pedestrian corridor.
for (let index = 0; index < 2; index++) {
const forklift = createForklift();
forklift.position.set(-13 + index * 23, 0, -2.5);
forklift.userData.offset = index * 12;
site.add(forklift);
animatedForklifts.push(forklift);
}
}
buildSite();
function createTruck(color) {
const group = new THREE.Group();
addBox(group, [2.6, 1.65, 2.55], [-1.15, 1.35, 0], color);
addBox(group, [2.45, 2.7, 2.55], [1.35, 1.85, 0], color);
addBox(group, [2.25, 0.72, 0.06], [1.35, 2.35, 1.3], 0x9cc6cf);
addBox(group, [0.06, 0.72, 1.9], [2.59, 2.35, 0], 0x9cc6cf);
addBox(group, [4.2, 0.18, 2.2], [1.25, 0.52, 0], 0x27343a);
for (const x of [-2, 0, 2]) {
for (const z of [-1.2, 1.2]) {
const wheel = addCylinder(
group,
0.43,
0.43,
0.27,
[x, 0.5, z],
0x172128,
10
);
wheel.rotation.z = Math.PI / 2;
}
}
return group;
}
function createCargo(delivery, bay, orderIndex) {
const group = new THREE.Group();
group.userData.bayId = bay.id;
const visiblePallets = Math.min(delivery.pallets, 10);
const columns = Math.min(4, visiblePallets);
const rows = Math.ceil(visiblePallets / columns);
const size = delivery.pallets > 7 ? 1.0 : 1.15;
for (let index = 0; index < visiblePallets; index++) {
const column = index % columns;
const row = Math.floor(index / columns);
const x = bay.x + (column - (columns - 1) / 2) * 1.35;
const z = -16.4 + row * 1.25 + orderIndex * 0.16;
addBox(
group,
[size, 0.16, size],
[x, 0.23, z],
0x9d7349
);
addBox(
group,
[size * 0.9, 0.78, size * 0.9],
[x, 0.7, z],
delivery.color
);
}
if (delivery.pallets > visiblePallets) {
addTextPanel(
group,
`+${delivery.pallets - visiblePallets}`,
1.3,
0.5,
[bay.x, 1.8, -15.8],
"#17262e",
"#ffffff"
);
}
return group;
}
function clearDynamic() {
beacons.length = 0;
while (dynamicGroup.children.length) {
const object = dynamicGroup.children.pop();
object.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
for (const entry of materials) {
if (entry.map) entry.map.dispose();
entry.dispose();
}
}
});
}
}
function renderScene() {
clearDynamic();
const slotOrders = DELIVERIES.filter(
(delivery) => assignments[delivery.id].slot === selectedSlot
);
for (const bay of BAYS) {
const bayOrders = slotOrders.filter(
(delivery) => assignments[delivery.id].bay === bay.id
);
const bucket = validation.buckets.get(`${selectedSlot}:${bay.id}`) || {
pallets: 0,
kg: 0,
ids: []
};
const bayIssues = validation.issues.filter(
(issue) =>
issue.slot === selectedSlot &&
issue.bay === bay.id
);
const overloaded =
bucket.pallets > bay.capPallets ||
bucket.kg > bay.capKg;
const hasIssue = bayIssues.length > 0;
const outlineColor = overloaded
? 0xff5f66
: hasIssue
? 0xffad58
: editBay === bay.id
? 0x5bd6c4
: 0x52666f;
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(7.9, 0.22, 8.55)
),
new THREE.LineBasicMaterial({
color: outlineColor,
transparent: true,
opacity: editBay === bay.id ? 1 : 0.68
})
);
outline.position.set(bay.x, 0.18, -16.15);
dynamicGroup.add(outline);
if (editBay === bay.id) {
const marker = addBox(
dynamicGroup,
[8.25, 0.05, 8.9],
[bay.x, 0.025, -16.15],
0x5bd6c4,
{
transparent: true,
opacity: 0.09,
castShadow: false
}
);
marker.userData.bayId = bay.id;
}
if (bayOrders.length) {
const truck = createTruck(bayOrders[0].color);
truck.position.set(bay.x, 0.04, -6.65);
truck.scale.setScalar(0.9);
truck.userData.bayId = bay.id;
dynamicGroup.add(truck);
bayOrders.forEach((delivery, index) => {
const cargo = createCargo(delivery, bay, index);
dynamicGroup.add(cargo);
});
}
// Visualize the available pedestrian clearance in front of the dock.
const clearanceStrip = addBox(
dynamicGroup,
[bay.clearance * 1.15, 0.035, 0.55],
[bay.x, 0.2, -11.05],
hasIssue ? 0xff6b6b : 0x5bd6c4,
{
transparent: true,
opacity: hasIssue ? 0.72 : 0.35,
castShadow: false
}
);
clearanceStrip.userData.bayId = bay.id;
for (const issue of bayIssues) {
const badgeColor =
issue.type === "time" ? "#ffad58" : "#ff6b6b";
addTextPanel(
dynamicGroup,
issue.type === "clearance"
? "CLEARANCE"
: issue.type === "capacity"
? "OVER CAPACITY"
: "TIME WINDOW",
4.8,
0.8,
[bay.x, 4.1, -13.3],
badgeColor,
"#172027"
);
const beaconMaterial = new THREE.MeshBasicMaterial({
color: badgeColor,
transparent: true,
opacity: 0.8
});
const beacon = new THREE.Mesh(
new THREE.SphereGeometry(0.22, 10, 8),
beaconMaterial
);
beacon.position.set(bay.x, 4.7, -13.3);
dynamicGroup.add(beacon);
beacons.push(beacon);
if (issue.type === "clearance") {
const requiredWidth = Math.min(
7.3,
deliveryById(issue.id).footprint * 1.35
);
addBox(
dynamicGroup,
[requiredWidth, 0.045, 3.2],
[bay.x, 0.19, -9.7],
0xff5f66,
{
transparent: true,
opacity: 0.34,
castShadow: false
}
);
}
}
}
// Empty-bay indicator.
const occupied = new Set(
slotOrders.map((delivery) => assignments[delivery.id].bay)
);
const emptyCount = BAYS.length - occupied.size;
addTextPanel(
dynamicGroup,
`${slotOrders.length} ORDERS / ${emptyCount} BAYS OPEN`,
10,
1.1,
[0, 0.12, 14.2],
"#18282f",
"#b8d6d5"
).rotation.x = -Math.PI / 2;
}
// ---------------------------------------------------------------------
// Custom camera controls — no Three.js addons
// ---------------------------------------------------------------------
const pointer = {
down: false,
moved: false,
x: 0,
y: 0
};
canvas.addEventListener("pointerdown", (event) => {
pointer.down = true;
pointer.moved = false;
pointer.x = event.clientX;
pointer.y = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.down) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
if (Math.abs(dx) + Math.abs(dy) > 2) pointer.moved = true;
controls.theta -= dx * 0.005;
controls.phi = THREE.MathUtils.clamp(
controls.phi + dy * 0.004,
0.38,
1.22
);
pointer.x = event.clientX;
pointer.y = event.clientY;
updateCamera();
});
const raycaster = new THREE.Raycaster();
const pointerNdc = new THREE.Vector2();
canvas.addEventListener("pointerup", (event) => {
pointer.down = false;
if (pointer.moved) return;
const rect = canvas.getBoundingClientRect();
pointerNdc.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNdc.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNdc, camera);
const hits = raycaster.intersectObjects(scene.children, true);
for (const hit of hits) {
let object = hit.object;
while (object && !object.userData.bayId) {
object = object.parent;
}
if (object?.userData.bayId) {
editBay = object.userData.bayId;
renderInspector();
renderScene();
showToast(`${byId(editBay).name} selected`);
break;
}
}
});
canvas.addEventListener("pointercancel", () => {
pointer.down = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.045,
30,
95
);
updateCamera();
},
{ passive: false }
);
document.querySelector("#camera-zoom-in").addEventListener("click", () => {
controls.radius = Math.max(30, controls.radius - 5);
updateCamera();
});
document.querySelector("#camera-zoom-out").addEventListener("click", () => {
controls.radius = Math.min(95, controls.radius + 5);
updateCamera();
});
document.querySelector("#camera-home").addEventListener("click", () => {
controls.theta = 0.55;
controls.phi = 0.83;
controls.radius = 68;
updateCamera();
});
function resize() {
const rect = canvas.parentElement.getBoundingClientRect();
camera.aspect = Math.max(1, rect.width / Math.max(1, rect.height));
camera.updateProjectionMatrix();
renderer.setSize(rect.width, rect.height, false);
}
new ResizeObserver(resize).observe(
document.querySelector("#scene-wrap")
);
resize();
// ---------------------------------------------------------------------
// UI actions
// ---------------------------------------------------------------------
deliverySelect.addEventListener("change", () => {
selectDelivery(deliverySelect.value);
});
slotSelect.addEventListener("change", () => {
editSlot = Number(slotSelect.value);
selectedSlot = editSlot;
renderAll();
});
baySelect.addEventListener("change", () => {
editBay = baySelect.value;
renderInspector();
renderScene();
});
document.querySelector("#assign-button").addEventListener("click", () => {
const delivery = deliveryById(selectedDelivery);
assignments[selectedDelivery] = {
slot: editSlot,
bay: editBay
};
selectedSlot = editSlot;
renderAll();
const issues = validation.byId[selectedDelivery];
if (issues.length) {
showToast(
`${selectedDelivery} assigned, but ${issues.length} conflict${
issues.length === 1 ? "" : "s"
} remain.`,
true
);
} else {
showToast(`${selectedDelivery} assigned legally.`);
}
});
document.querySelector("#suggest-button").addEventListener("click", () => {
const delivery = deliveryById(selectedDelivery);
const candidates = [];
for (const slot of delivery.windows) {
for (const bay of BAYS) {
const issues = validateCandidate(delivery, slot, bay.id);
if (!issues.length) {
const bucket = buildBucket(slot, bay.id, delivery.id);
candidates.push({
slot,
bay: bay.id,
score:
bucket.pallets / bay.capPallets +
bucket.kg / bay.capKg
});
}
}
}
candidates.sort((a, b) => a.score - b.score);
if (!candidates.length) {
showToast(
"No legal slot and bay combination is currently available.",
true
);
return;
}
const best = candidates[0];
assignments[selectedDelivery] = {
slot: best.slot,
bay: best.bay
};
selectedSlot = best.slot;
editSlot = best.slot;
editBay = best.bay;
renderAll();
showToast(
`${selectedDelivery} moved to ${SLOTS[best.slot].range} at ${byId(best.bay).name}.`
);
});
document.querySelector("#reset-button").addEventListener("click", () => {
resetAssignments();
renderAll();
showToast("Scenario reset to its original conflicted schedule.");
});
// ---------------------------------------------------------------------
// Main render loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
for (const worker of animatedWorkers) {
const travel = (elapsed * worker.userData.speed + worker.userData.offset) % 58;
worker.position.x = -29 + travel;
worker.position.z =
worker.userData.baseZ + Math.sin(elapsed * 1.3 + worker.userData.offset) * 0.3;
worker.rotation.y = Math.sin(elapsed * 1.3 + worker.userData.offset) > 0
? Math.PI / 2
: -Math.PI / 2;
}
for (const forklift of animatedForklifts) {
forklift.position.x =
-13 +
Math.sin(elapsed * 0.28 + forklift.userData.offset) * 17;
forklift.rotation.y =
Math.cos(elapsed * 0.28 + forklift.userData.offset) > 0
? 0
: Math.PI;
}
for (let index = 0; index < beacons.length; index++) {
beacons[index].scale.setScalar(
0.85 + Math.sin(elapsed * 4 + index) * 0.22
);
beacons[index].material.opacity =
0.55 + Math.sin(elapsed * 4 + index) * 0.25;
}
renderer.render(scene, camera);
}
function renderAll() {
validation = validateSchedule();
renderDeliveries();
renderTimeline();
renderInspector();
renderConflicts();
renderMetrics();
renderScene();
}
renderAll();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```