Prompt specification
Build or customize the Three.js 3D scene "Compare direct and shopping-loop walking 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 direct and shopping-loop walking routes
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-direct-and-shopping-loop-walking-routes-x2760
Scene Description Prompt:
Compare direct and shopping-loop walks through voxel Montevideo Ciudad Vieja to the same destination. Build one HTML scene using Three.js from cdnjs.
Working Three.js Starter (compare-direct-and-shopping-loop-walking-routes-x2760):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ciudad Vieja — Two Ways to the Same Place</title>
<style>
:root {
--ink: #17353b;
--muted: #587177;
--paper: rgba(255, 253, 247, 0.91);
--line: rgba(23, 53, 59, 0.14);
--direct: #12aebe;
--direct-light: #d8f7f8;
--loop: #f26a4f;
--loop-light: #ffe6df;
--gold: #eebd57;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b7d0d2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
#app {
position: fixed;
inset: 0;
}
#app::after {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 48%, rgba(19, 52, 58, 0.08) 100%);
z-index: 2;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: absolute;
z-index: 10;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow:
0 18px 50px rgba(25, 57, 61, 0.13),
0 2px 8px rgba(25, 57, 61, 0.08);
backdrop-filter: blur(16px) saturate(1.15);
-webkit-backdrop-filter: blur(16px) saturate(1.15);
}
.hero {
top: 18px;
left: 18px;
width: min(405px, calc(100vw - 36px));
padding: 20px 22px 18px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: var(--loop);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 19px;
height: 2px;
background: var(--loop);
}
h1 {
margin: 0;
max-width: 340px;
font-size: clamp(25px, 3vw, 37px);
line-height: 0.99;
letter-spacing: -0.055em;
font-weight: 850;
}
.hero p {
margin: 12px 0 0;
max-width: 345px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.hero-tags {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 14px;
}
.hero-tags span {
padding: 5px 8px;
border: 1px solid var(--line);
border-radius: 999px;
color: #476267;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metrics {
top: 18px;
right: 18px;
width: min(326px, calc(100vw - 36px));
padding: 17px;
border-radius: 18px;
}
.metrics-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.metrics-title {
font-size: 11px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.same-end {
padding: 5px 7px;
border-radius: 7px;
background: #eef1eb;
color: #627064;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
#comparison-summary {
margin: -3px 0 13px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.route-card {
margin-top: 9px;
padding: 12px 12px 10px;
border: 1px solid var(--line);
border-left-width: 4px;
border-radius: 12px;
background: rgba(255, 255, 255, 0.56);
transition: opacity 180ms ease, transform 180ms ease;
}
.route-card.dimmed {
opacity: 0.38;
transform: scale(0.985);
}
#direct-card { border-left-color: var(--direct); }
#loop-card { border-left-color: var(--loop); }
.route-head {
display: flex;
align-items: center;
gap: 8px;
}
.route-head strong {
font-size: 12px;
letter-spacing: -0.01em;
}
.route-head .status {
margin-left: auto;
color: var(--muted);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.75);
}
.dot.direct { background: var(--direct); }
.dot.loop { background: var(--loop); }
.route-stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 10px;
}
.route-stats div {
display: flex;
flex-direction: column;
gap: 1px;
}
.route-stats b {
font-size: 18px;
line-height: 1;
letter-spacing: -0.045em;
}
.route-stats span {
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.route-desc {
margin: 8px 0 7px;
color: var(--muted);
font-size: 9px;
}
.meter {
height: 3px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 53, 59, 0.09);
}
.meter i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 80ms linear;
}
#direct-meter { background: var(--direct); }
#loop-meter { background: var(--loop); }
.controls {
left: 18px;
bottom: 103px;
display: flex;
align-items: center;
gap: 14px;
padding: 10px 12px;
border-radius: 14px;
}
.control-group {
display: flex;
align-items: center;
gap: 4px;
}
.control-label {
margin-right: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.control-divider {
width: 1px;
height: 25px;
background: var(--line);
}
.small-button {
min-width: 31px;
height: 29px;
padding: 0 9px;
border: 1px solid transparent;
border-radius: 8px;
background: transparent;
color: #456066;
cursor: pointer;
font-size: 9px;
font-weight: 800;
transition: background 140ms ease, color 140ms ease;
}
.small-button:hover {
background: rgba(23, 53, 59, 0.07);
}
.small-button.selected {
border-color: rgba(23, 53, 59, 0.12);
background: #eef0eb;
color: var(--ink);
}
#labels-toggle.selected {
background: var(--direct-light);
color: #087d88;
}
.timeline {
left: 50%;
bottom: 18px;
width: min(680px, calc(100vw - 36px));
transform: translateX(-50%);
display: grid;
grid-template-columns: 43px 62px 1fr;
align-items: center;
gap: 12px;
padding: 12px 16px 10px;
border-radius: 17px;
}
#play {
width: 40px;
height: 40px;
border: 0;
border-radius: 12px;
background: var(--ink);
color: white;
cursor: pointer;
font-size: 15px;
box-shadow: 0 5px 13px rgba(23, 53, 59, 0.18);
}
#play:hover { background: #244b52; }
.time-readout {
display: flex;
flex-direction: column;
gap: 1px;
}
#current-time {
font-size: 17px;
font-weight: 850;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.time-readout span:last-child {
color: var(--muted);
font-size: 8px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.scrubber {
position: relative;
min-width: 0;
}
input[type="range"] {
--p: 0%;
display: block;
width: 100%;
height: 5px;
margin: 8px 0 8px;
border-radius: 999px;
outline: none;
cursor: pointer;
appearance: none;
background:
linear-gradient(to right, var(--ink) 0 var(--p), rgba(23, 53, 59, 0.12) var(--p) 100%);
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid white;
border-radius: 50%;
appearance: none;
background: var(--ink);
box-shadow: 0 2px 6px rgba(23, 53, 59, 0.26);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid white;
border-radius: 50%;
background: var(--ink);
box-shadow: 0 2px 6px rgba(23, 53, 59, 0.26);
}
#ticks {
position: relative;
height: 12px;
color: #758589;
font-size: 7px;
font-weight: 750;
}
#ticks span {
position: absolute;
transform: translateX(-50%);
font-variant-numeric: tabular-nums;
}
.hint {
right: 18px;
bottom: 105px;
max-width: 205px;
padding: 10px 12px;
border-radius: 12px;
background: rgba(255, 253, 247, 0.75);
color: #5d7277;
font-size: 9px;
line-height: 1.5;
backdrop-filter: blur(8px);
}
.hint b {
color: var(--ink);
font-weight: 800;
}
#labels {
position: absolute;
inset: 0;
z-index: 4;
pointer-events: none;
transition: opacity 180ms ease;
}
#labels.hidden { opacity: 0; }
.map-label {
position: absolute;
transform: translate(-50%, -100%);
white-space: nowrap;
pointer-events: none;
user-select: none;
}
.map-label.place {
padding: 5px 7px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 7px;
background: rgba(255, 253, 247, 0.86);
color: #29474c;
box-shadow: 0 4px 10px rgba(23, 53, 59, 0.11);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.map-label.poi {
padding: 4px 6px;
border-radius: 6px;
background: rgba(23, 53, 59, 0.82);
color: white;
font-size: 7px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.map-label.street {
color: rgba(40, 69, 73, 0.66);
font-size: 6px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
text-shadow: 0 1px rgba(255, 255, 255, 0.7);
}
.map-label.route-tag {
padding: 5px 8px;
border-radius: 999px;
box-shadow: 0 4px 10px rgba(23, 53, 59, 0.11);
font-size: 7px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.direct-tag {
background: var(--direct-light);
color: #087d88;
}
.loop-tag {
background: var(--loop-light);
color: #b94934;
}
.map-label.shop-label {
width: 18px;
height: 18px;
display: grid;
place-items: center;
transform: translate(-50%, -50%);
border: 2px solid white;
border-radius: 50%;
background: var(--loop);
color: white;
box-shadow: 0 4px 9px rgba(172, 62, 40, 0.28);
font-size: 7px;
font-weight: 900;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #b7d0d2;
color: var(--ink);
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loading-inner {
text-align: center;
}
.loading-cube {
width: 29px;
height: 29px;
margin: 0 auto 15px;
border: 2px solid var(--ink);
border-top-color: var(--loop);
animation: cube-spin 900ms ease-in-out infinite alternate;
}
.loading-inner strong {
display: block;
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.loading-inner span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
}
@keyframes cube-spin {
to { transform: rotate(45deg) scale(0.78); }
}
@media (max-width: 760px) {
.hero {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 17px;
}
.hero p, .hero-tags { display: none; }
h1 { max-width: 270px; font-size: 25px; }
.metrics {
top: 112px;
right: 12px;
width: calc(100vw - 24px);
padding: 12px;
}
.metrics-head { margin-bottom: 7px; }
#comparison-summary { margin-bottom: 8px; }
.route-card { padding: 9px 10px 8px; }
.route-stats { margin-top: 7px; }
.route-stats b { font-size: 15px; }
.route-desc { margin: 5px 0; }
.controls {
left: 12px;
bottom: 91px;
gap: 7px;
padding: 7px;
}
.control-label { display: none; }
.control-divider { margin: 0 2px; }
.small-button { padding: 0 7px; }
.timeline {
bottom: 12px;
width: calc(100vw - 24px);
grid-template-columns: 39px 52px 1fr;
gap: 8px;
padding: 10px 11px 8px;
}
.hint { display: none; }
}
</style>
</head>
<body>
<div id="app">
<div id="labels"></div>
<section class="hero panel">
<div class="eyebrow">Montevideo · Walking study</div>
<h1>Two ways to the same place.</h1>
<p>
A voxel model of Ciudad Vieja compares the shortest street walk with a
browsing loop through four shop stops—both beginning at Plaza
Constitución and ending at Mercado del Puerto.
</p>
<div class="hero-tags">
<span>Ciudad Vieja</span>
<span>Same destination</span>
<span>Interactive city model</span>
</div>
</section>
<aside class="metrics panel">
<div class="metrics-head">
<div class="metrics-title">Route comparison</div>
<div class="same-end">Same endpoint</div>
</div>
<div id="comparison-summary">Calculating route distances…</div>
<article class="route-card" id="direct-card">
<div class="route-head">
<span class="dot direct"></span>
<strong>Direct</strong>
<span class="status" id="direct-status">Ready</span>
</div>
<div class="route-stats">
<div><b id="direct-distance">—</b><span>distance</span></div>
<div><b id="direct-time">—</b><span>walk time</span></div>
</div>
<p class="route-desc" id="direct-desc">Shortest street path</p>
<div class="meter"><i id="direct-meter"></i></div>
</article>
<article class="route-card" id="loop-card">
<div class="route-head">
<span class="dot loop"></span>
<strong>Shopping loop</strong>
<span class="status" id="loop-status">Ready</span>
</div>
<div class="route-stats">
<div><b id="loop-distance">—</b><span>distance</span></div>
<div><b id="loop-time">—</b><span>walk + browse</span></div>
</div>
<p class="route-desc" id="loop-desc">Four browsing stops</p>
<div class="meter"><i id="loop-meter"></i></div>
</article>
</aside>
<nav class="controls panel" aria-label="Scene controls">
<div class="control-group">
<span class="control-label">Routes</span>
<button class="small-button selected" data-mode="both">Both</button>
<button class="small-button" data-mode="direct">Direct</button>
<button class="small-button" data-mode="loop">Loop</button>
</div>
<div class="control-divider"></div>
<div class="control-group">
<span class="control-label">View</span>
<button class="small-button selected" data-view="overview">Map</button>
<button class="small-button" data-view="street">Street</button>
<button class="small-button" data-view="market">Market</button>
</div>
<div class="control-divider"></div>
<button class="small-button selected" id="labels-toggle" aria-pressed="true">
Labels
</button>
</nav>
<section class="timeline panel" aria-label="Walking timeline">
<button id="play" aria-label="Play comparison" aria-pressed="false">▶</button>
<div class="time-readout">
<span id="current-time">00:00</span>
<span>Simulated time</span>
</div>
<div class="scrubber">
<input
id="timeline"
type="range"
min="0"
max="28"
value="0"
step="0.05"
aria-label="Simulated walking time"
>
<div id="ticks"></div>
</div>
</section>
<div class="hint">
<b>Drag</b> to orbit · <b>Scroll</b> to zoom ·<br>
<b>Right-drag</b> to pan · <b>Space</b> to play
</div>
<div id="loading">
<div class="loading-inner">
<div class="loading-cube"></div>
<strong>Building Ciudad Vieja</strong>
<span>Arranging streets, blocks, and two ways through</span>
</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const app = document.getElementById("app");
const labelLayer = document.getElementById("labels");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
app.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb7d0d2);
scene.fog = new THREE.Fog(0xb7d0d2, 63, 105);
const camera = new THREE.OrthographicCamera(-20, 20, 20, -20, 0.1, 180);
const hemisphere = new THREE.HemisphereLight(0xe9f6f4, 0x786d5c, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.15);
sun.position.set(-18, 31, 19);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -29;
sun.shadow.camera.right = 29;
sun.shadow.camera.top = 29;
sun.shadow.camera.bottom = -29;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xb9e7ef, 0.8);
fillLight.position.set(20, 12, -20);
scene.add(fillLight);
// ---------------------------------------------------------------------
// Shared materials and helpers
// ---------------------------------------------------------------------
const materialCache = new Map();
function makeMaterial(color, options = {}) {
const key = `${color}-${JSON.stringify(options)}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0.02,
...options
})
);
}
return materialCache.get(key);
}
const materials = {
base: makeMaterial(0xd8cbb6),
ground: makeMaterial(0xded4c3),
sidewalk: makeMaterial(0xeee3ce),
road: makeMaterial(0x536568),
roadEdge: makeMaterial(0x718083),
water: makeMaterial(0x69a9b1, { roughness: 0.3, metalness: 0.1 }),
wave: new THREE.MeshBasicMaterial({
color: 0xc6edf0,
transparent: true,
opacity: 0.38,
depthWrite: false
}),
grass: makeMaterial(0x8eaa78),
grassLight: makeMaterial(0xa7bb82),
trunk: makeMaterial(0x775b45),
leaf: makeMaterial(0x66876a),
leafLight: makeMaterial(0x789873),
roof: makeMaterial(0xa75e50),
roofDark: makeMaterial(0x745d58),
cream: makeMaterial(0xe8d6b4),
marketRoof: makeMaterial(0xb75d4b),
marketAccent: makeMaterial(0x327b7c),
fountain: makeMaterial(0xc9c4b3),
fountainWater: makeMaterial(0x65adb6, { roughness: 0.25 }),
gold: makeMaterial(0xeebd57, {
emissive: 0x5b3900,
emissiveIntensity: 0.25
}),
direct: new THREE.MeshBasicMaterial({ color: 0x12aebe }),
loop: new THREE.MeshBasicMaterial({ color: 0xf26a4f }),
directGlow: new THREE.MeshBasicMaterial({
color: 0x12aebe,
transparent: true,
opacity: 0.2,
depthWrite: false
}),
loopGlow: new THREE.MeshBasicMaterial({
color: 0xf26a4f,
transparent: true,
opacity: 0.19,
depthWrite: false
})
};
function addBox(width, height, depth, x, y, z, material, parent = scene) {
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;
}
let randomSeed = 78123;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function randomRange(min, max) {
return min + random() * (max - min);
}
function pick(array) {
return array[Math.floor(random() * array.length)];
}
// ---------------------------------------------------------------------
// Base, streets, waterfront
// ---------------------------------------------------------------------
const worldWidth = 37;
const worldDepth = 34;
const worldCenterZ = 2;
addBox(
worldWidth,
0.72,
worldDepth,
0,
-0.38,
worldCenterZ,
materials.base
);
addBox(
worldWidth - 0.3,
0.08,
worldDepth - 0.3,
0,
-0.02,
worldCenterZ,
materials.ground
);
// Río de la Plata.
addBox(37, 0.18, 12, 0, -0.05, 25, materials.water);
addBox(37, 0.35, 0.65, 0, 0.04, 18.7, materials.sidewalk);
const verticalStreets = [-13, -8, -3, 2, 7, 12];
const horizontalStreets = [-10, -5, 0, 5, 10, 15];
// Sidewalks first, then road surfaces.
for (const z of horizontalStreets) {
addBox(35, 0.08, 2.3, 0, 0.04, z, materials.sidewalk);
}
for (const x of verticalStreets) {
addBox(2.3, 0.08, 32, x, 0.045, 1.5, materials.sidewalk);
}
for (const z of horizontalStreets) {
addBox(35, 0.09, 1.38, 0, 0.105, z, materials.road);
}
for (const x of verticalStreets) {
addBox(1.38, 0.09, 32, x, 0.11, 1.5, materials.road);
}
// Small waterfront wave marks.
const waves = [];
for (let i = 0; i < 24; i++) {
const wave = addBox(
randomRange(0.8, 2.6),
0.018,
0.055,
randomRange(-17, 16),
0.07,
randomRange(19.6, 30.5),
materials.wave
);
wave.castShadow = false;
wave.receiveShadow = false;
waves.push(wave);
}
// ---------------------------------------------------------------------
// Buildings and instanced windows
// ---------------------------------------------------------------------
const windowData = [];
const buildingPalette = [
0xe6c6a4, 0xd49a84, 0xc4c9ae, 0x93adb0,
0xd8b66c, 0xb98572, 0xc6d4ce, 0xe0d7c3,
0xaeb7c5, 0xbc8e78
];
function addWindows(x, z, width, depth, height) {
const floors = Math.max(1, Math.floor((height - 0.35) / 0.75));
const frontColumns = Math.max(1, Math.floor(width / 0.75));
for (let floor = 0; floor < floors; floor++) {
const y = 0.62 + floor * 0.76;
for (let col = 0; col < frontColumns; col++) {
const px = x - width / 2 + (col + 0.5) * (width / frontColumns);
const offset = width / frontColumns * 0.28;
windowData.push({
x: px,
y,
z: z + depth / 2 + 0.022,
sx: 0.25,
sy: 0.34,
sz: 0.035,
color: random() < 0.17 ? 0xf4c46e : 0x38545b
});
windowData.push({
x: px,
y,
z: z - depth / 2 - 0.022,
sx: 0.25,
sy: 0.34,
sz: 0.035,
color: random() < 0.13 ? 0xf4c46e : 0x38545b
});
}
const sideColumns = Math.max(1, Math.floor(depth / 0.75));
for (let col = 0; col < sideColumns; col++) {
const pz = z - depth / 2 + (col + 0.5) * (depth / sideColumns);
const offset = depth / sideColumns * 0.27;
windowData.push({
x: x + width / 2 + 0.022,
y,
z: pz,
sx: 0.035,
sy: 0.34,
sz: 0.25,
color: random() < 0.15 ? 0xf4c46e : 0x38545b
});
windowData.push({
x: x - width / 2 - 0.022,
y,
z: pz,
sx: 0.035,
sy: 0.34,
sz: 0.25,
color: random() < 0.15 ? 0xf4c46e : 0x38545b
});
}
}
}
function addBuilding(
x,
z,
width,
depth,
height,
color,
roofColor = 0x9d6657,
parent = scene
) {
const body = addBox(
width,
height,
depth,
x,
height / 2 + 0.1,
z,
makeMaterial(color),
parent
);
addBox(
width + 0.12,
0.15,
depth + 0.12,
x,
height + 0.13,
z,
makeMaterial(roofColor),
parent
);
addWindows(x, z, width, depth, height);
if (random() < 0.28) {
addBox(
randomRange(0.4, 0.7),
randomRange(0.3, 0.65),
randomRange(0.4, 0.7),
x + randomRange(-width * 0.25, width * 0.25),
height + 0.38,
z + randomRange(-depth * 0.25, depth * 0.25),
materials.roofDark,
parent
);
}
if (random() < 0.18) {
addBox(
width * 0.72,
0.09,
0.35,
x,
0.85,
z + depth / 2 + 0.18,
makeMaterial(pick([0x7f9f9c, 0xc88b6e, 0x647c83])),
parent
);
}
return body;
}
const xEdges = [-17, -13, -8, -3, 2, 7, 12, 17];
const zEdges = [-13, -10, -5, 0, 5, 10, 15, 18];
const reservedBlocks = new Set(["0-3", "1-2", "1-1", "3-3", "3-5"]);
for (let bx = 0; bx < xEdges.length - 1; bx++) {
for (let bz = 0; bz < zEdges.length - 1; bz++) {
if (reservedBlocks.has(`${bx}-${bz}`)) continue;
const x0 = xEdges[bx] + 1.0;
const x1 = xEdges[bx + 1] - 1.0;
const z0 = zEdges[bz] + 1.0;
const z1 = zEdges[bz + 1] - 1.0;
const width = x1 - x0;
const depth = z1 - z0;
if (width < 1.2 || depth < 1.2) continue;
const columns = Math.max(1, Math.floor(width / 2.45));
const rows = Math.max(1, Math.floor(depth / 2.35));
const cellWidth = width / columns;
const cellDepth = depth / rows;
for (let col = 0; col < columns; col++) {
for (let row = 0; row < rows; row++) {
const buildingWidth = Math.max(0.85, cellWidth - randomRange(0.25, 0.48));
const buildingDepth = Math.max(0.85, cellDepth - randomRange(0.25, 0.48));
const buildingHeight = randomRange(1.55, 5.45);
addBuilding(
x0 + (col + 0.5) * cellWidth,
z0 + (row + 0.5) * cellDepth,
buildingWidth,
buildingDepth,
buildingHeight,
pick(buildingPalette),
pick([0x9d6657, 0x7d6d64, 0x697a78, 0xa67b59])
);
}
}
}
}
// One instanced mesh keeps the window count inexpensive.
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.52,
metalness: 0.04
});
const windowMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
windowMaterial,
windowData.length
);
const dummy = new THREE.Object3D();
windowData.forEach((data, index) => {
dummy.position.set(data.x, data.y, data.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(data.sx, data.sy, data.sz);
dummy.updateMatrix();
windowMesh.setMatrixAt(index, dummy.matrix);
windowMesh.setColorAt(index, new THREE.Color(data.color));
});
windowMesh.instanceMatrix.needsUpdate = true;
if (windowMesh.instanceColor) windowMesh.instanceColor.needsUpdate = true;
scene.add(windowMesh);
// ---------------------------------------------------------------------
// Parks, trees, plazas, and landmark buildings
// ---------------------------------------------------------------------
function createTree(x, z, scale = 1) {
const group = new THREE.Group();
addBox(
0.18 * scale,
0.9 * scale,
0.18 * scale,
0,
0.45 * scale,
0,
materials.trunk,
group
);
addBox(
0.9 * scale,
0.65 * scale,
0.9 * scale,
0,
1.08 * scale,
0,
materials.leaf,
group
);
addBox(
0.64 * scale,
0.54 * scale,
0.72 * scale,
-0.12 * scale,
1.47 * scale,
0.08 * scale,
materials.leafLight,
group
);
group.position.set(x, 0.16, z);
scene.add(group);
return group;
}
function createFountain(x, z) {
const group = new THREE.Group();
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.7, 0.78, 0.25, 20),
materials.fountain
);
base.position.y = 0.13;
base.castShadow = true;
group.add(base);
const water = new THREE.Mesh(
new THREE.CylinderGeometry(0.55, 0.55, 0.06, 20),
materials.fountainWater
);
water.position.y = 0.29;
group.add(water);
addBox(0.16, 0.7, 0.16, 0, 0.53, 0, materials.fountain, group);
addBox(0.5, 0.12, 0.5, 0, 0.9, 0, materials.fountain, group);
group.position.set(x, 0.12, z);
scene.add(group);
}
function createPlaza(x, z, width, depth) {
addBox(width, 0.11, depth, x, 0.12, z, materials.grass);
addBox(width * 0.22, 0.035, depth, x, 0.19, z, materials.sidewalk);
addBox(width, 0.035, depth * 0.22, x, 0.195, z, materials.sidewalk);
createFountain(x, z);
}
// Plaza Constitución.
createPlaza(-15.1, 2.45, 3.1, 3.8);
createTree(-16.25, 1.25, 0.85);
createTree(-14.3, 3.7, 0.78);
createTree(-16.25, 3.6, 0.72);
// Plaza Matriz.
createPlaza(-10.65, -2.45, 3.75, 3.7);
createTree(-12.1, -3.9, 0.8);
createTree(-9.15, -1.15, 0.82);
createTree(-11.9, -0.95, 0.7);
// Mercado del Puerto.
addBuilding(
4.55, 12.65, 4.15, 3.35, 2.25,
0xe0c596, 0xb75d4b
);
for (let i = 0; i < 6; i++) {
addBox(
0.23,
1.55,
0.23,
3.05 + i * 0.6,
0.9,
10.84,
materials.cream
);
}
addBox(4.55, 0.25, 0.36, 4.55, 1.78, 10.84, materials.marketAccent);
addBox(1.4, 0.8, 0.25, 4.55, 0.63, 10.79, materials.marketAccent);
// Small market canopies beside the entrance.
for (const x of [3.25, 5.75]) {
addBox(0.85, 0.75, 0.7, x, 0.53, 10.65, materials.cream);
addBox(0.95, 0.12, 0.85, x, 0.96, 10.65, materials.marketRoof);
}
// Teatro Solís.
addBuilding(4.55, 2.6, 4.0, 2.8, 2.45, 0xe6d8bd, 0x796e61);
for (let i = 0; i < 6; i++) {
addBox(
0.25,
2.0,
0.25,
2.9 + i * 0.66,
1.15,
1.05,
materials.cream
);
}
const pedimentShape = new THREE.Shape();
pedimentShape.moveTo(-2.2, 0);
pedimentShape.lineTo(2.2, 0);
pedimentShape.lineTo(0, 1.1);
pedimentShape.closePath();
const pediment = new THREE.Mesh(
new THREE.ExtrudeGeometry(pedimentShape, {
depth: 3,
bevelEnabled: false
}),
materials.cream
);
pediment.position.set(4.55, 2.62, 1.03);
pediment.castShadow = true;
scene.add(pediment);
// Catedral / central church block.
addBuilding(-10.55, -7.55, 3.5, 3.55, 2.85, 0xdfd3b9, 0x776d65);
addBox(0.85, 4.1, 0.9, -11.95, 2.12, -7.65, materials.cream);
addBox(0.85, 3.65, 0.9, -9.15, 1.9, -7.65, materials.cream);
addBox(0.38, 1.35, 0.38, -11.95, 4.82, -7.65, materials.gold);
addBox(0.38, 1.15, 0.38, -9.15, 4.47, -7.65, materials.gold);
// A handful of street trees.
[
[-16.2, -2.2], [-14.4, -1.1], [-6.5, -11.6],
[-4.8, -9.8], [8.7, -1.6], [10.7, 2.4],
[8.6, 12.8], [-6.5, 12.7], [-11.7, 7.5]
].forEach(([x, z], index) => {
createTree(x, z, 0.64 + (index % 3) * 0.08);
});
// ---------------------------------------------------------------------
// Small urban details
// ---------------------------------------------------------------------
function createCar(x, z, rotation, color) {
const group = new THREE.Group();
addBox(
0.72, 0.32, 1.35,
0, 0.36, 0,
makeMaterial(color),
group
);
addBox(
0.58, 0.32, 0.63,
0, 0.65, -0.03,
makeMaterial(0x9cb3b6),
group
);
group.position.set(x, 0.12, z);
group.rotation.y = rotation;
scene.add(group);
}
[
[-10.8, 5, 0, 0xd9b64e],
[-5.6, -5, 0, 0xc66e54],
[7, -2.7, Math.PI / 2, 0xd8d5c8],
[10.3, 5, Math.PI / 2, 0x557d87],
[-10.5, 10, 0, 0x77a5a0],
[4.5, -7.5, Math.PI / 2, 0xd1c5a4]
].forEach((args) => createCar(...args));
// Ferry on the water.
const ferry = new THREE.Group();
addBox(2.8, 0.38, 1.15, 0, 0, 0, materials.cream, ferry);
addBox(1.65, 0.55, 0.88, 0, 0.4, 0, materials.marketAccent, ferry);
addBox(0.18, 0.95, 0.18, -0.65, 0.85, 0, materials.gold, ferry);
ferry.position.set(8.5, 0.48, 22.2);
ferry.rotation.y = -0.08;
scene.add(ferry);
// ---------------------------------------------------------------------
// Routes
// ---------------------------------------------------------------------
const directPath = [
{ x: -13.35, z: 5.2 },
{ x: -3.0, z: 5.2 },
{ x: 2.0, z: 5.2 },
{ x: 2.0, z: 9.3 },
{ x: 2.65, z: 10.2 }
];
const loopPath = [
{ x: -13.35, z: 5.2 },
{ x: -8.0, z: 5.2 },
{ x: -8.0, z: 0.0 },
{ x: -3.0, z: 0.0 },
{ x: -3.0, z: -5.0 },
{ x: 2.0, z: -5.0 },
{ x: 2.0, z: 0.0 },
{ x: -3.0, z: 0.0 },
{ x: -3.0, z: 5.2 },
{ x: 2.0, z: 5.2 },
{ x: 2.0, z: 9.3 },
{ x: 2.65, z: 10.2 }
];
const METERS_PER_UNIT = 32;
const WALK_SPEED = 75; // metres per simulated minute
const BROWSE_MINUTES = 1.5;
const shoppingStopIndices = new Set([2, 4, 6, 8]);
const directGroup = new THREE.Group();
const loopGroup = new THREE.Group();
scene.add(directGroup, loopGroup);
function pathLength(path) {
let total = 0;
for (let i = 0; i < path.length - 1; i++) {
total += Math.hypot(
path[i + 1].x - path[i].x,
path[i + 1].z - path[i].z
);
}
return total;
}
const directLength = pathLength(directPath);
const loopLength = pathLength(loopPath);
function createRoute(path, colorMaterial, glowMaterial, parent, y, width) {
const glowWidth = width * 2.25;
for (let i = 0; i < path.length - 1; i++) {
const a = path[i];
const b = path[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const angle = -Math.atan2(dz, dx);
const mx = (a.x + b.x) / 2;
const mz = (a.z + b.z) / 2;
const glow = addBox(
length + glowWidth,
0.035,
glowWidth,
mx,
y - 0.055,
mz,
glowMaterial,
parent
);
glow.rotation.y = angle;
glow.castShadow = false;
glow.receiveShadow = false;
const line = addBox(
length + width,
0.065,
width,
mx,
y,
mz,
colorMaterial,
parent
);
line.rotation.y = angle;
line.castShadow = false;
line.receiveShadow = false;
}
for (const point of path) {
const joint = new THREE.Mesh(
new THREE.CylinderGeometry(width * 0.56, width * 0.56, 0.065, 20),
colorMaterial
);
joint.position.set(point.x, y, point.z);
parent.add(joint);
}
}
createRoute(
directPath,
materials.direct,
materials.directGlow,
directGroup,
0.205,
0.30
);
createRoute(
loopPath,
materials.loop,
materials.loopGlow,
loopGroup,
0.255,
0.30
);
// Shopping stop markers.
const stopMarkers = [];
const shoppingStops = [2, 4, 6, 8].map((index) => loopPath[index]);
shoppingStops.forEach((point, index) => {
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0xf26a4f,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.055, 8, 32),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.set(point.x, 0.31, point.z);
loopGroup.add(ring);
addBox(
0.23, 0.23, 0.23,
point.x, 0.38, point.z,
materials.gold,
loopGroup
);
stopMarkers.push(ring);
});
// Start marker.
const startGroup = new THREE.Group();
addBox(0.62, 0.1, 0.62, 0, 0.23, 0, materials.direct, startGroup);
addBox(0.15, 1.1, 0.15, 0, 0.77, 0, materials.direct, startGroup);
addBox(0.68, 0.34, 0.07, 0.28, 1.15, 0, materials.direct, startGroup);
startGroup.position.set(directPath[0].x, 0.11, directPath[0].z);
scene.add(startGroup);
// Destination marker.
const destinationGroup = new THREE.Group();
const destinationRingMaterial = new THREE.MeshBasicMaterial({
color: 0xeebd57,
transparent: true,
opacity: 0.78,
depthWrite: false
});
const destinationRing = new THREE.Mesh(
new THREE.TorusGeometry(0.65, 0.07, 10, 40),
destinationRingMaterial
);
destinationRing.rotation.x = Math.PI / 2;
destinationRing.position.y = 0.34;
destinationGroup.add(destinationRing);
addBox(0.15, 1.6, 0.15, 0, 0.9, 0, materials.gold, destinationGroup);
const destinationGem = new THREE.Mesh(
new THREE.OctahedronGeometry(0.38),
materials.gold
);
destinationGem.position.y = 1.95;
destinationGroup.add(destinationGem);
destinationGroup.position.set(directPath.at(-1).x, 0.12, directPath.at(-1).z);
scene.add(destinationGroup);
// ---------------------------------------------------------------------
// Walking animation
// ---------------------------------------------------------------------
function createWalker(color) {
const group = new THREE.Group();
addBox(
0.29, 0.48, 0.24,
0, 0.56, 0,
makeMaterial(color),
group
);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.16, 12, 10),
makeMaterial(0xe7b48d)
);
head.position.y = 0.96;
head.castShadow = true;
group.add(head);
addBox(0.18, 0.28, 0.12, 0, 0.58, -0.18, materials.roofDark, group);
addBox(0.1, 0.28, 0.12, -0.2, 0.54, 0, materials.cream, group);
addBox(0.1, 0.28, 0.12, 0.2, 0.54, 0, materials.cream, group);
addBox(0.11, 0.28, 0.13, -0.08, 0.16, 0, materials.ink ?? materials.roofDark, group);
addBox(0.11, 0.28, 0.13, 0.08, 0.16, 0, materials.ink ?? materials.roofDark, group);
return group;
}
const directWalker = createWalker(0x087d88);
const loopWalker = createWalker(0xd6533a);
directGroup.add(directWalker);
loopGroup.add(loopWalker);
function samplePath(path, distance) {
let remaining = Math.max(0, distance);
for (let i = 0; i < path.length - 1; i++) {
const a = path[i];
const b = path[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (remaining <= length || i === path.length - 2) {
const t = length === 0 ? 0 : Math.min(1, remaining / length);
return {
x: a.x + dx * t,
z: a.z + dz * t,
tangentX: dx / (length || 1),
tangentZ: dz / (length || 1)
};
}
remaining -= length;
}
const last = path.at(-1);
const previous = path.at(-2);
return {
x: last.x,
z: last.z,
tangentX: last.x - previous.x,
tangentZ: last.z - previous.z
};
}
function directDistanceAtTime(time) {
return Math.min(directLength, time * WALK_SPEED / METERS_PER_UNIT);
}
function loopDistanceAtTime(time) {
let elapsed = 0;
let traveled = 0;
for (let i = 0; i < loopPath.length - 1; i++) {
const a = loopPath[i];
const b = loopPath[i + 1];
const length = Math.hypot(b.x - a.x, b.z - a.z);
const segmentTime = length * METERS_PER_UNIT / WALK_SPEED;
if (time < elapsed + segmentTime) {
const fraction = (time - elapsed) / segmentTime;
return traveled + length * fraction;
}
elapsed += segmentTime;
traveled += length;
if (shoppingStopIndices.has(i + 1)) {
if (time < elapsed + BROWSE_MINUTES) return traveled;
elapsed += BROWSE_MINUTES;
}
}
return traveled;
}
const directDuration = directLength * METERS_PER_UNIT / WALK_SPEED;
const loopDuration =
loopLength * METERS_PER_UNIT / WALK_SPEED +
shoppingStopIndices.size * BROWSE_MINUTES;
const maxTimeline = Math.ceil(loopDuration);
const timeline = document.getElementById("timeline");
timeline.max = maxTimeline;
const ticks = document.getElementById("ticks");
for (let i = 0; i < 5; i++) {
const tick = document.createElement("span");
tick.style.left = `${i * 25}%`;
tick.textContent = `${Math.round(maxTimeline * i / 4)}m`;
ticks.appendChild(tick);
}
function updateWalkers(time) {
const directSample = samplePath(directPath, directDistanceAtTime(time));
const loopSample = samplePath(loopPath, loopDistanceAtTime(time));
directWalker.position.set(directSample.x, 0.2, directSample.z);
directWalker.rotation.y = Math.atan2(directSample.tangentX, directSample.tangentZ);
directWalker.position.y += Math.abs(Math.sin(time * 7)) * 0.035;
loopWalker.position.set(loopSample.x, 0.26, loopSample.z);
loopWalker.rotation.y = Math.atan2(loopSample.tangentX, loopSample.tangentZ);
loopWalker.position.y += Math.abs(Math.sin(time * 7)) * 0.035;
}
// ---------------------------------------------------------------------
// HTML labels projected from 3D anchors
// ---------------------------------------------------------------------
const labels = [];
function addMapLabel(text, anchor, className = "place") {
const element = document.createElement("div");
element.className = `map-label ${className}`;
element.textContent = text;
labelLayer.appendChild(element);
labels.push({ element, anchor });
return element;
}
addMapLabel("Plaza Constitución", new THREE.Vector3(-15.1, 1.9, 2.6));
addMapLabel("Plaza Matriz", new THREE.Vector3(-10.65, 1.8, -2.4));
addMapLabel("Mercado del Puerto", new THREE.Vector3(4.55, 3.2, 12.65), "poi");
addMapLabel("Teatro Solís", new THREE.Vector3(4.55, 4.1, 2.5), "poi");
addMapLabel("Catedral", new THREE.Vector3(-10.55, 5.2, -7.6), "poi");
addMapLabel("Start", new THREE.Vector3(-13.35, 1.6, 5.2), "poi");
addMapLabel("Destination", new THREE.Vector3(2.65, 2.8, 10.2), "poi");
addMapLabel("Direct", new THREE.Vector3(-4.8, 0.9, 5.2), "route-tag direct-tag");
addMapLabel("Shopping loop", new THREE.Vector3(-3.0, 0.9, -5.0), "route-tag loop-tag");
shoppingStops.forEach((point, index) => {
addMapLabel(
String(index + 1).padStart(2, "0"),
new THREE.Vector3(point.x, 0.85, point.z),
"shop-label"
);
});
[
["Sarandí", -5.0, 0.35, 5.0],
["Cerro Largo", 9.0, 0.35, 0.0],
["Juan Carlos Gómez", 7.0, 0.35, 10.0],
["Av. Constitución", -13.0, 0.35, -7.0],
["Julio Herrera", -3.0, 0.35, -9.0],
["Río de la Plata", 9.0, 0.35, 22.0]
].forEach(([text, x, y, z]) => {
addMapLabel(text, new THREE.Vector3(x, y, z), "street");
});
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labels) {
projected.copy(label.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// ---------------------------------------------------------------------
// Camera controls implemented without external addons
// ---------------------------------------------------------------------
const cameraState = {
azimuth: -0.58,
polar: 0.82,
zoom: 1.03,
target: new THREE.Vector3(-0.5, 0, 2.3)
};
const cameraGoal = {
azimuth: cameraState.azimuth,
polar: cameraState.polar,
zoom: cameraState.zoom,
target: cameraState.target.clone()
};
const cameraRadius = 48;
function updateCamera(delta) {
const blend = 1 - Math.exp(-delta * 7);
cameraState.azimuth = THREE.MathUtils.lerp(
cameraState.azimuth, cameraGoal.azimuth, blend
);
cameraState.polar = THREE.MathUtils.lerp(
cameraState.polar, cameraGoal.polar, blend
);
cameraState.zoom = THREE.MathUtils.lerp(
cameraState.zoom, cameraGoal.zoom, blend
);
cameraState.target.lerp(cameraGoal.target, blend);
const sinPolar = Math.sin(cameraState.polar);
camera.position.set(
cameraState.target.x +
cameraRadius * sinPolar * Math.sin(cameraState.azimuth),
cameraState.target.y +
cameraRadius * Math.cos(cameraState.polar),
cameraState.target.z +
cameraRadius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function setCameraPreset(name) {
if (name === "overview") {
cameraGoal.azimuth = -0.58;
cameraGoal.polar = 0.82;
cameraGoal.zoom = 1.03;
cameraGoal.target.set(-0.5, 0, 2.3);
} else if (name === "street") {
cameraGoal.azimuth = -1.08;
cameraGoal.polar = 1.16;
cameraGoal.zoom = 1.85;
cameraGoal.target.set(-3.8, 0, 4.3);
} else if (name === "market") {
cameraGoal.azimuth = -0.68;
cameraGoal.polar = 0.94;
cameraGoal.zoom = 2.15;
cameraGoal.target.set(2.9, 0, 9.8);
}
}
const pointers = new Map();
let activePointer = null;
let interactionMode = "orbit";
let pinchDistance = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
activePointer = event.pointerId;
interactionMode = event.button === 2 ? "pan" : "orbit";
if (pointers.size === 2) {
const values = [...pointers.values()];
pinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size >= 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (pinchDistance > 0) {
cameraGoal.zoom = THREE.MathUtils.clamp(
cameraGoal.zoom * distance / pinchDistance,
0.55,
3.4
);
}
pinchDistance = distance;
return;
}
if (event.pointerId !== activePointer) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
if (interactionMode === "orbit") {
cameraGoal.azimuth -= dx * 0.006;
cameraGoal.polar = THREE.MathUtils.clamp(
cameraGoal.polar + dy * 0.0045,
0.38,
1.36
);
} else {
const viewHeight = 32 / cameraGoal.zoom;
const scale = viewHeight / window.innerHeight;
const rightX = Math.cos(cameraGoal.azimuth);
const rightZ = -Math.sin(cameraGoal.azimuth);
const forwardX = -Math.sin(cameraGoal.azimuth);
const forwardZ = -Math.cos(cameraGoal.azimuth);
cameraGoal.target.x -= rightX * dx * scale;
cameraGoal.target.z -= rightZ * dx * scale;
cameraGoal.target.x -= forwardX * dy * scale;
cameraGoal.target.z -= forwardZ * dy * scale;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (event.pointerId === activePointer) activePointer = null;
if (pointers.size < 2) pinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal.zoom = THREE.MathUtils.clamp(
cameraGoal.zoom * Math.exp(-event.deltaY * 0.001),
0.55,
3.4
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.target.tagName === "INPUT") return;
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
} else if (event.key === "ArrowLeft") {
cameraGoal.azimuth -= 0.08;
} else if (event.key === "ArrowRight") {
cameraGoal.azimuth += 0.08;
} else if (event.key === "ArrowUp") {
cameraGoal.polar = THREE.MathUtils.clamp(cameraGoal.polar - 0.05, 0.38, 1.36);
} else if (event.key === "ArrowDown") {
cameraGoal.polar = THREE.MathUtils.clamp(cameraGoal.polar + 0.05, 0.38, 1.36);
} else if (event.key === "+" || event.key === "=") {
cameraGoal.zoom = THREE.MathUtils.clamp(cameraGoal.zoom * 1.1, 0.55, 3.4);
} else if (event.key === "-") {
cameraGoal.zoom = THREE.MathUtils.clamp(cameraGoal.zoom / 1.1, 0.55, 3.4);
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = 32;
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
// ---------------------------------------------------------------------
// UI and route comparison
// ---------------------------------------------------------------------
const directDistanceElement = document.getElementById("direct-distance");
const directTimeElement = document.getElementById("direct-time");
const loopDistanceElement = document.getElementById("loop-distance");
const loopTimeElement = document.getElementById("loop-time");
const directStatus = document.getElementById("direct-status");
const loopStatus = document.getElementById("loop-status");
const directMeter = document.getElementById("direct-meter");
const loopMeter = document.getElementById("loop-meter");
const comparisonSummary = document.getElementById("comparison-summary");
const currentTimeElement = document.getElementById("current-time");
const playButton = document.getElementById("play");
const directDistanceKm = directLength * METERS_PER_UNIT / 1000;
const loopDistanceKm = loopLength * METERS_PER_UNIT / 1000;
const distanceDifference = loopDistanceKm - directDistanceKm;
const timeDifference = Math.round(loopDuration - directDuration);
directDistanceElement.textContent = `${directDistanceKm.toFixed(2)} km`;
directTimeElement.textContent = `${Math.round(directDuration)} min`;
loopDistanceElement.textContent = `${loopDistanceKm.toFixed(2)} km`;
loopTimeElement.textContent = `${Math.round(loopDuration)} min`;
comparisonSummary.textContent =
`Shopping adds ${distanceDifference.toFixed(2)} km and ${timeDifference} minutes.`;
let currentTime = 0;
let playing = false;
function formatClock(minutes) {
const wholeMinutes = Math.floor(minutes);
const seconds = Math.floor((minutes - wholeMinutes) * 60);
return `${String(wholeMinutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}
function togglePlayback() {
if (currentTime >= maxTimeline) currentTime = 0;
playing = !playing;
playButton.textContent = playing ? "Ⅱ" : "▶";
playButton.setAttribute("aria-pressed", String(playing));
playButton.setAttribute("aria-label", playing ? "Pause comparison" : "Play comparison");
}
playButton.addEventListener("click", togglePlayback);
timeline.addEventListener("input", () => {
currentTime = Number(timeline.value);
playing = false;
playButton.textContent = "▶";
playButton.setAttribute("aria-pressed", "false");
});
document.querySelectorAll("[data-mode]").forEach((button) => {
button.addEventListener("click", () => {
const mode = button.dataset.mode;
const showDirect = mode === "both" || mode === "direct";
const showLoop = mode === "both" || mode === "loop";
directGroup.visible = showDirect;
loopGroup.visible = showLoop;
document.getElementById("direct-card").classList.toggle("dimmed", !showDirect);
document.getElementById("loop-card").classList.toggle("dimmed", !showLoop);
document.querySelectorAll("[data-mode]").forEach((other) => {
other.classList.toggle("selected", other === button);
});
});
});
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
setCameraPreset(button.dataset.view);
document.querySelectorAll("[data-view]").forEach((other) => {
other.classList.toggle("selected", other === button);
});
});
});
const labelsToggle = document.getElementById("labels-toggle");
labelsToggle.addEventListener("click", () => {
const hidden = labelLayer.classList.toggle("hidden");
labelsToggle.classList.toggle("selected", !hidden);
labelsToggle.setAttribute("aria-pressed", String(!hidden));
});
function updateTimelineUI() {
currentTimeElement.textContent = formatClock(currentTime);
timeline.value = currentTime;
timeline.style.setProperty(
"--p",
`${currentTime / maxTimeline * 100}%`
);
const directProgress = Math.min(1, currentTime / directDuration);
const loopProgress = Math.min(1, currentTime / loopDuration);
directMeter.style.width = `${directProgress * 100}%`;
loopMeter.style.width = `${loopProgress * 100}%`;
directStatus.textContent =
currentTime >= directDuration ? "Arrived" :
currentTime > 0 ? "Walking" : "Ready";
loopStatus.textContent =
currentTime >= loopDuration ? "Arrived" :
currentTime > 0 ? "Walking / browsing" : "Ready";
}
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
currentTime = Math.min(maxTimeline, currentTime + delta * 0.82);
if (currentTime >= maxTimeline) {
playing = false;
playButton.textContent = "▶";
playButton.setAttribute("aria-pressed", "false");
}
}
updateWalkers(currentTime);
updateTimelineUI();
updateCamera(delta);
updateLabels();
const pulse = 1 + Math.sin(clock.elapsedTime * 3.1) * 0.12;
destinationRing.scale.setScalar(pulse);
destinationRingMaterial.opacity = 0.62 + Math.sin(clock.elapsedTime * 3.1) * 0.16;
destinationGem.rotation.y = clock.elapsedTime * 0.7;
destinationGem.position.y = 1.95 + Math.sin(clock.elapsedTime * 2.2) * 0.05;
stopMarkers.forEach((marker, index) => {
const localPulse = 1 + Math.sin(clock.elapsedTime * 3.2 + index) * 0.18;
marker.scale.setScalar(localPulse);
marker.material.opacity = 0.48 + Math.sin(clock.elapsedTime * 3.2 + index) * 0.22;
});
waves.forEach((wave, index) => {
wave.position.x += Math.sin(clock.elapsedTime * 0.5 + index) * 0.0007;
});
ferry.position.y = 0.48 + Math.sin(clock.elapsedTime * 0.9) * 0.025;
ferry.rotation.z = Math.sin(clock.elapsedTime * 0.7) * 0.012;
renderer.render(scene, camera);
});
// Remove the loading screen once the first frame is ready.
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
setTimeout(() => document.getElementById("loading").remove(), 450);
});
</script>
</body>
</html>
```