Prompt specification
Build or customize the Three.js 3D scene "Virtual diode direction test board" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Virtual diode direction test board
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/virtual-diode-direction-test-board-x1617
Scene Description Prompt:
Create a standalone low-poly scene in one HTML file using Three.js from cdnjs. Build a symbolic diode between a reversible low-voltage virtual source and resistor; show current according to an explicitly chosen ideal or threshold model.
Working Three.js Starter (virtual-diode-direction-test-board-x1617):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Diode Lab</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(10, 18, 31, 0.88);
--border: rgba(151, 206, 255, 0.22);
--text: #eaf4ff;
--muted: #91a7bd;
--cyan: #7de8ff;
--amber: #ffc45a;
--green: #70f0ad;
--red: #ff7373;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #08111e;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 20px;
left: 20px;
width: min(350px, calc(100vw - 40px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.36),
inset 0 1px 0 rgba(255, 255, 255, 0.05);
backdrop-filter: blur(14px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0 0 5px;
font-size: 23px;
line-height: 1.1;
}
.subtitle {
margin: 0 0 17px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.control {
margin-top: 15px;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
font-size: 13px;
font-weight: 700;
}
output,
.readout-value {
color: var(--amber);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
accent-color: var(--amber);
cursor: pointer;
}
select {
width: 100%;
padding: 10px 11px;
border: 1px solid var(--border);
border-radius: 9px;
outline: none;
background: #111d2d;
color: var(--text);
font: inherit;
font-size: 13px;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
button {
padding: 9px 10px;
border: 1px solid var(--border);
border-radius: 9px;
background: #17263a;
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition:
background 140ms ease,
transform 140ms ease;
}
button:hover {
background: #21364f;
transform: translateY(-1px);
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
.readout {
min-width: 0;
padding: 10px;
border: 1px solid rgba(151, 206, 255, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
}
.readout-label {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.readout-value {
display: block;
overflow: hidden;
color: var(--cyan);
font-size: 14px;
font-weight: 800;
text-overflow: ellipsis;
white-space: nowrap;
}
.formula {
margin-top: 13px;
padding: 10px 11px;
border-left: 3px solid var(--cyan);
border-radius: 5px;
background: rgba(125, 232, 255, 0.07);
color: #c9e4f7;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
line-height: 1.5;
}
#camera-hint {
position: fixed;
z-index: 10;
right: 20px;
bottom: 20px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(10, 18, 31, 0.72);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(10px);
pointer-events: none;
}
@media (max-width: 650px) {
.panel {
top: 10px;
left: 10px;
width: min(330px, calc(100vw - 20px));
padding: 14px;
}
h1 {
font-size: 20px;
}
.subtitle {
margin-bottom: 12px;
}
#camera-hint {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive low-poly diode circuit"></div>
<section class="panel" aria-label="Circuit controls and measurements">
<div class="eyebrow">Interactive circuit</div>
<h1>Diode Lab</h1>
<p class="subtitle">
A reversible low-voltage source drives a 100 Ω resistor and
a series diode.
</p>
<div class="control">
<div class="control-row">
<label for="voltage">Source voltage</label>
<output id="voltage-output" for="voltage">+2.0 V</output>
</div>
<input
id="voltage"
type="range"
min="-5"
max="5"
step="0.1"
value="2"
>
</div>
<div class="control">
<div class="control-row">
<label for="model">Diode model</label>
</div>
<select id="model">
<option value="ideal">Ideal diode — V<sub>f</sub> = 0 V</option>
<option value="threshold" selected>
Constant-drop silicon — V<sub>f</sub> = 0.70 V
</option>
</select>
<div class="button-row">
<button id="reverse" type="button">Reverse polarity</button>
<button id="zero" type="button">Set to 0 V</button>
</div>
</div>
<div class="readouts" aria-live="polite">
<div class="readout">
<span class="readout-label">Current</span>
<span class="readout-value" id="current-output">13.0 mA</span>
</div>
<div class="readout">
<span class="readout-label">Diode state</span>
<span class="readout-value" id="state-output">CONDUCTING</span>
</div>
<div class="readout">
<span class="readout-label">Diode voltage</span>
<span class="readout-value" id="diode-voltage-output">0.70 V</span>
</div>
<div class="readout">
<span class="readout-label">Resistor voltage</span>
<span class="readout-value" id="resistor-voltage-output">1.30 V</span>
</div>
</div>
<div class="formula" id="model-note"></div>
</section>
<div id="camera-hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const RESISTANCE = 100; // ohms
const THRESHOLD_VOLTAGE = 0.70; // volts
const voltageInput = document.querySelector("#voltage");
const modelInput = document.querySelector("#model");
const voltageOutput = document.querySelector("#voltage-output");
const currentOutput = document.querySelector("#current-output");
const stateOutput = document.querySelector("#state-output");
const diodeVoltageOutput = document.querySelector("#diode-voltage-output");
const resistorVoltageOutput = document.querySelector("#resistor-voltage-output");
const modelNote = document.querySelector("#model-note");
const sceneHost = document.querySelector("#scene");
// ---------------------------------------------------------------------
// Renderer, scene, lighting, and camera
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111e);
scene.fog = new THREE.Fog(0x08111e, 18, 36);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xbadfff, 0x172033, 1.8);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d6, 3.1);
keyLight.position.set(-5, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x48bfff, 1.6);
rimLight.position.set(8, 5, -8);
scene.add(rimLight);
const cameraTarget = new THREE.Vector3(0, 0.35, 0);
let cameraYaw = 0.66;
let cameraPolar = 0.88;
let cameraRadius = 13.4;
function updateCamera() {
const horizontalRadius = Math.sin(cameraPolar) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontalRadius * Math.sin(cameraYaw),
cameraTarget.y + Math.cos(cameraPolar) * cameraRadius,
cameraTarget.z + horizontalRadius * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0.66;
cameraPolar = 0.88;
cameraRadius = 13.4;
updateCamera();
}
updateCamera();
// Direct camera controls implemented without Three.js addons.
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - deltaY * 0.005,
0.32,
1.38
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
7.2,
21
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// Materials and reusable geometry helpers
// ---------------------------------------------------------------------
const materials = {
board: new THREE.MeshStandardMaterial({
color: 0x123c3b,
roughness: 0.7,
metalness: 0.22
}),
boardEdge: new THREE.MeshStandardMaterial({
color: 0x0a282b,
roughness: 0.78,
metalness: 0.18
}),
ground: new THREE.MeshStandardMaterial({
color: 0x0b1523,
roughness: 0.94,
metalness: 0.02
}),
wire: new THREE.MeshStandardMaterial({
color: 0xc78642,
roughness: 0.34,
metalness: 0.72
}),
resistor: new THREE.MeshStandardMaterial({
color: 0xe6c38f,
roughness: 0.62,
metalness: 0.04
}),
resistorBand: new THREE.MeshStandardMaterial({
color: 0x8c3c2d,
roughness: 0.56
}),
resistorBand2: new THREE.MeshStandardMaterial({
color: 0x343b48,
roughness: 0.58
}),
battery: new THREE.MeshStandardMaterial({
color: 0xe3a83b,
roughness: 0.56,
metalness: 0.08
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x273446,
roughness: 0.45,
metalness: 0.65
}),
screw: new THREE.MeshStandardMaterial({
color: 0xaab8c6,
roughness: 0.3,
metalness: 0.85
})
};
const up = new THREE.Vector3(0, 1, 0);
function addMesh(mesh, castShadow = true, receiveShadow = true) {
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
scene.add(mesh);
return mesh;
}
function cylinderBetween(
start,
end,
radius,
material,
radialSegments = 8
) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const midpoint = new THREE.Vector3()
.addVectors(start, end)
.multiplyScalar(0.5);
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
return mesh;
}
function makeTextTexture(
text,
{
color = "#eaf4ff",
background = "rgba(8, 17, 30, 0.78)",
border = "rgba(151, 206, 255, 0.22)",
width = 512,
height = 128
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
context.clearRect(0, 0, width, height);
if (background) {
context.fillStyle = background;
context.fillRect(4, 12, width - 8, height - 24);
context.strokeStyle = border;
context.lineWidth = 3;
context.strokeRect(4, 12, width - 8, height - 24);
}
context.textAlign = "center";
context.textBaseline = "middle";
context.font = `700 ${Math.floor(height * 0.43)}px ui-sans-serif, system-ui, sans-serif`;
context.lineJoin = "round";
context.strokeStyle = "rgba(0, 0, 0, 0.7)";
context.lineWidth = 6;
context.strokeText(text, width / 2, height / 2 + 1);
context.fillStyle = color;
context.fillText(text, width / 2, height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
return texture;
}
function makeLabel(text, worldWidth = 2.2, color = "#eaf4ff") {
const texture = makeTextTexture(text, { color });
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.25, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeSymbolTexture(symbol, color) {
return makeTextTexture(symbol, {
color,
background: "rgba(8, 17, 30, 0.8)",
border: "rgba(255, 255, 255, 0.18)",
width: 128,
height: 128
});
}
// ---------------------------------------------------------------------
// Board and environment
// ---------------------------------------------------------------------
const boardBase = addMesh(
new THREE.Mesh(
new THREE.BoxGeometry(11.2, 0.24, 6.2),
materials.boardEdge
)
);
boardBase.position.set(0, -0.19, 0);
const boardTop = addMesh(
new THREE.Mesh(
new THREE.BoxGeometry(10.9, 0.12, 5.9),
materials.board
)
);
boardTop.position.set(0, -0.02, 0);
const ground = addMesh(
new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
materials.ground
),
false,
true
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.45;
const grid = new THREE.GridHelper(50, 50, 0x1a3044, 0x142334);
grid.position.y = -0.445;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const screwGeometry = new THREE.CylinderGeometry(
0.11,
0.11,
0.10,
8
);
for (const x of [-5.05, 5.05]) {
for (const z of [-2.55, 2.55]) {
const screw = addMesh(
new THREE.Mesh(screwGeometry, materials.screw),
true,
true
);
screw.position.set(x, 0.08, z);
}
}
// ---------------------------------------------------------------------
// Series circuit: source -> resistor -> diode -> source
// ---------------------------------------------------------------------
const wireY = 0.22;
// Top-left source lead.
addMesh(
cylinderBetween(
new THREE.Vector3(-4, wireY, -1.02),
new THREE.Vector3(-4, wireY, -2),
0.065,
materials.wire
)
);
// Top wire from source to resistor.
addMesh(
cylinderBetween(
new THREE.Vector3(-4, wireY, -2),
new THREE.Vector3(-2.75, wireY, -2),
0.065,
materials.wire
)
);
// Resistor leads.
addMesh(
cylinderBetween(
new THREE.Vector3(-2.75, wireY, -2),
new THREE.Vector3(-2.4, 0.34, -2),
0.055,
materials.wire
)
);
addMesh(
cylinderBetween(
new THREE.Vector3(0.4, 0.34, -2),
new THREE.Vector3(0.75, wireY, -2),
0.055,
materials.wire
)
);
// Resistor body.
addMesh(
cylinderBetween(
new THREE.Vector3(-2.4, 0.34, -2),
new THREE.Vector3(0.4, 0.34, -2),
0.29,
materials.resistor,
10
)
);
// Color bands.
for (const [x, material] of [
[-1.95, materials.resistorBand],
[-1.72, materials.resistorBand2],
[-0.95, materials.resistorBand]
]) {
addMesh(
cylinderBetween(
new THREE.Vector3(x - 0.055, 0.34, -2),
new THREE.Vector3(x + 0.055, 0.34, -2),
0.298,
material,
10
)
);
}
// Top wire from resistor to diode.
addMesh(
cylinderBetween(
new THREE.Vector3(0.75, wireY, -2),
new THREE.Vector3(4, wireY, -2),
0.065,
materials.wire
)
);
// Right-side wire and diode leads.
addMesh(
cylinderBetween(
new THREE.Vector3(4, wireY, -2),
new THREE.Vector3(4, wireY, -1.25),
0.065,
materials.wire
)
);
addMesh(
cylinderBetween(
new THREE.Vector3(4, wireY, -1.25),
new THREE.Vector3(4, wireY, -0.38),
0.055,
materials.wire
)
);
addMesh(
cylinderBetween(
new THREE.Vector3(4, wireY, 0.66),
new THREE.Vector3(4, wireY, 1.25),
0.055,
materials.wire
)
);
// Symbolic diode: a low-poly triangular anode and a cathode bar.
const diodeShape = new THREE.Shape();
diodeShape.moveTo(-0.48, -0.38);
diodeShape.lineTo(0.48, -0.38);
diodeShape.lineTo(0, 0.48);
diodeShape.closePath();
const diodeGeometry = new THREE.ExtrudeGeometry(diodeShape, {
depth: 0.38,
bevelEnabled: false,
curveSegments: 1
});
diodeGeometry.rotateX(Math.PI / 2);
const diodeMaterial = new THREE.MeshStandardMaterial({
color: 0xffc45a,
emissive: 0xff9d23,
emissiveIntensity: 0.7,
roughness: 0.42,
metalness: 0.12,
flatShading: true
});
const diodeTriangle = addMesh(
new THREE.Mesh(diodeGeometry, diodeMaterial)
);
diodeTriangle.position.set(4, 0.60, 0);
const cathodeMaterial = new THREE.MeshStandardMaterial({
color: 0x9aa7b5,
emissive: 0x222b37,
emissiveIntensity: 0.25,
roughness: 0.35,
metalness: 0.75
});
const cathodeBar = addMesh(
new THREE.Mesh(
new THREE.BoxGeometry(0.96, 0.38, 0.11),
cathodeMaterial
)
);
cathodeBar.position.set(4, 0.41, 0.57);
// Right-side and bottom return wires.
addMesh(
cylinderBetween(
new THREE.Vector3(4, wireY, 1.25),
new THREE.Vector3(4, wireY, 2),
0.065,
materials.wire
)
);
addMesh(
cylinderBetween(
new THREE.Vector3(4, wireY, 2),
new THREE.Vector3(-4, wireY, 2),
0.065,
materials.wire
)
);
addMesh(
cylinderBetween(
new THREE.Vector3(-4, wireY, 2),
new THREE.Vector3(-4, wireY, 1.02),
0.065,
materials.wire
)
);
// Reversible source: faceted battery body and color-coded end caps.
addMesh(
cylinderBetween(
new THREE.Vector3(-4, 0.38, -0.90),
new THREE.Vector3(-4, 0.38, 0.90),
0.40,
materials.battery,
8
)
);
const topCapMaterial = new THREE.MeshStandardMaterial({
color: 0xff5f63,
emissive: 0x7d171b,
emissiveIntensity: 0.35,
roughness: 0.42,
metalness: 0.12
});
const bottomCapMaterial = new THREE.MeshStandardMaterial({
color: 0x55a8ff,
emissive: 0x123d77,
emissiveIntensity: 0.35,
roughness: 0.42,
metalness: 0.12
});
addMesh(
cylinderBetween(
new THREE.Vector3(-4, 0.38, -1.02),
new THREE.Vector3(-4, 0.38, -0.90),
0.43,
topCapMaterial,
8
)
);
addMesh(
cylinderBetween(
new THREE.Vector3(-4, 0.38, 0.90),
new THREE.Vector3(-4, 0.38, 1.02),
0.43,
bottomCapMaterial,
8
)
);
// Terminal polarity symbols.
const plusTexture = makeSymbolTexture("+", "#ff8588");
const minusTexture = makeSymbolTexture("−", "#83c1ff");
const topTerminalSymbol = new THREE.Sprite(
new THREE.SpriteMaterial({
map: plusTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
topTerminalSymbol.scale.set(0.48, 0.48, 1);
topTerminalSymbol.position.set(-4, 1.14, -1.04);
topTerminalSymbol.renderOrder = 30;
scene.add(topTerminalSymbol);
const bottomTerminalSymbol = new THREE.Sprite(
new THREE.SpriteMaterial({
map: minusTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
bottomTerminalSymbol.scale.set(0.48, 0.48, 1);
bottomTerminalSymbol.position.set(-4, 1.14, 1.04);
bottomTerminalSymbol.renderOrder = 30;
scene.add(bottomTerminalSymbol);
// Component labels.
const sourceLabel = makeLabel("REVERSIBLE SOURCE", 2.55);
sourceLabel.position.set(-4, 1.70, 0);
scene.add(sourceLabel);
const resistorLabel = makeLabel("R = 100 Ω", 1.8);
resistorLabel.position.set(-1, 1.15, -2);
scene.add(resistorLabel);
const diodeLabel = makeLabel("D1 • Si", 1.55, "#ffd47d");
diodeLabel.position.set(4, 1.23, 0);
scene.add(diodeLabel);
const anodeLabel = makeLabel("A", 0.55, "#ffd47d");
anodeLabel.position.set(4.85, 0.76, -0.45);
scene.add(anodeLabel);
const cathodeLabel = makeLabel("K", 0.55, "#dbe8f5");
cathodeLabel.position.set(4.85, 0.76, 0.57);
scene.add(cathodeLabel);
// Diode state ring on the board.
const stateRingMaterial = new THREE.MeshBasicMaterial({
color: 0x70f0ad,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const stateRing = new THREE.Mesh(
new THREE.TorusGeometry(0.82, 0.025, 4, 28),
stateRingMaterial
);
stateRing.rotation.x = Math.PI / 2;
stateRing.position.set(4, 0.055, 0);
scene.add(stateRing);
const diodeLight = new THREE.PointLight(0xffad36, 0, 3.2, 2);
diodeLight.position.set(4, 1.0, 0);
scene.add(diodeLight);
// ---------------------------------------------------------------------
// Conventional-current indicators
// ---------------------------------------------------------------------
const currentPath = [
new THREE.Vector3(-4, 0.68, -2),
new THREE.Vector3(-2.75, 0.68, -2),
new THREE.Vector3(0.75, 0.68, -2),
new THREE.Vector3(4, 0.68, -2),
new THREE.Vector3(4, 0.68, -1.25),
new THREE.Vector3(4, 0.68, -0.52),
new THREE.Vector3(4, 0.68, 0.64),
new THREE.Vector3(4, 0.68, 1.25),
new THREE.Vector3(4, 0.68, 2),
new THREE.Vector3(-4, 0.68, 2),
new THREE.Vector3(-4, 0.68, 1.02),
new THREE.Vector3(-4, 0.68, -1.02),
new THREE.Vector3(-4, 0.68, -2)
];
const cumulativeLengths = [0];
let totalPathLength = 0;
for (let i = 0; i < currentPath.length - 1; i++) {
totalPathLength += currentPath[i].distanceTo(currentPath[i + 1]);
cumulativeLengths.push(totalPathLength);
}
function sampleCurrentPath(distance) {
const wrappedDistance =
((distance % totalPathLength) + totalPathLength) % totalPathLength;
for (let i = 0; i < currentPath.length - 1; i++) {
const segmentStart = cumulativeLengths[i];
const segmentEnd = cumulativeLengths[i + 1];
if (wrappedDistance <= segmentEnd) {
const segmentLength = segmentEnd - segmentStart;
const amount = (wrappedDistance - segmentStart) / segmentLength;
return currentPath[i].clone().lerp(
currentPath[i + 1],
amount
);
}
}
return currentPath[currentPath.length - 1].clone();
}
const currentParticleGeometry = new THREE.IcosahedronGeometry(
0.085,
0
);
const currentParticleMaterial = new THREE.MeshBasicMaterial({
color: 0x8cf5ff,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const currentParticles = [];
const particleCount = 34;
for (let i = 0; i < particleCount; i++) {
const particle = new THREE.Mesh(
currentParticleGeometry,
currentParticleMaterial
);
particle.userData.pathOffset = i / particleCount;
particle.visible = false;
scene.add(particle);
currentParticles.push(particle);
}
const currentArrowMaterial = new THREE.MeshBasicMaterial({
color: 0x8cf5ff,
transparent: true,
opacity: 0.9,
depthTest: false,
depthWrite: false
});
function makeCurrentArrow(position, direction) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.33, 3),
currentArrowMaterial
);
arrow.position.copy(position);
arrow.quaternion.setFromUnitVectors(
up,
direction.clone().normalize()
);
arrow.renderOrder = 15;
scene.add(arrow);
return arrow;
}
const currentArrows = [
makeCurrentArrow(
new THREE.Vector3(2.25, 0.79, -2),
new THREE.Vector3(1, 0, 0)
),
makeCurrentArrow(
new THREE.Vector3(4, 0.79, -1.48),
new THREE.Vector3(0, 0, -1)
),
makeCurrentArrow(
new THREE.Vector3(0.6, 0.79, 2),
new THREE.Vector3(-1, 0, 0)
)
];
// ---------------------------------------------------------------------
// Explicit circuit model
//
// Series current:
// I = max(0, Vsource - Vforward) / R
//
// Ideal model: Vforward = 0 V
// Threshold model: Vforward = 0.70 V
//
// Reverse voltage never produces current in either model.
// ---------------------------------------------------------------------
const redColor = new THREE.Color(0xff5f63);
const blueColor = new THREE.Color(0x55a8ff);
const redEmissive = new THREE.Color(0x7d171b);
const blueEmissive = new THREE.Color(0x123d77);
let circuitState = {
voltage: 2,
forwardVoltage: THRESHOLD_VOLTAGE,
current: 0,
conducting: true,
diodeVoltage: THRESHOLD_VOLTAGE,
resistorVoltage: 1.3
};
function formatSignedVoltage(value) {
if (Math.abs(value) < 0.0001) return "0.0 V";
return `${value > 0 ? "+" : "−"}${Math.abs(value).toFixed(1)} V`;
}
function updateCircuit() {
const voltage = Number(voltageInput.value);
const useThreshold = modelInput.value === "threshold";
const forwardVoltage = useThreshold ? THRESHOLD_VOLTAGE : 0;
// Strictly forward-biased and above the selected forward threshold.
const conducting = voltage > forwardVoltage;
const current = conducting
? (voltage - forwardVoltage) / RESISTANCE
: 0;
// With no current the resistor has no voltage drop, so the diode
// supports the source voltage.
const diodeVoltage = conducting ? forwardVoltage : voltage;
const resistorVoltage = current * RESISTANCE;
circuitState = {
voltage,
forwardVoltage,
current,
conducting,
diodeVoltage,
resistorVoltage
};
voltageOutput.textContent = formatSignedVoltage(voltage);
currentOutput.textContent = `${(current * 1000).toFixed(1)} mA`;
let stateText;
let stateColor;
if (conducting) {
stateText = "CONDUCTING";
stateColor = "#70f0ad";
} else if (voltage < 0) {
stateText = "REVERSE BIASED";
stateColor = "#ff7373";
} else if (voltage === 0) {
stateText = "NO DRIVE";
stateColor = "#a9bbcc";
} else {
stateText = useThreshold ? "BELOW 0.70 V" : "NO FORWARD DRIVE";
stateColor = "#ffc45a";
}
stateOutput.textContent = stateText;
stateOutput.style.color = stateColor;
diodeVoltageOutput.textContent = formatSignedVoltage(diodeVoltage);
resistorVoltageOutput.textContent = formatSignedVoltage(
resistorVoltage
);
modelNote.innerHTML = useThreshold
? "Series model: <b>I = max(0, V − 0.70 V) / 100 Ω</b><br>" +
"Forward drop = 0.70 V. Reverse bias blocks current."
: "Series model: <b>I = max(0, V − 0 V) / 100 Ω</b><br>" +
"Ideal forward drop = 0 V. Reverse bias blocks current.";
// Reverse the source's physical terminal colors and +/- symbols.
const sourceIsPositive = voltage >= 0;
topCapMaterial.color.copy(sourceIsPositive ? redColor : blueColor);
topCapMaterial.emissive.copy(
sourceIsPositive ? redEmissive : blueEmissive
);
bottomCapMaterial.color.copy(
sourceIsPositive ? blueColor : redColor
);
bottomCapMaterial.emissive.copy(
sourceIsPositive ? blueEmissive : redEmissive
);
topTerminalSymbol.material.map = sourceIsPositive
? plusTexture
: minusTexture;
bottomTerminalSymbol.material.map = sourceIsPositive
? minusTexture
: plusTexture;
topTerminalSymbol.material.needsUpdate = true;
bottomTerminalSymbol.material.needsUpdate = true;
// Diode visual state.
diodeMaterial.color.set(conducting ? 0xffc45a : 0x6c7887);
diodeMaterial.emissive.set(conducting ? 0xff9d23 : 0x18202c);
diodeMaterial.emissiveIntensity = conducting ? 0.75 : 0.12;
cathodeMaterial.emissive.set(conducting ? 0x385466 : 0x202834);
cathodeMaterial.emissiveIntensity = conducting ? 0.4 : 0.18;
stateRingMaterial.color.set(
conducting
? 0x70f0ad
: voltage < 0
? 0xff7373
: 0xffc45a
);
currentParticleMaterial.opacity = conducting ? 0.95 : 0;
currentArrowMaterial.opacity = conducting ? 0.9 : 0;
for (const particle of currentParticles) {
particle.visible = conducting;
}
for (const arrow of currentArrows) {
arrow.visible = conducting;
}
}
voltageInput.addEventListener("input", updateCircuit);
modelInput.addEventListener("change", updateCircuit);
document.querySelector("#reverse").addEventListener("click", () => {
voltageInput.value = String(-Number(voltageInput.value));
updateCircuit();
});
document.querySelector("#zero").addEventListener("click", () => {
voltageInput.value = "0";
updateCircuit();
});
updateCircuit();
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let currentPhase = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (circuitState.conducting) {
// Faster animation for larger current, with a small minimum speed
// so that very small nonzero currents remain visible.
const currentMilliamps = circuitState.current * 1000;
const animationSpeed = 0.32 + Math.min(1.8, currentMilliamps * 0.026);
currentPhase =
(currentPhase + delta * animationSpeed) % totalPathLength;
for (const particle of currentParticles) {
const distance =
currentPhase +
particle.userData.pathOffset * totalPathLength;
particle.position.copy(sampleCurrentPath(distance));
const pulse = 0.82 + Math.sin(elapsed * 8 + distance * 0.12) * 0.18;
particle.scale.setScalar(pulse);
}
diodeMaterial.emissiveIntensity =
0.68 + Math.sin(elapsed * 5.5) * 0.20;
diodeLight.intensity = 0.9 + Math.sin(elapsed * 5.5) * 0.25;
stateRingMaterial.opacity = 0.58 + Math.sin(elapsed * 3.2) * 0.16;
} else {
diodeLight.intensity = 0;
stateRingMaterial.opacity = 0.48;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```