Prompt specification
Build or customize the Three.js 3D scene "Two sample blocks and energy counters" 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: Two sample blocks and energy counters
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-sample-blocks-and-energy-counters-x1469
Scene Description Prompt:
Make a low-poly Three.js scene in a self-contained HTML file using cdnjs: two sample blocks and energy counters. Place equal-mass sample blocks with different stated specific heat capacities beside identical heaters; display temperature changes computed from equal energy inputs under no-loss assumptions.
Working Three.js Starter (two-sample-blocks-and-energy-counters-x1469):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Thermal Comparison Lab</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:
radial-gradient(circle at 50% 35%, #14283a 0%, #091521 55%, #050b12 100%);
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 5;
top: 20px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 5px;
color: #64ddd0;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #f5f8fb;
font-size: clamp(22px, 3vw, 36px);
line-height: 1;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 8px;
color: #9eb1c3;
font-size: 13px;
}
.status-pill,
.mass-pill {
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 999px;
background: rgba(8, 18, 29, 0.72);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(12px);
color: #dbe7f0;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.status-pill {
padding: 10px 14px;
}
.status-pill::before {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 8px;
border-radius: 50%;
background: #64ddd0;
box-shadow: 0 0 12px #64ddd0;
content: "";
vertical-align: 1px;
}
.mass-pill {
position: fixed;
top: 88px;
right: 24px;
padding: 9px 13px;
color: #b9c9d7;
}
.hud {
position: fixed;
z-index: 4;
top: 96px;
left: 50%;
width: min(900px, calc(100% - 48px));
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
transform: translateX(-50%);
pointer-events: none;
}
.sample-card {
position: relative;
overflow: hidden;
padding: 15px 17px 14px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-top: 2px solid var(--accent);
border-radius: 14px;
background: linear-gradient(
145deg,
rgba(13, 29, 43, 0.91),
rgba(7, 17, 27, 0.78)
);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(15px);
}
.sample-card::after {
position: absolute;
top: -44px;
right: -38px;
width: 120px;
height: 120px;
border-radius: 50%;
background: var(--accent);
filter: blur(55px);
opacity: 0.12;
content: "";
}
.card-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.sample-name {
display: flex;
align-items: center;
min-width: 0;
gap: 9px;
color: #f3f7fa;
font-size: 14px;
font-weight: 800;
}
.sample-dot {
flex: 0 0 auto;
width: 11px;
height: 11px;
border-radius: 3px;
background: var(--accent);
box-shadow: 0 0 14px color-mix(in srgb, var(--accent), transparent 35%);
}
.heat-capacity {
color: #8ea4b6;
font-size: 10px;
font-weight: 650;
text-align: right;
white-space: nowrap;
}
.heat-capacity strong {
display: block;
margin-top: 2px;
color: #dce7ee;
font-size: 11px;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 12px;
}
.metric {
min-width: 0;
}
.metric-label {
display: block;
margin-bottom: 4px;
color: #7890a3;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric-value {
display: block;
overflow: hidden;
color: #f7fafc;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: clamp(17px, 2.2vw, 25px);
font-weight: 750;
letter-spacing: -0.05em;
text-overflow: ellipsis;
white-space: nowrap;
font-variant-numeric: tabular-nums;
}
.energy-value {
color: var(--accent);
}
.outcome {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 10px;
padding-top: 9px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: #a8bbc9;
font-size: 11px;
}
.outcome strong {
color: #f1f6f9;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.progress-track {
height: 4px;
margin-top: 10px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--accent);
box-shadow: 0 0 12px var(--accent);
}
.controls {
position: fixed;
z-index: 6;
bottom: 20px;
left: 50%;
display: flex;
align-items: center;
width: min(900px, calc(100% - 48px));
gap: 16px;
padding: 12px 14px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 15px;
background: rgba(7, 16, 25, 0.86);
box-shadow: 0 15px 50px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(16px);
transform: translateX(-50%);
}
.button {
flex: 0 0 auto;
min-width: 128px;
padding: 10px 16px;
border: 0;
border-radius: 9px;
background: #64ddd0;
box-shadow: 0 7px 22px rgba(100, 221, 208, 0.18);
color: #06201f;
cursor: pointer;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.03em;
transition: transform 120ms ease, filter 120ms ease;
}
.button:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
.button.secondary {
min-width: 72px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.07);
box-shadow: none;
color: #dce6ed;
}
.slider-wrap {
flex: 1 1 auto;
min-width: 150px;
}
.slider-label {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: #849bad;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.slider-label strong {
color: #eaf1f5;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 99px;
outline: none;
background: linear-gradient(
to right,
#64ddd0 0%,
#64ddd0 var(--range-fill, 33.33%),
rgba(255, 255, 255, 0.11) var(--range-fill, 33.33%),
rgba(255, 255, 255, 0.11) 100%
);
appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #dffbf7;
border-radius: 50%;
background: #40bdb1;
box-shadow: 0 0 0 4px rgba(100, 221, 208, 0.12);
cursor: pointer;
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #dffbf7;
border-radius: 50%;
background: #40bdb1;
cursor: pointer;
}
.formula {
flex: 0 0 auto;
padding-left: 14px;
border-left: 1px solid rgba(255, 255, 255, 0.1);
color: #7f96a8;
font-size: 10px;
line-height: 1.5;
text-align: center;
}
.formula strong {
display: block;
color: #dce6ed;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
}
.camera-hint {
position: fixed;
z-index: 3;
right: 24px;
bottom: 103px;
color: rgba(194, 211, 223, 0.56);
font-size: 10px;
letter-spacing: 0.05em;
pointer-events: none;
}
@media (max-width: 760px) {
.topbar {
top: 14px;
left: 16px;
right: 16px;
}
.subtitle,
.mass-pill,
.camera-hint,
.formula {
display: none;
}
.status-pill {
padding: 8px 10px;
font-size: 9px;
}
.hud {
top: 70px;
width: calc(100% - 24px);
gap: 8px;
}
.sample-card {
padding: 11px;
}
.heat-capacity {
font-size: 8px;
}
.metrics {
margin-top: 8px;
}
.metric-value {
font-size: 16px;
}
.outcome {
margin-top: 7px;
padding-top: 6px;
font-size: 9px;
}
.outcome strong {
font-size: 11px;
}
.controls {
bottom: 10px;
width: calc(100% - 24px);
gap: 9px;
padding: 10px;
}
.button {
min-width: 102px;
padding: 9px 11px;
font-size: 10px;
}
.button.secondary {
min-width: 55px;
}
.slider-label {
font-size: 8px;
}
}
@media (max-width: 520px) {
.hud {
grid-template-columns: 1fr;
}
.sample-card {
padding: 9px 11px;
}
.metrics {
grid-template-columns: 1.25fr 1fr;
}
.metric-value {
font-size: 15px;
}
.outcome {
display: none;
}
.progress-track {
margin-top: 7px;
}
.controls {
align-items: stretch;
flex-wrap: wrap;
}
.slider-wrap {
flex-basis: 100%;
order: -1;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive 3D thermal comparison scene"></div>
<header class="topbar">
<div>
<div class="eyebrow">Interactive thermal lab</div>
<h1>Same Heat. Different Rise.</h1>
<div class="subtitle">
Equal-mass samples · identical heaters · no heat loss
</div>
</div>
<div id="status" class="status-pill" aria-live="polite">Ready</div>
</header>
<div class="mass-pill">Equal mass · 0.50 kg each</div>
<section id="cards" class="hud" aria-label="Sample measurements"></section>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<section class="controls" aria-label="Experiment controls">
<button id="startButton" class="button">Start heating</button>
<button id="resetButton" class="button secondary">Reset</button>
<div class="slider-wrap">
<label class="slider-label" for="energySlider">
<span>Equal energy input to each heater</span>
<strong id="energySetting">27,000 J</strong>
</label>
<input
id="energySlider"
type="range"
min="9000"
max="54000"
step="1000"
value="27000"
/>
</div>
<div class="formula">
<strong>ΔT = Q / (m · c)</strong>
no-loss assumption
</div>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -----------------------------
// Experiment constants
// -----------------------------
const MASS = 0.50; // kg
const INITIAL_TEMP = 20; // °C
const DURATION = 8; // seconds for the full selected input
const DEFAULT_ENERGY = 27000; // J delivered to EACH heater
const samples = [
{
id: "A",
name: "Sample A",
materialName: "Metal sample",
c: 900, // J/(kg·K)
color: 0xff835f,
css: "#ff835f",
x: -2.35,
outerSide: -1
},
{
id: "B",
name: "Sample B",
materialName: "Ceramic sample",
c: 1500, // J/(kg·K)
color: 0x45c9b8,
css: "#45c9b8",
x: 2.35,
outerSide: 1
}
];
let targetEnergy = DEFAULT_ENERGY;
let elapsed = 0;
let progress = 0;
let playing = false;
const clamp = THREE.MathUtils.clamp;
const fract = (x) => x - Math.floor(x);
const formatJ = (value) =>
`${Math.round(value).toLocaleString("en-US")} J`;
const formatTemp = (value) => `${value.toFixed(1)}°C`;
// -----------------------------
// UI
// -----------------------------
const cardsElement = document.getElementById("cards");
const statusElement = document.getElementById("status");
const startButton = document.getElementById("startButton");
const resetButton = document.getElementById("resetButton");
const energySlider = document.getElementById("energySlider");
const energySetting = document.getElementById("energySetting");
cardsElement.innerHTML = samples
.map(
(sample) => `
<article class="sample-card" style="--accent:${sample.css}">
<div class="card-head">
<div class="sample-name">
<span class="sample-dot"></span>
<span>${sample.name}</span>
</div>
<div class="heat-capacity">
specific heat
<strong>c = ${sample.c.toLocaleString("en-US")} J/(kg·K)</strong>
</div>
</div>
<div class="metrics">
<div class="metric">
<span class="metric-label">Energy counter</span>
<strong id="energy-${sample.id}" class="metric-value energy-value">
0 J
</strong>
</div>
<div class="metric">
<span class="metric-label">Temperature</span>
<strong id="temp-${sample.id}" class="metric-value">
${formatTemp(INITIAL_TEMP)}
</strong>
</div>
</div>
<div class="outcome">
<span>Temperature change <strong id="delta-${sample.id}">+0.0°C</strong></span>
<span id="final-${sample.id}">Final ${formatTemp(
INITIAL_TEMP + targetEnergy / (MASS * sample.c)
)}</span>
</div>
<div class="progress-track">
<div id="progress-${sample.id}" class="progress-fill"></div>
</div>
</article>
`
)
.join("");
const ui = {};
for (const sample of samples) {
ui[sample.id] = {
energy: document.getElementById(`energy-${sample.id}`),
temp: document.getElementById(`temp-${sample.id}`),
delta: document.getElementById(`delta-${sample.id}`),
final: document.getElementById(`final-${sample.id}`),
progress: document.getElementById(`progress-${sample.id}`)
};
}
function updateSliderFill() {
const min = Number(energySlider.min);
const max = Number(energySlider.max);
const percent = ((targetEnergy - min) / (max - min)) * 100;
energySlider.style.setProperty("--range-fill", `${percent}%`);
energySetting.textContent = formatJ(targetEnergy);
}
function measurement(sample) {
const deliveredEnergy = targetEnergy * progress;
const deltaT = deliveredEnergy / (MASS * sample.c);
return {
deliveredEnergy,
deltaT,
temperature: INITIAL_TEMP + deltaT
};
}
function updateStatus() {
if (progress >= 1) {
statusElement.textContent = "Complete · equal energy delivered";
} else if (playing) {
statusElement.textContent = "Heating both samples";
} else if (progress > 0) {
statusElement.textContent = "Paused";
} else {
statusElement.textContent = "Ready";
}
startButton.textContent =
progress >= 1
? "Replay"
: playing
? "Pause"
: progress > 0
? "Resume"
: "Start heating";
}
// -----------------------------
// Three.js setup
// -----------------------------
const container = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08131e);
scene.fog = new THREE.Fog(0x08131e, 16, 31);
const camera = new THREE.PerspectiveCamera(
40,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
container.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xa8d8ff, 0x18202a, 2.1);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff0d8, 3.4);
key.position.set(5.5, 9, 7.5);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -9;
key.shadow.camera.right = 9;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -7;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 30;
key.shadow.bias = -0.0005;
scene.add(key);
const rim = new THREE.DirectionalLight(0x4b9cff, 1.8);
rim.position.set(-7, 5, -5);
scene.add(rim);
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0x0d1a27,
roughness: 0.94,
metalness: 0.03,
flatShading: true
}),
bench: new THREE.MeshStandardMaterial({
color: 0x263746,
roughness: 0.73,
metalness: 0.12,
flatShading: true
}),
benchEdge: new THREE.MeshStandardMaterial({
color: 0x344b5d,
roughness: 0.58,
metalness: 0.2,
flatShading: true
}),
dark: new THREE.MeshStandardMaterial({
color: 0x111923,
roughness: 0.5,
metalness: 0.5,
flatShading: true
}),
heater: new THREE.MeshStandardMaterial({
color: 0x252f39,
roughness: 0.44,
metalness: 0.7,
flatShading: true
}),
ceramic: new THREE.MeshStandardMaterial({
color: 0xc9d0d1,
roughness: 0.9,
metalness: 0,
flatShading: true
})
};
function mesh(geometry, material, position, castShadow = true) {
const object = new THREE.Mesh(geometry, material);
if (position) object.position.copy(position);
object.castShadow = castShadow;
object.receiveShadow = true;
scene.add(object);
return object;
}
// Low-poly circular platform and laboratory bench.
mesh(
new THREE.CylinderGeometry(7.1, 7.45, 0.38, 10),
materials.floor,
new THREE.Vector3(0, -0.22, 0),
false
);
const grid = new THREE.GridHelper(12.5, 16, 0x274052, 0x182b3a);
grid.position.y = -0.015;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
const bench = mesh(
new THREE.BoxGeometry(11.5, 0.34, 5.25),
materials.bench,
new THREE.Vector3(0, 0.43, 0)
);
mesh(
new THREE.BoxGeometry(11.65, 0.1, 5.4),
materials.benchEdge,
new THREE.Vector3(0, 0.64, 0)
);
for (const x of [-4.8, 4.8]) {
for (const z of [-1.8, 1.8]) {
mesh(
new THREE.CylinderGeometry(0.12, 0.16, 0.8, 6),
materials.dark,
new THREE.Vector3(x, 0, z)
);
}
}
// Faceted back wall.
const wallColors = [0x0d1b29, 0x102232, 0x0c1925, 0x122638, 0x0d1b29];
for (let i = 0; i < 5; i++) {
const wallMaterial = new THREE.MeshStandardMaterial({
color: wallColors[i],
roughness: 0.94,
metalness: 0.04,
flatShading: true
});
mesh(
new THREE.BoxGeometry(2.55, 4.9, 0.16),
wallMaterial,
new THREE.Vector3((i - 2) * 2.43, 2.18, -3.55),
false
);
}
function makeTextTexture(
width,
height,
draw,
transparent = false
) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
draw(context, width, height);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
if (transparent) {
texture.premultiplyAlpha = true;
}
return { canvas, context, texture };
}
// Rear lab sign.
const sign = makeTextTexture(1024, 180, (ctx, w, h) => {
ctx.fillStyle = "#0a1723";
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = "#58d7ca";
ctx.fillRect(0, 0, 18, h);
ctx.fillRect(w - 18, 0, 18, h);
ctx.textAlign = "center";
ctx.fillStyle = "#edf7f8";
ctx.font = "800 58px system-ui, sans-serif";
ctx.fillText("THERMAL COMPARISON", w / 2, 79);
ctx.fillStyle = "#7897aa";
ctx.font = "700 27px ui-monospace, monospace";
ctx.fillText("EQUAL MASS • EQUAL ENERGY • NO LOSS", w / 2, 128);
});
const signMesh = new THREE.Mesh(
new THREE.PlaneGeometry(4.8, 0.84),
new THREE.MeshBasicMaterial({
map: sign.texture,
toneMapped: false
})
);
signMesh.position.set(0, 3.72, -3.44);
scene.add(signMesh);
function createChamferedBlock(width, height, depth) {
const shape = new THREE.Shape();
shape.moveTo(-width / 2, -height / 2);
shape.lineTo(width / 2, -height / 2);
shape.lineTo(width / 2, height / 2);
shape.lineTo(-width / 2, height / 2);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: 0.075,
bevelThickness: 0.075
});
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
return geometry;
}
function cylinderBetween(start, end, radius, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const rod = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
rod.position.copy(start).add(end).multiplyScalar(0.5);
rod.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
rod.castShadow = true;
return rod;
}
const stations = [];
for (const sample of samples) {
const group = new THREE.Group();
group.position.x = sample.x;
scene.add(group);
const heaterMaterial = materials.heater.clone();
const heaterBase = new THREE.Mesh(
new THREE.BoxGeometry(2.55, 0.46, 2.48),
heaterMaterial
);
heaterBase.position.y = 0.88;
heaterBase.castShadow = true;
heaterBase.receiveShadow = true;
group.add(heaterBase);
const plateMaterial = new THREE.MeshStandardMaterial({
color: 0x303a43,
roughness: 0.45,
metalness: 0.72,
emissive: 0xff3b20,
emissiveIntensity: 0.06,
flatShading: true
});
const plate = new THREE.Mesh(
new THREE.BoxGeometry(2.18, 0.12, 2.12),
plateMaterial
);
plate.position.y = 1.17;
plate.castShadow = true;
group.add(plate);
const coilMaterial = new THREE.MeshStandardMaterial({
color: 0xff6339,
roughness: 0.4,
metalness: 0.28,
emissive: 0xff3014,
emissiveIntensity: 0.45,
flatShading: true
});
const coilPoints = [];
for (let i = 0; i < 8; i++) {
coilPoints.push(
new THREE.Vector3(
-0.84 + i * 0.24,
1.27,
i % 2 === 0 ? -0.63 : 0.63
)
);
}
for (let i = 0; i < coilPoints.length - 1; i++) {
group.add(
cylinderBetween(
coilPoints[i],
coilPoints[i + 1],
0.045,
coilMaterial,
6
)
);
}
// Feet.
for (const x of [-0.95, 0.95]) {
for (const z of [-0.95, 0.95]) {
const foot = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.14, 0.18, 6),
materials.dark
);
foot.position.set(x, 0.72, z);
group.add(foot);
}
}
// Equal-size sample blocks.
const blockMaterial = new THREE.MeshStandardMaterial({
color: sample.color,
roughness: 0.67,
metalness: 0.08,
emissive: sample.color,
emissiveIntensity: 0.025,
flatShading: true
});
const block = new THREE.Mesh(
createChamferedBlock(1.82, 1.34, 1.82),
blockMaterial
);
block.position.y = 1.98;
block.castShadow = true;
block.receiveShadow = true;
group.add(block);
// Label attached to the front of each block.
const blockLabel = makeTextTexture(
640,
300,
(ctx, w, h) => {
ctx.fillStyle = "#f5f2e9";
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = sample.css;
ctx.fillRect(0, 0, w, 24);
ctx.fillStyle = "#17232d";
ctx.textAlign = "center";
ctx.font = "900 69px system-ui, sans-serif";
ctx.fillText(sample.id === "A" ? "SAMPLE A" : "SAMPLE B", w / 2, 105);
ctx.fillStyle = "#52616c";
ctx.font = "700 37px ui-monospace, monospace";
ctx.fillText("m = 0.50 kg", w / 2, 164);
ctx.font = "700 31px ui-monospace, monospace";
ctx.fillText(`c = ${sample.c} J/(kg·K)`, w / 2, 220);
ctx.strokeStyle = "#c4c7c5";
ctx.lineWidth = 5;
ctx.strokeRect(3, 3, w - 6, h - 6);
}
);
const labelMesh = new THREE.Mesh(
new THREE.PlaneGeometry(1.48, 0.69),
new THREE.MeshBasicMaterial({
map: blockLabel.texture,
toneMapped: false
})
);
labelMesh.position.set(0, 1.99, 0.946);
group.add(labelMesh);
// 3D energy counter mounted on the front of each station.
const display = makeTextTexture(
640,
240,
(ctx, w, h) => {
ctx.fillStyle = "#061019";
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = "#172d3b";
ctx.fillRect(0, 0, w, 52);
ctx.fillStyle = "#8ca3b3";
ctx.font = "800 25px ui-monospace, monospace";
ctx.fillText("ENERGY COUNTER", 24, 34);
ctx.textAlign = "right";
ctx.fillStyle = sample.css;
ctx.fillText(`HEATER ${sample.id}`, w - 24, 34);
ctx.textAlign = "left";
ctx.fillStyle = "#f2fbfc";
ctx.font = "800 71px ui-monospace, monospace";
ctx.fillText("0 J", 25, 143);
ctx.fillStyle = "#223b49";
ctx.fillRect(25, 178, w - 50, 18);
ctx.fillStyle = sample.css;
ctx.fillRect(25, 178, 0, 18);
ctx.fillStyle = "#718b9c";
ctx.font = "700 19px ui-monospace, monospace";
ctx.fillText("EQUAL INPUT", 25, 225);
}
);
const consoleBody = new THREE.Mesh(
new THREE.BoxGeometry(2.34, 0.91, 0.18),
materials.dark
);
consoleBody.position.set(0, 1.03, 2.48);
consoleBody.castShadow = true;
group.add(consoleBody);
const displayMesh = new THREE.Mesh(
new THREE.PlaneGeometry(2.15, 0.81),
new THREE.MeshBasicMaterial({
map: display.texture,
toneMapped: false
})
);
displayMesh.position.set(0, 1.03, 2.576);
group.add(displayMesh);
// Thermometer placed on the outside of each station.
const thermometer = new THREE.Group();
thermometer.position.set(sample.outerSide * 1.42, 0, 0.08);
group.add(thermometer);
const tubeBase = 1.26;
const tubeHeight = 2.5;
const tube = new THREE.Mesh(
new THREE.CylinderGeometry(0.078, 0.078, tubeHeight, 8),
materials.ceramic
);
tube.position.y = tubeBase + tubeHeight / 2;
tube.castShadow = true;
thermometer.add(tube);
const mercury = new THREE.Mesh(
new THREE.CylinderGeometry(0.04, 0.04, 1, 8),
new THREE.MeshStandardMaterial({
color: 0xff5746,
emissive: 0xff2d20,
emissiveIntensity: 0.45,
roughness: 0.35
})
);
thermometer.add(mercury);
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(0.14, 10, 6),
new THREE.MeshStandardMaterial({
color: 0xff5746,
emissive: 0xff2d20,
emissiveIntensity: 0.4,
roughness: 0.4
})
);
bulb.position.y = tubeBase;
thermometer.add(bulb);
for (let tick = 0; tick <= 6; tick++) {
const tickMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.26, 0.025, 0.035),
materials.dark
);
tickMesh.position.set(
sample.outerSide * 0.15,
tubeBase + (tick / 6) * tubeHeight,
0
);
thermometer.add(tickMesh);
}
const heaterLight = new THREE.PointLight(0xff5a2a, 0, 4.5, 2);
heaterLight.position.set(0, 1.5, 0);
group.add(heaterLight);
// Small animated energy particles.
const particleMaterial = new THREE.MeshBasicMaterial({
color: 0xffd166,
toneMapped: false
});
const particles = [];
for (let i = 0; i < 12; i++) {
const particle = new THREE.Mesh(
new THREE.OctahedronGeometry(0.065, 0),
particleMaterial
);
particle.userData.phase = i / 12;
particle.visible = false;
group.add(particle);
particles.push(particle);
}
stations.push({
sample,
group,
blockMaterial,
coilMaterial,
plateMaterial,
heaterLight,
mercury,
tubeBase,
tubeHeight,
display,
particles
});
}
function updateDisplay(station, deliveredEnergy) {
const { context: ctx, canvas, texture } = station.display;
const w = canvas.width;
const h = canvas.height;
const sample = station.sample;
ctx.fillStyle = "#061019";
ctx.fillRect(0, 0, w, h);
ctx.fillStyle = "#172d3b";
ctx.fillRect(0, 0, w, 52);
ctx.fillStyle = "#8ca3b3";
ctx.font = "800 25px ui-monospace, monospace";
ctx.textAlign = "left";
ctx.fillText("ENERGY COUNTER", 24, 34);
ctx.textAlign = "right";
ctx.fillStyle = sample.css;
ctx.fillText(`HEATER ${sample.id}`, w - 24, 34);
ctx.textAlign = "left";
ctx.fillStyle = "#f2fbfc";
ctx.font = "800 71px ui-monospace, monospace";
ctx.fillText(formatJ(deliveredEnergy), 25, 143);
ctx.fillStyle = "#223b49";
ctx.fillRect(25, 178, w - 50, 18);
ctx.fillStyle = sample.css;
ctx.fillRect(25, 178, (w - 50) * progress, 18);
ctx.fillStyle = "#718b9c";
ctx.font = "700 19px ui-monospace, monospace";
ctx.fillText("EQUAL INPUT", 25, 225);
texture.needsUpdate = true;
}
// -----------------------------
// Direct camera controls
// No OrbitControls or addons.
// -----------------------------
const cameraTarget = new THREE.Vector3(0, 1.55, 0.25);
let cameraTheta = 0.38;
let cameraPhi = 1.08;
let cameraRadius = 14;
let cameraTouched = false;
function updateCamera() {
const offset = new THREE.Vector3().setFromSphericalCoords(
cameraRadius,
cameraPhi,
cameraTheta
);
camera.position.copy(cameraTarget).add(offset);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.38;
cameraPhi = 1.08;
cameraRadius = innerWidth / innerHeight < 0.8 ? 18.5 : 14;
cameraTouched = false;
updateCamera();
}
const canvas = renderer.domElement;
const pointer = {
active: false,
id: null,
x: 0,
y: 0
};
canvas.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
cameraTouched = true;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
cameraTheta -= dx * 0.006;
cameraPhi = clamp(cameraPhi - dy * 0.005, 0.48, 1.43);
pointer.x = event.clientX;
pointer.y = event.clientY;
updateCamera();
});
function stopPointer(event) {
if (event.pointerId !== pointer.id) return;
pointer.active = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopPointer);
canvas.addEventListener("pointercancel", stopPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraTouched = true;
cameraRadius = clamp(
cameraRadius + event.deltaY * 0.008,
9.5,
21
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
// -----------------------------
// Experiment controls
// -----------------------------
startButton.addEventListener("click", () => {
if (progress >= 1) {
elapsed = 0;
progress = 0;
}
playing = !playing;
updateStatus();
});
resetButton.addEventListener("click", () => {
elapsed = 0;
progress = 0;
playing = false;
updateStatus();
});
energySlider.addEventListener("input", () => {
targetEnergy = Number(energySlider.value);
elapsed = 0;
progress = 0;
playing = false;
updateSliderFill();
updateStatus();
});
// -----------------------------
// Resize and animation
// -----------------------------
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
if (!cameraTouched) {
cameraRadius = camera.aspect < 0.8 ? 18.5 : 14;
updateCamera();
}
}
addEventListener("resize", onResize);
const clock = new THREE.Clock();
let visualTime = 0;
let displayAccumulator = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
visualTime += delta;
if (playing) {
elapsed += delta;
progress = clamp(elapsed / DURATION, 0, 1);
if (progress >= 1) {
playing = false;
updateStatus();
}
}
displayAccumulator += delta;
for (const station of stations) {
const { sample } = station;
const state = measurement(sample);
// DOM readouts.
ui[sample.id].energy.textContent = formatJ(state.deliveredEnergy);
ui[sample.id].temp.textContent = formatTemp(state.temperature);
ui[sample.id].delta.textContent = `+${state.deltaT.toFixed(1)}°C`;
ui[sample.id].final.textContent =
`Final ${formatTemp(INITIAL_TEMP + targetEnergy / (MASS * sample.c))}`;
ui[sample.id].progress.style.width = `${progress * 100}%`;
// Heat glow.
station.coilMaterial.emissiveIntensity =
0.4 + progress * 2.1 + (playing ? Math.sin(visualTime * 9) * 0.14 : 0);
station.plateMaterial.emissiveIntensity = 0.05 + progress * 0.42;
station.blockMaterial.emissiveIntensity = 0.025 + progress * 0.12;
station.heaterLight.intensity = progress * 28;
// Thermometer fill: 20°C at the bottom, 140°C at the top.
const temperatureFraction = clamp(
(state.temperature - INITIAL_TEMP) / 120,
0,
1
);
const mercuryHeight = Math.max(
0.005,
temperatureFraction * station.tubeHeight
);
station.mercury.scale.y = mercuryHeight;
station.mercury.position.y =
station.tubeBase + mercuryHeight / 2;
// Rising low-poly energy particles.
for (const particle of station.particles) {
particle.visible = playing;
const phase = fract(
particle.userData.phase + visualTime * (0.48 + progress * 0.18)
);
const angle = particle.userData.phase * Math.PI * 2 + phase * 5;
particle.position.set(
Math.sin(angle) * (0.95 + phase * 0.2),
1.28 + phase * 1.55,
Math.cos(angle) * (0.78 + phase * 0.12)
);
particle.scale.setScalar(
0.35 + Math.sin(phase * Math.PI) * 0.9
);
particle.rotation.x += delta * 2;
particle.rotation.y += delta * 3;
}
// The in-scene counters refresh at 12 Hz.
if (displayAccumulator > 1 / 12) {
updateDisplay(station, state.deliveredEnergy);
}
}
if (displayAccumulator > 1 / 12) {
displayAccumulator = 0;
}
renderer.render(scene, camera);
}
updateSliderFill();
updateStatus();
resetCamera();
animate();
</script>
</body>
</html>
```