Prompt specification
Build or customize the Three.js 3D scene "Balanced reaction atom-token board" 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: Balanced reaction atom-token board
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/balanced-reaction-atom-token-board-x1985
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a molecular-token representation of a simple balanced reaction such as two hydrogen molecules plus one oxygen molecule forming two water molecules; preserve atom identities. A 3D cutaway view.
Working Three.js Starter (balanced-reaction-atom-token-board-x1985):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Molecular Token Reaction — 2H₂ + O₂ → 2H₂O</title>
<style>
:root {
color-scheme: dark;
--bg: #07101d;
--panel: rgba(10, 22, 38, 0.82);
--panel-strong: rgba(9, 20, 34, 0.94);
--line: rgba(137, 205, 255, 0.18);
--text: #e8f6ff;
--muted: #91a9bd;
--cyan: #61e8ff;
--blue: #5d8dff;
--hydrogen: #d8f8ff;
--oxygen: #ff796f;
--success: #72f0b8;
}
* {
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;
}
button {
color: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
header {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
right: 18px;
min-height: 88px;
border-radius: 18px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 15px 20px;
pointer-events: none;
}
.heading {
min-width: 290px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(17px, 2vw, 23px);
line-height: 1.15;
letter-spacing: -0.025em;
}
.subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
}
.equation {
display: flex;
align-items: center;
justify-content: center;
gap: 9px;
white-space: nowrap;
font-size: clamp(18px, 2.3vw, 29px);
font-weight: 650;
}
.molecule {
display: inline-flex;
align-items: baseline;
gap: 4px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
transition: 180ms ease;
}
.molecule.reactant {
border-color: rgba(255, 121, 111, 0.28);
}
.molecule.product {
border-color: rgba(114, 240, 184, 0.28);
}
.coefficient {
color: var(--muted);
font-size: 0.75em;
}
.arrow {
color: var(--cyan);
font-size: 1.15em;
text-shadow: 0 0 18px rgba(97, 232, 255, 0.45);
}
.phase {
min-width: 158px;
text-align: right;
pointer-events: auto;
}
.phase-label {
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.phase-value {
display: inline-flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 750;
}
.phase-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--oxygen);
box-shadow: 0 0 14px var(--oxygen);
}
aside {
position: fixed;
z-index: 9;
top: 124px;
bottom: 102px;
width: 268px;
border-radius: 18px;
padding: 17px;
overflow: auto;
scrollbar-width: thin;
scrollbar-color: rgba(97, 232, 255, 0.28) transparent;
}
#tokenPanel {
left: 18px;
}
#inspector {
right: 18px;
width: 294px;
}
.panel-title {
margin: 0 0 4px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.01em;
}
.panel-copy {
margin: 0 0 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.token-list {
display: grid;
gap: 7px;
}
.token-row {
width: 100%;
display: grid;
grid-template-columns: 42px 1fr auto;
align-items: center;
gap: 9px;
padding: 8px;
border: 1px solid transparent;
border-radius: 11px;
background: rgba(255, 255, 255, 0.032);
color: var(--text);
text-align: left;
cursor: pointer;
transition: 160ms ease;
}
.token-row:hover {
border-color: rgba(97, 232, 255, 0.3);
background: rgba(97, 232, 255, 0.065);
transform: translateX(2px);
}
.token-row.selected {
border-color: rgba(97, 232, 255, 0.6);
background: rgba(97, 232, 255, 0.1);
box-shadow: inset 3px 0 var(--cyan);
}
.token-icon {
width: 36px;
height: 36px;
display: grid;
place-items: center;
border-radius: 10px;
font-size: 17px;
font-weight: 850;
border: 1px solid rgba(255, 255, 255, 0.15);
}
.token-icon.hydrogen {
color: var(--hydrogen);
background: rgba(216, 248, 255, 0.08);
}
.token-icon.oxygen {
color: var(--oxygen);
background: rgba(255, 121, 111, 0.09);
}
.token-id {
font-size: 12px;
font-weight: 800;
}
.token-destination {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.token-arrow {
color: var(--cyan);
font-size: 15px;
}
.section {
margin-top: 19px;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 9px 0;
color: var(--muted);
font-size: 11px;
}
.legend-swatch {
width: 13px;
height: 13px;
border-radius: 4px;
}
.legend-line {
width: 21px;
height: 2px;
background: #7eb9da;
box-shadow: 0 0 8px rgba(126, 185, 218, 0.45);
}
.legend-line.dashed {
height: 0;
border-top: 1px dashed var(--cyan);
background: none;
box-shadow: none;
}
.selected-token {
display: flex;
align-items: center;
gap: 13px;
margin: 13px 0 18px;
}
#selectedIcon {
width: 58px;
height: 58px;
border-radius: 17px;
display: grid;
place-items: center;
font-size: 28px;
font-weight: 850;
border: 1px solid rgba(255, 255, 255, 0.16);
}
#selectedId {
font-size: 18px;
font-weight: 800;
}
#selectedElement {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.mapping {
display: grid;
gap: 8px;
}
.mapping-box {
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.mapping-label {
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.mapping-value {
font-size: 12px;
font-weight: 720;
}
.conservation {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.count-box {
padding: 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
text-align: center;
}
.count-box strong {
display: block;
font-size: 20px;
}
.count-box span {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.note {
margin-top: 11px;
padding: 11px;
border-left: 2px solid var(--success);
border-radius: 0 9px 9px 0;
background: rgba(114, 240, 184, 0.055);
color: #b9d8ce;
font-size: 10px;
line-height: 1.55;
}
.controls {
position: fixed;
z-index: 12;
left: 50%;
bottom: 18px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 9px;
padding: 10px;
border-radius: 16px;
white-space: nowrap;
}
.control-button {
min-height: 38px;
padding: 0 14px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: 160ms ease;
}
.control-button:hover {
border-color: rgba(97, 232, 255, 0.45);
background: rgba(97, 232, 255, 0.075);
transform: translateY(-1px);
}
.control-button.primary {
border-color: rgba(97, 232, 255, 0.55);
background: linear-gradient(135deg, rgba(49, 183, 219, 0.28), rgba(77, 104, 225, 0.22));
color: #ecfbff;
box-shadow: 0 0 22px rgba(97, 232, 255, 0.08);
}
.toggle {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 0 8px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 14px;
height: 14px;
accent-color: var(--cyan);
}
.progress-wrap {
position: fixed;
z-index: 11;
left: 50%;
bottom: 79px;
width: min(430px, 48vw);
height: 3px;
transform: translateX(-50%);
border-radius: 99px;
overflow: hidden;
background: rgba(255, 255, 255, 0.08);
}
#progress {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--oxygen), var(--cyan), var(--success));
box-shadow: 0 0 14px rgba(97, 232, 255, 0.6);
}
.hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 94px;
transform: translateX(-50%);
color: rgba(185, 210, 228, 0.66);
font-size: 10px;
letter-spacing: 0.02em;
pointer-events: none;
}
@media (max-width: 1050px) {
#inspector {
width: 260px;
}
#tokenPanel {
width: 240px;
}
.phase {
display: none;
}
}
@media (max-width: 780px) {
header {
top: 10px;
left: 10px;
right: 10px;
min-height: 75px;
padding: 12px;
}
.heading {
min-width: 0;
}
.subtitle {
display: none;
}
.equation {
gap: 4px;
font-size: 16px;
}
.molecule {
padding: 6px 7px;
}
aside {
top: auto;
bottom: 83px;
width: auto;
max-height: 38vh;
padding: 12px;
}
#tokenPanel {
left: 10px;
right: 10px;
}
#inspector {
display: none;
}
#tokenPanel .section,
#tokenPanel .panel-copy {
display: none;
}
.token-list {
grid-template-columns: repeat(3, 1fr);
}
.token-row {
grid-template-columns: 30px 1fr;
}
.token-icon {
width: 28px;
height: 28px;
border-radius: 8px;
font-size: 14px;
}
.token-arrow {
display: none;
}
.controls {
bottom: 10px;
width: calc(100% - 20px);
justify-content: center;
overflow-x: auto;
}
.control-button {
padding: 0 10px;
}
.progress-wrap {
bottom: 69px;
width: 72vw;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D molecular-token reaction"></canvas>
<header class="glass">
<div class="heading">
<div class="eyebrow">Molecular Token Reaction</div>
<h1>Conserving every atom</h1>
<div class="subtitle">One token per atom · same identity before and after reaction</div>
</div>
<div class="equation" aria-label="Two hydrogen molecules plus one oxygen molecule form two water molecules">
<span class="molecule reactant">
<span class="coefficient">2</span><span>H<sub>2</sub></span>
</span>
<span>+</span>
<span class="molecule reactant"><span>O<sub>2</sub></span></span>
<span class="arrow">→</span>
<span class="molecule product">
<span class="coefficient">2</span><span>H<sub>2</sub>O</span>
</span>
</div>
<div class="phase">
<div class="phase-label">Reaction state</div>
<div class="phase-value">
<span class="phase-dot" id="phaseDot"></span>
<span id="phaseText">Reactants</span>
</div>
</div>
</header>
<aside id="tokenPanel" class="glass">
<h2 class="panel-title">Atom-token ledger</h2>
<p class="panel-copy">
Select a token in the scene or ledger. IDs persist while bonds break and reform.
</p>
<div class="token-list" id="tokenList">
<button class="token-row" data-atom="H1">
<span class="token-icon hydrogen">H</span>
<span>
<span class="token-id">H1</span>
<span class="token-destination">H₂ (A) → H₂O (A)</span>
</span>
<span class="token-arrow">→</span>
</button>
<button class="token-row" data-atom="H2">
<span class="token-icon hydrogen">H</span>
<span>
<span class="token-id">H2</span>
<span class="token-destination">H₂ (A) → H₂O (A)</span>
</span>
<span class="token-arrow">→</span>
</button>
<button class="token-row" data-atom="H3">
<span class="token-icon hydrogen">H</span>
<span>
<span class="token-id">H3</span>
<span class="token-destination">H₂ (B) → H₂O (B)</span>
</span>
<span class="token-arrow">→</span>
</button>
<button class="token-row" data-atom="H4">
<span class="token-icon hydrogen">H</span>
<span>
<span class="token-id">H4</span>
<span class="token-destination">H₂ (B) → H₂O (B)</span>
</span>
<span class="token-arrow">→</span>
</button>
<button class="token-row" data-atom="O1">
<span class="token-icon oxygen">O</span>
<span>
<span class="token-id">O1</span>
<span class="token-destination">O₂ → H₂O (A)</span>
</span>
<span class="token-arrow">→</span>
</button>
<button class="token-row" data-atom="O2">
<span class="token-icon oxygen">O</span>
<span>
<span class="token-id">O2</span>
<span class="token-destination">O₂ → H₂O (B)</span>
</span>
<span class="token-arrow">→</span>
</button>
</div>
<div class="section">
<h3 class="panel-title">Visual key</h3>
<div class="legend-row">
<span class="legend-swatch" style="background:#d8f8ff"></span>
Hydrogen token
</div>
<div class="legend-row">
<span class="legend-swatch" style="background:#ff796f"></span>
Oxygen token
</div>
<div class="legend-row">
<span class="legend-line"></span>
Chemical bond
</div>
<div class="legend-row">
<span class="legend-line dashed"></span>
Atom-identity trajectory
</div>
</div>
<div class="section">
<h3 class="panel-title">Conservation check</h3>
<div class="conservation">
<div class="count-box">
<strong>4 H</strong>
<span>Reactants</span>
</div>
<div class="count-box">
<strong>4 H</strong>
<span>Products</span>
</div>
<div class="count-box">
<strong>2 O</strong>
<span>Reactants</span>
</div>
<div class="count-box">
<strong>2 O</strong>
<span>Products</span>
</div>
</div>
</div>
</aside>
<aside id="inspector" class="glass">
<h2 class="panel-title">Selected token</h2>
<p class="panel-copy">Identity and molecular membership are fixed properties of each token.</p>
<div class="selected-token">
<div id="selectedIcon" class="hydrogen">H</div>
<div>
<div id="selectedId">H1</div>
<div id="selectedElement">Hydrogen · atomic token</div>
</div>
</div>
<div class="mapping">
<div class="mapping-box">
<div class="mapping-label">Reactant membership</div>
<div class="mapping-value" id="selectedSource">Hydrogen molecule A</div>
</div>
<div class="mapping-box">
<div class="mapping-label">Product membership</div>
<div class="mapping-value" id="selectedDestination">Water molecule A</div>
</div>
<div class="mapping-box">
<div class="mapping-label">Identity status</div>
<div class="mapping-value" id="selectedStatus" style="color:var(--success)">
Preserved · token H1
</div>
</div>
</div>
<div class="section">
<h3 class="panel-title">What the animation does</h3>
<p class="panel-copy">
The six atom meshes are never replaced. Each travels along its own trajectory,
the reactant bonds dissolve, and four new O–H bonds form.
</p>
<div class="note">
Click any atom or ledger token to isolate its identity path. The molecule
coefficients describe copies of the same H₂ and O₂ reactants, not additional atom IDs.
</div>
</div>
<div class="section">
<h3 class="panel-title">Camera</h3>
<p class="panel-copy">
Drag to orbit, scroll to zoom, and double-click the scene to restore the view.
Keyboard: <strong>R</strong> resets the camera and <strong>Space</strong> runs the reaction.
</p>
</div>
</aside>
<div class="hint">Drag to orbit · scroll to zoom · click a token to inspect its identity</div>
<div class="progress-wrap"><div id="progress"></div></div>
<div class="controls glass">
<button class="control-button primary" id="reactionButton">Form products</button>
<button class="control-button" id="resetButton">Reset reaction</button>
<button class="control-button" id="viewButton">Reset view</button>
<label class="toggle">
<input id="labelsToggle" type="checkbox" checked />
Labels
</label>
<label class="toggle">
<input id="trailsToggle" type="checkbox" checked />
Identity trails
</label>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const phaseText = document.querySelector("#phaseText");
const phaseDot = document.querySelector("#phaseDot");
const progressBar = document.querySelector("#progress");
const reactionButton = document.querySelector("#reactionButton");
const resetButton = document.querySelector("#resetButton");
const viewButton = document.querySelector("#viewButton");
const labelsToggle = document.querySelector("#labelsToggle");
const trailsToggle = document.querySelector("#trailsToggle");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101d);
scene.fog = new THREE.FogExp2(0x07101d, 0.025);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const world = new THREE.Group();
scene.add(world);
const ambient = new THREE.HemisphereLight(0xb9e8ff, 0x142033, 1.55);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4, 9, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x35dfff, 55, 18, 2);
cyanLight.position.set(-4, 1, 4);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xff655e, 42, 16, 2);
warmLight.position.set(4, -1, 2);
scene.add(warmLight);
const cameraState = {
radius: 13.1,
phi: 1.17,
theta: 0.56,
target: new THREE.Vector3(0, 0.15, 0)
};
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = 13.1;
cameraState.phi = 1.17;
cameraState.theta = 0.56;
cameraState.target.set(0, 0.15, 0);
updateCamera();
}
updateCamera();
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 makeAtomLabel(element, id, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 320;
labelCanvas.height = 112;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 320, 112);
roundedRect(ctx, 5, 7, 310, 98, 24);
ctx.fillStyle = "rgba(6, 16, 29, 0.9)";
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "800 46px system-ui, sans-serif";
ctx.textBaseline = "middle";
ctx.fillText(element, 25, 57);
ctx.fillStyle = "#e9f8ff";
ctx.font = "750 34px system-ui, sans-serif";
ctx.fillText(id, 92, 57);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(1.28, 0.45, 1);
sprite.position.set(0, 0.72, 0);
sprite.renderOrder = 20;
return sprite;
}
function makePlaque(title, subtitle, color) {
const c = document.createElement("canvas");
c.width = 640;
c.height = 160;
const ctx = c.getContext("2d");
roundedRect(ctx, 6, 6, 628, 148, 28);
ctx.fillStyle = "rgba(7, 18, 32, 0.76)";
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.font = "800 43px system-ui, sans-serif";
ctx.fillText(title, 320, 58);
ctx.fillStyle = "#a9c1d3";
ctx.font = "600 25px system-ui, sans-serif";
ctx.fillText(subtitle, 320, 110);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(3.4, 0.85, 1);
return sprite;
}
const chamber = new THREE.Group();
world.add(chamber);
const radius = 6.8;
const chamberHeight = 7.4;
const chamberY = 0.35;
const thetaStart = Math.PI * 5 / 12;
const thetaLength = Math.PI * 7 / 6;
const shellGeometry = new THREE.CylinderGeometry(
radius,
radius,
chamberHeight,
96,
1,
true,
thetaStart,
thetaLength
);
const shellMaterial = new THREE.MeshStandardMaterial({
color: 0x5e9fc4,
metalness: 0.35,
roughness: 0.62,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false
});
const shell = new THREE.Mesh(shellGeometry, shellMaterial);
shell.position.y = chamberY;
shell.renderOrder = -2;
chamber.add(shell);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x4ccde8,
transparent: true,
opacity: 0.52
});
function addChamberEdge(angle) {
const x = Math.sin(angle) * radius;
const z = Math.cos(angle) * radius;
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, chamberY - chamberHeight / 2, z),
new THREE.Vector3(x, chamberY + chamberHeight / 2, z)
]);
const line = new THREE.Line(geometry, edgeMaterial);
chamber.add(line);
}
addChamberEdge(thetaStart);
addChamberEdge(thetaStart + thetaLength);
function addArc(r, y, opacity) {
const points = [];
for (let i = 0; i <= 96; i++) {
const angle = thetaStart + (i / 96) * thetaLength;
points.push(
new THREE.Vector3(
Math.sin(angle) * r,
y,
Math.cos(angle) * r
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color: 0x4ccde8,
transparent: true,
opacity
});
chamber.add(new THREE.Line(geometry, material));
}
addArc(radius, chamberY - chamberHeight / 2, 0.38);
addArc(radius, chamberY + chamberHeight / 2, 0.2);
const floor = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 0.18, 96),
new THREE.MeshStandardMaterial({
color: 0x0c1b2b,
metalness: 0.5,
roughness: 0.55,
transparent: true,
opacity: 0.92
})
);
floor.position.y = chamberY - chamberHeight / 2 - 0.05;
floor.receiveShadow = true;
chamber.add(floor);
const grid = new THREE.GridHelper(13, 26, 0x39769b, 0x19354a);
grid.position.y = chamberY - chamberHeight / 2 + 0.055;
grid.material.transparent = true;
grid.material.opacity = 0.38;
chamber.add(grid);
const reactionRing = new THREE.Mesh(
new THREE.TorusGeometry(1.18, 0.025, 8, 96),
new THREE.MeshBasicMaterial({
color: 0x61e8ff,
transparent: true,
opacity: 0.25
})
);
reactionRing.rotation.x = Math.PI / 2;
reactionRing.position.set(0, -2.78, 0);
world.add(reactionRing);
const reactantPlaque = makePlaque(
"REACTANTS",
"2 H₂ + O₂",
"#ff9188"
);
reactantPlaque.position.set(-2.25, -2.25, 1.4);
world.add(reactantPlaque);
const productPlaque = makePlaque(
"PRODUCTS",
"2 H₂O",
"#72f0b8"
);
productPlaque.position.set(2.65, -2.25, 1.4);
world.add(productPlaque);
const zonePlaque = makePlaque(
"REACTION ZONE",
"IDENTITY-PRESERVING TRANSFORMATION",
"#61e8ff"
);
zonePlaque.scale.set(2.8, 0.7, 1);
zonePlaque.position.set(0, 3.65, -0.2);
world.add(zonePlaque);
const atomData = [
{
id: "H1",
element: "H",
color: 0xd8f8ff,
source: "Hydrogen molecule A",
destination: "Water molecule A",
initial: new THREE.Vector3(-3.45, 1.25, 0.1),
final: new THREE.Vector3(2.0, 1.35, 0.05)
},
{
id: "H2",
element: "H",
color: 0xd8f8ff,
source: "Hydrogen molecule A",
destination: "Water molecule A",
initial: new THREE.Vector3(-2.15, 1.25, 0.1),
final: new THREE.Vector3(0.75, 2.38, 0)
},
{
id: "H3",
element: "H",
color: 0xd8f8ff,
source: "Hydrogen molecule B",
destination: "Water molecule B",
initial: new THREE.Vector3(-3.55, -1.1, -0.15),
final: new THREE.Vector3(4.35, -1.05, 0.05)
},
{
id: "H4",
element: "H",
color: 0xd8f8ff,
source: "Hydrogen molecule B",
destination: "Water molecule B",
initial: new THREE.Vector3(-2.25, -1.1, -0.15),
final: new THREE.Vector3(3.1, -2.28, 0)
},
{
id: "O1",
element: "O",
color: 0xff796f,
source: "Oxygen molecule",
destination: "Water molecule A",
initial: new THREE.Vector3(-1.35, 0.05, 0.35),
final: new THREE.Vector3(0.75, 1.25, 0)
},
{
id: "O2",
element: "O",
color: 0xff796f,
source: "Oxygen molecule",
destination: "Water molecule B",
initial: new THREE.Vector3(-0.05, 0.05, 0.35),
final: new THREE.Vector3(3.1, -1.15, 0)
}
];
const atoms = new Map();
const pickableMeshes = [];
const identityPaths = [];
for (const data of atomData) {
const group = new THREE.Group();
group.position.copy(data.initial);
world.add(group);
const material = new THREE.MeshStandardMaterial({
color: data.color,
emissive: new THREE.Color(data.color).multiplyScalar(0.22),
emissiveIntensity: 1.15,
metalness: 0.18,
roughness: 0.28
});
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(data.element === "O" ? 0.43 : 0.32, 2),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.atomId = data.id;
group.add(mesh);
pickableMeshes.push(mesh);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(data.element === "O" ? 0.54 : 0.43, 0.018, 6, 40),
new THREE.MeshBasicMaterial({
color: data.color,
transparent: true,
opacity: 0.5,
depthWrite: false
})
);
group.add(ring);
const label = makeAtomLabel(
data.element,
data.id,
`#${new THREE.Color(data.color).getHexString()}`
);
group.add(label);
const midpoint = data.initial.clone().add(data.final).multiplyScalar(0.5);
midpoint.y += 1.3;
midpoint.z += 0.25;
const curve = new THREE.QuadraticBezierCurve3(
data.initial.clone(),
midpoint,
data.final.clone()
);
const pathMaterial = new THREE.LineDashedMaterial({
color: data.color,
transparent: true,
opacity: 0.14,
dashSize: 0.14,
gapSize: 0.1,
depthWrite: false
});
const pathLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(curve.getPoints(80)),
pathMaterial
);
pathLine.computeLineDistances();
world.add(pathLine);
identityPaths.push(pathLine);
const atom = {
...data,
group,
mesh,
ring,
label,
curve
};
atoms.set(data.id, atom);
}
const selectionHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.64, 0.035, 8, 48),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
})
);
selectionHalo.renderOrder = 30;
world.add(selectionHalo);
const bondMaterialBase = new THREE.MeshStandardMaterial({
color: 0x8dc9e5,
emissive: 0x245c77,
emissiveIntensity: 0.7,
metalness: 0.25,
roughness: 0.4,
transparent: true,
opacity: 1
});
const bondGeometry = new THREE.CylinderGeometry(0.055, 0.055, 1, 12);
const bonds = [];
function addBond(a, b, phase, offset = 0, double = false) {
const material = bondMaterialBase.clone();
const mesh = new THREE.Mesh(bondGeometry, material);
mesh.castShadow = true;
world.add(mesh);
bonds.push({ a, b, phase, offset, mesh, double });
}
addBond("H1", "H2", "reactant");
addBond("H3", "H4", "reactant");
addBond("O1", "O2", "reactant", -0.13, true);
addBond("O1", "O2", "reactant", 0.13, true);
addBond("O1", "H1", "product");
addBond("O1", "H2", "product");
addBond("O2", "H3", "product");
addBond("O2", "H4", "product");
const yAxis = new THREE.Vector3(0, 1, 0);
function updateBondMesh(bond) {
const start = atoms.get(bond.a).group.position;
const end = atoms.get(bond.b).group.position;
const direction = end.clone().sub(start);
const length = direction.length();
if (length < 0.001) {
bond.mesh.visible = false;
return;
}
bond.mesh.visible = true;
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const perpendicular = new THREE.Vector3(
-direction.y,
direction.x,
0
).normalize().multiplyScalar(bond.offset);
midpoint.add(perpendicular);
bond.mesh.position.copy(midpoint);
bond.mesh.quaternion.setFromUnitVectors(
yAxis,
direction.normalize()
);
bond.mesh.scale.set(1, length, 1);
}
let selectedId = "H1";
function selectAtom(id) {
selectedId = id;
const atom = atoms.get(id);
for (const [tokenId, item] of atoms) {
const selected = tokenId === id;
item.ring.material.opacity = selected ? 0.95 : 0.5;
item.mesh.scale.setScalar(selected ? 1.08 : 1);
}
for (const path of identityPaths) {
const isSelected = path.userData.atomId === id;
path.material.opacity = isSelected ? 0.82 : 0.12;
path.material.color.set(isSelected ? 0xffffff : path.userData.baseColor);
path.material.linewidth = isSelected ? 2 : 1;
}
const icon = document.querySelector("#selectedIcon");
icon.textContent = atom.element;
icon.className = atom.element === "O" ? "oxygen" : "hydrogen";
icon.style.background =
atom.element === "O"
? "rgba(255,121,111,0.09)"
: "rgba(216,248,255,0.08)";
icon.style.color =
atom.element === "O" ? "#ff796f" : "#d8f8ff";
document.querySelector("#selectedId").textContent = atom.id;
document.querySelector("#selectedElement").textContent =
`${atom.element === "O" ? "Oxygen" : "Hydrogen"} · atomic token`;
document.querySelector("#selectedSource").textContent = atom.source;
document.querySelector("#selectedDestination").textContent = atom.destination;
document.querySelector("#selectedStatus").textContent =
`Preserved · token ${atom.id}`;
for (const row of document.querySelectorAll(".token-row")) {
row.classList.toggle("selected", row.dataset.atom === id);
}
}
for (const path of identityPaths) {
const atomId = atoms.get(
path === identityPaths[0] ? "H1" :
path === identityPaths[1] ? "H2" :
path === identityPaths[2] ? "H3" :
path === identityPaths[3] ? "H4" :
path === identityPaths[4] ? "O1" : "O2"
).id;
path.userData.atomId = atomId;
path.userData.baseColor = atomId.startsWith("O") ? 0xff796f : 0xd8f8ff;
}
for (const row of document.querySelectorAll(".token-row")) {
row.addEventListener("click", () => selectAtom(row.dataset.atom));
}
selectAtom(selectedId);
let progress = 0;
let animation = null;
function smooth01(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function animateTo(target, duration = 2600) {
animation = {
from: progress,
to: target,
start: performance.now(),
duration
};
}
reactionButton.addEventListener("click", () => {
if (progress > 0.995) {
progress = 0;
}
animateTo(1, 2600);
});
resetButton.addEventListener("click", () => {
animateTo(0, 1700);
});
viewButton.addEventListener("click", resetCamera);
labelsToggle.addEventListener("change", () => {
for (const atom of atoms.values()) {
atom.label.visible = labelsToggle.checked;
}
});
trailsToggle.addEventListener("change", () => {
for (const path of identityPaths) {
path.visible = trailsToggle.checked;
}
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
if (Math.abs(dx) + Math.abs(dy) > 2) {
pointerMoved = true;
}
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.35,
1.48
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
pointerDown = false;
if (!pointerMoved) {
const rect = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickableMeshes, false);
if (hits.length) {
selectAtom(hits[0].object.userData.atomId);
}
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
7.5,
20
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "KeyR") resetCamera();
if (event.code === "Space") {
event.preventDefault();
reactionButton.click();
}
});
let previousPhase = "";
function updateScene(delta, elapsed) {
if (animation) {
const raw = THREE.MathUtils.clamp(
(performance.now() - animation.start) / animation.duration,
0,
1
);
progress = THREE.MathUtils.lerp(
animation.from,
animation.to,
raw
);
if (raw >= 1) {
progress = animation.to;
animation = null;
}
}
const easedProgress = smooth01(progress);
for (const atom of atoms.values()) {
const position = atom.curve.getPoint(easedProgress);
atom.group.position.copy(position);
atom.ring.quaternion.copy(camera.quaternion);
}
for (const bond of bonds) {
updateBondMesh(bond);
if (bond.phase === "reactant") {
bond.mesh.material.opacity =
1 - smooth01((progress - 0.18) / 0.32);
} else {
bond.mesh.material.opacity =
smooth01((progress - 0.48) / 0.34);
}
bond.mesh.visible = bond.mesh.material.opacity > 0.01;
}
const selectedAtom = atoms.get(selectedId);
selectionHalo.position.copy(selectedAtom.group.position);
selectionHalo.quaternion.copy(camera.quaternion);
selectionHalo.scale.setScalar(
1 + Math.sin(elapsed * 4.5) * 0.045
);
const transitionStrength =
Math.sin(Math.PI * THREE.MathUtils.clamp(progress, 0, 1));
reactionRing.material.opacity = 0.22 + transitionStrength * 0.7;
reactionRing.scale.setScalar(1 + transitionStrength * 0.18);
progressBar.style.width = `${progress * 100}%`;
let phase;
if (progress < 0.01) phase = "Reactants";
else if (progress > 0.99) phase = "Products";
else phase = "Reaction in progress";
if (phase !== previousPhase) {
phaseText.textContent = phase;
phaseDot.style.background =
phase === "Products"
? "#72f0b8"
: phase === "Reactants"
? "#ff796f"
: "#61e8ff";
phaseDot.style.boxShadow = `0 0 14px ${phaseDot.style.background}`;
reactionButton.textContent =
phase === "Products" ? "Replay reaction" : "Form products";
previousPhase = phase;
}
cyanLight.intensity = 48 + transitionStrength * 25;
warmLight.intensity = 38 + transitionStrength * 22;
}
const clock = new THREE.Clock();
function animate() {
const delta = clock.getDelta();
const elapsed = clock.elapsedTime;
updateScene(delta, elapsed);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```