Prompt specification
Build or customize the Three.js 3D scene "Latent-heat energy budget explorer" 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: Latent-heat energy budget explorer
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/latent-heat-energy-budget-explorer-x1492
Scene Description Prompt:
A low-poly ideal sample receiving energy, with temperature and phase fraction responding separately. Show its mass and parameters. Three.js from cdnjs, one HTML file.
Working Three.js Starter (latent-heat-energy-budget-explorer-x1492):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Thermal Phase Lab — Ideal Sample</title>
<style>
:root {
--bg: #050b14;
--panel: rgba(8, 17, 30, 0.82);
--panel-solid: #0a1524;
--line: rgba(139, 190, 221, 0.16);
--text: #eaf6ff;
--muted: #7891a8;
--cyan: #54ddff;
--orange: #ff8a4c;
--yellow: #ffd166;
--purple: #a78bfa;
--green: #65e6a6;
}
* {
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;
}
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;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
pointer-events: auto;
background:
linear-gradient(145deg, rgba(18, 33, 51, 0.55), transparent 48%),
var(--panel);
border: 1px solid var(--line);
border-radius: 16px;
box-shadow:
0 20px 50px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.025);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
header {
position: absolute;
top: 20px;
left: 24px;
right: 24px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
border: 1px solid rgba(84, 221, 255, 0.55);
border-radius: 10px;
background: rgba(84, 221, 255, 0.07);
box-shadow: 0 0 30px rgba(84, 221, 255, 0.12);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--cyan);
border-radius: 50%;
}
.brand-mark::before {
width: 13px;
height: 13px;
left: 8px;
top: 12px;
}
.brand-mark::after {
width: 7px;
height: 7px;
right: 6px;
top: 6px;
background: var(--orange);
}
h1 {
margin: 0;
font-size: 16px;
font-weight: 670;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.subtitle {
margin-top: 4px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.05em;
}
.header-badges {
display: flex;
align-items: center;
gap: 8px;
}
.badge {
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 999px;
color: #a9bed0;
background: rgba(7, 15, 26, 0.65);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.badge.live {
color: var(--green);
border-color: rgba(101, 230, 166, 0.24);
}
.live-dot {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 6px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 10px var(--green);
vertical-align: 1px;
}
.left-panel {
position: absolute;
top: 92px;
left: 24px;
width: 286px;
padding: 19px;
max-height: calc(100vh - 116px);
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: rgba(84, 221, 255, 0.25) transparent;
}
.right-panel {
position: absolute;
top: 92px;
right: 24px;
width: 306px;
padding: 17px;
max-height: calc(100vh - 116px);
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: rgba(84, 221, 255, 0.25) transparent;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel-title {
margin: 6px 0 16px;
font-size: 14px;
font-weight: 620;
letter-spacing: 0.02em;
}
.control {
margin-bottom: 19px;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 9px;
}
.control label {
color: #b8cad8;
font-size: 12px;
font-weight: 520;
}
.control output {
color: var(--cyan);
font-variant-numeric: tabular-nums;
font-size: 12px;
font-weight: 700;
}
.control.phase output {
color: var(--orange);
}
input[type="range"] {
--pct: 50%;
appearance: none;
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
outline: none;
background: linear-gradient(
to right,
var(--cyan) 0 var(--pct),
rgba(134, 170, 194, 0.18) var(--pct) 100%
);
cursor: pointer;
}
.control.phase input[type="range"] {
background: linear-gradient(
to right,
var(--orange) 0 var(--pct),
rgba(134, 170, 194, 0.18) var(--pct) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
appearance: none;
width: 14px;
height: 14px;
border: 2px solid #dff8ff;
border-radius: 50%;
background: #0b1b2b;
box-shadow: 0 0 0 4px rgba(84, 221, 255, 0.1);
}
.control.phase input[type="range"]::-webkit-slider-thumb {
box-shadow: 0 0 0 4px rgba(255, 138, 76, 0.1);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #dff8ff;
border-radius: 50%;
background: #0b1b2b;
}
.range-scale {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #526b80;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.divider {
height: 1px;
margin: 17px 0;
background: var(--line);
}
.preset-title {
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.presets,
.buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.buttons {
grid-template-columns: 1fr 1fr;
margin-top: 12px;
}
button {
min-height: 34px;
border: 1px solid var(--line);
border-radius: 9px;
color: #a9bfd1;
background: rgba(123, 163, 190, 0.055);
cursor: pointer;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
transition: 150ms ease;
}
button:hover {
color: #effaff;
border-color: rgba(84, 221, 255, 0.36);
background: rgba(84, 221, 255, 0.09);
transform: translateY(-1px);
}
button.primary {
color: #031018;
border-color: transparent;
background: var(--cyan);
}
button.primary:hover {
background: #8aeaff;
}
.model-note {
color: #647e94;
font-size: 10px;
line-height: 1.55;
}
.model-note strong {
color: #9eb6c9;
font-weight: 600;
}
.state-card {
--state: var(--cyan);
position: relative;
overflow: hidden;
margin-bottom: 10px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(116, 158, 187, 0.035);
}
.state-card::after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 80px;
height: 80px;
border-radius: 50%;
background: var(--state);
filter: blur(45px);
opacity: 0.09;
pointer-events: none;
}
.state-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.state-label {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.state-tag {
color: var(--state);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.state-number {
margin-top: 9px;
font-size: 35px;
font-weight: 580;
line-height: 1;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.state-number small {
margin-left: 5px;
color: var(--muted);
font-size: 13px;
font-weight: 550;
letter-spacing: 0;
}
.meter {
height: 5px;
margin-top: 13px;
overflow: hidden;
border-radius: 999px;
background: rgba(122, 157, 181, 0.12);
}
.meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--state);
box-shadow: 0 0 13px var(--state);
transition: width 120ms linear;
}
.meter-labels {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #536c81;
font-size: 8px;
font-variant-numeric: tabular-nums;
}
.chart-wrap {
margin-top: 13px;
padding: 11px 10px 8px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(4, 10, 18, 0.5);
}
.chart-head {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 2px 7px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.chart-legend {
display: flex;
gap: 9px;
font-size: 8px;
letter-spacing: 0;
text-transform: none;
}
.legend-item {
color: #7891a8;
}
.legend-item::before {
content: "";
display: inline-block;
width: 10px;
height: 2px;
margin: 0 4px 2px 0;
background: var(--cyan);
}
.legend-item.phase::before {
background: var(--orange);
}
#historyChart {
display: block;
width: 100%;
height: 88px;
}
.parameters {
margin-top: 13px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(4, 10, 18, 0.38);
}
.parameter-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px 14px;
margin-top: 11px;
}
.parameter-name {
color: #5e788d;
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.parameter-value {
margin-top: 3px;
color: #c8dae8;
font-size: 11px;
font-weight: 600;
font-variant-numeric: tabular-nums;
}
.parameter-value.emphasis {
color: var(--cyan);
}
.footer-note {
position: absolute;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
color: #60798d;
font-size: 9px;
letter-spacing: 0.08em;
text-align: center;
text-transform: uppercase;
white-space: nowrap;
}
@media (max-width: 950px) {
.left-panel {
left: 12px;
width: 250px;
}
.right-panel {
right: 12px;
width: 270px;
}
.footer-note {
display: none;
}
}
@media (max-width: 680px) {
header {
top: 12px;
left: 12px;
right: 12px;
}
.subtitle,
.header-badges .badge:not(.live) {
display: none;
}
.left-panel {
top: auto;
bottom: 10px;
left: 10px;
width: calc(50% - 15px);
max-height: 43vh;
padding: 13px;
}
.right-panel {
top: 66px;
right: 10px;
width: calc(50% - 15px);
max-height: 43vh;
padding: 11px;
}
.panel-title,
.chart-wrap,
.parameters,
.model-note,
.range-scale {
display: none;
}
.control {
margin-bottom: 13px;
}
.state-number {
font-size: 27px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<header>
<div class="brand">
<div class="brand-mark"></div>
<div>
<h1>Thermal Phase Lab</h1>
<div class="subtitle">Idealized energy receiver / sample 01</div>
</div>
</div>
<div class="header-badges">
<div class="badge">Two-state model</div>
<div class="badge live"><span class="live-dot"></span>Live</div>
</div>
</header>
<aside class="panel left-panel">
<div class="eyebrow">Experiment controls</div>
<div class="panel-title">Energy input and material response</div>
<div class="control">
<div class="control-head">
<label for="power">Heater power</label>
<output id="powerOutput">320 W</output>
</div>
<input id="power" type="range" min="0" max="500" step="5" value="320" />
<div class="range-scale"><span>0 W</span><span>500 W</span></div>
</div>
<div class="control">
<div class="control-head">
<label for="ambient">Ambient temperature</label>
<output id="ambientOutput">22 °C</output>
</div>
<input id="ambient" type="range" min="5" max="40" step="1" value="22" />
<div class="range-scale"><span>5 °C</span><span>40 °C</span></div>
</div>
<div class="control">
<div class="control-head">
<label for="loss">Heat-loss coefficient</label>
<output id="lossOutput">1.80 W/K</output>
</div>
<input id="loss" type="range" min="0.4" max="5" step="0.1" value="1.8" />
<div class="range-scale"><span>Insulated</span><span>5.0 W/K</span></div>
</div>
<div class="control phase">
<div class="control-head">
<label for="tau">Phase response time τ</label>
<output id="tauOutput">8.0 s</output>
</div>
<input id="tau" type="range" min="1" max="20" step="0.5" value="8" />
<div class="range-scale"><span>Fast</span><span>Slow</span></div>
</div>
<div class="divider"></div>
<div class="preset-title">Power presets</div>
<div class="presets">
<button data-power="0">Off</button>
<button data-power="120">Low</button>
<button data-power="320">Nominal</button>
<button data-power="500">High</button>
<button data-power="220">Steady</button>
<button data-power="410">Rapid</button>
</div>
<div class="buttons">
<button id="pauseButton" class="primary">Pause</button>
<button id="resetButton">Reset</button>
</div>
<div class="divider"></div>
<div class="model-note">
<strong>Model:</strong> temperature integrates net heat, while liquid fraction
independently approaches its melting-window equilibrium. Latent heat couples
the two states without merging their readouts.
</div>
</aside>
<aside class="panel right-panel">
<div class="eyebrow">State telemetry</div>
<div class="panel-title">Independent response channels</div>
<section class="state-card" id="temperatureCard">
<div class="state-top">
<div class="state-label">Temperature</div>
<div class="state-tag" id="temperatureTrend">Heating</div>
</div>
<div class="state-number">
<span id="temperatureValue">18.0</span><small>°C</small>
</div>
<div class="meter"><div class="meter-fill" id="temperatureFill"></div></div>
<div class="meter-labels"><span>0 °C</span><span>180 °C</span></div>
</section>
<section class="state-card" id="phaseCard">
<div class="state-top">
<div class="state-label">Liquid phase fraction</div>
<div class="state-tag" id="phaseState">Solid</div>
</div>
<div class="state-number">
<span id="phaseValue">0</span><small>% liquid</small>
</div>
<div class="meter"><div class="meter-fill" id="phaseFill"></div></div>
<div class="meter-labels"><span>Solid</span><span>Liquid</span></div>
</section>
<div class="chart-wrap">
<div class="chart-head">
<span>60 s history</span>
<div class="chart-legend">
<span class="legend-item">Temperature</span>
<span class="legend-item phase">Phase</span>
</div>
</div>
<canvas id="historyChart"></canvas>
</div>
<div class="parameters">
<div class="eyebrow">Sample parameters</div>
<div class="parameter-grid">
<div>
<div class="parameter-name">Mass</div>
<div class="parameter-value emphasis">0.250 kg</div>
</div>
<div>
<div class="parameter-name">Specific heat</div>
<div class="parameter-value">900 J/kg·K</div>
</div>
<div>
<div class="parameter-name">Latent heat</div>
<div class="parameter-value">80.0 kJ/kg</div>
</div>
<div>
<div class="parameter-name">Melting point</div>
<div class="parameter-value">58 °C</div>
</div>
<div>
<div class="parameter-name">Phase band</div>
<div class="parameter-value">± 10 °C</div>
</div>
<div>
<div class="parameter-name">Current input</div>
<div class="parameter-value" id="currentInput">320 W</div>
</div>
<div>
<div class="parameter-name">Enthalpy gain</div>
<div class="parameter-value" id="enthalpyGain">0.0 kJ</div>
</div>
<div>
<div class="parameter-name">Elapsed time</div>
<div class="parameter-value" id="elapsedTime">0.0 s</div>
</div>
</div>
</div>
</aside>
<div class="footer-note">
Drag to orbit · Shift-drag or right-drag to pan · Scroll to zoom
</div>
</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 application state
// ------------------------------------------------------------
const PARAMETERS = Object.freeze({
mass: 0.250, // kg
specificHeat: 900, // J/(kg K)
latentHeat: 80000, // J/kg
meltingPoint: 58, // deg C
phaseBand: 10, // deg C
initialTemperature: 18
});
const controls = {
power: 320,
ambient: 22,
loss: 1.8,
tau: 8
};
let temperature = PARAMETERS.initialTemperature;
let phaseFraction = 0;
let running = true;
let simulationTime = 0;
let historyAccumulator = 0;
const history = [];
const clamp = THREE.MathUtils.clamp;
function smoothstep(value, min, max) {
const x = clamp((value - min) / (max - min), 0, 1);
return x * x * (3 - 2 * x);
}
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const sceneHost = document.getElementById("scene");
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050b14);
scene.fog = new THREE.FogExp2(0x050b14, 0.038);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Direct camera controls: no external controls module.
const cameraTarget = new THREE.Vector3(0, 1.0, 0);
let cameraTheta = 0.68;
let cameraPhi = 1.10;
let cameraRadius = 6.8;
let dragging = false;
let dragButton = 0;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragButton = event.button;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (event.shiftKey || dragButton === 2) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const up = new THREE.Vector3()
.crossVectors(right, forward)
.normalize();
const panScale = cameraRadius * 0.0014;
cameraTarget.addScaledVector(right, -dx * panScale);
cameraTarget.addScaledVector(up, dy * panScale);
} else {
cameraTheta -= dx * 0.006;
cameraPhi = clamp(cameraPhi - dy * 0.006, 0.28, 1.46);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
3.8,
12
);
updateCamera();
},
{ passive: false }
);
// ------------------------------------------------------------
// Lighting and environment
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x8fc8ed, 0x07101b, 1.25));
const keyLight = new THREE.DirectionalLight(0xbdeaff, 2.4);
keyLight.position.set(-4, 6, 4);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.camera.left = -4;
keyLight.shadow.camera.right = 4;
keyLight.shadow.camera.top = 4;
keyLight.shadow.camera.bottom = -4;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x876fff, 1.15);
rimLight.position.set(4, 2, -4);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x07101c,
roughness: 0.91,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(16, 32, 0x28435b, 0x11273a);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
// Sparse background points.
const starPositions = new Float32Array(330 * 3);
for (let i = 0; i < 330; i++) {
starPositions[i * 3] = (Math.random() - 0.5) * 18;
starPositions[i * 3 + 1] = Math.random() * 8;
starPositions[i * 3 + 2] = -3 - Math.random() * 14;
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.BufferAttribute(starPositions, 3)
);
scene.add(
new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x8fc9e8,
size: 0.025,
transparent: true,
opacity: 0.42,
depthWrite: false
})
)
);
// ------------------------------------------------------------
// Pedestal
// ------------------------------------------------------------
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.52, 1.7, 0.25, 48),
new THREE.MeshStandardMaterial({
color: 0x101d2b,
roughness: 0.38,
metalness: 0.68
})
);
pedestal.position.y = 0.125;
pedestal.receiveShadow = true;
pedestal.castShadow = true;
scene.add(pedestal);
const pedestalTop = new THREE.Mesh(
new THREE.CylinderGeometry(1.36, 1.43, 0.035, 48),
new THREE.MeshStandardMaterial({
color: 0x15283a,
roughness: 0.25,
metalness: 0.78
})
);
pedestalTop.position.y = 0.267;
pedestalTop.receiveShadow = true;
scene.add(pedestalTop);
for (let index = 0; index < 2; index++) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(1.58 + index * 0.12, 0.012, 5, 96),
new THREE.MeshBasicMaterial({
color: index === 0 ? 0x54ddff : 0x41627b,
transparent: true,
opacity: index === 0 ? 0.42 : 0.25
})
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.025 + index * 0.018;
scene.add(ring);
}
// ------------------------------------------------------------
// Low-poly ideal sample
// ------------------------------------------------------------
const sampleGroup = new THREE.Group();
sampleGroup.position.y = 1.08;
scene.add(sampleGroup);
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0x54c9ef,
emissive: 0x123a50,
emissiveIntensity: 0.25,
roughness: 0.32,
metalness: 0.25,
flatShading: true
});
const core = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.88, 1),
coreMaterial
);
core.castShadow = true;
core.receiveShadow = true;
sampleGroup.add(core);
const liquidMaterial = new THREE.MeshStandardMaterial({
color: 0xff743d,
emissive: 0xff4f1f,
emissiveIntensity: 0.45,
roughness: 0.2,
metalness: 0.18,
transparent: true,
opacity: 0,
depthWrite: false,
flatShading: true
});
const liquidShell = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.91, 1),
liquidMaterial
);
liquidShell.castShadow = true;
sampleGroup.add(liquidShell);
const wireMaterial = new THREE.MeshBasicMaterial({
color: 0xc8f6ff,
wireframe: true,
transparent: true,
opacity: 0.34
});
const wire = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.935, 1),
wireMaterial
);
sampleGroup.add(wire);
// Separate low-poly pieces visualize the growing liquid fraction.
const facetGeometry = new THREE.TetrahedronGeometry(0.19, 0);
const facetMaterial = new THREE.MeshStandardMaterial({
color: 0xff9b50,
emissive: 0xff4f18,
emissiveIntensity: 0.55,
roughness: 0.2,
metalness: 0.15,
transparent: true,
opacity: 0.82,
flatShading: true
});
const facets = [];
for (let index = 0; index < 22; index++) {
const theta = Math.random() * Math.PI * 2;
const z = Math.random() * 2 - 1;
const radius = 0.64 + Math.random() * 0.28;
const planarRadius = Math.sqrt(Math.max(0, 1 - z * z));
const facet = new THREE.Mesh(facetGeometry, facetMaterial);
facet.position.set(
Math.cos(theta) * planarRadius * radius,
z * radius,
Math.sin(theta) * planarRadius * radius
);
facet.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
const sizeVariation = 0.65 + Math.random() * 0.55;
facet.userData.sizeVariation = sizeVariation;
facet.userData.phaseOffset = Math.random() * Math.PI * 2;
facet.visible = false;
facet.castShadow = true;
facets.push(facet);
sampleGroup.add(facet);
}
// Temperature-only halo below the sample.
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x54ddff,
transparent: true,
opacity: 0.65
});
const temperatureHalo = new THREE.Mesh(
new THREE.TorusGeometry(1.15, 0.018, 6, 80),
haloMaterial
);
temperatureHalo.rotation.x = Math.PI / 2;
temperatureHalo.position.y = 0.33;
scene.add(temperatureHalo);
// ------------------------------------------------------------
// Energy emitter and beam
// ------------------------------------------------------------
const emitterGroup = new THREE.Group();
emitterGroup.position.y = 3.42;
scene.add(emitterGroup);
const emitter = new THREE.Mesh(
new THREE.CylinderGeometry(0.47, 0.58, 0.22, 32),
new THREE.MeshStandardMaterial({
color: 0x263a4c,
roughness: 0.3,
metalness: 0.82
})
);
emitter.castShadow = true;
emitterGroup.add(emitter);
const emitterRing = new THREE.Mesh(
new THREE.TorusGeometry(0.43, 0.045, 8, 48),
new THREE.MeshStandardMaterial({
color: 0xffa04a,
emissive: 0xff6427,
emissiveIntensity: 1.4,
roughness: 0.25,
metalness: 0.45
})
);
emitterRing.rotation.x = Math.PI / 2;
emitterRing.position.y = -0.12;
emitterGroup.add(emitterRing);
const beamMaterial = new THREE.MeshBasicMaterial({
color: 0xffa04a,
transparent: true,
opacity: 0.09,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending
});
const beam = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.7, 1.78, 32, 1, true),
beamMaterial
);
beam.position.y = 2.47;
scene.add(beam);
const heaterLight = new THREE.PointLight(0xff8b4c, 14, 6, 2);
heaterLight.position.set(0, 3.05, 0);
scene.add(heaterLight);
// Low-poly incoming energy particles.
const sparkGeometry = new THREE.TetrahedronGeometry(0.045, 0);
const sparkMaterial = new THREE.MeshBasicMaterial({
color: 0xffc15c,
transparent: true,
opacity: 0.88
});
const sparkCount = 30;
const sparks = new THREE.InstancedMesh(
sparkGeometry,
sparkMaterial,
sparkCount
);
sparks.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
sparks.frustumCulled = false;
scene.add(sparks);
const sparkSeeds = Array.from({ length: sparkCount }, () => ({
angle: Math.random() * Math.PI * 2,
offset: Math.random(),
speed: 0.24 + Math.random() * 0.45,
spin: Math.random() * Math.PI * 2
}));
const sparkDummy = new THREE.Object3D();
// ------------------------------------------------------------
// UI references
// ------------------------------------------------------------
const ui = {
power: document.getElementById("power"),
ambient: document.getElementById("ambient"),
loss: document.getElementById("loss"),
tau: document.getElementById("tau"),
powerOutput: document.getElementById("powerOutput"),
ambientOutput: document.getElementById("ambientOutput"),
lossOutput: document.getElementById("lossOutput"),
tauOutput: document.getElementById("tauOutput"),
temperatureValue: document.getElementById("temperatureValue"),
temperatureFill: document.getElementById("temperatureFill"),
temperatureTrend: document.getElementById("temperatureTrend"),
phaseValue: document.getElementById("phaseValue"),
phaseFill: document.getElementById("phaseFill"),
phaseState: document.getElementById("phaseState"),
phaseCard: document.getElementById("phaseCard"),
currentInput: document.getElementById("currentInput"),
enthalpyGain: document.getElementById("enthalpyGain"),
elapsedTime: document.getElementById("elapsedTime"),
pauseButton: document.getElementById("pauseButton"),
resetButton: document.getElementById("resetButton"),
historyChart: document.getElementById("historyChart")
};
function updateRangeFill(input) {
const percent =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--pct", `${percent}%`);
}
function syncControls() {
controls.power = Number(ui.power.value);
controls.ambient = Number(ui.ambient.value);
controls.loss = Number(ui.loss.value);
controls.tau = Number(ui.tau.value);
ui.powerOutput.textContent = `${controls.power.toFixed(0)} W`;
ui.ambientOutput.textContent = `${controls.ambient.toFixed(0)} °C`;
ui.lossOutput.textContent = `${controls.loss.toFixed(2)} W/K`;
ui.tauOutput.textContent = `${controls.tau.toFixed(1)} s`;
updateRangeFill(ui.power);
updateRangeFill(ui.ambient);
updateRangeFill(ui.loss);
updateRangeFill(ui.tau);
}
[ui.power, ui.ambient, ui.loss, ui.tau].forEach((input) => {
input.addEventListener("input", syncControls);
});
document.querySelectorAll("[data-power]").forEach((button) => {
button.addEventListener("click", () => {
ui.power.value = button.dataset.power;
syncControls();
});
});
ui.pauseButton.addEventListener("click", () => {
running = !running;
ui.pauseButton.textContent = running ? "Pause" : "Resume";
ui.pauseButton.classList.toggle("primary", running);
});
ui.resetButton.addEventListener("click", () => {
temperature = PARAMETERS.initialTemperature;
phaseFraction = 0;
simulationTime = 0;
historyAccumulator = 0;
history.length = 0;
});
syncControls();
// ------------------------------------------------------------
// Physics
// ------------------------------------------------------------
function simulate(dt) {
// Equilibrium liquid fraction in the idealized melting window.
const equilibriumPhase = smoothstep(
temperature,
PARAMETERS.meltingPoint - PARAMETERS.phaseBand,
PARAMETERS.meltingPoint + PARAMETERS.phaseBand
);
// Independent first-order phase state.
const response = 1 - Math.exp(-dt / controls.tau);
const nextPhase =
phaseFraction +
(equilibriumPhase - phaseFraction) * response;
// Conductive loss may become heat gain below ambient.
const heatLoss = controls.loss * (temperature - controls.ambient);
// Latent heat couples the phase change back into the thermal state.
const phaseRate = (nextPhase - phaseFraction) / Math.max(dt, 0.0001);
const latentPower =
PARAMETERS.mass * PARAMETERS.latentHeat * phaseRate;
const netHeat =
controls.power -
heatLoss -
latentPower;
temperature +=
(netHeat /
(PARAMETERS.mass * PARAMETERS.specificHeat)) *
dt;
phaseFraction = clamp(nextPhase, 0, 1);
simulationTime += dt;
}
// ------------------------------------------------------------
// Telemetry
// ------------------------------------------------------------
const coolColor = new THREE.Color(0x54ddff);
const warmColor = new THREE.Color(0xffd166);
const hotColor = new THREE.Color(0xff5f45);
const currentTemperatureColor = new THREE.Color();
function temperatureColor(normalized) {
if (normalized < 0.5) {
return currentTemperatureColor
.copy(coolColor)
.lerp(warmColor, normalized * 2);
}
return currentTemperatureColor
.copy(warmColor)
.lerp(hotColor, (normalized - 0.5) * 2);
}
function updateTelemetry() {
const tempNormalized = clamp(temperature / 180, 0, 1);
const tempColor = temperatureColor(tempNormalized);
ui.temperatureValue.textContent = temperature.toFixed(1);
ui.temperatureFill.style.width = `${tempNormalized * 100}%`;
ui.temperatureFill.style.background = `#${tempColor.getHexString()}`;
ui.temperatureFill.style.boxShadow = `0 0 13px #${tempColor.getHexString()}`;
const equilibriumPhase = smoothstep(
temperature,
PARAMETERS.meltingPoint - PARAMETERS.phaseBand,
PARAMETERS.meltingPoint + PARAMETERS.phaseBand
);
const phaseRate =
(equilibriumPhase - phaseFraction) /
Math.max(controls.tau, 0.01);
ui.temperatureTrend.textContent =
phaseRate > 0.015
? "Heating + melting"
: temperature > controls.ambient + 0.4
? "Heating"
: temperature < controls.ambient - 0.4
? "Cooling"
: "Stable";
ui.phaseValue.textContent = Math.round(phaseFraction * 100);
ui.phaseFill.style.width = `${phaseFraction * 100}%`;
if (phaseFraction < 0.02) {
ui.phaseState.textContent = "Solid";
ui.phaseCard.style.setProperty("--state", "#a78bfa");
} else if (phaseFraction > 0.98) {
ui.phaseState.textContent = "Liquid";
ui.phaseCard.style.setProperty("--state", "#ff8a4c");
} else {
ui.phaseState.textContent = "Two-phase";
ui.phaseCard.style.setProperty("--state", "#ffd166");
}
const enthalpyGain =
PARAMETERS.mass *
PARAMETERS.specificHeat *
(temperature - PARAMETERS.initialTemperature) +
PARAMETERS.mass * PARAMETERS.latentHeat * phaseFraction;
ui.currentInput.textContent = `${controls.power.toFixed(0)} W`;
ui.enthalpyGain.textContent = `${(enthalpyGain / 1000).toFixed(1)} kJ`;
ui.elapsedTime.textContent = `${simulationTime.toFixed(1)} s`;
// Sample appearance.
const coreScale = 1 - phaseFraction * 0.14;
core.scale.setScalar(coreScale);
liquidMaterial.opacity = phaseFraction * 0.72;
liquidShell.scale.setScalar(1 + phaseFraction * 0.025);
liquidMaterial.emissiveIntensity = 0.3 + phaseFraction * 0.55;
coreMaterial.emissive.copy(tempColor);
coreMaterial.emissiveIntensity = 0.08 + tempNormalized * 0.32;
wireMaterial.color.copy(tempColor).lerp(new THREE.Color(0xffffff), 0.42);
wireMaterial.opacity = 0.22 + phaseFraction * 0.22;
facets.forEach((facet) => {
facet.visible = phaseFraction > 0.01;
const scale =
phaseFraction *
facet.userData.sizeVariation *
(0.9 + Math.sin(simulationTime * 1.6 + facet.userData.phaseOffset) * 0.1);
facet.scale.setScalar(Math.max(0.001, scale));
});
// Temperature halo.
haloMaterial.color.copy(tempColor);
haloMaterial.opacity = 0.25 + tempNormalized * 0.65;
temperatureHalo.scale.setScalar(
1 + Math.sin(simulationTime * 2.1) * 0.012
);
// Energy beam.
beamMaterial.color.copy(tempColor).lerp(new THREE.Color(0xff8a3d), 0.35);
beamMaterial.opacity =
running && controls.power > 0
? 0.025 + (controls.power / 500) * 0.12
: 0.008;
emitterRing.material.emissiveIntensity =
controls.power > 0 ? 0.6 + (controls.power / 500) * 2.1 : 0.2;
heaterLight.color.copy(tempColor);
heaterLight.intensity =
controls.power > 0
? 3 + (controls.power / 500) * 22
: 0.5;
sparkMaterial.opacity = running ? 0.35 + (controls.power / 500) * 0.65 : 0.1;
drawHistory();
}
// ------------------------------------------------------------
// History graph
// ------------------------------------------------------------
const chartContext = ui.historyChart.getContext("2d");
function drawHistory() {
const rect = ui.historyChart.getBoundingClientRect();
if (rect.width < 1 || rect.height < 1) return;
const dpr = Math.min(window.devicePixelRatio, 2);
const targetWidth = Math.round(rect.width * dpr);
const targetHeight = Math.round(rect.height * dpr);
if (
ui.historyChart.width !== targetWidth ||
ui.historyChart.height !== targetHeight
) {
ui.historyChart.width = targetWidth;
ui.historyChart.height = targetHeight;
}
chartContext.setTransform(dpr, 0, 0, dpr, 0, 0);
chartContext.clearRect(0, 0, rect.width, rect.height);
const padding = {
left: 5,
right: 5,
top: 6,
bottom: 7
};
const width = rect.width - padding.left - padding.right;
const height = rect.height - padding.top - padding.bottom;
chartContext.strokeStyle = "rgba(120, 160, 185, 0.10)";
chartContext.lineWidth = 1;
for (let i = 0; i <= 3; i++) {
const y = padding.top + (height * i) / 3;
chartContext.beginPath();
chartContext.moveTo(padding.left, y);
chartContext.lineTo(padding.left + width, y);
chartContext.stroke();
}
if (history.length < 2) return;
const visibleHistory = history.slice(-Math.ceil(60 / 0.15));
function plotLine(color, valueFunction) {
chartContext.strokeStyle = color;
chartContext.lineWidth = 1.7;
chartContext.beginPath();
visibleHistory.forEach((sample, index) => {
const x =
padding.left +
(index / Math.max(1, visibleHistory.length - 1)) * width;
const normalized = clamp(valueFunction(sample), 0, 1);
const y = padding.top + (1 - normalized) * height;
if (index === 0) chartContext.moveTo(x, y);
else chartContext.lineTo(x, y);
});
chartContext.stroke();
}
plotLine("#54ddff", (sample) => sample.temperature / 180);
plotLine("#ff8a4c", (sample) => sample.phaseFraction);
}
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
let uiAccumulator = 0;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (running) {
simulate(dt);
}
historyAccumulator += dt;
if (historyAccumulator >= 0.15) {
historyAccumulator %= 0.15;
history.push({
time: simulationTime,
temperature,
phaseFraction
});
while (
history.length > 2 &&
simulationTime - history[0].time > 60
) {
history.shift();
}
}
sampleGroup.rotation.y += dt * 0.18;
sampleGroup.rotation.x = Math.sin(clock.elapsedTime * 0.35) * 0.025;
// Animated low-poly energy fragments.
const visibleSparkCount = Math.max(
3,
Math.round((controls.power / 500) * sparkCount)
);
for (let index = 0; index < sparkCount; index++) {
const seed = sparkSeeds[index];
const progress =
(seed.offset + clock.elapsedTime * seed.speed) % 1;
const radius = 0.12 + progress * 0.56;
const angle = seed.angle + progress * 1.7;
sparkDummy.position.set(
Math.cos(angle) * radius,
3.18 - progress * 1.72,
Math.sin(angle) * radius
);
sparkDummy.rotation.set(
seed.spin + progress * 5,
seed.spin * 0.6 + progress * 4,
progress * 6
);
const sparkScale =
index < visibleSparkCount ? 0.65 + Math.random() * 0.4 : 0.001;
sparkDummy.scale.setScalar(sparkScale);
sparkDummy.updateMatrix();
sparks.setMatrixAt(index, sparkDummy.matrix);
}
sparks.instanceMatrix.needsUpdate = true;
uiAccumulator += dt;
if (uiAccumulator >= 0.08) {
uiAccumulator = 0;
updateTelemetry();
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
// ------------------------------------------------------------
// Resize handling
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
drawHistory();
});
</script>
</body>
</html>
```