Prompt specification
Build or customize the Three.js 3D scene "Island walkway network comparison" 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: Island walkway network comparison
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/island-walkway-network-comparison-x1699
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing shortest paths constrained to surfaces. Compose a small island with bridges and paths beside a direct-distance marker; compare shortest allowed route with geometric straight-line distance when water cannot be crossed. Voxel style.
Working Three.js Starter (island-walkway-network-comparison-x1699):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tideline — Surface Shortest Path</title>
<style>
:root {
--ink: #15353b;
--muted: #547277;
--paper: rgba(248, 244, 229, 0.94);
--line: rgba(21, 53, 59, 0.13);
--direct: #e44c78;
--route: #ffb000;
--mint: #18b89a;
--shadow: 0 18px 55px rgba(24, 67, 72, 0.17);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cce7e9;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
will-change: left, top, opacity;
}
.endpoint {
display: grid;
place-items: center;
width: 38px;
height: 38px;
border: 3px solid rgba(255, 255, 255, 0.92);
border-radius: 11px;
background: var(--mint);
color: white;
font-size: 16px;
font-weight: 900;
box-shadow:
0 7px 18px rgba(20, 57, 60, 0.22),
inset 0 -4px 0 rgba(0, 0, 0, 0.08);
}
.endpoint.b {
background: #1788bb;
}
.measure-label {
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.76);
border-radius: 10px;
background: rgba(255, 246, 240, 0.94);
box-shadow: 0 8px 24px rgba(31, 73, 77, 0.18);
color: var(--ink);
text-align: center;
backdrop-filter: blur(8px);
}
.measure-label strong {
display: block;
color: var(--direct);
font-size: 13px;
letter-spacing: 0.07em;
}
.measure-label span {
display: block;
margin-top: 3px;
color: #6a6268;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.bridge-label {
padding: 5px 8px;
border-radius: 7px;
background: rgba(21, 53, 59, 0.78);
color: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
box-shadow: 0 5px 15px rgba(21, 53, 59, 0.16);
}
.brand {
position: fixed;
top: 24px;
left: 26px;
z-index: 10;
pointer-events: none;
}
.brand-mark {
display: flex;
align-items: center;
gap: 10px;
color: var(--ink);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand-mark::before {
width: 13px;
height: 13px;
border-radius: 3px;
background: var(--mint);
box-shadow:
5px 5px 0 #f0b934,
10px 10px 0 rgba(21, 53, 59, 0.16);
content: "";
transform: rotate(-8deg);
}
.brand h1 {
margin: 12px 0 5px;
font-size: clamp(28px, 3.2vw, 48px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.brand p {
max-width: 390px;
margin: 0;
color: rgba(21, 53, 59, 0.7);
font-size: 12px;
font-weight: 650;
line-height: 1.5;
}
.panel {
position: fixed;
top: 22px;
right: 22px;
z-index: 20;
width: min(326px, calc(100vw - 44px));
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.eyebrow {
color: var(--mint);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.panel h2 {
margin: 7px 0 5px;
font-size: 24px;
letter-spacing: -0.04em;
}
.panel-copy {
margin: 0 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.metric {
margin-top: 13px;
}
.metric-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.metric-name {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--direct);
}
.swatch.route {
background: var(--route);
}
.metric-value {
font-size: 22px;
font-weight: 900;
letter-spacing: -0.045em;
white-space: nowrap;
}
.metric-value small {
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0;
}
.bar {
height: 8px;
margin-top: 7px;
overflow: hidden;
border-radius: 99px;
background: rgba(21, 53, 59, 0.09);
}
.bar > div {
height: 100%;
border-radius: inherit;
background: var(--direct);
transform-origin: left;
animation: grow 900ms cubic-bezier(0.22, 1, 0.36, 1) both;
}
.bar.route > div {
background: var(--route);
animation-delay: 120ms;
}
@keyframes grow {
from { transform: scaleX(0); }
}
.status {
display: inline-flex;
align-items: center;
gap: 5px;
margin-left: auto;
color: var(--direct);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.status::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
content: "";
}
.status.allowed {
color: #13846f;
}
.delta {
display: flex;
align-items: center;
justify-content: space-between;
margin: 17px 0 15px;
padding: 12px 13px;
border-radius: 11px;
background: rgba(21, 53, 59, 0.065);
}
.delta span {
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.delta strong {
font-size: 15px;
}
.route-note {
display: flex;
gap: 10px;
align-items: flex-start;
padding: 12px 0 15px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.route-note b {
color: var(--ink);
}
.route-note::before {
flex: 0 0 auto;
width: 8px;
height: 8px;
margin-top: 4px;
border-radius: 2px;
background: var(--route);
content: "";
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
}
button {
border: 0;
font: inherit;
cursor: pointer;
}
.controls button {
padding: 9px 7px;
border: 1px solid rgba(21, 53, 59, 0.12);
border-radius: 9px;
background: rgba(255, 255, 255, 0.7);
color: var(--ink);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.04em;
text-transform: uppercase;
transition:
background 160ms ease,
transform 160ms ease;
}
.controls button:hover {
background: white;
transform: translateY(-1px);
}
.controls button.active {
border-color: rgba(24, 184, 154, 0.25);
background: rgba(24, 184, 154, 0.11);
color: #107c68;
}
.legend {
position: fixed;
left: 26px;
bottom: 24px;
z-index: 12;
display: flex;
flex-wrap: wrap;
gap: 8px;
pointer-events: auto;
}
.legend button {
display: flex;
align-items: center;
gap: 8px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 9px;
background: rgba(248, 244, 229, 0.88);
box-shadow: 0 8px 24px rgba(24, 67, 72, 0.11);
color: var(--ink);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.03em;
backdrop-filter: blur(10px);
}
.legend button.off {
opacity: 0.43;
}
.legend .line {
width: 19px;
height: 4px;
border-radius: 4px;
background: var(--direct);
}
.legend .line.route {
background: var(--route);
}
.help {
position: fixed;
right: 25px;
bottom: 23px;
z-index: 10;
color: rgba(21, 53, 59, 0.62);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.025em;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 16px;
left: 16px;
}
.brand h1 {
margin-top: 10px;
font-size: 28px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 68px;
width: calc(100vw - 24px);
padding: 15px;
}
.panel-copy,
.route-note,
.controls {
display: none;
}
.panel h2 {
margin-bottom: 10px;
font-size: 19px;
}
.metric {
margin-top: 8px;
}
.delta {
margin: 10px 0 0;
padding: 9px 11px;
}
.legend {
left: 12px;
bottom: 14px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="labels"></div>
<header class="brand">
<div class="brand-mark">Tideline Lab</div>
<h1>Across the tide</h1>
<p>
A shortest path cannot cut across the channel. Compare the geometric
shortcut with the shortest route that stays on paths and bridges.
</p>
</header>
<aside class="panel">
<div class="eyebrow">Constrained route study</div>
<h2>Two ways across</h2>
<p class="panel-copy">
Water is impassable. The legal route follows the island trail and crosses
the central footbridge.
</p>
<div class="metric">
<div class="metric-head">
<div class="metric-name">
<span class="swatch"></span>
Direct distance
<span class="status">Blocked</span>
</div>
<div class="metric-value">
<span id="directValue">—</span>
<small>m</small>
</div>
</div>
<div class="bar"><div id="directBar" style="width: 100%"></div></div>
</div>
<div class="metric">
<div class="metric-head">
<div class="metric-name">
<span class="swatch route"></span>
Shortest allowed
<span class="status allowed">Allowed</span>
</div>
<div class="metric-value">
<span id="routeValue">—</span>
<small>m</small>
</div>
</div>
<div class="bar route"><div id="routeBar"></div></div>
</div>
<div class="delta">
<span>Surface detour</span>
<strong id="deltaValue">—</strong>
</div>
<div class="route-note">
<div>
<b id="routeDescription">Central footbridge</b><br />
The dashed line is the unobstructed chord. It passes directly over
open water.
</div>
</div>
<div class="controls">
<button id="isoButton" class="active">Isometric</button>
<button id="topButton">Top view</button>
<button id="resetButton">Reset</button>
</div>
</aside>
<nav class="legend" aria-label="Scene layers">
<button id="directToggle">
<span class="line"></span>
Direct distance
</button>
<button id="routeToggle">
<span class="line route"></span>
Allowed route
</button>
</nav>
<div class="help">Drag to orbit · Shift-drag to pan · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcce7e9);
scene.fog = new THREE.Fog(0xcce7e9, 28, 54);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
document.body.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(
-10, 10, 10, -10, 0.1, 100
);
const orbit = {
theta: 0.72,
phi: 0.86,
zoom: 1,
target: new THREE.Vector3(0, 0.35, 0)
};
let baseViewSize = 9.3;
const cameraRadius = 32;
const hemiLight = new THREE.HemisphereLight(0xeafcff, 0x52736b, 2.1);
scene.add(hemiLight);
const sun = new THREE.DirectionalLight(0xfff2d0, 3.1);
sun.position.set(-10, 18, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9ce8ef, 0.75);
fill.position.set(12, 7, -12);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared materials and utility functions
// -------------------------------------------------------------------------
const material = {
sand: new THREE.MeshStandardMaterial({
color: 0xe8c985,
roughness: 0.93
}),
sandLight: new THREE.MeshStandardMaterial({
color: 0xf2dda3,
roughness: 0.95
}),
grass: new THREE.MeshStandardMaterial({
color: 0x6fae64,
roughness: 0.95
}),
grassDark: new THREE.MeshStandardMaterial({
color: 0x4f9258,
roughness: 0.95
}),
grassLight: new THREE.MeshStandardMaterial({
color: 0x8ac574,
roughness: 0.95
}),
rock: new THREE.MeshStandardMaterial({
color: 0x83928a,
roughness: 0.92
}),
trunk: new THREE.MeshStandardMaterial({
color: 0x73563b,
roughness: 1
}),
leaves: new THREE.MeshStandardMaterial({
color: 0x2f8054,
roughness: 0.95
}),
leavesLight: new THREE.MeshStandardMaterial({
color: 0x4d9a5e,
roughness: 0.95
}),
path: new THREE.MeshStandardMaterial({
color: 0xd4b77c,
roughness: 1
}),
wood: new THREE.MeshStandardMaterial({
color: 0x8d6847,
roughness: 0.9
}),
woodLight: new THREE.MeshStandardMaterial({
color: 0xb08758,
roughness: 0.9
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x604c3d,
roughness: 0.95
}),
route: new THREE.MeshStandardMaterial({
color: 0xffc21a,
emissive: 0xff9800,
emissiveIntensity: 0.6,
roughness: 0.48
}),
direct: new THREE.MeshBasicMaterial({
color: 0xe44c78
}),
water: new THREE.MeshPhysicalMaterial({
color: 0x54b8bd,
roughness: 0.3,
metalness: 0.05,
transparent: true,
opacity: 0.92
})
};
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const xAxis = new THREE.Vector3(1, 0, 0);
function box(
width,
height,
depth,
x,
y,
z,
mat,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function boxBetween(p1, p2, width, height, mat, parent = scene) {
const direction = new THREE.Vector3().subVectors(p2, p1);
const length = direction.length();
const midpoint = new THREE.Vector3()
.addVectors(p1, p2)
.multiplyScalar(0.5);
const mesh = box(
length,
height,
width,
midpoint.x,
midpoint.y,
midpoint.z,
mat,
parent
);
mesh.quaternion.setFromUnitVectors(
xAxis,
direction.normalize()
);
return mesh;
}
function hash2(x, z) {
const n = Math.sin(x * 127.1 + z * 311.7) * 43758.5453;
return n - Math.floor(n);
}
// -------------------------------------------------------------------------
// Ocean and voxel terrain
// -------------------------------------------------------------------------
const water = new THREE.Mesh(
new THREE.PlaneGeometry(70, 55),
material.water
);
water.rotation.x = -Math.PI / 2;
water.position.y = -0.08;
water.receiveShadow = true;
scene.add(water);
const waveGroup = new THREE.Group();
scene.add(waveGroup);
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0xd6ffff,
transparent: true,
opacity: 0.36
});
const waves = [];
for (let i = 0; i < 70; i++) {
const x = (hash2(i, 4) - 0.5) * 38;
const z = (hash2(i, 9) - 0.5) * 30;
const length = 0.25 + hash2(i, 13) * 0.72;
const wave = box(
length,
0.018,
0.045,
x,
0.005,
z,
waveMaterial,
waveGroup,
false
);
wave.rotation.y = (hash2(i, 18) - 0.5) * 0.8;
waves.push({
mesh: wave,
phase: hash2(i, 21) * Math.PI * 2
});
}
const islandGroup = new THREE.Group();
const bridgeGroup = new THREE.Group();
const pathGroup = new THREE.Group();
const propGroup = new THREE.Group();
scene.add(islandGroup, bridgeGroup, pathGroup, propGroup);
const terrainCells = new Map();
function terrainInfo(x, z) {
const leftQ =
Math.pow((x + 5.5) / 4.1, 2) +
Math.pow(z / 5.25, 2);
const rightQ =
Math.pow((x - 5.5) / 4.1, 2) +
Math.pow(z / 5.25, 2);
const q = Math.min(leftQ, rightQ);
let height;
if (q < 0.16) height = 1.65;
else if (q < 0.36) height = 1.25;
else if (q < 0.64) height = 0.86;
else if (q < 0.88) height = 0.55;
else height = 0.30;
return { q, height };
}
for (let x = -10; x <= 10; x++) {
for (let z = -7; z <= 7; z++) {
const info = terrainInfo(x, z);
if (info.q > 1.035) continue;
const key = `${x},${z}`;
terrainCells.set(key, info);
const cellSize = 0.96;
const edge = info.q > 0.68;
const variation = hash2(x, z);
let topMaterial = material.grass;
if (info.q < 0.16) {
topMaterial =
variation > 0.66 ? material.rock : material.grassDark;
} else if (info.q < 0.4) {
topMaterial =
variation > 0.62 ? material.grassLight : material.grass;
} else if (edge) {
topMaterial =
variation > 0.52 ? material.sandLight : material.sand;
} else if (variation > 0.72) {
topMaterial = material.grassLight;
}
if (edge) {
const fullHeight = info.height + 0.45;
box(
cellSize,
fullHeight,
cellSize,
x,
-0.45 + fullHeight / 2,
z,
topMaterial,
islandGroup
);
} else {
const baseHeight = 0.26;
const lowerHeight = info.height + 0.45 - baseHeight;
const lowerY = -0.45 + lowerHeight / 2;
box(
cellSize,
lowerHeight,
cellSize,
x,
lowerY,
z,
material.sand,
islandGroup
);
box(
cellSize,
0.26,
cellSize,
x,
info.height - 0.13,
z,
topMaterial,
islandGroup
);
}
}
}
function terrainHeightAt(x, z) {
const ix = Math.round(x);
const iz = Math.round(z);
const cell = terrainCells.get(`${ix},${iz}`);
if (cell) return cell.height;
const info = terrainInfo(x, z);
return info.q <= 1.035 ? info.height : 0.3;
}
// -------------------------------------------------------------------------
// Route graph: paths and bridge edges
// -------------------------------------------------------------------------
const nodeData = {
A: [-6.5, 1.25, 2.4],
W1: [-4.7, 1.25, 2.4],
W2: [-3.0, 0.86, 1.6],
BW: [-1.5, 0.72, 0.0],
BE: [ 1.5, 0.72, 0.0],
E1: [ 3.0, 0.86, -1.6],
E2: [ 4.7, 1.25, -2.4],
B: [ 6.5, 1.25, -2.4],
NW1: [-3.8, 1.15, 2.5],
NW: [-1.5, 0.72, 2.5],
NE: [ 1.5, 0.72, 2.5],
NE1: [ 3.2, 0.86, 2.5],
NE2: [ 4.7, 1.25, 2.5]
};
const nodes = {};
for (const [name, values] of Object.entries(nodeData)) {
nodes[name] = new THREE.Vector3(...values);
}
const edges = [
{ a: "A", b: "W1", type: "path" },
{ a: "W1", b: "W2", type: "path" },
{ a: "W2", b: "BW", type: "path" },
{ a: "BW", b: "BE", type: "bridge" },
{ a: "BE", b: "E1", type: "path" },
{ a: "E1", b: "E2", type: "path" },
{ a: "E2", b: "B", type: "path" },
{ a: "W1", b: "NW1", type: "path" },
{ a: "NW1", b: "NW", type: "path" },
{ a: "NW", b: "NE", type: "bridge" },
{ a: "NE", b: "NE1", type: "path" },
{ a: "NE1", b: "NE2", type: "path" },
{ a: "NE2", b: "E1", type: "path" },
{ a: "NE2", b: "E2", type: "path" }
];
const adjacency = new Map();
for (const edge of edges) {
const weight = nodes[edge.a].distanceTo(nodes[edge.b]);
edge.weight = weight;
if (!adjacency.has(edge.a)) adjacency.set(edge.a, []);
if (!adjacency.has(edge.b)) adjacency.set(edge.b, []);
adjacency.get(edge.a).push({ to: edge.b, weight, edge });
adjacency.get(edge.b).push({ to: edge.a, weight, edge });
}
function dijkstra(startId, endId) {
const distance = new Map();
const previous = new Map();
const visited = new Set();
for (const id of Object.keys(nodes)) distance.set(id, Infinity);
distance.set(startId, 0);
while (visited.size < Object.keys(nodes).length) {
let current = null;
let currentDistance = Infinity;
for (const [id, value] of distance) {
if (!visited.has(id) && value < currentDistance) {
current = id;
currentDistance = value;
}
}
if (current === null || current === endId) break;
visited.add(current);
for (const connection of adjacency.get(current) || []) {
if (visited.has(connection.to)) continue;
const candidate = currentDistance + connection.weight;
if (candidate < distance.get(connection.to)) {
distance.set(connection.to, candidate);
previous.set(connection.to, {
node: current,
edge: connection.edge
});
}
}
}
const path = [];
const pathEdges = [];
let cursor = endId;
while (cursor !== startId) {
const previousItem = previous.get(cursor);
if (!previousItem) break;
path.unshift(cursor);
pathEdges.unshift(previousItem.edge);
cursor = previousItem.node;
}
path.unshift(startId);
return {
distance: distance.get(endId),
path,
edges: pathEdges
};
}
const shortest = dijkstra("A", "B");
const directDistance = nodes.A.distanceTo(nodes.B);
const extraDistance = shortest.distance - directDistance;
const extraPercent = (extraDistance / directDistance) * 100;
document.getElementById("directValue").textContent =
directDistance.toFixed(1);
document.getElementById("routeValue").textContent =
shortest.distance.toFixed(1);
document.getElementById("deltaValue").textContent =
`+${extraDistance.toFixed(1)} m · +${extraPercent.toFixed(0)}%`;
document.getElementById("routeBar").style.width =
`${(directDistance / shortest.distance) * 100}%`;
const usesCentralBridge = shortest.edges.some(
edge => edge.type === "bridge" && edge.a === "BW" && edge.b === "BE"
);
document.getElementById("routeDescription").textContent =
usesCentralBridge
? "Central footbridge"
: "Northern footbridge";
// -------------------------------------------------------------------------
// Path tiles
// -------------------------------------------------------------------------
function addPathTiles(edge) {
const a = nodes[edge.a];
const b = nodes[edge.b];
const horizontalDistance = Math.hypot(b.x - a.x, b.z - a.z);
const count = Math.max(2, Math.ceil(horizontalDistance / 0.28));
const angle = Math.atan2(b.z - a.z, b.x - a.x);
for (let i = 0; i < count; i++) {
const t = (i + 0.5) / count;
const x = THREE.MathUtils.lerp(a.x, b.x, t);
const z = THREE.MathUtils.lerp(a.z, b.z, t);
const y = THREE.MathUtils.lerp(a.y, b.y, t);
const tile = box(
0.33,
0.11,
0.58,
x,
y + 0.035,
z,
material.path,
pathGroup
);
tile.rotation.y = -angle;
}
}
for (const edge of edges) {
if (edge.type === "path") addPathTiles(edge);
}
// -------------------------------------------------------------------------
// Voxel bridges
// -------------------------------------------------------------------------
function addBridge(z, name) {
const deckY = 0.72;
const startX = -1.5;
const endX = 1.5;
const plankCount = 12;
for (let i = 0; i < plankCount; i++) {
const t = (i + 0.5) / plankCount;
const x = THREE.MathUtils.lerp(startX, endX, t);
box(
0.28,
0.16,
0.86,
x,
deckY,
z,
i % 3 === 0 ? material.woodLight : material.wood,
bridgeGroup
);
}
// Rails and uprights.
for (const side of [-0.43, 0.43]) {
box(
3.45,
0.10,
0.10,
0,
deckY + 0.53,
z + side,
material.woodDark,
bridgeGroup
);
for (let i = 0; i <= 6; i++) {
const x = -1.5 + i * 0.5;
box(
0.10,
0.57,
0.10,
x,
deckY + 0.27,
z + side,
material.woodDark,
bridgeGroup
);
}
}
// Piers.
for (const x of [-0.75, 0, 0.75]) {
box(
0.15,
1.15,
0.15,
x,
0.05,
z,
material.woodDark,
bridgeGroup
);
}
return name;
}
addBridge(0, "Central bridge");
addBridge(2.5, "North bridge");
// -------------------------------------------------------------------------
// Highlighted shortest path
// -------------------------------------------------------------------------
const routeGroup = new THREE.Group();
scene.add(routeGroup);
const routePulse = new THREE.Mesh(
unitBox,
new THREE.MeshBasicMaterial({ color: 0xfff7bf })
);
routePulse.scale.set(0.26, 0.26, 0.26);
routePulse.castShadow = true;
routeGroup.add(routePulse);
const routeHalo = new THREE.Mesh(
unitBox,
new THREE.MeshBasicMaterial({
color: 0xffd23f,
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
routeHalo.scale.set(0.52, 0.52, 0.52);
routeGroup.add(routeHalo);
for (const edge of shortest.edges) {
const a = nodes[edge.a];
const b = nodes[edge.b];
const horizontalDistance = Math.hypot(b.x - a.x, b.z - a.z);
const count = Math.max(3, Math.ceil(horizontalDistance / 0.15));
const angle = Math.atan2(b.z - a.z, b.x - a.x);
for (let i = 0; i < count; i++) {
const t = (i + 0.5) / count;
const x = THREE.MathUtils.lerp(a.x, b.x, t);
const z = THREE.MathUtils.lerp(a.z, b.z, t);
const y = THREE.MathUtils.lerp(a.y, b.y, t) + 0.14;
const dash = box(
0.21,
0.08,
0.15,
x,
y,
z,
material.route,
routeGroup,
false
);
dash.rotation.y = -angle;
}
}
for (const nodeId of shortest.path) {
const point = nodes[nodeId];
box(
0.23,
0.12,
0.23,
point.x,
point.y + 0.16,
point.z,
material.route,
routeGroup,
false
);
}
// -------------------------------------------------------------------------
// Direct-distance chord
// -------------------------------------------------------------------------
const directGroup = new THREE.Group();
scene.add(directGroup);
const directStart = nodes.A.clone();
const directEnd = nodes.B.clone();
const lineHeight = 1.62;
directStart.y += lineHeight;
directEnd.y += lineHeight;
function addDashedLine(start, end, dashLength = 0.54, gapLength = 0.27) {
const direction = new THREE.Vector3().subVectors(end, start);
const distance = direction.length();
const unit = direction.clone().normalize();
for (let offset = 0; offset < distance; offset += dashLength + gapLength) {
const length = Math.min(dashLength, distance - offset);
const p1 = start.clone().addScaledVector(unit, offset);
const p2 = start.clone().addScaledVector(unit, offset + length);
boxBetween(
p1,
p2,
0.09,
0.07,
material.direct,
directGroup
);
}
}
addDashedLine(directStart, directEnd);
addDashedLine(
nodes.A.clone(),
directStart.clone(),
0.22,
0.15
);
addDashedLine(
nodes.B.clone(),
directEnd.clone(),
0.22,
0.15
);
const directMid = directStart.clone().lerp(directEnd, 0.5);
box(
0.48, 0.07, 0.08,
directMid.x, directMid.y, directMid.z,
material.direct, directGroup, false
);
box(
0.08, 0.07, 0.48,
directMid.x, directMid.y, directMid.z,
material.direct, directGroup, false
);
// -------------------------------------------------------------------------
// Voxel scenery
// -------------------------------------------------------------------------
function addTree(x, z, scale = 1) {
const y = terrainHeightAt(x, z);
box(
0.19 * scale,
0.68 * scale,
0.19 * scale,
x,
y + 0.34 * scale,
z,
material.trunk,
propGroup
);
box(
0.75 * scale,
0.58 * scale,
0.75 * scale,
x,
y + 0.78 * scale,
z,
material.leaves,
propGroup
);
box(
0.54 * scale,
0.49 * scale,
0.54 * scale,
x - 0.08 * scale,
y + 1.22 * scale,
z + 0.03 * scale,
material.leavesLight,
propGroup
);
box(
0.35 * scale,
0.34 * scale,
0.35 * scale,
x + 0.04 * scale,
y + 1.59 * scale,
z,
material.leaves,
propGroup
);
}
const treePositions = [
[-8.5, 0.0, 0.90],
[-7.7, -2.6, 0.82],
[-8.2, 3.2, 0.74],
[-6.0, -3.7, 0.94],
[-4.3, -3.5, 0.78],
[-3.4, 4.1, 0.70],
[-5.0, 4.2, 0.65],
[-8.2, -4.5, 0.62],
[8.2, 0.0, 0.92],
[7.8, -2.7, 0.80],
[8.3, 3.0, 0.72],
[6.0, 3.8, 0.88],
[5.4, 0.4, 0.72],
[3.7, -3.9, 0.85],
[6.2, -4.2, 0.65],
[8.4, 4.1, 0.58]
];
for (const [x, z, scale] of treePositions) {
addTree(x, z, scale);
}
// Small shoreline rocks.
const rockPositions = [
[-9.3, -1.7], [-8.8, 4.2], [-6.8, -5.0],
[-2.4, -4.8], [2.4, 4.8], [5.0, -5.1],
[8.9, -1.1], [9.1, 2.1]
];
for (const [x, z] of rockPositions) {
const y = terrainHeightAt(x, z);
const size = 0.28 + hash2(x, z) * 0.24;
box(
size,
size * 0.8,
size,
x,
y + size * 0.35,
z,
material.rock,
propGroup
);
}
// Tiny cabin on the western island.
const cabinX = -7.2;
const cabinZ = -1.1;
const cabinY = terrainHeightAt(cabinX, cabinZ);
box(
1.25, 0.82, 1.05,
cabinX, cabinY + 0.41, cabinZ,
material.sandLight, propGroup
);
const roofMaterial = new THREE.MeshStandardMaterial({
color: 0xb65c50,
roughness: 0.9
});
box(
1.43, 0.18, 1.20,
cabinX, cabinY + 0.91, cabinZ,
roofMaterial, propGroup
);
box(
0.63, 0.18, 0.63,
cabinX, cabinY + 1.08, cabinZ,
roofMaterial, propGroup
);
box(
0.23, 0.48, 0.03,
cabinX, cabinY + 0.24, cabinZ + 0.54,
material.woodDark, propGroup
);
// Voxel lighthouse on the eastern island.
const lighthouseX = 7.35;
const lighthouseZ = 0.35;
const lighthouseY = terrainHeightAt(lighthouseX, lighthouseZ);
const lighthouseWhite = new THREE.MeshStandardMaterial({
color: 0xf3eee0,
roughness: 0.85
});
const lighthouseRed = new THREE.MeshStandardMaterial({
color: 0xdb5b51,
roughness: 0.85
});
const lighthouseGlass = new THREE.MeshStandardMaterial({
color: 0xffe291,
emissive: 0xffb22e,
emissiveIntensity: 0.85,
roughness: 0.35
});
box(
1.08, 0.22, 1.08,
lighthouseX, lighthouseY + 0.11, lighthouseZ,
material.rock, propGroup
);
box(
0.72, 0.78, 0.72,
lighthouseX, lighthouseY + 0.61, lighthouseZ,
lighthouseWhite, propGroup
);
box(
0.60, 0.27, 0.60,
lighthouseX, lighthouseY + 1.13, lighthouseZ,
lighthouseRed, propGroup
);
box(
0.49, 0.53, 0.49,
lighthouseX, lighthouseY + 1.53, lighthouseZ,
lighthouseWhite, propGroup
);
box(
0.56, 0.16, 0.56,
lighthouseX, lighthouseY + 1.88, lighthouseZ,
lighthouseGlass, propGroup
);
box(
0.69, 0.15, 0.69,
lighthouseX, lighthouseY + 2.03, lighthouseZ,
roofMaterial, propGroup
);
// Endpoint beacons.
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0x19b99a,
emissive: 0x0c775f,
emissiveIntensity: 0.35,
roughness: 0.5
});
const endBeaconMaterial = new THREE.MeshStandardMaterial({
color: 0x1788bb,
emissive: 0x0b577d,
emissiveIntensity: 0.35,
roughness: 0.5
});
for (const [id, mat] of [["A", beaconMaterial], ["B", endBeaconMaterial]]) {
const point = nodes[id];
box(
0.68, 0.14, 0.68,
point.x, point.y + 0.08, point.z,
material.woodDark, propGroup
);
box(
0.13, 0.72, 0.13,
point.x, point.y + 0.44, point.z,
mat, propGroup
);
box(
0.44, 0.22, 0.44,
point.x, point.y + 0.88, point.z,
mat, propGroup
);
}
// -------------------------------------------------------------------------
// DOM labels projected into the 3D scene
// -------------------------------------------------------------------------
const labels = [];
const labelRoot = document.getElementById("labels");
function addWorldLabel(className, html, position) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.innerHTML = html;
labelRoot.appendChild(element);
labels.push({ element, position: position.clone() });
return element;
}
addWorldLabel(
"endpoint",
"A",
nodes.A.clone().add(new THREE.Vector3(0, 1.24, 0))
);
addWorldLabel(
"endpoint b",
"B",
nodes.B.clone().add(new THREE.Vector3(0, 1.24, 0))
);
addWorldLabel(
"measure-label",
`<strong>DIRECT ${directDistance.toFixed(1)} m</strong>
<span>Open water · blocked</span>`,
directMid.clone().add(new THREE.Vector3(0, 0.28, 0))
);
addWorldLabel(
"bridge-label",
"Central footbridge",
new THREE.Vector3(0, 1.52, 0)
);
addWorldLabel(
"bridge-label",
"North bridge",
new THREE.Vector3(0, 1.52, 2.5)
);
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labels) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
label.element.style.opacity = visible ? "1" : "0";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
}
// -------------------------------------------------------------------------
// Custom camera controls — no Three.js addons
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
baseViewSize = aspect < 0.85 ? 12.8 : 9.3;
camera.left = (-baseViewSize * aspect) / orbit.zoom;
camera.right = (baseViewSize * aspect) / orbit.zoom;
camera.top = baseViewSize / orbit.zoom;
camera.bottom = -baseViewSize / orbit.zoom;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
camera.position.set(
orbit.target.x + cameraRadius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + cameraRadius * cosPhi,
orbit.target.z + cameraRadius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (event.shiftKey || event.buttons === 2) {
const scale = (baseViewSize * 2) / window.innerHeight / orbit.zoom;
const right = new THREE.Vector3(
Math.cos(orbit.theta),
0,
-Math.sin(orbit.theta)
);
const forward = new THREE.Vector3(
-Math.sin(orbit.theta),
0,
-Math.cos(orbit.theta)
);
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(forward, dy * scale);
orbit.target.x = THREE.MathUtils.clamp(orbit.target.x, -5, 5);
orbit.target.z = THREE.MathUtils.clamp(orbit.target.z, -5, 5);
} else {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi + dy * 0.005, 0.08, 1.38);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.8
);
resize();
updateCamera();
},
{ passive: false }
);
const isoButton = document.getElementById("isoButton");
const topButton = document.getElementById("topButton");
const resetButton = document.getElementById("resetButton");
function setView(view) {
if (view === "top") {
orbit.theta = 0;
orbit.phi = 0.08;
topButton.classList.add("active");
isoButton.classList.remove("active");
} else {
orbit.theta = 0.72;
orbit.phi = 0.86;
isoButton.classList.add("active");
topButton.classList.remove("active");
}
updateCamera();
}
isoButton.addEventListener("click", () => setView("iso"));
topButton.addEventListener("click", () => setView("top"));
resetButton.addEventListener("click", () => {
orbit.zoom = 1;
orbit.target.set(0, 0.35, 0);
setView("iso");
resize();
updateCamera();
});
const directToggle = document.getElementById("directToggle");
const routeToggle = document.getElementById("routeToggle");
directToggle.addEventListener("click", () => {
directGroup.visible = !directGroup.visible;
directToggle.classList.toggle("off", !directGroup.visible);
});
routeToggle.addEventListener("click", () => {
routeGroup.visible = !routeGroup.visible;
routeToggle.classList.toggle("off", !routeGroup.visible);
});
window.addEventListener("resize", () => {
resize();
updateCamera();
});
resize();
updateCamera();
// -------------------------------------------------------------------------
// Animation: moving route pulse and subtle water motion
// -------------------------------------------------------------------------
const routeSegmentLengths = [];
for (const edge of shortest.edges) {
routeSegmentLengths.push(edge.weight);
}
const routeTotalLength = routeSegmentLengths.reduce(
(sum, length) => sum + length,
0
);
function pointAlongRoute(distance) {
let remaining = THREE.MathUtils.euclideanModulo(
distance,
routeTotalLength
);
for (let i = 0; i < shortest.edges.length; i++) {
const edge = shortest.edges[i];
const length = routeSegmentLengths[i];
if (remaining <= length) {
const t = length === 0 ? 0 : remaining / length;
return nodes[edge.a].clone().lerp(nodes[edge.b], t);
}
remaining -= length;
}
return nodes.B.clone();
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
for (const wave of waves) {
wave.mesh.position.y =
0.005 + Math.sin(elapsed * 0.85 + wave.phase) * 0.025;
}
if (routeGroup.visible) {
const routePoint = pointAlongRoute(
elapsed * 2.15
).add(new THREE.Vector3(0, 0.25, 0));
routePulse.position.copy(routePoint);
routeHalo.position.copy(routePoint);
routeHalo.rotation.y = elapsed * 1.4;
routeHalo.rotation.x = elapsed * 0.8;
const haloScale = 0.49 + Math.sin(elapsed * 5) * 0.05;
routeHalo.scale.set(haloScale, haloScale, haloScale);
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```