Prompt specification
Build or customize the Three.js 3D scene "Steam-era energy museum exhibit" 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: Steam-era energy museum exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/steam-era-energy-museum-exhibit-x1479
Scene Description Prompt:
Compose a boiler-shaped heat source, piston model, flywheel, and cold reservoir with clear energy-flow labels; present an educational abstraction rather than a buildable pressure system. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (steam-era-energy-museum-exhibit-x1479):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Heat Engine — Educational Abstraction</title>
<style>
:root {
color-scheme: dark;
--bg: #091019;
--panel: rgba(10, 17, 27, 0.9);
--line: rgba(165, 196, 220, 0.18);
--text: #eaf4fb;
--muted: #9cb0c1;
--hot: #ff7138;
--work: #ffc857;
--cold: #42c8ff;
}
* {
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;
}
#stage,
#labels {
position: absolute;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#stage canvas:active {
cursor: grabbing;
}
#labels {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.panel {
position: absolute;
z-index: 4;
top: 18px;
left: 18px;
width: min(340px, calc(100vw - 36px));
padding: 17px 18px 16px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 7px;
color: #7dd9ff;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 29px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 14px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.diagram-key {
display: grid;
gap: 7px;
margin: 13px 0;
padding: 11px;
border: 1px solid rgba(165, 196, 220, 0.12);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
font-size: 11px;
line-height: 1.35;
}
.key-row {
display: flex;
align-items: center;
gap: 8px;
}
.swatch {
width: 19px;
height: 4px;
flex: 0 0 auto;
border-radius: 99px;
box-shadow: 0 0 12px currentColor;
}
.swatch.hot {
color: var(--hot);
background: currentColor;
}
.swatch.work {
color: var(--work);
background: currentColor;
}
.swatch.cold {
color: var(--cold);
background: currentColor;
}
.key-row strong {
min-width: 28px;
}
.note {
margin: 0;
color: #aab9c7;
font-size: 10.5px;
line-height: 1.5;
}
.note strong {
color: #e7f3fa;
}
.tag {
--accent: #ffffff;
position: absolute;
top: 0;
left: 0;
min-width: max-content;
padding: 7px 9px 7px 10px;
border: 1px solid rgba(190, 214, 230, 0.22);
border-left: 3px solid var(--accent);
border-radius: 5px;
background: rgba(8, 15, 24, 0.88);
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.3);
color: var(--text);
font-size: 9.5px;
line-height: 1.15;
letter-spacing: 0.055em;
text-transform: uppercase;
white-space: nowrap;
will-change: transform;
}
.tag strong {
display: block;
color: var(--accent);
font-size: 10px;
letter-spacing: 0.09em;
}
.tag small {
display: block;
margin-top: 3px;
color: #aebfcb;
font-size: 8px;
font-weight: 600;
letter-spacing: 0.045em;
}
.tag.object {
background: rgba(8, 15, 24, 0.77);
}
.controls {
position: absolute;
z-index: 5;
right: 18px;
bottom: 18px;
display: flex;
gap: 8px;
align-items: center;
}
button {
min-height: 36px;
padding: 0 13px;
border: 1px solid rgba(177, 208, 229, 0.25);
border-radius: 7px;
background: rgba(11, 20, 31, 0.88);
color: #eaf5fc;
font: 700 10px/1 ui-sans-serif, system-ui, sans-serif;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
}
button:hover {
border-color: rgba(125, 217, 255, 0.7);
background: rgba(20, 35, 48, 0.95);
}
.help {
position: absolute;
z-index: 3;
right: 18px;
bottom: 65px;
color: rgba(198, 216, 229, 0.62);
font-size: 9px;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 700px) {
.panel {
top: 10px;
left: 10px;
width: min(310px, calc(100vw - 20px));
padding: 13px 14px;
}
.subtitle {
margin-bottom: 9px;
}
.diagram-key {
gap: 5px;
margin: 9px 0;
padding: 8px;
}
.tag.secondary {
display: none;
}
.tag {
padding: 5px 7px;
font-size: 8px;
}
.tag strong {
font-size: 8.5px;
}
.tag small {
display: none;
}
.controls {
right: 10px;
bottom: 10px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels" aria-hidden="true"></div>
<section class="panel">
<div class="eyebrow">Educational abstraction</div>
<h1>Voxel Heat Engine</h1>
<p class="subtitle">
A conceptual energy-flow model: a hot boiler feeds a working piston,
the piston drives a flywheel, and rejected heat moves to a cold reservoir.
</p>
<div class="diagram-key">
<div class="key-row">
<span class="swatch hot"></span>
<strong>Q<sub>H</sub></strong>
<span>Heat supplied by the hot boiler</span>
</div>
<div class="key-row">
<span class="swatch work"></span>
<strong>W</strong>
<span>Mechanical work delivered to the flywheel</span>
</div>
<div class="key-row">
<span class="swatch cold"></span>
<strong>Q<sub>C</sub></strong>
<span>Heat rejected to the cold reservoir</span>
</div>
</div>
<p class="note">
<strong>Not a buildable pressure system.</strong>
Shapes, dimensions, pipe routes, and arrows are diagrammatic. The flywheel
stores and smooths mechanical energy; it is not a net energy source.
</p>
</section>
<div class="help">Drag to orbit · Scroll to zoom · Space to pause</div>
<div class="controls">
<button id="pause" type="button">Pause</button>
<button id="reset" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const labelsRoot = document.querySelector("#labels");
const pauseButton = document.querySelector("#pause");
const resetButton = document.querySelector("#reset");
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x091019);
scene.fog = new THREE.Fog(0x091019, 20, 43);
const renderer = new THREE.WebGLRenderer({
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.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.3, 0);
const cameraState = {
azimuth: 0.66,
polar: 1.13,
radius: 14.8
};
function applyCamera() {
const portraitFit = Math.max(1, 1.05 / camera.aspect);
const radius = cameraState.radius * portraitFit;
camera.position.set(
cameraTarget.x +
radius * Math.sin(cameraState.polar) * Math.sin(cameraState.azimuth),
cameraTarget.y + radius * Math.cos(cameraState.polar),
cameraTarget.z +
radius * Math.sin(cameraState.polar) * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
const hemisphere = new THREE.HemisphereLight(0xbfe4ff, 0x251b20, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1dd, 3.0);
keyLight.position.set(5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5dbdff, 1.2);
rimLight.position.set(-8, 5, -7);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Shared geometry and material helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materials = {
floorA: new THREE.MeshStandardMaterial({
color: 0x17232d,
roughness: 0.92,
metalness: 0.05
}),
floorB: new THREE.MeshStandardMaterial({
color: 0x101b25,
roughness: 0.95,
metalness: 0.04
}),
foundation: new THREE.MeshStandardMaterial({
color: 0x263541,
roughness: 0.8,
metalness: 0.12
}),
boiler: new THREE.MeshStandardMaterial({
color: 0x9d4a34,
roughness: 0.72,
metalness: 0.18
}),
boilerDark: new THREE.MeshStandardMaterial({
color: 0x422a2a,
roughness: 0.68,
metalness: 0.28
}),
hot: new THREE.MeshStandardMaterial({
color: 0xff681f,
emissive: 0xff3200,
emissiveIntensity: 1.1,
roughness: 0.48,
metalness: 0.02
}),
hotBright: new THREE.MeshBasicMaterial({
color: 0xffb044
}),
pipe: new THREE.MeshStandardMaterial({
color: 0xd95b2b,
emissive: 0x7f1d05,
emissiveIntensity: 0.36,
roughness: 0.62,
metalness: 0.28
}),
housing: new THREE.MeshStandardMaterial({
color: 0x37606d,
roughness: 0.56,
metalness: 0.38
}),
housingDark: new THREE.MeshStandardMaterial({
color: 0x203944,
roughness: 0.48,
metalness: 0.52
}),
piston: new THREE.MeshStandardMaterial({
color: 0xd8e8ea,
roughness: 0.3,
metalness: 0.68
}),
wheel: new THREE.MeshStandardMaterial({
color: 0xe9ad35,
emissive: 0x5b2c00,
emissiveIntensity: 0.25,
roughness: 0.42,
metalness: 0.58
}),
wheelDark: new THREE.MeshStandardMaterial({
color: 0x6b4518,
roughness: 0.48,
metalness: 0.62
}),
rod: new THREE.MeshStandardMaterial({
color: 0xffd986,
roughness: 0.3,
metalness: 0.68
}),
coldWall: new THREE.MeshStandardMaterial({
color: 0x205b7d,
roughness: 0.58,
metalness: 0.2
}),
coldWater: new THREE.MeshStandardMaterial({
color: 0x27b9f4,
emissive: 0x006b9b,
emissiveIntensity: 0.8,
roughness: 0.32,
metalness: 0.05
}),
coldBright: new THREE.MeshBasicMaterial({
color: 0x80e5ff
})
};
function addBox(w, h, d, x, y, z, material, parent = scene) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addVoxelBatch(items, material, name = "voxel batch") {
if (!items.length) return null;
const mesh = new THREE.InstancedMesh(unitBox, material, items.length);
const dummy = new THREE.Object3D();
items.forEach((item, index) => {
dummy.position.set(item[0], item[1], item[2]);
dummy.scale.set(item[3], item[4], item[5]);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.name = name;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel ground
// -------------------------------------------------------------------------
const floorA = [];
const floorB = [];
for (let ix = -10; ix <= 10; ix++) {
for (let iz = -5; iz <= 5; iz++) {
const item = [
ix * 0.7,
0.02,
iz * 0.7,
0.67,
0.1,
0.67
];
if ((ix + iz) % 2 === 0) floorA.push(item);
else floorB.push(item);
}
}
addVoxelBatch(floorA, materials.floorA, "floor tiles A");
addVoxelBatch(floorB, materials.floorB, "floor tiles B");
addBox(15.4, 0.18, 8.2, 0, -0.1, 0, materials.foundation);
// Component foundations.
addBox(2.7, 0.24, 2.7, -4.35, 0.2, 0, materials.foundation);
addBox(3.0, 0.24, 1.85, -0.35, 0.2, 0, materials.foundation);
addBox(1.4, 0.24, 1.6, 2.3, 0.2, 0, materials.foundation);
addBox(2.7, 0.24, 1.9, 5.2, 0.2, 0, materials.foundation);
// -------------------------------------------------------------------------
// Boiler-shaped hot reservoir
// -------------------------------------------------------------------------
const boilerX = -4.35;
const boilerShell = [];
const boilerTrim = [];
for (let iy = 0; iy < 10; iy++) {
const y = 0.43 + iy * 0.25;
for (let ix = -4; ix <= 4; ix++) {
for (let iz = -4; iz <= 4; iz++) {
const x = boilerX + ix * 0.25;
const z = iz * 0.25;
const radius = Math.hypot(ix * 0.25, iz * 0.25);
if (radius > 1.12) continue;
// Front cutaway window reveals the conceptual hot interior.
const inCutaway =
iz >= 2 &&
Math.abs(x - boilerX) < 0.58 &&
y > 0.93 &&
y < 2.23;
if (inCutaway) continue;
const item = [x, y, z, 0.235, 0.235, 0.235];
if (iy === 2 || iy === 7 || iy === 9) {
boilerTrim.push(item);
} else {
boilerShell.push(item);
}
}
}
}
addVoxelBatch(boilerShell, materials.boiler, "boiler shell voxels");
addVoxelBatch(boilerTrim, materials.boilerDark, "boiler bands");
// Voxel chimney.
const chimney = [];
for (let iy = 0; iy < 6; iy++) {
for (let ix = -1; ix <= 1; ix++) {
for (let iz = -1; iz <= 1; iz++) {
if (Math.abs(ix) + Math.abs(iz) > 1) continue;
chimney.push([
boilerX + ix * 0.19,
2.88 + iy * 0.2,
-0.34 + iz * 0.19,
0.18,
0.18,
0.18
]);
}
}
}
addVoxelBatch(chimney, materials.boilerDark, "voxel chimney");
// Door frame around the cutaway.
addBox(0.15, 1.42, 0.16, boilerX - 0.61, 1.56, 1.0, materials.boilerDark);
addBox(0.15, 1.42, 0.16, boilerX + 0.61, 1.56, 1.0, materials.boilerDark);
addBox(1.35, 0.15, 0.16, boilerX, 0.93, 1.0, materials.boilerDark);
addBox(1.35, 0.15, 0.16, boilerX, 2.2, 1.0, materials.boilerDark);
// Animated flame-like voxel cluster inside the boiler.
const flameGroup = new THREE.Group();
flameGroup.position.set(boilerX, 0, 0.84);
scene.add(flameGroup);
const flameOffsets = [
[-0.42, 0.64, 0.00],
[-0.21, 0.58, 0.08],
[0.00, 0.61, -0.02],
[0.21, 0.57, 0.05],
[0.42, 0.64, 0.00],
[-0.30, 0.82, 0.03],
[-0.10, 0.88, -0.03],
[0.10, 0.85, 0.03],
[0.30, 0.80, 0.00],
[-0.18, 1.08, 0.00],
[0.00, 1.16, 0.00],
[0.18, 1.02, 0.00]
];
flameOffsets.forEach((offset, index) => {
const size = index % 3 === 0 ? 0.23 : 0.18;
const flame = addBox(
size,
size * (index > 8 ? 1.35 : 1),
size,
offset[0],
offset[1],
offset[2],
index % 2 ? materials.hotBright : materials.hot,
flameGroup
);
flame.userData.baseY = offset[1];
flame.userData.phase = index * 0.73;
});
const boilerGlow = new THREE.PointLight(0xff4b17, 3.2, 4.2, 2);
boilerGlow.position.set(boilerX, 1.25, 1.35);
scene.add(boilerGlow);
// Voxel hot line from boiler to piston.
const hotPipe = [];
for (let i = 0; i < 10; i++) {
hotPipe.push([
-3.23 + i * 0.19,
1.17,
0,
0.17,
0.17,
0.17
]);
}
addVoxelBatch(hotPipe, materials.pipe, "hot transfer voxels");
addBox(0.12, 0.42, 0.42, -3.18, 1.17, 0, materials.boilerDark);
addBox(0.12, 0.42, 0.42, -1.73, 1.17, 0, materials.housingDark);
// -------------------------------------------------------------------------
// Piston / working-fluid chamber
// -------------------------------------------------------------------------
const pistonY = 1.45;
const housingShell = [];
const housingTrim = [];
for (let ix = 0; ix < 11; ix++) {
const x = -1.5 + ix * 0.22;
for (let gy = -2; gy <= 2; gy++) {
for (let gz = -2; gz <= 2; gz++) {
const y = pistonY + gy * 0.21;
const z = gz * 0.21;
const radius = Math.hypot(gy * 0.21, gz * 0.21);
if (radius > 0.51) continue;
// Open front-center so the moving piston remains legible.
if (gz >= 1 && Math.abs(gy) <= 1) continue;
const item = [x, y, z, 0.205, 0.205, 0.205];
if (ix === 0 || ix === 10) housingTrim.push(item);
else housingShell.push(item);
}
}
}
addVoxelBatch(housingShell, materials.housing, "piston chamber voxels");
addVoxelBatch(housingTrim, materials.housingDark, "piston chamber end rings");
addBox(0.16, 1.08, 1.08, -1.58, pistonY, 0, materials.housingDark);
addBox(0.16, 1.08, 1.08, 0.78, pistonY, 0, materials.housingDark);
const pistonMesh = addBox(
0.24,
0.72,
0.72,
-0.15,
pistonY,
0,
materials.piston
);
// Orange voxels represent the conceptual working fluid.
const fluidGroup = new THREE.Group();
fluidGroup.position.set(-0.15, pistonY, 0);
scene.add(fluidGroup);
const fluidOffsets = [
[-0.39, 0.18, 0.16],
[-0.39, -0.18, -0.16],
[0.39, 0.18, -0.16],
[0.39, -0.18, 0.16],
[-0.36, 0.28, -0.24],
[0.36, -0.28, 0.24]
];
fluidOffsets.forEach((offset) => {
addBox(
0.16,
0.16,
0.16,
offset[0],
offset[1],
offset[2],
materials.hot,
fluidGroup
);
});
// -------------------------------------------------------------------------
// Flywheel and crank-slider connection
// -------------------------------------------------------------------------
const wheelCenter = new THREE.Vector3(2.3, pistonY, 0);
const wheelGroup = new THREE.Group();
wheelGroup.position.copy(wheelCenter);
scene.add(wheelGroup);
const wheelRing = [];
for (let ix = -5; ix <= 5; ix++) {
for (let iy = -5; iy <= 5; iy++) {
const radius = Math.hypot(ix * 0.19, iy * 0.19);
if (radius < 0.61 || radius > 0.98) continue;
wheelRing.push([
ix * 0.19,
iy * 0.19,
0,
0.175,
0.175,
0.175
]);
}
}
addVoxelBatch(wheelRing, materials.wheel, "flywheel ring voxels");
// Cross-shaped voxel spokes.
for (let i = -4; i <= 4; i++) {
addBox(
0.17,
0.17,
0.17,
i * 0.18,
0,
0,
materials.wheelDark,
wheelGroup
);
addBox(
0.17,
0.17,
0.17,
0,
i * 0.18,
0,
materials.wheelDark,
wheelGroup
);
}
addBox(0.48, 0.48, 0.42, 0, 0, 0, materials.wheelDark, wheelGroup);
addBox(0.26, 0.26, 1.5, wheelCenter.x, wheelCenter.y, 0, materials.housingDark);
const crankRadius = 0.68;
const crankPin = addBox(
0.24,
0.24,
0.24,
crankRadius,
0,
0.28,
materials.hot,
wheelGroup
);
const connectingRod = addBox(
1,
0.12,
0.12,
0,
pistonY,
0.28,
materials.rod
);
// -------------------------------------------------------------------------
// Cold reservoir
// -------------------------------------------------------------------------
const coldX = 5.2;
const coldShell = [];
const coldFill = [];
for (let ix = -4; ix <= 3; ix++) {
for (let iy = 0; iy <= 5; iy++) {
for (let iz = -2; iz <= 2; iz++) {
const x = coldX + ix * 0.25;
const y = 0.39 + iy * 0.25;
const z = iz * 0.25;
const edgeX = ix === -4 || ix === 3;
const edgeZ = iz === -2 || iz === 2;
const edgeY = iy === 0;
const topRim = iy === 5 && (edgeX || edgeZ);
// Front-center cutaway exposes the cold contents.
const frontCutaway = iz === 2 && ix > -4 && ix < 3 && iy > 0;
if (edgeX || edgeZ || edgeY || topRim) {
if (!frontCutaway) {
coldShell.push([x, y, z, 0.235, 0.235, 0.235]);
}
} else if (iy <= 2) {
coldFill.push([x, y, z, 0.235, 0.235, 0.235]);
}
}
}
}
addVoxelBatch(coldShell, materials.coldWall, "cold reservoir walls");
addVoxelBatch(coldFill, materials.coldWater, "cold reservoir contents");
// Small cold-colored motes above the reservoir.
const coldMotes = [];
for (let i = 0; i < 7; i++) {
const mote = addBox(
0.11,
0.11,
0.11,
coldX - 0.65 + (i % 4) * 0.4,
2.03 + Math.floor(i / 4) * 0.25,
0.1 + (i % 2) * 0.15,
materials.coldBright
);
mote.userData.baseY = mote.position.y;
mote.userData.phase = i * 0.9;
coldMotes.push(mote);
}
// -------------------------------------------------------------------------
// Voxel energy arrows
// -------------------------------------------------------------------------
function addHorizontalArrow(startX, startY, endX, y, z, material) {
const shaftEnd = endX - 0.3;
const count = Math.max(2, Math.floor((shaftEnd - startX) / 0.19));
for (let i = 0; i <= count; i++) {
const t = i / count;
addBox(
0.105,
0.105,
0.105,
THREE.MathUtils.lerp(startX, shaftEnd, t),
y,
z,
material
);
}
// Stepped, right-pointing voxel arrowhead.
addBox(0.13, 0.13, 0.13, endX, y, z, material);
addBox(0.13, 0.13, 0.13, endX - 0.18, y, z, material);
addBox(0.13, 0.13, 0.13, endX - 0.18, y + 0.16, z, material);
addBox(0.13, 0.13, 0.13, endX - 0.18, y - 0.16, z, material);
addBox(0.13, 0.13, 0.13, endX - 0.34, y + 0.32, z, material);
addBox(0.13, 0.13, 0.13, endX - 0.34, y - 0.32, z, material);
}
addHorizontalArrow(
-3.35,
2.72,
-1.15,
2.72,
1.08,
materials.hot
);
addHorizontalArrow(
0.15,
3.18,
1.92,
3.18,
1.1,
materials.wheel
);
addHorizontalArrow(
0.15,
0.35,
4.02,
0.35,
1.12,
materials.coldWater
);
// Moving packets reinforce direction and energy type.
const flowPackets = [];
function addFlowPackets(startX, endX, y, z, material, count, speed) {
for (let i = 0; i < count; i++) {
const packet = addBox(
0.15,
0.15,
0.15,
startX,
y,
z,
material
);
packet.castShadow = false;
flowPackets.push({
mesh: packet,
startX,
endX,
y,
z,
offset: i / count,
speed
});
}
}
addFlowPackets(-3.25, -1.22, 2.72, 1.08, materials.hotBright, 3, 0.31);
addFlowPackets(0.25, 1.83, 3.18, 1.1, materials.rod, 2, 0.38);
addFlowPackets(0.25, 3.93, 0.35, 1.12, materials.coldBright, 3, 0.25);
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelDefinitions = [
{
title: "Boiler / Hot Reservoir",
subtitle: "Conceptual heat source",
color: "#ff7138",
position: new THREE.Vector3(boilerX, 3.55, 0.75),
secondary: true
},
{
title: "Q_H · Heat In",
subtitle: "Hot reservoir → working fluid",
color: "#ff7138",
position: new THREE.Vector3(-2.2, 3.14, 1.12)
},
{
title: "Piston / Working Fluid",
subtitle: "Pressure changes drive the piston",
color: "#e8f4f7",
position: new THREE.Vector3(-0.68, 2.13, 0.45),
secondary: true
},
{
title: "W · Mechanical Work",
subtitle: "Piston → flywheel",
color: "#ffc857",
position: new THREE.Vector3(1.03, 3.66, 1.12)
},
{
title: "Flywheel",
subtitle: "Rotational energy store",
color: "#ffc857",
position: new THREE.Vector3(2.55, 3.85, 0.1),
secondary: true
},
{
title: "Q_C · Heat Out",
subtitle: "Working fluid → cold sink",
color: "#42c8ff",
position: new THREE.Vector3(2.65, 0.78, 1.15)
},
{
title: "Cold Reservoir / Sink",
subtitle: "Conceptual rejection reservoir",
color: "#42c8ff",
position: new THREE.Vector3(5.35, 2.45, 0.55),
secondary: true
}
];
const labels = labelDefinitions.map((definition) => {
const element = document.createElement("div");
element.className = `tag${definition.secondary ? " secondary" : ""}`;
element.style.setProperty("--accent", definition.color);
element.innerHTML = `
<strong>${definition.title}</strong>
<small>${definition.subtitle}</small>
`;
labelsRoot.appendChild(element);
return {
element,
position: definition.position
};
});
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
labels.forEach((label) => {
projected.copy(label.position).project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible = projected.z > -1 && projected.z < 1;
label.element.style.opacity = visible ? "1" : "0";
label.element.style.transform =
`translate(${x.toFixed(1)}px, ${y.toFixed(1)}px) translate(-50%, -50%)`;
});
}
// -------------------------------------------------------------------------
// Direct camera controls: drag to orbit and wheel to zoom
// -------------------------------------------------------------------------
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;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - dy * 0.005,
0.48,
1.45
);
pointerX = event.clientX;
pointerY = event.clientY;
applyCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
9.5,
23
);
applyCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.azimuth = 0.66;
cameraState.polar = 1.13;
cameraState.radius = 14.8;
applyCamera();
}
resetButton.addEventListener("click", resetCamera);
let paused = false;
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const zAxis = new THREE.Vector3(0, 0, 1);
const xAxis = new THREE.Vector3(1, 0, 0);
const pinWorld = new THREE.Vector3();
const rodDirection = new THREE.Vector3();
const rodMidpoint = new THREE.Vector3();
let simulationTime = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) simulationTime += delta;
const angle = simulationTime * 1.25;
wheelGroup.rotation.z = angle;
// Crank pin in world space.
pinWorld
.set(crankRadius, 0, 0.28)
.applyAxisAngle(zAxis, angle)
.add(wheelCenter);
// Exact crank-slider relation for the educational piston motion.
const rodLength = 2.2;
const verticalOffset = pinWorld.y - pistonY;
const horizontalProjection = Math.sqrt(
Math.max(
0.01,
rodLength * rodLength -
verticalOffset * verticalOffset
)
);
const pistonX = pinWorld.x - horizontalProjection;
pistonMesh.position.x = pistonX;
fluidGroup.position.x = pistonX;
// Connecting rod.
rodDirection.set(
pinWorld.x - (pistonX + 0.12),
pinWorld.y - pistonY,
0
);
const rodLengthNow = rodDirection.length();
rodDirection.normalize();
rodMidpoint.set(
(pinWorld.x + pistonX + 0.12) * 0.5,
(pinWorld.y + pistonY) * 0.5,
0.28
);
connectingRod.position.copy(rodMidpoint);
connectingRod.quaternion.setFromUnitVectors(xAxis, rodDirection);
connectingRod.scale.set(rodLengthNow, 0.12, 0.12);
// Flickering conceptual hot interior.
flameGroup.children.forEach((flame, index) => {
const phase = flame.userData.phase;
flame.position.y =
flame.userData.baseY +
Math.sin(simulationTime * 5.5 + phase) * 0.045;
flame.rotation.y =
Math.sin(simulationTime * 2.2 + phase) * 0.18;
flame.scale.y =
flame.userData.baseY > 1
? 0.18 * (1 + Math.sin(simulationTime * 6 + index) * 0.2)
: 0.18;
});
materials.hot.emissiveIntensity =
1.05 + Math.sin(simulationTime * 4.2) * 0.18;
boilerGlow.intensity = 3.0 + Math.sin(simulationTime * 3.7) * 0.55;
// Cold reservoir motes.
coldMotes.forEach((mote) => {
mote.position.y =
mote.userData.baseY +
Math.sin(simulationTime * 1.4 + mote.userData.phase) * 0.08;
});
materials.coldWater.emissiveIntensity =
0.72 + Math.sin(simulationTime * 1.8) * 0.12;
// Animated flow packets.
flowPackets.forEach((packet) => {
const progress = (simulationTime * packet.speed + packet.offset) % 1;
packet.mesh.position.x = THREE.MathUtils.lerp(
packet.startX,
packet.endX,
progress
);
packet.mesh.position.y =
packet.y + Math.sin(progress * Math.PI * 4) * 0.025;
});
updateLabels();
renderer.render(scene, camera);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
applyCamera();
}
window.addEventListener("resize", resize);
applyCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```