Prompt specification
Build or customize the Three.js 3D scene "Cross-Connection Detection 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: Cross-Connection Detection Puzzle
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/cross-connection-detection-puzzle-x3996
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: cross-connection detection puzzle. Create a schematic network puzzle containing one explicitly incorrect cross-connection. Let users trace the path and isolate it without presenting the model as real plumbing installation guidance. Return one self-contained HTML file.
Working Three.js Starter (cross-connection-detection-puzzle-x3996):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="dark">
<title>Cross/Check — Network Separation Puzzle</title>
<style>
:root {
--bg: #07131b;
--panel: rgba(9, 24, 33, 0.88);
--panel-strong: rgba(11, 30, 41, 0.96);
--line: rgba(159, 213, 228, 0.16);
--text: #e7f6fa;
--muted: #8da8b2;
--cyan: #43d7e9;
--cyan-soft: rgba(67, 215, 233, 0.14);
--orange: #ff9368;
--gold: #f1cf72;
--danger: #ff617b;
--danger-soft: rgba(255, 97, 123, 0.13);
--success: #79e6ad;
--success-soft: rgba(121, 230, 173, 0.13);
--warning: #f1cf72;
--shadow: 0 18px 55px rgba(0, 0, 0, 0.32);
}
* {
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;
}
button {
font: inherit;
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
z-index: 1;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 25%, rgba(2, 9, 13, 0.25) 100%),
linear-gradient(180deg, rgba(3, 12, 17, 0.08), rgba(3, 12, 17, 0.28));
}
.topbar {
position: fixed;
z-index: 5;
top: 24px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 38px;
height: 38px;
border: 1px solid rgba(67, 215, 233, 0.42);
display: grid;
place-items: center;
background: rgba(7, 24, 33, 0.76);
box-shadow: 0 0 28px rgba(67, 215, 233, 0.08);
transform: rotate(45deg);
}
.brand-mark span {
width: 12px;
height: 12px;
background: var(--cyan);
box-shadow: 0 0 16px rgba(67, 215, 233, 0.75);
}
.brand-copy {
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.4);
}
.eyebrow {
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
color: var(--cyan);
}
.brand h1 {
margin: 4px 0 0;
font-size: clamp(19px, 2vw, 27px);
line-height: 1;
letter-spacing: -0.04em;
font-weight: 760;
}
.top-tag {
border: 1px solid var(--line);
border-radius: 999px;
padding: 8px 12px;
color: var(--muted);
background: rgba(7, 19, 27, 0.72);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 4;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
overflow: hidden;
}
.panel::before {
content: "";
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: 2px;
background: var(--cyan);
opacity: 0.7;
}
.mission-panel {
top: 100px;
left: 28px;
width: 328px;
border-radius: 3px 16px 16px 16px;
}
.candidates-panel {
top: 100px;
right: 28px;
width: 278px;
border-radius: 16px 3px 16px 16px;
}
.panel-head {
padding: 19px 20px 15px;
border-bottom: 1px solid var(--line);
}
.panel-kicker {
color: var(--muted);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.panel-head h2 {
margin: 7px 0 0;
font-size: 18px;
letter-spacing: -0.025em;
line-height: 1.2;
}
.panel-body {
padding: 17px 20px 20px;
}
.brief {
margin: 0;
color: #abc0c8;
font-size: 12.5px;
line-height: 1.65;
}
.brief strong {
color: var(--text);
font-weight: 650;
}
.steps {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 16px 0;
}
.step {
position: relative;
min-height: 52px;
padding: 10px 10px 9px 38px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
font: 700 10px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.step::before {
content: attr(data-number);
position: absolute;
left: 11px;
top: 10px;
width: 18px;
height: 18px;
display: grid;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 50%;
font-size: 9px;
color: var(--muted);
}
.step.active {
color: var(--cyan);
border-color: rgba(67, 215, 233, 0.35);
background: var(--cyan-soft);
}
.step.active::before {
color: var(--cyan);
border-color: var(--cyan);
box-shadow: 0 0 13px rgba(67, 215, 233, 0.25);
}
.step.done {
color: var(--success);
border-color: rgba(121, 230, 173, 0.3);
background: var(--success-soft);
}
.step.done::before {
content: "✓";
color: var(--success);
border-color: var(--success);
}
.status-card {
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.025);
padding: 13px 13px 13px 15px;
display: grid;
grid-template-columns: 8px 1fr;
gap: 11px;
align-items: start;
min-height: 79px;
}
.status-dot {
width: 7px;
height: 7px;
margin-top: 5px;
border-radius: 50%;
background: var(--muted);
box-shadow: 0 0 0 4px rgba(141, 168, 178, 0.08);
}
.status-card.danger .status-dot {
background: var(--danger);
box-shadow: 0 0 0 4px var(--danger-soft), 0 0 14px rgba(255, 97, 123, 0.4);
}
.status-card.success .status-dot {
background: var(--success);
box-shadow: 0 0 0 4px var(--success-soft), 0 0 14px rgba(121, 230, 173, 0.36);
}
.status-card.warning .status-dot {
background: var(--warning);
box-shadow: 0 0 0 4px rgba(241, 207, 114, 0.1);
}
.status-title {
margin: 0 0 5px;
font-size: 12px;
font-weight: 720;
letter-spacing: -0.01em;
}
.status-body {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.48;
}
.progress-wrap {
margin-top: 14px;
}
.progress-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.progress-track {
height: 3px;
overflow: hidden;
background: rgba(255, 255, 255, 0.07);
}
.progress-fill {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--cyan), #a8f3fb);
box-shadow: 0 0 14px rgba(67, 215, 233, 0.45);
transition: width 0.1s linear;
}
.button-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 8px;
margin-top: 14px;
}
.action-button,
.icon-button,
.view-button {
border: 1px solid rgba(67, 215, 233, 0.32);
color: #dffbff;
background: rgba(67, 215, 233, 0.11);
cursor: pointer;
transition: background 0.18s ease, border-color 0.18s ease, transform 0.18s ease;
}
.action-button:hover,
.icon-button:hover,
.view-button:hover {
background: rgba(67, 215, 233, 0.2);
border-color: rgba(67, 215, 233, 0.65);
}
.action-button:active,
.icon-button:active,
.view-button:active {
transform: translateY(1px);
}
.action-button {
min-height: 42px;
padding: 0 14px;
display: flex;
align-items: center;
justify-content: center;
gap: 9px;
border-radius: 4px;
font: 750 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.action-button svg {
width: 15px;
height: 15px;
}
.icon-button {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border-radius: 4px;
}
.icon-button svg {
width: 16px;
height: 16px;
}
.selection-box {
margin-top: 15px;
border-top: 1px solid var(--line);
padding-top: 15px;
}
.selection-label {
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.selection-name {
margin-top: 7px;
font-size: 14px;
font-weight: 720;
}
.selection-meta {
margin-top: 5px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
#isolateButton {
width: 100%;
margin-top: 12px;
border-color: rgba(255, 97, 123, 0.34);
background: rgba(255, 97, 123, 0.1);
color: #ffd9e0;
}
#isolateButton:hover:not(:disabled) {
background: rgba(255, 97, 123, 0.18);
border-color: rgba(255, 97, 123, 0.65);
}
#isolateButton:disabled {
cursor: not-allowed;
opacity: 0.35;
border-color: var(--line);
background: rgba(255, 255, 255, 0.03);
color: var(--muted);
}
.candidate-list {
display: grid;
gap: 8px;
}
.candidate {
width: 100%;
border: 1px solid var(--line);
border-radius: 5px;
padding: 11px 12px;
color: var(--text);
background: rgba(255, 255, 255, 0.025);
text-align: left;
cursor: pointer;
transition: border-color 0.18s ease, background 0.18s ease;
}
.candidate:hover,
.candidate.selected {
border-color: rgba(67, 215, 233, 0.45);
background: rgba(67, 215, 233, 0.07);
}
.candidate.alert {
border-color: rgba(255, 97, 123, 0.58);
background: var(--danger-soft);
animation: alert-pulse 1.4s ease-in-out infinite;
}
.candidate.isolated {
border-color: rgba(121, 230, 173, 0.42);
background: var(--success-soft);
animation: none;
}
@keyframes alert-pulse {
0%, 100% { box-shadow: 0 0 0 rgba(255, 97, 123, 0); }
50% { box-shadow: 0 0 22px rgba(255, 97, 123, 0.12); }
}
.candidate-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.candidate-id {
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.1em;
}
.candidate-state {
color: var(--muted);
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.candidate.alert .candidate-state {
color: var(--danger);
}
.candidate.isolated .candidate-state {
color: var(--success);
}
.candidate-location {
margin-top: 7px;
color: var(--muted);
font-size: 10px;
}
.readout {
margin-top: 15px;
border-top: 1px solid var(--line);
padding-top: 14px;
display: grid;
gap: 9px;
}
.readout-row {
display: flex;
align-items: center;
justify-content: space-between;
color: var(--muted);
font: 650 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.05em;
}
.readout-row strong {
color: var(--text);
font-weight: 750;
}
.circuit-key {
display: inline-flex;
align-items: center;
gap: 7px;
}
.circuit-key::before {
content: "";
width: 17px;
height: 3px;
background: var(--cyan);
box-shadow: 0 0 8px rgba(67, 215, 233, 0.45);
}
.circuit-key.b::before {
background: var(--orange);
box-shadow: 0 0 8px rgba(255, 147, 104, 0.45);
}
.legend {
position: fixed;
z-index: 4;
left: 50%;
bottom: 50px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 18px;
padding: 10px 15px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 19, 27, 0.72);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
color: var(--muted);
font: 650 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.07em;
text-transform: uppercase;
white-space: nowrap;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.legend-swatch {
width: 16px;
height: 3px;
background: var(--cyan);
}
.legend-swatch.orange {
background: var(--orange);
}
.legend-swatch.gold {
background: var(--gold);
}
.legend-swatch.gray {
background: #63717b;
}
.camera-panel {
position: fixed;
z-index: 4;
right: 28px;
bottom: 53px;
display: flex;
gap: 6px;
padding: 6px;
border: 1px solid var(--line);
background: rgba(7, 19, 27, 0.76);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
.view-button {
width: 35px;
height: 34px;
display: grid;
place-items: center;
border-color: transparent;
background: transparent;
border-radius: 3px;
color: var(--muted);
font: 750 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.view-button:hover {
color: var(--text);
}
.disclaimer {
position: fixed;
z-index: 4;
left: 28px;
right: 28px;
bottom: 17px;
display: flex;
justify-content: space-between;
gap: 20px;
color: #708994;
font: 600 9px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
}
.hint {
color: #8ca5ae;
}
#labelLayer {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
padding: 6px 8px;
border: 1px solid rgba(159, 213, 228, 0.18);
border-radius: 3px;
color: #d6e9ee;
background: rgba(5, 17, 24, 0.75);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
font: 750 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
will-change: transform;
}
.world-label.endpoint {
border-color: rgba(67, 215, 233, 0.3);
color: #bff8ff;
}
.world-label.endpoint.b {
border-color: rgba(255, 147, 104, 0.32);
color: #ffd0ba;
}
.world-label.bridge-label {
pointer-events: auto;
cursor: pointer;
border-color: rgba(241, 207, 114, 0.35);
color: #ffe9a9;
}
.world-label.bridge-label:hover,
.world-label.bridge-label.selected {
border-color: var(--cyan);
color: #e9fdff;
background: rgba(8, 31, 40, 0.92);
}
.world-label.bridge-label.alert {
border-color: var(--danger);
color: #ffd9e1;
background: rgba(58, 10, 23, 0.92);
box-shadow: 0 0 18px rgba(255, 97, 123, 0.22);
}
#tooltip {
position: fixed;
z-index: 10;
display: none;
pointer-events: none;
padding: 7px 9px;
border: 1px solid rgba(159, 213, 228, 0.22);
border-radius: 3px;
color: var(--text);
background: rgba(5, 17, 24, 0.94);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.07em;
text-transform: uppercase;
}
#toast {
position: fixed;
z-index: 12;
top: 92px;
left: 50%;
transform: translate(-50%, -14px);
width: min(430px, calc(100vw - 40px));
padding: 15px 18px;
border: 1px solid rgba(121, 230, 173, 0.48);
border-radius: 7px;
color: #ddffec;
background: rgba(12, 48, 37, 0.95);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.38), 0 0 34px rgba(121, 230, 173, 0.12);
text-align: center;
opacity: 0;
pointer-events: none;
transition: opacity 0.3s ease, transform 0.3s ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast strong {
display: block;
margin-bottom: 4px;
font-size: 13px;
}
#toast span {
color: #9acbb1;
font-size: 11px;
}
@media (max-width: 920px) {
.mission-panel {
left: 16px;
width: 292px;
}
.candidates-panel {
right: 16px;
width: 235px;
}
.topbar {
left: 16px;
right: 16px;
}
.legend {
display: none;
}
.camera-panel {
right: 16px;
}
.disclaimer {
left: 16px;
right: 16px;
}
}
@media (max-width: 680px) {
.top-tag,
.candidates-panel,
.hint {
display: none;
}
.mission-panel {
top: auto;
bottom: 42px;
left: 12px;
right: 12px;
width: auto;
max-height: 47vh;
overflow-y: auto;
}
.panel-body {
padding: 14px 16px 16px;
}
.panel-head {
padding: 14px 16px 12px;
}
.brief {
font-size: 11.5px;
}
.steps {
margin: 11px 0;
}
.camera-panel {
top: 82px;
right: 12px;
bottom: auto;
}
.disclaimer {
left: 14px;
right: 14px;
bottom: 12px;
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="vignette"></div>
<div id="labelLayer"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"><span></span></div>
<div class="brand-copy">
<div class="eyebrow">Network separation puzzle</div>
<h1>CROSS / CHECK</h1>
</div>
</div>
<div class="top-tag">Fictional diagnostic simulation · Case 01</div>
</header>
<aside class="panel mission-panel">
<div class="panel-head">
<div class="panel-kicker">Mission objective</div>
<h2>Find the false crossover</h2>
</div>
<div class="panel-body">
<p class="brief">
Circuit A should reach only <strong>Delivery A</strong>. Circuit B must remain dark.
Trace the source, identify the conductive crossover, then <strong>isolate that link</strong>.
</p>
<div class="steps">
<div class="step" id="traceStep" data-number="1">Trace source</div>
<div class="step" id="isolateStep" data-number="2">Isolate link</div>
</div>
<div class="status-card" id="statusCard">
<span class="status-dot"></span>
<div>
<h3 class="status-title" id="statusTitle">Awaiting trace</h3>
<p class="status-body" id="statusBody">
Run the source trace. A healthy route reaches only Delivery A.
</p>
</div>
</div>
<div class="progress-wrap">
<div class="progress-label">
<span>Trace progress</span>
<span id="progressText">00%</span>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
</div>
<div class="button-row">
<button class="action-button" id="traceButton" type="button">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
<path d="M4 12h12"></path>
<path d="m13 7 5 5-5 5"></path>
<path d="M4 6v12"></path>
</svg>
<span id="traceButtonText">Run source trace</span>
</button>
<button class="icon-button" id="resetButton" type="button" title="Reset puzzle" aria-label="Reset puzzle">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
<path d="M4 4v6h6"></path>
<path d="M5.5 15a8 8 0 1 0 .8-8.4L4 10"></path>
</svg>
</button>
</div>
<div class="selection-box">
<div class="selection-label">Selected network element</div>
<div class="selection-name" id="selectionName">Nothing selected</div>
<div class="selection-meta" id="selectionMeta">
Select a pipe or crossover in the schematic.
</div>
<button class="action-button" id="isolateButton" type="button" disabled>
<span id="isolateButtonText">Isolate selected link</span>
</button>
</div>
</div>
</aside>
<aside class="panel candidates-panel">
<div class="panel-head">
<div class="panel-kicker">Crossover candidates</div>
<h2>Three sleeves, one fault</h2>
</div>
<div class="panel-body">
<div class="candidate-list">
<button class="candidate" type="button" data-edge="x1">
<div class="candidate-top">
<span class="candidate-id">X-01</span>
<span class="candidate-state" data-state="x1">Unverified</span>
</div>
<div class="candidate-location">Left crossover bay</div>
</button>
<button class="candidate" type="button" data-edge="x2">
<div class="candidate-top">
<span class="candidate-id">X-02</span>
<span class="candidate-state" data-state="x2">Unverified</span>
</div>
<div class="candidate-location">Center crossover bay</div>
</button>
<button class="candidate" type="button" data-edge="x3">
<div class="candidate-top">
<span class="candidate-id">X-03</span>
<span class="candidate-state" data-state="x3">Unverified</span>
</div>
<div class="candidate-location">Right crossover bay</div>
</button>
</div>
<div class="readout">
<div class="readout-row">
<span class="circuit-key">Circuit A</span>
<strong id="circuitAState">Standby</strong>
</div>
<div class="readout-row">
<span class="circuit-key b">Circuit B</span>
<strong id="circuitBState">Standby</strong>
</div>
<div class="readout-row">
<span>Fault count</span>
<strong>Exactly one</strong>
</div>
</div>
</div>
</aside>
<div class="legend" aria-hidden="true">
<div class="legend-item"><span class="legend-swatch"></span>Circuit A</div>
<div class="legend-item"><span class="legend-swatch orange"></span>Circuit B</div>
<div class="legend-item"><span class="legend-swatch gold"></span>Crossover</div>
<div class="legend-item"><span class="legend-swatch gray"></span>Isolated</div>
</div>
<div class="camera-panel" aria-label="Camera controls">
<button class="view-button" type="button" data-view="iso" title="Isometric view">ISO</button>
<button class="view-button" type="button" data-view="top" title="Top view">TOP</button>
<button class="view-button" type="button" data-view="front" title="Front view">FRT</button>
<button class="view-button" type="button" data-view="zoomIn" title="Zoom in">+</button>
<button class="view-button" type="button" data-view="zoomOut" title="Zoom out">−</button>
</div>
<footer class="disclaimer">
<span>Fictional schematic only · Not plumbing, electrical, or installation guidance</span>
<span class="hint">Drag to orbit · Scroll to zoom · T trace · I isolate · R reset</span>
</footer>
<div id="tooltip"></div>
<div id="toast" role="status" aria-live="polite">
<strong>Cross-connection isolated</strong>
<span>The two circuits are now independent in this fictional model.</span>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const labelLayer = document.getElementById("labelLayer");
const tooltip = document.getElementById("tooltip");
const toast = document.getElementById("toast");
const statusCard = document.getElementById("statusCard");
const statusTitle = document.getElementById("statusTitle");
const statusBody = document.getElementById("statusBody");
const traceButton = document.getElementById("traceButton");
const traceButtonText = document.getElementById("traceButtonText");
const resetButton = document.getElementById("resetButton");
const isolateButton = document.getElementById("isolateButton");
const isolateButtonText = document.getElementById("isolateButtonText");
const selectionName = document.getElementById("selectionName");
const selectionMeta = document.getElementById("selectionMeta");
const progressFill = document.getElementById("progressFill");
const progressText = document.getElementById("progressText");
const traceStep = document.getElementById("traceStep");
const isolateStep = document.getElementById("isolateStep");
const circuitAState = document.getElementById("circuitAState");
const circuitBState = document.getElementById("circuitBState");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131b);
scene.fog = new THREE.Fog(0x07131b, 18, 38);
const camera = new THREE.PerspectiveCamera(40, innerWidth / innerHeight, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xaeefff, 0x142028, 1.6);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xdff8ff, 3.1);
keyLight.position.set(4, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xff9b73, 1.2);
rimLight.position.set(-8, 2, -6);
scene.add(rimLight);
const cyanLight = new THREE.PointLight(0x39d7ea, 16, 13, 2);
cyanLight.position.set(-5.5, 2.8, 2.4);
scene.add(cyanLight);
const orangeLight = new THREE.PointLight(0xff8656, 12, 12, 2);
orangeLight.position.set(5.4, -2.7, 2.1);
scene.add(orangeLight);
const world = new THREE.Group();
scene.add(world);
const cameraState = {
yaw: 0.48,
pitch: 0.48,
distance: 16.5,
target: new THREE.Vector3(0, -0.15, 0)
};
function updateCamera() {
const { yaw, pitch, distance, target } = cameraState;
const horizontal = Math.cos(pitch) * distance;
camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * distance,
target.z + Math.cos(yaw) * horizontal
);
camera.lookAt(target);
}
function setCameraView(view) {
if (view === "iso") {
cameraState.yaw = 0.48;
cameraState.pitch = 0.48;
cameraState.distance = 16.5;
} else if (view === "top") {
cameraState.yaw = 0;
cameraState.pitch = 1.15;
cameraState.distance = 16;
} else if (view === "front") {
cameraState.yaw = 0;
cameraState.pitch = 0.04;
cameraState.distance = 15.5;
} else if (view === "zoomIn") {
cameraState.distance = Math.max(10.5, cameraState.distance - 1.4);
} else if (view === "zoomOut") {
cameraState.distance = Math.min(23, cameraState.distance + 1.4);
}
updateCamera();
}
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(8.3, 8.3, 0.28, 6),
new THREE.MeshStandardMaterial({
color: 0x0d2530,
roughness: 0.78,
metalness: 0.18,
flatShading: true
})
);
platform.scale.z = 0.64;
platform.position.y = -3.43;
platform.receiveShadow = true;
world.add(platform);
const grid = new THREE.GridHelper(16, 16, 0x2b7584, 0x1c4d58);
grid.position.y = -3.275;
grid.material.transparent = true;
grid.material.opacity = 0.18;
world.add(grid);
const nodeDefs = {
S: { p: [-6.0, 2.25, 0], type: "A", label: "Source A" },
A1: { p: [-3.8, 2.25, 0], type: "A" },
A2: { p: [-1.6, 2.25, 0], type: "A" },
A3: { p: [ 0.6, 2.25, 0], type: "A" },
A4: { p: [ 2.8, 2.25, 0], type: "A" },
D: { p: [ 5.0, 2.25, 0], type: "A", label: "Delivery A" },
B0: { p: [-6.0, -2.25, 0], type: "B", label: "Return B" },
B1: { p: [-3.8, -2.25, 0], type: "B" },
B2: { p: [-1.6, -2.25, 0], type: "B" },
B3: { p: [ 0.6, -2.25, 0], type: "B" },
B4: { p: [ 2.8, -2.25, 0], type: "B" },
B5: { p: [ 5.0, -2.25, 0], type: "B", label: "Collect B" }
};
const nodePositions = {};
const nodeMeshes = {};
function addNode(key, def) {
const position = new THREE.Vector3(...def.p);
nodePositions[key] = position;
const isEndpoint = Boolean(def.label);
const geometry = isEndpoint
? new THREE.OctahedronGeometry(0.38, 0)
: new THREE.IcosahedronGeometry(0.25, 0);
const color = def.type === "A" ? 0x43d7e9 : 0xff9368;
const material = new THREE.MeshStandardMaterial({
color,
emissive: new THREE.Color(color).multiplyScalar(0.16),
emissiveIntensity: 0.9,
roughness: 0.35,
metalness: 0.28,
flatShading: true
});
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
world.add(mesh);
nodeMeshes[key] = mesh;
const ring = new THREE.Mesh(
new THREE.TorusGeometry(isEndpoint ? 0.52 : 0.34, 0.025, 6, 28),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: isEndpoint ? 0.55 : 0.24
})
);
ring.position.copy(position);
ring.rotation.y = Math.PI / 2;
world.add(ring);
}
Object.entries(nodeDefs).forEach(([key, def]) => addNode(key, def));
const edges = new Map();
const edgePickMeshes = [];
const labelItems = [];
function makeEdgeMaterial(color) {
return new THREE.MeshStandardMaterial({
color,
emissive: new THREE.Color(color).multiplyScalar(0.11),
emissiveIntensity: 0.75,
roughness: 0.43,
metalness: 0.2,
flatShading: true,
transparent: true,
opacity: 1
});
}
function createPipeSegment(edge, start, end, radius = 0.13) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(radius, radius, length, 8, 1, false);
const mesh = new THREE.Mesh(geometry, edge.material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.userData.edgeId = edge.id;
world.add(mesh);
edgePickMeshes.push(mesh);
edge.meshes.push(mesh);
const glowGeometry = new THREE.CylinderGeometry(radius * 1.52, radius * 1.52, length, 8, 1, false);
const glow = new THREE.Mesh(glowGeometry, edge.glowMaterial);
glow.position.copy(mesh.position);
glow.quaternion.copy(mesh.quaternion);
glow.visible = false;
glow.renderOrder = 3;
world.add(glow);
edge.glows.push(glow);
}
function addIsolationMarker(edge, midpoint) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({ color: 0xff6f87 });
for (const rotation of [Math.PI / 4, -Math.PI / 4]) {
const bar = new THREE.Mesh(new THREE.BoxGeometry(0.42, 0.065, 0.065), material);
bar.rotation.z = rotation;
group.add(bar);
}
group.position.copy(midpoint);
group.position.z += 0.16;
group.visible = false;
world.add(group);
edge.isolationMarker = group;
}
function registerEdge(def) {
const material = makeEdgeMaterial(def.color);
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.16,
depthWrite: false
});
const edge = {
...def,
baseColor: new THREE.Color(def.color),
material,
glowMaterial,
meshes: [],
glows: [],
isolationMarker: null,
conductive: def.kind !== "sleeve"
};
edges.set(edge.id, edge);
return edge;
}
const mainEdges = [
{ id: "a1", name: "A-01", from: "S", to: "A1", circuit: "A", color: 0x43d7e9 },
{ id: "a2", name: "A-02", from: "A1", to: "A2", circuit: "A", color: 0x43d7e9 },
{ id: "a3", name: "A-03", from: "A2", to: "A3", circuit: "A", color: 0x43d7e9 },
{ id: "a4", name: "A-04", from: "A3", to: "A4", circuit: "A", color: 0x43d7e9 },
{ id: "a5", name: "A-05", from: "A4", to: "D", circuit: "A", color: 0x43d7e9 },
{ id: "b1", name: "B-01", from: "B0", to: "B1", circuit: "B", color: 0xff9368 },
{ id: "b2", name: "B-02", from: "B1", to: "B2", circuit: "B", color: 0xff9368 },
{ id: "b3", name: "B-03", from: "B2", to: "B3", circuit: "B", color: 0xff9368 },
{ id: "b4", name: "B-04", from: "B3", to: "B4", circuit: "B", color: 0xff9368 },
{ id: "b5", name: "B-05", from: "B4", to: "B5", circuit: "B", color: 0xff9368 }
];
for (const def of mainEdges) {
const edge = registerEdge({ ...def, kind: "main" });
createPipeSegment(edge, nodePositions[def.from], nodePositions[def.to]);
addIsolationMarker(edge, nodePositions[def.from].clone().lerp(nodePositions[def.to], 0.5));
}
const bridgeDefs = [
{ id: "x1", name: "X-01", from: "A2", to: "B2", color: 0x8998a2, conductive: false, bay: "Left" },
{ id: "x2", name: "X-02", from: "A3", to: "B3", color: 0xf1cf72, conductive: true, bay: "Center" },
{ id: "x3", name: "X-03", from: "A4", to: "B4", color: 0x8998a2, conductive: false, bay: "Right" }
];
for (const def of bridgeDefs) {
const edge = registerEdge({
...def,
kind: def.conductive ? "bridge" : "sleeve",
circuit: "X"
});
const top = nodePositions[def.from];
const bottom = nodePositions[def.to];
if (def.conductive) {
createPipeSegment(edge, top, bottom, 0.145);
addIsolationMarker(edge, top.clone().lerp(bottom, 0.5));
for (const point of [top, bottom]) {
const collar = new THREE.Mesh(
new THREE.TorusGeometry(0.245, 0.038, 6, 22),
new THREE.MeshStandardMaterial({
color: 0xf1cf72,
emissive: 0x5d4310,
roughness: 0.4,
metalness: 0.35,
flatShading: true
})
);
collar.position.copy(point);
collar.rotation.y = Math.PI / 2;
collar.castShadow = true;
world.add(collar);
}
} else {
const gap = 0.34;
const start = new THREE.Vector3(top.x, top.y - gap, top.z);
const end = new THREE.Vector3(bottom.x, bottom.y + gap, bottom.z);
createPipeSegment(edge, start, end, 0.115);
for (const point of [start, end]) {
const cap = new THREE.Mesh(
new THREE.TorusGeometry(0.17, 0.027, 6, 18),
new THREE.MeshStandardMaterial({
color: 0x9aa8b0,
roughness: 0.56,
metalness: 0.2,
flatShading: true
})
);
cap.position.copy(point);
cap.rotation.y = Math.PI / 2;
world.add(cap);
}
}
}
function addArrow(edge, fraction = 0.57) {
if (edge.circuit !== "A") return;
const from = nodePositions[edge.from];
const to = nodePositions[edge.to];
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.115, 0.32, 6),
new THREE.MeshStandardMaterial({
color: 0xc9fbff,
emissive: 0x43d7e9,
emissiveIntensity: 0.8,
roughness: 0.35,
flatShading: true
})
);
arrow.position.copy(from).lerp(to, fraction);
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3().subVectors(to, from).normalize()
);
world.add(arrow);
}
mainEdges.filter(def => def.circuit === "A").forEach(def => addArrow(edges.get(def.id)));
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x31434c,
roughness: 0.8,
metalness: 0.2,
flatShading: true
});
for (const key of ["S", "D", "B0", "B5", "A3", "B3"]) {
const position = nodePositions[key];
const height = position.y + 3.22;
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.07, height, 6),
supportMaterial
);
post.position.set(position.x, -3.22 + height / 2, position.z);
post.castShadow = true;
world.add(post);
}
const pulse = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.16, 1),
new THREE.MeshBasicMaterial({ color: 0xe9fdff })
);
pulse.visible = false;
world.add(pulse);
const pulseHalo = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.28, 1),
new THREE.MeshBasicMaterial({
color: 0x43d7e9,
wireframe: true,
transparent: true,
opacity: 0.52
})
);
pulse.add(pulseHalo);
const pulseLight = new THREE.PointLight(0x43d7e9, 0, 2.6, 2);
world.add(pulseLight);
const warningHalos = [];
for (const key of ["A3", "B3"]) {
const halo = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.47, 1),
new THREE.MeshBasicMaterial({
color: 0xff617b,
wireframe: true,
transparent: true,
opacity: 0
})
);
halo.position.copy(nodePositions[key]);
world.add(halo);
warningHalos.push(halo);
}
function createWorldLabel(html, position, className = "", edgeId = null) {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.innerHTML = html;
labelLayer.appendChild(element);
if (edgeId) {
element.addEventListener("click", event => {
event.stopPropagation();
selectEdge(edgeId);
});
}
labelItems.push({ element, position: position.clone() });
return element;
}
createWorldLabel(
"Source A",
nodePositions.S.clone().add(new THREE.Vector3(0, 0.66, 0)),
"endpoint"
);
createWorldLabel(
"Delivery A",
nodePositions.D.clone().add(new THREE.Vector3(0, 0.66, 0)),
"endpoint"
);
createWorldLabel(
"Return B",
nodePositions.B0.clone().add(new THREE.Vector3(0, -0.66, 0)),
"endpoint b"
);
createWorldLabel(
"Collect B",
nodePositions.B5.clone().add(new THREE.Vector3(0, -0.66, 0)),
"endpoint b"
);
const bridgeLabels = {};
for (const def of bridgeDefs) {
const midpoint = nodePositions[def.from].clone().lerp(nodePositions[def.to], 0.5);
midpoint.z = 0.38;
const label = createWorldLabel(def.name, midpoint, "bridge-label", def.id);
bridgeLabels[def.id] = label;
}
const state = {
selected: null,
hovered: null,
isolated: new Set(),
contamination: false,
solved: false,
visited: new Set(),
trace: {
running: false,
index: 0,
t: 0,
duration: 0.72,
sequence: []
}
};
const topSequence = [
{ edge: "a1", from: "S", to: "A1" },
{ edge: "a2", from: "A1", to: "A2" },
{ edge: "a3", from: "A2", to: "A3" },
{ edge: "a4", from: "A3", to: "A4" },
{ edge: "a5", from: "A4", to: "D" }
];
const fullSequence = [
{ edge: "a1", from: "S", to: "A1" },
{ edge: "a2", from: "A1", to: "A2" },
{ edge: "a3", from: "A2", to: "A3" },
{ edge: "x2", from: "A3", to: "B3", alert: true },
{ edge: "b3", from: "B3", to: "B2", alert: true },
{ edge: "b2", from: "B2", to: "B1", alert: true },
{ edge: "b1", from: "B1", to: "B0", alert: true },
{ edge: "b4", from: "B3", to: "B4", alert: true },
{ edge: "b5", from: "B4", to: "B5", alert: true },
{ edge: "a4", from: "A3", to: "A4" },
{ edge: "a5", from: "A4", to: "D" }
];
function setStatus(title, body, tone = "neutral") {
statusTitle.textContent = title;
statusBody.textContent = body;
statusCard.classList.remove("danger", "success", "warning");
if (tone !== "neutral") statusCard.classList.add(tone);
}
function updateCandidatePanel() {
for (const def of bridgeDefs) {
const card = document.querySelector(`.candidate[data-edge="${def.id}"]`);
const stateLabel = card.querySelector(".candidate-state");
card.classList.remove("alert", "isolated", "selected");
stateLabel.textContent = "Unverified";
if (state.isolated.has(def.id)) {
stateLabel.textContent = "Isolated";
card.classList.add("isolated");
} else if (state.contamination && def.id === "x2") {
stateLabel.textContent = "Conducting";
card.classList.add("alert");
} else if (state.contamination) {
stateLabel.textContent = "No signal";
}
if (state.selected === def.id) card.classList.add("selected");
bridgeLabels[def.id].classList.toggle("alert", state.contamination && def.id === "x2" && !state.solved);
bridgeLabels[def.id].classList.toggle("selected", state.selected === def.id);
}
}
function updateSelectionPanel() {
if (!state.selected) {
selectionName.textContent = "Nothing selected";
selectionMeta.textContent = "Select a pipe or crossover in the schematic.";
isolateButton.disabled = true;
isolateButtonText.textContent = "Isolate selected link";
return;
}
const edge = edges.get(state.selected);
const isolated = state.isolated.has(edge.id);
selectionName.textContent = `${edge.name} · ${edge.kind === "main" ? "Circuit link" : "Crossover sleeve"}`;
if (edge.kind === "sleeve") {
selectionMeta.textContent = "Separated sleeve with no conductive through-path in this model.";
isolateButton.disabled = true;
isolateButtonText.textContent = "No conductive link";
} else if (edge.id === "x2") {
selectionMeta.textContent = isolated
? "Incorrect cross-connection X-02 is isolated. The source and return paths are independent."
: "Potentially conductive crossover between center junction A3 and junction B3.";
isolateButton.disabled = false;
isolateButtonText.textContent = isolated ? "Restore X-02 link" : "Isolate X-02 cross-link";
} else {
selectionMeta.textContent = isolated
? "This normal circuit segment is currently isolated."
: "Primary circuit segment. A healthy source trace should pass through it.";
isolateButton.disabled = false;
isolateButtonText.textContent = isolated ? "Restore circuit link" : "Isolate circuit link";
}
}
function selectEdge(id) {
state.selected = id;
updateSelectionPanel();
updateCandidatePanel();
}
function clearTraceVisuals() {
state.visited.clear();
state.contamination = false;
state.trace.running = false;
state.trace.index = 0;
state.trace.t = 0;
pulse.visible = false;
pulseLight.intensity = 0;
progressFill.style.width = "0%";
progressText.textContent = "00%";
traceButtonText.textContent = "Run source trace";
traceStep.classList.remove("active");
circuitAState.textContent = "Standby";
circuitBState.textContent = "Standby";
warningHalos.forEach(halo => halo.material.opacity = 0);
updateCandidatePanel();
}
function startTrace() {
if (state.trace.running) {
stopTrace();
return;
}
clearTraceVisuals();
state.trace.running = true;
state.trace.index = 0;
state.trace.t = 0;
state.trace.sequence = state.isolated.has("x2")
? [...topSequence]
: fullSequence.map(step => ({ ...step }));
traceButtonText.textContent = "Stop trace";
traceStep.classList.add("active");
setStatus(
"Tracing from Source A",
"The pulse is following the connected source route.",
"neutral"
);
circuitAState.textContent = "Tracing";
}
function stopTrace() {
state.trace.running = false;
pulse.visible = false;
pulseLight.intensity = 0;
traceButtonText.textContent = "Run source trace";
traceStep.classList.remove("active");
setStatus(
"Trace paused",
"Run the source trace again when ready.",
"warning"
);
}
function finishTrace() {
state.trace.running = false;
pulse.visible = false;
pulseLight.intensity = 0;
traceButtonText.textContent = "Run source trace";
traceStep.classList.remove("active");
traceStep.classList.add("done");
if (state.contamination) {
circuitAState.textContent = "Complete";
circuitBState.textContent = "Contaminated";
setStatus(
"Cross-circuit path confirmed",
"Circuit B lit through crossover X-02. Select that crossover and isolate the link.",
"danger"
);
} else {
circuitAState.textContent = "Complete";
circuitBState.textContent = "Dark";
setStatus(
"Circuits remained separate",
"The source reached Delivery A without entering Circuit B.",
"success"
);
}
updateCandidatePanel();
}
function updateTrace(delta) {
if (!state.trace.running) return;
const sequence = state.trace.sequence;
if (state.trace.index >= sequence.length) {
finishTrace();
return;
}
const step = sequence[state.trace.index];
const edge = edges.get(step.edge);
if (state.isolated.has(edge.id)) {
state.trace.running = false;
traceButtonText.textContent = "Run source trace";
traceStep.classList.remove("active");
setStatus(
"Trace halted at isolation",
`The route stops at ${edge.name}. Restore it or reset the puzzle to continue.`,
"warning"
);
return;
}
state.trace.t += delta / state.trace.duration;
const from = nodePositions[step.from];
const to = nodePositions[step.to];
const position = from.clone().lerp(to, Math.min(1, state.trace.t));
pulse.visible = true;
pulse.position.copy(position);
pulseHalo.rotation.y += 0.08;
pulseHalo.rotation.x += 0.04;
pulseLight.position.copy(position);
pulseLight.color.set(step.alert || state.contamination ? 0xff617b : 0x43d7e9);
pulseLight.intensity = 2.2;
if (state.trace.t >= 1) {
state.visited.add(edge.id);
state.trace.index += 1;
state.trace.t = 0;
if (edge.id === "x2") {
state.contamination = true;
circuitBState.textContent = "Contaminated";
selectEdge("x2");
setStatus(
"Unexpected branch detected",
"The trace crossed into Circuit B at X-02. This crossover is the incorrect cross-connection.",
"danger"
);
updateCandidatePanel();
}
if (state.trace.index >= sequence.length) {
finishTrace();
}
}
const progress = Math.min(100, ((state.trace.index + state.trace.t) / sequence.length) * 100);
progressFill.style.width = `${progress}%`;
progressText.textContent = `${Math.floor(progress).toString().padStart(2, "0")}%`;
}
function toggleIsolation(id) {
const edge = edges.get(id);
if (!edge || !edge.conductive) return;
if (state.isolated.has(id)) {
state.isolated.delete(id);
if (id === "x2") {
state.solved = false;
isolateStep.classList.remove("done");
setStatus(
"X-02 restored",
"The false crossover is active again. Isolate it to complete the puzzle.",
"warning"
);
} else {
setStatus(
"Circuit link restored",
"The normal segment is back in service.",
"neutral"
);
}
} else {
state.isolated.add(id);
if (id === "x2") {
state.solved = true;
isolateStep.classList.add("done");
setStatus(
"Cross-connection isolated",
"Incorrect link X-02 is disconnected. The source and return paths remain independent.",
"success"
);
toast.classList.add("show");
window.setTimeout(() => toast.classList.remove("show"), 4200);
} else {
setStatus(
"Normal circuit segment isolated",
`${edge.name} is not the fault. Restore this link or reset the puzzle.`,
"warning"
);
}
}
updateSelectionPanel();
updateCandidatePanel();
}
function resetPuzzle() {
state.isolated.clear();
state.solved = false;
state.selected = null;
clearTraceVisuals();
traceStep.classList.remove("done", "active");
isolateStep.classList.remove("done", "active");
updateSelectionPanel();
updateCandidatePanel();
setStatus(
"Awaiting trace",
"Run the source trace. A healthy route reaches only Delivery A.",
"neutral"
);
}
function updateEdgeVisuals(time) {
for (const edge of edges.values()) {
const isolated = state.isolated.has(edge.id);
const selected = state.selected === edge.id;
const hovered = state.hovered === edge.id;
const active = state.trace.running &&
state.trace.sequence[state.trace.index]?.edge === edge.id;
const visited = state.visited.has(edge.id);
let color = edge.baseColor;
let emissive = edge.baseColor;
let intensity = 0.75;
if (isolated) {
color = new THREE.Color(0x58656e);
emissive = new THREE.Color(0x253038);
intensity = 0.3;
} else if (active) {
emissive = new THREE.Color(
state.contamination || edge.circuit === "B" ? 0xff617b : 0x8cf4ff
);
intensity = 1.65;
} else if (visited && state.contamination && (edge.circuit === "B" || edge.id === "x2")) {
emissive = new THREE.Color(0xff617b);
intensity = 1.05;
} else if (visited) {
emissive = edge.baseColor;
intensity = 1.05;
} else if (selected || hovered) {
emissive = new THREE.Color(0xecfbff);
intensity = 0.95;
}
edge.material.color.copy(color);
edge.material.emissive.copy(emissive);
edge.material.emissiveIntensity = intensity;
edge.material.opacity = isolated ? 0.42 : 1;
if (edge.isolationMarker) {
edge.isolationMarker.visible = isolated;
edge.isolationMarker.rotation.z = time * 0.0012;
}
const glowVisible = !isolated && (selected || hovered || active);
edge.glowMaterial.opacity = active ? 0.25 : selected || hovered ? 0.17 : 0;
edge.glowMaterial.color.set(
active && (state.contamination || edge.circuit === "B") ? 0xff617b : 0xbdf8ff
);
for (const glow of edge.glows) glow.visible = glowVisible;
}
}
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerDown = { active: false, x: 0, y: 0, moved: false };
let lastPointerX = 0;
let lastPointerY = 0;
function pickEdge(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);
const hits = raycaster.intersectObjects(edgePickMeshes, false);
return hits.length ? hits[0].object.userData.edgeId : null;
}
renderer.domElement.addEventListener("pointerdown", event => {
pointerDown.active = true;
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
pointerDown.moved = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (pointerDown.active) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (Math.hypot(event.clientX - pointerDown.x, event.clientY - pointerDown.y) > 4) {
pointerDown.moved = true;
renderer.domElement.classList.add("dragging");
}
if (pointerDown.moved) {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(cameraState.pitch + dy * 0.0045, 0.06, 1.22);
updateCamera();
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
return;
}
const hit = pickEdge(event);
state.hovered = hit;
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
if (hit) {
const edge = edges.get(hit);
tooltip.style.display = "block";
tooltip.style.left = `${event.clientX + 14}px`;
tooltip.style.top = `${event.clientY + 14}px`;
tooltip.textContent = edge.kind === "sleeve"
? `${edge.name} · separated sleeve`
: `${edge.name} · ${edge.kind}`;
} else {
tooltip.style.display = "none";
}
});
renderer.domElement.addEventListener("pointerup", event => {
if (pointerDown.active && !pointerDown.moved) {
const hit = pickEdge(event);
if (hit) selectEdge(hit);
}
pointerDown.active = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointerleave", () => {
state.hovered = null;
tooltip.style.display = "none";
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.008,
10.5,
23
);
updateCamera();
}, { passive: false });
traceButton.addEventListener("click", startTrace);
resetButton.addEventListener("click", resetPuzzle);
isolateButton.addEventListener("click", () => {
if (state.selected) toggleIsolation(state.selected);
});
document.querySelectorAll(".candidate").forEach(card => {
card.addEventListener("click", () => selectEdge(card.dataset.edge));
});
document.querySelectorAll(".view-button").forEach(button => {
button.addEventListener("click", () => setCameraView(button.dataset.view));
});
window.addEventListener("keydown", event => {
if (event.target instanceof HTMLButtonElement && event.key === " ") return;
const key = event.key.toLowerCase();
if (key === "t") startTrace();
if (key === "r") resetPuzzle();
if (key === "i" && state.selected) toggleIsolation(state.selected);
if (key === "escape") {
state.selected = null;
updateSelectionPanel();
updateCandidatePanel();
}
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
function updateLabels() {
for (const item of labelItems) {
const projected = item.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
item.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
item.element.style.transform = `translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
}
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateTrace(delta);
updateEdgeVisuals(time);
updateLabels();
if (state.contamination && !state.solved) {
warningHalos.forEach((halo, index) => {
halo.material.opacity = 0.36 + Math.sin(time * 4 + index) * 0.16;
halo.rotation.y += 0.004;
halo.rotation.x += 0.002;
});
}
if (pulse.visible) {
pulseHalo.rotation.y += 0.12;
pulseHalo.rotation.x += 0.07;
}
renderer.render(scene, camera);
}
updateCamera();
updateSelectionPanel();
updateCandidatePanel();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```