Prompt specification
Build or customize the Three.js 3D scene "Power-limited evening workspace" 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: Power-limited evening workspace
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/power-limited-evening-workspace-x0196
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows desk-lamp illumination. Build a room with several task lights and a fictional energy budget; users activate combinations that illuminate selected work areas while a labeled illustrative meter tracks demand. Low-poly.
Working Three.js Starter (power-limited-evening-workspace-x0196):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lumen Lab — Task-Light Energy Studio</title>
<style>
:root {
color-scheme: dark;
--bg: #111c22;
--panel: rgba(20, 32, 39, 0.91);
--panel-solid: #18262e;
--line: rgba(255, 255, 255, 0.11);
--text: #eef4f3;
--muted: #9eb0b4;
--mint: #8fe0c1;
--amber: #f3bd6b;
--red: #ff817a;
--blue: #8ebbe5;
}
* {
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;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
min-width: 126px;
padding: 7px 10px 8px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 9px;
background: rgba(13, 23, 29, 0.88);
box-shadow: 0 7px 24px rgba(0, 0, 0, 0.25);
color: var(--text);
text-align: left;
pointer-events: auto;
cursor: pointer;
transform: translate(-50%, -50%);
backdrop-filter: blur(10px);
transition:
border-color 160ms ease,
background 160ms ease,
opacity 160ms ease;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 18px;
background: rgba(255, 255, 255, 0.25);
}
.scene-label.selected {
border-color: var(--zone-color, var(--mint));
background: rgba(17, 30, 35, 0.95);
}
.scene-label.on {
box-shadow:
0 0 0 1px color-mix(in srgb, var(--zone-color) 38%, transparent),
0 7px 24px rgba(0, 0, 0, 0.28);
}
.scene-label strong {
display: block;
margin-bottom: 2px;
font-size: 11px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.scene-label span {
display: block;
color: var(--muted);
font-size: 10px;
}
.topbar {
position: fixed;
z-index: 10;
top: 0;
left: 0;
right: 0;
min-height: 84px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 18px 24px;
pointer-events: none;
background: linear-gradient(
to bottom,
rgba(11, 20, 25, 0.93),
rgba(11, 20, 25, 0.68) 68%,
transparent
);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
flex: 0 0 auto;
border: 1px solid rgba(143, 224, 193, 0.48);
border-radius: 50%;
}
.brand-mark::before {
content: "";
position: absolute;
inset: 8px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 18px rgba(143, 224, 193, 0.65);
}
.brand-mark::after {
content: "";
position: absolute;
width: 1px;
height: 12px;
top: -7px;
left: 18px;
background: var(--mint);
}
.eyebrow {
margin: 0 0 3px;
color: var(--mint);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(18px, 2.1vw, 27px);
font-weight: 650;
letter-spacing: -0.025em;
}
.top-status {
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(19, 31, 38, 0.72);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(12px);
}
.top-status::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--mint);
box-shadow: 0 0 9px rgba(143, 224, 193, 0.75);
}
.top-status.over::before {
background: var(--red);
box-shadow: 0 0 9px rgba(255, 129, 122, 0.75);
}
.panel {
position: fixed;
z-index: 8;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
}
.controls {
top: 98px;
left: 24px;
width: 322px;
max-height: calc(100vh - 124px);
padding: 17px;
overflow: auto;
scrollbar-width: thin;
scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
}
.panel-heading {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
margin-bottom: 14px;
}
.panel-heading h2,
.meter h2 {
margin: 0;
font-size: 14px;
font-weight: 700;
letter-spacing: 0.01em;
}
.panel-heading p,
.meter-intro {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.clear-button {
flex: 0 0 auto;
padding: 6px 8px;
border: 1px solid var(--line);
border-radius: 7px;
background: transparent;
color: var(--muted);
font-size: 10px;
cursor: pointer;
}
.clear-button:hover {
color: var(--text);
border-color: rgba(255, 255, 255, 0.25);
}
.section-label {
margin: 16px 0 8px;
color: #b9c6c8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.zone-list {
display: grid;
gap: 7px;
}
.zone-card {
display: grid;
grid-template-columns: minmax(0, 1fr) 38px;
gap: 6px;
padding: 5px;
border: 1px solid transparent;
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
transition:
border-color 150ms ease,
background 150ms ease;
}
.zone-card.selected {
border-color: color-mix(in srgb, var(--zone-color) 56%, transparent);
background: color-mix(in srgb, var(--zone-color) 10%, transparent);
}
.zone-main,
.power-button {
border: 0;
cursor: pointer;
}
.zone-main {
display: grid;
grid-template-columns: 9px minmax(0, 1fr) auto;
align-items: center;
gap: 9px;
min-width: 0;
padding: 5px 6px;
border-radius: 8px;
background: transparent;
text-align: left;
}
.zone-main:hover {
background: rgba(255, 255, 255, 0.04);
}
.zone-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--zone-color);
opacity: 0.38;
box-shadow: 0 0 0 3px color-mix(in srgb, var(--zone-color) 9%, transparent);
}
.zone-card.on .zone-dot {
opacity: 1;
box-shadow: 0 0 10px var(--zone-color);
}
.zone-copy {
min-width: 0;
}
.zone-copy strong {
display: block;
overflow: hidden;
font-size: 12px;
font-weight: 650;
text-overflow: ellipsis;
white-space: nowrap;
}
.zone-copy small {
display: block;
margin-top: 3px;
overflow: hidden;
color: var(--muted);
font-size: 9px;
text-overflow: ellipsis;
white-space: nowrap;
}
.zone-state {
color: #77898e;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
}
.zone-card.on .zone-state {
color: var(--zone-color);
}
.power-button {
align-self: stretch;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
color: #71858a;
font-size: 14px;
}
.power-button:hover {
color: var(--text);
border-color: rgba(255, 255, 255, 0.24);
}
.zone-card.on .power-button {
border-color: color-mix(in srgb, var(--zone-color) 45%, transparent);
background: color-mix(in srgb, var(--zone-color) 13%, transparent);
color: var(--zone-color);
}
.preset-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.preset-button {
min-height: 58px;
padding: 9px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
text-align: left;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease;
}
.preset-button:hover {
border-color: rgba(255, 255, 255, 0.24);
background: rgba(255, 255, 255, 0.065);
}
.preset-button.active {
border-color: rgba(143, 224, 193, 0.55);
background: rgba(143, 224, 193, 0.09);
}
.preset-button strong {
display: block;
font-size: 10px;
font-weight: 700;
}
.preset-button span {
display: block;
margin-top: 6px;
color: var(--muted);
font-size: 9px;
}
.preset-button .safe {
color: var(--mint);
}
.preset-button .over {
color: var(--red);
}
.camera-tip {
display: flex;
align-items: center;
gap: 8px;
margin-top: 14px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
line-height: 1.4;
}
.mouse-icon {
position: relative;
width: 13px;
height: 18px;
flex: 0 0 auto;
border: 1px solid #75888d;
border-radius: 7px;
}
.mouse-icon::before {
content: "";
position: absolute;
top: 3px;
left: 5px;
width: 1px;
height: 4px;
background: #75888d;
}
.meter {
top: 98px;
right: 24px;
width: 310px;
padding: 17px;
}
.meter-title-row {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.illustrative-tag {
padding: 5px 7px;
border: 1px solid rgba(243, 189, 107, 0.34);
border-radius: 6px;
color: var(--amber);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.meter-value {
display: flex;
align-items: baseline;
gap: 7px;
margin: 19px 0 2px;
}
.meter-value strong {
font-size: 35px;
font-weight: 620;
letter-spacing: -0.04em;
}
.meter-value span {
color: var(--muted);
font-size: 11px;
}
.budget-copy {
color: var(--muted);
font-size: 10px;
}
.meter-track {
position: relative;
height: 12px;
margin: 17px 0 8px;
overflow: visible;
border-radius: 99px;
background:
linear-gradient(90deg, transparent 49.5%, rgba(255, 255, 255, 0.12) 50%, transparent 50.5%),
rgba(255, 255, 255, 0.075);
}
.meter-fill {
position: absolute;
inset: 0 auto 0 0;
width: 0;
border-radius: inherit;
background: var(--mint);
box-shadow: 0 0 14px rgba(143, 224, 193, 0.3);
transition:
width 350ms cubic-bezier(0.22, 1, 0.36, 1),
background 180ms ease;
}
.meter-fill.warning {
background: var(--amber);
box-shadow: 0 0 14px rgba(243, 189, 107, 0.3);
}
.meter-fill.over {
background: var(--red);
box-shadow: 0 0 14px rgba(255, 129, 122, 0.35);
}
.budget-marker {
position: absolute;
z-index: 2;
top: -5px;
bottom: -5px;
left: 63.333%;
width: 2px;
border-radius: 2px;
background: #e9edf0;
box-shadow: 0 0 0 3px rgba(233, 237, 240, 0.08);
}
.budget-marker::before {
content: "";
position: absolute;
left: 50%;
top: -3px;
width: 6px;
height: 6px;
border-radius: 50%;
background: #e9edf0;
transform: translate(-50%, -50%);
}
.meter-scale {
display: flex;
justify-content: space-between;
color: #74878c;
font-size: 8px;
}
.budget-status {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 16px;
padding: 10px 11px;
border: 1px solid rgba(143, 224, 193, 0.2);
border-radius: 10px;
background: rgba(143, 224, 193, 0.055);
}
.budget-status.over {
border-color: rgba(255, 129, 122, 0.25);
background: rgba(255, 129, 122, 0.07);
}
.budget-status.warning {
border-color: rgba(243, 189, 107, 0.25);
background: rgba(243, 189, 107, 0.07);
}
.budget-status strong {
display: block;
color: var(--mint);
font-size: 11px;
}
.budget-status.over strong {
color: var(--red);
}
.budget-status.warning strong {
color: var(--amber);
}
.budget-status small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.remaining {
color: var(--text);
font-size: 11px;
white-space: nowrap;
}
.load-breakdown {
display: flex;
flex-wrap: wrap;
gap: 6px;
min-height: 24px;
margin-top: 11px;
}
.load-chip {
padding: 5px 7px;
border-radius: 6px;
background: rgba(255, 255, 255, 0.055);
color: var(--muted);
font-size: 9px;
}
.load-chip strong {
color: var(--text);
font-weight: 650;
}
.selected-card {
margin-top: 13px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.selected-card .section-label {
margin-top: 0;
}
.selected-card h3 {
margin: 0 0 5px;
font-size: 12px;
font-weight: 650;
}
.selected-card p {
margin: 0;
color: var(--muted);
font-size: 9px;
line-height: 1.55;
}
.disclaimer {
margin: 12px 0 0;
color: #71858a;
font-size: 8px;
line-height: 1.45;
}
@media (max-width: 850px) {
.controls {
top: auto;
bottom: 14px;
left: 14px;
width: min(322px, calc(100vw - 28px));
max-height: 48vh;
}
.meter {
top: 88px;
right: 14px;
width: min(285px, calc(100vw - 28px));
}
.topbar {
padding: 15px 17px;
}
}
@media (max-width: 560px) {
.top-status {
display: none;
}
.meter {
top: 80px;
right: 10px;
width: 225px;
padding: 13px;
}
.meter .selected-card,
.meter .load-breakdown,
.meter .disclaimer,
.meter-intro {
display: none;
}
.meter-value {
margin-top: 13px;
}
.meter-value strong {
font-size: 28px;
}
.controls {
left: 10px;
bottom: 10px;
width: calc(100vw - 20px);
max-height: 43vh;
padding: 13px;
}
.camera-tip {
display: none;
}
.scene-label {
min-width: 104px;
padding: 6px 8px;
}
.scene-label strong {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<p class="eyebrow">Lumen Lab / 01</p>
<h1>Task-light energy studio</h1>
</div>
</div>
<div class="top-status" id="topStatus">
4 zones · fictional 1-hour model
</div>
</header>
<aside class="panel controls" aria-label="Lighting controls">
<div class="panel-heading">
<div>
<h2>Work-area lighting</h2>
<p>Select an area, then power one or more task lights.</p>
</div>
<button class="clear-button" id="clearButton" type="button">Clear</button>
</div>
<div class="section-label">Task lights</div>
<div class="zone-list" id="zoneList"></div>
<div class="section-label">Combination presets</div>
<div class="preset-grid" id="presetGrid">
<button class="preset-button" type="button" data-preset="focus">
<strong>Focused draft</strong>
<span class="safe">1 lamp · 18 Wh</span>
</button>
<button class="preset-button" type="button" data-preset="precision">
<strong>Precision pair</strong>
<span class="safe">2 lamps · 21 Wh</span>
</button>
<button class="preset-button" type="button" data-preset="review">
<strong>Evening review</strong>
<span class="safe">2 lamps · 25 Wh</span>
</button>
<button class="preset-button" type="button" data-preset="balanced">
<strong>Balanced set</strong>
<span class="safe">2 lamps · 27 Wh</span>
</button>
<button class="preset-button" type="button" data-preset="handoff">
<strong>Full handoff</strong>
<span class="over">4 lamps · 55 Wh</span>
</button>
</div>
<div class="camera-tip">
<span class="mouse-icon" aria-hidden="true"></span>
<span>Drag to orbit · Shift-drag to pan · Scroll to zoom · Double-click to reset</span>
</div>
</aside>
<aside class="panel meter" aria-label="Illustrative energy meter">
<div class="meter-title-row">
<div>
<h2>Energy meter</h2>
<p class="meter-intro">Fictional demand for one modeled hour.</p>
</div>
<span class="illustrative-tag">Illustrative</span>
</div>
<div class="meter-value" aria-live="polite">
<strong id="demandValue">0 Wh</strong>
<span id="budgetValue">/ 38 Wh budget</span>
</div>
<div class="budget-copy">Modeled load from active task lights</div>
<div class="meter-track" aria-hidden="true">
<div class="meter-fill" id="meterFill"></div>
<div class="budget-marker" title="38 Wh budget"></div>
</div>
<div class="meter-scale">
<span>0</span>
<span>Budget line: 38</span>
<span>60 Wh</span>
</div>
<div class="budget-status" id="budgetStatus">
<div>
<strong id="statusTitle">Standby</strong>
<small id="statusDetail">No task lights are active</small>
</div>
<div class="remaining" id="remainingValue">38 Wh left</div>
</div>
<div class="load-breakdown" id="loadBreakdown"></div>
<div class="selected-card">
<div class="section-label">Selected work area</div>
<h3 id="selectedName">Drafting bay</h3>
<p id="selectedDescription"></p>
</div>
<p class="disclaimer">
Energy values and budget are fictional teaching values, not utility readings,
appliance specifications, or a billing calculation.
</p>
</aside>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Data
// ------------------------------------------------------------
const BUDGET = 38;
const METER_MAX = 60;
const zones = [
{
id: "draft",
name: "Drafting bay",
lampId: 0,
color: "#f3bd6b",
description:
"A warm focused pool for sketch development and close paper work.",
anchor: [-4.2, 2.25, -2.65]
},
{
id: "assembly",
name: "Assembly bench",
lampId: 1,
color: "#8ebbe5",
description:
"Cool directional light for small parts, tools, and visual inspection.",
anchor: [3.45, 2.35, -2.55]
},
{
id: "plan",
name: "Plan table",
lampId: 2,
color: "#a8d9b0",
description:
"A low-glare task beam centered on plans and a compact monitor.",
anchor: [-3.45, 2.25, 3.45]
},
{
id: "reading",
name: "Reading nook",
lampId: 3,
color: "#c6a7e8",
description:
"A softer floor-level pool for books, notes, and end-of-day review.",
anchor: [5.2, 2.45, 3.75]
}
];
const lamps = [
{
id: 0,
zoneId: "draft",
name: "Draft lamp",
load: 18,
color: 0xffd28a,
base: [-5.65, 1.59, -3.85],
joint: [-5.65, 2.13, -3.85],
shade: [-4.9, 2.78, -3.3],
target: [-4.15, 1.59, -2.65],
angle: 0.49,
pool: [1.7, 1.15],
kind: "desk"
},
{
id: 1,
zoneId: "assembly",
name: "Precision lamp",
load: 12,
color: 0xc5e4ff,
base: [5.25, 1.59, -3.65],
joint: [5.25, 2.18, -3.65],
shade: [4.62, 2.83, -3.22],
target: [3.45, 1.59, -2.52],
angle: 0.45,
pool: [1.55, 1.0],
kind: "desk"
},
{
id: 2,
zoneId: "plan",
name: "Monitor task bar",
load: 9,
color: 0xb8f0c5,
base: [-4.95, 1.59, 4.05],
joint: [-4.95, 2.1, 4.05],
shade: [-4.3, 2.68, 3.76],
target: [-3.4, 1.59, 3.46],
angle: 0.48,
pool: [1.65, 1.05],
kind: "desk"
},
{
id: 3,
zoneId: "reading",
name: "Reading task lamp",
load: 16,
color: 0xd7b6ff,
base: [7.0, 0.08, 4.0],
joint: [7.0, 1.62, 4.0],
shade: [6.25, 2.67, 3.83],
target: [5.15, 0.06, 3.7],
angle: 0.52,
pool: [1.6, 1.15],
kind: "floor"
}
];
const presets = {
focus: [0],
precision: [1, 2],
review: [2, 3],
balanced: [0, 2],
handoff: [0, 1, 2, 3]
};
let selectedZone = "draft";
let activePreset = null;
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const labelHost = document.getElementById("labels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x17242b);
scene.fog = new THREE.Fog(0x17242b, 25, 43);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly room showing four task lights"
);
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
theta: 0.72,
phi: 1.01,
radius: 23,
target: new THREE.Vector3(0, 1.0, 0)
};
const hemi = new THREE.HemisphereLight(0xbdd4e8, 0x4a382e, 1.45);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe5bd, 1.65);
sun.position.set(7, 14, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(1536, 1536);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0006;
scene.add(sun);
const coolFill = new THREE.DirectionalLight(0x8fb8dc, 0.55);
coolFill.position.set(-9, 7, -3);
scene.add(coolFill);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.82,
metalness: options.metalness ?? 0,
flatShading: true,
side: options.side ?? THREE.FrontSide,
transparent: options.transparent ?? false,
opacity: options.opacity ?? 1,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
const mats = {
floor: material(0x9a8066),
floorDark: material(0x725c49),
wall: material(0xd5cbb9),
wallSide: material(0xbeb5a8),
trim: material(0xeee6d8),
wood: material(0xb18359),
woodLight: material(0xd3a16e),
woodDark: material(0x644838),
metal: material(0x394a50, { metalness: 0.5, roughness: 0.55 }),
darkMetal: material(0x233138, { metalness: 0.55, roughness: 0.5 }),
paper: material(0xf2ead7),
bluePaper: material(0x93b7cb),
seat: material(0x5e7778),
rug: material(0x60777b),
green: material(0x5d8963),
greenDark: material(0x3c6348),
cream: material(0xe7d7ba),
red: material(0xb35e54),
screen: material(0x17323f, {
emissive: 0x29596d,
emissiveIntensity: 0.45
})
};
function addBox(parent, size, position, mat, rotation = null) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
if (rotation) mesh.rotation.set(rotation[0], rotation[1], rotation[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
mat,
segments = 8
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(parent, a, b, radius, mat, segments = 7) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const delta = end.clone().sub(start);
const midpoint = start.clone().add(end).multiplyScalar(0.5);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, delta.length(), segments),
mat
);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
delta.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Room shell
// ------------------------------------------------------------
addBox(scene, [17, 0.3, 12.2], [0, -0.18, 0], mats.floor);
addBox(scene, [17, 5.2, 0.25], [0, 2.45, -6.12], mats.wall);
addBox(scene, [0.25, 5.2, 12.2], [-8.62, 2.45, 0], mats.wallSide);
addBox(scene, [17.1, 0.16, 0.16], [0, 0.05, -5.94], mats.trim);
addBox(scene, [0.16, 0.16, 12.2], [-8.44, 0.05, 0], mats.trim);
// Floor seams.
for (let x = -8; x <= 8; x += 2) {
addBox(scene, [0.025, 0.012, 12], [x, -0.022, 0], mats.floorDark);
}
for (let z = -5; z <= 5; z += 2) {
addBox(scene, [17, 0.012, 0.025], [0, -0.021, z], mats.floorDark);
}
// Back window.
addBox(scene, [4.4, 2.25, 0.08], [-2.9, 3.1, -5.94], mats.darkMetal);
addBox(scene, [4.08, 1.94, 0.1], [-2.9, 3.1, -5.86], material(0x789eb0));
addBox(scene, [0.11, 2.05, 0.13], [-2.9, 3.1, -5.78], mats.trim);
addBox(scene, [4.15, 0.11, 0.13], [-2.9, 3.1, -5.78], mats.trim);
addBox(scene, [4.55, 0.12, 0.42], [-2.9, 1.92, -5.75], mats.trim);
// Wall panels and geometric art.
addBox(scene, [2.25, 1.35, 0.08], [3.2, 3.1, -5.92], mats.darkMetal);
addBox(scene, [1.85, 0.95, 0.1], [3.2, 3.1, -5.84], material(0xd0a05d));
addBox(scene, [0.58, 0.58, 0.12], [2.88, 2.83, -5.75], mats.cream);
addBox(scene, [0.26, 1.25, 0.12], [3.7, 3.22, -5.73], mats.bluePaper);
// Left-wall notice board.
addBox(scene, [0.08, 1.8, 2.6], [-8.44, 2.9, -1.6], mats.woodDark);
for (let i = 0; i < 3; i++) {
addBox(
scene,
[0.1, 0.65, 0.55],
[-8.37, 2.55 + i * 0.38, -2.25 + i * 0.72],
i === 1 ? mats.bluePaper : mats.paper
);
}
// Storage cabinet.
const cabinet = new THREE.Group();
scene.add(cabinet);
addBox(cabinet, [2.2, 2.2, 0.9], [6.3, 1.1, -5.35], mats.wood);
addBox(cabinet, [2.32, 0.14, 1.0], [6.3, 2.25, -5.35], mats.woodLight);
addBox(cabinet, [0.04, 1.9, 0.05], [6.3, 1.15, -4.87], mats.woodDark);
addCylinder(cabinet, 0.06, 0.06, 0.18, [6.0, 1.35, -4.82], mats.metal);
addCylinder(cabinet, 0.06, 0.06, 0.18, [6.6, 1.35, -4.82], mats.metal);
// ------------------------------------------------------------
// Furniture helpers
// ------------------------------------------------------------
function makeTable(width, depth, topY = 1.48) {
const group = new THREE.Group();
addBox(
group,
[width, 0.18, depth],
[0, topY, 0],
mats.woodLight
);
addBox(
group,
[width - 0.15, 0.12, depth - 0.15],
[0, topY - 0.14, 0],
mats.woodDark
);
const legX = width / 2 - 0.22;
const legZ = depth / 2 - 0.22;
for (const x of [-legX, legX]) {
for (const z of [-legZ, legZ]) {
addBox(group, [0.15, topY - 0.18, 0.15], [x, (topY - 0.18) / 2, z], mats.metal);
}
}
return group;
}
function makeChair() {
const group = new THREE.Group();
addBox(group, [0.9, 0.16, 0.85], [0, 0.78, 0], mats.seat);
addBox(group, [0.9, 0.9, 0.14], [0, 1.25, -0.38], mats.seat);
for (const x of [-0.34, 0.34]) {
for (const z of [-0.3, 0.3]) {
addBox(group, [0.1, 0.75, 0.1], [x, 0.38, z], mats.darkMetal);
}
}
return group;
}
function markArea(group, zoneId) {
group.traverse((child) => {
if (child.isMesh) child.userData.zoneId = zoneId;
});
}
const areaGroups = [];
const outlines = new Map();
function addZoneRug(zoneId, x, z, width, depth, color) {
const rug = addBox(
scene,
[width, 0.035, depth],
[x, 0.005, z],
material(color, { roughness: 1 })
);
rug.castShadow = false;
const edgeGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(width, 0.045, depth)
);
const outline = new THREE.LineSegments(
edgeGeometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.18
})
);
outline.position.set(x, 0.04, z);
scene.add(outline);
outlines.set(zoneId, outline);
}
// Drafting bay.
addZoneRug("draft", -4.2, -2.65, 4.65, 3.65, 0xc59a58);
const draftTable = makeTable(3.8, 2.05);
draftTable.position.set(-4.2, 0, -2.75);
scene.add(draftTable);
markArea(draftTable, "draft");
const draftingSheet = addBox(
draftTable,
[2.15, 0.035, 1.22],
[0.22, 1.59, 0.08],
mats.bluePaper,
[0, 0.08, 0]
);
draftingSheet.castShadow = false;
for (let i = 0; i < 4; i++) {
addBox(
draftTable,
[1.2 - i * 0.15, 0.012, 0.025],
[0.15, 1.615, -0.27 + i * 0.16],
mats.paper
);
}
const draftChair = makeChair();
draftChair.position.set(-4.25, 0, -1.15);
draftChair.rotation.y = Math.PI;
scene.add(draftChair);
markArea(draftChair, "draft");
// Assembly bench.
addZoneRug("assembly", 3.45, -2.6, 5.1, 3.6, 0x648ca0);
const assemblyTable = makeTable(4.25, 2.15);
assemblyTable.position.set(3.45, 0, -2.68);
scene.add(assemblyTable);
markArea(assemblyTable, "assembly");
addBox(assemblyTable, [1.05, 0.32, 0.78], [-0.9, 1.75, 0], mats.red);
addBox(assemblyTable, [0.66, 0.55, 0.66], [0.1, 1.84, -0.15], mats.metal);
addCylinder(assemblyTable, 0.36, 0.36, 0.16, [1.2, 1.67, 0.16], mats.greenDark);
addCylinder(assemblyTable, 0.19, 0.19, 0.18, [1.2, 1.69, 0.16], mats.green);
addBox(assemblyTable, [0.9, 0.06, 0.15], [0.3, 1.6, 0.65], mats.darkMetal);
const assemblyChair = makeChair();
assemblyChair.position.set(3.5, 0, -0.93);
assemblyChair.rotation.y = Math.PI;
scene.add(assemblyChair);
markArea(assemblyChair, "assembly");
// Plan table.
addZoneRug("plan", -3.45, 3.55, 5.1, 3.6, 0x769276);
const planTable = makeTable(4.0, 1.95);
planTable.position.set(-3.45, 0, 3.55);
scene.add(planTable);
markArea(planTable, "plan");
addBox(planTable, [1.62, 0.035, 0.92], [-0.55, 1.59, 0.1], mats.paper);
for (let i = 0; i < 5; i++) {
addBox(
planTable,
[0.85, 0.012, 0.025],
[-0.55, 1.615, -0.2 + i * 0.14],
mats.bluePaper
);
}
addBox(planTable, [0.12, 0.47, 0.12], [0.9, 1.73, -0.4], mats.darkMetal);
addBox(planTable, [1.28, 0.78, 0.13], [0.9, 2.15, -0.4], mats.darkMetal);
addBox(planTable, [1.12, 0.62, 0.03], [0.9, 2.15, -0.32], mats.screen);
addBox(planTable, [0.7, 0.035, 0.23], [0.9, 1.6, 0.05], mats.darkMetal);
const planChair = makeChair();
planChair.position.set(-3.4, 0, 5.08);
scene.add(planChair);
markArea(planChair, "plan");
// Reading nook.
addZoneRug("reading", 5.1, 3.7, 4.25, 3.7, 0x806c8f);
const readingTable = makeTable(2.35, 1.45, 1.15);
readingTable.position.set(5.05, 0, 3.8);
scene.add(readingTable);
markArea(readingTable, "reading");
addBox(readingTable, [0.7, 0.08, 0.48], [-0.3, 1.28, 0], mats.red);
addBox(readingTable, [0.66, 0.06, 0.46], [-0.28, 1.34, 0], mats.paper);
addBox(readingTable, [0.58, 0.06, 0.43], [-0.25, 1.39, 0.01], mats.bluePaper);
addCylinder(readingTable, 0.13, 0.11, 0.22, [0.62, 1.33, 0.15], mats.cream);
const readingChair = makeChair();
readingChair.position.set(5.15, 0, 5.12);
scene.add(readingChair);
markArea(readingChair, "reading");
// Low-poly plant.
const plant = new THREE.Group();
plant.position.set(7.45, 0, 1.3);
scene.add(plant);
addCylinder(plant, 0.38, 0.27, 0.55, [0, 0.28, 0], mats.red, 7);
cylinderBetween(plant, [0, 0.5, 0], [-0.08, 1.25, 0.04], 0.055, mats.woodDark);
cylinderBetween(plant, [-0.08, 1.25, 0.04], [0.4, 1.7, 0], 0.045, mats.woodDark);
const leafGeometry = new THREE.ConeGeometry(0.28, 0.72, 5);
[
[-0.2, 1.35, 0, 0.2],
[0.2, 1.48, 0.02, -0.4],
[0.45, 1.8, 0, 0.25],
[0.05, 1.88, -0.05, -0.25]
].forEach(([x, y, z, rz]) => {
const leaf = new THREE.Mesh(leafGeometry, mats.green);
leaf.position.set(x, y, z);
leaf.rotation.z = rz;
leaf.castShadow = true;
plant.add(leaf);
});
// Storage blocks and decorative books.
const bookColors = [0xc36f59, 0xd7b76b, 0x668b9a, 0x77936d];
for (let i = 0; i < 4; i++) {
addBox(
scene,
[0.42, 0.58 + i * 0.04, 0.72],
[4.8 + i * 0.47, 0.3 + (0.58 + i * 0.04) / 2, 5.2],
material(bookColors[i])
);
}
// ------------------------------------------------------------
// Task lights and light pools
// ------------------------------------------------------------
function makePoolTexture() {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const context = canvas.getContext("2d");
const gradient = context.createRadialGradient(64, 64, 2, 64, 64, 64);
gradient.addColorStop(0, "rgba(255,255,255,0.95)");
gradient.addColorStop(0.35, "rgba(255,255,255,0.52)");
gradient.addColorStop(1, "rgba(255,255,255,0)");
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const poolTexture = makePoolTexture();
const lampGroups = [];
function makeTaskLamp(data) {
const group = new THREE.Group();
group.userData.lampId = data.id;
scene.add(group);
const baseRadius = data.kind === "floor" ? 0.48 : 0.34;
const baseHeight = data.kind === "floor" ? 0.12 : 0.1;
addCylinder(
group,
baseRadius,
baseRadius * 1.08,
baseHeight,
data.base,
mats.darkMetal,
10
);
const baseTop = [
data.base[0],
data.base[1] + baseHeight / 2,
data.base[2]
];
cylinderBetween(group, baseTop, data.joint, 0.055, mats.metal);
cylinderBetween(group, data.joint, data.shade, 0.05, mats.metal);
addCylinder(
group,
0.09,
0.09,
0.15,
data.joint,
mats.red,
8
);
const shadeMaterial = material(data.color, {
roughness: 0.72,
side: THREE.DoubleSide
});
const shade = new THREE.Mesh(
new THREE.ConeGeometry(0.39, 0.48, 10, 1, true),
shadeMaterial
);
shade.position.set(...data.shade);
shade.castShadow = true;
shade.receiveShadow = true;
const targetVector = new THREE.Vector3(...data.target);
const shadeVector = new THREE.Vector3(...data.shade);
const awayFromTarget = shadeVector.clone().sub(targetVector).normalize();
shade.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
awayFromTarget
);
group.add(shade);
const lightPosition = shadeVector
.clone()
.add(targetVector.clone().sub(shadeVector).normalize().multiplyScalar(0.18));
const bulbMaterial = material(0xfff1ca, {
emissive: data.color,
emissiveIntensity: 0,
roughness: 0.4
});
const bulb = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.095, 0),
bulbMaterial
);
bulb.position.copy(lightPosition);
group.add(bulb);
const spot = new THREE.SpotLight(
data.color,
0,
9,
data.angle,
0.58,
1.35
);
spot.position.copy(lightPosition);
spot.castShadow = true;
spot.shadow.mapSize.set(512, 512);
spot.shadow.camera.near = 0.15;
spot.shadow.camera.far = 9;
spot.shadow.bias = -0.0012;
const targetObject = new THREE.Object3D();
targetObject.position.set(...data.target);
scene.add(targetObject);
spot.target = targetObject;
scene.add(spot);
const fill = new THREE.PointLight(data.color, 0, 3.1, 1.4);
fill.position.copy(targetVector).add(new THREE.Vector3(0, 0.32, 0));
scene.add(fill);
const pool = new THREE.Mesh(
new THREE.CircleGeometry(1, 32),
new THREE.MeshBasicMaterial({
map: poolTexture,
color: data.color,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.AdditiveBlending,
side: THREE.DoubleSide
})
);
pool.position.set(...data.target);
pool.rotation.x = -Math.PI / 2;
pool.scale.set(data.pool[0], data.pool[1], 1);
pool.renderOrder = 2;
scene.add(pool);
const beamLength = lightPosition.distanceTo(targetVector);
const beamRadius = Math.tan(data.angle) * beamLength * 0.78;
const beam = new THREE.Mesh(
new THREE.ConeGeometry(beamRadius, beamLength, 10, 1, true),
new THREE.MeshBasicMaterial({
color: data.color,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending
})
);
beam.position.copy(
lightPosition.clone().add(targetVector).multiplyScalar(0.5)
);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
lightPosition.clone().sub(targetVector).normalize()
);
beam.renderOrder = 1;
scene.add(beam);
const lamp = {
...data,
group,
spot,
fill,
bulbMaterial,
pool,
beam,
on: false,
power: 0
};
group.traverse((child) => {
if (child.isMesh) child.userData.lampId = data.id;
});
lampGroups.push(group);
return lamp;
}
const lampObjects = lamps.map(makeTaskLamp);
const lampById = new Map(lampObjects.map((lamp) => [lamp.id, lamp]));
// ------------------------------------------------------------
// UI construction
// ------------------------------------------------------------
const zoneList = document.getElementById("zoneList");
const labels = new Map();
zones.forEach((zone) => {
const lamp = lampById.get(zone.lampId);
const card = document.createElement("div");
card.className = "zone-card";
card.dataset.zone = zone.id;
card.style.setProperty("--zone-color", zone.color);
card.innerHTML = `
<button class="zone-main" type="button" aria-label="Select ${zone.name}">
<span class="zone-dot"></span>
<span class="zone-copy">
<strong>${zone.name}</strong>
<small>${lamp.name} · ${lamp.load} Wh / modeled hour</small>
</span>
<span class="zone-state">OFF</span>
</button>
<button
class="power-button"
type="button"
aria-label="Toggle ${lamp.name}"
aria-pressed="false"
title="Toggle ${lamp.name}"
>○</button>
`;
zoneList.appendChild(card);
card.querySelector(".zone-main").addEventListener("click", () => {
selectZone(zone.id);
});
card.querySelector(".power-button").addEventListener("click", () => {
toggleLamp(zone.lampId);
});
const sceneLabel = document.createElement("button");
sceneLabel.className = "scene-label";
sceneLabel.type = "button";
sceneLabel.style.setProperty("--zone-color", zone.color);
sceneLabel.setAttribute("aria-label", `Toggle ${zone.name} task light`);
sceneLabel.innerHTML = `
<strong>${zone.name}</strong>
<span>${lamp.load} Wh · off</span>
`;
sceneLabel.addEventListener("click", () => {
toggleLamp(zone.lampId);
});
labelHost.appendChild(sceneLabel);
labels.set(zone.id, sceneLabel);
});
const demandValue = document.getElementById("demandValue");
const budgetValue = document.getElementById("budgetValue");
const meterFill = document.getElementById("meterFill");
const budgetStatus = document.getElementById("budgetStatus");
const statusTitle = document.getElementById("statusTitle");
const statusDetail = document.getElementById("statusDetail");
const remainingValue = document.getElementById("remainingValue");
const loadBreakdown = document.getElementById("loadBreakdown");
const selectedName = document.getElementById("selectedName");
const selectedDescription = document.getElementById("selectedDescription");
const topStatus = document.getElementById("topStatus");
function selectZone(zoneId) {
selectedZone = zoneId;
updateUI();
}
function toggleLamp(lampId) {
const lamp = lampById.get(lampId);
lamp.on = !lamp.on;
selectedZone = lamp.zoneId;
activePreset = null;
updateUI();
}
function applyPreset(presetId) {
const enabled = new Set(presets[presetId]);
lampObjects.forEach((lamp) => {
lamp.on = enabled.has(lamp.id);
});
activePreset = presetId;
const firstEnabled = lampObjects.find((lamp) => lamp.on);
if (firstEnabled) selectedZone = firstEnabled.zoneId;
updateUI();
}
function updateUI() {
let demand = 0;
zones.forEach((zone) => {
const lamp = lampById.get(zone.lampId);
const card = zoneList.querySelector(`[data-zone="${zone.id}"]`);
const label = labels.get(zone.id);
card.classList.toggle("selected", selectedZone === zone.id);
card.classList.toggle("on", lamp.on);
card.querySelector(".zone-state").textContent = lamp.on ? "ON" : "OFF";
card.querySelector(".power-button").textContent = lamp.on ? "●" : "○";
card.querySelector(".power-button").setAttribute(
"aria-pressed",
String(lamp.on)
);
label.classList.toggle("selected", selectedZone === zone.id);
label.classList.toggle("on", lamp.on);
label.querySelector("span").textContent =
`${lamp.load} Wh · ${lamp.on ? "on" : "off"}`;
if (lamp.on) demand += lamp.load;
});
const activeIds = lampObjects
.filter((lamp) => lamp.on)
.map((lamp) => lamp.id)
.sort((a, b) => a - b);
document.querySelectorAll(".preset-button").forEach((button) => {
const presetIds = [...presets[button.dataset.preset]].sort((a, b) => a - b);
const matches =
presetIds.length === activeIds.length &&
presetIds.every((id, index) => id === activeIds[index]);
button.classList.toggle("active", matches);
});
const remaining = BUDGET - demand;
const percentage = Math.min((demand / METER_MAX) * 100, 100);
demandValue.textContent = `${demand} Wh`;
budgetValue.textContent = `/ ${BUDGET} Wh budget`;
meterFill.style.width = `${percentage}%`;
meterFill.classList.remove("warning", "over");
budgetStatus.classList.remove("over", "warning");
topStatus.classList.remove("over");
if (demand === 0) {
statusTitle.textContent = "Standby";
statusDetail.textContent = "No task lights are active";
remainingValue.textContent = `${BUDGET} Wh left`;
} else if (demand > BUDGET) {
meterFill.classList.add("over");
budgetStatus.classList.add("over");
topStatus.classList.add("over");
statusTitle.textContent = "Over illustrative budget";
statusDetail.textContent = `${demand - BUDGET} Wh above the modeled limit`;
remainingValue.textContent = `${Math.abs(remaining)} Wh over`;
topStatus.textContent = "Over budget · fictional 1-hour model";
} else if (remaining <= 6) {
meterFill.classList.add("warning");
budgetStatus.classList.add("warning");
statusTitle.textContent = "Near illustrative limit";
statusDetail.textContent = "Only a small modeled allowance remains";
remainingValue.textContent = `${remaining} Wh left`;
topStatus.textContent = "Near limit · fictional 1-hour model";
} else {
statusTitle.textContent = "Within illustrative budget";
statusDetail.textContent = "The modeled combination stays below budget";
remainingValue.textContent = `${remaining} Wh left`;
topStatus.textContent = "Within budget · fictional 1-hour model";
}
loadBreakdown.innerHTML = lampObjects
.filter((lamp) => lamp.on)
.map(
(lamp) =>
`<span class="load-chip"><strong>${lamp.load} Wh</strong> ${lamp.name}</span>`
)
.join("");
const zone = zones.find((item) => item.id === selectedZone);
selectedName.textContent = zone.name;
selectedDescription.textContent = zone.description;
zones.forEach((item) => {
const outline = outlines.get(item.id);
if (!outline) return;
const isSelected = item.id === selectedZone;
const isOn = lampById.get(item.lampId).on;
outline.material.opacity = isSelected ? 0.95 : isOn ? 0.52 : 0.12;
outline.material.color.set(item.color);
});
}
document.getElementById("clearButton").addEventListener("click", () => {
lampObjects.forEach((lamp) => {
lamp.on = false;
});
activePreset = null;
updateUI();
});
document.querySelectorAll(".preset-button").forEach((button) => {
button.addEventListener("click", () => {
applyPreset(button.dataset.preset);
});
});
updateUI();
// ------------------------------------------------------------
// Custom camera controls and picking
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerState = {
down: false,
moved: false,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0
};
function updateCamera() {
const offset = new THREE.Vector3().setFromSphericalCoords(
orbit.radius,
orbit.phi,
orbit.theta
);
camera.position.copy(orbit.target).add(offset);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
function pickAt(clientX, clientY) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(
[...lampGroups, ...areaGroups],
true
);
if (!hits.length) return;
const hit = hits[0].object;
if (typeof hit.userData.lampId === "number") {
toggleLamp(hit.userData.lampId);
} else if (hit.userData.zoneId) {
selectZone(hit.userData.zoneId);
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerState.down = true;
pointerState.moved = false;
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerState.down) return;
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
if (
Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY
) > 5
) {
pointerState.moved = true;
}
if (event.shiftKey || event.buttons === 2) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrix,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const panScale = orbit.radius * 0.0011;
orbit.target.addScaledVector(right, -dx * panScale);
orbit.target.addScaledVector(up, dy * panScale);
orbit.target.x = THREE.MathUtils.clamp(orbit.target.x, -3.2, 3.2);
orbit.target.y = THREE.MathUtils.clamp(orbit.target.y, 0.4, 2.0);
orbit.target.z = THREE.MathUtils.clamp(orbit.target.z, -2.4, 2.4);
} else {
orbit.theta = THREE.MathUtils.clamp(
orbit.theta - dx * 0.005,
0.12,
1.35
);
orbit.phi = THREE.MathUtils.clamp(
orbit.phi + dy * 0.004,
0.64,
1.25
);
}
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (pointerState.down && !pointerState.moved) {
pickAt(event.clientX, event.clientY);
}
pointerState.down = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerState.down = false;
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
15,
30
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.theta = 0.72;
orbit.phi = 1.01;
orbit.radius = 23;
orbit.target.set(0, 1, 0);
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.75));
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
zones.forEach((zone) => {
const label = labels.get(zone.id);
projected.set(...zone.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
label.style.display = visible ? "block" : "none";
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.style.left = `${x}px`;
label.style.top = `${y}px`;
});
}
function animate() {
updateCamera();
lampObjects.forEach((lamp) => {
const desired = lamp.on ? 1 : 0;
lamp.power += (desired - lamp.power) * 0.12;
lamp.spot.intensity = 118 * lamp.power;
lamp.fill.intensity = 7 * lamp.power;
lamp.bulbMaterial.emissiveIntensity = 2.8 * lamp.power;
lamp.pool.material.opacity = 0.58 * lamp.power;
lamp.beam.material.opacity = 0.055 * lamp.power;
lamp.pool.visible = lamp.power > 0.005;
lamp.beam.visible = lamp.power > 0.005;
});
updateLabels();
renderer.render(scene, camera);
}
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```