Prompt specification
Build or customize the Three.js 3D scene "Flashlight circuit teaching cutaway" 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: Flashlight circuit teaching cutaway
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/flashlight-circuit-teaching-cutaway-x1615
Scene Description Prompt:
Compose a stylized flashlight with source, switch, and lamp connections exposed; trace a complete loop and distinguish the mechanical switch position from decorative shell geometry. Use Three.js from cdnjs in a single HTML file, with visible voxel blocks.
Working Three.js Starter (flashlight-circuit-teaching-cutaway-x1615):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Flashlight — Exposed Circuit</title>
<style>
:root {
--bg: #091321;
--panel: rgba(10, 21, 36, 0.88);
--line: rgba(160, 195, 225, 0.22);
--text: #e8f3ff;
--muted: #91a8be;
--amber: #ffbd3b;
--cyan: #59e9ff;
--shell: #71839d;
--green: #65f0a5;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 1;
background:
radial-gradient(circle at 50% 43%, transparent 42%, rgba(2, 7, 14, 0.48) 100%),
linear-gradient(180deg, rgba(19, 44, 69, 0.08), rgba(3, 8, 15, 0.22));
}
.panel {
position: fixed;
z-index: 5;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 10px;
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(10px);
}
#title-panel {
top: 20px;
left: 20px;
width: 320px;
padding: 17px 18px;
}
.eyebrow {
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 6px 0 8px;
font-size: 23px;
line-height: 1;
letter-spacing: 0.04em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
#legend {
top: 20px;
right: 20px;
width: 238px;
padding: 14px 16px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 8px 0;
color: #c7d7e6;
font-size: 10px;
}
.swatch {
width: 13px;
height: 13px;
flex: 0 0 13px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(255,255,255,.2);
}
.swatch.wire { background: #dc941d; }
.swatch.current { background: #fff18b; box-shadow: 0 0 9px #ffca45; }
.swatch.shell { background: #536985; }
.swatch.source { background: #e7772d; }
.swatch.lamp { background: #50e8ff; box-shadow: 0 0 9px #50e8ff; }
#status-panel {
left: 20px;
bottom: 20px;
width: 330px;
padding: 15px 16px;
}
.status-line {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
.status-value {
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: .12em;
}
#status-panel[data-state="open"] .status-value {
color: #ff9d5c;
}
.status-dot {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 10px var(--green);
}
#status-panel[data-state="open"] .status-dot {
background: #ff9d5c;
box-shadow: 0 0 10px #ff9d5c;
}
.circuit-path {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 6px;
margin: 12px 0;
color: #c5d7e7;
font-size: 9px;
line-height: 1.5;
}
.circuit-path b {
color: var(--amber);
}
button {
width: 100%;
padding: 10px 12px;
border: 1px solid rgba(255, 189, 59, 0.55);
border-radius: 6px;
background: rgba(255, 189, 59, 0.12);
color: #ffe0a0;
font: 700 10px ui-monospace, monospace;
letter-spacing: .1em;
cursor: pointer;
pointer-events: auto;
}
button:hover {
background: rgba(255, 189, 59, 0.22);
}
#help {
right: 20px;
bottom: 20px;
max-width: 260px;
padding: 12px 14px;
color: var(--muted);
font-size: 10px;
line-height: 1.65;
}
#help strong { color: #d7e9f8; }
#leaders,
#annotations {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
pointer-events: none;
z-index: 3;
}
#leaders { overflow: visible; }
.callout {
position: absolute;
transform: translate(-50%, -50%);
padding: 7px 9px;
border-left: 3px solid var(--cyan);
border-radius: 3px;
background: rgba(7, 15, 26, 0.9);
box-shadow: 0 8px 24px rgba(0,0,0,.25);
color: #dcecff;
font-size: 9px;
font-weight: 700;
letter-spacing: .08em;
line-height: 1.35;
text-transform: uppercase;
white-space: nowrap;
}
.callout.shell { border-color: #8398b2; }
.callout.copper { border-color: var(--amber); }
.callout.current { border-color: #fff18b; }
.callout.lamp { border-color: var(--cyan); }
@media (max-width: 760px) {
#title-panel { width: 250px; }
#legend, #help { display: none; }
#status-panel { width: 290px; }
.callout { font-size: 8px; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="vignette"></div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="annotations" aria-hidden="true">
<div class="callout" id="ann-source">Power source · 3.0 V DC</div>
<div class="callout shell" id="ann-shell">Decorative shell · no current</div>
<div class="callout shell" id="ann-housing">Non-conductive switch cover</div>
<div class="callout copper" id="ann-mechanical">Mechanical contact · closed</div>
<div class="callout lamp" id="ann-lamp">Lamp · exposed + / − leads</div>
<div class="callout current" id="ann-loop">Live loop · moving current cubes</div>
</div>
<section class="panel" id="title-panel">
<div class="eyebrow">Exposed circuit study / 01</div>
<h1>VOXEL FLASHLIGHT</h1>
<p class="subtitle">
Cutaway assembly with a complete battery → switch → lamp → battery loop.
The copper arm is the mechanical switch; slate pieces are covers only.
</p>
</section>
<section class="panel" id="legend">
<div class="eyebrow">Material key</div>
<div class="legend-row"><span class="swatch wire"></span>Conductive wiring</div>
<div class="legend-row"><span class="swatch current"></span>Current direction</div>
<div class="legend-row"><span class="swatch shell"></span>Decorative / insulating</div>
<div class="legend-row"><span class="swatch source"></span>Power source</div>
<div class="legend-row"><span class="swatch lamp"></span>Lamp emitter</div>
</section>
<section class="panel" id="status-panel" data-state="closed" aria-live="polite">
<div class="status-line">
<span><span class="status-dot"></span>Mechanical position</span>
<span class="status-value" id="switch-state">CLOSED</span>
</div>
<div class="status-line" style="margin-top:8px">
<span>Circuit</span>
<span class="status-value" id="circuit-state">COMPLETE LOOP</span>
</div>
<div class="circuit-path">
<span>SOURCE +</span><b>→</b>
<span>SWITCH</span><b>→</b>
<span>LAMP +</span><b>→</b>
<span>LAMP −</span><b>→</b>
<span>SOURCE −</span>
</div>
<button id="switch-button" type="button">OPEN MECHANICAL SWITCH</button>
</section>
<section class="panel" id="help">
<strong>Controls</strong><br>
Drag to orbit · wheel to zoom<br>
Click the copper switch arm to toggle it<br>
Press <strong>Space</strong> for the switch
</section>
<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 renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x091321);
scene.fog = new THREE.Fog(0x091321, 15, 31);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const target = new THREE.Vector3(-0.15, -0.05, 0);
let theta = 0.42;
let phi = 1.18;
let radius = innerWidth / innerHeight < 1 ? 18 : 13;
function updateCamera() {
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xc4e4ff, 0x172033, 2.2));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(3, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ca8ff, 1.5);
rimLight.position.set(-6, 3, -5);
scene.add(rimLight);
const lampLight = new THREE.PointLight(0x49eaff, 28, 6, 2);
lampLight.position.set(3.12, 0.05, 0);
scene.add(lampLight);
const materials = {
shell: new THREE.MeshStandardMaterial({
color: 0x304963, roughness: 0.78, metalness: 0.28
}),
shellDark: new THREE.MeshStandardMaterial({
color: 0x172a40, roughness: 0.88, metalness: 0.18
}),
housing: new THREE.MeshStandardMaterial({
color: 0x697a91, roughness: 0.82, metalness: 0.08
}),
source: new THREE.MeshStandardMaterial({
color: 0xe7792f, roughness: 0.52, metalness: 0.22
}),
sourceDark: new THREE.MeshStandardMaterial({
color: 0x8f421e, roughness: 0.68, metalness: 0.18
}),
copper: new THREE.MeshStandardMaterial({
color: 0xf0a52c,
emissive: 0x6d3100,
emissiveIntensity: 0.55,
roughness: 0.24,
metalness: 0.82
}),
wire: new THREE.MeshStandardMaterial({
color: 0xe6a326,
emissive: 0x6e3700,
emissiveIntensity: 0.5,
roughness: 0.35,
metalness: 0.68
}),
positive: new THREE.MeshStandardMaterial({
color: 0xff4b50, emissive: 0x730008, emissiveIntensity: 0.7
}),
negative: new THREE.MeshStandardMaterial({
color: 0x388dff, emissive: 0x001d70, emissiveIntensity: 0.7
}),
white: new THREE.MeshBasicMaterial({ color: 0xf4fbff }),
reflector: new THREE.MeshStandardMaterial({
color: 0xd3aa55, roughness: 0.3, metalness: 0.72
}),
led: new THREE.MeshStandardMaterial({
color: 0x8ff7ff,
emissive: 0x20dfff,
emissiveIntensity: 2.8,
roughness: 0.22,
metalness: 0.08
}),
lens: new THREE.MeshPhysicalMaterial({
color: 0xa6efff,
transparent: true,
opacity: 0.22,
roughness: 0.08,
metalness: 0,
transmission: 0.3,
depthWrite: false
}),
current: new THREE.MeshBasicMaterial({
color: 0xfff28b,
toneMapped: false
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x06101b,
transparent: true,
opacity: 0.62
});
function block(w, h, d, x, y, z, material, edges = false, parent = scene) {
const geometry = new THREE.BoxGeometry(w, h, d);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
if (edges) {
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
mesh.add(outline);
}
parent.add(mesh);
return mesh;
}
function frameAtX(x, outer, bar, depth, material) {
block(depth, bar, outer * 2, x, outer - bar / 2, 0, material);
block(depth, bar, outer * 2, x, -outer + bar / 2, 0, material);
block(depth, outer * 2 - bar, bar, x, 0, outer - bar / 2, material);
block(depth, outer * 2 - bar, bar, x, 0, -outer + bar / 2, material);
}
/* Voxel floor */
{
const tileGeometry = new THREE.BoxGeometry(0.48, 0.075, 0.48);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
metalness: 0.04
});
const cols = 28;
const rows = 17;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, cols * rows);
const matrix = new THREE.Matrix4();
const color = new THREE.Color();
let index = 0;
for (let z = 0; z < rows; z++) {
for (let x = 0; x < cols; x++) {
matrix.makeTranslation(
(x - (cols - 1) / 2) * 0.51,
-1.38,
(z - (rows - 1) / 2) * 0.51
);
tiles.setMatrixAt(index, matrix);
const variation = ((x * 17 + z * 11) % 5) * 0.012;
color.setHSL(0.59, 0.25, 0.075 + variation);
tiles.setColorAt(index, color);
index++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
}
/* Display cradle */
block(7.9, 0.13, 0.7, -0.2, -1.08, -0.28, materials.shellDark, true);
/* Decorative flashlight shell: intentionally partial/cut away */
for (let x = -4.1; x <= 3.55; x += 0.36) {
block(0.31, 0.28, 0.31, x, 0.77, -0.55, materials.shell);
block(0.31, 0.28, 0.31, x, -0.76, 0.54, materials.shell);
}
block(7.75, 0.12, 0.12, -0.28, 0.68, -0.55, materials.shellDark);
frameAtX(-4.22, 0.91, 0.28, 0.3, materials.shell);
frameAtX(3.47, 1.02, 0.29, 0.32, materials.shell);
/* Grip accents around the battery source */
for (const x of [-3.95, -3.15, -2.35]) {
block(0.15, 1.63, 1.72, x, -0.02, 0, materials.shellDark);
}
/* Voxel power source */
block(1.92, 0.16, 1.36, -3.15, -0.61, 0, materials.sourceDark, true);
block(0.5, 0.94, 1.18, -3.65, -0.04, 0, materials.source, true);
block(0.5, 0.94, 1.18, -3.15, -0.04, 0, materials.source, true);
block(0.5, 0.94, 1.18, -2.65, -0.04, 0, materials.source, true);
for (const x of [-3.65, -3.15, -2.65]) {
block(0.39, 0.12, 0.74, x, 0.48, -0.03, materials.sourceDark);
}
/* Exposed source terminals */
block(0.27, 0.23, 0.24, -3.65, 0.05, 0.78, materials.positive, true);
block(0.27, 0.23, 0.24, -2.65, 0.05, 0.78, materials.negative, true);
/* Pixel-style plus and minus symbols */
block(0.035, 0.14, 0.035, -3.65, 0.05, 0.915, materials.white);
block(0.14, 0.035, 0.035, -3.65, 0.05, 0.915, materials.white);
block(0.14, 0.035, 0.035, -2.65, 0.05, 0.915, materials.white);
/* Non-conductive switch cover */
block(1.62, 0.18, 0.88, -0.8, -0.34, 0.28, materials.housing, true);
block(1.62, 0.94, 0.14, -0.8, 0.08, 0.03, materials.housing, true);
block(0.14, 0.94, 0.72, -1.57, 0.08, 0.34, materials.housing, true);
block(0.14, 0.94, 0.72, -0.03, 0.08, 0.34, materials.housing, true);
block(0.34, 0.14, 0.68, -1.43, 0.58, 0.33, materials.shell);
block(0.34, 0.14, 0.68, -0.17, 0.58, 0.33, materials.shell);
/* Switch fixed contacts */
block(0.42, 0.12, 0.4, -1.35, -0.02, 0.8, materials.shellDark, true);
block(0.42, 0.12, 0.4, -0.25, -0.02, 0.8, materials.shellDark, true);
block(0.25, 0.18, 0.28, -1.35, 0.1, 0.82, materials.copper, true);
block(0.25, 0.18, 0.28, -0.25, 0.1, 0.82, materials.copper, true);
/* The copper arm is the mechanical switch position */
const leverGroup = new THREE.Group();
leverGroup.position.set(-1.35, 0.18, 0.86);
scene.add(leverGroup);
const leverBar = block(
1.12, 0.1, 0.24, 0.56, 0, 0, materials.copper, true, leverGroup
);
leverBar.userData.isSwitch = true;
block(0.2, 0.2, 0.29, 0.03, 0, 0, materials.sourceDark, true, leverGroup);
block(0.12, 0.12, 0.29, 1.03, 0.08, 0, materials.white, true, leverGroup);
const leverTip = new THREE.Object3D();
leverTip.position.set(1.03, 0.08, 0);
leverGroup.add(leverTip);
/* Lamp optical frame and exposed emitter */
frameAtX(2.08, 0.98, 0.15, 0.18, materials.reflector);
frameAtX(2.28, 0.84, 0.14, 0.18, materials.reflector);
frameAtX(2.48, 0.71, 0.13, 0.18, materials.reflector);
block(0.64, 0.64, 0.64, 2.91, 0, 0, materials.led, true);
block(0.09, 0.86, 0.86, 3.28, 0, 0, materials.lens);
/* Lamp terminal leads and pads */
block(0.09, 0.09, 0.45, 2.65, 0.05, 0.54, materials.copper);
block(0.09, 0.09, 0.45, 3.15, 0.05, 0.54, materials.copper);
block(0.24, 0.2, 0.26, 2.65, 0.05, 0.76, materials.positive, true);
block(0.24, 0.2, 0.26, 3.15, 0.05, 0.76, materials.negative, true);
block(0.035, 0.12, 0.035, 2.65, 0.05, 0.905, materials.white);
block(0.12, 0.035, 0.035, 2.65, 0.05, 0.905, materials.white);
block(0.12, 0.035, 0.035, 3.15, 0.05, 0.905, materials.white);
/* Block-built conductor paths */
function sampledPolyline(points, spacing = 0.135) {
const positions = [];
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const distance = a.distanceTo(b);
const steps = Math.max(1, Math.ceil(distance / spacing));
for (let step = 0; step < steps; step++) {
positions.push(a.clone().lerp(b, step / steps));
}
}
positions.push(points[points.length - 1].clone());
return positions;
}
function voxelWire(points) {
const positions = sampledPolyline(points);
const geometry = new THREE.BoxGeometry(0.145, 0.145, 0.145);
const mesh = new THREE.InstancedMesh(geometry, materials.wire, positions.length);
const matrix = new THREE.Matrix4();
positions.forEach((position, i) => {
matrix.makeTranslation(position.x, position.y, position.z);
mesh.setMatrixAt(i, matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.computeBoundingSphere();
scene.add(mesh);
}
voxelWire([
new THREE.Vector3(-3.65, 0.05, 0.96),
new THREE.Vector3(-3.65, 1.02, 0.96),
new THREE.Vector3(-1.35, 1.02, 0.96),
new THREE.Vector3(-1.35, 0.19, 0.96)
]);
voxelWire([
new THREE.Vector3(-0.25, 0.19, 0.96),
new THREE.Vector3(-0.25, 1.34, 0.96),
new THREE.Vector3(2.65, 1.34, 0.96),
new THREE.Vector3(2.65, 0.05, 0.90)
]);
voxelWire([
new THREE.Vector3(3.15, 0.05, 0.90),
new THREE.Vector3(3.15, -0.84, 0.96),
new THREE.Vector3(-2.65, -0.84, 0.96),
new THREE.Vector3(-2.65, 0.05, 0.96)
]);
/* Full closed-loop route, including the source and lamp interiors */
const routePoints = [
new THREE.Vector3(-3.65, 0.05, 0.96),
new THREE.Vector3(-3.65, 1.02, 0.96),
new THREE.Vector3(-1.35, 1.02, 0.96),
new THREE.Vector3(-1.35, 0.19, 0.96),
new THREE.Vector3(-0.25, 0.19, 0.96),
new THREE.Vector3(-0.25, 1.34, 0.96),
new THREE.Vector3(2.65, 1.34, 0.96),
new THREE.Vector3(2.65, 0.05, 0.90),
new THREE.Vector3(2.65, 0, 0.5),
new THREE.Vector3(3.15, 0, 0.5),
new THREE.Vector3(3.15, 0.05, 0.90),
new THREE.Vector3(3.15, -0.84, 0.96),
new THREE.Vector3(-2.65, -0.84, 0.96),
new THREE.Vector3(-2.65, 0.05, 0.96),
new THREE.Vector3(-3.15, -0.05, 0.35),
new THREE.Vector3(-3.65, 0.05, 0.96)
];
const routeSegments = [];
let routeLength = 0;
for (let i = 0; i < routePoints.length - 1; i++) {
const length = routePoints[i].distanceTo(routePoints[i + 1]);
routeSegments.push({
start: routeLength,
length,
a: routePoints[i],
b: routePoints[i + 1]
});
routeLength += length;
}
function pointOnRoute(distance) {
const d = ((distance % routeLength) + routeLength) % routeLength;
for (const segment of routeSegments) {
if (d <= segment.start + segment.length) {
const t = (d - segment.start) / segment.length;
return segment.a.clone().lerp(segment.b, t);
}
}
return routePoints[routePoints.length - 1].clone();
}
const currentCubes = [];
const currentGeometry = new THREE.BoxGeometry(0.105, 0.105, 0.105);
for (let i = 0; i < 18; i++) {
const cube = new THREE.Mesh(currentGeometry, materials.current);
scene.add(cube);
currentCubes.push(cube);
}
/* Switch state and UI */
let switchClosed = true;
let leverTarget = 0;
const switchButton = document.getElementById("switch-button");
const statusPanel = document.getElementById("status-panel");
const switchState = document.getElementById("switch-state");
const circuitState = document.getElementById("circuit-state");
const mechanicalCallout = document.getElementById("ann-mechanical");
function setSwitch(closed) {
switchClosed = closed;
leverTarget = closed ? 0 : THREE.MathUtils.degToRad(27);
statusPanel.dataset.state = closed ? "closed" : "open";
switchState.textContent = closed ? "CLOSED" : "OPEN";
circuitState.textContent = closed ? "COMPLETE LOOP" : "LOOP BROKEN";
mechanicalCallout.textContent = closed
? "Mechanical contact · closed"
: "Mechanical contact · open";
switchButton.textContent = closed
? "OPEN MECHANICAL SWITCH"
: "CLOSE MECHANICAL SWITCH";
}
switchButton.addEventListener("click", () => setSwitch(!switchClosed));
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
setSwitch(!switchClosed);
}
});
/* Screen-space callouts */
const annotationDefinitions = [
{
element: document.getElementById("ann-source"),
anchor: new THREE.Vector3(-3.15, 0.58, 0),
offset: new THREE.Vector2(-100, -78)
},
{
element: document.getElementById("ann-shell"),
anchor: new THREE.Vector3(0.15, 0.83, -0.55),
offset: new THREE.Vector2(95, -65)
},
{
element: document.getElementById("ann-housing"),
anchor: new THREE.Vector3(-0.8, 0.48, 0.25),
offset: new THREE.Vector2(-120, 75)
},
{
element: mechanicalCallout,
anchor: new THREE.Vector3(),
offset: new THREE.Vector2(115, -45)
},
{
element: document.getElementById("ann-lamp"),
anchor: new THREE.Vector3(2.92, 0.43, 0.25),
offset: new THREE.Vector2(100, -70)
},
{
element: document.getElementById("ann-loop"),
anchor: new THREE.Vector3(0.68, 1.34, 0.96),
offset: new THREE.Vector2(0, -58)
}
];
const leaderGroup = document.getElementById("leaders");
const svgNamespace = "http://www.w3.org/2000/svg";
annotationDefinitions.forEach((definition) => {
const line = document.createElementNS(svgNamespace, "polyline");
line.setAttribute("fill", "none");
line.setAttribute("stroke", "rgba(178, 207, 232, .58)");
line.setAttribute("stroke-width", "1");
leaderGroup.appendChild(line);
definition.line = line;
});
function updateAnnotations() {
const width = innerWidth;
const height = innerHeight;
leverTip.getWorldPosition(annotationDefinitions[3].anchor);
annotationDefinitions.forEach((definition) => {
const projected = definition.anchor.clone().project(camera);
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const labelX = THREE.MathUtils.clamp(
anchorX + definition.offset.x,
85,
width - 85
);
const labelY = THREE.MathUtils.clamp(
anchorY + definition.offset.y,
45,
height - 45
);
const elbowX = anchorX + definition.offset.x * 0.35;
definition.element.style.left = `${labelX}px`;
definition.element.style.top = `${labelY}px`;
definition.line.setAttribute(
"points",
`${anchorX},${anchorY} ${elbowX},${labelY} ${labelX},${labelY}`
);
});
}
/* Direct camera controls: orbit and zoom, without addons */
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let dragging = false;
let dragDistance = 0;
let previousX = 0;
let previousY = 0;
function pointerHitsLever(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObject(leverBar, false).length > 0;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragDistance = 0;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (dragging) {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
dragDistance += Math.abs(dx) + Math.abs(dy);
theta -= dx * 0.006;
phi = THREE.MathUtils.clamp(phi - dy * 0.005, 0.42, 1.48);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
} else {
renderer.domElement.style.cursor =
pointerHitsLever(event) ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (dragDistance < 6 && pointerHitsLever(event)) {
setSwitch(!switchClosed);
}
dragging = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radius = THREE.MathUtils.clamp(
radius + event.deltaY * 0.008,
7.5,
23
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
/* Animation */
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const damping = 1 - Math.exp(-delta * 13);
leverGroup.rotation.z = THREE.MathUtils.lerp(
leverGroup.rotation.z,
leverTarget,
damping
);
const flowing = switchClosed && Math.abs(leverGroup.rotation.z) < 0.025;
currentCubes.forEach((cube) => {
cube.visible = flowing;
if (flowing) {
const distance =
elapsed * 2.35 +
(cube.id * routeLength) / currentCubes.length;
cube.position.copy(pointOnRoute(distance));
}
});
const lightTarget = switchClosed ? 28 : 0;
lampLight.intensity = THREE.MathUtils.lerp(
lampLight.intensity,
lightTarget,
damping
);
materials.led.emissiveIntensity = THREE.MathUtils.lerp(
materials.led.emissiveIntensity,
switchClosed ? 2.8 : 0.22,
damping
);
materials.lens.opacity = THREE.MathUtils.lerp(
materials.lens.opacity,
switchClosed ? 0.24 : 0.12,
damping
);
scene.updateMatrixWorld();
updateAnnotations();
renderer.render(scene, camera);
}
setSwitch(true);
animate();
</script>
</body>
</html>
```