Prompt specification
Build or customize the Three.js 3D scene "Switchboard connection puzzle with busy lines" 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: Switchboard connection puzzle with busy lines
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/switchboard-connection-puzzle-with-busy-lines-x2548
Scene Description Prompt:
A voxel telephone-switchboard puzzle connecting fictional calls through limited sockets, with busy lines and complete connections visibly marked. Three.js from cdnjs, one HTML file.
Working Three.js Starter (switchboard-connection-puzzle-with-busy-lines-x2548):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Exchange — Telephone Switchboard Puzzle</title>
<style>
:root {
--ink: #061015;
--panel: rgba(9, 24, 30, 0.9);
--panel-light: rgba(16, 39, 46, 0.94);
--line: rgba(159, 214, 205, 0.2);
--text: #e8f1e9;
--muted: #91aaa4;
--amber: #ffb454;
--green: #52e0a5;
--red: #ff6f61;
--cream: #efe4c7;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--ink);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
background:
radial-gradient(circle at 50% 40%, rgba(30, 83, 83, 0.2), transparent 52%),
#061015;
}
button {
font: inherit;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#labels {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
z-index: 3;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
pointer-events: auto;
color: var(--text);
border: 1px solid var(--line);
background: rgba(7, 22, 27, 0.9);
box-shadow:
0 8px 20px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(10px);
transition:
border-color 160ms ease,
background 160ms ease,
box-shadow 160ms ease,
opacity 160ms ease;
}
.world-label:hover {
border-color: rgba(239, 228, 199, 0.65);
background: rgba(14, 36, 42, 0.97);
z-index: 20;
}
.world-label:focus-visible {
outline: 2px solid var(--cream);
outline-offset: 3px;
}
.source-label,
.target-label {
width: 132px;
min-height: 70px;
padding: 8px 9px 7px;
border-radius: 5px;
text-align: left;
cursor: pointer;
}
.source-label {
border-left: 3px solid var(--call-color, var(--amber));
}
.target-label {
border-right: 3px solid #577078;
}
.label-kicker {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.world-label strong {
display: block;
margin-top: 3px;
overflow: hidden;
color: var(--text);
font-size: 13px;
font-weight: 800;
line-height: 1.1;
text-overflow: ellipsis;
white-space: nowrap;
}
.source-label .request {
display: block;
margin-top: 3px;
color: #d9c59e;
font-size: 10px;
font-weight: 700;
}
.target-label small {
display: block;
margin-top: 2px;
overflow: hidden;
color: var(--muted);
font-size: 9px;
text-overflow: ellipsis;
white-space: nowrap;
}
.status {
display: inline-flex;
align-items: center;
gap: 4px;
margin-top: 5px;
color: var(--muted);
font-size: 7px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status::before {
width: 5px;
height: 5px;
border-radius: 50%;
background: #587078;
content: "";
}
.source-label.selected {
border-color: var(--amber);
background: rgba(54, 39, 20, 0.95);
box-shadow:
0 0 0 1px rgba(255, 180, 84, 0.18),
0 8px 26px rgba(0, 0, 0, 0.4);
}
.source-label.selected .status,
.source-label.busy .status {
color: var(--amber);
}
.source-label.selected .status::before,
.source-label.busy .status::before {
background: var(--amber);
box-shadow: 0 0 8px var(--amber);
animation: pulse 1s ease-in-out infinite;
}
.source-label.complete,
.target-label.complete {
border-color: rgba(82, 224, 165, 0.8);
background: rgba(8, 39, 32, 0.94);
}
.source-label.complete .status,
.target-label.complete .status {
color: var(--green);
}
.source-label.complete .status::before,
.target-label.complete .status::before {
background: var(--green);
box-shadow: 0 0 8px var(--green);
}
.source-label.complete strong::after {
margin-left: 4px;
color: var(--green);
content: "✓";
}
.target-label.fault {
border-color: var(--red);
background: rgba(67, 24, 23, 0.95);
animation: shake 240ms ease;
}
.relay-label {
width: 82px;
padding: 7px 7px 6px;
border-radius: 4px;
text-align: center;
cursor: pointer;
}
.relay-label strong {
display: block;
color: var(--cream);
font-size: 10px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.relay-label span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
}
.relay-label.occupied {
border-color: rgba(255, 180, 84, 0.7);
}
.relay-label.occupied span {
color: var(--amber);
}
.relay-label.full {
border-color: rgba(255, 180, 84, 0.95);
background: rgba(56, 38, 19, 0.95);
}
.topbar {
position: absolute;
z-index: 8;
top: 20px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.brand {
min-width: 245px;
}
.eyebrow {
color: var(--amber);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 4px 0 5px;
color: var(--cream);
font-size: clamp(20px, 2.5vw, 31px);
font-weight: 900;
letter-spacing: 0.09em;
line-height: 1;
text-transform: uppercase;
}
.subtitle {
color: var(--muted);
font-size: 11px;
font-weight: 600;
}
.top-actions {
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
.counter {
min-width: 116px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 5px;
background: var(--panel);
text-align: center;
backdrop-filter: blur(10px);
}
.counter strong {
color: var(--green);
font-size: 16px;
}
.counter span {
margin-left: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.icon-button {
height: 39px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 5px;
color: var(--text);
background: var(--panel);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(10px);
}
.icon-button:hover:not(:disabled) {
border-color: var(--cream);
background: var(--panel-light);
}
.icon-button:disabled {
opacity: 0.35;
cursor: default;
}
.guide {
position: absolute;
z-index: 8;
left: 24px;
bottom: 22px;
width: 278px;
padding: 14px 15px 13px;
border: 1px solid var(--line);
border-radius: 7px;
background: var(--panel);
box-shadow: 0 15px 45px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(14px);
}
.guide h2 {
margin: 0 0 9px;
color: var(--cream);
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.steps {
display: grid;
gap: 7px;
margin: 0;
padding: 0;
list-style: none;
}
.steps li {
display: grid;
grid-template-columns: 21px 1fr;
align-items: center;
gap: 8px;
color: #c1d0cb;
font-size: 10px;
line-height: 1.35;
}
.step-number {
display: grid;
width: 21px;
height: 21px;
place-items: center;
border: 1px solid rgba(239, 228, 199, 0.26);
border-radius: 3px;
color: var(--cream);
font-size: 9px;
font-weight: 900;
}
.selection {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.selection strong {
color: var(--text);
}
.legend {
position: absolute;
z-index: 8;
right: 24px;
bottom: 22px;
display: flex;
align-items: center;
gap: 15px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 5px;
background: var(--panel);
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #586a70;
}
.dot.busy {
background: var(--amber);
box-shadow: 0 0 8px var(--amber);
}
.dot.done {
background: var(--green);
box-shadow: 0 0 8px var(--green);
}
.dot.fault {
background: var(--red);
box-shadow: 0 0 8px var(--red);
}
.camera-hint {
position: absolute;
z-index: 7;
left: 50%;
bottom: 22px;
color: rgba(181, 204, 198, 0.5);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
transform: translateX(-50%);
white-space: nowrap;
}
#toast {
position: absolute;
z-index: 30;
top: 100px;
left: 50%;
max-width: min(440px, calc(100vw - 30px));
padding: 10px 15px;
border: 1px solid rgba(255, 180, 84, 0.5);
border-radius: 5px;
color: #ffe1ae;
background: rgba(45, 31, 16, 0.95);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -8px);
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
border-color: rgba(255, 111, 97, 0.65);
color: #ffc2bb;
background: rgba(58, 20, 18, 0.96);
}
#success {
position: absolute;
z-index: 25;
inset: 0;
display: grid;
place-items: center;
background: rgba(2, 9, 12, 0.48);
opacity: 0;
pointer-events: none;
transition: opacity 300ms ease;
backdrop-filter: blur(3px);
}
#success.show {
opacity: 1;
pointer-events: auto;
}
.success-card {
width: min(420px, calc(100vw - 32px));
padding: 28px;
border: 1px solid rgba(82, 224, 165, 0.48);
border-radius: 8px;
background: rgba(7, 28, 28, 0.96);
box-shadow: 0 25px 80px rgba(0, 0, 0, 0.58);
text-align: center;
}
.success-icon {
display: grid;
width: 54px;
height: 54px;
margin: 0 auto 15px;
place-items: center;
border: 1px solid rgba(82, 224, 165, 0.55);
border-radius: 50%;
color: var(--green);
background: rgba(82, 224, 165, 0.08);
font-size: 25px;
box-shadow: 0 0 30px rgba(82, 224, 165, 0.16);
}
.success-card h2 {
margin: 5px 0 8px;
color: var(--cream);
font-size: 25px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.success-card p {
margin: 0 0 20px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.success-card button {
height: 40px;
padding: 0 18px;
border: 1px solid var(--green);
border-radius: 4px;
color: #062018;
background: var(--green);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
cursor: pointer;
}
@keyframes pulse {
0%,
100% {
opacity: 0.45;
}
50% {
opacity: 1;
}
}
@keyframes shake {
0%,
100% {
transform: translate(-50%, -50%);
}
35% {
transform: translate(calc(-50% - 5px), -50%);
}
70% {
transform: translate(calc(-50% + 5px), -50%);
}
}
@media (max-width: 800px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.subtitle,
.camera-hint,
.legend {
display: none;
}
.brand {
min-width: 0;
}
.counter {
min-width: 80px;
padding-inline: 8px;
}
.icon-button {
padding-inline: 9px;
}
.guide {
left: 12px;
bottom: 12px;
width: 245px;
padding: 10px 11px;
}
.steps li {
font-size: 9px;
}
.source-label,
.target-label {
width: 106px;
padding: 6px 7px;
}
.world-label strong {
font-size: 11px;
}
.source-label .request,
.target-label small {
font-size: 8px;
}
}
</style>
</head>
<body>
<main id="app">
<div id="labels"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Night Shift / Exchange 06</div>
<h1>Voxel Exchange</h1>
<div class="subtitle">Six fictional calls. Five shared sockets. One clear board.</div>
</div>
<div class="top-actions">
<div class="counter">
<strong id="completeCount">0</strong>
<span>/ 6 clear</span>
</div>
<button class="icon-button" id="undoButton" type="button" disabled>Undo</button>
<button class="icon-button" id="resetButton" type="button">Reset</button>
</div>
</header>
<aside class="guide">
<h2>Patch the board</h2>
<ol class="steps">
<li>
<span class="step-number">1</span>
<span>Select a caller. Its line becomes busy.</span>
</li>
<li>
<span class="step-number">2</span>
<span>Route the call through a central socket.</span>
</li>
<li>
<span class="step-number">3</span>
<span>Select the matching outgoing line.</span>
</li>
</ol>
<div class="selection" id="selectionText">
<strong>No caller selected.</strong><br />
Socket A carries two calls; B–E carry one each.
</div>
</aside>
<div class="legend" aria-hidden="true">
<span><i class="dot busy"></i>Busy</span>
<span><i class="dot done"></i>Complete</span>
<span><i class="dot fault"></i>Fault</span>
</div>
<div class="camera-hint">Drag to orbit · Wheel to zoom · Right-drag to pan</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="success">
<div class="success-card">
<div class="success-icon">✓</div>
<div class="eyebrow">Exchange clear</div>
<h2>All lines connected</h2>
<p>
Every fictional call has reached its intended destination.
The night shift can breathe again.
</p>
<button id="playAgain" type="button">Clear the board</button>
</div>
</div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.querySelector("#app");
const labelLayer = document.querySelector("#labels");
const toastElement = document.querySelector("#toast");
const completeCountElement = document.querySelector("#completeCount");
const undoButton = document.querySelector("#undoButton");
const resetButton = document.querySelector("#resetButton");
const playAgainButton = document.querySelector("#playAgain");
const successElement = document.querySelector("#success");
const selectionText = document.querySelector("#selectionText");
const palette = {
ink: 0x071015,
board: 0x183038,
boardTop: 0x234048,
panel: 0x10282f,
cream: 0xefe4c7,
brass: 0xb69559,
dark: 0x071116,
amber: 0xffb454,
green: 0x52e0a5,
red: 0xff6f61,
muted: 0x75908d
};
const callDefinitions = [
{ id: "mara", caller: "Mara", wants: "Ada", line: "C-01", color: "#ffb454" },
{ id: "ivo", caller: "Ivo", wants: "Bo", line: "C-02", color: "#ff8f6b" },
{ id: "tilda", caller: "Tilda", wants: "Cleo", line: "C-03", color: "#ffd166" },
{ id: "ben", caller: "Ben", wants: "Dex", line: "C-04", color: "#e9a0d1" },
{ id: "nova", caller: "Nova", wants: "Ezra", line: "C-05", color: "#87d8c0" },
{ id: "sable", caller: "Sable", wants: "Finn", line: "C-06", color: "#93b9ff" }
];
const targetDefinitions = [
{ id: "ada", name: "Ada", station: "Night log" },
{ id: "bo", name: "Bo", station: "Signal lamps" },
{ id: "cleo", name: "Cleo", station: "Map room" },
{ id: "dex", name: "Dex", station: "Dream desk" },
{ id: "ezra", name: "Ezra", station: "East deck" },
{ id: "finn", name: "Finn", station: "Fox studio" }
];
const relayDefinitions = [
{ id: "A", name: "Socket A", capacity: 2 },
{ id: "B", name: "Socket B", capacity: 1 },
{ id: "C", name: "Socket C", capacity: 1 },
{ id: "D", name: "Socket D", capacity: 1 },
{ id: "E", name: "Socket E", capacity: 1 }
];
const calls = callDefinitions.map((definition) => ({
...definition,
relayId: null,
targetId: null
}));
const targets = targetDefinitions.map((definition) => ({ ...definition }));
const relays = relayDefinitions.map((definition) => ({ ...definition }));
let selectedCallId = null;
let history = [];
let toastTimer = 0;
let targetFlashTimer = 0;
let successShown = false;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x061015);
scene.fog = new THREE.FogExp2(0x061015, 0.022);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional voxel telephone switchboard"
);
app.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xbde8e0, 0x101820, 1.9);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffe5bd, 3.1);
keyLight.position.set(-7, 16, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -18;
keyLight.shadow.camera.right = 18;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -14;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4cc9b0, 1.5);
rimLight.position.set(12, 8, -12);
scene.add(rimLight);
const tealPoint = new THREE.PointLight(0x3bc8b2, 15, 18, 2);
tealPoint.position.set(0, 5, 4);
scene.add(tealPoint);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0x071317,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.61;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(50, 50, 0x285149, 0x142c2d);
grid.position.y = -0.595;
grid.material.transparent = true;
grid.material.opacity = 0.33;
scene.add(grid);
const world = new THREE.Group();
scene.add(world);
const pickMeshes = [];
const callVisuals = new Map();
const targetVisuals = new Map();
const relayVisuals = new Map();
const sourceLabels = new Map();
const targetLabels = new Map();
const relayLabels = new Map();
const cableGroups = new Map();
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.72,
metalness: options.metalness ?? 0.08,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
}
function makeBox(parent, width, height, depth, color, x, y, z, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
parent.add(mesh);
return mesh;
}
function makeLabel(className, html) {
const element = document.createElement("button");
element.type = "button";
element.className = `world-label ${className}`;
element.innerHTML = html;
labelLayer.appendChild(element);
return element;
}
function createBoard() {
makeBox(world, 25.6, 1.1, 12.7, palette.board, 0, 0, 0, {
roughness: 0.82
});
makeBox(world, 25.1, 0.16, 12.2, palette.boardTop, 0, 0.63, 0, {
roughness: 0.65
});
makeBox(world, 4.7, 0.13, 11.55, palette.panel, -8.85, 0.76, 0);
makeBox(world, 4.7, 0.13, 11.55, palette.panel, 8.85, 0.76, 0);
makeBox(world, 10.1, 0.13, 8.2, palette.panel, 0, 0.76, 0);
makeBox(world, 25.7, 0.18, 0.18, palette.brass, 0, 0.66, -6.27, {
metalness: 0.42
});
makeBox(world, 25.7, 0.18, 0.18, palette.brass, 0, 0.66, 6.27, {
metalness: 0.42
});
makeBox(world, 0.18, 0.18, 12.6, palette.brass, -12.72, 0.66, 0, {
metalness: 0.42
});
makeBox(world, 0.18, 0.18, 12.6, palette.brass, 12.72, 0.66, 0, {
metalness: 0.42
});
const screwPositions = [
[-12.1, -5.7],
[-12.1, 5.7],
[12.1, -5.7],
[12.1, 5.7],
[-5.0, -3.85],
[-5.0, 3.85],
[5.0, -3.85],
[5.0, 3.85]
];
for (const [x, z] of screwPositions) {
makeBox(world, 0.19, 0.08, 0.19, palette.brass, x, 0.88, z, {
metalness: 0.65,
roughness: 0.4
});
}
for (let x = -12; x <= 12; x += 3) {
makeBox(world, 0.045, 0.025, 12.0, 0x315159, x, 0.735, 0, {
castShadow: false,
roughness: 0.9
});
}
for (let z = -4.5; z <= 4.5; z += 3) {
makeBox(world, 24.8, 0.025, 0.045, 0x315159, 0, 0.735, z, {
castShadow: false,
roughness: 0.9
});
}
for (const x of [-6.35, 6.35]) {
for (let z = -5.35; z <= 5.36; z += 0.75) {
makeBox(world, 0.14, 0.09, 0.28, 0x4d5b58, x, 0.87, z, {
castShadow: false,
emissive: 0x17352f,
emissiveIntensity: 0.3
});
}
}
}
function addPickMesh(parent, type, id, width, depth, x, y, z) {
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
const hitMesh = new THREE.Mesh(
new THREE.BoxGeometry(width, 1.7, depth),
hitMaterial
);
hitMesh.position.set(x, y, z);
hitMesh.userData.pick = { type, id };
parent.add(hitMesh);
pickMeshes.push(hitMesh);
return hitMesh;
}
function createSocket(type, id, x, z, capacity = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
world.add(group);
const isRelay = type === "relay";
const baseSize = isRelay ? 1.4 : 1.18;
makeBox(
group,
baseSize,
0.27,
baseSize,
isRelay ? 0x33443e : 0x31434a,
0,
0.95,
0,
{ metalness: 0.22 }
);
makeBox(
group,
baseSize - 0.18,
0.08,
baseSize - 0.18,
isRelay ? 0x172820 : 0x15252c,
0,
1.12,
0,
{ castShadow: false }
);
const rimColor = isRelay ? palette.brass : 0x8d9b91;
makeBox(group, baseSize, 0.11, 0.12, rimColor, 0, 1.19, -baseSize / 2 + 0.06, {
metalness: 0.48,
roughness: 0.4
});
makeBox(group, baseSize, 0.11, 0.12, rimColor, 0, 1.19, baseSize / 2 - 0.06, {
metalness: 0.48,
roughness: 0.4
});
makeBox(group, 0.12, 0.11, baseSize - 0.12, rimColor, -baseSize / 2 + 0.06, 1.19, 0, {
metalness: 0.48,
roughness: 0.4
});
makeBox(group, 0.12, 0.11, baseSize - 0.12, rimColor, baseSize / 2 - 0.06, 1.19, 0, {
metalness: 0.48,
roughness: 0.4
});
const plugMaterial = material(0x20343b, {
roughness: 0.45,
metalness: 0.35
});
const plug = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.32, 0.34),
plugMaterial
);
plug.position.set(0, 1.32, 0);
plug.castShadow = true;
group.add(plug);
const lampMaterial = material(0x18282d, {
emissive: 0x000000,
emissiveIntensity: 0
});
const lamp = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.2, 0.2),
lampMaterial
);
lamp.position.set(baseSize / 2 - 0.23, 1.27, -baseSize / 2 + 0.23);
lamp.castShadow = false;
group.add(lamp);
const anchor = new THREE.Vector3(x, 1.35, z);
addPickMesh(group, type, id, baseSize + 0.35, baseSize + 0.35, 0, 1.12, 0);
if (isRelay) {
const occupancy = [];
for (let i = 0; i < capacity; i++) {
const occupancyMaterial = material(0x1a292d, {
emissive: 0x000000,
emissiveIntensity: 0
});
const occupancyLamp = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.13, 0.19),
occupancyMaterial
);
occupancyLamp.position.set(
capacity === 1 ? 0 : (i === 0 ? -0.24 : 0.24),
1.28,
baseSize / 2 - 0.2
);
group.add(occupancyLamp);
occupancy.push(occupancyLamp);
}
relayVisuals.set(id, {
group,
plug,
plugMaterial,
lamp,
lampMaterial,
occupancy,
anchor: new THREE.Vector3(x, 1.95, z),
capacity
});
const label = makeLabel(
"relay-label",
`<strong>Socket ${id}</strong><span>0 / ${capacity}</span>`
);
label.addEventListener("click", (event) => {
event.stopPropagation();
handleRelay(id);
});
relayLabels.set(id, label);
}
return { group, plug, plugMaterial, lamp, lampMaterial, anchor };
}
createBoard();
const sourceZ = [-4.65, -2.79, -0.93, 0.93, 2.79, 4.65];
calls.forEach((call, index) => {
const x = -9.35;
const z = sourceZ[index];
const visual = createSocket("call", call.id, x, z);
visual.anchor.y = 1.7;
callVisuals.set(call.id, visual);
const label = makeLabel(
"source-label",
`
<span class="label-kicker">
<span>${call.line}</span>
<span>IN</span>
</span>
<strong>${call.caller}</strong>
<span class="request">Call ${call.wants}</span>
<span class="status">Waiting</span>
`
);
label.style.setProperty("--call-color", call.color);
label.addEventListener("click", (event) => {
event.stopPropagation();
handleCall(call.id);
});
sourceLabels.set(call.id, label);
});
targets.forEach((target, index) => {
const x = 9.35;
const z = sourceZ[index];
const visual = createSocket("target", target.id, x, z);
visual.anchor.y = 1.7;
targetVisuals.set(target.id, visual);
const label = makeLabel(
"target-label",
`
<span class="label-kicker">
<span>OUT</span>
<span>${String(index + 1).padStart(2, "0")}</span>
</span>
<strong>${target.name}</strong>
<small>${target.station}</small>
<span class="status">Open</span>
`
);
label.addEventListener("click", (event) => {
event.stopPropagation();
handleTarget(target.id);
});
targetLabels.set(target.id, label);
});
[-4, -2, 0, 2, 4].forEach((x, index) => {
const relay = relays[index];
createSocket("relay", relay.id, x, 0, relay.capacity);
});
function findCall(id) {
return calls.find((call) => call.id === id);
}
function findTarget(id) {
return targets.find((target) => target.id === id);
}
function findRelay(id) {
return relays.find((relay) => relay.id === id);
}
function relayLoad(relayId) {
return calls.filter((call) => call.relayId === relayId).length;
}
function targetOwner(targetId) {
return calls.find((call) => call.targetId === targetId) ?? null;
}
function stateOf(call) {
if (call.targetId) return "complete";
if (call.relayId) return "busy";
return "waiting";
}
function saveHistory() {
history.push(
calls.map((call) => ({
id: call.id,
relayId: call.relayId,
targetId: call.targetId
}))
);
}
function restoreSnapshot(snapshot) {
for (const saved of snapshot) {
const call = findCall(saved.id);
call.relayId = saved.relayId;
call.targetId = saved.targetId;
}
}
function showToast(message, error = false) {
toastElement.textContent = message;
toastElement.classList.toggle("error", error);
toastElement.classList.add("show");
window.clearTimeout(toastTimer);
toastTimer = window.setTimeout(() => {
toastElement.classList.remove("show");
}, 2400);
}
function handleCall(id) {
const call = findCall(id);
selectedCallId = id;
if (call.targetId) {
showToast(`${call.caller} → ${findTarget(call.targetId).name} is already complete.`);
} else if (!call.relayId) {
showToast(`${call.caller} selected. Now choose a central socket.`);
} else {
showToast(`${call.caller} is busy on Socket ${call.relayId}. Choose the outgoing line.`);
}
renderState();
}
function handleRelay(id) {
const relay = findRelay(id);
const selected = selectedCallId ? findCall(selectedCallId) : null;
if (!selected) {
const assigned = calls.find((call) => call.relayId === id);
if (assigned) {
selectedCallId = assigned.id;
showToast(`${assigned.caller} selected on Socket ${id}.`);
renderState();
} else {
showToast("Select a caller before choosing a socket.", true);
}
return;
}
if (selected.targetId) {
showToast("This call is complete. Reset the board to re-patch it.", true);
return;
}
if (selected.relayId === id) {
showToast(`${selected.caller} already uses Socket ${id}. Choose the outgoing line.`);
return;
}
const load = relayLoad(id);
if (load >= relay.capacity) {
showToast(`Socket ${id} is full. Choose another socket.`, true);
return;
}
saveHistory();
selected.relayId = id;
selected.targetId = null;
showToast(`${selected.caller} routed through Socket ${id}.`);
renderState();
}
function handleTarget(id) {
const target = findTarget(id);
const selected = selectedCallId ? findCall(selectedCallId) : null;
if (!selected) {
showToast("Select a caller before choosing an outgoing line.", true);
return;
}
if (selected.targetId) {
showToast("That call is already complete.", true);
return;
}
if (!selected.relayId) {
showToast("Patch the call through a central socket first.", true);
return;
}
const owner = targetOwner(id);
if (owner && owner.id !== selected.id) {
showToast(`${target.name}'s line is already connected.`, true);
return;
}
if (selected.wants.toLowerCase() !== target.id) {
const label = targetLabels.get(id);
label.classList.remove("fault");
void label.offsetWidth;
label.classList.add("fault");
window.clearTimeout(targetFlashTimer);
targetFlashTimer = window.setTimeout(() => label.classList.remove("fault"), 600);
showToast(
`${selected.caller} asked for ${selected.wants}, not ${target.name}.`,
true
);
return;
}
saveHistory();
selected.targetId = id;
showToast(`${selected.caller} → ${target.name}. Connection complete.`);
renderState();
}
function undo() {
const snapshot = history.pop();
if (!snapshot) return;
restoreSnapshot(snapshot);
if (selectedCallId && findCall(selectedCallId).targetId) {
selectedCallId = null;
}
renderState();
showToast("Last patch undone.");
}
function resetBoard() {
for (const call of calls) {
call.relayId = null;
call.targetId = null;
}
selectedCallId = null;
history = [];
successShown = false;
successElement.classList.remove("show");
renderState();
showToast("Switchboard cleared.");
}
undoButton.addEventListener("click", undo);
resetButton.addEventListener("click", resetBoard);
playAgainButton.addEventListener("click", resetBoard);
function setLamp(lampMaterial, color, intensity) {
lampMaterial.color.setHex(color);
lampMaterial.emissive.setHex(color);
lampMaterial.emissiveIntensity = intensity;
}
function renderState() {
for (const call of calls) {
const visual = callVisuals.get(call.id);
const label = sourceLabels.get(call.id);
const state = stateOf(call);
const selected = selectedCallId === call.id;
label.classList.toggle("selected", selected);
label.classList.toggle("busy", state === "busy");
label.classList.toggle("complete", state === "complete");
const status = label.querySelector(".status");
status.textContent =
state === "complete" ? "Complete" : state === "busy" ? "Busy" : "Waiting";
if (state === "complete") {
setLamp(visual.lampMaterial, palette.green, 1.5);
visual.plugMaterial.color.setHex(palette.green);
visual.plugMaterial.emissive.setHex(palette.green);
visual.plugMaterial.emissiveIntensity = 0.35;
} else if (state === "busy") {
const color = new THREE.Color(call.color).getHex();
setLamp(visual.lampMaterial, color, selected ? 1.8 : 0.85);
visual.plugMaterial.color.setHex(color);
visual.plugMaterial.emissive.setHex(color);
visual.plugMaterial.emissiveIntensity = selected ? 0.45 : 0.18;
} else if (selected) {
setLamp(visual.lampMaterial, palette.amber, 1.8);
visual.plugMaterial.color.setHex(palette.amber);
visual.plugMaterial.emissive.setHex(palette.amber);
visual.plugMaterial.emissiveIntensity = 0.4;
} else {
setLamp(visual.lampMaterial, 0x18282d, 0);
visual.plugMaterial.color.setHex(0x20343b);
visual.plugMaterial.emissive.setHex(0x000000);
visual.plugMaterial.emissiveIntensity = 0;
}
}
for (const target of targets) {
const visual = targetVisuals.get(target.id);
const label = targetLabels.get(target.id);
const owner = targetOwner(target.id);
const connected = Boolean(owner);
label.classList.toggle("complete", connected);
label.querySelector(".status").textContent = connected ? "Connected" : "Open";
if (connected) {
setLamp(visual.lampMaterial, palette.green, 1.5);
visual.plugMaterial.color.setHex(palette.green);
visual.plugMaterial.emissive.setHex(palette.green);
visual.plugMaterial.emissiveIntensity = 0.4;
visual.plug.scale.setScalar(1);
} else {
setLamp(visual.lampMaterial, 0x18282d, 0);
visual.plugMaterial.color.setHex(0x20343b);
visual.plugMaterial.emissive.setHex(0x000000);
visual.plugMaterial.emissiveIntensity = 0;
visual.plug.scale.setScalar(0.78);
}
}
for (const relay of relays) {
const visual = relayVisuals.get(relay.id);
const label = relayLabels.get(relay.id);
const load = relayLoad(relay.id);
const full = load >= relay.capacity;
label.classList.toggle("occupied", load > 0);
label.classList.toggle("full", full);
label.querySelector("span").textContent = `${load} / ${relay.capacity}`;
visual.occupancy.forEach((lamp, index) => {
if (index < load) {
setLamp(lamp.material, palette.amber, 1.1);
} else {
setLamp(lamp.material, 0x18282d, 0);
}
});
const assignedCall = calls.find((call) => call.relayId === relay.id);
if (assignedCall) {
const color = new THREE.Color(assignedCall.color).getHex();
visual.plugMaterial.color.setHex(color);
visual.plugMaterial.emissive.setHex(color);
visual.plugMaterial.emissiveIntensity = 0.2;
setLamp(visual.lampMaterial, palette.amber, 0.55);
} else {
visual.plugMaterial.color.setHex(0x20343b);
visual.plugMaterial.emissive.setHex(0x000000);
visual.plugMaterial.emissiveIntensity = 0;
setLamp(visual.lampMaterial, 0x18282d, 0);
}
}
const completed = calls.filter((call) => call.targetId).length;
completeCountElement.textContent = String(completed);
undoButton.disabled = history.length === 0;
const selected = selectedCallId ? findCall(selectedCallId) : null;
if (selected) {
const route = selected.relayId
? `through Socket ${selected.relayId}`
: "not yet routed";
selectionText.innerHTML = `
<strong>${selected.caller} → ${selected.wants}</strong><br />
Selected call is ${route}. ${
selected.targetId ? "The connection is complete." : "Choose the matching outgoing line."
}
`;
} else {
selectionText.innerHTML = `
<strong>No caller selected.</strong><br />
Socket A carries two calls; B–E carry one each.
`;
}
rebuildCables();
if (completed === calls.length && !successShown) {
successShown = true;
window.setTimeout(() => {
successElement.classList.add("show");
createConfetti();
}, 550);
}
}
function disposeGroup(group) {
const geometries = new Set();
const materials = new Set();
group.traverse((object) => {
if (object.geometry) geometries.add(object.geometry);
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach((item) => materials.add(item));
} else {
materials.add(object.material);
}
}
});
geometries.forEach((geometry) => geometry.dispose());
materials.forEach((item) => item.dispose());
group.clear();
}
function addBeam(group, from, to, beamMaterial) {
const direction = new THREE.Vector3().subVectors(to, from);
const length = direction.length();
if (length < 0.001) return;
const beam = new THREE.Mesh(
new THREE.BoxGeometry(0.085, length, 0.085),
beamMaterial
);
beam.position.copy(from).add(to).multiplyScalar(0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
beam.castShadow = true;
beam.receiveShadow = true;
group.add(beam);
}
function addJoint(group, point, jointMaterial) {
const joint = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.15, 0.15),
jointMaterial
);
joint.position.copy(point);
joint.castShadow = true;
group.add(joint);
}
function rebuildCables() {
for (const [callId, group] of cableGroups) {
disposeGroup(group);
world.remove(group);
cableGroups.delete(callId);
}
calls.forEach((call, index) => {
if (!call.relayId) return;
const group = new THREE.Group();
world.add(group);
cableGroups.set(call.id, group);
const complete = Boolean(call.targetId);
const color = complete ? palette.green : new THREE.Color(call.color).getHex();
const cableMaterial = material(color, {
roughness: 0.42,
metalness: 0.12,
emissive: color,
emissiveIntensity: complete ? 0.5 : 0.24
});
cableMaterial.userData.pulse = !complete;
group.userData.cableMaterial = cableMaterial;
const sourceVisual = callVisuals.get(call.id);
const relayVisual = relayVisuals.get(call.relayId);
const source = sourceVisual.anchor.clone();
const relay = relayVisual.anchor.clone();
const y = 1.2 + index * 0.025;
source.y = y;
relay.y = y;
const laneLeft = -8.15 + index * 0.11;
const sourcePath = [
source,
new THREE.Vector3(laneLeft, y, source.z),
new THREE.Vector3(laneLeft, y, relay.z),
new THREE.Vector3(relay.x - 0.72, y, relay.z),
relay
];
for (let i = 0; i < sourcePath.length - 1; i++) {
addBeam(group, sourcePath[i], sourcePath[i + 1], cableMaterial);
}
sourcePath.forEach((point) => addJoint(group, point, cableMaterial));
if (complete) {
const targetVisual = targetVisuals.get(call.targetId);
const target = targetVisual.anchor.clone();
target.y = y;
const laneRight = 8.15 - index * 0.11;
const targetPath = [
relay,
new THREE.Vector3(relay.x + 0.72, y, relay.z),
new THREE.Vector3(laneRight, y, target.z),
new THREE.Vector3(target.x - 0.72, y, target.z),
target
];
for (let i = 0; i < targetPath.length - 1; i++) {
addBeam(group, targetPath[i], targetPath[i + 1], cableMaterial);
}
targetPath.slice(1).forEach((point) => addJoint(group, point, cableMaterial));
}
});
}
const confetti = new THREE.Group();
scene.add(confetti);
const confettiPieces = [];
function createConfetti() {
disposeGroup(confetti);
for (let i = 0; i < 85; i++) {
const color = [palette.amber, palette.green, palette.cream, 0xff8f6b][
i % 4
];
const piece = makeBox(
confetti,
0.12 + Math.random() * 0.1,
0.08 + Math.random() * 0.12,
0.12 + Math.random() * 0.1,
color,
(Math.random() - 0.5) * 22,
5 + Math.random() * 8,
(Math.random() - 0.5) * 11,
{ castShadow: false }
);
confettiPieces.push({
mesh: piece,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 1.3,
-1.2 - Math.random() * 1.8,
(Math.random() - 0.5) * 0.8
),
spin: new THREE.Vector3(
Math.random() * 0.1,
Math.random() * 0.12,
Math.random() * 0.1
)
});
}
}
function updateConfetti(delta) {
for (const piece of confettiPieces) {
piece.velocity.y -= 0.8 * delta;
piece.mesh.position.addScaledVector(piece.velocity, delta);
piece.mesh.rotation.x += piece.spin.x;
piece.mesh.rotation.y += piece.spin.y;
piece.mesh.rotation.z += piece.spin.z;
if (piece.mesh.position.y < -1.5) {
piece.mesh.position.y = 8 + Math.random() * 3;
piece.mesh.position.x = (Math.random() - 0.5) * 22;
}
}
}
const cameraState = {
yaw: 0,
pitch: 0.69,
radius: window.innerWidth < 760 ? 35 : 26.5,
target: new THREE.Vector3(0, 0.3, 0)
};
function updateCamera() {
const { yaw, pitch, radius, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(yaw) * Math.cos(pitch),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(yaw) * Math.cos(pitch)
);
camera.lookAt(target);
}
function resetCamera() {
cameraState.yaw = 0;
cameraState.pitch = 0.69;
cameraState.radius = window.innerWidth < 760 ? 35 : 26.5;
cameraState.target.set(0, 0.3, 0);
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerState = {
down: false,
button: 0,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerState.down = true;
pointerState.button = event.button;
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
pointerState.moved = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerState.down) return;
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
const totalDistance = Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY
);
if (totalDistance > 5) pointerState.moved = true;
if (pointerState.button === 2 || event.shiftKey) {
camera.updateMatrixWorld();
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const scale = cameraState.radius * 0.0016;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(up, dy * scale);
cameraState.target.x = THREE.MathUtils.clamp(cameraState.target.x, -4, 4);
cameraState.target.z = THREE.MathUtils.clamp(cameraState.target.z, -2.5, 2.5);
} else {
cameraState.yaw -= dx * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.004,
0.38,
1.16
);
}
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerState.down) return;
pointerState.down = false;
if (
pointerState.button === 0 &&
!pointerState.moved &&
event.clientX === pointerState.startX &&
event.clientY === pointerState.startY
) {
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);
const hits = raycaster.intersectObjects(pickMeshes, false);
if (hits.length) {
const pick = hits[0].object.userData.pick;
if (pick.type === "call") handleCall(pick.id);
if (pick.type === "relay") handleRelay(pick.id);
if (pick.type === "target") handleTarget(pick.id);
}
}
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState.down = false;
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.018,
18,
42
);
}, { passive: false });
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("dblclick", resetCamera);
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = window.innerWidth < 760 ? 48 : 42;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
const projected = new THREE.Vector3();
function positionLabel(element, anchor) {
projected.copy(anchor).project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
const visible = projected.z > -1 && projected.z < 1;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.opacity = visible ? "1" : "0";
element.style.pointerEvents = visible ? "auto" : "none";
}
function updateLabels() {
for (const visual of callVisuals.values()) {
const label = sourceLabels.get(
[...callVisuals.entries()].find(([, item]) => item === visual)[0]
);
positionLabel(label, visual.anchor);
}
for (const [id, visual] of targetVisuals) {
positionLabel(targetLabels.get(id), visual.anchor);
}
for (const [id, visual] of relayVisuals) {
positionLabel(relayLabels.get(id), visual.anchor);
}
}
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera();
for (const visual of callVisuals.values()) {
const base = visual.lampMaterial.emissiveIntensity;
if (base > 0) {
visual.lampMaterial.emissiveIntensity =
base * (0.78 + Math.sin(elapsed * 5.2) * 0.22);
}
}
for (const visual of relayVisuals.values()) {
visual.occupancy.forEach((lamp) => {
if (lamp.material.emissiveIntensity > 0) {
lamp.material.emissiveIntensity =
0.8 + Math.sin(elapsed * 4.1) * 0.3;
}
});
}
for (const group of cableGroups.values()) {
const cableMaterial = group.userData.cableMaterial;
if (cableMaterial?.userData.pulse) {
cableMaterial.emissiveIntensity = 0.18 + Math.sin(elapsed * 4.5) * 0.09;
}
}
if (successShown) updateConfetti(delta);
updateLabels();
renderer.render(scene, camera);
}
renderState();
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```