Prompt specification
Build or customize the Three.js 3D scene "Transistor package beside a switch diagram" 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: Transistor package beside a switch diagram
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/transistor-package-beside-a-switch-diagram-x2549
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: Transistor package beside a switch diagram. Model a simplified transistor package with three labeled terminals beside a mechanical switch. Distinguish physical appearance from the schematic representation of its control relationship. Return one self-contained HTML file.
Working Three.js Starter (transistor-package-beside-a-switch-diagram-x2549):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Transistor Package and Switch Schematic</title>
<style>
:root {
--bg: #080d17;
--panel: rgba(15, 24, 39, 0.82);
--line: rgba(151, 177, 207, 0.22);
--text: #edf5ff;
--muted: #93a7bf;
--base: #ffbd59;
--collector: #58d9ef;
--emitter: #ff7185;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 22%, rgba(34, 82, 111, 0.2), transparent 34%),
linear-gradient(145deg, #0b1320, #060a12);
color: var(--text);
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;
outline: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 3;
}
.intro {
position: absolute;
top: 26px;
left: 30px;
width: min(470px, calc(100vw - 60px));
}
.eyebrow {
margin-bottom: 9px;
color: #68d6e9;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 3.2vw, 48px);
line-height: 1.02;
letter-spacing: -0.045em;
font-weight: 760;
}
.intro p {
max-width: 440px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
position: absolute;
top: 28px;
right: 28px;
display: flex;
align-items: stretch;
gap: 10px;
pointer-events: auto;
}
.control-card {
min-width: 220px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
#switchButton {
width: 100%;
padding: 10px 12px;
border: 1px solid rgba(255, 189, 89, 0.5);
border-radius: 9px;
background: rgba(255, 189, 89, 0.1);
color: #ffd79a;
font: inherit;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease, transform 120ms ease;
}
#switchButton:hover {
background: rgba(255, 189, 89, 0.18);
border-color: rgba(255, 189, 89, 0.8);
}
#switchButton:active { transform: translateY(1px); }
#status {
margin-top: 9px;
color: #8fa3bb;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-align: center;
text-transform: uppercase;
}
#status.on {
color: #74e6c3;
}
#resetButton {
align-self: center;
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(15, 24, 39, 0.72);
color: var(--muted);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
cursor: pointer;
}
#resetButton:hover { color: white; }
.legend {
position: absolute;
left: 28px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
gap: 16px;
max-width: calc(100vw - 56px);
padding: 11px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(9, 16, 28, 0.72);
color: #a9bad0;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.legend span { display: inline-flex; align-items: center; gap: 7px; }
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.dot.base { color: var(--base); background: currentColor; }
.dot.collector { color: var(--collector); background: currentColor; }
.dot.emitter { color: var(--emitter); background: currentColor; }
.dot.signal { color: #8ef0c7; background: currentColor; }
.hint {
position: absolute;
right: 28px;
bottom: 26px;
color: rgba(166, 184, 207, 0.58);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
overflow: hidden;
}
.label {
position: absolute;
white-space: nowrap;
transform: translate(-50%, -50%);
color: #dceaff;
font-size: 10px;
font-weight: 780;
letter-spacing: 0.08em;
text-transform: uppercase;
text-shadow: 0 2px 8px #000;
user-select: none;
}
.label.section {
padding: 7px 10px;
border: 1px solid rgba(121, 159, 191, 0.25);
border-radius: 7px;
background: rgba(8, 15, 26, 0.77);
color: #a9c3da;
font-size: 9px;
backdrop-filter: blur(8px);
}
.label.component {
padding: 5px 8px;
border: 1px solid rgba(121, 159, 191, 0.25);
border-radius: 6px;
background: rgba(8, 15, 26, 0.76);
color: #bcd0e6;
font-size: 9px;
}
.label.terminal {
width: 26px;
height: 26px;
display: grid;
place-items: center;
border: 1px solid currentColor;
border-radius: 50%;
background: rgba(6, 12, 22, 0.92);
box-shadow: 0 0 14px color-mix(in srgb, currentColor 35%, transparent);
font-size: 11px;
}
.label.base { color: var(--base); }
.label.collector { color: var(--collector); }
.label.emitter { color: var(--emitter); }
.label.signal { color: #8ef0c7; }
.label.note { color: #7f95ae; font-size: 8px; }
@media (max-width: 760px) {
.intro { top: 18px; left: 18px; }
.intro p { display: none; }
.controls { top: auto; right: 14px; bottom: 60px; }
.control-card { min-width: 175px; }
#resetButton { display: none; }
.legend { left: 14px; bottom: 14px; gap: 10px; }
.hint { display: none; }
}
</style>
</head>
<body>
<div id="labels"></div>
<div id="hud">
<header class="intro">
<div class="eyebrow">Semiconductor study / 01</div>
<h1>Switching a transistor</h1>
<p>
The left side shows the physical package, metal leads, and mechanical switch.
The right side reduces the same control relationship to a schematic.
</p>
</header>
<div class="controls">
<div class="control-card">
<button id="switchButton" type="button">Close switch</button>
<div id="status">Switch open · Base off · Q1 off</div>
</div>
<button id="resetButton" type="button">Reset view</button>
</div>
<div class="legend">
<span><i class="dot base"></i>Base</span>
<span><i class="dot collector"></i>Collector</span>
<span><i class="dot emitter"></i>Emitter</span>
<span><i class="dot signal"></i>Current path</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Click either switch</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080d17);
scene.fog = new THREE.Fog(0x080d17, 15, 31);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive 3D transistor and switch diagram");
document.body.prepend(renderer.domElement);
const clock = new THREE.Clock();
const labelsHost = document.getElementById("labels");
const labels = [];
const switchButton = document.getElementById("switchButton");
const statusElement = document.getElementById("status");
const resetButton = document.getElementById("resetButton");
const COLORS = {
base: 0xffbd59,
collector: 0x58d9ef,
emitter: 0xff7185,
signal: 0x8ef0c7,
copper: 0xc98a52,
metal: 0xb7c3cf,
wire: 0x73879d
};
function standard(color, roughness = 0.65, metalness = 0.05) {
return new THREE.MeshStandardMaterial({ color, roughness, metalness });
}
function emissive(color, intensity = 0.08) {
return new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: intensity,
roughness: 0.48,
metalness: 0.18
});
}
function addLabel(text, worldPosition, className = "", localGroup = null) {
const element = document.createElement("div");
element.className = `label ${className}`;
element.innerHTML = text;
labelsHost.appendChild(element);
const localPosition = localGroup ? worldPosition.clone() : null;
const group = localGroup;
labels.push({
element,
worldPosition: localGroup ? null : worldPosition.clone(),
localPosition,
group
});
}
function updateLabels() {
const width = innerWidth;
const height = innerHeight;
for (const label of labels) {
let world;
if (label.group) {
world = label.group.localToWorld(label.localPosition.clone());
} else {
world = label.worldPosition.clone();
}
const projected = world.project(camera);
const visible = projected.z > -1 && projected.z < 1;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
const hemi = new THREE.HemisphereLight(0xb9dcff, 0x182030, 1.7);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.4);
keyLight.position.set(3, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3fc9ff, 1.7);
rimLight.position.set(-6, 4, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa84d, 12, 10, 2);
warmLight.position.set(-3.5, 2.4, 2.5);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({ color: 0x09111d, roughness: 0.94, metalness: 0.02 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.08;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x28425d, 0x142238);
grid.position.y = -0.071;
grid.material.transparent = true;
grid.material.opacity = 0.48;
scene.add(grid);
function addEdges(mesh, color = 0x41536a, opacity = 0.65) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({ color, transparent: true, opacity })
);
edges.position.copy(mesh.position);
edges.rotation.copy(mesh.rotation);
edges.scale.copy(mesh.scale);
mesh.parent.add(edges);
return edges;
}
function cylinderBetween(parent, a, b, radius, material, radialSegments = 10) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
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;
parent.add(mesh);
return mesh;
}
function alignCylinder(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.scale.y = direction.length();
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
}
const v = (x, y, z = 0) => new THREE.Vector3(x, y, z);
/* --------------------------------------------------------------
Physical side
-------------------------------------------------------------- */
const physicalBoard = new THREE.Mesh(
new THREE.BoxGeometry(4.35, 0.16, 3.15),
standard(0x182538, 0.76, 0.16)
);
physicalBoard.position.set(-2.4, 0.05, 0.25);
physicalBoard.receiveShadow = true;
physicalBoard.castShadow = true;
scene.add(physicalBoard);
addEdges(physicalBoard, 0x52708e, 0.58);
const trimMaterial = standard(0x6f5131, 0.55, 0.45);
const boardTrim = new THREE.Mesh(
new THREE.BoxGeometry(4.18, 0.025, 0.025),
trimMaterial
);
boardTrim.position.set(-2.4, 0.145, 1.76);
scene.add(boardTrim);
const transistor = new THREE.Group();
transistor.position.set(-3.22, 1.72, 0.28);
scene.add(transistor);
const packageShape = new THREE.Shape();
packageShape.moveTo(-0.46, -0.52);
packageShape.lineTo(0.46, -0.52);
packageShape.quadraticCurveTo(0.58, -0.28, 0.56, 0.15);
packageShape.quadraticCurveTo(0.53, 0.54, 0.28, 0.64);
packageShape.lineTo(-0.28, 0.64);
packageShape.quadraticCurveTo(-0.53, 0.54, -0.56, 0.15);
packageShape.quadraticCurveTo(-0.58, -0.28, -0.46, -0.52);
const packageGeometry = new THREE.ExtrudeGeometry(packageShape, {
depth: 0.72,
bevelEnabled: true,
bevelSegments: 3,
steps: 1,
bevelSize: 0.075,
bevelThickness: 0.075,
curveSegments: 12
});
packageGeometry.translate(0, 0, -0.36);
const packageMaterial = new THREE.MeshPhysicalMaterial({
color: 0x18232b,
roughness: 0.58,
metalness: 0.08,
clearcoat: 0.22,
clearcoatRoughness: 0.62
});
const packageBody = new THREE.Mesh(packageGeometry, packageMaterial);
packageBody.castShadow = true;
packageBody.receiveShadow = true;
transistor.add(packageBody);
const packageCanvas = document.createElement("canvas");
packageCanvas.width = 512;
packageCanvas.height = 512;
const packageContext = packageCanvas.getContext("2d");
packageContext.clearRect(0, 0, 512, 512);
packageContext.fillStyle = "rgba(232, 241, 248, 0.96)";
packageContext.textAlign = "center";
packageContext.font = "800 62px Arial";
packageContext.fillText("Q1", 256, 145);
packageContext.font = "700 34px Arial";
packageContext.fillText("2N3904", 256, 235);
packageContext.fillStyle = "rgba(160, 188, 211, 0.9)";
packageContext.font = "600 28px Arial";
packageContext.fillText("NPN", 256, 300);
packageContext.fillStyle = "rgba(255, 189, 89, 0.9)";
packageContext.fillRect(164, 352, 184, 5);
const packageTexture = new THREE.CanvasTexture(packageCanvas);
packageTexture.colorSpace = THREE.SRGBColorSpace;
const packageLabel = new THREE.Mesh(
new THREE.PlaneGeometry(0.72, 0.72),
new THREE.MeshBasicMaterial({
map: packageTexture,
transparent: true,
depthWrite: false
})
);
packageLabel.position.set(0, 0.03, 0.465);
packageLabel.renderOrder = 3;
transistor.add(packageLabel);
const metalMaterial = standard(COLORS.metal, 0.29, 0.88);
const leadPoints = {
E: [v(-0.22, -0.49, 0.04), v(-0.32, -1.02, 0.04), v(-0.32, -1.51, 0.04)],
B: [v(0, -0.49, 0.04), v(0, -1.08, 0.04), v(0, -1.51, 0.04)],
C: [v(0.22, -0.49, 0.04), v(0.32, -1.02, 0.04), v(0.32, -1.51, 0.04)]
};
for (const points of Object.values(leadPoints)) {
cylinderBetween(transistor, points[0], points[1], 0.035, metalMaterial);
cylinderBetween(transistor, points[1], points[2], 0.035, metalMaterial);
}
const terminalMaterials = {
E: emissive(COLORS.emitter, 0.5),
B: emissive(COLORS.base, 0.5),
C: emissive(COLORS.collector, 0.5)
};
for (const [name, points] of Object.entries(leadPoints)) {
const foot = points[2];
const pad = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.115, 0.035, 24),
terminalMaterials[name]
);
pad.position.set(foot.x, -1.55, foot.z);
pad.receiveShadow = true;
transistor.add(pad);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.018, 8, 28),
terminalMaterials[name]
);
ring.rotation.x = Math.PI / 2;
ring.position.set(foot.x, -1.525, foot.z);
transistor.add(ring);
}
addLabel("E", v(-0.32, -1.08, 0.10), "terminal emitter", transistor);
addLabel("B", v(0, -1.08, 0.10), "terminal base", transistor);
addLabel("C", v(0.32, -1.08, 0.10), "terminal collector", transistor);
const physicalSwitch = new THREE.Group();
scene.add(physicalSwitch);
const switchBase = new THREE.Mesh(
new THREE.BoxGeometry(1.03, 0.29, 0.76),
standard(0x101720, 0.69, 0.1)
);
switchBase.position.set(-1.25, 0.30, 0.54);
switchBase.castShadow = true;
switchBase.receiveShadow = true;
physicalSwitch.add(switchBase);
addEdges(switchBase, 0x53657a, 0.65);
const switchWell = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.27, 0.075, 28),
standard(0x070b11, 0.62, 0.25)
);
switchWell.position.set(-1.38, 0.48, 0.54);
physicalSwitch.add(switchWell);
const capMaterial = emissive(0x304a60, 0.08);
const switchCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.205, 0.22, 0.15, 28),
capMaterial
);
switchCap.position.set(-1.38, 0.55, 0.54);
switchCap.castShadow = true;
switchCap.userData.toggle = true;
physicalSwitch.add(switchCap);
const physicalLeverPivot = new THREE.Group();
physicalLeverPivot.position.set(-1.56, 0.57, 0.54);
physicalLeverPivot.rotation.z = 0.28;
physicalSwitch.add(physicalLeverPivot);
const lever = new THREE.Mesh(
new THREE.BoxGeometry(0.68, 0.075, 0.12),
metalMaterial
);
lever.position.x = 0.32;
lever.castShadow = true;
physicalLeverPivot.add(lever);
const leverTip = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
standard(0xd7e0e8, 0.34, 0.65)
);
leverTip.position.x = 0.66;
leverTip.castShadow = true;
physicalLeverPivot.add(leverTip);
const baseTraceMaterial = standard(COLORS.copper, 0.48, 0.55);
const traceCurve = new THREE.CatmullRomCurve3([
v(-1.65, 0.17, 0.78),
v(-2.05, 0.17, 0.78),
v(-2.74, 0.17, 0.78),
v(-3.22, 0.17, 0.32)
]);
const baseTrace = new THREE.Mesh(
new THREE.TubeGeometry(traceCurve, 50, 0.026, 8, false),
baseTraceMaterial
);
baseTrace.receiveShadow = true;
scene.add(baseTrace);
addLabel("Physical appearance", v(-2.4, 0.28, 1.76), "section");
addLabel("Q1 · NPN", v(-3.22, 2.55, 0.3), "component");
addLabel("S1 · mechanical switch", v(-1.25, 0.98, 0.55), "component");
addLabel("base link", v(-2.42, 0.29, 0.78), "note");
/* --------------------------------------------------------------
Schematic side
-------------------------------------------------------------- */
const schematic = new THREE.Group();
schematic.position.set(2.42, 2.48, -1.02);
schematic.rotation.y = 0.12;
scene.add(schematic);
const panel = new THREE.Mesh(
new THREE.BoxGeometry(4.35, 4.65, 0.16),
standard(0x101b2a, 0.79, 0.12)
);
panel.castShadow = true;
panel.receiveShadow = true;
schematic.add(panel);
const panelCanvas = document.createElement("canvas");
panelCanvas.width = 1024;
panelCanvas.height = 1024;
const panelContext = panelCanvas.getContext("2d");
const panelGradient = panelContext.createLinearGradient(0, 0, 0, 1024);
panelGradient.addColorStop(0, "#15263a");
panelGradient.addColorStop(1, "#0b1422");
panelContext.fillStyle = panelGradient;
panelContext.fillRect(0, 0, 1024, 1024);
panelContext.strokeStyle = "rgba(105, 145, 180, 0.12)";
panelContext.lineWidth = 1;
for (let x = 0; x <= 1024; x += 64) {
panelContext.beginPath();
panelContext.moveTo(x, 0);
panelContext.lineTo(x, 1024);
panelContext.stroke();
}
for (let y = 0; y <= 1024; y += 64) {
panelContext.beginPath();
panelContext.moveTo(0, y);
panelContext.lineTo(1024, y);
panelContext.stroke();
}
panelContext.strokeStyle = "rgba(92, 220, 232, 0.26)";
panelContext.lineWidth = 3;
panelContext.strokeRect(28, 28, 968, 968);
const panelTexture = new THREE.CanvasTexture(panelCanvas);
panelTexture.colorSpace = THREE.SRGBColorSpace;
const panelFace = new THREE.Mesh(
new THREE.PlaneGeometry(4.29, 4.59),
new THREE.MeshStandardMaterial({
map: panelTexture,
roughness: 0.86,
metalness: 0.03
})
);
panelFace.position.z = 0.087;
panelFace.receiveShadow = true;
schematic.add(panelFace);
const diagramZ = 0.145;
const railMaterial = standard(COLORS.wire, 0.5, 0.3);
const controlMaterial = emissive(COLORS.base, 0.025);
const outputMaterial = emissive(COLORS.collector, 0.025);
const switchLeverMaterial = emissive(COLORS.base, 0.025);
const groundMaterial = standard(COLORS.wire, 0.5, 0.3);
// Supply rail.
cylinderBetween(
schematic,
v(-1.72, 1.55, diagramZ),
v(1.68, 1.55, diagramZ),
0.019,
railMaterial
);
// Base-control branch.
cylinderBetween(
schematic,
v(-1.40, 1.55, diagramZ),
v(-1.40, 0.50, diagramZ),
0.019,
controlMaterial
);
cylinderBetween(
schematic,
v(-0.82, 0.50, diagramZ),
v(-0.58, 0.50, diagramZ),
0.019,
controlMaterial
);
const resistorPoints = [
v(-0.58, 0.50, diagramZ),
v(-0.49, 0.62, diagramZ),
v(-0.40, 0.38, diagramZ),
v(-0.31, 0.62, diagramZ),
v(-0.22, 0.38, diagramZ),
v(-0.13, 0.62, diagramZ),
v(-0.13, 0.50, diagramZ)
];
for (let i = 0; i < resistorPoints.length - 1; i++) {
cylinderBetween(
schematic,
resistorPoints[i],
resistorPoints[i + 1],
0.019,
controlMaterial
);
}
cylinderBetween(
schematic,
v(-0.13, 0.50, diagramZ),
v(0.48, 0.50, diagramZ),
0.019,
controlMaterial
);
cylinderBetween(
schematic,
v(0.48, 0.15, diagramZ),
v(0.48, 0.85, diagramZ),
0.021,
controlMaterial
);
// Collector and emitter.
cylinderBetween(
schematic,
v(0.48, 0.75, diagramZ),
v(1.15, 1.05, diagramZ),
0.021,
outputMaterial
);
cylinderBetween(
schematic,
v(0.48, 0.25, diagramZ),
v(1.15, -0.05, diagramZ),
0.021,
outputMaterial
);
// Load branch from supply rail to collector.
cylinderBetween(
schematic,
v(1.15, 1.55, diagramZ),
v(1.15, 1.40, diagramZ),
0.019,
outputMaterial
);
const loadMaterial = emissive(COLORS.collector, 0.025);
const loadBlock = new THREE.Mesh(
new THREE.BoxGeometry(0.44, 0.28, 0.055),
loadMaterial
);
loadBlock.position.set(1.15, 1.25, diagramZ);
schematic.add(loadBlock);
cylinderBetween(
schematic,
v(1.15, 1.11, diagramZ),
v(1.15, 1.05, diagramZ),
0.019,
outputMaterial
);
cylinderBetween(
schematic,
v(1.15, -0.05, diagramZ),
v(1.15, -1.13, diagramZ),
0.019,
outputMaterial
);
// Ground symbol.
cylinderBetween(
schematic,
v(0.86, -1.13, diagramZ),
v(1.44, -1.13, diagramZ),
0.018,
groundMaterial
);
cylinderBetween(
schematic,
v(0.94, -1.22, diagramZ),
v(1.36, -1.22, diagramZ),
0.018,
groundMaterial
);
cylinderBetween(
schematic,
v(1.02, -1.31, diagramZ),
v(1.28, -1.31, diagramZ),
0.018,
groundMaterial
);
// Switch contacts.
const contactMaterial = standard(0xcbd8e3, 0.35, 0.65);
for (const point of [v(-1.40, 0.50, diagramZ), v(-0.82, 0.50, diagramZ)]) {
const contact = new THREE.Mesh(
new THREE.SphereGeometry(0.045, 14, 10),
contactMaterial
);
contact.position.copy(point);
schematic.add(contact);
}
// Dynamic schematic switch lever.
const schematicLever = cylinderBetween(
schematic,
v(-1.40, 0.50, diagramZ),
v(-0.82, 0.82, diagramZ),
0.027,
switchLeverMaterial
);
const schematicHandle = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 16, 12),
switchLeverMaterial
);
schematicHandle.position.set(-0.82, 0.82, diagramZ);
schematicHandle.userData.toggle = true;
schematic.add(schematicHandle);
// Emitter arrow.
const emitterDirection = v(1.15 - 0.48, -0.05 - 0.25, 0).normalize();
const emitterArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.068, 0.18, 3),
outputMaterial
);
emitterArrow.position.set(0.90, 0.14, diagramZ);
emitterArrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
emitterDirection
);
schematic.add(emitterArrow);
// Base node glow.
const baseNode = new THREE.Mesh(
new THREE.SphereGeometry(0.064, 16, 12),
controlMaterial
);
baseNode.position.set(0.48, 0.50, diagramZ + 0.02);
schematic.add(baseNode);
addLabel("Schematic · control relationship", v(0, 2.03, 0.18), "section", schematic);
addLabel("S1", v(-1.13, 0.94, 0.19), "component base", schematic);
addLabel("Rᴮ", v(-0.35, 0.87, 0.19), "component base", schematic);
addLabel("B", v(0.35, 0.50, 0.20), "terminal base", schematic);
addLabel("C", v(0.91, 0.95, 0.20), "terminal collector", schematic);
addLabel("E", v(0.91, -0.03, 0.20), "terminal emitter", schematic);
addLabel("LOAD", v(1.57, 1.26, 0.20), "component collector", schematic);
addLabel("+5 V", v(-1.58, 1.73, 0.20), "component signal", schematic);
addLabel("GND", v(1.38, -1.43, 0.20), "component", schematic);
addLabel("switch command → base current", v(-0.12, -0.72, 0.20), "note", schematic);
addLabel("logic, not to scale", v(0, -2.02, 0.20), "note", schematic);
/* --------------------------------------------------------------
Animated current pulses
-------------------------------------------------------------- */
const basePath = [
v(-1.40, 1.55, diagramZ + 0.06),
v(-1.40, 0.50, diagramZ + 0.06),
v(-0.82, 0.50, diagramZ + 0.06),
v(-0.13, 0.50, diagramZ + 0.06),
v(0.48, 0.50, diagramZ + 0.06)
];
const outputPath = [
v(1.15, 1.55, diagramZ + 0.07),
v(1.15, 1.05, diagramZ + 0.07),
v(0.48, 0.75, diagramZ + 0.07),
v(1.15, -0.05, diagramZ + 0.07),
v(1.15, -1.13, diagramZ + 0.07)
];
function pointAlong(points, normalizedPosition) {
const lengths = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const length = points[i].distanceTo(points[i + 1]);
lengths.push(length);
total += length;
}
let distance = ((normalizedPosition % 1) + 1) % 1 * total;
for (let i = 0; i < lengths.length; i++) {
if (distance <= lengths[i]) {
return points[i].clone().lerp(points[i + 1], distance / lengths[i]);
}
distance -= lengths[i];
}
return points.at(-1).clone();
}
const pulseGeometry = new THREE.SphereGeometry(0.047, 12, 10);
const basePulseMaterial = new THREE.MeshBasicMaterial({ color: COLORS.base });
const outputPulseMaterial = new THREE.MeshBasicMaterial({ color: COLORS.signal });
const pulses = [];
for (let i = 0; i < 3; i++) {
const basePulse = new THREE.Mesh(pulseGeometry, basePulseMaterial);
const outputPulse = new THREE.Mesh(pulseGeometry, outputPulseMaterial);
schematic.add(basePulse, outputPulse);
pulses.push({ basePulse, outputPulse });
}
/* --------------------------------------------------------------
State and animation
-------------------------------------------------------------- */
let switchClosed = false;
let switchValue = 0;
let targetSwitchValue = 0;
function updateStatus() {
switchButton.textContent = switchClosed ? "Open switch" : "Close switch";
statusElement.textContent = switchClosed
? "Switch closed · Base driven · Q1 on"
: "Switch open · Base off · Q1 off";
statusElement.classList.toggle("on", switchClosed);
}
function toggleSwitch() {
switchClosed = !switchClosed;
targetSwitchValue = switchClosed ? 1 : 0;
updateStatus();
}
switchButton.addEventListener("click", toggleSwitch);
updateStatus();
/* --------------------------------------------------------------
Custom camera controls — no add-ons
-------------------------------------------------------------- */
const target = new THREE.Vector3(0, 1.25, 0);
let cameraRadius = innerWidth < 760 ? 20.5 : 12.6;
let cameraTheta = 0.27;
let cameraPhi = 1.06;
let dragging = false;
let dragMoved = 0;
let lastX = 0;
let lastY = 0;
const pointers = new Map();
let pinchDistance = 0;
function updateCamera() {
camera.position.set(
target.x + cameraRadius * Math.sin(cameraPhi) * Math.sin(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * Math.sin(cameraPhi) * Math.cos(cameraTheta)
);
camera.lookAt(target);
}
function resetView() {
cameraRadius = innerWidth < 760 ? 20.5 : 12.6;
cameraTheta = 0.27;
cameraPhi = 1.06;
updateCamera();
}
resetButton.addEventListener("click", resetView);
updateCamera();
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
dragging = true;
dragMoved = 0;
lastX = event.clientX;
lastY = event.clientY;
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * (pinchDistance / Math.max(distance, 1)),
6.5,
27
);
}
pinchDistance = distance;
updateCamera();
return;
}
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
dragMoved += Math.abs(dx) + Math.abs(dy);
cameraTheta -= dx * 0.0055;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.0045, 0.42, 1.43);
lastX = event.clientX;
lastY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
pointers.delete(event.pointerId);
if (dragMoved < 7) {
const pointer = new THREE.Vector2(
(event.clientX / innerWidth) * 2 - 1,
-(event.clientY / innerHeight) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(pointer, camera);
const targets = [switchCap, schematicHandle];
const intersections = raycaster.intersectObjects(targets, false);
if (intersections.length) {
toggleSwitch();
}
}
if (pointers.size === 0) {
dragging = false;
pinchDistance = 0;
}
});
renderer.domElement.addEventListener("pointercancel", (event) => {
pointers.delete(event.pointerId);
if (!pointers.size) {
dragging = false;
pinchDistance = 0;
}
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
6.5,
27
);
updateCamera();
}, { passive: false });
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const controlMaterials = [controlMaterial, switchLeverMaterial];
const outputMaterials = [outputMaterial, loadMaterial];
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
switchValue = THREE.MathUtils.damp(
switchValue,
targetSwitchValue,
9,
delta
);
// Physical switch movement.
physicalLeverPivot.rotation.z = THREE.MathUtils.lerp(0.28, 0, switchValue);
switchCap.position.y = 0.55 - switchValue * 0.065;
capMaterial.emissive.setHex(switchClosed ? COLORS.signal : 0x304a60);
capMaterial.emissiveIntensity = switchClosed ? 0.9 : 0.08;
baseTraceMaterial.emissive = new THREE.Color(COLORS.base);
baseTraceMaterial.emissiveIntensity = switchClosed ? 0.12 : 0;
// Schematic switch movement.
const switchAngle = THREE.MathUtils.lerp(Math.atan2(0.32, 0.58), 0, switchValue);
const switchEnd = v(
-1.40 + Math.cos(switchAngle) * 0.58,
0.50 + Math.sin(switchAngle) * 0.58,
diagramZ
);
alignCylinder(
schematicLever,
v(-1.40, 0.50, diagramZ),
switchEnd
);
schematicHandle.position.copy(switchEnd);
// Energized schematic paths.
for (const material of controlMaterials) {
material.emissiveIntensity = THREE.MathUtils.lerp(
0.025,
1.35,
switchValue
);
}
for (const material of outputMaterials) {
material.emissiveIntensity = THREE.MathUtils.lerp(
0.025,
1.2,
switchValue
);
}
// Moving charge indicators.
pulses.forEach((pulse, index) => {
const visible = switchValue > 0.78;
pulse.basePulse.visible = visible;
pulse.outputPulse.visible = visible;
if (visible) {
const phase = elapsed * 0.27 + index / pulses.length;
pulse.basePulse.position.copy(pointAlong(basePath, phase));
pulse.outputPulse.position.copy(pointAlong(outputPath, phase + 0.12));
}
});
scene.updateMatrixWorld(true);
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```