Prompt specification
Build or customize the Three.js 3D scene "Transistor-as-switch teaching block" 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-as-switch teaching block
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/transistor-as-switch-teaching-block-x1677
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: transistor-as-switch teaching block. Build a labeled three-terminal symbolic switch controlling a virtual lamp, with separate control and load paths; explain that the block is a simplified transistor model. Return one self-contained HTML file.
Working Three.js Starter (transistor-as-switch-teaching-block-x1677):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Transistor-as-Switch Teaching Block</title>
<style>
:root {
--ink: #173536;
--muted: #627575;
--paper: #f7faf5;
--panel: #fbfcf8;
--line: #d7e0d8;
--amber: #e89a26;
--amber-soft: #fff1d8;
--blue: #287eaa;
--blue-soft: #e4f3fa;
--green: #318766;
--shadow: 0 18px 50px rgba(35, 63, 55, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: #e7eee8;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
#stage {
position: fixed;
inset: 0 370px 0 0;
overflow: hidden;
background:
radial-gradient(circle at 45% 35%, rgba(255,255,255,.72), transparent 38%),
linear-gradient(145deg, #eef4ef, #dde7df);
}
#sceneCanvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#sceneCanvas:active { cursor: grabbing; }
.scene-heading {
position: absolute;
top: 27px;
left: 30px;
z-index: 2;
max-width: 510px;
pointer-events: none;
}
.eyebrow {
display: block;
margin-bottom: 8px;
color: #52706b;
font-size: 11px;
font-weight: 800;
letter-spacing: .16em;
text-transform: uppercase;
}
.scene-heading h2 {
margin: 0;
font-size: clamp(24px, 3vw, 40px);
line-height: 1.03;
letter-spacing: -.045em;
}
.scene-heading p {
margin: 10px 0 0;
color: #5a706d;
font-size: 14px;
line-height: 1.5;
}
.view-hint {
position: absolute;
top: 30px;
right: 25px;
z-index: 2;
padding: 8px 11px;
border: 1px solid rgba(54, 81, 73, .13);
border-radius: 999px;
color: #607570;
background: rgba(249, 252, 248, .67);
backdrop-filter: blur(8px);
font-size: 11px;
pointer-events: none;
}
.scene-note {
position: absolute;
left: 26px;
right: 26px;
bottom: 22px;
z-index: 2;
padding: 12px 15px;
border: 1px solid rgba(57, 85, 76, .13);
border-radius: 13px;
color: #536965;
background: rgba(250, 252, 248, .78);
backdrop-filter: blur(10px);
box-shadow: 0 8px 24px rgba(39, 70, 59, .07);
font-size: 12px;
line-height: 1.45;
pointer-events: none;
}
.scene-note strong { color: var(--ink); }
#panel {
position: fixed;
inset: 0 0 0 auto;
z-index: 5;
width: 370px;
overflow-y: auto;
border-left: 1px solid var(--line);
background: var(--panel);
box-shadow: var(--shadow);
scrollbar-width: thin;
scrollbar-color: #b8c7bd transparent;
}
.panel-inner { padding: 25px 24px 28px; }
.panel-header {
padding-bottom: 21px;
border-bottom: 1px solid var(--line);
}
.panel-header h1 {
margin: 0;
font-size: 27px;
line-height: 1.08;
letter-spacing: -.04em;
}
.panel-header p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.section {
padding: 20px 0;
border-bottom: 1px solid var(--line);
}
.section:last-child { border-bottom: 0; }
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.section-title h3 {
margin: 0;
font-size: 12px;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
}
.drive-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 15px;
}
.drive-copy strong {
display: block;
font-size: 17px;
}
.drive-copy small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 11px;
}
.toggle {
position: relative;
width: 76px;
height: 38px;
flex: 0 0 auto;
padding: 0;
border: 0;
border-radius: 999px;
background: #cbd4ce;
cursor: pointer;
transition: background .2s ease;
}
.toggle.active { background: var(--blue); }
.toggle-knob {
position: absolute;
top: 4px;
left: 4px;
width: 30px;
height: 30px;
border-radius: 50%;
background: white;
box-shadow: 0 3px 9px rgba(25, 50, 43, .22);
transition: transform .2s ease;
}
.toggle.active .toggle-knob { transform: translateX(38px); }
.toggle:focus-visible,
.utility-button:focus-visible,
input:focus-visible {
outline: 3px solid rgba(40, 126, 170, .3);
outline-offset: 3px;
}
.range-heading {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 7px;
font-size: 12px;
}
.range-heading output {
color: var(--blue);
font-weight: 800;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0 9px;
accent-color: var(--blue);
cursor: pointer;
}
.microcopy {
margin: 6px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.status-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 16px;
}
.status-card {
padding: 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: #f5f8f3;
}
.status-card span {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: .08em;
text-transform: uppercase;
}
.status-card strong {
display: block;
margin-top: 5px;
font-size: 14px;
}
.status-card strong.on { color: var(--green); }
.status-card strong.off { color: #8a9692; }
.terminal-list {
display: grid;
gap: 9px;
}
.terminal {
display: grid;
grid-template-columns: 37px 1fr;
gap: 10px;
align-items: center;
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: #f6f8f4;
}
.terminal-letter {
display: grid;
place-items: center;
width: 31px;
height: 31px;
border-radius: 9px;
color: white;
font-size: 13px;
font-weight: 900;
}
.terminal.c .terminal-letter { background: var(--amber); }
.terminal.b .terminal-letter { background: var(--blue); }
.terminal.e .terminal-letter { background: #536a66; }
.terminal strong {
display: block;
font-size: 13px;
}
.terminal small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 11px;
}
.legend {
display: grid;
gap: 8px;
margin-bottom: 14px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #506763;
font-size: 12px;
}
.legend-line {
width: 25px;
height: 5px;
border-radius: 4px;
}
.legend-line.load { background: var(--amber); }
.legend-line.control { background: var(--blue); }
.legend-line.common { background: #758580; }
.explanation {
margin: 0;
color: #536965;
font-size: 12px;
line-height: 1.58;
}
.explanation strong { color: var(--ink); }
.warning {
margin-top: 12px;
padding: 11px 12px;
border-left: 3px solid var(--amber);
border-radius: 0 9px 9px 0;
color: #685b42;
background: var(--amber-soft);
font-size: 11px;
line-height: 1.5;
}
.utility-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 18px;
}
.utility-button {
padding: 10px 9px;
border: 1px solid #cbd6cd;
border-radius: 10px;
color: var(--ink);
background: #f1f5ef;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: background .15s ease, transform .15s ease;
}
.utility-button:hover {
background: #e8eee7;
transform: translateY(-1px);
}
@media (max-width: 900px) {
#stage {
inset: 0 0 335px 0;
}
#panel {
inset: auto 0 0 0;
width: auto;
height: 335px;
border-top: 1px solid var(--line);
border-left: 0;
}
.panel-inner {
padding: 20px;
}
.scene-heading {
top: 18px;
left: 19px;
}
.scene-heading h2 { font-size: 25px; }
.scene-heading p { max-width: 290px; font-size: 12px; }
.view-hint { display: none; }
.scene-note { left: 14px; right: 14px; bottom: 13px; font-size: 10px; }
}
@media (max-width: 520px) {
#stage { inset-bottom: 350px; }
#panel { height: 350px; }
.scene-heading p { display: none; }
.scene-heading .eyebrow { font-size: 9px; }
.scene-heading h2 { font-size: 22px; }
.panel-inner { padding: 17px; }
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive low-poly transistor teaching model">
<canvas id="sceneCanvas" aria-label="Three-dimensional transistor-as-switch model"></canvas>
<header class="scene-heading">
<span class="eyebrow">Field Note 03 / Semiconductor Switching</span>
<h2>One control input.<br />One switched load.</h2>
<p>The blue base path commands the block. The amber collector–emitter path powers the lamp.</p>
</header>
<div class="view-hint">Drag to orbit · Scroll to zoom · Right-drag to pan</div>
<div class="scene-note">
<strong>Teaching model:</strong> the rocker is a visual metaphor for an idealized transistor switch.
Blue and amber paths are separated for clarity; this common-emitter arrangement still shares a 0 V reference.
</div>
</main>
<aside id="panel" aria-label="Model controls and explanation">
<div class="panel-inner">
<header class="panel-header">
<span class="eyebrow">Interactive Teaching Block</span>
<h1>Transistor<br />as a switch</h1>
<p>A simplified NPN low-side switch. Drive the base to close the collector–emitter path and illuminate the virtual lamp.</p>
</header>
<section class="section">
<div class="section-title">
<h3>Base drive</h3>
<span class="eyebrow" id="driveBadge">ON</span>
</div>
<div class="drive-row">
<div class="drive-copy">
<strong id="driveText">Base energized</strong>
<small>Conceptual drive, not a datasheet value</small>
</div>
<button id="driveToggle" class="toggle active" type="button"
aria-label="Toggle base drive" aria-pressed="true">
<span class="toggle-knob"></span>
</button>
</div>
<div class="range-heading">
<label for="baseRange">Drive level</label>
<output id="baseValue" for="baseRange">100%</output>
</div>
<input id="baseRange" type="range" min="0" max="100" value="100" />
<p class="microcopy">Ideal model: any positive base drive closes the C–E path. The level also scales the displayed lamp brightness.</p>
<div class="status-grid" aria-live="polite">
<div class="status-card">
<span>C–E path</span>
<strong id="pathState" class="on">CLOSED</strong>
</div>
<div class="status-card">
<span>Virtual lamp</span>
<strong id="lampState" class="on">ON</strong>
</div>
</div>
</section>
<section class="section">
<div class="section-title"><h3>Three terminals</h3></div>
<div class="terminal-list">
<div class="terminal c">
<div class="terminal-letter">C</div>
<div><strong>Collector</strong><small>Load input from the lamp</small></div>
</div>
<div class="terminal b">
<div class="terminal-letter">B</div>
<div><strong>Base</strong><small>Control input from the drive source</small></div>
</div>
<div class="terminal e">
<div class="terminal-letter">E</div>
<div><strong>Emitter</strong><small>Return path to 0 V</small></div>
</div>
</div>
</section>
<section class="section">
<div class="section-title"><h3>Path legend</h3></div>
<div class="legend">
<div class="legend-row"><span class="legend-line load"></span>Amber: load path, +V → lamp → C → E → 0 V</div>
<div class="legend-row"><span class="legend-line control"></span>Blue: separate base-control path</div>
<div class="legend-row"><span class="legend-line common"></span>Gray: shared 0 V reference</div>
</div>
<p class="explanation">
<strong>This block is a simplified transistor model.</strong> It demonstrates the
switching relationship: a small base command controls a larger load current.
It is not a literal mechanical switch and does not calculate real transistor behavior.
</p>
<div class="warning">
A real BJT needs base <em>current</em>, has non-ohmic behavior, requires suitable biasing,
and may not turn off completely. The separated paths here are diagrammatic; the control
and load circuits are not galvanically isolated.
</div>
<div class="utility-row">
<button id="flowToggle" class="utility-button" type="button">Pause current</button>
<button id="resetView" class="utility-button" type="button">Reset view</button>
</div>
</section>
</div>
</aside>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const canvas = document.getElementById("sceneCanvas");
const state = {
driveLevel: 1,
previousDriveLevel: 1,
flowPaused: false,
flowClock: 0
};
// ---------- Renderer, scene, camera ----------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7eee8);
scene.fog = new THREE.Fog(0xe7eee8, 19, 39);
const camera = new THREE.PerspectiveCamera(39, 1, 0.1, 100);
camera.position.set(0, 8, 13);
const hemi = new THREE.HemisphereLight(0xf7fff8, 0x7b8e80, 2.0);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff5dc, 3.0);
key.position.set(-5, 10, 7);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -9;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
key.shadow.bias = -0.00045;
scene.add(key);
const rim = new THREE.DirectionalLight(0xb9dcff, 1.25);
rim.position.set(7, 5, -6);
scene.add(rim);
// ---------- Custom camera controls (no addons) ----------
class CameraController {
constructor(camera, domElement) {
this.camera = camera;
this.domElement = domElement;
this.target = new THREE.Vector3(0, 0.72, 0);
this.radius = 15.4;
this.theta = 0.02;
this.phi = 1.03;
this.minRadius = 9;
this.maxRadius = 28;
this.dragging = false;
this.panning = false;
this.pointerId = null;
this.lastX = 0;
this.lastY = 0;
this.right = new THREE.Vector3();
this.up = new THREE.Vector3();
this.forward = new THREE.Vector3();
this.bindEvents();
this.updateCamera();
}
bindEvents() {
this.domElement.addEventListener("contextmenu", e => e.preventDefault());
this.domElement.addEventListener("pointerdown", e => {
this.dragging = true;
this.panning = e.button === 2 || e.shiftKey;
this.pointerId = e.pointerId;
this.lastX = e.clientX;
this.lastY = e.clientY;
this.domElement.setPointerCapture(e.pointerId);
});
this.domElement.addEventListener("pointermove", e => {
if (!this.dragging || e.pointerId !== this.pointerId) return;
const dx = e.clientX - this.lastX;
const dy = e.clientY - this.lastY;
this.lastX = e.clientX;
this.lastY = e.clientY;
if (this.panning) {
this.camera.getWorldDirection(this.forward);
this.right.crossVectors(this.forward, this.camera.up).normalize();
this.up.crossVectors(this.right, this.forward).normalize();
const scale = this.radius * 0.00125;
this.target.addScaledVector(this.right, -dx * scale);
this.target.addScaledVector(this.up, dy * scale);
} else {
this.theta -= dx * 0.0058;
this.phi = THREE.MathUtils.clamp(this.phi - dy * 0.0048, 0.54, 1.36);
}
this.updateCamera();
});
const endDrag = e => {
if (e.pointerId !== this.pointerId) return;
this.dragging = false;
this.panning = false;
this.pointerId = null;
};
this.domElement.addEventListener("pointerup", endDrag);
this.domElement.addEventListener("pointercancel", endDrag);
this.domElement.addEventListener("wheel", e => {
e.preventDefault();
this.radius = THREE.MathUtils.clamp(
this.radius * Math.exp(e.deltaY * 0.001),
this.minRadius,
this.maxRadius
);
this.updateCamera();
}, { passive: false });
}
updateCamera() {
const sinPhi = Math.sin(this.phi);
this.camera.position.set(
this.target.x + this.radius * sinPhi * Math.sin(this.theta),
this.target.y + this.radius * Math.cos(this.phi),
this.target.z + this.radius * sinPhi * Math.cos(this.theta)
);
this.camera.lookAt(this.target);
}
reset(aspect) {
this.target.set(0, 0.72, 0);
this.theta = 0.02;
this.phi = 1.03;
this.radius = 15.4;
this.fitToAspect(aspect);
this.updateCamera();
}
fitToAspect(aspect) {
if (aspect < 1.08) {
this.radius = Math.max(this.radius, 15.4 * (1.08 / aspect));
}
this.radius = THREE.MathUtils.clamp(this.radius, this.minRadius, this.maxRadius);
}
}
const controls = new CameraController(camera, canvas);
// ---------- Materials and geometry helpers ----------
const material = (color, options = {}) => new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.05,
flatShading: true,
...options
});
function addBox(w, h, d, x, y, z, mat, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(radius, height, x, y, z, mat, parent = scene, sides = 8) {
const mesh = new THREE.Mesh(new THREE.CylinderGeometry(radius, radius, height, sides), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const upAxis = new THREE.Vector3(0, 1, 0);
function addCylinderBetween(a, b, radius, mat, parent = scene, sides = 7) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const midpoint = new THREE.Vector3().addVectors(a, b).multiplyScalar(0.5);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, sides),
mat
);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(upAxis, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addPolyline(points, mat, radius = 0.055) {
const group = new THREE.Group();
const vectors = points.map(p => p.isVector3 ? p : new THREE.Vector3(...p));
for (let i = 0; i < vectors.length - 1; i++) {
addCylinderBetween(vectors[i], vectors[i + 1], radius, mat, group, 7);
}
for (let i = 1; i < vectors.length - 1; i++) {
const joint = new THREE.Mesh(new THREE.IcosahedronGeometry(radius * 1.12, 0), mat);
joint.position.copy(vectors[i]);
joint.castShadow = true;
group.add(joint);
}
scene.add(group);
return group;
}
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function addLabel(text, position, options = {}) {
const fontSize = options.fontSize || 34;
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
ctx.font = `800 ${fontSize}px system-ui, sans-serif`;
const textWidth = Math.ceil(ctx.measureText(text).width);
canvas.width = textWidth + 42;
canvas.height = 60;
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 2, 2, canvas.width - 4, canvas.height - 4, 13);
ctx.fillStyle = options.background || "rgba(248,251,246,.94)";
ctx.fill();
ctx.strokeStyle = options.border || "rgba(62,87,79,.22)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.font = `800 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = options.color || "#173536";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.position.copy(position);
sprite.renderOrder = 20;
const width = options.worldWidth || Math.max(0.56, canvas.width / 110);
sprite.scale.set(width, canvas.height / 110, 1);
scene.add(sprite);
return sprite;
}
// ---------- Ground and teaching board ----------
const groundMat = material(0xe2e9e1, { roughness: 0.95, metalness: 0 });
const ground = new THREE.Mesh(new THREE.PlaneGeometry(60, 60), groundMat);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.12;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(22, 22, 0xbecdc2, 0xd0dbd1);
grid.position.y = -0.105;
grid.material.transparent = true;
grid.material.opacity = 0.35;
scene.add(grid);
const boardSideMat = material(0xaebdb1);
const boardTopMat = material(0xd8e1d7, { roughness: 0.88 });
const boardEdgeMat = material(0x71867b);
addBox(11.7, 0.31, 6.25, 0, 0.10, 0, boardSideMat);
addBox(11.35, 0.09, 5.9, 0, 0.285, 0, boardTopMat);
addBox(11.55, 0.08, 0.10, 0, 0.31, 3.0, boardEdgeMat);
const boltMat = material(0x61746b, { metalness: 0.55, roughness: 0.42 });
const boltPositions = [
[-5.25, -2.65], [5.25, -2.65], [-5.25, 2.65], [5.25, 2.65]
];
boltPositions.forEach(([x, z]) => {
const bolt = new THREE.Mesh(new THREE.CylinderGeometry(0.10, 0.10, 0.06, 6), boltMat);
bolt.position.set(x, 0.37, z);
bolt.castShadow = true;
scene.add(bolt);
});
// ---------- Circuit points ----------
const V_PLUS = new THREE.Vector3(-4.75, 0.70, 1.25);
const LAMP_IN = new THREE.Vector3(-2.95, 0.68, 1.25);
const LAMP_OUT = new THREE.Vector3(-2.05, 0.68, 1.25);
const COLLECTOR = new THREE.Vector3(1.0, 1.48, 1.25);
const EMITTER = new THREE.Vector3(3.6, 1.48, 1.25);
const BASE = new THREE.Vector3(2.0, 1.48, -0.65);
const V_RETURN = new THREE.Vector3(-4.75, 0.70, 2.55);
const SOURCE_OUT = new THREE.Vector3(-1.05, 0.68, -1.65);
const SOURCE_RETURN = new THREE.Vector3(-2.75, 0.68, -1.65);
const loadWireMat = material(0xe89a26, {
roughness: 0.48,
metalness: 0.16,
emissive: 0xe89a26,
emissiveIntensity: 0.10
});
const controlWireMat = material(0x287eaa, {
roughness: 0.48,
metalness: 0.14,
emissive: 0x287eaa,
emissiveIntensity: 0.08
});
const commonWireMat = material(0x6d8179, { roughness: 0.68 });
addPolyline([V_PLUS, LAMP_IN], loadWireMat, 0.058);
addPolyline([
LAMP_OUT,
new THREE.Vector3(-1.4, 0.68, 1.25),
new THREE.Vector3(-1.4, 1.48, 1.25),
COLLECTOR
], loadWireMat, 0.058);
addPolyline([
EMITTER,
new THREE.Vector3(3.6, 0.68, 1.25),
new THREE.Vector3(4.1, 0.68, 1.25),
new THREE.Vector3(4.1, 0.68, 2.55),
V_RETURN
], loadWireMat, 0.058);
addPolyline([
SOURCE_OUT,
new THREE.Vector3(1.4, 0.68, -1.65),
new THREE.Vector3(1.4, 1.48, -0.65),
BASE
], controlWireMat, 0.052);
addPolyline([
SOURCE_RETURN,
new THREE.Vector3(-3.35, 0.68, -1.65),
new THREE.Vector3(-3.35, 0.68, -2.55),
new THREE.Vector3(-4.95, 0.68, -2.55),
new THREE.Vector3(-4.95, 0.68, 2.55),
V_RETURN
], commonWireMat, 0.036);
// ---------- Power terminals ----------
function addPowerTerminal(x, z, capColor, labelText) {
const baseMat = material(0x53665e, { metalness: 0.42 });
const capMat = material(capColor, { metalness: 0.25, emissive: capColor, emissiveIntensity: 0.05 });
addCylinder(0.28, 0.16, x, 0.41, z, baseMat);
addCylinder(0.12, 0.34, x, 0.62, z, baseMat);
addCylinder(0.23, 0.11, x, 0.83, z, capMat, scene, 8);
addLabel(labelText, new THREE.Vector3(x, 1.48, z), {
worldWidth: labelText.length > 2 ? 0.78 : 0.57,
background: "rgba(249,251,247,.96)"
});
}
addPowerTerminal(-4.75, 1.25, 0xd95d45, "+V");
addPowerTerminal(-4.75, 2.55, 0x4d6d7b, "0 V");
// ---------- Low-poly lamp ----------
const lampGroup = new THREE.Group();
lampGroup.position.set(-2.5, 0, 1.25);
scene.add(lampGroup);
const lampBaseMat = material(0x3d504a, { metalness: 0.4, roughness: 0.46 });
const lampTrimMat = material(0xd8b56a, { metalness: 0.58, roughness: 0.36 });
addCylinder(0.38, 0.45, 0, 0.53, 0, lampBaseMat, lampGroup, 8);
addCylinder(0.31, 0.12, 0, 0.79, 0, lampTrimMat, lampGroup, 8);
addCylinder(0.15, 0.34, 0, 0.94, 0, lampBaseMat, lampGroup, 8);
const bulbMat = material(0xd8b36b, {
roughness: 0.35,
metalness: 0,
emissive: 0xff9a20,
emissiveIntensity: 0.03,
flatShading: true
});
const bulb = new THREE.Mesh(new THREE.IcosahedronGeometry(0.48, 1), bulbMat);
bulb.position.set(0, 1.48, 0);
bulb.castShadow = true;
lampGroup.add(bulb);
const filamentMat = new THREE.MeshBasicMaterial({ color: 0xffbb55 });
addCylinderBetween(
new THREE.Vector3(-0.12, 1.48, 0),
new THREE.Vector3(0.12, 1.48, 0),
0.022,
filamentMat,
lampGroup,
6
);
const haloMat = new THREE.MeshBasicMaterial({
color: 0xffb64c,
transparent: true,
opacity: 0,
depthWrite: false
});
const halo = new THREE.Mesh(new THREE.IcosahedronGeometry(0.66, 1), haloMat);
halo.position.copy(bulb.position);
lampGroup.add(halo);
const lampLight = new THREE.PointLight(0xffa62d, 0, 4.5, 2);
lampLight.position.set(0, 1.48, 0);
lampGroup.add(lampLight);
addLabel("LAMP", new THREE.Vector3(-2.5, 2.35, 1.25), {
worldWidth: 1.15,
background: "rgba(255,246,225,.96)",
border: "rgba(232,154,38,.42)",
color: "#7f531b"
});
// ---------- Simplified transistor block ----------
const transistorBodyMat = material(0x315b57, { roughness: 0.78 });
const transistorTopMat = material(0x244743, { roughness: 0.62 });
const transistorSideMat = material(0x3e6a64, { roughness: 0.75 });
const copperMat = material(0xc78543, { metalness: 0.65, roughness: 0.34 });
const baseMat = material(0x2c87b4, { metalness: 0.35, roughness: 0.38 });
const leverMat = material(0x5fc1aa, {
metalness: 0.32,
roughness: 0.42,
emissive: 0x1d6758,
emissiveIntensity: 0.08
});
addBox(2.8, 0.90, 2.35, 2.0, 0.75, 0.30, transistorBodyMat);
addBox(2.56, 0.09, 2.11, 2.0, 1.235, 0.30, transistorTopMat);
// Side ribs add a faceted, industrial character.
for (let i = 0; i < 4; i++) {
addBox(0.09, 0.56, 1.95, 0.72 + i * 0.83, 0.73, 0.30, transistorSideMat);
}
function addModuleTerminal(position, terminalMaterial) {
addCylinder(0.15, 0.36, position.x, 1.38, position.z, terminalMaterial, scene, 8);
addCylinder(0.22, 0.075, position.x, 1.25, position.z, terminalMaterial, scene, 8);
}
addModuleTerminal(COLLECTOR, copperMat);
addModuleTerminal(EMITTER, copperMat);
addModuleTerminal(BASE, baseMat);
// C and E contact pads.
addCylinder(0.22, 0.08, COLLECTOR.x, 1.42, COLLECTOR.z, copperMat, scene, 10);
addCylinder(0.22, 0.08, EMITTER.x, 1.42, EMITTER.z, copperMat, scene, 10);
// The hinged blade is intentionally symbolic, not literal transistor mechanics.
const lever = new THREE.Group();
lever.position.set(COLLECTOR.x, 1.56, COLLECTOR.z);
scene.add(lever);
addBox(2.60, 0.12, 0.30, 1.30, 0, 0, leverMat, lever);
addBox(0.34, 0.24, 0.48, 2.22, 0.06, 0, leverMat, lever);
addCylinder(0.13, 0.50, 0, 0.03, 0, copperMat, lever, 8);
// Small base-driven status indicator on the block.
const statusMat = material(0x537068, {
emissive: 0x39bd78,
emissiveIntensity: 0
});
const statusLED = new THREE.Mesh(new THREE.IcosahedronGeometry(0.11, 1), statusMat);
statusLED.position.set(2.12, 1.35, -0.10);
scene.add(statusLED);
// A blue pilot line visually links B to the symbolic switch mechanism.
addPolyline([
new THREE.Vector3(BASE.x, 1.53, BASE.z),
new THREE.Vector3(2.0, 1.54, -0.30),
new THREE.Vector3(1.0, 1.54, 1.25)
], controlWireMat, 0.026);
addLabel("C", new THREE.Vector3(COLLECTOR.x, 1.98, COLLECTOR.z), {
worldWidth: 0.55,
background: "rgba(255,238,210,.98)",
border: "rgba(232,154,38,.5)",
color: "#7e531d"
});
addLabel("E", new THREE.Vector3(EMITTER.x, 1.98, EMITTER.z), {
worldWidth: 0.55,
background: "rgba(241,245,241,.98)",
border: "rgba(83,106,102,.38)"
});
addLabel("B", new THREE.Vector3(BASE.x, 1.98, BASE.z), {
worldWidth: 0.55,
background: "rgba(226,243,250,.98)",
border: "rgba(40,126,170,.48)",
color: "#205f7d"
});
addLabel("TRANSISTOR • SIMPLIFIED", new THREE.Vector3(2.0, 3.03, 0.25), {
worldWidth: 3.35,
background: "rgba(244,249,244,.96)",
border: "rgba(60,95,84,.28)"
});
// ---------- Base-drive source and pushbutton ----------
const sourceMat = material(0x55726b);
const sourceTopMat = material(0x75958a);
const sourceButtonMat = material(0x2c87b4, {
metalness: 0.28,
roughness: 0.42,
emissive: 0x2c87b4,
emissiveIntensity: 0.05
});
const sourceIndicatorMat = material(0x48716b, {
emissive: 0x39bd78,
emissiveIntensity: 0
});
addBox(1.75, 0.56, 1.05, -1.9, 0.56, -1.65, sourceMat);
addBox(1.52, 0.08, 0.84, -1.9, 0.88, -1.65, sourceTopMat);
addCylinder(0.26, 0.11, -1.9, 0.94, -1.65, sourceButtonMat, scene, 10);
const plunger = addCylinder(0.17, 0.22, -1.9, 1.02, -1.65, sourceButtonMat, scene, 8);
const sourceIndicator = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.09, 1),
sourceIndicatorMat
);
sourceIndicator.position.set(-1.33, 0.99, -1.34);
scene.add(sourceIndicator);
// Small source terminals.
addCylinder(0.10, 0.18, SOURCE_OUT.x, 0.69, SOURCE_OUT.z, baseMat, scene, 8);
addCylinder(0.10, 0.18, SOURCE_RETURN.x, 0.69, SOURCE_RETURN.z, commonWireMat, scene, 8);
addLabel("BASE DRIVE", new THREE.Vector3(-1.9, 1.75, -1.65), {
worldWidth: 1.65,
background: "rgba(229,244,250,.97)",
border: "rgba(40,126,170,.42)",
color: "#225e79"
});
// Path captions.
addLabel("LOAD PATH", new THREE.Vector3(-1.65, 0.48, 2.92), {
worldWidth: 1.75,
background: "rgba(255,239,212,.95)",
border: "rgba(232,154,38,.38)",
color: "#7f531b"
});
addLabel("CONTROL PATH", new THREE.Vector3(-0.05, 0.48, -2.78), {
worldWidth: 2.20,
background: "rgba(229,244,250,.95)",
border: "rgba(40,126,170,.38)",
color: "#225e79"
});
// ---------- Animated current particles ----------
class PolylinePath {
constructor(points) {
this.points = points.map(p => p.isVector3 ? p.clone() : new THREE.Vector3(...p));
this.lengths = [];
this.total = 0;
for (let i = 0; i < this.points.length - 1; i++) {
const length = this.points[i].distanceTo(this.points[i + 1]);
this.lengths.push(length);
this.total += length;
}
}
sample(t, target) {
let distance = ((t % 1) + 1) % 1 * this.total;
for (let i = 0; i < this.lengths.length; i++) {
const length = this.lengths[i];
if (distance <= length || i === this.lengths.length - 1) {
const local = length === 0 ? 0 : distance / length;
target.lerpVectors(this.points[i], this.points[i + 1], local);
return target;
}
distance -= length;
}
target.copy(this.points[this.points.length - 1]);
return target;
}
}
const loadFlowPath = new PolylinePath([
V_PLUS,
LAMP_IN,
LAMP_OUT,
new THREE.Vector3(-1.4, 0.68, 1.25),
new THREE.Vector3(-1.4, 1.48, 1.25),
COLLECTOR,
new THREE.Vector3(1.35, 1.70, 1.25),
new THREE.Vector3(3.25, 1.70, 1.25),
EMITTER,
new THREE.Vector3(3.6, 0.68, 1.25),
new THREE.Vector3(4.1, 0.68, 1.25),
new THREE.Vector3(4.1, 0.68, 2.55),
V_RETURN
]);
const controlFlowPath = new PolylinePath([
SOURCE_OUT,
new THREE.Vector3(1.4, 0.68, -1.65),
new THREE.Vector3(1.4, 1.48, -0.65),
BASE
]);
const loadParticles = [];
const controlParticles = [];
function makeParticles(path, count, color, size) {
const particleMat = new THREE.MeshBasicMaterial({ color });
const geometry = new THREE.SphereGeometry(size, 6, 4);
const particles = [];
for (let i = 0; i < count; i++) {
const mesh = new THREE.Mesh(geometry, particleMat);
mesh.userData.offset = i / count;
scene.add(mesh);
particles.push(mesh);
}
return particles;
}
loadParticles.push(...makeParticles(loadFlowPath, 22, 0xffd16b, 0.065));
controlParticles.push(...makeParticles(controlFlowPath, 8, 0x8dd8ff, 0.052));
// ---------- UI ----------
const driveToggle = document.getElementById("driveToggle");
const baseRange = document.getElementById("baseRange");
const baseValue = document.getElementById("baseValue");
const driveText = document.getElementById("driveText");
const driveBadge = document.getElementById("driveBadge");
const pathState = document.getElementById("pathState");
const lampState = document.getElementById("lampState");
const flowToggle = document.getElementById("flowToggle");
const resetView = document.getElementById("resetView");
function updateUI() {
const on = state.driveLevel > 0;
const percentage = Math.round(state.driveLevel * 100);
baseRange.value = String(percentage);
baseValue.value = `${percentage}%`;
driveToggle.classList.toggle("active", on);
driveToggle.setAttribute("aria-pressed", String(on));
driveText.textContent = on ? "Base energized" : "Base off";
driveBadge.textContent = on ? "ON" : "OFF";
pathState.textContent = on ? "CLOSED" : "OPEN";
pathState.className = on ? "on" : "off";
lampState.textContent = on ? "ON" : "OFF";
lampState.className = on ? "on" : "off";
}
driveToggle.addEventListener("click", () => {
if (state.driveLevel > 0) {
state.previousDriveLevel = state.driveLevel;
state.driveLevel = 0;
} else {
state.driveLevel = state.previousDriveLevel || 1;
}
updateUI();
});
baseRange.addEventListener("input", () => {
state.driveLevel = Number(baseRange.value) / 100;
if (state.driveLevel > 0) state.previousDriveLevel = state.driveLevel;
updateUI();
});
flowToggle.addEventListener("click", () => {
state.flowPaused = !state.flowPaused;
flowToggle.textContent = state.flowPaused ? "Resume current" : "Pause current";
});
resetView.addEventListener("click", () => {
controls.reset(camera.aspect);
});
updateUI();
// ---------- Resize and animation ----------
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
controls.fitToAspect(camera.aspect);
controls.updateCamera();
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
const offBulbColor = new THREE.Color(0xd8b36b);
const onBulbColor = new THREE.Color(0xffd36a);
const temporary = new THREE.Vector3();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const drive = state.driveLevel;
const lit = drive > 0;
const brightness = lit ? drive : 0;
// Symbolic switch closes when the base is driven.
const targetLeverAngle = lit ? 0 : 0.49;
lever.rotation.z = THREE.MathUtils.damp(lever.rotation.z, targetLeverAngle, 8.5, dt);
// Pushbutton and source indicator.
const targetPlungerY = lit ? 0.94 : 1.02;
plunger.position.y = THREE.MathUtils.damp(plunger.position.y, targetPlungerY, 10, dt);
sourceButtonMat.emissiveIntensity = THREE.MathUtils.damp(
sourceButtonMat.emissiveIntensity,
lit ? 0.36 : 0.04,
8,
dt
);
sourceIndicatorMat.emissiveIntensity = THREE.MathUtils.damp(
sourceIndicatorMat.emissiveIntensity,
lit ? 1.8 : 0,
8,
dt
);
statusMat.emissiveIntensity = THREE.MathUtils.damp(
statusMat.emissiveIntensity,
lit ? 2.0 : 0,
8,
dt
);
// Lamp brightness.
bulbMat.color.copy(offBulbColor).lerp(onBulbColor, brightness);
bulbMat.emissiveIntensity = THREE.MathUtils.damp(
bulbMat.emissiveIntensity,
lit ? 0.12 + brightness * 2.2 : 0.02,
8,
dt
);
lampLight.intensity = THREE.MathUtils.damp(lampLight.intensity, brightness * 30, 8, dt);
haloMat.opacity = THREE.MathUtils.damp(haloMat.opacity, brightness * 0.12, 8, dt);
halo.scale.setScalar(1 + brightness * 0.08);
// Wire highlighting.
loadWireMat.emissiveIntensity = THREE.MathUtils.damp(
loadWireMat.emissiveIntensity,
lit ? 0.18 + brightness * 0.55 : 0.07,
8,
dt
);
controlWireMat.emissiveIntensity = THREE.MathUtils.damp(
controlWireMat.emissiveIntensity,
lit ? 0.18 + brightness * 0.35 : 0.05,
8,
dt
);
leverMat.emissiveIntensity = THREE.MathUtils.damp(
leverMat.emissiveIntensity,
lit ? 0.18 : 0.03,
8,
dt
);
// Moving dots communicate conventional current direction.
if (lit && !state.flowPaused) {
state.flowClock += dt * (0.055 + brightness * 0.075);
}
for (const particle of loadParticles) {
particle.visible = lit;
if (lit) {
loadFlowPath.sample(state.flowClock + particle.userData.offset, temporary);
particle.position.copy(temporary);
particle.position.y += 0.035;
}
}
for (const particle of controlParticles) {
particle.visible = lit;
if (lit) {
controlFlowPath.sample(state.flowClock * 1.15 + particle.userData.offset, temporary);
particle.position.copy(temporary);
particle.position.y += 0.028;
}
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```