Prompt specification
Build or customize the Three.js 3D scene "Choose a route between upper lane and coastal descent" 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: Choose a route between upper lane and coastal descent
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/choose-a-route-between-upper-lane-and-coastal-descent-x2748
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: Choose a route between upper lane and coastal descent. Let users inspect a schematic Barranco path network from two elevations. Show the necessary connection points and compare direct visual distance with actual walking distance.
Working Three.js Starter (choose-a-route-between-upper-lane-and-coastal-descent-x2748):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Barranco — Voxel Route Study</title>
<style>
:root {
--ink: #142b2b;
--muted: #657773;
--paper: #f4efe2;
--paper-2: #e8e1d1;
--line: rgba(20, 43, 43, 0.14);
--upper: #f2b85b;
--coast: #42d4ca;
--shadow: 0 18px 50px rgba(18, 36, 36, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c8d8d3;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.panel {
position: fixed;
z-index: 10;
background: rgba(247, 243, 233, 0.93);
border: 1px solid rgba(255, 255, 255, 0.68);
box-shadow: var(--shadow);
backdrop-filter: blur(16px) saturate(1.08);
-webkit-backdrop-filter: blur(16px) saturate(1.08);
}
.route-panel {
top: 22px;
left: 22px;
width: 350px;
max-height: calc(100vh - 44px);
overflow: auto;
border-radius: 18px;
padding: 22px;
scrollbar-width: thin;
}
.view-panel {
top: 22px;
right: 22px;
width: 238px;
border-radius: 18px;
padding: 17px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
background: var(--coast);
box-shadow: 13px 0 0 var(--upper);
margin-right: 13px;
}
h1 {
margin: 0;
font-size: 34px;
line-height: 0.98;
letter-spacing: -0.055em;
}
.subtitle {
margin: 10px 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.section-label {
margin: 18px 0 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.route-choices {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.route-choice,
.view-choice {
appearance: none;
border: 1px solid var(--line);
color: var(--ink);
background: rgba(255, 255, 255, 0.43);
cursor: pointer;
transition:
transform 150ms ease,
border-color 150ms ease,
background 150ms ease,
box-shadow 150ms ease;
}
.route-choice {
min-height: 66px;
border-radius: 12px;
padding: 11px;
text-align: left;
}
.route-choice:hover,
.view-choice:hover {
transform: translateY(-1px);
border-color: rgba(20, 43, 43, 0.3);
}
.route-choice.active {
background: #fffaf0;
border-color: var(--route-color);
box-shadow: inset 0 0 0 1px var(--route-color);
}
.route-choice .route-name {
display: block;
margin-top: 7px;
font-size: 12px;
font-weight: 800;
}
.route-dot {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 5px;
border-radius: 2px;
background: var(--route-color);
vertical-align: 1px;
}
.route-description {
min-height: 38px;
margin: 11px 1px 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.metric {
min-height: 86px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.45);
}
.metric.walking {
background: #fffaf0;
border-color: color-mix(in srgb, var(--route-color) 48%, transparent);
}
.metric-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.metric-value {
display: block;
margin-top: 7px;
font-size: 24px;
font-weight: 800;
letter-spacing: -0.05em;
}
.metric-sub {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.comparison {
margin-top: 9px;
padding: 10px 11px;
border-radius: 10px;
color: #fffaf0;
background: var(--ink);
font-size: 10px;
line-height: 1.45;
}
.comparison strong {
color: var(--route-color);
}
.ledger {
margin: 0;
padding: 0;
list-style: none;
}
.ledger li {
display: grid;
grid-template-columns: 24px 1fr auto;
gap: 8px;
align-items: center;
min-height: 40px;
border-bottom: 1px solid var(--line);
cursor: pointer;
}
.ledger li:hover .ledger-name {
color: var(--route-color);
}
.ledger-index {
display: grid;
place-items: center;
width: 21px;
height: 21px;
border-radius: 6px;
color: #fffaf0;
background: var(--route-color);
font-size: 8px;
font-weight: 900;
}
.ledger-name {
display: block;
font-size: 10px;
font-weight: 750;
transition: color 150ms ease;
}
.ledger-elevation {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
}
.ledger-distance {
color: var(--muted);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.view-panel h2 {
margin: 0 0 11px;
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.view-choices {
display: grid;
gap: 6px;
}
.view-choice {
width: 100%;
min-height: 35px;
padding: 8px 10px;
border-radius: 9px;
text-align: left;
font-size: 10px;
font-weight: 750;
}
.view-choice.active {
color: #fffaf0;
border-color: var(--ink);
background: var(--ink);
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 11px;
padding-top: 11px;
border-top: 1px solid var(--line);
font-size: 10px;
font-weight: 700;
}
.toggle-row input {
width: 15px;
height: 15px;
accent-color: var(--ink);
cursor: pointer;
}
.node-info {
min-height: 90px;
margin-top: 13px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.4);
}
.node-info .node-kicker {
color: var(--route-color, var(--coast));
font-size: 8px;
font-weight: 900;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.node-info strong {
display: block;
margin-top: 4px;
font-size: 13px;
}
.node-info p {
margin: 5px 0 0;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
.legend {
display: grid;
gap: 7px;
margin-top: 12px;
color: var(--muted);
font-size: 9px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
}
.legend-swatch {
width: 20px;
height: 5px;
border-radius: 2px;
}
.legend-swatch.sightline {
height: 0;
border-top: 2px dashed #fff8df;
background: transparent;
}
.hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 19px;
transform: translateX(-50%);
padding: 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.4);
border-radius: 999px;
color: rgba(255, 250, 240, 0.9);
background: rgba(20, 43, 43, 0.76);
backdrop-filter: blur(8px);
font-size: 9px;
letter-spacing: 0.03em;
white-space: nowrap;
pointer-events: none;
}
.world-label {
position: absolute;
left: 0;
top: 0;
display: flex;
align-items: center;
gap: 6px;
max-width: 175px;
padding: 5px 7px;
border: 1px solid rgba(255, 255, 255, 0.4);
border-radius: 7px;
color: #fffaf0;
background: rgba(20, 43, 43, 0.82);
box-shadow: 0 5px 16px rgba(20, 43, 43, 0.16);
backdrop-filter: blur(7px);
font-size: 8px;
font-weight: 750;
white-space: nowrap;
will-change: transform;
}
.world-label .label-index {
display: grid;
place-items: center;
width: 18px;
height: 18px;
flex: 0 0 auto;
border-radius: 5px;
color: var(--ink);
background: var(--label-color, #f2b85b);
font-size: 7px;
font-weight: 900;
}
.world-label.junction {
border-color: rgba(255, 255, 255, 0.76);
box-shadow: 0 0 0 2px rgba(244, 239, 226, 0.22);
}
.world-label.direct {
border: 1px dashed rgba(255, 248, 223, 0.82);
color: #fff8df;
background: rgba(20, 43, 43, 0.56);
font-size: 8px;
letter-spacing: 0.08em;
}
.footnote {
margin-top: 13px;
color: #81908a;
font-size: 8px;
line-height: 1.45;
}
@media (max-width: 820px) {
.route-panel {
top: 10px;
left: 10px;
width: min(340px, calc(100vw - 20px));
max-height: 46vh;
padding: 17px;
border-radius: 14px;
}
h1 {
font-size: 27px;
}
.view-panel {
top: auto;
right: 10px;
bottom: 48px;
width: 205px;
padding: 13px;
border-radius: 14px;
}
.hint {
left: 10px;
right: 10px;
bottom: 10px;
transform: none;
text-align: center;
white-space: normal;
}
}
@media (max-height: 690px) {
.route-panel {
max-height: calc(100vh - 20px);
top: 10px;
}
.subtitle,
.route-description,
.footnote {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel map of Barranco routes"></canvas>
<div id="labels" aria-hidden="true"></div>
<section class="panel route-panel" aria-label="Route comparison">
<div class="eyebrow">Field model 07 / Barranco</div>
<h1>Upper lane to<br />coastal descent</h1>
<p class="subtitle">
Compare two schematic path connections through the cliff network.
Select a route to reveal its required gates and walking distance.
</p>
<div class="section-label">Choose a route</div>
<div class="route-choices">
<button
class="route-choice active"
type="button"
data-route="upper"
style="--route-color: var(--upper)"
>
<span class="route-dot"></span><span style="font-size:9px;color:var(--muted)">LONGER CONTOUR</span>
<span class="route-name">Upper lane</span>
</button>
<button
class="route-choice"
type="button"
data-route="coast"
style="--route-color: var(--coast)"
>
<span class="route-dot"></span><span style="font-size:9px;color:var(--muted)">STEEPER SHORTCUT</span>
<span class="route-name">Coastal descent</span>
</button>
</div>
<p id="route-description" class="route-description"></p>
<div class="section-label">Distance comparison</div>
<div class="metrics">
<div class="metric">
<span class="metric-label">Direct visual</span>
<strong id="direct-distance" class="metric-value">—</strong>
<span class="metric-sub">Straight 3D sightline</span>
</div>
<div class="metric walking">
<span class="metric-label">Actual walking</span>
<strong id="walking-distance" class="metric-value">—</strong>
<span class="metric-sub">Summed path centerline</span>
</div>
</div>
<div id="comparison" class="comparison"></div>
<div class="section-label">Required connection points</div>
<ol id="ledger" class="ledger"></ol>
<p class="footnote">
Schematic model, not survey-grade. Elevations and distances are illustrative.
The dashed line is the uninterrupted visual distance; the illuminated route
follows every required connection point.
</p>
</section>
<aside class="panel view-panel" aria-label="View controls">
<h2>Inspection views</h2>
<div class="view-choices">
<button class="view-choice active" type="button" data-view="upper">
Upper elevation
</button>
<button class="view-choice" type="button" data-view="coast">
Coastal elevation
</button>
<button class="view-choice" type="button" data-view="plan">
Plan overview
</button>
</div>
<label class="toggle-row">
<span>Direct sightline</span>
<input id="sightline-toggle" type="checkbox" checked />
</label>
<label class="toggle-row">
<span>Connection labels</span>
<input id="points-toggle" type="checkbox" checked />
</label>
<div id="node-info" class="node-info"></div>
<div class="legend">
<div class="legend-item">
<span class="legend-swatch" style="background:var(--upper)"></span>
Upper-lane network
</div>
<div class="legend-item">
<span class="legend-swatch" style="background:var(--coast)"></span>
Coastal network
</div>
<div class="legend-item">
<span class="legend-swatch sightline"></span>
Direct visual distance
</div>
</div>
</aside>
<div class="hint">
Drag to orbit · Shift/right-drag to pan · Scroll to zoom · Select a marker for details
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const labelsRoot = document.querySelector("#labels");
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc8d8d3);
scene.fog = new THREE.Fog(0xc8d8d3, 43, 92);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
160
);
const hemisphere = new THREE.HemisphereLight(0xe8f4ef, 0x765a42, 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d1, 3.2);
sun.position.set(-18, 35, 14);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -32;
sun.shadow.camera.right = 32;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 85;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9ecfd0, 0.65);
fill.position.set(24, 12, -20);
scene.add(fill);
// -------------------------------------------------------------------------
// Custom camera controls — no addons
// -------------------------------------------------------------------------
class CameraRig {
constructor(cameraObject, domElement) {
this.camera = cameraObject;
this.dom = domElement;
this.target = new THREE.Vector3(-2, 5, -2);
this.spherical = new THREE.Spherical();
this.transition = null;
this.drag = null;
this.onPointerDown = this.onPointerDown.bind(this);
this.onPointerMove = this.onPointerMove.bind(this);
this.onPointerUp = this.onPointerUp.bind(this);
this.onWheel = this.onWheel.bind(this);
this.dom.addEventListener("pointerdown", this.onPointerDown);
this.dom.addEventListener("pointermove", this.onPointerMove);
this.dom.addEventListener("pointerup", this.onPointerUp);
this.dom.addEventListener("pointercancel", this.onPointerUp);
this.dom.addEventListener("wheel", this.onWheel, { passive: false });
this.dom.addEventListener("contextmenu", (event) => event.preventDefault());
}
syncFromCamera() {
const offset = this.camera.position.clone().sub(this.target);
this.spherical.setFromVector3(offset);
}
applySpherical() {
const offset = new THREE.Vector3().setFromSpherical(this.spherical);
this.camera.position.copy(this.target).add(offset);
this.camera.lookAt(this.target);
}
onPointerDown(event) {
if (event.target !== this.dom) return;
this.transition = null;
this.drag = {
mode: event.button === 2 || event.shiftKey ? "pan" : "orbit",
x: event.clientX,
y: event.clientY
};
this.dom.setPointerCapture(event.pointerId);
}
onPointerMove(event) {
if (!this.drag) return;
const dx = event.clientX - this.drag.x;
const dy = event.clientY - this.drag.y;
this.drag.x = event.clientX;
this.drag.y = event.clientY;
if (this.drag.mode === "orbit") {
this.spherical.theta -= dx * 0.005;
this.spherical.phi -= dy * 0.005;
this.spherical.phi = THREE.MathUtils.clamp(
this.spherical.phi,
0.13,
Math.PI * 0.47
);
this.applySpherical();
} else {
const scale = this.spherical.radius * 0.0013;
const right = new THREE.Vector3();
const up = new THREE.Vector3();
this.camera.matrixWorld.extractBasis(right, up, new THREE.Vector3());
this.target.addScaledVector(right, -dx * scale);
this.target.addScaledVector(up, dy * scale);
this.applySpherical();
}
}
onPointerUp(event) {
if (!this.drag) return;
this.drag = null;
if (this.dom.hasPointerCapture(event.pointerId)) {
this.dom.releasePointerCapture(event.pointerId);
}
}
onWheel(event) {
event.preventDefault();
this.transition = null;
this.spherical.radius *= Math.exp(event.deltaY * 0.001);
this.spherical.radius = THREE.MathUtils.clamp(
this.spherical.radius,
10,
72
);
this.applySpherical();
}
transitionTo(position, target, duration = 900) {
this.transition = {
start: performance.now(),
duration,
fromPosition: this.camera.position.clone(),
fromTarget: this.target.clone(),
toPosition: position.clone(),
toTarget: target.clone()
};
}
focus(point, radius = 18) {
const direction = this.camera.position
.clone()
.sub(this.target)
.normalize();
const position = point.clone().add(direction.multiplyScalar(radius));
this.transitionTo(position, point.clone(), 750);
}
update(now) {
if (!this.transition) {
this.applySpherical();
return;
}
const transition = this.transition;
const t = THREE.MathUtils.clamp(
(now - transition.start) / transition.duration,
0,
1
);
const eased = 1 - Math.pow(1 - t, 3);
this.camera.position.lerpVectors(
transition.fromPosition,
transition.toPosition,
eased
);
this.target.lerpVectors(
transition.fromTarget,
transition.toTarget,
eased
);
this.camera.lookAt(this.target);
if (t >= 1) {
this.transition = null;
this.syncFromCamera();
}
}
}
const controls = new CameraRig(camera, canvas);
const views = {
upper: {
position: new THREE.Vector3(-24, 18, -25),
target: new THREE.Vector3(-2, 5, -2)
},
coast: {
position: new THREE.Vector3(25, 10, 25),
target: new THREE.Vector3(2, 3, 5)
},
plan: {
position: new THREE.Vector3(0, 44, 0.01),
target: new THREE.Vector3(0, 3, 0)
}
};
camera.position.copy(views.upper.position);
controls.target.copy(views.upper.target);
controls.syncFromCamera();
// -------------------------------------------------------------------------
// Route graph
// -------------------------------------------------------------------------
const nodeData = {
S: {
name: "Upper lane",
description: "Lane-side origin at the signed overlook.",
p: [-13, 9.15, -8]
},
u1: {
name: "Lane bend",
description: "First gate where the upper route leaves the contour lane.",
p: [-8, 9.1, -8]
},
u2: {
name: "West overlook",
description: "A narrow shelf above the ravine before the ridge traverse.",
p: [-4, 8.8, -5]
},
u3: {
name: "Ridge gate",
description: "Required ridge connection into the central switchback section.",
p: [-1, 8.0, -3]
},
u4: {
name: "Upper switchback",
description: "The contour route changes direction above the lower terrace.",
p: [2, 6.7, 0]
},
u5: {
name: "Upper spur",
description: "Short link toward the shared sea junction.",
p: [0, 6.3, 3]
},
c1: {
name: "Stair head",
description: "Where the coastal branch commits to the stepped descent.",
p: [-11, 8.85, -4]
},
c2: {
name: "Cliff landing",
description: "Intermediate landing above the lower coastal terrace.",
p: [-7, 8.65, 0]
},
c3: {
name: "Lower terrace",
description: "Steepest part of the branch, where the stair meets the sea path.",
p: [-3, 6.85, 3]
},
c4: {
name: "Sea path",
description: "Low coastal connection leading toward the common junction.",
p: [1, 5.25, 5]
},
C: {
name: "Sea junction",
description:
"Required shared connection: both alternatives rejoin here before the final descent.",
p: [4, 4.15, 4]
},
E: {
name: "Coastal descent",
description: "Water-side terminus reached by the final shared link.",
p: [10, 1.8, 8]
}
};
const NODES = Object.fromEntries(
Object.entries(nodeData).map(([key, data]) => [
key,
{
...data,
p: new THREE.Vector3(...data.p)
}
])
);
const routes = {
upper: {
name: "Upper lane",
color: 0xf2b85b,
cssColor: "#f2b85b",
nodes: ["S", "u1", "u2", "u3", "u4", "u5", "C", "E"],
description:
"A broad contour traverse with several switchbacks. It keeps more of the path above the ravine, but adds extra centerline distance."
},
coast: {
name: "Coastal descent",
color: 0x42d4ca,
cssColor: "#42d4ca",
nodes: ["S", "c1", "c2", "c3", "c4", "C", "E"],
description:
"A steeper stepped branch that cuts toward the sea. It rejoins the upper route at the shared sea junction."
}
};
for (const route of Object.values(routes)) {
route.edges = route.nodes.slice(1).map((node, index) => [
route.nodes[index],
node
]);
}
const edgeMap = new Map();
for (const route of Object.values(routes)) {
for (const edge of route.edges) {
const key = [...edge].sort().join("-");
edgeMap.set(key, edge);
}
}
const allEdges = [...edgeMap.values()];
function routeMeasurements(route) {
const cumulative = [0];
for (let i = 1; i < route.nodes.length; i++) {
cumulative.push(
cumulative[i - 1] +
NODES[route.nodes[i - 1]].p.distanceTo(
NODES[route.nodes[i]].p
)
);
}
return {
cumulative,
total: cumulative[cumulative.length - 1]
};
}
const directDistance = NODES.S.p.distanceTo(NODES.E.p);
const state = {
route: "upper",
showPoints: true,
showSightline: true
};
// -------------------------------------------------------------------------
// Voxel terrain
// -------------------------------------------------------------------------
function terrainHeight(x, z) {
const height =
8.3 -
Math.max(0, x + 4) * 0.55 -
Math.max(0, z + 1) * 0.32 +
Math.max(0, -z - 5) * 0.15;
return Math.max(-1.5, Math.round(height / 0.35) * 0.35);
}
const terrainCells = [];
const terrainStep = 1.45;
for (let x = -18; x <= 11.5; x += terrainStep) {
for (let z = -14; z <= 11.5; z += terrainStep) {
const height = terrainHeight(x, z);
if (height < -0.55) continue;
terrainCells.push({ x, z, height });
}
}
const terrainGeometry = new THREE.BoxGeometry(1, 1, 1);
const terrainMaterial = new THREE.MeshStandardMaterial({
roughness: 0.96,
metalness: 0,
vertexColors: true
});
const terrain = new THREE.InstancedMesh(
terrainGeometry,
terrainMaterial,
terrainCells.length
);
terrain.castShadow = true;
terrain.receiveShadow = true;
const dummy = new THREE.Object3D();
const color = new THREE.Color();
const palette = {
high: new THREE.Color(0x94715b),
middle: new THREE.Color(0xb38a65),
low: new THREE.Color(0xc6a977),
sand: new THREE.Color(0xd7c18f)
};
terrainCells.forEach((cell, index) => {
const bottom = -1.45;
const height = cell.height - bottom;
dummy.position.set(
cell.x,
bottom + height * 0.5,
cell.z
);
dummy.scale.set(
terrainStep - 0.055,
height,
terrainStep - 0.055
);
dummy.updateMatrix();
terrain.setMatrixAt(index, dummy.matrix);
if (cell.height > 6.2) {
color.copy(palette.high);
} else if (cell.height > 4.0) {
color.copy(palette.middle);
} else if (cell.height > 1.4) {
color.copy(palette.low);
} else {
color.copy(palette.sand);
}
const variation =
0.94 +
0.08 *
Math.abs(
Math.sin(cell.x * 3.17 + cell.z * 2.31)
);
color.multiplyScalar(variation);
terrain.setColorAt(index, color);
});
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
terrain.computeBoundingSphere();
scene.add(terrain);
// Sea
const sea = new THREE.Mesh(
new THREE.BoxGeometry(54, 0.3, 44),
new THREE.MeshStandardMaterial({
color: 0x3d91a1,
roughness: 0.3,
metalness: 0.04,
transparent: true,
opacity: 0.92
})
);
sea.position.set(5, -1.02, 0);
sea.receiveShadow = true;
scene.add(sea);
// Voxel wave streaks
const waveCount = 72;
const waveMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 0.035, 0.09),
new THREE.MeshBasicMaterial({
color: 0xd6f1e9,
transparent: true,
opacity: 0.42
}),
waveCount
);
let seed = 741;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
for (let i = 0; i < waveCount; i++) {
const x = -1 + random() * 25;
const z = -18 + random() * 36;
const width = 0.55 + random() * 2.2;
dummy.position.set(x, -0.845, z);
dummy.scale.set(width, 1, 1);
dummy.updateMatrix();
waveMesh.setMatrixAt(i, dummy.matrix);
}
waveMesh.instanceMatrix.needsUpdate = true;
scene.add(waveMesh);
// -------------------------------------------------------------------------
// Path tiles
// -------------------------------------------------------------------------
function buildPathTiles(edges, tileColor, size, spacing, opacity, lift) {
const transforms = [];
for (const [from, to] of edges) {
const start = NODES[from].p;
const end = NODES[to].p;
const distance = start.distanceTo(end);
const count = Math.max(1, Math.ceil(distance / spacing));
for (let i = 0; i <= count; i++) {
const t = i / count;
const point = new THREE.Vector3().lerpVectors(start, end, t);
point.y += lift;
transforms.push(point);
}
}
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: tileColor,
roughness: 0.72,
transparent: opacity < 1,
opacity
}),
transforms.length
);
transforms.forEach((point, index) => {
dummy.position.copy(point);
dummy.scale.set(size, 0.16, size);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.computeBoundingSphere();
scene.add(mesh);
return mesh;
}
const baseNetwork = buildPathTiles(
allEdges,
0x5e7169,
0.29,
0.38,
0.38,
0.10
);
const routeMeshes = {
upper: buildPathTiles(
routes.upper.edges,
routes.upper.color,
0.47,
0.37,
1,
0.19
),
coast: buildPathTiles(
routes.coast.edges,
routes.coast.color,
0.47,
0.37,
1,
0.19
)
};
routeMeshes.upper.visible = true;
routeMeshes.coast.visible = false;
// -------------------------------------------------------------------------
// Direct sightline
// -------------------------------------------------------------------------
const sightline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
NODES.S.p,
NODES.E.p
]),
new THREE.LineDashedMaterial({
color: 0xfff5d8,
dashSize: 0.48,
gapSize: 0.3,
transparent: true,
opacity: 0.7,
depthTest: false
})
);
sightline.computeLineDistances();
sightline.renderOrder = 10;
scene.add(sightline);
// -------------------------------------------------------------------------
// Nodes and connection-point markers
// -------------------------------------------------------------------------
const nodeMeshes = [];
const nodeFrames = [];
const nodeLabels = {};
function createNodeMarker(key) {
const node = NODES[key];
const geometry = new THREE.BoxGeometry(0.64, 0.64, 0.64);
const material = new THREE.MeshStandardMaterial({
color: 0x50635d,
roughness: 0.6,
metalness: 0.08
});
const marker = new THREE.Mesh(geometry, material);
marker.position.copy(node.p);
marker.position.y += 0.28;
marker.castShadow = true;
marker.receiveShadow = true;
marker.userData.nodeKey = key;
scene.add(marker);
nodeMeshes.push(marker);
const frame = new THREE.Group();
frame.position.copy(node.p);
frame.position.y += 0.05;
const frameMaterial = new THREE.MeshBasicMaterial({
color: 0xf2b85b,
transparent: true,
opacity: 0.9
});
const long = 1.02;
const thickness = 0.07;
const north = new THREE.Mesh(
new THREE.BoxGeometry(long, thickness, thickness),
frameMaterial
);
north.position.z = -0.46;
const south = north.clone();
south.position.z = 0.46;
const east = new THREE.Mesh(
new THREE.BoxGeometry(thickness, thickness, long),
frameMaterial
);
east.position.x = 0.46;
const west = east.clone();
west.position.x = -0.46;
frame.add(north, south, east, west);
scene.add(frame);
nodeFrames[key] = frame;
const label = document.createElement("div");
label.className = `world-label${key === "C" ? " junction" : ""}`;
label.innerHTML = `
<span class="label-index">01</span>
<span>${node.name}</span>
`;
label.dataset.enabled = "0";
labelsRoot.appendChild(label);
nodeLabels[key] = {
element: label,
position: node.p.clone().add(new THREE.Vector3(0, 1.12, 0))
};
}
Object.keys(NODES).forEach(createNodeMarker);
// -------------------------------------------------------------------------
// Small voxel landmarks
// -------------------------------------------------------------------------
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x69503b,
roughness: 1
});
const crownMaterial = new THREE.MeshStandardMaterial({
color: 0x64775a,
roughness: 1
});
const allRouteNodes = Object.values(NODES).map((node) => node.p);
function addTree(x, z) {
const ground = terrainHeight(x, z);
if (ground < 2.5 || ground > 8.9) return;
const tooClose = allRouteNodes.some(
(point) => Math.hypot(point.x - x, point.z - z) < 2.1
);
if (tooClose) return;
const tree = new THREE.Group();
tree.position.set(x, ground, z);
const trunk = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.8, 0.2),
trunkMaterial
);
trunk.position.y = 0.4;
const crown = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.72, 0.72),
crownMaterial
);
crown.position.y = 1.03;
const crownTop = new THREE.Mesh(
new THREE.BoxGeometry(0.46, 0.38, 0.46),
crownMaterial
);
crownTop.position.set(0.08, 1.52, -0.04);
tree.add(trunk, crown, crownTop);
tree.rotation.y = random() * Math.PI;
tree.castShadow = true;
scene.add(tree);
}
let treeAttempts = 0;
let treeCount = 0;
while (treeCount < 24 && treeAttempts < 250) {
treeAttempts++;
const x = -17 + random() * 19;
const z = -12 + random() * 22;
const before = scene.children.length;
addTree(x, z);
if (scene.children.length > before) treeCount++;
}
// Upper-lane lookout building
const lookout = new THREE.Group();
lookout.position.set(-14.5, 8.75, -9.4);
const wall = new THREE.Mesh(
new THREE.BoxGeometry(1.65, 1.15, 1.35),
new THREE.MeshStandardMaterial({ color: 0xe8dfcc, roughness: 0.95 })
);
wall.position.y = 0.58;
const roof = new THREE.Mesh(
new THREE.BoxGeometry(1.9, 0.28, 1.58),
new THREE.MeshStandardMaterial({ color: 0xb65f45, roughness: 0.9 })
);
roof.position.y = 1.28;
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x345d61,
emissive: 0x102528,
roughness: 0.4
});
const windowMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.44, 0.42, 0.04),
windowMaterial
);
windowMesh.position.set(0.36, 0.67, 0.69);
lookout.add(wall, roof, windowMesh);
lookout.castShadow = true;
scene.add(lookout);
// Coastal landing and pier
const pierMaterial = new THREE.MeshStandardMaterial({
color: 0x8f7456,
roughness: 0.9
});
for (let x = 4.4; x <= 11.2; x += 1.05) {
const plank = new THREE.Mesh(
new THREE.BoxGeometry(0.92, 0.28, 2.0),
pierMaterial
);
plank.position.set(x, 1.27, 8.2);
plank.castShadow = true;
plank.receiveShadow = true;
scene.add(plank);
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 1.2, 0.14),
pierMaterial
);
post.position.set(x + 0.4, 0.62, 7.35);
post.castShadow = true;
scene.add(post);
}
// Small coastal marker tower
const tower = new THREE.Group();
tower.position.set(11.4, 1.4, 10.2);
const towerBody = new THREE.Mesh(
new THREE.BoxGeometry(0.65, 1.75, 0.65),
new THREE.MeshStandardMaterial({ color: 0xf3ead6, roughness: 0.9 })
);
towerBody.position.y = 0.88;
const towerTop = new THREE.Mesh(
new THREE.BoxGeometry(0.82, 0.28, 0.82),
new THREE.MeshStandardMaterial({ color: 0xc9684c, roughness: 0.8 })
);
towerTop.position.y = 1.88;
tower.add(towerBody, towerTop);
tower.castShadow = true;
scene.add(tower);
// -------------------------------------------------------------------------
// UI and interaction
// -------------------------------------------------------------------------
const directDistanceElement = document.querySelector("#direct-distance");
const walkingDistanceElement = document.querySelector("#walking-distance");
const comparisonElement = document.querySelector("#comparison");
const routeDescriptionElement = document.querySelector("#route-description");
const ledgerElement = document.querySelector("#ledger");
const nodeInfoElement = document.querySelector("#node-info");
function updateNodeStyles() {
const route = routes[state.route];
for (const [key, marker] of nodeMeshes.entries()) {
const nodeKey = marker.userData.nodeKey;
const selected = route.nodes.includes(nodeKey);
const endpoint = nodeKey === "S" || nodeKey === "E";
marker.material.color.set(
selected ? route.color : 0x50635d
);
marker.scale.setScalar(
selected ? (endpoint ? 1.2 : 1) : 0.82
);
const frame = nodeFrames[nodeKey];
frame.visible = selected && state.showPoints;
frame.children.forEach((part) => {
part.material.color.set(route.color);
});
const index = route.nodes.indexOf(nodeKey);
const label = nodeLabels[nodeKey];
label.element.querySelector(".label-index").textContent =
String(index + 1).padStart(2, "0");
label.element.style.setProperty(
"--label-color",
route.cssColor
);
label.element.dataset.enabled =
selected && state.showPoints ? "1" : "0";
}
}
function showNodeInfo(key) {
const route = routes[state.route];
const node = NODES[key];
const index = route.nodes.indexOf(key);
const measurements = routeMeasurements(route);
const status =
key === "C"
? "Shared / required junction"
: key === "S"
? "Route origin"
: key === "E"
? "Route terminus"
: "Required connection";
nodeInfoElement.innerHTML = `
<div class="node-kicker">${status}</div>
<strong>${node.name}</strong>
<p>
${node.description}<br />
Elevation ${node.p.y.toFixed(1)} m ·
${
index >= 0
? `${measurements.cumulative[index].toFixed(1)} m from origin`
: "Outside selected route"
}
</p>
`;
nodeInfoElement.style.setProperty(
"--route-color",
route.cssColor
);
}
function updateRouteUI() {
const route = routes[state.route];
const measurements = routeMeasurements(route);
const difference = measurements.total - directDistance;
const ratio = measurements.total / directDistance;
document.querySelectorAll(".route-choice").forEach((button) => {
button.classList.toggle(
"active",
button.dataset.route === state.route
);
});
routeDescriptionElement.textContent = route.description;
directDistanceElement.textContent = `${directDistance.toFixed(1)} m`;
walkingDistanceElement.textContent = `${measurements.total.toFixed(1)} m`;
comparisonElement.innerHTML = `
This route adds <strong>${difference.toFixed(1)} m</strong>
beyond the sightline and measures
<strong>${ratio.toFixed(2)}×</strong> the direct visual distance.
`;
comparisonElement.style.setProperty(
"--route-color",
route.cssColor
);
ledgerElement.innerHTML = route.nodes
.map((key, index) => {
const node = NODES[key];
return `
<li data-node="${key}">
<span class="ledger-index">
${String(index + 1).padStart(2, "0")}
</span>
<span>
<span class="ledger-name">${node.name}</span>
<span class="ledger-elevation">
Elevation ${node.p.y.toFixed(1)} m
${key === "C" ? " · shared junction" : ""}
</span>
</span>
<span class="ledger-distance">
${measurements.cumulative[index].toFixed(1)} m
</span>
</li>
`;
})
.join("");
ledgerElement.querySelectorAll("li").forEach((item) => {
item.addEventListener("click", () => {
const key = item.dataset.node;
showNodeInfo(key);
controls.focus(NODES[key].p, 16);
});
});
routeMeshes.upper.visible = state.route === "upper";
routeMeshes.coast.visible = state.route === "coast";
updateNodeStyles();
showNodeInfo("C");
}
document.querySelectorAll(".route-choice").forEach((button) => {
button.addEventListener("click", () => {
state.route = button.dataset.route;
updateRouteUI();
});
});
document.querySelectorAll(".view-choice").forEach((button) => {
button.addEventListener("click", () => {
document.querySelectorAll(".view-choice").forEach((choice) => {
choice.classList.toggle("active", choice === button);
});
const view = views[button.dataset.view];
controls.transitionTo(view.position, view.target);
});
});
document
.querySelector("#sightline-toggle")
.addEventListener("change", (event) => {
state.showSightline = event.target.checked;
sightline.visible = state.showSightline;
directLabel.dataset.enabled = state.showSightline ? "1" : "0";
});
document
.querySelector("#points-toggle")
.addEventListener("change", (event) => {
state.showPoints = event.target.checked;
updateNodeStyles();
});
// Clickable node markers
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerStart = null;
canvas.addEventListener("pointerdown", (event) => {
pointerStart = {
x: event.clientX,
y: event.clientY
};
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerStart) return;
const movement = Math.hypot(
event.clientX - pointerStart.x,
event.clientY - pointerStart.y
);
pointerStart = null;
if (movement > 5) return;
const rect = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(nodeMeshes, false);
if (hits.length) {
showNodeInfo(hits[0].object.userData.nodeKey);
}
});
// Direct-distance world label
const directLabel = document.createElement("div");
directLabel.className = "world-label direct";
directLabel.textContent = `DIRECT ${directDistance.toFixed(1)} m`;
directLabel.dataset.enabled = "1";
labelsRoot.appendChild(directLabel);
const directLabelPosition = NODES.S.p
.clone()
.lerp(NODES.E.p, 0.5)
.add(new THREE.Vector3(0, 1.8, 0));
// -------------------------------------------------------------------------
// Project HTML labels
// -------------------------------------------------------------------------
function positionLabel(element, worldPosition, enabled) {
if (!enabled) {
element.style.opacity = "0";
element.style.pointerEvents = "none";
return;
}
const projected = worldPosition.clone().project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
projected.x < -1.2 ||
projected.x > 1.2 ||
projected.y < -1.2 ||
projected.y > 1.2
) {
element.style.opacity = "0";
return;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.opacity = "1";
element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -100%)`;
}
function updateLabels() {
for (const [key, label] of Object.entries(nodeLabels)) {
positionLabel(
label.element,
label.position,
label.element.dataset.enabled === "1"
);
}
positionLabel(
directLabel,
directLabelPosition,
state.showSightline
);
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateRouteUI();
function animate(now) {
requestAnimationFrame(animate);
controls.update(now);
updateLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```