Prompt specification
Build or customize the Three.js 3D scene "Step-up and step-down comparison" 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: Step-up and step-down comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/step-up-and-step-down-comparison-x1674
Scene Description Prompt:
Place two ideal transformer models with different turn ratios under the same alternating input; compare output voltage and inverse current relationships under equal power assumptions. Build this with Three.js from cdnjs as a stylized scene in one self-contained HTML file.
Working Three.js Starter (step-up-and-step-down-comparison-x1674):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ideal Transformer Comparison</title>
<style>
:root {
color-scheme: dark;
--bg: #07111e;
--panel: rgba(10, 22, 38, 0.82);
--border: rgba(148, 188, 224, 0.19);
--text: #eef6ff;
--muted: #91a9c1;
--cyan: #39dcff;
--orange: #ffad55;
--violet: #d47cff;
--green: #7dffc3;
}
* {
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;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
pointer-events: auto;
background:
linear-gradient(145deg, rgba(19, 38, 61, 0.78), rgba(7, 17, 31, 0.9));
border: 1px solid var(--border);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.brand {
position: absolute;
top: 22px;
left: 22px;
width: 390px;
padding: 20px 22px 18px;
border-radius: 18px;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 7px;
font-size: clamp(25px, 3vw, 36px);
line-height: 1.05;
letter-spacing: -0.045em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 16px;
}
.metric {
min-width: 0;
padding: 10px;
border: 1px solid rgba(148, 188, 224, 0.13);
border-radius: 11px;
background: rgba(2, 9, 18, 0.35);
}
.metric span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.metric strong {
display: block;
overflow: hidden;
color: var(--text);
font-size: 14px;
text-overflow: ellipsis;
white-space: nowrap;
}
.controls {
position: absolute;
top: 22px;
right: 22px;
width: 310px;
padding: 18px;
border-radius: 18px;
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
}
.control-title strong {
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.live {
display: flex;
align-items: center;
gap: 6px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.live::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px var(--green);
content: "";
}
.control {
margin: 13px 0;
}
.control label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: #c6d8e9;
font-size: 11px;
font-weight: 700;
}
.control output {
color: var(--cyan);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
background:
linear-gradient(
90deg,
var(--cyan) 0 var(--fill, 50%),
rgba(148, 188, 224, 0.16) var(--fill, 50%) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid #dff8ff;
border-radius: 50%;
appearance: none;
background: #1597b7;
box-shadow: 0 0 15px rgba(57, 220, 255, 0.55);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #dff8ff;
border-radius: 50%;
background: #1597b7;
box-shadow: 0 0 15px rgba(57, 220, 255, 0.55);
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
button {
min-height: 38px;
border: 1px solid rgba(57, 220, 255, 0.24);
border-radius: 10px;
color: #dff8ff;
background: rgba(57, 220, 255, 0.08);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
transition: 160ms ease;
}
button:hover {
border-color: rgba(57, 220, 255, 0.65);
background: rgba(57, 220, 255, 0.16);
transform: translateY(-1px);
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 11px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(148, 188, 224, 0.13);
color: var(--muted);
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--dot);
box-shadow: 0 0 8px var(--dot);
}
.comparison {
position: absolute;
right: 22px;
bottom: 22px;
left: 22px;
display: grid;
grid-template-columns: 1fr 1fr 1.2fr;
gap: 12px;
align-items: stretch;
}
.result {
overflow: hidden;
padding: 16px 17px;
border-radius: 16px;
}
.result-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.result h2,
.result h3 {
margin: 0;
font-size: 15px;
letter-spacing: -0.02em;
}
.ratio {
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 999px;
font-size: 11px;
font-weight: 900;
}
.t1 {
color: var(--cyan);
}
.t2 {
color: var(--violet);
}
.equations {
margin-bottom: 12px;
color: #dce9f5;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.7;
}
.result-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.datum {
padding: 9px;
border-radius: 10px;
background: rgba(1, 8, 16, 0.42);
}
.datum span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.datum strong {
font-size: 14px;
font-variant-numeric: tabular-nums;
}
.law {
color: var(--green);
}
.law .result-grid {
grid-template-columns: 1fr 1fr;
}
.law p {
margin: 12px 0 0;
color: #b8cbdc;
font-size: 11px;
line-height: 1.55;
}
.hint {
position: absolute;
bottom: 185px;
left: 50%;
padding: 7px 11px;
border: 1px solid rgba(148, 188, 224, 0.13);
border-radius: 999px;
color: rgba(207, 225, 241, 0.67);
background: rgba(7, 17, 30, 0.55);
font-size: 10px;
transform: translateX(-50%);
white-space: nowrap;
}
@media (max-width: 900px) {
.brand {
top: 12px;
left: 12px;
width: calc(100% - 24px);
padding: 15px;
}
.brand .subtitle {
display: none;
}
.metrics {
margin-top: 11px;
}
.controls {
top: 170px;
right: 12px;
width: 270px;
padding: 14px;
}
.comparison {
right: 12px;
bottom: 12px;
left: 12px;
grid-template-columns: 1fr 1fr;
}
.law {
grid-column: 1 / -1;
}
.law .result-grid {
grid-template-columns: repeat(3, 1fr);
}
.hint {
display: none;
}
}
@media (max-width: 580px) {
.brand {
width: calc(100% - 24px);
}
h1 {
font-size: 25px;
}
.controls {
top: 166px;
right: 12px;
left: 12px;
width: auto;
}
.control {
margin: 9px 0;
}
.legend,
.control-title {
display: none;
}
.buttons {
margin-top: 10px;
}
.comparison {
grid-template-columns: 1fr;
max-height: 47vh;
overflow-y: auto;
}
.law {
grid-column: auto;
}
.law .result-grid {
grid-template-columns: 1fr 1fr;
}
.result {
padding: 13px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D transformer comparison"></canvas>
<main id="hud">
<section class="brand panel">
<div class="eyebrow">Ideal Transformer Bench</div>
<h1>Same input.<br>Different turns.</h1>
<p class="subtitle">
Two lossless transformers share one alternating source. Each load receives
the same real power, so a higher output voltage produces a lower output current.
</p>
<div class="metrics">
<div class="metric">
<span>Shared input</span>
<strong id="vinValue">120 V RMS</strong>
</div>
<div class="metric">
<span>Load power</span>
<strong id="powerValue">120 W</strong>
</div>
<div class="metric">
<span>Input current</span>
<strong id="iinValue">1.00 A RMS</strong>
</div>
</div>
</section>
<section class="controls panel">
<div class="control-title">
<strong>Experiment controls</strong>
<span class="live">Live model</span>
</div>
<div class="control">
<label for="vin">
Source voltage
<output id="vinOut">120 V RMS</output>
</label>
<input id="vin" type="range" min="24" max="240" step="6" value="120">
</div>
<div class="control">
<label for="power">
Equal power per load
<output id="powerOut">120 W</output>
</label>
<input id="power" type="range" min="30" max="480" step="10" value="120">
</div>
<div class="control">
<label for="frequency">
AC frequency
<output id="freqOut">50 Hz</output>
</label>
<input id="frequency" type="range" min="20" max="60" step="1" value="50">
</div>
<div class="buttons">
<button id="pause" type="button" aria-pressed="false">Ⅱ Pause</button>
<button id="resetView" type="button">Reset camera</button>
</div>
<div class="legend">
<span><i class="dot" style="--dot:#ffad55"></i>Primary</span>
<span><i class="dot" style="--dot:#39dcff"></i>Secondary</span>
<span><i class="dot" style="--dot:#d47cff"></i>Current</span>
</div>
</section>
<section class="comparison">
<article class="result panel t1">
<div class="result-head">
<h2>Transformer T1</h2>
<span class="ratio">1 : 1</span>
</div>
<div class="equations">
V₂ = 1 × V₁<br>
I₂ = 1 × I₁
</div>
<div class="result-grid">
<div class="datum">
<span>Output V</span>
<strong id="t1V">120.0 V</strong>
</div>
<div class="datum">
<span>Output I</span>
<strong id="t1I">1.00 A</strong>
</div>
<div class="datum">
<span>Load R</span>
<strong id="t1R">12.0 Ω</strong>
</div>
</div>
</article>
<article class="result panel t2">
<div class="result-head">
<h2>Transformer T2</h2>
<span class="ratio">1 : 2</span>
</div>
<div class="equations">
V₂ = 2 × V₁<br>
I₂ = ½ × I₁
</div>
<div class="result-grid">
<div class="datum">
<span>Output V</span>
<strong id="t2V">240.0 V</strong>
</div>
<div class="datum">
<span>Output I</span>
<strong id="t2I">0.50 A</strong>
</div>
<div class="datum">
<span>Load R</span>
<strong id="t2R">48.0 Ω</strong>
</div>
</div>
</article>
<article class="result panel law">
<div class="result-head">
<h3>Equal-power relationship</h3>
<span class="ratio">IDEAL</span>
</div>
<div class="result-grid">
<div class="datum">
<span>Voltage ratio</span>
<strong>V₂/V₁ = N₂/N₁</strong>
</div>
<div class="datum">
<span>Current ratio</span>
<strong>I₂/I₁ = N₁/N₂</strong>
</div>
<div class="datum">
<span>Power</span>
<strong>P₁ = P₂</strong>
</div>
</div>
<p>
At fixed power, <strong>I₂ = P / V₂</strong>. Therefore doubling the
secondary voltage halves the secondary current. The resistive load
values also change so both models still consume equal power.
</p>
</article>
</section>
<div class="hint">Drag to orbit · Wheel to zoom · Double-click to reset</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const canvas = $("scene");
const state = {
vin: 120,
power: 120,
frequency: 50,
paused: false
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111e);
scene.fog = new THREE.Fog(0x07111e, 19, 38);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
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;
scene.add(new THREE.HemisphereLight(0xbadfff, 0x102039, 2.2));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(-5, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.normalBias = 0.03;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x39dcff, 38, 14, 2);
cyanLight.position.set(5, 5, 3);
scene.add(cyanLight);
const orangeLight = new THREE.PointLight(0xffa04c, 30, 13, 2);
orangeLight.position.set(-6, 4, 1);
scene.add(orangeLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
new THREE.MeshStandardMaterial({
color: 0x081421,
metalness: 0.15,
roughness: 0.82
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.03;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0x36536d, 0x1d3045);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const primaryMaterial = new THREE.MeshStandardMaterial({
color: 0xd97825,
emissive: 0xff7d24,
emissiveIntensity: 0.38,
metalness: 0.65,
roughness: 0.28
});
const secondaryMaterial = new THREE.MeshStandardMaterial({
color: 0x159ab9,
emissive: 0x20d7ff,
emissiveIntensity: 0.46,
metalness: 0.58,
roughness: 0.25
});
const steelMaterial = new THREE.MeshStandardMaterial({
color: 0x33465d,
metalness: 0.84,
roughness: 0.3
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x111d2c,
metalness: 0.65,
roughness: 0.42
});
const copperMaterial = new THREE.MeshStandardMaterial({
color: 0xb86832,
emissive: 0x71300e,
emissiveIntensity: 0.16,
metalness: 0.82,
roughness: 0.28
});
function addBox(parent, size, position, material) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(parent, radius, height, position, material, segments = 24) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, segments),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(parent, a, b, radius, material) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 12),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function paintLabel(label, title, subtitle, accent = "#39dcff") {
const { ctx, canvas, texture } = label;
const w = canvas.width;
const h = canvas.height;
ctx.clearRect(0, 0, w, h);
const gradient = ctx.createLinearGradient(0, 0, w, h);
gradient.addColorStop(0, "rgba(12,29,48,0.96)");
gradient.addColorStop(1, "rgba(5,14,26,0.96)");
roundedRect(ctx, 5, 5, w - 10, h - 10, 25);
ctx.fillStyle = gradient;
ctx.fill();
ctx.strokeStyle = "rgba(151,194,229,0.38)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
roundedRect(ctx, 22, 27, 7, h - 54, 4);
ctx.fill();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#f1f8ff";
ctx.font = "800 43px system-ui, sans-serif";
ctx.fillText(title, w / 2 + 6, h * 0.41);
ctx.fillStyle = "#9fb6cc";
ctx.font = "700 25px system-ui, sans-serif";
ctx.fillText(subtitle, w / 2 + 6, h * 0.72);
texture.needsUpdate = true;
}
function makeLabel(title, subtitle, accent = "#39dcff", worldWidth = 3.2) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 160;
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldWidth * 0.3125, 1);
sprite.renderOrder = 20;
const label = {
sprite,
canvas: labelCanvas,
ctx: labelCanvas.getContext("2d"),
texture
};
paintLabel(label, title, subtitle, accent);
return label;
}
class HelixCurve extends THREE.Curve {
constructor(turns, radius, height) {
super();
this.turns = turns;
this.radius = radius;
this.height = height;
}
getPoint(t, target = new THREE.Vector3()) {
const angle = t * this.turns * Math.PI * 2;
return target.set(
Math.cos(angle) * this.radius,
(t - 0.5) * this.height,
Math.sin(angle) * this.radius
);
}
}
function makeCoil(parent, legX, turns, material) {
const curve = new HelixCurve(turns, 0.47, 2.35);
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(curve, turns * 28, 0.058, 8, false),
material
);
mesh.position.set(legX, 2.65, 0);
mesh.castShadow = true;
parent.add(mesh);
cylinderBetween(
parent,
new THREE.Vector3(legX + 0.47, 3.825, 0),
new THREE.Vector3(legX + 0.47, 4.39, 0),
0.044,
material
);
addCylinder(
parent,
0.13,
0.3,
[legX + 0.47, 4.39, 0],
material,
18
);
return mesh;
}
function createMeter(parent, x, color) {
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.1,
metalness: 0.3,
roughness: 0.25
});
addBox(
parent,
[0.38, 0.08, 0.38],
[x, 0.42, 1.5],
darkMaterial
);
const bar = new THREE.Mesh(
new THREE.CylinderGeometry(0.105, 0.105, 1, 18),
material
);
bar.position.set(x, 0.7, 1.5);
bar.castShadow = true;
parent.add(bar);
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.15, 18, 12),
material
);
cap.position.set(x, 0.7, 1.5);
parent.add(cap);
const label = makeLabel("V₂", "VOLTS", "#39dcff", 0.7);
label.sprite.position.set(x - 0.55, 0.62, 1.82);
parent.add(label.sprite);
return { bar, cap };
}
function createTransformer({
id,
x,
ratio,
primaryTurns,
secondaryTurns,
accent
}) {
const root = new THREE.Group();
root.position.x = x;
scene.add(root);
addBox(
root,
[3.55, 0.28, 2.55],
[0, 0.14, 0],
darkMaterial
);
addBox(
root,
[3.28, 0.045, 2.28],
[0, 0.305, 0],
new THREE.MeshStandardMaterial({
color: 0x1a6380,
emissive: 0x0b3b52,
emissiveIntensity: 0.6,
metalness: 0.55,
roughness: 0.32
})
);
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0x27384d,
emissive: 0x0a3759,
emissiveIntensity: 0.12,
metalness: 0.88,
roughness: 0.3
});
addBox(root, [0.42, 3.0, 0.82], [-1, 2.65, 0], coreMaterial);
addBox(root, [0.42, 3.0, 0.82], [1, 2.65, 0], coreMaterial);
addBox(root, [2.42, 0.38, 0.82], [0, 4.0, 0], coreMaterial);
addBox(root, [2.42, 0.38, 0.82], [0, 1.3, 0], coreMaterial);
addBox(root, [0.55, 0.85, 0.66], [-1, 0.72, 0], steelMaterial);
addBox(root, [0.55, 0.85, 0.66], [1, 0.72, 0], steelMaterial);
const laminationMaterial = new THREE.MeshStandardMaterial({
color: 0x668097,
metalness: 0.9,
roughness: 0.38
});
for (let i = 0; i < 11; i++) {
addBox(
root,
[0.018, 0.25, 0.84],
[-1.04 + i * 0.208, 4.0, 0],
laminationMaterial
);
}
makeCoil(root, -1, primaryTurns, primaryMaterial);
makeCoil(root, 1, secondaryTurns, secondaryMaterial);
const label = makeLabel(
`${id} · ${ratio}`,
id === "T1"
? "100 : 100 TURNS"
: "100 : 200 TURNS · STEP-UP",
accent,
3.35
);
label.sprite.position.set(0, 5.2, 0);
root.add(label.sprite);
const voltageMeter = createMeter(root, -0.58, 0x39dcff);
const currentMeter = createMeter(root, 0.58, 0xd47cff);
const currentLabel = makeLabel("I₂", "ARMS", "#d47cff", 0.7);
currentLabel.sprite.position.set(1.13, 0.62, 1.82);
root.add(currentLabel.sprite);
const fluxPoints = [
new THREE.Vector3(-1, 3.82, 0.47),
new THREE.Vector3(-0.78, 3.98, 0.47),
new THREE.Vector3(0.78, 3.98, 0.47),
new THREE.Vector3(1, 3.82, 0.47),
new THREE.Vector3(1, 1.48, 0.47),
new THREE.Vector3(0.78, 1.32, 0.47),
new THREE.Vector3(-0.78, 1.32, 0.47),
new THREE.Vector3(-1, 1.48, 0.47)
];
const fluxCurve = new THREE.CatmullRomCurve3(
fluxPoints,
true,
"catmullrom",
0.25
);
const fluxMaterial = new THREE.MeshBasicMaterial({
color: 0x62aaff,
transparent: true,
opacity: 0.45,
blending: THREE.AdditiveBlending
});
const fluxLine = new THREE.Mesh(
new THREE.TubeGeometry(fluxCurve, 120, 0.017, 6, true),
fluxMaterial
);
root.add(fluxLine);
const fluxParticles = [];
for (let i = 0; i < 3; i++) {
const particle = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 12, 8),
new THREE.MeshBasicMaterial({
color: 0x9acbff,
transparent: true,
blending: THREE.AdditiveBlending
})
);
root.add(particle);
fluxParticles.push(particle);
}
return {
id,
x,
root,
label,
coreMaterial,
voltageMeter,
currentMeter,
fluxCurve,
fluxMaterial,
fluxParticles,
primaryCable: null,
outputCable: null,
load: null
};
}
const transformers = [
createTransformer({
id: "T1",
x: -3.2,
ratio: "1 : 1",
primaryTurns: 8,
secondaryTurns: 8,
accent: "#39dcff"
}),
createTransformer({
id: "T2",
x: 3.2,
ratio: "1 : 2",
primaryTurns: 8,
secondaryTurns: 16,
accent: "#d47cff"
})
];
function createLoad(x, accent) {
const root = new THREE.Group();
root.position.x = x;
scene.add(root);
addBox(root, [1.8, 0.2, 1.6], [0, 0.1, 0], darkMaterial);
addBox(
root,
[0.22, 0.85, 0.55],
[0, 0.62, 0],
steelMaterial
);
const ceramicMaterial = new THREE.MeshStandardMaterial({
color: 0xd8d1b9,
emissive: accent,
emissiveIntensity: 0.06,
metalness: 0.08,
roughness: 0.54
});
const body = addCylinder(
root,
0.5,
1.18,
[0, 1.25, 0],
ceramicMaterial,
36
);
body.rotation.z = Math.PI / 2;
const endMaterial = new THREE.MeshStandardMaterial({
color: 0xb66c32,
metalness: 0.82,
roughness: 0.27
});
for (const side of [-1, 1]) {
const cap = addCylinder(
root,
0.525,
0.13,
[side * 0.63, 1.25, 0],
endMaterial,
32
);
cap.rotation.z = Math.PI / 2;
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.505, 0.035, 8, 32),
darkMaterial
);
band.rotation.y = Math.PI / 2;
band.position.set(side * 0.28, 1.25, 0);
root.add(band);
}
const label = makeLabel("120 W LOAD", "12.0 Ω · 1.00 A", accent, 2.35);
label.sprite.position.set(0, 2.45, 0);
root.add(label.sprite);
return {
root,
label,
ceramicMaterial,
high: new THREE.Vector3(x < 0 ? -6.78 : 6.78, 1.25, 0),
low: new THREE.Vector3(x < 0 ? -5.42 : 5.42, 1.25, 0)
};
}
const loads = {
T1: createLoad(-6.1, "#39dcff"),
T2: createLoad(6.1, "#d47cff")
};
const source = new THREE.Group();
source.position.set(0, 0, 3);
scene.add(source);
const sourceGlass = new THREE.MeshStandardMaterial({
color: 0x1a89a8,
emissive: 0x0b526d,
emissiveIntensity: 0.45,
metalness: 0.25,
roughness: 0.18,
transparent: true,
opacity: 0.58
});
addCylinder(source, 0.68, 1.35, [0, 1.05, 0], sourceGlass, 40);
addCylinder(source, 0.77, 0.13, [0, 0.36, 0], steelMaterial, 40);
addCylinder(source, 0.77, 0.13, [0, 1.76, 0], steelMaterial, 40);
const sourceRing = new THREE.Mesh(
new THREE.TorusGeometry(0.76, 0.027, 8, 64),
new THREE.MeshBasicMaterial({
color: 0x56e6ff,
transparent: true,
opacity: 0.8
})
);
sourceRing.position.set(0, 1.05, 0.66);
source.add(sourceRing);
const waveCount = 65;
const wavePositions = new Float32Array(waveCount * 3);
const waveGeometry = new THREE.BufferGeometry();
waveGeometry.setAttribute(
"position",
new THREE.BufferAttribute(wavePositions, 3).setUsage(THREE.DynamicDrawUsage)
);
const wave = new THREE.Line(
waveGeometry,
new THREE.LineBasicMaterial({
color: 0x7ef2ff,
transparent: true,
opacity: 0.95
})
);
source.add(wave);
const sourceLabel = makeLabel(
"SHARED AC SOURCE",
"120 V RMS · 50 Hz",
"#ffad55",
2.65
);
sourceLabel.sprite.position.set(0, 2.65, 0);
source.add(sourceLabel.sprite);
function makeCable(points, material, radius = 0.038) {
const curve = new THREE.CatmullRomCurve3(
points.map((point) => new THREE.Vector3(...point)),
false,
"catmullrom",
0.35
);
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(curve, points.length * 22, radius, 8, false),
material
);
mesh.castShadow = true;
scene.add(mesh);
return {
curve,
mesh,
pulses: [],
current: 1,
maxCurrent: 1
};
}
const cablePrimaryMaterial = new THREE.MeshStandardMaterial({
color: 0xc87832,
emissive: 0xd65b16,
emissiveIntensity: 0.55,
metalness: 0.65,
roughness: 0.3
});
const cableSecondaryMaterial = new THREE.MeshStandardMaterial({
color: 0x198eaa,
emissive: 0x0d9bc2,
emissiveIntensity: 0.65,
metalness: 0.55,
roughness: 0.28
});
const returnCableMaterial = new THREE.MeshStandardMaterial({
color: 0x5a3e32,
emissive: 0x331a0e,
emissiveIntensity: 0.25,
metalness: 0.72,
roughness: 0.4
});
function createPulses(cable, color, count) {
const geometry = new THREE.SphereGeometry(0.07, 12, 8);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
blending: THREE.AdditiveBlending
});
for (let i = 0; i < count; i++) {
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
cable.pulses.push({
mesh,
offset: i / count
});
}
}
for (const transformer of transformers) {
const rootX = transformer.x;
const primaryEnd = [rootX - 0.53, 4.39, 0];
const secondaryEnd = [rootX + 1.47, 4.39, 0];
transformer.primaryCable = makeCable(
[
[0, 1.82, 3],
[rootX < 0 ? -1.15 : 1.15, 2.75, 3.05],
[rootX < 0 ? -3.55 : 3.15, 5.05, 1.55],
primaryEnd
],
cablePrimaryMaterial,
0.043
);
createPulses(transformer.primaryCable, 0xffa24c, 6);
const load = loads[transformer.id];
transformer.load = load;
const outputPoints = transformer.id === "T1"
? [
secondaryEnd,
[-2.1, 5.0, 1.25],
[-5.65, 2.7, 1.65],
load.high.toArray()
]
: [
secondaryEnd,
[5.08, 5.0, 1.25],
[6.05, 2.7, 1.65],
load.high.toArray()
];
transformer.outputCable = makeCable(
outputPoints,
cableSecondaryMaterial,
0.043
);
createPulses(
transformer.outputCable,
0x48e4ff,
transformer.id === "T1" ? 6 : 3
);
makeCable(
[
load.low.toArray(),
[transformer.id === "T1" ? -5.0 : 5.0, 0.48, 2.55],
[0, 0.42, 3.65],
[0, 0.4, 3]
],
returnCableMaterial,
0.031
);
}
function formatNumber(value, digits = 1) {
return Number(value).toFixed(digits).replace(/\.0+$/, (match) =>
digits > 0 ? match : ""
);
}
function setMeter(meter, height) {
const h = Math.max(0.22, height);
meter.bar.scale.y = h;
meter.bar.position.y = 0.49 + h / 2;
meter.cap.position.y = 0.49 + h;
}
function updateReadouts() {
const vin = state.vin;
const power = state.power;
const inputCurrent = power / vin;
const t1Voltage = vin;
const t1Current = power / t1Voltage;
const t1Resistance = (t1Voltage * t1Voltage) / power;
const t2Voltage = vin * 2;
const t2Current = power / t2Voltage;
const t2Resistance = (t2Voltage * t2Voltage) / power;
$("vinValue").textContent = `${vin} V RMS`;
$("powerValue").textContent = `${power} W`;
$("iinValue").textContent = `${formatNumber(inputCurrent, 2)} A RMS`;
$("vinOut").textContent = `${vin} V RMS`;
$("powerOut").textContent = `${power} W`;
$("freqOut").textContent = `${state.frequency} Hz`;
$("t1V").textContent = `${formatNumber(t1Voltage)} V`;
$("t1I").textContent = `${formatNumber(t1Current, 2)} A`;
$("t1R").textContent = `${formatNumber(t1Resistance)} Ω`;
$("t2V").textContent = `${formatNumber(t2Voltage)} V`;
$("t2I").textContent = `${formatNumber(t2Current, 2)} A`;
$("t2R").textContent = `${formatNumber(t2Resistance)} Ω`;
paintLabel(
sourceLabel,
"SHARED AC SOURCE",
`${vin} V RMS · ${state.frequency} Hz`,
"#ffad55"
);
paintLabel(
loads.T1.label,
`${power} W LOAD`,
`${formatNumber(t1Resistance)} Ω · ${formatNumber(t1Current, 2)} A`,
"#39dcff"
);
paintLabel(
loads.T2.label,
`${power} W LOAD`,
`${formatNumber(t2Resistance)} Ω · ${formatNumber(t2Current, 2)} A`,
"#d47cff"
);
const maxVoltage = Math.max(t1Voltage, t2Voltage);
const maxCurrent = Math.max(inputCurrent, t1Current, t2Current);
setMeter(
transformers[0].voltageMeter,
0.24 + (t1Voltage / maxVoltage) * 2.05
);
setMeter(
transformers[1].voltageMeter,
0.24 + (t2Voltage / maxVoltage) * 2.05
);
setMeter(
transformers[0].currentMeter,
0.24 + (t1Current / maxCurrent) * 2.05
);
setMeter(
transformers[1].currentMeter,
0.24 + (t2Current / maxCurrent) * 2.05
);
transformers.forEach((transformer) => {
transformer.primaryCable.current = inputCurrent;
transformer.primaryCable.maxCurrent = maxCurrent;
});
transformers[0].outputCable.current = t1Current;
transformers[0].outputCable.maxCurrent = maxCurrent;
transformers[1].outputCable.current = t2Current;
transformers[1].outputCable.maxCurrent = maxCurrent;
loads.T1.ceramicMaterial.emissiveIntensity =
0.05 + (power / 480) * 0.2;
loads.T2.ceramicMaterial.emissiveIntensity =
0.05 + (power / 480) * 0.2;
document.querySelectorAll('input[type="range"]').forEach((input) => {
const percent =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--fill", `${percent}%`);
});
}
$("vin").addEventListener("input", (event) => {
state.vin = Number(event.target.value);
updateReadouts();
});
$("power").addEventListener("input", (event) => {
state.power = Number(event.target.value);
updateReadouts();
});
$("frequency").addEventListener("input", (event) => {
state.frequency = Number(event.target.value);
updateReadouts();
});
$("pause").addEventListener("click", () => {
state.paused = !state.paused;
$("pause").textContent = state.paused ? "▶ Resume" : "Ⅱ Pause";
$("pause").setAttribute("aria-pressed", String(state.paused));
});
const orbit = {
theta: 0.31,
phi: 1.12,
radius: innerWidth < 700 ? 21 : 16.8,
target: new THREE.Vector3(0, 2.65, 0)
};
function resetCamera() {
orbit.theta = 0.31;
orbit.phi = 1.12;
orbit.radius = innerWidth < 700 ? 21 : 16.8;
orbit.target.set(0, 2.65, 0);
}
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
$("resetView").addEventListener("click", resetCamera);
canvas.addEventListener("dblclick", resetCamera);
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.45,
1.43
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
10,
28
);
},
{ passive: false }
);
let elapsed = 0;
let previousTime = performance.now();
function animate(now) {
const dt = THREE.MathUtils.clamp((now - previousTime) / 1000, 0, 0.05);
previousTime = now;
if (!state.paused) {
elapsed += dt;
}
// Stylized, slowed phase so the 20–60 Hz signal remains readable.
const phase = elapsed * state.frequency * 0.08 * Math.PI * 2;
const drive = Math.sin(phase);
const driveMagnitude = Math.abs(drive);
for (let i = 0; i < waveCount; i++) {
const x = -0.55 + (1.1 * i) / (waveCount - 1);
wavePositions[i * 3] = x;
wavePositions[i * 3 + 1] =
1.02 + Math.sin(phase + x * 7.2) * 0.22;
wavePositions[i * 3 + 2] = 0.69;
}
waveGeometry.attributes.position.needsUpdate = true;
sourceRing.scale.setScalar(
1 + driveMagnitude * 0.035 + Math.sin(phase * 0.5) * 0.01
);
sourceRing.material.opacity = 0.55 + driveMagnitude * 0.4;
primaryMaterial.emissiveIntensity =
0.25 + driveMagnitude * (0.35 + state.vin / 240 * 0.25);
secondaryMaterial.emissiveIntensity =
0.3 + driveMagnitude * (0.4 + state.vin / 240 * 0.25);
for (const transformer of transformers) {
transformer.coreMaterial.emissiveIntensity =
0.07 + driveMagnitude * (0.14 + state.vin / 240 * 0.16);
transformer.fluxMaterial.opacity =
0.15 + driveMagnitude * 0.48;
transformer.fluxParticles.forEach((particle, index) => {
let t = (elapsed * 0.095 + index / 3) % 1;
if (drive < 0) t = 1 - t;
particle.position.copy(transformer.fluxCurve.getPointAt(t));
particle.scale.setScalar(0.75 + driveMagnitude * 0.5);
});
for (const cable of [
transformer.primaryCable,
transformer.outputCable
]) {
for (const pulse of cable.pulses) {
let t = (elapsed * 0.18 + pulse.offset) % 1;
if (drive < 0) t = 1 - t;
pulse.mesh.position.copy(cable.curve.getPointAt(t));
const relativeCurrent = cable.current / cable.maxCurrent;
pulse.mesh.scale.setScalar(
0.55 + Math.sqrt(relativeCurrent) * 0.7
);
}
}
}
updateCamera();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
updateReadouts();
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```