Prompt specification
Build or customize the Three.js 3D scene "Shade-design fashion exhibit" 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: Shade-design fashion exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/shade-design-fashion-exhibit-x0592
Scene Description Prompt:
Create a standalone voxel scene in one HTML file using Three.js from cdnjs. Create adjustable brim and canopy models around simple figures; users inspect projected shadows at several light angles and compare coverage through a purely geometric display.
Working Three.js Starter (shade-design-fashion-exhibit-x0592):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Shade Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #080d12;
--panel: #10171f;
--panel-2: #151f29;
--line: #26333f;
--text: #edf3f7;
--muted: #8d9aa7;
--accent: #f4c35b;
--cyan: #56d7c9;
--pink: #ef789c;
--lime: #b7dc68;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app {
height: 100vh;
display: grid;
grid-template-rows: 64px minmax(0, 1fr);
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 22px;
border-bottom: 1px solid var(--line);
background: #0c1218;
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 240px;
}
.brand-mark {
width: 30px;
height: 30px;
position: relative;
transform: rotate(-8deg);
}
.brand-mark i {
position: absolute;
display: block;
background: var(--accent);
border-radius: 2px;
}
.brand-mark i:nth-child(1) {
width: 15px;
height: 15px;
left: 0;
top: 12px;
}
.brand-mark i:nth-child(2) {
width: 13px;
height: 13px;
left: 12px;
top: 2px;
}
.brand-mark i:nth-child(3) {
width: 8px;
height: 8px;
left: 23px;
top: 16px;
background: var(--cyan);
}
.brand h1 {
font-size: 15px;
letter-spacing: 0.02em;
margin: 0;
}
.brand p {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.16em;
margin: 3px 0 0;
text-transform: uppercase;
}
.top-meta {
display: flex;
align-items: center;
justify-content: center;
gap: 9px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
background: var(--cyan);
border-radius: 50%;
box-shadow: 0 0 14px rgba(86, 215, 201, 0.7);
}
.button {
border: 1px solid var(--line);
background: var(--panel-2);
border-radius: 7px;
padding: 9px 12px;
cursor: pointer;
font-size: 11px;
letter-spacing: 0.04em;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
.button:hover {
background: #1c2834;
border-color: #40505f;
}
.button:active {
transform: translateY(1px);
}
.button.primary {
color: #15130b;
border-color: var(--accent);
background: var(--accent);
font-weight: 750;
}
.button.primary:hover {
background: #ffd477;
}
.button.active {
color: var(--accent);
border-color: rgba(244, 195, 91, 0.55);
background: rgba(244, 195, 91, 0.1);
}
main {
min-height: 0;
display: grid;
grid-template-columns: 296px minmax(0, 1fr);
}
.sidebar {
min-height: 0;
overflow-y: auto;
padding: 20px;
border-right: 1px solid var(--line);
background: var(--panel);
scrollbar-width: thin;
scrollbar-color: #33414d transparent;
}
.eyebrow {
color: var(--accent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.sidebar h2 {
margin: 7px 0 5px;
font-size: 21px;
letter-spacing: -0.025em;
}
.intro {
color: var(--muted);
font-size: 12px;
line-height: 1.55;
margin: 0 0 20px;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 22px 0 10px;
color: #c7d0d8;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-title span:last-child {
color: #63717e;
font-weight: 500;
letter-spacing: 0.04em;
text-transform: none;
}
.specimens {
display: grid;
gap: 7px;
}
.specimen {
width: 100%;
display: grid;
grid-template-columns: 13px 1fr auto;
align-items: center;
gap: 10px;
padding: 11px;
text-align: left;
border: 1px solid var(--line);
border-radius: 8px;
background: #121a22;
cursor: pointer;
transition: 150ms ease;
}
.specimen:hover {
border-color: #40505f;
}
.specimen.active {
border-color: rgba(244, 195, 91, 0.65);
background: rgba(244, 195, 91, 0.075);
}
.specimen .swatch {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--swatch);
box-shadow: 0 0 10px color-mix(in srgb, var(--swatch) 45%, transparent);
}
.specimen b {
display: block;
font-size: 12px;
margin-bottom: 3px;
}
.specimen small {
display: block;
color: var(--muted);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.specimen .index {
color: #5f6c78;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.control {
padding: 13px 0 10px;
border-bottom: 1px solid rgba(38, 51, 63, 0.7);
}
.control:last-of-type {
border-bottom: 0;
}
.control-head {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
}
.control label {
font-size: 12px;
font-weight: 650;
}
.value {
color: var(--accent);
font-size: 11px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
--fill: 40%;
width: 100%;
height: 4px;
margin: 5px 0 3px;
appearance: none;
border-radius: 4px;
background:
linear-gradient(
to right,
var(--accent) 0 var(--fill),
#2a3743 var(--fill) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
appearance: none;
border: 3px solid #111820;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
}
input[type="range"]::-moz-range-thumb {
width: 10px;
height: 10px;
border: 3px solid #111820;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 1px var(--accent);
}
.range-scale {
display: flex;
justify-content: space-between;
color: #596773;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.note {
margin-top: 18px;
padding: 12px;
border-left: 2px solid var(--cyan);
background: rgba(86, 215, 201, 0.055);
color: #9eabb5;
font-size: 10px;
line-height: 1.55;
}
.note strong {
color: #c9d5dc;
}
.sidebar-actions {
display: flex;
gap: 8px;
margin-top: 16px;
}
.sidebar-actions .button {
flex: 1;
}
.workspace {
min-width: 0;
min-height: 0;
display: grid;
grid-template-rows: minmax(380px, 1fr) 262px;
}
.scene-card {
position: relative;
min-height: 0;
overflow: hidden;
background: #0b1117;
}
#viewport {
position: absolute;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.scene-top {
position: absolute;
top: 16px;
left: 18px;
right: 18px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
pointer-events: none;
z-index: 3;
}
.scene-title {
padding: 10px 12px;
border: 1px solid rgba(73, 88, 101, 0.48);
border-radius: 8px;
background: rgba(11, 17, 23, 0.78);
backdrop-filter: blur(9px);
}
.scene-title strong {
display: block;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scene-title span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
}
.view-buttons {
display: flex;
gap: 6px;
pointer-events: auto;
}
.view-buttons .button {
background: rgba(17, 25, 33, 0.86);
backdrop-filter: blur(9px);
}
.light-dock {
position: absolute;
left: 18px;
bottom: 18px;
z-index: 3;
padding: 9px;
border: 1px solid rgba(73, 88, 101, 0.55);
border-radius: 9px;
background: rgba(11, 17, 23, 0.83);
backdrop-filter: blur(10px);
}
.light-dock > span {
display: block;
margin: 1px 4px 8px;
color: #778692;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.light-options {
display: flex;
gap: 5px;
}
.light-options button {
border: 1px solid #2c3a46;
border-radius: 6px;
background: #121b24;
padding: 8px 10px;
cursor: pointer;
color: #aab5be;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.light-options button.active {
color: var(--accent);
border-color: rgba(244, 195, 91, 0.65);
background: rgba(244, 195, 91, 0.1);
}
.scene-help {
position: absolute;
right: 18px;
bottom: 18px;
color: #778692;
font-size: 9px;
letter-spacing: 0.04em;
text-align: right;
line-height: 1.6;
pointer-events: none;
}
#labels {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
z-index: 2;
}
.figure-label {
position: absolute;
left: 0;
top: 0;
white-space: nowrap;
padding: 5px 7px;
border: 1px solid rgba(104, 120, 134, 0.45);
border-radius: 5px;
background: rgba(10, 15, 20, 0.76);
color: #aeb9c2;
font-size: 9px;
letter-spacing: 0.05em;
transform: translate(-50%, -100%);
transition:
color 150ms ease,
border-color 150ms ease;
}
.figure-label.active {
color: #fff1c8;
border-color: rgba(244, 195, 91, 0.62);
}
.analysis {
min-width: 0;
padding: 16px 18px 18px;
border-top: 1px solid var(--line);
background: #0d141b;
overflow: hidden;
}
.analysis-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 12px;
}
.analysis-heading h2 {
margin: 0;
font-size: 13px;
letter-spacing: 0.02em;
}
.analysis-heading p {
margin: 4px 0 0;
color: var(--muted);
font-size: 10px;
}
.legend {
display: flex;
gap: 12px;
color: #8c99a5;
font-size: 9px;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 7px;
height: 7px;
border-radius: 2px;
}
.coverage-grid {
height: calc(100% - 39px);
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 11px;
}
.coverage-card {
min-width: 0;
border: 1px solid var(--line);
border-radius: 9px;
background: #111a22;
overflow: hidden;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease;
}
.coverage-card:hover,
.coverage-card.active {
border-color: rgba(244, 195, 91, 0.58);
background: #141e27;
}
.coverage-header {
height: 38px;
padding: 0 10px;
display: flex;
align-items: center;
justify-content: space-between;
border-bottom: 1px solid var(--line);
}
.coverage-header strong {
display: block;
font-size: 10px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.coverage-header small {
display: block;
margin-top: 2px;
color: #6e7c88;
font-size: 8px;
font-variant-numeric: tabular-nums;
}
.coverage-body {
height: calc(100% - 38px);
display: grid;
grid-template-columns: minmax(0, 1fr) 94px;
}
.coverage-card canvas {
width: 100%;
height: 100%;
display: block;
min-height: 145px;
}
.coverage-stat {
display: flex;
flex-direction: column;
justify-content: center;
padding: 10px;
border-left: 1px solid var(--line);
background: rgba(7, 12, 17, 0.22);
}
.coverage-stat strong {
color: var(--accent);
font-size: 23px;
letter-spacing: -0.05em;
font-variant-numeric: tabular-nums;
}
.coverage-stat span {
margin-top: 3px;
color: #84919c;
font-size: 9px;
line-height: 1.3;
}
.coverage-stat small {
margin-top: 12px;
color: #c4cdd4;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
@media (max-width: 1000px) {
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100vh;
}
main {
grid-template-columns: 250px minmax(0, 1fr);
}
.coverage-body {
grid-template-columns: 1fr;
}
.coverage-stat {
display: none;
}
}
@media (max-width: 760px) {
#app {
grid-template-rows: 60px auto;
}
.top-meta {
display: none;
}
main {
display: block;
}
.sidebar {
border-right: 0;
border-bottom: 1px solid var(--line);
}
.workspace {
grid-template-rows: 520px auto;
}
.coverage-grid {
grid-template-columns: 1fr;
}
.coverage-card {
min-height: 205px;
}
.coverage-body {
grid-template-columns: minmax(0, 1fr) 94px;
}
.coverage-stat {
display: flex;
}
.scene-help {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i>
</div>
<div>
<h1>Voxel Shade Lab</h1>
<p>Geometric daylight study</p>
</div>
</div>
<div class="top-meta">
<span class="status-dot"></span>
<span>Three fixed solar vectors</span>
</div>
<button class="button" id="sweepButton">Cycle light angles</button>
</header>
<main>
<aside class="sidebar">
<div class="eyebrow">Specimen editor</div>
<h2>Shape the canopy</h2>
<p class="intro">
Adjust one voxel figure, then inspect its projected footprint under
three directional light vectors.
</p>
<div class="section-title">
<span>Figures</span>
<span>Select to edit</span>
</div>
<div class="specimens">
<button class="specimen active" data-specimen="0">
<span class="swatch" style="--swatch:#56d7c9"></span>
<span>
<b>Specimen A</b>
<small id="specimenDetail0">—</small>
</span>
<span class="index">01</span>
</button>
<button class="specimen" data-specimen="1">
<span class="swatch" style="--swatch:#ef789c"></span>
<span>
<b>Specimen B</b>
<small id="specimenDetail1">—</small>
</span>
<span class="index">02</span>
</button>
<button class="specimen" data-specimen="2">
<span class="swatch" style="--swatch:#b7dc68"></span>
<span>
<b>Specimen C</b>
<small id="specimenDetail2">—</small>
</span>
<span class="index">03</span>
</button>
</div>
<div class="section-title">
<span>Selected geometry</span>
<span id="selectedName">Specimen A</span>
</div>
<div class="control">
<div class="control-head">
<label for="brimRange">Brim diameter</label>
<span class="value" id="brimValue">1.45 m</span>
</div>
<input id="brimRange" type="range" min="1.05" max="2.45" step="0.05" />
<div class="range-scale"><span>1.05 m</span><span>2.45 m</span></div>
</div>
<div class="control">
<div class="control-head">
<label for="canopyRange">Canopy span</label>
<span class="value" id="canopyValue">1.30 m</span>
</div>
<input id="canopyRange" type="range" min="0.95" max="2.10" step="0.05" />
<div class="range-scale"><span>0.95 m</span><span>2.10 m</span></div>
</div>
<div class="control">
<div class="control-head">
<label for="heightRange">Canopy height</label>
<span class="value" id="heightValue">0.62 m</span>
</div>
<input id="heightRange" type="range" min="0.35" max="0.95" step="0.05" />
<div class="range-scale"><span>0.35 m</span><span>0.95 m</span></div>
</div>
<div class="note">
<strong>How to read the study:</strong><br />
The lower maps are direct geometric projections. Polygons are projected
from the figure and canopy envelopes onto the ground plane, then sampled
to calculate coverage.
</div>
<div class="sidebar-actions">
<button class="button" id="resetModel">Reset model</button>
<button class="button" id="resetAll">Reset all</button>
</div>
</aside>
<section class="workspace">
<div class="scene-card">
<div id="viewport"></div>
<div id="labels"></div>
<div class="scene-top">
<div class="scene-title">
<strong>Perspective viewport</strong>
<span id="activeLightLabel">Northwest light · 32° elevation</span>
</div>
<div class="view-buttons">
<button class="button" id="topView">Top</button>
<button class="button" id="resetView">Reset view</button>
</div>
</div>
<div class="light-dock">
<span>Sun vector</span>
<div class="light-options">
<button data-light="0" class="active">NW 32°</button>
<button data-light="1">NE 64°</button>
<button data-light="2">SE 24°</button>
</div>
</div>
<div class="scene-help">
Drag to orbit · Scroll to zoom<br />
Click a figure to select it
</div>
</div>
<section class="analysis">
<div class="analysis-heading">
<div>
<h2>Ground coverage comparison</h2>
<p>Parallel projection · common 10.4 × 5.4 m study area</p>
</div>
<div class="legend">
<span><i style="background:#56d7c9"></i>A</span>
<span><i style="background:#ef789c"></i>B</span>
<span><i style="background:#b7dc68"></i>C</span>
</div>
</div>
<div class="coverage-grid">
<article class="coverage-card active" data-light="0" tabindex="0">
<div class="coverage-header">
<div>
<strong>Northwest</strong>
<small>Azimuth −50° · elevation 32°</small>
</div>
</div>
<div class="coverage-body">
<canvas id="coverage0"></canvas>
<div class="coverage-stat">
<strong id="coveragePercent0">—</strong>
<span>study area covered</span>
<small id="coverageArea0">—</small>
</div>
</div>
</article>
<article class="coverage-card" data-light="1" tabindex="0">
<div class="coverage-header">
<div>
<strong>Northeast</strong>
<small>Azimuth 45° · elevation 64°</small>
</div>
</div>
<div class="coverage-body">
<canvas id="coverage1"></canvas>
<div class="coverage-stat">
<strong id="coveragePercent1">—</strong>
<span>study area covered</span>
<small id="coverageArea1">—</small>
</div>
</div>
</article>
<article class="coverage-card" data-light="2" tabindex="0">
<div class="coverage-header">
<div>
<strong>Southeast</strong>
<small>Azimuth 135° · elevation 24°</small>
</div>
</div>
<div class="coverage-body">
<canvas id="coverage2"></canvas>
<div class="coverage-stat">
<strong id="coveragePercent2">—</strong>
<span>study area covered</span>
<small id="coverageArea2">—</small>
</div>
</div>
</article>
</div>
</section>
</section>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.querySelector("#viewport");
const labelsElement = document.querySelector("#labels");
const HAT_Y = 2.53;
const HAT_BOTTOM = HAT_Y + 0.08;
const BODY_TOP = 2.48;
const figures = [
{
id: "A",
name: "Specimen A",
color: 0x56d7c9,
css: "#56d7c9",
x: -3.15,
z: 0.15,
brim: 1.45,
canopy: 1.30,
height: 0.62,
initial: { brim: 1.45, canopy: 1.30, height: 0.62 }
},
{
id: "B",
name: "Specimen B",
color: 0xef789c,
css: "#ef789c",
x: 0,
z: -0.2,
brim: 1.9,
canopy: 1.55,
height: 0.68,
initial: { brim: 1.9, canopy: 1.55, height: 0.68 }
},
{
id: "C",
name: "Specimen C",
color: 0xb7dc68,
css: "#b7dc68",
x: 3.15,
z: 0.05,
brim: 2.2,
canopy: 1.1,
height: 0.85,
initial: { brim: 2.2, canopy: 1.1, height: 0.85 }
}
];
const lightPresets = [
{
name: "Northwest",
azimuth: -50,
elevation: 32
},
{
name: "Northeast",
azimuth: 45,
elevation: 64
},
{
name: "Southeast",
azimuth: 135,
elevation: 24
}
];
const roi = {
minX: -5.2,
maxX: 5.2,
minZ: -2.7,
maxZ: 2.7
};
let selectedIndex = 0;
let activeLightIndex = 0;
let sweepTimer = null;
let modelUpdateQueued = false;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1117);
scene.fog = new THREE.Fog(0x0b1117, 17, 31);
const camera = new THREE.PerspectiveCamera(38, 1, 0.1, 80);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xdceeff, 0x18212c, 1.45);
scene.add(hemisphere);
const fillLight = new THREE.DirectionalLight(0x9ebcff, 0.65);
fillLight.position.set(-7, 9, 5);
scene.add(fillLight);
const sun = new THREE.DirectionalLight(0xffedc4, 3.1);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
new THREE.MeshStandardMaterial({
color: 0x151d25,
roughness: 0.95,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(22, 44, 0x34434f, 0x25313b);
grid.position.y = 0.009;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// A second, coarser grid makes the ground plane easy to read spatially.
const coarseGrid = new THREE.GridHelper(22, 11, 0x3f505d, 0x2f3d48);
coarseGrid.position.y = 0.012;
coarseGrid.material.transparent = true;
coarseGrid.material.opacity = 0.3;
scene.add(coarseGrid);
const figureGroups = [];
const labelElements = [];
function addBox(parent, size, position, material, figureIndex = null) {
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 = true;
mesh.receiveShadow = true;
if (figureIndex !== null) mesh.userData.figureIndex = figureIndex;
parent.add(mesh);
return mesh;
}
function createSelectionRing(index) {
const ring = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color: figures[index].color,
transparent: true,
opacity: 0.28,
depthWrite: false
});
const size = 2.25;
const thickness = 0.035;
const y = 0.025;
addBox(ring, [size, thickness, thickness], [0, y, -size / 2], material);
addBox(ring, [size, thickness, thickness], [0, y, size / 2], material);
addBox(ring, [thickness, thickness, size], [-size / 2, y, 0], material);
addBox(ring, [thickness, thickness, size], [size / 2, y, 0], material);
return { group: ring, material };
}
function createFigure(figure, index) {
const group = new THREE.Group();
group.position.set(figure.x, 0, figure.z);
group.userData.figureIndex = index;
const bodyMaterial = new THREE.MeshStandardMaterial({
color: figure.color,
roughness: 0.68,
metalness: 0.03
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x26343e,
roughness: 0.82
});
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xe6d8c4,
roughness: 0.8
});
const eyeMaterial = new THREE.MeshBasicMaterial({ color: 0x182027 });
// Feet and legs.
addBox(group, [0.25, 0.22, 0.42], [-0.21, 0.11, 0.08], darkMaterial, index);
addBox(group, [0.25, 0.22, 0.42], [0.21, 0.11, 0.08], darkMaterial, index);
addBox(group, [0.23, 0.78, 0.25], [-0.21, 0.61, 0], darkMaterial, index);
addBox(group, [0.23, 0.78, 0.25], [0.21, 0.61, 0], darkMaterial, index);
// Torso and belt.
addBox(group, [0.78, 0.73, 0.45], [0, 1.39, 0], bodyMaterial, index);
addBox(group, [0.8, 0.09, 0.47], [0, 1.19, 0], darkMaterial, index);
// Arms.
addBox(group, [0.21, 0.63, 0.34], [-0.52, 1.38, 0], bodyMaterial, index);
addBox(group, [0.21, 0.63, 0.34], [0.52, 1.38, 0], bodyMaterial, index);
addBox(group, [0.2, 0.18, 0.32], [-0.52, 1.0, 0.01], skinMaterial, index);
addBox(group, [0.2, 0.18, 0.32], [0.52, 1.0, 0.01], skinMaterial, index);
// Head.
addBox(group, [0.19, 0.13, 0.19], [0, 1.83, 0], skinMaterial, index);
addBox(group, [0.57, 0.59, 0.55], [0, 2.17, 0], skinMaterial, index);
addBox(group, [0.07, 0.07, 0.025], [-0.13, 2.22, 0.283], eyeMaterial, index);
addBox(group, [0.07, 0.07, 0.025], [0.13, 2.22, 0.283], eyeMaterial, index);
const hatGroup = new THREE.Group();
hatGroup.position.y = HAT_Y;
hatGroup.userData.figureIndex = index;
group.add(hatGroup);
const ring = createSelectionRing(index);
group.add(ring.group);
scene.add(group);
figureGroups.push({
group,
hatGroup,
ringMaterial: ring.material
});
const label = document.createElement("div");
label.className = "figure-label";
labelsElement.appendChild(label);
labelElements.push(label);
}
figures.forEach(createFigure);
function clearHat(figureIndex) {
const hat = figureGroups[figureIndex].hatGroup;
hat.traverse((child) => {
if (child.isMesh || child.isInstancedMesh) {
child.geometry?.dispose();
if (Array.isArray(child.material)) {
child.material.forEach((material) => material.dispose());
} else {
child.material?.dispose();
}
}
});
hat.clear();
}
function buildHat(figureIndex) {
const figure = figures[figureIndex];
const hat = figureGroups[figureIndex].hatGroup;
clearHat(figureIndex);
const dummy = new THREE.Object3D();
const cellSize = 0.17;
// Pixelated circular brim.
const brimRadius = figure.brim / 2;
const brimCells = [];
for (let x = -Math.ceil(brimRadius / cellSize);
x <= Math.ceil(brimRadius / cellSize); x++) {
for (let z = -Math.ceil(brimRadius / cellSize);
z <= Math.ceil(brimRadius / cellSize); z++) {
const px = x * cellSize;
const pz = z * cellSize;
if (Math.hypot(px, pz) <= brimRadius - cellSize * 0.08) {
brimCells.push([px, pz]);
}
}
}
const brimMaterial = new THREE.MeshStandardMaterial({
color: 0xf3bf52,
roughness: 0.7,
metalness: 0.02
});
const brim = new THREE.InstancedMesh(
new THREE.BoxGeometry(cellSize * 0.95, 0.1, cellSize * 0.95),
brimMaterial,
brimCells.length
);
brimCells.forEach(([x, z], i) => {
dummy.position.set(x, 0, z);
dummy.updateMatrix();
brim.setMatrixAt(i, dummy.matrix);
});
brim.instanceMatrix.needsUpdate = true;
brim.castShadow = true;
brim.receiveShadow = true;
brim.userData.figureIndex = figureIndex;
hat.add(brim);
// Stepped voxel canopy: broad lower tiers taper into a crown.
const canopyMaterial = new THREE.MeshStandardMaterial({
color: 0xf7d889,
roughness: 0.64,
metalness: 0.01
});
const layerCount = Math.max(5, Math.round(figure.height / 0.115));
const layerHeight = figure.height / layerCount;
for (let layer = 0; layer < layerCount; layer++) {
const t = layer / Math.max(1, layerCount - 1);
const radius = Math.max(
cellSize * 1.1,
(figure.canopy / 2) * (1 - 0.64 * t)
);
const cells = [];
for (let x = -Math.ceil(radius / cellSize);
x <= Math.ceil(radius / cellSize); x++) {
for (let z = -Math.ceil(radius / cellSize);
z <= Math.ceil(radius / cellSize); z++) {
const px = x * cellSize;
const pz = z * cellSize;
if (Math.hypot(px, pz) <= radius - cellSize * 0.08) {
cells.push([px, pz]);
}
}
}
if (!cells.length) continue;
const tier = new THREE.InstancedMesh(
new THREE.BoxGeometry(
cellSize * 0.92,
layerHeight * 0.9,
cellSize * 0.92
),
canopyMaterial,
cells.length
);
const y = 0.08 + (layer + 0.5) * layerHeight;
cells.forEach(([x, z], i) => {
dummy.position.set(x, y, z);
dummy.updateMatrix();
tier.setMatrixAt(i, dummy.matrix);
});
tier.instanceMatrix.needsUpdate = true;
tier.castShadow = true;
tier.receiveShadow = true;
tier.userData.figureIndex = figureIndex;
hat.add(tier);
}
const capMaterial = new THREE.MeshStandardMaterial({
color: figure.color,
roughness: 0.58
});
addBox(
hat,
[0.22, 0.11, 0.22],
[0, figure.height + 0.13, 0],
capMaterial,
figureIndex
);
}
figures.forEach((_, index) => buildHat(index));
// -------------------------------------------------------------------------
// Camera controls: direct orbit, pan-free zoom, and pinch support.
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.25, 0);
const cameraState = {
radius: 11.4,
theta: 0.58,
phi: 1.03
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraState.radius = 11.4;
cameraState.theta = 0.58;
cameraState.phi = 1.03;
updateCamera();
}
updateCamera();
const pointers = new Map();
let dragging = false;
let pointerStart = { x: 0, y: 0 };
let previousPinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragging = true;
pointerStart = {
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && dragging) {
const dx = event.clientX - pointerStart.x;
const dy = event.clientY - pointerStart.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.12,
1.42
);
pointerStart = {
x: event.clientX,
y: event.clientY
};
updateCamera();
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (previousPinchDistance / distance),
5.5,
19
);
updateCamera();
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
dragging = pointers.size === 1;
previousPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.5,
19
);
updateCamera();
},
{ passive: false }
);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
renderer.domElement.addEventListener("click", (event) => {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(figureGroups.map((entry) => entry.group), true);
if (hits.length) {
let object = hits[0].object;
while (object && object.userData.figureIndex === undefined) {
object = object.parent;
}
if (object) selectFigure(object.userData.figureIndex);
}
});
// -------------------------------------------------------------------------
// Lighting and controls.
// -------------------------------------------------------------------------
function lightDirection(preset) {
const elevation = THREE.MathUtils.degToRad(preset.elevation);
const azimuth = THREE.MathUtils.degToRad(preset.azimuth);
return new THREE.Vector3(
Math.cos(elevation) * Math.cos(azimuth),
Math.sin(elevation),
Math.cos(elevation) * Math.sin(azimuth)
).normalize();
}
function setLight(index) {
activeLightIndex = index;
const preset = lightPresets[index];
const direction = lightDirection(preset);
sun.target.position.copy(cameraTarget);
sun.position.copy(cameraTarget).addScaledVector(direction, 14);
document.querySelectorAll("[data-light]").forEach((element) => {
element.classList.toggle("active", Number(element.dataset.light) === index);
});
document.querySelector("#activeLightLabel").textContent =
`${preset.name} light · ${preset.elevation}° elevation`;
}
function selectFigure(index) {
selectedIndex = index;
document.querySelectorAll("[data-specimen]").forEach((element) => {
element.classList.toggle("active", Number(element.dataset.specimen) === index);
});
figureGroups.forEach((entry, i) => {
entry.ringMaterial.color.set(
i === selectedIndex ? figures[i].color : 0x52616d
);
entry.ringMaterial.opacity = i === selectedIndex ? 0.88 : 0.22;
});
syncControls();
}
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function syncControls() {
const figure = figures[selectedIndex];
document.querySelector("#brimRange").value = figure.brim;
document.querySelector("#canopyRange").value = figure.canopy;
document.querySelector("#heightRange").value = figure.height;
document.querySelector("#brimValue").textContent = `${figure.brim.toFixed(2)} m`;
document.querySelector("#canopyValue").textContent = `${figure.canopy.toFixed(2)} m`;
document.querySelector("#heightValue").textContent = `${figure.height.toFixed(2)} m`;
document.querySelector("#selectedName").textContent = figure.name;
document.querySelectorAll('input[type="range"]').forEach(updateRangeFill);
figures.forEach((item, index) => {
document.querySelector(`#specimenDetail${index}`).textContent =
`B ${item.brim.toFixed(2)} · C ${item.canopy.toFixed(2)} · H ${item.height.toFixed(2)}`;
});
}
function queueModelUpdate() {
if (modelUpdateQueued) return;
modelUpdateQueued = true;
requestAnimationFrame(() => {
buildHat(selectedIndex);
updateCoverage();
modelUpdateQueued = false;
});
}
document.querySelectorAll("[data-specimen]").forEach((button) => {
button.addEventListener("click", () => {
selectFigure(Number(button.dataset.specimen));
});
});
document.querySelector("#brimRange").addEventListener("input", (event) => {
figures[selectedIndex].brim = Number(event.target.value);
syncControls();
queueModelUpdate();
});
document.querySelector("#canopyRange").addEventListener("input", (event) => {
figures[selectedIndex].canopy = Number(event.target.value);
syncControls();
queueModelUpdate();
});
document.querySelector("#heightRange").addEventListener("input", (event) => {
figures[selectedIndex].height = Number(event.target.value);
syncControls();
queueModelUpdate();
});
document.querySelectorAll("[data-light]").forEach((element) => {
element.addEventListener("click", () => {
setLight(Number(element.dataset.light));
});
});
document.querySelector("#resetModel").addEventListener("click", () => {
const figure = figures[selectedIndex];
Object.assign(figure, figure.initial);
syncControls();
buildHat(selectedIndex);
updateCoverage();
});
document.querySelector("#resetAll").addEventListener("click", () => {
figures.forEach((figure, index) => {
Object.assign(figure, figure.initial);
buildHat(index);
});
selectFigure(0);
resetCamera();
setLight(0);
updateCoverage();
});
document.querySelector("#resetView").addEventListener("click", resetCamera);
document.querySelector("#topView").addEventListener("click", () => {
cameraState.radius = 13.5;
cameraState.theta = 0;
cameraState.phi = 0.055;
updateCamera();
});
document.querySelector("#sweepButton").addEventListener("click", (event) => {
if (sweepTimer) {
clearInterval(sweepTimer);
sweepTimer = null;
event.currentTarget.textContent = "Cycle light angles";
event.currentTarget.classList.remove("active");
return;
}
event.currentTarget.textContent = "Stop light cycle";
event.currentTarget.classList.add("active");
sweepTimer = setInterval(() => {
setLight((activeLightIndex + 1) % lightPresets.length);
}, 2400);
});
// -------------------------------------------------------------------------
// Pure geometric projection and coverage calculation.
//
// A 3D point P is projected to the ground along the negative light vector:
// P_ground = P - L * (P.y / L.y).
// -------------------------------------------------------------------------
function projectPoint(point, direction) {
const t = point.y / direction.y;
return {
x: point.x - direction.x * t,
z: point.z - direction.z * t
};
}
function convexHull(points) {
const unique = points
.map((point) => ({ x: point.x, z: point.z }))
.filter((point, index, array) =>
index === 0 ||
Math.abs(point.x - array[index - 1].x) > 1e-7 ||
Math.abs(point.z - array[index - 1].z) > 1e-7
)
.sort((a, b) => a.x - b.x || a.z - b.z);
if (unique.length < 3) return unique;
const cross = (o, a, b) =>
(a.x - o.x) * (b.z - o.z) - (a.z - o.z) * (b.x - o.x);
const lower = [];
for (const point of unique) {
while (
lower.length >= 2 &&
cross(lower[lower.length - 2], lower[lower.length - 1], point) <= 0
) {
lower.pop();
}
lower.push(point);
}
const upper = [];
for (let i = unique.length - 1; i >= 0; i--) {
const point = unique[i];
while (
upper.length >= 2 &&
cross(upper[upper.length - 2], upper[upper.length - 1], point) <= 0
) {
upper.pop();
}
upper.push(point);
}
lower.pop();
upper.pop();
return lower.concat(upper);
}
function projectCuboid(figure, centerX, centerZ, width, depth, y0, y1, direction) {
const points = [];
for (const y of [y0, y1]) {
for (const xOffset of [-width / 2, width / 2]) {
for (const zOffset of [-depth / 2, depth / 2]) {
points.push(
projectPoint(
new THREE.Vector3(
figure.x + centerX + xOffset,
y,
figure.z + centerZ + zOffset
),
direction
)
);
}
}
}
return convexHull(points);
}
function projectCircle(figure, radius, y, direction, segments = 24) {
const points = [];
for (let i = 0; i < segments; i++) {
const angle = (i / segments) * Math.PI * 2;
points.push(
projectPoint(
new THREE.Vector3(
figure.x + Math.cos(angle) * radius,
y,
figure.z + Math.sin(angle) * radius
),
direction
)
);
}
return convexHull(points);
}
function projectCanopy(figure, direction) {
const points = [];
const segments = 24;
const bottomRadius = figure.canopy / 2;
const topRadius = bottomRadius * 0.36;
for (const [radius, y] of [
[bottomRadius, HAT_BOTTOM],
[topRadius, HAT_BOTTOM + figure.height]
]) {
for (let i = 0; i < segments; i++) {
const angle = (i / segments) * Math.PI * 2;
points.push(
projectPoint(
new THREE.Vector3(
figure.x + Math.cos(angle) * radius,
y,
figure.z + Math.sin(angle) * radius
),
direction
)
);
}
}
return convexHull(points);
}
function polygonBounds(polygon) {
let minX = Infinity;
let maxX = -Infinity;
let minZ = Infinity;
let maxZ = -Infinity;
polygon.forEach((point) => {
minX = Math.min(minX, point.x);
maxX = Math.max(maxX, point.x);
minZ = Math.min(minZ, point.z);
maxZ = Math.max(maxZ, point.z);
});
return { minX, maxX, minZ, maxZ };
}
function buildProjectedPolygons(direction) {
return figures.map((figure) => {
const polygons = [
projectCuboid(figure, -0.21, 0, 0.23, 0.25, 0.02, 1.02, direction),
projectCuboid(figure, 0.21, 0, 0.23, 0.25, 0.02, 1.02, direction),
projectCuboid(figure, 0, 0, 0.78, 0.45, 1.02, 1.78, direction),
projectCuboid(figure, -0.52, 0, 0.21, 0.34, 1.07, 1.72, direction),
projectCuboid(figure, 0.52, 0, 0.21, 0.34, 1.07, 1.72, direction),
projectCuboid(figure, 0, 0, 0.57, 0.55, 1.88, BODY_TOP, direction),
projectCircle(figure, figure.brim / 2, HAT_Y, direction),
projectCanopy(figure, direction)
];
polygons.forEach((polygon) => {
polygon.bounds = polygonBounds(polygon);
});
return polygons;
});
}
function pointInPolygon(x, z, polygon) {
const bounds = polygon.bounds;
if (
x < bounds.minX || x > bounds.maxX ||
z < bounds.minZ || z > bounds.maxZ
) {
return false;
}
let inside = false;
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
const xi = polygon[i].x;
const zi = polygon[i].z;
const xj = polygon[j].x;
const zj = polygon[j].z;
const intersects =
((zi > z) !== (zj > z)) &&
(x < ((xj - xi) * (z - zi)) / (zj - zi) + xi);
if (intersects) inside = !inside;
}
return inside;
}
function calculateCoverage(polygonsByFigure) {
const columns = 120;
const rows = 64;
const totalSamples = columns * rows;
const roiWidth = roi.maxX - roi.minX;
const roiDepth = roi.maxZ - roi.minZ;
const roiArea = roiWidth * roiDepth;
let covered = 0;
const perFigure = [0, 0, 0];
for (let row = 0; row < rows; row++) {
const z = roi.minZ + ((row + 0.5) / rows) * roiDepth;
for (let column = 0; column < columns; column++) {
const x = roi.minX + ((column + 0.5) / columns) * roiWidth;
let anyCovered = false;
for (let figureIndex = 0; figureIndex < polygonsByFigure.length; figureIndex++) {
const polygons = polygonsByFigure[figureIndex];
let figureCovered = false;
for (const polygon of polygons) {
if (pointInPolygon(x, z, polygon)) {
figureCovered = true;
break;
}
}
if (figureCovered) {
perFigure[figureIndex]++;
anyCovered = true;
}
}
if (anyCovered) covered++;
}
}
return {
percent: (covered / totalSamples) * 100,
area: covered * roiArea / totalSamples,
perFigure: perFigure.map((count) => count * roiArea / totalSamples)
};
}
function hexToRgba(hex, alpha) {
const value = parseInt(hex.replace("#", ""), 16);
const r = (value >> 16) & 255;
const g = (value >> 8) & 255;
const b = value & 255;
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
function drawCoverageMap(canvasIndex, preset, polygonsByFigure) {
const canvas = document.querySelector(`#coverage${canvasIndex}`);
const rect = canvas.getBoundingClientRect();
const width = Math.max(1, rect.width);
const height = Math.max(1, rect.height);
const dpr = Math.min(window.devicePixelRatio, 2);
canvas.width = Math.round(width * dpr);
canvas.height = Math.round(height * dpr);
const context = canvas.getContext("2d");
context.setTransform(dpr, 0, 0, dpr, 0, 0);
context.clearRect(0, 0, width, height);
const roiWidth = roi.maxX - roi.minX;
const roiDepth = roi.maxZ - roi.minZ;
const mapAspect = roiWidth / roiDepth;
const availableWidth = width - 18;
const availableHeight = height - 18;
let mapWidth;
let mapHeight;
if (availableWidth / availableHeight > mapAspect) {
mapHeight = availableHeight;
mapWidth = mapHeight * mapAspect;
} else {
mapWidth = availableWidth;
mapHeight = mapWidth / mapAspect;
}
const mapX = (width - mapWidth) / 2;
const mapY = (height - mapHeight) / 2;
const mapPoint = (x, z) => ({
x: mapX + ((x - roi.minX) / roiWidth) * mapWidth,
y: mapY + ((z - roi.minZ) / roiDepth) * mapHeight
});
context.fillStyle = "#0c141b";
context.fillRect(0, 0, width, height);
context.fillStyle = "#101922";
context.fillRect(mapX, mapY, mapWidth, mapHeight);
context.strokeStyle = "rgba(102, 122, 138, 0.16)";
context.lineWidth = 1;
context.beginPath();
for (let x = Math.ceil(roi.minX); x <= roi.maxX; x++) {
const p = mapPoint(x, roi.minZ);
const q = mapPoint(x, roi.maxZ);
context.moveTo(p.x, p.y);
context.lineTo(q.x, q.y);
}
for (let z = Math.ceil(roi.minZ); z <= roi.maxZ; z++) {
const p = mapPoint(roi.minX, z);
const q = mapPoint(roi.maxX, z);
context.moveTo(p.x, p.y);
context.lineTo(q.x, q.y);
}
context.stroke();
context.save();
context.beginPath();
context.rect(mapX, mapY, mapWidth, mapHeight);
context.clip();
figures.forEach((figure, index) => {
const polygons = polygonsByFigure[index];
// Fill all polygons for one specimen as a single non-zero path.
context.beginPath();
polygons.forEach((polygon) => {
polygon.forEach((point, pointIndex) => {
const screen = mapPoint(point.x, point.z);
if (pointIndex === 0) context.moveTo(screen.x, screen.y);
else context.lineTo(screen.x, screen.y);
});
context.closePath();
});
context.fillStyle = hexToRgba(figure.css, 0.24);
context.fill("nonzero");
polygons.forEach((polygon) => {
context.beginPath();
polygon.forEach((point, pointIndex) => {
const screen = mapPoint(point.x, point.z);
if (pointIndex === 0) context.moveTo(screen.x, screen.y);
else context.lineTo(screen.x, screen.y);
});
context.closePath();
context.strokeStyle = hexToRgba(figure.css, 0.62);
context.lineWidth = 1;
context.stroke();
});
});
context.restore();
// Figure markers.
figures.forEach((figure) => {
const center = mapPoint(figure.x, figure.z);
const size = 5;
context.fillStyle = figure.css;
context.fillRect(
center.x - size / 2,
center.y - size / 2,
size,
size
);
context.fillStyle = "#edf3f7";
context.font = "700 9px system-ui";
context.fillText(figure.id, center.x + 6, center.y + 3);
});
// Small source-direction indicator.
const direction = lightDirection(preset);
const arrowX = mapX + mapWidth - 24;
const arrowY = mapY + 20;
const arrowLength = 18;
const angle = Math.atan2(direction.z, direction.x);
context.strokeStyle = "#f4c35b";
context.fillStyle = "#f4c35b";
context.lineWidth = 1.5;
context.beginPath();
context.moveTo(
arrowX - Math.cos(angle) * arrowLength / 2,
arrowY + Math.sin(angle) * arrowLength / 2
);
context.lineTo(
arrowX + Math.cos(angle) * arrowLength / 2,
arrowY - Math.sin(angle) * arrowLength / 2
);
context.stroke();
context.beginPath();
context.arc(arrowX, arrowY, 2, 0, Math.PI * 2);
context.fill();
context.font = "700 8px system-ui";
context.fillText("SOURCE", arrowX - 17, arrowY + 14);
}
function updateCoverage() {
lightPresets.forEach((preset, index) => {
const direction = lightDirection(preset);
const polygons = buildProjectedPolygons(direction);
const stats = calculateCoverage(polygons);
document.querySelector(`#coveragePercent${index}`).textContent =
`${stats.percent.toFixed(1)}%`;
document.querySelector(`#coverageArea${index}`).textContent =
`${stats.area.toFixed(2)} m²`;
drawCoverageMap(index, preset, polygons);
});
}
// -------------------------------------------------------------------------
// Rendering and UI synchronization.
// -------------------------------------------------------------------------
function resizeRenderer() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resizeRenderer);
resizeObserver.observe(viewport);
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
figures.forEach((figure, index) => {
const projected = new THREE.Vector3(
figure.x,
HAT_Y + figure.height + 0.62,
figure.z
).project(camera);
const label = labelElements[index];
const visible = projected.z > -1 && projected.z < 1;
label.style.display = visible ? "block" : "none";
label.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
label.classList.toggle("active", index === selectedIndex);
label.textContent =
`${figure.id} · ${figure.brim.toFixed(2)} m brim`;
});
}
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
updateLabels();
}
let coverageResizeTimer = null;
window.addEventListener("resize", () => {
clearTimeout(coverageResizeTimer);
coverageResizeTimer = setTimeout(updateCoverage, 120);
});
syncControls();
setLight(0);
updateCoverage();
resizeRenderer();
animate();
</script>
</body>
</html>
```