Prompt specification
Build or customize the Three.js 3D scene "Find accessible routes through a sloped historic block" 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: Find accessible routes through a sloped historic block
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/find-accessible-routes-through-a-sloped-historic-block-x2704
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts Pelourinho's sloping streets in Salvador. Let users compare stair, steep-lane, and gentler routes in a schematic district slice. Show distance and slope separately without claiming compliance with accessibility regulations. Voxel style.
Working Three.js Starter (find-accessible-routes-through-a-sloped-historic-block-x2704):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pelourinho — Schematic Route Comparison</title>
<style>
:root {
--ink: #183238;
--muted: #617276;
--paper: rgba(250, 247, 238, 0.94);
--line: rgba(24, 50, 56, 0.14);
--stair: #f2a93b;
--steep: #e75b73;
--gentle: #168e83;
--shadow: 0 18px 50px rgba(29, 51, 51, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bdd3d1;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.masthead {
position: fixed;
top: 22px;
left: 24px;
z-index: 4;
width: min(430px, calc(100vw - 48px));
pointer-events: none;
text-shadow: 0 1px 12px rgba(255, 255, 255, 0.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: #345b5e;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 26px;
height: 3px;
content: "";
border-radius: 4px;
background: var(--stair);
}
h1 {
margin: 0;
max-width: 420px;
font-size: clamp(32px, 4.6vw, 62px);
font-weight: 850;
letter-spacing: -0.055em;
line-height: 0.96;
}
.subtitle {
max-width: 370px;
margin: 12px 0 0;
color: #466266;
font-size: 13px;
font-weight: 560;
line-height: 1.45;
}
.model-badge {
display: inline-flex;
align-items: center;
margin-top: 13px;
padding: 7px 10px;
border: 1px solid rgba(24, 50, 56, 0.15);
border-radius: 999px;
background: rgba(250, 247, 238, 0.74);
color: #35565a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.panel {
position: fixed;
top: 18px;
right: 18px;
z-index: 5;
width: 372px;
max-height: calc(100vh - 36px);
padding: 17px;
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.66);
border-radius: 20px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(16px) saturate(1.12);
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.panel h2 {
margin: 0 0 3px;
font-size: 18px;
letter-spacing: -0.025em;
}
.panel-intro {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.compare-button {
flex: 0 0 auto;
padding: 7px 10px;
border: 1px solid rgba(24, 50, 56, 0.15);
border-radius: 999px;
background: transparent;
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
transition: 160ms ease;
}
.compare-button:hover,
.compare-button.active {
border-color: var(--ink);
background: var(--ink);
color: #fffaf0;
}
.route-list {
display: grid;
gap: 9px;
}
.route-card {
width: 100%;
padding: 12px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.57);
color: var(--ink);
text-align: left;
cursor: pointer;
transition:
transform 160ms ease,
border-color 160ms ease,
background 160ms ease,
box-shadow 160ms ease;
}
.route-card:hover {
transform: translateY(-1px);
border-color: rgba(24, 50, 56, 0.28);
background: rgba(255, 255, 255, 0.9);
}
.route-card.active {
border-color: var(--route-color);
background: #fffdf7;
box-shadow: 0 7px 18px color-mix(in srgb, var(--route-color) 19%, transparent);
}
.route-head {
display: flex;
align-items: center;
gap: 9px;
}
.route-swatch {
width: 11px;
height: 11px;
flex: 0 0 auto;
border-radius: 3px;
background: var(--route-color);
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.09);
}
.route-name {
font-size: 13px;
font-weight: 800;
letter-spacing: -0.015em;
}
.route-kind {
margin-left: auto;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.metrics {
display: grid;
grid-template-columns: 0.9fr 1.25fr;
margin-top: 11px;
overflow: hidden;
border: 1px solid rgba(24, 50, 56, 0.11);
border-radius: 10px;
background: rgba(247, 244, 235, 0.72);
}
.metric {
min-width: 0;
padding: 9px 10px;
}
.metric + .metric {
border-left: 1px solid rgba(24, 50, 56, 0.11);
}
.metric-label {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric-value {
display: block;
font-size: 18px;
font-weight: 850;
letter-spacing: -0.035em;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.metric-detail {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
line-height: 1.3;
}
.method {
margin: 12px 2px 0;
color: var(--muted);
font-size: 9.5px;
line-height: 1.5;
}
.legal {
margin: 12px 0 0;
padding: 10px 11px;
border: 1px solid rgba(231, 91, 115, 0.22);
border-radius: 10px;
background: rgba(231, 91, 115, 0.075);
color: #624a4d;
font-size: 9.5px;
line-height: 1.48;
}
.legal strong {
color: #713e48;
}
.controls {
position: fixed;
bottom: 18px;
left: 18px;
z-index: 4;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border: 1px solid rgba(255, 255, 255, 0.6);
border-radius: 14px;
background: rgba(250, 247, 238, 0.84);
box-shadow: 0 8px 26px rgba(29, 51, 51, 0.13);
backdrop-filter: blur(12px);
}
.control-button {
padding: 7px 10px;
border: 1px solid rgba(24, 50, 56, 0.13);
border-radius: 9px;
background: rgba(255, 255, 255, 0.65);
color: var(--ink);
cursor: pointer;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.045em;
text-transform: uppercase;
}
.control-button:hover {
background: #fff;
}
.control-hint {
padding: 0 7px 0 4px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
white-space: nowrap;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #bdd3d1;
color: #35575a;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
.masthead {
top: 14px;
left: 15px;
width: calc(100vw - 30px);
}
h1 {
max-width: 290px;
font-size: 35px;
}
.subtitle {
max-width: 280px;
font-size: 11px;
}
.model-badge {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
max-height: 53vh;
padding: 12px;
border-radius: 15px;
}
.panel-head {
margin-bottom: 9px;
}
.panel h2 {
font-size: 15px;
}
.route-card {
padding: 9px;
}
.metrics {
margin-top: 8px;
}
.metric {
padding: 7px 8px;
}
.metric-value {
font-size: 15px;
}
.controls {
top: 130px;
bottom: auto;
left: 12px;
}
.control-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive schematic voxel model of Pelourinho, Salvador"></main>
<header class="masthead">
<div class="eyebrow">Salvador · Bahia · Brazil</div>
<h1>Pelourinho,<br />uphill</h1>
<p class="subtitle">
A schematic voxel district slice for comparing a stair cut, a steep lane,
and a longer gentler route.
</p>
<div class="model-badge">Schematic model · no compliance score</div>
</header>
<aside class="panel" aria-label="Route comparison">
<div class="panel-head">
<div>
<h2>Compare the three routes</h2>
<p class="panel-intro">
Select a card or a route in the model to focus it.
</p>
</div>
<button id="compareAll" class="compare-button active" type="button">
Compare all
</button>
</div>
<div id="routeList" class="route-list"></div>
<p class="method">
Distance is the modeled three-dimensional path length. Overall grade is
rise divided by horizontal run; maximum grade is the steepest modeled
segment. Rounded values are diagrammatic, not survey measurements.
</p>
<p class="legal">
<strong>Model only — not an accessibility assessment.</strong>
This schematic does not establish accessibility, legal compliance, safe
travel, or current route conditions. The stair route is not step-free.
Consult local authorities and current on-site information for real travel
decisions.
</p>
</aside>
<nav class="controls" aria-label="Scene controls">
<button id="resetView" class="control-button" type="button">Reset view</button>
<button id="toggleLabels" class="control-button" type="button">Hide labels</button>
<span class="control-hint">
Drag: orbit · Wheel: zoom · Shift-drag: pan · Double-click: reset
</span>
</nav>
<div id="loading">Building the district…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Three-dimensional schematic Pelourinho district. Drag to orbit and use the mouse wheel to zoom."
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbdd3d1);
scene.fog = new THREE.Fog(0xbdd3d1, 190, 420);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
600
);
const hemisphere = new THREE.HemisphereLight(0xf6fbff, 0x765c4e, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d1, 3.2);
sun.position.set(-62, 115, 74);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -105;
sun.shadow.camera.right = 105;
sun.shadow.camera.top = 105;
sun.shadow.camera.bottom = -105;
sun.shadow.camera.near = 10;
sun.shadow.camera.far = 260;
sun.shadow.bias = -0.0007;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8e6e4, 0.85);
fill.position.set(80, 45, -85);
scene.add(fill);
const district = new THREE.Group();
scene.add(district);
const labelGroup = new THREE.Group();
scene.add(labelGroup);
const pickableRouteMeshes = [];
// -------------------------------------------------------------------------
// Deterministic helpers
// -------------------------------------------------------------------------
let randomSeed = 73421;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
const DISTRICT_WIDTH = 144;
const DISTRICT_DEPTH = 136;
const Z_MIN = -68;
const Z_MAX = 68;
const METERS_PER_UNIT = 4.25;
function surfaceY(z) {
const t = (z - Z_MIN) / (DISTRICT_DEPTH);
return 35 - t * 40;
}
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const matrix = new THREE.Matrix4();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
const position = new THREE.Vector3();
const color = new THREE.Color();
function makeInstances(items, material, geometry = unitBox) {
const mesh = new THREE.InstancedMesh(geometry, material, items.length);
items.forEach((item, index) => {
position.set(...item.position);
scale.set(...item.scale);
if (item.quaternion) {
quaternion.copy(item.quaternion);
} else {
quaternion.setFromEuler(
new THREE.Euler(
item.rotation?.[0] ?? 0,
item.rotation?.[1] ?? 0,
item.rotation?.[2] ?? 0
)
);
}
matrix.compose(position, quaternion, scale);
mesh.setMatrixAt(index, matrix);
if (item.color !== undefined) {
color.set(item.color);
mesh.setColorAt(index, color);
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeBox(width, height, depth, x, y, z, material, parent = district) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function distanceToSegment(px, pz, ax, az, bx, bz) {
const dx = bx - ax;
const dz = bz - az;
const lengthSquared = dx * dx + dz * dz;
if (lengthSquared === 0) {
return Math.hypot(px - ax, pz - az);
}
const t = clamp(
((px - ax) * dx + (pz - az) * dz) / lengthSquared,
0,
1
);
const qx = ax + t * dx;
const qz = az + t * dz;
return Math.hypot(px - qx, pz - qz);
}
function distanceToPolyline(px, pz, points) {
let minimum = Infinity;
for (let i = 0; i < points.length - 1; i++) {
minimum = Math.min(
minimum,
distanceToSegment(
px,
pz,
points[i][0],
points[i][1],
points[i + 1][0],
points[i + 1][1]
)
);
}
return minimum;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(text, subtitle = "", accent = "#f2a93b", scaleWidth = 28) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 170;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 624, 150, 22);
ctx.fillStyle = "rgba(250,247,238,0.92)";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = accent;
ctx.stroke();
ctx.fillStyle = "#183238";
ctx.font = "800 43px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 320, subtitle ? 61 : 83);
if (subtitle) {
ctx.fillStyle = "#617276";
ctx.font = "700 24px system-ui, sans-serif";
ctx.fillText(subtitle, 320, 116);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scaleWidth, scaleWidth * 0.266, 1);
sprite.renderOrder = 20;
labelGroup.add(sprite);
return sprite;
}
// -------------------------------------------------------------------------
// Sloping voxel-model terrain
// -------------------------------------------------------------------------
const terrainGeometry = new THREE.PlaneGeometry(
DISTRICT_WIDTH,
DISTRICT_DEPTH,
36,
34
);
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPositions = terrainGeometry.attributes.position;
const terrainColors = new Float32Array(terrainPositions.count * 3);
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
const y = surfaceY(z);
terrainPositions.setY(i, y);
const checker = (Math.floor((x + 100) / 4) + Math.floor((z + 100) / 4)) % 2;
const variation = 0.91 + checker * 0.025 + random() * 0.035;
terrainColors[i * 3] = 0.79 * variation;
terrainColors[i * 3 + 1] = 0.69 * variation;
terrainColors[i * 3 + 2] = 0.51 * variation;
}
terrainGeometry.setAttribute(
"color",
new THREE.BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.96,
flatShading: true
});
const terrain = new THREE.Mesh(terrainGeometry, terrainMaterial);
terrain.receiveShadow = true;
district.add(terrain);
const earthMaterial = new THREE.MeshStandardMaterial({
color: 0x9e755b,
roughness: 1,
flatShading: true
});
const earthBottom = new THREE.Mesh(
new THREE.BoxGeometry(DISTRICT_WIDTH, 4, DISTRICT_DEPTH),
earthMaterial
);
earthBottom.position.set(0, -11, 0);
earthBottom.receiveShadow = true;
district.add(earthBottom);
function addSkirtQuad(a, b, c, d) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([...a, ...b, ...c, ...a, ...c, ...d], 3)
);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, earthMaterial);
mesh.receiveShadow = true;
district.add(mesh);
}
for (let z = Z_MIN; z < Z_MAX; z += 4) {
const z2 = Math.min(z + 4, Z_MAX);
const x = -DISTRICT_WIDTH / 2;
addSkirtQuad(
[x, surfaceY(z), z],
[x, surfaceY(z2), z2],
[x, -9, z2],
[x, -9, z]
);
addSkirtQuad(
[-x, surfaceY(z2), z2],
[-x, surfaceY(z), z],
[-x, -9, z],
[-x, -9, z2]
);
}
for (let x = -DISTRICT_WIDTH / 2; x < DISTRICT_WIDTH / 2; x += 4) {
const x2 = Math.min(x + 4, DISTRICT_WIDTH / 2);
addSkirtQuad(
[x2, surfaceY(Z_MAX), Z_MAX],
[x, surfaceY(Z_MAX), Z_MAX],
[x, -9, Z_MAX],
[x2, -9, Z_MAX]
);
addSkirtQuad(
[x, surfaceY(Z_MIN), Z_MIN],
[x2, surfaceY(Z_MIN), Z_MIN],
[x2, -9, Z_MIN],
[x, -9, Z_MIN]
);
}
// Stylized water beyond the lower edge.
const water = new THREE.Mesh(
new THREE.BoxGeometry(178, 1.2, 23),
new THREE.MeshStandardMaterial({
color: 0x4ba7a6,
roughness: 0.55,
metalness: 0.06
})
);
water.position.set(0, -8.6, 80);
water.receiveShadow = true;
district.add(water);
// -------------------------------------------------------------------------
// Street network
// -------------------------------------------------------------------------
const streetMaterial = new THREE.MeshStandardMaterial({
color: 0x596164,
roughness: 0.94
});
const streetItems = [];
function addStreetSegment(ax, az, bx, bz, width = 5.5) {
const a = new THREE.Vector3(ax, surfaceY(az) + 0.16, az);
const b = new THREE.Vector3(bx, surfaceY(bz) + 0.16, bz);
const direction = b.clone().sub(a);
const length = direction.length();
const q = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
direction.clone().normalize()
);
streetItems.push({
position: a.clone().add(b).multiplyScalar(0.5).toArray(),
scale: [length, 0.26, width],
quaternion: q
});
}
function addStreet(points, width = 5.5) {
for (let i = 0; i < points.length - 1; i++) {
addStreetSegment(
points[i][0],
points[i][1],
points[i + 1][0],
points[i + 1][1],
width
);
}
}
const secondaryStreets = [
[[-59, -62], [-55, -39], [-59, -16], [-53, 8], [-57, 31], [-51, 61]],
[[-66, -25], [-40, -22], [-15, -24], [10, -21], [36, -18], [65, -15]],
[[-64, 15], [-39, 12], [-13, 16], [14, 13], [40, 17], [66, 20]],
[[17, -63], [15, -40], [19, -18], [16, 5], [20, 29], [18, 61]],
[[-61, 52], [-32, 50], [-5, 54], [25, 50], [49, 52], [66, 57]],
[[-45, -53], [-21, -54], [4, -53], [28, -53], [50, -49]]
];
secondaryStreets.forEach((street) => addStreet(street));
const streetsMesh = makeInstances(streetItems, streetMaterial);
streetsMesh.castShadow = false;
district.add(streetsMesh);
// -------------------------------------------------------------------------
// Route definitions and metrics
// -------------------------------------------------------------------------
const routeDefinitions = [
{
id: "stairs",
name: "Stair cut",
kind: "Direct stair sequence",
color: 0xf2a93b,
css: "#f2a93b",
mode: "stairs",
points: [
[-40, -52], [-36, -41], [-39, -30], [-33, -19], [-35, -7],
[-29, 5], [-31, 18], [-24, 30], [-26, 42], [-18, 52]
]
},
{
id: "steep",
name: "Steep lane",
kind: "Short switchback lane",
color: 0xe75b73,
css: "#e75b73",
mode: "lane",
points: [
[-16, -52], [-9, -42], [-14, -31], [-5, -22], [-10, -11],
[1, -1], [-5, 10], [4, 21], [-1, 32], [7, 43], [2, 53]
]
},
{
id: "gentle",
name: "Gentler lane",
kind: "Long contouring route",
color: 0x168e83,
css: "#168e83",
mode: "lane",
points: [
[22, -52], [40, -49], [57, -42], [48, -33], [65, -25],
[51, -14], [70, -5], [55, 7], [71, 17], [56, 28],
[69, 38], [52, 49], [34, 52], [24, 48]
]
}
];
function computeRouteMetrics(points) {
let distance = 0;
let horizontal = 0;
let maxGrade = 0;
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const horizontalLength = Math.hypot(b.x - a.x, b.z - a.z);
const vertical = Math.abs(b.y - a.y);
distance += a.distanceTo(b);
horizontal += horizontalLength;
if (horizontalLength > 0) {
maxGrade = Math.max(maxGrade, (vertical / horizontalLength) * 100);
}
}
const rise = Math.abs(points[points.length - 1].y - points[0].y);
const averageGrade = horizontal > 0 ? (rise / horizontal) * 100 : 0;
return {
distanceMeters: distance * METERS_PER_UNIT,
averageGrade,
maximumGrade: maxGrade
};
}
const routes = [];
function buildRoute(definition) {
const vectorPoints = definition.points.map(
([x, z]) => new THREE.Vector3(x, surfaceY(z), z)
);
let pathPoints = vectorPoints;
let curve = null;
if (definition.mode === "lane") {
curve = new THREE.CatmullRomCurve3(
vectorPoints,
false,
"catmullrom",
0.08
);
pathPoints = curve.getPoints(300);
}
const metrics = computeRouteMetrics(pathPoints);
const route = {
...definition,
pathPoints,
curve,
metrics,
center: new THREE.Vector3(),
markerMaterial: new THREE.MeshStandardMaterial({
color: definition.color,
emissive: definition.color,
emissiveIntensity: 0.26,
roughness: 0.58,
transparent: true,
opacity: 1,
depthWrite: false
})
};
for (const point of pathPoints) {
route.center.add(point);
}
route.center.multiplyScalar(1 / pathPoints.length);
const roadItems = [];
const markerItems = [];
let stepCount = 0;
if (definition.mode === "stairs") {
const treadItems = [];
const riserItems = [];
for (let segment = 0; segment < vectorPoints.length - 1; segment++) {
const a = vectorPoints[segment];
const b = vectorPoints[segment + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const horizontalLength = Math.hypot(dx, dz);
const count = Math.ceil(horizontalLength / 1.25);
const treadLength = horizontalLength / count;
const angle = Math.atan2(dx, dz);
let previousY = a.y;
stepCount += count;
for (let step = 0; step < count; step++) {
const t = (step + 0.5) / count;
const x = a.x + dx * t;
const z = a.z + dz * t;
const y = surfaceY(z);
treadItems.push({
position: [x, y + 0.17, z],
scale: [4.7, 0.34, treadLength + 0.04],
rotation: [0, angle, 0]
});
const boundaryT = step / count;
const bx = a.x + dx * boundaryT;
const bz = a.z + dz * boundaryT;
const boundaryY = surfaceY(bz);
const riserHeight = Math.abs(boundaryY - previousY);
if (riserHeight > 0.02) {
riserItems.push({
position: [bx, (boundaryY + previousY) / 2, bz],
scale: [4.7, riserHeight + 0.04, 0.15],
rotation: [0, angle, 0]
});
}
markerItems.push({
position: [x, y + 0.39, z],
scale: [4.15, 0.13, 0.22],
rotation: [0, angle, 0]
});
previousY = boundaryY;
}
}
const treadMaterial = new THREE.MeshStandardMaterial({
color: 0x687073,
roughness: 0.95
});
const riserMaterial = new THREE.MeshStandardMaterial({
color: 0x4b5559,
roughness: 1
});
district.add(makeInstances(treadItems, treadMaterial));
district.add(makeInstances(riserItems, riserMaterial));
} else {
const samples = pathPoints;
for (let i = 0; i < samples.length - 1; i++) {
const a = samples[i];
const b = samples[i + 1];
const direction = b.clone().sub(a);
const length = direction.length();
const q = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
direction.clone().normalize()
);
roadItems.push({
position: a.clone().add(b).multiplyScalar(0.5).toArray(),
scale: [length, 0.34, 5.5],
quaternion: q
});
}
for (let i = 4; i < samples.length - 3; i += 9) {
const point = samples[i];
markerItems.push({
position: [point.x, point.y + 0.47, point.z],
scale: [1.45, 0.3, 1.45]
});
}
const roadMaterial = new THREE.MeshStandardMaterial({
color: definition.id === "steep" ? 0x566164 : 0x60716e,
roughness: 0.95
});
district.add(makeInstances(roadItems, roadMaterial));
}
const markerMesh = makeInstances(markerItems, route.markerMaterial);
markerMesh.userData.routeId = definition.id;
markerMesh.castShadow = false;
district.add(markerMesh);
pickableRouteMeshes.push(markerMesh);
const endpoints = new THREE.Group();
for (const point of [pathPoints[0], pathPoints[pathPoints.length - 1]]) {
const marker = makeBox(
1.8,
1.8,
1.8,
point.x,
point.y + 1.5,
point.z,
route.markerMaterial,
endpoints
);
marker.rotation.y = Math.PI / 4;
marker.userData.routeId = definition.id;
pickableRouteMeshes.push(marker);
}
district.add(endpoints);
route.endpoints = endpoints;
const midpointIndex = Math.floor(pathPoints.length * 0.46);
const midpoint = pathPoints[midpointIndex];
const routeLabel = makeLabel(
definition.name.toUpperCase(),
definition.id === "stairs" ? "step sequence" : "schematic lane",
definition.css,
definition.id === "gentle" ? 25 : 22
);
routeLabel.position.set(
midpoint.x,
midpoint.y + 5.5,
midpoint.z
);
route.label = routeLabel;
route.stepCount = stepCount;
routes.push(route);
}
routeDefinitions.forEach(buildRoute);
// -------------------------------------------------------------------------
// Voxel buildings
// -------------------------------------------------------------------------
const facadePalette = [
0xe9b95f, 0xe98767, 0x72b8b1, 0xf0d5a1,
0xd77878, 0x9abbd0, 0xe9e0c7, 0xc69b73,
0xdf9e53, 0x73a48e
];
const roofPalette = [
0xa94f3f, 0xb95d46, 0xc26a4d, 0x93463b
];
const bodyItems = [];
const foundationItems = [];
const roofItems = [];
const windowItems = [];
const doorItems = [];
const awningItems = [];
const buildingMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.82
});
const foundationMaterial = new THREE.MeshStandardMaterial({
color: 0x9d8d76,
roughness: 1
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88
});
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x263f46,
roughness: 0.45,
metalness: 0.06
});
const doorMaterial = new THREE.MeshStandardMaterial({
color: 0x355a55,
roughness: 0.75
});
const awningMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.8
});
const allRouteXZ = routes.map((route) =>
route.pathPoints.map((point) => [point.x, point.z])
);
function nearAnyRoute(x, z, clearance) {
return allRouteXZ.some(
(points) => distanceToPolyline(x, z, points) < clearance
);
}
function nearSecondaryStreet(x, z, clearance) {
return secondaryStreets.some(
(points) => distanceToPolyline(x, z, points) < clearance
);
}
for (let z = -58; z <= 52; z += 12.5) {
for (let x = -61; x <= 62; x += 12.5) {
const jitterX = (random() - 0.5) * 2.2;
const jitterZ = (random() - 0.5) * 2.2;
const px = x + jitterX;
const pz = z + jitterZ;
const width = 6.5 + random() * 3.2;
const depth = 6 + random() * 3.4;
const height = 8 + random() * 13;
const radius = Math.max(width, depth) * 0.55;
if (nearAnyRoute(px, pz, radius + 3.2)) continue;
if (nearSecondaryStreet(px, pz, radius + 2.4)) continue;
// Keep the landmark areas open.
if (Math.hypot(px - 35, pz + 8) < 19) continue;
if (Math.hypot(px + 5, pz - 61) < 12) continue;
const base = surfaceY(pz);
const cornerLow = Math.min(
surfaceY(pz - depth / 2),
surfaceY(pz + depth / 2)
);
const foundationBottom = cornerLow - 0.9;
foundationItems.push({
position: [px, (foundationBottom + base) / 2, pz],
scale: [width + 0.7, base - foundationBottom, depth + 0.7]
});
bodyItems.push({
position: [px, base + height / 2, pz],
scale: [width, height, depth],
color: facadePalette[Math.floor(random() * facadePalette.length)]
});
roofItems.push({
position: [px, base + height + 0.55, pz],
scale: [width + 0.8, 1.1, depth + 0.8],
color: roofPalette[Math.floor(random() * roofPalette.length)]
});
roofItems.push({
position: [px, base + height + 1.45, pz],
scale: [width * 0.68, 0.9, depth * 0.68],
color: roofPalette[Math.floor(random() * roofPalette.length)]
});
if (random() > 0.65) {
roofItems.push({
position: [
px + (random() - 0.5) * width * 0.5,
base + height + 2.3,
pz + (random() - 0.5) * depth * 0.5
],
scale: [0.8, 1.8, 0.8],
color: 0xe2c7a4
});
}
const floors = Math.max(2, Math.floor((height - 2) / 3.2));
const windowColumns = width > 8 ? 3 : 2;
for (let floor = 0; floor < floors; floor++) {
const wy = base + 2.5 + floor * 3.1;
for (let column = 0; column < windowColumns; column++) {
const wx =
px +
(column - (windowColumns - 1) / 2) * (width / (windowColumns + 0.7));
windowItems.push({
position: [wx, wy, pz + depth / 2 + 0.055],
scale: [0.78, 1.18, 0.11]
});
}
if (random() > 0.24) {
windowItems.push({
position: [px + width / 2 + 0.055, wy, pz],
scale: [0.78, 1.18, 0.11],
rotation: [0, Math.PI / 2, 0]
});
}
}
doorItems.push({
position: [px, base + 1.35, pz + depth / 2 + 0.08],
scale: [1.45, 2.7, 0.16]
});
awningItems.push({
position: [px, base + 3.05, pz + depth / 2 + 0.48],
scale: [2.3, 0.18, 0.9],
color: facadePalette[Math.floor(random() * facadePalette.length)]
});
}
}
district.add(makeInstances(foundationItems, foundationMaterial));
district.add(makeInstances(bodyItems, buildingMaterial));
district.add(makeInstances(roofItems, roofMaterial));
district.add(makeInstances(windowItems, windowMaterial));
district.add(makeInstances(doorItems, doorMaterial));
district.add(makeInstances(awningItems, awningMaterial));
// -------------------------------------------------------------------------
// Landmark church
// -------------------------------------------------------------------------
const church = new THREE.Group();
district.add(church);
const cream = new THREE.MeshStandardMaterial({
color: 0xf0dfbd,
roughness: 0.82
});
const churchRed = new THREE.MeshStandardMaterial({
color: 0xb55743,
roughness: 0.84
});
const churchX = 35;
const churchZ = -8;
const churchBase = surfaceY(churchZ);
makeBox(14, 14, 17, churchX, churchBase + 7, churchZ, cream, church);
makeBox(15.2, 1.2, 18.2, churchX, churchBase + 14.2, churchZ, churchRed, church);
for (const side of [-1, 1]) {
const tx = churchX + side * 5.6;
const tz = churchZ + 2.4;
makeBox(4.5, 18, 4.5, tx, churchBase + 9, tz, cream, church);
makeBox(5.1, 1, 5.1, tx, churchBase + 18.2, tz, churchRed, church);
makeBox(
1.6,
3.1,
0.16,
tx,
churchBase + 14.1,
tz + 2.31,
windowMaterial,
church
);
}
makeBox(
3.1,
4.1,
0.18,
churchX,
churchBase + 7.5,
churchZ + 8.58,
windowMaterial,
church
);
makeBox(
2.6,
3.6,
0.2,
churchX,
churchBase + 2.2,
churchZ + 8.58,
doorMaterial,
church
);
for (let step = 0; step < 5; step++) {
makeBox(
7.2 - step * 0.25,
0.45,
1.1,
churchX,
churchBase + 0.25 - step * 0.2,
churchZ + 9.2 + step * 0.9,
cream,
church
);
}
makeBox(
0.35,
2.4,
0.35,
churchX,
churchBase + 19.6,
churchZ + 2.4,
churchRed,
church
);
makeBox(
1.5,
0.35,
0.35,
churchX,
churchBase + 20.1,
churchZ + 2.4,
churchRed,
church
);
const churchLabel = makeLabel(
"CHURCH",
"landmark illustration",
"#b55743",
24
);
churchLabel.position.set(churchX, churchBase + 24, churchZ);
// -------------------------------------------------------------------------
// Landmark elevator at the lower edge
// -------------------------------------------------------------------------
const elevator = new THREE.Group();
district.add(elevator);
const elevatorRed = new THREE.MeshStandardMaterial({
color: 0xb8493e,
roughness: 0.8
});
const elevatorBase = surfaceY(61);
makeBox(1.7, 13, 3.5, -7.8, elevatorBase + 6.5, 61, elevatorRed, elevator);
makeBox(1.7, 13, 3.5, -3.2, elevatorBase + 6.5, 61, elevatorRed, elevator);
makeBox(7.4, 1.5, 4.2, -5.5, elevatorBase + 13.4, 61, elevatorRed, elevator);
makeBox(5.7, 6.2, 0.16, -5.5, elevatorBase + 6.2, 62.82, windowMaterial, elevator);
const elevatorLabel = makeLabel(
"ELEVADOR",
"landmark illustration",
"#b8493e",
21
);
elevatorLabel.position.set(-5.5, elevatorBase + 18.5, 61);
// -------------------------------------------------------------------------
// Voxel trees and street lamps
// -------------------------------------------------------------------------
const trunkItems = [];
const foliageItems = [];
const lampPostItems = [];
const lampTopItems = [];
const treeLocations = [
[-62, -47], [-61, -5], [-60, 36], [63, -48], [64, -8],
[63, 37], [-27, -59], [-3, -59], [35, -59], [25, 59],
[57, 58], [63, 54], [-43, 59], [-20, 59]
];
for (const [x, z] of treeLocations) {
if (nearAnyRoute(x, z, 5)) continue;
const y = surfaceY(z);
trunkItems.push({
position: [x, y + 1.7, z],
scale: [0.75, 3.4, 0.75]
});
foliageItems.push({
position: [x, y + 4.3, z],
scale: [3.4, 3.1, 3.4],
color: 0x507e61
});
foliageItems.push({
position: [x + 0.35, y + 6, z - 0.2],
scale: [2.4, 2.3, 2.4],
color: 0x67966c
});
}
const lampPositions = [
[-52, -35], [-54, 2], [-50, 42], [-34, -24], [-9, -25],
[29, -19], [52, -15], [-37, 14], [-9, 16], [35, 17],
[15, 42], [47, 51], [-20, 51]
];
for (const [x, z] of lampPositions) {
const y = surfaceY(z);
lampPostItems.push({
position: [x, y + 2.1, z],
scale: [0.24, 4.2, 0.24]
});
lampTopItems.push({
position: [x, y + 4.35, z],
scale: [0.8, 0.7, 0.8]
});
}
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x77533b,
roughness: 1
});
const foliageMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
});
const lampPostMaterial = new THREE.MeshStandardMaterial({
color: 0x354a4d,
roughness: 0.7
});
const lampTopMaterial = new THREE.MeshStandardMaterial({
color: 0xffe4a2,
emissive: 0xffca73,
emissiveIntensity: 0.45,
roughness: 0.45
});
district.add(makeInstances(trunkItems, trunkMaterial));
district.add(makeInstances(foliageItems, foliageMaterial));
district.add(makeInstances(lampPostItems, lampPostMaterial));
district.add(makeInstances(lampTopItems, lampTopMaterial));
const upperLabel = makeLabel(
"UPPER EDGE",
"Pelourinho hillside",
"#f2a93b",
26
);
upperLabel.position.set(-4, surfaceY(-61) + 5, -61);
const lowerLabel = makeLabel(
"LOWER EDGE",
"toward the harbor",
"#168e83",
25
);
lowerLabel.position.set(0, surfaceY(60) + 5, 60);
// -------------------------------------------------------------------------
// Direct camera controls — no external controls module
// -------------------------------------------------------------------------
const DEFAULT_TARGET = new THREE.Vector3(0, 10, 0);
const cameraState = {
target: DEFAULT_TARGET.clone(),
targetGoal: DEFAULT_TARGET.clone(),
radius: 190,
radiusGoal: 190,
theta: 0.72,
phi: 0.86
};
function updateCamera() {
const offset = new THREE.Vector3().setFromSphericalCoords(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.copy(cameraState.target).add(offset);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.targetGoal.copy(DEFAULT_TARGET);
cameraState.radiusGoal = 190;
cameraState.theta = 0.72;
cameraState.phi = 0.86;
}
let pointerDown = false;
let pointerMoved = false;
let lastX = 0;
let lastY = 0;
let panMode = false;
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
lastX = event.clientX;
lastY = event.clientY;
panMode = event.shiftKey || event.button === 2;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
if (Math.abs(dx) + Math.abs(dy) > 2) pointerMoved = true;
lastX = event.clientX;
lastY = event.clientY;
if (panMode) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const horizontalForward = forward.clone();
horizontalForward.y = 0;
horizontalForward.normalize();
const panScale = cameraState.radius * 0.00135;
cameraState.targetGoal
.addScaledVector(right, -dx * panScale)
.addScaledVector(horizontalForward, dy * panScale);
} else {
cameraState.theta -= dx * 0.005;
cameraState.phi = clamp(cameraState.phi - dy * 0.0042, 0.34, 1.35);
}
});
canvas.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerMoved) {
pickRoute(event);
}
pointerDown = false;
panMode = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
panMode = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radiusGoal = clamp(
cameraState.radiusGoal * Math.exp(event.deltaY * 0.001),
82,
270
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowLeft") cameraState.theta += 0.08;
if (key === "ArrowRight") cameraState.theta -= 0.08;
if (key === "ArrowUp") cameraState.phi = clamp(cameraState.phi - 0.06, 0.34, 1.35);
if (key === "ArrowDown") cameraState.phi = clamp(cameraState.phi + 0.06, 0.34, 1.35);
if (key === "+" || key === "=") cameraState.radiusGoal = clamp(cameraState.radiusGoal - 8, 82, 270);
if (key === "-") cameraState.radiusGoal = clamp(cameraState.radiusGoal + 8, 82, 270);
if (key === "Home") resetCamera();
});
// -------------------------------------------------------------------------
// Route selection, cards, and ray picking
// -------------------------------------------------------------------------
const routeList = document.querySelector("#routeList");
const compareAllButton = document.querySelector("#compareAll");
let selectedRouteId = null;
routes.forEach((route) => {
const card = document.createElement("button");
card.className = "route-card";
card.type = "button";
card.dataset.route = route.id;
card.style.setProperty("--route-color", route.css);
card.setAttribute("aria-pressed", "false");
const distance = Math.round(route.metrics.distanceMeters / 10) * 10;
const average = Math.round(route.metrics.averageGrade);
const maximum = Math.round(route.metrics.maximumGrade);
const routeKind =
route.mode === "stairs"
? `≈ ${route.stepCount} steps`
: route.kind;
card.innerHTML = `
<span class="route-head">
<span class="route-swatch"></span>
<span class="route-name">${route.name}</span>
<span class="route-kind">${routeKind}</span>
</span>
<span class="metrics">
<span class="metric">
<span class="metric-label">Distance</span>
<span class="metric-value">≈ ${distance} m</span>
<span class="metric-detail">modeled path length</span>
</span>
<span class="metric">
<span class="metric-label">Slope</span>
<span class="metric-value">≈ ${average}%</span>
<span class="metric-detail">overall · max ≈ ${maximum}%</span>
</span>
</span>
`;
card.addEventListener("click", () => {
selectRoute(selectedRouteId === route.id ? null : route.id);
});
route.card = card;
routeList.appendChild(card);
});
function selectRoute(routeId) {
selectedRouteId = routeId;
routes.forEach((route) => {
const selected = route.id === routeId;
const dimmed = routeId !== null && !selected;
route.markerMaterial.opacity = dimmed ? 0.16 : 1;
route.markerMaterial.emissiveIntensity = selected ? 0.65 : 0.26;
route.markerMaterial.depthWrite = !dimmed;
route.card.classList.toggle("active", selected);
route.card.setAttribute("aria-pressed", String(selected));
});
compareAllButton.classList.toggle("active", routeId === null);
if (routeId) {
const route = routes.find((item) => item.id === routeId);
cameraState.targetGoal.copy(route.center);
cameraState.radiusGoal = route.id === "gentle" ? 132 : 112;
} else {
cameraState.targetGoal.copy(DEFAULT_TARGET);
cameraState.radiusGoal = 190;
}
}
compareAllButton.addEventListener("click", () => selectRoute(null));
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function pickRoute(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickableRouteMeshes, false);
if (intersections.length) {
const routeId = intersections[0].object.userData.routeId;
selectRoute(selectedRouteId === routeId ? null : routeId);
}
}
// -------------------------------------------------------------------------
// Other controls and resize
// -------------------------------------------------------------------------
document.querySelector("#resetView").addEventListener("click", resetCamera);
const labelButton = document.querySelector("#toggleLabels");
let labelsVisible = true;
labelButton.addEventListener("click", () => {
labelsVisible = !labelsVisible;
labelGroup.visible = labelsVisible;
labelButton.textContent = labelsVisible ? "Hide labels" : "Show labels";
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
cameraState.target.lerp(cameraState.targetGoal, 0.085);
cameraState.radius += (cameraState.radiusGoal - cameraState.radius) * 0.085;
updateCamera();
renderer.render(scene, camera);
}
updateCamera();
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```