Prompt specification
Build or customize the Three.js 3D scene "Single Bridge Network Vulnerability" 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: Single Bridge Network Vulnerability
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/single-bridge-network-vulnerability-x3588
Scene Description Prompt:
Make a voxel region-building puzzle in standalone HTML with Three.js from cdnjs; remove a critical bridge and budget for alternate links.
Working Three.js Starter (single-bridge-network-vulnerability-x3588):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Faultline — Voxel Relay Puzzle</title>
<style>
:root {
--bg: #07121d;
--panel: rgba(10, 24, 37, 0.88);
--panel-strong: rgba(8, 20, 31, 0.96);
--line: rgba(153, 220, 237, 0.16);
--text: #eaf8fb;
--muted: #89aab5;
--cyan: #50e3c2;
--cyan-dark: #123e3d;
--orange: #ffb15c;
--red: #ff617c;
--purple: #ba9cff;
--shadow: 0 18px 55px rgba(0, 0, 0, 0.38);
}
* {
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: 2px;
}
[hidden] {
display: none !important;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(255, 255, 255, 0.035), transparent 42%),
var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
#mission {
top: 20px;
left: 20px;
width: min(370px, calc(100vw - 40px));
padding: 20px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1,
h2,
p {
margin: 0;
}
h1 {
font-size: clamp(26px, 4vw, 38px);
line-height: 0.98;
letter-spacing: -0.055em;
}
h2 {
font-size: 17px;
letter-spacing: -0.02em;
}
.mission-copy {
margin-top: 12px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.phase-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin: 17px 0 12px;
}
.phase-pill {
display: inline-flex;
align-items: center;
min-height: 27px;
padding: 0 10px;
border: 1px solid rgba(80, 227, 194, 0.25);
border-radius: 999px;
background: rgba(80, 227, 194, 0.08);
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.link-status {
color: var(--muted);
font-size: 12px;
text-align: right;
}
.link-status.connected {
color: var(--cyan);
}
.objectives {
display: grid;
gap: 8px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.objective {
display: grid;
grid-template-columns: 25px 1fr;
gap: 10px;
align-items: center;
color: var(--muted);
font-size: 12px;
}
.objective-number {
display: grid;
place-items: center;
width: 24px;
height: 24px;
border: 1px solid var(--line);
border-radius: 8px;
color: #bcd1d8;
font-size: 11px;
font-weight: 800;
}
.objective.done {
color: var(--text);
}
.objective.done .objective-number {
border-color: rgba(80, 227, 194, 0.35);
background: rgba(80, 227, 194, 0.13);
color: var(--cyan);
}
.mission-note {
margin-top: 13px;
padding: 10px 11px;
border-left: 2px solid var(--orange);
border-radius: 0 8px 8px 0;
background: rgba(255, 177, 92, 0.075);
color: #c7b9a9;
font-size: 11px;
line-height: 1.45;
}
#build-panel {
top: 20px;
right: 20px;
width: min(324px, calc(100vw - 40px));
padding: 18px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.tiny-tag {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.cut-button {
width: 100%;
min-height: 48px;
border: 1px solid rgba(255, 97, 124, 0.38);
border-radius: 12px;
background:
linear-gradient(180deg, rgba(255, 97, 124, 0.18), rgba(255, 97, 124, 0.08));
color: #ff9bad;
cursor: pointer;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: 160ms ease;
}
.cut-button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(255, 97, 124, 0.7);
background: rgba(255, 97, 124, 0.2);
color: #ffd2da;
}
.cut-button:disabled {
border-color: rgba(80, 227, 194, 0.24);
background: rgba(80, 227, 194, 0.08);
color: var(--cyan);
cursor: default;
}
.control-section {
margin-top: 14px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.control-label {
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.budget-card {
display: flex;
align-items: end;
justify-content: space-between;
gap: 15px;
}
.budget-number {
color: var(--cyan);
font-size: 31px;
font-weight: 850;
line-height: 1;
letter-spacing: -0.05em;
}
.budget-number small {
color: var(--muted);
font-size: 12px;
font-weight: 650;
letter-spacing: 0;
}
.budget-caption {
color: var(--muted);
font-size: 11px;
text-align: right;
}
.budget-track {
height: 5px;
margin-top: 11px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.075);
}
.budget-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), #9df5d8);
box-shadow: 0 0 14px rgba(80, 227, 194, 0.5);
transition: width 180ms ease;
}
.budget-fill.low {
background: linear-gradient(90deg, var(--orange), var(--red));
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 4px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(0, 0, 0, 0.16);
}
.segmented button,
.layer-button,
.small-button {
min-height: 35px;
border: 1px solid transparent;
border-radius: 8px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: 140ms ease;
}
.segmented button:hover:not(:disabled),
.layer-button:hover:not(:disabled),
.small-button:hover:not(:disabled) {
color: var(--text);
background: rgba(255, 255, 255, 0.055);
}
.segmented button.active {
border-color: rgba(80, 227, 194, 0.2);
background: rgba(80, 227, 194, 0.12);
color: var(--cyan);
}
button:disabled {
opacity: 0.34;
cursor: not-allowed;
}
.layer-row {
display: grid;
grid-template-columns: 38px 1fr 38px;
gap: 7px;
}
.layer-button {
border-color: var(--line);
background: rgba(0, 0, 0, 0.13);
}
.layer-value {
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--text);
background: rgba(0, 0, 0, 0.13);
font-size: 11px;
font-weight: 800;
text-align: center;
}
.layer-value span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
}
.readout {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 10px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.readout strong {
color: var(--text);
font-weight: 700;
}
.action-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.small-button {
border-color: var(--line);
background: rgba(255, 255, 255, 0.025);
}
.small-button.accent {
border-color: rgba(80, 227, 194, 0.2);
color: var(--cyan);
}
#help {
position: fixed;
z-index: 8;
left: 50%;
bottom: 18px;
display: flex;
align-items: center;
gap: 17px;
max-width: calc(100vw - 40px);
padding: 10px 15px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 18, 29, 0.78);
box-shadow: 0 10px 32px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(14px);
color: var(--muted);
font-size: 10px;
white-space: nowrap;
transform: translateX(-50%);
}
#help b {
color: var(--text);
font-weight: 750;
}
.help-separator {
width: 1px;
height: 14px;
background: var(--line);
}
.world-label {
position: fixed;
z-index: 4;
padding: 6px 9px;
border: 1px solid rgba(80, 227, 194, 0.25);
border-radius: 7px;
background: rgba(5, 18, 26, 0.76);
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.22);
color: var(--cyan);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
#target-label {
border-color: rgba(186, 156, 255, 0.3);
color: var(--purple);
}
#bridge-label {
border-color: rgba(255, 97, 124, 0.32);
color: #ff9bad;
}
#toast {
position: fixed;
z-index: 40;
left: 50%;
top: 24px;
max-width: min(460px, calc(100vw - 40px));
padding: 11px 15px;
border: 1px solid rgba(80, 227, 194, 0.22);
border-radius: 10px;
background: rgba(7, 22, 31, 0.94);
box-shadow: var(--shadow);
color: #dff9f5;
font-size: 12px;
line-height: 1.4;
pointer-events: none;
opacity: 0;
transform: translate(-50%, -10px);
transition: 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.warning {
border-color: rgba(255, 177, 92, 0.35);
color: #ffe0bc;
}
#toast.error {
border-color: rgba(255, 97, 124, 0.4);
color: #ffd0d8;
}
#win-overlay {
position: fixed;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
padding: 20px;
background: rgba(2, 9, 15, 0.68);
backdrop-filter: blur(9px);
}
.win-card {
width: min(430px, 100%);
padding: 28px;
border: 1px solid rgba(80, 227, 194, 0.28);
border-radius: 22px;
background:
radial-gradient(circle at 50% 0%, rgba(80, 227, 194, 0.13), transparent 45%),
var(--panel-strong);
box-shadow: 0 25px 90px rgba(0, 0, 0, 0.55);
text-align: center;
}
.win-icon {
display: grid;
place-items: center;
width: 58px;
height: 58px;
margin: 0 auto 16px;
border: 1px solid rgba(80, 227, 194, 0.35);
border-radius: 18px;
background: rgba(80, 227, 194, 0.12);
color: var(--cyan);
font-size: 27px;
}
.win-card h2 {
font-size: 29px;
letter-spacing: -0.05em;
}
.win-card p {
margin-top: 9px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.win-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin: 21px 0;
}
.win-stat {
padding: 12px 8px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(0, 0, 0, 0.15);
}
.win-stat strong {
display: block;
color: var(--cyan);
font-size: 20px;
}
.win-stat span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.win-card .small-button {
min-height: 43px;
width: 100%;
border-color: rgba(80, 227, 194, 0.25);
background: rgba(80, 227, 194, 0.12);
color: var(--cyan);
}
@media (max-width: 760px) {
#mission {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 14px;
}
.mission-copy,
.objectives,
.mission-note {
display: none;
}
#mission h1 {
font-size: 25px;
}
.phase-row {
margin-bottom: 0;
}
#build-panel {
top: auto;
right: 10px;
bottom: 68px;
width: min(310px, calc(100vw - 20px));
padding: 13px;
}
.panel-title {
margin-bottom: 9px;
}
.control-section {
margin-top: 9px;
padding-top: 9px;
}
.budget-number {
font-size: 24px;
}
#help {
bottom: 8px;
width: calc(100vw - 20px);
justify-content: center;
gap: 10px;
overflow: hidden;
white-space: nowrap;
}
.help-optional {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="source-label" class="world-label">WEST RELAY</div>
<div id="target-label" class="world-label">EAST RELAY</div>
<div id="bridge-label" class="world-label">CRITICAL SPAN</div>
<section id="mission" class="panel">
<div class="eyebrow">Voxel Relay / Region 07</div>
<h1>FAULTLINE</h1>
<p class="mission-copy">
The old crossing is unstable. Sever the critical span, then construct a new
relay around the fault using no more than 12 link blocks.
</p>
<div class="phase-row">
<span id="phase-pill" class="phase-pill">Phase 1 / 3</span>
<span id="link-status" class="link-status">No stable relay</span>
</div>
<div class="objectives">
<div id="cut-objective" class="objective">
<span id="cut-check" class="objective-number">1</span>
<span>Sever the critical bridge</span>
</div>
<div id="link-objective" class="objective">
<span id="link-check" class="objective-number">2</span>
<span>Connect both relay regions</span>
</div>
<div id="budget-objective" class="objective">
<span id="budget-check" class="objective-number">3</span>
<span>Finish inside the 12-block budget</span>
</div>
</div>
<div class="mission-note">
The striped fault rejects every new link. A narrow edge route fits the
budget; the direct crossing does not.
</div>
</section>
<section id="build-panel" class="panel">
<div class="panel-title">
<h2>Build Console</h2>
<span class="tiny-tag">Manual Relay</span>
</div>
<button id="cut-button" class="cut-button">Sever Critical Span</button>
<div class="control-section">
<div class="control-label">Link budget</div>
<div class="budget-card">
<div id="budget-number" class="budget-number">—</div>
<div id="budget-caption" class="budget-caption">
Demolition required<br />to release budget
</div>
</div>
<div class="budget-track">
<div id="budget-fill" class="budget-fill"></div>
</div>
</div>
<div class="control-section">
<div class="control-label">Construction mode</div>
<div class="segmented">
<button id="build-mode" class="active" disabled>Build</button>
<button id="erase-mode" disabled>Erase</button>
</div>
</div>
<div class="control-section">
<div class="control-label">Vertical layer</div>
<div class="layer-row">
<button id="layer-down" class="layer-button" disabled>−</button>
<div class="layer-value">
<strong id="layer-value">L2</strong>
<span id="layer-name">Deck level</span>
</div>
<button id="layer-up" class="layer-button" disabled>+</button>
</div>
<div class="readout">
<span>Cell <strong id="coordinate-value">—</strong></span>
<span>Links <strong id="link-count">0</strong></span>
</div>
</div>
<div class="control-section action-grid">
<button id="undo-button" class="small-button" disabled>Undo</button>
<button id="hint-button" class="small-button accent" disabled>Show Detour</button>
<button id="clear-button" class="small-button" disabled>Clear Links</button>
<button id="reset-button" class="small-button">Reset Run</button>
<button id="focus-button" class="small-button" style="grid-column: 1 / -1">
Recenter Camera
</button>
</div>
</section>
<div id="help">
<span><b>LMB</b> orbit / build</span>
<span class="help-separator"></span>
<span><b>RMB</b> pan</span>
<span class="help-separator"></span>
<span><b>Wheel</b> zoom</span>
<span class="help-separator help-optional"></span>
<span class="help-optional"><b>WASD</b> move · <b>Q/E</b> rotate</span>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="win-overlay" hidden>
<div class="win-card">
<div class="win-icon">✓</div>
<div class="eyebrow">Relay certified</div>
<h2>Reroute complete</h2>
<p>
Both regions are connected by a stable voxel link, and the damaged
crossing is gone.
</p>
<div class="win-stats">
<div class="win-stat">
<strong id="win-links">0</strong>
<span>Links used</span>
</div>
<div class="win-stat">
<strong id="win-budget">0</strong>
<span>Credits left</span>
</div>
<div class="win-stat">
<strong id="win-time">0s</strong>
<span>Build time</span>
</div>
</div>
<button id="continue-button" class="small-button">Continue Building</button>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const stage = $("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071521);
scene.fog = new THREE.FogExp2(0x071521, 0.023);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
150
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0x9ddcff, 0x14212b, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d6, 3.4);
sun.position.set(8, 18, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x4ac9ff, 1.25);
rimLight.position.set(-12, 8, -8);
scene.add(rimLight);
const world = new THREE.Group();
scene.add(world);
const builtGroup = new THREE.Group();
const endpointGroup = new THREE.Group();
const faultGroup = new THREE.Group();
world.add(builtGroup, endpointGroup, faultGroup);
const GRID_X = 16;
const GRID_Z = 12;
const BUDGET = 12;
const START_CELL = { x: 5, y: 2, z: 5 };
const TARGET_CELL = { x: 10, y: 2, z: 5 };
let cut = false;
let won = false;
let currentMode = "build";
let activeLayer = 2;
let cutAt = 0;
let toastTimer = 0;
let hintTimer = 0;
const built = new Map();
const undoStack = [];
const terrainOccupancy = new Set();
const endpointCells = new Map();
const oldBridgeKeys = new Set();
const keyFor = (x, y, z) => `${x},${y},${z}`;
const keyForCell = (cell) => keyFor(cell.x, cell.y, cell.z);
const terrainColors = [
0x24485a,
0x2d5967,
0x376a72,
0x3f7779
];
const terrainMaterials = terrainColors.map(
(color) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.92,
metalness: 0.02
})
);
const linkMaterial = new THREE.MeshStandardMaterial({
color: 0x54e4c4,
emissive: 0x087360,
emissiveIntensity: 0.52,
roughness: 0.32,
metalness: 0.08
});
const sourceMaterial = new THREE.MeshStandardMaterial({
color: 0x65ffd4,
emissive: 0x0bc78c,
emissiveIntensity: 1.15,
roughness: 0.25
});
const targetMaterial = new THREE.MeshStandardMaterial({
color: 0xc5adff,
emissive: 0x7248df,
emissiveIntensity: 1.05,
roughness: 0.25
});
const criticalMaterial = new THREE.MeshStandardMaterial({
color: 0xff8c6a,
emissive: 0xb8292d,
emissiveIntensity: 0.78,
roughness: 0.34,
metalness: 0.12
});
const voxelGeometry = new THREE.BoxGeometry(0.92, 0.92, 0.92);
const endpointGeometry = new THREE.BoxGeometry(0.96, 0.96, 0.96);
const faultMaterial = new THREE.MeshBasicMaterial({
color: 0xff4869,
transparent: true,
opacity: 0.065,
depthWrite: false
});
const faultTopMaterial = new THREE.MeshBasicMaterial({
color: 0xff4869,
transparent: true,
opacity: 0.14,
depthWrite: false
});
const faultColumnGeometry = new THREE.BoxGeometry(0.88, 4, 0.88);
const faultTopGeometry = new THREE.BoxGeometry(0.9, 0.035, 0.9);
const ghostGeometry = new THREE.BoxGeometry(0.92, 0.92, 0.92);
const ghostMaterial = new THREE.MeshBasicMaterial({
color: 0x70ffe1,
transparent: true,
opacity: 0.19,
depthWrite: false
});
let ghostGroup = null;
const debris = [];
let oldBridgeGroup = new THREE.Group();
world.add(oldBridgeGroup);
function hash(x, z) {
const n = Math.sin(x * 12.9898 + z * 78.233) * 43758.5453;
return n - Math.floor(n);
}
function addVoxel(x, y, z, material, parent = world, geometry = voxelGeometry) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x + 0.5, y + 0.5, z + 0.5);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function buildTerrain() {
for (let x = 0; x < GRID_X; x++) {
for (let z = 0; z < GRID_Z; z++) {
let height = 0;
if (x <= 5 || x >= 10) {
height = 1;
if (hash(x, z) > 0.76) height = 2;
if ((x === 0 && z === 0) || (x === 15 && z === 11)) {
height = 1;
}
}
if (
(x === 7 && z === 1) ||
(x === 8 && z === 10) ||
(x === 7 && z === 10)
) {
height = 1;
}
if ((x === 5 && z === 5) || (x === 10 && z === 5)) {
height = 1;
}
for (let y = 0; y < height; y++) {
const matIndex = Math.min(y, terrainMaterials.length - 1);
addVoxel(x, y, z, terrainMaterials[matIndex]);
terrainOccupancy.add(keyFor(x, y, z));
}
}
}
const sourceSupport = addVoxel(5, 1, 5, terrainMaterials[2]);
sourceSupport.receiveShadow = true;
terrainOccupancy.add(keyFor(5, 1, 5));
const targetSupport = addVoxel(10, 1, 5, terrainMaterials[2]);
targetSupport.receiveShadow = true;
terrainOccupancy.add(keyFor(10, 1, 5));
const source = addVoxel(
START_CELL.x,
START_CELL.y,
START_CELL.z,
sourceMaterial,
endpointGroup,
endpointGeometry
);
source.userData.cell = { ...START_CELL };
const target = addVoxel(
TARGET_CELL.x,
TARGET_CELL.y,
TARGET_CELL.z,
targetMaterial,
endpointGroup,
endpointGeometry
);
target.userData.cell = { ...TARGET_CELL };
endpointCells.set(keyForCell(START_CELL), source);
endpointCells.set(keyForCell(TARGET_CELL), target);
const sourceLight = new THREE.PointLight(0x4bffd0, 2.2, 6, 2);
sourceLight.position.set(5.5, 3.25, 5.5);
scene.add(sourceLight);
const targetLight = new THREE.PointLight(0x9f7cff, 2.0, 6, 2);
targetLight.position.set(10.5, 3.25, 5.5);
scene.add(targetLight);
}
function buildFaultZone() {
for (let x = 6; x <= 9; x++) {
for (let z = 3; z <= 7; z++) {
const column = new THREE.Mesh(faultColumnGeometry, faultMaterial);
column.position.set(x + 0.5, 2, z + 0.5);
faultGroup.add(column);
const top = new THREE.Mesh(faultTopGeometry, faultTopMaterial);
top.position.set(x + 0.5, 4.02, z + 0.5);
faultGroup.add(top);
}
}
}
function createBridge() {
world.remove(oldBridgeGroup);
oldBridgeGroup = new THREE.Group();
world.add(oldBridgeGroup);
oldBridgeKeys.clear();
for (let x = 6; x <= 9; x++) {
const mesh = addVoxel(
x,
2,
5,
criticalMaterial,
oldBridgeGroup,
endpointGeometry
);
mesh.userData.bridge = true;
oldBridgeKeys.add(keyFor(x, 2, 5));
}
}
const sea = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90),
new THREE.MeshStandardMaterial({
color: 0x0a2637,
roughness: 0.58,
metalness: 0.24,
transparent: true,
opacity: 0.94
})
);
sea.rotation.x = -Math.PI / 2;
sea.position.y = -0.08;
sea.receiveShadow = true;
scene.add(sea);
const seaGrid = new THREE.GridHelper(80, 80, 0x245469, 0x173846);
seaGrid.position.y = -0.055;
seaGrid.material.transparent = true;
seaGrid.material.opacity = 0.18;
scene.add(seaGrid);
buildTerrain();
buildFaultZone();
createBridge();
let gridLines = null;
const gridMaterial = new THREE.LineBasicMaterial({
color: 0x88e5d3,
transparent: true,
opacity: 0.19,
depthWrite: false
});
function rebuildGrid() {
if (gridLines) {
world.remove(gridLines);
gridLines.geometry.dispose();
}
const points = [];
const y = activeLayer + 1.025;
for (let x = 0; x <= GRID_X; x++) {
points.push(x, y, 0, x, y, GRID_Z);
}
for (let z = 0; z <= GRID_Z; z++) {
points.push(0, y, z, GRID_X, y, z);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(points, 3)
);
gridLines = new THREE.LineSegments(geometry, gridMaterial);
world.add(gridLines);
}
rebuildGrid();
const hoverGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(1.025, 1.025, 1.025)
);
const hoverMaterial = new THREE.LineBasicMaterial({
color: 0x60f5d2,
transparent: true,
opacity: 0.95,
depthTest: false
});
const hoverBox = new THREE.LineSegments(hoverGeometry, hoverMaterial);
hoverBox.visible = false;
hoverBox.renderOrder = 10;
world.add(hoverBox);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const activePlane = new THREE.Plane(
new THREE.Vector3(0, 1, 0),
-(activeLayer + 0.5)
);
const planeHit = new THREE.Vector3();
const cameraState = {
focus: new THREE.Vector3(7.5, 1.55, 5.5),
radius: 22,
theta: 0.65,
phi: 0.95
};
function updateCamera() {
const { radius, theta, phi, focus } = cameraState;
const offset = new THREE.Vector3(
Math.sin(phi) * Math.sin(theta) * radius,
Math.cos(phi) * radius,
Math.sin(phi) * Math.cos(theta) * radius
);
camera.position.copy(focus).add(offset);
camera.lookAt(focus);
camera.updateMatrixWorld();
}
function focusView() {
cameraState.focus.set(7.5, 1.55, 5.5);
cameraState.radius = 22;
cameraState.theta = 0.65;
cameraState.phi = 0.95;
updateCamera();
}
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 getBuildCell() {
activePlane.constant = -(activeLayer + 0.5);
const builtHit = raycaster.intersectObjects(builtGroup.children, false)[0];
if (builtHit && builtHit.object.userData.cell?.y === activeLayer) {
return { ...builtHit.object.userData.cell };
}
const hit = raycaster.ray.intersectPlane(activePlane, planeHit);
if (!hit) return null;
const x = Math.floor(planeHit.x);
const z = Math.floor(planeHit.z);
if (x < 0 || x >= GRID_X || z < 0 || z >= GRID_Z) {
return null;
}
return { x, y: activeLayer, z };
}
function getBuiltCell() {
const hit = raycaster.intersectObjects(builtGroup.children, false)[0];
return hit ? { ...hit.object.userData.cell } : null;
}
function isForbidden(cell) {
return (
cell.x >= 6 &&
cell.x <= 9 &&
cell.z >= 3 &&
cell.z <= 7
);
}
function updateHover() {
if (currentMode === "erase") {
const cell = getBuiltCell();
if (!cell) {
hoverBox.visible = false;
$("coordinate-value").textContent = "—";
return;
}
hoverBox.visible = true;
hoverBox.position.set(cell.x + 0.5, cell.y + 0.5, cell.z + 0.5);
hoverMaterial.color.setHex(0xffb15c);
$("coordinate-value").textContent = `${cell.x}, ${cell.y}, ${cell.z}`;
return;
}
const cell = getBuildCell();
if (!cell) {
hoverBox.visible = false;
$("coordinate-value").textContent = "—";
return;
}
hoverBox.visible = true;
hoverBox.position.set(cell.x + 0.5, cell.y + 0.5, cell.z + 0.5);
const occupied =
terrainOccupancy.has(keyForCell(cell)) ||
endpointCells.has(keyForCell(cell));
if (isForbidden(cell)) {
hoverMaterial.color.setHex(0xff617c);
} else if (occupied || built.has(keyForCell(cell))) {
hoverMaterial.color.setHex(0xffb15c);
} else {
hoverMaterial.color.setHex(0x60f5d2);
}
$("coordinate-value").textContent = `${cell.x}, ${cell.y}, ${cell.z}`;
}
function announce(message, type = "") {
const toast = $("toast");
toast.textContent = message;
toast.className = `show ${type}`.trim();
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.className = "";
}, 2700);
}
function createBuiltMesh(cell) {
const mesh = addVoxel(
cell.x,
cell.y,
cell.z,
linkMaterial,
builtGroup
);
mesh.userData.cell = { ...cell };
built.set(keyForCell(cell), mesh);
}
function placeBlock(cell) {
if (!cut) {
announce("Sever the critical span before starting a permanent reroute.", "warning");
return;
}
const key = keyForCell(cell);
if (isForbidden(cell)) {
announce("The striped fault rejects new relay blocks at every layer.", "error");
return;
}
if (endpointCells.has(key)) {
announce("That cell belongs to a fixed relay anchor.", "warning");
return;
}
if (terrainOccupancy.has(key)) {
announce("Solid rock already occupies this cell.", "warning");
return;
}
if (built.has(key)) {
announce("A link block is already placed there.", "warning");
return;
}
if (built.size >= BUDGET) {
announce("The 12-link budget is exhausted. Erase a block or undo.", "error");
return;
}
createBuiltMesh(cell);
undoStack.push({ type: "add", cell: { ...cell } });
updateUI();
checkWin();
}
function removeBuilt(cell, recordUndo = true) {
const key = keyForCell(cell);
const mesh = built.get(key);
if (!mesh) return;
builtGroup.remove(mesh);
built.delete(key);
if (recordUndo) {
undoStack.push({ type: "remove", cell: { ...cell } });
}
}
function undo() {
if (!undoStack.length) return;
const action = undoStack.pop();
if (action.type === "add") {
removeBuilt(action.cell, false);
} else {
createBuiltMesh(action.cell);
}
updateUI();
announce(
action.type === "add"
? "Last placement removed and refunded."
: "Last removal restored."
);
}
function clearLinks() {
if (!built.size) return;
for (const mesh of built.values()) {
builtGroup.remove(mesh);
}
built.clear();
undoStack.length = 0;
updateUI();
announce("All player-built links cleared. Budget fully refunded.");
}
function setMode(mode) {
currentMode = mode;
$("build-mode").classList.toggle("active", mode === "build");
$("erase-mode").classList.toggle("active", mode === "erase");
hoverBox.visible = false;
updateHover();
}
function setLayer(layer) {
activeLayer = THREE.MathUtils.clamp(layer, 0, 3);
activePlane.constant = -(activeLayer + 0.5);
const names = [
"Foundation",
"Lower deck",
"Deck level",
"Upper deck"
];
$("layer-value").textContent = `L${activeLayer}`;
$("layer-name").textContent = names[activeLayer];
$("layer-down").disabled = !cut || activeLayer === 0;
$("layer-up").disabled = !cut || activeLayer === 3;
rebuildGrid();
hoverBox.visible = false;
updateHover();
}
function cutBridge() {
if (cut) return;
cut = true;
cutAt = performance.now();
const bridgeBlocks = [...oldBridgeGroup.children];
for (const mesh of bridgeBlocks) {
oldBridgeGroup.remove(mesh);
world.add(mesh);
debris.push({
mesh,
velocity: new THREE.Vector3(
(Math.random() - 0.5) * 2.1,
1.2 + Math.random() * 1.5,
(Math.random() - 0.5) * 2.1
),
spin: new THREE.Vector3(
(Math.random() - 0.5) * 4,
(Math.random() - 0.5) * 4,
(Math.random() - 0.5) * 4
)
});
}
setMode("build");
setLayer(2);
updateUI();
announce(
"Critical span severed. 12 link credits released; the striped fault is now unsafe."
);
}
function resetRun() {
for (const mesh of built.values()) {
builtGroup.remove(mesh);
}
built.clear();
undoStack.length = 0;
for (const piece of debris) {
world.remove(piece.mesh);
}
debris.length = 0;
cut = false;
won = false;
cutAt = 0;
currentMode = "build";
activeLayer = 2;
createBridge();
removeHint();
$("win-overlay").hidden = true;
setMode("build");
setLayer(2);
updateUI();
focusView();
announce("Run reset. The unstable span has been restored.");
}
function getReachable() {
const startKey = keyForCell(START_CELL);
const targetKey = keyForCell(TARGET_CELL);
const visited = new Set([startKey]);
const queue = [{ ...START_CELL }];
while (queue.length) {
const current = queue.shift();
const currentKey = keyForCell(current);
const neighbors = [
{ x: current.x - 1, y: current.y, z: current.z },
{ x: current.x + 1, y: current.y, z: current.z },
{ x: current.x, y: current.y, z: current.z - 1 },
{ x: current.x, y: current.y, z: current.z + 1 },
{ x: current.x, y: current.y - 1, z: current.z },
{ x: current.x, y: current.y + 1, z: current.z }
];
for (const neighbor of neighbors) {
const neighborKey = keyForCell(neighbor);
if (visited.has(neighborKey)) continue;
const isEndpoint = endpointCells.has(neighborKey);
const isBuilt = built.has(neighborKey);
if (!isEndpoint && !isBuilt) continue;
visited.add(neighborKey);
queue.push(neighbor);
}
}
return { visited, connected: visited.has(targetKey) };
}
function checkWin() {
if (!cut || won) return;
const { connected } = getReachable();
if (!connected) return;
won = true;
updateUI();
setTimeout(() => {
const elapsed = (performance.now() - cutAt) / 1000;
$("win-links").textContent = built.size;
$("win-budget").textContent = BUDGET - built.size;
$("win-time").textContent = `${elapsed.toFixed(1)}s`;
$("win-overlay").hidden = false;
}, 420);
}
function removeHint() {
clearTimeout(hintTimer);
if (!ghostGroup) return;
world.remove(ghostGroup);
ghostGroup.traverse((object) => {
if (object.geometry && object.geometry !== ghostGeometry) {
object.geometry.dispose();
}
});
ghostGroup = null;
}
function showHint() {
if (!cut) {
announce("Sever the critical span first.", "warning");
return;
}
removeHint();
ghostGroup = new THREE.Group();
world.add(ghostGroup);
const path = [
{ x: 5, y: 2, z: 4 },
{ x: 5, y: 2, z: 3 },
{ x: 5, y: 2, z: 2 },
{ x: 6, y: 2, z: 2 },
{ x: 7, y: 2, z: 2 },
{ x: 8, y: 2, z: 2 },
{ x: 9, y: 2, z: 2 },
{ x: 10, y: 2, z: 2 },
{ x: 10, y: 2, z: 3 },
{ x: 10, y: 2, z: 4 }
];
for (const cell of path) {
const mesh = new THREE.Mesh(ghostGeometry, ghostMaterial);
mesh.position.set(cell.x + 0.5, cell.y + 0.5, cell.z + 0.5);
ghostGroup.add(mesh);
}
const linePoints = [
new THREE.Vector3(5.5, 3.05, 5.5),
...path.map(
(cell) => new THREE.Vector3(cell.x + 0.5, 3.05, cell.z + 0.5)
),
new THREE.Vector3(10.5, 3.05, 5.5)
];
const lineGeometry = new THREE.BufferGeometry().setFromPoints(linePoints);
const line = new THREE.Line(
lineGeometry,
new THREE.LineBasicMaterial({
color: 0x7effdf,
transparent: true,
opacity: 0.8
})
);
ghostGroup.add(line);
hintTimer = setTimeout(removeHint, 8500);
announce("Ghost route shown: 10 blocks along the northern fault edge.");
}
function updateUI() {
const connection = cut
? getReachable()
: { visited: new Set(), connected: false };
const remaining = BUDGET - built.size;
const usedPercent = (built.size / BUDGET) * 100;
$("cut-button").disabled = cut;
$("cut-button").textContent = cut
? "Critical Span Severed ✓"
: "Sever Critical Span";
$("budget-number").innerHTML = cut
? `${remaining} <small>/ ${BUDGET} left</small>`
: "—";
$("budget-caption").innerHTML = cut
? `${built.size} block${built.size === 1 ? "" : "s"} placed<br />one credit per link`
: "Demolition required<br />to release budget";
$("budget-fill").style.width = `${usedPercent}%`;
$("budget-fill").classList.toggle("low", remaining <= 2);
$("build-mode").disabled = !cut;
$("erase-mode").disabled = !cut;
$("layer-down").disabled = !cut || activeLayer === 0;
$("layer-up").disabled = !cut || activeLayer === 3;
$("undo-button").disabled = !cut || undoStack.length === 0;
$("hint-button").disabled = !cut;
$("clear-button").disabled = !cut || built.size === 0;
$("link-count").textContent = built.size;
$("phase-pill").textContent = connection.connected
? "Phase 3 / 3"
: cut
? "Phase 2 / 3"
: "Phase 1 / 3";
$("link-status").textContent = connection.connected
? "Stable relay online"
: cut && built.size
? `${connection.visited.size - 1} linked block${connection.visited.size - 1 === 1 ? "" : "s"} reachable`
: "No stable relay";
$("link-status").classList.toggle("connected", connection.connected);
$("cut-objective").classList.toggle("done", cut);
$("cut-check").textContent = cut ? "✓" : "1";
$("link-objective").classList.toggle("done", connection.connected);
$("link-check").textContent = connection.connected ? "✓" : "2";
$("budget-objective").classList.toggle("done", connection.connected);
$("budget-check").textContent = connection.connected ? "✓" : "3";
}
let drag = null;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
drag = {
button: event.button,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
if (drag) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
if (
Math.hypot(
event.clientX - drag.startX,
event.clientY - drag.startY
) > 5
) {
drag.moved = true;
}
if (drag.button === 0) {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.32,
1.36
);
} else {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0)
.setY(0)
.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3())
.setY(0)
.normalize();
const scale = cameraState.radius * 0.00155;
cameraState.focus
.addScaledVector(right, -dx * scale)
.addScaledVector(forward, dy * scale);
cameraState.focus.x = THREE.MathUtils.clamp(
cameraState.focus.x,
-5,
21
);
cameraState.focus.z = THREE.MathUtils.clamp(
cameraState.focus.z,
-7,
19
);
}
drag.lastX = event.clientX;
drag.lastY = event.clientY;
updateCamera();
}
updateHover();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!drag) return;
const wasClick = !drag.moved && drag.button === 0;
drag = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
updatePointer(event);
if (!wasClick) return;
if (currentMode === "erase") {
const cell = getBuiltCell();
if (!cell) {
announce("Click a player-built link to erase it.", "warning");
return;
}
removeBuilt(cell);
updateUI();
announce("Link removed and one credit refunded.");
} else {
const cell = getBuildCell();
if (!cell) {
announce("Point at a cell inside the construction grid.", "warning");
return;
}
placeBlock(cell);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
drag = null;
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!drag) hoverBox.visible = false;
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
10,
38
);
updateCamera();
updateHover();
},
{ passive: false }
);
function panWithKeyboard(key) {
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0)
.setY(0)
.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3())
.setY(0)
.normalize();
const amount = cameraState.radius * 0.045;
if (key === "w" || key === "arrowup") {
cameraState.focus.addScaledVector(forward, amount);
}
if (key === "s" || key === "arrowdown") {
cameraState.focus.addScaledVector(forward, -amount);
}
if (key === "a" || key === "arrowleft") {
cameraState.focus.addScaledVector(right, -amount);
}
if (key === "d" || key === "arrowright") {
cameraState.focus.addScaledVector(right, amount);
}
updateCamera();
}
addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if ((event.ctrlKey || event.metaKey) && key === "z") {
event.preventDefault();
undo();
return;
}
if (["w", "a", "s", "d", "q", "e", "arrowup", "arrowdown", "arrowleft", "arrowright"].includes(key)) {
event.preventDefault();
}
if (key === "q") {
cameraState.theta -= 0.09;
updateCamera();
} else if (key === "e") {
cameraState.theta += 0.09;
updateCamera();
} else {
panWithKeyboard(key);
}
if (key === "b" && cut) setMode("build");
if (key === "x" && cut) setMode("erase");
if (key === "h") showHint();
if (key === "[") setLayer(activeLayer - 1);
if (key === "]") setLayer(activeLayer + 1);
});
$("cut-button").addEventListener("click", cutBridge);
$("build-mode").addEventListener("click", () => setMode("build"));
$("erase-mode").addEventListener("click", () => setMode("erase"));
$("layer-down").addEventListener("click", () => setLayer(activeLayer - 1));
$("layer-up").addEventListener("click", () => setLayer(activeLayer + 1));
$("undo-button").addEventListener("click", undo);
$("hint-button").addEventListener("click", showHint);
$("clear-button").addEventListener("click", clearLinks);
$("reset-button").addEventListener("click", resetRun);
$("focus-button").addEventListener("click", focusView);
$("continue-button").addEventListener("click", () => {
$("win-overlay").hidden = true;
});
function projectLabel(element, x, y, z) {
const position = new THREE.Vector3(x, y, z).project(camera);
const visible = position.z > -1 && position.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left = `${((position.x + 1) / 2) * innerWidth}px`;
element.style.top = `${((-position.y + 1) / 2) * innerHeight}px`;
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
for (let i = debris.length - 1; i >= 0; i--) {
const piece = debris[i];
piece.velocity.y -= 12 * dt;
piece.mesh.position.addScaledVector(piece.velocity, dt);
piece.mesh.rotation.x += piece.spin.x * dt;
piece.mesh.rotation.y += piece.spin.y * dt;
piece.mesh.rotation.z += piece.spin.z * dt;
if (piece.mesh.position.y < -2.5) {
world.remove(piece.mesh);
debris.splice(i, 1);
}
}
sourceMaterial.emissiveIntensity = 1.0 + Math.sin(time * 2.4) * 0.2;
targetMaterial.emissiveIntensity = 0.92 + Math.sin(time * 2.4 + 1.4) * 0.2;
criticalMaterial.emissiveIntensity = 0.66 + Math.sin(time * 3.1) * 0.17;
faultMaterial.opacity = 0.055 + Math.sin(time * 2.2) * 0.014;
sea.position.y = -0.08 + Math.sin(time * 0.55) * 0.018;
if (ghostGroup) {
ghostMaterial.opacity = 0.15 + Math.sin(time * 3.0) * 0.055;
}
projectLabel($("source-label"), 5.5, 3.65, 5.5);
projectLabel($("target-label"), 10.5, 3.65, 5.5);
if (cut) {
$("bridge-label").style.display = "none";
} else {
projectLabel($("bridge-label"), 7.5, 3.6, 5.5);
}
renderer.render(scene, camera);
}
focusView();
updateUI();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```