Prompt specification
Build or customize the Three.js 3D scene "Virtual motor commutation 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: Virtual motor commutation exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/virtual-motor-commutation-exhibit-x1655
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring motor torque on a current-carrying loop. Compose a simplified loop, rotating shaft, and symbolic commutator display; reverse current at the appropriate phase so torque does not unrealistically remain constant without commutation. Voxel style.
Working Three.js Starter (virtual-motor-commutation-exhibit-x1655):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel DC Motor — Split-Ring Commutator</title>
<style>
:root {
--bg: #07111d;
--panel: rgba(9, 21, 35, 0.92);
--line: #29465d;
--text: #d9f3ff;
--muted: #83a7bb;
--cyan: #58dcff;
--green: #73f29a;
--amber: #ffc857;
--red: #ff647c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 18px;
left: 18px;
width: min(460px, calc(100vw - 390px));
pointer-events: none;
text-shadow: 2px 2px 0 #07111d;
}
.kicker {
display: inline-block;
margin-bottom: 8px;
padding: 5px 8px;
border: 2px solid var(--cyan);
background: rgba(7, 17, 29, 0.82);
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
}
h1 {
margin: 0;
font-size: clamp(24px, 3.2vw, 42px);
line-height: 0.98;
letter-spacing: -0.05em;
}
.brand p {
max-width: 430px;
margin: 12px 0 0;
color: #b4d5e5;
font-size: 13px;
line-height: 1.45;
}
.panel {
position: fixed;
top: 18px;
right: 18px;
width: 340px;
max-height: calc(100vh - 36px);
overflow: auto;
padding: 15px;
border: 2px solid var(--line);
background: var(--panel);
box-shadow: 8px 8px 0 rgba(0, 0, 0, 0.32);
backdrop-filter: blur(10px);
scrollbar-width: thin;
}
.panel h2 {
margin: 0 0 12px;
color: var(--cyan);
font-size: 13px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.formula {
padding: 10px;
border-left: 4px solid var(--green);
background: #0b1c2c;
color: #dfffe9;
font-size: 14px;
line-height: 1.55;
}
.formula strong {
color: var(--green);
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 10px;
}
.readout {
min-height: 57px;
padding: 8px;
border: 1px solid #264156;
background: #091827;
}
.readout span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout b {
color: var(--text);
font-size: 16px;
}
.status {
margin-top: 9px;
padding: 8px 10px;
border: 1px solid #31536a;
background: #0b2030;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.07em;
}
.status.flash {
color: #101820;
background: var(--amber);
border-color: #fff1b0;
}
.chart-wrap {
margin-top: 12px;
border: 1px solid #29465d;
background: #06121e;
}
#phaseChart {
display: block;
width: 100%;
height: 124px;
image-rendering: pixelated;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 12px;
padding: 7px 9px;
border-top: 1px solid #213b4f;
color: var(--muted);
font-size: 9px;
}
.legend i {
display: inline-block;
width: 9px;
height: 9px;
margin-right: 4px;
vertical-align: -1px;
}
.controls {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.control {
margin-bottom: 12px;
}
.control label {
display: flex;
justify-content: space-between;
margin-bottom: 6px;
color: #b7d4e3;
font-size: 11px;
}
.control output {
color: var(--cyan);
}
input[type="range"] {
width: 100%;
height: 6px;
appearance: none;
border-radius: 0;
background: #1c3547;
outline: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 12px;
height: 18px;
appearance: none;
border: 2px solid #07111d;
background: var(--cyan);
cursor: ew-resize;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 18px;
border: 2px solid #07111d;
border-radius: 0;
background: var(--cyan);
cursor: ew-resize;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 38px;
border: 2px solid #35566d;
border-radius: 0;
background: #10283b;
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
button:hover {
border-color: var(--cyan);
background: #15364d;
}
button.primary {
border-color: var(--green);
color: var(--green);
}
.note {
margin: 12px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.scene-label {
position: fixed;
z-index: 5;
padding: 5px 7px;
border: 1px solid currentColor;
background: rgba(5, 14, 23, 0.84);
color: var(--text);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.03em;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.scene-label.cyan {
color: var(--cyan);
}
.scene-label.green {
color: var(--green);
}
.scene-label.amber {
color: var(--amber);
}
.hint {
position: fixed;
left: 18px;
bottom: 17px;
padding: 7px 9px;
border: 1px solid #29465d;
background: rgba(6, 17, 29, 0.82);
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
#loader {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #07111d;
color: var(--cyan);
font-size: 13px;
letter-spacing: 0.12em;
}
#loader.hidden {
display: none;
}
@media (max-width: 760px) {
.brand {
width: calc(100vw - 36px);
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
width: min(340px, calc(100vw - 20px));
max-height: 52vh;
}
.hint {
display: none;
}
.scene-label {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="brand">
<div class="kicker">VOXEL MOTOR LAB / DC</div>
<h1>Torque survives<br>the dead center.</h1>
<p>
A single-turn copper loop rotates in a fixed magnetic field. At each
brush gap, the split-ring commutator reverses current so the torque keeps
its driving direction instead of incorrectly remaining constant.
</p>
</header>
<aside class="panel">
<h2>Commutator phase monitor</h2>
<div class="formula">
τ<sub>y</sub> = I·A·B·cos θ<br>
Split ring reverses <strong>I</strong> at 90° and 270°.<br>
Effective drive torque: <strong>τ ∝ |cos θ|</strong>
</div>
<div class="readouts">
<div class="readout">
<span>Rotor phase</span>
<b id="phaseValue">0.0°</b>
</div>
<div class="readout">
<span>Coil current</span>
<b id="currentValue">+I</b>
</div>
<div class="readout">
<span>Torque</span>
<b id="torqueValue">0.00 N·m</b>
</div>
<div class="readout">
<span>Reversals</span>
<b id="commCount">0</b>
</div>
</div>
<div id="commStatus" class="status">COMMUTATOR CLOSED · TORQUE DRIVING</div>
<div class="chart-wrap">
<canvas id="phaseChart" width="640" height="248"></canvas>
<div class="legend">
<span><i style="background:#73f29a"></i>with commutation</span>
<span><i style="background:#ff647c"></i>without commutation</span>
<span><i style="background:#ffc857"></i>current sign</span>
</div>
</div>
<div class="controls">
<div class="control">
<label for="fieldRange">
Magnetic field strength
<output id="fieldOutput">1.00 T</output>
</label>
<input id="fieldRange" type="range" min="0.35" max="1.8" step="0.05" value="1">
</div>
<div class="control">
<label for="loadRange">
Mechanical load / damping
<output id="loadOutput">0.85</output>
</label>
<input id="loadRange" type="range" min="0.25" max="1.65" step="0.05" value="0.85">
</div>
<div class="buttons">
<button id="pauseButton" class="primary" type="button">Pause motion</button>
<button id="resetButton" type="button">Reset motor</button>
</div>
</div>
<p class="note">
The rotor and split ring rotate together. The two carbon brushes and DC
source remain fixed. Amber voxel markers show conventional current;
they reverse at every commutation event.
</p>
</aside>
<div id="label-field" class="scene-label cyan">FIXED FIELD B → +X</div>
<div id="label-coil" class="scene-label amber">SINGLE-TURN COPPER LOOP</div>
<div id="label-commutator" class="scene-label amber">SPLIT-RING COMMUTATOR</div>
<div id="label-brush" class="scene-label">FIXED CARBON BRUSH</div>
<div id="label-torque" class="scene-label green">TORQUE +Y</div>
<div id="label-source" class="scene-label cyan">DC SOURCE</div>
<div class="hint">
Drag: orbit · Wheel / pinch: zoom · Double-click: reset camera
</div>
<div id="loader">ASSEMBLING VOXEL MOTOR...</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const loader = document.getElementById("loader");
const TWO_PI = Math.PI * 2;
// ---------------------------------------------------------------------
// Renderer, scene, camera
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 15, 30);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
viewport.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xa5ddff, 0x101724, 1.65);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(6, 10, 7);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 10;
key.shadow.camera.bottom = -10;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 30;
scene.add(key);
const cyanLight = new THREE.PointLight(0x2ac8ff, 18, 12, 2);
cyanLight.position.set(-3, 2, 3);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xff9a45, 15, 11, 2);
warmLight.position.set(4, -1, -2);
scene.add(warmLight);
// ---------------------------------------------------------------------
// Materials and voxel construction helpers
// ---------------------------------------------------------------------
const materials = {
base: new THREE.MeshStandardMaterial({
color: 0x0b1a28,
roughness: 0.9,
metalness: 0.15
}),
pedestal: new THREE.MeshStandardMaterial({
color: 0x172d3f,
roughness: 0.65,
metalness: 0.35
}),
dark: new THREE.MeshStandardMaterial({
color: 0x071019,
roughness: 0.7,
metalness: 0.35
}),
steel: new THREE.MeshStandardMaterial({
color: 0x9fb8c7,
roughness: 0.3,
metalness: 0.8
}),
copper: new THREE.MeshStandardMaterial({
color: 0xd87932,
emissive: 0x4b1604,
emissiveIntensity: 0.35,
roughness: 0.28,
metalness: 0.72
}),
copperDark: new THREE.MeshStandardMaterial({
color: 0x8e431f,
roughness: 0.4,
metalness: 0.68
}),
commA: new THREE.MeshStandardMaterial({
color: 0xff8b38,
emissive: 0xff4c08,
emissiveIntensity: 0.38,
roughness: 0.3,
metalness: 0.62
}),
commB: new THREE.MeshStandardMaterial({
color: 0xffd16b,
emissive: 0xa95b08,
emissiveIntensity: 0.3,
roughness: 0.3,
metalness: 0.62
}),
magnetN: new THREE.MeshStandardMaterial({
color: 0xec4864,
emissive: 0x5c0716,
emissiveIntensity: 0.25,
roughness: 0.58,
metalness: 0.18
}),
magnetS: new THREE.MeshStandardMaterial({
color: 0x3189e8,
emissive: 0x072b61,
emissiveIntensity: 0.25,
roughness: 0.58,
metalness: 0.18
}),
carbon: new THREE.MeshStandardMaterial({
color: 0x202a31,
roughness: 0.95,
metalness: 0.08
}),
field: new THREE.MeshStandardMaterial({
color: 0x58dcff,
emissive: 0x1495c4,
emissiveIntensity: 1.15,
transparent: true,
opacity: 0.82,
roughness: 0.4
}),
torque: new THREE.MeshStandardMaterial({
color: 0x73f29a,
emissive: 0x1aa34b,
emissiveIntensity: 1.15,
roughness: 0.4
}),
current: new THREE.MeshStandardMaterial({
color: 0xffd36a,
emissive: 0xff8b0a,
emissiveIntensity: 1.7,
roughness: 0.35
}),
positive: new THREE.MeshStandardMaterial({
color: 0xff596d,
emissive: 0x8a1020,
emissiveIntensity: 0.55,
roughness: 0.5
}),
negative: new THREE.MeshStandardMaterial({
color: 0x43bfff,
emissive: 0x075d96,
emissiveIntensity: 0.55,
roughness: 0.5
})
};
function outline(mesh, color = 0x06101a, opacity = 0.5) {
const edgeGeometry = new THREE.EdgesGeometry(mesh.geometry);
const edgeMaterial = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
const edges = new THREE.LineSegments(edgeGeometry, edgeMaterial);
mesh.add(edges);
return mesh;
}
function box(
width,
height,
depth,
x,
y,
z,
material,
parent = scene,
addEdges = false
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (addEdges) outline(mesh);
return mesh;
}
function beamBetween(
startArray,
endArray,
width,
depth,
material,
parent = scene
) {
const start = new THREE.Vector3(...startArray);
const end = new THREE.Vector3(...endArray);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, length, depth),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeBlockArrow(length, material, axis = "x") {
const group = new THREE.Group();
if (axis === "x") {
box(length * 0.72, 0.11, 0.11, length * 0.14, 0, 0, material, group);
box(0.28, 0.27, 0.27, length * 0.52, 0, 0, material, group);
box(0.19, 0.19, 0.19, length * 0.69, 0, 0, material, group);
box(0.11, 0.11, 0.11, length * 0.82, 0, 0, material, group);
} else {
box(0.11, length * 0.72, 0.11, 0, length * 0.14, 0, material, group);
box(0.27, 0.28, 0.27, 0, length * 0.52, 0, material, group);
box(0.19, 0.19, 0.19, 0, length * 0.69, 0, material, group);
box(0.11, 0.11, 0.11, 0, length * 0.82, 0, material, group);
}
return group;
}
// ---------------------------------------------------------------------
// Platform and permanent magnets
// ---------------------------------------------------------------------
box(13, 0.36, 9.2, 0, -3.0, 0, materials.base, scene, true);
box(3.6, 0.28, 3.6, 0, -2.66, 0, materials.pedestal, scene, true);
const grid = new THREE.GridHelper(12, 24, 0x355169, 0x1b3042);
grid.position.y = -2.81;
grid.material.transparent = true;
grid.material.opacity = 0.6;
scene.add(grid);
for (const x of [-5.7, 5.7]) {
for (const z of [-3.8, 3.8]) {
box(0.55, 0.55, 0.55, x, -2.55, z, materials.steel, scene, true);
}
}
// North pole on +X, south pole on -X: B points toward +X.
box(1.8, 3.5, 2.8, 4.15, 0, 0, materials.magnetN, scene, true);
box(0.52, 2.85, 2.35, 3.0, 0, 0, materials.magnetN, scene, true);
box(1.8, 3.5, 2.8, -4.15, 0, 0, materials.magnetS, scene, true);
box(0.52, 2.85, 2.35, -3.0, 0, 0, materials.magnetS, scene, true);
for (const x of [-1.9, 0, 1.9]) {
const arrow = makeBlockArrow(0.95, materials.field, "x");
arrow.position.set(x - 0.45, 1.95, 0);
scene.add(arrow);
}
// ---------------------------------------------------------------------
// Rotor: shaft, copper loop, supports, split-ring commutator
// ---------------------------------------------------------------------
const rotor = new THREE.Group();
scene.add(rotor);
const coilHalfWidth = 1.45;
const coilHalfHeight = 1.2;
const coilCenterY = 0.18;
const coilTop = coilCenterY + coilHalfHeight;
const coilBottom = coilCenterY - coilHalfHeight;
const commutatorY = -2.03;
const commutatorRadius = 0.72;
// Square voxel shaft.
box(0.34, 5.75, 0.34, 0, 0.05, 0, materials.steel, rotor, true);
box(0.52, 0.22, 0.52, 0, 2.91, 0, materials.steel, rotor, true);
box(0.52, 0.22, 0.52, 0, -2.78, 0, materials.dark, rotor, true);
// Simplified rectangular conducting loop.
box(
coilHalfWidth * 2 + 0.18,
0.19,
0.19,
0,
coilTop,
0,
materials.copper,
rotor,
true
);
box(
coilHalfWidth * 2 + 0.18,
0.19,
0.19,
0,
coilBottom,
0,
materials.copper,
rotor,
true
);
for (const x of [-coilHalfWidth, coilHalfWidth]) {
box(
0.19,
coilHalfHeight * 2,
0.19,
x,
coilCenterY,
0,
materials.copper,
rotor,
true
);
}
// Insulated corner blocks.
for (const x of [-coilHalfWidth, coilHalfWidth]) {
for (const y of [coilTop, coilBottom]) {
box(0.29, 0.29, 0.29, x, y, 0, materials.copperDark, rotor, true);
}
}
// Non-conducting support arms.
for (const y of [coilTop + 0.18, coilBottom - 0.18]) {
for (const side of [-1, 1]) {
box(
coilHalfWidth - 0.18,
0.12,
0.14,
side * (coilHalfWidth * 0.5 + 0.12),
y,
0,
materials.dark,
rotor,
true
);
}
}
// Split ring made from two electrically isolated half-rings.
const segmentCount = 40;
for (let i = 0; i < segmentCount; i++) {
const phi = (i / segmentCount) * TWO_PI;
const cosine = Math.cos(phi);
// Physical brush gaps at the two split-ring boundaries.
if (Math.abs(cosine) < 0.2) continue;
const segment = box(
0.21,
0.58,
0.13,
Math.cos(phi) * commutatorRadius,
commutatorY,
Math.sin(phi) * commutatorRadius,
cosine >= 0 ? materials.commA : materials.commB,
rotor,
true
);
// Local X is radial; rotate it toward the segment's radial direction.
segment.rotation.y = -phi;
}
// Leads from the two coil ends to the two rotating commutator halves.
beamBetween(
[coilHalfWidth, coilBottom, 0],
[commutatorRadius, commutatorY, 0],
0.10,
0.10,
materials.copper,
rotor
);
beamBetween(
[-coilHalfWidth, coilBottom, 0],
[-commutatorRadius, commutatorY, 0],
0.10,
0.10,
materials.copperDark,
rotor
);
// Fixed carbon brushes touch opposite sides of the rotating split ring.
for (const side of [-1, 1]) {
box(
0.58,
0.72,
0.76,
side * 1.22,
commutatorY,
0,
materials.dark,
scene,
true
);
box(
0.23,
0.40,
0.43,
side * 0.79,
commutatorY,
0,
materials.carbon,
scene,
true
);
box(
0.10,
0.52,
0.58,
side * 1.51,
commutatorY,
0,
side > 0 ? materials.positive : materials.negative,
scene,
true
);
}
// Dynamic arrow next to the right-hand active side of the loop.
const currentArrow = makeBlockArrow(0.62, materials.current, "y");
currentArrow.position.set(
coilHalfWidth + 0.28,
coilCenterY - 0.13,
0.19
);
rotor.add(currentArrow);
// ---------------------------------------------------------------------
// Fixed DC source and blocky circuit leads
// ---------------------------------------------------------------------
box(1.9, 1.15, 1.05, 3.0, -2.12, -3.0, materials.dark, scene, true);
box(1.55, 0.18, 0.82, 3.0, -1.49, -3.0, materials.steel, scene, true);
box(0.28, 0.24, 0.28, 2.65, -1.34, -3.0, materials.positive, scene, true);
box(0.28, 0.24, 0.28, 3.35, -1.34, -3.0, materials.negative, scene, true);
// A plus sign on the positive terminal face.
box(0.34, 0.09, 0.06, 2.65, -1.94, -2.46, materials.positive, scene);
box(0.09, 0.34, 0.06, 2.65, -1.94, -2.46, materials.positive, scene);
// A minus sign on the negative terminal face.
box(0.34, 0.09, 0.06, 3.35, -1.94, -2.46, materials.negative, scene);
const positiveCable = [
[1.55, commutatorY, 0],
[2.12, commutatorY, 0],
[2.12, commutatorY, -1.58],
[2.65, commutatorY, -1.58],
[2.65, -1.34, -1.58],
[2.65, -1.34, -2.86]
];
const negativeCable = [
[-1.55, commutatorY, 0],
[-2.15, commutatorY, 0],
[-2.15, commutatorY, -3.48],
[3.35, commutatorY, -3.48],
[3.35, -1.34, -3.48],
[3.35, -1.34, -2.86]
];
for (let i = 0; i < positiveCable.length - 1; i++) {
beamBetween(
positiveCable[i],
positiveCable[i + 1],
0.09,
0.09,
materials.positive
);
}
for (let i = 0; i < negativeCable.length - 1; i++) {
beamBetween(
negativeCable[i],
negativeCable[i + 1],
0.09,
0.09,
materials.negative
);
}
// ---------------------------------------------------------------------
// Torque vector: blocky right-hand arc around the +Y shaft axis
// ---------------------------------------------------------------------
const torqueRadius = 2.02;
const torqueY = 0.55;
for (let i = 0; i < 15; i++) {
const angle = 0.18 + i * 0.145;
box(
0.18,
0.12,
0.26,
Math.cos(angle) * torqueRadius,
torqueY,
-Math.sin(angle) * torqueRadius,
materials.torque,
scene,
true
);
}
const torqueArrow = makeBlockArrow(0.56, materials.torque, "x");
const torqueEndAngle = 0.18 + 15 * 0.145;
const torqueTangent = new THREE.Vector3(
-Math.sin(torqueEndAngle),
0,
-Math.cos(torqueEndAngle)
).normalize();
torqueArrow.position.set(
Math.cos(torqueEndAngle) * torqueRadius,
torqueY,
-Math.sin(torqueEndAngle) * torqueRadius
);
torqueArrow.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
torqueTangent
);
scene.add(torqueArrow);
// ---------------------------------------------------------------------
// Current markers traveling around the rectangular loop
// ---------------------------------------------------------------------
const markerCount = 18;
const currentMarkers = [];
for (let i = 0; i < markerCount; i++) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.15, 0.15),
materials.current
);
marker.castShadow = true;
rotor.add(marker);
currentMarkers.push(marker);
}
const loopTop = coilTop;
const loopBottom = coilBottom;
const loopLength =
4 * coilHalfWidth + 4 * coilHalfHeight;
function pointOnLoop(t, target) {
t = ((t % 1) + 1) % 1;
let distance = t * loopLength;
// Top: +X to -X.
if (distance < coilHalfWidth * 2) {
const u = distance / (coilHalfWidth * 2);
target.set(
coilHalfWidth - 2 * coilHalfWidth * u,
loopTop,
0.14
);
return;
}
distance -= coilHalfWidth * 2;
// Left: top to bottom.
if (distance < coilHalfHeight * 2) {
const u = distance / (coilHalfHeight * 2);
target.set(
-coilHalfWidth,
loopTop - 2 * coilHalfHeight * u,
0.14
);
return;
}
distance -= coilHalfHeight * 2;
// Bottom: -X to +X.
if (distance < coilHalfWidth * 2) {
const u = distance / (coilHalfWidth * 2);
target.set(
-coilHalfWidth + 2 * coilHalfWidth * u,
loopBottom,
0.14
);
return;
}
distance -= coilHalfWidth * 2;
// Right: bottom to top.
const u = distance / (coilHalfHeight * 2);
target.set(
coilHalfWidth,
loopBottom + 2 * coilHalfHeight * u,
0.14
);
}
// ---------------------------------------------------------------------
// Physics and commutation
//
// Local coil normal is +Z. After a rotation theta about +Y:
// n = (sin(theta), 0, cos(theta))
// With B along +X:
// tau_y = I * A * B * cos(theta)
//
// The split ring reverses I at theta = 90 degrees and 270 degrees.
// Thus the ideal commuted torque becomes positive and pulses to zero:
// tau_y = I * A * B * |cos(theta)|
// ---------------------------------------------------------------------
let theta = 0.12;
let omega = 0.55;
let fieldStrength = 1;
let loadDamping = 0.85;
let currentSign = 1;
let commutationCount = 0;
let commutationFlash = 0;
let currentTravel = 0;
let paused = false;
const torqueConstant = 2.55;
const rotorInertia = 2.35;
function updatePhysics(dt) {
// Each interval of width pi corresponds to one half revolution.
// A brush gap is encountered at theta = pi/2 + k*pi.
const commutationSector = Math.floor(
(theta + Math.PI / 2) / Math.PI
);
const nextSign = (commutationSector & 1) ? -1 : 1;
if (nextSign !== currentSign) {
currentSign = nextSign;
commutationCount++;
commutationFlash = 1;
}
// Before commutation this expression changes sign every half turn.
// The physical split ring changes currentSign at the same instant.
const rawTorque =
currentSign *
Math.cos(theta) *
torqueConstant *
fieldStrength;
const drag =
loadDamping * omega +
0.075 * Math.tanh(omega * 8);
omega += ((rawTorque - drag) / rotorInertia) * dt;
omega = THREE.MathUtils.clamp(omega, -2.5, 3.2);
theta += omega * dt;
// Current markers reverse direction when the current reverses.
currentTravel += currentSign * dt * 0.23;
}
// ---------------------------------------------------------------------
// Camera controls implemented directly, without an addon
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, -0.12, 0);
const spherical = new THREE.Spherical(13.4, 1.08, 0.56);
const pointerMap = new Map();
let dragPrevious = null;
let previousPinchDistance = 0;
const canvas = renderer.domElement;
function pointerDistance() {
const points = [...pointerMap.values()];
if (points.length < 2) return 0;
return Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
pointerMap.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.setPointerCapture(event.pointerId);
if (pointerMap.size === 1) {
dragPrevious = {
x: event.clientX,
y: event.clientY
};
} else if (pointerMap.size === 2) {
previousPinchDistance = pointerDistance();
dragPrevious = null;
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerMap.has(event.pointerId)) return;
pointerMap.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointerMap.size === 1 && dragPrevious) {
const dx = event.clientX - dragPrevious.x;
const dy = event.clientY - dragPrevious.y;
spherical.theta -= dx * 0.006;
spherical.phi -= dy * 0.005;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi,
0.28,
1.48
);
dragPrevious = {
x: event.clientX,
y: event.clientY
};
} else if (pointerMap.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
spherical.radius *= previousPinchDistance / distance;
spherical.radius = THREE.MathUtils.clamp(
spherical.radius,
8,
21
);
}
previousPinchDistance = distance;
}
});
function endPointer(event) {
pointerMap.delete(event.pointerId);
dragPrevious = pointerMap.size === 1
? [...pointerMap.values()][0]
: null;
previousPinchDistance = pointerDistance();
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("lostpointercapture", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
spherical.radius *= Math.exp(event.deltaY * 0.001);
spherical.radius = THREE.MathUtils.clamp(
spherical.radius,
8,
21
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
spherical.radius = 13.4;
spherical.phi = 1.08;
spherical.theta = 0.56;
});
function updateCamera() {
camera.position.setFromSpherical(spherical);
camera.position.add(cameraTarget);
camera.lookAt(cameraTarget);
}
// ---------------------------------------------------------------------
// Projected HTML labels
// ---------------------------------------------------------------------
const projected = new THREE.Vector3();
const localLabelPoint = new THREE.Vector3();
const labelDefinitions = [
{
element: document.getElementById("label-field"),
position: new THREE.Vector3(-3.15, 2.45, 0)
},
{
element: document.getElementById("label-coil"),
object: rotor,
local: new THREE.Vector3(0, 1.82, 0)
},
{
element: document.getElementById("label-commutator"),
position: new THREE.Vector3(0.35, -2.25, 0)
},
{
element: document.getElementById("label-brush"),
position: new THREE.Vector3(-1.45, -2.32, 0.1)
},
{
element: document.getElementById("label-torque"),
position: new THREE.Vector3(2.05, 0.72, 0)
},
{
element: document.getElementById("label-source"),
position: new THREE.Vector3(3.0, -1.05, -3.0)
}
];
function updateLabels() {
for (const definition of labelDefinitions) {
if (definition.object) {
localLabelPoint.copy(definition.local);
definition.object.localToWorld(localLabelPoint);
projected.copy(localLabelPoint);
} else {
projected.copy(definition.position);
}
projected.project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
definition.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
definition.element.style.left = `${x}px`;
definition.element.style.top = `${y}px`;
}
}
// ---------------------------------------------------------------------
// Phase chart
// ---------------------------------------------------------------------
const chart = document.getElementById("phaseChart");
const chartContext = chart.getContext("2d");
chartContext.setTransform(2, 0, 0, 2, 0, 0);
function drawPhaseChart() {
const ctx = chartContext;
const width = 320;
const height = 124;
ctx.clearRect(0, 0, width, height);
// Grid.
ctx.strokeStyle = "#173044";
ctx.lineWidth = 1;
for (let x = 24; x <= 308; x += 35.5) {
ctx.beginPath();
ctx.moveTo(x, 8);
ctx.lineTo(x, 76);
ctx.stroke();
}
ctx.strokeStyle = "#36536a";
ctx.beginPath();
ctx.moveTo(24, 42);
ctx.lineTo(308, 42);
ctx.stroke();
ctx.fillStyle = "#668ba0";
ctx.font = "8px monospace";
ctx.fillText("TORQUE", 3, 13);
ctx.fillText("+", 9, 29);
ctx.fillText("0", 12, 45);
ctx.fillText("−", 9, 66);
// Torque without commutation: reverses every half revolution.
ctx.strokeStyle = "#ff647c";
ctx.lineWidth = 1.5;
ctx.setLineDash([4, 3]);
ctx.beginPath();
for (let px = 24; px <= 308; px++) {
const phase = ((px - 24) / 284) * TWO_PI;
const py = 42 - Math.cos(phase) * 28;
if (px === 24) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
ctx.stroke();
ctx.setLineDash([]);
// Torque with the physically necessary split-ring commutation.
ctx.strokeStyle = "#73f29a";
ctx.lineWidth = 2.2;
ctx.beginPath();
for (let px = 24; px <= 308; px++) {
const phase = ((px - 24) / 284) * TWO_PI;
const py = 42 - Math.abs(Math.cos(phase)) * 28;
if (px === 24) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
ctx.stroke();
// Current sign track.
ctx.strokeStyle = "#ffc857";
ctx.lineWidth = 1.4;
ctx.beginPath();
for (let px = 24; px <= 308; px++) {
const phase = ((px - 24) / 284) * TWO_PI;
const py = Math.cos(phase) >= 0 ? 91 : 110;
if (px === 24) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
ctx.stroke();
ctx.fillStyle = "#668ba0";
ctx.fillText("CURRENT", 3, 91);
ctx.fillText("0°", 22, 121);
ctx.fillText("90°", 93, 121);
ctx.fillText("180°", 166, 121);
ctx.fillText("270°", 239, 121);
// Current rotor phase marker.
const normalizedPhase =
((theta % TWO_PI) + TWO_PI) % TWO_PI;
const markerX = 24 + (normalizedPhase / TWO_PI) * 284;
ctx.strokeStyle = "#d9f3ff";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(markerX, 8);
ctx.lineTo(markerX, 113);
ctx.stroke();
ctx.fillStyle = "#d9f3ff";
ctx.fillRect(markerX - 3, 8, 6, 4);
}
// ---------------------------------------------------------------------
// UI controls and readouts
// ---------------------------------------------------------------------
const phaseValue = document.getElementById("phaseValue");
const currentValue = document.getElementById("currentValue");
const torqueValue = document.getElementById("torqueValue");
const commCountValue = document.getElementById("commCount");
const commStatus = document.getElementById("commStatus");
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const fieldRange = document.getElementById("fieldRange");
const loadRange = document.getElementById("loadRange");
const fieldOutput = document.getElementById("fieldOutput");
const loadOutput = document.getElementById("loadOutput");
fieldRange.addEventListener("input", () => {
fieldStrength = Number(fieldRange.value);
fieldOutput.textContent = `${fieldStrength.toFixed(2)} T`;
materials.field.emissiveIntensity = 0.65 + fieldStrength * 0.55;
});
loadRange.addEventListener("input", () => {
loadDamping = Number(loadRange.value);
loadOutput.textContent = loadDamping.toFixed(2);
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume motion" : "Pause motion";
});
resetButton.addEventListener("click", () => {
theta = 0.12;
omega = 0.55;
currentSign = 1;
commutationCount = 0;
commutationFlash = 0;
currentTravel = 0;
paused = false;
pauseButton.textContent = "Pause motion";
});
function updateReadouts() {
const degrees =
((THREE.MathUtils.radToDeg(theta) % 360) + 360) % 360;
const torque =
currentSign *
Math.cos(theta) *
torqueConstant *
fieldStrength;
phaseValue.textContent = `${degrees.toFixed(1)}°`;
currentValue.textContent = currentSign > 0 ? "+I" : "−I";
torqueValue.textContent = `${Math.abs(torque).toFixed(2)} N·m`;
commCountValue.textContent = String(commutationCount);
if (commutationFlash > 0.55) {
commStatus.className = "status flash";
commStatus.textContent =
"BRUSH GAP CROSSING · CURRENT REVERSING";
} else {
commStatus.className = "status";
commStatus.textContent =
"COMMUTATOR CLOSED · TORQUE DRIVING";
}
}
// ---------------------------------------------------------------------
// Main loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const markerPoint = new THREE.Vector3();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
updatePhysics(dt);
}
commutationFlash = Math.max(
0,
commutationFlash - dt * 1.55
);
rotor.rotation.y = theta;
// Reverse the visible arrow along with the conventional current.
currentArrow.rotation.z = currentSign > 0 ? 0 : Math.PI;
for (let i = 0; i < currentMarkers.length; i++) {
const base = i / currentMarkers.length;
pointOnLoop(base + currentTravel, markerPoint);
currentMarkers[i].position.copy(markerPoint);
currentMarkers[i].rotation.y = theta * 0.45;
}
const torqueMagnitude =
Math.abs(currentSign * Math.cos(theta)) * fieldStrength;
materials.torque.emissiveIntensity =
0.55 + torqueMagnitude * 0.75 + commutationFlash * 0.5;
materials.commA.emissiveIntensity =
0.22 + fieldStrength * 0.22 + commutationFlash * 1.4;
materials.commB.emissiveIntensity =
0.18 + fieldStrength * 0.20 + commutationFlash * 1.4;
materials.current.emissiveIntensity =
1.35 + commutationFlash * 1.3;
updateCamera();
rotor.updateMatrixWorld(true);
updateLabels();
updateReadouts();
drawPhaseChart();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loader.classList.add("hidden");
window.__sceneReady = true;
}
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.setSize(innerWidth, innerHeight);
});
animate();
</script>
<script>
setTimeout(() => {
if (!window.__sceneReady) {
const loader = document.getElementById("loader");
if (loader && !loader.classList.contains("hidden")) {
loader.textContent =
"Unable to initialize the scene. Check WebGL and the Three.js CDN connection.";
}
}
}, 10000);
</script>
</body>
</html>
```