Prompt specification
Build or customize the Three.js 3D scene "School-route crossing corner" 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: School-route crossing corner
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/school-route-crossing-corner-x0423
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Compose a neighborhood crossing with curb extensions, waiting areas, and visitors; a selected route shows where each traveler waits and when their movement begins.
Working Three.js Starter (school-route-crossing-corner-x0423):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Crossing Time — Voxel Neighborhood</title>
<style>
:root {
--ink: #24332f;
--muted: #66746e;
--paper: rgba(250, 249, 243, 0.93);
--line: rgba(36, 51, 47, 0.13);
--teal: #149f91;
--coral: #ef765e;
--yellow: #e4aa32;
--shadow: 0 18px 50px rgba(37, 54, 48, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e1;
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;
cursor: pointer;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.brand {
position: absolute;
top: 22px;
left: 22px;
width: min(370px, calc(100vw - 44px));
padding: 22px 23px 20px;
border-radius: 22px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 13px;
color: #52645d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(20, 159, 145, 0.12);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(31px, 3vw, 44px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.045em;
}
.brand p {
max-width: 310px;
margin: 15px 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 9px 16px;
padding-top: 14px;
border-top: 1px solid var(--line);
color: #53635c;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend i {
width: 10px;
height: 10px;
border: 2px solid currentColor;
border-radius: 2px;
}
.legend .wait-key { color: var(--yellow); }
.legend .route-key { color: var(--teal); }
.legend .walk-key { color: var(--coral); }
.route-panel {
position: absolute;
top: 22px;
right: 22px;
width: 345px;
max-height: calc(100vh - 44px);
overflow: auto;
padding: 18px;
border-radius: 22px;
scrollbar-width: thin;
}
.panel-kicker {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
color: #68766f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.live {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--teal);
}
.live::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(20, 159, 145, 0.1);
}
.route-tabs {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-bottom: 18px;
}
.route-tab {
min-width: 0;
padding: 10px 7px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.48);
font-size: 10px;
font-weight: 750;
line-height: 1.2;
transition: 160ms ease;
}
.route-tab span {
display: block;
width: 9px;
height: 9px;
margin: 0 auto 7px;
border-radius: 3px;
background: var(--route-color);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--route-color) 17%, transparent);
}
.route-tab:hover,
.route-tab.active {
border-color: var(--route-color);
background: color-mix(in srgb, var(--route-color) 10%, white);
transform: translateY(-1px);
}
.route-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
margin-bottom: 14px;
}
.route-heading h2 {
margin: 0 0 5px;
font-family: Georgia, "Times New Roman", serif;
font-size: 24px;
font-weight: 500;
letter-spacing: -0.025em;
}
.route-heading p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.route-count {
flex: 0 0 auto;
padding: 6px 8px;
border-radius: 8px;
background: #edf0ea;
color: #5e6b65;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.schedule-label {
display: flex;
justify-content: space-between;
padding: 10px 1px 7px;
border-top: 1px solid var(--line);
color: #75817c;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#travelerList {
display: grid;
gap: 8px;
}
.traveler-card {
display: grid;
grid-template-columns: 36px 1fr auto;
gap: 10px;
align-items: center;
width: 100%;
padding: 10px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.48);
text-align: left;
transition: 160ms ease;
}
.traveler-card:hover,
.traveler-card.active {
border-color: color-mix(in srgb, var(--route-color) 55%, transparent);
background: color-mix(in srgb, var(--route-color) 7%, white);
}
.avatar {
position: relative;
width: 34px;
height: 34px;
border-radius: 10px;
background: color-mix(in srgb, var(--route-color) 17%, white);
}
.avatar::before {
content: "";
position: absolute;
left: 10px;
top: 7px;
width: 14px;
height: 14px;
border-radius: 4px;
background: var(--route-color);
}
.avatar::after {
content: "";
position: absolute;
left: 6px;
bottom: 5px;
width: 22px;
height: 10px;
border-radius: 4px 4px 2px 2px;
background: var(--route-color);
}
.traveler-name {
display: block;
margin-bottom: 3px;
font-size: 12px;
font-weight: 800;
}
.traveler-detail {
display: block;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.traveler-detail b {
color: var(--ink);
}
.status {
min-width: 54px;
padding: 5px 6px;
border-radius: 7px;
background: #edf0eb;
color: #6f7b75;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.07em;
text-align: center;
text-transform: uppercase;
}
.status.moving {
background: color-mix(in srgb, var(--route-color) 15%, white);
color: color-mix(in srgb, var(--route-color) 80%, #25332e);
}
.status.arrived {
background: #e4eee8;
color: #34715f;
}
.panel-note {
margin: 14px 2px 0;
color: #7a8580;
font-size: 9px;
line-height: 1.45;
}
.timeline-panel {
position: absolute;
left: 22px;
right: 394px;
bottom: 22px;
padding: 15px 18px 14px;
border-radius: 18px;
}
.timeline-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 11px;
}
.timeline-title {
display: flex;
align-items: center;
gap: 9px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.timeline-title i {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--teal);
}
.timeline-right {
display: flex;
align-items: center;
gap: 11px;
}
#clock {
min-width: 42px;
color: #4f5f58;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
font-weight: 700;
}
#playButton {
display: grid;
place-items: center;
width: 30px;
height: 30px;
border: 0;
border-radius: 9px;
background: var(--ink);
color: white;
font-size: 11px;
}
.timeline-track {
position: relative;
height: 25px;
}
#timelineMarkers {
position: absolute;
z-index: 2;
top: -3px;
left: 0;
right: 0;
height: 10px;
}
.schedule-marker {
position: absolute;
top: 0;
width: 8px;
height: 8px;
padding: 0;
border: 2px solid rgba(255, 255, 255, 0.9);
border-radius: 2px;
background: var(--route-color);
box-shadow: 0 2px 7px rgba(30, 45, 39, 0.18);
transform: translateX(-50%);
}
#timeline {
position: absolute;
z-index: 3;
top: 3px;
left: 0;
width: 100%;
height: 5px;
margin: 0;
appearance: none;
border-radius: 4px;
background: linear-gradient(
to right,
var(--teal) 0%,
var(--teal) var(--progress, 0%),
#d9dfd9 var(--progress, 0%),
#d9dfd9 100%
);
outline: none;
}
#timeline::-webkit-slider-thumb {
width: 14px;
height: 14px;
appearance: none;
border: 3px solid white;
border-radius: 4px;
background: var(--ink);
box-shadow: 0 2px 7px rgba(27, 42, 36, 0.25);
}
#timeline::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid white;
border-radius: 4px;
background: var(--ink);
}
.timeline-foot {
display: flex;
justify-content: space-between;
margin-top: 4px;
color: #7d8883;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.camera-dock {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
.camera-help {
margin-right: 4px;
padding: 9px 11px;
border-radius: 11px;
background: rgba(250, 249, 243, 0.8);
color: #6f7c76;
font-size: 9px;
line-height: 1.4;
backdrop-filter: blur(10px);
}
.camera-buttons {
display: flex;
gap: 5px;
padding: 6px;
border-radius: 13px;
background: var(--paper);
box-shadow: var(--shadow);
}
.camera-buttons button {
display: grid;
place-items: center;
width: 30px;
height: 30px;
padding: 0;
border: 0;
border-radius: 8px;
background: #edf0ea;
font-size: 14px;
font-weight: 800;
}
.camera-buttons button:hover { background: #dfe7df; }
#worldLabels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
top: 0;
left: 0;
width: 150px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.9);
border-left: 4px solid var(--route-color);
border-radius: 9px;
background: rgba(250, 249, 243, 0.94);
box-shadow: 0 8px 22px rgba(29, 45, 38, 0.16);
color: var(--ink);
will-change: transform;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -6px;
width: 10px;
height: 10px;
background: rgba(250, 249, 243, 0.94);
transform: translateX(-50%) rotate(45deg);
border-right: 1px solid rgba(255, 255, 255, 0.9);
border-bottom: 1px solid rgba(255, 255, 255, 0.9);
}
.world-label strong {
display: block;
margin-bottom: 2px;
font-size: 10px;
}
.world-label small {
display: block;
color: #6f7c76;
font-size: 8px;
line-height: 1.35;
}
.world-label b {
display: block;
margin-top: 3px;
color: var(--route-color);
font-size: 8px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
@media (max-width: 900px) {
.brand {
width: 285px;
padding: 17px;
}
.brand p { display: none; }
h1 { font-size: 31px; }
.route-panel {
width: 310px;
padding: 15px;
}
.timeline-panel {
right: 22px;
bottom: 84px;
}
.camera-help { display: none; }
}
@media (max-width: 650px) {
.brand {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px 15px;
}
.brand h1 { font-size: 25px; }
.brand h1 br { display: none; }
.brand .legend { display: none; }
.eyebrow { margin-bottom: 7px; font-size: 8px; }
.route-panel {
top: auto;
right: 10px;
bottom: 132px;
width: calc(100vw - 20px);
max-height: 39vh;
padding: 12px;
}
.route-tabs { margin-bottom: 10px; }
.route-heading { margin-bottom: 8px; }
.route-heading h2 { font-size: 20px; }
.schedule-label { padding-top: 6px; }
.traveler-card { padding: 7px; }
.timeline-panel {
left: 10px;
right: 10px;
bottom: 10px;
padding: 11px 13px;
}
.camera-dock { display: none; }
.world-label { width: 128px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel neighborhood crossing"></canvas>
<div id="worldLabels"></div>
<main class="ui">
<section class="brand panel">
<div class="eyebrow">Voxel mobility study · Block 04</div>
<h1>A little pause.<br />A safer crossing.</h1>
<p>
Explore how curb extensions create compact waiting rooms. Select a route to
reveal each traveler’s waiting point and planned movement time.
</p>
<div class="legend">
<span class="route-key"><i></i>Selected route</span>
<span class="wait-key"><i></i>Wait zone</span>
<span class="walk-key"><i></i>Crossing</span>
</div>
</section>
<aside class="route-panel panel">
<div class="panel-kicker">
<span>Route planner</span>
<span class="live">Live plan</span>
</div>
<nav class="route-tabs" aria-label="Choose a route">
<button class="route-tab active" data-route="school" style="--route-color:#149f91">
<span></span>School Run
</button>
<button class="route-tab" data-route="market" style="--route-color:#ef765e">
<span></span>Market Loop
</button>
<button class="route-tab" data-route="library" style="--route-color:#e4aa32">
<span></span>Library Way
</button>
</nav>
<div class="route-heading">
<div>
<h2 id="routeTitle">School Run</h2>
<p id="routeDescription">East school gates · curb-to-curb</p>
</div>
<span class="route-count" id="routeCount">2 travelers</span>
</div>
<div class="schedule-label">
<span>Traveler schedule</span>
<span>Movement start</span>
</div>
<div id="travelerList"></div>
<p class="panel-note">
Times run on a 30-second planning loop. Click a marker to jump to a
movement start; click a traveler in the scene to reveal its route.
</p>
</aside>
<section class="timeline-panel panel">
<div class="timeline-head">
<div class="timeline-title">
<i id="timelineColor"></i>
<span id="timelineRoute">School Run timeline</span>
</div>
<div class="timeline-right">
<span id="clock">00:00</span>
<button id="playButton" aria-label="Pause timeline">Ⅱ</button>
</div>
</div>
<div class="timeline-track">
<div id="timelineMarkers"></div>
<input id="timeline" type="range" min="0" max="30" step="0.05" value="0" aria-label="Movement timeline" />
</div>
<div class="timeline-foot">
<span>Wait</span>
<span>Movement begins</span>
<span>Arrival</span>
</div>
</section>
<div class="camera-dock">
<div class="camera-help">
Drag to orbit · wheel to zoom<br />Shift-drag to pan
</div>
<div class="camera-buttons">
<button id="orbitLeft" aria-label="Orbit left">‹</button>
<button id="orbitRight" aria-label="Orbit right">›</button>
<button id="zoomIn" aria-label="Zoom in">+</button>
<button id="zoomOut" aria-label="Zoom out">−</button>
<button id="homeCamera" aria-label="Reset camera">⌂</button>
</div>
</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const canvas = $("#scene");
const labelLayer = $("#worldLabels");
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e1);
scene.fog = new THREE.Fog(0xdce9e1, 43, 76);
const camera = new THREE.OrthographicCamera(-20, 20, 12, -12, 0.1, 120);
const cameraState = {
target: new THREE.Vector3(0, 0.4, 0),
theta: 2.32,
phi: 0.78,
radius: 39,
zoom: 1
};
function updateCamera() {
const { target, theta, phi, radius } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = aspect < 0.8 ? 29 : 25;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setSize(width, height, false);
updateCamera();
}
window.addEventListener("resize", resize);
resize();
const hemisphere = new THREE.HemisphereLight(0xf7fbf4, 0x809184, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d3, 3.15);
sun.position.set(-15, 25, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -31;
sun.shadow.camera.right = 31;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d9e5, 0.85);
fill.position.set(20, 10, -18);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and voxel helpers
// -------------------------------------------------------------------------
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...options
});
}
const M = {
base: standard(0xb9c9ae),
grass: standard(0xaec49f),
road: standard(0x435057),
roadEdge: standard(0x596468),
sidewalk: standard(0xe7e4d8),
sidewalkAlt: standard(0xdeddd1),
curb: standard(0xf2efe4),
tileLine: standard(0xc8c8bc),
white: standard(0xf8f4df),
yellow: standard(0xe7b642),
wood: standard(0x9b6848),
woodDark: standard(0x704934),
trunk: standard(0x76513a),
leafA: standard(0x4d8562),
leafB: standard(0x6d9e6d),
leafC: standard(0x86ac78),
metal: standard(0x3f514d),
glass: standard(0x7ea7aa, { roughness: 0.28, metalness: 0.08 }),
darkGlass: standard(0x385459, { roughness: 0.25, metalness: 0.12 }),
lamp: standard(0xffe8a8, { emissive: 0xffcf66, emissiveIntensity: 0.8 }),
greenLamp: standard(0x65e7a4, { emissive: 0x36c778, emissiveIntensity: 1.8 }),
redLamp: standard(0xef715d, { emissive: 0xca382c, emissiveIntensity: 0.55 })
};
function addBox(w, h, d, x, y, z, colorOrMaterial, parent = scene, cast = true) {
const material = colorOrMaterial instanceof THREE.Material
? colorOrMaterial
: standard(colorOrMaterial);
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addTextSign(text, x, y, z, background = "#29433c", foreground = "#fff6dc") {
const signCanvas = document.createElement("canvas");
signCanvas.width = 512;
signCanvas.height = 128;
const context = signCanvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, 512, 128);
context.fillStyle = foreground;
context.font = "700 52px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(signCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(3.1, 0.77),
new THREE.MeshBasicMaterial({ map: texture })
);
sign.position.set(x, y, z);
sign.rotation.y = Math.PI;
scene.add(sign);
return sign;
}
// -------------------------------------------------------------------------
// Street, sidewalks, curb extensions, and markings
// -------------------------------------------------------------------------
addBox(52, 0.9, 40, 0, -0.67, 0, M.grass);
addBox(46, 0.12, 9, 0, 0.02, 0, M.road);
addBox(46, 0.36, 11.5, 0, 0.22, 10.25, M.sidewalk);
addBox(46, 0.36, 11.5, 0, 0.22, -10.25, M.sidewalk);
// Fine sidewalk seams.
for (let x = -22; x <= 22; x += 2.5) {
addBox(0.022, 0.012, 10.8, x, 0.407, 10.15, M.tileLine, scene, false);
addBox(0.022, 0.012, 10.8, x, 0.407, -10.15, M.tileLine, scene, false);
}
for (let z = 5; z <= 15; z += 2.25) {
addBox(44, 0.012, 0.022, 0, 0.407, z, M.tileLine, scene, false);
addBox(44, 0.012, 0.022, 0, 0.407, -z, M.tileLine, scene, false);
}
// Curbs leave an opening at the crossing.
for (const z of [-4.52, 4.52]) {
addBox(20.6, 0.34, 0.18, -11.7, 0.23, z, M.curb);
addBox(20.6, 0.34, 0.18, 11.7, 0.23, z, M.curb);
}
// Four curb extensions / bulb-outs.
for (const x of [-2.8, 2.8]) {
for (const z of [-4.55, 4.55]) {
addBox(3.4, 0.36, 2.1, x, 0.22, z, M.sidewalkAlt);
}
}
for (const x of [-2.8, 2.8]) {
addBox(3.4, 0.32, 0.13, x, 0.25, -3.48, M.curb);
addBox(3.4, 0.32, 0.13, x, 0.25, 3.48, M.curb);
}
// Tactile warning pads at each ramp.
for (const x of [-1.35, 1.35]) {
for (const z of [-3.25, 3.25]) {
for (let ix = -1; ix <= 1; ix++) {
for (let iz = -1; iz <= 1; iz++) {
addBox(
0.13, 0.035, 0.13,
x + ix * 0.16, 0.445, z + iz * 0.16,
M.yellow, scene, false
);
}
}
}
}
// Zebra crossing.
for (let z = -2.7; z <= 2.71; z += 0.78) {
addBox(2.45, 0.025, 0.43, 0, 0.095, z, M.white, scene, false);
}
// Road center line, interrupted by the crossing.
for (let x = -21; x <= 21; x += 3.2) {
if (Math.abs(x) > 3.5) {
addBox(1.6, 0.024, 0.11, x, 0.096, 0, M.yellow, scene, false);
}
}
// Subtle cycle-lane strips along the road edges.
for (const z of [-3.92, 3.92]) {
for (const side of [-1, 1]) {
for (let x = side * 7; x < side * 23; x += side * 4) {
addBox(2.4, 0.018, 0.42, x, 0.092, z, standard(0x809b8d), scene, false);
}
}
}
// -------------------------------------------------------------------------
// Buildings
// -------------------------------------------------------------------------
function makeBuilding(x, z, w, d, h, wallColor, roofColor, variant = 0) {
const group = new THREE.Group();
group.position.set(x, 0.4, z);
scene.add(group);
addBox(w, h, d, 0, h / 2, 0, wallColor, group);
addBox(w + 0.28, 0.22, d + 0.28, 0, h + 0.11, 0, roofColor, group);
addBox(w * 0.48, 0.45, d * 0.42, w * 0.12, h + 0.42, -d * 0.08, roofColor, group);
// Roof chimney / vents.
addBox(0.45, 0.75, 0.45, -w * 0.24, h + 0.58, d * 0.18, M.wallDark || M.metal, group);
addBox(0.85, 0.28, 0.65, w * 0.28, h + 0.35, d * 0.23, M.metal, group);
const floors = Math.max(1, Math.floor(h / 2.25));
const windowColor = variant % 3 === 0 ? M.lamp : M.darkGlass;
// Windows on all four faces.
for (let floor = 0; floor < floors; floor++) {
const wy = 1.25 + floor * 1.8;
const columns = Math.max(2, Math.floor(w / 1.4));
for (let col = 0; col < columns; col++) {
const wx = -w * 0.34 + (col / Math.max(1, columns - 1)) * w * 0.68;
addBox(0.58, 0.72, 0.07, wx, wy, d / 2 + 0.04, windowColor, group, false);
addBox(0.58, 0.72, 0.07, wx, wy, -d / 2 - 0.04, windowColor, group, false);
}
const sideColumns = Math.max(2, Math.floor(d / 1.5));
for (let col = 0; col < sideColumns; col++) {
const wz = -d * 0.3 + (col / Math.max(1, sideColumns - 1)) * d * 0.6;
addBox(0.07, 0.72, 0.58, w / 2 + 0.04, wy, wz, windowColor, group, false);
addBox(0.07, 0.72, 0.58, -w / 2 - 0.04, wy, wz, windowColor, group, false);
}
}
// Door facing the road.
const doorZ = z > 0 ? -d / 2 - 0.055 : d / 2 + 0.055;
addBox(0.72, 1.4, 0.1, -w * 0.28, 0.7, doorZ, M.woodDark, group);
addBox(0.08, 0.08, 0.08, -w * 0.28 + 0.22, 0.72, doorZ + (z > 0 ? -0.07 : 0.07), M.yellow, group, false);
if (variant % 2 === 0) {
const awningZ = z > 0 ? -d / 2 - 0.4 : d / 2 + 0.4;
addBox(w * 0.72, 0.13, 0.75, 0, 1.83, awningZ, roofColor, group);
}
return group;
}
makeBuilding(-15.2, 12.8, 4.4, 3.7, 3.6, 0xc88268, 0x73574d, 0);
makeBuilding(-8.0, 12.9, 4.2, 3.5, 4.5, 0x8fa78e, 0x596b61, 1);
makeBuilding(7.8, 12.9, 4.5, 3.8, 5.1, 0x7696a1, 0x4e6972, 2);
makeBuilding(15.0, 12.7, 4.2, 3.7, 3.8, 0xd2b16f, 0x766347, 0);
makeBuilding(-15.0, -12.8, 4.3, 3.7, 4.1, 0xc38f73, 0x71584f, 1);
makeBuilding(-7.5, -12.8, 4.6, 3.8, 3.5, 0x95a885, 0x5d6d59, 2);
makeBuilding(7.5, -12.8, 4.5, 3.9, 4.6, 0xd5b873, 0x756548, 0);
makeBuilding(15.1, -12.8, 4.4, 3.6, 3.7, 0x7f9aa0, 0x526b70, 1);
addTextSign("CORNER MARKET", -7.5, 2.8, -14.67, "#36584d", "#fff2cf");
// -------------------------------------------------------------------------
// Street furniture and trees
// -------------------------------------------------------------------------
function makeTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.4, z);
group.scale.setScalar(scale);
scene.add(group);
addBox(1.25, 0.24, 1.25, 0, 0.12, 0, M.curb, group);
addBox(0.92, 0.08, 0.92, 0, 0.27, 0, standard(0x718c64), group, false);
addBox(0.28, 1.6, 0.28, 0, 1.03, 0, M.trunk, group);
addBox(1.55, 1.05, 1.4, 0, 2.02, 0, M.leafA, group);
addBox(1.18, 0.92, 1.18, -0.45, 2.72, 0.05, M.leafB, group);
addBox(1.0, 0.72, 1.05, 0.42, 2.76, -0.08, M.leafC, group);
}
[
[-19, 8.0, 1.05], [-11, 8.0, 0.86], [4.7, 8.1, 0.94], [17.8, 8.0, 1.08],
[-19, -8.0, 0.92], [-10.8, -8.1, 1.08], [4.7, -8.1, 0.85], [18.4, -8.0, 1.04]
].forEach(([x, z, s]) => makeTree(x, z, s));
function makeLamp(x, z, direction = 1) {
const group = new THREE.Group();
group.position.set(x, 0.4, z);
scene.add(group);
addBox(0.32, 0.18, 0.32, 0, 0.09, 0, M.metal, group);
addBox(0.13, 3.3, 0.13, 0, 1.65, 0, M.metal, group);
addBox(0.86, 0.11, 0.12, direction * 0.36, 3.25, 0, M.metal, group);
addBox(0.45, 0.2, 0.32, direction * 0.72, 3.12, 0, M.lamp, group, false);
}
makeLamp(-4.7, 6.8, 1);
makeLamp(4.7, 6.8, -1);
makeLamp(-4.7, -6.8, 1);
makeLamp(4.7, -6.8, -1);
function makeBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.4, z);
group.rotation.y = rotation;
scene.add(group);
addBox(1.65, 0.13, 0.5, 0, 0.52, 0, M.wood, group);
addBox(1.65, 0.58, 0.12, 0, 0.86, -0.2, M.wood, group);
addBox(0.12, 0.48, 0.42, -0.62, 0.26, 0, M.metal, group);
addBox(0.12, 0.48, 0.42, 0.62, 0.26, 0, M.metal, group);
}
makeBench(-10.1, -6.1, 0);
makeBench(9.8, 6.0, 0);
// Bus shelter.
const shelter = new THREE.Group();
shelter.position.set(10.2, 0.4, 8.0);
scene.add(shelter);
addBox(3.2, 0.16, 1.5, 0, 2.55, 0, M.metal, shelter);
addBox(0.12, 2.4, 0.12, -1.45, 1.25, 0.5, M.metal, shelter);
addBox(0.12, 2.4, 0.12, 1.45, 1.25, 0.5, M.metal, shelter);
addBox(0.12, 2.4, 0.12, -1.45, 1.25, -0.55, M.metal, shelter);
addBox(0.12, 2.4, 0.12, 1.45, 1.25, -0.55, M.metal, shelter);
addBox(2.75, 1.75, 0.06, 0, 1.35, -0.58, M.glass, shelter, false);
addBox(2.35, 0.14, 0.44, 0, 0.58, -0.23, M.wood, shelter);
// Bike racks.
function makeBikeRack(x, z) {
const group = new THREE.Group();
group.position.set(x, 0.4, z);
scene.add(group);
for (let i = 0; i < 3; i++) {
const rx = (i - 1) * 0.7;
addBox(0.08, 0.7, 0.08, rx, 0.35, -0.28, M.metal, group);
addBox(0.08, 0.7, 0.08, rx, 0.35, 0.28, M.metal, group);
addBox(0.08, 0.08, 0.64, rx, 0.7, 0, M.metal, group);
}
}
makeBikeRack(-10.5, 8.0);
// Pedestrian signals.
function makeSignal(x, z, faceNorth = true) {
const group = new THREE.Group();
group.position.set(x, 0.4, z);
scene.add(group);
addBox(0.38, 0.16, 0.38, 0, 0.08, 0, M.metal, group);
addBox(0.11, 2.5, 0.11, 0, 1.25, 0, M.metal, group);
addBox(0.38, 0.9, 0.28, 0, 2.25, 0, M.metal, group);
const faceZ = faceNorth ? 0.15 : -0.15;
addBox(0.14, 0.14, 0.04, 0, 2.51, faceZ, M.redLamp, group, false);
addBox(0.14, 0.14, 0.04, 0, 2.25, faceZ, M.greenLamp, group, false);
}
makeSignal(-1.72, 3.85, false);
makeSignal(1.72, -3.85, true);
// -------------------------------------------------------------------------
// Voxel people
// -------------------------------------------------------------------------
function makePerson(accentColor, skinColor = 0xd7a17d) {
const group = new THREE.Group();
const materials = [];
function personMaterial(color, options = {}) {
const material = standard(color, options);
materials.push(material);
return material;
}
const shirt = personMaterial(accentColor);
const shirtDark = personMaterial(new THREE.Color(accentColor).multiplyScalar(0.78));
const skin = personMaterial(skinColor);
const pants = personMaterial(0x34464d);
const hair = personMaterial(0x44372f);
const shoes = personMaterial(0x293632);
addBox(0.44, 0.62, 0.27, 0, 1.08, 0, shirt, group);
addBox(0.36, 0.34, 0.32, 0, 1.58, 0, skin, group);
addBox(0.38, 0.1, 0.34, 0, 1.77, 0, hair, group);
addBox(0.07, 0.07, 0.04, -0.09, 1.61, 0.17, M.darkGlass, group, false);
addBox(0.07, 0.07, 0.04, 0.09, 1.61, 0.17, M.darkGlass, group, false);
addBox(0.14, 0.52, 0.17, -0.3, 1.05, 0, shirtDark, group);
addBox(0.14, 0.52, 0.17, 0.3, 1.05, 0, shirtDark, group);
addBox(0.13, 0.13, 0.16, -0.3, 0.76, 0, skin, group);
addBox(0.13, 0.13, 0.16, 0.3, 0.76, 0, skin, group);
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.12, 0.79, 0);
rightLeg.position.set(0.12, 0.79, 0);
group.add(leftLeg, rightLeg);
addBox(0.16, 0.57, 0.18, 0, -0.28, 0, pants, leftLeg);
addBox(0.16, 0.57, 0.18, 0, -0.28, 0, pants, rightLeg);
addBox(0.19, 0.12, 0.28, 0, -0.61, 0.05, shoes, leftLeg);
addBox(0.19, 0.12, 0.28, 0, -0.61, 0.05, shoes, rightLeg);
group.userData.materials = materials;
group.userData.leftLeg = leftLeg;
group.userData.rightLeg = rightLeg;
return group;
}
// -------------------------------------------------------------------------
// Route and traveler data
// -------------------------------------------------------------------------
const routes = {
school: {
id: "school",
name: "School Run",
color: "#149f91",
description: "East school gates · curb-to-curb",
travelers: [
{
id: "mira",
name: "Mira",
wait: "Northwest curb extension",
start: 3,
destination: "West school gate",
skin: 0xd39b78,
path: [
[-3.7, 0.43, 4.75], [-2.2, 0.43, 3.55], [-0.62, 0.11, 2.72],
[-0.62, 0.11, -2.72], [-2.2, 0.43, -3.55], [-7, 0.43, -6.8],
[-12.5, 0.43, -6.8]
]
},
{
id: "theo",
name: "Theo",
wait: "Northeast curb extension",
start: 9,
destination: "East school gate",
skin: 0x9b684d,
path: [
[3.6, 0.43, 4.7], [2, 0.43, 3.55], [0.62, 0.11, 2.72],
[0.62, 0.11, -2.72], [2, 0.43, -3.55], [7, 0.43, -6.8],
[13, 0.43, -6.8]
]
}
]
},
market: {
id: "market",
name: "Market Loop",
color: "#ef765e",
description: "Corner shops · return via the crossing",
travelers: [
{
id: "ines",
name: "Inés",
wait: "Southwest curb extension",
start: 6,
destination: "Northwest market row",
skin: 0xc98561,
path: [
[-3.7, 0.43, -4.75], [-2.2, 0.43, -3.55], [-0.62, 0.11, -2.72],
[-0.62, 0.11, 2.72], [-2.2, 0.43, 3.55], [-7, 0.43, 6.8],
[-13, 0.43, 6.8]
]
},
{
id: "omar",
name: "Omar",
wait: "Southeast curb extension",
start: 13,
destination: "Northeast market row",
skin: 0x754a37,
path: [
[3.5, 0.43, -4.8], [2, 0.43, -3.55], [0.62, 0.11, -2.72],
[0.62, 0.11, 2.72], [2, 0.43, 3.55], [7, 0.43, 6.8],
[13, 0.43, 6.8]
]
}
]
},
library: {
id: "library",
name: "Library Way",
color: "#e4aa32",
description: "Public library · slower evening pace",
travelers: [
{
id: "jun",
name: "Jun",
wait: "North curb extension",
start: 16,
destination: "South library plaza",
skin: 0xe0ad82,
path: [
[-2, 0.43, 4.2], [-1, 0.43, 3.4], [0.62, 0.11, 2.72],
[0.62, 0.11, -2.72], [2, 0.43, -3.55], [8, 0.43, -6.8],
[14, 0.43, -6.8]
]
},
{
id: "zara",
name: "Zara",
wait: "South curb extension",
start: 21,
destination: "North library plaza",
skin: 0x8f5e45,
path: [
[-2, 0.43, -4.2], [-1, 0.43, -3.4], [0.62, 0.11, -2.72],
[0.62, 0.11, 2.72], [1, 0.43, 3.4], [7, 0.43, 6.8],
[14, 0.43, 6.8]
]
}
]
}
};
const travelers = [];
const routeGroups = {};
const peopleMeshes = [];
function makeRouteLine(route) {
const group = new THREE.Group();
group.userData.routeId = route.id;
scene.add(group);
const lineMaterial = new THREE.MeshBasicMaterial({
color: route.color,
transparent: true,
opacity: 0.82,
depthWrite: false
});
for (const traveler of route.travelers) {
const points = traveler.path.map((p) => new THREE.Vector3(...p));
for (let i = 0; i < points.length - 1; i++) {
const a = points[i].clone().add(new THREE.Vector3(0, 0.055, 0));
const b = points[i + 1].clone().add(new THREE.Vector3(0, 0.055, 0));
const direction = b.clone().sub(a);
const length = direction.length();
const segment = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.035, length),
lineMaterial
);
segment.position.copy(a).add(b).multiplyScalar(0.5);
segment.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction.normalize()
);
group.add(segment);
}
for (const point of points) {
const node = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.055, 0.13),
lineMaterial
);
node.position.copy(point).add(new THREE.Vector3(0, 0.06, 0));
group.add(node);
}
}
return group;
}
function makeWaitMarker(position, color) {
const group = new THREE.Group();
group.position.copy(position);
scene.add(group);
const markerMaterial = standard(color, {
emissive: color,
emissiveIntensity: 0.22,
roughness: 0.7
});
addBox(0.92, 0.035, 0.085, 0, 0.045, 0.42, markerMaterial, group, false);
addBox(0.92, 0.035, 0.085, 0, 0.045, -0.42, markerMaterial, group, false);
addBox(0.085, 0.035, 0.92, 0.42, 0.045, 0, markerMaterial, group, false);
addBox(0.085, 0.035, 0.92, -0.42, 0.045, 0, markerMaterial, group, false);
const beacon = new THREE.Group();
beacon.position.set(0, 0.08, 0);
group.add(beacon);
addBox(0.065, 0.95, 0.065, 0, 0.48, 0, markerMaterial, beacon, false);
addBox(0.32, 0.19, 0.07, 0.13, 0.95, 0, markerMaterial, beacon, false);
group.userData.beacon = beacon;
return group;
}
Object.values(routes).forEach((route) => {
routeGroups[route.id] = makeRouteLine(route);
route.travelers.forEach((data) => {
const path = data.path.map((p) => new THREE.Vector3(...p));
const person = makePerson(route.color, data.skin);
person.userData.travelerId = data.id;
person.userData.routeId = route.id;
scene.add(person);
peopleMeshes.push(person);
const marker = makeWaitMarker(path[0], route.color);
marker.userData.travelerId = data.id;
const label = document.createElement("div");
label.className = "world-label";
label.style.setProperty("--route-color", route.color);
label.innerHTML = `
<strong>${data.name}</strong>
<small>Waits: ${data.wait}</small>
<b>Movement begins · ${formatTime(data.start)}</b>
`;
label.hidden = true;
labelLayer.appendChild(label);
travelers.push({
...data,
routeId: route.id,
routeColor: route.color,
path,
pathLength: path.slice(1).reduce((sum, point, index) =>
sum + point.distanceTo(path[index]), 0),
person,
marker,
label,
state: "waiting"
});
});
});
// -------------------------------------------------------------------------
// Cars for scale and neighborhood activity
// -------------------------------------------------------------------------
function makeCar(color, direction) {
const group = new THREE.Group();
const bodyMaterial = standard(color);
const glassMaterial = standard(0x31474e, { roughness: 0.25 });
addBox(2.65, 0.55, 1.08, 0, 0.48, 0, bodyMaterial, group);
addBox(1.35, 0.5, 0.93, -0.12, 0.95, 0, bodyMaterial, group);
addBox(1.0, 0.32, 0.96, -0.12, 0.99, 0, glassMaterial, group, false);
addBox(0.09, 0.32, 0.98, 0.48, 0.99, 0, glassMaterial, group, false);
for (const x of [-0.85, 0.85]) {
for (const z of [-0.58, 0.58]) {
addBox(0.48, 0.42, 0.2, x, 0.3, z, M.darkGlass, group);
}
}
addBox(0.12, 0.16, 0.68, 1.36, 0.52, 0, M.lamp, group, false);
addBox(0.12, 0.16, 0.68, -1.36, 0.52, 0, M.redLamp, group, false);
group.position.set(direction > 0 ? -24 : 24, 0.09, direction > 0 ? -2.05 : 2.05);
scene.add(group);
return { group, direction, speed: 3.0 + Math.random() * 0.7 };
}
const cars = [makeCar(0xc45c4f, 1), makeCar(0x466f7c, -1)];
// -------------------------------------------------------------------------
// UI state
// -------------------------------------------------------------------------
let selectedRouteId = "school";
let selectedTravelerId = null;
let currentTime = 0;
let playing = true;
const loopDuration = 30;
function formatTime(seconds) {
const whole = Math.max(0, Math.floor(seconds));
return `00:${String(whole).padStart(2, "0")}`;
}
function getTravelerState(traveler, time) {
if (time < traveler.start) return "waiting";
const distance = (time - traveler.start) * 1.28;
return distance >= traveler.pathLength ? "arrived" : "moving";
}
function setRoute(routeId) {
selectedRouteId = routeId;
selectedTravelerId = null;
document.querySelectorAll(".route-tab").forEach((button) => {
button.classList.toggle("active", button.dataset.route === routeId);
});
const route = routes[routeId];
$("#routeTitle").textContent = route.name;
$("#routeDescription").textContent = route.description;
$("#routeCount").textContent = `${route.travelers.length} travelers`;
$("#timelineRoute").textContent = `${route.name} timeline`;
$("#timelineColor").style.background = route.color;
Object.entries(routeGroups).forEach(([id, group]) => {
group.visible = id === routeId;
});
travelers.forEach((traveler) => {
const focused = traveler.routeId === routeId;
traveler.person.userData.materials.forEach((material) => {
material.transparent = !focused;
material.opacity = focused ? 1 : 0.22;
material.depthWrite = focused;
});
traveler.marker.visible = focused;
traveler.label.hidden = !focused;
});
renderSchedule();
renderTimelineMarkers();
updateScheduleStatus();
}
function renderSchedule() {
const route = routes[selectedRouteId];
const list = $("#travelerList");
list.innerHTML = "";
route.travelers.forEach((traveler) => {
const card = document.createElement("div");
card.className = "traveler-card";
card.dataset.traveler = traveler.id;
card.style.setProperty("--route-color", route.color);
card.innerHTML = `
<div class="avatar"></div>
<div>
<span class="traveler-name">${traveler.name}</span>
<span class="traveler-detail">
${traveler.wait}<br>
<b>Starts ${formatTime(traveler.start)}</b> · ${traveler.destination}
</span>
</div>
<span class="status">Waiting</span>
`;
card.addEventListener("click", () => {
selectedTravelerId = traveler.id;
focusTraveler(traveler);
updateScheduleStatus();
});
list.appendChild(card);
});
}
function updateScheduleStatus() {
document.querySelectorAll(".traveler-card").forEach((card) => {
const traveler = travelers.find((item) => item.id === card.dataset.traveler);
const status = getTravelerState(traveler, currentTime);
const statusElement = card.querySelector(".status");
statusElement.textContent = status;
statusElement.className = `status ${status}`;
card.classList.toggle("active", traveler.id === selectedTravelerId);
});
}
function renderTimelineMarkers() {
const container = $("#timelineMarkers");
container.innerHTML = "";
const route = routes[selectedRouteId];
route.travelers.forEach((traveler) => {
const marker = document.createElement("button");
marker.className = "schedule-marker";
marker.style.setProperty("--route-color", route.color);
marker.style.left = `${(traveler.start / loopDuration) * 100}%`;
marker.title = `${traveler.name} starts at ${formatTime(traveler.start)}`;
marker.setAttribute("aria-label", marker.title);
marker.addEventListener("click", () => {
currentTime = Math.max(0, traveler.start - 0.2);
$("#timeline").value = currentTime;
selectedTravelerId = traveler.id;
updateTimelineUI();
updateScheduleStatus();
});
container.appendChild(marker);
});
}
function updateTimelineUI() {
$("#clock").textContent = formatTime(currentTime);
const percent = (currentTime / loopDuration) * 100;
$("#timeline").style.setProperty("--progress", `${percent}%`);
$("#timeline").value = currentTime;
}
document.querySelectorAll(".route-tab").forEach((button) => {
button.addEventListener("click", () => setRoute(button.dataset.route));
});
$("#timeline").addEventListener("input", (event) => {
currentTime = Number(event.target.value);
updateTimelineUI();
updateScheduleStatus();
});
$("#playButton").addEventListener("click", () => {
playing = !playing;
$("#playButton").textContent = playing ? "Ⅱ" : "▶";
$("#playButton").setAttribute("aria-label", playing ? "Pause timeline" : "Play timeline");
});
// -------------------------------------------------------------------------
// Traveler animation
// -------------------------------------------------------------------------
const sampleDirection = new THREE.Vector3();
const samplePosition = new THREE.Vector3();
function updateTravelers(time, delta) {
travelers.forEach((traveler) => {
const state = getTravelerState(traveler, time);
traveler.state = state;
if (state === "waiting") {
traveler.person.position.copy(traveler.path[0]);
traveler.person.rotation.y = traveler.path[0].x < 0 ? 0.4 : -0.4;
traveler.person.userData.leftLeg.rotation.x = 0;
traveler.person.userData.rightLeg.rotation.x = 0;
return;
}
const distance = Math.min(
traveler.pathLength,
(time - traveler.start) * 1.28
);
let traversed = 0;
let segmentIndex = traveler.path.length - 2;
for (let i = 0; i < traveler.path.length - 1; i++) {
const segmentLength = traveler.path[i].distanceTo(traveler.path[i + 1]);
if (distance <= traversed + segmentLength) {
segmentIndex = i;
break;
}
traversed += segmentLength;
}
const start = traveler.path[segmentIndex];
const end = traveler.path[segmentIndex + 1];
const segmentLength = start.distanceTo(end);
const segmentProgress = segmentLength
? THREE.MathUtils.clamp((distance - traversed) / segmentLength, 0, 1)
: 0;
samplePosition.lerpVectors(start, end, segmentProgress);
sampleDirection.copy(end).sub(start).normalize();
const bob = state === "moving" ? Math.abs(Math.sin(time * 9 + traveler.start)) * 0.035 : 0;
traveler.person.position.copy(samplePosition);
traveler.person.position.y += bob;
const desiredAngle = Math.atan2(sampleDirection.x, sampleDirection.z);
let angleDifference = desiredAngle - traveler.person.rotation.y;
angleDifference = Math.atan2(Math.sin(angleDifference), Math.cos(angleDifference));
traveler.person.rotation.y += angleDifference * Math.min(1, delta * 10);
const stride = state === "moving" ? Math.sin(time * 10 + traveler.start) * 0.55 : 0;
traveler.person.userData.leftLeg.rotation.x = stride;
traveler.person.userData.rightLeg.rotation.x = -stride;
const focused = traveler.routeId === selectedRouteId;
const emphasis = focused && traveler.id === selectedTravelerId ? 1.04 : 1;
traveler.person.scale.setScalar(emphasis);
});
}
function updateMarkers(time) {
travelers.forEach((traveler) => {
if (traveler.routeId !== selectedRouteId) return;
const pulse = 1 + Math.sin(time * 4 + traveler.start) * 0.12;
traveler.marker.userData.beacon.scale.setScalar(pulse);
});
}
// -------------------------------------------------------------------------
// HTML world labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
travelers.forEach((traveler) => {
if (traveler.routeId !== selectedRouteId) {
traveler.label.hidden = true;
return;
}
projected.copy(traveler.path[0]);
projected.y += 1.75;
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
traveler.label.hidden = !visible;
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
traveler.label.style.transform = `translate(${x}px, ${y}px) translate(-50%, -100%)`;
});
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit, pan, zoom
// -------------------------------------------------------------------------
let pointerDown = false;
let pointerMoved = false;
let panMode = false;
let lastX = 0;
let lastY = 0;
let activePointerId = null;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const cameraForward = new THREE.Vector3();
const cameraRight = new THREE.Vector3();
const cameraUp = new THREE.Vector3(0, 1, 0);
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
panMode = event.shiftKey || event.button === 2;
lastX = event.clientX;
lastY = event.clientY;
activePointerId = event.pointerId;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (Math.abs(dx) + Math.abs(dy) > 2) pointerMoved = true;
if (panMode) {
camera.getWorldDirection(cameraForward);
cameraForward.y = 0;
cameraForward.normalize();
cameraRight.crossVectors(cameraForward, cameraUp).normalize();
const panScale = 0.025 / cameraState.zoom;
cameraState.target.addScaledVector(cameraRight, -dx * panScale);
cameraState.target.addScaledVector(cameraForward, -dy * panScale);
} else {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi + dy * 0.005, 0.36, 1.32);
}
updateCamera();
return;
}
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(peopleMeshes, true);
canvas.style.cursor = hits.length ? "pointer" : "grab";
});
function endPointer(event) {
if (!pointerDown) return;
if (!pointerMoved) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(peopleMeshes, true);
if (hits.length) {
let object = hits[0].object;
while (object && !object.userData.travelerId) object = object.parent;
if (object?.userData.travelerId) {
const traveler = travelers.find((item) => item.id === object.userData.travelerId);
setRoute(traveler.routeId);
selectedTravelerId = traveler.id;
updateScheduleStatus();
}
}
}
pointerDown = false;
panMode = false;
activePointerId = null;
canvas.style.cursor = "grab";
}
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 * Math.exp(-event.deltaY * 0.001),
0.7,
2.5
);
updateCamera();
}, { passive: false });
function focusTraveler(traveler) {
cameraState.target.set(traveler.path[0].x, 0.55, traveler.path[0].z);
cameraState.zoom = 1.48;
updateCamera();
}
function resetCamera() {
cameraState.target.set(0, 0.4, 0);
cameraState.theta = 2.32;
cameraState.phi = 0.78;
cameraState.radius = 39;
cameraState.zoom = 1;
updateCamera();
}
$("#orbitLeft").addEventListener("click", () => {
cameraState.theta -= 0.22;
updateCamera();
});
$("#orbitRight").addEventListener("click", () => {
cameraState.theta += 0.22;
updateCamera();
});
$("#zoomIn").addEventListener("click", () => {
cameraState.zoom = THREE.MathUtils.clamp(cameraState.zoom * 1.15, 0.7, 2.5);
updateCamera();
});
$("#zoomOut").addEventListener("click", () => {
cameraState.zoom = THREE.MathUtils.clamp(cameraState.zoom / 1.15, 0.7, 2.5);
updateCamera();
});
$("#homeCamera").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraState.theta -= 0.12;
if (event.key === "ArrowRight") cameraState.theta += 0.12;
if (event.key === "+" || event.key === "=") cameraState.zoom *= 1.1;
if (event.key === "-") cameraState.zoom /= 1.1;
if (event.key === " ") {
event.preventDefault();
playing = !playing;
$("#playButton").textContent = playing ? "Ⅱ" : "▶";
}
updateCamera();
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let uiAccumulator = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
currentTime += delta;
if (currentTime >= loopDuration) currentTime %= loopDuration;
}
updateTravelers(currentTime, delta);
updateMarkers(currentTime);
cars.forEach((car) => {
car.group.position.x += car.direction * car.speed * delta;
if (car.group.position.x > 25) car.group.position.x = -25;
if (car.group.position.x < -25) car.group.position.x = 25;
});
uiAccumulator += delta;
if (uiAccumulator > 0.08) {
updateTimelineUI();
updateScheduleStatus();
uiAccumulator = 0;
}
updateLabels();
renderer.render(scene, camera);
}
setRoute("school");
updateCamera();
animate();
</script>
</body>
</html>
```