Prompt specification
Build or customize the Three.js 3D scene "Concentration-unit exploration bench" 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: Concentration-unit exploration bench
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/concentration-unit-exploration-bench-x1996
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: concentration-unit exploration bench. Let users change amount and volume in a virtual solution; calculate a chosen concentration unit consistently and distinguish mass concentration from molar concentration. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (concentration-unit-exploration-bench-x1996):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="light">
<title>Concentration Lab — Voxel Solution Bench</title>
<style>
:root {
--ink: #173536;
--ink-2: #315152;
--muted: #71807c;
--paper: #f7f6f0;
--panel: #fbfaf6;
--line: #dfe3dc;
--molar: #087f83;
--molar-soft: #d9efeb;
--mass: #e96749;
--mass-soft: #fae4dc;
--lime: #d7ef65;
--shadow: 0 18px 50px rgba(30, 57, 52, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
min-height: 100%;
margin: 0;
}
body {
overflow: hidden;
color: var(--ink);
background: var(--paper);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
-webkit-font-smoothing: antialiased;
}
button,
input,
select {
font: inherit;
}
button {
color: inherit;
}
.app {
display: grid;
grid-template-rows: 66px minmax(0, 1fr);
width: 100vw;
height: 100vh;
}
.topbar {
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 24px;
color: #f7f6f0;
background: var(--ink);
border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
display: grid;
grid-template-columns: repeat(3, 7px);
grid-template-rows: repeat(3, 7px);
gap: 3px;
padding: 6px;
background: rgba(255, 255, 255, 0.08);
border: 1px solid rgba(255, 255, 255, 0.12);
}
.brand-mark i {
display: block;
width: 7px;
height: 7px;
background: var(--lime);
}
.brand-mark i:nth-child(1) {
grid-column: 1;
grid-row: 1;
}
.brand-mark i:nth-child(2) {
grid-column: 2;
grid-row: 1;
}
.brand-mark i:nth-child(3) {
grid-column: 2;
grid-row: 2;
}
.brand-mark i:nth-child(4) {
grid-column: 3;
grid-row: 2;
}
.brand-mark i:nth-child(5) {
grid-column: 1;
grid-row: 3;
}
.brand-mark i:nth-child(6) {
grid-column: 3;
grid-row: 3;
}
.brand-name {
display: flex;
align-items: baseline;
gap: 9px;
white-space: nowrap;
}
.brand-name strong {
font-size: 14px;
letter-spacing: 0.13em;
}
.brand-name span {
color: rgba(255, 255, 255, 0.48);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.top-status {
display: flex;
align-items: center;
gap: 18px;
color: rgba(255, 255, 255, 0.72);
font-size: 11px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.live {
display: flex;
align-items: center;
gap: 8px;
}
.live::before {
width: 7px;
height: 7px;
content: "";
background: var(--lime);
border-radius: 50%;
box-shadow: 0 0 0 5px rgba(215, 239, 101, 0.1);
}
.ghost-button {
padding: 8px 12px;
color: #f7f6f0;
background: transparent;
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 7px;
cursor: pointer;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: 160ms ease;
}
.ghost-button:hover {
color: var(--ink);
background: var(--lime);
border-color: var(--lime);
}
main {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
min-height: 0;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
isolation: isolate;
background: #dfe4dd;
}
.stage canvas {
position: absolute;
inset: 0;
z-index: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
.stage canvas:active {
cursor: grabbing;
}
.stage-copy {
position: absolute;
top: 28px;
left: 30px;
z-index: 3;
width: min(360px, calc(100% - 60px));
pointer-events: none;
}
.eyebrow {
margin: 0 0 9px;
color: #56706b;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.stage-copy h1 {
max-width: 330px;
margin: 0;
color: var(--ink);
font-size: clamp(28px, 3.1vw, 48px);
font-weight: 760;
letter-spacing: -0.055em;
line-height: 0.98;
}
.stage-copy p:last-child {
max-width: 310px;
margin: 13px 0 0;
color: #61736f;
font-size: 12px;
line-height: 1.55;
}
.mode-chip {
position: absolute;
top: 28px;
right: 28px;
z-index: 3;
padding: 9px 12px;
color: var(--molar);
background: rgba(248, 249, 244, 0.84);
border: 1px solid rgba(8, 127, 131, 0.18);
border-radius: 7px;
box-shadow: 0 8px 24px rgba(30, 57, 52, 0.08);
backdrop-filter: blur(12px);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.mode-chip[data-family="mass"] {
color: var(--mass);
border-color: rgba(233, 103, 73, 0.22);
}
.legend {
position: absolute;
bottom: 25px;
left: 27px;
z-index: 3;
display: flex;
flex-wrap: wrap;
gap: 9px 17px;
max-width: 430px;
padding: 11px 13px;
color: #516662;
background: rgba(248, 249, 244, 0.82);
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 8px;
box-shadow: 0 8px 24px rgba(30, 57, 52, 0.08);
backdrop-filter: blur(12px);
font-size: 10px;
pointer-events: none;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: #58b8b2;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.legend .solute-dot {
background: #ef8a42;
}
.legend .note {
color: #81908c;
}
.gesture {
position: absolute;
right: 26px;
bottom: 27px;
z-index: 3;
padding: 8px 10px;
color: #5e726d;
background: rgba(248, 249, 244, 0.72);
border: 1px solid rgba(255, 255, 255, 0.66);
border-radius: 7px;
backdrop-filter: blur(10px);
font-size: 10px;
pointer-events: none;
}
.world-label {
position: absolute;
z-index: 2;
min-width: 115px;
padding: 8px 10px;
color: var(--ink);
background: rgba(251, 250, 246, 0.88);
border: 1px solid rgba(255, 255, 255, 0.82);
border-radius: 7px;
box-shadow: 0 8px 22px rgba(30, 57, 52, 0.09);
backdrop-filter: blur(8px);
transform: translate(-50%, -50%);
pointer-events: none;
}
.world-label::after {
position: absolute;
bottom: -18px;
left: 50%;
width: 1px;
height: 18px;
content: "";
background: rgba(42, 74, 69, 0.35);
}
.world-label b {
display: block;
margin-bottom: 2px;
font-size: 9px;
letter-spacing: 0.13em;
}
.world-label small {
display: block;
color: #71807c;
font-size: 9px;
}
.panel {
z-index: 5;
overflow-y: auto;
padding: 25px 24px 30px;
background: var(--panel);
border-left: 1px solid var(--line);
box-shadow: -12px 0 40px rgba(30, 57, 52, 0.06);
scrollbar-width: thin;
scrollbar-color: #c7d0c9 transparent;
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
margin-bottom: 22px;
}
.panel-head h2 {
margin: 0;
font-size: 23px;
letter-spacing: -0.04em;
}
.panel-head p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.section {
padding: 17px 0 19px;
border-top: 1px solid var(--line);
}
.section:first-of-type {
border-top: 0;
padding-top: 0;
}
.section-title {
display: flex;
align-items: center;
gap: 9px;
margin: 0 0 13px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-title span {
display: grid;
width: 22px;
height: 22px;
place-items: center;
color: var(--ink);
background: #e8ece4;
border-radius: 5px;
font-size: 9px;
}
.field {
margin-bottom: 12px;
}
.field:last-child {
margin-bottom: 0;
}
.field-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: var(--ink-2);
font-size: 11px;
font-weight: 650;
}
.field-label small {
color: #89948f;
font-size: 9px;
font-weight: 500;
}
select,
.number-wrap {
width: 100%;
color: var(--ink);
background: #fff;
border: 1px solid #d8ded7;
border-radius: 8px;
outline: none;
transition: border-color 140ms ease, box-shadow 140ms ease;
}
select {
height: 42px;
padding: 0 34px 0 12px;
cursor: pointer;
appearance: none;
background-image:
linear-gradient(45deg, transparent 50%, #70807b 50%),
linear-gradient(135deg, #70807b 50%, transparent 50%);
background-position:
calc(100% - 16px) 18px,
calc(100% - 11px) 18px;
background-repeat: no-repeat;
background-size: 5px 5px, 5px 5px;
}
select:focus,
.number-wrap:focus-within {
border-color: #8bb6b2;
box-shadow: 0 0 0 3px rgba(8, 127, 131, 0.09);
}
.number-wrap {
display: flex;
align-items: center;
height: 43px;
overflow: hidden;
}
.number-wrap input {
width: 100%;
min-width: 0;
height: 100%;
padding: 0 12px;
color: var(--ink);
background: transparent;
border: 0;
outline: none;
font-size: 16px;
font-weight: 680;
}
.number-wrap input::-webkit-inner-spin-button {
opacity: 0.45;
}
.number-wrap .suffix {
padding: 0 12px 0 8px;
color: #87938e;
font-size: 10px;
font-weight: 700;
white-space: nowrap;
}
.unit-row {
display: grid;
grid-template-columns: minmax(0, 1fr) 150px;
gap: 8px;
align-items: stretch;
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 3px;
min-height: 43px;
padding: 3px;
background: #e9ede6;
border: 1px solid #d8ded7;
border-radius: 8px;
}
.segmented.two {
grid-template-columns: repeat(2, 1fr);
}
.segmented button {
padding: 0 7px;
color: #71807c;
background: transparent;
border: 0;
border-radius: 5px;
cursor: pointer;
font-size: 10px;
font-weight: 750;
transition: 140ms ease;
}
.segmented button:hover {
color: var(--ink);
}
.segmented button.active {
color: var(--ink);
background: #fff;
box-shadow: 0 2px 7px rgba(35, 58, 53, 0.09);
}
.hint {
margin: 7px 0 0;
color: #87938e;
font-size: 9px;
line-height: 1.45;
}
.quick-values {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin-top: 9px;
}
.quick-values button {
height: 29px;
color: #687873;
background: #f0f2ed;
border: 1px solid #e0e5dd;
border-radius: 6px;
cursor: pointer;
font-size: 9px;
font-weight: 700;
transition: 140ms ease;
}
.quick-values button:hover,
.quick-values button.active {
color: var(--ink);
background: #e4eee2;
border-color: #c9d9c9;
}
.result-card {
position: relative;
overflow: hidden;
padding: 18px;
background: var(--molar-soft);
border: 1px solid rgba(8, 127, 131, 0.15);
border-radius: 11px;
transition: 180ms ease;
}
.result-card[data-family="mass"] {
background: var(--mass-soft);
border-color: rgba(233, 103, 73, 0.18);
}
.result-card::after {
position: absolute;
top: 0;
right: 0;
width: 52px;
height: 52px;
content: "";
background:
linear-gradient(135deg, transparent 50%, rgba(255, 255, 255, 0.38) 50%);
}
.result-kicker {
color: var(--molar);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.result-card[data-family="mass"] .result-kicker {
color: var(--mass);
}
.result-number {
display: flex;
align-items: baseline;
gap: 8px;
margin-top: 8px;
overflow-wrap: anywhere;
}
.result-number strong {
font-size: clamp(27px, 3vw, 38px);
letter-spacing: -0.055em;
line-height: 1;
}
.result-number span {
color: var(--ink-2);
font-size: 14px;
font-weight: 750;
}
.result-note {
margin: 10px 0 0;
color: #55706b;
font-size: 10px;
line-height: 1.5;
}
.compare-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
.compare-card {
padding: 12px;
background: #f3f4ef;
border: 1px solid transparent;
border-radius: 8px;
transition: 150ms ease;
}
.compare-card.is-selected.mass-card {
background: var(--mass-soft);
border-color: rgba(233, 103, 73, 0.22);
}
.compare-card.is-selected.molar-card {
background: var(--molar-soft);
border-color: rgba(8, 127, 131, 0.2);
}
.compare-card b {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.compare-card strong {
display: block;
font-size: 13px;
letter-spacing: -0.025em;
}
.equation {
margin-top: 9px;
padding: 12px;
overflow-wrap: anywhere;
background: #eff2ec;
border-radius: 8px;
}
.equation b {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.equation code {
color: var(--ink-2);
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 10px;
line-height: 1.55;
}
.canonical {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 9px;
}
.canonical div {
padding: 10px 8px;
background: #f3f4ef;
border-radius: 7px;
}
.canonical span {
display: block;
margin-bottom: 4px;
color: #87938e;
font-size: 8px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.canonical strong {
font-size: 11px;
}
.distinction {
margin-top: 9px;
padding: 12px;
background: #173536;
border-radius: 9px;
color: rgba(255, 255, 255, 0.72);
font-size: 10px;
line-height: 1.5;
}
.distinction b {
color: var(--lime);
}
.conversion {
margin: 8px 0 0;
color: #c3d0cc;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
line-height: 1.5;
}
.warning {
display: none;
margin-top: 8px;
padding: 10px;
color: #9f3f2c;
background: #f9e1d9;
border-radius: 7px;
font-size: 10px;
line-height: 1.45;
}
.warning.visible {
display: block;
}
@media (max-width: 980px) {
body {
overflow: auto;
}
.app {
display: block;
height: auto;
min-height: 100vh;
}
.topbar {
position: sticky;
top: 0;
height: 62px;
}
main {
display: block;
}
.stage {
height: 65vh;
min-height: 480px;
}
.panel {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
.panel-head,
.section,
.result-card,
.compare-grid,
.equation,
.canonical,
.distinction {
max-width: 650px;
margin-left: auto;
margin-right: auto;
}
.top-status > span {
display: none;
}
}
@media (max-width: 600px) {
.topbar {
padding: 0 14px;
}
.brand-name span {
display: none;
}
.stage {
height: 58vh;
min-height: 420px;
}
.stage-copy {
top: 20px;
left: 18px;
}
.stage-copy h1 {
max-width: 250px;
font-size: 31px;
}
.stage-copy p:last-child {
display: none;
}
.mode-chip {
top: 20px;
right: 16px;
max-width: 140px;
text-align: right;
}
.legend {
right: 16px;
bottom: 17px;
left: 16px;
}
.gesture {
display: none;
}
.panel {
padding: 22px 17px 30px;
}
.unit-row {
grid-template-columns: 1fr;
}
.segmented {
min-height: 38px;
}
.compare-grid {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i>
</div>
<div class="brand-name">
<strong>CONCENTRATION LAB</strong>
<span>Voxel bench / 01</span>
</div>
</div>
<div class="top-status">
<span>Interactive solution station</span>
<div class="live">Live calculations</div>
<button class="ghost-button" id="resetAll" type="button">Reset</button>
</div>
</header>
<main>
<section class="stage" id="stage" aria-label="Interactive voxel concentration bench">
<div class="stage-copy">
<p class="eyebrow">Experiment 01 / Solution bench</p>
<h1>Concentration, made tangible.</h1>
<p>
Change the solute amount and final solution volume.
The voxel liquid and readout update together.
</p>
</div>
<div class="mode-chip" id="modeChip" data-family="molar">
Molar concentration · n / V
</div>
<div class="world-label" id="soluteLabel">
<b id="soluteLabelText">SOLUTE · NaCl</b>
<small id="soluteLabelSub">58.44 g/mol</small>
</div>
<div class="world-label" id="solventLabel">
<b>SOLUTION</b>
<small id="solventLabelSub">0.500 L</small>
</div>
<div class="legend">
<span><i></i> Solvent voxels</span>
<span><i class="solute-dot"></i> Solute markers</span>
<span class="note">Markers are symbolic, not literal particles</span>
</div>
<div class="gesture">Drag to orbit · Scroll to zoom</div>
</section>
<aside class="panel" aria-label="Concentration controls">
<div class="panel-head">
<div>
<h2>Build the solution</h2>
<p>Report units change; the solution does not.</p>
</div>
</div>
<section class="section">
<h3 class="section-title"><span>01</span> Solute</h3>
<div class="field">
<label class="field-label" for="soluteSelect">
Substance
<small>Preset molar mass</small>
</label>
<select id="soluteSelect">
<option value="sodiumChloride">Sodium chloride · NaCl</option>
<option value="glucose">Glucose · C₆H₁₂O₆</option>
<option value="caffeine">Caffeine · C₈H₁₀N₄O₂</option>
<option value="ethanol">Ethanol · C₂H₅OH</option>
<option value="copperSulfate">Copper(II) sulfate · CuSO₄</option>
</select>
</div>
<div class="field">
<label class="field-label" for="molarMass">
Molar mass, M
<small>Used for mass ↔ mole conversion</small>
</label>
<div class="number-wrap">
<input id="molarMass" type="number" min="0.001" step="0.001" value="58.44">
<span class="suffix">g/mol</span>
</div>
</div>
<div class="field">
<label class="field-label" for="amount">
Amount of solute
<small>Choose how amount is expressed</small>
</label>
<div class="unit-row">
<div class="number-wrap">
<input id="amount" type="number" min="0" step="any" value="0.25">
</div>
<div class="segmented" id="amountUnits" aria-label="Amount unit">
<button type="button" data-amount-unit="mol" class="active" aria-pressed="true">mol</button>
<button type="button" data-amount-unit="mmol" aria-pressed="false">mmol</button>
<button type="button" data-amount-unit="g" aria-pressed="false">g</button>
</div>
</div>
<p class="hint">
Choosing another amount unit preserves the physical amount of solute.
</p>
</div>
</section>
<section class="section">
<h3 class="section-title"><span>02</span> Solution volume</h3>
<div class="field">
<label class="field-label" for="volume">
Final solution volume
<small>Solution volume, not solvent volume</small>
</label>
<div class="unit-row">
<div class="number-wrap">
<input id="volume" type="number" min="0" step="any" value="0.5">
</div>
<div class="segmented two" id="volumeUnits" aria-label="Volume unit">
<button type="button" data-volume-unit="L" class="active" aria-pressed="true">L</button>
<button type="button" data-volume-unit="mL" aria-pressed="false">mL</button>
</div>
</div>
<div class="quick-values" id="quickVolumes" aria-label="Quick volume values">
<button type="button" data-volume-ml="100">100 mL</button>
<button type="button" data-volume-ml="250">250 mL</button>
<button type="button" data-volume-ml="500">500 mL</button>
<button type="button" data-volume-ml="1000">1 L</button>
</div>
</div>
</section>
<section class="section">
<h3 class="section-title"><span>03</span> Report</h3>
<div class="field">
<label class="field-label" for="outputUnit">
Concentration unit
<small>Choose the dimensional family</small>
</label>
<select id="outputUnit">
<optgroup label="Molar concentration — amount / volume">
<option value="mol/L">mol/L · M</option>
<option value="mmol/L">mmol/L · mM</option>
<option value="µmol/L">µmol/L · µM</option>
<option value="mol/mL">mol/mL</option>
</optgroup>
<optgroup label="Mass concentration — mass / volume">
<option value="g/L">g/L</option>
<option value="mg/L">mg/L</option>
<option value="µg/mL">µg/mL</option>
<option value="g/mL">g/mL</option>
<option value="mg/mL">mg/mL</option>
</optgroup>
</select>
</div>
<div class="result-card" id="resultCard" data-family="molar">
<div class="result-kicker" id="resultFamily">Selected result · Molar concentration</div>
<div class="result-number" aria-live="polite">
<strong id="resultNumber">0.500</strong>
<span id="resultUnit">mol/L</span>
</div>
<p class="result-note" id="resultNote">
Molar concentration uses the amount of solute in moles.
</p>
</div>
<div class="compare-grid">
<div class="compare-card mass-card" id="massCard">
<b>Mass concentration · m / V</b>
<strong id="massValue">29.22 g/L</strong>
</div>
<div class="compare-card molar-card" id="molarCard">
<b>Molar concentration · n / V</b>
<strong id="molarValue">0.500 mol/L</strong>
</div>
</div>
<div class="equation">
<b id="equationTitle">Molar concentration equation</b>
<code id="equationText">c = n / V</code>
</div>
<div class="canonical">
<div>
<span>Amount</span>
<strong id="canonicalAmount">0.250 mol</strong>
</div>
<div>
<span>Solute mass</span>
<strong id="canonicalMass">14.61 g</strong>
</div>
<div>
<span>Volume</span>
<strong id="canonicalVolume">0.500 L</strong>
</div>
</div>
<div class="distinction">
<b>Mass concentration is not molar concentration.</b>
Mass concentration measures grams of solute per volume.
Molar concentration measures moles of solute per volume.
<p class="conversion" id="conversionText"></p>
</div>
<div class="warning" id="warning" role="alert"></div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------------
// State and calculations
// ---------------------------------------------------------------------------
const substances = {
sodiumChloride: {
name: "Sodium chloride",
formula: "NaCl",
molarMass: 58.44
},
glucose: {
name: "Glucose",
formula: "C₆H₁₂O₆",
molarMass: 180.156
},
caffeine: {
name: "Caffeine",
formula: "C₈H₁₀N₄O₂",
molarMass: 194.19
},
ethanol: {
name: "Ethanol",
formula: "C₂H₅OH",
molarMass: 46.07
},
copperSulfate: {
name: "Copper(II) sulfate",
formula: "CuSO₄",
molarMass: 63.546
}
};
const outputUnits = {
"mol/L": {
family: "molar",
factor: 1,
label: "Molar concentration",
description: "Molar concentration uses the amount of solute in moles."
},
"mmol/L": {
family: "molar",
factor: 1000,
label: "Molar concentration",
description: "Molar concentration uses the amount of solute in moles."
},
"µmol/L": {
family: "molar",
factor: 1e6,
label: "Molar concentration",
description: "Molar concentration uses the amount of solute in moles."
},
"mol/mL": {
family: "molar",
factor: 0.001,
label: "Molar concentration",
description: "Molar concentration uses the amount of solute in moles."
},
"g/L": {
family: "mass",
factor: 1,
label: "Mass concentration",
description: "Mass concentration uses the mass of solute in grams."
},
"mg/L": {
family: "mass",
factor: 1000,
label: "Mass concentration",
description: "Mass concentration uses the mass of solute in grams."
},
"µg/mL": {
family: "mass",
factor: 1000,
label: "Mass concentration",
description: "Mass concentration uses the mass of solute in grams."
},
"g/mL": {
family: "mass",
factor: 0.001,
label: "Mass concentration",
description: "Mass concentration uses the mass of solute in grams."
},
"mg/mL": {
family: "mass",
factor: 1,
label: "Mass concentration",
description: "Mass concentration uses the mass of solute in grams."
}
};
const state = {
substanceKey: "sodiumChloride",
molarMass: 58.44,
amount: 0.25,
amountUnit: "mol",
volume: 0.5,
volumeUnit: "L",
outputUnit: "mol/L"
};
const $ = (id) => document.getElementById(id);
const amountInput = $("amount");
const volumeInput = $("volume");
const molarMassInput = $("molarMass");
const soluteSelect = $("soluteSelect");
const outputSelect = $("outputUnit");
const warning = $("warning");
function getMoles() {
if (state.amountUnit === "mol") return state.amount;
if (state.amountUnit === "mmol") return state.amount / 1000;
if (state.amountUnit === "g") {
return state.molarMass > 0 ? state.amount / state.molarMass : NaN;
}
return NaN;
}
function getVolumeLiters() {
return state.volumeUnit === "L" ? state.volume : state.volume / 1000;
}
function formatNumber(value, maxDigits = 4) {
if (!Number.isFinite(value)) return "—";
const absolute = Math.abs(value);
if (absolute !== 0 && (absolute >= 1e5 || absolute < 1e-4)) {
return value.toExponential(3).replace("e+", " × 10^").replace("e-", " × 10^-");
}
return new Intl.NumberFormat("en-US", {
maximumFractionDigits: maxDigits,
useGrouping: true
}).format(value);
}
function setText(id, value) {
$(id).textContent = value;
}
function updateSegmentButtons() {
document.querySelectorAll("[data-amount-unit]").forEach((button) => {
const active = button.dataset.amountUnit === state.amountUnit;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
document.querySelectorAll("[data-volume-unit]").forEach((button) => {
const active = button.dataset.volumeUnit === state.volumeUnit;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
}
function render() {
const moles = getMoles();
const volumeL = getVolumeLiters();
const massG = moles * state.molarMass;
const molarConcentration = volumeL > 0 ? moles / volumeL : NaN;
const massConcentration = volumeL > 0 ? massG / volumeL : NaN;
const output = outputUnits[state.outputUnit];
const selectedValue =
output.family === "molar"
? molarConcentration * output.factor
: massConcentration * output.factor;
const valid =
Number.isFinite(moles) &&
Number.isFinite(volumeL) &&
volumeL > 0 &&
state.molarMass > 0 &&
state.amount >= 0;
updateSegmentButtons();
document.querySelectorAll("[data-volume-ml]").forEach((button) => {
const target = Number(button.dataset.volumeMl) / 1000;
button.classList.toggle("active", Math.abs(target - volumeL) < 0.0005);
});
$("resultCard").dataset.family = output.family;
$("modeChip").dataset.family = output.family;
setText("resultNumber", valid ? formatNumber(selectedValue) : "—");
setText("resultUnit", state.outputUnit);
setText(
"resultFamily",
`Selected result · ${output.label}`
);
setText("resultNote", output.description);
setText(
"modeChip",
output.family === "molar"
? "Molar concentration · n / V"
: "Mass concentration · m / V"
);
setText(
"massValue",
valid ? `${formatNumber(massConcentration)} g/L` : "—"
);
setText(
"molarValue",
valid ? `${formatNumber(molarConcentration)} mol/L` : "—"
);
$("massCard").classList.toggle(
"is-selected",
output.family === "mass"
);
$("molarCard").classList.toggle(
"is-selected",
output.family === "molar"
);
setText(
"canonicalAmount",
valid ? `${formatNumber(moles)} mol` : "—"
);
setText(
"canonicalMass",
valid ? `${formatNumber(massG)} g` : "—"
);
setText(
"canonicalVolume",
Number.isFinite(volumeL) ? `${formatNumber(volumeL)} L` : "—"
);
if (valid) {
const baseText =
output.family === "molar"
? `c = n / V = (${formatNumber(moles)} mol) / (${formatNumber(volumeL)} L) = ${formatNumber(molarConcentration)} mol/L`
: `ρₘ = m / V = (${formatNumber(massG)} g) / (${formatNumber(volumeL)} L) = ${formatNumber(massConcentration)} g/L`;
const conversionText =
state.outputUnit === (output.family === "molar" ? "mol/L" : "g/L")
? ""
: ` → ${formatNumber(selectedValue)} ${state.outputUnit}`;
setText(
"equationTitle",
output.family === "molar"
? "Molar concentration equation"
: "Mass concentration equation"
);
setText("equationText", baseText + conversionText);
setText(
"conversionText",
`ρₘ = c × M = ${formatNumber(massConcentration)} g/L = ` +
`${formatNumber(molarConcentration)} mol/L × ${formatNumber(state.molarMass)} g/mol`
);
warning.classList.remove("visible");
warning.textContent = "";
} else {
setText("equationTitle", "Calculation unavailable");
setText("equationText", "Check the amount, molar mass, and solution volume.");
setText("conversionText", "");
warning.classList.add("visible");
if (!(state.molarMass > 0)) {
warning.textContent = "Molar mass must be greater than zero.";
} else if (!(volumeL > 0)) {
warning.textContent = "Solution volume must be greater than zero.";
} else {
warning.textContent = "Enter a valid, non-negative amount of solute.";
}
}
const substance = substances[state.substanceKey];
setText(
"soluteLabelText",
`SOLUTE · ${substance.formula}`
);
setText(
"soluteLabelSub",
`${formatNumber(state.molarMass)} g/mol`
);
setText(
"solventLabelSub",
Number.isFinite(volumeL)
? `${formatNumber(volumeL)} L solution`
: "Invalid volume"
);
updateScene(moles, volumeL, valid ? selectedValue : NaN, output);
}
amountInput.addEventListener("input", () => {
state.amount = Number(amountInput.value);
if (!Number.isFinite(state.amount)) state.amount = 0;
render();
});
volumeInput.addEventListener("input", () => {
state.volume = Number(volumeInput.value);
if (!Number.isFinite(state.volume)) state.volume = 0;
render();
});
molarMassInput.addEventListener("input", () => {
state.molarMass = Number(molarMassInput.value);
if (!Number.isFinite(state.molarMass)) state.molarMass = 0;
render();
});
outputSelect.addEventListener("change", () => {
state.outputUnit = outputSelect.value;
render();
});
soluteSelect.addEventListener("change", () => {
const previousMoles = getMoles();
state.substanceKey = soluteSelect.value;
const substance = substances[state.substanceKey];
state.molarMass = substance.molarMass;
molarMassInput.value = substance.molarMass;
if (state.amountUnit === "g" && Number.isFinite(previousMoles)) {
state.amount = previousMoles * substance.molarMass;
amountInput.value = formatNumber(state.amount, 6);
}
render();
});
document.querySelectorAll("[data-amount-unit]").forEach((button) => {
button.addEventListener("click", () => {
const moles = getMoles();
const nextUnit = button.dataset.amountUnit;
if (Number.isFinite(moles)) {
if (nextUnit === "mol") state.amount = moles;
if (nextUnit === "mmol") state.amount = moles * 1000;
if (nextUnit === "g") state.amount = moles * state.molarMass;
}
state.amountUnit = nextUnit;
amountInput.value = formatNumber(state.amount, 7);
render();
});
});
document.querySelectorAll("[data-volume-unit]").forEach((button) => {
button.addEventListener("click", () => {
const volumeL = getVolumeLiters();
const nextUnit = button.dataset.volumeUnit;
if (Number.isFinite(volumeL)) {
state.volume = nextUnit === "L" ? volumeL : volumeL * 1000;
}
state.volumeUnit = nextUnit;
volumeInput.value = formatNumber(state.volume, 7);
render();
});
});
document.querySelectorAll("[data-volume-ml]").forEach((button) => {
button.addEventListener("click", () => {
const targetLiters = Number(button.dataset.volumeMl) / 1000;
state.volume =
state.volumeUnit === "L" ? targetLiters : targetLiters * 1000;
volumeInput.value = formatNumber(state.volume, 7);
render();
});
});
$("resetAll").addEventListener("click", () => {
state.substanceKey = "sodiumChloride";
state.molarMass = 58.44;
state.amount = 0.25;
state.amountUnit = "mol";
state.volume = 0.5;
state.volumeUnit = "L";
state.outputUnit = "mol/L";
soluteSelect.value = state.substanceKey;
molarMassInput.value = state.molarMass;
amountInput.value = state.amount;
volumeInput.value = state.volume;
outputSelect.value = state.outputUnit;
render();
});
// ---------------------------------------------------------------------------
// Three.js voxel bench
// ---------------------------------------------------------------------------
const stage = $("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe4dd);
scene.fog = new THREE.Fog(0xdfe4dd, 16, 30);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.03;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D voxel concentration bench. Drag to orbit and scroll to zoom."
);
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 60);
const cameraTarget = new THREE.Vector3(0, 0.8, 0);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x73837b, 2.35);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff7df, 3.25);
keyLight.position.set(5, 10, 7);
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.00025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbce7e3, 1.15);
fillLight.position.set(-7, 5, -4);
scene.add(fillLight);
const materials = {
benchTop: new THREE.MeshStandardMaterial({
color: 0xd9c9a8,
roughness: 0.82
}),
benchEdge: new THREE.MeshStandardMaterial({
color: 0xb7a27f,
roughness: 0.9
}),
frame: new THREE.MeshStandardMaterial({
color: 0x315656,
roughness: 0.7
}),
mat: new THREE.MeshStandardMaterial({
color: 0x345d58,
roughness: 0.9
}),
white: new THREE.MeshStandardMaterial({
color: 0xf5f2e9,
roughness: 0.6
}),
dark: new THREE.MeshStandardMaterial({
color: 0x203d3d,
roughness: 0.55
}),
amber: new THREE.MeshStandardMaterial({
color: 0xd9783c,
roughness: 0.42
}),
glass: new THREE.MeshStandardMaterial({
color: 0xd9ffff,
transparent: true,
opacity: 0.15,
roughness: 0.12,
metalness: 0,
depthWrite: false,
side: THREE.DoubleSide
}),
solvent: new THREE.MeshStandardMaterial({
color: 0x5cbdb7,
transparent: true,
opacity: 0.48,
roughness: 0.3,
depthWrite: false
}),
solute: new THREE.MeshStandardMaterial({
color: 0xf28a3d,
emissive: 0x6c2105,
emissiveIntensity: 0.22,
roughness: 0.38
}),
lime: new THREE.MeshStandardMaterial({
color: 0xd7ef65,
roughness: 0.6
})
};
function makeBox(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Floor and voxel workbench.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xcdd5cd,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.05;
floor.receiveShadow = true;
scene.add(floor);
const floorGrid = new THREE.GridHelper(32, 32, 0xaebbb1, 0xb8c2b9);
floorGrid.position.y = -3.035;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.45;
scene.add(floorGrid);
makeBox(10.4, 0.5, 5.35, materials.benchTop, 0, 0, 0);
makeBox(10.5, 0.16, 5.45, materials.benchEdge, 0, -0.31, 0);
[-4.45, 4.45].forEach((x) => {
[-1.85, 1.85].forEach((z) => {
makeBox(0.42, 2.62, 0.42, materials.frame, x, -1.58, z);
});
});
makeBox(9.1, 0.24, 0.3, materials.frame, 0, -2.45, -1.85);
makeBox(9.1, 0.24, 0.3, materials.frame, 0, -2.45, 1.85);
makeBox(8.6, 0.72, 0.55, materials.frame, 0, -0.76, 2.36);
makeBox(1.35, 0.08, 0.08, materials.lime, 0, -0.76, 2.66);
makeBox(3.8, 0.055, 2.85, materials.mat, 0.15, 0.285, 0.05);
// Central voxel beaker.
const beaker = new THREE.Group();
beaker.position.set(0.15, 0, 0.15);
scene.add(beaker);
makeBox(2.16, 0.08, 1.54, materials.glass, 0, 0.38, 0, beaker);
makeBox(2.16, 2.88, 0.035, materials.glass, 0, 1.84, -0.755, beaker);
makeBox(2.16, 2.88, 0.035, materials.glass, 0, 1.84, 0.755, beaker);
makeBox(0.035, 2.88, 1.54, materials.glass, -1.065, 1.84, 0, beaker);
makeBox(0.035, 2.88, 1.54, materials.glass, 1.065, 1.84, 0, beaker);
beaker.children.forEach((child) => {
if (child.material === materials.glass) {
child.castShadow = false;
child.receiveShadow = false;
child.renderOrder = 3;
}
});
// Square voxel rim and corner posts.
makeBox(2.25, 0.09, 0.09, materials.white, 0, 3.31, -0.78, beaker);
makeBox(2.25, 0.09, 0.09, materials.white, 0, 3.31, 0.78, beaker);
makeBox(0.09, 0.09, 1.64, materials.white, -1.08, 3.31, 0, beaker);
makeBox(0.09, 0.09, 1.64, materials.white, 1.08, 3.31, 0, beaker);
[-1.08, 1.08].forEach((x) => {
[-0.78, 0.78].forEach((z) => {
makeBox(0.075, 2.98, 0.075, materials.white, x, 1.84, z, beaker);
});
});
// Graduation marks.
for (let i = 0; i < 7; i++) {
const width = i % 2 === 0 ? 0.34 : 0.22;
makeBox(
width,
0.035,
0.035,
materials.white,
0.55,
0.68 + i * 0.35,
0.792,
beaker
);
}
const solventGeometry = new THREE.BoxGeometry(0.255, 0.255, 0.255);
const solventMesh = new THREE.InstancedMesh(
solventGeometry,
materials.solvent,
315
);
solventMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
solventMesh.castShadow = false;
solventMesh.receiveShadow = false;
solventMesh.frustumCulled = false;
solventMesh.renderOrder = 1;
beaker.add(solventMesh);
const soluteGeometry = new THREE.BoxGeometry(0.16, 0.16, 0.16);
const soluteMesh = new THREE.InstancedMesh(
soluteGeometry,
materials.solute,
140
);
soluteMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
soluteMesh.castShadow = true;
soluteMesh.receiveShadow = true;
soluteMesh.frustumCulled = false;
beaker.add(soluteMesh);
// Voxel reagent bottle.
const bottle = new THREE.Group();
bottle.position.set(-3.15, 0.26, -0.68);
scene.add(bottle);
makeBox(0.9, 1.18, 0.72, materials.amber, 0, 0.62, 0, bottle);
makeBox(0.64, 0.28, 0.56, materials.amber, 0, 1.34, 0, bottle);
makeBox(0.68, 0.16, 0.6, materials.dark, 0, 1.56, 0, bottle);
makeBox(0.54, 0.09, 0.48, materials.lime, 0, 1.68, 0, bottle);
makeBox(1.15, 0.08, 0.86, materials.dark, 0, 0.03, 0, bottle);
// Voxel pipette on the bench.
const pipette = new THREE.Group();
pipette.position.set(2.65, 0.38, -1.42);
pipette.rotation.y = -0.56;
scene.add(pipette);
makeBox(0.18, 0.18, 1.45, materials.white, 0, 0, 0, pipette);
makeBox(0.12, 0.12, 0.55, materials.dark, 0, 0, 1.0, pipette);
makeBox(0.075, 0.075, 0.48, materials.amber, 0, 0, 1.48, pipette);
makeBox(0.24, 0.24, 0.25, materials.lime, 0, 0, -0.54, pipette);
// Small voxel spatula.
makeBox(0.18, 0.08, 1.55, materials.white, -2.1, 0.35, 1.65);
makeBox(0.42, 0.07, 0.52, materials.dark, -2.1, 0.35, 2.4);
// Digital bench readout.
makeBox(1.5, 0.12, 1.1, materials.dark, 3.2, 0.34, 0.35);
makeBox(0.22, 0.72, 0.22, materials.frame, 3.2, 0.72, 0.35);
makeBox(2.25, 1.18, 0.22, materials.dark, 3.2, 1.45, 0.35);
const readoutCanvas = document.createElement("canvas");
readoutCanvas.width = 640;
readoutCanvas.height = 320;
const readoutContext = readoutCanvas.getContext("2d");
const readoutTexture = new THREE.CanvasTexture(readoutCanvas);
readoutTexture.colorSpace = THREE.SRGBColorSpace;
const readoutSprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: readoutTexture,
transparent: true,
depthWrite: false
})
);
readoutSprite.position.set(3.2, 1.45, 0.51);
readoutSprite.scale.set(2.08, 1.04, 1);
scene.add(readoutSprite);
// Reagent bottle label.
const bottleCanvas = document.createElement("canvas");
bottleCanvas.width = 320;
bottleCanvas.height = 150;
const bottleContext = bottleCanvas.getContext("2d");
const bottleTexture = new THREE.CanvasTexture(bottleCanvas);
bottleTexture.colorSpace = THREE.SRGBColorSpace;
const bottleLabel = new THREE.Sprite(
new THREE.SpriteMaterial({
map: bottleTexture,
transparent: true,
depthWrite: false
})
);
bottleLabel.position.set(0, 0.76, 0.395);
bottleLabel.scale.set(1.02, 0.48, 1);
bottle.add(bottleLabel);
// Decorative calibration blocks.
makeBox(0.38, 0.38, 0.38, materials.lime, -3.8, 0.47, 1.55);
makeBox(0.38, 0.38, 0.38, materials.dark, -3.25, 0.47, 1.55);
makeBox(0.38, 0.38, 0.38, materials.white, -3.8, 0.47, 1.05);
const dummy = new THREE.Object3D();
function pseudoRandom(index, salt) {
const value = Math.sin((index + 1) * 12.9898 + salt * 78.233) * 43758.5453;
return value - Math.floor(value);
}
function drawBottleLabel() {
const substance = substances[state.substanceKey];
bottleContext.clearRect(0, 0, 320, 150);
bottleContext.fillStyle = "#fbf7e9";
bottleContext.fillRect(0, 0, 320, 150);
bottleContext.fillStyle = "#173536";
bottleContext.fillRect(0, 0, 320, 18);
bottleContext.fillStyle = "#173536";
bottleContext.font = "700 25px system-ui, sans-serif";
bottleContext.textAlign = "center";
bottleContext.fillText(substance.formula, 160, 65);
bottleContext.font = "500 18px system-ui, sans-serif";
bottleContext.fillStyle = "#536963";
bottleContext.fillText(`${formatNumber(state.molarMass)} g/mol`, 160, 98);
bottleContext.fillStyle = "#e96749";
bottleContext.fillRect(118, 118, 84, 7);
bottleTexture.needsUpdate = true;
}
function drawReadout(value, output) {
readoutContext.clearRect(0, 0, 640, 320);
readoutContext.fillStyle = "#173b3b";
readoutContext.fillRect(0, 0, 640, 320);
readoutContext.fillStyle = output.family === "molar" ? "#7bd1c8" : "#f08a70";
readoutContext.fillRect(0, 0, 18, 320);
readoutContext.fillStyle = "#93aaa4";
readoutContext.font = "700 18px ui-monospace, monospace";
readoutContext.textAlign = "left";
readoutContext.fillText("BENCH 01 / LIVE", 44, 45);
readoutContext.fillStyle = "#f7f4e9";
readoutContext.font = "700 50px ui-monospace, monospace";
readoutContext.fillText(
Number.isFinite(value) ? formatNumber(value) : "CHECK",
44,
125
);
readoutContext.fillStyle = output.family === "molar" ? "#7bd1c8" : "#f08a70";
readoutContext.font = "700 25px ui-monospace, monospace";
readoutContext.fillText(state.outputUnit, 44, 170);
readoutContext.fillStyle = "#78948d";
readoutContext.font = "500 17px ui-monospace, monospace";
readoutContext.fillText(
output.family === "molar" ? "c = n / V" : "rho_m = m / V",
44,
226
);
readoutContext.fillStyle = "#263f3e";
readoutContext.fillRect(44, 256, 552, 2);
readoutContext.fillStyle = "#91a49f";
readoutContext.font = "500 15px ui-monospace, monospace";
readoutContext.fillText(
`${formatNumber(getVolumeLiters())} L / ${formatNumber(getMoles())} mol`,
44,
289
);
readoutTexture.needsUpdate = true;
}
function updateScene(moles, volumeL, selectedValue, output) {
const volumeRatio = THREE.MathUtils.clamp(volumeL / 2, 0, 1);
const rows = volumeL > 0
? Math.max(1, Math.min(9, Math.ceil(Math.pow(volumeRatio, 0.62) * 9)))
: 0;
let solventIndex = 0;
for (let row = 0; row < rows; row++) {
for (let depth = 0; depth < 5; depth++) {
for (let column = 0; column < 7; column++) {
dummy.position.set(
(column - 3) * 0.27,
0.59 + row * 0.27,
(depth - 2) * 0.27
);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(1);
dummy.updateMatrix();
solventMesh.setMatrixAt(solventIndex++, dummy.matrix);
}
}
}
solventMesh.count = solventIndex;
solventMesh.instanceMatrix.needsUpdate = true;
let desiredSoluteCount = 0;
if (Number.isFinite(moles) && moles > 0 && rows > 0) {
desiredSoluteCount = Math.min(
140,
Math.round(Math.pow(Math.min(moles, 2) / 2, 0.55) * 120)
);
desiredSoluteCount = Math.min(desiredSoluteCount, rows * 35);
}
for (let i = 0; i < desiredSoluteCount; i++) {
const column = Math.floor(pseudoRandom(i, 1) * 7);
const depth = Math.floor(pseudoRandom(i, 2) * 5);
const row = Math.floor(pseudoRandom(i, 3) * rows);
const scale = 0.78 + pseudoRandom(i, 4) * 0.38;
dummy.position.set(
(column - 3) * 0.27,
0.59 + row * 0.27,
(depth - 2) * 0.27
);
dummy.rotation.set(
pseudoRandom(i, 5) * 0.28,
pseudoRandom(i, 6) * 0.28,
pseudoRandom(i, 7) * 0.28
);
dummy.scale.setScalar(scale);
dummy.updateMatrix();
soluteMesh.setMatrixAt(i, dummy.matrix);
}
soluteMesh.count = desiredSoluteCount;
soluteMesh.instanceMatrix.needsUpdate = true;
drawBottleLabel();
drawReadout(selectedValue, output);
}
// ---------------------------------------------------------------------------
// Custom camera controls — no addons are used.
// ---------------------------------------------------------------------------
const cameraState = {
theta: 0.66,
phi: 1.04,
radius: 12.7,
dragging: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.theta = 0.66;
cameraState.phi = 1.04;
cameraState.radius = 12.7;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
cameraState.dragging = true;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!cameraState.dragging) return;
const deltaX = event.clientX - cameraState.lastX;
const deltaY = event.clientY - cameraState.lastY;
cameraState.lastX = event.clientX;
cameraState.lastY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.006,
0.48,
1.38
);
updateCamera();
});
function endDrag(event) {
cameraState.dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.4,
18
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("keydown", (event) => {
const step = 0.08;
if (event.key === "ArrowLeft") cameraState.theta += step;
if (event.key === "ArrowRight") cameraState.theta -= step;
if (event.key === "ArrowUp") cameraState.phi = Math.max(0.48, cameraState.phi - step);
if (event.key === "ArrowDown") cameraState.phi = Math.min(1.38, cameraState.phi + step);
if (event.key === "+" || event.key === "=") {
cameraState.radius = Math.max(7.4, cameraState.radius - 0.7);
}
if (event.key === "-" || event.key === "_") {
cameraState.radius = Math.min(18, cameraState.radius + 0.7);
}
updateCamera();
});
const soluteAnchor = new THREE.Vector3(-3.15, 2.18, -0.35);
const solventAnchor = new THREE.Vector3(1.45, 2.05, 0.55);
function positionWorldLabel(element, anchor) {
const projected = anchor.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * stage.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * stage.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
updateCamera();
render();
function animate() {
positionWorldLabel($("soluteLabel"), soluteAnchor);
positionWorldLabel($("solventLabel"), solventAnchor);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```