Prompt specification
Build or customize the Three.js 3D scene "Expansion coefficient comparison tool" 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 coefficient comparison tool
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/expansion-coefficient-comparison-tool-x1508
Scene Description Prompt:
Compare voxel material bars expanding with temperature, using stated coefficients and flagging the linear approximation's limits. Three.js from cdnjs, one HTML file.
Working Three.js Starter (expansion-coefficient-comparison-tool-x1508):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Thermal Expansion Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: #0b1728;
--panel-2: #101f33;
--line: #263a52;
--text: #eef5ff;
--muted: #9fb0c5;
--cyan: #5ad1e6;
--amber: #ffc46b;
--green: #7ee2a8;
}
* {
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;
}
#layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 380px;
width: 100%;
height: 100vh;
height: 100dvh;
}
#viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 35%, #14253b 0%, #07111f 68%);
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.scene-heading {
position: absolute;
top: 20px;
left: 22px;
pointer-events: none;
text-shadow: 0 2px 12px #000;
}
.scene-heading h2 {
margin: 0 0 5px;
font-size: 19px;
font-weight: 700;
}
.scene-heading p {
margin: 0;
color: var(--muted);
font-size: 12px;
}
.scene-legend {
position: absolute;
left: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 9px 16px;
max-width: 520px;
padding: 10px 12px;
border: 1px solid rgba(90, 209, 230, 0.24);
border-radius: 10px;
background: rgba(7, 17, 31, 0.78);
color: var(--muted);
font-size: 11px;
pointer-events: none;
backdrop-filter: blur(8px);
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend-line {
width: 25px;
height: 2px;
background: var(--cyan);
}
#hint {
position: absolute;
right: 18px;
bottom: 22px;
color: var(--muted);
font-size: 11px;
pointer-events: none;
text-shadow: 0 2px 10px #000;
}
#loading {
position: absolute;
inset: 0;
display: grid;
place-items: center;
color: var(--muted);
font-size: 14px;
background: #07111f;
}
#panel {
overflow-y: auto;
padding: 22px;
border-left: 1px solid var(--line);
background:
linear-gradient(180deg, #0d1b2e 0%, var(--panel) 260px);
scrollbar-color: #35506e var(--panel);
}
#panel h1 {
margin: 0;
font-size: 23px;
line-height: 1.15;
}
.subtitle {
margin: 8px 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.section {
margin-top: 18px;
padding-top: 17px;
border-top: 1px solid var(--line);
}
.section-title {
margin: 0 0 11px;
color: #c8d6e8;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.temperature-readout {
display: flex;
align-items: baseline;
gap: 5px;
margin-bottom: 4px;
}
#temperatureValue {
font-size: 42px;
font-weight: 760;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.degree {
color: var(--muted);
font-size: 21px;
}
#deltaValue {
margin-left: auto;
color: var(--muted);
font-size: 12px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
margin: 12px 0 4px;
accent-color: var(--cyan);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
color: #71869f;
font-size: 10px;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 14px;
color: #bdcadd;
font-size: 12px;
}
.control-row strong {
color: var(--cyan);
font-variant-numeric: tabular-nums;
}
.microcopy {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.buttons {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 15px;
}
button {
appearance: none;
border: 1px solid #35506e;
border-radius: 8px;
padding: 8px 11px;
background: #13263d;
color: var(--text);
font: inherit;
font-size: 11px;
cursor: pointer;
}
button:hover {
border-color: var(--cyan);
background: #18304b;
}
button[aria-pressed="true"] {
border-color: var(--cyan);
color: var(--cyan);
}
.checkbox-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 13px;
color: #bdcadd;
font-size: 12px;
}
.checkbox-row input {
accent-color: var(--cyan);
}
.material-card {
margin-bottom: 9px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(16, 31, 51, 0.72);
}
.material-header {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 11px;
height: 11px;
flex: 0 0 auto;
border-radius: 3px;
box-shadow: 0 0 10px currentColor;
}
.material-name {
font-size: 12px;
font-weight: 700;
}
.coefficient {
margin-left: auto;
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.material-stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 11px;
}
.material-stat {
padding-left: 9px;
border-left: 2px solid #2b435e;
}
.material-stat span {
display: block;
color: var(--muted);
font-size: 10px;
}
.material-stat strong {
display: block;
margin-top: 2px;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.formula {
padding: 13px;
border: 1px solid #2d4661;
border-radius: 9px;
background: #0a1626;
color: #d9e8fa;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.6;
}
.formula small {
display: block;
margin-top: 5px;
color: var(--muted);
font-family: inherit;
font-size: 10px;
}
.notice {
margin-top: 12px;
padding: 10px 11px;
border: 1px solid;
border-radius: 8px;
font-size: 11px;
line-height: 1.45;
}
.notice.ok {
border-color: rgba(126, 226, 168, 0.32);
background: rgba(126, 226, 168, 0.07);
color: #b9f3d2;
}
.notice.warn {
border-color: rgba(255, 196, 107, 0.42);
background: rgba(255, 196, 107, 0.08);
color: #ffd89e;
}
.limits {
margin: 11px 0 0;
padding-left: 18px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.limits li {
margin-bottom: 7px;
}
.footnote {
margin: 16px 0 0;
color: #71869f;
font-size: 10px;
line-height: 1.5;
}
@media (max-width: 850px) {
#layout {
grid-template-columns: 1fr;
grid-template-rows: minmax(300px, 58vh) minmax(0, 42vh);
}
#panel {
padding: 17px;
border-top: 1px solid var(--line);
border-left: 0;
}
.scene-heading {
top: 13px;
left: 14px;
}
.scene-heading h2 {
font-size: 16px;
}
#hint {
display: none;
}
.scene-legend {
left: 12px;
bottom: 12px;
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="layout">
<main id="viewport">
<canvas id="scene" aria-label="Interactive 3D voxel thermal expansion comparison"></canvas>
<div class="scene-heading">
<h2>Voxel thermal-expansion specimens</h2>
<p>12 × 2 × 2 voxels · isotropic, unconstrained expansion</p>
</div>
<div class="scene-legend">
<span class="legend-item">
<span class="legend-line"></span>
20 °C reference envelope
</span>
<span class="legend-item">
All numeric readouts are unscaled
</span>
</div>
<div id="hint">Drag: rotate · Wheel: zoom · Shift-drag: pan</div>
<div id="loading">Loading Three.js…</div>
</main>
<aside id="panel">
<h1>Thermal Expansion Lab</h1>
<p class="subtitle">
Compare three voxel specimens at the same temperature. The cyan
wireframe shows each specimen at the 20 °C reference state.
</p>
<section class="section">
<h2 class="section-title">Temperature</h2>
<div class="temperature-readout">
<span id="temperatureValue">100</span>
<span class="degree">°C</span>
<span id="deltaValue">ΔT = +80 K</span>
</div>
<input
id="temperature"
type="range"
min="-20"
max="200"
step="1"
value="100"
aria-label="Temperature in degrees Celsius"
>
<div class="range-labels">
<span>−20 °C</span>
<span>20 °C reference</span>
<span>200 °C</span>
</div>
<div class="control-row">
<label for="gain">Display expansion magnification</label>
<strong id="gainValue">120×</strong>
</div>
<input
id="gain"
type="range"
min="1"
max="250"
step="1"
value="120"
aria-label="Display expansion magnification"
>
<div class="range-labels">
<span>1× = true scale</span>
<span>250×</span>
</div>
<p class="microcopy">
The gain magnifies only the displayed strain, equally in all three
dimensions. It does not affect the calculated lengths below.
</p>
<div class="buttons">
<button id="sweepButton" type="button" aria-pressed="false">
Sweep temperature
</button>
<button id="referenceButton" type="button">
Reset to 20 °C
</button>
<button id="cameraButton" type="button">
Reset camera
</button>
</div>
<label class="checkbox-row">
<input id="showReference" type="checkbox" checked>
Show 20 °C reference envelopes
</label>
</section>
<section class="section">
<h2 class="section-title">Material comparison</h2>
<article class="material-card">
<div class="material-header">
<span class="swatch" style="color:#91b4d9;background:#91b4d9"></span>
<span class="material-name">Aluminum 6061-T6</span>
<span class="coefficient">α = 23.6 × 10⁻⁶ K⁻¹</span>
</div>
<div class="material-stats">
<div class="material-stat">
<span>True ΔL</span>
<strong id="al-delta">+0.227 mm</strong>
</div>
<div class="material-stat">
<span>True length</span>
<strong id="al-length">120.227 mm</strong>
</div>
</div>
</article>
<article class="material-card">
<div class="material-header">
<span class="swatch" style="color:#d97742;background:#d97742"></span>
<span class="material-name">Copper C110</span>
<span class="coefficient">α = 16.9 × 10⁻⁶ K⁻¹</span>
</div>
<div class="material-stats">
<div class="material-stat">
<span>True ΔL</span>
<strong id="cu-delta">+0.162 mm</strong>
</div>
<div class="material-stat">
<span>True length</span>
<strong id="cu-length">120.162 mm</strong>
</div>
</div>
</article>
<article class="material-card">
<div class="material-header">
<span class="swatch" style="color:#70869d;background:#70869d"></span>
<span class="material-name">Steel AISI 1020</span>
<span class="coefficient">α = 12.0 × 10⁻⁶ K⁻¹</span>
</div>
<div class="material-stats">
<div class="material-stat">
<span>True ΔL</span>
<strong id="steel-delta">+0.115 mm</strong>
</div>
<div class="material-stat">
<span>True length</span>
<strong id="steel-length">120.115 mm</strong>
</div>
</div>
</article>
</section>
<section class="section">
<h2 class="section-title">Model and stated data</h2>
<div class="formula">
L(T) = L₀[1 + α(T − T₀)]
<small>
L₀ = 120.000 mm, T₀ = 20 °C.<br>
A temperature difference in °C has the same magnitude in K.
</small>
</div>
<p class="microcopy">
Nominal specimen: 120 × 20 × 20 mm. Coefficients are representative
constant values commonly quoted near room temperature, especially
over approximately 20–100 °C.
</p>
<div id="rangeNotice" class="notice ok">
Temperature is within the representative 20–100 °C coefficient band.
The calculation is still a first-order approximation.
</div>
<ul class="limits">
<li>
The law assumes constant α and neglects higher-order terms of
order (αΔT)².
</li>
<li>
Outside the stated 20–100 °C band, the results are extrapolations,
not certified property predictions.
</li>
<li>
Alloy, temper, texture and anisotropy can alter the effective
coefficient.
</li>
<li>
Phase transformations, constrained expansion, thermal stress and
creep are not modeled.
</li>
</ul>
<p class="footnote">
Engineering illustration only. Verify critical calculations using
temperature-dependent coefficient data from the material supplier.
</p>
</section>
</aside>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Constants and material data
// -------------------------------------------------------------------------
const REFERENCE_TEMPERATURE = 20;
const INITIAL_LENGTH_MM = 120;
const VOXEL_COUNT_X = 12;
const VOXEL_COUNT_Y = 2;
const VOXEL_COUNT_Z = 2;
// One nominal voxel is 10 mm in each direction.
const VOXEL_SIZE_MM = 10;
const WORLD_UNITS_PER_MM = 0.009;
const WORLD_VOXEL_SIZE = VOXEL_SIZE_MM * WORLD_UNITS_PER_MM;
const INITIAL_LENGTH_WORLD =
VOXEL_COUNT_X * WORLD_VOXEL_SIZE;
const INITIAL_SECTION_WORLD =
VOXEL_COUNT_Y * WORLD_VOXEL_SIZE;
const BASE_Y = 0.025;
const MATERIALS = [
{
key: "al",
name: "Aluminum 6061-T6",
shortName: "ALUMINUM 6061-T6",
alpha: 23.6e-6,
color: 0x91b4d9,
css: "#91b4d9",
rowZ: -1.22
},
{
key: "cu",
name: "Copper C110",
shortName: "COPPER C110",
alpha: 16.9e-6,
color: 0xd97742,
css: "#d97742",
rowZ: 0
},
{
key: "steel",
name: "Steel AISI 1020",
shortName: "STEEL AISI 1020",
alpha: 12.0e-6,
color: 0x70869d,
css: "#70869d",
rowZ: 1.22
}
];
// -------------------------------------------------------------------------
// DOM references
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const canvas = document.getElementById("scene");
const temperatureInput = document.getElementById("temperature");
const gainInput = document.getElementById("gain");
const temperatureValue = document.getElementById("temperatureValue");
const gainValue = document.getElementById("gainValue");
const deltaValue = document.getElementById("deltaValue");
const rangeNotice = document.getElementById("rangeNotice");
const showReference = document.getElementById("showReference");
const sweepButton = document.getElementById("sweepButton");
const referenceButton = document.getElementById("referenceButton");
const cameraButton = document.getElementById("cameraButton");
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 7, 15);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.05,
50
);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const hemisphere = new THREE.HemisphereLight(
0xc5dcff,
0x07101b,
1.65
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(3.8, 6.5, 4.8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.1;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x5fa8ff, 1.15);
fillLight.position.set(-4, 2.5, -3.5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffb66e, 0.9);
rimLight.position.set(0, 2, -5);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(24, 18),
new THREE.MeshStandardMaterial({
color: 0x0a1625,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = 0;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(
20,
40,
0x31506d,
0x193149
);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.33;
scene.add(grid);
const unitBoxGeometry = new THREE.BoxGeometry(1, 1, 1);
const unitEdgesGeometry = new THREE.EdgesGeometry(unitBoxGeometry);
const referenceGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(
INITIAL_LENGTH_WORLD,
INITIAL_SECTION_WORLD,
INITIAL_SECTION_WORLD
)
);
// -------------------------------------------------------------------------
// Scene labels generated with a canvas texture
// -------------------------------------------------------------------------
function roundedRectPath(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function createSceneLabel(entry) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 640;
labelCanvas.height = 180;
const context = labelCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.position.set(-1.43, 0.26, entry.rowZ);
sprite.scale.set(1.72, 0.48, 1);
sprite.renderOrder = 30;
scene.add(sprite);
entry.labelCanvas = labelCanvas;
entry.labelContext = context;
entry.labelTexture = texture;
entry.labelSprite = sprite;
}
function updateSceneLabel(entry, actualLengthMM) {
const context = entry.labelContext;
const width = entry.labelCanvas.width;
const height = entry.labelCanvas.height;
context.clearRect(0, 0, width, height);
roundedRectPath(context, 4, 4, width - 8, height - 8, 22);
context.fillStyle = "rgba(7, 17, 31, 0.88)";
context.fill();
context.strokeStyle = "rgba(255,255,255,0.14)";
context.lineWidth = 3;
context.stroke();
context.fillStyle = entry.css;
context.fillRect(19, 24, 10, height - 48);
context.textAlign = "left";
context.textBaseline = "middle";
context.fillStyle = "#eef5ff";
context.font = "700 35px system-ui, sans-serif";
context.fillText(entry.shortName, 48, 60);
context.fillStyle = "#9fb0c5";
context.font = "500 29px system-ui, sans-serif";
context.fillText(
`${actualLengthMM.toFixed(3)} mm at selected T`,
48,
116
);
entry.labelTexture.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Build voxel specimens
// -------------------------------------------------------------------------
for (const entry of MATERIALS) {
entry.group = new THREE.Group();
entry.group.position.set(0, BASE_Y, entry.rowZ);
scene.add(entry.group);
const voxelMaterial = new THREE.MeshStandardMaterial({
color: entry.color,
roughness: entry.key === "cu" ? 0.3 : 0.43,
metalness: entry.key === "cu" ? 0.68 : 0.42
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x07111f,
transparent: true,
opacity: 0.72
});
entry.voxels = [];
for (let x = 0; x < VOXEL_COUNT_X; x++) {
for (let y = 0; y < VOXEL_COUNT_Y; y++) {
for (let z = 0; z < VOXEL_COUNT_Z; z++) {
const mesh = new THREE.Mesh(
unitBoxGeometry,
voxelMaterial
);
mesh.castShadow = true;
mesh.receiveShadow = true;
const edges = new THREE.LineSegments(
unitEdgesGeometry,
edgeMaterial
);
mesh.add(edges);
entry.group.add(mesh);
entry.voxels.push({
mesh,
edges,
x,
y,
z
});
}
}
}
entry.referenceOutline = new THREE.LineSegments(
referenceGeometry,
new THREE.LineBasicMaterial({
color: 0x5ad1e6,
transparent: true,
opacity: 0.42
})
);
entry.referenceOutline.position.set(
0,
BASE_Y + INITIAL_SECTION_WORLD / 2,
entry.rowZ
);
entry.referenceOutline.renderOrder = 4;
scene.add(entry.referenceOutline);
createSceneLabel(entry);
}
// -------------------------------------------------------------------------
// Update specimen geometry and numerical results
// -------------------------------------------------------------------------
function formatSigned(value, digits = 3) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(digits)} mm`;
}
function updateScene() {
const temperature = Number(temperatureInput.value);
const gain = Number(gainInput.value);
const deltaT = temperature - REFERENCE_TEMPERATURE;
temperatureValue.textContent = String(temperature);
gainValue.textContent = `${gain}×`;
deltaValue.textContent =
`${deltaT >= 0 ? "+" : "−"}${Math.abs(deltaT)} K`;
for (const entry of MATERIALS) {
const strain = entry.alpha * deltaT;
const actualLengthMM =
INITIAL_LENGTH_MM * (1 + strain);
const actualDeltaMM =
actualLengthMM - INITIAL_LENGTH_MM;
// At gain = 1 this is the physical strain exactly.
// Larger gains magnify the visible deformation only.
const displayScale = 1 + strain * gain;
const cubeSize =
WORLD_VOXEL_SIZE * displayScale;
const lengthWorld =
VOXEL_COUNT_X * cubeSize;
for (const voxel of entry.voxels) {
const x =
-lengthWorld / 2 +
(voxel.x + 0.5) * cubeSize;
const y =
(voxel.y + 0.5) * cubeSize;
const z =
(voxel.z + 0.5) * cubeSize;
voxel.mesh.position.set(x, y, z);
voxel.mesh.scale.setScalar(cubeSize);
voxel.edges.scale.setScalar(cubeSize);
}
document.getElementById(`${entry.key}-delta`).textContent =
formatSigned(actualDeltaMM);
document.getElementById(`${entry.key}-length`).textContent =
`${actualLengthMM.toFixed(3)} mm`;
updateSceneLabel(entry, actualLengthMM);
}
const withinCoefficientBand =
temperature >= 20 && temperature <= 100;
if (withinCoefficientBand) {
rangeNotice.className = "notice ok";
rangeNotice.textContent =
"Temperature is within the representative 20–100 °C coefficient band. " +
"The calculation is still a first-order approximation.";
} else {
rangeNotice.className = "notice warn";
rangeNotice.textContent =
"Temperature is outside the representative 20–100 °C coefficient band. " +
"The displayed values extrapolate a constant room-temperature α and " +
"should not be treated as certified material data.";
}
}
// -------------------------------------------------------------------------
// Direct camera controls: rotate, zoom and pan
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0.05, 0.28, 0),
radius: 4.65,
theta: 0.53,
phi: 1.08
};
const initialCameraState = {
target: new THREE.Vector3(0.05, 0.28, 0),
radius: 4.65,
theta: 0.53,
phi: 1.08
};
function updateCamera() {
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi,
0.42,
1.48
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.copy(initialCameraState.target);
cameraState.radius = initialCameraState.radius;
cameraState.theta = initialCameraState.theta;
cameraState.phi = initialCameraState.phi;
updateCamera();
}
let dragging = false;
let dragMode = "rotate";
let lastPointerX = 0;
let lastPointerY = 0;
let activePointerId = null;
const forwardVector = new THREE.Vector3();
const rightVector = new THREE.Vector3();
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0 && event.button !== 2) return;
event.preventDefault();
dragging = true;
dragMode = event.shiftKey ? "pan" : "rotate";
lastPointerX = event.clientX;
lastPointerY = event.clientY;
activePointerId = event.pointerId;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", event => {
if (!dragging || event.pointerId !== activePointerId) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (dragMode === "rotate") {
cameraState.theta -= dx * 0.006;
cameraState.phi -= dy * 0.005;
} else {
camera.getWorldDirection(forwardVector);
rightVector
.crossVectors(forwardVector, camera.up)
.normalize();
const panScale = cameraState.radius * 0.00125;
cameraState.target.addScaledVector(
rightVector,
-dx * panScale
);
cameraState.target.y += dy * panScale;
}
updateCamera();
});
function endPointerDrag(event) {
if (!dragging) return;
if (
activePointerId !== null &&
canvas.hasPointerCapture(activePointerId)
) {
canvas.releasePointerCapture(activePointerId);
}
dragging = false;
activePointerId = null;
canvas.classList.remove("dragging");
}
canvas.addEventListener("pointerup", endPointerDrag);
canvas.addEventListener("pointercancel", endPointerDrag);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius *= Math.exp(event.deltaY * 0.001);
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
2.5,
10
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", event => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
function stopSweep() {
if (!sweeping) return;
sweeping = false;
sweepButton.textContent = "Sweep temperature";
sweepButton.setAttribute("aria-pressed", "false");
}
temperatureInput.addEventListener("input", () => {
stopSweep();
updateScene();
});
gainInput.addEventListener("input", updateScene);
showReference.addEventListener("change", () => {
for (const entry of MATERIALS) {
entry.referenceOutline.visible = showReference.checked;
}
});
referenceButton.addEventListener("click", () => {
stopSweep();
temperatureInput.value = "20";
updateScene();
});
cameraButton.addEventListener("click", resetCamera);
let sweeping = false;
let sweepStartTime = 0;
sweepButton.addEventListener("click", () => {
if (sweeping) {
stopSweep();
} else {
sweeping = true;
sweepStartTime = performance.now();
sweepButton.textContent = "Pause sweep";
sweepButton.setAttribute("aria-pressed", "true");
}
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
resetCamera();
updateScene();
document.getElementById("loading").remove();
function animate(now) {
requestAnimationFrame(animate);
if (sweeping) {
const elapsed = now - sweepStartTime;
const halfCycle = 18000;
const phase = (elapsed % (halfCycle * 2)) / halfCycle;
const triangle = phase <= 1 ? phase : 2 - phase;
const minimum = Number(temperatureInput.min);
const maximum = Number(temperatureInput.max);
const temperature = Math.round(
minimum + (maximum - minimum) * triangle
);
if (temperature !== Number(temperatureInput.value)) {
temperatureInput.value = String(temperature);
updateScene();
}
}
updateCamera();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```