Prompt specification
Build or customize the Three.js 3D scene "Rearrange thresholds for a public event" 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: Rearrange thresholds for a public event
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/rearrange-thresholds-for-a-public-event-x2212
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring thresholds between public and private spaces. Let users open a community building's event route while keeping a private office zone closed. Highlight reachable spaces and prevent visitors from crossing closed access boundaries. Voxel style.
Working Three.js Starter (rearrange-thresholds-for-a-public-event-x2212):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Community Thresholds</title>
<style>
:root {
--ink: #17313b;
--muted: #64767b;
--paper: rgba(249, 248, 242, 0.92);
--line: rgba(23, 49, 59, 0.14);
--teal: #138a78;
--mint: #bce8d6;
--amber: #d58a22;
--rose: #c64d59;
--yellow: #f4c45b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e6de;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app {
position: relative;
width: 100vw;
height: 100vh;
isolation: isolate;
}
canvas.webgl {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
cursor: grab;
}
canvas.webgl:active {
cursor: grabbing;
}
.topbar {
position: absolute;
z-index: 20;
top: 20px;
left: 22px;
right: 22px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(249, 248, 242, 0.82);
backdrop-filter: blur(12px);
box-shadow: 0 8px 28px rgba(32, 49, 51, 0.07);
}
.brand-mark {
position: relative;
width: 31px;
height: 31px;
border: 2px solid var(--ink);
border-radius: 4px;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--ink);
}
.brand-mark::before {
width: 2px;
height: 100%;
left: 9px;
}
.brand-mark::after {
width: 2px;
height: 100%;
right: 8px;
}
.brand-copy {
display: grid;
gap: 2px;
}
.brand-copy strong {
font-size: 12px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.brand-copy span {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.top-actions {
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.live-status {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(249, 248, 242, 0.82);
backdrop-filter: blur(12px);
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(19, 138, 120, 0.13);
}
.reset-view {
border: 1px solid var(--line);
border-radius: 10px;
padding: 10px 13px;
background: rgba(249, 248, 242, 0.82);
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.reset-view:hover {
background: #fffdf6;
}
.panel {
position: absolute;
z-index: 20;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
backdrop-filter: blur(16px);
box-shadow: 0 18px 55px rgba(32, 49, 51, 0.11);
}
.control-panel {
top: 98px;
left: 22px;
width: 292px;
padding: 20px;
}
.eyebrow {
margin: 0 0 9px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(28px, 2.5vw, 38px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.intro {
margin: 13px 0 19px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.section-label {
margin: 19px 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.route-control {
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 14px;
border: 1px solid rgba(213, 138, 34, 0.25);
border-radius: 12px;
background: rgba(244, 196, 91, 0.09);
color: var(--ink);
text-align: left;
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease,
border-color 150ms ease;
}
.route-control:hover {
transform: translateY(-1px);
background: rgba(244, 196, 91, 0.15);
}
.route-control[aria-pressed="true"] {
border-color: rgba(19, 138, 120, 0.27);
background: rgba(19, 138, 120, 0.09);
}
.route-copy {
display: grid;
gap: 4px;
}
.route-copy strong {
font-size: 12px;
}
.route-copy small {
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.switch {
position: relative;
flex: 0 0 auto;
width: 38px;
height: 22px;
border-radius: 999px;
background: #d8d4c9;
transition: background 180ms ease;
}
.switch::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 16px;
height: 16px;
border-radius: 50%;
background: white;
box-shadow: 0 2px 5px rgba(20, 40, 45, 0.18);
transition: transform 180ms ease;
}
[aria-pressed="true"] .switch {
background: var(--teal);
}
[aria-pressed="true"] .switch::after {
transform: translateX(16px);
}
.private-control {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 13px 14px;
border: 1px solid rgba(198, 77, 89, 0.22);
border-radius: 12px;
background: rgba(198, 77, 89, 0.07);
}
.private-copy {
display: grid;
gap: 4px;
}
.private-copy strong {
font-size: 12px;
}
.private-copy small {
color: var(--muted);
font-size: 10px;
}
.locked-badge {
padding: 5px 7px;
border-radius: 6px;
background: var(--rose);
color: white;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.11em;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.metric {
padding: 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.45);
}
.metric strong {
display: block;
font-size: 21px;
line-height: 1;
letter-spacing: -0.04em;
}
.metric span {
display: block;
margin-top: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend {
display: grid;
gap: 9px;
margin-top: 12px;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 10px;
}
.legend-swatch {
width: 12px;
height: 12px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.06);
}
.legend-swatch.accessible {
background: var(--mint);
}
.legend-swatch.blocked {
background: #f1c76e;
}
.legend-swatch.restricted {
background: #e39aa1;
}
.inspector {
top: 98px;
right: 22px;
width: 274px;
padding: 20px;
}
.selected-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.inspector h2 {
margin: 0;
font-size: 25px;
line-height: 1.03;
letter-spacing: -0.045em;
}
.status-pill {
flex: 0 0 auto;
margin-top: 2px;
padding: 6px 7px;
border-radius: 6px;
background: var(--mint);
color: #0b6558;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.status-pill.blocked {
background: #f3d794;
color: #88550c;
}
.status-pill.restricted {
background: #e6a0a7;
color: #812c36;
}
.inspector p {
min-height: 52px;
margin: 14px 0 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.route-visitor {
width: 100%;
padding: 11px 12px;
border: 0;
border-radius: 9px;
background: var(--ink);
color: white;
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transition:
transform 150ms ease,
opacity 150ms ease;
}
.route-visitor:hover:not(:disabled) {
transform: translateY(-1px);
}
.route-visitor:disabled {
opacity: 0.3;
cursor: not-allowed;
}
.policy-note {
margin-top: 14px;
padding-top: 14px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
line-height: 1.5;
}
.policy-note strong {
color: var(--ink);
}
#labels {
position: absolute;
inset: 0;
z-index: 8;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
min-width: 92px;
padding: 6px 8px;
border: 1px solid rgba(23, 49, 59, 0.16);
border-radius: 7px;
background: rgba(250, 249, 243, 0.9);
box-shadow: 0 5px 14px rgba(31, 47, 49, 0.08);
color: var(--ink);
text-align: center;
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -8px;
width: 1px;
height: 8px;
background: rgba(23, 49, 59, 0.3);
}
.world-label .label-name,
.world-label .label-state {
display: block;
white-space: nowrap;
}
.world-label .label-name {
font-size: 9px;
font-weight: 800;
letter-spacing: 0.035em;
}
.world-label .label-state {
margin-top: 2px;
color: var(--teal);
font-size: 7px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.world-label.blocked {
border-color: rgba(213, 138, 34, 0.32);
}
.world-label.blocked .label-state {
color: #a86710;
}
.world-label.restricted {
border-color: rgba(198, 77, 89, 0.32);
}
.world-label.restricted .label-state {
color: var(--rose);
}
.gate-label {
position: absolute;
display: grid;
gap: 2px;
min-width: 104px;
padding: 6px 8px;
border: 1px solid rgba(198, 77, 89, 0.32);
border-radius: 7px;
background: rgba(255, 247, 245, 0.93);
color: var(--rose);
text-align: center;
transform: translate(-50%, -100%);
box-shadow: 0 5px 14px rgba(31, 47, 49, 0.08);
}
.gate-label span {
font-size: 7px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.gate-label strong {
font-size: 9px;
letter-spacing: 0.08em;
}
.gate-label.open {
border-color: rgba(19, 138, 120, 0.32);
background: rgba(244, 253, 248, 0.94);
color: var(--teal);
}
.hint-bar {
position: absolute;
z-index: 20;
left: 50%;
bottom: 19px;
display: flex;
align-items: center;
gap: 13px;
max-width: calc(100vw - 44px);
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(249, 248, 242, 0.86);
box-shadow: 0 9px 28px rgba(32, 49, 51, 0.09);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
transform: translateX(-50%);
backdrop-filter: blur(12px);
white-space: nowrap;
}
.hint-bar b {
color: var(--ink);
font-weight: 800;
}
.hint-separator {
width: 3px;
height: 3px;
border-radius: 50%;
background: #aeb9b9;
}
#toast {
position: absolute;
z-index: 40;
left: 50%;
bottom: 72px;
max-width: min(420px, calc(100vw - 32px));
padding: 12px 16px;
border: 1px solid rgba(23, 49, 59, 0.14);
border-radius: 10px;
background: var(--ink);
color: white;
box-shadow: 0 14px 38px rgba(23, 49, 59, 0.2);
font-size: 11px;
line-height: 1.4;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 8px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 900px) {
.control-panel {
top: auto;
bottom: 66px;
left: 12px;
width: 265px;
padding: 15px;
}
.control-panel h1,
.control-panel .intro,
.control-panel .legend {
display: none;
}
.control-panel .section-label {
margin-top: 12px;
}
.inspector {
top: 78px;
right: 12px;
width: 235px;
padding: 15px;
}
.inspector h2 {
font-size: 20px;
}
.inspector p {
min-height: 0;
font-size: 10px;
}
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.live-status {
display: none;
}
.hint-bar {
bottom: 13px;
max-width: calc(100vw - 24px);
overflow: hidden;
text-overflow: ellipsis;
}
}
@media (max-width: 560px) {
.brand-copy span,
.reset-view {
display: none;
}
.control-panel {
width: calc(100vw - 24px);
}
.control-panel .metrics,
.control-panel .policy-note,
.inspector .policy-note {
display: none;
}
.inspector {
top: 76px;
right: 12px;
width: 205px;
}
.hint-bar .desktop-hint {
display: none;
}
}
</style>
</head>
<body>
<main id="app">
<div id="labels" aria-hidden="true"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<strong>Community Thresholds</strong>
<span>Public / private access study</span>
</div>
</div>
<div class="top-actions">
<div class="live-status">
<span class="live-dot"></span>
Visitor access simulation
</div>
<button class="reset-view" id="resetView" type="button">Reset view</button>
</div>
</header>
<section class="panel control-panel" aria-label="Access controls">
<p class="eyebrow">Access control / 01</p>
<h1>Community<br />thresholds</h1>
<p class="intro">
Open the community event route while the private office remains closed.
Mint spaces are reachable from the public entrance.
</p>
<div class="section-label">Operable boundary</div>
<button
class="route-control"
id="eventToggle"
type="button"
aria-pressed="false"
>
<span class="route-copy">
<strong>Community event route</strong>
<small id="eventDescription">Hall access is closed</small>
</span>
<span class="switch" aria-hidden="true"></span>
</button>
<div class="section-label">Restricted boundary</div>
<div class="private-control">
<span class="private-copy">
<strong>Private office zone</strong>
<small>Policy-locked threshold</small>
</span>
<span class="locked-badge">Locked</span>
</div>
<div class="metrics">
<div class="metric">
<strong id="reachableCount">4</strong>
<span>Reachable spaces</span>
</div>
<div class="metric">
<strong>2</strong>
<span>Restricted spaces</span>
</div>
</div>
<div class="section-label">Access legend</div>
<div class="legend">
<div class="legend-item">
<span class="legend-swatch accessible"></span>
Visitor reachable from the street
</div>
<div class="legend-item">
<span class="legend-swatch blocked"></span>
Additional route must be opened
</div>
<div class="legend-item">
<span class="legend-swatch restricted"></span>
Private / crossing prohibited
</div>
</div>
</section>
<aside class="panel inspector" aria-label="Selected space">
<p class="eyebrow">Selected space</p>
<div class="selected-heading">
<h2 id="selectedName">Community lobby</h2>
<span class="status-pill" id="selectedStatus">Reachable</span>
</div>
<p id="selectedDescription">
The main public threshold and orientation point for the building.
</p>
<button class="route-visitor" id="routeVisitor" type="button">
Route a visitor here
</button>
<div class="policy-note">
<strong>Boundary policy:</strong> visitors can only traverse thresholds
marked open. The private office threshold never participates in routing.
</div>
</aside>
<div class="hint-bar" aria-hidden="true">
<b>Drag</b> to orbit
<span class="hint-separator"></span>
<b>Scroll</b> to zoom
<span class="hint-separator"></span>
<b class="desktop-hint">Click a gate or room</b>
<span class="hint-separator"></span>
<b>E</b> toggles event route
</div>
<div id="toast" role="status" aria-live="polite"></div>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Core setup
// -------------------------------------------------------------------------
const app = document.getElementById("app");
const labelLayer = document.getElementById("labels");
const toastElement = document.getElementById("toast");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e6de);
scene.fog = new THREE.Fog(0xe8e6de, 34, 68);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.className = "webgl";
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel community building. Drag to orbit and click spaces or gates."
);
app.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-20, 20, 12, -12, 0.1, 100);
const cameraTarget = new THREE.Vector3(-1.2, 0, 0);
let cameraTheta = 0.72;
let cameraPhi = 0.83;
const cameraRadius = 29;
function updateCamera() {
const spherical = new THREE.Spherical(
cameraRadius,
cameraPhi,
cameraTheta
);
camera.position.copy(cameraTarget).add(
new THREE.Vector3().setFromSpherical(spherical)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf7f4e9, 0x7f9290, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3d6, 3.1);
sun.position.set(-11, 22, 14);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -23;
sun.shadow.camera.right = 23;
sun.shadow.camera.top = 23;
sun.shadow.camera.bottom = -23;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d8d4, 0.75);
fill.position.set(14, 10, -18);
scene.add(fill);
const palette = {
ground: 0xe5e2d8,
cream: 0xf1eee2,
wall: 0xf5f1e5,
wallShade: 0xd5d6cd,
dark: 0x29414a,
teal: 0x138a78,
mint: 0xbce8d6,
amber: 0xd58a22,
amberLight: 0xf1c76e,
rose: 0xc64d59,
roseLight: 0xe39aa1,
yellow: 0xf4c45b,
blue: 0x4d8197,
wood: 0x98745b
};
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.92,
metalness: 0,
...options
});
}
function makeBox(
parent,
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Building plot
// -------------------------------------------------------------------------
const ground = makeBox(
scene,
34,
0.2,
31,
standardMaterial(palette.ground),
-1,
-0.27,
0
);
ground.castShadow = false;
const grid = new THREE.GridHelper(32, 32, 0xb9c2bc, 0xd3d5cd);
grid.position.set(-1, -0.155, 0);
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const roadMaterial = standardMaterial(0x617276);
makeBox(scene, 28, 0.05, 1.35, roadMaterial, -1, -0.11, 13.1);
makeBox(scene, 28, 0.08, 0.32, standardMaterial(0xf3f0e5), -1, -0.08, 12.3);
for (let x = -12; x <= 10; x += 3.2) {
makeBox(
scene,
1.4,
0.025,
0.08,
standardMaterial(0xf3df9e),
x,
-0.075,
13.1
);
}
// -------------------------------------------------------------------------
// Spatial graph
// -------------------------------------------------------------------------
const roomDefinitions = [
{
id: "plaza",
name: "Civic plaza",
x: 0,
z: 9,
w: 9,
d: 5,
type: "public",
base: 0xcfd8ce,
description:
"The public forecourt and street entrance. All visitor access begins here."
},
{
id: "lobby",
name: "Community lobby",
x: 0,
z: 4,
w: 8,
d: 5,
type: "public",
base: 0xdedbd0,
description:
"The main public threshold and orientation point for the building."
},
{
id: "courtyard",
name: "Courtyard",
x: 7,
z: 9,
w: 5,
d: 5,
type: "public",
base: 0xcbd9ce,
description:
"An open public gathering area connected directly to the civic plaza."
},
{
id: "gallery",
name: "Community gallery",
x: -7,
z: -2,
w: 6,
d: 6,
type: "public",
base: 0xded7c8,
description:
"A public exhibition area. It remains reachable, but its private-office threshold stays locked."
},
{
id: "hall",
name: "Event hall",
x: 5,
z: -3,
w: 8,
d: 7,
type: "public",
base: 0xe3d2bd,
description:
"The community event room. It becomes reachable only when the event threshold is opened."
},
{
id: "office",
name: "Private office",
x: -7,
z: -8,
w: 6,
d: 5,
type: "private",
base: 0xd7c4c0,
description:
"A restricted staff zone. The policy-locked threshold prevents all visitor routing."
},
{
id: "records",
name: "Records room",
x: -11.25,
z: -8,
w: 3.5,
d: 5,
type: "private",
base: 0xd3c0bc,
description:
"A private records space inside the restricted office zone. Visitor access is prohibited."
}
];
const edgeDefinitions = [
{
id: "arrival",
a: "plaza",
b: "lobby",
kind: "fixed",
open: true
},
{
id: "courtyard-link",
a: "plaza",
b: "courtyard",
kind: "fixed",
open: true
},
{
id: "gallery-link",
a: "lobby",
b: "gallery",
kind: "fixed",
open: true
},
{
id: "event-threshold",
a: "lobby",
b: "hall",
kind: "event",
open: false
},
{
id: "office-threshold",
a: "gallery",
b: "office",
kind: "private",
open: false
},
{
id: "records-threshold",
a: "office",
b: "records",
kind: "private",
open: false
}
];
const rooms = new Map();
const edges = new Map();
const adjacency = new Map();
const interactiveMeshes = [];
roomDefinitions.forEach((definition) => {
adjacency.set(definition.id, []);
});
edgeDefinitions.forEach((definition) => {
const edge = { ...definition };
edges.set(edge.id, edge);
adjacency.get(edge.a).push(edge);
adjacency.get(edge.b).push(edge);
});
function edgeBetween(a, b) {
return adjacency.get(a)?.find(
(edge) =>
(edge.a === a && edge.b === b) ||
(edge.a === b && edge.b === a)
);
}
// -------------------------------------------------------------------------
// Voxel room construction
// -------------------------------------------------------------------------
function createWorldLabel(room) {
const element = document.createElement("div");
element.className = "world-label";
element.innerHTML = `
<span class="label-name">${room.name}</span>
<span class="label-state">Checking access</span>
`;
labelLayer.appendChild(element);
room.labelElement = element;
room.labelAnchor = new THREE.Vector3(room.x, room.type === "private" ? 2.5 : 2.1, room.z);
}
function buildRoom(definition) {
const group = new THREE.Group();
group.position.set(definition.x, 0, definition.z);
scene.add(group);
const floorMaterial = standardMaterial(definition.base, {
emissive: 0x000000,
emissiveIntensity: 0
});
const floor = makeBox(
group,
definition.w,
0.18,
definition.d,
floorMaterial,
0,
-0.09,
0
);
floor.userData.kind = "room";
floor.userData.roomId = definition.id;
interactiveMeshes.push(floor);
const haloMaterial = new THREE.MeshBasicMaterial({
color: palette.mint,
transparent: true,
opacity: 0.12,
depthWrite: false
});
const halo = makeBox(
group,
definition.w + 0.18,
0.025,
definition.d + 0.18,
haloMaterial,
0,
0.018,
0
);
halo.castShadow = false;
halo.receiveShadow = false;
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(floor.geometry),
new THREE.LineBasicMaterial({
color: palette.teal,
transparent: true,
opacity: 0.8
})
);
outline.position.copy(floor.position);
group.add(outline);
const room = {
...definition,
group,
floor,
floorMaterial,
halo,
haloMaterial,
outline,
state: "unknown"
};
const wallMaterial = standardMaterial(palette.wall);
const privateWallMaterial = standardMaterial(0xe2d7d1);
const wallHeight = definition.type === "private" ? 1.65 : 0.82;
const wallThickness = 0.15;
function wall(width, depth, x, z, material = wallMaterial) {
makeBox(
group,
width,
wallHeight,
depth,
material,
x,
wallHeight / 2,
z
);
}
if (definition.id === "lobby") {
wall(0.16, definition.d, -definition.w / 2, 0);
wall(0.16, definition.d, definition.w / 2, 0);
}
if (definition.id === "gallery") {
wall(0.16, definition.d, -definition.w / 2, 0);
}
if (definition.id === "hall") {
wall(definition.w, 0.16, 0, -definition.d / 2);
wall(0.16, definition.d, definition.w / 2, 0);
}
if (definition.id === "office") {
const opening = 2.2;
const segment = (definition.w - opening) / 2;
wall(segment, 0.16, -(opening / 2 + segment / 2), definition.d / 2, privateWallMaterial);
wall(segment, 0.16, opening / 2 + segment / 2, definition.d / 2, privateWallMaterial);
wall(definition.w, 0.16, 0, -definition.d / 2, privateWallMaterial);
wall(0.16, definition.d, -definition.w / 2, 0, privateWallMaterial);
wall(0.16, definition.d, definition.w / 2, 0, privateWallMaterial);
}
if (definition.id === "records") {
wall(definition.w, 0.16, 0, -definition.d / 2, privateWallMaterial);
wall(0.16, definition.d, -definition.w / 2, 0, privateWallMaterial);
const sideSegment = (definition.d - 1.6) / 2;
wall(
0.16,
sideSegment,
definition.w / 2,
-(1.6 / 2 + sideSegment / 2),
privateWallMaterial
);
wall(
0.16,
sideSegment,
definition.w / 2,
1.6 / 2 + sideSegment / 2,
privateWallMaterial
);
}
rooms.set(definition.id, room);
createWorldLabel(room);
}
roomDefinitions.forEach(buildRoom);
// -------------------------------------------------------------------------
// Thresholds and gates
// -------------------------------------------------------------------------
function buildThreshold(edge) {
const roomA = rooms.get(edge.a);
const roomB = rooms.get(edge.b);
const midpoint = new THREE.Vector3(
(roomA.x + roomB.x) / 2,
0,
(roomA.z + roomB.z) / 2
);
const root = new THREE.Group();
root.position.copy(midpoint);
root.rotation.y = Math.atan2(
roomB.x - roomA.x,
roomB.z - roomA.z
);
scene.add(root);
const stripMaterial = new THREE.MeshBasicMaterial({
color: edge.open ? palette.teal : palette.rose,
transparent: true,
opacity: 0.85
});
if (edge.kind === "fixed") {
const strip = makeBox(
root,
0.24,
0.035,
1.35,
stripMaterial,
0,
0.04,
0
);
strip.castShadow = false;
} else {
const width = edge.id === "records-threshold" ? 1.55 : 2.25;
const postMaterial = standardMaterial(palette.dark);
const panelMaterial = standardMaterial(
edge.open ? palette.teal : palette.rose,
{ roughness: 0.75 }
);
makeBox(root, 0.17, 1.55, 0.17, postMaterial, -width / 2, 0.78, 0);
makeBox(root, 0.17, 1.55, 0.17, postMaterial, width / 2, 0.78, 0);
makeBox(root, width + 0.25, 0.16, 0.17, postMaterial, 0, 1.5, 0);
const hinge = new THREE.Group();
hinge.position.x = -width / 2;
root.add(hinge);
const panel = makeBox(
hinge,
width,
1.05,
0.12,
panelMaterial,
width / 2,
0.68,
0
);
panel.userData.kind = "gate";
panel.userData.edgeId = edge.id;
interactiveMeshes.push(panel);
const stripe = makeBox(
root,
width,
0.035,
0.18,
stripMaterial,
0,
0.04,
0
);
stripe.castShadow = false;
const label = document.createElement("div");
label.className = "gate-label";
label.innerHTML = `
<span>${edge.kind === "event" ? "Community event" : "Private access"}</span>
<strong>${edge.open ? "Open" : edge.kind === "private" ? "Locked" : "Closed"}</strong>
`;
labelLayer.appendChild(label);
edge.hinge = hinge;
edge.panel = panel;
edge.panelMaterial = panelMaterial;
edge.labelElement = label;
edge.labelAnchor = midpoint.clone().add(new THREE.Vector3(0, 2.25, 0));
}
edge.stripMaterial = stripMaterial;
edge.root = root;
edge.midpoint = midpoint;
}
edges.forEach(buildThreshold);
function updateGate(edge) {
if (edge.kind === "fixed") {
edge.stripMaterial.color.set(edge.open ? palette.teal : palette.rose);
return;
}
edge.hinge.rotation.y = edge.open ? -Math.PI / 2 : 0;
edge.panelMaterial.color.set(edge.open ? palette.teal : palette.rose);
edge.stripMaterial.color.set(edge.open ? palette.teal : palette.rose);
edge.labelElement.classList.toggle("open", edge.open);
edge.labelElement.querySelector("strong").textContent = edge.open
? "Open"
: edge.kind === "private"
? "Locked"
: "Closed";
}
edges.forEach(updateGate);
// -------------------------------------------------------------------------
// Furniture and voxel details
// -------------------------------------------------------------------------
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
const wood = standardMaterial(palette.wood);
const metal = standardMaterial(palette.dark);
makeBox(group, 1.8, 0.18, 0.55, wood, 0, 0.58, 0);
makeBox(group, 1.8, 0.62, 0.14, wood, 0, 0.95, -0.22);
makeBox(group, 0.16, 0.58, 0.44, metal, -0.65, 0.29, 0);
makeBox(group, 0.16, 0.58, 0.44, metal, 0.65, 0.29, 0);
}
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
const trunk = standardMaterial(0x795c47);
const leaves = standardMaterial(0x769c78);
const leavesLight = standardMaterial(0x94b58d);
makeBox(group, 0.32, 1.35, 0.32, trunk, 0, 0.67, 0);
makeBox(group, 1.25, 1.05, 1.25, leaves, 0, 1.7, 0);
makeBox(group, 0.78, 0.78, 0.78, leavesLight, 0.37, 2.25, -0.12);
makeBox(group, 0.65, 0.65, 0.65, leaves, -0.38, 2.35, 0.1);
}
function addPlanter(x, z, color = 0x789577) {
makeBox(scene, 0.85, 0.48, 0.85, standardMaterial(0xc7b8a4), x, 0.24, z);
makeBox(scene, 0.64, 0.45, 0.64, standardMaterial(color), x, 0.68, z);
}
addBench(-2.5, 10.1);
addBench(2.2, 10.1);
addBench(7, 7.4, Math.PI / 2);
addTree(-5.1, 10.5, 0.9);
addTree(9.7, 10.4, 1);
addTree(-11.3, 11, 0.82);
addTree(8.7, 5.2, 0.72);
addPlanter(5.4, 7.8);
addPlanter(8.4, 8.1);
addPlanter(-4.5, 6.3);
// Plaza paving
for (let x = -3.5; x <= 3.5; x += 1.1) {
makeBox(
scene,
0.72,
0.035,
0.72,
standardMaterial(x % 2 ? 0xd4d9ce : 0xc5d0c7),
x,
0.025,
8.1
);
}
// Lobby reception and stools
makeBox(scene, 2.25, 0.9, 0.85, standardMaterial(0xb88868), 2, 0.45, 4.6);
makeBox(scene, 2.4, 0.12, 1, standardMaterial(0xe8d7bf), 2, 0.96, 4.6);
makeBox(scene, 0.65, 1.1, 0.14, standardMaterial(palette.dark), -1.2, 0.72, 2.2);
for (let x = -2; x <= 2; x += 1.3) {
makeBox(scene, 0.58, 0.15, 0.58, standardMaterial(0xb88966), x, 0.47, 5.65);
makeBox(scene, 0.16, 0.47, 0.16, standardMaterial(palette.dark), x, 0.23, 5.65);
}
// Gallery plinths and art blocks
const artColors = [0xe68a61, 0x4e8b9b, 0xe3b54e, 0x7c9c79];
[
[-8.5, -3.8],
[-6.2, -3.6],
[-8.4, -0.7],
[-5.8, -0.3]
].forEach(([x, z], index) => {
makeBox(scene, 0.82, 0.65, 0.82, standardMaterial(0xe7e1d2), x, 0.33, z);
makeBox(
scene,
0.55 + (index % 2) * 0.2,
0.75 + (index % 3) * 0.18,
0.55,
standardMaterial(artColors[index]),
x,
1.02,
z
);
});
// Event hall stage and seating
makeBox(scene, 5.3, 0.48, 1.6, standardMaterial(0x8f6858), 5, 0.24, -5.2);
makeBox(scene, 3.8, 0.7, 0.24, standardMaterial(palette.dark), 5, 1.2, -5.75);
makeBox(scene, 1.4, 1.0, 0.18, standardMaterial(0x7896a0), 5, 1.55, -5.55);
const chairMaterial = standardMaterial(0x718b8b);
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 4; col++) {
const x = 2.6 + col * 1.55;
const z = -3.4 + row * 1.35;
makeBox(scene, 0.55, 0.16, 0.55, chairMaterial, x, 0.48, z);
makeBox(scene, 0.55, 0.55, 0.12, chairMaterial, x, 0.79, z - 0.22);
makeBox(scene, 0.12, 0.48, 0.12, palette.dark, x - 0.18, 0.24, z);
makeBox(scene, 0.12, 0.48, 0.12, palette.dark, x + 0.18, 0.24, z);
}
}
// Private office desks
for (const x of [-8.3, -5.7]) {
makeBox(scene, 1.5, 0.18, 0.75, standardMaterial(0x9b765e), x, 0.76, -7.2);
makeBox(scene, 0.18, 0.76, 0.18, palette.dark, x - 0.55, 0.38, -7.2);
makeBox(scene, 0.18, 0.76, 0.18, palette.dark, x + 0.55, 0.38, -7.2);
makeBox(scene, 0.62, 0.48, 0.55, standardMaterial(0x53666c), x, 1.08, -7.25);
}
// Records shelving
for (let z = -9.7; z <= -6.4; z += 1.05) {
makeBox(scene, 2.4, 1.85, 0.22, standardMaterial(0x917363), -11.25, 0.93, z);
for (let level = 0; level < 3; level++) {
makeBox(
scene,
1.9,
0.35,
0.16,
standardMaterial(level % 2 ? 0xc6b28d : 0xa99275),
-11.25,
0.4 + level * 0.5,
z + 0.15
);
}
}
// Entrance bollards
for (const x of [-4, -2.6, 2.6, 4]) {
makeBox(scene, 0.2, 0.72, 0.2, standardMaterial(palette.dark), x, 0.36, 11.8);
}
// -------------------------------------------------------------------------
// Reachability and visual state
// -------------------------------------------------------------------------
let reachable = new Set();
function recomputeReachability() {
const nextReachable = new Set(["plaza"]);
const stack = ["plaza"];
while (stack.length) {
const node = stack.pop();
for (const edge of adjacency.get(node) ?? []) {
if (!edge.open) continue;
const other = edge.a === node ? edge.b : edge.a;
if (!nextReachable.has(other)) {
nextReachable.add(other);
stack.push(other);
}
}
}
reachable = nextReachable;
for (const room of rooms.values()) {
let state;
if (room.type === "private") {
state = "restricted";
} else if (reachable.has(room.id)) {
state = "accessible";
} else {
state = "blocked";
}
room.state = state;
room.labelElement.className = `world-label ${state}`;
const stateText =
state === "accessible"
? "Visitor reachable"
: state === "blocked"
? "Route closed"
: "Private · no entry";
room.labelElement.querySelector(".label-state").textContent = stateText;
if (state === "accessible") {
room.haloMaterial.color.set(palette.mint);
room.haloMaterial.opacity = 0.15;
room.outline.material.color.set(palette.teal);
room.floorMaterial.emissive.set(0x2f8b77);
room.floorMaterial.emissiveIntensity = 0.11;
} else if (state === "blocked") {
room.haloMaterial.color.set(palette.amberLight);
room.haloMaterial.opacity = 0.1;
room.outline.material.color.set(palette.amber);
room.floorMaterial.emissive.set(0xb16b12);
room.floorMaterial.emissiveIntensity = 0.13;
} else {
room.haloMaterial.color.set(palette.roseLight);
room.haloMaterial.opacity = 0.13;
room.outline.material.color.set(palette.rose);
room.floorMaterial.emissive.set(0xa23b48);
room.floorMaterial.emissiveIntensity = 0.12;
}
}
document.getElementById("reachableCount").textContent =
String([...reachable].filter((id) => rooms.get(id).type === "public").length);
updateInspector();
}
// -------------------------------------------------------------------------
// Visitors and route visualization
// -------------------------------------------------------------------------
const visitorColors = [
0xe36f56,
0x3c8794,
0xe1ae43,
0x7167a0,
0x5e9d72,
0xd9859e,
0x447b9a,
0xc28a4b
];
function nodePosition(nodeId) {
const room = rooms.get(nodeId);
return new THREE.Vector3(room.x, 0, room.z);
}
function createVisitor(color, nodeId, offsetX = 0, offsetZ = 0) {
const group = new THREE.Group();
const bodyMaterial = standardMaterial(color);
const skinMaterial = standardMaterial(0xe6b68f);
const darkMaterial = standardMaterial(palette.dark);
makeBox(group, 0.42, 0.55, 0.34, bodyMaterial, 0, 0.48, 0);
makeBox(group, 0.34, 0.34, 0.34, skinMaterial, 0, 0.92, 0);
makeBox(group, 0.14, 0.28, 0.16, darkMaterial, -0.12, 0.14, 0);
makeBox(group, 0.14, 0.28, 0.16, darkMaterial, 0.12, 0.14, 0);
const start = nodePosition(nodeId);
group.position.set(start.x + offsetX, 0, start.z + offsetZ);
scene.add(group);
return {
mesh: group,
node: nodeId,
route: [],
speed: 1.15 + Math.random() * 0.5,
waitTime: Math.random() * 0.6
};
}
const visitors = [
createVisitor(visitorColors[0], "plaza", -1.6, 0.7),
createVisitor(visitorColors[1], "plaza", 1.2, -0.8),
createVisitor(visitorColors[2], "lobby", -1.5, 0.4),
createVisitor(visitorColors[3], "lobby", 1.4, -0.6),
createVisitor(visitorColors[4], "courtyard", -0.5, 0.2),
createVisitor(visitorColors[5], "courtyard", 0.7, -0.7),
createVisitor(visitorColors[6], "gallery", -0.8, 0.3),
createVisitor(visitorColors[7], "gallery", 0.9, -0.4)
];
function findPath(start, target) {
if (start === target) return [start];
const queue = [[start]];
const visited = new Set([start]);
while (queue.length) {
const path = queue.shift();
const current = path[path.length - 1];
for (const edge of adjacency.get(current) ?? []) {
if (!edge.open) continue;
const other = edge.a === current ? edge.b : edge.a;
if (visited.has(other)) continue;
const nextPath = [...path, other];
if (other === target) return nextPath;
visited.add(other);
queue.push(nextPath);
}
}
return null;
}
function chooseNextNode(visitor) {
const options = (adjacency.get(visitor.node) ?? [])
.filter((edge) => {
if (!edge.open) return false;
const other = edge.a === visitor.node ? edge.b : edge.a;
return reachable.has(other);
})
.map((edge) => (edge.a === visitor.node ? edge.b : edge.a));
if (!options.length) return null;
return options[Math.floor(Math.random() * options.length)];
}
const routeGeometry = new THREE.BufferGeometry();
const routePositions = new Float32Array(32 * 3);
const routePositionAttribute = new THREE.BufferAttribute(routePositions, 3);
routePositionAttribute.setUsage(THREE.DynamicDrawUsage);
routeGeometry.setAttribute("position", routePositionAttribute);
routeGeometry.setDrawRange(0, 0);
const routeLine = new THREE.Line(
routeGeometry,
new THREE.LineDashedMaterial({
color: palette.yellow,
dashSize: 0.34,
gapSize: 0.18,
transparent: true,
opacity: 0.95,
depthTest: false
})
);
routeLine.renderOrder = 10;
routeLine.visible = false;
scene.add(routeLine);
let activeVisitor = null;
function updateRouteLine() {
if (!activeVisitor || activeVisitor.route.length < 2) {
routeLine.visible = false;
return;
}
const points = [activeVisitor.mesh.position.clone()];
points[0].y = 0.38;
for (let i = 1; i < activeVisitor.route.length; i++) {
const point = nodePosition(activeVisitor.route[i]);
point.y = 0.38;
points.push(point);
}
points.forEach((point, index) => {
routePositions[index * 3] = point.x;
routePositions[index * 3 + 1] = point.y;
routePositions[index * 3 + 2] = point.z;
});
routeGeometry.setDrawRange(0, points.length);
routePositionAttribute.needsUpdate = true;
routeLine.computeLineDistances();
routeLine.visible = true;
}
function routeVisitorTo(targetId) {
if (!reachable.has(targetId)) return false;
for (const visitor of visitors) {
const path = findPath(visitor.node, targetId);
if (path && path.length > 1) {
visitor.route = path;
activeVisitor = visitor;
updateRouteLine();
return true;
}
}
for (const visitor of visitors) {
if (visitor.node === targetId) {
visitor.route = [targetId];
activeVisitor = visitor;
routeLine.visible = false;
return true;
}
}
return false;
}
// Selection frame
const selectionFrame = new THREE.Group();
const selectionMaterial = new THREE.MeshBasicMaterial({
color: palette.yellow,
transparent: true,
opacity: 0.9
});
const selectionHorizontal = [
makeBox(selectionFrame, 1, 0.04, 0.09, selectionMaterial, 0, 0.06, 0.5),
makeBox(selectionFrame, 1, 0.04, 0.09, selectionMaterial, 0, 0.06, -0.5)
];
const selectionVertical = [
makeBox(selectionFrame, 0.09, 0.04, 1, selectionMaterial, 0.5, 0.06, 0),
makeBox(selectionFrame, 0.09, 0.04, 1, selectionMaterial, -0.5, 0.06, 0)
];
scene.add(selectionFrame);
let selectedRoomId = "lobby";
function updateSelectionFrame() {
const room = rooms.get(selectedRoomId);
selectionFrame.position.set(room.x, 0, room.z);
selectionHorizontal.forEach((bar) => {
bar.scale.x = room.w + 0.28;
bar.position.z = (room.d + 0.28) / 2;
});
selectionHorizontal[1].position.z *= -1;
selectionVertical.forEach((bar) => {
bar.scale.z = room.d + 0.28;
bar.position.x = (room.w + 0.28) / 2;
});
selectionVertical[1].position.x *= -1;
}
function updateInspector() {
const room = rooms.get(selectedRoomId);
const status = document.getElementById("selectedStatus");
const routeButton = document.getElementById("routeVisitor");
document.getElementById("selectedName").textContent = room.name;
document.getElementById("selectedDescription").textContent = room.description;
status.className = "status-pill";
if (room.type === "private") {
status.textContent = "Restricted";
status.classList.add("restricted");
routeButton.disabled = true;
routeButton.textContent = "Visitor route prohibited";
} else if (reachable.has(room.id)) {
status.textContent = "Reachable";
routeButton.disabled = false;
routeButton.textContent = "Route a visitor here";
} else {
status.textContent = "Route closed";
status.classList.add("blocked");
routeButton.disabled = true;
routeButton.textContent = "Open event route to enable";
}
}
function selectRoom(roomId, announce = false) {
if (!rooms.has(roomId)) return;
selectedRoomId = roomId;
updateSelectionFrame();
updateInspector();
const room = rooms.get(roomId);
if (announce) {
if (room.type === "private") {
showToast(`${room.name} is restricted. Visitor routing is blocked.`);
} else if (!reachable.has(roomId)) {
showToast(`${room.name} is not reachable until the event route is opened.`);
}
}
}
// -------------------------------------------------------------------------
// Access actions
// -------------------------------------------------------------------------
let toastTimer = null;
function showToast(message) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toastElement.classList.remove("show");
}, 3100);
}
const eventEdge = edges.get("event-threshold");
const eventToggle = document.getElementById("eventToggle");
const eventDescription = document.getElementById("eventDescription");
function setEventRoute(open) {
if (!open) {
const visitorsInside = visitors.some(
(visitor) =>
visitor.node === "hall" ||
visitor.route.includes("hall")
);
if (visitorsInside) {
showToast(
"The event route stays open until the visitor currently approaching it has cleared the threshold."
);
return;
}
}
eventEdge.open = open;
updateGate(eventEdge);
recomputeReachability();
eventToggle.setAttribute("aria-pressed", String(open));
eventDescription.textContent = open
? "Hall access is open"
: "Hall access is closed";
if (open) {
selectRoom("hall");
routeVisitorTo("hall");
showToast(
"Community event route opened. The event hall is now visitor-reachable."
);
} else {
if (activeVisitor?.route.includes("hall")) {
activeVisitor.route = [activeVisitor.node];
}
updateRouteLine();
showToast(
"Community event route closed. The event hall is no longer visitor-reachable."
);
}
}
eventToggle.addEventListener("click", () => {
setEventRoute(!eventEdge.open);
});
document.getElementById("routeVisitor").addEventListener("click", () => {
if (!reachable.has(selectedRoomId)) return;
const didRoute = routeVisitorTo(selectedRoomId);
showToast(
didRoute
? `A visitor is now following an open-only route to ${rooms.get(selectedRoomId).name}.`
: `A visitor is already in ${rooms.get(selectedRoomId).name}.`
);
});
document.getElementById("resetView").addEventListener("click", () => {
cameraTheta = 0.72;
cameraPhi = 0.83;
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "e") {
setEventRoute(!eventEdge.open);
}
});
// -------------------------------------------------------------------------
// Picking and direct camera controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
let pointerMoved = false;
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;
}
function pick(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(interactiveMeshes, false)[0];
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerDown = {
x: event.clientX,
y: event.clientY,
pointerId: event.pointerId
};
pointerMoved = false;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 3) pointerMoved = true;
cameraTheta -= dx * 0.007;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.006,
0.52,
1.18
);
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
updateCamera();
} else {
const hit = pick(event);
renderer.domElement.style.cursor = hit ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerDown && !pointerMoved) {
const hit = pick(event);
if (hit?.object.userData.kind === "room") {
selectRoom(hit.object.userData.roomId, true);
}
if (hit?.object.userData.kind === "gate") {
const edge = edges.get(hit.object.userData.edgeId);
if (edge.kind === "event") {
setEventRoute(!edge.open);
} else if (edge.kind === "private") {
showToast(
"Private office access is locked by policy and cannot be opened."
);
}
}
}
pointerDown = null;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * (event.deltaY > 0 ? 0.9 : 1.1),
0.65,
1.75
);
camera.updateProjectionMatrix();
}, { passive: false });
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
// -------------------------------------------------------------------------
// Labels and responsive sizing
// -------------------------------------------------------------------------
function projectLabel(element, position) {
const projected = position.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(21, 29 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
for (const visitor of visitors) {
if (visitor.route.length > 1) {
const nextNode = visitor.route[1];
const connectingEdge = edgeBetween(visitor.node, nextNode);
// This is the key access invariant: no visitor can begin or continue
// across a threshold that is currently closed.
if (!connectingEdge || !connectingEdge.open) {
visitor.route = [visitor.node];
continue;
}
const target = nodePosition(nextNode);
const direction = target.clone().sub(visitor.mesh.position);
direction.y = 0;
const distance = direction.length();
const step = visitor.speed * delta;
if (distance <= step) {
visitor.mesh.position.x = target.x;
visitor.mesh.position.z = target.z;
visitor.node = nextNode;
visitor.route.shift();
} else {
direction.normalize();
visitor.mesh.position.addScaledVector(direction, step);
}
} else {
visitor.waitTime -= delta;
if (visitor.waitTime <= 0) {
const nextNode = chooseNextNode(visitor);
if (nextNode) {
visitor.route = [visitor.node, nextNode];
}
visitor.waitTime = 0.3 + Math.random() * 1.1;
}
}
const bob = Math.sin(clock.elapsedTime * 8 + visitor.mesh.id) * 0.015;
visitor.mesh.position.y = bob;
}
if (activeVisitor) updateRouteLine();
for (const room of rooms.values()) {
projectLabel(room.labelElement, room.labelAnchor);
}
for (const edge of edges.values()) {
if (edge.labelElement) {
projectLabel(edge.labelElement, edge.labelAnchor);
}
}
renderer.render(scene, camera);
}
recomputeReachability();
updateSelectionFrame();
updateInspector();
animate();
showToast(
"The community event route is closed. Open it while the private office remains locked."
);
</script>
</body>
</html>
```