Prompt specification
Build or customize the Three.js 3D scene "Compare river-edge and upper-street routes" 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: Compare river-edge and upper-street routes
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-river-edge-and-upper-street-routes-x2856
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows Porto's Ribeira waterfront. Let users trace routes to the same viewpoint from two starting levels. Show stair connections and bridge access as explicit links in the compact Porto network. Voxel style.
Working Three.js Starter (compare-river-edge-and-upper-street-routes-x2856):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Porto Ribeira — Two-Level Route Network</title>
<style>
:root {
--ink: #17313b;
--muted: #64767c;
--paper: rgba(251, 248, 239, 0.94);
--paper-solid: #fbf8ef;
--line: rgba(28, 58, 65, 0.13);
--lower: #ed715f;
--upper: #0c9188;
--stair: #e2a92e;
--bridge: #816bd4;
--water: #79b9c1;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e7;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app,
#scene-wrap,
#labels {
position: absolute;
inset: 0;
}
#scene-wrap {
z-index: 0;
}
#scene-wrap canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#labels {
z-index: 5;
pointer-events: none;
overflow: hidden;
}
.brand {
position: absolute;
z-index: 15;
top: 22px;
left: 24px;
pointer-events: none;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.55);
}
.brand-kicker {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 5px;
color: #31525b;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.brand-kicker::before {
width: 22px;
height: 3px;
background: var(--lower);
content: "";
}
.brand h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.brand p {
max-width: 340px;
margin: 8px 0 0;
color: #47626a;
font-size: 12px;
line-height: 1.45;
}
.panel {
position: absolute;
z-index: 20;
top: 18px;
right: 18px;
bottom: 18px;
width: min(370px, calc(100vw - 36px));
padding: 20px;
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 22px;
background: var(--paper);
box-shadow:
0 18px 50px rgba(39, 62, 66, 0.18),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
backdrop-filter: blur(16px);
scrollbar-width: thin;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.panel h2 {
margin: 0;
font-size: 25px;
line-height: 1.05;
letter-spacing: -0.045em;
}
.panel-intro {
margin: 9px 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.goal-card {
display: grid;
grid-template-columns: 34px 1fr auto;
align-items: center;
gap: 10px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.48);
}
.goal-icon {
display: grid;
width: 34px;
height: 34px;
place-items: center;
border-radius: 10px;
background: #17313b;
color: #fff8e8;
font-size: 17px;
}
.goal-card strong {
display: block;
font-size: 12px;
}
.goal-card small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.goal-card .same {
color: #789096;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-label {
display: flex;
justify-content: space-between;
margin: 17px 0 8px;
color: #6b7d82;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.start-tabs {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.start-tab {
position: relative;
min-height: 68px;
padding: 10px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.42);
color: var(--ink);
cursor: pointer;
text-align: left;
transition: 160ms ease;
}
.start-tab::before {
position: absolute;
top: 10px;
bottom: 10px;
left: 0;
width: 4px;
border-radius: 0 5px 5px 0;
background: var(--tab-color);
content: "";
opacity: 0.32;
}
.start-tab.active {
border-color: var(--tab-color);
background: #fffdf8;
box-shadow: 0 5px 16px rgba(31, 53, 59, 0.08);
transform: translateY(-1px);
}
.start-tab.active::before {
opacity: 1;
}
.start-tab span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.start-tab strong {
display: block;
margin-top: 6px;
font-size: 12px;
line-height: 1.2;
}
.route-card {
margin-top: 12px;
padding: 13px;
border: 1px solid var(--line);
border-radius: 15px;
background: rgba(255, 255, 255, 0.42);
}
.route-card-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.route-card-top strong {
font-size: 12px;
}
.route-kind {
padding: 4px 7px;
border-radius: 999px;
background: rgba(23, 49, 59, 0.08);
color: #536970;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.route-hint {
min-height: 30px;
margin: 8px 0 11px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
}
.metric {
padding: 9px 7px;
border-radius: 10px;
background: #f1ede3;
}
.metric strong {
display: block;
font-size: 15px;
letter-spacing: -0.035em;
}
.metric span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.quick-actions {
display: grid;
gap: 7px;
margin-top: 11px;
}
.quick-button,
.small-button {
border: 1px solid var(--line);
border-radius: 10px;
background: #fffdf8;
color: var(--ink);
cursor: pointer;
transition: 140ms ease;
}
.quick-button {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 11px;
font-size: 10px;
font-weight: 750;
text-align: left;
}
.quick-button:hover,
.small-button:hover {
border-color: rgba(23, 49, 59, 0.32);
transform: translateY(-1px);
}
.quick-button .arrow {
color: #839399;
font-size: 14px;
}
.route-tools {
display: flex;
gap: 7px;
margin-top: 8px;
}
.small-button {
flex: 1;
padding: 8px;
color: #62757b;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.compare-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.compare-card {
min-width: 0;
padding: 11px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.38);
cursor: pointer;
transition: 150ms ease;
}
.compare-card.active {
border-color: var(--card-color);
background: #fffdf8;
}
.compare-card span {
display: block;
color: var(--card-color);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.compare-card strong {
display: block;
margin-top: 6px;
font-size: 18px;
letter-spacing: -0.04em;
}
.compare-card small {
display: block;
min-height: 25px;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.network-key {
display: flex;
flex-wrap: wrap;
gap: 9px 13px;
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
}
.key-item {
display: flex;
align-items: center;
gap: 6px;
}
.key-line {
width: 18px;
height: 4px;
border-radius: 5px;
background: var(--key);
}
.gesture-note {
margin: 12px 0 0;
color: #7b8b8f;
font-size: 9px;
line-height: 1.5;
}
.legend {
position: absolute;
z-index: 12;
bottom: 20px;
left: 22px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100vw - 450px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
padding: 7px 9px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 999px;
background: rgba(251, 248, 239, 0.78);
color: #476169;
box-shadow: 0 5px 16px rgba(40, 67, 70, 0.08);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.legend-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--dot);
}
.view-controls {
position: absolute;
z-index: 12;
right: 406px;
bottom: 20px;
display: flex;
gap: 5px;
}
.view-controls button {
width: 34px;
height: 34px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 10px;
background: rgba(251, 248, 239, 0.87);
color: var(--ink);
cursor: pointer;
box-shadow: 0 5px 16px rgba(40, 67, 70, 0.09);
font-size: 15px;
backdrop-filter: blur(8px);
}
.view-controls button:hover {
background: #fff;
transform: translateY(-1px);
}
.node-label,
.link-label,
.world-label {
position: absolute;
z-index: 8;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.node-label {
display: flex;
align-items: center;
gap: 6px;
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.85);
border-radius: 9px;
background: rgba(251, 248, 239, 0.93);
color: #29434c;
box-shadow: 0 4px 12px rgba(32, 57, 63, 0.12);
pointer-events: auto;
cursor: pointer;
font-size: 9px;
font-weight: 780;
transition: 140ms ease;
}
.node-label:hover {
z-index: 10;
background: #fff;
transform: translate(-50%, -50%) scale(1.04);
}
.node-label .node-dot {
width: 8px;
height: 8px;
border: 2px solid #fff;
border-radius: 3px;
background: #8da0a4;
box-shadow: 0 0 0 1px rgba(23, 49, 59, 0.12);
}
.node-label.start .node-dot {
background: var(--lower);
}
.node-label.start.upper-start .node-dot {
background: var(--upper);
}
.node-label.visited {
border-color: var(--route-color, var(--upper));
}
.node-label.visited .node-dot {
background: var(--route-color, var(--upper));
}
.node-label.current {
color: #fff;
background: #17313b;
border-color: #17313b;
box-shadow: 0 5px 18px rgba(23, 49, 59, 0.25);
}
.node-label.current .node-dot {
background: #fff;
}
.node-label.goal {
border-color: #ef705e;
background: #fff8ed;
}
.node-label.goal .node-dot {
background: #ef705e;
}
.link-label {
padding: 4px 7px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 7px;
box-shadow: 0 3px 10px rgba(32, 57, 63, 0.1);
color: #fff;
pointer-events: auto;
cursor: pointer;
font-size: 7px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.link-label.stairs {
background: var(--stair);
}
.link-label.bridge {
background: var(--bridge);
}
.link-label:hover {
filter: brightness(1.06);
}
.world-label {
color: rgba(28, 65, 76, 0.48);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.22em;
text-transform: uppercase;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}
#toast {
position: absolute;
z-index: 40;
left: 50%;
bottom: 72px;
max-width: min(440px, calc(100vw - 32px));
padding: 10px 14px;
border-radius: 11px;
background: #17313b;
color: #fff9ec;
box-shadow: 0 8px 25px rgba(23, 49, 59, 0.2);
font-size: 11px;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition: 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: absolute;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
background: #dce9e7;
color: #37565e;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 13px;
left: 14px;
}
.brand h1 {
font-size: 27px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 44vh;
padding: 15px;
border-radius: 18px;
}
.panel h2 {
font-size: 21px;
}
.panel-intro {
margin-bottom: 11px;
}
.view-controls {
top: 82px;
right: 12px;
bottom: auto;
}
.legend {
top: 82px;
bottom: auto;
left: 12px;
max-width: calc(100vw - 100px);
}
.legend-item {
padding: 6px 8px;
font-size: 7px;
}
.node-label {
padding: 4px 6px;
font-size: 7px;
}
.link-label {
font-size: 6px;
}
#toast {
bottom: calc(44vh + 20px);
}
}
</style>
</head>
<body>
<div id="app">
<div id="scene-wrap"></div>
<div id="labels"></div>
<header class="brand">
<div class="brand-kicker">Porto / Wayfinding study 01</div>
<h1>Ribeira, in two levels.</h1>
<p>
Trace a waterfront path from the quay or the upper terrace to the
same Douro overlook.
</p>
</header>
<aside class="panel">
<div class="panel-heading">
<div>
<p class="eyebrow">Compact Porto network</p>
<h2>Find your way down.</h2>
</div>
</div>
<p class="panel-intro">
Select a starting level, then click connected map markers. Stairs
and bridge decks are highlighted as explicit links.
</p>
<div class="goal-card">
<div class="goal-icon">◎</div>
<div>
<strong>Ribeira Viewpoint</strong>
<small>South bank · shared destination</small>
</div>
<span class="same">Same goal</span>
</div>
<div class="section-label">
<span>01 / Choose starting level</span>
<span>Two routes</span>
</div>
<div class="start-tabs" role="tablist">
<button
class="start-tab active"
id="lower-tab"
data-level="lower"
role="tab"
aria-selected="true"
style="--tab-color: var(--lower)"
>
<span>Level 0 · Riverside</span>
<strong>Ribeira Quay</strong>
</button>
<button
class="start-tab"
id="upper-tab"
data-level="upper"
role="tab"
aria-selected="false"
style="--tab-color: var(--upper)"
>
<span>Level 1 · Upper town</span>
<strong>Upper Terrace</strong>
</button>
</div>
<div class="section-label">
<span>02 / Trace the network</span>
<span id="trace-progress">Ready</span>
</div>
<div class="route-card">
<div class="route-card-top">
<strong id="route-title">From Ribeira Quay</strong>
<span class="route-kind" id="route-kind">Manual trace</span>
</div>
<p class="route-hint" id="route-hint"></p>
<div class="metrics">
<div class="metric">
<strong id="route-distance">0 m</strong>
<span>Distance</span>
</div>
<div class="metric">
<strong id="route-ascent">0 m</strong>
<span>Ascent</span>
</div>
<div class="metric">
<strong id="route-descent">0 m</strong>
<span>Descent</span>
</div>
</div>
<div class="quick-actions" id="quick-actions"></div>
<div class="route-tools">
<button class="small-button" id="undo-button">← Undo</button>
<button class="small-button" id="clear-button">Clear trace</button>
</div>
</div>
<div class="section-label">
<span>03 / Compare</span>
<span>Tap to switch</span>
</div>
<div class="compare-grid">
<button
class="compare-card active"
id="lower-compare"
style="--card-color: var(--lower)"
>
<span>Lower start</span>
<strong id="lower-summary">Ready</strong>
<small id="lower-detail">Trace from the Ribeira Quay</small>
</button>
<button
class="compare-card"
id="upper-compare"
style="--card-color: var(--upper)"
>
<span>Upper start</span>
<strong id="upper-summary">Ready</strong>
<small id="upper-detail">Trace from the Upper Terrace</small>
</button>
</div>
<div class="network-key" aria-label="Network link legend">
<span class="key-item">
<i class="key-line" style="--key: #d6cbb8"></i>
Walk
</span>
<span class="key-item">
<i class="key-line" style="--key: var(--stair)"></i>
Stairs
</span>
<span class="key-item">
<i class="key-line" style="--key: var(--bridge)"></i>
Bridge
</span>
</div>
<p class="gesture-note">
Drag the model to orbit. Scroll or use + / − to zoom. Clicking an
invalid marker will explain the next valid connection.
</p>
</aside>
<div class="legend">
<div class="legend-item">
<i class="legend-dot" style="--dot: var(--lower)"></i>
Lower start
</div>
<div class="legend-item">
<i class="legend-dot" style="--dot: var(--upper)"></i>
Upper start
</div>
<div class="legend-item">
<i class="legend-dot" style="--dot: var(--stair)"></i>
Stair connection
</div>
<div class="legend-item">
<i class="legend-dot" style="--dot: var(--bridge)"></i>
Bridge access
</div>
</div>
<div class="view-controls">
<button id="zoom-in" aria-label="Zoom in">+</button>
<button id="zoom-out" aria-label="Zoom out">−</button>
<button id="reset-view" aria-label="Reset camera" title="Reset view">⌂</button>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="loading">Building the Ribeira…</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const sceneWrap = $("#scene-wrap");
const labelsRoot = $("#labels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e7);
scene.fog = new THREE.Fog(0xdce9e7, 42, 82);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
sceneWrap.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-20, 20, 14, -14, 0.1, 120);
const orbit = {
theta: 0.69,
phi: 0.83,
zoom: 1,
target: new THREE.Vector3(2.0, 2.1, 2.4)
};
const defaultOrbit = {
theta: 0.69,
phi: 0.83,
zoom: 1
};
let viewHeight = 28;
function updateCamera() {
const radius = 38;
const { theta, phi, target } = orbit;
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);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
viewHeight = aspect < 1 ? 38 / aspect : 28;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
const hemisphere = new THREE.HemisphereLight(0xeaf6f4, 0x9b8873, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.1);
sun.position.set(-12, 24, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -26;
sun.shadow.camera.right = 26;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xcde5ee, 0.75);
fill.position.set(14, 10, -16);
scene.add(fill);
const nodeDefs = {
A: {
name: "Ribeira Quay",
short: "Ribeira Quay",
pos: [-10, 1.48, 0.8],
elev: 2,
type: "start"
},
B: {
name: "Stair foot",
short: "Stair foot",
pos: [-6, 1.48, 1.15],
elev: 2,
type: "node"
},
C: {
name: "Terrace steps",
short: "Stair head",
pos: [-6, 5.08, -1.1],
elev: 46,
type: "node"
},
D: {
name: "Upper terrace",
short: "Upper Terrace",
pos: [-1, 5.08, -1.55],
elev: 46,
type: "start"
},
E: {
name: "Upper bridge landing",
short: "Upper landing",
pos: [5.5, 6.65, 1.3],
elev: 46,
type: "node"
},
F: {
name: "Upper bridge",
short: "Upper bridge",
pos: [5.5, 6.65, 8.4],
elev: 46,
type: "node"
},
G: {
name: "Ribeira Viewpoint",
short: "Viewpoint",
pos: [5.5, 2.38, 13.25],
elev: 12,
type: "goal"
},
H: {
name: "Lower bridge landing",
short: "Lower landing",
pos: [5.5, 2.64, 1.3],
elev: 2,
type: "node"
},
I: {
name: "Lower bridge",
short: "Lower bridge",
pos: [5.5, 2.64, 8.4],
elev: 2,
type: "node"
}
};
const edgeDefs = [
{ a: "A", b: "B", type: "walk", distance: 70 },
{ a: "B", b: "C", type: "stairs", distance: 82 },
{ a: "C", b: "D", type: "walk", distance: 68 },
{ a: "D", b: "E", type: "walk", distance: 92 },
{ a: "E", b: "F", type: "bridge", distance: 180 },
{ a: "F", b: "G", type: "walk", distance: 112 },
{ a: "A", b: "H", type: "walk", distance: 210 },
{ a: "H", b: "I", type: "bridge", distance: 180 },
{ a: "I", b: "G", type: "walk", distance: 112 }
];
const adjacency = new Map(Object.keys(nodeDefs).map((id) => [id, []]));
for (const edge of edgeDefs) {
adjacency.get(edge.a).push(edge.b);
adjacency.get(edge.b).push(edge.a);
}
function getEdge(a, b) {
return edgeDefs.find(
(edge) =>
(edge.a === a && edge.b === b) ||
(edge.a === b && edge.b === a)
);
}
const palette = {
water: 0x78b8c0,
waterLight: 0xb8dadd,
stone: 0xd7c9ac,
stoneDark: 0xb8aa8e,
stoneLight: 0xe9dec3,
quay: 0xc7b795,
roof: 0xb85f4b,
roofDark: 0x934b3f,
bridge: 0xb7a98b,
dark: 0x244651,
green: 0x648d67,
greenLight: 0x80a873,
cream: 0xf3dfb7
};
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.84,
metalness: options.metalness ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
}
function addBox(
width,
height,
depth,
color,
x,
y,
z,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
options.material || material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
scene.add(mesh);
return mesh;
}
function addCylinder(
radiusTop,
radiusBottom,
height,
color,
x,
y,
z,
segments = 10,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
options.material || material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
scene.add(mesh);
return mesh;
}
// Deterministic pseudo-random values keep the voxel city stable.
let randomSeed = 8127;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
// -------------------------------------------------------------------------
// Water, banks and quay
// -------------------------------------------------------------------------
addBox(38, 0.34, 20, palette.water, 0, -0.19, 7.5, {
roughness: 0.34,
metalness: 0.04,
castShadow: false
});
const waterRipples = [];
for (let i = 0; i < 42; i++) {
const ripple = addBox(
0.7 + random() * 2.2,
0.025,
0.08 + random() * 0.05,
palette.waterLight,
-17 + random() * 34,
0.015,
2 + random() * 12,
{
transparent: true,
opacity: 0.32,
castShadow: false,
receiveShadow: false
}
);
ripple.userData.baseX = ripple.position.x;
ripple.userData.phase = random() * Math.PI * 2;
waterRipples.push(ripple);
}
// Porto bank rises behind the lower quay.
addBox(34, 4.35, 9.2, palette.stone, 0, 2.175, -5.45);
addBox(34, 0.18, 9.4, palette.stoneLight, 0, 4.38, -5.45);
// Lower riverside promenade.
addBox(34, 1.12, 2.6, palette.quay, 0, 0.56, 0.35);
addBox(34, 0.13, 2.7, palette.stoneLight, 0, 1.18, 0.35);
// River wall, with alternating stone blocks.
for (let x = -16.5; x <= 16.5; x += 1.5) {
addBox(
1.44,
0.75,
0.32,
random() > 0.5 ? palette.stone : palette.stoneDark,
x,
0.48,
1.69
);
}
// South/Gaia bank and viewpoint platform.
addBox(38, 1.55, 4.6, palette.stone, 0, 0.775, 15.1);
addBox(38, 0.16, 4.7, palette.stoneLight, 0, 1.61, 15.1);
addBox(3.6, 0.22, 2.2, palette.cream, 5.5, 1.76, 13.35);
addBox(3.9, 0.12, 0.22, palette.stoneLight, 5.5, 1.93, 12.35);
// -------------------------------------------------------------------------
// Voxel Ribeira buildings
// -------------------------------------------------------------------------
const facadeColors = [
0xe7b49e,
0xf0cf91,
0xd9d2b2,
0xdca3a0,
0xe8b56f,
0xabc1b1,
0xc3a8ba,
0xf0ded0
];
const windowColor = 0x294c58;
const balconyColor = 0xeee0c4;
function addBuilding(x, z, width, depth, height, color) {
const baseY = 4.48;
addBox(width, height, depth, color, x, baseY + height / 2, z);
const roofColor = random() > 0.55 ? palette.roof : palette.roofDark;
addBox(
width + 0.28,
0.28,
depth + 0.3,
roofColor,
x,
baseY + height + 0.14,
z
);
addBox(
width * 0.64,
0.38,
depth * 0.58,
roofColor,
x,
baseY + height + 0.43,
z
);
const floors = Math.max(2, Math.floor(height / 1.25));
const columns = Math.max(2, Math.floor(width / 0.78));
for (let floor = 0; floor < floors; floor++) {
const windowY = baseY + 0.75 + floor * 1.13;
for (let column = 0; column < columns; column++) {
if (random() < 0.12) continue;
const windowX =
x - width / 2 + 0.45 + column * ((width - 0.8) / Math.max(1, columns - 1));
addBox(
0.3,
0.5,
0.06,
windowColor,
windowX,
windowY,
z + depth / 2 + 0.035,
{ castShadow: false }
);
if (random() > 0.82) {
addBox(
0.42,
0.12,
0.18,
balconyColor,
windowX,
windowY - 0.32,
z + depth / 2 + 0.11
);
}
}
}
addBox(
0.46,
0.9,
0.07,
0x6c4d43,
x,
baseY + 0.45,
z + depth / 2 + 0.04
);
if (random() > 0.55) {
addBox(
0.42,
0.85,
0.5,
roofColor,
x + width * 0.25,
baseY + height + 0.78,
z - depth * 0.15
);
}
}
const frontXs = [-14, -10, -2, 2.7, 7.2, 11.8, 15.2];
for (const x of frontXs) {
addBuilding(
x,
-4.0,
2.35 + random() * 0.75,
2.55 + random() * 0.6,
3.2 + random() * 3.0,
facadeColors[Math.floor(random() * facadeColors.length)]
);
}
const backXs = [-15, -10.3, -5.2, 0.1, 5.1, 10.1, 15];
for (const x of backXs) {
addBuilding(
x,
-7.65,
2.5 + random() * 1.25,
2.5 + random() * 0.8,
3.7 + random() * 3.5,
facadeColors[Math.floor(random() * facadeColors.length)]
);
}
// Small landmark tower.
addBox(1.45, 6.5, 1.45, 0xe5c57c, -13.1, 7.72, -7.3);
addBox(1.7, 0.32, 1.7, palette.roofDark, -13.1, 11.1, -7.3);
addBox(0.7, 0.9, 0.7, 0xe8d8ae, -13.1, 11.72, -7.3);
addBox(0.58, 0.58, 0.08, 0xf7edcf, -13.1, 8.65, -6.54, {
castShadow: false
});
addBox(0.06, 0.4, 0.06, 0x294c58, -13.1, 8.68, -6.48, {
castShadow: false
});
addBox(0.28, 0.06, 0.06, 0x294c58, -13.1, 8.68, -6.48, {
castShadow: false
});
// Market awnings and quay stalls.
const awningColors = [0xd87b5f, 0xe8c464, 0x5e958b, 0xd2a3ad];
for (let i = 0; i < 7; i++) {
const x = -12.5 + i * 3.2;
const color = awningColors[i % awningColors.length];
addBox(1.45, 0.18, 0.95, color, x, 2.1, 0.68);
addBox(0.12, 1.65, 0.12, 0x765f4d, x - 0.62, 1.42, 0.86);
addBox(0.12, 1.65, 0.12, 0x765f4d, x + 0.62, 1.42, 0.86);
addBox(1.25, 0.7, 0.72, 0xb79461, x, 1.5, 0.68);
}
// Bollards and lamps.
for (let x = -15; x <= 15; x += 3) {
addCylinder(0.13, 0.18, 0.55, 0x38505a, x, 1.48, 1.32, 8);
addBox(0.26, 0.16, 0.26, 0x38505a, x, 1.82, 1.32);
if (Math.round((x + 15) / 3) % 2 === 0) {
addBox(0.11, 2.7, 0.11, 0x405962, x + 1.2, 2.45, 0.9);
addBox(0.46, 0.4, 0.46, 0xf1d697, x + 1.2, 3.95, 0.9);
addBox(0.56, 0.12, 0.56, 0x405962, x + 1.2, 4.2, 0.9);
}
}
// -------------------------------------------------------------------------
// Explicit stair connection
// -------------------------------------------------------------------------
const stairStartZ = 1.15;
const stairEndZ = -1.1;
const stairStartY = 1.25;
const stairEndY = 4.62;
const stairCount = 17;
for (let i = 0; i < stairCount; i++) {
const t = i / (stairCount - 1);
const z = THREE.MathUtils.lerp(stairStartZ, stairEndZ, t);
const topY = THREE.MathUtils.lerp(stairStartY, stairEndY, t);
addBox(
1.65,
0.3,
0.34,
i % 3 === 0 ? palette.stoneLight : palette.stone,
-6,
topY - 0.15,
z
);
if (i > 5) {
addBox(
1.72,
topY - 1.0,
0.34,
palette.stoneDark,
-6,
(topY - 1.0) / 2,
z
);
}
}
addBox(0.12, 0.9, 0.12, 0x455f66, -6.86, 3.1, 0.07);
addBox(0.12, 0.9, 0.12, 0x455f66, -5.14, 3.1, 0.07);
// Upper-terrace approach to the upper bridge landing.
for (let i = 0; i < 12; i++) {
const t = i / 11;
addBox(
0.75,
0.24,
0.95,
palette.stoneLight,
THREE.MathUtils.lerp(-1, 5.5, t),
THREE.MathUtils.lerp(4.95, 6.43, t),
THREE.MathUtils.lerp(-1.55, 1.3, t)
);
}
// -------------------------------------------------------------------------
// Iconic two-level bridge
// -------------------------------------------------------------------------
addBox(3.15, 0.32, 12.1, 0x48636b, 5.5, 2.15, 7.25);
addBox(3.35, 0.28, 12.25, 0xb29a77, 5.5, 2.38, 7.25);
addBox(2.85, 0.38, 12.1, 0x596e72, 5.5, 6.22, 7.25);
addBox(3.05, 0.18, 12.25, 0xc0ae8c, 5.5, 6.48, 7.25);
// Stone pylons and stepped voxel arches.
for (const z of [1.65, 12.75]) {
for (const x of [3.92, 7.08]) {
addBox(0.75, 6.65, 1.0, palette.bridge, x, 3.32, z);
addBox(0.92, 0.3, 1.18, palette.stoneLight, x, 6.76, z);
}
addBox(3.95, 0.55, 0.9, palette.bridge, 5.5, 6.05, z);
addBox(4.1, 0.22, 1.05, palette.stoneLight, 5.5, 6.4, z);
}
for (const x of [3.92, 7.08]) {
for (let i = 0; i < 17; i++) {
const t = i / 16;
const z = 2.7 + t * 9.1;
const y = 0.65 + Math.sin(Math.PI * t) * 4.7;
addBox(
0.52,
0.68,
0.76,
i % 2 ? palette.bridge : palette.stoneDark,
x,
y,
z
);
}
}
// Upper-deck railings.
for (const x of [3.91, 7.09]) {
for (let z = 1.4; z <= 13; z += 0.85) {
addBox(0.08, 0.7, 0.08, 0x53696d, x, 6.84, z);
}
addBox(0.09, 0.09, 11.8, 0x53696d, x, 7.16, 7.2);
}
// -------------------------------------------------------------------------
// Trees, boat and small details
// -------------------------------------------------------------------------
function addTree(x, y, z, scale = 1) {
addBox(
0.25 * scale,
1.45 * scale,
0.25 * scale,
0x765946,
x,
y + 0.72 * scale,
z
);
addBox(
1.25 * scale,
1.1 * scale,
1.15 * scale,
palette.green,
x,
y + 1.75 * scale,
z
);
addBox(
0.88 * scale,
0.88 * scale,
1.38 * scale,
palette.greenLight,
x + 0.08 * scale,
y + 2.15 * scale,
z
);
}
addTree(2.8, 1.72, 13.7, 1.1);
addTree(8.15, 1.72, 14.1, 0.95);
addTree(-15.1, 1.72, 14.6, 0.85);
addTree(13.8, 1.72, 13.2, 1.1);
addTree(-15.2, 4.47, -8.8, 0.85);
addTree(13.8, 4.47, -8.9, 0.9);
// Small wooden cargo boat.
addBox(3.5, 0.48, 1.3, 0xb85f45, -10.6, 0.4, 8.2);
addBox(2.8, 0.2, 1.0, 0xe2bf82, -10.7, 0.69, 8.2);
addBox(1.15, 0.65, 0.88, 0x5f8d85, -11.25, 1.03, 8.2);
addBox(0.9, 0.18, 0.95, 0xf0d7a7, -11.25, 1.43, 8.2);
addBox(0.09, 2.2, 0.09, 0x5f5146, -9.75, 1.45, 8.2);
// Crates and barrels on the quay.
for (let i = 0; i < 8; i++) {
const x = -15 + random() * 9;
const z = 0.65 + random() * 0.55;
addBox(
0.55,
0.55,
0.55,
random() > 0.5 ? 0xb88d57 : 0x96704b,
x,
1.54,
z
);
}
// -------------------------------------------------------------------------
// Network base links
// -------------------------------------------------------------------------
const up = new THREE.Vector3(0, 1, 0);
function createSegment(start, end, radius, mat, parent = scene) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(radius, radius, 1, 8);
geometry.translate(0, 0.5, 0);
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.copy(start);
mesh.quaternion.setFromUnitVectors(up, direction.clone().normalize());
mesh.scale.y = length;
mesh.castShadow = false;
mesh.receiveShadow = false;
parent.add(mesh);
return mesh;
}
const walkMaterial = material(0xd7cdb9, {
transparent: true,
opacity: 0.78,
roughness: 1
});
const stairMaterial = material(palette.roofDark, {
roughness: 0.85,
emissive: 0x5b3300,
emissiveIntensity: 0.15
});
const bridgeMaterial = material(palette.bridge, {
roughness: 0.8,
emissive: 0x25175e,
emissiveIntensity: 0.18
});
for (const edge of edgeDefs) {
const a = new THREE.Vector3(...nodeDefs[edge.a].pos);
const b = new THREE.Vector3(...nodeDefs[edge.b].pos);
const mat =
edge.type === "stairs"
? stairMaterial
: edge.type === "bridge"
? bridgeMaterial
: walkMaterial;
createSegment(a, b, edge.type === "walk" ? 0.032 : 0.052, mat);
}
// -------------------------------------------------------------------------
// Network node markers and HTML labels
// -------------------------------------------------------------------------
const nodeGroups = new Map();
const nodeLabels = new Map();
const hitMeshes = [];
const labelEntries = [];
function makeNodeMarker(id) {
const definition = nodeDefs[id];
const group = new THREE.Group();
group.position.set(...definition.pos);
group.userData.nodeId = id;
group.userData.phase = random() * Math.PI * 2;
const isGoal = definition.type === "goal";
const isStart = definition.type === "start";
const baseColor =
id === "A"
? 0xed715f
: id === "D"
? 0x0c9188
: isGoal
? 0xef705e
: 0x38545d;
const base = new THREE.Mesh(
new THREE.BoxGeometry(isGoal ? 0.82 : 0.68, 0.15, isGoal ? 0.82 : 0.68),
material(baseColor)
);
base.position.y = -0.29;
base.castShadow = true;
group.add(base);
const accent = new THREE.Mesh(
new THREE.BoxGeometry(0.29, 0.43, 0.29),
material(isGoal ? 0xef705e : 0xf7eed9)
);
accent.position.y = 0.02;
accent.castShadow = true;
group.add(accent);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.15, 0.17),
material(isGoal ? 0xffd3c5 : baseColor)
);
cap.position.y = 0.3;
cap.castShadow = true;
group.add(cap);
// Square voxel halo.
for (let i = 0; i < 4; i++) {
const angle = (i * Math.PI) / 2;
const piece = new THREE.Mesh(
new THREE.BoxGeometry(isGoal ? 0.43 : 0.34, 0.07, 0.12),
material(baseColor, {
transparent: true,
opacity: 0.62,
emissive: baseColor,
emissiveIntensity: 0.08
})
);
piece.position.set(
Math.sin(angle) * 0.31,
-0.22,
Math.cos(angle) * 0.31
);
piece.rotation.y = angle;
group.add(piece);
}
const hit = new THREE.Mesh(
new THREE.SphereGeometry(0.82, 10, 8),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
hit.userData.nodeId = id;
group.add(hit);
hitMeshes.push(hit);
scene.add(group);
nodeGroups.set(id, group);
const label = document.createElement("button");
label.className = "node-label";
label.type = "button";
label.setAttribute("aria-label", `Trace to ${definition.name}`);
label.innerHTML = `<span class="node-dot"></span><span>${definition.short}</span>`;
label.addEventListener("click", (event) => {
event.stopPropagation();
traceToNode(id);
});
labelsRoot.appendChild(label);
nodeLabels.set(id, label);
const offsets = {
A: [-25, 17],
B: [-12, 20],
C: [-70, -4],
D: [-42, -23],
E: [74, -16],
F: [77, -9],
G: [0, 28],
H: [81, 13],
I: [77, 15]
};
labelEntries.push({
element: label,
position: new THREE.Vector3(...definition.pos),
offset: offsets[id] || [0, -22],
type: "node"
});
return group;
}
for (const id of Object.keys(nodeDefs)) {
makeNodeMarker(id);
}
function addLinkLabel(text, position, offset, edge) {
const label = document.createElement("button");
label.className = `link-label ${edge.type}`;
label.type = "button";
label.textContent = text;
label.title = `Follow ${edge.type} connection`;
label.addEventListener("click", (event) => {
event.stopPropagation();
followLink(edge);
});
labelsRoot.appendChild(label);
labelEntries.push({
element: label,
position: new THREE.Vector3(...position),
offset,
type: "link"
});
}
addLinkLabel(
"Stair connection · 72 steps",
[-6, 3.1, 0.05],
[-72, -7],
getEdge("B", "C")
);
addLinkLabel(
"Bridge access · upper deck",
[5.5, 6.8, 4.85],
[88, -14],
getEdge("E", "F")
);
addLinkLabel(
"Bridge access · lower deck",
[5.5, 2.8, 4.85],
[88, 10],
getEdge("H", "I")
);
function addWorldLabel(text, position, offset = [0, 0]) {
const element = document.createElement("div");
element.className = "world-label";
element.textContent = text;
labelsRoot.appendChild(element);
labelEntries.push({
element,
position: new THREE.Vector3(...position),
offset,
type: "world"
});
}
addWorldLabel("Douro river", [-10.2, 0.1, 7.5], [0, 0]);
addWorldLabel("Porto bank", [-8.8, 4.8, -8.8], [0, 0]);
addWorldLabel("Gaia bank", [11.4, 2.1, 15], [0, 0]);
// -------------------------------------------------------------------------
// Route state and drawing
// -------------------------------------------------------------------------
const routes = {
lower: ["A"],
upper: ["D"]
};
let activeLevel = "lower";
let activeRouteGroup = null;
let routeSegments = [];
let routeStartedAt = performance.now();
const lowerColor = new THREE.Color(0xed715f);
const upperColor = new THREE.Color(0x0c9188);
function disposeGroup(group) {
if (!group) return;
group.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
if (Array.isArray(child.material)) {
child.material.forEach((mat) => mat.dispose());
} else {
child.material.dispose();
}
}
});
scene.remove(group);
}
function routeStats(route) {
let distance = 0;
let ascent = 0;
let descent = 0;
const types = new Set();
for (let i = 0; i < route.length - 1; i++) {
const edge = getEdge(route[i], route[i + 1]);
if (!edge) continue;
distance += edge.distance;
types.add(edge.type);
const delta =
nodeDefs[route[i + 1]].elev - nodeDefs[route[i]].elev;
if (delta > 0) ascent += delta;
if (delta < 0) descent += Math.abs(delta);
}
return { distance, ascent, descent, types };
}
function routeKind(types) {
const parts = [];
if (types.has("stairs")) parts.push("stairs");
if (types.has("bridge")) parts.push("bridge");
return parts.length ? parts.join(" + ") : "manual trace";
}
function renderActiveRoute(announce = false) {
disposeGroup(activeRouteGroup);
activeRouteGroup = new THREE.Group();
scene.add(activeRouteGroup);
routeSegments = [];
routeStartedAt = performance.now();
const route = routes[activeLevel];
const routeColor = activeLevel === "lower" ? lowerColor : upperColor;
for (let i = 0; i < route.length - 1; i++) {
const edge = getEdge(route[i], route[i + 1]);
if (!edge) continue;
const start = new THREE.Vector3(...nodeDefs[route[i]].pos);
const end = new THREE.Vector3(...nodeDefs[route[i + 1]].pos);
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const color =
edge.type === "stairs"
? new THREE.Color(0xe1a52c)
: edge.type === "bridge"
? new THREE.Color(0x765ed0)
: routeColor;
const mat = material(color, {
roughness: 0.5,
emissive: color,
emissiveIntensity: 0.38
});
const geometry = new THREE.CylinderGeometry(0.085, 0.085, 1, 10);
geometry.translate(0, 0.5, 0);
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.copy(start);
mesh.quaternion.setFromUnitVectors(up, direction.clone().normalize());
mesh.scale.y = 0.001;
mesh.castShadow = false;
mesh.receiveShadow = false;
activeRouteGroup.add(mesh);
routeSegments.push({
mesh,
length,
delay: i * 0.11,
duration: 0.42
});
}
renderUI();
if (announce) {
const complete = route[route.length - 1] === "G";
showToast(
complete
? "Destination reached. Compare the route from the other level."
: `${routeKind(routeStats(route).types)} route selected.`
);
}
}
function setRoute(level, route, announce = true) {
routes[level] = [...route];
if (activeLevel === level) {
renderActiveRoute(announce);
} else {
renderUI();
}
}
function traceToNode(id) {
const route = routes[activeLevel];
const current = route[route.length - 1];
if (current === id) return;
if (current === "G") {
showToast("This route already reaches the shared viewpoint.");
return;
}
const edge = getEdge(current, id);
if (!edge) {
showToast(
`${nodeDefs[id].name} is not directly connected to ${nodeDefs[current].name}. Follow a solid link.`
);
return;
}
setRoute(activeLevel, [...route, id], false);
if (id === "G") {
showToast("Shared destination reached — now try the other starting level.");
} else {
showToast(`Added ${nodeDefs[id].name}.`);
}
}
function followLink(edge) {
const route = routes[activeLevel];
const current = route[route.length - 1];
if (current === edge.a) {
traceToNode(edge.b);
} else if (current === edge.b) {
traceToNode(edge.a);
} else {
showToast(
`Reach ${nodeDefs[edge.a].name} or ${nodeDefs[edge.b].name} before following this ${edge.type} link.`
);
}
}
// -------------------------------------------------------------------------
// Interface rendering
// -------------------------------------------------------------------------
function validNextNames(route) {
const current = route[route.length - 1];
if (current === "G") return [];
return adjacency
.get(current)
.filter((id) => !route.includes(id) || id === current)
.map((id) => nodeDefs[id].short);
}
function renderQuickActions() {
const container = $("#quick-actions");
container.innerHTML = "";
const options =
activeLevel === "lower"
? [
{
name: "Via stair connection",
sub: "Upper town → upper bridge deck",
route: ["A", "B", "C", "D", "E", "F", "G"]
},
{
name: "Via lower bridge access",
sub: "Waterfront → lower bridge deck",
route: ["A", "H", "I", "G"]
}
]
: [
{
name: "Across the upper bridge",
sub: "Shortest upper-level route",
route: ["D", "E", "F", "G"]
},
{
name: "Down through Ribeira",
sub: "Terrace → quay → lower bridge",
route: ["D", "C", "B", "A", "H", "I", "G"]
}
];
for (const option of options) {
const button = document.createElement("button");
button.className = "quick-button";
button.type = "button";
button.innerHTML = `
<span>${option.name}<small style="display:block;margin-top:3px;color:#7a8b90;font-size:8px;font-weight:600">${option.sub}</small></span>
<span class="arrow">→</span>
`;
button.addEventListener("click", () => {
setRoute(activeLevel, option.route, true);
});
container.appendChild(button);
}
}
function updateComparison(level) {
const route = routes[level];
const stats = routeStats(route);
const complete = route[route.length - 1] === "G";
const hasTrace = route.length > 1;
$(`#${level}-summary`).textContent = hasTrace
? `${stats.distance} m`
: "Ready";
$(`#${level}-detail`).textContent = complete
? `${routeKind(stats.types)} · ${stats.ascent} m ascent`
: hasTrace
? `Partial trace · ${route.length - 1} link${route.length - 1 === 1 ? "" : "s"}`
: level === "lower"
? "Trace from the Ribeira Quay"
: "Trace from the Upper Terrace";
}
function renderUI() {
const route = routes[activeLevel];
const stats = routeStats(route);
const complete = route[route.length - 1] === "G";
const startName =
activeLevel === "lower" ? "Ribeira Quay" : "Upper Terrace";
$("#lower-tab").classList.toggle("active", activeLevel === "lower");
$("#upper-tab").classList.toggle("active", activeLevel === "upper");
$("#lower-tab").setAttribute(
"aria-selected",
String(activeLevel === "lower")
);
$("#upper-tab").setAttribute(
"aria-selected",
String(activeLevel === "upper")
);
$("#lower-compare").classList.toggle("active", activeLevel === "lower");
$("#upper-compare").classList.toggle("active", activeLevel === "upper");
$("#route-title").textContent = `From ${startName}`;
$("#route-kind").textContent = routeKind(stats.types);
$("#route-distance").textContent = `${stats.distance} m`;
$("#route-ascent").textContent = `${stats.ascent} m`;
$("#route-descent").textContent = `${stats.descent} m`;
$("#trace-progress").textContent = complete
? "Complete"
: `${route.length - 1} link${route.length - 1 === 1 ? "" : "s"}`;
if (complete) {
$("#route-hint").textContent =
"You have reached the shared viewpoint. Switch levels to compare another way down.";
} else {
const next = validNextNames(route);
$("#route-hint").textContent = next.length
? `Next connected marker${next.length > 1 ? "s" : ""}: ${next.join(" or ")}.`
: "Choose a connected marker on the model.";
}
$("#undo-button").disabled = route.length <= 1;
$("#clear-button").disabled = route.length <= 1;
renderQuickActions();
updateComparison("lower");
updateComparison("upper");
updateMarkerClasses();
}
function updateMarkerClasses() {
const route = routes[activeLevel];
const routeColor = activeLevel === "lower" ? "#ed715f" : "#0c9188";
for (const [id, label] of nodeLabels) {
const isVisited = route.includes(id);
const isCurrent = route[route.length - 1] === id;
label.classList.toggle("start", id === "A" || id === "D");
label.classList.toggle("upper-start", id === "D");
label.classList.toggle("visited", isVisited);
label.classList.toggle("current", isCurrent);
label.classList.toggle("goal", id === "G");
if (isVisited) {
label.style.setProperty("--route-color", routeColor);
} else {
label.style.removeProperty("--route-color");
}
}
}
function switchLevel(level) {
if (activeLevel === level) return;
activeLevel = level;
renderActiveRoute(false);
}
$("#lower-tab").addEventListener("click", () => switchLevel("lower"));
$("#upper-tab").addEventListener("click", () => switchLevel("upper"));
$("#lower-compare").addEventListener("click", () => switchLevel("lower"));
$("#upper-compare").addEventListener("click", () => switchLevel("upper"));
$("#undo-button").addEventListener("click", () => {
const route = routes[activeLevel];
if (route.length > 1) {
setRoute(activeLevel, route.slice(0, -1), false);
showToast("Last marker removed.");
}
});
$("#clear-button").addEventListener("click", () => {
const start = activeLevel === "lower" ? "A" : "D";
setRoute(activeLevel, [start], false);
showToast("Trace cleared. Choose a connected marker.");
});
let toastTimer;
function showToast(message) {
const toast = $("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
// -------------------------------------------------------------------------
// Direct camera controls — no Three.js addons
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.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;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.42,
1.25
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
} else {
const rect = renderer.domElement.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 hit = raycaster.intersectObjects(hitMeshes, false)[0];
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointerDown = false;
renderer.domElement.style.cursor = "grab";
if (!pointerMoved) {
const rect = renderer.domElement.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 hit = raycaster.intersectObjects(hitMeshes, false)[0];
if (hit) {
traceToNode(hit.object.userData.nodeId);
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.8
);
updateCamera();
},
{ passive: false }
);
$("#zoom-in").addEventListener("click", () => {
orbit.zoom = THREE.MathUtils.clamp(orbit.zoom * 1.12, 0.65, 1.8);
updateCamera();
});
$("#zoom-out").addEventListener("click", () => {
orbit.zoom = THREE.MathUtils.clamp(orbit.zoom / 1.12, 0.65, 1.8);
updateCamera();
});
$("#reset-view").addEventListener("click", () => {
orbit.theta = defaultOrbit.theta;
orbit.phi = defaultOrbit.phi;
orbit.zoom = defaultOrbit.zoom;
updateCamera();
});
// -------------------------------------------------------------------------
// Animation and screen-space labels
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const entry of labelEntries) {
projected.copy(entry.position).project(camera);
const x = (projected.x * 0.5 + 0.5) * width + entry.offset[0];
const y = (-projected.y * 0.5 + 0.5) * height + entry.offset[1];
const visible =
projected.z > -1 &&
projected.z < 1 &&
x > -160 &&
x < width + 160 &&
y > -80 &&
y < height + 80;
entry.element.style.display = visible ? "block" : "none";
entry.element.style.left = `${x}px`;
entry.element.style.top = `${y}px`;
}
}
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const now = performance.now();
for (const ripple of waterRipples) {
ripple.position.x =
ripple.userData.baseX + Math.sin(elapsed * 0.45 + ripple.userData.phase) * 0.32;
}
const route = routes[activeLevel];
const current = route[route.length - 1];
for (const [id, group] of nodeGroups) {
const isCurrent = id === current;
const isGoal = id === "G";
const pulse = 1 + Math.sin(elapsed * 2.5 + group.userData.phase) * 0.045;
const scale = isCurrent ? pulse * 1.16 : isGoal ? pulse * 1.08 : 1;
group.scale.setScalar(scale);
if (isGoal) {
group.rotation.y = elapsed * 0.35;
}
}
for (const segment of routeSegments) {
const progress = THREE.MathUtils.clamp(
(now - routeStartedAt - segment.delay * 1000) /
(segment.duration * 1000),
0,
1
);
const eased = 1 - Math.pow(1 - progress, 3);
segment.mesh.scale.y = Math.max(0.001, segment.length * eased);
}
updateLabels();
renderer.render(scene, camera);
}
renderActiveRoute(false);
updateCamera();
animate();
requestAnimationFrame(() => {
$("#loading").classList.add("hidden");
setTimeout(() => $("#loading").remove(), 400);
});
</script>
</body>
</html>
```