Prompt specification
Build or customize the Three.js 3D scene "Plan an inter-island supply journey" 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: Plan an inter-island supply journey
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/plan-an-inter-island-supply-journey-x2288
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows Lapita island settlements and maritime exchange. Let users choose a schematic route between nearby islands using supplied wind and distance assumptions. Separate the navigation exercise from claims about a particular prehistoric voyage. Voxel style.
Working Three.js Starter (plan-an-inter-island-supply-journey-x2288):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Lapita Island Exchange — Schematic Navigation Exercise</title>
<style>
:root {
--ink: #edf7ef;
--muted: #9cb8b8;
--dim: #6f9194;
--panel: rgba(7, 27, 34, 0.91);
--panel-2: rgba(13, 43, 51, 0.72);
--line: rgba(159, 215, 207, 0.17);
--teal: #63e1cf;
--teal-dark: #1d776f;
--coral: #ff8b6a;
--sand: #e2cf9a;
--amber: #f2bd68;
--danger: #ff806c;
--shadow: 0 20px 60px rgba(0, 0, 0, 0.32);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071d27;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.atmosphere {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 35% 40%, rgba(53, 139, 135, 0.08), transparent 43%),
linear-gradient(180deg, rgba(255, 255, 255, 0.015), transparent 28%);
z-index: 2;
}
.brand {
position: fixed;
z-index: 5;
top: 26px;
left: 28px;
width: min(540px, calc(100vw - 440px));
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 11px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 30px;
height: 2px;
background: var(--teal);
box-shadow: 0 0 12px rgba(99, 225, 207, 0.7);
}
h1 {
margin: 0;
max-width: 540px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 4.5vw, 67px);
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 460px;
margin: 15px 0 14px;
color: #bdd0cd;
font-size: 14px;
line-height: 1.55;
}
.badges {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(188, 225, 217, 0.18);
border-radius: 4px;
background: rgba(5, 26, 32, 0.55);
color: #bad0cd;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.badge::before {
content: "";
width: 5px;
height: 5px;
border-radius: 1px;
background: var(--teal);
}
.badge.coral::before {
background: var(--coral);
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
right: 18px;
bottom: 18px;
width: 370px;
overflow: auto;
padding: 19px;
border: 1px solid rgba(167, 220, 211, 0.17);
border-radius: 12px;
background:
linear-gradient(180deg, rgba(16, 47, 54, 0.76), rgba(6, 25, 32, 0.94)),
var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(1.2);
scrollbar-width: thin;
scrollbar-color: rgba(99, 225, 207, 0.3) transparent;
}
.panel::-webkit-scrollbar {
width: 5px;
}
.panel::-webkit-scrollbar-thumb {
background: rgba(99, 225, 207, 0.3);
border-radius: 8px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
padding-bottom: 13px;
border-bottom: 1px solid var(--line);
}
.panel-heading strong {
font-size: 11px;
letter-spacing: 0.17em;
}
.live {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--teal);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.live::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 10px var(--teal);
animation: pulse 2s infinite;
}
@keyframes pulse {
50% {
opacity: 0.4;
}
}
.scope-note {
margin-bottom: 20px;
padding: 12px 13px;
border-left: 3px solid var(--coral);
background: rgba(255, 139, 106, 0.075);
color: #b8c9c5;
font-size: 11px;
line-height: 1.5;
}
.scope-note strong {
display: block;
margin-bottom: 3px;
color: #ffd3c6;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section {
margin: 0 -2px 21px;
padding: 0 2px 19px;
border-bottom: 1px solid var(--line);
}
.section:last-child {
border-bottom: 0;
}
.section h2 {
display: flex;
align-items: center;
gap: 9px;
margin: 0 0 14px;
font-size: 12px;
letter-spacing: 0.035em;
}
.section h2 span {
color: var(--teal);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.field + .field {
margin-top: 12px;
}
.field-label,
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: #b8cdca;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.045em;
}
.control-label output {
color: var(--teal);
font-variant-numeric: tabular-nums;
}
select {
width: 100%;
height: 39px;
padding: 0 35px 0 11px;
border: 1px solid rgba(173, 221, 214, 0.18);
border-radius: 5px;
outline: none;
background-color: rgba(4, 23, 29, 0.8);
color: var(--ink);
font-size: 12px;
appearance: none;
background-image:
linear-gradient(45deg, transparent 50%, var(--muted) 50%),
linear-gradient(135deg, var(--muted) 50%, transparent 50%);
background-position:
calc(100% - 16px) 16px,
calc(100% - 11px) 16px;
background-size: 5px 5px, 5px 5px;
background-repeat: no-repeat;
cursor: pointer;
}
select:focus {
border-color: var(--teal);
box-shadow: 0 0 0 3px rgba(99, 225, 207, 0.08);
}
.field-row {
display: grid;
grid-template-columns: 1fr 36px 1fr;
align-items: end;
gap: 8px;
}
.swap {
display: grid;
width: 36px;
height: 36px;
place-items: center;
border: 1px solid rgba(173, 221, 214, 0.18);
border-radius: 5px;
background: rgba(4, 23, 29, 0.8);
color: var(--teal);
cursor: pointer;
transition: 160ms ease;
}
.swap:hover {
border-color: var(--teal);
background: rgba(99, 225, 207, 0.09);
transform: rotate(180deg);
}
.replan {
width: 100%;
height: 40px;
margin-top: 13px;
border: 1px solid rgba(99, 225, 207, 0.45);
border-radius: 5px;
background: rgba(99, 225, 207, 0.11);
color: #c7fff5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
transition: 160ms ease;
}
.replan:hover {
background: rgba(99, 225, 207, 0.2);
box-shadow: 0 0 22px rgba(99, 225, 207, 0.08);
}
.quick-title {
display: flex;
justify-content: space-between;
margin: 16px 0 8px;
color: #91aaa7;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.quick-title span {
color: #587b7e;
font-weight: 600;
}
.nearby-links {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
}
.nearby-link {
display: flex;
align-items: center;
justify-content: space-between;
min-width: 0;
padding: 8px;
border: 1px solid rgba(173, 221, 214, 0.11);
border-radius: 4px;
background: rgba(255, 255, 255, 0.025);
color: #b7cbc8;
font-size: 10px;
text-align: left;
cursor: pointer;
transition: 140ms ease;
}
.nearby-link:hover {
border-color: rgba(99, 225, 207, 0.45);
color: white;
background: rgba(99, 225, 207, 0.07);
}
.nearby-link small {
margin-left: 5px;
color: var(--teal);
font-size: 9px;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 6px 0 0;
border-radius: 5px;
outline: none;
background: rgba(159, 215, 207, 0.16);
appearance: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 2px solid #09242c;
border-radius: 3px;
background: var(--teal);
box-shadow: 0 0 0 1px var(--teal);
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #09242c;
border-radius: 3px;
background: var(--teal);
box-shadow: 0 0 0 1px var(--teal);
}
.range-ends {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #54777a;
font-size: 8px;
text-transform: uppercase;
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
margin-top: 16px;
}
.mode-button {
min-height: 49px;
padding: 8px;
border: 1px solid rgba(173, 221, 214, 0.12);
border-radius: 5px;
background: rgba(255, 255, 255, 0.025);
color: #819c9c;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
cursor: pointer;
transition: 140ms ease;
}
.mode-button span {
display: block;
margin-top: 4px;
color: #567679;
font-size: 8px;
font-weight: 600;
letter-spacing: 0;
text-transform: none;
}
.mode-button.active {
border-color: rgba(99, 225, 207, 0.5);
background: rgba(99, 225, 207, 0.09);
color: #c8fff6;
}
.mode-button.active span {
color: #82aaa5;
}
details {
margin-top: 14px;
border-top: 1px solid rgba(159, 215, 207, 0.1);
}
summary {
padding: 11px 0 0;
color: #8fa9a7;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
cursor: pointer;
}
details p,
details li {
color: #789896;
font-size: 10px;
line-height: 1.55;
}
details ul {
padding-left: 16px;
}
.route-summary {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-bottom: 12px;
}
.metric {
padding: 11px;
border: 1px solid rgba(173, 221, 214, 0.11);
border-radius: 5px;
background: rgba(255, 255, 255, 0.025);
}
.metric small {
display: block;
margin-bottom: 4px;
color: #6d9090;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.metric strong {
color: #e5fff9;
font-family: Georgia, "Times New Roman", serif;
font-size: 23px;
font-weight: 500;
}
.metric strong span {
color: #7eaaa5;
font-family: inherit;
font-size: 12px;
}
.route-status {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
color: #9eb5b2;
font-size: 9px;
}
.route-status strong {
color: var(--teal);
font-size: 9px;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.route-path {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 6px;
margin: 8px 0 13px;
padding: 9px 10px;
border-radius: 4px;
background: rgba(0, 0, 0, 0.15);
color: #d9eeea;
font-size: 10px;
font-weight: 800;
}
.route-path i {
color: var(--teal);
font-style: normal;
}
.legs {
display: grid;
gap: 7px;
}
.leg {
padding: 10px;
border: 1px solid rgba(173, 221, 214, 0.1);
border-radius: 5px;
background: rgba(255, 255, 255, 0.022);
}
.leg-top {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 5px;
color: #d8e9e6;
font-size: 10px;
}
.leg-top i {
color: var(--teal);
font-style: normal;
}
.leg-top b {
color: #c8fff5;
font-size: 10px;
}
.leg-detail {
color: #789a98;
font-size: 9px;
line-height: 1.4;
}
.wind-meter {
height: 3px;
margin-top: 7px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.07);
}
.wind-meter span {
display: block;
height: 100%;
border-radius: inherit;
background: var(--teal);
}
.cargo-title {
margin: 16px 0 8px;
color: #789a98;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.cargo {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.cargo span {
padding: 6px 8px;
border: 1px solid rgba(173, 221, 214, 0.1);
border-radius: 3px;
color: #9eb6b2;
font-size: 8px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.cargo i {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 5px;
border-radius: 1px;
background: var(--sand);
}
.cargo span:nth-child(2) i {
background: #e7ece1;
}
.cargo span:nth-child(3) i {
background: #66a993;
}
.distance-table {
display: grid;
grid-template-columns: 1fr auto;
gap: 5px 12px;
margin-top: 9px;
color: #789a98;
font-size: 9px;
}
.distance-table span:nth-child(even) {
color: #aac4c0;
font-variant-numeric: tabular-nums;
text-align: right;
}
.labels {
position: fixed;
z-index: 6;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.map-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(180, 222, 215, 0.19);
border-radius: 4px;
background: rgba(5, 27, 34, 0.82);
color: #d8e8e4;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.22);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.03em;
white-space: nowrap;
pointer-events: auto;
cursor: pointer;
transform: translate(-50%, -100%);
backdrop-filter: blur(6px);
transition: 140ms ease;
}
.map-label:hover {
border-color: var(--teal);
background: rgba(11, 48, 55, 0.94);
}
.map-label.is-start {
border-color: rgba(99, 225, 207, 0.75);
}
.map-label.is-end {
border-color: rgba(255, 139, 106, 0.75);
}
.label-code {
display: grid;
width: 22px;
height: 22px;
place-items: center;
border-radius: 2px;
background: rgba(255, 255, 255, 0.07);
color: #9dbbb6;
font-size: 8px;
}
.is-start .label-code {
background: var(--teal);
color: #08272b;
}
.is-end .label-code {
background: var(--coral);
color: #311c19;
}
.tag {
display: none;
margin-left: 2px;
padding: 3px 4px;
border-radius: 2px;
font-size: 7px;
letter-spacing: 0.08em;
}
.is-start .start-tag {
display: inline;
background: rgba(99, 225, 207, 0.13);
color: var(--teal);
}
.is-end .end-tag {
display: inline;
background: rgba(255, 139, 106, 0.13);
color: var(--coral);
}
.legend {
position: fixed;
z-index: 5;
left: 28px;
bottom: 28px;
display: flex;
flex-wrap: wrap;
gap: 15px;
max-width: 520px;
color: #9eb6b3;
font-size: 9px;
pointer-events: none;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend i {
display: inline-block;
width: 9px;
height: 9px;
border-radius: 1px;
background: var(--sand);
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
}
.legend .route-key {
width: 19px;
height: 0;
border-top: 2px dashed var(--teal);
border-radius: 0;
box-shadow: none;
}
.legend .end-key {
background: var(--coral);
}
.legend .wind-key {
height: 0;
border-top: 2px solid #8db5b2;
box-shadow: none;
}
.hint {
position: fixed;
z-index: 5;
right: 408px;
bottom: 26px;
color: rgba(189, 211, 207, 0.58);
font-size: 9px;
letter-spacing: 0.035em;
pointer-events: none;
text-align: right;
}
.hint kbd {
padding: 2px 4px;
border: 1px solid rgba(190, 220, 214, 0.15);
border-radius: 2px;
color: #a7c2be;
font: inherit;
}
.loading {
position: fixed;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
background: #071d27;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 980px) {
.brand {
width: calc(100vw - 420px);
}
.hint {
display: none;
}
.legend {
max-width: calc(100vw - 430px);
}
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 15px;
width: calc(100vw - 30px);
}
.brand h1 {
max-width: 310px;
font-size: 39px;
}
.subtitle {
max-width: 310px;
font-size: 12px;
}
.badges {
display: none;
}
.panel {
top: auto;
right: 9px;
bottom: 9px;
left: 9px;
width: auto;
max-height: 47vh;
padding: 14px;
}
.panel-heading {
margin-bottom: 10px;
}
.scope-note {
margin-bottom: 13px;
padding: 9px 10px;
}
.section {
margin-bottom: 14px;
padding-bottom: 13px;
}
.legend {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel map of fictional Lapita-style island settlements"></canvas>
<div class="atmosphere"></div>
<header class="brand">
<div class="eyebrow">Pacific navigation study / 01</div>
<h1>Lapita island exchange</h1>
<p class="subtitle">
Choose a route between nearby islands, compare wind effects, and watch a schematic
exchange voyage unfold.
</p>
<div class="badges">
<span class="badge">Generic island names</span>
<span class="badge">Illustrative geography</span>
<span class="badge coral">Exercise, not reconstruction</span>
</div>
</header>
<aside class="panel" aria-label="Route workbench">
<div class="panel-heading">
<strong>ROUTE WORKBENCH</strong>
<span class="live">LIVE MODEL</span>
</div>
<div class="scope-note">
<strong>Separate the model from the past</strong>
This tool tests a schematic route against stated assumptions. It does not claim
that any particular Lapita crew, canoe, date, cargo, or prehistoric path is represented.
Island forms and exchange objects are illustrative.
</div>
<section class="section">
<h2><span>01</span> Choose nearby islands</h2>
<div class="field-row">
<label class="field">
<span class="field-label">Departure island</span>
<select id="fromIsland" aria-label="Departure island"></select>
</label>
<button class="swap" id="swapRoute" title="Swap departure and arrival" aria-label="Swap route">
↕
</button>
<label class="field">
<span class="field-label">Arrival island</span>
<select id="toIsland" aria-label="Arrival island"></select>
</label>
</div>
<button class="replan" id="replan">Re-plan schematic route</button>
<div class="quick-title">
Direct nearby links
<span>from departure</span>
</div>
<div class="nearby-links" id="nearbyLinks"></div>
</section>
<section class="section">
<h2><span>02</span> Set the assumptions</h2>
<div class="field">
<label class="control-label" for="windDirection">
<span>Wind direction · FROM</span>
<output id="windDirectionValue">120° ESE</output>
</label>
<input id="windDirection" type="range" min="0" max="355" step="5" value="120" />
<div class="range-ends"><span>N 0°</span><span>E 90°</span><span>S 180°</span><span>W 270°</span></div>
</div>
<div class="field">
<label class="control-label" for="windSpeed">
<span>Wind speed</span>
<output id="windSpeedValue">12 kn</output>
</label>
<input id="windSpeed" type="range" min="0" max="24" step="1" value="12" />
<div class="range-ends"><span>Calm</span><span>24 knots</span></div>
</div>
<div class="mode-switch" role="group" aria-label="Route planning method">
<button class="mode-button active" data-mode="distance">
Shortest distance
<span>Minimize chart nmi</span>
</button>
<button class="mode-button" data-mode="wind">
Wind-aware
<span>Minimize estimated time</span>
</button>
</div>
<details>
<summary>Read the passage assumptions</summary>
<ul>
<li>Distances are fictional straight-line chart values.</li>
<li>A nearby leg is no more than 45 nautical miles.</li>
<li>Base paddling speed is 3 knots.</li>
<li>Simplified sail contribution is 42% of the downwind component.</li>
<li>No current, tide, swell, visibility, reef passage, or canoe-turn penalties are modeled.</li>
</ul>
</details>
</section>
<section class="section">
<h2><span>03</span> Compare the passage</h2>
<div class="route-summary">
<div class="metric">
<small>Total distance</small>
<strong id="totalDistance">— <span>nmi</span></strong>
</div>
<div class="metric">
<small>Estimated passage</small>
<strong id="totalTime">— <span>hours</span></strong>
</div>
</div>
<div class="route-status">
<span id="routeModeLabel">Shortest-distance path</span>
<strong id="legCount">—</strong>
</div>
<div class="route-path" id="routePath" aria-live="polite"></div>
<div class="legs" id="routeLegs"></div>
<div class="cargo-title">Illustrative exchange cargo</div>
<div class="cargo">
<span><i></i>Decorated pottery</span>
<span><i></i>Shell ornaments</span>
<span><i></i>Greenstone pieces</span>
</div>
<details>
<summary>View all supplied nearby distances</summary>
<div class="distance-table" id="distanceTable"></div>
</details>
</section>
</aside>
<div class="labels" id="labels"></div>
<div class="legend" aria-hidden="true">
<span><i></i> Settlement</span>
<span><i class="route-key"></i> Schematic route</span>
<span><i class="end-key"></i> Arrival</span>
<span><i class="wind-key"></i> Wind travel</span>
</div>
<div class="hint">
Drag to orbit · Wheel to zoom · Click island = departure ·
<kbd>Shift</kbd> + click = arrival
</div>
<div class="loading" id="loading">Building island network…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Exercise data: generic islands and deliberately fictional chart distances.
// Coordinates are display coordinates, not real geographic coordinates.
// -------------------------------------------------------------------------
const ISLANDS = [
{
id: "north",
name: "Coral North",
short: "CN",
x: -18,
z: -10,
radius: 4.1,
seed: 1,
dock: -0.25,
houses: 3,
palms: 3
},
{
id: "palm",
name: "Palm Cay",
short: "PC",
x: -8,
z: 3,
radius: 3.45,
seed: 4,
dock: 0.15,
houses: 3,
palms: 4
},
{
id: "stone",
name: "Stone Islet",
short: "SI",
x: 3,
z: -4,
radius: 3.15,
seed: 7,
dock: 0.25,
houses: 2,
palms: 2
},
{
id: "gate",
name: "Reef Gate",
short: "RG",
x: 11,
z: 10,
radius: 3.65,
seed: 11,
dock: 0.2,
houses: 3,
palms: 3
},
{
id: "east",
name: "East Cay",
short: "EC",
x: 25,
z: 1,
radius: 3.55,
seed: 14,
dock: 0.3,
houses: 2,
palms: 3
},
{
id: "long",
name: "Long Spit",
short: "LS",
x: -12,
z: 23,
radius: 4.0,
seed: 18,
dock: 0.25,
houses: 3,
palms: 2
}
];
const DISTANCES = {
"north|palm": 18,
"north|stone": 24,
"north|gate": 38,
"north|east": 47,
"north|long": 34,
"palm|stone": 16,
"palm|gate": 25,
"palm|east": 34,
"palm|long": 22,
"stone|gate": 17,
"stone|east": 28,
"stone|long": 31,
"gate|east": 20,
"gate|long": 31,
"east|long": 49
};
const MAX_NEARBY_NMI = 45;
const BASE_PADDLE_KN = 3;
const SAIL_EFFICIENCY = 0.42;
const islandById = Object.fromEntries(ISLANDS.map((island) => [island.id, island]));
const state = {
windFrom: 120,
windSpeed: 12,
mode: "distance",
routePath: [],
routeLegs: [],
worldSegments: [],
routeTravel: 0,
destinationRing: null
};
const $ = (id) => document.getElementById(id);
const clamp = THREE.MathUtils.clamp;
const degToRad = THREE.MathUtils.degToRad;
const radToDeg = THREE.MathUtils.radToDeg;
function distanceBetween(a, b) {
return DISTANCES[`${a}|${b}`] ?? DISTANCES[`${b}|${a}`];
}
function wrapDegrees(value) {
return ((value + 180) % 360 + 360) % 360 - 180;
}
function cardinal(bearing) {
const points = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
return points[Math.round(((bearing % 360) + 360) % 360 / 45) % 8];
}
function bearingBetween(a, b) {
const from = islandById[a];
const to = islandById[b];
const dx = to.x - from.x;
const dz = to.z - from.z;
return (radToDeg(Math.atan2(dx, -dz)) + 360) % 360;
}
function estimateLeg(from, to) {
const nauticalMiles = distanceBetween(from, to);
const bearing = bearingBetween(from, to);
const windTravelDirection = state.windFrom + 180;
const windAngle = wrapDegrees(bearing - windTravelDirection);
const alongHeading = state.windSpeed * Math.cos(degToRad(windAngle));
let effectiveSpeed;
if (alongHeading >= 0) {
effectiveSpeed = BASE_PADDLE_KN + alongHeading * SAIL_EFFICIENCY;
} else {
const headwindFraction = Math.min(
1,
Math.abs(alongHeading) / Math.max(1, state.windSpeed)
);
effectiveSpeed = Math.max(
1.2,
BASE_PADDLE_KN * (1 - 0.22 * headwindFraction)
);
}
const absoluteAngle = Math.abs(windAngle);
let relation = "crosswind";
if (absoluteAngle <= 50) relation = "tailwind";
if (absoluteAngle >= 130) relation = "headwind";
return {
from,
to,
nauticalMiles,
bearing,
windAngle,
relation,
effectiveSpeed,
hours: nauticalMiles / effectiveSpeed
};
}
function findRoute(start, end) {
const costs = new Map(ISLANDS.map((island) => [island.id, Infinity]));
const previous = new Map(ISLANDS.map((island) => [island.id, null]));
const visited = new Set();
costs.set(start, 0);
while (visited.size < ISLANDS.length) {
let current = null;
let lowest = Infinity;
for (const island of ISLANDS) {
if (!visited.has(island.id) && costs.get(island.id) < lowest) {
current = island.id;
lowest = costs.get(island.id);
}
}
if (current === null || current === end) break;
visited.add(current);
for (const neighbor of ISLANDS) {
if (visited.has(neighbor.id)) continue;
const nauticalMiles = distanceBetween(current, neighbor.id);
if (nauticalMiles > MAX_NEARBY_NMI) continue;
const legCost =
state.mode === "wind"
? estimateLeg(current, neighbor.id).hours
: nauticalMiles;
const candidate = costs.get(current) + legCost;
if (candidate < costs.get(neighbor.id)) {
costs.set(neighbor.id, candidate);
previous.set(neighbor.id, current);
}
}
}
if (!Number.isFinite(costs.get(end))) return [];
const path = [end];
while (path[0] !== start) {
const parent = previous.get(path[0]);
if (!parent) return [];
path.unshift(parent);
}
return path;
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const canvas = $("scene");
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
$("loading").textContent = "WebGL is required to display this exercise.";
throw error;
}
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071d27);
scene.fog = new THREE.FogExp2(0x071d27, 0.012);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
250
);
const cameraState = {
theta: 0.72,
phi: 0.77,
radius: 70,
target: new THREE.Vector3(2, 0, 6)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xbcece3, 0x15303a, 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe2aa, 3.25);
sun.position.set(-24, 42, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -55;
sun.shadow.camera.right = 55;
sun.shadow.camera.top = 45;
sun.shadow.camera.bottom = -45;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.0007;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x64d8d0, 1.2);
rimLight.position.set(30, 16, -28);
scene.add(rimLight);
const ocean = new THREE.Mesh(
new THREE.PlaneGeometry(145, 115),
new THREE.MeshStandardMaterial({
color: 0x0a3440,
roughness: 0.9,
metalness: 0.05
})
);
ocean.rotation.x = -Math.PI / 2;
ocean.position.y = -0.14;
ocean.receiveShadow = true;
scene.add(ocean);
const gridVertices = [];
for (let x = -68; x <= 68; x += 4) {
gridVertices.push(x, -0.095, -54, x, -0.095, 54);
}
for (let z = -52; z <= 52; z += 4) {
gridVertices.push(-68, -0.095, z, 68, -0.095, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const chartGrid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x75a7a4,
transparent: true,
opacity: 0.095
})
);
scene.add(chartGrid);
let randomSeed = 81273;
function seededRandom() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
const waveGeometry = new THREE.BoxGeometry(1, 0.025, 0.07);
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0x76d7d2,
transparent: true,
opacity: 0.16,
depthWrite: false
});
const waves = new THREE.InstancedMesh(waveGeometry, waveMaterial, 320);
const dummy = new THREE.Object3D();
for (let i = 0; i < 320; i++) {
const x = (seededRandom() - 0.5) * 128;
const z = (seededRandom() - 0.5) * 102;
const nearIsland = ISLANDS.some(
(island) =>
Math.abs(x - island.x) < island.radius * 1.45 &&
Math.abs(z - island.z) < island.radius * 1.45
);
if (nearIsland) {
dummy.position.set(999, 999, 999);
dummy.scale.set(0.01, 0.01, 0.01);
} else {
dummy.position.set(x, -0.025 + seededRandom() * 0.04, z);
dummy.scale.set(
0.5 + seededRandom() * 2.1,
1,
0.5 + seededRandom() * 0.7
);
}
dummy.rotation.set(0, (seededRandom() - 0.5) * 0.5, 0);
dummy.updateMatrix();
waves.setMatrixAt(i, dummy.matrix);
}
waves.instanceMatrix.needsUpdate = true;
scene.add(waves);
const materials = {
wall: new THREE.MeshStandardMaterial({ color: 0xb87952, roughness: 0.92 }),
wallDark: new THREE.MeshStandardMaterial({ color: 0x82513f, roughness: 0.95 }),
roof: new THREE.MeshStandardMaterial({ color: 0x745a38, roughness: 1 }),
roofLight: new THREE.MeshStandardMaterial({ color: 0x9b7849, roughness: 1 }),
paintedBand: new THREE.MeshStandardMaterial({ color: 0x9c3f32, roughness: 0.86 }),
door: new THREE.MeshStandardMaterial({ color: 0x16262a, roughness: 1 }),
trunk: new THREE.MeshStandardMaterial({ color: 0x6e4b31, roughness: 1 }),
leaf: new THREE.MeshStandardMaterial({ color: 0x315f45, roughness: 0.95 }),
leafLight: new THREE.MeshStandardMaterial({ color: 0x4d7951, roughness: 0.95 }),
wood: new THREE.MeshStandardMaterial({ color: 0x9c7247, roughness: 0.94 }),
post: new THREE.MeshStandardMaterial({ color: 0x5c3f2d, roughness: 1 }),
pottery: new THREE.MeshStandardMaterial({ color: 0xb95d3f, roughness: 0.78 }),
shell: new THREE.MeshStandardMaterial({ color: 0xe5e4cf, roughness: 0.66 }),
greenstone: new THREE.MeshStandardMaterial({ color: 0x4b8b7d, roughness: 0.55 })
};
function box(parent, width, height, depth, material, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createHut(island, gridX, gridZ, rotation, index) {
const ground = island.cellMap.get(`${gridX},${gridZ}`) ?? 0.45;
const hut = new THREE.Group();
hut.position.set(
island.x + gridX * 1.05,
ground,
island.z + gridZ * 1.05
);
hut.rotation.y = rotation;
box(hut, 1.05, 0.78, 0.84, materials.wall, 0, 0.39, 0);
box(hut, 1.08, 0.09, 0.87, materials.paintedBand, 0, 0.47, 0);
box(hut, 1.36, 0.2, 1.08, materials.roof, 0, 0.88, 0);
box(hut, 0.94, 0.2, 0.76, materials.roofLight, 0, 1.08, 0);
box(hut, 0.27, 0.45, 0.055, materials.door, 0, 0.225, 0.438);
if (index % 2 === 0) {
box(
hut,
0.12,
0.12,
0.045,
materials.shell,
-0.29,
0.57,
0.44
);
}
island.group.add(hut);
}
function createPalm(island, gridX, gridZ) {
const ground = island.cellMap.get(`${gridX},${gridZ}`) ?? 0.45;
const palm = new THREE.Group();
palm.position.set(
island.x + gridX * 1.05,
ground,
island.z + gridZ * 1.05
);
for (let level = 0; level < 4; level++) {
box(
palm,
0.19 - level * 0.015,
0.46,
0.19 - level * 0.015,
materials.trunk,
level * 0.035,
0.22 + level * 0.43,
0
);
}
const crownY = 1.83;
box(palm, 1.72, 0.14, 0.3, materials.leaf, 0, crownY, 0);
box(palm, 0.3, 0.14, 1.72, materials.leaf, 0, crownY, 0);
const diagonalA = box(
palm,
1.32,
0.14,
0.28,
materials.leafLight,
0,
crownY + 0.08,
0
);
diagonalA.rotation.y = Math.PI / 4;
const diagonalB = box(
palm,
1.32,
0.14,
0.28,
materials.leafLight,
0,
crownY + 0.08,
0
);
diagonalB.rotation.y = -Math.PI / 4;
island.group.add(palm);
}
function createDock(island) {
const angle = island.dock;
const outwardX = Math.sin(angle);
const outwardZ = Math.cos(angle);
const sideX = Math.cos(angle);
const sideZ = -Math.sin(angle);
const start = island.radius * 1.05;
for (let step = 0; step < 4; step++) {
const distance = start + step * 0.83;
const plank = box(
island.group,
0.74,
0.12,
0.96,
materials.wood,
island.x + outwardX * distance,
0.12,
island.z + outwardZ * distance
);
plank.rotation.y = angle;
}
for (let side = -1; side <= 1; side += 2) {
for (const distance of [start + 0.25, start + 2.35]) {
box(
island.group,
0.16,
0.72,
0.16,
materials.post,
island.x + outwardX * distance + sideX * side * 0.42,
0.2,
island.z + outwardZ * distance + sideZ * side * 0.42
);
}
}
const cargoColors = [materials.pottery, materials.shell, materials.greenstone];
for (let i = 0; i < 3; i++) {
const distance = start + 0.8 + i * 0.25;
const offset = (i - 1) * 0.48;
const crate = box(
island.group,
0.34,
0.34,
0.34,
cargoColors[i],
island.x + outwardX * distance + sideX * offset,
0.37,
island.z + outwardZ * distance + sideZ * offset
);
crate.rotation.y = angle + 0.2;
island.exchangeMarkers.push({
mesh: crate,
baseY: crate.position.y,
phase: i + island.seed
});
}
}
const hitMeshes = [];
const islandLabels = [];
for (const island of ISLANDS) {
island.group = new THREE.Group();
scene.add(island.group);
island.cellMap = new Map();
island.exchangeMarkers = [];
const cells = [];
const gridRadius = island.radius;
for (let gridX = -gridRadius; gridX <= gridRadius; gridX++) {
for (let gridZ = -gridRadius; gridZ <= gridRadius; gridZ++) {
const nx = gridX / gridRadius;
const nz = gridZ / gridRadius;
const radial = Math.sqrt(nx * nx + nz * nz);
const irregularity =
0.07 * Math.sin(gridX * 1.72 + island.seed) +
0.05 * Math.cos(gridZ * 1.39 + island.seed * 0.7);
if (radial + irregularity > 1) continue;
let height;
if (radial > 0.73) {
height = 0.25 + ((gridX + gridZ + 20) % 3) * 0.035;
} else {
const terrace = Math.floor((1 - radial) * 4);
height =
0.58 +
terrace * 0.36 +
0.08 * Math.sin(gridX * 2.1 + gridZ * 1.4 + island.seed);
}
const color = new THREE.Color();
if (height < 0.45) {
color.set(0xdac895);
} else if (height > 1.45) {
color.set(0x53784f);
} else {
color.set(0x66865a);
}
if ((gridX * 7 + gridZ * 11 + island.seed) % 13 === 0 && height > 0.6) {
color.set(0x7d8b79);
}
cells.push({ gridX, gridZ, height, color });
island.cellMap.set(`${gridX},${gridZ}`, height);
}
}
const terrain = new THREE.InstancedMesh(
new THREE.BoxGeometry(1.025, 1, 1.025),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
metalness: 0
}),
cells.length
);
cells.forEach((cell, index) => {
dummy.position.set(
island.x + cell.gridX * 1.05,
cell.height / 2,
island.z + cell.gridZ * 1.05
);
dummy.scale.set(1, cell.height, 1);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
terrain.setMatrixAt(index, dummy.matrix);
terrain.setColorAt(index, cell.color);
});
terrain.instanceMatrix.needsUpdate = true;
if (terrain.instanceColor) terrain.instanceColor.needsUpdate = true;
terrain.castShadow = true;
terrain.receiveShadow = true;
island.group.add(terrain);
const shallows = new THREE.Mesh(
new THREE.BoxGeometry(island.radius * 2.48, 0.09, island.radius * 2.48),
new THREE.MeshBasicMaterial({
color: 0x42b6ad,
transparent: true,
opacity: 0.16,
depthWrite: false
})
);
shallows.position.set(island.x, -0.06, island.z);
island.group.add(shallows);
const housePositions = [
[-1, 0],
[1, 0],
[0, 1],
[-1, -1],
[1, -1]
];
for (let i = 0; i < island.houses; i++) {
const [gx, gz] = housePositions[i];
createHut(island, gx, gz, (i - 1) * 0.22 + island.seed * 0.04, i);
}
const palmPositions = [
[2, -1],
[-2, 1],
[0, -2],
[2, 1],
[-1, 2]
];
for (let i = 0; i < island.palms; i++) {
const [gx, gz] = palmPositions[i];
createPalm(island, gx, gz);
}
createDock(island);
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
});
const hitMesh = new THREE.Mesh(
new THREE.CylinderGeometry(
island.radius * 1.05,
island.radius * 1.05,
3.6,
8
),
hitMaterial
);
hitMesh.position.set(island.x, 1.25, island.z);
hitMesh.userData.islandId = island.id;
island.group.add(hitMesh);
hitMeshes.push(hitMesh);
}
// -------------------------------------------------------------------------
// Route and exchange canoe
// -------------------------------------------------------------------------
const routeGroup = new THREE.Group();
scene.add(routeGroup);
function clearRouteGroup() {
while (routeGroup.children.length) {
const child = routeGroup.children[0];
routeGroup.remove(child);
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach((material) => material.dispose());
} else {
object.material.dispose();
}
}
});
}
state.destinationRing = null;
}
function squareRing(island, color) {
const group = new THREE.Group();
const radius = island.radius * 1.28;
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.9
});
box(group, radius * 2, 0.09, 0.11, material, island.x, 0.24, island.z - radius);
box(group, radius * 2, 0.09, 0.11, material, island.x, 0.24, island.z + radius);
box(group, 0.11, 0.09, radius * 2, material, island.x - radius, 0.24, island.z);
box(group, 0.11, 0.09, radius * 2, material, island.x + radius, 0.24, island.z);
return group;
}
function directionArrow(color, opacity = 1) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
box(group, 0.11, 0.08, 1.1, material, 0, 0, 0);
const left = box(group, 0.1, 0.08, 0.72, material, -0.2, 0, 0.42);
left.rotation.y = 0.68;
const right = box(group, 0.1, 0.08, 0.72, material, 0.2, 0, 0.42);
right.rotation.y = -0.68;
return group;
}
function buildRouteVisual(path, legs) {
clearRouteGroup();
if (path.length < 2) return;
const startIsland = islandById[path[0]];
const endIsland = islandById[path[path.length - 1]];
routeGroup.add(squareRing(startIsland, 0x63e1cf));
state.destinationRing = squareRing(endIsland, 0xff8b6a);
routeGroup.add(state.destinationRing);
const points = path.map((id) => {
const island = islandById[id];
return new THREE.Vector3(island.x, 0.66, island.z);
});
const solidGeometry = new THREE.BufferGeometry().setFromPoints(points);
const solidLine = new THREE.Line(
solidGeometry,
new THREE.LineBasicMaterial({
color: 0x2f8f86,
transparent: true,
opacity: 0.52
})
);
routeGroup.add(solidLine);
const dashedGeometry = new THREE.BufferGeometry().setFromPoints(points);
const dashedLine = new THREE.Line(
dashedGeometry,
new THREE.LineDashedMaterial({
color: 0x7ff4df,
dashSize: 0.9,
gapSize: 0.5,
transparent: true,
opacity: 0.98
})
);
dashedLine.computeLineDistances();
routeGroup.add(dashedLine);
for (const leg of legs) {
const from = islandById[leg.from];
const to = islandById[leg.to];
const arrow = directionArrow(0xa3fff0, 0.95);
arrow.position.set(
THREE.MathUtils.lerp(from.x, to.x, 0.58),
0.77,
THREE.MathUtils.lerp(from.z, to.z, 0.58)
);
arrow.rotation.y = Math.atan2(to.x - from.x, to.z - from.z);
routeGroup.add(arrow);
for (const ratio of [0.28, 0.78]) {
const buoy = box(
routeGroup,
0.22,
0.22,
0.22,
new THREE.MeshBasicMaterial({ color: 0xb9fff1 }),
THREE.MathUtils.lerp(from.x, to.x, ratio),
0.66,
THREE.MathUtils.lerp(from.z, to.z, ratio)
);
buoy.castShadow = false;
}
}
}
function createCanoe() {
const canoe = new THREE.Group();
const hull = new THREE.MeshStandardMaterial({
color: 0x563a2a,
roughness: 0.85
});
const trim = new THREE.MeshStandardMaterial({
color: 0xc49758,
roughness: 0.8
});
const sailMaterial = new THREE.MeshStandardMaterial({
color: 0xe7ddbd,
roughness: 0.9,
side: THREE.DoubleSide
});
box(canoe, 0.66, 0.25, 1.65, hull, 0, 0, 0);
box(canoe, 0.38, 0.23, 0.55, trim, 0, 0.08, 1.02);
box(canoe, 0.38, 0.23, 0.55, trim, 0, 0.08, -1.02);
box(canoe, 0.08, 1.55, 0.08, hull, 0, 0.83, -0.14);
const sail = box(canoe, 0.78, 0.98, 0.055, sailMaterial, 0, 1.03, -0.12);
const sailBand = box(
canoe,
0.8,
0.09,
0.065,
new THREE.MeshStandardMaterial({ color: 0xa33f33, roughness: 0.85 }),
0,
0.88,
-0.12
);
const cargo = new THREE.Group();
box(cargo, 0.22, 0.22, 0.22, materials.pottery, -0.17, 0.24, -0.43);
box(cargo, 0.18, 0.18, 0.18, materials.shell, 0.16, 0.22, -0.44);
box(cargo, 0.2, 0.2, 0.2, materials.greenstone, 0, 0.23, -0.72);
canoe.add(cargo);
canoe.userData.sail = sail;
canoe.userData.sailBand = sailBand;
canoe.userData.cargo = cargo;
canoe.visible = false;
scene.add(canoe);
return canoe;
}
const canoe = createCanoe();
// -------------------------------------------------------------------------
// Wind indicators
// -------------------------------------------------------------------------
const windGroup = new THREE.Group();
scene.add(windGroup);
const windArrows = [];
for (let i = 0; i < 17; i++) {
const arrow = directionArrow(0x9bd5d1, 0.23);
arrow.position.set(
(seededRandom() - 0.5) * 108,
0.9 + seededRandom() * 1.5,
(seededRandom() - 0.5) * 88
);
windGroup.add(arrow);
windArrows.push(arrow);
}
function updateWindArrows(delta) {
if (state.windSpeed <= 0) {
windGroup.visible = false;
return;
}
windGroup.visible = true;
const travelBearing = state.windFrom + 180;
const dx = Math.sin(degToRad(travelBearing));
const dz = -Math.cos(degToRad(travelBearing));
const speed = 1.3 + state.windSpeed * 0.13;
for (const arrow of windArrows) {
arrow.position.x += dx * speed * delta;
arrow.position.z += dz * speed * delta;
arrow.position.y += Math.sin(performance.now() * 0.001 + arrow.position.x) * 0.001;
if (arrow.position.x > 56) arrow.position.x = -56;
if (arrow.position.x < -56) arrow.position.x = 56;
if (arrow.position.z > 45) arrow.position.z = -45;
if (arrow.position.z < -45) arrow.position.z = 45;
arrow.rotation.y = Math.atan2(dx, dz);
}
}
// -------------------------------------------------------------------------
// Labels and route UI
// -------------------------------------------------------------------------
for (const island of ISLANDS) {
const label = document.createElement("button");
label.className = "map-label";
label.type = "button";
label.setAttribute(
"aria-label",
`${island.name}. Click to set as departure; Shift-click to set as arrival.`
);
label.innerHTML = `
<span class="label-code">${island.short}</span>
<span>${island.name}</span>
<span class="tag start-tag">START</span>
<span class="tag end-tag">ARRIVAL</span>
`;
label.addEventListener("click", (event) => {
event.stopPropagation();
chooseIsland(island.id, event.shiftKey);
});
$("labels").appendChild(label);
islandLabels.push({ island, element: label });
}
function updateLabelStates() {
const start = $("fromIsland").value;
const end = $("toIsland").value;
for (const { island, element } of islandLabels) {
element.classList.toggle("is-start", island.id === start);
element.classList.toggle("is-end", island.id === end);
}
}
function populateSelects() {
$("fromIsland").innerHTML = ISLANDS.map(
(island) => `<option value="${island.id}">${island.name}</option>`
).join("");
$("fromIsland").value = "north";
refreshDestinationOptions();
}
function reachableFrom(start) {
const reached = new Set([start]);
const queue = [start];
while (queue.length) {
const current = queue.shift();
for (const island of ISLANDS) {
if (
!reached.has(island.id) &&
distanceBetween(current, island.id) <= MAX_NEARBY_NMI
) {
reached.add(island.id);
queue.push(island.id);
}
}
}
return reached;
}
function refreshDestinationOptions() {
const start = $("fromIsland").value;
const previous = $("toIsland").value;
const reachable = reachableFrom(start);
$("toIsland").innerHTML = ISLANDS.filter(
(island) => island.id !== start && reachable.has(island.id)
)
.map((island) => `<option value="${island.id}">${island.name}</option>`)
.join("");
if (previous && previous !== start && reachable.has(previous)) {
$("toIsland").value = previous;
} else {
$("toIsland").selectedIndex = 0;
}
updateNearbyLinks();
}
function updateNearbyLinks() {
const start = $("fromIsland").value;
const links = ISLANDS
.filter(
(island) =>
island.id !== start &&
distanceBetween(start, island.id) <= MAX_NEARBY_NMI
)
.sort(
(a, b) =>
distanceBetween(start, a.id) - distanceBetween(start, b.id)
);
$("nearbyLinks").innerHTML = links
.map(
(island) => `
<button class="nearby-link" data-island="${island.id}">
<span>${island.name}</span>
<small>${distanceBetween(start, island.id)} nmi</small>
</button>
`
)
.join("");
$("nearbyLinks")
.querySelectorAll("[data-island]")
.forEach((button) => {
button.addEventListener("click", () => {
$("toIsland").value = button.dataset.island;
planRoute();
});
});
}
function updateDistanceTable() {
const rows = [];
for (let i = 0; i < ISLANDS.length; i++) {
for (let j = i + 1; j < ISLANDS.length; j++) {
const a = ISLANDS[i];
const b = ISLANDS[j];
const nauticalMiles = distanceBetween(a.id, b.id);
if (nauticalMiles <= MAX_NEARBY_NMI) {
rows.push(
`<span>${a.short} — ${b.short}</span><span>${nauticalMiles} nmi</span>`
);
}
}
}
$("distanceTable").innerHTML = rows.join("");
}
function updateRouteUI(path, legs) {
const totalDistance = legs.reduce(
(sum, leg) => sum + leg.nauticalMiles,
0
);
const totalHours = legs.reduce((sum, leg) => sum + leg.hours, 0);
$("totalDistance").innerHTML = `${totalDistance} <span>nmi</span>`;
$("totalTime").innerHTML = `${totalHours.toFixed(1)} <span>hours</span>`;
$("routeModeLabel").textContent =
state.mode === "distance"
? "Shortest-distance path"
: "Wind-aware path";
$("legCount").textContent = `${legs.length} nearby ${legs.length === 1 ? "leg" : "legs"}`;
$("routePath").innerHTML = path
.map((id) => `<span>${islandById[id].short}</span>`)
.join("<i>→</i>");
$("routeLegs").innerHTML = legs
.map((leg) => {
const color =
leg.relation === "tailwind"
? "#63e1cf"
: leg.relation === "headwind"
? "#ff806c"
: "#f2bd68";
const magnitude =
state.windSpeed > 0
? clamp(
(Math.abs(Math.cos(degToRad(leg.windAngle))) * 100).toFixed(0),
4,
100
)
: 4;
return `
<div class="leg">
<div class="leg-top">
<span>${islandById[leg.from].short} <i>→</i> ${islandById[leg.to].short}</span>
<b>${leg.hours.toFixed(1)} h</b>
</div>
<div class="leg-detail">
${leg.nauticalMiles} nmi · ${Math.round(leg.bearing)}° ${cardinal(leg.bearing)}
· ${leg.relation} · ${leg.effectiveSpeed.toFixed(1)} kn effective
</div>
<div class="wind-meter"><span style="width:${magnitude}%;background:${color}"></span></div>
</div>
`;
})
.join("");
updateLabelStates();
}
function planRoute() {
const start = $("fromIsland").value;
const end = $("toIsland").value;
const path = findRoute(start, end);
state.routePath = path;
state.routeLegs = [];
state.worldSegments = [];
state.routeTravel = 0;
for (let i = 0; i < path.length - 1; i++) {
const leg = estimateLeg(path[i], path[i + 1]);
state.routeLegs.push(leg);
const from = islandById[leg.from];
const to = islandById[leg.to];
state.worldSegments.push({
from,
to,
length: Math.hypot(to.x - from.x, to.z - from.z)
});
}
buildRouteVisual(path, state.routeLegs);
updateRouteUI(path, state.routeLegs);
canoe.visible = state.worldSegments.length > 0;
}
function chooseIsland(id, asDestination) {
if (asDestination) {
$("toIsland").value = id;
} else {
$("fromIsland").value = id;
refreshDestinationOptions();
}
planRoute();
}
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
$("fromIsland").addEventListener("change", () => {
refreshDestinationOptions();
planRoute();
});
$("toIsland").addEventListener("change", planRoute);
$("swapRoute").addEventListener("click", () => {
const start = $("fromIsland").value;
const end = $("toIsland").value;
$("fromIsland").value = end;
refreshDestinationOptions();
$("toIsland").value = start;
planRoute();
});
$("replan").addEventListener("click", planRoute);
$("windDirection").addEventListener("input", (event) => {
state.windFrom = Number(event.target.value);
$("windDirectionValue").textContent = `${state.windFrom}° ${cardinal(state.windFrom)}`;
planRoute();
});
$("windSpeed").addEventListener("input", (event) => {
state.windSpeed = Number(event.target.value);
$("windSpeedValue").textContent = `${state.windSpeed} kn`;
planRoute();
});
document.querySelectorAll(".mode-button").forEach((button) => {
button.addEventListener("click", () => {
state.mode = button.dataset.mode;
document.querySelectorAll(".mode-button").forEach((other) => {
other.classList.toggle("active", other === button);
});
planRoute();
});
});
// -------------------------------------------------------------------------
// Direct camera controls, hit testing, and label projection
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function islandUnderPointer(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(hitMeshes, false)[0];
return hit ? hit.object.userData.islandId : null;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointerDown = true;
pointerMoved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (Math.abs(dx) + Math.abs(dy) > 2) pointerMoved = true;
cameraState.theta -= dx * 0.006;
cameraState.phi = clamp(cameraState.phi + dy * 0.0045, 0.36, 1.27);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
} else {
canvas.style.cursor = islandUnderPointer(event) ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerMoved) {
const islandId = islandUnderPointer(event);
if (islandId) chooseIsland(islandId, event.shiftKey);
}
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
pointerMoved = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
42,
96
);
updateCamera();
},
{ passive: false }
);
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const { island, element } of islandLabels) {
const position = new THREE.Vector3(
island.x,
island.radius * 0.58 + 2.4,
island.z
);
position.project(camera);
const visible =
position.z > -1 &&
position.z < 1 &&
Math.abs(position.x) < 1.15 &&
Math.abs(position.y) < 1.15;
element.style.display = visible ? "flex" : "none";
if (visible) {
element.style.left = `${(position.x * 0.5 + 0.5) * width}px`;
element.style.top = `${(-position.y * 0.5 + 0.5) * height}px`;
}
}
}
function updateCanoe(delta) {
if (!state.worldSegments.length) return;
const totalWorldLength = state.worldSegments.reduce(
(sum, segment) => sum + segment.length,
0
);
state.routeTravel =
(state.routeTravel + delta * 4.1) % totalWorldLength;
let distance = state.routeTravel;
for (const segment of state.worldSegments) {
if (distance <= segment.length) {
const ratio = distance / segment.length;
canoe.position.set(
THREE.MathUtils.lerp(segment.from.x, segment.to.x, ratio),
0.68,
THREE.MathUtils.lerp(segment.from.z, segment.to.z, ratio)
);
canoe.rotation.y = Math.atan2(
segment.to.x - segment.from.x,
segment.to.z - segment.from.z
);
break;
}
distance -= segment.length;
}
canoe.userData.sail.rotation.y =
Math.sin(performance.now() * 0.0015) * 0.08;
canoe.userData.cargo.position.y =
Math.sin(performance.now() * 0.003) * 0.025;
}
function animateExchangeMarkers(time, delta) {
for (const island of ISLANDS) {
for (const marker of island.exchangeMarkers) {
marker.mesh.position.y =
marker.baseY + Math.sin(time * 0.002 + marker.phase) * 0.07;
marker.mesh.rotation.y += delta * 0.22;
}
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Initialize and render
// -------------------------------------------------------------------------
populateSelects();
updateDistanceTable();
planRoute();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
const time = performance.now();
updateWindArrows(delta);
updateCanoe(delta);
animateExchangeMarkers(time, delta);
updateLabels();
if (state.destinationRing) {
const pulse = 1 + Math.sin(time * 0.003) * 0.025;
state.destinationRing.scale.set(pulse, 1, pulse);
}
renderer.render(scene, camera);
});
$("loading").classList.add("hidden");
</script>
</body>
</html>
```