Prompt specification
Build or customize the Three.js 3D scene "Neighborhood shade planning blocks" 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: Neighborhood shade planning blocks
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-shade-planning-blocks-x1255
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring sunlight and cast shadows. Build three blocks with different building heights and a small playground; compare shaded areas under the same sun direction with clear object silhouettes. Voxel style.
Working Three.js Starter (neighborhood-shade-planning-blocks-x1255):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Shadow Study</title>
<style>
:root {
--ink: #24323a;
--muted: #68777d;
--panel: rgba(250, 249, 243, 0.92);
--line: rgba(36, 50, 58, 0.14);
--sun: #f2a93b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8e8e8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 20px;
left: 20px;
z-index: 10;
width: min(340px, calc(100vw - 40px));
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(46, 65, 67, 0.15);
backdrop-filter: blur(16px);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: #65767b;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--sun);
content: "";
box-shadow: 0 0 0 3px rgba(242, 169, 59, 0.16);
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.04em;
}
.intro {
margin: 9px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.metrics {
display: grid;
gap: 8px;
}
.metric {
--accent: #eb7c62;
padding: 10px 11px;
border: 1px solid var(--line);
border-left: 4px solid var(--accent);
border-radius: 10px;
background: rgba(255, 255, 255, 0.62);
}
.metric-head,
.metric-value {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.metric-head {
margin-bottom: 7px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric-value {
font-size: 12px;
color: var(--muted);
}
.metric-value strong {
color: var(--ink);
font-size: 16px;
}
.bar {
height: 5px;
overflow: hidden;
border-radius: 5px;
background: rgba(36, 50, 58, 0.08);
}
.bar span {
display: block;
height: 100%;
border-radius: inherit;
background: var(--accent);
}
.note {
margin: 12px 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 7px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
button {
min-height: 34px;
padding: 0 11px;
border: 1px solid rgba(36, 50, 58, 0.15);
border-radius: 9px;
background: rgba(255, 255, 255, 0.8);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: white;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.toggle {
display: flex;
align-items: center;
gap: 8px;
min-height: 34px;
padding: 0 10px;
border: 1px solid rgba(36, 50, 58, 0.15);
border-radius: 9px;
background: rgba(255, 255, 255, 0.8);
font-size: 11px;
font-weight: 750;
cursor: pointer;
}
.toggle input {
accent-color: #2d8d82;
}
.sun-chip {
position: fixed;
top: 20px;
right: 20px;
z-index: 10;
display: flex;
align-items: center;
gap: 11px;
padding: 11px 14px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 14px;
background: rgba(250, 249, 243, 0.88);
box-shadow: 0 10px 30px rgba(46, 65, 67, 0.12);
backdrop-filter: blur(12px);
pointer-events: none;
}
.sun-icon {
display: grid;
width: 34px;
height: 34px;
place-items: center;
border-radius: 10px;
background: #fff0cf;
color: #d8871e;
font-size: 22px;
}
.sun-chip strong,
.sun-chip small {
display: block;
}
.sun-chip strong {
font-size: 11px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.sun-chip small {
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.hint {
position: fixed;
right: 20px;
bottom: 18px;
z-index: 10;
padding: 9px 12px;
border-radius: 10px;
background: rgba(36, 50, 58, 0.72);
color: white;
font-size: 10px;
letter-spacing: 0.03em;
pointer-events: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 5;
overflow: hidden;
pointer-events: none;
}
.scene-label {
position: absolute;
min-width: 112px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.88);
border-radius: 10px;
background: rgba(250, 249, 243, 0.91);
box-shadow: 0 7px 20px rgba(36, 50, 58, 0.14);
text-align: center;
transform: translate(-50%, -100%);
will-change: transform;
}
.scene-label::after {
position: absolute;
bottom: -5px;
left: calc(50% - 5px);
width: 10px;
height: 10px;
background: rgba(250, 249, 243, 0.91);
content: "";
transform: rotate(45deg);
}
.scene-label strong {
display: block;
color: var(--label-color, var(--ink));
font-size: 11px;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.scene-label span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.playground-label {
position: absolute;
padding: 5px 8px;
border-radius: 7px;
background: rgba(36, 50, 58, 0.76);
color: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transform: translate(-50%, -50%);
white-space: nowrap;
}
@media (max-width: 680px) {
.panel {
top: 10px;
left: 10px;
width: min(300px, calc(100vw - 20px));
padding: 14px;
}
h1 {
font-size: 21px;
}
.sun-chip {
top: auto;
right: 10px;
bottom: 10px;
}
.hint {
display: none;
}
.metric {
padding: 8px 9px;
}
}
</style>
</head>
<body>
<section class="panel">
<div class="eyebrow">Voxel shadow study</div>
<h1>One sun.<br>Three heights.</h1>
<p class="intro">
Identical footprints reveal how building height changes the projected
shaded area under a fixed afternoon sun.
</p>
<div class="metrics" id="metrics"></div>
<p class="note">
Values are building-only projections onto a level plane.
The playground and other objects also cast real-time shadows.
</p>
<div class="controls">
<button id="reset" type="button">↺ Reset view</button>
<button id="zoomOut" type="button" aria-label="Zoom out">−</button>
<button id="zoomIn" type="button" aria-label="Zoom in">+</button>
<label class="toggle">
<input id="shadowToggle" type="checkbox" checked>
Shadow outlines
</label>
</div>
</section>
<aside class="sun-chip">
<div class="sun-icon">☀</div>
<div>
<strong>Fixed sun vector</strong>
<small>50° elevation · shadows toward ↘</small>
</div>
</aside>
<div id="labels"></div>
<div class="hint">Drag to orbit · Shift/right-drag to pan · Scroll to zoom</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 camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8e8e8);
scene.fog = new THREE.Fog(0xd8e8e8, 42, 78);
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.05;
document.body.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-20, 20, 12, -12, 0.1, 100);
const controls = {
target: new THREE.Vector3(0, 1.1, 0),
radius: 35,
theta: 2.18,
phi: 1.0,
zoom: 1
};
function updateCamera() {
const { target, radius, theta, phi } = controls;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.cos(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.sin(theta)
);
camera.lookAt(target);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(23, 41 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
updateCamera();
}
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Lighting: one shared sun direction for the entire scene
// -------------------------------------------------------------------------
const sunVector = new THREE.Vector3(-16, 24, -12);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.25);
sun.position.copy(sunVector);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -27;
sun.shadow.camera.right = 27;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 80;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
const skyLight = new THREE.HemisphereLight(0xeaf8ff, 0xb7aa8d, 2.0);
scene.add(skyLight);
const fillLight = new THREE.DirectionalLight(0xc8e4ff, 0.45);
fillLight.position.set(12, 10, 15);
scene.add(fillLight);
// Ground-direction guide.
const shadowDirection = new THREE.Vector3(
-sunVector.x,
0,
-sunVector.z
).normalize();
const sunGuide = new THREE.ArrowHelper(
shadowDirection,
new THREE.Vector3(-18, 0.12, -10),
8,
0xe9a02f,
1.1,
0.65
);
sunGuide.line.material.transparent = true;
sunGuide.line.material.opacity = 0.75;
sunGuide.cone.material.transparent = true;
sunGuide.cone.material.opacity = 0.85;
scene.add(sunGuide);
// -------------------------------------------------------------------------
// Voxel construction helpers
// -------------------------------------------------------------------------
const edgeColor = 0x26343b;
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
...options
});
}
function box(
width,
height,
depth,
color,
x,
y,
z,
{
cast = true,
receive = true,
outline = true,
roughness = 0.88,
rotation = null
} = {}
) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = standardMaterial(color, { roughness });
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
if (rotation) {
mesh.rotation.set(rotation.x || 0, rotation.y || 0, rotation.z || 0);
}
if (outline) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 25),
new THREE.LineBasicMaterial({
color: edgeColor,
transparent: true,
opacity: 0.46
})
);
edges.renderOrder = 2;
mesh.add(edges);
}
scene.add(mesh);
return mesh;
}
function makeTree(x, z, scale = 1) {
box(
0.42 * scale, 1.25 * scale, 0.42 * scale,
0x79523d, x, 0.08 + 0.625 * scale, z
);
box(
1.7 * scale, 1.35 * scale, 1.7 * scale,
0x5d9b65, x, 0.08 + 1.65 * scale, z
);
box(
1.1 * scale, 0.75 * scale, 1.1 * scale,
0x75ae70, x - 0.2 * scale, 0.08 + 2.45 * scale, z + 0.1 * scale
);
box(
0.85 * scale, 0.65 * scale, 0.85 * scale,
0x4f865a, x + 0.35 * scale, 0.08 + 2.35 * scale, z - 0.2 * scale
);
}
function makeBench(x, z) {
box(1.7, 0.18, 0.55, 0x9b6943, x, 0.58, z);
box(1.7, 0.5, 0.14, 0xa87449, x, 0.86, z - 0.22);
box(0.16, 0.52, 0.42, 0x435057, x - 0.62, 0.32, z);
box(0.16, 0.52, 0.42, 0x435057, x + 0.62, 0.32, z);
}
// -------------------------------------------------------------------------
// Ground and three comparison blocks
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(48, 32),
standardMaterial(0xe8e3d5, { roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.03;
ground.receiveShadow = true;
scene.add(ground);
const buildingDefinitions = [
{
name: "Low",
x: -10,
z: -3.8,
height: 3.2,
width: 3.2,
depth: 3.0,
color: 0xec7964,
plotColor: 0xe9d9cd,
accent: "#ec7964"
},
{
name: "Medium",
x: 0,
z: -3.8,
height: 5.6,
width: 3.2,
depth: 3.0,
color: 0x39998d,
plotColor: 0xd8e2d8,
accent: "#2d9187"
},
{
name: "High",
x: 10,
z: -3.8,
height: 8.8,
width: 3.2,
depth: 3.0,
color: 0xe3a340,
plotColor: 0xe6dfc9,
accent: "#d9952e"
}
];
const plotTop = 0.08;
const buildingBase = plotTop;
function addBuildingWindows(definition) {
const floors = Math.max(2, Math.round(definition.height / 1.4));
const columns = 3;
const floorHeight = definition.height / floors;
const windowMaterial = standardMaterial(0xffe19a, {
roughness: 0.45,
emissive: 0x8d5b18,
emissiveIntensity: 0.18
});
for (let floor = 0; floor < floors; floor++) {
const y = buildingBase + (floor + 0.52) * floorHeight;
for (let column = 0; column < columns; column++) {
const x =
definition.x +
(column - (columns - 1) / 2) * 0.82;
// Front-facing windows.
const front = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 0.55, 0.07),
windowMaterial
);
front.position.set(
x,
y,
definition.z + definition.depth / 2 + 0.045
);
front.receiveShadow = true;
scene.add(front);
// Left-side windows, visible from the default camera.
const side = new THREE.Mesh(
new THREE.BoxGeometry(0.07, 0.55, 0.48),
windowMaterial
);
side.position.set(
definition.x - definition.width / 2 - 0.045,
y,
definition.z + (column - 1) * 0.78
);
side.receiveShadow = true;
scene.add(side);
}
}
}
for (const definition of buildingDefinitions) {
box(
6.4,
plotTop,
7.6,
definition.plotColor,
definition.x,
plotTop / 2,
definition.z,
{
cast: false,
outline: true
}
);
box(
definition.width,
definition.height,
definition.depth,
definition.color,
definition.x,
buildingBase + definition.height / 2,
definition.z
);
addBuildingWindows(definition);
// Identical corner trees make the blocks feel spatially related.
makeTree(definition.x - 2.35, -0.35, 0.9);
makeBench(definition.x + 2.25, -0.85);
// Small entrance marker.
box(
0.22,
0.22,
0.22,
0xf6d467,
definition.x + 1.95,
0.2,
definition.z + 2.05
);
}
// -------------------------------------------------------------------------
// Small voxel playground
// -------------------------------------------------------------------------
box(
8.4,
0.08,
4.0,
0xcbd9cf,
0.2,
0.04,
4.8,
{ cast: false }
);
// Sandbox.
box(
2.55,
0.16,
2.3,
0xe8c77f,
-1.65,
0.16,
4.75,
{ cast: false }
);
box(2.8, 0.35, 0.16, 0xa66f46, -1.65, 0.28, 3.63);
box(2.8, 0.35, 0.16, 0xa66f46, -1.65, 0.28, 5.87);
box(0.16, 0.35, 2.4, 0xa66f46, -3.0, 0.28, 4.75);
box(0.16, 0.35, 2.4, 0xa66f46, -0.3, 0.28, 4.75);
// Playground tower.
for (const dx of [-0.48, 0.48]) {
for (const dz of [-0.42, 0.42]) {
box(
0.18,
1.15,
0.18,
0x3f8290,
-1.65 + dx,
0.66,
4.05 + dz
);
}
}
box(1.35, 0.18, 1.2, 0xefb443, -1.65, 1.3, 4.05);
// Voxel stairs.
for (let step = 0; step < 4; step++) {
box(
0.9,
0.24,
0.38,
0xef9f42,
-1.65,
1.12 - step * 0.22,
4.75 + step * 0.38
);
}
// Straight slide board.
box(
0.9,
0.12,
1.9,
0xe85f56,
-1.65,
0.77,
5.52,
{
rotation: {
x: -0.48
}
}
);
// Swing frame.
box(0.2, 1.85, 0.2, 0x3d7d83, 1.1, 1.0, 5.0);
box(0.2, 1.85, 0.2, 0x3d7d83, 4.2, 1.0, 5.0);
box(3.45, 0.2, 0.22, 0x3d7d83, 2.65, 1.92, 5.0);
for (const seatX of [2.0, 3.25]) {
box(0.045, 1.12, 0.045, 0x48545a, seatX - 0.18, 1.25, 5.0);
box(0.045, 1.12, 0.045, 0x48545a, seatX + 0.18, 1.25, 5.0);
box(0.72, 0.12, 0.36, 0xf2b43d, seatX, 0.66, 5.0);
}
// Seesaw.
box(0.34, 0.36, 0.34, 0x65747a, 0.25, 0.29, 5.0);
box(
2.0,
0.14,
0.34,
0xee805d,
0.25,
0.58,
5.0,
{
rotation: {
z: 0.17
}
}
);
// -------------------------------------------------------------------------
// Projected shadow-area geometry
// -------------------------------------------------------------------------
function convexHull(points) {
const sorted = points
.map(point => ({ x: point.x, y: point.y }))
.sort((a, b) => a.x - b.x || a.y - b.y);
const cross = (o, a, b) =>
(a.x - o.x) * (b.y - o.y) -
(a.y - o.y) * (b.x - o.x);
const lower = [];
for (const point of sorted) {
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 = sorted.length - 1; i >= 0; i--) {
const point = sorted[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 polygonArea(points) {
let area = 0;
for (let i = 0; i < points.length; i++) {
const a = points[i];
const b = points[(i + 1) % points.length];
area += a.x * b.y - b.x * a.y;
}
return Math.abs(area) / 2;
}
const shadowOverlayGroup = new THREE.Group();
scene.add(shadowOverlayGroup);
function createShadowOverlay(definition) {
const halfWidth = definition.width / 2;
const halfDepth = definition.depth / 2;
// Horizontal displacement of the roof edge under the shared sun.
const shiftX =
(-sunVector.x / sunVector.y) * definition.height;
const shiftZ =
(-sunVector.z / sunVector.y) * definition.height;
const corners = [
{ x: -halfWidth, y: -halfDepth },
{ x: halfWidth, y: -halfDepth },
{ x: halfWidth, y: halfDepth },
{ x: -halfWidth, y: halfDepth }
];
const projectedPoints = [];
for (const corner of corners) {
projectedPoints.push(corner);
projectedPoints.push({
x: corner.x + shiftX,
y: corner.y + shiftZ
});
}
const hull = convexHull(projectedPoints);
const area = polygonArea(hull);
const shape = new THREE.Shape();
shape.moveTo(hull[0].x, -hull[0].y);
for (let i = 1; i < hull.length; i++) {
shape.lineTo(hull[i].x, -hull[i].y);
}
shape.closePath();
const fill = new THREE.Mesh(
new THREE.ShapeGeometry(shape),
new THREE.MeshBasicMaterial({
color: new THREE.Color(definition.accent),
transparent: true,
opacity: 0.14,
depthWrite: false,
side: THREE.DoubleSide
})
);
fill.rotation.x = -Math.PI / 2;
fill.position.set(definition.x, plotTop + 0.025, definition.z);
fill.renderOrder = 1;
shadowOverlayGroup.add(fill);
const linePoints = hull.map(point =>
new THREE.Vector3(
definition.x + point.x,
plotTop + 0.045,
definition.z + point.y
)
);
const outline = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(linePoints),
new THREE.LineDashedMaterial({
color: new THREE.Color(definition.accent),
dashSize: 0.28,
gapSize: 0.16,
transparent: true,
opacity: 0.95,
depthTest: true
})
);
outline.computeLineDistances();
outline.renderOrder = 4;
shadowOverlayGroup.add(outline);
return area;
}
const buildingAreas = buildingDefinitions.map(definition => ({
...definition,
area: createShadowOverlay(definition)
}));
// -------------------------------------------------------------------------
// Screen-space labels
// -------------------------------------------------------------------------
const labelLayer = document.getElementById("labels");
const buildingLabels = [];
for (const definition of buildingAreas) {
const label = document.createElement("div");
label.className = "scene-label";
label.style.setProperty("--label-color", definition.accent);
label.innerHTML = `
<strong>${definition.name} · ${definition.height.toFixed(1)} m</strong>
<span>${Math.round(definition.area)} m² projected shade</span>
`;
labelLayer.appendChild(label);
buildingLabels.push({
element: label,
position: new THREE.Vector3(
definition.x,
definition.height + 1.0,
definition.z
)
});
}
const playgroundLabel = document.createElement("div");
playgroundLabel.className = "playground-label";
playgroundLabel.textContent = "Playground";
labelLayer.appendChild(playgroundLabel);
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
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`;
}
// -------------------------------------------------------------------------
// Comparison cards
// -------------------------------------------------------------------------
const metricsElement = document.getElementById("metrics");
const maximumArea = Math.max(...buildingAreas.map(item => item.area));
for (const definition of buildingAreas) {
const metric = document.createElement("div");
metric.className = "metric";
metric.style.setProperty("--accent", definition.accent);
metric.innerHTML = `
<div class="metric-head">
<span>${definition.name} block</span>
<span>${definition.height.toFixed(1)} m high</span>
</div>
<div class="metric-value">
<span>Projected shade</span>
<strong>${Math.round(definition.area)} m²</strong>
</div>
<div class="bar">
<span style="width: ${(definition.area / maximumArea) * 100}%"></span>
</div>
`;
metricsElement.appendChild(metric);
}
// -------------------------------------------------------------------------
// Direct camera controls — no controls addon
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
let drag = null;
canvas.addEventListener("contextmenu", event => event.preventDefault());
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0 && event.button !== 2) return;
drag = {
x: event.clientX,
y: event.clientY,
mode: event.shiftKey || event.button === 2 ? "pan" : "orbit"
};
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
if (!drag) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
if (drag.mode === "orbit") {
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.5,
1.32
);
} else {
const worldPerPixel =
(camera.top - camera.bottom) /
camera.zoom /
canvas.clientHeight;
const right = new THREE.Vector3()
.setFromMatrixColumn(camera.matrixWorld, 0)
.setY(0)
.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3());
const up = new THREE.Vector3()
.crossVectors(right, forward)
.normalize();
controls.target.addScaledVector(right, -dx * worldPerPixel);
controls.target.addScaledVector(up, dy * worldPerPixel);
controls.target.x = THREE.MathUtils.clamp(
controls.target.x, -10, 10
);
controls.target.z = THREE.MathUtils.clamp(
controls.target.z, -8, 8
);
controls.target.y = THREE.MathUtils.clamp(
controls.target.y, 0.5, 2.5
);
}
drag.x = event.clientX;
drag.y = event.clientY;
updateCamera();
});
function stopDrag(event) {
drag = null;
canvas.style.cursor = "grab";
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDrag);
canvas.addEventListener("pointercancel", stopDrag);
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.4
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset").addEventListener("click", () => {
controls.target.set(0, 1.1, 0);
controls.theta = 2.18;
controls.phi = 1.0;
controls.zoom = 1;
updateCamera();
});
document.getElementById("zoomIn").addEventListener("click", () => {
controls.zoom = Math.min(2.4, controls.zoom * 1.15);
updateCamera();
});
document.getElementById("zoomOut").addEventListener("click", () => {
controls.zoom = Math.max(0.65, controls.zoom / 1.15);
updateCamera();
});
document.getElementById("shadowToggle").addEventListener("change", event => {
shadowOverlayGroup.visible = event.target.checked;
});
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
resize();
function animate() {
renderer.render(scene, camera);
for (const label of buildingLabels) {
positionLabel(label.element, label.position);
}
positionLabel(
playgroundLabel,
new THREE.Vector3(0.25, 2.35, 5.0)
);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```