Prompt specification
Build or customize the Three.js 3D scene "Rotating loop generator 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: Rotating loop generator model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rotating-loop-generator-model-x1637
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts an electrical generator with a rotating loop. Build a wire loop rotating in a uniform magnetic field with a flux indicator and voltage plot; calculate the sinusoidal ideal response from the changing orientation. Low-poly.
Working Three.js Starter (rotating-loop-generator-model-x1637):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rotating Loop Generator</title>
<style>
:root {
color-scheme: dark;
--bg: #07131b;
--panel: rgba(9, 22, 31, 0.82);
--panel-strong: rgba(7, 18, 26, 0.94);
--line: rgba(151, 220, 226, 0.16);
--text: #eaf8f8;
--muted: #8caab3;
--cyan: #65e7df;
--orange: #ffab5c;
--violet: #a895ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 70% 15%, rgba(40, 115, 125, 0.22), transparent 36%),
linear-gradient(145deg, #091923, #040b11 72%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.panel {
border: 1px solid var(--line);
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.intro {
position: fixed;
z-index: 5;
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 20px 21px 18px;
border-radius: 16px;
pointer-events: none;
}
.eyebrow,
.panel-title,
.stat-label,
.control-label,
.plot-label {
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
animation: pulse 1.8s ease-in-out infinite;
}
.live-dot.paused {
background: var(--orange);
box-shadow: 0 0 12px var(--orange);
animation: none;
}
@keyframes pulse {
50% {
opacity: 0.45;
transform: scale(0.75);
}
}
h1 {
margin: 10px 0 7px;
font-size: clamp(25px, 3vw, 37px);
line-height: 1;
letter-spacing: -0.045em;
font-weight: 720;
}
.intro p {
margin: 0;
max-width: 340px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.intro-footer {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-top: 15px;
}
.chip {
padding: 6px 8px;
border: 1px solid var(--line);
border-radius: 6px;
color: #bad0d5;
background: rgba(255, 255, 255, 0.025);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.chip.copper {
color: var(--orange);
border-color: rgba(255, 171, 92, 0.22);
}
.chip.field {
color: var(--cyan);
border-color: rgba(101, 231, 223, 0.22);
}
.controls {
position: fixed;
z-index: 6;
top: 20px;
right: 20px;
width: 278px;
padding: 17px;
border-radius: 15px;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 15px;
}
.panel-title {
color: #cce4e6;
}
.badge {
padding: 5px 7px;
border-radius: 5px;
background: rgba(101, 231, 223, 0.08);
color: var(--cyan);
font: 750 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.control + .control {
margin-top: 15px;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
letter-spacing: 0.11em;
}
.control-value {
color: var(--text);
font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
accent-color: var(--cyan);
cursor: pointer;
}
#fieldSlider {
accent-color: var(--orange);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
button {
min-height: 36px;
padding: 8px 10px;
border: 1px solid rgba(126, 210, 215, 0.18);
border-radius: 8px;
color: #d7ebed;
background: rgba(101, 231, 223, 0.055);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(101, 231, 223, 0.12);
border-color: rgba(101, 231, 223, 0.34);
}
button:active {
transform: translateY(1px);
}
button.secondary {
color: var(--muted);
background: rgba(255, 255, 255, 0.025);
}
.camera-hint {
margin-top: 11px;
color: #6f8d96;
font: 600 9px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
text-align: center;
}
.flux-card {
position: fixed;
z-index: 5;
left: 20px;
bottom: 20px;
width: 292px;
padding: 17px;
border-radius: 15px;
pointer-events: none;
}
.gauge-wrap {
position: relative;
height: 116px;
margin-top: 8px;
}
.gauge-wrap svg {
width: 100%;
height: 100%;
overflow: visible;
}
.gauge-number {
position: absolute;
left: 50%;
top: 54px;
transform: translateX(-50%);
text-align: center;
pointer-events: none;
}
.gauge-number strong {
display: block;
font: 720 19px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.04em;
}
.gauge-number span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
}
.formula {
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 8px;
color: #c8dcdf;
background: rgba(0, 0, 0, 0.13);
font: 600 11px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: center;
}
.formula em {
color: var(--cyan);
font-style: normal;
}
.mini-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 11px;
}
.mini-stat {
padding: 8px 7px;
border-radius: 7px;
background: rgba(255, 255, 255, 0.025);
}
.stat-label {
display: block;
color: #6f8d96;
font-size: 8px;
letter-spacing: 0.1em;
}
.stat-value {
display: block;
margin-top: 5px;
color: #dceef0;
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.plot-card {
position: fixed;
z-index: 5;
right: 20px;
bottom: 20px;
width: 392px;
padding: 17px;
border-radius: 15px;
pointer-events: none;
}
.voltage-main {
display: flex;
align-items: flex-end;
justify-content: space-between;
margin: -2px 0 5px;
}
.voltage-value {
font: 730 31px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.06em;
}
.voltage-value small {
margin-left: 4px;
color: var(--muted);
font-size: 13px;
letter-spacing: 0;
}
.phase-readout {
color: var(--orange);
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
#voltagePlot {
display: block;
width: 100%;
height: 155px;
margin-top: 5px;
border-radius: 8px;
background: rgba(1, 8, 12, 0.38);
}
.plot-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
}
.plot-stats {
display: flex;
gap: 15px;
}
.plot-stats > div {
min-width: 58px;
}
.plot-stats strong {
display: block;
margin-top: 4px;
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.legend-line {
display: inline-block;
width: 17px;
height: 2px;
margin-right: 6px;
vertical-align: middle;
background: var(--cyan);
box-shadow: 0 0 8px rgba(101, 231, 223, 0.65);
}
.world-label {
position: fixed;
z-index: 3;
transform: translate(-50%, -50%);
padding: 5px 7px;
border: 1px solid rgba(101, 231, 223, 0.2);
border-radius: 5px;
color: var(--cyan);
background: rgba(4, 15, 21, 0.76);
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.22);
pointer-events: none;
font: 750 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
white-space: nowrap;
}
.world-label.copper {
color: var(--orange);
border-color: rgba(255, 171, 92, 0.22);
}
@media (max-width: 780px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 15px;
}
.intro p {
display: none;
}
.intro-footer {
margin-top: 10px;
}
.controls {
top: 112px;
right: 12px;
width: 220px;
padding: 13px;
}
.flux-card {
left: 12px;
bottom: 12px;
width: 222px;
padding: 13px;
}
.plot-card {
right: 12px;
bottom: 12px;
width: calc(100vw - 258px);
min-width: 150px;
padding: 13px;
}
.gauge-wrap {
height: 94px;
}
.gauge-number {
top: 43px;
}
.gauge-number strong {
font-size: 15px;
}
.mini-stats {
grid-template-columns: 1fr 1fr;
}
.mini-stat:last-child {
display: none;
}
.voltage-value {
font-size: 23px;
}
#voltagePlot {
height: 118px;
}
.plot-footer {
display: block;
}
.plot-stats {
gap: 9px;
}
.world-label {
display: none;
}
}
@media (max-width: 520px) {
.controls {
width: 196px;
}
.flux-card {
width: 185px;
}
.plot-card {
width: calc(100vw - 221px);
}
.formula,
.mini-stats,
.plot-footer,
.camera-hint {
display: none;
}
.voltage-value {
font-size: 19px;
}
#voltagePlot {
height: 100px;
}
}
@media (max-height: 650px) {
.intro p,
.intro-footer,
.camera-hint {
display: none;
}
.gauge-wrap {
height: 85px;
}
.gauge-number {
top: 37px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="intro panel">
<div class="eyebrow">
<span id="liveDot" class="live-dot"></span>
Low-poly physics / Generator 01
</div>
<h1>AC Generator</h1>
<p>
A single copper loop rotates through a uniform magnetic field.
Its changing orientation changes the magnetic flux, producing a
sinusoidal induced voltage.
</p>
<div class="intro-footer">
<span class="chip copper">1 TURN</span>
<span class="chip field">UNIFORM B FIELD</span>
<span class="chip">IDEAL CONDUCTOR</span>
</div>
</section>
<section class="controls panel">
<div class="panel-head">
<span class="panel-title">Field controls</span>
<span class="badge">LIVE</span>
</div>
<div class="control">
<label class="control-label" for="speedSlider">
Rotation rate
<span id="speedValue" class="control-value">1.20 rev/s</span>
</label>
<input id="speedSlider" type="range" min="0.25" max="2.5" step="0.05" value="1.2" />
</div>
<div class="control">
<label class="control-label" for="fieldSlider">
Flux density
<span id="fieldValue" class="control-value">0.80 T</span>
</label>
<input id="fieldSlider" type="range" min="0.2" max="1.4" step="0.05" value="0.8" />
</div>
<div class="buttons">
<button id="pauseButton" type="button">Pause</button>
<button id="zeroButton" type="button">Zero phase</button>
<button id="viewButton" class="secondary" type="button">Reset view</button>
<button id="fullscreenButton" class="secondary" type="button">Fullscreen</button>
</div>
<div class="camera-hint">DRAG TO ORBIT · SCROLL TO ZOOM</div>
</section>
<section class="flux-card panel">
<div class="panel-head">
<span class="panel-title">Flux indicator</span>
<span class="badge">SIGNED</span>
</div>
<div class="gauge-wrap">
<svg viewBox="0 0 180 112" aria-label="Magnetic flux gauge">
<path
d="M 20 90 A 70 70 0 0 1 160 90"
fill="none"
stroke="rgba(140,170,179,.22)"
stroke-width="8"
stroke-linecap="round"
/>
<path
d="M 20 90 A 70 70 0 0 1 160 90"
fill="none"
stroke="rgba(101,231,223,.18)"
stroke-width="8"
stroke-linecap="round"
pathLength="1"
stroke-dasharray="0.5 1"
/>
<g stroke="rgba(180,215,220,.35)" stroke-width="1">
<line x1="20" y1="90" x2="27" y2="90" />
<line x1="55" y1="38" x2="59" y2="45" />
<line x1="90" y1="20" x2="90" y2="29" />
<line x1="125" y1="38" x2="121" y2="45" />
<line x1="160" y1="90" x2="153" y2="90" />
</g>
<g id="fluxNeedle" transform="rotate(0 90 90)">
<line x1="90" y1="90" x2="90" y2="28" stroke="#65e7df" stroke-width="3" stroke-linecap="round" />
<circle cx="90" cy="90" r="6" fill="#07131b" stroke="#65e7df" stroke-width="2" />
</g>
<text x="15" y="106" fill="#78939b" font-size="8">−Φmax</text>
<text x="84" y="106" fill="#78939b" font-size="8">0</text>
<text x="143" y="106" fill="#78939b" font-size="8">+Φmax</text>
</svg>
<div class="gauge-number">
<strong id="fluxValue">+0.0264 Wb</strong>
<span>Φ(t)</span>
</div>
</div>
<div class="formula">
Φ = <em>N B A cos θ</em><br />
ε = −dΦ/dt = <em>N B A ω sin θ</em>
</div>
<div class="mini-stats">
<div class="mini-stat">
<span class="stat-label">Angle</span>
<span id="angleValue" class="stat-value">0°</span>
</div>
<div class="mini-stat">
<span class="stat-label">Flux max</span>
<span id="fluxMaxValue" class="stat-value">0.0264 Wb</span>
</div>
<div class="mini-stat">
<span class="stat-label">Area</span>
<span class="stat-value">0.033 m²</span>
</div>
</div>
</section>
<section class="plot-card panel">
<div class="panel-head">
<span class="panel-title">Induced voltage</span>
<span class="badge">ε(t)</span>
</div>
<div class="voltage-main">
<div class="voltage-value"><span id="voltageValue">+0.000</span><small>V</small></div>
<div id="phaseValue" class="phase-readout">θ = 0°</div>
</div>
<canvas id="voltagePlot" aria-label="Live sinusoidal voltage plot"></canvas>
<div class="plot-footer">
<div class="plot-stats">
<div>
<span class="stat-label">Peak</span>
<strong id="peakValue">0.199 V</strong>
</div>
<div>
<span class="stat-label">RMS</span>
<strong id="rmsValue">0.141 V</strong>
</div>
<div>
<span class="stat-label">Power</span>
<strong id="powerValue">0.00 mW</strong>
</div>
</div>
<span class="plot-label"><span class="legend-line"></span>3 s window</span>
</div>
</section>
<div id="fieldLabel" class="world-label">B = 0.80 T · +Z</div>
<div id="coilLabel" class="world-label copper">COPPER LOOP · 1 TURN</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM references
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const speedSlider = document.getElementById("speedSlider");
const fieldSlider = document.getElementById("fieldSlider");
const speedValue = document.getElementById("speedValue");
const fieldValue = document.getElementById("fieldValue");
const pauseButton = document.getElementById("pauseButton");
const zeroButton = document.getElementById("zeroButton");
const viewButton = document.getElementById("viewButton");
const fullscreenButton = document.getElementById("fullscreenButton");
const liveDot = document.getElementById("liveDot");
const fluxNeedle = document.getElementById("fluxNeedle");
const fluxValue = document.getElementById("fluxValue");
const fluxMaxValue = document.getElementById("fluxMaxValue");
const angleValue = document.getElementById("angleValue");
const voltageValue = document.getElementById("voltageValue");
const phaseValue = document.getElementById("phaseValue");
const peakValue = document.getElementById("peakValue");
const rmsValue = document.getElementById("rmsValue");
const powerValue = document.getElementById("powerValue");
const fieldLabel = document.getElementById("fieldLabel");
const coilLabel = document.getElementById("coilLabel");
const plotCanvas = document.getElementById("voltagePlot");
const plotContext = plotCanvas.getContext("2d");
// -------------------------------------------------------------------------
// Physical constants and state
// -------------------------------------------------------------------------
const TURNS = 1;
const COIL_WIDTH_METERS = 0.22;
const COIL_HEIGHT_METERS = 0.15;
const COIL_AREA = COIL_WIDTH_METERS * COIL_HEIGHT_METERS;
const LOAD_RESISTANCE = 12;
let fieldStrength = Number(fieldSlider.value);
let rotationFrequency = Number(speedSlider.value);
let angularVelocity = Math.PI * 2 * rotationFrequency;
let angle = 0;
let simulationTime = 0;
let running = true;
// -------------------------------------------------------------------------
// Renderer, scene and camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x07131b, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07131b, 0.035);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraControl = {
target: new THREE.Vector3(0, 0.55, 0),
azimuth: 0.72,
polar: 1.12,
radius: 12.8,
desiredAzimuth: 0.72,
desiredPolar: 1.12,
desiredRadius: 12.8
};
function updateCamera(dt = 1) {
const damping = 1 - Math.exp(-dt * 8);
cameraControl.azimuth +=
(cameraControl.desiredAzimuth - cameraControl.azimuth) * damping;
cameraControl.polar +=
(cameraControl.desiredPolar - cameraControl.polar) * damping;
cameraControl.radius +=
(cameraControl.desiredRadius - cameraControl.radius) * damping;
const sinPolar = Math.sin(cameraControl.polar);
const cosPolar = Math.cos(cameraControl.polar);
camera.position.set(
cameraControl.target.x +
cameraControl.radius * sinPolar * Math.sin(cameraControl.azimuth),
cameraControl.target.y + cameraControl.radius * cosPolar,
cameraControl.target.z +
cameraControl.radius * sinPolar * Math.cos(cameraControl.azimuth)
);
camera.lookAt(cameraControl.target);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa6e9ef, 0x15212a, 2.15));
const keyLight = new THREE.DirectionalLight(0xffefd7, 3.25);
keyLight.position.set(6, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x65e7df, 1.5);
rimLight.position.set(-7, 4, -8);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9c50, 16, 10, 2);
warmLight.position.set(4.5, 2.8, 3.5);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0x0a1820,
roughness: 0.96,
metalness: 0.05,
flatShading: true
}),
base: new THREE.MeshStandardMaterial({
color: 0x263f49,
roughness: 0.62,
metalness: 0.45,
flatShading: true
}),
baseDark: new THREE.MeshStandardMaterial({
color: 0x152a34,
roughness: 0.68,
metalness: 0.5,
flatShading: true
}),
steel: new THREE.MeshStandardMaterial({
color: 0x9cb2bb,
roughness: 0.34,
metalness: 0.76,
flatShading: true
}),
steelDark: new THREE.MeshStandardMaterial({
color: 0x435b65,
roughness: 0.44,
metalness: 0.68,
flatShading: true
}),
copper: new THREE.MeshStandardMaterial({
color: 0xff9f43,
roughness: 0.3,
metalness: 0.68,
flatShading: true
}),
copperDark: new THREE.MeshStandardMaterial({
color: 0xa64f24,
roughness: 0.42,
metalness: 0.7,
flatShading: true
}),
poleFront: new THREE.MeshStandardMaterial({
color: 0x279b91,
roughness: 0.46,
metalness: 0.48,
flatShading: true
}),
poleBack: new THREE.MeshStandardMaterial({
color: 0x365c80,
roughness: 0.5,
metalness: 0.5,
flatShading: true
}),
field: new THREE.MeshBasicMaterial({
color: 0x65e7df,
transparent: true,
opacity: 0.48,
depthWrite: false
}),
fieldSoft: new THREE.MeshBasicMaterial({
color: 0x65e7df,
transparent: true,
opacity: 0.22,
depthWrite: false
})
};
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addEdges(mesh, color = 0xffffff, opacity = 0.2) {
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
edge.renderOrder = 2;
mesh.add(edge);
return edge;
}
function addCylinder(
parent,
radius,
length,
position,
material,
radialSegments = 8,
axis = "y"
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
),
material
);
mesh.position.copy(position);
if (axis === "x") mesh.rotation.z = Math.PI / 2;
if (axis === "z") mesh.rotation.x = Math.PI / 2;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Stage and base
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.CircleGeometry(24, 64),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.76;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x416d76, 0x28444e);
grid.position.y = -1.745;
grid.material.transparent = true;
grid.material.opacity = 0.19;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(5.25, 5.45, 0.22, 12),
materials.baseDark
);
platform.position.y = -1.64;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const base = addBox(
scene,
new THREE.Vector3(7.3, 0.4, 3.75),
new THREE.Vector3(0, -1.27, 0),
materials.base
);
addEdges(base, 0x8fc6cb, 0.14);
addBox(
scene,
new THREE.Vector3(5.9, 0.12, 2.45),
new THREE.Vector3(0, -1.01, 0),
materials.baseDark
);
for (const x of [-3.0, 3.0]) {
for (const z of [-1.4, 1.4]) {
const foot = addBox(
scene,
new THREE.Vector3(0.7, 0.35, 0.7),
new THREE.Vector3(x, -1.57, z),
materials.steelDark
);
addEdges(foot, 0xa9d2d6, 0.1);
}
}
for (const x of [-3.1, 3.1]) {
for (const z of [-1.5, 1.5]) {
addCylinder(
scene,
0.11,
0.08,
new THREE.Vector3(x, -1.015, z),
materials.steel,
8,
"y"
);
}
}
// Bearing supports
for (const x of [-2.35, 2.35]) {
const post = addBox(
scene,
new THREE.Vector3(0.58, 2.08, 0.84),
new THREE.Vector3(x, 0.02, 0),
materials.base
);
addEdges(post, 0x87bcc2, 0.12);
const bearing = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.12, 6, 14),
materials.steelDark
);
bearing.position.set(x, 1.28, 0);
bearing.rotation.y = Math.PI / 2;
bearing.castShadow = true;
scene.add(bearing);
addCylinder(
scene,
0.17,
0.52,
new THREE.Vector3(x, 1.28, 0),
materials.steel,
10,
"x"
);
}
// -------------------------------------------------------------------------
// Magnetic pole frames
// -------------------------------------------------------------------------
const magnetCenterY = 1.28;
const outerWidth = 5.2;
const outerHeight = 4.1;
const innerWidth = 3.35;
const innerHeight = 2.55;
const frameDepth = 0.38;
const sideWidth = (outerWidth - innerWidth) / 2;
const topHeight = (outerHeight - innerHeight) / 2;
function buildPoleFrame(z, material) {
const group = new THREE.Group();
group.position.z = z;
scene.add(group);
const topBottom = addBox(
group,
new THREE.Vector3(outerWidth, topHeight, frameDepth),
new THREE.Vector3(
0,
magnetCenterY + innerHeight / 2 + topHeight / 2,
0
),
material
);
addEdges(topBottom, 0xb9ffff, 0.13);
const bottom = addBox(
group,
new THREE.Vector3(outerWidth, topHeight, frameDepth),
new THREE.Vector3(
0,
magnetCenterY - innerHeight / 2 - topHeight / 2,
0
),
material
);
addEdges(bottom, 0xb9ffff, 0.13);
for (const side of [-1, 1]) {
const sideBar = addBox(
group,
new THREE.Vector3(sideWidth, innerHeight, frameDepth),
new THREE.Vector3(
side * (innerWidth / 2 + sideWidth / 2),
magnetCenterY,
0
),
material
);
addEdges(sideBar, 0xb9ffff, 0.13);
}
return group;
}
buildPoleFrame(-1.88, materials.poleBack);
buildPoleFrame(1.88, materials.poleFront);
// Top and bottom magnetic yoke pieces joining the two poles.
for (const direction of [-1, 1]) {
const yoke = addBox(
scene,
new THREE.Vector3(outerWidth, 0.42, 3.98),
new THREE.Vector3(
0,
magnetCenterY + direction * (innerHeight / 2 + topHeight / 2),
0
),
direction > 0 ? materials.poleFront : materials.poleBack
);
addEdges(yoke, 0xb9ffff, 0.11);
}
// Pole identifiers as low-poly blocks.
for (const [z, material] of [
[-2.09, materials.poleBack],
[2.09, materials.poleFront]
]) {
for (const x of [-2.28, 2.28]) {
const poleCap = addBox(
scene,
new THREE.Vector3(0.46, 0.46, 0.16),
new THREE.Vector3(x, magnetCenterY + 1.67, z),
material
);
addEdges(poleCap, 0xd9ffff, 0.12);
}
}
// -------------------------------------------------------------------------
// Uniform magnetic field arrows, pointing along +Z
// -------------------------------------------------------------------------
function buildFieldArrow(x, y, opacityScale = 1) {
const arrow = new THREE.Group();
arrow.position.set(x, y, 0);
scene.add(arrow);
const shaftMaterial = materials.field.clone();
shaftMaterial.opacity *= opacityScale;
const coneMaterial = materials.field.clone();
coneMaterial.opacity *= opacityScale;
const shaft = addCylinder(
arrow,
0.022,
2.75,
new THREE.Vector3(0, 0, 0),
shaftMaterial,
6,
"z"
);
shaft.castShadow = false;
const cone = new THREE.Mesh(
new THREE.ConeGeometry(0.105, 0.32, 6),
coneMaterial
);
cone.position.z = 1.53;
cone.rotation.x = Math.PI / 2;
arrow.add(cone);
}
for (const x of [-1.03, 0, 1.03]) {
for (const y of [0.48, 2.08]) {
buildFieldArrow(x, y, x === 0 ? 1 : 0.72);
}
}
// Faint field lines behind the coil.
for (const x of [-1.03, 0, 1.03]) {
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, 1.28, -1.62),
new THREE.Vector3(x, 1.28, 1.62)
]),
new THREE.LineDashedMaterial({
color: 0x65e7df,
transparent: true,
opacity: 0.13,
dashSize: 0.12,
gapSize: 0.12
})
);
line.computeLineDistances();
scene.add(line);
}
// -------------------------------------------------------------------------
// Rotating copper loop and axle
// -------------------------------------------------------------------------
const coilGroup = new THREE.Group();
coilGroup.position.y = magnetCenterY;
scene.add(coilGroup);
const halfWidth = 1.45;
const halfHeight = 0.95;
const wireThickness = 0.14;
const wireMeshes = [];
const topWire = addBox(
coilGroup,
new THREE.Vector3(halfWidth * 2, wireThickness, 0.16),
new THREE.Vector3(0, halfHeight, 0),
materials.copper
);
wireMeshes.push(topWire);
const bottomWire = addBox(
coilGroup,
new THREE.Vector3(halfWidth * 2, wireThickness, 0.16),
new THREE.Vector3(0, -halfHeight, 0),
materials.copper
);
wireMeshes.push(bottomWire);
for (const side of [-1, 1]) {
const sideWire = addBox(
coilGroup,
new THREE.Vector3(wireThickness, halfHeight * 2, 0.16),
new THREE.Vector3(side * (halfWidth - wireThickness / 2), 0, 0),
materials.copper
);
wireMeshes.push(sideWire);
}
for (const mesh of wireMeshes) {
addEdges(mesh, 0xffd39b, 0.34);
}
// Low-poly corner joints.
for (const x of [-halfWidth + 0.06, halfWidth - 0.06]) {
for (const y of [-halfHeight + 0.06, halfHeight - 0.06]) {
const joint = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105, 0),
materials.copperDark
);
joint.position.set(x, y, 0);
joint.castShadow = true;
coilGroup.add(joint);
}
}
const axle = addCylinder(
coilGroup,
0.1,
6.35,
new THREE.Vector3(0, 0, 0),
materials.steel,
10,
"x"
);
addEdges(axle, 0xd9f6f7, 0.12);
for (const x of [-1.75, 1.75]) {
addCylinder(
coilGroup,
0.16,
0.16,
new THREE.Vector3(x, 0, 0),
materials.copperDark,
8,
"x"
);
}
// Slip ring and brush assembly on the left.
const slipRing = new THREE.Mesh(
new THREE.TorusGeometry(0.28, 0.065, 6, 16),
materials.copper
);
slipRing.position.set(-3.04, 0, 0);
slipRing.rotation.y = Math.PI / 2;
slipRing.castShadow = true;
coilGroup.add(slipRing);
const brush = addBox(
scene,
new THREE.Vector3(0.55, 0.28, 0.42),
new THREE.Vector3(-3.27, magnetCenterY, 0),
materials.baseDark
);
addEdges(brush, 0x9bd1d4, 0.14);
// Terminal block on the right.
const terminalBase = addBox(
scene,
new THREE.Vector3(0.75, 0.48, 1.35),
new THREE.Vector3(3.36, -0.72, 0),
materials.baseDark
);
addEdges(terminalBase, 0x9bd1d4, 0.12);
for (const z of [-0.38, 0.38]) {
addCylinder(
scene,
0.1,
0.34,
new THREE.Vector3(3.55, -0.38, z),
materials.copper,
8,
"y"
);
}
// -------------------------------------------------------------------------
// 3D flux indicator: translucent swept area and rotating area normal
// -------------------------------------------------------------------------
const fluxAreaMaterial = new THREE.MeshBasicMaterial({
color: 0x65e7df,
transparent: true,
opacity: 0.07,
side: THREE.DoubleSide,
depthWrite: false
});
const fluxArea = new THREE.Mesh(
new THREE.PlaneGeometry(2.62, 1.73),
fluxAreaMaterial
);
fluxArea.position.z = 0.02;
fluxArea.renderOrder = 1;
coilGroup.add(fluxArea);
const fluxAreaBorder = new THREE.LineSegments(
new THREE.EdgesGeometry(fluxArea.geometry),
new THREE.LineBasicMaterial({
color: 0x65e7df,
transparent: true,
opacity: 0.32,
depthWrite: false
})
);
fluxAreaBorder.position.z = 0.025;
fluxAreaBorder.renderOrder = 2;
coilGroup.add(fluxAreaBorder);
const normalMaterial = new THREE.MeshBasicMaterial({
color: 0x65e7df,
transparent: true,
opacity: 0.82,
depthWrite: false
});
const normalShaft = addCylinder(
coilGroup,
0.035,
0.95,
new THREE.Vector3(0, 0, 0.5),
normalMaterial,
6,
"z"
);
normalShaft.castShadow = false;
const normalHead = new THREE.Mesh(
new THREE.ConeGeometry(0.12, 0.31, 6),
normalMaterial
);
normalHead.position.z = 1.12;
normalHead.rotation.x = Math.PI / 2;
normalHead.renderOrder = 4;
coilGroup.add(normalHead);
// -------------------------------------------------------------------------
// Physics helpers
// -------------------------------------------------------------------------
function signed(value, digits = 3) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(digits)}`;
}
function currentPhysics() {
const flux = TURNS * fieldStrength * COIL_AREA * Math.cos(angle);
const voltage =
TURNS *
fieldStrength *
COIL_AREA *
angularVelocity *
Math.sin(angle);
return { flux, voltage };
}
function updatePeakReadouts() {
const peak =
TURNS * fieldStrength * COIL_AREA * angularVelocity;
const maxFlux = TURNS * fieldStrength * COIL_AREA;
peakValue.textContent = `${peak.toFixed(3)} V`;
rmsValue.textContent = `${(peak / Math.SQRT2).toFixed(3)} V`;
fluxMaxValue.textContent = `${maxFlux.toFixed(4)} Wb`;
speedValue.textContent = `${rotationFrequency.toFixed(2)} rev/s`;
fieldValue.textContent = `${fieldStrength.toFixed(2)} T`;
fieldLabel.textContent = `B = ${fieldStrength.toFixed(2)} T · +Z`;
}
function updatePhysicsUI() {
const { flux, voltage } = currentPhysics();
const degrees = ((THREE.MathUtils.radToDeg(angle) % 360) + 360) % 360;
const normalizedFlux = Math.cos(angle);
fluxNeedle.setAttribute(
"transform",
`rotate(${normalizedFlux * 90} 90 90)`
);
fluxValue.textContent = `${signed(flux, 4)} Wb`;
angleValue.textContent = `${Math.round(degrees)}°`;
voltageValue.textContent = signed(voltage, 3);
phaseValue.textContent = `θ = ${Math.round(degrees)}°`;
const power = (voltage * voltage) / LOAD_RESISTANCE;
powerValue.textContent =
power >= 0.001
? `${(power * 1000).toFixed(2)} mW`
: `${(power * 1e6).toFixed(1)} µW`;
const fluxColor =
normalizedFlux >= 0 ? 0x65e7df : 0xa895ff;
fluxAreaMaterial.color.setHex(fluxColor);
fluxAreaBorder.material.color.setHex(fluxColor);
normalMaterial.color.setHex(fluxColor);
fluxAreaMaterial.opacity = 0.025 + Math.abs(normalizedFlux) * 0.075;
fluxAreaBorder.material.opacity = 0.15 + Math.abs(normalizedFlux) * 0.22;
}
// -------------------------------------------------------------------------
// Voltage plot
// -------------------------------------------------------------------------
function preparePlotCanvas() {
const rect = plotCanvas.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio || 1, 2);
const width = Math.max(1, Math.round(rect.width));
const height = Math.max(1, Math.round(rect.height));
if (
plotCanvas.width !== Math.round(width * dpr) ||
plotCanvas.height !== Math.round(height * dpr)
) {
plotCanvas.width = Math.round(width * dpr);
plotCanvas.height = Math.round(height * dpr);
}
plotContext.setTransform(dpr, 0, 0, dpr, 0, 0);
return { width, height };
}
function drawVoltagePlot() {
const { width, height } = preparePlotCanvas();
const ctx = plotContext;
ctx.clearRect(0, 0, width, height);
const margin = {
left: width < 230 ? 29 : 38,
right: 9,
top: 12,
bottom: 24
};
const plotWidth = width - margin.left - margin.right;
const plotHeight = height - margin.top - margin.bottom;
if (plotWidth <= 0 || plotHeight <= 0) return;
const peak =
TURNS * fieldStrength * COIL_AREA * angularVelocity;
const scaleMax = Math.max(0.05, peak * 1.25);
const centerY = margin.top + plotHeight / 2;
const toY = (voltage) =>
centerY - (voltage / scaleMax) * (plotHeight / 2);
// Grid.
ctx.lineWidth = 1;
ctx.strokeStyle = "rgba(131, 180, 188, 0.10)";
ctx.beginPath();
for (let i = 0; i <= 6; i++) {
const x = margin.left + (plotWidth * i) / 6;
ctx.moveTo(x, margin.top);
ctx.lineTo(x, margin.top + plotHeight);
}
for (let i = 0; i <= 4; i++) {
const y = margin.top + (plotHeight * i) / 4;
ctx.moveTo(margin.left, y);
ctx.lineTo(margin.left + plotWidth, y);
}
ctx.stroke();
// Zero axis.
ctx.strokeStyle = "rgba(157, 207, 212, 0.27)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(margin.left, centerY);
ctx.lineTo(margin.left + plotWidth, centerY);
ctx.stroke();
// Y-axis labels.
ctx.fillStyle = "#718f98";
ctx.font = "9px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
ctx.fillText(
scaleMax.toFixed(2),
margin.left - 5,
margin.top + 4
);
ctx.fillText("0", margin.left - 5, centerY);
ctx.fillText(
`−${scaleMax.toFixed(2)}`,
margin.left - 5,
margin.top + plotHeight - 4
);
// Time labels.
ctx.textBaseline = "top";
ctx.textAlign = "left";
ctx.fillText("−3 s", margin.left, margin.top + plotHeight + 7);
ctx.textAlign = "center";
ctx.fillText(
"−1.5 s",
margin.left + plotWidth / 2,
margin.top + plotHeight + 7
);
ctx.textAlign = "right";
ctx.fillText("NOW", margin.left + plotWidth, margin.top + plotHeight + 7);
// Analytical rolling three-second waveform.
const startTime = simulationTime - 3;
const samples = Math.max(90, Math.round(plotWidth));
ctx.beginPath();
for (let i = 0; i <= samples; i++) {
const fraction = i / samples;
const t = startTime + fraction * 3;
const phaseAtTime = angle - angularVelocity * (simulationTime - t);
const voltage =
TURNS *
fieldStrength *
COIL_AREA *
angularVelocity *
Math.sin(phaseAtTime);
const x = margin.left + fraction * plotWidth;
const y = toY(voltage);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
ctx.strokeStyle = "#65e7df";
ctx.lineWidth = 2;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.shadowColor = "rgba(101, 231, 223, 0.48)";
ctx.shadowBlur = 7;
ctx.stroke();
ctx.shadowBlur = 0;
// Current-value marker.
const currentVoltage = currentPhysics().voltage;
const markerY = toY(currentVoltage);
ctx.fillStyle = "#ffab5c";
ctx.beginPath();
ctx.arc(
margin.left + plotWidth,
markerY,
3.4,
0,
Math.PI * 2
);
ctx.fill();
ctx.strokeStyle = "rgba(255, 171, 92, 0.45)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(margin.left + plotWidth, markerY);
ctx.lineTo(margin.left + plotWidth - 7, markerY);
ctx.stroke();
}
// -------------------------------------------------------------------------
// Custom camera controls — no external control addon
// -------------------------------------------------------------------------
let pointerActive = false;
let previousPointerX = 0;
let previousPointerY = 0;
let activePointerId = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (!event.isPrimary) return;
pointerActive = true;
activePointerId = event.pointerId;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive || event.pointerId !== activePointerId) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraControl.desiredAzimuth -= dx * 0.006;
cameraControl.desiredPolar = THREE.MathUtils.clamp(
cameraControl.desiredPolar - dy * 0.005,
0.42,
1.43
);
});
function releasePointer(event) {
if (event.pointerId !== activePointerId) return;
pointerActive = false;
activePointerId = null;
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.style.cursor = "grab";
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraControl.desiredRadius = THREE.MathUtils.clamp(
cameraControl.desiredRadius + event.deltaY * 0.008,
7.2,
19
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCameraView);
function resetCameraView() {
cameraControl.desiredAzimuth = 0.72;
cameraControl.desiredPolar = 1.12;
cameraControl.desiredRadius = 12.8;
}
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
speedSlider.addEventListener("input", () => {
rotationFrequency = Number(speedSlider.value);
angularVelocity = Math.PI * 2 * rotationFrequency;
updatePeakReadouts();
});
fieldSlider.addEventListener("input", () => {
fieldStrength = Number(fieldSlider.value);
updatePeakReadouts();
});
pauseButton.addEventListener("click", () => {
running = !running;
pauseButton.textContent = running ? "Pause" : "Resume";
liveDot.classList.toggle("paused", !running);
});
zeroButton.addEventListener("click", () => {
angle = 0;
simulationTime = 0;
updatePhysicsUI();
});
viewButton.addEventListener("click", resetCameraView);
fullscreenButton.addEventListener("click", async () => {
try {
if (!document.fullscreenElement) {
await document.documentElement.requestFullscreen();
} else {
await document.exitFullscreen();
}
} catch (error) {
console.warn("Fullscreen request was not available.", error);
}
});
// -------------------------------------------------------------------------
// Projected world labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
const fieldLabelPosition = new THREE.Vector3(0, 4.05, 0);
const coilLabelPosition = new THREE.Vector3(0, -0.02, 0);
function positionWorldLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
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`;
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
let uiAccumulator = 0;
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (running) {
simulationTime += dt;
angle = (angle + angularVelocity * dt) % (Math.PI * 2);
}
coilGroup.rotation.x = angle;
updateCamera(dt);
positionWorldLabel(fieldLabel, fieldLabelPosition);
positionWorldLabel(coilLabel, coilLabelPosition);
uiAccumulator += dt;
if (uiAccumulator >= 1 / 30) {
updatePhysicsUI();
uiAccumulator = 0;
}
drawVoltagePlot();
renderer.render(scene, camera);
}
updatePeakReadouts();
updatePhysicsUI();
requestAnimationFrame(animate);
</script>
</body>
</html>
```