Prompt specification
Build or customize the Three.js 3D scene "Compare stair and funicular journeys" 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 stair and funicular journeys
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-stair-and-funicular-journeys-x2780
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Petit-Champlain in Québec City. Let users choose between walking and a fictional funicular schedule for the same destinations. Display path length, elevation gain, and waiting time as distinct quantities. Voxel style.
Working Three.js Starter (compare-stair-and-funicular-journeys-x2780):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Petit-Champlain — Walk or Funicular</title>
<style>
:root {
color-scheme: dark;
--ink: #f4f1e8;
--muted: #a9bdc2;
--panel: rgba(13, 28, 34, 0.91);
--panel-soft: rgba(255, 255, 255, 0.065);
--border: rgba(255, 255, 255, 0.13);
--cyan: #63e4db;
--cyan-dark: #1f8f91;
--orange: #ff9b54;
--orange-dark: #d85d38;
--yellow: #ffd16a;
--shadow: 0 18px 50px rgba(2, 12, 17, 0.28);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #9bc5c8;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
button,
select,
input {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.brand {
position: fixed;
z-index: 5;
top: 24px;
left: 26px;
pointer-events: none;
text-shadow: 0 2px 14px rgba(0, 0, 0, 0.45);
}
.eyebrow {
margin-bottom: 6px;
color: #fff3ca;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.brand h1 {
max-width: 550px;
margin: 0;
font-size: clamp(25px, 3.6vw, 47px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.brand p {
margin: 9px 0 0;
color: #edf7f6;
font-size: 13px;
font-weight: 600;
}
.panel {
position: fixed;
z-index: 6;
top: 22px;
right: 22px;
width: min(390px, calc(100vw - 44px));
max-height: calc(100vh - 44px);
overflow: auto;
padding: 18px;
border: 1px solid var(--border);
border-radius: 22px;
background:
linear-gradient(150deg, rgba(255, 255, 255, 0.07), transparent 38%),
var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(125%);
scrollbar-width: thin;
scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.panel-title {
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.service-badge {
display: inline-flex;
align-items: center;
gap: 7px;
color: #d5e9e8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.service-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 12px var(--orange);
content: "";
}
.control-label {
display: block;
margin: 0 0 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
margin-bottom: 16px;
padding: 5px;
border: 1px solid var(--border);
border-radius: 15px;
background: rgba(0, 0, 0, 0.18);
}
.mode-switch button {
min-height: 45px;
border: 0;
border-radius: 11px;
background: transparent;
color: #b7c9cc;
cursor: pointer;
font-size: 13px;
font-weight: 800;
transition:
background 160ms ease,
color 160ms ease,
box-shadow 160ms ease;
}
.mode-switch button[data-mode="walk"].active {
background: var(--cyan-dark);
color: #062b2c;
box-shadow: 0 5px 18px rgba(43, 185, 180, 0.2);
}
.mode-switch button[data-mode="fun"].active {
background: var(--orange-dark);
color: #fff8e7;
box-shadow: 0 5px 18px rgba(230, 101, 55, 0.22);
}
.field {
margin-bottom: 14px;
}
.select-wrap {
position: relative;
}
.select-wrap::after {
position: absolute;
top: 50%;
right: 14px;
color: #aac3c5;
font-size: 12px;
pointer-events: none;
content: "▾";
transform: translateY(-58%);
}
select,
input[type="time"] {
width: 100%;
min-height: 46px;
border: 1px solid var(--border);
border-radius: 12px;
outline: none;
background: rgba(255, 255, 255, 0.07);
font-size: 13px;
font-weight: 650;
}
select {
padding: 0 38px 0 13px;
appearance: none;
cursor: pointer;
}
input[type="time"] {
padding: 0 12px;
color-scheme: dark;
}
select:focus,
input[type="time"]:focus {
border-color: rgba(99, 228, 219, 0.72);
box-shadow: 0 0 0 3px rgba(99, 228, 219, 0.11);
}
.metrics {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 8px;
margin: 17px 0 12px;
}
.metric {
min-width: 0;
padding: 12px 10px 11px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel-soft);
}
.metric-label {
display: block;
min-height: 25px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.07em;
line-height: 1.25;
text-transform: uppercase;
}
.metric-value {
display: block;
margin-top: 6px;
overflow: hidden;
color: #fff;
font-size: clamp(17px, 2vw, 23px);
font-weight: 850;
letter-spacing: -0.04em;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric-value small {
color: #bed0d2;
font-size: 10px;
font-weight: 750;
letter-spacing: 0;
}
.metric:nth-child(1) .metric-value {
color: var(--cyan);
}
.metric:nth-child(2) .metric-value {
color: var(--yellow);
}
.metric:nth-child(3) .metric-value {
color: var(--orange);
}
.microcopy {
margin: 0;
color: #90a9ad;
font-size: 10px;
line-height: 1.45;
}
.route-card,
.schedule-card {
margin-top: 14px;
padding: 13px;
border: 1px solid var(--border);
border-radius: 14px;
background: rgba(0, 0, 0, 0.14);
}
.route-card strong,
.schedule-card strong {
display: block;
margin-bottom: 6px;
font-size: 12px;
}
.route-card p {
margin: 0;
color: #b6c9cb;
font-size: 11px;
line-height: 1.5;
}
.compare {
display: block;
margin-top: 9px;
color: #e5eeed;
font-size: 10px;
font-weight: 700;
}
.schedule-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 9px;
}
.schedule-head span {
color: var(--muted);
font-size: 10px;
}
.schedule {
display: flex;
gap: 6px;
overflow-x: auto;
padding-bottom: 4px;
scrollbar-width: none;
}
.schedule::-webkit-scrollbar {
display: none;
}
.schedule button {
flex: 0 0 auto;
padding: 7px 9px;
border: 1px solid var(--border);
border-radius: 8px;
background: rgba(255, 255, 255, 0.045);
color: #a9bec1;
cursor: pointer;
font-size: 10px;
font-weight: 800;
}
.schedule button.next {
border-color: var(--orange);
background: rgba(255, 155, 84, 0.16);
color: #ffd2aa;
}
.schedule button:hover {
border-color: rgba(255, 255, 255, 0.35);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 14px;
color: #a8bdc0;
font-size: 10px;
font-weight: 700;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
display: inline-block;
width: 16px;
height: 4px;
border-radius: 3px;
background: var(--cyan);
}
.legend i.rail {
background: var(--orange);
}
.reset {
position: fixed;
z-index: 5;
left: 25px;
bottom: 24px;
display: inline-flex;
align-items: center;
gap: 8px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.24);
border-radius: 11px;
background: rgba(12, 30, 35, 0.72);
color: #eff8f6;
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.17);
cursor: pointer;
font-size: 11px;
font-weight: 800;
backdrop-filter: blur(10px);
}
.reset:hover {
background: rgba(12, 30, 35, 0.9);
}
.hint {
position: fixed;
z-index: 5;
left: 50%;
bottom: 25px;
padding: 9px 13px;
border-radius: 999px;
background: rgba(12, 30, 35, 0.58);
color: #e6f1ef;
font-size: 10px;
font-weight: 700;
pointer-events: none;
transform: translateX(-50%);
backdrop-filter: blur(8px);
}
#label-layer {
position: fixed;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.map-label {
position: absolute;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 8px;
background: rgba(13, 28, 34, 0.78);
color: #fff;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.03em;
white-space: nowrap;
box-shadow: 0 5px 14px rgba(0, 0, 0, 0.16);
transform: translate(-50%, -50%);
backdrop-filter: blur(5px);
}
.map-label::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--yellow);
box-shadow: 0 0 8px rgba(255, 209, 106, 0.7);
content: "";
}
.map-label.station::before {
background: var(--orange);
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #16343a;
color: #d5e8e6;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 16px;
}
.brand h1 {
max-width: 65vw;
font-size: 28px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 46vh;
padding: 14px;
border-radius: 18px;
}
.metrics {
margin-top: 12px;
}
.metric {
padding: 9px;
}
.metric-label {
min-height: 0;
}
.reset {
top: 86px;
bottom: auto;
left: 16px;
}
.hint {
display: none;
}
.map-label {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel map of Petit-Champlain"></canvas>
<header class="brand">
<div class="eyebrow">Québec City · Québec</div>
<h1>Petit-Champlain</h1>
<p>A walking and fictional funicular wayfinding study.</p>
</header>
<aside class="panel" aria-label="Route planner">
<div class="panel-head">
<span class="panel-title">Route planner</span>
<span class="service-badge">Fictional service</span>
</div>
<span class="control-label">Travel mode</span>
<div class="mode-switch" role="group" aria-label="Travel mode">
<button type="button" data-mode="walk" class="active" aria-pressed="true">
Walk
</button>
<button type="button" data-mode="fun" aria-pressed="false">
Funicular
</button>
</div>
<div class="field">
<label class="control-label" for="destination">Destination</label>
<div class="select-wrap">
<select id="destination">
<option value="terrace">Dufferin Terrace</option>
<option value="church">Église Saint-Louis</option>
<option value="castle">Château Frontenac</option>
</select>
</div>
</div>
<div class="field">
<label class="control-label" for="arrival-time">
Arrival at funicular stop
</label>
<input
id="arrival-time"
type="time"
value="09:07"
min="09:00"
max="18:40"
step="60"
/>
</div>
<div class="metrics" aria-live="polite">
<div class="metric">
<span class="metric-label">Path length</span>
<span class="metric-value" id="metric-length">—</span>
</div>
<div class="metric">
<span class="metric-label">Elevation gain</span>
<span class="metric-value" id="metric-elevation">—</span>
</div>
<div class="metric">
<span class="metric-label">Waiting time</span>
<span class="metric-value" id="metric-wait">—</span>
</div>
</div>
<p class="microcopy">
Path length is moving distance and excludes waiting. Elevation gain counts
every positive rise, including the funicular ascent.
</p>
<section class="route-card">
<strong id="route-title">Selected route</strong>
<p id="route-description"></p>
<span class="compare" id="comparison"></span>
</section>
<section class="schedule-card">
<div class="schedule-head">
<strong>Les Voûtes timetable</strong>
<span>Every 10 min · 3 min ride</span>
</div>
<div id="schedule" class="schedule"></div>
<p class="microcopy" style="margin-top: 9px">
Imaginary service from 09:00 to 18:40. Select a departure to arrive
five minutes beforehand.
</p>
</section>
<div class="legend" aria-label="Map legend">
<span><i></i> Walking route</span>
<span><i class="rail"></i> Funicular route</span>
</div>
</aside>
<div id="label-layer" aria-hidden="true"></div>
<button class="reset" id="reset-view" type="button">
<span aria-hidden="true">↺</span>
Reset view
</button>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="loading">Building Petit-Champlain…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const labelLayer = document.querySelector("#label-layer");
const destinationSelect = document.querySelector("#destination");
const arrivalInput = document.querySelector("#arrival-time");
const metricLength = document.querySelector("#metric-length");
const metricElevation = document.querySelector("#metric-elevation");
const metricWait = document.querySelector("#metric-wait");
const routeTitle = document.querySelector("#route-title");
const routeDescription = document.querySelector("#route-description");
const comparison = document.querySelector("#comparison");
const scheduleElement = document.querySelector("#schedule");
const modeButtons = [...document.querySelectorAll("[data-mode]")];
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.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9bc8ca);
scene.fog = new THREE.Fog(0x9bc8ca, 47, 92);
const camera = new THREE.OrthographicCamera(-25, 25, 20, -20, 0.1, 160);
const cameraTarget = new THREE.Vector3(0, 1.5, 0);
let cameraYaw = 0.72;
let cameraElevation = 0.68;
let cameraZoom = 1;
const hemi = new THREE.HemisphereLight(0xd9f6ff, 0x655747, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe1ad, 3.25);
sun.position.set(-18, 32, 20);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -30;
sun.shadow.camera.right = 30;
sun.shadow.camera.top = 30;
sun.shadow.camera.bottom = -30;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 85;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x85c8e1, 0.7);
fill.position.set(22, 14, -25);
scene.add(fill);
const material = {
stone: new THREE.MeshStandardMaterial({ color: 0xc8b99b, roughness: 0.94 }),
lowerStone: new THREE.MeshStandardMaterial({ color: 0xb7aa8c, roughness: 0.98 }),
upperStone: new THREE.MeshStandardMaterial({ color: 0xd0c3a4, roughness: 0.94 }),
wall: new THREE.MeshStandardMaterial({ color: 0xe3d5b7, roughness: 0.88 }),
brick: new THREE.MeshStandardMaterial({ color: 0xb96952, roughness: 0.9 }),
brickDark: new THREE.MeshStandardMaterial({ color: 0x8f4e42, roughness: 0.94 }),
tealWall: new THREE.MeshStandardMaterial({ color: 0x78a6a0, roughness: 0.9 }),
blueWall: new THREE.MeshStandardMaterial({ color: 0x7f9ca8, roughness: 0.92 }),
creamWall: new THREE.MeshStandardMaterial({ color: 0xe4c995, roughness: 0.9 }),
roof: new THREE.MeshStandardMaterial({ color: 0x45545b, roughness: 0.84 }),
roofRed: new THREE.MeshStandardMaterial({ color: 0x8c4b42, roughness: 0.88 }),
window: new THREE.MeshStandardMaterial({
color: 0x2f535c,
roughness: 0.42,
metalness: 0.12,
}),
windowLit: new THREE.MeshStandardMaterial({
color: 0xffd886,
emissive: 0xffb84b,
emissiveIntensity: 0.55,
roughness: 0.5,
}),
wood: new THREE.MeshStandardMaterial({ color: 0x76503b, roughness: 0.95 }),
darkWood: new THREE.MeshStandardMaterial({ color: 0x473a32, roughness: 0.96 }),
trunk: new THREE.MeshStandardMaterial({ color: 0x75503a, roughness: 1 }),
leaves: new THREE.MeshStandardMaterial({ color: 0x4f8661, roughness: 0.95 }),
leavesLight: new THREE.MeshStandardMaterial({ color: 0x75a66d, roughness: 0.95 }),
water: new THREE.MeshStandardMaterial({
color: 0x3c9eae,
roughness: 0.27,
metalness: 0.12,
}),
waterLight: new THREE.MeshStandardMaterial({
color: 0xa9e0df,
transparent: true,
opacity: 0.55,
roughness: 0.5,
}),
rail: new THREE.MeshStandardMaterial({
color: 0x4a4140,
roughness: 0.62,
metalness: 0.48,
}),
railAccent: new THREE.MeshStandardMaterial({
color: 0xe66b3d,
roughness: 0.58,
metalness: 0.18,
}),
railGlow: new THREE.MeshStandardMaterial({
color: 0xff9b54,
emissive: 0xd85d28,
emissiveIntensity: 0.35,
}),
lamp: new THREE.MeshStandardMaterial({
color: 0xffdb8d,
emissive: 0xffb842,
emissiveIntensity: 0.85,
}),
routeWalk: new THREE.MeshStandardMaterial({
color: 0x63e4db,
emissive: 0x1b8f91,
emissiveIntensity: 0.52,
roughness: 0.5,
}),
routeFun: new THREE.MeshStandardMaterial({
color: 0xff9b54,
emissive: 0xca4e2a,
emissiveIntensity: 0.48,
roughness: 0.5,
}),
routeRide: new THREE.MeshStandardMaterial({
color: 0xffbd68,
emissive: 0xe55d2e,
emissiveIntensity: 0.58,
roughness: 0.45,
}),
white: new THREE.MeshStandardMaterial({ color: 0xf7f2df, roughness: 0.6 }),
person: new THREE.MeshStandardMaterial({ color: 0x263b42, roughness: 0.7 }),
};
function box(
parent,
width,
height,
depth,
x,
y,
z,
mat,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function barBetween(parent, a, b, width, height, mat) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(length, height, width),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function groundHeight(x) {
if (x < 2) return 0;
if (x < 4) return 1.5;
if (x < 6) return 3;
if (x < 8) return 4.5;
return 6;
}
let seed = 8127;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
function createBuilding(
x,
z,
width,
depth,
height,
wallMaterial,
roofMaterial = material.roof
) {
const base = groundHeight(x);
const group = new THREE.Group();
group.position.set(x, base, z);
scene.add(group);
box(group, width, height, depth, 0, height / 2, 0, wallMaterial);
box(
group,
width + 0.28,
0.28,
depth + 0.28,
0,
height + 0.14,
0,
roofMaterial
);
box(
group,
width * 0.58,
0.48,
depth * 0.58,
0,
height + 0.5,
0,
roofMaterial
);
const floors = Math.max(2, Math.floor((height - 0.8) / 1.55));
const frontColumns = Math.max(2, Math.floor(width / 1.35));
for (let floor = 0; floor < floors; floor++) {
const y = 1.2 + floor * 1.55;
if (y > height - 0.35) continue;
for (let column = 0; column < frontColumns; column++) {
const wx =
-width / 2 +
0.75 +
column * ((width - 1.5) / Math.max(1, frontColumns - 1));
const windowMat = random() > 0.72 ? material.windowLit : material.window;
box(
group,
0.48,
0.68,
0.08,
wx,
y,
depth / 2 + 0.045,
windowMat,
false,
false
);
}
for (let column = 0; column < Math.max(2, Math.floor(depth / 1.7)); column++) {
const wz =
-depth / 2 +
0.8 +
column * ((depth - 1.6) / Math.max(1, Math.floor(depth / 1.7) - 1));
const windowMat = random() > 0.76 ? material.windowLit : material.window;
box(
group,
0.08,
0.68,
0.48,
width / 2 + 0.045,
y,
wz,
windowMat,
false,
false
);
}
}
box(
group,
0.86,
1.42,
0.12,
0,
0.71,
depth / 2 + 0.08,
material.darkWood,
false,
false
);
if (width > 3.8) {
box(
group,
0.45,
1.15,
0.45,
width * 0.26,
height + 0.95,
-depth * 0.2,
material.brickDark
);
}
return group;
}
function createTree(x, z, base = groundHeight(x), size = 1) {
box(scene, 0.3 * size, 1.35 * size, 0.3 * size,
x, base + 0.67 * size, z, material.trunk);
box(scene, 1.45 * size, 1.1 * size, 1.35 * size,
x, base + 1.55 * size, z, material.leaves);
box(scene, 0.95 * size, 0.9 * size, 0.9 * size,
x + 0.15 * size, base + 2.25 * size, z - 0.05, material.leavesLight);
}
function createLamp(x, z, base = groundHeight(x)) {
box(scene, 0.12, 1.75, 0.12, x, base + 0.87, z, material.rail);
box(scene, 0.38, 0.38, 0.38, x, base + 1.9, z, material.lamp);
box(scene, 0.58, 0.12, 0.58, x, base + 2.15, z, material.rail);
}
function createPerson(x, y, z, colorMaterial = material.person) {
box(scene, 0.22, 0.45, 0.22, x, y + 0.43, z, colorMaterial);
box(scene, 0.22, 0.22, 0.22, x, y + 0.78, z, material.white);
}
// The city is built entirely from box geometry.
box(scene, 43, 1.2, 39, 0, -1.55, 0, material.stone, false, true);
box(scene, 8, 0.75, 36, -16, -0.55, 0, material.water, false, true);
box(scene, 0.55, 1.05, 36, -11.75, -0.3, 0, material.darkWood);
box(scene, 14, 2, 32, -5, -1, 0, material.lowerStone, false, true);
box(scene, 2, 3.5, 27, 3, -0.25, -1, material.stone, false, true);
box(scene, 2, 5, 27, 5, 0.5, -1, material.stone, false, true);
box(scene, 2, 6.5, 27, 7, 1.25, -1, material.stone, false, true);
box(scene, 12, 8, 32, 14, 2, 0, material.upperStone, false, true);
// Main pedestrian surfaces.
box(scene, 3.7, 0.07, 26, -4.8, 0.04, 0.4, material.stone, false, true);
box(scene, 1.8, 0.07, 22, -9.5, 0.04, 3, material.stone, false, true);
box(scene, 5.3, 0.07, 3.5, 12.5, 6.04, -6.3, material.stone, false, true);
box(scene, 2.4, 0.07, 13, 10.1, 6.04, -2, material.stone, false, true);
// Stepped retaining edges.
for (const x of [1.85, 3.85, 5.85, 7.85]) {
for (let i = 0; i < 4; i++) {
box(
scene,
0.34,
0.28,
5.8,
x + i * 0.27,
groundHeight(x) + 0.15 + i * 0.15,
1.4,
material.wall,
false,
true
);
}
}
// Historic lower district.
createBuilding(-8.7, 3.6, 4.1, 5.1, 7.2, material.brick);
createBuilding(-8.7, -4.4, 4.2, 5.8, 8.2, material.creamWall, material.roofRed);
createBuilding(-0.7, 4.8, 4.0, 4.7, 6.4, material.tealWall);
createBuilding(-0.7, -3.7, 4.1, 5.8, 8.1, material.brickDark);
createBuilding(-8.8, -10.1, 4.2, 4.4, 6.5, material.blueWall);
// Upper district.
createBuilding(10.8, 4.1, 4.4, 5.0, 7.1, material.creamWall, material.roofRed);
createBuilding(15.2, 3.1, 4.6, 5.3, 8.2, material.brick);
createBuilding(10.8, -0.6, 4.0, 4.4, 6.3, material.tealWall);
createBuilding(15.1, -2.7, 4.1, 4.7, 7.6, material.wall);
createBuilding(16.5, -13.0, 5.2, 4.2, 9.8, material.wall, material.roof);
// Château roof tiers.
box(scene, 5.8, 0.38, 4.8, 16.5, 16.12, -13, material.roofRed);
box(scene, 4.3, 0.55, 3.6, 16.5, 16.55, -13, material.roofRed);
box(scene, 2.6, 0.75, 2.1, 16.5, 17.05, -13, material.roofRed);
for (const x of [15.25, 17.75]) {
box(scene, 0.8, 2.1, 0.8, x, 17.75, -13, material.wall);
}
// Église Saint-Louis.
createBuilding(5, -1.5, 3.1, 4.3, 6.0, material.wall, material.roof);
box(scene, 1.75, 9.2, 1.75, 5, 7.6, -2.2, material.wall);
box(scene, 2.05, 0.35, 2.05, 5, 12.37, -2.2, material.roof);
box(scene, 1.25, 1.05, 1.25, 5, 13.05, -2.2, material.roof);
box(scene, 0.16, 2.1, 0.16, 5, 14.45, -2.2, material.rail);
box(scene, 0.8, 0.16, 0.16, 5, 15.2, -2.2, material.rail);
// Market kiosks around the walking origin.
for (const [x, z, color] of [
[-11.2, 6.2, material.brick],
[-8.8, 11.0, material.tealWall],
[-6.7, 11.4, material.creamWall],
]) {
box(scene, 1.7, 1.25, 1.4, x, 0.64, z, color);
box(scene, 2.0, 0.2, 1.65, x, 1.36, z, material.roofRed);
box(scene, 1.1, 0.55, 0.06, x, 0.9, z + 0.73, material.window);
}
// Dufferin lookout deck.
box(scene, 5.3, 0.18, 2.8, 12, 6.12, -8, material.wood);
for (let i = 0; i < 7; i++) {
box(scene, 0.12, 0.75, 0.12, 9.45 + i * 0.85, 6.58, -9.25, material.darkWood);
}
box(scene, 5.35, 0.12, 0.12, 12, 6.96, -9.25, material.darkWood);
// Trees, lamps and pedestrians.
[
[-11.2, 0.5], [-11.1, -7.8], [-2.0, -10.4],
[1.1, 8.1], [8.8, 9.0], [18.1, 7.1],
[18.1, -6.4], [13.5, -12.4], [8.9, -11.2],
[7.4, 2.6]
].forEach(([x, z], index) => {
createTree(x, z, groundHeight(x), 0.8 + (index % 3) * 0.12);
});
[
[-11.2, 10.6], [-4.5, 10.7], [-4.3, -7.1],
[9.2, 8.5], [12.2, 0.7], [18.4, -5.8],
[8.7, -8.2], [4.8, 2.4]
].forEach(([x, z]) => createLamp(x, z));
[
[-9.9, 0.25, 8.6], [-5.5, 0.25, 7.8], [-3.4, 0.25, 4.6],
[1.4, 0.25, 2.7], [3.1, 1.75, 1.2], [5.4, 3.35, 0.5],
[9.4, 6.35, -4.7], [11.8, 6.35, -7.4],
[15.8, 6.35, -9.2], [13.4, 6.35, 1.1]
].forEach(([x, y, z]) => createPerson(x, y, z));
// Water shimmer blocks.
const waterBits = [];
for (let i = 0; i < 26; i++) {
const bit = box(
scene,
0.8 + random() * 2.2,
0.025,
0.12,
-19.3 + random() * 6.5,
-0.16,
-17 + random() * 34,
material.waterLight,
false,
false
);
bit.userData.speed = 0.25 + random() * 0.35;
waterBits.push(bit);
}
// Small voxel boats.
for (const [x, z, color] of [
[-16.3, -6, material.brickRed ?? material.brick],
[-14.7, 7.5, material.wall],
]) {
const boat = new THREE.Group();
boat.position.set(x, 0, z);
scene.add(boat);
box(boat, 1.15, 0.42, 2.6, 0, 0, 0, color);
box(boat, 0.8, 0.55, 0.9, 0, 0.48, -0.2, material.white);
box(boat, 0.09, 2.7, 0.09, 0, 1.5, 0, material.wood);
}
const lowerStation = [-6, 0.35, 9];
const upperStation = [8, 6.35, -5];
function buildFunicular() {
const group = new THREE.Group();
scene.add(group);
const a = new THREE.Vector3(...lowerStation);
const b = new THREE.Vector3(...upperStation);
const direction = b.clone().sub(a).normalize();
const perpendicular = new THREE.Vector3(direction.z, 0, -direction.x).normalize();
for (const offset of [-0.43, 0.43]) {
const offsetVector = perpendicular.clone().multiplyScalar(offset);
barBetween(
group,
a.clone().add(offsetVector).toArray(),
b.clone().add(offsetVector).toArray(),
0.09,
0.11,
material.rail
);
}
const total = a.distanceTo(b);
const sleepers = Math.floor(total / 0.65);
for (let i = 0; i <= sleepers; i++) {
const t = i / sleepers;
const p = a.clone().lerp(b, t);
const sleeper = box(group, 0.18, 0.1, 1.35, p.x, p.y, p.z, material.railAccent);
sleeper.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
direction
);
}
for (let i = 0; i <= 8; i++) {
const t = i / 8;
const p = a.clone().lerp(b, t);
const ground = groundHeight(p.x);
const height = Math.max(0.25, p.y - ground);
box(group, 0.22, height, 0.22, p.x, ground + height / 2, p.z, material.darkWood);
}
// Station shelters.
for (const station of [a, b]) {
const shelter = new THREE.Group();
shelter.position.copy(station);
group.add(shelter);
const stationY = station.y - 0.2;
box(shelter, 2.7, 0.18, 2.25, 0, stationY, 0, material.darkWood);
for (const dx of [-1.1, 1.1]) {
box(shelter, 0.14, 1.8, 0.14, dx, stationY + 0.9, -0.8, material.rail);
}
box(shelter, 2.7, 0.22, 1.9, 0, stationY + 1.85, 0, material.roofRed);
box(shelter, 1.5, 0.6, 0.08, 0, stationY + 1.15, 0.92, material.windowLit);
}
}
buildFunicular();
const funicularCar = new THREE.Group();
scene.add(funicularCar);
box(funicularCar, 2.5, 1.15, 1.25, 0, 0, 0, material.brickDark);
box(funicularCar, 2.35, 0.85, 1.31, 0, 0.1, 0, material.railAccent);
box(funicularCar, 1.85, 0.48, 1.35, 0, 0.32, 0, material.window);
box(funicularCar, 2.7, 0.16, 1.42, 0, 0.76, 0, material.roof);
const destinationMarker = new THREE.Group();
scene.add(destinationMarker);
for (const x of [-0.35, 0.35]) {
for (const z of [-0.35, 0.35]) {
box(
destinationMarker,
0.13,
0.55,
0.13,
x,
0.28,
z,
material.routeFun,
false,
false
);
}
}
const traveler = new THREE.Group();
scene.add(traveler);
box(traveler, 0.3, 0.48, 0.3, 0, 0.38, 0, material.white);
box(traveler, 0.26, 0.26, 0.26, 0, 0.75, 0, material.routeWalk);
const routeGroup = new THREE.Group();
scene.add(routeGroup);
const A = [-10, 0.35, 9];
const B = [-6, 0.35, 9];
const C = [8, 6.35, -5];
const E = [5, 3.35, 1.2];
const D = [12, 6.35, -8];
const F = [17, 6.35, -9.5];
const destinations = {
church: {
name: "Église Saint-Louis",
point: E,
walk: [
[A, [-6, 0.35, 9], [-3, 0.35, 5], [1.5, 0.35, 3],
[2.5, 1.85, 2], [4.5, 3.35, 0], E],
],
fun: [
{ type: "walk", points: [A, B] },
{ type: "ride", points: [B, C] },
{
type: "walk",
points: [C, [7.4, 6.35, -3], [6.4, 4.85, -1], E],
},
],
},
terrace: {
name: "Dufferin Terrace",
point: D,
walk: [
[A, [-6, 0.35, 9], [-3, 0.35, 5], [1.5, 0.35, 3],
[2.5, 1.85, 2], [4.5, 3.35, 0], [6.5, 4.85, -2],
[8.5, 6.35, -4], D],
],
fun: [
{ type: "walk", points: [A, B] },
{ type: "ride", points: [B, C] },
{ type: "walk", points: [C, [10, 6.35, -6], D] },
],
},
castle: {
name: "Château Frontenac",
point: F,
walk: [
[A, [-6, 0.35, 9], [-3, 0.35, 5], [1.5, 0.35, 3],
[2.5, 1.85, 2], [4.5, 3.35, 0], [6.5, 4.85, -2],
[8.5, 6.35, -4], D, [14.3, 6.35, -8], F],
],
fun: [
{ type: "walk", points: [A, B] },
{ type: "ride", points: [B, C] },
{
type: "walk",
points: [C, [10, 6.35, -6], [13.5, 6.35, -8], F],
},
],
},
};
let mode = "walk";
let currentItinerary = [];
let currentStats = null;
let currentWait = 0;
let travelerElapsed = 0;
function vector(point) {
return new THREE.Vector3(...point);
}
function itineraryStats(itinerary) {
let distance = 0;
let gain = 0;
for (const segment of itinerary) {
for (let i = 1; i < segment.points.length; i++) {
const a = vector(segment.points[i - 1]);
const b = vector(segment.points[i]);
distance += a.distanceTo(b);
gain += Math.max(0, b.y - a.y);
}
}
return { distance, gain };
}
function walkingItinerary(destination) {
return destination.walk.map((points) => ({ type: "walk", points }));
}
function clearRoute() {
for (const child of [...routeGroup.children]) {
child.geometry?.dispose();
routeGroup.remove(child);
}
}
function drawRoute(itinerary) {
clearRoute();
for (const segment of itinerary) {
const mat =
segment.type === "ride"
? material.routeRide
: mode === "walk"
? material.routeWalk
: material.routeFun;
for (let i = 1; i < segment.points.length; i++) {
const a = vector(segment.points[i - 1]);
const b = vector(segment.points[i]);
const direction = b.clone().sub(a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.12, segment.type === "ride" ? 0.28 : 0.34),
mat
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
direction.normalize()
);
routeGroup.add(mesh);
}
for (const point of segment.points) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.28, 0.14, 0.28),
mat
);
marker.position.copy(vector(point));
routeGroup.add(marker);
}
}
}
const departures = [];
for (let minutes = 9 * 60; minutes <= 18 * 60 + 40; minutes += 10) {
departures.push(minutes);
}
function minutesFromTime(value) {
const [hours, minutes] = value.split(":").map(Number);
return hours * 60 + minutes;
}
function timeFromMinutes(value) {
const normalized = ((value % 1440) + 1440) % 1440;
const hours = Math.floor(normalized / 60);
const minutes = normalized % 60;
return `${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
}
function getWaitInfo() {
const current = minutesFromTime(arrivalInput.value || "09:07");
let index = departures.findIndex((departure) => departure >= current);
let nextDay = false;
if (index === -1) {
index = 0;
nextDay = true;
}
const departure = departures[index];
const wait = nextDay
? 1440 - current + departure
: departure - current;
return { index, departure, wait, nextDay };
}
function formatWait(wait) {
if (wait === 0) return "0 <small>min</small>";
if (wait >= 60) {
const hours = Math.floor(wait / 60);
const minutes = wait % 60;
return `${hours}<small>h</small> ${minutes}<small>m</small>`;
}
return `${wait} <small>min</small>`;
}
function renderSchedule(waitInfo) {
scheduleElement.replaceChildren();
let start = Math.max(0, waitInfo.index - 1);
if (waitInfo.index >= departures.length - 6) {
start = Math.max(0, departures.length - 6);
}
for (let i = start; i < Math.min(departures.length, start + 7); i++) {
const departure = departures[i];
const button = document.createElement("button");
button.type = "button";
button.textContent = timeFromMinutes(departure);
if (i === waitInfo.index) {
button.classList.add("next");
button.title = "Next scheduled departure";
}
button.addEventListener("click", () => {
arrivalInput.value =
departure === 9 * 60
? "09:00"
: timeFromMinutes(Math.max(9 * 60, departure - 5));
renderUI();
});
scheduleElement.append(button);
}
}
function segmentDistance(segment) {
let distance = 0;
for (let i = 1; i < segment.points.length; i++) {
distance += vector(segment.points[i - 1]).distanceTo(
vector(segment.points[i])
);
}
return distance;
}
function renderUI() {
const destination = destinations[destinationSelect.value];
const walkItinerary = walkingItinerary(destination);
const funItinerary = destination.fun;
currentItinerary = mode === "walk" ? walkItinerary : funItinerary;
currentStats = itineraryStats(currentItinerary);
currentWait = mode === "fun" ? getWaitInfo().wait : 0;
metricLength.innerHTML = `${currentStats.distance.toFixed(1)} <small>m</small>`;
metricElevation.innerHTML = `${currentStats.gain.toFixed(1)} <small>m</small>`;
metricWait.innerHTML = formatWait(currentWait);
if (mode === "walk") {
routeTitle.textContent = "Walk the stepped lanes";
routeDescription.textContent =
"Follow the lower promenade, climb the stair terraces, and continue along the upper street.";
} else {
const access = segmentDistance(funItinerary[0]);
const ride = segmentDistance(funItinerary[1]);
const exit = segmentDistance(funItinerary[2]);
const waitInfo = getWaitInfo();
routeTitle.textContent =
waitInfo.nextDay
? "Ride the first service tomorrow"
: `Next departure: ${timeFromMinutes(waitInfo.departure)}`;
routeDescription.textContent =
`Walk ${access.toFixed(1)} m to the lower stop, ride ${ride.toFixed(1)} m ` +
`up the incline, then walk ${exit.toFixed(1)} m to ${destination.name}. ` +
`The fictional ride lasts 3 minutes.`;
}
const walkStats = itineraryStats(walkItinerary);
const funStats = itineraryStats(funItinerary);
const comparisonText =
`Walking: ${walkStats.distance.toFixed(1)} m · ` +
`Funicular: ${funStats.distance.toFixed(1)} m`;
comparison.textContent = comparisonText;
modeButtons.forEach((button) => {
const active = button.dataset.mode === mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
renderSchedule(getWaitInfo());
drawRoute(currentItinerary);
destinationMarker.position.copy(vector(destination.point));
const markerMaterial = mode === "walk" ? material.routeWalk : material.routeFun;
destinationMarker.children.forEach((child) => {
child.material = markerMaterial;
});
traveler.children[1].material =
mode === "walk" ? material.routeWalk : material.routeFun;
travelerElapsed = 0;
}
modeButtons.forEach((button) => {
button.addEventListener("click", () => {
mode = button.dataset.mode;
renderUI();
});
});
destinationSelect.addEventListener("change", renderUI);
arrivalInput.addEventListener("input", renderUI);
// HTML labels projected from the 3D scene.
const labelData = [
{ text: "Marché du Petit-Champlain", position: [-10, 2.2, 9] },
{ text: "Église Saint-Louis", position: [5, 12.8, -2.2] },
{ text: "Dufferin Terrace", position: [12, 7.5, -8] },
{ text: "Château Frontenac", position: [16.5, 17.6, -13] },
{ text: "Lower stop", position: [-6, 2.1, 9], station: true },
{ text: "Upper stop", position: [8, 8, -5], station: true },
];
const labels = labelData.map((data) => {
const element = document.createElement("div");
element.className = `map-label${data.station ? " station" : ""}`;
element.textContent = data.text;
labelLayer.append(element);
return {
element,
position: new THREE.Vector3(...data.position),
};
});
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const projected = label.position.clone().project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
projected.x > -1.1 &&
projected.x < 1.1 &&
projected.y > -1.1 &&
projected.y < 1.1;
label.element.style.display = visible ? "flex" : "none";
if (visible) {
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
function updateCamera() {
const radius = 53;
const horizontal = Math.cos(cameraElevation) * radius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraElevation) * radius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight);
const aspect = window.innerWidth / window.innerHeight;
const viewSize = 34;
camera.left = (-viewSize * aspect) / 2;
camera.right = (viewSize * aspect) / 2;
camera.top = viewSize / 2;
camera.bottom = -viewSize / 2;
camera.updateProjectionMatrix();
}
function resetView() {
cameraYaw = 0.72;
cameraElevation = 0.68;
cameraZoom = 1;
cameraTarget.set(0, 1.5, 0);
updateCamera();
}
document.querySelector("#reset-view").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraYaw -= dx * 0.006;
cameraElevation = THREE.MathUtils.clamp(
cameraElevation + dy * 0.004,
0.34,
1.18
);
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.68,
2.1
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", resize);
function buildAnimationStages() {
const stages = [];
for (const segment of currentItinerary) {
if (segment.type === "ride" && mode === "fun" && stages.length === 1) {
stages.push({
type: "wait",
duration: Math.max(1.1, Math.min(currentWait, 8) * 0.48),
position: vector(segment.points[0]),
});
}
stages.push({
type: segment.type,
duration:
segment.type === "ride"
? 3
: Math.max(0.7, segmentDistance(segment) * 0.105),
points: segment.points.map(vector),
});
}
return stages;
}
function updateTraveler(delta) {
if (!currentItinerary.length) return;
travelerElapsed += delta;
const stages = buildAnimationStages();
const total = stages.reduce((sum, stage) => sum + stage.duration, 0);
let localTime = travelerElapsed % total;
for (const stage of stages) {
if (localTime <= stage.duration || stage === stages[stages.length - 1]) {
if (stage.type === "wait") {
traveler.position.copy(stage.position);
traveler.position.y += 0.08 + Math.sin(travelerElapsed * 5) * 0.06;
} else {
const segmentTime = Math.min(
1,
Math.max(0, localTime / stage.duration)
);
const pointCount = stage.points.length - 1;
const scaled = segmentTime * pointCount;
const segmentIndex = Math.min(
pointCount - 1,
Math.floor(scaled)
);
const segmentProgress = scaled - segmentIndex;
const a = stage.points[segmentIndex];
const b = stage.points[segmentIndex + 1];
traveler.position.copy(a).lerp(b, segmentProgress);
traveler.position.y += 0.06 + Math.sin(travelerElapsed * 8) * 0.025;
}
return;
}
localTime -= stage.duration;
}
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateTraveler(delta);
// Funicular car moves continuously along the fixed track.
let carProgress = (time * 0.035) % 2;
if (carProgress > 1) carProgress = 2 - carProgress;
const carPosition = vector(lowerStation).lerp(
vector(upperStation),
carProgress
);
funicularCar.position.copy(carPosition);
funicularCar.position.y += 0.64;
const carDirection = vector(upperStation)
.sub(vector(lowerStation))
.normalize()
.multiplyScalar(carProgress < 0.5 ? 1 : -1);
funicularCar.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
carDirection
);
destinationMarker.rotation.y = time * 0.75;
destinationMarker.position.y =
vector(destinations[destinationSelect.value].point).y +
0.05 +
Math.sin(time * 2.6) * 0.08;
for (const bit of waterBits) {
bit.position.z += delta * bit.userData.speed;
if (bit.position.z > 17.5) bit.position.z = -17.5;
}
updateLabels();
renderer.render(scene, camera);
}
resize();
updateCamera();
renderUI();
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```