Prompt specification
Build or customize the Three.js 3D scene "Motor energy-flow teaching tool" 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: Motor energy-flow teaching tool
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/motor-energy-flow-teaching-tool-x1656
Scene Description Prompt:
Allow changing idealized current and load torque in a bounded rotational model; show electrical input, mechanical output, and modeled losses without claiming a real motor design. Use Three.js from cdnjs in a single HTML file, with low-poly forms.
Working Three.js Starter (motor-energy-flow-teaching-tool-x1656):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bounded Rotational Model</title>
<style>
:root {
color-scheme: dark;
--bg: #071119;
--panel: rgba(10, 23, 33, 0.88);
--panel-border: rgba(165, 206, 224, 0.16);
--text: #edf6fa;
--muted: #91a8b4;
--cyan: #58dfd0;
--orange: #ffb45c;
--blue: #73a8ff;
--red: #ff786f;
--green: #7bea9b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
background:
radial-gradient(circle at 58% 38%, #17313c 0%, #091720 42%, #050c12 100%);
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#viewport canvas:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: -4px;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 20px 55px rgba(0, 0, 0, 0.34),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
.controls {
top: 20px;
left: 20px;
width: 342px;
padding: 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 13px currentColor;
content: "";
}
h1 {
margin: 0;
font-size: 25px;
font-weight: 720;
letter-spacing: -0.035em;
}
.intro {
margin: 8px 0 20px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.control {
margin: 16px 0;
}
.control-header {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.control label {
color: #dce9ef;
font-size: 12px;
font-weight: 650;
}
output {
color: var(--cyan);
font-variant-numeric: tabular-nums;
font-size: 14px;
font-weight: 760;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
appearance: none;
-webkit-appearance: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--fill, 50%),
#263945 var(--fill, 50%),
#263945 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #10232d;
border-radius: 50%;
appearance: none;
-webkit-appearance: none;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(88, 223, 208, 0.35);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #10232d;
border-radius: 50%;
background: var(--cyan);
}
#loadSlider {
background: linear-gradient(
90deg,
var(--orange) 0%,
var(--orange) var(--fill, 50%),
#263945 var(--fill, 50%),
#263945 100%
);
}
#loadSlider::-webkit-slider-thumb {
background: var(--orange);
box-shadow: 0 0 0 1px rgba(255, 180, 92, 0.35);
}
#loadSlider::-moz-range-thumb {
background: var(--orange);
}
.range-limits {
display: flex;
justify-content: space-between;
margin-top: 6px;
color: #718995;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.divider {
height: 1px;
margin: 18px 0;
background: rgba(180, 215, 228, 0.1);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
color: #c8d8df;
font-size: 11px;
font-weight: 650;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 10px rgba(123, 234, 155, 0.65);
}
.status[data-mode="stalled"] .status-dot {
background: var(--red);
box-shadow: 0 0 10px rgba(255, 120, 111, 0.65);
}
.status[data-mode="accelerating"] .status-dot,
.status[data-mode="starting"] .status-dot {
background: var(--orange);
box-shadow: 0 0 10px rgba(255, 180, 92, 0.65);
}
.small-stat {
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 15px;
}
.stat {
min-width: 0;
padding: 10px 9px;
border: 1px solid rgba(170, 210, 225, 0.09);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.stat-label {
display: block;
margin-bottom: 5px;
color: #7f98a5;
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.07em;
}
.stat-value {
display: block;
overflow: hidden;
color: #e9f3f7;
font-size: 13px;
font-weight: 720;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 35px;
border: 1px solid rgba(159, 203, 218, 0.15);
border-radius: 9px;
color: #dce9ef;
background: rgba(255, 255, 255, 0.045);
cursor: pointer;
font-size: 11px;
font-weight: 680;
transition:
border-color 120ms ease,
background 120ms ease,
transform 120ms ease;
}
button:hover {
border-color: rgba(88, 223, 208, 0.45);
background: rgba(88, 223, 208, 0.08);
}
button:active {
transform: translateY(1px);
}
.metrics {
top: 20px;
right: 20px;
width: 342px;
padding: 18px;
}
.metric {
margin-top: 14px;
}
.metric-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.metric-name {
color: #c4d5dd;
font-size: 11px;
font-weight: 650;
}
.metric-value {
color: var(--text);
font-size: 25px;
font-weight: 760;
letter-spacing: -0.035em;
font-variant-numeric: tabular-nums;
}
.metric.input .metric-value {
color: var(--orange);
}
.metric.output .metric-value {
color: var(--cyan);
}
.metric.loss .metric-value {
color: var(--blue);
}
.bar {
height: 4px;
margin-top: 7px;
overflow: hidden;
border-radius: 999px;
background: #1c303a;
}
.bar > span {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 100ms linear;
}
.bar.input > span {
background: var(--orange);
}
.bar.output > span {
background: var(--cyan);
}
.bar.loss > span {
background: var(--blue);
}
.loss-breakdown {
display: grid;
gap: 7px;
margin-top: 17px;
padding-top: 14px;
border-top: 1px solid rgba(180, 215, 228, 0.1);
}
.loss-row {
display: flex;
justify-content: space-between;
gap: 12px;
color: var(--muted);
font-size: 10px;
}
.loss-row strong {
color: #cbdce4;
font-variant-numeric: tabular-nums;
}
.balance {
margin-top: 12px;
padding: 9px 10px;
border-radius: 9px;
color: #9db2bc;
background: rgba(115, 168, 255, 0.06);
font-size: 9px;
line-height: 1.45;
}
.model {
bottom: 20px;
left: 20px;
width: 430px;
padding: 17px 19px;
}
.model h2 {
margin: 0 0 9px;
font-size: 13px;
font-weight: 720;
}
.model p {
margin: 6px 0;
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.equations {
margin: 10px 0;
padding: 10px 11px;
border-radius: 10px;
color: #b7d3dc;
background: rgba(0, 0, 0, 0.18);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
line-height: 1.75;
}
.model-constants {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin: 11px 0;
}
.constant {
padding: 8px;
border: 1px solid rgba(170, 210, 225, 0.08);
border-radius: 8px;
color: #839ca8;
font-size: 8px;
}
.constant strong {
display: block;
margin-top: 3px;
color: #d9e8ee;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.caution {
color: #d7b47d !important;
}
.help {
position: fixed;
right: 20px;
bottom: 20px;
z-index: 9;
padding: 9px 12px;
border: 1px solid rgba(165, 206, 224, 0.12);
border-radius: 999px;
color: #829aa6;
background: rgba(7, 17, 25, 0.72);
font-size: 9px;
backdrop-filter: blur(10px);
}
@media (max-width: 820px) {
.controls {
top: 10px;
left: 10px;
width: min(330px, calc(100vw - 20px));
max-height: 49vh;
overflow: auto;
padding: 16px;
}
.metrics {
top: auto;
right: 10px;
bottom: 50px;
width: min(330px, calc(100vw - 20px));
padding: 14px;
}
.model {
display: none;
}
.help {
right: 10px;
bottom: 10px;
}
h1 {
font-size: 21px;
}
.intro {
margin-bottom: 12px;
}
}
@media (max-height: 720px) and (min-width: 821px) {
.model {
display: none;
}
}
</style>
</head>
<body>
<main id="viewport"></main>
<section class="panel controls" aria-label="Model controls">
<div class="eyebrow">Idealized · bounded</div>
<h1>Rotational drive model</h1>
<p class="intro">
Change the commanded current and opposing load torque. The bounded
low-poly mechanism responds dynamically.
</p>
<div class="control">
<div class="control-header">
<label for="currentSlider">Current command</label>
<output id="currentValue" for="currentSlider">7.5 A</output>
</div>
<input
id="currentSlider"
type="range"
min="0"
max="12"
step="0.1"
value="7.5"
aria-label="Current command in amperes"
>
<div class="range-limits">
<span>0 A</span>
<span>12 A bounded</span>
</div>
</div>
<div class="control">
<div class="control-header">
<label for="loadSlider">Opposing load torque</label>
<output id="loadValue" for="loadSlider">0.80 N·m</output>
</div>
<input
id="loadSlider"
type="range"
min="0"
max="3.2"
step="0.05"
value="0.8"
aria-label="Load torque in newton meters"
>
<div class="range-limits">
<span>0 N·m</span>
<span>3.2 N·m bounded</span>
</div>
</div>
<div class="divider"></div>
<div class="status-row">
<div id="status" class="status" data-mode="starting" aria-live="polite">
<span class="status-dot"></span>
<span id="stateLabel">Starting</span>
</div>
<span id="pauseLabel" class="small-stat">Live model</span>
</div>
<div class="stats">
<div class="stat">
<span class="stat-label">Actual current</span>
<span id="actualCurrent" class="stat-value">7.50 A</span>
</div>
<div class="stat">
<span class="stat-label">Speed</span>
<span id="rpm" class="stat-value">0 rpm</span>
</div>
<div class="stat">
<span class="stat-label">Drive torque</span>
<span id="driveTorque" class="stat-value">1.65 N·m</span>
</div>
</div>
<div class="buttons">
<button id="pauseButton" type="button" aria-pressed="false">Pause</button>
<button id="resetButton" type="button">Reset model</button>
</div>
</section>
<section class="panel metrics" aria-label="Power measurements">
<div class="eyebrow">Live power view</div>
<div class="metric input">
<div class="metric-head">
<span class="metric-name">Electrical input</span>
<span id="pinValue" class="metric-value">0 W</span>
</div>
<div class="bar input"><span id="pinBar"></span></div>
</div>
<div class="metric output">
<div class="metric-head">
<span class="metric-name">Mechanical output</span>
<span id="poutValue" class="metric-value">0 W</span>
</div>
<div class="bar output"><span id="poutBar"></span></div>
</div>
<div class="metric loss">
<div class="metric-head">
<span class="metric-name">Modeled losses</span>
<span id="plossValue" class="metric-value">0 W</span>
</div>
<div class="bar loss"><span id="plossBar"></span></div>
</div>
<div class="loss-breakdown">
<div class="loss-row">
<span>Copper / I²R</span>
<strong id="copperLoss">0 W</strong>
</div>
<div class="loss-row">
<span>Core-loss allowance</span>
<strong id="coreLoss">0 W</strong>
</div>
<div class="loss-row">
<span>Bearing + viscous friction</span>
<strong id="frictionLoss">0 W</strong>
</div>
</div>
<div id="balanceText" class="balance">
Power balance: transient energy is stored in rotor inertia.
</div>
</section>
<section class="panel model" aria-label="Model assumptions">
<h2>Model boundary and assumptions</h2>
<div class="equations">
τe = Kt I<br>
J ω̇ = τe − τload − τfriction<br>
Pin = Kt I ω + I²R + Pcore<br>
Pout = τload ω
</div>
<div class="model-constants">
<div class="constant">Torque constant<strong>0.22 N·m/A</strong></div>
<div class="constant">Winding resistance<strong>0.35 Ω</strong></div>
<div class="constant">Rotor inertia<strong>0.045 kg·m²</strong></div>
</div>
<p>
Friction uses a Coulomb-plus-viscous approximation; the core allowance
scales with ω². Electrical input is equivalent ideal-source power, not a
measured terminal voltage.
</p>
<p class="caution">
Illustrative values only. No winding, thermal, magnetic-saturation,
gearbox, efficiency-map, or hardware validation is represented.
</p>
</section>
<div class="help">Drag to orbit · wheel to zoom · double-click to reset view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// UI and bounded idealized model
// ------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const MODEL = Object.freeze({
maxCurrent: 12,
maxLoadTorque: 3.2,
maxSpeed: 180, // rad/s
torqueConstant: 0.22, // N·m/A
electricalConstant: 0.22, // V per rad/s
windingResistance: 0.35, // ohm
inertia: 0.045, // kg·m²
viscousFriction: 0.018, // N·m/(rad/s)
coulombFriction: 0.035, // N·m
staticFriction: 0.045, // N·m
coreLossCoefficient: 0.0025,
currentResponseTime: 0.12, // s
fixedTimeStep: 1 / 120
});
const state = {
commandCurrent: 7.5,
loadTorque: 0.8,
actualCurrent: 7.5,
omega: 0,
paused: false,
speedLimitLoss: 0
};
const currentSlider = $("currentSlider");
const loadSlider = $("loadSlider");
function paintRange(input) {
const percent =
100 * (Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min));
input.style.setProperty("--fill", `${percent}%`);
}
function updateControlLabels() {
$("currentValue").textContent = `${state.commandCurrent.toFixed(1)} A`;
$("loadValue").textContent = `${state.loadTorque.toFixed(2)} N·m`;
paintRange(currentSlider);
paintRange(loadSlider);
}
currentSlider.addEventListener("input", () => {
state.commandCurrent = Number(currentSlider.value);
updateControlLabels();
});
loadSlider.addEventListener("input", () => {
state.loadTorque = Number(loadSlider.value);
updateControlLabels();
});
$("pauseButton").addEventListener("click", () => {
state.paused = !state.paused;
$("pauseButton").textContent = state.paused ? "Resume" : "Pause";
$("pauseButton").setAttribute("aria-pressed", String(state.paused));
$("pauseLabel").textContent = state.paused ? "Model paused" : "Live model";
});
$("resetButton").addEventListener("click", () => {
state.commandCurrent = 7.5;
state.loadTorque = 0.8;
state.actualCurrent = 7.5;
state.omega = 0;
state.paused = false;
state.speedLimitLoss = 0;
currentSlider.value = "7.5";
loadSlider.value = "0.8";
$("pauseButton").textContent = "Pause";
$("pauseButton").setAttribute("aria-pressed", "false");
$("pauseLabel").textContent = "Live model";
updateControlLabels();
});
function simulateStep(dt) {
const currentBlend =
1 - Math.exp(-dt / MODEL.currentResponseTime);
state.actualCurrent +=
(state.commandCurrent - state.actualCurrent) * currentBlend;
const availableTorque =
MODEL.torqueConstant * state.actualCurrent;
state.speedLimitLoss = 0;
if (state.omega > 0.0001) {
const frictionTorque =
MODEL.coulombFriction + MODEL.viscousFriction * state.omega;
const netTorque =
availableTorque - state.loadTorque - frictionTorque;
let nextOmega =
state.omega + (netTorque / MODEL.inertia) * dt;
if (nextOmega < 0) {
nextOmega = 0;
}
if (nextOmega > MODEL.maxSpeed) {
state.speedLimitLoss =
0.5 *
MODEL.inertia *
(nextOmega * nextOmega - MODEL.maxSpeed * MODEL.maxSpeed) /
dt;
nextOmega = MODEL.maxSpeed;
}
state.omega = nextOmega;
} else {
state.omega = 0;
// Static-friction threshold determines whether the rotor can break away.
if (
availableTorque >
state.loadTorque + MODEL.staticFriction
) {
const breakawayTorque =
availableTorque -
state.loadTorque -
MODEL.staticFriction;
state.omega =
(breakawayTorque / MODEL.inertia) * dt;
}
}
}
function getMeasurements() {
const omega = state.omega;
const current = state.actualCurrent;
const driveTorque = MODEL.torqueConstant * current;
const frictionTorque =
omega > 0.0001
? MODEL.coulombFriction + MODEL.viscousFriction * omega
: 0;
const conversionPower = driveTorque * omega;
const copperLoss = current * current * MODEL.windingResistance;
const coreLoss =
MODEL.coreLossCoefficient * omega * omega;
const frictionLoss = frictionTorque * omega;
const electricalInput =
conversionPower + copperLoss + coreLoss;
const mechanicalOutput = state.loadTorque * omega;
const modeledLosses =
copperLoss +
coreLoss +
frictionLoss +
state.speedLimitLoss;
const netTorque =
driveTorque -
state.loadTorque -
frictionTorque;
let mode = "steady";
let label = "Near equilibrium";
if (omega < 0.03) {
if (
driveTorque <=
state.loadTorque + MODEL.staticFriction
) {
mode = "stalled";
label = "Stalled / held";
} else {
mode = "starting";
label = "Starting";
}
} else if (netTorque > 0.03) {
mode = "accelerating";
label = "Accelerating";
} else if (netTorque < -0.03) {
mode = "decelerating";
label = "Decelerating";
}
return {
omega,
current,
driveTorque,
electricalInput,
mechanicalOutput,
modeledLosses,
copperLoss,
coreLoss,
frictionLoss: frictionLoss + state.speedLimitLoss,
mode,
label
};
}
function formatPower(value) {
if (value >= 1000) {
return `${(value / 1000).toFixed(2)} kW`;
}
return `${Math.round(value)} W`;
}
function setBar(id, value) {
const percent = THREE.MathUtils.clamp(value / 520, 0, 1) * 100;
$(id).style.width = `${percent}%`;
}
let lastUIUpdate = 0;
function updateUI(now) {
if (now - lastUIUpdate < 80) return;
lastUIUpdate = now;
const m = getMeasurements();
$("actualCurrent").textContent = `${m.current.toFixed(2)} A`;
$("rpm").textContent =
`${Math.round(m.omega * 60 / (2 * Math.PI))} rpm`;
$("driveTorque").textContent =
`${m.driveTorque.toFixed(2)} N·m`;
$("stateLabel").textContent = m.label;
$("status").dataset.mode = m.mode;
$("pinValue").textContent = formatPower(m.electricalInput);
$("poutValue").textContent = formatPower(m.mechanicalOutput);
$("plossValue").textContent = formatPower(m.modeledLosses);
setBar("pinBar", m.electricalInput);
setBar("poutBar", m.mechanicalOutput);
setBar("plossBar", m.modeledLosses);
$("copperLoss").textContent = formatPower(m.copperLoss);
$("coreLoss").textContent = formatPower(m.coreLoss);
$("frictionLoss").textContent = formatPower(m.frictionLoss);
const storageRate =
m.electricalInput -
m.mechanicalOutput -
m.modeledLosses;
if (Math.abs(storageRate) < 1) {
$("balanceText").textContent =
"Power balance: steady-state balance; no net inertia storage.";
} else if (storageRate > 0) {
$("balanceText").textContent =
`Power balance: ${formatPower(storageRate)} is increasing rotor kinetic energy.`;
} else {
$("balanceText").textContent =
`Power balance: ${formatPower(Math.abs(storageRate))} is being released from rotor kinetic energy.`;
}
}
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const viewport = $("viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly rotational model. Drag to orbit, wheel to zoom."
);
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071119);
scene.fog = new THREE.FogExp2(0x071119, 0.027);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
// Camera controls implemented locally; no examples/addons are imported.
const cameraState = {
yaw: 0.76,
pitch: 0.39,
distance: 7.6,
target: new THREE.Vector3(0.35, 0.05, 0)
};
function updateCamera() {
const { yaw, pitch, distance, target } = cameraState;
const horizontal = Math.cos(pitch) * distance;
camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * distance,
target.z + Math.cos(yaw) * horizontal
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = 0.76;
cameraState.pitch = 0.39;
cameraState.distance = 7.6;
cameraState.target.set(0.35, 0.05, 0);
}
updateCamera();
const ambient = new THREE.HemisphereLight(0xbad9e6, 0x142029, 1.8);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffe5c4, 3.5);
keyLight.position.set(3.5, 7, 4.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x58dfd0, 2.4);
rimLight.position.set(-4, 2.2, -4);
scene.add(rimLight);
const warmPoint = new THREE.PointLight(0xffad55, 12, 9, 2);
warmPoint.position.set(-0.5, 2.5, 2.2);
scene.add(warmPoint);
const material = (options) =>
new THREE.MeshStandardMaterial({
flatShading: true,
...options
});
const materials = {
base: material({
color: 0x172832,
metalness: 0.55,
roughness: 0.57
}),
baseEdge: material({
color: 0x2b4652,
metalness: 0.7,
roughness: 0.38
}),
steel: material({
color: 0x8ea2aa,
metalness: 0.82,
roughness: 0.28
}),
darkSteel: material({
color: 0x344852,
metalness: 0.72,
roughness: 0.42
}),
housing: material({
color: 0x78929c,
metalness: 0.55,
roughness: 0.35,
transparent: true,
opacity: 0.22,
side: THREE.DoubleSide,
depthWrite: false
}),
copper: material({
color: 0xc46d32,
emissive: 0xff7a2a,
emissiveIntensity: 0.15,
metalness: 0.62,
roughness: 0.35
}),
rotor: material({
color: 0xe87936,
emissive: 0xff5a1f,
emissiveIntensity: 0.05,
metalness: 0.5,
roughness: 0.32
}),
rotorDark: material({
color: 0x7c351f,
metalness: 0.68,
roughness: 0.4
}),
load: material({
color: 0x287f7b,
emissive: 0x0b5c59,
emissiveIntensity: 0.13,
metalness: 0.58,
roughness: 0.36
}),
brake: material({
color: 0xd9664e,
metalness: 0.48,
roughness: 0.4
}),
cable: material({
color: 0x18242a,
metalness: 0.1,
roughness: 0.78
}),
led: material({
color: 0x65efb4,
emissive: 0x39e69b,
emissiveIntensity: 2.4,
metalness: 0.1,
roughness: 0.3
})
};
function addMesh(
geometry,
meshMaterial,
position = [0, 0, 0],
parent = scene
) {
const mesh = new THREE.Mesh(geometry, meshMaterial);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function axisCylinder(radius, length, segments, meshMaterial, position, parent) {
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
segments,
1,
false
);
const mesh = addMesh(geometry, meshMaterial, position, parent);
mesh.rotation.z = Math.PI / 2;
return mesh;
}
function makeLabel(text, color = "#dcebf0") {
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 72;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = "700 25px system-ui";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "rgba(5, 15, 22, 0.78)";
context.fillRect(18, 9, 284, 54);
context.strokeStyle = color;
context.lineWidth = 2;
context.strokeRect(18, 9, 284, 54);
context.fillStyle = color;
context.fillText(text, 160, 37);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(1.35, 0.304, 1);
sprite.renderOrder = 20;
return sprite;
}
// Floor and low-poly display plinth.
const floor = addMesh(
new THREE.PlaneGeometry(40, 40),
material({
color: 0x09151d,
metalness: 0.12,
roughness: 0.88
}),
[0, -1.46, 0]
);
floor.rotation.x = -Math.PI / 2;
floor.castShadow = false;
const grid = new THREE.GridHelper(18, 36, 0x24414c, 0x152a33);
grid.position.y = -1.445;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
addMesh(
new THREE.BoxGeometry(6.3, 0.28, 2.75),
materials.base,
[0.35, -1.29, 0]
);
addMesh(
new THREE.BoxGeometry(6.42, 0.07, 2.86),
materials.baseEdge,
[0.35, -1.115, 0]
);
for (const x of [-0.72, 0.72]) {
addMesh(
new THREE.BoxGeometry(0.46, 0.28, 2.1),
materials.darkSteel,
[x, -0.91, 0]
);
}
// Stator housing, rings, and longitudinal frame pieces.
const housing = axisCylinder(
1.16,
1.88,
16,
materials.housing,
[0, 0, 0],
scene
);
const housingEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(housing.geometry, 24),
new THREE.LineBasicMaterial({
color: 0x8eabb5,
transparent: true,
opacity: 0.43
})
);
housing.add(housingEdges);
for (const x of [-0.96, 0.96]) {
const ring = addMesh(
new THREE.TorusGeometry(1.12, 0.095, 6, 16),
materials.darkSteel,
[x, 0, 0]
);
ring.rotation.y = Math.PI / 2;
}
for (let i = 0; i < 4; i++) {
const angle = Math.PI / 4 + i * Math.PI / 2;
const rail = addMesh(
new THREE.BoxGeometry(1.92, 0.085, 0.085),
materials.steel,
[
0,
Math.cos(angle) * 1.18,
Math.sin(angle) * 1.18
]
);
rail.rotation.x = angle;
}
// Copper stator coils.
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2;
const radius = 0.88;
const coil = addMesh(
new THREE.BoxGeometry(0.77, 0.25, 0.2),
materials.copper,
[
0,
Math.cos(angle) * radius,
Math.sin(angle) * radius
]
);
coil.rotation.x = angle;
}
// Rotating assembly: rotor, shaft, and load disk.
const rotorAssembly = new THREE.Group();
scene.add(rotorAssembly);
axisCylinder(
0.59,
1.34,
12,
materials.rotor,
[0, 0, 0],
rotorAssembly
);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const radius = 0.51;
const fin = addMesh(
new THREE.BoxGeometry(0.91, 0.22, 0.105),
materials.rotorDark,
[
0,
Math.cos(angle) * radius,
Math.sin(angle) * radius
],
rotorAssembly
);
fin.rotation.x = angle;
}
axisCylinder(
0.14,
4.25,
10,
materials.steel,
[0.58, 0, 0],
rotorAssembly
);
axisCylinder(
0.72,
0.15,
12,
materials.rotorDark,
[-0.72, 0, 0],
rotorAssembly
);
axisCylinder(
0.87,
0.34,
12,
materials.load,
[2.15, 0, 0],
rotorAssembly
);
const loadRing = addMesh(
new THREE.TorusGeometry(0.78, 0.045, 5, 16),
materials.steel,
[2.34, 0, 0],
rotorAssembly
);
loadRing.rotation.y = Math.PI / 2;
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const blade = addMesh(
new THREE.BoxGeometry(0.10, 0.72, 0.15),
materials.load,
[
2.34,
Math.cos(angle) * 0.38,
Math.sin(angle) * 0.38
],
rotorAssembly
);
blade.rotation.x = angle;
}
// Static brake/load assembly.
const brakePad = addMesh(
new THREE.BoxGeometry(0.43, 0.24, 0.56),
materials.brake,
[2.13, 0.91, 0]
);
const brakeBody = addMesh(
new THREE.BoxGeometry(0.63, 0.33, 0.68),
materials.darkSteel,
[2.13, 1.13, 0]
);
axisCylinder(
0.16,
0.42,
10,
materials.steel,
[2.13, 1.35, 0],
scene
);
// Idealized source/control box and cables.
addMesh(
new THREE.BoxGeometry(1.18, 0.78, 0.55),
materials.darkSteel,
[-2.25, -0.45, -1.12]
);
addMesh(
new THREE.BoxGeometry(0.83, 0.39, 0.025),
material({
color: 0x0c3437,
emissive: 0x0b7b75,
emissiveIntensity: 0.55,
metalness: 0.1,
roughness: 0.35
}),
[-2.25, -0.36, -0.83]
);
const sourceLed = addMesh(
new THREE.SphereGeometry(0.07, 8, 5),
materials.led,
[-1.9, -0.65, -0.81]
);
function addCable(points) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point))
);
const geometry = new THREE.TubeGeometry(
curve,
22,
0.035,
5,
false
);
return addMesh(geometry, materials.cable);
}
addCable([
[-1.72, -0.35, -1.18],
[-1.5, -0.08, -1.35],
[-1.25, -0.15, -1.1],
[-1.05, -0.25, -0.78]
]);
addCable([
[-1.72, -0.55, -1.18],
[-1.52, -0.78, -1.4],
[-1.22, -0.75, -1.12],
[-1.05, -0.48, -0.78]
]);
// World-space labels.
const rotorLabel = makeLabel("ROTOR", "#ffbd78");
rotorLabel.position.set(-0.15, 1.48, 0);
scene.add(rotorLabel);
const loadLabel = makeLabel("LOAD", "#72eee0");
loadLabel.position.set(2.22, 1.56, 0);
scene.add(loadLabel);
const sourceLabel = makeLabel("IDEAL SOURCE", "#8ebaff");
sourceLabel.position.set(-2.25, 0.11, -1.12);
scene.add(sourceLabel);
// Direction arrows.
const driveArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(2.48, 0.76, 0),
0.82,
0x58dfd0,
0.19,
0.12
);
scene.add(driveArrow);
const loadArrow = new THREE.ArrowHelper(
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(1.65, 0.76, 0),
0.68,
0xff786f,
0.17,
0.11
);
scene.add(loadArrow);
// ------------------------------------------------------------
// Direct pointer / keyboard camera controls
// ------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
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.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch - deltaY * 0.005,
0.08,
1.28
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
4.4,
12
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraState.yaw -= 0.08;
break;
case "ArrowRight":
cameraState.yaw += 0.08;
break;
case "ArrowUp":
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + 0.06,
0.08,
1.28
);
break;
case "ArrowDown":
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch - 0.06,
0.08,
1.28
);
break;
case "+":
case "=":
cameraState.distance = Math.max(
4.4,
cameraState.distance - 0.35
);
break;
case "-":
cameraState.distance = Math.min(
12,
cameraState.distance + 0.35
);
break;
default:
handled = false;
}
if (handled) event.preventDefault();
});
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
let accumulator = 0;
function animate(now) {
requestAnimationFrame(animate);
const frameDelta = Math.min(clock.getDelta(), 0.05);
if (!state.paused) {
accumulator += frameDelta;
while (accumulator >= MODEL.fixedTimeStep) {
simulateStep(MODEL.fixedTimeStep);
accumulator -= MODEL.fixedTimeStep;
}
// Visual rotation is slowed for readability; physics uses real dt.
rotorAssembly.rotation.x +=
state.omega * frameDelta * 0.10;
}
const currentFraction =
state.actualCurrent / MODEL.maxCurrent;
materials.copper.emissiveIntensity =
0.12 + currentFraction * 1.5;
materials.rotor.emissiveIntensity =
0.025 + currentFraction * 0.18;
brakePad.position.y =
0.91 - THREE.MathUtils.clamp(state.loadTorque / 3.2, 0, 1) * 0.07;
driveArrow.visible = state.actualCurrent > 0.03;
driveArrow.setLength(
0.48 + currentFraction * 0.48,
0.19,
0.12
);
loadArrow.visible = state.loadTorque > 0.03;
loadArrow.setLength(
0.38 + THREE.MathUtils.clamp(state.loadTorque / 3.2, 0, 1) * 0.45,
0.17,
0.11
);
const measurements = getMeasurements();
sourceLed.material.emissive.setHex(
measurements.mode === "stalled" ? 0xff443b : 0x39e69b
);
sourceLed.material.color.setHex(
measurements.mode === "stalled" ? 0xff786f : 0x65efb4
);
updateCamera();
updateUI(now);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateControlLabels();
requestAnimationFrame(animate);
</script>
</body>
</html>
```