Prompt specification
Build or customize the Three.js 3D scene "pH scale and ion-count schematic" 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: pH scale and ion-count schematic
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/ph-scale-and-ion-count-schematic-x1921
Scene Description Prompt:
Create a standalone cutaway scene in one HTML file using Three.js from cdnjs. Build a logarithmic pH scale beside clearly symbolic hydronium concentration markers; explain the tenfold scale steps and avoid representing pH as a linear amount of acidity.
Working Three.js Starter (ph-scale-and-ion-count-schematic-x1921):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>pH — The Logarithmic Cutaway</title>
<style>
:root {
--bg: #07131d;
--panel: rgba(9, 26, 38, 0.82);
--panel-border: rgba(141, 222, 255, 0.18);
--text: #eaf8ff;
--muted: #91adbd;
--cyan: #68e5f2;
--amber: #ffca78;
--blue: #74a9ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 30% 28%, #12374a 0%, transparent 37%),
radial-gradient(circle at 78% 65%, #10263d 0%, transparent 42%),
linear-gradient(145deg, #07131d, #040a11);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--panel-border);
background: var(--panel);
box-shadow:
0 22px 65px rgba(0, 0, 0, 0.3),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px) saturate(125%);
-webkit-backdrop-filter: blur(18px) saturate(125%);
}
header {
top: 22px;
left: 24px;
width: min(440px, calc(100vw - 48px));
padding: 20px 22px 18px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 1px;
content: "";
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(27px, 3vw, 42px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1.02;
}
header p {
max-width: 390px;
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
#concept {
top: 22px;
right: 24px;
width: min(350px, calc(100vw - 48px));
padding: 20px;
border-radius: 18px;
}
.section-kicker {
margin-bottom: 8px;
color: var(--amber);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.formula {
margin: 0 0 13px;
color: #ffffff;
font-family: Georgia, "Times New Roman", serif;
font-size: 23px;
line-height: 1.25;
}
#concept p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.rule {
display: flex;
gap: 12px;
align-items: flex-start;
margin-top: 15px;
padding: 12px;
border: 1px solid rgba(104, 229, 242, 0.15);
border-radius: 12px;
background: rgba(55, 172, 195, 0.07);
color: #cdebf2;
font-size: 12px;
line-height: 1.48;
}
.rule-icon {
flex: 0 0 auto;
display: grid;
width: 28px;
height: 28px;
place-items: center;
border-radius: 50%;
color: #06141e;
background: var(--cyan);
font-weight: 900;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 15px;
margin-top: 15px;
color: #a9c2cf;
font-size: 11px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend i {
display: inline-block;
width: 9px;
height: 9px;
border-radius: 50%;
}
.legend .marker {
width: 12px;
height: 12px;
background: var(--blue);
box-shadow: 0 0 8px rgba(116, 169, 255, 0.75);
}
.legend .equal-step {
width: 16px;
height: 2px;
border-radius: 0;
background: var(--cyan);
}
#controls {
left: 50%;
bottom: 22px;
width: min(650px, calc(100vw - 36px));
padding: 15px 17px;
border-radius: 17px;
transform: translateX(-50%);
}
.control-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 10px;
}
.sample-label {
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout {
display: flex;
align-items: baseline;
gap: 10px;
white-space: nowrap;
}
#phValue {
color: var(--cyan);
font-size: 29px;
font-weight: 800;
letter-spacing: -0.04em;
}
#classification {
color: #d3e7ef;
font-size: 12px;
}
.range-wrap {
display: grid;
grid-template-columns: 38px 1fr 38px;
gap: 11px;
align-items: center;
}
.range-end {
color: #7894a4;
font-size: 10px;
font-weight: 750;
text-align: center;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 99px;
outline: none;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(90deg, #f45d68, #ffc76b, #62d8e6, #657dff);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #07131d;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: #ffffff;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.65), 0 0 16px rgba(255, 255, 255, 0.38);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #07131d;
border-radius: 50%;
background: #ffffff;
box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.65), 0 0 16px rgba(255, 255, 255, 0.38);
}
.control-bottom {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
margin-top: 11px;
}
#concentration {
color: #dceef5;
font-size: 12px;
}
#concentration strong {
color: #ffffff;
font-size: 14px;
}
.control-actions {
display: flex;
align-items: center;
gap: 9px;
}
button {
border: 1px solid rgba(143, 215, 239, 0.2);
border-radius: 9px;
padding: 7px 10px;
color: #cde5ee;
background: rgba(118, 188, 212, 0.08);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.05em;
cursor: pointer;
}
button:hover {
border-color: rgba(104, 229, 242, 0.52);
color: #ffffff;
background: rgba(104, 229, 242, 0.13);
}
#interactionHint {
color: #708b9a;
font-size: 10px;
}
@media (max-width: 900px) {
#concept {
top: auto;
right: 14px;
bottom: 145px;
width: min(340px, calc(100vw - 28px));
padding: 15px;
}
#concept p {
font-size: 12px;
}
.rule {
margin-top: 10px;
padding: 9px;
}
header {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
}
}
@media (max-width: 560px) {
header {
padding: 15px 17px;
}
header p {
display: none;
}
#concept {
bottom: 154px;
}
#concept .legend,
#concept .rule {
display: none;
}
#controls {
bottom: 12px;
padding: 13px;
}
.control-bottom {
align-items: flex-start;
flex-direction: column;
gap: 7px;
}
#interactionHint {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive three-dimensional cutaway showing hydronium concentration on a logarithmic pH scale"
></canvas>
<header class="panel">
<div class="eyebrow">Interactive chemistry / logarithmic scale</div>
<h1>pH is a ratio scale,<br />not an acidity amount.</h1>
<p>
A cutaway vessel sits beside an equal-step logarithmic ruler.
Move the sample to see why one pH step represents a tenfold change
in hydronium concentration.
</p>
</header>
<aside id="concept" class="panel">
<div class="section-kicker">The tenfold rule</div>
<div class="formula">pH = −log<sub>10</sub>([H<sub>3</sub>O<sup>+</sup>] / 1 M)</div>
<p>
Moving <strong>down one whole-number step</strong> means the hydronium
concentration becomes <strong>10× larger</strong>. Moving up one step
makes it <strong>10× smaller</strong>.
</p>
<div class="rule">
<span class="rule-icon">×10</span>
<span>
Equal vertical distances represent equal <em>ratios</em> on this
logarithmic scale. They do not represent equal amounts of acid.
</span>
</div>
<p>
At approximately 25 °C, pH 7 is neutral. The values shown here use the
common dilute-solution approximation.
</p>
<div class="legend">
<span><i class="marker"></i> Equal-size symbolic marker</span>
<span><i class="equal-step"></i> One step = tenfold ratio</span>
</div>
</aside>
<section id="controls" class="panel">
<div class="control-top">
<div class="sample-label">Cutaway sample</div>
<div class="readout">
<span id="phValue">pH 7</span>
<span id="classification">neutral</span>
</div>
</div>
<div class="range-wrap">
<span class="range-end">0<br />acidic</span>
<input
id="phSlider"
type="range"
min="0"
max="14"
step="1"
value="7"
aria-label="Sample pH"
/>
<span class="range-end">14<br />basic</span>
</div>
<div class="control-bottom">
<div id="concentration" aria-live="polite">
[H<sub>3</sub>O<sup>+</sup>] = <strong>10<sup>−7</sup> M</strong>
</div>
<div class="control-actions">
<span id="interactionHint">Drag to orbit · wheel to zoom</span>
<button id="resetView" type="button">RESET VIEW</button>
</div>
</div>
</section>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x07131d, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const controls = {
target: new THREE.Vector3(0.45, 1.12, 0),
yaw: -0.055,
pitch: 0.18,
zoom: 1,
dragging: false,
previousX: 0,
previousY: 0
};
function updateCamera() {
const narrowFactor = Math.max(1, 1.35 / camera.aspect);
const distance = 10.4 * controls.zoom * narrowFactor;
const horizontal = Math.cos(controls.pitch) * distance;
camera.position.set(
controls.target.x + Math.sin(controls.yaw) * horizontal,
controls.target.y + Math.sin(controls.pitch) * distance,
controls.target.z + Math.cos(controls.yaw) * horizontal
);
camera.lookAt(controls.target);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting and ground
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb8ecff, 0x132331, 1.65));
const keyLight = new THREE.DirectionalLight(0xe6f8ff, 3.2);
keyLight.position.set(-4, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x3b9fff, 30, 14, 2);
rimLight.position.set(4, 3, -3);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb267, 16, 12, 2);
warmLight.position.set(-4, 1.5, 3);
scene.add(warmLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(36, 36),
new THREE.MeshStandardMaterial({
color: 0x07131c,
roughness: 0.88,
metalness: 0.08,
transparent: true,
opacity: 0.76
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.58;
ground.receiveShadow = true;
scene.add(ground);
const groundGrid = new THREE.GridHelper(24, 40, 0x1e5267, 0x102d3a);
groundGrid.position.y = -1.57;
groundGrid.material.transparent = true;
groundGrid.material.opacity = 0.24;
scene.add(groundGrid);
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
const yAxis = new THREE.Vector3(0, 1, 0);
function cylinderBetween(start, end, radius, material, radialSegments = 10) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makeSectorShape(radius, startAngle, lengthAngle, segments = 96) {
const shape = new THREE.Shape();
const first = startAngle - Math.PI / 2;
shape.moveTo(0, 0);
for (let i = 0; i <= segments; i++) {
const angle = first + (lengthAngle * i) / segments;
shape.lineTo(
radius * Math.cos(angle),
radius * Math.sin(angle)
);
}
shape.closePath();
return shape;
}
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.lineTo(x + width - r, y);
context.quadraticCurveTo(x + width, y, x + width, y + r);
context.lineTo(x + width, y + height - r);
context.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
context.lineTo(x + r, y + height);
context.quadraticCurveTo(x, y + height, x, y + height - r);
context.lineTo(x, y + r);
context.quadraticCurveTo(x, y, x + r, y);
context.closePath();
}
function makeTextSprite(
text,
worldWidth,
worldHeight,
{
color = "#d9eef7",
background = null,
border = null,
weight = 700,
fontFamily = "Arial, sans-serif",
renderOrder = 10
} = {}
) {
const canvasWidth = 768;
const canvasHeight = Math.max(
96,
Math.min(256, Math.round(canvasWidth * worldHeight / worldWidth))
);
const textCanvas = document.createElement("canvas");
textCanvas.width = canvasWidth;
textCanvas.height = canvasHeight;
const context = textCanvas.getContext("2d");
context.clearRect(0, 0, canvasWidth, canvasHeight);
if (background) {
roundedRect(
context,
4,
4,
canvasWidth - 8,
canvasHeight - 8,
canvasHeight * 0.18
);
context.fillStyle = background;
context.fill();
if (border) {
context.strokeStyle = border;
context.lineWidth = 3;
context.stroke();
}
}
const fontSize = Math.round(canvasHeight * 0.55);
context.font = `${weight} ${fontSize}px ${fontFamily}`;
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(
text,
canvasWidth / 2,
canvasHeight / 2 + canvasHeight * 0.025,
canvasWidth - 30
);
const texture = new THREE.CanvasTexture(textCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldHeight, 1);
sprite.renderOrder = renderOrder;
return sprite;
}
const superscript = {
"-": "⁻",
"0": "⁰",
"1": "¹",
"2": "²",
"3": "³",
"4": "⁴",
"5": "⁵",
"6": "⁶",
"7": "⁷",
"8": "⁸",
"9": "⁹"
};
function powerOfTen(exponent) {
return "10" + String(exponent).split("").map((digit) => superscript[digit]).join("");
}
// -------------------------------------------------------------------------
// Cutaway vessel
// -------------------------------------------------------------------------
const vessel = new THREE.Group();
vessel.position.x = -1.35;
scene.add(vessel);
const thetaStart = 0.38;
const thetaLength = Math.PI * 2 - 0.76;
const vesselBottom = -1.25;
const vesselTop = 1.95;
const vesselHeight = vesselTop - vesselBottom;
const glassMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb8e8f7,
roughness: 0.08,
metalness: 0,
transparent: true,
opacity: 0.18,
side: THREE.DoubleSide,
depthWrite: false
});
const glassEdgeMaterial = new THREE.MeshBasicMaterial({
color: 0x8fd6e8,
transparent: true,
opacity: 0.7
});
const glassShell = new THREE.Mesh(
new THREE.CylinderGeometry(
1.45,
1.34,
vesselHeight,
96,
1,
true,
thetaStart,
thetaLength
),
glassMaterial
);
glassShell.position.y = (vesselBottom + vesselTop) / 2;
glassShell.renderOrder = 3;
vessel.add(glassShell);
function makeRim(radius, y) {
const points = [];
for (let i = 0; i <= 96; i++) {
const theta = thetaStart + (thetaLength * i) / 96;
points.push(
new THREE.Vector3(
radius * Math.sin(theta),
y,
radius * Math.cos(theta)
)
);
}
const curve = new THREE.CatmullRomCurve3(points, false, "centripetal");
const rim = new THREE.Mesh(
new THREE.TubeGeometry(curve, 96, 0.026, 8, false),
glassEdgeMaterial
);
rim.renderOrder = 4;
return rim;
}
vessel.add(makeRim(1.45, vesselTop));
vessel.add(makeRim(1.34, vesselBottom));
for (const theta of [thetaStart, thetaStart + thetaLength]) {
vessel.add(
cylinderBetween(
new THREE.Vector3(
1.39 * Math.sin(theta),
vesselBottom,
1.39 * Math.cos(theta)
),
new THREE.Vector3(
1.45 * Math.sin(theta),
vesselTop,
1.45 * Math.cos(theta)
),
0.022,
glassEdgeMaterial,
8
)
);
}
const glassBase = new THREE.Mesh(
new THREE.CircleGeometry(1.34, 96),
new THREE.MeshPhysicalMaterial({
color: 0x9bdcf0,
roughness: 0.15,
transparent: true,
opacity: 0.2,
side: THREE.DoubleSide,
depthWrite: false
})
);
glassBase.rotation.x = -Math.PI / 2;
glassBase.position.y = vesselBottom;
glassBase.renderOrder = 3;
vessel.add(glassBase);
const liquidShape = makeSectorShape(1.30, thetaStart, thetaLength);
const waterMaterial = new THREE.MeshPhysicalMaterial({
color: 0x39c8d8,
emissive: 0x0c4852,
emissiveIntensity: 0.34,
roughness: 0.18,
metalness: 0.02,
transparent: true,
opacity: 0.52,
side: THREE.DoubleSide,
depthWrite: false
});
const waterGeometry = new THREE.ExtrudeGeometry(liquidShape, {
depth: 1.78,
bevelEnabled: true,
bevelSegments: 2,
steps: 1,
bevelSize: 0.025,
bevelThickness: 0.018,
curveSegments: 96
});
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.rotation.x = -Math.PI / 2;
water.position.y = -1.08;
water.renderOrder = 2;
water.receiveShadow = true;
vessel.add(water);
const surfaceMaterial = new THREE.MeshBasicMaterial({
color: 0x77edf2,
transparent: true,
opacity: 0.42,
side: THREE.DoubleSide,
depthWrite: false
});
const waterSurface = new THREE.Mesh(
new THREE.ShapeGeometry(liquidShape, 96),
surfaceMaterial
);
waterSurface.rotation.x = -Math.PI / 2;
waterSurface.position.y = 0.70;
waterSurface.renderOrder = 4;
vessel.add(waterSurface);
// Emphasize the two radial cut faces of the liquid.
const cutLineMaterial = new THREE.LineBasicMaterial({
color: 0x8ef4f6,
transparent: true,
opacity: 0.62
});
for (const theta of [thetaStart, thetaStart + thetaLength]) {
const points = [
new THREE.Vector3(0, 0.70, 0),
new THREE.Vector3(
1.30 * Math.sin(theta),
0.70,
1.30 * Math.cos(theta)
),
new THREE.Vector3(
1.30 * Math.sin(theta),
-1.08,
1.30 * Math.cos(theta)
),
new THREE.Vector3(0, -1.08, 0)
];
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
cutLineMaterial
);
line.renderOrder = 5;
vessel.add(line);
}
const sampleOrb = new THREE.Mesh(
new THREE.SphereGeometry(0.16, 28, 18),
new THREE.MeshStandardMaterial({
color: 0x68e5f2,
emissive: 0x19727b,
emissiveIntensity: 0.7,
roughness: 0.22
})
);
sampleOrb.position.set(0, 0.15, 0.26);
sampleOrb.castShadow = true;
vessel.add(sampleOrb);
const vesselTitle = makeTextSprite(
"CUTAWAY VESSEL",
1.45,
0.22,
{
color: "#dff8ff",
background: "rgba(6,22,33,0.82)",
border: "rgba(120,218,239,0.28)"
}
);
vesselTitle.position.set(0, 2.40, 0.2);
vessel.add(vesselTitle);
const vesselNote = makeTextSprite(
"FIXED-SIZE SYMBOLS · COUNT IS NOT TO SCALE",
1.90,
0.16,
{
color: "#91afbd",
background: "rgba(5,18,27,0.72)"
}
);
vesselNote.position.set(0, -1.46, 0.45);
vessel.add(vesselNote);
// Illustrative hydronium glyphs. Their number and size never encode amount.
const hydroniumPositions = [
[-0.48, 0.35, 0.05],
[0.34, 0.44, 0.18],
[-0.32, -0.12, 0.42],
[0.45, -0.18, 0.04],
[-0.12, 0.53, -0.43],
[-0.67, -0.39, -0.22],
[0.18, -0.53, -0.32]
];
for (const [x, y, z] of hydroniumPositions) {
const ion = makeTextSprite(
"H₃O⁺",
0.48,
0.20,
{
color: "#eef7ff",
background: "rgba(54,112,207,0.92)",
border: "rgba(142,195,255,0.65)",
renderOrder: 8
}
);
ion.position.set(x, y, z);
vessel.add(ion);
}
// -------------------------------------------------------------------------
// Logarithmic pH ruler
// -------------------------------------------------------------------------
const ruler = new THREE.Group();
scene.add(ruler);
const rulerTop = 4.15;
const rulerBottom = -1.30;
const rulerX = 2.18;
const railMaterial = new THREE.MeshBasicMaterial({
color: 0x77b9cc,
transparent: true,
opacity: 0.72
});
ruler.add(
cylinderBetween(
new THREE.Vector3(rulerX, rulerBottom, 0),
new THREE.Vector3(rulerX, rulerTop, 0),
0.018,
railMaterial,
10
)
);
const tickMaterial = new THREE.MeshBasicMaterial({
color: 0x8ebdca,
transparent: true,
opacity: 0.65
});
for (let pH = 0; pH <= 14; pH++) {
const y = rulerTop - (pH / 14) * (rulerTop - rulerBottom);
const major = pH % 2 === 0;
const tickLength = major ? 0.22 : 0.12;
ruler.add(
cylinderBetween(
new THREE.Vector3(rulerX - tickLength, y, 0),
new THREE.Vector3(rulerX + tickLength, y, 0),
major ? 0.014 : 0.008,
tickMaterial,
8
)
);
const number = makeTextSprite(
String(pH),
major ? 0.27 : 0.22,
major ? 0.18 : 0.15,
{
color: major ? "#dff6ff" : "#8eacba",
background: major ? "rgba(7,22,32,0.72)" : null
}
);
number.position.set(1.84, y, 0);
ruler.add(number);
// Every marker is deliberately identical in size.
const marker = makeTextSprite(
"H₃O⁺",
0.46,
0.19,
{
color: "#f0f7ff",
background: "rgba(52,105,198,0.94)",
border: "rgba(142,195,255,0.72)",
renderOrder: 9
}
);
marker.position.set(2.72, y, 0);
ruler.add(marker);
const concentration = makeTextSprite(
`${powerOfTen(-pH)} M`,
0.92,
0.17,
{
color: pH === 7 ? "#78edf2" : "#b8d0dc",
background: pH === 7 ? "rgba(9,50,59,0.55)" : null
}
);
concentration.position.set(3.39, y, 0);
ruler.add(concentration);
}
const rulerHeaderPH = makeTextSprite(
"pH",
0.36,
0.23,
{
color: "#ffffff",
background: "rgba(8,28,40,0.88)",
border: "rgba(116,215,235,0.3)"
}
);
rulerHeaderPH.position.set(1.86, 4.56, 0);
ruler.add(rulerHeaderPH);
const rulerHeaderIon = makeTextSprite(
"SYMBOL",
0.67,
0.20,
{
color: "#c7eaff",
background: "rgba(8,28,40,0.82)"
}
);
rulerHeaderIon.position.set(2.72, 4.56, 0);
ruler.add(rulerHeaderIon);
const rulerHeaderValue = makeTextSprite(
"[H₃O⁺]",
0.86,
0.20,
{
color: "#c7eaff",
background: "rgba(8,28,40,0.82)"
}
);
rulerHeaderValue.position.set(3.42, 4.56, 0);
ruler.add(rulerHeaderValue);
const tenfoldNote = makeTextSprite(
"ONE WHOLE-NUMBER STEP = ×10 IN H₃O⁺",
2.80,
0.22,
{
color: "#ffe2a8",
background: "rgba(34,27,17,0.82)",
border: "rgba(255,202,120,0.24)"
}
);
tenfoldNote.position.set(2.74, -1.64, 0);
ruler.add(tenfoldNote);
// Downward direction arrow.
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0xffca78,
transparent: true,
opacity: 0.75
});
ruler.add(
cylinderBetween(
new THREE.Vector3(4.10, 4.12, 0),
new THREE.Vector3(4.10, -1.13, 0),
0.014,
arrowMaterial,
8
)
);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.095, 0.28, 16),
arrowMaterial
);
arrowHead.position.set(4.10, -1.20, 0);
arrowHead.rotation.z = Math.PI;
ruler.add(arrowHead);
const moreAcidLabel = makeTextSprite(
"MORE H₃O⁺ ↓",
0.92,
0.19,
{
color: "#ffe2a8",
background: "rgba(25,23,18,0.76)"
}
);
moreAcidLabel.position.set(4.12, 4.45, 0);
ruler.add(moreAcidLabel);
// Active pH indicator on the logarithmic rail.
const selectionMaterial = new THREE.MeshBasicMaterial({
color: 0x7ff7ef,
transparent: true,
opacity: 0.92
});
const selectionLine = new THREE.Mesh(
new THREE.CylinderGeometry(0.012, 0.012, 0.47, 8),
selectionMaterial
);
selectionLine.rotation.z = Math.PI / 2;
selectionLine.position.set(2.45, rulerTop, 0);
selectionLine.renderOrder = 12;
ruler.add(selectionLine);
const selectionDot = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 18, 12),
selectionMaterial
);
selectionDot.renderOrder = 12;
ruler.add(selectionDot);
// -------------------------------------------------------------------------
// Interactive sample and pH readout
// -------------------------------------------------------------------------
const phSlider = document.querySelector("#phSlider");
const phValue = document.querySelector("#phValue");
const classification = document.querySelector("#classification");
const concentration = document.querySelector("#concentration");
const colorRed = new THREE.Color(0xf04f62);
const colorAmber = new THREE.Color(0xffb85c);
const colorCyan = new THREE.Color(0x38cbd7);
const colorViolet = new THREE.Color(0x657dff);
const workingColor = new THREE.Color();
function yForPH(pH) {
return rulerTop - (pH / 14) * (rulerTop - rulerBottom);
}
function updateSample() {
const pH = Number(phSlider.value);
const y = yForPH(pH);
selectionDot.position.set(rulerX, y, 0);
selectionLine.position.set(2.45, y, 0);
if (pH <= 3.5) {
workingColor.copy(colorRed).lerp(colorAmber, pH / 3.5);
} else if (pH <= 7) {
workingColor.copy(colorAmber).lerp(colorCyan, (pH - 3.5) / 3.5);
} else {
workingColor.copy(colorCyan).lerp(colorViolet, (pH - 7) / 7);
}
waterMaterial.color.copy(workingColor);
waterMaterial.emissive.copy(workingColor).multiplyScalar(0.22);
surfaceMaterial.color.copy(workingColor).lerp(new THREE.Color(0xffffff), 0.3);
sampleOrb.material.color.copy(workingColor);
sampleOrb.material.emissive.copy(workingColor).multiplyScalar(0.3);
let category;
if (pH < 3) category = "strongly acidic";
else if (pH < 7) category = "acidic";
else if (pH === 7) category = "neutral";
else if (pH < 10) category = "mildly basic";
else category = "strongly basic";
phValue.textContent = `pH ${pH}`;
classification.textContent = category;
concentration.innerHTML =
`[H<sub>3</sub>O<sup>+</sup>] = <strong>10<sup>${-pH}</sup> M</strong>`;
phSlider.setAttribute(
"aria-valuetext",
`pH ${pH}, ${category}, hydronium concentration 10 to the power of ${-pH} molar`
);
}
phSlider.addEventListener("input", updateSample);
updateSample();
// -------------------------------------------------------------------------
// Camera controls implemented directly, without addons
// -------------------------------------------------------------------------
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
controls.dragging = true;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const deltaX = event.clientX - controls.previousX;
const deltaY = event.clientY - controls.previousY;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
controls.yaw -= deltaX * 0.006;
controls.pitch = THREE.MathUtils.clamp(
controls.pitch + deltaY * 0.004,
-0.18,
0.56
);
updateCamera();
});
function endDrag(event) {
controls.dragging = false;
canvas.style.cursor = "grab";
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("pointerleave", (event) => {
if (controls.dragging && event.buttons === 0) endDrag(event);
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom + event.deltaY * 0.0008,
0.68,
1.48
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") controls.yaw += 0.08;
else if (key === "arrowright") controls.yaw -= 0.08;
else if (key === "arrowup") controls.pitch += 0.05;
else if (key === "arrowdown") controls.pitch -= 0.05;
else if (key === "+" || key === "=") controls.zoom -= 0.08;
else if (key === "-") controls.zoom += 0.08;
else return;
controls.pitch = THREE.MathUtils.clamp(controls.pitch, -0.18, 0.56);
controls.zoom = THREE.MathUtils.clamp(controls.zoom, 0.68, 1.48);
updateCamera();
event.preventDefault();
});
canvas.style.cursor = "grab";
document.querySelector("#resetView").addEventListener("click", () => {
controls.yaw = -0.055;
controls.pitch = 0.18;
controls.zoom = 1;
updateCamera();
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
function animate(time) {
const pulse = 1 + Math.sin(time * 0.0022) * 0.045;
sampleOrb.scale.setScalar(pulse);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```