Prompt specification
Build or customize the Three.js 3D scene "Inspect daily routes through nested spaces" 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: Inspect daily routes through nested spaces
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/inspect-daily-routes-through-nested-spaces-x2972
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows Beijing's hutongs and courtyard compounds. Let users trace a visitor, resident, and delivery path through a fictional courtyard layout. Highlight permitted thresholds and avoid allowing shortcuts across private rooms or walls. Voxel style.
Working Three.js Starter (inspect-daily-routes-through-nested-spaces-x2972):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hutong Threshold Atlas</title>
<style>
:root {
--ink: #16252c;
--panel: rgba(18, 34, 40, 0.92);
--panel-soft: rgba(25, 43, 49, 0.82);
--line: rgba(255, 255, 255, 0.13);
--text: #f5f1e7;
--muted: #aebcba;
--role: #ff7657;
--role-soft: rgba(255, 118, 87, 0.17);
--danger: #e25c57;
--shadow: 0 16px 48px rgba(12, 22, 26, 0.26);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d2c3;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
outline: none;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(10, 22, 26, 0.12), transparent 24%),
radial-gradient(circle at 50% 42%, transparent 48%, rgba(23, 35, 37, 0.13));
}
.topbar {
position: fixed;
z-index: 20;
top: 18px;
left: 20px;
right: 20px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
color: var(--ink);
filter: drop-shadow(0 2px 8px rgba(255, 255, 255, 0.28));
}
.seal {
width: 46px;
height: 46px;
display: grid;
place-items: center;
border: 2px solid rgba(22, 37, 44, 0.88);
background: rgba(248, 243, 226, 0.9);
color: #7f3028;
font-family: serif;
font-size: 25px;
font-weight: 800;
box-shadow: 4px 4px 0 rgba(22, 37, 44, 0.22);
}
.brand h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(18px, 2vw, 25px);
letter-spacing: 0.01em;
line-height: 1.05;
}
.brand p {
margin: 5px 0 0;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
opacity: 0.72;
}
.view-button {
pointer-events: auto;
border: 1px solid rgba(22, 37, 44, 0.28);
background: rgba(248, 243, 226, 0.88);
color: var(--ink);
padding: 10px 13px;
cursor: pointer;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
box-shadow: 3px 3px 0 rgba(22, 37, 44, 0.14);
}
.view-button:hover {
background: #fffaf0;
transform: translateY(-1px);
}
.panel {
position: fixed;
z-index: 20;
background: var(--panel);
border: 1px solid var(--line);
box-shadow: var(--shadow);
backdrop-filter: blur(13px);
-webkit-backdrop-filter: blur(13px);
}
.role-panel {
top: 98px;
left: 20px;
width: 292px;
padding: 16px;
}
.panel-kicker {
margin-bottom: 10px;
color: #c6d0cd;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.panel-heading {
margin: 0 0 5px;
font-family: Georgia, "Times New Roman", serif;
font-size: 21px;
font-weight: 500;
}
.panel-intro {
margin: 0 0 14px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.role-list {
display: grid;
gap: 9px;
}
.role-button {
width: 100%;
display: grid;
grid-template-columns: 42px 1fr 12px;
gap: 11px;
align-items: center;
padding: 11px;
text-align: left;
color: var(--text);
background: rgba(255, 255, 255, 0.045);
border: 1px solid rgba(255, 255, 255, 0.1);
cursor: pointer;
transition: 150ms ease;
}
.role-button:hover {
background: rgba(255, 255, 255, 0.09);
transform: translateX(2px);
}
.role-button.active {
background: var(--role-soft);
border-color: color-mix(in srgb, var(--role) 65%, transparent);
box-shadow: inset 3px 0 0 var(--role);
}
.role-glyph {
width: 42px;
height: 42px;
display: grid;
place-items: center;
background: #26383d;
color: var(--role);
border: 1px solid rgba(255, 255, 255, 0.12);
font-family: serif;
font-size: 21px;
font-weight: 800;
}
.role-name {
display: block;
font-size: 13px;
font-weight: 800;
margin-bottom: 3px;
}
.role-access {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.role-dot {
width: 8px;
height: 8px;
background: var(--role);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--role) 16%, transparent);
}
.guardrail {
margin-top: 14px;
padding: 12px;
display: grid;
grid-template-columns: 28px 1fr;
gap: 10px;
align-items: start;
background: rgba(255, 255, 255, 0.045);
border: 1px solid rgba(255, 255, 255, 0.08);
}
.lock {
width: 27px;
height: 27px;
display: grid;
place-items: center;
color: var(--role);
background: var(--role-soft);
font-size: 14px;
font-weight: 900;
}
.guardrail strong {
display: block;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
margin-bottom: 4px;
}
.guardrail p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.route-panel {
top: 98px;
right: 20px;
width: 356px;
padding: 17px;
}
.route-topline {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
.route-topline .panel-kicker {
margin: 0;
}
.route-distance {
padding: 4px 7px;
color: var(--role);
background: var(--role-soft);
border: 1px solid color-mix(in srgb, var(--role) 32%, transparent);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.06em;
white-space: nowrap;
}
#routeTitle {
margin: 12px 0 5px;
font-family: Georgia, "Times New Roman", serif;
font-size: 24px;
font-weight: 500;
}
#routeDescription {
min-height: 38px;
margin: 0 0 13px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.route-steps {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 6px;
min-height: 50px;
padding: 10px;
background: rgba(4, 14, 18, 0.24);
border: 1px solid rgba(255, 255, 255, 0.07);
}
.route-step {
display: inline-flex;
align-items: center;
gap: 5px;
color: #dce4e1;
font-size: 9px;
font-weight: 700;
}
.route-step::before {
content: "";
width: 6px;
height: 6px;
flex: 0 0 auto;
background: #78918d;
}
.route-step.threshold::before {
background: var(--role);
box-shadow: 0 0 0 3px var(--role-soft);
}
.route-step.start::before,
.route-step.destination::before {
background: #fff4cf;
box-shadow: 0 0 0 3px rgba(255, 244, 207, 0.12);
}
.route-arrow {
color: #607570;
font-size: 10px;
}
.route-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.action-button {
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.055);
color: var(--text);
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.action-button.primary {
color: #15252b;
background: var(--role);
border-color: var(--role);
}
.action-button:hover {
filter: brightness(1.12);
transform: translateY(-1px);
}
.action-button:disabled {
cursor: default;
opacity: 0.35;
transform: none;
filter: none;
}
.bottom-bar {
position: fixed;
z-index: 15;
left: 50%;
bottom: 16px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 15px;
padding: 8px 12px;
background: rgba(22, 37, 44, 0.82);
color: #e8ece8;
border: 1px solid rgba(255, 255, 255, 0.12);
box-shadow: 0 8px 24px rgba(20, 30, 32, 0.18);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.05em;
white-space: nowrap;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-swatch {
width: 14px;
height: 5px;
background: var(--role);
}
.legend-swatch.threshold {
width: 9px;
height: 9px;
background: transparent;
border: 2px solid var(--role);
}
.legend-swatch.blocked {
background: var(--danger);
}
#labels {
position: fixed;
inset: 0;
z-index: 10;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -100%);
max-width: 150px;
padding: 5px 7px;
color: #203139;
background: rgba(250, 247, 235, 0.93);
border: 1px solid rgba(32, 49, 57, 0.18);
box-shadow: 0 5px 14px rgba(28, 40, 42, 0.13);
font-size: 9px;
font-weight: 850;
line-height: 1.15;
text-align: center;
white-space: nowrap;
}
.world-label span {
display: block;
margin-top: 3px;
color: #61736f;
font-size: 7px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.world-label.legal {
border-bottom: 3px solid var(--role);
}
.world-label.legal span {
color: var(--role);
}
.world-label.blocked {
opacity: 0.72;
border-bottom: 3px solid var(--danger);
}
.world-label.blocked span {
color: var(--danger);
}
.area-label {
position: absolute;
transform: translate(-50%, -50%);
padding: 5px 9px;
color: rgba(29, 49, 55, 0.67);
background: rgba(246, 241, 222, 0.55);
border: 1px solid rgba(37, 54, 57, 0.12);
font-family: Georgia, "Times New Roman", serif;
font-size: 10px;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
#tooltip {
position: fixed;
z-index: 40;
display: none;
width: 210px;
padding: 10px 11px;
background: rgba(16, 30, 35, 0.96);
color: var(--text);
border: 1px solid rgba(255, 255, 255, 0.15);
box-shadow: var(--shadow);
pointer-events: none;
}
#tooltip strong {
display: block;
margin-bottom: 4px;
font-size: 11px;
}
#tooltip span {
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
#toast {
position: fixed;
z-index: 50;
left: 50%;
bottom: 64px;
transform: translate(-50%, 20px);
max-width: min(420px, calc(100vw - 30px));
padding: 11px 15px;
color: #fff7eb;
background: rgba(134, 43, 40, 0.95);
border: 1px solid rgba(255, 255, 255, 0.18);
box-shadow: var(--shadow);
opacity: 0;
pointer-events: none;
transition: 180ms ease;
font-size: 11px;
font-weight: 750;
text-align: center;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #d8d2c3;
color: var(--ink);
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loading-block {
text-align: center;
}
.loading-block b {
display: block;
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
font-weight: 500;
margin-bottom: 7px;
}
.loading-block span {
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
opacity: 0.6;
}
@media (max-width: 880px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.seal {
width: 39px;
height: 39px;
font-size: 21px;
}
.brand p {
display: none;
}
.role-panel {
top: 70px;
left: 12px;
right: 12px;
width: auto;
padding: 10px;
}
.role-panel .panel-heading,
.role-panel .panel-intro,
.role-panel .panel-kicker,
.guardrail {
display: none;
}
.role-list {
display: flex;
overflow-x: auto;
scrollbar-width: none;
}
.role-list::-webkit-scrollbar {
display: none;
}
.role-button {
min-width: 205px;
padding: 8px;
}
.role-glyph {
width: 36px;
height: 36px;
}
.route-panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
max-height: 42vh;
overflow: auto;
}
#routeTitle {
margin-top: 8px;
font-size: 20px;
}
#routeDescription {
min-height: 0;
margin-bottom: 9px;
}
.route-steps {
min-height: 38px;
padding: 7px;
}
.bottom-bar {
display: none;
}
#toast {
bottom: 48vh;
}
.area-label {
font-size: 8px;
}
}
@media (max-width: 520px) {
.view-button {
padding: 8px;
font-size: 9px;
}
.world-label {
max-width: 112px;
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel model of Beijing hutongs and courtyard compounds"></canvas>
<div class="vignette"></div>
<div id="labels"></div>
<header class="topbar">
<div class="brand">
<div class="seal">胡同</div>
<div>
<h1>Hutong Threshold Atlas</h1>
<p>Fictional courtyard · access-aware wayfinding</p>
</div>
</div>
<button class="view-button" id="resetView">Reset view</button>
</header>
<aside class="panel role-panel" aria-label="Choose traveler type">
<div class="panel-kicker">Choose a traveler</div>
<h2 class="panel-heading">Three routes, three permissions</h2>
<p class="panel-intro">
Select a role, then tap a destination sign. Routes follow the courtyard access graph—not straight lines.
</p>
<div class="role-list">
<button class="role-button active" data-role="visitor" aria-pressed="true">
<span class="role-glyph">访</span>
<span>
<span class="role-name">Visitor</span>
<span class="role-access">Public lane · welcome court · parlour threshold</span>
</span>
<span class="role-dot"></span>
</button>
<button class="role-button" data-role="resident" aria-pressed="false">
<span class="role-glyph">住</span>
<span>
<span class="role-name">Resident</span>
<span class="role-access">Resident court · family and study thresholds</span>
</span>
<span class="role-dot"></span>
</button>
<button class="role-button" data-role="delivery" aria-pressed="false">
<span class="role-glyph">送</span>
<span>
<span class="role-name">Delivery</span>
<span class="role-access">Service alley · kitchen · pantry threshold</span>
</span>
<span class="role-dot"></span>
</button>
</div>
<div class="guardrail">
<div class="lock">▣</div>
<div>
<strong>Graph-locked movement</strong>
<p>
Walls and private rooms have no legal shortcut edges. A blocked sign cannot be used as a waypoint.
</p>
</div>
</div>
</aside>
<section class="panel route-panel" aria-label="Active route">
<div class="route-topline">
<div class="panel-kicker">Active trace</div>
<div class="route-distance" id="routeDistance">—</div>
</div>
<h2 id="routeTitle">Welcome court</h2>
<p id="routeDescription"></p>
<div class="route-steps" id="routeSteps" aria-live="polite"></div>
<div class="route-actions">
<button class="action-button primary" id="replayRoute">Replay trace</button>
<button class="action-button" id="clearRoute">Clear route</button>
</div>
</section>
<div class="bottom-bar">
<span>Drag: orbit</span>
<span>Shift / right-drag: pan</span>
<span>Wheel: zoom</span>
<span>Tap signs: trace</span>
<span class="legend-item"><i class="legend-swatch"></i> Legal path</span>
<span class="legend-item"><i class="legend-swatch threshold"></i> Threshold</span>
<span class="legend-item"><i class="legend-swatch blocked"></i> Restricted</span>
</div>
<div id="tooltip"></div>
<div id="toast" role="status"></div>
<div id="loading">
<div class="loading-block">
<b>Building the hutongs</b>
<span>Voxel access model</span>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Core scene
// -------------------------------------------------------------------------
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d2c3);
scene.fog = new THREE.Fog(0xd8d2c3, 62, 105);
const camera = new THREE.OrthographicCamera(-25, 25, 17, -17, 0.1, 180);
const hemisphere = new THREE.HemisphereLight(0xfff3d6, 0x66716e, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d0, 3.1);
sun.position.set(-24, 38, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 36;
sun.shadow.camera.bottom = -36;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d7d2, 0.65);
fill.position.set(28, 18, -30);
scene.add(fill);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.84,
metalness: options.metalness ?? 0,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
depthWrite: options.depthWrite ?? true
});
const M = {
ground: material(0xc8bea4),
courtyard: material(0xb7aa8b),
road: material(0x777b76),
paving: material(0x94948a),
pavingAlt: material(0xa2a094),
wall: material(0x85867e),
wallCap: material(0x666b67),
roof: material(0x3e494e),
roofTop: material(0x596064),
wallLight: material(0xd0c3a2),
wood: material(0x723e2d),
red: material(0x8d392e),
window: material(0xe2b866, {
emissive: 0xb16e27,
emissiveIntensity: 0.52
}),
darkWood: material(0x3d3029),
leaf: material(0x58765a),
leafLight: material(0x718c68),
trunk: material(0x6b4d36),
stone: material(0x9a9b8d),
gold: material(0xe6b84f, {
emissive: 0xa66a12,
emissiveIntensity: 0.45
})
};
function box(w, h, d, x, y, z, mat, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function buildGround() {
box(60, 0.28, 44, 2, -0.18, -5, M.ground);
// Main hutong and eastern service alley.
box(53, 0.12, 4.1, 2, 0.015, -16, M.road);
box(4.1, 0.12, 24, 25, 0.015, -4, M.road);
// Courtyard paving.
box(17, 0.1, 21, -12.5, 0.035, -3.5, M.courtyard);
box(12, 0.1, 21, 3, 0.035, -3.5, M.courtyard);
box(13, 0.1, 21, 16.5, 0.035, -3.5, M.courtyard);
// Voxel paving slabs.
for (let x = -23; x <= 27; x += 3.1) {
box(2.7, 0.025, 0.12, x, 0.09, -16, M.pavingAlt);
box(2.7, 0.025, 0.12, x + 0.4, 0.09, -14.65, M.paving);
}
for (let z = -14; z <= 4; z += 2.7) {
box(0.12, 0.025, 2.3, 25, 0.09, z, M.pavingAlt);
box(0.12, 0.025, 2.3, 23.35, 0.09, z + 0.3, M.paving);
}
// Low hutong boundary on the north side.
box(13, 1.9, 0.65, -16.5, 0.95, -20, M.wall);
box(14, 1.9, 0.65, 10, 0.95, -20, M.wall);
box(13, 0.2, 0.85, -16.5, 1.95, -20, M.wallCap);
box(14, 0.2, 0.85, 10, 1.95, -20, M.wallCap);
}
function wall(x, z, w, d) {
box(w, 1.7, d, x, 0.85, z, M.wall);
box(w + 0.12, 0.18, d + 0.12, x, 1.78, z, M.wallCap);
}
function buildWalls() {
// Visitor compound: public opening at x = -11.
wall(-21, -3.5, 0.55, 21);
wall(-4, -3.5, 0.55, 21);
wall(-12.5, 7, 17, 0.55);
wall(-16.5, -14, 9, 0.55);
wall(-7, -14, 6, 0.55);
// Resident compound: narrow resident opening at x = -2.
wall(-3, -3.5, 0.55, 21);
wall(9, -3.5, 0.55, 21);
wall(3, 7, 12, 0.55);
wall(1, -14, 4, 0.55);
// Delivery compound: service opening through the east wall.
wall(10, -3.5, 0.55, 21);
wall(16.5, 7, 13, 0.55);
wall(16.5, -14, 13, 0.55);
wall(23, -11.5, 0.55, 5);
wall(23, 1, 0.55, 12);
// Gate posts and overhead lintel.
box(0.8, 3.2, 0.8, -11.9, 1.6, -14, M.red);
box(0.8, 3.2, 0.8, -10.1, 1.6, -14, M.red);
box(3.1, 0.42, 0.9, -11, 3.15, -14, M.roof);
box(0.8, 3.2, 0.8, -3.35, 1.6, -14, M.red);
box(0.8, 3.2, 0.8, -1.15, 1.6, -14, M.red);
box(3.0, 0.42, 0.9, -2.25, 3.15, -14, M.roof);
}
function addBuilding(x, z, w, d, options = {}) {
const body = material(options.bodyColor ?? 0xc9b994);
const beam = material(options.beamColor ?? 0x793b2d);
box(w, 2.6, d, x, 1.3, z, body);
// Stepped, blocky tiled roof.
box(w + 1.05, 0.38, d + 1.05, x, 2.72, z, M.roof);
box(w + 0.55, 0.34, d + 0.55, x, 3.06, z, M.roofTop);
box(w - 0.1, 0.3, d - 0.1, x, 3.36, z, M.roof);
// Front beam.
box(w + 0.12, 0.22, 0.18, x, 2.25, z + d / 2 + 0.08, beam);
// Front lattice windows.
const frontZ = z + d / 2 + 0.035;
const count = Math.max(2, Math.floor(w / 2.4));
for (let i = 0; i < count; i++) {
if (options.doorSide === "front" && Math.abs(i - (count - 1) / 2) < 0.25) {
continue;
}
const wx = x - w / 2 + 1.45 + i * ((w - 2.6) / Math.max(1, count - 1));
box(1.1, 1.05, 0.08, wx, 1.38, frontZ, M.window);
box(0.07, 1.15, 0.11, wx, 1.38, frontZ + 0.055, M.darkWood);
box(1.2, 0.07, 0.11, wx, 1.38, frontZ + 0.055, M.darkWood);
}
if (options.doorSide !== "west") {
box(1.05, 1.9, 0.09, x, 0.95, frontZ + 0.05, M.darkWood);
box(0.07, 1.92, 0.13, x, 0.95, frontZ + 0.09, M.red);
} else {
const westX = x - w / 2 - 0.04;
box(0.09, 1.9, 1.25, westX, 0.95, z - 0.5, M.darkWood);
box(0.13, 1.92, 0.08, westX - 0.05, 0.95, z - 0.5, M.red);
}
// Side windows.
if (w > 7) {
box(0.07, 0.95, 1.05, x + w / 2 + 0.04, 1.35, z + d * 0.18, M.window);
box(0.07, 0.95, 1.05, x - w / 2 - 0.04, 1.35, z + d * 0.18, M.window);
}
}
function buildArchitecture() {
// Visitor quarter.
addBuilding(-16, -11, 8, 6, { doorSide: "front" });
addBuilding(-13, 4.5, 10, 5, {});
addBuilding(-6.5, -1.5, 3.5, 6, {});
// Resident quarter.
addBuilding(4, -11, 10, 6, { doorSide: "west" });
addBuilding(3, 4.5, 10, 5, {});
addBuilding(7, -3.5, 4, 6, { doorSide: "front" });
// Delivery quarter.
addBuilding(13, -11, 8, 6, { doorSide: "front" });
addBuilding(19, 2.5, 6, 5, { doorSide: "front" });
addBuilding(13.5, 5.6, 6, 3.5, {});
// Shiyuan gatehouse framing the start of all routes.
box(0.8, 3.6, 1.0, -7, 1.8, -20, M.red);
box(0.8, 3.6, 1.0, -3, 1.8, -20, M.red);
box(5.8, 0.55, 1.3, -5, 3.55, -20, M.roof);
box(4.8, 0.35, 0.95, -5, 4.03, -20, M.roofTop);
box(1.25, 2.45, 0.15, -5, 1.25, -20, M.darkWood);
}
function addTree(x, z, scale = 1) {
box(0.55 * scale, 2.1 * scale, 0.55 * scale, x, 1.05 * scale, z, M.trunk);
box(2.3 * scale, 1.8 * scale, 2.15 * scale, x, 2.75 * scale, z, M.leaf);
box(1.6 * scale, 1.5 * scale, 1.55 * scale, x - 0.65 * scale, 3.65 * scale, z + 0.1, M.leafLight);
box(1.45 * scale, 1.2 * scale, 1.4 * scale, x + 0.7 * scale, 3.45 * scale, z - 0.15, M.leaf);
}
function addLantern(x, z) {
box(0.12, 2.35, 0.12, x, 1.18, z, M.darkWood);
box(0.12, 0.12, 0.9, x, 2.3, z + 0.36, M.darkWood);
box(0.55, 0.7, 0.55, x, 1.9, z + 0.72, M.red);
box(0.31, 0.31, 0.58, x, 1.9, z + 0.72, M.gold);
}
function addPot(x, z) {
box(0.8, 0.65, 0.8, x, 0.33, z, M.stone);
box(0.9, 0.15, 0.9, x, 0.7, z, M.wallCap);
addTree(x, z, 0.48);
}
function buildDecor() {
[
[-19, 3.8, 0.85],
[-7, 2.3, 0.72],
[-20, -6.5, 0.65],
[-5.8, -5.7, 0.52],
[0.2, 2.5, 0.68],
[9.2, -0.2, 0.48],
[21.8, 5.2, 0.68],
[21.7, -10.2, 0.54],
[-22, -18.6, 0.62],
[14, -18.5, 0.6]
].forEach(([x, z, s]) => addTree(x, z, s));
addPot(-19, -0.8);
addPot(-2.7, 3.5);
addPot(9.3, 3.8);
addPot(21.5, -2.6);
[
[-11.9, -14.2],
[-10.1, -14.2],
[-12.7, -7.7],
[-3.35, -14.2],
[-1.15, -14.2],
[0.2, -9.8],
[7, -6.8],
[22.7, -8.8],
[22.7, -5.1],
[13, -7.7],
[18, -0.3]
].forEach(([x, z]) => addLantern(x, z));
}
buildGround();
buildWalls();
buildArchitecture();
buildDecor();
// -------------------------------------------------------------------------
// Graph-based access model
//
// The scene is decorative. Movement is calculated only over these nodes and
// edges. There are deliberately no edges through walls or between compounds.
// -------------------------------------------------------------------------
const nodes = {
g0: { x: -5, z: -16, name: "Shiyuan Gate", type: "start" },
sW: { x: -12, z: -16, name: "West hutong", type: "street" },
sC: { x: -2, z: -16, name: "Central hutong", type: "street" },
sE: { x: 16, z: -16, name: "East hutong", type: "street" },
eT: { x: 25, z: -16, name: "Service lane", type: "service" },
eM: { x: 25, z: -7, name: "Service gate lane", type: "service" },
eS: { x: 25, z: 3, name: "South service lane", type: "service" },
v0: { x: -11, z: -13, name: "Visitor threshold", type: "threshold" },
v1: { x: -11, z: -5, name: "Welcome court", type: "court" },
v2: { x: -13, z: -8, name: "Parlour threshold", type: "threshold" },
v3: { x: -16, z: -10.5, name: "Private guest room", type: "room" },
r0: { x: -2, z: -13, name: "Resident threshold", type: "threshold" },
r1: { x: -2, z: -5, name: "Resident court", type: "court" },
r2: { x: -1, z: -10, name: "Family threshold", type: "threshold" },
r3: { x: 2, z: -10.5, name: "Family room", type: "room" },
r4: { x: 7, z: -6.5, name: "Study threshold", type: "threshold" },
r5: { x: 7, z: -4.5, name: "Family study", type: "room" },
d0: { x: 21, z: -7, name: "Service threshold", type: "threshold" },
d1: { x: 18, z: -5, name: "Delivery court", type: "court" },
d2: { x: 13, z: -8, name: "Kitchen threshold", type: "threshold" },
d3: { x: 13, z: -10.5, name: "Kitchen", type: "room" },
d4: { x: 18, z: 0, name: "Pantry threshold", type: "threshold" },
d5: { x: 18, z: 2.5, name: "Pantry store", type: "room" }
};
const ALL = ["visitor", "resident", "delivery"];
const edgeDefinitions = [
{ a: "g0", b: "sW", roles: ALL },
{ a: "sW", b: "sC", roles: ALL },
{ a: "sC", b: "sE", roles: ALL },
{ a: "sE", b: "eT", roles: ["delivery"] },
{ a: "eT", b: "eM", roles: ["delivery"] },
{ a: "eM", b: "eS", roles: ["delivery"] },
{ a: "sW", b: "v0", roles: ["visitor"] },
{ a: "v0", b: "v1", roles: ["visitor"] },
{ a: "v1", b: "v2", roles: ["visitor"] },
// Intentionally unreachable for every role: no private-room shortcut.
{ a: "v2", b: "v3", roles: [] },
{ a: "sC", b: "r0", roles: ["resident"] },
{ a: "r0", b: "r1", roles: ["resident"] },
{ a: "r1", b: "r2", roles: ["resident"] },
{ a: "r2", b: "r3", roles: ["resident"] },
{ a: "r1", b: "r4", roles: ["resident"] },
{ a: "r4", b: "r5", roles: ["resident"] },
{ a: "eM", b: "d0", roles: ["delivery"] },
{ a: "d0", b: "d1", roles: ["delivery"] },
{ a: "d1", b: "d2", roles: ["delivery"] },
{ a: "d2", b: "d3", roles: ["delivery"] },
{ a: "d1", b: "d4", roles: ["delivery"] },
{ a: "d4", b: "d5", roles: ["delivery"] }
];
const adjacency = Object.fromEntries(Object.keys(nodes).map((id) => [id, []]));
for (const edge of edgeDefinitions) {
adjacency[edge.a].push({ to: edge.b, roles: edge.roles });
adjacency[edge.b].push({ to: edge.a, roles: edge.roles });
}
function findPath(role, targetId) {
if (targetId === "g0") return ["g0"];
const queue = ["g0"];
const previous = new Map([["g0", null]]);
while (queue.length) {
const current = queue.shift();
for (const link of adjacency[current]) {
if (!link.roles.includes(role)) continue;
if (previous.has(link.to)) continue;
previous.set(link.to, current);
if (link.to === targetId) {
const path = [targetId];
let cursor = targetId;
while (previous.get(cursor) !== null) {
cursor = previous.get(cursor);
path.unshift(cursor);
}
return path;
}
queue.push(link.to);
}
}
return null;
}
const roles = {
visitor: {
name: "Visitor",
color: 0xff7657,
css: "#ff7657",
defaultTarget: "v2",
description:
"Public access only: the hutong, welcome court, and designated parlour threshold. Sleeping rooms remain blocked.",
restriction: "Private guest rooms are not traversable."
},
resident: {
name: "Resident",
color: 0x3ed7b0,
css: "#3ed7b0",
defaultTarget: "r3",
description:
"Resident access: the hutong, resident court, family room, and study thresholds within the east compound.",
restriction: "Visitor and delivery thresholds are not resident shortcuts."
},
delivery: {
name: "Delivery",
color: 0xf6c453,
css: "#f6c453",
defaultTarget: "d3",
description:
"Service access: the hutong leads to the eastern service lane, then through the service, kitchen, and pantry thresholds.",
restriction: "The service lane cannot be used by visitors or residents."
}
};
let currentRole = "visitor";
let routeState = null;
let routeGroup = null;
let walker = null;
// -------------------------------------------------------------------------
// Visual graph, thresholds, and destination markers
// -------------------------------------------------------------------------
function createSegment(aId, bId, width, y, mat, parent) {
const a = nodes[aId];
const b = nodes[bId];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const geometry = new THREE.BoxGeometry(width, 0.11, length);
geometry.translate(0, 0, length / 2);
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.set(a.x, y, a.z);
mesh.rotation.y = Math.atan2(dx, dz);
mesh.receiveShadow = true;
parent.add(mesh);
return { mesh, length };
}
const graphGroup = new THREE.Group();
scene.add(graphGroup);
const edgeVisuals = edgeDefinitions.map((edge) => {
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x789f99,
roughness: 0.7,
emissive: 0x29433e,
emissiveIntensity: 0.35,
transparent: true,
opacity: 0.25,
depthWrite: false
});
const visual = createSegment(
edge.a,
edge.b,
0.19,
0.18,
edgeMaterial,
graphGroup
);
return { ...edge, ...visual };
});
const thresholdPads = [];
const thresholdSpecs = [
{ id: "v0", roles: ["visitor"] },
{ id: "v2", roles: ["visitor"] },
{ id: "r0", roles: ["resident"] },
{ id: "r2", roles: ["resident"] },
{ id: "r4", roles: ["resident"] },
{ id: "d0", roles: ["delivery"] },
{ id: "d2", roles: ["delivery"] },
{ id: "d4", roles: ["delivery"] }
];
for (const spec of thresholdSpecs) {
const node = nodes[spec.id];
const padMaterial = new THREE.MeshStandardMaterial({
color: 0xff7657,
emissive: 0xff7657,
emissiveIntensity: 0.65,
transparent: true,
opacity: 0.55,
depthWrite: false
});
const pad = box(1.7, 0.09, 1.7, node.x, 0.17, node.z, padMaterial);
pad.castShadow = false;
const corners = new THREE.Group();
corners.position.set(node.x, 0.23, node.z);
scene.add(corners);
const cornerMat = material(roles[currentRole].color, {
emissive: roles[currentRole].color,
emissiveIntensity: 0.55
});
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(0.28, 0.12, 0.28, sx * 0.73, 0, sz * 0.73, cornerMat, corners);
}
}
thresholdPads.push({
...spec,
node,
pad,
corners,
cornerMat
});
}
const markerRecords = [];
const markerMeshes = [];
const labelsRoot = document.getElementById("labels");
function addMarker(nodeId) {
const node = nodes[nodeId];
const group = new THREE.Group();
group.position.set(node.x, node.type === "room" ? 5.25 : 2.7, node.z);
scene.add(group);
const floatGroup = new THREE.Group();
group.add(floatGroup);
const signMaterial = material(roles[currentRole].color, {
emissive: roles[currentRole].color,
emissiveIntensity: 0.65
});
const sign = new THREE.Mesh(new THREE.BoxGeometry(0.78, 0.78, 0.18), signMaterial);
sign.castShadow = true;
sign.userData.nodeId = nodeId;
floatGroup.add(sign);
box(0.11, 1.45, 0.11, 0, -1.02, 0, M.darkWood, floatGroup);
box(0.58, 0.12, 0.58, 0, -1.75, 0, M.stone, floatGroup);
const label = document.createElement("div");
label.className = "world-label";
labelsRoot.appendChild(label);
const record = {
nodeId,
node,
group,
floatGroup,
sign,
signMaterial,
label,
legal: false,
blockedUntil: 0
};
markerRecords.push(record);
markerMeshes.push(sign);
}
[
"v1",
"v2",
"v3",
"r1",
"r3",
"r5",
"d1",
"d3",
"d5"
].forEach(addMarker);
const areaLabels = [
{ text: "Shijia · Public court", x: -12, y: 0.7, z: 0.8 },
{ text: "Tongfu · Resident court", x: 3, y: 0.7, z: 0.8 },
{ text: "Houyuan · Service court", x: 16.5, y: 0.7, z: 0.8 }
].map((spec) => {
const element = document.createElement("div");
element.className = "area-label";
element.textContent = spec.text;
labelsRoot.appendChild(element);
return { ...spec, element };
});
function updateAccessVisuals() {
const role = roles[currentRole];
for (const visual of edgeVisuals) {
const permitted = visual.roles.includes(currentRole);
const roleSpecific = visual.roles.length === 1;
const color = roleSpecific ? role.color : 0x6d9d96;
visual.mesh.material.color.setHex(color);
visual.mesh.material.emissive.setHex(color);
visual.mesh.material.emissiveIntensity = permitted ? 0.28 : 0;
visual.mesh.material.opacity = permitted ? (roleSpecific ? 0.66 : 0.38) : 0.018;
}
for (const pad of thresholdPads) {
const permitted = pad.roles.includes(currentRole);
const color = permitted ? role.color : 0x755b57;
pad.pad.material.color.setHex(color);
pad.pad.material.emissive.setHex(color);
pad.pad.material.emissiveIntensity = permitted ? 0.68 : 0.04;
pad.pad.material.opacity = permitted ? 0.62 : 0.13;
pad.cornerMat.color.setHex(color);
pad.cornerMat.emissive.setHex(color);
pad.cornerMat.emissiveIntensity = permitted ? 0.6 : 0.02;
}
for (const marker of markerRecords) {
const path = findPath(currentRole, marker.nodeId);
marker.legal = Boolean(path);
const color = marker.legal ? role.color : 0xb74e48;
marker.signMaterial.color.setHex(color);
marker.signMaterial.emissive.setHex(color);
marker.signMaterial.emissiveIntensity = marker.legal ? 0.72 : 0.18;
marker.sign.scale.setScalar(marker.legal ? 1 : 0.82);
marker.label.className = `world-label ${marker.legal ? "legal" : "blocked"}`;
marker.label.innerHTML = `${marker.node.name}<span>${
marker.legal ? "✓ permitted" : "× restricted"
}</span>`;
}
}
// -------------------------------------------------------------------------
// Route creation and animation
// -------------------------------------------------------------------------
const routeTitle = document.getElementById("routeTitle");
const routeDescription = document.getElementById("routeDescription");
const routeSteps = document.getElementById("routeSteps");
const routeDistance = document.getElementById("routeDistance");
const replayButton = document.getElementById("replayRoute");
const clearButton = document.getElementById("clearRoute");
function pathDistance(path) {
let distance = 0;
for (let i = 1; i < path.length; i++) {
const a = nodes[path[i - 1]];
const b = nodes[path[i]];
distance += Math.hypot(b.x - a.x, b.z - a.z);
}
return distance;
}
function clearRoute() {
if (routeGroup) {
scene.remove(routeGroup);
routeGroup.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) child.material.dispose();
});
}
routeGroup = null;
if (walker) {
scene.remove(walker);
walker.traverse((child) => {
if (child.geometry) child.geometry.dispose();
if (child.material) child.material.dispose();
});
}
walker = null;
routeState = null;
routeTitle.textContent = "Choose a destination";
routeDescription.textContent =
"Tap a glowing sign to trace a permitted route. Red signs are informational and cannot become waypoints.";
routeSteps.innerHTML =
'<span class="route-step">No active route</span>';
routeDistance.textContent = "—";
replayButton.disabled = true;
clearButton.disabled = true;
}
function createWalker(color) {
const group = new THREE.Group();
const bodyMaterial = material(color, {
emissive: color,
emissiveIntensity: 0.38
});
const body = new THREE.Mesh(new THREE.BoxGeometry(0.72, 0.82, 0.72), bodyMaterial);
body.position.y = 0.35;
body.castShadow = true;
group.add(body);
const head = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.48, 0.48),
material(0xe6c79b)
);
head.position.y = 1.0;
head.castShadow = true;
group.add(head);
const hat = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.18, 0.62),
material(0x354a4d)
);
hat.position.y = 1.31;
hat.castShadow = true;
group.add(hat);
scene.add(group);
return group;
}
function setRoute(targetId) {
const path = findPath(currentRole, targetId);
if (!path) {
showToast(
`${nodes[targetId].name} is restricted for ${roles[currentRole].name.toLowerCase()}s. No shortcut was created.`
);
const marker = markerRecords.find((item) => item.nodeId === targetId);
if (marker) marker.blockedUntil = performance.now() + 1200;
return;
}
clearRoute();
routeGroup = new THREE.Group();
scene.add(routeGroup);
const routeMaterial = new THREE.MeshStandardMaterial({
color: roles[currentRole].color,
emissive: roles[currentRole].color,
emissiveIntensity: 0.95,
transparent: true,
opacity: 0.96,
depthWrite: false,
depthTest: false
});
const segments = [];
let start = 0;
for (let i = 1; i < path.length; i++) {
const aId = path[i - 1];
const bId = path[i];
const a = nodes[aId];
const b = nodes[bId];
const length = Math.hypot(b.x - a.x, b.z - a.z);
const dx = b.x - a.x;
const dz = b.z - a.z;
const geometry = new THREE.BoxGeometry(0.58, 0.16, length);
geometry.translate(0, 0, length / 2);
const mesh = new THREE.Mesh(geometry, routeMaterial.clone());
mesh.position.set(a.x, 0.31, a.z);
mesh.rotation.y = Math.atan2(dx, dz);
mesh.scale.z = 0.001;
mesh.renderOrder = 20;
routeGroup.add(mesh);
segments.push({
mesh,
a,
b,
length,
start,
end: start + length
});
start += length;
}
walker = createWalker(roles[currentRole].color);
routeState = {
path,
targetId,
segments,
distance: 0,
total: start,
speed: Math.max(5.2, start / 8.5)
};
const thresholdCount = path.filter(
(id) => nodes[id].type === "threshold"
).length;
routeTitle.textContent = nodes[targetId].name;
routeDescription.textContent = roles[currentRole].description;
routeDistance.textContent = `${Math.round(start)} m · ${segments.length} legs`;
replayButton.disabled = false;
clearButton.disabled = false;
routeSteps.innerHTML = path
.map((id, index) => {
const node = nodes[id];
const typeClass =
index === 0
? "start"
: index === path.length - 1
? "destination"
: node.type === "threshold"
? "threshold"
: "";
return `
<span class="route-step ${typeClass}">${node.name}</span>
${index < path.length - 1 ? '<span class="route-arrow">›</span>' : ""}
`;
})
.join("");
// Explicitly mention that this path used only legal thresholds.
routeDescription.textContent += ` ${thresholdCount} permitted threshold${
thresholdCount === 1 ? "" : "s"
} crossed.`;
}
function replayRoute() {
if (!routeState) return;
routeState.distance = 0;
for (const segment of routeState.segments) {
segment.mesh.scale.z = 0.001;
}
}
function updateWalker(dt) {
if (!routeState || !walker) return;
routeState.distance = Math.min(
routeState.total,
routeState.distance + dt * routeState.speed
);
let activeSegment = routeState.segments[0];
let localT = 0;
for (const segment of routeState.segments) {
const reveal = THREE.MathUtils.clamp(
(routeState.distance - segment.start) / segment.length,
0.001,
1
);
segment.mesh.scale.z = reveal;
if (routeState.distance >= segment.start) {
activeSegment = segment;
localT = THREE.MathUtils.clamp(
(routeState.distance - segment.start) / segment.length,
0,
1
);
}
}
const x = THREE.MathUtils.lerp(
activeSegment.a.x,
activeSegment.b.x,
localT
);
const z = THREE.MathUtils.lerp(
activeSegment.a.z,
activeSegment.b.z,
localT
);
const bob = Math.sin(performance.now() * 0.012) * 0.055;
walker.position.set(x, 0.47 + bob, z);
walker.rotation.y = Math.atan2(
activeSegment.b.x - activeSegment.a.x,
activeSegment.b.z - activeSegment.a.z
);
}
replayButton.addEventListener("click", replayRoute);
clearButton.addEventListener("click", clearRoute);
// -------------------------------------------------------------------------
// Role controls
// -------------------------------------------------------------------------
const roleButtons = [...document.querySelectorAll(".role-button")];
function setRole(role, traceDefault = true) {
currentRole = role;
const roleInfo = roles[role];
document.documentElement.style.setProperty("--role", roleInfo.css);
document.documentElement.style.setProperty(
"--role-soft",
`${roleInfo.css}2b`
);
for (const button of roleButtons) {
const active = button.dataset.role === role;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
}
updateAccessVisuals();
if (traceDefault) {
setRoute(roleInfo.defaultTarget);
} else {
clearRoute();
}
}
roleButtons.forEach((button) => {
button.addEventListener("click", () => setRole(button.dataset.role));
});
// -------------------------------------------------------------------------
// Camera controls — implemented directly, without Three.js add-ons
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(2, 0, -4),
radius: 61,
azimuth: 0.66,
polar: 0.78,
minRadius: 29,
maxRadius: 86
};
function updateCamera() {
const sinPolar = Math.sin(controls.polar);
const cameraPosition = new THREE.Vector3(
controls.target.x + controls.radius * sinPolar * Math.sin(controls.azimuth),
controls.target.y + controls.radius * Math.cos(controls.polar),
controls.target.z + controls.radius * sinPolar * Math.cos(controls.azimuth)
);
camera.position.copy(cameraPosition);
camera.lookAt(controls.target);
}
function resetView() {
controls.target.set(2, 0, -4);
controls.radius = 61;
controls.azimuth = 0.66;
controls.polar = 0.78;
updateCamera();
}
document.getElementById("resetView").addEventListener("click", resetView);
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
const tooltip = document.getElementById("tooltip");
const toast = document.getElementById("toast");
let pointerDown = false;
let pointerMoved = false;
let dragButton = 0;
let lastPointerX = 0;
let lastPointerY = 0;
function updatePointer(event) {
pointer.x = (event.clientX / window.innerWidth) * 2 - 1;
pointer.y = -(event.clientY / window.innerHeight) * 2 + 1;
}
function markerUnderPointer(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(markerMeshes, false);
return hits.length ? hits[0].object.userData.nodeId : null;
}
function showTooltip(event, nodeId) {
const marker = markerRecords.find((item) => item.nodeId === nodeId);
if (!marker) return;
tooltip.innerHTML = `
<strong>${marker.node.name}</strong>
<span>${
marker.legal
? `Permitted for ${roles[currentRole].name.toLowerCase()}s. Click to trace the legal access path.`
: `Restricted for ${roles[currentRole].name.toLowerCase()}s. This destination cannot be used as a waypoint.`
}</span>
`;
tooltip.style.left = `${Math.min(event.clientX + 15, window.innerWidth - 230)}px`;
tooltip.style.top = `${Math.min(event.clientY + 15, window.innerHeight - 90)}px`;
tooltip.style.display = "block";
}
let toastTimer = null;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2800);
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
dragButton = event.button;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
tooltip.style.display = "none";
});
canvas.addEventListener("pointermove", (event) => {
if (pointerDown) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (Math.abs(dx) + Math.abs(dy) > 2) pointerMoved = true;
const panning = dragButton === 2 || dragButton === 1 || event.shiftKey;
if (panning) {
const scale = controls.radius / window.innerHeight * 1.3;
const rightX = Math.cos(controls.azimuth);
const rightZ = -Math.sin(controls.azimuth);
controls.target.x -= dx * scale * rightX;
controls.target.z -= dx * scale * rightZ;
controls.target.x += dy * scale * Math.sin(controls.azimuth);
controls.target.z += dy * scale * Math.cos(controls.azimuth);
} else {
controls.azimuth -= dx * 0.006;
controls.polar = THREE.MathUtils.clamp(
controls.polar + dy * 0.005,
0.42,
1.22
);
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
return;
}
const nodeId = markerUnderPointer(event);
if (nodeId) showTooltip(event, nodeId);
else tooltip.style.display = "none";
});
canvas.addEventListener("pointerup", (event) => {
pointerDown = false;
if (!pointerMoved) {
const nodeId = markerUnderPointer(event);
if (nodeId) setRoute(nodeId);
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener("pointerleave", () => {
if (!pointerDown) tooltip.style.display = "none";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
controls.minRadius,
controls.maxRadius
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "1") setRole("visitor");
if (event.key === "2") setRole("resident");
if (event.key === "3") setRole("delivery");
if (event.key.toLowerCase() === "r") replayRoute();
if (event.key === "Escape") clearRoute();
});
// -------------------------------------------------------------------------
// Labels, resizing, and render loop
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function projectElement(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
element.style.display = visible ? "block" : "none";
if (visible) {
const screenX = (projected.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const viewHeight = Math.max(34, 58 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const now = performance.now();
updateWalker(dt);
for (const pad of thresholdPads) {
const permitted = pad.roles.includes(currentRole);
const pulse = permitted ? 1 + Math.sin(now * 0.004 + pad.node.x) * 0.07 : 1;
pad.pad.scale.set(pulse, 1, pulse);
pad.corners.rotation.y = Math.sin(now * 0.001 + pad.node.z) * 0.035;
}
for (const marker of markerRecords) {
marker.floatGroup.position.y =
Math.sin(now * 0.0025 + marker.node.x) * 0.12;
if (marker.blockedUntil > now) {
const flash = 0.5 + Math.sin(now * 0.018) * 0.5;
marker.signMaterial.emissiveIntensity = 0.2 + flash * 0.9;
}
projectElement(
marker.label,
marker.node.x,
marker.group.position.y + 1.1,
marker.node.z
);
}
for (const label of areaLabels) {
projectElement(label.element, label.x, label.y, label.z);
}
renderer.render(scene, camera);
}
resize();
setRole("visitor", true);
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```