Prompt specification
Build or customize the Three.js 3D scene "Gap opening changes understory light" 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: Gap opening changes understory light
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/gap-opening-changes-understory-light-x0722
Scene Description Prompt:
Gap opening changes understory light. Remove one canopy panel above a seedling model and update its light patch; preserve the sun direction so the effect follows only from the opening. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (gap-opening-changes-understory-light-x0722):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Canopy Gap — Voxel Light Experiment</title>
<style>
:root {
color-scheme: dark;
--ink: #eef5e8;
--muted: #9fb3a7;
--panel: rgba(12, 24, 23, 0.86);
--border: rgba(205, 235, 214, 0.16);
--accent: #ffd477;
--green: #88d49b;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b1516;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging { cursor: grabbing; }
.card {
position: fixed;
z-index: 10;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
}
.intro {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 19px 20px 18px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.intro p {
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.controls {
right: 22px;
top: 22px;
width: 294px;
padding: 17px;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.status-label {
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--accent);
font-size: 12px;
font-weight: 750;
}
.status::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 14px currentColor;
content: "";
}
.status.closed { color: #a7c3b2; }
button {
width: 100%;
min-height: 42px;
margin-top: 8px;
border: 1px solid rgba(255, 212, 119, 0.34);
border-radius: 10px;
background: var(--accent);
color: #261b08;
font: inherit;
font-size: 13px;
font-weight: 800;
cursor: pointer;
transition: transform 120ms ease, filter 120ms ease;
}
button:hover { filter: brightness(1.07); }
button:active { transform: translateY(1px); }
button.secondary {
border-color: var(--border);
background: rgba(255, 255, 255, 0.045);
color: var(--ink);
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 16px;
}
.metric {
min-height: 76px;
padding: 12px;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(255, 255, 255, 0.035);
}
.metric strong {
display: block;
margin-top: 7px;
color: var(--ink);
font-size: 20px;
line-height: 1;
}
.metric span {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.sun-lock {
display: flex;
align-items: center;
gap: 11px;
margin-top: 12px;
padding: 11px 12px;
border: 1px solid rgba(255, 212, 119, 0.18);
border-radius: 11px;
background: rgba(255, 212, 119, 0.055);
}
.sun-icon {
position: relative;
flex: 0 0 31px;
width: 31px;
height: 31px;
border: 1px solid rgba(255, 212, 119, 0.52);
border-radius: 50%;
}
.sun-icon::before {
position: absolute;
inset: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 12px rgba(255, 212, 119, 0.7);
content: "";
}
.sun-copy strong {
display: block;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.sun-copy small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.legend {
bottom: 22px;
left: 22px;
display: flex;
flex-wrap: wrap;
gap: 15px;
padding: 12px 15px;
color: var(--muted);
font-size: 11px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 3px;
background: #477f52;
}
.swatch.light { background: var(--accent); }
.swatch.trunk { background: #7c5435; }
.world-label {
position: fixed;
z-index: 5;
left: 0;
top: 0;
pointer-events: none;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.17);
border-radius: 7px;
background: rgba(10, 20, 20, 0.76);
color: #e8f1e8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -50%);
will-change: transform;
}
#gapLabel {
border-color: rgba(255, 212, 119, 0.28);
color: var(--accent);
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #0b1516;
color: var(--muted);
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#loading.hidden { display: none; }
@media (max-width: 760px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px;
}
.intro p { display: none; }
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
.metrics { display: none; }
.legend { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="card intro">
<div class="eyebrow">Understory light experiment</div>
<h1>Canopy gap</h1>
<p>
One voxel canopy panel has been removed above the seedling.
The sunlight vector stays locked; only the canopy geometry changes.
</p>
</section>
<aside class="card controls" aria-label="Experiment controls">
<div class="status-row">
<span class="status-label">Canopy state</span>
<span id="status" class="status" aria-live="polite">Gap open</span>
</div>
<button id="toggleGap" type="button">Restore canopy panel</button>
<button id="resetView" class="secondary" type="button">Reset camera</button>
<div class="metrics">
<div class="metric">
<span>Direct-light patch</span>
<strong id="lightCount">—</strong>
</div>
<div class="metric">
<span>Change from closed</span>
<strong id="lightDelta">—</strong>
</div>
</div>
<div class="sun-lock">
<div class="sun-icon" aria-hidden="true"></div>
<div class="sun-copy">
<strong>Sun direction locked</strong>
<small id="sunVector">X — · Y — · Z —</small>
</div>
</div>
</aside>
<div class="card legend">
<span class="legend-item"><i class="swatch"></i> Canopy</span>
<span class="legend-item"><i class="swatch light"></i> Sunlit ground</span>
<span class="legend-item"><i class="swatch trunk"></i> Trunks</span>
</div>
<div id="seedlingLabel" class="world-label">Seedling</div>
<div id="gapLabel" class="world-label">Open canopy gap</div>
<div id="loading">Building voxel forest…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1516);
scene.fog = new THREE.Fog(0x0b1516, 17, 31);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
// Orthographic camera gives the scene a clean voxel-diorama appearance.
const camera = new THREE.OrthographicCamera(
-10, 10, 7.5, -7.5, 0.1, 100
);
const cameraState = {
target: new THREE.Vector3(0, 2.15, 0),
yaw: 0.72,
pitch: 0.64,
zoom: 1
};
function updateCamera() {
const { target, yaw, pitch, zoom } = cameraState;
const radius = 20;
camera.position.set(
target.x + radius * Math.sin(yaw) * Math.cos(pitch),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(yaw) * Math.cos(pitch)
);
camera.lookAt(target);
camera.zoom = zoom;
camera.updateProjectionMatrix();
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const viewHeight = 14.5;
camera.left = -viewHeight * aspect * 0.5;
camera.right = viewHeight * aspect * 0.5;
camera.top = viewHeight * 0.5;
camera.bottom = -viewHeight * 0.5;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// -------------------------------------------------------------------------
// Fixed lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0x91c7d1, 0x263b28, 1.55
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedb0, 3.25);
sun.position.set(-2.3, 14, -2.5);
sun.target.position.set(0, 0, 0);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
// This vector is intentionally never changed by the gap interaction.
const sunDirection = sun.position.clone()
.sub(sun.target.position)
.normalize();
// -------------------------------------------------------------------------
// Deterministic color variation
// -------------------------------------------------------------------------
let randomSeed = 781923;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const voxelGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
// -------------------------------------------------------------------------
// Voxel ground
// -------------------------------------------------------------------------
const groundSize = 15;
const groundGeometry = new THREE.BoxGeometry(0.94, 0.42, 0.94);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0
});
const ground = new THREE.InstancedMesh(
groundGeometry,
groundMaterial,
groundSize * groundSize
);
ground.receiveShadow = true;
const groundColor = new THREE.Color();
let groundIndex = 0;
for (let x = 0; x < groundSize; x++) {
for (let z = 0; z < groundSize; z++) {
const px = x - (groundSize - 1) * 0.5;
const pz = z - (groundSize - 1) * 0.5;
const height = 0.34 + random() * 0.1;
dummy.position.set(px, -height * 0.5, pz);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, height / 0.42, 1);
dummy.updateMatrix();
ground.setMatrixAt(groundIndex, dummy.matrix);
groundColor.setHSL(
0.27 + random() * 0.035,
0.25 + random() * 0.08,
0.17 + random() * 0.055
);
ground.setColorAt(groundIndex, groundColor);
groundIndex++;
}
}
ground.instanceMatrix.needsUpdate = true;
ground.instanceColor.needsUpdate = true;
scene.add(ground);
const soilBase = new THREE.Mesh(
new THREE.BoxGeometry(15.4, 0.7, 15.4),
new THREE.MeshStandardMaterial({
color: 0x342b21,
roughness: 1
})
);
soilBase.position.y = -0.58;
soilBase.receiveShadow = true;
scene.add(soilBase);
// -------------------------------------------------------------------------
// Canopy made of individual voxel instances
//
// The central 5 x 5 group is one removable panel. The remaining canopy
// and the removable panel use separate InstancedMesh objects.
// -------------------------------------------------------------------------
const canopyGrid = 13;
const canopySpacing = 0.76;
const canopyY = 5.35;
const gapRadius = 2;
const fixedCanopyMatrices = [];
const panelCanopyMatrices = [];
for (let gx = -6; gx <= 6; gx++) {
for (let gz = -6; gz <= 6; gz++) {
const isPanel =
Math.abs(gx) <= gapRadius &&
Math.abs(gz) <= gapRadius;
const px = gx * canopySpacing;
const pz = gz * canopySpacing;
const py = isPanel
? canopyY
: canopyY + (random() - 0.5) * 0.14;
dummy.position.set(px, py, pz);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.72, 0.72, 0.72);
dummy.updateMatrix();
const matrix = dummy.matrix.clone();
if (isPanel) {
panelCanopyMatrices.push(matrix);
} else {
fixedCanopyMatrices.push(matrix);
}
}
}
const canopyMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
metalness: 0
});
const fixedCanopy = new THREE.InstancedMesh(
voxelGeometry,
canopyMaterial,
fixedCanopyMatrices.length
);
fixedCanopy.castShadow = true;
fixedCanopy.receiveShadow = true;
fixedCanopy.instanceMatrix.setUsage(THREE.StaticDrawUsage);
const canopyColor = new THREE.Color();
fixedCanopyMatrices.forEach((matrix, index) => {
fixedCanopy.setMatrixAt(index, matrix);
canopyColor.setHSL(
0.30 + random() * 0.055,
0.38 + random() * 0.17,
0.25 + random() * 0.10
);
fixedCanopy.setColorAt(index, canopyColor);
});
fixedCanopy.instanceMatrix.needsUpdate = true;
fixedCanopy.instanceColor.needsUpdate = true;
fixedCanopy.computeBoundingSphere();
scene.add(fixedCanopy);
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
metalness: 0
});
const panelCanopy = new THREE.InstancedMesh(
voxelGeometry,
panelMaterial,
panelCanopyMatrices.length
);
panelCanopy.castShadow = true;
panelCanopy.receiveShadow = true;
panelCanopy.instanceMatrix.setUsage(THREE.StaticDrawUsage);
panelCanopyMatrices.forEach((matrix, index) => {
panelCanopy.setMatrixAt(index, matrix);
canopyColor.setHSL(
0.22 + random() * 0.04,
0.46 + random() * 0.10,
0.37 + random() * 0.07
);
panelCanopy.setColorAt(index, canopyColor);
});
panelCanopy.instanceMatrix.needsUpdate = true;
panelCanopy.instanceColor.needsUpdate = true;
panelCanopy.computeBoundingSphere();
scene.add(panelCanopy);
// -------------------------------------------------------------------------
// Voxel trunks and branches
// -------------------------------------------------------------------------
const trunkMatrices = [];
const trunkPositions = [
[-4.25, -3.75],
[-4.35, 3.70],
[ 4.20, -3.70],
[ 4.25, 3.75],
[-0.25, -4.25],
[ 0.35, 4.25]
];
function addTrunkBlock(x, y, z, size = 0.72) {
dummy.position.set(x, y, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(size, size, size);
dummy.updateMatrix();
trunkMatrices.push(dummy.matrix.clone());
}
for (const [x, z] of trunkPositions) {
for (let level = 0; level < 7; level++) {
addTrunkBlock(x, 0.36 + level * 0.68, z);
}
addTrunkBlock(x - 0.48, 4.56, z, 0.62);
addTrunkBlock(x + 0.48, 4.70, z, 0.62);
addTrunkBlock(x, 4.58, z - 0.48, 0.62);
}
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
});
const trunks = new THREE.InstancedMesh(
voxelGeometry,
trunkMaterial,
trunkMatrices.length
);
trunks.castShadow = true;
trunks.receiveShadow = true;
const trunkColor = new THREE.Color();
trunkMatrices.forEach((matrix, index) => {
trunks.setMatrixAt(index, matrix);
trunkColor.setHSL(
0.075 + random() * 0.025,
0.42 + random() * 0.12,
0.24 + random() * 0.08
);
trunks.setColorAt(index, trunkColor);
});
trunks.instanceMatrix.needsUpdate = true;
trunks.instanceColor.needsUpdate = true;
scene.add(trunks);
// -------------------------------------------------------------------------
// Seedling: block-built stem, soil and leaves
// -------------------------------------------------------------------------
const seedling = new THREE.Group();
seedling.position.set(0.78, 0, 0.88);
scene.add(seedling);
const soilMaterial = new THREE.MeshStandardMaterial({
color: 0x5b402a,
roughness: 1
});
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x4c9c50,
roughness: 0.85
});
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0x68bd62,
roughness: 0.82
});
const brightLeafMaterial = new THREE.MeshStandardMaterial({
color: 0x91d875,
roughness: 0.8
});
function addBlock(parent, material, x, y, z, sx, sy = sx, sz = sx) {
const mesh = new THREE.Mesh(voxelGeometry, material);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Four soil blocks around the seedling.
addBlock(seedling, soilMaterial, -0.16, 0.12, -0.16, 0.28);
addBlock(seedling, soilMaterial, 0.16, 0.12, -0.16, 0.28);
addBlock(seedling, soilMaterial, -0.16, 0.12, 0.16, 0.28);
addBlock(seedling, soilMaterial, 0.16, 0.12, 0.16, 0.28);
// Stem.
for (let level = 0; level < 5; level++) {
addBlock(
seedling,
stemMaterial,
0,
0.38 + level * 0.25,
0,
0.22
);
}
// Lower leaf pair.
addBlock(seedling, leafMaterial, -0.24, 0.72, 0.00, 0.25);
addBlock(seedling, leafMaterial, -0.47, 0.72, 0.00, 0.25);
addBlock(seedling, leafMaterial, 0.24, 0.78, 0.00, 0.25);
addBlock(seedling, leafMaterial, 0.47, 0.78, 0.00, 0.25);
// Upper leaf pair, rotated in the voxel grid.
addBlock(seedling, brightLeafMaterial, 0.00, 1.08, -0.23, 0.25);
addBlock(seedling, brightLeafMaterial, 0.00, 1.08, -0.46, 0.25);
addBlock(seedling, brightLeafMaterial, 0.00, 1.17, 0.23, 0.25);
addBlock(seedling, brightLeafMaterial, 0.00, 1.17, 0.46, 0.25);
// New leaves at the top.
addBlock(seedling, brightLeafMaterial, -0.19, 1.43, 0.00, 0.23);
addBlock(seedling, brightLeafMaterial, 0.19, 1.43, 0.00, 0.23);
addBlock(seedling, brightLeafMaterial, 0.00, 1.66, 0.00, 0.24);
// -------------------------------------------------------------------------
// Computed sun patch
//
// For every ground voxel, a ray is cast toward the fixed sun. If it passes
// through the canopy, that voxel belongs to the illuminated patch. Closing
// or opening the one panel therefore updates the patch without moving the
// sun.
// -------------------------------------------------------------------------
const patchMaterial = new THREE.MeshBasicMaterial({
color: 0xffd477,
transparent: true,
opacity: 0.36,
depthWrite: false,
blending: THREE.AdditiveBlending,
polygonOffset: true,
polygonOffsetFactor: -1
});
const patchGeometry = new THREE.BoxGeometry(0.88, 0.035, 0.88);
const lightPatch = new THREE.InstancedMesh(
patchGeometry,
patchMaterial,
groundSize * groundSize
);
lightPatch.count = 0;
lightPatch.frustumCulled = false;
lightPatch.renderOrder = 2;
scene.add(lightPatch);
const raycaster = new THREE.Raycaster();
const rayOrigin = new THREE.Vector3();
const patchDummy = new THREE.Object3D();
let gapOpen = true;
let closedExposure = 0;
let openExposure = 0;
function measureExposure(includePanel) {
const previousVisibility = panelCanopy.visible;
// Ensure the panel participates in the closed-state ray test.
if (includePanel) panelCanopy.visible = true;
scene.updateMatrixWorld(true);
const occluders = [fixedCanopy];
if (includePanel) occluders.push(panelCanopy);
let count = 0;
for (let gx = 0; gx < groundSize; gx++) {
for (let gz = 0; gz < groundSize; gz++) {
const px = gx - (groundSize - 1) * 0.5;
const pz = gz - (groundSize - 1) * 0.5;
rayOrigin.set(px, 0.045, pz);
raycaster.set(rayOrigin, sunDirection);
const hits = raycaster.intersectObjects(occluders, false);
if (hits.length === 0) {
count++;
}
}
}
panelCanopy.visible = previousVisibility;
return count;
}
function updateLightPatch() {
closedExposure = measureExposure(true);
openExposure = measureExposure(false);
const occluders = [fixedCanopy];
if (!gapOpen) occluders.push(panelCanopy);
scene.updateMatrixWorld(true);
let patchIndex = 0;
for (let gx = 0; gx < groundSize; gx++) {
for (let gz = 0; gz < groundSize; gz++) {
const px = gx - (groundSize - 1) * 0.5;
const pz = gz - (groundSize - 1) * 0.5;
rayOrigin.set(px, 0.045, pz);
raycaster.set(rayOrigin, sunDirection);
const hits = raycaster.intersectObjects(occluders, false);
if (hits.length === 0) {
patchDummy.position.set(px, 0.028, pz);
patchDummy.rotation.set(0, 0, 0);
patchDummy.scale.set(1, 1, 1);
patchDummy.updateMatrix();
lightPatch.setMatrixAt(patchIndex, patchDummy.matrix);
patchIndex++;
}
}
}
lightPatch.count = patchIndex;
lightPatch.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// UI state
// -------------------------------------------------------------------------
const toggleButton = document.getElementById("toggleGap");
const resetButton = document.getElementById("resetView");
const status = document.getElementById("status");
const lightCount = document.getElementById("lightCount");
const lightDelta = document.getElementById("lightDelta");
const gapLabel = document.getElementById("gapLabel");
function updateUI() {
const activeCount = gapOpen ? openExposure : closedExposure;
const delta = openExposure - closedExposure;
status.textContent = gapOpen ? "Gap open" : "Panel closed";
status.classList.toggle("closed", !gapOpen);
toggleButton.textContent = gapOpen
? "Restore canopy panel"
: "Remove canopy panel";
lightCount.textContent = `${activeCount} tiles`;
lightDelta.textContent = delta >= 0 ? `+${delta}` : `${delta}`;
gapLabel.textContent = gapOpen
? "Open canopy gap"
: "Canopy panel";
}
function setGap(open) {
gapOpen = open;
panelCanopy.visible = !gapOpen;
// Shadow maps update automatically; this also requests an immediate update.
renderer.shadowMap.needsUpdate = true;
updateLightPatch();
updateUI();
}
toggleButton.addEventListener("click", () => {
setGap(!gapOpen);
});
resetButton.addEventListener("click", () => {
cameraState.yaw = 0.72;
cameraState.pitch = 0.64;
cameraState.zoom = 1;
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
setGap(!gapOpen);
}
if (event.key.toLowerCase() === "r" && event.target === document.body) {
cameraState.yaw = 0.72;
cameraState.pitch = 0.64;
cameraState.zoom = 1;
updateCamera();
}
});
// -------------------------------------------------------------------------
// Custom camera controls: drag to orbit and wheel to zoom
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.30,
1.25
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
1.75
);
updateCamera();
}, { passive: false });
// -------------------------------------------------------------------------
// Projected world labels
// -------------------------------------------------------------------------
const seedlingLabel = document.getElementById("seedlingLabel");
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
const seedlingLabelPosition = new THREE.Vector3(0.78, 1.95, 0.88);
const gapLabelPosition = new THREE.Vector3(0, 5.95, 0);
// -------------------------------------------------------------------------
// Initialize
// -------------------------------------------------------------------------
panelCanopy.visible = false;
updateLightPatch();
updateUI();
document.getElementById("sunVector").textContent =
`X ${sunDirection.x.toFixed(2)} · ` +
`Y ${sunDirection.y.toFixed(2)} · ` +
`Z ${sunDirection.z.toFixed(2)}`;
loading.classList.add("hidden");
renderer.setAnimationLoop(() => {
positionLabel(seedlingLabel, seedlingLabelPosition);
positionLabel(gapLabel, gapLabelPosition);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```