Prompt specification
Build or customize the Three.js 3D scene "Inclined Track and Countermoving Cabins" 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: Inclined Track and Countermoving Cabins
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/inclined-track-and-countermoving-cabins-x2097
Scene Description Prompt:
Two low-poly funicular cabins move oppositely along inclined tracks, linked by a visible cable over a top pulley. One HTML file using Three.js from cdnjs.
Working Three.js Starter (inclined-track-and-countermoving-cabins-x2097):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Funicular</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d9e8;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 22px;
left: 22px;
z-index: 10;
width: min(330px, calc(100vw - 44px));
padding: 18px 20px;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 16px;
color: #f6fbff;
background: rgba(18, 34, 43, 0.78);
box-shadow: 0 12px 35px rgba(20, 40, 50, 0.2);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
user-select: none;
}
.eyebrow {
margin: 0 0 5px;
color: #94dac7;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 24px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 15px;
color: rgba(236, 247, 251, 0.72);
font-size: 13px;
line-height: 1.45;
}
.legend {
display: flex;
gap: 16px;
margin-bottom: 15px;
color: rgba(255, 255, 255, 0.86);
font-size: 12px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.08);
}
.dot.red {
background: #e5573e;
}
.dot.blue {
background: #318eb8;
}
.controls {
display: flex;
gap: 8px;
}
button {
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 9px;
padding: 8px 12px;
color: #fff;
background: rgba(255, 255, 255, 0.09);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.17);
transform: translateY(-1px);
}
.hint {
position: fixed;
right: 20px;
bottom: 18px;
z-index: 10;
padding: 8px 11px;
border-radius: 9px;
color: rgba(255, 255, 255, 0.86);
background: rgba(18, 34, 43, 0.62);
font-size: 11px;
pointer-events: none;
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
@media (max-width: 600px) {
.panel {
top: 12px;
left: 12px;
padding: 14px 16px;
}
h1 {
font-size: 20px;
}
.subtitle {
margin-bottom: 11px;
}
.hint {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div class="panel">
<p class="eyebrow">Alpine Transit</p>
<h1>Summit Funicular</h1>
<p class="subtitle">
Two low-poly cabins climb and descend the same mountain line,
connected through the summit pulley.
</p>
<div class="legend">
<span><i class="dot red"></i> Ascending</span>
<span><i class="dot blue"></i> Descending</span>
</div>
<div class="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset camera</button>
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d9e8);
scene.fog = new THREE.Fog(0xb9d9e8, 27, 70);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({
antialias: 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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xd9f1ff,
0x65704b,
2.15
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cc, 3.1);
sun.position.set(-12, 22, 14);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x88bbd9, 0.7);
fillLight.position.set(14, 8, -16);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Small direct camera-control implementation
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 3.35, 0),
azimuth: 0.66,
polar: 1.12,
radius: 25.5
};
const defaultCamera = {
azimuth: cameraState.azimuth,
polar: cameraState.polar,
radius: cameraState.radius
};
function updateCamera() {
const { target, azimuth, polar, radius } = cameraState;
camera.position.set(
target.x + radius * Math.sin(polar) * Math.sin(azimuth),
target.y + radius * Math.cos(polar),
target.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(target);
}
updateCamera();
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.32,
1.48
);
updateCamera();
});
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 * Math.exp(event.deltaY * 0.001),
11,
48
);
updateCamera();
},
{ passive: false }
);
document.getElementById("resetButton").addEventListener("click", () => {
cameraState.azimuth = defaultCamera.azimuth;
cameraState.polar = defaultCamera.polar;
cameraState.radius = defaultCamera.radius;
updateCamera();
});
// -------------------------------------------------------------------------
// Reusable geometry helpers
// -------------------------------------------------------------------------
const unitCylinder = new THREE.CylinderGeometry(1, 1, 1, 8);
const up = new THREE.Vector3(0, 1, 0);
const temporaryDirection = new THREE.Vector3();
const temporaryMidpoint = new THREE.Vector3();
function makeBeam(start, end, radius, material, radialSegments = 8) {
const geometry =
radialSegments === 8
? unitCylinder
: new THREE.CylinderGeometry(1, 1, 1, radialSegments);
const mesh = new THREE.Mesh(geometry, material);
setBeam(mesh, start, end, radius);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function setBeam(mesh, start, end, radius) {
temporaryDirection.subVectors(end, start);
const length = temporaryDirection.length();
temporaryMidpoint.addVectors(start, end).multiplyScalar(0.5);
mesh.position.copy(temporaryMidpoint);
mesh.quaternion.setFromUnitVectors(
up,
temporaryDirection.normalize()
);
mesh.scale.set(radius, length, radius);
}
function makeBox(width, height, depth, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeCone(radius, height, sides, material) {
const mesh = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, sides),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0x78945f,
roughness: 1,
flatShading: true
}),
mountain: new THREE.MeshStandardMaterial({
color: 0x76858a,
roughness: 1,
flatShading: true
}),
snow: new THREE.MeshStandardMaterial({
color: 0xe9f2ef,
roughness: 0.95,
flatShading: true
}),
rock: new THREE.MeshStandardMaterial({
color: 0x68716e,
roughness: 1,
flatShading: true
}),
rail: new THREE.MeshStandardMaterial({
color: 0x5c6262,
metalness: 0.72,
roughness: 0.4
}),
sleeper: new THREE.MeshStandardMaterial({
color: 0x655044,
roughness: 0.95
}),
support: new THREE.MeshStandardMaterial({
color: 0x58615f,
metalness: 0.35,
roughness: 0.75
}),
cable: new THREE.MeshStandardMaterial({
color: 0x202b30,
metalness: 0.65,
roughness: 0.32
}),
pulley: new THREE.MeshStandardMaterial({
color: 0xc99b47,
metalness: 0.65,
roughness: 0.34
}),
pulleyDark: new THREE.MeshStandardMaterial({
color: 0x343c3d,
metalness: 0.75,
roughness: 0.38
}),
red: new THREE.MeshStandardMaterial({
color: 0xd84b35,
roughness: 0.72
}),
blue: new THREE.MeshStandardMaterial({
color: 0x267fa6,
roughness: 0.72
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf0d9a5,
roughness: 0.72
}),
roof: new THREE.MeshStandardMaterial({
color: 0x38484a,
roughness: 0.68
}),
window: new THREE.MeshStandardMaterial({
color: 0x244a5c,
metalness: 0.35,
roughness: 0.24,
emissive: 0x0b2633,
emissiveIntensity: 0.18
}),
brass: new THREE.MeshStandardMaterial({
color: 0xd4a84e,
metalness: 0.7,
roughness: 0.3
}),
darkWood: new THREE.MeshStandardMaterial({
color: 0x4e392e,
roughness: 1
}),
station: new THREE.MeshStandardMaterial({
color: 0x8d624d,
roughness: 0.9
}),
stationRoof: new THREE.MeshStandardMaterial({
color: 0x3a4d50,
roughness: 0.8
}),
trunk: new THREE.MeshStandardMaterial({
color: 0x654736,
roughness: 1
}),
pine: new THREE.MeshStandardMaterial({
color: 0x355e4d,
roughness: 1,
flatShading: true
}),
pineLight: new THREE.MeshStandardMaterial({
color: 0x4b7859,
roughness: 1,
flatShading: true
})
};
// -------------------------------------------------------------------------
// Mountain landscape
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CircleGeometry(65, 48),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.05;
ground.receiveShadow = true;
scene.add(ground);
function addMountain(x, z, radius, height, colorMaterial) {
const mountain = makeCone(
radius,
height,
7,
colorMaterial || materials.mountain
);
mountain.position.set(x, height * 0.5 - 0.2, z);
mountain.rotation.y = x * 0.13;
scene.add(mountain);
const cap = makeCone(
radius * 0.39,
height * 0.39,
7,
materials.snow
);
cap.position.set(x, height * 0.805 - 0.2, z);
cap.rotation.y = mountain.rotation.y;
scene.add(cap);
}
addMountain(-22, -24, 13, 18);
addMountain(-6, -31, 16, 25);
addMountain(13, -27, 14, 21);
addMountain(27, -25, 12, 17, 0x819095);
const sunDisc = new THREE.Mesh(
new THREE.SphereGeometry(2.2, 12, 8),
new THREE.MeshBasicMaterial({ color: 0xffe6a2 })
);
sunDisc.position.set(-17, 18, -38);
scene.add(sunDisc);
function addCloud(x, y, z, scale) {
const group = new THREE.Group();
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xf4f7f2,
roughness: 1,
flatShading: true
});
const pieces = [
[-1.1, 0, 0, 1.15],
[0, 0.25, 0, 1.35],
[1.15, 0, 0, 0.95],
[0.35, -0.12, 0.35, 0.9]
];
for (const [px, py, pz, size] of pieces) {
const piece = new THREE.Mesh(
new THREE.IcosahedronGeometry(size, 1),
cloudMaterial
);
piece.position.set(px, py, pz);
group.add(piece);
}
group.position.set(x, y, z);
group.scale.setScalar(scale);
scene.add(group);
}
addCloud(-15, 13, -28, 1.15);
addCloud(10, 15, -34, 1.4);
addCloud(25, 11, -25, 0.85);
// Deterministic pseudo-random scenery.
let randomSeed = 1937;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.16, 1.2, 6),
materials.trunk
);
trunk.position.y = 0.6;
trunk.castShadow = true;
tree.add(trunk);
const foliageMaterial =
random() > 0.5 ? materials.pine : materials.pineLight;
const lower = makeCone(0.8, 1.65, 6, foliageMaterial);
lower.position.y = 1.55;
tree.add(lower);
const upper = makeCone(0.58, 1.35, 6, foliageMaterial);
upper.position.y = 2.35;
tree.add(upper);
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
tree.rotation.y = random() * Math.PI;
scene.add(tree);
}
for (let i = 0; i < 38; i++) {
const x = -25 + random() * 50;
const z = -14 + random() * 38;
// Keep the central railway corridor clear.
if (Math.abs(z) < 4.2) continue;
if (x > -11 && x < 10 && Math.abs(z) < 8) continue;
addTree(x, z, 0.65 + random() * 0.75);
}
for (let i = 0; i < 20; i++) {
const rock = makeCone(
0.25 + random() * 0.55,
0.3 + random() * 0.65,
5,
materials.rock
);
rock.position.set(
-19 + random() * 38,
0.08,
-11 + random() * 28
);
rock.rotation.y = random() * Math.PI;
scene.add(rock);
}
// -------------------------------------------------------------------------
// Inclined railway
// -------------------------------------------------------------------------
const bottomPoint = new THREE.Vector3(-8.7, 0.65, 0);
const topPoint = new THREE.Vector3(8.1, 7.25, 0);
const trackVector = new THREE.Vector3().subVectors(
topPoint,
bottomPoint
);
const trackLength = trackVector.length();
const trackDirection = trackVector.clone().normalize();
const trackNormal = new THREE.Vector3(
-trackDirection.y,
trackDirection.x,
0
).normalize();
const trackAngle = Math.atan2(trackDirection.y, trackDirection.x);
const trackCenters = [-1.22, 1.22];
const railOffsets = [-0.43, 0.43];
function pointOnTrack(t, z = 0) {
return new THREE.Vector3().lerpVectors(
bottomPoint,
topPoint,
t
).add(new THREE.Vector3(0, 0, z));
}
for (const z of trackCenters) {
// Main rails.
for (const offset of railOffsets) {
const rail = makeBeam(
pointOnTrack(0, z + offset),
pointOnTrack(1, z + offset),
0.075,
materials.rail
);
scene.add(rail);
}
// Sleepers.
const sleeperCount = 18;
for (let i = 0; i <= sleeperCount; i++) {
const t = i / sleeperCount;
const sleeper = makeBox(
0.19,
0.13,
1.3,
materials.sleeper
);
sleeper.position.copy(pointOnTrack(t, z));
sleeper.rotation.z = trackAngle;
scene.add(sleeper);
}
// Timber and steel trestle supports.
for (const t of [0.12, 0.35, 0.58, 0.79, 0.94]) {
const anchor = pointOnTrack(t, z);
const top = anchor.clone().addScaledVector(trackNormal, -0.13);
for (const side of [-0.43, 0.43]) {
const foot = top.clone();
foot.z += side;
foot.y = 0;
const leg = makeBeam(
foot,
top,
0.095,
materials.support
);
scene.add(leg);
}
const crossBeam = makeBox(
0.18,
0.15,
1.15,
materials.support
);
crossBeam.position.copy(top);
crossBeam.rotation.z = trackAngle;
scene.add(crossBeam);
if (t > 0.15 && t < 0.9) {
const braceStart = top.clone();
braceStart.y = 0.2;
braceStart.z -= 0.42;
const braceEnd = top.clone();
braceEnd.addScaledVector(trackDirection, 0.9);
braceEnd.z += 0.42;
braceEnd.addScaledVector(trackNormal, -0.08);
scene.add(
makeBeam(braceStart, braceEnd, 0.045, materials.support)
);
}
}
}
// -------------------------------------------------------------------------
// Stations
// -------------------------------------------------------------------------
function addStation(position, angle, isTop) {
const group = new THREE.Group();
group.position.copy(position);
group.rotation.z = angle;
const platform = makeBox(2.7, 0.24, 4.7, materials.sleeper);
platform.position.y = -0.22;
group.add(platform);
const building = makeBox(2.4, 1.7, 2.3, materials.station);
building.position.set(0, 0.72, isTop ? -2.0 : 2.0);
group.add(building);
const roof = makeCone(1.85, 0.9, 4, materials.stationRoof);
roof.position.set(0, 1.88, isTop ? -2.0 : 2.0);
roof.rotation.y = Math.PI * 0.25;
group.add(roof);
const door = makeBox(0.75, 1.2, 0.05, materials.darkWood);
door.position.set(0, 0.46, isTop ? -0.83 : 0.83);
group.add(door);
const window = makeBox(0.65, 0.52, 0.05, materials.window);
window.position.set(0.67, 0.94, isTop ? -0.82 : 0.82);
group.add(window);
scene.add(group);
}
addStation(
bottomPoint.clone().add(new THREE.Vector3(-1.1, -0.06, 0)),
trackAngle,
false
);
addStation(
topPoint.clone().add(new THREE.Vector3(0.75, 0.08, 0)),
trackAngle,
true
);
// -------------------------------------------------------------------------
// Summit pulley
// -------------------------------------------------------------------------
const topWheelCenter = topPoint.clone().add(
new THREE.Vector3(-0.05, 1.05, 0)
);
const rotatingWheel = new THREE.Group();
rotatingWheel.position.copy(topWheelCenter);
scene.add(rotatingWheel);
const wheelRing = new THREE.Mesh(
new THREE.TorusGeometry(0.64, 0.105, 8, 28),
materials.pulley
);
wheelRing.castShadow = true;
rotatingWheel.add(wheelRing);
const cableAroundWheel = new THREE.Mesh(
new THREE.TorusGeometry(0.75, 0.035, 6, 36),
materials.cable
);
rotatingWheel.add(cableAroundWheel);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const start = new THREE.Vector3(0, 0, 0);
const end = new THREE.Vector3(
Math.cos(angle) * 0.58,
Math.sin(angle) * 0.58,
0
);
const spoke = makeBeam(
start,
end,
0.045,
materials.pulleyDark
);
rotatingWheel.add(spoke);
}
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 0.35, 12),
materials.pulleyDark
);
hub.rotation.x = Math.PI / 2;
hub.castShadow = true;
rotatingWheel.add(hub);
// Pulley support frame.
for (const z of [-0.86, 0.86]) {
const foot = topPoint.clone();
foot.z += z;
const top = topWheelCenter.clone();
top.z += z;
scene.add(makeBeam(foot, top, 0.09, materials.support));
}
const axle = makeBeam(
topWheelCenter.clone().add(new THREE.Vector3(0, 0, -1.02)),
topWheelCenter.clone().add(new THREE.Vector3(0, 0, 1.02)),
0.085,
materials.pulleyDark
);
scene.add(axle);
// Lower return wheel.
const bottomWheelCenter = bottomPoint.clone().add(
new THREE.Vector3(0, 0.72, 0)
);
const bottomWheel = new THREE.Group();
bottomWheel.position.copy(bottomWheelCenter);
scene.add(bottomWheel);
const lowerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.085, 8, 24),
materials.pulleyDark
);
bottomWheel.add(lowerRing);
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
bottomWheel.add(
makeBeam(
new THREE.Vector3(),
new THREE.Vector3(
Math.cos(angle) * 0.39,
Math.sin(angle) * 0.39,
0
),
0.035,
materials.pulley
)
);
}
// -------------------------------------------------------------------------
// Funicular cabins
// -------------------------------------------------------------------------
function createCabin(bodyMaterial, accentMaterial) {
const cabin = new THREE.Group();
// Chassis.
const chassis = makeBox(1.65, 0.17, 1.12, materials.pulleyDark);
chassis.position.y = -0.03;
cabin.add(chassis);
// Bogie wheels.
const wheelGeometry = new THREE.CylinderGeometry(
0.21,
0.21,
0.13,
10
);
for (const x of [-0.53, 0.53]) {
for (const z of [-0.44, 0.44]) {
const wheel = new THREE.Mesh(
wheelGeometry,
materials.pulleyDark
);
wheel.rotation.x = Math.PI / 2;
wheel.position.set(x, -0.19, z);
wheel.castShadow = true;
cabin.add(wheel);
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.15, 8),
materials.brass
);
hub.rotation.x = Math.PI / 2;
hub.position.copy(wheel.position);
cabin.add(hub);
}
}
// Lower body.
const body = makeBox(1.48, 0.68, 1.06, bodyMaterial);
body.position.y = 0.42;
cabin.add(body);
// Upper window band.
const windowBand = makeBox(1.42, 0.61, 1.01, materials.window);
windowBand.position.y = 1.02;
cabin.add(windowBand);
// Roof and eaves.
const roof = makeBox(1.66, 0.17, 1.18, materials.roof);
roof.position.y = 1.39;
cabin.add(roof);
const roofCap = makeBox(1.30, 0.10, 0.89, accentMaterial);
roofCap.position.y = 1.51;
cabin.add(roofCap);
// Corner posts.
for (const x of [-0.68, 0.68]) {
for (const z of [-0.48, 0.48]) {
const post = makeBox(0.085, 0.92, 0.085, accentMaterial);
post.position.set(x, 0.97, z);
cabin.add(post);
}
}
// Side window divisions.
for (const z of [-0.515, 0.515]) {
for (const x of [-0.22, 0.22]) {
const divider = makeBox(
0.055,
0.57,
0.035,
accentMaterial
);
divider.position.set(x, 1.02, z);
cabin.add(divider);
}
}
// Front and rear windows.
for (const x of [-0.72, 0.72]) {
const endFrame = makeBox(0.045, 0.61, 0.93, accentMaterial);
endFrame.position.set(x, 1.02, 0);
cabin.add(endFrame);
}
// Gold belt line and number plaque.
const stripe = makeBox(1.52, 0.09, 1.075, materials.brass);
stripe.position.y = 0.72;
cabin.add(stripe);
const plaque = makeBox(0.24, 0.22, 0.04, materials.brass);
plaque.position.set(0.42, 0.40, 0.55);
cabin.add(plaque);
// Cable attachment.
const attachment = new THREE.Mesh(
new THREE.CylinderGeometry(0.085, 0.085, 0.15, 8),
materials.brass
);
attachment.position.y = 1.57;
cabin.add(attachment);
return cabin;
}
const ascendingCabin = createCabin(materials.red, materials.cream);
const descendingCabin = createCabin(materials.blue, materials.cream);
scene.add(ascendingCabin, descendingCabin);
// Dynamic visible cable branches.
const ascendingCable = makeBeam(
bottomPoint,
topWheelCenter,
0.043,
materials.cable
);
const descendingCable = makeBeam(
topPoint,
topWheelCenter,
0.043,
materials.cable
);
scene.add(ascendingCable, descendingCable);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let running = true;
let elapsed = 0;
const cycleDuration = 20;
const clock = new THREE.Clock();
const pauseButton = document.getElementById("pauseButton");
pauseButton.addEventListener("click", () => {
running = !running;
pauseButton.textContent = running ? "Pause" : "Play";
});
function setCabinPosition(cabin, t, z) {
const position = pointOnTrack(t, z);
cabin.position.copy(position);
cabin.rotation.z = trackAngle;
cabin.updateMatrixWorld(true);
}
function cabinAttachment(cabin) {
return cabin.localToWorld(new THREE.Vector3(0, 1.62, 0));
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (running) elapsed += delta;
// Smooth back-and-forth motion: each cabin reaches an endpoint in turn.
const phase = (elapsed % cycleDuration) / cycleDuration;
const progress = 0.5 - 0.5 * Math.cos(phase * Math.PI * 2);
const ascendingProgress = progress;
const descendingProgress = 1 - progress;
setCabinPosition(
ascendingCabin,
ascendingProgress,
trackCenters[0]
);
setCabinPosition(
descendingCabin,
descendingProgress,
trackCenters[1]
);
setBeam(
ascendingCable,
cabinAttachment(ascendingCabin),
topWheelCenter,
0.043
);
setBeam(
descendingCable,
cabinAttachment(descendingCabin),
topWheelCenter,
0.043
);
rotatingWheel.rotation.z = elapsed * 0.58;
bottomWheel.rotation.z = -elapsed * 0.82;
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
</script>
</body>
</html>
```