Prompt specification
Build or customize the Three.js 3D scene "Small Rail Yard Sorting Scene" 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: Small Rail Yard Sorting Scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/small-rail-yard-sorting-scene-x2131
Scene Description Prompt:
An original voxel rail yard with three sidings, coupled wagons, and explicit switch states. One HTML file using Three.js from cdnjs.
Working Three.js Starter (small-rail-yard-sorting-scene-x2131):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Rail Yard</title>
<style>
:root {
--ink: #eaf4ef;
--muted: #9eb2ad;
--panel: rgba(17, 29, 31, 0.92);
--line: rgba(220, 241, 231, 0.14);
--accent: #f0ad52;
--green: #73d49b;
--red: #ef776c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #a9c7cc;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #142426;
color: #e9f0e9;
letter-spacing: 0.16em;
font-size: 12px;
font-weight: 800;
text-transform: uppercase;
transition: opacity 0.45s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 52%, rgba(20, 34, 36, 0.18) 100%);
}
.brand {
position: fixed;
top: 22px;
left: 24px;
z-index: 3;
pointer-events: none;
text-shadow: 0 2px 14px rgba(19, 34, 35, 0.24);
}
.brand__eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 6px;
color: #f5fbf4;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.brand__eyebrow::before {
content: "";
width: 22px;
height: 3px;
background: var(--accent);
border-radius: 9px;
box-shadow: 0 0 12px rgba(240, 173, 82, 0.55);
}
.brand h1 {
margin: 0;
font-size: clamp(24px, 3vw, 39px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.brand p {
margin: 9px 0 0;
color: rgba(245, 251, 244, 0.78);
font-size: 12px;
letter-spacing: 0.035em;
}
.panel {
position: fixed;
top: 20px;
right: 20px;
z-index: 4;
width: min(324px, calc(100vw - 40px));
overflow: hidden;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(15, 27, 29, 0.28);
backdrop-filter: blur(14px);
}
.panel__head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 16px 17px 14px;
border-bottom: 1px solid var(--line);
}
.panel__head strong {
font-size: 12px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.live::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.panel__body {
padding: 15px 17px 17px;
}
.section-label {
margin: 0 0 9px;
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.destinations {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 17px;
}
.destination {
position: relative;
padding: 10px 7px 9px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
color: var(--muted);
cursor: pointer;
transition: 0.18s ease;
}
.destination:hover {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.25);
color: var(--ink);
}
.destination.active {
border-color: rgba(240, 173, 82, 0.62);
background: rgba(240, 173, 82, 0.13);
color: #ffd89a;
box-shadow: inset 0 0 0 1px rgba(240, 173, 82, 0.08);
}
.destination b {
display: block;
margin-bottom: 2px;
font-size: 17px;
line-height: 1;
}
.destination span {
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.switch-list {
display: grid;
gap: 7px;
margin-bottom: 16px;
}
.switch-row {
display: grid;
grid-template-columns: 30px 1fr 92px;
align-items: center;
gap: 8px;
min-height: 38px;
padding: 6px 7px 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.switch-row__name {
display: flex;
align-items: center;
gap: 7px;
color: #dce8e3;
font-size: 11px;
font-weight: 800;
}
.switch-row__name i {
display: block;
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--switch-color);
box-shadow: 0 0 9px color-mix(in srgb, var(--switch-color) 55%, transparent);
}
.switch-row__hint {
color: #738985;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.switch-toggle {
min-width: 0;
padding: 7px 6px;
border: 1px solid var(--line);
border-radius: 7px;
background: rgba(255, 255, 255, 0.045);
color: #a9bbb6;
cursor: pointer;
font: inherit;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: 0.18s ease;
}
.switch-toggle:hover {
border-color: rgba(255, 255, 255, 0.28);
color: white;
}
.switch-toggle.reverse {
border-color: rgba(115, 212, 155, 0.38);
background: rgba(115, 212, 155, 0.12);
color: #9ae7b8;
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
}
.action {
min-height: 42px;
padding: 0 15px;
border: 0;
border-radius: 10px;
cursor: pointer;
font: inherit;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: 0.18s ease;
}
.action.primary {
background: var(--accent);
color: #2d261c;
box-shadow: 0 8px 24px rgba(240, 173, 82, 0.2);
}
.action.primary:hover {
transform: translateY(-1px);
background: #ffc16c;
}
.action.secondary {
background: rgba(255, 255, 255, 0.055);
color: #b7c8c3;
}
.action.secondary:hover {
background: rgba(255, 255, 255, 0.1);
color: white;
}
.action:disabled {
cursor: default;
opacity: 0.42;
transform: none;
}
.status {
margin-top: 16px;
padding: 12px 13px;
border-left: 3px solid var(--accent);
border-radius: 4px 10px 10px 4px;
background: rgba(255, 255, 255, 0.04);
}
.status strong {
display: block;
margin-bottom: 4px;
color: #f2f7f2;
font-size: 11px;
letter-spacing: 0.035em;
}
.status span {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.help {
position: fixed;
left: 24px;
bottom: 20px;
z-index: 3;
display: flex;
align-items: center;
gap: 12px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.16);
border-radius: 10px;
background: rgba(20, 34, 35, 0.5);
color: rgba(239, 247, 241, 0.8);
backdrop-filter: blur(8px);
pointer-events: none;
font-size: 10px;
letter-spacing: 0.035em;
}
.key {
display: inline-grid;
min-width: 21px;
height: 21px;
place-items: center;
padding: 0 5px;
border: 1px solid rgba(255, 255, 255, 0.25);
border-bottom-width: 2px;
border-radius: 5px;
color: white;
font-size: 9px;
font-weight: 800;
}
.legend {
position: fixed;
right: 22px;
bottom: 20px;
z-index: 3;
display: flex;
gap: 12px;
color: rgba(239, 247, 241, 0.72);
pointer-events: none;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 8px rgba(115, 212, 155, 0.65);
}
.legend span:last-child i {
background: var(--red);
box-shadow: 0 0 8px rgba(239, 119, 108, 0.55);
}
@media (max-width: 700px) {
.brand {
top: 15px;
left: 15px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
width: calc(100vw - 20px);
}
.panel__head,
.panel__body {
padding-left: 13px;
padding-right: 13px;
}
.help,
.legend {
display: none;
}
}
</style>
</head>
<body>
<div id="loading">Building the yard…</div>
<div class="vignette"></div>
<header class="brand">
<div class="brand__eyebrow">Northbank / Sector 07</div>
<h1>Voxel Rail Yard</h1>
<p>Three sidings. Five coupled units. Live point control.</p>
</header>
<aside class="panel">
<div class="panel__head">
<strong>Yard Control</strong>
<span class="live">Points Live</span>
</div>
<div class="panel__body">
<p class="section-label">Destination</p>
<div class="destinations" aria-label="Select destination siding">
<button class="destination" data-destination="A">
<b>A</b><span>Ore</span>
</button>
<button class="destination active" data-destination="B">
<b>B</b><span>Boxcars</span>
</button>
<button class="destination" data-destination="C">
<b>C</b><span>Mixed</span>
</button>
</div>
<p class="section-label">Switch states</p>
<div class="switch-list">
<div class="switch-row" style="--switch-color:#f08a4b">
<span class="switch-row__name"><i></i>Switch A</span>
<span class="switch-row__hint">Siding A</span>
<button class="switch-toggle" id="switch-A" data-switch="A">Normal</button>
</div>
<div class="switch-row" style="--switch-color:#38a3a5">
<span class="switch-row__name"><i></i>Switch B</span>
<span class="switch-row__hint">Siding B</span>
<button class="switch-toggle" id="switch-B" data-switch="B">Normal</button>
</div>
<div class="switch-row" style="--switch-color:#e5b84c">
<span class="switch-row__name"><i></i>Switch C</span>
<span class="switch-row__hint">Siding C</span>
<button class="switch-toggle" id="switch-C" data-switch="C">Normal</button>
</div>
</div>
<div class="actions">
<button class="action primary" id="dispatch">Route to siding B</button>
<button class="action secondary" id="return">Return</button>
</div>
<div class="status" aria-live="polite">
<strong id="status-title">Consist on departure lead</strong>
<span id="status-detail">
Select a siding and route the coupled shunter and four wagons.
</span>
</div>
</div>
</aside>
<div class="help">
<span><span class="key">Drag</span> orbit</span>
<span><span class="key">Shift</span> + drag to pan</span>
<span><span class="key">Wheel</span> zoom</span>
<span><span class="key">1</span><span class="key">2</span><span class="key">3</span> select siding</span>
</div>
<div class="legend">
<span><i></i>Reverse</span>
<span><i></i>Normal</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const loading = document.querySelector("#loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xa9c7cc);
scene.fog = new THREE.Fog(0xa9c7cc, 57, 105);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xd9f0ef, 0x566044, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d2, 3.25);
sun.position.set(-24, 36, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 38;
sun.shadow.camera.bottom = -38;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x9ec9da, 0.75);
rim.position.set(26, 16, -30);
scene.add(rim);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
...options
});
const M = {
grass: material(0x728665),
grassDark: material(0x536a51),
concrete: material(0x9b9b89),
concreteDark: material(0x6f756c),
ballast: material(0x666c69),
ballastEdge: material(0x555c59),
sleeper: material(0x4d453a),
rail: material(0xaeb8b6, { metalness: 0.78, roughness: 0.32 }),
darkMetal: material(0x27383a, { metalness: 0.55, roughness: 0.48 }),
black: material(0x182021),
rubber: material(0x151a1a),
cream: material(0xe7dfc8),
yellow: material(0xe9a63b),
orange: material(0xdc7040),
red: material(0xa9493f),
blue: material(0x3e7195),
teal: material(0x3e8f89),
green: material(0x66875a),
window: material(0x203f4a, { metalness: 0.3, roughness: 0.25 }),
glass: material(0x80bdc6, { metalness: 0.2, roughness: 0.18 }),
brick: material(0x986b54),
roof: material(0x44585a, { metalness: 0.15 }),
wood: material(0x876444),
white: material(0xe7eee5),
lampGreen: material(0x58ca83, {
emissive: 0x58ca83,
emissiveIntensity: 1.7
}),
lampRed: material(0xef685d, {
emissive: 0xef685d,
emissiveIntensity: 1.7
})
};
function box(parent, w, h, d, x, y, z, mat, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(parent, radius, height, x, y, z, mat, segments = 12) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function group(parent, x = 0, y = 0, z = 0) {
const g = new THREE.Group();
g.position.set(x, y, z);
parent?.add(g);
return g;
}
const V = (x, z) => new THREE.Vector3(x, 0, z);
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeTextSprite(text, options = {}) {
const {
width = 512,
height = 128,
background = "rgba(18,31,33,0.88)",
foreground = "#eef7ef",
border = "rgba(255,255,255,0.16)",
scale = [5.2, 1.3, 1],
font = "800 48px system-ui"
} = options;
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, width, height);
roundedRect(ctx, 5, 5, width - 10, height - 10, 24);
ctx.fillStyle = background;
ctx.fill();
ctx.strokeStyle = border;
ctx.lineWidth = 4;
ctx.stroke();
ctx.fillStyle = foreground;
ctx.font = font;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, width / 2, height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(...scale);
sprite.renderOrder = 10;
return sprite;
}
function makeDynamicSign() {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(4.5, 1.125, 1);
sprite.renderOrder = 11;
function draw(text, active = false) {
ctx.clearRect(0, 0, 512, 128);
roundedRect(ctx, 5, 5, 502, 118, 24);
ctx.fillStyle = active ? "rgba(34,74,56,0.94)" : "rgba(20,31,33,0.92)";
ctx.fill();
ctx.strokeStyle = active
? "rgba(115,212,155,0.6)"
: "rgba(255,255,255,0.17)";
ctx.lineWidth = 4;
ctx.stroke();
ctx.beginPath();
ctx.arc(38, 64, 10, 0, Math.PI * 2);
ctx.fillStyle = active ? "#73d49b" : "#ef776c";
ctx.fill();
ctx.fillStyle = "#edf5ef";
ctx.font = "900 43px system-ui";
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillText(text, 64, 67);
texture.needsUpdate = true;
}
return { sprite, draw };
}
// -------------------------------------------------------------------------
// Ground, ballast, and rails
// -------------------------------------------------------------------------
box(scene, 86, 0.6, 76, 0, -0.55, 0, M.grassDark, false);
box(scene, 73, 0.22, 53, 0, -0.12, -5, M.grass, false);
box(scene, 66, 0.18, 47, 0, 0, -5, M.concrete, false);
// Subtle painted yard lanes.
const lanePaint = material(0xb9b39a);
for (let z = -25; z <= 10; z += 7) {
box(scene, 1.4, 0.025, 2.2, -27, 0.12, z, lanePaint, false);
box(scene, 1.4, 0.025, 2.2, 27, 0.12, z, lanePaint, false);
}
function addTrack(points) {
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (length < 0.001) continue;
const angle = -Math.atan2(dz, dx);
const mx = (a.x + b.x) * 0.5;
const mz = (a.z + b.z) * 0.5;
const perpX = -dz / length;
const perpZ = dx / length;
const ballast = box(
scene,
length + 0.06,
0.14,
2.35,
mx,
0.08,
mz,
M.ballast,
false
);
ballast.rotation.y = angle;
ballast.receiveShadow = true;
for (const side of [-1, 1]) {
const rail = box(
scene,
length + 0.1,
0.14,
0.12,
mx + perpX * side * 0.52,
0.39,
mz + perpZ * side * 0.52,
M.rail,
false
);
rail.rotation.y = angle;
}
const tieCount = Math.max(1, Math.floor(length / 1.13));
for (let j = 0; j < tieCount; j++) {
const t = (j + 0.5) / tieCount;
const tie = box(
scene,
0.25,
0.15,
1.72,
a.x + dx * t,
0.26,
a.z + dz * t,
M.sleeper,
false
);
tie.rotation.y = angle;
}
}
}
const switches = [
{ id: "A", x: -17, target: -10, color: 0xf08a4b },
{ id: "B", x: -5, target: 2, color: 0x38a3a5 },
{ id: "C", x: 7, target: 14, color: 0xe5b84c }
];
function branchPoints(s) {
return [
V(s.x, 12),
V(s.x + 1.8, 11.83),
V(s.x + 4.1, 11.05),
V(s.target, 9.35),
V(s.target, 7.65),
V(s.target, 4),
V(s.target, -22)
];
}
addTrack([V(-40, 12), V(34, 12)]);
switches.forEach((s) => {
s.points = branchPoints(s);
addTrack(s.points);
});
// -------------------------------------------------------------------------
// Physical switches, point blades, levers, lamps, and signs
// -------------------------------------------------------------------------
const switchVisuals = {};
switches.forEach((s) => {
const pivot = group(scene, s.x - 0.58, 0.45, 12);
const bladeA = box(pivot, 1.55, 0.13, 0.11, 0.76, 0, 0.31, M.rail);
const bladeB = box(pivot, 1.55, 0.13, 0.11, 0.76, 0, -0.31, M.rail);
const stand = group(scene, s.x, 0.1, 13.55);
box(stand, 0.46, 0.24, 0.46, 0, 0.12, 0, M.darkMetal);
box(stand, 0.16, 1.25, 0.16, 0, 0.75, 0, M.darkMetal);
const lever = group(stand, 0, 1.25, 0);
box(lever, 0.12, 0.95, 0.12, 0, 0.42, 0, M.cream);
box(lever, 0.34, 0.2, 0.34, 0, 0.91, 0, M.red);
const lampMat = M.lampRed.clone();
const lamp = new THREE.Mesh(
new THREE.SphereGeometry(0.19, 12, 8),
lampMat
);
lamp.position.set(s.x, 2.05, 13.55);
scene.add(lamp);
const signalPole = group(scene, s.x, 0, 13.55);
box(signalPole, 0.12, 2.25, 0.12, 0, 1.1, 0, M.darkMetal);
box(signalPole, 0.56, 0.42, 0.5, 0, 2.05, 0, M.darkMetal);
const sign = makeDynamicSign();
sign.sprite.position.set(s.x, 3.03, 13.55);
scene.add(sign.sprite);
sign.draw(`${s.id} · NORMAL`, false);
switchVisuals[s.id] = {
pivot,
bladeA,
bladeB,
lever,
lampMat,
sign,
currentAngle: 0,
targetAngle: 0,
currentLever: -0.62,
targetLever: -0.62
};
});
// -------------------------------------------------------------------------
// Siding markers and occupancy lamps
// -------------------------------------------------------------------------
const sidingNames = {
A: "A · ORE SIDING",
B: "B · BOX SIDING",
C: "C · MIXED SIDING"
};
const sidingColors = {
A: "#f2a15f",
B: "#65c2c0",
C: "#e4c267"
};
const occupancyMats = {};
switches.forEach((s) => {
const sign = makeTextSprite(sidingNames[s.id], {
foreground: sidingColors[s.id],
border: "rgba(255,255,255,0.12)",
scale: [5.4, 1.35, 1]
});
sign.position.set(s.target, 2.55, -18.5);
scene.add(sign);
const post = box(scene, 0.16, 2.2, 0.16, s.target, 1.1, -18.5, M.darkMetal);
const lampMat = material(0x40554d, {
emissive: 0x1a2a24,
emissiveIntensity: 0.4
});
const lamp = new THREE.Mesh(
new THREE.SphereGeometry(0.2, 12, 8),
lampMat
);
lamp.position.set(s.target, 2.45, -18.5);
scene.add(lamp);
occupancyMats[s.id] = lampMat;
});
// -------------------------------------------------------------------------
// Yard structures and voxel scenery
// -------------------------------------------------------------------------
function addBuilding(x, z, w, d, h, wallMat, roofMat = M.roof) {
const b = group(scene, x, 0, z);
box(b, w, h, d, 0, h / 2, 0, wallMat);
box(b, w + 0.35, 0.3, d + 0.35, 0, h + 0.12, 0, roofMat);
box(b, 1.25, 2.05, 0.12, 0, 1.02, d / 2 + 0.07, M.darkMetal);
box(b, w * 0.2, 1.05, 0.12, -w * 0.25, h * 0.62, d / 2 + 0.08, M.window);
box(b, w * 0.2, 1.05, 0.12, w * 0.25, h * 0.62, d / 2 + 0.08, M.window);
return b;
}
addBuilding(-26, -16, 8, 6, 4.1, M.brick);
addBuilding(25, -16, 10, 7, 5.1, M.concreteDark);
addBuilding(26, 2.4, 4.5, 5.4, 3.4, M.cream);
// Warehouse roof ribs.
for (let i = 0; i < 6; i++) {
box(
scene,
0.14,
0.12,
7.3,
20.8 + i * 1.65,
5.35,
-16,
M.darkMetal
);
}
// Office details.
box(scene, 2.3, 0.22, 2.3, -26, 4.32, -16, M.darkMetal);
box(scene, 0.15, 1.4, 0.15, -27.1, 5.1, -16, M.darkMetal);
box(scene, 0.7, 0.45, 0.05, -25.35, 4.85, -12.96, M.lampGreen);
function addContainer(x, z, color, rotation = 0, stack = 0) {
const c = group(scene, x, 0.22 + stack * 2.55, z);
c.rotation.y = rotation;
const mat = material(color);
box(c, 4.6, 2.35, 2.25, 0, 1.18, 0, mat);
for (let i = -2; i <= 2; i++) {
box(c, 0.08, 2.18, 2.31, i * 0.8, 1.18, 0, material(
new THREE.Color(color).multiplyScalar(0.72)
));
}
box(c, 4.7, 0.14, 2.35, 0, 0.08, 0, M.darkMetal);
return c;
}
addContainer(-25.5, -5.2, 0x3e7195, 0, 0);
addContainer(-25.5, -5.2, 0xb45e3d, 0, 1);
addContainer(-24.6, -1.1, 0xc79442, Math.PI / 2, 0);
addContainer(25.2, -6.2, 0x507c68, Math.PI / 2, 0);
addContainer(25.2, -6.2, 0x8a4c40, Math.PI / 2, 1);
// Crane.
const crane = group(scene, -24, 0, 5.2);
box(crane, 0.65, 7.2, 0.65, 0, 3.6, 0, M.yellow);
box(crane, 9, 0.42, 0.55, 2.1, 7.15, 0, M.yellow);
box(crane, 0.62, 2.25, 0.62, 0, 5.7, 0, M.darkMetal);
box(crane, 0.06, 2.75, 0.06, 5.25, 5.65, 0, M.darkMetal);
box(crane, 1.05, 0.45, 0.9, 5.25, 4.12, 0, M.red);
box(crane, 2.2, 0.45, 2.2, 0, 0.22, 0, M.concreteDark);
// Water tower.
const tower = group(scene, 27.5, 0, 3.2);
for (const x of [-0.8, 0.8]) {
for (const z of [-0.8, 0.8]) {
box(tower, 0.24, 5.2, 0.24, x, 2.6, z, M.darkMetal);
}
}
box(tower, 2.3, 2.25, 2.3, 0, 6.2, 0, M.blue);
box(tower, 2.6, 0.25, 2.6, 0, 7.42, 0, M.darkMetal);
box(tower, 0.26, 2.4, 0.26, 0, 5.0, 1.25, M.darkMetal);
box(tower, 1.4, 0.15, 0.15, 0, 4.0, 1.25, M.darkMetal);
// Platform and pallets.
box(scene, 5.6, 0.65, 2.2, -19.5, 0.33, -1.6, M.concreteDark);
for (let i = 0; i < 5; i++) {
box(
scene,
0.8,
0.5,
0.75,
21 + (i % 2) * 1.1,
0.35 + Math.floor(i / 4) * 0.5,
-5.4 - Math.floor(i / 2) * 0.9,
M.wood
);
}
function addTree(x, z, scale = 1) {
const t = group(scene, x, 0, z);
box(t, 0.55 * scale, 3 * scale, 0.55 * scale, 0, 1.5 * scale, 0, M.wood);
box(t, 2.5 * scale, 1.7 * scale, 2.5 * scale, 0, 3.55 * scale, 0, M.green);
box(t, 1.8 * scale, 1.35 * scale, 1.8 * scale, 0.25 * scale, 4.55 * scale, 0, M.grassDark);
}
[
[-34, -24, 1.2],
[-31, -29, 0.9],
[-34, -11, 1.1],
[-33, 8, 0.9],
[-20, -28, 1.0],
[32, -26, 1.15],
[34, -14, 0.9],
[33, 9, 1.1],
[19, 9, 0.85],
[-3, -29, 1.0]
].forEach((p) => addTree(...p));
// Perimeter fence.
for (let x = -35; x <= 35; x += 3.5) {
box(scene, 0.1, 1.45, 0.1, x, 0.72, -29.2, M.darkMetal);
}
box(scene, 70, 0.07, 0.07, 0, 1.15, -29.2, M.darkMetal);
box(scene, 70, 0.07, 0.07, 0, 0.68, -29.2, M.darkMetal);
// Yard lamps.
switches.forEach((s) => {
const lampPost = group(scene, s.target + 2.3, 0, 10.2);
box(lampPost, 0.12, 4.2, 0.12, 0, 2.1, 0, M.darkMetal);
box(lampPost, 0.8, 0.16, 0.5, 0.3, 4.15, 0, M.darkMetal);
box(lampPost, 0.48, 0.12, 0.36, 0.3, 4.02, 0, M.lampGreen);
});
// -------------------------------------------------------------------------
// Train construction
// -------------------------------------------------------------------------
const wheelGeometry = new THREE.CylinderGeometry(0.34, 0.34, 0.18, 12);
const hubGeometry = new THREE.CylinderGeometry(0.15, 0.15, 0.205, 10);
function addWheel(parent, x, z) {
const wheel = new THREE.Mesh(wheelGeometry, M.rubber);
wheel.rotation.x = Math.PI / 2;
wheel.position.set(x, 0.79, z);
wheel.castShadow = true;
parent.add(wheel);
const hub = new THREE.Mesh(hubGeometry, M.rail);
hub.rotation.x = Math.PI / 2;
hub.position.set(x, 0.79, z);
parent.add(hub);
}
function addVehicleWheels(parent, halfLength, axlePositions) {
for (const x of axlePositions) {
addWheel(parent, x, -0.66);
addWheel(parent, x, 0.66);
}
}
function createLocomotive() {
const g = group();
g.userData.halfLength = 1.6;
box(g, 3.2, 0.3, 1.42, 0, 1.05, 0, M.darkMetal);
box(g, 1.75, 0.95, 1.25, 0.48, 1.68, 0, M.yellow);
box(g, 1.08, 1.48, 1.42, -0.72, 1.92, 0, M.yellow);
box(g, 1.3, 0.18, 1.58, -0.72, 2.75, 0, M.darkMetal);
box(g, 0.76, 0.55, 1.47, -0.72, 2.27, 0, M.window);
box(g, 0.08, 0.58, 0.64, -0.35, 2.28, 0, M.window);
box(g, 1.18, 0.14, 1.3, 0.48, 2.2, 0, M.red);
box(g, 0.25, 0.7, 0.3, 0.93, 2.35, 0, M.darkMetal);
box(g, 0.37, 0.12, 0.42, 0.93, 2.72, 0, M.black);
box(g, 0.08, 0.23, 0.25, 1.34, 1.72, -0.4, M.lampGreen);
box(g, 0.08, 0.23, 0.25, 1.34, 1.72, 0.4, M.lampGreen);
box(g, 3.24, 0.14, 1.46, 0, 1.32, 0, M.red);
addVehicleWheels(g, 1.6, [-0.9, 0.9]);
return g;
}
function createWagon(type) {
const g = group();
g.userData.halfLength = 1.35;
box(g, 2.7, 0.28, 1.4, 0, 1.06, 0, M.darkMetal);
addVehicleWheels(g, 1.35, [-0.82, 0.82]);
if (type === "container") {
box(g, 2.25, 1.32, 1.22, 0, 1.86, 0, M.blue);
for (let i = -2; i <= 2; i++) {
box(g, 0.06, 1.22, 1.27, i * 0.42, 1.86, 0, M.teal);
}
box(g, 0.12, 1.38, 1.3, -1.13, 1.86, 0, M.darkMetal);
box(g, 0.12, 1.38, 1.3, 1.13, 1.86, 0, M.darkMetal);
} else if (type === "tank") {
const tank = new THREE.Mesh(
new THREE.CylinderGeometry(0.63, 0.63, 2.25, 8),
M.teal
);
tank.rotation.z = Math.PI / 2;
tank.position.set(0, 1.83, 0);
tank.castShadow = true;
g.add(tank);
box(g, 0.16, 1.05, 1.05, 0, 1.82, 0, M.darkMetal);
box(g, 0.42, 0.18, 0.44, 0, 2.55, 0, M.cream);
box(g, 0.35, 0.24, 1.35, 0, 1.22, 0, M.darkMetal);
} else if (type === "crates") {
box(g, 2.15, 0.92, 1.2, 0, 1.67, 0, M.wood);
box(g, 1.65, 0.76, 1.08, -0.15, 2.49, 0, M.orange);
box(g, 0.12, 0.82, 1.24, -0.15, 2.49, 0, M.cream);
box(g, 1.65, 0.1, 0.12, -0.15, 2.49, 0, M.cream);
} else {
box(g, 2.3, 1.58, 1.34, 0, 1.99, 0, M.red);
box(g, 2.5, 0.16, 1.5, 0, 2.86, 0, M.darkMetal);
box(g, 0.08, 0.55, 0.78, 1.18, 2.32, 0, M.window);
box(g, 0.7, 0.55, 0.08, -0.55, 2.32, 0.7, M.window);
box(g, 0.7, 0.55, 0.08, 0.55, 2.32, 0.7, M.window);
box(g, 0.36, 0.4, 0.34, -0.7, 3.05, 0, M.darkMetal);
}
// Buffers.
box(g, 0.18, 0.24, 0.22, 1.46, 1.08, -0.46, M.rail);
box(g, 0.18, 0.24, 0.22, 1.46, 1.08, 0.46, M.rail);
box(g, 0.18, 0.24, 0.22, -1.46, 1.08, -0.46, M.rail);
box(g, 0.18, 0.24, 0.22, -1.46, 1.08, 0.46, M.rail);
return g;
}
const units = [
createLocomotive(),
createWagon("container"),
createWagon("tank"),
createWagon("crates"),
createWagon("brake")
];
units.forEach((u) => scene.add(u));
// Coupling bars.
const couplers = [];
for (let i = 0; i < units.length - 1; i++) {
const coupler = box(
scene,
1,
0.13,
0.14,
0,
1.08,
0,
M.rail
);
couplers.push(coupler);
}
// -------------------------------------------------------------------------
// Path sampling and train movement
// -------------------------------------------------------------------------
function makePath(points) {
const segments = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (length < 0.0001) continue;
segments.push({
a,
b,
length,
start: total,
dir: new THREE.Vector3(dx / length, 0, dz / length)
});
total += length;
}
function sample(distance) {
const d = THREE.MathUtils.clamp(distance, 0, total);
let segment = segments[segments.length - 1];
for (const candidate of segments) {
if (d <= candidate.start + candidate.length) {
segment = candidate;
break;
}
}
const local = THREE.MathUtils.clamp(
(d - segment.start) / segment.length,
0,
1
);
return {
pos: new THREE.Vector3(
THREE.MathUtils.lerp(segment.a.x, segment.b.x, local),
0,
THREE.MathUtils.lerp(segment.a.z, segment.b.z, local)
),
dir: segment.dir.clone()
};
}
return { points, segments, total, sample };
}
const mainPath = makePath([V(-40, 12), V(34, 12)]);
const train = {
path: mainPath,
headS: 14,
spacing: 3.45,
moving: false,
direction: 1,
phase: "waiting",
targetS: 0,
berthed: null
};
function updateTrainTransforms() {
units.forEach((unit, i) => {
const sample = train.path.sample(train.headS - i * train.spacing);
unit.position.copy(sample.pos);
unit.rotation.y = -Math.atan2(sample.dir.z, sample.dir.x);
unit.updateMatrixWorld(true);
});
couplers.forEach((coupler, i) => {
const lead = units[i];
const follower = units[i + 1];
const rear = lead.localToWorld(
new THREE.Vector3(-lead.userData.halfLength, 1.08, 0)
);
const front = follower.localToWorld(
new THREE.Vector3(follower.userData.halfLength, 1.08, 0)
);
const delta = front.clone().sub(rear);
const length = Math.max(0.04, delta.length());
coupler.position.copy(rear).addScaledVector(delta, 0.5);
coupler.scale.x = length;
coupler.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
delta.normalize()
);
});
}
// -------------------------------------------------------------------------
// UI and yard state
// -------------------------------------------------------------------------
const switchStates = {
A: "NORMAL",
B: "NORMAL",
C: "NORMAL"
};
let selectedDestination = "B";
const statusTitle = document.querySelector("#status-title");
const statusDetail = document.querySelector("#status-detail");
const dispatchButton = document.querySelector("#dispatch");
const returnButton = document.querySelector("#return");
function setStatus(title, detail) {
statusTitle.textContent = title;
statusDetail.textContent = detail;
}
function updateOccupancy() {
switches.forEach((s) => {
const occupied = train.berthed === s.id;
occupancyMats[s.id].color.setHex(occupied ? 0x62d891 : 0x40554d);
occupancyMats[s.id].emissive.setHex(occupied ? 0x3dbf72 : 0x1a2a24);
occupancyMats[s.id].emissiveIntensity = occupied ? 1.45 : 0.35;
});
}
function updateSwitchVisual(s) {
const visual = switchVisuals[s.id];
const reverse = switchStates[s.id] === "REVERSE";
visual.targetAngle = reverse ? 0.31 : 0;
visual.targetLever = reverse ? 0.62 : -0.62;
visual.lampMat.color.setHex(reverse ? 0x58ca83 : 0xef685d);
visual.lampMat.emissive.setHex(reverse ? 0x58ca83 : 0xef685d);
visual.lampMat.emissiveIntensity = 1.7;
visual.sign.draw(`${s.id} · ${switchStates[s.id]}`, reverse);
const button = document.querySelector(`#switch-${s.id}`);
button.textContent = reverse ? "Reverse" : "Normal";
button.classList.toggle("reverse", reverse);
}
function setSwitch(id, state, announce = true) {
switchStates[id] = state;
updateSwitchVisual(switches.find((s) => s.id === id));
if (announce) {
setStatus(
`Switch ${id} set to ${state.toLowerCase()}`,
`The point indication and physical lever now show ${state.toLowerCase()}.`
);
}
}
function selectDestination(id) {
selectedDestination = id;
document.querySelectorAll(".destination").forEach((button) => {
button.classList.toggle("active", button.dataset.destination === id);
});
dispatchButton.textContent = `Route to siding ${id}`;
setStatus(
`Siding ${id} selected`,
`The dispatcher will throw Switch ${id} automatically before routing the consist.`
);
}
function beginRoute(destinationId) {
const s = switches.find((item) => item.id === destinationId);
setSwitch(destinationId, "REVERSE", false);
const routePoints = [V(-40, 12), ...s.points];
train.path = makePath(routePoints);
train.headS = 14;
train.direction = 1;
train.moving = true;
train.phase = "routing";
train.targetS =
train.path.total - (units.length - 1) * train.spacing - 0.8;
train.berthed = null;
updateOccupancy();
updatePanelAvailability();
setStatus(
`Routing consist to siding ${destinationId}`,
"Switch points are reverse. The shunter is moving through the throat with four coupled wagons."
);
}
function dispatch() {
if (train.moving) {
setStatus(
"Consist already moving",
"Wait for the current movement to finish or use Return to bring it back to the lead."
);
return;
}
if (train.berthed) {
train.pendingDestination = selectedDestination;
train.direction = -1;
train.moving = true;
train.phase = "returning";
updatePanelAvailability();
setStatus(
"Returning consist to the lead",
`The train will be re-routed to siding ${selectedDestination} after clearing the occupied siding.`
);
return;
}
beginRoute(selectedDestination);
}
function returnToLead() {
if (train.moving && train.phase === "routing") {
train.pendingDestination = null;
train.direction = -1;
train.phase = "returning";
updatePanelAvailability();
setStatus(
"Movement cancelled",
"The consist is reversing to the departure lead."
);
return;
}
if (train.phase === "waiting") {
setStatus(
"Consist already on the lead",
"Choose a destination and route the coupled train."
);
return;
}
train.pendingDestination = null;
train.direction = -1;
train.moving = true;
train.phase = "returning";
train.berthed = null;
updateOccupancy();
updatePanelAvailability();
setStatus(
"Returning consist to the lead",
"The selected switch will remain in its last set position."
);
}
function updatePanelAvailability() {
dispatchButton.disabled = train.moving;
returnButton.disabled = train.phase === "waiting" && !train.moving;
}
document.querySelectorAll(".destination").forEach((button) => {
button.addEventListener("click", () => {
selectDestination(button.dataset.destination);
});
});
document.querySelectorAll(".switch-toggle").forEach((button) => {
button.addEventListener("click", () => {
const id = button.dataset.switch;
const next =
switchStates[id] === "NORMAL" ? "REVERSE" : "NORMAL";
setSwitch(id, next);
});
});
dispatchButton.addEventListener("click", dispatch);
returnButton.addEventListener("click", returnToLead);
switches.forEach((s) => updateSwitchVisual(s));
updatePanelAvailability();
// -------------------------------------------------------------------------
// Custom camera controls: orbit, pan, wheel zoom
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 0, -3),
radius: 58,
yaw: 0.7,
pitch: 0.72
};
function updateCamera() {
const { target, radius, yaw, pitch } = cameraState;
camera.position.set(
target.x + radius * Math.sin(pitch) * Math.sin(yaw),
target.y + radius * Math.cos(pitch),
target.z + radius * Math.sin(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
}
updateCamera();
let dragging = false;
let panning = false;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
if (panning) {
const right = new THREE.Vector3();
camera.getWorldDirection(right);
right.cross(camera.up).normalize();
cameraState.target.addScaledVector(
right,
-dx * cameraState.radius * 0.0016
);
cameraState.target.z += dy * cameraState.radius * 0.0014;
} else {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.27,
1.28
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
panning = false;
if (event.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch (_) {}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
22,
82
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "1") selectDestination("A");
if (event.key === "2") selectDestination("B");
if (event.key === "3") selectDestination("C");
if (event.code === "Space") {
event.preventDefault();
dispatch();
}
if (event.key.toLowerCase() === "r") returnToLead();
if (event.key === "ArrowLeft") cameraState.yaw -= 0.08;
if (event.key === "ArrowRight") cameraState.yaw += 0.08;
if (event.key === "ArrowUp") {
cameraState.pitch = Math.min(1.28, cameraState.pitch + 0.06);
}
if (event.key === "ArrowDown") {
cameraState.pitch = Math.max(0.27, cameraState.pitch - 0.06);
}
updateCamera();
});
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
// Smooth the mechanical switch blades and point levers.
switches.forEach((s) => {
const visual = switchVisuals[s.id];
visual.currentAngle = THREE.MathUtils.lerp(
visual.currentAngle,
visual.targetAngle,
1 - Math.exp(-dt * 8)
);
visual.currentLever = THREE.MathUtils.lerp(
visual.currentLever,
visual.targetLever,
1 - Math.exp(-dt * 9)
);
visual.pivot.rotation.y = visual.currentAngle;
visual.lever.rotation.z = visual.currentLever;
});
if (train.moving) {
train.headS += train.direction * dt * 2.85;
if (train.direction > 0 && train.headS >= train.targetS) {
train.headS = train.targetS;
train.moving = false;
train.phase = "berthed";
train.berthed = selectedDestination;
train.pendingDestination = null;
updateOccupancy();
updatePanelAvailability();
setStatus(
`Consist berthed in siding ${train.berthed}`,
"All five units are coupled and standing clear of the switch. Return brings the consist to the lead."
);
} else if (train.direction < 0 && train.headS <= 14) {
train.headS = 14;
train.moving = false;
train.phase = "waiting";
train.path = mainPath;
train.berthed = null;
updateOccupancy();
updatePanelAvailability();
if (train.pendingDestination) {
const next = train.pendingDestination;
train.pendingDestination = null;
beginRoute(next);
} else {
setStatus(
"Consist on departure lead",
"Choose a siding and route the coupled shunter and four wagons."
);
}
}
}
updateTrainTransforms();
renderer.render(scene, camera);
}
updateTrainTransforms();
renderer.render(scene, camera);
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
animate();
</script>
</body>
</html>
```