Prompt specification
Build or customize the Three.js 3D scene "Two resistors under one voltage" 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 resistors under one voltage
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-resistors-under-one-voltage-x1658
Scene Description Prompt:
Make a stylized Three.js scene in a self-contained HTML file using cdnjs: two resistors under one voltage. Place different model resistances across identical virtual voltage sources; show distinct currents and equal applied voltages without confusing physical resistor size with resistance.
Verify the behavior against the actual geometry and data in each relevant state. Use schematic labels for deliberately synthetic elements.
Technical checks: Initialize valid band positions for both visible resistors. Use a shared voltage and show distinct currents that match the selected resistances.
Return one complete HTML document. The file must run in a sandbox allowing inline scripts and the cdnjs Three.js r160 module. Construct procedural assets, use local event handlers and avoid remote assets, runtime network requests besides Three.js, pointer lock and native form submission. Keep the main requested geometry visible in the initial view.
Working Three.js Starter (two-resistors-under-one-voltage-x1658):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Resistor Model Lab — Equal Voltage, Different Current</title>
<style>
:root {
color-scheme: dark;
--bg: #07111e;
--panel: rgba(10, 22, 38, 0.86);
--line: rgba(173, 205, 238, 0.18);
--text: #edf7ff;
--muted: #9db1c8;
--a: #ffbd69;
--b: #58d7ff;
--source: #7ea8ff;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 15%, #152b43 0%, transparent 42%),
linear-gradient(180deg, #0a1727 0%, var(--bg) 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button { font: inherit; }
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(3, 9, 17, 0.4), transparent 22%, transparent 70%, rgba(3, 9, 17, 0.48)),
radial-gradient(circle at center, transparent 48%, rgba(2, 7, 14, 0.34) 100%);
z-index: 2;
}
.hud {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
header {
position: absolute;
top: 28px;
left: 30px;
width: min(490px, calc(100vw - 60px));
text-shadow: 0 2px 16px rgba(0, 0, 0, 0.55);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
color: #89a8c7;
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 28px;
height: 2px;
background: var(--source);
box-shadow: 0 0 12px rgba(126, 168, 255, 0.8);
}
h1 {
margin: 12px 0 10px;
font-size: clamp(30px, 4vw, 52px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 780;
}
h1 span {
color: #9ec5ff;
}
header p {
max-width: 465px;
margin: 0;
color: #b1c3d7;
font-size: 13px;
line-height: 1.55;
}
.law {
position: absolute;
top: 28px;
right: 30px;
width: 300px;
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(8, 20, 35, 0.7);
backdrop-filter: blur(14px);
box-shadow: 0 18px 45px rgba(0, 0, 0, 0.22);
}
.law-label {
color: #7994b2;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
}
.law strong {
display: block;
margin-top: 8px;
color: #dceaff;
font: 700 18px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.law strong span { color: #8db7ff; }
.law p {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
}
.scene-legend {
position: absolute;
left: 30px;
bottom: 166px;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: calc(100vw - 60px);
}
.chip {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 19, 33, 0.7);
color: #a9bdd2;
font-size: 11px;
backdrop-filter: blur(10px);
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #f3f8ff;
box-shadow: 0 0 10px currentColor;
}
.dot.body {
width: 19px;
height: 6px;
border-radius: 4px;
background: #d8c59e;
box-shadow: none;
}
.dock {
position: absolute;
right: 24px;
bottom: 22px;
left: 24px;
display: grid;
grid-template-columns: 220px minmax(0, 1fr) 150px;
gap: 12px;
align-items: stretch;
max-width: 1160px;
margin: 0 auto;
padding: 12px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 24px 65px rgba(0, 0, 0, 0.42);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.control-block,
.readout,
.transport {
border-radius: 13px;
background: rgba(255, 255, 255, 0.035);
}
.control-block {
padding: 12px 13px;
}
.control-title {
display: block;
margin-bottom: 9px;
color: #8098b3;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
}
.voltage-buttons {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
}
.voltage-buttons button {
min-height: 34px;
border: 1px solid rgba(142, 178, 218, 0.18);
border-radius: 8px;
background: rgba(11, 25, 43, 0.75);
color: #9eb2c8;
cursor: pointer;
font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
transition: 150ms ease;
}
.voltage-buttons button:hover {
border-color: rgba(142, 178, 218, 0.42);
color: white;
}
.voltage-buttons button.active {
border-color: #7ea8ff;
background: #214473;
color: #f2f7ff;
box-shadow: 0 0 18px rgba(87, 138, 230, 0.2);
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.readout {
position: relative;
min-width: 0;
padding: 11px 13px 10px;
overflow: hidden;
}
.readout::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 3px;
background: var(--accent);
box-shadow: 0 0 15px color-mix(in srgb, var(--accent), transparent 45%);
}
.readout-top {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 8px;
}
.branch-name {
color: var(--accent);
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.13em;
}
.resistance {
color: #e9f3ff;
font: 750 15px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.metrics {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 12px;
margin-top: 11px;
}
.metric span {
display: block;
color: #738da8;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
}
.metric strong {
display: block;
margin-top: 5px;
color: #f0f7ff;
font: 750 16px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.transport {
display: flex;
flex-direction: column;
justify-content: center;
gap: 7px;
padding: 10px;
}
.transport button {
min-height: 33px;
border: 1px solid rgba(142, 178, 218, 0.17);
border-radius: 8px;
background: rgba(10, 25, 43, 0.7);
color: #a9bed4;
cursor: pointer;
font-size: 11px;
transition: 150ms ease;
}
.transport button:hover {
color: white;
border-color: rgba(142, 178, 218, 0.4);
}
.camera-help {
position: absolute;
right: 30px;
bottom: 158px;
color: rgba(168, 190, 214, 0.65);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
#fatal {
position: fixed;
inset: 20px;
z-index: 20;
display: grid;
place-items: center;
border: 1px solid #753d4d;
border-radius: 18px;
background: #101722;
color: #ffd8df;
text-align: center;
}
#fatal[hidden] { display: none; }
@media (max-width: 900px) {
.law { display: none; }
.dock {
grid-template-columns: 185px 1fr;
}
.transport {
grid-column: 1 / -1;
flex-direction: row;
}
.transport button { flex: 1; }
.camera-help { display: none; }
.scene-legend { bottom: 202px; }
}
@media (max-width: 620px) {
header { top: 18px; left: 18px; width: calc(100vw - 36px); }
header p { max-width: 330px; font-size: 11px; }
h1 { font-size: 31px; }
.scene-legend { left: 18px; bottom: 238px; }
.chip { font-size: 9px; padding: 7px 9px; }
.dock {
right: 10px;
bottom: 10px;
left: 10px;
grid-template-columns: 1fr;
padding: 8px;
gap: 7px;
}
.control-block { padding: 9px 10px; }
.readouts { gap: 6px; }
.readout { padding: 9px 9px 8px; }
.resistance { font-size: 12px; }
.metrics { gap: 5px; margin-top: 8px; }
.metric strong { font-size: 12px; }
.transport { grid-column: auto; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="vignette"></div>
<main class="hud">
<header>
<div class="eyebrow">Model circuits / comparative study 01</div>
<h1>Same voltage.<br><span>Different current.</span></h1>
<p>Both loads use identical resistor-body geometry. Only the synthetic model resistance changes, so Ohm’s law produces two different currents.</p>
</header>
<aside class="law">
<div class="law-label">APPLIED VOLTAGE IS EQUAL</div>
<strong>V<sub>A</sub> = V<sub>B</sub> = <span id="equalVoltage">9.00 V</span></strong>
<p>Identical ideal sources · unequal R<sub>model</sub> · unequal I</p>
</aside>
<div class="scene-legend">
<div class="chip"><span class="dot"></span> Moving dots show current direction; speed is proportional to current</div>
<div class="chip"><span class="dot body"></span> Both resistor bodies have the same dimensions</div>
</div>
<div class="camera-help">Drag to orbit · wheel to zoom · double-click to reset</div>
<section class="dock" aria-label="Circuit model controls and readouts">
<div class="control-block">
<span class="control-title">SOURCE SETPOINT</span>
<div class="voltage-buttons" role="group" aria-label="Applied voltage">
<button type="button" data-voltage="5" aria-pressed="false">5 V</button>
<button type="button" data-voltage="9" class="active" aria-pressed="true">9 V</button>
<button type="button" data-voltage="12" aria-pressed="false">12 V</button>
</div>
</div>
<div class="readouts">
<article class="readout" style="--accent: var(--a)">
<div class="readout-top">
<span class="branch-name">LOAD A</span>
<span class="resistance">R<sub>model</sub> = 220 Ω</span>
</div>
<div class="metrics">
<div class="metric"><span>APPLIED V</span><strong id="voltageA">9.00 V</strong></div>
<div class="metric"><span>CURRENT I</span><strong id="currentA">40.91 mA</strong></div>
</div>
</article>
<article class="readout" style="--accent: var(--b)">
<div class="readout-top">
<span class="branch-name">LOAD B</span>
<span class="resistance">R<sub>model</sub> = 1.00 kΩ</span>
</div>
<div class="metrics">
<div class="metric"><span>APPLIED V</span><strong id="voltageB">9.00 V</strong></div>
<div class="metric"><span>CURRENT I</span><strong id="currentB">9.00 mA</strong></div>
</div>
</article>
</div>
<div class="transport">
<button type="button" id="flowToggle" aria-pressed="false">Pause current flow</button>
<button type="button" id="resetView">Reset camera</button>
</div>
</section>
</main>
<div id="fatal" hidden>
<div>
<strong>Three.js could not be initialized.</strong><br>
This scene requires access to the specified cdnjs Three.js module.
</div>
</div>
<script type="module">
const fatal = document.getElementById("fatal");
try {
const THREE = await import("https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js");
startScene(THREE);
} catch (error) {
console.error(error);
fatal.hidden = false;
}
function startScene(THREE) {
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
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;
stage.appendChild(renderer.domElement);
const canvas = renderer.domElement;
canvas.tabIndex = 0;
canvas.setAttribute("aria-label", "Interactive 3D comparison of two resistor models under equal voltage");
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x081421, 0.027);
const camera = new THREE.PerspectiveCamera(43, window.innerWidth / window.innerHeight, 0.1, 100);
const view = {
theta: 0.58,
phi: 1.03,
radius: 13.8,
target: new THREE.Vector3(0, 0.72, 0)
};
const hemisphere = new THREE.HemisphereLight(0xb7dcff, 0x18202a, 2.05);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd9eaff, 3.2);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4e8dff, 1.5);
rimLight.position.set(8, 6, -7);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffad64, 22, 9, 2);
warmLight.position.set(-5, 4, 2);
scene.add(warmLight);
const coolLight = new THREE.PointLight(0x4ed8ff, 22, 9, 2);
coolLight.position.set(5, 4, 2);
scene.add(coolLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({ color: 0x08131f, roughness: 0.88, metalness: 0.12 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.13;
floor.receiveShadow = true;
scene.add(floor);
const board = new THREE.Mesh(
new THREE.BoxGeometry(12.4, 0.2, 7.15),
new THREE.MeshStandardMaterial({
color: 0x10263a,
roughness: 0.62,
metalness: 0.28
})
);
board.position.y = 0;
board.castShadow = true;
board.receiveShadow = true;
scene.add(board);
const boardEdge = new THREE.LineSegments(
new THREE.EdgesGeometry(board.geometry),
new THREE.LineBasicMaterial({ color: 0x41617f, transparent: true, opacity: 0.55 })
);
boardEdge.position.copy(board.position);
scene.add(boardEdge);
const gridVertices = [];
for (let x = -6; x <= 6.001; x += 0.5) {
gridVertices.push(x, 0.106, -3.5, x, 0.106, 3.5);
}
for (let z = -3.5; z <= 3.501; z += 0.5) {
gridVertices.push(-6, 0.106, z, 6, 0.106, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute("position", new THREE.Float32BufferAttribute(gridVertices, 3));
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({ color: 0x83b4d7, transparent: true, opacity: 0.105 })
);
scene.add(grid);
const screwGeometry = new THREE.CylinderGeometry(0.075, 0.075, 0.035, 20);
const screwMaterial = new THREE.MeshStandardMaterial({
color: 0x7890a6,
roughness: 0.35,
metalness: 0.8
});
for (const [x, z] of [
[-5.95, -3.28], [5.95, -3.28], [-5.95, 3.28], [5.95, 3.28]
]) {
const screw = new THREE.Mesh(screwGeometry, screwMaterial);
screw.position.set(x, 0.13, z);
screw.castShadow = true;
scene.add(screw);
}
const branchDefs = [
{
id: "A",
x: -3.15,
resistance: 220,
resistanceText: "220 Ω",
accent: 0xffbd69,
accentCSS: "#ffbd69",
bands: [
{ color: 0xb7352a, x: -0.49, width: 0.11 }, // red: 2
{ color: 0xb7352a, x: -0.17, width: 0.11 }, // red: 2
{ color: 0x6b3f24, x: 0.15, width: 0.11 }, // brown: ×10
{ color: 0xd8aa3e, x: 0.50, width: 0.14 } // gold tolerance
]
},
{
id: "B",
x: 3.15,
resistance: 1000,
resistanceText: "1.00 kΩ",
accent: 0x58d7ff,
accentCSS: "#58d7ff",
bands: [
{ color: 0x6b3f24, x: -0.49, width: 0.11 }, // brown: 1
{ color: 0x15191e, x: -0.17, width: 0.11 }, // black: 0
{ color: 0xb7352a, x: 0.15, width: 0.11 }, // red: ×100
{ color: 0xd8aa3e, x: 0.50, width: 0.14 } // gold tolerance
]
}
];
function roundedRectPath(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createLabel() {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 768;
labelCanvas.height = 256;
const ctx = labelCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 30;
return { canvas: labelCanvas, ctx, texture, sprite };
}
function drawLabel(label, rows, accentCSS) {
const ctx = label.ctx;
const w = label.canvas.width;
const h = label.canvas.height;
ctx.clearRect(0, 0, w, h);
ctx.shadowColor = "rgba(0, 0, 0, 0.45)";
ctx.shadowBlur = 22;
ctx.shadowOffsetY = 8;
roundedRectPath(ctx, 7, 7, w - 14, h - 14, 22);
ctx.fillStyle = "rgba(7, 17, 30, 0.94)";
ctx.fill();
ctx.shadowColor = "transparent";
ctx.shadowBlur = 0;
ctx.shadowOffsetY = 0;
ctx.strokeStyle = accentCSS;
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accentCSS;
ctx.fillRect(28, 31, 6, h - 62);
let y = 49;
for (const row of rows) {
ctx.font = `${row.weight || 700} ${row.size || 28}px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace`;
ctx.fillStyle = row.color || "#dceaff";
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillText(row.text, 54, y);
y += row.gap || 58;
}
label.texture.needsUpdate = true;
}
function createFlatLabel(text, accentCSS, width = 2.65) {
const c = document.createElement("canvas");
c.width = 1024;
c.height = 128;
const ctx = c.getContext("2d");
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
toneMapped: false
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(width, width / 8), material);
mesh.rotation.x = -Math.PI / 2;
mesh.position.y = 0.118;
function redraw() {
ctx.clearRect(0, 0, c.width, c.height);
ctx.font = "700 38px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
ctx.fillStyle = accentCSS;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, c.width / 2, c.height / 2);
texture.needsUpdate = true;
}
redraw();
return mesh;
}
function addCylinderBetween(a, b, radius, material, parent = scene) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 16),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createSource(def) {
const group = new THREE.Group();
group.position.set(def.x, 0, 1.55);
scene.add(group);
const body = new THREE.Mesh(
new THREE.BoxGeometry(1.35, 0.9, 1.05),
new THREE.MeshStandardMaterial({
color: 0x193d68,
roughness: 0.38,
metalness: 0.42
})
);
body.position.y = 0.57;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry),
new THREE.LineBasicMaterial({ color: 0x6da8ff, transparent: true, opacity: 0.7 })
);
edges.position.copy(body.position);
group.add(edges);
const terminalGeometry = new THREE.CylinderGeometry(0.115, 0.115, 0.22, 20);
const positive = new THREE.Mesh(
terminalGeometry,
new THREE.MeshStandardMaterial({
color: 0xe6574b,
emissive: 0x7b1510,
emissiveIntensity: 0.8,
roughness: 0.28,
metalness: 0.65
})
);
positive.position.set(0.36, 1.12, 0);
positive.castShadow = true;
group.add(positive);
const negative = new THREE.Mesh(
terminalGeometry,
new THREE.MeshStandardMaterial({
color: 0xb8c7d8,
emissive: 0x26384b,
emissiveIntensity: 0.6,
roughness: 0.3,
metalness: 0.7
})
);
negative.position.set(-0.36, 1.12, 0);
negative.castShadow = true;
group.add(negative);
function terminalText(text, x, color) {
const c = document.createElement("canvas");
c.width = 128;
c.height = 128;
const ctx = c.getContext("2d");
ctx.font = "800 78px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = color;
ctx.fillText(text, 64, 64);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
toneMapped: false
}));
sprite.position.set(x, 1.43, 0.05);
sprite.scale.set(0.38, 0.38, 1);
sprite.renderOrder = 31;
group.add(sprite);
}
terminalText("+", 0.36, "#ff756b");
terminalText("−", -0.36, "#d8e6f5");
const label = createLabel();
label.sprite.position.set(def.x, 1.94, 1.55);
label.sprite.scale.set(2.72, 0.91, 1);
scene.add(label.sprite);
return { group, body, label };
}
function createResistor(def) {
const group = new THREE.Group();
group.position.set(def.x, 1.03, -1.35);
scene.add(group);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: 0xd8c49a,
roughness: 0.56,
metalness: 0.04
});
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.25, 1.55, 32),
bodyMaterial
);
body.rotation.z = Math.PI / 2;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const endMaterial = new THREE.MeshStandardMaterial({
color: 0xb99e72,
roughness: 0.48,
metalness: 0.08
});
for (const x of [-0.76, 0.76]) {
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.265, 0.265, 0.065, 32),
endMaterial
);
cap.rotation.z = Math.PI / 2;
cap.position.x = x;
cap.castShadow = true;
group.add(cap);
}
const leadMaterial = new THREE.MeshStandardMaterial({
color: 0xb9c7d2,
roughness: 0.25,
metalness: 0.88
});
for (const x of [-1.025, 1.025]) {
const lead = new THREE.Mesh(
new THREE.CylinderGeometry(0.036, 0.036, 0.55, 16),
leadMaterial
);
lead.rotation.z = Math.PI / 2;
lead.position.x = x;
lead.castShadow = true;
group.add(lead);
}
// Every band has an explicit local X position. The same body and
// band spacing are used for both models, so size does not encode R.
const bands = def.bands;
bands.forEach((bandSpec) => {
const band = new THREE.Mesh(
new THREE.CylinderGeometry(0.258, 0.258, bandSpec.width, 32),
new THREE.MeshStandardMaterial({
color: bandSpec.color,
roughness: bandSpec.color === 0xd8aa3e ? 0.28 : 0.44,
metalness: bandSpec.color === 0xd8aa3e ? 0.72 : 0.12
})
);
band.rotation.z = Math.PI / 2;
band.position.x = bandSpec.x;
band.castShadow = true;
group.add(band);
});
const label = createLabel();
label.sprite.position.set(def.x, 2.28, -1.35);
label.sprite.scale.set(3.25, 1.08, 1);
scene.add(label.sprite);
const dimensionMaterial = new THREE.LineBasicMaterial({
color: def.accent,
transparent: true,
opacity: 0.72
});
const dimensionGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(def.x - 0.775, 0.125, -1.9),
new THREE.Vector3(def.x + 0.775, 0.125, -1.9),
new THREE.Vector3(def.x - 0.775, 0.125, -2.02),
new THREE.Vector3(def.x - 0.775, 0.125, -1.78),
new THREE.Vector3(def.x + 0.775, 0.125, -2.02),
new THREE.Vector3(def.x + 0.775, 0.125, -1.78)
]);
scene.add(new THREE.LineSegments(dimensionGeometry, dimensionMaterial));
const flatLabel = createFlatLabel("IDENTICAL BODY SIZE", def.accentCSS, 2.35);
flatLabel.position.set(def.x, 0.122, -2.28);
scene.add(flatLabel);
return { group, body, label };
}
function pathMetrics(points) {
const cumulative = [0];
let total = 0;
for (let i = 1; i < points.length; i++) {
total += points[i].distanceTo(points[i - 1]);
cumulative.push(total);
}
return { cumulative, total };
}
function samplePath(points, metrics, distance) {
const d = ((distance % metrics.total) + metrics.total) % metrics.total;
for (let i = 1; i < points.length; i++) {
if (d <= metrics.cumulative[i]) {
const segmentLength = metrics.cumulative[i] - metrics.cumulative[i - 1];
const t = segmentLength === 0 ? 0 : (d - metrics.cumulative[i - 1]) / segmentLength;
return new THREE.Vector3().lerpVectors(points[i - 1], points[i], t);
}
}
return points[0].clone();
}
function createBranch(def) {
const source = createSource(def);
const resistor = createResistor(def);
const wireMaterial = new THREE.MeshStandardMaterial({
color: 0x7890a6,
roughness: 0.42,
metalness: 0.52
});
const jointMaterial = new THREE.MeshStandardMaterial({
color: 0xaab9c7,
roughness: 0.35,
metalness: 0.6
});
const points = [
new THREE.Vector3(def.x + 0.36, 1.24, 1.55),
new THREE.Vector3(def.x + 1.55, 1.24, 1.55),
new THREE.Vector3(def.x + 1.55, 0.36, 1.55),
new THREE.Vector3(def.x + 1.55, 0.36, -1.35),
new THREE.Vector3(def.x + 1.30, 0.36, -1.35),
new THREE.Vector3(def.x + 1.30, 1.03, -1.35),
new THREE.Vector3(def.x + 0.775, 1.03, -1.35),
new THREE.Vector3(def.x, 1.03, -1.35),
new THREE.Vector3(def.x - 0.775, 1.03, -1.35),
new THREE.Vector3(def.x - 1.30, 1.03, -1.35),
new THREE.Vector3(def.x - 1.30, 0.36, -1.35),
new THREE.Vector3(def.x - 1.55, 0.36, -1.35),
new THREE.Vector3(def.x - 1.55, 0.36, 1.55),
new THREE.Vector3(def.x - 1.55, 1.24, 1.55),
new THREE.Vector3(def.x - 0.36, 1.24, 1.55)
];
// Draw the external conductors. The two horizontal lead sections
// and the resistor itself are separate, physically visible geometry.
for (const [start, end] of [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [9, 10], [10, 11], [11, 12], [12, 13], [13, 14]]) {
addCylinderBetween(points[start], points[end], 0.036, wireMaterial);
}
for (const index of [1, 2, 3, 4, 10, 11, 12, 13]) {
const joint = new THREE.Mesh(new THREE.SphereGeometry(0.057, 16, 12), jointMaterial);
joint.position.copy(points[index]);
joint.castShadow = true;
scene.add(joint);
}
const arrowMaterial = new THREE.MeshStandardMaterial({
color: def.accent,
emissive: def.accent,
emissiveIntensity: 0.85,
roughness: 0.35
});
const rightArrow = new THREE.Mesh(new THREE.ConeGeometry(0.105, 0.28, 18), arrowMaterial);
rightArrow.rotation.z = -Math.PI / 2;
rightArrow.position.set(def.x + 0.98, 1.25, 1.55);
scene.add(rightArrow);
const leftArrow = new THREE.Mesh(new THREE.ConeGeometry(0.105, 0.28, 18), arrowMaterial);
leftArrow.rotation.z = Math.PI / 2;
leftArrow.position.set(def.x - 1.30, 0.38, 1.55);
scene.add(leftArrow);
const metrics = pathMetrics(points);
const particleMaterial = new THREE.MeshStandardMaterial({
color: def.accent,
emissive: def.accent,
emissiveIntensity: 2.2,
roughness: 0.2
});
const particles = [];
for (let i = 0; i < 8; i++) {
const particle = new THREE.Mesh(new THREE.SphereGeometry(0.072, 16, 12), particleMaterial);
particle.castShadow = false;
scene.add(particle);
particles.push(particle);
}
return {
def,
source,
resistor,
points,
metrics,
particles,
current: 0,
speed: 0
};
}
const branches = branchDefs.map(createBranch);
function formatCurrent(amperes) {
const milliamps = amperes * 1000;
if (amperes >= 1) return `${amperes.toFixed(2)} A`;
if (milliamps >= 10) return `${milliamps.toFixed(2)} mA`;
return `${milliamps.toFixed(2)} mA`;
}
let appliedVoltage = 9;
let flowTime = 0;
let flowPaused = false;
function updateReadouts() {
document.getElementById("equalVoltage").textContent = `${appliedVoltage.toFixed(2)} V`;
for (const branch of branches) {
const current = appliedVoltage / branch.def.resistance;
branch.current = current;
branch.speed = current * 50;
drawLabel(branch.source.label, [
{ text: "SYNTHETIC IDEAL SOURCE", size: 27, color: "#8fb8ff", gap: 49 },
{ text: `${appliedVoltage.toFixed(2)} V`, size: 50, color: "#f0f6ff", gap: 60 },
{ text: "IDENTICAL VIRTUAL SOURCE", size: 24, color: "#9fb2c8" }
], "#7ea8ff");
drawLabel(branch.resistor.label, [
{ text: `LOAD ${branch.def.id} · SYNTHETIC MODEL`, size: 25, color: branch.def.accentCSS, gap: 48 },
{ text: `R = ${branch.def.resistanceText}`, size: 45, color: "#f3f7fc", gap: 58 },
{ text: `V ${appliedVoltage.toFixed(2)} V · I ${formatCurrent(current)}`, size: 26, color: "#b7c8da" }
], branch.def.accentCSS);
document.getElementById(`voltage${branch.def.id}`).textContent = `${appliedVoltage.toFixed(2)} V`;
document.getElementById(`current${branch.def.id}`).textContent = formatCurrent(current);
}
}
const voltageButtons = [...document.querySelectorAll("[data-voltage]")];
voltageButtons.forEach((button) => {
button.addEventListener("click", () => {
appliedVoltage = Number(button.dataset.voltage);
voltageButtons.forEach((other) => {
const active = other === button;
other.classList.toggle("active", active);
other.setAttribute("aria-pressed", String(active));
});
updateReadouts();
});
});
const flowToggle = document.getElementById("flowToggle");
flowToggle.addEventListener("click", () => {
flowPaused = !flowPaused;
flowToggle.textContent = flowPaused ? "Resume current flow" : "Pause current flow";
flowToggle.setAttribute("aria-pressed", String(flowPaused));
});
function resetView() {
view.theta = 0.58;
view.phi = 1.03;
view.radius = window.innerWidth / window.innerHeight < 0.72 ? 32 : 13.8;
view.target.set(0, 0.72, 0);
}
document.getElementById("resetView").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
// Direct, dependency-free orbit and zoom controls.
let dragging = false;
let pointerId = null;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerId = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(pointerId);
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== pointerId) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(view.phi - dy * 0.005, 0.38, 1.43);
});
function endDrag(event) {
if (!dragging || (event.pointerId !== undefined && event.pointerId !== pointerId)) return;
dragging = false;
if (pointerId !== null && canvas.hasPointerCapture(pointerId)) {
canvas.releasePointerCapture(pointerId);
}
pointerId = null;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("lostpointercapture", () => {
dragging = false;
pointerId = null;
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
7,
42
);
}, { passive: false });
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft": view.theta += 0.09; break;
case "ArrowRight": view.theta -= 0.09; break;
case "ArrowUp": view.phi = Math.max(0.38, view.phi - 0.07); break;
case "ArrowDown": view.phi = Math.min(1.43, view.phi + 0.07); break;
case "+": case "=": view.radius = Math.max(7, view.radius * 0.9); break;
case "-": view.radius = Math.min(42, view.radius * 1.1); break;
case "Home": resetView(); break;
default: handled = false;
}
if (handled) event.preventDefault();
});
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
view.target.x + view.radius * sinPhi * Math.sin(view.theta),
view.target.y + view.radius * Math.cos(view.phi),
view.target.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(view.target);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
if (window.innerWidth / window.innerHeight < 0.72) view.radius = 32;
updateReadouts();
updateCamera();
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!flowPaused) flowTime += delta;
for (const branch of branches) {
branch.particles.forEach((particle, index) => {
const offset = (index / branch.particles.length) * branch.metrics.total;
const position = samplePath(
branch.points,
branch.metrics,
flowTime * branch.speed + offset
);
particle.position.copy(position);
});
}
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
}
</script>
</body>
</html>
```