Prompt specification
Build or customize the Three.js 3D scene "Two Bus Lines Same Frequency Different Phase" 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: Two Bus Lines Same Frequency Different Phase
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-bus-lines-same-frequency-different-phase-x2142
Scene Description Prompt:
Compare low-poly bus-transfer waits under two timetable phase offsets, keeping service frequency and passenger arrivals fixed. One HTML file using Three.js from cdnjs.
Working Three.js Starter (two-bus-lines-same-frequency-different-phase-x2142):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bus Transfer Timing Lab</title>
<style>
:root {
--ink: #172522;
--muted: #63716d;
--paper: #f7f8f4;
--glass: rgba(255, 255, 255, 0.88);
--line: rgba(23, 37, 34, 0.12);
--a: #087f73;
--b: #d7673f;
--headway: 15;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfe9e4;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button,
select {
color: var(--ink);
}
#stage {
position: fixed;
inset: 84px 0 42px;
overflow: hidden;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 10;
top: 12px;
left: 14px;
right: 14px;
min-height: 60px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
padding: 10px 12px 10px 16px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 16px;
background: rgba(250, 252, 248, 0.91);
box-shadow: 0 10px 35px rgba(39, 62, 55, 0.10);
backdrop-filter: blur(14px);
}
.brand {
min-width: 0;
}
.eyebrow {
margin-bottom: 2px;
color: #52736a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand h1 {
margin: 0;
font-size: clamp(17px, 2vw, 25px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
}
.controls {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 8px;
flex-wrap: wrap;
}
.control-button,
.speed-select {
height: 36px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.82);
cursor: pointer;
transition:
background 120ms ease,
transform 120ms ease;
}
.control-button {
padding: 0 12px;
font-size: 12px;
font-weight: 750;
}
.control-button.primary {
min-width: 88px;
color: white;
border-color: #183f38;
background: #183f38;
}
.control-button:hover,
.speed-select:hover {
background: white;
transform: translateY(-1px);
}
.control-button.primary:hover {
background: #24584e;
}
.speed-select {
padding: 0 28px 0 10px;
font-size: 12px;
font-weight: 700;
}
.offset-control {
display: flex;
align-items: center;
gap: 8px;
height: 36px;
padding: 0 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.72);
font-size: 11px;
font-weight: 750;
white-space: nowrap;
}
.offset-control input {
width: 92px;
accent-color: var(--b);
cursor: pointer;
}
.offset-control output {
min-width: 37px;
color: #a74c2e;
font-variant-numeric: tabular-nums;
}
.panels {
position: absolute;
z-index: 3;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
top: 0;
bottom: 0;
width: 50%;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 14px;
}
#panelA {
left: 0;
}
#panelB {
left: 50%;
}
.card {
pointer-events: auto;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 14px;
background: var(--glass);
box-shadow: 0 8px 28px rgba(39, 62, 55, 0.09);
backdrop-filter: blur(12px);
}
.scenario-card {
width: min(350px, calc(100% - 8px));
padding: 12px 13px;
}
.scenario-heading {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 10px;
}
.scenario-badge {
display: grid;
width: 34px;
height: 34px;
place-items: center;
border-radius: 10px;
color: white;
font-size: 15px;
font-weight: 900;
box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.10);
}
#panelA .scenario-badge {
background: var(--a);
}
#panelB .scenario-badge {
background: var(--b);
}
.scenario-heading h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.015em;
}
.scenario-heading p {
margin: 2px 0 0;
color: var(--muted);
font-size: 10px;
}
.phase-chip {
padding: 6px 8px;
border-radius: 8px;
font-size: 11px;
font-weight: 850;
font-variant-numeric: tabular-nums;
}
#panelA .phase-chip {
color: #05665e;
background: rgba(8, 127, 115, 0.11);
}
#panelB .phase-chip {
color: #a64b2b;
background: rgba(215, 103, 63, 0.12);
}
.phase-strip {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 8px;
margin-top: 11px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.phase-track {
position: relative;
height: 8px;
overflow: visible;
border-radius: 999px;
background:
repeating-linear-gradient(
90deg,
rgba(23, 37, 34, 0.13) 0 1px,
transparent 1px 20%
),
rgba(23, 37, 34, 0.08);
}
.phase-fill {
position: absolute;
inset: 0 auto 0 0;
border-radius: inherit;
}
#panelA .phase-fill {
background: rgba(8, 127, 115, 0.52);
}
#panelB .phase-fill {
background: rgba(215, 103, 63, 0.58);
}
.phase-marker {
position: absolute;
top: 50%;
width: 12px;
height: 12px;
border: 2px solid white;
border-radius: 50%;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.18);
transform: translate(-50%, -50%);
}
#panelA .phase-marker {
background: var(--a);
}
#panelB .phase-marker {
background: var(--b);
}
.live-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 9px;
padding-top: 8px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
}
.live-state {
color: var(--ink);
font-weight: 800;
}
.metrics-card {
width: min(350px, calc(100% - 8px));
padding: 11px 13px 12px;
}
.metrics-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.clock {
color: var(--ink);
font-size: 11px;
font-variant-numeric: tabular-nums;
letter-spacing: 0;
}
.metric-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.metric {
min-width: 0;
}
.metric-value {
font-size: clamp(20px, 2vw, 27px);
font-weight: 850;
line-height: 1;
letter-spacing: -0.055em;
font-variant-numeric: tabular-nums;
}
.metric-value span {
margin-left: 2px;
font-size: 11px;
font-weight: 700;
letter-spacing: 0;
}
.metric-label {
margin-top: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
#panelA .metric-value {
color: var(--a);
}
#panelB .metric-value {
color: var(--b);
}
.wait-scale {
position: relative;
height: 6px;
margin-top: 11px;
overflow: hidden;
border-radius: 99px;
background: rgba(23, 37, 34, 0.08);
}
.wait-fill {
height: 100%;
border-radius: inherit;
transition: width 180ms ease;
}
#panelA .wait-fill {
background: var(--a);
}
#panelB .wait-fill {
background: var(--b);
}
.scale-labels {
display: flex;
justify-content: space-between;
margin-top: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 700;
}
.difference-badge {
position: absolute;
z-index: 5;
top: 16px;
left: 50%;
min-width: 160px;
padding: 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 999px;
color: #8b4028;
background: rgba(255, 246, 240, 0.93);
box-shadow: 0 7px 20px rgba(94, 48, 31, 0.11);
font-size: 10px;
font-weight: 850;
text-align: center;
pointer-events: none;
transform: translateX(-50%);
}
.footer {
position: fixed;
z-index: 8;
right: 14px;
bottom: 9px;
left: 14px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: #526760;
font-size: 10px;
font-weight: 650;
}
.footer strong {
color: #2a423b;
}
.footer .progress-track {
position: absolute;
right: 0;
bottom: 4px;
left: 0;
height: 2px;
overflow: hidden;
border-radius: 99px;
background: rgba(23, 37, 34, 0.08);
}
#progress {
width: 0;
height: 100%;
background: #315d53;
transition: width 80ms linear;
}
.stacked .panels {
display: grid;
grid-template-rows: 1fr 1fr;
}
.stacked .panel {
position: relative;
top: auto;
bottom: auto;
left: auto;
width: 100%;
padding: 8px;
}
.stacked #panelB {
left: auto;
}
.stacked .difference-badge {
display: none;
}
.stacked .scenario-card,
.stacked .metrics-card {
width: min(350px, calc(100% - 4px));
}
.stacked .metric-value {
font-size: 20px;
}
@media (max-width: 760px) {
.topbar {
top: 7px;
right: 7px;
left: 7px;
align-items: stretch;
flex-direction: column;
gap: 8px;
padding: 9px 10px;
}
.brand p {
display: none;
}
.brand h1 {
font-size: 17px;
}
.controls {
justify-content: flex-start;
}
.control-button,
.speed-select,
.offset-control {
height: 32px;
}
.offset-control {
flex: 1 1 190px;
}
.offset-control input {
flex: 1;
min-width: 70px;
}
#stage {
top: 119px;
bottom: 46px;
}
.footer {
right: 8px;
left: 8px;
font-size: 8px;
}
.footer .desktop-only {
display: none;
}
.scenario-card,
.metrics-card {
padding: 9px 10px;
}
.scenario-heading h2 {
font-size: 12px;
}
.phase-strip {
margin-top: 7px;
}
.live-line {
margin-top: 6px;
padding-top: 6px;
}
.metrics-title {
margin-bottom: 6px;
}
.wait-scale {
margin-top: 7px;
}
}
</style>
</head>
<body>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Urban mobility / timing lab</div>
<h1>Two minutes can become twelve</h1>
<p>Same service frequency. Same transfer riders. Different timetable phase.</p>
</div>
<div class="controls">
<button class="control-button primary" id="playButton" type="button">
❚❚ Pause
</button>
<button class="control-button" id="restartButton" type="button">
Restart
</button>
<label class="offset-control">
Phase B
<input
id="offsetSlider"
type="range"
min="2"
max="14"
step="1"
value="12"
aria-label="Scenario B connector phase offset"
>
<output id="offsetOutput">+12m</output>
</label>
<select class="speed-select" id="speedSelect" aria-label="Playback speed">
<option value="0.5">0.5× speed</option>
<option value="1">1× speed</option>
<option value="2" selected>2× speed</option>
<option value="4">4× speed</option>
</select>
<button class="control-button" id="cameraButton" type="button">
Reset view
</button>
</div>
</header>
<main id="stage">
<canvas id="scene" aria-label="Animated comparison of two bus transfer phases"></canvas>
<div class="panels">
<section class="panel" id="panelA">
<div class="card scenario-card">
<div class="scenario-heading">
<span class="scenario-badge">A</span>
<div>
<h2>Aligned connection</h2>
<p>Connector follows the feeder</p>
</div>
<span class="phase-chip">+2 min</span>
</div>
<div class="phase-strip">
<span>Feeder 0m</span>
<div class="phase-track">
<div class="phase-fill" style="width: 13.333%"></div>
<div class="phase-marker" style="left: 13.333%"></div>
</div>
<span>15m headway</span>
</div>
<div class="live-line">
<span class="live-state" id="liveA">10 waiting</span>
<span id="waveA">Wave 1 of 4</span>
</div>
</div>
<div class="card metrics-card">
<div class="metrics-title">
<span>Transfer outcomes</span>
<span class="clock" id="clockA">00:00</span>
</div>
<div class="metric-grid">
<div class="metric">
<div class="metric-value" id="meanA">1.6<span>min</span></div>
<div class="metric-label">Mean wait</div>
</div>
<div class="metric">
<div class="metric-value" id="p90A">2<span>min</span></div>
<div class="metric-label">90th percentile</div>
</div>
<div class="metric">
<div class="metric-value" id="ontimeA">100<span>%</span></div>
<div class="metric-label">Wait ≤ 5 min</div>
</div>
</div>
<div class="wait-scale">
<div class="wait-fill" id="waitFillA" style="width: 13.333%"></div>
</div>
<div class="scale-labels">
<span>0 min</span>
<span>15 min headway</span>
</div>
</div>
</section>
<section class="panel" id="panelB">
<div class="card scenario-card">
<div class="scenario-heading">
<span class="scenario-badge">B</span>
<div>
<h2>Delayed connection</h2>
<p>Connector arrives later in the cycle</p>
</div>
<span class="phase-chip" id="phaseChipB">+12 min</span>
</div>
<div class="phase-strip">
<span>Feeder 0m</span>
<div class="phase-track">
<div class="phase-fill" id="phaseFillB" style="width: 80%"></div>
<div class="phase-marker" id="phaseMarkerB" style="left: 80%"></div>
</div>
<span>15m headway</span>
</div>
<div class="live-line">
<span class="live-state" id="liveB">10 waiting</span>
<span id="waveB">Wave 1 of 4</span>
</div>
</div>
<div class="card metrics-card">
<div class="metrics-title">
<span>Transfer outcomes</span>
<span class="clock" id="clockB">00:00</span>
</div>
<div class="metric-grid">
<div class="metric">
<div class="metric-value" id="meanB">11.6<span>min</span></div>
<div class="metric-label">Mean wait</div>
</div>
<div class="metric">
<div class="metric-value" id="p90B">12<span>min</span></div>
<div class="metric-label">90th percentile</div>
</div>
<div class="metric">
<div class="metric-value" id="ontimeB">0<span>%</span></div>
<div class="metric-label">Wait ≤ 5 min</div>
</div>
</div>
<div class="wait-scale">
<div class="wait-fill" id="waitFillB" style="width: 80%"></div>
</div>
<div class="scale-labels">
<span>0 min</span>
<span>15 min headway</span>
</div>
</div>
</section>
</div>
<div class="difference-badge" id="differenceBadge">
B adds 10.0 min mean wait
</div>
</main>
<footer class="footer">
<span>Drag to orbit · wheel to zoom · double-click to reset</span>
<span class="desktop-only">
<strong>Fixed:</strong>
15m headway · feeder every 30m · 10 riders at t+0 and 6 at t+1
</span>
<div class="progress-track"><div id="progress"></div></div>
</footer>
<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 CONFIG = {
headway: 15,
feederInterval: 30,
horizon: 120,
waves: 4,
earlyRiders: 10,
lateRiders: 6,
offsetA: 2
};
const stage = $("#stage");
const canvas = $("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
const camera = new THREE.OrthographicCamera(-8, 8, 8, -8, 0.1, 100);
const cameraState = {
azimuth: 0,
polar: 0.72,
zoom: 1,
target: new THREE.Vector3(0, 0.2, 0)
};
let offsetB = 12;
let simTime = 0;
let playing = true;
let playbackSpeed = 2;
let lastFrame = performance.now();
let stacked = false;
let lastUiUpdate = 0;
const mod = (value, divisor) =>
((value % divisor) + divisor) % divisor;
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.86,
metalness: options.metalness ?? 0,
flatShading: options.flatShading ?? true,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
side: options.side ?? THREE.FrontSide,
depthWrite: options.depthWrite ?? true
});
}
function setShadow(mesh, cast = true, receive = true) {
mesh.castShadow = cast;
mesh.receiveShadow = receive;
return mesh;
}
function makeBox(width, height, depth, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
return setShadow(mesh);
}
function makeCylinder(
radiusTop,
radiusBottom,
height,
segments,
mat,
x = 0,
y = 0,
z = 0
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
mat
);
mesh.position.set(x, y, z);
return setShadow(mesh);
}
function makeOvalRoad(rx, rz, width, mat, y = 0.035) {
const shape = new THREE.Shape();
shape.absellipse(0, 0, rx, rz, 0, Math.PI * 2, false);
const hole = new THREE.Path();
hole.absellipse(
0,
0,
rx - width / 2,
rz - width / 2,
0,
Math.PI * 2,
true
);
shape.holes.push(hole);
const geometry = new THREE.ShapeGeometry(shape, 64);
geometry.rotateX(-Math.PI / 2);
const road = new THREE.Mesh(geometry, mat);
road.position.y = y;
road.receiveShadow = true;
return road;
}
function addRoadMarkings(world) {
const rx = 5.65;
const rz = 3.95;
for (let i = 0; i < 32; i++) {
const angle = (i / 32) * Math.PI * 2;
const x = rx * Math.cos(angle);
const z = rz * Math.sin(angle);
const tangentX = -rx * Math.sin(angle);
const tangentZ = rz * Math.cos(angle);
const dash = makeBox(0.10, 0.025, 0.46, world.dashMat);
dash.position.set(x, 0.067, z);
dash.rotation.y = Math.atan2(tangentX, tangentZ);
dash.castShadow = false;
world.scene.add(dash);
}
}
function addTree(world, x, z, scale = 1) {
const tree = new THREE.Group();
const trunk = makeCylinder(
0.09 * scale,
0.12 * scale,
0.62 * scale,
6,
world.trunkMat,
0,
0.31 * scale,
0
);
const crown = new THREE.Mesh(
new THREE.ConeGeometry(0.48 * scale, 1.05 * scale, 7),
world.leafMat
);
crown.position.y = 1.08 * scale;
crown.rotation.y = 0.3;
setShadow(crown);
tree.add(trunk, crown);
tree.position.set(x, 0.02, z);
world.scene.add(tree);
}
function addBuilding(world, x, z, width, height, depth, color) {
const building = new THREE.Group();
const wall = makeBox(
width,
height,
depth,
material(color),
0,
height / 2,
0
);
const roof = makeBox(
width + 0.12,
0.14,
depth + 0.12,
world.roofMat,
0,
height + 0.07,
0
);
const windowMat = world.windowMat;
for (const side of [-1, 1]) {
building.add(
makeBox(
width * 0.58,
0.24,
0.035,
windowMat,
0,
height * 0.62,
side * (depth / 2 + 0.021)
)
);
}
building.add(wall, roof);
building.position.set(x, 0, z);
world.scene.add(building);
}
function addShelter(world) {
const shelter = new THREE.Group();
const postMat = world.metalMat;
const glassMat = material("#91c7c0", {
transparent: true,
opacity: 0.30,
roughness: 0.2,
depthWrite: false
});
shelter.add(
makeBox(1.45, 0.12, 1.35, world.roofMat, 0, 1.50, 0),
makeBox(1.30, 0.92, 0.045, glassMat, 0, 0.88, 0.59)
);
for (const x of [-0.62, 0.62]) {
for (const z of [-0.52, 0.52]) {
shelter.add(
makeBox(0.07, 1.48, 0.07, postMat, x, 0.74, z)
);
}
}
const bench = makeBox(1.05, 0.10, 0.30, world.benchMat, 0, 0.52, 0.38);
const benchBack = makeBox(
1.05,
0.38,
0.07,
world.benchMat,
0,
0.72,
0.51
);
shelter.add(bench, benchBack);
shelter.position.set(4.38, 0.03, 1.45);
world.scene.add(shelter);
const signPole = makeCylinder(
0.035,
0.045,
1.35,
8,
world.metalMat,
5.12,
0.69,
-0.82
);
const sign = makeBox(
0.52,
0.34,
0.08,
world.signMat,
5.12,
1.40,
-0.82
);
world.scene.add(signPole, sign);
}
function makeBus(world, color, isFeeder = false) {
const bus = new THREE.Group();
const bodyMat = material(color, { roughness: 0.62 });
const trimMat = world.busTrimMat;
const glassMat = material("#27464b", { roughness: 0.32 });
const wheelMat = material("#26302f", { roughness: 0.95 });
bus.add(
makeBox(1.13, 0.64, 2.45, bodyMat, 0, 0.67, 0),
makeBox(1.02, 0.15, 2.18, trimMat, 0, 1.04, 0),
makeBox(0.96, 0.34, 0.045, glassMat, 0, 0.86, -1.24),
makeBox(0.94, 0.30, 0.04, glassMat, 0, 0.86, 1.24)
);
for (const side of [-1, 1]) {
for (const z of [-0.48, 0.42]) {
bus.add(
makeBox(
0.035,
0.29,
0.70,
glassMat,
side * 0.578,
0.86,
z
)
);
}
}
for (const x of [-0.59, 0.59]) {
for (const z of [-0.78, 0.78]) {
const wheel = makeCylinder(
0.22,
0.22,
0.15,
10,
wheelMat,
x,
0.25,
z
);
wheel.rotation.z = Math.PI / 2;
bus.add(wheel);
}
}
const lampMat = material("#fff0b5", {
emissive: "#c79c37",
emissiveIntensity: 0.4
});
for (const x of [-0.32, 0.32]) {
bus.add(
makeBox(0.16, 0.11, 0.035, lampMat, x, 0.57, -1.245)
);
}
if (isFeeder) {
bus.add(
makeBox(0.88, 0.18, 0.06, world.purpleSignMat, 0, 1.01, -1.26)
);
}
bus.traverse((object) => {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
}
});
return bus;
}
function makePerson(world, index) {
const person = new THREE.Group();
const palette = [
"#e56b4e",
"#3f8f83",
"#d7a43d",
"#6874a6",
"#a76588",
"#4f8fa8"
];
const body = makeCylinder(
0.13,
0.17,
0.38,
5,
world.personMaterials[index % world.personMaterials.length],
0,
0.30,
0
);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.135, 6, 4),
world.headMat
);
head.position.y = 0.63;
setShadow(head);
const legs = makeBox(
0.18,
0.19,
0.14,
world.personLegMat,
0,
0.10,
0
);
person.add(body, head, legs);
return person;
}
function makePulse(world, color) {
const mat = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.035, 4, 24),
mat
);
ring.rotation.x = Math.PI / 2;
ring.position.set(5.48, 0.11, 0);
world.scene.add(ring);
return ring;
}
function createWorld(accentColor) {
const scene = new THREE.Scene();
scene.background = new THREE.Color("#dce9e4");
scene.fog = new THREE.Fog("#dce9e4", 32, 62);
const hemisphere = new THREE.HemisphereLight(
"#effcf7",
"#7e8d78",
2.0
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff3d9", 3.2);
sun.position.set(-8, 14, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0005;
scene.add(sun);
const world = {
scene,
offset: CONFIG.offsetA,
connectorBus: null,
feederBus: null,
passengers: [],
connectorPulse: null,
feederPulse: null,
personMaterials: [
material("#e56b4e"),
material("#3f8f83"),
material("#d7a43d"),
material("#6874a6"),
material("#a76588"),
material("#4f8fa8")
],
headMat: material("#e7b68e"),
personLegMat: material("#37424a"),
trunkMat: material("#72513d"),
leafMat: material("#4e8963"),
roofMat: material("#536a67"),
windowMat: material("#4e7778", { roughness: 0.35 }),
benchMat: material("#b87949"),
metalMat: material("#687774", { roughness: 0.45 }),
dashMat: material("#f4f0d8"),
busTrimMat: material("#f0eee4"),
signMat: material(accentColor),
purpleSignMat: material("#8d72c9")
};
const ground = makeBox(
14,
0.50,
10,
material("#9fbea2"),
0,
-0.25,
0
);
ground.receiveShadow = true;
scene.add(ground);
const road = makeOvalRoad(
6.4,
4.6,
1.55,
material("#586462", { roughness: 0.96 })
);
scene.add(road);
addRoadMarkings(world);
const plaza = makeBox(
2.05,
0.09,
3.30,
material("#d6d1bb"),
4.50,
0.015,
1.35
);
plaza.receiveShadow = true;
scene.add(plaza);
addShelter(world);
addBuilding(world, -3.75, -2.35, 1.35, 1.25, 1.05, "#d8b48e");
addBuilding(world, -1.85, -2.70, 1.15, 0.95, 1.00, "#b8c8b2");
addBuilding(world, 0.35, -2.65, 1.50, 1.45, 1.10, "#d0a58d");
addBuilding(world, 2.35, -2.30, 1.05, 0.85, 0.95, "#b4c4c8");
addTree(world, -4.55, -1.25, 0.90);
addTree(world, -2.55, -1.10, 0.78);
addTree(world, -0.55, -1.45, 1.05);
addTree(world, 1.25, -1.30, 0.72);
addTree(world, 2.90, -1.10, 0.92);
addTree(world, -0.15, 1.65, 0.68);
addTree(world, -2.75, 1.70, 0.82);
addTree(world, 2.20, 1.75, 0.76);
world.connectorBus = makeBus(world, "#efb43d");
world.feederBus = makeBus(world, "#8069b7", true);
scene.add(world.connectorBus, world.feederBus);
for (let i = 0; i < CONFIG.earlyRiders + CONFIG.lateRiders; i++) {
const person = makePerson(world, i);
person.visible = false;
scene.add(person);
world.passengers.push(person);
}
world.connectorPulse = makePulse(world, accentColor);
world.feederPulse = makePulse(world, "#8069b7");
return world;
}
const worldA = createWorld("#087f73");
const worldB = createWorld("#d7673f");
worldB.offset = offsetB;
function placeOnRoute(object, rx, rz, time, period, phaseOffset) {
const elapsedSinceStop = mod(time - phaseOffset, period);
const angle = -(elapsedSinceStop / period) * Math.PI * 2;
const x = rx * Math.cos(angle);
const z = rz * Math.sin(angle);
const tangentX = rx * Math.sin(angle);
const tangentZ = -rz * Math.cos(angle);
object.position.set(x, 0.065, z);
object.rotation.y = Math.atan2(tangentX, tangentZ);
}
function updateWorld(world) {
placeOnRoute(
world.connectorBus,
5.72,
3.99,
simTime,
CONFIG.headway,
world.offset
);
placeOnRoute(
world.feederBus,
5.10,
3.50,
simTime,
CONFIG.feederInterval,
0
);
const wave = Math.min(
CONFIG.waves - 1,
Math.max(0, Math.floor((simTime + 0.00001) / CONFIG.feederInterval))
);
let waitingCount = 0;
world.passengers.forEach((person, index) => {
const delayedRider = index >= CONFIG.earlyRiders;
const riderDelay = delayedRider ? 1 : 0;
const arrivalTime = wave * CONFIG.feederInterval + riderDelay;
const boardingTime = wave * CONFIG.feederInterval + world.offset;
const visible =
simTime + 0.00001 >= arrivalTime &&
simTime < boardingTime - 0.00001;
person.visible = visible;
if (visible) {
waitingCount++;
const localIndex = index % CONFIG.earlyRiders;
const column = localIndex % 2;
const row = Math.floor(localIndex / 2);
person.position.set(
4.23 + column * 0.34,
0.07,
1.02 + row * 0.30
);
person.rotation.y = -Math.PI / 2;
const bob = Math.sin(simTime * 2.5 + index) * 0.012;
person.position.y += bob;
}
});
const boardingAge = simTime - (wave * CONFIG.feederInterval + world.offset);
if (boardingAge >= 0 && boardingAge < 1.35) {
world.connectorPulse.visible = true;
world.connectorPulse.scale.setScalar(1 + boardingAge * 0.62);
world.connectorPulse.material.opacity = 1 - boardingAge / 1.35;
} else {
world.connectorPulse.visible = false;
}
const feederAge = mod(simTime, CONFIG.feederInterval);
if (feederAge < 1.35) {
world.feederPulse.visible = true;
world.feederPulse.scale.setScalar(1 + feederAge * 0.55);
world.feederPulse.material.opacity = 0.7 * (1 - feederAge / 1.35);
} else {
world.feederPulse.visible = false;
}
return {
waitingCount,
boarding: boardingAge >= 0 && boardingAge < 0.7,
wave
};
}
function computeStats(offset) {
const waits = [];
for (let i = 0; i < CONFIG.earlyRiders + CONFIG.lateRiders; i++) {
const riderDelay = i >= CONFIG.earlyRiders ? 1 : 0;
waits.push(offset - riderDelay);
}
const mean = waits.reduce((sum, value) => sum + value, 0) / waits.length;
const sorted = [...waits].sort((a, b) => a - b);
const p90 = sorted[Math.ceil(sorted.length * 0.9) - 1];
const onTime = waits.filter((wait) => wait <= 5).length / waits.length;
return { mean, p90, onTime };
}
function setMetric(element, value, unit = "min") {
element.innerHTML = `${value}<span>${unit}</span>`;
}
function updateStaticUI() {
const statsA = computeStats(CONFIG.offsetA);
const statsB = computeStats(offsetB);
setMetric($("#meanA"), statsA.mean.toFixed(1));
setMetric($("#p90A"), statsA.p90.toFixed(0));
setMetric($("#ontimeA"), Math.round(statsA.onTime * 100), "%");
setMetric($("#meanB"), statsB.mean.toFixed(1));
setMetric($("#p90B"), statsB.p90.toFixed(0));
setMetric($("#ontimeB"), Math.round(statsB.onTime * 100), "%");
const percentA = (CONFIG.offsetA / CONFIG.headway) * 100;
const percentB = (offsetB / CONFIG.headway) * 100;
$("#waitFillA").style.width = `${percentA}%`;
$("#waitFillB").style.width = `${percentB}%`;
$("#phaseFillB").style.width = `${percentB}%`;
$("#phaseMarkerB").style.left = `${percentB}%`;
$("#offsetOutput").value = `+${offsetB}m`;
$("#phaseChipB").textContent = `+${offsetB} min`;
const difference = statsB.mean - statsA.mean;
$("#differenceBadge").textContent =
`B adds ${difference.toFixed(1)} min mean wait`;
}
function formatClock(time) {
const minutes = Math.floor(time);
const seconds = Math.floor((time - minutes) * 60);
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}
function updateLiveUI(statusA, statusB) {
const clock = formatClock(simTime);
$("#clockA").textContent = clock;
$("#clockB").textContent = clock;
const waveNumber = Math.min(
CONFIG.waves,
Math.floor(simTime / CONFIG.feederInterval) + 1
);
$("#waveA").textContent = `Wave ${waveNumber} of ${CONFIG.waves}`;
$("#waveB").textContent = `Wave ${waveNumber} of ${CONFIG.waves}`;
$("#liveA").textContent = statusA.boarding
? "Boarding now"
: statusA.waitingCount
? `${statusA.waitingCount} waiting`
: "Next feeder approaching";
$("#liveB").textContent = statusB.boarding
? "Boarding now"
: statusB.waitingCount
? `${statusB.waitingCount} waiting`
: "Next feeder approaching";
$("#progress").style.width =
`${Math.min(100, (simTime / CONFIG.horizon) * 100)}%`;
}
function resetSimulation() {
simTime = 0;
lastFrame = performance.now();
updateStaticUI();
}
function updateCamera() {
const radius = 34;
const { azimuth, polar, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(polar) * Math.sin(azimuth),
target.y + radius * Math.cos(polar),
target.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resetCamera() {
cameraState.azimuth = 0;
cameraState.polar = 0.72;
cameraState.zoom = 1;
updateCamera();
}
function configureCamera(viewWidth, viewHeight) {
const aspect = viewWidth / Math.max(1, viewHeight);
const verticalSize = Math.max(15.5, 15.8 / aspect);
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
camera.updateProjectionMatrix();
}
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
stacked = width < 720;
document.body.classList.toggle("stacked", stacked);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height, false);
}
function renderViews() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setScissorTest(true);
if (stacked) {
const topHeight = Math.floor(height / 2);
const bottomHeight = height - topHeight;
configureCamera(width, topHeight);
renderer.setViewport(0, bottomHeight, width, topHeight);
renderer.setScissor(0, bottomHeight, width, topHeight);
renderer.render(worldA.scene, camera);
configureCamera(width, bottomHeight);
renderer.setViewport(0, 0, width, bottomHeight);
renderer.setScissor(0, 0, width, bottomHeight);
renderer.render(worldB.scene, camera);
} else {
const leftWidth = Math.floor(width / 2);
const rightWidth = width - leftWidth;
configureCamera(leftWidth, height);
renderer.setViewport(0, 0, leftWidth, height);
renderer.setScissor(0, 0, leftWidth, height);
renderer.render(worldA.scene, camera);
configureCamera(rightWidth, height);
renderer.setViewport(leftWidth, 0, rightWidth, height);
renderer.setScissor(leftWidth, 0, rightWidth, height);
renderer.render(worldB.scene, camera);
}
renderer.setScissorTest(false);
}
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - lastFrame) / 1000, 0.1);
lastFrame = now;
if (playing) {
simTime += delta * playbackSpeed;
if (simTime >= CONFIG.horizon) {
simTime = CONFIG.horizon;
playing = false;
$("#playButton").textContent = "▶ Replay";
}
}
const statusA = updateWorld(worldA);
const statusB = updateWorld(worldB);
if (now - lastUiUpdate > 80) {
updateLiveUI(statusA, statusB);
lastUiUpdate = now;
}
updateCamera();
renderViews();
}
$("#playButton").addEventListener("click", () => {
if (simTime >= CONFIG.horizon) {
resetSimulation();
playing = true;
} else {
playing = !playing;
}
$("#playButton").textContent = playing ? "❚❚ Pause" : "▶ Play";
lastFrame = performance.now();
});
$("#restartButton").addEventListener("click", () => {
resetSimulation();
playing = true;
$("#playButton").textContent = "❚❚ Pause";
});
$("#offsetSlider").addEventListener("input", (event) => {
offsetB = Number(event.target.value);
worldB.offset = offsetB;
resetSimulation();
});
$("#speedSelect").addEventListener("change", (event) => {
playbackSpeed = Number(event.target.value);
});
$("#cameraButton").addEventListener("click", resetCamera);
let dragging = false;
let dragX = 0;
let dragY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - dragX;
const dy = event.clientY - dragY;
dragX = event.clientX;
dragY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - dy * 0.005,
0.38,
1.18
);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.85
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement ||
event.target instanceof HTMLSelectElement) {
return;
}
if (event.code === "Space") {
event.preventDefault();
$("#playButton").click();
} else if (event.key.toLowerCase() === "r") {
$("#restartButton").click();
} else if (event.key.toLowerCase() === "c") {
resetCamera();
}
});
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
window.addEventListener("resize", resize);
updateStaticUI();
resetCamera();
resize();
requestAnimationFrame(animate);
</script>
</body>
</html>
```