Prompt specification
Build or customize the Three.js 3D scene "Library Returns and Advice Desks" 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: Library Returns and Advice Desks
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/library-returns-and-advice-desks-x3251
Scene Description Prompt:
Library returns and advice desks. Build a library entrance with two service types and accessible circulation. Compare one shared queue with separate queues using fixed demand and clearly stated routing rules. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (library-returns-and-advice-desks-x3251):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Library Flow Lab</title>
<style>
:root {
--navy: #17324d;
--navy-2: #294b68;
--paper: #fbf8f1;
--panel: #ffffff;
--line: #dce4e7;
--muted: #667985;
--orange: #ed8747;
--orange-soft: #fff0e4;
--violet: #7a69bd;
--violet-soft: #efecfb;
--teal: #169b8b;
--teal-soft: #e0f4ef;
--red: #c85151;
--shadow: 0 12px 30px rgba(23, 50, 77, 0.10);
}
* { box-sizing: border-box; }
html, body {
margin: 0;
min-height: 100%;
color: var(--navy);
background: #eaf0f1;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button, select, input { font: inherit; }
button:focus-visible, select:focus-visible, input:focus-visible {
outline: 3px solid rgba(22, 155, 139, 0.35);
outline-offset: 2px;
}
.app {
height: 100vh;
display: grid;
grid-template-rows: 72px minmax(0, 1fr);
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 0 22px;
color: white;
background: var(--navy);
border-bottom: 4px solid var(--teal);
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
position: relative;
flex: 0 0 auto;
}
.brand-mark i {
position: absolute;
display: block;
background: var(--orange);
border-radius: 2px;
}
.brand-mark i:nth-child(1) { width: 17px; height: 24px; left: 2px; top: 7px; }
.brand-mark i:nth-child(2) { width: 17px; height: 17px; left: 20px; top: 14px; background: #f7c16c; }
.brand-mark i:nth-child(3) { width: 15px; height: 7px; left: 4px; top: 1px; background: #67d0be; }
.brand h1 {
margin: 0;
font-size: 19px;
line-height: 1.1;
letter-spacing: 0.01em;
}
.brand p {
margin: 4px 0 0;
color: #bcd0dc;
font-size: 12px;
}
.header-pills {
display: flex;
gap: 8px;
flex-wrap: wrap;
justify-content: flex-end;
}
.pill {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(255,255,255,.14);
border-radius: 999px;
color: #dbe8ee;
background: rgba(255,255,255,.06);
font-size: 11px;
font-weight: 750;
letter-spacing: .04em;
text-transform: uppercase;
}
.pill::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: #63d8c4;
box-shadow: 0 0 0 4px rgba(99,216,196,.12);
}
main {
min-height: 0;
display: grid;
grid-template-columns: 292px minmax(460px, 1fr) 326px;
gap: 12px;
padding: 12px;
}
aside {
min-height: 0;
overflow-y: auto;
display: flex;
flex-direction: column;
gap: 12px;
scrollbar-width: thin;
}
.card {
background: var(--panel);
border: 1px solid rgba(23, 50, 77, .07);
border-radius: 16px;
box-shadow: var(--shadow);
overflow: hidden;
}
.card-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: 15px 16px 10px;
}
.card h2 {
margin: 0;
font-size: 13px;
letter-spacing: .08em;
text-transform: uppercase;
}
.card-body {
padding: 0 16px 16px;
}
.eyebrow {
color: var(--teal);
font-size: 10px;
font-weight: 850;
letter-spacing: .13em;
text-transform: uppercase;
}
.demand-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 10px 0 12px;
}
.demand-stat {
padding: 11px;
border-radius: 11px;
background: #f4f6f5;
}
.demand-stat strong {
display: block;
font-size: 22px;
line-height: 1;
}
.demand-stat span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 11px;
}
.demand-stat.returns { background: var(--orange-soft); }
.demand-stat.advice { background: var(--violet-soft); }
.demand-stat.returns strong { color: #bc612c; }
.demand-stat.advice strong { color: #6654aa; }
#demandChart {
display: block;
width: 100%;
height: 90px;
border: 1px solid var(--line);
border-radius: 10px;
background: #fbfcfb;
}
.chart-key {
display: flex;
gap: 14px;
margin-top: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.dot {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 5px;
border-radius: 50%;
}
.dot.orange { background: var(--orange); }
.dot.violet { background: var(--violet); }
.dot.teal { background: var(--teal); }
.rule {
padding: 12px 0;
border-top: 1px solid var(--line);
}
.rule:first-child { border-top: 0; padding-top: 4px; }
.rule-title {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
font-size: 13px;
font-weight: 800;
}
.rule-number {
width: 23px;
height: 23px;
display: grid;
place-items: center;
border-radius: 7px;
color: white;
background: var(--navy);
font-size: 11px;
}
.rule p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.rule.shared .rule-number { background: var(--orange); }
.rule.separate .rule-number { background: var(--violet); }
.rule.access .rule-number { background: var(--teal); }
.callout {
padding: 11px 12px;
border-radius: 10px;
color: #46606f;
background: #f0f6f5;
font-size: 11px;
line-height: 1.5;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
border-radius: 18px;
background: #dbe8e6;
box-shadow: 0 14px 36px rgba(23, 50, 77, .16);
isolation: isolate;
}
#scene {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene.dragging { cursor: grabbing; }
.stage-toolbar {
position: absolute;
z-index: 3;
top: 14px;
left: 14px;
right: 14px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 10px;
pointer-events: none;
}
.tool-cluster {
display: flex;
align-items: center;
gap: 7px;
flex-wrap: wrap;
pointer-events: auto;
}
.segmented {
display: flex;
padding: 4px;
border: 1px solid rgba(23,50,77,.08);
border-radius: 12px;
background: rgba(255,255,255,.94);
box-shadow: 0 5px 16px rgba(23,50,77,.12);
}
.segmented button {
padding: 8px 11px;
border: 0;
border-radius: 8px;
color: var(--muted);
background: transparent;
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.segmented button[aria-pressed="true"] {
color: white;
background: var(--navy);
box-shadow: 0 3px 8px rgba(23,50,77,.20);
}
.tool-button, .tool-select {
min-height: 40px;
border: 1px solid rgba(23,50,77,.08);
border-radius: 10px;
color: var(--navy);
background: rgba(255,255,255,.94);
box-shadow: 0 5px 16px rgba(23,50,77,.12);
cursor: pointer;
font-size: 12px;
font-weight: 800;
}
.tool-button { padding: 0 12px; }
.tool-button:hover, .tool-select:hover { background: white; }
.tool-select {
padding: 0 28px 0 10px;
}
.stage-hud {
position: absolute;
z-index: 2;
left: 14px;
right: 14px;
bottom: 14px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.hud-card {
padding: 10px 12px;
border: 1px solid rgba(255,255,255,.55);
border-radius: 12px;
color: var(--navy);
background: rgba(255,255,255,.88);
box-shadow: 0 8px 20px rgba(23,50,77,.12);
backdrop-filter: blur(8px);
}
.hud-main {
display: flex;
align-items: baseline;
gap: 9px;
}
#clock {
font-size: 21px;
font-weight: 900;
font-variant-numeric: tabular-nums;
}
#modeBadge {
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: .07em;
text-transform: uppercase;
}
.progress-track {
width: 220px;
height: 5px;
margin-top: 8px;
overflow: hidden;
border-radius: 999px;
background: #dbe4e5;
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--teal);
}
.camera-help {
color: #dce8eb;
font-size: 10px;
text-align: right;
text-shadow: 0 1px 3px rgba(23,50,77,.8);
}
.live-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.live-stat {
min-height: 72px;
padding: 11px;
border-radius: 11px;
background: #f4f7f7;
}
.live-stat strong {
display: block;
font-size: 22px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.live-stat span {
display: block;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
line-height: 1.25;
}
.live-stat.highlight {
background: var(--teal-soft);
}
table {
width: 100%;
border-collapse: collapse;
font-size: 11px;
}
th, td {
padding: 9px 7px;
border-bottom: 1px solid var(--line);
text-align: right;
font-variant-numeric: tabular-nums;
}
th:first-child, td:first-child { text-align: left; }
th {
color: var(--muted);
font-size: 9px;
letter-spacing: .05em;
text-transform: uppercase;
}
th.selected, td.selected {
background: rgba(22,155,139,.07);
color: var(--teal);
font-weight: 850;
}
.access-list {
display: grid;
gap: 8px;
margin-top: 10px;
}
.access-item {
display: flex;
align-items: center;
gap: 10px;
padding: 9px 0;
border-bottom: 1px solid var(--line);
}
.access-item:last-child { border-bottom: 0; }
.access-icon {
width: 31px;
height: 31px;
display: grid;
place-items: center;
flex: 0 0 auto;
border-radius: 9px;
color: var(--teal);
background: var(--teal-soft);
font-size: 15px;
font-weight: 900;
}
.access-item strong {
display: block;
font-size: 11px;
}
.access-item small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 10px 11px;
border-radius: 10px;
background: #f1f7f5;
font-size: 11px;
font-weight: 800;
}
.toggle-row input {
width: 17px;
height: 17px;
accent-color: var(--teal);
}
.fine-print {
margin: 10px 0 0;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
@media (max-width: 1180px) {
body { overflow: auto; }
.app { height: auto; min-height: 100vh; }
main {
grid-template-columns: 280px minmax(0, 1fr);
align-items: start;
}
.stage { min-height: 720px; }
aside.right {
grid-column: 1 / -1;
display: grid;
grid-template-columns: 1fr 1fr;
overflow: visible;
}
}
@media (max-width: 760px) {
.app { grid-template-rows: auto auto; }
header {
min-height: 82px;
padding: 12px 14px;
align-items: flex-start;
}
.header-pills { display: none; }
main {
display: flex;
flex-direction: column;
padding: 8px;
}
.stage {
order: -1;
min-height: 610px;
}
aside.right { display: flex; }
.stage-toolbar {
align-items: stretch;
flex-direction: column;
}
.tool-cluster { width: 100%; }
.segmented { flex: 1; }
.segmented button { flex: 1; }
.stage-hud { align-items: flex-start; }
.camera-help { display: none; }
.progress-track { width: 170px; }
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"><i></i><i></i><i></i></div>
<div>
<h1>Library Flow Lab</h1>
<p>Returns + advice desks · accessible entrance circulation</p>
</div>
</div>
<div class="header-pills">
<span class="pill">Fixed demand</span>
<span class="pill">Voxel model</span>
<span class="pill">Live simulation</span>
</div>
</header>
<main>
<aside class="left" aria-label="Demand and routing rules">
<section class="card">
<div class="card-head">
<h2>Fixed demand</h2>
<span class="eyebrow">Model minutes</span>
</div>
<div class="card-body">
<div class="demand-grid">
<div class="demand-stat returns">
<strong id="returnCount">—</strong>
<span>Return visits</span>
</div>
<div class="demand-stat advice">
<strong id="adviceCount">—</strong>
<span>Advice visits</span>
</div>
</div>
<svg id="demandChart" viewBox="0 0 300 90" role="img"
aria-label="Fixed arrival times for returns and advice"></svg>
<div class="chart-key">
<span><i class="dot orange"></i>Returns</span>
<span><i class="dot violet"></i>Advice</span>
<span><i class="dot teal"></i>Now</span>
</div>
<div class="callout" style="margin-top:12px">
Service times are fixed at <strong>1.5 min</strong> for returns and
<strong>2.5 min</strong> for advice. Every configuration receives
exactly the same 36 visitors and arrival times.
</div>
</div>
</section>
<section class="card">
<div class="card-head">
<h2>Routing rules</h2>
<span class="eyebrow">Explicit FIFO</span>
</div>
<div class="card-body">
<div class="rule shared">
<div class="rule-title"><span class="rule-number">A</span>One shared queue</div>
<p>
All visitors join one arrival-order line. The front visitor can
only visit their matching desk. If that desk is busy, the whole
line waits, even when the other desk is idle.
</p>
</div>
<div class="rule separate">
<div class="rule-title"><span class="rule-number">B</span>Two separate queues</div>
<p>
Returns join the returns line and advice joins the advice line.
Each line is FIFO and each desk serves independently. No visitor
is reassigned and no one receives queue priority.
</p>
</div>
<div class="rule access">
<div class="rule-title"><span class="rule-number">C</span>Access rule</div>
<p>
One visitor uses a wheelchair. Service priority is identical in
both configurations. A step-free route and turning space are
provided independently of queue order.
</p>
</div>
</div>
</section>
<section class="card">
<div class="card-head"><h2>Reading the model</h2></div>
<div class="card-body">
<div class="callout">
Orange people need <strong>returns</strong>; purple people need
<strong>advice</strong>. Green counter lamps mean that desk is
serving. The teal floor overlay shows the accessible circulation
route.
</div>
</div>
</section>
</aside>
<section class="stage" id="stage" aria-label="Interactive three-dimensional library entrance">
<canvas id="scene" tabindex="0"
aria-label="Three-dimensional voxel library entrance. Drag to orbit and scroll to zoom."></canvas>
<div class="stage-toolbar">
<div class="tool-cluster">
<div class="segmented" role="group" aria-label="Queue configuration">
<button type="button" data-mode="shared" aria-pressed="true">Shared queue</button>
<button type="button" data-mode="separate" aria-pressed="false">Separate queues</button>
</div>
</div>
<div class="tool-cluster">
<button class="tool-button" id="playButton" type="button" aria-label="Pause simulation">Pause</button>
<button class="tool-button" id="restartButton" type="button">Restart</button>
<select class="tool-select" id="speedSelect" aria-label="Simulation speed">
<option value="0.5">0.5×</option>
<option value="1">1×</option>
<option value="2" selected>2×</option>
<option value="4">4×</option>
</select>
<button class="tool-button" id="cameraButton" type="button">Reset view</button>
</div>
</div>
<div class="stage-hud">
<div class="hud-card">
<div class="hud-main">
<span id="clock">00.0 min</span>
<span id="modeBadge">Shared FIFO</span>
</div>
<div class="progress-track"><div id="progressFill"></div></div>
</div>
<div class="camera-help">
Drag to orbit · scroll to zoom<br />
Space: play/pause · R: restart · 1/2: queue mode
</div>
</div>
</section>
<aside class="right" aria-label="Results and accessibility">
<section class="card">
<div class="card-head">
<h2>Live outcome</h2>
<span class="eyebrow" id="liveModeLabel">Shared</span>
</div>
<div class="card-body">
<div class="live-grid">
<div class="live-stat"><strong id="arrivedNow">0</strong><span>ARRIVED</span></div>
<div class="live-stat"><strong id="completedNow">0</strong><span>COMPLETED</span></div>
<div class="live-stat highlight"><strong id="waitingNow">0</strong><span>WAITING</span></div>
<div class="live-stat"><strong id="servedNow">0</strong><span>AT DESKS</span></div>
<div class="live-stat highlight" style="grid-column:1/-1">
<strong id="liveAverageWait">0.0 min</strong>
<span>AVERAGE WAIT OF VISITORS WHO HAVE STARTED SERVICE</span>
</div>
</div>
</div>
</section>
<section class="card">
<div class="card-head">
<h2>Queue comparison</h2>
<span class="eyebrow">Same demand</span>
</div>
<div class="card-body">
<table aria-label="Shared queue and separate queue comparison">
<thead>
<tr>
<th>Metric</th>
<th data-col="shared" class="selected">Shared</th>
<th data-col="separate">Separate</th>
</tr>
</thead>
<tbody id="comparisonBody"></tbody>
</table>
<p class="fine-print">
Average and percentile waits are measured from arrival to service
start. Utilization is total occupied desk time divided by available
desk time. This is a schematic comparison, not a code-compliance model.
</p>
</div>
</section>
<section class="card">
<div class="card-head">
<h2>Accessible circulation</h2>
<span class="eyebrow">Clear route</span>
</div>
<div class="card-body">
<div class="toggle-row">
<label for="accessToggle">Show accessible route</label>
<input id="accessToggle" type="checkbox" checked>
</div>
<div class="access-list">
<div class="access-item">
<div class="access-icon">↗</div>
<div><strong>Step-free ramp</strong><small>0.36 m rise over 4.32 m: a 1:12 schematic slope.</small></div>
</div>
<div class="access-item">
<div class="access-icon">↔</div>
<div><strong>1.8 m clear route</strong><small>Continuous route from the ramp to a rear cross-aisle.</small></div>
</div>
<div class="access-item">
<div class="access-icon">○</div>
<div><strong>1.5 m turning areas</strong><small>Turning circles are shown near the ramp, center aisle, and desks.</small></div>
</div>
<div class="access-item">
<div class="access-icon">▱</div>
<div><strong>Accessible counter height</strong><small>Desk surfaces are 0.82 m high with open knee space below.</small></div>
</div>
</div>
<p class="fine-print">
Verify slope, landing dimensions, clearances, signage, and local
accessibility requirements before using this as a real design.
</p>
</div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const $$ = (selector) => [...document.querySelectorAll(selector)];
// ------------------------------------------------------------
// Fixed demand and deterministic queue simulations
// ------------------------------------------------------------
const fixedDemand = [
[0.0, "R"], [0.0, "A"], [0.5, "R"], [1.0, "A"],
[1.5, "R"], [2.0, "A"], [2.5, "R"], [3.0, "R"],
[3.5, "A"], [4.0, "R"], [4.5, "A"], [5.0, "R"],
[5.5, "A"], [6.0, "R"], [6.5, "A"], [7.0, "R"],
[7.5, "R"], [8.0, "A"], [8.5, "A"], [9.0, "R"],
[9.5, "A"], [10.0, "R"], [10.5, "A"], [11.0, "R"],
[11.5, "A"], [12.0, "R"], [12.5, "A"], [13.0, "R"],
[13.5, "A"], [14.0, "R"], [14.5, "A"], [15.0, "R"],
[16.0, "A"], [17.0, "A"], [18.0, "R"], [18.5, "A"]
];
const serviceDuration = { R: 1.5, A: 2.5 };
const wheelchairVisitorId = 4;
const maximumArrival = fixedDemand.at(-1)[0];
const demand = fixedDemand.map(([arrival, type], id) => ({
id,
arrival,
type,
wheelchair: id === wheelchairVisitorId
}));
function percentile(values, p) {
if (!values.length) return 0;
const sorted = [...values].sort((a, b) => a - b);
return sorted[Math.max(0, Math.ceil(sorted.length * p) - 1)];
}
function simulate(mode) {
const records = demand.map((d) => ({
...d,
start: null,
end: null,
wait: null
}));
const sharedQueue = [];
const separateQueues = { R: [], A: [] };
const servers = {
R: { busy: false, end: Infinity, visitor: null },
A: { busy: false, end: Infinity, visitor: null }
};
let nextArrival = 0;
let now = 0;
const infinity = Number.POSITIVE_INFINITY;
function beginService(record) {
record.start = now;
record.wait = record.start - record.arrival;
record.end = now + serviceDuration[record.type];
servers[record.type] = {
busy: true,
end: record.end,
visitor: record
};
}
function startEligibleServices() {
if (mode === "shared") {
// Strict shared FIFO with head-of-line blocking.
while (sharedQueue.length) {
const next = sharedQueue[0];
if (servers[next.type].busy) break;
sharedQueue.shift();
beginService(next);
}
} else {
// Independent type-specific FIFO queues.
for (const type of ["R", "A"]) {
while (separateQueues[type].length && !servers[type].busy) {
beginService(separateQueues[type].shift());
}
}
}
}
while (true) {
const eventTimes = [];
if (nextArrival < records.length) {
eventTimes.push(records[nextArrival].arrival);
}
for (const type of ["R", "A"]) {
if (servers[type].busy) eventTimes.push(servers[type].end);
}
if (!eventTimes.length) break;
now = Math.min(...eventTimes);
// Complete services first at a coincident timestamp.
for (const type of ["R", "A"]) {
if (servers[type].busy && servers[type].end <= now + 1e-9) {
servers[type].busy = false;
servers[type].end = Infinity;
servers[type].visitor = null;
}
}
// Add all arrivals at this timestamp, retaining the fixed input order.
while (
nextArrival < records.length &&
records[nextArrival].arrival <= now + 1e-9
) {
const record = records[nextArrival++];
if (mode === "shared") {
sharedQueue.push(record);
} else {
separateQueues[record.type].push(record);
}
}
startEligibleServices();
}
const waits = records.map((r) => r.wait).filter((v) => v !== null);
const makespan = Math.max(...records.map((r) => r.end));
const serviceTime =
records.filter((r) => r.type === "R").length * serviceDuration.R +
records.filter((r) => r.type === "A").length * serviceDuration.A;
return {
mode,
records,
metrics: {
averageWait: waits.reduce((sum, value) => sum + value, 0) / waits.length,
p90Wait: percentile(waits, 0.90),
maxWait: Math.max(...waits),
makespan,
utilization: serviceTime / (2 * makespan),
throughputPerHour: 60 / makespan
}
};
}
const simulations = {
shared: simulate("shared"),
separate: simulate("separate")
};
$("#returnCount").textContent = demand.filter((d) => d.type === "R").length;
$("#adviceCount").textContent = demand.filter((d) => d.type === "A").length;
// ------------------------------------------------------------
// Demand chart and comparison table
// ------------------------------------------------------------
function renderDemandChart() {
const chart = $("#demandChart");
const left = 13;
const right = 288;
const chartWidth = right - left;
let content = `
<line x1="${left}" y1="45" x2="${right}" y2="45"
stroke="#dfe6e7" stroke-width="1"/>
<line x1="${left}" y1="24" x2="${right}" y2="24"
stroke="#edf1f2" stroke-width="1"/>
<line x1="${left}" y1="66" x2="${right}" y2="66"
stroke="#edf1f2" stroke-width="1"/>
<text x="4" y="28" font-size="8" fill="#ed8747" font-weight="700">R</text>
<text x="4" y="70" font-size="8" fill="#7a69bd" font-weight="700">A</text>
<text x="${left}" y="86" font-size="8" fill="#7c8b93">0 min</text>
<text x="${right}" y="86" text-anchor="end" font-size="8" fill="#7c8b93">18.5 min</text>
`;
for (const visitor of demand) {
const x = left + (visitor.arrival / maximumArrival) * chartWidth;
const y = visitor.type === "R" ? 24 : 66;
const color = visitor.type === "R" ? "#ed8747" : "#7a69bd";
content += `
<line x1="${x}" y1="${y - 5}" x2="${x}" y2="${y + 5}"
stroke="${color}" stroke-width="2"/>
<circle cx="${x}" cy="${y}" r="2.5" fill="${color}"/>
`;
}
content += `<line id="chartNow" x1="${left}" y1="10" x2="${left}" y2="76"
stroke="#169b8b" stroke-width="1.5" stroke-dasharray="3 2"/>`;
chart.innerHTML = content;
}
function renderComparison() {
const rows = [
["Average wait", "averageWait", " min", 1],
["90th-percentile wait", "p90Wait", " min", 1],
["Maximum wait", "maxWait", " min", 1],
["Last departure", "makespan", " min", 1],
["Desk utilization", "utilization", "%", 100, 0],
["Throughput", "throughputPerHour", "/hr", 1]
];
$("#comparisonBody").innerHTML = rows.map(([label, key, suffix, multiplier, digits]) => {
const shared = simulations.shared.metrics[key] * multiplier;
const separate = simulations.separate.metrics[key] * multiplier;
return `
<tr>
<td>${label}</td>
<td data-col="shared">${shared.toFixed(digits)}</td>
<td data-col="separate">${separate.toFixed(digits)}${suffix}</td>
</tr>
`;
}).join("");
}
renderDemandChart();
renderComparison();
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const canvas = $("#scene");
const stage = $("#stage");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0xdbe8e6, 1);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe8e6);
scene.fog = new THREE.Fog(0xdbe8e6, 38, 68);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.7, 1.0);
let cameraTheta = 0.62;
let cameraPhi = 0.78;
let cameraRadius = 31;
function updateCamera() {
const horizontal = cameraRadius * Math.cos(cameraPhi);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.sin(cameraPhi),
cameraTarget.z + horizontal * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.62;
cameraPhi = 0.78;
cameraRadius = 31;
updateCamera();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf8fbfa, 0x78909b, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3dc, 3.2);
sun.position.set(-13, 22, 15);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbfe9e3, 1.1);
fill.position.set(15, 9, -8);
scene.add(fill);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(color, new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02
}));
}
return materialCache.get(color);
}
function addBox(parent, size, position, color, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
color && color.isMaterial ? color : material(color)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.cast ?? true;
mesh.receiveShadow = options.receive ?? true;
if (options.rotationY !== undefined) mesh.rotation.y = options.rotationY;
if (options.rotationX !== undefined) mesh.rotation.x = options.rotationX;
parent.add(mesh);
return mesh;
}
function labelTexture(title, subtitle = "", background = "#17324d", foreground = "#ffffff") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 768;
labelCanvas.height = 192;
const context = labelCanvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, labelCanvas.width, labelCanvas.height);
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = foreground;
context.font = "800 55px Arial, sans-serif";
context.fillText(title, 384, subtitle ? 68 : 96);
if (subtitle) {
context.globalAlpha = 0.82;
context.font = "600 25px Arial, sans-serif";
context.fillText(subtitle, 384, 135);
}
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function wallLabel(parent, title, subtitle, position, width, height, background, foreground) {
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
map: labelTexture(title, subtitle, background, foreground),
transparent: false
})
);
mesh.position.set(position[0], position[1], position[2]);
parent.add(mesh);
return mesh;
}
function tag(parent, title, subtitle, position, width, background, foreground) {
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: labelTexture(title, subtitle, background, foreground),
transparent: true,
depthTest: false
}));
sprite.position.set(position[0], position[1], position[2]);
sprite.scale.set(width, width * 0.25, 1);
parent.add(sprite);
return sprite;
}
const world = new THREE.Group();
scene.add(world);
// Ground and raised lobby slab.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
material(0xcfdedc)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.47;
ground.receiveShadow = true;
world.add(ground);
addBox(world, [20.4, 0.4, 13.4], [0, -0.2, -0.5], 0xb9c7c7, { cast: false });
addBox(world, [20.4, 0.18, 2.8], [0, -0.43, 11.55], 0xc4d1cf, { cast: false });
const tileGeometry = new THREE.BoxGeometry(0.98, 0.06, 0.98);
const tileMaterials = [
new THREE.MeshStandardMaterial({ color: 0xf3efe6, roughness: 0.9 }),
new THREE.MeshStandardMaterial({ color: 0xeae5da, roughness: 0.9 })
];
for (let x = 0; x < 20; x++) {
for (let z = 0; z < 13; z++) {
const tile = new THREE.Mesh(tileGeometry, tileMaterials[(x + z) % 2]);
tile.position.set(-9.5 + x, 0.025, -6.5 + z);
tile.receiveShadow = true;
world.add(tile);
}
}
// Back wall and low side walls.
addBox(world, [20.5, 4.5, 0.36], [0, 2.2, -7.25], 0xf5f0e6);
addBox(world, [0.35, 1.0, 13.2], [-10.15, 0.48, -0.5], 0xd5dedb);
addBox(world, [0.35, 1.0, 13.2], [10.15, 0.48, -0.5], 0xd5dedb);
for (const x of [-8.7, 8.7]) {
addBox(world, [0.65, 4.2, 0.65], [x, 2.05, -6.8], 0xe3dccf);
addBox(world, [0.82, 0.16, 0.82], [x, 0.12, -6.8], 0xbac8c5);
}
wallLabel(
world,
"LIBRARY",
"ENTRANCE • RETURNS + ADVICE",
[0, 3.35, -7.04],
6.6,
1.65,
"#17324d",
"#ffffff"
);
// Voxel shelves and books.
function makeShelf(x) {
const group = new THREE.Group();
group.position.set(x, 0, -6.48);
world.add(group);
addBox(group, [3.1, 0.18, 0.85], [0, 0.18, 0], 0x6e543d);
addBox(group, [3.1, 0.18, 0.85], [0, 3.45, 0], 0x6e543d);
addBox(group, [0.18, 3.45, 0.85], [-1.46, 1.8, 0], 0x6e543d);
addBox(group, [0.18, 3.45, 0.85], [1.46, 1.8, 0], 0x6e543d);
const bookColors = ["#ed8747", "#7a69bd", "#169b8b", "#e1b85b", "#c85151", "#53758d"];
for (const y of [0.65, 1.42, 2.19, 2.96]) {
addBox(group, [2.9, 0.12, 0.78], [0, y - 0.25, 0], 0x87684a);
for (let i = 0; i < 8; i++) {
const height = 0.38 + (i % 3) * 0.06;
addBox(
group,
[0.22, height, 0.46],
[-1.18 + i * 0.33, y - 0.19 + height / 2, 0.07],
bookColors[(i + Math.round(y)) % bookColors.length]
);
}
}
}
makeShelf(-6.8);
makeShelf(6.8);
// Voxel plants.
function makePlant(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
world.add(group);
addBox(group, [0.75, 0.65, 0.75], [0, 0.32, 0], 0xc47d52);
addBox(group, [0.18, 1.1, 0.18], [0, 1.0, 0], 0x466b4f);
addBox(group, [0.8, 0.55, 0.55], [0, 1.3, 0], 0x4e8c64);
addBox(group, [0.55, 0.65, 0.65], [-0.25, 1.65, 0], 0x67a878);
addBox(group, [0.55, 0.55, 0.6], [0.3, 1.55, 0.05], 0x3f7c58);
}
makePlant(-8.7, 4.6);
makePlant(8.7, 4.6);
// Accessible ramp at the left side of the entrance.
const rampX = -7.0;
const rampStartZ = 6.0;
const rampEndZ = 10.32;
const rampLength = 4.35;
const rampAngle = Math.atan2(0.36, 4.32);
const ramp = addBox(
world,
[2.4, 0.12, rampLength],
[rampX, -0.22, 8.16],
0xc9d7d4,
{ rotationX: rampAngle }
);
ramp.receiveShadow = true;
function rampTopAt(z) {
return 0.02 - ((z - rampStartZ) / (rampEndZ - rampStartZ)) * 0.36;
}
for (const side of [-1, 1]) {
const railX = rampX + side * 1.14;
addBox(
world,
[0.08, 0.08, rampLength],
[railX, 0.74, 8.16],
0x496b78,
{ rotationX: rampAngle }
);
for (const z of [6.3, 8.15, 10.0]) {
addBox(world, [0.09, 0.95, 0.09], [railX, rampTopAt(z) + 0.48, z], 0x496b78);
}
}
// Accessible route overlay.
const accessGroup = new THREE.Group();
world.add(accessGroup);
const routeFillMaterial = new THREE.MeshStandardMaterial({
color: 0xbfe8df,
roughness: 0.95,
transparent: true,
opacity: 0.58
});
function routeStrip(a, b, width = 1.8) {
const dx = b[0] - a[0];
const dz = b[1] - a[1];
const length = Math.hypot(dx, dz);
const mesh = addBox(
accessGroup,
[width, 0.018, length],
[(a[0] + b[0]) / 2, 0.071, (a[1] + b[1]) / 2],
routeFillMaterial,
{ cast: false }
);
mesh.rotation.y = -Math.atan2(dz, dx);
return mesh;
}
function routeDashes(a, b, spacing = 0.62) {
const dx = b[0] - a[0];
const dz = b[1] - a[1];
const length = Math.hypot(dx, dz);
const count = Math.max(1, Math.floor(length / spacing));
for (let i = 0; i <= count; i++) {
const t = i / count;
addBox(
accessGroup,
[0.16, 0.028, 0.16],
[a[0] + dx * t, 0.09, a[1] + dz * t],
0x169b8b,
{ cast: false }
);
}
}
function turningCircle(x, z) {
const radius = 0.75;
const segments = 20;
for (let i = 0; i < segments; i++) {
const angle = (i / segments) * Math.PI * 2;
addBox(
accessGroup,
[0.13, 0.03, 0.13],
[x + Math.cos(angle) * radius, 0.094, z + Math.sin(angle) * radius],
0x169b8b,
{ cast: false }
);
}
}
routeStrip([-7, 5.8], [-7, -2.0]);
routeStrip([-7, -2.0], [6.4, -2.0]);
routeStrip([-4.2, -2.0], [-4.2, -3.0]);
routeStrip([4.2, -2.0], [4.2, -3.0]);
routeDashes([-7, 5.7], [-7, -1.9]);
routeDashes([-6.8, -2], [6.2, -2]);
routeDashes([-4.2, -2.1], [-4.2, -2.8]);
routeDashes([4.2, -2.1], [4.2, -2.8]);
turningCircle(-7, 3.0);
turningCircle(0, -2.0);
turningCircle(-4.2, -2.45);
turningCircle(4.2, -2.45);
for (let z = 6.35; z < 10.2; z += 0.48) {
const marker = addBox(
accessGroup,
[0.18, 0.025, 0.43],
[rampX, rampTopAt(z) + 0.025, z],
0x169b8b,
{ cast: false }
);
marker.rotation.x = rampAngle;
}
tag(accessGroup, "1:12 RAMP", "STEP-FREE ENTRY", [-7, 1.5, 8.0], 2.25, "#169b8b", "#ffffff");
tag(accessGroup, "1.8 m CLEAR", "CONTINUOUS ROUTE", [-7, 1.15, 3.0], 2.15, "#169b8b", "#ffffff");
tag(accessGroup, "1.5 m TURN", "KEEP CLEAR", [0, 0.8, -2.2], 1.8, "#169b8b", "#ffffff");
// Queue paths.
const sharedPath = [
{ x: 0.0, z: -0.45 },
{ x: 0.0, z: 4.8 },
{ x: 2.2, z: 4.8 },
{ x: 2.2, z: -0.45 }
];
const separatePath = {
R: [{ x: -4.2, z: -0.9 }, { x: -4.2, z: 5.0 }],
A: [{ x: 4.2, z: -0.9 }, { x: 4.2, z: 5.0 }]
};
function pointAlongPath(path, distance) {
for (let i = 0; i < path.length - 1; i++) {
const a = path[i];
const b = path[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (distance <= length) {
const t = length ? distance / length : 0;
return { x: a.x + dx * t, z: a.z + dz * t };
}
distance -= length;
}
const a = path.at(-2);
const b = path.at(-1);
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz) || 1;
return {
x: b.x + dx / length * distance,
z: b.z + dz / length * distance
};
}
function makeRail(parent, path) {
const offsetPath = path.map((point, index) => {
const previous = path[Math.max(0, index - 1)];
const next = path[Math.min(path.length - 1, index + 1)];
const dx = next.x - previous.x;
const dz = next.z - previous.z;
const length = Math.hypot(dx, dz) || 1;
return { x: point.x - dz / length * 0.37, z: point.z + dx / length * 0.37 };
});
for (let i = 0; i < offsetPath.length - 1; i++) {
const a = offsetPath[i];
const b = offsetPath[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
addBox(
parent,
[length, 0.07, 0.07],
[(a.x + b.x) / 2, 0.73, (a.z + b.z) / 2],
0x536f7b,
{ rotationY: -Math.atan2(dz, dx) }
);
const posts = Math.max(1, Math.ceil(length / 1.4));
for (let p = 0; p <= posts; p++) {
if (i > 0 && p === 0) continue;
const t = p / posts;
addBox(
parent,
[0.09, 0.88, 0.09],
[a.x + dx * t, 0.43, a.z + dz * t],
0x536f7b
);
}
}
}
const sharedQueueGroup = new THREE.Group();
const separateQueueGroup = new THREE.Group();
world.add(sharedQueueGroup, separateQueueGroup);
makeRail(sharedQueueGroup, sharedPath);
makeRail(separateQueueGroup, separatePath.R);
makeRail(separateQueueGroup, separatePath.A);
const sharedTag = tag(
sharedQueueGroup, "ONE COMMON FIFO", "MATCHING DESK ONLY",
[0.9, 1.85, 4.9], 3.0, "#ed8747", "#ffffff"
);
const separateTagR = tag(
separateQueueGroup, "RETURNS FIFO", "INDEPENDENT LINE",
[-4.2, 1.85, 5.1], 2.65, "#ed8747", "#ffffff"
);
const separateTagA = tag(
separateQueueGroup, "ADVICE FIFO", "INDEPENDENT LINE",
[4.2, 1.85, 5.1], 2.65, "#7a69bd", "#ffffff"
);
// Service desks.
const deskData = {
R: { x: -4.2, color: 0xed8747, label: "RETURNS" },
A: { x: 4.2, color: 0x7a69bd, label: "ADVICE" }
};
function createPerson(color, wheelchair = false, staff = false) {
const group = new THREE.Group();
const bodyColor = staff ? (color === 0xed8747 ? 0x287d73 : 0x4d6590) : color;
if (wheelchair) {
addBox(group, [0.58, 0.14, 0.58], [0, 0.43, 0.03], 0x334b59);
addBox(group, [0.58, 0.60, 0.12], [0, 0.72, -0.23], 0x334b59);
addBox(group, [0.12, 0.48, 0.48], [-0.39, 0.28, 0.04], 0x263844);
addBox(group, [0.12, 0.48, 0.48], [0.39, 0.28, 0.04], 0x263844);
addBox(group, [0.42, 0.48, 0.28], [0, 0.76, 0.02], bodyColor);
addBox(group, [0.32, 0.32, 0.32], [0, 1.17, 0.02], 0xe2b48c);
addBox(group, [0.34, 0.10, 0.34], [0, 1.36, 0.02], 0x4c3e37);
addBox(group, [0.12, 0.35, 0.14], [-0.27, 0.77, 0.04], bodyColor);
addBox(group, [0.12, 0.35, 0.14], [0.27, 0.77, 0.04], bodyColor);
} else {
addBox(group, [0.16, 0.44, 0.18], [-0.13, 0.22, 0], 0x354b5d);
addBox(group, [0.16, 0.44, 0.18], [0.13, 0.22, 0], 0x354b5d);
addBox(group, [0.50, 0.58, 0.32], [0, 0.72, 0], bodyColor);
addBox(group, [0.12, 0.42, 0.16], [-0.32, 0.70, 0], bodyColor);
addBox(group, [0.12, 0.42, 0.16], [0.32, 0.70, 0], bodyColor);
addBox(group, [0.34, 0.34, 0.34], [0, 1.18, 0], 0xe2b48c);
addBox(group, [0.36, 0.10, 0.36], [0, 1.38, 0], staff ? 0x334b59 : 0x4c3e37);
}
if (staff) group.scale.setScalar(0.94);
group.userData.wheelchair = wheelchair;
return group;
}
const deskStatusMaterials = {};
function makeDesk(type) {
const data = deskData[type];
const group = new THREE.Group();
world.add(group);
// 0.82 m counter surface with open knee space.
addBox(group, [3.7, 0.14, 1.0], [data.x, 0.75, -4.5], data.color);
addBox(group, [0.28, 0.68, 0.75], [data.x - 1.56, 0.34, -4.5], 0x9c7658);
addBox(group, [0.28, 0.68, 0.75], [data.x + 1.56, 0.34, -4.5], 0x9c7658);
addBox(group, [0.62, 0.42, 0.12], [data.x + 0.7, 1.02, -4.55], 0x334b59);
addBox(group, [0.48, 0.32, 0.08], [data.x + 0.7, 1.04, -4.47], 0x8ecac2);
addBox(group, [0.72, 0.04, 0.28], [data.x + 0.7, 0.84, -4.35], 0x455f6f);
tag(group, data.label, type === "R" ? "BOOK RETURNS" : "INFORMATION + ADVICE",
[data.x, 2.0, -4.8], 3.2,
type === "R" ? "#ed8747" : "#7a69bd", "#ffffff");
const lampMaterial = new THREE.MeshStandardMaterial({
color: 0x73858c,
emissive: 0x000000,
roughness: 0.5
});
deskStatusMaterials[type] = lampMaterial;
addBox(group, [0.24, 0.24, 0.24], [data.x - 1.25, 1.05, -4.45], lampMaterial);
const staff = createPerson(data.color, false, true);
staff.position.set(data.x, 0.06, -5.6);
staff.rotation.y = Math.PI;
group.add(staff);
if (type === "R") {
addBox(group, [0.9, 0.85, 0.9], [data.x - 1.35, 0.43, -3.65], 0xb87043);
addBox(group, [0.72, 0.12, 0.72], [data.x - 1.35, 0.91, -3.65], 0x354b5d);
} else {
addBox(group, [0.8, 0.14, 0.55], [data.x - 1.2, 0.93, -4.42], 0xf0c66e);
addBox(group, [0.12, 0.32, 0.12], [data.x - 1.2, 1.09, -4.42], 0xc65b52);
}
}
makeDesk("R");
makeDesk("A");
// Visitors.
const visitors = demand.map((visitor) => {
const person = createPerson(
visitor.type === "R" ? 0xed8747 : 0x7a69bd,
visitor.wheelchair,
false
);
person.visible = false;
world.add(person);
return person;
});
// ------------------------------------------------------------
// Animation state and direct camera controls
// ------------------------------------------------------------
let mode = "shared";
let activeSimulation = simulations.shared;
let simulationTime = 0;
let playing = true;
let speed = 2;
let previousFrame = performance.now();
let lastUiUpdate = 0;
function setMode(nextMode) {
mode = nextMode;
activeSimulation = simulations[mode];
simulationTime = 0;
playing = true;
sharedQueueGroup.visible = mode === "shared";
separateQueueGroup.visible = mode === "separate";
sharedTag.visible = mode === "shared";
separateTagR.visible = mode === "separate";
separateTagA.visible = mode === "separate";
$$("[data-mode]").forEach((button) => {
button.setAttribute("aria-pressed", String(button.dataset.mode === mode));
});
$$("[data-col]").forEach((cell) => {
cell.classList.toggle("selected", cell.dataset.col === mode);
});
$("#liveModeLabel").textContent = mode === "shared" ? "Shared" : "Separate";
$("#modeBadge").textContent = mode === "shared" ? "Shared FIFO" : "Two FIFO lines";
$("#playButton").textContent = "Pause";
$("#playButton").setAttribute("aria-label", "Pause simulation");
updateVisitors(simulationTime);
updateLiveUI();
}
function togglePlayback() {
if (simulationTime >= activeSimulation.metrics.makespan) {
simulationTime = 0;
playing = true;
} else {
playing = !playing;
}
$("#playButton").textContent = playing ? "Pause" : "Play";
$("#playButton").setAttribute("aria-label", playing ? "Pause simulation" : "Play simulation");
}
function restart() {
simulationTime = 0;
playing = true;
$("#playButton").textContent = "Pause";
$("#playButton").setAttribute("aria-label", "Pause simulation");
updateVisitors(simulationTime);
updateLiveUI();
}
$$("[data-mode]").forEach((button) => {
button.addEventListener("click", () => setMode(button.dataset.mode));
});
$("#playButton").addEventListener("click", togglePlayback);
$("#restartButton").addEventListener("click", restart);
$("#cameraButton").addEventListener("click", resetCamera);
$("#speedSelect").addEventListener("change", (event) => {
speed = Number(event.target.value);
});
$("#accessToggle").addEventListener("change", (event) => {
accessGroup.visible = event.target.checked;
});
let dragging = false;
let dragX = 0;
let dragY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.004, 0.28, 1.30);
updateCamera();
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
17,
48
);
updateCamera();
}, { passive: false });
window.addEventListener("keydown", (event) => {
if (["INPUT", "SELECT", "TEXTAREA", "BUTTON"].includes(document.activeElement?.tagName)) {
return;
}
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
} else if (event.key.toLowerCase() === "r") {
restart();
} else if (event.key === "1") {
setMode("shared");
} else if (event.key === "2") {
setMode("separate");
}
});
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();
}
new ResizeObserver(resize).observe(stage);
resize();
// ------------------------------------------------------------
// Visitor positions and live metrics
// ------------------------------------------------------------
function queuePosition(type, index) {
if (mode === "shared") {
return pointAlongPath(sharedPath, index * 0.72);
}
const path = separatePath[type];
return pointAlongPath(path, index * 0.72);
}
function servicePosition(type) {
return { x: deskData[type].x, z: -3.45 };
}
function queueOrigin(type) {
return mode === "shared"
? { x: 0, z: -0.45 }
: { x: deskData[type].x, z: -0.9 };
}
function lerpPoint(a, b, t) {
const clamped = THREE.MathUtils.clamp(t, 0, 1);
return {
x: a.x + (b.x - a.x) * clamped,
z: a.z + (b.z - a.z) * clamped
};
}
function faceMovement(object, dx, dz) {
if (Math.abs(dx) + Math.abs(dz) > 0.001) {
object.rotation.y = Math.atan2(dx, dz);
}
}
function updateVisitors(time) {
const records = activeSimulation.records;
const arrivedWaiting = records.filter(
(r) => r.arrival <= time && r.start > time
);
const sharedWaiting = arrivedWaiting;
const separateWaiting = {
R: arrivedWaiting.filter((r) => r.type === "R"),
A: arrivedWaiting.filter((r) => r.type === "A")
};
records.forEach((record) => {
const object = visitors[record.id];
if (time < record.arrival) {
object.visible = false;
return;
}
object.visible = true;
if (time >= record.end) {
// Walk away through the lobby, then disappear.
const progress = (time - record.end) / 1.35;
const origin = servicePosition(record.type);
const midpoint = { x: -7, z: 2.7 };
const exit = { x: -7, z: 5.65 };
const destination = progress < 0.48
? lerpPoint(origin, midpoint, progress / 0.48)
: lerpPoint(midpoint, exit, (progress - 0.48) / 0.52);
object.position.set(destination.x, 0.07, destination.z);
if (progress < 0.48) {
faceMovement(object, midpoint.x - origin.x, midpoint.z - origin.z);
} else {
faceMovement(object, exit.x - midpoint.x, exit.z - midpoint.z);
}
if (progress >= 1) object.visible = false;
return;
}
if (time >= record.start) {
// Move from the queue head to the service counter.
const origin = queueOrigin(record.type);
const destination = servicePosition(record.type);
const progress = (time - record.start) / 0.34;
const point = lerpPoint(origin, destination, progress);
object.position.set(point.x, 0.07, point.z);
faceMovement(object, destination.x - origin.x, destination.z - origin.z);
return;
}
// Waiting: determine the physical queue index at this moment.
const waitingList = mode === "shared"
? sharedWaiting
: separateWaiting[record.type];
const index = Math.max(0, waitingList.findIndex((r) => r.id === record.id));
const destination = queuePosition(record.type, index);
const arrivalProgress = Math.min(1, (time - record.arrival) / 0.62);
const entrance = { x: -7, z: 5.55 };
const point = lerpPoint(entrance, destination, arrivalProgress);
object.position.set(point.x, 0.07, point.z);
faceMovement(object, destination.x - entrance.x, destination.z - entrance.z);
});
const serving = { R: false, A: false };
for (const record of records) {
if (record.start <= time && time < record.end) {
serving[record.type] = true;
}
}
for (const type of ["R", "A"]) {
const mat = deskStatusMaterials[type];
mat.color.set(serving[type] ? 0x38c9a5 : 0x73858c);
mat.emissive.set(serving[type] ? 0x0c5c48 : 0x000000);
}
}
function updateLiveUI() {
const records = activeSimulation.records;
const arrived = records.filter((r) => r.arrival <= simulationTime);
const completed = records.filter((r) => r.end <= simulationTime);
const waiting = records.filter((r) => r.arrival <= simulationTime && r.start > simulationTime);
const atDesks = records.filter((r) => r.start <= simulationTime && simulationTime < r.end);
const started = records.filter((r) => r.start !== null && r.start <= simulationTime);
const average = started.length
? started.reduce((sum, r) => sum + r.wait, 0) / started.length
: 0;
$("#arrivedNow").textContent = arrived.length;
$("#completedNow").textContent = completed.length;
$("#waitingNow").textContent = waiting.length;
$("#servedNow").textContent = atDesks.length;
$("#liveAverageWait").textContent = `${average.toFixed(1)} min`;
$("#clock").textContent = `${simulationTime.toFixed(1).padStart(4, "0")} min`;
const totalTime = activeSimulation.metrics.makespan;
const progress = Math.min(1, simulationTime / totalTime);
$("#progressFill").style.width = `${progress * 100}%`;
const chartNow = $("#chartNow");
if (chartNow) {
const x = 13 + Math.min(1, simulationTime / maximumArrival) * 275;
chartNow.setAttribute("x1", x);
chartNow.setAttribute("x2", x);
}
}
function animate(now) {
const deltaSeconds = Math.min(0.1, (now - previousFrame) / 1000);
previousFrame = now;
if (playing) {
// One model minute per real second at 1×.
simulationTime += deltaSeconds * speed;
if (simulationTime >= activeSimulation.metrics.makespan) {
simulationTime = activeSimulation.metrics.makespan;
playing = false;
$("#playButton").textContent = "Replay";
$("#playButton").setAttribute("aria-label", "Replay simulation");
}
}
updateVisitors(simulationTime);
if (now - lastUiUpdate > 90) {
updateLiveUI();
lastUiUpdate = now;
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
setMode("shared");
updateVisitors(0);
updateLiveUI();
requestAnimationFrame(animate);
</script>
</body>
</html>
```