Prompt specification
Build or customize the Three.js 3D scene "Temporary market layout 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: Temporary market layout planner
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/temporary-market-layout-planner-x0512
Scene Description Prompt:
Arrange low-poly stalls, entrances, and services in a plaza, then dismantle the market to restore public space. Three.js from cdnjs, one HTML file.
Working Three.js Starter (temporary-market-layout-planner-x0512):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Common Ground — Market to Plaza</title>
<style>
:root {
--ink: #173f3a;
--ink-soft: #4f6965;
--paper: #f7f4ec;
--paper-strong: #fffdf8;
--line: rgba(23, 63, 58, 0.14);
--coral: #ed745d;
--coral-dark: #ce5845;
--gold: #e8b84a;
--teal: #4f9b91;
--shadow: 0 18px 50px rgba(30, 54, 48, 0.14);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce8e1;
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;
}
.topbar {
position: fixed;
top: 0;
left: 0;
right: 0;
z-index: 10;
height: 82px;
padding: 18px 24px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
background: linear-gradient(to bottom, rgba(239, 244, 237, 0.92), rgba(239, 244, 237, 0));
}
.brand {
display: flex;
align-items: center;
gap: 13px;
pointer-events: auto;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 4px;
padding: 5px;
border: 1px solid rgba(23, 63, 58, 0.22);
border-radius: 12px;
background: rgba(255, 253, 248, 0.78);
box-shadow: 0 8px 24px rgba(30, 54, 48, 0.08);
transform: rotate(-3deg);
}
.brand-mark i {
display: block;
border-radius: 3px;
background: var(--ink);
}
.brand-mark i:nth-child(2) { background: var(--coral); }
.brand-mark i:nth-child(3) { background: var(--gold); }
.brand-mark i:nth-child(4) { background: var(--teal); }
.brand-title {
margin: 0;
font-size: 17px;
font-weight: 850;
line-height: 1.05;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.brand-subtitle {
margin: 5px 0 0;
color: var(--ink-soft);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.top-actions {
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.status-pill {
min-width: 132px;
padding: 10px 14px;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
border: 1px solid rgba(23, 63, 58, 0.14);
border-radius: 999px;
background: rgba(255, 253, 248, 0.84);
backdrop-filter: blur(14px);
box-shadow: 0 8px 30px rgba(30, 54, 48, 0.08);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #48a273;
box-shadow: 0 0 0 4px rgba(72, 162, 115, 0.13);
}
.status-pill.working .status-dot {
background: var(--gold);
box-shadow: 0 0 0 4px rgba(232, 184, 74, 0.17);
animation: pulse 1.2s infinite;
}
.status-pill.restored .status-dot {
background: var(--teal);
box-shadow: 0 0 0 4px rgba(79, 155, 145, 0.17);
}
@keyframes pulse {
50% { opacity: 0.4; transform: scale(0.8); }
}
.icon-button {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border: 1px solid rgba(23, 63, 58, 0.14);
border-radius: 50%;
background: rgba(255, 253, 248, 0.84);
color: var(--ink);
cursor: pointer;
backdrop-filter: blur(14px);
transition: transform 160ms ease, background 160ms ease;
}
.icon-button:hover {
transform: translateY(-2px);
background: var(--paper-strong);
}
.control-panel {
position: fixed;
z-index: 10;
top: 98px;
left: 24px;
width: 306px;
max-height: calc(100vh - 190px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(23, 63, 58, 0.13);
border-radius: 22px;
background: rgba(255, 253, 248, 0.91);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: rgba(23, 63, 58, 0.2) transparent;
}
.eyebrow {
margin: 0 0 5px;
color: var(--coral-dark);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 26px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.intro {
margin: 10px 0 18px;
color: var(--ink-soft);
font-size: 12px;
line-height: 1.55;
}
.inventory {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-bottom: 20px;
}
.inventory-card {
min-width: 0;
padding: 10px 8px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(247, 244, 236, 0.7);
text-align: left;
color: var(--ink);
cursor: pointer;
transition: transform 150ms ease, border-color 150ms ease, background 150ms ease;
}
.inventory-card:hover,
.inventory-card.active {
transform: translateY(-2px);
border-color: rgba(237, 116, 93, 0.55);
background: #fffaf0;
}
.inventory-icon {
width: 24px;
height: 24px;
margin-bottom: 7px;
display: grid;
place-items: center;
border-radius: 7px;
color: white;
font-size: 13px;
font-weight: 900;
}
.inventory-card:nth-child(1) .inventory-icon { background: var(--coral); }
.inventory-card:nth-child(2) .inventory-icon { background: var(--gold); }
.inventory-card:nth-child(3) .inventory-icon { background: var(--teal); }
.inventory-card strong {
display: block;
font-size: 11px;
white-space: nowrap;
}
.inventory-card small {
display: block;
margin-top: 2px;
color: var(--ink-soft);
font-size: 9px;
}
.section-title {
margin: 18px 0 9px;
display: flex;
align-items: center;
justify-content: space-between;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.section-title span {
color: var(--ink-soft);
font-size: 9px;
font-weight: 700;
letter-spacing: 0;
text-transform: none;
}
.layout-list {
display: grid;
gap: 7px;
}
.layout-button {
padding: 9px 10px;
display: grid;
grid-template-columns: 42px 1fr 18px;
align-items: center;
gap: 10px;
border: 1px solid var(--line);
border-radius: 12px;
background: transparent;
color: var(--ink);
text-align: left;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease;
}
.layout-button:hover {
background: rgba(247, 244, 236, 0.75);
}
.layout-button.active {
border-color: rgba(23, 63, 58, 0.34);
background: #f0eee3;
}
.layout-button strong {
display: block;
font-size: 11px;
}
.layout-button small {
display: block;
margin-top: 2px;
color: var(--ink-soft);
font-size: 9px;
}
.layout-arrow {
color: var(--ink-soft);
font-size: 16px;
}
.mini-plan {
position: relative;
width: 40px;
height: 34px;
border: 1px solid rgba(23, 63, 58, 0.15);
border-radius: 7px;
background: #fffcf5;
overflow: hidden;
}
.mini-plan i {
position: absolute;
display: block;
width: 6px;
height: 5px;
border-radius: 1px;
background: var(--coral);
}
.mini-plan.row i:nth-child(1) { left: 5px; top: 7px; }
.mini-plan.row i:nth-child(2) { left: 17px; top: 7px; }
.mini-plan.row i:nth-child(3) { left: 29px; top: 7px; }
.mini-plan.row i:nth-child(4) { left: 5px; top: 20px; }
.mini-plan.row i:nth-child(5) { left: 17px; top: 20px; }
.mini-plan.row i:nth-child(6) { left: 29px; top: 20px; }
.mini-plan.court i:nth-child(1) { left: 6px; top: 6px; }
.mini-plan.court i:nth-child(2) { left: 17px; top: 4px; }
.mini-plan.court i:nth-child(3) { left: 27px; top: 10px; }
.mini-plan.court i:nth-child(4) { left: 27px; top: 22px; }
.mini-plan.court i:nth-child(5) { left: 13px; top: 25px; }
.mini-plan.court i:nth-child(6) { left: 4px; top: 18px; }
.mini-plan.festival i:nth-child(1) { left: 3px; top: 9px; }
.mini-plan.festival i:nth-child(2) { left: 11px; top: 9px; }
.mini-plan.festival i:nth-child(3) { left: 19px; top: 9px; }
.mini-plan.festival i:nth-child(4) { left: 27px; top: 9px; }
.mini-plan.festival i:nth-child(5) { left: 35px; top: 9px; }
.mini-plan.festival i:nth-child(6) { left: 18px; top: 23px; }
.arrange-toggle {
width: 100%;
margin-top: 15px;
padding: 11px 12px;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
border: 1px solid rgba(23, 63, 58, 0.2);
border-radius: 12px;
background: var(--ink);
color: #fffdf8;
font-size: 11px;
font-weight: 800;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
.arrange-toggle:hover { transform: translateY(-1px); }
.arrange-toggle.active { background: var(--coral-dark); }
.hint {
margin: 10px 0 0;
padding: 9px 10px;
border-radius: 10px;
background: rgba(79, 155, 145, 0.1);
color: var(--ink-soft);
font-size: 10px;
line-height: 1.45;
}
.hint strong { color: var(--ink); }
.primary-action {
width: 100%;
margin-top: 18px;
padding: 14px 15px;
display: flex;
align-items: center;
justify-content: space-between;
border: 0;
border-radius: 14px;
background: var(--coral);
color: #fff;
box-shadow: 0 9px 20px rgba(206, 88, 69, 0.22);
font-size: 12px;
font-weight: 850;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease, opacity 150ms ease;
}
.primary-action:hover {
transform: translateY(-2px);
background: var(--coral-dark);
}
.primary-action:disabled {
opacity: 0.55;
cursor: wait;
transform: none;
}
.primary-action.teal {
background: var(--teal);
box-shadow: 0 9px 20px rgba(79, 155, 145, 0.2);
}
.primary-action.teal:hover { background: #3d8279; }
.stats-panel {
position: fixed;
z-index: 10;
top: 98px;
right: 24px;
width: 212px;
padding: 17px;
border: 1px solid rgba(23, 63, 58, 0.13);
border-radius: 19px;
background: rgba(255, 253, 248, 0.89);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
}
.stat-label {
margin: 0;
color: var(--ink-soft);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.stat-value {
margin-top: 6px;
display: flex;
align-items: baseline;
gap: 4px;
}
.stat-value strong {
font-size: 36px;
line-height: 1;
letter-spacing: -0.05em;
}
.stat-value span {
color: var(--ink-soft);
font-size: 11px;
}
.meter {
height: 7px;
margin: 12px 0 8px;
overflow: hidden;
border-radius: 99px;
background: rgba(23, 63, 58, 0.1);
}
.meter > div {
width: 68%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--coral), var(--gold));
transition: width 120ms linear;
}
.stat-caption {
margin: 0;
color: var(--ink-soft);
font-size: 10px;
line-height: 1.45;
}
.restored-note {
margin-top: 12px;
padding-top: 12px;
display: none;
border-top: 1px solid var(--line);
color: var(--teal);
font-size: 10px;
font-weight: 800;
line-height: 1.4;
}
.restored-note.visible { display: flex; gap: 7px; }
.check {
width: 18px;
height: 18px;
flex: 0 0 auto;
display: grid;
place-items: center;
border-radius: 50%;
background: var(--teal);
color: white;
font-size: 11px;
}
.timeline {
position: fixed;
z-index: 10;
left: 50%;
bottom: 22px;
width: min(650px, calc(100vw - 40px));
min-height: 67px;
padding: 13px 18px;
display: grid;
grid-template-columns: repeat(4, 1fr);
align-items: center;
transform: translateX(-50%);
border: 1px solid rgba(23, 63, 58, 0.13);
border-radius: 18px;
background: rgba(255, 253, 248, 0.91);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.timeline::before {
content: "";
position: absolute;
top: 28px;
left: 11%;
right: 11%;
height: 2px;
background: rgba(23, 63, 58, 0.12);
}
.timeline-fill {
position: absolute;
z-index: 0;
top: 28px;
left: 11%;
width: 38%;
height: 2px;
background: var(--coral);
transition: width 300ms ease, left 300ms ease;
}
.step {
position: relative;
z-index: 1;
display: grid;
justify-items: center;
gap: 5px;
color: var(--ink-soft);
text-align: center;
}
.step-dot {
width: 10px;
height: 10px;
border: 2px solid rgba(23, 63, 58, 0.2);
border-radius: 50%;
background: var(--paper-strong);
}
.step.active,
.step.complete { color: var(--ink); }
.step.active .step-dot { border-color: var(--coral); background: var(--coral); }
.step.complete .step-dot { border-color: var(--teal); background: var(--teal); }
.step small {
font-size: 8px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.step span {
display: none;
font-size: 10px;
font-weight: 700;
}
.selection-label {
position: fixed;
z-index: 8;
left: 50%;
bottom: 100px;
padding: 8px 12px;
border: 1px solid rgba(23, 63, 58, 0.12);
border-radius: 99px;
background: rgba(255, 253, 248, 0.9);
color: var(--ink);
box-shadow: 0 8px 25px rgba(30, 54, 48, 0.1);
font-size: 10px;
font-weight: 750;
transform: translateX(-50%) translateY(10px);
opacity: 0;
pointer-events: none;
transition: opacity 160ms ease, transform 160ms ease;
}
.selection-label.visible {
opacity: 1;
transform: translateX(-50%) translateY(0);
}
.toast {
position: fixed;
z-index: 30;
left: 50%;
top: 94px;
max-width: min(420px, calc(100vw - 40px));
padding: 11px 16px;
border-radius: 12px;
background: var(--ink);
color: white;
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 700;
text-align: center;
opacity: 0;
transform: translate(-50%, -10px);
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 900px) {
.stats-panel { display: none; }
.control-panel { width: 270px; }
.step span { display: block; }
}
@media (max-width: 640px) {
.topbar { height: 70px; padding: 12px 14px; }
.brand-subtitle, .status-pill { display: none; }
.brand-title { font-size: 14px; }
.control-panel {
top: 76px;
left: 12px;
width: 235px;
max-height: calc(100vh - 160px);
padding: 15px;
}
.control-panel h1 { font-size: 21px; }
.intro { display: none; }
.inventory { margin-bottom: 12px; }
.timeline { bottom: 10px; width: calc(100vw - 20px); padding: 10px; }
.step span { display: none; }
.timeline::before, .timeline-fill { top: 23px; }
.selection-label { bottom: 75px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly market plaza"></canvas>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"><i></i><i></i><i></i><i></i></div>
<div>
<p class="brand-title">Common Ground</p>
<p class="brand-subtitle">Market → public space</p>
</div>
</div>
<div class="top-actions">
<div class="status-pill" id="statusPill">
<span class="status-dot"></span>
<span id="statusText">Market open</span>
</div>
<button class="icon-button" id="resetView" title="Reset camera" aria-label="Reset camera">
<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8">
<path d="M4 12a8 8 0 1 0 2.3-5.7L4 8.6"/>
<path d="M4 4v4.6h4.6"/>
</svg>
</button>
</div>
</header>
<aside class="control-panel">
<p class="eyebrow">Plaza programme</p>
<h1>A market with<br>a second life.</h1>
<p class="intro">Arrange the temporary market, then remove it to reveal the permanent public realm underneath.</p>
<div class="inventory" id="inventory">
<button class="inventory-card" data-kind="stall">
<span class="inventory-icon">6</span>
<strong>Stalls</strong>
<small>Canopies</small>
</button>
<button class="inventory-card" data-kind="entrance">
<span class="inventory-icon">2</span>
<strong>Entries</strong>
<small>Gates</small>
</button>
<button class="inventory-card" data-kind="service">
<span class="inventory-icon">3</span>
<strong>Services</strong>
<small>Support</small>
</button>
</div>
<div class="section-title">Plaza plan <span id="layoutDescription">Linear market row</span></div>
<div class="layout-list">
<button class="layout-button active" data-layout="row">
<span class="mini-plan row"><i></i><i></i><i></i><i></i><i></i><i></i></span>
<span><strong>Market row</strong><small>Clear central axis</small></span>
<span class="layout-arrow">›</span>
</button>
<button class="layout-button" data-layout="court">
<span class="mini-plan court"><i></i><i></i><i></i><i></i><i></i><i></i></span>
<span><strong>Courtyard</strong><small>Social market ring</small></span>
<span class="layout-arrow">›</span>
</button>
<button class="layout-button" data-layout="festival">
<span class="mini-plan festival"><i></i><i></i><i></i><i></i><i></i><i></i></span>
<span><strong>Festival edge</strong><small>Open event spine</small></span>
<span class="layout-arrow">›</span>
</button>
</div>
<button class="arrange-toggle" id="arrangeToggle">
<svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.9">
<path d="M12 2v20M2 12h20M8 6l4-4 4 4M8 18l4 4 4-4M6 8l-4 4 4 4M18 8l4 4-4 4"/>
</svg>
<span id="arrangeText">Arrange market pieces</span>
</button>
<p class="hint" id="arrangeHint"><strong>Tip:</strong> choose a plan, then drag individual stalls, entrances, or services around the plaza.</p>
<button class="primary-action" id="dismantleButton">
<span id="dismantleText">Dismantle the market</span>
<span>→</span>
</button>
<button class="primary-action teal" id="rebuildButton" style="display:none">
<span>Rebuild the market</span>
<span>↻</span>
</button>
</aside>
<aside class="stats-panel">
<p class="stat-label">Plaza occupancy</p>
<div class="stat-value"><strong id="occupancyValue">68</strong><span>%</span></div>
<div class="meter"><div id="occupancyMeter"></div></div>
<p class="stat-caption" id="statCaption">Temporary structures currently occupy the central plaza.</p>
<div class="restored-note" id="restoredNote">
<span class="check">✓</span>
<span>The public plaza is fully restored and open for everyday use.</span>
</div>
</aside>
<div class="timeline" aria-label="Transformation progress">
<div class="timeline-fill" id="timelineFill"></div>
<div class="step active" data-step="0"><span class="step-dot"></span><small>01</small><span>Arrange</span></div>
<div class="step" data-step="1"><span class="step-dot"></span><small>02</small><span>Market</span></div>
<div class="step" data-step="2"><span class="step-dot"></span><small>03</small><span>Dismantle</span></div>
<div class="step" data-step="3"><span class="step-dot"></span><small>04</small><span>Open plaza</span></div>
</div>
<div class="selection-label" id="selectionLabel">Selected</div>
<div class="toast" id="toast"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
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(0xdce8e1);
scene.fog = new THREE.Fog(0xdce8e1, 39, 78);
const camera = new THREE.PerspectiveCamera(37, window.innerWidth / window.innerHeight, 0.1, 120);
const cameraState = {
target: new THREE.Vector3(0, 0.6, 0),
theta: 0.72,
phi: 1.02,
radius: 29
};
function updateCamera() {
const s = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * s * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * s * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xf8fff5, 0x789188, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff2d5, 3.1);
sun.position.set(-13, 24, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaddbd2, 1.0);
fill.position.set(16, 10, -15);
scene.add(fill);
const world = new THREE.Group();
const marketRoot = new THREE.Group();
scene.add(world, marketRoot);
const palette = {
ink: 0x173f3a,
coral: 0xed745d,
coralLight: 0xf39b79,
gold: 0xe8b84a,
teal: 0x4f9b91,
cream: 0xfff4d9,
wood: 0xa86f43,
woodDark: 0x704832,
stone: 0xded8c8,
metal: 0x365b55,
white: 0xfffdf7
};
function mat(color, roughness = 0.88, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness,
flatShading: true,
...extra
});
}
const materials = {
wood: mat(palette.wood),
woodDark: mat(palette.woodDark),
metal: mat(palette.metal, 0.7),
cream: mat(palette.cream),
coral: mat(palette.coral),
gold: mat(palette.gold),
teal: mat(palette.teal),
ink: mat(palette.ink),
white: mat(palette.white)
};
function box(parent, w, h, d, x, y, z, material, cast = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(parent, r1, r2, h, x, y, z, material, segments = 6, cast = true) {
const mesh = new THREE.Mesh(new THREE.CylinderGeometry(r1, r2, h, segments), material);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function polySphere(parent, radius, x, y, z, material, detail = 0) {
const mesh = new THREE.Mesh(new THREE.IcosahedronGeometry(radius, detail), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function sign(parent, text, width, height, x, y, z, bg = "#173f3a", fg = "#fffdf7") {
const c = document.createElement("canvas");
c.width = 768;
c.height = 192;
const ctx = c.getContext("2d");
ctx.fillStyle = bg;
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = fg;
ctx.font = "800 62px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, c.width / 2, c.height / 2 + 3);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.MeshBasicMaterial({
map: texture,
side: THREE.DoubleSide,
toneMapped: false
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(width, height), material);
mesh.position.set(x, y, z);
parent.add(mesh);
return mesh;
}
// Plaza and paving.
const distantGround = new THREE.Mesh(
new THREE.PlaneGeometry(140, 140),
mat(0xaec7b2, 1)
);
distantGround.rotation.x = -Math.PI / 2;
distantGround.position.y = -0.39;
distantGround.receiveShadow = true;
world.add(distantGround);
const plazaBase = box(world, 30.4, 0.55, 30.4, 0, -0.23, 0, mat(0xcac4b4), false);
plazaBase.receiveShadow = true;
box(world, 31.1, 0.24, 0.35, 0, -0.01, 15.0, materials.cream, false);
box(world, 31.1, 0.24, 0.35, 0, -0.01, -15.0, materials.cream, false);
box(world, 0.35, 0.24, 30.2, 15.0, -0.01, 0, materials.cream, false);
box(world, 0.35, 0.24, 30.2, -15.0, -0.01, 0, materials.cream, false);
const tileGeometry = new THREE.BoxGeometry(1.39, 0.07, 1.39);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
flatShading: true
});
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, 20 * 20);
const dummy = new THREE.Object3D();
const tileColors = [0xe8e1d2, 0xded7c8, 0xeee7d9, 0xd8d1c2, 0xe5ddcd];
let tileIndex = 0;
for (let x = 0; x < 20; x++) {
for (let z = 0; z < 20; z++) {
dummy.position.set((x - 9.5) * 1.46, 0.075, (z - 9.5) * 1.46);
dummy.rotation.y = (Math.floor(Math.random() * 4) * Math.PI) / 2;
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
tiles.setColorAt(tileIndex, new THREE.Color(tileColors[(x * 7 + z * 3) % tileColors.length]));
tileIndex++;
}
}
tiles.receiveShadow = true;
world.add(tiles);
const centerInlay = cylinder(world, 3.65, 3.65, 0.08, 0, 0.12, 0, mat(0xeee7d9), 48, false);
centerInlay.receiveShadow = true;
const centerRing = new THREE.Mesh(
new THREE.RingGeometry(2.86, 3.02, 48),
new THREE.MeshBasicMaterial({ color: palette.coral, side: THREE.DoubleSide, toneMapped: false })
);
centerRing.rotation.x = -Math.PI / 2;
centerRing.position.y = 0.17;
world.add(centerRing);
// Permanent plaza furniture.
function createTree(x, z, scale = 1) {
const g = new THREE.Group();
g.position.set(x, 0, z);
g.scale.setScalar(scale);
cylinder(g, 0.28, 0.38, 2.5, 0, 1.25, 0, materials.woodDark, 6);
polySphere(g, 1.55, 0, 3.05, 0, mat(0x5d9b72), 0);
polySphere(g, 1.08, -0.9, 2.75, 0.3, mat(0x75aa7c), 0);
polySphere(g, 1.0, 0.88, 2.85, -0.28, mat(0x679f70), 0);
world.add(g);
}
function createBench(x, z, rotation = 0) {
const g = new THREE.Group();
g.position.set(x, 0, z);
g.rotation.y = rotation;
box(g, 2.45, 0.18, 0.72, 0, 0.72, 0, materials.wood);
box(g, 2.45, 0.62, 0.13, 0, 1.14, -0.3, materials.woodDark);
box(g, 0.16, 0.7, 0.62, -0.92, 0.35, 0, materials.metal);
box(g, 0.16, 0.7, 0.62, 0.92, 0.35, 0, materials.metal);
world.add(g);
}
function createLamp(x, z) {
const g = new THREE.Group();
g.position.set(x, 0, z);
cylinder(g, 0.08, 0.13, 4.4, 0, 2.2, 0, materials.metal, 6);
cylinder(g, 0.31, 0.42, 0.18, 0, 0.1, 0, materials.metal, 6);
box(g, 0.62, 0.62, 0.62, 0, 4.35, 0, materials.cream);
box(g, 0.7, 0.12, 0.7, 0, 4.7, 0, materials.metal);
world.add(g);
}
function createPlanter(x, z, color = 0x9f7655) {
const g = new THREE.Group();
g.position.set(x, 0, z);
cylinder(g, 0.85, 0.68, 0.72, 0, 0.36, 0, mat(color), 8);
cylinder(g, 0.75, 0.75, 0.08, 0, 0.74, 0, mat(0x6c7f52), 8);
polySphere(g, 0.72, 0, 1.12, 0, mat(0x6d9f69), 0);
world.add(g);
}
function createBikeRack(x, z, rotation = 0) {
const g = new THREE.Group();
g.position.set(x, 0, z);
g.rotation.y = rotation;
for (let i = 0; i < 3; i++) {
const hoop = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.055, 5, 12, Math.PI),
materials.metal
);
hoop.rotation.y = Math.PI / 2;
hoop.position.set((i - 1) * 0.65, 0.54, 0);
hoop.castShadow = true;
g.add(hoop);
}
world.add(g);
}
[
[-11.7, -10.8, 1.02], [11.5, -10.5, 0.94],
[-11.5, 10.5, 0.97], [11.8, 10.3, 1.04],
[-12.6, 0.2, 0.83], [12.5, 0.5, 0.86]
].forEach(v => createTree(...v));
createBench(-10.6, 7.1, 0);
createBench(10.4, 7.0, 0);
createBench(-10.5, -7.1, 0);
createBench(10.5, -7.0, 0);
createBench(-7.3, 11.1, Math.PI / 2);
createBench(7.3, 11.1, Math.PI / 2);
createLamp(-8.4, 8.2);
createLamp(8.4, 8.2);
createLamp(-8.4, -8.2);
createLamp(8.4, -8.2);
createPlanter(-12.5, 7.3);
createPlanter(12.5, 7.3, 0xb47b55);
createPlanter(-12.5, -7.3, 0x8d7259);
createPlanter(12.5, -7.3);
createBikeRack(-4.9, 11.5);
createBikeRack(4.9, 11.5);
// Small permanent residents make the restored plaza feel active.
function createPerson(x, z, color, rotation = 0) {
const g = new THREE.Group();
g.position.set(x, 0, z);
g.rotation.y = rotation;
const shirt = mat(color);
cylinder(g, 0.22, 0.28, 0.72, 0, 1.0, 0, shirt, 5);
polySphere(g, 0.25, 0, 1.57, 0, mat(0xd9a27f), 0);
cylinder(g, 0.09, 0.09, 0.62, -0.11, 0.31, 0, materials.ink, 5);
cylinder(g, 0.09, 0.09, 0.62, 0.11, 0.31, 0, materials.ink, 5);
box(g, 0.13, 0.5, 0.13, -0.31, 1.0, 0, shirt);
box(g, 0.13, 0.5, 0.13, 0.31, 1.0, 0, shirt);
world.add(g);
}
createPerson(-3.8, 7.6, 0x567f9b, 0.5);
createPerson(3.6, 7.9, 0xd77862, -0.8);
createPerson(-7.4, 2.8, 0xe4b84e, 1.3);
createPerson(7.2, 2.4, 0x5a9a8d, -1.2);
createPerson(0.8, 8.8, 0x8f6fa0, 0.2);
createPerson(-5.7, -8.8, 0x477c72, 2.2);
// Market construction.
const items = [];
const itemByKind = { stall: [], entrance: [], service: [] };
function createRoof(parent, width, depth, y, colors) {
const stripCount = 8;
const stripWidth = width / stripCount;
for (let i = 0; i < stripCount; i++) {
const x = -width / 2 + stripWidth * (i + 0.5);
const roof = box(
parent,
stripWidth + 0.015,
0.085,
depth,
x,
y - Math.abs(x) * 0.28,
0,
mat(colors[i % colors.length])
);
roof.rotation.z = -Math.sign(x) * 0.28;
}
for (let i = 0; i < 8; i++) {
const x = -width / 2 + 0.24 + i * (width - 0.48) / 7;
box(parent, 0.28, 0.28, 0.07, x, y - 0.48, depth / 2 + 0.015, mat(colors[i % colors.length]));
}
}
function createStall(index) {
const g = new THREE.Group();
const canopyColors = [
[palette.coral, palette.cream],
[palette.teal, palette.cream],
[palette.gold, palette.cream],
[0x6689a8, palette.cream]
][index % 4];
box(g, 3.15, 0.045, 2.25, 0, 0.16, 0, mat(canopyColors[0]), false);
box(g, 2.55, 1.05, 1.25, 0, 0.68, -0.05, materials.wood);
box(g, 2.78, 0.12, 1.45, 0, 1.25, -0.02, materials.woodDark);
for (const x of [-1.32, 1.32]) {
for (const z of [-0.76, 0.76]) {
box(g, 0.095, 2.75, 0.095, x, 1.53, z, materials.metal);
}
}
createRoof(g, 3.2, 2.15, 3.02, canopyColors);
box(g, 2.35, 0.65, 0.08, 0, 1.75, -0.76, materials.cream);
sign(g, ["PRODUCE", "BAKERY", "FLOWERS", "PANTRY", "CRAFT", "COFFEE"][index],
1.65, 0.38, 0, 1.78, -0.705, "#173f3a", "#fffdf7");
const goods = [0xe45644, 0xe8b547, 0x6ea764, 0xd8824c, 0x8d6cab];
for (let i = 0; i < 5; i++) {
const geo = i % 2 ? new THREE.IcosahedronGeometry(0.14, 0) : new THREE.BoxGeometry(0.23, 0.18, 0.23);
const product = new THREE.Mesh(geo, mat(goods[(index + i) % goods.length]));
product.position.set(-0.78 + i * 0.39, 1.43, 0.02);
product.castShadow = true;
g.add(product);
}
box(g, 0.72, 0.42, 0.58, 0.9, 0.38, -0.95, materials.woodDark);
box(g, 0.72, 0.42, 0.58, -0.9, 0.38, -0.95, materials.woodDark);
marketRoot.add(g);
return { group: g, kind: "stall", label: ["Produce stall", "Bakery stall", "Flower stall", "Pantry stall", "Craft stall", "Coffee stall"][index] };
}
function createEntrance(index) {
const g = new THREE.Group();
const color = index ? palette.teal : palette.coral;
for (const x of [-1.7, 1.7]) {
box(g, 0.2, 3.35, 0.2, x, 1.68, 0, mat(color));
cylinder(g, 0.35, 0.35, 0.18, x, 0.1, 0, materials.cream, 8);
}
box(g, 3.65, 0.25, 0.25, 0, 3.22, 0, mat(color));
sign(g, index ? "SOUTH ENTRY" : "MARKET ENTRY", 2.65, 0.58, 0, 2.8, 0.14,
index ? "#4f9b91" : "#ed745d", "#fffdf7");
const stringPoints = [];
for (let i = 0; i <= 12; i++) {
const x = -1.5 + i * 0.25;
const y = 3.02 - Math.sin((i / 12) * Math.PI) * 0.24;
stringPoints.push(new THREE.Vector3(x, y, 0.05));
}
const stringCurve = new THREE.CatmullRomCurve3(stringPoints);
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(stringCurve.getPoints(40)),
new THREE.LineBasicMaterial({ color: palette.ink })
);
g.add(line);
for (let i = 0; i < 7; i++) {
const x = -1.25 + i * 0.42;
const y = 2.91 - Math.sin((i / 6) * Math.PI) * 0.23;
const flag = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.3, 3),
mat(i % 2 ? palette.gold : palette.cream)
);
flag.rotation.z = Math.PI;
flag.position.set(x, y, 0.05);
flag.castShadow = true;
g.add(flag);
}
marketRoot.add(g);
return { group: g, kind: "entrance", label: index ? "South entrance" : "Market entrance" };
}
function createCoffeeCart() {
const g = new THREE.Group();
box(g, 2.1, 0.9, 1.25, 0, 0.75, 0, mat(palette.coral));
box(g, 2.3, 0.12, 1.42, 0, 1.25, 0, materials.woodDark);
for (const x of [-0.78, 0.78]) {
const wheel = cylinder(g, 0.34, 0.34, 0.14, x, 0.34, 0, materials.ink, 10);
wheel.rotation.x = Math.PI / 2;
}
for (const x of [-0.92, 0.92]) {
box(g, 0.08, 1.65, 0.08, x, 2.0, -0.38, materials.metal);
}
createRoof(g, 2.25, 1.55, 3.02, [palette.coral, palette.cream]);
box(g, 0.65, 0.48, 0.45, 0.55, 1.55, 0, materials.cream);
cylinder(g, 0.24, 0.18, 0.48, -0.55, 1.55, 0, materials.metal, 8);
sign(g, "COFFEE", 1.0, 0.28, 0, 1.03, 0.64, "#ed745d", "#fffdf7");
marketRoot.add(g);
return { group: g, kind: "service", label: "Coffee cart" };
}
function createInfoDesk() {
const g = new THREE.Group();
cylinder(g, 0.82, 0.92, 1.15, 0, 0.7, 0, materials.teal, 7);
cylinder(g, 1.02, 1.02, 0.14, 0, 1.34, 0, materials.cream, 8);
cylinder(g, 0.06, 0.06, 2.1, 0, 2.25, 0, materials.metal, 6);
const umbrella = new THREE.Mesh(new THREE.ConeGeometry(1.65, 0.62, 8), materials.gold);
umbrella.position.y = 3.25;
umbrella.castShadow = true;
g.add(umbrella);
sign(g, "INFO", 0.92, 0.3, 0, 0.92, 0.92, "#4f9b91", "#fffdf7");
marketRoot.add(g);
return { group: g, kind: "service", label: "Wayfinding desk" };
}
function createWaterStation() {
const g = new THREE.Group();
box(g, 1.8, 0.85, 1.0, 0, 0.55, 0, materials.teal);
box(g, 1.95, 0.12, 1.12, 0, 1.03, 0, materials.cream);
cylinder(g, 0.37, 0.37, 0.95, -0.42, 1.56, 0, materials.metal, 8);
cylinder(g, 0.39, 0.39, 0.08, -0.42, 2.05, 0, materials.cream, 8);
box(g, 0.38, 0.46, 0.38, 0.43, 1.32, 0, materials.gold);
for (let i = 0; i < 4; i++) {
cylinder(g, 0.09, 0.07, 0.22, 0.2 + i * 0.19, 1.2, 0.25, materials.white, 6);
}
sign(g, "WATER", 0.98, 0.27, 0, 0.69, 0.51, "#4f9b91", "#fffdf7");
marketRoot.add(g);
return { group: g, kind: "service", label: "Water refill station" };
}
for (let i = 0; i < 6; i++) items.push(createStall(i));
items.push(createEntrance(0));
items.push(createEntrance(1));
items.push(createCoffeeCart());
items.push(createInfoDesk());
items.push(createWaterStation());
const layoutPositions = {
row: [
[-6.0, -3.7, 0], [-2.0, -3.7, 0], [2.0, -3.7, 0],
[-6.0, -0.5, 0], [-2.0, -0.5, 0], [2.0, -0.5, 0],
[-9.0, 4.8, 0], [9.0, 4.8, 0],
[-4.8, 3.5, 0], [0, 4.8, 0], [4.8, 3.5, 0]
],
court: [
[-4.7, 3.4, 0.75], [-0.8, 4.7, 0], [4.0, 3.4, -0.75],
[5.1, -0.5, -1.55], [2.0, -4.2, -2.35], [-3.8, -3.5, 2.45],
[-8.6, 0.3, 1.57], [8.6, 0.3, -1.57],
[-4.4, -1.5, 0.65], [4.8, -1.2, -0.65], [0.2, 0.2, 0]
],
festival: [
[-7.0, -2.6, 0], [-3.5, -2.6, 0], [0, -2.6, 0],
[3.5, -2.6, 0], [7.0, -2.6, 0], [0.3, 4.2, Math.PI],
[-9.0, 5.3, 0], [9.0, 5.3, 0],
[-6.0, 3.0, 0], [6.0, 3.0, 0], [0, -5.6, 0]
]
};
items.forEach((item, index) => {
item.id = index;
item.group.userData.marketItem = item;
item.kind = item.group.userData.marketItem.kind || item.kind;
itemByKind[item.kind].push(item);
const p = layoutPositions.row[index];
item.group.position.set(p[0], 0, p[1]);
item.group.rotation.y = p[2];
item.targetRotation = p[2];
item.radius = item.kind === "stall" ? 2.0 : item.kind === "entrance" ? 1.9 : 1.4;
item.transition = null;
});
// Invisible ground plane used for dragging.
const dragPlane = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false,
side: THREE.DoubleSide
})
);
dragPlane.rotation.x = -Math.PI / 2;
dragPlane.position.y = 0.12;
scene.add(dragPlane);
const selectionRing = new THREE.Mesh(
new THREE.RingGeometry(0.92, 1.04, 48),
new THREE.MeshBasicMaterial({
color: palette.teal,
transparent: true,
opacity: 0.88,
side: THREE.DoubleSide,
depthWrite: false,
toneMapped: false
})
);
selectionRing.rotation.x = -Math.PI / 2;
selectionRing.position.y = 0.20;
selectionRing.visible = false;
scene.add(selectionRing);
// UI state.
const statusPill = document.getElementById("statusPill");
const statusText = document.getElementById("statusText");
const arrangeToggle = document.getElementById("arrangeToggle");
const arrangeText = document.getElementById("arrangeText");
const arrangeHint = document.getElementById("arrangeHint");
const dismantleButton = document.getElementById("dismantleButton");
const dismantleText = document.getElementById("dismantleText");
const rebuildButton = document.getElementById("rebuildButton");
const occupancyValue = document.getElementById("occupancyValue");
const occupancyMeter = document.getElementById("occupancyMeter");
const statCaption = document.getElementById("statCaption");
const restoredNote = document.getElementById("restoredNote");
const selectionLabel = document.getElementById("selectionLabel");
const toast = document.getElementById("toast");
const timelineFill = document.getElementById("timelineFill");
const steps = [...document.querySelectorAll(".step")];
let phase = "trade";
let arranging = false;
let currentLayout = "row";
let selectedItem = null;
let occupiedPercent = 68;
let globalTransition = null;
let toastTimer = null;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2300);
}
function setOccupancy(value) {
occupiedPercent = Math.max(0, Math.min(100, value));
occupancyValue.textContent = Math.round(occupiedPercent);
occupancyMeter.style.width = occupiedPercent + "%";
}
function updateTimeline(activeStep) {
steps.forEach((step, i) => {
step.classList.toggle("active", i === activeStep);
step.classList.toggle("complete", i < activeStep);
});
const positions = [12, 36, 63, 88];
timelineFill.style.left = "11%";
timelineFill.style.width = (positions[activeStep] - 11) + "%";
}
function setStatus(label, mode = "") {
statusText.textContent = label;
statusPill.className = "status-pill" + (mode ? " " + mode : "");
}
function updatePhaseUI() {
if (phase === "trade") {
if (arranging) {
setStatus("Arranging", "working");
updateTimeline(0);
} else {
setStatus("Market open");
updateTimeline(1);
}
dismantleButton.style.display = "flex";
rebuildButton.style.display = "none";
dismantleButton.disabled = false;
restoredNote.classList.remove("visible");
} else if (phase === "dismantling") {
setStatus("Dismantling", "working");
updateTimeline(2);
dismantleButton.style.display = "flex";
rebuildButton.style.display = "none";
dismantleButton.disabled = true;
dismantleText.textContent = "Dismantling market…";
} else if (phase === "restored") {
setStatus("Public space", "restored");
updateTimeline(3);
dismantleButton.style.display = "none";
rebuildButton.style.display = "flex";
rebuildButton.disabled = false;
restoredNote.classList.add("visible");
statCaption.textContent = "All temporary structures have been removed; permanent civic amenities remain.";
} else if (phase === "rebuilding") {
setStatus("Rebuilding", "working");
updateTimeline(1);
dismantleButton.style.display = "none";
rebuildButton.style.display = "flex";
rebuildButton.disabled = true;
}
}
function selectItem(item) {
selectedItem = item;
selectionRing.visible = !!item && arranging && phase === "trade";
if (item) {
const scale = item.radius * 1.18;
selectionRing.scale.setScalar(scale);
selectionLabel.textContent = item.label + " · drag to place";
selectionLabel.classList.add("visible");
} else {
selectionLabel.classList.remove("visible");
}
document.querySelectorAll(".inventory-card").forEach(card => {
card.classList.toggle("active", !!item && card.dataset.kind === item.kind);
});
}
function setArranging(value) {
if (phase !== "trade") {
showToast("Rebuild the market before arranging it again.");
return;
}
arranging = value;
arrangeToggle.classList.toggle("active", arranging);
arrangeText.textContent = arranging ? "Finish arranging" : "Arrange market pieces";
arrangeHint.innerHTML = arranging
? "<strong>Arrangement mode:</strong> drag a selected piece. Use the three plans to move the whole market."
: "<strong>Tip:</strong> choose a plan, then drag individual stalls, entrances, or services around the plaza.";
if (!arranging) selectItem(null);
updatePhaseUI();
}
arrangeToggle.addEventListener("click", () => setArranging(!arranging));
document.querySelectorAll(".layout-button").forEach(button => {
button.addEventListener("click", () => {
if (phase !== "trade") {
showToast("The restored plaza is clear. Rebuild the market to choose a new plan.");
return;
}
currentLayout = button.dataset.layout;
document.querySelectorAll(".layout-button").forEach(b => b.classList.toggle("active", b === button));
document.getElementById("layoutDescription").textContent = {
row: "Linear market row",
court: "Social market ring",
festival: "Open event spine"
}[currentLayout];
if (!arranging) setArranging(true);
const now = performance.now();
items.forEach((item, index) => {
const target = layoutPositions[currentLayout][index];
item.transition = {
type: "move",
start: now,
duration: 780,
from: item.group.position.clone(),
to: new THREE.Vector3(target[0], 0, target[1]),
fromRotation: item.group.rotation.y,
toRotation: target[2]
};
});
showToast(`${button.querySelector("strong").textContent} plan applied.`);
});
});
document.querySelectorAll(".inventory-card").forEach(card => {
card.addEventListener("click", () => {
if (phase !== "trade") return;
if (!arranging) setArranging(true);
const item = itemByKind[card.dataset.kind][0];
selectItem(item);
showToast(`${card.querySelector("strong").textContent} selected — drag it into place.`);
});
});
function easeInCubic(t) { return t * t * t; }
function easeOutCubic(t) { return 1 - Math.pow(1 - t, 3); }
function easeInOut(t) {
return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function dismantleMarket() {
if (phase !== "trade") return;
arranging = false;
arrangeToggle.classList.remove("active");
arrangeText.textContent = "Arrange market pieces";
selectItem(null);
phase = "dismantling";
updatePhaseUI();
const now = performance.now();
globalTransition = {
type: "dismantle",
start: now,
duration: 1150,
completed: false,
states: items.map((item, index) => {
const from = item.group.position.clone();
const angle = (index / items.length) * Math.PI * 2 + 0.2;
const to = new THREE.Vector3(Math.cos(angle) * 15, -0.65, Math.sin(angle) * 15);
return { item, from, to, fromRotation: item.group.rotation.y, delay: index * 55 };
})
};
showToast("Temporary structures are being dismantled.");
}
function rebuildMarket() {
if (phase !== "restored") return;
phase = "rebuilding";
updatePhaseUI();
const now = performance.now();
globalTransition = {
type: "rebuild",
start: now,
duration: 1250,
completed: false,
states: items.map((item, index) => {
item.group.visible = true;
item.group.scale.setScalar(0.001);
const target = layoutPositions[currentLayout][index];
const to = new THREE.Vector3(target[0], 0, target[1]);
return {
item,
from: item.group.position.clone(),
to,
fromRotation: item.group.rotation.y,
toRotation: target[2],
delay: index * 48
};
})
};
showToast("Rebuilding the market in the selected plan.");
}
dismantleButton.addEventListener("click", dismantleMarket);
rebuildButton.addEventListener("click", rebuildMarket);
document.getElementById("resetView").addEventListener("click", () => {
cameraState.theta = 0.72;
cameraState.phi = 1.02;
cameraState.radius = 29;
cameraState.target.set(0, 0.6, 0);
showToast("Camera reset.");
});
// Custom camera controls and direct object manipulation.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let orbiting = false;
let draggingItem = null;
let dragOffset = new THREE.Vector3();
let lastPointer = { x: 0, y: 0 };
function setPointer(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 marketHit(event) {
setPointer(event);
const hits = raycaster.intersectObjects(marketRoot.children, true);
for (const hit of hits) {
let object = hit.object;
while (object && object !== marketRoot) {
if (object.userData.marketItem) return object.userData.marketItem;
object = object.parent;
}
}
return null;
}
function groundPoint(event) {
setPointer(event);
const hits = raycaster.intersectObject(dragPlane, false);
return hits.length ? hits[0].point : null;
}
canvas.addEventListener("pointerdown", event => {
lastPointer = { x: event.clientX, y: event.clientY };
canvas.setPointerCapture(event.pointerId);
if (phase === "trade" && arranging) {
const item = marketHit(event);
if (item) {
const point = groundPoint(event);
if (point) {
item.transition = null;
draggingItem = item;
dragOffset.copy(item.group.position).sub(point);
selectItem(item);
canvas.style.cursor = "grabbing";
return;
}
} else {
selectItem(null);
}
}
orbiting = true;
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
if (draggingItem) {
const point = groundPoint(event);
if (point) {
point.add(dragOffset);
draggingItem.group.position.set(
THREE.MathUtils.clamp(point.x, -10.5, 10.5),
0,
THREE.MathUtils.clamp(point.z, -10.5, 10.5)
);
}
return;
}
if (orbiting) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi + dy * 0.005, 0.38, 1.38);
lastPointer = { x: event.clientX, y: event.clientY };
return;
}
if (phase === "trade" && arranging) {
const item = marketHit(event);
canvas.style.cursor = item ? "grab" : "default";
} else {
canvas.style.cursor = "grab";
}
});
function endPointer(event) {
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
draggingItem = null;
orbiting = false;
canvas.style.cursor = "grab";
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(cameraState.radius + event.deltaY * 0.018, 16, 42);
}, { passive: false });
window.addEventListener("keydown", event => {
if (event.key === "Escape") selectItem(null);
});
function updateGlobalTransition(now) {
if (!globalTransition) return;
const elapsed = now - globalTransition.start;
let progressSum = 0;
globalTransition.states.forEach(state => {
const raw = THREE.MathUtils.clamp((elapsed - state.delay) / globalTransition.duration, 0, 1);
progressSum += raw;
if (globalTransition.type === "dismantle") {
const e = easeInCubic(raw);
state.item.group.position.lerpVectors(state.from, state.to, e);
state.item.group.rotation.y = state.fromRotation + e * 1.25;
state.item.group.scale.setScalar(Math.max(0.001, 1 - e));
if (raw === 1) state.item.group.visible = false;
} else {
const e = easeOutCubic(raw);
state.item.group.position.lerpVectors(state.from, state.to, e);
state.item.group.rotation.y = THREE.MathUtils.lerp(state.fromRotation, state.toRotation, e);
const s = Math.max(0.001, e);
state.item.group.scale.setScalar(s);
}
});
const average = progressSum / globalTransition.states.length;
if (globalTransition.type === "dismantle") {
setOccupancy(68 * (1 - average));
statCaption.textContent = "Temporary structures are leaving the central plaza.";
} else {
setOccupancy(68 * average);
statCaption.textContent = "Temporary market structures are returning to the plaza.";
}
if (average >= 1) {
if (globalTransition.type === "dismantle") {
phase = "restored";
setOccupancy(0);
updatePhaseUI();
showToast("Public space restored — the plaza is open again.");
} else {
phase = "trade";
items.forEach((item, index) => {
const p = layoutPositions[currentLayout][index];
item.group.position.set(p[0], 0, p[1]);
item.group.rotation.y = p[2];
item.group.scale.setScalar(1);
});
setOccupancy(68);
updatePhaseUI();
showToast("Market rebuilt. The plaza is ready for trade.");
}
globalTransition = null;
}
}
function animate(now) {
requestAnimationFrame(animate);
items.forEach(item => {
const transition = item.transition;
if (!transition || transition.type !== "move") return;
const t = THREE.MathUtils.clamp((now - transition.start) / transition.duration, 0, 1);
const e = easeInOut(t);
item.group.position.lerpVectors(transition.from, transition.to, e);
item.group.rotation.y = THREE.MathUtils.lerp(transition.fromRotation, transition.toRotation, e);
if (t === 1) item.transition = null;
});
updateGlobalTransition(now);
if (selectedItem && selectionRing.visible) {
selectionRing.position.set(selectedItem.group.position.x, 0.2, selectedItem.group.position.z);
const pulse = 1 + Math.sin(now * 0.004) * 0.035;
const base = selectedItem.radius * 1.18;
selectionRing.scale.set(base * pulse, base * pulse, 1);
selectionRing.material.opacity = 0.65 + Math.sin(now * 0.004) * 0.18;
}
updateCamera();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updatePhaseUI();
requestAnimationFrame(animate);
</script>
</body>
</html>
```