Prompt specification
Build or customize the Three.js 3D scene "Half-wave rectification demonstration" 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: Half-wave rectification demonstration
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/half-wave-rectification-demonstration-x1618
Scene Description Prompt:
Make a stylized Three.js scene in a self-contained HTML file using cdnjs: half-wave rectification demonstration. Place an alternating input waveform beside the output of a diode-resistor model; show permitted and blocked portions with consistent polarity and stated idealization.
Working Three.js Starter (half-wave-rectification-demonstration-x1618):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Half-Wave Rectification — Three.js Demonstration</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 25, 42, 0.86);
--border: rgba(130, 190, 220, 0.22);
--teal: #35e0c5;
--coral: #ff667c;
--text: #eaf7ff;
--muted: #91aabd;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#ui {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
header {
position: absolute;
top: 22px;
left: 26px;
right: 26px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: var(--text);
font-size: clamp(24px, 3vw, 42px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
}
.controls {
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
min-width: 72px;
padding: 9px 13px;
border: 1px solid var(--border);
border-radius: 9px;
background: var(--panel);
color: var(--text);
cursor: pointer;
font: inherit;
font-size: 12px;
font-weight: 700;
backdrop-filter: blur(12px);
}
button:hover {
border-color: rgba(53, 224, 197, 0.7);
color: var(--teal);
}
button:focus-visible {
outline: 2px solid var(--teal);
outline-offset: 3px;
}
.info {
position: absolute;
left: 26px;
bottom: 24px;
width: min(600px, calc(100vw - 52px));
padding: 15px 17px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(14px);
}
.info strong {
color: var(--teal);
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.info p {
margin: 6px 0 0;
color: #c4d7e4;
font-size: 12px;
line-height: 1.55;
}
.legend {
position: absolute;
right: 26px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 9px 15px;
max-width: 410px;
padding: 12px 15px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(14px);
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.teal {
color: var(--teal);
background: var(--teal);
}
.coral {
color: var(--coral);
background: var(--coral);
}
.hint {
position: absolute;
right: 28px;
top: 90px;
color: rgba(200, 222, 235, 0.58);
font-size: 11px;
text-align: right;
}
@media (max-width: 760px) {
header {
top: 15px;
left: 16px;
right: 16px;
}
.subtitle,
.hint {
display: none;
}
.info {
left: 16px;
bottom: 16px;
width: calc(100vw - 32px);
}
.legend {
display: none;
}
button {
min-width: 62px;
padding: 8px 10px;
}
}
</style>
</head>
<body>
<div id="ui">
<header>
<div>
<p class="eyebrow">Signal Path / 01</p>
<h1>Half-wave rectification</h1>
<p class="subtitle">
Alternating input → ideal diode → resistor → measured output
</p>
</div>
<div class="controls">
<button id="pause" type="button" aria-label="Pause or resume animation">
Pause
</button>
<button id="reset" type="button" aria-label="Reset camera and animation">
Reset
</button>
</div>
</header>
<div class="hint">
Drag to orbit · Wheel to zoom<br>
Arrow keys: orbit · + / −: zoom · Space: pause
</div>
<aside class="info">
<strong>Idealization</strong>
<p>
The diode is an ideal switch: zero forward voltage drop when conducting
and infinite reverse resistance when blocking. With a positive load
resistance R, the diode conducts only while
<b>v<sub>in</sub> > 0</b>. Then
<b>v<sub>out</sub> = v<sub>in</sub></b>; otherwise
<b>v<sub>out</sub> = 0</b>. No reverse current flows.
</p>
</aside>
<div class="legend">
<span><i class="dot teal"></i> Permitted / conducting</span>
<span><i class="dot coral"></i> Blocked / reverse-biased</span>
<span>Output is measured across R to ground</span>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.022);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
120
);
const world = new THREE.Group();
world.position.y = -0.05;
scene.add(world);
const TEAL = 0x35e0c5;
const CORAL = 0xff667c;
const CYAN_TEXT = "#dffaff";
const WHITE = 0xeaf7ff;
// -------------------------------------------------------------------------
// Lighting and environment
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa9eaff, 0x08111d, 1.65));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.7);
keyLight.position.set(4, 9, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
scene.add(keyLight);
const tealLight = new THREE.PointLight(TEAL, 28, 28, 2);
tealLight.position.set(-9, 4, 6);
scene.add(tealLight);
const coralLight = new THREE.PointLight(CORAL, 20, 25, 2);
coralLight.position.set(9, -1, 7);
scene.add(coralLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x081321,
roughness: 0.92,
metalness: 0.12
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.63;
floor.receiveShadow = true;
scene.add(floor);
// A few faint background rings for a stylized laboratory atmosphere.
for (let i = 0; i < 4; i++) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(7.5 + i * 2.3, 0.012, 6, 160),
new THREE.MeshBasicMaterial({
color: i % 2 ? 0x21475e : 0x17483f,
transparent: true,
opacity: 0.23,
depthWrite: false
})
);
ring.position.set(0, -0.1, -5.5 - i * 0.25);
scene.add(ring);
}
// -------------------------------------------------------------------------
// Utility functions
// -------------------------------------------------------------------------
function addText(
parent,
text,
x,
y,
width,
height,
options = {}
) {
const {
color = CYAN_TEXT,
weight = 650,
align = "center",
opacity = 1,
z = 0.22,
background = null
} = options;
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = Math.max(
32,
Math.round(1024 * height / Math.max(width, 0.001))
);
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (background) {
ctx.fillStyle = background;
const radius = Math.min(canvas.height * 0.22, 28);
ctx.beginPath();
ctx.roundRect(
4, 4, canvas.width - 8, canvas.height - 8,
radius, radius
);
ctx.fill();
}
const lines = String(text).split("\n");
const maxWidth = canvas.width * 0.92;
let fontSize = Math.min(
90,
(canvas.height / (lines.length * 1.35)) * 0.92
);
const setFont = () => {
ctx.font = `${weight} ${fontSize}px Inter, system-ui, sans-serif`;
};
setFont();
let widest = 0;
for (const line of lines) {
widest = Math.max(widest, ctx.measureText(line).width);
}
if (widest > maxWidth) {
fontSize *= maxWidth / widest;
setFont();
}
ctx.textAlign = align;
ctx.textBaseline = "middle";
ctx.fillStyle = color;
const xPosition = align === "left"
? canvas.width * 0.04
: align === "right"
? canvas.width * 0.96
: canvas.width * 0.5;
const lineHeight = fontSize * 1.18;
const firstY =
canvas.height * 0.5 - (lines.length - 1) * lineHeight * 0.5;
lines.forEach((line, index) => {
ctx.fillText(line, xPosition, firstY + index * lineHeight);
});
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
opacity,
depthWrite: false,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
material
);
mesh.position.set(x, y, z);
parent.add(mesh);
return mesh;
}
function makeLine(parent, points, color = 0x36566d, opacity = 0.5) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(p => new THREE.Vector3(...p))
);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
parent.add(line);
return line;
}
function cylinderBetween(
parent,
a,
b,
radius,
material,
radialSegments = 12
) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function curveTube(parent, points, radius, material, closed = false) {
const curve = new THREE.CatmullRomCurve3(
points.map(p => new THREE.Vector3(...p)),
closed,
"centripetal"
);
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(
curve,
Math.max(16, points.length * 3),
radius,
7,
closed
),
material
);
parent.add(mesh);
return mesh;
}
function createCard(x, accentColor) {
const group = new THREE.Group();
group.position.set(x, 0, 0);
world.add(group);
const body = new THREE.Mesh(
new THREE.BoxGeometry(4.65, 5.0, 0.18),
new THREE.MeshPhysicalMaterial({
color: 0x0c2034,
roughness: 0.46,
metalness: 0.18,
clearcoat: 0.35,
clearcoatRoughness: 0.4
})
);
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(body.geometry),
new THREE.LineBasicMaterial({
color: 0x36566d,
transparent: true,
opacity: 0.62
})
);
group.add(edges);
const accent = new THREE.Mesh(
new THREE.BoxGeometry(4.15, 0.035, 0.025),
new THREE.MeshBasicMaterial({ color: accentColor })
);
accent.position.set(0, 2.25, 0.105);
group.add(accent);
return group;
}
function addPill(parent, x, y, width, height, color, opacity = 0.75) {
const pill = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({
color: 0x091827,
transparent: true,
opacity,
depthWrite: false
})
);
pill.position.set(x, y, 0.16);
parent.add(pill);
return pill;
}
// -------------------------------------------------------------------------
// Shared graph coordinates
// -------------------------------------------------------------------------
const GRAPH_LEFT = -1.9;
const GRAPH_RIGHT = 1.9;
const GRAPH_BASE = -0.85;
const GRAPH_AMPLITUDE = 1.15;
function graphX(t) {
return THREE.MathUtils.lerp(
GRAPH_LEFT,
GRAPH_RIGHT,
t / (Math.PI * 2)
);
}
function graphY(value) {
return GRAPH_BASE + value * GRAPH_AMPLITUDE;
}
function addGraphGrid(group) {
const gridMaterial = new THREE.LineBasicMaterial({
color: 0x36566d,
transparent: true,
opacity: 0.25
});
for (let i = 0; i <= 6; i++) {
const x = THREE.MathUtils.lerp(
GRAPH_LEFT,
GRAPH_RIGHT,
i / 6
);
makeLine(
group,
[[x, GRAPH_BASE - GRAPH_AMPLITUDE - 0.08, 0.115],
[x, GRAPH_BASE + GRAPH_AMPLITUDE + 0.12, 0.115]],
0x36566d,
0.2
);
}
for (let i = -1; i <= 1; i++) {
const y = graphY(i);
makeLine(
group,
[[GRAPH_LEFT, y, 0.115],
[GRAPH_RIGHT, y, 0.115]],
0x36566d,
0.22
);
}
makeLine(
group,
[[GRAPH_LEFT, GRAPH_BASE, 0.14],
[GRAPH_RIGHT, GRAPH_BASE, 0.14]],
0x8ba8ba,
0.75
);
makeLine(
group,
[[GRAPH_LEFT, GRAPH_BASE - GRAPH_AMPLITUDE - 0.08, 0.14],
[GRAPH_LEFT, GRAPH_BASE + GRAPH_AMPLITUDE + 0.12, 0.14]],
0x8ba8ba,
0.58
);
addText(group, "0", graphX(0), -1.08, 0.38, 0.19, {
color: "#8eaabd",
weight: 600
});
addText(group, "π", graphX(Math.PI), -1.08, 0.38, 0.19, {
color: "#8eaabd",
weight: 600
});
addText(group, "2π", graphX(Math.PI * 2), -1.08, 0.48, 0.19, {
color: "#8eaabd",
weight: 600
});
addText(group, "+Vpk", -1.63, 0.48, 0.62, 0.18, {
color: "#73a5b8",
weight: 600
});
addText(group, "−Vpk", -1.63, -2.15, 0.62, 0.18, {
color: "#73a5b8",
weight: 600
});
}
function addArea(
group,
t0,
t1,
waveform,
color,
opacity
) {
const vertices = [];
const indices = [];
const segments = 90;
for (let i = 0; i <= segments; i++) {
const t = THREE.MathUtils.lerp(t0, t1, i / segments);
const x = graphX(t);
const y = graphY(waveform(t));
vertices.push(x, GRAPH_BASE, 0.112);
vertices.push(x, y, 0.112);
if (i < segments) {
const a = i * 2;
indices.push(a, a + 1, a + 2);
indices.push(a + 1, a + 3, a + 2);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
geometry.setIndex(indices);
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
side: THREE.DoubleSide,
depthWrite: false
})
);
group.add(mesh);
return mesh;
}
function waveformPoints(t0, t1, waveform, z = 0.18) {
const points = [];
const segments = 100;
for (let i = 0; i <= segments; i++) {
const t = THREE.MathUtils.lerp(t0, t1, i / segments);
points.push([graphX(t), graphY(waveform(t)), z]);
}
return points;
}
function addWaveSegment(
group,
t0,
t1,
waveform,
color,
radius = 0.027
) {
return curveTube(
group,
waveformPoints(t0, t1, waveform),
radius,
new THREE.MeshBasicMaterial({ color })
);
}
function addDashedHorizontal(group, x0, x1, y, color) {
const dashLength = 0.12;
const gap = 0.075;
const material = new THREE.MeshBasicMaterial({ color });
for (let x = x0; x < x1; x += dashLength + gap) {
const end = Math.min(x + dashLength, x1);
cylinderBetween(
group,
[x, y, 0.18],
[end, y, 0.18],
0.018,
material,
8
);
}
}
// -------------------------------------------------------------------------
// Input graph
// -------------------------------------------------------------------------
const inputCard = createCard(-5.05, TEAL);
addText(inputCard, "ALTERNATING INPUT", 0, 2.02, 3.85, 0.42, {
color: "#eaf7ff",
weight: 800
});
addText(inputCard, "v_in(t) = Vpk · sin(t)", 0, 1.62, 3.2, 0.25, {
color: "#8fb5c8",
weight: 600
});
addGraphGrid(inputCard);
addArea(
inputCard,
0,
Math.PI,
t => Math.sin(t),
TEAL,
0.12
);
addArea(
inputCard,
Math.PI,
Math.PI * 2,
t => Math.sin(t),
CORAL,
0.13
);
addWaveSegment(
inputCard,
0,
Math.PI * 2,
t => Math.sin(t),
0xa9c4d4,
0.014
);
addWaveSegment(
inputCard,
0,
Math.PI,
t => Math.sin(t),
TEAL,
0.029
);
addWaveSegment(
inputCard,
Math.PI,
Math.PI * 2,
t => Math.sin(t),
CORAL,
0.029
);
addPill(inputCard, -0.88, 0.80, 1.55, 0.32, 0x0a1c2a);
addText(inputCard, "PERMITTED • + HALF", -0.88, 0.80, 1.45, 0.22, {
color: "#35e0c5",
weight: 800
});
addPill(inputCard, 0.86, -1.60, 1.62, 0.32, 0x0a1c2a);
addText(inputCard, "BLOCKED • − HALF", 0.86, -1.60, 1.52, 0.22, {
color: "#ff667c",
weight: 800
});
addText(inputCard, "TIME →", 0, -2.28, 1.35, 0.19, {
color: "#7194a8",
weight: 650
});
const inputMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 16, 12),
new THREE.MeshBasicMaterial({ color: 0xffffff })
);
inputMarker.position.z = 0.25;
inputCard.add(inputMarker);
// -------------------------------------------------------------------------
// Output graph
// -------------------------------------------------------------------------
const outputCard = createCard(5.05, TEAL);
addText(outputCard, "RECTIFIED OUTPUT", 0, 2.02, 3.85, 0.42, {
color: "#eaf7ff",
weight: 800
});
addText(outputCard, "v_out(t) = max(0, v_in(t))", 0, 1.62, 3.35, 0.25, {
color: "#8fb5c8",
weight: 600
});
addGraphGrid(outputCard);
addArea(
outputCard,
0,
Math.PI,
t => Math.max(0, Math.sin(t)),
TEAL,
0.19
);
addWaveSegment(
outputCard,
0,
Math.PI,
t => Math.max(0, Math.sin(t)),
TEAL,
0.032
);
addDashedHorizontal(
outputCard,
graphX(Math.PI),
graphX(Math.PI * 2),
GRAPH_BASE,
CORAL
);
addPill(outputCard, -0.86, 0.80, 1.72, 0.32, 0x0a1c2a);
addText(outputCard, "PERMITTED • PASSES", -0.86, 0.80, 1.62, 0.22, {
color: "#35e0c5",
weight: 800
});
addPill(outputCard, 0.88, -1.40, 1.82, 0.32, 0x0a1c2a);
addText(outputCard, "BLOCKED • OUTPUT = 0", 0.88, -1.40, 1.72, 0.22, {
color: "#ff667c",
weight: 800
});
addText(outputCard, "TIME →", 0, -2.28, 1.35, 0.19, {
color: "#7194a8",
weight: 650
});
const outputMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.058, 16, 12),
new THREE.MeshBasicMaterial({ color: TEAL })
);
outputMarker.position.z = 0.25;
outputCard.add(outputMarker);
// -------------------------------------------------------------------------
// Central diode-resistor model
// -------------------------------------------------------------------------
const circuitCard = createCard(0, 0x79a8c1);
addText(circuitCard, "DIODE–RESISTOR MODEL", 0, 2.02, 3.95, 0.42, {
color: "#eaf7ff",
weight: 800
});
addText(circuitCard, "ideal diode + load R", 0, 1.62, 2.65, 0.25, {
color: "#8fb5c8",
weight: 600
});
const forwardState = addText(
circuitCard,
"FORWARD • ON",
-0.18,
1.12,
1.75,
0.25,
{ color: "#35e0c5", weight: 800, opacity: 1 }
);
const reverseState = addText(
circuitCard,
"REVERSE • OFF",
-0.18,
1.12,
1.75,
0.25,
{ color: "#ff667c", weight: 800, opacity: 0 }
);
const wireMaterial = new THREE.MeshStandardMaterial({
color: 0x87a5b8,
roughness: 0.36,
metalness: 0.76
});
const componentMaterial = new THREE.MeshStandardMaterial({
color: 0x15394a,
roughness: 0.36,
metalness: 0.48,
emissive: 0x061c22,
emissiveIntensity: 0.5
});
const resistorMaterial = new THREE.MeshStandardMaterial({
color: 0xd8c6a0,
roughness: 0.5,
metalness: 0.2
});
// AC source.
const sourceCenter = new THREE.Vector3(-1.35, -0.15, 0.30);
const sourceBack = new THREE.Mesh(
new THREE.CircleGeometry(0.35, 48),
new THREE.MeshStandardMaterial({
color: 0x071522,
roughness: 0.6,
metalness: 0.2
})
);
sourceBack.position.copy(sourceCenter);
sourceBack.position.z = 0.255;
circuitCard.add(sourceBack);
const sourceRingMaterial = new THREE.MeshStandardMaterial({
color: TEAL,
emissive: TEAL,
emissiveIntensity: 0.6,
roughness: 0.3,
metalness: 0.45
});
const sourceRing = new THREE.Mesh(
new THREE.TorusGeometry(0.35, 0.035, 10, 56),
sourceRingMaterial
);
sourceRing.position.copy(sourceCenter);
sourceRing.position.z = 0.31;
circuitCard.add(sourceRing);
const sourceSine = [];
for (let i = 0; i <= 40; i++) {
const x = -0.22 + (i / 40) * 0.44;
const y = -0.15 + Math.sin((i / 40) * Math.PI * 2) * 0.11;
sourceSine.push([x, y, 0.34]);
}
curveTube(
circuitCard,
sourceSine,
0.013,
new THREE.MeshBasicMaterial({ color: 0x9fd5e8 })
);
addText(circuitCard, "v_in(t)", -1.36, 0.72, 0.95, 0.23, {
color: "#dffaff",
weight: 750
});
// Moving polarity signs around the AC source.
const plusMaterial = new THREE.MeshBasicMaterial({
color: TEAL,
transparent: true,
opacity: 1
});
const minusMaterial = new THREE.MeshBasicMaterial({
color: CORAL,
transparent: true,
opacity: 1
});
function makePlus(x, y, material) {
const group = new THREE.Group();
cylinderBetween(
group,
[x - 0.07, y, 0.36],
[x + 0.07, y, 0.36],
0.012,
material,
8
);
cylinderBetween(
group,
[x, y - 0.07, 0.36],
[x, y + 0.07, 0.36],
0.012,
material,
8
);
circuitCard.add(group);
return group;
}
function makeMinus(x, y, material) {
const group = new THREE.Group();
cylinderBetween(
group,
[x - 0.07, y, 0.36],
[x + 0.07, y, 0.36],
0.012,
material,
8
);
circuitCard.add(group);
return group;
}
const plusTop = makePlus(-0.86, 0.31, plusMaterial);
const plusBottom = makePlus(-0.86, -0.63, plusMaterial);
const minusTop = makeMinus(-0.86, 0.31, minusMaterial);
const minusBottom = makeMinus(-0.86, -0.63, minusMaterial);
// Source and ground wiring.
cylinderBetween(
circuitCard,
[-1.35, 0.20, 0.29],
[-1.35, 0.54, 0.29],
0.025,
wireMaterial
);
cylinderBetween(
circuitCard,
[-1.35, -0.50, 0.29],
[-1.35, -1.35, 0.29],
0.025,
wireMaterial
);
cylinderBetween(
circuitCard,
[-1.35, 0.20, 0.29],
[-0.73, 0.20, 0.29],
0.025,
wireMaterial
);
// Diode: anode on the left, cathode stripe on the right.
const diodeBody = cylinderBetween(
circuitCard,
[-0.73, 0.20, 0.38],
[0.23, 0.20, 0.38],
0.145,
componentMaterial,
24
);
const cathodeStripe = new THREE.Mesh(
new THREE.TorusGeometry(0.151, 0.026, 10, 32),
new THREE.MeshStandardMaterial({
color: 0xf5f3df,
roughness: 0.36,
metalness: 0.3
})
);
cathodeStripe.rotation.y = Math.PI / 2;
cathodeStripe.position.set(0.11, 0.20, 0.38);
circuitCard.add(cathodeStripe);
cylinderBetween(
circuitCard,
[0.23, 0.20, 0.29],
[1.50, 0.20, 0.29],
0.025,
wireMaterial
);
addText(circuitCard, "A", -0.75, -0.20, 0.34, 0.22, {
color: "#9fc1d1",
weight: 800
});
addText(circuitCard, "K", 0.25, -0.20, 0.34, 0.22, {
color: "#f4f0dc",
weight: 800
});
// Conventional-current direction, shown only while the diode conducts.
const currentArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-0.70, 0.60, 0.34),
0.92,
TEAL,
0.18,
0.11
);
currentArrow.line.material.transparent = true;
currentArrow.cone.material.transparent = true;
circuitCard.add(currentArrow);
addText(circuitCard, "i only when ON", -0.18, 0.78, 1.35, 0.18, {
color: "#70a9b8",
weight: 650
});
// Resistor from the output node to ground.
cylinderBetween(
circuitCard,
[1.50, 0.20, 0.29],
[1.50, -0.10, 0.29],
0.025,
wireMaterial
);
const resistor = cylinderBetween(
circuitCard,
[1.50, -0.12, 0.38],
[1.50, -0.78, 0.38],
0.15,
resistorMaterial,
20
);
const resistorBandMaterial = new THREE.MeshStandardMaterial({
color: 0x6e4d43,
roughness: 0.52,
metalness: 0.12
});
[-0.58, -0.43, -0.28].forEach(y => {
const band = new THREE.Mesh(
new THREE.TorusGeometry(0.153, 0.018, 8, 28),
resistorBandMaterial
);
band.rotation.x = Math.PI / 2;
band.position.set(1.50, y, 0.38);
circuitCard.add(band);
});
cylinderBetween(
circuitCard,
[1.50, -0.78, 0.29],
[1.50, -1.35, 0.29],
0.025,
wireMaterial
);
cylinderBetween(
circuitCard,
[-1.35, -1.35, 0.29],
[1.50, -1.35, 0.29],
0.025,
wireMaterial
);
// Ground symbol.
cylinderBetween(
circuitCard,
[0.05, -1.35, 0.29],
[0.05, -1.57, 0.29],
0.022,
wireMaterial
);
[0.44, 0.28, 0.13].forEach((width, index) => {
cylinderBetween(
circuitCard,
[0.05 - width / 2, -1.57 - index * 0.10, 0.29],
[0.05 + width / 2, -1.57 - index * 0.10, 0.29],
0.018,
wireMaterial,
8
);
});
// Output node and labels.
const outputNodeMaterial = new THREE.MeshBasicMaterial({ color: TEAL });
const outputNode = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 16, 12),
outputNodeMaterial
);
outputNode.position.set(1.50, 0.20, 0.43);
circuitCard.add(outputNode);
addText(circuitCard, "v_out", 1.72, 0.61, 0.88, 0.25, {
color: "#dffaff",
weight: 800
});
addText(circuitCard, "R", 2.02, -0.44, 0.34, 0.27, {
color: "#ead5b7",
weight: 850
});
addText(
circuitCard,
"ON: v_out = v_in | OFF: v_out = 0",
0,
-2.02,
3.95,
0.22,
{ color: "#b9d0dd", weight: 650 }
);
addText(
circuitCard,
"IDEAL: V_D = 0 ON, R_reverse = ∞",
0,
-2.28,
3.95,
0.20,
{ color: "#7194a8", weight: 700 }
);
// Animated charge/current dots follow the conducting loop.
const currentCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-1.35, -1.35, 0.52),
new THREE.Vector3(-1.35, 0.20, 0.52),
new THREE.Vector3(-0.73, 0.20, 0.52),
new THREE.Vector3(0.23, 0.20, 0.52),
new THREE.Vector3(1.50, 0.20, 0.52),
new THREE.Vector3(1.50, -1.35, 0.52)
],
true,
"centripetal"
);
const flowGroup = new THREE.Group();
circuitCard.add(flowGroup);
const flowMaterial = new THREE.MeshBasicMaterial({ color: 0xbaffef });
const flowParticles = [];
for (let i = 0; i < 7; i++) {
const particle = new THREE.Mesh(
new THREE.SphereGeometry(0.045, 12, 8),
flowMaterial
);
flowGroup.add(particle);
flowParticles.push(particle);
}
// -------------------------------------------------------------------------
// Camera controls implemented without Three.js addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, -0.20, 0);
const spherical = new THREE.Spherical(15.5, 1.39, 0);
let zoomFactor = 1;
let fitRadius = 15.5;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
spherical.radius = fitRadius * zoomFactor;
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
function resetCamera() {
spherical.set(fitRadius, 1.39, 0);
zoomFactor = 1;
updateCamera();
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
fitRadius = 15.5 * Math.max(1, 1.45 / aspect);
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
spherical.theta -= dx * 0.005;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi + dy * 0.004,
1.05,
1.68
);
updateCamera();
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
zoomFactor = THREE.MathUtils.clamp(
zoomFactor * Math.exp(event.deltaY * 0.001),
0.72,
1.8
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
let paused = false;
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
}
function resetAll() {
elapsed = 0;
resetCamera();
if (paused) togglePause();
}
const pauseButton = document.getElementById("pause");
const resetButton = document.getElementById("reset");
pauseButton.addEventListener("click", togglePause);
resetButton.addEventListener("click", resetAll);
window.addEventListener("keydown", event => {
switch (event.code) {
case "ArrowLeft":
spherical.theta += 0.08;
break;
case "ArrowRight":
spherical.theta -= 0.08;
break;
case "ArrowUp":
spherical.phi = Math.max(1.05, spherical.phi - 0.06);
break;
case "ArrowDown":
spherical.phi = Math.min(1.68, spherical.phi + 0.06);
break;
case "+":
case "=":
zoomFactor = Math.max(0.72, zoomFactor * 0.9);
break;
case "-":
case "_":
zoomFactor = Math.min(1.8, zoomFactor * 1.1);
break;
case "Space":
event.preventDefault();
togglePause();
break;
case "KeyR":
resetAll();
break;
default:
return;
}
updateCamera();
});
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let elapsed = 0;
let previousTime = performance.now();
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) elapsed += dt;
const phase = (elapsed * 0.68) % (Math.PI * 2);
const voltage = Math.sin(phase);
const conducting = voltage > 0.015;
// Moving markers on the two graphs.
inputMarker.position.set(
graphX(phase),
graphY(voltage),
0.25
);
outputMarker.position.set(
graphX(phase),
graphY(Math.max(0, voltage)),
0.25
);
outputMarker.material.color.setHex(
conducting ? TEAL : CORAL
);
// Source polarity swaps with the alternating input.
plusTop.visible = conducting;
plusBottom.visible = !conducting;
minusTop.visible = !conducting;
minusBottom.visible = conducting;
plusMaterial.color.setHex(conducting ? TEAL : CORAL);
minusMaterial.color.setHex(conducting ? CORAL : TEAL);
sourceRingMaterial.color.setHex(conducting ? TEAL : CORAL);
sourceRingMaterial.emissive.setHex(conducting ? TEAL : CORAL);
sourceRingMaterial.emissiveIntensity = conducting ? 0.85 : 0.45;
// State labels and diode glow.
forwardState.material.opacity = conducting ? 1 : 0;
reverseState.material.opacity = conducting ? 0 : 1;
componentMaterial.emissive.setHex(conducting ? TEAL : CORAL);
componentMaterial.emissiveIntensity = conducting ? 0.72 : 0.30;
outputNodeMaterial.color.setHex(conducting ? TEAL : CORAL);
currentArrow.line.material.opacity = conducting ? 0.95 : 0.12;
currentArrow.cone.material.opacity = conducting ? 0.95 : 0.12;
flowGroup.visible = conducting;
if (conducting) {
flowParticles.forEach((particle, index) => {
const u = (elapsed * 0.16 + index / flowParticles.length) % 1;
particle.position.copy(currentCurve.getPointAt(u));
});
}
renderer.render(scene, camera);
}
resize();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```