Prompt specification
Build or customize the Three.js 3D scene "Public-space coexistence planner" 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: Public-space coexistence planner
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/public-space-coexistence-planner-x0548
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring street performance and audience space. Create a plaza with market, performance, seating, and walking zones; users place temporary activities so viewing areas remain useful and all entrances stay connected. Low-poly.
Working Three.js Starter (public-space-coexistence-planner-x0548):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Plaza / Pop-up Lab</title>
<style>
:root {
--ink: #203331;
--muted: #687975;
--paper: rgba(250, 248, 241, 0.94);
--paper-solid: #faf8f1;
--line: rgba(32, 51, 49, 0.13);
--accent: #e86642;
--accent-dark: #ba4528;
--green: #397865;
--red: #c84e43;
--shadow: 0 18px 50px rgba(37, 54, 49, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e4;
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;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.glass {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#brand {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
display: flex;
align-items: center;
gap: 12px;
padding: 11px 15px 11px 11px;
border-radius: 16px;
pointer-events: none;
}
.brand-mark {
width: 40px;
height: 40px;
display: grid;
place-items: center;
border-radius: 12px;
color: white;
background: var(--accent);
box-shadow: 0 7px 16px rgba(232, 102, 66, 0.28);
}
.brand-mark svg {
width: 23px;
height: 23px;
}
.brand-copy strong {
display: block;
font-size: 13px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.brand-copy span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.05em;
}
#panel {
position: fixed;
z-index: 20;
top: 94px;
left: 22px;
width: 312px;
max-height: calc(100vh - 122px);
overflow: auto;
padding: 18px;
border-radius: 20px;
scrollbar-width: thin;
transition:
transform 180ms ease,
opacity 180ms ease;
}
#panel.hidden {
transform: translateX(-370px);
opacity: 0;
pointer-events: none;
}
.eyebrow {
margin: 0 0 4px;
color: var(--accent-dark);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 23px;
line-height: 1.1;
letter-spacing: -0.035em;
}
.intro {
margin: 9px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 18px;
}
.stat {
min-width: 0;
padding: 10px 7px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.52);
text-align: center;
}
.stat strong {
display: block;
overflow: hidden;
color: var(--ink);
font-size: 14px;
text-overflow: ellipsis;
white-space: nowrap;
}
.stat span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.section-label {
display: flex;
align-items: center;
justify-content: space-between;
margin: 17px 0 9px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.section-label span:last-child {
color: var(--muted);
font-weight: 600;
letter-spacing: 0;
text-transform: none;
}
.tools {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.tool {
position: relative;
min-height: 68px;
padding: 10px 9px 9px 38px;
border: 1px solid var(--line);
border-radius: 13px;
color: var(--ink);
background: rgba(255, 255, 255, 0.58);
cursor: pointer;
text-align: left;
transition:
border-color 140ms ease,
background 140ms ease,
transform 140ms ease;
}
.tool:hover {
transform: translateY(-1px);
border-color: rgba(232, 102, 66, 0.48);
background: white;
}
.tool.active {
border-color: var(--accent);
background: #fff3ed;
box-shadow: inset 0 0 0 1px var(--accent);
}
.tool-icon {
position: absolute;
top: 10px;
left: 9px;
width: 23px;
height: 23px;
display: grid;
place-items: center;
border-radius: 7px;
color: var(--accent-dark);
background: #f8dfd4;
font-size: 14px;
font-weight: 900;
}
.tool strong {
display: block;
font-size: 11px;
line-height: 1.2;
}
.tool small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
line-height: 1.25;
}
.rules {
display: grid;
gap: 7px;
padding: 11px;
border-radius: 13px;
background: rgba(56, 120, 101, 0.08);
}
.rule {
display: flex;
align-items: flex-start;
gap: 8px;
color: #4d655f;
font-size: 10px;
line-height: 1.35;
}
.rule-dot {
flex: 0 0 auto;
width: 7px;
height: 7px;
margin-top: 3px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 3px rgba(57, 120, 101, 0.11);
}
#liveRule {
min-height: 39px;
margin-top: 9px;
padding: 9px 10px;
border: 1px dashed rgba(57, 120, 101, 0.35);
border-radius: 11px;
color: var(--green);
background: rgba(57, 120, 101, 0.05);
font-size: 10px;
line-height: 1.35;
}
#liveRule.invalid {
color: var(--red);
border-color: rgba(200, 78, 67, 0.38);
background: rgba(200, 78, 67, 0.06);
}
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.action {
min-height: 38px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--ink);
background: rgba(255, 255, 255, 0.7);
cursor: pointer;
font-size: 10px;
font-weight: 700;
}
.action:hover {
background: white;
}
.action.danger {
color: var(--red);
}
.mini-note {
margin: 13px 1px 0;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
#topActions {
position: fixed;
z-index: 15;
top: 22px;
right: 22px;
display: flex;
gap: 7px;
}
.view-button {
height: 40px;
padding: 0 13px;
border: 1px solid rgba(255, 255, 255, 0.76);
border-radius: 12px;
color: var(--ink);
background: var(--paper);
box-shadow: 0 9px 26px rgba(37, 54, 49, 0.12);
backdrop-filter: blur(14px);
cursor: pointer;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.03em;
}
.view-button:hover {
background: white;
}
#togglePanel {
display: none;
}
#legend {
position: fixed;
z-index: 10;
right: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 12px;
max-width: 510px;
padding: 12px 15px;
border-radius: 14px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
color: #526560;
font-size: 9px;
font-weight: 650;
}
.swatch {
width: 10px;
height: 10px;
border: 1px solid rgba(32, 51, 49, 0.12);
border-radius: 3px;
}
#hint {
position: fixed;
z-index: 9;
bottom: 23px;
left: 50%;
padding: 10px 15px;
border-radius: 999px;
color: #526560;
font-size: 10px;
font-weight: 650;
pointer-events: none;
transform: translateX(-50%);
white-space: nowrap;
}
#toast {
position: fixed;
z-index: 50;
left: 50%;
top: 24px;
max-width: min(460px, calc(100vw - 32px));
padding: 11px 16px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 12px;
color: white;
background: rgba(31, 50, 48, 0.92);
box-shadow: var(--shadow);
opacity: 0;
pointer-events: none;
transform: translate(-50%, -18px);
transition:
opacity 170ms ease,
transform 170ms ease;
font-size: 11px;
font-weight: 650;
text-align: center;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.error {
background: rgba(164, 59, 49, 0.94);
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #dce8e4;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
text-align: center;
}
.loader-ring {
width: 42px;
height: 42px;
margin: 0 auto 13px;
border: 3px solid rgba(32, 51, 49, 0.12);
border-top-color: var(--accent);
border-radius: 50%;
animation: spin 900ms linear infinite;
}
.loader span {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (max-width: 780px) {
#brand {
top: 12px;
left: 12px;
}
.brand-copy span {
display: none;
}
#topActions {
top: 12px;
right: 12px;
}
#togglePanel {
display: block;
}
#panel {
top: 67px;
left: 12px;
width: min(312px, calc(100vw - 24px));
max-height: calc(100vh - 86px);
}
#panel.hidden {
transform: translateX(-370px);
}
#legend {
display: none;
}
#hint {
bottom: 12px;
max-width: calc(100vw - 24px);
overflow: hidden;
text-overflow: ellipsis;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly plaza planning scene"></canvas>
<div id="loading">
<div class="loader">
<div class="loader-ring"></div>
<span>Preparing the plaza</span>
</div>
</div>
<header id="brand" class="glass">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
<path d="M4 19V8l8-4 8 4v11" />
<path d="M8 19v-6h8v6M3 19h18" />
</svg>
</div>
<div class="brand-copy">
<strong>Plaza / Pop-up Lab</strong>
<span>Flexible public-space planning</span>
</div>
</header>
<div id="topActions">
<button id="topView" class="view-button">TOP VIEW</button>
<button id="resetView" class="view-button">RESET VIEW</button>
<button id="togglePanel" class="view-button">PLANNER</button>
</div>
<aside id="panel" class="glass">
<p class="eyebrow">Temporary programming</p>
<h1>Shape the plaza</h1>
<p class="intro">
Place a temporary activity, then check its footprint, audience area,
and route impact before committing.
</p>
<div class="stats">
<div class="stat">
<strong id="activityCount">0</strong>
<span>Activities</span>
</div>
<div class="stat">
<strong id="gateCount">4 / 4</strong>
<span>Gates linked</span>
</div>
<div class="stat">
<strong id="viewingCount">Clear</strong>
<span>Sightlines</span>
</div>
</div>
<div class="section-label">
<span>1. Choose an activity</span>
<span>Click to activate</span>
</div>
<div class="tools">
<button class="tool" data-type="stage" aria-pressed="false">
<span class="tool-icon">P</span>
<strong>Pop-up act</strong>
<small>Small stage + audience buffer</small>
</button>
<button class="tool" data-type="screen" aria-pressed="false">
<span class="tool-icon">▣</span>
<strong>Outdoor screen</strong>
<small>Screen + clear viewing area</small>
</button>
<button class="tool" data-type="cart" aria-pressed="false">
<span class="tool-icon">M</span>
<strong>Market cart</strong>
<small>Compact temporary retail</small>
</button>
<button class="tool" data-type="food" aria-pressed="false">
<span class="tool-icon">F</span>
<strong>Food kiosk</strong>
<small>Service kiosk with awning</small>
</button>
<button class="tool" data-type="seating" aria-pressed="false">
<span class="tool-icon">S</span>
<strong>Extra seating</strong>
<small>Flexible bench cluster</small>
</button>
</div>
<div class="section-label">
<span>2. Placement guardrails</span>
<span>Automatic</span>
</div>
<div class="rules">
<div class="rule">
<span class="rule-dot"></span>
<span>Primary walking lanes and gate thresholds stay clear.</span>
</div>
<div class="rule">
<span class="rule-dot"></span>
<span>Performance and screen viewing areas remain unobstructed.</span>
</div>
<div class="rule">
<span class="rule-dot"></span>
<span>Every plaza entrance remains connected after placement.</span>
</div>
</div>
<div id="liveRule">Select an activity to begin.</div>
<div class="actions">
<button id="rotateTool" class="action">Rotate 90° (R)</button>
<button id="undoTool" class="action">Undo last</button>
<button id="clearTool" class="action danger">Clear all</button>
<button id="cancelTool" class="action">Cancel selection</button>
</div>
<p class="mini-note">
Click the plaza to place. Drag to orbit, right-drag to pan, and scroll to
zoom. Double-click a temporary activity to remove it.
</p>
</aside>
<div id="legend" class="glass">
<div class="legend-item">
<span class="swatch" style="background:#e9dfc8"></span>Walking
</div>
<div class="legend-item">
<span class="swatch" style="background:#ead8ad"></span>Market
</div>
<div class="legend-item">
<span class="swatch" style="background:#d9d2e8"></span>Performance
</div>
<div class="legend-item">
<span class="swatch" style="background:#cfe0cb"></span>Seating
</div>
<div class="legend-item">
<span class="swatch" style="background:#f4b46d"></span>Protected viewing
</div>
</div>
<div id="hint" class="glass">Choose an activity · click a valid location · R rotates</div>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#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.06;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#dce8e4");
scene.fog = new THREE.Fog("#dce8e4", 72, 145);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
220,
);
const hemi = new THREE.HemisphereLight("#f8fff7", "#81928b", 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight("#fff4d9", 3.1);
sun.position.set(-28, 46, 25);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -42;
sun.shadow.camera.right = 42;
sun.shadow.camera.top = 38;
sun.shadow.camera.bottom = -38;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b9dce0", 0.85);
fill.position.set(32, 18, -30);
scene.add(fill);
const plazaGroup = new THREE.Group();
const tempGroup = new THREE.Group();
scene.add(plazaGroup, tempGroup);
const animatedPeople = [];
const staticObstacles = [];
const activities = [];
const tempModels = new Map();
// -------------------------------------------------------------------------
// Shared materials and geometry helpers
// -------------------------------------------------------------------------
const material = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.87,
metalness: 0.02,
flatShading: true,
...extra,
});
const M = {
ground: material("#bfc9bd"),
plazaBase: material("#d1c9b9"),
plaza: material("#e7e1d4"),
path: material("#e9dfc8"),
market: material("#ead8ad"),
performance: material("#d9d2e8"),
seating: material("#cfe0cb"),
viewing: material("#f4b46d", {
transparent: true,
opacity: 0.72,
depthWrite: false,
}),
navy: material("#344b62"),
navyLight: material("#49677f"),
orange: material("#e86642"),
cream: material("#f3ead4"),
wood: material("#a97645"),
woodDark: material("#765036"),
green: material("#668d67"),
greenLight: material("#83a879"),
greenDark: material("#486b50"),
trunk: material("#76543d"),
white: material("#f6f5ec"),
black: material("#283331"),
pink: material("#d88779"),
blue: material("#6b94aa"),
yellow: material("#dfb557"),
asphalt: material("#7c8582"),
building: material("#aebbb3"),
building2: material("#c3c8bb"),
};
function box(parent, w, h, d, mat, x = 0, y = 0, z = 0, cast = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(
parent,
topRadius,
bottomRadius,
height,
segments,
mat,
x = 0,
y = 0,
z = 0,
cast = true,
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(topRadius, bottomRadius, height, segments),
mat,
);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function plane(parent, w, d, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(w, d), mat);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(x, y, z);
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function rect(cx, cz, w, d, pad = 0) {
return {
minX: cx - w / 2 - pad,
maxX: cx + w / 2 + pad,
minZ: cz - d / 2 - pad,
maxZ: cz + d / 2 + pad,
};
}
function overlaps(a, b, clearance = 0) {
return (
a.minX - clearance < b.maxX &&
a.maxX + clearance > b.minX &&
a.minZ - clearance < b.maxZ &&
a.maxZ + clearance > b.minZ
);
}
function zoneOutline(cx, cz, w, d, color, y = 0.075) {
const points = [
new THREE.Vector3(cx - w / 2, y, cz - d / 2),
new THREE.Vector3(cx + w / 2, y, cz - d / 2),
new THREE.Vector3(cx + w / 2, y, cz + d / 2),
new THREE.Vector3(cx - w / 2, y, cz + d / 2),
];
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.LineLoop(
geometry,
new THREE.LineDashedMaterial({
color,
dashSize: 0.65,
gapSize: 0.42,
transparent: true,
opacity: 0.7,
}),
);
line.computeLineDistances();
scene.add(line);
return line;
}
function groundLabel(text, w, d, x, z, background, foreground = "#33433f") {
const c = document.createElement("canvas");
c.width = 768;
c.height = 160;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = background;
ctx.beginPath();
ctx.roundRect(8, 8, c.width - 16, c.height - 16, 28);
ctx.fill();
ctx.fillStyle = foreground;
ctx.font = "800 58px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.letterSpacing = "8px";
ctx.fillText(text, c.width / 2, c.height / 2 + 2);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(w, d),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
}),
);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(x, 0.082, z);
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Plaza surface and programmed zones
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(220, 220),
M.ground,
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.02;
ground.receiveShadow = true;
scene.add(ground);
box(plazaGroup, 64, 0.8, 52, M.plazaBase, 0, -0.53, 0);
box(plazaGroup, 60, 0.2, 48, M.plaza, 0, -0.1, 0);
// A quiet street edge around the plaza.
box(plazaGroup, 78, 0.12, 6, M.asphalt, 0, -0.98, -28);
box(plazaGroup, 78, 0.12, 6, M.asphalt, 0, -0.98, 28);
box(plazaGroup, 6, 0.12, 62, M.asphalt, -34, -0.98, 0);
box(plazaGroup, 6, 0.12, 62, M.asphalt, 34, -0.98, 0);
// Fine paving grid.
const pavingPositions = [];
for (let x = -30; x <= 30; x += 3) {
pavingPositions.push(x, 0.018, -24, x, 0.018, 24);
}
for (let z = -24; z <= 24; z += 3) {
pavingPositions.push(-30, 0.018, z, 30, 0.018, z);
}
const pavingGeometry = new THREE.BufferGeometry();
pavingGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(pavingPositions, 3),
);
scene.add(
new THREE.LineSegments(
pavingGeometry,
new THREE.LineBasicMaterial({
color: "#65736d",
transparent: true,
opacity: 0.12,
}),
),
);
// Functional zones.
plane(plazaGroup, 21, 12, M.market, 16.5, 0.027, -12.5);
plane(plazaGroup, 15, 14.5, M.performance, -16, 0.028, -10.75);
plane(plazaGroup, 21, 13, M.seating, 16.5, 0.027, 12.5);
plane(plazaGroup, 13, 6.5, M.viewing, -16, 0.035, -6.8);
// Primary walking network.
plane(plazaGroup, 60, 5, M.path, 0, 0.055, 0);
plane(plazaGroup, 5, 48, M.path, 0, 0.057, 0);
// Dashed route centerlines.
const routeMaterial = new THREE.LineDashedMaterial({
color: "#b4aa91",
dashSize: 0.8,
gapSize: 0.8,
transparent: true,
opacity: 0.65,
});
[
[new THREE.Vector3(-29, 0.075, 0), new THREE.Vector3(29, 0.075, 0)],
[new THREE.Vector3(0, 0.077, -23), new THREE.Vector3(0, 0.077, 23)],
].forEach(([a, b]) => {
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([a, b]),
routeMaterial,
);
line.computeLineDistances();
scene.add(line);
});
zoneOutline(16.5, -12.5, 21, 12, "#ad8641");
zoneOutline(-16, -10.75, 15, 14.5, "#7c7198");
zoneOutline(16.5, 12.5, 21, 13, "#66876a");
zoneOutline(-16, -6.8, 13, 6.5, "#cc713c");
groundLabel("MARKET", 9, 1.9, 16.5, -19.25, "#ead8ad", "#72582f");
groundLabel("PERFORMANCE", 11, 1.9, -16, -18.5, "#d9d2e8", "#5e5672");
groundLabel("SEATING", 8, 1.9, 16.5, 19.25, "#cfe0cb", "#47654c");
groundLabel("VIEWING", 8, 1.55, -16, -4.15, "#f4b46d", "#814427");
// Entrances.
const gateData = [
{ name: "NORTH", x: 0, z: -24, rotation: 0 },
{ name: "SOUTH", x: 0, z: 24, rotation: Math.PI },
{ name: "WEST", x: -30, z: 0, rotation: Math.PI / 2 },
{ name: "EAST", x: 30, z: 0, rotation: -Math.PI / 2 },
];
gateData.forEach((gate) => {
const gateGroup = new THREE.Group();
gateGroup.position.set(gate.x, 0, gate.z);
gateGroup.rotation.y = gate.rotation;
scene.add(gateGroup);
box(gateGroup, 0.28, 3.1, 0.28, M.navy, -2.85, 1.55, 0.25);
box(gateGroup, 0.28, 3.1, 0.28, M.navy, 2.85, 1.55, 0.25);
box(gateGroup, 6, 0.45, 0.38, M.orange, 0, 3.05, 0.25);
box(gateGroup, 5.3, 0.2, 0.25, M.cream, 0, 2.84, 0.25);
groundLabel(
gate.name,
5.2,
1.15,
gate.x + (gate.rotation ? 0 : 0),
gate.z + (gate.rotation ? 0 : 0),
"#344b62",
"#ffffff",
);
});
// Low-poly context buildings.
const buildingData = [
[-42, 4.6, -28, 10, 8, 12, M.building],
[-27, 3.5, -35, 12, 6, 9, M.building2],
[-10, 5.2, -36, 14, 9, 10, M.building],
[10, 3.4, -35, 11, 5, 9, M.building2],
[31, 4.8, -33, 13, 8, 11, M.building],
[-40, 3.8, 28, 12, 6, 10, M.building2],
[40, 3.4, 29, 10, 5, 11, M.building],
[-40, 2.8, 10, 8, 4, 12, M.building2],
[41, 2.6, 6, 8, 4, 12, M.building2],
];
buildingData.forEach(([x, h, z, w, hh, d, mat]) => {
box(scene, w, hh, d, mat, x, -1 + hh / 2, z);
box(
scene,
w * 0.78,
0.35,
d * 0.8,
M.roof || M.building2,
x,
-1 + hh + 0.15,
z,
);
});
// -------------------------------------------------------------------------
// People, trees, stalls, stage, and seating
// -------------------------------------------------------------------------
function createPerson(color, scale = 1) {
const group = new THREE.Group();
const clothes = material(color);
const torso = cylinder(group, 0.23, 0.31, 0.72, 6, clothes, 0, 0.92, 0);
torso.rotation.y = Math.PI / 6;
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.22, 8, 6),
material("#c98768"),
);
head.position.y = 1.48;
head.castShadow = true;
group.add(head);
const hair = new THREE.Mesh(
new THREE.SphereGeometry(0.225, 8, 5, 0, Math.PI * 2, 0, Math.PI / 2),
material("#4c3a30"),
);
hair.position.y = 1.53;
hair.castShadow = true;
group.add(hair);
const legs = [];
[-0.13, 0.13].forEach((x) => {
const leg = cylinder(
group,
0.09,
0.1,
0.62,
5,
M.navy,
x,
0.31,
0,
);
legs.push(leg);
});
const arms = [];
[-1, 1].forEach((side) => {
const pivot = new THREE.Group();
pivot.position.set(side * 0.29, 1.16, 0);
group.add(pivot);
const arm = cylinder(
pivot,
0.07,
0.08,
0.53,
5,
clothes,
0,
-0.23,
0,
);
arms.push(pivot);
});
group.scale.setScalar(scale);
group.userData.arms = arms;
group.userData.legs = legs;
return group;
}
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
cylinder(group, 0.24, 0.32, 3, 6, M.trunk, 0, 1.5, 0);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.65, 0),
M.green,
);
crown.position.y = 3.65;
crown.castShadow = true;
group.add(crown);
const crown2 = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.22, 0),
M.greenLight,
);
crown2.position.set(0.8, 3.3, 0.2);
crown2.castShadow = true;
group.add(crown2);
staticObstacles.push({
name: "Tree planter",
rect: rect(x, z, 2.6, 2.6),
});
}
addTree(-27, -20, 1);
addTree(27, -20, 0.95);
addTree(-27, 20, 0.95);
addTree(27, 20, 1.05);
function addPlanter(x, z) {
box(plazaGroup, 3.1, 0.65, 1.2, M.cream, x, 0.325, z);
box(plazaGroup, 2.6, 0.12, 0.82, M.greenDark, x, 0.69, z);
staticObstacles.push({
name: "Planter",
rect: rect(x, z, 3.1, 1.3),
});
}
addPlanter(-6.8, 18.3);
addPlanter(6.8, 18.3);
function addStall(x, z, color, accent, label) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
box(group, 3.5, 1.05, 1.7, M.wood, 0, 0.53, 0);
box(group, 3.7, 0.16, 1.9, M.woodDark, 0, 1.08, 0);
box(group, 0.14, 2.45, 0.14, M.woodDark, -1.65, 1.22, -0.7);
box(group, 0.14, 2.45, 0.14, M.woodDark, 1.65, 1.22, -0.7);
const roof = box(group, 4, 0.17, 2.7, accent, 0, 2.48, 0);
roof.rotation.x = 0.08;
for (let i = 0; i < 5; i++) {
box(
group,
0.42,
0.045,
2.68,
i % 2 === 0 ? M.cream : accent,
-1.6 + i * 0.8,
2.57,
0,
false,
);
}
box(group, 0.13, 0.13, 2.45, M.cream, -1.65, 2.38, 0);
box(group, 0.13, 0.13, 2.45, M.cream, 1.65, 2.38, 0);
const productColors = [M.orange, M.yellow, M.greenLight, M.pink];
for (let i = 0; i < 5; i++) {
const product = new THREE.Mesh(
i % 2
? new THREE.IcosahedronGeometry(0.22, 0)
: new THREE.BoxGeometry(0.35, 0.25, 0.35),
productColors[i % productColors.length],
);
product.position.set(-1.25 + i * 0.62, 1.28, 0.12);
product.castShadow = true;
group.add(product);
}
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(1.65, 0.43),
new THREE.MeshBasicMaterial({ color: accent }),
);
sign.position.set(0, 0.69, 0.856);
group.add(sign);
staticObstacles.push({
name: label,
rect: rect(x, z, 4.1, 2.8),
});
}
addStall(10, -15.4, M.orange, M.orange, "Market stall");
addStall(16, -15.4, M.blue, M.blue, "Market stall");
addStall(22, -15.4, M.green, M.green, "Market stall");
addStall(10, -9.4, M.yellow, M.yellow, "Market stall");
addStall(16, -9.4, M.pink, M.pink, "Market stall");
addStall(22, -9.4, M.navyLight, M.navyLight, "Market stall");
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
box(group, 4.8, 0.28, 0.85, M.wood, 0, 0.57, 0);
box(group, 4.8, 0.95, 0.18, M.woodDark, 0, 1.03, 0.35);
box(group, 0.18, 0.56, 0.72, M.navy, -1.75, 0.28, 0);
box(group, 0.18, 0.56, 0.72, M.navy, 1.75, 0.28, 0);
staticObstacles.push({
name: "Bench",
rect: rotation
? rect(x, z, 1.4, 5)
: rect(x, z, 5, 1.4),
});
}
[10, 16, 22].forEach((x) => {
addBench(x, 9.5);
addBench(x, 16.5);
});
// Main performance stage.
const stage = new THREE.Group();
stage.position.set(-16, 0, -13);
scene.add(stage);
box(stage, 11, 0.95, 5.5, M.navy, 0, 0.475, 0);
box(stage, 10.7, 0.12, 5.2, M.woodDark, 0, 1.01, 0);
box(stage, 9.4, 3.4, 0.25, M.navyLight, 0, 2.65, -2.2);
box(stage, 0.24, 4.2, 0.24, M.navy, -5.1, 2.1, 2.2);
box(stage, 0.24, 4.2, 0.24, M.navy, 5.1, 2.1, 2.2);
box(stage, 10.4, 0.22, 0.24, M.navy, 0, 4.1, 2.2);
for (let i = 0; i < 5; i++) {
box(
stage,
1.7,
2.7,
0.08,
i % 2 ? M.orange : M.cream,
-3.7 + i * 1.85,
2.65,
-2.02,
false,
);
}
const performerColors = [M.orange, M.yellow, M.blue];
for (let i = 0; i < 3; i++) {
const performer = createPerson(performerColors[i], 1.05);
performer.position.set(-2.4 + i * 2.4, 1.08, 0.3);
stage.add(performer);
animatedPeople.push({
group: performer,
phase: i * 1.7,
kind: "performer",
});
}
staticObstacles.push({
name: "Main stage",
rect: rect(-16, -13, 11.2, 5.7),
});
// Audience in the protected viewing area.
const audienceColors = [
"#c76750",
"#658b86",
"#d1a74c",
"#6e7796",
"#789b6a",
"#ad7168",
];
const audienceRows = [-9.0, -7.35, -5.75];
audienceRows.forEach((z, row) => {
for (let i = 0; i < 7; i++) {
if ((row + i) % 5 === 0) continue;
const x = -21 + i * 1.55 + (row % 2) * 0.35;
const person = createPerson(
audienceColors[(row * 3 + i) % audienceColors.length],
0.86 + ((i + row) % 3) * 0.055,
);
person.position.set(x, 0.06, z);
person.rotation.y = Math.PI;
scene.add(person);
animatedPeople.push({
group: person,
phase: row * 2.4 + i,
kind: "audience",
});
}
});
// A few people browsing and using the seating zone.
for (let i = 0; i < 9; i++) {
const person = createPerson(
audienceColors[(i + 2) % audienceColors.length],
0.88 + (i % 3) * 0.06,
);
const positions = [
[12.5, -6.2],
[19.1, -12.2],
[24, -5.9],
[11.5, 12.6],
[20.2, 13.1],
[24.1, 8.2],
[-7.8, -8.5],
[-6.7, 8.1],
[6.2, -6.1],
];
person.position.set(positions[i][0], 0.06, positions[i][1]);
person.rotation.y = i * 0.8;
scene.add(person);
animatedPeople.push({
group: person,
phase: i * 0.9,
kind: "audience",
});
}
// -------------------------------------------------------------------------
// Protected areas and activity definitions
// -------------------------------------------------------------------------
const protectedAreas = [
{
name: "the north–south walking lane",
rect: rect(0, 0, 5, 48),
},
{
name: "the east–west walking lane",
rect: rect(0, 0, 60, 5),
},
{
name: "the main audience viewing area",
rect: rect(-16, -6.8, 13, 6.5),
},
{
name: "the north entrance",
rect: rect(0, -23, 6, 2),
},
{
name: "the south entrance",
rect: rect(0, 23, 6, 2),
},
{
name: "the west entrance",
rect: rect(-29, 0, 2, 6),
},
{
name: "the east entrance",
rect: rect(29, 0, 2, 6),
},
];
const activityDefs = {
stage: {
label: "Pop-up act",
footprint: [5, 3],
view: [9, 5],
viewGap: 0.8,
},
screen: {
label: "Outdoor screen",
footprint: [3.2, 1.4],
view: [7, 4],
viewGap: 0.8,
},
cart: {
label: "Market cart",
footprint: [3.4, 2.2],
},
food: {
label: "Food kiosk",
footprint: [4, 2.6],
},
seating: {
label: "Extra seating",
footprint: [4, 2],
},
};
function rotatedDimensions(base, rotation) {
return rotation % 2
? [base[1], base[0]]
: [base[0], base[1]];
}
function getFootprint(type, x, z, rotation) {
const [w, d] = rotatedDimensions(activityDefs[type].footprint, rotation);
return {
rect: rect(x, z, w, d),
w,
d,
};
}
function getViewArea(type, x, z, rotation) {
const def = activityDefs[type];
if (!def.view) return null;
const [w, d] = rotatedDimensions(def.view, rotation);
const offset =
def.footprint[1] / 2 + def.view[1] / 2 + def.viewGap;
const cx = rotation % 2 ? x + offset : x;
const cz = rotation % 2 ? z : z + offset;
return {
rect: rect(cx, cz, w, d),
w,
d,
cx,
cz,
};
}
// Grid-based entrance connectivity. Activities and fixed structures are
// obstacles; primary paths remain available as alternate routes.
function checkConnectivity(candidateFootprints = []) {
const cell = 1.5;
const cols = 40;
const rows = 32;
const minX = -30;
const minZ = -24;
const total = cols * rows;
const blocked = new Uint8Array(total);
const obstacles = [
...staticObstacles.map((o) => o.rect),
...candidateFootprints,
];
function blockedAt(col, row) {
if (col < 0 || row < 0 || col >= cols || row >= rows) return true;
return blocked[row * cols + col];
}
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const x = minX + (col + 0.5) * cell;
const z = minZ + (row + 0.5) * cell;
for (const obstacle of obstacles) {
if (
x > obstacle.minX - 0.6 &&
x < obstacle.maxX + 0.6 &&
z > obstacle.minZ - 0.6 &&
z < obstacle.maxZ + 0.6
) {
blocked[row * cols + col] = 1;
break;
}
}
}
}
function nearestCell(x, z) {
const col = THREE.MathUtils.clamp(
Math.floor((x - minX) / cell),
0,
cols - 1,
);
const row = THREE.MathUtils.clamp(
Math.floor((z - minZ) / cell),
0,
rows - 1,
);
return row * cols + col;
}
const gates = [
nearestCell(0, -23.25),
nearestCell(0, 23.25),
nearestCell(-29.25, 0),
nearestCell(29.25, 0),
];
if (gates.some((index) => blocked[index])) return false;
const visited = new Uint8Array(total);
const queue = new Int32Array(total);
let head = 0;
let tail = 0;
queue[tail++] = gates[0];
visited[gates[0]] = 1;
const directions = [
[-1, 0],
[1, 0],
[0, -1],
[0, 1],
[-1, -1],
[-1, 1],
[1, -1],
[1, 1],
];
while (head < tail) {
const index = queue[head++];
const col = index % cols;
const row = Math.floor(index / cols);
for (const [dx, dz] of directions) {
const nextCol = col + dx;
const nextRow = row + dz;
if (
nextCol < 0 ||
nextRow < 0 ||
nextCol >= cols ||
nextRow >= rows
) {
continue;
}
const next = nextRow * cols + nextCol;
if (visited[next] || blocked[next]) continue;
// Prevent moving diagonally through a blocked corner.
if (dx !== 0 && dz !== 0) {
if (
blockedAt(col + dx, row) ||
blockedAt(col, row + dz)
) {
continue;
}
}
visited[next] = 1;
queue[tail++] = next;
}
}
return gates.every((index) => visited[index]);
}
function validatePlacement(type, x, z, rotation) {
const footprint = getFootprint(type, x, z, rotation);
const f = footprint.rect;
if (
f.minX < -29.3 ||
f.maxX > 29.3 ||
f.minZ < -23.3 ||
f.maxZ > 23.3
) {
return { valid: false, reason: "Place the activity fully inside the plaza." };
}
for (const protectedArea of protectedAreas) {
if (overlaps(f, protectedArea.rect)) {
return {
valid: false,
reason: `Keep ${protectedArea.name} clear.`,
};
}
}
for (const obstacle of staticObstacles) {
if (overlaps(f, obstacle.rect, 0.15)) {
return {
valid: false,
reason: `Move away from ${obstacle.name.toLowerCase()}.`,
};
}
}
for (const activity of activities) {
if (overlaps(f, activity.footprint)) {
return {
valid: false,
reason: "Temporary activities cannot overlap.",
};
}
}
const view = getViewArea(type, x, z, rotation);
if (view) {
if (
view.rect.minX < -29 ||
view.rect.maxX > 29 ||
view.rect.minZ < -23 ||
view.rect.maxZ > 23
) {
return {
valid: false,
reason: "The required viewing area extends beyond the plaza.",
};
}
for (const protectedArea of protectedAreas) {
if (overlaps(view.rect, protectedArea.rect)) {
return {
valid: false,
reason: `The viewing area overlaps ${protectedArea.name}.`,
};
}
}
for (const obstacle of staticObstacles) {
if (overlaps(view.rect, obstacle.rect)) {
return {
valid: false,
reason: `The viewing area is blocked by ${obstacle.name.toLowerCase()}.`,
};
}
}
for (const activity of activities) {
if (
overlaps(view.rect, activity.footprint) ||
(activity.viewRect && overlaps(view.rect, activity.viewRect))
) {
return {
valid: false,
reason: "The new viewing area conflicts with another activity.",
};
}
}
}
const candidateFootprints = [
...activities.map((activity) => activity.footprint),
f,
];
if (!checkConnectivity(candidateFootprints)) {
return {
valid: false,
reason: "This placement would disconnect one or more entrances.",
};
}
return {
valid: true,
reason: "Ready to place: sightlines and gate connections are clear.",
footprint,
view,
};
}
// -------------------------------------------------------------------------
// Temporary activity models
// -------------------------------------------------------------------------
function createTemporaryModel(type) {
const group = new THREE.Group();
if (type === "stage") {
box(group, 4.7, 0.45, 2.7, M.navy, 0, 0.225, 0);
box(group, 4.5, 0.1, 2.5, M.woodDark, 0, 0.49, 0);
box(group, 0.13, 2.6, 0.13, M.navy, -2.1, 1.7, -1.05);
box(group, 0.13, 2.6, 0.13, M.navy, 2.1, 1.7, -1.05);
box(group, 4.35, 0.16, 2.4, M.orange, 0, 2.95, -0.1);
box(group, 3.8, 0.09, 0.12, M.cream, 0, 2.78, 1.05);
const performer = createPerson(M.yellow, 0.95);
performer.position.set(0, 0.55, 0.1);
group.add(performer);
animatedPeople.push({
group: performer,
phase: Math.random() * 8,
kind: "performer",
});
}
if (type === "cart") {
box(group, 3.1, 0.9, 1.45, M.wood, 0, 0.45, 0);
box(group, 3.35, 0.14, 1.7, M.woodDark, 0, 0.94, 0);
box(group, 0.12, 1.9, 0.12, M.navy, -1.45, 1.3, -0.6);
box(group, 0.12, 1.9, 0.12, M.navy, 1.45, 1.3, -0.6);
box(group, 3.5, 0.16, 2, M.orange, 0, 2.2, 0);
box(group, 0.2, 0.65, 1.55, M.black, -1.2, 0.25, 0.72);
box(group, 0.2, 0.65, 1.55, M.black, 1.2, 0.25, 0.72);
for (let i = 0; i < 4; i++) {
const fruit = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.19, 0),
[M.orange, M.greenLight, M.yellow, M.pink][i],
);
fruit.position.set(-0.9 + i * 0.6, 1.12, 0.05);
fruit.castShadow = true;
group.add(fruit);
}
}
if (type === "food") {
box(group, 3.5, 1.65, 1.8, M.cream, 0, 0.83, 0);
box(group, 2.9, 0.72, 0.09, M.orange, 0, 1.14, 0.94);
box(group, 3.9, 0.17, 2.35, M.navy, 0, 2.55, -0.1);
box(group, 0.12, 2.1, 0.12, M.navy, -1.7, 1.55, -0.9);
box(group, 0.12, 2.1, 0.12, M.navy, 1.7, 1.55, -0.9);
box(group, 3.25, 0.13, 0.65, M.woodDark, 0, 1.38, 0.45);
}
if (type === "screen") {
box(group, 0.16, 2.45, 0.16, M.navy, -1.25, 1.22, 0);
box(group, 0.16, 2.45, 0.16, M.navy, 1.25, 1.22, 0);
box(group, 3.05, 1.65, 0.16, M.black, 0, 2.05, 0);
box(group, 2.78, 1.38, 0.035, M.navyLight, 0, 2.05, 0.095);
box(group, 1.45, 0.09, 0.05, M.orange, -0.25, 2.2, 0.13);
box(group, 1.9, 0.09, 0.05, M.cream, -0.45, 1.93, 0.13);
}
if (type === "seating") {
box(group, 3.7, 0.25, 0.72, M.wood, 0, 0.53, -0.43);
box(group, 3.7, 0.8, 0.16, M.woodDark, 0, 0.92, -0.03);
box(group, 0.15, 0.5, 0.65, M.navy, -1.45, 0.25, -0.43);
box(group, 0.15, 0.5, 0.65, M.navy, 1.45, 0.25, -0.43);
box(group, 3.7, 0.25, 0.72, M.wood, 0, 0.53, 0.43);
box(group, 3.7, 0.8, 0.16, M.woodDark, 0, 0.92, 0.03);
box(group, 0.15, 0.5, 0.65, M.navy, -1.45, 0.25, 0.43);
box(group, 0.15, 0.5, 0.65, M.navy, 1.45, 0.25, 0.43);
}
return group;
}
function addActivity(type, x, z, rotation) {
const id = activities.length + 1;
const model = createTemporaryModel(type);
model.position.set(x, 0, z);
model.rotation.y = rotation * (Math.PI / 2);
model.userData.activityId = id;
tempGroup.add(model);
const footprint = getFootprint(type, x, z, rotation);
const view = getViewArea(type, x, z, rotation);
const activity = {
id,
type,
x,
z,
rotation,
model,
footprint: footprint.rect,
viewRect: view?.rect ?? null,
};
activities.push(activity);
tempModels.set(id, model);
updateStats();
return activity;
}
function removeActivity(id) {
const index = activities.findIndex((activity) => activity.id === id);
if (index === -1) return;
const activity = activities[index];
tempGroup.remove(activity.model);
activities.splice(index, 1);
updateStats();
}
// -------------------------------------------------------------------------
// Placement preview
// -------------------------------------------------------------------------
const pickMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide,
});
const pickPlane = new THREE.Mesh(
new THREE.PlaneGeometry(60, 48),
pickMaterial,
);
pickPlane.rotation.x = -Math.PI / 2;
pickPlane.position.y = 0.12;
scene.add(pickPlane);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerKnown = false;
let activeTool = null;
let toolRotation = 0;
let ghost = null;
let ghostDirty = false;
let placementPoint = null;
const ghostFootMaterial = new THREE.MeshBasicMaterial({
color: "#4c9c79",
transparent: true,
opacity: 0.38,
depthWrite: false,
side: THREE.DoubleSide,
});
const ghostViewMaterial = new THREE.MeshBasicMaterial({
color: "#e5a14f",
transparent: true,
opacity: 0.25,
depthWrite: false,
side: THREE.DoubleSide,
});
const ghostLineMaterial = new THREE.LineBasicMaterial({
color: "#3f8c6d",
transparent: true,
opacity: 0.95,
});
function buildGhost(type) {
if (ghost) scene.remove(ghost);
ghost = new THREE.Group();
const foot = new THREE.Mesh(
new THREE.PlaneGeometry(1, 1),
ghostFootMaterial,
);
foot.rotation.x = -Math.PI / 2;
foot.position.y = 0.11;
ghost.add(foot);
const boxOutline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(1, 1, 1)),
ghostLineMaterial,
);
boxOutline.position.y = 0.9;
ghost.add(boxOutline);
const view = new THREE.Mesh(
new THREE.PlaneGeometry(1, 1),
ghostViewMaterial,
);
view.rotation.x = -Math.PI / 2;
view.position.y = 0.105;
ghost.add(view);
ghost.userData.foot = foot;
ghost.userData.boxOutline = boxOutline;
ghost.userData.view = view;
ghost.visible = false;
scene.add(ghost);
}
function updatePointer(event) {
const bounds = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y = -((event.clientY - bounds.top) / bounds.height) * 2 + 1;
pointerKnown = true;
}
function updateGhost() {
ghostDirty = false;
if (!activeTool || !ghost || !pointerKnown) {
if (ghost) ghost.visible = false;
return;
}
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObject(pickPlane, false)[0];
if (!hit) {
ghost.visible = false;
placementPoint = null;
return;
}
const x = Math.round(hit.point.x * 2) / 2;
const z = Math.round(hit.point.z * 2) / 2;
const validation = validatePlacement(activeTool, x, z, toolRotation);
placementPoint = { x, z, validation };
ghost.visible = true;
ghost.position.set(x, 0, z);
const footprint = validation.footprint ?? getFootprint(activeTool, x, z, toolRotation);
ghost.userData.foot.scale.set(footprint.w, footprint.d, 1);
ghost.userData.boxOutline.scale.set(
footprint.w,
1.8,
footprint.d,
);
const color = validation.valid ? "#4c9c79" : "#c84e43";
ghostFootMaterial.color.set(color);
ghostLineMaterial.color.set(color);
const view = validation.view ?? getViewArea(activeTool, x, z, toolRotation);
ghost.userData.view.visible = Boolean(view);
if (view) {
ghost.userData.view.scale.set(view.w, view.d, 1);
ghost.userData.view.position.set(view.cx - x, 0.105, view.cz - z);
ghostViewMaterial.color.set(validation.valid ? "#e5a14f" : "#c84e43");
}
const liveRule = document.querySelector("#liveRule");
liveRule.textContent = validation.reason;
liveRule.classList.toggle("invalid", !validation.valid);
}
function attemptPlacement() {
if (!activeTool || !placementPoint) return;
const { x, z, validation } = placementPoint;
if (!validation.valid) {
showToast(validation.reason, true);
return;
}
addActivity(activeTool, x, z, toolRotation);
showToast(`${activityDefs[activeTool].label} placed successfully.`);
updateGhost();
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, and touch pinch
// -------------------------------------------------------------------------
const controls = {
theta: 0.72,
phi: 0.9,
radius: 62,
target: new THREE.Vector3(0, 0, 0),
};
function applyCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta),
);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
function resetCamera() {
controls.theta = 0.72;
controls.phi = 0.9;
controls.radius = 62;
controls.target.set(0, 0, 0);
applyCamera();
}
function topCamera() {
controls.theta = 0;
controls.phi = 0.08;
controls.radius = 67;
controls.target.set(0, 0, 0);
applyCamera();
}
const pointers = new Map();
let dragMode = null;
let lastX = 0;
let lastY = 0;
let dragDistance = 0;
let pinchDistance = 0;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
updatePointer(event);
if (pointers.size === 2) {
const values = [...pointers.values()];
pinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y,
);
dragMode = "pinch";
return;
}
dragMode = event.button === 2 || event.button === 1 ? "pan" : "orbit";
lastX = event.clientX;
lastY = event.clientY;
dragDistance = 0;
if (ghost) ghost.visible = false;
});
canvas.addEventListener("pointermove", (event) => {
updatePointer(event);
if (pointers.has(event.pointerId)) {
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
}
if (pointers.size === 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y,
);
if (pinchDistance > 0 && distance > 0) {
controls.radius = THREE.MathUtils.clamp(
controls.radius * (pinchDistance / distance),
18,
105,
);
applyCamera();
}
pinchDistance = distance;
return;
}
if (!dragMode) {
if (activeTool) ghostDirty = true;
return;
}
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
dragDistance += Math.abs(dx) + Math.abs(dy);
if (dragMode === "orbit") {
controls.theta -= dx * 0.0055;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.0045,
0.08,
1.42,
);
}
if (dragMode === "pan") {
camera.updateMatrixWorld();
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrix, 0);
right.y = 0;
right.normalize();
const forward = controls.target.clone().sub(camera.position);
forward.y = 0;
forward.normalize();
const scale = controls.radius * 0.00145;
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(forward, -dy * scale);
controls.target.x = THREE.MathUtils.clamp(
controls.target.x,
-22,
22,
);
controls.target.z = THREE.MathUtils.clamp(
controls.target.z,
-18,
18,
);
}
applyCamera();
});
canvas.addEventListener("pointerup", (event) => {
const wasClick =
dragMode === "orbit" && dragDistance < 6 && event.button === 0;
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (wasClick) {
updatePointer(event);
ghostDirty = true;
updateGhost();
attemptPlacement();
}
if (pointers.size === 0) {
dragMode = null;
if (activeTool) ghostDirty = true;
}
});
canvas.addEventListener("pointercancel", (event) => {
pointers.delete(event.pointerId);
dragMode = null;
if (activeTool) ghostDirty = true;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * (event.deltaY > 0 ? 1.09 : 0.92),
18,
105,
);
applyCamera();
},
{ passive: false },
);
canvas.addEventListener("dblclick", (event) => {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(tempModels.values(), true);
if (!hits.length) return;
let object = hits[0].object;
while (object && !object.userData.activityId) object = object.parent;
if (object?.userData.activityId) {
const id = object.userData.activityId;
removeActivity(id);
showToast("Temporary activity removed.");
}
});
// -------------------------------------------------------------------------
// Interface
// -------------------------------------------------------------------------
const toolButtons = [...document.querySelectorAll(".tool")];
const liveRule = document.querySelector("#liveRule");
const toast = document.querySelector("#toast");
let toastTimer;
function showToast(message, error = false) {
toast.textContent = message;
toast.classList.toggle("error", error);
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2600);
}
function selectTool(type) {
if (activeTool === type) {
activeTool = null;
} else {
activeTool = type;
buildGhost(type);
if (pointerKnown) {
ghostDirty = true;
updateGhost();
}
}
toolButtons.forEach((button) => {
const selected = button.dataset.type === activeTool;
button.classList.toggle("active", selected);
button.setAttribute("aria-pressed", String(selected));
});
if (activeTool) {
liveRule.textContent = "Move the pointer over the plaza to preview the activity.";
liveRule.classList.remove("invalid");
} else {
liveRule.textContent = "Select an activity to begin.";
liveRule.classList.remove("invalid");
if (ghost) ghost.visible = false;
}
}
toolButtons.forEach((button) => {
button.addEventListener("click", () => selectTool(button.dataset.type));
});
document.querySelector("#rotateTool").addEventListener("click", () => {
if (!activeTool) {
showToast("Select an activity before rotating it.", true);
return;
}
toolRotation = (toolRotation + 1) % 4;
showToast("Activity rotated 90 degrees.");
ghostDirty = true;
updateGhost();
});
document.querySelector("#undoTool").addEventListener("click", () => {
const activity = activities.at(-1);
if (!activity) {
showToast("There are no temporary activities to undo.", true);
return;
}
removeActivity(activity.id);
showToast("Last temporary activity removed.");
});
document.querySelector("#clearTool").addEventListener("click", () => {
if (!activities.length) {
showToast("The plaza already has no temporary activities.", true);
return;
}
while (activities.length) removeActivity(activities.at(-1).id);
showToast("All temporary activities removed.");
});
document.querySelector("#cancelTool").addEventListener("click", () => {
if (activeTool) selectTool(activeTool);
});
document.querySelector("#resetView").addEventListener("click", resetCamera);
document.querySelector("#topView").addEventListener("click", topCamera);
document.querySelector("#togglePanel").addEventListener("click", () => {
document.querySelector("#panel").classList.toggle("hidden");
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r" && activeTool) {
toolRotation = (toolRotation + 1) % 4;
ghostDirty = true;
updateGhost();
}
if (event.key === "Escape" && activeTool) {
selectTool(activeTool);
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "z") {
event.preventDefault();
const activity = activities.at(-1);
if (activity) removeActivity(activity.id);
}
});
function updateStats() {
document.querySelector("#activityCount").textContent = activities.length;
const connected = checkConnectivity(
activities.map((activity) => activity.footprint),
);
document.querySelector("#gateCount").textContent = connected
? "4 / 4"
: "Review";
document.querySelector("#viewingCount").textContent = connected
? "Clear"
: "Conflict";
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
for (const person of animatedPeople) {
const { group, phase, kind } = person;
const motion = Math.sin(time * (kind === "performer" ? 3.2 : 1.4) + phase);
group.rotation.z = motion * (kind === "performer" ? 0.035 : 0.012);
if (group.userData.arms) {
group.userData.arms.forEach((arm, index) => {
arm.rotation.z =
(index === 0 ? 1 : -1) *
(kind === "performer"
? 0.42 + Math.sin(time * 3.5 + phase + index) * 0.45
: 0.12 + motion * 0.025);
});
}
}
if (ghostDirty) updateGhost();
renderer.render(scene, camera);
}
applyCamera();
updateStats();
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
setTimeout(() => {
showToast("Choose an activity and click a clear location in the plaza.");
}, 500);
});
</script>
</body>
</html>
```