Prompt specification
Build or customize the Three.js 3D scene "Historical engine valve and piston teaching model" 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: Historical engine valve and piston teaching model
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/historical-engine-valve-and-piston-teaching-model-x2538
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing steam engines and mechanical power transmission. Create a simplified referenced steam-engine cutaway with cylinder, valve paths, and crank. Use schematic pressure colors and avoid presenting operating instructions for a real engine. Low-poly.
Working Three.js Starter (historical-engine-valve-and-piston-teaching-model-x2538):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Steam Engine & Mechanical Transmission — Schematic Cutaway</title>
<style>
:root {
--bg: #08121d;
--panel: rgba(10, 22, 34, 0.88);
--line: rgba(163, 193, 214, 0.22);
--text: #e5eef5;
--muted: #8fa5b6;
--red: #ff5964;
--blue: #39a9ff;
--gold: #ffc857;
--copper: #bd704b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-card {
position: fixed;
top: 22px;
left: 22px;
width: min(430px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 14px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 31px);
font-weight: 760;
line-height: 1.05;
letter-spacing: -0.025em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
#legend {
position: fixed;
top: 22px;
right: 22px;
width: 238px;
padding: 16px;
border-radius: 14px;
pointer-events: none;
}
.legend-title {
margin-bottom: 12px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin: 9px 0;
color: #d9e5ed;
font-size: 12px;
}
.swatch {
width: 17px;
height: 8px;
flex: 0 0 auto;
border-radius: 3px;
box-shadow: 0 0 12px currentColor;
}
.swatch.red {
color: var(--red);
background: var(--red);
}
.swatch.blue {
color: var(--blue);
background: var(--blue);
}
.swatch.gold {
color: var(--gold);
background: var(--gold);
}
.swatch.iron {
color: #71869a;
background: #71869a;
}
.legend-note {
margin: 13px 0 0;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
#labels {
position: fixed;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
z-index: 5;
padding: 6px 8px;
transform: translate(-50%, calc(-100% - 16px));
border: 1px solid rgba(154, 188, 212, 0.24);
border-radius: 5px;
background: rgba(6, 15, 24, 0.79);
color: #d9e8f2;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.25);
}
.scene-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 16px;
background: linear-gradient(rgba(181, 208, 226, 0.72), transparent);
}
.scene-label::before {
content: "";
position: absolute;
bottom: -19px;
left: calc(50% - 3px);
width: 6px;
height: 6px;
border: 1px solid #91afc2;
border-radius: 50%;
background: #101d28;
}
.scene-label.red {
border-color: rgba(255, 89, 100, 0.45);
color: #ffb2b7;
}
.scene-label.blue {
border-color: rgba(57, 169, 255, 0.45);
color: #a9dcff;
}
.scene-label.gold {
border-color: rgba(255, 200, 87, 0.45);
color: #ffe1a2;
}
body.labels-hidden .scene-label {
display: none;
}
#controls {
position: fixed;
left: 50%;
bottom: 23px;
z-index: 20;
display: flex;
align-items: center;
gap: 6px;
padding: 7px;
transform: translateX(-50%);
border-radius: 13px;
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid transparent;
border-radius: 8px;
background: transparent;
color: #afc1cf;
font: inherit;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.03em;
cursor: pointer;
transition: color 150ms ease, background 150ms ease, border-color 150ms ease;
white-space: nowrap;
}
button:hover {
color: white;
background: rgba(122, 158, 184, 0.13);
}
button.active {
border-color: rgba(255, 200, 87, 0.32);
background: rgba(255, 200, 87, 0.1);
color: #ffe0a0;
}
button.primary {
min-width: 82px;
color: #09131d;
background: var(--gold);
}
button.primary:hover {
color: #09131d;
background: #ffd77e;
}
.divider {
width: 1px;
height: 23px;
margin: 0 2px;
background: var(--line);
}
#camera-help {
position: fixed;
left: 22px;
bottom: 24px;
z-index: 10;
color: #7f95a7;
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
#disclaimer {
position: fixed;
right: 22px;
bottom: 24px;
z-index: 10;
max-width: 280px;
color: #8196a7;
font-size: 10px;
line-height: 1.45;
text-align: right;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #08121d;
color: #91a8ba;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.15em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 850px) {
#legend {
top: auto;
right: 12px;
bottom: 82px;
width: 205px;
padding: 12px;
}
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
#controls {
bottom: 12px;
max-width: calc(100vw - 20px);
overflow-x: auto;
}
#camera-help,
#disclaimer {
display: none;
}
.legend-note {
display: none;
}
.scene-label {
font-size: 8px;
}
}
@media (max-width: 520px) {
#legend {
display: none;
}
.subtitle {
display: none;
}
button {
padding: 0 10px;
font-size: 10px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive schematic steam engine and mechanical transmission scene"></canvas>
<div id="labels"></div>
<header id="title-card" class="glass">
<div class="eyebrow">Mechanical Study · Plate 01</div>
<h1>Steam & Mechanical Power</h1>
<p class="subtitle">
A simplified low-poly cutaway showing cylinder pressure regions, valve paths,
a slider-crank, belt drive, reduction gears, and an auxiliary engine.
</p>
</header>
<aside id="legend" class="glass">
<div class="legend-title">Relative pressure key</div>
<div class="legend-row"><span class="swatch red"></span>High-pressure region</div>
<div class="legend-row"><span class="swatch blue"></span>Low-pressure / exhaust</div>
<div class="legend-row"><span class="swatch gold"></span>Mechanical motion</div>
<div class="legend-row"><span class="swatch iron"></span>Structural casing</div>
<p class="legend-note">
Colors are comparative schematic cues only. No dimensions, pressures,
temperatures, or operating sequence are represented.
</p>
</aside>
<nav id="controls" class="glass" aria-label="Scene controls">
<button id="play" class="primary" type="button">Pause</button>
<div class="divider"></div>
<button class="view active" data-view="overview" type="button">Overview</button>
<button class="view" data-view="cylinder" type="button">Cylinder</button>
<button class="view" data-view="transmission" type="button">Transmission</button>
<div class="divider"></div>
<button id="labels-toggle" class="active" type="button">Labels</button>
<button id="pressure-toggle" class="active" type="button">Pressure</button>
<button id="reset" type="button">Reset</button>
</nav>
<div id="camera-help">Drag to orbit · Wheel to zoom · Right-drag to pan</div>
<div id="disclaimer">
Conceptual cutaway for visual study only.<br />
Not an operating guide for a real steam engine.
</div>
<div id="loading">Building schematic assembly…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, scene, and direct camera controls (no addons)
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.2;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08121d);
scene.fog = new THREE.Fog(0x08121d, 13, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const controls = {
target: new THREE.Vector3(0.8, 1.55, -0.65),
desiredTarget: new THREE.Vector3(0.8, 1.55, -0.65),
radius: 13,
desiredRadius: 13,
theta: 0.06,
desiredTheta: 0.06,
phi: 1.16,
desiredPhi: 1.16
};
const pointer = {
down: false,
button: 0,
x: 0,
y: 0,
panning: false
};
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
pointer.down = true;
pointer.button = event.button;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.panning = event.button === 2 || event.shiftKey;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!pointer.down) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointer.panning) {
const scale = controls.radius * 0.00135;
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
controls.desiredTarget.addScaledVector(right, -dx * scale);
controls.desiredTarget.addScaledVector(up, dy * scale);
} else {
controls.desiredTheta -= dx * 0.0055;
controls.desiredPhi = THREE.MathUtils.clamp(
controls.desiredPhi - dy * 0.0045,
0.27,
1.48
);
}
});
function releasePointer(event) {
pointer.down = false;
pointer.panning = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
controls.desiredRadius = THREE.MathUtils.clamp(
controls.desiredRadius * Math.exp(event.deltaY * 0.001),
3.2,
21
);
},
{ passive: false }
);
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 7.5);
controls.radius = THREE.MathUtils.lerp(
controls.radius,
controls.desiredRadius,
smoothing
);
controls.theta = THREE.MathUtils.lerp(
controls.theta,
controls.desiredTheta,
smoothing
);
controls.phi = THREE.MathUtils.lerp(
controls.phi,
controls.desiredPhi,
smoothing
);
controls.target.lerp(controls.desiredTarget, smoothing);
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
// -------------------------------------------------------------------------
// Lighting and environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9ec6e4, 0x17202a, 1.7));
const keyLight = new THREE.DirectionalLight(0xffe4c2, 3.2);
keyLight.position.set(-4, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 28;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4b8fff, 2.1);
rimLight.position.set(7, 5, -7);
scene.add(rimLight);
const warmFill = new THREE.PointLight(0xff7c4f, 18, 10, 2);
warmFill.position.set(-4.5, 2.5, 2.5);
scene.add(warmFill);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0c1822,
roughness: 0.95,
metalness: 0.08
});
const floor = new THREE.Mesh(new THREE.PlaneGeometry(38, 24), floorMaterial);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.07;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(28, 28, 0x274252, 0x1b303e);
grid.position.y = -0.055;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const backWall = new THREE.Mesh(
new THREE.PlaneGeometry(30, 12),
new THREE.MeshStandardMaterial({
color: 0x0b1722,
roughness: 1,
metalness: 0
})
);
backWall.position.set(0, 5, -7.5);
backWall.receiveShadow = true;
scene.add(backWall);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const material = (color, metalness = 0.35, roughness = 0.58) =>
new THREE.MeshStandardMaterial({
color,
metalness,
roughness,
flatShading: true
});
const materials = {
iron: material(0x263746, 0.72, 0.47),
ironLight: material(0x405566, 0.68, 0.48),
steel: material(0xa5b5c0, 0.82, 0.3),
brass: material(0xc99a49, 0.78, 0.34),
copper: material(0x9f563a, 0.65, 0.48),
dark: material(0x101a23, 0.48, 0.72),
belt: material(0x17181b, 0.1, 0.88),
gold: material(0xffc857, 0.42, 0.36),
red: material(0xff5964, 0.22, 0.38),
blue: material(0x39a9ff, 0.2, 0.4)
};
materials.red.emissive.setHex(0x7b101a);
materials.blue.emissive.setHex(0x073d70);
function addMesh(parent, geometry, mat, position = [0, 0, 0]) {
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBox(parent, size, position, mat) {
return addMesh(parent, new THREE.BoxGeometry(...size), mat, position);
}
function addCylinder(parent, radius, height, position, mat, radialSegments = 12) {
return addMesh(
parent,
new THREE.CylinderGeometry(radius, radius, height, radialSegments),
mat,
position
);
}
function addTorus(parent, radius, tube, position, mat, rotation = [0, 0, 0]) {
const mesh = addMesh(
parent,
new THREE.TorusGeometry(radius, tube, 7, 24),
mat,
position
);
mesh.rotation.set(...rotation);
return mesh;
}
function addTube(parent, points, radius, mat, segments = 36) {
const curve = new THREE.CatmullRomCurve3(
points.map(point => new THREE.Vector3(...point)),
false,
"catmullrom",
0.28
);
const mesh = addMesh(
parent,
new THREE.TubeGeometry(curve, segments, radius, 7, false),
mat
);
return { mesh, curve };
}
const yAxis = new THREE.Vector3(0, 1, 0);
function orientCylinder(mesh, start, end) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.scale.set(1, length, 1);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
}
function makeDynamicRod(parent, radius, mat) {
const mesh = addMesh(
parent,
new THREE.CylinderGeometry(radius, radius, 1, 8),
mat
);
return mesh;
}
function addEdges(mesh, color = 0x8aa6b8, opacity = 0.4) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 24),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
mesh.add(edges);
return edges;
}
// -------------------------------------------------------------------------
// Main engine base and boiler
// -------------------------------------------------------------------------
const engine = new THREE.Group();
scene.add(engine);
const mainBase = addBox(
engine,
[8.2, 0.28, 2.25],
[-1.25, 0.09, -0.05],
materials.iron
);
addEdges(mainBase);
const baseTrim = addBox(
engine,
[8.32, 0.08, 2.34],
[-1.25, 0.27, -0.05],
materials.brass
);
addEdges(baseTrim, 0xd4ad67, 0.28);
for (const x of [-4.65, -2.7, -0.8, 1.05, 2.15]) {
addBox(engine, [0.36, 0.42, 1.85], [x, 0.42, -0.05], materials.dark);
addCylinder(engine, 0.09, 2.05, [x, 0.68, -0.05], materials.ironLight, 8);
}
const boiler = new THREE.Group();
boiler.position.set(-4.55, 0.25, -0.82);
engine.add(boiler);
addCylinder(boiler, 0.72, 2.65, [0, 1.5, 0], materials.copper, 14);
addCylinder(boiler, 0.76, 0.13, [0, 0.12, 0], materials.ironLight, 14);
addCylinder(boiler, 0.76, 0.13, [0, 2.88, 0], materials.ironLight, 14);
addTorus(boiler, 0.735, 0.065, [0, 0.65, 0], materials.brass, [Math.PI / 2, 0, 0]);
addTorus(boiler, 0.735, 0.065, [0, 2.32, 0], materials.brass, [Math.PI / 2, 0, 0]);
addCylinder(boiler, 0.24, 0.48, [0, 3.15, 0], materials.ironLight, 10);
addCylinder(boiler, 0.3, 0.12, [0, 3.4, 0], materials.brass, 10);
const boilerGauge = addCylinder(
boiler,
0.21,
0.09,
[0, 2.02, 0.71],
materials.brass,
12
);
boilerGauge.rotation.x = Math.PI / 2;
const gaugeFace = addCylinder(
boiler,
0.16,
0.015,
[0, 2.02, 0.765],
new THREE.MeshStandardMaterial({
color: 0xd8e1dc,
roughness: 0.7,
metalness: 0.05
}),
12
);
gaugeFace.rotation.x = Math.PI / 2;
const gaugeNeedle = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 2.02, 0.78),
new THREE.Vector3(-0.08, 2.11, 0.78)
]),
new THREE.LineBasicMaterial({ color: 0xff5964 })
);
boiler.add(gaugeNeedle);
const fireDoor = addBox(
boiler,
[0.5, 0.46, 0.08],
[0, 0.55, 0.72],
materials.dark
);
addEdges(fireDoor, 0xc8894e, 0.45);
// -------------------------------------------------------------------------
// Main cylinder: cutaway shell, piston, pressure volumes, and piston rod
// -------------------------------------------------------------------------
const cylinderCenterX = -2.2;
const cylinderY = 2.15;
const cylinderLeft = -3.35;
const cylinderRight = -1.05;
const cylinderShell = addMesh(
engine,
new THREE.CylinderGeometry(
0.72,
0.72,
2.3,
16,
1,
true,
Math.PI,
Math.PI
),
new THREE.MeshStandardMaterial({
color: 0x435767,
metalness: 0.7,
roughness: 0.45,
flatShading: true,
side: THREE.DoubleSide
}),
[cylinderCenterX, cylinderY, 0]
);
cylinderShell.rotation.z = Math.PI / 2;
addTorus(
engine,
0.74,
0.1,
[cylinderLeft, cylinderY, 0],
materials.ironLight,
[0, Math.PI / 2, 0]
);
addTorus(
engine,
0.74,
0.1,
[cylinderRight, cylinderY, 0],
materials.ironLight,
[0, Math.PI / 2, 0]
);
const leftCap = addCylinder(
engine,
0.72,
0.14,
[cylinderLeft, cylinderY, 0],
materials.iron,
16
);
leftCap.rotation.z = Math.PI / 2;
// Cutaway longitudinal lips.
addBox(engine, [2.35, 0.1, 0.11], [cylinderCenterX, cylinderY, 0.7], materials.brass);
addBox(engine, [2.35, 0.1, 0.11], [cylinderCenterX, cylinderY, -0.7], materials.ironLight);
const chamberGeometry = new THREE.CylinderGeometry(0.57, 0.57, 1, 14);
const leftPressureMaterial = new THREE.MeshStandardMaterial({
color: 0xff5964,
emissive: 0x7b101a,
emissiveIntensity: 0.8,
transparent: true,
opacity: 0.3,
depthWrite: false,
roughness: 0.8,
flatShading: true
});
const rightPressureMaterial = leftPressureMaterial.clone();
const leftPressure = addMesh(
engine,
chamberGeometry,
leftPressureMaterial,
[cylinderLeft + 0.5, cylinderY, 0]
);
leftPressure.rotation.z = Math.PI / 2;
leftPressure.castShadow = false;
const rightPressure = addMesh(
engine,
chamberGeometry,
rightPressureMaterial,
[cylinderRight - 0.5, cylinderY, 0]
);
rightPressure.rotation.z = Math.PI / 2;
rightPressure.castShadow = false;
const piston = addCylinder(
engine,
0.635,
0.22,
[-2.2, cylinderY, 0],
materials.steel,
16
);
piston.rotation.z = Math.PI / 2;
const pistonBandA = addTorus(
engine,
0.637,
0.035,
[-2.2, cylinderY, 0],
materials.brass,
[0, Math.PI / 2, 0]
);
const pistonBandB = addTorus(
engine,
0.637,
0.035,
[-2.2, cylinderY, 0],
materials.brass,
[0, Math.PI / 2, 0]
);
const pistonRod = makeDynamicRod(engine, 0.085, materials.steel);
// Crosshead guide rails.
addBox(engine, [1.85, 0.09, 0.12], [-0.55, 2.33, 0.2], materials.ironLight);
addBox(engine, [1.85, 0.09, 0.12], [-0.55, 2.33, -0.2], materials.ironLight);
addBox(engine, [1.85, 0.09, 0.15], [-0.55, 1.86, 0.22], materials.iron);
addBox(engine, [1.85, 0.09, 0.15], [-0.55, 1.86, -0.22], materials.iron);
const crosshead = addBox(
engine,
[0.36, 0.42, 0.48],
[-0.5, 2.1, 0],
materials.brass
);
addEdges(crosshead);
// -------------------------------------------------------------------------
// Valve chest and schematic pressure paths
// -------------------------------------------------------------------------
const valveChest = addBox(
engine,
[1.12, 0.52, 0.72],
[-2.15, 3.25, 0],
new THREE.MeshStandardMaterial({
color: 0x647b8b,
metalness: 0.65,
roughness: 0.4,
transparent: true,
opacity: 0.38,
flatShading: true
})
);
addEdges(valveChest, 0xb5cbd9, 0.75);
const slideValve = addBox(
engine,
[0.62, 0.17, 0.48],
[-2.15, 3.25, 0.02],
materials.brass
);
addEdges(slideValve, 0xffd47a, 0.65);
const leftPort = addTube(
engine,
[
[-2.65, 3.02, 0],
[-2.65, 2.84, 0],
[-2.65, 2.67, 0]
],
0.11,
materials.red,
12
);
const rightPort = addTube(
engine,
[
[-1.68, 3.02, 0],
[-1.68, 2.84, 0],
[-1.68, 2.67, 0]
],
0.11,
materials.blue,
12
);
const steamPath = addTube(
engine,
[
[-4.55, 3.62, -0.82],
[-4.25, 3.92, -0.55],
[-3.65, 3.86, -0.18],
[-2.95, 3.75, 0.05],
[-2.55, 3.5, 0.02]
],
0.13,
materials.red,
48
);
const exhaustPath = addTube(
engine,
[
[-1.77, 3.42, 0],
[-1.38, 3.32, -0.4],
[-0.85, 2.9, -1.08],
[-0.45, 2.15, -1.3],
[-0.15, 1.2, -1.35]
],
0.115,
materials.blue,
42
);
const exhaustHousing = addCylinder(
engine,
0.27,
0.62,
[-0.15, 0.85, -1.35],
materials.iron,
10
);
// Moving schematic flow markers.
const flowGeometry = new THREE.SphereGeometry(0.075, 7, 5);
const steamFlow = [];
const exhaustFlow = [];
for (let i = 0; i < 6; i++) {
const steamDot = new THREE.Mesh(flowGeometry, materials.red);
engine.add(steamDot);
steamFlow.push(steamDot);
const exhaustDot = new THREE.Mesh(flowGeometry, materials.blue);
engine.add(exhaustDot);
exhaustFlow.push(exhaustDot);
}
// -------------------------------------------------------------------------
// Slider-crank and flywheel
// -------------------------------------------------------------------------
const flywheelCenter = new THREE.Vector3(1.45, 1.25, 0);
const flywheelRadius = 1.07;
const crankRadius = 0.52;
const connectingRodLength = 2.25;
const crossheadY = 2.1;
const flywheel = new THREE.Group();
flywheel.position.copy(flywheelCenter);
engine.add(flywheel);
addTorus(flywheel, flywheelRadius, 0.16, [0, 0, 0], materials.ironLight);
addTorus(flywheel, 0.89, 0.035, [0, 0, 0], materials.brass);
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const spoke = addBox(
flywheel,
[0.105, 0.96, 0.13],
[
Math.cos(angle) * 0.47,
Math.sin(angle) * 0.47,
0
],
materials.ironLight
);
spoke.rotation.z = angle - Math.PI / 2;
}
const flywheelHub = addCylinder(
flywheel,
0.26,
0.35,
[0, 0, 0],
materials.brass,
12
);
flywheelHub.rotation.x = Math.PI / 2;
const crankPin = addCylinder(
flywheel,
0.12,
0.38,
[crankRadius, 0, 0.1],
materials.gold,
10
);
crankPin.rotation.x = Math.PI / 2;
const connectingRod = makeDynamicRod(engine, 0.105, materials.brass);
const crankCounterweight = addCylinder(
flywheel,
0.37,
0.16,
[-0.25, 0, -0.12],
materials.copper,
10
);
crankCounterweight.rotation.x = Math.PI / 2;
addBox(engine, [0.55, 1.0, 0.65], [1.45, 0.78, 0], materials.iron);
// -------------------------------------------------------------------------
// Belt drive and reduction gears
// -------------------------------------------------------------------------
const secondaryCenter = new THREE.Vector3(4.55, 1.25, 0);
const secondaryRadius = 0.49;
const beltBigRadius = 1.08;
const beltSmallRadius = 0.5;
const beltDistance = secondaryCenter.x - flywheelCenter.x;
const tangentAngle = Math.acos((beltBigRadius - beltSmallRadius) / beltDistance);
const beltPoints = [];
beltPoints.push(
new THREE.Vector3(
flywheelCenter.x + Math.cos(tangentAngle) * beltBigRadius,
flywheelCenter.y + Math.sin(tangentAngle) * beltBigRadius,
0
)
);
beltPoints.push(
new THREE.Vector3(
secondaryCenter.x + Math.cos(tangentAngle) * beltSmallRadius,
secondaryCenter.y + Math.sin(tangentAngle) * beltSmallRadius,
0
)
);
for (let i = 1; i <= 9; i++) {
const angle = tangentAngle - (2 * tangentAngle * i) / 9;
beltPoints.push(
new THREE.Vector3(
secondaryCenter.x + Math.cos(angle) * beltSmallRadius,
secondaryCenter.y + Math.sin(angle) * beltSmallRadius,
0
)
);
}
beltPoints.push(
new THREE.Vector3(
flywheelCenter.x + Math.cos(-tangentAngle) * beltBigRadius,
flywheelCenter.y + Math.sin(-tangentAngle) * beltBigRadius,
0
)
);
for (let i = 1; i <= 13; i++) {
const angle = -tangentAngle - ((Math.PI * 2 - 2 * tangentAngle) * i) / 13;
beltPoints.push(
new THREE.Vector3(
flywheelCenter.x + Math.cos(angle) * beltBigRadius,
flywheelCenter.y + Math.sin(angle) * beltBigRadius,
0
)
);
}
const beltCurve = new THREE.CatmullRomCurve3(beltPoints, true, "catmullrom", 0.15);
const beltMesh = addMesh(
engine,
new THREE.TubeGeometry(beltCurve, 110, 0.058, 6, true),
materials.belt
);
beltMesh.castShadow = false;
const beltMarkers = [];
const markerGeometry = new THREE.SphereGeometry(0.052, 7, 5);
for (let i = 0; i < 15; i++) {
const marker = new THREE.Mesh(markerGeometry, materials.gold);
engine.add(marker);
beltMarkers.push(marker);
}
const secondaryPulley = new THREE.Group();
secondaryPulley.position.copy(secondaryCenter);
engine.add(secondaryPulley);
addTorus(secondaryPulley, secondaryRadius, 0.12, [0, 0, 0], materials.ironLight);
addTorus(secondaryPulley, 0.36, 0.035, [0, 0, 0], materials.brass);
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2;
const spoke = addBox(
secondaryPulley,
[0.075, 0.42, 0.09],
[Math.cos(angle) * 0.2, Math.sin(angle) * 0.2, 0],
materials.steel
);
spoke.rotation.z = angle - Math.PI / 2;
}
const secondaryHub = addCylinder(
secondaryPulley,
0.15,
0.31,
[0, 0, 0],
materials.brass,
10
);
secondaryHub.rotation.x = Math.PI / 2;
function createGear(radius, toothCount, mat, position) {
const group = new THREE.Group();
group.position.set(...position);
engine.add(group);
const disk = addCylinder(group, radius, 0.19, [0, 0, 0], mat, 18);
disk.rotation.x = Math.PI / 2;
const hub = addCylinder(group, radius * 0.3, 0.28, [0, 0, 0], materials.brass, 10);
hub.rotation.x = Math.PI / 2;
for (let i = 0; i < toothCount; i++) {
const angle = (i / toothCount) * Math.PI * 2;
const tooth = addBox(
group,
[0.14, 0.19, 0.2],
[
Math.cos(angle) * (radius + 0.045),
Math.sin(angle) * (radius + 0.045),
0
],
mat
);
tooth.rotation.z = angle;
}
for (let i = 0; i < 5; i++) {
const angle = (i / 5) * Math.PI * 2;
const hole = addCylinder(
group,
radius * 0.095,
0.205,
[
Math.cos(angle) * radius * 0.56,
Math.sin(angle) * radius * 0.56,
0
],
materials.dark,
8
);
hole.rotation.x = Math.PI / 2;
}
return group;
}
const gearOne = createGear(0.49, 12, materials.copper, [4.55, 1.25, 0.76]);
const gearTwo = createGear(0.68, 16, materials.ironLight, [5.69, 1.25, 0.76]);
const pulleyShaft = addCylinder(
engine,
0.1,
0.95,
[4.55, 1.25, 0.39],
materials.steel,
10
);
pulleyShaft.rotation.x = Math.PI / 2;
// Working load behind the gear train.
const loadBase = addBox(
engine,
[1.45, 0.22, 1.35],
[6.48, 0.08, -0.05],
materials.iron
);
addEdges(loadBase);
const loadBody = addBox(
engine,
[0.88, 1.12, 1.02],
[6.48, 0.72, -0.05],
materials.copper
);
addEdges(loadBody);
addCylinder(engine, 0.37, 0.65, [6.48, 1.55, -0.05], materials.ironLight, 12);
addCylinder(engine, 0.22, 0.24, [6.48, 1.94, -0.05], materials.brass, 10);
const loadShaft = addCylinder(
engine,
0.095,
0.78,
[5.69, 1.25, 0.39],
materials.steel,
10
);
loadShaft.rotation.x = Math.PI / 2;
// -------------------------------------------------------------------------
// Compact auxiliary steam engine in the background
// -------------------------------------------------------------------------
const auxiliary = new THREE.Group();
auxiliary.position.set(6.65, 0, -2.65);
scene.add(auxiliary);
addBox(auxiliary, [2.5, 0.22, 1.25], [0.45, 0.08, 0], materials.iron);
addCylinder(auxiliary, 0.4, 1.6, [-0.25, 1.02, 0], materials.copper, 12);
addTorus(auxiliary, 0.41, 0.055, [-0.25, 0.55, 0], materials.brass, [Math.PI / 2, 0, 0]);
addTorus(auxiliary, 0.41, 0.055, [-0.25, 1.5, 0], materials.brass, [Math.PI / 2, 0, 0]);
addCylinder(auxiliary, 0.18, 0.32, [-0.25, 1.94, 0], materials.ironLight, 10);
const auxCylinderShell = addMesh(
auxiliary,
new THREE.CylinderGeometry(
0.3,
0.3,
0.95,
12,
1,
true,
Math.PI,
Math.PI
),
materials.ironLight,
[0.58, 1.6, 0]
);
const auxPiston = addCylinder(
auxiliary,
0.255,
0.12,
[0.58, 1.6, 0],
materials.steel,
12
);
const auxFlywheel = new THREE.Group();
auxFlywheel.position.set(1.42, 0.92, 0);
auxiliary.add(auxFlywheel);
addTorus(auxFlywheel, 0.57, 0.1, [0, 0, 0], materials.ironLight);
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2;
const spoke = addBox(
auxFlywheel,
[0.065, 0.54, 0.08],
[Math.cos(angle) * 0.25, Math.sin(angle) * 0.25, 0],
materials.brass
);
spoke.rotation.z = angle - Math.PI / 2;
}
const auxHub = addCylinder(
auxFlywheel,
0.13,
0.25,
[0, 0, 0],
materials.copper,
10
);
auxHub.rotation.x = Math.PI / 2;
addTube(
auxiliary,
[
[-0.25, 2.08, 0],
[0.03, 2.15, 0],
[0.4, 1.98, 0],
[0.58, 1.95, 0]
],
0.075,
materials.red,
22
);
// -------------------------------------------------------------------------
// Labels projected manually into the HTML layer
// -------------------------------------------------------------------------
const labelLayer = document.querySelector("#labels");
const labels = [];
function addLabel(text, anchor, className = "") {
const element = document.createElement("div");
element.className = `scene-label ${className}`.trim();
element.textContent = text;
labelLayer.appendChild(element);
const item = { element, anchor: anchor.clone() };
labels.push(item);
return item;
}
addLabel("Boiler", new THREE.Vector3(-4.55, 3.55, -0.82));
addLabel("Cutaway cylinder", new THREE.Vector3(-2.45, 2.95, 0));
addLabel("Valve chest", new THREE.Vector3(-2.15, 3.72, 0), "red");
addLabel("Pressure paths", new THREE.Vector3(-3.6, 4.02, -0.2), "blue");
const pistonLabel = addLabel("Piston", new THREE.Vector3(-2.25, 2.83, 0), "gold");
addLabel("Crosshead", new THREE.Vector3(-0.65, 2.55, 0));
addLabel("Connecting rod", new THREE.Vector3(0.45, 1.72, 0.05), "gold");
addLabel("Crank & flywheel", new THREE.Vector3(1.45, 2.52, 0), "gold");
addLabel("Belt reduction", new THREE.Vector3(3.0, 1.5, 0.02), "gold");
addLabel("Gear train", new THREE.Vector3(5.2, 2.08, 0.76), "gold");
addLabel("Working load", new THREE.Vector3(6.48, 2.13, -0.05));
addLabel("Auxiliary steam engine", new THREE.Vector3(6.65, 2.55, -2.65));
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const projected = label.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.08 &&
projected.x < 1.08 &&
projected.y > -1.08 &&
projected.y < 1.08;
if (!visible) {
label.element.style.display = "none";
continue;
}
label.element.style.display = "";
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
let playing = true;
let pressureVisible = true;
const playButton = document.querySelector("#play");
const labelsButton = document.querySelector("#labels-toggle");
const pressureButton = document.querySelector("#pressure-toggle");
const viewButtons = [...document.querySelectorAll(".view")];
playButton.addEventListener("click", () => {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
});
labelsButton.addEventListener("click", () => {
document.body.classList.toggle("labels-hidden");
labelsButton.classList.toggle("active");
});
pressureButton.addEventListener("click", () => {
pressureVisible = !pressureVisible;
pressureButton.classList.toggle("active");
});
function setView(view) {
viewButtons.forEach(button => {
button.classList.toggle("active", button.dataset.view === view);
});
if (view === "cylinder") {
controls.desiredTarget.set(-2.1, 2.15, 0);
controls.desiredRadius = 5.4;
controls.desiredTheta = 0.08;
controls.desiredPhi = 1.13;
} else if (view === "transmission") {
controls.desiredTarget.set(5.15, 1.35, -0.25);
controls.desiredRadius = 6.0;
controls.desiredTheta = 0.04;
controls.desiredPhi = 1.15;
} else {
controls.desiredTarget.set(0.8, 1.55, -0.65);
controls.desiredRadius = 13;
controls.desiredTheta = 0.06;
controls.desiredPhi = 1.16;
}
}
viewButtons.forEach(button => {
button.addEventListener("click", () => setView(button.dataset.view));
});
document.querySelector("#reset").addEventListener("click", () => {
setView("overview");
controls.desiredTarget.set(0.8, 1.55, -0.65);
controls.desiredRadius = 13;
controls.desiredTheta = 0.06;
controls.desiredPhi = 1.16;
});
window.addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const highColor = new THREE.Color(0xff5964);
const lowColor = new THREE.Color(0x39a9ff);
const highEmissive = new THREE.Color(0x7b101a);
const lowEmissive = new THREE.Color(0x073d70);
const pistonWorld = new THREE.Vector3();
const crossheadWorld = new THREE.Vector3();
const crankWorld = new THREE.Vector3();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const phase = clock.elapsedTime * 1.02;
updateCamera(dt);
if (playing) {
flywheel.rotation.z = -phase;
secondaryPulley.rotation.z = -phase * (flywheelRadius / secondaryRadius);
gearOne.rotation.z = -phase * 1.55;
gearTwo.rotation.z = phase * 1.55 * (0.49 / 0.68);
auxFlywheel.rotation.z = -phase * 0.62;
const crankAngle = -phase;
crankWorld.set(
flywheelCenter.x + Math.cos(crankAngle) * crankRadius,
flywheelCenter.y + Math.sin(crankAngle) * crankRadius,
0.08
);
const verticalDifference = crankWorld.y - crossheadY;
const horizontalReach = Math.sqrt(
Math.max(0.001, connectingRodLength ** 2 - verticalDifference ** 2)
);
const crossheadX = crankWorld.x - horizontalReach;
crossheadWorld.set(crossheadX, crossheadY, 0);
const pistonX = crossheadX - 1.55;
pistonWorld.set(pistonX, cylinderY, 0);
piston.position.x = pistonX;
pistonBandA.position.x = pistonX - 0.065;
pistonBandB.position.x = pistonX + 0.065;
crosshead.position.x = crossheadX;
orientCylinder(pistonRod, pistonWorld, crossheadWorld);
orientCylinder(connectingRod, crossheadWorld, crankWorld);
slideValve.position.x = -2.15 + Math.sin(phase) * 0.23;
const leftLength = Math.max(0.08, pistonX - cylinderLeft);
const rightLength = Math.max(0.08, cylinderRight - pistonX);
leftPressure.scale.y = leftLength;
rightPressure.scale.y = rightLength;
leftPressure.position.x = cylinderLeft + leftLength * 0.5;
rightPressure.position.x = cylinderRight - rightLength * 0.5;
const leftIsHigh = Math.sin(phase) > 0;
leftPressureMaterial.color.copy(leftIsHigh ? highColor : lowColor);
leftPressureMaterial.emissive.copy(leftIsHigh ? highEmissive : lowEmissive);
rightPressureMaterial.color.copy(leftIsHigh ? lowColor : highColor);
rightPressureMaterial.emissive.copy(leftIsHigh ? lowEmissive : highEmissive);
leftPressure.visible = pressureVisible;
rightPressure.visible = pressureVisible;
pistonLabel.anchor.set(pistonX, 2.88, 0);
steamFlow.forEach((dot, index) => {
const t = (phase * 0.18 + index / steamFlow.length) % 1;
dot.position.copy(steamPath.curve.getPointAt(t));
});
exhaustFlow.forEach((dot, index) => {
const t = (phase * 0.23 + index / exhaustFlow.length) % 1;
dot.position.copy(exhaustPath.curve.getPointAt(t));
});
beltMarkers.forEach((marker, index) => {
const t = (phase * 0.12 + index / beltMarkers.length) % 1;
marker.position.copy(beltCurve.getPointAt(t));
});
auxPiston.position.y = 1.58 + Math.sin(phase * 0.62) * 0.12;
}
updateLabels();
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
// Set an initial composition and begin the loop.
controls.target.set(0.8, 1.55, -0.65);
controls.desiredTarget.copy(controls.target);
updateCamera(1);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
setTimeout(() => document.querySelector("#loading").remove(), 450);
});
animate();
</script>
</body>
</html>
```