Prompt specification
Build or customize the Three.js 3D scene "Expansion joint rail segment" 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: Expansion joint rail segment
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/expansion-joint-rail-segment-x1505
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a low-poly rail expansion-joint demonstration. Mount two short rail segments on sleepers with a narrow gap between their facing ends; keep their outer ends fixed for this idealized comparison, and use a temperature control to change their lengths toward the joint. A magnified inset should show the shrinking gap with clear measurement markers while the main view retains the whole assembly. State the ideal linear-expansion assumption and label the displayed displacement as exaggerated for visibility. Stop or flag the model when the chosen parameters would close the gap rather than allowing the rail ends to overlap.
Working Three.js Starter (expansion-joint-rail-segment-x1505):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Low-Poly Rail Expansion Joint</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: #dce9ec;
}
button,
input {
font: inherit;
}
#mainCanvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#mainCanvas:active {
cursor: grabbing;
}
.title-card {
position: fixed;
top: 18px;
left: 18px;
z-index: 5;
max-width: 440px;
padding: 14px 16px;
color: #f3f8f9;
background: rgba(16, 31, 38, 0.86);
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 12px;
box-shadow: 0 12px 35px rgba(15, 35, 43, 0.16);
pointer-events: none;
backdrop-filter: blur(8px);
}
.title-card h1 {
margin: 0;
font-size: clamp(19px, 2vw, 27px);
line-height: 1.15;
letter-spacing: -0.02em;
}
.title-card p {
margin: 7px 0 0;
color: #c5d5da;
font-size: 13px;
line-height: 1.45;
}
.exaggeration-badge {
display: inline-block;
margin-top: 10px;
padding: 5px 8px;
color: #251708;
background: #ffc15a;
border-radius: 5px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.045em;
text-transform: uppercase;
}
#insetPanel {
position: fixed;
top: 18px;
right: 18px;
z-index: 6;
width: min(380px, calc(100vw - 36px));
overflow: hidden;
color: #f3f8f9;
background: rgba(16, 31, 38, 0.9);
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 12px;
box-shadow: 0 14px 40px rgba(15, 35, 43, 0.23);
pointer-events: none;
backdrop-filter: blur(8px);
}
.inset-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 9px 12px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
border-bottom: 1px solid rgba(255, 255, 255, 0.12);
}
.inset-header span:last-child {
color: #ffc15a;
}
#insetCanvas {
display: block;
width: 100%;
height: 220px;
background: #e5eef0;
}
.inset-readout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
padding: 10px 12px 12px;
font-size: 12px;
}
.inset-readout div {
padding: 7px 8px;
background: rgba(255, 255, 255, 0.07);
border-radius: 6px;
}
.inset-readout span {
display: block;
color: #adc1c8;
font-size: 10px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.inset-readout strong {
display: block;
margin-top: 2px;
font-size: 14px;
}
#controlsPanel {
position: fixed;
left: 18px;
bottom: 18px;
z-index: 7;
width: min(410px, calc(100vw - 36px));
padding: 14px;
color: #edf5f6;
background: rgba(16, 31, 38, 0.9);
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 12px;
box-shadow: 0 14px 40px rgba(15, 35, 43, 0.24);
backdrop-filter: blur(9px);
}
.status {
min-height: 40px;
display: flex;
align-items: center;
padding: 8px 10px;
margin-bottom: 12px;
color: #dcebed;
background: rgba(255, 255, 255, 0.07);
border-left: 4px solid #58b7c8;
border-radius: 5px;
font-size: 12px;
line-height: 1.35;
}
.status.heating {
border-left-color: #ffad42;
color: #ffe0b5;
}
.status.cooling {
border-left-color: #62c8e8;
color: #c8f1ff;
}
.status.stopped {
color: #ffd6d1;
background: rgba(190, 48, 38, 0.26);
border-left-color: #ff5a48;
font-weight: 700;
}
.temperature-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
}
.temperature-row label {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.045em;
text-transform: uppercase;
}
#temperatureValue {
font-size: 21px;
font-variant-numeric: tabular-nums;
}
#temperatureSlider {
width: 100%;
margin: 0;
accent-color: #ff9d38;
cursor: pointer;
}
.slider-labels {
display: flex;
justify-content: space-between;
margin-top: 3px;
color: #9fb4bb;
font-size: 10px;
}
.readout-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 12px;
}
.readout-grid div {
padding: 7px;
background: rgba(255, 255, 255, 0.065);
border-radius: 6px;
}
.readout-grid span {
display: block;
color: #9fb4bb;
font-size: 9px;
line-height: 1.2;
text-transform: uppercase;
}
.readout-grid strong {
display: block;
margin-top: 3px;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.assumption {
margin: 12px 0 0;
padding-top: 10px;
color: #b9cbd0;
border-top: 1px solid rgba(255, 255, 255, 0.12);
font-size: 10.5px;
line-height: 1.45;
}
.assumption b {
color: #f3f8f9;
}
.button-row {
display: flex;
gap: 8px;
margin-top: 10px;
}
button {
flex: 1;
padding: 8px 9px;
color: #f5fafb;
background: rgba(255, 255, 255, 0.1);
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 6px;
cursor: pointer;
font-size: 11px;
font-weight: 750;
}
button:hover {
background: rgba(255, 255, 255, 0.17);
}
button.warning {
color: #251708;
background: #ffb24b;
border-color: #ffc777;
}
.pin {
position: fixed;
z-index: 4;
transform: translate(-50%, -100%);
padding: 4px 7px;
color: #14262c;
background: rgba(255, 255, 255, 0.9);
border: 1px solid rgba(20, 38, 44, 0.18);
border-radius: 5px;
box-shadow: 0 4px 12px rgba(20, 38, 44, 0.12);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.04em;
pointer-events: none;
white-space: nowrap;
}
.pin.fixed {
color: #3a2106;
background: #ffc76c;
}
.pin.joint {
color: #fff;
background: #e9613f;
}
@media (max-width: 760px) {
.title-card {
top: 10px;
left: 10px;
max-width: calc(100vw - 20px);
padding: 11px 13px;
}
.title-card p {
display: none;
}
#insetPanel {
top: 82px;
right: 10px;
width: calc(100vw - 20px);
}
#insetCanvas {
height: 170px;
}
#controlsPanel {
left: 10px;
bottom: 10px;
width: calc(100vw - 20px);
}
}
</style>
</head>
<body>
<canvas id="mainCanvas" aria-label="Interactive main rail assembly"></canvas>
<header class="title-card">
<h1>Rail Expansion Joint</h1>
<p>
Drag to orbit, use the wheel to zoom, and change temperature to move
the joint-facing rail ends. The outer ends remain fixed.
</p>
<span class="exaggeration-badge">
Displayed displacement exaggerated ×6 for visibility
</span>
</header>
<aside id="insetPanel" aria-label="Magnified joint inset">
<div class="inset-header">
<span>Magnified joint</span>
<span>Dimension ticks: rendered gap</span>
</div>
<canvas id="insetCanvas"></canvas>
<div class="inset-readout">
<div>
<span>Rendered gap</span>
<strong id="visualGapValue">72.0 mm</strong>
</div>
<div>
<span>Equivalent physical gap</span>
<strong id="physicalGapValue">12.0 mm</strong>
</div>
</div>
</aside>
<section id="controlsPanel">
<div id="status" class="status" role="status" aria-live="polite">
Reference state.
</div>
<div class="temperature-row">
<label for="temperatureSlider">Rail temperature</label>
<strong id="temperatureValue">20.0 °C</strong>
</div>
<input
id="temperatureSlider"
type="range"
min="-40"
max="260"
step="0.1"
value="20"
aria-label="Rail temperature"
/>
<div class="slider-labels">
<span>−40 °C</span>
<span>Reference: 20 °C</span>
<span>Closure limit: 228.3 °C</span>
<span>260 °C</span>
</div>
<div class="readout-grid">
<div>
<span>Physical gap</span>
<strong id="physicalGapReadout">12.0 mm</strong>
</div>
<div>
<span>Travel per end</span>
<strong id="physicalTravelReadout">0.00 mm</strong>
</div>
<div>
<span>Rendered gap</span>
<strong id="renderedGapReadout">72.0 mm</strong>
</div>
</div>
<p class="assumption">
<b>Ideal linear-expansion assumption:</b>
ΔL = αL(T − T₀), with L = 2.4 m,
α = 12×10⁻⁶ °C⁻¹ (steel-like), and T₀ = 20 °C.
Each rail is a uniform, unstressed kinematic bar with one fixed outer
anchor and one translating joint-facing end. Buckling, yielding,
fastener effects, and thermal stress are not modeled.
Rendered travel and the corresponding reference-gap rendering are
scaled ×6; the numerical physical gap and travel remain unscaled.
</p>
<div class="button-row">
<button id="resetButton" type="button">Reset / reference</button>
<button id="closureButton" class="warning" type="button">
Test closure limit
</button>
</div>
</section>
<div id="leftPin" class="pin fixed">FIXED OUTER END</div>
<div id="rightPin" class="pin fixed">FIXED OUTER END</div>
<div id="jointPin" class="pin joint">JOINT</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const CONFIG = Object.freeze({
railLength: 2.4,
physicalGap: 0.012,
alpha: 12e-6,
referenceTemperature: 20,
displayScale: 6,
minimumTemperature: -40,
maximumTemperature: 260
});
const closureDeltaT =
CONFIG.physicalGap / (2 * CONFIG.alpha * CONFIG.railLength);
const closureTemperature = CONFIG.referenceTemperature + closureDeltaT;
const visualInitialGap = CONFIG.physicalGap * CONFIG.displayScale;
const visualHalfGap = visualInitialGap / 2;
// These anchors never move.
const leftOuterX = -(CONFIG.railLength + visualHalfGap);
const rightOuterX = CONFIG.railLength + visualHalfGap;
const mainCanvas = document.getElementById("mainCanvas");
const insetCanvas = document.getElementById("insetCanvas");
const mainRenderer = new THREE.WebGLRenderer({
canvas: mainCanvas,
antialias: true,
powerPreference: "high-performance"
});
mainRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
mainRenderer.outputColorSpace = THREE.SRGBColorSpace;
mainRenderer.toneMapping = THREE.ACESFilmicToneMapping;
mainRenderer.toneMappingExposure = 1.08;
mainRenderer.shadowMap.enabled = true;
mainRenderer.shadowMap.type = THREE.PCFSoftShadowMap;
const insetRenderer = new THREE.WebGLRenderer({
canvas: insetCanvas,
antialias: true
});
insetRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
insetRenderer.outputColorSpace = THREE.SRGBColorSpace;
insetRenderer.toneMapping = THREE.ACESFilmicToneMapping;
insetRenderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9ec);
scene.fog = new THREE.Fog(0xdce9ec, 13, 27);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Inset measurement objects use layer 1. The main camera sees layer 0 only.
const insetCamera = new THREE.OrthographicCamera(-0.7, 0.7, 0.27, -0.27, 0.05, 20);
insetCamera.layers.set(0);
insetCamera.layers.enable(1);
insetCamera.position.set(0.48, 1.04, 1.72);
insetCamera.lookAt(0, 0.36, 0);
const hemisphere = new THREE.HemisphereLight(0xeaf8ff, 0x5a5145, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3da, 3.2);
sun.position.set(-4, 8, 5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 6;
sun.shadow.camera.bottom = -6;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 25;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8bc9e0, 1.1);
fill.position.set(5, 3, -4);
scene.add(fill);
const ballastMaterial = new THREE.MeshStandardMaterial({
color: 0x796f63,
roughness: 1,
flatShading: true
});
const sleeperMaterial = new THREE.MeshStandardMaterial({
color: 0x5d4b3b,
roughness: 0.94,
flatShading: true
});
const railBaseMaterial = new THREE.MeshStandardMaterial({
color: 0x38474d,
metalness: 0.68,
roughness: 0.48,
flatShading: true
});
const railWebMaterial = new THREE.MeshStandardMaterial({
color: 0x48575c,
metalness: 0.66,
roughness: 0.42,
flatShading: true
});
const railHeadMaterial = new THREE.MeshStandardMaterial({
color: 0x9ba7a9,
metalness: 0.88,
roughness: 0.24,
flatShading: true
});
const clampMaterial = new THREE.MeshStandardMaterial({
color: 0xf2a63b,
metalness: 0.25,
roughness: 0.55,
flatShading: true
});
const endMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0xe9613f,
metalness: 0.25,
roughness: 0.5,
flatShading: true
});
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;
}
// Ground and low-poly track bed.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 30),
new THREE.MeshStandardMaterial({
color: 0xbfd0cb,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.085;
ground.receiveShadow = true;
scene.add(ground);
const ballast = makeBox(6.2, 0.16, 1.65, ballastMaterial);
ballast.position.set(0, 0, 0);
scene.add(ballast);
const sleeperXs = [-2.05, -1.48, -0.91, -0.34, 0.34, 0.91, 1.48, 2.05];
for (const x of sleeperXs) {
const sleeper = makeBox(0.23, 0.13, 1.55, sleeperMaterial);
sleeper.position.set(x, 0.145, 0);
scene.add(sleeper);
}
// Each rail's local +X direction points toward the joint.
function createRail(outerX, flipped) {
const group = new THREE.Group();
group.position.x = outerX;
if (flipped) group.rotation.y = Math.PI;
const pieces = [];
const specifications = [
{ height: 0.05, depth: 0.20, material: railBaseMaterial, y: 0.235 },
{ height: 0.14, depth: 0.052, material: railWebMaterial, y: 0.325 },
{ height: 0.06, depth: 0.13, material: railHeadMaterial, y: 0.425 }
];
for (const specification of specifications) {
const piece = makeBox(
1,
specification.height,
specification.depth,
specification.material
);
piece.position.y = specification.y;
group.add(piece);
pieces.push(piece);
}
const innerEndMarker = makeBox(
0.012,
0.28,
0.205,
endMarkerMaterial
);
innerEndMarker.position.y = 0.325;
group.add(innerEndMarker);
scene.add(group);
return { group, pieces, innerEndMarker };
}
const leftRail = createRail(leftOuterX, false);
const rightRail = createRail(rightOuterX, true);
function setRailLength(rail, length) {
for (const piece of rail.pieces) {
piece.scale.x = length;
piece.position.x = length / 2;
}
rail.innerEndMarker.position.x = length - 0.006;
}
// Orange outer clamps make the fixed anchors explicit.
const leftClamp = makeBox(0.13, 0.29, 0.52, clampMaterial);
leftClamp.position.set(leftOuterX - 0.065, 0.26, 0);
scene.add(leftClamp);
const rightClamp = makeBox(0.13, 0.29, 0.52, clampMaterial);
rightClamp.position.set(rightOuterX + 0.065, 0.26, 0);
scene.add(rightClamp);
const clampFootLeft = makeBox(0.42, 0.07, 0.64, clampMaterial);
clampFootLeft.position.set(leftOuterX - 0.065, 0.105, 0);
scene.add(clampFootLeft);
const clampFootRight = makeBox(0.42, 0.07, 0.64, clampMaterial);
clampFootRight.position.set(rightOuterX + 0.065, 0.105, 0);
scene.add(clampFootRight);
// Dynamic inset-only measurement geometry.
const dimensionGroup = new THREE.Group();
function createDynamicLine(color) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({ color })
);
dimensionGroup.add(line);
return line;
}
const horizontalDimension = createDynamicLine(0xe9613f);
const leftExtension = createDynamicLine(0xe9613f);
const rightExtension = createDynamicLine(0xe9613f);
const arrowGeometry = new THREE.ConeGeometry(0.025, 0.08, 6);
const arrowMaterial = new THREE.MeshBasicMaterial({ color: 0xe9613f });
const leftArrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
leftArrow.rotation.z = Math.PI / 2;
dimensionGroup.add(leftArrow);
const rightArrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
rightArrow.rotation.z = -Math.PI / 2;
dimensionGroup.add(rightArrow);
dimensionGroup.traverse((object) => object.layers.set(1));
scene.add(dimensionGroup);
function updateLine(line, start, end) {
const attribute = line.geometry.getAttribute("position");
attribute.setXYZ(0, start.x, start.y, start.z);
attribute.setXYZ(1, end.x, end.y, end.z);
attribute.needsUpdate = true;
line.geometry.computeBoundingSphere();
}
function updateDimensionMarkers(leftInnerX, rightInnerX) {
const y = 0.585;
const z = 0.27;
updateLine(
horizontalDimension,
new THREE.Vector3(leftInnerX, y, z),
new THREE.Vector3(rightInnerX, y, z)
);
updateLine(
leftExtension,
new THREE.Vector3(leftInnerX, 0.445, z),
new THREE.Vector3(leftInnerX, 0.625, z)
);
updateLine(
rightExtension,
new THREE.Vector3(rightInnerX, 0.445, z),
new THREE.Vector3(rightInnerX, 0.625, z)
);
leftArrow.position.set(leftInnerX + 0.04, y, z);
rightArrow.position.set(rightInnerX - 0.04, y, z);
}
const temperatureSlider = document.getElementById("temperatureSlider");
const temperatureValue = document.getElementById("temperatureValue");
const statusElement = document.getElementById("status");
const physicalGapReadout = document.getElementById("physicalGapReadout");
const physicalTravelReadout = document.getElementById("physicalTravelReadout");
const renderedGapReadout = document.getElementById("renderedGapReadout");
const visualGapValue = document.getElementById("visualGapValue");
const physicalGapValue = document.getElementById("physicalGapValue");
temperatureSlider.min = CONFIG.minimumTemperature;
temperatureSlider.max = CONFIG.maximumTemperature;
temperatureSlider.value = CONFIG.referenceTemperature;
let modelStopped = false;
function updateModel(temperature) {
const rawPhysicalTravel =
CONFIG.alpha *
CONFIG.railLength *
(temperature - CONFIG.referenceTemperature);
// Latch the model at the first possible closure.
if (!modelStopped && rawPhysicalTravel >= CONFIG.physicalGap / 2) {
modelStopped = true;
}
// When stopped, hold the ends at first contact and never allow overlap.
const physicalTravel = modelStopped
? CONFIG.physicalGap / 2
: rawPhysicalTravel;
const physicalGap = Math.max(
0,
CONFIG.physicalGap - 2 * physicalTravel
);
const visualTravel = physicalTravel * CONFIG.displayScale;
const visualGap = Math.max(
0,
visualInitialGap - 2 * visualTravel
);
const leftInnerX = -visualHalfGap + visualTravel;
const rightInnerX = visualHalfGap - visualTravel;
const renderedLength = CONFIG.railLength + visualTravel;
setRailLength(leftRail, renderedLength);
setRailLength(rightRail, renderedLength);
updateDimensionMarkers(leftInnerX, rightInnerX);
const physicalGapMm = physicalGap * 1000;
const physicalTravelMm = physicalTravel * 1000;
const visualGapMm = visualGap * 1000;
temperatureValue.textContent = `${temperature.toFixed(1)} °C`;
temperatureSlider.setAttribute(
"aria-valuetext",
`${temperature.toFixed(1)} degrees Celsius`
);
physicalGapReadout.textContent = `${physicalGapMm.toFixed(1)} mm`;
physicalTravelReadout.textContent = `${physicalTravelMm.toFixed(2)} mm`;
renderedGapReadout.textContent = `${visualGapMm.toFixed(1)} mm`;
visualGapValue.textContent = `${visualGapMm.toFixed(1)} mm`;
physicalGapValue.textContent = `${physicalGapMm.toFixed(1)} mm`;
statusElement.className = "status";
if (modelStopped) {
statusElement.classList.add("stopped");
statusElement.textContent =
`MODEL STOPPED: closure is predicted at ${closureTemperature.toFixed(1)} °C. ` +
`The physical gap is 0.0 mm and the rail ends are held at first contact. ` +
`Overlap is prevented; reset the model to continue.`;
} else if (temperature > CONFIG.referenceTemperature) {
statusElement.classList.add("heating");
statusElement.textContent =
`Heating: each joint-facing end travels inward by ` +
`${Math.abs(physicalTravelMm).toFixed(2)} mm in the physical model. ` +
`The physical gap is shrinking.`;
} else if (temperature < CONFIG.referenceTemperature) {
statusElement.classList.add("cooling");
statusElement.textContent =
`Cooling: each rail contracts, moving its joint-facing end away from ` +
`the joint by ${Math.abs(physicalTravelMm).toFixed(2)} mm. ` +
`The physical gap is opening.`;
} else {
statusElement.textContent =
`Reference state at ${CONFIG.referenceTemperature.toFixed(1)} °C: ` +
`the physical gap is 12.0 mm and neither rail end is moving.`;
}
}
temperatureSlider.addEventListener("input", () => {
updateModel(Number(temperatureSlider.value));
});
document.getElementById("resetButton").addEventListener("click", () => {
modelStopped = false;
temperatureSlider.value = CONFIG.referenceTemperature;
updateModel(CONFIG.referenceTemperature);
});
document.getElementById("closureButton").addEventListener("click", () => {
modelStopped = false;
temperatureSlider.value = closureTemperature + 0.1;
updateModel(Number(temperatureSlider.value));
});
// Camera controls are implemented directly here, without addons.
const orbit = {
target: new THREE.Vector3(0, 0.22, 0),
yaw: 0.59,
pitch: 0.47,
distance: 8.8
};
function updateMainCamera() {
const horizontalDistance =
orbit.distance * Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + horizontalDistance * Math.sin(orbit.yaw),
orbit.target.y + orbit.distance * Math.sin(orbit.pitch),
orbit.target.z + horizontalDistance * Math.cos(orbit.yaw)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.yaw = 0.59;
orbit.pitch = 0.47;
orbit.distance = 8.8;
updateMainCamera();
}
let dragging = false;
let previousX = 0;
let previousY = 0;
mainCanvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
mainCanvas.setPointerCapture(event.pointerId);
});
mainCanvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
orbit.yaw -= deltaX * 0.006;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + deltaY * 0.005,
0.18,
1.22
);
updateMainCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (mainCanvas.hasPointerCapture(event.pointerId)) {
mainCanvas.releasePointerCapture(event.pointerId);
}
}
}
mainCanvas.addEventListener("pointerup", endDrag);
mainCanvas.addEventListener("pointercancel", endDrag);
mainCanvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.distance = THREE.MathUtils.clamp(
orbit.distance * Math.exp(event.deltaY * 0.001),
5.4,
14
);
updateMainCamera();
},
{ passive: false }
);
mainCanvas.addEventListener("dblclick", resetCamera);
// Project simple HTML labels from world positions.
const leftPin = document.getElementById("leftPin");
const rightPin = document.getElementById("rightPin");
const jointPin = document.getElementById("jointPin");
const leftPinPosition = new THREE.Vector3(leftOuterX, 0.69, 0);
const rightPinPosition = new THREE.Vector3(rightOuterX, 0.69, 0);
const jointPinPosition = new THREE.Vector3(0, 0.76, 0.1);
function placePin(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
mainRenderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
const insetWidth = Math.max(1, insetCanvas.clientWidth);
const insetHeight = Math.max(1, insetCanvas.clientHeight);
insetRenderer.setSize(insetWidth, insetHeight, false);
const insetAspect = insetWidth / insetHeight;
const halfHeight = 0.27;
insetCamera.left = -halfHeight * insetAspect;
insetCamera.right = halfHeight * insetAspect;
insetCamera.top = halfHeight;
insetCamera.bottom = -halfHeight;
insetCamera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
if ("ResizeObserver" in window) {
new ResizeObserver(resize).observe(insetCanvas);
}
resize();
updateMainCamera();
updateModel(CONFIG.referenceTemperature);
function animate() {
mainRenderer.render(scene, camera);
insetRenderer.render(scene, insetCamera);
placePin(leftPin, leftPinPosition);
placePin(rightPin, rightPinPosition);
placePin(jointPin, jointPinPosition);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```