Prompt specification
Build or customize the Three.js 3D scene "Service-desk layout workshop" 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: Service-desk layout workshop
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/service-desk-layout-workshop-x0420
Scene Description Prompt:
Service-desk layout workshop. Create a configurable reception room with counters, chairs, and doorway; users place shared equipment so each service position has a clear approach and visible communication path. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (service-desk-layout-workshop-x0420):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Reception Flow Lab</title>
<style>
:root {
--ink: #17242d;
--muted: #687780;
--line: #d9e1e4;
--panel: rgba(255, 255, 255, 0.96);
--teal: #087f78;
--teal-dark: #075e59;
--teal-soft: #dff3ef;
--green: #168a65;
--green-soft: #e3f5ed;
--red: #c94f54;
--red-soft: #fbe9e9;
--blue: #3977a8;
--amber: #e7a32d;
--shadow: 0 12px 34px rgba(24, 44, 54, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: #e8eef0;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
width: 100%;
height: 100%;
display: grid;
grid-template-rows: 68px 1fr;
}
header {
position: relative;
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 20px;
background: #ffffff;
border-bottom: 1px solid var(--line);
box-shadow: 0 2px 10px rgba(30, 50, 60, 0.04);
}
.brand {
display: flex;
align-items: center;
min-width: 250px;
gap: 12px;
}
.brand-mark {
width: 38px;
height: 38px;
position: relative;
flex: 0 0 auto;
}
.brand-mark i {
position: absolute;
width: 14px;
height: 14px;
border-radius: 3px;
background: var(--teal);
box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.09);
}
.brand-mark i:nth-child(1) {
left: 2px;
top: 16px;
}
.brand-mark i:nth-child(2) {
left: 17px;
top: 10px;
background: #38a69d;
}
.brand-mark i:nth-child(3) {
left: 17px;
top: 25px;
background: #b6ded8;
}
.brand h1 {
margin: 0;
font-size: 17px;
letter-spacing: -0.02em;
}
.brand p {
margin: 2px 0 0;
color: var(--muted);
font-size: 12px;
}
.header-status {
display: flex;
align-items: center;
gap: 8px;
padding: 7px 11px;
color: var(--teal-dark);
background: var(--teal-soft);
border: 1px solid #c7e8e2;
border-radius: 999px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(8, 127, 120, 0.11);
}
.header-actions {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 7px;
min-width: 360px;
}
.icon-btn,
.soft-btn,
.primary-btn,
.danger-btn {
border: 0;
border-radius: 9px;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
.icon-btn:active,
.soft-btn:active,
.primary-btn:active,
.danger-btn:active {
transform: translateY(1px);
}
.icon-btn {
width: 36px;
height: 36px;
background: #f2f5f6;
border: 1px solid #e1e7e9;
font-weight: 800;
}
.icon-btn:hover:not(:disabled) {
background: #e8eeee;
}
.icon-btn:disabled {
opacity: 0.35;
cursor: default;
}
.soft-btn,
.primary-btn,
.danger-btn {
height: 36px;
padding: 0 13px;
font-size: 12px;
font-weight: 750;
}
.soft-btn {
background: #f2f5f6;
border: 1px solid #e0e6e8;
}
.soft-btn:hover {
background: #e8eeee;
}
.primary-btn {
color: white;
background: var(--teal);
box-shadow: 0 4px 10px rgba(8, 127, 120, 0.18);
}
.primary-btn:hover {
background: var(--teal-dark);
}
.danger-btn {
color: var(--red);
background: var(--red-soft);
}
main {
min-height: 0;
display: grid;
grid-template-columns: 278px minmax(0, 1fr) 310px;
}
aside {
min-height: 0;
overflow-y: auto;
background: var(--panel);
z-index: 5;
scrollbar-width: thin;
scrollbar-color: #c8d2d6 transparent;
}
.left-panel {
border-right: 1px solid var(--line);
}
.right-panel {
border-left: 1px solid var(--line);
}
.panel-section {
padding: 18px;
border-bottom: 1px solid var(--line);
}
.panel-section:last-child {
border-bottom: 0;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.section-heading h2 {
margin: 0;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-heading span {
color: var(--muted);
font-size: 11px;
}
.field-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.field {
display: grid;
gap: 6px;
}
.field.full {
grid-column: 1 / -1;
}
.field label {
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
.number-wrap {
display: flex;
align-items: center;
height: 36px;
overflow: hidden;
background: #f4f7f8;
border: 1px solid #dfe6e8;
border-radius: 9px;
}
.number-wrap input {
width: 100%;
height: 100%;
padding: 0 10px;
color: var(--ink);
background: transparent;
border: 0;
outline: none;
font-weight: 750;
}
.number-wrap span {
padding-right: 10px;
color: #8b979d;
font-size: 11px;
}
.full-btn {
width: 100%;
margin-top: 11px;
}
.tools {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.tool {
min-height: 68px;
display: flex;
align-items: center;
gap: 9px;
padding: 9px;
text-align: left;
background: #f8fafb;
border: 1px solid #dfe6e8;
border-radius: 11px;
cursor: pointer;
transition:
border-color 120ms ease,
background 120ms ease,
transform 120ms ease;
}
.tool:hover {
border-color: #9bcfc9;
background: #f0f8f6;
transform: translateY(-1px);
}
.tool.active {
border-color: var(--teal);
background: var(--teal-soft);
box-shadow: inset 0 0 0 1px rgba(8, 127, 120, 0.12);
}
.tool-icon {
width: 31px;
height: 31px;
display: grid;
place-items: center;
flex: 0 0 auto;
border-radius: 8px;
color: white;
background: var(--teal);
font-size: 16px;
font-weight: 900;
}
.tool[data-tool="chair"] .tool-icon {
background: #d66c52;
}
.tool[data-tool="printer"] .tool-icon {
background: #4b82b4;
}
.tool[data-tool="display"] .tool-icon {
background: #24566c;
}
.tool[data-tool="radio"] .tool-icon {
background: #d99924;
}
.tool[data-tool="doorway"] .tool-icon {
background: #687c87;
}
.tool b {
display: block;
font-size: 11px;
line-height: 1.2;
}
.tool small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
line-height: 1.2;
}
.tool-note {
margin: 12px 0 0;
padding: 10px;
color: #65737a;
background: #f5f8f9;
border-radius: 9px;
font-size: 11px;
line-height: 1.45;
}
.switch-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
font-size: 12px;
font-weight: 700;
}
.switch {
position: relative;
width: 40px;
height: 23px;
flex: 0 0 auto;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: absolute;
inset: 0;
border-radius: 999px;
background: #cbd4d8;
cursor: pointer;
transition: background 150ms ease;
}
.switch-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 17px;
height: 17px;
border-radius: 50%;
background: white;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
transition: transform 150ms ease;
}
.switch input:checked + .switch-track {
background: var(--teal);
}
.switch input:checked + .switch-track::after {
transform: translateX(17px);
}
.inspector-empty {
padding: 13px;
color: var(--muted);
background: #f7f9fa;
border: 1px dashed #cfd9dd;
border-radius: 10px;
font-size: 11px;
line-height: 1.45;
}
.selected-card {
padding: 12px;
background: #f7f9fa;
border: 1px solid #dfe6e8;
border-radius: 10px;
}
.selected-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
}
.selected-title strong {
font-size: 12px;
}
.selected-title small {
color: var(--muted);
font-size: 10px;
}
.selected-meta {
margin: 0 0 11px;
color: var(--muted);
font-size: 10px;
}
.selected-actions {
display: flex;
gap: 7px;
}
.selected-actions button {
flex: 1;
height: 31px;
padding: 0 8px;
font-size: 10px;
}
#viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #f7fafb 0%, #e5ecef 70%);
}
#scene {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.view-tools {
position: absolute;
top: 16px;
right: 16px;
display: flex;
gap: 6px;
padding: 6px;
background: rgba(255, 255, 255, 0.92);
border: 1px solid rgba(208, 219, 223, 0.95);
border-radius: 12px;
box-shadow: var(--shadow);
backdrop-filter: blur(8px);
}
.view-tools .icon-btn {
width: 34px;
height: 34px;
background: #f4f7f8;
}
.mode-chip {
position: absolute;
top: 17px;
left: 17px;
max-width: calc(100% - 180px);
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
background: rgba(255, 255, 255, 0.94);
border: 1px solid rgba(208, 219, 223, 0.95);
border-radius: 11px;
box-shadow: var(--shadow);
font-size: 11px;
font-weight: 750;
backdrop-filter: blur(8px);
}
.mode-chip .chip-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--teal);
}
.mode-chip span:last-child {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.legend {
position: absolute;
right: 16px;
bottom: 16px;
display: grid;
gap: 7px;
padding: 11px 13px;
background: rgba(255, 255, 255, 0.93);
border: 1px solid rgba(208, 219, 223, 0.95);
border-radius: 11px;
box-shadow: var(--shadow);
font-size: 10px;
backdrop-filter: blur(8px);
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
color: #5e6c73;
}
.legend-swatch {
width: 10px;
height: 10px;
border-radius: 2px;
background: var(--green);
}
.legend-swatch.path {
background: #27a7ca;
}
.legend-swatch.blocked {
background: var(--red);
}
.legend-swatch.door {
background: var(--blue);
}
.bottom-hint {
position: absolute;
left: 16px;
bottom: 16px;
max-width: min(530px, calc(100% - 180px));
padding: 10px 13px;
color: #59686f;
background: rgba(255, 255, 255, 0.92);
border: 1px solid rgba(208, 219, 223, 0.95);
border-radius: 11px;
box-shadow: var(--shadow);
font-size: 10px;
line-height: 1.45;
backdrop-filter: blur(8px);
}
kbd {
display: inline-grid;
min-width: 19px;
height: 19px;
place-items: center;
margin: 0 2px;
padding: 0 5px;
color: #536269;
background: #edf2f3;
border: 1px solid #d5dee1;
border-bottom-width: 2px;
border-radius: 5px;
font: 700 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.score-card {
padding: 14px;
background: linear-gradient(145deg, #f1faf7, #f8fbfb);
border: 1px solid #cfe7df;
border-radius: 13px;
}
.score-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.score-head strong {
font-size: 13px;
}
.score-badge {
padding: 5px 8px;
color: var(--green);
background: var(--green-soft);
border-radius: 999px;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.score-badge.warning {
color: var(--red);
background: var(--red-soft);
}
.score-copy {
margin: 8px 0 12px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.metric-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
}
.metric {
padding: 8px 6px;
text-align: center;
background: rgba(255, 255, 255, 0.82);
border: 1px solid #dce9e4;
border-radius: 9px;
}
.metric b {
display: block;
color: var(--teal-dark);
font-size: 16px;
}
.metric span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
line-height: 1.15;
text-transform: uppercase;
letter-spacing: 0.04em;
}
.station-list {
display: grid;
gap: 8px;
}
.station {
padding: 10px;
background: #fafcfc;
border: 1px solid #dfe6e8;
border-radius: 10px;
cursor: pointer;
transition:
border-color 120ms ease,
transform 120ms ease;
}
.station:hover {
border-color: #9accc5;
transform: translateX(2px);
}
.station-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 8px;
}
.station-top strong {
font-size: 11px;
}
.station-top span {
color: var(--muted);
font-size: 9px;
}
.checks {
display: flex;
flex-wrap: wrap;
gap: 5px;
}
.check {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 4px 6px;
border-radius: 6px;
font-size: 8px;
font-weight: 800;
}
.check.ok {
color: var(--green);
background: var(--green-soft);
}
.check.bad {
color: var(--red);
background: var(--red-soft);
}
.gear-list {
display: grid;
gap: 7px;
}
.gear-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 8px 9px;
background: #f7f9fa;
border-radius: 8px;
font-size: 10px;
}
.gear-name {
display: flex;
align-items: center;
gap: 8px;
color: #4c5b62;
}
.gear-dot {
width: 9px;
height: 9px;
border-radius: 3px;
background: var(--blue);
}
.gear-dot.display {
background: #24566c;
}
.gear-dot.radio {
background: var(--amber);
}
.gear-count {
font-weight: 850;
}
#toast {
position: fixed;
left: 50%;
bottom: 24px;
z-index: 100;
max-width: 380px;
padding: 11px 16px;
color: white;
background: rgba(22, 38, 46, 0.94);
border-radius: 10px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
font-size: 11px;
font-weight: 700;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 1150px) {
main {
grid-template-columns: 250px minmax(0, 1fr) 280px;
}
.header-status {
display: none;
}
.panel-section {
padding: 15px;
}
}
@media (max-width: 920px) {
main {
grid-template-columns: 235px minmax(0, 1fr);
}
.right-panel {
display: none;
}
.header-actions {
min-width: 0;
}
.brand p {
display: none;
}
}
@media (max-width: 680px) {
#app {
grid-template-rows: 58px 1fr;
}
main {
grid-template-columns: 1fr;
}
.left-panel {
display: none;
}
header {
padding: 0 10px;
}
.brand {
min-width: 0;
}
.brand h1 {
font-size: 14px;
}
.header-actions .soft-btn {
display: none;
}
.bottom-hint {
display: none;
}
.legend {
bottom: 12px;
right: 12px;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"><i></i><i></i><i></i></div>
<div>
<h1>Reception Flow Lab</h1>
<p>Service-desk layout workshop</p>
</div>
</div>
<div class="header-status">
<span class="status-dot"></span>
Live workshop
</div>
<div class="header-actions">
<button class="icon-btn" id="undoBtn" title="Undo (Ctrl+Z)">↶</button>
<button class="icon-btn" id="redoBtn" title="Redo (Ctrl+Shift+Z)">↷</button>
<button class="soft-btn" id="saveBtn">Save</button>
<button class="soft-btn" id="resetBtn">Reset</button>
<button class="primary-btn" id="exportBtn">Export JSON</button>
</div>
</header>
<main>
<aside class="left-panel">
<section class="panel-section">
<div class="section-heading">
<h2>Room configuration</h2>
<span>Grid units</span>
</div>
<div class="field-row">
<div class="field">
<label for="roomWidth">Width</label>
<div class="number-wrap">
<input id="roomWidth" type="number" min="12" max="24" value="16" />
<span>cells</span>
</div>
</div>
<div class="field">
<label for="roomDepth">Depth</label>
<div class="number-wrap">
<input id="roomDepth" type="number" min="8" max="18" value="10" />
<span>cells</span>
</div>
</div>
<div class="field">
<label for="stationCount">Starter stations</label>
<div class="number-wrap">
<input id="stationCount" type="number" min="1" max="6" value="3" />
<span>positions</span>
</div>
</div>
<div class="field">
<label>Room state</label>
<div class="number-wrap">
<input value="Editable" readonly />
<span>live</span>
</div>
</div>
</div>
<button class="primary-btn full-btn" id="applyRoomBtn">Apply room size</button>
<button class="soft-btn full-btn" id="starterBtn">Load starter layout</button>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Place an object</h2>
<span>Click floor</span>
</div>
<div class="tools">
<button class="tool" data-tool="counter">
<span class="tool-icon">▦</span>
<span><b>Counter</b><small>Service position</small></span>
</button>
<button class="tool" data-tool="chair">
<span class="tool-icon">♟</span>
<span><b>Chair</b><small>Staff seating</small></span>
</button>
<button class="tool" data-tool="printer">
<span class="tool-icon">P</span>
<span><b>Printer</b><small>Shared equipment</small></span>
</button>
<button class="tool" data-tool="display">
<span class="tool-icon">D</span>
<span><b>Display</b><small>Communication anchor</small></span>
</button>
<button class="tool" data-tool="radio">
<span class="tool-icon">R</span>
<span><b>Radio dock</b><small>Communication anchor</small></span>
</button>
<button class="tool" data-tool="doorway">
<span class="tool-icon">↔</span>
<span><b>Doorway</b><small>Snaps to wall</small></span>
</button>
</div>
<p class="tool-note">
Select a tool, then click the room. Drag placed objects to move them.
Display and radio docks create visible communication paths.
</p>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Workshop checks</h2>
<span>Automatic</span>
</div>
<button class="primary-btn full-btn" id="autoGearBtn">
Auto-place missing shared gear
</button>
<div class="switch-row">
<span>Show overlays</span>
<label class="switch">
<input id="overlayToggle" type="checkbox" checked />
<span class="switch-track"></span>
</label>
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Selection</h2>
<span id="selectionHint">Nothing selected</span>
</div>
<div id="inspectorEmpty" class="inspector-empty">
Click an object in the room to inspect, rotate, or delete it.
</div>
<div id="inspectorCard" class="selected-card" hidden>
<div class="selected-title">
<strong id="selectedName">Counter</strong>
<small id="selectedType">service station</small>
</div>
<p class="selected-meta" id="selectedMeta"></p>
<div class="selected-actions">
<button class="soft-btn" id="rotateBtn">Rotate</button>
<button class="danger-btn" id="deleteBtn">Delete</button>
</div>
</div>
</section>
</aside>
<section id="viewport">
<canvas id="scene" aria-label="Interactive 3D reception room"></canvas>
<div class="mode-chip">
<span class="chip-dot"></span>
<span id="modeText">Select and drag objects</span>
</div>
<div class="view-tools">
<button class="icon-btn" id="fitBtn" title="Fit room">⌂</button>
<button class="icon-btn" id="topBtn" title="Top view">▦</button>
<button class="icon-btn" id="zoomInBtn" title="Zoom in">+</button>
<button class="icon-btn" id="zoomOutBtn" title="Zoom out">−</button>
</div>
<div class="legend">
<div class="legend-row">
<span class="legend-swatch"></span>
Clear service approach
</div>
<div class="legend-row">
<span class="legend-swatch path"></span>
Visible communication path
</div>
<div class="legend-row">
<span class="legend-swatch blocked"></span>
Blocked path / approach
</div>
<div class="legend-row">
<span class="legend-swatch door"></span>
Door approach
</div>
</div>
<div class="bottom-hint">
<kbd>Drag</kbd> orbit ·
<kbd>Shift</kbd> + drag pan ·
<kbd>Wheel</kbd> zoom ·
<kbd>R</kbd> rotate ·
<kbd>Delete</kbd> remove ·
<kbd>Esc</kbd> cancel tool
</div>
</section>
<aside class="right-panel">
<section class="panel-section">
<div class="score-card">
<div class="score-head">
<strong>Layout readiness</strong>
<span class="score-badge" id="scoreBadge">Checking</span>
</div>
<p class="score-copy" id="scoreCopy">
Each counter needs a clear customer approach, a staff chair,
and an unobstructed view to a display or radio dock.
</p>
<div class="metric-grid">
<div class="metric">
<b id="approachMetric">0/0</b>
<span>Approaches</span>
</div>
<div class="metric">
<b id="chairMetric">0/0</b>
<span>Staff chairs</span>
</div>
<div class="metric">
<b id="commMetric">0/0</b>
<span>Visible links</span>
</div>
</div>
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Service positions</h2>
<span id="stationSummary">0 stations</span>
</div>
<div class="station-list" id="stationList"></div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Shared equipment</h2>
<span>Placed</span>
</div>
<div class="gear-list">
<div class="gear-row">
<span class="gear-name"><i class="gear-dot"></i>Printer</span>
<span class="gear-count" id="printerCount">0</span>
</div>
<div class="gear-row">
<span class="gear-name"><i class="gear-dot display"></i>Display</span>
<span class="gear-count" id="displayCount">0</span>
</div>
<div class="gear-row">
<span class="gear-name"><i class="gear-dot radio"></i>Radio dock</span>
<span class="gear-count" id="radioCount">0</span>
</div>
</div>
</section>
<section class="panel-section">
<div class="section-heading">
<h2>Workshop prompt</h2>
<span>Try this</span>
</div>
<p class="tool-note" style="margin-top: 0">
Move the printer away from a service approach. Then place a display
so every staff chair has an unobstructed sightline to it.
</p>
</section>
</aside>
</main>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const $$ = (selector) => [...document.querySelectorAll(selector)];
const viewport = $("#viewport");
const canvas = $("#scene");
const state = {
room: { width: 16, depth: 10 },
items: [],
selectedId: null,
tool: null,
showOverlays: true
};
const history = [];
const redoStack = [];
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8eef0);
scene.fog = new THREE.Fog(0xe8eef0, 35, 70);
const camera = new THREE.PerspectiveCamera(
42,
viewport.clientWidth / viewport.clientHeight,
0.1,
150
);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(viewport.clientWidth, viewport.clientHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const roomGroup = new THREE.Group();
const itemsGroup = new THREE.Group();
const overlayGroup = new THREE.Group();
scene.add(roomGroup, itemsGroup, overlayGroup);
const hemi = new THREE.HemisphereLight(0xf8fdff, 0x8b9b9d, 2.2);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff8e9, 3.2);
sun.position.set(-10, 18, 11);
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 = 55;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xcfe8ff, 0.9);
fill.position.set(13, 9, -12);
scene.add(fill);
const cameraState = {
target: new THREE.Vector3(0, 0, 0),
radius: 28,
phi: 0.79,
theta: 0.72
};
const geometryCache = new Map();
const materialCache = new Map();
const overlayMaterialCache = new Map();
const raycaster = new THREE.Raycaster();
const pointerNdc = new THREE.Vector2();
const groundPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -0.08);
const typeNames = {
counter: "Counter",
chair: "Staff chair",
printer: "Shared printer",
display: "Display",
radio: "Radio dock",
doorway: "Doorway"
};
const toolNames = {
counter: "Counter",
chair: "Staff chair",
printer: "Shared printer",
display: "Display",
radio: "Radio dock",
doorway: "Doorway"
};
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function getGeometry(w, h, d) {
const key = `${w}|${h}|${d}`;
if (!geometryCache.has(key)) {
geometryCache.set(key, new THREE.BoxGeometry(w, h, d));
}
return geometryCache.get(key);
}
function getMaterial(color, opacity = 1, emissive = null) {
const key = `${color}|${opacity}|${emissive || ""}`;
if (!materialCache.has(key)) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.04,
transparent: opacity < 1,
opacity
});
if (emissive) {
material.emissive.set(emissive);
material.emissiveIntensity = 0.35;
}
materialCache.set(key, material);
}
return materialCache.get(key);
}
function getOverlayMaterial(color, opacity = 0.8) {
const key = `${color}|${opacity}`;
if (!overlayMaterialCache.has(key)) {
overlayMaterialCache.set(
key,
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false,
toneMapped: false
})
);
}
return overlayMaterialCache.get(key);
}
function block(parent, w, h, d, x, y, z, color, options = {}) {
const mesh = new THREE.Mesh(
getGeometry(w, h, d),
options.overlay
? getOverlayMaterial(color, options.opacity ?? 0.8)
: getMaterial(color, options.opacity ?? 1, options.emissive)
);
mesh.position.set(x, y, z);
mesh.castShadow = !options.overlay;
mesh.receiveShadow = !options.overlay;
parent.add(mesh);
return mesh;
}
function clearGroup(group) {
while (group.children.length) {
group.remove(group.children[group.children.length - 1]);
}
}
function gridToWorld(x, z) {
return {
x: x - (state.room.width - 1) / 2,
z: z - (state.room.depth - 1) / 2
};
}
function worldToGrid(x, z) {
return {
x: Math.round(x + (state.room.width - 1) / 2),
z: Math.round(z + (state.room.depth - 1) / 2)
};
}
function rotateVector(dx, dz, rotation) {
switch (((rotation % 4) + 4) % 4) {
case 1:
return { x: dz, z: -dx };
case 2:
return { x: -dx, z: -dz };
case 3:
return { x: -dz, z: dx };
default:
return { x: dx, z: dz };
}
}
function inBounds(x, z) {
return (
x >= 0 &&
z >= 0 &&
x < state.room.width &&
z < state.room.depth
);
}
function itemKey(x, z) {
return `${x},${z}`;
}
function getItem(id) {
return state.items.find((item) => item.id === id);
}
function getOccupancy(excludedId = null) {
const map = new Map();
for (const item of state.items) {
if (item.type === "doorway" || item.id === excludedId) continue;
map.set(itemKey(item.x, item.z), item);
}
return map;
}
function canPlace(item, x, z, occupancy = getOccupancy(item.id)) {
if (!inBounds(x, z)) return false;
return !occupancy.has(itemKey(x, z));
}
function doorWorld(item) {
const { width, depth } = state.room;
const along = gridToWorld(item.index, item.index);
if (item.side === "N") {
return { x: along.x + 0.5, z: -depth / 2 };
}
if (item.side === "S") {
return { x: along.x + 0.5, z: depth / 2 };
}
if (item.side === "W") {
return { x: -width / 2, z: along.z + 0.5 };
}
return { x: width / 2, z: along.z + 0.5 };
}
function updateDoorCoordinates(item) {
if (item.side === "N" || item.side === "S") {
item.x = item.index;
item.z = 0;
} else {
item.x = 0;
item.z = item.index;
}
}
function doorApproachCell(item) {
if (item.side === "N") return { x: item.index, z: 0 };
if (item.side === "S") return { x: item.index, z: state.room.depth - 1 };
if (item.side === "W") return { x: 0, z: item.index };
return { x: state.room.width - 1, z: item.index };
}
function nearestDoorEdge(point) {
const { width, depth } = state.room;
const options = [
{
side: "N",
distance: Math.abs(point.z + depth / 2),
index: clamp(Math.round(point.x + width / 2 - 0.5), 0, width - 1)
},
{
side: "S",
distance: Math.abs(point.z - depth / 2),
index: clamp(Math.round(point.x + width / 2 - 0.5), 0, width - 1)
},
{
side: "W",
distance: Math.abs(point.x + width / 2),
index: clamp(Math.round(point.z + depth / 2 - 0.5), 0, depth - 1)
},
{
side: "E",
distance: Math.abs(point.x - width / 2),
index: clamp(Math.round(point.z + depth / 2 - 0.5), 0, depth - 1)
}
];
options.sort((a, b) => a.distance - b.distance);
return options[0];
}
function buildRoom() {
clearGroup(roomGroup);
const { width, depth } = state.room;
block(
roomGroup,
width + 0.8,
0.36,
depth + 0.8,
0,
-0.25,
0,
"#b8c8cc"
);
for (let x = 0; x < width; x++) {
for (let z = 0; z < depth; z++) {
const world = gridToWorld(x, z);
const isAccent = (x + z) % 7 === 0;
const color = isAccent ? "#dce8e8" : "#f3f5f3";
block(
roomGroup,
0.94,
0.12,
0.94,
world.x,
-0.02,
world.z,
color
);
}
}
const doors = state.items.filter((item) => item.type === "doorway");
const hasDoor = (side, index) =>
doors.some((door) => door.side === side && door.index === index);
for (let i = 0; i < width; i++) {
const x = -width / 2 + i + 0.5;
if (!hasDoor("N", i)) {
block(roomGroup, 0.9, 0.9, 0.9, x, 0.45, -depth / 2, "#e7ecec");
block(roomGroup, 0.9, 0.9, 0.9, x, 1.35, -depth / 2, "#d5dfe0");
}
if (!hasDoor("S", i)) {
block(roomGroup, 0.9, 0.9, 0.9, x, 0.45, depth / 2, "#e7ecec");
block(roomGroup, 0.9, 0.9, 0.9, x, 1.35, depth / 2, "#d5dfe0");
}
}
for (let i = 0; i < depth; i++) {
const z = -depth / 2 + i + 0.5;
if (!hasDoor("W", i)) {
block(roomGroup, 0.9, 0.9, 0.9, -width / 2, 0.45, z, "#e7ecec");
block(roomGroup, 0.9, 0.9, 0.9, -width / 2, 1.35, z, "#d5dfe0");
}
if (!hasDoor("E", i)) {
block(roomGroup, 0.9, 0.9, 0.9, width / 2, 0.45, z, "#e7ecec");
block(roomGroup, 0.9, 0.9, 0.9, width / 2, 1.35, z, "#d5dfe0");
}
}
}
function buildCounter(group) {
const teal = "#188b82";
const tealDark = "#126d67";
const mint = "#55b8ac";
const white = "#f8faf8";
const screen = "#193d46";
for (const x of [-0.48, 0, 0.48]) {
block(group, 0.46, 0.62, 0.72, x, 0.35, 0, teal);
block(group, 0.48, 0.15, 0.82, x, 0.735, 0, white);
}
block(group, 1.43, 0.1, 0.05, 0, 0.48, 0.39, mint);
block(group, 0.12, 0.31, 0.12, 0.27, 0.95, -0.14, tealDark);
block(group, 0.3, 0.23, 0.07, 0.27, 1.1, -0.14, screen);
block(group, 0.21, 0.14, 0.025, 0.27, 1.12, -0.095, "#70c8ef", {
emissive: "#1d87ad"
});
block(group, 0.28, 0.06, 0.25, -0.28, 0.85, 0.06, "#f0c05b");
}
function buildChair(group) {
const coral = "#d66d55";
const coralDark = "#ad4f3c";
const dark = "#4f5e63";
block(group, 0.58, 0.14, 0.58, 0, 0.43, 0, coral);
block(group, 0.58, 0.53, 0.14, 0, 0.73, -0.24, coralDark);
for (const x of [-0.21, 0.21]) {
for (const z of [-0.21, 0.21]) {
block(group, 0.1, 0.35, 0.1, x, 0.2, z, dark);
}
}
}
function buildPrinter(group) {
const blue = "#4c83b3";
const blueDark = "#2e5e85";
const white = "#f7f8f4";
const dark = "#394a53";
block(group, 0.76, 0.55, 0.66, 0, 0.32, 0, blue);
block(group, 0.58, 0.15, 0.48, 0, 0.67, 0, blueDark);
block(group, 0.4, 0.04, 0.34, 0, 0.79, 0.05, white);
block(group, 0.35, 0.2, 0.05, 0, 0.44, 0.355, dark);
block(group, 0.07, 0.07, 0.03, 0.21, 0.6, 0.355, "#6de0b1", {
emissive: "#248b66"
});
}
function buildDisplay(group) {
const dark = "#234553";
const frame = "#152e38";
const cyan = "#61c9df";
block(group, 0.64, 0.1, 0.45, 0, 0.08, 0, frame);
block(group, 0.12, 0.85, 0.12, 0, 0.52, 0, frame);
block(group, 0.95, 0.58, 0.13, 0, 1.22, 0, frame);
block(group, 0.76, 0.39, 0.04, 0, 1.23, 0.085, cyan, {
emissive: "#1786a1"
});
for (const x of [-0.23, 0, 0.23]) {
block(group, 0.13, 0.06, 0.025, x, 1.25, 0.115, "#d7f8ff");
}
}
function buildRadio(group) {
const amber = "#e5a22f";
const amberDark = "#a66d18";
const dark = "#39484e";
block(group, 0.64, 0.3, 0.52, 0, 0.19, 0, amber);
block(group, 0.46, 0.2, 0.38, 0, 0.44, 0, amberDark);
block(group, 0.18, 0.13, 0.05, 0, 0.46, 0.21, dark);
block(group, 0.07, 0.07, 0.04, -0.13, 0.46, 0.215, "#ffdf76", {
emissive: "#d08d12"
});
block(group, 0.07, 0.55, 0.07, 0.22, 0.74, -0.12, dark);
block(group, 0.1, 0.1, 0.1, 0.22, 1.04, -0.12, amber);
}
function buildDoorway(group) {
const amber = "#e7ad3f";
const amberDark = "#bd7d20";
for (const x of [-0.55, 0.55]) {
for (let i = 0; i < 5; i++) {
block(
group,
0.29,
0.29,
0.32,
x,
0.16 + i * 0.3,
0,
i % 2 === 0 ? amber : amberDark
);
}
}
for (const x of [-0.45, -0.15, 0.15, 0.45]) {
block(group, 0.29, 0.29, 0.32, x, 1.66, 0, amber);
}
block(group, 1.15, 0.04, 0.48, 0, 0.08, 0, "#78a6b9");
}
function buildItem(item) {
const group = new THREE.Group();
group.userData.itemId = item.id;
if (item.type === "counter") buildCounter(group);
if (item.type === "chair") buildChair(group);
if (item.type === "printer") buildPrinter(group);
if (item.type === "display") buildDisplay(group);
if (item.type === "radio") buildRadio(group);
if (item.type === "doorway") buildDoorway(group);
if (item.type === "doorway") {
const world = doorWorld(item);
group.position.set(world.x, 0, world.z);
group.rotation.y = item.side === "E" || item.side === "W"
? Math.PI / 2
: 0;
} else {
const world = gridToWorld(item.x, item.z);
group.position.set(world.x, 0, world.z);
group.rotation.y = (item.rot || 0) * Math.PI / 2;
}
itemsGroup.add(group);
item.group = group;
}
function buildItems() {
clearGroup(itemsGroup);
state.items.forEach(buildItem);
}
function isTallBlocker(type) {
return type === "counter" || type === "printer" || type === "display";
}
function lineOfSight(start, end, occupancy = getOccupancy()) {
const startWorld = gridToWorld(start.x, start.z);
const endWorld = gridToWorld(end.x, end.z);
const dx = endWorld.x - startWorld.x;
const dz = endWorld.z - startWorld.z;
const distance = Math.hypot(dx, dz);
if (distance > 20) return false;
const steps = Math.max(2, Math.ceil(distance / 0.12));
for (let i = 1; i < steps; i++) {
const t = i / steps;
const worldX = startWorld.x + dx * t;
const worldZ = startWorld.z + dz * t;
const grid = worldToGrid(worldX, worldZ);
if (!inBounds(grid.x, grid.z)) return false;
const blocker = occupancy.get(itemKey(grid.x, grid.z));
if (!blocker || !isTallBlocker(blocker.type)) continue;
if (
(grid.x === start.x && grid.z === start.z) ||
(grid.x === end.x && grid.z === end.z)
) {
continue;
}
return false;
}
return true;
}
function validateLayout() {
const occupancy = getOccupancy();
const counters = state.items.filter((item) => item.type === "counter");
const chairs = state.items.filter((item) => item.type === "chair");
const anchors = state.items.filter(
(item) => item.type === "display" || item.type === "radio"
);
const results = counters.map((counter, index) => {
const front = rotateVector(0, 1, counter.rot || 0);
const back = rotateVector(0, -1, counter.rot || 0);
const approach = {
x: counter.x + front.x,
z: counter.z + front.z
};
const approachOk =
inBounds(approach.x, approach.z) &&
!occupancy.has(itemKey(approach.x, approach.z));
let chair = null;
let bestScore = Infinity;
for (const candidate of chairs) {
const dx = candidate.x - counter.x;
const dz = candidate.z - counter.z;
const along = dx * back.x + dz * back.z;
const lateral = Math.abs(dx * front.x + dz * front.z);
if (along < 0.75 || along > 1.6 || lateral > 0.55) continue;
const score = Math.abs(along - 1) + lateral * 0.25;
if (score < bestScore) {
bestScore = score;
chair = candidate;
}
}
const source = chair || counter;
let selectedAnchor = null;
let visible = false;
let nearestDistance = Infinity;
for (const anchor of anchors) {
const sourceWorld = gridToWorld(source.x, source.z);
const anchorWorld = gridToWorld(anchor.x, anchor.z);
const distance = Math.hypot(
anchorWorld.x - sourceWorld.x,
anchorWorld.z - sourceWorld.z
);
if (distance < nearestDistance) {
nearestDistance = distance;
selectedAnchor = anchor;
}
if (lineOfSight(source, anchor, occupancy)) {
if (!visible || distance < nearestDistance) {
selectedAnchor = anchor;
visible = true;
nearestDistance = distance;
}
}
}
return {
id: counter.id,
label: `Position ${String(index + 1).padStart(2, "0")}`,
counter,
approach,
approachOk,
chair,
chairOk: Boolean(chair),
anchor: selectedAnchor,
visible
};
});
return results;
}
function drawCellOutline(x, z, color, opacity = 0.95) {
const world = gridToWorld(x, z);
const half = 0.43;
const length = 0.27;
block(
overlayGroup,
length,
0.035,
0.06,
world.x - half + length / 2,
0.1,
world.z - half,
color,
{ overlay: true, opacity }
);
block(
overlayGroup,
length,
0.035,
0.06,
world.x + half - length / 2,
0.1,
world.z - half,
color,
{ overlay: true, opacity }
);
block(
overlayGroup,
length,
0.035,
0.06,
world.x - half + length / 2,
0.1,
world.z + half,
color,
{ overlay: true, opacity }
);
block(
overlayGroup,
length,
0.035,
0.06,
world.x + half - length / 2,
0.1,
world.z + half,
color,
{ overlay: true, opacity }
);
block(
overlayGroup,
0.06,
0.035,
length,
world.x - half,
0.1,
world.z - half + length / 2,
color,
{ overlay: true, opacity }
);
block(
overlayGroup,
0.06,
0.035,
length,
world.x + half,
0.1,
world.z - half + length / 2,
color,
{ overlay: true, opacity }
);
block(
overlayGroup,
0.06,
0.035,
length,
world.x - half,
0.1,
world.z + half - length / 2,
color,
{ overlay: true, opacity }
);
block(
overlayGroup,
0.06,
0.035,
length,
world.x + half,
0.1,
world.z + half - length / 2,
color,
{ overlay: true, opacity }
);
}
function drawPath(start, end, visible) {
const startWorld = gridToWorld(start.x, start.z);
const endWorld = gridToWorld(end.x, end.z);
const dx = endWorld.x - startWorld.x;
const dz = endWorld.z - startWorld.z;
const distance = Math.hypot(dx, dz);
const steps = Math.max(2, Math.floor(distance / 0.28));
const color = visible ? "#20a7c4" : "#d65358";
for (let i = 0; i <= steps; i++) {
const t = i / steps;
block(
overlayGroup,
0.075,
0.075,
0.075,
startWorld.x + dx * t,
1.17,
startWorld.z + dz * t,
color,
{ overlay: true, opacity: 0.95 }
);
}
}
function buildOverlays(results) {
clearGroup(overlayGroup);
if (!state.showOverlays) return;
for (const result of results) {
const approachColor = result.approachOk ? "#168a65" : "#d65358";
if (inBounds(result.approach.x, result.approach.z)) {
const world = gridToWorld(result.approach.x, result.approach.z);
block(
overlayGroup,
0.86,
0.025,
0.86,
world.x,
0.075,
world.z,
approachColor,
{ overlay: true, opacity: result.approachOk ? 0.17 : 0.24 }
);
drawCellOutline(
result.approach.x,
result.approach.z,
approachColor
);
}
if (result.anchor) {
drawPath(
result.chair || result.counter,
result.anchor,
result.visible
);
}
}
for (const door of state.items.filter((item) => item.type === "doorway")) {
const cell = doorApproachCell(door);
if (!inBounds(cell.x, cell.z)) continue;
const world = gridToWorld(cell.x, cell.z);
block(
overlayGroup,
0.86,
0.025,
0.86,
world.x,
0.074,
world.z,
"#3977a8",
{ overlay: true, opacity: 0.15 }
);
drawCellOutline(cell.x, cell.z, "#3977a8", 0.8);
}
const selected = getItem(state.selectedId);
if (selected && selected.type !== "doorway") {
drawCellOutline(selected.x, selected.z, "#e2a72f", 1);
}
}
function updateUI(results) {
const clearCount = results.filter((result) => result.approachOk).length;
const chairCount = results.filter((result) => result.chairOk).length;
const commCount = results.filter((result) => result.visible).length;
const total = results.length;
$("#approachMetric").textContent = `${clearCount}/${total}`;
$("#chairMetric").textContent = `${chairCount}/${total}`;
$("#commMetric").textContent = `${commCount}/${total}`;
$("#stationSummary").textContent = `${total} station${total === 1 ? "" : "s"}`;
const ready = total > 0 && clearCount === total && chairCount === total && commCount === total;
const badge = $("#scoreBadge");
badge.textContent = ready ? "Ready" : total === 0 ? "No stations" : "Needs work";
badge.classList.toggle("warning", !ready);
$("#scoreCopy").textContent = ready
? "Every service position has a clear customer approach, a staff chair, and a visible communication link."
: "Resolve the highlighted checks before the layout is workshop-ready.";
const list = $("#stationList");
if (!total) {
list.innerHTML = `
<div class="inspector-empty">
Place a counter to create the first service position.
</div>
`;
} else {
list.innerHTML = results
.map(
(result) => `
<div class="station" data-focus-id="${result.id}">
<div class="station-top">
<strong>${result.label}</strong>
<span>Grid ${result.counter.x}, ${result.counter.z}</span>
</div>
<div class="checks">
<span class="check ${result.approachOk ? "ok" : "bad"}">
${result.approachOk ? "✓" : "×"} Approach
</span>
<span class="check ${result.chairOk ? "ok" : "bad"}">
${result.chairOk ? "✓" : "×"} Chair
</span>
<span class="check ${result.visible ? "ok" : "bad"}">
${result.visible ? "✓" : "×"} Sightline
</span>
</div>
</div>
`
)
.join("");
}
const countType = (type) =>
state.items.filter((item) => item.type === type).length;
$("#printerCount").textContent = countType("printer");
$("#displayCount").textContent = countType("display");
$("#radioCount").textContent = countType("radio");
updateInspector();
updateHistoryButtons();
}
function updateInspector() {
const item = getItem(state.selectedId);
const empty = $("#inspectorEmpty");
const card = $("#inspectorCard");
if (!item) {
empty.hidden = false;
card.hidden = true;
$("#selectionHint").textContent = "Nothing selected";
return;
}
empty.hidden = true;
card.hidden = false;
$("#selectedName").textContent = typeNames[item.type];
$("#selectedType").textContent =
item.type === "doorway" ? "room entrance" : "placed object";
$("#selectionHint").textContent = "Object selected";
if (item.type === "doorway") {
$("#selectedMeta").textContent =
`${item.side} wall · wall segment ${item.index + 1}`;
$("#rotateBtn").textContent = "Change side";
} else {
$("#selectedMeta").textContent =
`Grid position ${item.x}, ${item.z} · rotation ${((item.rot || 0) * 90) % 360}°`;
$("#rotateBtn").textContent = "Rotate";
}
}
function updateModeChip() {
if (state.tool) {
$("#modeText").textContent =
`Place ${toolNames[state.tool]} — click an empty floor cell`;
} else {
$("#modeText").textContent = "Select and drag objects";
}
}
function refreshAll() {
const results = validateLayout();
buildOverlays(results);
updateUI(results);
updateModeChip();
}
function snapshot() {
return JSON.stringify({
room: { ...state.room },
items: state.items.map((item) => ({
id: item.id,
type: item.type,
x: item.x,
z: item.z,
rot: item.rot || 0,
side: item.side,
index: item.index
}))
});
}
function pushHistory() {
history.push(snapshot());
if (history.length > 60) history.shift();
redoStack.length = 0;
updateHistoryButtons();
}
function restoreSnapshot(serialized) {
const restored = JSON.parse(serialized);
state.room = restored.room;
state.items = restored.items;
state.selectedId = null;
buildRoom();
buildItems();
syncRoomInputs();
refreshAll();
}
function undo() {
if (!history.length) return;
redoStack.push(snapshot());
restoreSnapshot(history.pop());
toast("Undid last change");
}
function redo() {
if (!redoStack.length) return;
history.push(snapshot());
restoreSnapshot(redoStack.pop());
toast("Redid last change");
}
function updateHistoryButtons() {
$("#undoBtn").disabled = history.length === 0;
$("#redoBtn").disabled = redoStack.length === 0;
}
function createId(type) {
return `${type}-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
}
function selectItem(id) {
state.selectedId = id;
refreshAll();
}
function setTool(tool) {
state.tool = state.tool === tool ? null : tool;
$$(".tool").forEach((button) => {
button.classList.toggle("active", button.dataset.tool === state.tool);
});
updateModeChip();
}
function placeToolAt(point) {
if (!state.tool) return;
const type = state.tool;
const item = {
id: createId(type),
type,
x: Math.round(point.x),
z: Math.round(point.z),
rot: 0
};
if (type === "doorway") {
const edge = nearestDoorEdge(point);
item.side = edge.side;
item.index = edge.index;
updateDoorCoordinates(item);
const duplicate = state.items.some(
(existing) =>
existing.type === "doorway" &&
existing.side === item.side &&
existing.index === item.index
);
if (duplicate) {
toast("That doorway slot is already occupied");
return;
}
} else {
if (!canPlace(item, item.x, item.z)) {
toast("That cell is occupied or outside the room");
return;
}
}
pushHistory();
state.items.push(item);
state.selectedId = item.id;
buildItems();
if (item.type === "doorway") buildRoom();
refreshAll();
toast(`${typeNames[type]} placed`);
}
function deleteSelected() {
const item = getItem(state.selectedId);
if (!item) return;
pushHistory();
state.items = state.items.filter((entry) => entry.id !== item.id);
state.selectedId = null;
buildItems();
if (item.type === "doorway") buildRoom();
refreshAll();
toast(`${typeNames[item.type]} removed`);
}
function rotateSelected() {
const item = getItem(state.selectedId);
if (!item) return;
pushHistory();
if (item.type === "doorway") {
const sides = ["N", "E", "S", "W"];
const current = sides.indexOf(item.side);
item.side = sides[(current + 1) % sides.length];
const maxIndex =
item.side === "N" || item.side === "S"
? state.room.width - 1
: state.room.depth - 1;
item.index = clamp(item.index, 0, maxIndex);
updateDoorCoordinates(item);
buildItems();
buildRoom();
} else {
item.rot = ((item.rot || 0) + 1) % 4;
item.group.rotation.y = item.rot * Math.PI / 2;
}
refreshAll();
toast(`${typeNames[item.type]} rotated`);
}
function loadStarter(recordHistory = true) {
if (recordHistory) pushHistory();
const { width, depth } = state.room;
const stationCount = clamp(
parseInt($("#stationCount").value, 10) || 3,
1,
6
);
state.items = [];
state.selectedId = null;
const margin = 2;
const usable = width - margin * 2;
const counterZ = Math.max(2, depth - 3);
for (let i = 0; i < stationCount; i++) {
const x =
stationCount === 1
? Math.floor((width - 1) / 2)
: Math.round(margin + (usable * i) / (stationCount - 1));
state.items.push({
id: createId("counter"),
type: "counter",
x,
z: counterZ,
rot: 0
});
state.items.push({
id: createId("chair"),
type: "chair",
x,
z: counterZ - 1,
rot: 0
});
}
state.items.push({
id: createId("printer"),
type: "printer",
x: 1,
z: 1,
rot: 0
});
state.items.push({
id: createId("display"),
type: "display",
x: Math.floor((width - 1) / 2),
z: 1,
rot: 0
});
state.items.push({
id: createId("radio"),
type: "radio",
x: width - 2,
z: 1,
rot: 0
});
const door = {
id: createId("doorway"),
type: "doorway",
x: 0,
z: Math.floor(depth * 0.55),
rot: 0,
side: "W",
index: Math.floor(depth * 0.55)
};
updateDoorCoordinates(door);
state.items.push(door);
buildRoom();
buildItems();
refreshAll();
fitCamera();
if (recordHistory) toast("Starter layout loaded");
}
function applyRoomSize() {
const width = clamp(parseInt($("#roomWidth").value, 10) || 16, 12, 24);
const depth = clamp(parseInt($("#roomDepth").value, 10) || 10, 8, 18);
pushHistory();
state.room = { width, depth };
const occupancy = new Set();
for (const item of state.items) {
if (item.type === "doorway") {
const maxIndex =
item.side === "N" || item.side === "S" ? width - 1 : depth - 1;
item.index = clamp(item.index, 0, maxIndex);
updateDoorCoordinates(item);
continue;
}
let x = clamp(item.x, 0, width - 1);
let z = clamp(item.z, 0, depth - 1);
if (occupancy.has(itemKey(x, z))) {
let found = false;
for (let radius = 1; radius < Math.max(width, depth); radius++) {
for (let dz = -radius; dz <= radius && !found; dz++) {
for (let dx = -radius; dx <= radius && !found; dx++) {
const nx = x + dx;
const nz = z + dz;
if (
inBounds(nx, nz) &&
!occupancy.has(itemKey(nx, nz))
) {
x = nx;
z = nz;
found = true;
}
}
}
}
}
item.x = x;
item.z = z;
occupancy.add(itemKey(x, z));
}
buildRoom();
buildItems();
refreshAll();
fitCamera();
toast(`Room resized to ${width} × ${depth}`);
}
function autoPlaceMissingGear() {
const missing = ["printer", "display", "radio"].filter(
(type) => !state.items.some((item) => item.type === type)
);
if (!missing.length) {
toast("All shared equipment types are already placed");
return;
}
pushHistory();
const occupancy = getOccupancy();
const reserved = new Set();
for (const counter of state.items.filter((item) => item.type === "counter")) {
const front = rotateVector(0, 1, counter.rot || 0);
reserved.add(itemKey(counter.x + front.x, counter.z + front.z));
}
for (const door of state.items.filter((item) => item.type === "doorway")) {
const cell = doorApproachCell(door);
reserved.add(itemKey(cell.x, cell.z));
}
const candidates = [];
for (let x = 0; x < state.room.width; x++) {
for (let z = 0; z < state.room.depth; z++) {
if (!occupancy.has(itemKey(x, z)) && !reserved.has(itemKey(x, z))) {
candidates.push({ x, z });
}
}
}
const counters = state.items.filter((item) => item.type === "counter");
for (const type of missing) {
let best = null;
let bestScore = -Infinity;
for (const candidate of candidates) {
if (occupancy.has(itemKey(candidate.x, candidate.z))) continue;
if (reserved.has(itemKey(candidate.x, candidate.z))) continue;
let score = 0;
if (type === "display") {
let visibleCount = 0;
for (const counter of counters) {
const back = rotateVector(0, -1, counter.rot || 0);
const source = {
x: counter.x + back.x,
z: counter.z + back.z
};
if (lineOfSight(source, candidate, occupancy)) visibleCount++;
}
score =
visibleCount * 100 +
(state.room.depth - candidate.z) * 3 -
Math.abs(candidate.x - (state.room.width - 1) / 2);
}
if (type === "printer") {
score =
(state.room.width - candidate.x) * 3 +
(state.room.depth - candidate.z) * 2;
}
if (type === "radio") {
score =
candidate.x * 3 +
(state.room.depth - candidate.z) * 2;
}
if (score > bestScore) {
bestScore = score;
best = candidate;
}
}
if (!best) continue;
const item = {
id: createId(type),
type,
x: best.x,
z: best.z,
rot: 0
};
state.items.push(item);
occupancy.set(itemKey(best.x, best.z), item);
reserved.add(itemKey(best.x, best.z));
}
buildItems();
refreshAll();
toast("Missing shared gear placed");
}
function syncRoomInputs() {
$("#roomWidth").value = state.room.width;
$("#roomDepth").value = state.room.depth;
}
function updateCamera() {
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(cameraState.target);
camera.lookAt(cameraState.target);
}
function fitCamera() {
const maxDimension = Math.max(state.room.width, state.room.depth);
cameraState.target.set(0, 0, 0);
cameraState.radius = maxDimension * 1.55 + 3;
cameraState.phi = 0.79;
cameraState.theta = 0.72;
updateCamera();
}
function topCamera() {
const maxDimension = Math.max(state.room.width, state.room.depth);
cameraState.target.set(0, 0, 0);
cameraState.radius = maxDimension * 1.25 + 3;
cameraState.phi = 0.055;
cameraState.theta = 0;
updateCamera();
}
function focusItem(id) {
const item = getItem(id);
if (!item) return;
const world =
item.type === "doorway" ? doorWorld(item) : gridToWorld(item.x, item.z);
cameraState.target.set(world.x, 0, world.z);
cameraState.radius = Math.max(11, cameraState.radius * 0.7);
cameraState.phi = 0.88;
updateCamera();
}
function updatePointerNdc(event) {
const rect = canvas.getBoundingClientRect();
pointerNdc.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNdc.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNdc, camera);
}
function pointOnFloor(event) {
updatePointerNdc(event);
const point = new THREE.Vector3();
const hit = raycaster.ray.intersectPlane(groundPlane, point);
return hit ? point : null;
}
function pickItem(event) {
updatePointerNdc(event);
const hits = raycaster.intersectObjects(itemsGroup.children, true);
for (const hit of hits) {
let object = hit.object;
while (object) {
if (object.userData.itemId) {
return state.items.find((item) => item.id === object.userData.itemId);
}
object = object.parent;
}
}
return null;
}
const pointer = {
down: false,
mode: null,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false,
dragBefore: null,
dragItem: null
};
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointer.down = true;
pointer.mode = null;
pointer.startX = event.clientX;
pointer.startY = event.clientY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
pointer.moved = false;
const item = pickItem(event);
if (item && event.button === 0) {
selectItem(item.id);
pointer.mode = "drag";
pointer.dragItem = item;
pointer.dragBefore = snapshot();
return;
}
if (event.button === 0 && state.tool) {
const point = pointOnFloor(event);
if (point) placeToolAt(point);
pointer.mode = "tool";
return;
}
if (event.button === 2 || event.button === 1 || event.shiftKey) {
pointer.mode = "pan";
} else {
pointer.mode = "orbit";
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.down) return;
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
if (
Math.abs(event.clientX - pointer.startX) +
Math.abs(event.clientY - pointer.startY) >
4
) {
pointer.moved = true;
}
if (pointer.mode === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = clamp(cameraState.phi - dy * 0.005, 0.08, 1.42);
updateCamera();
}
if (pointer.mode === "pan") {
const scale = (cameraState.radius / Math.max(canvas.clientHeight, 1)) * 0.9;
const right = { x: Math.cos(cameraState.theta), z: -Math.sin(cameraState.theta) };
const forward = { x: Math.sin(cameraState.theta), z: Math.cos(cameraState.theta) };
cameraState.target.x -= dx * scale * right.x + dy * scale * forward.x;
cameraState.target.z -= dx * scale * right.z + dy * scale * forward.z;
updateCamera();
}
if (pointer.mode === "drag" && pointer.dragItem) {
const item = pointer.dragItem;
const point = pointOnFloor(event);
if (!point) return;
if (item.type === "doorway") {
const edge = nearestDoorEdge(point);
const duplicate = state.items.some(
(existing) =>
existing !== item &&
existing.type === "doorway" &&
existing.side === edge.side &&
existing.index === edge.index
);
if (!duplicate) {
item.side = edge.side;
item.index = edge.index;
updateDoorCoordinates(item);
const world = doorWorld(item);
item.group.position.set(world.x, 0, world.z);
item.group.rotation.y =
item.side === "E" || item.side === "W" ? Math.PI / 2 : 0;
}
} else {
const x = clamp(Math.round(point.x), 0, state.room.width - 1);
const z = clamp(Math.round(point.z), 0, state.room.depth - 1);
if (canPlace(item, x, z)) {
item.x = x;
item.z = z;
const world = gridToWorld(x, z);
item.group.position.set(world.x, 0, world.z);
}
}
refreshAll();
}
});
function endPointer(event) {
if (!pointer.down) return;
if (
pointer.mode === "drag" &&
pointer.dragItem &&
pointer.moved &&
pointer.dragBefore
) {
const before = JSON.parse(pointer.dragBefore);
const after = JSON.parse(snapshot());
if (JSON.stringify(before) !== JSON.stringify(after)) {
history.push(pointer.dragBefore);
redoStack.length = 0;
updateHistoryButtons();
}
if (pointer.dragItem.type === "doorway") {
buildRoom();
refreshAll();
}
}
if (
pointer.mode === "orbit" &&
!pointer.moved &&
!state.tool &&
state.selectedId
) {
state.selectedId = null;
refreshAll();
}
pointer.down = false;
pointer.mode = null;
pointer.dragItem = null;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius * (event.deltaY > 0 ? 1.09 : 0.92),
8,
60
);
updateCamera();
},
{ passive: false }
);
$$(".tool").forEach((button) => {
button.addEventListener("click", () => setTool(button.dataset.tool));
});
$("#applyRoomBtn").addEventListener("click", applyRoomSize);
$("#starterBtn").addEventListener("click", () => loadStarter(true));
$("#autoGearBtn").addEventListener("click", autoPlaceMissingGear);
$("#rotateBtn").addEventListener("click", rotateSelected);
$("#deleteBtn").addEventListener("click", deleteSelected);
$("#overlayToggle").addEventListener("change", (event) => {
state.showOverlays = event.target.checked;
refreshAll();
});
$("#undoBtn").addEventListener("click", undo);
$("#redoBtn").addEventListener("click", redo);
$("#fitBtn").addEventListener("click", fitCamera);
$("#topBtn").addEventListener("click", topCamera);
$("#zoomInBtn").addEventListener("click", () => {
cameraState.radius = clamp(cameraState.radius * 0.86, 8, 60);
updateCamera();
});
$("#zoomOutBtn").addEventListener("click", () => {
cameraState.radius = clamp(cameraState.radius * 1.16, 8, 60);
updateCamera();
});
$("#stationList").addEventListener("click", (event) => {
const station = event.target.closest("[data-focus-id]");
if (station) focusItem(station.dataset.focusId);
});
$("#saveBtn").addEventListener("click", () => {
try {
localStorage.setItem("reception-flow-lab", snapshot());
toast("Layout saved in this browser");
} catch (error) {
toast("Could not save locally");
}
});
$("#resetBtn").addEventListener("click", () => {
if (confirm("Replace the current layout with a starter layout?")) {
loadStarter(true);
}
});
$("#exportBtn").addEventListener("click", () => {
const exportData = {
version: 1,
exportedAt: new Date().toISOString(),
room: state.room,
items: state.items.map((item) => ({
id: item.id,
type: item.type,
x: item.x,
z: item.z,
rotation: item.rot || 0,
side: item.side,
index: item.index
}))
};
const blob = new Blob([JSON.stringify(exportData, null, 2)], {
type: "application/json"
});
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = url;
anchor.download = "reception-flow-layout.json";
anchor.click();
URL.revokeObjectURL(url);
toast("Layout exported");
});
window.addEventListener("keydown", (event) => {
const activeTag = document.activeElement?.tagName;
const isInput =
activeTag === "INPUT" || activeTag === "TEXTAREA" || activeTag === "SELECT";
if (isInput) return;
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "z") {
event.preventDefault();
if (event.shiftKey) redo();
else undo();
return;
}
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "y") {
event.preventDefault();
redo();
return;
}
if (event.key === "Escape") {
state.tool = null;
state.selectedId = null;
$$(".tool").forEach((button) => button.classList.remove("active"));
refreshAll();
}
if (event.key.toLowerCase() === "r") {
event.preventDefault();
rotateSelected();
}
if (event.key === "Delete" || event.key === "Backspace") {
event.preventDefault();
deleteSelected();
}
const shortcuts = {
"1": "counter",
"2": "chair",
"3": "printer",
"4": "display",
"5": "radio",
"6": "doorway"
};
if (shortcuts[event.key]) {
setTool(shortcuts[event.key]);
}
});
let toastTimer;
function toast(message) {
const element = $("#toast");
element.textContent = message;
element.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => element.classList.remove("show"), 2200);
}
const resizeObserver = new ResizeObserver(() => {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
});
resizeObserver.observe(viewport);
buildRoom();
loadStarter(false);
updateCamera();
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```