Prompt specification
Build or customize the Three.js 3D scene "Plan a temporary gathering around fixed shade areas" 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: Plan a temporary gathering around fixed shade areas
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/plan-a-temporary-gathering-around-fixed-shade-areas-x2776
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts Santo Domingo's plaza in Oaxaca. Let users arrange fictional tables and seating in the modeled plaza. Compare shaded occupancy and route connectivity under one fixed sun position. Voxel style.
Working Three.js Starter (plan-a-temporary-gathering-around-fixed-shade-areas-x2776):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Santo Domingo Plaza Lab</title>
<style>
:root {
--ink: #263238;
--muted: #65777b;
--paper: rgba(255, 250, 240, 0.94);
--line: rgba(49, 68, 69, 0.15);
--teal: #287b78;
--teal-soft: #d9ece7;
--gold: #e7ad3f;
--coral: #df7959;
--shadow: 0 18px 60px rgba(38, 50, 48, 0.19);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button, input { font: inherit; }
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
image-rendering: pixelated;
cursor: grab;
}
#scene.placing { cursor: crosshair; }
#scene.dragging { cursor: grabbing; }
.brand {
position: fixed;
top: 20px;
left: 20px;
z-index: 5;
max-width: 380px;
padding: 15px 18px 14px;
border: 1px solid rgba(255,255,255,.55);
border-radius: 14px;
background: rgba(255, 250, 240, .88);
backdrop-filter: blur(12px);
box-shadow: var(--shadow);
pointer-events: none;
}
.eyebrow {
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: .16em;
text-transform: uppercase;
}
.brand h1 {
margin: 3px 0 2px;
font-size: 24px;
line-height: 1.05;
letter-spacing: -.035em;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.4;
}
.sun-badge {
display: inline-flex;
align-items: center;
gap: 6px;
margin-top: 10px;
padding: 5px 8px;
border-radius: 999px;
background: #fff2c9;
color: #72561d;
font-size: 10px;
font-weight: 800;
letter-spacing: .08em;
}
.sun-badge::before {
content: "";
width: 8px;
height: 8px;
background: var(--gold);
box-shadow: 0 0 0 3px rgba(231,173,63,.22);
}
#panel {
position: fixed;
z-index: 6;
top: 20px;
right: 20px;
width: 326px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 17px;
border: 1px solid rgba(255,255,255,.6);
border-radius: 16px;
background: var(--paper);
backdrop-filter: blur(14px);
box-shadow: var(--shadow);
scrollbar-width: thin;
}
.panel-title {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
}
.panel-title h2 {
margin: 0;
font-size: 15px;
letter-spacing: -.02em;
}
.panel-title span {
color: var(--muted);
font-size: 10px;
}
.section {
padding: 13px 0;
border-top: 1px solid var(--line);
}
.section:first-of-type { border-top: 0; padding-top: 0; }
.section-label {
margin-bottom: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
}
.tool-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
button {
color: var(--ink);
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255,255,255,.75);
cursor: pointer;
transition: transform .12s ease, background .12s ease, border-color .12s ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(40,123,120,.42);
background: white;
}
button:disabled {
opacity: .38;
cursor: default;
}
.tool {
min-height: 59px;
padding: 8px 5px;
font-size: 11px;
font-weight: 750;
}
.tool .icon {
display: block;
margin-bottom: 4px;
color: var(--teal);
font-size: 19px;
line-height: 1;
}
.tool small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 500;
}
.tool.active {
color: white;
border-color: var(--teal);
background: var(--teal);
}
.tool.active .icon,
.tool.active small { color: white; }
.row {
display: flex;
gap: 7px;
}
.row > button { flex: 1; }
.small-button {
min-height: 34px;
padding: 7px 9px;
font-size: 11px;
font-weight: 700;
}
.selection {
min-height: 39px;
margin-bottom: 8px;
padding: 9px 10px;
border-radius: 9px;
background: rgba(40,123,120,.08);
color: var(--muted);
font-size: 11px;
}
.selection strong { color: var(--ink); }
.metric {
margin-bottom: 9px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255,255,255,.56);
}
.metric:last-child { margin-bottom: 0; }
.metric-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.metric-name {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.metric-value {
font-size: 24px;
font-weight: 850;
line-height: 1;
letter-spacing: -.04em;
}
.metric-sub {
margin-top: 7px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
.metric-bar {
height: 6px;
margin-top: 9px;
overflow: hidden;
border-radius: 999px;
background: #eadfca;
}
.metric-bar > div {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--teal);
transition: width .22s ease;
}
.metric.route .metric-bar > div { background: var(--gold); }
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 31px;
color: var(--muted);
font-size: 11px;
cursor: pointer;
}
.toggle input {
width: 32px;
height: 18px;
margin: 0;
accent-color: var(--teal);
cursor: pointer;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px 10px;
margin-top: 8px;
color: var(--muted);
font-size: 9px;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 2px;
}
.dot.shade { background: var(--teal); }
.dot.sun { background: var(--coral); }
.dot.route { background: var(--gold); }
.footnote {
margin: 10px 0 0;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
#help {
position: fixed;
z-index: 5;
left: 20px;
bottom: 20px;
max-width: min(510px, calc(100vw - 390px));
padding: 10px 13px;
border: 1px solid rgba(255,255,255,.55);
border-radius: 10px;
background: rgba(255,250,240,.88);
color: var(--muted);
font-size: 10px;
line-height: 1.45;
backdrop-filter: blur(10px);
box-shadow: 0 8px 30px rgba(38,50,48,.12);
pointer-events: none;
}
#help strong { color: var(--ink); }
#toast {
position: fixed;
z-index: 10;
left: 50%;
bottom: 28px;
padding: 10px 14px;
border-radius: 999px;
background: rgba(35,48,50,.93);
color: white;
font-size: 11px;
font-weight: 650;
box-shadow: var(--shadow);
opacity: 0;
transform: translate(-50%, 12px);
transition: opacity .2s ease, transform .2s ease;
pointer-events: none;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 10px;
left: 10px;
right: 10px;
max-width: none;
padding: 11px 13px;
}
.brand h1 { font-size: 19px; }
.brand p { display: none; }
.sun-badge { margin-top: 7px; }
#panel {
top: auto;
left: 10px;
right: 10px;
bottom: 10px;
width: auto;
max-height: 47vh;
padding: 13px;
}
#help { display: none; }
.tool { min-height: 52px; }
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div class="brand">
<div class="eyebrow">Oaxaca · Fictional occupancy study</div>
<h1>Santo Domingo Plaza</h1>
<p>Arrange voxel tables and seating, then compare shaded capacity and pedestrian connectivity.</p>
<div class="sun-badge">FIXED SUN · SOUTHWEST · 10:30</div>
</div>
<aside id="panel">
<div class="panel-title">
<h2>Plaza arrangement</h2>
<span>Three.js / voxel</span>
</div>
<div class="section">
<div class="section-label">Add furniture</div>
<div class="tool-grid">
<button class="tool" data-kind="table">
<span class="icon">▦</span>
Table
<small>4 companion places</small>
</button>
<button class="tool" data-kind="chair">
<span class="icon">▣</span>
Chair
<small>1 place</small>
</button>
<button class="tool" data-kind="bench">
<span class="icon">▤</span>
Bench
<small>4 places</small>
</button>
</div>
</div>
<div class="section">
<div class="section-label">Selected object</div>
<div id="selection" class="selection">Nothing selected.</div>
<div class="row">
<button id="rotate" class="small-button" disabled>Rotate 90° · R</button>
<button id="delete" class="small-button" disabled>Delete</button>
</div>
</div>
<div class="section">
<div class="section-label">Current arrangement</div>
<div class="metric">
<div class="metric-head">
<div class="metric-name">Shaded occupancy</div>
<div id="shadeValue" class="metric-value">0/0</div>
</div>
<div id="shadeSub" class="metric-sub">No seats placed.</div>
<div class="metric-bar"><div id="shadeBar"></div></div>
</div>
<div class="metric route">
<div class="metric-head">
<div class="metric-name">Route connectivity</div>
<div id="routeValue" class="metric-value">0/4</div>
</div>
<div id="routeSub" class="metric-sub">Entrances connected to the central hub.</div>
<div class="metric-bar"><div id="routeBar"></div></div>
</div>
<div class="row" style="margin-top:9px">
<button id="baseline" class="small-button">Pin current as baseline</button>
<button id="clearBaseline" class="small-button">Clear baseline</button>
</div>
</div>
<div class="section">
<div class="section-label">Analysis overlays</div>
<label class="toggle">
Fixed-sun shade map
<input id="shadeToggle" type="checkbox" checked>
</label>
<label class="toggle">
Connected route network
<input id="routeToggle" type="checkbox" checked>
</label>
<div class="legend">
<span><i class="dot shade"></i> shaded seat</span>
<span><i class="dot sun"></i> sunlit seat</span>
<span><i class="dot route"></i> shortest route</span>
<span>Green / red gate</span>
</div>
</div>
<div class="section">
<div class="section-label">Arrangement</div>
<div class="row">
<button id="demo" class="small-button">Load example</button>
<button id="reset" class="small-button">Clear furniture</button>
</div>
<p class="footnote">
This is a fictional planning model, not a survey of current plaza use.
Sun shade is calculated from the same fixed southwest direction used by
the renderer’s shadows.
</p>
</div>
</aside>
<div id="help">
<strong>Place:</strong> choose furniture, then click the paving.
<strong>Move:</strong> drag an object. <strong>View:</strong> drag background to orbit,
right-drag to pan, wheel to zoom. <strong>Keys:</strong> R rotates, Delete removes, Esc cancels.
</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";
// ---------------------------------------------------------------------
// Renderer, scene, and fixed lighting
// ---------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.5));
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(0xdce9ea);
scene.fog = new THREE.Fog(0xdce9ea, 105, 180);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
240
);
// North is negative Z. This sun remains fixed for the entire study.
const SUN_DIRECTION = new THREE.Vector3(-38, 52, 32).normalize();
const hemisphere = new THREE.HemisphereLight(0xeaf8ff, 0x80624b, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedbe, 3.0);
sun.position.copy(SUN_DIRECTION).multiplyScalar(85);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -62;
sun.shadow.camera.right = 62;
sun.shadow.camera.top = 62;
sun.shadow.camera.bottom = -62;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 190;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
scene.add(sun.target);
const cameraState = {
yaw: 0.78,
pitch: 0.73,
distance: 91,
target: new THREE.Vector3(0, 1.7, 0)
};
function updateCamera() {
const cp = Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
cameraState.distance * cp * Math.sin(cameraState.yaw),
cameraState.target.y +
cameraState.distance * Math.sin(cameraState.pitch),
cameraState.target.z +
cameraState.distance * cp * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
updateCamera();
// ---------------------------------------------------------------------
// Shared scene helpers
// ---------------------------------------------------------------------
const materialCache = new Map();
function materialFor(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0
})
);
}
return materialCache.get(color);
}
function addBox(
parent,
width,
height,
depth,
x,
y,
z,
color,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
materialFor(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
// Static blockers are used for both analytical shade and route tests.
const shadeBlockers = [];
const routeRects = [];
function addShadeBlocker(x, z, width, depth, y0, y1) {
shadeBlockers.push({
minX: x - width / 2,
maxX: x + width / 2,
minZ: z - depth / 2,
maxZ: z + depth / 2,
minY: y0,
maxY: y1
});
}
function addRouteRect(x, z, width, depth) {
routeRects.push({ x, z, width, depth, angle: 0 });
}
// ---------------------------------------------------------------------
// Voxel environment: plaza, church, arcades, kiosk, trees, and people
// ---------------------------------------------------------------------
function buildEnvironment() {
addBox(scene, 100, 1, 84, 0, -0.62, 0, 0xb99b76);
addBox(scene, 72, 0.22, 58, 0, -0.1, 0, 0xcbb08b);
// Instanced sandstone paving.
const tileGeometry = new THREE.BoxGeometry(1.92, 0.09, 1.92);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const tileCount = 34 * 27;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
const dummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let iz = 0; iz < 27; iz++) {
for (let ix = 0; ix < 34; ix++) {
dummy.position.set(-33 + ix * 2, 0.035, -25 + iz * 2);
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const warm = (ix + iz) % 2 === 0;
tileColor.setHex(warm ? 0xdac49d : 0xe2cdaa);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
// Low curb around the modeled plaza.
addBox(scene, 0.5, 0.35, 55, -34.25, 0.16, 0, 0xad8d68);
addBox(scene, 0.5, 0.35, 55, 34.25, 0.16, 0, 0xad8d68);
addBox(scene, 69, 0.35, 0.5, 0, 0.16, -27.25, 0xad8d68);
addBox(scene, 69, 0.35, 0.5, 0, 0.16, 27.25, 0xad8d68);
// Santo Domingo-inspired church mass at the north end.
addBox(scene, 30, 14, 12, 0, 7, -34, 0xc65f69);
addShadeBlocker(0, -34, 30, 12, 0, 14);
addBox(scene, 31, 0.7, 12.7, 0, 14.2, -34, 0xf0d6a4);
addBox(scene, 31, 0.45, 12.5, 0, 7.1, -34, 0xe9bf8d);
addBox(scene, 29.5, 14.1, 0.55, 0, 7.05, -27.78, 0xcb6870);
// Central doorway and stepped voxel arch.
addBox(scene, 4.4, 6.1, 0.3, 0, 3.05, -27.42, 0x4b3b3c);
addBox(scene, 0.75, 6.6, 0.38, -2.75, 3.3, -27.28, 0xf1d8aa);
addBox(scene, 0.75, 6.6, 0.38, 2.75, 3.3, -27.28, 0xf1d8aa);
addBox(scene, 6.3, 0.75, 0.38, 0, 6.6, -27.28, 0xf1d8aa);
addBox(scene, 5.1, 0.75, 0.4, 0, 7.3, -27.25, 0xf1d8aa);
addBox(scene, 3.8, 0.65, 0.4, 0, 7.95, -27.22, 0xf1d8aa);
addBox(scene, 2.2, 1.0, 0.16, 0, 3.0, -27.2, 0x72564a);
// Rose window and cross.
addBox(scene, 3.7, 3.7, 0.35, 0, 10.7, -27.4, 0xf0d4a1);
addBox(scene, 3.1, 3.1, 0.38, 0, 10.7, -27.18, 0x4a4244);
addBox(scene, 0.25, 2.4, 0.15, 0, 10.7, -26.94, 0xd89b66);
addBox(scene, 2.4, 0.25, 0.15, 0, 10.7, -26.93, 0xd89b66);
// Twin block towers.
for (const x of [-13.7, 13.7]) {
addBox(scene, 5.8, 19, 7, x, 9.5, -31.5, 0xc15c67);
addShadeBlocker(x, -31.5, 5.8, 7, 0, 19);
addBox(scene, 6.5, 0.75, 7.7, x, 13.7, -31.5, 0xf0d5a2);
addBox(scene, 5.4, 4.8, 6.2, x, 18, -31.5, 0xd8a067);
addBox(scene, 6.2, 0.65, 7.1, x, 20.5, -31.5, 0xf1d9aa);
addBox(scene, 1.35, 2.5, 0.2, x, 16.0, -28.25, 0x4d3e40);
addBox(scene, 1.05, 1.0, 0.22, x, 19.0, -28.28, 0x4d3e40);
addBox(scene, 0.32, 2.2, 0.32, x, 21.4, -31.5, 0xf0d5a2);
addBox(scene, 1.25, 0.32, 0.32, x, 21.65, -31.5, 0xf0d5a2);
}
addBox(scene, 8.2, 0.45, 3.3, 0, 0.22, -26.7, 0xb99770);
addBox(scene, 6.8, 0.45, 2.7, 0, 0.44, -26.0, 0xc9aa80);
// Pastel side arcades.
const arcadeColors = [0xe7a065, 0x79a9b1, 0xd8c56f, 0xd98276];
const sideSegments = [
{ z: -19, length: 10, height: 11 },
{ z: -8, length: 9, height: 13 },
{ z: 8, length: 9, height: 10.5 },
{ z: 19, length: 10, height: 12 }
];
for (const side of [-1, 1]) {
sideSegments.forEach((segment, index) => {
const x = side * 38;
const color = arcadeColors[(index + (side > 0 ? 2 : 0)) % 4];
addBox(
scene,
8,
segment.height,
segment.length,
x,
segment.height / 2,
segment.z,
color
);
addShadeBlocker(
x,
segment.z,
8,
segment.length,
0,
segment.height
);
const frontX = side * 33.93;
for (let j = 0; j < 4; j++) {
const z = segment.z - segment.length / 2 + 1.6 +
j * ((segment.length - 3.2) / 3);
addBox(scene, 0.25, 3.4, 2.05, frontX, 3.0, z, 0x56494a);
addBox(
scene,
0.58,
segment.height - 1.0,
0.58,
side * 33.38,
(segment.height - 1.0) / 2,
z - 0.7,
0xf0d7aa
);
}
addBox(
scene,
1.1,
0.72,
segment.length + 0.4,
side * 33.8,
segment.height + 0.25,
segment.z,
0xf0d7aa
);
});
}
// South arcades, leaving a central entrance.
const southSegments = [
{ x: -23, width: 10, height: 11, color: 0x7ca6ae },
{ x: -11, width: 10, height: 12.5, color: 0xe3b35f },
{ x: 11, width: 10, height: 11.5, color: 0xcf7d72 },
{ x: 23, width: 10, height: 13, color: 0x8ba77d }
];
for (const segment of southSegments) {
addBox(
scene,
segment.width,
segment.height,
8,
segment.x,
segment.height / 2,
31.5,
segment.color
);
addShadeBlocker(
segment.x,
31.5,
segment.width,
8,
0,
segment.height
);
for (let j = 0; j < 4; j++) {
const x = segment.x - segment.width / 2 + 1.6 +
j * ((segment.width - 3.2) / 3);
addBox(scene, 2.05, 3.4, 0.25, x, 3.0, 27.43, 0x514547);
addBox(
scene,
0.58,
segment.height - 1,
0.58,
x - 0.7,
(segment.height - 1) / 2,
27.15,
0xf0d7aa
);
}
addBox(
scene,
segment.width + 0.4,
0.72,
1.1,
segment.x,
segment.height + 0.25,
27.3,
0xf0d7aa
);
}
// Buildings framing the northern corners.
for (const x of [-25, 25]) {
addBox(scene, 11, 10.5, 10, x, 5.25, -31.5, 0xdfaa66);
addShadeBlocker(x, -31.5, 11, 10, 0, 10.5);
addBox(scene, 11.5, 0.65, 10.5, x, 10.6, -31.5, 0xf0d5a0);
}
// Central bandstand / kiosk.
addBox(scene, 7.2, 0.5, 7.2, 0, 0.25, 0, 0xb99c71);
addRouteRect(0, 0, 7.2, 7.2);
addBox(scene, 6.4, 0.28, 6.4, 0, 0.62, 0, 0xd9c49a);
for (const x of [-2.7, 2.7]) {
for (const z of [-2.7, 2.7]) {
addBox(scene, 0.55, 4.8, 0.55, x, 3.0, z, 0xf0ddb6);
}
}
for (const z of [-2.7, 2.7]) {
for (const x of [-1.35, 1.35]) {
addBox(scene, 0.45, 4.8, 0.45, x, 3.0, z, 0xead3a6);
}
}
addBox(scene, 8.1, 0.7, 8.1, 0, 5.85, 0, 0x3f8c88);
addBox(scene, 7.2, 0.5, 7.2, 0, 6.45, 0, 0xf1d8aa);
addBox(scene, 5.9, 0.65, 5.9, 0, 7.0, 0, 0x3f8c88);
addBox(scene, 0.65, 1.0, 0.65, 0, 7.7, 0, 0xf1d8aa);
addShadeBlocker(0, 0, 8.1, 8.1, 5.5, 6.2);
addShadeBlocker(0, 0, 5.9, 5.9, 6.7, 7.35);
// Four tree planters.
for (const [x, z] of [
[-25, -16],
[25, -16],
[-25, 16],
[25, 16]
]) {
addBox(scene, 4.2, 1.0, 4.2, x, 0.5, z, 0xa67e58);
addBox(scene, 3.6, 0.18, 3.6, x, 1.05, z, 0x806746);
addRouteRect(x, z, 4.2, 4.2);
addBox(scene, 0.7, 3.6, 0.7, x, 2.75, z, 0x70533d);
addBox(scene, 5.2, 3.0, 5.2, x, 5.0, z, 0x5f9369);
addBox(scene, 3.7, 2.0, 3.7, x, 7.1, z, 0x76a774);
addBox(scene, 2.5, 1.6, 2.5, x + 1.0, 8.4, z - 0.5, 0x87b27c);
addShadeBlocker(x, z, 5.2, 5.2, 3.5, 6.5);
addShadeBlocker(x, z, 3.7, 3.7, 6.1, 8.1);
addShadeBlocker(x + 1, z - 0.5, 2.5, 2.5, 7.6, 9.2);
addBox(scene, 0.55, 0.35, 0.55, x - 1.2, 1.28, z + 1.2, 0xdb8e77);
addBox(scene, 0.55, 0.35, 0.55, x + 1.2, 1.28, z - 1.1, 0xe2b451);
}
// Simple voxel lamps.
for (const [x, z] of [
[-31, -21], [-31, 21], [31, -21], [31, 21],
[-19, 25], [19, 25]
]) {
addBox(scene, 0.24, 4.2, 0.24, x, 2.1, z, 0x4c5050);
addBox(scene, 0.85, 0.35, 0.85, x, 4.35, z, 0x4c5050);
addBox(scene, 0.62, 0.65, 0.62, x, 4.05, z, 0xffd77e);
}
// Scale figures.
makePerson(-5.2, -20.5, 0x6b7e9b);
makePerson(4.5, -22.1, 0xc66d54);
makePerson(-30.1, 19.8, 0x5d8c78);
makePerson(19.8, 24.5, 0x88709b);
makePerson(30.2, 6.0, 0xbe8c49);
makePerson(6.5, 20.0, 0x527f98);
}
function makePerson(x, z, shirtColor) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
addBox(group, 0.28, 0.85, 0.32, -0.18, 0.43, 0, 0x4d5054);
addBox(group, 0.28, 0.85, 0.32, 0.18, 0.43, 0, 0x4d5054);
addBox(group, 0.82, 0.95, 0.48, 0, 1.31, 0, shirtColor);
addBox(group, 0.22, 0.75, 0.28, -0.53, 1.28, 0, 0xbd8664);
addBox(group, 0.22, 0.75, 0.28, 0.53, 1.28, 0, 0xbd8664);
addBox(group, 0.55, 0.55, 0.52, 0, 2.04, 0, 0xbd8664);
addBox(group, 0.58, 0.18, 0.55, 0, 2.28, 0, 0x4d3b32);
}
buildEnvironment();
// ---------------------------------------------------------------------
// Analysis grid and overlays
// ---------------------------------------------------------------------
const GRID = {
minX: -33,
minZ: -26,
width: 66,
depth: 52,
cell: 1,
columns: 66,
rows: 52
};
GRID.count = GRID.columns * GRID.rows;
const HUB_POINTS = [
[-5, 0],
[5, 0],
[0, -5],
[0, 5]
];
const GATES = [
{ name: "West", x: -32.5, z: 0 },
{ name: "East", x: 32.5, z: 0 },
{ name: "North", x: 0, z: -25.5 },
{ name: "South", x: 0, z: 25.5 }
];
const shadeMapMaterial = new THREE.MeshBasicMaterial({
color: 0x2c8290,
transparent: true,
opacity: 0.24,
depthWrite: false
});
const shadeMap = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.94, 0.025, 0.94),
shadeMapMaterial,
GRID.count
);
shadeMap.count = 0;
shadeMap.renderOrder = 2;
scene.add(shadeMap);
const routeMaterial = new THREE.MeshBasicMaterial({
color: 0xf4c54e,
transparent: true,
opacity: 0.76,
depthWrite: false
});
const routeOverlay = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.31, 0.035, 0.31),
routeMaterial,
GRID.count
);
routeOverlay.count = 0;
routeOverlay.renderOrder = 3;
scene.add(routeOverlay);
const seatMarkerMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.96
});
const seatMarkers = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.22, 0.17, 0.22),
seatMarkerMaterial,
2500
);
seatMarkers.count = 0;
seatMarkers.renderOrder = 4;
scene.add(seatMarkers);
const gateMarkers = [];
for (const gate of GATES) {
const group = new THREE.Group();
group.position.set(gate.x, 0, gate.z);
scene.add(group);
addBox(group, 0.22, 1.65, 0.22, 0, 0.82, 0, 0x455c58);
const topMaterial = new THREE.MeshBasicMaterial({ color: 0x4eaa72 });
const top = new THREE.Mesh(
new THREE.BoxGeometry(0.58, 0.58, 0.58),
topMaterial
);
top.position.y = 1.85;
group.add(top);
gateMarkers.push({ group, topMaterial });
}
for (const [x, z] of HUB_POINTS) {
addBox(
scene,
0.48,
0.18,
0.48,
x,
0.18,
z,
0x3f9892,
false,
false
);
}
const analysisDummy = new THREE.Object3D();
function rayHitsBox(origin, direction, box) {
let tMin = 0;
let tMax = Infinity;
const axes = [
["x", origin.x, direction.x, box.minX, box.maxX],
["y", origin.y, direction.y, box.minY, box.maxY],
["z", origin.z, direction.z, box.minZ, box.maxZ]
];
for (const [, o, d, min, max] of axes) {
if (Math.abs(d) < 1e-8) {
if (o < min || o > max) return false;
continue;
}
let t1 = (min - o) / d;
let t2 = (max - o) / d;
if (t1 > t2) [t1, t2] = [t2, t1];
tMin = Math.max(tMin, t1);
tMax = Math.min(tMax, t2);
if (tMax < tMin) return false;
}
return tMax > 0.001;
}
function isShadedAt(x, y, z) {
const origin = { x, y, z };
for (const blocker of shadeBlockers) {
if (rayHitsBox(origin, SUN_DIRECTION, blocker)) return true;
}
return false;
}
function buildShadeMap() {
let count = 0;
for (let iz = 0; iz < GRID.rows; iz++) {
for (let ix = 0; ix < GRID.columns; ix++) {
const x = GRID.minX + ix + 0.5;
const z = GRID.minZ + iz + 0.5;
if (!isShadedAt(x, 0.16, z)) continue;
analysisDummy.position.set(x, 0.115, z);
analysisDummy.updateMatrix();
shadeMap.setMatrixAt(count, analysisDummy.matrix);
count++;
}
}
shadeMap.count = count;
shadeMap.instanceMatrix.needsUpdate = true;
}
buildShadeMap();
// ---------------------------------------------------------------------
// Furniture model and placement geometry
// ---------------------------------------------------------------------
const furniture = new THREE.Group();
furniture.name = "Furniture";
scene.add(furniture);
const items = [];
let nextItemId = 1;
let selectedItem = null;
let placementKind = null;
const SPECS = {
table: {
label: "Table with four companion stools",
width: 5.4,
depth: 5.4,
capacity: 4,
seats: [
[-2, 0],
[2, 0],
[0, -2],
[0, 2]
]
},
chair: {
label: "Single chair",
width: 1.4,
depth: 1.4,
capacity: 1,
seats: [[0, 0]]
},
bench: {
label: "Four-place bench",
width: 4.2,
depth: 1.6,
capacity: 4,
seats: [
[-1.35, 0],
[-0.45, 0],
[0.45, 0],
[1.35, 0]
]
}
};
function buildFurniture(kind) {
const group = new THREE.Group();
group.userData.kind = kind;
group.userData.id = nextItemId++;
if (kind === "table") {
addBox(group, 3.2, 0.3, 3.2, 0, 1.45, 0, 0xd37852);
addBox(group, 0.32, 1.3, 0.32, -1.25, 0.65, -1.25, 0x695447);
addBox(group, 0.32, 1.3, 0.32, 1.25, 0.65, -1.25, 0x695447);
addBox(group, 0.32, 1.3, 0.32, -1.25, 0.65, 1.25, 0x695447);
addBox(group, 0.32, 1.3, 0.32, 1.25, 0.65, 1.25, 0x695447);
for (const [x, z] of SPECS.table.seats) {
addBox(group, 0.76, 0.22, 0.76, x, 0.67, z, 0xf0d3a4);
addBox(group, 0.24, 0.58, 0.24, x, 0.29, z, 0x775a47);
}
addBox(group, 0.75, 0.08, 0.75, 0, 1.64, 0, 0xf0c35f);
}
if (kind === "chair") {
addBox(group, 1.0, 0.2, 1.0, 0, 0.68, 0, 0x468d8b);
addBox(group, 1.0, 1.05, 0.2, 0, 1.18, 0.48, 0x468d8b);
addBox(group, 0.18, 0.62, 0.18, -0.36, 0.31, -0.34, 0x5c4b42);
addBox(group, 0.18, 0.62, 0.18, 0.36, 0.31, -0.34, 0x5c4b42);
addBox(group, 0.18, 0.62, 0.18, -0.36, 0.31, 0.34, 0x5c4b42);
addBox(group, 0.18, 0.62, 0.18, 0.36, 0.31, 0.34, 0x5c4b42);
}
if (kind === "bench") {
addBox(group, 3.6, 0.25, 1.05, 0, 0.68, 0, 0xd5a241);
addBox(group, 3.6, 1.0, 0.22, 0, 1.22, 0.48, 0xd5a241);
addBox(group, 0.28, 0.58, 0.72, -1.35, 0.29, 0, 0x5d4b42);
addBox(scene === group ? group : group, 0.28, 0.58, 0.72, 1.35, 0.29, 0, 0x5d4b42);
}
return group;
}
function itemRect(item, x = item.position.x, z = item.position.z, angle = item.rotation.y) {
const spec = SPECS[item.userData.kind];
return {
x,
z,
width: spec.width,
depth: spec.depth,
angle
};
}
function rectCorners(rect) {
const c = Math.cos(rect.angle);
const s = Math.sin(rect.angle);
const hx = rect.width / 2;
const hz = rect.depth / 2;
return [
[-hx, -hz],
[hx, -hz],
[hx, hz],
[-hx, hz]
].map(([lx, lz]) => ({
x: rect.x + c * lx + s * lz,
z: rect.z - s * lx + c * lz
}));
}
function pointInRect(x, z, rect) {
const dx = x - rect.x;
const dz = z - rect.z;
const c = Math.cos(rect.angle);
const s = Math.sin(rect.angle);
const localX = c * dx - s * dz;
const localZ = s * dx + c * dz;
return (
Math.abs(localX) <= rect.width / 2 &&
Math.abs(localZ) <= rect.depth / 2
);
}
function rectsOverlap(a, b) {
const aCorners = rectCorners(a);
const bCorners = rectCorners(b);
const axes = [
{ x: Math.cos(a.angle), z: -Math.sin(a.angle) },
{ x: Math.sin(a.angle), z: Math.cos(a.angle) },
{ x: Math.cos(b.angle), z: -Math.sin(b.angle) },
{ x: Math.sin(b.angle), z: Math.cos(b.angle) }
];
for (const axis of axes) {
let minA = Infinity;
let maxA = -Infinity;
let minB = Infinity;
let maxB = -Infinity;
for (const p of aCorners) {
const projection = p.x * axis.x + p.z * axis.z;
minA = Math.min(minA, projection);
maxA = Math.max(maxA, projection);
}
for (const p of bCorners) {
const projection = p.x * axis.x + p.z * axis.z;
minB = Math.min(minB, projection);
maxB = Math.max(maxB, projection);
}
if (maxA <= minB + 0.01 || maxB <= minA + 0.01) return false;
}
return true;
}
function canPlace(rect, ignoredItem = null) {
for (const corner of rectCorners(rect)) {
if (
corner.x < -33.2 ||
corner.x > 33.2 ||
corner.z < -26.2 ||
corner.z > 26.2
) {
return false;
}
}
for (const obstacle of routeRects) {
if (rectsOverlap(rect, obstacle)) return false;
}
for (const item of items) {
if (item === ignoredItem) continue;
if (rectsOverlap(rect, itemRect(item))) return false;
}
return true;
}
function insertItem(kind, x, z, angle = 0, quiet = false) {
const rect = {
x,
z,
width: SPECS[kind].width,
depth: SPECS[kind].depth,
angle
};
if (!canPlace(rect)) {
if (!quiet) showToast("That spot overlaps a planter, kiosk, or another object.");
return null;
}
const item = buildFurniture(kind);
item.position.set(x, 0, z);
item.rotation.y = angle;
furniture.add(item);
items.push(item);
return item;
}
const selectionHelper = new THREE.BoxHelper(undefined, 0xffd166);
selectionHelper.visible = false;
selectionHelper.material.depthTest = false;
selectionHelper.renderOrder = 10;
scene.add(selectionHelper);
function selectItem(item) {
selectedItem = item;
if (!item) {
selectionHelper.visible = false;
} else {
selectionHelper.setFromObject(item);
selectionHelper.visible = true;
}
updateSelectionPanel();
}
function updateSelectionPanel() {
const selection = document.querySelector("#selection");
const rotate = document.querySelector("#rotate");
const remove = document.querySelector("#delete");
if (selectedItem) {
selection.innerHTML =
`<strong>${SPECS[selectedItem.userData.kind].label}</strong><br>` +
`Fictional capacity: ${SPECS[selectedItem.userData.kind].capacity} place(s)`;
rotate.disabled = false;
remove.disabled = false;
} else {
selection.textContent = "Nothing selected. Click an object to inspect it.";
rotate.disabled = true;
remove.disabled = true;
}
}
function clearItems() {
furniture.traverse((object) => {
if (object.isMesh) object.geometry.dispose();
});
furniture.clear();
items.length = 0;
selectItem(null);
}
function loadDemo() {
clearItems();
insertItem("table", -28, -10, 0, true);
insertItem("table", 9, 10, 0, true);
insertItem("bench", -19, -17, 0, true);
insertItem("bench", 14, -13, 0, true);
insertItem("chair", -15, 8, 0, true);
insertItem("chair", -13.5, 9.5, 0, true);
insertItem("chair", 9, 5.5, 0, true);
insertItem("chair", 13.5, 10, 0, true);
refreshAnalysis();
showToast("Example arrangement loaded.");
}
// ---------------------------------------------------------------------
// Route connectivity: grid flood fill from the central hub
// ---------------------------------------------------------------------
function gridIndex(x, z) {
const ix = Math.floor((x - GRID.minX) / GRID.cell);
const iz = Math.floor((z - GRID.minZ) / GRID.cell);
if (
ix < 0 || ix >= GRID.columns ||
iz < 0 || iz >= GRID.rows
) {
return -1;
}
return iz * GRID.columns + ix;
}
function gridWorld(index) {
const ix = index % GRID.columns;
const iz = Math.floor(index / GRID.columns);
return {
x: GRID.minX + ix + 0.5,
z: GRID.minZ + iz + 0.5
};
}
function isBlockedAt(x, z) {
for (const rect of routeRects) {
if (pointInRect(x, z, rect)) return true;
}
for (const item of items) {
if (pointInRect(x, z, itemRect(item))) return true;
}
return false;
}
function computeRoutes() {
const parent = new Int32Array(GRID.count);
parent.fill(-2);
const queue = new Int32Array(GRID.count);
let head = 0;
let tail = 0;
let walkable = 0;
for (const [x, z] of HUB_POINTS) {
const index = gridIndex(x, z);
if (index >= 0 && !isBlockedAt(x, z)) {
parent[index] = -1;
queue[tail++] = index;
}
}
while (head < tail) {
const current = queue[head++];
const ix = current % GRID.columns;
const iz = Math.floor(current / GRID.columns);
const neighbors = [];
if (ix > 0) neighbors.push(current - 1);
if (ix < GRID.columns - 1) neighbors.push(current + 1);
if (iz > 0) neighbors.push(current - GRID.columns);
if (iz < GRID.rows - 1) neighbors.push(current + GRID.columns);
for (const neighbor of neighbors) {
if (parent[neighbor] !== -2) continue;
const p = gridWorld(neighbor);
if (isBlockedAt(p.x, p.z)) continue;
parent[neighbor] = current;
queue[tail++] = neighbor;
}
}
for (let iz = 0; iz < GRID.rows; iz++) {
for (let ix = 0; ix < GRID.columns; ix++) {
const x = GRID.minX + ix + 0.5;
const z = GRID.minZ + iz + 0.5;
if (!isBlockedAt(x, z)) walkable++;
}
}
const gateResults = [];
let connected = 0;
for (const gate of GATES) {
const index = gridIndex(gate.x, gate.z);
const reachable = index >= 0 && parent[index] !== -2;
if (reachable) connected++;
const path = [];
if (reachable) {
let current = index;
while (current >= 0) {
path.push(gridWorld(current));
current = parent[current];
}
}
gateResults.push({ ...gate, reachable, path });
}
return {
connected,
walkable,
walkablePercent: (walkable / GRID.count) * 100,
paths: gateResults.filter((gate) => gate.reachable).map((gate) => gate.path),
gateResults
};
}
function updateRouteOverlay(routeData) {
const routeCells = new Map();
for (const path of routeData.paths) {
for (const point of path) {
const key = `${point.x.toFixed(2)},${point.z.toFixed(2)}`;
routeCells.set(key, point);
}
}
let count = 0;
for (const point of routeCells.values()) {
analysisDummy.position.set(point.x, 0.16, point.z);
analysisDummy.updateMatrix();
routeOverlay.setMatrixAt(count, analysisDummy.matrix);
count++;
}
routeOverlay.count = count;
routeOverlay.instanceMatrix.needsUpdate = true;
routeData.gateResults.forEach((gate, index) => {
gateMarkers[index].topMaterial.color.setHex(
gate.reachable ? 0x4eaa72 : 0xd75d55
);
});
}
// ---------------------------------------------------------------------
// Metrics and baseline comparison
// ---------------------------------------------------------------------
let baseline = null;
let analysis = {
total: 0,
shaded: 0,
connected: 0,
walkablePercent: 100
};
function refreshAnalysis() {
let total = 0;
let shaded = 0;
seatMarkers.count = 0;
let markerCount = 0;
const shadedColor = new THREE.Color(0x287b78);
const sunlitColor = new THREE.Color(0xdf7959);
for (const item of items) {
const spec = SPECS[item.userData.kind];
const c = Math.cos(item.rotation.y);
const s = Math.sin(item.rotation.y);
for (const [localX, localZ] of spec.seats) {
const x = item.position.x + c * localX + s * localZ;
const z = item.position.z - s * localX + c * localZ;
const inShade = isShadedAt(x, 0.76, z);
total++;
if (inShade) shaded++;
if (markerCount < 2500) {
analysisDummy.position.set(x, 0.27, z);
analysisDummy.updateMatrix();
seatMarkers.setMatrixAt(markerCount, analysisDummy.matrix);
seatMarkers.setColorAt(
markerCount,
inShade ? shadedColor : sunlitColor
);
markerCount++;
}
}
}
seatMarkers.count = markerCount;
seatMarkers.instanceMatrix.needsUpdate = true;
if (seatMarkers.instanceColor) seatMarkers.instanceColor.needsUpdate = true;
const routeData = computeRoutes();
updateRouteOverlay(routeData);
analysis = {
total,
shaded,
connected: routeData.connected,
walkablePercent: routeData.walkablePercent
};
updateMetrics();
}
function updateMetrics() {
const shadeValue = document.querySelector("#shadeValue");
const shadeSub = document.querySelector("#shadeSub");
const shadeBar = document.querySelector("#shadeBar");
const routeValue = document.querySelector("#routeValue");
const routeSub = document.querySelector("#routeSub");
const routeBar = document.querySelector("#routeBar");
const shadePercent = analysis.total
? (analysis.shaded / analysis.total) * 100
: 0;
shadeValue.textContent = `${analysis.shaded}/${analysis.total}`;
shadeBar.style.width = `${shadePercent}%`;
let shadeText = analysis.total
? `${analysis.total - analysis.shaded} sunlit place(s) · ${shadePercent.toFixed(0)}% shaded`
: "No seats placed.";
if (baseline) {
const delta = analysis.shaded - baseline.shaded;
shadeText += ` · baseline ${baseline.shaded}/${baseline.total}, Δ ${delta >= 0 ? "+" : ""}${delta}`;
}
shadeSub.textContent = shadeText;
routeValue.textContent = `${analysis.connected}/4`;
routeBar.style.width = `${(analysis.connected / 4) * 100}%`;
let routeText =
`${analysis.walkablePercent.toFixed(1)}% of the plaza grid remains walkable.`;
if (baseline) {
const delta = analysis.connected - baseline.connected;
routeText += ` Baseline ${baseline.connected}/4, Δ ${delta >= 0 ? "+" : ""}${delta}.`;
} else {
routeText += " Pin a baseline to compare edits.";
}
routeSub.textContent = routeText;
}
let analysisQueued = false;
function scheduleAnalysis() {
if (analysisQueued) return;
analysisQueued = true;
requestAnimationFrame(() => {
analysisQueued = false;
refreshAnalysis();
});
}
// ---------------------------------------------------------------------
// Custom pointer controls, selection, placement, and dragging
// ---------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const groundPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const groundPoint = new THREE.Vector3();
let pointerState = null;
function updateRay(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);
}
function pointOnGround(event) {
updateRay(event);
return raycaster.ray.intersectPlane(groundPlane, groundPoint)
? groundPoint.clone()
: null;
}
function pickFurniture(event) {
updateRay(event);
const hits = raycaster.intersectObjects(furniture.children, true);
for (const hit of hits) {
let object = hit.object;
while (object && object.parent !== furniture) object = object.parent;
if (object && object.parent === furniture) return object;
}
return null;
}
function setPlacementKind(kind) {
placementKind = placementKind === kind ? null : kind;
document.querySelectorAll("[data-kind]").forEach((button) => {
button.classList.toggle("active", button.dataset.kind === placementKind);
});
canvas.classList.toggle("placing", Boolean(placementKind));
showToast(
placementKind
? `Click the paving to place a ${placementKind}. Esc cancels.`
: "Placement mode cancelled."
);
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
event.preventDefault();
canvas.setPointerCapture(event.pointerId);
if (event.button === 2) {
pointerState = {
mode: "pan",
startX: event.clientX,
startY: event.clientY
};
return;
}
const hit = pickFurniture(event);
if (hit) {
selectItem(hit);
const point = pointOnGround(event);
if (!point) return;
pointerState = {
mode: "drag",
item: hit,
offsetX: hit.position.x - point.x,
offsetZ: hit.position.z - point.z,
lastX: hit.position.x,
lastZ: hit.position.z
};
canvas.classList.add("dragging");
return;
}
if (placementKind) {
const point = pointOnGround(event);
if (!point) return;
const x = Math.round(point.x * 4) / 4;
const z = Math.round(point.z * 4) / 4;
const item = insertItem(placementKind, x, z);
if (item) {
selectItem(item);
scheduleAnalysis();
}
return;
}
pointerState = {
mode: "orbit",
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerState) return;
if (pointerState.mode === "drag") {
const point = pointOnGround(event);
if (!point) return;
const x = Math.round((point.x + pointerState.offsetX) * 4) / 4;
const z = Math.round((point.z + pointerState.offsetZ) * 4) / 4;
const item = pointerState.item;
const candidate = itemRect(item, x, z, item.rotation.y);
if (canPlace(candidate, item)) {
item.position.x = x;
item.position.z = z;
pointerState.lastX = x;
pointerState.lastZ = z;
selectionHelper.setFromObject(item);
scheduleAnalysis();
}
}
if (pointerState.mode === "orbit") {
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
pointerState.moved ||=
Math.abs(event.clientX - pointerState.startX) +
Math.abs(event.clientY - pointerState.startY) > 4;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.34,
1.28
);
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
}
if (pointerState.mode === "pan") {
const dx = event.clientX - pointerState.startX;
const dy = event.clientY - pointerState.startY;
const scale = cameraState.distance * 0.0015;
const right = {
x: Math.cos(cameraState.yaw),
z: -Math.sin(cameraState.yaw)
};
const forward = {
x: Math.sin(cameraState.yaw),
z: Math.cos(cameraState.yaw)
};
cameraState.target.x -= right.x * dx * scale;
cameraState.target.z -= right.z * dx * scale;
cameraState.target.x += forward.x * dy * scale;
cameraState.target.z += forward.z * dy * scale;
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x, -25, 25
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z, -20, 20
);
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
}
});
function endPointer(event) {
if (!pointerState) return;
if (
pointerState.mode === "orbit" &&
!pointerState.moved &&
!placementKind
) {
selectItem(null);
}
pointerState = null;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
scheduleAnalysis();
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
46,
128
);
},
{ passive: false }
);
function rotateSelected() {
if (!selectedItem) return;
const nextAngle = selectedItem.rotation.y + Math.PI / 2;
const candidate = itemRect(
selectedItem,
selectedItem.position.x,
selectedItem.position.z,
nextAngle
);
if (!canPlace(candidate, selectedItem)) {
showToast("The rotated object would overlap something else.");
return;
}
selectedItem.rotation.y = nextAngle;
selectionHelper.setFromObject(selectedItem);
scheduleAnalysis();
}
function deleteSelected() {
if (!selectedItem) return;
const item = selectedItem;
furniture.remove(item);
item.traverse((object) => {
if (object.isMesh) object.geometry.dispose();
});
const index = items.indexOf(item);
if (index >= 0) items.splice(index, 1);
selectItem(null);
scheduleAnalysis();
}
// ---------------------------------------------------------------------
// Interface events
// ---------------------------------------------------------------------
document.querySelectorAll("[data-kind]").forEach((button) => {
button.addEventListener("click", () => {
setPlacementKind(button.dataset.kind);
});
});
document.querySelector("#rotate").addEventListener("click", rotateSelected);
document.querySelector("#delete").addEventListener("click", deleteSelected);
document.querySelector("#demo").addEventListener("click", loadDemo);
document.querySelector("#reset").addEventListener("click", () => {
clearItems();
refreshAnalysis();
showToast("Furniture cleared.");
});
document.querySelector("#baseline").addEventListener("click", () => {
baseline = {
total: analysis.total,
shaded: analysis.shaded,
connected: analysis.connected
};
document.querySelector("#baseline").textContent = "Replace baseline";
updateMetrics();
showToast("Current arrangement pinned as the comparison baseline.");
});
document.querySelector("#clearBaseline").addEventListener("click", () => {
baseline = null;
document.querySelector("#baseline").textContent = "Pin current as baseline";
updateMetrics();
});
document.querySelector("#shadeToggle").addEventListener("change", (event) => {
shadeMap.visible = event.target.checked;
});
document.querySelector("#routeToggle").addEventListener("change", (event) => {
routeOverlay.visible = event.target.checked;
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.key === "Escape") {
placementKind = null;
document.querySelectorAll("[data-kind]").forEach((button) => {
button.classList.remove("active");
});
canvas.classList.remove("placing");
}
if (event.key.toLowerCase() === "r") rotateSelected();
if (event.key === "Delete" || event.key === "Backspace") {
deleteSelected();
}
});
let toastTimer = null;
function showToast(message) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 2400);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.5));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
// ---------------------------------------------------------------------
// Initial state and render loop
// ---------------------------------------------------------------------
loadDemo();
updateSelectionPanel();
function animate() {
requestAnimationFrame(animate);
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```