Prompt specification
Build or customize the Three.js 3D scene "Two-Route Track Switch Cutaway" 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-Route Track Switch Cutaway
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-route-track-switch-cutaway-x2129
Scene Description Prompt:
Create a standalone cutaway scene in one HTML file using Three.js from cdnjs. Model a simplified railway switch with a common approach and two exits. Show movable point alignment and the selected route without disconnected rail endpoints.
Working Three.js Starter (two-route-track-switch-cutaway-x2129):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Railway Turnout — Cutaway Study</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(10, 22, 29, 0.88);
--border: rgba(170, 220, 226, 0.18);
--muted: #8ea7ad;
--text: #e7f3f4;
--cyan: #6fffea;
--orange: #ff9d45;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071118;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 19px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
color: var(--text);
backdrop-filter: blur(15px);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.32);
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 22px;
line-height: 1.15;
letter-spacing: -0.025em;
}
.subtitle {
margin: 9px 0 17px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
appearance: none;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 10px;
padding: 10px 11px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
color 160ms ease,
transform 160ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(111, 255, 234, 0.45);
}
button.active {
border-color: rgba(111, 255, 234, 0.7);
background: rgba(111, 255, 234, 0.12);
color: var(--cyan);
}
.range-wrap {
margin-top: 17px;
}
.range-label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#positionValue {
color: var(--text);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
appearance: none;
border-radius: 99px;
outline: none;
background:
linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--fill, 0%),
rgba(255, 255, 255, 0.13) var(--fill, 0%),
rgba(255, 255, 255, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid #10262d;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(111, 255, 234, 0.6);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #10262d;
border-radius: 50%;
background: var(--cyan);
}
.status {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 11px;
}
#routeStatus {
color: var(--cyan);
font-weight: 800;
letter-spacing: 0.06em;
}
.legend {
display: grid;
gap: 8px;
margin-top: 14px;
color: var(--muted);
font-size: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
}
.swatch {
width: 20px;
height: 3px;
border-radius: 99px;
}
.swatch.route {
background: var(--cyan);
box-shadow: 0 0 8px rgba(111, 255, 234, 0.7);
}
.swatch.point {
background: var(--orange);
}
.swatch.rail {
background: #a8bac1;
}
.help {
position: fixed;
z-index: 9;
right: 18px;
bottom: 16px;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 99px;
background: rgba(7, 17, 24, 0.7);
color: #829ba2;
font-size: 10px;
letter-spacing: 0.02em;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 5;
top: 0;
left: 0;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 6px;
background: rgba(8, 18, 24, 0.76);
color: #b7c9ce;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
transition:
color 180ms ease,
border-color 180ms ease,
background 180ms ease;
}
.scene-label.active {
border-color: rgba(111, 255, 234, 0.55);
background: rgba(8, 38, 40, 0.88);
color: var(--cyan);
}
.scene-label.point-label {
border-color: rgba(255, 157, 69, 0.45);
color: #ffc07e;
}
@media (max-width: 680px) {
.panel {
top: 12px;
left: 12px;
width: min(305px, calc(100vw - 24px));
padding: 15px;
}
h1 {
font-size: 19px;
}
.subtitle,
.legend {
display: none;
}
.help {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<section class="panel">
<div class="eyebrow">Infrastructure study / 04</div>
<h1>Moving-point turnout</h1>
<p class="subtitle">
A continuous common approach divides into two exits. The point blades
and route indicator move together to show the selected alignment.
</p>
<div class="switch">
<button id="leftButton" class="active" type="button">LEFT EXIT</button>
<button id="rightButton" type="button">RIGHT EXIT</button>
</div>
<div class="range-wrap">
<div class="range-label">
<span>Point alignment</span>
<span id="positionValue">0% right</span>
</div>
<input
id="position"
type="range"
min="0"
max="100"
step="1"
value="0"
aria-label="Move railway switch points"
/>
</div>
<div class="status">
<span>Selected route</span>
<span id="routeStatus">LEFT</span>
</div>
<div class="legend">
<div class="legend-item">
<span class="swatch route"></span>
Continuous selected route
</div>
<div class="legend-item">
<span class="swatch point"></span>
Movable tapered point blades
</div>
<div class="legend-item">
<span class="swatch rail"></span>
Fixed steel rail and ballast
</div>
</div>
</section>
<div id="commonLabel" class="scene-label">Common approach</div>
<div id="leftLabel" class="scene-label active">Exit A · Left</div>
<div id="rightLabel" class="scene-label">Exit B · Right</div>
<div id="pointLabel" class="scene-label point-label">Moving point</div>
<div class="help">
Drag: orbit · Wheel: zoom · Shift/right-drag: pan
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071118);
scene.fog = new THREE.Fog(0x071118, 27, 58);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xb9e9f2, 0x3b281d, 1.65);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d8, 3.3);
sun.position.set(8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x65d8e5, 1.25);
rim.position.set(-10, 7, -11);
scene.add(rim);
// -------------------------------------------------------------------------
// Track geometry constants
// -------------------------------------------------------------------------
const GAUGE = 1.56;
const HALF_GAUGE = GAUGE * 0.5;
const APPROACH_START_Z = 9.4;
const THROAT_Z = 2.3;
const EXIT_Z = -9.4;
const BRANCH_OFFSET = 4.8;
const BRANCH_LENGTH = THROAT_Z - EXIT_Z;
const RAIL_BASE_Y = 0.28;
const RAIL_TOP_Y = 0.365;
const ROUTE_Y = 0.51;
const steelBaseColor = new THREE.Color(0x26343b);
const steelTopColor = new THREE.Color(0x9aabb2);
const routeColor = new THREE.Color(0x6fffea);
const inactiveRouteColor = new THREE.Color(0x53666b);
const commonBaseMaterial = new THREE.MeshStandardMaterial({
color: steelBaseColor,
metalness: 0.86,
roughness: 0.43
});
const commonTopMaterial = new THREE.MeshStandardMaterial({
color: steelTopColor,
metalness: 0.96,
roughness: 0.22
});
function routeRailMaterials() {
return {
base: new THREE.MeshStandardMaterial({
color: steelBaseColor,
emissive: routeColor,
emissiveIntensity: 0,
metalness: 0.82,
roughness: 0.4
}),
top: new THREE.MeshStandardMaterial({
color: steelTopColor,
emissive: routeColor,
emissiveIntensity: 0,
metalness: 0.95,
roughness: 0.2
})
};
}
const leftRailMaterials = routeRailMaterials();
const rightRailMaterials = routeRailMaterials();
function smootherStep(t) {
t = THREE.MathUtils.clamp(t, 0, 1);
return t * t * t * (t * (t * 6 - 15) + 10);
}
// A linear curve through explicitly sampled points. This avoids spline
// overshoot and guarantees that every route joins the common approach at
// exactly the same coordinate.
class PolylineCurve extends THREE.Curve {
constructor(points) {
super();
this.points = points;
}
getPoint(t, target = new THREE.Vector3()) {
const scaled = t * (this.points.length - 1);
const index = Math.floor(scaled);
const nextIndex = Math.min(index + 1, this.points.length - 1);
const fraction = scaled - index;
return target
.copy(this.points[index])
.lerp(this.points[nextIndex], fraction);
}
}
function branchFrame(sign, u) {
u = THREE.MathUtils.clamp(u, 0, 1);
const s = smootherStep(u);
const dx = sign * BRANCH_OFFSET * 30 * u * u * (1 - u) * (1 - u);
const dz = -BRANCH_LENGTH;
const tangent = new THREE.Vector3(dx, 0, dz).normalize();
const normal = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const center = new THREE.Vector3(
sign * BRANCH_OFFSET * s,
0,
THROAT_Z - BRANCH_LENGTH * u
);
return { center, tangent, normal };
}
function branchRailPoints(sign, side, count = 90) {
const points = [];
for (let i = 0; i < count; i++) {
const u = i / (count - 1);
const { center, normal } = branchFrame(sign, u);
const p = center.clone().addScaledVector(normal, side * HALF_GAUGE);
p.y = RAIL_BASE_Y;
points.push(p);
}
return points;
}
function branchCenterPoints(sign, count = 90) {
const points = [];
for (let i = 0; i < count; i++) {
const u = i / (count - 1);
const { center } = branchFrame(sign, u);
center.y = ROUTE_Y;
points.push(center);
}
return points;
}
function makeTube(points, radius, material, tubularSegments = 120) {
const geometry = new THREE.TubeGeometry(
new PolylineCurve(points),
tubularSegments,
radius,
8,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeRailPair(points, baseMaterial, topMaterial) {
const base = makeTube(
points.map((p) => new THREE.Vector3(p.x, RAIL_BASE_Y, p.z)),
0.083,
baseMaterial,
150
);
const top = makeTube(
points.map((p) => new THREE.Vector3(p.x, RAIL_TOP_Y, p.z)),
0.037,
topMaterial,
150
);
scene.add(base, top);
return { base, top };
}
// -------------------------------------------------------------------------
// Cutaway ground block and exposed geological strata
// -------------------------------------------------------------------------
const topsoilMaterial = new THREE.MeshStandardMaterial({
color: 0x493a2c,
roughness: 1
});
const subsoilMaterial = new THREE.MeshStandardMaterial({
color: 0x76503a,
roughness: 1
});
const aggregateMaterial = new THREE.MeshStandardMaterial({
color: 0x555958,
roughness: 0.94
});
function makeBlock(width, height, depth, y, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.y = y;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
makeBlock(13, 0.30, 20, -0.30, topsoilMaterial);
makeBlock(13, 0.64, 20, -0.77, subsoilMaterial);
makeBlock(13, 0.60, 20, -1.39, aggregateMaterial);
const studioFloor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x09151c,
metalness: 0.05,
roughness: 0.92
})
);
studioFloor.rotation.x = -Math.PI / 2;
studioFloor.position.y = -1.71;
studioFloor.receiveShadow = true;
scene.add(studioFloor);
// Small aggregate inclusions exposed on the front cut face.
const inclusionGeometry = new THREE.IcosahedronGeometry(0.075, 0);
const inclusionMaterial = new THREE.MeshStandardMaterial({
color: 0x89908b,
roughness: 0.95
});
const inclusions = new THREE.InstancedMesh(
inclusionGeometry,
inclusionMaterial,
46
);
const dummy = new THREE.Object3D();
for (let i = 0; i < 46; i++) {
dummy.position.set(
THREE.MathUtils.randFloatSpread(12.4),
THREE.MathUtils.randFloat(-1.52, -0.24),
10.015
);
dummy.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
const scale = THREE.MathUtils.randFloat(0.55, 1.35);
dummy.scale.set(scale, scale * 0.7, scale);
dummy.updateMatrix();
inclusions.setMatrixAt(i, dummy.matrix);
}
inclusions.castShadow = true;
scene.add(inclusions);
// -------------------------------------------------------------------------
// Y-shaped ballast bed
// -------------------------------------------------------------------------
const ballastShape = new THREE.Shape();
const ballastOutline = [
[-1.75, APPROACH_START_Z],
[-1.75, 2.75],
[-6.55, EXIT_Z],
[-3.05, EXIT_Z],
[0, 1.75],
[3.05, EXIT_Z],
[6.55, EXIT_Z],
[1.75, 2.75],
[1.75, APPROACH_START_Z]
];
ballastShape.moveTo(
ballastOutline[0][0],
-ballastOutline[0][1]
);
for (let i = 1; i < ballastOutline.length; i++) {
ballastShape.lineTo(
ballastOutline[i][0],
-ballastOutline[i][1]
);
}
ballastShape.closePath();
const ballastTopMaterial = new THREE.MeshStandardMaterial({
color: 0x667177,
roughness: 1
});
const ballastSideMaterial = new THREE.MeshStandardMaterial({
color: 0x40484d,
roughness: 1
});
const ballastGeometry = new THREE.ExtrudeGeometry(ballastShape, {
depth: 0.18,
bevelEnabled: false,
curveSegments: 1
});
const ballast = new THREE.Mesh(ballastGeometry, [
ballastTopMaterial,
ballastSideMaterial
]);
ballast.rotation.x = -Math.PI / 2;
ballast.position.y = -0.18;
ballast.receiveShadow = true;
ballast.castShadow = true;
scene.add(ballast);
// Procedural stones scattered across the ballast.
const stoneGeometry = new THREE.IcosahedronGeometry(0.085, 0);
const stoneMaterial = new THREE.MeshStandardMaterial({
color: 0x788188,
roughness: 0.96
});
const stoneCount = 310;
const stones = new THREE.InstancedMesh(
stoneGeometry,
stoneMaterial,
stoneCount
);
for (let i = 0; i < stoneCount; i++) {
const z = THREE.MathUtils.randFloat(EXIT_Z + 0.25, APPROACH_START_Z - 0.2);
let centerX = 0;
if (z < THROAT_Z) {
const u = (THROAT_Z - z) / BRANCH_LENGTH;
const sign = Math.random() > 0.5 ? 1 : -1;
centerX = sign * BRANCH_OFFSET * smootherStep(u);
}
const x = centerX + THREE.MathUtils.randFloatSpread(z > THROAT_Z ? 3.0 : 2.45);
dummy.position.set(x, 0.055, z);
dummy.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
const scale = THREE.MathUtils.randFloat(0.48, 1.32);
dummy.scale.set(scale, scale * 0.55, scale);
dummy.updateMatrix();
stones.setMatrixAt(i, dummy.matrix);
}
stones.castShadow = true;
stones.receiveShadow = true;
scene.add(stones);
// -------------------------------------------------------------------------
// Sleepers
// -------------------------------------------------------------------------
const sleeperMaterial = new THREE.MeshStandardMaterial({
color: 0x4a3c31,
roughness: 0.88,
metalness: 0.02
});
const sleeperGeometry = new THREE.BoxGeometry(2.25, 0.16, 0.23);
function addSleeper(position, yaw = 0) {
const sleeper = new THREE.Mesh(sleeperGeometry, sleeperMaterial);
sleeper.position.copy(position);
sleeper.rotation.y = yaw;
sleeper.castShadow = true;
sleeper.receiveShadow = true;
scene.add(sleeper);
}
for (let z = APPROACH_START_Z - 0.3; z > 2.55; z -= 0.57) {
addSleeper(new THREE.Vector3(0, 0.13, z));
}
for (const sign of [-1, 1]) {
for (let u = 0.06; u < 0.98; u += 0.052) {
const { center, normal } = branchFrame(sign, u);
const yaw = Math.atan2(-normal.z, normal.x);
addSleeper(
new THREE.Vector3(center.x, 0.13, center.z),
yaw
);
}
}
// Long switch timbers around the moving point assembly.
for (const z of [1.55, 2.05, 2.55]) {
const timber = new THREE.Mesh(
new THREE.BoxGeometry(3.0, 0.16, 0.24),
sleeperMaterial
);
timber.position.set(0, 0.13, z);
timber.castShadow = true;
timber.receiveShadow = true;
scene.add(timber);
}
// -------------------------------------------------------------------------
// Rails
//
// The common rails terminate exactly at the switch coordinates.
// Each common rail then feeds an outer branch rail and the opposite
// inner rail. All four branch curves use the same exact throat points,
// so no selected route leaves a floating or disconnected endpoint.
// -------------------------------------------------------------------------
const commonRailPoints = [];
for (let i = 0; i < 30; i++) {
const z = THREE.MathUtils.lerp(
APPROACH_START_Z,
THROAT_Z,
i / 29
);
commonRailPoints.push(
new THREE.Vector3(-HALF_GAUGE, RAIL_BASE_Y, z),
new THREE.Vector3(HALF_GAUGE, RAIL_BASE_Y, z)
);
}
// Separate the two common rail point lists.
const commonLeftPoints = commonRailPoints.filter((_, i) => i % 2 === 0);
const commonRightPoints = commonRailPoints.filter((_, i) => i % 2 === 1);
makeRailPair(
commonLeftPoints,
commonBaseMaterial,
commonTopMaterial
);
makeRailPair(
commonRightPoints,
commonBaseMaterial,
commonTopMaterial
);
const leftOuterPoints = branchRailPoints(-1, -1);
const leftInnerPoints = branchRailPoints(-1, 1);
const rightOuterPoints = branchRailPoints(1, 1);
const rightInnerPoints = branchRailPoints(1, -1);
const leftOuterRail = makeRailPair(
leftOuterPoints,
leftRailMaterials.base,
leftRailMaterials.top
);
const leftInnerRail = makeRailPair(
leftInnerPoints,
leftRailMaterials.base,
leftRailMaterials.top
);
const rightOuterRail = makeRailPair(
rightOuterPoints,
rightRailMaterials.base,
rightRailMaterials.top
);
const rightInnerRail = makeRailPair(
rightInnerPoints,
rightRailMaterials.base,
rightRailMaterials.top
);
// Frog casting where the two inner rails cross.
const frog = new THREE.Mesh(
new THREE.BoxGeometry(0.95, 0.065, 0.62),
new THREE.MeshStandardMaterial({
color: 0x303a3f,
metalness: 0.9,
roughness: 0.36
})
);
frog.position.set(0, 0.245, 0.05);
frog.rotation.y = 0.18;
frog.castShadow = true;
scene.add(frog);
// -------------------------------------------------------------------------
// Continuous route indicators
// -------------------------------------------------------------------------
const commonRouteMaterial = new THREE.MeshBasicMaterial({
color: routeColor,
transparent: true,
opacity: 0.92
});
const leftRouteMaterial = new THREE.MeshBasicMaterial({
color: routeColor,
transparent: true,
opacity: 1
});
const rightRouteMaterial = new THREE.MeshBasicMaterial({
color: inactiveRouteColor,
transparent: true,
opacity: 0.28
});
const commonRoutePoints = [];
for (let i = 0; i < 30; i++) {
const z = THREE.MathUtils.lerp(
APPROACH_START_Z,
THROAT_Z,
i / 29
);
commonRoutePoints.push(new THREE.Vector3(0, ROUTE_Y, z));
}
makeTube(
commonRoutePoints,
0.038,
commonRouteMaterial,
70
);
makeTube(
branchCenterPoints(-1),
0.038,
leftRouteMaterial,
100
);
makeTube(
branchCenterPoints(1),
0.038,
rightRouteMaterial,
100
);
// Small glowing route nodes emphasize the exact common throat junction.
const throatNode = new THREE.Mesh(
new THREE.SphereGeometry(0.085, 16, 10),
new THREE.MeshBasicMaterial({ color: routeColor })
);
throatNode.position.set(0, ROUTE_Y, THROAT_Z);
scene.add(throatNode);
// Direction arrows are route markers, not rail segments.
const arrowGeometry = new THREE.ConeGeometry(0.105, 0.31, 3);
const commonArrowMaterial = new THREE.MeshStandardMaterial({
color: routeColor,
emissive: routeColor,
emissiveIntensity: 0.85,
transparent: true,
opacity: 0.95,
roughness: 0.25
});
const leftArrowMaterial = commonArrowMaterial.clone();
const rightArrowMaterial = commonArrowMaterial.clone();
function addArrow(position, tangent, material) {
const arrow = new THREE.Mesh(arrowGeometry, material);
arrow.position.copy(position);
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent.clone().normalize()
);
arrow.castShadow = true;
scene.add(arrow);
return arrow;
}
addArrow(
new THREE.Vector3(0, ROUTE_Y + 0.02, 7.1),
new THREE.Vector3(0, 0, -1),
commonArrowMaterial
);
addArrow(
new THREE.Vector3(0, ROUTE_Y + 0.02, 4.4),
new THREE.Vector3(0, 0, -1),
commonArrowMaterial
);
for (const sign of [-1, 1]) {
const material = sign < 0 ? leftArrowMaterial : rightArrowMaterial;
for (const u of [0.34, 0.53, 0.72, 0.91]) {
const { center, tangent } = branchFrame(sign, u);
center.y = ROUTE_Y + 0.02;
addArrow(center, tangent, material);
}
}
// -------------------------------------------------------------------------
// Movable tapered point blades
// -------------------------------------------------------------------------
function createBladeGeometry(length, height) {
const heelHalfWidth = 0.068;
const tipHalfWidth = 0.014;
const positions = new Float32Array([
-heelHalfWidth, -height / 2, 0,
heelHalfWidth, -height / 2, 0,
heelHalfWidth, height / 2, 0,
-heelHalfWidth, height / 2, 0,
-tipHalfWidth, -height / 2, -length,
tipHalfWidth, -height / 2, -length,
tipHalfWidth, height / 2, -length,
-tipHalfWidth, height / 2, -length
]);
const indices = [
0, 1, 2, 0, 2, 3,
4, 6, 5, 4, 7, 6,
0, 4, 5, 0, 5, 1,
1, 5, 6, 1, 6, 2,
2, 6, 7, 2, 7, 3,
3, 7, 4, 3, 4, 0
];
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const bladeMaterialLeft = new THREE.MeshStandardMaterial({
color: 0xd56d25,
emissive: 0xff842f,
emissiveIntensity: 0.85,
metalness: 0.72,
roughness: 0.3
});
const bladeMaterialRight = bladeMaterialLeft.clone();
const bladeGeometry = createBladeGeometry(2.45, 0.13);
const bladeLeft = new THREE.Mesh(bladeGeometry, bladeMaterialLeft);
bladeLeft.position.set(-0.67, RAIL_TOP_Y, 2.64);
bladeLeft.castShadow = true;
scene.add(bladeLeft);
const bladeRight = new THREE.Mesh(bladeGeometry, bladeMaterialRight);
bladeRight.position.set(0.67, RAIL_TOP_Y, 2.64);
bladeRight.castShadow = true;
scene.add(bladeRight);
// Visible blade pivots.
const pivotMaterial = new THREE.MeshStandardMaterial({
color: 0x303b40,
metalness: 0.9,
roughness: 0.3
});
for (const x of [-0.67, 0.67]) {
const pivot = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 0.16, 20),
pivotMaterial
);
pivot.position.set(x, 0.34, 2.64);
pivot.castShadow = true;
scene.add(pivot);
}
// Point machine and connecting rods beside the switch.
const machineMaterial = new THREE.MeshStandardMaterial({
color: 0x49565a,
metalness: 0.72,
roughness: 0.42
});
const machine = new THREE.Mesh(
new THREE.BoxGeometry(0.95, 0.46, 1.35),
machineMaterial
);
machine.position.set(2.35, 0.25, 2.7);
machine.castShadow = true;
machine.receiveShadow = true;
scene.add(machine);
const machineLid = new THREE.Mesh(
new THREE.BoxGeometry(1.03, 0.11, 1.43),
new THREE.MeshStandardMaterial({
color: 0xff9140,
metalness: 0.45,
roughness: 0.42
})
);
machineLid.position.set(2.35, 0.51, 2.7);
machineLid.castShadow = true;
scene.add(machineLid);
const rodMaterial = new THREE.MeshStandardMaterial({
color: 0xd88641,
metalness: 0.85,
roughness: 0.3
});
const mainRod = new THREE.Mesh(
new THREE.BoxGeometry(1.85, 0.055, 0.075),
rodMaterial
);
mainRod.position.set(1.38, 0.24, 2.64);
mainRod.castShadow = true;
scene.add(mainRod);
const crossRod = new THREE.Mesh(
new THREE.BoxGeometry(0.07, 0.055, 1.45),
rodMaterial
);
crossRod.position.set(0.68, 0.23, 2.25);
crossRod.castShadow = true;
scene.add(crossRod);
// -------------------------------------------------------------------------
// UI and switch state
// -------------------------------------------------------------------------
const leftButton = document.getElementById("leftButton");
const rightButton = document.getElementById("rightButton");
const slider = document.getElementById("position");
const positionValue = document.getElementById("positionValue");
const routeStatus = document.getElementById("routeStatus");
const leftLabel = document.getElementById("leftLabel");
const rightLabel = document.getElementById("rightLabel");
let targetPosition = 0;
let currentPosition = 0;
function updateRangeFill() {
slider.style.setProperty(
"--fill",
`${slider.value}%`
);
}
function setTarget(position) {
targetPosition = THREE.MathUtils.clamp(position, 0, 1);
slider.value = Math.round(targetPosition * 100);
updateRangeFill();
const route = targetPosition < 0.5 ? "LEFT" : "RIGHT";
leftButton.classList.toggle("active", route === "LEFT");
rightButton.classList.toggle("active", route === "RIGHT");
leftLabel.classList.toggle("active", route === "LEFT");
rightLabel.classList.toggle("active", route === "RIGHT");
routeStatus.textContent = route;
}
leftButton.addEventListener("click", () => setTarget(0));
rightButton.addEventListener("click", () => setTarget(1));
slider.addEventListener("input", () => {
setTarget(Number(slider.value) / 100);
});
updateRangeFill();
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and pan
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 0, -0.25),
azimuth: 0.58,
elevation: 0.49,
radius: 21.5,
dragging: false,
pointerId: null,
lastX: 0,
lastY: 0,
panning: false
};
function updateCamera() {
const horizontalRadius =
controls.radius * Math.cos(controls.elevation);
camera.position.set(
controls.target.x +
horizontalRadius * Math.sin(controls.azimuth),
controls.target.y +
controls.radius * Math.sin(controls.elevation),
controls.target.z +
horizontalRadius * Math.cos(controls.azimuth)
);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.pointerId = event.pointerId;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
controls.panning =
event.button === 2 || event.shiftKey || event.altKey;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!controls.dragging || event.pointerId !== controls.pointerId) {
return;
}
const dx = event.clientX - controls.lastX;
const dy = event.clientY - controls.lastY;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
if (controls.panning) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, new THREE.Vector3(0, 1, 0))
.normalize();
const panScale = controls.radius * 0.0014;
controls.target.addScaledVector(right, -dx * panScale);
controls.target.addScaledVector(forward, dy * panScale);
} else {
controls.azimuth -= dx * 0.006;
controls.elevation = THREE.MathUtils.clamp(
controls.elevation + dy * 0.005,
0.16,
1.25
);
}
});
function endDrag(event) {
if (event.pointerId !== controls.pointerId) return;
controls.dragging = false;
controls.pointerId = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
10,
34
);
},
{ passive: false }
);
updateCamera();
// -------------------------------------------------------------------------
// Screen-space labels
// -------------------------------------------------------------------------
const commonLabel = document.getElementById("commonLabel");
const pointLabel = document.getElementById("pointLabel");
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateLabels() {
placeLabel(
commonLabel,
new THREE.Vector3(0, 1.15, 8.35)
);
placeLabel(
leftLabel,
new THREE.Vector3(-4.8, 1.15, -8.55)
);
placeLabel(
rightLabel,
new THREE.Vector3(4.8, 1.15, -8.55)
);
const pointLabelX = THREE.MathUtils.lerp(-1.05, 1.05, currentPosition);
placeLabel(
pointLabel,
new THREE.Vector3(pointLabelX, 1.12, 2.45)
);
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const bladeSeparation = THREE.MathUtils.degToRad(16);
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-dt * 6.2);
currentPosition = THREE.MathUtils.lerp(
currentPosition,
targetPosition,
smoothing
);
if (Math.abs(currentPosition - targetPosition) < 0.0005) {
currentPosition = targetPosition;
}
const leftWeight = 1 - currentPosition;
const rightWeight = currentPosition;
// The tapered point blades rotate continuously around their real pivots.
bladeLeft.rotation.y = leftWeight * bladeSeparation;
bladeRight.rotation.y = -rightWeight * bladeSeparation;
bladeMaterialLeft.emissiveIntensity =
0.12 + leftWeight * 0.82;
bladeMaterialRight.emissiveIntensity =
0.12 + rightWeight * 0.82;
// Selected route rail colors follow the same point position.
leftRailMaterials.base.color
.copy(steelBaseColor)
.lerp(new THREE.Color(0x235e5d), leftWeight * 0.72);
leftRailMaterials.top.color
.copy(steelTopColor)
.lerp(routeColor, leftWeight * 0.78);
leftRailMaterials.base.emissiveIntensity = leftWeight * 0.36;
leftRailMaterials.top.emissiveIntensity = leftWeight * 0.55;
rightRailMaterials.base.color
.copy(steelBaseColor)
.lerp(new THREE.Color(0x235e5d), rightWeight * 0.72);
rightRailMaterials.top.color
.copy(steelTopColor)
.lerp(routeColor, rightWeight * 0.78);
rightRailMaterials.base.emissiveIntensity = rightWeight * 0.36;
rightRailMaterials.top.emissiveIntensity = rightWeight * 0.55;
leftRouteMaterial.color
.copy(inactiveRouteColor)
.lerp(routeColor, leftWeight);
leftRouteMaterial.opacity = 0.28 + leftWeight * 0.72;
rightRouteMaterial.color
.copy(inactiveRouteColor)
.lerp(routeColor, rightWeight);
rightRouteMaterial.opacity = 0.28 + rightWeight * 0.72;
leftArrowMaterial.opacity = 0.12 + leftWeight * 0.83;
rightArrowMaterial.opacity = 0.12 + rightWeight * 0.83;
positionValue.textContent =
`${Math.round(currentPosition * 100)}% right`;
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```