Prompt specification
Build or customize the Three.js 3D scene "Temporary shelter runoff puzzle" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Temporary shelter runoff puzzle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/temporary-shelter-runoff-puzzle-x0484
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring market-awning drainage. Create modular awnings above a plaza grid; users orient slopes and connect gutters so prescribed water paths reach collection points without crossing designated pedestrian entrances. Voxel style.
Working Three.js Starter (temporary-shelter-runoff-puzzle-x0484):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Plaza Flow — Market Awning Drainage</title>
<style>
:root {
--ink: #17272b;
--muted: #64777a;
--paper: #f5f0e7;
--panel: rgba(22, 38, 42, 0.95);
--line: rgba(255, 255, 255, 0.13);
--cyan: #39d5d0;
--cyan-dark: #167c80;
--coral: #ed745d;
--gold: #efb84d;
--safe: #51d49c;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e2d7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
white-space: nowrap;
user-select: none;
}
.bay-label {
display: flex;
align-items: center;
gap: 5px;
padding: 4px 7px;
border: 1px solid rgba(20, 38, 42, 0.12);
border-radius: 5px;
background: rgba(255, 253, 248, 0.88);
box-shadow: 0 3px 10px rgba(26, 39, 40, 0.12);
color: #304346;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
backdrop-filter: blur(5px);
}
.bay-label::before {
content: "";
width: 6px;
height: 6px;
border-radius: 1px;
background: var(--coral);
}
.bay-label.safe::before { background: var(--safe); }
.bay-label.selected { border-color: var(--gold); background: #fff8df; }
.bay-label.selected::before { background: var(--gold); }
.place-label {
padding: 6px 9px;
border-radius: 5px;
background: rgba(19, 37, 41, 0.9);
color: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
box-shadow: 0 6px 16px rgba(20, 30, 32, 0.18);
}
.place-label.collection {
border: 1px solid rgba(57, 213, 208, 0.55);
color: #8ff8ef;
}
.place-label.entry {
border: 1px solid rgba(237, 116, 93, 0.6);
color: #ffb4a5;
background: rgba(70, 37, 35, 0.88);
}
.brand {
position: fixed;
top: 18px;
left: 20px;
z-index: 10;
display: flex;
align-items: center;
gap: 12px;
pointer-events: none;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border-radius: 10px;
background: var(--ink);
color: var(--cyan);
font-size: 22px;
box-shadow: 0 8px 25px rgba(23, 39, 43, 0.2);
}
.brand h1 {
margin: 0;
font-size: 16px;
line-height: 1.1;
letter-spacing: 0.02em;
}
.brand p {
margin: 4px 0 0;
color: #617174;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.panel {
position: fixed;
z-index: 20;
left: 20px;
top: 80px;
width: 304px;
max-height: calc(100vh - 100px);
overflow: auto;
padding: 17px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel);
color: #f4f7f4;
box-shadow: 0 18px 55px rgba(20, 32, 34, 0.24);
backdrop-filter: blur(15px);
}
.eyebrow {
color: #8ca1a3;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.module-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-top: 7px;
}
.module-head h2 {
margin: 0;
font-size: 25px;
letter-spacing: -0.03em;
}
.module-state {
margin-top: 6px;
color: #aebcbd;
font-size: 11px;
}
.status-pill {
padding: 5px 7px;
border-radius: 5px;
background: rgba(237, 116, 93, 0.15);
color: #ffad9d;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.1em;
white-space: nowrap;
}
.status-pill.safe {
background: rgba(81, 212, 156, 0.13);
color: #80e7ba;
}
.divider {
height: 1px;
margin: 15px 0;
background: var(--line);
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
color: #dce6e5;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.control-title span {
color: #829799;
font-size: 9px;
font-weight: 700;
letter-spacing: 0;
text-transform: none;
}
.compass {
display: grid;
grid-template-columns: repeat(3, 42px);
grid-template-rows: repeat(3, 38px);
justify-content: center;
gap: 4px;
}
.compass button,
.action {
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
background: rgba(255, 255, 255, 0.055);
color: #e8eeee;
cursor: pointer;
transition: 140ms ease;
}
.compass button:hover,
.action:hover {
border-color: rgba(57, 213, 208, 0.55);
background: rgba(57, 213, 208, 0.1);
transform: translateY(-1px);
}
.compass button.active {
border-color: var(--cyan);
background: var(--cyan);
color: #103638;
box-shadow: 0 0 0 2px rgba(57, 213, 208, 0.12);
}
.compass .north { grid-column: 2; grid-row: 1; }
.compass .east { grid-column: 3; grid-row: 2; }
.compass .south { grid-column: 2; grid-row: 3; }
.compass .west { grid-column: 1; grid-row: 2; }
.compass .center {
grid-column: 2;
grid-row: 2;
display: grid;
place-items: center;
color: #779092;
font-size: 10px;
font-weight: 900;
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.action {
min-height: 39px;
padding: 8px 9px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
}
.action.primary {
border-color: rgba(57, 213, 208, 0.48);
background: rgba(57, 213, 208, 0.13);
color: #9bf8f0;
}
.action.primary.active {
background: var(--cyan);
color: #113638;
}
.action.danger:hover {
border-color: rgba(237, 116, 93, 0.55);
background: rgba(237, 116, 93, 0.11);
}
.hint {
margin: 10px 0 0;
color: #93a7a9;
font-size: 10px;
line-height: 1.5;
}
.hint strong { color: #e6eeee; }
.progress-head {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: #dce7e6;
font-size: 11px;
font-weight: 700;
}
.progress-track {
height: 7px;
overflow: hidden;
border-radius: 8px;
background: rgba(255, 255, 255, 0.09);
}
.progress-fill {
width: 87.5%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #1ca9aa, #4be2d5);
transition: width 300ms ease;
}
.bin-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.bin-stat {
padding: 9px;
border: 1px solid rgba(57, 213, 208, 0.16);
border-radius: 8px;
background: rgba(57, 213, 208, 0.055);
}
.bin-stat strong {
display: block;
color: #8ff4ed;
font-size: 18px;
}
.bin-stat span {
color: #8da3a5;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.objective {
position: fixed;
z-index: 12;
top: 18px;
right: 20px;
width: 270px;
padding: 13px 15px;
border: 1px solid rgba(23, 39, 43, 0.12);
border-radius: 12px;
background: rgba(250, 247, 239, 0.9);
box-shadow: 0 12px 34px rgba(23, 39, 43, 0.12);
backdrop-filter: blur(10px);
pointer-events: none;
}
.objective h3 {
margin: 5px 0 3px;
font-size: 14px;
}
.objective p {
margin: 0;
color: #6b7a7c;
font-size: 10px;
line-height: 1.45;
}
.legend {
position: fixed;
z-index: 12;
left: 20px;
bottom: 18px;
display: flex;
gap: 15px;
padding: 10px 13px;
border: 1px solid rgba(23, 39, 43, 0.1);
border-radius: 10px;
background: rgba(250, 247, 239, 0.88);
box-shadow: 0 8px 25px rgba(23, 39, 43, 0.1);
color: #536568;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.04em;
pointer-events: none;
backdrop-filter: blur(8px);
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--cyan);
}
.swatch.gold { background: var(--gold); }
.swatch.coral { background: var(--coral); }
.navigation-hint {
position: fixed;
z-index: 12;
right: 20px;
bottom: 18px;
padding: 9px 12px;
border-radius: 9px;
background: rgba(23, 39, 43, 0.85);
color: #c6d4d4;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.03em;
pointer-events: none;
}
#toast {
position: fixed;
z-index: 50;
left: 50%;
bottom: 72px;
max-width: min(520px, calc(100vw - 32px));
transform: translate(-50%, 15px);
padding: 10px 15px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
background: rgba(20, 37, 41, 0.95);
color: #eff8f6;
box-shadow: 0 12px 35px rgba(20, 35, 38, 0.25);
font-size: 11px;
opacity: 0;
pointer-events: none;
transition: 180ms ease;
}
#toast.show {
transform: translate(-50%, 0);
opacity: 1;
}
#toast.error {
border-color: rgba(237, 116, 93, 0.6);
background: rgba(76, 39, 36, 0.96);
}
@media (max-width: 760px) {
.brand { left: 12px; top: 12px; }
.brand h1 { font-size: 13px; }
.brand p { font-size: 8px; }
.brand-mark { width: 36px; height: 36px; }
.panel {
left: 12px;
right: 12px;
top: auto;
bottom: 12px;
width: auto;
max-height: 43vh;
padding: 14px;
}
.objective {
top: 12px;
right: 12px;
width: 195px;
padding: 10px 12px;
}
.objective h3 { font-size: 12px; }
.objective p { font-size: 9px; }
.legend, .navigation-hint { display: none; }
.compass { grid-template-columns: repeat(3, 36px); }
.compass button { font-size: 12px; }
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div id="labels"></div>
<header class="brand">
<div class="brand-mark">≋</div>
<div>
<h1>Plaza Flow</h1>
<p>Market awning drainage</p>
</div>
</header>
<aside class="panel">
<div class="eyebrow">Active module</div>
<div class="module-head">
<div>
<h2 id="selectedName">B4</h2>
<div class="module-state" id="selectedStatus">Inspecting drainage route</div>
</div>
<div class="status-pill" id="selectedBadge">OPEN</div>
</div>
<div class="divider"></div>
<div class="control-title">
Slope direction
<span>Low edge / outlet</span>
</div>
<div class="compass" aria-label="Slope direction">
<button class="north" data-dir="N" title="North">↑</button>
<button class="east" data-dir="E" title="East">→</button>
<div class="center">↓</div>
<button class="west" data-dir="W" title="West">←</button>
<button class="south" data-dir="S" title="South">↓</button>
</div>
<div class="actions">
<button class="action primary" id="linkButton">Link gutters</button>
<button class="action danger" id="cutButton">Remove outlet</button>
</div>
<p class="hint" id="toolHint">
Select an awning, then choose its downhill edge. Use <strong>Link gutters</strong>
to connect it to an adjacent module.
</p>
<div class="divider"></div>
<div class="progress-head">
<span>Water containment</span>
<span id="progressText">14 / 16 modules</span>
</div>
<div class="progress-track">
<div class="progress-fill" id="progressFill"></div>
</div>
<div class="bin-row">
<div class="bin-stat">
<strong id="leftCount">7</strong>
<span>West cistern</span>
</div>
<div class="bin-stat">
<strong id="rightCount">7</strong>
<span>East cistern</span>
</div>
</div>
<div class="divider"></div>
<div class="actions">
<button class="action" id="resetButton">Reset layout</button>
<button class="action primary" id="demoButton">Complete routes</button>
</div>
</aside>
<section class="objective">
<div class="eyebrow">Drainage brief</div>
<h3>Capture every market bay</h3>
<p>
Route runoff to either north cistern without crossing the two red pedestrian
entrance clearances.
</p>
</section>
<div class="legend">
<span><i class="swatch"></i>Captured water</span>
<span><i class="swatch gold"></i>Selected bay</span>
<span><i class="swatch coral"></i>Open spill</span>
</div>
<div class="navigation-hint">Drag to orbit · Scroll to zoom · Click awning to inspect</div>
<div id="toast"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const canvas = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee8de);
scene.fog = new THREE.Fog(0xeee8de, 35, 65);
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.05;
const camera = new THREE.OrthographicCamera(-10, 10, 10, -10, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 1.5, 0);
const cameraState = {
azimuth: 0.72,
polar: 0.78,
radius: 30,
zoom: 1
};
function updateCamera() {
const { azimuth, polar, radius } = cameraState;
camera.position.set(
cameraTarget.x + radius * Math.sin(polar) * Math.sin(azimuth),
cameraTarget.y + radius * Math.cos(polar),
cameraTarget.z + radius * Math.sin(polar) * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
}
function updateProjection() {
const aspect = window.innerWidth / window.innerHeight;
const halfHeight = 10.8 / cameraState.zoom;
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
updateCamera();
updateProjection();
scene.add(new THREE.HemisphereLight(0xfff5df, 0x72888a, 2.25));
const sun = new THREE.DirectionalLight(0xfff1d1, 3.2);
sun.position.set(-12, 20, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -17;
sun.shadow.camera.right = 17;
sun.shadow.camera.top = 17;
sun.shadow.camera.bottom = -17;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rim = new THREE.DirectionalLight(0xbde9ec, 1.1);
rim.position.set(15, 8, -12);
scene.add(rim);
// -------------------------------------------------------------------------
// Materials and voxel helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color, options = {}) {
const key = color + JSON.stringify(options);
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0.02,
...options
})
);
}
return materialCache.get(key);
}
function box(width, height, depth, color, x, y, z, parent = scene, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
options.material || material(color, options)
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function clearGroup(group) {
while (group.children.length) {
const child = group.children.pop();
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
group.remove(child);
}
}
function makeBeam(start, end, width, height, color, parent, options = {}) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = box(width, height, length, color, 0, 0, 0, parent, options);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction.normalize()
);
return mesh;
}
// -------------------------------------------------------------------------
// Plaza ground and environment
// -------------------------------------------------------------------------
const environment = new THREE.Group();
scene.add(environment);
box(
80, 0.25, 80, 0xd9d4ca, 0, -0.55, 0, environment,
{ castShadow: false }
);
box(
22.2, 0.55, 21.5, 0x9eaaa4, 0, -0.26, 0, environment
);
const tileColors = [0xe8e0d2, 0xded8ca, 0xeee7d9, 0xd9d4c8];
for (let r = 0; r < 5; r++) {
for (let c = 0; c < 5; c++) {
const x = (c - 2) * 4;
const z = (r - 2) * 4;
box(
3.84, 0.12, 3.84,
tileColors[(r + c) % tileColors.length],
x, 0.025, z, environment
);
}
}
function addPlanter(x, z, accent) {
const group = new THREE.Group();
group.position.set(x, 0, z);
environment.add(group);
box(2.1, 0.65, 1.5, 0x77837b, 0, 0.35, 0, group);
box(1.85, 0.12, 1.26, 0x4f6257, 0, 0.71, 0, group);
box(0.45, 1.6, 0.45, 0x755d48, -0.45, 1.25, 0, group);
box(0.45, 1.15, 0.45, 0x755d48, 0.4, 1.05, 0.1, group);
box(1.4, 1.1, 1.1, accent, -0.45, 2.25, 0, group);
box(1.1, 0.9, 1.25, 0x789b72, 0.35, 2.1, 0.08, group);
box(0.8, 0.65, 0.8, 0x94ae7f, 0.4, 2.75, 0.08, group);
}
addPlanter(-10, -7.7, 0x5e8064);
addPlanter(10, -7.7, 0x6f8b64);
addPlanter(-10, 7.8, 0x638469);
addPlanter(10, 7.8, 0x587866);
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
environment.add(group);
box(2.2, 0.18, 0.65, 0x8a6247, 0, 0.75, 0, group);
box(2.2, 0.75, 0.16, 0x8a6247, 0, 1.15, -0.27, group);
box(0.16, 0.72, 0.55, 0x4b4c45, -0.8, 0.36, 0, group);
box(0.16, 0.72, 0.55, 0x4b4c45, 0.8, 0.36, 0, group);
}
addBench(-9.6, 2.1, Math.PI / 2);
addBench(9.6, 2.1, -Math.PI / 2);
addBench(-3.4, -9.8);
addBench(3.4, -9.8);
// -------------------------------------------------------------------------
// Drainage layout
// -------------------------------------------------------------------------
const COLS = 4;
const ROWS = 4;
const SPACING = 4.4;
const MODULE_COUNT = COLS * ROWS;
const DIRS = {
N: { name: "North", dx: 0, dz: -1, v: new THREE.Vector3(0, 0, -1), angle: Math.PI },
E: { name: "East", dx: 1, dz: 0, v: new THREE.Vector3(1, 0, 0), angle: Math.PI / 2 },
S: { name: "South", dx: 0, dz: 1, v: new THREE.Vector3(0, 0, 1), angle: 0 },
W: { name: "West", dx: -1, dz: 0, v: new THREE.Vector3(-1, 0, 0), angle: -Math.PI / 2 }
};
const accentColors = [
0xd96e52, 0x4f8f87, 0xd8a546, 0x6e8f68, 0xb97067, 0x577d92
];
const modules = [];
const pickables = [];
const links = new Map();
const linkKey = (source, target) => `${source}-${target}`;
const connectionsGroup = new THREE.Group();
const collectionPipesGroup = new THREE.Group();
const flowGroup = new THREE.Group();
scene.add(connectionsGroup, collectionPipesGroup, flowGroup);
function moduleId(row, col) {
return row * COLS + col;
}
function moduleName(row, col) {
return `${String.fromCharCode(65 + col)}${row + 1}`;
}
function neighborId(module, dirName) {
const dir = DIRS[dirName];
const row = module.row + dir.dz;
const col = module.col + dir.dx;
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) return -1;
return moduleId(row, col);
}
function outletPosition(module) {
const dir = DIRS[module.dir];
return new THREE.Vector3(
module.x + dir.v.x * 1.76,
4.54,
module.z + dir.v.z * 1.76
);
}
function inletPosition(target, sourceDir) {
const dir = DIRS[sourceDir];
return new THREE.Vector3(
target.x - dir.v.x * 1.76,
4.54,
target.z - dir.v.z * 1.76
);
}
function createModule(row, col) {
const id = moduleId(row, col);
const x = (col - (COLS - 1) / 2) * SPACING;
const z = (row - (ROWS - 1) / 2) * SPACING;
const accent = accentColors[(row * 2 + col) % accentColors.length];
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
// Support posts and market furniture.
for (const px of [-1.43, 1.43]) {
for (const pz of [-1.43, 1.43]) {
box(0.14, 4.45, 0.14, 0x695749, px, 2.25, pz, group);
box(0.25, 0.12, 0.25, 0x4e463e, px, 0.2, pz, group);
}
}
box(
2.55, 0.18, 0.22, 0x665548, 0, 4.43, -1.43, group
);
box(
2.55, 0.18, 0.22, 0x665548, 0, 4.43, 1.43, group
);
box(1.7, 0.58, 0.9, 0x8d694c, 0.48, 0.48, 0.52, group);
box(1.82, 0.12, 1.02, accent, 0.48, 0.83, 0.52, group);
box(0.65, 0.48, 0.62, 0xb78a58, -0.78, 0.32, 0.72, group);
box(0.72, 0.12, 0.68, 0xd7b97e, -0.78, 0.62, 0.72, group);
box(0.38, 0.35, 0.38, 0xd2a84e, 0.72, 1.08, 0.53, group);
const roofPivot = new THREE.Group();
group.add(roofPivot);
// Five stepped strips form a visibly voxelized low edge.
for (let strip = 0; strip < 5; strip++) {
const stripZ = -1.32 + strip * 0.66;
const stripY = 5.22 - strip * 0.12;
const color = strip % 2 === 0 ? 0xf0d9b5 : accent;
const stripMesh = box(
3.34, 0.18, 0.64, color, 0, stripY, stripZ, roofPivot
);
stripMesh.userData.moduleId = id;
pickables.push(stripMesh);
}
// Low-edge gutter and block valance.
const gutter = box(
3.38, 0.18, 0.24, 0x28555a, 0, 4.53, 1.76, roofPivot
);
gutter.userData.moduleId = id;
pickables.push(gutter);
for (let tab = 0; tab < 7; tab++) {
const tabMesh = box(
0.39, 0.36, 0.18,
tab % 2 === 0 ? accent : 0xf0d9b5,
-1.32 + tab * 0.44, 4.31, 1.76, roofPivot
);
tabMesh.userData.moduleId = id;
pickables.push(tabMesh);
}
// Small cyan voxel arrow pointing toward the downhill edge.
const arrowY = 5.12;
box(0.13, 0.07, 0.58, 0x35bfc0, 0, arrowY, 0.42, roofPivot);
const headA = box(
0.13, 0.07, 0.42, 0x35bfc0, -0.1, arrowY, 0.75, roofPivot
);
headA.rotation.y = -Math.PI / 4;
const headB = box(
0.13, 0.07, 0.42, 0x35bfc0, 0.1, arrowY, 0.75, roofPivot
);
headB.rotation.y = Math.PI / 4;
// Selection frame and status beacon.
const selection = new THREE.Group();
group.add(selection);
const selectionMaterial = material(0xf1b542, {
emissive: 0x5c3900,
emissiveIntensity: 0.25
});
box(3.55, 0.08, 0.08, 0, 0, 0, 0, selection, {
material: selectionMaterial
});
box(3.55, 0.08, 0.08, 0, 0, 0, 0, selection, {
material: selectionMaterial
});
box(0.08, 0.08, 3.55, 0, 0, 0, 0, selection, {
material: selectionMaterial
});
box(0.08, 0.08, 3.55, 0, 0, 0, 0, selection, {
material: selectionMaterial
});
selection.children[0].position.set(0, 0.16, -1.77);
selection.children[1].position.set(0, 0.16, 1.77);
selection.children[2].position.set(-1.77, 0.16, 0);
selection.children[3].position.set(1.77, 0.16, 0);
selection.visible = false;
const beacon = box(
0.25, 0.25, 0.25, 0xed745d, x, 5.66, z, scene,
{ material: material(0xed745d, { emissive: 0x7a2417, emissiveIntensity: 0.25 }) }
);
const label = document.createElement("div");
label.className = "world-label bay-label";
label.textContent = moduleName(row, col);
document.getElementById("labels").appendChild(label);
const module = {
id, row, col, x, z,
dir: "S",
accent,
group,
roofPivot,
selection,
beacon,
label,
status: "leak",
reason: "",
flowPoints: [],
flowLength: 0,
particles: []
};
modules.push(module);
const particleMaterial = material(0x55eee0, {
emissive: 0x1aa6a2,
emissiveIntensity: 0.8,
roughness: 0.3
});
for (let p = 0; p < 2; p++) {
const particle = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.14, 0.14),
particleMaterial
);
particle.visible = false;
flowGroup.add(particle);
module.particles.push(particle);
}
return module;
}
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) createModule(row, col);
}
// -------------------------------------------------------------------------
// Pedestrian entrance clearances
// -------------------------------------------------------------------------
const gates = [
{ name: "ENTRY 01", x: -4.4, minX: -5.02, maxX: -3.78, minZ: 6.45, maxZ: 8.55 },
{ name: "ENTRY 02", x: 4.4, minX: 3.78, maxX: 5.02, minZ: 6.45, maxZ: 8.55 }
];
const gateGroup = new THREE.Group();
scene.add(gateGroup);
for (const gate of gates) {
const centerZ = (gate.minZ + gate.maxZ) / 2;
box(
1.18, 0.035, gate.maxZ - gate.minZ,
0xde725e, gate.x, 0.105, centerZ, gateGroup,
{ castShadow: false }
);
for (let stripe = 0; stripe < 5; stripe++) {
box(
0.18, 0.04, 0.65, 0xf2d8bf,
gate.x - 0.44 + stripe * 0.22,
0.135,
centerZ,
gateGroup,
{ castShadow: false }
);
}
const clearance = box(
1.16, 4.75, gate.maxZ - gate.minZ,
0xed745d, gate.x, 2.38, centerZ, gateGroup,
{
castShadow: false,
receiveShadow: false,
material: material(0xed745d, {
transparent: true,
opacity: 0.09,
depthWrite: false
})
}
);
box(0.12, 1.35, 0.12, 0x7e5147, gate.x - 0.61, 0.68, 7.7, gateGroup);
box(0.12, 1.35, 0.12, 0x7e5147, gate.x + 0.61, 0.68, 7.7, gateGroup);
const label = document.createElement("div");
label.className = "world-label place-label entry";
label.textContent = gate.name;
document.getElementById("labels").appendChild(label);
gate.label = label;
gate.anchor = new THREE.Vector3(gate.x, 1.25, 7.9);
}
function segmentIntersectsRect(a, b, rect) {
let tMin = 0;
let tMax = 1;
const axes = [
["x", rect.minX, rect.maxX],
["z", rect.minZ, rect.maxZ]
];
for (const [axis, min, max] of axes) {
const delta = b[axis] - a[axis];
const origin = a[axis];
if (Math.abs(delta) < 0.000001) {
if (origin < min || origin > max) return false;
} else {
let t1 = (min - origin) / delta;
let t2 = (max - origin) / delta;
if (t1 > t2) [t1, t2] = [t2, t1];
tMin = Math.max(tMin, t1);
tMax = Math.min(tMax, t2);
if (tMin > tMax) return false;
}
}
return true;
}
// -------------------------------------------------------------------------
// Collection cisterns
// -------------------------------------------------------------------------
const cisterns = [
{ id: 0, x: -6.6, z: -9.45, anchor: new THREE.Vector3(-6.6, 2.3, -9.45) },
{ id: 1, x: 6.6, z: -9.45, anchor: new THREE.Vector3( 6.6, 2.3, -9.45) }
];
for (const cistern of cisterns) {
const group = new THREE.Group();
group.position.set(cistern.x, 0, cistern.z);
scene.add(group);
box(3.0, 0.36, 2.45, 0x405b5b, 0, 0.2, 0, group);
box(2.66, 0.12, 2.12, 0x193d40, 0, 0.48, 0, group);
box(0.24, 0.88, 2.45, 0x315d5c, -1.38, 0.7, 0, group);
box(0.24, 0.88, 2.45, 0x315d5c, 1.38, 0.7, 0, group);
box(3.0, 0.88, 0.24, 0x315d5c, 0, 0.7, -1.1, group);
box(3.0, 0.88, 0.24, 0x315d5c, 0, 0.7, 1.1, group);
cistern.water = box(
2.52, 0.28, 1.98, 0x34c9c6, 0, 0.58, 0, group,
{
material: material(0x34c9c6, {
emissive: 0x117c7c,
emissiveIntensity: 0.35,
transparent: true,
opacity: 0.9
})
}
);
box(0.55, 1.4, 0.55, 0x2d5050, 0, 1.15, 0, group);
box(0.85, 0.25, 0.85, 0x4bcac4, 0, 1.95, 0, group);
const label = document.createElement("div");
label.className = "world-label place-label collection";
label.textContent = cistern.id === 0 ? "WEST CISTERN" : "EAST CISTERN";
document.getElementById("labels").appendChild(label);
cistern.label = label;
cistern.count = 0;
}
// -------------------------------------------------------------------------
// Initial network
// -------------------------------------------------------------------------
const initialDirections = new Array(MODULE_COUNT);
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const id = moduleId(row, col);
if (col === 0 || col === 3) initialDirections[id] = "N";
else if (col === 1) initialDirections[id] = row === 3 ? "N" : "W";
else initialDirections[id] = row === 3 ? "N" : "E";
}
}
const initialLinks = [];
for (let row = 0; row < ROWS; row++) {
initialLinks.push([moduleId(row, 0), moduleId(row - 1, 0)]);
initialLinks.push([moduleId(row, 3), moduleId(row - 1, 3)]);
}
for (let row = 0; row < 3; row++) {
initialLinks.push([moduleId(row, 1), moduleId(row, 0)]);
initialLinks.push([moduleId(row, 2), moduleId(row, 3)]);
}
// Two south-center modules intentionally begin with open outlets.
initialLinks.push([moduleId(3, 1), moduleId(2, 1)]);
initialLinks.push([moduleId(3, 2), moduleId(2, 2)]);
function resetLayout(showToast = true) {
links.clear();
modules.forEach((module, id) => {
module.dir = initialDirections[id];
module.roofPivot.rotation.y = DIRS[module.dir].angle;
});
for (const [source, target] of initialLinks) {
if (source >= 0 && target >= 0) {
links.set(linkKey(source, target), { source, target });
}
}
selectedId = moduleId(3, 1);
linkMode = false;
linkSource = null;
rebuildNetwork();
updateUI();
if (showToast) toast("Layout reset. Two south-center outlets remain open.");
}
// -------------------------------------------------------------------------
// Simulation and path construction
// -------------------------------------------------------------------------
let safeCount = 0;
let leftCount = 0;
let rightCount = 0;
function collectionForModule(module) {
if (module.row === 0 && module.dir === "N") {
if (module.col === 0) return cisterns[0];
if (module.col === 3) return cisterns[1];
}
return null;
}
function computeFlow() {
safeCount = 0;
leftCount = 0;
rightCount = 0;
cisterns.forEach((cistern) => cistern.count = 0);
for (const module of modules) {
const path = [];
const visited = new Set();
let current = module;
let endpoint = null;
let reason = "OUTLET OPEN";
while (true) {
if (visited.has(current.id)) {
reason = "DRAIN LOOP";
break;
}
visited.add(current.id);
path.push(current.id);
const collection = collectionForModule(current);
if (collection) {
endpoint = collection;
reason = "CAPTURED";
break;
}
const nextId = neighborId(current, current.dir);
if (nextId >= 0 && links.has(linkKey(current.id, nextId))) {
if (links.has(linkKey(nextId, current.id))) {
reason = "DRAIN LOOP";
break;
}
current = modules[nextId];
continue;
}
if (nextId < 0) {
reason = "SPILL BEYOND PLAZA";
} else {
reason = "GUTTER NOT CONNECTED";
}
break;
}
module.status = endpoint ? "safe" : "leak";
module.reason = reason;
module.flowPoints = [];
if (endpoint) {
safeCount++;
endpoint.count++;
if (endpoint.id === 0) leftCount++;
else rightCount++;
const points = [];
const first = modules[path[0]];
points.push(new THREE.Vector3(first.x, 5.47, first.z));
for (let i = 0; i < path.length; i++) {
const currentModule = modules[path[i]];
points.push(outletPosition(currentModule));
const nextId = path[i + 1];
if (nextId !== undefined) {
const nextModule = modules[nextId];
points.push(inletPosition(nextModule, currentModule.dir));
points.push(new THREE.Vector3(nextModule.x, 5.47, nextModule.z));
}
}
points.push(new THREE.Vector3(endpoint.x, 1.0, endpoint.z));
module.flowPoints = points;
let length = 0;
for (let i = 1; i < points.length; i++) {
length += points[i].distanceTo(points[i - 1]);
}
module.flowLength = length;
}
}
}
function samplePath(points, distance) {
if (points.length < 2) return new THREE.Vector3();
let remaining = distance;
for (let i = 1; i < points.length; i++) {
const segmentLength = points[i].distanceTo(points[i - 1]);
if (remaining <= segmentLength) {
const t = segmentLength === 0 ? 0 : remaining / segmentLength;
return points[i - 1].clone().lerp(points[i], t);
}
remaining -= segmentLength;
}
return points[points.length - 1].clone();
}
// -------------------------------------------------------------------------
// Network rendering
// -------------------------------------------------------------------------
function rebuildNetwork() {
clearGroup(connectionsGroup);
clearGroup(collectionPipesGroup);
const pipeMaterial = material(0x246d70, {
metalness: 0.08,
roughness: 0.62
});
const collarMaterial = material(0x5ab8b5, {
emissive: 0x0d4a4b,
emissiveIntensity: 0.18
});
for (const link of links.values()) {
const source = modules[link.source];
const target = modules[link.target];
const start = outletPosition(source);
const end = inletPosition(target, source.dir);
makeBeam(start, end, 0.2, 0.17, 0x246d70, connectionsGroup, {
material: pipeMaterial
});
box(0.34, 0.29, 0.34, 0x5ab8b5, start.x, start.y, start.z,
connectionsGroup, { material: collarMaterial });
box(0.34, 0.29, 0.34, 0x5ab8b5, end.x, end.y, end.z,
connectionsGroup, { material: collarMaterial });
}
for (const cistern of cisterns) {
const module = modules[moduleId(0, cistern.id === 0 ? 0 : 3)];
if (module.dir !== "N") continue;
const start = outletPosition(module);
const end = new THREE.Vector3(cistern.x, 1.85, cistern.z - 0.25);
makeBeam(start, end, 0.22, 0.19, 0x2b8988, collectionPipesGroup, {
material: material(0x2b8988, {
emissive: 0x0b4444,
emissiveIntensity: 0.18
})
});
box(0.38, 0.34, 0.38, 0x60d3cd, start.x, start.y, start.z,
collectionPipesGroup);
}
computeFlow();
updateModuleVisuals();
}
function updateModuleVisuals() {
for (const module of modules) {
module.beacon.material.color.setHex(
module.status === "safe" ? 0x51d49c : 0xed745d
);
module.beacon.material.emissive.setHex(
module.status === "safe" ? 0x155f3d : 0x7a2417
);
module.label.className =
"world-label bay-label " +
(module.status === "safe" ? "safe " : "") +
(module.id === selectedId ? "selected" : "");
}
}
// -------------------------------------------------------------------------
// Interaction and UI
// -------------------------------------------------------------------------
let selectedId = moduleId(3, 1);
let linkMode = false;
let linkSource = null;
let toastTimer = null;
const selectedName = document.getElementById("selectedName");
const selectedStatus = document.getElementById("selectedStatus");
const selectedBadge = document.getElementById("selectedBadge");
const linkButton = document.getElementById("linkButton");
const toolHint = document.getElementById("toolHint");
const progressText = document.getElementById("progressText");
const progressFill = document.getElementById("progressFill");
function toast(message, error = false) {
const element = document.getElementById("toast");
element.textContent = message;
element.classList.toggle("error", error);
element.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => element.classList.remove("show"), 2600);
}
function updateUI() {
const module = modules[selectedId];
selectedName.textContent = moduleName(module.row, module.col);
selectedStatus.textContent =
module.status === "safe"
? `Captured by ${module.reason === "CAPTURED" ? "north cistern" : "connected route"}`
: module.reason;
selectedBadge.textContent = module.status === "safe" ? "CAPTURED" : "SPILL";
selectedBadge.classList.toggle("safe", module.status === "safe");
document.querySelectorAll(".compass button").forEach((button) => {
button.classList.toggle("active", button.dataset.dir === module.dir);
});
linkButton.classList.toggle("active", linkMode);
linkButton.textContent = linkMode ? "Picking source…" : "Link gutters";
if (linkSource !== null) {
toolHint.innerHTML =
`Source: <strong>${moduleName(
modules[linkSource].row, modules[linkSource].col
)}</strong>. Click an adjacent awning to connect its downhill gutter.`;
} else if (linkMode) {
toolHint.innerHTML =
"Click the <strong>source awning</strong>, then an adjacent target. " +
"The source slope will align toward the target.";
} else {
toolHint.innerHTML =
"Select an awning, then choose its downhill edge. Use " +
"<strong>Link gutters</strong> to connect it to an adjacent module.";
}
progressText.textContent = `${safeCount} / ${MODULE_COUNT} modules`;
progressFill.style.width = `${(safeCount / MODULE_COUNT) * 100}%`;
document.getElementById("leftCount").textContent = leftCount;
document.getElementById("rightCount").textContent = rightCount;
modules.forEach((item) => {
item.selection.visible = item.id === selectedId;
});
}
function selectModule(id) {
selectedId = id;
updateUI();
}
function setSlope(id, dirName, announce = true) {
const module = modules[id];
if (module.dir === dirName) return;
module.dir = dirName;
module.roofPivot.rotation.y = DIRS[dirName].angle;
for (const [key, link] of [...links.entries()]) {
if (link.source === id) links.delete(key);
}
rebuildNetwork();
updateUI();
if (announce) {
toast(`${moduleName(module.row, module.col)} slopes ${DIRS[dirName].name.toLowerCase()}.`);
}
}
function tryLink(sourceId, targetId) {
if (sourceId === targetId) {
linkSource = null;
updateUI();
return;
}
const source = modules[sourceId];
const target = modules[targetId];
const dx = target.col - source.col;
const dz = target.row - source.row;
if (Math.abs(dx) + Math.abs(dz) !== 1) {
toast("Gutters can only connect adjacent awnings.", true);
return;
}
const dirName =
dx === 1 ? "E" :
dx === -1 ? "W" :
dz === 1 ? "S" : "N";
const start = outletPosition({ ...source, dir: dirName });
const end = inletPosition(target, dirName);
for (const gate of gates) {
if (segmentIntersectsRect(start, end, gate)) {
toast(
`Connection rejected: it crosses ${gate.name.toLowerCase()} clearance.`,
true
);
return;
}
}
if (links.has(linkKey(targetId, sourceId))) {
toast("That would create a two-module drain loop.", true);
return;
}
if (source.dir !== dirName) {
source.dir = dirName;
source.roofPivot.rotation.y = DIRS[dirName].angle;
}
for (const [key, link] of [...links.entries()]) {
if (link.source === sourceId) links.delete(key);
}
links.set(linkKey(sourceId, targetId), {
source: sourceId,
target: targetId
});
linkSource = null;
rebuildNetwork();
updateUI();
toast(
`Linked ${moduleName(source.row, source.col)} → ${moduleName(target.row, target.col)}.`
);
}
document.querySelectorAll(".compass button").forEach((button) => {
button.addEventListener("click", () => {
setSlope(selectedId, button.dataset.dir);
});
});
linkButton.addEventListener("click", () => {
linkMode = !linkMode;
linkSource = null;
updateUI();
});
document.getElementById("cutButton").addEventListener("click", () => {
let removed = false;
for (const [key, link] of [...links.entries()]) {
if (link.source === selectedId) {
links.delete(key);
removed = true;
}
}
rebuildNetwork();
updateUI();
toast(removed ? "Outlet gutter removed." : "This module has no outgoing gutter.");
});
document.getElementById("resetButton").addEventListener("click", () => resetLayout());
document.getElementById("demoButton").addEventListener("click", () => {
resetLayout(false);
toast("All sixteen modules now discharge into the north cisterns.");
});
// -------------------------------------------------------------------------
// Raycasting and manual camera controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
function pickModule(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickables, false);
if (!hits.length) return null;
return hits[0].object.userData.moduleId;
}
canvas.addEventListener("pointerdown", (event) => {
pointerDown = {
x: event.clientX,
y: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
button: event.button,
moved: false
};
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) {
if (!linkMode) {
canvas.style.cursor = pickModule(event) !== null ? "pointer" : "grab";
}
return;
}
const dx = event.clientX - pointerDown.lastX;
const dy = event.clientY - pointerDown.lastY;
pointerDown.lastX = event.clientX;
pointerDown.lastY = event.clientY;
if (Math.hypot(
event.clientX - pointerDown.x,
event.clientY - pointerDown.y
) > 4) pointerDown.moved = true;
if (pointerDown.moved && pointerDown.button === 0) {
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar + dy * 0.005,
0.45,
1.22
);
updateCamera();
canvas.style.cursor = "grabbing";
}
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerDown) return;
const wasClick =
!pointerDown.moved &&
pointerDown.button === 0 &&
Math.hypot(
event.clientX - pointerDown.x,
event.clientY - pointerDown.y
) < 6;
pointerDown = null;
canvas.style.cursor = "grab";
if (!wasClick) return;
const id = pickModule(event);
if (id === null || id === undefined) return;
if (linkMode) {
if (linkSource === null) {
linkSource = id;
selectModule(id);
toast(`Source selected: ${moduleName(modules[id].row, modules[id].col)}.`);
} else {
tryLink(linkSource, id);
}
} else {
selectModule(id);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * (event.deltaY > 0 ? 0.9 : 1.1),
0.7,
1.9
);
updateProjection();
}, { passive: false });
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
updateProjection();
});
// -------------------------------------------------------------------------
// DOM labels projected into the 3D scene
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
for (const module of modules) {
positionLabel(
module.label,
new THREE.Vector3(module.x, 6.03, module.z)
);
}
for (const cistern of cisterns) {
positionLabel(cistern.label, cistern.anchor);
}
for (const gate of gates) {
positionLabel(gate.label, gate.anchor);
}
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
for (const module of modules) {
module.particles.forEach((particle, particleIndex) => {
if (module.status !== "safe" || module.flowLength <= 0) {
particle.visible = false;
return;
}
particle.visible = true;
const phase =
(elapsed * 2.25 + module.id * 0.51 + particleIndex * 0.5) %
module.flowLength;
particle.position.copy(samplePath(module.flowPoints, phase));
particle.position.y += 0.16 + Math.sin(elapsed * 5 + module.id) * 0.04;
particle.rotation.x = elapsed * 1.6;
particle.rotation.y = elapsed * 2.1;
});
}
cisterns.forEach((cistern, index) => {
const fill = 0.35 + (cistern.count / 8) * 0.65;
cistern.water.scale.y = fill;
cistern.water.position.y = 0.42 + fill * 0.12;
cistern.water.material.emissiveIntensity =
0.22 + Math.sin(elapsed * 1.7 + index) * 0.08;
});
updateLabels();
renderer.render(scene, camera);
}
resetLayout(false);
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```