Prompt specification
Build or customize the Three.js 3D scene "Molecular assembly recipe tray" 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: Molecular assembly recipe tray
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/molecular-assembly-recipe-tray-x1997
Scene Description Prompt:
Molecular assembly recipe tray. Build reactant-token bins and a product assembly station using a fixed simple balanced reaction; show exactly how many of each reactant are consumed per product batch. Please build this with Three.js from cdnjs in one self-contained HTML file, using a 3D cutaway.
Working Three.js Starter (molecular-assembly-recipe-tray-x1997):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Molecular Assembly — Recipe Tray</title>
<style>
:root {
--bg: #07161e;
--panel: rgba(10, 28, 36, 0.84);
--panel-strong: rgba(10, 28, 36, 0.95);
--line: rgba(167, 218, 214, 0.18);
--text: #edf8f6;
--muted: #91aaae;
--cyan: #61e0ce;
--cyan-soft: rgba(97, 224, 206, 0.14);
--red: #ff756b;
--red-soft: rgba(255, 117, 107, 0.14);
--amber: #f4c45e;
--amber-soft: rgba(244, 196, 94, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 38%, #16323c 0%, #0a1c25 44%, #061119 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
top: 20px;
left: 22px;
right: 22px;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
padding: 10px 14px 10px 10px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(7, 22, 30, 0.72);
box-shadow: 0 14px 40px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(14px);
}
.brand-mark {
position: relative;
width: 37px;
height: 37px;
border: 1px solid rgba(97, 224, 206, 0.55);
border-radius: 10px;
transform: rotate(45deg);
background:
radial-gradient(circle at 35% 35%, rgba(97, 224, 206, 0.24), transparent 58%);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
border-radius: 50%;
transform: rotate(-45deg);
}
.brand-mark::before {
width: 8px;
height: 8px;
top: 9px;
left: 8px;
background: var(--cyan);
box-shadow: 12px 11px 0 var(--red);
}
.brand-mark::after {
width: 7px;
height: 7px;
top: 19px;
left: 19px;
background: #f2fbfa;
}
.brand-copy {
line-height: 1.05;
}
.brand-copy strong {
display: block;
font-size: 13px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.brand-copy span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status-pill {
display: flex;
align-items: center;
gap: 10px;
min-width: 218px;
justify-content: space-between;
padding: 11px 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 22, 30, 0.76);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.status-pill .dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
.status-pill.running .dot {
background: var(--amber);
box-shadow: 0 0 14px var(--amber);
animation: pulse 1s ease-in-out infinite;
}
.status-pill.paused .dot {
background: var(--red);
box-shadow: 0 0 14px var(--red);
}
.status-copy {
min-width: 0;
flex: 1;
}
.status-copy small {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.status-copy strong {
display: block;
margin-top: 2px;
overflow: hidden;
font-size: 11px;
font-weight: 650;
text-overflow: ellipsis;
white-space: nowrap;
}
#batchBadge {
color: var(--cyan);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
@keyframes pulse {
50% { opacity: 0.35; }
}
.recipe-panel {
position: fixed;
z-index: 10;
top: 92px;
right: 22px;
width: min(320px, calc(100vw - 44px));
padding: 18px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(18px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
color: var(--cyan);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 18px;
height: 1px;
background: currentColor;
}
.recipe-panel h1 {
margin: 9px 0 4px;
font-size: 21px;
font-weight: 650;
letter-spacing: -0.03em;
}
.recipe-panel > p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.equation {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
margin: 17px 0;
padding: 15px 9px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(0, 0, 0, 0.17);
font: 650 18px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation .hydrogen {
color: #dff8f5;
}
.equation .oxygen {
color: var(--red);
}
.equation .product {
color: var(--cyan);
}
.equation .operator {
color: var(--muted);
font-size: 13px;
}
.coefficient {
margin-right: 4px;
color: var(--amber);
font-size: 13px;
}
.section-label {
margin: 14px 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.token-ledger {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.token-card {
position: relative;
min-width: 0;
padding: 10px 8px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.token-card::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 2px;
background: var(--accent);
opacity: 0.85;
}
.token-card .value {
display: block;
color: var(--accent);
font: 750 21px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.token-card .name {
display: block;
margin-top: 7px;
color: #dcebea;
font-size: 11px;
font-weight: 700;
}
.token-card .unit {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
line-height: 1.3;
text-transform: uppercase;
}
.balance {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 9px;
padding: 10px 11px;
border: 1px solid rgba(97, 224, 206, 0.15);
border-radius: 10px;
background: var(--cyan-soft);
color: var(--muted);
font-size: 10px;
}
.balance strong {
color: var(--cyan);
font-weight: 700;
}
.run-ledger {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.run-ledger > div {
padding: 8px 7px;
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
}
.run-ledger span {
display: block;
color: var(--muted);
font-size: 8px;
text-transform: uppercase;
}
.run-ledger b {
display: block;
margin-top: 4px;
font: 700 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.controls {
position: fixed;
z-index: 12;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel-strong);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.34);
transform: translateX(-50%);
backdrop-filter: blur(18px);
}
.controls button {
height: 39px;
padding: 0 16px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
cursor: pointer;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.02em;
transition: 160ms ease;
}
.controls button:hover:not(:disabled) {
border-color: rgba(97, 224, 206, 0.5);
background: rgba(97, 224, 206, 0.1);
transform: translateY(-1px);
}
.controls button:disabled {
cursor: not-allowed;
opacity: 0.38;
}
.controls #runBtn {
display: flex;
align-items: center;
gap: 9px;
min-width: 142px;
justify-content: center;
border-color: rgba(97, 224, 206, 0.5);
background: var(--cyan);
color: #05221e;
}
.controls #runBtn:hover:not(:disabled) {
background: #83f0df;
}
.play-icon {
width: 0;
height: 0;
border-top: 5px solid transparent;
border-bottom: 5px solid transparent;
border-left: 8px solid currentColor;
}
.progress-track {
position: fixed;
z-index: 9;
left: 0;
right: 0;
bottom: 0;
height: 3px;
background: rgba(255, 255, 255, 0.04);
}
#progressBar {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--cyan), var(--amber));
box-shadow: 0 0 14px rgba(97, 224, 206, 0.55);
transition: width 80ms linear;
}
.help {
position: fixed;
z-index: 8;
left: 22px;
bottom: 24px;
color: rgba(190, 214, 214, 0.62);
font-size: 10px;
letter-spacing: 0.02em;
pointer-events: none;
}
.help kbd {
padding: 3px 5px;
border: 1px solid var(--line);
border-radius: 4px;
background: rgba(255, 255, 255, 0.04);
color: #c7dada;
font: inherit;
}
@media (max-width: 800px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.brand-copy span,
.status-copy small,
.help {
display: none;
}
.brand {
padding: 9px;
}
.status-pill {
min-width: 0;
max-width: 190px;
}
.recipe-panel {
top: 77px;
right: 12px;
width: 274px;
padding: 14px;
}
.recipe-panel h1 {
font-size: 18px;
}
.token-card {
padding: 9px 6px;
}
.controls {
bottom: 14px;
}
.controls button {
padding: 0 11px;
}
}
@media (max-width: 520px) {
.recipe-panel {
top: 70px;
left: 12px;
right: 12px;
width: auto;
padding: 12px 14px;
}
.recipe-panel > p,
.section-label,
.balance,
.run-ledger,
.recipe-panel .eyebrow {
display: none;
}
.recipe-panel h1 {
margin: 0 0 10px;
font-size: 15px;
}
.equation {
margin: 0;
padding: 10px;
font-size: 16px;
}
.token-ledger {
margin-top: 8px;
}
.token-card {
padding: 7px;
}
.token-card .value {
font-size: 16px;
}
.token-card .name {
margin-top: 4px;
font-size: 9px;
}
.token-card .unit {
display: none;
}
.controls {
width: calc(100vw - 24px);
justify-content: stretch;
}
.controls button {
flex: 1;
padding: 0 8px;
}
.controls #runBtn {
min-width: 0;
flex: 1.5;
}
}
</style>
</head>
<body>
<div id="viewport" aria-label="Interactive 3D molecular assembly tray"></div>
<div class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<strong>Molecular Assembly</strong>
<span>Recipe tray / cutaway view</span>
</div>
</div>
<div class="status-pill" id="statusPill">
<span class="dot"></span>
<span class="status-copy">
<small>Assembly status</small>
<strong id="statusText">Ready for one product batch</strong>
</span>
<span id="batchBadge">BATCH 00</span>
</div>
</div>
<aside class="recipe-panel">
<div class="eyebrow">Fixed balanced reaction</div>
<h1>Water synthesis</h1>
<p>One product batch uses the exact molecule-token quantities shown below.</p>
<div class="equation" aria-label="Two hydrogen molecules plus one oxygen molecule makes two water molecules">
<span class="hydrogen"><span class="coefficient">2</span>H<sub>2</sub></span>
<span class="operator">+</span>
<span class="oxygen"><span class="coefficient">1</span>O<sub>2</sub></span>
<span class="operator">→</span>
<span class="product"><span class="coefficient">2</span>H<sub>2</sub>O</span>
</div>
<div class="section-label">Exact consumption per batch</div>
<div class="token-ledger">
<div class="token-card" style="--accent:#dff8f5">
<span class="value">×2</span>
<span class="name">H₂</span>
<span class="unit">reactant tokens</span>
</div>
<div class="token-card" style="--accent:#ff756b">
<span class="value">×1</span>
<span class="name">O₂</span>
<span class="unit">reactant token</span>
</div>
<div class="token-card" style="--accent:#61e0ce">
<span class="value">×2</span>
<span class="name">H₂O</span>
<span class="unit">product tokens</span>
</div>
</div>
<div class="balance">
<span>Atom check</span>
<strong>H 4 → 4 · O 2 → 2</strong>
</div>
<div class="section-label">Session totals</div>
<div class="run-ledger">
<div>
<span>H₂ used</span>
<b id="totalH">0</b>
</div>
<div>
<span>O₂ used</span>
<b id="totalO">0</b>
</div>
<div>
<span>H₂O made</span>
<b id="totalP">0</b>
</div>
</div>
</aside>
<div class="controls">
<button id="runBtn" type="button">
<span class="play-icon" aria-hidden="true"></span>
<span id="runLabel">Run one batch</span>
</button>
<button id="pauseBtn" type="button" disabled>Pause</button>
<button id="resetBtn" type="button">Reset</button>
</div>
<div class="help">
<kbd>Drag</kbd> orbit <kbd>Wheel</kbd> zoom <kbd>Space</kbd> run
</div>
<div class="progress-track"><div id="progressBar"></div></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// DOM and renderer
// ------------------------------------------------------------
const viewport = document.getElementById("viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x07161e, 23, 39);
const camera = new THREE.OrthographicCamera(-8, 8, 6, -6, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.05, -0.1);
let cameraTheta = 0.68;
let cameraPhi = 0.87;
let cameraZoom = 1;
function updateCamera() {
const radius = 20;
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + radius * Math.cos(cameraPhi),
cameraTarget.z + radius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraZoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(11.8, 15.6 / aspect);
renderer.setSize(width, height);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// Direct camera controls: no addons.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.005, 0.43, 1.32);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.67,
1.85
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0.68;
cameraPhi = 0.87;
cameraZoom = 1;
updateCamera();
});
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc8f3ef, 0x26323a, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff3d6, 3.2);
keyLight.position.set(-5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x55d9cf, 1.4);
rimLight.position.set(7, 5, -7);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff7c68, 1.3, 8, 2);
warmLight.position.set(-3, 4, -1);
scene.add(warmLight);
// ------------------------------------------------------------
// Reusable materials and geometry helpers
// ------------------------------------------------------------
const material = {
tray: new THREE.MeshStandardMaterial({
color: 0x8fa5a5,
metalness: 0.18,
roughness: 0.62
}),
trayDark: new THREE.MeshStandardMaterial({
color: 0x263a40,
metalness: 0.22,
roughness: 0.64
}),
wall: new THREE.MeshStandardMaterial({
color: 0x496066,
metalness: 0.22,
roughness: 0.58
}),
cut: new THREE.MeshStandardMaterial({
color: 0xe7a64c,
emissive: 0x4c2d08,
emissiveIntensity: 0.25,
metalness: 0.22,
roughness: 0.48
}),
frame: new THREE.MeshStandardMaterial({
color: 0x26383e,
metalness: 0.72,
roughness: 0.3
}),
frameLight: new THREE.MeshStandardMaterial({
color: 0x8ba4a7,
metalness: 0.68,
roughness: 0.32
}),
glass: new THREE.MeshStandardMaterial({
color: 0x9fe5df,
transparent: true,
opacity: 0.13,
depthWrite: false,
side: THREE.DoubleSide,
metalness: 0.05,
roughness: 0.12
}),
belt: new THREE.MeshStandardMaterial({
color: 0x17272c,
metalness: 0.15,
roughness: 0.85
}),
hydrogen: new THREE.MeshStandardMaterial({
color: 0xf1fbfa,
emissive: 0x6ab6c3,
emissiveIntensity: 0.12,
roughness: 0.25,
metalness: 0.04
}),
oxygen: new THREE.MeshStandardMaterial({
color: 0xff625a,
emissive: 0x8e1712,
emissiveIntensity: 0.22,
roughness: 0.29,
metalness: 0.03
}),
bond: new THREE.MeshStandardMaterial({
color: 0x81989a,
metalness: 0.58,
roughness: 0.32
}),
cyanLed: new THREE.MeshStandardMaterial({
color: 0x61e0ce,
emissive: 0x61e0ce,
emissiveIntensity: 1.1,
roughness: 0.22
}),
amberLed: new THREE.MeshStandardMaterial({
color: 0xf4c45e,
emissive: 0xf4c45e,
emissiveIntensity: 0.8,
roughness: 0.25
})
};
function box(width, height, depth, mat, x, y, z, parent = scene, shadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(
topRadius,
bottomRadius,
height,
mat,
x,
y,
z,
parent = scene,
segments = 32
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(topRadius, bottomRadius, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function torus(radius, tube, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 12, 64),
mat
);
mesh.position.set(x, y, z);
mesh.rotation.x = Math.PI / 2;
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function bondBetween(a, b, radius, parent) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 12),
material.bond
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeLabel(draw, worldWidth, worldHeight) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 190;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(worldWidth, worldHeight, 1);
sprite.renderOrder = 4;
function update(nextDraw = draw) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
nextDraw(ctx, canvas.width, canvas.height);
texture.needsUpdate = true;
}
update();
return { sprite, canvas, ctx, texture, update };
}
// ------------------------------------------------------------
// Cutaway laboratory tray
// ------------------------------------------------------------
box(13.4, 0.36, 9.2, material.tray, 0, 0, 0);
box(13.0, 0.12, 8.8, material.trayDark, 0, 0.24, 0);
// Rear wall and shortened side wall create an exposed cutaway.
box(13.2, 2.9, 0.22, material.wall, 0, 1.65, -4.36);
box(0.22, 1.75, 5.25, material.wall, -6.55, 1.12, -1.72);
// Gold section edges emphasize the sliced construction.
box(13.15, 0.075, 0.075, material.cut, 0, 0.27, 4.58);
box(0.075, 0.075, 9.05, material.cut, 6.66, 0.27, 0);
box(0.22, 0.075, 5.25, material.cut, -6.55, 2.0, -1.72);
// Tray grid.
const gridVertices = [];
for (let x = -6; x <= 6; x += 1) {
gridVertices.push(x, 0.305, -4.1, x, 0.305, 4.1);
}
for (let z = -4; z <= 4; z += 1) {
gridVertices.push(-6.1, 0.305, z, 6.1, 0.305, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
scene.add(
new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x84aaa9,
transparent: true,
opacity: 0.075
})
)
);
// Front plaque.
const plaqueCanvas = document.createElement("canvas");
plaqueCanvas.width = 1200;
plaqueCanvas.height = 120;
const plaqueCtx = plaqueCanvas.getContext("2d");
plaqueCtx.fillStyle = "#102a32";
plaqueCtx.fillRect(0, 0, 1200, 120);
plaqueCtx.fillStyle = "#61e0ce";
plaqueCtx.fillRect(0, 0, 18, 120);
plaqueCtx.font = "700 42px system-ui";
plaqueCtx.fillText("MOLECULAR ASSEMBLY / SECTION A—A", 52, 76);
const plaqueTexture = new THREE.CanvasTexture(plaqueCanvas);
plaqueTexture.colorSpace = THREE.SRGBColorSpace;
const plaque = new THREE.Mesh(
new THREE.PlaneGeometry(6.1, 0.61),
new THREE.MeshBasicMaterial({ map: plaqueTexture })
);
plaque.position.set(0, 0.22, 4.62);
scene.add(plaque);
// Rear recipe board.
const board = makeLabel((ctx, w, h) => {
roundedRect(ctx, 8, 8, w - 16, h - 16, 22);
ctx.fillStyle = "rgba(8, 25, 32, 0.96)";
ctx.fill();
ctx.strokeStyle = "rgba(97, 224, 206, 0.46)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = "#61e0ce";
ctx.fillRect(36, 34, 48, 5);
ctx.font = "700 22px system-ui";
ctx.fillText("BATCH RECIPE / BALANCED", 36, 72);
ctx.font = "650 58px ui-monospace, monospace";
ctx.fillStyle = "#eff9f7";
ctx.fillText("2 H₂", 83, 139);
ctx.fillStyle = "#8ca8aa";
ctx.fillText("+", 285, 139);
ctx.fillStyle = "#ff756b";
ctx.fillText("O₂", 345, 139);
ctx.fillStyle = "#8ca8aa";
ctx.fillText("→", 480, 139);
ctx.fillStyle = "#61e0ce";
ctx.fillText("2 H₂O", 580, 139);
ctx.font = "500 20px system-ui";
ctx.fillStyle = "#91aaae";
ctx.fillText("PER BATCH: 2 H₂ + 1 O₂ → 2 H₂O", 36, 174);
}, 6.35, 1.89);
board.sprite.position.set(0.8, 2.35, -4.13);
scene.add(board.sprite);
// ------------------------------------------------------------
// Molecule token models
// ------------------------------------------------------------
function atom(radius, mat, x, y, z, parent) {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(radius, 24, 16),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createMolecule(type) {
const group = new THREE.Group();
group.userData.type = type;
if (type === "H2") {
const a = new THREE.Vector3(-0.19, 0, 0);
const b = new THREE.Vector3(0.19, 0, 0);
atom(0.135, material.hydrogen, a.x, a.y, a.z, group);
atom(0.135, material.hydrogen, b.x, b.y, b.z, group);
bondBetween(a, b, 0.043, group);
}
if (type === "O2") {
const a = new THREE.Vector3(-0.22, 0, 0);
const b = new THREE.Vector3(0.22, 0, 0);
atom(0.175, material.oxygen, a.x, a.y, a.z, group);
atom(0.175, material.oxygen, b.x, b.y, b.z, group);
bondBetween(a, b, 0.052, group);
}
if (type === "H2O") {
const oxygen = new THREE.Vector3(0, -0.07, 0);
const hydrogenA = new THREE.Vector3(-0.27, 0.17, 0);
const hydrogenB = new THREE.Vector3(0.27, 0.17, 0);
atom(0.18, material.oxygen, oxygen.x, oxygen.y, oxygen.z, group);
atom(0.115, material.hydrogen, hydrogenA.x, hydrogenA.y, hydrogenA.z, group);
atom(0.115, material.hydrogen, hydrogenB.x, hydrogenB.y, hydrogenB.z, group);
bondBetween(oxygen, hydrogenA, 0.039, group);
bondBetween(oxygen, hydrogenB, 0.039, group);
}
return group;
}
// ------------------------------------------------------------
// Reactant and product bins
// ------------------------------------------------------------
function createBin({
x,
z,
width = 2.9,
depth = 2.45,
name,
accent,
type,
capacity = 6
}) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
box(width, 0.18, depth, material.frame, 0, 0.39, 0, group);
box(width - 0.16, 0.08, depth - 0.16, material.trayDark, 0, 0.51, 0, group);
// Open-top transparent bin with a low front wall for visibility.
const wallHeight = 0.88;
const wallY = 0.52 + wallHeight / 2;
box(width, wallHeight, 0.065, material.glass, 0, wallY, -depth / 2, group, false);
box(0.065, wallHeight, depth, material.glass, -width / 2, wallY, 0, group, false);
box(0.065, wallHeight, depth, material.glass, width / 2, wallY, 0, group, false);
box(width, 0.3, 0.065, material.glass, 0, 0.67, depth / 2, group, false);
// Thin framing rails.
box(width, 0.055, 0.055, material.frameLight, 0, 0.96, -depth / 2, group);
box(0.055, 0.055, depth, material.frameLight, -width / 2, 0.96, 0, group);
box(0.055, 0.055, depth, material.frameLight, width / 2, 0.96, 0, group);
const label = makeLabel((ctx, w, h) => {
drawBinLabel(ctx, w, h, name, 0, accent, "READY");
}, 2.55, 0.76);
label.sprite.position.set(0, 1.82, 0);
group.add(label.sprite);
const tokens = [];
const allTokens = [];
for (let i = 0; i < capacity; i++) {
const token = createMolecule(type);
token.scale.setScalar(type === "H2O" ? 0.85 : 1);
let localX;
let localZ;
if (type === "H2") {
localX = (i % 3 - 1) * 0.76;
localZ = (Math.floor(i / 3) - 0.5) * 0.79;
} else if (type === "O2") {
localX = (i % 2 - 0.5) * 1.02;
localZ = (Math.floor(i / 2) - 1) * 0.68;
} else {
localX = (i % 3 - 1) * 0.73;
localZ = (Math.floor(i / 3) - 0.5) * 0.72;
}
token.position.set(localX, 0.77, localZ);
token.rotation.y = type === "H2O" ? (i % 3) * 0.4 : i * 0.18;
token.userData.home = {
position: token.position.clone(),
rotation: token.rotation.clone(),
scale: token.scale.clone()
};
group.add(token);
tokens.push(token);
allTokens.push(token);
}
return {
group,
label,
name,
accent,
type,
capacity,
tokens,
allTokens,
count: 0,
products: []
};
}
function drawBinLabel(ctx, w, h, name, count, accent, state) {
roundedRect(ctx, 8, 8, w - 16, h - 16, 20);
ctx.fillStyle = "rgba(9, 27, 34, 0.95)";
ctx.fill();
ctx.strokeStyle = "rgba(177, 218, 214, 0.25)";
ctx.lineWidth = 2;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(29, 28, 8, 130);
ctx.font = "700 36px system-ui";
ctx.fillStyle = "#edf8f6";
ctx.fillText(name, 58, 67);
ctx.font = "600 23px system-ui";
ctx.fillStyle = "#91aaae";
ctx.fillText(state, 58, 101);
ctx.font = "750 44px ui-monospace, monospace";
ctx.fillStyle = accent;
ctx.fillText(String(count).padStart(2, "0"), 58, 158);
ctx.font = "650 16px system-ui";
ctx.fillStyle = "#91aaae";
ctx.fillText("TOKENS", 125, 155);
}
const hydrogenBin = createBin({
x: -4.15,
z: -2.05,
name: "H₂ REACTANT",
accent: "#dff8f5",
type: "H2",
capacity: 6
});
const oxygenBin = createBin({
x: -4.15,
z: 1.35,
name: "O₂ REACTANT",
accent: "#ff756b",
type: "O2",
capacity: 6
});
const productBin = createBin({
x: 4.25,
z: -0.35,
width: 2.85,
depth: 2.7,
name: "H₂O PRODUCT",
accent: "#61e0ce",
type: "H2O",
capacity: 9
});
function updateBinLabel(bin, state = "READY") {
bin.label.update((ctx, w, h) => {
drawBinLabel(ctx, w, h, bin.name, bin.count, bin.accent, state);
});
}
// Route traces from both reactant bins to the assembly station.
function addRoute(points, color) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color,
dashSize: 0.18,
gapSize: 0.14,
transparent: true,
opacity: 0.42
})
);
line.computeLineDistances();
scene.add(line);
}
addRoute([
new THREE.Vector3(-3.0, 0.38, -2.05),
new THREE.Vector3(-2.25, 0.38, -1.45),
new THREE.Vector3(-1.4, 0.38, -0.55)
], 0xdff8f5);
addRoute([
new THREE.Vector3(-3.0, 0.38, 1.35),
new THREE.Vector3(-2.2, 0.38, 1.05),
new THREE.Vector3(-1.35, 0.38, 0.1)
], 0xff756b);
// ------------------------------------------------------------
// Assembly station
// ------------------------------------------------------------
const station = new THREE.Group();
station.position.set(-0.45, 0, -0.35);
scene.add(station);
cylinder(1.48, 1.58, 0.34, material.frame, 0, 0.45, 0, station);
cylinder(1.22, 1.30, 0.15, material.frameLight, 0, 0.68, 0, station);
const reactionRingMaterial = new THREE.MeshStandardMaterial({
color: 0x61e0ce,
emissive: 0x61e0ce,
emissiveIntensity: 0.6,
metalness: 0.42,
roughness: 0.24
});
torus(1.04, 0.065, reactionRingMaterial, 0, 0.81, 0, station);
const chamberWall = new THREE.Mesh(
new THREE.CylinderGeometry(1.02, 1.02, 0.76, 48, 1, true),
material.glass
);
chamberWall.position.y = 1.4;
station.add(chamberWall);
const chamberDome = new THREE.Mesh(
new THREE.SphereGeometry(1.02, 40, 20, 0, Math.PI * 2, 0, Math.PI / 2),
material.glass
);
chamberDome.position.y = 1.78;
station.add(chamberDome);
torus(1.02, 0.045, material.frameLight, 0, 1.78, 0, station);
// Gantry.
box(0.16, 2.35, 0.18, material.frame, -1.42, 1.86, -0.48, station);
box(0.16, 2.35, 0.18, material.frame, 1.42, 1.86, -0.48, station);
box(3.0, 0.17, 0.20, material.frame, 0, 3.03, -0.48, station);
box(2.7, 0.07, 0.09, material.frameLight, 0, 2.91, -0.48, station);
const toolHead = new THREE.Group();
toolHead.position.set(0, 2.48, -0.35);
station.add(toolHead);
box(0.48, 0.24, 0.36, material.frameLight, 0, 0, 0, toolHead);
cylinder(0.075, 0.075, 0.42, material.frame, 0, -0.3, 0, toolHead);
cylinder(0.15, 0.11, 0.16, material.cyanLed, 0, -0.58, 0, toolHead);
// Station status display.
const machineCanvas = document.createElement("canvas");
machineCanvas.width = 640;
machineCanvas.height = 220;
const machineCtx = machineCanvas.getContext("2d");
const machineTexture = new THREE.CanvasTexture(machineCanvas);
machineTexture.colorSpace = THREE.SRGBColorSpace;
const machinePanel = new THREE.Mesh(
new THREE.PlaneGeometry(1.48, 0.51),
new THREE.MeshBasicMaterial({ map: machineTexture })
);
machinePanel.position.set(0, 0.58, 1.25);
station.add(machinePanel);
function setMachine(text, color = "#61e0ce") {
machineCtx.clearRect(0, 0, 640, 220);
roundedRect(machineCtx, 6, 6, 628, 208, 20);
machineCtx.fillStyle = "#071a20";
machineCtx.fill();
machineCtx.strokeStyle = color;
machineCtx.lineWidth = 5;
machineCtx.stroke();
machineCtx.fillStyle = color;
machineCtx.font = "700 30px system-ui";
machineCtx.textAlign = "center";
machineCtx.fillText(text, 320, 96);
machineCtx.fillStyle = "#79969a";
machineCtx.font = "600 19px ui-monospace, monospace";
machineCtx.fillText("ASSEMBLY STATION / A-01", 320, 145);
machineCtx.fillStyle = color;
machineCtx.beginPath();
machineCtx.arc(320, 177, 8, 0, Math.PI * 2);
machineCtx.fill();
machineTexture.needsUpdate = true;
}
setMachine("READY");
// Output conveyor.
box(2.25, 0.18, 0.88, material.frame, 2.25, 0.68, -0.35, scene);
box(2.05, 0.08, 0.72, material.belt, 2.25, 0.81, -0.35, scene);
for (let x = 1.32; x <= 3.18; x += 0.31) {
const roller = cylinder(
0.095,
0.095,
0.78,
material.frameLight,
x,
0.83,
-0.35,
scene,
18
);
roller.rotation.x = Math.PI / 2;
}
// Direction arrows on the conveyor.
for (let x = 1.55; x < 3.1; x += 0.55) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.11, 0.22, 3),
material.cyanLed
);
arrow.position.set(x, 0.88, -0.35);
arrow.rotation.z = -Math.PI / 2;
scene.add(arrow);
}
// ------------------------------------------------------------
// Simulation state and UI
// ------------------------------------------------------------
const statusPill = document.getElementById("statusPill");
const statusText = document.getElementById("statusText");
const batchBadge = document.getElementById("batchBadge");
const runButton = document.getElementById("runBtn");
const runLabel = document.getElementById("runLabel");
const pauseButton = document.getElementById("pauseBtn");
const resetButton = document.getElementById("resetBtn");
const progressBar = document.getElementById("progressBar");
const totalH = document.getElementById("totalH");
const totalO = document.getElementById("totalO");
const totalP = document.getElementById("totalP");
let simTime = 0;
let running = false;
let paused = false;
let batchNumber = 0;
let batchStart = 0;
let reactionPulse = 0;
let sessionH = 0;
let sessionO = 0;
let sessionP = 0;
const tweens = [];
const scheduled = [];
const bursts = [];
function easeInOut(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function easeOut(t) {
return 1 - Math.pow(1 - t, 3);
}
function tween(duration, update, complete) {
tweens.push({
start: simTime,
duration,
update,
complete
});
}
function schedule(delay, callback) {
scheduled.push({
time: simTime + delay,
callback
});
}
function updateTweens() {
for (let i = tweens.length - 1; i >= 0; i--) {
const item = tweens[i];
const raw = THREE.MathUtils.clamp(
(simTime - item.start) / item.duration,
0,
1
);
item.update(raw);
if (raw >= 1) {
tweens.splice(i, 1);
if (item.complete) item.complete();
}
}
}
function updateScheduled() {
for (let i = scheduled.length - 1; i >= 0; i--) {
if (simTime >= scheduled[i].time) {
const callback = scheduled[i].callback;
scheduled.splice(i, 1);
callback();
}
}
}
function setStatus(text, mode = "") {
statusText.textContent = text;
statusPill.classList.remove("running", "paused");
if (mode) statusPill.classList.add(mode);
}
function updateTotals() {
totalH.textContent = String(sessionH);
totalO.textContent = String(sessionO);
totalP.textContent = String(sessionP);
}
function productSlot(index) {
const col = index % 3;
const row = Math.floor(index / 3);
return new THREE.Vector3(
(col - 1) * 0.73,
0.77,
(row - 0.5) * 0.72
);
}
function worldProductSlot(index) {
productBin.group.updateMatrixWorld(true);
return productBin.group.localToWorld(productSlot(index));
}
function createReactionBurst() {
const count = 34;
const positions = new Float32Array(count * 3);
const velocities = [];
for (let i = 0; i < count; i++) {
const angle = Math.random() * Math.PI * 2;
const radius = 0.15 + Math.random() * 0.8;
const y = 1.15 + Math.random() * 0.55;
positions[i * 3] = -0.45 + Math.cos(angle) * radius;
positions[i * 3 + 1] = y;
positions[i * 3 + 2] = -0.35 + Math.sin(angle) * radius;
velocities.push(
new THREE.Vector3(
Math.cos(angle) * (0.2 + Math.random() * 0.55),
0.35 + Math.random() * 0.7,
Math.sin(angle) * (0.2 + Math.random() * 0.55)
)
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
const pointsMaterial = new THREE.PointsMaterial({
color: 0x9bfff0,
size: 0.075,
transparent: true,
opacity: 1,
depthWrite: false
});
const points = new THREE.Points(geometry, pointsMaterial);
scene.add(points);
bursts.push({
points,
geometry,
pointsMaterial,
velocities,
origin: positions.slice(),
born: simTime
});
}
function updateBursts() {
for (let i = bursts.length - 1; i >= 0; i--) {
const burst = bursts[i];
const age = simTime - burst.born;
const positions = burst.geometry.attributes.position.array;
for (let j = 0; j < burst.velocities.length; j++) {
const velocity = burst.velocities[j];
positions[j * 3] =
burst.origin[j * 3] + velocity.x * age;
positions[j * 3 + 1] =
burst.origin[j * 3 + 1] +
velocity.y * age -
0.5 * age * age;
positions[j * 3 + 2] =
burst.origin[j * 3 + 2] +
velocity.z * age;
}
burst.geometry.attributes.position.needsUpdate = true;
burst.pointsMaterial.opacity = Math.max(0, 1 - age);
if (age >= 1) {
scene.remove(burst.points);
burst.geometry.dispose();
burst.pointsMaterial.dispose();
bursts.splice(i, 1);
}
}
}
function moveTokenTo(token, destination, duration = 0.85) {
scene.attach(token);
const start = token.position.clone();
const startRotation = token.rotation.clone();
tween(
duration,
(raw) => {
const p = easeInOut(raw);
token.position.lerpVectors(start, destination, p);
token.position.y += Math.sin(Math.PI * p) * 0.62;
token.rotation.y = startRotation.y + p * Math.PI * 1.35;
token.rotation.z = startRotation.z + Math.sin(p * Math.PI) * 0.15;
}
);
}
function outputProduct(product, slotIndex) {
const destination = worldProductSlot(slotIndex);
const curve = new THREE.CatmullRomCurve3([
product.position.clone(),
new THREE.Vector3(0.85, 1.28, -0.35),
new THREE.Vector3(1.75, 0.95, -0.35),
new THREE.Vector3(2.65, 0.93, -0.35),
destination
]);
tween(
1.25,
(raw) => {
const p = easeInOut(raw);
product.position.copy(curve.getPointAt(p));
product.rotation.y += 0.055;
},
() => {
productBin.group.add(product);
product.position.copy(productSlot(slotIndex));
product.rotation.set(0, slotIndex * 0.42, 0);
productBin.count++;
sessionP++;
updateTotals();
updateBinLabel(productBin, "PRODUCT READY");
}
);
}
function assembleProduct(productIndex) {
const product = createMolecule("H2O");
product.scale.setScalar(0.001);
const offsets = [
new THREE.Vector3(-0.68, 1.37, -0.52),
new THREE.Vector3(-0.25, 1.37, -0.12)
];
product.position.copy(
offsets[productIndex].add(station.position)
);
scene.add(product);
tween(
0.25,
(raw) => {
const p = easeOut(raw);
product.scale.setScalar(0.001 + p * 0.85);
},
() => {
outputProduct(product, productBin.count + productIndex);
}
);
}
function runBatch() {
if (running) return;
running = true;
paused = false;
batchNumber++;
batchStart = simTime;
batchBadge.textContent = `BATCH ${String(batchNumber).padStart(2, "0")}`;
runButton.disabled = true;
pauseButton.disabled = false;
pauseButton.textContent = "Pause";
setStatus("Transferring 2 H₂ + 1 O₂ to station", "running");
setMachine("TRANSFERRING", "#f4c45e");
progressBar.style.width = "0%";
// Reserve and consume exactly three reactant molecule tokens.
const h1 = hydrogenBin.tokens.pop();
const h2 = hydrogenBin.tokens.pop();
const o1 = oxygenBin.tokens.pop();
hydrogenBin.count -= 2;
oxygenBin.count -= 1;
sessionH += 2;
sessionO += 1;
updateTotals();
updateBinLabel(hydrogenBin, "2 CONSUMED / RUNNING");
updateBinLabel(oxygenBin, "1 CONSUMED / RUNNING");
updateBinLabel(productBin, "ASSEMBLING");
schedule(0.02, () => {
moveTokenTo(
h1,
new THREE.Vector3(-1.05, 1.34, -0.68),
0.82
);
});
schedule(0.45, () => {
moveTokenTo(
h2,
new THREE.Vector3(-0.36, 1.34, -0.04),
0.82
);
});
schedule(0.88, () => {
moveTokenTo(
o1,
new THREE.Vector3(0.25, 1.34, -0.66),
0.82
);
});
schedule(1.85, () => {
setStatus("Balancing atoms and assembling 2 H₂O", "running");
setMachine("REACTING", "#61e0ce");
reactionPulse = 1;
createReactionBurst();
[h1, h2, o1].forEach((token) => {
tween(
0.28,
(raw) => {
token.scale.setScalar(Math.max(0.001, 1 - easeInOut(raw)));
},
() => {
scene.remove(token);
}
);
});
});
schedule(2.22, () => {
setStatus("Product batch complete: 2 H₂O", "running");
setMachine("TRANSFERRING", "#f4c45e");
assembleProduct(0);
});
schedule(2.40, () => assembleProduct(1));
schedule(4.22, () => {
running = false;
runButton.disabled = false;
pauseButton.disabled = true;
runLabel.textContent = "Run next batch";
setStatus("Batch complete · 2 H₂O moved to product bin");
setMachine("BATCH COMPLETE", "#61e0ce");
progressBar.style.width = "100%";
updateBinLabel(productBin, "PRODUCT READY");
});
}
function restoreReactantTokens() {
for (const bin of [hydrogenBin, oxygenBin]) {
for (const token of bin.allTokens) {
if (token.parent) token.parent.remove(token);
bin.group.add(token);
const home = token.userData.home;
token.position.copy(home.position);
token.rotation.copy(home.rotation);
token.scale.copy(home.scale);
token.visible = true;
}
}
}
function resetSimulation() {
running = false;
paused = false;
batchNumber = 0;
batchStart = 0;
reactionPulse = 0;
tweens.length = 0;
scheduled.length = 0;
for (const burst of bursts) {
scene.remove(burst.points);
burst.geometry.dispose();
burst.pointsMaterial.dispose();
}
bursts.length = 0;
restoreReactantTokens();
for (const product of productBin.products) {
if (product.parent) product.parent.remove(product);
}
productBin.products.length = 0;
hydrogenBin.count = 6;
oxygenBin.count = 6;
productBin.count = 0;
sessionH = 0;
sessionO = 0;
sessionP = 0;
updateTotals();
updateBinLabel(hydrogenBin, "READY");
updateBinLabel(oxygenBin, "READY");
updateBinLabel(productBin, "READY");
batchBadge.textContent = "BATCH 00";
runButton.disabled = false;
runLabel.textContent = "Run one batch";
pauseButton.disabled = true;
pauseButton.textContent = "Pause";
progressBar.style.width = "0%";
setStatus("Ready for one product batch");
setMachine("READY");
}
runButton.addEventListener("click", runBatch);
pauseButton.addEventListener("click", () => {
if (!running) return;
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
if (paused) {
setStatus("Batch paused", "paused");
setMachine("PAUSED", "#ff756b");
} else {
setStatus("Batch resumed", "running");
setMachine("RUNNING", "#f4c45e");
}
});
resetButton.addEventListener("click", resetSimulation);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
if (!running) runBatch();
}
if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simTime += dt;
updateScheduled();
updateTweens();
updateBursts();
}
reactionPulse = Math.max(0, reactionPulse - dt * 1.55);
reactionRingMaterial.emissiveIntensity =
0.6 + reactionPulse * 4.5;
// Tool head descends during the reaction stage.
let targetToolY = 2.48;
if (running) {
const elapsed = simTime - batchStart;
if (elapsed > 1.65 && elapsed < 2.65) {
targetToolY = 1.64;
} else if (elapsed >= 2.65 && elapsed < 3.2) {
targetToolY = 2.08;
}
}
toolHead.position.y = THREE.MathUtils.lerp(
toolHead.position.y,
targetToolY,
0.075
);
if (running) {
const progress = THREE.MathUtils.clamp(
(simTime - batchStart) / 4.22,
0,
1
);
progressBar.style.width = `${progress * 100}%`;
}
renderer.render(scene, camera);
});
updateTotals();
</script>
</body>
</html>
```