Prompt specification
Build or customize the Three.js 3D scene "Electrostatics museum suspended-ball exhibit" 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: Electrostatics museum suspended-ball exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/electrostatics-museum-suspended-ball-exhibit-x1611
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Compose a stylized pair of suspended charged balls beside an ideal point-charge inset; distinguish the conceptual force demonstration from actual conductor charge distributions.
Working Three.js Starter (electrostatics-museum-suspended-ball-exhibit-x1611):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Electrostatics — Suspended Charge Study</title>
<style>
:root {
color-scheme: dark;
--bg: #050a10;
--panel: #09121c;
--panel-soft: rgba(13, 25, 36, 0.86);
--line: rgba(158, 194, 211, 0.16);
--text: #eaf4f7;
--muted: #8da5b1;
--positive: #ff745c;
--negative: #52dfff;
--force: #f5d36c;
--accent: #b8f46d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
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 {
font: inherit;
}
.app {
display: flex;
flex-direction: column;
height: 100dvh;
min-height: 640px;
background: var(--bg);
}
.topbar {
position: relative;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
min-height: 72px;
padding: 14px 22px;
border-bottom: 1px solid var(--line);
background: rgba(5, 10, 16, 0.94);
}
.brand {
display: flex;
align-items: center;
gap: 14px;
}
.brand-mark {
display: grid;
grid-template-columns: repeat(3, 7px);
grid-template-rows: repeat(3, 7px);
gap: 3px;
transform: rotate(-4deg);
}
.brand-mark i {
display: block;
background: #314754;
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(4),
.brand-mark i:nth-child(6),
.brand-mark i:nth-child(8) {
background: var(--accent);
}
.brand-mark i:nth-child(5) {
background: var(--positive);
}
.eyebrow,
.micro-label,
.chip,
.panel-kicker {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.eyebrow {
color: var(--accent);
font-size: 10px;
font-weight: 700;
}
.brand h1 {
margin: 3px 0 0;
font-size: clamp(16px, 2vw, 22px);
font-weight: 620;
letter-spacing: -0.025em;
}
.top-meta {
display: flex;
align-items: center;
gap: 8px;
}
.chip {
padding: 7px 9px;
border: 1px solid var(--line);
border-radius: 5px;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
.chip.live {
color: var(--accent);
border-color: rgba(184, 244, 109, 0.28);
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 360px;
flex: 1;
min-height: 0;
}
.main-panel {
position: relative;
min-width: 0;
overflow: hidden;
isolation: isolate;
background:
radial-gradient(circle at 54% 45%, rgba(30, 69, 82, 0.23), transparent 39%),
linear-gradient(145deg, #071018 0%, #050a10 70%);
}
.main-panel::before {
position: absolute;
z-index: 0;
inset: 0;
pointer-events: none;
content: "";
opacity: 0.23;
background-image:
linear-gradient(rgba(139, 177, 191, 0.08) 1px, transparent 1px),
linear-gradient(90deg, rgba(139, 177, 191, 0.08) 1px, transparent 1px);
background-size: 48px 48px;
mask-image: radial-gradient(circle at center, black, transparent 78%);
}
#mainViewport {
position: absolute;
z-index: 1;
inset: 0;
}
#mainViewport canvas,
#insetViewport canvas {
display: block;
width: 100%;
height: 100%;
}
#mainViewport canvas {
touch-action: none;
cursor: grab;
}
#mainViewport canvas.dragging {
cursor: grabbing;
}
.glass-card {
border: 1px solid var(--line);
background:
linear-gradient(135deg, rgba(15, 29, 40, 0.91), rgba(7, 15, 23, 0.82));
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
}
.concept-card {
position: absolute;
z-index: 4;
top: 22px;
left: 22px;
width: 282px;
padding: 17px;
border-radius: 9px;
pointer-events: none;
}
.card-index {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 12px;
color: var(--accent);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.card-index::before {
width: 20px;
height: 2px;
content: "";
background: var(--accent);
}
.concept-card h2 {
margin: 0;
font-size: 25px;
font-weight: 590;
line-height: 1.04;
letter-spacing: -0.04em;
}
.concept-card p {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 15px;
padding: 11px 12px;
border: 1px solid rgba(245, 211, 108, 0.2);
border-radius: 6px;
background: rgba(245, 211, 108, 0.055);
}
.equation strong {
color: var(--force);
font: 600 14px/1.25 Georgia, serif;
}
.equation span {
color: var(--muted);
font: 8px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-align: right;
text-transform: uppercase;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 14px;
color: #b6c7cf;
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 1px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.legend .positive {
color: var(--positive);
}
.legend .negative {
color: var(--negative);
}
.legend .force {
color: var(--force);
}
.surface-card {
position: absolute;
z-index: 4;
bottom: 22px;
left: 22px;
width: 352px;
padding: 16px;
border-radius: 9px;
pointer-events: none;
}
.surface-card h3 {
margin: 0 0 7px;
font-size: 13px;
font-weight: 650;
}
.surface-card p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.surface-card p strong {
color: #d5e4e9;
font-weight: 600;
}
.mode-control {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 4px;
margin-top: 13px;
padding: 3px;
border: 1px solid var(--line);
border-radius: 6px;
background: rgba(3, 8, 12, 0.65);
pointer-events: auto;
}
.mode-control button,
.sign-toggle button,
.reset-button,
.force-toggle {
border: 0;
cursor: pointer;
color: var(--muted);
transition:
color 160ms ease,
background 160ms ease,
box-shadow 160ms ease;
}
.mode-control button {
padding: 7px 8px;
border-radius: 4px;
background: transparent;
font-size: 10px;
}
.mode-control button.active {
color: #071018;
background: var(--accent);
box-shadow: 0 0 18px rgba(184, 244, 109, 0.15);
font-weight: 700;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
}
.mode-status {
color: #718b98;
font: 8px/1.35 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.force-toggle {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 5px 0;
background: transparent;
font-size: 10px;
white-space: nowrap;
}
.force-toggle i {
position: relative;
width: 25px;
height: 14px;
border: 1px solid #3a505d;
border-radius: 20px;
background: #101c25;
transition: background 160ms ease;
}
.force-toggle i::after {
position: absolute;
top: 2px;
left: 2px;
width: 8px;
height: 8px;
border-radius: 50%;
content: "";
background: #607581;
transition:
transform 160ms ease,
background 160ms ease;
}
.force-toggle[aria-pressed="true"] {
color: var(--force);
}
.force-toggle[aria-pressed="true"] i {
border-color: rgba(245, 211, 108, 0.5);
background: rgba(245, 211, 108, 0.13);
}
.force-toggle[aria-pressed="true"] i::after {
transform: translateX(11px);
background: var(--force);
}
.world-label {
position: absolute;
z-index: 3;
display: flex;
flex-direction: column;
align-items: center;
pointer-events: none;
transform: translate(-50%, -50%);
text-shadow: 0 2px 10px #000;
}
.world-label b {
font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.world-label small {
margin-top: 4px;
color: #8ea4af;
font: 7px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#positiveTag b {
color: var(--positive);
}
#negativeTag b {
color: var(--negative);
}
#forceTag b {
color: var(--force);
font-size: 14px;
}
#centerTag {
flex-direction: row;
gap: 5px;
opacity: 0.74;
}
#centerTag small {
margin: 0;
font-size: 6px;
}
.interaction-hint {
position: absolute;
z-index: 4;
right: 20px;
bottom: 20px;
display: flex;
align-items: center;
gap: 12px;
color: #68818e;
font: 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.reset-button {
padding: 6px 8px;
border: 1px solid var(--line);
border-radius: 4px;
background: rgba(10, 20, 28, 0.72);
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.reset-button:hover {
color: var(--text);
border-color: rgba(184, 244, 109, 0.4);
}
.inset-panel {
position: relative;
z-index: 5;
min-width: 0;
padding: 20px;
overflow-y: auto;
border-left: 1px solid var(--line);
background:
radial-gradient(circle at 50% 17%, rgba(50, 96, 108, 0.13), transparent 37%),
var(--panel);
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.panel-kicker {
color: var(--accent);
font-size: 9px;
font-weight: 700;
}
.panel-head h2 {
margin: 6px 0 0;
font-size: 21px;
font-weight: 590;
letter-spacing: -0.035em;
}
.not-badge {
padding: 5px 6px;
border: 1px solid rgba(255, 116, 92, 0.27);
border-radius: 4px;
color: var(--positive);
font: 7px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.09em;
text-align: center;
text-transform: uppercase;
white-space: nowrap;
}
#insetViewport {
position: relative;
height: 276px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 9px;
background: #07131d;
box-shadow:
inset 0 0 60px rgba(0, 0, 0, 0.32),
0 14px 35px rgba(0, 0, 0, 0.18);
}
.inset-overlay {
position: absolute;
z-index: 2;
pointer-events: none;
}
.inset-overlay.top-left {
top: 12px;
left: 12px;
}
.inset-overlay.top-right {
top: 12px;
right: 12px;
color: #617c89;
font: 7px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.1em;
text-align: right;
text-transform: uppercase;
}
.q-badge {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 8px;
border: 1px solid rgba(184, 244, 109, 0.22);
border-radius: 4px;
color: var(--accent);
background: rgba(184, 244, 109, 0.05);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.q-badge i {
width: 6px;
height: 6px;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.inset-caption {
position: absolute;
z-index: 2;
bottom: 11px;
left: 50%;
width: 90%;
color: #708996;
font: 7px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-align: center;
text-transform: uppercase;
transform: translateX(-50%);
pointer-events: none;
}
.sign-toggle {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
margin-top: 10px;
}
.sign-toggle button {
padding: 8px;
border: 1px solid var(--line);
border-radius: 5px;
background: #0b1721;
font: 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.sign-toggle button.active[data-sign="1"] {
color: var(--positive);
border-color: rgba(255, 116, 92, 0.45);
background: rgba(255, 116, 92, 0.08);
}
.sign-toggle button.active[data-sign="-1"] {
color: var(--negative);
border-color: rgba(82, 223, 255, 0.45);
background: rgba(82, 223, 255, 0.08);
}
.inset-formula {
margin-top: 12px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 7px;
background: rgba(15, 29, 40, 0.65);
text-align: center;
}
.inset-formula strong {
display: block;
color: var(--accent);
font: 500 19px/1.2 Georgia, serif;
}
.inset-formula span {
display: block;
margin-top: 6px;
color: #718b98;
font: 8px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.distinction {
margin-top: 12px;
padding: 14px;
border-left: 2px solid var(--positive);
border-radius: 0 7px 7px 0;
background: rgba(255, 116, 92, 0.055);
}
.distinction h3 {
margin: 0 0 7px;
color: #ffd0c8;
font-size: 11px;
letter-spacing: 0.02em;
}
.distinction p {
margin: 0;
color: #9aadb5;
font-size: 10px;
line-height: 1.55;
}
.mini-compare {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 12px;
}
.mini-compare div {
padding: 10px;
border: 1px solid var(--line);
border-radius: 6px;
background: rgba(9, 18, 27, 0.66);
}
.mini-compare b {
display: block;
margin-bottom: 5px;
color: #d7e7ec;
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.09em;
}
.mini-compare span {
color: #6f8793;
font-size: 9px;
line-height: 1.4;
}
@media (max-width: 980px) {
.layout {
grid-template-columns: minmax(0, 1fr) 320px;
}
.concept-card {
width: 250px;
}
.surface-card {
width: 320px;
}
.interaction-hint span {
display: none;
}
}
@media (max-width: 760px) {
body {
overflow: auto;
}
.app {
height: auto;
min-height: 100dvh;
}
.topbar {
padding: 12px 14px;
}
.top-meta .chip:first-child {
display: none;
}
.layout {
display: block;
}
.main-panel {
height: 720px;
}
.concept-card {
top: 12px;
left: 12px;
width: 230px;
padding: 13px;
}
.concept-card h2 {
font-size: 21px;
}
.surface-card {
bottom: 12px;
left: 12px;
width: min(320px, calc(100% - 24px));
}
.inset-panel {
min-height: 620px;
border-top: 1px solid var(--line);
border-left: 0;
}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
<i></i><i></i><i></i>
<i></i><i></i><i></i>
</div>
<div>
<div class="eyebrow">Electrostatic study / 04</div>
<h1>Suspended charge & the ideal point</h1>
</div>
</div>
<div class="top-meta">
<div class="chip">Three.js · core</div>
<div class="chip">Voxel laboratory</div>
<div class="chip live">Interactive model</div>
</div>
</header>
<div class="layout">
<main class="main-panel">
<div id="mainViewport"></div>
<section class="concept-card glass-card">
<div class="card-index">01 / Conceptual force demonstration</div>
<h2>Suspended,<br>attracted bodies.</h2>
<p>
The gold arrows show the center-to-center force predicted by
treating each charged body as an ideal point.
</p>
<div class="equation">
<strong>F⃗ = k q₁q₂ / r² r̂</strong>
<span>Equal magnitude<br>Opposite direction</span>
</div>
<div class="legend" aria-label="Charge legend">
<span class="positive"><i></i> positive charge</span>
<span class="negative"><i></i> negative charge</span>
<span class="force"><i></i> force</span>
</div>
</section>
<div id="positiveTag" class="world-label">
<b>+q</b>
<small>Surface-localized</small>
</div>
<div id="negativeTag" class="world-label">
<b>−q</b>
<small>Surface-localized</small>
</div>
<div id="centerTag" class="world-label">
<small>Model center</small>
</div>
<div id="forceTag" class="world-label">
<b>F</b>
<small>Attraction</small>
</div>
<section class="surface-card glass-card">
<div class="card-index">02 / Conductor distribution</div>
<h3>Real charge stays on the surface.</h3>
<p>
For a conductor, neighboring charge makes the surface density
<strong>nonuniform</strong>. Bright voxels indicate greater |σ|;
this is a <strong>qualitative map, not a field solution</strong>.
</p>
<div class="mode-control" role="group" aria-label="Ball visualization mode">
<button type="button" data-mode="surface" class="active">
Surface map
</button>
<button type="button" data-mode="uniform">
Point model
</button>
</div>
<div class="control-row">
<div id="modeStatus" class="mode-status" aria-live="polite">
Tints: qualitative surface-charge density
</div>
<button
id="forceToggle"
class="force-toggle"
type="button"
aria-pressed="true"
>
<i aria-hidden="true"></i>
Force vectors
</button>
</div>
</section>
<div class="interaction-hint">
<span>Drag to orbit · wheel to zoom</span>
<button id="resetView" class="reset-button" type="button">
Reset view
</button>
</div>
</main>
<aside class="inset-panel">
<div class="panel-head">
<div>
<div class="panel-kicker">Reference / inset</div>
<h2>Ideal point charge</h2>
</div>
<div class="not-badge">Not a<br>conductor map</div>
</div>
<div id="insetViewport">
<div class="inset-overlay top-left">
<div id="qBadge" class="q-badge"><i></i><span>+q</span></div>
</div>
<div class="inset-overlay top-right">
Mathematical<br>idealization
</div>
<div class="inset-caption">
Radial field visualization · particles show field direction
</div>
</div>
<div class="sign-toggle" role="group" aria-label="Point charge sign">
<button type="button" data-sign="1" class="active">+q / outward</button>
<button type="button" data-sign="-1">−q / inward</button>
</div>
<div class="inset-formula">
<strong id="insetFormula">E(r) = kq / r² r̂</strong>
<span id="fieldDirection">Electric field points outward</span>
</div>
<div class="distinction">
<h3>What the inset means</h3>
<p>
The single cube represents all charge concentrated at one
mathematical point. A real conductor instead carries charge on
its outer surface, where it redistributes in response to nearby
charges.
</p>
</div>
<div class="mini-compare">
<div>
<b>MAIN VIEW</b>
<span>Voxel shell with surface-localized, nonuniform charge.</span>
</div>
<div>
<b>INSET</b>
<span>One ideal point with a radial Coulomb field.</span>
</div>
</div>
</aside>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const mainViewport = document.querySelector("#mainViewport");
const insetViewport = document.querySelector("#insetViewport");
const positiveColor = 0xff745c;
const negativeColor = 0x52dfff;
const forceColor = 0xf5d36c;
const accentColor = 0xb8f46d;
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
/* ---------------------------------------------------------------
Main voxel scene
--------------------------------------------------------------- */
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
mainViewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x071019, 13, 28);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 60);
const cameraTarget = new THREE.Vector3(0, 2.55, 0);
const orbit = {
yaw: 0.19,
pitch: 1.19,
distance: 13.4,
targetYaw: 0.19,
targetPitch: 1.19,
targetDistance: 13.4
};
function updateCamera() {
const s = Math.sin(orbit.pitch);
const c = Math.cos(orbit.pitch);
camera.position.set(
cameraTarget.x + orbit.distance * s * Math.sin(orbit.yaw),
cameraTarget.y + orbit.distance * c,
cameraTarget.z + orbit.distance * s * Math.cos(orbit.yaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xa5dcff, 0x071018, 1.6);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xf4fbff, 3.1);
keyLight.position.set(4, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const warmLight = new THREE.PointLight(positiveColor, 22, 11, 2);
warmLight.position.set(-5, 3.5, 4);
scene.add(warmLight);
const coolLight = new THREE.PointLight(negativeColor, 20, 11, 2);
coolLight.position.set(5, 3.2, 4);
scene.add(coolLight);
const floorGeometry = new THREE.BoxGeometry(0.88, 0.12, 0.88);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
metalness: 0.08
});
const floorColumns = 15;
const floorRows = 9;
const floorTiles = new THREE.InstancedMesh(
floorGeometry,
floorMaterial,
floorColumns * floorRows
);
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
let floorIndex = 0;
for (let x = 0; x < floorColumns; x++) {
for (let z = 0; z < floorRows; z++) {
const px = (x - (floorColumns - 1) / 2) * 0.94;
const pz = (z - 3) * 0.94 - 0.8;
const height = ((x * 13 + z * 7) % 5) * 0.012;
dummy.position.set(px, -0.56 + height, pz);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
floorTiles.setMatrixAt(floorIndex, dummy.matrix);
const shade = 0.72 + ((x + z * 3) % 4) * 0.055;
tempColor.set(0x0e202c).multiplyScalar(shade);
floorTiles.setColorAt(floorIndex, tempColor);
floorIndex++;
}
}
floorTiles.receiveShadow = true;
floorTiles.instanceMatrix.needsUpdate = true;
floorTiles.instanceColor.needsUpdate = true;
scene.add(floorTiles);
// Suspended support beam, built from discrete blocks.
const beamGeometry = new THREE.BoxGeometry(1, 1, 1);
const beamMaterial = new THREE.MeshStandardMaterial({
color: 0x29404e,
roughness: 0.68,
metalness: 0.32
});
const beamSegments = 19;
const beam = new THREE.InstancedMesh(
beamGeometry,
beamMaterial,
beamSegments
);
for (let i = 0; i < beamSegments; i++) {
dummy.position.set((i - 9) * 0.46, 6.05, 0);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.43, 0.24, 0.38);
dummy.updateMatrix();
beam.setMatrixAt(i, dummy.matrix);
}
beam.castShadow = true;
beam.receiveShadow = true;
scene.add(beam);
const endCapMaterial = new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.15,
roughness: 0.5
});
for (const x of [-4.38, 4.38]) {
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.34, 0.48),
endCapMaterial
);
cap.position.set(x, 6.05, 0);
cap.castShadow = true;
scene.add(cap);
}
function addVoxelString(start, end, count = 22) {
const geometry = new THREE.BoxGeometry(0.065, 0.065, 0.065);
const material = new THREE.MeshStandardMaterial({
color: 0x9bb4bf,
roughness: 0.5,
metalness: 0.35
});
const group = new THREE.Group();
for (let i = 0; i < count; i++) {
const t = i / (count - 1);
const block = new THREE.Mesh(geometry, material);
block.position.lerpVectors(start, end, t);
group.add(block);
}
scene.add(group);
return group;
}
const positiveCenter = new THREE.Vector3(-2.2, 2.58, 0);
const negativeCenter = new THREE.Vector3(2.2, 2.58, 0);
addVoxelString(
new THREE.Vector3(positiveCenter.x, 5.93, 0),
new THREE.Vector3(positiveCenter.x, positiveCenter.y + 1.08, 0)
);
addVoxelString(
new THREE.Vector3(negativeCenter.x, 5.93, 0),
new THREE.Vector3(negativeCenter.x, negativeCenter.y + 1.08, 0)
);
function createVoxelBall(center, chargeSign, facingDirection) {
const step = 0.285;
const voxelSize = 0.265;
const points = [];
for (let x = -4; x <= 4; x++) {
for (let y = -4; y <= 4; y++) {
for (let z = -4; z <= 4; z++) {
const d2 = x * x + y * y + z * z;
// A hollow lattice shell: all charge is on the outer surface.
if (d2 >= 9 && d2 <= 16) {
points.push(new THREE.Vector3(x * step, y * step, z * step));
}
}
}
}
const geometry = new THREE.BoxGeometry(voxelSize, voxelSize, voxelSize);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.48,
metalness: 0.13
});
const mesh = new THREE.InstancedMesh(geometry, material, points.length);
mesh.position.copy(center);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
const baseColor = chargeSign > 0 ? positiveColor : negativeColor;
const maxRadius = Math.sqrt(16) * step;
const data = points.map((position) => {
const direction = position.clone().normalize();
const facing = THREE.MathUtils.clamp(
direction.dot(facingDirection),
-1,
1
);
// Qualitative surface-density variation:
// positive ball is denser on the near side;
// negative ball is denser toward its far side.
const density =
chargeSign > 0
? 0.42 + 0.58 * Math.max(0, facing)
: 0.92 - 0.43 * Math.max(0, facing);
return { position, density };
});
function update(mode) {
const base = new THREE.Color(baseColor);
const white = new THREE.Color(0xffffff);
data.forEach((entry, index) => {
const density = mode === "surface" ? entry.density : 0.55;
const scale = mode === "surface" ? 0.93 + density * 0.13 : 0.98;
dummy.position.copy(entry.position);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(scale);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
tempColor.copy(base).multiplyScalar(0.57 + density * 0.58);
tempColor.lerp(white, density * 0.045);
mesh.setColorAt(index, tempColor);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
}
update("surface");
scene.add(mesh);
return { mesh, update, data };
}
const positiveBall = createVoxelBall(
positiveCenter,
1,
new THREE.Vector3(1, 0, 0)
);
const negativeBall = createVoxelBall(
negativeCenter,
-1,
new THREE.Vector3(-1, 0, 0)
);
// Visible conceptual point markers. These are intentionally drawn
// through the conductor skin to distinguish model points from charge.
const centerMarkerMaterial = new THREE.MeshBasicMaterial({
color: 0xf3fbff,
transparent: true,
opacity: 0.95,
depthTest: false
});
const centerMarkers = [];
for (const center of [positiveCenter, negativeCenter]) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.15, 0.15),
centerMarkerMaterial
);
marker.position.copy(center);
marker.renderOrder = 20;
scene.add(marker);
centerMarkers.push(marker);
}
// Dashed center-to-center measurement line.
const distanceGeometry = new THREE.BufferGeometry().setFromPoints([
positiveCenter.clone().setY(2.66).setZ(1.18),
negativeCenter.clone().setY(2.66).setZ(1.18)
]);
const distanceMaterial = new THREE.LineDashedMaterial({
color: forceColor,
dashSize: 0.15,
gapSize: 0.11,
transparent: true,
opacity: 0.46,
depthTest: false
});
const distanceLine = new THREE.Line(
distanceGeometry,
distanceMaterial
);
distanceLine.computeLineDistances();
distanceLine.renderOrder = 18;
scene.add(distanceLine);
function createVoxelArrow(color) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.45,
roughness: 0.42,
metalness: 0.12
});
const cube = new THREE.BoxGeometry(1, 1, 1);
const shaft = new THREE.Mesh(cube, material);
shaft.position.set(0.28, 0, 0);
shaft.scale.set(0.56, 0.12, 0.12);
group.add(shaft);
const head = [
[0.59, 0, 0, 0.18],
[0.72, -0.10, 0, 0.15],
[0.72, 0.10, 0, 0.15],
[0.84, -0.20, 0, 0.13],
[0.84, 0, 0, 0.13],
[0.84, 0.20, 0, 0.13],
[0.95, 0, 0, 0.11]
];
for (const [x, y, z, size] of head) {
const block = new THREE.Mesh(cube, material);
block.position.set(x, y, z);
block.scale.setScalar(size);
group.add(block);
}
group.traverse((child) => {
if (child.isMesh) child.castShadow = true;
});
group.userData.material = material;
return group;
}
const positiveArrow = createVoxelArrow(forceColor);
positiveArrow.position.set(-1.08, 2.67, 1.18);
scene.add(positiveArrow);
const negativeArrow = createVoxelArrow(forceColor);
negativeArrow.position.set(1.08, 2.67, 1.18);
negativeArrow.rotation.y = Math.PI;
scene.add(negativeArrow);
// Sparse background voxels.
const starGeometry = new THREE.BoxGeometry(0.045, 0.045, 0.045);
const starMaterial = new THREE.MeshBasicMaterial({
color: 0x668897,
transparent: true,
opacity: 0.28,
depthWrite: false
});
const stars = new THREE.InstancedMesh(starGeometry, starMaterial, 55);
for (let i = 0; i < 55; i++) {
const seed = Math.sin(i * 127.1 + 3.7) * 43758.5453;
const random = seed - Math.floor(seed);
dummy.position.set(
(random - 0.5) * 18,
0.5 + random * 8,
-2 - random * 8
);
dummy.rotation.set(random * 2, random * 3, random * 4);
dummy.scale.setScalar(0.7 + random * 1.8);
dummy.updateMatrix();
stars.setMatrixAt(i, dummy.matrix);
}
scene.add(stars);
/* ---------------------------------------------------------------
Custom camera controls — no addons
--------------------------------------------------------------- */
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.targetYaw -= dx * 0.006;
orbit.targetPitch = THREE.MathUtils.clamp(
orbit.targetPitch - dy * 0.005,
0.72,
1.43
);
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.targetDistance = THREE.MathUtils.clamp(
orbit.targetDistance + event.deltaY * 0.009,
9.5,
17.5
);
},
{ passive: false }
);
function resetView() {
orbit.targetYaw = 0.19;
orbit.targetPitch = 1.19;
orbit.targetDistance = 13.4;
}
document.querySelector("#resetView").addEventListener("click", resetView);
renderer.domElement.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") orbit.targetYaw += 0.08;
if (event.key === "ArrowRight") orbit.targetYaw -= 0.08;
if (event.key === "ArrowUp") orbit.targetPitch -= 0.06;
if (event.key === "ArrowDown") orbit.targetPitch += 0.06;
if (event.key === "+" || event.key === "=") {
orbit.targetDistance = Math.max(9.5, orbit.targetDistance - 0.5);
}
if (event.key === "-") {
orbit.targetDistance = Math.min(17.5, orbit.targetDistance + 0.5);
}
if (event.key.toLowerCase() === "r") resetView();
});
/* ---------------------------------------------------------------
Surface / point-model controls
--------------------------------------------------------------- */
const modeButtons = document.querySelectorAll("[data-mode]");
const modeStatus = document.querySelector("#modeStatus");
modeButtons.forEach((button) => {
button.addEventListener("click", () => {
const mode = button.dataset.mode;
modeButtons.forEach((item) => {
item.classList.toggle("active", item === button);
});
positiveBall.update(mode);
negativeBall.update(mode);
modeStatus.textContent =
mode === "surface"
? "Tints: qualitative surface-charge density"
: "Tints: uniform point-charge stand-in";
});
});
const forceToggle = document.querySelector("#forceToggle");
const forceTag = document.querySelector("#forceTag");
let forcesVisible = true;
forceToggle.addEventListener("click", () => {
forcesVisible = !forcesVisible;
forceToggle.setAttribute("aria-pressed", String(forcesVisible));
positiveArrow.visible = forcesVisible;
negativeArrow.visible = forcesVisible;
distanceLine.visible = forcesVisible;
forceTag.style.display = forcesVisible ? "flex" : "none";
});
/* ---------------------------------------------------------------
Ideal point-charge inset
--------------------------------------------------------------- */
const insetRenderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
insetRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
insetRenderer.setClearColor(0x07131d, 1);
insetRenderer.outputColorSpace = THREE.SRGBColorSpace;
insetRenderer.toneMapping = THREE.ACESFilmicToneMapping;
insetRenderer.toneMappingExposure = 1.15;
insetViewport.appendChild(insetRenderer.domElement);
const insetScene = new THREE.Scene();
const insetCamera = new THREE.PerspectiveCamera(42, 1, 0.1, 20);
insetCamera.position.set(0, 0, 7.4);
insetCamera.lookAt(0, 0, 0);
const insetAmbient = new THREE.AmbientLight(0x9bc9d6, 1.1);
insetScene.add(insetAmbient);
const insetPointMaterial = new THREE.MeshStandardMaterial({
color: positiveColor,
emissive: positiveColor,
emissiveIntensity: 2,
roughness: 0.3
});
const insetPoint = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.27, 0.27),
insetPointMaterial
);
insetScene.add(insetPoint);
const insetLight = new THREE.PointLight(positiveColor, 16, 7, 2);
insetScene.add(insetLight);
const fieldMaterial = new THREE.LineBasicMaterial({
color: positiveColor,
transparent: true,
opacity: 0.24
});
const rayCount = 12;
const particlesPerRay = 3;
for (let i = 0; i < rayCount; i++) {
const angle = (i / rayCount) * Math.PI * 2;
const points = [
new THREE.Vector3(
Math.cos(angle) * 0.34,
Math.sin(angle) * 0.34,
0
),
new THREE.Vector3(
Math.cos(angle) * 2.82,
Math.sin(angle) * 2.82,
0
)
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, fieldMaterial);
insetScene.add(line);
}
const ringMaterial = new THREE.LineDashedMaterial({
color: 0x7ca1b0,
transparent: true,
opacity: 0.2,
dashSize: 0.07,
gapSize: 0.055
});
for (const radius of [0.82, 1.48, 2.15, 2.75]) {
const points = [];
for (let i = 0; i <= 96; i++) {
const angle = (i / 96) * Math.PI * 2;
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
Math.sin(angle) * radius,
-0.04
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const ring = new THREE.Line(geometry, ringMaterial);
ring.computeLineDistances();
insetScene.add(ring);
}
const particleMaterial = new THREE.MeshBasicMaterial({
color: positiveColor,
transparent: true,
opacity: 0.88
});
const particles = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.065, 0.065, 0.065),
particleMaterial,
rayCount * particlesPerRay
);
particles.frustumCulled = false;
insetScene.add(particles);
let insetSign = 1;
function setInsetSign(sign) {
insetSign = sign;
const color = sign > 0 ? positiveColor : negativeColor;
insetPointMaterial.color.setHex(color);
insetPointMaterial.emissive.setHex(color);
insetLight.color.setHex(color);
fieldMaterial.color.setHex(color);
particleMaterial.color.setHex(color);
document.querySelectorAll("[data-sign]").forEach((button) => {
button.classList.toggle(
"active",
Number(button.dataset.sign) === sign
);
});
document.querySelector("#qBadge span").textContent =
sign > 0 ? "+q" : "−q";
document.querySelector("#qBadge").style.color =
sign > 0 ? "#ff745c" : "#52dfff";
document.querySelector("#insetFormula").textContent =
sign > 0
? "E(r) = kq / r² r̂"
: "E(r) = −k|q| / r² r̂";
document.querySelector("#fieldDirection").textContent =
sign > 0
? "Electric field points outward"
: "Electric field points inward";
}
document.querySelectorAll("[data-sign]").forEach((button) => {
button.addEventListener("click", () => {
setInsetSign(Number(button.dataset.sign));
});
});
/* ---------------------------------------------------------------
Resize and projected HTML labels
--------------------------------------------------------------- */
let mainWidth = 1;
let mainHeight = 1;
function resizeMain() {
mainWidth = Math.max(1, mainViewport.clientWidth);
mainHeight = Math.max(1, mainViewport.clientHeight);
renderer.setSize(mainWidth, mainHeight, false);
camera.aspect = mainWidth / mainHeight;
camera.updateProjectionMatrix();
}
function resizeInset() {
const width = Math.max(1, insetViewport.clientWidth);
const height = Math.max(1, insetViewport.clientHeight);
insetRenderer.setSize(width, height, false);
insetCamera.aspect = width / height;
insetCamera.updateProjectionMatrix();
}
const mainResizeObserver = new ResizeObserver(resizeMain);
mainResizeObserver.observe(mainViewport);
const insetResizeObserver = new ResizeObserver(resizeInset);
insetResizeObserver.observe(insetViewport);
resizeMain();
resizeInset();
const positiveTag = document.querySelector("#positiveTag");
const negativeTag = document.querySelector("#negativeTag");
const centerTag = document.querySelector("#centerTag");
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "flex" : "none";
if (!visible) return;
element.style.left =
`${((projected.x + 1) / 2) * mainWidth}px`;
element.style.top =
`${((-projected.y + 1) / 2) * mainHeight}px`;
}
/* ---------------------------------------------------------------
Animation
--------------------------------------------------------------- */
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.04);
const smoothing = reducedMotion ? 1 : 0.085;
orbit.yaw += (orbit.targetYaw - orbit.yaw) * smoothing;
orbit.pitch += (orbit.targetPitch - orbit.pitch) * smoothing;
orbit.distance += (orbit.targetDistance - orbit.distance) * smoothing;
updateCamera();
const markerScale =
1 + (reducedMotion ? 0 : Math.sin(elapsed * 2.6) * 0.08);
for (const marker of centerMarkers) {
marker.scale.setScalar(markerScale);
}
const arrowGlow =
0.38 + (reducedMotion ? 0 : Math.sin(elapsed * 2.1) * 0.12);
positiveArrow.userData.material.emissiveIntensity = arrowGlow;
negativeArrow.userData.material.emissiveIntensity = arrowGlow;
placeLabel(
positiveTag,
positiveCenter.clone().add(new THREE.Vector3(0, 1.43, 0))
);
placeLabel(
negativeTag,
negativeCenter.clone().add(new THREE.Vector3(0, 1.43, 0))
);
placeLabel(
centerTag,
positiveCenter.clone().add(new THREE.Vector3(-0.15, -0.24, 0.75))
);
placeLabel(
forceTag,
new THREE.Vector3(0, 3.18, 1.18)
);
// Animate field particles. Positive charges move outward;
// negative charges make the field particles move inward.
let particleIndex = 0;
for (let ray = 0; ray < rayCount; ray++) {
const angle = (ray / rayCount) * Math.PI * 2;
for (let step = 0; step < particlesPerRay; step++) {
const phase =
(elapsed * (reducedMotion ? 0.035 : 0.22) +
step / particlesPerRay +
ray * 0.037) %
1;
const radius =
insetSign > 0
? 0.34 + phase * 2.45
: 2.79 - phase * 2.45;
dummy.position.set(
Math.cos(angle) * radius,
Math.sin(angle) * radius,
Math.sin(elapsed * 0.7 + ray) * 0.025
);
dummy.rotation.set(0, 0, elapsed * 0.4 + ray);
dummy.scale.setScalar(0.8 + Math.sin(phase * Math.PI) * 0.5);
dummy.updateMatrix();
particles.setMatrixAt(particleIndex, dummy.matrix);
particleIndex++;
}
}
particles.instanceMatrix.needsUpdate = true;
insetPoint.rotation.x = elapsed * 0.55;
insetPoint.rotation.y = elapsed * 0.8;
renderer.render(scene, camera);
insetRenderer.render(insetScene, insetCamera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```