Prompt specification
Build or customize the Three.js 3D scene "Particle-beam bending museum 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: Particle-beam bending museum exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/particle-beam-bending-museum-exhibit-x1647
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows magnetic forces on moving charged particles. Compose a bounded transparent chamber with prescribed magnetic field and charged-particle track; label the apparatus conceptual and omit real accelerator construction details. Voxel style.
Working Three.js Starter (particle-beam-bending-museum-exhibit-x1647):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Magnetic Curvature — Conceptual Visualization</title>
<style>
:root {
--bg: #050914;
--panel: rgba(9, 18, 34, 0.82);
--line: rgba(117, 215, 255, 0.22);
--cyan: #61dcff;
--amber: #ffbd5a;
--violet: #aa83ff;
--pink: #ff4f91;
--white: #eaf7ff;
--muted: #91a8bb;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 68% 38%, #10243c 0%, #08111f 35%, var(--bg) 75%);
color: var(--white);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
top: 24px;
left: 26px;
z-index: 10;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(26px, 4vw, 44px);
font-weight: 850;
line-height: 0.98;
letter-spacing: -0.055em;
text-shadow: 0 4px 30px rgba(0, 0, 0, 0.45);
}
.subtitle {
margin: 11px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.concept-badge {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding: 7px 10px;
border: 1px solid rgba(255, 189, 90, 0.38);
border-radius: 3px;
background: rgba(255, 189, 90, 0.07);
color: #ffd18e;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.concept-badge::before {
width: 6px;
height: 6px;
content: "";
background: var(--amber);
box-shadow: 0 0 12px rgba(255, 189, 90, 0.8);
}
.readout {
position: fixed;
top: 26px;
right: 26px;
z-index: 10;
display: grid;
grid-template-columns: auto auto;
gap: 7px 18px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 4px;
background: var(--panel);
backdrop-filter: blur(12px);
pointer-events: none;
font-size: 10px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout .key {
color: var(--muted);
}
.readout .value {
color: var(--cyan);
font-weight: 800;
text-align: right;
}
.live-dot {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 7px;
border-radius: 50%;
background: #6effb1;
box-shadow: 0 0 10px #6effb1;
vertical-align: 1px;
}
.legend {
position: fixed;
left: 26px;
bottom: 118px;
z-index: 10;
display: grid;
gap: 8px;
padding: 11px 13px;
border-left: 2px solid rgba(97, 220, 255, 0.28);
background: linear-gradient(90deg, rgba(7, 15, 28, 0.72), transparent);
color: var(--muted);
pointer-events: none;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 8px;
height: 8px;
box-shadow: 0 0 9px currentColor;
}
.swatch.field { color: var(--cyan); background: var(--cyan); }
.swatch.force { color: var(--pink); background: var(--pink); }
.swatch.positive { color: var(--amber); background: var(--amber); }
.swatch.negative { color: var(--violet); background: var(--violet); }
.swatch.velocity { color: white; background: white; }
.controls {
position: fixed;
left: 50%;
bottom: 18px;
z-index: 20;
display: flex;
align-items: center;
gap: 14px;
width: min(760px, calc(100vw - 24px));
padding: 12px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 5px;
background: var(--panel);
box-shadow: 0 16px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(16px);
}
button {
min-height: 38px;
padding: 0 13px;
border: 1px solid rgba(97, 220, 255, 0.3);
border-radius: 3px;
background: rgba(97, 220, 255, 0.08);
color: var(--white);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
button:hover {
border-color: rgba(97, 220, 255, 0.7);
background: rgba(97, 220, 255, 0.16);
}
button:active {
transform: translateY(1px);
}
button.primary {
min-width: 82px;
border-color: rgba(255, 189, 90, 0.5);
background: rgba(255, 189, 90, 0.12);
color: #ffd18e;
}
.slider-control {
flex: 1 1 170px;
min-width: 130px;
}
.slider-heading {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.slider-heading output {
color: var(--cyan);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 0;
appearance: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--range, 50%),
rgba(255, 255, 255, 0.12) var(--range, 50%)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 12px;
height: 12px;
border: 2px solid #07111d;
border-radius: 1px;
appearance: none;
background: var(--cyan);
box-shadow: 0 0 12px rgba(97, 220, 255, 0.75);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 2px solid #07111d;
border-radius: 1px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(97, 220, 255, 0.75);
}
.hint {
position: fixed;
right: 26px;
bottom: 115px;
z-index: 10;
color: rgba(145, 168, 187, 0.72);
pointer-events: none;
font-size: 9px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.world-label {
position: fixed;
top: 0;
left: 0;
z-index: 8;
display: none;
padding: 5px 7px;
transform: translate(-50%, -50%);
border: 1px solid rgba(97, 220, 255, 0.28);
border-radius: 2px;
background: rgba(5, 10, 20, 0.76);
color: var(--cyan);
pointer-events: none;
white-space: nowrap;
box-shadow: 0 3px 14px rgba(0, 0, 0, 0.35);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.world-label.positive {
border-color: rgba(255, 189, 90, 0.4);
color: var(--amber);
}
.world-label.negative {
border-color: rgba(170, 131, 255, 0.4);
color: var(--violet);
}
.world-label.force {
border-color: rgba(255, 79, 145, 0.45);
color: #ff82b3;
}
.world-label.velocity {
border-color: rgba(255, 255, 255, 0.26);
color: white;
}
.world-label.boundary {
color: rgba(160, 220, 240, 0.72);
}
@media (max-width: 760px) {
.topbar {
top: 18px;
left: 18px;
max-width: 78vw;
}
.subtitle {
max-width: 310px;
}
.readout {
top: auto;
right: 18px;
bottom: 103px;
display: none;
}
.legend {
display: none;
}
.hint {
display: none;
}
.controls {
flex-wrap: wrap;
gap: 10px;
bottom: 10px;
padding: 10px;
}
.slider-control {
flex-basis: calc(50% - 10px);
}
button {
flex: 1;
padding: 0 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="topbar">
<div class="eyebrow">Conceptual visualization</div>
<h1>FIELD / CURVATURE</h1>
<p class="subtitle">
Moving charges in a prescribed uniform magnetic field.
Force vectors illustrate <strong>F = q(v × B)</strong>.
</p>
<div class="concept-badge">Conceptual apparatus — not a build plan</div>
</header>
<div class="readout" aria-live="polite">
<span class="key">State</span>
<span class="value"><i class="live-dot"></i><span id="stateText">Live</span></span>
<span class="key">Field</span>
<span class="value" id="fieldReadout">B = +Z</span>
<span class="key">Strength</span>
<span class="value" id="strengthReadout">1.0 ×</span>
<span class="key">Speed</span>
<span class="value" id="speedReadout">1.0 ×</span>
</div>
<div class="legend" aria-hidden="true">
<div class="legend-item"><i class="swatch field"></i> Prescribed field</div>
<div class="legend-item"><i class="swatch force"></i> Magnetic force</div>
<div class="legend-item"><i class="swatch velocity"></i> Velocity</div>
<div class="legend-item"><i class="swatch positive"></i> Positive charge</div>
<div class="legend-item"><i class="swatch negative"></i> Negative charge</div>
</div>
<div class="hint">Drag to orbit · Wheel to zoom · Double-click to reset view</div>
<div id="fieldLabel" class="world-label">B = +Z</div>
<div id="boundaryLabel" class="world-label boundary">Transparent boundary</div>
<div id="positiveLabel" class="world-label positive">q +</div>
<div id="negativeLabel" class="world-label negative">q −</div>
<div id="positiveForceLabel" class="world-label force">F +</div>
<div id="negativeForceLabel" class="world-label force">F −</div>
<div id="positiveVelocityLabel" class="world-label velocity">v +</div>
<div id="negativeVelocityLabel" class="world-label velocity">v −</div>
<section class="controls" aria-label="Visualization controls">
<button id="toggleButton" class="primary" type="button">Pause</button>
<button id="polarityButton" type="button">B: +Z</button>
<div class="slider-control">
<div class="slider-heading">
<label for="strengthSlider">Field strength</label>
<output id="strengthOutput" for="strengthSlider">1.0×</output>
</div>
<input
id="strengthSlider"
type="range"
min="0.4"
max="1.8"
step="0.1"
value="1"
/>
</div>
<div class="slider-control">
<div class="slider-heading">
<label for="speedSlider">Particle speed</label>
<output id="speedOutput" for="speedSlider">1.0×</output>
</div>
<input
id="speedSlider"
type="range"
min="0.35"
max="1.8"
step="0.05"
value="1"
/>
</div>
<button id="restartButton" type="button">Restart</button>
<button id="viewButton" type="button">Reset view</button>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x050914, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.domElement.setAttribute(
"aria-label",
"Interactive conceptual visualization of charged particles in a magnetic field"
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x050914, 0.018);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
scene.add(new THREE.HemisphereLight(0x9edfff, 0x10101d, 1.25));
const keyLight = new THREE.DirectionalLight(0xb9eaff, 2.4);
keyLight.position.set(5, 9, 8);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x8867ff, 1.25);
rimLight.position.set(-8, 2, -7);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom and reset
// -------------------------------------------------------------------------
const Z_AXIS = new THREE.Vector3(0, 0, 1);
const cameraState = {
azimuth: 0.72,
polar: 1.03,
radius: 20,
target: new THREE.Vector3(0, 0, 0)
};
function updateCamera() {
const sinPolar = Math.sin(cameraState.polar);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPolar * Math.sin(cameraState.azimuth),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.polar),
cameraState.target.z +
cameraState.radius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraState.azimuth = 0.72;
cameraState.polar = 1.03;
cameraState.radius = 20;
cameraState.target.set(0, 0, 0);
updateCamera();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.005,
0.28,
Math.PI - 0.28
);
updateCamera();
});
function endDrag(event) {
dragging = false;
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();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
11,
31
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
updateCamera();
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function addBox(width, height, depth, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
parent.add(mesh);
return mesh;
}
function createVoxelArrow(material) {
const group = new THREE.Group();
// A stepped arrow assembled entirely from box geometry.
addBox(0.07, 0.07, 0.58, material, 0, 0, 0.29, group);
addBox(0.18, 0.18, 0.14, material, 0, 0, 0.65, group);
addBox(0.12, 0.12, 0.14, material, 0, 0, 0.79, group);
addBox(0.07, 0.07, 0.14, material, 0, 0, 0.93, group);
group.renderOrder = 8;
return group;
}
function orientArrow(arrow, origin, direction, length = 1) {
arrow.position.copy(origin);
arrow.quaternion.setFromUnitVectors(
Z_AXIS,
direction.clone().normalize()
);
arrow.scale.set(1, 1, length);
}
// -------------------------------------------------------------------------
// Voxel background
// -------------------------------------------------------------------------
const starMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const starCount = 100;
const stars = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
starMaterial,
starCount
);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
for (let i = 0; i < starCount; i++) {
const u = Math.random() * 2 - 1;
const angle = Math.random() * Math.PI * 2;
const radius = 14 + Math.random() * 17;
const planar = Math.sqrt(1 - u * u);
const size = 0.025 + Math.random() * 0.085;
dummy.position.set(
planar * Math.cos(angle) * radius,
u * radius,
planar * Math.sin(angle) * radius
);
dummy.scale.set(size, size, size);
dummy.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
dummy.updateMatrix();
stars.setMatrixAt(i, dummy.matrix);
color.setHSL(0.53 + Math.random() * 0.08, 0.62, 0.52);
stars.setColorAt(i, color);
}
stars.instanceMatrix.needsUpdate = true;
stars.instanceColor.needsUpdate = true;
scene.add(stars);
// -------------------------------------------------------------------------
// Bounded transparent chamber
// -------------------------------------------------------------------------
const chamberWidth = 12;
const chamberHeight = 7;
const chamberDepth = 8;
const panelMaterial = new THREE.MeshPhysicalMaterial({
color: 0x69d9ff,
transparent: true,
opacity: 0.045,
roughness: 0.18,
metalness: 0.05,
side: THREE.DoubleSide,
depthWrite: false
});
function addPanel(width, height, x, y, z, rx = 0, ry = 0) {
const panel = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
panelMaterial
);
panel.position.set(x, y, z);
panel.rotation.set(rx, ry, 0);
panel.renderOrder = 1;
scene.add(panel);
}
addPanel(chamberWidth, chamberHeight, 0, 0, chamberDepth / 2);
addPanel(chamberWidth, chamberHeight, 0, 0, -chamberDepth / 2);
addPanel(
chamberWidth,
chamberDepth,
0,
chamberHeight / 2,
0,
Math.PI / 2
);
addPanel(
chamberWidth,
chamberDepth,
0,
-chamberHeight / 2,
0,
Math.PI / 2
);
addPanel(
chamberDepth,
chamberHeight,
chamberWidth / 2,
0,
0,
0,
Math.PI / 2
);
addPanel(
chamberDepth,
chamberHeight,
-chamberWidth / 2,
0,
0,
0,
Math.PI / 2
);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x17364c,
metalness: 0.62,
roughness: 0.4,
emissive: 0x061c29,
emissiveIntensity: 0.8
});
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x3c7590,
metalness: 0.5,
roughness: 0.28,
emissive: 0x0b4358,
emissiveIntensity: 0.85
});
// Corner posts.
for (const sx of [-1, 1]) {
for (const sy of [-1, 1]) {
for (const sz of [-1, 1]) {
addBox(
0.16,
chamberHeight,
0.16,
frameMaterial,
sx * chamberWidth / 2,
sy * chamberHeight / 2,
sz * chamberDepth / 2
);
addBox(
0.25,
0.25,
0.25,
edgeMaterial,
sx * chamberWidth / 2,
sy * chamberHeight / 2,
sz * chamberDepth / 2
);
}
}
}
// X-axis frame beams.
for (const sy of [-1, 1]) {
for (const sz of [-1, 1]) {
addBox(
chamberWidth,
0.12,
0.12,
frameMaterial,
0,
sy * chamberHeight / 2,
sz * chamberDepth / 2
);
}
}
// Y-axis frame beams.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
addBox(
0.12,
chamberHeight,
0.12,
frameMaterial,
sx * chamberWidth / 2,
0,
sz * chamberDepth / 2
);
}
}
// Z-axis frame beams.
for (const sx of [-1, 1]) {
for (const sy of [-1, 1]) {
addBox(
0.12,
0.12,
chamberDepth,
frameMaterial,
sx * chamberWidth / 2,
sy * chamberHeight / 2,
0
);
}
}
const gridMaterial = new THREE.MeshBasicMaterial({
color: 0x4abde5,
transparent: true,
opacity: 0.11,
depthWrite: false
});
// Floor grid.
for (let z = -chamberDepth / 2; z <= chamberDepth / 2; z += 1) {
addBox(
chamberWidth,
0.022,
0.018,
gridMaterial,
0,
-chamberHeight / 2 + 0.025,
z
);
}
for (let x = -chamberWidth / 2; x <= chamberWidth / 2; x += 1) {
addBox(
0.018,
0.022,
chamberDepth,
gridMaterial,
x,
-chamberHeight / 2 + 0.025,
0
);
}
// Rear grid.
for (let x = -chamberWidth / 2; x <= chamberWidth / 2; x += 1) {
addBox(
0.016,
chamberHeight,
0.016,
gridMaterial,
x,
0,
-chamberDepth / 2 + 0.025
);
}
for (let y = -chamberHeight / 2; y <= chamberHeight / 2; y += 1) {
addBox(
chamberWidth,
0.016,
0.016,
gridMaterial,
0,
y,
-chamberDepth / 2 + 0.025
);
}
// Abstract plinth: visual framing only.
const plinthMaterial = new THREE.MeshStandardMaterial({
color: 0x0b1725,
metalness: 0.5,
roughness: 0.6,
emissive: 0x07111d,
emissiveIntensity: 0.5
});
addBox(
chamberWidth + 1.2,
0.3,
chamberDepth + 1.2,
plinthMaterial,
0,
-chamberHeight / 2 - 0.3,
0
);
addBox(
chamberWidth - 1.0,
0.24,
chamberDepth - 1.0,
plinthMaterial,
0,
-chamberHeight / 2 - 0.65,
0
);
// -------------------------------------------------------------------------
// Prescribed magnetic-field arrows
// -------------------------------------------------------------------------
const fieldMaterial = new THREE.MeshBasicMaterial({
color: 0x61dcff,
transparent: true,
opacity: 0.32,
depthWrite: false,
depthTest: false
});
const fieldArrows = [];
const fieldX = [-4.45, -1.5, 1.5, 4.45];
const fieldY = [-2.25, 0, 2.25];
const fieldZ = [-2.8, 0, 2.8];
let fieldIndex = 0;
for (const x of fieldX) {
for (const y of fieldY) {
const arrow = createVoxelArrow(fieldMaterial);
arrow.position.set(x, y, fieldZ[fieldIndex % fieldZ.length]);
arrow.scale.z = 0.9;
arrow.renderOrder = 4;
scene.add(arrow);
fieldArrows.push(arrow);
fieldIndex++;
}
}
// -------------------------------------------------------------------------
// Particle paths
// -------------------------------------------------------------------------
const positiveParticle = {
q: 1,
cx: 2.55,
cy: 0.1,
z0: -0.7,
zPhase: 0.2,
radius: 1.55,
offset: 2.55,
theta: 2.55,
color: 0xffbd5a,
position: new THREE.Vector3(),
velocity: new THREE.Vector3(),
forceDirection: new THREE.Vector3()
};
const negativeParticle = {
q: -1,
cx: -2.55,
cy: -0.1,
z0: 0.7,
zPhase: 1.1,
radius: 1.55,
offset: 0.4,
theta: 0.4,
color: 0xaa83ff,
position: new THREE.Vector3(),
velocity: new THREE.Vector3(),
forceDirection: new THREE.Vector3()
};
const particles = [positiveParticle, negativeParticle];
const trackCount = 110;
const trackGeometry = new THREE.BoxGeometry(0.13, 0.085, 0.085);
function buildTrack(particle, material) {
const track = new THREE.InstancedMesh(
trackGeometry,
material,
trackCount
);
for (let i = 0; i < trackCount; i++) {
const theta =
particle.offset +
(i / trackCount) * Math.PI * 2;
const x = particle.cx + Math.cos(theta) * particle.radius;
const y = particle.cy + Math.sin(theta) * particle.radius;
const z =
particle.z0 +
0.28 * Math.sin(theta + particle.zPhase);
const tangentAngle = Math.atan2(
Math.cos(theta),
-Math.sin(theta)
);
dummy.position.set(x, y, z);
dummy.rotation.set(0, 0, tangentAngle);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
track.setMatrixAt(i, dummy.matrix);
}
track.instanceMatrix.needsUpdate = true;
track.renderOrder = 3;
scene.add(track);
return track;
}
const positiveTrackMaterial = new THREE.MeshBasicMaterial({
color: 0xffb84d,
transparent: true,
opacity: 0.34,
depthWrite: false
});
const negativeTrackMaterial = new THREE.MeshBasicMaterial({
color: 0x9b72ff,
transparent: true,
opacity: 0.34,
depthWrite: false
});
buildTrack(positiveParticle, positiveTrackMaterial);
buildTrack(negativeParticle, negativeTrackMaterial);
// -------------------------------------------------------------------------
// Voxel particles, moving trails and charge badges
// -------------------------------------------------------------------------
function createParticle(particle) {
const group = new THREE.Group();
const coreMaterial = new THREE.MeshStandardMaterial({
color: particle.color,
emissive: particle.color,
emissiveIntensity: 0.75,
metalness: 0.18,
roughness: 0.28
});
const shellMaterial = new THREE.MeshBasicMaterial({
color: particle.color,
transparent: true,
opacity: 0.2,
wireframe: true,
depthWrite: false
});
addBox(0.38, 0.38, 0.38, coreMaterial, 0, 0, 0, group);
addBox(0.16, 0.16, 0.16, coreMaterial, 0.12, 0.12, 0.12, group);
const shell = new THREE.Mesh(
new THREE.BoxGeometry(0.64, 0.64, 0.64),
shellMaterial
);
group.add(shell);
group.renderOrder = 6;
scene.add(group);
const badge = new THREE.Group();
const badgeMaterial = new THREE.MeshBasicMaterial({
color: particle.color,
depthTest: false,
depthWrite: false
});
if (particle.q > 0) {
addBox(0.29, 0.07, 0.05, badgeMaterial, 0, 0, 0, badge);
addBox(0.07, 0.29, 0.05, badgeMaterial, 0, 0, 0, badge);
} else {
addBox(0.29, 0.07, 0.05, badgeMaterial, 0, 0, 0, badge);
}
badge.renderOrder = 10;
scene.add(badge);
const trailCount = 18;
const trailMaterial = new THREE.MeshBasicMaterial({
color: particle.color,
transparent: true,
opacity: 0.5,
depthWrite: false
});
const trail = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.15, 0.15, 0.15),
trailMaterial,
trailCount
);
trail.frustumCulled = false;
trail.renderOrder = 5;
scene.add(trail);
return { group, badge, trail, trailCount };
}
const positiveVisuals = createParticle(positiveParticle);
const negativeVisuals = createParticle(negativeParticle);
const forceMaterial = new THREE.MeshBasicMaterial({
color: 0xff4f91,
transparent: true,
opacity: 0.95,
depthWrite: false,
depthTest: false
});
const velocityMaterial = new THREE.MeshBasicMaterial({
color: 0xf5fbff,
transparent: true,
opacity: 0.78,
depthWrite: false,
depthTest: false
});
const positiveForceArrow = createVoxelArrow(forceMaterial);
const negativeForceArrow = createVoxelArrow(forceMaterial);
const positiveVelocityArrow = createVoxelArrow(velocityMaterial);
const negativeVelocityArrow = createVoxelArrow(velocityMaterial);
scene.add(
positiveForceArrow,
negativeForceArrow,
positiveVelocityArrow,
negativeVelocityArrow
);
// -------------------------------------------------------------------------
// Labels
// -------------------------------------------------------------------------
const labels = {
field: document.getElementById("fieldLabel"),
boundary: document.getElementById("boundaryLabel"),
positive: document.getElementById("positiveLabel"),
negative: document.getElementById("negativeLabel"),
positiveForce: document.getElementById("positiveForceLabel"),
negativeForce: document.getElementById("negativeForceLabel"),
positiveVelocity: document.getElementById("positiveVelocityLabel"),
negativeVelocity: document.getElementById("negativeVelocityLabel")
};
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.08 &&
projected.x < 1.08 &&
projected.y > -1.08 &&
projected.y < 1.08;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// -------------------------------------------------------------------------
// Simulation state and controls
// -------------------------------------------------------------------------
let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let fieldSign = 1;
let fieldStrength = 1;
let particleSpeed = 1;
const toggleButton = document.getElementById("toggleButton");
const polarityButton = document.getElementById("polarityButton");
const restartButton = document.getElementById("restartButton");
const viewButton = document.getElementById("viewButton");
const strengthSlider = document.getElementById("strengthSlider");
const speedSlider = document.getElementById("speedSlider");
const strengthOutput = document.getElementById("strengthOutput");
const speedOutput = document.getElementById("speedOutput");
const fieldReadout = document.getElementById("fieldReadout");
const strengthReadout = document.getElementById("strengthReadout");
const speedReadout = document.getElementById("speedReadout");
const stateText = document.getElementById("stateText");
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--range", `${percent}%`);
}
function updateReadouts() {
const fieldText = fieldSign > 0 ? "+Z" : "−Z";
fieldReadout.textContent = `B = ${fieldText}`;
strengthReadout.textContent = `${fieldStrength.toFixed(1)} ×`;
speedReadout.textContent = `${particleSpeed.toFixed(1)} ×`;
strengthOutput.textContent = `${fieldStrength.toFixed(1)}×`;
speedOutput.textContent = `${particleSpeed.toFixed(1)}×`;
labels.field.textContent = `B = ${fieldText}`;
polarityButton.textContent = `B: ${fieldText}`;
toggleButton.textContent = playing ? "Pause" : "Play";
stateText.textContent = playing ? "Live" : "Paused";
}
function updateFieldOrientation() {
const direction = new THREE.Vector3(0, 0, fieldSign);
const arrowLength = 0.72 + fieldStrength * 0.18;
for (const arrow of fieldArrows) {
arrow.quaternion.setFromUnitVectors(Z_AXIS, direction);
arrow.scale.set(1, 1, arrowLength);
}
fieldMaterial.opacity = 0.22 + fieldStrength * 0.1;
}
toggleButton.addEventListener("click", () => {
playing = !playing;
updateReadouts();
});
polarityButton.addEventListener("click", () => {
fieldSign *= -1;
updateFieldOrientation();
updateReadouts();
});
strengthSlider.addEventListener("input", () => {
fieldStrength = Number(strengthSlider.value);
updateRangeFill(strengthSlider);
updateFieldOrientation();
updateReadouts();
});
speedSlider.addEventListener("input", () => {
particleSpeed = Number(speedSlider.value);
updateRangeFill(speedSlider);
updateReadouts();
});
restartButton.addEventListener("click", () => {
for (const particle of particles) {
particle.theta = particle.offset;
}
});
viewButton.addEventListener("click", resetCamera);
updateRangeFill(strengthSlider);
updateRangeFill(speedSlider);
updateFieldOrientation();
updateReadouts();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const cameraDirection = new THREE.Vector3();
const temporaryOrigin = new THREE.Vector3();
function sampleParticlePosition(particle, theta, target) {
target.set(
particle.cx + Math.cos(theta) * particle.radius,
particle.cy + Math.sin(theta) * particle.radius,
particle.z0 + 0.28 * Math.sin(theta + particle.zPhase)
);
return target;
}
function updateParticle(particle, visuals, forceArrow, velocityArrow) {
// With a uniform B along +/-Z, reversing B reverses the sense of motion.
const directionSign = -particle.q * fieldSign;
const angularSpeed = 0.85 * particleSpeed * fieldStrength;
if (playing) {
particle.theta += directionSign * angularSpeed * clock.getDelta();
}
const theta = particle.theta;
const dTheta = directionSign * angularSpeed;
sampleParticlePosition(particle, theta, particle.position);
particle.velocity.set(
-Math.sin(theta) * particle.radius * dTheta,
Math.cos(theta) * particle.radius * dTheta,
0.28 * Math.cos(theta + particle.zPhase) * dTheta
);
// For the selected orbit, q(v x B) points radially inward.
particle.forceDirection.set(
-Math.cos(theta),
-Math.sin(theta),
0
).normalize();
visuals.group.position.copy(particle.position);
visuals.group.rotation.y += playing ? 0.012 : 0;
visuals.group.rotation.x += playing ? 0.006 : 0;
// Camera-facing voxel charge symbol.
camera.getWorldDirection(cameraDirection);
visuals.badge.position
.copy(particle.position)
.addScaledVector(cameraDirection, -0.29);
visuals.badge.quaternion.copy(camera.quaternion);
const forceLength =
0.48 + 0.48 * fieldStrength * Math.min(particleSpeed, 1.6);
orientArrow(
forceArrow,
particle.position,
particle.forceDirection,
forceLength
);
orientArrow(
velocityArrow,
particle.position,
particle.velocity,
0.9
);
// Moving voxel trail.
for (let i = 0; i < visuals.trailCount; i++) {
const trailTheta =
theta - directionSign * (i + 1) * 0.057;
sampleParticlePosition(
particle,
trailTheta,
temporaryOrigin
);
const tangentAngle = Math.atan2(
Math.cos(trailTheta) * directionSign,
-Math.sin(trailTheta) * directionSign
);
dummy.position.copy(temporaryOrigin);
dummy.rotation.set(0, 0, tangentAngle);
const scale = 1 - i / visuals.trailCount * 0.7;
dummy.scale.setScalar(scale);
dummy.updateMatrix();
visuals.trail.setMatrixAt(i, dummy.matrix);
}
visuals.trail.instanceMatrix.needsUpdate = true;
}
function updateLabels() {
placeLabel(
labels.field,
new THREE.Vector3(-4.45, 2.7, 2.8)
);
placeLabel(
labels.boundary,
new THREE.Vector3(4.9, 3.25, -3.75)
);
placeLabel(
labels.positive,
positiveParticle.position.clone().add(new THREE.Vector3(0, 0.5, 0))
);
placeLabel(
labels.negative,
negativeParticle.position.clone().add(new THREE.Vector3(0, 0.5, 0))
);
placeLabel(
labels.positiveForce,
positiveParticle.position
.clone()
.addScaledVector(positiveParticle.forceDirection, 0.7)
);
placeLabel(
labels.negativeForce,
negativeParticle.position
.clone()
.addScaledVector(negativeParticle.forceDirection, 0.7)
);
placeLabel(
labels.positiveVelocity,
positiveParticle.position
.clone()
.addScaledVector(
positiveParticle.velocity.clone().normalize(),
0.9
)
);
placeLabel(
labels.negativeVelocity,
negativeParticle.position
.clone()
.addScaledVector(
negativeParticle.velocity.clone().normalize(),
0.9
)
);
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
// The per-particle update uses a shared clock, so advance it once here.
// A small accumulator avoids sub-frame drift when paused.
if (playing) {
const directionPositive = -positiveParticle.q * fieldSign;
const directionNegative = -negativeParticle.q * fieldSign;
const angularSpeed = 0.85 * particleSpeed * fieldStrength;
positiveParticle.theta +=
directionPositive * angularSpeed * delta;
negativeParticle.theta +=
directionNegative * angularSpeed * delta;
}
updateParticle(
positiveParticle,
positiveVisuals,
positiveForceArrow,
positiveVelocityArrow
);
updateParticle(
negativeParticle,
negativeVisuals,
negativeForceArrow,
negativeVelocityArrow
);
updateLabels();
renderer.render(scene, camera);
}
// -------------------------------------------------------------------------
// Resize
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
</script>
</body>
</html>
```