Prompt specification
Build or customize the Three.js 3D scene "Heated rod with temperature markers" 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: Heated rod with temperature markers
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/heated-rod-with-temperature-markers-x1473
Scene Description Prompt:
Heat one end of a low-poly rod; show one-dimensional conduction with temperature markers distinct from heat-flow indicators. Three.js from cdnjs, one HTML file.
Working Three.js Starter (heated-rod-with-temperature-markers-x1473):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>1D Thermal Conduction</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: rgba(10, 21, 35, 0.82);
--border: rgba(148, 179, 210, 0.18);
--text: #edf7ff;
--muted: #91a8bd;
--cyan: #38c6ef;
--yellow: #ffd166;
--orange: #ff7a2f;
--red: #ff493d;
}
* {
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;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 35%, rgba(2, 7, 14, 0.45) 100%);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--border);
border-radius: 16px;
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(16px);
}
header {
position: absolute;
top: 24px;
left: 26px;
max-width: 520px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
color: var(--orange);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 3px;
border-radius: 4px;
background: var(--orange);
box-shadow: 0 0 16px rgba(255, 122, 47, 0.65);
}
h1 {
margin: 9px 0 7px;
font-size: clamp(25px, 3.2vw, 42px);
line-height: 1.05;
letter-spacing: -0.04em;
}
.subtitle {
margin: 0;
max-width: 460px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status-strip {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 16px;
}
.status-chip {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 29px;
padding: 6px 10px;
border: 1px solid rgba(148, 179, 210, 0.14);
border-radius: 999px;
background: rgba(7, 17, 29, 0.68);
color: #b9cad8;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-chip strong {
color: white;
font-variant-numeric: tabular-nums;
}
.status-dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 10px var(--orange);
}
.legend {
position: absolute;
top: 24px;
right: 24px;
width: 255px;
padding: 17px;
}
.legend-title {
margin-bottom: 13px;
color: #cbd9e5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 11px;
margin-top: 12px;
color: #dce9f3;
font-size: 12px;
}
.legend-row small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.legend-symbol {
position: relative;
flex: 0 0 38px;
width: 38px;
height: 28px;
}
.temp-symbol {
position: absolute;
top: 5px;
left: 10px;
width: 18px;
height: 18px;
border: 2px solid white;
border-radius: 50%;
background: linear-gradient(135deg, var(--cyan), var(--yellow), var(--red));
box-shadow: 0 0 15px rgba(56, 198, 239, 0.35);
}
.temp-symbol::after {
content: "";
position: absolute;
top: 18px;
left: 7px;
width: 2px;
height: 9px;
background: #d7e6f0;
}
.flow-symbol {
position: absolute;
top: 7px;
left: 2px;
width: 31px;
height: 8px;
border-radius: 2px;
background: var(--orange);
box-shadow: 0 0 13px rgba(255, 122, 47, 0.5);
}
.flow-symbol::after {
content: "";
position: absolute;
top: -5px;
right: -2px;
border-top: 9px solid transparent;
border-bottom: 9px solid transparent;
border-left: 10px solid var(--orange);
}
.temperature-scale {
margin-top: 17px;
padding-top: 15px;
border-top: 1px solid var(--border);
}
.scale-bar {
height: 8px;
border-radius: 999px;
background: linear-gradient(
90deg,
var(--cyan),
#77dfd1 28%,
var(--yellow) 62%,
var(--orange) 82%,
var(--red)
);
box-shadow: 0 0 18px rgba(255, 122, 47, 0.15);
}
.scale-labels {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.controls {
position: absolute;
bottom: 24px;
left: 24px;
width: min(430px, calc(100vw - 48px));
padding: 16px;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
button,
select {
min-height: 38px;
border: 1px solid rgba(148, 179, 210, 0.2);
border-radius: 10px;
background: rgba(19, 37, 56, 0.78);
color: #dceaf4;
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
text-transform: uppercase;
}
button {
flex: 1 1 auto;
padding: 0 14px;
cursor: pointer;
transition: 160ms ease;
}
button:hover,
select:hover {
border-color: rgba(56, 198, 239, 0.55);
background: rgba(27, 52, 73, 0.9);
transform: translateY(-1px);
}
button.active {
border-color: rgba(255, 122, 47, 0.7);
background: linear-gradient(
180deg,
rgba(255, 122, 47, 0.24),
rgba(255, 122, 47, 0.09)
);
color: #ffd1b3;
box-shadow: inset 0 0 18px rgba(255, 122, 47, 0.08);
}
button:focus-visible,
select:focus-visible,
input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
select {
padding: 0 30px 0 11px;
cursor: pointer;
}
.control-grid {
display: grid;
grid-template-columns: 1fr 120px;
gap: 16px;
align-items: end;
margin-top: 14px;
padding-top: 14px;
border-top: 1px solid var(--border);
}
.control-label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.control-label output {
color: white;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
accent-color: var(--orange);
cursor: pointer;
}
.equation {
position: absolute;
right: 24px;
bottom: 24px;
width: min(350px, calc(100vw - 48px));
padding: 16px 18px;
}
.equation-title {
color: var(--cyan);
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
font-style: italic;
}
.equation p {
margin: 8px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.equation strong {
color: #dfeaf3;
font-weight: 700;
}
.hint {
position: absolute;
right: 25px;
bottom: 174px;
color: rgba(177, 198, 214, 0.62);
font-size: 10px;
letter-spacing: 0.05em;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: var(--bg);
color: var(--muted);
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
header {
top: 18px;
left: 18px;
right: 18px;
}
.subtitle {
max-width: 390px;
}
.legend {
top: auto;
right: 18px;
bottom: 190px;
width: 210px;
}
.equation {
display: none;
}
.hint {
right: 18px;
bottom: 174px;
display: none;
}
.controls {
left: 18px;
bottom: 18px;
}
}
@media (max-width: 560px) {
.legend {
display: none;
}
h1 {
font-size: 27px;
}
.status-strip {
gap: 5px;
}
.status-chip {
font-size: 9px;
}
.control-grid {
grid-template-columns: 1fr 96px;
gap: 10px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<div class="hud">
<header>
<div class="eyebrow">Thermal line experiment</div>
<h1>One-dimensional conduction</h1>
<p class="subtitle">
Heat enters the left end of a faceted rod and diffuses toward the
ambient end. Point-temperature samples sit above the rod; directional
heat-flow chevrons travel below it.
</p>
<div class="status-strip">
<div class="status-chip">
<span class="status-dot"></span>
<span id="stateText">Conducting</span>
</div>
<div class="status-chip">
ΔT <strong id="deltaText">160°C</strong>
</div>
<div class="status-chip">
Sim time <strong id="timeText">0.0 s</strong>
</div>
</div>
</header>
<aside class="legend panel">
<div class="legend-title">Reading the visualization</div>
<div class="legend-row">
<div class="legend-symbol"><span class="temp-symbol"></span></div>
<div>
Temperature marker
<small>Colored puck + numeric sample</small>
</div>
</div>
<div class="legend-row">
<div class="legend-symbol"><span class="flow-symbol"></span></div>
<div>
Heat-flow indicator
<small>Moving orange chevron</small>
</div>
</div>
<div class="temperature-scale">
<div class="scale-bar"></div>
<div class="scale-labels">
<span id="coldLabel">20°C</span>
<span>Temperature</span>
<span id="hotLabel">180°C</span>
</div>
</div>
</aside>
<section class="controls panel">
<div class="button-row">
<button id="heaterButton" class="active">Heater on</button>
<button id="playButton">Pause</button>
<button id="resetButton">Reset</button>
<select id="speedSelect" aria-label="Simulation speed">
<option value="0.5">0.5× speed</option>
<option value="1" selected>1× speed</option>
<option value="2">2× speed</option>
<option value="4">4× speed</option>
</select>
</div>
<div class="control-grid">
<div>
<label class="control-label" for="setpoint">
Heater setpoint
<output id="setpointValue">180°C</output>
</label>
<input
id="setpoint"
type="range"
min="100"
max="220"
step="5"
value="180"
>
</div>
<div>
<label class="control-label" for="speedSelectCompact">
Boundary
<output>20°C</output>
</label>
<div class="scale-labels">
<span>x = 0</span>
<span>x = L</span>
</div>
</div>
</div>
</section>
<aside class="equation panel">
<div class="equation-title">∂T/∂t = α ∂²T/∂x²</div>
<p>
The segmented rod is solved with an explicit finite-difference
method. <strong>Colored pucks</strong> report local temperature;
<strong>orange arrows</strong> show the direction and relative
strength of heat flux.
</p>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
</div>
<div id="loading">Initializing thermal model…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and basic state
// -------------------------------------------------------------------------
const container = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const heaterButton = document.querySelector("#heaterButton");
const playButton = document.querySelector("#playButton");
const resetButton = document.querySelector("#resetButton");
const speedSelect = document.querySelector("#speedSelect");
const setpointSlider = document.querySelector("#setpoint");
const setpointValue = document.querySelector("#setpointValue");
const stateText = document.querySelector("#stateText");
const deltaText = document.querySelector("#deltaText");
const timeText = document.querySelector("#timeText");
const hotLabel = document.querySelector("#hotLabel");
const AMBIENT = 20;
const N = 23;
const LENGTH = 8;
const RADIUS = 0.52;
const DX = LENGTH / (N - 1);
let heaterOn = true;
let running = true;
let setpoint = 180;
let simulationSpeed = 1;
let simulationTime = 0;
let simulationAccumulator = 0;
const temperatures = new Float32Array(N);
const nextTemperatures = new Float32Array(N);
temperatures.fill(AMBIENT);
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lights
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 13, 31);
const camera = new THREE.PerspectiveCamera(
42,
container.clientWidth / container.clientHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.2, 0);
const cameraState = {
radius: 12.5,
theta: 0.67,
phi: 1.13
};
let cameraHasBeenMoved = false;
function applyCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
applyCamera();
const hemisphere = new THREE.HemisphereLight(0xc8e8ff, 0x07101b, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(3.5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3b9eff, 1.5);
rimLight.position.set(-7, 3, -6);
scene.add(rimLight);
const heaterLight = new THREE.PointLight(0xff5b28, 18, 5, 2);
heaterLight.position.set(-3.85, 0.65, 1.1);
scene.add(heaterLight);
const coldLight = new THREE.PointLight(0x20b9ef, 4, 4, 2);
coldLight.position.set(3.9, 0.2, 0.8);
scene.add(coldLight);
// -------------------------------------------------------------------------
// Ground, platform, and grid
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 32),
new THREE.MeshStandardMaterial({
color: 0x07111c,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.58;
floor.receiveShadow = true;
scene.add(floor);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(9.35, 0.18, 2.05),
new THREE.MeshStandardMaterial({
color: 0x101e2b,
roughness: 0.62,
metalness: 0.3
})
);
platform.position.y = -1.39;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformEdge = new THREE.LineSegments(
new THREE.EdgesGeometry(platform.geometry),
new THREE.LineBasicMaterial({
color: 0x345069,
transparent: true,
opacity: 0.55
})
);
platformEdge.position.copy(platform.position);
scene.add(platformEdge);
const grid = new THREE.GridHelper(26, 52, 0x254158, 0x102637);
grid.position.y = -1.57;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// -------------------------------------------------------------------------
// Utility: temperature colors and canvas-texture labels
// -------------------------------------------------------------------------
const coldColor = new THREE.Color("#35c5ef");
const warmColor = new THREE.Color("#ffd166");
const hotColor = new THREE.Color("#ff493d");
function temperatureColor(value, target = new THREE.Color()) {
const span = Math.max(1, setpoint - AMBIENT);
const t = THREE.MathUtils.clamp((value - AMBIENT) / span, 0, 1);
if (t < 0.52) {
return target
.copy(coldColor)
.lerp(warmColor, t / 0.52);
}
return target
.copy(warmColor)
.lerp(hotColor, (t - 0.52) / 0.48);
}
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width * 0.5, height * 0.5);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function createLabel(initialText, scale = [1, 0.33], accent = "#74d9ff") {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 104;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(scale[0], scale[1], 1);
sprite.renderOrder = 20;
let text = initialText;
let color = accent;
function paint() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRectPath(ctx, 5, 5, 310, 94, 22);
ctx.fillStyle = "rgba(7, 17, 29, 0.88)";
ctx.fill();
ctx.lineWidth = 3;
ctx.strokeStyle = color;
ctx.stroke();
roundedRectPath(ctx, 17, 19, 7, 66, 4);
ctx.fillStyle = color;
ctx.fill();
ctx.fillStyle = "#eff8ff";
ctx.font = "700 39px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 166, 53);
texture.needsUpdate = true;
}
sprite.userData.setText = (nextText, nextColor = color) => {
text = nextText;
color = nextColor;
paint();
};
paint();
return sprite;
}
function createGlowTexture(color) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext("2d");
const gradient = ctx.createRadialGradient(64, 64, 2, 64, 64, 64);
gradient.addColorStop(0, color);
gradient.addColorStop(0.22, color);
gradient.addColorStop(0.58, "rgba(0,0,0,0)");
gradient.addColorStop(1, "rgba(0,0,0,0)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
// -------------------------------------------------------------------------
// Low-poly, segmented rod
// -------------------------------------------------------------------------
const rodGroup = new THREE.Group();
scene.add(rodGroup);
const segmentLength = DX * 0.92;
const rodGeometry = new THREE.CylinderGeometry(
RADIUS,
RADIUS,
segmentLength,
8,
1,
false
);
rodGeometry.rotateZ(Math.PI / 2);
const rodEdges = new THREE.EdgesGeometry(rodGeometry, 20);
const rodEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x071019,
transparent: true,
opacity: 0.42
});
const rodSegments = [];
for (let i = 0; i < N; i++) {
const material = new THREE.MeshStandardMaterial({
color: coldColor.clone(),
emissive: 0x000000,
emissiveIntensity: 0,
roughness: 0.56,
metalness: 0.12,
flatShading: true
});
const mesh = new THREE.Mesh(rodGeometry, material);
mesh.position.set(-LENGTH / 2 + i * DX, 0, 0);
mesh.castShadow = true;
mesh.receiveShadow = true;
const edges = new THREE.LineSegments(rodEdges, rodEdgeMaterial);
mesh.add(edges);
rodGroup.add(mesh);
rodSegments.push(mesh);
}
const endCapGeometry = new THREE.CylinderGeometry(
RADIUS * 1.015,
RADIUS * 1.015,
0.075,
8,
1,
false
);
endCapGeometry.rotateZ(Math.PI / 2);
const hotCapMaterial = new THREE.MeshStandardMaterial({
color: 0xff493d,
emissive: 0xff2a18,
emissiveIntensity: 0.45,
roughness: 0.48,
metalness: 0.15,
flatShading: true
});
const coldCapMaterial = new THREE.MeshStandardMaterial({
color: 0x35c5ef,
emissive: 0x0b779b,
emissiveIntensity: 0.2,
roughness: 0.5,
metalness: 0.15,
flatShading: true
});
const hotCap = new THREE.Mesh(endCapGeometry, hotCapMaterial);
hotCap.position.x = -LENGTH / 2 - 0.018;
hotCap.castShadow = true;
rodGroup.add(hotCap);
const coldCap = new THREE.Mesh(endCapGeometry, coldCapMaterial);
coldCap.position.x = LENGTH / 2 + 0.018;
coldCap.castShadow = true;
rodGroup.add(coldCap);
// -------------------------------------------------------------------------
// Heater and ambient sink
// -------------------------------------------------------------------------
const heaterGroup = new THREE.Group();
scene.add(heaterGroup);
const heaterMaterial = new THREE.MeshStandardMaterial({
color: 0xff642d,
emissive: 0xff3b13,
emissiveIntensity: 1.35,
roughness: 0.36,
metalness: 0.35
});
const heaterRingGeometry = new THREE.TorusGeometry(
RADIUS + 0.105,
0.045,
6,
18
);
for (let i = 0; i < 5; i++) {
const ring = new THREE.Mesh(heaterRingGeometry, heaterMaterial);
ring.rotation.y = Math.PI / 2;
ring.position.x = -4.05 + i * 0.13;
ring.castShadow = true;
heaterGroup.add(ring);
}
const heaterGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: createGlowTexture("rgba(255,83,30,0.85)"),
transparent: true,
opacity: 0.38,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
heaterGlow.position.set(-3.92, 0.03, -0.05);
heaterGlow.scale.set(2.45, 2.45, 1);
heaterGroup.add(heaterGlow);
const coldRingMaterial = new THREE.MeshStandardMaterial({
color: 0x35c5ef,
emissive: 0x0b88b5,
emissiveIntensity: 0.7,
roughness: 0.35,
metalness: 0.35
});
const coldRingGeometry = new THREE.TorusGeometry(
RADIUS + 0.07,
0.035,
6,
18
);
for (let i = 0; i < 2; i++) {
const ring = new THREE.Mesh(coldRingGeometry, coldRingMaterial);
ring.rotation.y = Math.PI / 2;
ring.position.x = LENGTH / 2 + 0.08 + i * 0.11;
scene.add(ring);
}
const coldGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: createGlowTexture("rgba(35,185,235,0.6)"),
transparent: true,
opacity: 0.2,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
coldGlow.position.set(3.9, 0.02, -0.05);
coldGlow.scale.set(1.8, 1.8, 1);
scene.add(coldGlow);
// -------------------------------------------------------------------------
// Temperature markers above the rod
// -------------------------------------------------------------------------
const markerGroup = new THREE.Group();
scene.add(markerGroup);
const markerIndices = [];
for (let i = 2; i < N - 2; i += 3) {
markerIndices.push(i);
}
const stemGeometry = new THREE.CylinderGeometry(
0.022,
0.022,
0.58,
6
);
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x9eb3c5,
roughness: 0.5,
metalness: 0.55
});
const capGeometry = new THREE.CylinderGeometry(
0.15,
0.15,
0.065,
16
);
const markerRingGeometry = new THREE.TorusGeometry(
0.19,
0.018,
6,
24
);
const markerRingMaterial = new THREE.MeshBasicMaterial({
color: 0xeaf7ff,
transparent: true,
opacity: 0.9
});
const temperatureMarkers = [];
for (const index of markerIndices) {
const group = new THREE.Group();
group.position.x = -LENGTH / 2 + index * DX;
const stem = new THREE.Mesh(stemGeometry, stemMaterial);
stem.position.y = 0.81;
stem.castShadow = true;
group.add(stem);
const capMaterial = new THREE.MeshStandardMaterial({
color: coldColor.clone(),
emissive: coldColor.clone(),
emissiveIntensity: 0.22,
roughness: 0.32,
metalness: 0.16
});
const cap = new THREE.Mesh(capGeometry, capMaterial);
cap.position.y = 1.12;
cap.castShadow = true;
group.add(cap);
const ring = new THREE.Mesh(markerRingGeometry, markerRingMaterial);
ring.rotation.x = Math.PI / 2;
ring.position.y = 1.15;
group.add(ring);
const label = createLabel("20°C", [0.79, 0.26], "#35c5ef");
label.position.set(0, 1.61, 0);
group.add(label);
markerGroup.add(group);
temperatureMarkers.push({
index,
group,
capMaterial,
label
});
}
// -------------------------------------------------------------------------
// Heat-flow indicators below the rod
// -------------------------------------------------------------------------
const arrowGroup = new THREE.Group();
scene.add(arrowGroup);
const arrowShaftGeometry = new THREE.BoxGeometry(0.32, 0.045, 0.055);
const arrowHeadGeometry = new THREE.ConeGeometry(0.115, 0.24, 6);
const heatArrows = [];
for (let i = 0; i < 10; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xff7a2f,
transparent: true,
opacity: 0,
depthWrite: false
});
const group = new THREE.Group();
group.position.y = -0.92;
group.position.z = 0.16;
const shaft = new THREE.Mesh(arrowShaftGeometry, material);
group.add(shaft);
const head = new THREE.Mesh(arrowHeadGeometry, material);
head.rotation.z = -Math.PI / 2;
head.position.x = 0.25;
group.add(head);
group.userData.phase = i / 10;
group.userData.material = material;
arrowGroup.add(group);
heatArrows.push(group);
}
const flowLabel = createLabel(
"HEAT FLOW →",
[1.42, 0.31],
"#ff7a2f"
);
flowLabel.position.set(0, -0.7, 0.35);
scene.add(flowLabel);
const heaterLabel = createLabel(
"HEATER 180°C",
[1.25, 0.31],
"#ff7a2f"
);
heaterLabel.position.set(-3.35, -0.55, 0.35);
scene.add(heaterLabel);
const ambientLabel = createLabel(
"AMBIENT 20°C",
[1.25, 0.31],
"#35c5ef"
);
ambientLabel.position.set(3.38, -0.55, 0.35);
scene.add(ambientLabel);
const axisLabelLeft = createLabel("x = 0", [0.66, 0.25], "#ff7a2f");
axisLabelLeft.position.set(-4.12, -1.18, 0.64);
scene.add(axisLabelLeft);
const axisLabelRight = createLabel("x = L", [0.66, 0.25], "#35c5ef");
axisLabelRight.position.set(4.12, -1.18, 0.64);
scene.add(axisLabelRight);
// -------------------------------------------------------------------------
// Numerical model: explicit 1D heat equation
// -------------------------------------------------------------------------
function stepSimulation() {
const diffusionNumber = 0.21;
nextTemperatures[0] = heaterOn ? setpoint : AMBIENT;
nextTemperatures[N - 1] = AMBIENT;
for (let i = 1; i < N - 1; i++) {
const laplacian =
temperatures[i - 1] -
2 * temperatures[i] +
temperatures[i + 1];
nextTemperatures[i] =
temperatures[i] + diffusionNumber * laplacian;
}
temperatures.set(nextTemperatures);
simulationTime += 0.025;
}
function sampleTemperature(x) {
const position = THREE.MathUtils.clamp(
(x + LENGTH / 2) / DX,
0,
N - 1
);
const low = Math.floor(position);
const high = Math.min(N - 1, low + 1);
const fraction = position - low;
return THREE.MathUtils.lerp(
temperatures[low],
temperatures[high],
fraction
);
}
function gradientAt(x) {
const position = THREE.MathUtils.clamp(
(x + LENGTH / 2) / DX,
0,
N - 2
);
const index = Math.floor(position);
return Math.max(
0,
(temperatures[index] - temperatures[index + 1]) / DX
);
}
function steadyStateError() {
const hotBoundary = heaterOn ? setpoint : AMBIENT;
let maximumError = 0;
for (let i = 0; i < N; i++) {
const expected =
AMBIENT +
(hotBoundary - AMBIENT) * (1 - i / (N - 1));
maximumError = Math.max(
maximumError,
Math.abs(temperatures[i] - expected)
);
}
return maximumError;
}
// -------------------------------------------------------------------------
// Visual updates
// -------------------------------------------------------------------------
const temporaryColor = new THREE.Color();
function updateRod() {
for (let i = 0; i < N; i++) {
const temperature = temperatures[i];
temperatureColor(temperature, temporaryColor);
const material = rodSegments[i].material;
material.color.copy(temporaryColor);
material.emissive.copy(temporaryColor);
const normalized = THREE.MathUtils.clamp(
(temperature - AMBIENT) / Math.max(1, setpoint - AMBIENT),
0,
1
);
material.emissiveIntensity = 0.025 + normalized * 0.16;
}
temperatureColor(temperatures[0], temporaryColor);
hotCapMaterial.color.copy(temporaryColor);
hotCapMaterial.emissive.copy(temporaryColor);
}
function updateMarkers() {
for (const marker of temperatureMarkers) {
const temperature = temperatures[marker.index];
temperatureColor(temperature, temporaryColor);
marker.capMaterial.color.copy(temporaryColor);
marker.capMaterial.emissive.copy(temporaryColor);
const normalized = THREE.MathUtils.clamp(
(temperature - AMBIENT) / Math.max(1, setpoint - AMBIENT),
0,
1
);
marker.capMaterial.emissiveIntensity = 0.16 + normalized * 0.38;
marker.label.userData.setText(
`${Math.round(temperature)}°C`,
temporaryColor.getStyle()
);
}
}
function updateFlowArrows() {
const travelWidth = LENGTH - 0.35;
let visibleArrowCount = 0;
for (const arrow of heatArrows) {
const phase = arrow.userData.phase;
const travel = (simulationTime * 0.19 + phase) % 1;
const x = -travelWidth / 2 + travel * travelWidth;
arrow.position.x = x;
const gradient = gradientAt(x);
const strength = THREE.MathUtils.clamp(gradient / 75, 0, 1);
const material = arrow.userData.material;
material.opacity = heaterOn
? 0.13 + strength * 0.82
: 0;
arrow.scale.setScalar(0.74 + strength * 0.3);
if (heaterOn && gradient > 0.35) {
visibleArrowCount++;
}
}
flowLabel.material.opacity = visibleArrowCount ? 1 : 0.28;
}
function updateSourceVisuals(elapsed) {
const pulse = 0.5 + 0.5 * Math.sin(elapsed * 4.2);
heaterMaterial.emissiveIntensity = heaterOn
? 0.85 + pulse * 0.7
: 0.05;
heaterMaterial.color.set(heaterOn ? 0xff642d : 0x49342b);
heaterLight.intensity = heaterOn ? 13 + pulse * 8 : 0.25;
heaterGlow.material.opacity = heaterOn ? 0.25 + pulse * 0.2 : 0.04;
hotCapMaterial.emissiveIntensity = heaterOn
? 0.28 + pulse * 0.28
: 0.04;
}
function updateUI() {
const delta = temperatures[0] - temperatures[N - 1];
const error = steadyStateError();
deltaText.textContent = `${Math.round(delta)}°C`;
timeText.textContent = `${simulationTime.toFixed(1)} s`;
if (!running) {
stateText.textContent = "Paused";
} else if (!heaterOn) {
stateText.textContent = "Cooling";
} else if (error < 0.55) {
stateText.textContent = "Steady state";
} else {
stateText.textContent = "Conducting";
}
}
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
heaterButton.addEventListener("click", () => {
heaterOn = !heaterOn;
heaterButton.classList.toggle("active", heaterOn);
heaterButton.textContent = heaterOn ? "Heater on" : "Heater off";
heaterLabel.userData.setText(
heaterOn ? `HEATER ${setpoint}°C` : "HEATER OFF",
heaterOn ? "#ff7a2f" : "#66798a"
);
});
playButton.addEventListener("click", () => {
running = !running;
playButton.textContent = running ? "Pause" : "Play";
});
resetButton.addEventListener("click", () => {
temperatures.fill(AMBIENT);
simulationTime = 0;
simulationAccumulator = 0;
heaterOn = true;
setpoint = 180;
setpointSlider.value = "180";
setpointValue.textContent = "180°C";
hotLabel.textContent = "180°C";
heaterButton.classList.add("active");
heaterButton.textContent = "Heater on";
playButton.textContent = "Pause";
running = true;
heaterLabel.userData.setText("HEATER 180°C", "#ff7a2f");
updateRod();
updateMarkers();
});
speedSelect.addEventListener("change", () => {
simulationSpeed = Number(speedSelect.value);
});
setpointSlider.addEventListener("input", () => {
setpoint = Number(setpointSlider.value);
setpointValue.textContent = `${setpoint}°C`;
hotLabel.textContent = `${setpoint}°C`;
heaterLabel.userData.setText(
heaterOn ? `HEATER ${setpoint}°C` : "HEATER OFF",
heaterOn ? "#ff7a2f" : "#66798a"
);
if (heaterOn) {
temperatures[0] = setpoint;
}
});
// -------------------------------------------------------------------------
// Camera controls implemented directly, without examples/addons
// -------------------------------------------------------------------------
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);
cameraHasBeenMoved = true;
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.005,
0.42,
1.46
);
applyCamera();
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraHasBeenMoved = true;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7,
28
);
applyCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = 0.67;
cameraState.phi = 1.13;
const aspect = container.clientWidth / container.clientHeight;
cameraState.radius = THREE.MathUtils.clamp(
9.4 /
(2 *
Math.tan(THREE.MathUtils.degToRad(camera.fov * 0.5)) *
aspect),
11,
25
);
cameraHasBeenMoved = false;
applyCamera();
}
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
const width = container.clientWidth;
const height = container.clientHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (!cameraHasBeenMoved) {
const aspect = width / height;
cameraState.radius = THREE.MathUtils.clamp(
9.4 /
(2 *
Math.tan(THREE.MathUtils.degToRad(camera.fov * 0.5)) *
aspect),
11,
25
);
applyCamera();
}
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
let visualAccumulator = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (running) {
simulationAccumulator += delta * simulationSpeed;
while (simulationAccumulator >= 0.025) {
stepSimulation();
simulationAccumulator -= 0.025;
}
}
visualAccumulator += delta;
if (visualAccumulator >= 0.08) {
updateRod();
updateMarkers();
updateUI();
visualAccumulator = 0;
}
updateFlowArrows();
updateSourceVisuals(clock.elapsedTime);
renderer.render(scene, camera);
}
updateRod();
updateMarkers();
updateUI();
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
});
</script>
</body>
</html>
```