Prompt specification
Build or customize the Three.js 3D scene "Logic input controlling motor output" 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: Logic input controlling motor output
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/logic-input-controlling-motor-output-x1678
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a transistor used as an electronic switch. Show a small control signal enabling a modeled motor circuit through a switch abstraction; distinguish information flow from electrical energy supplied to the load. Stylized solid forms.
Working Three.js Starter (logic-input-controlling-motor-output-x1678):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Transistor Electronic Switch</title>
<style>
:root {
color-scheme: dark;
--bg: #08111f;
--panel: rgba(11, 24, 41, 0.82);
--border: rgba(159, 205, 226, 0.2);
--text: #edf7ff;
--muted: #9db2c7;
--cyan: #49e5ff;
--amber: #ffad3d;
--green: #73f2b2;
}
* { 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;
color: var(--text);
}
#app {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
header {
position: fixed;
z-index: 5;
top: 24px;
left: 28px;
max-width: 490px;
pointer-events: none;
text-shadow: 0 2px 15px rgba(0, 0, 0, 0.65);
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 7px;
font-size: clamp(24px, 3vw, 39px);
line-height: 1.05;
letter-spacing: -0.035em;
}
header p {
margin: 0;
max-width: 440px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
position: fixed;
z-index: 5;
top: 24px;
right: 24px;
width: 250px;
padding: 15px 16px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(14px);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.24);
}
.legend-title {
margin-bottom: 11px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 31px 1fr;
align-items: center;
gap: 10px;
margin: 9px 0;
}
.legend-line {
position: relative;
width: 29px;
height: 5px;
border-radius: 10px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(73, 229, 255, 0.55);
}
.legend-line.energy {
height: 10px;
background: var(--amber);
box-shadow: 0 0 14px rgba(255, 173, 61, 0.6);
}
.legend-row b {
display: block;
font-size: 12px;
}
.legend-row small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
min-width: 145px;
padding: 9px 11px 9px 13px;
border: 1px solid var(--border);
border-left: 3px solid var(--cyan);
border-radius: 9px;
background: rgba(7, 17, 30, 0.88);
box-shadow: 0 9px 25px rgba(0, 0, 0, 0.28);
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
white-space: nowrap;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 14px;
background: var(--cyan);
opacity: 0.65;
}
.world-label b {
display: block;
font-size: 10px;
letter-spacing: 0.08em;
}
.world-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.world-label.power {
border-left-color: var(--amber);
}
.world-label.power::after {
background: var(--amber);
}
.world-label.switch {
border-left-color: var(--green);
}
.world-label.switch::after {
background: var(--green);
}
.world-label.motor {
border-left-color: #dce8f4;
}
.world-label.motor::after {
background: #dce8f4;
}
.controls {
position: fixed;
z-index: 6;
bottom: 22px;
left: 50%;
display: flex;
align-items: center;
gap: 10px;
transform: translateX(-50%);
}
button {
appearance: none;
border: 1px solid var(--border);
border-radius: 11px;
padding: 11px 15px;
background: rgba(10, 23, 39, 0.9);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(10px);
transition: border-color 150ms, background 150ms, transform 150ms;
}
button:hover {
border-color: rgba(73, 229, 255, 0.55);
background: rgba(19, 42, 61, 0.95);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
#toggle {
display: flex;
align-items: center;
gap: 9px;
min-width: 158px;
}
#toggle .status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 12px rgba(115, 242, 178, 0.8);
}
#toggle.off .status-dot {
background: #68788a;
box-shadow: none;
}
.hint {
position: fixed;
z-index: 5;
right: 25px;
bottom: 25px;
color: #71869c;
font-size: 10px;
text-align: right;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: var(--muted);
font-size: 13px;
letter-spacing: 0.08em;
}
@media (max-width: 760px) {
header {
top: 17px;
left: 18px;
right: 18px;
}
header p { max-width: 330px; }
.legend {
top: auto;
right: 15px;
bottom: 78px;
width: 205px;
padding: 11px 13px;
}
.legend-title { display: none; }
.legend-row { margin: 6px 0; }
.world-label {
min-width: 115px;
padding: 7px 9px;
}
.world-label span { font-size: 9px; }
.hint { display: none; }
.controls {
bottom: 16px;
width: calc(100% - 30px);
justify-content: center;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="loading">LOADING TRANSISTOR SWITCH…</div>
<div id="labels"></div>
<header>
<div class="eyebrow">Electronic switching / signal vs. power</div>
<h1>Transistor as an electronic switch</h1>
<p>
A small gate command controls a separate power circuit. Cyan represents
information flow; amber represents electrical energy supplied to the motor.
</p>
</header>
<aside class="legend">
<div class="legend-title">Path key</div>
<div class="legend-row">
<div class="legend-line"></div>
<div>
<b>Information flow</b>
<small>Thin cyan gate command</small>
</div>
</div>
<div class="legend-row">
<div class="legend-line energy"></div>
<div>
<b>Electrical energy</b>
<small>Thick amber load current</small>
</div>
</div>
</aside>
<div class="controls">
<button id="toggle" type="button" aria-live="polite">
<span class="status-dot"></span>
<span id="toggleText">Disable control</span>
</button>
<button id="reset" type="button">Reset view</button>
</div>
<div class="hint">
DRAG TO ORBIT · SCROLL TO ZOOM · SPACE TO TOGGLE<br />
R TO RESET CAMERA
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, lighting
// -------------------------------------------------------------------------
const app = document.querySelector("#app");
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111f);
scene.fog = new THREE.Fog(0x08111f, 15, 30);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
theta: 0.57,
phi: 1.02,
radius: 13.8,
target: new THREE.Vector3(0, 0.9, -0.15)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xc6e8ff, 0x172238, 2.1);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
keyLight.position.set(4, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x35dfff, 22, 11, 2);
cyanLight.position.set(-3.7, 3.1, 3.2);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xff8e27, 30, 12, 2);
amberLight.position.set(3.2, 3.7, 0.2);
scene.add(amberLight);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const mat = {
floor: new THREE.MeshStandardMaterial({
color: 0x0c1725,
roughness: 0.92,
metalness: 0.05
}),
dark: new THREE.MeshStandardMaterial({
color: 0x172638,
roughness: 0.58,
metalness: 0.35
}),
darker: new THREE.MeshStandardMaterial({
color: 0x0c1724,
roughness: 0.46,
metalness: 0.48
}),
metal: new THREE.MeshStandardMaterial({
color: 0xaab9c7,
roughness: 0.27,
metalness: 0.88
}),
copper: new THREE.MeshStandardMaterial({
color: 0xd7803e,
roughness: 0.28,
metalness: 0.8
}),
controller: new THREE.MeshStandardMaterial({
color: 0x1d5268,
roughness: 0.45,
metalness: 0.36
}),
controllerDark: new THREE.MeshStandardMaterial({
color: 0x06141e,
roughness: 0.3,
metalness: 0.2
}),
transistor: new THREE.MeshStandardMaterial({
color: 0x10151d,
roughness: 0.34,
metalness: 0.22
}),
switchBase: new THREE.MeshStandardMaterial({
color: 0x173044,
roughness: 0.47,
metalness: 0.48
}),
frame: new THREE.MeshBasicMaterial({
color: 0x49e5ff,
transparent: true,
opacity: 0.34,
depthWrite: false
}),
motor: new THREE.MeshStandardMaterial({
color: 0x426a7b,
roughness: 0.42,
metalness: 0.58
}),
motorDark: new THREE.MeshStandardMaterial({
color: 0x203746,
roughness: 0.5,
metalness: 0.55
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x49e5ff,
emissive: 0x12b9d5,
emissiveIntensity: 0.8,
roughness: 0.28,
metalness: 0.15
}),
amber: new THREE.MeshStandardMaterial({
color: 0xffad3d,
emissive: 0xff5b08,
emissiveIntensity: 0.45,
roughness: 0.3,
metalness: 0.22
}),
green: new THREE.MeshStandardMaterial({
color: 0x73f2b2,
emissive: 0x1bd477,
emissiveIntensity: 0.8,
roughness: 0.3
})
};
function roundedShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const r = Math.min(radius, width / 2, height / 2);
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width, y + height,
x + width - r, y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
return shape;
}
function roundedBox(width, height, depth, radius, material) {
const bevel = Math.min(radius * 0.24, 0.045);
const geometry = new THREE.ExtrudeGeometry(
roundedShape(width, height, radius),
{
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: bevel,
bevelThickness: bevel,
curveSegments: 8
}
);
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function cylinderBetween(start, end, radius, material, radialSegments = 14) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius, radius, length, radialSegments
);
const mesh = new THREE.Mesh(geometry, 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;
return mesh;
}
function makeTube(curve, radius, material, segments = 80) {
const geometry = new THREE.TubeGeometry(
curve, segments, radius, 10, false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function curveFrom(points) {
return new THREE.CatmullRomCurve3(
points.map((p) => new THREE.Vector3(...p)),
false,
"catmullrom",
0.18
);
}
// -------------------------------------------------------------------------
// Ground
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
mat.floor
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(28, 28, 0x29465b, 0x162b3d);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
// -------------------------------------------------------------------------
// Control logic block
// -------------------------------------------------------------------------
const controller = new THREE.Group();
scene.add(controller);
const controllerBody = roundedBox(1.9, 1.0, 1.15, 0.14, mat.controller);
controllerBody.position.set(-4.2, 0.57, 1.65);
controller.add(controllerBody);
const screen = roundedBox(1.18, 0.45, 0.065, 0.06, mat.controllerDark);
screen.position.set(-4.02, 0.67, 2.235);
controller.add(screen);
const screenGlow = new THREE.Mesh(
new THREE.PlaneGeometry(0.9, 0.25),
new THREE.MeshBasicMaterial({
color: 0x49e5ff,
transparent: true,
opacity: 0.42
})
);
screenGlow.position.set(-4.02, 0.67, 2.273);
controller.add(screenGlow);
const controllerLedMaterial = mat.green.clone();
const controllerLed = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
controllerLedMaterial
);
controllerLed.position.set(-4.78, 0.68, 2.27);
controller.add(controllerLed);
const controllerPort = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.11, 0.24, 18),
mat.cyan
);
controllerPort.rotation.z = Math.PI / 2;
controllerPort.position.set(-3.16, 0.72, 1.65);
controller.add(controllerPort);
// -------------------------------------------------------------------------
// Separate high-current power source
// -------------------------------------------------------------------------
const powerSupply = new THREE.Group();
scene.add(powerSupply);
const batteryBody = roundedBox(2.4, 1.1, 1.2, 0.16, mat.dark);
batteryBody.position.set(-4.2, 0.59, -1.65);
powerSupply.add(batteryBody);
const batteryTop = roundedBox(2.16, 0.12, 1.02, 0.08, mat.darker);
batteryTop.position.set(-4.2, 1.17, -1.65);
powerSupply.add(batteryTop);
const positiveTerminal = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.19, 0.3, 20),
mat.amber
);
positiveTerminal.position.set(-3.55, 1.34, -1.65);
powerSupply.add(positiveTerminal);
const negativeTerminal = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.19, 0.3, 20),
mat.metal
);
negativeTerminal.position.set(-4.85, 1.34, -1.65);
powerSupply.add(negativeTerminal);
// Raised plus sign on the source's front.
const plusMaterial = new THREE.MeshBasicMaterial({ color: 0xffad3d });
const plusH = roundedBox(0.42, 0.1, 0.035, 0.025, plusMaterial);
const plusV = roundedBox(0.1, 0.42, 0.035, 0.025, plusMaterial);
plusH.position.set(-3.55, 0.74, -1.035);
plusV.position.copy(plusH.position);
powerSupply.add(plusH, plusV);
// -------------------------------------------------------------------------
// Switch abstraction and physical transistor package
// -------------------------------------------------------------------------
const switchGroup = new THREE.Group();
scene.add(switchGroup);
const switchBase = roundedBox(2.6, 0.28, 1.65, 0.12, mat.switchBase);
switchBase.position.set(-0.4, 0.18, 0);
switchGroup.add(switchBase);
const baseEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(2.62, 0.29, 1.67)),
new THREE.LineBasicMaterial({
color: 0x49e5ff,
transparent: true,
opacity: 0.46
})
);
baseEdges.position.copy(switchBase.position);
switchGroup.add(baseEdges);
// Transparent frame makes the switch a distinct enclosure.
const framePosts = [
[-1.55, -0.66],
[0.75, -0.66],
[-1.55, 0.66],
[0.75, 0.66]
];
for (const [x, z] of framePosts) {
const post = cylinderBetween(
new THREE.Vector3(x, 0.32, z),
new THREE.Vector3(x, 2.24, z),
0.025,
mat.frame,
8
);
switchGroup.add(post);
}
for (const z of [-0.66, 0.66]) {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(2.32, 0.045, 0.045),
mat.frame
);
bar.position.set(-0.4, 2.24, z);
switchGroup.add(bar);
}
// Three transistor leads. The center/front lead is the control base.
const collectorX = -0.82;
const emitterX = 0.02;
const baseX = -0.4;
for (const [x, z] of [
[collectorX, 0],
[emitterX, 0],
[baseX, 0.28]
]) {
const lead = cylinderBetween(
new THREE.Vector3(x, 0.3, z),
new THREE.Vector3(x, 0.74, z),
0.055,
mat.metal,
14
);
switchGroup.add(lead);
}
// Solid semicircular TO-92-style transistor package.
const transistorShape = new THREE.Shape();
transistorShape.moveTo(-0.68, 0);
transistorShape.lineTo(0.68, 0);
transistorShape.lineTo(0.68, 0.58);
transistorShape.absarc(0, 0.58, 0.68, 0, Math.PI, false);
transistorShape.lineTo(-0.68, 0);
const transistorGeometry = new THREE.ExtrudeGeometry(transistorShape, {
depth: 0.55,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.055,
bevelThickness: 0.055,
curveSegments: 24
});
transistorGeometry.translate(0, 0, -0.275);
transistorGeometry.computeVertexNormals();
const transistorBody = new THREE.Mesh(transistorGeometry, mat.transistor);
transistorBody.position.set(baseX, 0.69, 0);
transistorBody.castShadow = true;
transistorBody.receiveShadow = true;
switchGroup.add(transistorBody);
// Small indicator on the transistor face.
const transistorBadge = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 0.035, 24),
new THREE.MeshStandardMaterial({
color: 0x49e5ff,
emissive: 0x138ca5,
emissiveIntensity: 0.45,
roughness: 0.35
})
);
transistorBadge.rotation.x = Math.PI / 2;
transistorBadge.position.set(baseX, 1.35, 0.3);
switchGroup.add(transistorBadge);
// Visible conceptual open/closed contact above the transistor.
const contactMaterial = mat.amber.clone();
const contactLeft = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.1, 0.22, 18),
mat.copper
);
contactLeft.position.set(-0.96, 2.13, -0.1);
switchGroup.add(contactLeft);
const contactRight = contactLeft.clone();
contactRight.position.x = 0.16;
switchGroup.add(contactRight);
const contactBridge = new THREE.Mesh(
roundedBox(1.12, 0.12, 0.2, 0.05, contactMaterial).geometry,
contactMaterial
);
contactBridge.position.set(-0.4, 2.2, -0.1);
contactBridge.castShadow = true;
switchGroup.add(contactBridge);
// -------------------------------------------------------------------------
// Motor and rotating load
// -------------------------------------------------------------------------
const motorGroup = new THREE.Group();
motorGroup.position.set(3.55, 1.35, -0.55);
scene.add(motorGroup);
const motorBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.73, 0.73, 1.25, 36),
mat.motor
);
motorBody.rotation.x = Math.PI / 2;
motorBody.castShadow = true;
motorBody.receiveShadow = true;
motorGroup.add(motorBody);
const motorBack = new THREE.Mesh(
new THREE.CylinderGeometry(0.62, 0.62, 0.1, 36),
mat.motorDark
);
motorBack.rotation.x = Math.PI / 2;
motorBack.position.z = -0.66;
motorGroup.add(motorBack);
const motorRing = new THREE.Mesh(
new THREE.TorusGeometry(0.54, 0.065, 12, 36),
mat.copper
);
motorRing.position.z = 0.67;
motorGroup.add(motorRing);
const rotor = new THREE.Group();
rotor.position.z = 0.73;
motorGroup.add(rotor);
for (let i = 0; i < 4; i++) {
const blade = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.55, 0.075),
i % 2 === 0 ? mat.cyan : mat.motorDark
);
blade.position.y = 0.27;
blade.rotation.z = i * Math.PI / 2;
blade.castShadow = true;
rotor.add(blade);
}
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.17, 0.16, 24),
mat.metal
);
hub.rotation.x = Math.PI / 2;
rotor.add(hub);
const motorLegMaterial = mat.dark;
for (const x of [-0.4, 0.4]) {
const leg = roundedBox(0.3, 0.75, 0.35, 0.06, motorLegMaterial);
leg.position.set(x, -0.77, 0);
motorGroup.add(leg);
const foot = roundedBox(0.55, 0.1, 0.65, 0.05, motorLegMaterial);
foot.position.set(x, -1.13, 0);
motorGroup.add(foot);
}
const motorPositive = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.32, 18),
mat.copper
);
motorPositive.rotation.z = Math.PI / 2;
motorPositive.position.set(2.65, 1.3, -0.95);
scene.add(motorPositive);
const motorNegative = motorPositive.clone();
motorNegative.position.z = -0.15;
scene.add(motorNegative);
// -------------------------------------------------------------------------
// Signal and electrical-energy paths
// -------------------------------------------------------------------------
const controlCurve = curveFrom([
[-3.16, 0.72, 1.65],
[-2.55, 0.72, 1.65],
[-1.72, 0.86, 1.16],
[-0.82, 0.68, 0.53],
[-0.4, 0.68, 0.28]
]);
const powerInCurve = curveFrom([
[-3.55, 1.34, -1.65],
[-2.78, 1.34, -1.65],
[-1.76, 0.58, -0.7],
[-0.82, 0.58, 0]
]);
const powerOutCurve = curveFrom([
[0.02, 0.58, 0],
[0.85, 0.58, -0.12],
[1.65, 0.72, -0.72],
[2.65, 1.3, -0.95]
]);
const returnCurve = curveFrom([
[2.65, 1.3, -0.15],
[2.2, 0.42, -1.55],
[0.3, 0.32, -2.5],
[-2.6, 0.42, -2.55],
[-4.85, 1.34, -1.65]
]);
const controlTubeMaterial = mat.cyan.clone();
const controlTube = makeTube(controlCurve, 0.045, controlTubeMaterial);
scene.add(controlTube);
const energyTubeMaterial = mat.amber.clone();
const energyGlowMaterial = new THREE.MeshBasicMaterial({
color: 0xff8e27,
transparent: true,
opacity: 0.11,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const energyCurves = [powerInCurve, powerOutCurve, returnCurve];
for (const curve of energyCurves) {
scene.add(makeTube(curve, 0.105, energyTubeMaterial));
scene.add(makeTube(curve, 0.175, energyGlowMaterial));
}
// Moving particles reinforce direction and the distinction between paths.
const controlPacketMaterial = new THREE.MeshBasicMaterial({
color: 0xb5f7ff,
transparent: true,
opacity: 1
});
const controlPacketGeometry = new THREE.SphereGeometry(0.082, 14, 10);
const controlPackets = [];
for (let i = 0; i < 5; i++) {
const packet = new THREE.Mesh(
controlPacketGeometry,
controlPacketMaterial
);
packet.userData.offset = i / 5;
scene.add(packet);
controlPackets.push(packet);
}
const energyPacketMaterial = new THREE.MeshBasicMaterial({
color: 0xffd18b,
transparent: true,
opacity: 1
});
const energyPacketGeometry = new THREE.SphereGeometry(0.13, 14, 10);
const energyPackets = [];
for (let curveIndex = 0; curveIndex < energyCurves.length; curveIndex++) {
for (let i = 0; i < 5; i++) {
const packet = new THREE.Mesh(
energyPacketGeometry,
energyPacketMaterial
);
packet.userData.curveIndex = curveIndex;
packet.userData.offset = i / 5;
scene.add(packet);
energyPackets.push(packet);
}
}
// -------------------------------------------------------------------------
// World-space HTML labels
// -------------------------------------------------------------------------
const labelsElement = document.querySelector("#labels");
const projectedLabels = [];
function addWorldLabel(html, anchor, className = "") {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.innerHTML = html;
labelsElement.appendChild(element);
projectedLabels.push({
element,
anchor: new THREE.Vector3(...anchor)
});
return element;
}
addWorldLabel(
"<b>CONTROL / GATE</b><span>small signal · information only</span>",
[-4.2, 1.75, 1.65],
"control"
);
addWorldLabel(
"<b>DC SUPPLY</b><span>source of motor energy</span>",
[-4.2, 1.65, -1.65],
"power"
);
const switchLabel = addWorldLabel(
"<b>TRANSISTOR SWITCH</b><span id='switchStateText'>closed · C–E conducting</span>",
[-0.4, 2.65, 0],
"switch"
);
addWorldLabel(
"<b>MOTOR / LOAD</b><span>electrical energy → motion</span>",
[3.55, 2.25, -0.55],
"motor"
);
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of projectedLabels) {
const projected = label.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
label.element.style.display = visible ? "block" : "none";
if (visible) {
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`;
}
}
}
// -------------------------------------------------------------------------
// Direct camera controls — no OrbitControls or addons
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
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;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.42,
1.38
);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
8.5,
21
);
}, { passive: false });
function resetCamera() {
cameraState.theta = 0.57;
cameraState.phi = 1.02;
cameraState.radius = 13.8;
cameraState.target.set(0, 0.9, -0.15);
}
document.querySelector("#reset").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Switch state and animation
// -------------------------------------------------------------------------
let enabled = true;
let switchState = 1;
let motorSpeed = 8;
const toggleButton = document.querySelector("#toggle");
const toggleText = document.querySelector("#toggleText");
const switchStateText = document.querySelector("#switchStateText");
function toggleControl() {
enabled = !enabled;
toggleButton.classList.toggle("off", !enabled);
toggleText.textContent = enabled ? "Disable control" : "Enable control";
}
toggleButton.addEventListener("click", toggleControl);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
toggleControl();
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
const clock = new THREE.Clock();
const tempColor = new THREE.Color();
const dimCyan = new THREE.Color(0x275565);
const hotCyan = new THREE.Color(0x49e5ff);
const dimAmber = new THREE.Color(0x5e4934);
const hotAmber = new THREE.Color(0xffad3d);
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
switchState += (
(enabled ? 1 : 0) - switchState
) * (1 - Math.exp(-dt * 8));
// Contact abstraction visibly opens and closes.
contactBridge.position.y = THREE.MathUtils.lerp(
2.43, 2.2, switchState
);
contactBridge.rotation.z = THREE.MathUtils.lerp(
0.55, 0, switchState
);
contactMaterial.color.lerpColors(
dimAmber, hotAmber, switchState
);
contactMaterial.emissiveIntensity =
0.04 + switchState * 0.8;
controllerLedMaterial.color.lerpColors(
new THREE.Color(0x68788a),
new THREE.Color(0x73f2b2),
switchState
);
controllerLedMaterial.emissiveIntensity =
switchState * 1.1;
screenGlow.material.opacity = 0.08 + switchState * 0.42;
controlTubeMaterial.color.lerpColors(
dimCyan, hotCyan, switchState
);
controlTubeMaterial.emissiveIntensity =
0.08 + switchState * 0.8;
energyTubeMaterial.color.lerpColors(
dimAmber, hotAmber, switchState
);
energyTubeMaterial.emissiveIntensity =
0.03 + switchState * 0.55;
energyGlowMaterial.opacity = 0.018 + switchState * 0.12;
controlPacketMaterial.opacity = switchState;
energyPacketMaterial.opacity = switchState;
for (const packet of controlPackets) {
const u = (
elapsed * 0.33 + packet.userData.offset
) % 1;
packet.position.copy(controlCurve.getPointAt(u));
packet.scale.setScalar(0.7 + switchState * 0.3);
}
for (const packet of energyPackets) {
const curve = energyCurves[packet.userData.curveIndex];
const u = (
elapsed * 0.22 +
packet.userData.offset +
packet.userData.curveIndex * 0.08
) % 1;
packet.position.copy(curve.getPointAt(u));
packet.scale.setScalar(0.6 + switchState * 0.4);
}
motorSpeed += (
(enabled ? 8 : 0) - motorSpeed
) * (1 - Math.exp(-dt * 4));
rotor.rotation.z -= dt * motorSpeed;
switchStateText.textContent = enabled
? "closed · C–E conducting"
: "open · load disconnected";
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
document.querySelector("#loading").remove();
</script>
</body>
</html>
```