Prompt specification
Build or customize the Three.js 3D scene "Apartment Courtyard Shared Uses" 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: Apartment Courtyard Shared Uses
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/apartment-courtyard-shared-uses-x3271
Scene Description Prompt:
Let me rearrange laundry, play equipment, and seating in a voxel apartment courtyard while preserving access paths. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (apartment-courtyard-shared-uses-x3271):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Courtyard Planner</title>
<style>
:root {
--ink: #263735;
--muted: #6d7c79;
--panel: rgba(250, 252, 248, 0.91);
--border: rgba(47, 73, 68, 0.14);
--green: #287b68;
--green-dark: #1f6254;
--orange: #e58a52;
--red: #cf5a52;
--shadow: 0 18px 50px rgba(39, 61, 56, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: #dce6e2;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
color: inherit;
cursor: pointer;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
top: 18px;
left: 20px;
right: 20px;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
pointer-events: none;
}
.brand,
.actions,
.status-pill {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--border);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 270px;
padding: 11px 15px 11px 12px;
border-radius: 18px;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
place-items: center;
border-radius: 12px;
background: var(--green);
color: white;
font-size: 20px;
box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.12);
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1.1;
letter-spacing: -0.01em;
}
.brand p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
}
.actions {
display: flex;
align-items: center;
gap: 7px;
padding: 7px;
border-radius: 16px;
}
.action {
border: 0;
border-radius: 10px;
background: transparent;
padding: 9px 12px;
font-size: 12px;
font-weight: 650;
transition: 0.15s ease;
}
.action:hover {
background: rgba(40, 123, 104, 0.09);
color: var(--green-dark);
}
.action.primary {
background: var(--green);
color: white;
}
.action.primary:hover {
background: var(--green-dark);
}
.panel {
position: fixed;
z-index: 10;
width: 250px;
max-height: calc(100vh - 126px);
overflow: auto;
scrollbar-width: thin;
background: var(--panel);
border: 1px solid var(--border);
box-shadow: var(--shadow);
backdrop-filter: blur(20px);
-webkit-backdrop-filter: blur(20px);
border-radius: 20px;
}
.inventory {
left: 20px;
top: 91px;
bottom: 58px;
padding: 17px;
}
.panel-heading {
margin-bottom: 15px;
}
.panel-heading h2 {
margin: 0;
font-size: 15px;
letter-spacing: -0.01em;
}
.panel-heading p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.category {
margin-top: 16px;
}
.category-title {
display: flex;
align-items: center;
gap: 7px;
margin: 0 0 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.category-dot {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--category-color);
}
.item-list {
display: grid;
gap: 7px;
}
.item-button {
width: 100%;
display: flex;
align-items: center;
gap: 10px;
padding: 9px;
text-align: left;
border: 1px solid transparent;
border-radius: 12px;
background: rgba(255, 255, 255, 0.58);
transition: 0.15s ease;
}
.item-button:hover {
transform: translateY(-1px);
border-color: rgba(40, 123, 104, 0.24);
background: white;
box-shadow: 0 6px 16px rgba(38, 75, 67, 0.08);
}
.item-icon {
width: 34px;
height: 34px;
flex: 0 0 34px;
display: grid;
place-items: center;
border-radius: 9px;
color: white;
background: var(--accent);
font-size: 18px;
font-weight: 800;
box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.12);
}
.item-name {
flex: 1;
min-width: 0;
}
.item-name strong {
display: block;
font-size: 12px;
}
.item-name small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.item-count {
min-width: 20px;
height: 20px;
display: grid;
place-items: center;
border-radius: 7px;
background: rgba(38, 64, 59, 0.08);
color: var(--muted);
font-size: 10px;
font-weight: 800;
}
.path-note {
margin-top: 17px;
padding: 11px;
display: flex;
align-items: flex-start;
gap: 9px;
border-radius: 12px;
background: rgba(64, 149, 132, 0.09);
color: #37695e;
font-size: 10px;
line-height: 1.45;
}
.path-note strong { color: #245d52; }
.inspector {
right: 20px;
top: 91px;
padding: 16px;
}
.inspector[hidden] { display: none; }
.inspector h2 {
margin: 0;
font-size: 16px;
}
.inspector-meta {
margin: 6px 0 13px;
color: var(--muted);
font-size: 11px;
}
.inspector-meta span {
display: inline-block;
margin-right: 7px;
padding: 4px 7px;
border-radius: 7px;
background: rgba(40, 123, 104, 0.08);
color: var(--green-dark);
font-weight: 700;
}
.inspector-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.inspector-actions .danger {
grid-column: 1 / -1;
color: var(--red);
}
.status-pill {
position: fixed;
z-index: 9;
left: 50%;
top: 22px;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 8px;
padding: 9px 13px;
border-radius: 999px;
font-size: 11px;
font-weight: 750;
transition: 0.15s ease;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #48a68d;
box-shadow: 0 0 0 4px rgba(72, 166, 141, 0.13);
}
.status-pill.bad {
color: #a13f39;
background: rgba(255, 244, 240, 0.94);
}
.status-pill.bad .status-dot {
background: var(--red);
box-shadow: 0 0 0 4px rgba(207, 90, 82, 0.14);
}
.legend {
position: fixed;
z-index: 8;
left: 20px;
bottom: 17px;
display: flex;
gap: 12px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(250, 252, 248, 0.82);
backdrop-filter: blur(14px);
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 9px;
height: 9px;
display: inline-block;
border-radius: 3px;
background: #71b7aa;
}
.legend i.selection { background: #71a95d; }
.help {
position: fixed;
z-index: 8;
right: 20px;
bottom: 18px;
color: rgba(38, 55, 53, 0.68);
font-size: 10px;
pointer-events: none;
text-shadow: 0 1px 2px white;
}
.toast {
position: fixed;
z-index: 30;
left: 50%;
bottom: 28px;
transform: translate(-50%, 18px);
max-width: min(420px, calc(100vw - 40px));
padding: 11px 15px;
border-radius: 12px;
background: rgba(31, 49, 46, 0.93);
color: white;
box-shadow: var(--shadow);
font-size: 11px;
opacity: 0;
pointer-events: none;
transition: 0.22s ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.loading {
position: fixed;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
background: #dce6e2;
color: var(--green-dark);
font-size: 13px;
font-weight: 750;
transition: opacity 0.25s ease;
}
.loading.hide {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
.brand p, .help { display: none; }
.brand { min-width: 0; }
.panel { width: 220px; }
.status-pill { top: 82px; }
.inventory { bottom: 18px; }
.legend { display: none; }
}
@media (max-width: 650px) {
.topbar { top: 10px; left: 10px; right: 10px; }
.brand { padding: 9px; }
.brand h1 { display: none; }
.actions { padding: 5px; }
.action { padding: 8px 9px; font-size: 10px; }
.panel {
width: 195px;
max-height: 42vh;
top: auto;
bottom: 10px;
}
.inventory { left: 10px; }
.inspector { right: 10px; bottom: 10px; top: auto; width: 185px; }
.status-pill { top: 67px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel apartment courtyard"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-mark">▦</div>
<div>
<h1>Courtyard Commons</h1>
<p>Voxel layout studio · access paths stay clear</p>
</div>
</div>
<div class="actions">
<button class="action" id="topView">Top view</button>
<button class="action" id="save">Save</button>
<button class="action primary" id="reset">Reset</button>
</div>
</header>
<div class="status-pill" id="status">
<i class="status-dot"></i>
<span>All access paths clear</span>
</div>
<aside class="panel inventory">
<div class="panel-heading">
<h2>Add to courtyard</h2>
<p>Choose an item, then drag it into place. Teal tiles are protected access routes.</p>
</div>
<div id="inventory"></div>
<div class="path-note">
<span>↗</span>
<div><strong>Access network</strong><br>Every apartment door connects to the south gate through a protected one-tile path.</div>
</div>
</aside>
<aside class="panel inspector" id="inspector" hidden>
<h2 id="selectedName">Item</h2>
<div class="inspector-meta" id="selectedMeta"></div>
<div class="inspector-actions">
<button class="action" id="rotate">Rotate</button>
<button class="action" id="duplicate">Duplicate</button>
<button class="action danger" id="remove">Remove item</button>
</div>
</aside>
<div class="legend">
<span><i></i> Protected access path</span>
<span><i class="selection"></i> Valid placement</span>
</div>
<div class="help">Drag items · drag background to orbit · right-drag to pan · wheel to zoom · R to rotate</div>
<div class="toast" id="toast"></div>
<div class="loading" id="loading">Building the courtyard…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const inventoryEl = document.getElementById("inventory");
const inspectorEl = document.getElementById("inspector");
const selectedNameEl = document.getElementById("selectedName");
const selectedMetaEl = document.getElementById("selectedMeta");
const statusEl = document.getElementById("status");
const toastEl = document.getElementById("toast");
const GRID_W = 12;
const GRID_H = 10;
const CELL = 1.15;
const STORAGE_KEY = "voxel-courtyard-layout-v1";
const catalog = [
{
type: "dryRack", label: "Drying rack", category: "Laundry",
symbol: "▥", w: 2, d: 1, color: "#7aa8b3",
description: "Outdoor laundry"
},
{
type: "basket", label: "Laundry basket", category: "Laundry",
symbol: "▦", w: 1, d: 1, color: "#d28b62",
description: "Sorting basket"
},
{
type: "playhouse", label: "Playhouse", category: "Play equipment",
symbol: "⌂", w: 2, d: 2, color: "#dc8c6b",
description: "Small playhouse"
},
{
type: "slide", label: "Play slide", category: "Play equipment",
symbol: "↘", w: 2, d: 2, color: "#d7a84f",
description: "Tower and slide"
},
{
type: "seesaw", label: "Seesaw", category: "Play equipment",
symbol: "◇", w: 3, d: 1, color: "#73a36d",
description: "Balancing board"
},
{
type: "bench", label: "Bench", category: "Seating",
symbol: "▱", w: 2, d: 1, color: "#9b7659",
description: "Two-seat bench"
},
{
type: "armchair", label: "Armchair", category: "Seating",
symbol: "▣", w: 1, d: 1, color: "#608e82",
description: "Soft courtyard chair"
},
{
type: "table", label: "Shaded table", category: "Seating",
symbol: "⊤", w: 2, d: 2, color: "#b98661",
description: "Table with umbrella"
}
];
const categoryColors = {
Laundry: "#5d9eaa",
"Play equipment": "#d5855f",
Seating: "#638c72"
};
const protectedCells = new Set();
const cellKey = (x, z) => `${x},${z}`;
for (let x = 0; x < GRID_W; x++) protectedCells.add(cellKey(x, 4));
for (let z = 0; z <= 4; z++) {
protectedCells.add(cellKey(2, z));
protectedCells.add(cellKey(6, z));
protectedCells.add(cellKey(10, z));
}
for (let z = 4; z < GRID_H; z++) protectedCells.add(cellKey(6, z));
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce6e2);
scene.fog = new THREE.Fog(0xdce6e2, 30, 58);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const camera = new THREE.OrthographicCamera(-10, 10, 8, -8, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.4, 0);
let cameraTheta = 0.72;
let cameraPhi = 0.91;
let cameraRadius = 21;
let topView = false;
scene.add(new THREE.HemisphereLight(0xf5fbf5, 0x85958c, 2.05));
const sun = new THREE.DirectionalLight(0xfff3dc, 3.1);
sun.position.set(-8, 16, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0007;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xb8d9df, 0.8);
fillLight.position.set(10, 8, -10);
scene.add(fillLight);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const groundPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
let items = [];
let nextItemId = 1;
let selectedId = null;
let dragging = null;
let orbitDrag = null;
let panDrag = null;
let toastTimer = null;
const overlayMaterial = new THREE.MeshBasicMaterial({
color: 0x72a85f,
transparent: true,
opacity: 0.25,
depthWrite: false
});
const overlayLineMaterial = new THREE.LineBasicMaterial({
color: 0x477b45,
transparent: true,
opacity: 0.9
});
const selectionOverlay = new THREE.Group();
selectionOverlay.renderOrder = 4;
scene.add(selectionOverlay);
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
...extra
});
}
function addBox(parent, size, position, color, extra = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material(color, extra)
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function worldX(gridX) {
return (gridX - (GRID_W - 1) / 2) * CELL;
}
function worldZ(gridZ) {
return (gridZ - (GRID_H - 1) / 2) * CELL;
}
function itemWorldPosition(item, x = item.x, z = item.z) {
const footprint = getFootprint(item, item.rot);
return new THREE.Vector3(
worldX(x + (footprint.w - 1) / 2),
0,
worldZ(z + (footprint.d - 1) / 2)
);
}
function addLabel(text, x, y, z, width = 1.65) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 72;
const ctx = labelCanvas.getContext("2d");
ctx.fillStyle = "rgba(34, 57, 52, 0.88)";
ctx.fillRect(0, 0, 256, 72);
ctx.strokeStyle = "rgba(255,255,255,0.25)";
ctx.lineWidth = 4;
ctx.strokeRect(2, 2, 252, 68);
ctx.fillStyle = "white";
ctx.font = "700 30px system-ui";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 37);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
}));
sprite.position.set(x, y, z);
sprite.scale.set(width, width * 72 / 256, 1);
scene.add(sprite);
}
function addPlanter(parent, x, z, tree = true) {
addBox(parent, [0.85, 0.42, 0.85], [x, 0.21, z], "#b78161");
addBox(parent, [0.95, 0.12, 0.95], [x, 0.47, z], "#d19b79");
if (tree) {
addBox(parent, [0.18, 1.05, 0.18], [x, 0.96, z], "#806149");
addBox(parent, [0.72, 0.66, 0.72], [x, 1.55, z], "#6d9b70");
addBox(parent, [0.52, 0.48, 0.52], [x + 0.18, 2.02, z - 0.1], "#82ad79");
} else {
addBox(parent, [0.58, 0.34, 0.58], [x, 0.66, z], "#77a36d");
}
}
function addChevron(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.045, z);
group.rotation.y = rotation;
addBox(group, [0.1, 0.025, 0.38], [-0.12, 0, 0], "#4a8e82");
addBox(group, [0.1, 0.025, 0.38], [0.12, 0, 0], "#4a8e82");
scene.add(group);
}
function buildEnvironment() {
const environment = new THREE.Group();
scene.add(environment);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
material(0xcbd8d1)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.16;
ground.receiveShadow = true;
scene.add(ground);
addBox(environment,
[GRID_W * CELL + 0.3, 0.22, GRID_H * CELL + 0.3],
[0, -0.15, 0],
"#aab8ae"
);
const tileGeometry = new THREE.BoxGeometry(CELL * 0.955, 0.12, CELL * 0.955);
const tileMaterial = material(0xffffff);
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, GRID_W * GRID_H);
tiles.receiveShadow = true;
const matrix = new THREE.Matrix4();
let instance = 0;
for (let z = 0; z < GRID_H; z++) {
for (let x = 0; x < GRID_W; x++) {
matrix.makeTranslation(worldX(x), -0.06, worldZ(z));
tiles.setMatrixAt(instance, matrix);
const isPath = protectedCells.has(cellKey(x, z));
const checker = (x + z) % 2 === 0;
const color = new THREE.Color(
isPath ? 0x83bcb0 : (checker ? 0xe5e3d6 : 0xdadfd4)
);
tiles.setColorAt(instance, color);
instance++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
environment.add(tiles);
const lineVertices = [];
const halfW = GRID_W * CELL / 2;
const halfH = GRID_H * CELL / 2;
for (let x = 0; x <= GRID_W; x++) {
const wx = -halfW + x * CELL;
lineVertices.push(wx, 0.015, -halfH, wx, 0.015, halfH);
}
for (let z = 0; z <= GRID_H; z++) {
const wz = -halfH + z * CELL;
lineVertices.push(-halfW, 0.015, wz, halfW, 0.015, wz);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute("position", new THREE.Float32BufferAttribute(lineVertices, 3));
environment.add(new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({ color: 0xaab6aa, transparent: true, opacity: 0.35 })
));
for (const x of [1, 5, 9]) addChevron(worldX(x), worldZ(4), Math.PI / 2);
for (const x of [2, 6, 10]) {
if (x !== 6) addChevron(worldX(x), worldZ(2), 0);
}
for (const z of [6, 8]) addChevron(worldX(6), worldZ(z), 0);
const apartment = new THREE.Group();
scene.add(apartment);
const doorXs = [2, 6, 10];
const facadeZ = -GRID_H * CELL / 2 - 0.82;
doorXs.forEach((gridX, index) => {
const x = worldX(gridX);
addBox(apartment, [3.15, 3.85, 1.65], [x, 1.85, facadeZ - 0.3], "#c8b9a3");
addBox(apartment, [3.35, 0.23, 1.82], [x, 3.9, facadeZ - 0.25], "#d8c9b2");
addBox(apartment, [0.95, 2.15, 0.15], [x, 1.05, facadeZ + 0.56], "#526e68");
addBox(apartment, [0.12, 2.15, 0.2], [x - 0.48, 1.05, facadeZ + 0.65], "#e8dac5");
addBox(apartment, [0.12, 2.15, 0.2], [x + 0.48, 1.05, facadeZ + 0.65], "#e8dac5");
addBox(apartment, [1.1, 0.13, 0.2], [x, 2.15, facadeZ + 0.65], "#e8dac5");
addBox(apartment, [0.08, 0.08, 0.09], [x + 0.27, 1.04, facadeZ + 0.75], "#f4cf83");
[-1.04, 1.04].forEach(offset => {
addBox(apartment, [0.55, 0.78, 0.12], [x + offset, 2.78, facadeZ + 0.56], "#8fb8ba", {
emissive: 0x315c5d,
emissiveIntensity: 0.16
});
addBox(apartment, [0.68, 0.1, 0.18], [x + offset, 2.78, facadeZ + 0.64], "#f0e3cd");
});
addLabel(`APT ${index + 1}`, x, 4.35, facadeZ + 0.75, 1.5);
});
const gateZ = GRID_H * CELL / 2 + 0.16;
const gateX = worldX(6);
addBox(apartment, [0.32, 1.65, 0.32], [gateX - 1.0, 0.82, gateZ], "#8d6c53");
addBox(apartment, [0.32, 1.65, 0.32], [gateX + 1.0, 0.82, gateZ], "#8d6c53");
addBox(apartment, [2.3, 0.28, 0.38], [gateX, 1.58, gateZ], "#8d6c53");
addBox(apartment, [0.12, 1.25, 0.12], [gateX, 0.8, gateZ], "#53766e");
addLabel("GATE", gateX, 2.16, gateZ + 0.1, 1.2);
addBox(apartment, [4.1, 0.62, 0.35], [worldX(0), 0.28, gateZ], "#a68569");
addBox(apartment, [3.4, 0.62, 0.35], [worldX(10), 0.28, gateZ], "#a68569");
const decor = new THREE.Group();
scene.add(decor);
addPlanter(decor, -7.55, -5.9, true);
addPlanter(decor, 7.55, -5.9, true);
addPlanter(decor, -7.65, 5.65, false);
addPlanter(decor, 7.65, 5.65, false);
addBox(decor, [0.45, 1.4, 0.45], [-7.55, 0.7, -5.9], "#a4775a");
addBox(decor, [0.45, 1.4, 0.45], [7.55, 0.7, -5.9], "#a4775a");
}
function buildDryRack() {
const group = new THREE.Group();
const wood = "#9a7655";
const metal = "#6d8c87";
[-0.48, 0.48].forEach(x => {
[-0.32, 0.32].forEach(z => {
addBox(group, [0.08, 1.7, 0.08], [x * CELL, 0.85, z * CELL], metal);
});
});
addBox(group, [1.08 * CELL, 0.06, 0.06], [0, 1.65, -0.32 * CELL], metal);
addBox(group, [1.08 * CELL, 0.06, 0.06], [0, 1.65, 0.32 * CELL], metal);
addBox(group, [1.08 * CELL, 0.06, 0.06], [0, 1.2, -0.32 * CELL], metal);
addBox(group, [1.08 * CELL, 0.06, 0.06], [0, 1.2, 0.32 * CELL], metal);
const clothColors = ["#f1d7a4", "#d77d6d", "#8db6bd", "#e8e4d8"];
for (let i = 0; i < 4; i++) {
const x = (-0.36 + i * 0.24) * CELL;
addBox(group, [0.28 * CELL, 0.62, 0.035], [x, 1.25, 0], clothColors[i]);
addBox(group, [0.035, 0.12, 0.035], [x - 0.06, 1.63, 0], "#7e7265");
addBox(group, [0.035, 0.12, 0.035], [x + 0.06, 1.63, 0], "#7e7265");
}
addBox(group, [0.95 * CELL, 0.08, 0.42 * CELL], [0, 0.08, 0], wood);
return group;
}
function buildBasket() {
const group = new THREE.Group();
addBox(group, [0.78 * CELL, 0.12, 0.72 * CELL], [0, 0.08, 0], "#b97b57");
addBox(group, [0.78 * CELL, 0.5, 0.07], [0, 0.35, -0.34 * CELL], "#d49369");
addBox(group, [0.78 * CELL, 0.5, 0.07], [0, 0.35, 0.34 * CELL], "#d49369");
addBox(group, [0.07, 0.5, 0.72 * CELL], [-0.38 * CELL, 0.35, 0], "#d49369");
addBox(group, [0.07, 0.5, 0.72 * CELL], [0.38 * CELL, 0.35, 0], "#d49369");
addBox(group, [0.48 * CELL, 0.23, 0.42 * CELL], [-0.08, 0.49, 0.02], "#91b5b6");
addBox(group, [0.32 * CELL, 0.18, 0.32 * CELL], [0.15, 0.55, -0.08], "#e5bd73");
return group;
}
function buildPlayhouse() {
const group = new THREE.Group();
addBox(group, [1.55 * CELL, 1.18, 1.42 * CELL], [0, 0.59, 0], "#df9475");
addBox(group, [1.72 * CELL, 0.16, 1.58 * CELL], [0, 1.24, 0], "#e9aa89");
addBox(group, [1.25 * CELL, 0.36, 1.18 * CELL], [0, 1.48, 0], "#c76e58");
addBox(group, [0.78 * CELL, 0.32, 0.88 * CELL], [0, 1.77, 0], "#b95f4e");
addBox(group, [0.38 * CELL, 0.72, 0.06], [-0.18 * CELL, 0.37, 0.73 * CELL], "#694f43");
addBox(group, [0.3 * CELL, 0.3, 0.06], [0.4 * CELL, 0.78, 0.73 * CELL], "#a8d3d0", {
emissive: 0x426b6b,
emissiveIntensity: 0.12
});
addBox(group, [0.06, 0.06, 0.09], [0.3 * CELL, 0.72, 0.77 * CELL], "#f1d27f");
return group;
}
function buildSlide() {
const group = new THREE.Group();
const frame = "#648d8b";
const slideColor = "#e0ad4f";
[-0.63, -0.27].forEach(z => {
addBox(group, [0.1, 1.35, 0.1], [-0.55 * CELL, 0.67, z * CELL], frame);
});
addBox(group, [0.72 * CELL, 0.12, 0.72 * CELL], [-0.55 * CELL, 1.34, 0], "#77a09b");
addBox(group, [0.78 * CELL, 0.08, 0.78 * CELL], [-0.55 * CELL, 1.46, 0], slideColor);
for (let i = 0; i < 5; i++) {
const x = (-0.15 + i * 0.18) * CELL;
const y = 1.13 - i * 0.21;
addBox(group, [0.19 * CELL, 0.11, 0.52 * CELL], [x, y, 0], slideColor);
}
addBox(group, [0.12, 0.75, 0.12], [0.55 * CELL, 0.38, -0.3 * CELL], frame);
addBox(group, [0.12, 0.75, 0.12], [0.55 * CELL, 0.38, 0.3 * CELL], frame);
return group;
}
function buildSeesaw() {
const group = new THREE.Group();
const plank = addBox(group, [2.55 * CELL, 0.16, 0.32 * CELL], [0, 0.65, 0], "#d5a456");
plank.rotation.z = 0.09;
addBox(group, [0.34 * CELL, 0.48, 0.46 * CELL], [0, 0.25, 0], "#6d8f88");
addBox(group, [0.52 * CELL, 0.08, 0.62 * CELL], [0, 0.03, 0], "#a8b6a6");
addBox(group, [0.22 * CELL, 0.22, 0.22 * CELL], [-0.75 * CELL, 0.81, 0], "#d97863");
addBox(group, [0.22 * CELL, 0.22, 0.22 * CELL], [0.75 * CELL, 0.49, 0], "#7fa9a2");
return group;
}
function buildBench() {
const group = new THREE.Group();
addBox(group, [1.66 * CELL, 0.14, 0.48 * CELL], [0, 0.58, 0], "#a77e57");
addBox(group, [1.66 * CELL, 0.12, 0.42 * CELL], [0, 0.8, 0.35 * CELL], "#b68a60");
addBox(group, [1.66 * CELL, 0.1, 0.14 * CELL], [0, 0.7, 0.18 * CELL], "#8f6a4b");
[-0.58, 0.58].forEach(x => {
addBox(group, [0.13, 0.55, 0.42 * CELL], [x * CELL, 0.27, 0], "#6f6f5f");
});
return group;
}
function buildArmchair() {
const group = new THREE.Group();
addBox(group, [0.66 * CELL, 0.2, 0.62 * CELL], [0, 0.45, 0], "#668f82");
addBox(group, [0.66 * CELL, 0.68, 0.18 * CELL], [0, 0.78, 0.25 * CELL], "#5e887b");
addBox(group, [0.16 * CELL, 0.35, 0.6 * CELL], [-0.31 * CELL, 0.62, 0], "#76a094");
addBox(group, [0.16 * CELL, 0.35, 0.6 * CELL], [0.31 * CELL, 0.62, 0], "#76a094");
[-0.22, 0.22].forEach(x => {
addBox(group, [0.1, 0.34, 0.1], [x * CELL, 0.17, 0], "#725b46");
});
return group;
}
function buildTable() {
const group = new THREE.Group();
addBox(group, [1.5 * CELL, 0.14, 1.25 * CELL], [0, 0.93, 0], "#b8895f");
addBox(group, [0.16, 0.88, 0.16], [0, 0.44, 0], "#84684f");
addBox(group, [0.78 * CELL, 0.1, 0.72 * CELL], [0, 0.08, 0], "#84684f");
addBox(group, [0.07, 1.5, 0.07], [0, 1.72, 0], "#6d7770");
addBox(group, [1.6 * CELL, 0.12, 1.42 * CELL], [0, 2.48, 0], "#e0b970");
addBox(group, [1.15 * CELL, 0.16, 1.0 * CELL], [0, 2.61, 0], "#eccb86");
addBox(group, [0.65 * CELL, 0.16, 0.56 * CELL], [0, 2.74, 0], "#f1d99f");
return group;
}
const builders = {
dryRack: buildDryRack,
basket: buildBasket,
playhouse: buildPlayhouse,
slide: buildSlide,
seesaw: buildSeesaw,
bench: buildBench,
armchair: buildArmchair,
table: buildTable
};
function getCatalogEntry(type) {
return catalog.find(entry => entry.type === type);
}
function getFootprint(item, rot = item.rot) {
const entry = getCatalogEntry(item.type);
return rot % 2 === 0
? { w: entry.w, d: entry.d }
: { w: entry.d, d: entry.w };
}
function itemCells(item, x = item.x, z = item.z, rot = item.rot) {
const footprint = getFootprint(item, rot);
const cells = [];
for (let dz = 0; dz < footprint.d; dz++) {
for (let dx = 0; dx < footprint.w; dx++) {
cells.push({ x: x + dx, z: z + dz });
}
}
return cells;
}
function canPlace(x, z, width, depth, ignoreId = null) {
if (x < 0 || z < 0 || x + width > GRID_W || z + depth > GRID_H) {
return { ok: false, reason: "That item extends beyond the courtyard." };
}
for (let dz = 0; dz < depth; dz++) {
for (let dx = 0; dx < width; dx++) {
if (protectedCells.has(cellKey(x + dx, z + dz))) {
return { ok: false, reason: "That placement blocks a protected access path." };
}
}
}
for (const other of items) {
if (other.id === ignoreId) continue;
const footprint = getFootprint(other);
const overlaps =
x < other.x + footprint.w &&
x + width > other.x &&
z < other.z + footprint.d &&
z + depth > other.z;
if (overlaps) {
return { ok: false, reason: "That placement overlaps another item." };
}
}
return { ok: true, reason: "" };
}
function findSpot(type, rot = 0) {
const entry = getCatalogEntry(type);
const footprint = rot % 2 === 0
? { w: entry.w, d: entry.d }
: { w: entry.d, d: entry.w };
for (let z = 0; z <= GRID_H - footprint.d; z++) {
for (let x = 0; x <= GRID_W - footprint.w; x++) {
if (canPlace(x, z, footprint.w, footprint.d).ok) {
return { x, z };
}
}
}
return null;
}
function setItemTransform(item) {
item.group.position.copy(itemWorldPosition(item));
item.group.rotation.y = item.rot * Math.PI / 2;
}
function createItem(type, x, z, rot = 0) {
const entry = getCatalogEntry(type);
if (!entry) return null;
const group = builders[type]();
const item = {
id: nextItemId++,
type,
x,
z,
rot,
group
};
group.userData.itemId = item.id;
group.traverse(child => {
child.userData.itemId = item.id;
});
items.push(item);
scene.add(group);
setItemTransform(item);
return item;
}
function removeItem(item) {
const index = items.findIndex(candidate => candidate.id === item.id);
if (index !== -1) items.splice(index, 1);
scene.remove(item.group);
item.group.traverse(child => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
if (Array.isArray(child.material)) child.material.forEach(mat => mat.dispose());
else child.material.dispose();
}
});
}
function clearOverlay() {
while (selectionOverlay.children.length) {
const child = selectionOverlay.children.pop();
child.geometry?.dispose();
}
}
function updateOverlay(item, x = item.x, z = item.z, rot = item.rot, valid = true) {
clearOverlay();
if (!item) return;
const footprint = getFootprint(item, rot);
overlayMaterial.color.set(valid ? 0x6ca65b : 0xd75d54);
overlayLineMaterial.color.set(valid ? 0x477b45 : 0x9d3e39);
for (let dz = 0; dz < footprint.d; dz++) {
for (let dx = 0; dx < footprint.w; dx++) {
const wx = worldX(x + dx);
const wz = worldZ(z + dz);
const geometry = new THREE.BoxGeometry(CELL * 0.9, 0.035, CELL * 0.9);
const tile = new THREE.Mesh(geometry, overlayMaterial);
tile.position.set(wx, 0.025, wz);
selectionOverlay.add(tile);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
overlayLineMaterial
);
edges.position.copy(tile.position);
selectionOverlay.add(edges);
}
}
}
function selectItem(id) {
selectedId = id;
const item = items.find(candidate => candidate.id === id) || null;
if (!item) {
inspectorEl.hidden = true;
clearOverlay();
return;
}
const entry = getCatalogEntry(item.type);
const footprint = getFootprint(item);
selectedNameEl.textContent = entry.label;
selectedMetaEl.innerHTML = `
<span>${entry.category}</span>
<span>${footprint.w} × ${footprint.d}</span>
<span>${item.rot * 90}°</span>
`;
inspectorEl.hidden = false;
const validity = canPlace(item.x, item.z, footprint.w, footprint.d, item.id);
updateOverlay(item, item.x, item.z, item.rot, validity.ok);
}
function setStatus(good, message) {
statusEl.classList.toggle("bad", !good);
statusEl.querySelector("span").textContent = message;
}
function recomputeStatus() {
let valid = true;
for (const item of items) {
const footprint = getFootprint(item);
if (!canPlace(item.x, item.z, footprint.w, footprint.d, item.id).ok) {
valid = false;
break;
}
}
setStatus(valid, valid ? "All access paths clear" : "An access path is blocked");
}
function showToast(message) {
toastEl.textContent = message;
toastEl.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastEl.classList.remove("show"), 2300);
}
function buildInventory() {
inventoryEl.innerHTML = "";
const categories = ["Laundry", "Play equipment", "Seating"];
for (const category of categories) {
const section = document.createElement("section");
section.className = "category";
section.style.setProperty("--category-color", categoryColors[category]);
const title = document.createElement("h3");
title.className = "category-title";
title.innerHTML = `<i class="category-dot"></i>${category}`;
section.appendChild(title);
const list = document.createElement("div");
list.className = "item-list";
for (const entry of catalog.filter(candidate => candidate.category === category)) {
const button = document.createElement("button");
button.className = "item-button";
button.style.setProperty("--accent", entry.color);
button.innerHTML = `
<span class="item-icon">${entry.symbol}</span>
<span class="item-name">
<strong>${entry.label}</strong>
<small>${entry.w} × ${entry.d} tiles</small>
</span>
<span class="item-count" data-count="${entry.type}">0</span>
`;
button.addEventListener("click", () => addFromInventory(entry.type));
list.appendChild(button);
}
section.appendChild(list);
inventoryEl.appendChild(section);
}
updateInventoryCounts();
}
function updateInventoryCounts() {
for (const entry of catalog) {
const count = items.filter(item => item.type === entry.type).length;
const element = document.querySelector(`[data-count="${entry.type}"]`);
if (element) element.textContent = count;
}
}
function addFromInventory(type) {
const spot = findSpot(type);
if (!spot) {
showToast("There is no legal open space for that item.");
return;
}
const item = createItem(type, spot.x, spot.z);
selectItem(item.id);
updateInventoryCounts();
recomputeStatus();
showToast(`${getCatalogEntry(type).label} added. Drag it to fine-tune the layout.`);
}
function rotateSelected() {
const item = items.find(candidate => candidate.id === selectedId);
if (!item) return;
const nextRot = (item.rot + 1) % 4;
const nextFootprint = getFootprint(item, nextRot);
const sameSpot = canPlace(item.x, item.z, nextFootprint.w, nextFootprint.d, item.id);
if (sameSpot.ok) {
item.rot = nextRot;
} else {
const spot = findSpot(item.type, nextRot);
if (!spot) {
showToast("There is not enough room to rotate this item.");
return;
}
item.rot = nextRot;
item.x = spot.x;
item.z = spot.z;
showToast("Item rotated and moved to the nearest valid space.");
}
setItemTransform(item);
selectItem(item.id);
recomputeStatus();
}
function duplicateSelected() {
const item = items.find(candidate => candidate.id === selectedId);
if (!item) return;
const spot = findSpot(item.type, item.rot);
if (!spot) {
showToast("There is no legal open space for a duplicate.");
return;
}
const duplicate = createItem(item.type, spot.x, spot.z, item.rot);
selectItem(duplicate.id);
updateInventoryCounts();
recomputeStatus();
showToast("Item duplicated.");
}
function removeSelected() {
const item = items.find(candidate => candidate.id === selectedId);
if (!item) return;
removeItem(item);
selectItem(null);
updateInventoryCounts();
recomputeStatus();
showToast("Item removed.");
}
function resetLayout() {
items.slice().forEach(removeItem);
const initial = [
["dryRack", 0, 1, 0],
["basket", 0, 3, 0],
["playhouse", 8, 1, 0],
["table", 8, 2, 0],
["bench", 4, 2, 0],
["slide", 1, 6, 0],
["seesaw", 3, 7, 0],
["bench", 8, 6, 0],
["armchair", 4, 6, 0]
];
initial.forEach(([type, x, z, rot]) => createItem(type, x, z, rot));
selectItem(null);
updateInventoryCounts();
recomputeStatus();
try { localStorage.removeItem(STORAGE_KEY); } catch (error) {}
showToast("Courtyard restored to the starting layout.");
}
function saveLayout() {
const payload = {
version: 1,
items: items.map(item => ({
type: item.type,
x: item.x,
z: item.z,
rot: item.rot
}))
};
try {
localStorage.setItem(STORAGE_KEY, JSON.stringify(payload));
showToast("Layout saved in this browser.");
} catch (error) {
showToast("This browser could not save the layout.");
}
}
function loadLayout() {
let saved = null;
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (raw) saved = JSON.parse(raw);
} catch (error) {}
if (!saved || !Array.isArray(saved.items)) {
resetLayout();
return;
}
const loaded = [];
for (const data of saved.items) {
const entry = getCatalogEntry(data.type);
if (!entry) continue;
const rot = ((Number(data.rot) || 0) % 4 + 4) % 4;
const footprint = rot % 2 === 0
? { w: entry.w, d: entry.d }
: { w: entry.d, d: entry.w };
const x = Number(data.x);
const z = Number(data.z);
if (!canPlace(x, z, footprint.w, footprint.d).ok) continue;
loaded.push(createItem(data.type, x, z, rot));
}
if (!loaded.length) resetLayout();
else {
selectItem(null);
updateInventoryCounts();
recomputeStatus();
}
}
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 pointerToGround() {
return raycaster.ray.intersectPlane(groundPlane, new THREE.Vector3());
}
function pickItem() {
const hits = raycaster.intersectObjects(items.map(item => item.group), true);
for (const hit of hits) {
if (hit.object.userData.itemId) {
return items.find(item => item.id === hit.object.userData.itemId) || null;
}
}
return null;
}
function beginDrag(item, event) {
const groundPoint = pointerToGround();
if (!groundPoint) return;
dragging = {
item,
pointerId: event.pointerId,
startX: event.clientX,
startY: event.clientY,
moved: false,
originalX: item.x,
originalZ: item.z,
originalRot: item.rot,
offsetX: item.group.position.x - groundPoint.x,
offsetZ: item.group.position.z - groundPoint.z
};
canvas.setPointerCapture(event.pointerId);
}
function updateDrag(event) {
const groundPoint = pointerToGround();
if (!groundPoint) return;
if (!dragging.moved) {
const distance = Math.hypot(
event.clientX - dragging.startX,
event.clientY - dragging.startY
);
if (distance < 4) return;
dragging.moved = true;
}
const item = dragging.item;
const footprint = getFootprint(item);
const targetX = groundPoint.x + dragging.offsetX;
const targetZ = groundPoint.z + dragging.offsetZ;
const gridX = targetX / CELL + (GRID_W - 1) / 2;
const gridZ = targetZ / CELL + (GRID_H - 1) / 2;
item.x = THREE.MathUtils.clamp(
Math.round(gridX - (footprint.w - 1) / 2),
0,
GRID_W - footprint.w
);
item.z = THREE.MathUtils.clamp(
Math.round(gridZ - (footprint.d - 1) / 2),
0,
GRID_H - footprint.d
);
setItemTransform(item);
const validity = canPlace(item.x, item.z, footprint.w, footprint.d, item.id);
updateOverlay(item, item.x, item.z, item.rot, validity.ok);
if (validity.ok) {
setStatus(true, "Placement is clear");
} else {
setStatus(false, validity.reason);
}
}
function finishDrag(cancel = false) {
if (!dragging) return;
const drag = dragging;
dragging = null;
if (canvas.hasPointerCapture(drag.pointerId)) {
canvas.releasePointerCapture(drag.pointerId);
}
if (drag.moved) {
const item = drag.item;
const footprint = getFootprint(item);
const validity = canPlace(item.x, item.z, footprint.w, footprint.d, item.id);
if (!validity.ok || cancel) {
item.x = drag.originalX;
item.z = drag.originalZ;
item.rot = drag.originalRot;
setItemTransform(item);
showToast(cancel ? "Move canceled." : validity.reason);
} else {
showToast("Layout updated. Access paths remain clear.");
}
}
selectItem(drag.item.id);
recomputeStatus();
}
canvas.addEventListener("pointerdown", event => {
updatePointer(event);
if (event.button === 2 || event.button === 1) {
panDrag = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY
};
canvas.setPointerCapture(event.pointerId);
return;
}
const item = pickItem();
if (item) {
selectItem(item.id);
beginDrag(item, event);
} else {
orbitDrag = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
theta: cameraTheta,
phi: cameraPhi
};
canvas.setPointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointermove", event => {
updatePointer(event);
if (dragging && dragging.pointerId === event.pointerId) {
updateDrag(event);
return;
}
if (orbitDrag && orbitDrag.pointerId === event.pointerId) {
cameraTheta = orbitDrag.theta - (event.clientX - orbitDrag.x) * 0.006;
cameraPhi = THREE.MathUtils.clamp(
orbitDrag.phi - (event.clientY - orbitDrag.y) * 0.005,
0.08,
1.28
);
topView = false;
updateCamera();
return;
}
if (panDrag && panDrag.pointerId === event.pointerId) {
const dx = event.clientX - panDrag.x;
const dy = event.clientY - panDrag.y;
panDrag.x = event.clientX;
panDrag.y = event.clientY;
const scale = (camera.top - camera.bottom) / canvas.clientHeight / camera.zoom;
const right = new THREE.Vector3(Math.cos(cameraTheta), 0, -Math.sin(cameraTheta));
const forward = new THREE.Vector3(Math.sin(cameraTheta), 0, Math.cos(cameraTheta));
cameraTarget.addScaledVector(right, -dx * scale);
cameraTarget.addScaledVector(forward, -dy * scale);
updateCamera();
return;
}
if (!dragging && !orbitDrag && !panDrag) {
canvas.style.cursor = pickItem() ? "grab" : "default";
}
});
canvas.addEventListener("pointerup", event => {
if (dragging && dragging.pointerId === event.pointerId) {
finishDrag(false);
}
if (orbitDrag && orbitDrag.pointerId === event.pointerId) {
orbitDrag = null;
}
if (panDrag && panDrag.pointerId === event.pointerId) {
panDrag = null;
}
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", event => {
if (dragging && dragging.pointerId === event.pointerId) finishDrag(true);
orbitDrag = null;
panDrag = null;
});
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("wheel", event => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * (event.deltaY > 0 ? 0.92 : 1.08),
0.62,
2.35
);
updateCamera();
}, { passive: false });
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") {
event.preventDefault();
rotateSelected();
} else if (event.key === "Delete" || event.key === "Backspace") {
event.preventDefault();
removeSelected();
} else if (event.key === "Escape") {
if (dragging) finishDrag(true);
else selectItem(null);
}
});
document.getElementById("rotate").addEventListener("click", rotateSelected);
document.getElementById("duplicate").addEventListener("click", duplicateSelected);
document.getElementById("remove").addEventListener("click", removeSelected);
document.getElementById("save").addEventListener("click", saveLayout);
document.getElementById("reset").addEventListener("click", resetLayout);
document.getElementById("topView").addEventListener("click", event => {
topView = !topView;
if (topView) {
cameraTheta = 0;
cameraPhi = 0.08;
camera.zoom = 0.92;
} else {
cameraTheta = 0.72;
cameraPhi = 0.91;
camera.zoom = 1;
}
event.currentTarget.textContent = topView ? "Isometric view" : "Top view";
updateCamera();
});
function updateCamera() {
const aspect = window.innerWidth / Math.max(window.innerHeight, 1);
const verticalSize = Math.max(13.5, 17 / Math.max(aspect, 0.45));
camera.left = -verticalSize * aspect / 2;
camera.right = verticalSize * aspect / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
}
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight, false);
updateCamera();
}
window.addEventListener("resize", resize);
buildEnvironment();
buildInventory();
loadLayout();
resize();
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const pulse = 0.22 + Math.sin(clock.getElapsedTime() * 3.4) * 0.035;
overlayMaterial.opacity = pulse;
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hide");
setTimeout(() => document.getElementById("loading").remove(), 300);
});
</script>
</body>
</html>
```