Prompt specification
Build or customize the Three.js 3D scene "Chocolate melt and set bowls" 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: Chocolate melt and set bowls
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/chocolate-melt-and-set-bowls-x0090
Scene Description Prompt:
Animate voxel chocolate melting into a pool and setting in a mold, with a reversible, simplified temperature timeline. Three.js from cdnjs; standalone HTML.
Working Three.js Starter (chocolate-melt-and-set-bowls-x0090):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Chocolate — Melt / Set</title>
<style>
:root {
color-scheme: dark;
--cream: #f4e8d8;
--muted: #b8a89b;
--orange: #e8803c;
--orange-bright: #ffad63;
--blue: #74b7d5;
--glass: rgba(25, 18, 15, 0.78);
--line: rgba(255, 225, 198, 0.14);
--progress: 50%;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #100c0a;
color: var(--cream);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#app {
position: fixed;
inset: 0;
}
#app canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.header {
position: absolute;
top: 30px;
left: 34px;
max-width: 430px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 13px;
color: var(--orange-bright);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 1px;
content: "";
background: var(--orange);
box-shadow: 0 0 12px var(--orange);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 5vw, 68px);
font-weight: 400;
line-height: 0.94;
letter-spacing: -0.045em;
text-shadow: 0 4px 30px rgba(0, 0, 0, 0.35);
}
h1 em {
color: #d47a45;
font-weight: 400;
}
.subtitle {
max-width: 330px;
margin: 15px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.65;
}
.temperature-card {
position: absolute;
top: 30px;
right: 34px;
width: 196px;
padding: 18px 18px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--glass);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.32),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
}
.temperature-top {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 13px var(--orange);
}
.temperature-value {
display: flex;
align-items: flex-start;
margin: 7px 0 2px;
font-variant-numeric: tabular-nums;
}
#temperature {
font-family: Georgia, "Times New Roman", serif;
font-size: 50px;
line-height: 1;
letter-spacing: -0.06em;
}
.degrees {
margin: 4px 0 0 5px;
color: var(--orange-bright);
font-size: 20px;
}
#phase {
min-height: 16px;
margin-top: 7px;
color: var(--orange-bright);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.direction {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 15px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#direction-label {
color: var(--cream);
}
.timeline-panel {
position: absolute;
right: 34px;
bottom: 30px;
left: 34px;
max-width: 1120px;
margin: 0 auto;
padding: 19px 22px 17px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--glass);
box-shadow:
0 20px 70px rgba(0, 0, 0, 0.35),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.timeline-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.timeline-title {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.timeline-title strong {
margin-right: 9px;
color: var(--cream);
}
.controls {
display: flex;
align-items: center;
gap: 8px;
}
.control {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 33px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: var(--cream);
cursor: pointer;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.control:hover {
border-color: rgba(255, 190, 140, 0.35);
background: rgba(255, 255, 255, 0.09);
transform: translateY(-1px);
}
.control.primary {
min-width: 82px;
border-color: rgba(232, 128, 60, 0.38);
background: rgba(232, 128, 60, 0.13);
color: #ffc18f;
}
.control-icon {
font-size: 13px;
line-height: 1;
}
.range-wrap {
position: relative;
padding: 0 2px;
}
input[type="range"] {
width: 100%;
height: 20px;
margin: 0;
appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 4px;
border-radius: 999px;
background:
linear-gradient(
90deg,
var(--orange) 0%,
var(--orange-bright) var(--progress),
rgba(255, 255, 255, 0.12) var(--progress),
rgba(255, 255, 255, 0.12) 100%
);
}
input[type="range"]::-moz-range-track {
height: 4px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.12);
}
input[type="range"]::-moz-range-progress {
height: 4px;
border-radius: 999px;
background: linear-gradient(90deg, var(--orange), var(--orange-bright));
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
margin-top: -6px;
appearance: none;
border: 3px solid #281913;
border-radius: 50%;
background: #ffd2a9;
box-shadow:
0 0 0 1px rgba(255, 210, 169, 0.5),
0 0 16px rgba(255, 150, 80, 0.75);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #281913;
border-radius: 50%;
background: #ffd2a9;
box-shadow:
0 0 0 1px rgba(255, 210, 169, 0.5),
0 0 16px rgba(255, 150, 80, 0.75);
}
.markers {
display: grid;
grid-template-columns: repeat(4, 1fr);
margin-top: 8px;
color: #8f8177;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.markers span:nth-child(2),
.markers span:nth-child(3) {
text-align: center;
}
.markers span:last-child {
text-align: right;
}
.hint {
position: absolute;
bottom: 151px;
left: 50%;
padding: 7px 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
background: rgba(12, 8, 7, 0.45);
color: rgba(244, 232, 216, 0.54);
font-size: 9px;
letter-spacing: 0.08em;
transform: translateX(-50%);
backdrop-filter: blur(10px);
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #100c0a;
color: #c7aa92;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
.header {
top: 20px;
left: 20px;
}
h1 {
font-size: 42px;
}
.subtitle {
display: none;
}
.temperature-card {
top: 18px;
right: 18px;
width: 148px;
padding: 13px;
}
#temperature {
font-size: 38px;
}
.timeline-panel {
right: 14px;
bottom: 14px;
left: 14px;
padding: 15px;
}
.timeline-header {
align-items: flex-start;
gap: 12px;
}
.timeline-title strong {
display: block;
margin-bottom: 4px;
}
.control {
padding: 0 10px;
}
#reverse-label {
display: none;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="loading">Preparing the tempering chamber</div>
<div class="overlay">
<header class="header">
<div class="eyebrow">Thermal chocolate lab</div>
<h1>Voxel <em>Melt</em><br />& Set</h1>
<p class="subtitle">
A reversible chocolate phase study. Heat the voxel bar into a glossy
pool, then cool it back into its molded geometry.
</p>
</header>
<aside class="temperature-card">
<div class="temperature-top">
<span>Core temperature</span>
<span class="live-dot"></span>
</div>
<div class="temperature-value">
<span id="temperature">31.0</span>
<span class="degrees">°C</span>
</div>
<div id="phase">Melting</div>
<div class="direction">
<span>Timeline</span>
<span id="direction-label">Heating</span>
</div>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<section class="timeline-panel">
<div class="timeline-header">
<div class="timeline-title">
<strong>Temperature timeline</strong>
18°C solid — 42°C fully molten
</div>
<div class="controls">
<button id="play" class="control primary" type="button">
<span id="play-icon" class="control-icon">Ⅱ</span>
<span id="play-label">Pause</span>
</button>
<button id="reverse" class="control" type="button">
<span class="control-icon">⇄</span>
<span id="reverse-label">Reverse cycle</span>
</button>
</div>
</div>
<div class="range-wrap">
<input
id="timeline"
type="range"
min="18"
max="42"
step="0.1"
value="31"
aria-label="Chocolate temperature"
/>
</div>
<div class="markers" aria-hidden="true">
<span>18° · Solid</span>
<span>28° · Soft</span>
<span>34° · Melt</span>
<span>42° · Liquid</span>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const app = document.querySelector("#app");
const loading = document.querySelector("#loading");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x100c0a);
scene.fog = new THREE.Fog(0x100c0a, 15, 34);
const camera = new THREE.PerspectiveCamera(
37,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
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;
app.appendChild(renderer.domElement);
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D voxel chocolate melting and setting scene"
);
// A generated equirectangular studio environment gives the liquid and
// metal clear reflections without requiring any external assets.
const envCanvas = document.createElement("canvas");
envCanvas.width = 1024;
envCanvas.height = 512;
const envContext = envCanvas.getContext("2d");
const envGradient = envContext.createLinearGradient(0, 0, 0, 512);
envGradient.addColorStop(0, "#47362c");
envGradient.addColorStop(0.45, "#17100d");
envGradient.addColorStop(1, "#050403");
envContext.fillStyle = envGradient;
envContext.fillRect(0, 0, 1024, 512);
function paintGlow(x, y, radius, color) {
const glow = envContext.createRadialGradient(
x, y, 0,
x, y, radius
);
glow.addColorStop(0, color);
glow.addColorStop(1, "rgba(0,0,0,0)");
envContext.fillStyle = glow;
envContext.fillRect(x - radius, y - radius, radius * 2, radius * 2);
}
paintGlow(225, 140, 145, "rgba(255,225,190,0.95)");
paintGlow(680, 180, 170, "rgba(160,210,255,0.62)");
paintGlow(850, 105, 75, "rgba(255,135,65,0.72)");
envContext.fillStyle = "rgba(255,245,225,0.92)";
envContext.fillRect(145, 85, 170, 42);
envContext.fillStyle = "rgba(185,220,255,0.68)";
envContext.fillRect(620, 105, 115, 28);
const environment = new THREE.CanvasTexture(envCanvas);
environment.mapping = THREE.EquirectangularReflectionMapping;
environment.colorSpace = THREE.SRGBColorSpace;
scene.environment = environment;
// -------------------------------------------------------------------------
// Lighting and stage
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xffe4c9,
0x160b08,
1.35
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0dc, 3.7);
keyLight.position.set(6.5, 10, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const warmSpot = new THREE.SpotLight(
0xff8845,
85,
30,
Math.PI * 0.25,
0.7,
1.6
);
warmSpot.position.set(-7, 7, 4);
warmSpot.target.position.set(0, 0, 0);
scene.add(warmSpot, warmSpot.target);
const rimLight = new THREE.PointLight(0x75bde5, 42, 22, 2);
rimLight.position.set(6, 4, -6);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0x1b1512,
roughness: 0.88,
metalness: 0.03
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
const stage = new THREE.Mesh(
new THREE.CylinderGeometry(6.7, 6.9, 0.12, 96),
new THREE.MeshStandardMaterial({
color: 0x211916,
roughness: 0.7,
metalness: 0.15
})
);
stage.position.y = 0.035;
stage.receiveShadow = true;
scene.add(stage);
const stageRing = new THREE.Mesh(
new THREE.TorusGeometry(6.35, 0.025, 8, 160),
new THREE.MeshBasicMaterial({
color: 0xa5613b,
transparent: true,
opacity: 0.34
})
);
stageRing.rotation.x = Math.PI / 2;
stageRing.position.y = 0.105;
scene.add(stageRing);
// -------------------------------------------------------------------------
// Mold and voxel chocolate
// -------------------------------------------------------------------------
const COLS = 5;
const ROWS = 3;
const CELL = 1.45;
const GAP = 0.1;
const CUBE_HEIGHT = 0.64;
const FLOOR_TOP = 0.28;
const TOTAL_W = COLS * CELL + (COLS - 1) * GAP;
const TOTAL_D = ROWS * CELL + (ROWS - 1) * GAP;
const mold = new THREE.Group();
scene.add(mold);
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x262320,
roughness: 0.36,
metalness: 0.72
});
const railMaterial = new THREE.MeshStandardMaterial({
color: 0x77716b,
roughness: 0.27,
metalness: 0.86
});
const dividerMaterial = new THREE.MeshStandardMaterial({
color: 0x514b46,
roughness: 0.31,
metalness: 0.82
});
const innerFloorMaterial = new THREE.MeshStandardMaterial({
color: 0x30231d,
roughness: 0.5,
metalness: 0.42
});
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
addBox(
mold,
new THREE.Vector3(TOTAL_W + 0.95, 0.24, TOTAL_D + 0.95),
new THREE.Vector3(0, 0.13, 0),
baseMaterial
);
addBox(
mold,
new THREE.Vector3(TOTAL_W, 0.04, TOTAL_D),
new THREE.Vector3(0, 0.26, 0),
innerFloorMaterial,
false
);
const railHeight = 0.55;
const railY = FLOOR_TOP + railHeight * 0.5;
addBox(
mold,
new THREE.Vector3(TOTAL_W + 0.9, railHeight, 0.3),
new THREE.Vector3(0, railY, TOTAL_D * 0.5 + 0.2),
railMaterial
);
addBox(
mold,
new THREE.Vector3(TOTAL_W + 0.9, railHeight, 0.3),
new THREE.Vector3(0, railY, -TOTAL_D * 0.5 - 0.2),
railMaterial
);
addBox(
mold,
new THREE.Vector3(0.3, railHeight, TOTAL_D + 0.1),
new THREE.Vector3(TOTAL_W * 0.5 + 0.2, railY, 0),
railMaterial
);
addBox(
mold,
new THREE.Vector3(0.3, railHeight, TOTAL_D + 0.1),
new THREE.Vector3(-TOTAL_W * 0.5 - 0.2, railY, 0),
railMaterial
);
// Thin dividers define the final molded bar sections.
for (let column = 1; column < COLS; column++) {
const x =
-TOTAL_W * 0.5 +
column * (CELL + GAP) -
GAP * 0.5;
addBox(
mold,
new THREE.Vector3(0.045, 0.38, TOTAL_D),
new THREE.Vector3(x, FLOOR_TOP + 0.19, 0),
dividerMaterial
);
}
for (let row = 1; row < ROWS; row++) {
const z =
-TOTAL_D * 0.5 +
row * (CELL + GAP) -
GAP * 0.5;
addBox(
mold,
new THREE.Vector3(TOTAL_W, 0.38, 0.045),
new THREE.Vector3(0, FLOOR_TOP + 0.19, z),
dividerMaterial
);
}
// Decorative fasteners on the mold corners.
const screwMaterial = new THREE.MeshStandardMaterial({
color: 0xb5aaa0,
roughness: 0.2,
metalness: 0.95
});
const screwGeometry = new THREE.CylinderGeometry(
0.075,
0.075,
0.035,
20
);
for (const x of [-1, 1]) {
for (const z of [-1, 1]) {
const screw = new THREE.Mesh(screwGeometry, screwMaterial);
screw.position.set(
x * (TOTAL_W * 0.5 + 0.2),
FLOOR_TOP + railHeight + 0.012,
z * (TOTAL_D * 0.5 + 0.2)
);
screw.castShadow = true;
mold.add(screw);
}
}
const chocolateColors = [
0x512012,
0x481a0e,
0x5a2414,
0x43170c,
0x602717
];
const cubeGeometry = new THREE.BoxGeometry(
CELL - 0.08,
CUBE_HEIGHT,
CELL - 0.08
);
const cubeEdgeGeometry = new THREE.EdgesGeometry(cubeGeometry);
const guideGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(CELL - 0.025, CUBE_HEIGHT, CELL - 0.025)
);
const guideMaterial = new THREE.LineBasicMaterial({
color: 0xf1a164,
transparent: true,
opacity: 0.1,
depthWrite: false
});
const chocolateCubes = [];
for (let row = 0; row < ROWS; row++) {
for (let column = 0; column < COLS; column++) {
const x =
-TOTAL_W * 0.5 +
CELL * 0.5 +
column * (CELL + GAP);
const z =
-TOTAL_D * 0.5 +
CELL * 0.5 +
row * (CELL + GAP);
const material = new THREE.MeshPhysicalMaterial({
color:
chocolateColors[
(column + row * 2) % chocolateColors.length
],
roughness: 0.37,
metalness: 0,
clearcoat: 0.3,
clearcoatRoughness: 0.24,
envMapIntensity: 1.15
});
const cube = new THREE.Mesh(cubeGeometry, material);
cube.position.set(x, FLOOR_TOP + CUBE_HEIGHT * 0.5, z);
cube.castShadow = true;
cube.receiveShadow = true;
const edge = new THREE.LineSegments(
cubeEdgeGeometry,
new THREE.LineBasicMaterial({
color: 0xb66538,
transparent: true,
opacity: 0.24
})
);
cube.add(edge);
const guide = new THREE.LineSegments(
guideGeometry,
guideMaterial
);
guide.position.set(x, FLOOR_TOP + CUBE_HEIGHT * 0.5, z);
guide.renderOrder = 3;
mold.add(cube, guide);
chocolateCubes.push({
mesh: cube,
material,
edgeMaterial: edge.material,
x,
z,
phase: (column * 1.7 + row * 2.3) % (Math.PI * 2)
});
}
}
// -------------------------------------------------------------------------
// Liquid pool
// -------------------------------------------------------------------------
const poolMaterial = new THREE.MeshPhysicalMaterial({
color: 0x351006,
roughness: 0.12,
metalness: 0,
clearcoat: 1,
clearcoatRoughness: 0.055,
ior: 1.47,
reflectivity: 0.85,
envMapIntensity: 1.8,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
depthWrite: false
});
const poolBodyMaterial = poolMaterial.clone();
poolBodyMaterial.opacity = 0;
const poolBody = new THREE.Mesh(
new THREE.BoxGeometry(TOTAL_W - 0.1, 0.17, TOTAL_D - 0.1),
poolBodyMaterial
);
poolBody.position.y = FLOOR_TOP + 0.085;
poolBody.renderOrder = 1;
scene.add(poolBody);
const poolGeometry = new THREE.PlaneGeometry(
TOTAL_W - 0.12,
TOTAL_D - 0.12,
48,
30
);
poolGeometry.rotateX(-Math.PI / 2);
const pool = new THREE.Mesh(poolGeometry, poolMaterial);
pool.position.y = FLOOR_TOP + 0.18;
pool.renderOrder = 2;
scene.add(pool);
// -------------------------------------------------------------------------
// Subtle heat particles / steam
// -------------------------------------------------------------------------
const particleCanvas = document.createElement("canvas");
particleCanvas.width = 64;
particleCanvas.height = 64;
const particleContext = particleCanvas.getContext("2d");
const particleGradient = particleContext.createRadialGradient(
32, 32, 0,
32, 32, 32
);
particleGradient.addColorStop(0, "rgba(255,235,215,0.9)");
particleGradient.addColorStop(0.35, "rgba(220,190,165,0.35)");
particleGradient.addColorStop(1, "rgba(200,170,145,0)");
particleContext.fillStyle = particleGradient;
particleContext.fillRect(0, 0, 64, 64);
const particleTexture = new THREE.CanvasTexture(particleCanvas);
particleTexture.colorSpace = THREE.SRGBColorSpace;
const PARTICLE_COUNT = 44;
const particlePositions = new Float32Array(PARTICLE_COUNT * 3);
const particleSeeds = [];
for (let index = 0; index < PARTICLE_COUNT; index++) {
const x = (Math.random() - 0.5) * (TOTAL_W - 0.5);
const z = (Math.random() - 0.5) * (TOTAL_D - 0.5);
particlePositions[index * 3] = x;
particlePositions[index * 3 + 1] = 0.8;
particlePositions[index * 3 + 2] = z;
particleSeeds.push({
x,
z,
offset: Math.random(),
drift: 0.5 + Math.random() * 0.8
});
}
const particleGeometry = new THREE.BufferGeometry();
particleGeometry.setAttribute(
"position",
new THREE.BufferAttribute(particlePositions, 3)
);
const particleMaterial = new THREE.PointsMaterial({
color: 0xe7c9ae,
size: 0.18,
map: particleTexture,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.NormalBlending
});
const particles = new THREE.Points(
particleGeometry,
particleMaterial
);
scene.add(particles);
// -------------------------------------------------------------------------
// Camera controls implemented without addons
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.72,
pitch: 0.56,
radius: 13.2,
target: new THREE.Vector3(0, 0.48, 0)
};
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(yaw) * Math.cos(pitch),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(yaw) * Math.cos(pitch)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = 0.72;
cameraState.pitch = 0.56;
cameraState.radius = 13.2;
cameraState.target.set(0, 0.48, 0);
updateCamera();
}
updateCamera();
let pointerActive = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerActive = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
0.2,
1.18
);
updateCamera();
});
function releasePointer(event) {
pointerActive = false;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8,
21
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener(
"contextmenu",
(event) => event.preventDefault()
);
// -------------------------------------------------------------------------
// Reversible temperature timeline
// -------------------------------------------------------------------------
const MIN_TEMP = 18;
const MAX_TEMP = 42;
const reducedMotion = matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let temperature = 31;
let direction = 1;
let playing = !reducedMotion;
const temperatureElement = document.querySelector("#temperature");
const phaseElement = document.querySelector("#phase");
const directionElement = document.querySelector("#direction-label");
const timelineElement = document.querySelector("#timeline");
const playButton = document.querySelector("#play");
const playIcon = document.querySelector("#play-icon");
const playLabel = document.querySelector("#play-label");
const reverseButton = document.querySelector("#reverse");
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smoothstep(min, max, value) {
const x = clamp((value - min) / (max - min), 0, 1);
return x * x * (3 - 2 * x);
}
function updatePlayButton() {
playIcon.textContent = playing ? "Ⅱ" : "▶";
playLabel.textContent = playing ? "Pause" : "Play";
playButton.setAttribute(
"aria-label",
playing ? "Pause temperature timeline" : "Play temperature timeline"
);
}
function updateTimelineUI() {
const normalized =
(temperature - MIN_TEMP) / (MAX_TEMP - MIN_TEMP);
timelineElement.value = temperature.toFixed(1);
timelineElement.style.setProperty(
"--progress",
`${normalized * 100}%`
);
temperatureElement.textContent = temperature.toFixed(1);
if (temperature < 22.5) {
phaseElement.textContent =
direction < 0 ? "Set / Solid" : "Solid / Set";
} else if (temperature < 30) {
phaseElement.textContent =
direction < 0 ? "Setting" : "Softening";
} else if (temperature < 36) {
phaseElement.textContent =
direction < 0 ? "Setting" : "Melting";
} else {
phaseElement.textContent = "Liquid / Pour";
}
directionElement.textContent =
direction > 0 ? "Heating" : "Cooling";
updatePlayButton();
}
playButton.addEventListener("click", () => {
playing = !playing;
updatePlayButton();
});
reverseButton.addEventListener("click", () => {
direction *= -1;
updateTimelineUI();
});
timelineElement.addEventListener("pointerdown", () => {
playing = false;
updatePlayButton();
});
timelineElement.addEventListener("input", (event) => {
temperature = Number(event.target.value);
if (temperature >= MAX_TEMP - 0.01) direction = -1;
if (temperature <= MIN_TEMP + 0.01) direction = 1;
updateTimelineUI();
});
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
event.target === document.body
) {
event.preventDefault();
playing = !playing;
updatePlayButton();
}
});
updateTimelineUI();
// -------------------------------------------------------------------------
// Temperature-driven phase transformation
// -------------------------------------------------------------------------
const coldRimColor = new THREE.Color(0x75bde5);
const hotRimColor = new THREE.Color(0xff8b50);
function updateChocolateState(time) {
const melt = smoothstep(23, 35.5, temperature);
const solid = 1 - melt;
const poolAmount = smoothstep(23, 31.5, temperature);
for (const cube of chocolateCubes) {
const scaleXZ = Math.max(0.055, Math.pow(solid, 0.55));
const scaleY = Math.max(0.06, Math.pow(solid, 0.65));
cube.mesh.scale.set(scaleXZ, scaleY, scaleXZ);
cube.mesh.position.y =
FLOOR_TOP + CUBE_HEIGHT * scaleY * 0.5;
// A tiny thermal wobble avoids a mechanically frozen transition.
cube.mesh.rotation.y =
Math.sin(time * 0.7 + cube.phase) * 0.004 * melt;
cube.material.roughness = 0.38 - melt * 0.18;
cube.material.clearcoat = 0.28 + melt * 0.58;
cube.material.clearcoatRoughness = 0.27 - melt * 0.13;
cube.edgeMaterial.opacity = 0.25 - melt * 0.12;
}
guideMaterial.opacity = 0.08 + melt * 0.3;
const poolScale = 0.78 + melt * 0.22;
pool.scale.set(poolScale, 1, poolScale);
poolBody.scale.set(poolScale, 1, poolScale);
poolMaterial.opacity = poolAmount * 0.96;
poolBodyMaterial.opacity = poolAmount * 0.78;
poolMaterial.roughness = 0.18 - melt * 0.075;
poolMaterial.clearcoatRoughness = 0.09 - melt * 0.04;
pool.visible = poolAmount > 0.001;
poolBody.visible = poolAmount > 0.001;
particleMaterial.opacity = melt * 0.24;
particles.visible = melt > 0.03;
rimLight.color.copy(coldRimColor).lerp(hotRimColor, melt);
rimLight.intensity = 42 + melt * 25;
warmSpot.intensity = 85 + melt * 45;
if (pool.visible) {
const positions = poolGeometry.attributes.position;
for (let index = 0; index < positions.count; index++) {
const x = positions.getX(index);
const z = positions.getZ(index);
const broadWave =
Math.sin(x * 2.05 + time * 1.65) *
Math.cos(z * 2.55 - time * 1.1);
const fineWave =
Math.sin((x + z) * 5.2 + time * 2.7);
const wave =
(broadWave * 0.018 + fineWave * 0.006) * melt;
positions.setY(index, wave);
}
positions.needsUpdate = true;
poolGeometry.computeVertexNormals();
}
}
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let frame = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
frame++;
if (playing) {
temperature += direction * delta * 1.7;
if (temperature >= MAX_TEMP) {
temperature = MAX_TEMP;
direction = -1;
} else if (temperature <= MIN_TEMP) {
temperature = MIN_TEMP;
direction = 1;
}
updateTimelineUI();
}
updateChocolateState(elapsed);
const positions = particleGeometry.attributes.position;
for (let index = 0; index < PARTICLE_COUNT; index++) {
const seed = particleSeeds[index];
const life = (seed.offset + elapsed * 0.16 * seed.drift) % 1;
positions.setXYZ(
index,
seed.x + Math.sin(elapsed * 0.65 + seed.offset * 9) * 0.16,
0.72 + life * 2.15,
seed.z + Math.cos(elapsed * 0.5 + seed.offset * 7) * 0.13
);
}
positions.needsUpdate = true;
// A subtle phase-dependent pulse keeps the scene alive while paused.
if (frame % 2 === 0) {
renderer.render(scene, camera);
}
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
window.addEventListener("resize", resize);
// Render once before removing the loading screen.
updateChocolateState(0);
renderer.render(scene, camera);
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 600);
animate();
</script>
</body>
</html>
```