Prompt specification
Build or customize the Three.js 3D scene "Bridge Maintenance Scheduling Diorama" 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: Bridge Maintenance Scheduling Diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/bridge-maintenance-scheduling-diorama-x2111
Scene Description Prompt:
Use Three.js from cdnjs to build a fictional town with two bridges and a maintenance depot. Schedule closures so the remaining modeled network can still connect required destinations. Deliver one self-contained HTML file with visible voxel blocks.
Working Three.js Starter (bridge-maintenance-scheduling-diorama-x2111):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pixel Crossing — Voxel Infrastructure Control</title>
<style>
:root {
--ink: #172b32;
--muted: #65777d;
--panel: #f7f4ec;
--paper: #fffdf8;
--line: #d9ddd7;
--teal: #087f78;
--teal-dark: #075f5b;
--mint: #d7efe8;
--orange: #e66a45;
--orange-soft: #fde5dc;
--green: #247a55;
--green-soft: #e0f1e6;
--red: #b84c43;
--red-soft: #f7dfdc;
--yellow: #e8b84b;
--shadow: 0 18px 45px rgba(25, 53, 58, 0.16);
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bdd5cf;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button {
cursor: pointer;
}
#scene {
position: absolute;
inset: 0 390px 0 0;
overflow: hidden;
background: #bdd5cf;
}
#scene canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
z-index: 0;
}
#labels {
position: absolute;
inset: 0;
pointer-events: none;
z-index: 4;
overflow: hidden;
}
.map-label {
position: absolute;
transform: translate(-50%, -100%);
min-width: 108px;
padding: 7px 9px 6px;
border: 1px solid rgba(23, 43, 50, 0.2);
border-radius: 5px;
background: rgba(255, 253, 248, 0.94);
box-shadow: 0 5px 16px rgba(27, 57, 61, 0.15);
pointer-events: auto;
cursor: pointer;
user-select: none;
transition: transform 0.15s ease, background 0.15s ease;
backdrop-filter: blur(8px);
}
.map-label:hover {
transform: translate(-50%, -103%);
background: #ffffff;
}
.map-label.selected {
color: #ffffff;
background: var(--teal-dark);
border-color: var(--teal-dark);
}
.map-label .label-name {
display: block;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.055em;
text-transform: uppercase;
white-space: nowrap;
}
.map-label .label-detail {
display: block;
margin-top: 2px;
color: var(--muted);
font: 10px var(--mono);
white-space: nowrap;
}
.map-label.selected .label-detail {
color: #c6e8df;
}
.map-label.bridge-label {
cursor: default;
background: rgba(27, 52, 59, 0.9);
color: #ffffff;
border-color: rgba(255, 255, 255, 0.15);
}
.map-label.bridge-label.closed {
background: rgba(151, 55, 46, 0.94);
}
.map-label.bridge-label .label-detail {
color: #bed0d3;
}
.map-label.bridge-label.closed .label-detail {
color: #ffe0d9;
}
#masthead {
position: absolute;
z-index: 6;
top: 22px;
left: 24px;
pointer-events: none;
}
.brand-line {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
}
.brand-mark {
display: grid;
grid-template-columns: repeat(3, 5px);
grid-template-rows: repeat(3, 5px);
gap: 2px;
padding: 7px;
border-radius: 5px;
background: var(--ink);
box-shadow: 0 7px 18px rgba(23, 43, 50, 0.2);
}
.brand-mark i {
display: block;
width: 5px;
height: 5px;
background: #91d4c5;
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(5),
.brand-mark i:nth-child(8) {
background: #f3bd54;
}
.brand-mark i:nth-child(3),
.brand-mark i:nth-child(7) {
background: #e66a45;
}
.brand-title {
color: var(--ink);
font-size: 18px;
font-weight: 900;
letter-spacing: -0.035em;
}
.brand-subtitle {
margin-top: 2px;
color: rgba(23, 43, 50, 0.67);
font: 10px var(--mono);
letter-spacing: 0.1em;
text-transform: uppercase;
}
.town-tag {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(23, 43, 50, 0.14);
border-radius: 5px;
background: rgba(255, 253, 248, 0.78);
color: rgba(23, 43, 50, 0.74);
font: 10px var(--mono);
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.town-tag::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 3px rgba(36, 122, 85, 0.15);
}
#panel {
position: absolute;
z-index: 20;
top: 0;
right: 0;
bottom: 0;
width: 390px;
overflow-y: auto;
padding: 24px 22px 28px;
border-left: 1px solid rgba(23, 43, 50, 0.13);
background:
linear-gradient(rgba(247, 244, 236, 0.96), rgba(247, 244, 236, 0.96)),
repeating-linear-gradient(
90deg,
transparent 0,
transparent 12px,
rgba(23, 43, 50, 0.025) 12px,
rgba(23, 43, 50, 0.025) 13px
);
box-shadow: -14px 0 40px rgba(25, 55, 59, 0.1);
scrollbar-width: thin;
scrollbar-color: #b8c3bd transparent;
}
.panel-kicker {
color: var(--teal);
font: 10px var(--mono);
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 7px;
font-size: 29px;
line-height: 1.02;
letter-spacing: -0.05em;
}
.intro {
margin: 0 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.status-card {
position: relative;
overflow: hidden;
padding: 15px;
border: 1px solid #b9d8c5;
border-radius: 7px;
background: var(--green-soft);
}
.status-card::after {
content: "";
position: absolute;
width: 76px;
height: 76px;
right: -20px;
top: -22px;
border: 12px solid rgba(36, 122, 85, 0.06);
transform: rotate(18deg);
}
.status-card.failed {
border-color: #dfaaa4;
background: var(--red-soft);
}
.status-top {
display: flex;
align-items: center;
gap: 9px;
font-size: 13px;
font-weight: 850;
}
.status-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(36, 122, 85, 0.13);
}
.failed .status-dot {
background: var(--red);
box-shadow: 0 0 0 4px rgba(184, 76, 67, 0.13);
}
.status-detail {
margin-top: 7px;
color: #526c60;
font-size: 11px;
line-height: 1.45;
}
.failed .status-detail {
color: #865953;
}
.section {
margin-top: 22px;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.section-heading h2 {
margin: 0;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-heading .section-note {
color: var(--muted);
font: 9px var(--mono);
text-transform: uppercase;
}
.destination-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.destination-button {
display: flex;
align-items: center;
gap: 8px;
min-height: 42px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 5px;
background: var(--paper);
color: var(--ink);
text-align: left;
font-size: 11px;
font-weight: 750;
transition: border-color 0.15s, background 0.15s, transform 0.15s;
}
.destination-button:hover {
border-color: #8fa9a2;
transform: translateY(-1px);
}
.destination-button.selected {
color: #ffffff;
border-color: var(--teal-dark);
background: var(--teal-dark);
}
.destination-button.unreachable {
border-color: #dda49e;
background: #fff4f2;
}
.destination-icon {
display: grid;
place-items: center;
flex: 0 0 auto;
width: 21px;
height: 21px;
border-radius: 3px;
color: var(--teal-dark);
background: var(--mint);
font: 10px var(--mono);
font-weight: 900;
}
.selected .destination-icon {
color: var(--teal-dark);
background: #bce4d7;
}
.bridge-list {
display: grid;
gap: 8px;
}
.bridge-card {
padding: 12px;
border: 1px solid var(--line);
border-radius: 6px;
background: var(--paper);
}
.bridge-card.closed {
border-color: #e0a49c;
background: #fff8f5;
}
.bridge-card-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 10px;
}
.bridge-name {
font-size: 12px;
font-weight: 850;
}
.bridge-window {
margin-top: 3px;
color: var(--muted);
font: 10px var(--mono);
}
.bridge-state {
flex: 0 0 auto;
padding: 4px 6px;
border-radius: 3px;
color: var(--green);
background: var(--green-soft);
font: 9px var(--mono);
font-weight: 900;
letter-spacing: 0.06em;
}
.bridge-card.closed .bridge-state {
color: var(--red);
background: var(--red-soft);
}
.bridge-card.scheduled .bridge-state {
color: #856514;
background: #f8edc8;
}
.bridge-actions {
display: flex;
gap: 6px;
margin-top: 10px;
}
.mini-button {
padding: 5px 8px;
border: 1px solid var(--line);
border-radius: 4px;
background: transparent;
color: var(--muted);
font: 9px var(--mono);
font-weight: 750;
}
.mini-button:hover {
color: var(--ink);
border-color: #9eaea8;
background: #f2f2ec;
}
.mini-button.danger:hover {
color: var(--red);
border-color: #dfaaa4;
background: var(--red-soft);
}
.schedule-form {
margin-top: 9px;
padding: 13px;
border: 1px dashed #b6c3bd;
border-radius: 6px;
background: rgba(255, 253, 248, 0.65);
}
.form-row {
display: grid;
grid-template-columns: 1.1fr 1fr 1fr;
gap: 7px;
}
label.field-label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font: 9px var(--mono);
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
}
select,
input[type="time"] {
width: 100%;
min-width: 0;
height: 35px;
padding: 6px 7px;
border: 1px solid var(--line);
border-radius: 4px;
outline: none;
background: var(--paper);
color: var(--ink);
font: 10px var(--mono);
}
select:focus,
input[type="time"]:focus {
border-color: var(--teal);
box-shadow: 0 0 0 3px rgba(8, 127, 120, 0.1);
}
.primary-button {
width: 100%;
height: 37px;
margin-top: 10px;
border: 0;
border-radius: 4px;
color: #ffffff;
background: var(--teal-dark);
font-size: 11px;
font-weight: 850;
box-shadow: 0 6px 14px rgba(7, 95, 91, 0.15);
}
.primary-button:hover {
background: #064e4b;
}
.primary-button:disabled {
cursor: not-allowed;
opacity: 0.55;
}
.form-message {
min-height: 15px;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.form-message.error {
color: var(--red);
}
.form-message.success {
color: var(--green);
}
.recommended-button {
width: 100%;
margin-top: 8px;
padding: 7px;
border: 1px solid var(--line);
border-radius: 4px;
color: var(--muted);
background: transparent;
font: 9px var(--mono);
font-weight: 800;
}
.recommended-button:hover {
color: var(--ink);
background: #eeeee7;
}
.route-card {
padding: 13px;
border: 1px solid #b7d6ce;
border-radius: 6px;
background: #eaf5f0;
}
.route-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
font-size: 11px;
font-weight: 850;
}
.route-badge {
padding: 3px 6px;
border-radius: 3px;
color: var(--teal-dark);
background: #cce9df;
font: 9px var(--mono);
}
.route-text {
margin-top: 8px;
color: #456c65;
font: 10px/1.55 var(--mono);
}
.maintenance-note {
display: flex;
gap: 9px;
margin-top: 16px;
padding: 11px;
border-radius: 5px;
color: #65716e;
background: #ebe9e0;
font-size: 10px;
line-height: 1.45;
}
.maintenance-note strong {
color: var(--ink);
}
#timeline {
position: absolute;
z-index: 8;
left: 24px;
right: 24px;
bottom: 20px;
padding: 13px 16px 12px;
border: 1px solid rgba(23, 43, 50, 0.15);
border-radius: 7px;
background: rgba(255, 253, 248, 0.91);
box-shadow: 0 12px 35px rgba(30, 60, 63, 0.14);
backdrop-filter: blur(12px);
}
.timeline-top {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 9px;
}
.clock {
min-width: 81px;
font: 800 19px var(--mono);
letter-spacing: -0.05em;
}
.clock small {
display: block;
margin-top: 1px;
color: var(--muted);
font: 8px var(--mono);
letter-spacing: 0.08em;
}
.play-button {
display: grid;
place-items: center;
width: 31px;
height: 31px;
border: 1px solid var(--line);
border-radius: 4px;
color: var(--ink);
background: #ffffff;
font-size: 12px;
}
.timeline-legend {
display: flex;
align-items: center;
gap: 13px;
margin-left: auto;
color: var(--muted);
font: 9px var(--mono);
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 5px;
}
.legend-swatch {
width: 11px;
height: 7px;
border-radius: 1px;
background: #d6a13b;
}
.legend-swatch.route {
background: #20bda7;
}
.timeline-track {
position: relative;
height: 28px;
border: 1px solid #d3d8d2;
border-radius: 4px;
background:
repeating-linear-gradient(
90deg,
rgba(23, 43, 50, 0.08) 0,
rgba(23, 43, 50, 0.08) 1px,
transparent 1px,
transparent 12.5%
),
#f1f0e9;
}
.closure-window {
position: absolute;
top: 8px;
height: 10px;
min-width: 4px;
border-radius: 2px;
background: var(--yellow);
opacity: 0.86;
pointer-events: none;
}
#timeline input[type="range"] {
position: absolute;
inset: 0;
z-index: 3;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: ew-resize;
}
#currentMarker {
position: absolute;
z-index: 2;
top: -4px;
bottom: -4px;
width: 2px;
margin-left: -1px;
background: var(--teal-dark);
box-shadow: 0 0 0 2px rgba(255, 253, 248, 0.8);
pointer-events: none;
}
#currentMarker::before {
content: "";
position: absolute;
left: 50%;
top: -3px;
width: 7px;
height: 7px;
transform: translateX(-50%) rotate(45deg);
background: var(--teal-dark);
}
.time-ticks {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #84908c;
font: 8px var(--mono);
}
#instructions {
position: absolute;
z-index: 5;
left: 24px;
bottom: 132px;
padding: 6px 9px;
border-radius: 4px;
color: rgba(23, 43, 50, 0.68);
background: rgba(255, 253, 248, 0.67);
font: 9px var(--mono);
pointer-events: none;
backdrop-filter: blur(7px);
}
#toast {
position: fixed;
z-index: 50;
left: 50%;
bottom: 28px;
max-width: min(460px, 90vw);
padding: 12px 16px;
transform: translate(-50%, 20px);
opacity: 0;
border-radius: 5px;
color: #ffffff;
background: #173d42;
box-shadow: var(--shadow);
font-size: 11px;
line-height: 1.45;
pointer-events: none;
transition: opacity 0.2s, transform 0.2s;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
background: #8f3e38;
}
@media (max-width: 1050px) {
#scene {
right: 350px;
}
#panel {
width: 350px;
padding-inline: 18px;
}
#timeline {
left: 16px;
right: 16px;
}
#instructions {
left: 16px;
}
}
@media (max-width: 760px) {
#scene {
inset: 0 0 45vh 0;
}
#panel {
top: auto;
width: 100%;
height: 45vh;
border-top: 1px solid rgba(23, 43, 50, 0.15);
border-left: 0;
box-shadow: 0 -12px 35px rgba(25, 55, 59, 0.12);
}
#masthead {
top: 12px;
left: 14px;
}
.brand-title {
font-size: 15px;
}
.brand-subtitle,
.town-tag {
display: none;
}
#timeline {
left: 10px;
right: 10px;
bottom: 10px;
padding: 9px 12px;
}
#instructions {
display: none;
}
.timeline-legend {
display: none;
}
.clock {
min-width: 67px;
font-size: 16px;
}
}
</style>
</head>
<body>
<div id="scene">
<div id="labels"></div>
<header id="masthead">
<div class="brand-line">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
<i></i><i></i><i></i>
<i></i><i></i><i></i>
</div>
<div>
<div class="brand-title">Pixel Crossing</div>
<div class="brand-subtitle">Voxel infrastructure district</div>
</div>
</div>
<div class="town-tag">Fictional town · operations live</div>
</header>
<div id="instructions">Drag to orbit · wheel to zoom · shift-drag to pan · click a destination</div>
<section id="timeline" aria-label="Bridge closure timeline">
<div class="timeline-top">
<div class="clock">
<span id="timeDisplay">08:30</span>
<small>OPERATING CLOCK</small>
</div>
<button class="play-button" id="playToggle" title="Advance operating clock" aria-label="Play operating clock">▶</button>
<div class="timeline-legend">
<div class="legend-item"><span class="legend-swatch"></span> closure window</div>
<div class="legend-item"><span class="legend-swatch route"></span> active route</div>
</div>
</div>
<div class="timeline-track" id="timelineTrack">
<div class="closure-window" id="northWindow"></div>
<div class="closure-window" id="southWindow"></div>
<div id="currentMarker"></div>
<input id="timeSlider" type="range" min="300" max="1320" step="15" value="510" aria-label="Operating time">
</div>
<div class="time-ticks">
<span>05:00</span>
<span>08:00</span>
<span>11:00</span>
<span>14:00</span>
<span>17:00</span>
<span>20:00</span>
<span>22:00</span>
</div>
</section>
</div>
<aside id="panel">
<div class="panel-kicker">Municipal Works / District 07</div>
<h1>Bridge operations</h1>
<p class="intro">
Coordinate maintenance without stranding the Town Hall, Clinic, Market, or Depot.
Closures are accepted only while every required destination remains reachable.
</p>
<div class="status-card" id="networkStatus">
<div class="status-top">
<span class="status-dot"></span>
<span id="statusTitle">Checking network…</span>
</div>
<div class="status-detail" id="statusDetail">Modeled road graph is loading.</div>
</div>
<section class="section">
<div class="section-heading">
<h2>Required destinations</h2>
<span class="section-note">select route</span>
</div>
<div class="destination-grid" id="destinationGrid">
<button class="destination-button" data-destination="depot">
<span class="destination-icon">D</span> Maintenance Depot
</button>
<button class="destination-button" data-destination="townhall">
<span class="destination-icon">T</span> Town Hall
</button>
<button class="destination-button" data-destination="clinic">
<span class="destination-icon">C</span> East Clinic
</button>
<button class="destination-button" data-destination="market">
<span class="destination-icon">M</span> Market
</button>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Closure schedule</h2>
<span class="section-note">2 modeled spans</span>
</div>
<div class="bridge-list">
<article class="bridge-card" id="northCard">
<div class="bridge-card-head">
<div>
<div class="bridge-name">North Span</div>
<div class="bridge-window" id="northWindowText">06:00–10:00</div>
</div>
<span class="bridge-state" id="northState">CLOSED NOW</span>
</div>
<div class="bridge-actions">
<button class="mini-button" data-edit-bridge="north">Edit closure</button>
<button class="mini-button danger" data-clear-bridge="north">Clear</button>
</div>
</article>
<article class="bridge-card" id="southCard">
<div class="bridge-card-head">
<div>
<div class="bridge-name">South Span</div>
<div class="bridge-window" id="southWindowText">14:00–18:00</div>
</div>
<span class="bridge-state" id="southState">OPEN</span>
</div>
<div class="bridge-actions">
<button class="mini-button" data-edit-bridge="south">Edit closure</button>
<button class="mini-button danger" data-clear-bridge="south">Clear</button>
</div>
</article>
</div>
<div class="schedule-form">
<div class="form-row">
<div>
<label class="field-label" for="bridgeSelect">Span</label>
<select id="bridgeSelect">
<option value="north">North Span</option>
<option value="south">South Span</option>
</select>
</div>
<div>
<label class="field-label" for="startTime">From</label>
<input id="startTime" type="time" value="11:00">
</div>
<div>
<label class="field-label" for="endTime">Until</label>
<input id="endTime" type="time" value="12:00">
</div>
</div>
<button class="primary-button" id="applySchedule">Schedule safely</button>
<div class="form-message" id="formMessage">
The recommended plan never overlaps both bridge closures.
</div>
<button class="recommended-button" id="resetSchedule">RESTORE RECOMMENDED PLAN</button>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Active service route</h2>
<span class="section-note">from depot</span>
</div>
<div class="route-card">
<div class="route-title">
<span id="routeDestination">East Clinic</span>
<span class="route-badge" id="routeBadge">LIVE</span>
</div>
<div class="route-text" id="routeText">Calculating safest available route…</div>
</div>
</section>
<div class="maintenance-note">
<div style="font-size:17px">▣</div>
<div>
<strong>Maintenance Depot</strong><br>
Crews, spare blocks, and route barriers are staged here. The depot remains
reachable from either bank while at least one span is open.
</div>
</div>
</aside>
<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";
const sceneElement = document.getElementById("scene");
const labelsElement = document.getElementById("labels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbdd5cf);
scene.fog = new THREE.Fog(0xbdd5cf, 54, 92);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
sceneElement.insertBefore(renderer.domElement, sceneElement.firstChild);
const camera = new THREE.OrthographicCamera(-20, 20, 14, -14, 0.1, 150);
camera.position.set(27, 25, 31);
const hemi = new THREE.HemisphereLight(0xe7f5f0, 0x65776e, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0cc, 3.2);
sun.position.set(-18, 28, 19);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x91c9df, 0.65);
fill.position.set(18, 9, -16);
scene.add(fill);
const material = (color, roughness = 0.85, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const mats = {
platform: material(0x6f817b),
platformSide: material(0x52635f),
grass: [
material(0x8fb47d),
material(0x9cbd88),
material(0x87aa78),
material(0xa5c391)
],
soil: material(0x9f9874),
water: [
material(0x4c9fad, 0.36, 0.05),
material(0x5aabb6, 0.34, 0.05),
material(0x438f9f, 0.4, 0.04)
],
road: material(0x425257),
roadEdge: material(0xa6aaa0),
marking: material(0xe9d89e),
bridgeDeck: material(0xa9784d),
bridgeRoad: material(0x3e5155),
rail: material(0x765039),
cream: material(0xe7d8b5),
white: material(0xf2eee2),
teal: material(0x2d8b85),
tealRoof: material(0x246b69),
orange: material(0xd96946),
yellow: material(0xe4b84f),
gray: material(0x82928e),
dark: material(0x37484c),
glass: new THREE.MeshStandardMaterial({
color: 0x8fc6cf,
roughness: 0.25,
metalness: 0.15,
emissive: 0x183e46,
emissiveIntensity: 0.25
}),
lamp: new THREE.MeshStandardMaterial({
color: 0xffe6a8,
emissive: 0xffc95c,
emissiveIntensity: 0.75
}),
trunk: material(0x73513c),
leaves: [
material(0x4e8a61),
material(0x5e9c69),
material(0x76a96f)
],
red: material(0xc84f43),
barrierWhite: material(0xf3eee0)
};
const clickableMeshes = [];
const waterTiles = [];
const waveTiles = [];
const bridgeVisuals = {};
const routeVisuals = new Map();
const labels = [];
let selectedDestination = "clinic";
let currentMinutes = 510;
let playing = false;
let playTimer = null;
let toastTimer = null;
const schedules = {
north: { start: 360, end: 600 },
south: { start: 840, end: 1080 }
};
const destinations = {
depot: {
name: "Maintenance Depot",
short: "Depot",
icon: "D",
node: [-6.35, 8],
building: [-4.9, 8],
label: [-4.9, 4.2, 8]
},
townhall: {
name: "Town Hall",
short: "Town Hall",
icon: "T",
node: [-6.35, -10],
building: [-4.9, -10],
label: [-4.9, 5.0, -10]
},
clinic: {
name: "East Clinic",
short: "Clinic",
icon: "C",
node: [6.35, -10],
building: [4.9, -10],
label: [4.9, 4.7, -10]
},
market: {
name: "Market",
short: "Market",
icon: "M",
node: [6.35, -2],
building: [4.9, -2],
label: [4.9, 4.2, -2]
}
};
const nodes = {
nw: [-8, -10],
wm: [-8, -2],
sw: [-8, 8],
ne: [8, -10],
em: [8, -2],
se: [8, 8],
depot: destinations.depot.node,
townhall: destinations.townhall.node,
clinic: destinations.clinic.node,
market: destinations.market.node
};
const edges = [
{ id: "west-upper", a: "nw", b: "wm", type: "road", name: "West Wharf" },
{ id: "west-lower", a: "wm", b: "sw", type: "road", name: "Canal Road" },
{ id: "east-upper", a: "ne", b: "em", type: "road", name: "Hill Street" },
{ id: "east-lower", a: "em", b: "se", type: "road", name: "East Lane" },
{ id: "bridge-north", a: "nw", b: "ne", type: "bridge", bridge: "north", name: "North Span" },
{ id: "bridge-south", a: "sw", b: "se", type: "bridge", bridge: "south", name: "South Span" },
{ id: "depot-access", a: "sw", b: "depot", type: "road", name: "Depot Access" },
{ id: "hall-walk", a: "nw", b: "townhall", type: "road", name: "Civic Walk" },
{ id: "clinic-walk", a: "ne", b: "clinic", type: "road", name: "Clinic Walk" },
{ id: "market-walk", a: "em", b: "market", type: "road", name: "Market Walk" }
];
function addBox(width, height, depth, x, y, z, mat, parent = scene, cast = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function segmentBox(a, b, width, height, y, mat, parent = scene) {
const dx = b[0] - a[0];
const dz = b[1] - a[1];
const length = Math.hypot(dx, dz);
const horizontal = Math.abs(dx) >= Math.abs(dz);
const mesh = addBox(
horizontal ? length : width,
height,
horizontal ? width : length,
(a[0] + b[0]) / 2,
y,
(a[1] + b[1]) / 2,
mat,
parent,
false
);
return mesh;
}
function markClickable(object, destination) {
object.traverse((child) => {
if (child.isMesh) {
child.userData.destination = destination;
clickableMeshes.push(child);
}
});
}
// Deterministic scenery generation.
let seed = 7319;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function buildTerrain() {
addBox(27.8, 0.9, 25.8, 0, -0.58, 0, mats.platformSide);
addBox(27.2, 0.18, 25.2, 0, -0.08, 0, mats.soil, scene, false);
for (let z = -11; z <= 11; z++) {
for (let x = -12; x <= 12; x++) {
if (Math.abs(x) <= 1) {
const waterMat = mats.water[Math.abs(z) % mats.water.length];
const tile = addBox(1.02, 0.2, 1.02, x, -0.015, z, waterMat, scene, false);
tile.userData.baseY = tile.position.y;
tile.userData.phase = (x + 20) * 0.31 + z * 0.19;
waterTiles.push(tile);
} else {
const colorIndex = Math.floor(random() * mats.grass.length);
addBox(1.01, 0.24, 1.01, x, 0.02, z, mats.grass[colorIndex], scene, false);
}
}
}
// Small animated water highlights.
for (let i = 0; i < 15; i++) {
const wave = addBox(
0.18 + random() * 0.3,
0.025,
0.45 + random() * 0.6,
-0.7 + random() * 1.4,
0.105,
-10.8 + random() * 21.6,
new THREE.MeshBasicMaterial({
color: 0xb9e2df,
transparent: true,
opacity: 0.28 + random() * 0.2
}),
scene,
false
);
wave.userData.speed = 0.22 + random() * 0.35;
wave.userData.baseX = wave.position.x;
waveTiles.push(wave);
}
}
function buildRoads() {
// Main north-south avenues.
[-8, 8].forEach((x) => {
addBox(2.65, 0.16, 19.2, x, 0.16, -1, mats.roadEdge, scene, false);
addBox(2.25, 0.15, 19.2, x, 0.255, -1, mats.road, scene, false);
for (let z = -9; z <= 7; z += 2.1) {
addBox(0.12, 0.025, 0.7, x, 0.345, z, mats.marking, scene, false);
}
});
// Local access streets.
const localRoads = [
[-8, -6.55, -10],
[8, 6.55, -10],
[8, 6.55, -2],
[-8, -6.55, 8]
];
localRoads.forEach(([x1, x2, z]) => {
addBox(Math.abs(x2 - x1), 0.13, 1.35, (x1 + x2) / 2, 0.21, z, mats.road, scene, false);
});
// Outer lanes add town density without changing the modeled graph.
[-10.7, 10.7].forEach((x) => {
addBox(1.15, 0.12, 21.2, x, 0.19, -0.5, material(0x65716c), scene, false);
});
}
function buildBridge(z, key) {
const group = new THREE.Group();
group.name = key === "north" ? "North Span" : "South Span";
scene.add(group);
addBox(18.3, 0.34, 2.5, 0, 0.35, z, mats.bridgeDeck, group);
const roadMaterial = mats.bridgeRoad.clone();
addBox(17.7, 0.15, 1.72, 0, 0.585, z, roadMaterial, group, false);
// Exposed edge planks.
for (let x = -8.3; x <= 8.3; x += 0.72) {
addBox(0.11, 0.07, 2.42, x, 0.54, z, mats.bridgeDeck, group, false);
}
// Railings and posts.
[-1.16, 1.16].forEach((offset) => {
addBox(18.2, 0.13, 0.13, 0, 1.35, z + offset, mats.rail, group);
for (let x = -8.5; x <= 8.5; x += 1.7) {
addBox(0.17, 1.05, 0.17, x, 0.91, z + offset, mats.rail, group);
}
});
// Stone abutments.
[-8.65, 8.65].forEach((x) => {
addBox(0.9, 0.55, 2.8, x, 0.25, z, mats.gray, group);
});
const barrier = new THREE.Group();
group.add(barrier);
[-7.15, 7.15].forEach((x) => {
addBox(0.16, 1.05, 0.16, x, 0.85, z - 0.75, mats.dark, barrier);
addBox(0.16, 1.05, 0.16, x, 0.85, z + 0.75, mats.dark, barrier);
for (let i = 0; i < 6; i++) {
addBox(
0.24,
0.28,
0.42,
x,
1.05,
z - 0.85 + i * 0.34,
i % 2 === 0 ? mats.red : mats.barrierWhite,
barrier
);
}
});
bridgeVisuals[key] = { group, barrier, roadMaterial };
}
function addWindows(group, width, depth, height, count = 3) {
const usable = Math.max(1, Math.floor((width - 0.45) / 0.7));
const total = Math.min(count, usable);
const spacing = width / (total + 1);
for (let i = 0; i < total; i++) {
const x = -width / 2 + spacing * (i + 1);
addBox(
0.34,
0.37,
0.055,
x,
0.1 + height * 0.6,
depth / 2 + 0.035,
mats.glass,
group,
false
);
}
}
function buildBuilding({
x,
z,
width = 2.4,
depth = 2.4,
height = 2.4,
body = mats.cream,
roof = mats.tealRoof,
destination = null,
windows = true
}) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
addBox(width, height, depth, 0, 0.1 + height / 2, 0, body, group);
addBox(width + 0.22, 0.3, depth + 0.22, 0, 0.1 + height + 0.15, 0, roof, group);
addBox(
Math.max(0.8, width - 0.65),
0.45,
Math.max(0.8, depth - 0.65),
0,
0.1 + height + 0.5,
0,
roof,
group
);
if (windows) addWindows(group, width, depth, height, Math.max(2, Math.floor(width / 0.75)));
addBox(0.48, 0.8, 0.07, 0, 0.5, depth / 2 + 0.045, mats.dark, group, false);
if (destination) markClickable(group, destination);
return group;
}
function buildTownHall() {
const group = buildBuilding({
x: destinations.townhall.building[0],
z: destinations.townhall.building[1],
width: 3.2,
depth: 2.8,
height: 2.65,
body: mats.cream,
roof: mats.tealRoof,
destination: "townhall"
});
addBox(1.15, 2.0, 1.15, 0, 3.55, 0, mats.cream, group);
addBox(1.45, 0.25, 1.45, 0, 4.67, 0, mats.tealRoof, group);
addBox(0.06, 0.78, 0.42, 0, 4.98, 0, mats.yellow, group);
addBox(0.42, 0.06, 0.42, 0, 4.92, 0, mats.yellow, group);
}
function buildClinic() {
const group = buildBuilding({
x: destinations.clinic.building[0],
z: destinations.clinic.building[1],
width: 3.15,
depth: 2.7,
height: 2.35,
body: mats.white,
roof: mats.gray,
destination: "clinic"
});
addBox(1.05, 0.09, 0.34, 0, 3.03, 0, mats.red, group, false);
addBox(0.34, 0.09, 1.05, 0, 3.03, 0, mats.red, group, false);
addBox(0.75, 0.1, 0.6, 0, 3.6, 0, mats.white, group);
}
function buildMarket() {
const group = buildBuilding({
x: destinations.market.building[0],
z: destinations.market.building[1],
width: 3.1,
depth: 2.7,
height: 2.1,
body: mats.teal,
roof: mats.orange,
destination: "market"
});
for (let i = 0; i < 5; i++) {
addBox(
0.46,
0.18,
0.65,
-1.05 + i * 0.52,
1.65,
1.58,
i % 2 === 0 ? mats.yellow : mats.barrierWhite,
group
);
}
addBox(0.65, 0.45, 0.65, 0.82, 3.02, -0.3, mats.bridgeDeck, group);
}
function buildDepot() {
const group = buildBuilding({
x: destinations.depot.building[0],
z: destinations.depot.building[1],
width: 3.9,
depth: 3.25,
height: 2.25,
body: mats.gray,
roof: mats.dark,
destination: "depot"
});
for (let i = 0; i < 3; i++) {
addBox(0.82, 1.05, 0.08, -1.15 + i * 1.15, 0.73, 1.67, mats.dark, group, false);
addBox(0.62, 0.08, 0.09, -1.15 + i * 1.15, 1.25, 1.72, mats.yellow, group, false);
}
// Stacked maintenance blocks.
addBox(0.65, 0.55, 0.65, 1.05, 3.02, -0.85, mats.orange, group);
addBox(0.65, 0.55, 0.65, 1.05, 3.57, -0.85, mats.yellow, group);
// Small gantry crane.
addBox(0.16, 2.9, 0.16, -1.45, 1.85, -1.15, mats.dark, group);
addBox(0.16, 2.9, 0.16, 1.25, 1.85, -1.15, mats.dark, group);
addBox(2.95, 0.18, 0.22, -0.1, 3.28, -1.15, mats.yellow, group);
addBox(0.06, 0.68, 0.06, 0.72, 2.95, -1.15, mats.dark, group);
addBox(0.45, 0.3, 0.45, 0.72, 2.47, -1.15, mats.orange, group);
}
function buildTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.1, z);
scene.add(group);
addBox(0.32 * scale, 1.0 * scale, 0.32 * scale, 0, 0.5 * scale, 0, mats.trunk, group);
addBox(
1.05 * scale,
0.9 * scale,
1.05 * scale,
0,
1.3 * scale,
0,
mats.leaves[Math.floor(random() * mats.leaves.length)],
group
);
addBox(
0.72 * scale,
0.62 * scale,
0.72 * scale,
0.16 * scale,
1.82 * scale,
0.08 * scale,
mats.leaves[Math.floor(random() * mats.leaves.length)],
group
);
}
function buildScenery() {
buildTerrain();
buildRoads();
buildBridge(-10, "north");
buildBridge(8, "south");
buildTownHall();
buildClinic();
buildMarket();
buildDepot();
const houses = [
[-4.25, -6.4, 2.2, 2.5, 2.0, mats.cream, mats.orange],
[-4.35, -2.0, 2.5, 2.2, 2.5, mats.yellow, mats.tealRoof],
[-4.1, 2.1, 2.4, 2.4, 2.1, mats.white, mats.orange],
[-4.2, 5.5, 2.6, 2.2, 2.7, mats.cream, mats.tealRoof],
[4.25, -6.2, 2.4, 2.5, 2.2, mats.yellow, mats.tealRoof],
[4.35, -4.0, 2.0, 1.8, 1.8, mats.orange, mats.cream],
[4.2, 2.0, 2.3, 2.2, 2.4, mats.cream, mats.tealRoof],
[4.1, 5.2, 2.5, 2.3, 2.0, mats.white, mats.orange],
[-10.55, -7.7, 1.65, 2.0, 2.5, mats.white, mats.tealRoof],
[-10.55, -3.5, 1.7, 2.1, 1.9, mats.yellow, mats.orange],
[-10.55, 1.2, 1.7, 2.0, 2.8, mats.cream, mats.tealRoof],
[-10.55, 5.4, 1.65, 2.0, 2.1, mats.orange, mats.cream],
[10.55, -7.0, 1.65, 2.2, 2.3, mats.cream, mats.orange],
[10.55, -3.2, 1.7, 2.1, 2.8, mats.teal, mats.cream],
[10.55, 1.2, 1.7, 2.1, 1.9, mats.yellow, mats.tealRoof],
[10.55, 5.4, 1.65, 2.2, 2.4, mats.white, mats.orange]
];
houses.forEach(([x, z, w, d, h, body, roof]) => {
buildBuilding({ x, z, width: w, depth: d, height: h, body, roof });
});
const trees = [
[-6.7, -7.2], [-2.5, -7.5], [-2.5, -4.2], [-6.5, 0],
[-2.7, 0.4], [-6.5, 3.5], [-2.7, 4.7], [2.4, -7.4],
[2.5, -4.6], [6.6, -5.6], [2.7, 0.5], [6.6, 0.8],
[2.4, 3.2], [6.7, 4.2], [-8.1, -6.5], [8.1, 5.4]
];
trees.forEach(([x, z], index) => buildTree(x, z, 0.72 + (index % 3) * 0.12));
// Street lamps.
[-9.35, 9.35].forEach((x) => {
[-7.5, -0.5, 5.6].forEach((z) => {
addBox(0.12, 1.75, 0.12, x, 1.2, z, mats.dark);
addBox(0.4, 0.17, 0.4, x, 2.08, z, mats.lamp, scene, false);
});
});
}
function createRouteOverlays() {
const routeMaterial = new THREE.MeshBasicMaterial({
color: 0x20c9b0,
transparent: true,
opacity: 0.9,
depthTest: false,
depthWrite: false
});
edges.forEach((edge) => {
const a = nodes[edge.a];
const b = nodes[edge.b];
const y = edge.type === "bridge" ? 0.73 : 0.355;
const mesh = segmentBox(a, b, 0.38, 0.035, y, routeMaterial.clone());
mesh.visible = false;
mesh.renderOrder = 20;
routeVisuals.set(edge.id, mesh);
});
}
function createVehicle(x, z, color, direction = 1) {
const group = new THREE.Group();
scene.add(group);
addBox(0.75, 0.35, 1.2, 0, 0.51, 0, material(color), group);
addBox(0.62, 0.3, 0.58, 0, 0.82, -0.05, mats.glass, group);
addBox(0.06, 0.12, 0.06, 0, 0.91, -0.05, mats.white, group, false);
group.position.set(x, 0, z);
return { group, direction, speed: 1.3 + random() * 0.8 };
}
const vehicles = [
createVehicle(-8, -5.7, 0xe9b84b, 1),
createVehicle(8, 4.5, 0xdc6545, -1)
];
function addLabel({ title, detail, position, destination = null, bridge = null }) {
const element = document.createElement("div");
element.className = "map-label";
if (bridge) element.classList.add("bridge-label");
element.innerHTML = `
<span class="label-name">${title}</span>
<span class="label-detail">${detail}</span>
`;
labelsElement.appendChild(element);
const label = {
element,
titleElement: element.querySelector(".label-name"),
detailElement: element.querySelector(".label-detail"),
world: new THREE.Vector3(...position),
destination,
bridge
};
if (destination) {
element.addEventListener("click", () => selectDestination(destination));
}
labels.push(label);
return label;
}
function buildLabels() {
Object.entries(destinations).forEach(([key, destination]) => {
addLabel({
title: destination.name,
detail: "required stop",
position: destination.label,
destination: key
});
});
addLabel({
title: "North Span",
detail: "open",
position: [0, 2.25, -10],
bridge: "north"
});
addLabel({
title: "South Span",
detail: "open",
position: [0, 2.25, 8],
bridge: "south"
});
}
function formatTime(minutes) {
const normalized = ((Math.round(minutes) % 1440) + 1440) % 1440;
const h = Math.floor(normalized / 60);
const m = normalized % 60;
return `${String(h).padStart(2, "0")}:${String(m).padStart(2, "0")}`;
}
function parseTime(value) {
const [h, m] = value.split(":").map(Number);
return h * 60 + m;
}
function isClosed(bridge, time = currentMinutes, scheduleSet = schedules) {
const schedule = scheduleSet[bridge];
if (!schedule || schedule.start === null || schedule.end === null) return false;
return time >= schedule.start && time < schedule.end;
}
function edgeIsOpen(edge, time = currentMinutes, scheduleSet = schedules) {
return edge.type !== "bridge" || !isClosed(edge.bridge, time, scheduleSet);
}
function shortestPath(start, target, time = currentMinutes, scheduleSet = schedules) {
const distances = {};
const previous = {};
const unvisited = new Set(Object.keys(nodes));
Object.keys(nodes).forEach((node) => {
distances[node] = Infinity;
previous[node] = null;
});
distances[start] = 0;
while (unvisited.size) {
let current = null;
let best = Infinity;
unvisited.forEach((node) => {
if (distances[node] < best) {
best = distances[node];
current = node;
}
});
if (current === null) break;
if (current === target) break;
unvisited.delete(current);
edges.forEach((edge) => {
if (!edgeIsOpen(edge, time, scheduleSet)) return;
let neighbor = null;
if (edge.a === current) neighbor = edge.b;
else if (edge.b === current) neighbor = edge.a;
if (neighbor && unvisited.has(neighbor)) {
const candidate = distances[current] + 1;
if (candidate < distances[neighbor]) {
distances[neighbor] = candidate;
previous[neighbor] = edge;
}
}
});
}
if (!Number.isFinite(distances[target])) return null;
const pathNodes = [target];
const pathEdges = [];
let cursor = target;
while (cursor !== start) {
const edge = previous[cursor];
if (!edge) return null;
pathEdges.unshift(edge);
cursor = edge.a === cursor ? edge.b : edge.a;
pathNodes.unshift(cursor);
}
return { nodes: pathNodes, edges: pathEdges };
}
function allDestinationsReachable(time = currentMinutes, scheduleSet = schedules) {
return Object.keys(destinations).every((destination) =>
Boolean(shortestPath("depot", destination, time, scheduleSet))
);
}
function routeWorldPoints(path) {
if (!path) return [];
return path.nodes.map((node) => {
const p = nodes[node];
return new THREE.Vector3(p[0], 0.92, p[1]);
});
}
const routePulse = addBox(
0.42,
0.42,
0.42,
0,
0.92,
0,
new THREE.MeshBasicMaterial({
color: 0xd8fff5,
transparent: true,
opacity: 0.95,
depthTest: false
}),
scene,
false
);
routePulse.renderOrder = 21;
let activeRoutePoints = [];
function updateRoute() {
routeVisuals.forEach((mesh) => {
mesh.visible = false;
});
const path = shortestPath("depot", selectedDestination);
activeRoutePoints = routeWorldPoints(path);
if (path) {
path.edges.forEach((edge) => {
routeVisuals.get(edge.id).visible = true;
});
}
}
function updateBridgeVisuals() {
["north", "south"].forEach((key) => {
const closed = isClosed(key);
const visual = bridgeVisuals[key];
visual.barrier.visible = closed;
visual.roadMaterial.color.set(closed ? 0xa84f45 : 0x3e5155);
visual.roadMaterial.emissive.set(closed ? 0x38100d : 0x000000);
visual.roadMaterial.emissiveIntensity = closed ? 0.28 : 0;
});
}
function updateBridgeCards() {
["north", "south"].forEach((key) => {
const card = document.getElementById(`${key}Card`);
const state = document.getElementById(`${key}State`);
const windowText = document.getElementById(`${key}WindowText`);
const schedule = schedules[key];
card.classList.remove("closed", "scheduled");
if (!schedule) {
state.textContent = "OPEN";
windowText.textContent = "No closure scheduled";
} else {
windowText.textContent = `${formatTime(schedule.start)}–${formatTime(schedule.end)}`;
if (isClosed(key)) {
state.textContent = "CLOSED NOW";
card.classList.add("closed");
} else if (currentMinutes < schedule.start) {
state.textContent = "SCHEDULED";
card.classList.add("scheduled");
} else {
state.textContent = "COMPLETE";
}
}
});
}
function updateNetworkStatus() {
const reachable = allDestinationsReachable();
const statusCard = document.getElementById("networkStatus");
const title = document.getElementById("statusTitle");
const detail = document.getElementById("statusDetail");
statusCard.classList.toggle("failed", !reachable);
const closedBridges = ["north", "south"].filter((bridge) => isClosed(bridge));
const openBridges = ["north", "south"].filter((bridge) => !isClosed(bridge));
if (reachable) {
title.textContent = "All 4 destinations reachable";
if (closedBridges.length === 1) {
const closedName = closedBridges[0] === "north" ? "North" : "South";
const openName = openBridges[0] === "north" ? "North" : "South";
detail.textContent = `${closedName} Span is closed. ${openName} Span is carrying all cross-river traffic.`;
} else if (closedBridges.length === 0) {
detail.textContent = "Both spans are available. The network has two viable bridge options.";
} else {
detail.textContent = "A modeled cross-river route is still available at the selected operating time.";
}
} else {
title.textContent = "Network disconnected";
detail.textContent = "No route connects the Maintenance Depot to every required destination.";
}
document.querySelectorAll("[data-destination]").forEach((button) => {
const destination = button.dataset.destination;
const reachableToDestination = Boolean(shortestPath("depot", destination));
button.classList.toggle("unreachable", !reachableToDestination);
button.classList.toggle("selected", destination === selectedDestination);
});
}
function updateRouteCard() {
const destination = destinations[selectedDestination];
const path = shortestPath("depot", selectedDestination);
document.getElementById("routeDestination").textContent = destination.name;
if (!path) {
document.getElementById("routeBadge").textContent = "NO ROUTE";
document.getElementById("routeText").textContent = "No open modeled route is currently available.";
return;
}
document.getElementById("routeBadge").textContent = "LIVE";
const names = path.edges.map((edge) => edge.name);
if (selectedDestination === "depot") {
document.getElementById("routeText").textContent = "Maintenance Depot · origin and crew staging area.";
} else {
document.getElementById("routeText").textContent = `Maintenance Depot → ${names.join(" → ")} → ${destination.short}`;
}
}
function updateTimeline() {
const start = 300;
const end = 1320;
const span = end - start;
document.getElementById("timeDisplay").textContent = formatTime(currentMinutes);
document.getElementById("timeSlider").value = currentMinutes;
document.getElementById("currentMarker").style.left =
`${((currentMinutes - start) / span) * 100}%`;
["north", "south"].forEach((key) => {
const element = document.getElementById(`${key}Window`);
const schedule = schedules[key];
if (!schedule) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left = `${((schedule.start - start) / span) * 100}%`;
element.style.width = `${((schedule.end - schedule.start) / span) * 100}%`;
element.style.background = key === "north" ? "#e5aa43" : "#d98a4c";
});
}
function updateLabels() {
labels.forEach((label) => {
if (label.destination) {
label.element.classList.toggle("selected", label.destination === selectedDestination);
}
if (label.bridge) {
const closed = isClosed(label.bridge);
label.element.classList.toggle("closed", closed);
label.detailElement.textContent = closed
? "closed now"
: schedules[label.bridge]
? `closed ${formatTime(schedules[label.bridge].start)}`
: "open";
}
});
}
function renderAll() {
updateBridgeVisuals();
updateBridgeCards();
updateNetworkStatus();
updateRoute();
updateRouteCard();
updateTimeline();
updateLabels();
}
function selectDestination(destination) {
selectedDestination = destination;
renderAll();
}
function showToast(message, type = "success") {
const toast = document.getElementById("toast");
clearTimeout(toastTimer);
toast.textContent = message;
toast.className = type === "error" ? "show error" : "show";
toastTimer = setTimeout(() => {
toast.className = "";
}, 3600);
}
function setFormMessage(message, type = "") {
const element = document.getElementById("formMessage");
element.textContent = message;
element.className = `form-message ${type}`;
}
function validateSchedule(candidateSchedules) {
const boundaryTimes = new Set([300, 1320]);
Object.values(candidateSchedules).forEach((schedule) => {
if (!schedule) return;
boundaryTimes.add(schedule.start);
boundaryTimes.add(schedule.end);
boundaryTimes.add(schedule.start + 1);
boundaryTimes.add(schedule.end - 1);
});
const sorted = [...boundaryTimes].sort((a, b) => a - b);
for (const time of sorted) {
if (!allDestinationsReachable(time, candidateSchedules)) {
return {
valid: false,
message: `Rejected: at ${formatTime(time)}, no open span would connect every required destination.`
};
}
}
return { valid: true };
}
document.querySelectorAll("[data-destination]").forEach((button) => {
button.addEventListener("click", () => selectDestination(button.dataset.destination));
});
document.querySelectorAll("[data-edit-bridge]").forEach((button) => {
button.addEventListener("click", () => {
const key = button.dataset.editBridge;
const schedule = schedules[key];
document.getElementById("bridgeSelect").value = key;
if (schedule) {
document.getElementById("startTime").value = formatTime(schedule.start);
document.getElementById("endTime").value = formatTime(schedule.end);
}
setFormMessage(`Editing ${key === "north" ? "North" : "South"} Span.`);
document.getElementById("startTime").focus();
});
});
document.querySelectorAll("[data-clear-bridge]").forEach((button) => {
button.addEventListener("click", () => {
const key = button.dataset.clearBridge;
schedules[key] = null;
renderAll();
setFormMessage(`${key === "north" ? "North" : "South"} Span returned to unrestricted service.`, "success");
showToast("Closure cleared. The bridge is open to traffic.");
});
});
document.getElementById("applySchedule").addEventListener("click", () => {
const key = document.getElementById("bridgeSelect").value;
const start = parseTime(document.getElementById("startTime").value);
const end = parseTime(document.getElementById("endTime").value);
if (!Number.isFinite(start) || !Number.isFinite(end)) {
setFormMessage("Enter a valid start and end time.", "error");
return;
}
if (end <= start) {
setFormMessage("The end time must be later than the start time.", "error");
return;
}
const candidate = {
north: key === "north" ? { start, end } : schedules.north,
south: key === "south" ? { start, end } : schedules.south
};
const validation = validateSchedule(candidate);
if (!validation.valid) {
setFormMessage(validation.message, "error");
showToast("Schedule rejected to preserve required connections.", "error");
return;
}
schedules[key] = { start, end };
renderAll();
setFormMessage(
`Scheduled safely from ${formatTime(start)} to ${formatTime(end)}. Every required destination remains connected.`,
"success"
);
showToast(`${key === "north" ? "North" : "South"} Span closure scheduled safely.`);
});
document.getElementById("resetSchedule").addEventListener("click", () => {
schedules.north = { start: 360, end: 600 };
schedules.south = { start: 840, end: 1080 };
currentMinutes = 510;
document.getElementById("timeSlider").value = currentMinutes;
renderAll();
setFormMessage("Recommended non-overlapping closure plan restored.", "success");
showToast("Recommended operating plan restored.");
});
document.getElementById("timeSlider").addEventListener("input", (event) => {
currentMinutes = Number(event.target.value);
renderAll();
});
document.getElementById("playToggle").addEventListener("click", () => {
playing = !playing;
document.getElementById("playToggle").textContent = playing ? "Ⅱ" : "▶";
clearInterval(playTimer);
if (playing) {
playTimer = setInterval(() => {
currentMinutes += 15;
if (currentMinutes > 1320) currentMinutes = 300;
renderAll();
}, 520);
}
});
// Custom camera controls, implemented without Three.js addons.
const controls = {
target: new THREE.Vector3(0, 0.2, 0),
yaw: 0.72,
pitch: 0.7,
distance: 39,
zoom: 1.06
};
let pointerState = null;
function updateCamera() {
const horizontal = Math.cos(controls.pitch) * controls.distance;
camera.position.set(
controls.target.x + Math.sin(controls.yaw) * horizontal,
controls.target.y + Math.sin(controls.pitch) * controls.distance,
controls.target.z + Math.cos(controls.yaw) * horizontal
);
camera.lookAt(controls.target);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointerState = {
id: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false,
shift: event.shiftKey
};
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerState || pointerState.id !== event.pointerId) return;
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
if (Math.hypot(event.clientX - pointerState.startX, event.clientY - pointerState.startY) > 4) {
pointerState.moved = true;
}
if (event.shiftKey || pointerState.shift) {
const scale = 0.025 / controls.zoom;
const right = new THREE.Vector3(Math.cos(controls.yaw), 0, -Math.sin(controls.yaw));
const forward = new THREE.Vector3(Math.sin(controls.yaw), 0, Math.cos(controls.yaw));
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(forward, -dy * scale);
controls.target.x = THREE.MathUtils.clamp(controls.target.x, -9, 9);
controls.target.z = THREE.MathUtils.clamp(controls.target.z, -8, 8);
} else {
controls.yaw -= dx * 0.006;
controls.pitch = THREE.MathUtils.clamp(controls.pitch + dy * 0.005, 0.3, 1.18);
}
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerState || pointerState.id !== event.pointerId) return;
if (!pointerState.moved) {
const bounds = renderer.domElement.getBoundingClientRect();
const pointer = new THREE.Vector2(
((event.clientX - bounds.left) / bounds.width) * 2 - 1,
-((event.clientY - bounds.top) / bounds.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(clickableMeshes, false);
if (hits.length && hits[0].object.userData.destination) {
selectDestination(hits[0].object.userData.destination);
}
}
pointerState = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState = null;
});
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.62,
2.2
);
updateCamera();
},
{ passive: false }
);
function resize() {
const width = sceneElement.clientWidth;
const height = sceneElement.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
const aspect = width / height;
const halfHeight = Math.max(13.5, 15.4 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
updateCamera();
}
new ResizeObserver(resize).observe(sceneElement);
const projected = new THREE.Vector3();
function updateLabelPositions() {
const width = sceneElement.clientWidth;
const height = sceneElement.clientHeight;
labels.forEach((label) => {
projected.copy(label.world).project(camera);
const visible = projected.z >= -1 && projected.z <= 1;
label.element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
});
}
function animateRoutePulse(time) {
if (activeRoutePoints.length < 2) {
routePulse.visible = false;
return;
}
routePulse.visible = true;
const segmentLengths = [];
let totalLength = 0;
for (let i = 0; i < activeRoutePoints.length - 1; i++) {
const length = activeRoutePoints[i].distanceTo(activeRoutePoints[i + 1]);
segmentLengths.push(length);
totalLength += length;
}
if (!totalLength) return;
let distance = (time * 0.0042) % totalLength;
let segment = 0;
while (segment < segmentLengths.length - 1 && distance > segmentLengths[segment]) {
distance -= segmentLengths[segment];
segment++;
}
const start = activeRoutePoints[segment];
const end = activeRoutePoints[segment + 1];
const alpha = segmentLengths[segment] ? distance / segmentLengths[segment] : 0;
routePulse.position.lerpVectors(start, end, alpha);
routePulse.position.y += Math.sin(time * 0.006) * 0.12;
routePulse.rotation.y = time * 0.002;
}
buildScenery();
createRouteOverlays();
buildLabels();
renderAll();
resize();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
waterTiles.forEach((tile) => {
tile.position.y = tile.userData.baseY + Math.sin(elapsed * 1.1 + tile.userData.phase) * 0.018;
});
waveTiles.forEach((wave) => {
wave.position.z += wave.userData.speed * 0.008;
wave.position.x = wave.userData.baseX + Math.sin(elapsed * 0.8 + wave.position.z) * 0.06;
if (wave.position.z > 11.4) wave.position.z = -11.4;
});
vehicles.forEach((vehicle, index) => {
vehicle.group.position.z += vehicle.direction * vehicle.speed * 0.012;
if (vehicle.group.position.z > 8.2) vehicle.group.position.z = -9.4;
if (vehicle.group.position.z < -9.4) vehicle.group.position.z = 8.2;
vehicle.group.rotation.y = vehicle.direction > 0 ? 0 : Math.PI;
vehicle.group.position.y = Math.sin(elapsed * 3 + index) * 0.006;
});
animateRoutePulse(elapsed);
updateLabelPositions();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```