Prompt specification
Build or customize the Three.js 3D scene "Close a strait and reroute trade" 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: Close a strait and reroute trade
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/close-a-strait-and-reroute-trade-x1308
Scene Description Prompt:
Please make a voxel Three.js scene in one standalone HTML file using cdnjs. Let users block one fictional shipping passage. Trace the longer alternative route around an island chain and compare route distance while keeping vessel speed fixed.
Working Three.js Starter (close-a-strait-and-reroute-trade-x1308):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pelagic Route Lab — Morrow Strait</title>
<style>
:root {
--bg: #07151e;
--panel: rgba(9, 27, 37, 0.82);
--panel-strong: rgba(12, 35, 47, 0.94);
--line: rgba(170, 224, 237, 0.14);
--text: #edf9fb;
--muted: #8daab4;
--cyan: #47dcff;
--cyan-soft: rgba(71, 220, 255, 0.13);
--amber: #ffb765;
--red: #ff6474;
--green: #64e5a7;
--shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#viewport canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
header {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 78px;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0 26px;
border-bottom: 1px solid var(--line);
background: linear-gradient(
to bottom,
rgba(5, 18, 26, 0.94),
rgba(5, 18, 26, 0.66),
transparent
);
pointer-events: none;
}
.brand,
.header-actions,
.scenario {
pointer-events: auto;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
border: 1px solid rgba(71, 220, 255, 0.35);
border-radius: 11px;
background:
linear-gradient(135deg, rgba(71, 220, 255, 0.22), rgba(71, 220, 255, 0.03)),
rgba(5, 19, 28, 0.85);
box-shadow: inset 0 0 20px rgba(71, 220, 255, 0.08);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
left: 9px;
right: 9px;
height: 3px;
background: var(--cyan);
transform: skewX(-28deg);
box-shadow: 0 0 10px rgba(71, 220, 255, 0.45);
}
.brand-mark::before {
top: 12px;
}
.brand-mark::after {
top: 21px;
opacity: 0.55;
}
.brand-name {
font-size: 13px;
font-weight: 800;
letter-spacing: 0.17em;
line-height: 1.2;
}
.brand-subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.scenario {
display: flex;
align-items: center;
gap: 13px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.scenario strong {
color: var(--text);
font-weight: 650;
}
.scenario-divider {
width: 1px;
height: 22px;
background: var(--line);
}
.header-actions {
display: flex;
align-items: center;
gap: 9px;
}
.icon-button {
width: 38px;
height: 38px;
display: grid;
place-items: center;
color: var(--muted);
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(8, 25, 35, 0.72);
cursor: pointer;
transition: 160ms ease;
}
.icon-button:hover {
color: var(--cyan);
border-color: rgba(71, 220, 255, 0.35);
background: rgba(16, 45, 59, 0.9);
}
.panel {
position: absolute;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.025), transparent 45%),
var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
pointer-events: auto;
}
.control-panel {
top: 100px;
left: 24px;
width: 326px;
padding: 22px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 18px;
height: 1px;
background: var(--cyan);
box-shadow: 0 0 8px rgba(71, 220, 255, 0.45);
}
h1 {
margin: 12px 0 10px;
font-size: clamp(25px, 3vw, 34px);
line-height: 1.05;
letter-spacing: -0.045em;
font-weight: 720;
}
.intro {
margin: 0 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status {
display: flex;
align-items: center;
gap: 8px;
padding: 12px 13px;
margin-bottom: 11px;
border: 1px solid rgba(100, 229, 167, 0.18);
border-radius: 12px;
background: rgba(100, 229, 167, 0.065);
color: #baf4d7;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.status.blocked {
color: #ffc3ca;
border-color: rgba(255, 100, 116, 0.25);
background: rgba(255, 100, 116, 0.08);
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px rgba(100, 229, 167, 0.75);
}
.blocked .status-dot {
background: var(--red);
box-shadow: 0 0 12px rgba(255, 100, 116, 0.75);
animation: pulse 1.15s infinite;
}
@keyframes pulse {
50% {
opacity: 0.38;
transform: scale(0.72);
}
}
.primary-button,
.secondary-button {
width: 100%;
border: 0;
border-radius: 12px;
cursor: pointer;
transition:
transform 160ms ease,
filter 160ms ease,
background 160ms ease;
}
.primary-button {
display: flex;
align-items: center;
justify-content: center;
gap: 9px;
min-height: 45px;
color: #05141b;
background: linear-gradient(135deg, #7be8ff, #42c9ed);
box-shadow: 0 10px 24px rgba(37, 191, 229, 0.16);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.025em;
}
.primary-button:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
.primary-button.block-mode {
color: #fff3f4;
background: linear-gradient(135deg, #ff7b88, #e74d60);
box-shadow: 0 10px 24px rgba(231, 77, 96, 0.16);
}
.primary-button svg {
width: 16px;
height: 16px;
flex: 0 0 auto;
}
.secondary-button {
min-height: 42px;
margin-top: 9px;
color: var(--text);
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.035);
font-size: 12px;
font-weight: 700;
}
.secondary-button:hover {
background: rgba(71, 220, 255, 0.08);
border-color: rgba(71, 220, 255, 0.26);
}
.fixed-speed {
display: flex;
align-items: center;
gap: 13px;
margin-top: 17px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(0, 0, 0, 0.11);
}
.speed-icon {
width: 34px;
height: 34px;
display: grid;
place-items: center;
flex: 0 0 auto;
border-radius: 9px;
color: var(--cyan);
background: var(--cyan-soft);
}
.speed-icon svg {
width: 17px;
height: 17px;
}
.speed-copy {
flex: 1;
}
.speed-copy strong {
display: block;
margin-bottom: 2px;
font-size: 12px;
}
.speed-copy span {
color: var(--muted);
font-size: 10px;
}
.locked-tag {
padding: 4px 6px;
border: 1px solid rgba(71, 220, 255, 0.2);
border-radius: 6px;
color: var(--cyan);
background: rgba(71, 220, 255, 0.07);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.compare-panel {
top: 100px;
right: 24px;
width: 320px;
padding: 20px;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 10px;
margin-bottom: 16px;
}
.panel-heading h2 {
margin: 0 0 4px;
font-size: 16px;
letter-spacing: -0.02em;
}
.panel-heading p {
margin: 0;
color: var(--muted);
font-size: 11px;
}
.route-count {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
.route-row {
position: relative;
display: grid;
grid-template-columns: 8px 1fr auto;
gap: 11px;
align-items: center;
margin-bottom: 8px;
padding: 13px 12px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
transition: 180ms ease;
}
.route-row.active {
border-color: rgba(71, 220, 255, 0.27);
background: rgba(71, 220, 255, 0.065);
}
.route-row.blocked-row.active {
border-color: rgba(255, 100, 116, 0.25);
background: rgba(255, 100, 116, 0.06);
}
.route-swatch {
width: 7px;
height: 30px;
border-radius: 6px;
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 183, 101, 0.25);
}
.route-row.alternate .route-swatch {
background: var(--cyan);
box-shadow: 0 0 12px rgba(71, 220, 255, 0.3);
}
.route-name {
margin-bottom: 3px;
font-size: 12px;
font-weight: 700;
}
.route-meta {
color: var(--muted);
font-size: 9px;
}
.route-distance {
text-align: right;
font-size: 14px;
font-weight: 750;
font-variant-numeric: tabular-nums;
}
.route-distance small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 600;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.impact-card {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 14px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.impact-item {
min-width: 0;
padding: 10px 8px;
border-radius: 10px;
background: rgba(0, 0, 0, 0.12);
text-align: center;
}
.impact-value {
display: block;
margin-bottom: 4px;
color: var(--cyan);
font-size: 15px;
font-weight: 780;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.impact-label {
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.distance-note {
margin: 12px 1px 0;
color: var(--muted);
font-size: 9px;
line-height: 1.5;
}
.voyage-strip {
position: absolute;
left: 50%;
bottom: 24px;
width: min(560px, calc(100vw - 710px));
min-width: 410px;
transform: translateX(-50%);
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel-strong);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
pointer-events: auto;
}
.voyage-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
}
.voyage-route {
min-width: 0;
}
.voyage-route strong {
display: block;
overflow: hidden;
font-size: 11px;
text-overflow: ellipsis;
white-space: nowrap;
}
.voyage-route span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.voyage-metrics {
display: flex;
gap: 18px;
flex: 0 0 auto;
}
.voyage-metric {
text-align: right;
}
.voyage-metric strong {
display: block;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.voyage-metric span {
color: var(--muted);
font-size: 8px;
text-transform: uppercase;
}
.progress-track {
height: 5px;
overflow: hidden;
border-radius: 6px;
background: rgba(255, 255, 255, 0.075);
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #2da9d2, #70eaff);
box-shadow: 0 0 12px rgba(71, 220, 255, 0.5);
transition: width 120ms linear;
}
.legend {
position: absolute;
right: 25px;
bottom: 27px;
display: flex;
align-items: center;
gap: 15px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.04em;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-line {
width: 19px;
height: 3px;
border-radius: 4px;
background: var(--amber);
}
.legend-line.alt {
background: var(--cyan);
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--red);
}
.camera-hint {
position: absolute;
left: 25px;
bottom: 25px;
color: rgba(141, 170, 180, 0.72);
font-size: 9px;
letter-spacing: 0.03em;
pointer-events: none;
}
@media (max-width: 1120px) {
.voyage-strip {
left: 24px;
right: auto;
bottom: 24px;
width: 326px;
min-width: 0;
transform: none;
}
.legend {
display: none;
}
}
@media (max-width: 760px) {
header {
height: 66px;
padding: 0 14px;
}
.scenario {
display: none;
}
.control-panel {
top: 76px;
left: 12px;
width: 280px;
padding: 16px;
}
.control-panel .intro {
display: none;
}
h1 {
margin-top: 9px;
font-size: 24px;
}
.compare-panel {
top: 76px;
right: 12px;
width: 245px;
padding: 14px;
}
.route-row {
padding: 10px 9px;
}
.impact-card {
gap: 4px;
}
.impact-value {
font-size: 12px;
}
.voyage-strip {
left: 12px;
right: 12px;
bottom: 12px;
width: auto;
}
.camera-hint {
display: none;
}
}
@media (max-width: 580px) {
.compare-panel {
top: auto;
right: 12px;
bottom: 105px;
width: 225px;
}
.control-panel {
width: 245px;
}
.panel-heading p,
.distance-note,
.fixed-speed {
display: none;
}
.voyage-metrics {
gap: 9px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="hud">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<div class="brand-name">PELAGIC</div>
<div class="brand-subtitle">Route Lab</div>
</div>
</div>
<div class="scenario">
<span>Fictional scenario</span>
<span class="scenario-divider"></span>
<strong>Graywake Archipelago</strong>
</div>
<div class="header-actions">
<button class="icon-button" id="cameraReset" title="Reset camera" aria-label="Reset camera">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7">
<path d="M4 8V4h4M20 8V4h-4M4 16v4h4M20 16v4h-4" />
<circle cx="12" cy="12" r="3.2" />
</svg>
</button>
</div>
</header>
<section class="panel control-panel">
<div class="eyebrow">Scenario 04 / Navigation</div>
<h1>One passage.<br />Two ways around.</h1>
<p class="intro">
Block Morrow Strait and send the vessel around the Crownfire Isles.
Distance and arrival time update while vessel speed remains locked.
</p>
<div class="section-label">
<span>Morrow Strait</span>
<span>Controlled barrier</span>
</div>
<div class="status" id="passageStatus">
<span class="status-dot"></span>
<span id="passageState">Passage open</span>
</div>
<button class="primary-button" id="blockBtn">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
<path d="M4 7h16M6 7l1 13h10l1-13M9 11v5M15 11v5M12 7V4" />
</svg>
<span id="blockLabel">Block Morrow Strait</span>
</button>
<button class="secondary-button" id="voyageBtn">Start voyage</button>
<div class="fixed-speed">
<div class="speed-icon">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7">
<path d="M4 15a8 8 0 1 1 16 0M12 15l4-5M7 18h10" />
</svg>
</div>
<div class="speed-copy">
<strong>6.0 nautical knots</strong>
<span>Fixed for both route calculations</span>
</div>
<span class="locked-tag">Locked</span>
</div>
</section>
<section class="panel compare-panel">
<div class="panel-heading">
<div>
<h2>Route comparison</h2>
<p>Halcyon Port → Solace Harbor</p>
</div>
<span class="route-count">2 routes</span>
</div>
<div class="route-row" id="directRow">
<div class="route-swatch"></div>
<div>
<div class="route-name">Morrow Strait</div>
<div class="route-meta" id="directMeta">Direct passage · open</div>
</div>
<div class="route-distance">
<span id="directDistance">—</span>
<small>nm</small>
</div>
</div>
<div class="route-row alternate" id="alternateRow">
<div class="route-swatch"></div>
<div>
<div class="route-name">Crownfire detour</div>
<div class="route-meta">Around island chain</div>
</div>
<div class="route-distance">
<span id="alternateDistance">—</span>
<small>nm</small>
</div>
</div>
<div class="impact-card">
<div class="impact-item">
<span class="impact-value" id="extraDistance">—</span>
<span class="impact-label">Extra nm</span>
</div>
<div class="impact-item">
<span class="impact-value" id="extraPercent">—</span>
<span class="impact-label">Longer</span>
</div>
<div class="impact-item">
<span class="impact-value" id="timeImpact">—</span>
<span class="impact-label">Time added</span>
</div>
</div>
<p class="distance-note">
ETA uses the same 6.0 kn vessel speed in both cases.
The voyage animation is time-compressed for demonstration.
</p>
</section>
<section class="voyage-strip">
<div class="voyage-top">
<div class="voyage-route">
<strong id="activeRouteName">Morrow Strait</strong>
<span id="voyageState">Ready to depart</span>
</div>
<div class="voyage-metrics">
<div class="voyage-metric">
<strong id="traveledText">0.0 nm</strong>
<span>Traveled</span>
</div>
<div class="voyage-metric">
<strong id="etaText">—</strong>
<span>Remaining ETA</span>
</div>
</div>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
</section>
<div class="camera-hint">Drag to orbit · Wheel to zoom · Shift-drag to pan · R to reset</div>
<div class="legend">
<div class="legend-item"><span class="legend-line"></span> Direct route</div>
<div class="legend-item"><span class="legend-line alt"></span> Island detour</div>
<div class="legend-item"><span class="legend-dot"></span> Blocked passage</div>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Application state
// ------------------------------------------------------------
const FIXED_SPEED_KN = 6.0;
const TIME_COMPRESSION = 480;
const WATER_LEVEL = 0;
let passageBlocked = false;
let playing = false;
let traveled = 0;
let elapsed = 0;
let activeRoute;
let alternateReveal = 0;
let barrierAmount = 0;
const $ = (id) => document.getElementById(id);
const viewport = $("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07151e);
scene.fog = new THREE.Fog(0x07151e, 48, 105);
const camera = new THREE.PerspectiveCamera(
44,
window.innerWidth / window.innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.id = "sceneCanvas";
viewport.appendChild(renderer.domElement);
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0x9eeaff, 0x14242b, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedcf, 3.0);
sun.position.set(-24, 38, -18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 38;
sun.shadow.camera.bottom = -38;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x50cfff, 1.2);
rim.position.set(28, 15, 30);
scene.add(rim);
// ------------------------------------------------------------
// Materials and shared geometry
// ------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materials = {
rock: new THREE.MeshStandardMaterial({
color: 0x3d5962,
roughness: 0.95,
metalness: 0.02
}),
sand: new THREE.MeshStandardMaterial({
color: 0xb79d6d,
roughness: 0.98
}),
grass: new THREE.MeshStandardMaterial({
color: 0x4f8c70,
roughness: 0.93
}),
grassLight: new THREE.MeshStandardMaterial({
color: 0x63a27c,
roughness: 0.95
}),
dock: new THREE.MeshStandardMaterial({
color: 0x66777b,
roughness: 0.86
}),
concrete: new THREE.MeshStandardMaterial({
color: 0xa6ada6,
roughness: 0.9
}),
dark: new THREE.MeshStandardMaterial({
color: 0x102630,
roughness: 0.68
}),
white: new THREE.MeshStandardMaterial({
color: 0xd9e5df,
roughness: 0.72
}),
orange: new THREE.MeshStandardMaterial({
color: 0xed7d42,
roughness: 0.68
}),
red: new THREE.MeshStandardMaterial({
color: 0xe94d60,
roughness: 0.62
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x52dfff,
roughness: 0.48
}),
foliage: new THREE.MeshStandardMaterial({
color: 0x2d6f5d,
roughness: 1
}),
trunk: new THREE.MeshStandardMaterial({
color: 0x725542,
roughness: 1
})
};
function addBox(parent, material, x, y, z, sx, sy, sz) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Water and seabed
// ------------------------------------------------------------
const water = new THREE.Mesh(
new THREE.PlaneGeometry(76, 56),
new THREE.MeshStandardMaterial({
color: 0x0c5266,
roughness: 0.33,
metalness: 0.18,
transparent: true,
opacity: 0.94
})
);
water.rotation.x = -Math.PI / 2;
water.position.set(0, WATER_LEVEL, 1);
water.receiveShadow = true;
scene.add(water);
const seabed = new THREE.Mesh(
new THREE.PlaneGeometry(78, 58),
new THREE.MeshStandardMaterial({
color: 0x092832,
roughness: 1
})
);
seabed.rotation.x = -Math.PI / 2;
seabed.position.y = -0.7;
scene.add(seabed);
const gridVertices = [];
for (let x = -36; x <= 36; x += 2.4) {
gridVertices.push(x, 0.035, -26, x, 0.035, 28);
}
for (let z = -26; z <= 28; z += 2.4) {
gridVertices.push(-36, 0.035, z, 36, 0.035, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x74d5e8,
transparent: true,
opacity: 0.095,
depthWrite: false
})
);
scene.add(grid);
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0x8eeaff,
transparent: true,
opacity: 0.085,
depthWrite: false
});
const waveTiles = [];
for (let i = 0; i < 46; i++) {
const width = 0.8 + Math.random() * 2.0;
const tile = new THREE.Mesh(
new THREE.PlaneGeometry(width, 0.08),
waveMaterial
);
tile.rotation.x = -Math.PI / 2;
tile.position.set(
-35 + Math.random() * 70,
0.045,
-24 + Math.random() * 52
);
tile.userData.speed = 0.04 + Math.random() * 0.1;
waveTiles.push(tile);
scene.add(tile);
}
// ------------------------------------------------------------
// Voxel archipelago
// ------------------------------------------------------------
const CELL = 1.15;
const LAYER_HEIGHT = 0.7;
const landShapes = [
// Western mainland and eastern mainland.
{ cx: -22, cz: 0, rx: 7, rz: 15 },
{ cx: 22, cz: 3, rx: 8, rz: 14 },
// Promontories defining the central shipping passage.
{ cx: -9, cz: 0, rx: 4.5, rz: 3.5 },
{ cx: 9, cz: 0, rx: 4.5, rz: 3.5 },
// Crownfire island chain.
{ cx: -8.5, cz: 5.5, rx: 3.2, rz: 2.4 },
{ cx: -4.5, cz: 10, rx: 3.5, rz: 2.5 },
{ cx: 0, cz: 13, rx: 3.7, rz: 2.4 },
{ cx: 5, cz: 13.5, rx: 4, rz: 2.4 },
{ cx: 10, cz: 11.5, rx: 3.2, rz: 2.7 }
];
function hash2D(x, z) {
const n = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return n - Math.floor(n);
}
function shapeValue(x, z) {
let value = -Infinity;
for (const shape of landShapes) {
const dx = (x - shape.cx) / shape.rx;
const dz = (z - shape.cz) / shape.rz;
value = Math.max(value, 1 - Math.sqrt(dx * dx + dz * dz));
}
return value;
}
const voxelGroups = {
rock: [],
sand: [],
grass: [],
grassLight: []
};
const treePositions = [];
const terrainTop = [];
for (let ix = -23; ix <= 23; ix++) {
for (let iz = -18; iz <= 19; iz++) {
const x = ix * CELL;
const z = iz * CELL;
const value = shapeValue(x, z);
if (value <= 0) continue;
const edge = value < 0.17;
const random = hash2D(ix, iz);
let height = edge ? 1 : 2 + Math.floor(random * 3.2);
height = Math.min(height, 4);
terrainTop.push({ x, z, y: height * LAYER_HEIGHT });
for (let layer = 0; layer < height; layer++) {
const y = 0.35 + layer * LAYER_HEIGHT;
const item = { x, y, z };
if (layer === height - 1) {
if (height === 1) {
voxelGroups.sand.push(item);
} else if (random > 0.76) {
voxelGroups.grassLight.push(item);
} else {
voxelGroups.grass.push(item);
}
} else if (layer === 0) {
voxelGroups.rock.push(item);
} else {
voxelGroups.sand.push(item);
}
}
if (!edge && height >= 3 && random > 0.78) {
treePositions.push({
x: x + (hash2D(ix + 7, iz) - 0.5) * 0.28,
z: z + (hash2D(ix, iz + 9) - 0.5) * 0.28,
y: height * LAYER_HEIGHT,
scale: 0.72 + hash2D(ix + 13, iz + 5) * 0.5
});
}
}
}
const dummy = new THREE.Object3D();
const voxelGeometry = new THREE.BoxGeometry(
CELL * 0.94,
LAYER_HEIGHT * 0.94,
CELL * 0.94
);
for (const [key, items] of Object.entries(voxelGroups)) {
if (!items.length) continue;
const mesh = new THREE.InstancedMesh(
voxelGeometry,
materials[key],
items.length
);
items.forEach((item, index) => {
dummy.position.set(item.x, item.y, item.z);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
// Voxel trees.
const trunkMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.2, 0.9, 0.2),
materials.trunk,
treePositions.length
);
const crownMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.82, 0.7, 0.82),
materials.foliage,
treePositions.length
);
const crownTopMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.53, 0.48, 0.53),
materials.grassLight,
treePositions.length
);
treePositions.forEach((tree, index) => {
dummy.position.set(tree.x, tree.y + 0.42, tree.z);
dummy.scale.setScalar(tree.scale);
dummy.updateMatrix();
trunkMesh.setMatrixAt(index, dummy.matrix);
dummy.position.set(tree.x, tree.y + 1.02, tree.z);
dummy.updateMatrix();
crownMesh.setMatrixAt(index, dummy.matrix);
dummy.position.set(tree.x, tree.y + 1.48, tree.z);
dummy.scale.setScalar(tree.scale * 0.9);
dummy.updateMatrix();
crownTopMesh.setMatrixAt(index, dummy.matrix);
});
dummy.scale.setScalar(1);
trunkMesh.instanceMatrix.needsUpdate = true;
crownMesh.instanceMatrix.needsUpdate = true;
crownTopMesh.instanceMatrix.needsUpdate = true;
trunkMesh.castShadow = true;
crownMesh.castShadow = true;
crownTopMesh.castShadow = true;
scene.add(trunkMesh, crownMesh, crownTopMesh);
// ------------------------------------------------------------
// Ports and voxel harbor structures
// ------------------------------------------------------------
// Western port: Halcyon.
addBox(scene, materials.dock, -16.0, 0.42, -10.2, 5.0, 0.32, 1.55);
addBox(scene, materials.concrete, -18.1, 1.15, -10.7, 1.65, 1.55, 1.35);
addBox(scene, materials.white, -18.1, 2.05, -10.7, 1.9, 0.22, 1.55);
addBox(scene, materials.orange, -16.5, 0.92, -9.65, 1.2, 0.65, 0.8);
addBox(scene, materials.cyan, -16.5, 1.34, -9.65, 0.75, 0.22, 0.55);
addBox(scene, materials.dark, -13.5, 1.0, -10.2, 0.22, 1.25, 0.22);
// Eastern port: Solace.
addBox(scene, materials.dock, 16.6, 0.42, 9.4, 4.6, 0.32, 1.55);
addBox(scene, materials.concrete, 19.0, 1.2, 10.0, 1.8, 1.65, 1.4);
addBox(scene, materials.white, 19.0, 2.15, 10.0, 2.05, 0.22, 1.6);
addBox(scene, materials.orange, 17.3, 0.95, 8.75, 1.15, 0.68, 0.78);
addBox(scene, materials.dark, 14.3, 1.0, 9.4, 0.22, 1.25, 0.22);
// Voxel cranes.
function addCrane(x, z, rotation = 0) {
const crane = new THREE.Group();
crane.position.set(x, 0, z);
crane.rotation.y = rotation;
addBox(crane, materials.orange, 0, 1.8, 0, 0.28, 3.6, 0.28);
addBox(crane, materials.orange, 0.8, 3.45, 0, 2.0, 0.24, 0.24);
addBox(crane, materials.dark, 1.55, 2.7, 0, 0.08, 1.45, 0.08);
addBox(crane, materials.concrete, 1.55, 1.95, 0, 0.48, 0.35, 0.48);
scene.add(crane);
}
addCrane(-17.0, -11.7, 0.2);
addCrane(17.4, 11.0, Math.PI);
// Lighthouse on the eastern headland.
addBox(scene, materials.white, 11.2, 0.75, -0.9, 1.25, 1.5, 1.25);
addBox(scene, materials.red, 11.2, 1.65, -0.9, 0.92, 0.35, 0.92);
addBox(scene, materials.white, 11.2, 2.25, -0.9, 0.78, 0.9, 0.78);
addBox(scene, materials.cyan, 11.2, 2.86, -0.9, 0.65, 0.28, 0.65);
// ------------------------------------------------------------
// Route geometry
// ------------------------------------------------------------
function createRouteData(waypoints) {
const points = waypoints.map(([x, z]) => new THREE.Vector3(x, 0.69, z));
const cumulative = [0];
for (let i = 1; i < points.length; i++) {
cumulative.push(
cumulative[i - 1] + points[i].distanceTo(points[i - 1])
);
}
return {
points,
cumulative,
length: cumulative[cumulative.length - 1]
};
}
class PolylineCurve3 extends THREE.Curve {
constructor(points) {
super();
this.points = points;
this.cumulative = [0];
for (let i = 1; i < points.length; i++) {
this.cumulative.push(
this.cumulative[i - 1] + points[i].distanceTo(points[i - 1])
);
}
this.totalLength = this.cumulative[this.cumulative.length - 1];
}
getPoint(t, target = new THREE.Vector3()) {
const distance = THREE.MathUtils.clamp(t, 0, 1) * this.totalLength;
for (let i = 1; i < this.points.length; i++) {
if (distance <= this.cumulative[i]) {
const segmentLength =
this.cumulative[i] - this.cumulative[i - 1] || 1;
const fraction =
(distance - this.cumulative[i - 1]) / segmentLength;
return target
.copy(this.points[i - 1])
.lerp(this.points[i], fraction);
}
}
return target.copy(this.points[this.points.length - 1]);
}
}
const directRoute = createRouteData([
[-14.5, -9],
[-9, -6],
[-3, -3],
[0, 0],
[4, 4],
[9, 7],
[15, 9]
]);
const alternateRoute = createRouteData([
[-14.5, -9],
[-13, -4],
[-14.5, 2],
[-14, 7],
[-9, 15],
[-2, 19],
[6, 18],
[13, 13],
[15, 9]
]);
function sampleRoute(route, distance) {
const d = THREE.MathUtils.clamp(distance, 0, route.length);
for (let i = 1; i < route.points.length; i++) {
if (d <= route.cumulative[i]) {
const a = route.points[i - 1];
const b = route.points[i];
const segmentLength = route.cumulative[i] - route.cumulative[i - 1] || 1;
const fraction = (d - route.cumulative[i - 1]) / segmentLength;
const position = a.clone().lerp(b, fraction);
const tangent = b.clone().sub(a).normalize();
return { position, tangent };
}
}
const last = route.points[route.points.length - 1];
const tangent = last.clone().sub(route.points[route.points.length - 2]).normalize();
return { position: last.clone(), tangent };
}
function createRouteMesh(route, color, opacity, radius = 0.1) {
const curve = new PolylineCurve3(route.points);
const geometry = new THREE.TubeGeometry(
curve,
Math.max(160, Math.ceil(route.length * 8)),
radius,
7,
false
);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false,
fog: false
});
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 4;
scene.add(mesh);
return mesh;
}
const directMaterial = new THREE.MeshBasicMaterial({
color: 0xffb765,
transparent: true,
opacity: 0.82,
depthWrite: false,
fog: false
});
const directGeometry = new THREE.TubeGeometry(
new PolylineCurve3(directRoute.points),
Math.max(160, Math.ceil(directRoute.length * 8)),
0.105,
7,
false
);
const directMesh = new THREE.Mesh(directGeometry, directMaterial);
directMesh.renderOrder = 5;
scene.add(directMesh);
const alternateGhost = createRouteMesh(
alternateRoute,
0x47dcff,
0.22,
0.085
);
const alternateTrace = createRouteMesh(
alternateRoute,
0x70ebff,
0.98,
0.135
);
alternateTrace.visible = false;
alternateTrace.geometry.setDrawRange(0, 0);
function createRouteTicks(route, material) {
const group = new THREE.Group();
for (let distance = 5; distance < route.length - 1; distance += 5) {
const sample = sampleRoute(route, distance);
const tick = new THREE.Mesh(
new THREE.PlaneGeometry(0.38, 0.38),
material
);
tick.rotation.x = -Math.PI / 2;
tick.position.set(sample.position.x, 0.79, sample.position.z);
tick.renderOrder = 6;
group.add(tick);
}
scene.add(group);
return group;
}
const directTickMaterial = new THREE.MeshBasicMaterial({
color: 0xffc47e,
transparent: true,
opacity: 0.82,
depthWrite: false,
fog: false
});
const alternateTickMaterial = new THREE.MeshBasicMaterial({
color: 0x7cecff,
transparent: true,
opacity: 0.95,
depthWrite: false,
fog: false
});
const directTicks = createRouteTicks(directRoute, directTickMaterial);
const alternateTicks = createRouteTicks(alternateRoute, alternateTickMaterial);
alternateTicks.visible = false;
// ------------------------------------------------------------
// Blocked-passage barrier
// ------------------------------------------------------------
const barrier = new THREE.Group();
barrier.visible = false;
scene.add(barrier);
for (let i = -4; i <= 4; i++) {
addBox(
barrier,
i % 2 === 0 ? materials.red : materials.white,
0,
0.72,
i * 0.72,
0.36,
0.32,
0.68
);
}
for (const z of [-3.45, 3.45]) {
addBox(barrier, materials.dark, 0, 0.42, z, 0.8, 0.65, 0.8);
addBox(barrier, materials.red, 0, 0.88, z, 0.58, 0.32, 0.58);
addBox(barrier, materials.white, 0, 1.14, z, 0.3, 0.22, 0.3);
}
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0xff4358,
emissive: 0xff1730,
emissiveIntensity: 1.4,
roughness: 0.35
});
addBox(barrier, beaconMaterial, 0, 1.25, 0, 0.42, 0.42, 0.42);
const closurePlane = new THREE.Mesh(
new THREE.PlaneGeometry(1.15, 7.8),
new THREE.MeshBasicMaterial({
color: 0xff465d,
transparent: true,
opacity: 0.16,
depthWrite: false,
side: THREE.DoubleSide
})
);
closurePlane.rotation.x = -Math.PI / 2;
closurePlane.position.y = 0.48;
barrier.add(closurePlane);
// ------------------------------------------------------------
// Voxel vessel
// ------------------------------------------------------------
const ship = new THREE.Group();
scene.add(ship);
addBox(ship, materials.dark, 0, -0.12, 0, 1.28, 0.42, 2.45);
addBox(ship, materials.orange, 0, 0.12, 0.15, 1.35, 0.42, 2.35);
addBox(ship, materials.orange, 0, 0.14, 1.47, 0.86, 0.36, 0.65);
addBox(ship, materials.white, 0, 0.55, -0.75, 1.03, 0.68, 0.72);
addBox(ship, materials.cyan, 0, 0.74, -0.34, 0.78, 0.22, 0.12);
addBox(ship, materials.dark, 0, 0.94, -0.8, 0.22, 0.34, 0.22);
addBox(ship, materials.white, 0.15, 0.57, 0.45, 0.43, 0.42, 0.73);
addBox(ship, materials.orange, -0.18, 0.57, 0.45, 0.43, 0.42, 0.73);
addBox(ship, materials.white, 0.03, 1.22, -0.72, 0.07, 0.75, 0.07);
const wakePositions = new Float32Array(42 * 3);
const wakeGeometry = new THREE.BufferGeometry();
wakeGeometry.setAttribute(
"position",
new THREE.BufferAttribute(wakePositions, 3)
);
wakeGeometry.setDrawRange(0, 0);
const wake = new THREE.Line(
wakeGeometry,
new THREE.LineBasicMaterial({
color: 0xb5f4ff,
transparent: true,
opacity: 0.38,
depthWrite: false
})
);
scene.add(wake);
const wakePoints = [];
let wakeTimer = 0;
function clearWake() {
wakePoints.length = 0;
wakeGeometry.setDrawRange(0, 0);
}
function updateWake(dt, position) {
wakeTimer += dt;
if (wakeTimer < 0.065) return;
wakeTimer = 0;
wakePoints.unshift(position.clone().setY(0.57));
if (wakePoints.length > 42) {
wakePoints.length = 42;
}
wakePoints.forEach((point, index) => {
wakePositions[index * 3] = point.x;
wakePositions[index * 3 + 1] = point.y;
wakePositions[index * 3 + 2] = point.z;
});
wakeGeometry.attributes.position.needsUpdate = true;
wakeGeometry.setDrawRange(0, wakePoints.length);
}
// ------------------------------------------------------------
// Map labels
// ------------------------------------------------------------
function createLabel(text, color = "#dffaff", width = 8.2) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(4, 19, 27, 0.76)";
context.strokeStyle = "rgba(130, 226, 245, 0.25)";
context.lineWidth = 2;
context.beginPath();
context.roundRect(4, 8, 504, 80, 18);
context.fill();
context.stroke();
context.fillStyle = color;
context.font = "700 27px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.letterSpacing = "3px";
context.fillText(text, 256, 49);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
})
);
sprite.scale.set(width, width * 96 / 512, 1);
sprite.renderOrder = 20;
return sprite;
}
const halcyonLabel = createLabel("HALCYON PORT", "#b9eaf2", 7.6);
halcyonLabel.position.set(-17.8, 3.9, -11.4);
scene.add(halcyonLabel);
const solaceLabel = createLabel("SOLACE HARBOR", "#b9eaf2", 7.8);
solaceLabel.position.set(19.0, 4.0, 10.7);
scene.add(solaceLabel);
const straitLabel = createLabel("MORROW STRAIT", "#ffd3a0", 7.5);
straitLabel.position.set(0, 3.4, -0.7);
scene.add(straitLabel);
const islesLabel = createLabel("CROWNFIRE ISLES", "#8feaff", 9.1);
islesLabel.position.set(-0.2, 5.3, 13.7);
scene.add(islesLabel);
// ------------------------------------------------------------
// Manual orbit / pan / zoom camera controls
// ------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 1.4, 2),
yaw: 0.72,
pitch: 0.69,
distance: 58,
defaultTarget: new THREE.Vector3(0, 1.4, 2),
defaultYaw: 0.72,
defaultPitch: 0.69,
defaultDistance: 58
};
function updateCamera() {
const horizontal = Math.cos(cameraState.pitch) * cameraState.distance;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.yaw) * horizontal,
cameraState.target.y + Math.sin(cameraState.pitch) * cameraState.distance,
cameraState.target.z + Math.cos(cameraState.yaw) * horizontal
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.copy(cameraState.defaultTarget);
cameraState.yaw = cameraState.defaultYaw;
cameraState.pitch = cameraState.defaultPitch;
cameraState.distance = cameraState.defaultDistance;
updateCamera();
}
const pointer = {
active: false,
mode: "orbit",
id: null,
lastX: 0,
lastY: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.mode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
if (pointer.mode === "orbit") {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.28,
1.32
);
} else {
camera.updateMatrixWorld();
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
1
);
const amount = cameraState.distance * 0.00135;
cameraState.target.addScaledVector(right, -dx * amount);
cameraState.target.addScaledVector(up, dy * amount);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-22,
22
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-16,
20
);
cameraState.target.y = THREE.MathUtils.clamp(
cameraState.target.y,
0.2,
6
);
}
updateCamera();
});
function releasePointer(event) {
if (event.pointerId !== pointer.id) return;
pointer.active = false;
pointer.id = null;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
25,
88
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// UI calculations
// ------------------------------------------------------------
const extraDistance = alternateRoute.length - directRoute.length;
const extraPercent = (extraDistance / directRoute.length) * 100;
const timeImpact = extraDistance / FIXED_SPEED_KN;
function formatDistance(value) {
return value.toFixed(1);
}
function formatDuration(hours) {
if (hours <= 0) return "0m";
const wholeHours = Math.floor(hours);
const minutes = Math.round((hours - wholeHours) * 60);
if (wholeHours === 0) return `${minutes}m`;
return `${wholeHours}h ${minutes}m`;
}
$("directDistance").textContent = formatDistance(directRoute.length);
$("alternateDistance").textContent = formatDistance(alternateRoute.length);
$("extraDistance").textContent = `+${formatDistance(extraDistance)}`;
$("extraPercent").textContent = `+${Math.round(extraPercent)}%`;
$("timeImpact").textContent = `+${formatDuration(timeImpact)}`;
function updateRouteUI() {
$("passageStatus").classList.toggle("blocked", passageBlocked);
$("passageState").textContent = passageBlocked
? "Passage blocked"
: "Passage open";
$("blockBtn").classList.toggle("block-mode", passageBlocked);
$("blockLabel").textContent = passageBlocked
? "Reopen Morrow Strait"
: "Block Morrow Strait";
$("directMeta").textContent = passageBlocked
? "Direct passage · closed"
: "Direct passage · open";
$("directRow").classList.toggle("active", !passageBlocked);
$("directRow").classList.toggle("blocked-row", passageBlocked);
$("alternateRow").classList.toggle("active", passageBlocked);
$("activeRouteName").textContent = passageBlocked
? "Crownfire detour"
: "Morrow Strait";
$("voyageBtn").textContent = playing
? "Pause voyage"
: traveled > 0
? "Resume voyage"
: "Start voyage";
directMaterial.color.set(passageBlocked ? 0xff6170 : 0xffb765);
directTickMaterial.color.set(passageBlocked ? 0xff6170 : 0xffc47e);
directMaterial.opacity = passageBlocked ? 0.42 : 0.82;
directTickMaterial.opacity = passageBlocked ? 0.42 : 0.82;
directMesh.visible = true;
directTicks.visible = !passageBlocked;
alternateGhost.visible = true;
alternateTicks.visible = passageBlocked;
alternateTrace.visible = passageBlocked;
if (passageBlocked) {
alternateReveal = 0;
alternateTrace.geometry.setDrawRange(0, 0);
} else {
alternateReveal = 1;
alternateTrace.geometry.setDrawRange(
0,
alternateTrace.geometry.index.count
);
}
activeRoute = passageBlocked ? alternateRoute : directRoute;
traveled = 0;
playing = false;
clearWake();
$("voyageState").textContent = "Ready to depart";
$("progressFill").style.width = "0%";
$("traveledText").textContent = "0.0 nm";
$("etaText").textContent = formatDuration(activeRoute.length / FIXED_SPEED_KN);
}
function togglePassage() {
passageBlocked = !passageBlocked;
barrier.visible = passageBlocked;
updateRouteUI();
}
function toggleVoyage() {
if (traveled >= activeRoute.length) {
traveled = 0;
clearWake();
}
playing = !playing;
updateVoyageButton();
}
function updateVoyageButton() {
$("voyageBtn").textContent = playing
? "Pause voyage"
: traveled > 0
? "Resume voyage"
: "Start voyage";
}
$("blockBtn").addEventListener("click", togglePassage);
$("voyageBtn").addEventListener("click", toggleVoyage);
$("cameraReset").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
toggleVoyage();
}
if (event.key.toLowerCase() === "b") {
togglePassage();
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
let uiTimer = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (playing) {
const distancePerSecond =
(FIXED_SPEED_KN / 3600) * TIME_COMPRESSION;
traveled += distancePerSecond * dt;
if (traveled >= activeRoute.length) {
traveled = activeRoute.length;
playing = false;
updateVoyageButton();
$("voyageState").textContent = "Arrived at Solace Harbor";
}
}
const sample = sampleRoute(activeRoute, traveled);
ship.position.set(
sample.position.x,
0.96 + Math.sin(elapsed * 2.4) * 0.035,
sample.position.z
);
const desiredYaw = Math.atan2(
sample.tangent.x,
sample.tangent.z
);
const angleDifference = Math.atan2(
Math.sin(desiredYaw - ship.rotation.y),
Math.cos(desiredYaw - ship.rotation.y)
);
ship.rotation.y += angleDifference * Math.min(1, dt * 7);
ship.rotation.z = Math.sin(elapsed * 1.7) * 0.015;
updateWake(dt, ship.position);
// Animate the closed barrier and beacon.
barrierAmount = THREE.MathUtils.lerp(
barrierAmount,
passageBlocked ? 1 : 0,
1 - Math.exp(-dt * 8)
);
barrier.scale.y = Math.max(0.001, barrierAmount);
beaconMaterial.emissiveIntensity =
1.2 + Math.max(0, Math.sin(elapsed * 5.5)) * 1.8;
// Reveal the alternate route as a traced path after closure.
if (passageBlocked && alternateReveal < 1) {
alternateReveal = Math.min(1, alternateReveal + dt * 0.55);
const indexCount = alternateTrace.geometry.index.count;
alternateTrace.geometry.setDrawRange(
0,
Math.floor(indexCount * alternateReveal)
);
}
// Gentle movement of surface wave tiles.
for (const tile of waveTiles) {
tile.position.x += tile.userData.speed * dt;
if (tile.position.x > 37) tile.position.x = -37;
}
uiTimer += dt;
if (uiTimer > 0.08) {
uiTimer = 0;
const progress = activeRoute.length
? (traveled / activeRoute.length) * 100
: 0;
$("progressFill").style.width = `${progress.toFixed(2)}%`;
$("traveledText").textContent = `${traveled.toFixed(1)} nm`;
const remaining = Math.max(0, activeRoute.length - traveled);
$("etaText").textContent = formatDuration(
remaining / FIXED_SPEED_KN
);
if (!playing && traveled < activeRoute.length) {
$("voyageState").textContent =
traveled > 0 ? "Voyage paused" : "Ready to depart";
}
}
renderer.render(scene, camera);
}
updateRouteUI();
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```