Prompt specification
Build or customize the Three.js 3D scene "Courtyard canopy shade 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: Courtyard canopy shade exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/courtyard-canopy-shade-exhibit-x1583
Scene Description Prompt:
Show a voxel courtyard canopy shading a bench, with an inset explaining sun–canopy shadow geometry. Make standalone HTML with Three.js from cdnjs.
Working Three.js Starter (courtyard-canopy-shade-exhibit-x1583):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Courtyard Canopy</title>
<style>
:root {
--ink: #263832;
--muted: #66756e;
--paper: rgba(250, 247, 239, 0.86);
--line: rgba(38, 56, 50, 0.15);
--accent: #c96f4c;
--sun: #d99a37;
--shadow: rgba(39, 57, 51, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e3d7;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#stage canvas:active {
cursor: grabbing;
}
#story {
position: fixed;
top: 30px;
left: 32px;
z-index: 5;
width: min(410px, calc(100vw - 64px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 2px;
content: "";
background: var(--accent);
}
h1 {
margin: 0;
font-size: clamp(38px, 5vw, 68px);
font-weight: 760;
letter-spacing: -0.055em;
line-height: 0.94;
}
#story p {
max-width: 360px;
margin: 17px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.6;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.66);
border-radius: 18px;
background: var(--paper);
box-shadow:
0 18px 55px rgba(42, 49, 43, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.65);
backdrop-filter: blur(18px) saturate(120%);
-webkit-backdrop-filter: blur(18px) saturate(120%);
}
#sun-panel {
top: 28px;
right: 28px;
width: 270px;
padding: 18px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 16px;
}
.panel-heading strong {
font-size: 11px;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.status {
padding: 5px 8px;
border-radius: 999px;
background: #dce5d9;
color: #355444;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
white-space: nowrap;
}
.readout {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 5px;
}
.readout output {
font-size: 25px;
font-weight: 720;
letter-spacing: -0.04em;
}
.readout span,
.control-label {
color: var(--muted);
font-size: 11px;
font-weight: 650;
letter-spacing: 0.04em;
}
input[type="range"] {
width: 100%;
height: 24px;
margin: 0 0 11px;
accent-color: var(--accent);
cursor: pointer;
}
.control-label {
display: flex;
justify-content: space-between;
margin-top: 4px;
}
.control-label output {
color: var(--ink);
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin: 8px 0 14px;
}
.presets button,
#reset-view {
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.52);
color: var(--ink);
padding: 8px 6px;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
cursor: pointer;
transition:
background 160ms ease,
transform 160ms ease;
}
.presets button:hover,
#reset-view:hover {
background: #fff;
transform: translateY(-1px);
}
.switch {
display: flex;
align-items: center;
gap: 9px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
font-weight: 650;
cursor: pointer;
}
.switch input {
width: 16px;
height: 16px;
accent-color: var(--accent);
}
#reset-view {
width: 100%;
margin-top: 11px;
}
#inset {
right: 28px;
bottom: 28px;
width: 350px;
padding: 18px;
}
#inset .panel-heading {
margin-bottom: 8px;
}
.inset-subtitle {
margin: -4px 0 10px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.diagram {
width: 100%;
height: auto;
overflow: visible;
border-radius: 12px;
background:
linear-gradient(rgba(255, 255, 255, 0.52), rgba(255, 255, 255, 0.52)),
repeating-linear-gradient(
0deg,
transparent 0 23px,
rgba(38, 56, 50, 0.045) 23px 24px
),
repeating-linear-gradient(
90deg,
transparent 0 23px,
rgba(38, 56, 50, 0.045) 23px 24px
);
}
.steps {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin: 11px 0;
}
.step {
min-width: 0;
padding: 9px 8px;
border-radius: 10px;
background: rgba(38, 56, 50, 0.055);
}
.step b {
display: block;
margin-bottom: 3px;
color: var(--accent);
font-size: 10px;
}
.step span {
display: block;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.equation {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
padding: 9px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
}
.equation .accent {
color: var(--accent);
}
#inset-note {
min-height: 34px;
margin: 10px 1px 0;
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
.scene-tag {
position: fixed;
z-index: 4;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 999px;
background: rgba(250, 247, 239, 0.76);
box-shadow: 0 5px 18px rgba(40, 50, 45, 0.08);
color: var(--ink);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.11em;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.scene-tag::before {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 6px;
border-radius: 50%;
background: var(--accent);
content: "";
vertical-align: 1px;
}
#bench-tag::before {
background: #557263;
}
#help {
position: fixed;
bottom: 29px;
left: 32px;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
color: rgba(38, 56, 50, 0.67);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.05em;
pointer-events: none;
}
#help span {
padding: 5px 7px;
border: 1px solid rgba(38, 56, 50, 0.13);
border-radius: 6px;
background: rgba(250, 247, 239, 0.55);
}
@media (max-width: 900px) {
#story {
top: 20px;
left: 20px;
}
#story p {
display: none;
}
#sun-panel {
top: 18px;
right: 18px;
width: 235px;
padding: 14px;
}
#inset {
right: 18px;
bottom: 18px;
width: 310px;
padding: 14px;
}
.steps {
gap: 4px;
}
#help {
left: 20px;
bottom: 20px;
}
}
@media (max-width: 650px) {
#story {
top: 16px;
left: 16px;
}
#story h1 {
max-width: 210px;
font-size: 38px;
}
#sun-panel {
top: auto;
right: 12px;
bottom: 12px;
width: 210px;
max-height: 43vh;
overflow: auto;
}
#inset {
top: 126px;
right: 12px;
bottom: auto;
width: 210px;
padding: 12px;
}
.inset-subtitle,
.steps,
.equation {
display: none;
}
#inset-note {
font-size: 9px;
}
#help,
.scene-tag {
display: none;
}
}
</style>
</head>
<body>
<main id="stage" aria-label="Interactive three-dimensional voxel courtyard"></main>
<header id="story">
<div class="eyebrow">Light / Space / Shadow</div>
<h1>Courtyard<br />Canopy</h1>
<p>
A voxel roof intercepts parallel sunlight and translates it into a
precisely placed shadow across the bench.
</p>
</header>
<section id="sun-panel" class="panel" aria-label="Sunlight controls">
<div class="panel-heading">
<strong>Solar study</strong>
<span id="sun-state" class="status">ROOF OVERLAP</span>
</div>
<div class="readout">
<output id="azimuth-value" for="azimuth">140°</output>
<span>azimuth</span>
</div>
<input
id="azimuth"
type="range"
min="105"
max="175"
value="140"
aria-label="Sun azimuth"
/>
<div class="control-label">
<label for="elevation">Elevation</label>
<output id="elevation-value" for="elevation">52°</output>
</div>
<input
id="elevation"
type="range"
min="30"
max="68"
value="52"
aria-label="Sun elevation"
/>
<div class="presets" aria-label="Sun presets">
<button type="button" data-azimuth="120" data-elevation="42">LOW</button>
<button type="button" data-azimuth="140" data-elevation="52">MID</button>
<button type="button" data-azimuth="164" data-elevation="61">HIGH</button>
</div>
<label class="switch">
<input id="show-rays" type="checkbox" checked />
Show ray geometry
</label>
<button id="reset-view" type="button">Reset camera</button>
</section>
<section id="inset" class="panel" aria-label="Sun and canopy shadow geometry">
<div class="panel-heading">
<strong>Shadow geometry</strong>
<span class="status">INSET 01</span>
</div>
<p class="inset-subtitle">
A distant sun produces parallel rays. Continue those rays through the
canopy to locate the shadow on the bench plane.
</p>
<svg
class="diagram"
viewBox="0 0 340 180"
role="img"
aria-label="Diagram showing parallel sunlight rays blocked by a canopy and projected onto a bench"
>
<defs>
<marker
id="ray-arrow"
markerWidth="6"
markerHeight="6"
refX="5"
refY="3"
orient="auto"
>
<path d="M0,0 L6,3 L0,6 Z" fill="#d99a37" />
</marker>
</defs>
<!-- Distant sun -->
<circle cx="32" cy="31" r="13" fill="#e9b75d" />
<g stroke="#d99a37" stroke-width="2" stroke-linecap="round">
<path d="M32 10 V4" />
<path d="M32 58 V52" />
<path d="M11 31 H5" />
<path d="M59 31 H53" />
<path d="M17 16 L13 12" />
<path d="M47 46 L51 50" />
<path d="M17 46 L13 50" />
</g>
<text x="18" y="78" fill="#69766f" font-size="9" font-weight="700">
PARALLEL RAYS
</text>
<!-- Canopy and projected shadow -->
<polygon
points="82,60 202,60 212,75 92,75"
fill="#c96f4c"
stroke="#9f5138"
stroke-width="1.5"
/>
<text x="111" y="52" fill="#33463e" font-size="9" font-weight="800">
CANOPY
</text>
<polygon
points="92,75 212,75 322,145 202,145"
fill="#d99a37"
opacity="0.18"
/>
<!-- Incoming parallel rays -->
<g
fill="none"
stroke="#d99a37"
stroke-width="1.6"
stroke-dasharray="4 4"
marker-end="url(#ray-arrow)"
>
<path d="M44 23 L109 60" />
<path d="M82 23 L149 60" />
<path d="M122 23 L189 60" />
</g>
<!-- Projected continuations -->
<g
fill="none"
stroke="#d99a37"
stroke-width="1.4"
stroke-dasharray="3 5"
opacity="0.88"
>
<path d="M109 60 L220 126" />
<path d="M149 60 L260 126" />
<path d="M189 60 L300 126" />
</g>
<!-- Bench plane -->
<path
d="M155 145 H330"
stroke="#385247"
stroke-width="5"
stroke-linecap="round"
/>
<path
d="M235 145 V154 M305 145 V154"
stroke="#385247"
stroke-width="3"
/>
<text x="237" y="137" fill="#6c5a3a" font-size="9" font-weight="800">
SHADOW ON BENCH
</text>
</svg>
<div class="steps">
<div class="step">
<b>01</b>
<span>Sunlight arrives as parallel rays.</span>
</div>
<div class="step">
<b>02</b>
<span>The canopy blocks direct light.</span>
</div>
<div class="step">
<b>03</b>
<span>Projection defines the shaded area.</span>
</div>
</div>
<div class="equation">
<span>RAY</span>
<span>×</span>
<span class="accent">CANOPY</span>
<span>→</span>
<span>SHADE</span>
</div>
<p id="inset-note">
At the current angle, the canopy's projected footprint overlaps the seat
and backrest.
</p>
</section>
<div id="canopy-tag" class="scene-tag">VOXEL CANOPY</div>
<div id="bench-tag" class="scene-tag">BENCH / IN SHADE</div>
<div id="help">
<span>DRAG</span> orbit
<span>SCROLL</span> zoom
<span>DOUBLE-CLICK</span> reset
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Voxel courtyard. Drag to orbit and scroll to zoom."
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e3d7);
scene.fog = new THREE.Fog(0xe9e3d7, 40, 78);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(
0xfff4dc,
0x737b70,
1.65
);
scene.add(hemisphere);
const fill = new THREE.DirectionalLight(0xc9d9df, 0.65);
fill.position.set(15, 8, 18);
scene.add(fill);
const sun = new THREE.DirectionalLight(0xffe2aa, 3.7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
sun.shadow.radius = 2;
scene.add(sun);
scene.add(sun.target);
const sunFocus = new THREE.Vector3(2.25, 1.15, -1.15);
const roofCenter = new THREE.Vector3(0, 5.65, 1);
const roofWidth = 12;
const roofDepth = 8;
const benchCenter = new THREE.Vector3(2.25, 1.15, -1.15);
let sunDirection = new THREE.Vector3();
// ---------------------------------------------------------------------
// Materials and helpers
// ---------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const materials = {
base: material(0xbcb4a5),
roof: material(0xffffff, { vertexColors: true }),
wall: material(0xffffff, { vertexColors: true }),
post: material(0x76503b),
beam: material(0x5d4938),
wood: material(0xa86942),
woodLight: material(0xbc7c4e),
metal: material(0x3c514b),
soil: material(0x59483a),
planter: material(0xb8a68e),
leaf: material(0x5f7751),
leafLight: material(0x7f9564),
leafDark: material(0x435c43),
trunk: material(0x75513a)
};
function addBox(
width,
height,
depth,
x,
y,
z,
boxMaterial,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
boxMaterial
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
scene.add(mesh);
return mesh;
}
let seed = 731;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
const dummy = new THREE.Object3D();
// ---------------------------------------------------------------------
// Voxel paving
// ---------------------------------------------------------------------
addBox(37, 0.7, 29, 0, -0.46, 0, materials.base, false, true);
const columns = 18;
const rows = 14;
const tileGeometry = new THREE.BoxGeometry(1.91, 0.14, 1.91);
tileGeometry.setAttribute("color", new THREE.BufferAttribute(
new Float32Array(tileGeometry.getAttribute("position").count * 3).fill(1), 3
));
const tileMaterial = material(0xffffff, { vertexColors: true });
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
columns * rows
);
const tileColors = [
0xd8d1c2,
0xe1dacb,
0xcec8b9,
0xd5cdbc,
0xe4dccd
];
let tileIndex = 0;
for (let z = 0; z < rows; z++) {
for (let x = 0; x < columns; x++) {
dummy.position.set(
(x - (columns - 1) / 2) * 1.95,
-0.07,
(z - (rows - 1) / 2) * 1.95
);
dummy.rotation.y = 0;
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const color = new THREE.Color(
tileColors[Math.floor(random() * tileColors.length)]
);
color.offsetHSL(0, 0, (random() - 0.5) * 0.025);
tiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
// ---------------------------------------------------------------------
// Voxel courtyard walls
// ---------------------------------------------------------------------
const wallGeometry = new THREE.BoxGeometry(1.91, 1.26, 0.72);
wallGeometry.setAttribute("color", new THREE.BufferAttribute(
new Float32Array(wallGeometry.getAttribute("position").count * 3).fill(1), 3
));
const wallMaterial = material(0xffffff, { vertexColors: true });
const wallCount = 18 * 4 + 12 * 3;
const walls = new THREE.InstancedMesh(
wallGeometry,
wallMaterial,
wallCount
);
const wallColors = [0xd6cbb8, 0xd0c4b0, 0xdfd4c1, 0xc9bda9];
let wallIndex = 0;
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 18; col++) {
dummy.position.set(
(col - (18 - 1) / 2) * 1.95,
0.63 + row * 1.26,
-13.35
);
dummy.updateMatrix();
walls.setMatrixAt(wallIndex, dummy.matrix);
const color = new THREE.Color(
wallColors[Math.floor(random() * wallColors.length)]
);
color.offsetHSL(0, 0, (random() - 0.5) * 0.025);
walls.setColorAt(wallIndex++, color);
}
}
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 12; col++) {
dummy.position.set(
-17.45,
0.63 + row * 1.26,
-12.1 + col * 1.95
);
dummy.updateMatrix();
walls.setMatrixAt(wallIndex, dummy.matrix);
const color = new THREE.Color(
wallColors[Math.floor(random() * wallColors.length)]
);
color.offsetHSL(0, 0, (random() - 0.5) * 0.025);
walls.setColorAt(wallIndex++, color);
}
}
walls.instanceMatrix.needsUpdate = true;
walls.instanceColor.needsUpdate = true;
walls.castShadow = true;
walls.receiveShadow = true;
scene.add(walls);
// ---------------------------------------------------------------------
// Canopy: a continuous field of individual voxel roof modules
// ---------------------------------------------------------------------
const roofGeometry = new THREE.BoxGeometry(0.97, 0.38, 0.97);
roofGeometry.setAttribute("color", new THREE.BufferAttribute(
new Float32Array(roofGeometry.getAttribute("position").count * 3).fill(1), 3
));
const roofMaterial = material(0xffffff, { vertexColors: true });
const roof = new THREE.InstancedMesh(
roofGeometry,
roofMaterial,
12 * 8
);
let roofIndex = 0;
for (let z = 0; z < 8; z++) {
for (let x = 0; x < 12; x++) {
dummy.position.set(-5.5 + x, 5.65, -2.5 + z);
dummy.updateMatrix();
roof.setMatrixAt(roofIndex, dummy.matrix);
const checker = (x + z) % 3;
const color = new THREE.Color(
checker === 0
? 0xc66d4b
: checker === 1
? 0xd27b56
: 0xba6044
);
color.offsetHSL(0, 0, (random() - 0.5) * 0.025);
roof.setColorAt(roofIndex++, color);
}
}
roof.instanceMatrix.needsUpdate = true;
roof.instanceColor.needsUpdate = true;
roof.castShadow = true;
roof.receiveShadow = true;
scene.add(roof);
// Structural beams below the voxel modules.
addBox(12.4, 0.25, 0.3, 0, 5.33, -2.1, materials.beam);
addBox(12.4, 0.25, 0.3, 0, 5.33, 1.0, materials.beam);
addBox(12.4, 0.25, 0.3, 0, 5.33, 4.1, materials.beam);
addBox(0.3, 0.25, 8.3, -5.55, 5.33, 1, materials.beam);
addBox(0.3, 0.25, 8.3, 5.55, 5.33, 1, materials.beam);
// Four stacked-block posts.
const postGeometry = new THREE.BoxGeometry(0.65, 1.06, 0.65);
postGeometry.setAttribute("color", new THREE.BufferAttribute(
new Float32Array(postGeometry.getAttribute("position").count * 3).fill(1), 3
));
const postMaterial = material(0xffffff, { vertexColors: true });
const posts = new THREE.InstancedMesh(postGeometry, postMaterial, 20);
const postPositions = [
[-5.65, -2.35],
[5.65, -2.35],
[-5.65, 4.35],
[5.65, 4.35]
];
let postIndex = 0;
for (const [x, z] of postPositions) {
for (let level = 0; level < 5; level++) {
dummy.position.set(x, 0.53 + level * 1.06, z);
dummy.updateMatrix();
posts.setMatrixAt(postIndex, dummy.matrix);
const color = new THREE.Color(
level % 2 === 0 ? 0x76513b : 0x805a40
);
posts.setColorAt(postIndex++, color);
}
}
posts.instanceMatrix.needsUpdate = true;
posts.instanceColor.needsUpdate = true;
posts.castShadow = true;
posts.receiveShadow = true;
scene.add(posts);
// ---------------------------------------------------------------------
// Bench beneath the canopy
// ---------------------------------------------------------------------
const bx = benchCenter.x;
const bz = benchCenter.z;
// Five voxel seat slats.
for (let i = 0; i < 5; i++) {
addBox(
1.02,
0.28,
1.25,
bx - 2.16 + i * 1.08,
1.15,
bz,
i % 2 === 0 ? materials.wood : materials.woodLight
);
}
// Backrest slats.
addBox(5.45, 0.24, 0.25, bx, 1.72, bz - 0.77, materials.wood);
addBox(5.45, 0.24, 0.25, bx, 2.13, bz - 0.77, materials.woodLight);
// Supports and legs.
addBox(0.23, 1.65, 0.23, bx - 2.18, 1.32, bz - 0.76, materials.metal);
addBox(0.23, 1.65, 0.23, bx + 2.18, 1.32, bz - 0.76, materials.metal);
for (const xOffset of [-2.15, 2.15]) {
for (const zOffset of [-0.43, 0.43]) {
addBox(
0.22,
1.02,
0.22,
bx + xOffset,
0.51,
bz + zOffset,
materials.metal
);
}
}
// A small cube side table.
addBox(1.15, 1.05, 1.15, bx - 4.0, 0.525, bz + 0.15, materials.metal);
addBox(1.35, 0.16, 1.35, bx - 4.0, 1.12, bz + 0.15, materials.woodLight);
// ---------------------------------------------------------------------
// Voxel planters and tree
// ---------------------------------------------------------------------
function planter(x, z, scale = 1) {
addBox(
3.2 * scale,
0.78,
2.15 * scale,
x,
0.39,
z,
materials.planter
);
addBox(
2.85 * scale,
0.12,
1.82 * scale,
x,
0.8,
z,
materials.soil,
false,
true
);
const leafMaterials = [
materials.leaf,
materials.leafLight,
materials.leafDark
];
for (let i = 0; i < 12; i++) {
const size = 0.55 + random() * 0.65;
addBox(
size,
size,
size,
x + (random() - 0.5) * 2.0 * scale,
1.05 + random() * 1.3,
z + (random() - 0.5) * 1.2 * scale,
leafMaterials[Math.floor(random() * leafMaterials.length)]
);
}
}
planter(-11.5, -9.4, 1.05);
planter(10.3, -9.4, 1.05);
planter(12.2, -4.8, 0.72);
// A block-built courtyard tree.
addBox(0.72, 3.8, 0.72, -11.3, 1.9, 7.8, materials.trunk);
addBox(0.52, 1.25, 0.52, -10.65, 3.05, 7.8, materials.trunk);
const treeLeafMaterials = [
materials.leaf,
materials.leafLight,
materials.leafDark
];
for (let i = 0; i < 17; i++) {
const size = 1.05 + random() * 1.25;
addBox(
size,
size,
size,
-11.3 + (random() - 0.5) * 3.1,
3.9 + random() * 2.0,
7.8 + (random() - 0.5) * 2.7,
treeLeafMaterials[Math.floor(random() * treeLeafMaterials.length)]
);
}
// ---------------------------------------------------------------------
// Ray construction and projected canopy footprint
// ---------------------------------------------------------------------
const guideGroup = new THREE.Group();
scene.add(guideGroup);
function clearGuideGroup() {
while (guideGroup.children.length) {
const child = guideGroup.children[0];
guideGroup.remove(child);
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const list = Array.isArray(object.material)
? object.material
: [object.material];
list.forEach((item) => item.dispose());
}
});
}
}
function rebuildSunGuide() {
clearGuideGroup();
const direction = sunDirection.clone();
const roofY = roofCenter.y;
// Parallel rays that pass through the canopy region over the bench.
const roofSamples = [
new THREE.Vector3(-2.5, roofY, 1),
new THREE.Vector3(-0.5, roofY, 1),
new THREE.Vector3(1.5, roofY, 1)
];
roofSamples.forEach((point) => {
const start = point.clone().addScaledVector(direction, 9);
const ground = point
.clone()
.addScaledVector(direction, -roofY / direction.y);
const geometry = new THREE.BufferGeometry().setFromPoints([
start,
point,
ground
]);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color: 0xd99a37,
transparent: true,
opacity: 0.56,
dashSize: 0.28,
gapSize: 0.18,
depthTest: true
})
);
line.computeLineDistances();
guideGroup.add(line);
const arrow = new THREE.ArrowHelper(
direction.clone().negate(),
point.clone().addScaledVector(direction, 2.2),
1.35,
0xd99a37,
0.38,
0.2
);
guideGroup.add(arrow);
});
// Project the four canopy corners onto the paving.
const corners = [
new THREE.Vector3(-roofWidth / 2, 0, roofCenter.z - roofDepth / 2),
new THREE.Vector3(roofWidth / 2, 0, roofCenter.z - roofDepth / 2),
new THREE.Vector3(roofWidth / 2, 0, roofCenter.z + roofDepth / 2),
new THREE.Vector3(-roofWidth / 2, 0, roofCenter.z + roofDepth / 2)
].map((point) =>
point.addScaledVector(direction, -roofY / direction.y)
);
const outlinePoints = corners.map(
(point) => new THREE.Vector3(point.x, 0.11, point.z)
);
outlinePoints.push(outlinePoints[0].clone());
const outline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(outlinePoints),
new THREE.LineBasicMaterial({
color: 0xd99a37,
transparent: true,
opacity: 0.9
})
);
guideGroup.add(outline);
const positions = [];
const triangle = [0, 1, 2, 0, 2, 3];
triangle.forEach((index) => {
const point = corners[index];
positions.push(point.x, 0.095, point.z);
});
const footprintGeometry = new THREE.BufferGeometry();
footprintGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const footprint = new THREE.Mesh(
footprintGeometry,
new THREE.MeshBasicMaterial({
color: 0xd99a37,
transparent: true,
opacity: 0.09,
depthWrite: false,
side: THREE.DoubleSide
})
);
guideGroup.add(footprint);
}
// ---------------------------------------------------------------------
// Sun controls and projection readout
// ---------------------------------------------------------------------
const azimuthInput = document.querySelector("#azimuth");
const elevationInput = document.querySelector("#elevation");
const azimuthValue = document.querySelector("#azimuth-value");
const elevationValue = document.querySelector("#elevation-value");
const sunState = document.querySelector("#sun-state");
const insetNote = document.querySelector("#inset-note");
const showRays = document.querySelector("#show-rays");
function updateSun() {
const azimuth = Number(azimuthInput.value);
const elevation = Number(elevationInput.value);
const azimuthRadians = THREE.MathUtils.degToRad(azimuth);
const elevationRadians = THREE.MathUtils.degToRad(elevation);
sunDirection.set(
Math.cos(elevationRadians) * Math.cos(azimuthRadians),
Math.sin(elevationRadians),
Math.cos(elevationRadians) * Math.sin(azimuthRadians)
).normalize();
sun.position.copy(sunFocus).addScaledVector(sunDirection, 30);
sun.target.position.copy(sunFocus);
sun.target.updateMatrixWorld();
azimuthValue.textContent = `${Math.round(azimuth)}°`;
elevationValue.textContent = `${Math.round(elevation)}°`;
// The horizontal displacement from the roof down to the bench plane.
const drop = roofCenter.y - benchCenter.y;
const travel = drop / sunDirection.y;
const shiftX = -sunDirection.x * travel;
const shiftZ = -sunDirection.z * travel;
const projectedCenterX = roofCenter.x + shiftX;
const projectedCenterZ = roofCenter.z + shiftZ;
const distanceX = Math.abs(projectedCenterX - benchCenter.x);
const distanceZ = Math.abs(projectedCenterZ - benchCenter.z);
const overlaps = distanceX < 4.4 && distanceZ < 2.7;
sunState.textContent = overlaps ? "ROOF OVERLAP" : "PARTIAL SHADE";
sunState.style.background = overlaps ? "#dce5d9" : "#f1dfc8";
sunState.style.color = overlaps ? "#355444" : "#805b31";
insetNote.textContent = overlaps
? `The projected roof center is ${distanceX.toFixed(
1
)} m horizontally and ${distanceZ.toFixed(
1
)} m in depth from the bench center, so the seat remains in shade.`
: "At this angle, part of the canopy's projected footprint moves beyond the bench.";
rebuildSunGuide();
}
azimuthInput.addEventListener("input", updateSun);
elevationInput.addEventListener("input", updateSun);
showRays.addEventListener("change", () => {
guideGroup.visible = showRays.checked;
});
document.querySelectorAll(".presets button").forEach((button) => {
button.addEventListener("click", () => {
azimuthInput.value = button.dataset.azimuth;
elevationInput.value = button.dataset.elevation;
updateSun();
});
});
// ---------------------------------------------------------------------
// Custom orbit controls — no external controls module
// ---------------------------------------------------------------------
const desired = {
theta: 0.62,
phi: 1.15,
radius: 30,
target: new THREE.Vector3(0, 2.15, 0)
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius,
target: desired.target.clone()
};
function resetCamera() {
desired.theta = 0.62;
desired.phi = 1.15;
desired.radius = 30;
desired.target.set(0, 2.15, 0);
}
document.querySelector("#reset-view").addEventListener("click", resetCamera);
const pointers = new Map();
const canvas = renderer.domElement;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
canvas.focus({ preventScroll: true });
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const oldDistance = pointerDistance();
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi + dy * 0.005,
0.48,
1.38
);
} else if (pointers.size === 2) {
const newDistance = pointerDistance();
if (oldDistance > 0 && newDistance > 0) {
desired.radius = THREE.MathUtils.clamp(
desired.radius * (oldDistance / newDistance),
17,
43
);
}
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
17,
43
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
const step = event.shiftKey ? 0.12 : 0.06;
if (event.key === "ArrowLeft") desired.theta += step;
if (event.key === "ArrowRight") desired.theta -= step;
if (event.key === "ArrowUp") {
desired.phi = THREE.MathUtils.clamp(desired.phi - step, 0.48, 1.38);
}
if (event.key === "ArrowDown") {
desired.phi = THREE.MathUtils.clamp(desired.phi + step, 0.48, 1.38);
}
if (event.key === "+" || event.key === "=") {
desired.radius = Math.max(17, desired.radius - 1);
}
if (event.key === "-") {
desired.radius = Math.min(43, desired.radius + 1);
}
});
// ---------------------------------------------------------------------
// Screen-space scene labels
// ---------------------------------------------------------------------
const canopyLabel = document.querySelector("#canopy-tag");
const benchLabel = document.querySelector("#bench-tag");
const projected = new THREE.Vector3();
function updateLabel(element, position) {
projected.copy(position).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.exp(-8 * delta);
current.theta += (desired.theta - current.theta) * damping;
current.phi += (desired.phi - current.phi) * damping;
current.radius += (desired.radius - current.radius) * damping;
current.target.lerp(desired.target, damping);
const sinPhi = Math.sin(current.phi);
camera.position.set(
current.target.x +
current.radius * sinPhi * Math.sin(current.theta),
current.target.y + current.radius * Math.cos(current.phi),
current.target.z +
current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(current.target);
updateLabel(canopyLabel, new THREE.Vector3(0, 6.25, 1));
updateLabel(benchLabel, new THREE.Vector3(2.25, 2.65, -1.15));
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateSun();
animate();
</script>
</body>
</html>
```