Prompt specification
Build or customize the Three.js 3D scene "Compare direct stairs with a winding street approach" 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 stairs with a winding street approach
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-direct-stairs-with-a-winding-street-approach-x2956
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring the hillside lanes of Sidi Bou Said. Let users choose between two schematic routes to the same viewpoint. Display distance and climb separately and keep the coastal horizon fixed for both journeys. Voxel style.
Working Three.js Starter (compare-direct-stairs-with-a-winding-street-approach-x2956):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#d9eff7" />
<title>Sidi Bou Said — Voxel Route Atlas</title>
<style>
:root {
--ink: #18323f;
--muted: #6f858d;
--paper: rgba(255, 252, 244, 0.9);
--paper-solid: #fffdf7;
--line: rgba(24, 50, 63, 0.13);
--blue: #176be8;
--blue-dark: #0b4eb3;
--coral: #f16f52;
--coral-dark: #be4e38;
--shadow: 0 24px 70px rgba(29, 61, 74, 0.2);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bfe5f2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.wash {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(90deg, rgba(255, 252, 241, 0.26), transparent 31%),
linear-gradient(0deg, rgba(39, 91, 111, 0.11), transparent 26%);
}
.brand {
position: fixed;
top: 28px;
left: 30px;
z-index: 4;
pointer-events: none;
}
.brand-kicker {
margin-bottom: 7px;
color: var(--blue-dark);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.23em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
color: #143647;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 42px);
font-weight: 500;
letter-spacing: -0.045em;
line-height: 0.95;
}
.brand h1 span {
color: var(--blue);
font-style: italic;
}
.brand p {
margin: 9px 0 0;
color: rgba(20, 54, 71, 0.66);
font-size: 11px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.horizon-lock {
position: fixed;
top: 28px;
right: 28px;
z-index: 4;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 999px;
background: rgba(247, 252, 252, 0.69);
box-shadow: 0 9px 30px rgba(44, 94, 116, 0.1);
backdrop-filter: blur(12px);
color: #275469;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.lock-icon {
position: relative;
width: 13px;
height: 11px;
border: 2px solid var(--blue);
border-radius: 2px;
}
.lock-icon::before {
content: "";
position: absolute;
width: 8px;
height: 7px;
left: 0.5px;
top: -7px;
border: 2px solid var(--blue);
border-bottom: 0;
border-radius: 7px 7px 0 0;
}
.panel {
position: fixed;
z-index: 5;
top: 24px;
left: 24px;
width: 370px;
max-height: calc(100vh - 48px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.76);
border-radius: 24px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(22px) saturate(1.15);
scrollbar-width: thin;
scrollbar-color: rgba(23, 107, 232, 0.25) transparent;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--blue);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 18px;
height: 2px;
background: currentColor;
}
.panel h2 {
margin: 12px 0 7px;
font-family: Georgia, "Times New Roman", serif;
font-size: 29px;
font-weight: 500;
letter-spacing: -0.035em;
line-height: 1.05;
}
.intro {
margin: 0 0 16px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.route-list {
display: grid;
gap: 9px;
}
.route-card {
position: relative;
width: 100%;
padding: 14px 14px 13px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(255, 255, 255, 0.55);
text-align: left;
cursor: pointer;
transition:
transform 180ms ease,
border-color 180ms ease,
background 180ms ease,
box-shadow 180ms ease;
}
.route-card:hover {
transform: translateY(-1px);
border-color: rgba(23, 107, 232, 0.32);
}
.route-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--route-color);
opacity: 0.3;
transition: opacity 180ms ease;
}
.route-card.active {
border-color: var(--route-color);
background: rgba(255, 255, 255, 0.95);
box-shadow: 0 10px 26px rgba(36, 70, 84, 0.09);
}
.route-card.active::before {
opacity: 1;
}
.route-head {
display: flex;
align-items: flex-start;
gap: 10px;
}
.route-letter {
display: grid;
flex: 0 0 29px;
height: 29px;
place-items: center;
border-radius: 9px;
background: color-mix(in srgb, var(--route-color) 13%, white);
color: var(--route-color);
font-size: 11px;
font-weight: 900;
}
.route-title {
flex: 1;
min-width: 0;
}
.route-title strong {
display: block;
margin: 1px 0 2px;
font-size: 13px;
letter-spacing: -0.01em;
}
.route-title small {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.route-check {
width: 16px;
height: 16px;
margin-top: 2px;
border: 1px solid rgba(24, 50, 63, 0.17);
border-radius: 50%;
}
.route-card.active .route-check {
border: 4px solid var(--route-color);
background: white;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1px 1fr;
gap: 12px;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.metric-label {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.metric-value {
color: var(--ink);
font-size: 16px;
font-weight: 780;
letter-spacing: -0.035em;
}
.metric-value span {
margin-left: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
letter-spacing: 0;
}
.metric-divider {
background: var(--line);
}
.destination {
display: flex;
align-items: center;
gap: 11px;
margin-top: 12px;
padding: 12px;
border: 1px solid rgba(23, 107, 232, 0.13);
border-radius: 14px;
background: rgba(23, 107, 232, 0.055);
}
.destination-icon {
position: relative;
display: grid;
flex: 0 0 34px;
height: 34px;
place-items: center;
border-radius: 10px;
background: var(--blue);
box-shadow: 0 6px 14px rgba(23, 107, 232, 0.2);
}
.destination-icon::before {
content: "";
width: 12px;
height: 17px;
border-radius: 7px 7px 1px 1px;
background: white;
}
.destination strong {
display: block;
font-size: 11px;
}
.destination small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.journey {
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.journey-head {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
}
.play-button {
display: flex;
flex: 1;
align-items: center;
justify-content: center;
gap: 8px;
height: 38px;
border: 0;
border-radius: 11px;
background: var(--ink);
color: white;
font-size: 11px;
font-weight: 760;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
.play-button:hover {
background: var(--blue-dark);
transform: translateY(-1px);
}
#play-icon {
width: 15px;
color: #8fc8ff;
font-size: 10px;
}
.reset-button {
width: 38px;
height: 38px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.7);
color: var(--muted);
font-size: 17px;
cursor: pointer;
}
.reset-button:hover {
color: var(--blue);
border-color: rgba(23, 107, 232, 0.25);
}
.progress-meta {
display: flex;
justify-content: space-between;
margin-top: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 8px 0 0;
border-radius: 999px;
outline: none;
background: linear-gradient(
to right,
var(--active-route) var(--progress, 0%),
rgba(24, 50, 63, 0.12) var(--progress, 0%)
);
appearance: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid white;
border-radius: 50%;
background: var(--active-route);
box-shadow: 0 2px 8px rgba(24, 50, 63, 0.25);
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid white;
border-radius: 50%;
background: var(--active-route);
box-shadow: 0 2px 8px rgba(24, 50, 63, 0.25);
}
.profile-wrap {
position: relative;
height: 72px;
margin-top: 7px;
border-bottom: 1px solid var(--line);
background:
linear-gradient(rgba(24, 50, 63, 0.035) 1px, transparent 1px);
background-size: 100% 23px;
}
#profile {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
}
.profile-caption {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel-footer {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 13px;
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.panel-footer button {
padding: 0;
border: 0;
background: none;
color: var(--blue);
font-weight: 750;
cursor: pointer;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.map-label {
position: absolute;
top: 0;
left: 0;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 8px;
background: rgba(255, 253, 247, 0.84);
box-shadow: 0 5px 16px rgba(27, 60, 72, 0.12);
color: #284b5a;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
backdrop-filter: blur(8px);
will-change: transform;
}
.map-label::before {
content: "";
display: inline-block;
width: 5px;
height: 5px;
margin-right: 6px;
border-radius: 50%;
background: var(--label-color, var(--blue));
vertical-align: 1px;
}
.legend {
position: fixed;
right: 28px;
bottom: 25px;
z-index: 4;
display: flex;
align-items: center;
gap: 14px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 12px;
background: rgba(250, 253, 250, 0.72);
box-shadow: 0 8px 26px rgba(30, 68, 82, 0.1);
backdrop-filter: blur(12px);
color: #41606d;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 13px;
height: 5px;
border-radius: 2px;
background: var(--swatch);
}
.toast {
position: fixed;
z-index: 8;
left: 50%;
bottom: 28px;
padding: 11px 15px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 12px;
background: rgba(19, 50, 64, 0.9);
box-shadow: 0 12px 35px rgba(19, 50, 64, 0.2);
color: white;
font-size: 10px;
font-weight: 720;
letter-spacing: 0.04em;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition: opacity 250ms ease, transform 250ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 900px) {
.brand {
top: 18px;
left: 18px;
}
.brand p {
display: none;
}
.horizon-lock {
top: 17px;
right: 16px;
}
.panel {
top: auto;
left: 12px;
right: 12px;
bottom: 12px;
width: auto;
max-height: 48vh;
padding: 16px;
border-radius: 20px;
}
.panel h2 {
margin-top: 8px;
font-size: 24px;
}
.intro {
margin-bottom: 11px;
}
.legend {
display: none;
}
.map-label {
font-size: 7px;
}
}
@media (max-width: 520px) {
.horizon-lock {
padding: 8px;
}
.horizon-lock span:last-child {
display: none;
}
.panel {
max-height: 51vh;
}
.route-card {
padding: 11px 12px;
}
.metrics {
margin-top: 9px;
padding-top: 8px;
}
.destination {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel landscape of Sidi Bou Said"></main>
<div class="wash"></div>
<header class="brand">
<div class="brand-kicker">Tunisia · 36.8° N</div>
<h1>Sidi Bou <span>Said</span></h1>
<p>A voxel route atlas</p>
</header>
<div class="horizon-lock">
<span class="lock-icon"></span>
<span>Coastal horizon · shared framing</span>
</div>
<aside class="panel" aria-label="Route planner">
<div class="eyebrow">Choose your climb</div>
<h2>Two lanes.<br />One blue horizon.</h2>
<p class="intro">
Pick a schematic route through the white-and-blue hill. Both journeys finish
at the same overlook, with the Mediterranean held in view.
</p>
<div class="route-list">
<button
class="route-card active"
data-route="coast"
style="--route-color: var(--blue)"
aria-pressed="true"
>
<div class="route-head">
<span class="route-letter">A</span>
<span class="route-title">
<strong>Café Switchbacks</strong>
<small>Gentle terraces · longer promenade</small>
</span>
<span class="route-check"></span>
</div>
<div class="metrics">
<div>
<span class="metric-label">Distance</span>
<div class="metric-value">1.84 <span>km</span></div>
</div>
<div class="metric-divider"></div>
<div>
<span class="metric-label">Climb</span>
<div class="metric-value">+86 <span>m</span></div>
</div>
</div>
</button>
<button
class="route-card"
data-route="stairs"
style="--route-color: var(--coral)"
aria-pressed="false"
>
<div class="route-head">
<span class="route-letter">B</span>
<span class="route-title">
<strong>Blue Door Steps</strong>
<small>Direct lanes · steeper stair climb</small>
</span>
<span class="route-check"></span>
</div>
<div class="metrics">
<div>
<span class="metric-label">Distance</span>
<div class="metric-value">1.52 <span>km</span></div>
</div>
<div class="metric-divider"></div>
<div>
<span class="metric-label">Climb</span>
<div class="metric-value">+124 <span>m</span></div>
</div>
</div>
</button>
</div>
<div class="destination">
<span class="destination-icon"></span>
<span>
<strong>Common viewpoint · Blue Door Overlook</strong>
<small>Identical finish and coastal camera for either route.</small>
</span>
</div>
<section class="journey" aria-label="Journey replay">
<div class="journey-head">
<button id="play" class="play-button">
<span id="play-icon">▶</span>
<span id="play-label">Replay this route</span>
</button>
<button id="reset" class="reset-button" title="Reset journey" aria-label="Reset journey">↺</button>
</div>
<input id="progress" type="range" min="0" max="1000" value="0" aria-label="Route progress" />
<div class="progress-meta">
<span id="route-status">Café Switchbacks</span>
<span id="progress-label">0% complete</span>
</div>
<div class="profile-wrap">
<svg id="profile" viewBox="0 0 300 72" preserveAspectRatio="none" aria-hidden="true">
<defs>
<linearGradient id="profile-fill" x1="0" y1="0" x2="0" y2="1">
<stop offset="0" stop-color="currentColor" stop-opacity="0.26" />
<stop offset="1" stop-color="currentColor" stop-opacity="0" />
</linearGradient>
</defs>
<path id="profile-area" fill="url(#profile-fill)" />
<polyline
id="profile-line"
fill="none"
stroke="currentColor"
stroke-width="2.3"
stroke-linecap="round"
stroke-linejoin="round"
/>
<line
id="profile-cursor"
x1="0"
x2="0"
y1="5"
y2="70"
stroke="currentColor"
stroke-width="1"
stroke-dasharray="3 3"
opacity="0.42"
/>
<circle id="profile-dot" r="4" fill="currentColor" stroke="#fffdf7" stroke-width="2" />
</svg>
</div>
<div class="profile-caption">
<span>Elevation profile</span>
<span>Shared overlook</span>
</div>
<div class="panel-footer">
<span>Drag to orbit · scroll to zoom</span>
<button id="reset-camera">Reset view</button>
</div>
</section>
</aside>
<div id="labels" aria-hidden="true"></div>
<div class="legend">
<span class="legend-item"><i class="swatch" style="--swatch: var(--blue)"></i> Route A</span>
<span class="legend-item"><i class="swatch" style="--swatch: var(--coral)"></i> Route B</span>
<span class="legend-item"><i class="swatch" style="--swatch: #68b7c6"></i> Mediterranean</span>
</div>
<div id="toast" class="toast" role="status"></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 mount = document.querySelector("#scene");
const scene = new THREE.Scene();
const skyColor = new THREE.Color("#b9e3f1");
const seaColor = new THREE.Color("#4da7bc");
scene.background = skyColor;
scene.fog = new THREE.Fog(skyColor, 78, 145);
const camera = new THREE.PerspectiveCamera(46, innerWidth / innerHeight, 0.1, 300);
const cameraTarget = new THREE.Vector3(-2.8, 3.2, -0.5);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
mount.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight("#e9f8ff", "#b89b72", 2.35);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff3d8", 3.15);
sun.position.set(-28, 42, 27);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -38;
sun.shadow.camera.right = 38;
sun.shadow.camera.top = 38;
sun.shadow.camera.bottom = -38;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const neutralVertexColors = new Float32Array(unitBox.attributes.position.count * 3).fill(1);
unitBox.setAttribute("color", new THREE.BufferAttribute(neutralVertexColors, 3));
const dummy = new THREE.Object3D();
// Seeded randomness keeps the scene stable between reloads.
function mulberry32(seed) {
return function () {
let t = (seed += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(8417);
const rand = (min, max) => min + random() * (max - min);
function addInstance(mesh, item, index) {
dummy.position.set(...item.p);
dummy.rotation.set(...(item.r || [0, 0, 0]));
dummy.scale.set(...item.s);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
}
function makeInstanced(items, material, options = {}) {
const mesh = new THREE.InstancedMesh(unitBox, material, items.length);
items.forEach((item, index) => {
addInstance(mesh, item, index);
if (options.colors && item.color) {
mesh.setColorAt(index, new THREE.Color(item.color));
}
});
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
if (options.colors && mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
scene.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Quantized hillside and routes
// ------------------------------------------------------------
function terrainHeight(x, z) {
const raw =
Math.max(0, (7 - x) * 0.58) +
Math.sin(z * 0.17) * 0.35 +
Math.cos(x * 0.32) * 0.18;
return Math.max(0.12, Math.floor((raw + 0.1) / 0.55) * 0.55);
}
const commonStartX = 4.6;
const commonStartZ = 17;
const commonEndX = -6.3;
const commonEndZ = -11.5;
function routePoint(x, z) {
return new THREE.Vector3(x, terrainHeight(x, z) + 0.23, z);
}
const routeData = {
coast: {
name: "Café Switchbacks",
color: "#176be8",
distance: "1.84 km",
climb: "+86 m",
xz: [
[commonStartX, commonStartZ],
[6.1, 13.0],
[4.4, 9.2],
[6.0, 5.0],
[3.8, 0.8],
[5.3, -3.2],
[2.8, -7.0],
[0.2, -9.2],
[-2.7, -12.0],
[-5.3, -10.2],
[commonEndX, commonEndZ]
]
},
stairs: {
name: "Blue Door Steps",
color: "#f16f52",
distance: "1.52 km",
climb: "+124 m",
xz: [
[commonStartX, commonStartZ],
[3.0, 14.0],
[0.5, 11.5],
[-2.2, 9.0],
[-1.1, 5.6],
[-3.6, 3.1],
[-5.8, 0.5],
[-5.2, -3.2],
[-7.2, -6.8],
[commonEndX, commonEndZ]
]
}
};
const routeCurves = {};
const routeMeshes = {};
const routeSamples = {};
for (const [key, data] of Object.entries(routeData)) {
data.points = data.xz.map(([x, z]) => routePoint(x, z));
data.curve = new THREE.CatmullRomCurve3(data.points, false, "catmullrom", 0.15);
routeCurves[key] = data.curve;
routeSamples[key] = data.curve.getSpacedPoints(260);
}
// Terrain is built from one-unit columns for a clean voxel silhouette.
const terrainItems = [];
for (let x = -21; x <= 6; x += 1) {
for (let z = -23; z <= 21; z += 1) {
const h = terrainHeight(x + 0.5, z + 0.5);
const shade = rand(0, 1);
const color = new THREE.Color("#d9c8a3");
color.offsetHSL(0, rand(-0.025, 0.02), shade * 0.045 - 0.018);
terrainItems.push({
p: [x + 0.5, h / 2 - 0.55, z + 0.5],
s: [1.02, h + 0.55, 1.02],
color: `#${color.getHexString()}`
});
}
}
const terrainMaterial = new THREE.MeshStandardMaterial({
roughness: 0.95,
metalness: 0,
vertexColors: true
});
makeInstanced(terrainItems, terrainMaterial, {
castShadow: false,
receiveShadow: true,
colors: true
});
// Sea plane and low horizon.
const seaMaterial = new THREE.MeshStandardMaterial({
color: seaColor,
roughness: 0.38,
metalness: 0.06
});
const sea = new THREE.Mesh(new THREE.PlaneGeometry(300, 300), seaMaterial);
sea.rotation.x = -Math.PI / 2;
sea.position.set(30, -0.82, -10);
sea.receiveShadow = true;
scene.add(sea);
const waveItems = [];
for (let i = 0; i < 85; i++) {
waveItems.push({
p: [rand(8, 65), -0.785, rand(-62, 50)],
s: [rand(1.2, 5.5), 0.018, 0.07],
r: [0, rand(-0.12, 0.12), 0]
});
}
const waveMaterial = new THREE.MeshBasicMaterial({
color: "#c5f1f5",
transparent: true,
opacity: 0.22,
depthWrite: false
});
makeInstanced(waveItems, waveMaterial, {
castShadow: false,
receiveShadow: false
});
// Each route is a chain of little cubes rather than a smooth tube.
for (const [key, data] of Object.entries(routeData)) {
const items = [];
const count = 310;
for (let i = 0; i < count; i++) {
const p = data.curve.getPointAt(i / (count - 1));
items.push({
p: [p.x, p.y + 0.06, p.z],
s: [0.23, 0.12, 0.23]
});
}
const material = new THREE.MeshStandardMaterial({
color: data.color,
emissive: data.color,
emissiveIntensity: 0.22,
roughness: 0.62,
transparent: true,
opacity: key === "coast" ? 0.98 : 0.23,
depthWrite: false
});
routeMeshes[key] = makeInstanced(items, material, {
castShadow: false,
receiveShadow: false
});
}
// The second route gains a visible staircase rhythm.
const stairItems = [];
const stairCurve = routeCurves.stairs;
for (let i = 0; i < 155; i++) {
const t = i / 154;
const p = stairCurve.getPointAt(t);
const tangent = stairCurve.getTangentAt(t);
const yaw = Math.atan2(tangent.x, tangent.z);
stairItems.push({
p: [p.x, p.y - 0.035, p.z],
s: [0.78, 0.16, 1.05],
r: [0, yaw, 0]
});
}
const stairMaterial = new THREE.MeshStandardMaterial({
color: "#f0e4ca",
roughness: 0.9
});
makeInstanced(stairItems, stairMaterial, {
castShadow: true,
receiveShadow: true
});
// ------------------------------------------------------------
// Sidi Bou Said architecture
// ------------------------------------------------------------
function distanceToRoutes(x, z) {
let min = Infinity;
for (const samples of Object.values(routeSamples)) {
for (let i = 0; i < samples.length; i += 4) {
const p = samples[i];
const dx = p.x - x;
const dz = p.z - z;
const d = dx * dx + dz * dz;
if (d < min) min = d;
}
}
return Math.sqrt(min);
}
const bodyItems = [];
const roofItems = [];
const blueItems = [];
const domeItems = [];
const whiteItems = [];
const cafeItems = [];
const blueBodies = [];
const cafePoint = routeCurves.coast.getPointAt(0.34);
const cafeTangent = routeCurves.coast.getTangentAt(0.34);
const cafePosition = cafePoint.clone().add(
new THREE.Vector3(-cafeTangent.z, 0, cafeTangent.x).multiplyScalar(2.25)
);
for (let x = -18.5; x <= 1.6; x += 4.15) {
for (let z = -20; z <= 18.5; z += 4.0) {
const px = x + rand(-0.55, 0.55);
const pz = z + rand(-0.55, 0.55);
if (distanceToRoutes(px, pz) < 2.15) continue;
if (Math.hypot(px - cafePosition.x, pz - cafePosition.z) < 3.2) continue;
if (Math.hypot(px - commonEndX, pz - commonEndZ) < 4.0) continue;
const w = rand(1.8, 2.8);
const d = rand(1.7, 2.65);
const h = rand(1.8, 3.7);
const base = terrainHeight(px, pz);
const blueHouse = random() < 0.075;
const bodyItem = {
p: [px, base + h / 2, pz],
s: [w, h, d]
};
if (blueHouse) {
bodyItem.color = "#176be8";
blueBodies.push(bodyItem);
} else {
const white = new THREE.Color("#f7f1e4");
white.offsetHSL(0, 0, rand(-0.035, 0.025));
bodyItem.color = `#${white.getHexString()}`;
bodyItems.push(bodyItem);
}
roofItems.push({
p: [px, base + h + 0.12, pz],
s: [w + 0.2, 0.23, d + 0.2],
color: blueHouse ? "#1258be" : "#fffaf0"
});
// Blue front door.
blueItems.push({
p: [px, base + 0.57, pz + d / 2 + 0.045],
s: [0.43, 1.13, 0.08]
});
// Front windows.
blueItems.push({
p: [px - w * 0.29, base + 1.52, pz + d / 2 + 0.045],
s: [0.46, 0.63, 0.075]
});
blueItems.push({
p: [px + w * 0.29, base + 1.52, pz + d / 2 + 0.045],
s: [0.46, 0.63, 0.075]
});
// East-facing window catches the main light.
blueItems.push({
p: [px + w / 2 + 0.045, base + 1.48, pz + 0.15],
s: [0.075, 0.63, 0.47]
});
// An occasional upper cubic storey.
if (random() < 0.23) {
const upperH = rand(1.0, 1.7);
const upperW = w * rand(0.5, 0.72);
bodyItems.push({
p: [px + rand(-0.25, 0.25), base + h + upperH / 2 + 0.23, pz],
s: [upperW, upperH, d * 0.72],
color: "#fffaf0"
});
roofItems.push({
p: [px, base + h + upperH + 0.34, pz],
s: [upperW + 0.16, 0.2, d * 0.72 + 0.16],
color: "#ffffff"
});
}
// Stepped cubic domes.
if (random() < 0.16) {
const domeColor = random() < 0.48 ? "#176be8" : "#fffaf0";
const layers = 4;
for (let layer = 0; layer < layers; layer++) {
const ratio = 0.78 - layer * 0.2;
domeItems.push({
p: [px, base + h + 0.42 + layer * 0.29, pz],
s: [w * ratio, 0.3, d * ratio],
color: domeColor
});
}
}
}
}
const bodyMaterial = new THREE.MeshStandardMaterial({
color: "#ffffff",
roughness: 0.88,
vertexColors: true
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: "#ffffff",
roughness: 0.82,
vertexColors: true
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: "#176be8",
roughness: 0.64
});
makeInstanced(bodyItems, bodyMaterial, { colors: true });
makeInstanced(blueBodies, bodyMaterial, { colors: true });
makeInstanced(roofItems, roofMaterial, { colors: true });
makeInstanced(domeItems, roofMaterial, { colors: true });
makeInstanced(blueItems, accentMaterial);
// Low white sea wall.
const wallItems = [];
for (let z = -22; z <= 21; z += 1.65) {
wallItems.push({
p: [7.05, 0.08, z],
s: [0.38, 1.15, 1.48],
color: "#f9f1df"
});
}
makeInstanced(wallItems, roofMaterial, { colors: true });
// ------------------------------------------------------------
// Shared viewpoint terrace
// ------------------------------------------------------------
const endBase = terrainHeight(commonEndX, commonEndZ);
const viewpointItems = [
{ p: [commonEndX, endBase + 0.14, commonEndZ], s: [4.3, 0.28, 3.8], color: "#fff9ec" },
{ p: [commonEndX - 1.75, endBase + 0.7, commonEndZ], s: [0.28, 1.2, 3.7], color: "#fff9ec" },
{ p: [commonEndX + 1.75, endBase + 0.7, commonEndZ], s: [0.28, 1.2, 3.7], color: "#fff9ec" },
{ p: [commonEndX, endBase + 0.7, commonEndZ + 1.65], s: [3.7, 1.2, 0.28], color: "#fff9ec" },
{ p: [commonEndX - 0.85, endBase + 1.55, commonEndZ - 1.5], s: [0.25, 2.7, 0.25], color: "#176be8" },
{ p: [commonEndX + 0.85, endBase + 1.55, commonEndZ - 1.5], s: [0.25, 2.7, 0.25], color: "#176be8" },
{ p: [commonEndX, endBase + 2.75, commonEndZ - 1.5], s: [2.15, 0.25, 0.3], color: "#176be8" },
{ p: [commonEndX, endBase + 2.96, commonEndZ - 1.5], s: [1.55, 0.2, 0.3], color: "#176be8" }
];
makeInstanced(viewpointItems, roofMaterial, { colors: true });
// ------------------------------------------------------------
// Cafe terrace, trees, clouds and boat
// ------------------------------------------------------------
const cafeBase = terrainHeight(cafePosition.x, cafePosition.z);
cafeItems.push(
{ p: [cafePosition.x, cafeBase + 0.55, cafePosition.z], s: [0.18, 1.1, 0.18], color: "#176be8" },
{ p: [cafePosition.x, cafeBase + 1.23, cafePosition.z], s: [1.65, 0.22, 1.65], color: "#f16f52" },
{ p: [cafePosition.x, cafeBase + 1.39, cafePosition.z], s: [1.1, 0.16, 1.1], color: "#f16f52" },
{ p: [cafePosition.x, cafeBase + 1.53, cafePosition.z], s: [0.5, 0.12, 0.5], color: "#f16f52" }
);
makeInstanced(cafeItems, roofMaterial, { colors: true });
const trunkItems = [];
const leafItems = [];
for (let i = 0; i < 52; i++) {
const x = rand(-18, 4);
const z = rand(-20, 18);
if (distanceToRoutes(x, z) < 2.1) continue;
if (Math.hypot(x - commonEndX, z - commonEndZ) < 3.2) continue;
const base = terrainHeight(x, z);
const h = rand(1.1, 2.0);
trunkItems.push({
p: [x, base + h / 2, z],
s: [0.22, h, 0.22]
});
leafItems.push(
{ p: [x, base + h + 0.15, z], s: [1.35, 0.85, 1.2] },
{ p: [x - 0.3, base + h + 0.58, z + 0.05], s: [0.8, 0.7, 0.8] },
{ p: [x + 0.28, base + h + 0.42, z - 0.1], s: [0.9, 0.65, 0.75] }
);
}
const trunkMaterial = new THREE.MeshStandardMaterial({
color: "#796044",
roughness: 1
});
const leafMaterial = new THREE.MeshStandardMaterial({
color: "#668e64",
roughness: 0.95
});
makeInstanced(trunkItems, trunkMaterial);
makeInstanced(leafItems, leafMaterial);
const cloudItems = [];
for (let i = 0; i < 14; i++) {
const x = rand(-25, 55);
const y = rand(15, 24);
const z = rand(-55, -28);
cloudItems.push(
{ p: [x, y, z], s: [rand(2.5, 5.5), 0.85, 1.5] },
{ p: [x + 1.2, y + 0.55, z], s: [2.6, 1.2, 1.7] },
{ p: [x - 1.3, y + 0.35, z], s: [2.2, 0.9, 1.4] }
);
}
const cloudMaterial = new THREE.MeshStandardMaterial({
color: "#fffaf0",
roughness: 1,
fog: true
});
makeInstanced(cloudItems, cloudMaterial, {
castShadow: false,
receiveShadow: false
});
const boat = new THREE.Group();
const boatWhite = new THREE.MeshStandardMaterial({ color: "#fffaf0", roughness: 0.7 });
const boatBlue = new THREE.MeshStandardMaterial({ color: "#176be8", roughness: 0.55 });
const hull = new THREE.Mesh(unitBox, boatWhite);
hull.scale.set(2.3, 0.65, 0.85);
boat.add(hull);
const cabin = new THREE.Mesh(unitBox, boatWhite);
cabin.scale.set(1.1, 0.8, 0.7);
cabin.position.set(-0.2, 0.62, 0);
boat.add(cabin);
const stripe = new THREE.Mesh(unitBox, boatBlue);
stripe.scale.set(2.33, 0.18, 0.87);
stripe.position.y = -0.06;
boat.add(stripe);
boat.position.set(15, -0.22, 8);
boat.rotation.y = -0.35;
scene.add(boat);
// ------------------------------------------------------------
// Journey marker and route UI
// ------------------------------------------------------------
const marker = new THREE.Group();
const markerMaterial = new THREE.MeshStandardMaterial({
color: routeData.coast.color,
emissive: routeData.coast.color,
emissiveIntensity: 0.45,
roughness: 0.35
});
const markerCore = new THREE.Mesh(unitBox, markerMaterial);
markerCore.scale.setScalar(0.65);
markerCore.castShadow = true;
marker.add(markerCore);
const markerTopMaterial = new THREE.MeshStandardMaterial({
color: "#fffdf5",
emissive: "#fffdf5",
emissiveIntensity: 0.12,
roughness: 0.5
});
const markerTop = new THREE.Mesh(unitBox, markerTopMaterial);
markerTop.scale.setScalar(0.3);
markerTop.position.y = 0.55;
marker.add(markerTop);
const markerHaloItems = [
{ p: [-0.48, 0, 0], s: [0.12, 0.12, 0.35] },
{ p: [0.48, 0, 0], s: [0.12, 0.12, 0.35] },
{ p: [0, 0, -0.48], s: [0.35, 0.12, 0.12] },
{ p: [0, 0, 0.48], s: [0.35, 0.12, 0.12] }
];
makeInstanced(markerHaloItems, markerMaterial, {
castShadow: false,
receiveShadow: false
}).parent?.remove;
// The halo is easier to keep attached to the marker as regular meshes.
scene.children = scene.children.filter((child) => child !== markerHaloItems);
let activeRoute = "coast";
let progress = 0;
let playing = false;
let profileMin = 0;
let profileMax = 1;
const cards = [...document.querySelectorAll(".route-card")];
const range = document.querySelector("#progress");
const playButton = document.querySelector("#play");
const playIcon = document.querySelector("#play-icon");
const playLabel = document.querySelector("#play-label");
const routeStatus = document.querySelector("#route-status");
const progressLabel = document.querySelector("#progress-label");
const profile = document.querySelector("#profile");
const profileArea = document.querySelector("#profile-area");
const profileLine = document.querySelector("#profile-line");
const profileCursor = document.querySelector("#profile-cursor");
const profileDot = document.querySelector("#profile-dot");
const toast = document.querySelector("#toast");
let toastTimer;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
function profileY(worldY) {
const t = (worldY - profileMin) / Math.max(0.001, profileMax - profileMin);
return 64 - t * 52;
}
function updateProfile() {
const curve = routeCurves[activeRoute];
const points = [];
const ys = [];
for (let i = 0; i <= 80; i++) {
const p = curve.getPointAt(i / 80);
const x = (i / 80) * 300;
const y = profileY(p.y);
points.push(`${x.toFixed(2)},${y.toFixed(2)}`);
ys.push(p.y);
}
profileMin = Math.min(...ys);
profileMax = Math.max(...ys);
for (let i = 0; i <= 80; i++) {
const p = curve.getPointAt(i / 80);
points[i] = `${((i / 80) * 300).toFixed(2)},${profileY(p.y).toFixed(2)}`;
}
profileLine.setAttribute("points", points.join(" "));
profileArea.setAttribute("d", `M0,70 L${points.join(" L")} L300,70 Z`);
profile.style.color = routeData[activeRoute].color;
updateProgressDisplay();
}
function updateProgressDisplay() {
const curve = routeCurves[activeRoute];
const p = curve.getPointAt(progress);
const x = progress * 300;
const y = profileY(p.y);
range.value = String(Math.round(progress * 1000));
range.style.setProperty("--progress", `${progress * 100}%`);
progressLabel.textContent = `${Math.round(progress * 100)}% complete`;
profileCursor.setAttribute("x1", x);
profileCursor.setAttribute("x2", x);
profileDot.setAttribute("cx", x);
profileDot.setAttribute("cy", y);
marker.position.set(p.x, p.y + 0.42, p.z);
}
function selectRoute(key) {
activeRoute = key;
progress = 0;
playing = false;
const data = routeData[key];
document.documentElement.style.setProperty("--active-route", data.color);
cards.forEach((card) => {
const isActive = card.dataset.route === key;
card.classList.toggle("active", isActive);
card.setAttribute("aria-pressed", String(isActive));
});
for (const [routeKey, mesh] of Object.entries(routeMeshes)) {
const selected = routeKey === key;
mesh.material.opacity = selected ? 0.98 : 0.2;
mesh.material.emissiveIntensity = selected ? 0.32 : 0.05;
}
markerMaterial.color.set(data.color);
markerMaterial.emissive.set(data.color);
routeStatus.textContent = data.name;
playIcon.textContent = "▶";
playLabel.textContent = "Replay this route";
updateProfile();
}
cards.forEach((card) => {
card.addEventListener("click", () => selectRoute(card.dataset.route));
});
range.addEventListener("input", () => {
playing = false;
progress = Number(range.value) / 1000;
playIcon.textContent = "▶";
playLabel.textContent = progress >= 1 ? "Replay this route" : "Play this route";
updateProgressDisplay();
});
playButton.addEventListener("click", () => {
if (progress >= 0.999) progress = 0;
playing = !playing;
playIcon.textContent = playing ? "Ⅱ" : "▶";
playLabel.textContent = playing
? "Pause journey"
: progress >= 1
? "Replay this route"
: "Continue journey";
});
document.querySelector("#reset").addEventListener("click", () => {
progress = 0;
playing = false;
playIcon.textContent = "▶";
playLabel.textContent = "Replay this route";
updateProgressDisplay();
});
// ------------------------------------------------------------
// Projected map labels
// ------------------------------------------------------------
const labelDefinitions = [
{
text: "Marina Steps · Start",
position: new THREE.Vector3(commonStartX, terrainHeight(commonStartX, commonStartZ) + 1.4, commonStartZ),
color: "#176be8"
},
{
text: "Blue Door Overlook",
position: new THREE.Vector3(commonEndX, endBase + 3.8, commonEndZ),
color: "#176be8"
},
{
text: "Café Terrace",
position: new THREE.Vector3(cafePosition.x, cafeBase + 2.1, cafePosition.z),
color: "#f16f52"
}
];
const labelElements = labelDefinitions.map((definition) => {
const element = document.createElement("div");
element.className = "map-label";
element.textContent = definition.text;
element.style.setProperty("--label-color", definition.color);
document.querySelector("#labels").appendChild(element);
return element;
});
function updateLabels() {
labelDefinitions.forEach((definition, index) => {
const projected = definition.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
labelElements[index].style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
labelElements[index].style.transform =
`translate(${x}px, ${y}px) translate(-50%, -50%)`;
});
}
// ------------------------------------------------------------
// Direct camera controls — no examples/addons
// ------------------------------------------------------------
let azimuth = 0.69;
let elevation = 0.59;
let radius = 58;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const horizontal = Math.cos(elevation) * radius;
camera.position.set(
cameraTarget.x + Math.sin(azimuth) * horizontal,
cameraTarget.y + Math.sin(elevation) * radius,
cameraTarget.z + Math.cos(azimuth) * horizontal
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
azimuth = 0.69;
elevation = 0.59;
radius = innerWidth < 700 ? 70 : 58;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
azimuth -= dx * 0.005;
elevation = THREE.MathUtils.clamp(elevation + dy * 0.0035, 0.42, 0.76);
updateCamera();
});
function stopDragging(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(radius + event.deltaY * 0.035, 43, 82);
updateCamera();
},
{ passive: false }
);
document.querySelector("#reset-camera").addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.fov = innerWidth < 700 ? 52 : 46;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
}
addEventListener("resize", resize);
resetCamera();
selectRoute("coast");
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
if (playing) {
progress = Math.min(1, progress + dt / 12);
if (progress >= 1) {
playing = false;
playIcon.textContent = "▶";
playLabel.textContent = "Replay this route";
showToast("Blue Door Overlook reached · the horizon is unchanged");
}
updateProgressDisplay();
}
const markerBob = playing ? Math.sin(elapsed * 5.5) * 0.08 : 0;
marker.position.y += markerBob;
markerTop.rotation.y += dt * 1.4;
boat.position.y = -0.22 + Math.sin(elapsed * 1.2) * 0.035;
boat.rotation.z = Math.sin(elapsed * 0.8) * 0.012;
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```