Prompt specification
Build or customize the Three.js 3D scene "Funicular Position Coupling Demonstration" 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: Funicular Position Coupling Demonstration
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/funicular-position-coupling-demonstration-x2098
Scene Description Prompt:
Funicular position coupling demonstration. Animate a schematic funicular where one cabin rises as the other descends. Maintain a stated cable-length relation and label omitted traction and braking dynamics. Please build this with Three.js from cdnjs in one self-contained HTML file, using a 3D cutaway.
Working Three.js Starter (funicular-position-coupling-demonstration-x2098):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Funicular Position Coupling — 3D Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #07131d;
--panel: rgba(8, 23, 34, 0.86);
--panel-border: rgba(151, 203, 221, 0.2);
--text: #e6f5fa;
--muted: #9bb8c4;
--cyan: #4fe1ed;
--orange: #ff9a43;
--yellow: #ffe16a;
--danger: #ffb36b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 65% 18%, #17394a 0%, #07131d 52%, #040b12 100%);
color: var(--text);
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;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: 16px;
box-shadow:
0 18px 55px rgba(0, 0, 0, 0.34),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px) saturate(125%);
}
.info-panel {
position: absolute;
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 18px 18px 16px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 15px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 0 0 13px;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
color: #bce9ef;
font-size: 12px;
font-weight: 700;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
.status.paused .status-dot {
background: var(--orange);
box-shadow: none;
}
.angle {
color: var(--muted);
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.formula-box {
padding: 12px 13px;
border: 1px solid rgba(79, 225, 237, 0.18);
border-radius: 12px;
background: rgba(14, 49, 62, 0.34);
}
.formula-main {
color: var(--yellow);
font-family: Georgia, "Times New Roman", serif;
font-size: 17px;
line-height: 1.35;
}
.formula-sub {
margin-top: 7px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.readouts {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 8px;
margin-top: 10px;
}
.readout {
padding: 9px 10px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.readout-label {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.readout-value {
color: var(--text);
font-size: 15px;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
.readout-value.cyan {
color: var(--cyan);
}
.readout-value.orange {
color: var(--orange);
}
.warning {
margin-top: 10px;
padding: 10px 11px;
border-left: 3px solid var(--danger);
border-radius: 7px;
background: rgba(255, 154, 67, 0.08);
color: #dfc6ad;
font-size: 11px;
line-height: 1.45;
}
.warning strong {
color: #ffd1a7;
}
.model-note {
margin: 9px 0 0;
color: #7896a4;
font-size: 10px;
line-height: 1.4;
}
.controls {
position: absolute;
left: 50%;
bottom: 20px;
display: flex;
align-items: center;
gap: 8px;
width: max-content;
max-width: calc(100vw - 40px);
padding: 10px;
transform: translateX(-50%);
}
button {
min-height: 36px;
padding: 8px 12px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition:
background 120ms ease,
border-color 120ms ease,
transform 120ms ease;
}
button:hover {
border-color: rgba(79, 225, 237, 0.55);
background: rgba(79, 225, 237, 0.1);
}
button:active {
transform: translateY(1px);
}
button.primary {
min-width: 86px;
border-color: rgba(79, 225, 237, 0.38);
background: rgba(79, 225, 237, 0.14);
color: #bff8fc;
}
.divider {
width: 1px;
height: 28px;
margin: 0 3px;
background: rgba(255, 255, 255, 0.1);
}
.speed-control {
display: flex;
align-items: center;
gap: 8px;
padding: 0 6px;
color: var(--muted);
font-size: 11px;
}
input[type="range"] {
width: 90px;
accent-color: var(--cyan);
}
.legend {
position: absolute;
right: 20px;
top: 20px;
width: 238px;
padding: 13px 14px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.legend-title {
margin-bottom: 7px;
color: var(--text);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
margin: 5px 0;
}
.swatch {
width: 19px;
height: 4px;
flex: 0 0 auto;
border-radius: 4px;
background: currentColor;
box-shadow: 0 0 8px currentColor;
}
.legend-item.a {
color: var(--orange);
}
.legend-item.b {
color: var(--cyan);
}
.legend-item.route {
color: var(--yellow);
}
.legend-item.cable {
color: #f6d78b;
}
.camera-help {
margin-top: 9px;
padding-top: 8px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: #7896a4;
font-size: 10px;
}
.pin {
position: absolute;
top: 0;
left: 0;
display: none;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.17);
border-radius: 7px;
background: rgba(4, 14, 21, 0.83);
color: var(--text);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
box-shadow: 0 7px 20px rgba(0, 0, 0, 0.25);
}
.pin::after {
position: absolute;
left: 50%;
bottom: -6px;
width: 1px;
height: 6px;
background: rgba(255, 255, 255, 0.35);
content: "";
}
.pin.a {
color: var(--orange);
}
.pin.b {
color: var(--cyan);
}
.pin.route {
color: var(--yellow);
}
.pin.cutaway {
color: #b8ced7;
}
@media (max-width: 780px) {
.info-panel {
top: 10px;
left: 10px;
width: min(340px, calc(100vw - 20px));
padding: 14px;
}
.legend {
display: none;
}
.subtitle,
.model-note {
display: none;
}
.controls {
bottom: 10px;
flex-wrap: wrap;
justify-content: center;
max-width: calc(100vw - 20px);
}
.divider {
display: none;
}
}
</style>
</head>
<body>
<div class="hud">
<section class="panel info-panel" aria-label="Simulation information">
<p class="eyebrow">Kinematic engineering model</p>
<h1>Funicular position coupling</h1>
<p class="subtitle">
A cutaway schematic of two counter-running cabins on a continuous
hauling cable. One cabin rises while the other descends.
</p>
<div class="status-row">
<div class="status" id="status">
<span class="status-dot"></span>
<span id="statusText">Running</span>
</div>
<div class="angle">Track inclination: 48.4°</div>
</div>
<div class="formula-box">
<div class="formula-main">
L<sub>route</sub> =
2L<sub>t</sub> + C<sub>c</sub> − (s<sub>A</sub> + s<sub>B</sub>)
= constant
</div>
<div class="formula-sub">
s<sub>i</sub> = q<sub>i</sub>L<sub>t</sub>,
q<sub>A</sub> + q<sub>B</sub> = 1.<br />
L<sub>t</sub> = √(18² + 16²) = 24.08 m;
C<sub>c</sub> = π(3.20) = 10.05 m.
</div>
</div>
<div class="readouts">
<div class="readout">
<span class="readout-label">Cabin A / orange</span>
<span class="readout-value orange" id="readA">q = 0.500</span>
</div>
<div class="readout">
<span class="readout-label">Cabin B / cyan</span>
<span class="readout-value cyan" id="readB">q = 0.500</span>
</div>
<div class="readout">
<span class="readout-label">Position sum</span>
<span class="readout-value" id="readSum">sA + sB = 24.08 m</span>
</div>
<div class="readout">
<span class="readout-label">Routed cable length</span>
<span class="readout-value" id="readLength">34.14 m</span>
</div>
</div>
<div class="warning">
<strong>Traction and braking dynamics omitted.</strong>
Motion is prescribed kinematically to satisfy the cable-length
constraint. No motor torque, brake force, friction, slack, elastic
stretch, passenger loading, or switch dynamics are simulated.
</div>
<p class="model-note">
s is measured upward from the lower station on each branch. The
highlighted route runs from A to the crown pulley and then to B.
</p>
</section>
<aside class="panel legend" aria-label="Legend and camera help">
<div class="legend-title">Legend</div>
<div class="legend-item a">
<span class="swatch"></span><span>Cabin A / positive travel</span>
</div>
<div class="legend-item b">
<span class="swatch"></span><span>Cabin B / opposite travel</span>
</div>
<div class="legend-item route">
<span class="swatch"></span><span>Constant cabin-to-cabin route</span>
</div>
<div class="legend-item cable">
<span class="swatch"></span><span>Continuous cable loop</span>
</div>
<div class="camera-help">
Drag to orbit · scroll to zoom<br />
Use the view buttons for standard angles.
</div>
</aside>
<div class="panel controls" aria-label="Simulation controls">
<button id="playButton" class="primary" type="button">Pause</button>
<button id="resetButton" type="button">Reset motion</button>
<span class="divider"></span>
<button id="viewThree" type="button">3/4 view</button>
<button id="viewSide" type="button">Side</button>
<button id="viewTop" type="button">Top</button>
<span class="divider"></span>
<label class="speed-control">
Speed
<input
id="speed"
type="range"
min="0.04"
max="0.28"
step="0.01"
value="0.12"
aria-label="Animation speed"
/>
</label>
</div>
<div class="pin a" id="pinA">CABIN A</div>
<div class="pin b" id="pinB">CABIN B</div>
<div class="pin" id="pinLower">LOWER STATION</div>
<div class="pin" id="pinUpper">UPPER STATION</div>
<div class="pin route" id="pinRoute">CONSTANT CABLE ROUTE</div>
<div class="pin cutaway" id="pinCutaway">CUTAWAY / COVER REMOVED</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Model constants
// -------------------------------------------------------------------------
const TRACK_RISE = 18.0;
const TRACK_RUN = 16.0;
const TRACK_LENGTH = Math.hypot(TRACK_RISE, TRACK_RUN);
const CROWN_RADIUS = 3.2;
const CROWN_ARC = Math.PI * CROWN_RADIUS;
const ROUTE_LENGTH = TRACK_LENGTH + CROWN_ARC;
const TRACK_X = [-3.2, 3.2];
const LOWER_Z = 8.0;
const UPPER_Z = -8.0;
const COLORS = {
cabinA: 0xff9438,
cabinB: 0x42dce9,
cable: 0xf4cf72,
route: 0xffe16a,
steel: 0x6f8792,
darkSteel: 0x263a45,
concrete: 0x63727a
};
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131d);
scene.fog = new THREE.FogExp2(0x07131d, 0.017);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xb9e9ff,
0x18212a,
1.65
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d7, 3.0);
sun.position.set(12, 30, 19);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -30;
sun.shadow.camera.right = 30;
sun.shadow.camera.top = 35;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x59d6ff, 1.4);
rim.position.set(-18, 13, -18);
scene.add(rim);
const warmFill = new THREE.PointLight(0xff9b4b, 20, 38, 2);
warmFill.position.set(-7, 9, 8);
scene.add(warmFill);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
...options
});
const mats = {
earthTop: material(0x3e554b, { roughness: 1 }),
earthTopRight: material(0x52685a, {
roughness: 1,
transparent: true,
opacity: 0.78
}),
trench: material(0x33454b, { roughness: 0.95 }),
strata: [
material(0x765b46, { roughness: 1 }),
material(0x53665d, { roughness: 1 }),
material(0x876849, { roughness: 1 }),
material(0x475b57, { roughness: 1 })
],
steel: material(COLORS.steel, {
roughness: 0.38,
metalness: 0.72
}),
darkSteel: material(COLORS.darkSteel, {
roughness: 0.42,
metalness: 0.7
}),
concrete: material(COLORS.concrete, { roughness: 0.92 }),
black: material(0x111a21, { roughness: 0.7 }),
glass: new THREE.MeshPhysicalMaterial({
color: 0x9eefff,
roughness: 0.12,
metalness: 0.0,
transmission: 0.28,
transparent: true,
opacity: 0.62
})
};
function terrainHeight(z) {
return ((LOWER_Z - z) / TRACK_RUN) * TRACK_RISE;
}
function makeQuad(a, b, c, d, mat, castShadow = false) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[...a, ...b, ...c, ...d],
3
)
);
geometry.setIndex([0, 1, 2, 0, 2, 3]);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, mat);
mesh.receiveShadow = true;
mesh.castShadow = castShadow;
scene.add(mesh);
return mesh;
}
function surfaceQuad(x0, x1, z0, z1, offset, mat) {
return makeQuad(
[x0, terrainHeight(z0) + offset, z0],
[x1, terrainHeight(z0) + offset, z0],
[x1, terrainHeight(z1) + offset, z1],
[x0, terrainHeight(z1) + offset, z1],
mat
);
}
function verticalWallX(x, z0, z1, topOffset, bottomOffset, mat) {
return makeQuad(
[x, terrainHeight(z0) + bottomOffset, z0],
[x, terrainHeight(z0) + topOffset, z0],
[x, terrainHeight(z1) + topOffset, z1],
[x, terrainHeight(z1) + bottomOffset, z1],
mat
);
}
function verticalWallZ(z, x0, x1, topY, bottomY, mat) {
return makeQuad(
[x0, bottomY, z],
[x1, bottomY, z],
[x1, topY, z],
[x0, topY, z],
mat
);
}
const cylinderBetween = (() => {
const up = new THREE.Vector3(0, 1, 0);
const direction = new THREE.Vector3();
return function (a, b, radius, mat, radialSegments = 8) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
direction.subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
);
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
up,
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
};
})();
// -------------------------------------------------------------------------
// Excavated terrain / 3D cutaway
// -------------------------------------------------------------------------
const terrain = new THREE.Group();
scene.add(terrain);
const Z_MIN = -12;
const Z_MAX = 12;
const TRENCH_DEPTH = 4.2;
// Outer earth banks and the lower floor of the removed cover.
surfaceQuad(-13, -6, Z_MIN, Z_MAX, 0, mats.earthTop);
surfaceQuad(7, 13, Z_MIN, Z_MAX, 0, mats.earthTopRight);
surfaceQuad(-6, 7, Z_MIN, Z_MAX, -TRENCH_DEPTH, mats.trench);
// Full-depth geological section on the left side of the trench.
for (let band = 0; band < 4; band++) {
const lower = -TRENCH_DEPTH + band * (TRENCH_DEPTH / 4);
const upper = lower + TRENCH_DEPTH / 4;
verticalWallX(
-6,
Z_MIN,
Z_MAX,
upper,
lower,
mats.strata[band]
);
}
// The near side is deliberately sectioned more deeply for visibility.
for (let band = 0; band < 2; band++) {
const lower = -1.8 + band * 0.9;
const upper = lower + 0.9;
verticalWallX(
7,
Z_MIN,
Z_MAX,
upper,
lower,
mats.strata[band]
);
}
// Outer walls and front faces give the cutaway a solid, model-like body.
verticalWallX(
-13,
Z_MIN,
Z_MAX,
0,
-9 - terrainHeight(Z_MIN),
mats.strata[1]
);
verticalWallX(
13,
Z_MIN,
Z_MAX,
0,
-9 - terrainHeight(Z_MIN),
mats.earthTopRight
);
verticalWallZ(
Z_MAX,
-13,
-6,
terrainHeight(Z_MAX),
-9,
mats.strata[0]
);
verticalWallZ(
Z_MAX,
7,
13,
terrainHeight(Z_MAX),
-9,
mats.earthTopRight
);
verticalWallZ(
Z_MAX,
-6,
7,
terrainHeight(Z_MAX) - TRENCH_DEPTH,
-9,
mats.trench
);
// Fine geological section lines on the exposed wall.
for (let band = 1; band < 4; band++) {
const offset = -TRENCH_DEPTH + band * (TRENCH_DEPTH / 4);
const points = [];
for (let i = 0; i <= 40; i++) {
const z = THREE.MathUtils.lerp(Z_MIN, Z_MAX, i / 40);
points.push(new THREE.Vector3(-5.985, terrainHeight(z) + offset, z));
}
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: 0xb59b7f,
transparent: true,
opacity: 0.28
})
);
scene.add(line);
}
// -------------------------------------------------------------------------
// Track structure
// -------------------------------------------------------------------------
const trackGroup = new THREE.Group();
scene.add(trackGroup);
function addMeshToParent(parent, geometry, mat, position, rotation = null) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(...position);
if (rotation) mesh.rotation.set(...rotation);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
for (const x of TRACK_X) {
// Two rails per track.
for (const offset of [-0.47, 0.47]) {
const curve = new THREE.LineCurve3(
new THREE.Vector3(x + offset, 0, LOWER_Z),
new THREE.Vector3(x + offset, TRACK_RISE, UPPER_Z)
);
const rail = new THREE.Mesh(
new THREE.TubeGeometry(curve, 1, 0.065, 8, false),
mats.steel
);
rail.castShadow = true;
trackGroup.add(rail);
}
// Cross ties.
for (let i = 0; i <= 14; i++) {
const q = i / 14;
const tie = addMeshToParent(
trackGroup,
new THREE.BoxGeometry(1.55, 0.12, 0.24),
mats.darkSteel,
[x, q * TRACK_RISE, LOWER_Z - q * TRACK_RUN]
);
tie.rotation.x = -Math.atan2(TRACK_RISE, TRACK_RUN);
}
// Visible support columns in the excavated trench.
for (let i = 1; i <= 6; i++) {
const q = i / 7;
const y = q * TRACK_RISE;
const z = LOWER_Z - q * TRACK_RUN;
const floorY = terrainHeight(z) - TRENCH_DEPTH + 0.12;
cylinderBetween(
[x - 0.68, floorY, z],
[x - 0.68, y - 0.12, z],
0.085,
mats.darkSteel
);
cylinderBetween(
[x + 0.68, floorY, z],
[x + 0.68, y - 0.12, z],
0.085,
mats.darkSteel
);
cylinderBetween(
[x - 0.68, floorY + 0.6, z],
[x + 0.68, y - 0.5, z],
0.05,
mats.steel
);
}
}
// Stations.
const lowerPlatform = addMeshToParent(
trackGroup,
new THREE.BoxGeometry(8.4, 0.35, 2.5),
mats.concrete,
[0, -0.12, 9.25]
);
lowerPlatform.receiveShadow = true;
const upperPlatform = addMeshToParent(
trackGroup,
new THREE.BoxGeometry(8.4, 0.35, 2.5),
mats.concrete,
[0, 18.12, -9.25]
);
upperPlatform.receiveShadow = true;
for (const stationZ of [9.3, -9.3]) {
const stationY = stationZ > 0 ? 0 : 18;
for (const x of [-3.6, 3.6]) {
cylinderBetween(
[x, stationY, stationZ],
[x, stationY + 3.1, stationZ],
0.1,
mats.darkSteel
);
}
addMeshToParent(
trackGroup,
new THREE.BoxGeometry(7.7, 0.18, 2.7),
mats.darkSteel,
[0, stationY + 3.1, stationZ]
);
}
// -------------------------------------------------------------------------
// Continuous cable path
//
// Global cable coordinate:
// left branch: bottom -> top
// crown arc: left -> right
// right branch: top -> bottom
// lower return arc: right -> left
// -------------------------------------------------------------------------
class FunicularCableCurve extends THREE.Curve {
constructor() {
super();
this.lineLength = TRACK_LENGTH;
this.arcLength = CROWN_ARC;
this.totalLength =
2 * this.lineLength + 2 * this.arcLength;
}
getPoint(t, target = new THREE.Vector3()) {
let u = THREE.MathUtils.clamp(t, 0, 1) * this.totalLength;
if (u < this.lineLength) {
const f = u / this.lineLength;
return target.set(
-CROWN_RADIUS,
f * TRACK_RISE,
LOWER_Z - f * TRACK_RUN
);
}
u -= this.lineLength;
if (u < this.arcLength) {
const theta = (u / this.arcLength) * Math.PI;
return target.set(
-CROWN_RADIUS * Math.cos(theta),
TRACK_RISE + CROWN_RADIUS * Math.sin(theta),
UPPER_Z
);
}
u -= this.arcLength;
if (u < this.lineLength) {
const f = u / this.lineLength;
return target.set(
CROWN_RADIUS,
TRACK_RISE - f * TRACK_RISE,
UPPER_Z + f * TRACK_RUN
);
}
u -= this.lineLength;
const theta = (u / this.arcLength) * Math.PI;
return target.set(
CROWN_RADIUS * Math.cos(theta),
-CROWN_RADIUS * Math.sin(theta),
LOWER_Z
);
}
}
const cableCurve = new FunicularCableCurve();
const cable = new THREE.Mesh(
new THREE.TubeGeometry(
cableCurve,
320,
0.075,
8,
true
),
material(COLORS.cable, {
roughness: 0.34,
metalness: 0.64,
emissive: 0x38270a,
emissiveIntensity: 0.35
})
);
cable.castShadow = true;
scene.add(cable);
// Crown and lower return wheels.
for (const [centerY, centerZ] of [
[TRACK_RISE, UPPER_Z],
[0, LOWER_Z]
]) {
const wheel = addMeshToParent(
scene,
new THREE.TorusGeometry(CROWN_RADIUS, 0.12, 10, 80),
mats.darkSteel,
[0, centerY, centerZ]
);
wheel.castShadow = true;
const hub = addMeshToParent(
scene,
new THREE.CylinderGeometry(0.28, 0.28, 0.32, 20),
mats.steel,
[0, centerY, centerZ],
[Math.PI / 2, 0, 0]
);
hub.castShadow = true;
}
// Bright cable-material markers show circulation direction.
const markerGeometry = new THREE.SphereGeometry(0.105, 10, 8);
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffed9b,
emissive: 0xffbd32,
emissiveIntensity: 1.0,
roughness: 0.35
});
const cableMarkers = [];
const markerCount = 22;
for (let i = 0; i < markerCount; i++) {
const marker = new THREE.Mesh(markerGeometry, markerMaterial);
scene.add(marker);
cableMarkers.push(marker);
}
// -------------------------------------------------------------------------
// Cabins
// -------------------------------------------------------------------------
function createCabin(color, label) {
const group = new THREE.Group();
const accent = material(color, {
roughness: 0.36,
metalness: 0.28,
emissive: color,
emissiveIntensity: 0.06
});
// Cable grip.
addMeshToParent(
group,
new THREE.BoxGeometry(0.48, 0.18, 0.42),
mats.darkSteel,
[0, 0, 0]
);
// Hanger.
cylinderBetween(
[0, -0.1, 0],
[0, -0.8, 0],
0.065,
mats.steel,
10
);
// Roof and lower body.
addMeshToParent(
group,
new THREE.BoxGeometry(1.72, 0.17, 1.38),
accent,
[0, -0.82, 0]
);
addMeshToParent(
group,
new THREE.BoxGeometry(1.5, 0.92, 1.18),
accent,
[0, -1.36, 0]
);
// Windows.
addMeshToParent(
group,
new THREE.BoxGeometry(1.28, 0.52, 0.035),
mats.glass,
[0, -1.24, 0.605]
);
addMeshToParent(
group,
new THREE.BoxGeometry(0.035, 0.52, 0.92),
mats.glass,
[0.765, -1.24, 0]
);
addMeshToParent(
group,
new THREE.BoxGeometry(0.035, 0.52, 0.92),
mats.glass,
[-0.765, -1.24, 0]
);
// Underframe and wheels.
addMeshToParent(
group,
new THREE.BoxGeometry(1.62, 0.13, 1.18),
mats.darkSteel,
[0, -1.89, 0]
);
for (const x of [-0.47, 0.47]) {
for (const z of [-0.42, 0.42]) {
const wheel = addMeshToParent(
group,
new THREE.CylinderGeometry(0.14, 0.14, 0.12, 14),
mats.black,
[x, -1.95, z]
);
wheel.rotation.z = Math.PI / 2;
}
}
group.userData.label = label;
scene.add(group);
return group;
}
const cabinA = createCabin(COLORS.cabinA, "A");
const cabinB = createCabin(COLORS.cabinB, "B");
function placeCabin(cabin, x, q) {
cabin.position.set(
x,
q * TRACK_RISE,
LOWER_Z - q * TRACK_RUN
);
}
// -------------------------------------------------------------------------
// Highlighted constant route from A, over the crown, to B
// -------------------------------------------------------------------------
const ROUTE_POINT_COUNT = 54;
const routePositions = new Float32Array(ROUTE_POINT_COUNT * 3);
const routeGeometry = new THREE.BufferGeometry();
routeGeometry.setAttribute(
"position",
new THREE.BufferAttribute(routePositions, 3)
);
const routeMaterial = new THREE.LineDashedMaterial({
color: COLORS.route,
dashSize: 0.42,
gapSize: 0.22,
transparent: true,
opacity: 0.92,
depthTest: true
});
const routeLine = new THREE.Line(routeGeometry, routeMaterial);
scene.add(routeLine);
function updateRouteLine(qA, qB) {
const positions = routeGeometry.attributes.position.array;
let index = 0;
const write = (x, y, z) => {
positions[index++] = x;
positions[index++] = y;
positions[index++] = z;
};
// A to left crown tangent.
write(
-CROWN_RADIUS,
qA * TRACK_RISE,
LOWER_Z - qA * TRACK_RUN
);
write(-CROWN_RADIUS, TRACK_RISE, UPPER_Z);
// Crown arc.
for (let i = 1; i <= 30; i++) {
const theta = (i / 30) * Math.PI;
write(
-CROWN_RADIUS * Math.cos(theta),
TRACK_RISE + CROWN_RADIUS * Math.sin(theta),
UPPER_Z
);
}
// Right crown tangent to B.
write(
CROWN_RADIUS,
qB * TRACK_RISE,
LOWER_Z - qB * TRACK_RUN
);
routeGeometry.attributes.position.needsUpdate = true;
routeGeometry.computeBoundingSphere();
routeLine.computeLineDistances();
}
// -------------------------------------------------------------------------
// Reference arrows: positive coupling direction
// -------------------------------------------------------------------------
function addDirectionArrow(x, q, direction, color) {
const y = q * TRACK_RISE;
const z = LOWER_Z - q * TRACK_RUN;
const tangent = new THREE.Vector3(
0,
direction * TRACK_RISE,
direction * -TRACK_RUN
).normalize();
const arrowMat = new THREE.MeshBasicMaterial({ color });
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 0.72, 8),
arrowMat
);
shaft.position.set(x, y, z);
shaft.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
scene.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.12, 0.32, 12),
arrowMat
);
head.position.copy(shaft.position).addScaledVector(tangent, 0.49);
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
scene.add(head);
}
addDirectionArrow(-4.65, 0.28, 1, COLORS.cabinA);
addDirectionArrow(4.65, 0.72, -1, COLORS.cabinB);
// -------------------------------------------------------------------------
// Camera controls implemented directly
// -------------------------------------------------------------------------
const cameraState = {
azimuth: 0.72,
polar: 1.05,
radius: 40,
target: new THREE.Vector3(0, 8.3, 0)
};
function updateCamera() {
const sinPolar = Math.sin(cameraState.polar);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPolar * Math.sin(cameraState.azimuth),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.polar),
cameraState.target.z +
cameraState.radius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.005,
0.22,
1.46
);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.025,
22,
68
);
},
{ passive: false }
);
function setView(azimuth, polar, radius = 40) {
cameraState.azimuth = azimuth;
cameraState.polar = polar;
cameraState.radius = radius;
}
document.getElementById("viewThree").onclick = () =>
setView(0.72, 1.05, 40);
document.getElementById("viewSide").onclick = () =>
setView(0.02, 1.24, 43);
document.getElementById("viewTop").onclick = () =>
setView(0.72, 0.2, 45);
// -------------------------------------------------------------------------
// UI and simulation
// -------------------------------------------------------------------------
const playButton = document.getElementById("playButton");
const status = document.getElementById("status");
const statusText = document.getElementById("statusText");
const speedInput = document.getElementById("speed");
let playing = true;
let simulationTime = 0;
let speed = Number(speedInput.value);
playButton.onclick = () => {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
statusText.textContent = playing ? "Running" : "Paused";
status.classList.toggle("paused", !playing);
};
document.getElementById("resetButton").onclick = () => {
simulationTime = 0;
};
speedInput.oninput = () => {
speed = Number(speedInput.value);
};
const readA = document.getElementById("readA");
const readB = document.getElementById("readB");
const readSum = document.getElementById("readSum");
const readLength = document.getElementById("readLength");
// -------------------------------------------------------------------------
// Screen-space labels
// -------------------------------------------------------------------------
const pins = {
a: document.getElementById("pinA"),
b: document.getElementById("pinB"),
lower: document.getElementById("pinLower"),
upper: document.getElementById("pinUpper"),
route: document.getElementById("pinRoute"),
cutaway: document.getElementById("pinCutaway")
};
const projected = new THREE.Vector3();
function positionPin(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
if (!visible) {
element.style.display = "none";
return;
}
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.display = "block";
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
const labelPosition = new THREE.Vector3();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
simulationTime += dt;
}
const phase = simulationTime * Math.PI * 2 * speed;
// The exact position coupling:
// qA + qB = 1.
// Positive delta raises A and lowers B by equal normalized travel.
const delta = 0.24 * Math.sin(phase);
const qA = 0.5 + delta;
const qB = 0.5 - delta;
placeCabin(cabinA, TRACK_X[0], qA);
placeCabin(cabinB, TRACK_X[1], qB);
updateRouteLine(qA, qB);
// Cable-material markers move by the same traveled distance.
const markerOffset = delta * TRACK_LENGTH;
for (let i = 0; i < cableMarkers.length; i++) {
const u =
(i / cableMarkers.length) * cableCurve.totalLength +
markerOffset;
const t =
((u % cableCurve.totalLength) + cableCurve.totalLength) %
cableCurve.totalLength;
cableMarkers[i].position.copy(cableCurve.getPoint(t));
}
// Numerical readouts.
const sA = qA * TRACK_LENGTH;
const sB = qB * TRACK_LENGTH;
const measuredRoute =
2 * TRACK_LENGTH + CROWN_ARC - (sA + sB);
readA.textContent = `q = ${qA.toFixed(3)}`;
readB.textContent = `q = ${qB.toFixed(3)}`;
readSum.textContent = `sA + sB = ${(sA + sB).toFixed(2)} m`;
readLength.textContent = `${measuredRoute.toFixed(2)} m`;
updateCamera();
labelPosition.copy(cabinA.position).add(new THREE.Vector3(0, 0.45, 0));
positionPin(pins.a, labelPosition);
labelPosition.copy(cabinB.position).add(new THREE.Vector3(0, 0.45, 0));
positionPin(pins.b, labelPosition);
positionPin(
pins.lower,
new THREE.Vector3(-4.7, 1.0, 10.0)
);
positionPin(
pins.upper,
new THREE.Vector3(4.8, 19.0, -10.0)
);
positionPin(
pins.route,
new THREE.Vector3(
0,
TRACK_RISE + CROWN_RADIUS + 0.45,
UPPER_Z
)
);
positionPin(
pins.cutaway,
new THREE.Vector3(7.5, 5.8, 6.0)
);
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCamera();
animate();
</script>
</body>
</html>
```