Prompt specification
Build or customize the Three.js 3D scene "Museum After-Hours Delivery 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: Museum After-Hours Delivery Puzzle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/museum-after-hours-delivery-puzzle-x3463
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing line of sight, occlusion, and watcher visibility. Build an original museum layout where a maintenance robot follows a route around patrolling inspection cones. Make every rule visible and use deterministic patrol timing. Voxel style.
Working Three.js Starter (museum-after-hours-delivery-puzzle-x3463):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lumen Vault — Museum Visibility Simulation</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(9, 20, 31, 0.88);
--panel-solid: #0b1824;
--line: rgba(160, 205, 225, 0.2);
--text: #e9f5fa;
--muted: #91a8b7;
--cyan: #5de4ff;
--green: #74ffb5;
--red: #ff6f73;
--amber: #ffc65c;
--pink: #e892ff;
}
* {
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,
input,
select {
font: inherit;
}
button {
color: var(--text);
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
outline: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 14px;
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#header {
position: absolute;
top: 16px;
left: 16px;
width: min(390px, calc(100vw - 32px));
padding: 16px 18px;
}
.eyebrow {
color: var(--cyan);
font: 700 11px/1.3 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 3px;
font-size: 25px;
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
#riskBadge {
position: absolute;
top: 18px;
left: 50%;
transform: translateX(-50%);
min-width: 190px;
padding: 11px 18px;
text-align: center;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(8, 18, 28, 0.88);
color: var(--cyan);
font: 800 12px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.13em;
box-shadow: 0 8px 25px rgba(0, 0, 0, 0.25);
}
#riskBadge.exposed {
color: var(--green);
border-color: rgba(116, 255, 181, 0.5);
box-shadow: 0 0 25px rgba(116, 255, 181, 0.14);
}
#rules {
position: absolute;
top: 118px;
left: 16px;
width: min(360px, calc(100vw - 32px));
padding: 15px 17px 14px;
max-height: calc(100vh - 255px);
overflow: auto;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
color: #c9dbe4;
font: 800 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.section-title small {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
}
#rules ol {
margin: 0;
padding: 0;
list-style: none;
counter-reset: rule;
}
#rules li {
position: relative;
counter-increment: rule;
margin: 0;
padding: 0 0 11px 30px;
color: #bdd0da;
font-size: 11.5px;
line-height: 1.45;
}
#rules li::before {
content: "0" counter(rule);
position: absolute;
left: 0;
top: 1px;
color: var(--cyan);
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#rules b {
color: #f2fbff;
font-weight: 750;
}
.legend-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin: 2px 0 12px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
}
.swatch {
width: 17px;
height: 3px;
flex: 0 0 auto;
border-radius: 3px;
background: currentColor;
}
.swatch.green {
color: var(--green);
}
.swatch.red {
color: var(--red);
}
.swatch.gray {
color: #8093a1;
}
.swatch.blue {
color: var(--cyan);
}
#overlayOptions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px 10px;
padding-top: 10px;
border-top: 1px solid var(--line);
}
.toggle {
display: flex;
align-items: center;
gap: 7px;
color: #bcd0da;
font-size: 10.5px;
cursor: pointer;
user-select: none;
}
.toggle input {
width: 13px;
height: 13px;
margin: 0;
accent-color: var(--cyan);
}
#watchers {
position: absolute;
top: 18px;
right: 16px;
width: 306px;
padding: 15px;
}
.watcher-row {
margin-top: 9px;
padding: 11px;
border: 1px solid rgba(150, 190, 210, 0.12);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
transition: border-color 120ms ease, background 120ms ease;
}
.watcher-row:hover,
.watcher-row.selected {
border-color: rgba(160, 220, 240, 0.34);
background: rgba(120, 205, 230, 0.06);
}
.watcher-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.watcher-name {
display: flex;
align-items: center;
gap: 8px;
color: #f0f8fb;
font: 800 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
}
.watcher-dot {
width: 8px;
height: 8px;
border-radius: 2px;
background: currentColor;
box-shadow: 0 0 9px currentColor;
}
.watcher-state {
font: 900 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.09em;
color: #8093a1;
}
.watcher-state.visible {
color: var(--green);
}
.watcher-state.blocked {
color: var(--red);
}
.watcher-data {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 8px;
color: var(--muted);
font: 600 9.5px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#controls {
position: absolute;
left: 50%;
bottom: 16px;
transform: translateX(-50%);
width: min(920px, calc(100vw - 32px));
padding: 11px 13px;
}
.control-row {
display: flex;
align-items: center;
gap: 10px;
}
.transport {
display: flex;
align-items: center;
gap: 6px;
}
.control-button {
min-height: 34px;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
cursor: pointer;
font: 750 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.06em;
}
.control-button:hover {
background: rgba(93, 228, 255, 0.1);
border-color: rgba(93, 228, 255, 0.4);
}
#playButton {
min-width: 66px;
color: var(--cyan);
}
#timeline {
min-width: 80px;
flex: 1;
height: 5px;
accent-color: var(--cyan);
cursor: pointer;
}
#clock {
min-width: 102px;
color: #d9e8ee;
text-align: right;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#viewControls {
display: flex;
gap: 6px;
margin-left: 2px;
}
#help {
margin-top: 8px;
color: #718795;
text-align: center;
font: 600 9px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.035em;
}
@media (max-width: 900px) {
#rules {
top: auto;
bottom: 102px;
max-height: 185px;
width: 300px;
}
#rules ol {
columns: 2;
column-gap: 15px;
}
#rules li {
break-inside: avoid;
}
#watchers {
width: 270px;
}
#riskBadge {
top: auto;
bottom: 105px;
}
#viewControls {
display: none;
}
}
@media (max-width: 650px) {
#header {
padding: 12px 14px;
}
h1 {
font-size: 20px;
}
#rules {
display: none;
}
#watchers {
top: auto;
right: 10px;
bottom: 98px;
width: 230px;
padding: 10px;
}
.watcher-row {
padding: 8px;
}
.watcher-data {
font-size: 8.5px;
}
#riskBadge {
left: 10px;
bottom: 98px;
transform: none;
min-width: 120px;
font-size: 9px;
}
#controls {
bottom: 9px;
width: calc(100vw - 18px);
}
#clock {
display: none;
}
#help {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<div id="hud">
<section id="header" class="panel">
<div class="eyebrow">Museum Systems / Visibility Study</div>
<h1>Lumen Vault</h1>
<p class="subtitle">
A maintenance robot circulates through three galleries while inspection
drones sweep deterministic patrol fields.
</p>
</section>
<div id="riskBadge">ROBOT UNSEEN</div>
<section id="rules" class="panel">
<div class="section-title">
<span>Simulation Rules</span>
<small>All rules are live</small>
</div>
<ol>
<li>
<b>Robot route:</b> the amber robot follows the cyan circuit at
<span id="robotSpeedText">—</span> and pauses for one second at four
blue service pads.
</li>
<li>
<b>Watcher motion:</b> every drone follows its colored floor route,
faces its direction of travel, and repeats after 30, 20, or 40 seconds.
</li>
<li>
<b>Inspection field:</b> a voxel cell belongs to a cone when its center
is inside the stated range and ±26° of the watcher's facing.
</li>
<li>
<b>Occlusion:</b> wall and exhibit-case footprints block visibility,
even when the robot is inside a cone.
</li>
<li>
<b>Visibility:</b> the robot is exposed only when a watcher has both
cone coverage and an unobstructed ray to the robot.
</li>
<li>
<b>Determinism:</b> the 60-second master clock drives everything.
Scrubbing reproduces exactly the same positions and visibility states.
</li>
</ol>
<div class="legend-grid">
<div class="legend-item green"><span class="swatch green"></span>Clear ray / exposed</div>
<div class="legend-item red"><span class="swatch red"></span>Occluded ray</div>
<div class="legend-item gray"><span class="swatch gray"></span>Outside cone</div>
<div class="legend-item blue"><span class="swatch blue"></span>Robot route</div>
</div>
<div id="overlayOptions">
<label class="toggle"><input id="showCones" type="checkbox" checked>Cones</label>
<label class="toggle"><input id="showRays" type="checkbox" checked>LoS rays</label>
<label class="toggle"><input id="showRoute" type="checkbox" checked>Route</label>
<label class="toggle"><input id="showBlockers" type="checkbox" checked>Blockers</label>
<label class="toggle"><input id="showLabels" type="checkbox" checked>Labels</label>
<label class="toggle"><input id="showGrid" type="checkbox" checked>Grid</label>
</div>
</section>
<section id="watchers" class="panel">
<div class="section-title">
<span>Watcher Visibility</span>
<small>Click to select</small>
</div>
<div id="watcherRows"></div>
</section>
<section id="controls" class="panel">
<div class="control-row">
<div class="transport">
<button id="playButton" class="control-button">PAUSE</button>
<button id="resetButton" class="control-button">RESET</button>
<button id="speedButton" class="control-button">1×</button>
</div>
<input
id="timeline"
type="range"
min="0"
max="60"
step="0.01"
value="0"
aria-label="Simulation timeline"
>
<div id="clock">00.0 / 60.0s</div>
<div id="viewControls">
<button id="isoButton" class="control-button">ISO</button>
<button id="topButton" class="control-button">TOP</button>
</div>
</div>
<div id="help">
DRAG: ORBIT · WHEEL: ZOOM · RIGHT-DRAG / SHIFT-DRAG: PAN ·
SPACE: PAUSE · R: RESET · ARROWS: ORBIT
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Application state
// -------------------------------------------------------------------------
const MASTER_PERIOD = 60;
const ROBOT_DWELL_TIME = 1;
const ROBOT_TRAVEL_TIME = MASTER_PERIOD - 4;
const CONE_CELL_SIZE = 0.35;
const CONE_HALF_ANGLE = THREE.MathUtils.degToRad(26);
let simTime = 0;
let playing = true;
let simulationSpeed = 1;
let selectedWatcher = 0;
const app = document.getElementById("app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 34, 67);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
app.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(
-20,
20,
13.5,
-13.5,
0.1,
120
);
const cameraState = {
theta: 0.72,
phi: 0.72,
zoom: 1,
target: new THREE.Vector3(0, 0, 0)
};
const hemisphere = new THREE.HemisphereLight(0xb9e8ff, 0x16212a, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 2.4);
sun.position.set(-12, 24, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.0007;
scene.add(sun);
const cyanFill = new THREE.PointLight(0x47dfff, 18, 18, 2);
cyanFill.position.set(-7, 5, 1);
scene.add(cyanFill);
const pinkFill = new THREE.PointLight(0xd773ff, 12, 15, 2);
pinkFill.position.set(7, 4, -4);
scene.add(pinkFill);
const routeGroup = new THREE.Group();
const rayGroup = new THREE.Group();
const blockerOutlineGroup = new THREE.Group();
const labelsGroup = new THREE.Group();
scene.add(routeGroup, rayGroup, blockerOutlineGroup, labelsGroup);
const occluders = [];
const watcherPickMeshes = [];
// -------------------------------------------------------------------------
// Small construction helpers
// -------------------------------------------------------------------------
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.78,
metalness: options.metalness ?? 0.08,
flatShading: true,
...options
});
}
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(
text,
{
color = "#dff8ff",
background = "rgba(5,15,23,0.82)",
border = "rgba(120,220,245,0.28)",
width = 512,
height = 128,
fontSize = 42
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const context = canvas.getContext("2d");
context.clearRect(0, 0, width, height);
context.fillStyle = background;
context.fillRect(0, 0, width, height);
context.strokeStyle = border;
context.lineWidth = 4;
context.strokeRect(3, 3, width - 6, height - 6);
let size = fontSize;
context.font = `800 ${size}px ui-monospace, SFMono-Regular, Menlo, monospace`;
const measured = context.measureText(text).width;
if (measured > width - 38) {
size = Math.max(18, Math.floor(size * (width - 38) / measured));
context.font = `800 ${size}px ui-monospace, SFMono-Regular, Menlo, monospace`;
}
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(text, width / 2, height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
return sprite;
}
function addRoomLabel(text, x, z, color = "#a9cbd8") {
const label = makeTextSprite(text, {
color,
fontSize: 34,
background: "rgba(5,15,23,0.58)",
border: "rgba(110,170,190,0.12)"
});
label.position.set(x, 0.42, z);
label.scale.set(3.7, 0.925, 1);
labelsGroup.add(label);
return label;
}
function addOutline(cx, cz, sx, sz, color = 0xff7077) {
const x0 = cx - sx / 2;
const x1 = cx + sx / 2;
const z0 = cz - sz / 2;
const z1 = cz + sz / 2;
const y = 0.065;
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x0, y, z0),
new THREE.Vector3(x1, y, z0),
new THREE.Vector3(x1, y, z0),
new THREE.Vector3(x1, y, z1),
new THREE.Vector3(x1, y, z1),
new THREE.Vector3(x0, y, z1),
new THREE.Vector3(x0, y, z1),
new THREE.Vector3(x0, y, z0)
]);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.8
});
blockerOutlineGroup.add(new THREE.LineSegments(geometry, material));
}
function addBlock(
name,
cx,
cz,
sx,
sz,
height,
color,
{
occludes = true,
outline = true,
trim = true
} = {}
) {
const material = makeMaterial(color);
const block = addBox(
scene,
[sx, height, sz],
[cx, height / 2, cz],
material
);
block.name = name;
if (trim) {
addBox(
scene,
[sx + 0.08, 0.09, sz + 0.08],
[cx, height + 0.045, cz],
makeMaterial(0x6a8492, { roughness: 0.52 }),
false
);
}
if (occludes) {
const occluder = {
name,
minX: cx - sx / 2,
maxX: cx + sx / 2,
minZ: cz - sz / 2,
maxZ: cz + sz / 2
};
occluders.push(occluder);
if (outline) addOutline(cx, cz, sx, sz);
}
return block;
}
function addVoxelSculpture(parent, cubes, material) {
for (const cube of cubes) {
addBox(
parent,
cube.size,
cube.position,
cube.material ?? material,
true
);
}
}
// -------------------------------------------------------------------------
// Museum: floor, grid, walls, exhibits
// -------------------------------------------------------------------------
addBox(
scene,
[27.4, 0.34, 21.4],
[0, -0.19, 0],
makeMaterial(0x152630, { roughness: 0.94 }),
false
);
addBox(
scene,
[26.8, 0.08, 20.8],
[0, -0.015, 0],
makeMaterial(0x203744, { roughness: 0.96 }),
false
);
const gridVertices = [];
for (let x = -13; x <= 13; x += 1) {
gridVertices.push(
new THREE.Vector3(x, 0.035, -10),
new THREE.Vector3(x, 0.035, 10)
);
}
for (let z = -10; z <= 10; z += 1) {
gridVertices.push(
new THREE.Vector3(-13, 0.035, z),
new THREE.Vector3(13, 0.035, z)
);
}
const grid = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(gridVertices),
new THREE.LineBasicMaterial({
color: 0x426171,
transparent: true,
opacity: 0.28
})
);
scene.add(grid);
const wallMaterial = makeMaterial(0x334b58);
const wallTopMaterial = makeMaterial(0x718b96);
// Outer shell.
addBlock("North wall", 0, -10, 26.7, 0.55, 2.25, 0x304653, {
outline: false
});
addBlock("South wall", 0, 10, 26.7, 0.55, 2.25, 0x304653, {
outline: false
});
addBlock("West wall", -13, 0, 0.55, 20, 2.25, 0x304653, {
outline: false
});
addBlock("East wall", 13, 0, 0.55, 20, 2.25, 0x304653, {
outline: false
});
// Archive partition with a four-meter doorway on the robot route.
addBlock("Archive wall west", -6, -5, 8, 0.5, 2.15, 0x425d69);
addBlock("Archive wall east", 6, -5, 8, 0.5, 2.15, 0x425d69);
// Conservation partition.
addBlock("Conservation partition", 3, 5, 0.5, 5, 2.2, 0x425d69);
// Exhibit bases and voxel installations.
addBlock("Tidal Engine case", -6, 1, 3.1, 2.8, 0.72, 0x1b4e5c);
addBlock("Cartographer case", 5, 1.5, 2.6, 2.6, 0.66, 0x5b4930);
addBlock("Obsidian archive case", 8, -4, 2, 3, 0.95, 0x353a50);
const tidalSculpture = new THREE.Group();
tidalSculpture.position.set(-6, 0.75, 1);
scene.add(tidalSculpture);
addVoxelSculpture(
tidalSculpture,
[
{ size: [0.7, 1.3, 0.7], position: [-0.7, 0.65, 0] },
{ size: [0.7, 0.8, 0.7], position: [0, 0.4, 0] },
{ size: [0.7, 1.9, 0.7], position: [0.7, 0.95, 0] },
{ size: [0.55, 0.55, 0.55], position: [0, 1.65, 0.35] }
],
makeMaterial(0x5de4ff, { metalness: 0.15 })
);
const mapSculpture = new THREE.Group();
mapSculpture.position.set(5, 0.68, 1.5);
scene.add(mapSculpture);
addVoxelSculpture(
mapSculpture,
[
{ size: [0.65, 0.65, 0.65], position: [-0.65, 0.33, -0.45] },
{ size: [0.65, 0.65, 0.65], position: [0.65, 0.33, -0.45] },
{ size: [0.65, 0.65, 0.65], position: [-0.65, 0.33, 0.45] },
{ size: [0.65, 0.65, 0.65], position: [0.65, 0.33, 0.45] },
{ size: [0.65, 0.9, 0.65], position: [0, 0.45, 0] }
],
makeMaterial(0xffc65c, { metalness: 0.12 })
);
const archiveSculpture = new THREE.Group();
archiveSculpture.position.set(8, 0.98, -4);
scene.add(archiveSculpture);
addVoxelSculpture(
archiveSculpture,
[
{ size: [0.75, 1.1, 0.75], position: [-0.4, 0.55, 0] },
{ size: [0.75, 0.75, 0.75], position: [0.45, 0.38, 0.2] },
{ size: [0.6, 1.45, 0.6], position: [0.35, 0.73, -0.55] }
],
makeMaterial(0xb88cff, { metalness: 0.18 })
);
// Benches are decorative only and therefore do not occlude.
addBox(
scene,
[2.5, 0.32, 0.55],
[-6.7, 0.2, 4.4],
makeMaterial(0x8a6848)
);
addBox(
scene,
[2.5, 0.32, 0.55],
[5.3, 0.2, 4.5],
makeMaterial(0x8a6848)
);
addRoomLabel("01 TIDAL GALLERY", -6, 5.9);
addRoomLabel("02 CARTOGRAPHY", 5.2, 4.6);
addRoomLabel("03 OBSIDIAN ARCHIVE", 7.8, -7.7);
addRoomLabel("MAINTENANCE", -10.4, 8.8, "#5de4ff");
// -------------------------------------------------------------------------
// Robot route and deterministic timing
// -------------------------------------------------------------------------
const routePoints = [
{ x: -10.0, z: 7.5 },
{ x: -10.0, z: -7.5 },
{ x: -5.5, z: -7.5 },
{ x: -1.0, z: -7.5 },
{ x: 0.0, z: -3.0 },
{ x: 4.0, z: -1.8 },
{ x: 7.0, z: -1.8 },
{ x: 10.5, z: -1.8 },
{ x: 10.5, z: 7.8 },
{ x: 6.0, z: 8.6 },
{ x: 0.0, z: 8.6 },
{ x: -2.0, z: 8.6 },
{ x: -10.0, z: 7.5 }
];
const dwellAt = new Set([1, 4, 7, 10]);
const routeLength = routePoints.reduce((sum, point, index) => {
const next = routePoints[(index + 1) % routePoints.length];
return (
sum +
Math.hypot(next.x - point.x, next.z - point.z)
);
}, 0);
const robotSpeed = (routeLength - 4 * ROBOT_DWELL_TIME) / ROBOT_TRAVEL_TIME;
document.getElementById("robotSpeedText").textContent =
`${robotSpeed.toFixed(2)} m/s`;
const routeCurve = new THREE.CurvePath();
for (let i = 0; i < routePoints.length; i++) {
const a = routePoints[i];
const b = routePoints[(i + 1) % routePoints.length];
routeCurve.add(
new THREE.LineCurve3(
new THREE.Vector3(a.x, 0.075, a.z),
new THREE.Vector3(b.x, 0.075, b.z)
)
);
}
const routeLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(
routeCurve.getPoints(320)
),
new THREE.LineDashedMaterial({
color: 0x5de4ff,
dashSize: 0.42,
gapSize: 0.22,
transparent: true,
opacity: 0.9
})
);
routeLine.computeLineDistances();
routeGroup.add(routeLine);
// Route direction chevrons, made only from small boxes.
for (let i = 0; i < routePoints.length; i++) {
const a = routePoints[i];
const b = routePoints[(i + 1) % routePoints.length];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const ux = dx / length;
const uz = dz / length;
const arrow = new THREE.Group();
arrow.position.set(
a.x + dx * 0.53,
0.085,
a.z + dz * 0.53
);
arrow.rotation.y = Math.atan2(ux, uz);
const arrowMaterial = makeMaterial(0x5de4ff, {
emissive: 0x174b59,
emissiveIntensity: 0.55,
roughness: 0.5
});
addBox(arrow, [0.11, 0.035, 0.22], [0, 0, 0.12], arrowMaterial, false);
addBox(arrow, [0.11, 0.035, 0.22], [-0.13, 0, -0.02], arrowMaterial, false);
addBox(arrow, [0.11, 0.035, 0.22], [0.13, 0, -0.02], arrowMaterial, false);
routeGroup.add(arrow);
}
// Four service pads.
for (const index of dwellAt) {
const point = routePoints[index];
const pad = new THREE.Group();
pad.position.set(point.x, 0.055, point.z);
const padMaterial = makeMaterial(0x227a8c, {
emissive: 0x124855,
emissiveIntensity: 0.8,
roughness: 0.5
});
addBox(pad, [0.8, 0.055, 0.15], [0, 0, -0.32], padMaterial, false);
addBox(pad, [0.8, 0.055, 0.15], [0, 0, 0.32], padMaterial, false);
addBox(pad, [0.15, 0.055, 0.5], [-0.32, 0, 0], padMaterial, false);
addBox(pad, [0.15, 0.055, 0.5], [0.32, 0, 0], padMaterial, false);
routeGroup.add(pad);
const label = makeTextSprite(`S${[...dwellAt].indexOf(index) + 1}`, {
color: "#9cf3ff",
fontSize: 48,
width: 128,
height: 128,
background: "rgba(7,22,31,0.86)",
border: "rgba(93,228,255,0.48)"
});
label.position.set(point.x, 0.52, point.z);
label.scale.set(0.54, 0.54, 1);
labelsGroup.add(label);
}
const robotTimeline = [];
let timelineCursor = 0;
let previousYaw = 0;
for (let i = 0; i < routePoints.length; i++) {
const a = routePoints[i];
const b = routePoints[(i + 1) % routePoints.length];
const length = Math.hypot(b.x - a.x, b.z - a.z);
const duration = length / robotSpeed;
const yaw = Math.atan2(b.x - a.x, b.z - a.z);
previousYaw = yaw;
robotTimeline.push({
type: "move",
start: timelineCursor,
end: timelineCursor + duration,
a,
b,
yaw
});
timelineCursor += duration;
if (dwellAt.has(i)) {
robotTimeline.push({
type: "dwell",
start: timelineCursor,
end: timelineCursor + ROBOT_DWELL_TIME,
point: b,
yaw: previousYaw
});
timelineCursor += ROBOT_DWELL_TIME;
}
}
function sampleRobot(time) {
const t = ((time % MASTER_PERIOD) + MASTER_PERIOD) % MASTER_PERIOD;
for (const segment of robotTimeline) {
if (t >= segment.start && t < segment.end) {
if (segment.type === "dwell") {
return {
x: segment.point.x,
z: segment.point.z,
yaw: segment.yaw,
moving: false
};
}
const u = (t - segment.start) / (segment.end - segment.start);
return {
x: THREE.MathUtils.lerp(segment.a.x, segment.b.x, u),
z: THREE.MathUtils.lerp(segment.a.z, segment.b.z, u),
yaw: segment.yaw,
moving: true
};
}
}
const first = robotTimeline[0];
return {
x: first.a.x,
z: first.a.z,
yaw: first.yaw,
moving: true
};
}
// -------------------------------------------------------------------------
// Voxel maintenance robot
// -------------------------------------------------------------------------
const robot = new THREE.Group();
robot.name = "Maintenance robot";
scene.add(robot);
const robotBodyMaterial = makeMaterial(0xf3a33b, {
emissive: 0x5b2b00,
emissiveIntensity: 0.45,
metalness: 0.14
});
const robotDarkMaterial = makeMaterial(0x172832, { metalness: 0.28 });
const robotLightMaterial = makeMaterial(0x9ef8ff, {
emissive: 0x4eeaff,
emissiveIntensity: 1.8
});
addBox(robot, [0.72, 0.48, 0.62], [0, 0.53, 0], robotBodyMaterial);
addBox(robot, [0.58, 0.23, 0.52], [0, 0.94, 0.02], robotDarkMaterial);
addBox(robot, [0.31, 0.1, 0.08], [0, 0.96, 0.31], robotLightMaterial, false);
addBox(robot, [0.18, 0.3, 0.42], [-0.46, 0.28, 0], robotDarkMaterial);
addBox(robot, [0.18, 0.3, 0.42], [0.46, 0.28, 0], robotDarkMaterial);
addBox(robot, [0.16, 0.12, 0.16], [-0.46, 0.2, 0.25], robotLightMaterial, false);
addBox(robot, [0.16, 0.12, 0.16], [0.46, 0.2, 0.25], robotLightMaterial, false);
const serviceBeacon = addBox(
robot,
[0.18, 0.18, 0.18],
[0, 1.18, 0],
makeMaterial(0x74ffb5, {
emissive: 0x35ff9d,
emissiveIntensity: 1.7
}),
false
);
serviceBeacon.visible = false;
// Robot exposure frame.
const statusFrame = new THREE.Group();
const statusMaterial = makeMaterial(0x5de4ff, {
emissive: 0x135064,
emissiveIntensity: 1.1,
transparent: true,
opacity: 0.9
});
addBox(statusFrame, [1.55, 0.045, 0.08], [0, 0, 0.72], statusMaterial, false);
addBox(statusFrame, [1.55, 0.045, 0.08], [0, 0, -0.72], statusMaterial, false);
addBox(statusFrame, [0.08, 0.045, 1.35], [0.72, 0, 0], statusMaterial, false);
addBox(statusFrame, [0.08, 0.045, 1.35], [-0.72, 0, 0], statusMaterial, false);
scene.add(statusFrame);
// -------------------------------------------------------------------------
// Watchers and their deterministic closed patrol paths
// -------------------------------------------------------------------------
const watcherDefinitions = [
{
name: "AUREL",
role: "PERIMETER",
color: "#5de4ff",
range: 9,
period: 30,
phase: 0,
path: [
{ x: -9.5, z: -7.7 },
{ x: 9.4, z: -7.7 },
{ x: 10.7, z: -5.9 },
{ x: 10.7, z: 6.5 },
{ x: 9.2, z: 8.0 },
{ x: -9.4, z: 8.0 },
{ x: -10.7, z: 6.2 },
{ x: -10.7, z: -5.9 }
]
},
{
name: "MICA",
role: "GALLERIES",
color: "#ffc65c",
range: 8,
period: 20,
phase: 0.12,
path: [
{ x: -7.8, z: 1.8 },
{ x: -0.2, z: -1.2 },
{ x: 8.4, z: 0.0 },
{ x: 8.4, z: 5.0 },
{ x: -2.4, z: 5.4 },
{ x: -7.8, z: 3.5 }
]
},
{
name: "VESPER",
role: "ARCHIVE",
color: "#e892ff",
range: 7.5,
period: 40,
phase: 0.35,
path: [
{ x: -7.5, z: -2.9 },
{ x: 5.8, z: -2.8 },
{ x: 8.2, z: -6.8 },
{ x: 0.0, z: -8.0 },
{ x: -7.5, z: -5.9 }
]
}
];
function makeVoxelCone(range, color) {
const group = new THREE.Group();
const cells = [];
const halfSpan = Math.ceil(range / CONE_CELL_SIZE);
for (let ix = -halfSpan; ix <= halfSpan; ix++) {
for (let iz = 0; iz <= halfSpan; iz++) {
const x = ix * CONE_CELL_SIZE;
const z = iz * CONE_CELL_SIZE;
const distance = Math.hypot(x, z);
const angle = Math.abs(Math.atan2(x, z));
if (distance <= range && angle <= CONE_HALF_ANGLE) {
cells.push({ x, z });
}
}
}
const geometry = new THREE.BoxGeometry(
CONE_CELL_SIZE * 0.91,
0.035,
CONE_CELL_SIZE * 0.91
);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.11,
depthWrite: false,
side: THREE.DoubleSide
});
const mesh = new THREE.InstancedMesh(geometry, material, cells.length);
const dummy = new THREE.Object3D();
cells.forEach((cell, index) => {
dummy.position.set(cell.x, 0, cell.z);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.renderOrder = 2;
mesh.frustumCulled = false;
group.add(mesh);
return group;
}
const watchers = watcherDefinitions.map((definition, index) => {
const group = new THREE.Group();
group.name = definition.name;
const colorMaterial = makeMaterial(definition.color, {
emissive: definition.color,
emissiveIntensity: 0.28,
metalness: 0.22
});
const darkMaterial = makeMaterial(0x172630, {
metalness: 0.35
});
const body = addBox(
group,
[0.78, 0.48, 0.62],
[0, 0, 0],
colorMaterial
);
body.userData.watcherIndex = index;
watcherPickMeshes.push(body);
addBox(group, [0.34, 0.2, 0.09], [0, 0.02, 0.36], darkMaterial);
addBox(
group,
[0.2, 0.12, 0.06],
[0, 0.02, 0.42],
makeMaterial(definition.color, {
emissive: definition.color,
emissiveIntensity: 2
}),
false
);
addBox(group, [1.15, 0.12, 0.22], [0, 0.05, -0.08], colorMaterial);
addBox(group, [0.18, 0.32, 0.18], [0, 0.35, 0], darkMaterial);
scene.add(group);
const cone = makeVoxelCone(definition.range, definition.color);
scene.add(cone);
const label = makeTextSprite(
`${definition.name} / ${definition.period}s`,
{
color: definition.color,
fontSize: 31,
background: "rgba(5,15,23,0.86)",
border: definition.color
}
);
label.scale.set(2.55, 0.64, 1);
labelsGroup.add(label);
const lineMaterial = new THREE.LineBasicMaterial({
color: 0x8093a1,
transparent: true,
opacity: 0.25,
depthTest: false,
depthWrite: false
});
const lineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]);
const line = new THREE.Line(lineGeometry, lineMaterial);
line.renderOrder = 10;
rayGroup.add(line);
const impact = addBox(
rayGroup,
[0.16, 0.16, 0.16],
[0, 0, 0],
makeMaterial(0xff6f73, {
emissive: 0xff2f44,
emissiveIntensity: 1.7
}),
false
);
impact.visible = false;
impact.renderOrder = 11;
const pathLengths = [];
let pathTotal = 0;
for (let i = 0; i < definition.path.length; i++) {
const a = definition.path[i];
const b = definition.path[(i + 1) % definition.path.length];
const length = Math.hypot(b.x - a.x, b.z - a.z);
pathLengths.push(length);
pathTotal += length;
}
return {
...definition,
index,
group,
cone,
label,
line,
impact,
pathLengths,
pathTotal,
x: 0,
z: 0,
yaw: 0
};
});
function sampleClosedPath(watcher, time) {
const u = ((time / watcher.period + watcher.phase) % 1 + 1) % 1;
let distance = u * watcher.pathTotal;
for (let i = 0; i < watcher.path.length; i++) {
const length = watcher.pathLengths[i];
if (distance <= length || i === watcher.path.length - 1) {
const a = watcher.path[i];
const b = watcher.path[(i + 1) % watcher.path.length];
const localU = length === 0 ? 0 : Math.min(distance / length, 1);
return {
x: THREE.MathUtils.lerp(a.x, b.x, localU),
z: THREE.MathUtils.lerp(a.z, b.z, localU),
yaw: Math.atan2(b.x - a.x, b.z - a.z)
};
}
distance -= length;
}
const first = watcher.path[0];
return { x: first.x, z: first.z, yaw: 0 };
}
// -------------------------------------------------------------------------
// Exact 2D segment / axis-aligned rectangle intersection
// -------------------------------------------------------------------------
function segmentRectangleIntersection(ax, az, bx, bz, rectangle) {
const dx = bx - ax;
const dz = bz - az;
let tMin = 0;
let tMax = 1;
const axes = [
[ax, dx, rectangle.minX, rectangle.maxX],
[az, dz, rectangle.minZ, rectangle.maxZ]
];
for (const [origin, direction, min, max] of axes) {
if (Math.abs(direction) < 1e-8) {
if (origin < min || origin > max) return null;
continue;
}
let t1 = (min - origin) / direction;
let t2 = (max - origin) / direction;
if (t1 > t2) [t1, t2] = [t2, t1];
tMin = Math.max(tMin, t1);
tMax = Math.min(tMax, t2);
if (tMin > tMax) return null;
}
if (tMax < 0 || tMin > 1) return null;
return THREE.MathUtils.clamp(tMin, 0, 1);
}
function angleDifference(a, b) {
return Math.abs(Math.atan2(Math.sin(a - b), Math.cos(a - b)));
}
function evaluateVisibility(watcher, robotPosition) {
const dx = robotPosition.x - watcher.x;
const dz = robotPosition.z - watcher.z;
const distance = Math.hypot(dx, dz);
const bearing = Math.atan2(dx, dz);
const insideCone =
distance <= watcher.range &&
angleDifference(bearing, watcher.yaw) <= CONE_HALF_ANGLE;
let nearestHit = null;
let nearestT = Infinity;
for (const rectangle of occluders) {
const t = segmentRectangleIntersection(
watcher.x,
watcher.z,
robotPosition.x,
robotPosition.z,
rectangle
);
// A robot directly on a boundary is not treated as behind that boundary.
if (t !== null && t < 0.9995 && t < nearestT) {
nearestT = t;
nearestHit = rectangle;
}
}
let state = "OUTSIDE";
if (insideCone) {
state = nearestHit ? "BLOCKED" : "VISIBLE";
}
return {
state,
distance,
insideCone,
hit: nearestHit,
hitT: nearestHit ? nearestT : null
};
}
// -------------------------------------------------------------------------
// Watcher UI
// -------------------------------------------------------------------------
const watcherRows = document.getElementById("watcherRows");
const riskBadge = document.getElementById("riskBadge");
watchers.forEach((watcher) => {
const row = document.createElement("div");
row.className = "watcher-row";
row.dataset.watcher = watcher.index;
row.innerHTML = `
<div class="watcher-head">
<div class="watcher-name" style="color:${watcher.color}">
<span class="watcher-dot"></span>
${watcher.name}
</div>
<div class="watcher-state">—</div>
</div>
<div class="watcher-data">
<span>${watcher.role}</span>
<span class="watcher-distance">R ${watcher.range.toFixed(1)}m</span>
<span class="watcher-test">WAITING</span>
</div>
`;
row.addEventListener("click", () => {
selectedWatcher = watcher.index;
updateSelection();
});
watcherRows.appendChild(row);
watcher.row = row;
watcher.stateElement = row.querySelector(".watcher-state");
watcher.distanceElement = row.querySelector(".watcher-distance");
watcher.testElement = row.querySelector(".watcher-test");
});
function updateSelection() {
watchers.forEach((watcher) => {
watcher.row.classList.toggle("selected", watcher.index === selectedWatcher);
watcher.cone.children[0].material.opacity =
watcher.index === selectedWatcher ? 0.21 : 0.11;
watcher.group.scale.setScalar(
watcher.index === selectedWatcher ? 1.13 : 1
);
});
}
// -------------------------------------------------------------------------
// Simulation update
// -------------------------------------------------------------------------
const timeline = document.getElementById("timeline");
const clock = document.getElementById("clock");
const playButton = document.getElementById("playButton");
function updateSimulation() {
const robotSample = sampleRobot(simTime);
robot.position.set(robotSample.x, 0, robotSample.z);
robot.rotation.y = robotSample.yaw;
robot.position.y =
robotSample.moving ? Math.sin(simTime * 8) * 0.025 : 0;
serviceBeacon.visible = !robotSample.moving;
statusFrame.position.set(robotSample.x, 0.085, robotSample.z);
let exposed = false;
for (const watcher of watchers) {
const sample = sampleClosedPath(watcher, simTime);
watcher.x = sample.x;
watcher.z = sample.z;
watcher.yaw = sample.yaw;
watcher.group.position.set(sample.x, 2.75, sample.z);
watcher.group.rotation.y = sample.yaw;
watcher.cone.position.set(sample.x, 0.04, sample.z);
watcher.cone.rotation.y = sample.yaw;
watcher.label.position.set(sample.x, 4.0, sample.z);
const visibility = evaluateVisibility(watcher, robot.position);
const start = new THREE.Vector3(watcher.x, 2.75, watcher.z);
let end = new THREE.Vector3(
robot.position.x,
robot.position.y + 0.88,
robot.position.z
);
let color = 0x8093a1;
let opacity = 0.18;
if (visibility.state === "VISIBLE") {
color = 0x74ffb5;
opacity = 0.95;
exposed = true;
} else if (visibility.state === "BLOCKED") {
color = 0xff6f73;
opacity = 0.9;
end = start.clone().lerp(end, visibility.hitT);
watcher.impact.visible = true;
watcher.impact.position.copy(end);
} else {
watcher.impact.visible = false;
}
watcher.line.geometry.setFromPoints([start, end]);
watcher.line.material.color.setHex(color);
watcher.line.material.opacity = opacity;
watcher.stateElement.textContent = visibility.state;
watcher.stateElement.className = `watcher-state ${visibility.state.toLowerCase()}`;
watcher.distanceElement.textContent =
`${visibility.distance.toFixed(1)}m / ${watcher.range.toFixed(1)}m`;
watcher.testElement.textContent = visibility.insideCone
? "IN CONE"
: "OUTSIDE";
}
const statusColor = exposed ? 0x74ffb5 : 0x5de4ff;
statusMaterial.color.setHex(statusColor);
statusMaterial.emissive.setHex(exposed ? 0x247d4c : 0x135064);
statusMaterial.emissiveIntensity = exposed ? 1.55 : 0.8;
riskBadge.textContent = exposed ? "ROBOT EXPOSED" : "ROBOT UNSEEN";
riskBadge.classList.toggle("exposed", exposed);
timeline.value = simTime;
clock.textContent = `${simTime.toFixed(1).padStart(4, "0")} / 60.0s`;
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit, wheel zoom, drag pan
// -------------------------------------------------------------------------
function updateCamera() {
const horizontalDistance = Math.sin(cameraState.phi) * 35;
camera.position.set(
cameraState.target.x + Math.sin(cameraState.theta) * horizontalDistance,
cameraState.target.y + Math.cos(cameraState.phi) * 35,
cameraState.target.z + Math.cos(cameraState.theta) * horizontalDistance
);
camera.lookAt(cameraState.target);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const viewHeight = 27;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateCamera();
}
window.addEventListener("resize", resize);
const canvas = renderer.domElement;
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let drag = null;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
drag = {
id: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false,
mode: event.button === 2 || event.shiftKey ? "pan" : "orbit"
};
});
canvas.addEventListener("pointermove", (event) => {
if (!drag || drag.id !== event.pointerId) return;
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
const totalDistance = Math.hypot(
event.clientX - drag.startX,
event.clientY - drag.startY
);
if (totalDistance > 4) drag.moved = true;
if (drag.mode === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.08,
1.38
);
} else {
const scale = 24 / window.innerHeight / cameraState.zoom;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0)
.setY(0)
.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3())
.setY(0)
.normalize();
cameraState.target
.addScaledVector(right, -dx * scale)
.addScaledVector(forward, dy * scale);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-12,
12
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-10,
10
);
}
drag.lastX = event.clientX;
drag.lastY = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
if (!drag || drag.id !== event.pointerId) return;
if (!drag.moved && drag.mode === "orbit") {
const rect = canvas.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 hit = raycaster.intersectObjects(watcherPickMeshes, false)[0];
if (hit) {
selectedWatcher = hit.object.userData.watcherIndex;
updateSelection();
}
}
drag = null;
});
canvas.addEventListener("pointercancel", () => {
drag = null;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.3
);
updateCamera();
},
{ passive: false }
);
document.getElementById("isoButton").addEventListener("click", () => {
cameraState.theta = 0.72;
cameraState.phi = 0.72;
cameraState.zoom = 1;
cameraState.target.set(0, 0, 0);
updateCamera();
});
document.getElementById("topButton").addEventListener("click", () => {
cameraState.theta = 0;
cameraState.phi = 0.09;
cameraState.zoom = 1;
cameraState.target.set(0, 0, 0);
updateCamera();
});
// -------------------------------------------------------------------------
// Playback and overlays
// -------------------------------------------------------------------------
function setPlaying(value) {
playing = value;
playButton.textContent = playing ? "PAUSE" : "PLAY";
}
playButton.addEventListener("click", () => setPlaying(!playing));
document.getElementById("resetButton").addEventListener("click", () => {
simTime = 0;
updateSimulation();
});
document.getElementById("speedButton").addEventListener("click", (event) => {
const speeds = [0.5, 1, 2];
const next = (speeds.indexOf(simulationSpeed) + 1) % speeds.length;
simulationSpeed = speeds[next];
event.currentTarget.textContent = `${simulationSpeed}×`;
});
timeline.addEventListener("input", (event) => {
setPlaying(false);
simTime = Number(event.target.value);
updateSimulation();
});
document.getElementById("showCones").addEventListener("change", (event) => {
watchers.forEach((watcher) => {
watcher.cone.visible = event.target.checked;
});
});
document.getElementById("showRays").addEventListener("change", (event) => {
rayGroup.visible = event.target.checked;
});
document.getElementById("showRoute").addEventListener("change", (event) => {
routeGroup.visible = event.target.checked;
});
document.getElementById("showBlockers").addEventListener("change", (event) => {
blockerOutlineGroup.visible = event.target.checked;
});
document.getElementById("showLabels").addEventListener("change", (event) => {
labelsGroup.visible = event.target.checked;
});
document.getElementById("showGrid").addEventListener("change", (event) => {
grid.visible = event.target.checked;
});
window.addEventListener("keydown", (event) => {
const target = event.target;
if (target instanceof HTMLInputElement || target instanceof HTMLButtonElement) {
return;
}
if (event.code === "Space") {
event.preventDefault();
setPlaying(!playing);
} else if (event.key.toLowerCase() === "r") {
simTime = 0;
updateSimulation();
} else if (event.key === "ArrowLeft") {
cameraState.theta -= 0.08;
updateCamera();
} else if (event.key === "ArrowRight") {
cameraState.theta += 0.08;
updateCamera();
} else if (event.key === "ArrowUp") {
cameraState.phi = Math.max(0.08, cameraState.phi - 0.06);
updateCamera();
} else if (event.key === "ArrowDown") {
cameraState.phi = Math.min(1.38, cameraState.phi + 0.06);
updateCamera();
} else if (event.key === "+" || event.key === "=") {
cameraState.zoom = Math.min(2.3, cameraState.zoom * 1.1);
updateCamera();
} else if (event.key === "-" || event.key === "_") {
cameraState.zoom = Math.max(0.65, cameraState.zoom / 1.1);
updateCamera();
}
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
let lastFrame = performance.now();
function animate(now) {
const delta = Math.min((now - lastFrame) / 1000, 0.05);
lastFrame = now;
if (playing) {
simTime = (simTime + delta * simulationSpeed) % MASTER_PERIOD;
}
updateSimulation();
const pulse = 1 + Math.sin(now * 0.005) * 0.045;
statusFrame.scale.setScalar(pulse);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resize();
updateSelection();
updateSimulation();
requestAnimationFrame(animate);
</script>
</body>
</html>
```