Prompt specification
Build or customize the Three.js 3D scene "Open and closed circuit model board" 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: Open and closed circuit model board
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/open-and-closed-circuit-model-board-x1613
Scene Description Prompt:
Toggle a low-poly schematic circuit’s switch; show current only through the closed source–resistor loop. Use Three.js from cdnjs in one HTML file.
Working Three.js Starter (open-and-closed-circuit-model-board-x1613):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Low-Poly Circuit Switch</title>
<style>
:root {
color-scheme: dark;
--bg: #07131d;
--panel: rgba(9, 24, 35, 0.88);
--border: rgba(146, 210, 224, 0.18);
--text: #e8f7fa;
--muted: #8eabb5;
--cyan: #58e8ff;
--green: #72f3b3;
--amber: #ffbd62;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app, canvas {
width: 100%;
height: 100%;
display: block;
}
canvas {
touch-action: none;
cursor: grab;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
.intro {
position: absolute;
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 16px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.intro p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
position: absolute;
top: 22px;
right: 22px;
display: flex;
align-items: stretch;
gap: 10px;
pointer-events: auto;
}
.state-card {
min-width: 128px;
padding: 12px 15px;
border-radius: 14px;
}
.state-label {
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.state-value {
display: flex;
align-items: center;
gap: 8px;
margin-top: 7px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 13px rgba(255, 189, 98, 0.7);
}
.closed .dot {
background: var(--green);
box-shadow: 0 0 13px rgba(114, 243, 179, 0.8);
}
button {
border: 0;
font: inherit;
cursor: pointer;
}
#toggle {
min-width: 145px;
padding: 0 18px;
border: 1px solid rgba(255, 189, 98, 0.28);
border-radius: 14px;
color: #211506;
background: var(--amber);
font-weight: 850;
letter-spacing: 0.015em;
transition: transform 160ms ease, background 160ms ease, border-color 160ms ease;
}
#toggle[data-state="closed"] {
color: #052116;
background: var(--green);
border-color: rgba(114, 243, 179, 0.42);
}
#toggle:hover { transform: translateY(-2px); }
#toggle:active { transform: translateY(0) scale(0.98); }
.readout {
position: absolute;
left: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 13px;
padding: 13px 16px;
border-radius: 14px;
}
.readout-icon {
position: relative;
width: 28px;
height: 28px;
border: 2px solid var(--cyan);
border-radius: 50%;
box-shadow: inset 0 0 0 5px rgba(88, 232, 255, 0.08);
}
.readout-icon::after {
content: "";
position: absolute;
top: 5px;
right: -4px;
width: 7px;
height: 7px;
border-top: 2px solid var(--cyan);
border-right: 2px solid var(--cyan);
transform: rotate(45deg);
}
.readout-title {
color: var(--muted);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#currentValue {
margin-top: 3px;
color: var(--cyan);
font-size: 19px;
font-weight: 850;
}
.help {
position: absolute;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 9px;
padding: 11px 14px;
border-radius: 12px;
color: var(--muted);
font-size: 11px;
}
.help button {
padding: 6px 9px;
border: 1px solid var(--border);
border-radius: 7px;
color: var(--text);
background: rgba(255, 255, 255, 0.04);
font-size: 11px;
pointer-events: auto;
}
kbd {
padding: 2px 6px;
border: 1px solid var(--border);
border-radius: 5px;
color: var(--text);
background: rgba(255, 255, 255, 0.05);
font: 10px ui-monospace, monospace;
}
@media (max-width: 720px) {
.intro {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
}
.intro p { display: none; }
.controls {
top: 116px;
right: 14px;
}
.state-card { min-width: 108px; }
#toggle { min-width: 128px; }
.readout {
left: 14px;
bottom: 14px;
}
.help {
right: 14px;
bottom: 14px;
}
.help span { display: none; }
}
</style>
</head>
<body>
<div id="app"></div>
<div class="hud">
<section class="intro panel">
<div class="eyebrow">Interactive schematic</div>
<h1>Source → Resistor</h1>
<p>Close the switch to complete the loop. Cyan current is displayed only in the closed circuit.</p>
</section>
<div class="controls">
<div class="state-card panel" id="stateCard">
<div class="state-label">Circuit</div>
<div class="state-value"><span class="dot"></span><span id="stateValue">Open</span></div>
</div>
<button id="toggle" data-state="open" aria-pressed="false">Close switch</button>
</div>
<div class="readout panel">
<div class="readout-icon"></div>
<div>
<div class="readout-title">Current</div>
<div id="currentValue">0.0 A</div>
</div>
</div>
<div class="help panel">
<span><kbd>Drag</kbd> orbit · <kbd>Wheel</kbd> zoom · <kbd>Space</kbd> switch</span>
<button id="resetView" type="button">Reset view</button>
</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 container = document.getElementById("app");
const toggleButton = document.getElementById("toggle");
const stateCard = document.getElementById("stateCard");
const stateValue = document.getElementById("stateValue");
const currentValue = document.getElementById("currentValue");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131d);
scene.fog = new THREE.Fog(0x07131d, 19, 38);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xa9e9ff, 0x17242c, 2.0);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.0);
key.position.set(-4, 9, 7);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 1;
key.shadow.camera.far = 25;
scene.add(key);
const rim = new THREE.DirectionalLight(0x52dfff, 1.5);
rim.position.set(7, 4, -6);
scene.add(rim);
const materials = {
board: new THREE.MeshStandardMaterial({
color: 0x12323a,
roughness: 0.78,
metalness: 0.12,
flatShading: true
}),
border: new THREE.MeshStandardMaterial({
color: 0x10202b,
roughness: 0.72,
metalness: 0.2,
flatShading: true
}),
floor: new THREE.MeshStandardMaterial({
color: 0x08131c,
roughness: 1,
metalness: 0
}),
wire: new THREE.MeshStandardMaterial({
color: 0xd58a43,
roughness: 0.34,
metalness: 0.72,
flatShading: true
}),
metal: new THREE.MeshStandardMaterial({
color: 0xb9c8c8,
roughness: 0.27,
metalness: 0.85,
flatShading: true
}),
source: new THREE.MeshStandardMaterial({
color: 0xf0a145,
roughness: 0.48,
metalness: 0.18,
flatShading: true
}),
sourceDark: new THREE.MeshStandardMaterial({
color: 0x9e4f27,
roughness: 0.55,
metalness: 0.16,
flatShading: true
}),
resistor: new THREE.MeshStandardMaterial({
color: 0xe9d7ae,
roughness: 0.6,
metalness: 0.08,
flatShading: true
}),
blackBand: new THREE.MeshStandardMaterial({
color: 0x25282a,
roughness: 0.58,
metalness: 0.05
}),
redBand: new THREE.MeshStandardMaterial({
color: 0xd55a47,
roughness: 0.55,
metalness: 0.05
}),
blade: new THREE.MeshStandardMaterial({
color: 0xffbd62,
roughness: 0.34,
metalness: 0.52,
flatShading: true
}),
handle: new THREE.MeshStandardMaterial({
color: 0xff7048,
roughness: 0.4,
metalness: 0.22,
flatShading: true
})
};
function addMesh(geometry, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(a, b, radius, material, radialSegments = 8, parent = scene) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = addMesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material,
0, 0, 0,
parent
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
// Floor and board.
const floor = addMesh(
new THREE.PlaneGeometry(60, 60),
materials.floor,
0, -0.42, 0
);
floor.rotation.x = -Math.PI / 2;
floor.castShadow = false;
addMesh(
new THREE.BoxGeometry(8.8, 0.22, 7.4),
materials.border,
0, -0.23, 0
);
addMesh(
new THREE.BoxGeometry(8.55, 0.16, 7.15),
materials.board,
0, -0.08, 0
);
// Subtle PCB grid.
const gridPositions = [];
for (let x = -4; x <= 4.01; x += 0.5) {
gridPositions.push(x, 0.006, -3.3, x, 0.006, 3.3);
}
for (let z = -3; z <= 3.01; z += 0.5) {
gridPositions.push(-4, 0.006, z, 4, 0.006, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
scene.add(new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x75c9d4,
transparent: true,
opacity: 0.09
})
));
// Board mounting screws.
const screwMaterial = new THREE.MeshStandardMaterial({
color: 0x82969c,
roughness: 0.3,
metalness: 0.8,
flatShading: true
});
for (const x of [-3.9, 3.9]) {
for (const z of [-3.15, 3.15]) {
addMesh(
new THREE.CylinderGeometry(0.09, 0.09, 0.045, 8),
screwMaterial,
x, 0.035, z
);
}
}
// Circuit wire sections. The resistor and switch gaps are intentionally omitted.
const wireY = 0.13;
const wirePoints = {
sourcePlus: new THREE.Vector3(-3, wireY, -1.65),
topLeft: new THREE.Vector3(-3, wireY, -2.5),
resistorLeft: new THREE.Vector3(0, wireY, -2.5),
resistorRight: new THREE.Vector3(2.15, wireY, -2.5),
topRight: new THREE.Vector3(3, wireY, -2.5),
bottomRight: new THREE.Vector3(3, wireY, 2.5),
switchRight: new THREE.Vector3(-0.55, wireY, 2.5),
switchLeft: new THREE.Vector3(-2.05, wireY, 2.5),
bottomLeft: new THREE.Vector3(-3, wireY, 2.5),
sourceMinus: new THREE.Vector3(-3, wireY, 1.65)
};
const wireSections = [
[wirePoints.sourcePlus, wirePoints.topLeft],
[wirePoints.topLeft, wirePoints.resistorLeft],
[wirePoints.resistorRight, wirePoints.topRight],
[wirePoints.topRight, wirePoints.bottomRight],
[wirePoints.bottomRight, wirePoints.switchRight],
[wirePoints.switchLeft, wirePoints.bottomLeft],
[wirePoints.bottomLeft, wirePoints.sourceMinus]
];
for (const [a, b] of wireSections) {
cylinderBetween(a, b, 0.052, materials.wire, 8);
}
// Low-poly junction pads.
const padMaterial = new THREE.MeshStandardMaterial({
color: 0xe5a052,
roughness: 0.32,
metalness: 0.75,
flatShading: true
});
for (const point of [
wirePoints.topLeft,
wirePoints.topRight,
wirePoints.bottomRight,
wirePoints.bottomLeft
]) {
addMesh(
new THREE.CylinderGeometry(0.12, 0.12, 0.035, 10),
padMaterial,
point.x, 0.145, point.z
);
}
// Source / battery.
addMesh(
new THREE.BoxGeometry(0.98, 0.62, 3.15),
materials.source,
-3, 0.31, 0
);
addMesh(
new THREE.BoxGeometry(1.02, 0.08, 3.2),
materials.sourceDark,
-3, 0.03, 0
);
for (const z of [-1.64, 1.64]) {
const terminal = addMesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.26, 10),
materials.metal,
-3, wireY, z
);
terminal.rotation.x = Math.PI / 2;
}
// Raised + and − markings.
const signMaterial = new THREE.MeshStandardMaterial({
color: 0xfff3cf,
roughness: 0.7,
flatShading: true
});
addMesh(new THREE.BoxGeometry(0.42, 0.025, 0.10), signMaterial, -3, 0.635, -0.7);
addMesh(new THREE.BoxGeometry(0.10, 0.025, 0.42), signMaterial, -3, 0.635, -0.7);
addMesh(new THREE.BoxGeometry(0.42, 0.025, 0.10), signMaterial, -3, 0.635, 0.7);
// Resistor.
const resistorBody = addMesh(
new THREE.CylinderGeometry(0.25, 0.25, 1.65, 10),
materials.resistor,
1.075, 0.31, -2.5
);
resistorBody.rotation.z = Math.PI / 2;
cylinderBetween(
new THREE.Vector3(0, wireY, -2.5),
new THREE.Vector3(0.3, 0.29, -2.5),
0.048,
materials.metal
);
cylinderBetween(
new THREE.Vector3(1.85, 0.29, -2.5),
new THREE.Vector3(2.15, wireY, -2.5),
0.048,
materials.metal
);
const bandPositions = [0.5, 0.92, 1.34, 1.7];
bandPositions.forEach((x, index) => {
const band = addMesh(
new THREE.CylinderGeometry(0.258, 0.258, 0.13, 10),
index === 1 ? materials.blackBand : materials.redBand,
x, 0.31, -2.5
);
band.rotation.z = Math.PI / 2;
});
// Switch contacts and hinged blade.
const switchPivot = new THREE.Group();
switchPivot.position.set(-2.05, 0.24, 2.5);
switchPivot.rotation.z = 0.95;
scene.add(switchPivot);
addMesh(
new THREE.CylinderGeometry(0.19, 0.19, 0.13, 10),
materials.metal,
-2.05, 0.065, 2.5
);
const contactMaterial = new THREE.MeshStandardMaterial({
color: 0x60717a,
emissive: 0x51ffa9,
emissiveIntensity: 0.05,
roughness: 0.28,
metalness: 0.82,
flatShading: true
});
addMesh(
new THREE.CylinderGeometry(0.17, 0.17, 0.12, 10),
materials.metal,
-0.55, 0.06, 2.5
);
addMesh(
new THREE.CylinderGeometry(0.115, 0.115, 0.08, 10),
contactMaterial,
-0.55, 0.155, 2.5
);
addMesh(
new THREE.BoxGeometry(1.48, 0.10, 0.15),
materials.blade,
0.74, 0, 0,
switchPivot
);
addMesh(
new THREE.SphereGeometry(0.17, 8, 5),
materials.handle,
1.45, 0, 0,
switchPivot
);
// Invisible enlarged hit target for the switch.
const switchHit = addMesh(
new THREE.BoxGeometry(1.9, 0.55, 0.5),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
}),
-1.28, 0.3, 2.5
);
// Canvas labels placed on the board.
function addBoardLabel(text, x, z, width, color = "#91b7c1") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = color;
context.font = "700 48px ui-monospace, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 64);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const label = addMesh(
new THREE.PlaneGeometry(width, width * 0.25),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false
}),
x, 0.018, z
);
label.rotation.x = -Math.PI / 2;
label.castShadow = false;
label.receiveShadow = false;
}
addBoardLabel("9V SOURCE", -3, 2.98, 1.75, "#ffd49b");
addBoardLabel("R 1.8 Ω", 1.05, -3.03, 1.8, "#d9c8a1");
addBoardLabel("SWITCH", -1.3, 3.03, 1.55, "#ffc678");
// Current route follows the actual closed source-resistor loop.
const routePoints = [
new THREE.Vector3(-3, 0.24, -1.65),
new THREE.Vector3(-3, 0.24, -2.5),
new THREE.Vector3(0, 0.24, -2.5),
new THREE.Vector3(0.25, 0.59, -2.5),
new THREE.Vector3(1.9, 0.59, -2.5),
new THREE.Vector3(2.15, 0.24, -2.5),
new THREE.Vector3(3, 0.24, -2.5),
new THREE.Vector3(3, 0.24, 2.5),
new THREE.Vector3(-0.55, 0.29, 2.5),
new THREE.Vector3(-2.05, 0.29, 2.5),
new THREE.Vector3(-3, 0.24, 2.5),
new THREE.Vector3(-3, 0.24, 1.65),
new THREE.Vector3(-3, 0.69, 1.0),
new THREE.Vector3(-3, 0.69, -1.0),
new THREE.Vector3(-3, 0.24, -1.65)
];
const routeSegments = [];
let routeLength = 0;
for (let i = 0; i < routePoints.length - 1; i++) {
const a = routePoints[i];
const b = routePoints[i + 1];
const length = a.distanceTo(b);
routeSegments.push({ a, b, length, start: routeLength });
routeLength += length;
}
function sampleRoute(u) {
const distance = ((u % 1) + 1) % 1 * routeLength;
let segment = routeSegments[routeSegments.length - 1];
for (const candidate of routeSegments) {
if (distance <= candidate.start + candidate.length) {
segment = candidate;
break;
}
}
const local = (distance - segment.start) / segment.length;
return {
position: segment.a.clone().lerp(segment.b, local),
tangent: segment.b.clone().sub(segment.a).normalize()
};
}
const currentGroup = new THREE.Group();
currentGroup.visible = false;
scene.add(currentGroup);
const currentLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(routePoints),
new THREE.LineBasicMaterial({
color: 0x58e8ff,
transparent: true,
opacity: 0.46,
depthTest: false,
depthWrite: false
})
);
currentLine.renderOrder = 20;
currentGroup.add(currentLine);
const arrowGeometry = new THREE.ConeGeometry(0.09, 0.25, 5);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0x8cf5ff,
transparent: true,
opacity: 0.95,
depthTest: false,
depthWrite: false
});
const glowGeometry = new THREE.ConeGeometry(0.15, 0.31, 5);
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0x32dfff,
transparent: true,
opacity: 0.16,
depthTest: false,
depthWrite: false,
side: THREE.DoubleSide
});
const currentArrows = [];
const arrowCount = 15;
for (let i = 0; i < arrowCount; i++) {
const arrow = new THREE.Group();
const core = new THREE.Mesh(arrowGeometry, arrowMaterial);
const glow = new THREE.Mesh(glowGeometry, glowMaterial);
core.renderOrder = 22;
glow.renderOrder = 21;
arrow.add(glow, core);
currentGroup.add(arrow);
currentArrows.push(arrow);
}
// Custom camera controls implemented directly with pointer events.
const target = new THREE.Vector3(0, 0.05, 0);
const cameraState = {
radius: 13.2,
theta: 0,
phi: 0.73
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
target.y + cameraState.radius * Math.cos(cameraState.phi),
target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.radius = 13.2;
cameraState.theta = 0;
cameraState.phi = 0.73;
target.set(0, 0.05, 0);
updateCamera();
}
updateCamera();
let switchClosed = false;
function setSwitch(closed) {
switchClosed = closed;
currentGroup.visible = closed;
toggleButton.dataset.state = closed ? "closed" : "open";
toggleButton.setAttribute("aria-pressed", String(closed));
toggleButton.textContent = closed ? "Open switch" : "Close switch";
stateCard.classList.toggle("closed", closed);
stateValue.textContent = closed ? "Closed" : "Open";
currentValue.textContent = closed ? "1.8 A" : "0.0 A";
materials.blade.color.set(closed ? 0x78e9ad : 0xffbd62);
contactMaterial.color.set(closed ? 0x69f4b3 : 0x60717a);
contactMaterial.emissiveIntensity = closed ? 1.35 : 0.05;
}
function toggleSwitch() {
setSwitch(!switchClosed);
}
toggleButton.addEventListener("click", toggleSwitch);
document.getElementById("resetView").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target.tagName !== "BUTTON") {
event.preventDefault();
toggleSwitch();
}
if (event.key.toLowerCase() === "r") resetCamera();
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = renderer.domElement;
function updatePointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pointerHitsSwitch(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObject(switchHit, false).length > 0;
}
let dragMode = null;
let pointerMoved = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
dragMode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
pointerMoved = false;
previousX = event.clientX;
previousY = event.clientY;
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragMode) {
canvas.style.cursor = pointerHitsSwitch(event) ? "pointer" : "grab";
return;
}
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
if (Math.abs(dx) + Math.abs(dy) > 2) pointerMoved = true;
if (dragMode === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.32,
1.38
);
} else {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const panScale = cameraState.radius * 0.0013;
target.addScaledVector(right, -dx * panScale);
target.addScaledVector(up, dy * panScale);
}
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerMoved && pointerHitsSwitch(event)) {
toggleSwitch();
}
dragMode = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointercancel", () => {
dragMode = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
8,
22
);
updateCamera();
}, { passive: false });
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
const up = new THREE.Vector3(0, 1, 0);
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const targetAngle = switchClosed ? 0 : 0.95;
switchPivot.rotation.z +=
(targetAngle - switchPivot.rotation.z) * (1 - Math.exp(-delta * 12));
if (switchClosed) {
currentArrows.forEach((arrow, index) => {
const u = (index / currentArrows.length + elapsed * 0.13) % 1;
const sample = sampleRoute(u);
arrow.position.copy(sample.position);
arrow.quaternion.setFromUnitVectors(up, sample.tangent);
const pulse = 1 + Math.sin(elapsed * 7 + index) * 0.08;
arrow.scale.setScalar(pulse);
});
}
renderer.render(scene, camera);
}
setSwitch(false);
animate();
</script>
</body>
</html>
```