Prompt specification
Build or customize the Three.js 3D scene "Reconfigurable classroom cable puzzle" 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: Reconfigurable classroom cable puzzle
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reconfigurable-classroom-cable-puzzle-x0176
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring cable management. Build four movable computer desks and floor connection points; users route symbolic cables through approved channels while avoiding crossings through doors and chair movement areas. Low-poly.
Working Three.js Starter (reconfigurable-classroom-cable-puzzle-x0176):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Floorplan — Cable Management</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(13, 20, 34, 0.92);
--panel-border: rgba(155, 180, 220, 0.18);
--text: #edf4ff;
--muted: #9eb0c9;
--blue: #58a6ff;
--green: #58e6a9;
--amber: #ffc857;
--red: #ff6978;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #09111e;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
#app, #app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas { touch-action: none; }
.glass {
background: var(--panel);
border: 1px solid var(--panel-border);
box-shadow: 0 16px 44px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.topbar {
position: fixed;
z-index: 5;
top: 18px;
left: 18px;
padding: 13px 17px;
border-radius: 14px;
pointer-events: none;
}
.eyebrow {
color: var(--blue);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 3px 0 4px;
font-size: 20px;
line-height: 1.1;
letter-spacing: -0.025em;
}
.subtitle {
color: var(--muted);
font-size: 12px;
}
.toolbar {
position: fixed;
z-index: 6;
top: 18px;
right: 18px;
display: flex;
gap: 7px;
}
.toolbar button, .panel button {
color: var(--text);
border: 1px solid rgba(160, 190, 230, 0.2);
background: rgba(30, 43, 64, 0.86);
border-radius: 9px;
cursor: pointer;
transition: 150ms ease;
}
.toolbar button {
min-height: 36px;
padding: 8px 11px;
font-size: 12px;
}
.toolbar button:hover, .panel button:hover {
background: rgba(52, 75, 108, 0.95);
border-color: rgba(110, 180, 255, 0.55);
transform: translateY(-1px);
}
.panel {
position: fixed;
z-index: 5;
top: 76px;
right: 18px;
width: 318px;
max-height: calc(100vh - 96px);
padding: 15px;
border-radius: 16px;
overflow: auto;
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.panel h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.01em;
}
.count {
color: var(--green);
font-size: 11px;
font-weight: 750;
}
.instructions {
margin: 0 0 12px;
padding: 11px;
color: #c6d3e5;
background: rgba(75, 125, 190, 0.1);
border: 1px solid rgba(105, 160, 230, 0.16);
border-radius: 11px;
font-size: 11px;
line-height: 1.55;
}
.instructions strong { color: white; }
.route-list {
display: grid;
gap: 8px;
}
.route-card {
padding: 10px;
border: 1px solid rgba(150, 180, 220, 0.13);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
transition: 150ms ease;
}
.route-card:hover {
background: rgba(255, 255, 255, 0.065);
border-color: rgba(130, 180, 240, 0.3);
}
.route-card.selected {
border-color: rgba(88, 166, 255, 0.65);
box-shadow: inset 3px 0 0 var(--blue);
}
.route-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.desk-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 12px;
font-weight: 750;
}
.cable-dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.status {
padding: 3px 6px;
border-radius: 999px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
}
.status.valid {
color: var(--green);
background: rgba(88, 230, 169, 0.11);
}
.status.missing {
color: var(--amber);
background: rgba(255, 200, 87, 0.11);
}
.route-meta {
margin-top: 7px;
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font-size: 10px;
}
.clear-route {
padding: 3px 7px;
font-size: 10px;
background: transparent !important;
}
.panel-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 12px;
}
.panel-actions button {
min-height: 34px;
padding: 7px 9px;
font-size: 11px;
}
.panel-actions .primary {
background: rgba(53, 125, 218, 0.23);
border-color: rgba(88, 166, 255, 0.38);
}
.legend {
position: fixed;
z-index: 5;
left: 18px;
bottom: 18px;
width: 246px;
padding: 12px 14px;
border-radius: 14px;
pointer-events: none;
}
.legend-title {
margin-bottom: 8px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
color: #cfddf1;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 6px;
color: var(--muted);
font-size: 10px;
}
.swatch {
width: 17px;
height: 6px;
border-radius: 3px;
}
.swatch.channel { background: var(--blue); }
.swatch.cable { background: var(--green); }
.swatch.door { background: var(--red); }
.swatch.chair { background: var(--amber); }
#hint {
position: fixed;
z-index: 7;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
max-width: min(560px, calc(100vw - 590px));
padding: 10px 15px;
border-radius: 999px;
color: #dbe7f7;
font-size: 11px;
text-align: center;
pointer-events: none;
transition: opacity 180ms ease;
}
#toast {
position: fixed;
z-index: 10;
top: 86px;
left: 50%;
transform: translate(-50%, -12px);
padding: 10px 15px;
border-radius: 10px;
color: white;
font-size: 12px;
opacity: 0;
pointer-events: none;
transition: 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
color: #c9d9ef;
background: #09111e;
font-size: 13px;
letter-spacing: 0.08em;
transition: opacity 250ms ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }
@media (max-width: 820px) {
.topbar { top: 10px; left: 10px; }
.toolbar { top: 10px; right: 10px; }
.panel {
top: auto;
bottom: 10px;
right: 10px;
width: min(310px, calc(100vw - 20px));
max-height: 46vh;
}
.legend { display: none; }
#hint {
left: 10px;
bottom: 10px;
transform: none;
max-width: calc(100vw - 340px);
border-radius: 12px;
}
}
@media (max-width: 560px) {
.subtitle { display: none; }
.topbar { padding: 10px 12px; }
h1 { font-size: 16px; }
.toolbar button { padding: 7px 8px; }
.toolbar button span { display: none; }
#hint { display: none; }
.panel { max-height: 52vh; }
}
</style>
</head>
<body>
<div id="app"></div>
<div id="loading">BUILDING FLOORPLAN…</div>
<header class="topbar glass">
<div class="eyebrow">Facilities / Cable Planning</div>
<h1>Workplace Cable Manager</h1>
<div class="subtitle">Route four desks through approved floor channels</div>
</header>
<nav class="toolbar" aria-label="View controls">
<button id="reset-view" title="Reset camera"><span>⌂</span> Reset view</button>
<button id="top-view" title="Top view"><span>▦</span> Top view</button>
</nav>
<aside class="panel glass">
<div class="panel-header">
<h2>Cable schedule</h2>
<div class="count" id="route-count">0 / 4 valid</div>
</div>
<p class="instructions" id="instructions">
<strong>Route:</strong> click a floor connector, then click a desk.
For a custom route, click neighboring blue junction nodes before the desk.
Drag desks between marked docking points.
</p>
<div class="route-list" id="route-list"></div>
<div class="panel-actions">
<button class="primary" id="auto-route">Auto-route missing</button>
<button id="clear-routes">Clear routes</button>
<button id="reset-scene">Reset scene</button>
<button id="repair-routes">Repair all</button>
</div>
</aside>
<aside class="legend glass">
<div class="legend-title">Floor plan legend</div>
<div class="legend-row"><span class="swatch channel"></span> Approved cable channel</div>
<div class="legend-row"><span class="swatch cable"></span> Active symbolic cable</div>
<div class="legend-row"><span class="swatch door"></span> Door swing / keep clear</div>
<div class="legend-row"><span class="swatch chair"></span> Chair movement envelope</div>
</aside>
<div id="hint" class="glass">
Left-drag empty floor: orbit · Right-drag: pan · Wheel: zoom · Drag desk: move
</div>
<div id="toast" class="glass" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x09111e);
scene.fog = new THREE.Fog(0x09111e, 35, 65);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.05;
renderer.domElement.setAttribute("aria-label", "Interactive 3D cable management floorplan");
app.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraState = {
theta: 0.72,
phi: 0.82,
radius: 31,
target: new THREE.Vector3(0, 0, 0)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
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);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xb8d7ff, 0x253047, 1.55);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffffff, 2.2);
sun.position.set(-9, 17, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.PointLight(0x4a8cff, 18, 34, 2);
fill.position.set(10, 8, -4);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and helpers
// -------------------------------------------------------------------------
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.78,
metalness: options.metalness ?? 0.08,
flatShading: true,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
depthWrite: options.depthWrite ?? true,
side: options.side ?? THREE.FrontSide
});
}
const mat = {
floor: standard(0x172337),
floorInset: standard(0x1d2c43),
wall: standard(0x34445b),
wallDark: standard(0x202d40),
trim: standard(0x52647d),
deskTop: standard(0xc08a55),
deskEdge: standard(0x754f35),
metal: standard(0x8a99ac, { metalness: 0.5, roughness: 0.48 }),
darkMetal: standard(0x263448, { metalness: 0.45 }),
monitor: standard(0x15243a, { roughness: 0.38 }),
screen: standard(0x174c65, {
emissive: 0x1b89a8,
emissiveIntensity: 0.65,
roughness: 0.32
}),
chair: standard(0x354a68),
chairDark: standard(0x1c293c),
channel: standard(0x3977ba, {
emissive: 0x12385f,
emissiveIntensity: 0.65,
roughness: 0.58
}),
junction: standard(0x80bfff, {
emissive: 0x397fc4,
emissiveIntensity: 0.75
}),
outlet: standard(0x55e5ad, {
emissive: 0x1a9f70,
emissiveIntensity: 1.2
}),
door: standard(0xff5d70, {
transparent: true,
opacity: 0.18,
depthWrite: false
}),
chairZone: standard(0xffc857, {
transparent: true,
opacity: 0.16,
depthWrite: false
})
};
function box(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(radius, height, material, x = 0, y = 0, z = 0, parent = scene, segments = 10) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, segments),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, color = "#dceaff", scale = 2.2) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 112;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(9, 17, 30, 0.82)";
ctx.fillRect(4, 5, 504, 102);
ctx.strokeStyle = color;
ctx.lineWidth = 4;
ctx.strokeRect(4, 5, 504, 102);
ctx.fillStyle = color;
ctx.font = "700 42px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 57);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
depthTest: false
})
);
sprite.scale.set(scale, scale * 0.219, 1);
sprite.renderOrder = 20;
return sprite;
}
function rectBorder(rect, color, opacity = 0.9) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(rect.minX, 0.075, rect.minZ),
new THREE.Vector3(rect.maxX, 0.075, rect.minZ),
new THREE.Vector3(rect.maxX, 0.075, rect.maxZ),
new THREE.Vector3(rect.minX, 0.075, rect.maxZ)
]);
const line = new THREE.LineLoop(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
scene.add(line);
return line;
}
function floorRect(rect, material, y = 0.035) {
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(rect.maxX - rect.minX, rect.maxZ - rect.minZ),
material
);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(
(rect.minX + rect.maxX) / 2,
y,
(rect.minZ + rect.maxZ) / 2
);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Architectural shell
// -------------------------------------------------------------------------
box(30, 0.35, 20, mat.floor, 0, -0.18, 0);
box(29.4, 0.04, 19.4, mat.floorInset, 0, 0.012, 0);
// Floor grid lines.
const gridPoints = [];
for (let x = -14; x <= 14; x += 2) {
gridPoints.push(
new THREE.Vector3(x, 0.04, -9.5),
new THREE.Vector3(x, 0.04, 9.5)
);
}
for (let z = -9; z <= 9; z += 2) {
gridPoints.push(
new THREE.Vector3(-14.5, 0.04, z),
new THREE.Vector3(14.5, 0.04, z)
);
}
scene.add(
new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(gridPoints),
new THREE.LineBasicMaterial({
color: 0x5b7390,
transparent: true,
opacity: 0.12
})
)
);
// Back and left walls.
box(30, 3.4, 0.25, mat.wall, 0, 1.7, -10);
box(0.25, 3.4, 20, mat.wall, -15, 1.7, 0);
box(30, 0.13, 0.32, mat.trim, 0, 3.43, -9.86);
box(0.32, 0.13, 20, mat.trim, -14.86, 3.43, 0);
// Right wall with a door opening.
box(0.24, 3.4, 8.7, mat.wall, 15, 1.7, -5.65);
box(0.24, 3.4, 6.5, mat.wall, 15, 1.7, 6.75);
box(0.32, 0.22, 4.8, mat.trim, 14.84, 3.45, 1.2);
// Open door leaf.
const doorLeaf = box(0.15, 3.1, 2.4, mat.wallDark, 13.7, 1.55, -1.45);
doorLeaf.rotation.y = -Math.PI / 4;
const doorRect = {
minX: 9.8,
maxX: 14.8,
minZ: -1.35,
maxZ: 3.75
};
floorRect(doorRect, mat.door, 0.055);
rectBorder(doorRect, 0xff6978, 0.9);
// Door hatching.
const hatchPoints = [];
for (let x = doorRect.minX; x < doorRect.maxX; x += 1.25) {
hatchPoints.push(
new THREE.Vector3(x, 0.07, doorRect.minZ),
new THREE.Vector3(Math.min(x + 2.1, doorRect.maxX), 0.07, Math.min(doorRect.minZ + 2.1, doorRect.maxZ))
);
}
scene.add(
new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(hatchPoints),
new THREE.LineBasicMaterial({
color: 0xff6978,
transparent: true,
opacity: 0.42
})
)
);
const doorLabel = makeLabel("DOOR / KEEP CLEAR", "#ff7b89", 3.2);
doorLabel.position.set(12.25, 0.65, 1.2);
scene.add(doorLabel);
// -------------------------------------------------------------------------
// Approved channel graph
// -------------------------------------------------------------------------
const nodes = new Map();
const edges = [];
const adjacency = new Map();
const interactiveMeshes = [];
const outletRings = [];
function addNode(id, x, z, type = "junction") {
const node = { id, x, z, type, mesh: null };
nodes.set(id, node);
adjacency.set(id, []);
if (type === "outlet") {
const base = cylinder(0.48, 0.12, mat.darkMetal, x, 0.08, z, scene, 12);
const socket = cylinder(0.27, 0.24, mat.outlet, x, 0.2, z, scene, 12);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.38, 0.045, 6, 18),
new THREE.MeshBasicMaterial({ color: 0x58e6a9 })
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.12, z);
scene.add(ring);
outletRings.push(ring);
for (const mesh of [base, socket, ring]) {
mesh.userData = { kind: "outlet", outletId: id };
interactiveMeshes.push(mesh);
}
const label = makeLabel(id, "#74f0be", 1.35);
label.position.set(x, 1.12, z);
scene.add(label);
} else if (type === "port") {
const socket = cylinder(0.27, 0.08, mat.junction, x, 0.065, z, scene, 10);
socket.userData = { kind: "node", nodeId: id };
interactiveMeshes.push(socket);
node.mesh = socket;
} else {
const dot = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 8, 5),
mat.junction
);
dot.position.set(x, 0.105, z);
scene.add(dot);
dot.userData = { kind: "node", nodeId: id };
interactiveMeshes.push(dot);
node.mesh = dot;
}
return node;
}
function addEdge(a, b) {
const na = nodes.get(a);
const nb = nodes.get(b);
const edge = { a, b };
edges.push(edge);
adjacency.get(a).push(b);
adjacency.get(b).push(a);
const dx = nb.x - na.x;
const dz = nb.z - na.z;
const length = Math.hypot(dx, dz);
const middleX = (na.x + nb.x) / 2;
const middleZ = (na.z + nb.z) / 2;
const rail = box(
Math.abs(dx) > 0.01 ? length : 0.23,
0.045,
Math.abs(dz) > 0.01 ? length : 0.23,
mat.channel,
middleX,
0.05,
middleZ
);
rail.castShadow = false;
const dashGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(na.x, 0.078, na.z),
new THREE.Vector3(nb.x, 0.078, nb.z)
]);
const dashes = new THREE.Line(
dashGeometry,
new THREE.LineDashedMaterial({
color: 0x8ac8ff,
dashSize: 0.34,
gapSize: 0.22,
transparent: true,
opacity: 0.9
})
);
dashes.computeLineDistances();
scene.add(dashes);
}
// Floor connection points.
addNode("C1", -12, -7, "outlet");
addNode("C2", -12, 0, "outlet");
addNode("C3", -12, 7, "outlet");
addNode("C4", 12, -7, "outlet");
// Approved trunk rows.
["A1", "A2", "A3", "A4", "A5"].forEach((id, i) =>
addNode(`J_${id}`, -8 + i * 4, -7)
);
["B1", "B2", "B3", "B4", "B5"].forEach((id, i) =>
addNode(`J_${id}`, -8 + i * 4, 0)
);
["C1", "C2", "C3", "C4", "C5"].forEach((id, i) =>
addNode(`J_${id}`, -8 + i * 4, 7)
);
// Desk docking ports.
const slotDefinitions = [
{ id: "P_T1", x: -8, z: -5 },
{ id: "P_T2", x: -4, z: -5 },
{ id: "P_T3", x: 0, z: -5 },
{ id: "P_T4", x: 4, z: -5 },
{ id: "P_U1", x: 0, z: 3 },
{ id: "P_U2", x: 4, z: 3 },
{ id: "P_U3", x: 8, z: 3 }
];
for (const slot of slotDefinitions) addNode(slot.id, slot.x, slot.z, "port");
// Horizontal trunks.
addEdge("C1", "J_A1");
addEdge("J_A1", "J_A2");
addEdge("J_A2", "J_A3");
addEdge("J_A3", "J_A4");
addEdge("J_A4", "J_A5");
addEdge("J_A5", "C4");
addEdge("C2", "J_B1");
addEdge("J_B1", "J_B2");
addEdge("J_B2", "J_B3");
addEdge("J_B3", "J_B4");
addEdge("J_B4", "J_B5");
addEdge("C3", "J_C1");
addEdge("J_C1", "J_C2");
addEdge("J_C2", "J_C3");
addEdge("J_C3", "J_C4");
addEdge("J_C4", "J_C5");
// Vertical trunks.
addEdge("C1", "C2");
addEdge("C2", "C3");
["A1", "A2", "A3", "A4", "A5"].forEach(id => {
addEdge(`J_${id}`, `J_${id.replace("A", "B")}`);
addEdge(`J_${id.replace("A", "B")}`, `J_${id.replace("A", "C")}`);
});
// Approved drops to docking ports.
addEdge("J_A1", "P_T1");
addEdge("J_A2", "P_T2");
addEdge("J_A3", "P_T3");
addEdge("J_A4", "P_T4");
addEdge("J_B3", "P_U1");
addEdge("J_B4", "P_U2");
addEdge("J_B5", "P_U3");
// Docking rings.
const slotRings = new Map();
for (const slot of slotDefinitions) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.04, 6, 18),
new THREE.MeshBasicMaterial({
color: 0x5d91d8,
transparent: true,
opacity: 0.65
})
);
ring.rotation.x = Math.PI / 2;
ring.position.set(slot.x, 0.11, slot.z);
scene.add(ring);
slotRings.set(slot.id, ring);
}
const channelLabel = makeLabel("APPROVED CHANNEL", "#78b7ff", 2.8);
channelLabel.position.set(-3, 0.25, -8.35);
scene.add(channelLabel);
// -------------------------------------------------------------------------
// Desks, chairs, and movable chair envelopes
// -------------------------------------------------------------------------
const cableColors = [0x58e6a9, 0x69a7ff, 0xc58cff, 0xffcf65];
const desks = new Map();
const deskOrder = ["D1", "D2", "D3", "D4"];
function makeChair(deskId) {
const group = new THREE.Group();
box(1.05, 0.18, 1.0, mat.chair, 0, 0.7, 0, group);
box(1.05, 1.05, 0.16, mat.chair, 0, 1.28, 0.46, group);
box(0.12, 0.62, 0.12, mat.chairDark, -0.42, 0.33, 0, group);
box(0.12, 0.62, 0.12, mat.chairDark, 0.42, 0.33, 0, group);
box(0.12, 0.12, 0.8, mat.chairDark, 0, 0.7, -0.2, group);
return group;
}
function makeDesk(id, index, slotId) {
const group = new THREE.Group();
// Work surface.
box(3.2, 0.16, 1.25, mat.deskTop, 0, 1.55, 0, group);
box(3.05, 0.09, 1.1, mat.deskEdge, 0, 1.44, 0, group);
// Legs.
for (const x of [-1.38, 1.38]) {
for (const z of [-0.43, 0.43]) {
box(0.12, 1.48, 0.12, mat.darkMetal, x, 0.75, z, group);
}
}
// Monitor.
box(0.16, 0.52, 0.16, mat.darkMetal, 0, 1.9, 0.04, group);
box(0.8, 0.1, 0.48, mat.darkMetal, 0, 1.67, 0.04, group);
box(1.55, 0.92, 0.12, mat.monitor, 0, 2.32, 0.06, group);
box(1.36, 0.73, 0.025, mat.screen, 0, 2.32, -0.015, group);
// Keyboard and tower.
box(1.05, 0.055, 0.38, mat.darkMetal, -0.25, 1.71, -0.27, group);
box(0.48, 0.92, 0.7, mat.darkMetal, 1.02, 0.74, 0.1, group);
box(0.03, 0.52, 0.42, mat.screen, 0.76, 0.83, 0.1, group);
group.traverse(object => {
if (object.isMesh) {
object.userData.kind = "desk";
object.userData.deskId = id;
interactiveMeshes.push(object);
}
});
const label = makeLabel(`DESK ${index + 1}`, "#e8f2ff", 1.8);
label.position.set(0, 3.17, 0);
group.add(label);
const chair = makeChair(id);
scene.add(chair);
const zone = floorRect(
{ minX: -1.6, maxX: 1.6, minZ: -1.2, maxZ: 1.2 },
mat.chairZone,
0.06
);
const border = rectBorder(
{ minX: -1.6, maxX: 1.6, minZ: -1.2, maxZ: 1.2 },
0xffc857,
0.85
);
border.position.y = 0.04;
const desk = {
id,
index,
group,
chair,
zone,
border,
slotId,
color: cableColors[index]
};
desks.set(id, desk);
scene.add(group);
moveDeskToSlot(desk, slotId, false);
return desk;
}
makeDesk("D1", 0, "P_T1");
makeDesk("D2", 1, "P_T2");
makeDesk("D3", 2, "P_U2");
makeDesk("D4", 3, "P_U3");
const defaultSlots = {
D1: "P_T1",
D2: "P_T2",
D3: "P_U2",
D4: "P_U3"
};
function slotById(id) {
return slotDefinitions.find(slot => slot.id === id);
}
function occupiedSlot(slotId, ignoredDesk = null) {
for (const desk of desks.values()) {
if (desk.id !== ignoredDesk && desk.slotId === slotId) return true;
}
return false;
}
function updateDeskEnvelope(desk) {
const center = desk.group.position;
const chairX = center.x;
const chairZ = center.z + 1.8;
desk.chair.position.set(chairX, 0, chairZ);
const width = 3.2;
const depth = 2.4;
desk.zone.position.set(chairX, 0.06, chairZ);
desk.zone.scale.set(width / 3.2, depth / 2.4, 1);
const minX = chairX - width / 2;
const maxX = chairX + width / 2;
const minZ = chairZ - depth / 2;
const maxZ = chairZ + depth / 2;
desk.zoneRect = { minX, maxX, minZ, maxZ };
desk.border.geometry.dispose();
desk.border.geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(minX, 0.075, minZ),
new THREE.Vector3(maxX, 0.075, minZ),
new THREE.Vector3(maxX, 0.075, maxZ),
new THREE.Vector3(minX, 0.075, maxZ)
]);
}
function moveDeskToSlot(desk, slotId, shouldRepair = true) {
const slot = slotById(slotId);
if (!slot) return;
desk.slotId = slotId;
desk.group.position.set(slot.x, 0, slot.z + 1.5);
updateDeskEnvelope(desk);
if (shouldRepair) repairAllRoutes();
}
function allRestrictedRects() {
return [
doorRect,
...[...desks.values()].map(desk => desk.zoneRect).filter(Boolean)
];
}
function pointInRect(x, z, rect) {
return x >= rect.minX && x <= rect.maxX && z >= rect.minZ && z <= rect.maxZ;
}
function segmentIntersectsRect(ax, az, bx, bz, rect) {
let t0 = 0;
let t1 = 1;
const dx = bx - ax;
const dz = bz - az;
const axes = [
[ax, dx, rect.minX, rect.maxX],
[az, dz, rect.minZ, rect.maxZ]
];
for (const [p, d, min, max] of axes) {
if (Math.abs(d) < 1e-8) {
if (p < min || p > max) return false;
} else {
let tA = (min - p) / d;
let tB = (max - p) / d;
if (tA > tB) [tA, tB] = [tB, tA];
t0 = Math.max(t0, tA);
t1 = Math.min(t1, tB);
if (t0 > t1) return false;
}
}
return true;
}
function edgeAllowed(edge) {
const a = nodes.get(edge.a);
const b = nodes.get(edge.b);
return !allRestrictedRects().some(rect =>
segmentIntersectsRect(a.x, a.z, b.x, b.z, rect)
);
}
function findEdge(a, b) {
return edges.find(edge =>
(edge.a === a && edge.b === b) || (edge.a === b && edge.b === a)
);
}
function findPath(start, target) {
if (!nodes.has(start) || !nodes.has(target)) return null;
const queue = [start];
const previous = new Map([[start, null]]);
while (queue.length) {
const current = queue.shift();
if (current === target) break;
const neighbors = [...adjacency.get(current)].sort();
for (const next of neighbors) {
if (previous.has(next)) continue;
const edge = findEdge(current, next);
if (!edgeAllowed(edge)) continue;
const point = nodes.get(next);
if (next !== target && allRestrictedRects().some(rect =>
pointInRect(point.x, point.z, rect)
)) continue;
previous.set(next, current);
queue.push(next);
}
}
if (!previous.has(target)) return null;
const path = [];
let current = target;
while (current !== null) {
path.push(current);
current = previous.get(current);
}
return path.reverse();
}
function validatePath(path) {
if (!path || path.length < 2) return false;
for (let i = 0; i < path.length - 1; i++) {
if (!edgeAllowed(findEdge(path[i], path[i + 1]))) return false;
}
return true;
}
// -------------------------------------------------------------------------
// Routes and symbolic cables
// -------------------------------------------------------------------------
const routes = new Map();
const cableLayer = new THREE.Group();
scene.add(cableLayer);
const cableVisuals = new Map();
let selectedDeskId = "D1";
let routing = null;
function pathLength(path) {
let length = 0;
for (let i = 0; i < path.length - 1; i++) {
const a = nodes.get(path[i]);
const b = nodes.get(path[i + 1]);
length += Math.hypot(a.x - b.x, a.z - b.z);
}
return length;
}
function setRoute(deskId, outletId, path) {
for (const [id, route] of routes) {
if (id !== deskId && route.outletId === outletId) routes.delete(id);
}
routes.set(deskId, {
deskId,
outletId,
nodes: [...path],
valid: validatePath(path)
});
rebuildCables();
updatePanel();
}
function clearRoute(deskId) {
routes.delete(deskId);
rebuildCables();
updatePanel();
}
function disposeCableVisual(deskId) {
const visual = cableVisuals.get(deskId);
if (!visual) return;
visual.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material && object.material !== mat) {
if (Array.isArray(object.material)) object.material.forEach(m => m.dispose());
else object.material.dispose();
}
});
cableLayer.remove(visual);
cableVisuals.delete(deskId);
}
function rebuildCables() {
for (const deskId of [...cableVisuals.keys()]) disposeCableVisual(deskId);
for (const route of routes.values()) {
if (!route.valid || route.nodes.length < 2) continue;
const desk = desks.get(route.deskId);
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({ color: desk.color });
const jointMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.9
});
for (let i = 0; i < route.nodes.length - 1; i++) {
const a = nodes.get(route.nodes[i]);
const b = nodes.get(route.nodes[i + 1]);
const start = new THREE.Vector3(a.x, 0.16 + desk.index * 0.009, a.z);
const end = new THREE.Vector3(b.x, 0.16 + desk.index * 0.009, b.z);
const direction = end.clone().sub(start);
const length = direction.length();
const segment = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, length, 6),
material
);
segment.position.copy(start).add(end).multiplyScalar(0.5);
segment.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
group.add(segment);
}
for (const nodeId of route.nodes) {
const point = nodes.get(nodeId);
const joint = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 8, 5),
jointMaterial
);
joint.position.set(point.x, 0.16 + desk.index * 0.009, point.z);
group.add(joint);
}
cableLayer.add(group);
cableVisuals.set(route.deskId, group);
}
}
function repairAllRoutes() {
const usedOutlets = new Set();
const available = ["C1", "C2", "C3", "C4"];
for (const deskId of deskOrder) {
const existing = routes.get(deskId);
const candidates = existing
? [existing.outletId, ...available.filter(id => id !== existing.outletId)]
: available;
let assigned = null;
for (const outletId of candidates) {
if (usedOutlets.has(outletId)) continue;
const path = findPath(outletId, desks.get(deskId).slotId);
if (!path) continue;
assigned = { outletId, nodes: path, valid: true };
usedOutlets.add(outletId);
break;
}
if (assigned) {
routes.set(deskId, { deskId, ...assigned });
} else if (existing) {
routes.set(deskId, { ...existing, nodes: [], valid: false });
} else {
routes.set(deskId, {
deskId,
outletId: null,
nodes: [],
valid: false
});
}
}
rebuildCables();
updatePanel();
}
function autoRouteMissing() {
const used = new Set(
[...routes.values()].filter(route => route.valid).map(route => route.outletId)
);
for (const deskId of deskOrder) {
if (routes.get(deskId)?.valid) continue;
for (const outletId of ["C1", "C2", "C3", "C4"]) {
if (used.has(outletId)) continue;
const path = findPath(outletId, desks.get(deskId).slotId);
if (!path) continue;
setRoute(deskId, outletId, path);
used.add(outletId);
break;
}
}
toast("Missing cable routes repaired.");
}
// -------------------------------------------------------------------------
// Route preview and interaction state
// -------------------------------------------------------------------------
const previewMaterial = new THREE.LineDashedMaterial({
color: 0xffe18c,
dashSize: 0.38,
gapSize: 0.16,
transparent: true,
opacity: 0.95,
depthTest: false
});
const previewLine = new THREE.Line(
new THREE.BufferGeometry(),
previewMaterial
);
previewLine.visible = false;
previewLine.renderOrder = 15;
scene.add(previewLine);
const previewDot = new THREE.Mesh(
new THREE.SphereGeometry(0.17, 10, 6),
new THREE.MeshBasicMaterial({ color: 0xffe18c, depthTest: false })
);
previewDot.visible = false;
previewDot.renderOrder = 16;
scene.add(previewDot);
function updatePreview() {
if (!routing || routing.waypoints.length < 2) {
previewLine.visible = false;
previewDot.visible = false;
return;
}
const points = routing.waypoints.map(id => {
const node = nodes.get(id);
return new THREE.Vector3(node.x, 0.28, node.z);
});
previewLine.geometry.dispose();
previewLine.geometry = new THREE.BufferGeometry().setFromPoints(points);
previewLine.computeLineDistances();
previewLine.visible = true;
const last = points[points.length - 1];
previewDot.position.copy(last);
previewDot.visible = true;
}
function selectOutlet(outletId) {
routing = { outletId, waypoints: [outletId] };
updatePreview();
updatePanel();
toast(`Connector ${outletId} selected. Click a desk or neighboring channel nodes.`);
document.getElementById("instructions").innerHTML =
`<strong>${outletId} selected:</strong> click a desk for an automatic route, ` +
`or click adjacent blue nodes to build a custom route.`;
}
function clearRouting() {
routing = null;
updatePreview();
updatePanel();
document.getElementById("instructions").innerHTML =
`<strong>Route:</strong> click a floor connector, then click a desk. ` +
`For a custom route, click neighboring blue junction nodes before the desk. ` +
`Drag desks between marked docking points.`;
}
function appendNode(nodeId) {
if (!routing) {
toast("Select a floor connector first.");
return;
}
const last = routing.waypoints[routing.waypoints.length - 1];
if (last === nodeId) return;
if (!adjacency.get(last).includes(nodeId)) {
toast("Choose a directly connected channel node.");
return;
}
if (!edgeAllowed(findEdge(last, nodeId))) {
toast("That channel enters a restricted area.");
return;
}
routing.waypoints.push(nodeId);
updatePreview();
}
function completeRoute(deskId) {
if (!routing) {
selectDesk(deskId);
return;
}
const desk = desks.get(deskId);
const last = routing.waypoints[routing.waypoints.length - 1];
const tail = last === desk.slotId ? [desk.slotId] : findPath(last, desk.slotId);
if (!tail) {
toast("No safe approved-channel route to that desk.");
return;
}
const path = [...routing.waypoints, ...tail.slice(1)];
if (!validatePath(path)) {
toast("That route crosses a restricted area.");
return;
}
setRoute(deskId, routing.outletId, path);
clearRouting();
toast(`${routing?.outletId ?? "Cable"} routed to Desk ${desk.index + 1}.`);
}
// -------------------------------------------------------------------------
// Panel
// -------------------------------------------------------------------------
const routeList = document.getElementById("route-list");
const routeCount = document.getElementById("route-count");
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(2.05, 0.035, 6, 48),
new THREE.MeshBasicMaterial({
color: 0x8ac8ff,
transparent: true,
opacity: 0.65
})
);
selectionRing.rotation.x = Math.PI / 2;
selectionRing.position.y = 0.09;
scene.add(selectionRing);
function selectDesk(deskId) {
selectedDeskId = deskId;
const desk = desks.get(deskId);
selectionRing.position.set(desk.group.position.x, 0.09, desk.group.position.z);
updatePanel();
}
function updatePanel() {
let validCount = 0;
routeList.innerHTML = "";
for (const deskId of deskOrder) {
const desk = desks.get(deskId);
const route = routes.get(deskId);
const valid = route?.valid && route.nodes.length > 1;
if (valid) validCount++;
const card = document.createElement("div");
card.className = `route-card${deskId === selectedDeskId ? " selected" : ""}`;
card.dataset.deskId = deskId;
const color = `#${desk.color.toString(16).padStart(6, "0")}`;
const connector = route?.outletId ?? "—";
const length = valid ? `${pathLength(route.nodes).toFixed(1)} m` : "No route";
card.innerHTML = `
<div class="route-top">
<div class="desk-name">
<span class="cable-dot" style="background:${color};color:${color}"></span>
Desk ${String(desk.index + 1).padStart(2, "0")}
</div>
<span class="status ${valid ? "valid" : "missing"}">
${valid ? "VALID" : "NEEDS ROUTE"}
</span>
</div>
<div class="route-meta">
<span>${connector} → D${desk.index + 1} · ${length}</span>
<button class="clear-route" data-clear="${deskId}">Clear</button>
</div>
`;
routeList.appendChild(card);
}
routeCount.textContent = `${validCount} / 4 valid`;
routeCount.style.color = validCount === 4 ? "var(--green)" : "var(--amber)";
routeList.querySelectorAll(".route-card").forEach(card => {
card.addEventListener("click", event => {
if (event.target.closest("[data-clear]")) return;
selectDesk(card.dataset.deskId);
});
});
routeList.querySelectorAll("[data-clear]").forEach(button => {
button.addEventListener("click", event => {
event.stopPropagation();
clearRoute(button.dataset.clear);
});
});
}
// -------------------------------------------------------------------------
// Toasts
// -------------------------------------------------------------------------
const toastElement = document.getElementById("toast");
let toastTimer;
function toast(message) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastElement.classList.remove("show"), 2400);
}
// -------------------------------------------------------------------------
// Pointer controls, desk dragging, picking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const floorPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
let pointerState = null;
let activeSlotId = null;
function updatePointer(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);
}
function pickInteractive(event) {
updatePointer(event);
const hits = raycaster.intersectObjects(interactiveMeshes, false);
for (const hit of hits) {
if (!hit.object.userData.dead && hit.object.visible) return hit.object.userData;
}
return null;
}
function pointOnFloor(event) {
updatePointer(event);
return raycaster.ray.intersectPlane(floorPlane, new THREE.Vector3());
}
function setActiveSlot(slotId) {
activeSlotId = slotId;
for (const [id, ring] of slotRings) {
ring.material.color.set(id === slotId ? 0xffe18c : 0x5d91d8);
ring.material.opacity = id === slotId ? 1 : 0.65;
ring.scale.setScalar(id === slotId ? 1.22 : 1);
}
}
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
const hit = pickInteractive(event);
if (event.button === 2) {
pointerState = { mode: "pan", x: event.clientX, y: event.clientY };
return;
}
if (hit?.kind === "desk") {
const point = pointOnFloor(event);
const desk = desks.get(hit.deskId);
pointerState = {
mode: "desk",
deskId: hit.deskId,
x: event.clientX,
y: event.clientY,
moved: false,
offsetX: desk.group.position.x - point.x,
offsetZ: desk.group.position.z - point.z
};
return;
}
if (hit?.kind === "outlet") {
pointerState = {
mode: "outlet",
outletId: hit.outletId,
x: event.clientX,
y: event.clientY,
moved: false
};
return;
}
if (hit?.kind === "node") {
pointerState = {
mode: "node",
nodeId: hit.nodeId,
x: event.clientX,
y: event.clientY,
moved: false
};
return;
}
pointerState = {
mode: "orbit",
x: event.clientX,
y: event.clientY
};
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointerState) {
const hit = pickInteractive(event);
renderer.domElement.style.cursor =
hit?.kind === "desk" ? "grab" :
hit?.kind === "node" || hit?.kind === "outlet" ? "pointer" : "grab";
return;
}
if (pointerState.mode === "orbit") {
const dx = event.clientX - pointerState.x;
const dy = event.clientY - pointerState.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.08,
1.25
);
pointerState.x = event.clientX;
pointerState.y = event.clientY;
updateCamera();
return;
}
if (pointerState.mode === "pan") {
const dx = event.clientX - pointerState.x;
const dy = event.clientY - pointerState.y;
const scale = cameraState.radius * 0.00135;
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 2);
forward.y = 0;
forward.normalize();
cameraState.target
.addScaledVector(right, -dx * scale)
.addScaledVector(forward, dy * scale);
pointerState.x = event.clientX;
pointerState.y = event.clientY;
updateCamera();
return;
}
if (pointerState.mode === "desk") {
const distance = Math.hypot(
event.clientX - pointerState.x,
event.clientY - pointerState.y
);
if (distance > 4) pointerState.moved = true;
if (!pointerState.moved) return;
const point = pointOnFloor(event);
if (!point) return;
const desk = desks.get(pointerState.deskId);
desk.group.position.x = THREE.MathUtils.clamp(
point.x + pointerState.offsetX,
-13.2,
13.2
);
desk.group.position.z = THREE.MathUtils.clamp(
point.z + pointerState.offsetZ,
-8.3,
8.3
);
updateDeskEnvelope(desk);
selectionRing.position.set(desk.group.position.x, 0.09, desk.group.position.z);
renderer.domElement.style.cursor = "grabbing";
const portX = desk.group.position.x;
const portZ = desk.group.position.z - 1.5;
const candidates = slotDefinitions
.filter(slot => !occupiedSlot(slot.id, desk.id))
.map(slot => ({
slot,
distance: Math.hypot(slot.x - portX, slot.z - portZ)
}))
.sort((a, b) => a.distance - b.distance);
setActiveSlot(candidates[0]?.slot.id ?? null);
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (!pointerState) return;
const state = pointerState;
pointerState = null;
setActiveSlot(null);
if (state.mode === "desk") {
const desk = desks.get(state.deskId);
if (state.moved) {
const portX = desk.group.position.x;
const portZ = desk.group.position.z - 1.5;
const candidates = slotDefinitions
.filter(slot => !occupiedSlot(slot.id, desk.id))
.map(slot => ({
slot,
distance: Math.hypot(slot.x - portX, slot.z - portZ)
}))
.sort((a, b) => a.distance - b.distance);
if (candidates.length) {
moveDeskToSlot(desk, candidates[0].slot.id, false);
repairAllRoutes();
toast(`Desk ${desk.index + 1} moved. Routes were checked automatically.`);
} else {
moveDeskToSlot(desk, defaultSlots[desk.id], false);
repairAllRoutes();
}
} else if (routing) {
completeRoute(desk.id);
} else {
selectDesk(desk.id);
}
} else if (state.mode === "outlet" && !state.moved) {
selectOutlet(state.outletId);
} else if (state.mode === "node" && !state.moved) {
appendNode(state.nodeId);
}
renderer.domElement.style.cursor = "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState = null;
setActiveSlot(null);
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
48
);
updateCamera();
}, { passive: false });
// -------------------------------------------------------------------------
// UI buttons
// -------------------------------------------------------------------------
document.getElementById("reset-view").addEventListener("click", () => {
cameraState.theta = 0.72;
cameraState.phi = 0.82;
cameraState.radius = 31;
cameraState.target.set(0, 0, 0);
updateCamera();
});
document.getElementById("top-view").addEventListener("click", () => {
cameraState.theta = 0;
cameraState.phi = 0.08;
cameraState.radius = 30;
cameraState.target.set(0, 0, 0);
updateCamera();
});
document.getElementById("auto-route").addEventListener("click", autoRouteMissing);
document.getElementById("repair-routes").addEventListener("click", () => {
repairAllRoutes();
toast("All routes were checked against restricted areas.");
});
document.getElementById("clear-routes").addEventListener("click", () => {
routes.clear();
clearRouting();
rebuildCables();
updatePanel();
toast("All cable routes cleared.");
});
document.getElementById("reset-scene").addEventListener("click", () => {
routes.clear();
clearRouting();
for (const deskId of deskOrder) {
moveDeskToSlot(desks.get(deskId), defaultSlots[deskId], false);
}
const assignments = [
["D1", "C1"],
["D2", "C2"],
["D3", "C3"],
["D4", "C4"]
];
for (const [deskId, outletId] of assignments) {
const path = findPath(outletId, desks.get(deskId).slotId);
if (path) {
routes.set(deskId, { deskId, outletId, nodes: path, valid: true });
}
}
selectDesk("D1");
rebuildCables();
updatePanel();
toast("Floorplan reset.");
});
// -------------------------------------------------------------------------
// Initial routes and animation
// -------------------------------------------------------------------------
const assignments = [
["D1", "C1"],
["D2", "C2"],
["D3", "C3"],
["D4", "C4"]
];
for (const [deskId, outletId] of assignments) {
const path = findPath(outletId, desks.get(deskId).slotId);
if (path) {
routes.set(deskId, { deskId, outletId, nodes: path, valid: true });
}
}
selectDesk("D1");
rebuildCables();
updatePanel();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
outletRings.forEach((ring, index) => {
const pulse = 1 + Math.sin(time * 2.1 + index) * 0.055;
ring.scale.setScalar(pulse);
});
const selected = desks.get(selectedDeskId);
if (selected) {
selectionRing.position.x = selected.group.position.x;
selectionRing.position.z = selected.group.position.z;
selectionRing.material.opacity = 0.48 + Math.sin(time * 2.4) * 0.12;
}
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```