Prompt specification
Build or customize the Three.js 3D scene "Arrange a temporary exhibit without blocking square access" 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: Arrange a temporary exhibit without blocking square access
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/arrange-a-temporary-exhibit-without-blocking-square-ac-x2996
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Fatahillah Square in Jakarta's Kota Tua. Let users place fictional exhibit panels and seating in a schematic square. Preserve entrance routes and compare open space while keeping the historic building perimeter fixed. Voxel style.
Working Three.js Starter (arrange-a-temporary-exhibit-without-blocking-square-ac-x2996):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Fatahillah Square — Schematic Placement Study</title>
<style>
:root {
--ink: #17332f;
--muted: #60756f;
--paper: rgba(249, 247, 237, 0.94);
--line: rgba(23, 51, 47, 0.14);
--green: #167a68;
--green-soft: #d8ebe3;
--orange: #d66a3d;
--red: #c94d55;
--gold: #d6a83d;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 22px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 18px 20px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 16px 42px rgba(24, 52, 46, 0.12);
backdrop-filter: blur(12px);
}
.eyebrow {
margin: 0 0 7px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 37px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.brand p:last-child {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.panel {
position: absolute;
top: 22px;
right: 24px;
width: 294px;
padding: 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 16px 42px rgba(24, 52, 46, 0.12);
backdrop-filter: blur(12px);
pointer-events: auto;
user-select: none;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.fixed-badge {
padding: 4px 7px;
border-radius: 999px;
background: var(--green-soft);
color: var(--green);
font-size: 9px;
letter-spacing: 0.08em;
}
.tool-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
min-height: 38px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
}
button:hover:not(:disabled) {
background: white;
border-color: rgba(22, 122, 104, 0.38);
}
button:active:not(:disabled) {
transform: translateY(1px);
}
button.active {
border-color: var(--green);
background: var(--green);
color: white;
}
button:disabled {
cursor: default;
opacity: 0.38;
}
.wide { grid-column: 1 / -1; }
.divider {
height: 1px;
margin: 14px 0;
background: var(--line);
}
.row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.compare-label {
display: flex;
align-items: center;
gap: 8px;
font-size: 12px;
font-weight: 750;
cursor: pointer;
}
input[type="checkbox"] {
width: 17px;
height: 17px;
accent-color: var(--green);
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 11px;
}
.stat {
min-height: 63px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.58);
}
.stat span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.stat strong {
display: block;
font-size: 17px;
line-height: 1;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 9px 12px;
margin-top: 11px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.legend i {
display: inline-block;
width: 9px;
height: 9px;
margin-right: 4px;
border-radius: 2px;
vertical-align: -1px;
}
.counts {
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.status {
position: absolute;
bottom: 20px;
left: 50%;
max-width: calc(100vw - 32px);
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--paper);
box-shadow: 0 10px 30px rgba(24, 52, 46, 0.1);
color: var(--muted);
font-size: 11px;
font-weight: 650;
text-align: center;
transform: translateX(-50%);
backdrop-filter: blur(10px);
}
.status strong { color: var(--ink); }
.compass {
position: absolute;
bottom: 22px;
left: 24px;
display: grid;
place-items: center;
width: 48px;
height: 48px;
border: 1px solid var(--line);
border-radius: 50%;
background: var(--paper);
color: var(--green);
font-size: 10px;
font-weight: 900;
box-shadow: 0 10px 30px rgba(24, 52, 46, 0.08);
}
.compass::after {
content: "";
position: absolute;
top: 6px;
border-right: 4px solid transparent;
border-bottom: 9px solid var(--green);
border-left: 4px solid transparent;
}
.compass span { margin-top: 9px; }
#toast {
position: absolute;
bottom: 74px;
left: 50%;
max-width: min(430px, calc(100vw - 32px));
padding: 10px 14px;
border-radius: 9px;
background: #17332f;
color: white;
font-size: 12px;
font-weight: 700;
opacity: 0;
transform: translate(-50%, 8px);
transition: opacity 160ms ease, transform 160ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.brand p:last-child { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 63px;
width: min(294px, calc(100vw - 24px));
max-height: 58vh;
overflow-y: auto;
padding: 12px;
}
.status {
bottom: 12px;
width: calc(100vw - 84px);
border-radius: 10px;
}
.compass {
bottom: 12px;
left: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive schematic model of Fatahillah Square"></canvas>
<div id="ui">
<section class="brand">
<p class="eyebrow">Kota Tua · Jakarta</p>
<h1>Fatahillah<br />Square</h1>
<p>
Schematic open-space placement study. Exhibits and seating are fictional;
the historic building perimeter and entrance approaches remain fixed.
</p>
</section>
<aside class="panel" aria-label="Scene controls">
<div class="panel-title">
Placement tools
<span class="fixed-badge">Perimeter fixed</span>
</div>
<div class="tool-grid">
<button class="active" data-tool="exhibit">+ Exhibit panel</button>
<button data-tool="bench">+ Seating</button>
<button class="wide" data-tool="select">↖ Select & move</button>
<button id="rotate">Rotate 90°</button>
<button id="delete">Delete</button>
<button id="undo">Undo</button>
<button id="clear">Clear all</button>
</div>
<div class="divider"></div>
<label class="compare-label">
<input id="compare" type="checkbox" checked />
Compare remaining open space
</label>
<div class="stats">
<div class="stat">
<span>Baseline open</span>
<strong id="baseline-area">—</strong>
</div>
<div class="stat">
<span>Still open</span>
<strong id="current-area">—</strong>
</div>
<div class="stat">
<span>Open retained</span>
<strong id="retained-area">—</strong>
</div>
<div class="stat">
<span>New footprint</span>
<strong id="used-area">—</strong>
</div>
</div>
<div class="legend">
<span><i style="background:#63b69b"></i>Remaining open</span>
<span><i style="background:#df8050"></i>New use</span>
<span><i style="background:#e5cf8f"></i>Protected route</span>
</div>
<div class="divider"></div>
<div id="counts" class="counts">No exhibits or seating placed.</div>
</aside>
<div class="compass"><span>N</span></div>
<div class="status" id="status">
<strong>Exhibit tool:</strong> click the square to place. Right-drag orbits; wheel zooms.
</div>
<div id="toast" role="status" aria-live="polite"></div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#dce8e2");
scene.fog = new THREE.Fog("#dce8e2", 150, 280);
const camera = new THREE.OrthographicCamera(-80, 80, 50, -50, 0.1, 400);
const cameraState = {
target: new THREE.Vector3(0, 0, 0),
yaw: 0.63,
pitch: 0.76,
zoom: 1,
distance: 140
};
const hemisphere = new THREE.HemisphereLight("#f7f2df", "#668479", 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff4d8", 3.0);
sun.position.set(-58, 105, 42);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -90;
sun.shadow.camera.right = 90;
sun.shadow.camera.top = 80;
sun.shadow.camera.bottom = -80;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 230;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fixedRoot = new THREE.Group();
const itemRoot = new THREE.Group();
scene.add(fixedRoot, itemRoot);
const PLAZA = { halfW: 48, halfD: 32 };
const MONUMENT = { x: 0, z: 0, radius: 5.0 };
// Protected entrance approaches. All dimensions are schematic metres.
const routes = [
{ from: [0, -32], to: [0, 0], width: 7.0, name: "station" },
{ from: [-48, 9], to: [0, 0], width: 6.0, name: "west" },
{ from: [48, -5], to: [0, 0], width: 6.0, name: "east" },
{ from: [-12, 32], to: [0, 0], width: 6.0, name: "south" }
];
const materialCache = new Map();
function standardMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.94,
metalness: 0
})
);
}
return materialCache.get(color);
}
function box(parent, w, h, d, x, y, z, color, material = null) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material || standardMaterial(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Fixed context: paving, building perimeter, routes, gates and monument
// -------------------------------------------------------------------------
box(fixedRoot, 128, 1, 94, 0, -0.72, 0, "#91aaa0");
box(fixedRoot, 96, 0.4, 64, 0, -0.2, 0, "#d9cfad");
// Subtle voxel paving grid.
const gridVertices = [];
for (let x = -48; x <= 48; x += 4) {
gridVertices.push(x, 0.008, -32, x, 0.008, 32);
}
for (let z = -32; z <= 32; z += 4) {
gridVertices.push(-48, 0.008, z, 48, 0.008, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const pavingGrid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: "#7e826e",
transparent: true,
opacity: 0.13
})
);
fixedRoot.add(pavingGrid);
function createBuilding(x, z, w, d, h, color, face) {
const group = new THREE.Group();
group.position.set(x, 0, z);
fixedRoot.add(group);
box(group, w, h, d, 0, h / 2, 0, color);
box(group, w + 0.7, 0.45, d + 0.7, 0, h + 0.12, 0, "#6c756c");
box(group, w - 1.2, 0.55, d - 1.2, 0, h + 0.6, 0, "#56645d");
const rows = Math.max(1, Math.floor((h - 1.5) / 3.5));
const horizontalLength = face === "+z" || face === "-z" ? w : d;
for (let row = 0; row < rows; row++) {
const wy = 2.1 + row * 3.4;
const count = Math.max(2, Math.floor(horizontalLength / 4));
for (let i = 0; i < count; i++) {
const offset = (i - (count - 1) / 2) * 3.7;
const windowColor = (i + row) % 4 === 0 ? "#e8c978" : "#304b49";
if (face === "+z") {
box(group, 1.35, 1.55, 0.12, offset, wy, d / 2 + 0.08, windowColor);
} else if (face === "-z") {
box(group, 1.35, 1.55, 0.12, offset, wy, -d / 2 - 0.08, windowColor);
} else if (face === "+x") {
box(group, 0.12, 1.55, 1.35, w / 2 + 0.08, wy, offset, windowColor);
} else {
box(group, 0.12, 1.55, 1.35, -w / 2 - 0.08, wy, offset, windowColor);
}
}
}
// A simple central door.
if (face === "+z" || face === "-z") {
const sign = face === "+z" ? 1 : -1;
box(group, 2.1, 3.1, 0.18, 0, 1.55, sign * (d / 2 + 0.1), "#314b47");
} else {
const sign = face === "+x" ? 1 : -1;
box(group, 0.18, 3.1, 2.1, sign * (w / 2 + 0.1), 1.55, 0, "#314b47");
}
return group;
}
// North edge, including the visually recognizable station side.
createBuilding(-35, -41, 26, 13, 8.5, "#b88769", "+z");
createBuilding(-12, -41, 18, 13, 7.0, "#c49b78", "+z");
createBuilding(15, -41, 19, 13, 9.0, "#a96f58", "+z");
createBuilding(36, -41, 22, 13, 7.7, "#c3a178", "+z");
// South edge.
createBuilding(-35, 41, 24, 13, 7.5, "#b98b69", "-z");
createBuilding(1, 41, 18, 13, 8.0, "#c09c78", "-z");
createBuilding(28, 41, 30, 13, 7.0, "#aa7861", "-z");
// West edge.
createBuilding(-55, -16, 13, 20, 8.0, "#bc8d6c", "+x");
createBuilding(-55, 22, 13, 14, 7.0, "#a87860", "+x");
// East edge.
createBuilding(55, -18, 13, 20, 8.5, "#b68b68", "-x");
createBuilding(55, 15, 13, 22, 7.2, "#c49b74", "-x");
// Kota Tua Station marker and clock.
box(fixedRoot, 3.1, 3.1, 0.25, 15, 9.5, -34.25, "#ead9ae");
box(fixedRoot, 2.1, 2.1, 0.32, 15, 9.5, -34.05, "#284743");
box(fixedRoot, 0.18, 1.35, 0.15, 15, 9.72, -33.85, "#f1dfad");
box(fixedRoot, 0.9, 0.18, 0.15, 15.42, 9.5, -33.84, "#f1dfad");
function createLabel(text, x, y, z, width = 22) {
const c = document.createElement("canvas");
c.width = 512;
c.height = 96;
const ctx = c.getContext("2d");
ctx.fillStyle = "rgba(23,51,47,0.88)";
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = "#f5edcf";
ctx.font = "800 38px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, c.width / 2, c.height / 2);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
})
);
sprite.position.set(x, y, z);
sprite.scale.set(width, width * 96 / 512, 1);
sprite.renderOrder = 20;
fixedRoot.add(sprite);
}
createLabel("KOTA TUA STATION", 15, 13.6, -40, 24);
// Fixed dashed outline makes the immovable historic perimeter explicit.
const perimeterPoints = [
new THREE.Vector3(-48, 0.085, -32),
new THREE.Vector3(48, 0.085, -32),
new THREE.Vector3(48, 0.085, 32),
new THREE.Vector3(-48, 0.085, 32),
new THREE.Vector3(-48, 0.085, -32)
];
const perimeter = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(perimeterPoints),
new THREE.LineDashedMaterial({
color: "#375d55",
dashSize: 1.3,
gapSize: 0.8,
transparent: true,
opacity: 0.7
})
);
perimeter.computeLineDistances();
fixedRoot.add(perimeter);
// Protected route paving and simple entrance gates.
for (const route of routes) {
const [x1, z1] = route.from;
const [x2, z2] = route.to;
const dx = x2 - x1;
const dz = z2 - z1;
const length = Math.hypot(dx, dz);
const angle = -Math.atan2(dz, dx);
const path = box(
fixedRoot,
length,
0.045,
route.width,
(x1 + x2) / 2,
0.035,
(z1 + z2) / 2,
"#e6d29a"
);
path.rotation.y = angle;
path.castShadow = false;
const direction = new THREE.Vector2(dx, dz).normalize();
const perpendicular = new THREE.Vector2(-direction.y, direction.x);
for (const side of [-1, 1]) {
const gx = x1 + perpendicular.x * side * (route.width / 2 + 0.45);
const gz = z1 + perpendicular.y * side * (route.width / 2 + 0.45);
box(fixedRoot, 0.65, 2.8, 0.65, gx, 1.4, gz, "#61766c");
}
const lintel = box(
fixedRoot,
route.width + 1.3,
0.45,
0.65,
x1,
2.85,
z1,
"#61766c"
);
lintel.rotation.y = -Math.atan2(perpendicular.y, perpendicular.x);
}
// Fixed central civic datum, represented as a low voxel monument.
const monument = new THREE.Group();
fixedRoot.add(monument);
box(monument, 6.2, 0.55, 6.2, 0, 0.28, 0, "#77877d");
box(monument, 4.5, 0.75, 4.5, 0, 0.9, 0, "#c5b994");
box(monument, 2.8, 1.55, 2.8, 0, 2.02, 0, "#e0d2ad");
box(monument, 1.2, 0.8, 1.2, 0, 3.15, 0, "#a89f85");
// -------------------------------------------------------------------------
// Item geometry and placement rules
// -------------------------------------------------------------------------
const dimensions = {
exhibit: { w: 4.6, d: 1.5 },
bench: { w: 4.8, d: 1.7 }
};
function createItemModel(type) {
const group = new THREE.Group();
if (type === "exhibit") {
box(group, 0.22, 1.35, 0.28, -1.65, 0.68, 0, "#516b63");
box(group, 0.22, 1.35, 0.28, 1.65, 0.68, 0, "#516b63");
box(group, 4.4, 2.25, 0.24, 0, 1.83, 0, "#eadfbf");
box(group, 4.0, 1.85, 0.08, 0, 1.83, 0.16, "#285c59");
box(group, 3.55, 0.26, 0.08, 0, 2.47, 0.22, "#efad52");
// Fictional voxel artwork on the panel.
box(group, 0.52, 0.52, 0.07, -1.25, 1.72, 0.23, "#ef8354");
box(group, 0.52, 0.92, 0.07, -0.6, 1.85, 0.23, "#e7c65f");
box(group, 0.52, 0.38, 0.07, 0.05, 1.5, 0.23, "#77b6a3");
box(group, 0.52, 1.12, 0.07, 0.7, 1.93, 0.23, "#d6dfca");
box(group, 0.52, 0.55, 0.07, 1.35, 1.61, 0.23, "#e76d51");
} else {
// Voxel bench: five timber slats and dark supports.
for (let i = 0; i < 5; i++) {
box(
group,
4.8,
0.22,
0.24,
0,
0.92,
-0.64 + i * 0.32,
i % 2 ? "#ad7547" : "#bd8955"
);
}
box(group, 0.38, 0.82, 1.25, -1.7, 0.43, 0, "#435d56");
box(group, 0.38, 0.82, 1.25, 1.7, 0.43, 0, "#435d56");
}
return group;
}
function getCorners(x, z, w, d, rotation) {
const c = Math.cos(rotation);
const s = Math.sin(rotation);
const local = [
[-w / 2, -d / 2],
[w / 2, -d / 2],
[w / 2, d / 2],
[-w / 2, d / 2]
];
return local.map(([lx, lz]) => [
x + lx * c + lz * s,
z - lx * s + lz * c
]);
}
function getAxes(rotation) {
const c = Math.cos(rotation);
const s = Math.sin(rotation);
return [
[c, -s],
[s, c]
];
}
function routePolygon(route) {
const [x1, z1] = route.from;
const [x2, z2] = route.to;
const dx = x2 - x1;
const dz = z2 - z1;
const length = Math.hypot(dx, dz);
const direction = [dx / length, dz / length];
const perpendicular = [-direction[1], direction[0]];
const center = [(x1 + x2) / 2, (z1 + z2) / 2];
const along = direction.map(v => v * length / 2);
const across = perpendicular.map(v => v * route.width / 2);
return [
[
center[0] + along[0] + across[0],
center[1] + along[1] + across[1]
],
[
center[0] + along[0] - across[0],
center[1] + along[1] - across[1]
],
[
center[0] - along[0] - across[0],
center[1] - along[1] - across[1]
],
[
center[0] - along[0] + across[0],
center[1] - along[1] + across[1]
]
];
}
function polygonsOverlap(a, b, gap = 0) {
const axes = [];
for (const polygon of [a, b]) {
for (let i = 0; i < polygon.length; i++) {
const p = polygon[i];
const q = polygon[(i + 1) % polygon.length];
const ex = q[0] - p[0];
const ez = q[1] - p[1];
const length = Math.hypot(ex, ez) || 1;
axes.push([-ez / length, ex / length]);
}
}
for (const axis of axes) {
const project = polygon => polygon.map(p =>
p[0] * axis[0] + p[1] * axis[1]
);
const pa = project(a);
const pb = project(b);
const minA = Math.min(...pa);
const maxA = Math.max(...pa);
const minB = Math.min(...pb);
const maxB = Math.max(...pb);
if (maxA <= minB + gap || maxB <= minA + gap) {
return false;
}
}
return true;
}
function pointInRoute(x, z, route) {
const [x1, z1] = route.from;
const [x2, z2] = route.to;
const dx = x2 - x1;
const dz = z2 - z1;
const lengthSq = dx * dx + dz * dz;
const t = ((x - x1) * dx + (z - z1) * dz) / lengthSq;
if (t < 0 || t > 1) return false;
const closestX = x1 + dx * t;
const closestZ = z1 + dz * t;
return Math.hypot(x - closestX, z - closestZ) <= route.width / 2;
}
function pointInItem(x, z, item) {
const dx = x - item.x;
const dz = z - item.z;
const c = Math.cos(item.rotation);
const s = Math.sin(item.rotation);
const localX = c * dx - s * dz;
const localZ = s * dx + c * dz;
const dim = dimensions[item.type];
return (
Math.abs(localX) <= dim.w / 2 &&
Math.abs(localZ) <= dim.d / 2
);
}
function itemOverlapsMonument(x, z, w, d, rotation) {
const c = Math.cos(rotation);
const s = Math.sin(rotation);
const dx = MONUMENT.x - x;
const dz = MONUMENT.z - z;
const localX = c * dx - s * dz;
const localZ = s * dx + c * dz;
const qx = Math.max(Math.abs(localX) - w / 2, 0);
const qz = Math.max(Math.abs(localZ) - d / 2, 0);
return Math.hypot(qx, qz) < MONUMENT.radius + 0.3;
}
function isValidPlacement(x, z, type, rotation, ignoreId = null) {
const dim = dimensions[type];
if (
Math.abs(x) > PLAZA.halfW - dim.w / 2 - 0.35 ||
Math.abs(z) > PLAZA.halfD - dim.d / 2 - 0.35
) {
return false;
}
if (itemOverlapsMonument(x, z, dim.w, dim.d, rotation)) {
return false;
}
const candidate = getCorners(x, z, dim.w, dim.d, rotation);
for (const route of routes) {
if (polygonsOverlap(candidate, routePolygon(route), 0.22)) {
return false;
}
}
for (const item of items) {
if (item.id === ignoreId) continue;
const other = getCorners(
item.x,
item.z,
dimensions[item.type].w,
dimensions[item.type].d,
item.rotation
);
if (polygonsOverlap(candidate, other, 0.4)) {
return false;
}
}
return true;
}
// -------------------------------------------------------------------------
// Open-space comparison grid
// -------------------------------------------------------------------------
const GRID = {
cell: 2,
cols: PLAZA.halfW * 2 / 2,
rows: PLAZA.halfD * 2 / 2
};
const baselineCells = [];
for (let row = 0; row < GRID.rows; row++) {
for (let col = 0; col < GRID.cols; col++) {
const x = -PLAZA.halfW + (col + 0.5) * GRID.cell;
const z = -PLAZA.halfD + (row + 0.5) * GRID.cell;
const onRoute = routes.some(route => pointInRoute(x, z, route));
const onMonument =
Math.hypot(x - MONUMENT.x, z - MONUMENT.z) <= MONUMENT.radius;
if (!onRoute && !onMonument) {
baselineCells.push({ x, z, col, row });
}
}
}
const overlayCanvas = document.createElement("canvas");
overlayCanvas.width = GRID.cols * 10;
overlayCanvas.height = GRID.rows * 10;
const overlayContext = overlayCanvas.getContext("2d");
const overlayTexture = new THREE.CanvasTexture(overlayCanvas);
overlayTexture.colorSpace = THREE.SRGBColorSpace;
overlayTexture.magFilter = THREE.NearestFilter;
overlayTexture.minFilter = THREE.NearestFilter;
const overlay = new THREE.Mesh(
new THREE.PlaneGeometry(PLAZA.halfW * 2, PLAZA.halfD * 2),
new THREE.MeshBasicMaterial({
map: overlayTexture,
transparent: true,
opacity: 0.72,
depthWrite: false,
side: THREE.DoubleSide
})
);
overlay.rotation.x = -Math.PI / 2;
overlay.position.y = 0.062;
overlay.renderOrder = 2;
scene.add(overlay);
const baselineArea = baselineCells.length * GRID.cell * GRID.cell;
// -------------------------------------------------------------------------
// Items, preview, selection and history
// -------------------------------------------------------------------------
let items = [];
let nextId = 1;
let selectedId = null;
let tool = "exhibit";
let placementRotation = 0;
let preview = null;
let previewValid = false;
let dragging = null;
let navigation = null;
let pointerDown = null;
const history = [];
function takeSnapshot() {
return items.map(item => ({
id: item.id,
type: item.type,
x: item.x,
z: item.z,
rotation: item.rotation
}));
}
function pushHistory() {
history.push(takeSnapshot());
if (history.length > 50) history.shift();
document.querySelector("#undo").disabled = history.length === 0;
}
function makeItem(type, x, z, rotation, id) {
const group = createItemModel(type);
group.position.set(x, 0.075, z);
group.rotation.y = rotation;
group.userData.itemId = id;
const item = { id, type, x, z, rotation, group };
items.push(item);
itemRoot.add(group);
return item;
}
function removeAllItems() {
itemRoot.clear();
items = [];
selectedId = null;
}
function restoreSnapshot(snapshot) {
removeAllItems();
for (const saved of snapshot) {
makeItem(saved.type, saved.x, saved.z, saved.rotation, saved.id);
nextId = Math.max(nextId, saved.id + 1);
}
refreshScene();
}
const selectionRoot = new THREE.Group();
const selectionMaterial = new THREE.MeshBasicMaterial({
color: "#f3bf45",
transparent: true,
opacity: 0.88,
depthWrite: false,
side: THREE.DoubleSide
});
const selectionPlane = new THREE.Mesh(
new THREE.PlaneGeometry(1, 1),
selectionMaterial
);
selectionPlane.rotation.x = -Math.PI / 2;
selectionPlane.position.y = 0.09;
selectionRoot.add(selectionPlane);
selectionRoot.visible = false;
scene.add(selectionRoot);
function selectedItem() {
return items.find(item => item.id === selectedId) || null;
}
function updateSelectionMarker(valid = true) {
const item = selectedItem();
selectionRoot.visible = !!item;
if (!item) return;
const dim = dimensions[item.type];
selectionRoot.position.set(item.x, 0, item.z);
selectionRoot.rotation.y = item.rotation;
selectionPlane.scale.set(dim.w + 0.8, dim.d + 0.8, 1);
selectionMaterial.color.set(valid ? "#f3bf45" : "#d94e58");
}
function createPreview(type) {
if (preview) {
scene.remove(preview);
}
preview = createItemModel(type);
preview.position.y = 0.075;
preview.traverse(object => {
if (!object.isMesh) return;
object.material = object.material.clone();
object.material.transparent = true;
object.material.opacity = 0.54;
object.material.depthWrite = false;
object.castShadow = false;
object.receiveShadow = false;
});
const dim = dimensions[type];
const footprint = new THREE.Mesh(
new THREE.PlaneGeometry(dim.w + 0.35, dim.d + 0.35),
new THREE.MeshBasicMaterial({
color: "#3ca985",
transparent: true,
opacity: 0.38,
depthWrite: false,
side: THREE.DoubleSide
})
);
footprint.rotation.x = -Math.PI / 2;
footprint.position.y = 0.02;
footprint.userData.footprint = true;
preview.add(footprint);
scene.add(preview);
preview.visible = false;
}
function updatePreviewPosition(point = null) {
if (tool === "select" || !preview) return;
if (!point) {
preview.visible = false;
return;
}
const x = Math.round(point.x * 2) / 2;
const z = Math.round(point.z * 2) / 2;
preview.position.x = x;
preview.position.z = z;
preview.rotation.y = placementRotation;
preview.visible = true;
previewValid = isValidPlacement(
x,
z,
tool,
placementRotation
);
preview.traverse(object => {
if (object.userData.footprint) {
object.material.color.set(previewValid ? "#3ca985" : "#d94e58");
}
});
}
// -------------------------------------------------------------------------
// UI and statistics
// -------------------------------------------------------------------------
const statusElement = document.querySelector("#status");
const toastElement = document.querySelector("#toast");
let toastTimer;
function toast(message) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toastElement.classList.remove("show");
}, 2100);
}
function formatArea(value) {
return `${Math.round(value).toLocaleString()} m²`;
}
function updateOpenSpace() {
const cellArea = GRID.cell * GRID.cell;
let currentOpen = 0;
overlayContext.clearRect(
0,
0,
overlayCanvas.width,
overlayCanvas.height
);
for (const cell of baselineCells) {
const occupied = items.some(item => pointInItem(cell.x, cell.z, item));
if (!occupied) currentOpen++;
overlayContext.fillStyle = occupied ? "#df8050" : "#63b69b";
overlayContext.fillRect(
cell.col * 10,
cell.row * 10,
10,
10
);
}
overlayTexture.needsUpdate = true;
const currentArea = currentOpen * cellArea;
const usedArea = baselineArea - currentArea;
const retained = baselineArea ? currentArea / baselineArea * 100 : 0;
document.querySelector("#baseline-area").textContent =
formatArea(baselineArea);
document.querySelector("#current-area").textContent =
formatArea(currentArea);
document.querySelector("#retained-area").textContent =
`${retained.toFixed(1)}%`;
document.querySelector("#used-area").textContent =
formatArea(usedArea);
const exhibitCount = items.filter(item => item.type === "exhibit").length;
const benchCount = items.filter(item => item.type === "bench").length;
document.querySelector("#counts").textContent =
`${exhibitCount} fictional exhibit panel${exhibitCount === 1 ? "" : "s"} · ` +
`${benchCount} seating unit${benchCount === 1 ? "" : "s"}`;
overlay.visible = document.querySelector("#compare").checked;
}
function updateButtons() {
const hasSelection = !!selectedItem();
document.querySelector("#delete").disabled = !hasSelection;
document.querySelector("#rotate").disabled =
!hasSelection && tool === "select";
document.querySelector("#undo").disabled = history.length === 0;
}
function refreshScene() {
updateOpenSpace();
updateSelectionMarker();
updateButtons();
}
function setTool(nextTool) {
tool = nextTool;
document.querySelectorAll("[data-tool]").forEach(button => {
button.classList.toggle("active", button.dataset.tool === tool);
});
if (tool === "select") {
if (preview) preview.visible = false;
statusElement.innerHTML =
"<strong>Select:</strong> drag an item to move it. Right-drag orbits; middle-drag pans.";
} else if (tool === "exhibit") {
createPreview("exhibit");
updatePreviewPosition();
statusElement.innerHTML =
"<strong>Exhibit tool:</strong> click the square to place. Routes and perimeter are protected.";
} else {
createPreview("bench");
updatePreviewPosition();
statusElement.innerHTML =
"<strong>Seating tool:</strong> click the square to place. Press R to rotate.";
}
updateButtons();
}
document.querySelectorAll("[data-tool]").forEach(button => {
button.addEventListener("click", () => setTool(button.dataset.tool));
});
document.querySelector("#compare").addEventListener("change", event => {
overlay.visible = event.target.checked;
});
document.querySelector("#undo").addEventListener("click", () => {
const snapshot = history.pop();
if (snapshot) {
restoreSnapshot(snapshot);
toast("Last change undone.");
}
});
document.querySelector("#clear").addEventListener("click", () => {
if (!items.length) return;
pushHistory();
removeAllItems();
refreshScene();
toast("All fictional objects removed.");
});
function rotateSelection() {
if (tool === "select") {
const item = selectedItem();
if (!item) return;
const nextRotation = item.rotation + Math.PI / 2;
if (!isValidPlacement(
item.x,
item.z,
item.type,
nextRotation,
item.id
)) {
toast("That rotation would enter a protected route.");
return;
}
pushHistory();
item.rotation = nextRotation;
item.group.rotation.y = nextRotation;
refreshScene();
} else {
placementRotation =
(placementRotation + Math.PI / 2) % (Math.PI * 2);
updatePreviewPosition(lastGroundPoint);
}
}
function deleteSelection() {
const item = selectedItem();
if (!item) return;
pushHistory();
itemRoot.remove(item.group);
items = items.filter(candidate => candidate.id !== item.id);
selectedId = null;
refreshScene();
toast("Object deleted.");
}
document.querySelector("#rotate").addEventListener("click", rotateSelection);
document.querySelector("#delete").addEventListener("click", deleteSelection);
// -------------------------------------------------------------------------
// Raycasting and custom camera controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const groundPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.08);
const groundPoint = new THREE.Vector3();
let lastGroundPoint = null;
function updatePointer(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 getGroundPoint() {
const hit = raycaster.ray.intersectPlane(groundPlane, groundPoint);
return hit ? groundPoint.clone() : null;
}
function itemFromObject(object) {
let current = object;
while (current) {
if (current.userData.itemId !== undefined) {
return items.find(item => item.id === current.userData.itemId) || null;
}
current = current.parent;
}
return null;
}
function selectItem(item) {
selectedId = item ? item.id : null;
updateSelectionMarker();
updateButtons();
}
function updateCamera() {
const { target, yaw, pitch, zoom, distance } = cameraState;
const horizontal = Math.cos(pitch) * distance;
camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * distance,
target.z + Math.cos(yaw) * horizontal
);
camera.lookAt(target);
camera.zoom = zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewSize = 78;
camera.left = -viewSize * aspect / 2;
camera.right = viewSize * aspect / 2;
camera.top = viewSize / 2;
camera.bottom = -viewSize / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
updatePointer(event);
pointerDown = {
button: event.button,
x: event.clientX,
y: event.clientY,
moved: false
};
if (event.button === 2) {
navigation = { mode: "orbit" };
return;
}
if (event.button === 1) {
navigation = { mode: "pan" };
event.preventDefault();
return;
}
if (event.button !== 0) return;
if (tool === "select") {
const intersections = raycaster.intersectObjects(
items.map(item => item.group),
true
);
if (intersections.length) {
const item = itemFromObject(intersections[0].object);
if (item) {
selectItem(item);
dragging = {
item,
originX: item.x,
originZ: item.z,
preSnapshot: takeSnapshot(),
moved: false,
valid: true
};
canvas.style.cursor = "grabbing";
return;
}
}
navigation = { mode: "orbit" };
}
});
canvas.addEventListener("pointermove", event => {
updatePointer(event);
if (pointerDown) {
const distance = Math.hypot(
event.clientX - pointerDown.x,
event.clientY - pointerDown.y
);
if (distance > 5) pointerDown.moved = true;
}
if (navigation) {
const dx = event.movementX || 0;
const dy = event.movementY || 0;
if (navigation.mode === "orbit") {
cameraState.yaw -= dx * 0.007;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.35,
1.28
);
} else if (navigation.mode === "pan") {
const scale = 0.075 / cameraState.zoom;
const right = new THREE.Vector3(
Math.cos(cameraState.yaw),
0,
-Math.sin(cameraState.yaw)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.yaw),
0,
-Math.cos(cameraState.yaw)
);
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(forward, dy * scale);
}
updateCamera();
return;
}
const point = getGroundPoint();
lastGroundPoint = point;
if (dragging) {
if (!point) return;
const x = Math.round(point.x * 2) / 2;
const z = Math.round(point.z * 2) / 2;
if (x !== dragging.item.x || z !== dragging.item.z) {
dragging.moved = true;
}
dragging.item.x = x;
dragging.item.z = z;
dragging.item.group.position.x = x;
dragging.item.group.position.z = z;
dragging.valid = isValidPlacement(
x,
z,
dragging.item.type,
dragging.item.rotation,
dragging.item.id
);
updateSelectionMarker(dragging.valid);
return;
}
if (tool !== "select") {
updatePreviewPosition(point);
} else {
const intersections = raycaster.intersectObjects(
items.map(item => item.group),
true
);
canvas.style.cursor = intersections.length ? "pointer" : "default";
}
});
canvas.addEventListener("pointerup", event => {
updatePointer(event);
if (dragging) {
if (dragging.moved) {
if (!dragging.valid) {
dragging.item.x = dragging.originX;
dragging.item.z = dragging.originZ;
dragging.item.group.position.x = dragging.originX;
dragging.item.group.position.z = dragging.originZ;
toast("Move rejected: protected route or occupied space.");
} else {
history.push(dragging.preSnapshot);
if (history.length > 50) history.shift();
}
}
dragging = null;
canvas.style.cursor = "default";
refreshScene();
}
if (
pointerDown &&
pointerDown.button === 0 &&
!pointerDown.moved &&
tool !== "select"
) {
const point = getGroundPoint();
updatePreviewPosition(point);
if (!point) return;
const x = Math.round(point.x * 2) / 2;
const z = Math.round(point.z * 2) / 2;
if (!isValidPlacement(x, z, tool, placementRotation)) {
toast("Placement blocked by the perimeter or a protected entrance route.");
return;
}
pushHistory();
const item = makeItem(tool, x, z, placementRotation, nextId++);
selectItem(item);
refreshScene();
toast(tool === "exhibit" ? "Fictional exhibit panel placed." : "Seating placed.");
}
navigation = null;
pointerDown = null;
updateCamera();
});
canvas.addEventListener("pointercancel", () => {
navigation = null;
pointerDown = null;
dragging = null;
updateSelectionMarker();
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.7
);
updateCamera();
}, { passive: false });
window.addEventListener("keydown", event => {
if (event.target instanceof HTMLInputElement) return;
if (event.key.toLowerCase() === "r") {
rotateSelection();
} else if (event.key === "Delete" || event.key === "Backspace") {
event.preventDefault();
deleteSelection();
} else if (event.key === "1") {
setTool("exhibit");
} else if (event.key === "2") {
setTool("bench");
} else if (event.key === "3") {
setTool("select");
} else if (event.key === "Escape") {
setTool("select");
} else if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "z") {
event.preventDefault();
document.querySelector("#undo").click();
}
});
// -------------------------------------------------------------------------
// Initialize
// -------------------------------------------------------------------------
resize();
updateCamera();
setTool("exhibit");
refreshScene();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```