Prompt specification
Build or customize the Three.js 3D scene "Transit interchange route puzzle" 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: Transit interchange route puzzle
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/transit-interchange-route-puzzle-x0408
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this low-poly scene: Create a small interchange linking bus, tram, and footpath; users position boarding zones so transfers remain connected while service vehicles follow separate consistent movement lanes.
Working Three.js Starter (transit-interchange-route-puzzle-x0408):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Crosslink — Interchange Planner</title>
<style>
:root {
--ink: #17332f;
--muted: #607873;
--panel: rgba(250, 252, 248, 0.93);
--line: rgba(23, 51, 47, 0.12);
--bus: #f5a524;
--tram: #16aeb4;
--good: #218b68;
--bad: #dc5a47;
--shadow: 0 18px 55px rgba(27, 55, 50, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e3;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.topbar {
position: fixed;
z-index: 10;
top: 22px;
left: 24px;
pointer-events: none;
text-shadow: 0 1px 15px rgba(255, 255, 255, 0.55);
}
.eyebrow {
margin-bottom: 4px;
color: #477169;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.topbar h1 {
margin: 0;
font-size: clamp(27px, 3.2vw, 42px);
font-weight: 850;
letter-spacing: -0.055em;
line-height: 1;
}
.topbar p {
margin: 8px 0 0;
color: #4e7069;
font-size: 13px;
font-weight: 650;
}
.mode-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 12px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 999px;
background: rgba(250, 252, 248, 0.78);
box-shadow: 0 7px 25px rgba(37, 70, 63, 0.1);
backdrop-filter: blur(10px);
color: #466960;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.mode-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--good);
content: "";
box-shadow: 0 0 0 4px rgba(33, 139, 104, 0.13);
}
.panel {
position: fixed;
z-index: 20;
top: 22px;
right: 22px;
width: min(348px, calc(100vw - 44px));
max-height: calc(100vh - 44px);
overflow: auto;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 22px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(1.1);
scrollbar-width: thin;
}
.status {
display: grid;
grid-template-columns: 38px 1fr;
gap: 11px;
align-items: center;
padding: 13px;
border: 1px solid rgba(33, 139, 104, 0.15);
border-radius: 15px;
background: rgba(33, 139, 104, 0.075);
transition: 180ms ease;
}
.status.warning {
border-color: rgba(220, 90, 71, 0.2);
background: rgba(220, 90, 71, 0.08);
}
.status-icon {
display: grid;
width: 38px;
height: 38px;
place-items: center;
border-radius: 12px;
background: var(--good);
color: white;
font-size: 20px;
font-weight: 900;
}
.status.warning .status-icon {
background: var(--bad);
}
.status strong {
display: block;
margin-bottom: 2px;
font-size: 13px;
}
.status small {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 18px 2px 8px;
color: #6b817c;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-title span {
color: #99aaa6;
font-size: 9px;
letter-spacing: 0.06em;
}
.stop-list {
display: grid;
gap: 7px;
}
.stop-card {
position: relative;
display: grid;
grid-template-columns: 33px 1fr auto;
gap: 10px;
align-items: center;
min-height: 52px;
padding: 9px 10px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.65);
cursor: pointer;
transition: 150ms ease;
}
.stop-card:hover,
.stop-card:focus-visible {
transform: translateY(-1px);
border-color: rgba(23, 51, 47, 0.25);
outline: none;
}
.stop-card.selected {
border-color: rgba(23, 51, 47, 0.33);
background: white;
box-shadow: 0 7px 20px rgba(31, 64, 57, 0.08);
}
.stop-card.invalid {
border-color: rgba(220, 90, 71, 0.38);
background: rgba(220, 90, 71, 0.045);
}
.stop-card::before {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 3px;
background: var(--zone-color);
content: "";
}
.zone-icon {
display: grid;
width: 31px;
height: 31px;
place-items: center;
border-radius: 10px;
background: color-mix(in srgb, var(--zone-color) 18%, white);
color: color-mix(in srgb, var(--zone-color) 82%, #17332f);
font-size: 10px;
font-weight: 900;
}
.stop-name {
display: block;
font-size: 12px;
font-weight: 800;
}
.stop-sub {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.link-state {
display: flex;
align-items: center;
gap: 5px;
color: var(--good);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.link-state::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
content: "";
}
.stop-card.invalid .link-state {
color: var(--bad);
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.metric {
min-height: 72px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.55);
}
.metric-label {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric-value {
display: block;
font-size: 19px;
font-weight: 850;
letter-spacing: -0.04em;
}
.metric-note {
display: block;
margin-top: 2px;
color: #80938f;
font-size: 9px;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
button {
min-height: 39px;
border: 1px solid var(--line);
border-radius: 11px;
background: white;
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 800;
transition: 140ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(23, 51, 47, 0.24);
box-shadow: 0 6px 16px rgba(31, 64, 57, 0.08);
}
button:disabled {
cursor: default;
opacity: 0.45;
}
button.primary {
border-color: var(--ink);
background: var(--ink);
color: white;
}
.camera-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 8px;
}
.legend {
display: grid;
gap: 7px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.legend-row {
display: flex;
align-items: center;
justify-content: space-between;
color: #607873;
font-size: 10px;
}
.legend-label {
display: flex;
align-items: center;
gap: 7px;
}
.legend-line {
width: 22px;
height: 3px;
border-radius: 4px;
background: var(--bus);
}
.legend-line.tram {
background: var(--tram);
}
.legend-line.walk {
background: #c9bfa5;
}
.legend-row b {
color: #849792;
font-size: 9px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.help {
position: fixed;
z-index: 10;
bottom: 20px;
left: 24px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: min(650px, calc(100vw - 48px));
pointer-events: none;
}
.hint {
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 999px;
background: rgba(250, 252, 248, 0.8);
box-shadow: 0 7px 25px rgba(37, 70, 63, 0.08);
backdrop-filter: blur(10px);
color: #57736d;
font-size: 10px;
font-weight: 700;
}
.hint strong {
color: var(--ink);
}
.toast {
position: fixed;
z-index: 30;
bottom: 24px;
left: 50%;
padding: 10px 15px;
border-radius: 999px;
background: #17332f;
color: white;
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 750;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 15px);
transition: 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.topbar {
top: 15px;
left: 16px;
}
.topbar p,
.mode-badge {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 48vh;
padding: 13px;
border-radius: 18px;
}
.stop-list {
grid-template-columns: 1fr 1fr;
}
.stop-card {
grid-template-columns: 30px 1fr;
}
.link-state {
grid-column: 2;
}
.metrics {
grid-template-columns: repeat(4, 1fr);
}
.metric {
min-height: 62px;
padding: 8px;
}
.metric-value {
font-size: 15px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly transit interchange"></canvas>
<header class="topbar">
<div class="eyebrow">Urban Mobility Lab</div>
<h1>Crosslink</h1>
<p>Bus, tram and pedestrian interchange planner</p>
<div class="mode-badge">Design mode</div>
</header>
<aside class="panel" aria-label="Interchange planning controls">
<div id="status" class="status">
<div class="status-icon" id="statusIcon">✓</div>
<div>
<strong id="statusTitle">Transfer network connected</strong>
<small id="statusText">All four boarding zones link to the pedestrian network.</small>
</div>
</div>
<div class="section-title">
Boarding zones
<span>Drag in the scene</span>
</div>
<div class="stop-list">
<div class="stop-card selected" data-zone="busNorth" tabindex="0" style="--zone-color: var(--bus)">
<div class="zone-icon">B1</div>
<div>
<span class="stop-name">Bus · North</span>
<span class="stop-sub">Amber boarding pad</span>
</div>
<div class="link-state">Linked</div>
</div>
<div class="stop-card" data-zone="busSouth" tabindex="0" style="--zone-color: var(--bus)">
<div class="zone-icon">B2</div>
<div>
<span class="stop-name">Bus · South</span>
<span class="stop-sub">Amber boarding pad</span>
</div>
<div class="link-state">Linked</div>
</div>
<div class="stop-card" data-zone="tramWest" tabindex="0" style="--zone-color: var(--tram)">
<div class="zone-icon">T1</div>
<div>
<span class="stop-name">Tram · West</span>
<span class="stop-sub">Teal boarding pad</span>
</div>
<div class="link-state">Linked</div>
</div>
<div class="stop-card" data-zone="tramEast" tabindex="0" style="--zone-color: var(--tram)">
<div class="zone-icon">T2</div>
<div>
<span class="stop-name">Tram · East</span>
<span class="stop-sub">Teal boarding pad</span>
</div>
<div class="link-state">Linked</div>
</div>
</div>
<div class="section-title">Live checks</div>
<div class="metrics">
<div class="metric">
<span class="metric-label">Linked zones</span>
<span class="metric-value" id="linkedMetric">4 / 4</span>
<span class="metric-note">pedestrian access</span>
</div>
<div class="metric">
<span class="metric-label">Total walk</span>
<span class="metric-value" id="walkMetric">—</span>
<span class="metric-note">to central hub</span>
</div>
<div class="metric">
<span class="metric-label">Service lanes</span>
<span class="metric-value">Clear</span>
<span class="metric-note">bus / tram isolated</span>
</div>
<div class="metric">
<span class="metric-label">Lane conflict</span>
<span class="metric-value">0</span>
<span class="metric-note">crossing conflicts</span>
</div>
</div>
<div class="actions">
<button class="primary" id="autoLink">Auto-connect</button>
<button id="resetZones">Reset zones</button>
</div>
<div class="camera-row">
<button id="isoView">Isometric view</button>
<button id="planView">Plan view</button>
</div>
<div class="actions">
<button id="pause">Pause services</button>
<button id="resetView">Reset camera</button>
</div>
<div class="legend">
<div class="legend-row">
<span class="legend-label"><i class="legend-line"></i> Bus reservation</span>
<b>One-way loop</b>
</div>
<div class="legend-row">
<span class="legend-label"><i class="legend-line tram"></i> Tram reservation</span>
<b>Eastbound</b>
</div>
<div class="legend-row">
<span class="legend-label"><i class="legend-line walk"></i> Walk network</span>
<b>Shared plaza</b>
</div>
</div>
</aside>
<div class="help">
<div class="hint"><strong>Drag</strong> a colored zone to reposition it</div>
<div class="hint"><strong>Drag</strong> empty space to orbit</div>
<div class="hint"><strong>Right-drag</strong> to pan · <strong>Scroll</strong> to zoom</div>
</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";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
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.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#dce8e3");
scene.fog = new THREE.Fog("#dce8e3", 115, 220);
const camera = new THREE.OrthographicCamera(-50, 50, 30, -30, 0.1, 300);
const cameraState = {
target: new THREE.Vector3(-2, 0, 3),
azimuth: 0.58,
polar: 0.88,
radius: 100,
zoom: 1
};
const defaultCamera = {
target: new THREE.Vector3(-2, 0, 3),
azimuth: 0.58,
polar: 0.88,
zoom: 1
};
function updateCamera() {
const s = Math.sin(cameraState.polar);
const c = Math.cos(cameraState.polar);
camera.position.set(
cameraState.target.x +
cameraState.radius * s * Math.sin(cameraState.azimuth),
cameraState.target.y + cameraState.radius * c,
cameraState.target.z +
cameraState.radius * s * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
const aspect = window.innerWidth / window.innerHeight;
const viewHeight = window.innerWidth < 760 ? 66 : 58;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
updateCamera();
const hemi = new THREE.HemisphereLight("#f4fff9", "#78948b", 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight("#fff4dd", 3.2);
sun.position.set(-30, 65, 32);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -75;
sun.shadow.camera.right = 75;
sun.shadow.camera.top = 65;
sun.shadow.camera.bottom = -65;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 160;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b9e5e8", 0.65);
fill.position.set(45, 25, -40);
scene.add(fill);
const palette = {
ground: "#b9ccb7",
groundDark: "#9eb89e",
road: "#4b5655",
roadEdge: "#697270",
path: "#ddd4b9",
pathLight: "#eee6cd",
bus: "#f5a524",
busDark: "#b86f14",
tram: "#16aeb4",
tramDark: "#087b82",
green: "#218b68",
red: "#dc5a47",
ink: "#17332f",
cream: "#f5f0df"
};
function material(color, roughness = 0.85, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
ground: material(palette.ground),
groundDark: material(palette.groundDark),
road: material(palette.road, 0.96),
path: material(palette.path),
pathLight: material(palette.pathLight),
bus: material(palette.bus, 0.65),
busDark: material(palette.busDark, 0.72),
tram: material(palette.tram, 0.52, 0.12),
tramDark: material(palette.tramDark, 0.62, 0.08),
glass: material("#294746", 0.35, 0.15),
black: material("#26312f", 0.92),
white: material("#f7f5e9", 0.8),
trunk: material("#80634c"),
leafA: material("#4f8a68"),
leafB: material("#679b72"),
leafC: material("#3f765b"),
buildingA: material("#d8d4c4"),
buildingB: material("#c2cbc0"),
buildingC: material("#d2bba5"),
roof: material("#6b7975")
};
function addBox(parent, w, h, d, mat, x = 0, y = 0, z = 0, shadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
segments,
mat,
x = 0,
y = 0,
z = 0,
shadow = true
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, background, foreground = "#ffffff", width = 8) {
const c = document.createElement("canvas");
c.width = 512;
c.height = 128;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = background;
ctx.beginPath();
ctx.roundRect(5, 5, 502, 118, 24);
ctx.fill();
ctx.strokeStyle = "rgba(255,255,255,.35)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = foreground;
ctx.font = "800 45px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 65);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, width * 0.25, 1);
sprite.renderOrder = 20;
return sprite;
}
function addPathSegment(x1, z1, x2, z2, width = 3.2, mat = materials.path) {
const length = Math.hypot(x2 - x1, z2 - z1);
const mesh = addBox(
scene,
length,
0.16,
width,
mat,
(x1 + x2) / 2,
0.035,
(z1 + z2) / 2
);
mesh.rotation.y = -Math.atan2(z2 - z1, x2 - x1);
mesh.castShadow = false;
return mesh;
}
function addNetworkNode(x, z, color = palette.green) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.55, 0.09, 6, 24),
material(color, 0.7)
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.2, z);
ring.receiveShadow = true;
scene.add(ring);
return ring;
}
// Ground and raised diorama base.
addBox(scene, 130, 1.2, 96, materials.groundDark, 0, -0.72, 0);
addBox(scene, 129, 0.35, 95, materials.ground, 0, -0.08, 0);
// Fixed pedestrian network.
addPathSegment(-13.5, 0, 36, 0, 4.1, materials.pathLight);
addPathSegment(-13.5, -10, -13.5, 10, 3.1, materials.path);
addPathSegment(10, 0, 10, 19.5, 3.1, materials.path);
addPathSegment(32, 0, 32, 19.5, 3.1, materials.path);
addPathSegment(-15.7, -10, -13.5, -10, 2.6, materials.path);
addPathSegment(-15.7, 10, -13.5, 10, 2.6, materials.path);
// Central plaza and transfer hub.
const plaza = addCylinder(
scene,
6.5,
6.5,
0.18,
18,
materials.pathLight,
18,
0.05,
5.2
);
plaza.castShadow = false;
const hubRing = new THREE.Mesh(
new THREE.TorusGeometry(1.35, 0.11, 7, 36),
material(palette.green)
);
hubRing.rotation.x = Math.PI / 2;
hubRing.position.set(18, 0.22, 0);
scene.add(hubRing);
addCylinder(scene, 0.42, 0.55, 4.3, 8, materials.ink, 18, 2.15, 0);
const hubSign = makeTextSprite("HUB", palette.ink, "#ffffff", 4.6);
hubSign.position.set(18, 4.65, 0);
scene.add(hubSign);
// Bus loop.
const busCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-45, 0, -20),
new THREE.Vector3(-25, 0, -20),
new THREE.Vector3(-20, 0, -16),
new THREE.Vector3(-20, 0, 16),
new THREE.Vector3(-25, 0, 20),
new THREE.Vector3(-45, 0, 20),
new THREE.Vector3(-50, 0, 16),
new THREE.Vector3(-50, 0, -16)
],
true,
"centripetal"
);
function makeRibbon(curve, width, segments, mat) {
const positions = [];
const indices = [];
for (let i = 0; i < segments; i++) {
const u = i / segments;
const p = curve.getPointAt(u);
const t = curve.getTangentAt(u);
const nx = -t.z;
const nz = t.x;
const half = width / 2;
positions.push(
p.x + nx * half, 0.02, p.z + nz * half,
p.x - nx * half, 0.02, p.z - nz * half
);
const a = i * 2;
const b = ((i + 1) % segments) * 2;
indices.push(a, a + 1, b);
indices.push(b, a + 1, b + 1);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, mat);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
makeRibbon(busCurve, 6.4, 160, materials.road);
const busDashes = [];
for (let i = 0; i <= 220; i++) {
busDashes.push(busCurve.getPointAt(i / 220).clone().setY(0.12));
}
const busDashGeometry = new THREE.BufferGeometry().setFromPoints(busDashes);
const busDashLine = new THREE.Line(
busDashGeometry,
new THREE.LineDashedMaterial({
color: palette.bus,
dashSize: 1.2,
gapSize: 1.4,
transparent: true,
opacity: 0.8
})
);
busDashLine.computeLineDistances();
scene.add(busDashLine);
// Tram reservation, track and sleepers.
addBox(scene, 57, 0.16, 5.8, materials.road, 25.5, 0.03, 23);
addBox(scene, 57, 0.2, 0.16, materials.tramDark, 25.5, 0.18, 22.1);
addBox(scene, 57, 0.2, 0.16, materials.tramDark, 25.5, 0.18, 23.9);
for (let x = -2; x < 55; x += 1.8) {
addBox(scene, 0.24, 0.12, 3.35, materials.roadEdge, x, 0.16, 23);
}
function makeArrow(x, z, rotation, color) {
const shape = new THREE.Shape();
shape.moveTo(-0.85, -0.2);
shape.lineTo(0.2, -0.2);
shape.lineTo(0.2, -0.52);
shape.lineTo(0.9, 0);
shape.lineTo(0.2, 0.52);
shape.lineTo(0.2, 0.2);
shape.lineTo(-0.85, 0.2);
shape.closePath();
const mesh = new THREE.Mesh(
new THREE.ShapeGeometry(shape),
new THREE.MeshBasicMaterial({
color,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.85
})
);
mesh.rotation.x = -Math.PI / 2;
mesh.rotation.z = -rotation;
mesh.position.set(x, 0.18, z);
scene.add(mesh);
return mesh;
}
[0.06, 0.2, 0.36, 0.55, 0.72, 0.88].forEach((u) => {
const p = busCurve.getPointAt(u);
const t = busCurve.getTangentAt(u);
makeArrow(p.x, p.z, Math.atan2(t.z, t.x), palette.bus);
});
[2, 12, 26, 41, 51].forEach((x) => {
makeArrow(x, 23, 0, palette.tram);
});
const busLaneLabel = makeTextSprite(
"BUS LOOP • ONE WAY",
palette.busDark,
"#ffffff",
11
);
busLaneLabel.position.set(-35, 3.8, -20);
scene.add(busLaneLabel);
const tramLaneLabel = makeTextSprite(
"TRAM • EASTBOUND",
palette.tramDark,
"#ffffff",
10
);
tramLaneLabel.position.set(25, 3.8, 23);
scene.add(tramLaneLabel);
// Low-poly city context.
function addBuilding(x, z, w, d, h, wallMat, roofMat = materials.roof) {
const group = new THREE.Group();
group.position.set(x, 0, z);
addBox(group, w, h, d, wallMat, 0, h / 2, 0);
addBox(group, w + 0.35, 0.45, d + 0.35, roofMat, 0, h + 0.2, 0);
const windowMat = material("#78928e", 0.55, 0.05);
const rows = Math.max(1, Math.floor(h / 3));
const columns = Math.max(1, Math.floor(w / 3));
for (let row = 0; row < rows; row++) {
for (let col = 0; col < columns; col++) {
const wx = -w / 2 + (col + 0.5) * (w / columns);
const wy = 2 + row * ((h - 2) / Math.max(1, rows));
addBox(group, 1.1, 0.8, 0.08, windowMat, wx, wy, d / 2 + 0.05, false);
}
}
scene.add(group);
return group;
}
addBuilding(-58, -18, 10, 9, 7, materials.buildingA);
addBuilding(-59, 17, 9, 11, 10, materials.buildingB);
addBuilding(-2, -18, 9, 8, 8, materials.buildingC);
addBuilding(12, -21, 10, 9, 12, materials.buildingB);
addBuilding(27, -20, 9, 8, 7, materials.buildingA);
addBuilding(41, -17, 8, 9, 10, materials.buildingC);
addBuilding(49, 3, 8, 10, 6, materials.buildingB);
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
addCylinder(group, 0.22, 0.3, 2.2, 6, materials.trunk, 0, 1.1, 0);
addCylinder(
group,
0,
1.65,
3.5,
7,
materials.leafA,
0,
3.1,
0
);
addCylinder(
group,
0,
1.25,
2.8,
7,
materials.leafB,
0.15,
4.5,
0.08
);
scene.add(group);
}
[
[-10, 4], [-10, -4], [3, 7], [7, 8], [14, 10],
[22, 10], [28, 7], [36, 8], [38, 14], [4, 14],
[-4, -10], [23, -11], [34, -10]
].forEach(([x, z], index) => {
addTree(x, z, 0.82 + (index % 3) * 0.08);
});
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
addBox(group, 3, 0.18, 0.65, materials.trunk, 0, 0.7, 0);
addBox(group, 3, 0.65, 0.14, materials.trunk, 0, 1.05, -0.3);
addBox(group, 0.18, 0.65, 0.55, materials.ink, -1.1, 0.32, 0);
addBox(group, 0.18, 0.65, 0.55, materials.ink, 1.1, 0.32, 0);
scene.add(group);
}
addBench(15.5, 4.5, 0);
addBench(20.5, 4.5, 0);
addBench(13.5, 8.2, Math.PI / 2);
// Small information kiosk in the plaza.
addCylinder(scene, 1.2, 1.4, 0.55, 8, materials.ink, 18, 0.35, 6.4);
addBox(scene, 1.8, 2.8, 0.35, materials.tram, 18, 1.9, 6.4);
const kioskLabel = makeTextSprite("i", palette.tram, "#ffffff", 2.5);
kioskLabel.position.set(18, 3.65, 6.4);
scene.add(kioskLabel);
// Streetlights.
function addStreetlight(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
addCylinder(group, 0.09, 0.12, 4.8, 6, materials.ink, 0, 2.4, 0);
addBox(group, 1.6, 0.12, 0.12, materials.ink, 0.7, 4.75, 0);
addBox(group, 0.75, 0.16, 0.42, materials.white, 1.2, 4.62, 0, false);
scene.add(group);
}
addStreetlight(-11, 2.5, Math.PI / 2);
addStreetlight(6, 2.5, Math.PI / 2);
addStreetlight(25, 2.5, Math.PI / 2);
addStreetlight(35, 2.5, Math.PI / 2);
// Bus vehicle.
function createBus() {
const group = new THREE.Group();
addBox(group, 7.2, 2.3, 2.65, materials.bus, 0, 1.45, 0);
addBox(group, 7.35, 0.42, 2.7, materials.busDark, 0, 0.55, 0);
addBox(group, 6.65, 0.95, 2.69, materials.glass, 0, 2.02, 0);
addBox(group, 1.75, 1.15, 2.68, materials.glass, 2.55, 1.72, 0);
addBox(group, 0.18, 1.35, 2.72, materials.busDark, -1.65, 1.8, 0);
addBox(group, 0.18, 1.35, 2.72, materials.busDark, 1.1, 1.8, 0);
addBox(group, 6.9, 0.18, 2.76, materials.busDark, 0, 2.72, 0);
[-2.25, 2.25].forEach((x) => {
[-1.32, 1.32].forEach((z) => {
const wheel = addCylinder(
group,
0.52,
0.52,
0.26,
10,
materials.black,
x,
0.52,
z
);
wheel.rotation.x = Math.PI / 2;
});
});
const label = makeTextSprite("BUS", palette.busDark, "#ffffff", 4);
label.position.set(0, 3.55, 0);
group.add(label);
group.traverse((obj) => {
if (obj.isMesh) {
obj.castShadow = true;
obj.receiveShadow = true;
}
});
scene.add(group);
return group;
}
// Tram vehicle.
function createTram() {
const group = new THREE.Group();
addBox(group, 11.8, 1.35, 2.5, materials.tram, 0, 1.08, 0);
addBox(group, 11.2, 1.2, 2.54, materials.glass, -0.15, 2.05, 0);
addBox(group, 11.8, 0.32, 2.58, materials.tramDark, 0, 0.4, 0);
addBox(group, 11.9, 0.22, 2.62, materials.tramDark, 0, 2.72, 0);
[-4.2, 4.2].forEach((x) => {
[-1.18, 1.18].forEach((z) => {
const wheel = addCylinder(
group,
0.42,
0.42,
0.22,
10,
materials.black,
x,
0.37,
z
);
wheel.rotation.x = Math.PI / 2;
});
});
const label = makeTextSprite("TRAM", palette.tramDark, "#ffffff", 4);
label.position.set(0, 3.5, 0);
group.add(label);
group.traverse((obj) => {
if (obj.isMesh) {
obj.castShadow = true;
obj.receiveShadow = true;
}
});
scene.add(group);
return group;
}
const busVehicle = createBus();
const tramVehicle = createTram();
// Boarding zone definitions.
const BUS_X = -15.7;
const TRAM_Z = 19.5;
const ACCESS_LIMIT = 6.5;
const HUB_X = 18;
const HUB_Z = 0;
const zones = [
{
id: "busNorth",
label: "B1",
color: palette.bus,
type: "bus",
initial: new THREE.Vector3(BUS_X, 0, -10),
anchor: new THREE.Vector3(BUS_X, 0, -10),
min: new THREE.Vector3(BUS_X, 0, -15),
max: new THREE.Vector3(BUS_X, 0, -1),
group: new THREE.Group(),
connector: new THREE.Group()
},
{
id: "busSouth",
label: "B2",
color: palette.bus,
type: "bus",
initial: new THREE.Vector3(BUS_X, 0, 10),
anchor: new THREE.Vector3(BUS_X, 0, 10),
min: new THREE.Vector3(BUS_X, 0, 1),
max: new THREE.Vector3(BUS_X, 0, 15),
group: new THREE.Group(),
connector: new THREE.Group()
},
{
id: "tramWest",
label: "T1",
color: palette.tram,
type: "tram",
initial: new THREE.Vector3(10, 0, TRAM_Z),
anchor: new THREE.Vector3(10, 0, TRAM_Z),
min: new THREE.Vector3(4, 0, TRAM_Z),
max: new THREE.Vector3(20, 0, TRAM_Z),
group: new THREE.Group(),
connector: new THREE.Group()
},
{
id: "tramEast",
label: "T2",
color: palette.tram,
type: "tram",
initial: new THREE.Vector3(32, 0, TRAM_Z),
anchor: new THREE.Vector3(32, 0, TRAM_Z),
min: new THREE.Vector3(24, 0, TRAM_Z),
max: new THREE.Vector3(40, 0, TRAM_Z),
group: new THREE.Group(),
connector: new THREE.Group()
}
];
const zoneById = new Map(zones.map((zone) => [zone.id, zone]));
const hitMeshes = [];
function createStop(zone) {
const group = zone.group;
const colorMat = material(zone.color, 0.62);
addCylinder(group, 1.18, 1.32, 0.24, 16, colorMat, 0, 0.2, 0);
addCylinder(
group,
0.78,
0.92,
0.08,
16,
materials.white,
0,
0.35,
0
);
addCylinder(group, 0.09, 0.12, 1.75, 8, materials.ink, -0.72, 1.05, 0);
addBox(group, 1.05, 0.86, 0.14, zone.color, -0.72, 2.05, 0);
const label = makeTextSprite(zone.label, zone.color, "#17332f", 2.7);
label.position.set(-0.72, 2.75, 0);
group.add(label);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.48, 0.08, 6, 32),
new THREE.MeshBasicMaterial({
color: zone.color,
transparent: true,
opacity: 0.95
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.44;
ring.visible = false;
group.add(ring);
zone.selectionRing = ring;
const hit = new THREE.Mesh(
new THREE.CylinderGeometry(1.8, 1.8, 0.8, 12),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
})
);
hit.position.y = 0.55;
hit.userData.zoneId = zone.id;
group.add(hit);
hitMeshes.push(hit);
scene.add(group);
scene.add(zone.connector);
addNetworkNode(zone.anchor.x, zone.anchor.z, zone.color);
}
zones.forEach(createStop);
// Highlight rails showing the allowed movement range.
const busRail = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(BUS_X, 0.25, -15),
new THREE.Vector3(BUS_X, 0.25, 15)
]),
new THREE.LineDashedMaterial({
color: palette.bus,
dashSize: 0.6,
gapSize: 0.45,
transparent: true,
opacity: 0.85
})
);
busRail.computeLineDistances();
busRail.visible = false;
scene.add(busRail);
const tramRail = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(4, 0.25, TRAM_Z),
new THREE.Vector3(40, 0.25, TRAM_Z)
]),
new THREE.LineDashedMaterial({
color: palette.tram,
dashSize: 0.6,
gapSize: 0.45,
transparent: true,
opacity: 0.85
})
);
tramRail.computeLineDistances();
tramRail.visible = false;
scene.add(tramRail);
let selectedZone = zones[0];
let paused = false;
let toastTimer;
function showToast(message) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 1800);
}
function rebuildConnector(zone) {
while (zone.connector.children.length) {
const child = zone.connector.children[0];
zone.connector.remove(child);
child.geometry?.dispose();
}
const valid = zone.current.distanceTo(zone.anchor) <= ACCESS_LIMIT;
const dx = zone.anchor.x - zone.current.x;
const dz = zone.anchor.z - zone.current.z;
const length = Math.hypot(dx, dz);
const mat = new THREE.MeshBasicMaterial({
color: valid ? palette.green : palette.red,
transparent: true,
opacity: valid ? 0.95 : 0.62
});
const strip = addBox(
zone.connector,
Math.max(0.05, length),
0.09,
valid ? 0.42 : 0.28,
mat,
(zone.current.x + zone.anchor.x) / 2,
0.18,
(zone.current.z + zone.anchor.z) / 2,
false
);
strip.rotation.y = -Math.atan2(dz, dx);
strip.castShadow = false;
if (!valid) {
strip.material.opacity = 0.55;
}
}
function updateZone(zone) {
zone.group.position.set(zone.current.x, 0, zone.current.z);
rebuildConnector(zone);
updateInterface();
}
function selectZone(zone) {
selectedZone = zone;
zones.forEach((item) => {
item.selectionRing.visible = item === zone;
});
busRail.visible = zone.type === "bus";
tramRail.visible = zone.type === "tram";
document.querySelectorAll(".stop-card").forEach((card) => {
card.classList.toggle("selected", card.dataset.zone === zone.id);
});
}
function updateInterface() {
let validCount = 0;
let totalWalk = 0;
zones.forEach((zone) => {
const distance = zone.current.distanceTo(zone.anchor);
const valid = distance <= ACCESS_LIMIT;
const card = document.querySelector(`[data-zone="${zone.id}"]`);
card.classList.toggle("invalid", !valid);
card.querySelector(".link-state").textContent = valid ? "Linked" : "Out of range";
if (valid) {
validCount++;
const accessWalk = distance;
const anchorToHub =
Math.abs(zone.anchor.x - HUB_X) +
Math.abs(zone.anchor.z - HUB_Z);
totalWalk += accessWalk + anchorToHub;
}
});
document.querySelector("#linkedMetric").textContent = `${validCount} / 4`;
document.querySelector("#walkMetric").textContent =
validCount === 4 ? `${Math.round(totalWalk)} m` : "—";
const status = document.querySelector("#status");
const icon = document.querySelector("#statusIcon");
const title = document.querySelector("#statusTitle");
const text = document.querySelector("#statusText");
if (validCount === 4) {
status.classList.remove("warning");
icon.textContent = "✓";
title.textContent = "Transfer network connected";
text.textContent = "All four boarding zones link to the pedestrian network.";
} else {
status.classList.add("warning");
icon.textContent = "!";
title.textContent = `${4 - validCount} access link needs attention`;
text.textContent = "Move the highlighted zone closer to its fixed access node.";
}
document.querySelector("#autoLink").disabled = validCount === 4;
}
zones.forEach((zone) => {
zone.current = zone.initial.clone();
});
zones.forEach(updateZone);
selectZone(zones[0]);
// Direct camera and drag controls, without external examples/addons.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.35);
const planePoint = new THREE.Vector3();
let pointerMode = null;
let activeZone = null;
let lastPointer = { x: 0, y: 0 };
function setPointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findZoneAtPointer(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(hitMeshes, false);
if (!hits.length) return null;
return zoneById.get(hits[0].object.userData.zoneId) || null;
}
function moveZoneFromPointer(event, zone) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
if (!raycaster.ray.intersectPlane(dragPlane, planePoint)) return;
if (zone.type === "bus") {
zone.current.z = THREE.MathUtils.clamp(
planePoint.z,
zone.min.z,
zone.max.z
);
zone.current.x = zone.min.x;
} else {
zone.current.x = THREE.MathUtils.clamp(
planePoint.x,
zone.min.x,
zone.max.x
);
zone.current.z = zone.min.z;
}
updateZone(zone);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
lastPointer = { x: event.clientX, y: event.clientY };
if (event.button === 2) {
pointerMode = "pan";
canvas.style.cursor = "grabbing";
return;
}
const zone = findZoneAtPointer(event);
if (zone) {
activeZone = zone;
pointerMode = "zone";
selectZone(zone);
canvas.style.cursor = "grabbing";
} else {
pointerMode = "orbit";
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerMode) {
const zone = findZoneAtPointer(event);
canvas.style.cursor = zone ? "grab" : "default";
return;
}
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
lastPointer = { x: event.clientX, y: event.clientY };
if (pointerMode === "zone" && activeZone) {
moveZoneFromPointer(event, activeZone);
} else if (pointerMode === "orbit") {
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.005,
0.16,
1.32
);
updateCamera();
} else if (pointerMode === "pan") {
const worldPerPixel =
((camera.top - camera.bottom) / camera.zoom) /
canvas.clientHeight;
const right = new THREE.Vector3(
Math.cos(cameraState.azimuth),
0,
-Math.sin(cameraState.azimuth)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.azimuth),
0,
-Math.cos(cameraState.azimuth)
);
cameraState.target.addScaledVector(right, -dx * worldPerPixel);
cameraState.target.addScaledVector(forward, -dy * worldPerPixel);
updateCamera();
}
});
function endPointer(event) {
pointerMode = null;
activeZone = null;
canvas.style.cursor = "default";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * (event.deltaY > 0 ? 0.9 : 1.1),
0.65,
2.45
);
updateCamera();
},
{ passive: false }
);
document.querySelectorAll(".stop-card").forEach((card) => {
card.addEventListener("click", () => {
selectZone(zoneById.get(card.dataset.zone));
});
card.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
selectZone(zoneById.get(card.dataset.zone));
}
});
});
document.querySelector("#autoLink").addEventListener("click", () => {
let changed = 0;
zones.forEach((zone) => {
if (zone.current.distanceTo(zone.anchor) > ACCESS_LIMIT) {
zone.current.copy(zone.anchor);
updateZone(zone);
changed++;
}
});
showToast(
changed ? `${changed} boarding zone${changed > 1 ? "s" : ""} connected` :
"All access links are already connected"
);
});
document.querySelector("#resetZones").addEventListener("click", () => {
zones.forEach((zone) => {
zone.current.copy(zone.initial);
updateZone(zone);
});
showToast("Boarding zones reset");
});
document.querySelector("#pause").addEventListener("click", (event) => {
paused = !paused;
event.currentTarget.textContent = paused ? "Resume services" : "Pause services";
showToast(paused ? "Service vehicles paused" : "Service vehicles running");
});
document.querySelector("#isoView").addEventListener("click", () => {
cameraState.azimuth = defaultCamera.azimuth;
cameraState.polar = defaultCamera.polar;
cameraState.zoom = defaultCamera.zoom;
updateCamera();
});
document.querySelector("#planView").addEventListener("click", () => {
cameraState.azimuth = 0;
cameraState.polar = 0.16;
cameraState.zoom = 1.08;
updateCamera();
});
document.querySelector("#resetView").addEventListener("click", () => {
cameraState.target.copy(defaultCamera.target);
cameraState.azimuth = defaultCamera.azimuth;
cameraState.polar = defaultCamera.polar;
cameraState.zoom = defaultCamera.zoom;
updateCamera();
});
// Keyboard nudging for the selected zone.
window.addEventListener("keydown", (event) => {
if (!["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(event.key)) {
return;
}
if (document.activeElement?.tagName === "INPUT") return;
const zone = selectedZone;
const step = event.shiftKey ? 1 : 0.4;
let moved = false;
if (zone.type === "bus" && ["ArrowUp", "ArrowDown"].includes(event.key)) {
zone.current.z = THREE.MathUtils.clamp(
zone.current.z + (event.key === "ArrowUp" ? step : -step),
zone.min.z,
zone.max.z
);
moved = true;
}
if (zone.type === "tram" && ["ArrowLeft", "ArrowRight"].includes(event.key)) {
zone.current.x = THREE.MathUtils.clamp(
zone.current.x + (event.key === "ArrowRight" ? step : -step),
zone.min.x,
zone.max.x
);
moved = true;
}
if (moved) {
event.preventDefault();
updateZone(zone);
}
});
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
const clock = new THREE.Clock();
const busLength = busCurve.getLength();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
if (!paused) {
const busU = (elapsed * 3.8 / busLength + 0.11) % 1;
const busPosition = busCurve.getPointAt(busU);
const busTangent = busCurve.getTangentAt(busU);
busVehicle.position.copy(busPosition);
busVehicle.rotation.y = -Math.atan2(busTangent.z, busTangent.x);
const tramX = -4 + ((elapsed * 3.4) % 59);
tramVehicle.position.set(tramX, 0, 23);
tramVehicle.rotation.y = 0;
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```