Prompt specification
Build or customize the Three.js 3D scene "Two-coil transformer core model" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Two-coil transformer core model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-coil-transformer-core-model-x1673
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing an ideal two-coil transformer. Build primary and secondary coils around a closed magnetic-core shape with labeled turn counts; show alternating flux and state the ideal linked-flux approximation. Low-poly.
Working Three.js Starter (two-coil-transformer-core-model-x1673):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ideal Two-Coil Transformer</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #090d18;
color: #edf2ff;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.title-card {
position: absolute;
top: 22px;
left: 24px;
max-width: 430px;
}
.eyebrow {
margin-bottom: 7px;
color: #94a3ff;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3.2vw, 42px);
line-height: 1.05;
letter-spacing: -0.035em;
text-shadow: 0 3px 20px rgba(0, 0, 0, 0.35);
}
.subtitle {
margin-top: 10px;
max-width: 390px;
color: #aab4cc;
font-size: 13px;
line-height: 1.5;
}
.formula-card {
position: absolute;
left: 24px;
bottom: 24px;
width: min(390px, calc(100vw - 48px));
padding: 17px 19px;
border: 1px solid rgba(157, 171, 216, 0.23);
border-radius: 15px;
background: linear-gradient(
145deg,
rgba(20, 27, 47, 0.91),
rgba(12, 17, 32, 0.83)
);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(12px);
}
.formula-card .eyebrow {
margin-bottom: 10px;
color: #a9b6ff;
}
.equation {
margin: 7px 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
line-height: 1.25;
color: #ffffff;
}
.equation.small {
font-family: inherit;
font-size: 13px;
line-height: 1.55;
color: #bcc6df;
}
.equation .accent {
color: #ffc266;
}
.equation .cool {
color: #65c7ff;
}
.assumptions {
margin-top: 9px;
padding-top: 10px;
border-top: 1px solid rgba(157, 171, 216, 0.16);
color: #8f9ab5;
font-size: 11px;
line-height: 1.45;
}
.legend-card {
position: absolute;
top: 24px;
right: 24px;
min-width: 210px;
padding: 13px 15px;
border: 1px solid rgba(157, 171, 216, 0.18);
border-radius: 13px;
background: rgba(12, 17, 31, 0.72);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(10px);
font-size: 11px;
color: #aab4cc;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 6px 0;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.swatch.positive {
color: #ffad55;
background: #ffad55;
}
.swatch.negative {
color: #62c5ff;
background: #62c5ff;
}
.swatch.primary {
color: #ff7657;
background: #ff7657;
}
.swatch.secondary {
color: #a78bfa;
background: #a78bfa;
}
.controls {
position: absolute;
right: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.hint {
color: #7f8aa4;
font-size: 11px;
text-align: right;
line-height: 1.5;
}
button {
border: 1px solid rgba(169, 182, 255, 0.35);
border-radius: 10px;
padding: 9px 12px;
background: rgba(91, 105, 163, 0.22);
color: #e7ebff;
font: inherit;
font-size: 11px;
cursor: pointer;
backdrop-filter: blur(8px);
}
button:hover {
background: rgba(112, 128, 196, 0.34);
border-color: rgba(169, 182, 255, 0.62);
}
@media (max-width: 760px) {
.title-card {
top: 16px;
left: 16px;
}
.subtitle {
display: none;
}
.formula-card {
left: 14px;
bottom: 14px;
width: calc(100vw - 28px);
padding: 13px 15px;
}
.formula-card .assumptions {
display: none;
}
.equation {
font-size: 17px;
}
.legend-card {
top: 15px;
right: 14px;
min-width: 0;
padding: 9px 11px;
}
.legend-card .legend-heading {
display: none;
}
.controls {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly ideal transformer diagram"></canvas>
<div class="ui">
<section class="title-card">
<div class="eyebrow">Low-poly electromagnetic model</div>
<h1>Ideal Two-Coil Transformer</h1>
<div class="subtitle">
A closed magnetic core carries one shared alternating flux. The primary
and secondary windings occupy opposite limbs of the same magnetic circuit.
</div>
</section>
<section class="formula-card">
<div class="eyebrow">Ideal linked-flux approximation</div>
<div class="equation">
Φ<sub>p</sub>(t) ≈ Φ<sub>s</sub>(t) = Φ(t)
</div>
<div class="equation small">
Φ(t) = Φ<sub>max</sub> sin(ωt)
</div>
<div class="equation small">
λ<sub>p</sub> = N<sub>p</sub>Φ,
λ<sub>s</sub> = N<sub>s</sub>Φ
</div>
<div class="equation">
V<sub>p</sub> / V<sub>s</sub> =
N<sub>p</sub> / N<sub>s</sub> =
<span class="accent">32 / 16 = 2</span>
</div>
<div class="assumptions">
Ideal approximation: leakage flux, fringing, winding resistance,
magnetizing current, and core loss are neglected.
</div>
</section>
<aside class="legend-card">
<div class="legend-heading eyebrow" style="margin-bottom:7px">
Flux and windings
</div>
<div class="legend-row">
<span class="swatch positive"></span>
<span>Positive flux, +Φ</span>
</div>
<div class="legend-row">
<span class="swatch negative"></span>
<span>Negative flux, −Φ</span>
</div>
<div class="legend-row">
<span class="swatch primary"></span>
<span>Primary winding</span>
</div>
<div class="legend-row">
<span class="swatch secondary"></span>
<span>Secondary winding</span>
</div>
</aside>
<div class="controls">
<div class="hint">
Drag to orbit<br />
Scroll to zoom
</div>
<button id="reset-view" type="button">Reset view</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const resetButton = document.querySelector("#reset-view");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x090d18);
scene.fog = new THREE.Fog(0x090d18, 13, 25);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Direct camera controls, implemented without Three.js addons.
const target = new THREE.Vector3(0, 0.05, 0);
const initialView = {
radius: 9.3,
theta: 0.63,
phi: 1.08
};
const desiredView = { ...initialView };
const currentView = { ...initialView };
function updateCamera() {
const sinPhi = Math.sin(currentView.phi);
camera.position.set(
target.x + currentView.radius * sinPhi * Math.sin(currentView.theta),
target.y + currentView.radius * Math.cos(currentView.phi),
target.z + currentView.radius * sinPhi * Math.cos(currentView.theta)
);
camera.lookAt(target);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desiredView.theta -= dx * 0.006;
desiredView.phi = THREE.MathUtils.clamp(
desiredView.phi - dy * 0.006,
0.52,
1.46
);
});
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();
desiredView.radius = THREE.MathUtils.clamp(
desiredView.radius * Math.exp(event.deltaY * 0.001),
6.2,
14.5
);
},
{ passive: false }
);
resetButton.addEventListener("click", () => {
Object.assign(desiredView, initialView);
});
// Lighting.
scene.add(new THREE.HemisphereLight(0xc9dcff, 0x282033, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(5.5, 8.0, 7.0);
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;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x86a8ff, 1.35);
fillLight.position.set(-5, 2, 5);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xff8a5c, 0.9);
rimLight.position.set(2, 3, -6);
scene.add(rimLight);
// Ground and low-poly platform.
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0d1220,
roughness: 0.95,
metalness: 0.05
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.18;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(4.25, 4.5, 0.16, 24),
new THREE.MeshStandardMaterial({
color: 0x161d2e,
roughness: 0.72,
metalness: 0.22,
flatShading: true
})
);
platform.position.y = -2.08;
platform.receiveShadow = true;
scene.add(platform);
const grid = new THREE.GridHelper(16, 16, 0x39445f, 0x222a3c);
grid.position.y = -2.155;
grid.material.transparent = true;
grid.material.opacity = 0.27;
scene.add(grid);
// Closed square magnetic core.
const coreMaterial = new THREE.MeshStandardMaterial({
color: 0x4c566b,
roughness: 0.72,
metalness: 0.2,
flatShading: true,
emissive: 0x000000
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x98a4bc,
transparent: true,
opacity: 0.23
});
function addCoreBlock(size, position) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, coreMaterial);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
mesh.add(edges);
}
const verticalCoreSize = [0.62, 3.40, 0.72];
const horizontalCoreSize = [4.62, 0.62, 0.72];
addCoreBlock(verticalCoreSize, [-2.0, 0, 0]);
addCoreBlock(verticalCoreSize, [ 2.0, 0, 0]);
addCoreBlock(horizontalCoreSize, [0, 1.39, 0]);
addCoreBlock(horizontalCoreSize, [0, -1.39, 0]);
// A helix curve used for low-poly tubular windings.
class HelixCurve extends THREE.Curve {
constructor(centerX, radius, bottomY, height, turns, phase = 0) {
super();
this.centerX = centerX;
this.radius = radius;
this.bottomY = bottomY;
this.height = height;
this.turns = turns;
this.phase = phase;
}
getPoint(t, optionalTarget = new THREE.Vector3()) {
const theta = this.phase + t * this.turns * Math.PI * 2;
return optionalTarget.set(
this.centerX + this.radius * Math.cos(theta),
this.bottomY + t * this.height,
this.radius * Math.sin(theta)
);
}
}
const primaryMaterial = new THREE.MeshStandardMaterial({
color: 0xff7657,
roughness: 0.38,
metalness: 0.62,
flatShading: true,
emissive: 0x3b0900,
emissiveIntensity: 0.18
});
const secondaryMaterial = new THREE.MeshStandardMaterial({
color: 0xa78bfa,
roughness: 0.36,
metalness: 0.6,
flatShading: true,
emissive: 0x160b38,
emissiveIntensity: 0.18
});
function createCoil(centerX, turns, material) {
const radius = 0.54;
const height = 2.18;
const bottomY = -height / 2;
const phase = Math.PI / 2;
const curve = new HelixCurve(
centerX,
radius,
bottomY,
height,
turns,
phase
);
const geometry = new THREE.TubeGeometry(
curve,
turns * 8,
0.034,
5,
false
);
const coil = new THREE.Mesh(geometry, material);
coil.castShadow = true;
coil.receiveShadow = true;
scene.add(coil);
}
createCoil(-2.0, 32, primaryMaterial);
createCoil( 2.0, 16, secondaryMaterial);
// Canvas-based sprite labels; no CSS2DRenderer or other addon is used.
function makeLabel(title, subtitle, accent = "#9eafff") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 768;
labelCanvas.height = 220;
const context = labelCanvas.getContext("2d");
function roundedRect(ctx, x, y, w, h, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + w, y, x + w, y + h, radius);
ctx.arcTo(x + w, y + h, x, y + h, radius);
ctx.arcTo(x, y + h, x, y, radius);
ctx.arcTo(x, y, x + w, y, radius);
ctx.closePath();
}
context.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
context.shadowColor = "rgba(0,0,0,0.42)";
context.shadowBlur = 22;
context.shadowOffsetY = 8;
roundedRect(
context,
10,
10,
labelCanvas.width - 20,
labelCanvas.height - 20,
28
);
context.fillStyle = "rgba(12,17,31,0.88)";
context.fill();
context.shadowColor = "transparent";
context.fillStyle = accent;
context.fillRect(34, 38, 7, 142);
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = "#ffffff";
context.font = "800 44px system-ui, sans-serif";
context.fillText(title, labelCanvas.width / 2 + 4, 82);
context.fillStyle = accent;
context.font = "600 31px system-ui, sans-serif";
context.fillText(subtitle, labelCanvas.width / 2 + 4, 145);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.62, 2.62 * 220 / 768, 1);
sprite.renderOrder = 20;
return sprite;
}
const primaryLabel = makeLabel(
"PRIMARY · AC",
"Nₚ = 32 turns",
"#ff8c70"
);
primaryLabel.position.set(-2.05, 2.18, 0.72);
scene.add(primaryLabel);
const secondaryLabel = makeLabel(
"SECONDARY",
"Nₛ = 16 turns",
"#b8a1ff"
);
secondaryLabel.position.set(2.05, 2.18, 0.72);
scene.add(secondaryLabel);
const fluxLabel = makeLabel(
"ALTERNATING CORE FLUX",
"Φ(t) = Φmax sin(ωt)",
"#ffc266"
);
fluxLabel.scale.set(2.95, 2.95 * 220 / 768, 1);
fluxLabel.position.set(0, 0.28, 0.72);
scene.add(fluxLabel);
// Closed magnetic path through the four core limbs.
const loopPoints = [
new THREE.Vector2( 2.0, 1.39),
new THREE.Vector2(-2.0, 1.39),
new THREE.Vector2(-2.0, -1.39),
new THREE.Vector2( 2.0, -1.39)
];
const segmentLengths = loopPoints.map((point, index) => {
const next = loopPoints[(index + 1) % loopPoints.length];
return point.distanceTo(next);
});
const loopLength = segmentLengths.reduce((sum, length) => sum + length, 0);
function sampleLoop(u, offset = 0) {
let distance = ((u + offset) % 1 + 1) % 1 * loopLength;
for (let i = 0; i < loopPoints.length; i++) {
const start = loopPoints[i];
const end = loopPoints[(i + 1) % loopPoints.length];
const length = segmentLengths[i];
if (distance <= length) {
const t = distance / length;
return {
position: new THREE.Vector2(
THREE.MathUtils.lerp(start.x, end.x, t),
THREE.MathUtils.lerp(start.y, end.y, t)
),
tangent: new THREE.Vector2(
end.x - start.x,
end.y - start.y
).normalize()
};
}
distance -= length;
}
return {
position: loopPoints[0].clone(),
tangent: new THREE.Vector2(-1, 0)
};
}
const pathPositions = [];
for (let i = 0; i <= 160; i++) {
const sample = sampleLoop(i / 160);
pathPositions.push(
new THREE.Vector3(sample.position.x, sample.position.y, 0.395)
);
}
const fluxPath = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(pathPositions),
new THREE.LineDashedMaterial({
color: 0xffa14a,
dashSize: 0.12,
gapSize: 0.08,
transparent: true,
opacity: 0.7
})
);
fluxPath.computeLineDistances();
scene.add(fluxPath);
// Low-poly cone arrows show the circulating flux and reverse direction.
const fluxArrowMaterial = new THREE.MeshBasicMaterial({
color: 0xffa14a,
transparent: true,
opacity: 0.96
});
const fluxArrowGeometry = new THREE.ConeGeometry(0.105, 0.29, 4);
const fluxArrows = [];
const arrowCount = 28;
for (let i = 0; i < arrowCount; i++) {
const arrow = new THREE.Mesh(fluxArrowGeometry, fluxArrowMaterial);
arrow.renderOrder = 5;
scene.add(arrow);
fluxArrows.push(arrow);
}
const positiveFluxColor = new THREE.Color(0xffa14a);
const negativeFluxColor = new THREE.Color(0x55c4ff);
const neutralFluxColor = new THREE.Color(0xd8e3ff);
const currentFluxColor = new THREE.Color();
const up = new THREE.Vector3(0, 1, 0);
const tangent3 = new THREE.Vector3();
function fract(value) {
return value - Math.floor(value);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
// Smooth the directly controlled camera values.
currentView.radius +=
(desiredView.radius - currentView.radius) * 0.11;
currentView.theta +=
(desiredView.theta - currentView.theta) * 0.11;
currentView.phi +=
(desiredView.phi - currentView.phi) * 0.11;
updateCamera();
// Φ(t) = Φmax sin(ωt).
const omega = 1.8;
const flux = Math.sin(elapsed * omega);
const rate = Math.cos(elapsed * omega);
if (flux >= 0) {
currentFluxColor
.copy(neutralFluxColor)
.lerp(positiveFluxColor, flux);
} else {
currentFluxColor
.copy(neutralFluxColor)
.lerp(negativeFluxColor, -flux);
}
fluxArrowMaterial.color.copy(currentFluxColor);
fluxPath.material.color.copy(currentFluxColor);
coreMaterial.emissive
.copy(currentFluxColor)
.multiplyScalar(0.08 + 0.12 * Math.abs(flux));
// The integrated visual phase advances with the derivative of Φ,
// so the arrows reverse when the alternating flux changes sign.
const travel = 0.18 * flux;
const direction = rate >= 0 ? 1 : -1;
const arrowScale = 0.82 + 0.28 * Math.abs(flux);
fluxArrows.forEach((arrow, index) => {
const base = index / fluxArrows.length;
const u = fract(base + travel);
const sample = sampleLoop(u);
arrow.position.set(
sample.position.x,
sample.position.y,
0.66
);
tangent3.set(
sample.tangent.x * direction,
sample.tangent.y * direction,
0
).normalize();
arrow.quaternion.setFromUnitVectors(up, tangent3);
arrow.scale.setScalar(arrowScale);
});
renderer.render(scene, camera);
}
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```