Prompt specification
Build or customize the Three.js 3D scene "Equipotential surface model" 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: Equipotential surface model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/equipotential-surface-model-x1625
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Build selected equipotential shells around a charge proxy with a separate field-arrow inset; label the surfaces mathematical and distinguish potential value from field magnitude.
Working Three.js Starter (equipotential-surface-model-x1625):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Equipotential Shells — Three.js</title>
<style>
:root {
color-scheme: dark;
--bg: #050914;
--panel: #0a1220;
--panel-2: #0d1727;
--line: #203149;
--text: #e8f1ff;
--muted: #8ea2bc;
--cyan: #57d9ff;
--warm: #ff657a;
--violet: #8b7cff;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
height: 100vh;
display: flex;
flex-direction: column;
}
header {
min-height: 78px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 16px 22px;
border-bottom: 1px solid var(--line);
background: rgba(5, 9, 20, 0.92);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
min-width: 0;
}
.brand-mark {
width: 42px;
height: 42px;
flex: 0 0 auto;
display: grid;
place-items: center;
border: 1px solid #314764;
border-radius: 11px;
color: var(--cyan);
background:
linear-gradient(135deg, rgba(87, 217, 255, 0.12), transparent),
#0a1423;
font-size: 24px;
box-shadow: inset 0 0 18px rgba(87, 217, 255, 0.05);
}
h1 {
margin: 0;
font-size: clamp(17px, 2vw, 23px);
line-height: 1.1;
letter-spacing: -0.025em;
}
.subtitle {
margin-top: 5px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.04em;
}
.core-badge {
flex: 0 0 auto;
padding: 8px 11px;
border: 1px solid #263a55;
border-radius: 999px;
color: #a8bbd2;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
letter-spacing: 0.1em;
}
.layout {
min-height: 0;
flex: 1;
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 45%, rgba(26, 65, 91, 0.22), transparent 40%),
#060b16;
}
#sceneHost,
#labelLayer {
position: absolute;
inset: 0;
}
#sceneHost canvas {
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
}
#labelLayer {
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.stage-heading {
position: absolute;
z-index: 5;
top: 20px;
left: 22px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.stage-heading h2 {
margin: 6px 0 4px;
font-size: clamp(19px, 2.5vw, 30px);
letter-spacing: -0.035em;
}
.stage-heading p {
margin: 0;
color: var(--muted);
font-size: 12px;
}
.scale-chips {
position: absolute;
z-index: 5;
left: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 8px;
pointer-events: none;
}
.chip {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 8px 10px;
border: 1px solid rgba(67, 91, 121, 0.65);
border-radius: 8px;
background: rgba(7, 15, 27, 0.78);
color: #b5c6dc;
backdrop-filter: blur(8px);
font-size: 10px;
letter-spacing: 0.03em;
}
.chip i {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--warm);
box-shadow: 0 0 10px currentColor;
}
.chip.field i {
color: var(--cyan);
background: var(--cyan);
}
.camera-hint {
position: absolute;
z-index: 5;
right: 20px;
bottom: 22px;
color: #71849d;
font-size: 10px;
pointer-events: none;
}
.shell-label {
position: absolute;
top: 0;
left: 0;
min-width: 112px;
padding: 7px 9px;
border: 1px solid rgba(91, 119, 151, 0.48);
border-radius: 7px;
background: rgba(6, 13, 24, 0.86);
box-shadow: 0 7px 24px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(7px);
transform: translate(-50%, -50%);
transition:
border-color 160ms ease,
background 160ms ease,
opacity 160ms ease;
}
.shell-label.focused {
border-color: rgba(87, 217, 255, 0.8);
background: rgba(7, 19, 34, 0.94);
}
.label-kind {
display: block;
margin-bottom: 3px;
color: #7890ad;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 8px;
letter-spacing: 0.13em;
}
.shell-label b {
display: block;
color: #edf6ff;
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
font-weight: 600;
}
.shell-label small {
display: block;
margin-top: 2px;
color: #8fa4bd;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
aside {
min-height: 0;
overflow-y: auto;
border-left: 1px solid var(--line);
background: var(--panel);
scrollbar-color: #2c405a transparent;
}
.panel-section {
padding: 17px;
border-bottom: 1px solid var(--line);
}
.section-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.section-head h3 {
margin: 0;
font-size: 12px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.section-head span {
color: #70849d;
font-size: 9px;
letter-spacing: 0.05em;
}
#insetHost {
position: relative;
width: 100%;
height: 270px;
overflow: hidden;
border: 1px solid #243750;
border-radius: 10px;
background: #07111d;
}
#insetHost canvas {
width: 100%;
height: 100%;
display: block;
}
.inset-caption {
position: absolute;
z-index: 2;
left: 10px;
right: 10px;
pointer-events: none;
display: flex;
justify-content: space-between;
gap: 10px;
color: #8ca4bf;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.inset-caption span:last-child {
color: #b8eeff;
text-align: right;
}
.inset-note {
margin: 9px 0 0;
color: #8196b0;
font-size: 10px;
line-height: 1.5;
}
.arrow-gradient {
height: 4px;
margin-top: 10px;
border-radius: 999px;
background: linear-gradient(90deg, #fff0a0, #57d9ff);
opacity: 0.9;
}
.sample-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 9px;
}
.sample {
padding: 7px;
border: 1px solid #1d3048;
border-radius: 7px;
background: #091321;
text-align: center;
}
.sample b,
.sample small {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.sample b {
color: #d8e8fa;
font-size: 10px;
}
.sample small {
margin-top: 3px;
color: #71869f;
font-size: 8px;
}
.legend-row {
display: grid;
grid-template-columns: 13px 1fr auto;
align-items: center;
gap: 9px;
padding: 8px 0;
border-bottom: 1px solid rgba(38, 56, 80, 0.55);
}
.legend-row:last-child {
border-bottom: 0;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.legend-row b,
.legend-row small {
display: block;
}
.legend-row b {
color: #dce8f7;
font-family: Georgia, "Times New Roman", serif;
font-size: 13px;
font-weight: 600;
}
.legend-row small {
margin-top: 2px;
color: #72869f;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.legend-row .radius {
color: #8196af;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.note {
margin: 10px 0 0;
padding: 9px 10px;
border-left: 2px solid #3b5676;
background: rgba(22, 39, 61, 0.34);
color: #91a6bf;
font-size: 10px;
line-height: 1.55;
}
.control-label {
display: block;
margin: 13px 0 7px;
color: #9eb0c6;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.sign-button,
.reset-button {
min-height: 36px;
border: 1px solid #293e59;
border-radius: 7px;
background: #0b1626;
color: #91a6be;
cursor: pointer;
transition: 150ms ease;
}
.sign-button:hover,
.reset-button:hover {
border-color: #476384;
color: #e6f1ff;
}
.sign-button.active[data-sign="1"] {
border-color: rgba(255, 101, 122, 0.75);
background: rgba(255, 101, 122, 0.13);
color: #ff9bab;
}
.sign-button.active[data-sign="-1"] {
border-color: rgba(139, 124, 255, 0.75);
background: rgba(139, 124, 255, 0.13);
color: #b8b0ff;
}
.shell-list {
display: grid;
gap: 6px;
}
.shell-option {
display: grid;
grid-template-columns: 17px 11px 1fr;
align-items: center;
gap: 8px;
padding: 7px 8px;
border: 1px solid #1c2e45;
border-radius: 7px;
background: #08121f;
cursor: pointer;
}
.shell-option:hover {
border-color: #38506d;
}
.shell-option input {
width: 13px;
height: 13px;
margin: 0;
accent-color: #57d9ff;
}
.shell-option i {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 8px currentColor;
}
.shell-option b,
.shell-option small {
display: block;
}
.shell-option b {
color: #cfdeed;
font-family: Georgia, "Times New Roman", serif;
font-size: 12px;
font-weight: 600;
}
.shell-option small {
margin-top: 2px;
color: #6d829c;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 8px;
}
.range-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 10px;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
}
.range-value {
min-width: 37px;
color: #91a7bf;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
text-align: right;
}
.checkbox-line {
display: flex;
align-items: center;
gap: 8px;
margin-top: 12px;
color: #9eb1c8;
font-size: 11px;
}
.checkbox-line input {
accent-color: var(--cyan);
}
.reset-button {
width: 100%;
margin-top: 14px;
}
.readout {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.readout-card {
padding: 10px;
border: 1px solid #243852;
border-radius: 8px;
background: #081321;
}
.readout-card span {
display: block;
color: #70869f;
font-size: 8px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout-card b {
display: block;
margin-top: 5px;
color: #e2edf9;
font-family: Georgia, "Times New Roman", serif;
font-size: 16px;
font-weight: 600;
}
.equation-list {
display: grid;
gap: 8px;
}
.equation {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
padding: 8px 0;
border-bottom: 1px solid rgba(38, 56, 80, 0.55);
}
.equation:last-child {
border-bottom: 0;
}
.equation span {
color: #8398b2;
font-size: 10px;
}
.equation b {
color: #dce9f7;
font-family: Georgia, "Times New Roman", serif;
font-size: 13px;
font-weight: 600;
}
.direction-readout {
margin-top: 11px;
color: #8fa5be;
font-size: 10px;
line-height: 1.5;
}
.direction-readout strong {
color: #bfeaff;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100vh;
}
.layout {
grid-template-columns: 1fr;
}
.stage {
min-height: 620px;
}
aside {
overflow: visible;
border-left: 0;
border-top: 1px solid var(--line);
}
.core-badge {
display: none;
}
}
@media (max-width: 520px) {
header {
padding: 14px;
}
.stage {
min-height: 540px;
}
.stage-heading {
left: 15px;
top: 15px;
}
.scale-chips {
left: 15px;
bottom: 15px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">Φ</div>
<div>
<h1>Equipotential Shell Study</h1>
<div class="subtitle">Faceted surfaces encode signed potential · inset encodes field magnitude</div>
</div>
</div>
<div class="core-badge">THREE.JS CORE / R160</div>
</header>
<main class="layout">
<section class="stage" id="stage">
<div id="sceneHost"></div>
<div id="labelLayer"></div>
<div class="stage-heading">
<div class="eyebrow">Electrostatic visualization</div>
<h2>Selected equipotential shells</h2>
<p>Click a faceted shell to select it. Drag to orbit.</p>
</div>
<div class="scale-chips">
<span class="chip"><i></i>Surface color = potential Φ</span>
<span class="chip field"><i></i>Inset arrows = |E|</span>
</div>
<div class="camera-hint">DRAG TO ORBIT · WHEEL TO ZOOM</div>
</section>
<aside>
<section class="panel-section">
<div class="section-head">
<h3>Field-arrow inset</h3>
<span>SEPARATE SCALE</span>
</div>
<div id="insetHost">
<div class="inset-caption">
<span>|E| = 1/r²</span>
<span>length + brightness ∝ √|E|</span>
</div>
</div>
<p class="inset-note">
Arrow direction shows the sign of Q. Arrow length and brightness encode
field magnitude on a square-root scale for readability.
</p>
<div class="arrow-gradient"></div>
<div class="sample-grid" id="sampleGrid"></div>
</section>
<section class="panel-section">
<div class="section-head">
<h3>Potential legend</h3>
<span>SURFACE COLOR</span>
</div>
<div id="potentialLegend"></div>
<p class="note">
Color represents signed potential, not field strength. The field is
stronger closer to the proxy even though it is shown in a separate inset.
</p>
</section>
<section class="panel-section">
<div class="section-head">
<h3>Scene controls</h3>
<span>INTERACTIVE</span>
</div>
<span class="control-label">Charge proxy sign</span>
<div class="segmented">
<button class="sign-button active" data-sign="1" type="button">+Q · outward field</button>
<button class="sign-button" data-sign="-1" type="button">−Q · inward field</button>
</div>
<span class="control-label">Visible shells</span>
<div class="shell-list" id="shellList"></div>
<span class="control-label">Surface opacity</span>
<div class="range-row">
<input id="opacityRange" type="range" min="0.04" max="0.20" step="0.01" value="0.10">
<span class="range-value" id="opacityValue">0.10</span>
</div>
<label class="checkbox-line">
<input id="labelsToggle" type="checkbox" checked>
Show mathematical surface labels
</label>
<button class="reset-button" id="resetCamera" type="button">Reset camera</button>
</section>
<section class="panel-section">
<div class="section-head">
<h3>Selected shell</h3>
<span id="selectedShellName">SHELL 02</span>
</div>
<div class="readout">
<div class="readout-card">
<span>Potential</span>
<b id="readoutPhi">Φ = +0.741</b>
</div>
<div class="readout-card">
<span>Radius</span>
<b id="readoutRadius">r = 1.35</b>
</div>
</div>
</section>
<section class="panel-section">
<div class="section-head">
<h3>Mathematics</h3>
<span>NORMALIZED: kQ = 1</span>
</div>
<div class="equation-list">
<div class="equation">
<span>Signed potential</span>
<b id="potentialEquation">Φ(r) = +1/r</b>
</div>
<div class="equation">
<span>Field magnitude</span>
<b>|E(r)| = 1/r²</b>
</div>
</div>
<div class="direction-readout" id="directionReadout">
For <strong>Q > 0</strong>, E points radially outward.
</div>
</section>
</aside>
</main>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const sceneHost = document.getElementById("sceneHost");
const insetHost = document.getElementById("insetHost");
const labelLayer = document.getElementById("labelLayer");
const potentialLegend = document.getElementById("potentialLegend");
const shellList = document.getElementById("shellList");
const state = {
sign: 1,
opacity: 0.10,
showLabels: true,
focused: 1
};
const shellSpecs = [
{ radius: 0.80, phi: 1 / 0.80 },
{ radius: 1.35, phi: 1 / 1.35 },
{ radius: 2.15, phi: 1 / 2.15 },
{ radius: 3.25, phi: 1 / 3.25 }
];
const labelDirections = [
new THREE.Vector3(0.52, 0.77, 0.36).normalize(),
new THREE.Vector3(-0.48, 0.76, 0.44).normalize(),
new THREE.Vector3(0.60, 0.55, -0.58).normalize(),
new THREE.Vector3(-0.58, 0.61, -0.54).normalize()
];
const state3 = {
shells: []
};
function signedNumber(value, digits = 3) {
return `${value >= 0 ? "+" : "−"}${Math.abs(value).toFixed(digits)}`;
}
function potentialColor(phi) {
const maxAbs = 1.25;
const amount = THREE.MathUtils.clamp(Math.abs(phi) / maxAbs, 0, 1);
const remapped = THREE.MathUtils.clamp((amount - 0.24) / 0.76, 0, 1);
if (phi >= 0) {
return new THREE.Color("#f59e0b").lerp(
new THREE.Color("#ff3f61"),
remapped
);
}
return new THREE.Color("#38bdf8").lerp(
new THREE.Color("#8b5cf6"),
remapped
);
}
// ------------------------------------------------------------
// Main renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.setClearColor(0x060b16, 1);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x060b16, 0.022);
const camera = new THREE.PerspectiveCamera(44, 1, 0.1, 80);
const hemisphere = new THREE.HemisphereLight(0xa9dcff, 0x08101c, 1.35);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.2);
keyLight.position.set(5, 8, 6);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4b8cff, 1.4);
rimLight.position.set(-6, 1, -4);
scene.add(rimLight);
// Sparse, square-point background stars.
const starPositions = [];
for (let i = 0; i < 260; i++) {
const radius = 12 + Math.random() * 12;
const theta = Math.random() * Math.PI * 2;
const y = Math.random() * 2 - 1;
const planar = Math.sqrt(1 - y * y);
starPositions.push(
radius * planar * Math.cos(theta),
radius * y,
radius * planar * Math.sin(theta)
);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x6d91b5,
size: 0.035,
transparent: true,
opacity: 0.46,
depthWrite: false
})
);
scene.add(stars);
const grid = new THREE.GridHelper(14, 28, 0x284760, 0x12263a);
grid.position.y = -3.52;
grid.material.transparent = true;
grid.material.opacity = 0.18;
grid.material.depthWrite = false;
scene.add(grid);
// Low-poly charge proxy.
const chargeGroup = new THREE.Group();
scene.add(chargeGroup);
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0xff685f,
emissive: 0xff263f,
emissiveIntensity: 0.72,
roughness: 0.48,
metalness: 0.08,
flatShading: true
});
const chargeCore = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.29, 1),
coreMaterial
);
chargeGroup.add(chargeCore);
const chargeHalo = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.42, 1),
new THREE.MeshBasicMaterial({
color: 0xff5a6c,
wireframe: true,
transparent: true,
opacity: 0.27,
depthWrite: false
})
);
chargeGroup.add(chargeHalo);
function createChargeTexture(sign) {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 256, 256);
context.textAlign = "center";
context.textBaseline = "middle";
context.font = "700 88px system-ui, sans-serif";
context.shadowBlur = 18;
context.shadowColor = sign > 0 ? "#ff6b7b" : "#9b8cff";
context.fillStyle = "#ffffff";
context.fillText(sign > 0 ? "+Q" : "−Q", 128, 130);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
return texture;
}
let chargeTexture = createChargeTexture(state.sign);
const chargeSign = new THREE.Sprite(
new THREE.SpriteMaterial({
map: chargeTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
chargeSign.scale.set(0.85, 0.85, 1);
chargeSign.position.set(0, 0.02, 0.42);
chargeSign.renderOrder = 30;
chargeGroup.add(chargeSign);
// Selected equipotential shells.
shellSpecs.forEach((spec, index) => {
const group = new THREE.Group();
scene.add(group);
const geometry = new THREE.IcosahedronGeometry(spec.radius, 1);
const fill = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color: 0xff5067,
emissive: 0xff5067,
emissiveIntensity: 0.08,
transparent: true,
opacity: state.opacity,
side: THREE.DoubleSide,
depthWrite: false,
roughness: 0.72,
metalness: 0,
flatShading: true
})
);
const wire = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
color: 0xff5067,
wireframe: true,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
fill.userData.shellIndex = index;
wire.userData.shellIndex = index;
group.add(fill);
group.add(wire);
const label = document.createElement("div");
label.className = "shell-label";
label.innerHTML = `
<span class="label-kind">POTENTIAL</span>
<b class="phi-value"></b>
<small class="radius-value"></small>
`;
labelLayer.appendChild(label);
state3.shells.push({
...spec,
group,
fill,
wire,
label,
enabled: true,
anchor: labelDirections[index].clone().multiplyScalar(spec.radius)
});
});
// ------------------------------------------------------------
// Custom orbit controls
// ------------------------------------------------------------
const controls = {
theta: 0.73,
phi: 1.15,
distance: 9.2,
dragging: false,
moved: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.distance * sinPhi * Math.sin(controls.theta),
controls.distance * Math.cos(controls.phi),
controls.distance * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(0, 0, 0);
}
function resetCamera() {
controls.theta = 0.73;
controls.phi = 1.15;
controls.distance = 9.2;
updateCamera();
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pickMeshes = state3.shells.flatMap((shell) => [
shell.fill,
shell.wire
]);
function pickShell(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(pickMeshes, false)[0];
return hit ? hit.object.userData.shellIndex : null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
controls.dragging = true;
controls.moved = false;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (controls.dragging) {
const dx = event.clientX - controls.lastX;
const dy = event.clientY - controls.lastY;
if (Math.abs(dx) + Math.abs(dy) > 3) {
controls.moved = true;
}
controls.lastX = event.clientX;
controls.lastY = event.clientY;
controls.theta -= dx * 0.008;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.008,
0.18,
Math.PI - 0.18
);
updateCamera();
} else {
const hit = pickShell(event);
renderer.domElement.style.cursor = hit !== null ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!controls.moved) {
const hit = pickShell(event);
if (hit !== null) {
focusShell(hit);
}
}
controls.dragging = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
controls.dragging = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.distance = THREE.MathUtils.clamp(
controls.distance * Math.exp(event.deltaY * 0.001),
5.4,
15.5
);
updateCamera();
},
{ passive: false }
);
document.getElementById("resetCamera").addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Separate field-arrow inset
// ------------------------------------------------------------
const insetRenderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
insetRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
insetRenderer.outputColorSpace = THREE.SRGBColorSpace;
insetRenderer.setClearColor(0x07111d, 1);
insetHost.appendChild(insetRenderer.domElement);
const insetScene = new THREE.Scene();
const insetCamera = new THREE.OrthographicCamera(-2, 2, 2, -2, 0.1, 20);
insetCamera.position.set(0, 0, 5);
insetCamera.lookAt(0, 0, 0);
const insetCore = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.17, 1),
new THREE.MeshStandardMaterial({
color: 0xff685f,
emissive: 0xff263f,
emissiveIntensity: 0.7,
flatShading: true,
roughness: 0.45
})
);
insetScene.add(insetCore);
const insetSign = new THREE.Sprite(
new THREE.SpriteMaterial({
map: chargeTexture,
transparent: true,
depthTest: false
})
);
insetSign.scale.set(0.58, 0.58, 1);
insetSign.position.z = 0.2;
insetScene.add(insetSign);
const fieldSamples = [
{ radius: 0.55, count: 4, offset: Math.PI / 4 },
{ radius: 0.95, count: 6, offset: 0 },
{ radius: 1.50, count: 8, offset: Math.PI / 8 }
];
const maxField = 1 / (fieldSamples[0].radius ** 2);
fieldSamples.forEach((sample) => {
const points = [];
for (let i = 0; i < 96; i++) {
const angle = (i / 96) * Math.PI * 2;
points.push(
new THREE.Vector3(
Math.cos(angle) * sample.radius,
Math.sin(angle) * sample.radius,
-0.03
)
);
}
const ring = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: 0x35506d,
transparent: true,
opacity: 0.26,
depthWrite: false
})
);
insetScene.add(ring);
});
const arrowGroup = new THREE.Group();
insetScene.add(arrowGroup);
function disposeGroup(group) {
group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material)
? object.material
: [object.material];
materials.forEach((material) => material.dispose());
}
});
}
function buildFieldArrows() {
while (arrowGroup.children.length) {
const child = arrowGroup.children[0];
arrowGroup.remove(child);
disposeGroup(child);
}
const lowColor = new THREE.Color("#42d6ff");
const highColor = new THREE.Color("#fff0a0");
fieldSamples.forEach((sample) => {
const fieldMagnitude = 1 / (sample.radius ** 2);
const normalized = Math.sqrt(fieldMagnitude / maxField);
const length = 0.72 * normalized;
const color = lowColor.clone().lerp(highColor, normalized);
for (let i = 0; i < sample.count; i++) {
const angle =
sample.offset + (i / sample.count) * Math.PI * 2;
const origin = new THREE.Vector3(
Math.cos(angle) * sample.radius,
Math.sin(angle) * sample.radius,
0.04
);
const direction = new THREE.Vector3(
Math.cos(angle),
Math.sin(angle),
0
).multiplyScalar(state.sign);
const arrow = new THREE.ArrowHelper(
direction,
origin,
length,
color,
Math.min(0.15, length * 0.34),
Math.min(0.095, length * 0.20)
);
arrow.line.material.transparent = true;
arrow.line.material.opacity = 0.92;
arrow.line.material.depthTest = false;
arrow.cone.material.transparent = true;
arrow.cone.material.opacity = 0.96;
arrow.cone.material.depthTest = false;
arrow.line.renderOrder = 20;
arrow.cone.renderOrder = 20;
arrowGroup.add(arrow);
}
});
}
document.getElementById("sampleGrid").innerHTML = fieldSamples
.map((sample) => `
<div class="sample">
<b>r = ${sample.radius.toFixed(2)}</b>
<small>|E| = ${(1 / (sample.radius ** 2)).toFixed(2)}</small>
</div>
`)
.join("");
// ------------------------------------------------------------
// UI and appearance updates
// ------------------------------------------------------------
function updateShellAppearance() {
state3.shells.forEach((shell, index) => {
shell.group.visible = shell.enabled;
const focused = state.focused === index;
shell.fill.material.opacity = state.opacity * (focused ? 1.55 : 1);
shell.wire.material.opacity = focused ? 0.72 : 0.38;
shell.fill.material.emissiveIntensity = focused ? 0.22 : 0.08;
shell.label.classList.toggle("focused", focused);
});
}
function updateReadout() {
const shell = state3.shells[state.focused];
document.getElementById("selectedShellName").textContent =
`SHELL ${String(state.focused + 1).padStart(2, "0")}`;
document.getElementById("readoutPhi").textContent =
`Φ = ${signedNumber(state.sign * shell.phi, 3)}`;
document.getElementById("readoutRadius").textContent =
`r = ${shell.radius.toFixed(2)}`;
}
function focusShell(index) {
state.focused = index;
updateShellAppearance();
updateReadout();
}
function refreshPotentialAppearance() {
const signText = state.sign > 0 ? "+" : "−";
state3.shells.forEach((shell, index) => {
const signedPhi = state.sign * shell.phi;
const color = potentialColor(signedPhi);
shell.fill.material.color.copy(color);
shell.fill.material.emissive.copy(color);
shell.wire.material.color.copy(color);
shell.label.querySelector(".phi-value").textContent =
`Φ = ${signText}${shell.phi.toFixed(3)}`;
shell.label.querySelector(".radius-value").textContent =
`r = ${shell.radius.toFixed(2)}`;
});
potentialLegend.innerHTML = state3.shells
.map((shell) => {
const color = potentialColor(state.sign * shell.phi);
const cssColor = `#${color.getHexString()}`;
return `
<div class="legend-row">
<i class="swatch" style="background:${cssColor};color:${cssColor}"></i>
<div>
<b>Φ = ${signText}${shell.phi.toFixed(3)}</b>
<small>signed potential</small>
</div>
<span class="radius">r = ${shell.radius.toFixed(2)}</span>
</div>
`;
})
.join("");
shellList.querySelectorAll(".shell-option").forEach((option, index) => {
const shell = state3.shells[index];
const color = potentialColor(state.sign * shell.phi);
const cssColor = `#${color.getHexString()}`;
option.querySelector("i").style.background = cssColor;
option.querySelector("i").style.color = cssColor;
option.querySelector("b").textContent =
`Φ = ${signText}${shell.phi.toFixed(3)}`;
});
document.getElementById("potentialEquation").textContent =
`Φ(r) = ${signText}1/r`;
document.getElementById("directionReadout").innerHTML =
state.sign > 0
? "For <strong>Q > 0</strong>, E points radially outward."
: "For <strong>Q < 0</strong>, E points radially inward.";
const nextTexture = createChargeTexture(state.sign);
chargeSign.material.map = nextTexture;
insetSign.material.map = nextTexture;
chargeTexture.dispose();
chargeTexture = nextTexture;
coreMaterial.color.set(state.sign > 0 ? 0xff685f : 0x756cff);
coreMaterial.emissive.set(state.sign > 0 ? 0xff263f : 0x3828c7);
chargeHalo.material.color.set(state.sign > 0 ? 0xff5a6c : 0x8d7cff);
insetCore.material.color.set(state.sign > 0 ? 0xff685f : 0x756cff);
insetCore.material.emissive.set(state.sign > 0 ? 0xff263f : 0x3828c7);
buildFieldArrows();
updateReadout();
}
shellList.innerHTML = state3.shells
.map(
(shell, index) => `
<label class="shell-option">
<input type="checkbox" checked data-shell="${index}">
<i></i>
<span>
<b>Φ = ${shell.phi.toFixed(3)}</b>
<small>r = ${shell.radius.toFixed(2)}</small>
</span>
</label>
`
)
.join("");
shellList.querySelectorAll("input[data-shell]").forEach((input) => {
input.addEventListener("change", () => {
const index = Number(input.dataset.shell);
state3.shells[index].enabled = input.checked;
if (!input.checked && state.focused === index) {
const next = state3.shells.findIndex((shell) => shell.enabled);
if (next !== -1) state.focused = next;
}
updateShellAppearance();
updateReadout();
});
});
document.querySelectorAll(".sign-button").forEach((button) => {
button.addEventListener("click", () => {
state.sign = Number(button.dataset.sign);
document.querySelectorAll(".sign-button").forEach((other) => {
other.classList.toggle("active", other === button);
});
refreshPotentialAppearance();
});
});
document.getElementById("opacityRange").addEventListener("input", (event) => {
state.opacity = Number(event.target.value);
document.getElementById("opacityValue").textContent =
state.opacity.toFixed(2);
updateShellAppearance();
});
document.getElementById("labelsToggle").addEventListener("change", (event) => {
state.showLabels = event.target.checked;
});
// ------------------------------------------------------------
// Resize and projected HTML labels
// ------------------------------------------------------------
function resizeMain() {
const width = Math.max(sceneHost.clientWidth, 1);
const height = Math.max(sceneHost.clientHeight, 1);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
function resizeInset() {
const width = Math.max(insetHost.clientWidth, 1);
const height = Math.max(insetHost.clientHeight, 1);
const aspect = width / height;
const halfHeight = Math.max(1.72, 1.92 / aspect);
const halfWidth = halfHeight * aspect;
insetCamera.left = -halfWidth;
insetCamera.right = halfWidth;
insetCamera.top = halfHeight;
insetCamera.bottom = -halfHeight;
insetCamera.updateProjectionMatrix();
insetRenderer.setSize(width, height, false);
}
const projected = new THREE.Vector3();
function updateLabels() {
const width = stage.clientWidth;
const height = stage.clientHeight;
state3.shells.forEach((shell) => {
projected.copy(shell.anchor).project(camera);
const visible =
state.showLabels &&
shell.enabled &&
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
shell.label.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
shell.label.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
});
}
const resizeObserver = new ResizeObserver(() => {
resizeMain();
resizeInset();
});
resizeObserver.observe(sceneHost);
resizeObserver.observe(insetHost);
window.addEventListener("resize", () => {
resizeMain();
resizeInset();
});
resizeMain();
resizeInset();
updateCamera();
refreshPotentialAppearance();
updateShellAppearance();
buildFieldArrows();
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
const reduceMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
if (!reduceMotion) {
const pulse = 1 + Math.sin(elapsed * 2.1) * 0.045;
chargeGroup.scale.setScalar(pulse);
chargeHalo.rotation.y = elapsed * 0.22;
chargeHalo.rotation.x = elapsed * 0.13;
}
stars.rotation.y = elapsed * 0.006;
updateLabels();
renderer.render(scene, camera);
insetRenderer.render(insetScene, insetCamera);
}
animate();
</script>
</body>
</html>
```