Prompt specification
Build or customize the Three.js 3D scene "Logarithmic concentration explorer" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Logarithmic concentration explorer
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/logarithmic-concentration-explorer-x1924
Scene Description Prompt:
Explore pH versus positive hydronium concentration in a low-poly dilute-solution model that omits activity corrections. One HTML file using Three.js from cdnjs.
Working Three.js Starter (logarithmic-concentration-explorer-x1924):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>pH × Positive Hydronium Concentration</title>
<style>
:root {
color-scheme: dark;
--bg: #06101d;
--panel: rgba(9, 22, 38, 0.86);
--panel-border: rgba(145, 207, 255, 0.16);
--text: #edf7ff;
--muted: #91a9bd;
--cyan: #58d9ff;
--pink: #ff4775;
--green: #61efb2;
--amber: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 35% 35%, #102943 0%, #081727 38%, #040b14 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.brand {
position: fixed;
z-index: 2;
top: 26px;
left: 28px;
max-width: 520px;
pointer-events: none;
text-shadow: 0 2px 24px rgba(0, 0, 0, 0.45);
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 0.72rem;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(2rem, 4vw, 4.25rem);
font-weight: 760;
letter-spacing: -0.055em;
line-height: 0.95;
}
.brand p {
max-width: 430px;
margin: 14px 0 0;
color: #b3c8d9;
font-size: 0.95rem;
line-height: 1.55;
}
.panel {
position: fixed;
z-index: 4;
top: 24px;
right: 24px;
width: min(370px, calc(100vw - 48px));
max-height: calc(100vh - 48px);
overflow: auto;
padding: 18px;
border: 1px solid var(--panel-border);
border-radius: 22px;
background: var(--panel);
box-shadow:
0 24px 80px rgba(0, 0, 0, 0.38),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(20px) saturate(125%);
scrollbar-width: thin;
scrollbar-color: #28465d transparent;
}
.panel-head {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 17px;
color: #c9dbe8;
font-size: 0.78rem;
font-weight: 680;
letter-spacing: 0.02em;
}
.status-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 14px rgba(97, 239, 178, 0.8);
}
.chip {
margin-left: auto;
padding: 5px 7px;
border: 1px solid rgba(255, 209, 102, 0.2);
border-radius: 999px;
background: rgba(255, 209, 102, 0.07);
color: var(--amber);
font-size: 0.62rem;
font-weight: 650;
white-space: nowrap;
}
.control-title {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 16px;
margin-bottom: 10px;
}
.control-title label {
color: var(--muted);
font-size: 0.76rem;
font-weight: 680;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.ph-readout {
color: var(--pink);
font-size: 2.5rem;
font-weight: 780;
line-height: 1;
letter-spacing: -0.06em;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 20px;
margin: 0;
appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 6px;
border-radius: 999px;
background:
linear-gradient(
90deg,
#ff4775 0%,
#d6589d 36%,
#58d9ff 50%,
#62e4c0 72%,
#ffd166 100%
);
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.12);
}
input[type="range"]::-moz-range-track {
height: 6px;
border-radius: 999px;
background:
linear-gradient(
90deg,
#ff4775 0%,
#d6589d 36%,
#58d9ff 50%,
#62e4c0 72%,
#ffd166 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
margin-top: -6.5px;
appearance: none;
border: 3px solid #f6fbff;
border-radius: 50%;
background: #ff4775;
box-shadow:
0 0 0 4px rgba(255, 71, 117, 0.18),
0 3px 12px rgba(0, 0, 0, 0.5);
}
input[type="range"]::-moz-range-thumb {
width: 15px;
height: 15px;
border: 3px solid #f6fbff;
border-radius: 50%;
background: #ff4775;
box-shadow: 0 0 0 4px rgba(255, 71, 117, 0.18);
}
.scale {
display: flex;
justify-content: space-between;
margin-top: 3px;
color: #708ba0;
font-size: 0.66rem;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin: 16px 0;
}
.presets button,
.tool-button {
border: 1px solid rgba(151, 210, 255, 0.14);
border-radius: 11px;
background: rgba(105, 180, 230, 0.07);
color: #c5d9e8;
cursor: pointer;
transition:
border-color 140ms ease,
background 140ms ease,
transform 140ms ease;
}
.presets button {
padding: 9px 7px;
font-size: 0.73rem;
font-weight: 650;
}
.presets button:hover,
.tool-button:hover {
border-color: rgba(88, 217, 255, 0.46);
background: rgba(88, 217, 255, 0.12);
}
.presets button:active,
.tool-button:active {
transform: translateY(1px);
}
.concentration-card {
padding: 16px;
border: 1px solid rgba(255, 71, 117, 0.17);
border-radius: 16px;
background:
radial-gradient(circle at 100% 0%, rgba(255, 71, 117, 0.11), transparent 55%),
rgba(255, 255, 255, 0.025);
}
.card-label {
color: var(--muted);
font-size: 0.69rem;
font-weight: 680;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.concentration-value {
display: flex;
align-items: baseline;
gap: 7px;
margin: 5px 0 12px;
}
#concentrationValue {
color: #fff;
font-size: 1.72rem;
font-weight: 760;
letter-spacing: -0.045em;
font-variant-numeric: tabular-nums;
}
#concentrationUnit {
color: var(--pink);
font-size: 0.76rem;
font-weight: 650;
}
.formula {
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.075);
color: #9db5c7;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.7rem;
line-height: 1.5;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin: 10px 0 15px;
}
.stat {
min-width: 0;
padding: 12px;
border: 1px solid rgba(143, 202, 245, 0.1);
border-radius: 13px;
background: rgba(255, 255, 255, 0.025);
}
.stat strong {
display: block;
overflow: hidden;
margin-top: 6px;
color: #eef9ff;
font-size: 0.83rem;
font-weight: 690;
text-overflow: ellipsis;
white-space: nowrap;
}
.graph-wrap {
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.075);
}
.graph-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 2px;
}
.graph-heading span:last-child {
color: #718da3;
font-size: 0.64rem;
}
.graph-wrap svg {
display: block;
width: 100%;
height: auto;
overflow: visible;
}
.graph-wrap text {
fill: #809aae;
font-family: ui-sans-serif, system-ui, sans-serif;
font-size: 9px;
}
.graph-wrap .axis-label {
fill: #b7ccda;
font-size: 9.5px;
font-weight: 650;
}
.controls-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
}
.toggle {
display: flex;
align-items: center;
gap: 8px;
color: #9db5c7;
font-size: 0.73rem;
cursor: pointer;
}
.toggle input {
width: 15px;
height: 15px;
accent-color: var(--cyan);
}
.note {
margin: 15px 0 0;
padding: 12px 13px;
border: 1px solid rgba(255, 209, 102, 0.12);
border-radius: 12px;
background: rgba(255, 209, 102, 0.045);
color: #9eafba;
font-size: 0.69rem;
line-height: 1.55;
}
.note strong {
color: #d4c59d;
}
.legend {
position: fixed;
z-index: 2;
left: 28px;
bottom: 25px;
display: flex;
align-items: center;
gap: 15px;
padding: 10px 13px;
border: 1px solid rgba(143, 202, 245, 0.11);
border-radius: 13px;
background: rgba(5, 15, 27, 0.58);
color: #94aebe;
font-size: 0.7rem;
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
background: var(--pink);
box-shadow: 0 0 9px rgba(255, 71, 117, 0.65);
}
.swatch.water {
background: var(--cyan);
box-shadow: 0 0 9px rgba(88, 217, 255, 0.45);
}
.view-tools {
position: fixed;
z-index: 5;
right: 26px;
bottom: 25px;
display: flex;
gap: 7px;
}
.tool-button {
width: 39px;
height: 39px;
display: grid;
place-items: center;
background: rgba(7, 20, 35, 0.78);
color: #c9e1f1;
font-size: 1.1rem;
backdrop-filter: blur(12px);
}
.tool-button.home {
width: auto;
padding: 0 12px;
font-size: 0.72rem;
}
.interaction-hint {
position: fixed;
z-index: 2;
left: 50%;
bottom: 25px;
transform: translateX(-50%);
color: rgba(181, 207, 224, 0.62);
font-size: 0.69rem;
letter-spacing: 0.03em;
pointer-events: none;
}
@media (max-width: 980px) {
.brand {
top: 18px;
left: 18px;
}
.brand p {
max-width: 360px;
font-size: 0.82rem;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: min(370px, calc(100vw - 24px));
max-height: 48vh;
border-radius: 18px;
}
.legend,
.interaction-hint {
display: none;
}
.view-tools {
right: 15px;
bottom: calc(48vh + 24px);
}
}
@media (max-width: 560px) {
.brand {
top: 14px;
left: 14px;
}
.brand h1 {
font-size: 2rem;
}
.brand p {
display: none;
}
.panel {
padding: 14px;
}
.stats {
grid-template-columns: 1fr;
}
.view-tools {
bottom: calc(48vh + 24px);
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional hydronium solution"></div>
<header class="brand">
<div class="eyebrow">Low-poly · ideal dilute solution</div>
<h1>pH × hydronium</h1>
<p>
Explore how the concentration of positively charged hydronium ions changes
across fourteen orders of magnitude.
</p>
</header>
<aside class="panel" aria-label="Model controls and measurements">
<div class="panel-head">
<span class="status-dot"></span>
<span>Ideal concentration model</span>
<span class="chip">activity corrections off</span>
</div>
<div class="control-title">
<label for="phRange">pH</label>
<output id="phOutput" class="ph-readout" for="phRange">7.00</output>
</div>
<input
id="phRange"
type="range"
min="0"
max="14"
step="0.01"
value="7"
aria-label="pH"
>
<div class="scale">
<span>0 · acidic</span>
<span>7</span>
<span>14 · basic</span>
</div>
<div class="presets">
<button type="button" data-ph="3">pH 3</button>
<button type="button" data-ph="7">pH 7</button>
<button type="button" data-ph="11">pH 11</button>
</div>
<section class="concentration-card">
<div class="card-label">[H₃O⁺]</div>
<div class="concentration-value">
<span id="concentrationValue">100</span>
<span id="concentrationUnit">nmol L⁻¹</span>
</div>
<div class="formula">
[H₃O⁺] = 10<sup>−pH</sup> mol L⁻¹<br>
pH = −log₁₀([H₃O⁺] / 1 mol L⁻¹)
</div>
</section>
<div class="stats">
<div class="stat">
<span class="card-label">Ions in 250 mL</span>
<strong id="ionCount">—</strong>
</div>
<div class="stat">
<span class="card-label">Positive charge</span>
<strong id="chargeValue">—</strong>
</div>
</div>
<div class="graph-wrap">
<div class="graph-heading">
<span class="card-label">pH versus [H₃O⁺]</span>
<span>concentration axis is logarithmic</span>
</div>
<svg
viewBox="0 0 360 180"
role="img"
aria-label="Graph of pH against logarithmic hydronium concentration"
>
<defs>
<linearGradient id="lineGradient" x1="0" x2="1">
<stop offset="0%" stop-color="#58d9ff"/>
<stop offset="50%" stop-color="#c280ff"/>
<stop offset="100%" stop-color="#ff4775"/>
</linearGradient>
</defs>
<rect
x="46"
y="18"
width="278"
height="126"
rx="8"
fill="rgba(105,180,230,0.025)"
stroke="rgba(132,187,224,0.1)"
/>
<g opacity="0.55">
<line x1="46" y1="81" x2="324" y2="81" stroke="#294158" stroke-dasharray="3 5"/>
<line x1="96" y1="18" x2="96" y2="144" stroke="#294158" stroke-dasharray="3 5"/>
<line x1="245" y1="18" x2="245" y2="144" stroke="#294158" stroke-dasharray="3 5"/>
</g>
<line
x1="46"
y1="18"
x2="324"
y2="144"
stroke="url(#lineGradient)"
stroke-width="3"
stroke-linecap="round"
/>
<line id="graphCrossH" x1="46" x2="324" stroke="#ff4775" stroke-opacity="0.45"/>
<line id="graphCrossV" y1="18" y2="144" stroke="#ff4775" stroke-opacity="0.45"/>
<circle
id="graphDot"
r="5.5"
fill="#ff4775"
stroke="#fff"
stroke-width="2"
/>
<text x="46" y="159" text-anchor="middle">10⁻¹⁴ M</text>
<text x="185" y="159" text-anchor="middle">10⁻⁷ M</text>
<text x="324" y="159" text-anchor="middle">1 M</text>
<text x="37" y="148" text-anchor="end">0</text>
<text x="37" y="84" text-anchor="end">7</text>
<text x="37" y="21" text-anchor="end">14</text>
<text x="185" y="176" text-anchor="middle" class="axis-label">
[H₃O⁺] →
</text>
<text
x="13"
y="81"
text-anchor="middle"
class="axis-label"
transform="rotate(-90 13 81)"
>
pH
</text>
</svg>
</div>
<div class="controls-row">
<label class="toggle">
<input id="motionToggle" type="checkbox" checked>
Particle motion
</label>
<span class="card-label" id="markerCount">— / 900 markers</span>
</div>
<p class="note">
<strong>Dilute-solution approximation.</strong>
Activity coefficients, ionic-strength corrections, water autoionization,
and electroneutrality are not modeled. The rendered ions are capped,
logarithmically scaled schematic markers rather than one marker per molecule.
</p>
</aside>
<div class="legend">
<span class="legend-item">
<span class="swatch"></span>
H₃O⁺
</span>
<span class="legend-item">
<span class="swatch water"></span>
Dilute solution
</span>
</div>
<div class="interaction-hint">Drag to orbit · scroll to zoom</div>
<div class="view-tools" aria-label="Camera controls">
<button id="zoomIn" class="tool-button" type="button" aria-label="Zoom in">+</button>
<button id="zoomOut" class="tool-button" type="button" aria-label="Zoom out">−</button>
<button id="resetView" class="tool-button home" type="button">Reset</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
// ---------------------------------------------------------------------
// Model constants
// ---------------------------------------------------------------------
const MAX_MARKERS = 900;
const SAMPLE_VOLUME_L = 0.25;
const FARADAY = 96485.33212;
const phRange = $("phRange");
const motionToggle = $("motionToggle");
let currentPH = Number(phRange.value);
let currentConcentration = Math.pow(10, -currentPH);
let visibleMarkers = 0;
let simulationTime = 0;
// ---------------------------------------------------------------------
// Three.js renderer and scene
// ---------------------------------------------------------------------
const host = $("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(host.clientWidth, host.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.13;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Low-poly beaker containing animated hydronium ions"
);
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06101d);
scene.fog = new THREE.FogExp2(0x06101d, 0.037);
const camera = new THREE.PerspectiveCamera(
42,
host.clientWidth / host.clientHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.05, 0);
let currentYaw = 0.66;
let currentPitch = 0.31;
let currentRadius = 9.3;
let goalYaw = currentYaw;
let goalPitch = currentPitch;
let goalRadius = currentRadius;
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9bdfff, 0x091421, 1.75));
const keyLight = new THREE.DirectionalLight(0xd8f2ff, 3.2);
keyLight.position.set(4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 20;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x416dff, 1.55);
rimLight.position.set(-5, 3, -5);
scene.add(rimLight);
const pinkLight = new THREE.PointLight(0xff2f68, 1.1, 9, 2);
pinkLight.position.set(0.5, 0.8, 3.2);
scene.add(pinkLight);
// ---------------------------------------------------------------------
// Low-poly environment
// ---------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.CircleGeometry(12, 32),
new THREE.MeshStandardMaterial({
color: 0x07111c,
roughness: 0.94,
metalness: 0.05
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.44;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 18, 0x1d4965, 0x102a3d);
grid.position.y = -2.425;
grid.material.transparent = true;
grid.material.opacity = 0.27;
scene.add(grid);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(2.42, 2.62, 0.16, 12),
new THREE.MeshStandardMaterial({
color: 0x0d1c2a,
roughness: 0.7,
metalness: 0.18,
flatShading: true
})
);
pedestal.position.y = -2.34;
pedestal.receiveShadow = true;
pedestal.castShadow = true;
scene.add(pedestal);
const glassGeometry = new THREE.CylinderGeometry(
2.13,
2.02,
4.45,
12,
1,
true
);
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0x9bdfff,
roughness: 0.17,
metalness: 0,
transparent: true,
opacity: 0.12,
transmission: 0.08,
side: THREE.DoubleSide,
depthWrite: false
});
const glass = new THREE.Mesh(glassGeometry, glassMaterial);
glass.renderOrder = 4;
scene.add(glass);
const glassEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(glassGeometry, 12),
new THREE.LineBasicMaterial({
color: 0xa5dcff,
transparent: true,
opacity: 0.34
})
);
glassEdges.renderOrder = 5;
scene.add(glassEdges);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(2.13, 0.045, 4, 12),
new THREE.MeshStandardMaterial({
color: 0x9edcff,
roughness: 0.28,
metalness: 0.25,
transparent: true,
opacity: 0.72,
flatShading: true
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = 2.225;
rim.renderOrder = 5;
scene.add(rim);
const bottomGlass = new THREE.Mesh(
new THREE.CylinderGeometry(2.04, 2.04, 0.12, 12),
new THREE.MeshPhysicalMaterial({
color: 0x8ed6f7,
roughness: 0.22,
transparent: true,
opacity: 0.24,
depthWrite: false
})
);
bottomGlass.position.y = -2.19;
bottomGlass.renderOrder = 4;
scene.add(bottomGlass);
const waterGeometry = new THREE.CylinderGeometry(
1.99,
1.91,
3.68,
12,
1,
false
);
const water = new THREE.Mesh(
waterGeometry,
new THREE.MeshPhysicalMaterial({
color: 0x1686bd,
roughness: 0.28,
metalness: 0,
transparent: true,
opacity: 0.11,
side: THREE.DoubleSide,
depthWrite: false
})
);
water.position.y = -0.04;
water.renderOrder = 0;
scene.add(water);
const waterSurface = new THREE.Mesh(
new THREE.CylinderGeometry(1.99, 1.99, 0.025, 12),
new THREE.MeshPhysicalMaterial({
color: 0x55c9ee,
roughness: 0.2,
metalness: 0.08,
transparent: true,
opacity: 0.25,
depthWrite: false
})
);
waterSurface.position.y = 1.79;
waterSurface.renderOrder = 1;
scene.add(waterSurface);
for (const radius of [1.25, 1.57]) {
const ripple = new THREE.Mesh(
new THREE.RingGeometry(radius, radius + 0.018, 12),
new THREE.MeshBasicMaterial({
color: 0x76ddff,
transparent: true,
opacity: 0.2,
side: THREE.DoubleSide,
depthWrite: false
})
);
ripple.rotation.x = -Math.PI / 2;
ripple.position.y = 1.812;
ripple.renderOrder = 2;
scene.add(ripple);
}
// ---------------------------------------------------------------------
// Hydronium particle system
//
// Oxygen and hydrogen atoms are rendered with instanced meshes, keeping
// draw calls low even when hundreds of schematic ions are visible.
// ---------------------------------------------------------------------
const oxygenMaterial = new THREE.MeshStandardMaterial({
color: 0xff365f,
emissive: 0x7a0825,
emissiveIntensity: 0.48,
roughness: 0.38,
metalness: 0.06,
flatShading: true
});
const hydrogenMaterial = new THREE.MeshStandardMaterial({
color: 0xf2fbff,
emissive: 0x8bdfff,
emissiveIntensity: 0.2,
roughness: 0.52,
flatShading: true
});
const oxygenMesh = new THREE.InstancedMesh(
new THREE.OctahedronGeometry(0.105, 0),
oxygenMaterial,
MAX_MARKERS
);
const hydrogenMesh = new THREE.InstancedMesh(
new THREE.TetrahedronGeometry(0.055, 0),
hydrogenMaterial,
MAX_MARKERS * 3
);
oxygenMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
hydrogenMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
oxygenMesh.frustumCulled = false;
hydrogenMesh.frustumCulled = false;
oxygenMesh.renderOrder = 3;
hydrogenMesh.renderOrder = 3;
scene.add(oxygenMesh, hydrogenMesh);
const hydrogenOffsets = [
new THREE.Vector3(0.12, 0.075, 0),
new THREE.Vector3(-0.06, 0.075, 0.104),
new THREE.Vector3(-0.06, 0.075, -0.104)
];
const ions = [];
for (let i = 0; i < MAX_MARKERS; i++) {
const angle = Math.random() * Math.PI * 2;
const radius = Math.sqrt(Math.random()) * 1.72;
const y = -1.68 + Math.random() * 3.18;
const randomDirection = new THREE.Vector3(
Math.random() - 0.5,
Math.random() - 0.5,
Math.random() - 0.5
).normalize();
ions.push({
position: new THREE.Vector3(
Math.cos(angle) * radius,
y,
Math.sin(angle) * radius
),
velocity: randomDirection.multiplyScalar(
0.025 + Math.random() * 0.025
),
quaternion: new THREE.Quaternion().setFromEuler(
new THREE.Euler(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
)
),
axis: new THREE.Vector3(
Math.random() - 0.5,
Math.random() - 0.5,
Math.random() - 0.5
).normalize(),
spin: (Math.random() - 0.5) * 0.65
});
}
const dummy = new THREE.Object3D();
const tempOffset = new THREE.Vector3();
const spinQuaternion = new THREE.Quaternion();
const workingQuaternion = new THREE.Quaternion();
function updateParticleMatrices(time) {
for (let i = 0; i < visibleMarkers; i++) {
const ion = ions[i];
workingQuaternion.copy(ion.quaternion);
spinQuaternion.setFromAxisAngle(ion.axis, time * ion.spin);
workingQuaternion.multiply(spinQuaternion);
const ionScale = 1.18;
dummy.position.copy(ion.position);
dummy.quaternion.copy(workingQuaternion);
dummy.scale.setScalar(ionScale);
dummy.updateMatrix();
oxygenMesh.setMatrixAt(i, dummy.matrix);
for (let h = 0; h < 3; h++) {
tempOffset
.copy(hydrogenOffsets[h])
.multiplyScalar(ionScale)
.applyQuaternion(workingQuaternion)
.add(ion.position);
dummy.position.copy(tempOffset);
dummy.quaternion.copy(workingQuaternion);
dummy.scale.setScalar(1);
dummy.updateMatrix();
hydrogenMesh.setMatrixAt(i * 3 + h, dummy.matrix);
}
}
oxygenMesh.count = visibleMarkers;
hydrogenMesh.count = visibleMarkers * 3;
oxygenMesh.instanceMatrix.needsUpdate = true;
hydrogenMesh.instanceMatrix.needsUpdate = true;
}
function moveParticles(delta) {
const radialLimit = 1.72;
const radialLimitSquared = radialLimit * radialLimit;
const lowerY = -1.68;
const upperY = 1.5;
for (let i = 0; i < visibleMarkers; i++) {
const ion = ions[i];
const p = ion.position;
const v = ion.velocity;
p.addScaledVector(v, delta);
const radiusSquared = p.x * p.x + p.z * p.z;
if (radiusSquared > radialLimitSquared) {
const radius = Math.sqrt(radiusSquared);
p.x *= radialLimit / radius;
p.z *= radialLimit / radius;
const outwardVelocity =
v.x * p.x + v.z * p.z;
v.x -=
(2 * outwardVelocity * p.x) / radialLimitSquared;
v.z -=
(2 * outwardVelocity * p.z) / radialLimitSquared;
}
if (p.y < lowerY) {
p.y = lowerY;
v.y = Math.abs(v.y);
} else if (p.y > upperY) {
p.y = upperY;
v.y = -Math.abs(v.y);
}
}
}
// ---------------------------------------------------------------------
// Formatting helpers
// ---------------------------------------------------------------------
function concentrationParts(c) {
if (c >= 0.1) {
return [c.toPrecision(3), "mol L⁻¹"];
}
if (c >= 1e-3) {
return [(c * 1e3).toPrecision(3), "mmol L⁻¹"];
}
if (c >= 1e-6) {
return [(c * 1e6).toPrecision(3), "μmol L⁻¹"];
}
if (c >= 1e-9) {
return [(c * 1e9).toPrecision(3), "nmol L⁻¹"];
}
if (c >= 1e-12) {
return [(c * 1e12).toPrecision(3), "pmol L⁻¹"];
}
return [(c * 1e15).toPrecision(3), "fmol L⁻¹"];
}
function superscript(value) {
const digits = String(value);
const map = {
"-": "⁻",
"0": "⁰",
"1": "¹",
"2": "²",
"3": "³",
"4": "⁴",
"5": "⁵",
"6": "⁶",
"7": "⁷",
"8": "⁸",
"9": "⁹"
};
return [...digits].map((digit) => map[digit] ?? digit).join("");
}
function scientificNotation(value) {
if (value === 0) return "0";
const exponent = Math.floor(Math.log10(value));
const mantissa = value / Math.pow(10, exponent);
return `${mantissa.toFixed(2)} × 10${superscript(exponent)}`;
}
function chargeParts(coulombs) {
if (coulombs >= 1) {
return [coulombs.toFixed(2), "C"];
}
if (coulombs >= 1e-3) {
return [(coulombs * 1e3).toFixed(2), "mC"];
}
if (coulombs >= 1e-6) {
return [(coulombs * 1e6).toFixed(2), "μC"];
}
return [(coulombs * 1e9).toFixed(2), "nC"];
}
// ---------------------------------------------------------------------
// Graph helpers
// ---------------------------------------------------------------------
const plot = {
left: 46,
top: 18,
width: 278,
height: 126
};
function graphX(logConcentration) {
return (
plot.left +
((logConcentration + 14) / 14) * plot.width
);
}
function graphY(ph) {
return plot.top + ((14 - ph) / 14) * plot.height;
}
function updateGraph() {
const x = graphX(-currentPH);
const y = graphY(currentPH);
$("graphDot").setAttribute("cx", x);
$("graphDot").setAttribute("cy", y);
$("graphCrossH").setAttribute("y1", y);
$("graphCrossH").setAttribute("y2", y);
$("graphCrossV").setAttribute("x1", x);
$("graphCrossV").setAttribute("x2", x);
}
// ---------------------------------------------------------------------
// UI state
// ---------------------------------------------------------------------
function updateModel() {
currentPH = Number(phRange.value);
currentConcentration = Math.pow(10, -currentPH);
$("phOutput").textContent = currentPH.toFixed(2);
const [concentrationValue, concentrationUnit] =
concentrationParts(currentConcentration);
$("concentrationValue").textContent = concentrationValue;
$("concentrationUnit").textContent = concentrationUnit;
const moles =
currentConcentration * SAMPLE_VOLUME_L;
const ionCount = moles * 6.02214076e23;
$("ionCount").textContent =
scientificNotation(ionCount);
const totalCharge = moles * FARADAY;
const [chargeValue, chargeUnit] = chargeParts(totalCharge);
$("chargeValue").textContent =
`${chargeValue} ${chargeUnit}`;
// A logarithmic display is necessary because the concentration spans
// fourteen orders of magnitude. The minimum keeps the dilute/basic
// state visibly populated without implying literal particle counts.
const normalized = (-currentPH + 14) / 14;
visibleMarkers = Math.min(
MAX_MARKERS,
Math.round(18 + 882 * Math.pow(normalized, 1.6))
);
$("markerCount").textContent =
`${visibleMarkers} / ${MAX_MARKERS} markers`;
pinkLight.intensity =
0.65 + (visibleMarkers / MAX_MARKERS) * 1.05;
updateGraph();
updateParticleMatrices(simulationTime);
}
phRange.addEventListener("input", updateModel);
document.querySelectorAll("[data-ph]").forEach((button) => {
button.addEventListener("click", () => {
phRange.value = button.dataset.ph;
updateModel();
});
});
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// ---------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
goalYaw -= deltaX * 0.006;
goalPitch = THREE.MathUtils.clamp(
goalPitch + deltaY * 0.005,
0.08,
1.15
);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (
event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
goalRadius = THREE.MathUtils.clamp(
goalRadius * Math.exp(event.deltaY * 0.001),
5.4,
15
);
},
{ passive: false }
);
$("zoomIn").addEventListener("click", () => {
goalRadius = THREE.MathUtils.clamp(goalRadius - 0.65, 5.4, 15);
});
$("zoomOut").addEventListener("click", () => {
goalRadius = THREE.MathUtils.clamp(goalRadius + 0.65, 5.4, 15);
});
$("resetView").addEventListener("click", () => {
goalYaw = 0.66;
goalPitch = 0.31;
goalRadius = 9.3;
});
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
function resize() {
const width = host.clientWidth;
const height = host.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(host);
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (motionToggle.checked) {
simulationTime += delta;
moveParticles(delta);
updateParticleMatrices(simulationTime);
}
const damping = 0.09;
currentYaw += (goalYaw - currentYaw) * damping;
currentPitch += (goalPitch - currentPitch) * damping;
currentRadius += (goalRadius - currentRadius) * damping;
camera.position.set(
cameraTarget.x +
currentRadius *
Math.sin(currentYaw) *
Math.cos(currentPitch),
cameraTarget.y +
currentRadius *
Math.sin(currentPitch),
cameraTarget.z +
currentRadius *
Math.cos(currentYaw) *
Math.cos(currentPitch)
);
camera.lookAt(cameraTarget);
renderer.render(scene, camera);
}
resize();
updateModel();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```