Prompt specification
Build or customize the Three.js 3D scene "Train journey graph exhibit" 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: Train journey graph exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/train-journey-graph-exhibit-x1407
Scene Description Prompt:
Compose a short train route with acceleration, cruising, and braking segments; connect each segment to corresponding graph shapes without changing the time scale between panels. Use Three.js from cdnjs in a single HTML file, with visible voxel blocks.
Working Three.js Starter (train-journey-graph-exhibit-x1407):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Train — Shared-Time Motion Study</title>
<style>
:root {
--bg: #eaf1f6;
--panel: #ffffff;
--ink: #17253a;
--muted: #65748a;
--line: #dce5ed;
--accel: #2878f0;
--cruise: #e5a91e;
--brake: #eb5968;
--shadow: 0 14px 36px rgba(31, 55, 78, 0.10);
}
* { box-sizing: border-box; }
body {
margin: 0;
color: var(--ink);
background:
radial-gradient(circle at 85% 0%, #ffffff 0, transparent 36%),
var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
.app {
width: min(1480px, calc(100% - 32px));
margin: 24px auto 36px;
}
.masthead {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 18px;
}
.eyebrow {
margin: 0 0 6px;
color: #4e6b8d;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 3.2vw, 48px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 650px;
margin: 10px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.clock-chip {
flex: none;
min-width: 165px;
padding: 13px 17px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.78);
box-shadow: var(--shadow);
}
.clock-chip span {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.clock-chip strong {
display: block;
margin-top: 3px;
font-variant-numeric: tabular-nums;
font-size: 23px;
}
.hero {
display: grid;
grid-template-columns: minmax(0, 1fr) 255px;
gap: 16px;
}
.panel {
overflow: hidden;
border: 1px solid var(--line);
border-radius: 19px;
background: var(--panel);
box-shadow: var(--shadow);
}
.scene-card { min-width: 0; }
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 15px 18px;
border-bottom: 1px solid var(--line);
}
.panel-heading strong {
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel-heading small {
color: var(--muted);
font-size: 11px;
}
#scene {
position: relative;
height: clamp(360px, 46vw, 500px);
min-height: 360px;
overflow: hidden;
background: #cfe7f2;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
.scene-tools {
position: absolute;
top: 13px;
right: 13px;
display: flex;
gap: 7px;
}
.scene-tools button {
padding: 8px 10px;
border: 1px solid rgba(255,255,255,.7);
border-radius: 9px;
color: #fff;
background: rgba(20, 36, 57, .76);
cursor: pointer;
backdrop-filter: blur(7px);
font-size: 11px;
font-weight: 700;
}
.scene-tools button:hover { background: rgba(20, 36, 57, .94); }
.scene-hint {
position: absolute;
left: 14px;
bottom: 13px;
padding: 7px 10px;
border-radius: 8px;
color: rgba(255,255,255,.94);
background: rgba(20, 36, 57, .64);
font-size: 10px;
pointer-events: none;
}
.transport {
display: grid;
grid-template-columns: auto auto minmax(100px, 1fr) auto;
align-items: center;
gap: 12px;
padding: 13px 17px;
border-top: 1px solid var(--line);
}
.control {
min-width: 82px;
padding: 9px 13px;
border: 0;
border-radius: 10px;
color: #fff;
background: #1b2b42;
cursor: pointer;
font-size: 12px;
font-weight: 800;
}
.control.secondary {
color: var(--ink);
background: #edf2f7;
}
#timeline {
width: 100%;
accent-color: #1b2b42;
cursor: pointer;
}
#rate {
min-width: 58px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--ink);
background: #fff;
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.readout {
display: flex;
flex-direction: column;
gap: 12px;
}
.metric {
padding: 18px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(255,255,255,.9);
box-shadow: var(--shadow);
}
.metric-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
}
.metric-value {
margin-top: 5px;
font-size: 30px;
font-weight: 850;
letter-spacing: -.04em;
font-variant-numeric: tabular-nums;
}
.metric-value small {
color: var(--muted);
font-size: 12px;
font-weight: 700;
letter-spacing: 0;
}
#phaseValue {
margin-top: 8px;
display: inline-block;
padding: 7px 9px;
border-radius: 8px;
color: #fff;
background: var(--accel);
font-size: 11px;
font-weight: 800;
}
.route-key {
display: grid;
gap: 10px;
}
.key-row {
display: grid;
grid-template-columns: 10px 1fr;
align-items: center;
gap: 9px;
font-size: 12px;
}
.key-row i {
width: 10px;
height: 10px;
border-radius: 3px;
}
.key-row span {
color: var(--muted);
}
.charts {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 16px;
margin-top: 16px;
}
.chart-card {
position: relative;
overflow: hidden;
}
.chart-card::before {
position: absolute;
inset: 0 0 auto;
height: 4px;
background: var(--stage-color);
content: "";
}
.chart-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 17px 18px 8px;
}
.chart-title {
display: flex;
align-items: center;
gap: 9px;
font-size: 13px;
font-weight: 850;
}
.chart-title i {
width: 9px;
height: 24px;
border-radius: 4px;
background: var(--stage-color);
}
.chart-number {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .08em;
}
.chart-card canvas {
display: block;
width: 100%;
height: 190px;
cursor: crosshair;
}
.chart-caption {
min-height: 43px;
padding: 0 18px 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.shared-note {
margin: 12px 2px 0;
color: var(--muted);
text-align: right;
font-size: 10px;
letter-spacing: .04em;
}
@media (max-width: 900px) {
.hero { grid-template-columns: 1fr; }
.readout { display: grid; grid-template-columns: repeat(3, 1fr); }
.route-key { grid-column: 1 / -1; }
.charts { grid-template-columns: 1fr; }
.chart-card canvas { height: 220px; }
}
@media (max-width: 620px) {
.app { width: min(100% - 18px, 1480px); margin-top: 14px; }
.masthead { align-items: flex-start; }
.clock-chip { display: none; }
.readout { grid-template-columns: 1fr 1fr; }
.route-key { grid-column: 1 / -1; }
.transport { grid-template-columns: auto auto 1fr; }
#rate { display: none; }
.panel-heading small { display: none; }
}
</style>
</head>
<body>
<main class="app">
<header class="masthead">
<div>
<p class="eyebrow">Voxel Rail / Shared-Time Study</p>
<h1>One route. Three motion signatures.</h1>
<p class="subtitle">
A voxel train accelerates, cruises, and brakes along one continuous route.
Every colored segment and graph panel shares the exact same 0–34 second clock.
</p>
</div>
<div class="clock-chip">
<span>Shared timeline</span>
<strong id="clockValue">00:00.0 / 00:34</strong>
</div>
</header>
<section class="hero">
<div class="panel scene-card">
<div class="panel-heading">
<strong>Continuous voxel route</strong>
<small>432 m total · 18 m/s peak speed</small>
</div>
<div id="scene" aria-label="Interactive 3D voxel railway scene">
<div class="scene-tools">
<button id="resetCamera" type="button">Reset view</button>
</div>
<div class="scene-hint">Drag to orbit · Scroll to zoom</div>
</div>
<div class="transport">
<button id="playPause" class="control" type="button">Pause</button>
<button id="replay" class="control secondary" type="button">Replay</button>
<input
id="timeline"
type="range"
min="0"
max="34"
step="0.01"
value="0"
aria-label="Shared route time"
/>
<button id="rate" type="button">1×</button>
</div>
</div>
<aside class="readout">
<div class="metric">
<div class="metric-label">Current phase</div>
<div id="phaseValue">Acceleration</div>
</div>
<div class="metric">
<div class="metric-label">Train speed</div>
<div class="metric-value"><span id="speedValue">0.0</span> <small>m/s</small></div>
</div>
<div class="metric">
<div class="metric-label">Distance traveled</div>
<div class="metric-value"><span id="distanceValue">0</span> <small>m</small></div>
</div>
<div class="metric route-key">
<div class="metric-label">Route ↔ graph colors</div>
<div class="key-row">
<i style="background:var(--accel)"></i>
<span>Acceleration · rising velocity</span>
</div>
<div class="key-row">
<i style="background:var(--cruise)"></i>
<span>Cruising · constant velocity</span>
</div>
<div class="key-row">
<i style="background:var(--brake)"></i>
<span>Braking · falling velocity</span>
</div>
</div>
</aside>
</section>
<section class="charts" aria-label="Synchronized motion graphs">
<article class="panel chart-card" style="--stage-color:var(--accel)">
<div class="chart-head">
<div class="chart-title"><i></i>Acceleration</div>
<div class="chart-number">01 / RISING</div>
</div>
<canvas id="accelGraph" aria-label="Acceleration speed graph"></canvas>
<div class="chart-caption">Linear velocity rise at +1.8 m/s², from 0 to 18 m/s.</div>
</article>
<article class="panel chart-card" style="--stage-color:var(--cruise)">
<div class="chart-head">
<div class="chart-title"><i></i>Cruising</div>
<div class="chart-number">02 / FLAT</div>
</div>
<canvas id="cruiseGraph" aria-label="Cruising speed graph"></canvas>
<div class="chart-caption">Constant 18 m/s plateau for 14 seconds.</div>
</article>
<article class="panel chart-card" style="--stage-color:var(--brake)">
<div class="chart-head">
<div class="chart-title"><i></i>Braking</div>
<div class="chart-number">03 / FALLING</div>
</div>
<canvas id="brakeGraph" aria-label="Braking speed graph"></canvas>
<div class="chart-caption">Linear velocity decrease at −1.8 m/s², ending at rest.</div>
</article>
</section>
<p class="shared-note">
All panels: identical 0–34 s horizontal scale · click a graph to scrub the route
</p>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Shared physical model. Every visualization uses these values.
// ------------------------------------------------------------
const V_MAX = 18;
const ACCEL = 1.8;
const T_ACCEL = 10;
const T_CRUISE = 14;
const T_BRAKE = 10;
const T_END = T_ACCEL + T_CRUISE + T_BRAKE;
const D_ACCEL = 0.5 * ACCEL * T_ACCEL * T_ACCEL;
const D_CRUISE = V_MAX * T_CRUISE;
const D_BRAKE = 0.5 * ACCEL * T_BRAKE * T_BRAKE;
const D_TOTAL = D_ACCEL + D_CRUISE + D_BRAKE;
const SEGMENTS = [
{ name: "Acceleration", start: 0, end: T_ACCEL, color: "#2878f0" },
{ name: "Cruising", start: T_ACCEL, end: T_ACCEL + T_CRUISE, color: "#e5a91e" },
{ name: "Braking", start: T_ACCEL + T_CRUISE, end: T_END, color: "#eb5968" }
];
function speedAt(t) {
if (t <= 0) return 0;
if (t < T_ACCEL) return ACCEL * t;
if (t < T_ACCEL + T_CRUISE) return V_MAX;
const q = t - T_ACCEL - T_CRUISE;
return Math.max(0, V_MAX - ACCEL * q);
}
function distanceAt(t) {
if (t <= 0) return 0;
if (t < T_ACCEL) return 0.5 * ACCEL * t * t;
if (t < T_ACCEL + T_CRUISE) {
return D_ACCEL + V_MAX * (t - T_ACCEL);
}
const q = t - T_ACCEL - T_CRUISE;
return D_ACCEL + D_CRUISE + V_MAX * q - 0.5 * ACCEL * q * q;
}
function phaseAt(t) {
if (t >= T_END) return "Complete";
if (t < T_ACCEL) return "Acceleration";
if (t < T_ACCEL + T_CRUISE) return "Cruising";
return "Braking";
}
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const sceneEl = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcfe7f2);
scene.fog = new THREE.Fog(0xcfe7f2, 42, 82);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 140);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
sceneEl.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xe8f7ff, 0x71836b, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2dc, 3.1);
sun.position.set(-16, 28, 17);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -35;
sun.shadow.camera.right = 35;
sun.shadow.camera.top = 28;
sun.shadow.camera.bottom = -28;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.0004;
scene.add(sun);
const materials = new Map();
function material(color) {
if (!materials.has(color)) {
materials.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02
})
);
}
return materials.get(color);
}
function box(w, h, d, color, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Instanced grass blocks.
const grassPositions = [];
for (let x = -29; x <= 29; x += 1.05) {
for (let z = -13; z <= 13; z += 1.05) {
if (Math.abs(z) < 1.55) continue;
grassPositions.push([x, z]);
}
}
const grass = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.96, 0.24, 0.96),
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1
}),
grassPositions.length
);
const dummy = new THREE.Object3D();
const grassColors = [0x88aa68, 0x93b574, 0x7fa264, 0x9dbd7e];
grassPositions.forEach(([x, z], index) => {
dummy.position.set(x, -0.18, z);
dummy.updateMatrix();
grass.setMatrixAt(index, dummy.matrix);
grass.setColorAt(
index,
new THREE.Color(grassColors[(index * 7 + Math.abs(Math.round(z))) % grassColors.length])
);
});
grass.instanceMatrix.needsUpdate = true;
grass.instanceColor.needsUpdate = true;
grass.receiveShadow = true;
scene.add(grass);
const ROUTE_START = -21;
const ROUTE_LENGTH = 42;
function distanceToX(distance) {
return ROUTE_START + (distance / D_TOTAL) * ROUTE_LENGTH;
}
// Ballast made from visible blocks.
for (let x = ROUTE_START - 0.6; x < ROUTE_START + ROUTE_LENGTH + 0.7; x += 0.58) {
const shade = Math.round(x * 10) % 2 === 0 ? 0xa9a397 : 0xb8b1a4;
box(0.52, 0.13, 1.85, shade, x, 0.02, 0);
}
// Dark sleepers.
for (let x = ROUTE_START; x <= ROUTE_START + ROUTE_LENGTH; x += 0.72) {
box(0.28, 0.13, 1.55, 0x4d493f, x, 0.13, 0);
}
// Colored rails connect each 3D route segment to its graph color.
const routeSections = [
{
color: SEGMENTS[0].color,
d0: 0,
d1: D_ACCEL
},
{
color: SEGMENTS[1].color,
d0: D_ACCEL,
d1: D_ACCEL + D_CRUISE
},
{
color: SEGMENTS[2].color,
d0: D_ACCEL + D_CRUISE,
d1: D_TOTAL
}
];
routeSections.forEach(section => {
const x0 = distanceToX(section.d0);
const x1 = distanceToX(section.d1);
const length = x1 - x0;
const center = (x0 + x1) * 0.5;
box(length, 0.10, 0.10, section.color, center, 0.29, -0.48);
box(length, 0.10, 0.10, section.color, center, 0.29, 0.48);
// Small side marker blocks reinforce the segment color.
for (let x = x0 + 0.25; x < x1; x += 1.15) {
box(0.18, 0.22, 0.18, section.color, x, 0.10, 1.25);
}
});
// Stage gates.
function gate(x, color, height = 1.5) {
box(0.22, height, 0.22, color, x, height * 0.5 + 0.1, 1.35);
box(0.75, 0.22, 0.28, color, x, height + 0.12, 1.35);
}
gate(ROUTE_START, SEGMENTS[0].color);
gate(distanceToX(D_ACCEL), SEGMENTS[1].color, 1.75);
gate(distanceToX(D_ACCEL + D_CRUISE), SEGMENTS[2].color, 2);
gate(ROUTE_START + ROUTE_LENGTH, SEGMENTS[2].color);
function labelSprite(text, color) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 112;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(20,34,52,.91)";
ctx.fillRect(0, 0, 512, 112);
ctx.fillStyle = color;
ctx.fillRect(0, 0, 15, 112);
ctx.fillStyle = "#ffffff";
ctx.font = "800 42px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 263, 57);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(4.6, 1.0, 1);
return sprite;
}
const accelLabel = labelSprite("ACCELERATE", SEGMENTS[0].color);
accelLabel.position.set(distanceToX(D_ACCEL * 0.52), 2.2, 1.45);
scene.add(accelLabel);
const cruiseLabel = labelSprite("CRUISE", SEGMENTS[1].color);
cruiseLabel.position.set(0, 2.2, 1.45);
scene.add(cruiseLabel);
const brakeLabel = labelSprite("BRAKE", SEGMENTS[2].color);
brakeLabel.position.set(
distanceToX(D_ACCEL + D_CRUISE + D_BRAKE * 0.48),
2.2,
1.45
);
scene.add(brakeLabel);
// Voxel train.
const train = new THREE.Group();
scene.add(train);
const trainYellow = 0xf3b82f;
const trainDark = 0x263548;
const windowColor = 0x74c9e5;
const redColor = 0xc8474f;
function addVehicle(centerX, locomotive = false) {
box(1.38, 0.47, 0.88, trainYellow, centerX, 0.70, 0, train);
box(1.48, 0.12, 0.98, trainDark, centerX, 0.98, 0, train);
box(0.16, 0.44, 0.80, redColor, centerX - 0.72, 0.72, 0, train);
// Cube wheels.
for (const wheelX of [centerX - 0.46, centerX + 0.46]) {
for (const wheelZ of [-0.49, 0.49]) {
box(0.26, 0.26, 0.16, trainDark, wheelX, 0.34, wheelZ, train);
}
}
if (locomotive) {
box(0.58, 0.62, 0.82, trainYellow, centerX - 0.14, 1.31, 0, train);
box(0.65, 0.10, 0.88, trainDark, centerX - 0.14, 1.66, 0, train);
box(0.09, 0.28, 0.60, windowColor, centerX + 0.19, 1.37, 0, train);
box(0.27, 0.20, 0.08, windowColor, centerX - 0.27, 1.37, 0.43, train);
box(0.27, 0.20, 0.08, windowColor, centerX - 0.27, 1.37, -0.43, train);
box(0.18, 0.27, 0.20, trainDark, centerX - 0.03, 1.91, 0, train);
const headlightMaterial = new THREE.MeshStandardMaterial({
color: 0xfff2a8,
emissive: 0xffd84d,
emissiveIntensity: 1.8
});
for (const z of [-0.23, 0.23]) {
const light = new THREE.Mesh(
new THREE.BoxGeometry(0.08, 0.12, 0.12),
headlightMaterial
);
light.position.set(centerX + 0.73, 0.77, z);
train.add(light);
}
} else {
box(0.48, 0.28, 0.06, windowColor, centerX, 0.80, 0.46, train);
box(0.48, 0.28, 0.06, windowColor, centerX, 0.80, -0.46, train);
}
}
addVehicle(0.18, true);
addVehicle(-1.35, false);
addVehicle(-2.72, false);
// Couplings.
box(0.45, 0.09, 0.09, trainDark, -0.60, 0.53, 0, train);
box(0.48, 0.09, 0.09, trainDark, -2.04, 0.53, 0, train);
// Voxel trees.
function tree(x, z, scale = 1) {
const trunk = box(0.28 * scale, 1.25 * scale, 0.28 * scale,
0x77563c, x, 0.48 * scale, z);
trunk.castShadow = true;
box(1.0 * scale, 0.65 * scale, 1.0 * scale,
0x3f8050, x, 1.05 * scale, z);
box(0.72 * scale, 0.55 * scale, 0.72 * scale,
0x4b925a, x, 1.55 * scale, z);
box(0.42 * scale, 0.42 * scale, 0.42 * scale,
0x5ca268, x, 1.95 * scale, z);
}
[
[-25, -5, 1.1], [-20, -6.2, .9], [-15, -5.2, 1.2],
[-10, -7, 1], [-4, -6.3, 1.15], [3, -6.6, .9],
[9, -5.2, 1.1], [16, -6.3, 1], [23, -5, 1.2],
[-25, 5.4, .85], [-19, 6.2, 1], [-10, 6.1, .8],
[-5, 6.8, .95], [4, 6.4, .85], [12, 6.2, 1],
[20, 5.8, 1.1], [25, 4.5, .8]
].forEach(args => tree(...args));
// Voxel buildings.
function building(x, z, w, h, d, color) {
box(w, h, d, color, x, h * 0.5 - 0.02, z);
box(w + .12, .16, d + .12, 0x405267, x, h + .05, z);
const rows = Math.max(1, Math.floor(h / 1.15));
const cols = Math.max(1, Math.floor(w / .9));
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const wx = x - w * .35 + col * (w * .7 / Math.max(1, cols - 1));
const wy = .62 + row * .95;
if (wy < h - .25) {
box(.28, .34, .06, 0x8bd2df, wx, wy, z + d * .5 + .04);
}
}
}
}
building(-27, -9.4, 3.2, 4.4, 2.5, 0xe4a36e);
building(-22, -10.1, 2.6, 3.4, 2.3, 0xd47e6d);
building(-15, -10.3, 3.6, 5.1, 2.8, 0x8aabc1);
building(-8, -10, 2.8, 3.8, 2.4, 0xc5a4cc);
building(0, -10.5, 3.7, 5.4, 2.7, 0x8eb7a8);
building(8, -10, 2.8, 4.2, 2.4, 0xe0b36d);
building(16, -10.2, 3.4, 4.8, 2.6, 0x9eaac2);
building(24, -9.5, 3.0, 3.7, 2.5, 0xd98878);
// Small station at the beginning and end.
function station(x, color) {
box(2.6, .22, 1.15, 0xd8dde1, x, .18, 2.1);
for (const dx of [-1, 1]) {
box(.16, 1.6, .16, color, x + dx, .93, 2.55);
}
box(2.8, .18, 1.4, color, x, 1.75, 2.45);
box(1.5, .8, .65, 0xe8eef1, x, 2.25, 2.45);
box(1.65, .12, .78, color, x, 2.71, 2.45);
}
station(-23.6, SEGMENTS[0].color);
station(23.3, SEGMENTS[2].color);
// Block clouds.
function cloud(x, y, z) {
box(2.2, .55, .75, 0xf5fbfd, x, y, z);
box(1.2, .85, .75, 0xf5fbfd, x - .45, y + .35, z);
box(1.3, .65, .75, 0xf5fbfd, x + .55, y + .25, z);
}
cloud(-17, 10, -12);
cloud(-4, 11.5, -15);
cloud(12, 9.5, -13);
cloud(25, 12, -16);
// ------------------------------------------------------------
// Direct camera controls: pointer orbit and wheel zoom.
// ------------------------------------------------------------
const cameraState = {
yaw: 0,
pitch: 0.56,
radius: 45,
target: new THREE.Vector3(0, 0, 0)
};
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + Math.sin(yaw) * Math.cos(pitch) * radius,
target.y + Math.sin(pitch) * radius,
target.z + Math.cos(yaw) * Math.cos(pitch) * radius
);
camera.lookAt(target);
}
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.004,
0.22,
1.15
);
});
renderer.domElement.addEventListener("pointerup", () => dragging = false);
renderer.domElement.addEventListener("pointercancel", () => dragging = false);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.025,
28,
68
);
}, { passive: false });
document.querySelector("#resetCamera").addEventListener("click", () => {
cameraState.yaw = 0;
cameraState.pitch = 0.56;
cameraState.radius = 45;
});
function resizeScene() {
const width = sceneEl.clientWidth;
const height = sceneEl.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resizeScene).observe(sceneEl);
resizeScene();
updateCamera();
// ------------------------------------------------------------
// Shared-time graph panels
// ------------------------------------------------------------
const graphCanvases = [
document.querySelector("#accelGraph"),
document.querySelector("#cruiseGraph"),
document.querySelector("#brakeGraph")
];
function rgba(hex, alpha) {
const n = parseInt(hex.slice(1), 16);
return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${alpha})`;
}
function drawGraph(canvas, segmentIndex) {
const rect = canvas.getBoundingClientRect();
if (!rect.width || !rect.height) return;
const dpr = Math.min(window.devicePixelRatio, 2);
const width = rect.width;
const height = rect.height;
const pixelWidth = Math.round(width * dpr);
const pixelHeight = Math.round(height * dpr);
if (canvas.width !== pixelWidth || canvas.height !== pixelHeight) {
canvas.width = pixelWidth;
canvas.height = pixelHeight;
}
const ctx = canvas.getContext("2d");
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, width, height);
const plot = {
left: 39,
right: width - 15,
top: 16,
bottom: height - 31
};
const x = t => plot.left + (t / T_END) * (plot.right - plot.left);
const y = v => plot.bottom - (v / V_MAX) * (plot.bottom - plot.top);
// Identical stage bands in every panel.
SEGMENTS.forEach(segment => {
ctx.fillStyle = rgba(segment.color, 0.045);
ctx.fillRect(
x(segment.start),
plot.top,
x(segment.end) - x(segment.start),
plot.bottom - plot.top
);
});
// Horizontal grid and speed labels.
ctx.font = "10px system-ui";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
[0, 6, 12, 18].forEach(value => {
ctx.strokeStyle = "#e4ebf1";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(plot.left, y(value));
ctx.lineTo(plot.right, y(value));
ctx.stroke();
ctx.fillStyle = "#7b899a";
ctx.fillText(String(value), plot.left - 7, y(value));
});
// Shared vertical grid: 0, 10, 24, 34 seconds.
ctx.textAlign = "center";
ctx.textBaseline = "top";
[0, T_ACCEL, T_ACCEL + T_CRUISE, T_END].forEach(time => {
ctx.strokeStyle = "#e4ebf1";
ctx.beginPath();
ctx.moveTo(x(time), plot.top);
ctx.lineTo(x(time), plot.bottom);
ctx.stroke();
ctx.fillStyle = "#7b899a";
ctx.fillText(`${time}s`, x(time), plot.bottom + 9);
});
ctx.save();
ctx.translate(11, (plot.top + plot.bottom) * 0.5);
ctx.rotate(-Math.PI / 2);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#7b899a";
ctx.font = "600 9px system-ui";
ctx.fillText("m/s", 0, 0);
ctx.restore();
// Complete speed trace in the background.
ctx.strokeStyle = "#cbd5df";
ctx.lineWidth = 2;
ctx.beginPath();
for (let t = 0; t <= T_END + 0.001; t += 0.05) {
const px = x(t);
const py = y(speedAt(t));
if (t === 0) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
ctx.stroke();
// Highlight only the segment represented by this panel.
const segment = SEGMENTS[segmentIndex];
ctx.strokeStyle = segment.color;
ctx.lineWidth = 4;
ctx.lineCap = "round";
ctx.lineJoin = "round";
ctx.beginPath();
for (let t = segment.start; t <= segment.end + 0.001; t += 0.04) {
const px = x(t);
const py = y(speedAt(t));
if (t === segment.start) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
ctx.stroke();
// Endpoints make the graph signature explicit.
[segment.start, segment.end].forEach(time => {
ctx.fillStyle = "#fff";
ctx.strokeStyle = segment.color;
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(x(time), y(speedAt(time)), 4.5, 0, Math.PI * 2);
ctx.fill();
ctx.stroke();
});
// One playhead across every panel.
const currentX = x(currentTime);
const currentY = y(speedAt(currentTime));
ctx.save();
ctx.setLineDash([4, 4]);
ctx.strokeStyle = "#26384d";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(currentX, plot.top);
ctx.lineTo(currentX, plot.bottom);
ctx.stroke();
ctx.restore();
ctx.fillStyle = "#26384d";
ctx.beginPath();
ctx.arc(currentX, currentY, 4.5, 0, Math.PI * 2);
ctx.fill();
ctx.strokeStyle = "#fff";
ctx.lineWidth = 2;
ctx.stroke();
}
graphCanvases.forEach((canvas, index) => {
canvas.addEventListener("click", event => {
const rect = canvas.getBoundingClientRect();
const localX = event.clientX - rect.left;
const left = 39;
const right = rect.width - 15;
const ratio = THREE.MathUtils.clamp(
(localX - left) / (right - left),
0,
1
);
currentTime = ratio * T_END;
});
});
// ------------------------------------------------------------
// Transport and synchronization
// ------------------------------------------------------------
let currentTime = 0;
let playing = true;
let playbackRate = 1;
const timeline = document.querySelector("#timeline");
const playPause = document.querySelector("#playPause");
const replay = document.querySelector("#replay");
const rateButton = document.querySelector("#rate");
function setPlaying(value) {
playing = value;
playPause.textContent = playing ? "Pause" : "Play";
}
playPause.addEventListener("click", () => {
if (currentTime >= T_END) currentTime = 0;
setPlaying(!playing);
});
replay.addEventListener("click", () => {
currentTime = 0;
setPlaying(true);
});
rateButton.addEventListener("click", () => {
playbackRate = playbackRate === 1 ? 2 : playbackRate === 2 ? 0.5 : 1;
rateButton.textContent = `${playbackRate}×`;
});
timeline.addEventListener("input", () => {
currentTime = Number(timeline.value);
});
function formatClock(time) {
const whole = Math.floor(time);
const tenths = Math.floor((time - whole) * 10);
return `00:${String(whole).padStart(2, "0")}.${tenths}`;
}
function updateInterface() {
const speed = speedAt(currentTime);
const distance = distanceAt(currentTime);
const phase = phaseAt(currentTime);
document.querySelector("#speedValue").textContent = speed.toFixed(1);
document.querySelector("#distanceValue").textContent = Math.round(distance);
document.querySelector("#clockValue").textContent =
`${formatClock(currentTime)} / 00:34`;
const phaseElement = document.querySelector("#phaseValue");
phaseElement.textContent = phase;
const phaseColor =
phase === "Acceleration" ? SEGMENTS[0].color :
phase === "Cruising" ? SEGMENTS[1].color :
phase === "Braking" ? SEGMENTS[2].color :
"#42536a";
phaseElement.style.background = phaseColor;
timeline.value = currentTime;
train.position.x = distanceToX(distance);
train.position.y = Math.sin(currentTime * 8) * 0.006;
graphCanvases.forEach((canvas, index) => drawGraph(canvas, index));
}
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
currentTime += delta * playbackRate;
if (currentTime >= T_END) {
currentTime = T_END;
setPlaying(false);
}
}
updateCamera();
updateInterface();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```