Prompt specification
Build or customize the Three.js 3D scene "Single-charge vector field stand" 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: Single-charge vector field stand
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/single-charge-vector-field-stand-x1621
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows electric fields around point charges. Build a sparse three-dimensional vector field around one point-charge proxy; show outward or inward direction by sign and keep arrow length scaling explicitly bounded. Low-poly.
Working Three.js Starter (single-charge-vector-field-stand-x1621):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Electric Field — Point Charge</title>
<style>
:root {
color-scheme: dark;
--bg: #061018;
--panel: rgba(9, 23, 32, 0.78);
--border: rgba(140, 215, 230, 0.17);
--text: #e9f7fa;
--muted: #8ba8b3;
--mint: #72efd0;
--positive: #ff6178;
--negative: #5d91ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#stage canvas {
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
#hud {
position: fixed;
top: 28px;
left: 30px;
z-index: 3;
max-width: 360px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.45);
}
.kicker {
margin-bottom: 8px;
color: var(--mint);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 48px);
font-weight: 650;
letter-spacing: -0.045em;
line-height: 1;
}
#hud p {
max-width: 330px;
margin: 13px 0 15px;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
.status {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(7, 19, 27, 0.66);
backdrop-filter: blur(12px);
color: #c9dde3;
font-size: 12px;
}
#statusDot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--positive);
box-shadow: 0 0 14px currentColor;
}
#controls {
position: fixed;
z-index: 4;
left: 28px;
bottom: 28px;
width: min(340px, calc(100vw - 56px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 24px 60px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
}
.control-heading {
margin-bottom: 8px;
color: #c9dbe1;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
margin-bottom: 17px;
}
button {
border: 1px solid var(--border);
border-radius: 10px;
padding: 10px 11px;
background: rgba(255, 255, 255, 0.035);
color: var(--muted);
font: inherit;
font-size: 13px;
font-weight: 650;
cursor: pointer;
transition:
color 160ms ease,
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
color: var(--text);
border-color: rgba(140, 215, 230, 0.38);
}
button:active {
transform: translateY(1px);
}
button[aria-pressed="true"] {
color: #07161b;
border-color: transparent;
background: var(--mint);
}
.slider-row {
margin-top: 14px;
}
.slider-label {
display: flex;
justify-content: space-between;
gap: 14px;
margin-bottom: 7px;
color: #a9c0c9;
font-size: 12px;
}
output {
color: var(--mint);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 7px 0 0;
border-radius: 999px;
accent-color: var(--mint);
cursor: pointer;
}
.equation {
margin-top: 18px;
padding: 12px 13px;
border: 1px solid rgba(114, 239, 208, 0.14);
border-radius: 11px;
background: rgba(114, 239, 208, 0.045);
}
.equation span {
display: block;
margin-bottom: 6px;
color: var(--mint);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.15em;
}
.equation b {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 16px;
font-weight: 400;
}
.equation small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 11px;
}
#hint {
position: fixed;
z-index: 3;
right: 28px;
bottom: 28px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 10px;
background: rgba(7, 19, 27, 0.58);
backdrop-filter: blur(10px);
color: var(--muted);
font-size: 11px;
line-height: 1.6;
text-align: right;
pointer-events: none;
}
#chargeLabel {
position: fixed;
z-index: 2;
display: grid;
place-items: center;
min-width: 58px;
height: 30px;
padding: 0 10px;
border: 1px solid rgba(255, 255, 255, 0.19);
border-radius: 999px;
background: rgba(5, 14, 20, 0.82);
box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35);
color: var(--positive);
font-size: 17px;
font-weight: 750;
transform: translate(-50%, -50%);
pointer-events: none;
}
@media (max-width: 700px) {
#hud {
top: 18px;
left: 18px;
max-width: calc(100vw - 36px);
}
#hud p {
max-width: 280px;
font-size: 12px;
}
#controls {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="hud">
<div class="kicker">Point charge / 3D</div>
<h1>Electric field</h1>
<p>
A sparse, low-poly vector field around one point-charge proxy.
Arrowheads show the field direction.
</p>
<div class="status">
<i id="statusDot"></i>
<span id="directionText">Outward: E points away from +q</span>
</div>
</div>
<section id="controls" aria-label="Electric field controls">
<div class="control-heading">Charge sign</div>
<div class="segmented">
<button id="positiveButton" type="button" aria-pressed="true">
+ Positive
</button>
<button id="negativeButton" type="button" aria-pressed="false">
− Negative
</button>
</div>
<div class="slider-row">
<label class="slider-label" for="magnitude">
<span>Charge magnitude</span>
<output id="magnitudeOutput">1.00 nC</output>
</label>
<input
id="magnitude"
type="range"
min="0.25"
max="2.5"
step="0.05"
value="1"
/>
</div>
<div class="slider-row">
<label class="slider-label" for="lengthCap">
<span>Maximum arrow length</span>
<output id="lengthOutput">1.40 u</output>
</label>
<input
id="lengthCap"
type="range"
min="0.70"
max="1.60"
step="0.05"
value="1.40"
/>
</div>
<div class="equation">
<span>Bounded response</span>
<b>L = L<sub>max</sub> · tanh(|E| / E<sub>ref</sub>)</b>
<small>0 ≤ L ≤ L<sub>max</sub></small>
</div>
</section>
<div id="hint">
Drag to orbit · Scroll to zoom<br />
Double-click to reset view
</div>
<div id="chargeLabel">+q</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.querySelector("#stage");
const chargeLabel = document.querySelector("#chargeLabel");
const statusDot = document.querySelector("#statusDot");
const directionText = document.querySelector("#directionText");
const positiveButton = document.querySelector("#positiveButton");
const negativeButton = document.querySelector("#negativeButton");
const magnitudeInput = document.querySelector("#magnitude");
const lengthInput = document.querySelector("#lengthCap");
const magnitudeOutput = document.querySelector("#magnitudeOutput");
const lengthOutput = document.querySelector("#lengthOutput");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x061018);
scene.fog = new THREE.FogExp2(0x061018, 0.025);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional electric field visualization"
);
stage.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
44,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, 0, 0);
const cameraState = {
radius: 13.5,
theta: 0.72,
phi: 1.04
};
const initialCamera = { ...cameraState };
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(target);
camera.lookAt(target);
}
updateCamera();
// Lighting.
scene.add(new THREE.HemisphereLight(0xb5eaff, 0x071018, 1.7));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.1);
keyLight.position.set(5, 8, 7);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x66dfff, 1.1);
rimLight.position.set(-6, 2, -5);
scene.add(rimLight);
// A restrained floor grid provides depth without becoming a solid plane.
const grid = new THREE.GridHelper(
24,
24,
0x285a6b,
0x153644
);
grid.position.y = -6.4;
grid.material.transparent = true;
grid.material.opacity = 0.24;
grid.material.depthWrite = false;
scene.add(grid);
// Low-poly equipotential shells around the single charge.
const innerRadius = 2.45;
const outerRadius = 4.55;
function createShell(radius, opacity) {
const geometry = new THREE.IcosahedronGeometry(radius, 1);
const material = new THREE.MeshBasicMaterial({
color: 0x4b8ba2,
wireframe: true,
transparent: true,
opacity,
depthWrite: false
});
const shell = new THREE.Mesh(geometry, material);
scene.add(shell);
return shell;
}
const shells = [
createShell(innerRadius, 0.12),
createShell(outerRadius, 0.075)
];
// One point-charge proxy.
const chargeGeometry = new THREE.IcosahedronGeometry(0.68, 1);
const chargeMaterial = new THREE.MeshStandardMaterial({
color: 0xff6178,
emissive: 0x3c0812,
emissiveIntensity: 0.8,
roughness: 0.34,
metalness: 0.08,
flatShading: true
});
const charge = new THREE.Mesh(chargeGeometry, chargeMaterial);
scene.add(charge);
const chargeEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(chargeGeometry),
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.52
})
);
charge.add(chargeEdges);
const halo = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.96, 1),
new THREE.MeshBasicMaterial({
color: 0xff6178,
wireframe: true,
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
scene.add(halo);
const chargeLight = new THREE.PointLight(0xff6178, 22, 11, 2);
scene.add(chargeLight);
// Fibonacci directions create a sparse but genuinely 3D distribution.
function fibonacciDirections(count) {
const directions = [];
const goldenAngle = Math.PI * (3 - Math.sqrt(5));
for (let i = 0; i < count; i++) {
const y = 1 - (i / (count - 1)) * 2;
const radial = Math.sqrt(Math.max(0, 1 - y * y));
const theta = goldenAngle * i;
directions.push(
new THREE.Vector3(
Math.cos(theta) * radial,
y,
Math.sin(theta) * radial
)
);
}
return directions;
}
const samplePoints = [];
for (const direction of fibonacciDirections(16)) {
samplePoints.push(direction.clone().multiplyScalar(innerRadius));
}
for (const direction of fibonacciDirections(26)) {
samplePoints.push(direction.clone().multiplyScalar(outerRadius));
}
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0x78efd2,
emissive: 0x073f37,
emissiveIntensity: 0.62,
roughness: 0.4,
metalness: 0.08,
transparent: true,
opacity: 0.95
});
// Five-sided shafts and four-sided heads keep the geometry intentionally low-poly.
const shaftMesh = new THREE.InstancedMesh(
new THREE.CylinderGeometry(0.027, 0.027, 1, 5, 1, false),
arrowMaterial,
samplePoints.length
);
const headMesh = new THREE.InstancedMesh(
new THREE.ConeGeometry(0.095, 1, 4, 1, false),
arrowMaterial,
samplePoints.length
);
const anchorMesh = new THREE.InstancedMesh(
new THREE.OctahedronGeometry(0.052, 0),
new THREE.MeshBasicMaterial({
color: 0xa4ffe9,
transparent: true,
opacity: 0.65
}),
samplePoints.length
);
for (const mesh of [shaftMesh, headMesh, anchorMesh]) {
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
scene.add(mesh);
}
const dummy = new THREE.Object3D();
const anchorDummy = new THREE.Object3D();
const up = new THREE.Vector3(0, 1, 0);
const coulombConstant = 8.9875517923e9;
const chargePosition = new THREE.Vector3(0, 0, 0);
let chargeSign = 1;
let chargeMagnitude = Number(magnitudeInput.value);
let maximumArrowLength = Number(lengthInput.value);
/*
E is evaluated from Coulomb's law:
E(r) = k q r / |r|^3
The displayed length is independently bounded with tanh:
L = Lmax * tanh(|E| / Eref)
Since tanh(x) is always in [0, 1), the arrow length can never
exceed the selected Lmax.
*/
const referenceField =
coulombConstant * 1e-9 / (outerRadius * outerRadius);
function updateField() {
const signedCharge = chargeSign * chargeMagnitude * 1e-9;
samplePoints.forEach((point, index) => {
const displacement = point.clone().sub(chargePosition);
const radiusSquared = displacement.lengthSq();
// Full electric field vector: its sign determines inward/outward.
const electricField = displacement.clone().multiplyScalar(
(coulombConstant * signedCharge) /
(radiusSquared * Math.sqrt(radiusSquared))
);
const fieldMagnitude = electricField.length();
const direction = electricField.normalize();
// Explicitly bounded length mapping.
const arrowLength =
maximumArrowLength *
Math.tanh(fieldMagnitude / referenceField);
const headLength = Math.min(0.29, arrowLength * 0.4);
const shaftLength = Math.max(0.0001, arrowLength - headLength);
// Shaft.
dummy.position
.copy(point)
.addScaledVector(direction, shaftLength * 0.5);
dummy.quaternion.setFromUnitVectors(up, direction);
dummy.scale.set(1, shaftLength, 1);
dummy.updateMatrix();
shaftMesh.setMatrixAt(index, dummy.matrix);
// Arrowhead: the cone points along +Y before being oriented.
dummy.position
.copy(point)
.addScaledVector(direction, shaftLength + headLength * 0.5);
dummy.quaternion.setFromUnitVectors(up, direction);
dummy.scale.set(1, headLength, 1);
dummy.updateMatrix();
headMesh.setMatrixAt(index, dummy.matrix);
// Small octahedral marker at each sample point.
anchorDummy.position.copy(point);
anchorDummy.quaternion.identity();
anchorDummy.scale.setScalar(1);
anchorDummy.updateMatrix();
anchorMesh.setMatrixAt(index, anchorDummy.matrix);
});
shaftMesh.instanceMatrix.needsUpdate = true;
headMesh.instanceMatrix.needsUpdate = true;
anchorMesh.instanceMatrix.needsUpdate = true;
}
const positiveColor = new THREE.Color(0xff6178);
const negativeColor = new THREE.Color(0x5d91ff);
function setChargeSign(sign) {
chargeSign = sign;
const positive = sign > 0;
const color = positive ? positiveColor : negativeColor;
positiveButton.setAttribute("aria-pressed", String(positive));
negativeButton.setAttribute("aria-pressed", String(!positive));
chargeMaterial.color.copy(color);
chargeMaterial.emissive.copy(color).multiplyScalar(0.18);
halo.material.color.copy(color);
chargeLight.color.copy(color);
statusDot.style.background = positive
? "var(--positive)"
: "var(--negative)";
directionText.textContent = positive
? "Outward: E points away from +q"
: "Inward: E points toward −q";
chargeLabel.textContent = positive ? "+q" : "−q";
chargeLabel.style.color = positive
? "var(--positive)"
: "var(--negative)";
updateField();
}
positiveButton.addEventListener("click", () => setChargeSign(1));
negativeButton.addEventListener("click", () => setChargeSign(-1));
magnitudeInput.addEventListener("input", () => {
chargeMagnitude = Number(magnitudeInput.value);
magnitudeOutput.textContent = `${chargeMagnitude.toFixed(2)} nC`;
updateField();
});
lengthInput.addEventListener("input", () => {
maximumArrowLength = Number(lengthInput.value);
lengthOutput.textContent = `${maximumArrowLength.toFixed(2)} u`;
updateField();
});
// Custom camera controls; no examples or addon modules are used.
let dragging = false;
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.006,
0.16,
Math.PI - 0.16
);
});
function stopDragging(event) {
dragging = false;
if (
event &&
canvas.hasPointerCapture &&
canvas.hasPointerCapture(event.pointerId)
) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.5,
21
);
},
{ passive: false }
);
function resetCamera() {
cameraState.radius = initialCamera.radius;
cameraState.theta = initialCamera.theta;
cameraState.phi = initialCamera.phi;
}
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (["INPUT", "BUTTON"].includes(document.activeElement?.tagName)) {
return;
}
if (event.key === "0") resetCamera();
if (event.key === "ArrowLeft") cameraState.theta -= 0.08;
if (event.key === "ArrowRight") cameraState.theta += 0.08;
if (event.key === "ArrowUp") {
cameraState.phi = Math.max(0.16, cameraState.phi - 0.08);
}
if (event.key === "ArrowDown") {
cameraState.phi = Math.min(Math.PI - 0.16, cameraState.phi + 0.08);
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
const projectedLabel = new THREE.Vector3();
const labelAnchor = new THREE.Vector3(0, 1.06, 0);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
// A slow orbit reveals the three-dimensional arrangement.
if (!dragging && !reducedMotion) {
cameraState.theta += delta * 0.075;
}
updateCamera();
charge.rotation.y += delta * 0.22;
charge.rotation.x += delta * 0.08;
halo.rotation.y -= delta * 0.16;
halo.rotation.z += delta * 0.09;
shells[0].rotation.y += delta * 0.035;
shells[1].rotation.y -= delta * 0.022;
shells[1].rotation.z += delta * 0.018;
projectedLabel.copy(labelAnchor).project(camera);
chargeLabel.style.left =
`${(projectedLabel.x * 0.5 + 0.5) * window.innerWidth}px`;
chargeLabel.style.top =
`${(-projectedLabel.y * 0.5 + 0.5) * window.innerHeight}px`;
chargeLabel.style.opacity =
projectedLabel.z > 1 ? "0" : "1";
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
setChargeSign(1);
animate();
</script>
</body>
</html>
```