Prompt specification
Build or customize the Three.js 3D scene "Low-power market lighting game" 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: Low-power market lighting game
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/low-power-market-lighting-game-x0520
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring night-market lighting. Create a night-market model with a fictional lighting budget; users select lamp locations to illuminate service surfaces and routes while a transparent counter tracks assigned power values. Low-poly.
Working Three.js Starter (low-power-market-lighting-game-x0520):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Night / Light — Market Lighting Planner</title>
<style>
:root {
color-scheme: dark;
--bg: #070b11;
--panel: rgba(10, 16, 24, 0.78);
--panel-strong: rgba(10, 16, 24, 0.92);
--line: rgba(190, 219, 231, 0.14);
--line-strong: rgba(190, 219, 231, 0.25);
--text: #edf5f5;
--muted: #8fa3aa;
--dim: #5e727b;
--orange: #ff7b4b;
--amber: #ffc36b;
--mint: #64dfc4;
--blue: #8bcdf2;
--danger: #ff6672;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
color: inherit;
font: inherit;
}
button:focus-visible {
outline: 2px solid var(--mint);
outline-offset: 3px;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
#scene.node-hover {
cursor: crosshair;
}
#labelLayer {
position: fixed;
inset: 0;
overflow: hidden;
pointer-events: none;
z-index: 2;
}
.world-label {
position: absolute;
left: 0;
top: 0;
display: flex;
align-items: center;
gap: 6px;
min-height: 24px;
padding: 5px 8px;
border: 1px solid rgba(194, 224, 232, 0.18);
border-radius: 5px;
background: rgba(8, 13, 19, 0.78);
color: #dbe9ec;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
line-height: 1;
white-space: nowrap;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.2);
will-change: transform;
}
.world-label em {
color: var(--amber);
font-style: normal;
font-size: 8px;
letter-spacing: 0.04em;
}
.node-label {
opacity: 0.72;
}
.node-label.assigned {
opacity: 1;
border-color: rgba(255, 195, 107, 0.3);
}
.node-label .pin {
width: 5px;
height: 5px;
border-radius: 50%;
background: #8197a0;
box-shadow: 0 0 0 3px rgba(129, 151, 160, 0.1);
}
.node-label.assigned .pin {
background: var(--amber);
box-shadow: 0 0 10px rgba(255, 195, 107, 0.8);
}
.service-label {
padding: 5px 7px;
background: rgba(8, 13, 19, 0.67);
opacity: 0.86;
}
.service-label .dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--dim);
}
.service-label.lit {
border-color: rgba(100, 223, 196, 0.38);
}
.service-label.lit .dot {
background: var(--mint);
box-shadow: 0 0 9px rgba(100, 223, 196, 0.8);
}
.service-label.lit em {
color: var(--mint);
}
.topbar {
position: fixed;
top: 20px;
left: 22px;
right: 22px;
z-index: 20;
display: flex;
justify-content: space-between;
align-items: flex-start;
pointer-events: none;
}
.brand,
.top-actions {
pointer-events: auto;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
padding: 9px 13px 9px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(8, 13, 20, 0.73);
backdrop-filter: blur(14px);
box-shadow: 0 14px 38px rgba(0, 0, 0, 0.22);
}
.brand-mark {
position: relative;
width: 35px;
height: 35px;
border: 1px solid rgba(255, 123, 75, 0.48);
border-radius: 7px;
background:
linear-gradient(135deg, rgba(255, 123, 75, 0.22), transparent 65%),
#111924;
overflow: hidden;
}
.brand-mark::before {
content: "";
position: absolute;
width: 15px;
height: 15px;
left: 9px;
top: 7px;
border-radius: 50%;
background: var(--orange);
box-shadow:
0 0 8px rgba(255, 123, 75, 0.7),
0 12px 22px rgba(255, 123, 75, 0.55);
}
.brand-mark::after {
content: "";
position: absolute;
width: 1px;
height: 28px;
left: 16px;
top: -1px;
background: rgba(255, 255, 255, 0.32);
transform: rotate(24deg);
}
.brand h1 {
margin: 0;
font-size: 15px;
line-height: 1;
letter-spacing: 0.18em;
font-weight: 800;
}
.brand p {
margin: 5px 0 0;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.top-actions {
display: flex;
align-items: center;
gap: 8px;
}
.scenario {
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(8, 13, 20, 0.72);
backdrop-filter: blur(14px);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.scenario strong {
color: var(--amber);
font-weight: 800;
}
.icon-button,
.text-button {
border: 1px solid var(--line);
border-radius: 7px;
background: rgba(8, 13, 20, 0.76);
backdrop-filter: blur(14px);
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
color 150ms ease;
}
.icon-button {
width: 37px;
height: 37px;
display: grid;
place-items: center;
color: var(--muted);
font-size: 17px;
}
.text-button {
height: 37px;
padding: 0 13px;
color: #c8d7db;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.icon-button:hover,
.text-button:hover {
color: white;
border-color: rgba(255, 255, 255, 0.32);
background: rgba(20, 29, 39, 0.88);
}
.mobile-only {
display: none;
}
.panel {
position: fixed;
z-index: 15;
top: 84px;
bottom: 73px;
width: 286px;
padding: 16px;
overflow: auto;
border: 1px solid var(--line);
border-radius: 11px;
background:
linear-gradient(180deg, rgba(17, 25, 35, 0.84), rgba(8, 13, 20, 0.73)),
rgba(8, 13, 20, 0.78);
backdrop-filter: blur(18px) saturate(125%);
box-shadow:
0 22px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.025);
scrollbar-width: thin;
scrollbar-color: #33434c transparent;
}
.panel.left {
left: 22px;
}
.panel.right {
right: 22px;
width: 310px;
display: flex;
flex-direction: column;
}
.section + .section {
margin-top: 19px;
padding-top: 17px;
border-top: 1px solid var(--line);
}
.eyebrow {
margin: 0 0 5px;
color: var(--dim);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
}
.section-heading h2 {
margin: 0;
color: #dce8ea;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-heading span {
color: var(--dim);
font-size: 9px;
}
.lamp-options {
display: grid;
gap: 7px;
}
.lamp-card {
width: 100%;
display: grid;
grid-template-columns: 34px 1fr auto;
align-items: center;
gap: 9px;
padding: 10px;
text-align: left;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
.lamp-card:hover {
border-color: var(--line-strong);
background: rgba(255, 255, 255, 0.045);
transform: translateX(1px);
}
.lamp-card.active {
border-color: rgba(255, 123, 75, 0.65);
background:
linear-gradient(90deg, rgba(255, 123, 75, 0.12), transparent),
rgba(255, 255, 255, 0.025);
box-shadow: inset 2px 0 0 var(--orange);
}
.fixture-icon {
position: relative;
width: 30px;
height: 32px;
display: grid;
place-items: center;
}
.fixture-icon::before {
content: "";
position: absolute;
left: 14px;
bottom: 3px;
width: 1px;
height: 20px;
background: #647780;
}
.fixture-icon::after {
content: "";
position: absolute;
left: 9px;
top: 5px;
width: 11px;
height: 8px;
border-radius: 2px;
background: var(--fixture-color);
box-shadow: 0 0 12px color-mix(in srgb, var(--fixture-color), transparent 35%);
}
.fixture-icon.pendant::before {
height: 24px;
}
.fixture-icon.pendant::after {
top: 3px;
width: 12px;
border-radius: 9px 9px 3px 3px;
}
.fixture-icon.flood::after {
top: 7px;
width: 14px;
height: 7px;
transform: rotate(-18deg);
}
.lamp-copy strong {
display: block;
margin-bottom: 4px;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.025em;
}
.lamp-copy small {
display: block;
color: var(--muted);
font-size: 8.5px;
line-height: 1.35;
}
.lamp-spec {
text-align: right;
}
.lamp-spec b {
display: block;
color: #eef7f7;
font-size: 12px;
}
.lamp-spec small {
color: var(--dim);
font-size: 7.5px;
white-space: nowrap;
}
.budget-card {
padding: 12px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.025);
}
.budget-top {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 10px;
}
.budget-top span {
color: var(--muted);
font-size: 9px;
}
.budget-top strong {
display: block;
margin-top: 4px;
font-size: 20px;
font-weight: 750;
letter-spacing: -0.035em;
}
.budget-top strong small {
color: var(--dim);
font-size: 10px;
font-weight: 600;
}
.remaining {
padding-bottom: 3px;
color: var(--mint);
font-size: 9px;
font-weight: 750;
text-align: right;
}
.budget-track {
position: relative;
height: 5px;
margin-top: 12px;
overflow: hidden;
border-radius: 5px;
background: rgba(255, 255, 255, 0.07);
}
.budget-track i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--orange), var(--amber));
box-shadow: 0 0 12px rgba(255, 157, 91, 0.35);
transition: width 220ms ease;
}
.budget-caption {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--dim);
font-size: 8px;
}
.coverage-list {
display: grid;
gap: 11px;
}
.coverage-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 6px 12px;
align-items: center;
}
.coverage-row > span {
color: #c8d6d9;
font-size: 9.5px;
}
.coverage-row > strong {
color: var(--mint);
font-size: 9px;
font-weight: 750;
}
.coverage-bar {
grid-column: 1 / -1;
height: 3px;
overflow: hidden;
border-radius: 3px;
background: rgba(255, 255, 255, 0.065);
}
.coverage-bar i {
display: block;
width: 0;
height: 100%;
background: var(--mint);
transition: width 250ms ease;
}
.mini-note {
margin: 11px 0 0;
color: var(--dim);
font-size: 8.5px;
line-height: 1.55;
}
.counter-header {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 14px;
}
.counter-header h2 {
margin: 0;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.clear-button {
padding: 0;
border: 0;
background: transparent;
color: var(--dim);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
cursor: pointer;
}
.clear-button:hover {
color: var(--orange);
}
.counter-total {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
padding: 14px;
border: 1px solid rgba(255, 195, 107, 0.2);
border-radius: 9px;
background:
radial-gradient(circle at 80% 20%, rgba(255, 195, 107, 0.1), transparent 55%),
rgba(255, 255, 255, 0.025);
}
.counter-total span {
color: var(--muted);
font-size: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.counter-total strong {
display: block;
margin-top: 4px;
color: var(--amber);
font-size: 27px;
font-weight: 760;
letter-spacing: -0.05em;
}
.counter-total strong small {
color: var(--dim);
font-size: 10px;
letter-spacing: 0;
}
.fixture-count {
color: var(--muted);
font-size: 9px;
text-align: right;
}
.fixture-count strong {
display: block;
color: white;
font-size: 13px;
letter-spacing: 0;
}
.counter-bar {
height: 4px;
margin: 10px 0 15px;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.065);
}
.counter-bar i {
display: block;
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--orange), var(--amber));
transition: width 220ms ease;
}
.counter-labels {
display: grid;
grid-template-columns: 1fr auto;
padding: 0 2px 8px;
border-bottom: 1px solid var(--line);
color: var(--dim);
font-size: 7.5px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#counterRows {
min-height: 90px;
margin-top: 3px;
}
.counter-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 4px 10px;
padding: 11px 2px;
border-bottom: 1px solid rgba(190, 219, 231, 0.08);
cursor: pointer;
}
.counter-row:hover .row-location {
color: white;
}
.row-main {
min-width: 0;
}
.row-location {
overflow: hidden;
color: #d5e2e4;
font-size: 10px;
font-weight: 720;
text-overflow: ellipsis;
white-space: nowrap;
}
.row-type {
margin-top: 4px;
color: var(--muted);
font-size: 8.5px;
}
.row-type i {
display: inline-block;
width: 5px;
height: 5px;
margin-right: 5px;
border-radius: 50%;
background: var(--fixture-color);
box-shadow: 0 0 7px var(--fixture-color);
}
.row-watt {
padding-top: 1px;
color: var(--amber);
font-size: 11px;
font-weight: 760;
text-align: right;
}
.row-remove {
grid-column: 2;
justify-self: end;
width: 22px;
height: 20px;
padding: 0;
border: 1px solid transparent;
border-radius: 4px;
background: transparent;
color: var(--dim);
font-size: 14px;
line-height: 1;
cursor: pointer;
}
.row-remove:hover {
border-color: rgba(255, 102, 114, 0.3);
color: var(--danger);
background: rgba(255, 102, 114, 0.07);
}
.empty-counter {
padding: 28px 10px;
color: var(--dim);
font-size: 9px;
line-height: 1.6;
text-align: center;
}
.counter-footer {
margin-top: auto;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.counter-footer p {
margin: 0;
color: var(--dim);
font-size: 8px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 11px;
color: var(--muted);
font-size: 8px;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--mint);
}
.legend i.uncovered {
background: var(--dim);
}
.hint-bar {
position: fixed;
z-index: 14;
bottom: 19px;
left: 50%;
display: flex;
align-items: center;
gap: 15px;
max-width: calc(100vw - 44px);
padding: 9px 13px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(8, 13, 20, 0.78);
backdrop-filter: blur(14px);
color: var(--muted);
font-size: 8px;
letter-spacing: 0.04em;
white-space: nowrap;
box-shadow: 0 10px 32px rgba(0, 0, 0, 0.22);
}
.hint-bar span {
display: flex;
align-items: center;
gap: 5px;
}
kbd {
min-width: 19px;
padding: 3px 5px;
border: 1px solid var(--line-strong);
border-radius: 4px;
background: rgba(255, 255, 255, 0.04);
color: #d9e6e8;
font-family: inherit;
font-size: 7px;
font-weight: 750;
text-align: center;
}
.tooltip {
position: fixed;
z-index: 40;
display: none;
min-width: 150px;
padding: 9px 11px;
pointer-events: none;
border: 1px solid var(--line-strong);
border-radius: 7px;
background: rgba(6, 10, 15, 0.94);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
}
.tooltip strong {
display: block;
color: white;
font-size: 10px;
}
.tooltip span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 8.5px;
}
.toast {
position: fixed;
z-index: 50;
left: 50%;
bottom: 76px;
padding: 10px 14px;
transform: translate(-50%, 14px);
opacity: 0;
pointer-events: none;
border: 1px solid rgba(100, 223, 196, 0.32);
border-radius: 7px;
background: rgba(9, 18, 23, 0.94);
color: #d9eeea;
font-size: 9px;
font-weight: 650;
transition:
opacity 180ms ease,
transform 180ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.toast.error {
border-color: rgba(255, 102, 114, 0.45);
color: #ffd6d9;
}
@media (max-width: 1100px) {
.panel.left {
width: 258px;
}
.panel.right {
width: 280px;
}
.scenario {
display: none;
}
.hint-bar {
gap: 9px;
}
}
@media (max-width: 760px) {
.topbar {
top: 12px;
left: 12px;
right: 12px;
}
.brand {
padding: 7px 10px 7px 8px;
}
.brand h1 {
font-size: 12px;
}
.brand p {
display: none;
}
.brand-mark {
width: 30px;
height: 30px;
}
.brand-mark::before {
left: 7px;
top: 6px;
}
.brand-mark::after {
left: 14px;
height: 24px;
}
.mobile-only {
display: grid;
}
.top-actions {
gap: 5px;
}
.top-actions .text-button {
display: none;
}
.panel {
display: none;
top: 66px;
bottom: auto;
left: 12px;
right: 12px;
width: auto;
max-height: 47vh;
padding: 14px;
}
body.kit-open .panel.left {
display: block;
}
body.counter-open .panel.right {
display: flex;
}
.hint-bar {
bottom: 10px;
max-width: calc(100vw - 20px);
overflow: hidden;
gap: 10px;
}
.hint-bar span:nth-child(3),
.hint-bar span:nth-child(4) {
display: none;
}
.world-label {
font-size: 8px;
}
}
</style>
</head>
<body class="kit-open">
<canvas id="scene" aria-label="Interactive low-poly night market lighting planner"></canvas>
<div id="labelLayer"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>NIGHT / LIGHT</h1>
<p>Market lighting planner</p>
</div>
</div>
<div class="top-actions">
<div class="scenario">
Draft 01 · <strong id="scenarioPower">180 W</strong>
</div>
<button
class="icon-button mobile-only"
id="toggleKit"
type="button"
aria-label="Toggle fixture kit"
title="Fixture kit"
>
☷
</button>
<button
class="icon-button mobile-only"
id="toggleCounter"
type="button"
aria-label="Toggle power counter"
title="Power counter"
>
≡
</button>
<button class="text-button" id="resetPlan" type="button">Clear plan</button>
<button class="icon-button" id="resetView" type="button" title="Reset view" aria-label="Reset view">
↺
</button>
</div>
</header>
<aside class="panel left" aria-label="Fixture kit and coverage">
<section class="section">
<p class="eyebrow">Select a fixture</p>
<div class="section-heading">
<h2>Fixture Kit</h2>
<span>Click a market node</span>
</div>
<div class="lamp-options" id="lampOptions"></div>
<p class="mini-note">
Starter fixtures are editable. Use <strong>Shift + click</strong> to remove a lamp.
</p>
</section>
<section class="section">
<div class="section-heading">
<h2>Fictional Night Budget</h2>
<span>Planning units</span>
</div>
<div class="budget-card">
<div class="budget-top">
<div>
<span>Assigned power</span>
<strong><span id="budgetAssigned">180</span> <small>/ 1,800 W</small></strong>
</div>
<div class="remaining" id="budgetRemaining">1,620 W free</div>
</div>
<div class="budget-track"><i id="budgetFill"></i></div>
<div class="budget-caption">
<span>0 W</span>
<span>1,800 W limit</span>
</div>
</div>
</section>
<section class="section">
<div class="section-heading">
<h2>Plan Coverage</h2>
<span>Live estimate</span>
</div>
<div class="coverage-list">
<div class="coverage-row">
<span>Service counters</span>
<strong id="coverageService">4 / 6</strong>
<div class="coverage-bar"><i id="coverageServiceBar"></i></div>
</div>
<div class="coverage-row">
<span>Main promenade</span>
<strong id="coveragePromenade">0%</strong>
<div class="coverage-bar"><i id="coveragePromenadeBar"></i></div>
</div>
<div class="coverage-row">
<span>North arcade</span>
<strong id="coverageNorth">0%</strong>
<div class="coverage-bar"><i id="coverageNorthBar"></i></div>
</div>
<div class="coverage-row">
<span>South arcade</span>
<strong id="coverageSouth">0%</strong>
<div class="coverage-bar"><i id="coverageSouthBar"></i></div>
</div>
</div>
<p class="mini-note">
Coverage is estimated from fixture reach in this fictional planning model.
</p>
</section>
</aside>
<aside class="panel right" aria-label="Transparent assigned power counter">
<div class="counter-header">
<div>
<p class="eyebrow">Transparent planning counter</p>
<h2>Assigned Power</h2>
</div>
<button class="clear-button" id="clearPlan" type="button">Clear all</button>
</div>
<div class="counter-total">
<div>
<span>Current draw</span>
<strong><span id="counterAssigned">180</span> <small>W</small></strong>
</div>
<div class="fixture-count">
<strong id="fixtureCount">2</strong>
fixtures assigned
</div>
</div>
<div class="counter-bar"><i id="counterFill"></i></div>
<div class="counter-labels">
<span>Location / fixture</span>
<span>Power</span>
</div>
<div id="counterRows"></div>
<div class="counter-footer">
<p>
W is a fictional planning unit for this scenario. A placement is accepted only
while the total remains within the 1,800 W night budget.
</p>
<div class="legend">
<span><i></i> Covered</span>
<span><i class="uncovered"></i> Uncovered</span>
</div>
</div>
</aside>
<div class="hint-bar" aria-label="Interaction instructions">
<span><kbd>Click</kbd> place fixture</span>
<span><kbd>Shift</kbd> remove</span>
<span><kbd>Drag</kbd> orbit</span>
<span><kbd>Scroll</kbd> zoom</span>
</div>
<div class="tooltip" id="tooltip"></div>
<div class="toast" 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 canvas = document.getElementById("scene");
const labelLayer = document.getElementById("labelLayer");
const tooltip = document.getElementById("tooltip");
const toastElement = document.getElementById("toast");
const BUDGET = 1800;
const LAMP_TYPES = {
bollard: {
id: "bollard",
name: "Path Bollard",
shortName: "Bollard",
watt: 60,
range: 5.5,
color: 0xffbf67,
colorCSS: "#ffbf67",
description: "Low-glare pedestrian light"
},
pendant: {
id: "pendant",
name: "Market Pendant",
shortName: "Pendant",
watt: 120,
range: 8.2,
color: 0xffdfa4,
colorCSS: "#ffdfa4",
description: "Warm service and route light"
},
flood: {
id: "flood",
name: "Area Flood",
shortName: "Flood",
watt: 220,
range: 11.8,
color: 0x9adfff,
colorCSS: "#9adfff",
description: "Wide coverage for large approaches"
}
};
const NODE_DATA = [
["W1", "West gate", -15.0, 0.0],
["W2", "Tea approach", -10.0, 0.0],
["W3", "West promenade", -5.0, 0.0],
["C1", "Central crossing", 0.0, 0.0],
["E1", "East promenade", 5.0, 0.0],
["E2", "Spice approach", 10.0, 0.0],
["E3", "East gate", 15.0, 0.0],
["N1", "North tea edge", -10.0, -3.7],
["N2", "North tea counter", 0.0, -3.7],
["N3", "North spice edge", 10.0, -3.7],
["S1", "South cake edge", -10.0, 3.7],
["S2", "South citrus edge", 0.0, 3.7],
["S3", "South checkout edge", 10.0, 3.7]
];
const nodes = [];
const nodeById = new Map();
const assignments = new Map();
const pickTargets = [];
const serviceTargets = [];
const routeSegments = [];
let selectedLamp = "pendant";
let hoveredNode = null;
let toastTimer = 0;
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
let renderer;
try {
renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
} catch (error) {
document.body.innerHTML =
'<div style="padding:40px;color:#eee;font:16px system-ui">' +
"This scene requires WebGL.</div>";
throw error;
}
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070b12);
scene.fog = new THREE.FogExp2(0x080d15, 0.018);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
150
);
const controls = {
target: new THREE.Vector3(0, 0.3, 0),
yaw: 0.58,
pitch: 0.72,
radius: 32
};
function updateCamera() {
const horizontal = Math.cos(controls.pitch) * controls.radius;
camera.position.set(
controls.target.x + Math.sin(controls.yaw) * horizontal,
controls.target.y + Math.sin(controls.pitch) * controls.radius,
controls.target.z + Math.cos(controls.yaw) * horizontal
);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0x617da8, 0x24130d, 1.25);
scene.add(hemisphere);
const moon = new THREE.DirectionalLight(0x9ab8ea, 2.0);
moon.position.set(-13, 22, 11);
moon.castShadow = true;
moon.shadow.mapSize.set(2048, 2048);
moon.shadow.camera.left = -28;
moon.shadow.camera.right = 28;
moon.shadow.camera.top = 23;
moon.shadow.camera.bottom = -23;
moon.shadow.camera.near = 1;
moon.shadow.camera.far = 65;
moon.shadow.bias = -0.0004;
scene.add(moon);
const ambient = new THREE.AmbientLight(0x70899b, 0.22);
scene.add(ambient);
// -------------------------------------------------------------------------
// Materials and low-poly market construction
// -------------------------------------------------------------------------
const materials = {
ground: new THREE.MeshStandardMaterial({
color: 0x101820,
roughness: 0.94,
metalness: 0.04
}),
curb: new THREE.MeshStandardMaterial({
color: 0x28333c,
roughness: 0.8
}),
darkWood: new THREE.MeshStandardMaterial({
color: 0x33251e,
roughness: 0.83
}),
wood: new THREE.MeshStandardMaterial({
color: 0x6c4933,
roughness: 0.72
}),
counter: new THREE.MeshStandardMaterial({
color: 0x472b22,
roughness: 0.76
}),
metal: new THREE.MeshStandardMaterial({
color: 0x28343b,
roughness: 0.48,
metalness: 0.55
}),
crate: new THREE.MeshStandardMaterial({
color: 0x62422c,
roughness: 0.9
}),
leaf: new THREE.MeshStandardMaterial({
color: 0x315345,
roughness: 0.95
}),
pot: new THREE.MeshStandardMaterial({
color: 0x694634,
roughness: 0.88
})
};
function addBox(parent, size, position, material, cast = true, receive = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
material,
segments = 8
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const ground = new THREE.Mesh(new THREE.PlaneGeometry(52, 36), materials.ground);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(50, 25, 0x31414c, 0x1d2a33);
grid.position.y = 0.012;
grid.scale.z = 0.7;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
addBox(scene, [52, 0.18, 0.3], [0, 0.02, -18], materials.curb, false, true);
addBox(scene, [52, 0.18, 0.3], [0, 0.02, 18], materials.curb, false, true);
addBox(scene, [0.3, 0.18, 36], [-26, 0.02, 0], materials.curb, false, true);
addBox(scene, [0.3, 0.18, 36], [26, 0.02, 0], materials.curb, false, true);
function makeSignTexture(text, accent = "#ff8a52") {
const c = document.createElement("canvas");
c.width = 768;
c.height = 160;
const ctx = c.getContext("2d");
ctx.fillStyle = "#101820";
ctx.fillRect(0, 0, c.width, c.height);
ctx.strokeStyle = accent;
ctx.lineWidth = 8;
ctx.strokeRect(10, 10, c.width - 20, c.height - 20);
ctx.fillStyle = accent;
ctx.fillRect(28, 34, 9, 92);
ctx.fillStyle = "#f3eee2";
ctx.font = "800 57px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, c.width / 2 + 10, c.height / 2 + 2);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
function createStall({
x,
z,
rotation,
name,
accent,
productColor
}) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
addBox(group, [5.2, 0.18, 3.6], [0, 0.09, 0], materials.curb);
addBox(group, [4.7, 2.6, 0.22], [0, 1.45, -1.45], materials.darkWood);
addBox(group, [4.8, 0.9, 1.15], [0, 0.62, 1.15], materials.counter);
addBox(group, [5.05, 0.15, 1.35], [0, 1.12, 1.15], materials.wood);
const surfaceMaterial = new THREE.MeshStandardMaterial({
color: 0x503126,
emissive: 0x6a2b18,
emissiveIntensity: 0.16,
roughness: 0.52,
metalness: 0.06
});
const surface = addBox(
group,
[4.55, 0.055, 0.88],
[0, 1.23, 1.18],
surfaceMaterial,
false,
true
);
for (const side of [-1, 1]) {
addBox(
group,
[0.14, 3.05, 0.14],
[side * 2.35, 1.52, 1.3],
materials.metal
);
addBox(
group,
[0.14, 3.05, 0.14],
[side * 2.35, 1.52, -1.38],
materials.metal
);
}
const stripeMaterials = [
new THREE.MeshStandardMaterial({ color: accent, roughness: 0.83 }),
new THREE.MeshStandardMaterial({ color: 0xe7bd80, roughness: 0.85 })
];
for (let i = 0; i < 8; i++) {
const stripe = addBox(
group,
[0.69, 0.14, 3.35],
[(i - 3.5) * 0.69, 3.02, -0.1],
stripeMaterials[i % 2]
);
stripe.rotation.x = -0.035;
}
addBox(group, [5.75, 0.18, 0.18], [0, 2.8, 1.54], materials.metal);
const signMaterial = new THREE.MeshBasicMaterial({
map: makeSignTexture(name, accent),
transparent: true,
side: THREE.DoubleSide
});
const sign = new THREE.Mesh(new THREE.PlaneGeometry(3.7, 0.7), signMaterial);
sign.position.set(0, 2.18, 1.34);
group.add(sign);
const productMaterial = new THREE.MeshStandardMaterial({
color: productColor,
emissive: productColor,
emissiveIntensity: 0.08,
roughness: 0.64
});
for (let i = 0; i < 6; i++) {
const product = addBox(
group,
[0.26 + (i % 2) * 0.12, 0.28, 0.3],
[-1.75 + i * 0.67, 1.43, 1.15],
productMaterial,
true,
false
);
product.rotation.y = i * 0.7;
}
const floorRing = new THREE.Mesh(
new THREE.CircleGeometry(2.15, 28),
new THREE.MeshBasicMaterial({
color: 0x4c6670,
transparent: true,
opacity: 0.045,
depthWrite: false,
side: THREE.DoubleSide
})
);
floorRing.rotation.x = -Math.PI / 2;
floorRing.position.set(0, 0.027, 1.75);
group.add(floorRing);
const localService = new THREE.Vector3(0, 0, 1.15).applyAxisAngle(
new THREE.Vector3(0, 1, 0),
rotation
);
const service = {
id: `service-${name.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`,
name,
x: x + localService.x,
z: z + localService.z,
surface,
floorRing,
covered: false
};
serviceTargets.push(service);
return service;
}
createStall({
x: -10,
z: -7.2,
rotation: 0,
name: "NOODLE 24",
accent: "#d95739",
productColor: 0xe8a34f
});
createStall({
x: 0,
z: -7.4,
rotation: 0,
name: "TEA HOUSE",
accent: "#658b73",
productColor: 0x88c2a8
});
createStall({
x: 10,
z: -7.2,
rotation: 0,
name: "SPICE BOX",
accent: "#c55538",
productColor: 0xd66b3f
});
createStall({
x: -10,
z: 7.2,
rotation: Math.PI,
name: "MOON CAKE",
accent: "#886a99",
productColor: 0xc8a4d5
});
createStall({
x: 0,
z: 7.4,
rotation: Math.PI,
name: "CITRUS LAB",
accent: "#c9983c",
productColor: 0xe0b94f
});
createStall({
x: 10,
z: 7.2,
rotation: Math.PI,
name: "NIGHT DESK",
accent: "#3d7b8a",
productColor: 0x71b7c4
});
// Route definitions are split into individual segments for live coverage.
const routeDefinitions = [
{ a: [-16, 0], b: [-10, 0], group: "promenade" },
{ a: [-10, 0], b: [-5, 0], group: "promenade" },
{ a: [-5, 0], b: [0, 0], group: "promenade" },
{ a: [0, 0], b: [5, 0], group: "promenade" },
{ a: [5, 0], b: [10, 0], group: "promenade" },
{ a: [10, 0], b: [16, 0], group: "promenade" },
{ a: [-10, 0], b: [-10, -4.7], group: "north" },
{ a: [0, 0], b: [0, -4.8], group: "north" },
{ a: [10, 0], b: [10, -4.7], group: "north" },
{ a: [-10, 0], b: [-10, 4.7], group: "south" },
{ a: [0, 0], b: [0, 4.8], group: "south" },
{ a: [10, 0], b: [10, 4.7], group: "south" }
];
for (const definition of routeDefinitions) {
const [ax, az] = definition.a;
const [bx, bz] = definition.b;
const dx = bx - ax;
const dz = bz - az;
const length = Math.hypot(dx, dz);
const material = new THREE.MeshBasicMaterial({
color: 0x294753,
transparent: true,
opacity: 0.35,
depthWrite: false
});
const segment = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.035, length + 0.03),
material
);
segment.position.set((ax + bx) / 2, 0.045, (az + bz) / 2);
segment.rotation.y = Math.atan2(dx, dz);
scene.add(segment);
routeSegments.push({
...definition,
mesh: segment,
material,
x: (ax + bx) / 2,
z: (az + bz) / 2,
covered: false
});
}
// Low-poly crates, planters and market props.
function createCrate(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
addBox(group, [1.15, 0.75, 0.95], [0, 0.38, 0], materials.crate);
addBox(group, [1.22, 0.08, 1.02], [0, 0.52, 0], materials.wood);
addBox(group, [1.22, 0.08, 1.02], [0, 0.2, 0], materials.wood);
}
createCrate(-17.2, -7.6, 0.14);
createCrate(-16.2, -7.4, -0.2);
createCrate(16.7, -6.7, 0.22);
createCrate(17.5, 6.8, -0.12);
createCrate(-17.5, 7.2, 0.18);
createCrate(5.5, -11.7, -0.18);
createCrate(-5.7, 11.7, 0.12);
function createPlanter(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
addCylinder(group, 0.48, 0.36, 0.65, [0, 0.33, 0], materials.pot, 7);
addCylinder(group, 0.07, 0.09, 1.2, [0, 1.0, 0], materials.wood, 6);
const foliageMaterial = materials.leaf;
for (let i = 0; i < 4; i++) {
const leaf = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.48 - i * 0.04, 0),
foliageMaterial
);
leaf.position.set(
Math.cos(i * 1.7) * 0.25,
1.45 + i * 0.16,
Math.sin(i * 1.7) * 0.25
);
leaf.castShadow = true;
group.add(leaf);
}
}
createPlanter(-18.2, -1.8);
createPlanter(18.2, 1.9);
createPlanter(-6.8, -12.2);
createPlanter(7.4, 12.4);
function createStreetSign(x, z, text, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
addCylinder(group, 0.055, 0.07, 3.2, [0, 1.6, 0], materials.metal, 7);
const material = new THREE.MeshBasicMaterial({
map: makeSignTexture(text, "#67d9c0"),
transparent: true,
side: THREE.DoubleSide
});
const board = new THREE.Mesh(new THREE.PlaneGeometry(2.4, 0.5), material);
board.position.set(0, 2.75, 0.04);
group.add(board);
}
createStreetSign(-14.8, -5.6, "NIGHT MARKET", 0.1);
createStreetSign(14.8, 5.8, "FOOD ARCADE", -0.12);
// Sparse stars / dust in the night sky.
const starPositions = [];
let randomSeed = 8117;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
for (let i = 0; i < 170; i++) {
starPositions.push(
(random() - 0.5) * 70,
9 + random() * 22,
(random() - 0.5) * 55
);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x9dbbd2,
size: 0.065,
transparent: true,
opacity: 0.45,
depthWrite: false
})
);
scene.add(stars);
// -------------------------------------------------------------------------
// Lamp placement nodes
// -------------------------------------------------------------------------
for (const [id, name, x, z] of NODE_DATA) {
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x26343c,
emissive: 0x162329,
emissiveIntensity: 0.2,
roughness: 0.7,
metalness: 0.35
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.39, 0.48, 0.12, 12),
baseMaterial
);
base.position.set(x, 0.06, z);
base.receiveShadow = true;
scene.add(base);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0x78949f,
transparent: true,
opacity: 0.48,
depthWrite: false,
side: THREE.DoubleSide
});
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.5, 0.57, 32),
ringMaterial
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(x, 0.135, z);
scene.add(ring);
const pick = new THREE.Mesh(
new THREE.SphereGeometry(0.65, 8, 6),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
pick.position.set(x, 0.42, z);
pick.userData.nodeId = id;
scene.add(pick);
pickTargets.push(pick);
const label = document.createElement("div");
label.className = "world-label node-label";
label.innerHTML = `<span class="pin"></span><b>${id}</b><em>PLACE</em>`;
labelLayer.appendChild(label);
const node = {
id,
name,
x,
z,
base,
baseMaterial,
ring,
ringMaterial,
label,
fixture: null,
pulseOffset: random() * Math.PI * 2
};
nodes.push(node);
nodeById.set(id, node);
}
// Service labels.
for (const service of serviceTargets) {
const label = document.createElement("div");
label.className = "world-label service-label";
label.innerHTML = `<span class="dot"></span><b>${service.name}</b><em>DIM</em>`;
labelLayer.appendChild(label);
service.label = label;
}
// -------------------------------------------------------------------------
// Fixture geometry, point lights and range visualization
// -------------------------------------------------------------------------
function disposeFixture(node) {
if (!node.fixture) return;
scene.remove(node.fixture);
node.fixture.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const objectMaterials = Array.isArray(object.material)
? object.material
: [object.material];
objectMaterials.forEach((material) => material.dispose());
}
});
node.fixture = null;
}
function createFixture(node, type) {
const group = new THREE.Group();
group.position.set(node.x, 0, node.z);
const poleMaterial = new THREE.MeshStandardMaterial({
color: 0x34444b,
roughness: 0.42,
metalness: 0.65
});
const headMaterial = new THREE.MeshStandardMaterial({
color: type.color,
emissive: type.color,
emissiveIntensity: 2.7,
roughness: 0.28,
metalness: 0.15
});
let lightHeight = 1.55;
if (type.id === "bollard") {
addCylinder(group, 0.09, 0.12, 1.5, [0, 0.83, 0], poleMaterial, 7);
addBox(group, [0.42, 0.22, 0.42], [0, 1.58, 0], headMaterial);
lightHeight = 1.48;
} else if (type.id === "pendant") {
addCylinder(group, 0.07, 0.1, 3.35, [0, 1.75, 0], poleMaterial, 7);
addBox(group, [0.85, 0.07, 0.07], [0.3, 3.39, 0], poleMaterial);
const shade = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.48, 0.42, 8),
poleMaterial
);
shade.position.set(0.68, 3.19, 0);
shade.castShadow = true;
group.add(shade);
addCylinder(
group,
0.29,
0.29,
0.06,
[0.68, 2.96, 0],
headMaterial,
12
);
lightHeight = 2.94;
} else {
addCylinder(group, 0.08, 0.13, 3.85, [0, 1.98, 0], poleMaterial, 7);
addBox(group, [0.9, 0.08, 0.08], [0.3, 3.85, 0], poleMaterial);
const head = addBox(
group,
[0.88, 0.42, 0.35],
[0.65, 3.66, 0],
headMaterial
);
head.rotation.z = -0.28;
lightHeight = 3.56;
}
const light = new THREE.PointLight(
type.color,
25 + type.watt * 0.56,
type.range,
2
);
light.position.set(0, lightHeight, 0);
group.add(light);
const pool = new THREE.Mesh(
new THREE.CircleGeometry(type.range, 48),
new THREE.MeshBasicMaterial({
color: type.color,
transparent: true,
opacity: 0.032,
depthWrite: false,
blending: THREE.AdditiveBlending,
side: THREE.DoubleSide
})
);
pool.rotation.x = -Math.PI / 2;
pool.position.y = 0.026;
group.add(pool);
const rangeRing = new THREE.Mesh(
new THREE.RingGeometry(type.range - 0.025, type.range, 64),
new THREE.MeshBasicMaterial({
color: type.color,
transparent: true,
opacity: 0.14,
depthWrite: false,
side: THREE.DoubleSide
})
);
rangeRing.rotation.x = -Math.PI / 2;
rangeRing.position.y = 0.032;
group.add(rangeRing);
group.userData.light = light;
group.userData.baseIntensity = light.intensity;
node.fixture = group;
scene.add(group);
}
function assignedTotal() {
let total = 0;
for (const typeId of assignments.values()) {
total += LAMP_TYPES[typeId].watt;
}
return total;
}
function setAssignment(nodeId, typeId, announce = false) {
const node = nodeById.get(nodeId);
const type = LAMP_TYPES[typeId];
const previousType = assignments.get(nodeId);
const previousWatt = previousType ? LAMP_TYPES[previousType].watt : 0;
const nextTotal = assignedTotal() - previousWatt + type.watt;
if (nextTotal > BUDGET) {
showToast(
`Budget exceeded. ${type.name} needs ${type.watt} W; only ${Math.max(
0,
BUDGET - (assignedTotal() - previousWatt)
)} W remains.`,
true
);
return false;
}
disposeFixture(node);
assignments.set(nodeId, typeId);
createFixture(node, type);
if (announce) {
showToast(`${type.name} assigned at ${node.name} · ${type.watt} W`);
}
updateCoverage();
updateInterface();
return true;
}
function removeAssignment(nodeId, announce = true) {
const node = nodeById.get(nodeId);
if (!assignments.has(nodeId)) return;
const typeId = assignments.get(nodeId);
disposeFixture(node);
assignments.delete(nodeId);
updateCoverage();
updateInterface();
if (announce) {
showToast(
`${LAMP_TYPES[typeId].name} removed from ${node.name}`
);
}
}
function clearPlan(announce = true) {
for (const node of nodes) disposeFixture(node);
assignments.clear();
updateCoverage();
updateInterface();
if (announce) showToast("Lighting plan cleared");
}
// -------------------------------------------------------------------------
// Coverage model and visual feedback
// -------------------------------------------------------------------------
function positionIsCovered(x, z, rangeFactor = 0.9) {
for (const [nodeId, typeId] of assignments.entries()) {
const node = nodeById.get(nodeId);
const type = LAMP_TYPES[typeId];
const distance = Math.hypot(node.x - x, node.z - z);
if (distance <= type.range * rangeFactor) return true;
}
return false;
}
function updateCoverage() {
let litServices = 0;
for (const service of serviceTargets) {
service.covered = positionIsCovered(service.x, service.z, 0.9);
if (service.covered) {
litServices++;
service.surface.material.color.setHex(0xd79b58);
service.surface.material.emissive.setHex(0xff8d46);
service.surface.material.emissiveIntensity = 0.72;
service.floorRing.material.color.setHex(0x64dfc4);
service.floorRing.material.opacity = 0.14;
service.label.classList.add("lit");
service.label.querySelector("em").textContent = "LIT";
} else {
service.surface.material.color.setHex(0x503126);
service.surface.material.emissive.setHex(0x6a2b18);
service.surface.material.emissiveIntensity = 0.16;
service.floorRing.material.color.setHex(0x4c6670);
service.floorRing.material.opacity = 0.045;
service.label.classList.remove("lit");
service.label.querySelector("em").textContent = "DIM";
}
}
const groups = {
promenade: { covered: 0, total: 0 },
north: { covered: 0, total: 0 },
south: { covered: 0, total: 0 }
};
for (const segment of routeSegments) {
segment.covered = positionIsCovered(segment.x, segment.z, 0.84);
groups[segment.group].total++;
if (segment.covered) {
groups[segment.group].covered++;
segment.material.color.setHex(0x5ce1c7);
segment.material.opacity = 0.8;
} else {
segment.material.color.setHex(0x294753);
segment.material.opacity = 0.35;
}
}
const servicePercent = (litServices / serviceTargets.length) * 100;
setCoverage("service", litServices, serviceTargets.length, servicePercent);
for (const group of ["promenade", "north", "south"]) {
const data = groups[group];
setCoverage(
group,
data.covered,
data.total,
(data.covered / data.total) * 100
);
}
}
function setCoverage(group, covered, total, percent) {
const prefix = group === "service" ? "coverageService" : `coverage${group[0].toUpperCase()}${group.slice(1)}`;
const valueElement = document.getElementById(`${prefix}`);
const barElement = document.getElementById(`${prefix}Bar`);
if (!valueElement || !barElement) return;
valueElement.textContent =
group === "service" ? `${covered} / ${total}` : `${Math.round(percent)}%`;
barElement.style.width = `${percent}%`;
}
function updateNodeVisuals() {
const time = performance.now() * 0.001;
for (const node of nodes) {
const typeId = assignments.get(node.id);
const type = typeId ? LAMP_TYPES[typeId] : null;
const isHovered = node === hoveredNode;
let color = 0x78949f;
let opacity = 0.44;
if (type) {
color = type.color;
opacity = 0.82;
} else if (selectedLamp) {
color = 0xff7b4b;
opacity = 0.54;
}
node.ringMaterial.color.setHex(color);
node.ringMaterial.opacity = opacity;
const pulse =
!type && selectedLamp
? 1 + Math.sin(time * 2.2 + node.pulseOffset) * 0.055
: 1;
const hoverScale = isHovered ? 1.22 : 1;
node.ring.scale.setScalar(pulse * hoverScale);
node.label.classList.toggle("assigned", Boolean(type));
node.label.querySelector("em").textContent = type
? `${type.watt} W`
: "PLACE";
}
}
// -------------------------------------------------------------------------
// Interface
// -------------------------------------------------------------------------
function renderLampOptions() {
const container = document.getElementById("lampOptions");
container.innerHTML = "";
for (const type of Object.values(LAMP_TYPES)) {
const button = document.createElement("button");
button.type = "button";
button.className = `lamp-card${type.id === selectedLamp ? " active" : ""}`;
button.dataset.lamp = type.id;
button.setAttribute("aria-pressed", String(type.id === selectedLamp));
button.style.setProperty("--fixture-color", type.colorCSS);
button.innerHTML = `
<span class="fixture-icon ${type.id}" aria-hidden="true"></span>
<span class="lamp-copy">
<strong>${type.name}</strong>
<small>${type.description}</small>
</span>
<span class="lamp-spec">
<b>${type.watt} W</b>
<small>${type.range.toFixed(1)}m reach</small>
</span>
`;
button.addEventListener("click", () => {
selectedLamp = type.id;
renderLampOptions();
updateNodeVisuals();
});
container.appendChild(button);
}
}
function updateInterface() {
const total = assignedTotal();
const remaining = BUDGET - total;
const percent = (total / BUDGET) * 100;
document.getElementById("budgetAssigned").textContent = total.toLocaleString();
document.getElementById("counterAssigned").textContent = total.toLocaleString();
document.getElementById("budgetRemaining").textContent = `${remaining.toLocaleString()} W free`;
document.getElementById("budgetFill").style.width = `${percent}%`;
document.getElementById("counterFill").style.width = `${percent}%`;
document.getElementById("fixtureCount").textContent = assignments.size;
document.getElementById("scenarioPower").textContent = `${total.toLocaleString()} W`;
const rows = document.getElementById("counterRows");
if (assignments.size === 0) {
rows.innerHTML = `
<div class="empty-counter">
No fixtures assigned.<br />
Select a fixture and click a market node.
</div>
`;
return;
}
rows.innerHTML = "";
for (const [nodeId, typeId] of assignments.entries()) {
const node = nodeById.get(nodeId);
const type = LAMP_TYPES[typeId];
const row = document.createElement("div");
row.className = "counter-row";
row.dataset.node = nodeId;
row.style.setProperty("--fixture-color", type.colorCSS);
row.innerHTML = `
<div class="row-main">
<div class="row-location">${node.id} · ${node.name}</div>
<div class="row-type"><i></i>${type.name}</div>
</div>
<div class="row-watt">${type.watt} W</div>
<button class="row-remove" type="button" data-remove="${nodeId}" aria-label="Remove ${type.name} at ${node.name}">×</button>
`;
rows.appendChild(row);
}
}
function showToast(message, isError = false) {
clearTimeout(toastTimer);
toastElement.textContent = message;
toastElement.classList.toggle("error", isError);
toastElement.classList.add("show");
toastTimer = window.setTimeout(() => {
toastElement.classList.remove("show");
}, 2700);
}
document.getElementById("counterRows").addEventListener("click", (event) => {
const removeButton = event.target.closest("[data-remove]");
if (removeButton) {
event.stopPropagation();
removeAssignment(removeButton.dataset.remove);
return;
}
const row = event.target.closest(".counter-row");
if (row) focusNode(row.dataset.node);
});
document.getElementById("resetPlan").addEventListener("click", () => clearPlan());
document.getElementById("clearPlan").addEventListener("click", () => clearPlan());
document.getElementById("resetView").addEventListener("click", () => {
controls.target.set(0, 0.3, 0);
controls.yaw = 0.58;
controls.pitch = 0.72;
controls.radius = 32;
});
document.getElementById("toggleKit").addEventListener("click", () => {
document.body.classList.toggle("kit-open");
document.body.classList.remove("counter-open");
});
document.getElementById("toggleCounter").addEventListener("click", () => {
document.body.classList.toggle("counter-open");
document.body.classList.remove("kit-open");
});
function focusNode(nodeId) {
const node = nodeById.get(nodeId);
if (!node) return;
controls.target.set(node.x, 0.4, node.z);
controls.radius = 21;
controls.pitch = 0.78;
}
// -------------------------------------------------------------------------
// Direct camera controls, picking and placement interaction
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const drag = {
active: false,
dragging: false,
pointerId: null,
button: 0,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0
};
function nodeAt(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
camera.updateMatrixWorld();
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickTargets, false);
return intersections.length
? nodeById.get(intersections[0].object.userData.nodeId)
: null;
}
function setHovered(node, event = null) {
if (hoveredNode === node) {
if (node && event) positionTooltip(event.clientX, event.clientY);
return;
}
hoveredNode = node;
if (!node) {
tooltip.style.display = "none";
canvas.classList.remove("node-hover");
return;
}
canvas.classList.add("node-hover");
const typeId = assignments.get(node.id);
const type = typeId ? LAMP_TYPES[typeId] : null;
tooltip.innerHTML = `
<strong>${node.id} · ${node.name}</strong>
<span>${
type
? `${type.name} · ${type.watt} W assigned`
: "Unassigned · click to place selected fixture"
}</span>
`;
tooltip.style.display = "block";
if (event) positionTooltip(event.clientX, event.clientY);
}
function positionTooltip(x, y) {
const width = tooltip.offsetWidth || 160;
const height = tooltip.offsetHeight || 55;
tooltip.style.left = `${Math.min(x + 14, window.innerWidth - width - 10)}px`;
tooltip.style.top = `${Math.min(y + 14, window.innerHeight - height - 10)}px`;
}
canvas.addEventListener("pointerdown", (event) => {
drag.active = true;
drag.dragging = false;
drag.pointerId = event.pointerId;
drag.button = event.button;
drag.startX = event.clientX;
drag.startY = event.clientY;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (drag.active && event.pointerId === drag.pointerId) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
const totalMovement = Math.hypot(
event.clientX - drag.startX,
event.clientY - drag.startY
);
if (totalMovement > 5) drag.dragging = true;
if (drag.dragging) {
if (drag.button === 0 || event.pointerType === "touch") {
controls.yaw -= dx * 0.006;
controls.pitch = THREE.MathUtils.clamp(
controls.pitch + dy * 0.0045,
0.28,
1.22
);
} else if (drag.button === 2) {
panCamera(dx, dy);
}
}
drag.lastX = event.clientX;
drag.lastY = event.clientY;
return;
}
setHovered(nodeAt(event.clientX, event.clientY), event);
});
function finishPointer(event, cancelled = false) {
if (!drag.active || event.pointerId !== drag.pointerId) return;
const wasDragging = drag.dragging;
const button = drag.button;
drag.active = false;
drag.dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (!cancelled && !wasDragging && button === 0) {
const node = nodeAt(event.clientX, event.clientY);
if (node) {
if (event.shiftKey) {
removeAssignment(node.id);
} else {
setAssignment(node.id, selectedLamp, true);
}
}
}
}
canvas.addEventListener("pointerup", (event) => finishPointer(event));
canvas.addEventListener("pointercancel", (event) => finishPointer(event, true));
canvas.addEventListener("pointerleave", () => {
if (!drag.active) setHovered(null);
});
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
function panCamera(dx, dy) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3().crossVectors(forward, camera.up).normalize();
const panScale = controls.radius * 0.0014;
controls.target.addScaledVector(right, -dx * panScale);
controls.target.addScaledVector(forward, -dy * panScale);
controls.target.x = THREE.MathUtils.clamp(controls.target.x, -20, 20);
controls.target.z = THREE.MathUtils.clamp(controls.target.z, -14, 14);
}
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius + event.deltaY * 0.018,
14,
47
);
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.key === "1") selectedLamp = "bollard";
if (event.key === "2") selectedLamp = "pendant";
if (event.key === "3") selectedLamp = "flood";
if (["1", "2", "3"].includes(event.key)) {
renderLampOptions();
updateNodeVisuals();
}
if ((event.key === "Delete" || event.key === "Backspace") && hoveredNode) {
removeAssignment(hoveredNode.id);
}
if (event.key.toLowerCase() === "f" && hoveredNode) {
focusNode(hoveredNode.id);
}
});
// -------------------------------------------------------------------------
// World-space label projection
// -------------------------------------------------------------------------
const projectionVector = new THREE.Vector3();
function placeWorldLabel(element, x, y, z, baseOpacity = 1) {
projectionVector.set(x, y, z).project(camera);
const visible = projectionVector.z > -1 && projectionVector.z < 1;
const screenX = (projectionVector.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-projectionVector.y * 0.5 + 0.5) * window.innerHeight;
element.style.opacity = visible ? baseOpacity : 0;
element.style.transform = `translate3d(${screenX}px, ${screenY}px, 0) translate(-50%, -50%)`;
}
function updateWorldLabels() {
for (const node of nodes) {
placeWorldLabel(node.label, node.x, 0.48, node.z, node.fixture ? 1 : 0.68);
}
for (const service of serviceTargets) {
placeWorldLabel(service.label, service.x, 1.75, service.z, 0.88);
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
updateCamera();
updateNodeVisuals();
for (const node of nodes) {
if (!node.fixture) continue;
const light = node.fixture.userData.light;
const baseIntensity = node.fixture.userData.baseIntensity;
light.intensity =
baseIntensity *
(1 + Math.sin(elapsed * 1.7 + node.pulseOffset) * 0.025);
}
stars.rotation.y = Math.sin(elapsed * 0.025) * 0.018;
updateWorldLabels();
renderer.render(scene, camera);
});
// Editable starter plan.
setAssignment("W2", "pendant");
setAssignment("N2", "bollard");
renderLampOptions();
updateCoverage();
updateInterface();
updateNodeVisuals();
</script>
</body>
</html>
```