Prompt specification
Build or customize the Three.js 3D scene "Electron-density visualization 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: Electron-density visualization explorer
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/electron-density-visualization-explorer-x1932
Scene Description Prompt:
Make a low-poly Three.js scene in a self-contained HTML file using cdnjs: electron-density visualization explorer. Allow selecting a few clearly labeled analytic orbital-density illustrations; show probability distribution and nodal regions while distinguishing visualization color from charge sign or actual glowing matter.
Working Three.js Starter (electron-density-visualization-explorer-x1932):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Electron Density Explorer</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(9, 18, 29, 0.88);
--panel-solid: #0b1722;
--line: rgba(165, 197, 218, 0.17);
--text: #e8f1f5;
--muted: #8da4b3;
--cyan: #58d4c7;
--amber: #f2c35a;
--plus: #70a9ff;
--minus: #ff806f;
--node: #98a7af;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
[hidden] {
display: none !important;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#scene:active {
cursor: grabbing;
}
.brand {
position: fixed;
z-index: 3;
top: 22px;
left: 24px;
pointer-events: none;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.55);
}
.kicker {
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.brand h1 {
margin: 5px 0 3px;
font-size: clamp(22px, 2.5vw, 34px);
font-weight: 680;
letter-spacing: -0.035em;
}
.brand p {
margin: 0;
color: #a7bbc7;
font-size: 12px;
letter-spacing: 0.015em;
}
.panel {
position: fixed;
z-index: 4;
top: 96px;
left: 24px;
width: 322px;
max-height: calc(100vh - 120px);
overflow: auto;
padding: 18px;
border: 1px solid var(--line);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 18px 70px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #304655 transparent;
}
.section + .section {
margin-top: 21px;
padding-top: 19px;
border-top: 1px solid var(--line);
}
.section-title {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: #c9d7df;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.section-title span {
color: var(--cyan);
font-variant-numeric: tabular-nums;
}
.orbital-list {
display: grid;
gap: 8px;
}
.orbital-card {
width: 100%;
padding: 11px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
text-align: left;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.orbital-card:hover {
transform: translateY(-1px);
border-color: rgba(88, 212, 199, 0.45);
background: rgba(88, 212, 199, 0.055);
}
.orbital-card[aria-pressed="true"] {
border-color: rgba(88, 212, 199, 0.8);
background:
linear-gradient(
90deg,
rgba(88, 212, 199, 0.12),
rgba(88, 212, 199, 0.035)
);
box-shadow: inset 3px 0 0 var(--cyan);
}
.orbital-top {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
}
.orbital-top strong {
font-size: 15px;
letter-spacing: 0.01em;
}
.orbital-top span {
color: var(--muted);
font-size: 10px;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.orbital-card small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10.5px;
line-height: 1.4;
}
.controls {
display: grid;
gap: 10px;
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-height: 24px;
color: #c6d5dd;
font-size: 12px;
cursor: pointer;
}
.toggle span {
display: flex;
align-items: center;
gap: 8px;
}
.toggle input {
width: 16px;
height: 16px;
margin: 0;
accent-color: var(--cyan);
cursor: pointer;
}
.dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: currentColor;
}
.dot.samples {
color: var(--amber);
}
.dot.shell {
color: var(--cyan);
}
.dot.node {
color: var(--node);
}
.segment {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 4px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(0, 0, 0, 0.16);
}
.segment button {
padding: 8px 5px;
border: 0;
border-radius: 7px;
background: transparent;
color: var(--muted);
font-size: 10px;
font-weight: 750;
cursor: pointer;
}
.segment button[aria-pressed="true"] {
background: rgba(88, 212, 199, 0.14);
color: #dffcf6;
box-shadow: inset 0 0 0 1px rgba(88, 212, 199, 0.35);
}
.legend {
margin-top: 12px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(0, 0, 0, 0.13);
}
.legend-title {
margin-bottom: 8px;
color: #d5e1e7;
font-size: 10px;
font-weight: 750;
}
.gradient {
height: 8px;
border-radius: 999px;
background: linear-gradient(90deg, #24576a, #2ba79c, #f3c75d);
}
.legend-scale {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: var(--muted);
font-size: 9px;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 9px;
color: #aebfc9;
font-size: 10px;
}
.swatch {
width: 17px;
height: 9px;
border-radius: 3px;
background: var(--node);
opacity: 0.75;
}
.sign-pairs {
display: flex;
justify-content: space-between;
gap: 8px;
}
.sign-key {
flex: 1;
padding: 7px;
border-radius: 7px;
background: rgba(255, 255, 255, 0.035);
text-align: center;
font-size: 10px;
}
.plus-color {
color: var(--plus);
}
.minus-color {
color: var(--minus);
}
.note {
margin: 11px 0 0;
padding: 10px;
border-left: 2px solid var(--amber);
border-radius: 0 7px 7px 0;
background: rgba(242, 195, 90, 0.06);
color: #b9c7ce;
font-size: 10px;
line-height: 1.55;
}
.note strong {
color: #f4dca0;
}
.readout {
position: fixed;
z-index: 3;
top: 24px;
right: 24px;
width: 340px;
padding: 17px 18px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(7, 16, 25, 0.74);
box-shadow: 0 15px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(14px);
pointer-events: none;
}
.readout .eyebrow {
color: var(--cyan);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.readout h2 {
margin: 5px 0 10px;
font-size: 22px;
font-weight: 680;
letter-spacing: -0.02em;
}
.equation {
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
color: #eef6f8;
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
}
.readout p {
margin: 9px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.readout p strong {
color: #d6e3e9;
}
.hint {
position: fixed;
z-index: 3;
right: 24px;
bottom: 20px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 16, 25, 0.75);
color: #91a8b5;
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(10px);
}
.hint b {
color: #d6e4ea;
font-weight: 700;
}
#loading {
position: fixed;
z-index: 10;
inset: 0;
display: grid;
place-items: center;
background: #071019;
color: #a9c0cc;
font-size: 12px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#loading.hidden {
display: none;
}
@media (max-width: 760px) {
.brand {
top: 14px;
left: 14px;
}
.brand p {
display: none;
}
.panel {
top: 74px;
left: 12px;
width: calc(100vw - 24px);
max-height: 44vh;
padding: 14px;
}
.readout {
display: none;
}
.hint {
right: 12px;
bottom: 12px;
left: 12px;
text-align: center;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive three-dimensional electron probability density visualization"
></canvas>
<header class="brand">
<div class="kicker">Analytic orbital lab</div>
<h1>Electron density explorer</h1>
<p>Probability is the subject. Color is only a key.</p>
</header>
<aside class="panel" aria-label="Visualization controls">
<section class="section">
<div class="section-title"><span>01</span> Choose an orbital</div>
<div class="orbital-list" id="orbitalList">
<button
class="orbital-card"
data-orbital="s"
aria-pressed="true"
>
<div class="orbital-top">
<strong>1s</strong>
<span>Spherical</span>
</div>
<small>ψ ∝ e<sup>−r/a₀</sup> · continuous density · no nodal surface</small>
</button>
<button
class="orbital-card"
data-orbital="p"
aria-pressed="false"
>
<div class="orbital-top">
<strong>2p<sub>z</sub></strong>
<span>Two lobes</span>
</div>
<small>ψ ∝ z e<sup>−r/2a₀</sup> · opposite signs · one nodal plane</small>
</button>
<button
class="orbital-card"
data-orbital="d"
aria-pressed="false"
>
<div class="orbital-top">
<strong>3d<sub>z²</sub></strong>
<span>Axial + torus</span>
</div>
<small>ψ ∝ (3z²−r²)e<sup>−r/3a₀</sup> · sign regions · two nodal cones</small>
</button>
</div>
</section>
<section class="section">
<div class="section-title"><span>02</span> Display layers</div>
<div class="controls">
<label class="toggle">
<span><i class="dot samples"></i>Probability samples</span>
<input id="showSamples" type="checkbox" checked>
</label>
<label class="toggle">
<span><i class="dot shell"></i>Iso-density shells</span>
<input id="showShells" type="checkbox" checked>
</label>
<label class="toggle">
<span><i class="dot node"></i>Nodal set + near-node band</span>
<input id="showNodes" type="checkbox" checked>
</label>
<label class="toggle">
<span><i class="dot"></i>Reference axes</span>
<input id="showAxes" type="checkbox" checked>
</label>
</div>
</section>
<section class="section">
<div class="section-title"><span>03</span> Color encoding</div>
<div class="segment" role="group" aria-label="Color encoding mode">
<button id="densityMode" aria-pressed="true">Density magnitude</button>
<button id="signMode" aria-pressed="false">Wavefunction sign</button>
</div>
<div class="legend">
<div class="legend-title" id="legendTitle">
Color = relative |ψ|² within the selected orbital
</div>
<div id="densityLegend">
<div class="gradient"></div>
<div class="legend-scale">
<span>Low probability density</span>
<span>High</span>
</div>
</div>
<div id="signLegend" class="sign-pairs" hidden>
<div class="sign-key plus-color">ψ > 0</div>
<div class="sign-key minus-color">ψ < 0</div>
</div>
<div class="legend-row">
<span class="swatch"></span>
<span>Neutral gray: nodal set, where ψ = 0</span>
</div>
<div class="legend-row">
<span class="plus-color">ψ +</span>
<span class="minus-color">ψ −</span>
<span>Sign labels remain independent of color</span>
</div>
</div>
<p class="note">
<strong>Not glowing matter.</strong>
The dots are probability samples and the shells are iso-density contours.
Color is a visualization convention—not physical light, an electron’s
literal color, or a claim that charge creates visible glow.
</p>
</section>
</aside>
<section class="readout" aria-live="polite">
<div class="eyebrow">Selected illustration</div>
<h2 id="readoutName">1s orbital</h2>
<div class="equation" id="readoutFormula">ψ₁ₛ(r) ∝ e<sup>−r/a₀</sup></div>
<p>
<strong>Probability distribution:</strong>
<span id="readoutDensity">|ψ|² = e<sup>−2r/a₀</sup></span>
</p>
<p>
<strong>Nodal structure:</strong>
<span id="readoutNode">
No nodal surface; the probability density is nonzero away from the nucleus.
</span>
</p>
<p>
These are scaled hydrogen-like analytic forms. Radial parameters are adjusted
for legibility, but the angular symmetry and zero sets are preserved.
</p>
</section>
<div class="hint">
<b>Drag</b> orbit · <b>Shift/right-drag</b> pan · <b>Wheel/pinch</b> zoom ·
<b>Double-click/R</b> reset
</div>
<div id="loading">Building orbital fields…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const canvas = $("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setClearColor(0x071019, 1);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.FogExp2(0x071019, 0.035);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.05,
100
);
camera.up.set(0, 1, 0);
const cameraState = {
theta: -0.72,
phi: 1.08,
radius: 8.7,
target: new THREE.Vector3(0, 0, 0)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.cos(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.sin(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xb9dded, 0x13202b, 1.7));
const keyLight = new THREE.DirectionalLight(0xffedcf, 2.0);
keyLight.position.set(4, 6, 5);
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x69a9ff, 1.1);
fillLight.position.set(-5, 1, -3);
scene.add(fillLight);
const floorGrid = new THREE.GridHelper(
12,
24,
0x294250,
0x142733
);
floorGrid.position.y = -3.25;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.42;
scene.add(floorGrid);
const nucleus = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.16, 1),
new THREE.MeshStandardMaterial({
color: 0xf2f0e9,
roughness: 0.7,
metalness: 0.05
})
);
scene.add(nucleus);
function makeLabel(text, color = "#dce8ef", width = 1.2, boxed = true) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 320;
labelCanvas.height = 112;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 320, 112);
if (boxed) {
context.fillStyle = "rgba(5, 13, 21, 0.78)";
context.fillRect(10, 20, 300, 72);
context.strokeStyle = color;
context.globalAlpha = 0.52;
context.lineWidth = 2;
context.strokeRect(10, 20, 300, 72);
context.globalAlpha = 1;
}
context.fillStyle = color;
context.font = "600 48px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 160, 57);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 112 / 320, 1);
sprite.renderOrder = 10;
return sprite;
}
const nucleusLabel = makeLabel("nucleus", "#dce8ef", 1.25);
nucleusLabel.position.set(0, -0.42, 0);
scene.add(nucleusLabel);
// Reference axes use the same local frame as the orbital illustrations.
const referenceRoot = new THREE.Group();
referenceRoot.rotation.x = -Math.PI / 2;
scene.add(referenceRoot);
function axisLine(from, to, color) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(...from),
new THREE.Vector3(...to)
]);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.56
});
return new THREE.Line(geometry, material);
}
referenceRoot.add(axisLine([-3.6, 0, 0], [3.6, 0, 0], 0x3ba9b0));
referenceRoot.add(axisLine([0, -3.6, 0], [0, 3.6, 0], 0x8d78c7));
referenceRoot.add(axisLine([0, 0, -3.6], [0, 0, 3.6], 0xd7ac55));
const xLabel = makeLabel("x", "#54c4c8", 0.48, false);
const yLabel = makeLabel("y", "#b59be0", 0.48, false);
const zLabel = makeLabel("z", "#e8bd67", 0.48, false);
xLabel.position.set(3.9, 0, 0);
yLabel.position.set(0, 3.9, 0);
zLabel.position.set(0, 0, 3.9);
referenceRoot.add(xLabel, yLabel, zLabel);
// The orbital illustrations are generated in conventional x,y,z coordinates.
// Rotating the root makes the orbital z-axis vertical in the scene.
const orbitalRoot = new THREE.Group();
orbitalRoot.rotation.x = -Math.PI / 2;
scene.add(orbitalRoot);
const shellGroup = new THREE.Group();
const sampleGroup = new THREE.Group();
const nodeGroup = new THREE.Group();
const labelGroup = new THREE.Group();
orbitalRoot.add(shellGroup, sampleGroup, nodeGroup, labelGroup);
const orbitals = {
s: {
key: "s",
name: "1s orbital",
shortName: "1s",
l: 0,
beta: 1,
scale: 1,
thresholds: [0.025, 0.09, 0.28],
angular: () => 1,
maxAngular: 1,
maxDensity: 1,
formula: "ψ₁ₛ(r) ∝ e<sup>−r/a₀</sup>",
densityText: "|ψ|² = e<sup>−2r/a₀</sup>",
nodeText:
"No nodal surface; the probability density is nonzero away from the nucleus.",
nodeType: "none",
sampleCount: 2200
},
p: {
key: "p",
name: "2p_z orbital",
shortName: "2p_z",
l: 1,
beta: 0.5,
scale: 0.65,
thresholds: [0.045, 0.14],
angular: (direction) => direction.z,
maxAngular: 1,
maxDensity: 4 * Math.exp(-2),
formula: "ψ₂p_z ∝ z e<sup>−r/2a₀</sup>",
densityText: "|ψ|² = z² e<sup>−r/a₀</sup>",
nodeText:
"One nodal plane at z = 0. The upper and lower lobes have opposite ψ signs.",
nodeType: "plane",
sampleCount: 1900
},
d: {
key: "d",
name: "3d_z² orbital",
shortName: "3d_z²",
l: 2,
beta: 1 / 3,
scale: 0.42,
thresholds: [8, 24],
angular: (direction) => 3 * direction.z * direction.z - 1,
maxAngular: 2,
maxDensity: 4 * Math.pow(6, 4) * Math.exp(-4),
formula: "ψ₃d_z² ∝ (3z²−r²)e<sup>−r/3a₀</sup>",
densityText: "|ψ|² = (3z²−r²)²e<sup>−2r/3a₀</sup>",
nodeText:
"Two nodal cones satisfy 3z² = r². Axial and equatorial regions have opposite ψ signs.",
nodeType: "cones",
sampleCount: 1800
}
};
const densityColors = {
low: new THREE.Color(0x24576a),
middle: new THREE.Color(0x2ba79c),
high: new THREE.Color(0xf3c75d),
plus: new THREE.Color(0x70a9ff),
minus: new THREE.Color(0xff806f)
};
const shellMeshes = [];
let cloudPoints = null;
let cloudSigns = null;
let cloudDensities = null;
let currentOrbital = orbitals.s;
let colorMode = "density";
function shellDensityColor(index, count) {
if (count === 1) {
return densityColors.middle.clone();
}
const t = index / (count - 1);
if (t < 0.5) {
return densityColors.low.clone().lerp(densityColors.middle, t * 2);
}
return densityColors.middle.clone().lerp(
densityColors.high,
(t - 0.5) * 2
);
}
function densityColor(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
const color = new THREE.Color();
if (t < 0.5) {
color.lerpColors(densityColors.low, densityColors.middle, t * 2);
} else {
color.lerpColors(
densityColors.middle,
densityColors.high,
(t - 0.5) * 2
);
}
return color;
}
function disposeObject(object) {
object.traverse((child) => {
if (child.geometry) {
child.geometry.dispose();
}
if (child.material) {
const materials = Array.isArray(child.material)
? child.material
: [child.material];
for (const material of materials) {
if (material.map) {
material.map.dispose();
}
material.dispose();
}
}
});
}
function clearGroup(group) {
while (group.children.length) {
const child = group.children.pop();
disposeObject(child);
}
}
function waveValue(orbital, position) {
const radius = position.length();
const z = position.z;
if (orbital.key === "s") {
return Math.exp(-radius);
}
if (orbital.key === "p") {
return z * Math.exp(-radius / 2);
}
return (
(3 * z * z - radius * radius) *
Math.exp(-radius / 3)
);
}
function densityValue(orbital, position) {
const value = waveValue(orbital, position);
return value * value;
}
function outerIsoRadius(orbital, angularSquared, threshold) {
const { l, beta } = orbital;
if (l === 0) {
return (
Math.log(angularSquared / threshold) /
(2 * beta)
);
}
const radialFactor = (radius) =>
Math.pow(radius, 2 * l) *
angularSquared *
Math.exp(-2 * beta * radius);
const maximumRadius = l / beta;
const maximumValue = radialFactor(maximumRadius);
if (maximumValue < threshold) {
return null;
}
let low = maximumRadius;
let high = maximumRadius + 0.25;
while (high < 16 && radialFactor(high) > threshold) {
high += 0.25;
}
for (let iteration = 0; iteration < 42; iteration++) {
const middle = (low + high) * 0.5;
if (radialFactor(middle) > threshold) {
low = middle;
} else {
high = middle;
}
}
return (low + high) * 0.5;
}
function buildShell(orbital, threshold, shellIndex, shellCount) {
const latitudeSteps = 14;
const longitudeSteps = 24;
const grid = [];
const positions = {
positive: [],
negative: []
};
for (let latitude = 0; latitude <= latitudeSteps; latitude++) {
const row = [];
const theta = Math.PI * latitude / latitudeSteps;
for (let longitude = 0; longitude <= longitudeSteps; longitude++) {
const phi = 2 * Math.PI * longitude / longitudeSteps;
const sinTheta = Math.sin(theta);
const direction = new THREE.Vector3(
sinTheta * Math.cos(phi),
sinTheta * Math.sin(phi),
Math.cos(theta)
);
const angularValue = orbital.angular(direction);
const angularSquared = angularValue * angularValue;
if (angularSquared <= 1e-8) {
row.push(null);
continue;
}
const radius = outerIsoRadius(
orbital,
angularSquared,
threshold
);
if (radius === null || radius > 16) {
row.push(null);
continue;
}
row.push({
position: direction.multiplyScalar(radius),
sign: Math.sign(angularValue)
});
}
grid.push(row);
}
function addTriangle(array, a, b, c) {
array.push(
a.position.x, a.position.y, a.position.z,
b.position.x, b.position.y, b.position.z,
c.position.x, c.position.y, c.position.z
);
}
for (let latitude = 0; latitude < latitudeSteps; latitude++) {
for (let longitude = 0; longitude < longitudeSteps; longitude++) {
const a = grid[latitude][longitude];
const b = grid[latitude][longitude + 1];
const c = grid[latitude + 1][longitude + 1];
const d = grid[latitude + 1][longitude];
if (!a || !b || !c || !d) {
continue;
}
if (
a.sign === b.sign &&
b.sign === c.sign &&
c.sign === d.sign
) {
const array = a.sign > 0
? positions.positive
: positions.negative;
addTriangle(array, a, b, d);
addTriangle(array, b, c, d);
}
}
}
const densityColor = shellDensityColor(shellIndex, shellCount);
const baseOpacity = shellCount === 3
? [0.15, 0.22, 0.31][shellIndex]
: [0.17, 0.28][shellIndex];
for (const [signName, array] of Object.entries(positions)) {
if (!array.length) {
continue;
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(array, 3)
);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color: densityColor,
transparent: true,
opacity: baseOpacity,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true,
roughness: 0.86,
metalness: 0.02
});
const mesh = new THREE.Mesh(geometry, material);
mesh.userData = {
sign: signName === "positive" ? 1 : -1,
densityColor: densityColor.clone(),
baseOpacity
};
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 12),
new THREE.LineBasicMaterial({
color: densityColor.clone().lerp(new THREE.Color(0xffffff), 0.2),
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
edges.userData = {
sign: mesh.userData.sign,
densityColor: mesh.userData.densityColor.clone()
};
mesh.add(edges);
shellGroup.add(mesh);
shellMeshes.push(mesh);
}
}
// Deterministic pseudo-random generator keeps the illustrative clouds stable.
function createRandom(seed) {
let state = seed >>> 0;
return function random() {
state += 0x6d2b79f5;
let value = state;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
function sampleGamma(shape, rate, random) {
const d = shape - 1 / 3;
const c = 1 / Math.sqrt(9 * d);
let value;
do {
const u1 = Math.max(random(), 1e-12);
const u2 = random();
const magnitude = Math.sqrt(-2 * Math.log(u1));
value = magnitude * Math.cos(2 * Math.PI * u2);
} while (value <= 0);
const u3 = Math.max(random(), 1e-12);
return rate * d * value * Math.pow(u3, 1 / shape);
}
function randomDirection(orbital, random) {
let direction;
let angularValue;
do {
const z = random() * 2 - 1;
const phi = random() * Math.PI * 2;
const radius = Math.sqrt(1 - z * z);
direction = new THREE.Vector3(
radius * Math.cos(phi),
radius * Math.sin(phi),
z
);
angularValue = orbital.angular(direction);
} while (
random() >
(angularValue * angularValue) /
(orbital.maxAngular * orbital.maxAngular)
);
return direction;
}
function buildCloud(orbital) {
const random = createRandom(
orbital.key === "s" ? 1931 :
orbital.key === "p" ? 3927 : 7813
);
const count = orbital.sampleCount;
const positions = new Float32Array(count * 3);
const colors = new Float32Array(count * 3);
const signs = new Float32Array(count);
const densities = new Float32Array(count);
const radialShape = 2 * orbital.l + 1;
const radialRate = 2 * orbital.beta;
for (let index = 0; index < count; index++) {
const radius = sampleGamma(
radialShape,
radialRate,
random
);
const direction = randomDirection(orbital, random);
const position = direction.multiplyScalar(radius);
positions[index * 3] = position.x;
positions[index * 3 + 1] = position.y;
positions[index * 3 + 2] = position.z;
const wave = waveValue(orbital, position);
const density = wave * wave;
signs[index] = Math.sign(wave);
densities[index] = density / orbital.maxDensity;
const color = densityColor(densities[index]);
color.toArray(colors, index * 3);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.BufferAttribute(colors, 3)
);
const material = new THREE.PointsMaterial({
size: 0.065,
vertexColors: true,
transparent: true,
opacity: 0.72,
depthWrite: false,
sizeAttenuation: true
});
// Deliberately not additive: probability samples do not glow.
material.blending = THREE.NormalBlending;
cloudPoints = new THREE.Points(geometry, material);
cloudSigns = signs;
cloudDensities = densities;
sampleGroup.add(cloudPoints);
}
function buildPlaneNode(orbital) {
const size = 6.8;
const plane = new THREE.Mesh(
new THREE.PlaneGeometry(size, size),
new THREE.MeshBasicMaterial({
color: 0x98a7af,
transparent: true,
opacity: 0.09,
side: THREE.DoubleSide,
depthWrite: false
})
);
nodeGroup.add(plane);
const grid = new THREE.GridHelper(size, 12, 0x8c9da7, 0x61717b);
grid.rotation.x = Math.PI / 2;
grid.material.transparent = true;
grid.material.opacity = 0.25;
nodeGroup.add(grid);
const random = createRandom(444);
const count = 650;
const positions = new Float32Array(count * 3);
for (let index = 0; index < count; index++) {
positions[index * 3] = (random() - 0.5) * 6.5;
positions[index * 3 + 1] = (random() - 0.5) * 6.5;
positions[index * 3 + 2] = (random() - 0.5) * 0.09;
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
nodeGroup.add(
new THREE.Points(
geometry,
new THREE.PointsMaterial({
color: 0x9aa8b0,
size: 0.055,
transparent: true,
opacity: 0.5,
depthWrite: false
})
)
);
const label = makeLabel("ψ = 0 · nodal plane", "#aab6bd", 2.25);
label.position.set(2.25, -2.25, 0);
labelGroup.add(label);
}
function coneGeometry(height, radius, segments = 20) {
const positions = [];
for (let index = 0; index < segments; index++) {
const angle1 = 2 * Math.PI * index / segments;
const angle2 = 2 * Math.PI * (index + 1) / segments;
const base1 = [
radius * Math.cos(angle1),
radius * Math.sin(angle1),
height
];
const base2 = [
radius * Math.cos(angle2),
radius * Math.sin(angle2),
height
];
positions.push(
0, 0, 0,
base1[0], base1[1], base1[2],
base2[0], base2[1], base2[2]
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
function buildConeNodes(orbital) {
const height = 6.8;
const radius = Math.sqrt(2) * height;
for (const direction of [1, -1]) {
const geometry = coneGeometry(
direction * height,
radius
);
const surface = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
color: 0x98a7af,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
})
);
const wire = new THREE.Mesh(
geometry.clone(),
new THREE.MeshBasicMaterial({
color: 0x98a7af,
wireframe: true,
transparent: true,
opacity: 0.23,
depthWrite: false
})
);
nodeGroup.add(surface, wire);
}
const random = createRandom(777);
const count = 900;
const positions = new Float32Array(count * 3);
for (let index = 0; index < count; index++) {
const sign = random() > 0.5 ? 1 : -1;
const z = sign * (0.35 + random() * 6.3);
const coneRadius = Math.sqrt(2) * Math.abs(z);
const angle = random() * Math.PI * 2;
const jitter = 0.10;
positions[index * 3] =
coneRadius * Math.cos(angle) + (random() - 0.5) * jitter;
positions[index * 3 + 1] =
coneRadius * Math.sin(angle) + (random() - 0.5) * jitter;
positions[index * 3 + 2] = z + (random() - 0.5) * jitter;
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
nodeGroup.add(
new THREE.Points(
geometry,
new THREE.PointsMaterial({
color: 0x9aa8b0,
size: 0.055,
transparent: true,
opacity: 0.52,
depthWrite: false
})
)
);
const label = makeLabel("ψ = 0 · nodal cones", "#aab6bd", 2.45);
label.position.set(-1.2, 2.8, 2.6);
labelGroup.add(label);
}
function addSignLabels(orbital) {
if (orbital.key === "p") {
const positive = makeLabel("ψ +", "#70a9ff", 1.4);
const negative = makeLabel("ψ −", "#ff806f", 1.4);
positive.position.set(0, 0, 3.4);
negative.position.set(0, 0, -3.4);
labelGroup.add(positive, negative);
}
if (orbital.key === "d") {
const positive = makeLabel("ψ +", "#70a9ff", 1.65);
const negative = makeLabel("ψ −", "#ff806f", 1.65);
positive.position.set(0, 0, 6.1);
negative.position.set(4.6, 0, 0);
labelGroup.add(positive, negative);
}
}
function updateCloudColors() {
if (!cloudPoints) {
return;
}
const colors = cloudPoints.geometry.getAttribute("color");
for (let index = 0; index < cloudDensities.length; index++) {
const color = colorMode === "sign"
? cloudSigns[index] > 0
? densityColors.plus
: densityColors.minus
: densityColor(cloudDensities[index]);
color.toArray(colors.array, index * 3);
}
colors.needsUpdate = true;
}
function updateColorMode() {
for (const mesh of shellMeshes) {
const sign = mesh.userData.sign;
if (colorMode === "sign") {
mesh.material.color.set(
sign > 0 ? densityColors.plus : densityColors.minus
);
mesh.material.opacity = 0.28;
} else {
mesh.material.color.copy(mesh.userData.densityColor);
mesh.material.opacity = mesh.userData.baseOpacity;
}
const edges = mesh.children.find(
(child) => child.isLineSegments
);
if (edges) {
edges.material.color.set(
colorMode === "sign"
? sign > 0
? densityColors.plus
: densityColors.minus
: mesh.userData.densityColor
);
edges.material.opacity =
colorMode === "sign" ? 0.36 : 0.2;
}
}
updateCloudColors();
$("#densityMode").setAttribute(
"aria-pressed",
String(colorMode === "density")
);
$("#signMode").setAttribute(
"aria-pressed",
String(colorMode === "sign")
);
$("#densityLegend").hidden = colorMode !== "density";
$("#signLegend").hidden = colorMode !== "sign";
$("#legendTitle").textContent = colorMode === "density"
? "Color = relative |ψ|² within the selected orbital"
: "Color = algebraic sign of ψ, not density magnitude";
}
function updateVisibility() {
sampleGroup.visible = $("#showSamples").checked;
shellGroup.visible = $("#showShells").checked;
nodeGroup.visible = $("#showNodes").checked;
referenceRoot.visible = $("#showAxes").checked;
}
function selectOrbital(key) {
currentOrbital = orbitals[key];
clearGroup(shellGroup);
clearGroup(sampleGroup);
clearGroup(nodeGroup);
clearGroup(labelGroup);
shellMeshes.length = 0;
cloudPoints = null;
cloudSigns = null;
cloudDensities = null;
orbitalRoot.scale.setScalar(currentOrbital.scale);
currentOrbital.thresholds.forEach((threshold, index) => {
buildShell(
currentOrbital,
threshold,
index,
currentOrbital.thresholds.length
);
});
buildCloud(currentOrbital);
if (currentOrbital.nodeType === "plane") {
buildPlaneNode(currentOrbital);
} else if (currentOrbital.nodeType === "cones") {
buildConeNodes(currentOrbital);
}
addSignLabels(currentOrbital);
for (const button of document.querySelectorAll(".orbital-card")) {
button.setAttribute(
"aria-pressed",
String(button.dataset.orbital === key)
);
}
$("#readoutName").textContent = currentOrbital.name;
$("#readoutFormula").innerHTML = currentOrbital.formula;
$("#readoutDensity").innerHTML = currentOrbital.densityText;
$("#readoutNode").textContent = currentOrbital.nodeText;
updateColorMode();
updateVisibility();
}
document.querySelectorAll(".orbital-card").forEach((button) => {
button.addEventListener("click", () => {
selectOrbital(button.dataset.orbital);
});
});
[
"#showSamples",
"#showShells",
"#showNodes",
"#showAxes"
].forEach((selector) => {
$(selector).addEventListener("change", updateVisibility);
});
$("#densityMode").addEventListener("click", () => {
colorMode = "density";
updateColorMode();
});
$("#signMode").addEventListener("click", () => {
colorMode = "sign";
updateColorMode();
});
// Direct camera controls: no OrbitControls or example-module imports.
const pointers = new Map();
let interaction = null;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) {
return 0;
}
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
function resetCamera() {
cameraState.theta = -0.72;
cameraState.phi = 1.08;
cameraState.radius = 8.7;
cameraState.target.set(0, 0, 0);
updateCamera();
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
interaction = {
type:
event.shiftKey || event.button === 2
? "pan"
: "orbit",
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
interaction = {
type: "pinch",
distance: pointerDistance()
};
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId) || !interaction) {
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && interaction.type !== "pinch") {
const deltaX = event.clientX - interaction.x;
const deltaY = event.clientY - interaction.y;
interaction.x = event.clientX;
interaction.y = event.clientY;
if (interaction.type === "orbit") {
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.006,
0.16,
Math.PI - 0.16
);
} else {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 1);
const panScale = cameraState.radius * 0.00125;
cameraState.target
.addScaledVector(right, -deltaX * panScale)
.addScaledVector(up, deltaY * panScale);
}
updateCamera();
} else if (pointers.size === 2 && interaction.type === "pinch") {
const distance = pointerDistance();
if (distance > 0 && interaction.distance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius *
interaction.distance / distance,
4.1,
18
);
updateCamera();
}
interaction.distance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
interaction = {
type: "orbit",
x: remaining.x,
y: remaining.y
};
} else if (pointers.size === 0) {
interaction = null;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
4.1,
18
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (
document.activeElement &&
document.activeElement.tagName === "INPUT"
) {
return;
}
const key = event.key.toLowerCase();
if (key === "r") {
resetCamera();
} else if (key === "arrowleft") {
cameraState.theta -= 0.08;
} else if (key === "arrowright") {
cameraState.theta += 0.08;
} else if (key === "arrowup") {
cameraState.phi = Math.max(
0.16,
cameraState.phi - 0.06
);
} else if (key === "arrowdown") {
cameraState.phi = Math.min(
Math.PI - 0.16,
cameraState.phi + 0.06
);
} else if (key === "+" || key === "=") {
cameraState.radius = Math.max(4.1, cameraState.radius * 0.9);
} else if (key === "-") {
cameraState.radius = Math.min(18, cameraState.radius * 1.1);
} else {
return;
}
event.preventDefault();
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
selectOrbital("s");
updateCamera();
renderer.render(scene, camera);
$("#loading").classList.add("hidden");
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
}
render();
</script>
</body>
</html>
```