Prompt specification
Build or customize the Three.js 3D scene "Unequal ice melting comparison" 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: Unequal ice melting comparison
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/unequal-ice-melting-comparison-x0122
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts ice cubes and melting. Place equal-volume ice pieces with different surface areas on identical plates; animate a labeled qualitative melting comparison while water collects beneath each original shape. Low-poly.
Working Three.js Starter (unequal-ice-melting-comparison-x0122):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Surface Area Melt Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #06131c;
--panel: rgba(8, 25, 35, 0.78);
--border: rgba(173, 231, 244, 0.2);
--text: #edfaff;
--muted: #9dbbc5;
--fast: #ffc56f;
--slow: #72e1ec;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
header {
position: fixed;
top: 24px;
left: 50%;
width: min(720px, calc(100% - 36px));
transform: translateX(-50%);
z-index: 5;
text-align: center;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.5);
}
.eyebrow {
margin-bottom: 7px;
color: var(--slow);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.65rem);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px auto 0;
color: #b8d1d9;
font-size: 0.92rem;
}
.model-badge {
display: inline-block;
margin-top: 12px;
padding: 6px 10px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(5, 19, 27, 0.58);
color: #c9e2e9;
font-size: 0.67rem;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scene-label {
position: fixed;
z-index: 4;
width: 246px;
padding: 13px 14px 12px;
transform: translate(-50%, -100%);
margin-top: -29px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
transition: opacity 120ms linear;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 29px;
background: linear-gradient(var(--accent), transparent);
opacity: 0.8;
}
.scene-label.faster {
--accent: var(--fast);
}
.scene-label.slower {
--accent: var(--slow);
}
.label-kicker {
display: flex;
align-items: center;
gap: 7px;
color: var(--accent);
font-size: 0.65rem;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.label-kicker::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 14px var(--accent);
}
.scene-label h2 {
margin: 6px 0 10px;
font-size: 1.16rem;
letter-spacing: -0.025em;
}
.comparison-row {
display: flex;
justify-content: space-between;
gap: 12px;
padding-top: 9px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
font-size: 0.74rem;
}
.comparison-row span {
color: var(--muted);
}
.comparison-row strong {
color: var(--accent);
text-align: right;
}
.meter {
height: 4px;
margin-top: 10px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.09);
}
.meter > div {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--accent);
box-shadow: 0 0 12px var(--accent);
}
#readout {
position: fixed;
left: 22px;
bottom: 22px;
z-index: 6;
width: min(390px, calc(100% - 44px));
padding: 13px 15px;
border: 1px solid var(--border);
border-radius: 13px;
background: var(--panel);
backdrop-filter: blur(12px);
pointer-events: none;
}
#volumeText {
font-size: 0.84rem;
font-weight: 700;
}
#areaText,
#comparisonStatus {
margin-top: 5px;
color: var(--muted);
font-size: 0.72rem;
line-height: 1.4;
}
#comparisonStatus {
color: #d8edf2;
}
#controls {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 7;
display: flex;
gap: 8px;
padding: 8px;
border: 1px solid var(--border);
border-radius: 13px;
background: var(--panel);
backdrop-filter: blur(12px);
}
button {
border: 1px solid rgba(177, 230, 242, 0.22);
border-radius: 9px;
padding: 9px 12px;
background: rgba(170, 225, 238, 0.08);
color: var(--text);
font: inherit;
font-size: 0.75rem;
font-weight: 750;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(170, 225, 238, 0.17);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--slow);
outline-offset: 2px;
}
#hint {
position: fixed;
left: 50%;
bottom: 25px;
z-index: 3;
transform: translateX(-50%);
color: rgba(215, 239, 245, 0.62);
font-size: 0.7rem;
letter-spacing: 0.04em;
pointer-events: none;
text-shadow: 0 2px 10px #000;
}
@media (max-width: 760px) {
header {
top: 15px;
}
.subtitle {
max-width: 320px;
font-size: 0.78rem;
}
.model-badge {
margin-top: 8px;
}
.scene-label {
width: 184px;
padding: 10px;
}
.scene-label h2 {
margin: 5px 0 8px;
font-size: 0.94rem;
}
.comparison-row {
font-size: 0.64rem;
}
#readout {
left: 12px;
bottom: 76px;
width: calc(100% - 24px);
padding: 10px 12px;
}
#controls {
right: 12px;
bottom: 12px;
}
button {
padding: 8px 10px;
font-size: 0.69rem;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header>
<div class="eyebrow">Surface Area Melt Lab</div>
<h1>Which ice melts first?</h1>
<p class="subtitle">
Equal total ice volume on identical plates, under the same conditions.
</p>
<div class="model-badge">Qualitative comparison · not a thermal simulation</div>
</header>
<div id="labelA" class="scene-label faster">
<div class="label-kicker">Group A</div>
<h2>3 ice cubes</h2>
<div class="comparison-row">
<span>Surface area</span>
<strong>More</strong>
</div>
<div class="comparison-row">
<span>Melting</span>
<strong id="statusA">Faster</strong>
</div>
<div class="meter"><div id="meterA"></div></div>
</div>
<div id="labelB" class="scene-label slower">
<div class="label-kicker">Group B</div>
<h2>1 round ice piece</h2>
<div class="comparison-row">
<span>Surface area</span>
<strong>Less</strong>
</div>
<div class="comparison-row">
<span>Melting</span>
<strong id="statusB">Slower</strong>
</div>
<div class="meter"><div id="meterB"></div></div>
</div>
<div id="readout">
<div id="volumeText">Calculating equal volumes…</div>
<div id="areaText"></div>
<div id="comparisonStatus">Both ice groups are beginning to melt.</div>
</div>
<div id="controls">
<button id="pauseButton" type="button">Pause</button>
<button id="replayButton" type="button">Replay</button>
<button id="viewButton" type="button">Reset view</button>
</div>
<div id="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06131c);
scene.fog = new THREE.Fog(0x06131c, 11, 25);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.62, 0);
const defaultView = {
theta: 0,
phi: 0.91,
radius: window.innerWidth < 700 ? 14 : 9.7
};
const view = { ...defaultView };
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
cameraTarget.x + view.radius * sinPhi * Math.sin(view.theta),
cameraTarget.y + view.radius * Math.cos(view.phi),
cameraTarget.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc7f3ff, 0x071018, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x67dff2, 1.6);
rimLight.position.set(-5, 3.5, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffc477, 18, 10, 2);
warmLight.position.set(-2.4, 3.2, 3.8);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Ground and plates
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
new THREE.MeshStandardMaterial({
color: 0x0b202a,
roughness: 0.92,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.012;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(24, 24, 0x164453, 0x102f3b);
grid.position.y = -0.006;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
const PLATE_TOP = 0.24;
const LEFT_X = -2.25;
const RIGHT_X = 2.25;
const ceramicMaterial = new THREE.MeshStandardMaterial({
color: 0xd7edf0,
roughness: 0.25,
metalness: 0.08
});
const ceramicEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0xa9cbd2,
roughness: 0.32,
metalness: 0.12
});
function createPlate(x) {
const plate = new THREE.Group();
plate.position.x = x;
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.38, 1.42, 0.16, 32),
ceramicEdgeMaterial
);
base.position.y = 0.08;
base.castShadow = true;
base.receiveShadow = true;
plate.add(base);
const top = new THREE.Mesh(
new THREE.CylinderGeometry(1.29, 1.29, 0.08, 32),
ceramicMaterial
);
top.position.y = 0.20;
top.castShadow = true;
top.receiveShadow = true;
plate.add(top);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(1.31, 0.048, 6, 40),
ceramicMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 0.245;
rim.castShadow = true;
plate.add(rim);
scene.add(plate);
return plate;
}
createPlate(LEFT_X);
createPlate(RIGHT_X);
// -------------------------------------------------------------------------
// Equal-volume calculation
// -------------------------------------------------------------------------
function triangleArea(a, b, c) {
const ab = new THREE.Vector3().subVectors(b, a);
const ac = new THREE.Vector3().subVectors(c, a);
return 0.5 * ab.cross(ac).length();
}
function geometryMetrics(geometry) {
const position = geometry.attributes.position;
const index = geometry.index;
let volume = 0;
let area = 0;
const triangleCount = index
? index.count / 3
: position.count / 3;
for (let triangle = 0; triangle < triangleCount; triangle++) {
let ia;
let ib;
let ic;
if (index) {
ia = index.getX(triangle * 3);
ib = index.getX(triangle * 3 + 1);
ic = index.getX(triangle * 3 + 2);
} else {
ia = triangle * 3;
ib = triangle * 3 + 1;
ic = triangle * 3 + 2;
}
const a = new THREE.Vector3().fromBufferAttribute(position, ia);
const b = new THREE.Vector3().fromBufferAttribute(position, ib);
const c = new THREE.Vector3().fromBufferAttribute(position, ic);
volume += a.dot(new THREE.Vector3().crossVectors(b, c)) / 6;
area += triangleArea(a, b, c);
}
return {
volume: Math.abs(volume),
area
};
}
const cubeSide = 0.64;
const cubeVolume = cubeSide ** 3;
const equalGroupVolume = cubeVolume * 3;
const cubeGroupArea = 6 * cubeSide ** 2 * 3;
// A faceted low-poly round piece. Its geometry is scaled so that its
// triangulated volume exactly matches the total volume of the three cubes.
const sphereSource = new THREE.IcosahedronGeometry(1, 1);
const sphereSourceMetrics = geometryMetrics(sphereSource);
const sphereRadius = Math.cbrt(
equalGroupVolume / sphereSourceMetrics.volume
);
const sphereGeometry = new THREE.IcosahedronGeometry(sphereRadius, 1);
const sphereMetrics = geometryMetrics(sphereGeometry);
const sphereArea = sphereMetrics.area;
const areaRatio = cubeGroupArea / sphereArea;
document.getElementById("volumeText").textContent =
`Equal total volume: ${equalGroupVolume.toFixed(3)} volume units on each plate.`;
document.getElementById("areaText").textContent =
`Total surface area: ${cubeGroupArea.toFixed(2)} u² for the cubes vs. ` +
`${sphereArea.toFixed(2)} u² for the round piece (${areaRatio.toFixed(2)}×).`;
// -------------------------------------------------------------------------
// Ice and water materials
// -------------------------------------------------------------------------
const iceMaterial = new THREE.MeshPhysicalMaterial({
color: 0x9de8f5,
roughness: 0.23,
metalness: 0,
transmission: 0.14,
thickness: 0.55,
transparent: true,
opacity: 0.9,
ior: 1.31,
clearcoat: 0.52,
clearcoatRoughness: 0.25,
flatShading: true
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0xd9f8ff,
transparent: true,
opacity: 0.34
});
function createWaterMaterial() {
return new THREE.MeshPhysicalMaterial({
color: 0x43c6df,
emissive: 0x0b4050,
emissiveIntensity: 0.18,
roughness: 0.12,
metalness: 0.02,
transmission: 0.12,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
});
}
function createPuddleGeometry(radius, sides, squareLike = false) {
const positions = [0, 0, 0];
const indices = [];
const variation = [0.92, 1.03, 0.96, 1.05, 0.94, 1.02, 0.97, 1.04];
for (let i = 0; i < sides; i++) {
const angle = (i / sides) * Math.PI * 2;
const cosine = Math.cos(angle);
const sine = Math.sin(angle);
let r = radius * variation[i % variation.length];
if (squareLike) {
r = radius / Math.max(Math.abs(cosine), Math.abs(sine));
r *= 0.93 + variation[i % variation.length] * 0.07;
}
positions.push(cosine * r, 0, sine * r);
}
for (let i = 0; i < sides; i++) {
const next = (i + 1) % sides;
indices.push(0, i + 1, next + 1);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function createPuddle(x, z, radius, squareLike = false, sides = 10) {
const puddle = new THREE.Mesh(
createPuddleGeometry(radius, sides, squareLike),
createWaterMaterial()
);
puddle.position.set(x, PLATE_TOP + 0.012, z);
puddle.renderOrder = 2;
scene.add(puddle);
return puddle;
}
const cubeEntries = [];
const sphereEntries = [];
function createCube(x, z, bubbleOffsets) {
const group = new THREE.Group();
const geometry = new THREE.BoxGeometry(
cubeSide,
cubeSide,
cubeSide
);
const mesh = new THREE.Mesh(geometry, iceMaterial.clone());
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 1),
edgeMaterial.clone()
);
group.add(edges);
const bubbleMaterial = new THREE.MeshBasicMaterial({
color: 0xe9fbff,
transparent: true,
opacity: 0.4,
depthWrite: false
});
const bubbleGeometry = new THREE.IcosahedronGeometry(0.035, 0);
for (const offset of bubbleOffsets) {
const bubble = new THREE.Mesh(bubbleGeometry, bubbleMaterial);
bubble.position.set(
offset[0] * cubeSide,
offset[1] * cubeSide,
offset[2] * cubeSide
);
group.add(bubble);
}
group.position.set(x, PLATE_TOP + cubeSide / 2, z);
scene.add(group);
const puddle = createPuddle(
x,
z,
cubeSide * 0.73,
true,
8
);
cubeEntries.push({
group,
iceMaterial: mesh.material,
edgeMaterial: edges.material,
bubbleMaterial,
baseHeight: cubeSide,
puddle,
puddleRadius: cubeSide * 0.73
});
}
createCube(LEFT_X - 0.43, -0.28, [
[-0.17, 0.12, 0.1],
[0.14, -0.05, -0.16],
[0.08, 0.2, 0.12]
]);
createCube(LEFT_X + 0.43, -0.28, [
[0.12, 0.16, -0.1],
[-0.16, -0.08, 0.11],
[0.02, 0.04, 0.2]
]);
createCube(LEFT_X, 0.43, [
[-0.13, 0.16, -0.12],
[0.18, 0.02, 0.08],
[0.02, -0.18, 0.13]
]);
function createRoundPiece(x, z) {
const group = new THREE.Group();
const mesh = new THREE.Mesh(sphereGeometry, iceMaterial.clone());
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(sphereGeometry, 1),
edgeMaterial.clone()
);
edges.material.opacity = 0.17;
group.add(edges);
const bubbleMaterial = new THREE.MeshBasicMaterial({
color: 0xe9fbff,
transparent: true,
opacity: 0.38,
depthWrite: false
});
const bubbleGeometry = new THREE.IcosahedronGeometry(0.04, 0);
const bubblePositions = [
[-0.28, 0.18, 0.14],
[0.24, -0.1, 0.25],
[0.08, 0.28, -0.22]
];
for (const offset of bubblePositions) {
const bubble = new THREE.Mesh(bubbleGeometry, bubbleMaterial);
bubble.position.set(
offset[0] * sphereRadius,
offset[1] * sphereRadius,
offset[2] * sphereRadius
);
group.add(bubble);
}
group.position.set(x, PLATE_TOP + sphereRadius, z);
scene.add(group);
const puddle = createPuddle(
x,
z,
sphereRadius * 1.05,
false,
12
);
sphereEntries.push({
group,
iceMaterial: mesh.material,
edgeMaterial: edges.material,
bubbleMaterial,
baseHeight: sphereRadius * 2,
puddle,
puddleRadius: sphereRadius * 1.05
});
}
createRoundPiece(RIGHT_X, 0);
// -------------------------------------------------------------------------
// Melt animation
// -------------------------------------------------------------------------
const LOOP_LENGTH = 18.2;
const CUBE_MELT_TIME = 10.5;
const ROUND_MELT_TIME = 15;
let elapsed = 0;
let previousTime = performance.now();
let paused = false;
const statusA = document.getElementById("statusA");
const statusB = document.getElementById("statusB");
const meterA = document.getElementById("meterA");
const meterB = document.getElementById("meterB");
const comparisonStatus = document.getElementById("comparisonStatus");
function updateIceEntry(entry, melt, fade, phase, index) {
const remaining = Math.max(0.000001, 1 - melt);
const iceScale = Math.cbrt(remaining);
const vanish = 1 - THREE.MathUtils.smoothstep(melt, 0.965, 1);
entry.group.scale.setScalar(iceScale);
entry.group.position.y =
PLATE_TOP + 0.018 + (entry.baseHeight * iceScale) / 2;
entry.iceMaterial.opacity = 0.9 * fade * vanish;
entry.edgeMaterial.opacity = 0.34 * fade * vanish;
entry.bubbleMaterial.opacity = 0.4 * fade * vanish;
entry.group.visible = entry.iceMaterial.opacity > 0.005;
// A constant-depth pool grows with the square root of two-thirds
// of the melted fraction, preserving the qualitative volume relation.
const puddleGrowth = Math.pow(Math.max(0, melt), 2 / 3);
const shimmer = 1 + Math.sin(phase * 2.1 + index * 1.7) * 0.012;
entry.puddle.scale.setScalar(puddleGrowth * shimmer);
entry.puddle.material.opacity =
0.73 *
THREE.MathUtils.smoothstep(melt, 0, 0.09) *
fade;
entry.puddle.visible = entry.puddle.material.opacity > 0.005;
}
function updateMelt(phase) {
const fade =
1 - THREE.MathUtils.smoothstep(phase, 17.2, LOOP_LENGTH);
const cubeMelt = THREE.MathUtils.clamp(
phase / CUBE_MELT_TIME,
0,
1
);
const roundMelt = THREE.MathUtils.clamp(
phase / ROUND_MELT_TIME,
0,
1
);
cubeEntries.forEach((entry, index) => {
updateIceEntry(entry, cubeMelt, fade, phase, index);
});
sphereEntries.forEach((entry, index) => {
updateIceEntry(entry, roundMelt, fade, phase, index + 10);
});
meterA.style.width = `${cubeMelt * 100}%`;
meterB.style.width = `${roundMelt * 100}%`;
if (cubeMelt >= 0.995 && roundMelt < 0.995) {
statusA.textContent = "Melted first";
statusB.textContent = "Still melting · slower";
comparisonStatus.textContent =
"Result: the cube group melted first because it exposed more surface area.";
} else if (roundMelt >= 0.995) {
statusA.textContent = "Melted first";
statusB.textContent = "Melted";
comparisonStatus.textContent =
"Both groups have melted; the cube group completed first.";
} else if (cubeMelt > 0.05) {
statusA.textContent = "Melting faster";
statusB.textContent = "Melting slower";
comparisonStatus.textContent =
"The higher-surface-area cubes are shrinking and collecting water faster.";
} else {
statusA.textContent = "Faster";
statusB.textContent = "Slower";
comparisonStatus.textContent =
"Both ice groups are beginning to melt.";
}
}
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelA = document.getElementById("labelA");
const labelB = document.getElementById("labelB");
const anchorA = new THREE.Vector3(LEFT_X, 2.15, 0);
const anchorB = new THREE.Vector3(RIGHT_X, 2.15, 0);
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
const width = window.innerWidth;
const height = window.innerHeight;
let x = (projected.x * 0.5 + 0.5) * width;
let y = (-projected.y * 0.5 + 0.5) * height;
const halfWidth = element.offsetWidth / 2;
x = THREE.MathUtils.clamp(x, halfWidth + 8, width - halfWidth - 8);
y = THREE.MathUtils.clamp(y, 155, height - 105);
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
positionLabel(labelA, anchorA);
positionLabel(labelB, anchorB);
}
// -------------------------------------------------------------------------
// Custom camera controls: no add-ons or examples modules
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
view.theta -= deltaX * 0.006;
view.phi = THREE.MathUtils.clamp(
view.phi - deltaY * 0.005,
0.28,
1.35
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius + event.deltaY * 0.008,
6,
18
);
updateCamera();
},
{ passive: false }
);
function resetView() {
Object.assign(view, defaultView);
updateCamera();
}
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("viewButton").addEventListener("click", resetView);
const pauseButton = document.getElementById("pauseButton");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Play" : "Pause";
});
document.getElementById("replayButton").addEventListener("click", () => {
elapsed = 0;
paused = false;
pauseButton.textContent = "Pause";
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
elapsed += delta;
}
const phase = elapsed % LOOP_LENGTH;
updateMelt(phase);
updateLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```